-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
triangles_data.go
117 lines (96 loc) · 2.79 KB
/
triangles_data.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
package gfx
import (
"fmt"
"image/color"
)
// TrianglesData specifies a list of Triangles vertices with three common properties:
// TrianglesPosition, TrianglesColor and TrianglesPicture.
type TrianglesData []Vertex
var (
_ TrianglesPosition = (*TrianglesData)(nil)
_ TrianglesColor = (*TrianglesData)(nil)
_ TrianglesPicture = (*TrianglesData)(nil)
)
// MakeTrianglesData creates Vertexes of length len initialized with default property values.
//
// Prefer this function to make(Vertexes, len), because make zeros them, while this function
// does the correct intialization.
func MakeTrianglesData(len int) *TrianglesData {
td := &TrianglesData{}
td.SetLen(len)
return td
}
// Len returns the number of vertices in Vertexes.
func (td *TrianglesData) Len() int {
return len(*td)
}
// SetLen resizes Vertexes to len, while keeping the original content.
//
// If len is greater than Vertexes's current length, the new data is filled with default
// values ((0, 0), white, (0, 0), 0).
func (td *TrianglesData) SetLen(length int) {
switch current := td.Len(); {
case length > current:
for i := 0; i < length-current; i++ {
*td = append(*td, Vertex{Color: ColorWhite})
}
case length < current:
*td = (*td)[:length]
}
}
// Slice returns a sub-Triangles of this TrianglesData.
func (td *TrianglesData) Slice(i, j int) Triangles {
s := (*td)[i:j]
return &s
}
func (td *TrianglesData) updateData(t Triangles) {
// fast path optimization
if t, ok := t.(*TrianglesData); ok {
copy(*td, *t)
return
}
// slow path manual copy
if t, ok := t.(TrianglesPosition); ok {
for i := range *td {
(*td)[i].Position = t.Position(i)
}
}
if t, ok := t.(TrianglesColor); ok {
for i := range *td {
(*td)[i].Color = t.Color(i)
}
}
if t, ok := t.(TrianglesPicture); ok {
for i := range *td {
(*td)[i].Picture, (*td)[i].Intensity = t.Picture(i)
}
}
}
// Update copies vertex properties from the supplied Triangles into this Vertexes.
//
// TrianglesPosition, TrianglesColor and TrianglesTexture are supported.
func (td *TrianglesData) Update(t Triangles) {
if td.Len() != t.Len() {
panic(fmt.Errorf("(%T).Update: invalid triangles length", td))
}
td.updateData(t)
}
// Copy returns an exact independent copy of this Vertexes.
func (td *TrianglesData) Copy() Triangles {
copyTd := TrianglesData{}
copyTd.SetLen(td.Len())
copyTd.Update(td)
return ©Td
}
// Position returns the position property of i-th vertex.
func (td *TrianglesData) Position(i int) Vec {
return (*td)[i].Position
}
// Color returns the color property of i-th vertex.
func (td *TrianglesData) Color(i int) color.NRGBA {
return (*td)[i].Color
}
// Picture returns the picture property of i-th vertex.
func (td *TrianglesData) Picture(i int) (pic Vec, intensity float64) {
return (*td)[i].Picture, (*td)[i].Intensity
}