44 #include <visp3/core/vpMath.h>
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
54 void fprintf_matrix(FILE *fp, Matrix m)
58 fprintf(fp,
"(matrix\n");
59 for (i = 0; i < 4; i++)
60 fprintf(fp,
"\t%.4f\t%.4f\t%.4f\t%.4f\n", m[i][0], m[i][1], m[i][2], m[i][3]);
69 void ident_matrix(Matrix m)
71 static Matrix identity = IDENTITY_MATRIX;
74 memmove((
char *)m, (
char *)identity,
sizeof(Matrix));
92 void premult_matrix(Matrix a, Matrix b)
97 for (i = 0; i < 4; i++)
98 for (j = 0; j < 4; j++)
99 m[i][j] = b[i][0] * a[0][j] + b[i][1] * a[1][j] + b[i][2] * a[2][j] + b[i][3] * a[3][j];
101 memmove((
char *)a, (
char *)m,
sizeof(Matrix));
111 void premult3_matrix(Matrix a, Matrix b)
117 memmove((
char *)m, (
char *)a,
sizeof(Matrix));
118 for (i = 0; i < 3; i++)
119 for (j = 0; j < 4; j++)
120 a[i][j] = b[i][0] * m[0][j] + b[i][1] * m[1][j] + b[i][2] * m[2][j];
129 void prescale_matrix(Matrix m, Vector *vp)
133 for (i = 0; i < 4; i++) {
146 void pretrans_matrix(Matrix m, Vector *vp)
150 for (i = 0; i < 4; i++)
151 m[3][i] += vp->x * m[0][i] + vp->y * m[1][i] + vp->z * m[2][i];
161 void postleft_matrix(Matrix m,
char axis)
168 for (i = 0; i < 4; i++)
172 for (i = 0; i < 4; i++)
176 for (i = 0; i < 4; i++)
180 static char proc_name[] =
"postleft_matrix";
181 fprintf(stderr,
"%s: axis unknown\n", proc_name);
193 void postmult_matrix(Matrix a, Matrix b)
198 for (i = 0; i < 4; i++)
199 for (j = 0; j < 4; j++)
200 m[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j] + a[i][3] * b[3][j];
202 memmove((
char *)a, (
char *)m,
sizeof(Matrix));
212 void postmult3_matrix(Matrix a, Matrix b)
218 memmove((
char *)m, (
char *)a,
sizeof(Matrix));
219 for (i = 0; i < 4; i++)
220 for (j = 0; j < 3; j++)
221 a[i][j] = m[i][0] * b[0][j] + m[i][1] * b[1][j] + m[i][2] * b[2][j];
230 void postscale_matrix(Matrix m, Vector *vp)
234 for (i = 0; i < 4; i++) {
246 void posttrans_matrix(Matrix m, Vector *vp)
250 for (i = 0; i < 4; i++) {
251 m[i][0] += m[i][3] * vp->x;
252 m[i][1] += m[i][3] * vp->y;
253 m[i][2] += m[i][3] * vp->z;
262 void transpose_matrix(Matrix m)
267 for (i = 0; i < 4; i++)
268 for (j = 0; j < i; j++)
269 SWAP(m[i][j], m[j][i], t);
280 float cosin_to_angle(
float ca,
float sa)
284 if (FABS(ca) < M_EPSILON) {
285 a = (sa > (float)0.0) ? (float)M_PI_2 : (
float)(-M_PI_2);
288 a = (float)atan((
double)(sa / ca));
290 a += (sa >(
float)0.0) ? (
float)M_PI : (float)(-M_PI);
303 void cosin_to_lut(Index level,
float *coslut,
float *sinlut)
306 int i_pi_2 = TWO_POWER(level);
309 double step = M_PI_2 / (double)i_pi_2;
324 for (i = 1, a = step; i < i_pi_2; i++, a += step) {
325 float ca = (float)cos(a);
327 coslut[quad + i] = ca;
329 sinlut[quad - i] = ca;
330 sinlut[quad + i] = ca;
332 coslut[quad - i] = -ca;
333 coslut[quad + i] = -ca;
335 sinlut[quad - i] = -ca;
336 sinlut[quad + i] = -ca;
338 coslut[quad - i] = ca;
350 float norm_vector(Vector *vp)
354 if ((norm = (
float)sqrt((
double)DOT_PRODUCT(*vp, *vp))) > M_EPSILON) {
360 static char proc_name[] =
"norm_vector";
361 fprintf(stderr,
"%s: nul vector\n", proc_name);
373 void plane_norme(Vector *np, Point3f *ap, Point3f *bp, Point3f *cp)
377 DIF_COORD3(u, *bp, *ap);
378 DIF_COORD3(v, *cp, *ap);
381 CROSS_PRODUCT(*np, u, v);
392 void point_matrix(Point4f *p4, Point3f *p3, Matrix m)
394 float x = p3->x, y = p3->y, z = p3->z;
396 p4->x = COORD3_COL(x, y, z, m, 0);
397 p4->y = COORD3_COL(x, y, z, m, 1);
398 p4->z = COORD3_COL(x, y, z, m, 2);
399 p4->w = COORD3_COL(x, y, z, m, 3);
410 void point_3D_3D(Point3f *ip,
int size, Matrix m, Point3f *op)
412 Point3f *pend = ip + size;
414 for (; ip < pend; ip++, op++) {
419 op->x = COORD3_COL(x, y, z, m, 0);
420 op->y = COORD3_COL(x, y, z, m, 1);
421 op->z = COORD3_COL(x, y, z, m, 2);
432 void point_3D_4D(Point3f *p3,
int size, Matrix m, Point4f *p4)
434 Point3f *pend = p3 + size;
436 for (; p3 < pend; p3++, p4++) {
441 p4->x = COORD3_COL(x, y, z, m, 0);
442 p4->y = COORD3_COL(x, y, z, m, 1);
443 p4->z = COORD3_COL(x, y, z, m, 2);
444 p4->w = COORD3_COL(x, y, z, m, 3);
456 void rotate_vector(Vector *vp,
float a, Vector *axis)
458 Vector n, u, v, cross;
461 a *= (float)M_PI / (
float)180.0;
476 f = DOT_PRODUCT(*vp, n);
478 MUL_COORD3(u, f, f, f);
480 DIF_COORD3(v, *vp, u);
482 f = (float)cos((
double)a);
483 MUL_COORD3(v, f, f, f);
485 CROSS_PRODUCT(cross, n, *vp);
486 f = (float)sin((
double)a);
487 MUL_COORD3(cross, f, f, f);
489 SET_COORD3(*vp, u.x + v.x + cross.x, u.y + v.y + cross.y, u.z + v.z + cross.z);
500 void upright_vector(Vector *vp, Vector *up)
502 if (FABS(vp->z) > M_EPSILON) {
503 up->z = -(vp->x + vp->y) / vp->z;
506 else if (FABS(vp->y) > M_EPSILON) {
507 up->y = -(vp->x + vp->z) / vp->y;
510 else if (FABS(vp->x) > M_EPSILON) {
511 up->x = -(vp->y + vp->z) / vp->x;
515 static char proc_name[] =
"upright_vector";
516 up->x = up->y = up->z = 0.0;
517 fprintf(stderr,
"%s: nul vector\n", proc_name);
530 void Matrix_to_Position(Matrix m, AritPosition *pp)
532 Matrix_to_Rotate(m, &pp->rotate);
533 SET_COORD3(pp->scale, 1.0, 1.0, 1.0);
534 SET_COORD3(pp->translate, m[3][0], m[3][1], m[3][2]);
563 void Matrix_to_Rotate(Matrix m, Vector *vp)
566 float cy = (float)sqrt(1.0 - (
double)(sy * sy));
569 if (FABS(cy) > M_EPSILON) {
570 float sz = m[0][1] / cy;
571 float cz = m[0][0] / cy;
576 SET_COORD3(*vp, cosin_to_angle(cx, sx), cosin_to_angle(cy, sy), cosin_to_angle(cz, sz));
582 SET_COORD3(*vp, cosin_to_angle(cx, sx), (sy > (
float)0.0) ? (
float)M_PI_2 : (
float)(-M_PI_2), (
float)0.0);
584 vp->x *= (float)180.0 / (
float)M_PI;
585 vp->y *= (float)180.0 / (
float)M_PI;
586 vp->z *= (float)180.0 / (
float)M_PI;
596 void Position_to_Matrix(AritPosition *pp, Matrix m)
598 Rotate_to_Matrix(&pp->rotate, m);
599 prescale_matrix(m, &pp->scale);
600 m[3][0] = pp->translate.x;
601 m[3][1] = pp->translate.y;
602 m[3][2] = pp->translate.z;
625 void Rotate_to_Matrix(Vector *vp, Matrix m)
627 float rx = vp->x * (float)M_PI / (
float)180.0,
628 ry = vp->y * (float)M_PI / (
float)180.0, rz = vp->z * (float)M_PI / (
float)180.0;
629 float cx = (float)cos((
double)rx), sx = (
float)sin((
double)rx), cy = (float)cos((
double)ry),
630 sy = (
float)sin((
double)ry), cz = (float)cos((
double)rz), sz = (
float)sin((
double)rz);
633 m[1][0] = (sx * sy * cz) - (cx * sz);
634 m[2][0] = (cx * sy * cz) + (sx * sz);
637 m[1][1] = (sx * sy * sz) + (cx * cz);
638 m[2][1] = (cx * sy * sz) - (sx * cz);
644 m[0][3] = m[1][3] = m[2][3] = 0.0;
645 m[3][0] = m[3][1] = m[3][2] = 0.0;
672 void Rotaxis_to_Matrix(
float a, Vector *axis, Matrix m)
681 a *= (float)M_PI / (
float)180.0;
683 cosa = (float)cos((
double)a);
684 sina = (float)sin((
double)a);
685 vera = (float)1.0 - cosa;
690 MUL_COORD3(conv, vera, vera, vera);
691 MUL_COORD3(tilde, sina, sina, sina);
693 m[0][0] = conv.x * n.x + cosa;
694 m[0][1] = conv.x * n.y + tilde.z;
695 m[0][2] = conv.x * n.z - tilde.y;
697 m[1][0] = conv.y * n.x - tilde.z;
698 m[1][1] = conv.y * n.y + cosa;
699 m[1][2] = conv.y * n.z + tilde.x;
701 m[2][0] = conv.z * n.x + tilde.y;
702 m[2][1] = conv.z * n.y - tilde.x;
703 m[2][2] = conv.z * n.z + cosa;
705 m[0][3] = m[2][3] = m[1][3] = 0.0;
706 m[3][0] = m[3][1] = m[3][2] = 0.0;
718 void Rotrans_to_Matrix(Vector *rp, Vector *tp, Matrix m)
720 Rotate_to_Matrix(rp, m);
732 void Scale_to_Matrix(Vector *vp, Matrix m)
745 void Translate_to_Matrix(Vector *vp, Matrix m)