about summary refs log tree commit diff
path: root/converter/other/pamtosvg/vector.c
blob: a02b933b8b59ee2e4ed94ab8e3704111c264f3c9 (plain) (blame)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* vector.c: vector/point operations. */

#define _XOPEN_SOURCE 500  /* get M_PI in math.h */
#include <math.h>
#include <errno.h>
#include <assert.h>
#include <string.h>

#include "pm_c_util.h"

#include "vector.h"
#include "message.h"
#include "epsilon.h"

static float
acosD(float               const v,
      at_exception_type * const excepP) {

    float vAdj;
    float a;
    float retval;

    if (epsilon_equal(v, 1.0))
        vAdj = 1.0;
    else if (epsilon_equal(v, -1.0))
        vAdj = -1.0;
    else
        vAdj = v;

    errno = 0;
    a = acos(vAdj);
    if (errno == ERANGE || errno == EDOM) {
        at_exception_fatal(excepP, strerror(errno));
        retval = 0.0;
    } else
        retval = a * 180.0 / M_PI;

    return retval;
}



Vector
vector_fromPoint(Point const c) {
/* Vector corresponding to point 'c', taken as a vector from the origin.  */

    Vector v;

    v.dx = c.x;
    v.dy = c.y;
    v.dz = c.z;

    return v;
}



Vector
vector_fromTwoPoints(Point const c1,
                     Point const c2) {

    Vector retval;

    retval.dx = c1.x - c2.x;
    retval.dy = c1.y - c2.y;
    retval.dz = c1.z - c2.z;

    return retval;
}



/* And the converse: given a vector, return the corresponding point.  */

Point
vector_toPoint_point(Vector const v) {
/* vector as a point, i.e., a displacement from the origin.  */

    Point coord;

    coord.x = v.dx;
    coord.y = v.dy;
    coord.z = v.dz;

    return coord;
}



float
vector_magnitude(Vector const v) {

    return sqrt(SQR(v.dx) + SQR(v.dy) + SQR(v.dz));
}



Vector
vector_normalized(Vector const v) {

    Vector new_v;
    float const m = vector_magnitude(v);

    if (m > 0.0) {
        new_v.dx = v.dx / m;
        new_v.dy = v.dy / m;
        new_v.dz = v.dz / m;
    } else {
        new_v.dx = v.dx;
        new_v.dy = v.dy;
        new_v.dz = v.dz;
    }

    return new_v;
}



Vector
vector_sum(Vector const addend,
           Vector const adder) {

    Vector retval;

    retval.dx = addend.dx + adder.dx;
    retval.dy = addend.dy + adder.dy;
    retval.dz = addend.dz + adder.dz;

    return retval;
}



float
vector_dotProduct(Vector const v1,
                  Vector const v2) {

    return v1.dx * v2.dx + v1.dy * v2.dy + v1.dz * v2.dz;
}



Vector
vector_scaled(Vector const v,
              float  const r) {

    Vector retval;

    retval.dx = v.dx * r;
    retval.dy = v.dy * r;
    retval.dz = v.dz * r;

    return retval;
}



float
vector_angle(Vector              const inVector,
             Vector              const outVector,
             at_exception_type * const exP) {

/* The angle between 'inVector' and 'outVector' in degrees, in the range zero
   to 180.
*/

    Vector const v1 = vector_normalized(inVector);
    Vector const v2 = vector_normalized(outVector);

    return acosD(vector_dotProduct(v2, v1), exP);
}



Point
vector_sumPoint(Point const c,
                Vector      const v) {

    Point retval;

    retval.x = c.x + v.dx;
    retval.y = c.y + v.dy;
    retval.z = c.z + v.dz;

    return retval;
}



Point
vector_diffPoint(Point  const c,
                 Vector const v) {

    Point retval;

    retval.x = c.x - v.dx;
    retval.y = c.y - v.dy;
    retval.z = c.z - v.dz;

    return retval;
}



Vector
vector_IPointDiff(pm_pixelcoord const coord1,
                  pm_pixelcoord const coord2) {

    Vector retval;

    retval.dx = (int) (coord1.col - coord2.col);
    retval.dy = (int) (coord1.row - coord2.row);
    retval.dz = 0.0;

    return retval;
}



pm_pixelcoord
vector_sumIntPoint(pm_pixelcoord const c,
                   Vector        const v) {

/* This returns the rounded sum.  */

    pm_pixelcoord retval;

    retval.col = ROUND ((float) c.col + v.dx);
    retval.row = ROUND ((float) c.row + v.dy);

    return retval;
}



Vector
vector_abs(Vector const v) {

/* First-quadrant mirror of 'v' (both components unsigned) */

    Vector retval;

    retval.dx = (float) fabs (v.dx);
    retval.dy = (float) fabs (v.dy);
    retval.dz = (float) fabs (v.dz);

    return retval;
}



Vector
vector_pointDirection(Point const final,
                      Point const initial) {

    return vector_normalized(vector_fromTwoPoints(final, initial));
}



Vector
vector_horizontal(void) {

    Vector retval;

    retval.dx = 1.0;
    retval.dy = 0.0;
    retval.dz = 0.0;

    return retval;
}



Vector
vector_zero(void) {

    Vector retval;

    retval.dx = 0.0;
    retval.dy = 0.0;
    retval.dz = 0.0;

    return retval;
}



bool
vector_equal(Vector const comparand,
             Vector const comparator) {

    return
        epsilon_equal(comparand.dx, comparator.dx)
        &&
        epsilon_equal(comparand.dy, comparator.dy)
        &&
        epsilon_equal(comparand.dz, comparator.dz)
        ;
}