/
group.go
229 lines (183 loc) · 5.59 KB
/
group.go
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
package tracer
import (
"math"
"sort"
"github.com/google/go-cmp/cmp"
"github.com/DanTulovsky/tracer/constants"
)
// Group is a collection of other groups/objects
type Group struct {
members []Shaper
Shape
}
// NewGroup returns a new, empty group
func NewGroup() *Group {
g := &Group{
members: []Shaper{},
Shape: Shape{
transform: IM(),
transformInverse: IM().Inverse(),
material: NewDefaultMaterial(),
shape: "group",
},
}
g.calculateBounds()
return g
}
// Equal returns true if the groups are equal
func (g *Group) Equal(g2 *Group) bool {
return g.Shape.Equal(&g2.Shape) &&
cmp.Equal(g.members, g2.members)
}
// AddMember adds a new member to this group
func (g *Group) AddMember(m Shaper) {
g.members = append(g.members, m)
m.SetParent(g)
g.calculateBounds()
}
// AddMembers adds a new member to this group
func (g *Group) AddMembers(m ...Shaper) {
for _, mem := range m {
g.members = append(g.members, mem)
mem.SetParent(g)
}
g.calculateBounds()
}
// Members returns all the direct members of this group
func (g *Group) Members() []Shaper {
return g.members
}
// HasMembers returns true if this is a group that has members
func (g *Group) HasMembers() bool {
return len(g.members) > 0
}
// Includes implements includes logic
func (g *Group) Includes(s Shaper) bool {
for _, m := range g.members {
if m.Includes(s) {
return true
}
}
return false
}
// SetWorldConfig attachs the world config to this object
func (g *Group) SetWorldConfig(wc *WorldConfig) {
for _, m := range g.members {
m.SetWorldConfig(wc)
}
g.wc = wc
}
// checkAxis is a helper function for check for intersection of the group's bounding box and ray
func (g *Group) checkAxis(o, d, min, max float64) (float64, float64) {
var tmin, tmax float64
tminNumerator := min - o
tmaxNumerator := max - o
if math.Abs(d) >= constants.Epsilon {
tmin = tminNumerator / d
tmax = tmaxNumerator / d
} else {
tmin = tminNumerator * math.MaxFloat64
tmax = tmaxNumerator * math.MaxFloat64
}
if tmin > tmax {
tmin, tmax = tmax, tmin
}
return tmin, tmax
}
// IntersectWithBoundingBox returns true if the ray intersects with the bounding box
// min and max define the bounding box
func (g *Group) IntersectWithBoundingBox(r Ray, b Bound) bool {
var tmin, tmax float64
xtmin, xtmax := g.checkAxis(r.Origin.X(), r.Dir.X(), b.Min.X(), b.Max.X())
ytmin, ytmax := g.checkAxis(r.Origin.Y(), r.Dir.Y(), b.Min.Y(), b.Max.Y())
ztmin, ztmax := g.checkAxis(r.Origin.Z(), r.Dir.Z(), b.Min.Z(), b.Max.Z())
tmin = math.Max(math.Max(xtmin, ytmin), ztmin)
tmax = math.Min(math.Min(xtmax, ytmax), ztmax)
// missed the bounding box
if tmin > tmax {
return false
}
return true
}
// IntersectWith returns the 't' values of Ray r intersecting with the group
func (g *Group) IntersectWith(r Ray, t Intersections) Intersections {
// transform the ray by the inverse of the group transfrom matrix
// instead of changing the group, we change the ray coming from the camera
// by the inverse, which achieves the same thing
r = r.Transform(g.transformInverse)
if !g.IntersectWithBoundingBox(r, g.Bounds()) {
// bail out early, ray does not intersect group bounding box
return t
}
xs := NewIntersections()
for _, m := range g.Members() {
mxs := m.IntersectWith(r, xs)
t = append(t, mxs...)
xs = xs[:0]
}
return t
}
// NormalAt returns the normal vector at the given point on the surface of the group
func (g *Group) NormalAt(p Point, xs *Intersection) Vector {
panic("called NormalAt on a group")
}
func (g *Group) boundBoxFromBoundingBoxes(boxes []Bound) Bound {
if len(boxes) <= 0 {
return Bound{
Min: Origin(),
Max: Origin(),
}
}
var x []float64
var y []float64
var z []float64
for _, b := range boxes {
x = append(x, b.Min.X())
x = append(x, b.Max.X())
y = append(y, b.Min.Y())
y = append(y, b.Max.Y())
z = append(z, b.Min.Z())
z = append(z, b.Max.Z())
}
sort.Float64s(x)
sort.Float64s(y)
sort.Float64s(z)
return NewBound(
NewPoint(x[0], y[0], z[0]),
NewPoint(x[len(x)-1], y[len(y)-1], z[len(z)-1]))
}
// PrecomputeValues precomputes some values for render speedup
func (g *Group) PrecomputeValues() {
// calculate group bounding box
// g.calculateBounds()
}
// calculateBounds sets the g.bound variable
func (g *Group) calculateBounds() {
// combine bounding boxes for all sub-objects into one
// convert all member bounding boxes into group space
var all []Bound
for _, m := range g.members {
mb := m.Bounds()
// transform all 8 points to World space by multiplying by the m's transformation matrix
p1 := NewPoint(mb.Min.X(), mb.Min.Y(), mb.Min.Z()).TimesMatrix(m.Transform())
p2 := NewPoint(mb.Max.X(), mb.Min.Y(), mb.Min.Z()).TimesMatrix(m.Transform())
p3 := NewPoint(mb.Min.X(), mb.Max.Y(), mb.Min.Z()).TimesMatrix(m.Transform())
p4 := NewPoint(mb.Max.X(), mb.Max.Y(), mb.Min.Z()).TimesMatrix(m.Transform())
p5 := NewPoint(mb.Min.X(), mb.Min.Y(), mb.Max.Z()).TimesMatrix(m.Transform())
p6 := NewPoint(mb.Max.X(), mb.Min.Y(), mb.Max.Z()).TimesMatrix(m.Transform())
p7 := NewPoint(mb.Min.X(), mb.Max.Y(), mb.Max.Z()).TimesMatrix(m.Transform())
p8 := NewPoint(mb.Max.X(), mb.Max.Y(), mb.Max.Z()).TimesMatrix(m.Transform())
// now find the min and max of all the point sto get the new bounding box
all = append(all, boundingBoxFromPoints(p1, p2, p3, p4, p5, p6, p7, p8))
}
// not combine all bounding boxes into one
g.bound = g.boundBoxFromBoundingBoxes(all)
}
// NumShapes returns the number of shapes contained in this object
func (g *Group) NumShapes() int {
var num int
for _, m := range g.members {
num = num + m.NumShapes()
}
return num
}