lsst.gauss2d gbf99507273+b0138be388
 
Loading...
Searching...
No Matches
gaussian.h
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_GAUSSIAN_H
26#define LSST_GAUSS2D_GAUSSIAN_H
27
28#include <algorithm>
29#include <iostream>
30#include <memory>
31#include <optional>
32#include <stdexcept>
33#include <string>
34#include <utility>
35#include <vector>
36
37#include "centroid.h"
38#include "ellipse.h"
39#include "object.h"
40
41namespace lsst::gauss2d {
42
47class GaussianIntegral : public Object {
48public:
49 virtual ~GaussianIntegral() = default;
50
51 virtual double get_value() const = 0;
52 virtual void set_value(double value) = 0;
53
54 virtual std::string repr(bool name_keywords = false, std::string_view namespace_separator
55 = Object::CC_NAMESPACE_SEPARATOR) const override
56 = 0;
57 virtual std::string str() const override = 0;
58
59 virtual bool operator==(const GaussianIntegral& other) const {
60 return this->get_value() == other.get_value();
61 }
62 virtual bool operator!=(const GaussianIntegral& other) const { return !(*this == other); }
63};
64
72class GaussianIntegralValue : public GaussianIntegral {
73public:
74 explicit GaussianIntegralValue(double value = 1.);
75 explicit GaussianIntegralValue(std::shared_ptr<double> value);
76
77 ~GaussianIntegralValue() {};
78
79 double get_value() const override { return *_value; }
80 void set_value(double value) override { *_value = value; }
81
82 std::string repr(bool name_keywords = false,
83 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
84 std::string str() const override;
85
86private:
87 // TODO: Add some value safety to this
88 // Probably must be a thin wrapper with a getter/setter enforcing >= 0
89 // either that or delete the shared_ptr constructor and add a copy constructor
90 std::shared_ptr<double> _value;
91};
92
99class Gaussian : public Object {
100public:
108 explicit Gaussian(std::shared_ptr<Centroid> centroid = nullptr,
109 std::shared_ptr<Ellipse> ellipse = nullptr,
110 std::shared_ptr<GaussianIntegral> integral = nullptr);
111 ~Gaussian();
112
114 double get_const_normal() const;
116 double get_integral_value() const;
117
124
125 std::shared_ptr<Centroid> get_centroid_ptr();
126 std::shared_ptr<Ellipse> get_ellipse_ptr();
127 std::shared_ptr<GaussianIntegral> get_integral_ptr();
128
129 const Centroid& get_centroid_const() const;
130 const Ellipse& get_ellipse_const() const;
131 const GaussianIntegral& get_integral_const() const;
132
133 void set_const_normal(double const_normal);
134 void set_integral_value(double integral);
135
136 void set_centroid_ptr(std::shared_ptr<Centroid> centroid);
137 void set_ellipse_ptr(std::shared_ptr<Ellipse> ellipse);
138 void set_integral_ptr(std::shared_ptr<GaussianIntegral> integral);
139
140 std::string repr(bool name_keywords = false,
141 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
142 std::string str() const override;
143
144 bool operator==(const Gaussian& other) const;
145 bool operator!=(const Gaussian& other) const;
146
147private:
148 std::shared_ptr<Centroid> _centroid;
149 std::shared_ptr<Ellipse> _ellipse;
150 std::shared_ptr<GaussianIntegral> _integral;
151
160 template <typename T>
161 std::shared_ptr<T> _check_not_nullptr(std::shared_ptr<T> ptr, std::string name) {
162 if (ptr == nullptr) throw std::invalid_argument(this->str() + "Can't set " + name + " to nullptr");
163 return ptr;
164 }
165};
166
175class Gaussians : public Object {
176public:
177 typedef std::vector<std::shared_ptr<Gaussian>> Data;
178
179 // These constructors explicitly copy inputs rather than moving
180 explicit Gaussians(std::optional<const Data> data);
181 explicit Gaussians(std::vector<std::optional<const Data>> data);
182
183 Gaussian& operator[](size_t i);
184 const Gaussian& operator[](size_t i) const;
185
186 Gaussian& at(size_t i) const;
187 const Gaussian& at_const(size_t i) const;
188 std::shared_ptr<Gaussian> at_ptr(size_t i) const;
189
190 using iterator = typename Data::iterator;
191 using const_iterator = typename Data::const_iterator;
192
193 typename Data::iterator begin() noexcept;
194 typename Data::const_iterator cbegin() const noexcept;
195
196 typename Data::const_iterator begin() const noexcept;
197 typename Data::const_iterator end() const noexcept;
198
199 typename Data::iterator end() noexcept;
200 typename Data::const_iterator cend() const noexcept;
201
202 Data get_data() const;
203
204 size_t size() const;
205
206 std::string repr(bool name_keywords = false,
207 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
208 std::string str() const override;
209
210private:
211 Data _data = {};
212
213 size_t assign(const Data& data, size_t i = 0);
214};
215
220class ConvolvedGaussian : public Object {
221public:
222 explicit ConvolvedGaussian(std::shared_ptr<const Gaussian> source = nullptr,
223 std::shared_ptr<const Gaussian> kernel = nullptr);
224
225 const Gaussian& get_source() const;
226 const Gaussian& get_kernel() const;
227
228 std::unique_ptr<Gaussian> make_convolution() const;
229
230 std::string repr(bool name_keywords = false,
231 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
232 std::string str() const override;
233
234 bool operator==(const ConvolvedGaussian& other) const;
235 bool operator!=(const ConvolvedGaussian& other) const;
236
237private:
238 std::shared_ptr<const Gaussian> _source;
239 std::shared_ptr<const Gaussian> _kernel;
240};
241
249class ConvolvedGaussians : public Object {
250public:
251 typedef std::vector<std::shared_ptr<ConvolvedGaussian>> Data;
252
253 // These constructors explicitly copy inputs rather than moving
254 explicit ConvolvedGaussians(std::optional<const Data> data);
255 explicit ConvolvedGaussians(std::vector<std::optional<const Data>> data);
256
257 ConvolvedGaussian& at(size_t i) const;
258 const ConvolvedGaussian& at_const(size_t i) const;
259 std::shared_ptr<ConvolvedGaussian> at_ptr(size_t i) const;
260
261 using iterator = typename Data::iterator;
262 using const_iterator = typename Data::const_iterator;
263
264 typename Data::iterator begin() noexcept;
265 typename Data::iterator end() noexcept;
266
267 typename Data::const_iterator begin() const noexcept;
268 typename Data::const_iterator end() const noexcept;
269
270 typename Data::const_iterator cbegin() const noexcept;
271 typename Data::const_iterator cend() const noexcept;
272
273 Data get_data() const;
274
275 size_t size() const;
276
277 std::string repr(bool name_keywords = false,
278 std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR) const override;
279 std::string str() const override;
280
281 ConvolvedGaussian& operator[](size_t i);
282 const ConvolvedGaussian& operator[](size_t i) const;
283
284private:
285 Data _data = {};
286
287 size_t assign(const Data& data, size_t i = 0);
288};
289
290} // namespace lsst::gauss2d
291#endif
A 2D coordinate representing the center of a plane figure.
Definition centroid.h:114
Definition gaussian.h:220
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition gaussian.cc:233
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:239
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:282
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition gaussian.cc:275
An Ellipse with sigma_x, sigma_y, and rho values.
Definition ellipse.h:283
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
Definition gaussian.cc:39
Interface for the normalization (total integrated value) of a 2D Gaussian.
Definition gaussian.h:47
virtual std::string str() const override=0
Return a brief, human-readable string representation of this.
virtual std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override=0
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
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition gaussian.cc:84
GaussianIntegral & get_integral()
Get the integral object.
Definition gaussian.cc:59
double get_integral_value() const
Get the integral value.
Definition gaussian.cc:55
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
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition gaussian.cc:164
std::string str() const override
Return a brief, human-readable string representation of this.
Definition gaussian.cc:171
Definition object.h:40
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:45