PCL
RigidTransformation.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/RigidTransformation.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_RigidTransformation_h
53 #define __PCL_RigidTransformation_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Algebra.h>
61 #include <pcl/Array.h>
62 #include <pcl/Matrix.h>
63 #include <pcl/Point.h>
64 
65 namespace pcl
66 {
67 
68 // ----------------------------------------------------------------------------
69 
79 class PCL_CLASS RigidTransformation
80 {
81 public:
82 
88  : m_H( Matrix::UnitMatrix( 3 ) )
89  {
90  }
91 
96  : m_H( H )
97  {
98  }
99 
128  template <class point_list1, class point_list2>
129  RigidTransformation( const point_list1& P1, const point_list2& P2 )
130  : m_H( LS_SVD( P1, P2 ) )
131  {
132  }
133 
138 
142  RigidTransformation& operator =( const RigidTransformation& ) = default;
143 
148 
152  RigidTransformation& operator =( RigidTransformation&& ) = default;
153 
161  template <typename T1, typename T2>
162  void Apply( T1& x, T2& y ) const
163  {
164  double fx = double( x );
165  double fy = double( y );
166  x = T1( m_H[0][0]*fx + m_H[0][1]*fy + m_H[0][2] );
167  y = T2( m_H[1][0]*fx + m_H[1][1]*fy + m_H[1][2] );
168  }
169 
176  template <typename T>
178  {
179  Apply( p.x, p.y );
180  return p;
181  }
182 
188  DPoint operator ()( double x, double y ) const
189  {
190  DPoint p( x, y );
191  Apply( p.x, p.y );
192  return p;
193  }
194 
200  template <typename T>
201  DPoint operator ()( const GenericPoint<T>& p ) const
202  {
203  return operator ()( double( p.x ), double( p.y ) );
204  }
205 
219  {
220  return RigidTransformation( m_H.Inverse() );
221  }
222 
227  {
228  return m_H;
229  }
230 
234  operator const Matrix&() const
235  {
236  return TransformationMatrix();
237  }
238 
242  bool IsValid() const
243  {
244  return !m_H.IsEmpty();
245  }
246 
251  {
252  m_H.EnsureUnique();
253  }
254 
255 private:
256 
257  Matrix m_H;
258 
259  /*
260  * Implementation of the SVD-based least squares method to compute an
261  * optimal rigid transformation matrix.
262  */
263  template <class point_list1, class point_list2>
264  static Matrix LS_SVD( const point_list1& P1, const point_list2& P2 )
265  {
266  int n = int( Min( P1.Length(), P2.Length() ) );
267  if ( n < 3 )
268  throw Error( "RigidTransformation::LS_SVD(): Less than three points specified." );
269 
270  /*
271  * Centroid coordinates
272  */
273  DPoint p10( 0 );
274  for ( int i = 0; i < n; ++i )
275  p10 += P1[i];
276  p10 /= n;
277  DPoint p20( 0 );
278  for ( int i = 0; i < n; ++i )
279  p20 += P2[i];
280  p20 /= n;
281 
282  /*
283  * Covariance matrix
284  */
285  Matrix M1( 2, n );
286  for ( int i = 0; i < n; ++i )
287  {
288  M1[0][i] = P1[i].x - p10.x;
289  M1[1][i] = P1[i].y - p10.y;
290  }
291  Matrix M2( 2, n );
292  for ( int i = 0; i < n; ++i )
293  {
294  M2[0][i] = P2[i].x - p20.x;
295  M2[1][i] = P2[i].y - p20.y;
296  }
297  Matrix H = M1 * M2.Transpose();
298 
299  /*
300  * Rotation matrix R
301  */
302  InPlaceSVD svd( H );
303  Matrix R = svd.V * H.Transpose();
304 
305  /*
306  * Rotation + translation matrix
307  */
308  return Matrix( R[0][0], R[0][1], p20.x - R[0][0]*p10.x - R[0][1]*p10.y,
309  R[1][0], R[1][1], p20.y - R[1][0]*p10.x - R[1][1]*p10.y,
310  0.0, 0.0, 1.0 );
311  }
312 };
313 
314 // ----------------------------------------------------------------------------
315 
316 } // pcl
317 
318 #endif // __PCL_RigidTransformation_h
319 
320 // ----------------------------------------------------------------------------
321 // EOF pcl/RigidTransformation.h - Released 2024-06-18T15:48:54Z
A simple exception with an associated error message.
Definition: Exception.h:239
Generic dynamic matrix of arbitrary dimensions.
Definition: Matrix.h:123
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
64-bit floating point real matrix.
Rigid geometric transformation.
const Matrix & TransformationMatrix() const
RigidTransformation(RigidTransformation &&)=default
GenericPoint< T > & Apply(GenericPoint< T > &p) const
RigidTransformation(const point_list1 &P1, const point_list2 &P2)
RigidTransformation(const RigidTransformation &)=default
RigidTransformation Inverse() const
RigidTransformation(const Matrix &H)
void Apply(T1 &x, T2 &y) const
constexpr const T & Min(const T &a, const T &b) noexcept
Definition: Utility.h:90
void Apply(FI i, FI j, F f) noexcept(noexcept(f))
Definition: Utility.h:249
PCL root namespace.
Definition: AbstractImage.h:77