-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbox.t
54 lines (38 loc) · 1.13 KB
/
bbox.t
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
local m = require("mem")
local templatize = require("templatize")
local BBox = templatize(function(VecT)
local real = VecT.RealType
local struct BBoxT { mins: VecT, maxs: VecT }
terra BBoxT:__construct(mins: VecT, maxs: VecT) : {}
self.mins = m.copy(mins)
self.maxs = m.copy(maxs)
end
terra BBoxT:__construct() : {}
self:__construct(VecT.stackAlloc([math.huge]), VecT.stackAlloc([-math.huge]))
end
terra BBoxT:expand(point: VecT)
self.mins:minInPlace(point)
self.maxs:maxInPlace(point)
end
terra BBoxT:expand(amount: real)
[VecT.foreach(`self.mins, function(x) return quote [x] = [x] - amount end end)]
[VecT.foreach(`self.maxs, function(x) return quote [x] = [x] + amount end end)]
end
terra BBoxT:contains(point: VecT)
return point > self.mins and point < self.maxs
end
terra BBoxT:unionWith(other: &BBoxT)
self.mins:minInPlace(other.mins)
self.maxs:maxInPlace(other.maxs)
end
terra BBoxT:intersectWith(other: &BBoxT)
self.mins:maxInPlace(other.mins)
self.maxs:minInPlace(other.maxs)
end
terra BBoxT:extents()
return self.maxs - self.mins
end
m.addConstructors(BBoxT)
return BBoxT
end)
return BBox