1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
* Copyright (C) 2010 Learning Algorithms and Systems Laboratory, EPFL, Switzerland
* Author: Eric Sauser
* email: eric.sauser@a3.epf.ch
* website: lasa.epfl.ch
*
* Permission is granted to copy, distribute, and/or modify this program
* under the terms of the GNU General Public License, version 2 or any
* later version published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details
*/
#ifndef MATRIX3_H
#define MATRIX3_H
#include "MathLibCommon.h"
#include <math.h>
#include "TMatrix.h"
#include "Vector3.h"
#ifdef USE_MATHLIB_NAMESPACE
namespace MathLib {
#endif
/**
* \class Matrix3
*
* \ingroup MathLib
*
* \brief An implementation of the template TMatrix class
*
* This template square matrix class can be used for doing various matrix manipulation.
* This should be combined with the TVector class for doing almost anything
* you ever dreamt of.
*/
class Matrix3 : public TMatrix<3>
{
//friend class Referential;
//friend class Matrix4;
public:
/// A constant zero matrix
static const Matrix3 ZERO;
/// A constant identity matrix
static const Matrix3 IDENTITY;
/// Empty constructor
inline Matrix3(bool clear=true):TMatrix<3>(clear){};
/// Copy constructor
inline Matrix3(const Matrix3 &matrix):TMatrix<3>(matrix){};
/// Copy constructor
inline Matrix3(const TMatrix<3> &matrix):TMatrix<3>(matrix){};
/*
/// Copy constructor
//inline Matrix3(const Matrix & matrix):TMatrix<3>(matrix){};
*/
/// Constructor with data pointer
inline Matrix3(const REALTYPE *array):TMatrix<3>(array){};
/// Constructor with values
inline Matrix3(REALTYPE _00, REALTYPE _01, REALTYPE _02,
REALTYPE _10, REALTYPE _11, REALTYPE _12,
REALTYPE _20, REALTYPE _21, REALTYPE _22):TMatrix<3>(false){
Set(_00,_01,_02,
_10,_11,_12,
_20,_21,_22);
}
inline virtual ~Matrix3(){}
inline Matrix3& Set(REALTYPE _00, REALTYPE _01, REALTYPE _02,
REALTYPE _10, REALTYPE _11, REALTYPE _12,
REALTYPE _20, REALTYPE _21, REALTYPE _22){
REALTYPE *dst = _;
*(dst++) = _00; *(dst++) = _01; *(dst++) = _02;
*(dst++) = _10; *(dst++) = _11; *(dst++) = _12;
*(dst++) = _20; *(dst++) = _21; *(dst++) = _22;
return *this;
}
inline Matrix3& Set(const Matrix3& matrix){
TMatrix<3>::Set(matrix);
return *this;
}
inline Matrix3& Set(const Matrix& matrix){
TMatrix<3>::Set(matrix);
return *this;
}
inline Matrix3& Set(const REALTYPE *array){
TMatrix<3>::Set(array);
return *this;
}
/*
inline Matrix3& Set(const Matrix& matrix){
TMatrix<3>::Set(matrix);
return *this;
}
*/
/// Assign each column
inline Matrix3& SetColumns(const Vector3 &vector0, const Vector3 &vector1, const Vector3 &vector2){
SetColumn(vector0,0);
SetColumn(vector1,1);
SetColumn(vector2,2);
return *this;
}
/// Assign each row
inline Matrix3& SetRows(const Vector3 &vector0, const Vector3 &vector1, const Vector3 &vector2){
SetRow(vector0,0);
SetRow(vector1,1);
SetRow(vector2,2);
return *this;
}
/// Create a skew symmetric matix from a vector
inline Matrix3& SkewSymmetric(Vector3 vector){
REALTYPE *dst=_;
*(dst++) = R_ZERO; *(dst++) =-vector._[2]; *(dst++) = vector._[1];
*(dst++) = vector._[2]; *(dst++) = R_ZERO; *(dst++) =-vector._[0];
*(dst++) =-vector._[1]; *(dst++) = vector._[0]; *(dst++) = R_ZERO;
return *this;
}
inline static Matrix3 SRotationX(REALTYPE angleX){
Matrix3 result; return result.RotationX(angleX);
}
inline static Matrix3 SRotationY(REALTYPE angleY){
Matrix3 result; return result.RotationY(angleY);
}
inline static Matrix3 SRotationZ(REALTYPE angleZ){
Matrix3 result; return result.RotationZ(angleZ);
}
inline static Matrix3 SRotationYXZ(REALTYPE angleX, REALTYPE angleY, REALTYPE angleZ){
Matrix3 result; return result.RotationYXZ(angleX,angleY,angleZ);
}
inline static Matrix3 SRotationV(REALTYPE angle, const Vector3 &axis){
Matrix3 result; return result.RotationV(angle,axis);
}
inline static Matrix3 SRotationV(const Vector3 &axis){
Matrix3 result; return result.RotationV(axis);
}
/// Create a rotation matrix around axis X with a given angle
Matrix3& RotationX(REALTYPE angleX);
/// Create a rotation matrix around axis Y with a given angle
Matrix3& RotationY(REALTYPE angleY);
/// Create a rotation matrix around axis Z with a given angle
Matrix3& RotationZ(REALTYPE angleZ);
/// Create a rotation matrix around axes X,Y,Z with given angles
Matrix3& RotationYXZ(REALTYPE angleX, REALTYPE angleY, REALTYPE angleZ);
/// Create a rotation matrix around an arbitrary axis with a given angle
Matrix3& RotationV(REALTYPE angle, const Vector3 &axis);
/// Create a rotation matrix around an arbitrary axis with a given angle
inline Matrix3& RotationV(const Vector3 &axis){
return RotationV(axis.Norm(),axis);
}
/// Get the rotation axis of a rotation matrix (arbitrary norm)
inline Vector3 GetRotationAxis(){
Vector3 result;
return GetRotationAxis(result);
}
/// Get the rotation axis of a rotation matrix (arbitrary norm)
inline Vector3& GetRotationAxis(Vector3 &result){
result._[0] = _[2*3+1]-_[1*3+2];
result._[1] = _[0*3+2]-_[2*3+0];
result._[2] = _[1*3+0]-_[0*3+1];
return result;
}
/// Get the rotation axis of a rotation matrix (the norm equals the rotation angle)
inline Vector3 GetExactRotationAxis() const{
Vector3 result;
return GetExactRotationAxis(result);
}
/// Get the rotation axis of a rotation matrix (the norm equals the rotation angle)
inline Vector3& GetExactRotationAxis(Vector3 &result) const{
GetNormRotationAxis(result);
result *= GetRotationAngle();
return result;
}
/// Get the rotation axis of a rotation matrix (the norm equals 1)
inline Vector3& GetNormRotationAxis(Vector3 &result) const{
result._[0] = _[2*3+1]-_[1*3+2];
result._[1] = _[0*3+2]-_[2*3+0];
result._[2] = _[1*3+0]-_[0*3+1];
REALTYPE norm = result.Norm();
if(norm>EPSILON)
result*=(R_ONE/norm);
else
result.Zero();
return result;
}
/// Get the rotation angle of a rotation matrix
inline REALTYPE GetRotationAngle() const{
REALTYPE res = (_[0*3+0]+_[1*3+1]+_[2*3+2]-1.0f)/2.0;
if(res>R_ONE) return R_ZERO;
else if (res<-R_ONE) return PI;
else return acos(res);
}
/// Return a matrix where the amount of rotation has been scaled
inline Matrix3& RotationScale(REALTYPE scale){
Matrix3 result;
return RotationScale(scale,result);
}
/// Return a matrix where the amount of rotation has been scaled
inline Matrix3& RotationScale(REALTYPE scale, Matrix3 & result){
Vector3 v;
GetNormRotationAxis(v);
result.RotationV(GetRotationAngle()*scale,v);
return result;
}
/// Return a rotation matrix in between src and dst. Scale is in [0,1]
inline Matrix3& RotationScale(const Matrix3& src, const Matrix3& dst, REALTYPE scale){
if(scale<R_ZERO) scale = R_ZERO;
if(scale>R_ONE) scale = R_ONE;
Matrix3 tmpM;
src.Transpose(tmpM);
tmpM.Mult(dst,*this);
Vector3 tmpV;
GetExactRotationAxis(tmpV);
tmpV *= scale;
tmpM.RotationV(tmpV);
src.Mult(tmpM,*this);
return *this;
}
/// Normalize the matrix (Gram-Schmidt) with the given primary axis (x=0,y=1,z=2)
Matrix3& Normalize(int mainAxe = 2)
{
if((mainAxe<0)||(mainAxe>2)) mainAxe=2;
Vector3 v0(_[0*3+0],_[1*3+0],_[2*3+0]);
Vector3 v1(_[0*3+1],_[1*3+1],_[2*3+1]);
Vector3 v2(_[0*3+2],_[1*3+2],_[2*3+2]);
switch(mainAxe){
case 0:
v0.Normalize();
v1-=v0 * v0.Dot(v1);
v1.Normalize();
v2 = v0.Cross(v1);
break;
case 1:
v1.Normalize();
v2-=v1 * v1.Dot(v2);
v2.Normalize();
v0 = v1.Cross(v2);
break;
case 2:
v2.Normalize();
v1-=v2 * v2.Dot(v1);
v1.Normalize();
v0 = v1.Cross(v2);
break;
}
SetColumn(v0,0);
SetColumn(v1,1);
SetColumn(v2,2);
return *this;
}
/// Normalize the matrix (Gram-Schmidt) according to the given order of axis (x=0,y=1,z=2)
Matrix3& Normalize(int firAxe, int secAxe, int trdAxe)
{
Vector3 v0(_[0*3+0],_[1*3+0],_[2*3+0]);
Vector3 v1(_[0*3+1],_[1*3+1],_[2*3+1]);
Vector3 v2(_[0*3+2],_[1*3+2],_[2*3+2]);
Vector3 w0;
Vector3 w1;
Vector3 w2;
switch(firAxe){case 0: w0 = v0; break; case 1: w0 = v1; break; case 2: w0 = v2; break;}
switch(secAxe){case 0: w1 = v0; break; case 1: w1 = v1; break; case 2: w1 = v2; break;}
switch(trdAxe){case 0: w2 = v0; break; case 1: w2 = v1; break; case 2: w2 = v2; break;}
w0.Normalize();
w1-=w0 * w0.Dot(w1);
w1.Normalize();
w2 = w0.Cross(w1);
switch(firAxe){case 0: v0 = w0; break; case 1: v0 = w1; break; case 2: v0 = w2; break;}
switch(secAxe){case 0: v1 = w0; break; case 1: v1 = w1; break; case 2: v1 = w2; break;}
switch(trdAxe){case 0: v2 = w0; break; case 1: v2 = w1; break; case 2: v2 = w2; break;}
SetColumn(v0,0);
SetColumn(v1,1);
SetColumn(v2,2);
return *this;
}
inline Matrix3& SetCross(const Vector3& vector){
return SkewSymmetric(vector);
}
Matrix3& EulerRotation(int axis0, int axis1, int axis2, const Vector3& angles);
Vector3& GivensRotationPlane(REALTYPE a, REALTYPE b, Vector3 & result, int path=0);
Vector3& GetEulerAnglesGeneric(int i, int neg, int alt, int rev, Vector3& result, int path=0);
Vector3& GetEulerAnglesXZX(bool rev, Vector3& result, int path=0){return GetEulerAnglesGeneric(0,0,0,(rev?1:0),result,path);}
Vector3& GetEulerAnglesYXY(bool rev, Vector3& result, int path=0){return GetEulerAnglesGeneric(1,0,0,(rev?1:0),result,path);}
Vector3& GetEulerAnglesZYZ(bool rev, Vector3& result, int path=0){return GetEulerAnglesGeneric(2,0,0,(rev?1:0),result,path);}
Vector3& GetEulerAnglesXZY(bool rev, Vector3& result, int path=0){
if(rev){return GetEulerAnglesGeneric(1,1,1,(rev?1:0),result,path);}
else{ return GetEulerAnglesGeneric(0,0,1,(rev?1:0),result,path);}
}
Vector3& GetEulerAnglesYXZ(bool rev, Vector3& result, int path=0){
if(rev){return GetEulerAnglesGeneric(2,1,1,(rev?1:0),result,path);}
else{ return GetEulerAnglesGeneric(1,0,1,(rev?1:0),result,path);}
}
Vector3& GetEulerAnglesZYX(bool rev, Vector3& result, int path=0){
if(rev){return GetEulerAnglesGeneric(0,1,1,(rev?1:0),result,path);}
else{ return GetEulerAnglesGeneric(2,0,1,(rev?1:0),result,path);}
}
Vector3& GetEulerAnglesXYX(bool rev, Vector3& result, int path=0){return GetEulerAnglesGeneric(0,1,0,(rev?1:0),result,path);}
Vector3& GetEulerAnglesYZY(bool rev, Vector3& result, int path=0){return GetEulerAnglesGeneric(1,1,0,(rev?1:0),result,path);}
Vector3& GetEulerAnglesZXZ(bool rev, Vector3& result, int path=0){return GetEulerAnglesGeneric(2,1,0,(rev?1:0),result,path);}
Vector3& GetEulerAnglesXYZ(bool rev, Vector3& result, int path=0){
if(rev){return GetEulerAnglesGeneric(2,0,1,(rev?1:0),result,path);}
else{ return GetEulerAnglesGeneric(0,1,1,(rev?1:0),result,path);}
}
Vector3& GetEulerAnglesYZX(bool rev, Vector3& result, int path=0){
if(rev){return GetEulerAnglesGeneric(0,0,1,(rev?1:0),result,path);}
else{ return GetEulerAnglesGeneric(1,1,1,(rev?1:0),result,path);}
}
Vector3& GetEulerAnglesZXY(bool rev, Vector3& result, int path=0){
if(rev){return GetEulerAnglesGeneric(1,0,1,(rev?1:0),result,path);}
else{ return GetEulerAnglesGeneric(2,1,1,(rev?1:0),result,path);}
}
Vector3& GetEulerAngles(int r1, int r2, int r3, int rev, Vector3& result, int path=0){
switch(r1){
case 0:
switch(r2){
case 1:
switch(r3){
case 0: return GetEulerAnglesXYX(rev, result,path); break;
case 2: return GetEulerAnglesXYZ(rev, result,path); break;
}
break;
case 2:
switch(r3){
case 0: return GetEulerAnglesXZX(rev, result,path); break;
case 1: return GetEulerAnglesXZY(rev, result,path); break;
}
break;
}
break;
case 1:
switch(r2){
case 0:
switch(r3){
case 1: return GetEulerAnglesYXY(rev, result,path); break;
case 2: return GetEulerAnglesYXZ(rev, result,path); break;
}
break;
case 2:
switch(r3){
case 0: return GetEulerAnglesYZX(rev, result,path); break;
case 1: return GetEulerAnglesYZY(rev, result,path); break;
}
break;
}
break;
case 2:
switch(r2){
case 0:
switch(r3){
case 1: return GetEulerAnglesZXY(rev, result,path); break;
case 2: return GetEulerAnglesZXZ(rev, result,path); break;
}
break;
case 1:
switch(r3){
case 0: return GetEulerAnglesZYX(rev, result,path); break;
case 2: return GetEulerAnglesZYZ(rev, result,path); break;
}
break;
}
break;
}
cout << "GET EULER ANGLES ERROR (bad indices)"<<endl;
return result;
}
};
typedef Matrix3 Mat3;
#ifdef USE_MATHLIB_NAMESPACE
}
#endif
#endif
|