Untitled
Loading...
Searching...
No Matches
utilities.c
Go to the documentation of this file.
1
2#include "mathf.h"
3#include "types.h"
4#include <limits.h>
5#include <math.h>
6#include <stdlib.h>
7#include "utilities.h"
8#include <string.h>
9
10
11
12int vector_alloc(Vector * v, size_t i)
13{
14 if (v->type_size == 0)
15 {
16 printf("ERR: NO TYPE SIZE FOR VECTOR SPECIFIED\n");
17 return 0;
18 }
19 v->buffer = malloc(v->type_size * i);
20 if (!v->buffer) return 0;
21
22 return 1;
23}
24
25int vector_push(Vector * v, void * o)
26{
27
28 if (!v->buffer)
29 {
30
31 if (!vector_alloc(v, 1)) return 0;
32 }
33 else
34 {
35
36 if (v->count + 1 >= v->capacity)
37 {
38 v->capacity = (v->capacity + 1) * 2;
39 v->buffer = realloc(v->buffer, v->type_size * v->capacity);
40 }
41 }
42
43 size_t offset = v->count * v->type_size;
44 memcpy(v->buffer, o, v->type_size);
45
46 return v->count++;
47}
48
49void * vector_expand(Vector * v, size_t i)
50{
51
52 if (!v->buffer)
53 {
54 if (!vector_alloc(v, i)) return NULL;
55 }
56 else
57 {
58
59 if (v->count + i >= v->capacity)
60 {
61 v->capacity = ((v->capacity + 1) * 2 + v->count + i) * 2;
62 v->buffer = realloc(v->buffer, v->type_size * v->capacity);
63 }
64 if (!v->buffer) printf("BUFFER WITHOUT SIZE\n");
65 }
66
67
68
69 int original_count = v->count;
70 v->count += i;
71 return (v->buffer + original_count * v->type_size);
72}
73
74void init_vector(Vector * v, size_t i, size_t type_size)
75{
76 v->type_size = type_size;
77
78 if (i)
79 {
80 v->buffer = malloc(type_size * i);
81 }
82 else
83 {
84 v->buffer = NULL;
85 }
86 v->count = i;
87 v->capacity = i;
88}
89
91{
92 free(v->buffer);
93
94}
95
96
97mat4 compose_2d_transform(vec2 transform, float angle, vec2 scale) {
98 float c = cosf(angle);
99 float s = sinf(angle);
100
101 mat4 out;
102 out.a0 = c * scale.x; out.b0 = -s * scale.y; out.c0 = 0.0f; out.d0 = transform.x;
103 out.a1 = s * scale.x; out.b1 = c * scale.y; out.c1 = 0.0f; out.d1 = transform.y;
104 out.a2 = 0.0f; out.b2 = 0.0f; out.c2 = 1.0f; out.d2 = 0.0f;
105 out.a3 = 0.0f; out.b3 = 0.0f; out.c3 = 0.0f; out.d3 = 1.0f;
106 return out;
107}
108
109
110uint16_t float32_to_float16(float f) {
111 uint32_t x = *(uint32_t*)&f;
112
113 uint32_t sign = (x >> 16) & 0x8000;
114 int32_t exponent = ((x >> 23) & 0xff) - 127;
115 uint32_t mantissa = x & 0x7fffff;
116
117 if (exponent <= -15) {
118 return sign;
119 }
120 if (exponent >= 16) {
121 return sign | 0x7c00;
122 }
123
124 uint32_t new_exponent = (exponent + 15) << 10;
125 uint32_t new_mantissa = mantissa >> 13;
126
127 return sign | new_exponent | new_mantissa;
128}
129
130
131
132float
134 ( float x )
135{ return x - floorf(x * (1.0f / 289.0f)) * 289.0f; }
136
137vec4
139 ( vec4 x )
140
141{
142
143 vec4 p = vec4_mul(
144 vec4_add(
145 vec4_scale(x, 34.0f),
146 (vec4){ 1.0f, 1.0f, 1.0f, 1.0f }
147 ),
148 x
149 );
150
151 p.x = mod289(p.x);
152 p.y = mod289(p.y);
153 p.z = mod289(p.z);
154 p.w = mod289(p.w);
155 return p;
156
157}
158
159vec3
161 (vec3 x)
162
163{
164
165 vec3 p;
166 p.x = x.x - floorf(x.x);
167 p.y = x.y - floorf(x.y);
168 p.z = x.z - floorf(x.z);
169
170 return p;
171
172}
173
174vec4
176 ( vec4 x )
177
178{
179
180 vec4 p;
181 p.x = x.x - floorf(x.x);
182 p.y = x.y - floorf(x.y);
183 p.z = x.z - floorf(x.z);
184 p.w = x.w - floorf(x.w);
185
186 return p;
187
188}
189
190
191float
193 ( vec3 p )
194
195{
196
197 vec3 a = (vec3){floorf(p.x), floorf(p.y), floorf(p.z)};
198 vec3 d = vec3_sub(p, a);
199
200 d = vec3_mul(vec3_mul(d, d), vec3_sub((vec3){3.0f, 3.0f, 3.0f}, vec3_scale(d, 2.0f)));
201
202 vec4 b = vec4_add((vec4){a.x, a.x, a.y, a.y}, (vec4){0.0f, 1.0f, 0.0f, 1.0f});
203 vec4 k1 = perm((vec4){b.x, b.y, b.x, b.y});
204 vec4 k2 = perm(vec4_add((vec4){k1.x, k1.y, k1.x, k1.y}, (vec4){b.z, b.z, b.w, b.w}));
205
206 vec4 c = vec4_add(k2, (vec4){a.z, a.z, a.z, a.z});
207 vec4 k3 = perm(c);
208 vec4 k4 = perm(vec4_add(c, (vec4){1.0f, 1.0f, 1.0f, 1.0f}));
209
210 vec4 o1 = vec4_fract(vec4_scale(k3, (1.0f / 41.0f)));
211 vec4 o2 = vec4_fract(vec4_scale(k4, (1.0f / 41.0f)));
212
213 vec4 o3 = vec4_add(vec4_scale(o2, d.z), vec4_scale(o1, (1.0f - d.z)));
214 vec2 o4 = vec2_add(vec2_scale((vec2){o3.y, o3.w}, d.x), vec2_scale((vec2){o3.x, o3.z}, (1.0f - d.x)));
215
216 return o4.y * d.y + o4.x * (1.0 - d.y);
217
218}
219
220float
222 ( vec3 p )
223
224{
225
226 float v = 0.0;
227 float a = 0.5;
228 float f = 1.0;
229
230 vec3 shift = (vec3){100, 100, 100};
231 for (int i = 0; i < 5; ++i) {
232
233 v += a * fbm_noise(vec3_scale(p, f));
234
235 p = vec3_add(vec3_scale(p, 2.0), shift);
236 f *= 2.5;
237 a *= 0.45;
238
239 }
240
241 return v;
242
243}
244
245
246bool test_sphere_aabb(vec3 light_pos, float light_radius, vec3 min, vec3 max)
247{
248
249 vec3 closest_point = vec3_clamp(light_pos, min, max);
250
251 vec3 delta = vec3_sub(light_pos, closest_point);
252
253 return vec3_dot(delta, delta) <= (light_radius * light_radius);
254}
255
256
257
258void init_aabb(AABB * a, vec3 center, vec3 extents)
259{
260
261 a->center = center;
262 a->extents = extents;
263
264 a->min_x = a->center.x - a->extents.x;
265 a->max_x = a->center.x + a->extents.x;
266 a->min_y = a->center.y - a->extents.y;
267 a->max_y = a->center.y + a->extents.y;
268 a->min_z = a->center.z - a->extents.z;
269 a->max_z = a->center.z + a->extents.z;
270
271}
272
273
274
275
277{
278
279 if (
280 a->min_x <= b->max_x &&
281 a->max_x >= b->min_x &&
282 a->min_y <= b->max_y &&
283 a->max_y >= b->min_y &&
284 a->min_z <= b->max_z &&
285 a->max_z >= b->min_z
286 )
287 {
288 return true;
289 }
290 else
291 {
292 return false;
293 }
294
295}
296
297
299{
300
301 aabb->extents = (vec3){fabsf(aabb->max_x - aabb->min_x) / 2, fabsf(aabb->max_y - aabb->min_y) / 2, fabsf(aabb->max_z - aabb->min_z) / 2};
302 aabb->center = (vec3){aabb->min_x + aabb->extents.x, aabb->min_y + aabb->extents.y, aabb->min_z + aabb->extents.z};
303
304}
306{
307 vec3 center = aabb->center;
308 aabb->min_x = center.x - aabb->extents.x;
309 aabb->max_x = center.x + aabb->extents.x;
310 aabb->min_y = center.y - aabb->extents.y;
311 aabb->max_y = center.y + aabb->extents.y;
312 aabb->min_z = center.z - aabb->extents.z;
313 aabb->max_z = center.z + aabb->extents.z;
314
315
316}
317
318float triangle_area(vec3 a, vec3 b, vec3 c) {
319 vec3 ab = {b.x - a.x, b.y - a.y, b.z - a.z};
320 vec3 ac = {c.x - a.x, c.y - a.y, c.z - a.z};
321 vec3 cross = {
322 ab.y * ac.z - ab.z * ac.y,
323 ab.z * ac.x - ab.x * ac.z,
324 ab.x * ac.y - ab.y * ac.x
325 };
326 return sqrtf(cross.x * cross.x + cross.y * cross.y + cross.z * cross.z) * 0.5f;
327}
328
329
330bool is_similar_plane(vec3 n1, vec3 n2, vec3 p1, vec3 p2, float epsilon) {
331
332 float dot = vec3_dot(n1, n2);
333 if (dot < 1 - epsilon) return false;
334
335 float d1 = -vec3_dot(n1, p1);
336 float d2 = -vec3_dot(n2, p2);
337 return fabsf(d1 - d2) < epsilon;
338
339}
340
341bool ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c, float *out_t, vec3 *out_hit_point, vec3 * barycentric) {
342
343 vec3 edge1 = vec3_sub(b, a);
344 vec3 edge2 = vec3_sub(c, a);
345 vec3 h = vec3_cross(dir, edge2);
346 float dA = vec3_dot(edge1, h);
347
348 if (fabsf(dA) < 0.000001f) {
349 return false;
350 }
351
352 float f = 1.0f / dA;
353 vec3 s = vec3_sub(origin, a);
354 float u = f * vec3_dot(s, h);
355 if (u < 0.0f || u > 1.0f) {
356 return false;
357 }
358
359 vec3 q = vec3_cross(s, edge1);
360 float v = f * vec3_dot(dir, q);
361 if (v < 0.0f || u + v > 1.0f) {
362 return false;
363 }
364
365 float t = f * vec3_dot(edge2, q);
366 if (t > 0.0f) {
367 *out_t = t;
368
369 float w = 1.0f - u - v;
370 *out_hit_point = vec3_add(
371 vec3_add(vec3_scale(a, w),
372 vec3_scale(b, u)),
373 vec3_scale(c, v));
374 barycentric->x = w;
375 barycentric->y = u;
376 barycentric->z = v;
377
378 return true;
379 }
380
381 // printf("REJECT: t < 0\n");
382 return false;
383}
384
385bool compute_barycentric(const vec2 uv0, const vec2 uv1, const vec2 uv2,
386 const vec2 p, float * b0, float * b1, float * b2)
387{
388 float x0 = uv0.x, y0 = uv0.y;
389 float x1 = uv1.x, y1 = uv1.y;
390 float x2 = uv2.x, y2 = uv2.y;
391 float x = p.x, y = p.y;
392
393 float denom = (y1 - y2)*(x0 - x2) + (x2 - x1)*(y0 - y2);
394 if (fabsf(denom) < 1e-6f)
395 {
396 return false;
397 }
398
399
400 float inv_denom = 1.0f / denom;
401
402 *b0 = ((y1 - y2)*(x - x2) + (x2 - x1)*(y - y2)) * inv_denom;
403 *b1 = ((y2 - y0)*(x - x2) + (x0 - x2)*(y - y2)) * inv_denom;
404 *b2 = 1.0f - *b0 - *b1;
405
406 return true;
407}
408
409void compute_3d_barycentric(vec3 a, vec3 b, vec3 c, vec3 p, float * wA, float * wB, float * wC)
410{
411
412 vec3 v0 = vec3_sub(b, a);
413 vec3 v1 = vec3_sub(c, a);
414 vec3 v2 = vec3_sub(p, a);
415
416 vec3 tn = vec3_cross(v0, v1);
417 float ta = vec3_length(tn);
418
419 float wwC = vec3_length(vec3_cross(v0, v2)) / ta;
420 float wwB = vec3_length(vec3_cross(v2, v1)) / ta;
421 float wwA = 1.0f - wwB - wwC;
422 *wA = wwA;
423 *wB = wwB;
424 *wC = wwC;
425
426
427}
428
429vec3 generate_bounce(uint32_t i, uint32_t N) {
430
431
432 int grid = 8;
433
434 int ix = i % grid;
435 int iy = i / grid;
436
437 float u = (ix + (float)rand() / RAND_MAX) / grid;
438 float v = (iy + (float)rand() / RAND_MAX) / grid;
439 float r = sqrt(u);
440 float theta = 2.0f * M_PI * v;
441
442 float x = r * cos(theta);
443 float y = r * sin(theta);
444 float z = sqrt(fmaxf(0, 1.0f - x*x - y*y));
445 return (vec3){x,y,z};
446}
447
448
450{
451
452 float phi = M_PI * (3.0f - sqrtf(5.0f));
453
454 float y = 1.0f - 2.0f*(i+0.5f)/N;
455
456 float r = sqrtf(1-y*y);
457
458 float theta = phi*i;
459
460 return (vec3){
461 cos(theta)*r,
462 y,
463 sin(theta)*r};
464}
465
467{
468 aabb.center = vec3_add(aabb.center, pos);
469 calculate_aabb_bounds(&aabb);
470 return aabb;
471
472}
473
475{
476
477 aabb.center = mat4_mul_point(mat, aabb.center);
478
479 vec3 AX = mat4_mul_direction(mat, (vec3){1,0,0});
480 vec3 AY = mat4_mul_direction(mat, (vec3){0,1,0});
481 vec3 AZ = mat4_mul_direction(mat, (vec3){0,0,1});
482
483
484 vec3 new_extents;
485 new_extents.x = fabsf(AX.x) * aabb.extents.x + fabsf(AY.x) * aabb.extents.y + fabsf(AZ.x) * aabb.extents.z;
486 new_extents.y = fabsf(AX.y) * aabb.extents.x + fabsf(AY.y) * aabb.extents.y + fabsf(AZ.y) * aabb.extents.z;
487 new_extents.z = fabsf(AX.z) * aabb.extents.x + fabsf(AY.z) * aabb.extents.y + fabsf(AZ.z) * aabb.extents.z;
488 aabb.extents = new_extents;
489 aabb.min_x = aabb.center.x - new_extents.x;
490 aabb.max_x = aabb.center.x + new_extents.x;
491 aabb.min_y = aabb.center.y - new_extents.y;
492 aabb.max_y = aabb.center.y + new_extents.y;
493 aabb.min_z = aabb.center.z - new_extents.z;
494 aabb.max_z = aabb.center.z + new_extents.z;
495
496 return aabb;
497
498}
500{
501 vec3 AX = mat4_mul_direction(mat, (vec3){1,0,0});
502 vec3 AY = mat4_mul_direction(mat, (vec3){0,1,0});
503 vec3 AZ = mat4_mul_direction(mat, (vec3){0,0,1});
504
505
506 vec3 new_extents;
507 new_extents.x = fabsf(AX.x) * aabb.extents.x + fabsf(AY.x) * aabb.extents.y + fabsf(AZ.x) * aabb.extents.z;
508 new_extents.y = fabsf(AX.y) * aabb.extents.x + fabsf(AY.y) * aabb.extents.y + fabsf(AZ.y) * aabb.extents.z;
509 new_extents.z = fabsf(AX.z) * aabb.extents.x + fabsf(AY.z) * aabb.extents.y + fabsf(AZ.z) * aabb.extents.z;
510 aabb.extents = new_extents;
511 aabb.min_x = aabb.center.x - new_extents.x;
512 aabb.max_x = aabb.center.x + new_extents.x;
513 aabb.min_y = aabb.center.y - new_extents.y;
514 aabb.max_y = aabb.center.y + new_extents.y;
515 aabb.min_z = aabb.center.z - new_extents.z;
516 aabb.max_z = aabb.center.z + new_extents.z;
517
518 return aabb;
519}
520
521
522
523
524
525bool vec4_equal(vec4 a, vec4 b, float eps)
526{
527 if (fabsf(a.x - b.x) < eps &&
528 fabsf(a.y - b.y) < eps &&
529 fabsf(a.z - b.z) < eps &&
530 fabsf(a.w - b.w) < eps)
531 {
532
533 return true;
534 }
535 else
536 {
537
538 return false;
539 }
540}
541bool vec3_equal(vec3 a, vec3 b, float eps)
542{
543 if (fabsf(a.x - b.x) < eps &&
544 fabsf(a.y - b.y) < eps &&
545 fabsf(a.z - b.z) < eps)
546 {
547
548 return true;
549 }
550 else
551 {
552
553 return false;
554 }
555}
556
558{
559
560 float eps = 0.001f;
561 int eq = 0;
562 if (vec3_equal(a->pts[A].pos, b->pts[A].pos, 0.001f)) eq++;
563 if (vec3_equal(a->pts[A].pos, b->pts[B].pos, 0.001f)) eq++;
564 if (vec3_equal(a->pts[A].pos, b->pts[C].pos, 0.001f)) eq++;
565 if (vec3_equal(a->pts[B].pos, b->pts[A].pos, 0.001f)) eq++;
566 if (vec3_equal(a->pts[B].pos, b->pts[B].pos, 0.001f)) eq++;
567 if (vec3_equal(a->pts[B].pos, b->pts[C].pos, 0.001f)) eq++;
568 if (vec3_equal(a->pts[C].pos, b->pts[A].pos, 0.001f)) eq++;
569 if (vec3_equal(a->pts[C].pos, b->pts[B].pos, 0.001f)) eq++;
570 if (vec3_equal(a->pts[C].pos, b->pts[C].pos, 0.001f)) eq++;
571
572 // Why 3?
573 if (eq >= 3) return true;
574 return false;
575
576
577}
578bool mesh_faces_are_contiguous(MeshFace * a, vec3 * a_verts, MeshFace * b, vec3 * b_verts)
579{
580
581 float eps = 0.001f;
582 int eq = 0;
583 for (int i = 0; i < 3; i++)
584 {
585 size_t i0 = a->indices[i];
586 size_t i1 = a->indices[(i+1)%3];
587 vec3 a0 = a_verts[i0];
588 vec3 a1 = a_verts[i1];
589 for (int j = 0; j < 3; j++)
590 {
591 size_t j0 = b->indices[j];
592 size_t j1 = b->indices[(j+1)%3];
593 vec3 b0 = b_verts[j0];
594 vec3 b1 = b_verts[j1];
595 if ((vec3_equal(a0, b0, 0.001f) && vec3_equal(a1, b1, 0.001f)) ||
596 (vec3_equal(a0, b1, 0.001f) && vec3_equal(a1, b0, 0.001f)))
597 {
598 return true;
599 }
600
601 }
602
603 }
604
605 return false;
606}
607
608
609
610
611int leftmost_point(vec3 *points, int count) {
612 int min_idx = 0;
613 for (int i = 1; i < count; i++) {
614 if (points[i].x < points[min_idx].x ||
615 (points[i].x == points[min_idx].x && points[i].y < points[min_idx].y)) {
616 min_idx = i;
617 }
618 }
619 return min_idx;
620}
621int leftmost_pointr(vec3 *points, int count) {
622 int min_idx = 0;
623 for (int i = 1; i < count; i++) {
624 if (points[i].z < points[min_idx].z ||
625 (points[i].z == points[min_idx].z && points[i].y < points[min_idx].y)) {
626 min_idx = i;
627 }
628 }
629 return min_idx;
630}
631size_t leftmost_pointi(vec3 *points, size_t * indices, size_t count) {
632 size_t min_idx = 0;
633 for (int i = 1; i < count; i++) {
634 if (points[indices[i]].x < points[indices[min_idx]].x ||
635 (points[indices[i]].x == points[indices[min_idx]].x && points[indices[i]].y < points[indices[min_idx]].y)) {
636 min_idx = i;
637 }
638 }
639 return min_idx;
640}
641size_t leftmost_pointri(vec3 *points, size_t * indices, size_t count) {
642 size_t min_idx = 0;
643 for (int i = 1; i < count; i++) {
644 if (points[indices[i]].z < points[indices[min_idx]].z ||
645 (points[indices[i]].z == points[indices[min_idx]].z && points[indices[i]].y < points[indices[min_idx]].y)) {
646 min_idx = i;
647 }
648 }
649 return min_idx;
650}
651size_t leftmost_pointv2(vec2 *points, size_t count) {
652 size_t min_idx = 0;
653 for (int i = 1; i < count; i++) {
654 if (points[i].x < points[min_idx].x ||
655 (points[i].x == points[min_idx].x && points[i].y < points[min_idx].y)) {
656 min_idx = i;
657 }
658 }
659 return min_idx;
660}
661
662bool ccwv2(vec2 p, vec2 a, vec2 b) {
663 float cross = (a.x - p.x) * (b.y - p.y) - (a.y - p.y) * (b.x - p.x);
664 return cross > 0;
665}
666
667bool ccw(vec3 p, vec3 a, vec3 b) {
668 float cross = (a.x - p.x) * (b.y - p.y) - (a.y - p.y) * (b.x - p.x);
669 return cross > 0;
670}
671bool ccwr(vec3 p, vec3 a, vec3 b) {
672 float cross = (a.z - p.z) * (b.y - p.y) - (a.y - p.y) * (b.z - p.z);
673 return cross > 0;
674}
675
676void compute_convex_hull(vec3 *points, int count, vec3 *out_hull, int *out_count, vec3 normal) {
677 if (count < 3) return;
678
679
680
681 bool z_deg = 1 - fabsf(normal.z) < 0.001f;
682 int left = z_deg ? leftmost_pointr(points, count) : leftmost_point(points, count);
683 int p = left, q;
684 int max_steps = count + 2;
685 int steps = 0;
686 do {
687 if (++steps > max_steps) {
688 fprintf(stderr, "Error: convex hull infinite loop detected.\n");
689 break;
690 }
691 out_hull[(*out_count)++] = points[p];
692 q = (p + 1) % count;
693 for (int i = 0; i < count; i++) {
694
695 if (z_deg)
696 {
697
698 if (ccwr(points[p], points[i], points[q])) {
699
700 q = i;
701 }
702 }
703 else
704 {
705
706 if (ccw(points[p], points[i], points[q])) {
707 q = i;
708 }
709 }
710 }
711 p = q;
712 } while (p != left);
713}
714
715
716
717
718void compute_hull_faces(vec3 * points, size_t * indices, size_t count, size_t * out_hull, size_t * out_count, vec3 normal) {
719
720 if (count < 3) return;
721
722
723 vec3 N = vec3_normalize(normal);
724 vec3 up;
725
726 if (fabsf(N.z) < 0.9f)
727 up = (vec3){0,0,1};
728 else
729 up = (vec3){1,0,0};
730
731 vec3 T = vec3_normalize(vec3_cross(up, N));
732 vec3 B = vec3_cross(N, T);
733
734 vec2 * local_points = malloc(sizeof(vec2) * count);
735
736 for (int i = 0; i < count; i++)
737 {
738 vec3 p = points[indices[i]];
739
740 local_points[i].x = vec3_dot(p, T);
741 local_points[i].y = vec3_dot(p, B);
742 }
743
744 bool z_deg = 1 - fabsf(normal.z) < 0.001f;
745 int left = leftmost_pointv2(local_points, count);
746 int p = left, q;
747 int max_steps = count + 2;
748 int steps = 0;
749 do {
750 if (++steps > max_steps) {
751 fprintf(stderr, "Error: convex hull infinite loop detected.\n");
752 break;
753 }
754 out_hull[(*out_count)++] = indices[p];
755 q = (p + 1) % count;
756 for (int i = 0; i < count; i++) {
757
758
759 if (ccwv2(local_points[p], local_points[i], local_points[q])) {
760 q = i;
761 }
762 }
763 p = q;
764 } while (p != left);
765 free(local_points);
766}
767
768void compute_aabb_from_points(vec3 * points, int point_count, AABB * aabb)
769{
770 aabb->min_x = INFINITY;
771 aabb->min_y = INFINITY;
772 aabb->min_z = INFINITY;
773 aabb->max_x = -INFINITY;
774 aabb->max_y = -INFINITY;
775 aabb->max_z = -INFINITY;
776
777 for (int i = 0; i < point_count; i++)
778 {
779 vec3 p = points[i];
780 if (p.x < aabb->min_x) aabb->min_x = p.x;
781 if (p.y < aabb->min_y) aabb->min_y = p.y;
782 if (p.z < aabb->min_z) aabb->min_z = p.z;
783 if (p.x > aabb->max_x) aabb->max_x = p.x;
784 if (p.y > aabb->max_y) aabb->max_y = p.y;
785 if (p.z > aabb->max_z) aabb->max_z = p.z;
786 }
787
789
790}
791
792
793
795 const vec3 *in,
796 int in_count,
797 vec3 *out,
798 vec3 plane_point,
799 vec3 plane_normal)
800{
801 int out_count = 0;
802
803 for(int i=0;i<in_count;i++)
804 {
805 int j = (i+1)%in_count;
806
807 vec3 A = in[i];
808 vec3 B = in[j];
809
810 float da = vec3_dot(vec3_sub(A, plane_point), plane_normal);
811 float db = vec3_dot(vec3_sub(B, plane_point), plane_normal);
812
813 bool Ain = da > 0;
814 bool Bin = db > 0;
815
816 if(Ain && Bin)
817 {
818 out[out_count++] = B;
819 }
820 else if(Ain && !Bin)
821 {
822 float t = da/(da-db);
823 out[out_count++] = vec3_add(A,
824 vec3_scale(vec3_sub(B, A), t));
825 }
826 else if(!Ain && Bin)
827 {
828 float t = da/(da-db);
829 out[out_count++] = vec3_add(A,
830 vec3_scale(vec3_sub(B, A), t));
831 out[out_count++] = B;
832 }
833 }
834
835 return out_count;
836}
837
838int clip_pts_against_plane( ClipVertex *in, int in_count, ClipVertex *out, vec3 plane_point, vec3 plane_normal)
839{
840 int out_count = 0;
841
842 for(int i=0;i<in_count;i++)
843 {
844 int j = (i+1)%in_count;
845
846 vec3 A = in[i].p;
847 vec3 B = in[j].p;
848
849 float da = vec3_dot(vec3_sub(A, plane_point), plane_normal);
850 float db = vec3_dot(vec3_sub(B, plane_point), plane_normal);
851
852 bool Ain = da > 0;
853 bool Bin = db > 0;
854
855 if(Ain && Bin)
856 {
857 out[out_count].p = B;
858 out[out_count].edge0 = in[j].edge0;
859 out[out_count].edge1 = in[j].edge1;
860 out_count++;
861 }
862 else if(Ain && !Bin)
863 {
864 float t = da/(da-db);
865 out[out_count].p = vec3_add(A, vec3_scale(vec3_sub(B, A), t));
866
867 out[out_count].edge0 = in[i].edge0;
868 out[out_count].edge1 = in[i].edge1;
869 out_count++;
870 }
871 else if(!Ain && Bin)
872 {
873 float t = da/(da-db);
874 out[out_count].p = vec3_add(A, vec3_scale(vec3_sub(B, A), t));
875 out[out_count].edge0 = in[i].edge0;
876 out[out_count].edge1 = in[i].edge1;
877 out_count++;
878 out[out_count].p = B;
879 out[out_count].edge0 = in[j].edge0;
880 out[out_count].edge1 = in[j].edge1;
881 out_count++;
882 }
883 }
884
885 return out_count;
886}
887
888// vec3 compute_hull_centroid(vec3 *hull_points, int hull_count, float bottom, float top) {
889// double signed_area = 0.0;
890// double cx = 0.0, cy = 0.0;
891
892// for (int i = 0; i < hull_count; i++) {
893// int j = (i + 1) % hull_count;
894
895// double xi = hull_points[i].x;
896// double yi = hull_points[i].y;
897// double xj = hull_points[j].x;
898// double yj = hull_points[j].y;
899
900// double a = xi * yj - xj * yi;
901// signed_area += a;
902
903// cx += (xi + xj) * a;
904// cy += (yi + yj) * a;
905// }
906
907// signed_area *= 0.5;
908
909// vec3 centroid;
910// if (fabs(signed_area) < 1e-6) {
911// centroid.x = 0;
912// centroid.y = 0;
913// for (int i = 0; i < hull_count; i++) {
914// centroid.x += hull_points[i].x;
915// centroid.y += hull_points[i].y;
916// }
917// centroid.x /= hull_count;
918// centroid.y /= hull_count;
919// } else {
920// centroid.x = cx / (6.0 * signed_area);
921// centroid.y = cy / (6.0 * signed_area);
922// }
923
924// centroid.z = (bottom + top) * 0.5f;
925
926
927// return centroid;
928// }
929
930
931
932int clip_tri_against_plane(Vector * points, Vector * faces, vec3 plane, vec3 plane_point, vec3 face_points[3], vec3 j_normal)
933{
934 vec3 * pts = CAST_VEC(*points, vec3);
935
936 float d[3];
937 bool s[3] = {0};
938 vec3 p = plane_point;
939 vec3 normal = plane;
940
941 int splits = 0;
942 for (int k = 0; k < 3; k++)
943 {
944
945 vec3 inside = vec3_sub(face_points[k], p);
946 d[k] = vec3_dot(inside, normal);
947
948
949
950 if (d[k] <= 0.0f)
951 {
952 s[k] = true;
953 splits++;
954 }
955
956 }
957 if (splits == 0)
958 {
959
960 return 0;
961
962 }
963 else if (splits == 3)
964 {
965 // dont add face, it is completely outside
966 return 3;
967
968 }
969 else
970 {
971
972 vec3 new_points[3];
973 int new_point_count = 0;
974 for (int k = 0; k < 3; k++)
975 {
976 float da = d[k];
977 float db = d[(k+1) % 3];
978 if (s[k] != s[(k+1) % 3])
979 {
980 // for edge ab
981 // da = dot(n, a-p)
982 // db = dot(n, b-p)
983
984 // t = da / (da - db)
985 // point = a + t(b-a)
986 float t = da / (da - db);
987 new_points[new_point_count++] =
988 vec3_add(face_points[k],
989 vec3_scale(vec3_sub(face_points[(k+1) % 3], face_points[k]), t));
990
991 }
992 }
993
994 vec3 * inside = vector_expand(points, 2);
995 inside[0] = new_points[0];
996 inside[1] = new_points[1];
997 size_t ib = points->count - 2;
998 size_t ic = points->count - 1;
999
1000 int opp_outside_tri_emitted = 0;
1001 int opp_inside_tri_emitted = 0;
1002
1003
1004 int in = 0;
1005 int in_idx[3] = {0};
1006 int in_idx_cnt = 0;
1007 for (int k = 0; k < 3; k++)
1008 {
1009
1010 size_t ia = 0;
1011 size_t oa = 0;
1012 if (!s[k])
1013 {
1014 in++;
1015 in_idx[in_idx_cnt++] = k;
1016
1017 }
1018 }
1019 if (in > 1)
1020 {
1021
1022
1023 inside = vector_expand(points, 1);
1024 inside[0] = face_points[in_idx[0]];
1025 int ia = points->count - 1;
1026
1027 MeshFace * i0 = vector_expand(faces, 1);
1028 i0->indices[0] = ia;
1029 i0->indices[1] = ic;
1030 i0->indices[2] = ib;
1031 i0->normal = j_normal;
1032
1033 }
1034 else
1035 {
1036 inside = vector_expand(points, 1);
1037 inside[0] = face_points[in_idx[0]];
1038 int ia = points->count - 1;
1039
1040 MeshFace * i0 = vector_expand(faces, 1);
1041 i0->indices[0] = ia;
1042 i0->indices[1] = ib;
1043 i0->indices[2] = ic;
1044 i0->normal = j_normal;
1045
1046 inside = vector_expand(points, 1);
1047 inside[0] = face_points[in_idx[1]];
1048 size_t id = points->count - 1;
1049
1050 i0 = vector_expand(faces, 1);
1051 size_t ic1 = 0;
1052 if (vec3_length(vec3_sub(new_points[0], inside[0])) > vec3_length(vec3_sub(new_points[1], inside[0])))
1053 {
1054 ic1 = ic;
1055 }
1056 else
1057 {
1058 ic1 = ib;
1059 }
1060
1061 i0->indices[0] = id;
1062 i0->indices[1] = ic1;
1063 i0->indices[2] = ia;
1064 i0->normal = j_normal;
1065
1066
1067 }
1068
1069
1070
1071 return 1;
1072
1073 }
1074
1075
1076}
1077
1078bool perform_clip(Vector * points, Vector * faces, vec3 planes[6], vec3 plane_points[6], vec3 face_points[3], vec3 normal)
1079{
1080
1081
1082 bool did_clip = false;
1083 int iter = 0;
1084 int safety = 2;
1085 bool has_not_split = true;
1086 bool is_out = false;
1087 int clipped = -1;
1088 vec3 pts_a[64];
1089 pts_a[0] = face_points[0];
1090 pts_a[1] = face_points[1];
1091 pts_a[2] = face_points[2];
1092 int a_cnt = 3;
1093 vec3 pts_b[64];
1094 int b_cnt = 0;
1095
1096 b_cnt = clip_against_plane(pts_a, a_cnt, pts_b, plane_points[0], planes[0]);
1097
1098 for(int i=1; i < b_cnt-1; i++)
1099 {
1100
1101 vec3 e0 = vec3_sub(pts_b[i], pts_b[0]);
1102 vec3 e1 = vec3_sub(pts_b[i+1], pts_b[0]);
1103
1104 float area2 = vec3_length(vec3_cross(e0, e1));
1105 if (area2 < 0.001f) continue;
1106 vec3 * pts = vector_expand(points, 3);
1107 pts[0] = pts_b[0];
1108 pts[1] = pts_b[i];
1109 pts[2] = pts_b[i+1];
1110
1111 MeshFace * f = vector_expand(faces, 1);
1112 f->indices[0] = points->count-3;
1113 f->indices[1] = points->count-2;
1114 f->indices[2] = points->count-1;
1115 f->normal = normal;
1116 }
1117 return did_clip;
1118}
1119
1120
1121
1122void clip_triangle(Vector * points, Vector * faces, vec3 planes[6], vec3 plane_points[6], vec3 a, vec3 b, vec3 c, vec3 normal)
1123{
1124
1125
1126 Vector new_verts;
1127 init_vector(&new_verts, 0, sizeof(vec3));
1128 Vector new_faces;
1129 init_vector(&new_faces, 0, sizeof(MeshFace));
1130
1131
1132
1133 int did_cut = false;
1134 bool cannot_cut = false;
1135
1136 int safety = 100;
1137 int iter = 0;
1138 perform_clip(&new_verts, &new_faces, planes, plane_points, (vec3[]){a,b,c}, normal);
1139
1140 if (new_verts.count > 0 && new_faces.count > 0)
1141 {
1142 if (points->capacity > 0)
1143 {
1144
1145 if (points->capacity < points->count + new_verts.count)
1146 {
1147 points->capacity = (points->capacity * 2) + new_verts.count + points->count;
1148 // printf("%p\n", points->buffer);
1149 points->buffer = realloc(points->buffer, sizeof(vec3) * points->capacity);
1150 }
1151 }
1152 else
1153 {
1154 points->capacity = new_verts.count * 2;
1155 points->buffer = malloc(sizeof(vec3) * points->capacity);
1156 }
1157 vec3 * pts = CAST_VEC(*points, vec3);
1158 vec3 * new_pts = CAST_VEC(new_verts, vec3);
1159 for (int i = 0; i < new_verts.count; i++)
1160 {
1161 int index = i + points->count;
1162 pts[index] = new_pts[i];
1163 }
1164 if (faces->capacity > 0)
1165 {
1166
1167 if (faces->capacity < faces->count + new_faces.count)
1168 {
1169 faces->capacity = (faces->capacity * 2) + new_faces.count + faces->count;
1170 faces->buffer = realloc(faces->buffer, sizeof(MeshFace) * faces->capacity);
1171 }
1172 }
1173 else
1174 {
1175
1176 faces->capacity = new_faces.count * 2;
1177 faces->buffer = malloc(sizeof(MeshFace) * faces->capacity);
1178 }
1179 MeshFace * f = CAST_VEC(*faces, MeshFace);
1180
1181 MeshFace * mfs = CAST_VEC(new_faces, MeshFace);
1182 for (int i = 0; i < new_faces.count; i++)
1183 {
1184 int index = i + faces->count;
1185 f[index] = mfs[i];
1186 f[index].indices[0] += points->count;
1187 f[index].indices[1] += points->count;
1188 f[index].indices[2] += points->count;
1189 }
1190 points->count += new_verts.count;
1191 faces->count += new_faces.count;
1192
1193 if (new_verts.buffer != NULL) free(new_verts.buffer);
1194 if (new_faces.buffer != NULL) free(new_faces.buffer);
1195 }
1196
1197
1198
1199}
1200
1201
1202
1203
1205{
1206
1207 vec3 v0 = vec3_sub(B, A);
1208 vec3 v1 = vec3_sub(C, A);
1209 vec3 v2 = vec3_sub(P, A);
1210
1211 float d00 = vec3_dot(v0, v0);
1212 float d01 = vec3_dot(v0, v1);
1213 float d11 = vec3_dot(v1, v1);
1214 float d20 = vec3_dot(v2, v0);
1215 float d21 = vec3_dot(v2, v1);
1216
1217
1218 float denom = d00 * d11 - d01 * d01;
1219
1220 float v = (d11 * d20 - d01 * d21) / denom;
1221 float w = (d00 * d21 - d01 * d20) / denom;
1222 float u = 1.0f - v - w;
1223
1224
1225 const float eps = 0.01f;
1226
1227 bool inside =
1228 u >= -eps &&
1229 v >= -eps &&
1230 w >= -eps;
1231
1232 return inside;
1233}
1234
1235vec3 project_point_plane(vec3 point, vec3 plane_point, vec3 normal)
1236{
1237
1238 float d = vec3_dot(vec3_sub(point, plane_point), normal);
1239
1240 return vec3_sub(point, vec3_scale(normal, d));
1241
1242}
1243
1245{
1246 vec3 ab = vec3_sub(b, a);
1247
1248 float t = vec3_dot(vec3_sub(p, a), ab) / vec3_dot(ab, ab);
1249
1250 t = clamp(t, 0.0f, 1.0f);
1251
1252 return vec3_add(a, vec3_scale(ab, t));
1253
1254}
1255
1257
1258 vec2 ab = {b.x - a.x, b.y - a.y};
1259 vec2 ap = {p.x - a.x, p.y - a.y};
1260
1261 float t = (ap.x * ab.x + ap.y * ab.y) / (ab.x * ab.x + ab.y * ab.y);
1262 t = fmaxf(0.0f, fminf(1.0f, t));
1263
1264 vec2 closest = { a.x + t * ab.x, a.y + t * ab.y };
1265 float dx = p.x - closest.x;
1266 float dy = p.y - closest.y;
1267
1268 return sqrtf(dx*dx + dy*dy);
1269
1270}
1271
1272
1273vec3 segment_plane_distance(vec3 proposed, vec3 pts[3], vec3 normal)
1274{
1275
1276 vec3 b = proposed;
1277 float da = vec3_dot(vec3_sub(proposed, pts[0]), normal);
1278 float db = vec3_dot(vec3_sub(b, pts[0]), normal);
1279 float dd = da * db;
1280 if (dd > 0) return (vec3){999,999,999};
1281
1282 float t = da/(da-db);
1283
1284 return vec3_add(proposed, vec3_scale(vec3_sub(b, proposed), t));
1285
1286}
1287
1288vec3 point_triangle_distance(vec3 proposed, vec3 pts[3], vec3 normal)
1289{
1290 vec3 p = project_point_plane(proposed, pts[0], normal);
1291
1292
1293
1294 vec3 ab = closest_point_segment(proposed, pts[0], pts[1]);
1295 vec3 bc = closest_point_segment(proposed, pts[1], pts[2]);
1296 vec3 ac = closest_point_segment(proposed, pts[2], pts[0]);
1297
1298 float ab_dist = vec3_length(vec3_sub(proposed, ab));
1299 float bc_dist = vec3_length(vec3_sub(proposed, bc));
1300 float ac_dist = vec3_length(vec3_sub(proposed, ac));
1301 float p_dist = vec3_length(vec3_sub(p, proposed));
1302
1303 if (ab_dist <= bc_dist && ab_dist <= ac_dist && ab_dist <= p_dist)
1304 {
1305 return ab;
1306 }
1307 if (bc_dist <= ab_dist && bc_dist <= ac_dist && bc_dist <= p_dist)
1308 {
1309 return bc;
1310 }
1311 if (ac_dist <= ab_dist && ac_dist <= bc_dist && ac_dist <= p_dist)
1312 {
1313 return ac;
1314
1315 }
1316 if (p_dist <= ab_dist && p_dist <= bc_dist && p_dist <= ac_dist)
1317 {
1318 return p;
1319 }
1320 return p;
1321
1322}
1323
1324
1325
1326// From Ericson
1328{
1329
1330 // Check if P in vertex region outside A
1331 vec3 ab = vec3_sub(b, a);
1332 vec3 ac = vec3_sub(c, a);
1333 vec3 ap = vec3_sub(p, a);
1334 float d1 = vec3_dot(ab, ap);
1335 float d2 = vec3_dot(ac, ap);
1336 if (d1 <= 0.0f && d2 <= 0.0f) return a; // barycentric coordinates (1,0,0)
1337
1338 // Check if P in vertex region outside B
1339 vec3 bp = vec3_sub(p, b);
1340 float d3 = vec3_dot(ab, bp);
1341 float d4 = vec3_dot(ac, bp);
1342 if (d3 >= 0.0f && d4 <= d3) return b; // barycentric coordinates (0,1,0)
1343
1344 // Check if P in edge region of AB, if so return projection of P onto AB
1345 float vc = d1*d4 - d3*d2;
1346 if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f) {
1347
1348 float v = d1 / (d1 - d3);
1349 return vec3_add(a, vec3_scale(ab, v)); // barycentric coordinates (1-v,v,0)
1350
1351 }
1352
1353 // Check if P in vertex region outside C
1354 vec3 cp = vec3_sub(p, c);
1355 float d5 = vec3_dot(ab, cp);
1356 float d6 = vec3_dot(ac, cp);
1357 if (d6 >= 0.0f && d5 <= d6) return c; // barycentric coordinates (0,0,1)
1358
1359
1360 // // Check if P in edge region of AC, if so return projection of P onto AC
1361 float vb = d5*d2 - d1*d6;
1362 if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f) {
1363 float w = d2 / (d2 - d6);
1364 return vec3_add(a, vec3_scale(ac, w)); // barycentric coordinates (1-w,0,w)
1365 }
1366 // // Check if P in edge region of BC, if so return projection of P onto BC
1367 float va = d3*d6 - d5*d4;
1368 if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f) {
1369
1370 float w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
1371 return vec3_add(b, vec3_scale(vec3_sub(c, b), w)); // barycentric coordinates (0,1-w,w)
1372
1373 }
1374 // // P inside face region. Compute Q through its barycentric coordinates (u,v,w)
1375 float denom = 1.0f / (va + vb + vc);
1376 float v = vb * denom;
1377 float w = vc * denom;
1378 return vec3_add(vec3_add(a, vec3_scale(ab, v)), vec3_scale(ac, w)); // = u*a + v*b + w*c, u = va * denom = 1.0f - v - w
1379
1380}
1381#define DEGREES_TO_RAD (3.14159265358979323846 / 180.0)
1382vec3 spherical_to_cartesian(float azimuth_deg, float elevation_deg, float radius) {
1383
1384 float azimuth_rad = azimuth_deg * DEGREES_TO_RAD;
1385 float elevation_rad = elevation_deg * DEGREES_TO_RAD;
1386
1387 float projected_rad = radius * cosf(elevation_rad);
1388
1389 vec3 pos =
1390 {
1391 -projected_rad * sinf(azimuth_rad),
1392 radius * sinf(elevation_rad),
1393 -projected_rad * cosf(azimuth_rad)
1394
1395 };
1396
1397
1398 return pos;
1399}
RayHit ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c)
vec3 generate_sphere_dir(int i, int N)
float rand()
bool test_sphere_aabb(vec3 light_pos, float light_radius, vec4 min, vec4 max)
uniform vec4 planes[6]
AABBBounds transform_aabb_by_model_matrix(MeshNode mn, mat4 mat)
vec3 compute_3d_barycentric(vec3 a, vec3 b, vec3 c, vec3 p)
vec3 vec3_cross(vec3 a, vec3 b)
Definition mathf.h:80
float vec3_dot(vec3 a, vec3 b)
Definition mathf.h:322
vec4 vec4_add(vec4 a, vec4 b)
Definition mathf.h:290
vec3 vec3_normalize(vec3 v)
Definition mathf.h:46
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:248
vec3 vec3_mul(vec3 a, vec3 b)
Definition mathf.h:298
vec4 vec4_mul(vec4 a, vec4 b)
Definition mathf.h:302
vec2 vec2_add(vec2 a, vec2 b)
Definition mathf.h:274
vec2 vec2_scale(vec2 a, float b)
Definition mathf.h:244
vec3 vec3_sub(vec3 a, vec3 b)
Definition mathf.h:266
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:286
vec4 vec4_scale(vec4 a, float b)
Definition mathf.h:252
float clamp(float a, float b, float c)
Definition mathf.h:379
float vec3_length(vec3 v)
Definition mathf.h:328
float x
Definition types.h:11
float a1
Definition types.h:21
float w
Definition types.h:13
vec3 normal
Definition types.h:284
float z
Definition types.h:13
float d3
Definition types.h:21
float c2
Definition types.h:21
float b0
Definition types.h:21
float c1
Definition types.h:21
float x
Definition types.h:13
float a2
Definition types.h:21
@ B
Definition types.h:26
@ A
Definition types.h:26
@ C
Definition types.h:26
float x
Definition types.h:12
float max_z
Definition types.h:39
int edge1
Definition types.h:331
float min_x
Definition types.h:37
size_t type_size
Definition types.h:209
float max_y
Definition types.h:38
float b3
Definition types.h:21
float y
Definition types.h:11
float y
Definition types.h:13
size_t indices[3]
Definition types.h:283
vec3 p
Definition types.h:330
float y
Definition types.h:12
float d1
Definition types.h:21
float a3
Definition types.h:21
int edge0
Definition types.h:331
float c0
Definition types.h:21
vec3 pos
Definition types.h:220
float z
Definition types.h:12
size_t count
Definition types.h:209
float min_z
Definition types.h:39
float b2
Definition types.h:21
float d0
Definition types.h:21
float min_y
Definition types.h:38
Point pts[3]
Definition types.h:273
size_t capacity
Definition types.h:209
void * buffer
Definition types.h:210
float a0
Definition types.h:21
float c3
Definition types.h:21
float b1
Definition types.h:21
float d2
Definition types.h:21
float max_x
Definition types.h:37
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13
void clip_triangle(Vector *points, Vector *faces, vec3 planes[6], vec3 plane_points[6], vec3 a, vec3 b, vec3 c, vec3 normal)
Definition utilities.c:1122
AABB transform_aabb_by_model_matrix_rotation(AABB aabb, mat4 mat)
Definition utilities.c:499
bool vec4_equal(vec4 a, vec4 b, float eps)
Definition utilities.c:525
float fbm_noise(vec3 p)
Definition utilities.c:193
bool ccwv2(vec2 p, vec2 a, vec2 b)
Definition utilities.c:662
void compute_convex_hull(vec3 *points, int count, vec3 *out_hull, int *out_count, vec3 normal)
Definition utilities.c:676
vec3 point_triangle_distance(vec3 proposed, vec3 pts[3], vec3 normal)
Definition utilities.c:1288
bool perform_clip(Vector *points, Vector *faces, vec3 planes[6], vec3 plane_points[6], vec3 face_points[3], vec3 normal)
Definition utilities.c:1078
bool is_similar_plane(vec3 n1, vec3 n2, vec3 p1, vec3 p2, float epsilon)
Definition utilities.c:330
void init_vector(Vector *v, size_t i, size_t type_size)
Definition utilities.c:74
size_t leftmost_pointv2(vec2 *points, size_t count)
Definition utilities.c:651
bool point_in_triangle(vec3 P, vec3 A, vec3 B, vec3 C)
Definition utilities.c:1204
size_t leftmost_pointi(vec3 *points, size_t *indices, size_t count)
Definition utilities.c:631
bool compute_barycentric(const vec2 uv0, const vec2 uv1, const vec2 uv2, const vec2 p, float *b0, float *b1, float *b2)
Definition utilities.c:385
bool vec3_equal(vec3 a, vec3 b, float eps)
Definition utilities.c:541
int clip_against_plane(const vec3 *in, int in_count, vec3 *out, vec3 plane_point, vec3 plane_normal)
Definition utilities.c:794
void calculate_aabb_extents(AABB *aabb)
Definition utilities.c:298
int vector_alloc(Vector *v, size_t i)
Definition utilities.c:12
int leftmost_pointr(vec3 *points, int count)
Definition utilities.c:621
void compute_aabb_from_points(vec3 *points, int point_count, AABB *aabb)
Definition utilities.c:768
vec3 closest_point_segment(vec3 p, vec3 a, vec3 b)
Definition utilities.c:1244
void init_aabb(AABB *a, vec3 center, vec3 extents)
Definition utilities.c:258
bool aabbs_intersect(AABB *a, AABB *b)
Definition utilities.c:276
int clip_pts_against_plane(ClipVertex *in, int in_count, ClipVertex *out, vec3 plane_point, vec3 plane_normal)
Definition utilities.c:838
int leftmost_point(vec3 *points, int count)
Definition utilities.c:611
float mod289(float x)
Definition utilities.c:134
bool ccwr(vec3 p, vec3 a, vec3 b)
Definition utilities.c:671
float triangle_area(vec3 a, vec3 b, vec3 c)
Definition utilities.c:318
vec3 generate_bounce(uint32_t i, uint32_t N)
Definition utilities.c:429
void free_vector(Vector *v)
Definition utilities.c:90
AABB transform_aabb_by_position(AABB aabb, vec3 pos)
Definition utilities.c:466
vec4 perm(vec4 x)
Definition utilities.c:139
vec3 vec3_fract(vec3 x)
Definition utilities.c:161
float point_to_segment_dist(vec2 p, vec2 a, vec2 b)
Definition utilities.c:1256
void calculate_aabb_bounds(AABB *aabb)
Definition utilities.c:305
bool ccw(vec3 p, vec3 a, vec3 b)
Definition utilities.c:667
int clip_tri_against_plane(Vector *points, Vector *faces, vec3 plane, vec3 plane_point, vec3 face_points[3], vec3 j_normal)
Definition utilities.c:932
bool triangles_are_contiguous(Triangle *a, Triangle *b)
Definition utilities.c:557
void compute_hull_faces(vec3 *points, size_t *indices, size_t count, size_t *out_hull, size_t *out_count, vec3 normal)
Definition utilities.c:718
vec3 segment_plane_distance(vec3 proposed, vec3 pts[3], vec3 normal)
Definition utilities.c:1273
bool mesh_faces_are_contiguous(MeshFace *a, vec3 *a_verts, MeshFace *b, vec3 *b_verts)
Definition utilities.c:578
vec3 spherical_to_cartesian(float azimuth_deg, float elevation_deg, float radius)
Definition utilities.c:1382
uint16_t float32_to_float16(float f)
Definition utilities.c:110
vec3 closest_point_triangle(vec3 p, vec3 a, vec3 b, vec3 c)
Definition utilities.c:1327
size_t leftmost_pointri(vec3 *points, size_t *indices, size_t count)
Definition utilities.c:641
vec4 vec4_fract(vec4 x)
Definition utilities.c:176
void * vector_expand(Vector *v, size_t i)
Definition utilities.c:49
float fbm(vec3 p)
Definition utilities.c:222
#define DEGREES_TO_RAD
Definition utilities.c:1381
int vector_push(Vector *v, void *o)
Definition utilities.c:25
vec3 project_point_plane(vec3 point, vec3 plane_point, vec3 normal)
Definition utilities.c:1235
mat4 compose_2d_transform(vec2 transform, float angle, vec2 scale)
Definition utilities.c:97
vec3 vec3_clamp(vec3 a, vec3 b, vec3 c)
Definition utilities.h:91
#define CAST_VEC(vec, type)
Definition utilities.h:134
vec3 mat4_mul_direction(const mat4 m, vec3 v)
Definition utilities.h:323
vec3 mat4_mul_point(mat4 m, vec3 v)
Definition utilities.h:313
#define M_PI
Definition utilities.h:52