AffineGT.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/geometry/AffineGT.cpp
22 
23  \brief 2D Geometric transformation.
24 */
25 
26 // TerraLib
27 #include "../common/MatrixUtils.h"
28 #include "AffineGT.h"
29 
30 // STL
31 #include <cmath>
32 
33 te::gm::AffineGT::AffineGT() = default;
34 
35 te::gm::AffineGT::~AffineGT() = default;
36 
37 const std::string& te::gm::AffineGT::getName() const
38 {
39  static std::string name( "Affine" );
40  return name;
41 }
42 
43 bool te::gm::AffineGT::isValid( const GTParameters& params ) const
44 {
45  return ( ( params.m_directParameters.size() == 6 ) &&
46  ( params.m_inverseParameters.size() == 6 ) );
47 }
48 
49 void te::gm::AffineGT::directMap( const GTParameters& params, const double& pt1X,
50  const double& pt1Y, double& pt2X, double& pt2Y ) const
51 {
52  assert( isValid( params ) );
53 
54  pt2X = ( params.m_directParameters[0] * pt1X ) +
55  ( params.m_directParameters[1] * pt1Y ) +
56  params.m_directParameters[2];
57  pt2Y = ( params.m_directParameters[3] * pt1X ) +
58  ( params.m_directParameters[4] * pt1Y ) +
59  params.m_directParameters[5];
60 }
61 
62 void te::gm::AffineGT::inverseMap( const GTParameters& params, const double& pt2X,
63  const double& pt2Y, double& pt1X, double& pt1Y ) const
64 {
65  assert( isValid( params ) );
66 
67  pt1X = ( params.m_inverseParameters[0] * pt2X ) +
68  ( params.m_inverseParameters[1] * pt2Y ) +
69  params.m_inverseParameters[2];
70  pt1Y = ( params.m_inverseParameters[3] * pt2X ) +
71  ( params.m_inverseParameters[4] * pt2Y ) +
72  params.m_inverseParameters[5];
73 }
74 
76 {
77  return 3;
78 }
79 
81 {
82  te::gm::AffineGT* newTransPtr = new AffineGT;
84  return newTransPtr;
85 }
86 
88 {
89  m_computeParameters_tiepointsSize = static_cast<unsigned int>(params.m_tiePoints.size());
90 
92  return false;
93 
98 
100  {
101  const Coord2D& x_y = params.m_tiePoints[ m_computeParameters_tpIdx ].first;
102  const Coord2D& u_v = params.m_tiePoints[ m_computeParameters_tpIdx ].second;
103 
106 
113 
120 
123  }
124 
125  /* At calcule */
126  m_computeParameters_At = boost::numeric::ublas::trans( m_computeParameters_A );
127 
128  /* N calcule */
129  m_computeParameters_N = boost::numeric::ublas::prod( m_computeParameters_At, m_computeParameters_A );
130 
131  /* U calcule */
132  m_computeParameters_U = boost::numeric::ublas::prod( m_computeParameters_At, m_computeParameters_L );
133 
134  /* N_inv calcule */
135 
137  {
138  /* direct parameters calcule */
139 
140  m_computeParameters_X = boost::numeric::ublas::prod( m_computeParameters_N_inv,
142 
143  params.m_directParameters.resize( 6 );
144  params.m_directParameters[0] = m_computeParameters_X(0,0);
145  params.m_directParameters[1] = m_computeParameters_X(1,0);
146  params.m_directParameters[2] = m_computeParameters_X(2,0);
147  params.m_directParameters[3] = m_computeParameters_X(3,0);
148  params.m_directParameters[4] = m_computeParameters_X(4,0);
149  params.m_directParameters[5] = m_computeParameters_X(5,0);
150 
151  /* inverse parameters calcule */
152 
153  m_computeParameters_XExpanded.resize( 3, 3 );
160  m_computeParameters_XExpanded( 2, 0 ) = 0;
161  m_computeParameters_XExpanded( 2, 1 ) = 0;
162  m_computeParameters_XExpanded( 2, 2 ) = 1;
163 
165  {
166  params.m_inverseParameters.resize( 6 );
173 
174  return true;
175  }
176  else
177  {
178  return false;
179  }
180  }
181  else
182  {
183  return false;
184  }
185 }
186 
187 
188 bool te::gm::AffineGT::decompose( const std::vector< double >& transfParams,
189  double& translationX, double& translationY,
190  double& scalingFactorX, double& scalingFactorY, double& skew,
191  double& squeeze, double& scaling, double& rotation )
192 {
193  assert( transfParams.size() == 6 );
194 
195  double APar = transfParams[ 0 ];
196  double BPar = transfParams[ 1 ];
197  double CPar = transfParams[ 3 ];
198  double DPar = transfParams[ 4 ];
199 
200  double determinant = ( APar * DPar ) - ( BPar * CPar );
201 
202  if( determinant == 0.0 )
203  {
204  return false;
205  }
206  else if( determinant < 0.0 )
207  {
208  APar = transfParams[ 1 ];
209  BPar = transfParams[ 0 ];
210  CPar = transfParams[ 4 ];
211  DPar = transfParams[ 3 ];
212 
213  determinant = ( APar * DPar ) - ( BPar * CPar );
214 
215  const double FVar = 1.0 / ( ( APar * APar ) +
216  ( CPar * CPar ) );
217 
218  skew = ( ( APar * BPar ) + ( CPar * DPar ) ) * FVar;
219 
220  squeeze = 1.0 / sqrt( FVar * determinant );
221 
222  scaling = sqrt( determinant );
223 
224  scalingFactorX = scaling / squeeze;
225 
226  scalingFactorY = scaling * squeeze;
227 
228  rotation = atan( APar / CPar );
229  }
230  else
231  {
232  const double FVar = 1.0 / ( ( APar * APar ) +
233  ( CPar * CPar ) );
234 
235  skew = ( ( APar * BPar ) + ( CPar * DPar ) ) * FVar;
236 
237  squeeze = 1.0 / sqrt( FVar * determinant );
238 
239  scaling = sqrt( determinant );
240 
241  scalingFactorX = scaling * squeeze;
242 
243  scalingFactorY = scaling / squeeze;
244 
245  rotation = atan( CPar / APar );
246  }
247 
248  translationX = transfParams[ 2 ];
249 
250  translationY = transfParams[ 5 ];
251 
252  return true;
253 }
254 
255 
std::vector< TiePoint > m_tiePoints
Tie points.
Definition: GTParameters.h:95
double y
y-coordinate.
Definition: Coord2D.h:114
void directMap(const GTParameters &params, const double &pt1X, const double &pt1Y, double &pt2X, double &pt2Y) const
Direct mapping (from pt1 space into pt2 space).
Definition: AffineGT.cpp:49
boost::numeric::ublas::matrix< double > m_computeParameters_U
Definition: AffineGT.h:127
unsigned int m_computeParameters_tiepointsSize
Definition: AffineGT.h:119
boost::numeric::ublas::matrix< double > m_computeParameters_L
Definition: AffineGT.h:123
double x
x-coordinate.
Definition: Coord2D.h:113
boost::numeric::ublas::matrix< double > m_computeParameters_A
Definition: AffineGT.h:124
boost::numeric::ublas::matrix< double > m_computeParameters_XExpandedInv
Definition: AffineGT.h:131
unsigned int m_computeParameters_index1
Definition: AffineGT.h:120
std::vector< double > m_directParameters
Transformation numeric direct parameters.
Definition: GTParameters.h:100
AffineGT()
Default constructor.
unsigned int m_computeParameters_tpIdx
Definition: AffineGT.h:122
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
2D Geometric transformation base class.
unsigned int m_computeParameters_index2
Definition: AffineGT.h:121
2D Affine Geometric transformation.
Definition: AffineGT.h:66
~AffineGT()
Destructor.
GTParameters m_internalParameters
The current internal parameters.
const std::string & getName() const
Returns the current transformation name.
Definition: AffineGT.cpp:37
bool isValid() const
Tells if the current instance has a valid transformation.
bool computeParameters(GTParameters &params) const
Calculate the transformation parameters following the new supplied tie-points.
Definition: AffineGT.cpp:87
static bool decompose(const std::vector< double > &transfParams, double &translationX, double &translationY, double &scalingFactorX, double &scalingFactorY, double &skew, double &squeeze, double &scaling, double &rotation)
Returns the basic set of transform parameters given by the decomposition of a given affine transforma...
Definition: AffineGT.cpp:188
boost::numeric::ublas::matrix< double > m_computeParameters_XExpanded
Definition: AffineGT.h:130
boost::numeric::ublas::matrix< double > m_computeParameters_At
Definition: AffineGT.h:125
boost::numeric::ublas::matrix< double > m_computeParameters_X
Definition: AffineGT.h:129
void inverseMap(const GTParameters &params, const double &pt2X, const double &pt2Y, double &pt1X, double &pt1Y) const
Inverse mapping (from pt2 space into pt1 space).
Definition: AffineGT.cpp:62
boost::numeric::ublas::matrix< double > m_computeParameters_N_inv
Definition: AffineGT.h:128
bool GetInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Matrix inversion.
Definition: MatrixUtils.h:143
std::vector< double > m_inverseParameters
Transformation numeric inverse parameters.
Definition: GTParameters.h:101
2D Geometric transformation parameters.
Definition: GTParameters.h:50
boost::numeric::ublas::matrix< double > m_computeParameters_N
Definition: AffineGT.h:126
unsigned int getMinRequiredTiePoints() const
Returns the minimum number of required tie-points for the current transformation. ...
Definition: AffineGT.cpp:75
2D Affine Geometric transformation.
GeometricTransformation * clone() const
Creat a clone copy of this instance.
Definition: AffineGT.cpp:80