All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SecondDegreePolynomialGT.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/SecondDegreePolynomialGT.cpp
22 
23  \brief Second Degree Polynomial Geometric transformation.
24 */
25 
26 // TerraLib
27 #include "../common/MatrixUtils.h"
29 
30 // STL
31 #include <cmath>
32 
33 // Boost
34 #include <boost/numeric/ublas/matrix.hpp>
35 
37 {
38 }
39 
41 {
42 }
43 
45 {
46  static std::string name( "SecondDegreePolynomial" );
47  return name;
48 }
49 
51 {
52  return ( ( params.m_directParameters.size() == 12 ) &&
53  ( params.m_inverseParameters.size() == 12 ) );
54 }
55 
56 void te::gm::SecondDegreePolynomialGT::directMap( const GTParameters& params, const double& pt1X,
57  const double& pt1Y, double& pt2X, double& pt2Y ) const
58 {
59  assert( isValid( params ) );
60 
61  pt2X =
62  params.m_directParameters[ 0 ] +
63  ( params.m_directParameters[ 1 ] * pt1X ) +
64  ( params.m_directParameters[ 2 ] * pt1Y ) +
65  ( params.m_directParameters[ 3 ] * pt1X * pt1Y ) +
66  ( params.m_directParameters[ 4 ] * pt1X * pt1X ) +
67  ( params.m_directParameters[ 5 ] * pt1Y * pt1Y );
68 
69  pt2Y =
70  params.m_directParameters[ 6 ] +
71  ( params.m_directParameters[ 7 ] * pt1X ) +
72  ( params.m_directParameters[ 8 ] * pt1Y ) +
73  ( params.m_directParameters[ 9 ] * pt1X * pt1Y ) +
74  ( params.m_directParameters[ 10 ] * pt1X * pt1X ) +
75  ( params.m_directParameters[ 11 ] * pt1Y * pt1Y );
76 }
77 
78 void te::gm::SecondDegreePolynomialGT::inverseMap( const GTParameters& params, const double& pt2X,
79  const double& pt2Y, double& pt1X, double& pt1Y ) const
80 {
81  assert( isValid( params ) );
82 
83  pt1X =
84  params.m_inverseParameters[ 0 ] +
85  ( params.m_inverseParameters[ 1 ] * pt2X ) +
86  ( params.m_inverseParameters[ 2 ] * pt2Y ) +
87  ( params.m_inverseParameters[ 3 ] * pt2X * pt2Y ) +
88  ( params.m_inverseParameters[ 4 ] * pt2X * pt2X ) +
89  ( params.m_inverseParameters[ 5 ] * pt2Y * pt2Y );
90 
91  pt1Y =
92  params.m_inverseParameters[ 6 ] +
93  ( params.m_inverseParameters[ 7 ] * pt2X ) +
94  ( params.m_inverseParameters[ 8 ] * pt2Y ) +
95  ( params.m_inverseParameters[ 9 ] * pt2X * pt2Y ) +
96  ( params.m_inverseParameters[ 10 ] * pt2X * pt2X ) +
97  ( params.m_inverseParameters[ 11 ] * pt2Y * pt2Y );
98 }
99 
101 {
102  return 7;
103 }
104 
106 {
108  newTransPtr->m_internalParameters = m_internalParameters;
109  return newTransPtr;
110 };
111 
113 {
114  /* Reference: Remote Sensing - Models and Methods For Image Processing
115  Second Edition
116  Robert A. Schowengerdt
117  Academic Press
118  */
119 
120  // Creating the equation system parameters
121 
122  const unsigned int tiepointsSize = params.m_tiePoints.size();
123  if( tiepointsSize < getMinRequiredTiePoints() ) return false;
124 
125  boost::numeric::ublas::matrix< double > W( tiepointsSize, 6 );
126  boost::numeric::ublas::matrix< double > WI( tiepointsSize, 6 );
127  boost::numeric::ublas::matrix< double > X( tiepointsSize, 1 );
128  boost::numeric::ublas::matrix< double > XI( tiepointsSize, 1 );
129  boost::numeric::ublas::matrix< double > Y( tiepointsSize, 1 );
130  boost::numeric::ublas::matrix< double > YI( tiepointsSize, 1 );
131 
132  for ( unsigned int tpIdx = 0 ; tpIdx < tiepointsSize ; ++tpIdx )
133  {
134  const Coord2D& pt1 = params.m_tiePoints[ tpIdx ].first;
135 
136  W( tpIdx, 0 ) = 1;
137  W( tpIdx, 1 ) = pt1.x;
138  W( tpIdx, 2 ) = pt1.y;
139  W( tpIdx, 3 ) = pt1.x * pt1.y;
140  W( tpIdx, 4 ) = pt1.x * pt1.x;
141  W( tpIdx, 5 ) = pt1.y * pt1.y;
142 
143  const Coord2D& pt2 = params.m_tiePoints[ tpIdx ].second;
144 
145  WI( tpIdx, 0 ) = 1;
146  WI( tpIdx, 1 ) = pt2.x;
147  WI( tpIdx, 2 ) = pt2.y;
148  WI( tpIdx, 3 ) = pt2.x * pt2.y;
149  WI( tpIdx, 4 ) = pt2.x * pt2.x;
150  WI( tpIdx, 5 ) = pt2.y * pt2.y;
151 
152  X( tpIdx, 0 ) = pt2.x;
153 
154  XI( tpIdx, 0 ) = pt1.x;
155 
156  Y( tpIdx, 0 ) = pt2.y;
157 
158  YI( tpIdx, 0 ) = pt1.y;
159  }
160 
161  // Solving...
162 
163  boost::numeric::ublas::matrix< double > PinvW;
164  if( ! te::common::GetPseudoInverseMatrix( W, PinvW ) ) return false;
165 
166  boost::numeric::ublas::matrix< double > PinvWI;
167  if( ! te::common::GetPseudoInverseMatrix( WI, PinvWI ) ) return false;
168 
169  boost::numeric::ublas::matrix< double > A( boost::numeric::ublas::prod( PinvW, X ) );
170 
171  boost::numeric::ublas::matrix< double > AI( boost::numeric::ublas::prod( PinvWI, XI ) );
172 
173  boost::numeric::ublas::matrix< double > B( boost::numeric::ublas::prod( PinvW, Y ) );
174 
175  boost::numeric::ublas::matrix< double > BI( boost::numeric::ublas::prod( PinvWI, YI ) );
176 
177  // Copying the parameters to output
178 
179  params.m_directParameters.resize( 12 );
180  params.m_directParameters[ 0 ] = A( 0, 0 );
181  params.m_directParameters[ 1 ] = A( 1, 0 );
182  params.m_directParameters[ 2 ] = A( 2, 0 );
183  params.m_directParameters[ 3 ] = A( 3, 0 );
184  params.m_directParameters[ 4 ] = A( 4, 0 );
185  params.m_directParameters[ 5 ] = A( 5, 0 );
186  params.m_directParameters[ 6 ] = B( 0, 0 );
187  params.m_directParameters[ 7 ] = B( 1, 0 );
188  params.m_directParameters[ 8 ] = B( 2, 0 );
189  params.m_directParameters[ 9 ] = B( 3, 0 );
190  params.m_directParameters[ 10 ] = B( 4, 0 );
191  params.m_directParameters[ 11 ] = B( 5, 0 );
192 
193  params.m_inverseParameters.resize( 12 );
194  params.m_inverseParameters[ 0 ] = AI( 0, 0 );
195  params.m_inverseParameters[ 1 ] = AI( 1, 0 );
196  params.m_inverseParameters[ 2 ] = AI( 2, 0 );
197  params.m_inverseParameters[ 3 ] = AI( 3, 0 );
198  params.m_inverseParameters[ 4 ] = AI( 4, 0 );
199  params.m_inverseParameters[ 5 ] = AI( 5, 0 );
200  params.m_inverseParameters[ 6 ] = BI( 0, 0 );
201  params.m_inverseParameters[ 7 ] = BI( 1, 0 );
202  params.m_inverseParameters[ 8 ] = BI( 2, 0 );
203  params.m_inverseParameters[ 9 ] = BI( 3, 0 );
204  params.m_inverseParameters[ 10 ] = BI( 4, 0 );
205  params.m_inverseParameters[ 11 ] = BI( 5, 0 );
206 
207  return true;
208 }
209 
210 
Second Degree Polynomial Geometric transformation.
std::vector< TiePoint > m_tiePoints
Tie points.
Definition: GTParameters.h:95
const std::string & getName() const
Returns the current transformation name.
double y
y-coordinate.
Definition: Coord2D.h:87
double x
x-coordinate.
Definition: Coord2D.h:86
Second Degree Polynomial Geometric transformation.
std::vector< double > m_directParameters
Transformation numeric direct parameters.
Definition: GTParameters.h:100
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
2D Geometric transformation base class.
bool GetPseudoInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Pseudo matrix inversion.
Definition: MatrixUtils.h:152
GTParameters m_internalParameters
The current internal parameters.
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.
void inverseMap(const GTParameters &params, const double &pt2X, const double &pt2Y, double &pt1X, double &pt1Y) const
Inverse mapping (from pt2 space into pt1 space).
std::vector< double > m_inverseParameters
Transformation numeric inverse parameters.
Definition: GTParameters.h:101
2D Geometric transformation parameters.
Definition: GTParameters.h:50
unsigned int getMinRequiredTiePoints() const
Returns the minimum number of required tie-points for the current transformation. ...
GeometricTransformation * clone() const
Creat a clone copy of this instance.
void directMap(const GTParameters &params, const double &pt1X, const double &pt1Y, double &pt2X, double &pt2Y) const
Direct mapping (from pt1 space into pt2 space).