1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 1998-2001 - ENPC - Jean-Philippe Chancelier
* Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
/*------------------------------------------------------------------------
* Graphic library
--------------------------------------------------------------------------*/
#include "math_graphics.h"
/*
* we use spConfig.h for machine constants
* XXX : spConfig should be merged and unified
* with other machine constant scilab code
*/
#define spINSIDE_SPARSE
#include "../../sparse/includes/spConfig.h"
double Mini(const double vect[], int n)
{
int i = 0;
double vmin = LARGEST_REAL;
for (i = 0 ; i < n ; i++)
{
/* if (isinf(vect[i])== 0 && isnan(vect[i])==0 && vect[i] < vmin) */
if (finite(vect[i]) == 1 && vect[i] < vmin)
{
vmin = vect[i];
}
}
return vmin;
}
double Maxi(const double vect[], int n)
{
int i = 0;
double maxi = - LARGEST_REAL;
for (i = 0 ; i < n ; i++)
{
/* if (isinf(vect[i])== 0 && isnan(vect[i])==0 && vect[i] > maxi) */
if (finite(vect[i]) == 1 && vect[i] > maxi)
{
maxi = vect[i];
}
}
return maxi;
}
void MiniMaxi(const double vect[], int n, double * const min, double * const max)
{
int i = 0;
double _min = LARGEST_REAL;
double _max = -LARGEST_REAL;
for (; i < n ; i++)
{
/* if ( isinf(vect[i])== 0 && isnan(vect[i])==0 && vect[i] < vmin) */
if (finite(vect[i]) == 1)
{
if (vect[i] < _min)
{
_min = vect[i];
}
if (vect[i] > _max)
{
_max = vect[i];
}
}
}
*min = _min;
*max = _max;
}
/*----------------------------------------------------------------------------*/
/* perform the rotation of point from to point dest */
void rotate2D(double from[2], double center[2], double angle, double dest[2])
{
rotate2Dim(from, center, cos(angle), sin(angle), dest);
}
/*----------------------------------------------------------------------------*/
/* perform the rotation of point from to point to. */
/* the angle is directly given with its sine and cosine for speed */
void rotate2Dim(double from[2] ,
double center[2] ,
double cosAngle ,
double sinAngle ,
double dest[2])
{
double diff[2];
/* put the center to (0,0) */
diff[0] = from[0] - center[0];
diff[1] = from[1] - center[1];
/* turn and translate back */
dest[0] = diff[0] * cosAngle - diff[1] * sinAngle + center[0];
dest[1] = diff[0] * sinAngle + diff[1] * cosAngle + center[1];
}
/*----------------------------------------------------------------------------*/
/* perform the translation of point from to point to with vector trans */
void translate2D(double from[2], double trans[2], double dest[2])
{
dest[0] = from[0] + trans[0];
dest[1] = from[1] + trans[1];
}
/*----------------------------------------------------------------------------*/
void iTranslate2D(int from[2], int trans[2], int dest[2])
{
dest[0] = from[0] + trans[0];
dest[1] = from[1] + trans[1];
}
/*----------------------------------------------------------------------------*/
void vectSubstract2D(const double vect1[2], const double vect2[], double res[2])
{
res[0] = vect1[0] - vect2[0];
res[1] = vect1[1] - vect2[1];
}
/*----------------------------------------------------------------------------*/
void vectAdd2D(const double v1[2], const double v2[2], double res[2])
{
res[0] = v1[0] + v2[0];
res[1] = v1[1] + v2[1];
}
/*----------------------------------------------------------------------------*/
void scalarMult2D(const double v[2], const double scalar, double res[2])
{
res[0] = scalar * v[0];
res[1] = scalar * v[1];
}
/*----------------------------------------------------------------------------*/
void normalize2d(double vect[2])
{
double norm = NORM_2D(vect);
vect[0] /= norm;
vect[1] /= norm;
}
/*----------------------------------------------------------------------------*/
void iNormalize2d(int vect[2])
{
double norm = NORM_2D(vect);
vect[0] = round(vect[0] / norm);
vect[1] = round(vect[1] / norm);
}
/*----------------------------------------------------------------------------*/
BOOL isPointInTriangle(const double point[2], const double a[2],
const double b[2], const double c[2])
{
return ( areOnSameSideOfLine(point, a, b, c)
&& areOnSameSideOfLine(point, b, a, c)
&& areOnSameSideOfLine(point, c, a, b));
}
/*----------------------------------------------------------------------------*/
BOOL areOnSameSideOfLine(const double p1[2], const double p2[2],
const double a[2], const double b[2])
{
// point are on the same if and only if (AB^AP1).(AB^AP2) >= 0
double ab[3];
double ap1[3];
double ap2[3];
double cp1[3];
double cp2[3];
ab[0] = b[0] - a[0];
ab[1] = b[1] - a[1];
ab[2] = 0.0;
ap1[0] = p1[0] - a[0];
ap1[1] = p1[1] - a[1];
ap1[2] = 0.0;
ap2[0] = p2[0] - a[0];
ap2[1] = p2[1] - a[1];
ap2[2] = 0.0;
crossProduct(ab, ap1, cp1);
crossProduct(ab, ap2, cp2);
return (DOT_PROD_3D(cp1, cp2) >= 0.0);
}
/*----------------------------------------------------------------------------*/
void crossProduct(const double v1[3], const double v2[3], double res[3])
{
/* save data to be able to use v1 o v2 as res */
double v10 = v1[0];
double v20 = v2[0];
double v11 = v1[1];
double v21 = v2[1];
res[0] = v11 * v2[2] - v1[2] * v21 ;
res[1] = v1[2] * v20 - v10 * v2[2] ;
res[2] = v10 * v21 - v11 * v20 ;
}
/*----------------------------------------------------------------------------*/
void vectSubstract3D(const double v1[3] , const double v2[3], double res[3])
{
res[0] = v1[0] - v2[0];
res[1] = v1[1] - v2[1];
res[2] = v1[2] - v2[2];
}
/*----------------------------------------------------------------------------*/
void vectAdd3D(const double v1[3], const double v2[3], double res[3])
{
res[0] = v1[0] + v2[0];
res[1] = v1[1] + v2[1];
res[2] = v1[2] + v2[2];
}
/*----------------------------------------------------------------------------*/
void scalarMult3D(const double v[3], double scalar, double res[3])
{
res[0] = scalar * v[0];
res[1] = scalar * v[1];
res[2] = scalar * v[2];
}
/*----------------------------------------------------------------------------*/
void normalize3D(double vect[3])
{
double norm = NORM_3D(vect) ;
vect[0] /= norm ;
vect[1] /= norm ;
vect[2] /= norm ;
}
/*----------------------------------------------------------------------------*/
void setToIdentity(double mat4D[4][4])
{
int i = 0;
int j = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
mat4D[i][j] = 0.0;
}
mat4D[i][i] = 1.0;
}
}
/*----------------------------------------------------------------------------*/
void mat4DMult(const double mat4D[4][4], const double vect3D[3], double res[3])
{
int i = 0;
double res4D[4];
// w coordinate of the vector is supposed to be 1;
for (i = 0; i < 4; i++)
{
res4D[i] = vect3D[0] * mat4D[i][0] + vect3D[1] * mat4D[i][1]
+ vect3D[2] * mat4D[i][2] + mat4D[i][3];
}
res[0] = res4D[0] / res4D[3];
res[1] = res4D[1] / res4D[3];
res[2] = res4D[2] / res4D[3];
}
/*----------------------------------------------------------------------------*/
|