Debian Codegraph @

converter/other/pamtosvg/vector.c

raw
1 /* vector.c: vector/point operations. */
2
3 #define _XOPEN_SOURCE 500 /* get M_PI in math.h */
4 #include <math.h>
5 #include <errno.h>
6 #include <assert.h>
7 #include <string.h>
8
9 #include "pm_c_util.h"
10
11 #include "vector.h"
12 #include "message.h"
13 #include "epsilon.h"
14
15 static float
16 acosD(float const v,
17 at_exception_type * const excepP) {
18
19 float vAdj;
20 float a;
21 float retval;
22
23 if (epsilon_equal(v, 1.0))
24 vAdj = 1.0;
25 else if (epsilon_equal(v, -1.0))
26 vAdj = -1.0;
27 else
28 vAdj = v;
29
30 errno = 0;
31 a = acos(vAdj);
32 if (errno == ERANGE || errno == EDOM) {
33 at_exception_fatal(excepP, strerror(errno));
34 retval = 0.0;
35 } else
36 retval = a * 180.0 / M_PI;
37
38 return retval;
39 }
40
41
42
43 Vector
44 vector_fromPoint(Point const c) {
45 /* Vector corresponding to point 'c', taken as a vector from the origin. */
46
47 Vector v;
48
49 v.dx = c.x;
50 v.dy = c.y;
51 v.dz = c.z;
52
53 return v;
54 }
55
56
57
58 Vector
59 vector_fromTwoPoints(Point const c1,
60 Point const c2) {
61
62 Vector retval;
63
64 retval.dx = c1.x - c2.x;
65 retval.dy = c1.y - c2.y;
66 retval.dz = c1.z - c2.z;
67
68 return retval;
69 }
70
71
72
73 /* And the converse: given a vector, return the corresponding point. */
74
75 Point
76 vector_toPoint_point(Vector const v) {
77 /* vector as a point, i.e., a displacement from the origin. */
78
79 Point coord;
80
81 coord.x = v.dx;
82 coord.y = v.dy;
83 coord.z = v.dz;
84
85 return coord;
86 }
87
88
89
90 float
91 vector_magnitude(Vector const v) {
92
93 return sqrt(SQR(v.dx) + SQR(v.dy) + SQR(v.dz));
94 }
95
96
97
98 Vector
99 vector_normalized(Vector const v) {
100
101 Vector new_v;
102 float const m = vector_magnitude(v);
103
104 if (m > 0.0) {
105 new_v.dx = v.dx / m;
106 new_v.dy = v.dy / m;
107 new_v.dz = v.dz / m;
108 } else {
109 new_v.dx = v.dx;
110 new_v.dy = v.dy;
111 new_v.dz = v.dz;
112 }
113
114 return new_v;
115 }
116
117
118
119 Vector
120 vector_sum(Vector const addend,
121 Vector const adder) {
122
123 Vector retval;
124
125 retval.dx = addend.dx + adder.dx;
126 retval.dy = addend.dy + adder.dy;
127 retval.dz = addend.dz + adder.dz;
128
129 return retval;
130 }
131
132
133
134 float
135 vector_dotProduct(Vector const v1,
136 Vector const v2) {
137
138 return v1.dx * v2.dx + v1.dy * v2.dy + v1.dz * v2.dz;
139 }
140
141
142
143 Vector
144 vector_scaled(Vector const v,
145 float const r) {
146
147 Vector retval;
148
149 retval.dx = v.dx * r;
150 retval.dy = v.dy * r;
151 retval.dz = v.dz * r;
152
153 return retval;
154 }
155
156
157
158 float
159 vector_angle(Vector const inVector,
160 Vector const outVector,
161 at_exception_type * const exP) {
162
163 /* The angle between 'inVector' and 'outVector' in degrees, in the range zero
164 to 180.
165 */
166
167 Vector const v1 = vector_normalized(inVector);
168 Vector const v2 = vector_normalized(outVector);
169
170 return acosD(vector_dotProduct(v2, v1), exP);
171 }
172
173
174
175 Point
176 vector_sumPoint(Point const c,
177 Vector const v) {
178
179 Point retval;
180
181 retval.x = c.x + v.dx;
182 retval.y = c.y + v.dy;
183 retval.z = c.z + v.dz;
184
185 return retval;
186 }
187
188
189
190 Point
191 vector_diffPoint(Point const c,
192 Vector const v) {
193
194 Point retval;
195
196 retval.x = c.x - v.dx;
197 retval.y = c.y - v.dy;
198 retval.z = c.z - v.dz;
199
200 return retval;
201 }
202
203
204
205 Vector
206 vector_IPointDiff(pm_pixelcoord const coord1,
207 pm_pixelcoord const coord2) {
208
209 Vector retval;
210
211 retval.dx = (int) (coord1.col - coord2.col);
212 retval.dy = (int) (coord1.row - coord2.row);
213 retval.dz = 0.0;
214
215 return retval;
216 }
217
218
219
220 pm_pixelcoord
221 vector_sumIntPoint(pm_pixelcoord const c,
222 Vector const v) {
223
224 /* This returns the rounded sum. */
225
226 pm_pixelcoord retval;
227
228 retval.col = ROUND ((float) c.col + v.dx);
229 retval.row = ROUND ((float) c.row + v.dy);
230
231 return retval;
232 }
233
234
235
236 Vector
237 vector_abs(Vector const v) {
238
239 /* First-quadrant mirror of 'v' (both components unsigned) */
240
241 Vector retval;
242
243 retval.dx = (float) fabs (v.dx);
244 retval.dy = (float) fabs (v.dy);
245 retval.dz = (float) fabs (v.dz);
246
247 return retval;
248 }
249
250
251
252 Vector
253 vector_pointDirection(Point const final,
254 Point const initial) {
255
256 return vector_normalized(vector_fromTwoPoints(final, initial));
257 }
258
259
260
261 Vector
262 vector_horizontal(void) {
263
264 Vector retval;
265
266 retval.dx = 1.0;
267 retval.dy = 0.0;
268 retval.dz = 0.0;
269
270 return retval;
271 }
272
273
274
275 Vector
276 vector_zero(void) {
277
278 Vector retval;
279
280 retval.dx = 0.0;
281 retval.dy = 0.0;
282 retval.dz = 0.0;
283
284 return retval;
285 }
286
287
288
289 bool
290 vector_equal(Vector const comparand,
291 Vector const comparator) {
292
293 return
294 epsilon_equal(comparand.dx, comparator.dx)
295 &&
296 epsilon_equal(comparand.dy, comparator.dy)
297 &&
298 epsilon_equal(comparand.dz, comparator.dz)
299 ;
300 }
301
302
303