34 #include <libFreeWRL.h>
36 #include "../vrml_parser/Structs.h"
37 #include "../main/headers.h"
39 #include "LinearAlgebra.h"
41 #define DJ_KEEP_COMPILER_WARNING 0
42 double signd(
double val){
43 return val < 0.0 ? -1.0 : val > 0.0 ? 1.0 : 0;
45 double * vecsignd(
double *b,
double *a){
47 for (i = 0; i<3; i++) b[i] = signd(a[i]);
50 double * vecmuld(
double *c,
double *a,
double *b){
56 double * vecsetd(
double *b,
double x,
double y,
double z){
57 b[0] = x, b[1] = y; b[2] = z;
60 float *double2float(
float *b,
const double *a,
int n){
62 for(i=0;i<n;i++) b[i] = (
float)a[i];
65 double *float2double(
double *b,
float *a,
int n){
67 for(i=0;i<n;i++) b[i] = (
double)a[i];
70 double * vecadd2d(
double *c,
double *a,
double *b){
76 double *vecdif2d(
double *c,
double* a,
double *b){
81 double veclength2d(
double *p ){
82 return sqrt(p[0]*p[0] + p[1]*p[1]);
84 double vecdot2d(
double *a,
double *b){
85 return a[0]*b[0] + a[1]*b[1];
88 double* vecscale2d(
double* r,
double* v,
double s){
94 double vecnormal2d(
double *r,
double *v){
95 double ret = sqrt(vecdot2d(v,v));
97 if (APPROX(ret, 0))
return 0.;
98 vecscale2d(r,v,1./ret);
102 float * vecadd2f(
float *c,
float *a,
float *b){
108 float *vecdif2f(
float *c,
float* a,
float *b){
113 float veclength2f(
float *p ){
114 return (
float) sqrt(p[0]*p[0] + p[1]*p[1]);
116 float vecdot2f(
float *a,
float *b){
117 return a[0]*b[0] + a[1]*b[1];
120 float* vecscale2f(
float* r,
float* v,
float s){
126 float vecnormal2f(
float *r,
float *v){
127 float ret = (float)sqrt(vecdot2f(v,v));
129 if (APPROX(ret, 0))
return 0.0f;
130 vecscale2f(r,v,1.0f/ret);
138 double *vecaddd(
double *c,
double* a,
double *b)
145 float *vecadd3f(
float *c,
float *a,
float *b)
152 double *vecdifd(
double *c,
double* a,
double *b)
159 float *vecdif3f(
float *c,
float *a,
float *b)
166 double *veccopyd(
double *c,
double *a)
173 double *vecnegated(
double *b,
double *a)
180 float *veccopy3f(
float *b,
float *a)
187 int vecsame2f(
float *b,
float *a){
188 return a[0] == b[0] && a[1] == b[1] ? TRUE : FALSE;
190 float *veccopy2f(
float *b,
float *a)
196 float *vecset2f(
float *b,
float x,
float y)
201 double * veccrossd(
double *c,
double *a,
double *b)
206 c[0] = aa[1] * bb[2] - aa[2] * bb[1];
207 c[1] = aa[2] * bb[0] - aa[0] * bb[2];
208 c[2] = aa[0] * bb[1] - aa[1] * bb[0];
211 double veclengthd(
double *p )
213 return sqrt(p[0]*p[0] + p[1]*p[1] + p[2]*p[2]);
215 double vecdotd(
double *a,
double *b)
217 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
219 double* vecscaled(
double* r,
double* v,
double s)
227 double vecnormald(
double *r,
double *v)
229 double ret = sqrt(vecdotd(v,v));
231 if (APPROX(ret, 0))
return 0.;
232 vecscaled(r,v,1./ret);
238 c->x = a.y * b.z - a.z * b.y;
239 c->y = a.z * b.x - a.x * b.z;
240 c->z = a.x * b.y - a.y * b.x;
242 float *veccross3f(
float *c,
float *a,
float *b)
245 c[0] = a[1]*b[2] - a[2]*b[1];
246 c[1] = a[2]*b[0] - a[0]*b[2];
247 c[2] = a[0]*b[1] - a[1]*b[0];
252 return (
float) sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
254 float vecdot3f(
float *a,
float *b )
257 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
259 float vecdot4f(
float *a,
float *b )
261 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + + a[3]*b[3];
263 float *vecset3f(
float *b,
float x,
float y,
float z)
265 b[0] = x; b[1] = y; b[2] = z;
268 float veclength3f(
float *a){
269 return (
float)sqrt(vecdot3f(a, a));
273 return acos((V1->x*V2->x + V1->y*V2->y +V1->z*V2->z) /
274 sqrt( (V1->x*V1->x + V1->y*V1->y + V1->z*V1->z)*(V2->x*V2->x + V2->y*V2->y + V2->z*V2->z) ) );
277 float *veccopy4f(
float *b,
float *a)
288 float length_a, length_b, scalar, temp;
289 scalar = (float) calc_vector_scalar_product(a,b);
290 length_a = calc_vector_length(a);
291 length_b = calc_vector_length(b);
296 if (APPROX(scalar, 0)) {
297 return (
float) (M_PI/2.0);
300 if ( (length_a <= 0) || (length_b <= 0)){
301 printf(
"Divide by 0 in calc_angle_between_two_vectors(): No can do! \n");
305 temp = scalar /(length_a * length_b);
310 if ((temp >= 1) || (temp <= -1)){
311 if (temp < 0.0f)
return 3.141526f;
314 return (
float) acos(temp);
317 int vecsame3f(
float *a,
float *b){
320 if(a[i] != b[i]) isame = FALSE;
323 int vecsame4f(
float *a,
float *b){
326 if(a[i] != b[i]) isame = FALSE;
332 GLDOUBLE ret = sqrt(vecdot(v,v));
334 if (APPROX(ret, 0))
return 0.;
335 vecscale(r,v,1./ret);
338 float vecnormsquared3f(
float *a){
339 return a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
341 float *vecscale3f(
float *b,
float *a,
float scale){
347 float *vecscale4f(
float *b,
float *a,
float scale){
354 float *vecmult3f(
float *c,
float *a,
float *b){
357 for(;i<3;i++) c[i] = a[i]*b[i];
360 float *vecmult2f(
float *c,
float *a,
float *b){
363 for(;i<2;i++) c[i] = a[i]*b[i];
367 float *vecnormalize3f(
float *b,
float *a)
369 float norm = veclength3f(a);
370 if (APPROX(norm, 0.0f)){
371 b[0] = 1.0f; b[1] = 0.0f; b[2] = 0.0f;
373 vecscale3f(b, a, 1.0f / norm);
379 GLDOUBLE det3x3(GLDOUBLE* data)
381 return -data[1]*data[10]*data[4] +data[0]*data[10]*data[5] -data[2]*data[5]*data[8] +data[1]*data[6]*data[8] +data[2]*data[4]*data[9] -data[0]*data[6]*data[9];
383 float det3f(
float *a,
float *b,
float *c)
387 return vecdot3f(a,veccross3f(temp,b, c));
397 r->x = b[0]*a->x +b[4]*a->y +b[8]*a->z +b[12];
398 r->y = b[1]*a->x +b[5]*a->y +b[9]*a->z +b[13];
399 r->z = b[2]*a->x +b[6]*a->y +b[10]*a->z +b[14];
402 tmp.x = a->x; tmp.y = a->y; tmp.z = a->z;
403 r->x = b[0]*tmp.x +b[4]*tmp.y +b[8]*tmp.z +b[12];
404 r->y = b[1]*tmp.x +b[5]*tmp.y +b[9]*tmp.z +b[13];
405 r->z = b[2]*tmp.x +b[6]*tmp.y +b[10]*tmp.z +b[14];
409 GLDOUBLE* transformUPPER3X3d(
double *r,
double *a,
const GLDOUBLE* b){
412 r[0] = b[0]*tmp[0] +b[4]*tmp[1] +b[8]*tmp[2];
413 r[1] = b[1]*tmp[0] +b[5]*tmp[1] +b[9]*tmp[2];
414 r[2] = b[2]*tmp[0] +b[6]*tmp[1] +b[10]*tmp[2];
420 return transform(r,a,b);
422 GLDOUBLE* pointxyz2double(
double* r,
struct point_XYZ *p){
423 r[0] = p->x; r[1] = p->y; r[2] = p->z;
427 r->x = p[0]; r->y = p[1]; r->z = p[2];
430 double *transformAFFINEd(
double *r,
double *a,
const GLDOUBLE* mat){
433 double2pointxyz(&pa,a);
434 transformAFFINE(&pr,&pa,mat);
435 pointxyz2double(r,&pr);
438 double *transformFULL4d(
double *r,
double *a,
double *mat){
441 for (i=0; i<4; i++) {
450 float* transformf(
float* r,
const float* a,
const GLDOUBLE* b)
456 r[0] = (float) (b[0]*a[0] +b[4]*a[1] +b[8]*a[2] +b[12]);
457 r[1] = (float) (b[1]*a[0] +b[5]*a[1] +b[9]*a[2] +b[13]);
458 r[2] = (float) (b[2]*a[0] +b[6]*a[1] +b[10]*a[2] +b[14]);
460 tmp[0] =a[0]; tmp[1] = a[1]; tmp[2] = a[2];
461 r[0] = (float) (b[0]*tmp[0] +b[4]*tmp[1] +b[8]*tmp[2] +b[12]);
462 r[1] = (float) (b[1]*tmp[0] +b[5]*tmp[1] +b[9]*tmp[2] +b[13]);
463 r[2] = (float) (b[2]*tmp[0] +b[6]*tmp[1] +b[10]*tmp[2] +b[14]);
467 float* matmultvec4f(
float* r4,
float *mat4,
float* a4 )
471 memcpy(t4,a4,4*
sizeof(
float));
476 r4[i] += b[i][j]*t4[j];
480 float* vecmultmat4f_broken(
float* r4,
float* a4,
float *mat4 )
484 memcpy(t4,a4,4*
sizeof(
float));
489 r4[i] += t4[j]*b[j][i];
493 float* vecmultmat4f(
float* r4,
float* a4,
float *mat4 )
497 memcpy(t4,a4,4*
sizeof(
float));
506 float* matmultvec3f(
float* r3,
float *mat3,
float* a3 )
510 memcpy(t3,a3,3*
sizeof(
float));
515 r3[i] += b[i][j]*t3[j];
519 float* vecmultmat3f(
float* r3,
float* a3,
float *mat3 )
523 memcpy(t3,a3,3*
sizeof(
float));
528 r3[i] += t3[j]*b[j][i];
540 r->x = b[0]*a->x +b[4]*a->y +b[8]*a->z;
541 r->y = b[1]*a->x +b[5]*a->y +b[9]*a->z;
542 r->z = b[2]*a->x +b[6]*a->y +b[10]*a->z;
545 tmp.x = a->x; tmp.y = a->y; tmp.z = a->z;
546 r->x = b[0]*tmp.x +b[4]*tmp.y +b[8]*tmp.z;
547 r->y = b[1]*tmp.x +b[5]*tmp.y +b[9]*tmp.z;
548 r->z = b[2]*tmp.x +b[6]*tmp.y +b[10]*tmp.z;
562 return (a->x*b->x) + (a->y*b->y) + (a->z*b->z);
581 double closest_point_of_segment_to_y_axis_segment(
double y1,
double y2,
struct point_XYZ p1,
struct point_XYZ p2) {
583 double imin = (y1- p1.y) / (p2.y - p1.y);
584 double imax = (y2- p1.y) / (p2.y - p1.y);
587 double x12 = (p1.x - p2.x);
588 double z12 = (p1.z - p2.z);
589 double q = ( x12*x12 + z12*z12 );
592 double i = ((APPROX(q, 0)) ? 0 : (p1.x * x12 + p1.z * z12) / q);
594 printf(
"imin=%f, imax=%f => ",imin,imax);
603 if(imin < 0) imin = 0;
604 if(imax > 1) imax = 1;
606 printf(
"imin=%f, imax=%f\n",imin,imax);
609 if(i < imin) i = imin;
610 if(i > imax) i = imax;
614 BOOL line_intersect_line_3f(
float *p1,
float *v1,
float *p2,
float *v2,
float *t,
float *s,
float *x1,
float *x2)
622 float t1[3], cross[3];
623 float crosslength2, ss, tt;
624 veccross3f(cross, v1, v2);
625 crosslength2 = vecnormsquared3f(cross);
626 if (APPROX(crosslength2, 0.0f))
return FALSE;
627 crosslength2 = 1.0f / crosslength2;
628 tt = det3f(vecdif3f(t1, p2, p1), v2, cross) * crosslength2;
629 ss = det3f(vecdif3f(t1, p2, p1), v1, cross) * crosslength2;
631 vecadd3f(x1, p1, vecscale3f(t1, v1, tt));
633 vecadd3f(x2, p2, vecscale3f(t1, v2, ss));
641 BOOL line_intersect_planed_3f(
float *p,
float *v,
float *N,
float d,
float *pi,
float *t)
647 float t1[3], t2[3], nd, tt;
649 if (APPROX(nd, 0.0f))
return FALSE;
650 tt = -(d + vecdot3f(N, p)) / nd;
651 vecadd3f(t2, p, vecscale3f(t1, v, tt));
653 if (pi) veccopy3f(pi, t2);
656 BOOL line_intersect_plane_3f(
float *p,
float *v,
float *N,
float *pp,
float *pi,
float *t)
660 return line_intersect_planed_3f(p, v, N, d, pi, t);
663 BOOL line_intersect_cylinder_3f(
float *p,
float *v,
float radius,
float *pi)
672 float a = dx*dx + dz*dz;
673 float b = 2.0f*(dx * p[0] + dz * p[2]);
674 float c = p[0] * p[0] + p[2] * p[2] - radius*radius;
676 if (APPROX(a, 0.0f))
return FALSE;
682 float sol2 = (-b-(float) sqrt(und))/2;
684 vecadd3f(pp, p, vecscale3f(t2, v, sol));
685 if (pi) veccopy3f(pi, pp);
715 if(fabs(n.x) <= fabs(n.y) && fabs(n.x) <= fabs(n.z)) {
720 j->x = n.y*n.y + n.z*n.z;
723 }
else if(fabs(n.y) <= fabs(n.x) && fabs(n.y) <= fabs(n.z)) {
729 j->y = n.x*n.x + n.z*n.z;
738 j->z = n.x*n.x + n.y*n.y;
743 GLDOUBLE* mattranspose(GLDOUBLE* res, GLDOUBLE* mm)
751 memcpy(mcpy,m,
sizeof(GLDOUBLE)*16);
755 for (i = 0; i < 4; i++) {
759 for (j = 0; j < 4; j++) {
760 res[i*4+j] = m[j*4+i];
766 float* mattranspose4f(
float* res,
float* mm)
774 memcpy(mcpy,m,
sizeof(
float)*16);
778 for (i = 0; i < 4; i++) {
779 for (j = 0; j < 4; j++) {
780 res[i*4+j] = m[j*4+i];
785 float* mattranspose3f(
float* res,
float* mm)
793 memcpy(mcpy,m,
sizeof(
float)*9);
797 for (i = 0; i < 3; i++) {
798 for (j = 0; j < 3; j++) {
799 res[i*3+j] = m[j*3+i];
805 GLDOUBLE* matinverse98(GLDOUBLE* res, GLDOUBLE* mm)
817 memcpy(mcpy,m,
sizeof(GLDOUBLE)*16);
826 res[0] = (-m[9]*m[6] +m[5]*m[10])*Deta;
827 res[4] = (+m[8]*m[6] -m[4]*m[10])*Deta;
828 res[8] = (-m[8]*m[5] +m[4]*m[9])*Deta;
829 res[12] = ( m[12]*m[9]*m[6] -m[8]*m[13]*m[6] -m[12]*m[5]*m[10] +m[4]*m[13]*m[10] +m[8]*m[5]*m[14] -m[4]*m[9]*m[14])*Deta;
831 res[1] = (+m[9]*m[2] -m[1]*m[10])*Deta;
832 res[5] = (-m[8]*m[2] +m[0]*m[10])*Deta;
833 res[9] = (+m[8]*m[1] -m[0]*m[9])*Deta;
834 res[13] = (-m[12]*m[9]*m[2] +m[8]*m[13]*m[2] +m[12]*m[1]*m[10] -m[0]*m[13]*m[10] -m[8]*m[1]*m[14] +m[0]*m[9]*m[14])*Deta;
836 res[2] = (-m[5]*m[2] +m[1]*m[6])*Deta;
837 res[6] = (+m[4]*m[2] -m[0]*m[6])*Deta;
838 res[10] = (-m[4]*m[1] +m[0]*m[5])*Deta;
839 res[14] = ( m[12]*m[5]*m[2] -m[4]*m[13]*m[2] -m[12]*m[1]*m[6] +m[0]*m[13]*m[6] +m[4]*m[1]*m[14] -m[0]*m[5]*m[14])*Deta;
841 res[3] = (+m[5]*m[2]*m[11] -m[1]*m[6]*m[11])*Deta;
842 res[7] = (-m[4]*m[2]*m[11] +m[0]*m[6]*m[11])*Deta;
843 res[11] = (+m[4]*m[1]*m[11] -m[0]*m[5]*m[11])*Deta;
844 res[15] = (-m[8]*m[5]*m[2] +m[4]*m[9]*m[2] +m[8]*m[1]*m[6] -m[0]*m[9]*m[6] -m[4]*m[1]*m[10] +m[0]*m[5]*m[10])*Deta;
848 float* matinverse4f(
float* res,
float* mm)
858 memcpy(mcpy,m,
sizeof(
float)*16);
862 Deta = det3f(&m[0],&m[4],&m[8]);
865 res[0] = (-m[9]*m[6] +m[5]*m[10])*Deta;
866 res[4] = (+m[8]*m[6] -m[4]*m[10])*Deta;
867 res[8] = (-m[8]*m[5] +m[4]*m[9])*Deta;
868 res[12] = ( m[12]*m[9]*m[6] -m[8]*m[13]*m[6] -m[12]*m[5]*m[10] +m[4]*m[13]*m[10] +m[8]*m[5]*m[14] -m[4]*m[9]*m[14])*Deta;
870 res[1] = (+m[9]*m[2] -m[1]*m[10])*Deta;
871 res[5] = (-m[8]*m[2] +m[0]*m[10])*Deta;
872 res[9] = (+m[8]*m[1] -m[0]*m[9])*Deta;
873 res[13] = (-m[12]*m[9]*m[2] +m[8]*m[13]*m[2] +m[12]*m[1]*m[10] -m[0]*m[13]*m[10] -m[8]*m[1]*m[14] +m[0]*m[9]*m[14])*Deta;
875 res[2] = (-m[5]*m[2] +m[1]*m[6])*Deta;
876 res[6] = (+m[4]*m[2] -m[0]*m[6])*Deta;
877 res[10] = (-m[4]*m[1] +m[0]*m[5])*Deta;
878 res[14] = ( m[12]*m[5]*m[2] -m[4]*m[13]*m[2] -m[12]*m[1]*m[6] +m[0]*m[13]*m[6] +m[4]*m[1]*m[14] -m[0]*m[5]*m[14])*Deta;
880 res[3] = (+m[5]*m[2]*m[11] -m[1]*m[6]*m[11])*Deta;
881 res[7] = (-m[4]*m[2]*m[11] +m[0]*m[6]*m[11])*Deta;
882 res[11] = (+m[4]*m[1]*m[11] -m[0]*m[5]*m[11])*Deta;
883 res[15] = (-m[8]*m[5]*m[2] +m[4]*m[9]*m[2] +m[8]*m[1]*m[6] -m[0]*m[9]*m[6] -m[4]*m[1]*m[10] +m[0]*m[5]*m[10])*Deta;
910 return polynormal(r,pp+0,pp+1,pp+2);
914 GLDOUBLE* matrotate(GLDOUBLE* Result,
double Theta,
double x,
double y,
double z)
916 GLDOUBLE CosTheta = cos(Theta);
917 GLDOUBLE SinTheta = sin(Theta);
919 Result[0] = x*x + (y*y+z*z)*CosTheta;
920 Result[1] = x*y - x*y*CosTheta + z*SinTheta;
921 Result[2] = x*z - x*z*CosTheta - y*SinTheta;
922 Result[4] = x*y - x*y*CosTheta - z*SinTheta;
923 Result[5] = y*y + (x*x+z*z)*CosTheta;
924 Result[6] = z*y - z*y*CosTheta + x*SinTheta;
925 Result[8] = z*x - z*x*CosTheta + y*SinTheta;
926 Result[9] = z*y - z*y*CosTheta - x*SinTheta;
927 Result[10]= z*z + (x*x+y*y)*CosTheta;
939 GLDOUBLE* mattranslate(GLDOUBLE* r,
double dx,
double dy,
double dz)
941 r[0] = r[5] = r[10] = r[15] = 1;
942 r[1] = r[2] = r[3] = r[4] =
943 r[6] = r[7] = r[8] = r[9] =
951 GLDOUBLE* matmultiplyFULL(GLDOUBLE* r, GLDOUBLE* mm , GLDOUBLE* nn)
957 GLDOUBLE tm[16],tn[16];
964 memcpy(tm,m,
sizeof(GLDOUBLE)*16);
968 memcpy(tn,n,
sizeof(GLDOUBLE)*16);
972 if(1)
for(i=0;i<3;i++){
973 if(mm[i +12] != 0.0){
974 double p = mm[i +12];
975 printf(
"Ft[%d]%f ",i,p);
977 if(nn[i + 12] != 0.0){
978 double p = nn[i +12];
979 printf(
"FT[%d]%f ",i,p);
982 if(1)
for(i=0;i<3;i++){
983 if(mm[i*4 + 3] != 0.0){
984 double p = mm[i*4 + 3];
985 printf(
"Fp[%d]%f ",i,p);
987 if(nn[i*4 + 3] != 0.0){
988 double p = nn[i*4 + 3];
989 printf(
"FP[%d]%f ",i,p);
999 r[i*4+j] += m[i*4+k]*n[k*4+j];
1004 GLDOUBLE* matmultiplyAFFINE(GLDOUBLE* r, GLDOUBLE* nn , GLDOUBLE* mm)
1010 GLDOUBLE tm[16],tn[16];
1017 memcpy(tm,m,
sizeof(GLDOUBLE)*16);
1021 memcpy(tn,n,
sizeof(GLDOUBLE)*16);
1025 if(0)
for(i=0;i<3;i++){
1026 if(mm[i +12] != 0.0){
1027 double p = mm[i +12];
1028 printf(
"At[%d]%lf ",i,p);
1030 if(nn[i + 12] != 0.0){
1031 double p = nn[i +12];
1032 printf(
"AT[%d]%lf ",i,p);
1035 if(1)
for(i=0;i<3;i++){
1036 if(mm[i*4 + 3] != 0.0){
1037 double p = mm[i*4 + 3];
1038 printf(
"Ap[%d]%lf ",i,p);
1040 if(nn[i*4 + 3] != 0.0){
1041 double p = nn[i*4 + 3];
1042 printf(
"AP[%d]%lf ",i,p);
1049 r[0] = m[0]*n[0] +m[4]*n[1] +m[8]*n[2];
1050 r[4] = m[0]*n[4] +m[4]*n[5] +m[8]*n[6];
1051 r[8] = m[0]*n[8] +m[4]*n[9] +m[8]*n[10];
1052 r[12]= m[0]*n[12]+m[4]*n[13]+m[8]*n[14] +m[12];
1054 r[1] = m[1]*n[0] +m[5]*n[1] +m[9]*n[2];
1055 r[5] = m[1]*n[4] +m[5]*n[5] +m[9]*n[6];
1056 r[9] = m[1]*n[8] +m[5]*n[9] +m[9]*n[10];
1057 r[13]= m[1]*n[12]+m[5]*n[13]+m[9]*n[14] +m[13];
1059 r[2] = m[2]*n[0]+ m[6]*n[1] +m[10]*n[2];
1060 r[6] = m[2]*n[4]+ m[6]*n[5] +m[10]*n[6];
1061 r[10]= m[2]*n[8]+ m[6]*n[9] +m[10]*n[10];
1062 r[14]= m[2]*n[12]+m[6]*n[13]+m[10]*n[14] +m[14];
1064 r[3] = r[7] = r[11] = 0.0;
1069 GLDOUBLE* matmultiply(GLDOUBLE* r, GLDOUBLE* mm , GLDOUBLE* nn){
1070 return matmultiplyFULL(r,mm,nn);
1073 float* matmultiply4f(
float* r,
float* mm ,
float* nn)
1078 float tm[16],tn[16];
1085 memcpy(tm,m,
sizeof(
float)*16);
1089 memcpy(tn,n,
sizeof(
float)*16);
1098 r[i*4+j] += m[i*4+k]*n[k*4+j];
1102 float* matmultiply3f(
float* r,
float* mm ,
float* nn)
1114 memcpy(tm,m,
sizeof(
float)*9);
1118 memcpy(tn,n,
sizeof(
float)*9);
1127 r[i*3+j] += m[i*3+k]*n[k*3+j];
1131 float *axisangle_rotate3f(
float* b,
float *a,
float *axisangle)
1139 float cosine, sine, cross[3], dot, theta, *axis, t1[3], t2[3],t3[3],t4[3];
1140 theta = axisangle[3];
1142 cosine = (float)cos(theta);
1143 sine = (float)sin(theta);
1144 veccross3f(cross,axis, a);
1145 dot = vecdot3f(axis, a);
1146 vecadd3f(b,vecscale3f(t1, a, cosine), vecadd3f(t2, vecscale3f(t3, cross, sine), vecscale3f(t4, axis, dot*(1.0f - cosine))));
1149 float *matidentity4f(
float *b){
1161 float *matidentity3f(
float *b){
1164 for(i=0;i<9;i++) b[i] = 0.0f;
1165 for(i=0;i<3;i++) b[i*3 +i] = 1.0f;
1168 float *axisangle2matrix4f(
float *b,
float *axisangle){
1177 axisangle_rotate3f(mat[i], mat[i], axisangle);
1182 void matrixFromAxisAngle4d(
double *mat,
double rangle,
double x,
double y,
double z)
1201 for(i=0;i<16;i++) mat[i] = 0.0;
1202 for(i=0;i<4;i++) m[i][i] = 1.0;
1210 m[0][0] = c + x*x*t;
1211 m[1][1] = c + y*y*t;
1212 m[2][2] = c + z*z*t;
1217 m[1][0] = tmp1 + tmp2;
1218 m[0][1] = tmp1 - tmp2;
1221 m[2][0] = tmp1 - tmp2;
1222 m[0][2] = tmp1 + tmp2;
1225 m[2][1] = tmp1 + tmp2;
1226 m[1][2] = tmp1 - tmp2;
1230 void rotate_v2v_axisAngled(
double* axis,
double* angle,
double *orig,
double *result)
1233 double dv[3], iv[3], cv[3];
1248 vecnormald(dv,orig);
1249 vecnormald(iv,result);
1251 veccrossd(cv,dv,iv);
1252 cvl = vecnormald(cv,cv);
1256 if(APPROX(cvl, 0)) {
1261 *angle = atan2(cvl,vecdotd(dv,iv));
1272 veccross(&cv,dv,iv);
1273 cvl = vecnormal(&cv,&cv);
1275 if(APPROX(cvl, 0)) {
1280 a = atan2(cvl,vecdot(&dv,&iv));
1282 matrotate(res,a,cv.x,cv.y,cv.z);
1287 #define SHOW_NONSINGULARS 0 //or 1 for noisy
1296 BOOL matrix3x3_inverse_float(
float *inn,
float *outt)
1302 float *in[3], *out[3];
1311 #define ACCUMULATE pos += temp;
1327 temp = in[0][0] * in[1][1] * in[2][2];
1329 temp = in[0][1] * in[1][2] * in[2][0];
1331 temp = in[0][2] * in[1][0] * in[2][1];
1333 temp = -in[0][2] * in[1][1] * in[2][0];
1335 temp = -in[0][1] * in[1][0] * in[2][2];
1337 temp = -in[0][0] * in[1][2] * in[2][1];
1343 if(APPROX(det_1,0.0f)){
1347 if(SHOW_NONSINGULARS) fprintf (stderr,
"affine_matrix4_inverse: singular matrix\n");
1354 det_1 = 1.0f / det_1;
1355 out[0][0] = (in[1][1] * in[2][2] - in[1][2] * in[2][1] ) * det_1;
1356 out[1][0] = -(in[1][0] * in[2][2] - in[1][2] * in[2][0] ) * det_1;
1357 out[2][0] = (in[1][0] * in[2][1] - in[1][1] * in[2][0] ) * det_1;
1358 out[0][1] = -(in[0][1] * in[2][2] - in[0][2] * in[2][1] ) * det_1;
1359 out[1][1] = (in[0][0] * in[2][2] - in[0][2] * in[2][0] ) * det_1;
1360 out[2][1] = -(in[0][0] * in[2][1] - in[0][1] * in[2][0] ) * det_1;
1361 out[0][2] = (in[0][1] * in[1][2] - in[0][2] * in[1][1] ) * det_1;
1362 out[1][2] = -(in[0][0] * in[1][2] - in[0][2] * in[1][0] ) * det_1;
1363 out[2][2] = (in[0][0] * in[1][1] - in[0][1] * in[1][0] ) * det_1;
1368 float * mat423f(
float *out3x3,
float *in4x4)
1373 out3x3[i*3 + j] = in4x4[i*4 + j];
1377 float * matinverse3f(
float *out3x3,
float *in3x3)
1379 matrix3x3_inverse_float(in3x3,out3x3);
1383 float * transform3x3f(
float *out3,
float *in3,
float *mat3x3){
1386 memcpy(t3,in3,3*
sizeof(
float));
1390 out3[i] += t3[j]*mat3x3[j*3 + i];
1396 BOOL affine_matrix4x4_inverse_float(
float *inn,
float *outt)
1405 float *in[4], *out[4];
1414 #define ACCUMULATE pos += temp;
1432 temp = in[0][0] * in[1][1] * in[2][2];
1434 temp = in[0][1] * in[1][2] * in[2][0];
1436 temp = in[0][2] * in[1][0] * in[2][1];
1438 temp = -in[0][2] * in[1][1] * in[2][0];
1440 temp = -in[0][1] * in[1][0] * in[2][2];
1442 temp = -in[0][0] * in[1][2] * in[2][1];
1448 if(APPROX(det_1,0.0f)){
1451 if(SHOW_NONSINGULARS) fprintf (stderr,
"affine_matrix4_inverse: singular matrix\n");
1458 det_1 = 1.0f / det_1;
1459 out[0][0] = (in[1][1] * in[2][2] - in[1][2] * in[2][1] ) * det_1;
1460 out[1][0] = -(in[1][0] * in[2][2] - in[1][2] * in[2][0] ) * det_1;
1461 out[2][0] = (in[1][0] * in[2][1] - in[1][1] * in[2][0] ) * det_1;
1462 out[0][1] = -(in[0][1] * in[2][2] - in[0][2] * in[2][1] ) * det_1;
1463 out[1][1] = (in[0][0] * in[2][2] - in[0][2] * in[2][0] ) * det_1;
1464 out[2][1] = -(in[0][0] * in[2][1] - in[0][1] * in[2][0] ) * det_1;
1465 out[0][2] = (in[0][1] * in[1][2] - in[0][2] * in[1][1] ) * det_1;
1466 out[1][2] = -(in[0][0] * in[1][2] - in[0][2] * in[1][0] ) * det_1;
1467 out[2][2] = (in[0][0] * in[1][1] - in[0][1] * in[1][0] ) * det_1;
1470 out[3][0] = -(in[3][0] * out[0][0] + in[3][1]*out[1][0] + in[3][2]*out[2][0]);
1471 out[3][1] = -(in[3][0] * out[0][1] + in[3][1]*out[1][1] + in[3][2]*out[2][1]);
1472 out[3][2] = -(in[3][0] * out[0][2] + in[3][1]*out[1][2] + in[3][2]*out[2][2]);
1475 out[0][3] = out[1][3] = out[2][3] = 0.0f;
1481 BOOL affine_matrix4x4_inverse_double(
double *inn,
double *outt)
1489 double *in[4], *out[4];
1498 #define ACCUMULATE pos += temp;
1516 temp = in[0][0] * in[1][1] * in[2][2];
1518 temp = in[0][1] * in[1][2] * in[2][0];
1520 temp = in[0][2] * in[1][0] * in[2][1];
1522 temp = -in[0][2] * in[1][1] * in[2][0];
1524 temp = -in[0][1] * in[1][0] * in[2][2];
1526 temp = -in[0][0] * in[1][2] * in[2][1];
1532 if(APPROX(det_1,0.0)){
1535 if(SHOW_NONSINGULARS) fprintf (stderr,
"affine_matrix4_inverse: singular matrix\n");
1542 det_1 = 1.0 / det_1;
1543 out[0][0] = (in[1][1] * in[2][2] - in[1][2] * in[2][1] ) * det_1;
1544 out[1][0] = -(in[1][0] * in[2][2] - in[1][2] * in[2][0] ) * det_1;
1545 out[2][0] = (in[1][0] * in[2][1] - in[1][1] * in[2][0] ) * det_1;
1546 out[0][1] = -(in[0][1] * in[2][2] - in[0][2] * in[2][1] ) * det_1;
1547 out[1][1] = (in[0][0] * in[2][2] - in[0][2] * in[2][0] ) * det_1;
1548 out[2][1] = -(in[0][0] * in[2][1] - in[0][1] * in[2][0] ) * det_1;
1549 out[0][2] = (in[0][1] * in[1][2] - in[0][2] * in[1][1] ) * det_1;
1550 out[1][2] = -(in[0][0] * in[1][2] - in[0][2] * in[1][0] ) * det_1;
1551 out[2][2] = (in[0][0] * in[1][1] - in[0][1] * in[1][0] ) * det_1;
1554 out[3][0] = -(in[3][0] * out[0][0] + in[3][1]*out[1][0] + in[3][2]*out[2][0]);
1555 out[3][1] = -(in[3][0] * out[0][1] + in[3][1]*out[1][1] + in[3][2]*out[2][1]);
1556 out[3][2] = -(in[3][0] * out[0][2] + in[3][1]*out[1][2] + in[3][2]*out[2][2]);
1559 out[0][3] = out[1][3] = out[2][3] = 0.0;
1564 GLDOUBLE* matinverseAFFINE(GLDOUBLE* res, GLDOUBLE* mm){
1565 affine_matrix4x4_inverse_double(mm, res);
1568 GLDOUBLE* matinverseFULL(GLDOUBLE* res, GLDOUBLE* mm){
1569 matinverse98(res,mm);
1572 GLDOUBLE* matinverse(GLDOUBLE* res, GLDOUBLE* mm){
1573 matinverseFULL(res,mm);
1581 GLDOUBLE* veccross(GLDOUBLE* r, GLDOUBLE* v1, GLDOUBLE* v2)
1586 r[0] = v1[1]*v2[2] - v1[2]*v2[1];
1587 r[1] = v1[2]*v2[0] - v1[0]*v2[2];
1588 r[2] = v1[0]*v2[1] - v1[1]*v2[0];
1590 GLDOUBLE v2c[3] = {v2[0],v2[1],v2[2]};
1591 r[0] = v1[1]*v2c[2] - v1[2]*v2c[1];
1592 r[1] = v1[2]*v2c[0] - v1[0]*v2c[2];
1593 r[2] = v1[0]*v2c[1] - v1[1]*v2c[0];
1596 GLDOUBLE v1c[3] = {v1[0],v1[1],v1[2]};
1597 r[0] = v1c[1]*v2[2] - v1c[2]*v2[1];
1598 r[1] = v1c[2]*v2[0] - v1c[0]*v2[2];
1599 r[2] = v1c[0]*v2[1] - v1c[1]*v2[0];
1614 void scale_to_matrix (
double *mat,
struct point_XYZ *scale) {
1616 #define MAT00 mat[0]
1617 #define MAT01 mat[1]
1618 #define MAT02 mat[2]
1619 #if DJ_KEEP_COMPILER_WARNING
1620 #define MAT03 mat[3]
1622 #define MAT10 mat[4]
1623 #define MAT11 mat[5]
1624 #define MAT12 mat[6]
1625 #if DJ_KEEP_COMPILER_WARNING
1626 #define MAT13 mat[7]
1628 #define MAT20 mat[8]
1629 #define MAT21 mat[9]
1630 #define MAT22 mat[10]
1631 #if DJ_KEEP_COMPILER_WARNING
1632 #define MAT23 mat[11]
1633 #define MAT30 mat[12]
1634 #define MAT31 mat[13]
1635 #define MAT32 mat[14]
1636 #define MAT33 mat[15]
1650 static double identity[] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
1652 void loadIdentityMatrix (
double *mat) {
1653 memcpy((
void *)mat, (
void *)identity,
sizeof(
double)*16);
1655 double *matcopy(
double *r,
double*mat){
1656 memcpy((
void*)r, (
void*)mat,
sizeof(
double)*16);
1659 float *matdouble2float4(
float *rmat4,
double *dmat4){
1662 for (i=0; i<16; i++) {
1663 rmat4[i] = (float)dmat4[i];
1667 void printmatrix3(GLDOUBLE *mat,
char *description,
int row_major){
1669 printf(
"mat %s {\n",description);
1672 for(i = 0; i< 4; i++) {
1673 printf(
"mat [%2d-%2d] = ",i*4,(i*4)+3);
1675 printf(
" %lf ",mat[(i*4)+j]);
1682 printf(
"mat [%2d %2d %2d %2d] =",i,i+4,i+8,i+12);
1683 printf(
" %lf %lf %lf %lf\n",mat[i],mat[i+4],mat[i+8],mat[i+12]);
1688 void printmatrix2(GLDOUBLE* mat,
char* description ) {
1689 static int row_major = FALSE;
1690 printmatrix3(mat,description,row_major);
1695 void general_slerp(
double *ret,
double *p1,
double *p2,
int size,
const double t)
1706 double scale0, scale1, omega;
1713 scale0 = 0.5*(1.0 + cos(omega));
1714 scale1 = 1.0 - scale0;
1721 ret[i] = scale0 * p1[i] + scale1 * p2[i];
1724 double pret[3], pp1[3], pp2[3];
1725 pointxyz2double(pp1, p1);
1726 pointxyz2double(pp2, p2);
1727 general_slerp(pret, pp1, pp2, 3, t);
1728 double2pointxyz(ret,pret);