forked from go-gl-legacy/glu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesselator_test.go
278 lines (216 loc) · 6.65 KB
/
tesselator_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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2012 The go-gl 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 glu
import (
"github.com/go-gl/gl"
"testing"
)
type PolygonData struct {
BeginCount int
VertexCount int
EndCount int
ErrorCount int
EdgeFlagCount int
CombineCount int
Vertices []VertexData
}
type VertexData struct {
Location [3]float64
VertexHits int
CombineHits int
}
// Test shape is a square with a square hole inside.
var OuterContour [4][3]float64 = [4][3]float64{[3]float64{-2, 2, 0},
[3]float64{-2, -2, 0},
[3]float64{2, -2, 0},
[3]float64{2, 2, 0}}
var InnerContour [4][3]float64 = [4][3]float64{[3]float64{-1, 1, 0},
[3]float64{1, 1, 0},
[3]float64{1, -1, 0},
[3]float64{-1, -1, 0}}
// Pentagram with crossing edges. Invokes the combine callback.
var StarContour [5][3]float64 = [5][3]float64{[3]float64{0, 1, 0},
[3]float64{-1, -1, 0},
[3]float64{1, 0, 0},
[3]float64{-1, 0, 0},
[3]float64{1, -1, 0}}
func TestTesselatorData(t *testing.T) {
poly := new(PolygonData)
for _, v := range OuterContour {
poly.Vertices = append(poly.Vertices, VertexData{Location: v})
}
for _, v := range InnerContour {
poly.Vertices = append(poly.Vertices, VertexData{Location: v})
}
tess := NewTess()
tess.SetBeginCallback(tessBeginDataHandler)
tess.SetVertexCallback(tessVertexDataHandler)
tess.SetEndCallback(tessEndDataHandler)
tess.SetErrorCallback(tessErrorDataHandler)
tess.SetEdgeFlagCallback(tessEdgeFlagDataHandler)
tess.SetCombineCallback(tessCombineDataHandler)
tess.Normal(0, 0, 1)
tess.BeginPolygon(poly)
tess.BeginContour()
for v := 0; v < 4; v += 1 {
tess.Vertex(poly.Vertices[v].Location, &poly.Vertices[v])
}
tess.EndContour()
tess.BeginContour()
for v := 4; v < 8; v += 1 {
tess.Vertex(poly.Vertices[v].Location, &poly.Vertices[v])
}
tess.EndContour()
tess.EndPolygon()
expectedTriangles := 8
// There are a total of 24 edges, 8 of which are not edges. This means
// the EdgeFlag must be toggled to true 8 times.
expectedEdges := 8
checkPoly(t, poly, 1, expectedTriangles*3, 1, 0, expectedEdges, 0)
tess.Delete()
}
func TestTesselatorNil(t *testing.T) {
poly := new(PolygonData)
for _, v := range OuterContour {
poly.Vertices = append(poly.Vertices, VertexData{Location: v})
}
for _, v := range InnerContour {
poly.Vertices = append(poly.Vertices, VertexData{Location: v})
}
tess := NewTess()
tess.SetBeginCallback(tessBeginNilHandler)
tess.SetVertexCallback(tessVertexNilHandler)
tess.SetEndCallback(tessEndNilHandler)
tess.SetErrorCallback(tessErrorNilHandler)
tess.SetEdgeFlagCallback(tessEdgeFlagNilHandler)
tess.SetCombineCallback(tessCombineNilHandler)
tess.Normal(0, 0, 1)
tess.BeginPolygon(nil)
tess.BeginContour()
for v := 0; v < 4; v += 1 {
tess.Vertex(poly.Vertices[v].Location, nil)
}
tess.EndContour()
tess.BeginContour()
for v := 4; v < 8; v += 1 {
tess.Vertex(poly.Vertices[v].Location, nil)
}
tess.EndContour()
tess.EndPolygon()
tess.Delete()
}
func TestTesselatorStar(t *testing.T) {
poly := new(PolygonData)
for _, v := range StarContour {
poly.Vertices = append(poly.Vertices, VertexData{Location: v})
}
tess := NewTess()
tess.SetBeginCallback(tessBeginDataHandler)
tess.SetVertexCallback(tessVertexDataHandler)
tess.SetEndCallback(tessEndDataHandler)
tess.SetErrorCallback(tessErrorDataHandler)
tess.SetEdgeFlagCallback(tessEdgeFlagDataHandler)
tess.SetCombineCallback(tessCombineDataHandler)
tess.Normal(0, 0, 1)
tess.BeginPolygon(poly)
tess.BeginContour()
for v := range poly.Vertices {
tess.Vertex(poly.Vertices[v].Location, &poly.Vertices[v])
}
tess.EndContour()
tess.EndPolygon()
expectedTriangles := 5
// All edges lie on the edge of the polygon, so this is only set once.
expectedEdges := 1
expectedCombines := 5
checkPoly(t, poly, 1, expectedTriangles*3, 1, 0, expectedEdges, expectedCombines)
tess.Delete()
}
func tessBeginDataHandler(tessType gl.GLenum, polygonData interface{}) {
polygonDataPtr := polygonData.(*PolygonData)
polygonDataPtr.BeginCount += 1
}
func tessVertexDataHandler(vertexData interface{}, polygonData interface{}) {
polygonDataPtr := polygonData.(*PolygonData)
polygonDataPtr.VertexCount += 1
vertexDataPtr := vertexData.(*VertexData)
vertexDataPtr.VertexHits += 1
}
func tessEndDataHandler(polygonData interface{}) {
polygonDataPtr := polygonData.(*PolygonData)
polygonDataPtr.EndCount += 1
}
func tessErrorDataHandler(errno gl.GLenum, polygonData interface{}) {
polygonDataPtr := polygonData.(*PolygonData)
polygonDataPtr.ErrorCount += 1
}
func tessEdgeFlagDataHandler(flag bool, polygonData interface{}) {
polygonDataPtr := polygonData.(*PolygonData)
if flag {
polygonDataPtr.EdgeFlagCount += 1
}
}
func tessCombineDataHandler(coords [3]float64,
vertexData [4]interface{},
weight [4]float32,
polygonData interface{}) (outData interface{}) {
polygonDataPtr := polygonData.(*PolygonData)
polygonDataPtr.CombineCount += 1
for _, v := range vertexData {
vertexDataPtr := v.(*VertexData)
vertexDataPtr.CombineHits += 1
}
newVertex := VertexData{Location: coords}
polygonDataPtr.Vertices = append(polygonDataPtr.Vertices, newVertex)
return &polygonDataPtr.Vertices[len(polygonDataPtr.Vertices)-1]
}
func tessBeginNilHandler(tessType gl.GLenum, polygonData interface{}) {
}
func tessVertexNilHandler(vertexData interface{}, polygonData interface{}) {
}
func tessEndNilHandler(polygonData interface{}) {
}
func tessErrorNilHandler(errno gl.GLenum, polygonData interface{}) {
}
func tessEdgeFlagNilHandler(flag bool, polygonData interface{}) {
}
func tessCombineNilHandler(coords [3]float64,
vertexData [4]interface{},
weight [4]float32,
polygonData interface{}) (outData interface{}) {
return nil
}
func checkPoly(t *testing.T, poly *PolygonData, expectedBegins, expectedVertices,
expectedEnds, expectedErrors, expectedEdges, expectedCombines int) {
if poly.BeginCount != expectedBegins {
t.Errorf("Expected BeginCount == %v, got %v\n",
expectedBegins,
poly.BeginCount)
}
if poly.VertexCount != expectedVertices {
t.Errorf("Expected VertexCount == %v, got %v\n",
expectedVertices,
poly.VertexCount)
}
if poly.EndCount != expectedEnds {
t.Errorf("Expected EndCount == %v, got %v\n",
expectedEnds,
poly.EndCount)
}
if poly.ErrorCount != expectedErrors {
t.Errorf("Expected ErrorCount == %v, got %v\n",
expectedErrors,
poly.ErrorCount)
}
if poly.EdgeFlagCount != expectedEdges {
t.Errorf("Expected EdgeFlagCount == %v, got %v\n",
expectedEdges,
poly.EdgeFlagCount)
}
if poly.CombineCount != expectedCombines {
t.Errorf("Expected CombineCount == %v, got %v\n",
expectedCombines,
poly.CombineCount)
}
}