|
| 1 | +package MeshTypes_Test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/Patch2PDF/GDTF-Mesh-Reader/pkg/MeshTypes" |
| 9 | +) |
| 10 | + |
| 11 | +func RandomTriangle() *MeshTypes.Triangle { |
| 12 | + return &MeshTypes.Triangle{ |
| 13 | + V0: RandomVertex(), |
| 14 | + V1: RandomVertex(), |
| 15 | + V2: RandomVertex(), |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func RandomVertex() *MeshTypes.Vertex { |
| 20 | + normal := RandomVector() |
| 21 | + return &MeshTypes.Vertex{ |
| 22 | + Position: RandomVector(), |
| 23 | + Normal: &normal, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func RandomVector() MeshTypes.Vector { |
| 28 | + return MeshTypes.Vector{ |
| 29 | + X: rand.Float64(), |
| 30 | + Y: rand.Float64(), |
| 31 | + Z: rand.Float64(), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestAddTriangle(t *testing.T) { |
| 36 | + a := MeshTypes.Mesh{ |
| 37 | + Triangles: []*MeshTypes.Triangle{RandomTriangle()}, |
| 38 | + } |
| 39 | + b := RandomTriangle() |
| 40 | + copy := a.Copy() |
| 41 | + copy.AddTriangle(b) |
| 42 | + if !reflect.DeepEqual(copy, MeshTypes.Mesh{Triangles: []*MeshTypes.Triangle{a.Triangles[0], b}}) { |
| 43 | + t.Errorf("Mesh AddTriangle() Output does not match expected") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestMeshCopy(t *testing.T) { |
| 48 | + a := MeshTypes.Mesh{ |
| 49 | + Triangles: []*MeshTypes.Triangle{RandomTriangle()}, |
| 50 | + } |
| 51 | + copy := a.Copy() |
| 52 | + if !(reflect.DeepEqual(a, copy) && &a != ©) { |
| 53 | + t.Errorf("Mesh Copy() Output does not match expected") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestAddMesh(t *testing.T) { |
| 58 | + a := MeshTypes.Mesh{ |
| 59 | + Triangles: []*MeshTypes.Triangle{RandomTriangle()}, |
| 60 | + } |
| 61 | + b := MeshTypes.Mesh{ |
| 62 | + Triangles: []*MeshTypes.Triangle{RandomTriangle()}, |
| 63 | + } |
| 64 | + copy := a.Copy() |
| 65 | + result := copy.Add(&b) |
| 66 | + if !reflect.DeepEqual(*result, MeshTypes.Mesh{Triangles: []*MeshTypes.Triangle{a.Triangles[0], b.Triangles[0]}}) { |
| 67 | + t.Errorf("Mesh Add() Output does not match expected") |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestRotateAndTranslate(t *testing.T) { |
| 72 | + a := MeshTypes.Mesh{ |
| 73 | + Triangles: []*MeshTypes.Triangle{RandomTriangle()}, |
| 74 | + } |
| 75 | + translationMatrix := MeshTypes.Matrix{ |
| 76 | + X00: 1, X01: -1, X02: 5, X03: -20, |
| 77 | + X10: 1, X11: 2, X12: -3, X13: 10, |
| 78 | + X20: 1, X21: 5, X22: 3, X23: -5, |
| 79 | + X30: 1, X31: 6, X32: -6, X33: 4, |
| 80 | + } |
| 81 | + result := a.Copy() |
| 82 | + result.RotateAndTranslate(translationMatrix) |
| 83 | + want := a.Copy() |
| 84 | + for _, triangle := range want.Triangles { |
| 85 | + triangle.V0.Position = translationMatrix.MulPosition(triangle.V0.Position) // safe to use as func is tested in another place |
| 86 | + triangle.V1.Position = translationMatrix.MulPosition(triangle.V1.Position) |
| 87 | + triangle.V2.Position = translationMatrix.MulPosition(triangle.V2.Position) |
| 88 | + } |
| 89 | + if !reflect.DeepEqual(result, want) { |
| 90 | + t.Errorf("Mesh RotateAndTranslate() Output does not match expected") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestScaleToDimensions(t *testing.T) { |
| 95 | + a := MeshTypes.Mesh{ |
| 96 | + Triangles: []*MeshTypes.Triangle{ |
| 97 | + { |
| 98 | + V0: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: -1, Y: 1, Z: -1}}, |
| 99 | + V1: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 1, Y: -3, Z: 1}}, |
| 100 | + V2: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 1, Y: 1, Z: 0}}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + desiredSize := MeshTypes.Vector{ |
| 105 | + X: 4, Y: 4, Z: 8, |
| 106 | + } |
| 107 | + result := a.Copy() |
| 108 | + result.ScaleToDimensions(&desiredSize) |
| 109 | + want := MeshTypes.Mesh{ |
| 110 | + Triangles: []*MeshTypes.Triangle{ |
| 111 | + { |
| 112 | + V0: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: -2, Y: 1, Z: -4}}, |
| 113 | + V1: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 2, Y: -3, Z: 4}}, |
| 114 | + V2: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 2, Y: 1, Z: 0}}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | + if !reflect.DeepEqual(result, want) { |
| 119 | + t.Errorf("Mesh ScaleToDimensions() Output does not match expected") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestScaleToDimensionsEmptyMesh(t *testing.T) { |
| 124 | + a := MeshTypes.Mesh{ |
| 125 | + Triangles: []*MeshTypes.Triangle{}, |
| 126 | + } |
| 127 | + desiredSize := MeshTypes.Vector{ |
| 128 | + X: 4, Y: 4, Z: 8, |
| 129 | + } |
| 130 | + err := a.ScaleToDimensions(&desiredSize) |
| 131 | + if err == nil { |
| 132 | + t.Errorf("Mesh ScaleToDimensions() did not return error on faulty mesh") |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestScaleToDimensionsNullTriangle(t *testing.T) { |
| 137 | + a := MeshTypes.Mesh{ |
| 138 | + Triangles: []*MeshTypes.Triangle{nil}, |
| 139 | + } |
| 140 | + desiredSize := MeshTypes.Vector{ |
| 141 | + X: 4, Y: 4, Z: 8, |
| 142 | + } |
| 143 | + err := a.ScaleToDimensions(&desiredSize) |
| 144 | + if err == nil { |
| 145 | + t.Errorf("Mesh ScaleToDimensions() did not return error on faulty mesh") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestScaleToDimensionsNullVertex(t *testing.T) { |
| 150 | + a := MeshTypes.Mesh{ |
| 151 | + Triangles: []*MeshTypes.Triangle{ |
| 152 | + { |
| 153 | + V0: nil, |
| 154 | + V1: nil, |
| 155 | + V2: nil, |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | + desiredSize := MeshTypes.Vector{ |
| 160 | + X: 4, Y: 4, Z: 8, |
| 161 | + } |
| 162 | + err := a.ScaleToDimensions(&desiredSize) |
| 163 | + if err == nil { |
| 164 | + t.Errorf("Mesh ScaleToDimensions() did not return error on faulty mesh") |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func TestScaleToDimensions0Size(t *testing.T) { |
| 169 | + a := MeshTypes.Mesh{ |
| 170 | + Triangles: []*MeshTypes.Triangle{ |
| 171 | + { |
| 172 | + V0: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 0, Y: 0, Z: 0}}, |
| 173 | + V1: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 0, Y: 0, Z: 0}}, |
| 174 | + V2: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 0, Y: 0, Z: 0}}, |
| 175 | + }, |
| 176 | + }, |
| 177 | + } |
| 178 | + desiredSize := MeshTypes.Vector{ |
| 179 | + X: 4, Y: 4, Z: 8, |
| 180 | + } |
| 181 | + err := a.ScaleToDimensions(&desiredSize) |
| 182 | + if err == nil { |
| 183 | + t.Errorf("Mesh ScaleToDimensions() did not return error on faulty mesh") |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestScaleToDimensionsSameVertex(t *testing.T) { |
| 188 | + triangle := MeshTypes.Triangle{ |
| 189 | + V0: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 0, Y: 0, Z: 0}}, |
| 190 | + V1: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 0, Y: 0, Z: 0}}, |
| 191 | + V2: &MeshTypes.Vertex{Position: MeshTypes.Vector{X: 0, Y: 0, Z: 0}}, |
| 192 | + } |
| 193 | + a := MeshTypes.Mesh{ |
| 194 | + Triangles: []*MeshTypes.Triangle{ |
| 195 | + &triangle, |
| 196 | + { |
| 197 | + V0: triangle.V0, |
| 198 | + V1: triangle.V1, |
| 199 | + V2: triangle.V2, |
| 200 | + }, |
| 201 | + }, |
| 202 | + } |
| 203 | + desiredSize := MeshTypes.Vector{ |
| 204 | + X: 4, Y: 4, Z: 8, |
| 205 | + } |
| 206 | + err := a.ScaleToDimensions(&desiredSize) |
| 207 | + if err == nil { |
| 208 | + t.Errorf("Mesh ScaleToDimensions() did not return error on faulty mesh") |
| 209 | + } |
| 210 | +} |
0 commit comments