-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathoctree_test.go
155 lines (130 loc) · 4.35 KB
/
octree_test.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
package trees_test
import (
"fmt"
"math/rand"
"testing"
"github.com/EliCDavis/polyform/math/geometry"
"github.com/EliCDavis/polyform/modeling"
"github.com/EliCDavis/polyform/modeling/primitives"
"github.com/EliCDavis/polyform/modeling/repeat"
"github.com/EliCDavis/vector/vector3"
"github.com/stretchr/testify/assert"
)
func TestOctreeSingleTri(t *testing.T) {
// ARRANGE ================================================================
mesh := modeling.NewTriangleMesh([]int{0, 1, 2}).
SetFloat3Attribute(modeling.PositionAttribute, []vector3.Float64{
vector3.New(0., 0., 0.),
vector3.New(0., 1., 0.),
vector3.New(1., 1., 0.),
})
tree := mesh.OctTree()
// ACT ====================================================================
_, p := tree.ClosestPoint(vector3.Zero[float64]())
// ASSERT =================================================================
assert.Equal(t, p, vector3.Zero[float64]())
}
func TestOctreeTwoTris(t *testing.T) {
// ARRANGE ================================================================
mesh := modeling.NewTriangleMesh([]int{0, 1, 2, 0, 2, 3}).
SetFloat3Attribute(modeling.PositionAttribute, []vector3.Float64{
vector3.New(0., 0., 0.),
vector3.New(0., 1., 0.),
vector3.New(1., 1., 0.),
vector3.New(1., 0., 0.),
})
tree := mesh.OctTree()
// ACT ====================================================================
_, p := tree.ClosestPoint(vector3.Zero[float64]())
// ASSERT =================================================================
assert.Equal(t, p, vector3.Zero[float64]())
}
func TestOctreeSphere(t *testing.T) {
// ARRANGE ================================================================
mesh := primitives.UVSphere(1, 100, 100)
tree := mesh.OctTree()
testPointCount := 1000
testPoints := make([]vector3.Float64, testPointCount)
for i := 0; i < testPointCount; i++ {
testPoints[i] = vector3.New(
-1+(rand.Float64()*2),
-1+(rand.Float64()*2),
-1+(rand.Float64()*2),
).Normalized()
}
// ACT / ASSERT ===========================================================
for i := 0; i < testPointCount; i++ {
_, p := tree.ClosestPoint(testPoints[i])
assert.InDelta(t, testPoints[i].X(), p.X(), 0.05)
assert.InDelta(t, testPoints[i].Y(), p.Y(), 0.05)
assert.InDelta(t, testPoints[i].Z(), p.Z(), 0.05)
}
}
func TestOctreeLineSphere(t *testing.T) {
// ARRANGE ================================================================
mesh := modeling.NewLineStripMesh(
map[string][]vector3.Float64{
modeling.PositionAttribute: repeat.CirclePoints(100, 1),
},
nil,
nil,
nil,
)
tree := mesh.OctTree()
testPointCount := 100
testPoints := make([]vector3.Float64, testPointCount)
for i := 0; i < testPointCount; i++ {
testPoints[i] = vector3.New(
-1+(rand.Float64()*2),
0,
-1+(rand.Float64()*2),
)
testPoints[i] = testPoints[i].Normalized()
}
// ACT / ASSERT ===========================================================
for i := 0; i < testPointCount; i++ {
_, p := tree.ClosestPoint(testPoints[i].Scale(5))
assert.InDelta(t, testPoints[i].X(), p.X(), 0.05)
assert.InDelta(t, testPoints[i].Y(), p.Y(), 0.05)
assert.InDelta(t, testPoints[i].Z(), p.Z(), 0.05)
}
}
var result vector3.Float64
func BenchmarkOctreeLineSphere(b *testing.B) {
var r vector3.Float64
mesh := modeling.NewLineStripMesh(
map[string][]vector3.Float64{
modeling.PositionAttribute: repeat.CirclePoints(10000, 1),
},
nil,
nil,
nil,
)
tree := mesh.OctTree()
for n := 0; n < b.N; n++ {
// always record the result of Fib to prevent
// the compiler eliminating the function call.
_, r = tree.ClosestPoint(vector3.New(1., 0., 1.))
}
// always store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
result = r
}
var eleRes []int
func BenchmarkOctreeRay(b *testing.B) {
var r []int
mesh := primitives.UVSphere(1, 2, 3)
mesh = primitives.UnitCube()
tree := mesh.OctTree()
ray := geometry.NewRay(vector3.New(-1.35, -1.5, -1.5), vector3.New(1., 1., 1.))
b.Run(fmt.Sprintf("input_size_%d", mesh.PrimitiveCount()), func(b *testing.B) {
for n := 0; n < b.N; n++ {
// always record the result of Fib to prevent
// the compiler eliminating the function call.
r = tree.ElementsIntersectingRay(ray, 0, 4)
}
})
// always store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
eleRes = r
}