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