44 #include <visp3/core/vpMath.h> 46 #ifndef DOXYGEN_SHOULD_SKIP_THIS 53 void 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", m[i][0], m[i][1], m[i][2], m[i][3]);
68 void ident_matrix(Matrix m)
70 static Matrix identity = IDENTITY_MATRIX;
73 memmove((
char *)m, (
char *)identity,
sizeof(Matrix));
91 void premult_matrix(Matrix a, Matrix b)
96 for (i = 0; i < 4; i++)
97 for (j = 0; j < 4; j++)
98 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];
100 memmove((
char *)a, (
char *)m,
sizeof(Matrix));
110 void premult3_matrix(Matrix a, Matrix b)
116 memmove((
char *)m, (
char *)a,
sizeof(Matrix));
117 for (i = 0; i < 3; i++)
118 for (j = 0; j < 4; j++)
119 a[i][j] = b[i][0] * m[0][j] + b[i][1] * m[1][j] + b[i][2] * m[2][j];
128 void prescale_matrix(Matrix m, Vector *
vp)
132 for (i = 0; i < 4; i++) {
145 void pretrans_matrix(Matrix m, Vector *vp)
149 for (i = 0; i < 4; i++)
150 m[3][i] += vp->x * m[0][i] + vp->y * m[1][i] + vp->z * m[2][i];
160 void postleft_matrix(Matrix m,
char axis)
167 for (i = 0; i < 4; i++)
171 for (i = 0; i < 4; i++)
175 for (i = 0; i < 4; i++)
179 static char proc_name[] =
"postleft_matrix";
180 fprintf(stderr,
"%s: axis unknown\n", proc_name);
192 void postmult_matrix(Matrix a, Matrix b)
197 for (i = 0; i < 4; i++)
198 for (j = 0; j < 4; j++)
199 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];
201 memmove((
char *)a, (
char *)m,
sizeof(Matrix));
211 void postmult3_matrix(Matrix a, Matrix b)
217 memmove((
char *)m, (
char *)a,
sizeof(Matrix));
218 for (i = 0; i < 4; i++)
219 for (j = 0; j < 3; j++)
220 a[i][j] = m[i][0] * b[0][j] + m[i][1] * b[1][j] + m[i][2] * b[2][j];
229 void postscale_matrix(Matrix m, Vector *vp)
233 for (i = 0; i < 4; i++) {
245 void posttrans_matrix(Matrix m, Vector *vp)
249 for (i = 0; i < 4; i++) {
250 m[i][0] += m[i][3] * vp->x;
251 m[i][1] += m[i][3] * vp->y;
252 m[i][2] += m[i][3] * vp->z;
261 void transpose_matrix(Matrix m)
266 for (i = 0; i < 4; i++)
267 for (j = 0; j < i; j++)
268 SWAP(m[i][j], m[j][i], t);
279 float cosin_to_angle(
float ca,
float sa)
283 if (FABS(ca) < M_EPSILON) {
284 a = (sa > (float)0.0) ? (float)M_PI_2 : (
float)(-M_PI_2);
286 a = (float)atan((
double)(sa / ca));
288 a += (sa > (float)0.0) ? (float)M_PI : (
float)(-M_PI);
301 void cosin_to_lut(Index level,
float *coslut,
float *sinlut)
304 int i_pi_2 = TWO_POWER(level);
307 double step = M_PI_2 / (double)i_pi_2;
322 for (i = 1, a = step; i < i_pi_2; i++, a += step) {
323 float ca = (float)cos(a);
325 coslut[quad + i] = ca;
327 sinlut[quad - i] = ca;
328 sinlut[quad + i] = ca;
330 coslut[quad - i] = -ca;
331 coslut[quad + i] = -ca;
333 sinlut[quad - i] = -ca;
334 sinlut[quad + i] = -ca;
336 coslut[quad - i] = ca;
348 float norm_vector(Vector *vp)
352 if ((norm = (
float)sqrt((
double)DOT_PRODUCT(*vp, *vp))) > M_EPSILON) {
357 static char proc_name[] =
"norm_vector";
358 fprintf(stderr,
"%s: nul vector\n", proc_name);
370 void plane_norme(Vector *np, Point3f *ap, Point3f *bp, Point3f *cp)
374 DIF_COORD3(u, *bp, *ap);
375 DIF_COORD3(v, *cp, *ap);
378 CROSS_PRODUCT(*np, u, v);
389 void point_matrix(Point4f *p4, Point3f *p3, Matrix m)
391 float x = p3->x, y = p3->y, z = p3->z;
393 p4->x = COORD3_COL(x, y, z, m, 0);
394 p4->y = COORD3_COL(x, y, z, m, 1);
395 p4->z = COORD3_COL(x, y, z, m, 2);
396 p4->w = COORD3_COL(x, y, z, m, 3);
407 void point_3D_3D(Point3f *ip,
int size, Matrix m, Point3f *op)
409 Point3f *pend = ip + size;
411 for (; ip < pend; ip++, op++) {
416 op->x = COORD3_COL(x, y, z, m, 0);
417 op->y = COORD3_COL(x, y, z, m, 1);
418 op->z = COORD3_COL(x, y, z, m, 2);
429 void point_3D_4D(Point3f *p3,
int size, Matrix m, Point4f *p4)
431 Point3f *pend = p3 + size;
433 for (; p3 < pend; p3++, p4++) {
438 p4->x = COORD3_COL(x, y, z, m, 0);
439 p4->y = COORD3_COL(x, y, z, m, 1);
440 p4->z = COORD3_COL(x, y, z, m, 2);
441 p4->w = COORD3_COL(x, y, z, m, 3);
453 void rotate_vector(Vector *vp,
float a, Vector *axis)
455 Vector n, u, v, cross;
458 a *= (float)M_PI / (
float)180.0;
473 f = DOT_PRODUCT(*vp, n);
475 MUL_COORD3(u, f, f, f);
477 DIF_COORD3(v, *vp, u);
479 f = (float)cos((
double)a);
480 MUL_COORD3(v, f, f, f);
482 CROSS_PRODUCT(cross, n, *vp);
483 f = (float)sin((
double)a);
484 MUL_COORD3(cross, f, f, f);
486 SET_COORD3(*vp, u.x + v.x + cross.x, u.y + v.y + cross.y, u.z + v.z + cross.z);
497 void upright_vector(Vector *vp, Vector *up)
499 if (FABS(vp->z) > M_EPSILON) {
500 up->z = -(vp->x + vp->y) / vp->z;
502 }
else if (FABS(vp->y) > M_EPSILON) {
503 up->y = -(vp->x + vp->z) / vp->y;
505 }
else if (FABS(vp->x) > M_EPSILON) {
506 up->x = -(vp->y + vp->z) / vp->x;
509 static char proc_name[] =
"upright_vector";
510 up->x = up->y = up->z = 0.0;
511 fprintf(stderr,
"%s: nul vector\n", proc_name);
524 void Matrix_to_Position(Matrix m, AritPosition *pp)
526 Matrix_to_Rotate(m, &pp->rotate);
527 SET_COORD3(pp->scale, 1.0, 1.0, 1.0);
528 SET_COORD3(pp->translate, m[3][0], m[3][1], m[3][2]);
557 void Matrix_to_Rotate(Matrix m, Vector *vp)
560 float cy = (float)sqrt(1.0 - (
double)(sy * sy));
563 if (FABS(cy) > M_EPSILON) {
564 float sz = m[0][1] / cy;
565 float cz = m[0][0] / cy;
570 SET_COORD3(*vp, cosin_to_angle(cx, sx), cosin_to_angle(cy, sy), cosin_to_angle(cz, sz));
575 SET_COORD3(*vp, cosin_to_angle(cx, sx), (sy > (
float)0.0) ? (
float)M_PI_2 : (
float)(-M_PI_2), (
float)0.0);
577 vp->x *= (float)180.0 / (
float)M_PI;
578 vp->y *= (float)180.0 / (
float)M_PI;
579 vp->z *= (float)180.0 / (
float)M_PI;
589 void Position_to_Matrix(AritPosition *pp, Matrix m)
591 Rotate_to_Matrix(&pp->rotate, m);
592 prescale_matrix(m, &pp->scale);
593 m[3][0] = pp->translate.x;
594 m[3][1] = pp->translate.y;
595 m[3][2] = pp->translate.z;
618 void Rotate_to_Matrix(Vector *vp, Matrix m)
620 float rx = vp->x * (float)M_PI / (
float)180.0,
621 ry = vp->y * (float)M_PI / (
float)180.0, rz = vp->z * (float)M_PI / (
float)180.0;
622 float cx = (float)cos((
double)rx), sx = (
float)sin((
double)rx), cy = (float)cos((
double)ry),
623 sy = (
float)sin((
double)ry), cz = (float)cos((
double)rz), sz = (
float)sin((
double)rz);
626 m[1][0] = (sx * sy * cz) - (cx * sz);
627 m[2][0] = (cx * sy * cz) + (sx * sz);
630 m[1][1] = (sx * sy * sz) + (cx * cz);
631 m[2][1] = (cx * sy * sz) - (sx * cz);
637 m[0][3] = m[1][3] = m[2][3] = 0.0;
638 m[3][0] = m[3][1] = m[3][2] = 0.0;
665 void Rotaxis_to_Matrix(
float a, Vector *axis, Matrix m)
674 a *= (float)M_PI / (
float)180.0;
676 cosa = (float)cos((
double)a);
677 sina = (float)sin((
double)a);
678 vera = (float)1.0 - cosa;
683 MUL_COORD3(conv, vera, vera, vera);
684 MUL_COORD3(tilde, sina, sina, sina);
686 m[0][0] = conv.x * n.x + cosa;
687 m[0][1] = conv.x * n.y + tilde.z;
688 m[0][2] = conv.x * n.z - tilde.y;
690 m[1][0] = conv.y * n.x - tilde.z;
691 m[1][1] = conv.y * n.y + cosa;
692 m[1][2] = conv.y * n.z + tilde.x;
694 m[2][0] = conv.z * n.x + tilde.y;
695 m[2][1] = conv.z * n.y - tilde.x;
696 m[2][2] = conv.z * n.z + cosa;
698 m[0][3] = m[2][3] = m[1][3] = 0.0;
699 m[3][0] = m[3][1] = m[3][2] = 0.0;
711 void Rotrans_to_Matrix(Vector *rp, Vector *tp, Matrix m)
713 Rotate_to_Matrix(rp, m);
725 void Scale_to_Matrix(Vector *vp, Matrix m)
738 void Translate_to_Matrix(Vector *vp, Matrix m)