LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
LSST Data Management Base Package
Loading...
Searching...
No Matches
gaussian.cc
Go to the documentation of this file.
1/*
2 * This file is part of gauss2d.
3 *
4 * Developed for the LSST Data Management System.
5 * This product includes software developed by the LSST Project
6 * (https://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef LSST_GAUSS2D_GAUSSIAN_H
25
26#include <optional>
27#include <stdexcept>
28
32
33namespace lsst::gauss2d {
34
35GaussianIntegralValue::GaussianIntegralValue(double value) : _value(std::make_shared<double>(value)) {};
37 : _value(value == nullptr ? std::make_shared<double>(1) : std::move(value)) {};
38
39std::string GaussianIntegralValue::repr(bool name_keywords, std::string_view namespace_separator) const {
40 return type_name_str<GaussianIntegralValue>(false, namespace_separator) + "("
41 + (name_keywords ? "value=" : "") + to_string_float(*_value) + ")";
42}
44 return type_name_str<GaussianIntegralValue>(true) + "(value=" + to_string_float(*_value) + ")";
45}
46
49 : _centroid(centroid != nullptr ? std::move(centroid) : std::make_shared<Centroid>()),
50 _ellipse(ellipse != nullptr ? std::move(ellipse) : std::make_shared<Ellipse>()),
51 _integral(integral != nullptr ? std::move(integral) : std::make_shared<GaussianIntegralValue>()) {}
53
54double Gaussian::get_const_normal() const { return _integral->get_value() / (2 * _ellipse->get_area()); }
55double Gaussian::get_integral_value() const { return _integral->get_value(); };
56
57Centroid& Gaussian::get_centroid() { return *_centroid; }
58Ellipse& Gaussian::get_ellipse() { return *_ellipse; }
60
64
65const Centroid& Gaussian::get_centroid_const() const { return *_centroid; }
66const Ellipse& Gaussian::get_ellipse_const() const { return *_ellipse; }
67const GaussianIntegral& Gaussian::get_integral_const() const { return *_integral; }
68
69void Gaussian::set_const_normal(double const_normal) {
70 _integral->set_value(get_const_normal() * 2 * _ellipse->get_area());
71}
72void Gaussian::set_integral_value(double integral) { _integral->set_value(integral); }
73
75 _centroid = this->_check_not_nullptr<Centroid>(centroid, "centroid");
76}
78 _ellipse = this->_check_not_nullptr<Ellipse>(ellipse, "ellipse");
79}
81 _integral = this->_check_not_nullptr<GaussianIntegral>(integral, "integral");
82}
83
84std::string Gaussian::repr(bool name_keywords, std::string_view namespace_separator) const {
85 return type_name_str<Gaussian>(false, namespace_separator) + "(" + (name_keywords ? "centroid=" : "")
86 + _centroid->repr(name_keywords, namespace_separator) + ", " + (name_keywords ? "ellipse=" : "")
87 + _ellipse->repr(name_keywords, namespace_separator) + ", " + (name_keywords ? "integral=" : "")
88 + _integral->repr(name_keywords, namespace_separator) + ")";
89}
90
92 return type_name_str<Gaussian>(true) + "(centroid=" + _centroid->str() + ", ellipse=" + _ellipse->str()
93 + ", integral=" + _integral->str() + ")";
94}
95
96bool Gaussian::operator==(const Gaussian& other) const {
97 return (*_centroid == other.get_centroid_const()) && (*_ellipse == other.get_ellipse_const())
98 && (*_integral == other.get_integral_const());
99}
100
101bool Gaussian::operator!=(const Gaussian& other) const { return !(*this == other); }
102
104 out << g.str();
105 return out;
106}
107
108Gaussians::Gaussians(std::optional<const Data> data) {
109 if (data) {
110 size_t n_data = data->size();
111 if (n_data > 0) {
112 _data.resize(n_data, nullptr);
113 this->assign(*data);
114 }
115 }
116}
117Gaussians::Gaussians(std::vector<std::optional<const Data>> data) {
118 size_t n_data = 0;
119 for (const auto& datum : data) {
120 if (datum) n_data += datum->size();
121 }
122 if (n_data > 0) {
123 size_t i = 0;
124 _data.resize(n_data);
125 for (const auto& datum : data) i = this->assign(*datum, i);
126 }
127}
128
129Gaussian& Gaussians::operator[](size_t i) { return *(_data[i]); }
130const Gaussian& Gaussians::operator[](size_t i) const { return *(_data[i]); }
131
132size_t Gaussians::assign(const Data& data, size_t i) {
133 const size_t n_data = this->_data.size();
134 const size_t i_max = i + data.size();
135 if (!(i_max <= n_data)) {
136 throw std::out_of_range("data i_max=" + std::to_string(i_max) + ">= this._data.size()="
137 + std::to_string(n_data) + " for this=" + this->str());
138 }
139 size_t i_begin = i;
140 for (const auto& gauss : data) {
141 if (gauss == nullptr)
142 throw std::runtime_error("Gaussians data[" + std::to_string(i - i_begin) + "] can't be null");
143 _data[i++] = std::move(gauss);
144 }
145 return i;
146}
147
148Gaussian& Gaussians::at(size_t i) const { return *(_data.at(i)); }
149const Gaussian& Gaussians::at_const(size_t i) const { return *(_data.at(i)); }
150std::shared_ptr<Gaussian> Gaussians::at_ptr(size_t i) const { return _data.at(i); }
151
152typename Gaussians::Data::iterator Gaussians::begin() noexcept { return _data.begin(); }
153typename Gaussians::Data::const_iterator Gaussians::begin() const noexcept { return _data.cbegin(); }
154typename Gaussians::Data::const_iterator Gaussians::cbegin() const noexcept { return _data.cbegin(); }
155
156typename Gaussians::Data::iterator Gaussians::end() noexcept { return _data.end(); }
157typename Gaussians::Data::const_iterator Gaussians::end() const noexcept { return _data.cend(); }
158typename Gaussians::Data::const_iterator Gaussians::cend() const noexcept { return _data.cend(); }
159
160Gaussians::Data Gaussians::get_data() const { return _data; }
161
162size_t Gaussians::size() const { return _data.size(); }
163
164std::string Gaussians::repr(bool name_keywords, std::string_view namespace_separator) const {
165 std::string str
166 = type_name_str<Gaussians>(false, namespace_separator) + "(" + (name_keywords ? "data=" : "");
167 str += repr_iter_ptr(_data, name_keywords, namespace_separator);
168 return str + ")";
169}
170
172 std::string str = type_name_str<Gaussians>(true) + "(data=";
173 str += str_iter_ptr(_data);
174 return str + ")";
175}
176
177static const std::shared_ptr<const Gaussian> GAUSS_ZERO = std::make_shared<const Gaussian>();
178
181 : _source(source != nullptr ? source : GAUSS_ZERO),
182 _kernel(kernel != nullptr ? kernel : GAUSS_ZERO) {}
183
185 if (data) {
186 size_t n_data = data->size();
187 if (n_data > 0) {
188 _data.resize(n_data);
189 this->assign(*data);
190 }
191 }
192}
194 size_t n_data = 0;
195 for (const auto& datum : data) {
196 if (datum) n_data += datum->size();
197 }
198 if (n_data > 0) {
199 size_t i = 0;
200 _data.resize(n_data);
201 for (const auto& datum : data) i = this->assign(*datum, i);
202 }
203}
204
205size_t ConvolvedGaussians::assign(const Data& data, size_t i) {
206 const size_t n_data = this->_data.size();
207 const size_t i_max = i + data.size();
208 if (!(i_max <= n_data)) {
209 throw std::out_of_range("data i_max=" + std::to_string(i_max) + ">= this._data.size()="
210 + std::to_string(n_data) + " for this=" + this->str());
211 }
212 size_t i_begin = i;
213 for (const auto& gauss : data) {
214 if (gauss == nullptr)
215 throw std::runtime_error("ConvolvedGaussian data[" + std::to_string(i - i_begin)
216 + "] can't be null");
217 _data[i++] = std::move(gauss);
218 }
219 return i;
220}
221
222const Gaussian& ConvolvedGaussian::get_source() const { return *_source; }
223const Gaussian& ConvolvedGaussian::get_kernel() const { return *_kernel; }
224
226 return std::make_unique<Gaussian>(
227 _source->get_centroid_const().make_convolution(_kernel->get_centroid_const()),
228 _source->get_ellipse_const().make_convolution(_kernel->get_ellipse_const()),
229 std::make_shared<GaussianIntegralValue>(_source->get_integral_value()
230 + _kernel->get_integral_value()));
231}
232
233std::string ConvolvedGaussian::repr(bool name_keywords, std::string_view namespace_separator) const {
234 return type_name_str<ConvolvedGaussian>(false, namespace_separator) + "("
235 + (name_keywords ? "source=" : "") + _source->repr(name_keywords, namespace_separator) + ", "
236 + (name_keywords ? "kernel=" : "") + _kernel->repr(name_keywords, namespace_separator) + ")";
237}
238
240 return type_name_str<ConvolvedGaussian>(true) + "(source=" + _source->str() + ", kernel=" + _kernel->str()
241 + ")";
242}
243
245 return (this->get_source() == other.get_source()) && (this->get_kernel() == other.get_kernel());
246}
247
248bool ConvolvedGaussian::operator!=(const ConvolvedGaussian& other) const { return !(*this == other); }
249
250ConvolvedGaussian& ConvolvedGaussians::at(size_t i) const { return *(_data.at(i)); }
251const ConvolvedGaussian& ConvolvedGaussians::at_const(size_t i) const { return *(_data.at(i)); }
253
254typename ConvolvedGaussians::Data::iterator ConvolvedGaussians::begin() noexcept { return _data.begin(); }
255typename ConvolvedGaussians::Data::iterator ConvolvedGaussians::end() noexcept { return _data.end(); }
256
257typename ConvolvedGaussians::Data::const_iterator ConvolvedGaussians::begin() const noexcept {
258 return _data.begin();
259}
260typename ConvolvedGaussians::Data::const_iterator ConvolvedGaussians::end() const noexcept {
261 return _data.cend();
262}
263
264typename ConvolvedGaussians::Data::const_iterator ConvolvedGaussians::cbegin() const noexcept {
265 return _data.begin();
266}
267typename ConvolvedGaussians::Data::const_iterator ConvolvedGaussians::cend() const noexcept {
268 return _data.cend();
269}
270
272
273size_t ConvolvedGaussians::size() const { return _data.size(); }
274
275std::string ConvolvedGaussians::repr(bool name_keywords, std::string_view namespace_separator) const {
276 std::string str = type_name_str<ConvolvedGaussians>(false, namespace_separator) + "("
277 + (name_keywords ? "data=" : "");
278 str += repr_iter_ptr(_data, name_keywords, namespace_separator);
279 return str + ")";
280}
281
283 std::string str = type_name_str<ConvolvedGaussians>(true) + "(data=";
284 str += str_iter_ptr(_data);
285 return str + ")";
286}
287
288ConvolvedGaussian& ConvolvedGaussians::operator[](size_t i) { return *(_data[i]); }
289const ConvolvedGaussian& ConvolvedGaussians::operator[](size_t i) const { return *(_data[i]); }
290
291} // namespace lsst::gauss2d
292
293#endif
char * data
Definition BaseRecord.cc:61
T at(T... args)
T begin(T... args)
A 2D coordinate representing the center of a plane figure.
Definition centroid.h:114
A convolution of a Gaussian source and kernel.
Definition gaussian.h:220
bool operator!=(const ConvolvedGaussian &other) const
Definition gaussian.cc:248
bool operator==(const ConvolvedGaussian &other) const
Definition gaussian.cc:244
const Gaussian & get_source() const
Definition gaussian.cc:222
std::unique_ptr< Gaussian > make_convolution() const
Definition gaussian.cc:225
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 gaussian.cc:233
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:239
ConvolvedGaussian(std::shared_ptr< const Gaussian > source=nullptr, std::shared_ptr< const Gaussian > kernel=nullptr)
Definition gaussian.cc:179
const Gaussian & get_kernel() const
Definition gaussian.cc:223
ConvolvedGaussian & at(size_t i) const
Definition gaussian.cc:250
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:282
Data::iterator begin() noexcept
Definition gaussian.cc:254
ConvolvedGaussians(std::optional< const Data > data)
Definition gaussian.cc:184
Data::const_iterator cbegin() const noexcept
Definition gaussian.cc:264
std::shared_ptr< ConvolvedGaussian > at_ptr(size_t i) const
Definition gaussian.cc:252
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 gaussian.cc:275
Data::iterator end() noexcept
Definition gaussian.cc:255
Data::const_iterator cend() const noexcept
Definition gaussian.cc:267
const ConvolvedGaussian & at_const(size_t i) const
Definition gaussian.cc:251
ConvolvedGaussian & operator[](size_t i)
Definition gaussian.cc:288
An Ellipse with sigma_x, sigma_y, and rho values.
Definition ellipse.h:283
A 2D Gaussian with a Centroid, Ellipse, and integral.
Definition gaussian.h:99
Gaussian(std::shared_ptr< Centroid > centroid=nullptr, std::shared_ptr< Ellipse > ellipse=nullptr, std::shared_ptr< GaussianIntegral > integral=nullptr)
Construct a new Gaussian object.
Definition gaussian.cc:47
Ellipse & get_ellipse()
Get the ellipse object.
Definition gaussian.cc:58
void set_const_normal(double const_normal)
Definition gaussian.cc:69
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 gaussian.cc:84
GaussianIntegral & get_integral()
Get the integral object.
Definition gaussian.cc:59
bool operator!=(const Gaussian &other) const
Definition gaussian.cc:101
double get_integral_value() const
Get the integral value.
Definition gaussian.cc:55
void set_integral_value(double integral)
Definition gaussian.cc:72
bool operator==(const Gaussian &other) const
Definition gaussian.cc:96
const Centroid & get_centroid_const() const
Definition gaussian.cc:65
std::shared_ptr< Centroid > get_centroid_ptr()
Definition gaussian.cc:61
std::shared_ptr< Ellipse > get_ellipse_ptr()
Definition gaussian.cc:62
std::shared_ptr< GaussianIntegral > get_integral_ptr()
Definition gaussian.cc:63
void set_centroid_ptr(std::shared_ptr< Centroid > centroid)
Definition gaussian.cc:74
void set_ellipse_ptr(std::shared_ptr< Ellipse > ellipse)
Definition gaussian.cc:77
const GaussianIntegral & get_integral_const() const
Definition gaussian.cc:67
double get_const_normal() const
Get the multiplicative factor for Gaussian function evaluations: integral/(2*area)
Definition gaussian.cc:54
Centroid & get_centroid()
Get the centroid object.
Definition gaussian.cc:57
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:91
const Ellipse & get_ellipse_const() const
Definition gaussian.cc:66
void set_integral_ptr(std::shared_ptr< GaussianIntegral > integral)
Definition gaussian.cc:80
Interface for the normalization (total integrated value) of a 2D Gaussian.
Definition gaussian.h:47
A GaussianIntegral storing a float value.
Definition gaussian.h:72
GaussianIntegralValue(double value=1.)
Definition gaussian.cc:35
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:43
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 gaussian.cc:39
Gaussian & at(size_t i) const
Definition gaussian.cc:148
Data::iterator begin() noexcept
Definition gaussian.cc:152
std::shared_ptr< Gaussian > at_ptr(size_t i) const
Definition gaussian.cc:150
Gaussian & operator[](size_t i)
Definition gaussian.cc:129
const Gaussian & at_const(size_t i) const
Definition gaussian.cc:149
Data::const_iterator cbegin() const noexcept
Definition gaussian.cc:154
Gaussians(std::optional< const Data > data)
Definition gaussian.cc:108
size_t size() const
Definition gaussian.cc:162
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 gaussian.cc:164
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:171
Data::const_iterator end() const noexcept
Definition gaussian.cc:157
Data::const_iterator cend() const noexcept
Definition gaussian.cc:158
T end(T... args)
T move(T... args)
std::ostream & operator<<(std::ostream &out, const Covariance &obj)
Definition ellipse.cc:137
std::string repr_iter_ptr(const T &container, bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR)
Definition object.h:88
std::string to_string_float(const T value, const int precision=6, const bool scientific=true)
Definition to_string.h:15
std::string str_iter_ptr(const T &container)
Definition object.h:137
STL namespace.
T resize(T... args)
T size(T... args)
T to_string(T... args)