00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00044 #ifndef _TRANSFORMATION2_HXX_
00045 #define _TRANSFORMATION2_HXX_
00046
00047 #include <cmath>
00048
00050 template <class T>
00051 struct Vector2{
00052 T values[2] ;
00053
00055 Vector2(T x, T y) {values[0]=x; values[1]=y;}
00057 Vector2() {values[0]=0; values[1]=0;}
00058
00060 inline const T& x() const {return values[0];}
00062 inline const T& y() const {return values[1];}
00063
00065 inline T& x() {return values[0];}
00067 inline T& y() {return values[1];}
00068
00070 inline T norm2() const {
00071 return values[0]*values[0]+values[1]*values[1];
00072 }
00073
00074 };
00075
00077 template <class T>
00078 inline Vector2<T> operator * (const T& d, const Vector2<T>& v) {
00079 return Vector2<T>(v.values[0]*d, v.values[1]*d);
00080 }
00081
00083 template <class T>
00084 inline Vector2<T> operator * (const Vector2<T>& v, const T& d) {
00085 return Vector2<T>(v.values[0]*d, v.values[1]*d);
00086 }
00087
00089 template <class T>
00090 inline T operator * (const Vector2<T>& v1, const Vector2<T>& v2){
00091 return v1.values[0]*v2.values[0]
00092 + v1.values[1]*v2.values[1];
00093 }
00094
00096 template <class T>
00097 inline Vector2<T> operator + (const Vector2<T>& v1, const Vector2<T>& v2){
00098 return Vector2<T>(v1.values[0]+v2.values[0],
00099 v1.values[1]+v2.values[1]);
00100 }
00101
00103 template <class T>
00104 Vector2<T> operator - (const Vector2<T>& v1, const Vector2<T>& v2){
00105 return Vector2<T>(v1.values[0]-v2.values[0],
00106 v1.values[1]-v2.values[1]);
00107 }
00108
00109
00116 template <class T>
00117 struct Pose2{
00118 T values[3];
00119
00121 inline const T& x() const {return values[0];}
00123 inline const T& y() const {return values[1];}
00125 inline const T& theta() const {return values[2];}
00126
00128 inline T& x() {return values[0];}
00130 inline T& y() {return values[1];}
00132 inline T& theta() {return values[2];}
00133
00135 Pose2(){
00136 values[0]=0.; values[1]=0.; values[2]=0.;
00137 }
00138
00140 Pose2(const T& x, const T& y, const T& theta){
00141 values[0]=x, values[1]=y, values[2]=theta;
00142 }
00143 };
00144
00146 template <class T>
00147 Pose2<T> operator * (const Pose2<T>& v, const T& d){
00148 Pose2<T> r;
00149 for (int i=0; i<3; i++){
00150 r.values[i]=v.values[i]*d;
00151 }
00152 return r;
00153 }
00154
00155
00157 template <class T>
00158 struct Transformation2{
00159 T rotationMatrix[2][2];
00160 T translationVector[2];
00161
00165 Transformation2(bool initAsIdentity = true){
00166 if (initAsIdentity) {
00167 rotationMatrix[0][0]=1.; rotationMatrix[0][1]=0.;
00168 rotationMatrix[1][0]=0.; rotationMatrix[1][1]=1.;
00169 translationVector[0]=0.;
00170 translationVector[1]=0.;
00171 }
00172 }
00173
00175 inline static Transformation2<T> identity(){
00176 Transformation2<T> m(true);
00177 return m;
00178 }
00179
00181 Transformation2 (const T& x, const T& y, const T& theta){
00182 setRotation(theta);
00183 setTranslation(x,y);
00184 }
00185
00187 Transformation2 (const T& _theta, const Vector2<T>& trans):
00188 Transformation2(trans.x(), trans.y(), _theta){}
00189
00190
00192 Transformation2 (const Pose2<T>& v){
00193 setRotation(v.theta());
00194 setTranslation(v.x(),v.y());
00195 }
00196
00197
00199 inline Vector2<T> translation() const {
00200 return Vector2<T>(translationVector[0],
00201 translationVector[1]);
00202 }
00203
00205 inline T rotation() const {
00206 return atan2(rotationMatrix[1][0],rotationMatrix[0][0]);
00207 }
00208
00210 inline Pose2<T> toPoseType() const {
00211 Vector2<T> t=translation();
00212 T r=rotation();
00213 Pose2<T> rv(t.x(), t.y(), r );
00214 return rv;
00215 }
00216
00218 inline void setTranslation(const Vector2<T>& t){
00219 setTranslation(t.x(),t.y());
00220 }
00221
00223 inline void setRotation(const T& theta){
00224 T s=sin(theta), c=cos(theta);
00225 rotationMatrix[0][0]=c, rotationMatrix[0][1]=-s;
00226 rotationMatrix[1][0]=s, rotationMatrix[1][1]= c;
00227 }
00228
00230 inline void setTranslation(const T& x, const T& y){
00231 translationVector[0]=x;
00232 translationVector[1]=y;
00233 }
00234
00236 inline Transformation2<T> inv() const {
00237 Transformation2<T> rv(*this);
00238 for (int i=0; i<2; i++)
00239 for (int j=0; j<2; j++){
00240 rv.rotationMatrix[i][j]=rotationMatrix[j][i];
00241 }
00242
00243 for (int i=0; i<2; i++){
00244 rv.translationVector[i]=0;
00245 for (int j=0; j<2; j++){
00246 rv.translationVector[i]-=rv.rotationMatrix[i][j]*translationVector[j];
00247 }
00248 }
00249 return rv;
00250 }
00251
00252 };
00253
00255 template <class T>
00256 Vector2<T> operator * (const Transformation2<T>& m, const Vector2<T>& v){
00257 return Vector2<T>(
00258 m.rotationMatrix[0][0]*v.values[0]+
00259 m.rotationMatrix[0][1]*v.values[1]+
00260 m.translationVector[0],
00261 m.rotationMatrix[1][0]*v.values[0]+
00262 m.rotationMatrix[1][1]*v.values[1]+
00263 m.translationVector[1]);
00264 }
00265
00267 template <class T>
00268 Transformation2<T> operator * (const Transformation2<T>& m1, const Transformation2<T>& m2){
00269 Transformation2<T> rt;
00270 for (int i=0; i<2; i++)
00271 for (int j=0; j<2; j++){
00272 rt.rotationMatrix[i][j]=0.;
00273 for (int k=0; k<2; k++)
00274 rt.rotationMatrix[i][j]+=m1.rotationMatrix[i][k]*m2.rotationMatrix[k][j];
00275 }
00276 for (int i=0; i<2; i++){
00277 rt.translationVector[i]=m1.translationVector[i];
00278 for (int j=0; j<2; j++)
00279 rt.translationVector[i]+=m1.rotationMatrix[i][j]*m2.translationVector[j];
00280 }
00281 return rt;
00282 }
00283
00284
00286 template <class T>
00287 struct SMatrix3{
00288 T values[3][3];
00289 T det() const;
00290 SMatrix3<T> transpose() const;
00291 SMatrix3<T> adj() const;
00292 SMatrix3<T> inv() const;
00293 };
00294
00295
00297 template <class T>
00298 Pose2<T> operator * (const SMatrix3<T>& m, const Pose2<T>& p){
00299 Pose2<T> v;
00300 for (int i=0; i<3; i++){
00301 v.values[i]=0.;
00302 for (int j=0; j<3; j++)
00303 v.values[i]+=m.values[i][j]*p.values[j];
00304 }
00305 return v;
00306 }
00307
00309 template <class T>
00310 SMatrix3<T> operator * (const SMatrix3<T>& s, T& d){
00311 SMatrix3<T> m;
00312 for (int i=0; i<3; i++)
00313 for (int j=0; j<3; j++)
00314 m.values[i][j]=d*s.values[i][j];
00315 return m;
00316 }
00317
00319 template <class T>
00320 SMatrix3<T> operator * (const SMatrix3<T>& s1, const SMatrix3<T>& s2){
00321 SMatrix3<T> m;
00322 for (int i=0; i<3; i++)
00323 for (int j=0; j<3; j++){
00324 m.values[i][j]=0.;
00325 for (int k=0; k<3; k++){
00326 m.values[i][j]+=s1.values[i][k]*s2.values[k][j];
00327 }
00328 }
00329 return m;
00330 }
00331
00333 template <class T>
00334 SMatrix3<T> operator + (const SMatrix3<T>& s1, const SMatrix3<T>& s2){
00335 SMatrix3<T> m;
00336 for (int i=0; i<3; i++)
00337 for (int j=0; j<3; j++){
00338 m.values[i][j]=s1.values[i][j]+s2.values[i][j];
00339 }
00340 return m;
00341 }
00342
00343
00345 template <class T>
00346 T SMatrix3<T>::det() const{
00347 T dp= values[0][0]*values[1][1]*values[2][2]
00348 +values[0][1]*values[1][2]*values[2][0]
00349 +values[0][2]*values[1][0]*values[2][1];
00350 T dm=values[2][0]*values[1][1]*values[0][2]
00351 +values[2][1]*values[1][2]*values[0][0]
00352 +values[2][2]*values[1][0]*values[0][1];
00353 return dp-dm;
00354 }
00355
00357 template <class T>
00358 SMatrix3<T> SMatrix3<T>::transpose() const{
00359 SMatrix3<T> m;
00360 for (int i=0; i<3; i++)
00361 for (int j=0; j<3; j++)
00362 m.values[j][i]=values[i][j];
00363 return m;
00364 }
00365
00367 template <class T>
00368 SMatrix3<T> SMatrix3<T>::adj() const{
00369 SMatrix3<T> m;
00370 m.values[0][0]= values[1][1]*values[2][2]-values[2][1]*values[1][2];
00371 m.values[0][1]=-values[1][0]*values[2][2]+values[1][2]*values[2][0];
00372 m.values[0][2]= values[1][0]*values[2][1]-values[2][0]*values[1][1];
00373 m.values[1][0]=-values[0][1]*values[2][2]+values[2][1]*values[0][2];
00374 m.values[1][1]= values[0][0]*values[2][2]-values[2][0]*values[0][2];
00375 m.values[1][2]=-values[0][0]*values[2][1]+values[2][0]*values[0][1];
00376 m.values[2][0]= values[0][1]*values[1][2]-values[1][1]*values[0][2];
00377 m.values[2][1]=-values[0][0]*values[1][2]+values[1][0]*values[0][2];
00378 m.values[2][2]= values[0][0]*values[1][1]-values[1][0]*values[0][1];
00379 return m;
00380 }
00381
00383 template <class T>
00384 SMatrix3<T> SMatrix3<T>::inv() const{
00385 T id=1./det();
00386 SMatrix3<T> i=adj().transpose();
00387 return i*id;
00388 }
00389
00390
00391
00393 template <class T>
00394 struct Operations2D{
00395 typedef T BaseType;
00396 typedef Pose2<T> PoseType;
00397 typedef Pose2<T> ParametersType;
00398 typedef T RotationType;
00399 typedef Vector2<T> TranslationType;
00400 typedef Transformation2<T> TransformationType;
00401 typedef SMatrix3<T> CovarianceType;
00402 typedef SMatrix3<T> InformationType;
00403 };
00404
00405 #endif