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
00037
00038
00039 #ifndef _OSGMATRIX_H_
00040 #define _OSGMATRIX_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044
00045 #include <OSGVector.h>
00046
00047 OSG_BEGIN_NAMESPACE
00048
00049 #if defined(OSG_MICROSOFT_COMPILER_HACKS) || defined(__sun)
00050 static const UInt32 JacobiRank = 3;
00051 #endif
00052
00053 template <class ValueTypeT> class QuaternionBase;
00054
00063 template<class ValueTypeT>
00064 class TransformationMatrix
00065 {
00066
00067
00068 public:
00069
00070
00074 typedef ValueTypeT ValueType;
00075 typedef VectorInterface<ValueTypeT,
00076 VecStorage4<ValueTypeT> > VectorType;
00077
00078 typedef QuaternionBase <ValueType> QuaternionType;
00079
00080 typedef VectorInterface<ValueTypeT,
00081 VecStorage3<ValueTypeT> > VectorType3f;
00082
00083 typedef PointInterface<ValueTypeT,
00084 VecStorage3<ValueTypeT> > PointType3f;
00085
00087
00091 static const TransformationMatrix &identity(void);
00092
00094
00098 TransformationMatrix(void);
00099 TransformationMatrix(const TransformationMatrix &source );
00100
00101 TransformationMatrix(const VectorType3f &vector1,
00102 const VectorType3f &vector2,
00103 const VectorType3f &vector3);
00104
00105 TransformationMatrix(const VectorType3f &vector1,
00106 const VectorType3f &vector2,
00107 const VectorType3f &vector3,
00108 const VectorType3f &vector4);
00109
00110 TransformationMatrix(const ValueTypeT rVal00,
00111 const ValueTypeT rVal10,
00112 const ValueTypeT rVal20,
00113 const ValueTypeT rVal30,
00114
00115 const ValueTypeT rVal01,
00116 const ValueTypeT rVal11,
00117 const ValueTypeT rVal21,
00118 const ValueTypeT rVal31,
00119
00120 const ValueTypeT rVal02,
00121 const ValueTypeT rVal12,
00122 const ValueTypeT rVal22,
00123 const ValueTypeT rVal32,
00124
00125 const ValueTypeT rVal03,
00126 const ValueTypeT rVal13,
00127 const ValueTypeT rVal23,
00128 const ValueTypeT rVal33);
00129
00131
00135 ~TransformationMatrix(void);
00136
00138
00142 void setIdentity (void );
00143
00144 void setValue (const TransformationMatrix &mat );
00145
00146 void setValue (const VectorType3f &vector1,
00147 const VectorType3f &vector2,
00148 const VectorType3f &vector3 );
00149
00150 void setValue (const VectorType3f &vector1,
00151 const VectorType3f &vector2,
00152 const VectorType3f &vector3,
00153 const VectorType3f &vector4 );
00154
00155 void setValue (const ValueTypeT rVal00,
00156 const ValueTypeT rVal10,
00157 const ValueTypeT rVal20,
00158 const ValueTypeT rVal30,
00159
00160 const ValueTypeT rVal01,
00161 const ValueTypeT rVal11,
00162 const ValueTypeT rVal21,
00163 const ValueTypeT rVal31,
00164
00165 const ValueTypeT rVal02,
00166 const ValueTypeT rVal12,
00167 const ValueTypeT rVal22,
00168 const ValueTypeT rVal32,
00169
00170 const ValueTypeT rVal03,
00171 const ValueTypeT rVal13,
00172 const ValueTypeT rVal23,
00173 const ValueTypeT rVal33 );
00174
00175 void setValueTransposed(const ValueTypeT rVal00,
00176 const ValueTypeT rVal01,
00177 const ValueTypeT rVal02,
00178 const ValueTypeT rVal03,
00179
00180 const ValueTypeT rVal10,
00181 const ValueTypeT rVal11,
00182 const ValueTypeT rVal12,
00183 const ValueTypeT rVal13,
00184
00185 const ValueTypeT rVal20,
00186 const ValueTypeT rVal21,
00187 const ValueTypeT rVal22,
00188 const ValueTypeT rVal23,
00189
00190 const ValueTypeT rVal30,
00191 const ValueTypeT rVal31,
00192 const ValueTypeT rVal32,
00193 const ValueTypeT rVal33 );
00194
00195 void setValue (const ValueTypeT *pMat,
00196 bool bTransposed = true );
00197
00198 void setValue (const VectorType *pMat );
00199
00200 #ifndef WIN32
00201 void setValue (const VectorType3f *pMat );
00202 #endif
00203
00204 void setValue (const Char8 *string,
00205 bool bTransposed = true );
00206
00208
00212 ValueTypeT *getValues(void);
00213 const ValueTypeT *getValues(void) const;
00214
00216
00220 void setScale (const ValueTypeT s );
00221
00222 void setScale (const ValueTypeT sx,
00223 const ValueTypeT sy,
00224 const ValueTypeT sz );
00225
00226 void setScale (const VectorType3f &s );
00227
00228
00229 void setTranslate(const ValueTypeT tx,
00230 const ValueTypeT ty,
00231 const ValueTypeT tz );
00232
00233 void setTranslate(const VectorType3f &t );
00234
00235 void setTranslate(const PointType3f &t );
00236
00237
00238 void setRotate (const QuaternionType &q );
00239
00240
00241 void setTransform(const VectorType3f &t );
00242
00243 void setTransform(const QuaternionType &r );
00244
00245 void setTransform(const VectorType3f &t,
00246 const QuaternionType &r );
00247
00248 void setTransform(const VectorType3f &t,
00249 const QuaternionType &r,
00250 const VectorType3f &s );
00251
00252 void setTransform(const VectorType3f &t,
00253 const QuaternionType &r,
00254 const VectorType3f &s,
00255 const QuaternionType &so );
00256
00257 void setTransform(const VectorType3f &translation,
00258 const QuaternionType &rotation,
00259 const VectorType3f &scaleFactor,
00260 const QuaternionType &scaleOrientation,
00261 const VectorType3f ¢er );
00262
00264
00268 void getTransform( VectorType3f &translation,
00269 QuaternionType &rotation,
00270 VectorType3f &scaleFactor,
00271 QuaternionType &scaleOrientation,
00272 const VectorType3f ¢er) const;
00273
00274 void getTransform( VectorType3f &translation,
00275 QuaternionType &rotation,
00276 VectorType3f &scaleFactor,
00277 QuaternionType &scaleOrientation) const;
00278
00279 bool factor ( TransformationMatrix &r,
00280 VectorType3f &s,
00281 TransformationMatrix &u,
00282 VectorType3f &t,
00283 TransformationMatrix &proj) const;
00284
00286
00290 void multMatrixPnt (const PointType3f &src,
00291 PointType3f &dst) const;
00292 void multMatrixPnt ( PointType3f &pnt) const;
00293 void multFullMatrixPnt(const PointType3f &src,
00294 PointType3f &dst) const;
00295 void multFullMatrixPnt( PointType3f &pnt) const;
00296
00297 void multMatrixVec (const VectorType3f &src,
00298 VectorType3f &dst) const;
00299 void multMatrixVec ( VectorType3f &vec) const;
00300
00301 void mult (const PointType3f &src,
00302 PointType3f &dst) const;
00303 void mult ( PointType3f &vec) const;
00304 void mult (const VectorType3f &src,
00305 VectorType3f &dst) const;
00306 void mult ( VectorType3f &vec) const;
00307
00308
00309 void multPntMatrix (const PointType3f &src,
00310 PointType3f &dst) const;
00311 void multPntMatrix ( PointType3f &pnt) const;
00312 void multPntFullMatrix(const PointType3f &src,
00313 PointType3f &dst) const;
00314 void multPntFullMatrix( PointType3f &pnt) const;
00315
00316 void multVecMatrix (const VectorType3f &src,
00317 VectorType3f &dst) const;
00318 void multVecMatrix ( VectorType3f &vec) const;
00319
00320
00321 void multMatrixVec (const VectorType &src,
00322 VectorType &dst) const;
00323 void multMatrixVec ( VectorType &vec) const;
00324
00326
00330 bool equals (const TransformationMatrix &matrix,
00331 const ValueType tol ) const;
00332
00333 ValueTypeT det3 ( void ) const;
00334 ValueTypeT det ( void ) const;
00335
00336 bool inverse ( TransformationMatrix &result) const;
00337 bool invert ( void );
00338 bool invertFrom (const TransformationMatrix &matrix);
00339
00340 bool inverse3 ( TransformationMatrix &result) const;
00341 bool invert3 ( void );
00342 bool invertFrom3 (const TransformationMatrix &matrix);
00343
00344 bool transposed ( TransformationMatrix &result) const;
00345 bool transpose ( void );
00346 bool transposeFrom(const TransformationMatrix &matrix);
00347
00348 void mult (const TransformationMatrix &matrix);
00349 void multLeft (const TransformationMatrix &matrix);
00350
00351 void add (const TransformationMatrix &matrix);
00352 void scale ( ValueTypeT s );
00353 void addScaled (const TransformationMatrix &matrix,
00354 ValueTypeT s );
00355 void negate ( void );
00356
00357 ValueTypeT norm1 ( void ) const;
00358 ValueTypeT norm2 ( void ) const;
00359 ValueTypeT normInfinity ( void ) const;
00360
00361 bool sqrt ( TransformationMatrix &result) const;
00362 bool sqrtOf (const TransformationMatrix &matrix);
00363 bool sqrt ( void );
00364
00365 bool log ( TransformationMatrix &result) const;
00366 bool logOf (const TransformationMatrix &matrix);
00367
00368 bool exp ( TransformationMatrix &result) const;
00369 bool expOf (const TransformationMatrix &matrix);
00370
00372
00376 VectorType &operator [](UInt32 uiIndex);
00377 const VectorType &operator [](UInt32 uiIndex) const;
00378
00380
00384 TransformationMatrix &operator =(const TransformationMatrix &source);
00385
00387
00391 bool operator == (const TransformationMatrix &other) const;
00392 bool operator != (const TransformationMatrix &other) const;
00393
00395
00396
00397 protected:
00398
00399
00403 VectorType _matrix[4];
00404
00406
00410 ValueTypeT rowMulCol4(const TransformationMatrix &gRowMat,
00411 UInt32 iRow,
00412 const TransformationMatrix &gColMat,
00413 UInt32 iColumn) const;
00414
00415 ValueTypeT det2 (const ValueTypeT a1,
00416 const ValueTypeT a2,
00417 const ValueTypeT b1,
00418 const ValueTypeT b2 ) const;
00419 ValueTypeT det3 (const ValueTypeT a1,
00420 const ValueTypeT a2,
00421 const ValueTypeT a3,
00422 const ValueTypeT b1,
00423 const ValueTypeT b2,
00424 const ValueTypeT b3,
00425 const ValueTypeT c1,
00426 const ValueTypeT c2,
00427 const ValueTypeT c3 ) const;
00428
00429 #ifdef __sgi
00430 #pragma set woff 1424
00431 #endif
00432
00433 #if !defined(OSG_MICROSOFT_COMPILER_HACKS) && !defined(__sun)
00434 static const UInt32 JacobiRank = 3;
00435 #endif
00436
00437 bool jacobi(ValueTypeT evalues [JacobiRank],
00438 VectorType3f evectors[JacobiRank],
00439 Int32 &rots);
00440
00441 #ifdef __sgi
00442 #pragma reset woff 1424
00443 #endif
00444
00446
00447
00448 private:
00449
00450 static TransformationMatrix _identityMatrix;
00451 };
00452
00458 typedef TransformationMatrix<Real32> Matrix;
00459
00465 typedef TransformationMatrix<Real32> Matrix4f;
00466
00472 typedef TransformationMatrix<Real64> Matrix4d;
00473
00474 template<class ValueTypeT>
00475 std::ostream &operator <<( std::ostream &os,
00476 const TransformationMatrix<ValueTypeT> &obj);
00477
00478 OSG_END_NAMESPACE
00479
00480 #include <OSGMatrix.inl>
00481
00482 #endif
00483
00484