00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00029 #ifndef _MATVEC_H_
00030 #include <math.h>
00031 #include <string.h>
00032 #include <iostream>
00033 #include <stack>
00034
00035 #include <Inventor/SbLinear.h>
00036 #include <Inventor/nodes/SoTransform.h>
00037
00038 class vec3;
00039 class position;
00040 class mat3;
00041 class Quaternion;
00042 class transf;
00043
00044 #define MACHINE_ZERO 1.0e-9
00045 #define resabs 1.0e-5
00046
00047 typedef double col_Mat4[4][4];
00048
00050
00053 class vec3 {
00054
00056 double vec[3];
00057
00058 public:
00060 vec3() {}
00061
00063 vec3(const vec3& v) {set(v);}
00064
00066 vec3(double v[3]) {set(v);}
00067
00069 vec3(const SbVec3f& v) {set(v);}
00070
00072 vec3(double x,double y,double z) {set(x,y,z);}
00073
00075 const double& operator[](int i) const {return vec[i];}
00076
00078 double& operator[](int i) {return vec[i];}
00079
00081 void set(const vec3& v) {vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2];}
00082
00084 void set(double v[3]) {vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2];}
00085
00087 void set(double x,double y,double z) {vec[0] = x; vec[1] = y; vec[2] = z;}
00088
00089 inline double len() const;
00090 inline double len_sq() const;
00091
00092 inline void set(const SbVec3f &v);
00093 inline void toSbVec3f(SbVec3f &v) const;
00094 inline SbVec3f toSbVec3f() const;
00095
00096
00097
00098 inline vec3 const& operator=(vec3 const& v);
00099 inline vec3 const& operator*=(double s);
00100
00101
00102 inline vec3 const& operator+=(vec3 const& v);
00103 inline vec3 const& operator-=(vec3 const& v);
00104 inline vec3 const& operator/=(double s);
00105 inline bool operator==(vec3 const& v) const;
00106 inline bool operator<(vec3 const& v) const;
00107
00109 double x() const {return vec[0];}
00110
00112 double y() const {return vec[1];}
00113
00115 double z() const {return vec[2];}
00116
00118 double& x() {return vec[0];}
00119
00121 double& y() {return vec[1];}
00122
00124 double& z() {return vec[2];}
00125
00126
00127 void perpVectors(vec3 &v1,vec3 &v2);
00128
00129
00130
00131
00132 friend inline double operator%(vec3 const& v1, vec3 const& v2);
00133 friend inline double operator%(position const& p, vec3 const& v);
00134 friend inline double operator%(vec3 const& v, position const& p);
00135 friend inline vec3 operator*(double s, vec3 const& v);
00136 friend inline vec3 operator*(vec3 const& v, double s);
00137 friend inline vec3 operator*(mat3 const& m, vec3 const& v);
00138
00139 friend inline vec3 operator*(vec3 const& v, transf const& tr);
00140 friend inline vec3 operator>(vec3 const& v, transf const& tr);
00141 friend inline vec3 operator*(vec3 const& v1, vec3 const& v2);
00142 friend inline vec3 operator+(vec3 const& v1, vec3 const& v2);
00143 friend inline vec3 operator-(vec3 const& v1, vec3 const& v2);
00144 friend inline vec3 operator-(vec3 const& v);
00145 friend inline vec3 operator/(vec3 const& v, double s);
00146 friend inline vec3 normalise(vec3 const& v);
00147
00148 friend std::istream &operator>>(std::istream &is, vec3 &v);
00149 friend std::ostream &operator<<(std::ostream &os, const vec3 &v);
00150
00152 static const vec3 ZERO;
00153
00155 static const vec3 X;
00156
00158 static const vec3 Y;
00159
00161 static const vec3 Z;
00162 };
00163
00164
00166
00169 class position {
00170
00172 double pos[3];
00173
00174 public:
00175
00177 position() {}
00178
00180 position(const position& p) {set(p);}
00181
00183 position(double p[3]) {set(p);}
00184
00186 position(const SbVec3f& p) {set(p);}
00187
00189 position(double x,double y,double z) {set(x,y,z);}
00190
00192 const double& operator[](int i) const {return pos[i];}
00193
00195 double& operator[](int i) {return pos[i];}
00196
00198 void set(const position& p) {pos[0] = p[0]; pos[1] = p[1]; pos[2] = p[2];}
00199
00201 void set(double p[3]) {pos[0] = p[0]; pos[1] = p[1]; pos[2] = p[2];}
00202
00204 void set(double x,double y,double z) {pos[0] = x; pos[1] = y; pos[2] = z;}
00205
00206 void set(const vec3 &v){pos[0] = v.x(); pos[1] = v.y(); pos[2] = v.z();}
00207
00209 void get(double p[3]) const {p[0] = pos[0]; p[1] = pos[1]; p[2] = pos[2];}
00210
00211 inline void set(const SbVec3f &v);
00212 inline void toSbVec3f(SbVec3f &v) const;
00213 inline SbVec3f toSbVec3f() const;
00214
00215
00216
00217 inline position const& operator=(position const& p);
00218
00219
00220 inline position const& operator+=(vec3 const& v);
00221 inline position const& operator-=(vec3 const& v);
00222 inline bool operator==(position const& p) const;
00223
00225 double x() const {return pos[0];}
00226
00228 double y() const {return pos[1];}
00229
00231 double z() const {return pos[2];}
00232
00234 double& x() {return pos[0];}
00235
00237 double& y() {return pos[1];}
00238
00240 double& z() {return pos[2];}
00241
00242 friend inline double operator%(position const& p, vec3 const& v);
00243 friend inline double operator%(vec3 const& v, position const& p);
00244
00245 inline friend position operator*(double d, position const& p);
00246 inline friend position operator*(position const& p, transf const& tr);
00247 friend inline position operator+(position const& p, vec3 const& v);
00248 friend inline position operator-(position const& p, vec3 const& v);
00249 friend inline position operator+(vec3 const& v, position const& p);
00250 friend inline vec3 operator-(position const& p1, position const& p2);
00251 friend inline position operator+(position const& p1, position const& p2);
00252
00253 friend std::istream& operator>>(std::istream &is, position &p);
00254 friend std::ostream& operator<<(std::ostream &os, const position &p);
00255
00257 static const position ORIGIN;
00258 };
00259
00260
00262
00265 class mat3 {
00266
00268 double mat[9];
00269
00270 public:
00271
00273 mat3() {}
00274
00276 mat3(const double M[9]) {set(M);}
00277
00279 mat3(const Quaternion &q) {set(q);}
00280
00283 mat3(const vec3 &v1, const vec3 &v2, const vec3 &v3) {set(v1,v2,v3);}
00284
00286 void set(const double M[9]) {memcpy(mat,M,sizeof(double)*9);}
00287
00288 inline void set(const Quaternion &q);
00289 inline void set(const vec3 &v1, const vec3 &v2, const vec3 &v3);
00290
00291 inline void setCrossProductMatrix(const vec3 &v);
00292
00293 void ToEulerAngles(double &roll,double &pitch, double &yaw) const;
00294
00296 const double& element(int i,int j) const {return mat[j*3+i];}
00297
00299 double& element(int i,int j) {return mat[j*3+i];}
00300
00303 const double& operator[](int i) const {return mat[i];}
00306 double& operator[](int i) {return mat[i];}
00307
00309 vec3 row(int i) const {return vec3(mat[i],mat[i+3],mat[i+6]);}
00310
00311 inline double determinant() const;
00312
00313 inline mat3 transpose() const;
00314 mat3 inverse() const;
00315
00316 mat3 const& operator*=(mat3 const& m);
00317 friend vec3 operator*(mat3 const& m, vec3 const& v);
00318
00319 friend vec3 operator*(vec3 const& v, mat3 const& m);
00320 friend mat3 operator*(mat3 const& m1, mat3 const& m2);
00321 inline friend mat3 operator*(double const& s, mat3 const& m);
00322 inline mat3 const& operator+=(mat3 const& v);
00323
00324 friend std::istream& operator>>(std::istream &is, mat3 &m);
00325 friend std::ostream& operator<<(std::ostream &os, const mat3 &m);
00326
00327
00329 static const mat3 ZERO;
00330
00332 static const mat3 IDENTITY;
00333 };
00334
00336
00339 class Quaternion {
00340 public:
00342 double w, x, y, z;
00343
00345 Quaternion () : w(1.0),x(0.0),y(0.0),z(0.0) {}
00346
00348 Quaternion (double W, double X, double Y, double Z) {set(W,X,Y,Z);}
00349
00351 Quaternion (const mat3& R) {set(R);}
00352
00355 Quaternion (const double& angle, const vec3 &axis) {set(angle,axis);}
00356
00358 void set(const Quaternion& Q)
00359 {w = Q.w; x = Q.x; y = Q.y; z = Q.z;}
00360
00362 void set(double W, double X, double Y, double Z)
00363 {w = W; x = X; y = Y; z = Z; normalise();}
00364
00365 void set(const mat3& R);
00366 void set(const double& angle, const vec3 &axis);
00367 void set(const SbRotation &SbRot);
00368
00369 void ToRotationMatrix (mat3& m) const;
00370 void ToAngleAxis (double& angle, vec3& axis) const;
00371 inline SbRotation toSbRotation() const;
00372
00373
00374 inline Quaternion& operator= (const Quaternion& q);
00375 inline Quaternion operator+ (const Quaternion& q) const;
00376 inline Quaternion operator- (const Quaternion& q) const;
00377 inline Quaternion operator* (const Quaternion& q) const;
00378
00379 inline Quaternion operator- () const;
00380 inline double operator% (const Quaternion& q) const;
00381
00382 inline bool operator==(Quaternion const& q) const;
00383
00384
00385 inline double norm () const;
00386 inline Quaternion inverse () const;
00387
00388
00389 inline vec3 operator* (const vec3& v) const;
00390
00391
00392 inline position operator* (const position& pt) const;
00393
00394
00395 static Quaternion Slerp (double t, const Quaternion& p,
00396 const Quaternion& q);
00397
00398
00399 static void Intermediate (const Quaternion& q0, const Quaternion& q1,
00400 const Quaternion& q2, Quaternion& a, Quaternion& b);
00401
00402
00403 static Quaternion Squad (double t, const Quaternion& p,
00404 const Quaternion& a, const Quaternion& b, const Quaternion& q);
00405
00406 inline void normalise();
00407
00408 friend std::istream& operator>>(std::istream &is, Quaternion &q);
00409 friend std::ostream& operator<<(std::ostream &os, const Quaternion &q);
00410
00412 static const Quaternion IDENTITY;
00413
00414 };
00415
00417
00422 class transf {
00423
00425 mat3 R;
00426
00428 vec3 t;
00429
00431 Quaternion rot;
00432
00433 public:
00434
00436 transf() {R = mat3::IDENTITY; t = vec3::ZERO; rot = Quaternion::IDENTITY;}
00437
00439 transf(const Quaternion& r, const vec3& d) {set(r,d);}
00440
00442 transf(const mat3& r, const vec3& d) {set(r,d);}
00443
00445 transf(const SoTransform *IVt) {set(IVt);}
00446
00449 void set(const Quaternion& r, const vec3& d) {
00450 rot = r; t = d; rot.ToRotationMatrix(R);
00451 }
00452
00455 void set(const mat3& r, const vec3& d) {rot.set(r); t = d; R = r;}
00456
00457 inline void set(const SoTransform *IVt);
00458
00460 const Quaternion& rotation() const {return rot;}
00461
00463 const mat3& affine() const {return R;}
00464
00466 const vec3& translation() const {return t;}
00467
00468 inline void toSoTransform(SoTransform *IVt) const;
00469 inline void tocol_Mat4(col_Mat4 colTran) const;
00470 inline void toColMajorMatrix(double t[][4]) const;
00471 inline void toRowMajorMatrix(double t[][4]) const;
00473 void jacobian(double jac[]);
00474
00475 inline transf inverse() const;
00476
00477 inline transf const& operator=(transf const& tr);
00478 inline bool operator==(transf const& tr) const;
00479
00481 inline bool operator!=(transf const& tr) const {return !operator==(tr);}
00482
00483
00484
00485
00486 inline friend transf operator*(transf const& tr1, transf const& tr2);
00487 inline friend vec3 operator*(vec3 const& v, transf const& tr);
00488 inline friend vec3 operator>(vec3 const& v, transf const& tr);
00489 inline friend position operator*(position const& p, transf const& tr);
00490
00491
00492 inline friend transf translate_transf(const vec3& v);
00493 inline friend transf rotate_transf(double angle, const vec3& axis);
00494 inline friend transf coordinate_transf(const position& origin,
00495 const vec3& xaxis, const vec3& yaxis);
00496 inline friend transf rotXYZ(double, double, double);
00497
00498 friend std::istream& operator>>(std::istream &is, transf &tr);
00499 friend std::ostream& operator<<(std::ostream &os, const transf &tr);
00500
00502 static const transf IDENTITY;
00503 };
00504
00506
00523 class FlockTransf{
00524 private:
00525 transf mount, mountInv;
00526 transf flockBaseInv, objBase;
00527
00528 public:
00529 void identity();
00531 void setMount(transf t){mount = t; mountInv = t.inverse();}
00533 void setFlockBase(transf t){flockBaseInv = t.inverse();}
00535 void setObjectBase(transf t){objBase = t;}
00537 transf get(transf t) const;
00539 transf getAbsolute(transf t) const;
00541 transf get2(transf t) const;
00542 transf getMount() const {return mount;}
00543 };
00544
00546
00548
00552 void
00553 vec3::set(const SbVec3f &v)
00554 {
00555 vec[0] = (double) v[0];
00556 vec[1] = (double) v[1];
00557 vec[2] = (double) v[2];
00558 }
00559
00564 void
00565 vec3::toSbVec3f(SbVec3f &v) const
00566 {
00567 v[0] = (float) vec[0];
00568 v[1] = (float) vec[1];
00569 v[2] = (float) vec[2];
00570 }
00571
00575 SbVec3f
00576 vec3::toSbVec3f() const
00577 {
00578 return SbVec3f((float) vec[0],(float) vec[1],(float) vec[2]);
00579 }
00580
00584 double
00585 operator%(vec3 const& v1, vec3 const& v2)
00586 {
00587 return v1.vec[0]*v2.vec[0] + v1.vec[1]*v2.vec[1] + v1.vec[2]*v2.vec[2];
00588 }
00589
00593 vec3
00594 operator*(double s, vec3 const& v)
00595 {
00596 return vec3(s*v.vec[0],s*v.vec[1],s*v.vec[2]);
00597 }
00598
00602 vec3
00603 operator*(vec3 const& v, double s)
00604 {
00605 return vec3(s*v.vec[0],s*v.vec[1],s*v.vec[2]);
00606 }
00607
00611 double
00612 vec3::len() const
00613 {
00614 return sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
00615 }
00616
00620 double
00621 vec3::len_sq() const
00622 {
00623 return vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2];
00624 }
00625
00627 vec3 const&
00628 vec3::operator=(vec3 const& v)
00629 {
00630 vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2];
00631 return *this;
00632 }
00633
00635 vec3 const&
00636 vec3::operator*=(double s)
00637 {
00638 vec[0] *= s; vec[1] *= s; vec[2] *= s;
00639 return *this;
00640 }
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00658 vec3 const&
00659 vec3::operator+=(vec3 const& v)
00660 {
00661 vec[0] += v[0]; vec[1] += v[1]; vec[2] += v[2];
00662 return *this;
00663 }
00664
00666 vec3 const&
00667 vec3::operator-=(vec3 const& v)
00668 {
00669 vec[0] -= v[0]; vec[1] -= v[1]; vec[2] -= v[2];
00670 return *this;
00671 }
00672
00674 vec3 const&
00675 vec3::operator/=(double s)
00676 {
00677 vec[0] /= s; vec[1] /= s; vec[2] /= s;
00678 return *this;
00679 }
00680
00682 bool
00683 vec3::operator==(vec3 const& v) const
00684 {
00685 if ((*this - v).len_sq() < resabs * resabs)
00686 return true;
00687 return false;
00688 }
00689
00694 bool
00695 vec3::operator<(vec3 const& v) const
00696 {
00697 double v0diff,v1diff,v2diff;
00698 v2diff = vec[2]-v[2];
00699 if (v2diff < -resabs) return true;
00700 else if (v2diff < resabs) {
00701 v1diff = vec[1]-v[1];
00702 if (v1diff < -resabs) return true;
00703 else if (v1diff < resabs) {
00704 v0diff = vec[0]-v[0];
00705 if (v0diff < -resabs) return true;
00706 }
00707 }
00708 return false;
00709 }
00710
00712 double
00713 operator%(position const& p, vec3 const& v)
00714 {
00715 return p.pos[0]*v.vec[0] + p.pos[1]*v.vec[1] + p.pos[2]*v.vec[2];
00716 }
00717
00719 double
00720 operator%(vec3 const& v, position const& p)
00721 {
00722 return p.pos[0]*v.vec[0] + p.pos[1]*v.vec[1] + p.pos[2]*v.vec[2];
00723 }
00724
00725
00726 vec3
00727 operator*(const mat3 &m, const vec3 &v)
00728 {
00729 double result[3];
00730 result[0] = m[0] * v.vec[0] + m[3] * v.vec[1] + m[6] * v.vec[2];
00731 result[1] = m[1] * v.vec[0] + m[4] * v.vec[1] + m[7] * v.vec[2];
00732 result[2] = m[2] * v.vec[0] + m[5] * v.vec[1] + m[8] * v.vec[2];
00733 return vec3(result);
00734 }
00735
00736
00738 vec3
00739 operator*(vec3 const& v1, vec3 const& v2)
00740 {
00741 return vec3(v1.vec[1]*v2.vec[2]-v1.vec[2]*v2.vec[1],
00742 v1.vec[2]*v2.vec[0]-v1.vec[0]*v2.vec[2],
00743 v1.vec[0]*v2.vec[1]-v1.vec[1]*v2.vec[0]);
00744 }
00745
00747 vec3
00748 operator+(vec3 const& v1, vec3 const& v2)
00749 {
00750 return vec3(v1.vec[0]+v2.vec[0],v1.vec[1]+v2.vec[1],v1.vec[2]+v2.vec[2]);
00751 }
00752
00753
00755 vec3
00756 operator-(vec3 const& v1, vec3 const& v2)
00757 {
00758 return vec3(v1.vec[0]-v2.vec[0],v1.vec[1]-v2.vec[1],v1.vec[2]-v2.vec[2]);
00759 }
00760
00761
00763 vec3
00764 operator-(vec3 const& v)
00765 {
00766 return vec3(-v.vec[0],-v.vec[1],-v.vec[2]);
00767 }
00768
00769
00771 vec3
00772 operator/(vec3 const& v, double s)
00773 {
00774 return vec3(v.vec[0]/s,v.vec[1]/s,v.vec[2]/s);
00775 }
00776
00778 vec3
00779 normalise(vec3 const& v)
00780 {
00781 double length = v.len();
00782 #ifdef GRASPITDBG
00783 if (length == 0.0)
00784 printf("0 length in vec3 normalise\n");
00785 #endif
00786 return v/length;
00787 }
00788
00789
00790
00791
00793
00795
00799 void
00800 position::set(const SbVec3f &v)
00801 {
00802 pos[0] = (double) v[0];
00803 pos[1] = (double) v[1];
00804 pos[2] = (double) v[2];
00805 }
00806
00811 void
00812 position::toSbVec3f(SbVec3f &v) const
00813 {
00814 v[0] = (float) pos[0];
00815 v[1] = (float) pos[1];
00816 v[2] = (float) pos[2];
00817 }
00818
00822 SbVec3f
00823 position::toSbVec3f() const
00824 {
00825 return SbVec3f((float) pos[0],(float) pos[1],(float) pos[2]);
00826 }
00827
00829 position const&
00830 position::operator=(position const& p)
00831 {
00832 pos[0] = p[0]; pos[1] = p[1]; pos[2] = p[2];
00833 return *this;
00834 }
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00852 position const&
00853 position::operator+=(vec3 const& v)
00854 {
00855 pos[0] += v[0]; pos[1] += v[1]; pos[2] += v[2];
00856 return *this;
00857 }
00858
00860 position const&
00861 position::operator-=(vec3 const& v)
00862 {
00863 pos[0] -= v[0]; pos[1] -= v[1]; pos[2] -= v[2];
00864 return *this;
00865 }
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00880 position
00881 operator+(position const& p, vec3 const& v)
00882 {
00883 return position(p[0]+v[0], p[1]+v[1], p[2]+v[2]);
00884 }
00885
00887 position
00888 operator-(position const& p, vec3 const& v)
00889 {
00890 return position(p[0]-v[0], p[1]-v[1], p[2]-v[2]);
00891 }
00892
00894 position
00895 operator+(vec3 const& v, position const& p)
00896 {
00897 return position(p[0]+v[0], p[1]+v[1], p[2]+v[2]);
00898 }
00899
00901 vec3
00902 operator-(position const& p1, position const& p2)
00903 {
00904 return vec3(p1[0]-p2[0], p1[1]-p2[1], p1[2]-p2[2]);
00905 }
00906
00907 position
00908 operator+(position const& p1, position const& p2)
00909 {
00910 return position(p1[0]+p2[0], p1[1]+p2[1], p1[2]+p2[2]);
00911 }
00912
00913 position
00914 operator*(double d, position const& p)
00915 {
00916 return position(d*p[0], d*p[1], d*p[2]);
00917 }
00918
00922 bool
00923 position::operator==(position const& p) const
00924 {
00925 if ((*this - p).len_sq() < resabs * resabs)
00926 return true;
00927 return false;
00928 }
00929
00931
00933
00934
00938 void
00939 mat3::set(const Quaternion &q)
00940 {
00941 double tx = 2.0*q.x;
00942 double ty = 2.0*q.y;
00943 double tz = 2.0*q.z;
00944 double twx = tx*q.w;
00945 double twy = ty*q.w;
00946 double twz = tz*q.w;
00947 double txx = tx*q.x;
00948 double txy = ty*q.x;
00949 double txz = tz*q.x;
00950 double tyy = ty*q.y;
00951 double tyz = tz*q.y;
00952 double tzz = tz*q.z;
00953
00954 mat[0] = 1.0-(tyy+tzz); mat[1] = txy-twz; mat[2] = txz+twy;
00955 mat[3] = txy+twz; mat[4] = 1.0-(txx+tzz); mat[5] = tyz-twx;
00956 mat[6] = txz-twy; mat[7] = tyz+twx; mat[8] = 1.0-(txx+tyy);
00957 }
00958
00960 void
00961 mat3::set(const vec3 &v1, const vec3 &v2, const vec3 &v3)
00962 {
00963 mat[0] = v1[0]; mat[3] = v1[1]; mat[6] = v1[2];
00964 mat[1] = v2[0]; mat[4] = v2[1]; mat[7] = v2[2];
00965 mat[2] = v3[0]; mat[5] = v3[1]; mat[8] = v3[2];
00966 }
00967
00971 void
00972 mat3::setCrossProductMatrix(const vec3 &v)
00973 {
00974 mat[0]=0.0;
00975 mat[1]= v.z();
00976 mat[2]=-v.y();
00977
00978 mat[3]=-v.z();
00979 mat[4]=0.0;
00980 mat[5]= v.x();
00981
00982 mat[6]= v.y();
00983 mat[7]=-v.x();
00984 mat[8]=0.0;
00985
00986 }
00987
00989 double
00990 mat3::determinant() const
00991 {
00992 return mat[0] * (mat[4] * mat[8] - mat[7] * mat[5])
00993 + mat[3] * (mat[7] * mat[2] - mat[1] * mat[8])
00994 + mat[6] * (mat[1] * mat[5] - mat[4] * mat[2]);
00995 }
00996
00998 mat3
00999 mat3::transpose() const
01000 {
01001 double M[9];
01002
01003 M[0] = mat[0];
01004 M[1] = mat[3];
01005 M[2] = mat[6];
01006
01007 M[3] = mat[1];
01008 M[4] = mat[4];
01009 M[5] = mat[7];
01010
01011 M[6] = mat[2];
01012 M[7] = mat[5];
01013 M[8] = mat[8];
01014
01015 return mat3(M);
01016 }
01017
01019 mat3
01020 operator*(double const& s, mat3 const& m)
01021 {
01022 double M[9];
01023
01024 M[0] = s*m.mat[0];
01025 M[1] = s*m.mat[1];
01026 M[2] = s*m.mat[2];
01027 M[3] = s*m.mat[3];
01028 M[4] = s*m.mat[4];
01029 M[5] = s*m.mat[5];
01030 M[6] = s*m.mat[6];
01031 M[7] = s*m.mat[7];
01032 M[8] = s*m.mat[8];
01033 return mat3(M);
01034 }
01035
01037 mat3 const&
01038 mat3::operator+=(mat3 const& m)
01039 {
01040 mat[0] += m[0]; mat[1] += m[1]; mat[2] += m[2];
01041 mat[3] += m[3]; mat[4] += m[4]; mat[5] += m[5];
01042 mat[6] += m[6]; mat[7] += m[7]; mat[8] += m[8];
01043 return *this;
01044 }
01045
01047
01049
01053 SbRotation
01054 Quaternion::toSbRotation() const
01055 {
01056 return SbRotation((float) x,(float) y, (float) z, (float) w);
01057 }
01058
01060 Quaternion&
01061 Quaternion::operator=(const Quaternion& q)
01062 {
01063 set(q);
01064 return *this;
01065 }
01066
01068 Quaternion
01069 Quaternion::operator+(const Quaternion& q) const
01070 {
01071 return Quaternion(w+q.w,x+q.x,y+q.y,z+q.z);
01072 }
01073
01075 Quaternion
01076 Quaternion::operator-(const Quaternion& q) const
01077 {
01078 return Quaternion(w-q.w,x-q.x,y-q.y,z-q.z);
01079 }
01080
01082 Quaternion
01083 Quaternion::operator*(const Quaternion& q) const
01084 {
01085 return Quaternion(
01086 w*q.w-x*q.x-y*q.y-z*q.z,
01087 w*q.x+x*q.w+y*q.z-z*q.y,
01088 w*q.y+y*q.w+z*q.x-x*q.z,
01089 w*q.z+z*q.w+x*q.y-y*q.x
01090 );
01091 }
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01109 Quaternion
01110 Quaternion::operator-() const
01111 {
01112 return Quaternion(-w,-x,-y,-z);
01113 }
01114
01116 double
01117 Quaternion::operator%(const Quaternion& q) const
01118 {
01119 return w*q.w+x*q.x+y*q.y+z*q.z;
01120 }
01121
01123 double
01124 Quaternion::norm() const
01125 {
01126 return w*w+x*x+y*y+z*z;
01127 }
01128
01130 Quaternion
01131 Quaternion::inverse() const
01132 {
01133
01134 return Quaternion(w,-x,-y,-z);
01135 }
01136
01138 vec3
01139 Quaternion::operator*(const vec3& v) const
01140 {
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159 mat3 R;
01160 ToRotationMatrix(R);
01161
01162 vec3 result;
01163 result[0] = v.x()*R.element(0,0)+v.y()*R.element(1,0)+v.z()*R.element(2,0);
01164 result[1] = v.x()*R.element(0,1)+v.y()*R.element(1,1)+v.z()*R.element(2,1);
01165 result[2] = v.x()*R.element(0,2)+v.y()*R.element(1,2)+v.z()*R.element(2,2);
01166
01167 return result;
01168 }
01169
01171 position
01172 Quaternion::operator*(const position& p) const
01173 {
01174 mat3 R;
01175 ToRotationMatrix(R);
01176
01177 position result;
01178 result[0] = p.x()*R.element(0,0)+p.y()*R.element(1,0)+p.z()*R.element(2,0);
01179 result[1] = p.x()*R.element(0,1)+p.y()*R.element(1,1)+p.z()*R.element(2,1);
01180 result[2] = p.x()*R.element(0,2)+p.y()*R.element(1,2)+p.z()*R.element(2,2);
01181
01182 return result;
01183 }
01184
01188 bool
01189 Quaternion::operator==(Quaternion const& q) const
01190 {
01191 if ((w-q.w)*(w-q.w)+(x-q.x)*(x-q.x)+(y-q.y)*(y-q.y)+(z-q.z)*(z-q.z) <
01192 resabs * resabs)
01193 return true;
01194 return false;
01195 }
01196
01198 void
01199 Quaternion::normalise()
01200 {
01201 double nm=norm();
01202 double invnorm;
01203 if (nm==0.0) {
01204 #ifdef GRASPITDBG
01205 printf("0 norm in Quaternion normalise\n");
01206 #endif
01207 return;
01208 }
01209 invnorm =1.0/sqrt(nm);
01210 w*=invnorm; x*=invnorm; y*=invnorm; z*=invnorm;
01211 }
01212
01214
01216
01220 transf
01221 transf::inverse() const
01222 {
01223 return transf(rot.inverse(),-(rot.inverse() * t));
01224 }
01225
01227 transf const&
01228 transf::operator=(transf const& tr)
01229 {
01230 rot = tr.rotation();
01231 R = tr.affine();
01232 t = tr.translation();
01233 return *this;
01234 }
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01248 void
01249 transf::toSoTransform(SoTransform *IVt) const
01250 {
01251 IVt->rotation.setValue(rot.toSbRotation());
01252 IVt->translation.setValue(t.toSbVec3f());
01253 }
01255 void
01256 transf::toColMajorMatrix(double mat[][4]) const
01257 {
01258 for (int i=0; i<3; i++) {
01259 for(int j=0; j<3; j++) {
01260 mat[j][i] = R.element(j,i);
01261 }
01262 mat[i][3] = 0.0;
01263 }
01264 mat[3][0] = t[0];
01265 mat[3][1] = t[1];
01266 mat[3][2] = t[2];
01267 mat[3][3] = 1.0;
01268 }
01269
01271 void
01272 transf::toRowMajorMatrix(double mat[][4]) const
01273 {
01274 for (int i=0; i<3; i++) {
01275 for(int j=0; j<3; j++) {
01276 mat[i][j] = R.element(j,i);
01277 }
01278 mat[3][i] = 0.0;
01279 }
01280 mat[0][3] = t[0];
01281 mat[1][3] = t[1];
01282 mat[2][3] = t[2];
01283 mat[3][3] = 1.0;
01284 }
01285
01287 void
01288 transf::tocol_Mat4(col_Mat4 colTran) const
01289 {
01290 for (int j=0;j<3;j++) {
01291 for(int i=0;i<3;i++)
01292 colTran[i][j] = R.element(j,i);
01293 colTran[3][j] = 0.0;
01294 }
01295 colTran[0][3] = t[0];
01296 colTran[1][3] = t[1];
01297 colTran[2][3] = t[2];
01298 colTran[3][3] = 1.0;
01299 }
01300
01302 void
01303 transf::set(const SoTransform *IVt)
01304 {
01305 float q1,q2,q3,q4,x,y,z;
01306
01307
01308 IVt->rotation.getValue().getValue(q2,q3,q4,q1);
01309 IVt->translation.getValue().getValue(x,y,z);
01310 rot.w=(double)q1; rot.x=(double)q2; rot.y=(double)q3; rot.z=(double)q4;
01311 t[0]=(double)x; t[1]=(double)y; t[2]=(double)z;
01312 rot.ToRotationMatrix(R);
01313 }
01314
01316 transf
01317 operator*(const transf& tr2, const transf& tr1)
01318 {
01319 Quaternion newRot = tr1.rotation() * tr2.rotation();
01320 return transf(newRot, tr1.translation() + tr1.rotation() * tr2.translation());
01321 }
01322
01327 vec3
01328 operator*(const vec3& v, const transf& tr)
01329 {
01330 return tr.rot * v;
01331 }
01332
01333 vec3
01334 operator>(const vec3& v, const transf& tr)
01335 {
01336 return tr.rot*v + tr.t;
01337 }
01338
01339
01343 position
01344 operator*(const position& p, const transf& tr)
01345 {
01346 return tr.rot * p + tr.t;
01347 }
01348
01353 bool
01354 transf::operator==(transf const& tr) const
01355 {
01356 if (tr.rot == rot && tr.t == t)
01357 return true;
01358 return false;
01359 }
01360
01365 transf
01366 translate_transf(const vec3& v)
01367 {
01368 return transf(Quaternion::IDENTITY,v);
01369 }
01370
01375 transf
01376 rotate_transf(double angle, const vec3& axis)
01377 {
01378 return transf(Quaternion(angle,axis),vec3::ZERO);
01379 }
01380
01385 transf
01386 coordinate_transf(const position& o, const vec3& xaxis, const vec3& yaxis)
01387 {
01388 vec3 newxaxis = normalise(xaxis);
01389 vec3 newzaxis = normalise(newxaxis * yaxis);
01390 vec3 newyaxis = normalise(newzaxis * newxaxis);
01391 return transf(mat3(newxaxis,newyaxis,newzaxis),o - position::ORIGIN);
01392 }
01393
01394 transf
01395 rotXYZ(double rx, double ry, double rz)
01396 {
01397 transf r;
01398 vec3 x(1,0,0), y(0,1,0), z(0,0,1);
01399
01400 r = rotate_transf(rx, x);
01401 y = y * r.inverse();
01402 r = rotate_transf(ry, y) * r;
01403 z = z * r.inverse();
01404 r = rotate_transf(rz, z) * r;
01405
01406 return r;
01407 }
01408
01409 #define _MATVEC_H_
01410 #endif
01411
01412