Matrixd 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_MATRIXD
  14. #define OSG_MATRIXD 1
  15. #include <osg/Object>
  16. #include <osg/Vec3d>
  17. #include <osg/Vec4d>
  18. #include <osg/Quat>
  19. namespace osg {
  20. class Matrixf;
  21. class OSG_EXPORT Matrixd
  22. {
  23. public:
  24. typedef double value_type;
  25. typedef float other_value_type;
  26. inline Matrixd() { makeIdentity(); }
  27. inline Matrixd( const Matrixd& mat) { set(mat.ptr()); }
  28. Matrixd( const Matrixf& mat );
  29. inline explicit Matrixd( float const * const ptr ) { set(ptr); }
  30. inline explicit Matrixd( double const * const ptr ) { set(ptr); }
  31. inline explicit Matrixd( const Quat& quat ) { makeRotate(quat); }
  32. Matrixd(value_type a00, value_type a01, value_type a02, value_type a03,
  33. value_type a10, value_type a11, value_type a12, value_type a13,
  34. value_type a20, value_type a21, value_type a22, value_type a23,
  35. value_type a30, value_type a31, value_type a32, value_type a33);
  36. ~Matrixd() {}
  37. int compare(const Matrixd& m) const;
  38. bool operator < (const Matrixd& m) const { return compare(m)<0; }
  39. bool operator == (const Matrixd& m) const { return compare(m)==0; }
  40. bool operator != (const Matrixd& m) const { return compare(m)!=0; }
  41. inline value_type& operator()(int row, int col) { return _mat[row][col]; }
  42. inline value_type operator()(int row, int col) const { return _mat[row][col]; }
  43. inline bool valid() const { return !isNaN(); }
  44. inline bool isNaN() const { return osg::isNaN(_mat[0][0]) || osg::isNaN(_mat[0][1]) || osg::isNaN(_mat[0][2]) || osg::isNaN(_mat[0][3]) ||
  45. osg::isNaN(_mat[1][0]) || osg::isNaN(_mat[1][1]) || osg::isNaN(_mat[1][2]) || osg::isNaN(_mat[1][3]) ||
  46. osg::isNaN(_mat[2][0]) || osg::isNaN(_mat[2][1]) || osg::isNaN(_mat[2][2]) || osg::isNaN(_mat[2][3]) ||
  47. osg::isNaN(_mat[3][0]) || osg::isNaN(_mat[3][1]) || osg::isNaN(_mat[3][2]) || osg::isNaN(_mat[3][3]); }
  48. inline Matrixd& operator = (const Matrixd& rhs)
  49. {
  50. if( &rhs == this ) return *this;
  51. set(rhs.ptr());
  52. return *this;
  53. }
  54. Matrixd& operator = (const Matrixf& other);
  55. inline void set(const Matrixd& rhs) { set(rhs.ptr()); }
  56. void set(const Matrixf& rhs);
  57. inline void set(float const * const ptr)
  58. {
  59. value_type* local_ptr = (value_type*)_mat;
  60. for(int i=0;i<16;++i) local_ptr[i]=(value_type)ptr[i];
  61. }
  62. inline void set(double const * const ptr)
  63. {
  64. value_type* local_ptr = (value_type*)_mat;
  65. for(int i=0;i<16;++i) local_ptr[i]=(value_type)ptr[i];
  66. }
  67. void set(value_type a00, value_type a01, value_type a02,value_type a03,
  68. value_type a10, value_type a11, value_type a12,value_type a13,
  69. value_type a20, value_type a21, value_type a22,value_type a23,
  70. value_type a30, value_type a31, value_type a32,value_type a33);
  71. value_type * ptr() { return (value_type*)_mat; }
  72. const value_type * ptr() const { return (const value_type *)_mat; }
  73. bool isIdentity() const
  74. {
  75. return _mat[0][0]==1.0 && _mat[0][1]==0.0 && _mat[0][2]==0.0 && _mat[0][3]==0.0 &&
  76. _mat[1][0]==0.0 && _mat[1][1]==1.0 && _mat[1][2]==0.0 && _mat[1][3]==0.0 &&
  77. _mat[2][0]==0.0 && _mat[2][1]==0.0 && _mat[2][2]==1.0 && _mat[2][3]==0.0 &&
  78. _mat[3][0]==0.0 && _mat[3][1]==0.0 && _mat[3][2]==0.0 && _mat[3][3]==1.0;
  79. }
  80. void makeIdentity();
  81. void makeScale( const Vec3f& );
  82. void makeScale( const Vec3d& );
  83. void makeScale( value_type, value_type, value_type );
  84. void makeTranslate( const Vec3f& );
  85. void makeTranslate( const Vec3d& );
  86. void makeTranslate( value_type, value_type, value_type );
  87. void makeRotate( const Vec3f& from, const Vec3f& to );
  88. void makeRotate( const Vec3d& from, const Vec3d& to );
  89. void makeRotate( value_type angle, const Vec3f& axis );
  90. void makeRotate( value_type angle, const Vec3d& axis );
  91. void makeRotate( value_type angle, value_type x, value_type y, value_type z );
  92. void makeRotate( const Quat& );
  93. void makeRotate( value_type angle1, const Vec3f& axis1,
  94. value_type angle2, const Vec3f& axis2,
  95. value_type angle3, const Vec3f& axis3);
  96. void makeRotate( value_type angle1, const Vec3d& axis1,
  97. value_type angle2, const Vec3d& axis2,
  98. value_type angle3, const Vec3d& axis3);
  99. /** decompose the matrix into translation, rotation, scale and scale orientation.*/
  100. void decompose( osg::Vec3f& translation,
  101. osg::Quat& rotation,
  102. osg::Vec3f& scale,
  103. osg::Quat& so ) const;
  104. /** decompose the matrix into translation, rotation, scale and scale orientation.*/
  105. void decompose( osg::Vec3d& translation,
  106. osg::Quat& rotation,
  107. osg::Vec3d& scale,
  108. osg::Quat& so ) const;
  109. /** Set to an orthographic projection.
  110. * See glOrtho for further details.
  111. */
  112. void makeOrtho(double left, double right,
  113. double bottom, double top,
  114. double zNear, double zFar);
  115. /** Get the orthographic settings of the orthographic projection matrix.
  116. * Note, if matrix is not an orthographic matrix then invalid values
  117. * will be returned.
  118. */
  119. bool getOrtho(double& left, double& right,
  120. double& bottom, double& top,
  121. double& zNear, double& zFar) const;
  122. /** float version of getOrtho(..) */
  123. bool getOrtho(float& left, float& right,
  124. float& bottom, float& top,
  125. float& zNear, float& zFar) const;
  126. /** Set to a 2D orthographic projection.
  127. * See glOrtho2D for further details.
  128. */
  129. inline void makeOrtho2D(double left, double right,
  130. double bottom, double top)
  131. {
  132. makeOrtho(left,right,bottom,top,-1.0,1.0);
  133. }
  134. /** Set to a perspective projection.
  135. * See glFrustum for further details.
  136. */
  137. void makeFrustum(double left, double right,
  138. double bottom, double top,
  139. double zNear, double zFar);
  140. /** Get the frustum settings of a perspective projection matrix.
  141. * Note, if matrix is not a perspective matrix then invalid values
  142. * will be returned.
  143. */
  144. bool getFrustum(double& left, double& right,
  145. double& bottom, double& top,
  146. double& zNear, double& zFar) const;
  147. /** float version of getFrustum(..) */
  148. bool getFrustum(float& left, float& right,
  149. float& bottom, float& top,
  150. float& zNear, float& zFar) const;
  151. /** Set to a symmetrical perspective projection.
  152. * See gluPerspective for further details.
  153. * Aspect ratio is defined as width/height.
  154. */
  155. void makePerspective(double fovy, double aspectRatio,
  156. double zNear, double zFar);
  157. /** Get the frustum settings of a symmetric perspective projection
  158. * matrix.
  159. * Return false if matrix is not a perspective matrix,
  160. * where parameter values are undefined.
  161. * Note, if matrix is not a symmetric perspective matrix then the
  162. * shear will be lost.
  163. * Asymmetric matrices occur when stereo, power walls, caves and
  164. * reality center display are used.
  165. * In these configuration one should use the AsFrustum method instead.
  166. */
  167. bool getPerspective(double& fovy, double& aspectRatio,
  168. double& zNear, double& zFar) const;
  169. /** float version of getPerspective(..) */
  170. bool getPerspective(float& fovy, float& aspectRatio,
  171. float& zNear, float& zFar) const;
  172. /** Set the position and orientation to be a view matrix,
  173. * using the same convention as gluLookAt.
  174. */
  175. void makeLookAt(const Vec3d& eye,const Vec3d& center,const Vec3d& up);
  176. /** Get to the position and orientation of a modelview matrix,
  177. * using the same convention as gluLookAt.
  178. */
  179. void getLookAt(Vec3f& eye,Vec3f& center,Vec3f& up,
  180. value_type lookDistance=1.0f) const;
  181. /** Get to the position and orientation of a modelview matrix,
  182. * using the same convention as gluLookAt.
  183. */
  184. void getLookAt(Vec3d& eye,Vec3d& center,Vec3d& up,
  185. value_type lookDistance=1.0f) const;
  186. /** invert the matrix rhs, automatically select invert_4x3 or invert_4x4. */
  187. inline bool invert( const Matrixd& rhs)
  188. {
  189. bool is_4x3 = (rhs._mat[0][3]==0.0 && rhs._mat[1][3]==0.0 && rhs._mat[2][3]==0.0 && rhs._mat[3][3]==1.0);
  190. return is_4x3 ? invert_4x3(rhs) : invert_4x4(rhs);
  191. }
  192. /** 4x3 matrix invert, not right hand column is assumed to be 0,0,0,1. */
  193. bool invert_4x3( const Matrixd& rhs);
  194. /** full 4x4 matrix invert. */
  195. bool invert_4x4( const Matrixd& rhs);
  196. /** transpose a matrix */
  197. bool transpose(const Matrixd&rhs);
  198. /** transpose orthogonal part of the matrix **/
  199. bool transpose3x3(const Matrixd&rhs);
  200. /** ortho-normalize the 3x3 rotation & scale matrix */
  201. void orthoNormalize(const Matrixd& rhs);
  202. // basic utility functions to create new matrices
  203. inline static Matrixd identity( void );
  204. inline static Matrixd scale( const Vec3f& sv);
  205. inline static Matrixd scale( const Vec3d& sv);
  206. inline static Matrixd scale( value_type sx, value_type sy, value_type sz);
  207. inline static Matrixd translate( const Vec3f& dv);
  208. inline static Matrixd translate( const Vec3d& dv);
  209. inline static Matrixd translate( value_type x, value_type y, value_type z);
  210. inline static Matrixd rotate( const Vec3f& from, const Vec3f& to);
  211. inline static Matrixd rotate( const Vec3d& from, const Vec3d& to);
  212. inline static Matrixd rotate( value_type angle, value_type x, value_type y, value_type z);
  213. inline static Matrixd rotate( value_type angle, const Vec3f& axis);
  214. inline static Matrixd rotate( value_type angle, const Vec3d& axis);
  215. inline static Matrixd rotate( value_type angle1, const Vec3f& axis1,
  216. value_type angle2, const Vec3f& axis2,
  217. value_type angle3, const Vec3f& axis3);
  218. inline static Matrixd rotate( value_type angle1, const Vec3d& axis1,
  219. value_type angle2, const Vec3d& axis2,
  220. value_type angle3, const Vec3d& axis3);
  221. inline static Matrixd rotate( const Quat& quat);
  222. inline static Matrixd inverse( const Matrixd& matrix);
  223. inline static Matrixd orthoNormal(const Matrixd& matrix);
  224. /** Create an orthographic projection matrix.
  225. * See glOrtho for further details.
  226. */
  227. inline static Matrixd ortho(double left, double right,
  228. double bottom, double top,
  229. double zNear, double zFar);
  230. /** Create a 2D orthographic projection.
  231. * See glOrtho for further details.
  232. */
  233. inline static Matrixd ortho2D(double left, double right,
  234. double bottom, double top);
  235. /** Create a perspective projection.
  236. * See glFrustum for further details.
  237. */
  238. inline static Matrixd frustum(double left, double right,
  239. double bottom, double top,
  240. double zNear, double zFar);
  241. /** Create a symmetrical perspective projection.
  242. * See gluPerspective for further details.
  243. * Aspect ratio is defined as width/height.
  244. */
  245. inline static Matrixd perspective(double fovy, double aspectRatio,
  246. double zNear, double zFar);
  247. /** Create the position and orientation as per a camera,
  248. * using the same convention as gluLookAt.
  249. */
  250. inline static Matrixd lookAt(const Vec3f& eye,
  251. const Vec3f& center,
  252. const Vec3f& up);
  253. /** Create the position and orientation as per a camera,
  254. * using the same convention as gluLookAt.
  255. */
  256. inline static Matrixd lookAt(const Vec3d& eye,
  257. const Vec3d& center,
  258. const Vec3d& up);
  259. inline Vec3f preMult( const Vec3f& v ) const;
  260. inline Vec3d preMult( const Vec3d& v ) const;
  261. inline Vec3f postMult( const Vec3f& v ) const;
  262. inline Vec3d postMult( const Vec3d& v ) const;
  263. inline Vec3f operator* ( const Vec3f& v ) const;
  264. inline Vec3d operator* ( const Vec3d& v ) const;
  265. inline Vec4f preMult( const Vec4f& v ) const;
  266. inline Vec4d preMult( const Vec4d& v ) const;
  267. inline Vec4f postMult( const Vec4f& v ) const;
  268. inline Vec4d postMult( const Vec4d& v ) const;
  269. inline Vec4f operator* ( const Vec4f& v ) const;
  270. inline Vec4d operator* ( const Vec4d& v ) const;
  271. #ifdef OSG_USE_DEPRECATED_API
  272. inline void set(const Quat& q) { makeRotate(q); } /// deprecated, replace with makeRotate(q)
  273. inline void get(Quat& q) const { q = getRotate(); } /// deprecated, replace with getRotate()
  274. #endif
  275. void setRotate(const Quat& q);
  276. /** Get the matrix rotation as a Quat. Note that this function
  277. * assumes a non-scaled matrix and will return incorrect results
  278. * for scaled matrixces. Consider decompose() instead.
  279. */
  280. Quat getRotate() const;
  281. void setTrans( value_type tx, value_type ty, value_type tz );
  282. void setTrans( const Vec3f& v );
  283. void setTrans( const Vec3d& v );
  284. inline Vec3d getTrans() const { return Vec3d(_mat[3][0],_mat[3][1],_mat[3][2]); }
  285. inline Vec3d getScale() const {
  286. Vec3d x_vec(_mat[0][0],_mat[1][0],_mat[2][0]);
  287. Vec3d y_vec(_mat[0][1],_mat[1][1],_mat[2][1]);
  288. Vec3d z_vec(_mat[0][2],_mat[1][2],_mat[2][2]);
  289. return Vec3d(x_vec.length(), y_vec.length(), z_vec.length());
  290. }
  291. /** apply a 3x3 transform of v*M[0..2,0..2]. */
  292. inline static Vec3f transform3x3(const Vec3f& v,const Matrixd& m);
  293. /** apply a 3x3 transform of v*M[0..2,0..2]. */
  294. inline static Vec3d transform3x3(const Vec3d& v,const Matrixd& m);
  295. /** apply a 3x3 transform of M[0..2,0..2]*v. */
  296. inline static Vec3f transform3x3(const Matrixd& m,const Vec3f& v);
  297. /** apply a 3x3 transform of M[0..2,0..2]*v. */
  298. inline static Vec3d transform3x3(const Matrixd& m,const Vec3d& v);
  299. // basic Matrixd multiplication, our workhorse methods.
  300. void mult( const Matrixd&, const Matrixd& );
  301. void preMult( const Matrixd& );
  302. void postMult( const Matrixd& );
  303. /** Optimized version of preMult(translate(v)); */
  304. inline void preMultTranslate( const Vec3d& v );
  305. inline void preMultTranslate( const Vec3f& v );
  306. /** Optimized version of postMult(translate(v)); */
  307. inline void postMultTranslate( const Vec3d& v );
  308. inline void postMultTranslate( const Vec3f& v );
  309. /** Optimized version of preMult(scale(v)); */
  310. inline void preMultScale( const Vec3d& v );
  311. inline void preMultScale( const Vec3f& v );
  312. /** Optimized version of postMult(scale(v)); */
  313. inline void postMultScale( const Vec3d& v );
  314. inline void postMultScale( const Vec3f& v );
  315. /** Optimized version of preMult(rotate(q)); */
  316. inline void preMultRotate( const Quat& q );
  317. /** Optimized version of postMult(rotate(q)); */
  318. inline void postMultRotate( const Quat& q );
  319. inline void operator *= ( const Matrixd& other )
  320. { if( this == &other ) {
  321. Matrixd temp(other);
  322. postMult( temp );
  323. }
  324. else postMult( other );
  325. }
  326. inline Matrixd operator * ( const Matrixd &m ) const
  327. {
  328. osg::Matrixd r;
  329. r.mult(*this,m);
  330. return r;
  331. }
  332. protected:
  333. value_type _mat[4][4];
  334. };
  335. class RefMatrixd : public Object, public Matrixd
  336. {
  337. public:
  338. RefMatrixd():Object(false), Matrixd() {}
  339. RefMatrixd( const Matrixd& other) : Object(false), Matrixd(other) {}
  340. RefMatrixd( const Matrixf& other) : Object(false), Matrixd(other) {}
  341. RefMatrixd( const RefMatrixd& other) : Object(other), Matrixd(other) {}
  342. explicit RefMatrixd( Matrixd::value_type const * const def ):Object(false), Matrixd(def) {}
  343. RefMatrixd( Matrixd::value_type a00, Matrixd::value_type a01, Matrixd::value_type a02, Matrixd::value_type a03,
  344. Matrixd::value_type a10, Matrixd::value_type a11, Matrixd::value_type a12, Matrixd::value_type a13,
  345. Matrixd::value_type a20, Matrixd::value_type a21, Matrixd::value_type a22, Matrixd::value_type a23,
  346. Matrixd::value_type a30, Matrixd::value_type a31, Matrixd::value_type a32, Matrixd::value_type a33):
  347. Object(false),
  348. Matrixd(a00, a01, a02, a03,
  349. a10, a11, a12, a13,
  350. a20, a21, a22, a23,
  351. a30, a31, a32, a33) {}
  352. virtual Object* cloneType() const { return new RefMatrixd(); }
  353. virtual Object* clone(const CopyOp&) const { return new RefMatrixd(*this); }
  354. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const RefMatrixd*>(obj)!=NULL; }
  355. virtual const char* libraryName() const { return "osg"; }
  356. virtual const char* className() const { return "Matrix"; }
  357. protected:
  358. virtual ~RefMatrixd() {}
  359. };
  360. // static utility methods
  361. inline Matrixd Matrixd::identity(void)
  362. {
  363. Matrixd m;
  364. m.makeIdentity();
  365. return m;
  366. }
  367. inline Matrixd Matrixd::scale(value_type sx, value_type sy, value_type sz)
  368. {
  369. Matrixd m;
  370. m.makeScale(sx,sy,sz);
  371. return m;
  372. }
  373. inline Matrixd Matrixd::scale(const Vec3f& v )
  374. {
  375. return scale(v.x(), v.y(), v.z() );
  376. }
  377. inline Matrixd Matrixd::scale(const Vec3d& v )
  378. {
  379. return scale(v.x(), v.y(), v.z() );
  380. }
  381. inline Matrixd Matrixd::translate(value_type tx, value_type ty, value_type tz)
  382. {
  383. Matrixd m;
  384. m.makeTranslate(tx,ty,tz);
  385. return m;
  386. }
  387. inline Matrixd Matrixd::translate(const Vec3f& v )
  388. {
  389. return translate(v.x(), v.y(), v.z() );
  390. }
  391. inline Matrixd Matrixd::translate(const Vec3d& v )
  392. {
  393. return translate(v.x(), v.y(), v.z() );
  394. }
  395. inline Matrixd Matrixd::rotate( const Quat& q )
  396. {
  397. return Matrixd(q);
  398. }
  399. inline Matrixd Matrixd::rotate(value_type angle, value_type x, value_type y, value_type z )
  400. {
  401. Matrixd m;
  402. m.makeRotate(angle,x,y,z);
  403. return m;
  404. }
  405. inline Matrixd Matrixd::rotate(value_type angle, const Vec3f& axis )
  406. {
  407. Matrixd m;
  408. m.makeRotate(angle,axis);
  409. return m;
  410. }
  411. inline Matrixd Matrixd::rotate(value_type angle, const Vec3d& axis )
  412. {
  413. Matrixd m;
  414. m.makeRotate(angle,axis);
  415. return m;
  416. }
  417. inline Matrixd Matrixd::rotate( value_type angle1, const Vec3f& axis1,
  418. value_type angle2, const Vec3f& axis2,
  419. value_type angle3, const Vec3f& axis3)
  420. {
  421. Matrixd m;
  422. m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
  423. return m;
  424. }
  425. inline Matrixd Matrixd::rotate( value_type angle1, const Vec3d& axis1,
  426. value_type angle2, const Vec3d& axis2,
  427. value_type angle3, const Vec3d& axis3)
  428. {
  429. Matrixd m;
  430. m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
  431. return m;
  432. }
  433. inline Matrixd Matrixd::rotate(const Vec3f& from, const Vec3f& to )
  434. {
  435. Matrixd m;
  436. m.makeRotate(from,to);
  437. return m;
  438. }
  439. inline Matrixd Matrixd::rotate(const Vec3d& from, const Vec3d& to )
  440. {
  441. Matrixd m;
  442. m.makeRotate(from,to);
  443. return m;
  444. }
  445. inline Matrixd Matrixd::inverse( const Matrixd& matrix)
  446. {
  447. Matrixd m;
  448. m.invert(matrix);
  449. return m;
  450. }
  451. inline Matrixd Matrixd::orthoNormal(const Matrixd& matrix)
  452. {
  453. Matrixd m;
  454. m.orthoNormalize(matrix);
  455. return m;
  456. }
  457. inline Matrixd Matrixd::ortho(double left, double right,
  458. double bottom, double top,
  459. double zNear, double zFar)
  460. {
  461. Matrixd m;
  462. m.makeOrtho(left,right,bottom,top,zNear,zFar);
  463. return m;
  464. }
  465. inline Matrixd Matrixd::ortho2D(double left, double right,
  466. double bottom, double top)
  467. {
  468. Matrixd m;
  469. m.makeOrtho2D(left,right,bottom,top);
  470. return m;
  471. }
  472. inline Matrixd Matrixd::frustum(double left, double right,
  473. double bottom, double top,
  474. double zNear, double zFar)
  475. {
  476. Matrixd m;
  477. m.makeFrustum(left,right,bottom,top,zNear,zFar);
  478. return m;
  479. }
  480. inline Matrixd Matrixd::perspective(double fovy, double aspectRatio,
  481. double zNear, double zFar)
  482. {
  483. Matrixd m;
  484. m.makePerspective(fovy,aspectRatio,zNear,zFar);
  485. return m;
  486. }
  487. inline Matrixd Matrixd::lookAt(const Vec3f& eye,
  488. const Vec3f& center,
  489. const Vec3f& up)
  490. {
  491. Matrixd m;
  492. m.makeLookAt(eye,center,up);
  493. return m;
  494. }
  495. inline Matrixd Matrixd::lookAt(const Vec3d& eye,
  496. const Vec3d& center,
  497. const Vec3d& up)
  498. {
  499. Matrixd m;
  500. m.makeLookAt(eye,center,up);
  501. return m;
  502. }
  503. inline Vec3f Matrixd::postMult( const Vec3f& v ) const
  504. {
  505. value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
  506. return Vec3f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
  507. (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
  508. (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
  509. }
  510. inline Vec3d Matrixd::postMult( const Vec3d& v ) const
  511. {
  512. value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
  513. return Vec3d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
  514. (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
  515. (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
  516. }
  517. inline Vec3f Matrixd::preMult( const Vec3f& v ) const
  518. {
  519. value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
  520. return Vec3f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
  521. (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
  522. (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
  523. }
  524. inline Vec3d Matrixd::preMult( const Vec3d& v ) const
  525. {
  526. value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
  527. return Vec3d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
  528. (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
  529. (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
  530. }
  531. inline Vec4f Matrixd::postMult( const Vec4f& v ) const
  532. {
  533. return Vec4f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
  534. (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
  535. (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
  536. (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
  537. }
  538. inline Vec4d Matrixd::postMult( const Vec4d& v ) const
  539. {
  540. return Vec4d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
  541. (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
  542. (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
  543. (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
  544. }
  545. inline Vec4f Matrixd::preMult( const Vec4f& v ) const
  546. {
  547. return Vec4f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
  548. (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
  549. (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
  550. (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
  551. }
  552. inline Vec4d Matrixd::preMult( const Vec4d& v ) const
  553. {
  554. return Vec4d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
  555. (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
  556. (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
  557. (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
  558. }
  559. inline Vec3f Matrixd::transform3x3(const Vec3f& v,const Matrixd& m)
  560. {
  561. return Vec3f( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
  562. (m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
  563. (m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
  564. }
  565. inline Vec3d Matrixd::transform3x3(const Vec3d& v,const Matrixd& m)
  566. {
  567. return Vec3d( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
  568. (m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
  569. (m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
  570. }
  571. inline Vec3f Matrixd::transform3x3(const Matrixd& m,const Vec3f& v)
  572. {
  573. return Vec3f( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
  574. (m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
  575. (m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
  576. }
  577. inline Vec3d Matrixd::transform3x3(const Matrixd& m,const Vec3d& v)
  578. {
  579. return Vec3d( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
  580. (m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
  581. (m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
  582. }
  583. inline void Matrixd::preMultTranslate( const Vec3d& v )
  584. {
  585. for (unsigned i = 0; i < 3; ++i)
  586. {
  587. double tmp = v[i];
  588. if (tmp == 0)
  589. continue;
  590. _mat[3][0] += tmp*_mat[i][0];
  591. _mat[3][1] += tmp*_mat[i][1];
  592. _mat[3][2] += tmp*_mat[i][2];
  593. _mat[3][3] += tmp*_mat[i][3];
  594. }
  595. }
  596. inline void Matrixd::preMultTranslate( const Vec3f& v )
  597. {
  598. for (unsigned i = 0; i < 3; ++i)
  599. {
  600. float tmp = v[i];
  601. if (tmp == 0)
  602. continue;
  603. _mat[3][0] += tmp*_mat[i][0];
  604. _mat[3][1] += tmp*_mat[i][1];
  605. _mat[3][2] += tmp*_mat[i][2];
  606. _mat[3][3] += tmp*_mat[i][3];
  607. }
  608. }
  609. inline void Matrixd::postMultTranslate( const Vec3d& v )
  610. {
  611. for (unsigned i = 0; i < 3; ++i)
  612. {
  613. double tmp = v[i];
  614. if (tmp == 0)
  615. continue;
  616. _mat[0][i] += tmp*_mat[0][3];
  617. _mat[1][i] += tmp*_mat[1][3];
  618. _mat[2][i] += tmp*_mat[2][3];
  619. _mat[3][i] += tmp*_mat[3][3];
  620. }
  621. }
  622. inline void Matrixd::postMultTranslate( const Vec3f& v )
  623. {
  624. for (unsigned i = 0; i < 3; ++i)
  625. {
  626. float tmp = v[i];
  627. if (tmp == 0)
  628. continue;
  629. _mat[0][i] += tmp*_mat[0][3];
  630. _mat[1][i] += tmp*_mat[1][3];
  631. _mat[2][i] += tmp*_mat[2][3];
  632. _mat[3][i] += tmp*_mat[3][3];
  633. }
  634. }
  635. inline void Matrixd::preMultScale( const Vec3d& v )
  636. {
  637. _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
  638. _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
  639. _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
  640. }
  641. inline void Matrixd::preMultScale( const Vec3f& v )
  642. {
  643. _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
  644. _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
  645. _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
  646. }
  647. inline void Matrixd::postMultScale( const Vec3d& v )
  648. {
  649. _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
  650. _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
  651. _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
  652. }
  653. inline void Matrixd::postMultScale( const Vec3f& v )
  654. {
  655. _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
  656. _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
  657. _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
  658. }
  659. inline void Matrixd::preMultRotate( const Quat& q )
  660. {
  661. if (q.zeroRotation())
  662. return;
  663. Matrixd r;
  664. r.setRotate(q);
  665. preMult(r);
  666. }
  667. inline void Matrixd::postMultRotate( const Quat& q )
  668. {
  669. if (q.zeroRotation())
  670. return;
  671. Matrixd r;
  672. r.setRotate(q);
  673. postMult(r);
  674. }
  675. inline Vec3f operator* (const Vec3f& v, const Matrixd& m )
  676. {
  677. return m.preMult(v);
  678. }
  679. inline Vec3d operator* (const Vec3d& v, const Matrixd& m )
  680. {
  681. return m.preMult(v);
  682. }
  683. inline Vec4f operator* (const Vec4f& v, const Matrixd& m )
  684. {
  685. return m.preMult(v);
  686. }
  687. inline Vec4d operator* (const Vec4d& v, const Matrixd& m )
  688. {
  689. return m.preMult(v);
  690. }
  691. inline Vec3f Matrixd::operator* (const Vec3f& v) const
  692. {
  693. return postMult(v);
  694. }
  695. inline Vec3d Matrixd::operator* (const Vec3d& v) const
  696. {
  697. return postMult(v);
  698. }
  699. inline Vec4f Matrixd::operator* (const Vec4f& v) const
  700. {
  701. return postMult(v);
  702. }
  703. inline Vec4d Matrixd::operator* (const Vec4d& v) const
  704. {
  705. return postMult(v);
  706. }
  707. } //namespace osg
  708. #endif