PCL
PSFFit.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.4.7
6 // ----------------------------------------------------------------------------
7 // pcl/PSFFit.h - Released 2020-12-17T15:46:29Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2020 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_PSFFit_h
53 #define __PCL_PSFFit_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/ImageVariant.h>
61 #include <pcl/Matrix.h>
62 #include <pcl/String.h>
63 
64 namespace pcl
65 {
66 
67 // ----------------------------------------------------------------------------
68 
87 namespace PSFunction
88 {
89  enum value_type
90  {
91  Invalid = -1, // Represents an invalid or unsupported PSF type
92  Gaussian = 0, // Gaussian PSF
93  Moffat, // Moffat PSF with a fitted beta parameter
94  MoffatA, // Moffat PSF with fixed beta=10
95  Moffat8, // Moffat PSF with fixed beta=8
96  Moffat6, // Moffat PSF with fixed beta=6
97  Moffat4, // Moffat PSF with fixed beta=4
98  Moffat25, // Moffat PSF with fixed beta=2.5
99  Moffat15, // Moffat PSF with fixed beta=1.5
100  Lorentzian, // Lorentzian PSF, equivalent to a Moffat PSF with fixed beta=1
101  VariableShape, // Variable shape PSF
102 
103  NumberOfFunctions,
104 
105  Default = Gaussian
106  };
107 }
108 
109 // ----------------------------------------------------------------------------
110 
126 namespace PSFFitStatus
127 {
128  enum value_type
129  {
130  Invalid = -1,
131  NotFitted = 0,
132  FittedOk,
133  BadParameters,
134  NoSolution,
135  NoConvergence,
136  InaccurateSolution,
137  UnknownError
138  };
139 }
140 
141 // ----------------------------------------------------------------------------
142 
147 struct PSFData
148 {
152  typedef PSFunction::value_type psf_function;
153 
157  typedef PSFFitStatus::value_type psf_fit_status;
158 
159  psf_function function = PSFunction::Invalid;
160  bool circular = false;
161  psf_fit_status status = PSFFitStatus::NotFitted;
162  bool celestial = false;
163  double B = 0;
164  double A = 0;
165  DPoint c0 = 0.0;
166  DPoint q0 = 0.0;
167  double sx = 0;
168  double sy = 0;
169  double theta = 0;
170  double beta = 0;
171  double flux = 0;
172  double meanSignal = 0;
173  double mad = 0;
179  PSFData() = default;
180 
184  PSFData( const PSFData& ) = default;
185 
189  PSFData( PSFData&& ) = default;
190 
194  PSFData& operator =( const PSFData& ) = default;
195 
199  PSFData& operator =( PSFData&& ) = default;
200 
206  operator bool() const
207  {
208  return status == PSFFitStatus::FittedOk;
209  }
210 
215  String FunctionName() const;
216 
220  String StatusText() const;
221 
230  double FWHMx() const
231  {
232  return FWHM( function, sx, beta );
233  }
234 
243  double FWHMy() const
244  {
245  return FWHM( function, sy, beta );
246  }
247 
262  DRect Bounds() const
263  {
264  double d = (circular ? FWHMx() : Max( FWHMx(), FWHMy() ))/2;
265  return DRect( c0.x-d, c0.y-d, c0.x+d, c0.y+d );
266  }
267 
274  {
275  return celestial;
276  }
277 
282  void ToImage( Image& ) const;
283 
299  static double FWHM( psf_function function, double sigma, double beta = 2 )
300  {
301  PCL_PRECONDITION( beta > 0 )
302  switch ( function )
303  {
304  case PSFunction::Gaussian: return 2.3548200450309493 * sigma;
305  case PSFunction::Moffat: return 2 * sigma * Sqrt( Pow2( 1/beta ) - 1 );
306  case PSFunction::MoffatA: return 0.5358113941912513 * sigma;
307  case PSFunction::Moffat8: return 0.6016900619596693 * sigma;
308  case PSFunction::Moffat6: return 0.6998915581984769 * sigma;
309  case PSFunction::Moffat4: return 0.8699588840921645 * sigma;
310  case PSFunction::Moffat25: return 1.1305006161394060 * sigma;
311  case PSFunction::Moffat15: return 1.5328418730817597 * sigma;
312  case PSFunction::Lorentzian: return 2 * sigma;
313  case PSFunction::VariableShape: return 2 * sigma * Pow( beta*0.6931471805599453, 1/beta );
314  default: return 0; // ?!
315  }
316  }
317 };
318 
319 // ----------------------------------------------------------------------------
320 
325 class PSFFit
326 {
327 public:
328 
332  typedef PSFData::psf_function psf_function;
333 
337  typedef PSFData::psf_fit_status psf_fit_status;
338 
343 
389  PSFFit( const ImageVariant& image,
390  const DPoint& center, const DRect& rect,
391  psf_function function = PSFunction::Gaussian, bool circular = false,
392  double betaMin = 1.0, double betaMax = 4.0 );
393 
397  PSFFit( const PSFFit& ) = default;
398 
402  PSFFit( PSFFit&& ) = default;
403 
407  PSFFit& operator =( const PSFFit& x ) = default;
408 
412  PSFFit& operator =( PSFFit&& x ) = default;
413 
418  operator bool() const
419  {
420  return psf;
421  }
422 
423 private:
424 
425  Matrix S; // matrix of sampled pixel data
426  Vector P; // vector of function parameters
427  mutable double m_beta;
428 
429  Vector GoodnessOfFit( psf_function, bool circular ) const;
430 
431  friend class PSFFitEngine;
432 };
433 
434 // ----------------------------------------------------------------------------
435 
436 } // pcl
437 
438 #endif // __PCL_PSFFit_h
439 
440 // ----------------------------------------------------------------------------
441 // EOF pcl/PSFFit.h - Released 2020-12-17T15:46:29Z
PSF fit parameters.
Definition: PSFFit.h:147
PSFData psf
Definition: PSFFit.h:342
PCL root namespace.
Definition: AbstractImage.h:76
constexpr const T & Max(const T &a, const T &b) noexcept
Definition: Utility.h:119
constexpr T Pow2(T x) noexcept
Definition: Math.h:1717
Numerical Point Spread Function (PSF) fit to a source in an image.
Definition: PSFFit.h:325
PSFData::psf_function psf_function
Definition: PSFFit.h:332
64-bit floating-point rectangle in the R^2 space.
Acts like a union for all types of images in PCL, with optional class-wide ownership of transported i...
Definition: ImageVariant.h:317
64-bit floating point real matrix.
Unicode (UTF-16) string.
Definition: String.h:7923
PSFFitStatus::value_type psf_fit_status
Definition: PSFFit.h:157
bool HasCelestialCoordinates() const
Definition: PSFFit.h:273
PSFData::psf_fit_status psf_fit_status
Definition: PSFFit.h:337
64-bit floating point real vector.
64-bit floating-point point in the R^2 space.
PSFunction::value_type psf_function
Definition: PSFFit.h:152
double FWHMx() const
Definition: PSFFit.h:230
DRect Bounds() const
Definition: PSFFit.h:262
Complex< T1 > Pow(const Complex< T1 > &c, T2 x) noexcept
Definition: Complex.h:747
double FWHMy() const
Definition: PSFFit.h:243
static double FWHM(psf_function function, double sigma, double beta=2)
Definition: PSFFit.h:299
Complex< T > Sqrt(const Complex< T > &c) noexcept
Definition: Complex.h:674
32-bit floating point real image.