53 int svdcmp(
double **a,
int m,
int n,
double *w,
double **v);
54 int quintic(
double [],
double [],
double [],
int*,
double);
55 int quartic(
double[],
double[],
double[],
int* );
56 int cubic(
double[],
double[],
int*);
91 for(
int m=0; m<3; m++)
92 for(
int n=0; n<3; n++)
93 mat.
m[m][n] = (
real_t) m_ptr[m][n];
99 for(
int m=0; m<3; m++)
100 for(
int n=0; n<3; n++)
101 mat.
m[m][n] = (
real_t) m_ptr[m][n];
107 double **M = (
double**) malloc(3*
sizeof(
double*));
108 for(
int i=0; i<3; i++) M[i] = (
double*) malloc(3*
sizeof(
double));
109 for(
int m=0; m<3; m++)
110 for(
int n=0; n<3; n++)
111 M[m][n] = (
double) mat.
m[m][n];
118 float **M = (
float**) malloc(3*
sizeof(
float*));
119 for(
int i=0; i<3; i++) M[i] = (
float*) malloc(3*
sizeof(
float));
120 for(
int m=0; m<3; m++)
121 for(
int n=0; n<3; n++)
122 M[m][n] = (
float) mat.
m[m][n];
129 for(
int i=0; i<3; i++)
free((*m_ptr)[i]);
135 for(
int i=0; i<3; i++)
free((*m_ptr)[i]);
155 double* v = (
double*) malloc(3*
sizeof(
double));
156 v[0] = (double) vec.
v[0];
157 v[1] = (
double) vec.
v[1];
158 v[2] = (double) vec.
v[2];
164 float* v = (
float*) malloc(3*
sizeof(
float));
165 v[0] = (float) vec.
v[0];
166 v[1] = (
float) vec.
v[1];
167 v[2] = (float) vec.
v[2];
189 m.
m[0][0] = m00; m.
m[0][1] = m01; m.
m[0][2] = m02;
190 m.
m[1][0] = m10; m.
m[1][1] = m11; m.
m[1][2] = m12;
191 m.
m[2][0] = m20; m.
m[2][1] = m21; m.
m[2][2] = m22;
197 printf(
"%s: s = [ %.4f ] v = [ ",name,q.
s);
198 for(
unsigned int c=0; c<3; c++)
201 printf(
"%.4f ",q.
v.
v[c]);
208 printf(
"%s:\n",name);
209 for(
unsigned int r=0; r<3; r++)
212 for(
unsigned int c=0; c<3; c++)
214 double d = (double)(m.
m[r][c]);
223 for(
unsigned int i=0; i<m.size(); i++)
225 printf(
"%s.at(%i):\n",name,i);
226 for(
unsigned int r=0; r<3; r++)
229 for(
unsigned int c=0; c<3; c++)
231 double d = (double)(m.at(i).m[r][c]);
241 printf(
"%s: [ ",name);
242 for(
unsigned int c=0; c<3; c++)
244 double d = (double)(v.
v[c]);
252 for(
unsigned int i=0; i<v.size(); i++)
254 printf(
"%s.at(%i): [ ",name,i);
255 for(
unsigned int c=0; c<3; c++)
257 double d = (double)(v.at(i).v[c]);
267 FILE *fp = fopen( filename,
"r" );
268 if( fp ==
NULL )
return(
false);
279 n = fscanf(fp,
"%lf%lf%lf\n", &vx,&vy,&vz);
280 if((n!=3) || ferror(fp))
282 printf(
"file i/o error: %s (line %i)",filename,lineno);
323 iter != va.end(); iter++)
325 v_sum2.
v[0] += (*iter).v[0];
326 v_sum2.
v[1] += (*iter).v[1];
327 v_sum2.
v[2] += (*iter).v[2];
334 v_sum1.resize(va.size());
336 for(
unsigned int i=0; i<va.size(); i++)
337 v_sum1.at(i) = (va[i].v[0] + va[i].v[1] + va[i].v[2]);
343 iter != va.end(); iter++)
345 (*iter).v[0] *= (*iter).v[0];
346 (*iter).v[1] *= (*iter).v[1];
347 (*iter).v[2] *= (*iter).v[2];
398 va.
v[0] = vb.
v[0] + vc.
v[0];
399 va.
v[1] = vb.
v[1] + vc.
v[1];
400 va.
v[2] = vb.
v[2] + vc.
v[2];
419 va.
v[0] = vb.
v[0] - vc.
v[0];
420 va.
v[1] = vb.
v[1] - vc.
v[1];
421 va.
v[2] = vb.
v[2] - vc.
v[2];
426 return(va.
v[0]*vb.
v[0]+va.
v[1]*vb.
v[1]+va.
v[2]*vb.
v[2]);
431 va.
v[0] = (vb.
v[1] * vc.
v[2] - vc.
v[1] * vb.
v[2]);
432 va.
v[1] = (vb.
v[2] * vc.
v[0] - vc.
v[2] * vb.
v[0]);
433 va.
v[2] = (vb.
v[0] * vc.
v[1] - vc.
v[0] * vb.
v[1]);
438 return(
_sqrt(v.
v[0]*v.
v[0] + v.
v[1]*v.
v[1] + v.
v[2]*v.
v[2]));
443 return(v.
v[0] + v.
v[1] + v.
v[2]);
449 iter != va.end(); iter++)
451 (*iter).v[0] += a.
v[0];
452 (*iter).v[1] += a.
v[1];
453 (*iter).v[2] += a.
v[2];
460 iter != va.end(); iter++)
462 (*iter).v[0] -= a.
v[0];
463 (*iter).v[1] -= a.
v[1];
464 (*iter).v[2] -= a.
v[2];
471 iter != va.end(); iter++)
473 if(mask[0]) (*iter).v[0] = a.
v[0];
474 if(mask[1]) (*iter).v[1] = a.
v[1];
475 if(mask[2]) (*iter).v[2] = a.
v[2];
481 assert(va.size() == c.size());
482 for(
unsigned int i=0; i<va.size(); i++)
500 m.
m[0][0] = va.
v[0] * vb.
v[0];
501 m.
m[0][1] = va.
v[0] * vb.
v[1];
502 m.
m[0][2] = va.
v[0] * vb.
v[2];
503 m.
m[1][0] = va.
v[1] * vb.
v[0];
504 m.
m[1][1] = va.
v[1] * vb.
v[1];
505 m.
m[1][2] = va.
v[1] * vb.
v[2];
506 m.
m[2][0] = va.
v[2] * vb.
v[0];
507 m.
m[2][1] = va.
v[2] * vb.
v[1];
508 m.
m[2][2] = va.
v[2] * vb.
v[2];
513 return(va.
v[0] * vb.
v[0] + va.
v[1] * vb.
v[1] + va.
v[2] * vb.
v[2]);
519 for(
unsigned int r=0; r<3; r++)
520 for(
unsigned int c=0; c<3; c++)
528 for(
unsigned int r=0; r<3; r++)
529 for(
unsigned int c=0; c<3; c++)
531 md.
m[r][c] = ms.
m[r][c];
538 for(
unsigned int r=0; r<3; r++)
576 for(
unsigned int r=0; r<3; r++)
577 for(
unsigned int c=0; c<3; c++)
587 for(
unsigned int r=0; r<3; r++)
588 for(
unsigned int c=0; c<3; c++)
590 if(m.
m[r][c] != 0)
return(
false);
597 for(
unsigned int r=0; r<3; r++)
598 for(
unsigned int c=0; c<3; c++)
609 iter != ma.end(); iter++)
611 for(
unsigned int c=0; c<3; c++)
613 s.
m[0][c] += (*iter).m[0][c];
614 s.
m[1][c] += (*iter).m[1][c];
615 s.
m[2][c] += (*iter).m[2][c];
623 for(
unsigned int r=0; r<3; r++)
624 for(
unsigned int c=0; c<3; c++)
626 mr.
m[r][c] = ma.
m[r][c]-mb.
m[r][c];
632 for(
unsigned int r=0; r<3; r++)
633 for(
unsigned int c=0; c<3; c++)
635 ma.
m[r][c] -= mb.
m[r][c];
641 for(
unsigned int r=0; r<3; r++)
642 for(
unsigned int c=0; c<3; c++)
644 mr.
m[r][c] = ma.
m[r][c]+mb.
m[r][c];
650 for(
unsigned int r=0; r<3; r++)
651 for(
unsigned int c=0; c<3; c++)
653 ma.
m[r][c] += mb.
m[r][c];
660 real_t determinant = a.
m[0][0]*a.
m[1][1]*a.
m[2][2] + a.
m[0][1]*a.
m[1][2]*a.
m[2][0] +
661 a.
m[0][2]*a.
m[1][0]*a.
m[2][1] - a.
m[2][0]*a.
m[1][1]*a.
m[0][2] -
662 a.
m[2][1]*a.
m[1][2]*a.
m[0][0] - a.
m[2][2]*a.
m[1][0]*a.
m[0][1];
669 mi.
m[0][0] = (ma.
m[1][1]*ma.
m[2][2] - ma.
m[1][2]*ma.
m[2][1])/determinant;
670 mi.
m[0][1] = (ma.
m[0][2]*ma.
m[2][1] - ma.
m[0][1]*ma.
m[2][2])/determinant;
671 mi.
m[0][2] = (ma.
m[0][1]*ma.
m[1][2] - ma.
m[0][2]*ma.
m[1][1])/determinant;
673 mi.
m[1][0] = (ma.
m[1][2]*ma.
m[2][0] - ma.
m[1][0]*ma.
m[2][2])/determinant;
674 mi.
m[1][1] = (ma.
m[0][0]*ma.
m[2][2] - ma.
m[0][2]*ma.
m[2][0])/determinant;
675 mi.
m[1][2] = (ma.
m[0][2]*ma.
m[1][0] - ma.
m[0][0]*ma.
m[1][2])/determinant;
677 mi.
m[2][0] = (ma.
m[1][0]*ma.
m[2][1] - ma.
m[1][1]*ma.
m[2][0])/determinant;
678 mi.
m[2][1] = (ma.
m[0][1]*ma.
m[2][0] - ma.
m[0][0]*ma.
m[2][1])/determinant;
679 mi.
m[2][2] = (ma.
m[0][0]*ma.
m[1][1] - ma.
m[0][1]*ma.
m[1][0])/determinant;
684 m0.
m[0][0] = m1.
m[0][0]*m2.
m[0][0] + m1.
m[0][1]*m2.
m[1][0] + m1.
m[0][2]*m2.
m[2][0];
685 m0.
m[0][1] = m1.
m[0][0]*m2.
m[0][1] + m1.
m[0][1]*m2.
m[1][1] + m1.
m[0][2]*m2.
m[2][1];
686 m0.
m[0][2] = m1.
m[0][0]*m2.
m[0][2] + m1.
m[0][1]*m2.
m[1][2] + m1.
m[0][2]*m2.
m[2][2];
687 m0.
m[1][0] = m1.
m[1][0]*m2.
m[0][0] + m1.
m[1][1]*m2.
m[1][0] + m1.
m[1][2]*m2.
m[2][0];
688 m0.
m[1][1] = m1.
m[1][0]*m2.
m[0][1] + m1.
m[1][1]*m2.
m[1][1] + m1.
m[1][2]*m2.
m[2][1];
689 m0.
m[1][2] = m1.
m[1][0]*m2.
m[0][2] + m1.
m[1][1]*m2.
m[1][2] + m1.
m[1][2]*m2.
m[2][2];
690 m0.
m[2][0] = m1.
m[2][0]*m2.
m[0][0] + m1.
m[2][1]*m2.
m[1][0] + m1.
m[2][2]*m2.
m[2][0];
691 m0.
m[2][1] = m1.
m[2][0]*m2.
m[0][1] + m1.
m[2][1]*m2.
m[1][1] + m1.
m[2][2]*m2.
m[2][1];
692 m0.
m[2][2] = m1.
m[2][0]*m2.
m[0][2] + m1.
m[2][1]*m2.
m[1][2] + m1.
m[2][2]*m2.
m[2][2];
697 for(
unsigned int r=0; r<3; r++)
698 for(
unsigned int c=0; c<3; c++)
706 for(
unsigned int r=0; r<3; r++)
707 for(
unsigned int c=0; c<3; c++)
709 t.
m[r][c] = m.
m[c][r];
715 v0.
v[0] = m1.
m[0][0]*v2.
v[0] + m1.
m[0][1]*v2.
v[1] + m1.
m[0][2]*v2.
v[2];
716 v0.
v[1] = m1.
m[1][0]*v2.
v[0] + m1.
m[1][1]*v2.
v[1] + m1.
m[1][2]*v2.
v[2];
717 v0.
v[2] = m1.
m[2][0]*v2.
v[0] + m1.
m[2][1]*v2.
v[1] + m1.
m[2][2]*v2.
v[2];
723 va.resize(vb.size());
724 for(
unsigned int i=0; i<vb.size(); i++)
742 svdcmp(m_ptr, 3, 3, q_ptr, v_ptr);
767 return(
_sqrt((f_vn*f_vn) + (q.
s*q.
s)));
778 m.
m[0][0] = (a*a)+(b*b)-(c*c)-(d*d);
783 m.
m[1][1] = (a*a)+(c*c)-(b*b)-(d*d);
788 m.
m[2][2] = (a*a)+(d*d)-(b*b)-(c*c);
805 normR_v.assign(v.begin(),v.end());
810 for(scalar_array::iterator iter = _l.begin();
811 iter != _l.end(); iter++)
813 (*iter) = 1.0f /
_sqrt(*iter);
816 normR_v.assign(v.begin(),v.end());
823 if(coefficients.size() != 5)
return(0);
826 double dd[5] = {(double)coefficients[0],
827 (
double)coefficients[1],
828 (double)coefficients[2],
829 (
double)coefficients[3],
830 (double)coefficients[4] };
832 double sol[4] = {0,0,0,0};
833 double soli[4] = {0,0,0,0};
835 quartic(dd, sol, soli, &n_sol);
837 if(n_sol <= 0)
return(0);
840 for(
int i=0; i<n_sol; i++) r_sol[i] = (
real_t)sol[i];
847 for(
unsigned int i=0; i<sa.size(); i++) sa.at(i) =
_pow(sa[i],f);
852 for(
unsigned int i=0; i<sa.size(); i++) sa.at(i) = - sa.at(i);
857 const unsigned int sz)
861 for(
unsigned int i=0; i<sz; i++) sa.at(i) = f;
867 assert(sa.size() == sb.size());
868 for(
unsigned int i=0; i<(
unsigned int)sa.size(); i++)
869 sa.at(i) = sa.at(i) + sb.at(i);
874 for(
unsigned int i=0; i<(
unsigned int)sa.size(); i++) sa.at(i) = 0.;
881 assert(sb.size() == sc.size());
883 sa.resize(sb.size());
884 for(
unsigned int i=0; i<(
unsigned int)sb.size(); i++)
885 sa[i] =
_atan2(sb[i],sc[i]);
890 for(
unsigned int i=0; i<sa.size(); i++)
891 printf(
"%s.at(%i): [ %e ]\n",name,i,sa[i]);
896 for(
unsigned int i=0; i<(
unsigned int)sa.size(); i++)
904 assert(sa.size() == sb.size());
905 for(
unsigned int i=0; i<(
unsigned int)sa.size(); i++)
911 for(
unsigned int i=0; i<(
unsigned int)sa.size(); i++)
919 for(
unsigned int i=0; i<(
unsigned int)sa.size(); i++)
927 for(
unsigned int i=0; i<(
unsigned int)sa.size(); i++)
935 for(
unsigned int r=0; r<3; r++)
936 for(
unsigned int c=0; c<3; c++)
938 m.
m[r][c] *= m.
m[r][c];
945 fprintf((FILE*)fp,
"%s: [ ",name);
946 for(
unsigned int c=0; c<3; c++)
948 fprintf((FILE*)fp,
"%.4f ",v.
v[c]);
950 fprintf((FILE*)fp,
"]\n");
955 fprintf((FILE*)fp,
"%s:\n",name);
956 for(
unsigned int r=0; r<3; r++)
958 fprintf((FILE*)fp,
"[ ");
959 for(
unsigned int c=0; c<3; c++)
961 fprintf((FILE*)fp,
"%.4f ",m.
m[r][c]);
963 fprintf((FILE*)fp,
"]\n");
real_t vec3trans_mul_vec3(const vec3_t &va, const vec3_t &vb)
void scalar_array_clear(scalar_array &sa)
std::vector< vec3_t >::const_iterator vec3_array_const_iter
void mat33_eye(mat33_t &m)
real_t mat33_det(const mat33_t &a)
void mat33_from_float_pptr(mat33_t &mat, float **m_ptr)
void mat33_svd2(mat33_t &u, mat33_t &s, mat33_t &v, const mat33_t &m)
void free_float_ptr(float **v_ptr)
void _dbg_mat33_print(const mat33_t &m, char *name)
void mat33_to_col_vec3(vec3_t &c0, vec3_t &c1, vec3_t &c2, const mat33_t &m)
void normRv(vec3_t &n, const vec3_t &v)
void mat33_div(mat33_t &m, const real_t f)
float ** mat33_to_float_pptr(const mat33_t &mat)
bool _dbg_load_vec3_array(vec3_array &va, char *filename)
void vec3_array_sub(vec3_array &va, const vec3_t &a)
void mat33_mult(mat33_t &m0, const mat33_t &m1, const mat33_t &m2)
std::vector< mat33_t > mat33_array
void scalar_array_negate(scalar_array &sa)
real_t vec3_dot(const vec3_t &va, const vec3_t &vb)
void scalar_array_mult(scalar_array &sa, real_t f)
void free_double_ptr(double **v_ptr)
void vec3_from_double_ptr(vec3_t &vec, double *v_ptr)
bool mat33_all_zeros(const mat33_t &m)
void free_double_pptr(double ***m_ptr)
std::vector< vec3_t > vec3_array
void quat_mult(quat_t &q, const real_t s)
void _dbg_vec3_fprint(void *fp, const vec3_t &v, char *name)
void vec3_array_pow2(vec3_array &va)
int quartic(double[], double[], double[], int *)
double * vec3_to_double_ptr(const vec3_t &vec)
void mat33_clear(mat33_t &m)
double ** mat33_to_double_pptr(const mat33_t &mat)
float * vec3_to_float_ptr(const vec3_t &vec)
void vec3_cross(vec3_t &va, const vec3_t &vb, const vec3_t &vc)
void _dbg_mat33_array_print(const mat33_array &m, char *name)
void mat33_assign(mat33_t &m, const real_t m00, const real_t m01, const real_t m02, const real_t m10, const real_t m11, const real_t m12, const real_t m20, const real_t m21, const real_t m22)
void mat33_from_double_pptr(mat33_t &mat, double **m_ptr)
std::vector< vec3_t >::iterator vec3_array_iter
void vec3_array_mult(vec3_array &va, const scalar_array &c)
std::vector< real_t > scalar_array
real_t _pow(real_t a, real_t b)
void _dbg_scalar_array_print(const scalar_array &sa, char *name)
void scalar_array_atan2(scalar_array &sa, const scalar_array &sb, const scalar_array &sc)
void mat33_from_quat(mat33_t &m, const quat_t &q)
void scalar_array_add(scalar_array &sa, const scalar_array &sb)
void mat33_sub(mat33_t &mr, const mat33_t &ma, const mat33_t &mb)
void _dbg_vec3_print(const vec3_t &v, char *name)
void vec3_from_float_ptr(vec3_t &vec, float *v_ptr)
real_t vec3_norm(const vec3_t &v)
void scalar_array_assign(scalar_array &sa, const real_t f, const unsigned int sz)
int quintic(double[], double[], double[], int *, double)
int cubic(double[], double[], int *)
void mat33_set_all_zeros(mat33_t &m)
void vec3_array_add(vec3_array &va, const vec3_t &a)
real_t vec3_sum(const vec3_t &v)
void vec3_add(vec3_t &va, const real_t f)
void mat33_inv(mat33_t &mi, const mat33_t &ma)
int solve_polynomial(scalar_array &r_sol, const scalar_array &coefficients)
void scalar_array_pow(scalar_array &sa, const real_t f)
void mat33_array_sum(mat33_t &s, const mat33_array &ma)
real_t quat_norm(const quat_t &q)
void vec3_mul_vec3trans(mat33_t &m, const vec3_t &va, const vec3_t &vb)
int svdcmp(double **a, int m, int n, double *w, double **v)
void vec3_array_mean(vec3_t &v_mean, const vec3_array &va)
void vec3_clear(vec3_t &v)
void free_float_pptr(float ***m_ptr)
void vec3_sub(vec3_t &va, const real_t f)
void vec3_array_sum(vec3_t &v_sum2, const vec3_array &va)
void scalar_array_sub(scalar_array &sa, real_t f)
void mat33_copy(mat33_t &md, const mat33_t &ms)
void _dbg_quat_print(const quat_t &q, char *name)
void _dbg_vec3_array_print(const vec3_array &v, char *name)
void vec3_div(vec3_t &va, const real_t n)
void mat33_pow2(mat33_t &m)
real_t _atan2(real_t a, real_t b)
void mat33_transpose(mat33_t &t, const mat33_t m)
std::vector< mat33_t >::const_iterator mat33_array_const_iter
void _dbg_mat33_fprint(void *fp, const mat33_t &m, char *name)
void mat33_add(mat33_t &mr, const mat33_t &ma, const mat33_t &mb)
real_t mat33_sum(const mat33_t &m)
void vec3_array_set(vec3_array &va, const vec3_t &a, const bool mask[3])
void vec3_copy(vec3_t &a, const vec3_t &b)
void scalar_array_div(scalar_array &sa, real_t f)
void vec3_assign(vec3_t &v, const real_t x, const real_t y, const real_t z)
void vec3_mult(vec3_t &va, const real_t n)