Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TextureTransformRotation.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __TextureTransformRotation__hh__
26 #define __TextureTransformRotation__hh__
27 
28 #include <Base/Common/BIASpragmaStart.hh>
29 #include <Image/TextureTransform.hh>
30 #include <Base/Common/CompareFloatingPoint.hh>
31 
32 namespace BIAS {
33 
34  /** @class TextureTransformRotation
35  * @brief analytic properties of rotational image warp (around some point)
36  *
37  * See BIAS::TextureTransform for explanation of methods.
38  * @author koeser 04/2008
39  **/
41  public:
43  P_.newsize(1);
44  P_[0] = 0;
45  }
46 
48 
49  int MapForward(const HomgPoint2D& src, HomgPoint2D& sink) const {
50  const double& sx = src[0];
51  const double& sy = src[1];
52  sink[0] = A_[0][0]*sx + A_[0][1]*sy + tx_;
53  sink[1] = A_[1][0]*sx + A_[1][1]*sy + ty_;
54  sink[2] = 1;
55  return 0;
56  }
57 
58  int MapBackward(const HomgPoint2D& sink, HomgPoint2D& src) const {
59  src[0] = Ainv_[0][0]*sink[0] + Ainv_[0][1]*sink[1] + txinv_;
60  src[1] = Ainv_[1][0]*sink[0] + Ainv_[1][1]*sink[1] + tyinv_;
61  src[2] = 1;
62 #ifdef BIAS_DEBUG
63  HomgPoint2D testsink;
64  MapForward(src, testsink);
65  if (!Equal(testsink[0]+1e-4,sink[0]+1e-4, 1e-4) ||
66  !Equal(testsink[1]+1e-4,sink[1]+1e-4, 1e-4)) {
67  BIASERR(testsink<<" and "<<sink<<" are not equal!!!");
68  BIASABORT;
69  }
70 #endif
71  return 0;
72  }
73 
75  Matrix2x2<double>& Jac) const {
76  Jac = A_;
77  return 0;
78  }
79 
81  Matrix2x2<double>& Jac) const {
82  Jac = Ainv_;
83  return 0;
84  }
85 
86  /** @brief local warp is the same at any image position */
87  virtual bool TextureJacobianIsConstant() const { return true;};
88 
90  Jac.newsize(2,1);
91 
92  const double sinphi = sin(P_[0]);
93  const double cosphi = cos(P_[0]);
94 
95  const double x = src[0]-origin1_[0];
96  const double y = src[1]-origin1_[1];
97 
98  Jac[0][0] = -sinphi*x-cosphi*y;
99  Jac[1][0] = cosphi*x-sinphi*y;
100 
101  return 0;
102  }
103 
105  Jac.newsize(2,1);
106  const double sinphi = sin(P_[0]);
107  const double cosphi = cos(P_[0]);
108 
109  const double x = src[0]-origin1_[0];
110  const double y = src[1]-origin1_[1];
111  // dx/dP_ ( dcos(x)/dx = -sin, therefore sin and cos appear swapped)
112  Jac[0][0] = -sinphi*x+cosphi*y;
113 
114  // dy/dP_
115  Jac[1][0] = -cosphi*x-sinphi*y;
116 
117  return 0;
118  }
119 
120  /** @brief the jacobian depends on x */
121  bool ParameterJacobianIsConstant() const { return false;};
122 
123 
124  /** @brief rotation(rad) with respect to origin_!
125  * x' = A*(x-origin)+origin
126  where A==[cos -sin; sin cos]
127  */
128  void SetParameters(const Vector<double>& p) {
129  BIASASSERT(p.Size()==1);
130  P_ = p;
131  Ainv_[0][0] = Ainv_[1][1] = A_[0][0] = A_[1][1] = cos(p[0]);
132  Ainv_[1][0] = A_[0][1] = -sin(p[0]);
133  Ainv_[0][1] = A_[1][0] = -A_[0][1];
134 
136 
137  tx_ = -offset[0];
138  ty_ = -offset[1];
139 
140  txinv_ = -(Ainv_[0][0]*tx_ + Ainv_[0][1]*ty_);
141  tyinv_ = -(Ainv_[1][0]*tx_ + Ainv_[1][1]*ty_);
142  }
143 
145  Vector<double> p(1);
146  p[0] = P_[0] - deltaP[0];
147  SetParameters(p);
148  }
149 
151  // inverse is simply inverse angle: alpha -> -alpha
152  Jac.newsize(1,1);
153  Jac[0][0] = -1;
154  return 0;
155  }
156 
157  virtual TextureTransformRotation* Clone() const {
158  return new TextureTransformRotation(*this);
159  }
160 
161  /** @brief set origin in image1 and image2 */
162  void SetOrigin(const Vector2<double>& origin1) {
163  origin1_ = origin1;
164  SetParameters(P_);
165  }
166 
167  /** @brief origin relative to which rotation and scale is performed */
168  const Vector2<double>& GetOrigin() const {
169  return origin1_;
170  }
171 
172  protected:
173  /// cached local warp and inverse
175  /// cached displacement and inverse
176  double tx_, ty_, txinv_, tyinv_;
177  /// origin relative to which rotation and scale is performed
179  };
180 
181 } // end namespace BIAS
182 
183 #endif
virtual bool TextureJacobianIsConstant() const
local warp is the same at any image position
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int ParameterInversionJacobian(Matrix< double > &Jac) const
compute parameters for inverse operation and obtain the jacobian of the inverse parameters with respe...
const Vector2< double > & GetOrigin() const
origin relative to which rotation and scale is performed
double tx_
cached displacement and inverse
analytic properties of rotational image warp (around some point)
Vector< double > P_
current set of parameters, see SetParameters for meaning
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
Vector2< double > origin1_
origin relative to which rotation and scale is performed
class for representing parameterized image warps, such as homography, displacement, ...
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
int MapBackward(const HomgPoint2D &sink, HomgPoint2D &src) const
map a point in image &quot;source&quot; to a point in image &quot;sink&quot;
int ParameterJacobianBackward(Matrix< double > &Jac, const HomgPoint2D &src)
transformed position change when parameters change
int TextureJacobianForward(const HomgPoint2D &src, Matrix2x2< double > &Jac) const
shape change of the local region when mapping forward
int TextureJacobianBackward(const HomgPoint2D &sink, Matrix2x2< double > &Jac) const
shape change of the local region when mapping backward
int MapForward(const HomgPoint2D &src, HomgPoint2D &sink) const
map a point in image &quot;source&quot; to a point in image &quot;sink&quot;
void SetOrigin(const Vector2< double > &origin1)
set origin in image1 and image2
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
bool ParameterJacobianIsConstant() const
the jacobian depends on x
virtual TextureTransformRotation * Clone() const
virtual covariant copy constructor, caller must eventually destroy the created object ...
void ComposeWithInverseDeltaP(const Vector< double > &deltaP)
concatenate *this and an inverse transform with param deltaP and save new parameter vector to *this...
Matrix2x2< double > A_
cached local warp and inverse
int ParameterJacobianForward(Matrix< double > &Jac, const HomgPoint2D &src)
transformed position change when parameters change
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
void SetParameters(const Vector< double > &p)
rotation(rad) with respect to origin_! x&#39; = A*(x-origin)+origin where A==[cos -sin; sin cos] ...