PCL
WorldTransformation.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/WorldTransformation.h - Released 2024-12-28T16:53:48Z
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_WorldTransformation_h
53 #define __PCL_WorldTransformation_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/GridInterpolation.h>
62 #include <pcl/SurfaceSpline.h>
63 #include <pcl/WCSKeywords.h>
64 
65 /*
66  * Based on original work contributed by AndrĂ©s del Pozo.
67  */
68 
69 namespace pcl
70 {
71 
72 // ----------------------------------------------------------------------------
73 
74 #define __PCL_WCS_SPLINE_VERSION_CURRENT "2.0"
75 #define __PCL_WCS_SPLINE_VERSION_OLD "1.3"
76 #define __PCL_WCS_MIN_SPLINE_MAX_POINTS 1000
77 #define __PCL_WCS_MAX_SPLINE_MAX_DENSE_POINTS 2100
78 #define __PCL_WCS_MAX_SPLINE_MAX_DDM_POINTS 25000
79 #define __PCL_WCS_DEFAULT_SPLINE_MAX_DENSE_POINTS 2100
80 #define __PCL_WCS_DEFAULT_SPLINE_MAX_DDM_POINTS 4000
81 #define __PCL_WCS_DEFAULT_SPLINE_MAX_POINTS 4000
82 #define __PCL_WCS_DEFAULT_SPLINE_RBF RadialBasisFunction::DDMThinPlateSpline
83 #define __PCL_WCS_DEFAULT_SPLINE_ORDER 2
84 #define __PCL_WCS_DEFAULT_SPLINE_SMOOTHNESS 0.005F
85 #define __PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_ENABLED true
86 #define __PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_REJECT_FRACTION 0.10F
87 
88 // ----------------------------------------------------------------------------
89 
96 class PCL_CLASS WorldTransformation
97 {
98 public:
99 
103  WorldTransformation() = default;
104 
109 
114  {
115  }
116 
120  virtual bool IsEmpty() const
121  {
122  return false;
123  }
124 
128  virtual WorldTransformation* Clone() const = 0;
129 
137  virtual DPoint Direct( const DPoint& p ) const = 0;
138 
146  virtual DPoint Inverse( const DPoint& p ) const = 0;
147 
153 };
154 
155 // ----------------------------------------------------------------------------
156 
164 {
165 public:
166 
173  : m_transWI( transIW.Inverse() )
174  , m_transIW( transIW )
175  {
176  }
177 
182 
187 
192  {
193  }
194 
198  bool IsEmpty() const override
199  {
200  return false;
201  }
202 
205  WorldTransformation* Clone() const override
206  {
207  return new LinearWorldTransformation( *this );
208  }
209 
212  DPoint Direct( const DPoint& p ) const override
213  {
214  return m_transWI.Transform( p );
215  }
216 
219  DPoint Inverse( const DPoint& p ) const override
220  {
221  return m_transIW.Transform( p );
222  }
223 
229  {
230  return m_transIW;
231  }
232 
233 private:
234 
235  LinearTransformation m_transWI; // world -> image
236  LinearTransformation m_transIW; // image -> world
237 };
238 
239 // ----------------------------------------------------------------------------
240 
241 class PCL_CLASS XMLElement;
242 
259 {
260 public:
261 
262  using rbf_type = PointSurfaceSpline::rbf_type;
263 
371  SplineWorldTransformation( const Array<DPoint>& controlPointsW,
372  const Array<DPoint>& controlPointsI,
373  rbf_type rbf = __PCL_WCS_DEFAULT_SPLINE_RBF,
374  float smoothness = __PCL_WCS_DEFAULT_SPLINE_SMOOTHNESS,
375  int order = __PCL_WCS_DEFAULT_SPLINE_ORDER,
376  bool enableSimplifier = __PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_ENABLED,
377  float simplifierRejectFraction = __PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_REJECT_FRACTION,
378  const FVector& weights = FVector() )
379  : m_controlPointsW( controlPointsW )
380  , m_controlPointsI( controlPointsI )
381  , m_rbf( rbf )
382  , m_order( order )
383  , m_smoothness( smoothness )
384  , m_useSimplifiers( enableSimplifier )
385  , m_simplifierRejectFraction( simplifierRejectFraction )
386  , m_weights( weights )
387  {
388  EnsureValidRBFParameters();
389  InitializeSplines();
390  CalculateLinearApproximation();
391  }
392 
412  const LinearTransformation& linearIW = LinearTransformation::Null(), bool regenerate = false );
413 
427  {
428  Deserialize( data );
429  EnsureValidRBFParameters();
430  InitializeSplines();
431  if ( linearIW.IsSingularMatrix() )
432  CalculateLinearApproximation();
433  else
434  m_linearIW = linearIW;
435  }
436 
441 
446 
451  {
452  }
453 
457  SplineWorldTransformation& operator =( const SplineWorldTransformation& ) = default;
458 
463 
470  bool IsEmpty() const override
471  {
472  return m_controlPointsW.IsEmpty() || m_controlPointsI.IsEmpty();
473  }
474 
478  WorldTransformation* Clone() const override
479  {
480  return new SplineWorldTransformation( *this );
481  }
482 
499  DPoint Direct( const DPoint& pW ) const override
500  {
501  if ( m_gridWI.IsValid() )
502  if ( m_gridWI.ReferenceRect().IncludesFast( pW ) )
503  return m_gridWI( pW );
504  return m_splineWI( pW );
505  }
506 
523  DPoint Inverse( const DPoint& pI ) const override
524  {
525  if ( m_gridIW.IsValid() )
526  if ( m_gridIW.ReferenceRect().IncludesFast( pI ) )
527  return m_gridIW( pI );
528  return m_splineIW( pI );
529  }
530 
536  {
537  return m_linearIW;
538  }
539 
544  const IsoString& Version() const
545  {
546  return m_version;
547  }
548 
565  void InitializeGridInterpolations( const Rect& rectI, int deltaI = 16 )
566  {
567  PCL_PRECONDITION( deltaI >= 1 && deltaI <= 64 )
568  deltaI = Range( deltaI, 1, 64 );
569 
570  /*
571  * Compute reference region and grid distance in native spherical
572  * coordinates.
573  */
574  DRect rectW( std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
575  std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest() );
576  for ( int x = 0; x < rectI.Width(); ++x )
577  for ( int p = 0; p < 2; ++p )
578  {
579  DPoint pW = m_splineIW( DPoint( double( x ), p*(rectI.Height() - 1) ) );
580  if ( pW.x < rectW.x0 )
581  rectW.x0 = pW.x;
582  if ( pW.x > rectW.x1 )
583  rectW.x1 = pW.x;
584  if ( pW.y < rectW.y0 )
585  rectW.y0 = pW.y;
586  if ( pW.y > rectW.y1 )
587  rectW.y1 = pW.y;
588  }
589  for ( int y = 0; y < rectI.Height(); ++y )
590  for ( int p = 0; p < 2; ++p )
591  {
592  DPoint pW = m_splineIW( DPoint( p*(rectI.Width() - 1), double( y ) ) );
593  if ( pW.x < rectW.x0 )
594  rectW.x0 = pW.x;
595  if ( pW.x > rectW.x1 )
596  rectW.x1 = pW.x;
597  if ( pW.y < rectW.y0 )
598  rectW.y0 = pW.y;
599  if ( pW.y > rectW.y1 )
600  rectW.y1 = pW.y;
601  }
602  double deltaW = m_splineIW( DPoint( -deltaI/2, -deltaI/2 ) ).DistanceTo(
603  m_splineIW( DPoint( +deltaI/2, +deltaI/2 ) ) )/1.4142;
604 
605  m_gridWI.Initialize( rectW, deltaW, m_splineWI, false/*verbose*/ );
606  m_gridIW.Initialize( rectI, deltaI, m_splineIW, false/*verbose*/ );
607  }
608 
615  {
616  return m_gridWI.IsValid() && m_gridIW.IsValid();
617  }
618 
626  double GridInterpolationDelta() const
627  {
628  return HasGridInterpolations() ? m_gridIW.Delta() : 0.0;
629  }
630 
636  {
637  return "PCL:AstrometricSolution:SplineWorldTransformation:";
638  }
639 
645 
654  void Serialize( ByteArray& data ) const;
655 
668  {
669  return int( m_controlPointsW.Length() );
670  }
671 
685  {
686  return m_controlPointsW;
687  }
688 
702  {
703  return m_controlPointsI;
704  }
705 
716  rbf_type RBFType() const
717  {
718  return m_rbf;
719  }
720 
725  int SplineOrder() const
726  {
727  return m_order;
728  }
729 
735 
740  int MaxSplinePoints() const
741  {
742  return m_maxSplinePoints;
743  }
744 
763  void GetSplineLengths( int& xWI, int& yWI, int& xIW, int& yIW ) const
764  {
765  xWI = m_splineWI.SplineX().Length();
766  yWI = m_splineWI.SplineY().Length();
767  xIW = m_splineIW.SplineX().Length();
768  yIW = m_splineIW.SplineY().Length();
769  }
770 
785  {
786  return m_truncated; // fetched during initialization
787  }
788 
794  bool SimplifiersEnabled() const
795  {
796  return m_useSimplifiers;
797  }
798 
805  {
806  return m_simplifierRejectFraction;
807  }
808 
824  void GetSplineErrors( double& xWI, double& yWI, double& xIW, double& yIW ) const
825  {
826  xWI = m_splineWI.ErrorX();
827  yWI = m_splineWI.ErrorY();
828  xIW = m_splineIW.ErrorX();
829  yIW = m_splineIW.ErrorY();
830  }
831 
832 private:
833 
834  IsoString m_version = __PCL_WCS_SPLINE_VERSION_CURRENT;
835  Array<DPoint> m_controlPointsW;
836  Array<DPoint> m_controlPointsI;
837  rbf_type m_rbf = __PCL_WCS_DEFAULT_SPLINE_RBF;
838  int m_order = __PCL_WCS_DEFAULT_SPLINE_ORDER;
839  float m_smoothness = __PCL_WCS_DEFAULT_SPLINE_SMOOTHNESS;
840  int m_maxSplinePoints = __PCL_WCS_DEFAULT_SPLINE_MAX_POINTS;
841  bool m_useSimplifiers = __PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_ENABLED;
842  float m_simplifierRejectFraction = __PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_REJECT_FRACTION;
843  FVector m_weights;
844  bool m_truncated = false; // true => truncated control point vectors
845  Homography m_projectiveWI;
846  Homography m_projectiveIW;
847  PointSurfaceSpline m_splineWI;
848  PointSurfaceSpline m_splineIW;
849  PointGridInterpolation m_gridWI;
850  PointGridInterpolation m_gridIW;
851  LinearTransformation m_linearIW;
852 
853  static PointSurfaceSpline PointSurfaceSplineFromProperties( const PropertyArray&, const IsoString& version );
854  static PropertyArray PointSurfaceSplineToProperties( const PointSurfaceSpline&, const IsoString& );
855 
856  static SurfaceSpline<double> SurfaceSplineFromProperties( const PropertyArray&, const IsoString& version );
857  static PropertyArray SurfaceSplineToProperties( const SurfaceSpline<double>&, const IsoString& );
858 
859  static PointGridInterpolation PointGridInterpolationFromProperties( const PropertyArray&, const IsoString& version );
860  static PropertyArray PointGridInterpolationToProperties( const PointGridInterpolation&, const IsoString& );
861 
862  void Deserialize( const ByteArray& );
863  void EnsureValidRBFParameters();
864  void InitializeSplines();
865  void CalculateLinearApproximation();
866 };
867 
868 // ----------------------------------------------------------------------------
869 
870 } // pcl
871 
872 #endif // __PCL_WorldTransformation_h
873 
874 // ----------------------------------------------------------------------------
875 // EOF pcl/WorldTransformation.h - Released 2024-12-28T16:53:48Z
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
A generic rectangle in the two-dimensional space.
Definition: Rectangle.h:316
component x1
Horizontal coordinate of the lower right corner.
Definition: Rectangle.h:336
component y1
Vertical coordinate of the lower right corner.
Definition: Rectangle.h:337
component y0
Vertical coordinate of the upper left corner.
Definition: Rectangle.h:335
component x0
Horizontal coordinate of the upper left corner.
Definition: Rectangle.h:334
component Width() const noexcept
Definition: Rectangle.h:637
component Height() const noexcept
Definition: Rectangle.h:646
Generic vector of arbitrary length.
Definition: Vector.h:107
Homography geometric transformation.
Definition: Homography.h:85
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5443
A linear geometric transformation on the plane defined as a 2x3 matrix of 64-bit floating point scala...
static LinearTransformation Null()
Linear world coordinate transformation.
WorldTransformation * Clone() const override
LinearWorldTransformation(LinearWorldTransformation &&)=default
LinearWorldTransformation(const LinearWorldTransformation &)=default
DPoint Direct(const DPoint &p) const override
DPoint Inverse(const DPoint &p) const override
LinearWorldTransformation(const LinearTransformation &transIW)
const LinearTransformation & ApproximateLinearTransform() const override
Discretized vector surface interpolation/approximation in two dimensions.
Vector surface spline interpolation/approximation in two dimensions.
spline::rbf_type rbf_type
Surface spline world coordinate transformation.
const Array< DPoint > & NativeControlPoints() const
DPoint Direct(const DPoint &pW) const override
IsoString RBFTypeName() const
void GetSplineErrors(double &xWI, double &yWI, double &xIW, double &yIW) const
SplineWorldTransformation(const ByteArray &data, const LinearTransformation &linearIW=LinearTransformation::Null())
WorldTransformation * Clone() const override
const IsoString & Version() const
const Array< DPoint > & ImageControlPoints() const
void Serialize(ByteArray &data) const
SplineWorldTransformation(const PropertyArray &properties, const LinearTransformation &linearIW=LinearTransformation::Null(), bool regenerate=false)
PropertyArray ToProperties() const
SplineWorldTransformation(const SplineWorldTransformation &)=default
void GetSplineLengths(int &xWI, int &yWI, int &xIW, int &yIW) const
SplineWorldTransformation(const Array< DPoint > &controlPointsW, const Array< DPoint > &controlPointsI, rbf_type rbf=__PCL_WCS_DEFAULT_SPLINE_RBF, float smoothness=__PCL_WCS_DEFAULT_SPLINE_SMOOTHNESS, int order=__PCL_WCS_DEFAULT_SPLINE_ORDER, bool enableSimplifier=__PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_ENABLED, float simplifierRejectFraction=__PCL_WCS_DEFAULT_SURFACE_SIMPLIFIER_REJECT_FRACTION, const FVector &weights=FVector())
void InitializeGridInterpolations(const Rect &rectI, int deltaI=16)
DPoint Inverse(const DPoint &pI) const override
SplineWorldTransformation(SplineWorldTransformation &&)=default
const LinearTransformation & ApproximateLinearTransform() const override
Abstract base class of world coordinate transformations.
WorldTransformation(const WorldTransformation &)=default
virtual bool IsEmpty() const
virtual WorldTransformation * Clone() const =0
virtual DPoint Inverse(const DPoint &p) const =0
virtual const LinearTransformation & ApproximateLinearTransform() const =0
virtual DPoint Direct(const DPoint &p) const =0
constexpr const T & Range(const T &x, const T &a, const T &b) noexcept
Definition: Utility.h:190
PCL root namespace.
Definition: AbstractImage.h:77