forked from hschendel/stl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_test.go
97 lines (90 loc) · 1.86 KB
/
test_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
package stl
// This file contains code used by multiple test cases, and tests
// for the test code.
import (
"bytes"
"io/ioutil"
"testing"
)
func makeTestSolid() *Solid {
return &Solid{
Name: "Simple",
IsAscii: true,
Triangles: []Triangle{
{
Normal: Vec3{0, 0, -1},
Vertices: [3]Vec3{
{0, 0, 0},
{0, 1, 0},
{1, 0, 0},
},
},
{
Normal: Vec3{0, -1, 0},
Vertices: [3]Vec3{
{0, 0, 0},
{1, 0, 0},
{0, 0, 1},
},
},
{
Normal: Vec3{0.57735, 0.57735, 0.57735},
Vertices: [3]Vec3{
{0, 0, 1},
{1, 0, 0},
{0, 1, 0},
},
},
{
Normal: Vec3{-1, 0, 0},
Vertices: [3]Vec3{
{0, 0, 0},
{0, 0, 1},
{0, 1, 0},
},
},
},
}
}
func TestSolidSameOrderEqual(t *testing.T) {
testSolid := makeTestSolid()
if !testSolid.sameOrderAlmostEqual(testSolid) {
t.Fatal("self comparison failed")
}
}
func cmpFiles(filename1, filename2 string) (eq bool, err error) {
data1, err1 := ioutil.ReadFile(filename1)
if err1 != nil {
err = err1
return
}
data2, err2 := ioutil.ReadFile(filename2)
if err2 != nil {
err = err2
return
}
eq = bytes.Equal(data1, data2)
return
}
// true if s and o are identical up to the 5th decimal
func (s *Solid) sameOrderAlmostEqual(o *Solid) bool {
if !(bytes.Equal(s.BinaryHeader, o.BinaryHeader) &&
s.Name == o.Name &&
len(s.Triangles) == len(o.Triangles) &&
s.IsAscii == o.IsAscii) {
return false
}
for i, t := range s.Triangles {
if !t.sameOrderAlmostEqual(&o.Triangles[i], 0.000001) {
return false
}
}
return true
}
func (t *Triangle) sameOrderAlmostEqual(o *Triangle, tol float32) bool {
return t.Normal.almostEqual(o.Normal, tol) &&
t.Vertices[0].almostEqual(o.Vertices[0], tol) &&
t.Vertices[1].almostEqual(o.Vertices[1], tol) &&
t.Vertices[2].almostEqual(o.Vertices[2], tol) &&
t.Attributes == o.Attributes
}