LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
ellipse.h
Go to the documentation of this file.
1// -*- LSST-C++ -*-
2/*
3 * This file is part of gauss2d.
4 *
5 * Developed for the LSST Data Management System.
6 * This product includes software developed by the LSST Project
7 * (https://www.lsst.org).
8 * See the COPYRIGHT file at the top-level directory of this distribution
9 * for details of code ownership.
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25#ifndef LSST_GAUSS2D_ELLIPSE_H
26#define LSST_GAUSS2D_ELLIPSE_H
27
28#include <array>
29#include <cmath>
30#include <memory>
31#include <stdexcept>
32
33#include "object.h"
34#include "to_string.h"
35
36namespace lsst::gauss2d {
37
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;
42
43class Ellipse;
44class EllipseMajor;
45
57class Covariance : public Object {
58public:
66 explicit Covariance(double sigma_x_sq = 0, double sigma_y_sq = 0, double cov_xy = 0);
68 explicit Covariance(const Ellipse& ell);
69
71 static void check(double sigma_x_sq, double sigma_y_sq, double cov_xy);
73 void convolve(const Covariance& cov);
75 double get_sigma_x_sq() const { return _sigma_x_sq; };
77 double get_sigma_y_sq() const { return _sigma_y_sq; };
79 double get_cov_xy() const { return _cov_xy; };
81 std::array<double, 3> get_xyc() const { return {_sigma_x_sq, _sigma_y_sq, _cov_xy}; }
82
95
97 void set(const Ellipse& ellipse);
99 void set(double sigma_x_sq = 0, double sigma_y_sq = 0, double cov_xy = 0);
101 void set_sigma_x_sq(double sigma_x_sq);
103 void set_sigma_y_sq(double sigma_y_sq);
105 void set_cov_xy(double cov_xy);
107 void set_xyc(const std::array<double, 3>& xyc);
108
109 std::string repr(bool name_keywords = false,
110 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
111 std::string str() const override;
112
113 bool operator==(const Covariance& other) const;
114 bool operator!=(const Covariance& other) const;
115
116 friend std::ostream& operator<<(std::ostream& out, const Covariance& obj);
117
118private:
119 double _sigma_x_sq = 0;
120 double _sigma_y_sq = 0;
121 double _cov_xy = 0;
122};
123
132class EllipseData : public Object {
133public:
134 virtual ~EllipseData() = default;
135
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));
143 }
144 }
146 static void check_size(double size, std::string_view error_suffix = "") {
147 if (!(size >= 0)) {
148 throw std::invalid_argument("Invalid size=" + to_string_float(size) + "; size >= 0 required."
149 + std::string(error_suffix));
150 }
151 }
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));
157 }
158 }
160 virtual void convolve(const Ellipse& ell);
162 virtual double get_area() const;
164 virtual double get_cov_xy() const;
166 virtual double get_hwhm_x() const;
168 virtual double get_hwhm_y() const;
170 virtual double get_radius_trace() const;
172 virtual double get_sigma_x() const = 0;
174 virtual double get_sigma_y() const = 0;
176 virtual double get_rho() const = 0;
178 virtual double get_sigma_x_sq() const;
180 virtual double get_sigma_y_sq() const;
182 virtual double get_sigma_xy() const;
184 virtual std::array<double, 3> get_hxyr() const;
186 virtual std::array<double, 3> get_xyr() const;
187
189 virtual void set(double sigma_x, double sigma_y, double rho);
191 virtual void set(const Covariance& covar);
193 virtual void set(const EllipseMajor& ellipse);
195 virtual void set_h(double hwhm_x, double hwhm_y, double rho);
197 virtual void set_hwhm_x(double hwhm_x);
199 virtual void set_hwhm_y(double hwhm_y);
201 virtual void set_sigma_x(double sigma_x) = 0;
203 virtual void set_sigma_y(double sigma_y) = 0;
205 virtual void set_rho(double rho) = 0;
207 virtual void set_hxyr(const std::array<double, 3>& hxyr);
209 virtual void set_xyr(const std::array<double, 3>& xyr);
210
211 virtual std::string repr(bool name_keywords = false, std::string_view namespace_separator
212 = Object::CC_NAMESPACE_SEPARATOR) const override
213 = 0;
214 virtual std::string str() const override = 0;
215
216 bool operator==(const EllipseData& other) const { return get_xyr() == other.get_xyr(); };
217 bool operator!=(const EllipseData& other) const { return !(*this == other); };
218
220 out << obj.str();
221 return out;
222 }
223};
224
233public:
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)) {};
251
252 double get_sigma_x() const override;
253 double get_sigma_y() const override;
254 double get_rho() const override;
255 std::array<double, 3> get_xyr() const override;
256
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;
259 void set_sigma_x(double sigma_x) override;
260 void set_sigma_y(double sigma_y) override;
261 void set_rho(double rho) override;
262
263 std::string repr(bool name_keywords = false,
264 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
265 std::string str() const override;
266
267private:
271};
272
283class Ellipse : public EllipseData {
284public:
293 explicit Ellipse(double sigma_x = 0, double sigma_y = 0, double rho = 0);
294 explicit Ellipse(const Covariance& covar);
295 explicit Ellipse(const EllipseMajor& ellipse);
296
298 const EllipseData& get_data() const;
299 double get_rho() const override;
300 double get_sigma_x() const override;
301 double get_sigma_y() const override;
314
315 void set_rho(double rho) override;
316 void set_sigma_x(double sigma_x) override;
317 void set_sigma_y(double sigma_y) override;
318
319 std::string repr(bool name_keywords = false,
320 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
321 std::string str() const override;
322
323 bool operator==(const Ellipse& other) const;
324 bool operator!=(const Ellipse& other) const;
325
326private:
328};
329
337class EllipseMajor : public Object {
338public:
347 explicit EllipseMajor(double r_major, double axrat, double angle, bool degrees = false);
348 explicit EllipseMajor(const Covariance& covar, bool degrees = false);
349 explicit EllipseMajor(const Ellipse& ellipse, bool degrees = false);
350
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.");
357 }
358 }
360 double get_area() const { return M_PI * _r_major * _r_major * _axrat; }
362 double get_r_major() const { return _r_major; }
364 double get_axrat() const { return _axrat; }
366 double get_angle() const { return _angle; }
368 double get_angle_degrees() const { return _degrees ? _angle : _angle * M_180_PI; }
370 double get_angle_radians() const { return _degrees ? _angle * M_PI_180 : _angle; }
372 std::array<double, 3> get_rqa() const { return {_r_major, _axrat, _angle}; }
374 bool is_degrees() const { return _degrees; }
375
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);
382
383 std::string repr(bool name_keywords = false,
384 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
385 std::string str() const override;
386
387 bool operator==(const EllipseMajor& other) const;
388 bool operator!=(const EllipseMajor& other) const;
389
390private:
391 double _r_major = 0.;
392 double _axrat = 1.;
393 double _angle = 0.;
394 bool _degrees = false;
395};
396
397} // namespace lsst::gauss2d
398#endif
char * data
Definition BaseRecord.cc:61
table::Key< double > angle
#define M_PI
Definition ListMatch.cc:31
A representation of a 2D Gaussian with x and y standard deviations and a covariance value.
Definition ellipse.h:57
bool operator!=(const Covariance &other) const
Definition ellipse.cc:135
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
Return a full, callable string representation of this.
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
Covariance(double sigma_x_sq=0, double sigma_y_sq=0, double cov_xy=0)
Construct a new Covariance object.
Definition ellipse.cc:40
friend std::ostream & operator<<(std::ostream &out, const Covariance &obj)
Definition ellipse.cc:137
bool operator==(const Covariance &other) const
Definition ellipse.cc:134
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 ~EllipseData()=default
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
bool operator==(const EllipseData &other) const
Definition ellipse.h:216
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
bool operator!=(const EllipseData &other) const
Definition ellipse.h:217
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
Return a full, callable string representation of this.
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
friend std::ostream & operator<<(std::ostream &out, const EllipseData &obj)
Definition ellipse.h:219
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 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
bool operator!=(const Ellipse &other) const
Definition ellipse.cc:316
bool operator==(const Ellipse &other) const
Definition ellipse.cc:315
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
Ellipse(std::shared_ptr< EllipseData > data)
Definition ellipse.cc:232
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
Return a full, callable string representation of this.
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
An Ellipse with r_major, axrat and angle values.
Definition ellipse.h:337
bool operator!=(const EllipseMajor &other) const
Definition ellipse.cc:429
void set_degrees(bool degrees)
Definition ellipse.cc:399
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
void set_angle(double angle)
Definition ellipse.cc:397
double get_area() const
Return the area of this ellipse, equal to pi*sigma_major*sigma_minor.
Definition ellipse.h:360
void set_rqa(const std::array< double, 3 > &rqa)
Definition ellipse.cc:409
bool operator==(const EllipseMajor &other) const
Definition ellipse.cc:424
void set_r_major(double r_major)
Definition ellipse.cc:381
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition ellipse.cc:411
void set_axrat(double axrat)
Definition ellipse.cc:389
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
An EllipseData storing sigma_x, sigma_y, rho values as shared_ptrs.
Definition ellipse.h:232
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
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
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
A generic object from the gauss2d library.
Definition object.h:40
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:45
daf::base::PropertySet * set
Definition fits.cc:931
const double M_PI_180
Definition ellipse.h:40
const double M_SIGMA_HWHM
Definition ellipse.h:39
const double M_HWHM_SIGMA
Definition ellipse.h:38
const double M_180_PI
Definition ellipse.h:41
std::string to_string_float(const T value, const int precision=6, const bool scientific=true)
Definition to_string.h:15
STL namespace.
T to_string(T... args)