-
Notifications
You must be signed in to change notification settings - Fork 26
/
v3.h
310 lines (258 loc) · 3.45 KB
/
v3.h
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
304
305
306
307
308
309
310
/** \file
* 3D point operations.
*/
#ifndef _papercraft_v3_h_
#define _papercraft_v3_h_
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define EPS 0.00001
#ifndef M_PI
#define M_PI 3.1415926535897932384
#endif
static inline float
sign(
const float x
)
{
if (x < 0)
return -1;
if (x > 0)
return +1;
return 0;
}
static inline float
min(
const float a,
const float b
)
{
return a < b ? a : b;
}
static inline float
max(
const float a,
const float b
)
{
return a > b ? a : b;
}
typedef struct
{
float p[3];
} v3_t;
static inline int
v3_eq(
const v3_t v1,
const v3_t v2
)
{
float dx = v1.p[0] - v2.p[0];
float dy = v1.p[1] - v2.p[1];
float dz = v1.p[2] - v2.p[2];
if (-EPS < dx && dx < EPS
&& -EPS < dy && dy < EPS
&& -EPS < dz && dz < EPS)
return 1;
return 0;
}
static inline double
v3_len(
const v3_t * const v0,
const v3_t * const v1
)
{
float dx = v0->p[0] - v1->p[0];
float dy = v0->p[1] - v1->p[1];
float dz = v0->p[2] - v1->p[2];
return sqrt(dx*dx + dy*dy + dz*dz);
}
static inline double
v3_mag2(
const v3_t v0
)
{
float dx = v0.p[0];
float dy = v0.p[1];
float dz = v0.p[2];
return dx*dx + dy*dy + dz*dz;
}
static inline double
v3_mag(
const v3_t v0
)
{
return sqrt(v3_mag2(v0));
}
static inline v3_t
v3_add(
v3_t a,
v3_t b
)
{
v3_t c = { .p = {
a.p[0] + b.p[0],
a.p[1] + b.p[1],
a.p[2] + b.p[2],
} };
return c;
}
static inline v3_t
v3_sub(
v3_t a,
v3_t b
)
{
v3_t c = { .p = {
a.p[0] - b.p[0],
a.p[1] - b.p[1],
a.p[2] - b.p[2],
} };
return c;
}
static inline v3_t
v3_scale(
v3_t a,
float s
)
{
v3_t c = { .p = {
a.p[0]*s,
a.p[1]*s,
a.p[2]*s,
} };
return c;
}
static inline
v3_t
v3_norm(
const v3_t v
)
{
return v3_scale(v, 1/v3_mag(v));
}
static inline
v3_t
v3_mid(
const v3_t v0,
const v3_t v1,
const v3_t v2
)
{
return v3_norm(
v3_add(
v3_sub(v1, v0),
v3_sub(v2, v0)
)
);
}
static inline float
v3_dot(
v3_t a,
v3_t b
)
{
return a.p[0]*b.p[0] + a.p[1]*b.p[1] + a.p[2]*b.p[2];
}
static inline v3_t
v3_cross(
v3_t u,
v3_t v
)
{
float u1 = u.p[0];
float u2 = u.p[1];
float u3 = u.p[2];
float v1 = v.p[0];
float v2 = v.p[1];
float v3 = v.p[2];
v3_t c = { .p = {
u2*v3 - u3*v2,
u3*v1 - u1*v3,
u1*v2 - u2*v1,
}};
return c;
}
static inline v3_t
v3_min(
const v3_t a,
const v3_t b
)
{
v3_t c = { {
min(a.p[0], b.p[0]),
min(a.p[1], b.p[1]),
min(a.p[2], b.p[2]),
}};
return c;
}
static inline v3_t
v3_max(
const v3_t a,
const v3_t b
)
{
v3_t c = { {
max(a.p[0], b.p[0]),
max(a.p[1], b.p[1]),
max(a.p[2], b.p[2]),
}};
return c;
}
// Compute the length of a line in screen space, ignoring Z
static inline float
v3_dist_2d(
const v3_t * p0,
const v3_t * p1
)
{
const float dx = p1->p[0] - p0->p[0];
const float dy = p1->p[1] - p0->p[1];
return sqrt(dx*dx + dy*dy);
}
static inline void
v3_print(const v3_t p)
{
fprintf(stderr, "%+6.1f %+6.1f %+6.1f\n",
p.p[0],
p.p[1],
p.p[2]
);
}
typedef struct {
float p[4];
} v4_t;
typedef struct {
float m[4][4];
} m44_t;
static inline void
m44_mult(
m44_t * r,
const m44_t * a,
const m44_t * b
)
{
for(int i = 0 ; i < 4 ; i++)
{
for(int j = 0 ; j < 4 ; j++)
{
float d = 0;
for(int k = 0 ; k < 4 ; k++)
d += a->m[i][k] * b->m[k][j];
r->m[i][j] = d;
}
}
}
static inline v4_t
m44_multv(
const m44_t * const m,
const v4_t * const v
)
{
v4_t p = {};
for (int i = 0 ; i < 4 ; i++)
for (int j = 0 ; j < 4 ; j++)
p.p[i] += m->m[i][j] * v->p[j];
return p;
}
#endif