Skip to content

Commit 9e7d255

Browse files
authored
Merge pull request #19 from Patch2PDF/ptr-to-direct-transition
BREAKING CHANGE: ptr to copy transition
2 parents ce78336 + d3befec commit 9e7d255

13 files changed

Lines changed: 174 additions & 183 deletions

File tree

pkg/MeshTypes/matrix.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func (a Matrix) Mul(b Matrix) Matrix {
3737
return m
3838
}
3939

40+
func (a Matrix) MulScalar(b float64) Matrix {
41+
return Matrix{
42+
a.X00 * b, a.X01 * b, a.X02 * b, a.X03 * b,
43+
a.X10 * b, a.X11 * b, a.X12 * b, a.X13 * b,
44+
a.X20 * b, a.X21 * b, a.X22 * b, a.X23 * b,
45+
a.X30 * b, a.X31 * b, a.X32 * b, a.X33 * b,
46+
}
47+
}
48+
4049
func (a Matrix) MulPosition(b Vector) Vector {
4150
x := a.X00*b.X + a.X01*b.Y + a.X02*b.Z + a.X03
4251
y := a.X10*b.X + a.X11*b.Y + a.X12*b.Z + a.X13

pkg/MeshTypes/mesh.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ import (
55
)
66

77
type Mesh struct {
8-
Triangles []*Triangle
8+
Triangles []Triangle
99
}
1010

11-
func (obj *Mesh) AddTriangle(triangle *Triangle) {
11+
func (obj *Mesh) AddTriangle(triangle Triangle) {
1212
obj.Triangles = append(obj.Triangles, triangle)
1313
}
1414

1515
func (obj *Mesh) Copy() Mesh {
16-
triangles := make([]*Triangle, len(obj.Triangles))
17-
for index, triangle := range obj.Triangles {
18-
temp := triangle.Copy()
19-
triangles[index] = &temp
20-
}
16+
triangles := make([]Triangle, len(obj.Triangles))
17+
copy(triangles, obj.Triangles)
2118
return Mesh{Triangles: triangles}
2219
}
2320

@@ -31,42 +28,42 @@ func (obj *Mesh) Add(mesh ...*Mesh) *Mesh {
3128
}
3229

3330
func (obj *Mesh) RotateAndTranslate(translationMatrix Matrix) {
34-
for _, triangle := range obj.Triangles {
35-
triangle.V0.Position = translationMatrix.MulPosition(triangle.V0.Position)
36-
triangle.V1.Position = translationMatrix.MulPosition(triangle.V1.Position)
37-
triangle.V2.Position = translationMatrix.MulPosition(triangle.V2.Position)
38-
if triangle.V0.Normal != nil {
39-
n0 := translationMatrix.MulDirection(*triangle.V0.Normal)
40-
triangle.V0.Normal = &n0
31+
for triangle_id := range obj.Triangles {
32+
obj.Triangles[triangle_id].V0.Position = translationMatrix.MulPosition(obj.Triangles[triangle_id].V0.Position)
33+
obj.Triangles[triangle_id].V1.Position = translationMatrix.MulPosition(obj.Triangles[triangle_id].V1.Position)
34+
obj.Triangles[triangle_id].V2.Position = translationMatrix.MulPosition(obj.Triangles[triangle_id].V2.Position)
35+
if obj.Triangles[triangle_id].V0.Normal != nil {
36+
n0 := translationMatrix.MulDirection(*obj.Triangles[triangle_id].V0.Normal)
37+
obj.Triangles[triangle_id].V0.Normal = &n0
4138
}
42-
if triangle.V1.Normal != nil {
43-
n1 := translationMatrix.MulDirection(*triangle.V1.Normal)
44-
triangle.V1.Normal = &n1
39+
if obj.Triangles[triangle_id].V1.Normal != nil {
40+
n1 := translationMatrix.MulDirection(*obj.Triangles[triangle_id].V1.Normal)
41+
obj.Triangles[triangle_id].V1.Normal = &n1
4542
}
46-
if triangle.V2.Normal != nil {
47-
n2 := translationMatrix.MulDirection(*triangle.V2.Normal)
48-
triangle.V2.Normal = &n2
43+
if obj.Triangles[triangle_id].V2.Normal != nil {
44+
n2 := translationMatrix.MulDirection(*obj.Triangles[triangle_id].V2.Normal)
45+
obj.Triangles[triangle_id].V2.Normal = &n2
4946
}
5047
}
5148
}
5249

53-
func (obj *Mesh) calculateBoundingBox() Vector {
50+
func (obj *Mesh) CalculateBoundingBox() Vector {
5451
// init with first triangle to prohibit 0 values being min or max
55-
if len(obj.Triangles) == 0 || obj.Triangles[0] == nil || obj.Triangles[0].V0 == nil {
52+
if len(obj.Triangles) == 0 {
5653
return Vector{}
5754
}
5855
min := obj.Triangles[0].V0.Position
5956
max := obj.Triangles[0].V0.Position
6057

6158
for _, triangle := range obj.Triangles {
62-
min = triangle.V0.Position.Min(&min)
63-
max = triangle.V0.Position.Max(&max)
59+
min = triangle.V0.Position.Min(min)
60+
max = triangle.V0.Position.Max(max)
6461

65-
min = triangle.V1.Position.Min(&min)
66-
max = triangle.V1.Position.Max(&max)
62+
min = triangle.V1.Position.Min(min)
63+
max = triangle.V1.Position.Max(max)
6764

68-
min = triangle.V2.Position.Min(&min)
69-
max = triangle.V2.Position.Max(&max)
65+
min = triangle.V2.Position.Min(min)
66+
max = triangle.V2.Position.Max(max)
7067
}
7168
return Vector{
7269
X: max.X - min.X,
@@ -76,26 +73,26 @@ func (obj *Mesh) calculateBoundingBox() Vector {
7673
}
7774

7875
func (obj *Mesh) Scale(scaling Vector) error {
79-
scaledVectors := make(map[*Vertex]struct{})
80-
for _, triangle := range obj.Triangles {
76+
scaledVectors := make(map[Vertex]struct{})
77+
for triangle_id, triangle := range obj.Triangles {
8178
if _, exists := scaledVectors[triangle.V0]; !exists {
82-
triangle.V0.Position = triangle.V0.Position.Mult(scaling)
83-
scaledVectors[triangle.V0] = struct{}{}
79+
obj.Triangles[triangle_id].V0.Position = triangle.V0.Position.Mult(scaling)
80+
scaledVectors[obj.Triangles[triangle_id].V0] = struct{}{}
8481
}
8582
if _, exists := scaledVectors[triangle.V1]; !exists {
86-
triangle.V1.Position = triangle.V1.Position.Mult(scaling)
87-
scaledVectors[triangle.V1] = struct{}{}
83+
obj.Triangles[triangle_id].V1.Position = triangle.V1.Position.Mult(scaling)
84+
scaledVectors[obj.Triangles[triangle_id].V1] = struct{}{}
8885
}
8986
if _, exists := scaledVectors[triangle.V2]; !exists {
90-
triangle.V2.Position = triangle.V2.Position.Mult(scaling)
91-
scaledVectors[triangle.V2] = struct{}{}
87+
obj.Triangles[triangle_id].V2.Position = triangle.V2.Position.Mult(scaling)
88+
scaledVectors[obj.Triangles[triangle_id].V2] = struct{}{}
9289
}
9390
}
9491
return nil
9592
}
9693

9794
func (obj *Mesh) ScaleToDimensions(desiredSize *Vector) error {
98-
actual := obj.calculateBoundingBox()
95+
actual := obj.CalculateBoundingBox()
9996
if actual.X == 0 && actual.Y == 0 && actual.Z == 0 {
10097
return fmt.Errorf("invalid Mesh with 0 dimension")
10198
}

pkg/MeshTypes/triangle.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package MeshTypes
22

33
type Triangle struct {
4-
V0 *Vertex
5-
V1 *Vertex
6-
V2 *Vertex
4+
V0 Vertex
5+
V1 Vertex
6+
V2 Vertex
77
}
88

99
func (t *Triangle) Normal() Vector {
@@ -13,8 +13,5 @@ func (t *Triangle) Normal() Vector {
1313
}
1414

1515
func (obj *Triangle) Copy() Triangle {
16-
V0 := obj.V0.Copy()
17-
V1 := obj.V1.Copy()
18-
V2 := obj.V2.Copy()
19-
return Triangle{V0: &V0, V1: &V1, V2: &V2}
16+
return Triangle{V0: obj.V0.Copy(), V1: obj.V1.Copy(), V2: obj.V2.Copy()}
2017
}

pkg/MeshTypes/vector.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@ func (a Vector) Normalize() Vector {
5353
return Vector{a.X * r, a.Y * r, a.Z * r}
5454
}
5555

56-
func (obj Vector) ToVertex(normal *Vector) *Vertex {
57-
return &Vertex{
56+
func (obj Vector) ToVertex(normal *Vector) Vertex {
57+
return Vertex{
5858
Position: obj,
5959
Normal: normal,
6060
}
6161
}
6262

63-
func (a Vector) Min(b *Vector) Vector {
63+
func (a Vector) Min(b Vector) Vector {
6464
return Vector{
6565
math.Min(a.X, b.X),
6666
math.Min(a.Y, b.Y),
6767
math.Min(a.Z, b.Z),
6868
}
6969
}
7070

71-
func (a Vector) Max(b *Vector) Vector {
71+
func (a Vector) Max(b Vector) Vector {
7272
return Vector{
7373
math.Max(a.X, b.X),
7474
math.Max(a.Y, b.Y),

pkg/file_handlers/3ds.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func Load3DS(fileData *[]byte, desiredSize *MeshTypes.Vector) (*MeshTypes.Mesh,
3737

3838
file := bytes.NewReader(*fileData)
3939

40-
var vertices []*MeshTypes.Vertex
41-
var faces []*MeshTypes.Triangle
42-
var triangles []*MeshTypes.Triangle
40+
var vertices []MeshTypes.Vertex
41+
var faces []MeshTypes.Triangle
42+
var triangles []MeshTypes.Triangle
4343
for {
4444
header := ChunkHeader{}
4545
if err := binary.Read(file, binary.LittleEndian, &header); err != nil {
@@ -100,7 +100,7 @@ func Load3DS(fileData *[]byte, desiredSize *MeshTypes.Vector) (*MeshTypes.Mesh,
100100
return mesh, nil
101101
}
102102

103-
func readSmoothingGroups(file *bytes.Reader, triangles []*MeshTypes.Triangle) error {
103+
func readSmoothingGroups(file *bytes.Reader, triangles []MeshTypes.Triangle) error {
104104
groups := make([]uint32, len(triangles))
105105
if err := binary.Read(file, binary.LittleEndian, &groups); err != nil {
106106
return err
@@ -162,36 +162,36 @@ func readSmoothingGroups(file *bytes.Reader, triangles []*MeshTypes.Triangle) er
162162
// return matrix, nil
163163
// }
164164

165-
func readVertexList(file *bytes.Reader) ([]*MeshTypes.Vertex, error) {
165+
func readVertexList(file *bytes.Reader) ([]MeshTypes.Vertex, error) {
166166
var count uint16
167167
if err := binary.Read(file, binary.LittleEndian, &count); err != nil {
168168
return nil, err
169169
}
170-
result := make([]*MeshTypes.Vertex, count)
170+
result := make([]MeshTypes.Vertex, count)
171171
for i := range result {
172172
var v [3]float32
173173
if err := binary.Read(file, binary.LittleEndian, &v); err != nil {
174174
return nil, err
175175
}
176-
result[i] = &MeshTypes.Vertex{
176+
result[i] = MeshTypes.Vertex{
177177
Position: MeshTypes.Vector{X: float64(v[0]), Y: float64(v[1]), Z: float64(v[2])},
178178
}
179179
}
180180
return result, nil
181181
}
182182

183-
func readFaceList(file *bytes.Reader, vertices []*MeshTypes.Vertex) ([]*MeshTypes.Triangle, error) {
183+
func readFaceList(file *bytes.Reader, vertices []MeshTypes.Vertex) ([]MeshTypes.Triangle, error) {
184184
var count uint16
185185
if err := binary.Read(file, binary.LittleEndian, &count); err != nil {
186186
return nil, err
187187
}
188-
result := make([]*MeshTypes.Triangle, count)
188+
result := make([]MeshTypes.Triangle, count)
189189
for i := range result {
190190
var v [4]uint16
191191
if err := binary.Read(file, binary.LittleEndian, &v); err != nil {
192192
return nil, err
193193
}
194-
result[i] = &MeshTypes.Triangle{
194+
result[i] = MeshTypes.Triangle{
195195
V0: vertices[v[0]], V1: vertices[v[1]], V2: vertices[v[2]],
196196
}
197197
}

pkg/file_handlers/gltf.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func LoadGLTF(file io.Reader, desiredSize *Types.Vector) (*Types.Mesh, error) {
4949
Y: posAccessor.Max[2],
5050
Z: posAccessor.Max[1],
5151
})
52-
min = min.Min(&tempMin)
53-
max = max.Max(&tempMax)
52+
min = min.Min(tempMin)
53+
max = max.Max(tempMax)
5454
}
5555
}
5656

@@ -98,7 +98,7 @@ func LoadGLTF(file io.Reader, desiredSize *Types.Vector) (*Types.Mesh, error) {
9898
v1 := positions[indices[i+1]].ToVertex(n1)
9999
v2 := positions[indices[i+2]].ToVertex(n2)
100100

101-
mesh.AddTriangle(&Types.Triangle{V0: v0, V1: v1, V2: v2})
101+
mesh.AddTriangle(Types.Triangle{V0: v0, V1: v1, V2: v2})
102102
}
103103

104104
meshes.Add(&mesh)

pkg/primitives/cube.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,72 @@ func NewCube() Types.Mesh {
88
{X: -1, Y: -1, Z: -1}, {X: -1, Y: -1, Z: 1}, {X: 1, Y: -1, Z: 1}, {X: 1, Y: -1, Z: -1},
99
}
1010
mesh := Types.Mesh{
11-
Triangles: []*Types.Triangle{
11+
Triangles: []Types.Triangle{
1212
// top
1313
{
14-
V0: &Types.Vertex{Position: v[1], Normal: nil},
15-
V1: &Types.Vertex{Position: v[2], Normal: nil},
16-
V2: &Types.Vertex{Position: v[3], Normal: nil},
14+
V0: Types.Vertex{Position: v[1], Normal: nil},
15+
V1: Types.Vertex{Position: v[2], Normal: nil},
16+
V2: Types.Vertex{Position: v[3], Normal: nil},
1717
},
1818
{
19-
V0: &Types.Vertex{Position: v[0], Normal: nil},
20-
V1: &Types.Vertex{Position: v[1], Normal: nil},
21-
V2: &Types.Vertex{Position: v[3], Normal: nil},
19+
V0: Types.Vertex{Position: v[0], Normal: nil},
20+
V1: Types.Vertex{Position: v[1], Normal: nil},
21+
V2: Types.Vertex{Position: v[3], Normal: nil},
2222
},
2323
// left
2424
{
25-
V0: &Types.Vertex{Position: v[4], Normal: nil},
26-
V1: &Types.Vertex{Position: v[1], Normal: nil},
27-
V2: &Types.Vertex{Position: v[0], Normal: nil},
25+
V0: Types.Vertex{Position: v[4], Normal: nil},
26+
V1: Types.Vertex{Position: v[1], Normal: nil},
27+
V2: Types.Vertex{Position: v[0], Normal: nil},
2828
},
2929
{
30-
V0: &Types.Vertex{Position: v[4], Normal: nil},
31-
V1: &Types.Vertex{Position: v[5], Normal: nil},
32-
V2: &Types.Vertex{Position: v[1], Normal: nil},
30+
V0: Types.Vertex{Position: v[4], Normal: nil},
31+
V1: Types.Vertex{Position: v[5], Normal: nil},
32+
V2: Types.Vertex{Position: v[1], Normal: nil},
3333
},
3434
// front
3535
{
36-
V0: &Types.Vertex{Position: v[5], Normal: nil},
37-
V1: &Types.Vertex{Position: v[2], Normal: nil},
38-
V2: &Types.Vertex{Position: v[1], Normal: nil},
36+
V0: Types.Vertex{Position: v[5], Normal: nil},
37+
V1: Types.Vertex{Position: v[2], Normal: nil},
38+
V2: Types.Vertex{Position: v[1], Normal: nil},
3939
},
4040
{
41-
V0: &Types.Vertex{Position: v[5], Normal: nil},
42-
V1: &Types.Vertex{Position: v[6], Normal: nil},
43-
V2: &Types.Vertex{Position: v[2], Normal: nil},
41+
V0: Types.Vertex{Position: v[5], Normal: nil},
42+
V1: Types.Vertex{Position: v[6], Normal: nil},
43+
V2: Types.Vertex{Position: v[2], Normal: nil},
4444
},
4545
// right
4646
{
47-
V0: &Types.Vertex{Position: v[7], Normal: nil},
48-
V1: &Types.Vertex{Position: v[3], Normal: nil},
49-
V2: &Types.Vertex{Position: v[2], Normal: nil},
47+
V0: Types.Vertex{Position: v[7], Normal: nil},
48+
V1: Types.Vertex{Position: v[3], Normal: nil},
49+
V2: Types.Vertex{Position: v[2], Normal: nil},
5050
},
5151
{
52-
V0: &Types.Vertex{Position: v[7], Normal: nil},
53-
V1: &Types.Vertex{Position: v[2], Normal: nil},
54-
V2: &Types.Vertex{Position: v[6], Normal: nil},
52+
V0: Types.Vertex{Position: v[7], Normal: nil},
53+
V1: Types.Vertex{Position: v[2], Normal: nil},
54+
V2: Types.Vertex{Position: v[6], Normal: nil},
5555
},
5656
// back
5757
{
58-
V0: &Types.Vertex{Position: v[0], Normal: nil},
59-
V1: &Types.Vertex{Position: v[3], Normal: nil},
60-
V2: &Types.Vertex{Position: v[4], Normal: nil},
58+
V0: Types.Vertex{Position: v[0], Normal: nil},
59+
V1: Types.Vertex{Position: v[3], Normal: nil},
60+
V2: Types.Vertex{Position: v[4], Normal: nil},
6161
},
6262
{
63-
V0: &Types.Vertex{Position: v[7], Normal: nil},
64-
V1: &Types.Vertex{Position: v[4], Normal: nil},
65-
V2: &Types.Vertex{Position: v[3], Normal: nil},
63+
V0: Types.Vertex{Position: v[7], Normal: nil},
64+
V1: Types.Vertex{Position: v[4], Normal: nil},
65+
V2: Types.Vertex{Position: v[3], Normal: nil},
6666
},
6767
// bottom
6868
{
69-
V0: &Types.Vertex{Position: v[6], Normal: nil},
70-
V1: &Types.Vertex{Position: v[5], Normal: nil},
71-
V2: &Types.Vertex{Position: v[4], Normal: nil},
69+
V0: Types.Vertex{Position: v[6], Normal: nil},
70+
V1: Types.Vertex{Position: v[5], Normal: nil},
71+
V2: Types.Vertex{Position: v[4], Normal: nil},
7272
},
7373
{
74-
V0: &Types.Vertex{Position: v[7], Normal: nil},
75-
V1: &Types.Vertex{Position: v[6], Normal: nil},
76-
V2: &Types.Vertex{Position: v[4], Normal: nil},
74+
V0: Types.Vertex{Position: v[7], Normal: nil},
75+
V1: Types.Vertex{Position: v[6], Normal: nil},
76+
V2: Types.Vertex{Position: v[4], Normal: nil},
7777
},
7878
},
7979
}

0 commit comments

Comments
 (0)