PCL
ProjectionBase.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/ProjectionBase.h - Released 2024-06-18T15:48:54Z
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-2024 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_ProjectionBase_h
53 #define __PCL_ProjectionBase_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Optional.h>
60 #include <pcl/Point.h>
61 #include <pcl/SphericalRotation.h>
62 #include <pcl/WCSKeywords.h>
63 
64 /*
65  * Based on original work contributed by AndrĂ©s del Pozo.
66  */
67 
68 namespace pcl
69 {
70 
71 // ----------------------------------------------------------------------------
72 
79 class PCL_CLASS ProjectionBase
80 {
81 public:
82 
86  virtual ~ProjectionBase()
87  {
88  }
89 
93  virtual ProjectionBase* Clone() const = 0;
94 
98  virtual IsoString ProjCode() const = 0;
99 
103  virtual IsoString Identifier() const = 0;
104 
108  virtual IsoString Name() const = 0;
109 
114  virtual void GetWCS( WCSKeywords& wcs ) const;
115 
119  virtual bool Direct( DPoint& pW, const DPoint& pRD ) const noexcept
120  {
121  DPoint pN = m_sph.CelestialToNative( pRD );
122  return IsFinite( pN.x ) && IsFinite( pN.y ) && Project( pW, pN );
123  }
124 
128  virtual bool Inverse( DPoint& pRD, const DPoint& pW ) const noexcept
129  {
130  DPoint pN;
131  if ( Unproject( pN, pW ) )
132  if ( IsFinite( pN.x ) )
133  if ( IsFinite( pN.y ) )
134  {
135  pRD = m_sph.NativeToCelestial( pN );
136  return true;
137  }
138  return false;
139  }
140 
144  virtual bool CheckBrokenLine( const DPoint& cp1, const DPoint& cp2 ) const noexcept
145  {
146  return DistanceFast( m_sph.CelestialToNative( cp1 ), m_sph.CelestialToNative( cp2 ) ) < 150;
147  }
148 
152  virtual DPoint ProjectionOrigin() const
153  {
154  return DPoint( Deg( m_ra0 ), Deg( m_dec0 ) );
155  }
156 
161  static double Distance( const DPoint& p1, const DPoint& p2 )
162  {
163  double sinX, cosX;
164  SinCos( Rad( Abs( p1.x - p2.x ) ), sinX, cosX );
165  double sinY1, cosY1;
166  SinCos( Rad( p1.y ), sinY1, cosY1 );
167  double sinY2, cosY2;
168  SinCos( Rad( p2.y ), sinY2, cosY2 );
169  double K = cosY1*sinY2 - sinY1*cosY2*cosX;
170  return Deg( ArcTan( Sqrt( cosY2*sinX*cosY2*sinX + K*K ), sinY1*sinY2 + cosY1*cosY2*cosX ) );
171  };
172 
177  static double DistanceFast( const DPoint& p1, const DPoint& p2 )
178  {
179  double sinY1, cosY1;
180  SinCos( Rad( p1.y ), sinY1, cosY1 );
181  double sinY2, cosY2;
182  SinCos( Rad( p2.y ), sinY2, cosY2 );
183  return Deg( ArcCos( sinY1*sinY2 + cosY1*cosY2*Cos( Rad( p1.x - p2.x ) ) ) );
184  }
185 
193  {
194  return Vector{ Deg( m_ra0 ), Deg( m_dec0 ) };
195  }
196 
204  {
205  return Vector{ m_phi0, m_theta0 };
206  }
207 
215  {
216  double lonpole;
217  if ( m_lonpole.IsDefined() )
218  lonpole = m_lonpole();
219  else
220  {
221  // Default value for the native longitude of the celestial pole.
222  lonpole = ((Deg( m_dec0 ) < m_theta0) ? 180 : 0) + m_phi0;
223  if ( lonpole < -180 )
224  lonpole += 360;
225  else if ( lonpole > 180 )
226  lonpole -= 360;
227  }
228  return Vector{ lonpole, m_latpole.OrElse( 90.0 ) };
229  }
230 
236 
241  static IsoString ProjectionIdentifierToWCSCode( const IsoString& identifier );
242 
243 protected:
244 
245  double m_ra0 = 0; // celestial longitude of the reference point (radians)
246  double m_dec0 = 0; // celestial latitude of the reference point (radians)
247  double m_phi0 = 0; // native longitude of the reference point (degrees)
248  double m_theta0 = 0; // native latitude of the reference point (degrees)
249  Optional<double> m_lonpole; // native longitude of the celestial pole (degrees)
250  Optional<double> m_latpole; // native latitude of the celestial pole (degrees)
251  SphericalRotation m_sph;
252 
256  ProjectionBase() = default;
257 
261  ProjectionBase( const ProjectionBase& ) = default;
262 
267  void InitFromRefPoint( double lng0, double lat0, const Optional<double>& lonpole = Optional<double>() );
268 
272  void InitFromWCS( const WCSKeywords& );
273 
277  virtual bool Project( DPoint& pW, const DPoint& pN ) const noexcept = 0;
278 
282  virtual bool Unproject( DPoint& pN, const DPoint& pW ) const noexcept = 0;
283 
284  friend class ProjectionFactory;
285 };
286 
287 // ----------------------------------------------------------------------------
288 
289 } // pcl
290 
291 #endif // __PCL_ProjectionBase_h
292 
293 // ----------------------------------------------------------------------------
294 // EOF pcl/ProjectionBase.h - Released 2024-06-18T15:48:54Z
A generic point in the two-dimensional space.
Definition: Point.h:100
component x
Abscissa (horizontal, or X-axis coordinate).
Definition: Point.h:111
component y
Ordinate (vertical, or Y-axis coordinate).
Definition: Point.h:112
Generic vector of arbitrary length.
Definition: Vector.h:107
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5425
Base class of all projection systems.
virtual bool Inverse(DPoint &pRD, const DPoint &pW) const noexcept
ProjectionBase(const ProjectionBase &)=default
static IsoString WCSCodeToProjectionIdentifier(const IsoString &wcsCode)
static double DistanceFast(const DPoint &p1, const DPoint &p2)
Vector ReferenceCelestialCoordinates() const
Vector ReferenceNativeCoordinates() const
virtual DPoint ProjectionOrigin() const
virtual bool Unproject(DPoint &pN, const DPoint &pW) const noexcept=0
static IsoString ProjectionIdentifierToWCSCode(const IsoString &identifier)
virtual ~ProjectionBase()
virtual ProjectionBase * Clone() const =0
virtual IsoString Identifier() const =0
virtual IsoString Name() const =0
static double Distance(const DPoint &p1, const DPoint &p2)
Vector CelestialPoleNativeCoordinates() const
virtual IsoString ProjCode() const =0
virtual bool Project(DPoint &pW, const DPoint &pN) const noexcept=0
void InitFromRefPoint(double lng0, double lat0, const Optional< double > &lonpole=Optional< double >())
ProjectionBase()=default
virtual void GetWCS(WCSKeywords &wcs) const
void InitFromWCS(const WCSKeywords &)
virtual bool Direct(DPoint &pW, const DPoint &pRD) const noexcept
Selectable projection system.
Rotation in spherical coordinates.
A structure for management of WCS FITS header keywords and associated image properties.
Definition: WCSKeywords.h:89
Complex< T > Sqrt(const Complex< T > &c) noexcept
Definition: Complex.h:674
T Abs(const Complex< T > &c) noexcept
Definition: Complex.h:429
Complex< T > Cos(const Complex< T > &c) noexcept
Definition: Complex.h:806
bool IsFinite(float x) noexcept
Definition: Math.h:178
void SinCos(T x, T &sx, T &cx) noexcept
Definition: Math.h:1030
constexpr T ArcTan(T x) noexcept
Definition: Math.h:526
constexpr T Rad(T x) noexcept
Definition: Math.h:1894
constexpr T ArcCos(T x) noexcept
Definition: Math.h:502
constexpr T Deg(T x) noexcept
Definition: Math.h:606
PCL root namespace.
Definition: AbstractImage.h:77