-
Notifications
You must be signed in to change notification settings - Fork 1
/
prim3.h
490 lines (425 loc) · 21.9 KB
/
prim3.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//============================================================================
// Mini Spin-X Library
//
// Copyright (c) 2024, Jarkko Lempiainen
// All rights reserved.
//============================================================================
#ifndef PFC_CORE_MATH_PRIM3_H
#define PFC_CORE_MATH_PRIM3_H
//----------------------------------------------------------------------------
//============================================================================
// interface
//============================================================================
// external
#include "sxp_src/core/math/math.h"
#include "sxp_src/core/math/numeric.h"
namespace pfc
{
// new
template<typename> struct point3;
template<typename> struct line3;
template<typename> struct ray3;
template<typename> struct segment3;
template<typename> struct tri3;
template<typename> struct rect3;
template<typename> struct disc3;
template<typename> struct plane3;
template<typename> struct sphere3;
template<typename> struct cylinder3;
template<typename> struct capsule3;
template<typename> struct cone3;
template<typename> struct aabox3;
template<typename> struct oobox3;
template<typename> struct frustum3;
typedef point3<float> point3f;
typedef point3<double> point3d;
typedef point3<simd_float4> simd_point3f;
typedef line3<float> line3f;
typedef line3<double> line3d;
typedef line3<simd_float4> simd_line3f;
typedef ray3<float> ray3f;
typedef ray3<double> ray3d;
typedef ray3<simd_float4> simd_ray3f;
typedef segment3<float> segment3f;
typedef segment3<double> segment3d;
typedef segment3<simd_float4> simd_segment3f;
typedef tri3<float> tri3f;
typedef tri3<double> tri3d;
typedef tri3<simd_float4> simd_tri3f;
typedef rect3<float> rect3f;
typedef rect3<double> rect3d;
typedef rect3<simd_float4> simd_rect3f;
typedef disc3<float> disc3f;
typedef disc3<double> disc3d;
typedef disc3<simd_float4> simd_disc3f;
typedef plane3<float> plane3f;
typedef plane3<double> plane3d;
typedef plane3<simd_float4> simd_plane3f;
typedef sphere3<float> sphere3f;
typedef sphere3<double> sphere3d;
typedef sphere3<simd_float4> simd_sphere3f;
typedef cylinder3<float> cylinder3f;
typedef cylinder3<double> cylinder3d;
typedef cylinder3<simd_float4> simd_cylinder3f;
typedef capsule3<float> capsule3f;
typedef capsule3<double> capsule3d;
typedef capsule3<simd_float4> simd_capsule3f;
typedef cone3<float> cone3f;
typedef cone3<double> cone3d;
typedef cone3<simd_float4> simd_cone3f;
typedef aabox3<float> aabox3f;
typedef aabox3<double> aabox3d;
typedef aabox3<simd_float4> simd_aabox3f;
typedef oobox3<float> oobox3f;
typedef oobox3<double> oobox3d;
typedef oobox3<simd_float4> simd_oobox3f;
typedef frustum3<float> frustum3f;
typedef frustum3<double> frustum3d;
typedef frustum3<simd_float4> simd_frustum3f;
// area functions
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const tri3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const rect3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const disc3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const sphere3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const cylinder3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const capsule3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const cone3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const aabox3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const oobox3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t area(const frustum3<T>&);
// volume functions
template<typename T> PFC_INLINE typename math<T>::scalar_t volume(const sphere3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t volume(const cylinder3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t volume(const capsule3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t volume(const cone3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t volume(const aabox3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t volume(const oobox3<T>&);
template<typename T> PFC_INLINE typename math<T>::scalar_t volume(const frustum3<T>&);
//----------------------------------------------------------------------------
//============================================================================
// point3
//============================================================================
template<typename T>
struct point3
{ PFC_MONO(point3) {PFC_MVAR(pos);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE point3();
PFC_INLINE point3(const vec3<T> &pos_);
PFC_INLINE point3(scalar_t x_, scalar_t y_, scalar_t z_);
PFC_INLINE void set(const vec3<T> &pos_);
PFC_INLINE void set(scalar_t x_, scalar_t y_, scalar_t z_);
//--------------------------------------------------------------------------
static const point3<T> s_zero;
vec3<T> pos;
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, point3<T>, is_type_pod, is_type_pod<vec3<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, point3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
// line3
//============================================================================
template<typename T>
struct line3
{ PFC_MONO(line3) {PFC_MVAR2(pos, dir);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE line3();
PFC_INLINE line3(const vec3<T> &pos_, const vec3<T> &dir_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &dir_);
//--------------------------------------------------------------------------
static const line3<T> s_zero;
vec3<T> pos; // point on the line
vec3<T> dir; // unit direction of the line
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, line3<T>, is_type_pod, is_type_pod<vec3<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, line3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
// ray3
//============================================================================
template<typename T>
struct ray3
{ PFC_MONO(ray3) {PFC_MVAR2(pos, dir);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE ray3();
PFC_INLINE ray3(const vec3<T> &pos_, const vec3<T> &dir_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &dir_);
//--------------------------------------------------------------------------
static const ray3<T> s_zero;
vec3<T> pos; // starting point of the ray
vec3<T> dir; // unit direction of the ray
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, ray3<T>, is_type_pod, is_type_pod<vec3<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, ray3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
// segment3
//============================================================================
template<typename T>
struct segment3
{ PFC_MONO(segment3) {PFC_MVAR3(pos, dir, hlen);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE segment3();
PFC_INLINE segment3(const vec3<T> &pos_, const vec3<T> &dir_, scalar_t hlen_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &dir_, scalar_t hlen_);
//--------------------------------------------------------------------------
static const segment3<T> s_zero;
vec3<T> pos; // center position of the segment
vec3<T> dir; // unit direction of the segment
scalar_t hlen; // half-length of the segment
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, segment3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<typename math<T>::scalar_t>::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, segment3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<typename math<T>::scalar_t>::res);
//----------------------------------------------------------------------------
//============================================================================
// tri3
//============================================================================
template<typename T>
struct tri3
{ PFC_MONO(tri3) {PFC_MVAR3(a, b, c);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE tri3();
PFC_INLINE tri3(const vec3<T>&, const vec3<T>&, const vec3<T>&);
PFC_INLINE void set(const vec3<T>&, const vec3<T>&, const vec3<T>&);
//--------------------------------------------------------------------------
static const tri3<T> s_zero;
vec3<T> a, b, c; // 3 points of the triangle. normal = (b-a) x (c-a) = clock-wise
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, tri3<T>, is_type_pod, is_type_pod<vec3<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, tri3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
// rect3
//============================================================================
template<typename T>
struct rect3
{ PFC_MONO(rect3) {PFC_MVAR3(pos, normal, hsize);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE rect3();
PFC_INLINE rect3(const vec3<T> &pos_, const quat<T> &rot_, const vec2<T> &hsize_);
PFC_INLINE void set(const vec3<T> &pos_, const quat<T> &rot_, const vec2<T> &hsize_);
//--------------------------------------------------------------------------
static const rect3<T> s_zero;
vec3<T> pos; // center position of the rect
quat<T> rot; // rect orientation
vec2<T> hsize; // half of the length of the rect along each axis
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, rect3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<quat<T> >::res && is_type_pod<vec2<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, rect3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<quat<T> >::res && is_type_pod_stream<vec2<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
// disc3
//============================================================================
template<typename T>
struct disc3
{ PFC_MONO(disc3) {PFC_MVAR3(pos, normal, rad);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE disc3();
PFC_INLINE disc3(const vec3<T> &pos_, const vec3<T> &normal_, scalar_t rad_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &normal_, scalar_t rad_);
//--------------------------------------------------------------------------
static const disc3<T> s_zero;
vec3<T> pos;
vec3<T> normal;
scalar_t rad;
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, disc3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<typename math<T>::scalar_t>::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, disc3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<typename math<T>::scalar_t>::res);
//----------------------------------------------------------------------------
//============================================================================
// plane3
//============================================================================
template<typename T>
struct plane3
{ PFC_MONO(plane3) {PFC_MVAR2(normal, d);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE plane3();
PFC_INLINE plane3(const vec3<T> &normal_, scalar_t d_);
PFC_INLINE void set(const vec3<T> &normal_, scalar_t d_);
//--------------------------------------------------------------------------
static const plane3<T> s_zero;
vec3<T> normal; // unit normal of the plane
scalar_t d; // plane distance from origo [0, 0, 0]
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, plane3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<typename math<T>::scalar_t>::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, plane3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<typename math<T>::scalar_t>::res);
//----------------------------------------------------------------------------
//============================================================================
// sphere3
//============================================================================
template<typename T>
struct sphere3
{ PFC_MONO(sphere3) {PFC_MVAR2(pos, rad);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE sphere3();
PFC_INLINE sphere3(const vec3<T> &pos_, scalar_t rad_);
PFC_INLINE void set(const vec3<T> &pos_, scalar_t rad_);
//--------------------------------------------------------------------------
static const sphere3<T> s_zero;
vec3<T> pos; // center position of the sphere
scalar_t rad; // sphere radius
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, sphere3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<typename math<T>::scalar_t>::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, sphere3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<typename math<T>::scalar_t>::res);
//----------------------------------------------------------------------------
//============================================================================
// cylinder3
//============================================================================
template<typename T>
struct cylinder3
{ PFC_MONO(cylinder3) {PFC_MVAR4(pos, dir, hlen, rad);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE cylinder3();
PFC_INLINE cylinder3(const vec3<T> &pos_, const vec3<T> &dir_, scalar_t hlen_, scalar_t rad_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &dir_, scalar_t hlen_, scalar_t rad_);
//--------------------------------------------------------------------------
static const cylinder3<T> s_zero;
vec3<T> pos; // center position of the cylinder
vec3<T> dir; // cylinder unit direction
scalar_t hlen; // half of the length of the cylinder
scalar_t rad; // cylinder radius
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, cylinder3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<typename math<T>::scalar_t>::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, cylinder3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<typename math<T>::scalar_t>::res);
//----------------------------------------------------------------------------
//============================================================================
// capsule3
//============================================================================
template<typename T>
struct capsule3
{ PFC_MONO(capsule3) {PFC_MVAR4(pos, dir, hlen, rad);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE capsule3();
PFC_INLINE capsule3(const vec3<T> &pos_, const vec3<T> &dir_, scalar_t hlen_, scalar_t rad_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &dir_, scalar_t hlen_, scalar_t rad_);
//--------------------------------------------------------------------------
static const capsule3<T> s_zero;
vec3<T> pos; // center position of the capsule
vec3<T> dir; // capsule unit direction
scalar_t hlen; // half of the length of the capsule
scalar_t rad; // capsule radius
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, capsule3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<typename math<T>::scalar_t>::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, capsule3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<typename math<T>::scalar_t>::res);
//----------------------------------------------------------------------------
//============================================================================
// cone3
//============================================================================
template<typename T>
struct cone3
{ PFC_MONO(cone3) {PFC_MVAR4(apex_pos, dir, height, cos_apex_hangle);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE cone3();
PFC_INLINE cone3(const vec3<T> &apex_pos_, const vec3<T> &dir_, scalar_t height_, scalar_t cos_apex_hangle_);
PFC_INLINE void set(const vec3<T> &apex_pos_, const vec3<T> &dir_, scalar_t height_, scalar_t cos_apex_hangle_);
//--------------------------------------------------------------------------
static const cone3<T> s_zero;
vec3<T> apex_pos; // apex position
vec3<T> dir; // cone unit direction
scalar_t height; // cone height
scalar_t cos_apex_hangle; // cosine of half-angle of the apex
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, cone3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<typename math<T>::scalar_t>::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, cone3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<typename math<T>::scalar_t>::res);
//----------------------------------------------------------------------------
//============================================================================
// aabox3
//============================================================================
template<typename T>
struct aabox3
{ PFC_MONO(aabox3) {PFC_MVAR2(pos, hsize);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE aabox3();
PFC_INLINE aabox3(const vec3<T> &pos_, const vec3<T> &hsize_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &hsize_);
//--------------------------------------------------------------------------
static const aabox3<T> s_zero;
vec3<T> pos; // center position of the box
vec3<T> hsize; // half of the length of the box along each axis
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, aabox3<T>, is_type_pod, is_type_pod<vec3<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, aabox3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
// oobox3
//============================================================================
template<typename T>
struct oobox3
{ PFC_MONO(oobox3) {PFC_MVAR3(pos, hsize, rot);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE oobox3();
PFC_INLINE oobox3(const vec3<T> &pos_, const vec3<T> &hsize_, const quat<T> &rot_);
PFC_INLINE void set(const vec3<T> &pos_, const vec3<T> &hsize_, const quat<T> &rot_);
//--------------------------------------------------------------------------
static const oobox3<T> s_zero;
vec3<T> pos; // center position of the box
vec3<T> hsize; // half of the length of the box along each axis
quat<T> rot; // box orientation
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, oobox3<T>, is_type_pod, is_type_pod<vec3<T> >::res && is_type_pod<quat<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, oobox3<T>, is_type_pod_stream, is_type_pod_stream<vec3<T> >::res && is_type_pod_stream<quat<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
// frustum3
//============================================================================
template<typename T>
struct frustum3
{ PFC_MONO(frustum3) {PFC_VAR(proj_to_local);}
typedef T type_t;
typedef typename math<T>::scalar_t scalar_t;
//--------------------------------------------------------------------------
// construction
PFC_INLINE frustum3();
PFC_INLINE frustum3(const mat44<T> &p2l_);
PFC_INLINE void set(const mat44<T> &p2l_);
//--------------------------------------------------------------------------
mat44<T> proj_to_local; // projection -> local space transformation
};
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, frustum3<T>, is_type_pod, is_type_pod<mat44<T> >::res);
PFC_SET_TYPE_TRAIT_PARTIAL(typename T, frustum3<T>, is_type_pod_stream, is_type_pod_stream<mat44<T> >::res);
//----------------------------------------------------------------------------
//============================================================================
#include "prim3.inl"
} // namespace pfc
#endif