1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
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.
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.
26class OSG_EXPORT Matrixf
30 typedef float value_type;
31 typedef double other_value_type;
33 inline Matrixf() { makeIdentity(); }
34 inline Matrixf( const Matrixf& mat) { set(mat.ptr()); }
35 Matrixf( const Matrixd& mat );
36 inline explicit Matrixf( float const * const ptr ) { set(ptr); }
37 inline explicit Matrixf( double const * const ptr ) { set(ptr); }
38 inline explicit Matrixf( const Quat& quat ) { makeRotate(quat); }
40 Matrixf( value_type a00, value_type a01, value_type a02, value_type a03,
41 value_type a10, value_type a11, value_type a12, value_type a13,
42 value_type a20, value_type a21, value_type a22, value_type a23,
43 value_type a30, value_type a31, value_type a32, value_type a33);
47 int compare(const Matrixf& m) const;
49 bool operator < (const Matrixf& m) const { return compare(m)<0; }
50 bool operator == (const Matrixf& m) const { return compare(m)==0; }
51 bool operator != (const Matrixf& m) const { return compare(m)!=0; }
53 inline value_type& operator()(int row, int col) { return _mat[row][col]; }
54 inline value_type operator()(int row, int col) const { return _mat[row][col]; }
56 inline bool valid() const { return !isNaN(); }
57 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]) ||
58 osg::isNaN(_mat[1][0]) || osg::isNaN(_mat[1][1]) || osg::isNaN(_mat[1][2]) || osg::isNaN(_mat[1][3]) ||
59 osg::isNaN(_mat[2][0]) || osg::isNaN(_mat[2][1]) || osg::isNaN(_mat[2][2]) || osg::isNaN(_mat[2][3]) ||
60 osg::isNaN(_mat[3][0]) || osg::isNaN(_mat[3][1]) || osg::isNaN(_mat[3][2]) || osg::isNaN(_mat[3][3]); }
62 inline Matrixf& operator = (const Matrixf& rhs)
64 if( &rhs == this ) return *this;
69 Matrixf& operator = (const Matrixd& other);
71 inline void set(const Matrixf& rhs) { set(rhs.ptr()); }
73 void set(const Matrixd& rhs);
75 inline void set(float const * const ptr)
77 value_type* local_ptr = (value_type*)_mat;
78 for(int i=0;i<16;++i) local_ptr[i]=(value_type)ptr[i];
81 inline void set(double const * const ptr)
83 value_type* local_ptr = (value_type*)_mat;
84 for(int i=0;i<16;++i) local_ptr[i]=(value_type)ptr[i];
87 void set(value_type a00, value_type a01, value_type a02,value_type a03,
88 value_type a10, value_type a11, value_type a12,value_type a13,
89 value_type a20, value_type a21, value_type a22,value_type a23,
90 value_type a30, value_type a31, value_type a32,value_type a33);
92 value_type * ptr() { return (value_type*)_mat; }
93 const value_type * ptr() const { return (const value_type *)_mat; }
95 bool isIdentity() const
97 return _mat[0][0]==1.0f && _mat[0][1]==0.0f && _mat[0][2]==0.0f && _mat[0][3]==0.0f &&
98 _mat[1][0]==0.0f && _mat[1][1]==1.0f && _mat[1][2]==0.0f && _mat[1][3]==0.0f &&
99 _mat[2][0]==0.0f && _mat[2][1]==0.0f && _mat[2][2]==1.0f && _mat[2][3]==0.0f &&
100 _mat[3][0]==0.0f && _mat[3][1]==0.0f && _mat[3][2]==0.0f && _mat[3][3]==1.0f;
105 void makeScale( const Vec3f& );
106 void makeScale( const Vec3d& );
107 void makeScale( value_type, value_type, value_type );
109 void makeTranslate( const Vec3f& );
110 void makeTranslate( const Vec3d& );
111 void makeTranslate( value_type, value_type, value_type );
113 void makeRotate( const Vec3f& from, const Vec3f& to );
114 void makeRotate( const Vec3d& from, const Vec3d& to );
115 void makeRotate( value_type angle, const Vec3f& axis );
116 void makeRotate( value_type angle, const Vec3d& axis );
117 void makeRotate( value_type angle, value_type x, value_type y, value_type z );
118 void makeRotate( const Quat& );
119 void makeRotate( value_type angle1, const Vec3f& axis1,
120 value_type angle2, const Vec3f& axis2,
121 value_type angle3, const Vec3f& axis3);
122 void makeRotate( value_type angle1, const Vec3d& axis1,
123 value_type angle2, const Vec3d& axis2,
124 value_type angle3, const Vec3d& axis3);
127 /** decompose the matrix into translation, rotation, scale and scale orientation.*/
128 void decompose( osg::Vec3f& translation,
131 osg::Quat& so ) const;
133 /** decompose the matrix into translation, rotation, scale and scale orientation.*/
134 void decompose( osg::Vec3d& translation,
137 osg::Quat& so ) const;
140 /** Set to an orthographic projection.
141 * See glOrtho for further details.
143 void makeOrtho(double left, double right,
144 double bottom, double top,
145 double zNear, double zFar);
147 /** Get the orthographic settings of the orthographic projection matrix.
148 * Note, if matrix is not an orthographic matrix then invalid values
151 bool getOrtho(double& left, double& right,
152 double& bottom, double& top,
153 double& zNear, double& zFar) const;
155 /** float version of getOrtho(..) */
156 bool getOrtho(float& left, float& right,
157 float& bottom, float& top,
158 float& zNear, float& zFar) const;
161 /** Set to a 2D orthographic projection.
162 * See glOrtho2D for further details.
164 inline void makeOrtho2D(double left, double right,
165 double bottom, double top)
167 makeOrtho(left,right,bottom,top,-1.0,1.0);
171 /** Set to a perspective projection.
172 * See glFrustum for further details.
174 void makeFrustum(double left, double right,
175 double bottom, double top,
176 double zNear, double zFar);
178 /** Get the frustum settings of a perspective projection matrix.
179 * Note, if matrix is not a perspective matrix then invalid values
182 bool getFrustum(double& left, double& right,
183 double& bottom, double& top,
184 double& zNear, double& zFar) const;
186 /** float version of getFrustum(..) */
187 bool getFrustum(float& left, float& right,
188 float& bottom, float& top,
189 float& zNear, float& zFar) const;
191 /** Set to a symmetrical perspective projection.
192 * See gluPerspective for further details.
193 * Aspect ratio is defined as width/height.
195 void makePerspective(double fovy, double aspectRatio,
196 double zNear, double zFar);
198 /** Get the frustum settings of a symmetric perspective projection
200 * Return false if matrix is not a perspective matrix,
201 * where parameter values are undefined.
202 * Note, if matrix is not a symmetric perspective matrix then the
203 * shear will be lost.
204 * Asymmetric matrices occur when stereo, power walls, caves and
205 * reality center display are used.
206 * In these configuration one should use the AsFrustum method instead.
208 bool getPerspective(double& fovy, double& aspectRatio,
209 double& zNear, double& zFar) const;
211 /** float version of getPerspective(..) */
212 bool getPerspective(float& fovy, float& aspectRatio,
213 float& zNear, float& zFar) const;
215 /** Set the position and orientation to be a view matrix,
216 * using the same convention as gluLookAt.
218 void makeLookAt(const Vec3d& eye,const Vec3d& center,const Vec3d& up);
220 /** Get to the position and orientation of a modelview matrix,
221 * using the same convention as gluLookAt.
223 void getLookAt(Vec3f& eye,Vec3f& center,Vec3f& up,
224 value_type lookDistance=1.0f) const;
226 /** Get to the position and orientation of a modelview matrix,
227 * using the same convention as gluLookAt.
229 void getLookAt(Vec3d& eye,Vec3d& center,Vec3d& up,
230 value_type lookDistance=1.0f) const;
232 /** invert the matrix rhs, automatically select invert_4x3 or invert_4x4. */
233 inline bool invert( const Matrixf& rhs)
235 bool is_4x3 = (rhs._mat[0][3]==0.0f && rhs._mat[1][3]==0.0f && rhs._mat[2][3]==0.0f && rhs._mat[3][3]==1.0f);
236 return is_4x3 ? invert_4x3(rhs) : invert_4x4(rhs);
239 /** 4x3 matrix invert, not right hand column is assumed to be 0,0,0,1. */
240 bool invert_4x3( const Matrixf& rhs);
242 /** full 4x4 matrix invert. */
243 bool invert_4x4( const Matrixf& rhs);
245 /** transpose matrix **/
246 bool transpose(const Matrixf&rhs);
248 /** transpose orthogonal part of the matrix **/
249 bool transpose3x3(const Matrixf&rhs);
251 /** ortho-normalize the 3x3 rotation & scale matrix */
252 void orthoNormalize(const Matrixf& rhs);
254 //basic utility functions to create new matrices
255 inline static Matrixf identity( void );
256 inline static Matrixf scale( const Vec3f& sv);
257 inline static Matrixf scale( const Vec3d& sv);
258 inline static Matrixf scale( value_type sx, value_type sy, value_type sz);
259 inline static Matrixf translate( const Vec3f& dv);
260 inline static Matrixf translate( const Vec3d& dv);
261 inline static Matrixf translate( value_type x, value_type y, value_type z);
262 inline static Matrixf rotate( const Vec3f& from, const Vec3f& to);
263 inline static Matrixf rotate( const Vec3d& from, const Vec3d& to);
264 inline static Matrixf rotate( value_type angle, value_type x, value_type y, value_type z);
265 inline static Matrixf rotate( value_type angle, const Vec3f& axis);
266 inline static Matrixf rotate( value_type angle, const Vec3d& axis);
267 inline static Matrixf rotate( value_type angle1, const Vec3f& axis1,
268 value_type angle2, const Vec3f& axis2,
269 value_type angle3, const Vec3f& axis3);
270 inline static Matrixf rotate( value_type angle1, const Vec3d& axis1,
271 value_type angle2, const Vec3d& axis2,
272 value_type angle3, const Vec3d& axis3);
273 inline static Matrixf rotate( const Quat& quat);
274 inline static Matrixf inverse( const Matrixf& matrix);
275 inline static Matrixf orthoNormal(const Matrixf& matrix);
277 /** Create an orthographic projection matrix.
278 * See glOrtho for further details.
280 inline static Matrixf ortho(double left, double right,
281 double bottom, double top,
282 double zNear, double zFar);
284 /** Create a 2D orthographic projection.
285 * See glOrtho for further details.
287 inline static Matrixf ortho2D(double left, double right,
288 double bottom, double top);
290 /** Create a perspective projection.
291 * See glFrustum for further details.
293 inline static Matrixf frustum(double left, double right,
294 double bottom, double top,
295 double zNear, double zFar);
297 /** Create a symmetrical perspective projection.
298 * See gluPerspective for further details.
299 * Aspect ratio is defined as width/height.
301 inline static Matrixf perspective(double fovy, double aspectRatio,
302 double zNear, double zFar);
304 /** Create the position and orientation as per a camera,
305 * using the same convention as gluLookAt.
307 inline static Matrixf lookAt(const Vec3f& eye,
311 /** Create the position and orientation as per a camera,
312 * using the same convention as gluLookAt.
314 inline static Matrixf lookAt(const Vec3d& eye,
318 inline Vec3f preMult( const Vec3f& v ) const;
319 inline Vec3d preMult( const Vec3d& v ) const;
320 inline Vec3f postMult( const Vec3f& v ) const;
321 inline Vec3d postMult( const Vec3d& v ) const;
322 inline Vec3f operator* ( const Vec3f& v ) const;
323 inline Vec3d operator* ( const Vec3d& v ) const;
324 inline Vec4f preMult( const Vec4f& v ) const;
325 inline Vec4d preMult( const Vec4d& v ) const;
326 inline Vec4f postMult( const Vec4f& v ) const;
327 inline Vec4d postMult( const Vec4d& v ) const;
328 inline Vec4f operator* ( const Vec4f& v ) const;
329 inline Vec4d operator* ( const Vec4d& v ) const;
331#ifdef OSG_USE_DEPRECATED_API
332 inline void set(const Quat& q) { makeRotate(q); }
333 inline void get(Quat& q) const { q = getRotate(); }
336 void setRotate(const Quat& q);
337 /** Get the matrix rotation as a Quat. Note that this function
338 * assumes a non-scaled matrix and will return incorrect results
339 * for scaled matrixces. Consider decompose() instead.
341 Quat getRotate() const;
344 void setTrans( value_type tx, value_type ty, value_type tz );
345 void setTrans( const Vec3f& v );
346 void setTrans( const Vec3d& v );
348 inline Vec3d getTrans() const { return Vec3d(_mat[3][0],_mat[3][1],_mat[3][2]); }
350 inline Vec3d getScale() const {
351 Vec3d x_vec(_mat[0][0],_mat[1][0],_mat[2][0]);
352 Vec3d y_vec(_mat[0][1],_mat[1][1],_mat[2][1]);
353 Vec3d z_vec(_mat[0][2],_mat[1][2],_mat[2][2]);
354 return Vec3d(x_vec.length(), y_vec.length(), z_vec.length());
357 /** apply a 3x3 transform of v*M[0..2,0..2]. */
358 inline static Vec3f transform3x3(const Vec3f& v,const Matrixf& m);
360 /** apply a 3x3 transform of v*M[0..2,0..2]. */
361 inline static Vec3d transform3x3(const Vec3d& v,const Matrixf& m);
363 /** apply a 3x3 transform of M[0..2,0..2]*v. */
364 inline static Vec3f transform3x3(const Matrixf& m,const Vec3f& v);
366 /** apply a 3x3 transform of M[0..2,0..2]*v. */
367 inline static Vec3d transform3x3(const Matrixf& m,const Vec3d& v);
369 // basic Matrixf multiplication, our workhorse methods.
370 void mult( const Matrixf&, const Matrixf& );
371 void preMult( const Matrixf& );
372 void postMult( const Matrixf& );
374 /** Optimized version of preMult(translate(v)); */
375 inline void preMultTranslate( const Vec3d& v );
376 inline void preMultTranslate( const Vec3f& v );
377 /** Optimized version of postMult(translate(v)); */
378 inline void postMultTranslate( const Vec3d& v );
379 inline void postMultTranslate( const Vec3f& v );
381 /** Optimized version of preMult(scale(v)); */
382 inline void preMultScale( const Vec3d& v );
383 inline void preMultScale( const Vec3f& v );
384 /** Optimized version of postMult(scale(v)); */
385 inline void postMultScale( const Vec3d& v );
386 inline void postMultScale( const Vec3f& v );
388 /** Optimized version of preMult(rotate(q)); */
389 inline void preMultRotate( const Quat& q );
390 /** Optimized version of postMult(rotate(q)); */
391 inline void postMultRotate( const Quat& q );
393 inline void operator *= ( const Matrixf& other )
394 { if( this == &other ) {
398 else postMult( other );
401 inline Matrixf operator * ( const Matrixf &m ) const
408 /** Multiply by scalar. */
409 inline Matrixf operator * (value_type rhs) const
412 _mat[0][0]*rhs, _mat[0][1]*rhs, _mat[0][2]*rhs, _mat[0][3]*rhs,
413 _mat[1][0]*rhs, _mat[1][1]*rhs, _mat[1][2]*rhs, _mat[1][3]*rhs,
414 _mat[2][0]*rhs, _mat[2][1]*rhs, _mat[2][2]*rhs, _mat[2][3]*rhs,
415 _mat[3][0]*rhs, _mat[3][1]*rhs, _mat[3][2]*rhs, _mat[3][3]*rhs);
418 /** Unary multiply by scalar. */
419 inline Matrixf& operator *= (value_type rhs)
440 /** Divide by scalar. */
441 inline Matrixf operator / (value_type rhs) const
444 _mat[0][0]/rhs, _mat[0][1]/rhs, _mat[0][2]/rhs, _mat[0][3]/rhs,
445 _mat[1][0]/rhs, _mat[1][1]/rhs, _mat[1][2]/rhs, _mat[1][3]/rhs,
446 _mat[2][0]/rhs, _mat[2][1]/rhs, _mat[2][2]/rhs, _mat[2][3]/rhs,
447 _mat[3][0]/rhs, _mat[3][1]/rhs, _mat[3][2]/rhs, _mat[3][3]/rhs);
450 /** Unary divide by scalar. */
451 inline Matrixf& operator /= (value_type rhs)
472 /** Binary vector add. */
473 inline Matrixf operator + (const Matrixf& rhs) const
476 _mat[0][0] + rhs._mat[0][0],
477 _mat[0][1] + rhs._mat[0][1],
478 _mat[0][2] + rhs._mat[0][2],
479 _mat[0][3] + rhs._mat[0][3],
480 _mat[1][0] + rhs._mat[1][0],
481 _mat[1][1] + rhs._mat[1][1],
482 _mat[1][2] + rhs._mat[1][2],
483 _mat[1][3] + rhs._mat[1][3],
484 _mat[2][0] + rhs._mat[2][0],
485 _mat[2][1] + rhs._mat[2][1],
486 _mat[2][2] + rhs._mat[2][2],
487 _mat[2][3] + rhs._mat[2][3],
488 _mat[3][0] + rhs._mat[3][0],
489 _mat[3][1] + rhs._mat[3][1],
490 _mat[3][2] + rhs._mat[3][2],
491 _mat[3][3] + rhs._mat[3][3]);
494 /** Unary vector add. Slightly more efficient because no temporary
495 * intermediate object.
497 inline Matrixf& operator += (const Matrixf& rhs)
499 _mat[0][0] += rhs._mat[0][0];
500 _mat[0][1] += rhs._mat[0][1];
501 _mat[0][2] += rhs._mat[0][2];
502 _mat[0][3] += rhs._mat[0][3];
503 _mat[1][0] += rhs._mat[1][0];
504 _mat[1][1] += rhs._mat[1][1];
505 _mat[1][2] += rhs._mat[1][2];
506 _mat[1][3] += rhs._mat[1][3];
507 _mat[2][0] += rhs._mat[2][0];
508 _mat[2][1] += rhs._mat[2][1];
509 _mat[2][2] += rhs._mat[2][2];
510 _mat[2][3] += rhs._mat[2][3];
511 _mat[3][0] += rhs._mat[3][0];
512 _mat[3][1] += rhs._mat[3][1];
513 _mat[3][2] += rhs._mat[3][2];
514 _mat[3][3] += rhs._mat[3][3];
519 value_type _mat[4][4];
523class RefMatrixf : public Object, public Matrixf
527 RefMatrixf():Object(false), Matrixf() {}
528 RefMatrixf( const Matrixf& other) : Object(false), Matrixf(other) {}
529 RefMatrixf( const Matrixd& other) : Object(false), Matrixf(other) {}
530 RefMatrixf( const RefMatrixf& other) : Object(other), Matrixf(other) {}
531 explicit RefMatrixf( Matrixf::value_type const * const def ):Object(false), Matrixf(def) {}
532 RefMatrixf( Matrixf::value_type a00, Matrixf::value_type a01, Matrixf::value_type a02, Matrixf::value_type a03,
533 Matrixf::value_type a10, Matrixf::value_type a11, Matrixf::value_type a12, Matrixf::value_type a13,
534 Matrixf::value_type a20, Matrixf::value_type a21, Matrixf::value_type a22, Matrixf::value_type a23,
535 Matrixf::value_type a30, Matrixf::value_type a31, Matrixf::value_type a32, Matrixf::value_type a33):
537 Matrixf(a00, a01, a02, a03,
540 a30, a31, a32, a33) {}
542 virtual Object* cloneType() const { return new RefMatrixf(); }
543 virtual Object* clone(const CopyOp&) const { return new RefMatrixf(*this); }
544 virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const RefMatrixf*>(obj)!=NULL; }
545 virtual const char* libraryName() const { return "osg"; }
546 virtual const char* className() const { return "Matrix"; }
551 virtual ~RefMatrixf() {}
555//static utility methods
556inline Matrixf Matrixf::identity(void)
563inline Matrixf Matrixf::scale(value_type sx, value_type sy, value_type sz)
566 m.makeScale(sx,sy,sz);
570inline Matrixf Matrixf::scale(const Vec3f& v )
572 return scale(v.x(), v.y(), v.z() );
575inline Matrixf Matrixf::scale(const Vec3d& v )
577 return scale(v.x(), v.y(), v.z() );
580inline Matrixf Matrixf::translate(value_type tx, value_type ty, value_type tz)
583 m.makeTranslate(tx,ty,tz);
587inline Matrixf Matrixf::translate(const Vec3f& v )
589 return translate(v.x(), v.y(), v.z() );
592inline Matrixf Matrixf::translate(const Vec3d& v )
594 return translate(v.x(), v.y(), v.z() );
597inline Matrixf Matrixf::rotate( const Quat& q )
601inline Matrixf Matrixf::rotate(value_type angle, value_type x, value_type y, value_type z )
604 m.makeRotate(angle,x,y,z);
607inline Matrixf Matrixf::rotate(value_type angle, const Vec3f& axis )
610 m.makeRotate(angle,axis);
613inline Matrixf Matrixf::rotate(value_type angle, const Vec3d& axis )
616 m.makeRotate(angle,axis);
619inline Matrixf Matrixf::rotate( value_type angle1, const Vec3f& axis1,
620 value_type angle2, const Vec3f& axis2,
621 value_type angle3, const Vec3f& axis3)
624 m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
627inline Matrixf Matrixf::rotate( value_type angle1, const Vec3d& axis1,
628 value_type angle2, const Vec3d& axis2,
629 value_type angle3, const Vec3d& axis3)
632 m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
635inline Matrixf Matrixf::rotate(const Vec3f& from, const Vec3f& to )
638 m.makeRotate(from,to);
641inline Matrixf Matrixf::rotate(const Vec3d& from, const Vec3d& to )
644 m.makeRotate(from,to);
648inline Matrixf Matrixf::inverse( const Matrixf& matrix)
655inline Matrixf Matrixf::orthoNormal(const Matrixf& matrix)
658 m.orthoNormalize(matrix);
662inline Matrixf Matrixf::ortho(double left, double right,
663 double bottom, double top,
664 double zNear, double zFar)
667 m.makeOrtho(left,right,bottom,top,zNear,zFar);
671inline Matrixf Matrixf::ortho2D(double left, double right,
672 double bottom, double top)
675 m.makeOrtho2D(left,right,bottom,top);
679inline Matrixf Matrixf::frustum(double left, double right,
680 double bottom, double top,
681 double zNear, double zFar)
684 m.makeFrustum(left,right,bottom,top,zNear,zFar);
688inline Matrixf Matrixf::perspective(double fovy,double aspectRatio,
689 double zNear, double zFar)
692 m.makePerspective(fovy,aspectRatio,zNear,zFar);
696inline Matrixf Matrixf::lookAt(const Vec3f& eye,const Vec3f& center,const Vec3f& up)
699 m.makeLookAt(eye,center,up);
703inline Matrixf Matrixf::lookAt(const Vec3d& eye,const Vec3d& center,const Vec3d& up)
706 m.makeLookAt(eye,center,up);
710inline Vec3f Matrixf::postMult( const Vec3f& v ) const
712 value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
713 return Vec3f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
714 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
715 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
717inline Vec3d Matrixf::postMult( const Vec3d& v ) const
719 value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
720 return Vec3d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
721 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
722 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
725inline Vec3f Matrixf::preMult( const Vec3f& v ) const
727 value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
728 return Vec3f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
729 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
730 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
732inline Vec3d Matrixf::preMult( const Vec3d& v ) const
734 value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
735 return Vec3d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
736 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
737 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
740inline Vec4f Matrixf::postMult( const Vec4f& v ) const
742 return Vec4f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
743 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
744 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
745 (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
747inline Vec4d Matrixf::postMult( const Vec4d& v ) const
749 return Vec4d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
750 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
751 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
752 (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
755inline Vec4f Matrixf::preMult( const Vec4f& v ) const
757 return Vec4f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
758 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
759 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
760 (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
762inline Vec4d Matrixf::preMult( const Vec4d& v ) const
764 return Vec4d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
765 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
766 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
767 (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
769inline Vec3f Matrixf::transform3x3(const Vec3f& v,const Matrixf& m)
771 return Vec3f( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
772 (m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
773 (m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
775inline Vec3d Matrixf::transform3x3(const Vec3d& v,const Matrixf& m)
777 return Vec3d( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
778 (m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
779 (m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
782inline Vec3f Matrixf::transform3x3(const Matrixf& m,const Vec3f& v)
784 return Vec3f( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
785 (m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
786 (m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
788inline Vec3d Matrixf::transform3x3(const Matrixf& m,const Vec3d& v)
790 return Vec3d( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
791 (m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
792 (m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
795inline void Matrixf::preMultTranslate( const Vec3d& v )
797 for (unsigned i = 0; i < 3; ++i)
802 _mat[3][0] += tmp*_mat[i][0];
803 _mat[3][1] += tmp*_mat[i][1];
804 _mat[3][2] += tmp*_mat[i][2];
805 _mat[3][3] += tmp*_mat[i][3];
809inline void Matrixf::preMultTranslate( const Vec3f& v )
811 for (unsigned i = 0; i < 3; ++i)
816 _mat[3][0] += tmp*_mat[i][0];
817 _mat[3][1] += tmp*_mat[i][1];
818 _mat[3][2] += tmp*_mat[i][2];
819 _mat[3][3] += tmp*_mat[i][3];
823inline void Matrixf::postMultTranslate( const Vec3d& v )
825 for (unsigned i = 0; i < 3; ++i)
830 _mat[0][i] += tmp*_mat[0][3];
831 _mat[1][i] += tmp*_mat[1][3];
832 _mat[2][i] += tmp*_mat[2][3];
833 _mat[3][i] += tmp*_mat[3][3];
837inline void Matrixf::postMultTranslate( const Vec3f& v )
839 for (unsigned i = 0; i < 3; ++i)
844 _mat[0][i] += tmp*_mat[0][3];
845 _mat[1][i] += tmp*_mat[1][3];
846 _mat[2][i] += tmp*_mat[2][3];
847 _mat[3][i] += tmp*_mat[3][3];
851inline void Matrixf::preMultScale( const Vec3d& v )
853 _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
854 _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
855 _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
858inline void Matrixf::preMultScale( const Vec3f& v )
860 _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
861 _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
862 _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
865inline void Matrixf::postMultScale( const Vec3d& v )
867 _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
868 _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
869 _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
872inline void Matrixf::postMultScale( const Vec3f& v )
874 _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
875 _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
876 _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
880inline void Matrixf::preMultRotate( const Quat& q )
882 if (q.zeroRotation())
889inline void Matrixf::postMultRotate( const Quat& q )
891 if (q.zeroRotation())
898inline Vec3f operator* (const Vec3f& v, const Matrixf& m )
902inline Vec3d operator* (const Vec3d& v, const Matrixf& m )
906inline Vec4f operator* (const Vec4f& v, const Matrixf& m )
910inline Vec4d operator* (const Vec4d& v, const Matrixf& m )
915inline Vec3f Matrixf::operator* (const Vec3f& v) const
919inline Vec3d Matrixf::operator* (const Vec3d& v) const
923inline Vec4f Matrixf::operator* (const Vec4f& v) const
927inline Vec4d Matrixf::operator* (const Vec4d& v) const