7 #define RAD2DEG (180.0/PI) 8 #define DEG2RAD (PI/180.0) 15 vec2d() { x = y = 0.0; }
16 vec2d(
double X,
double Y) { x = X; y = Y; }
22 vec2d operator * (
double g) {
return vec2d(x*g, y*g); }
24 double operator * (
const vec2d& r) {
return (x*r.x + y*r.y); }
26 double norm() {
return sqrt(x*x + y*y); }
27 double unit() {
double R = sqrt(x*x + y*y);
if (R != 0) {x /= R; y/= R; };
return R; }
29 bool operator == (
const vec2d& r)
const {
return (x==r.x)&&(y==r.y); }
41 vec3d() { x = y = z = 0; }
42 vec3d(
double rx,
double ry,
double rz) { x = rx; y = ry; z = rz; }
43 vec3d(
const vec2d& r) { x = r.x; y = r.y; z = 0; }
45 vec3d operator + (
const vec3d& v)
const {
return vec3d( x + v.x, y + v.y, z + v.z); }
46 vec3d operator - (
const vec3d& v)
const {
return vec3d( x - v.x, y - v.y, z - v.z); }
49 return vec3d( y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
52 double operator * (
const vec3d& v)
const {
return (x*v.x + y*v.y + z*v.z); }
54 vec3d operator * (
const double g)
const {
return vec3d(x*g, y*g, z*g); }
55 vec3d operator / (
const double g)
const {
return vec3d(x/g, y/g, z/g); }
57 const vec3d& operator += (
const vec3d& v) { x += v.x; y += v.y; z += v.z;
return (*
this); }
58 const vec3d& operator -= (
const vec3d& v) { x -= v.x; y -= v.y; z -= v.z;
return (*
this); }
59 const vec3d& operator /= (
const double f) { x /= f; y /= f; z /= f;
return (*
this); }
60 const vec3d& operator /= (
const int n) { x /= n; y /= n; z /= n;
return (*
this); }
61 const vec3d& operator *= (
const double f) { x*=f; y*=f; z*=f;
return (*
this); }
63 vec3d operator - ()
const {
return vec3d(-x, -y, -z); }
65 double Length()
const {
return (
double) sqrt(x*x + y*y + z*z); }
66 double SqrLength()
const {
return x*x + y*y + z*z; }
71 if (L != 0) { x /= L; y /= L; z /= L; }
86 vec6d() { x = y = z = xy = yz = xz = 0; }
105 mat3d(
double a00,
double a01,
double a02,
double a10,
double a11,
double a12,
double a20,
double a21,
double a22);
107 double* operator [] (
int i) {
return m_data[i]; }
108 double& operator () (
int i,
int j) {
return m_data[i][j]; }
117 a[0][0] += m_data[0][k]*m[k][0]; a[0][1] += m_data[0][k]*m[k][1]; a[0][2] += m_data[0][k]*m[k][2];
118 a[1][0] += m_data[1][k]*m[k][0]; a[1][1] += m_data[1][k]*m[k][1]; a[1][2] += m_data[1][k]*m[k][2];
119 a[2][0] += m_data[2][k]*m[k][0]; a[2][1] += m_data[2][k]*m[k][1]; a[2][2] += m_data[2][k]*m[k][2];
132 a[0][0] += m_data[0][k]*m[k][0]; a[0][1] += m_data[0][k]*m[k][1]; a[0][2] += m_data[0][k]*m[k][2];
133 a[1][0] += m_data[1][k]*m[k][0]; a[1][1] += m_data[1][k]*m[k][1]; a[1][2] += m_data[1][k]*m[k][2];
134 a[2][0] += m_data[2][k]*m[k][0]; a[2][1] += m_data[2][k]*m[k][1]; a[2][2] += m_data[2][k]*m[k][2];
137 m_data[0][0] = a.m_data[0][0]; m_data[0][1] = a.m_data[0][1]; m_data[0][2] = a.m_data[0][2];
138 m_data[1][0] = a.m_data[1][0]; m_data[1][1] = a.m_data[1][1]; m_data[1][2] = a.m_data[1][2];
139 m_data[2][0] = a.m_data[2][0]; m_data[2][1] = a.m_data[2][1]; m_data[2][2] = a.m_data[2][2];
146 m_data[0][0] += m[0][0]; m_data[0][1] += m[0][1]; m_data[0][2] += m[0][2];
147 m_data[1][0] += m[1][0]; m_data[1][1] += m[1][1]; m_data[1][2] += m[1][2];
148 m_data[2][0] += m[2][0]; m_data[2][1] += m[2][1]; m_data[2][2] += m[2][2];
154 m_data[0][0] -= m[0][0]; m_data[0][1] -= m[0][1]; m_data[0][2] -= m[0][2];
155 m_data[1][0] -= m[1][0]; m_data[1][1] -= m[1][1]; m_data[1][2] -= m[1][2];
156 m_data[2][0] -= m[2][0]; m_data[2][1] -= m[2][1]; m_data[2][2] -= m[2][2];
161 mat3d& operator /=(
const double f)
163 m_data[0][0] /= f; m_data[0][1] /= f; m_data[0][2] /= f;
164 m_data[1][0] /= f; m_data[1][1] /= f; m_data[1][2] /= f;
165 m_data[2][0] /= f; m_data[2][1] /= f; m_data[2][2] /= f;
173 r.x = m_data[0][0]*b.x + m_data[0][1]*b.y + m_data[0][2]*b.z;
174 r.y = m_data[1][0]*b.x + m_data[1][1]*b.y + m_data[1][2]*b.z;
175 r.z = m_data[2][0]*b.x + m_data[2][1]*b.y + m_data[2][2]*b.z;
183 det += m_data[0][0]*m_data[1][1]*m_data[2][2];
184 det += m_data[0][1]*m_data[1][2]*m_data[2][0];
185 det += m_data[0][2]*m_data[1][0]*m_data[2][1];
186 det -= m_data[0][2]*m_data[1][1]*m_data[2][0];
187 det -= m_data[0][1]*m_data[1][0]*m_data[2][2];
188 det -= m_data[0][0]*m_data[1][2]*m_data[2][1];
196 m_data[0][0] = m_data[0][1] = m_data[0][2] = 0;
197 m_data[1][0] = m_data[1][1] = m_data[1][2] = 0;
198 m_data[2][0] = m_data[2][1] = m_data[2][2] = 0;
203 m_data[0][0] = m_data[1][1] = m_data[2][2] = 1;
204 m_data[0][1] = m_data[0][2] = m_data[1][2] = 0;
205 m_data[1][0] = m_data[2][0] = m_data[2][1] = 0;
223 ~
matrix() {
delete [] d; }
227 double* operator [] (
int i) {
return d + i*m_nc; }
228 double& operator () (
int i,
int j) {
return d[i*m_nc + j]; }
230 bool solve(vector<double>& x, vector<double>& b);
232 bool lsq_solve(vector<double>& x, vector<double>& b);
233 bool eigen_vectors(
matrix&
Eigen,vector<double>& eigen_values);
234 int Rows() {
return m_nr; }
236 void mult_transpose(vector<double>& x, vector<double>& y);
238 void mult_transpose_self(
matrix& AAt);
254 quatd () { x = y = z = 0; w = 1; }
258 w = (double) cos(angle * 0.5);
260 double sina = (double) sin(angle * 0.5);
276 double sina = (double) sqrt((1.0-d)*0.5);
277 double cosa = (double) sqrt((1.0+d)*0.5);
287 quatd(
const double qx,
const double qy,
const double qz,
const double qw = 1.0)
295 bool operator != (
const quatd& q) {
return ((x!=q.x) || (y!=q.y) || (z!=q.z) || (w!=q.w)); }
297 quatd operator - () {
return quatd(-x, -y, -z, -w); }
303 return quatd(x + q.x, y + q.y, z + q.z, w + q.w);
308 return quatd(x - q.x, y - q.y, z - q.z, w - q.w);
336 double qw = w*q.w - x*q.x - y*q.y - z*q.z;
337 double qx = w*q.x + x*q.w + y*q.z - z*q.y;
338 double qy = w*q.y + y*q.w + z*q.x - x*q.z;
339 double qz = w*q.z + z*q.w + x*q.y - y*q.x;
341 return quatd(qx, qy, qz, qw);
346 double qw = w*q.w - x*q.x - y*q.y - z*q.z;
347 double qx = w*q.x + x*q.w + y*q.z - z*q.y;
348 double qy = w*q.y + y*q.w + z*q.x - x*q.z;
349 double qz = w*q.z + z*q.w + x*q.y - y*q.x;
359 quatd operator*(
const double a)
const 361 return quatd(x*a, y*a, z*a, w*a);
366 quatd operator / (
const double a)
const 368 return quatd(x/a, y/a, z/a, w/a);
371 quatd& operator /= (
const double a)
383 quatd Conjugate()
const {
return quatd(-x, -y, -z, w); }
385 double Norm()
const {
return w*w + x*x + y*y + z*z; }
389 double N = (double) sqrt(w*w + x*x + y*y + z*z);
401 quatd Inverse()
const 403 double N = w*w + x*x + y*y + z*z;
405 return quatd(-x/N, -y/N, -z/N, w/N);
408 double DotProduct(
const quatd& q)
const 410 return w*q.w + x*q.x + y*q.y + z*q.z;
413 vec3d GetVector()
const 415 return vec3d(x, y, z).Normalize();
418 double GetAngle()
const 420 return (
double)(acos(w)*2.0);
439 void RotateVector(
vec3d& v)
const 441 if ((w == 0) || ((x==0) && (y==0) && (z==0)))
return;
444 double qw = v.x*x + v.y*y + v.z*z;
445 double qx = v.x*w - v.y*z + v.z*y;
446 double qy = v.y*w - v.z*x + v.x*z;
447 double qz = v.z*w - v.x*y + v.y*x;
450 v.x = (double) (w*qx + x*qw + y*qz - z*qy);
451 v.y = (double) (w*qy + y*qw + z*qx - x*qz);
452 v.z = (double) (w*qz + z*qw + x*qy - y*qx);
461 double qw = n.x*x + n.y*y + n.z*z;
462 double qx = n.x*w - n.y*z + n.z*y;
463 double qy = n.y*w - n.z*x + n.x*z;
464 double qz = n.z*w - n.x*y + n.y*x;
467 n.x = (w*qx + x*qw + y*qz - z*qy);
468 n.y = (w*qy + y*qw + z*qx - x*qz);
469 n.z = (w*qz + z*qw + x*qy - y*qx);
477 double qw, qx, qy, qz;
478 for (
int i=0; i<3; ++i)
481 qw = m[0][i]*x + m[1][i]*y + m[2][i]*z;
482 qx = m[0][i]*w - m[1][i]*z + m[2][i]*y;
483 qy = m[1][i]*w - m[2][i]*x + m[0][i]*z;
484 qz = m[2][i]*w - m[0][i]*y + m[1][i]*x;
487 a[0][i] = (w*qx + x*qw + y*qz - z*qy);
488 a[1][i] = (w*qy + y*qw + z*qx - x*qz);
489 a[2][i] = (w*qz + z*qw + x*qy - y*qx);
495 void RotateVectorP(
double* v,
double* r)
const 497 static double fx, fy, fz, fw;
498 static double qw, qx, qy, qz;
505 qw = v[0]*fx + v[1]*fy + v[2]*fz;
506 qx = v[0]*fw - v[1]*fz + v[2]*fy;
507 qy = v[1]*fw - v[2]*fx + v[0]*fz;
508 qz = v[2]*fw - v[0]*fy + v[1]*fx;
510 r[0] = (double) (fw*qx + fx*qw + fy*qz - fz*qy);
511 r[1] = (double) (fw*qy + fy*qw + fz*qx - fx*qz);
512 r[2] = (double) (fw*qz + fz*qw + fx*qy - fy*qx);
516 {
return q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w; }
519 {
quatd q = (q1*(1-t) + q2*t); q.MakeUnit();
return q; }
528 inline quatd operator * (
const double a,
const quatd& q)
533 typedef unsigned char uchar;
541 GLCOLOR() : a(255), b(0), g(0), r(0){}
542 GLCOLOR(uchar ur, uchar ug, uchar ub, uchar ua = 255)
544 r = ur; g = ug; b = ub; a = ua;
549 return GLCOLOR((uchar) (r*f), (uchar) (g*f), (uchar) (b*f));
554 return GLCOLOR(r+c.r, g+c.g, b+c.b);