ViSP  2.8.0
vpArit.h
1 /****************************************************************************
2  *
3  * $Id: vpArit.h 4310 2013-07-08 08:48:34Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Le module contient les procedures arithmetiques.
36  *
37  * Authors:
38  * Jean-Luc CORRE
39  *
40  *****************************************************************************/
41 #ifndef vpArit_h
42 #define vpArit_h
43 
44 #include <visp/vpConfig.h>
45 #include <stdio.h>
46 
47 #ifndef DOXYGEN_SHOULD_SKIP_THIS
48 
49 #define ADD_COORD2(r,a,b) { (r).x = (a).x + (b).x;\
50  (r).y = (a).y + (b).y; }
51 
52 #define ADD_COORD3(r,a,b) { (r).x = (a).x + (b).x;\
53  (r).y = (a).y + (b).y;\
54  (r).z = (a).z + (b).z; }
55 
56 #define INC_COORD2(r,a) { (r).x += (a).x; (r).y += (a).y; }
57 
58 #define INC_COORD3(r,a) { (r).x += (a).x; (r).y += (a).y;\
59  (r).z += (a).z; }
60 
61 #define CROSS_PRODUCT(r,a,b) { (r).x = (a).y * (b).z - (a).z * (b).y;\
62  (r).y = (a).z * (b).x - (a).x * (b).z;\
63  (r).z = (a).x * (b).y - (a).y * (b).x; }
64 
65 #define DIF_COORD2(r,a,b) { (r).x = (a).x - (b).x;\
66  (r).y = (a).y - (b).y; }
67 
68 #define DIF_COORD3(r,a,b) { (r).x = (a).x - (b).x;\
69  (r).y = (a).y - (b).y;\
70  (r).z = (a).z - (b).z; }
71 
72 #define DOT_PRODUCT(a,b) ( ((a).x * (b).x) +\
73  ((a).y * (b).y) +\
74  ((a).z * (b).z) )
75 
76 #define LENGTH3(a) (sqrt((double) DOT_PRODUCT((a),(a))))
77 
78 #define MID_COORD3(r,a,b) { (r).x = ((a).x + (b).x) / 2.0;\
79  (r).y = ((a).y + (b).y) / 2.0;\
80  (r).z = ((a).z + (b).z) / 2.0; }
81 
82 #define MUL_COORD3(r,a,b,c) { (r).x *= (a); (r).y *= (b); (r).z *= (c); }
83 
84 #define PAR_COORD3(r,t,a,b) { (r).x = ((b).x - (a).x) * (t) + (a).x;\
85  (r).y = ((b).y - (a).y) * (t) + (a).y;\
86  (r).z = ((b).z - (a).z) * (t) + (a).z; }
87 
88 #define SET_COORD2(r,a,b) { (r).x = (a); (r).y = (b); }
89 
90 #define SET_COORD3(r,a,b,c) { (r).x = (a); (r).y = (b); (r).z = (c); }
91 
92 #define SUB_COORD2(r,a) { (r).x -= (a).x; (r).y -= (a).y; }
93 
94 #define SUB_COORD3(r,a) { (r).x -= (a).x; (r).y -= (a).y;\
95  (r).z -= (a).z; }
96 
97 #define COORD3_COL(x,y,z,m,i) ( ((x) * (m)[0][i]) +\
98  ((y) * (m)[1][i]) +\
99  ((z) * (m)[2][i]) +\
100  (m)[3][i] )
101 
102 #define COORD4_COL(x,y,z,w,m,i) ( ((x) * (m)[0][i]) +\
103  ((y) * (m)[1][i]) +\
104  ((z) * (m)[2][i]) +\
105  ((w) * (m)[3][i]) )
106 
107 #define M_POLY1(x,a,b) ((a) * (x) + (b))
108 #define M_POLY2(x,a,b,c) (M_POLY1((x),(a),(b)) * (x) + (c))
109 #define M_POLY3(x,a,b,c,d) (M_POLY2((x),(a),(b),(c)) * (x) + (d))
110 
111 
112 typedef struct {
113  int x, y;
114 } Point2i;
115 
116 typedef struct {
117  short x, y;
118 } Point2s;
119 
120 typedef struct {
121  int x, y, z;
122 } Point3i;
123 
124 typedef struct {
125  float x, y, z;
126 } Point3f;
127 
128 typedef struct {
129  float x,y,z,w;
130 } Point4f;
131 
132 typedef struct {
133  float x,y,z;
134 } Vector;
135 
136 /*
137  * MATRIX
138  * ______
139  *
140  * Matrice homogene ou non.
141  * | Rotation | 0 |
142  * Matrice non homogene = | 3x3 | 0 |
143  * |-------------| 0 |
144  * | Translation | 1 |
145  */
146 typedef float Matrix[4][4];
147 
148 #define IDENTITY_MATRIX { {1.0, 0.0, 0.0, 0.0},\
149  {0.0, 1.0, 0.0, 0.0},\
150  {0.0, 0.0, 1.0, 0.0},\
151  {0.0, 0.0, 0.0, 1.0} }
152 
153 
154 /*
155  * POSITION
156  * ________
157  *
158  * La structure "Position" definit le positionnement d'un objet.
159  * Matrice de positionnement = R.S.T
160  * avec R = Rx.Ry.Rz Matrice de rotation autour des axes (Ox,Oy,Oz),
161  * Les angles sont donnes en degres;
162  * S = Sx.Sy.Sz Matrice d'homothetie sur les axes;
163  * T = Tx.Ty.Tz Matrice de translation sur les axes.
164  */
165 typedef struct {
166  Vector rotate; /* vecteur rotation */
167  Vector scale; /* vecteur homothetie */
168  Vector translate; /* vecteur translation */
169 } AritPosition;
170 
171 #define IDENTITY_ROTATE { 0.0, 0.0, 0.0 }
172 #define IDENTITY_SCALE { 1.0, 1.0, 1.0 }
173 #define IDENTITY_TRANSLATE { 0.0, 0.0, 0.0 }
174 
175 #define IDENTITY_POSITION { IDENTITY_ROTATE,\
176  IDENTITY_SCALE,\
177  IDENTITY_TRANSLATE }
178 
179 void fprintf_matrix (FILE *fp, Matrix m);
180 void ident_matrix (Matrix m);
181 void premult_matrix (Matrix a, Matrix b);
182 void premult3_matrix (Matrix a, Matrix b);
183 void prescale_matrix (Matrix m, Vector *vp);
184 void pretrans_matrix (Matrix m, Vector *vp);
185 void postleft_matrix (Matrix m, char axis);
186 void postmult_matrix (Matrix a, Matrix b);
187 void postmult3_matrix (Matrix a, Matrix b);
188 void postscale_matrix (Matrix m, Vector *vp);
189 void posttrans_matrix (Matrix m, Vector *vp);
190 void transpose_matrix (Matrix m);
191 
192 float cosin_to_angle (float ca, float sa);
193 float norm_vector (Vector *vp);
194 void point_matrix (Point4f *p4, Point3f *p3, Matrix m);
195 void plane_norme (Vector *np, Point3f *ap, Point3f *bp, Point3f *cp);
196 void point_3D_3D (Point3f *ip, int size, Matrix m, Point3f *op);
197 void point_3D_4D (Point3f *p3, int size, Matrix m, Point4f *p4);
198 void rotate_vector (Vector *vp, float a, Vector *axis);
199 void upright_vector (Vector *vp, Vector *up);
200 
201 void Matrix_to_Position (Matrix m, AritPosition *pp);
202 void Matrix_to_Rotate (Matrix m, Vector *vp);
203 void Position_to_Matrix (AritPosition *pp, Matrix m);
204 void Rotate_to_Matrix (Vector *vp, Matrix m);
205 void Rotaxis_to_Matrix (float a, Vector *axis, Matrix m);
206 void Rotrans_to_Matrix (Vector *rp, Vector *tp, Matrix m);
207 void Scale_to_Matrix (Vector *vp, Matrix m);
208 void Translate_to_Matrix (Vector *vp, Matrix m);
209 
210 void fscanf_Point3f (Point3f *pp);
211 void fscanf_Vector (Vector *vp);
212 
213 #endif
214 #endif