openscenegraph
Matrixd
Go to the documentation of this file.
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
14#ifndef OSG_MATRIXD
15#define OSG_MATRIXD 1
16
17#include <osg/Object>
18#include <osg/Vec3d>
19#include <osg/Vec4d>
20#include <osg/Quat>
21
22namespace osg {
23
24class Matrixf;
25
26class OSG_EXPORT Matrixd
27{
28 public:
29
30 typedef double value_type;
31 typedef float other_value_type;
32
33 inline Matrixd() { makeIdentity(); }
34 inline Matrixd( const Matrixd& mat) { set(mat.ptr()); }
35 Matrixd( const Matrixf& mat );
36 inline explicit Matrixd( float const * const ptr ) { set(ptr); }
37 inline explicit Matrixd( double const * const ptr ) { set(ptr); }
38 inline explicit Matrixd( const Quat& quat ) { makeRotate(quat); }
39
40 Matrixd(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);
44
45 ~Matrixd() {}
46
47 int compare(const Matrixd& m) const;
48
49 bool operator < (const Matrixd& m) const { return compare(m)<0; }
50 bool operator == (const Matrixd& m) const { return compare(m)==0; }
51 bool operator != (const Matrixd& m) const { return compare(m)!=0; }
52
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]; }
55
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]); }
61
62 inline Matrixd& operator = (const Matrixd& rhs)
63 {
64 if( &rhs == this ) return *this;
65 set(rhs.ptr());
66 return *this;
67 }
68
69 Matrixd& operator = (const Matrixf& other);
70
71 inline void set(const Matrixd& rhs) { set(rhs.ptr()); }
72
73 void set(const Matrixf& rhs);
74
75 inline void set(float const * const ptr)
76 {
77 value_type* local_ptr = (value_type*)_mat;
78 for(int i=0;i<16;++i) local_ptr[i]=(value_type)ptr[i];
79 }
80
81 inline void set(double const * const ptr)
82 {
83 value_type* local_ptr = (value_type*)_mat;
84 for(int i=0;i<16;++i) local_ptr[i]=(value_type)ptr[i];
85 }
86
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);
91
92 value_type * ptr() { return (value_type*)_mat; }
93 const value_type * ptr() const { return (const value_type *)_mat; }
94
95 bool isIdentity() const
96 {
97 return _mat[0][0]==1.0 && _mat[0][1]==0.0 && _mat[0][2]==0.0 && _mat[0][3]==0.0 &&
98 _mat[1][0]==0.0 && _mat[1][1]==1.0 && _mat[1][2]==0.0 && _mat[1][3]==0.0 &&
99 _mat[2][0]==0.0 && _mat[2][1]==0.0 && _mat[2][2]==1.0 && _mat[2][3]==0.0 &&
100 _mat[3][0]==0.0 && _mat[3][1]==0.0 && _mat[3][2]==0.0 && _mat[3][3]==1.0;
101 }
102
103 void makeIdentity();
104
105 void makeScale( const Vec3f& );
106 void makeScale( const Vec3d& );
107 void makeScale( value_type, value_type, value_type );
108
109 void makeTranslate( const Vec3f& );
110 void makeTranslate( const Vec3d& );
111 void makeTranslate( value_type, value_type, value_type );
112
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);
125
126
127 /** decompose the matrix into translation, rotation, scale and scale orientation.*/
128 void decompose( osg::Vec3f& translation,
129 osg::Quat& rotation,
130 osg::Vec3f& scale,
131 osg::Quat& so ) const;
132
133 /** decompose the matrix into translation, rotation, scale and scale orientation.*/
134 void decompose( osg::Vec3d& translation,
135 osg::Quat& rotation,
136 osg::Vec3d& scale,
137 osg::Quat& so ) const;
138
139
140 /** Set to an orthographic projection.
141 * See glOrtho for further details.
142 */
143 void makeOrtho(double left, double right,
144 double bottom, double top,
145 double zNear, double zFar);
146
147 /** Get the orthographic settings of the orthographic projection matrix.
148 * Note, if matrix is not an orthographic matrix then invalid values
149 * will be returned.
150 */
151 bool getOrtho(double& left, double& right,
152 double& bottom, double& top,
153 double& zNear, double& zFar) const;
154
155 /** float version of getOrtho(..) */
156 bool getOrtho(float& left, float& right,
157 float& bottom, float& top,
158 float& zNear, float& zFar) const;
159
160
161 /** Set to a 2D orthographic projection.
162 * See glOrtho2D for further details.
163 */
164 inline void makeOrtho2D(double left, double right,
165 double bottom, double top)
166 {
167 makeOrtho(left,right,bottom,top,-1.0,1.0);
168 }
169
170
171 /** Set to a perspective projection.
172 * See glFrustum for further details.
173 */
174 void makeFrustum(double left, double right,
175 double bottom, double top,
176 double zNear, double zFar);
177
178 /** Get the frustum settings of a perspective projection matrix.
179 * Note, if matrix is not a perspective matrix then invalid values
180 * will be returned.
181 */
182 bool getFrustum(double& left, double& right,
183 double& bottom, double& top,
184 double& zNear, double& zFar) const;
185
186 /** float version of getFrustum(..) */
187 bool getFrustum(float& left, float& right,
188 float& bottom, float& top,
189 float& zNear, float& zFar) const;
190
191 /** Set to a symmetrical perspective projection.
192 * See gluPerspective for further details.
193 * Aspect ratio is defined as width/height.
194 */
195 void makePerspective(double fovy, double aspectRatio,
196 double zNear, double zFar);
197
198 /** Get the frustum settings of a symmetric perspective projection
199 * matrix.
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.
207 */
208 bool getPerspective(double& fovy, double& aspectRatio,
209 double& zNear, double& zFar) const;
210
211 /** float version of getPerspective(..) */
212 bool getPerspective(float& fovy, float& aspectRatio,
213 float& zNear, float& zFar) const;
214
215 /** Set the position and orientation to be a view matrix,
216 * using the same convention as gluLookAt.
217 */
218 void makeLookAt(const Vec3d& eye,const Vec3d& center,const Vec3d& up);
219
220 /** Get to the position and orientation of a modelview matrix,
221 * using the same convention as gluLookAt.
222 */
223 void getLookAt(Vec3f& eye,Vec3f& center,Vec3f& up,
224 value_type lookDistance=1.0f) const;
225
226 /** Get to the position and orientation of a modelview matrix,
227 * using the same convention as gluLookAt.
228 */
229 void getLookAt(Vec3d& eye,Vec3d& center,Vec3d& up,
230 value_type lookDistance=1.0f) const;
231
232 /** invert the matrix rhs, automatically select invert_4x3 or invert_4x4. */
233 inline bool invert( const Matrixd& rhs)
234 {
235 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);
236 return is_4x3 ? invert_4x3(rhs) : invert_4x4(rhs);
237 }
238
239 /** 4x3 matrix invert, not right hand column is assumed to be 0,0,0,1. */
240 bool invert_4x3( const Matrixd& rhs);
241
242 /** full 4x4 matrix invert. */
243 bool invert_4x4( const Matrixd& rhs);
244
245 /** transpose a matrix */
246 bool transpose(const Matrixd&rhs);
247
248 /** transpose orthogonal part of the matrix **/
249 bool transpose3x3(const Matrixd&rhs);
250
251 /** ortho-normalize the 3x3 rotation & scale matrix */
252 void orthoNormalize(const Matrixd& rhs);
253
254 // basic utility functions to create new matrices
255 inline static Matrixd identity( void );
256 inline static Matrixd scale( const Vec3f& sv);
257 inline static Matrixd scale( const Vec3d& sv);
258 inline static Matrixd scale( value_type sx, value_type sy, value_type sz);
259 inline static Matrixd translate( const Vec3f& dv);
260 inline static Matrixd translate( const Vec3d& dv);
261 inline static Matrixd translate( value_type x, value_type y, value_type z);
262 inline static Matrixd rotate( const Vec3f& from, const Vec3f& to);
263 inline static Matrixd rotate( const Vec3d& from, const Vec3d& to);
264 inline static Matrixd rotate( value_type angle, value_type x, value_type y, value_type z);
265 inline static Matrixd rotate( value_type angle, const Vec3f& axis);
266 inline static Matrixd rotate( value_type angle, const Vec3d& axis);
267 inline static Matrixd rotate( value_type angle1, const Vec3f& axis1,
268 value_type angle2, const Vec3f& axis2,
269 value_type angle3, const Vec3f& axis3);
270 inline static Matrixd rotate( value_type angle1, const Vec3d& axis1,
271 value_type angle2, const Vec3d& axis2,
272 value_type angle3, const Vec3d& axis3);
273 inline static Matrixd rotate( const Quat& quat);
274 inline static Matrixd inverse( const Matrixd& matrix);
275 inline static Matrixd orthoNormal(const Matrixd& matrix);
276 /** Create an orthographic projection matrix.
277 * See glOrtho for further details.
278 */
279 inline static Matrixd ortho(double left, double right,
280 double bottom, double top,
281 double zNear, double zFar);
282
283 /** Create a 2D orthographic projection.
284 * See glOrtho for further details.
285 */
286 inline static Matrixd ortho2D(double left, double right,
287 double bottom, double top);
288
289 /** Create a perspective projection.
290 * See glFrustum for further details.
291 */
292 inline static Matrixd frustum(double left, double right,
293 double bottom, double top,
294 double zNear, double zFar);
295
296 /** Create a symmetrical perspective projection.
297 * See gluPerspective for further details.
298 * Aspect ratio is defined as width/height.
299 */
300 inline static Matrixd perspective(double fovy, double aspectRatio,
301 double zNear, double zFar);
302
303 /** Create the position and orientation as per a camera,
304 * using the same convention as gluLookAt.
305 */
306 inline static Matrixd lookAt(const Vec3f& eye,
307 const Vec3f& center,
308 const Vec3f& up);
309
310 /** Create the position and orientation as per a camera,
311 * using the same convention as gluLookAt.
312 */
313 inline static Matrixd lookAt(const Vec3d& eye,
314 const Vec3d& center,
315 const Vec3d& up);
316
317 inline Vec3f preMult( const Vec3f& v ) const;
318 inline Vec3d preMult( const Vec3d& v ) const;
319 inline Vec3f postMult( const Vec3f& v ) const;
320 inline Vec3d postMult( const Vec3d& v ) const;
321 inline Vec3f operator* ( const Vec3f& v ) const;
322 inline Vec3d operator* ( const Vec3d& v ) const;
323 inline Vec4f preMult( const Vec4f& v ) const;
324 inline Vec4d preMult( const Vec4d& v ) const;
325 inline Vec4f postMult( const Vec4f& v ) const;
326 inline Vec4d postMult( const Vec4d& v ) const;
327 inline Vec4f operator* ( const Vec4f& v ) const;
328 inline Vec4d operator* ( const Vec4d& v ) const;
329
330#ifdef OSG_USE_DEPRECATED_API
331 inline void set(const Quat& q) { makeRotate(q); } /// deprecated, replace with makeRotate(q)
332 inline void get(Quat& q) const { q = getRotate(); } /// deprecated, replace with getRotate()
333#endif
334
335 void setRotate(const Quat& q);
336 /** Get the matrix rotation as a Quat. Note that this function
337 * assumes a non-scaled matrix and will return incorrect results
338 * for scaled matrixces. Consider decompose() instead.
339 */
340 Quat getRotate() const;
341
342 void setTrans( value_type tx, value_type ty, value_type tz );
343 void setTrans( const Vec3f& v );
344 void setTrans( const Vec3d& v );
345
346 inline Vec3d getTrans() const { return Vec3d(_mat[3][0],_mat[3][1],_mat[3][2]); }
347
348 inline Vec3d getScale() const {
349 Vec3d x_vec(_mat[0][0],_mat[1][0],_mat[2][0]);
350 Vec3d y_vec(_mat[0][1],_mat[1][1],_mat[2][1]);
351 Vec3d z_vec(_mat[0][2],_mat[1][2],_mat[2][2]);
352 return Vec3d(x_vec.length(), y_vec.length(), z_vec.length());
353 }
354
355 /** apply a 3x3 transform of v*M[0..2,0..2]. */
356 inline static Vec3f transform3x3(const Vec3f& v,const Matrixd& m);
357
358 /** apply a 3x3 transform of v*M[0..2,0..2]. */
359 inline static Vec3d transform3x3(const Vec3d& v,const Matrixd& m);
360
361 /** apply a 3x3 transform of M[0..2,0..2]*v. */
362 inline static Vec3f transform3x3(const Matrixd& m,const Vec3f& v);
363
364 /** apply a 3x3 transform of M[0..2,0..2]*v. */
365 inline static Vec3d transform3x3(const Matrixd& m,const Vec3d& v);
366
367 // basic Matrixd multiplication, our workhorse methods.
368 void mult( const Matrixd&, const Matrixd& );
369 void preMult( const Matrixd& );
370 void postMult( const Matrixd& );
371
372 /** Optimized version of preMult(translate(v)); */
373 inline void preMultTranslate( const Vec3d& v );
374 inline void preMultTranslate( const Vec3f& v );
375 /** Optimized version of postMult(translate(v)); */
376 inline void postMultTranslate( const Vec3d& v );
377 inline void postMultTranslate( const Vec3f& v );
378
379 /** Optimized version of preMult(scale(v)); */
380 inline void preMultScale( const Vec3d& v );
381 inline void preMultScale( const Vec3f& v );
382 /** Optimized version of postMult(scale(v)); */
383 inline void postMultScale( const Vec3d& v );
384 inline void postMultScale( const Vec3f& v );
385
386 /** Optimized version of preMult(rotate(q)); */
387 inline void preMultRotate( const Quat& q );
388 /** Optimized version of postMult(rotate(q)); */
389 inline void postMultRotate( const Quat& q );
390
391 inline void operator *= ( const Matrixd& other )
392 { if( this == &other ) {
393 Matrixd temp(other);
394 postMult( temp );
395 }
396 else postMult( other );
397 }
398
399 inline Matrixd operator * ( const Matrixd &m ) const
400 {
401 osg::Matrixd r;
402 r.mult(*this,m);
403 return r;
404 }
405
406 protected:
407 value_type _mat[4][4];
408
409};
410
411class RefMatrixd : public Object, public Matrixd
412{
413 public:
414
415 RefMatrixd():Object(false), Matrixd() {}
416 RefMatrixd( const Matrixd& other) : Object(false), Matrixd(other) {}
417 RefMatrixd( const Matrixf& other) : Object(false), Matrixd(other) {}
418 RefMatrixd( const RefMatrixd& other) : Object(other), Matrixd(other) {}
419 explicit RefMatrixd( Matrixd::value_type const * const def ):Object(false), Matrixd(def) {}
420 RefMatrixd( Matrixd::value_type a00, Matrixd::value_type a01, Matrixd::value_type a02, Matrixd::value_type a03,
421 Matrixd::value_type a10, Matrixd::value_type a11, Matrixd::value_type a12, Matrixd::value_type a13,
422 Matrixd::value_type a20, Matrixd::value_type a21, Matrixd::value_type a22, Matrixd::value_type a23,
423 Matrixd::value_type a30, Matrixd::value_type a31, Matrixd::value_type a32, Matrixd::value_type a33):
424 Object(false),
425 Matrixd(a00, a01, a02, a03,
426 a10, a11, a12, a13,
427 a20, a21, a22, a23,
428 a30, a31, a32, a33) {}
429
430 virtual Object* cloneType() const { return new RefMatrixd(); }
431 virtual Object* clone(const CopyOp&) const { return new RefMatrixd(*this); }
432 virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const RefMatrixd*>(obj)!=NULL; }
433 virtual const char* libraryName() const { return "osg"; }
434 virtual const char* className() const { return "Matrix"; }
435
436
437 protected:
438
439 virtual ~RefMatrixd() {}
440};
441
442
443// static utility methods
444inline Matrixd Matrixd::identity(void)
445{
446 Matrixd m;
447 m.makeIdentity();
448 return m;
449}
450
451inline Matrixd Matrixd::scale(value_type sx, value_type sy, value_type sz)
452{
453 Matrixd m;
454 m.makeScale(sx,sy,sz);
455 return m;
456}
457
458inline Matrixd Matrixd::scale(const Vec3f& v )
459{
460 return scale(v.x(), v.y(), v.z() );
461}
462
463inline Matrixd Matrixd::scale(const Vec3d& v )
464{
465 return scale(v.x(), v.y(), v.z() );
466}
467
468inline Matrixd Matrixd::translate(value_type tx, value_type ty, value_type tz)
469{
470 Matrixd m;
471 m.makeTranslate(tx,ty,tz);
472 return m;
473}
474
475inline Matrixd Matrixd::translate(const Vec3f& v )
476{
477 return translate(v.x(), v.y(), v.z() );
478}
479
480inline Matrixd Matrixd::translate(const Vec3d& v )
481{
482 return translate(v.x(), v.y(), v.z() );
483}
484
485inline Matrixd Matrixd::rotate( const Quat& q )
486{
487 return Matrixd(q);
488}
489inline Matrixd Matrixd::rotate(value_type angle, value_type x, value_type y, value_type z )
490{
491 Matrixd m;
492 m.makeRotate(angle,x,y,z);
493 return m;
494}
495inline Matrixd Matrixd::rotate(value_type angle, const Vec3f& axis )
496{
497 Matrixd m;
498 m.makeRotate(angle,axis);
499 return m;
500}
501inline Matrixd Matrixd::rotate(value_type angle, const Vec3d& axis )
502{
503 Matrixd m;
504 m.makeRotate(angle,axis);
505 return m;
506}
507inline Matrixd Matrixd::rotate( value_type angle1, const Vec3f& axis1,
508 value_type angle2, const Vec3f& axis2,
509 value_type angle3, const Vec3f& axis3)
510{
511 Matrixd m;
512 m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
513 return m;
514}
515inline Matrixd Matrixd::rotate( value_type angle1, const Vec3d& axis1,
516 value_type angle2, const Vec3d& axis2,
517 value_type angle3, const Vec3d& axis3)
518{
519 Matrixd m;
520 m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
521 return m;
522}
523inline Matrixd Matrixd::rotate(const Vec3f& from, const Vec3f& to )
524{
525 Matrixd m;
526 m.makeRotate(from,to);
527 return m;
528}
529inline Matrixd Matrixd::rotate(const Vec3d& from, const Vec3d& to )
530{
531 Matrixd m;
532 m.makeRotate(from,to);
533 return m;
534}
535
536inline Matrixd Matrixd::inverse( const Matrixd& matrix)
537{
538 Matrixd m;
539 m.invert(matrix);
540 return m;
541}
542
543inline Matrixd Matrixd::orthoNormal(const Matrixd& matrix)
544{
545 Matrixd m;
546 m.orthoNormalize(matrix);
547 return m;
548}
549
550inline Matrixd Matrixd::ortho(double left, double right,
551 double bottom, double top,
552 double zNear, double zFar)
553{
554 Matrixd m;
555 m.makeOrtho(left,right,bottom,top,zNear,zFar);
556 return m;
557}
558
559inline Matrixd Matrixd::ortho2D(double left, double right,
560 double bottom, double top)
561{
562 Matrixd m;
563 m.makeOrtho2D(left,right,bottom,top);
564 return m;
565}
566
567inline Matrixd Matrixd::frustum(double left, double right,
568 double bottom, double top,
569 double zNear, double zFar)
570{
571 Matrixd m;
572 m.makeFrustum(left,right,bottom,top,zNear,zFar);
573 return m;
574}
575
576inline Matrixd Matrixd::perspective(double fovy, double aspectRatio,
577 double zNear, double zFar)
578{
579 Matrixd m;
580 m.makePerspective(fovy,aspectRatio,zNear,zFar);
581 return m;
582}
583
584inline Matrixd Matrixd::lookAt(const Vec3f& eye,
585 const Vec3f& center,
586 const Vec3f& up)
587{
588 Matrixd m;
589 m.makeLookAt(eye,center,up);
590 return m;
591}
592
593inline Matrixd Matrixd::lookAt(const Vec3d& eye,
594 const Vec3d& center,
595 const Vec3d& up)
596{
597 Matrixd m;
598 m.makeLookAt(eye,center,up);
599 return m;
600}
601
602inline Vec3f Matrixd::postMult( const Vec3f& v ) const
603{
604 value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
605 return Vec3f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
606 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
607 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
608}
609
610inline Vec3d Matrixd::postMult( const Vec3d& v ) const
611{
612 value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
613 return Vec3d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
614 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
615 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
616}
617
618inline Vec3f Matrixd::preMult( const Vec3f& v ) const
619{
620 value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
621 return Vec3f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
622 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
623 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
624}
625
626inline Vec3d Matrixd::preMult( const Vec3d& v ) const
627{
628 value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
629 return Vec3d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
630 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
631 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
632}
633
634inline Vec4f Matrixd::postMult( const Vec4f& v ) const
635{
636 return Vec4f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
637 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
638 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
639 (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
640}
641inline Vec4d Matrixd::postMult( const Vec4d& v ) const
642{
643 return Vec4d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
644 (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
645 (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
646 (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
647}
648
649inline Vec4f Matrixd::preMult( const Vec4f& v ) const
650{
651 return Vec4f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
652 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
653 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
654 (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
655}
656
657inline Vec4d Matrixd::preMult( const Vec4d& v ) const
658{
659 return Vec4d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
660 (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
661 (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
662 (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
663}
664
665inline Vec3f Matrixd::transform3x3(const Vec3f& v,const Matrixd& m)
666{
667 return Vec3f( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
668 (m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
669 (m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
670}
671inline Vec3d Matrixd::transform3x3(const Vec3d& v,const Matrixd& m)
672{
673 return Vec3d( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
674 (m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
675 (m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
676}
677
678inline Vec3f Matrixd::transform3x3(const Matrixd& m,const Vec3f& v)
679{
680 return Vec3f( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
681 (m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
682 (m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
683}
684inline Vec3d Matrixd::transform3x3(const Matrixd& m,const Vec3d& v)
685{
686 return Vec3d( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
687 (m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
688 (m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
689}
690
691inline void Matrixd::preMultTranslate( const Vec3d& v )
692{
693 for (unsigned i = 0; i < 3; ++i)
694 {
695 double tmp = v[i];
696 if (tmp == 0)
697 continue;
698 _mat[3][0] += tmp*_mat[i][0];
699 _mat[3][1] += tmp*_mat[i][1];
700 _mat[3][2] += tmp*_mat[i][2];
701 _mat[3][3] += tmp*_mat[i][3];
702 }
703}
704
705inline void Matrixd::preMultTranslate( const Vec3f& v )
706{
707 for (unsigned i = 0; i < 3; ++i)
708 {
709 float tmp = v[i];
710 if (tmp == 0)
711 continue;
712 _mat[3][0] += tmp*_mat[i][0];
713 _mat[3][1] += tmp*_mat[i][1];
714 _mat[3][2] += tmp*_mat[i][2];
715 _mat[3][3] += tmp*_mat[i][3];
716 }
717}
718
719inline void Matrixd::postMultTranslate( const Vec3d& v )
720{
721 for (unsigned i = 0; i < 3; ++i)
722 {
723 double tmp = v[i];
724 if (tmp == 0)
725 continue;
726 _mat[0][i] += tmp*_mat[0][3];
727 _mat[1][i] += tmp*_mat[1][3];
728 _mat[2][i] += tmp*_mat[2][3];
729 _mat[3][i] += tmp*_mat[3][3];
730 }
731}
732
733inline void Matrixd::postMultTranslate( const Vec3f& v )
734{
735 for (unsigned i = 0; i < 3; ++i)
736 {
737 float tmp = v[i];
738 if (tmp == 0)
739 continue;
740 _mat[0][i] += tmp*_mat[0][3];
741 _mat[1][i] += tmp*_mat[1][3];
742 _mat[2][i] += tmp*_mat[2][3];
743 _mat[3][i] += tmp*_mat[3][3];
744 }
745}
746
747inline void Matrixd::preMultScale( const Vec3d& v )
748{
749 _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
750 _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
751 _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
752}
753
754inline void Matrixd::preMultScale( const Vec3f& v )
755{
756 _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
757 _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
758 _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
759}
760
761inline void Matrixd::postMultScale( const Vec3d& v )
762{
763 _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
764 _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
765 _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
766}
767
768inline void Matrixd::postMultScale( const Vec3f& v )
769{
770 _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
771 _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
772 _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
773}
774
775inline void Matrixd::preMultRotate( const Quat& q )
776{
777 if (q.zeroRotation())
778 return;
779 Matrixd r;
780 r.setRotate(q);
781 preMult(r);
782}
783
784inline void Matrixd::postMultRotate( const Quat& q )
785{
786 if (q.zeroRotation())
787 return;
788 Matrixd r;
789 r.setRotate(q);
790 postMult(r);
791}
792
793inline Vec3f operator* (const Vec3f& v, const Matrixd& m )
794{
795 return m.preMult(v);
796}
797
798inline Vec3d operator* (const Vec3d& v, const Matrixd& m )
799{
800 return m.preMult(v);
801}
802
803inline Vec4f operator* (const Vec4f& v, const Matrixd& m )
804{
805 return m.preMult(v);
806}
807
808inline Vec4d operator* (const Vec4d& v, const Matrixd& m )
809{
810 return m.preMult(v);
811}
812
813inline Vec3f Matrixd::operator* (const Vec3f& v) const
814{
815 return postMult(v);
816}
817
818inline Vec3d Matrixd::operator* (const Vec3d& v) const
819{
820 return postMult(v);
821}
822
823inline Vec4f Matrixd::operator* (const Vec4f& v) const
824{
825 return postMult(v);
826}
827
828inline Vec4d Matrixd::operator* (const Vec4d& v) const
829{
830 return postMult(v);
831}
832
833
834} //namespace osg
835
836
837#endif