-
Notifications
You must be signed in to change notification settings - Fork 0
/
geom-box.stanza
232 lines (179 loc) · 7.46 KB
/
geom-box.stanza
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
defpackage geom/box :
import core
import utils/rnd
import geom/geom
import geom/vec
import geom/mat
val @doc-box = "BOXES -- tracking bounds"
#for (Prim in [Int Float Double Int Float Double Int Float Double]
BoxElt in [Vec3i Vec3f Vec3 Vec2i Vec2f Vec2 Int Float Double ]
BoxName in ["Box3i" "Box3f" "Box3" "Box2i" "Box2f" "Box2" "Box1i" "Box1f" "Box1"]
BoxElt-fill in [Vec3-fill Vec3-fill Vec3-fill Vec2-fill Vec2-fill Vec2-fill to-int to-float to-double]
Box in [Box3i Box3f Box3 Box2i Box2f Box2 Box1i Box1f Box1]
neg-inf in [neg-inf-i, neg-inf-f, neg-inf-d, neg-inf-i, neg-inf-f, neg-inf-d neg-inf-i, neg-inf-f, neg-inf-d]
pos-inf in [pos-inf-i, pos-inf-f, pos-inf-d, pos-inf-i, pos-inf-f, pos-inf-d pos-inf-i, pos-inf-f, pos-inf-d]
pos-inf-box in [pos-inf-box3i, pos-inf-box3f, pos-inf-box3, pos-inf-box2i, pos-inf-box2f, pos-inf-box2 pos-inf-box1i, pos-inf-box1f, pos-inf-box1d]
neg-inf-box in [neg-inf-box3i, neg-inf-box3f, neg-inf-box3, neg-inf-box2i, neg-inf-box2f, neg-inf-box2 neg-inf-box1i, neg-inf-box1f, neg-inf-box1d]
x2 in [2 2.0f 2.0, 2, 2.0f, 2.0, 2, 2.0f, 2.0]
x0 in [0 0.0f 0.0, 0, 0.0f, 0.0, 0, 0.0f, 0.0]) :
public defstruct Box <: Geom & Equalable :
lo: BoxElt
hi: BoxElt
public defn Box (v:BoxElt) -> Box :
Box(v, v)
defmethod print (o:OutputStream, b:Box) :
print-all(o, [BoxName "(", lo(b), ",", hi(b) ")"])
public defn contains? (b:Box, p:BoxElt) -> True|False :
not empty?(b & Box(p))
; WARNING: Integer overflow can cause this to be wrong if boxes are very large
public defn center (b:Box) -> BoxElt :
(lo(b) + hi(b)) / x2
; WARNING: Integer overflow can cause this to be wrong if boxes are very large
public defn dims (b:Box) -> BoxElt :
hi(b) - lo(b)
public defn dims-to-box (dims:BoxElt) -> Box :
val hdims = dims / x2
Box((- hdims), hdims)
public defn union (b:Box, v:BoxElt) -> Box :
Box(min(lo(b), v), max(hi(b), v))
public defn union (b0:Box, b1:Box) -> Box :
Box(min(lo(b0), lo(b1)), max(hi(b0), hi(b1)))
public defn bit-or (b0:Box, b1:Box) -> Box :
union(b0, b1)
public defn bit-and (b0:Box, b1:Box) -> Box :
Box(max(lo(b0), lo(b1)), min(hi(b0), hi(b1)))
public defn pos-inf-box () -> Box:
Box(BoxElt-fill(neg-inf), BoxElt-fill(pos-inf))
public defn neg-inf-box () -> Box:
Box(BoxElt-fill(pos-inf), BoxElt-fill(neg-inf))
public defn fatten (b:Box, amount:BoxElt) -> Box :
Box(lo(b) - amount, hi(b) + amount)
; Ideally this would just be an overload of `fatten`, but Prim and BoxElt are the same type for Box1
public defn thicken (b:Box, dims:Prim) -> Box :
fatten(b, BoxElt-fill(dims))
public defn plus (b:Box, off:BoxElt) -> Box :
Box(lo(b) + off, hi(b) + off)
public defn minus (b:Box, off:BoxElt) -> Box :
Box(lo(b) - off, hi(b) - off)
public defn times (f:Prim, b:Box) -> Box :
Box(f * lo(b), f * hi(b))
defmethod equal? (b1:Box, b2:Box) :
lo(b1) == lo(b2) and hi(b1) == hi(b2)
public defn contains? (b0:Box, b1:Box) -> True|False :
contains?(b0, lo(b1)) and contains?(b0, hi(b1))
public defn clamp (b:Box, v:BoxElt) -> BoxElt :
max(lo(b), min(hi(b), v))
public defn bounding-box (bs:Seqable<Box>) :
var result = neg-inf-box()
for b in bs do:
result = union(result, b)
result
public defn indices (bounds:Box3i) -> Seq<Vec3i> :
generate<Vec3i> :
for k in z(lo(bounds)) through z(hi(bounds)) do :
for j in y(lo(bounds)) through y(hi(bounds)) do :
for i in x(lo(bounds)) through x(hi(bounds)) do :
yield(Vec3i(i, j, k))
public defn indices (bounds:Box2i) -> Seq<Vec2i> :
generate<Vec2i> :
for j in y(lo(bounds)) through y(hi(bounds)) do :
for i in x(lo(bounds)) through x(hi(bounds)) do :
yield(Vec2i(i, j))
#for (BoxElt in [Int Float Double]
Box in [Box1i Box1f Box1]
Box2 in [Box2i Box2f Box2]
Box3 in [Box3i Box3f Box3]
x0 in [0 0.0f 0.0]) :
public defn x (b:Box2): Box(x(lo(b)), x(hi(b)))
public defn y (b:Box2): Box(y(lo(b)), y(hi(b)))
public defn x (b:Box3): Box(x(lo(b)), x(hi(b)))
public defn y (b:Box3): Box(y(lo(b)), y(hi(b)))
public defn z (b:Box3): Box(z(lo(b)), z(hi(b)))
public defn xy (b:Box) : Box2(xy(lo(b)), xy(hi(b)))
public defn xyz (b:Box) : Box3(xyz(lo(b)), xyz(hi(b)))
public defn empty? (b:Box) -> True|False :
dims(b) < x0
public defn corners (b:Box) -> Tuple<BoxElt> :
[lo(b), hi(b)]
public defn intersects? (b0:Box, b1:Box) -> True|False :
not (hi(b0) < lo(b1) or lo(b0) > hi(b1))
public defn closest-points (b0:Box, b1:Box) -> [BoxElt, BoxElt] :
; +----+ +-----+
if hi(b0) <= lo(b1):
; Don't intersect, b0 strictly less than b1
[hi(b0), lo(b1)]
else if hi(b1) <= lo(b0):
; Don't intersect, b1 strictly less than b0
[hi(b1), lo(b0)]
else:
; Nonempty intersection
; Choose a point from the overlap
val intersection = (b0 & b1)
val c = center(intersection)
[c, c]
public defn rnd (b:Box) -> BoxElt :
rnd(lo(b), hi(b))
#for (BoxElt in [Vec2i Vec2f Vec2]
Box in [Box2i Box2f Box2]
Box3 in [Box3i Box3f Box3]
x0 in [0 0.0f 0.0]) :
public defn xy (b:Box) -> Box : b
public defn xyz (b:Box) -> Box3 :
Box3(xyz(lo(b)), xyz(hi(b)))
public defn empty? (b:Box) -> True|False :
val d = dims(b)
x(d) < x0 or y(d) < x0
public defn corners (b:Box) -> Tuple<BoxElt> :
val [l, h] = [lo(b), hi(b)]
[BoxElt(x(l), y(l)),
BoxElt(x(h), y(l)),
BoxElt(x(h), y(h)),
BoxElt(x(l), y(h))]
public defn intersects? (b0:Box, b1:Box) -> True|False :
not ((x(hi(b0)) < x(lo(b1)) or x(lo(b0)) > x(hi(b1))) or
(y(hi(b0)) < y(lo(b1)) or y(lo(b0)) > y(hi(b1))))
public defn closest-points (b0:Box, b1:Box) -> [BoxElt, BoxElt] :
val [cx0, cx1] = closest-points(x(b0), x(b1))
val [cy0, cy1] = closest-points(y(b0), y(b1))
[BoxElt(cx0, cy0), BoxElt(cx1, cy1)]
public defn rnd (b:Box) -> BoxElt :
rnd(lo(b), hi(b))
#for (BoxElt in [Vec3i Vec3f Vec3]
Box in [Box3i Box3f Box3]
Box2 in [Box2i Box2f Box2]
x0 in [0 0.0f 0.0]) :
public defn xy (b:Box) -> Box2 :
Box2(xy(lo(b)), xy(hi(b)))
public defn xyz (b:Box) -> Box : b
public defn empty? (b:Box) -> True|False :
val d = dims(b)
x(d) < x0 or y(d) < x0 or z(d) < x0
public defn corners (b:Box) -> Tuple<BoxElt> :
val [l, h] = [lo(b), hi(b)]
[BoxElt(x(l), y(l), z(l)),
BoxElt(x(l), y(l), z(h)),
BoxElt(x(l), y(h), z(h)),
BoxElt(x(h), y(h), z(h)),
BoxElt(x(h), y(l), z(h)),
BoxElt(x(h), y(l), z(l)),
BoxElt(x(h), y(h), z(l)),
BoxElt(x(l), y(h), z(l))
]
public defn intersects? (b0:Box, b1:Box) -> True|False :
not ((x(hi(b0)) < x(lo(b1)) or x(lo(b0)) > x(hi(b1))) or
(y(hi(b0)) < y(lo(b1)) or y(lo(b0)) > y(hi(b1))) or
(z(hi(b0)) < z(lo(b1)) or z(lo(b0)) > z(hi(b1))))
public defn times (mat:Mat4, b:Box3) -> Box3 :
reduce(union, map(Box3{ mat * _ }, corners(b)))
public defn times (mat:Mat4, b:Box2) -> Box2 :
reduce(union, map(Box2{ xy(mat * xyz(_)) }, corners(b)))
public val pos-inf-f = FLOAT-MAX
public val neg-inf-f = -1.0f * FLOAT-MAX
public val pos-inf-d = DOUBLE-MAX
public val neg-inf-d = -1.0 * DOUBLE-MAX
; public val pos-inf-f = FLOAT-POSITIVE-INFINITY
; public val neg-inf-f = FLOAT-NEGATIVE-INFINITY
; public val pos-inf-d = DOUBLE-POSITIVE-INFINITY
; public val neg-inf-d = DOUBLE-NEGATIVE-INFINITY
public val pos-inf-i = INT-MAX
public val neg-inf-i = INT-MIN