OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Matrixf.h
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_MATRIXF
15 #define OSG_MATRIXF 1
16 
17 #include <osg/Object>
18 #include <osg/Vec3d>
19 #include <osg/Vec4d>
20 #include <osg/Quat>
21 
22 namespace osg {
23 
24 class Matrixf;
25 
27 {
28  public:
29 
30  typedef float value_type;
31  typedef double other_value_type;
32 
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); }
39 
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);
44 
45  ~Matrixf() {}
46 
47  int compare(const Matrixf& m) const;
48 
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; }
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 Matrixf& operator = (const Matrixf& rhs)
63  {
64  if( &rhs == this ) return *this;
65  set(rhs.ptr());
66  return *this;
67  }
68 
69  Matrixf& operator = (const Matrixd& other);
70 
71  inline void set(const Matrixf& rhs) { set(rhs.ptr()); }
72 
73  void set(const Matrixd& 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.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;
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 
128  void decompose( osg::Vec3f& translation,
129  osg::Quat& rotation,
130  osg::Vec3f& scale,
131  osg::Quat& so ) const;
132 
134  void decompose( osg::Vec3d& translation,
135  osg::Quat& rotation,
136  osg::Vec3d& scale,
137  osg::Quat& so ) const;
138 
139 
143  void makeOrtho(double left, double right,
144  double bottom, double top,
145  double zNear, double zFar);
146 
151  bool getOrtho(double& left, double& right,
152  double& bottom, double& top,
153  double& zNear, double& zFar) const;
154 
156  bool getOrtho(float& left, float& right,
157  float& bottom, float& top,
158  float& zNear, float& zFar) const;
159 
160 
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 
174  void makeFrustum(double left, double right,
175  double bottom, double top,
176  double zNear, double zFar);
177 
182  bool getFrustum(double& left, double& right,
183  double& bottom, double& top,
184  double& zNear, double& zFar) const;
185 
187  bool getFrustum(float& left, float& right,
188  float& bottom, float& top,
189  float& zNear, float& zFar) const;
190 
195  void makePerspective(double fovy, double aspectRatio,
196  double zNear, double zFar);
197 
208  bool getPerspective(double& fovy, double& aspectRatio,
209  double& zNear, double& zFar) const;
210 
212  bool getPerspective(float& fovy, float& aspectRatio,
213  float& zNear, float& zFar) const;
214 
218  void makeLookAt(const Vec3d& eye,const Vec3d& center,const Vec3d& up);
219 
223  void getLookAt(Vec3f& eye,Vec3f& center,Vec3f& up,
224  value_type lookDistance=1.0f) const;
225 
229  void getLookAt(Vec3d& eye,Vec3d& center,Vec3d& up,
230  value_type lookDistance=1.0f) const;
231 
233  inline bool invert( const Matrixf& rhs)
234  {
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);
237  }
238 
240  bool invert_4x3( const Matrixf& rhs);
241 
243  bool invert_4x4( const Matrixf& rhs);
244 
246  void orthoNormalize(const Matrixf& rhs);
247 
248  //basic utility functions to create new matrices
249  inline static Matrixf identity( void );
250  inline static Matrixf scale( const Vec3f& sv);
251  inline static Matrixf scale( const Vec3d& sv);
252  inline static Matrixf scale( value_type sx, value_type sy, value_type sz);
253  inline static Matrixf translate( const Vec3f& dv);
254  inline static Matrixf translate( const Vec3d& dv);
255  inline static Matrixf translate( value_type x, value_type y, value_type z);
256  inline static Matrixf rotate( const Vec3f& from, const Vec3f& to);
257  inline static Matrixf rotate( const Vec3d& from, const Vec3d& to);
258  inline static Matrixf rotate( value_type angle, value_type x, value_type y, value_type z);
259  inline static Matrixf rotate( value_type angle, const Vec3f& axis);
260  inline static Matrixf rotate( value_type angle, const Vec3d& axis);
261  inline static Matrixf rotate( value_type angle1, const Vec3f& axis1,
262  value_type angle2, const Vec3f& axis2,
263  value_type angle3, const Vec3f& axis3);
264  inline static Matrixf rotate( value_type angle1, const Vec3d& axis1,
265  value_type angle2, const Vec3d& axis2,
266  value_type angle3, const Vec3d& axis3);
267  inline static Matrixf rotate( const Quat& quat);
268  inline static Matrixf inverse( const Matrixf& matrix);
269  inline static Matrixf orthoNormal(const Matrixf& matrix);
270 
274  inline static Matrixf ortho(double left, double right,
275  double bottom, double top,
276  double zNear, double zFar);
277 
281  inline static Matrixf ortho2D(double left, double right,
282  double bottom, double top);
283 
287  inline static Matrixf frustum(double left, double right,
288  double bottom, double top,
289  double zNear, double zFar);
290 
295  inline static Matrixf perspective(double fovy, double aspectRatio,
296  double zNear, double zFar);
297 
301  inline static Matrixf lookAt(const Vec3f& eye,
302  const Vec3f& center,
303  const Vec3f& up);
304 
308  inline static Matrixf lookAt(const Vec3d& eye,
309  const Vec3d& center,
310  const Vec3d& up);
311 
312  inline Vec3f preMult( const Vec3f& v ) const;
313  inline Vec3d preMult( const Vec3d& v ) const;
314  inline Vec3f postMult( const Vec3f& v ) const;
315  inline Vec3d postMult( const Vec3d& v ) const;
316  inline Vec3f operator* ( const Vec3f& v ) const;
317  inline Vec3d operator* ( const Vec3d& v ) const;
318  inline Vec4f preMult( const Vec4f& v ) const;
319  inline Vec4d preMult( const Vec4d& v ) const;
320  inline Vec4f postMult( const Vec4f& v ) const;
321  inline Vec4d postMult( const Vec4d& v ) const;
322  inline Vec4f operator* ( const Vec4f& v ) const;
323  inline Vec4d operator* ( const Vec4d& v ) const;
324 
325 #ifdef USE_DEPRECATED_API
326  inline void set(const Quat& q) { makeRotate(q); }
327  inline void get(Quat& q) const { q = getRotate(); }
328 #endif
329 
330  void setRotate(const Quat& q);
335  Quat getRotate() const;
336 
337 
338  void setTrans( value_type tx, value_type ty, value_type tz );
339  void setTrans( const Vec3f& v );
340  void setTrans( const Vec3d& v );
341 
342  inline Vec3d getTrans() const { return Vec3d(_mat[3][0],_mat[3][1],_mat[3][2]); }
343 
344  inline Vec3d getScale() const {
345  Vec3d x_vec(_mat[0][0],_mat[1][0],_mat[2][0]);
346  Vec3d y_vec(_mat[0][1],_mat[1][1],_mat[2][1]);
347  Vec3d z_vec(_mat[0][2],_mat[1][2],_mat[2][2]);
348  return Vec3d(x_vec.length(), y_vec.length(), z_vec.length());
349  }
350 
352  inline static Vec3f transform3x3(const Vec3f& v,const Matrixf& m);
353 
355  inline static Vec3d transform3x3(const Vec3d& v,const Matrixf& m);
356 
358  inline static Vec3f transform3x3(const Matrixf& m,const Vec3f& v);
359 
361  inline static Vec3d transform3x3(const Matrixf& m,const Vec3d& v);
362 
363  // basic Matrixf multiplication, our workhorse methods.
364  void mult( const Matrixf&, const Matrixf& );
365  void preMult( const Matrixf& );
366  void postMult( const Matrixf& );
367 
369  inline void preMultTranslate( const Vec3d& v );
370  inline void preMultTranslate( const Vec3f& v );
372  inline void postMultTranslate( const Vec3d& v );
373  inline void postMultTranslate( const Vec3f& v );
374 
376  inline void preMultScale( const Vec3d& v );
377  inline void preMultScale( const Vec3f& v );
379  inline void postMultScale( const Vec3d& v );
380  inline void postMultScale( const Vec3f& v );
381 
383  inline void preMultRotate( const Quat& q );
385  inline void postMultRotate( const Quat& q );
386 
387  inline void operator *= ( const Matrixf& other )
388  { if( this == &other ) {
389  Matrixf temp(other);
390  postMult( temp );
391  }
392  else postMult( other );
393  }
394 
395  inline Matrixf operator * ( const Matrixf &m ) const
396  {
397  osg::Matrixf r;
398  r.mult(*this,m);
399  return r;
400  }
401 
403  inline Matrixf operator * (value_type rhs) const
404  {
405  return Matrixf(
406  _mat[0][0]*rhs, _mat[0][1]*rhs, _mat[0][2]*rhs, _mat[0][3]*rhs,
407  _mat[1][0]*rhs, _mat[1][1]*rhs, _mat[1][2]*rhs, _mat[1][3]*rhs,
408  _mat[2][0]*rhs, _mat[2][1]*rhs, _mat[2][2]*rhs, _mat[2][3]*rhs,
409  _mat[3][0]*rhs, _mat[3][1]*rhs, _mat[3][2]*rhs, _mat[3][3]*rhs);
410  }
411 
413  inline Matrixf& operator *= (value_type rhs)
414  {
415  _mat[0][0]*=rhs;
416  _mat[0][1]*=rhs;
417  _mat[0][2]*=rhs;
418  _mat[0][3]*=rhs;
419  _mat[1][0]*=rhs;
420  _mat[1][1]*=rhs;
421  _mat[1][2]*=rhs;
422  _mat[1][3]*=rhs;
423  _mat[2][0]*=rhs;
424  _mat[2][1]*=rhs;
425  _mat[2][2]*=rhs;
426  _mat[2][3]*=rhs;
427  _mat[3][0]*=rhs;
428  _mat[3][1]*=rhs;
429  _mat[3][2]*=rhs;
430  _mat[3][3]*=rhs;
431  return *this;
432  }
433 
435  inline Matrixf operator / (value_type rhs) const
436  {
437  return Matrixf(
438  _mat[0][0]/rhs, _mat[0][1]/rhs, _mat[0][2]/rhs, _mat[0][3]/rhs,
439  _mat[1][0]/rhs, _mat[1][1]/rhs, _mat[1][2]/rhs, _mat[1][3]/rhs,
440  _mat[2][0]/rhs, _mat[2][1]/rhs, _mat[2][2]/rhs, _mat[2][3]/rhs,
441  _mat[3][0]/rhs, _mat[3][1]/rhs, _mat[3][2]/rhs, _mat[3][3]/rhs);
442  }
443 
445  inline Matrixf& operator /= (value_type rhs)
446  {
447  _mat[0][0]/=rhs;
448  _mat[0][1]/=rhs;
449  _mat[0][2]/=rhs;
450  _mat[0][3]/=rhs;
451  _mat[1][0]/=rhs;
452  _mat[1][1]/=rhs;
453  _mat[1][2]/=rhs;
454  _mat[1][3]/=rhs;
455  _mat[2][0]/=rhs;
456  _mat[2][1]/=rhs;
457  _mat[2][2]/=rhs;
458  _mat[2][3]/=rhs;
459  _mat[3][0]/=rhs;
460  _mat[3][1]/=rhs;
461  _mat[3][2]/=rhs;
462  _mat[3][3]/=rhs;
463  return *this;
464  }
465 
467  inline Matrixf operator + (const Matrixf& rhs) const
468  {
469  return Matrixf(
470  _mat[0][0] + rhs._mat[0][0],
471  _mat[0][1] + rhs._mat[0][1],
472  _mat[0][2] + rhs._mat[0][2],
473  _mat[0][3] + rhs._mat[0][3],
474  _mat[1][0] + rhs._mat[1][0],
475  _mat[1][1] + rhs._mat[1][1],
476  _mat[1][2] + rhs._mat[1][2],
477  _mat[1][3] + rhs._mat[1][3],
478  _mat[2][0] + rhs._mat[2][0],
479  _mat[2][1] + rhs._mat[2][1],
480  _mat[2][2] + rhs._mat[2][2],
481  _mat[2][3] + rhs._mat[2][3],
482  _mat[3][0] + rhs._mat[3][0],
483  _mat[3][1] + rhs._mat[3][1],
484  _mat[3][2] + rhs._mat[3][2],
485  _mat[3][3] + rhs._mat[3][3]);
486  }
487 
491  inline Matrixf& operator += (const Matrixf& rhs)
492  {
493  _mat[0][0] += rhs._mat[0][0];
494  _mat[0][1] += rhs._mat[0][1];
495  _mat[0][2] += rhs._mat[0][2];
496  _mat[0][3] += rhs._mat[0][3];
497  _mat[1][0] += rhs._mat[1][0];
498  _mat[1][1] += rhs._mat[1][1];
499  _mat[1][2] += rhs._mat[1][2];
500  _mat[1][3] += rhs._mat[1][3];
501  _mat[2][0] += rhs._mat[2][0];
502  _mat[2][1] += rhs._mat[2][1];
503  _mat[2][2] += rhs._mat[2][2];
504  _mat[2][3] += rhs._mat[2][3];
505  _mat[3][0] += rhs._mat[3][0];
506  _mat[3][1] += rhs._mat[3][1];
507  _mat[3][2] += rhs._mat[3][2];
508  _mat[3][3] += rhs._mat[3][3];
509  return *this;
510  }
511 
512  protected:
513  value_type _mat[4][4];
514 
515 };
516 
517 class RefMatrixf : public Object, public Matrixf
518 {
519  public:
520 
521  RefMatrixf():Object(false), Matrixf() {}
522  RefMatrixf( const Matrixf& other) : Object(false), Matrixf(other) {}
523  RefMatrixf( const Matrixd& other) : Object(false), Matrixf(other) {}
524  RefMatrixf( const RefMatrixf& other) : Object(other), Matrixf(other) {}
525  explicit RefMatrixf( Matrixf::value_type const * const def ):Object(false), Matrixf(def) {}
530  Object(false),
531  Matrixf(a00, a01, a02, a03,
532  a10, a11, a12, a13,
533  a20, a21, a22, a23,
534  a30, a31, a32, a33) {}
535 
536  virtual Object* cloneType() const { return new RefMatrixf(); }
537  virtual Object* clone(const CopyOp&) const { return new RefMatrixf(*this); }
538  virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const RefMatrixf*>(obj)!=NULL; }
539  virtual const char* libraryName() const { return "osg"; }
540  virtual const char* className() const { return "Matrix"; }
541 
542 
543  protected:
544 
545  virtual ~RefMatrixf() {}
546 };
547 
548 
549 //static utility methods
551 {
552  Matrixf m;
553  m.makeIdentity();
554  return m;
555 }
556 
558 {
559  Matrixf m;
560  m.makeScale(sx,sy,sz);
561  return m;
562 }
563 
564 inline Matrixf Matrixf::scale(const Vec3f& v )
565 {
566  return scale(v.x(), v.y(), v.z() );
567 }
568 
569 inline Matrixf Matrixf::scale(const Vec3d& v )
570 {
571  return scale(v.x(), v.y(), v.z() );
572 }
573 
575 {
576  Matrixf m;
577  m.makeTranslate(tx,ty,tz);
578  return m;
579 }
580 
582 {
583  return translate(v.x(), v.y(), v.z() );
584 }
585 
587 {
588  return translate(v.x(), v.y(), v.z() );
589 }
590 
591 inline Matrixf Matrixf::rotate( const Quat& q )
592 {
593  return Matrixf(q);
594 }
596 {
597  Matrixf m;
598  m.makeRotate(angle,x,y,z);
599  return m;
600 }
601 inline Matrixf Matrixf::rotate(value_type angle, const Vec3f& axis )
602 {
603  Matrixf m;
604  m.makeRotate(angle,axis);
605  return m;
606 }
607 inline Matrixf Matrixf::rotate(value_type angle, const Vec3d& axis )
608 {
609  Matrixf m;
610  m.makeRotate(angle,axis);
611  return m;
612 }
613 inline Matrixf Matrixf::rotate( value_type angle1, const Vec3f& axis1,
614  value_type angle2, const Vec3f& axis2,
615  value_type angle3, const Vec3f& axis3)
616 {
617  Matrixf m;
618  m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
619  return m;
620 }
621 inline Matrixf Matrixf::rotate( value_type angle1, const Vec3d& axis1,
622  value_type angle2, const Vec3d& axis2,
623  value_type angle3, const Vec3d& axis3)
624 {
625  Matrixf m;
626  m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
627  return m;
628 }
629 inline Matrixf Matrixf::rotate(const Vec3f& from, const Vec3f& to )
630 {
631  Matrixf m;
632  m.makeRotate(from,to);
633  return m;
634 }
635 inline Matrixf Matrixf::rotate(const Vec3d& from, const Vec3d& to )
636 {
637  Matrixf m;
638  m.makeRotate(from,to);
639  return m;
640 }
641 
642 inline Matrixf Matrixf::inverse( const Matrixf& matrix)
643 {
644  Matrixf m;
645  m.invert(matrix);
646  return m;
647 }
648 
649 inline Matrixf Matrixf::orthoNormal(const Matrixf& matrix)
650 {
651  Matrixf m;
652  m.orthoNormalize(matrix);
653  return m;
654 }
655 
656 inline Matrixf Matrixf::ortho(double left, double right,
657  double bottom, double top,
658  double zNear, double zFar)
659 {
660  Matrixf m;
661  m.makeOrtho(left,right,bottom,top,zNear,zFar);
662  return m;
663 }
664 
665 inline Matrixf Matrixf::ortho2D(double left, double right,
666  double bottom, double top)
667 {
668  Matrixf m;
669  m.makeOrtho2D(left,right,bottom,top);
670  return m;
671 }
672 
673 inline Matrixf Matrixf::frustum(double left, double right,
674  double bottom, double top,
675  double zNear, double zFar)
676 {
677  Matrixf m;
678  m.makeFrustum(left,right,bottom,top,zNear,zFar);
679  return m;
680 }
681 
682 inline Matrixf Matrixf::perspective(double fovy,double aspectRatio,
683  double zNear, double zFar)
684 {
685  Matrixf m;
686  m.makePerspective(fovy,aspectRatio,zNear,zFar);
687  return m;
688 }
689 
690 inline Matrixf Matrixf::lookAt(const Vec3f& eye,const Vec3f& center,const Vec3f& up)
691 {
692  Matrixf m;
693  m.makeLookAt(eye,center,up);
694  return m;
695 }
696 
697 inline Matrixf Matrixf::lookAt(const Vec3d& eye,const Vec3d& center,const Vec3d& up)
698 {
699  Matrixf m;
700  m.makeLookAt(eye,center,up);
701  return m;
702 }
703 
704 inline Vec3f Matrixf::postMult( const Vec3f& v ) const
705 {
706  value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
707  return Vec3f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
708  (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
709  (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
710 }
711 inline Vec3d Matrixf::postMult( const Vec3d& v ) const
712 {
713  value_type d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
714  return Vec3d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
715  (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
716  (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
717 }
718 
719 inline Vec3f Matrixf::preMult( const Vec3f& v ) const
720 {
721  value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
722  return Vec3f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
723  (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
724  (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
725 }
726 inline Vec3d Matrixf::preMult( const Vec3d& v ) const
727 {
728  value_type d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
729  return Vec3d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
730  (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
731  (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
732 }
733 
734 inline Vec4f Matrixf::postMult( const Vec4f& v ) const
735 {
736  return Vec4f( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
737  (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
738  (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
739  (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
740 }
741 inline Vec4d Matrixf::postMult( const Vec4d& v ) const
742 {
743  return Vec4d( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
744  (_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
745  (_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
746  (_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
747 }
748 
749 inline Vec4f Matrixf::preMult( const Vec4f& v ) const
750 {
751  return Vec4f( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
752  (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
753  (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
754  (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
755 }
756 inline Vec4d Matrixf::preMult( const Vec4d& v ) const
757 {
758  return Vec4d( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
759  (_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
760  (_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
761  (_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
762 }
763 inline Vec3f Matrixf::transform3x3(const Vec3f& v,const Matrixf& m)
764 {
765  return Vec3f( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
766  (m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
767  (m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
768 }
769 inline Vec3d Matrixf::transform3x3(const Vec3d& v,const Matrixf& m)
770 {
771  return Vec3d( (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()));
774 }
775 
776 inline Vec3f Matrixf::transform3x3(const Matrixf& m,const Vec3f& v)
777 {
778  return Vec3f( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
779  (m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
780  (m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
781 }
782 inline Vec3d Matrixf::transform3x3(const Matrixf& m,const Vec3d& v)
783 {
784  return Vec3d( (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()) ) ;
787 }
788 
789 inline void Matrixf::preMultTranslate( const Vec3d& v )
790 {
791  for (unsigned i = 0; i < 3; ++i)
792  {
793  double tmp = v[i];
794  if (tmp == 0)
795  continue;
796  _mat[3][0] += tmp*_mat[i][0];
797  _mat[3][1] += tmp*_mat[i][1];
798  _mat[3][2] += tmp*_mat[i][2];
799  _mat[3][3] += tmp*_mat[i][3];
800  }
801 }
802 
803 inline void Matrixf::preMultTranslate( const Vec3f& v )
804 {
805  for (unsigned i = 0; i < 3; ++i)
806  {
807  float tmp = v[i];
808  if (tmp == 0)
809  continue;
810  _mat[3][0] += tmp*_mat[i][0];
811  _mat[3][1] += tmp*_mat[i][1];
812  _mat[3][2] += tmp*_mat[i][2];
813  _mat[3][3] += tmp*_mat[i][3];
814  }
815 }
816 
817 inline void Matrixf::postMultTranslate( const Vec3d& v )
818 {
819  for (unsigned i = 0; i < 3; ++i)
820  {
821  double tmp = v[i];
822  if (tmp == 0)
823  continue;
824  _mat[0][i] += tmp*_mat[0][3];
825  _mat[1][i] += tmp*_mat[1][3];
826  _mat[2][i] += tmp*_mat[2][3];
827  _mat[3][i] += tmp*_mat[3][3];
828  }
829 }
830 
831 inline void Matrixf::postMultTranslate( const Vec3f& v )
832 {
833  for (unsigned i = 0; i < 3; ++i)
834  {
835  float tmp = v[i];
836  if (tmp == 0)
837  continue;
838  _mat[0][i] += tmp*_mat[0][3];
839  _mat[1][i] += tmp*_mat[1][3];
840  _mat[2][i] += tmp*_mat[2][3];
841  _mat[3][i] += tmp*_mat[3][3];
842  }
843 }
844 
845 inline void Matrixf::preMultScale( const Vec3d& v )
846 {
847  _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
848  _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
849  _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
850 }
851 
852 inline void Matrixf::preMultScale( const Vec3f& v )
853 {
854  _mat[0][0] *= v[0]; _mat[0][1] *= v[0]; _mat[0][2] *= v[0]; _mat[0][3] *= v[0];
855  _mat[1][0] *= v[1]; _mat[1][1] *= v[1]; _mat[1][2] *= v[1]; _mat[1][3] *= v[1];
856  _mat[2][0] *= v[2]; _mat[2][1] *= v[2]; _mat[2][2] *= v[2]; _mat[2][3] *= v[2];
857 }
858 
859 inline void Matrixf::postMultScale( const Vec3d& v )
860 {
861  _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
862  _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
863  _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
864 }
865 
866 inline void Matrixf::postMultScale( const Vec3f& v )
867 {
868  _mat[0][0] *= v[0]; _mat[1][0] *= v[0]; _mat[2][0] *= v[0]; _mat[3][0] *= v[0];
869  _mat[0][1] *= v[1]; _mat[1][1] *= v[1]; _mat[2][1] *= v[1]; _mat[3][1] *= v[1];
870  _mat[0][2] *= v[2]; _mat[1][2] *= v[2]; _mat[2][2] *= v[2]; _mat[3][2] *= v[2];
871 }
872 
873 
874 inline void Matrixf::preMultRotate( const Quat& q )
875 {
876  if (q.zeroRotation())
877  return;
878  Matrixf r;
879  r.setRotate(q);
880  preMult(r);
881 }
882 
883 inline void Matrixf::postMultRotate( const Quat& q )
884 {
885  if (q.zeroRotation())
886  return;
887  Matrixf r;
888  r.setRotate(q);
889  postMult(r);
890 }
891 
892 inline Vec3f operator* (const Vec3f& v, const Matrixf& m )
893 {
894  return m.preMult(v);
895 }
896 inline Vec3d operator* (const Vec3d& v, const Matrixf& m )
897 {
898  return m.preMult(v);
899 }
900 inline Vec4f operator* (const Vec4f& v, const Matrixf& m )
901 {
902  return m.preMult(v);
903 }
904 inline Vec4d operator* (const Vec4d& v, const Matrixf& m )
905 {
906  return m.preMult(v);
907 }
908 
909 inline Vec3f Matrixf::operator* (const Vec3f& v) const
910 {
911  return postMult(v);
912 }
913 inline Vec3d Matrixf::operator* (const Vec3d& v) const
914 {
915  return postMult(v);
916 }
917 inline Vec4f Matrixf::operator* (const Vec4f& v) const
918 {
919  return postMult(v);
920 }
921 inline Vec4d Matrixf::operator* (const Vec4d& v) const
922 {
923  return postMult(v);
924 }
925 
926 
927 } //namespace osg
928 
929 
930 #endif
value_type & x()
Definition: Vec3d.h:85
#define OSG_EXPORT
Definition: Export.h:43
void makeIdentity()
virtual const char * className() const
Definition: Matrixf.h:540
bool zeroRotation() const
Definition: Quat.h:165
#define NULL
Definition: Export.h:59
static Matrixf rotate(const Vec3f &from, const Vec3f &to)
Definition: Matrixf.h:629
value_type & y()
Definition: Vec4d.h:91
void makeOrtho2D(double left, double right, double bottom, double top)
Definition: Matrixf.h:164
static Matrixf ortho2D(double left, double right, double bottom, double top)
Definition: Matrixf.h:665
Vec3d getTrans() const
Definition: Matrixf.h:342
void preMultTranslate(const Vec3d &v)
Definition: Matrixf.h:789
value_type & x()
Definition: Vec3f.h:80
value_type & z()
Definition: Vec3d.h:87
value_type & z()
Definition: Vec3f.h:82
void makeTranslate(const Vec3f &)
void postMultTranslate(const Vec3d &v)
Definition: Matrixf.h:817
Vec3f postMult(const Vec3f &v) const
Definition: Matrixf.h:704
static Matrixf identity(void)
Definition: Matrixf.h:550
Vec3f operator*(const Vec3f &v) const
Definition: Matrixf.h:909
virtual Object * cloneType() const
Definition: Matrixf.h:536
static Vec3f transform3x3(const Vec3f &v, const Matrixf &m)
Definition: Matrixf.h:763
void postMultScale(const Vec3d &v)
Definition: Matrixf.h:859
RefMatrixf(const Matrixd &other)
Definition: Matrixf.h:523
value_type & x()
Definition: Vec4d.h:90
void set(float const *const ptr)
Definition: Matrixf.h:75
value_type * ptr()
Definition: Matrixf.h:92
value_type & y()
Definition: Vec4f.h:88
RefMatrixf(const Matrixf &other)
Definition: Matrixf.h:522
bool invert(const Matrixf &rhs)
Definition: Matrixf.h:233
Matrixf(float const *const ptr)
Definition: Matrixf.h:36
double other_value_type
Definition: Matrixf.h:31
bool isNaN(float v)
Definition: Math.h:114
value_type _mat[4][4]
Definition: Matrixf.h:513
void orthoNormalize(const Matrixf &rhs)
void set(const Matrixf &rhs)
Definition: Matrixf.h:71
void preMultScale(const Vec3d &v)
Definition: Matrixf.h:845
bool valid() const
Definition: Matrixf.h:56
void makePerspective(double fovy, double aspectRatio, double zNear, double zFar)
static Matrixf scale(const Vec3f &sv)
Definition: Matrixf.h:564
Vec3d getScale() const
Definition: Matrixf.h:344
value_type operator()(int row, int col) const
Definition: Matrixf.h:54
void makeFrustum(double left, double right, double bottom, double top, double zNear, double zFar)
value_type & z()
Definition: Vec4d.h:92
void makeOrtho(double left, double right, double bottom, double top, double zNear, double zFar)
virtual ~RefMatrixf()
Definition: Matrixf.h:545
Matrixf(const Matrixf &mat)
Definition: Matrixf.h:34
Vec3f preMult(const Vec3f &v) const
Definition: Matrixf.h:719
value_type & y()
Definition: Vec3f.h:81
static Matrixf translate(const Vec3f &dv)
Definition: Matrixf.h:581
static Matrixf frustum(double left, double right, double bottom, double top, double zNear, double zFar)
Definition: Matrixf.h:673
Matrixf(const Quat &quat)
Definition: Matrixf.h:38
const value_type * ptr() const
Definition: Matrixf.h:93
static Matrixf lookAt(const Vec3f &eye, const Vec3f &center, const Vec3f &up)
Definition: Matrixf.h:690
virtual const char * libraryName() const
Definition: Matrixf.h:539
void setRotate(const Quat &q)
Vec3f operator*(const Vec3f &v, const Matrixd &m)
Definition: Matrixd.h:787
virtual Object * clone(const CopyOp &) const
Definition: Matrixf.h:537
static Matrixf ortho(double left, double right, double bottom, double top, double zNear, double zFar)
Definition: Matrixf.h:656
value_type & w()
Definition: Vec4f.h:90
Definition: AlphaFunc.h:19
RefMatrixf(const RefMatrixf &other)
Definition: Matrixf.h:524
void set(double const *const ptr)
Definition: Matrixf.h:81
void postMultRotate(const Quat &q)
Definition: Matrixf.h:883
float value_type
Definition: Matrixf.h:30
value_type & w()
Definition: Vec4d.h:93
value_type & z()
Definition: Vec4f.h:89
value_type & y()
Definition: Vec3d.h:86
value_type & x()
Definition: Vec4f.h:87
Matrixf(double const *const ptr)
Definition: Matrixf.h:37
value_type length() const
Definition: Vec3d.h:181
virtual bool isSameKindAs(const Object *obj) const
Definition: Matrixf.h:538
Definition: Quat.h:29
RefMatrixf(Matrixf::value_type a00, Matrixf::value_type a01, Matrixf::value_type a02, Matrixf::value_type a03, Matrixf::value_type a10, Matrixf::value_type a11, Matrixf::value_type a12, Matrixf::value_type a13, Matrixf::value_type a20, Matrixf::value_type a21, Matrixf::value_type a22, Matrixf::value_type a23, Matrixf::value_type a30, Matrixf::value_type a31, Matrixf::value_type a32, Matrixf::value_type a33)
Definition: Matrixf.h:526
void makeLookAt(const Vec3d &eye, const Vec3d &center, const Vec3d &up)
void mult(const Matrixf &, const Matrixf &)
RefMatrixf(Matrixf::value_type const *const def)
Definition: Matrixf.h:525
void preMultRotate(const Quat &q)
Definition: Matrixf.h:874
static Matrixf orthoNormal(const Matrixf &matrix)
Definition: Matrixf.h:649
bool isIdentity() const
Definition: Matrixf.h:95
value_type & operator()(int row, int col)
Definition: Matrixf.h:53
static Matrixf perspective(double fovy, double aspectRatio, double zNear, double zFar)
Definition: Matrixf.h:682
void makeRotate(const Vec3f &from, const Vec3f &to)
void makeScale(const Vec3f &)
static Matrixf inverse(const Matrixf &matrix)
Definition: Matrixf.h:642
bool isNaN() const
Definition: Matrixf.h:57