All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RSTGT.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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/RSTGT.cpp
22 
23  \brief 2D Rotation/scale/translation(rigid body) Geometric transformation.
24 */
25 
26 // TerraLib
27 #include "../common/MatrixUtils.h"
28 #include "RSTGT.h"
29 
30 // STL
31 #include <cmath>
32 
33 // Boost
34 #include <boost/numeric/ublas/matrix.hpp>
35 
37 {
38 }
39 
41 {
42 }
43 
44 const std::string& te::gm::RSTGT::getName() const
45 {
46  static std::string name( "RST" );
47  return name;
48 }
49 
50 bool te::gm::RSTGT::isValid( const GTParameters& params ) const
51 {
52  return ( ( params.m_directParameters.size() == 4 ) &&
53  ( params.m_inverseParameters.size() == 4 ) );
54 }
55 
56 void te::gm::RSTGT::directMap( const GTParameters& params, const double& pt1X,
57  const double& pt1Y, double& pt2X, double& pt2Y ) const
58 {
59  assert( isValid( params ) );
60 
61  pt2X = ( params.m_directParameters[0] * pt1X ) -
62  ( params.m_directParameters[1] * pt1Y ) +
63  params.m_directParameters[2];
64  pt2Y = ( params.m_directParameters[1] * pt1X ) +
65  ( params.m_directParameters[0] * pt1Y ) +
66  params.m_directParameters[3];
67 }
68 
69 void te::gm::RSTGT::inverseMap( const GTParameters& params, const double& pt2X,
70  const double& pt2Y, double& pt1X, double& pt1Y ) const
71 {
72  assert( isValid( params ) );
73 
74  pt1X = ( params.m_inverseParameters[0] * pt2X ) -
75  ( params.m_inverseParameters[1] * pt2Y ) +
76  params.m_inverseParameters[2];
77  pt1Y = ( params.m_inverseParameters[1] * pt2X ) +
78  ( params.m_inverseParameters[0] * pt2Y ) +
79  params.m_inverseParameters[3];
80 }
81 
83 {
84  return 2;
85 }
86 
88 {
89  te::gm::RSTGT* newTransPtr = new RSTGT;
90  newTransPtr->m_internalParameters = m_internalParameters;
91  return newTransPtr;
92 };
93 
95 {
96  const unsigned int tiepointsSize = params.m_tiePoints.size();
97  if( tiepointsSize < 2 ) return false;
98 
99  boost::numeric::ublas::matrix< double > A( 2 * tiepointsSize, 4 );
100  boost::numeric::ublas::matrix< double > L( 2 * tiepointsSize, 1 );
101  unsigned int index1 = 0;
102  unsigned int index2 = 0;
103 
104  for ( unsigned int tpIdx = 0 ; tpIdx < tiepointsSize ; ++tpIdx )
105  {
106  index1 = tpIdx*2;
107  index2 = index1 + 1;
108 
109  const Coord2D& x_y = params.m_tiePoints[ tpIdx ].first;
110  const Coord2D& u_v = params.m_tiePoints[ tpIdx ].second;
111 
112  A( index1, 0 ) = x_y.x;
113  A( index1, 1 ) = -1.0 * x_y.y;
114  A( index1, 2 ) = 1.0;
115  A( index1, 3 ) = 0.0;
116 
117  A( index2, 0 ) = x_y.y;
118  A( index2, 1 ) = x_y.x;
119  A( index2, 2 ) = 0.0;
120  A( index2, 3 ) = 1.0;
121 
122  L( index1, 0 ) = u_v.x;
123  L( index2, 0 ) = u_v.y;
124  }
125 
126 // std::cout << std::endl << "L:" << std::endl << L << std::endl;
127 // std::cout << std::endl << "A:" << std::endl << A << std::endl;
128 
129  /* At calcule */
130  boost::numeric::ublas::matrix< double > At( boost::numeric::ublas::trans( A ) ) ;
131 
132  /* N calcule */
133  boost::numeric::ublas::matrix< double > N( boost::numeric::ublas::prod( At, A ) );
134 
135  /* U calcule */
136  boost::numeric::ublas::matrix< double > U( boost::numeric::ublas::prod( At, L ) );
137 
138  /* N_inv calcule */
139  boost::numeric::ublas::matrix< double > N_inv;
140 
141  if ( te::common::getInverseMatrix( N, N_inv ) )
142  {
143  /* direct parameters calcule */
144 
145  boost::numeric::ublas::matrix< double > X(
146  boost::numeric::ublas::prod( N_inv, U ) );
147 
148 // std::cout << std::endl << "X:" << std::endl << X << std::endl;
149 
150  params.m_directParameters.resize( 4 );
151  params.m_directParameters[0] = X(0,0);
152  params.m_directParameters[1] = X(1,0);
153  params.m_directParameters[2] = X(2,0);
154  params.m_directParameters[3] = X(3,0);
155 
156  /* inverse parameters calcule */
157 
158  boost::numeric::ublas::matrix< double > XExpanded( 3, 3 );
159  XExpanded( 0, 0 ) = X(0,0);
160  XExpanded( 0, 1 ) = -1.0 * X(1,0);
161  XExpanded( 0, 2 ) = X(2,0);
162  XExpanded( 1, 0 ) = X(1,0);
163  XExpanded( 1, 1 ) = X(0,0);
164  XExpanded( 1, 2 ) = X(3,0);
165  XExpanded( 2, 0 ) = 0;
166  XExpanded( 2, 1 ) = 0;
167  XExpanded( 2, 2 ) = 1;
168 
169 // std::cout << std::endl << "XExpanded:" << std::endl << XExpanded << std::endl;
170 
171  boost::numeric::ublas::matrix< double > XExpandedInv;
172 
173  if( te::common::getInverseMatrix( XExpanded, XExpandedInv ) )
174  {
175 // std::cout << std::endl << "XExpandedInv:" << std::endl << XExpandedInv << std::endl;
176 
177  params.m_inverseParameters.resize( 4 );
178  params.m_inverseParameters[0] = XExpandedInv(0,0);
179  params.m_inverseParameters[1] = XExpandedInv(1,0);
180  params.m_inverseParameters[2] = XExpandedInv(0,2);
181  params.m_inverseParameters[3] = XExpandedInv(1,2);
182 
183  return true;
184  }
185  else
186  {
187  return false;
188  }
189  }
190  else
191  {
192  return false;
193  }
194 }
195 
196 
const std::string & getName() const
Returns the current transformation name.
Definition: RSTGT.cpp:44
double y
y-coordinate.
Definition: Coord2D.h:87
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: RSTGT.cpp:69
2D Rotation/scale/translation(rigid body) Geometric transformation.
Definition: RSTGT.h:65
bool computeParameters(GTParameters &params) const
Calculate the transformation parameters following the new supplied tie-points.
Definition: RSTGT.cpp:94
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
bool isValid() const
Tells if the current instance has a valid transformation.
std::vector< TiePoint > m_tiePoints
Tie points.
Definition: GTParameters.h:95
GTParameters m_internalParameters
The current internal parameters.
std::vector< double > m_inverseParameters
Transformation numeric inverse parameters.
Definition: GTParameters.h:101
unsigned int getMinRequiredTiePoints() const
Returns the minimum number of required tie-points for the current transformation. ...
Definition: RSTGT.cpp:82
bool getInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Matrix inversion.
Definition: MatrixUtils.h:104
2D Rotation/scale/translation (rigid body) Geometric transformation.
2D Geometric transformation base class.
RSTGT()
Default constructor.
Definition: RSTGT.cpp:36
std::vector< double > m_directParameters
Transformation numeric direct parameters.
Definition: GTParameters.h:100
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: RSTGT.cpp:56
~RSTGT()
Destructor.
Definition: RSTGT.cpp:40
GeometricTransformation * clone() const
Creat a clone copy of this instance.
Definition: RSTGT.cpp:87
2D Geometric transformation parameters.
Definition: GTParameters.h:50
double x
x-coordinate.
Definition: Coord2D.h:86