40 #include <visp3/core/vpMath.h>
45 #ifndef DOXYGEN_SHOULD_SKIP_THIS
53 fprintf_matrix (FILE *fp, Matrix m)
57 fprintf (fp,
"(matrix\n");
58 for (i = 0; i < 4; i++)
59 fprintf (fp,
"\t%.4f\t%.4f\t%.4f\t%.4f\n",
60 m[i][0], m[i][1], m[i][2], m[i][3]);
70 ident_matrix (Matrix m)
72 static Matrix identity = IDENTITY_MATRIX;
75 memmove ((
char *) m, (
char *) identity,
sizeof (Matrix));
94 premult_matrix (Matrix a, Matrix b)
99 for (i = 0; i < 4; i++)
100 for (j = 0; j < 4; j++)
101 m[i][j] = b[i][0] * a[0][j] +
106 memmove ((
char *) a, (
char *) m,
sizeof (Matrix));
117 premult3_matrix (Matrix a, Matrix b)
123 memmove ((
char *) m, (
char *) a,
sizeof (Matrix));
124 for (i = 0; i < 3; i++)
125 for (j = 0; j < 4; j++)
126 a[i][j] = b[i][0] * m[0][j] +
138 prescale_matrix (Matrix m, Vector *vp)
142 for (i = 0; i < 4; i++) {
156 pretrans_matrix (Matrix m, Vector *vp)
160 for (i = 0; i < 4; i++)
161 m[3][i] += vp->x * m[0][i] + vp->y * m[1][i] + vp->z * m[2][i];
172 postleft_matrix (Matrix m,
char axis)
179 for (i = 0; i < 4; i++) m[i][0] = - m[i][0];
182 for (i = 0; i < 4; i++) m[i][1] = - m[i][1];
185 for (i = 0; i < 4; i++) m[i][2] = - m[i][2];
188 static char proc_name[] =
"postleft_matrix";
189 fprintf (stderr,
"%s: axis unknown\n", proc_name);
202 postmult_matrix (Matrix a, Matrix b)
207 for (i = 0; i < 4; i++)
208 for (j = 0; j < 4; j++)
209 m[i][j] = a[i][0] * b[0][j] +
214 memmove ((
char *) a, (
char *) m,
sizeof (Matrix));
225 postmult3_matrix (Matrix a, Matrix b)
231 memmove ((
char *) m, (
char *) a,
sizeof (Matrix));
232 for (i = 0; i < 4; i++)
233 for (j = 0; j < 3; j++)
234 a[i][j] = m[i][0] * b[0][j] +
246 postscale_matrix (Matrix m, Vector *vp)
250 for (i = 0; i < 4; i++) {
264 posttrans_matrix (Matrix m, Vector *vp)
268 for (i = 0; i < 4; i++) {
269 m[i][0] += m[i][3] * vp->x;
270 m[i][1] += m[i][3] * vp->y;
271 m[i][2] += m[i][3] * vp->z;
281 transpose_matrix (Matrix m)
286 for (i = 0; i < 4; i++)
287 for (j = 0; j < i; j++)
288 SWAP(m[i][j],m[j][i],t);
300 cosin_to_angle (
float ca,
float sa)
304 if (FABS(ca) < M_EPSILON) {
305 a = (sa > (float)0.0) ? (float)M_PI_2 : (
float)(- M_PI_2);
308 a = (float) atan((
double) (sa / ca));
309 if (ca < (
float)0.0) a += (sa > (float)0.0) ? (float)M_PI : (
float)(- M_PI);
323 cosin_to_lut (Index level,
float *coslut,
float *sinlut)
326 int i_pi_2 = TWO_POWER(level);
329 double step = M_PI_2 / (double) i_pi_2;
332 coslut[quad] = 1.0; sinlut[quad] = 0.0;
334 coslut[quad] = 0.0; sinlut[quad] = 1.0;
336 coslut[quad] = -1.0; sinlut[quad] = 0.0;
338 coslut[quad] = 0.0; sinlut[quad] = -1.0;
340 for (i = 1, a = step; i < i_pi_2; i++, a += step) {
341 float ca = (float) cos (a);
343 coslut[quad + i] = ca;
345 sinlut[quad - i] = ca;
346 sinlut[quad + i] = ca;
348 coslut[quad - i] = - ca;
349 coslut[quad + i] = - ca;
351 sinlut[quad - i] = - ca;
352 sinlut[quad + i] = - ca;
354 coslut[quad - i] = ca;
367 norm_vector (Vector *vp)
371 if ((norm = (
float) sqrt ((
double) DOT_PRODUCT(*vp,*vp))) > M_EPSILON) {
377 static char proc_name[] =
"norm_vector";
378 fprintf (stderr,
"%s: nul vector\n", proc_name);
391 plane_norme (Vector *np, Point3f *ap, Point3f *bp, Point3f *cp)
395 DIF_COORD3(u,*bp,*ap);
396 DIF_COORD3(v,*cp,*ap);
399 CROSS_PRODUCT (*np,u,v);
411 point_matrix (Point4f *p4, Point3f *p3, Matrix m)
413 float x = p3->x, y = p3->y, z = p3->z;
415 p4->x = COORD3_COL(x,y,z,m,0);
416 p4->y = COORD3_COL(x,y,z,m,1);
417 p4->z = COORD3_COL(x,y,z,m,2);
418 p4->w = COORD3_COL(x,y,z,m,3);
433 point_3D_3D (Point3f *ip,
int size, Matrix m, Point3f *op)
435 Point3f *pend = ip + size;
437 for (; ip < pend; ip++, op++) {
442 op->x = COORD3_COL(x,y,z,m,0);
443 op->y = COORD3_COL(x,y,z,m,1);
444 op->z = COORD3_COL(x,y,z,m,2);
459 point_3D_4D (Point3f *p3,
int size, Matrix m, Point4f *p4)
461 Point3f *pend = p3 + size;
463 for (; p3 < pend; p3++, p4++) {
468 p4->x = COORD3_COL(x,y,z,m,0);
469 p4->y = COORD3_COL(x,y,z,m,1);
470 p4->z = COORD3_COL(x,y,z,m,2);
471 p4->w = COORD3_COL(x,y,z,m,3);
484 rotate_vector (Vector *vp,
float a, Vector *axis)
486 Vector n, u, v, cross;
489 a *= (float)M_PI / (
float)180.0;
504 f = DOT_PRODUCT(*vp, n);
510 f = (float) cos ((
double) a);
513 CROSS_PRODUCT(cross,n,*vp);
514 f = (float) sin ((
double) a);
515 MUL_COORD3(cross,f,f,f);
520 u.z + v.z + cross.z);
532 upright_vector (Vector *vp, Vector *up)
534 if (FABS(vp->z) > M_EPSILON) {
535 up->z = - (vp->x + vp->y) / vp->z;
538 else if (FABS(vp->y) > M_EPSILON) {
539 up->y = - (vp->x + vp->z) / vp->y;
542 else if (FABS(vp->x) > M_EPSILON) {
543 up->x = - (vp->y + vp->z) / vp->x;
547 static char proc_name[] =
"upright_vector";
548 up->x = up->y = up->z = 0.0;
549 fprintf (stderr,
"%s: nul vector\n", proc_name);
563 Matrix_to_Position (Matrix m, AritPosition *pp)
565 Matrix_to_Rotate (m, &pp->rotate);
566 SET_COORD3(pp->scale,1.0,1.0,1.0);
567 SET_COORD3(pp->translate,m[3][0],m[3][1],m[3][2]);
597 Matrix_to_Rotate (Matrix m, Vector *vp)
599 float sy = - m[0][2];
600 float cy = (float) sqrt (1.0 - (
double) (sy * sy));
603 if (FABS(cy) > M_EPSILON) {
604 float sz = m[0][1] / cy;
605 float cz = m[0][0] / cy;
611 cosin_to_angle (cx, sx),
612 cosin_to_angle (cy, sy),
613 cosin_to_angle (cz, sz));
620 cosin_to_angle (cx, sx),
621 (sy > (
float)0.0) ? (
float)M_PI_2 : (
float)(- M_PI_2),
624 vp->x *= (float)180.0 / (
float)M_PI;
625 vp->y *= (float)180.0 / (
float)M_PI;
626 vp->z *= (float)180.0 / (
float)M_PI;
637 Position_to_Matrix (AritPosition *pp, Matrix m)
639 Rotate_to_Matrix (&pp->rotate, m);
640 prescale_matrix (m, &pp->scale);
641 m[3][0] = pp->translate.x;
642 m[3][1] = pp->translate.y;
643 m[3][2] = pp->translate.z;
667 Rotate_to_Matrix (Vector *vp, Matrix m)
669 float rx = vp->x * (float)M_PI / (
float)180.0,
670 ry = vp->y * (float)M_PI / (
float)180.0,
671 rz = vp->z * (float)M_PI / (
float)180.0;
672 float cx = (float) cos ((
double) rx),
673 sx = (
float) sin ((
double) rx),
674 cy = (float) cos ((
double) ry),
675 sy = (
float) sin ((
double) ry),
676 cz = (float) cos ((
double) rz),
677 sz = (
float) sin ((
double) rz);
680 m[1][0] = (sx * sy * cz) - (cx * sz);
681 m[2][0] = (cx * sy * cz) + (sx * sz);
684 m[1][1] = (sx * sy * sz) + (cx * cz);
685 m[2][1] = (cx * sy * sz) - (sx * cz);
691 m[0][3] = m[1][3] = m[2][3] = 0.0;
692 m[3][0] = m[3][1] = m[3][2] = 0.0;
720 Rotaxis_to_Matrix (
float a, Vector *axis, Matrix m)
729 a *= (float)M_PI / (
float)180.0;
731 cosa = (float) cos ((
double) a);
732 sina = (float) sin ((
double) a);
733 vera = (float)1.0 - cosa;
738 MUL_COORD3(conv,vera,vera,vera);
739 MUL_COORD3(tilde,sina,sina,sina);
741 m[0][0] = conv.x * n.x + cosa;
742 m[0][1] = conv.x * n.y + tilde.z;
743 m[0][2] = conv.x * n.z - tilde.y;
745 m[1][0] = conv.y * n.x - tilde.z;
746 m[1][1] = conv.y * n.y + cosa;
747 m[1][2] = conv.y * n.z + tilde.x;
749 m[2][0] = conv.z * n.x + tilde.y;
750 m[2][1] = conv.z * n.y - tilde.x;
751 m[2][2] = conv.z * n.z + cosa;
753 m[0][3] = m[2][3] = m[1][3] = 0.0;
754 m[3][0] = m[3][1] = m[3][2] = 0.0;
767 Rotrans_to_Matrix (Vector *rp, Vector *tp, Matrix m)
769 Rotate_to_Matrix (rp, m);
782 Scale_to_Matrix (Vector *vp, Matrix m)
797 Translate_to_Matrix (Vector *vp, Matrix m)