-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaabb.98.c
110 lines (93 loc) · 2.9 KB
/
aabb.98.c
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
// File created: 2011-08-06 17:57:40
#include "aabb.98.h"
#include <assert.h>
#include <string.h>
#include "stdlib.any.h"
void mushaabb_make(mushaabb* aabb, const mushbounds* bounds) {
mushaabb_make_unsafe(aabb, bounds);
mushaabb_finalize(aabb);
}
void mushaabb_make_unsafe(mushaabb* aabb, const mushbounds* bounds) {
*aabb = (mushaabb){.bounds = *bounds};
}
void mushaabb_finalize(mushaabb* aabb) {
for (mushdim i = 0; i < MUSHSPACE_DIM; ++i)
assert (aabb->bounds.beg.v[i] <= aabb->bounds.end.v[i]);
aabb->size = aabb->bounds.end.x - aabb->bounds.beg.x + 1;
#if MUSHSPACE_DIM >= 2
aabb->width = aabb->size;
aabb->size *= aabb->bounds.end.y - aabb->bounds.beg.y + 1;
#endif
#if MUSHSPACE_DIM >= 3
aabb->area = aabb->size;
aabb->size *= aabb->bounds.end.z - aabb->bounds.beg.z + 1;
#endif
}
bool mushaabb_alloc(mushaabb* aabb) {
assert (aabb->size);
aabb->data = malloc(aabb->size * sizeof *aabb->data);
if (!aabb->data)
return false;
mushcell_space(aabb->data, aabb->size);
return true;
}
mushcell mushaabb_get(const mushaabb* aabb, mushcoords c) {
assert (mushbounds_contains(&aabb->bounds, c));
return aabb->data[mushaabb_get_idx(aabb, c)];
}
void mushaabb_put(mushaabb* aabb, mushcoords p, mushcell c) {
assert (mushbounds_contains(&aabb->bounds, p));
aabb->data[mushaabb_get_idx(aabb, p)] = c;
}
mushcell mushaabb_get_no_offset(const mushaabb* aabb, mushcoords c) {
// Can't assert contains(c + beg) since no_offset usage typically means that
// our beg/end don't match data.
return aabb->data[mushaabb_get_idx_no_offset(aabb, c)];
}
void mushaabb_put_no_offset(mushaabb* aabb, mushcoords p, mushcell c) {
aabb->data[mushaabb_get_idx_no_offset(aabb, p)] = c;
}
mushcell mushaabb_getter_no_offset(const void* aabb, mushcoords c) {
return mushaabb_get_no_offset(aabb, c);
}
size_t mushaabb_get_idx(const mushaabb* a, mushcoords c) {
return mushaabb_get_idx_no_offset(a, mushcoords_sub(c, a->bounds.beg));
}
size_t mushaabb_get_idx_no_offset(const mushaabb* aabb, mushcoords c) {
size_t i = c.x;
#if MUSHSPACE_DIM >= 2
i += aabb->width * c.y;
#if MUSHSPACE_DIM >= 3
i += aabb->area * c.z;
#endif
#else
(void)aabb;
#endif
return i;
}
bool mushaabb_can_direct_copy(const mushaabb* copier, const mushaabb* copiee) {
#if MUSHSPACE_DIM == 1
(void)copier; (void)copiee;
return true;
#else
if (copiee->size == copiee->width && copiee->size <= copier->width)
return true;
return copier->width == copiee->width
#if MUSHSPACE_DIM >= 3
&& copier->area == copiee->area
#endif
;
#endif
}
bool mushaabb_can_direct_copy_area(
const mushaabb* copier, const mushaabb* copiee, const mushaabb* owner)
{
#if MUSHSPACE_DIM >= 2
if (copiee->width != owner->width) return false;
#if MUSHSPACE_DIM >= 3
if (copiee->area != owner->area) return false;
#endif
#endif
(void)owner;
return mushaabb_can_direct_copy(copier, copiee);
}