| 1 |
package gfx |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"image/color" |
| 6 |
) |
| 7 |
|
| 8 |
// TrianglesData specifies a list of Triangles vertices with three common properties: |
| 9 |
// TrianglesPosition, TrianglesColor and TrianglesPicture. |
| 10 |
type TrianglesData []Vertex |
| 11 |
|
| 12 |
var ( |
| 13 |
_ TrianglesPosition = (*TrianglesData)(nil) |
| 14 |
_ TrianglesColor = (*TrianglesData)(nil) |
| 15 |
_ TrianglesPicture = (*TrianglesData)(nil) |
| 16 |
) |
| 17 |
|
| 18 |
// MakeTrianglesData creates Vertexes of length len initialized with default property values. |
| 19 |
// |
| 20 |
// Prefer this function to make(Vertexes, len), because make zeros them, while this function |
| 21 |
// does the correct intialization. |
| 22 |
func MakeTrianglesData(len int) *TrianglesData { |
| 23 |
td := &TrianglesData{} |
| 24 |
td.SetLen(len) |
| 25 |
|
| 26 |
return td |
| 27 |
} |
| 28 |
|
| 29 |
// Len returns the number of vertices in Vertexes. |
| 30 |
func (td *TrianglesData) Len() int { |
| 31 |
return len(*td) |
| 32 |
} |
| 33 |
|
| 34 |
// SetLen resizes Vertexes to len, while keeping the original content. |
| 35 |
// |
| 36 |
// If len is greater than Vertexes's current length, the new data is filled with default |
| 37 |
// values ((0, 0), white, (0, 0), 0). |
| 38 |
func (td *TrianglesData) SetLen(length int) { |
| 39 |
switch current := td.Len(); { |
| 40 |
case length > current: |
| 41 |
for i := 0; i < length-current; i++ { |
| 42 |
*td = append(*td, Vertex{Color: ColorWhite}) |
| 43 |
} |
| 44 |
case length < current: |
| 45 |
*td = (*td)[:length] |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// Slice returns a sub-Triangles of this TrianglesData. |
| 50 |
func (td *TrianglesData) Slice(i, j int) Triangles { |
| 51 |
s := (*td)[i:j] |
| 52 |
|
| 53 |
return &s |
| 54 |
} |
| 55 |
|
| 56 |
func (td *TrianglesData) updateData(t Triangles) { |
| 57 |
// fast path optimization |
| 58 |
if t, ok := t.(*TrianglesData); ok { |
| 59 |
copy(*td, *t) |
| 60 |
|
| 61 |
return |
| 62 |
} |
| 63 |
|
| 64 |
// slow path manual copy |
| 65 |
if t, ok := t.(TrianglesPosition); ok { |
| 66 |
for i := range *td { |
| 67 |
(*td)[i].Position = t.Position(i) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if t, ok := t.(TrianglesColor); ok { |
| 72 |
for i := range *td { |
| 73 |
(*td)[i].Color = t.Color(i) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if t, ok := t.(TrianglesPicture); ok { |
| 78 |
for i := range *td { |
| 79 |
(*td)[i].Picture, (*td)[i].Intensity = t.Picture(i) |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// Update copies vertex properties from the supplied Triangles into this Vertexes. |
| 85 |
// |
| 86 |
// TrianglesPosition, TrianglesColor and TrianglesTexture are supported. |
| 87 |
func (td *TrianglesData) Update(t Triangles) { |
| 88 |
if td.Len() != t.Len() { |
| 89 |
panic(fmt.Errorf("(%T).Update: invalid triangles length", td)) |
| 90 |
} |
| 91 |
|
| 92 |
td.updateData(t) |
| 93 |
} |
| 94 |
|
| 95 |
// Copy returns an exact independent copy of this Vertexes. |
| 96 |
func (td *TrianglesData) Copy() Triangles { |
| 97 |
copyTd := TrianglesData{} |
| 98 |
copyTd.SetLen(td.Len()) |
| 99 |
copyTd.Update(td) |
| 100 |
|
| 101 |
return ©Td |
| 102 |
} |
| 103 |
|
| 104 |
// Position returns the position property of i-th vertex. |
| 105 |
func (td *TrianglesData) Position(i int) Vec { |
| 106 |
return (*td)[i].Position |
| 107 |
} |
| 108 |
|
| 109 |
// Color returns the color property of i-th vertex. |
| 110 |
func (td *TrianglesData) Color(i int) color.NRGBA { |
| 111 |
return (*td)[i].Color |
| 112 |
} |
| 113 |
|
| 114 |
// Picture returns the picture property of i-th vertex. |
| 115 |
func (td *TrianglesData) Picture(i int) (pic Vec, intensity float64) { |
| 116 |
return (*td)[i].Picture, (*td)[i].Intensity |
| 117 |
} |