25#ifndef LSST_GAUSS2D_ELLIPSE_H
26#define LSST_GAUSS2D_ELLIPSE_H
36namespace lsst::gauss2d {
38const double M_HWHM_SIGMA = 1.1774100225154746910115693264599;
39const double M_SIGMA_HWHM = 0.84932180028801904272150283410295;
40const double M_PI_180 = M_PI / 180.;
41const double M_180_PI = 180. / M_PI;
66 explicit Covariance(
double sigma_x_sq = 0,
double sigma_y_sq = 0,
double cov_xy = 0);
71 static void check(
double sigma_x_sq,
double sigma_y_sq,
double cov_xy);
81 std::array<double, 3>
get_xyc()
const {
return {_sigma_x_sq, _sigma_y_sq, _cov_xy}; }
99 void set(
double sigma_x_sq = 0,
double sigma_y_sq = 0,
double cov_xy = 0);
107 void set_xyc(
const std::array<double, 3>& xyc);
109 std::string
repr(
bool name_keywords =
false,
111 std::string
str()
const override;
113 bool operator==(
const Covariance& other)
const;
114 bool operator!=(
const Covariance& other)
const;
116 friend std::ostream& operator<<(std::ostream& out,
const Covariance& obj);
119 double _sigma_x_sq = 0;
120 double _sigma_y_sq = 0;
137 static void check(
double size_x,
double size_y,
double rho, std::string_view error_suffix =
"") {
138 if (!(size_x >= 0) || !(size_y >= 0) || !(rho >= -1 && rho <= 1)) {
139 throw std::invalid_argument(
"Invalid size_x, size_y, rho=" + to_string_float(size_x) +
","
140 + to_string_float(size_y) +
"," + to_string_float(rho)
141 +
"; sigma_x,y >= 0 and 1 >= rho >= -1 required."
142 + std::string(error_suffix));
146 static void check_size(
double size, std::string_view error_suffix =
"") {
148 throw std::invalid_argument(
"Invalid size=" + to_string_float(size) +
"; size >= 0 required."
149 + std::string(error_suffix));
153 static void check_rho(
double rho, std::string_view error_suffix =
"") {
154 if (!(rho >= -1 && rho <= 1)) {
155 throw std::invalid_argument(
"Invalid rho=" + to_string_float(rho) +
"; 1 >= rho >= -1 required."
156 + std::string(error_suffix));
184 virtual std::array<double, 3>
get_hxyr()
const;
186 virtual std::array<double, 3>
get_xyr()
const;
189 virtual void set(
double sigma_x,
double sigma_y,
double rho);
195 virtual void set_h(
double hwhm_x,
double hwhm_y,
double rho);
207 virtual void set_hxyr(
const std::array<double, 3>& hxyr);
209 virtual void set_xyr(
const std::array<double, 3>& xyr);
211 virtual std::string
repr(
bool name_keywords =
false, std::string_view namespace_separator
214 virtual std::string
str()
const override = 0;
217 bool operator!=(
const EllipseData& other)
const {
return !(*
this == other); };
219 friend std::ostream& operator<<(std::ostream& out,
const EllipseData& obj) {
241 explicit EllipseValues(std::shared_ptr<double> sigma_x, std::shared_ptr<double> sigma_y,
242 std::shared_ptr<double> rho =
nullptr)
243 : _sigma_x(sigma_x == nullptr ? std::make_shared<double>(0) : std::move(sigma_x)),
244 _sigma_y(sigma_y == nullptr ? std::make_shared<double>(0) : std::move(sigma_y)),
245 _rho(rho == nullptr ? std::make_shared<double>(0) : std::move(rho)) {};
247 explicit EllipseValues(
double sigma_x = 0,
double sigma_y = 0,
double rho = 0)
248 : _sigma_x(std::make_shared<double>(sigma_x)),
249 _sigma_y(std::make_shared<double>(sigma_y)),
250 _rho(std::make_shared<double>(rho)) {};
254 double get_rho()
const override;
255 std::array<double, 3>
get_xyr()
const override;
257 void set(
double sigma_x,
double sigma_y,
double rho)
override;
258 void set_h(
double hwhm_x,
double hwhm_y,
double rho)
override;
261 void set_rho(
double rho)
override;
263 std::string
repr(
bool name_keywords =
false,
265 std::string
str()
const override;
268 std::shared_ptr<double> _sigma_x;
269 std::shared_ptr<double> _sigma_y;
270 std::shared_ptr<double> _rho;
285 explicit Ellipse(std::shared_ptr<EllipseData> data);
293 explicit Ellipse(
double sigma_x = 0,
double sigma_y = 0,
double rho = 0);
299 double get_rho()
const override;
315 void set_rho(
double rho)
override;
319 std::string
repr(
bool name_keywords =
false,
321 std::string
str()
const override;
323 bool operator==(
const Ellipse& other)
const;
324 bool operator!=(
const Ellipse& other)
const;
327 std::shared_ptr<EllipseData> _data;
347 explicit EllipseMajor(
double r_major,
double axrat,
double angle,
bool degrees =
false);
352 static void check(
double r_major,
double axrat,
double angle) {
353 if (!(r_major >= 0) || !(axrat >= 0 && axrat <= 1)) {
354 throw std::invalid_argument(
"Invalid r_major, axrat, angle=" + std::to_string(r_major) +
","
355 + std::to_string(axrat) +
"," + std::to_string(angle)
356 +
"; r_major >= 0, 1 >= axrat >= 0 required.");
360 double get_area()
const {
return M_PI * _r_major * _r_major * _axrat; }
372 std::array<double, 3>
get_rqa()
const {
return {_r_major, _axrat, _angle}; }
376 void set(
double r_major,
double axrat,
double angle);
377 void set_r_major(
double r_major);
378 void set_axrat(
double axrat);
379 void set_angle(
double angle);
380 void set_degrees(
bool degrees);
381 void set_rqa(
const std::array<double, 3>& rqa);
383 std::string
repr(
bool name_keywords =
false,
385 std::string
str()
const override;
391 double _r_major = 0.;
394 bool _degrees =
false;
A representation of a 2D Gaussian with x and y standard deviations and a covariance value.
Definition ellipse.h:57
std::shared_ptr< Covariance > make_convolution(const Covariance &cov) const
Return the convolution of this with another covariance.
Definition ellipse.cc:66
double get_cov_xy() const
Get the covariance.
Definition ellipse.h:79
double get_sigma_x_sq() const
Get the square of sigma_x.
Definition ellipse.h:75
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition ellipse.cc:122
void set_sigma_y_sq(double sigma_y_sq)
Set the square of sigma_y.
Definition ellipse.cc:97
void set_sigma_x_sq(double sigma_x_sq)
Set the square of sigma_x.
Definition ellipse.cc:89
void set_xyc(const std::array< double, 3 > &xyc)
Set sigma_x_sq, sigma_y_sq and cov_xy from an array ref.
Definition ellipse.cc:120
std::array< double, 3 > get_xyc() const
Get the array of sigma_x^2, sigma_y^2, covariance.
Definition ellipse.h:81
std::string str() const override
Return a brief, human-readable string representation of this.
Definition ellipse.cc:129
double get_sigma_y_sq() const
Get the square of sigma_y.
Definition ellipse.h:77
void convolve(const Covariance &cov)
Convolve with another covariance, adding the values of each parameter to this.
Definition ellipse.cc:59
std::unique_ptr< Covariance > make_convolution_uniq(const Covariance &cov) const
Same as make_convolution(), but returning a unique_ptr.
static void check(double sigma_x_sq, double sigma_y_sq, double cov_xy)
Check whether the supplied values are valid, throwing if not.
Definition ellipse.cc:46
void set_cov_xy(double cov_xy)
Set the off-diagonal (covariance) term.
Definition ellipse.cc:105
void set(const Ellipse &ellipse)
Set values from an ellipse instance.
Definition ellipse.cc:73
Covariance(double sigma_x_sq=0, double sigma_y_sq=0, double cov_xy=0)
Construct a new Covariance object.
Definition ellipse.cc:40
Interface for an object storing Ellipse data.
Definition ellipse.h:132
virtual double get_sigma_x_sq() const
Get the square of sigma_x.
Definition ellipse.cc:175
static void check_rho(double rho, std::string_view error_suffix="")
Check whether a rho value is valid.
Definition ellipse.h:153
virtual double get_rho() const =0
Get rho.
virtual void set_rho(double rho)=0
Set the correlation parameter (rho)
static void check_size(double size, std::string_view error_suffix="")
Check whether an x- or x-yaxis size value is valid.
Definition ellipse.h:146
virtual void set_hwhm_y(double hwhm_y)
Set the y-axis half-width at half-max (FWHM/2)
Definition ellipse.cc:304
virtual void set(double sigma_x, double sigma_y, double rho)
Set sigma_x, sigma_y, rho.
Definition ellipse.cc:256
virtual void set_h(double hwhm_x, double hwhm_y, double rho)
Set hwhm_x, hwhm_y, rho (half-width at half-max)
Definition ellipse.cc:297
virtual void convolve(const Ellipse &ell)
Convolve this ellipse with another.
Definition ellipse.cc:149
virtual void set_hwhm_x(double hwhm_x)
Set the x-axis half-width at half-max (FWHM/2)
Definition ellipse.cc:303
virtual double get_sigma_xy() const
Return sigma_x*sigma_y.
Definition ellipse.cc:147
virtual double get_sigma_y_sq() const
Get the square of sigma_y.
Definition ellipse.cc:180
virtual void set_xyr(const std::array< double, 3 > &xyr)
Set sigma_x, sigma_y, rho from an array.
Definition ellipse.cc:306
virtual void set_sigma_y(double sigma_y)=0
Set the y-axis dispersion (sigma)
virtual std::array< double, 3 > get_hxyr() const
Get hwhm_x, hwhm_y, rho.
Definition ellipse.cc:166
virtual double get_cov_xy() const
Return the covariance, equal to sigma_x*sigma_y*rho.
Definition ellipse.cc:160
static void check(double size_x, double size_y, double rho, std::string_view error_suffix="")
Check whether Ellipse parameter values are valid, throwing if not.
Definition ellipse.h:137
virtual double get_radius_trace() const
Return the trace radius, equal to sqrt(sigma_x^2 + sigma_y^2)
Definition ellipse.cc:173
virtual void set_hxyr(const std::array< double, 3 > &hxyr)
Set hwhm_x, hwhm_y, rho from an array.
Definition ellipse.cc:305
virtual std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override=0
virtual double get_hwhm_y() const
Get the y-axis half-width at half-maximum.
Definition ellipse.cc:164
virtual double get_area() const
Return the area of this ellipse, equal to pi*sigma_major*sigma_minor.
Definition ellipse.cc:142
virtual std::string str() const override=0
Return a brief, human-readable string representation of this.
virtual std::array< double, 3 > get_xyr() const
Get sigma_x, sigma_y, rho.
Definition ellipse.cc:169
virtual double get_sigma_y() const =0
Get sigma_y.
virtual double get_hwhm_x() const
Get the x-axis half-width at half-maximum.
Definition ellipse.cc:162
virtual void set_sigma_x(double sigma_x)=0
Set the x-axis dispersion (sigma)
virtual double get_sigma_x() const =0
Get sigma_x.
An Ellipse with r_major, axrat and angle values.
Definition ellipse.h:337
double get_axrat() const
Get the axis ratio.
Definition ellipse.h:364
double get_r_major() const
Get the major axis length.
Definition ellipse.h:362
double get_angle_radians() const
Get the position angle in radians.
Definition ellipse.h:370
bool is_degrees() const
Return if the units are degrees (true) or radians (false)
Definition ellipse.h:374
static void check(double r_major, double axrat, double angle)
Check whether the supplied values are valid, throwing if not.
Definition ellipse.h:352
double get_area() const
Return the area of this ellipse, equal to pi*sigma_major*sigma_minor.
Definition ellipse.h:360
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition ellipse.cc:411
std::array< double, 3 > get_rqa() const
Get the array of r_major, axrat, angle.
Definition ellipse.h:372
double get_angle() const
Get the position angle in the configured units.
Definition ellipse.h:366
double get_angle_degrees() const
Get the position angle in degrees.
Definition ellipse.h:368
std::string str() const override
Return a brief, human-readable string representation of this.
Definition ellipse.cc:418
EllipseMajor(double r_major, double axrat, double angle, bool degrees=false)
Construct a new EllipseMajor object with default float values.
Definition ellipse.cc:361
EllipseValues(std::shared_ptr< double > sigma_x, std::shared_ptr< double > sigma_y, std::shared_ptr< double > rho=nullptr)
Construct a new EllipseValues object.
Definition ellipse.h:241
void set_rho(double rho) override
Set the correlation parameter (rho)
Definition ellipse.cc:200
void set_h(double hwhm_x, double hwhm_y, double rho) override
Set hwhm_x, hwhm_y, rho (half-width at half-max)
Definition ellipse.cc:212
double get_rho() const override
Get rho.
Definition ellipse.cc:187
std::string str() const override
Return a brief, human-readable string representation of this.
Definition ellipse.cc:226
EllipseValues(double sigma_x=0, double sigma_y=0, double rho=0)
Construct a new EllipseValues object, creating new pointers for every value.
Definition ellipse.h:247
std::array< double, 3 > get_xyr() const override
Get sigma_x, sigma_y, rho.
Definition ellipse.cc:188
void set(double sigma_x, double sigma_y, double rho) override
Set sigma_x, sigma_y, rho.
Definition ellipse.cc:205
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition ellipse.cc:219
void set_sigma_x(double sigma_x) override
Set the x-axis dispersion (sigma)
Definition ellipse.cc:190
double get_sigma_y() const override
Get sigma_y.
Definition ellipse.cc:186
void set_sigma_y(double sigma_y) override
Set the y-axis dispersion (sigma)
Definition ellipse.cc:195
double get_sigma_x() const override
Get sigma_x.
Definition ellipse.cc:185
An Ellipse with sigma_x, sigma_y, and rho values.
Definition ellipse.h:283
std::shared_ptr< Ellipse > make_convolution(const Ellipse &ell) const
Return the convolution of this with another ellipse.
Definition ellipse.cc:245
double get_rho() const override
Get rho.
Definition ellipse.cc:319
std::unique_ptr< Ellipse > make_convolution_uniq(const Ellipse &ell) const
Same as make_convolution(), but returning a unique_ptr.
Definition ellipse.cc:248
void set_rho(double rho) override
Set the correlation parameter (rho)
Definition ellipse.cc:323
void set_sigma_y(double sigma_y) override
Set the y-axis dispersion (sigma)
Definition ellipse.cc:325
void set_sigma_x(double sigma_x) override
Set the x-axis dispersion (sigma)
Definition ellipse.cc:324
const EllipseData & get_data() const
Return a const ref to this ellipse's data.
Definition ellipse.cc:318
double get_sigma_x() const override
Get sigma_x.
Definition ellipse.cc:320
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition ellipse.cc:308
std::string str() const override
Return a brief, human-readable string representation of this.
Definition ellipse.cc:313
double get_sigma_y() const override
Get sigma_y.
Definition ellipse.cc:321
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:45