File: box_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (210 lines) | stat: -rw-r--r-- 5,231 bytes parent folder | download
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
// Copyright ©2022 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package r2

import (
	"testing"

	"golang.org/x/exp/rand"
)

func TestBoxContains(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 200; i++ {
		b := randomBox(rnd)
		for j := 0; j < 10; j++ {
			contained := b.random(rnd)
			if !b.Contains(contained) {
				t.Error("bounding box should contain Vec")
			}
		}
		uncontained := [4]Vec{
			Add(b.Max, Vec{1, 0}),
			Add(b.Max, Vec{0, 1}),
			Sub(b.Min, Vec{1, 0}),
			Sub(b.Min, Vec{0, 1}),
		}
		for _, unc := range uncontained {
			if b.Contains(unc) {
				t.Error("box should not contain vec")
			}
		}
	}
}

func TestBoxUnion(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 200; i++ {
		b1 := randomBox(rnd)
		b2 := randomBox(rnd)
		u := b1.Union(b2)
		for j := 0; j < 10; j++ {
			contained := b1.random(rnd)
			if !u.Contains(contained) {
				t.Error("union should contain b1's Vec")
			}
			contained = b2.random(rnd)
			if !u.Contains(contained) {
				t.Error("union should contain b2's Vec")
			}
		}
		uncontained := [4]Vec{
			Add(maxElem(b1.Max, b2.Max), Vec{1, 0}),
			Add(maxElem(b1.Max, b2.Max), Vec{0, 1}),
			Sub(minElem(b1.Min, b2.Min), Vec{1, 0}),
			Sub(minElem(b1.Min, b2.Min), Vec{0, 1}),
		}
		for _, unc := range uncontained {
			if !b1.Contains(unc) && !b2.Contains(unc) && u.Contains(unc) {
				t.Error("union should not contain Vec")
			}
		}
	}
}

func TestBoxCenter(t *testing.T) {
	const tol = 1e-11
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 300; i++ {
		b := randomBox(rnd)
		center := b.Center()
		size := b.Size()
		newBox := centeredBox(center, size)
		if b.Empty() {
			t.Fatal("random box result must be well formed")
		}
		if !vecApproxEqual(b.Min, newBox.Min, tol) {
			t.Errorf("min values of box not equal. got %g, expected %g", newBox.Min, b.Min)
		}
		if !vecApproxEqual(b.Max, newBox.Max, tol) {
			t.Errorf("max values of box not equal. got %g, expected %g", newBox.Max, b.Max)
		}
	}
}

func TestBoxAdd(t *testing.T) {
	const tol = 1e-14
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 12; i++ {
		b := randomBox(rnd)
		v := randomVec(rnd)
		got := b.Add(v)
		want := Box{Min: Add(b.Min, v), Max: Add(b.Max, v)}
		if !vecApproxEqual(got.Min, want.Min, tol) {
			t.Error("box min incorrect result")
		}
		if !vecApproxEqual(got.Max, want.Max, tol) {
			t.Error("box max incorrect result")
		}
	}
}

func TestBoxScale(t *testing.T) {
	const tol = 1e-11
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 300; i++ {
		b := randomBox(rnd)
		size := b.Size()

		scaler := absElem(randomVec(rnd))
		scaled := b.Scale(scaler)
		gotScaler := divElem(scaled.Size(), size)
		if !vecApproxEqual(scaler, gotScaler, tol) {
			t.Errorf("got scaled %g, expected %g", gotScaler, scaler)
		}
		center := b.Center()
		scaledCenter := scaled.Center()
		if !vecApproxEqual(center, scaledCenter, tol) {
			t.Error("scale modified center")
		}
	}
}

func TestBoxEmpty(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 300; i++ {
		v := absElem(randomVec(rnd))
		b := randomBox(rnd)
		min := b.Min
		max := b.Max
		if !(Box{Min: min, Max: min}).Empty() {
			t.Error("Box{min,min} should be empty")
		}
		if !(Box{Min: max, Max: max}).Empty() {
			t.Error("Box{max,max} should be empty")
		}
		bmm := Box{Min: min, Max: Sub(min, v)}
		if !bmm.Empty() {
			t.Error("Box{min,min-v} should be empty")
		} else if bmm.Canon().Empty() {
			t.Error("Canonical box of Box{min,min-v} is not empty")
		}
		bMM := Box{Min: Add(max, v), Max: max}
		if !bMM.Empty() {
			t.Error("Box{max+v,max} should be empty")
		} else if bmm.Canon().Empty() {
			t.Error("Canonical box of Box{max+v,max} is not empty")
		}
	}
}
func TestBoxCanon(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 300; i++ {
		b := randomBox(rnd)
		badBox := Box{Min: b.Max, Max: b.Min}
		canon := badBox.Canon()
		if canon != b {
			t.Error("swapped box canon should be equal to original box")
		}
	}
}

func TestBoxVertices(t *testing.T) {
	rnd := rand.New(rand.NewSource(1))
	for i := 0; i < 300; i++ {
		b := randomBox(rnd)
		gots := b.Vertices()
		wants := goldenVertices(b)
		if len(gots) != len(wants) {
			t.Fatalf("bad length of vertices. expect 4, got %d", len(gots))
		}
		for j, want := range wants {
			got := gots[j]
			if !vecEqual(want, got) {
				t.Errorf("%dth vertex not equal", j)
			}
		}
	}
}

// randomBox returns a random valid bounding Box.
func randomBox(rnd *rand.Rand) Box {
	spatialScale := randomRange(0, 2000)
	boxScale := randomRange(0.01, 1000)
	return centeredBox(Scale(spatialScale, randomVec(rnd)), Scale(boxScale, absElem(randomVec(rnd))))
}

// Random returns a random point within the Box.
// used to facilitate testing
func (b Box) random(rnd *rand.Rand) Vec {
	return Vec{
		X: randomRange(b.Min.X, b.Max.X),
		Y: randomRange(b.Min.Y, b.Max.Y),
	}
}

// randomRange returns a random float64 [a,b)
func randomRange(a, b float64) float64 {
	return a + (b-a)*rand.Float64()
}

func goldenVertices(a Box) []Vec {
	return []Vec{
		0: a.Min,
		1: {a.Max.X, a.Min.Y},
		2: a.Max,
		3: {a.Min.X, a.Max.Y},
	}
}