-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounds.all.c
166 lines (148 loc) · 5.41 KB
/
bounds.all.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
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
// File created: 2011-08-21 16:17:09
#include "bounds.all.h"
#include <assert.h>
size_t mushbounds_size(const mushbounds* bounds) {
size_t sz = 1;
for (mushdim i = 0; i < MUSHSPACE_DIM; ++i)
sz *= bounds->end.v[i] - bounds->beg.v[i] + 1;
return sz;
}
size_t mushbounds_clamped_size(const mushbounds* bounds) {
size_t sz = mush_size_t_add_clamped(
(size_t)bounds->end.v[0] - (size_t)bounds->beg.v[0], 1);
for (mushdim i = 1; i < MUSHSPACE_DIM; ++i)
sz = mush_size_t_mul_clamped(
sz, mush_size_t_add_clamped(
(size_t)bounds->end.v[i] - (size_t)bounds->beg.v[i], 1));
return sz;
}
bool mushbounds_contains(const mushbounds* bounds, mushcoords pos) {
if (pos.x < bounds->beg.x || pos.x > bounds->end.x) return false;
#if MUSHSPACE_DIM >= 2
if (pos.y < bounds->beg.y || pos.y > bounds->end.y) return false;
#if MUSHSPACE_DIM >= 3
if (pos.z < bounds->beg.z || pos.z > bounds->end.z) return false;
#endif
#endif
return true;
}
bool mushbounds_safe_contains(const mushbounds* bounds, mushcoords pos) {
for (mushdim i = 0; i < MUSHSPACE_DIM; ++i)
if (!mushbounds_safe_contains1(bounds, pos, i))
return false;
return true;
}
bool mushbounds_safe_contains1(
const mushbounds* bounds, mushcoords pos, mushdim i)
{
return bounds->beg.v[i] > bounds->end.v[i]
? pos.v[i] >= bounds->beg.v[i] || pos.v[i] <= bounds->end.v[i]
: pos.v[i] >= bounds->beg.v[i] && pos.v[i] <= bounds->end.v[i];
}
bool mushbounds_contains_bounds(const mushbounds* a, const mushbounds* b) {
return mushbounds_contains(a, b->beg) && mushbounds_contains(a, b->end);
}
bool mushbounds_overlaps(const mushbounds* a, const mushbounds* b) {
for (mushdim i = 0; i < MUSHSPACE_DIM; ++i)
if (a->beg.v[i] > b->end.v[i] || b->beg.v[i] > a->end.v[i])
return false;
return true;
}
bool mushbounds_safe_overlaps(const mushbounds* a, const mushbounds* b) {
for (mushdim i = 0; i < MUSHSPACE_DIM; ++i) {
if (a->beg.v[i] > a->end.v[i]) {
if (a->beg.v[i] > b->end.v[i] && b->beg.v[i] > a->end.v[i])
return false;
} else {
if (a->beg.v[i] > b->end.v[i] || b->beg.v[i] > a->end.v[i])
return false;
}
}
return true;
}
#if !MUSHSPACE_93
void mushbounds_get_overlap(
const mushbounds* a, const mushbounds* b, mushbounds* overlap)
{
overlap->beg = a->beg; mushcoords_max_into(&overlap->beg, b->beg);
overlap->end = a->end; mushcoords_min_into(&overlap->end, b->end);
assert (mushbounds_contains_bounds(a, overlap));
assert (mushbounds_contains_bounds(b, overlap));
}
void mushbounds_expand_to_cover(mushbounds* cover, const mushbounds* b) {
mushcoords_min_into(&cover->beg, b->beg);
mushcoords_max_into(&cover->end, b->end);
}
#if MUSHSPACE_DIM > 1
bool mushbounds_on_same_axis(
const mushbounds* a, const mushbounds* b, mushdim x)
{
return a->beg.v[x] == b->beg.v[x] && a->end.v[x] == b->end.v[x];
}
#endif
bool mushbounds_can_fuse(const mushbounds* a, const mushbounds* b) {
bool overlap = mushbounds_overlaps(a, b);
for (mushdim i = 0; i < MUSHSPACE_DIM; ++i) {
if (a->beg.v[i] == b->beg.v[i] && a->end.v[i] == b->end.v[i])
continue;
if (!( overlap
|| mushcell_add_clamped(a->end.v[i], 1) == b->beg.v[i]
|| mushcell_add_clamped(b->end.v[i], 1) == a->beg.v[i]))
return false;
for (mushdim j = i+1; j < MUSHSPACE_DIM; ++j)
if (a->beg.v[j] != b->beg.v[j] || a->end.v[j] != b->end.v[j])
return false;
#if MUSHSPACE_DIM > 1 && !defined(NDEBUG)
bool on_same_axis = false;
for (mushdim d = 0; d < MUSHSPACE_DIM; ++d)
on_same_axis = on_same_axis || mushbounds_on_same_axis(a, b, d);
assert (on_same_axis);
#endif
return true;
}
return false;
}
void mushbounds_tessellate(
mushbounds* bounds, mushcoords pos, const mushbounds* avoid)
{
if (!mushbounds_overlaps(bounds, avoid))
return;
assert (mushbounds_contains(bounds, pos));
for (mushdim i = 0; i < MUSHSPACE_DIM; ++i) {
// This could be improved, consider for instance the bottommost box in
// the following graphic and its current tessellation:
//
// +-------+ +--*--*-+
// | | |X . . |
// | | | . . |
// | +--- *..*..+---
// | | | . |
// | +--| *..+--|
// | | | | | |
// | | | | | |
// +--| | +--| |
//
// (Note that this isn't actually a tessellation: all points will get
// a rectangle containing the rectangle at X.)
//
// Any of the following three would be an improvement (and they would
// actually be tessellations):
//
// +--*--*-+ +-------+ +-----*-+
// | . . | | | | . |
// | . . | | | | . |
// | . +--- *.....+--- | +---
// | . | | | | |
// | +--| *..+--| *..+--|
// | | | | | | | | |
// | | | | | | | | |
// +--| | +--| | +--| |
mushcell ab = avoid->beg.v[i],
ae = avoid->end.v[i],
p = pos.v[i];
if (ae < p) bounds->beg.v[i] = mushcell_max(bounds->beg.v[i], ae+1);
if (ab > p) bounds->end.v[i] = mushcell_min(bounds->end.v[i], ab-1);
}
assert (!mushbounds_overlaps(bounds, avoid));
}
#endif // !MUSHSPACE_93