00001 #include <iostream>
00002 using namespace std;
00003 #include <vcg/math/quaternion.h>
00004 #include <vcg/math/matrix33.h>
00005
00006
00007 using namespace vcg;
00008
00009
00010
00011 ostream &operator<<(ostream &o, Point3f &q) {
00012 o.precision(6) ;
00013 o.setf( ios::fixed, ios::floatfield ) ;
00014 o << "[" << q[0] << " " << q[1] << " " << q[2] << "]";
00015 return o;
00016 }
00017
00018 ostream &operator<<(ostream &o, Quaternionf &q) {
00019 o.precision(6) ;
00020 o.setf( ios::fixed, ios::floatfield ) ;
00021 o << "[" << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << "]";
00022 return o;
00023 }
00024
00025 ostream &operator<<(ostream &o, Matrix33f &m) {
00026 o.precision(6) ;
00027 o.setf( ios::fixed, ios::floatfield ) ;
00028 for(int i = 0; i < 3; i++) {
00029 o << "[";
00030 for(int j = 0; j < 3; j++) {
00031 o << m[i][j] << " ";
00032 }
00033 o << "] ";
00034 }
00035 return o;
00036 }
00037 ostream &operator<<(ostream &o, Matrix44f &m) {
00038 o.precision(6) ;
00039 o.setf( ios::fixed, ios::floatfield ) ;
00040 for(int i = 0; i < 4; i++) {
00041 o << "[";
00042 for(int j = 0; j < 4; j++) {
00043 o << m[i][j] << " ";
00044 }
00045 o << "] ";
00046 }
00047 return o;
00048 }
00049
00050 bool verify(Quaternionf &q){
00051 cout << "Quaternion: " << q << endl;
00052 Matrix33f m;
00053 q.ToMatrix(m);
00054 cout << "To Matrix: " << m << endl;
00055 cout << "Row norms: " << m.GetRow(0).Norm() << " "
00056 << m.GetRow(1).Norm() << " "
00057 << m.GetRow(2).Norm() << endl;
00058 Point3f p(3, 4, 5);
00059 Point3f qp = q.Rotate(p);
00060 Point3f mp = m*p;
00061 cout << "Rotating p: " << p << endl;
00062 cout << "q*p = " << qp << " m*p: " << mp << endl;
00063 q.FromMatrix(m);
00064 cout << "Back to: " << q << endl << endl;
00065 return true;
00066 }
00067
00068 bool verify(Matrix33f &m) {
00069 cout << "Matrix: " << m << endl;
00070 cout << "Det: " << m.Determinant() << endl;
00071 cout << "Row norms: " << m.GetRow(0).Norm() << " "
00072 << m.GetRow(1).Norm() << " "
00073 << m.GetRow(2).Norm() << endl;
00074 cout << "Column norms: " << m.GetColumn(0).Norm() << " "
00075 << m.GetColumn(1).Norm() << " "
00076 << m.GetColumn(2).Norm() << endl;
00077 Matrix33f im = m.transpose() * m;
00078 im = im*m;
00079 cout << "Check ortonormality: " << im << endl;
00080 Quaternionf q;
00081 q.FromMatrix(m);
00082 cout << "To Quaternion: " << q << endl;
00083
00084 float alpha = 2*acos(q[0]);
00085 Point3f axis(q[1], q[2], q[3]);
00086 cout << "Norm: " << axis.SquaredNorm() + q[0]*q[0] << endl;
00087 axis.Normalize();
00088 cout << "angle: " << 2*acos(q[0]) << " Axis: " << axis << endl;
00089
00090 Point3f p(3, 4, 5);
00091 Point3f qp = q.Rotate(p);
00092 Point3f mp = m*p;
00093 cout << "Rotating p: " << p << endl;
00094 cout << "q*p = " << qp << " m*p: " << mp << endl;
00095 q.ToMatrix(m);
00096 cout << "Back to: " << m << endl<< endl;
00097 }
00098
00099 int main() {
00100
00101 Quaternionf q(1, 0, 0, 0);
00102
00103 verify(q);
00104
00105 q.FromAxis(M_PI/2, Point3f(0, 0, 1));
00106
00107 verify(q);
00108
00109 q.FromAxis(M_PI/4, Point3f(0, 1, 1));
00110
00111 verify(q);
00112
00113 Matrix33f m;
00114 m[0][0] = 0.70145; m[0][1] = 0.372035; m[0][2] = 0.607913;
00115 m[1][0] = -0.628023; m[1][1] = 0.725922; m[1][2] = 0.2804;
00116 m[2][0] = -0.336978; m[2][1] = -0.57847; m[2][2] = 0.742845;
00117
00118 cout << "verify matrix: " << endl;
00119 verify(m);
00120 verify(m);
00121
00122 q.FromAxis(0.7, Point3f(-0.20, -0.42, -0.83));
00123 cout << "verify from axis: " << endl;
00124 verify(q);
00125
00126
00127 Quaternionf iq= Inverse(q);
00128
00129
00130 cout << "matrix: " << m << endl;
00131
00132 Point3f p(3, 4, 5);
00133
00134 cout << "norms: " << m.GetRow(0).Norm() << " " << m.GetRow(1).Norm() << " " << m.GetRow(2).Norm() << endl;
00135 q.FromMatrix(m);
00136 q.ToMatrix(m);
00137 cout << "quaternion: " << q[0] << " " << q[1] << " " << q[2] << " " << q[3] <<endl;
00138 cout << "matrix: " << endl;
00139 for(int i = 0; i < 3; i++) {
00140 for(int j = 0; j < 3; j++) {
00141 cout << m[i][j] << " ";
00142 }
00143 cout << endl;
00144 }
00145 cout << endl;
00146
00147 cout << "Point: " << p[0] << " " << p[1] << " " << p[2] << endl;
00148
00149 Point3f r = q.Rotate(p);
00150 cout << "Rotated by q: " << r[0] << " " << r[1] << " " << r[2] << endl;
00151 r = m*p;
00152 cout << "Rotated by m: " << r[0] << " " << r[1] << " " << r[2] << endl;
00153
00154 q.FromMatrix(m);
00155 cout << "quaternion: " << q[0] << " " << q[1] << " " << q[2] << " " << q[3] <<endl;
00156 return 0;
00157 }