about summary refs log tree commit diff
path: root/converter/other/pamtosvg/vector.c
blob: 771e5f27bad6ce88bee071b7be114c95530719ce (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
/* 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-equal.h"

static float acos_d (float, at_exception_type * excep);


/* Given the point COORD, return the corresponding vector.  */

vector_type
make_vector(float_coord const c) {

    vector_type v;

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

    return v;
}



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

float_coord
vector_to_point(vector_type const v) {

    float_coord coord;

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

    return coord;
}



float
magnitude(vector_type const v) {

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



vector_type
normalize(vector_type const v) {

    vector_type new_v;
    float const m = 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_type
Vadd(vector_type const v1,
     vector_type const v2) {

    vector_type new_v;

    new_v.dx = v1.dx + v2.dx;
    new_v.dy = v1.dy + v2.dy;
    new_v.dz = v1.dz + v2.dz;

    return new_v;
}



float
Vdot(vector_type const v1,
     vector_type const v2) {

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



vector_type
Vmult_scalar(vector_type const v,
             float       const r) {

    vector_type new_v;

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

    return new_v;
}



/* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in
   degrees, in the range zero to 180.
*/

float
Vangle(vector_type         const in_vector, 
       vector_type         const out_vector,
       at_exception_type * const exP) {

    vector_type const v1 = normalize(in_vector);
    vector_type const v2 = normalize(out_vector);

    return acos_d(Vdot(v2, v1), exP);
}



float_coord
Vadd_point(float_coord const c,
           vector_type const v) {

    float_coord new_c;

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

    return new_c;
}



float_coord
Vsubtract_point(float_coord const c,
                vector_type const v) {

    float_coord new_c;

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

    return new_c;
}



pm_pixelcoord
Vadd_int_point(pm_pixelcoord const c,
               vector_type   const v) {

    pm_pixelcoord a;

    a.col = ROUND ((float) c.col + v.dx);
    a.row = ROUND ((float) c.row + v.dy);
    
    return a;
}



vector_type
Vabs(vector_type const v) {

    vector_type new_v;

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

    return new_v;
}



/* Operations on points.  */

float_coord
Padd(float_coord const coord1,
     float_coord const coord2) {

    float_coord sum;

    sum.x = coord1.x + coord2.x;
    sum.y = coord1.y + coord2.y;
    sum.z = coord1.z + coord2.z;

    return sum;
}



float_coord
Pmult_scalar(float_coord const coord,
             float       const r) {

    float_coord answer;

    answer.x = coord.x * r;
    answer.y = coord.y * r;
    answer.z = coord.z * r;

    return answer;
}



vector_type
Psubtract(float_coord const c1,
          float_coord const c2) {

    vector_type v;

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

    return v;
}



vector_type
Pdirection(float_coord const final,
           float_coord const initial) {

    return normalize(Psubtract(final, initial));
}



/* Operations on integer points.  */

vector_type
IPsubtract(pm_pixelcoord const coord1,
           pm_pixelcoord const coord2) {

    vector_type v;

    v.dx = (int) (coord1.col - coord2.col);
    v.dy = (int) (coord1.row - coord2.row);
    v.dz = 0.0;
    
    return v;
}



static float
acos_d(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;
}