Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/MeshTypes/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func (a Matrix) Mul(b Matrix) Matrix {
return m
}

func (a Matrix) MulScalar(b float64) Matrix {
return Matrix{
a.X00 * b, a.X01 * b, a.X02 * b, a.X03 * b,
a.X10 * b, a.X11 * b, a.X12 * b, a.X13 * b,
a.X20 * b, a.X21 * b, a.X22 * b, a.X23 * b,
a.X30 * b, a.X31 * b, a.X32 * b, a.X33 * b,
}
}

func (a Matrix) MulPosition(b Vector) Vector {
x := a.X00*b.X + a.X01*b.Y + a.X02*b.Z + a.X03
y := a.X10*b.X + a.X11*b.Y + a.X12*b.Z + a.X13
Expand Down
71 changes: 34 additions & 37 deletions pkg/MeshTypes/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import (
)

type Mesh struct {
Triangles []*Triangle
Triangles []Triangle
}

func (obj *Mesh) AddTriangle(triangle *Triangle) {
func (obj *Mesh) AddTriangle(triangle Triangle) {
obj.Triangles = append(obj.Triangles, triangle)
}

func (obj *Mesh) Copy() Mesh {
triangles := make([]*Triangle, len(obj.Triangles))
for index, triangle := range obj.Triangles {
temp := triangle.Copy()
triangles[index] = &temp
}
triangles := make([]Triangle, len(obj.Triangles))
copy(triangles, obj.Triangles)
return Mesh{Triangles: triangles}
}

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

func (obj *Mesh) RotateAndTranslate(translationMatrix Matrix) {
for _, triangle := range obj.Triangles {
triangle.V0.Position = translationMatrix.MulPosition(triangle.V0.Position)
triangle.V1.Position = translationMatrix.MulPosition(triangle.V1.Position)
triangle.V2.Position = translationMatrix.MulPosition(triangle.V2.Position)
if triangle.V0.Normal != nil {
n0 := translationMatrix.MulDirection(*triangle.V0.Normal)
triangle.V0.Normal = &n0
for triangle_id := range obj.Triangles {
obj.Triangles[triangle_id].V0.Position = translationMatrix.MulPosition(obj.Triangles[triangle_id].V0.Position)
obj.Triangles[triangle_id].V1.Position = translationMatrix.MulPosition(obj.Triangles[triangle_id].V1.Position)
obj.Triangles[triangle_id].V2.Position = translationMatrix.MulPosition(obj.Triangles[triangle_id].V2.Position)
if obj.Triangles[triangle_id].V0.Normal != nil {
n0 := translationMatrix.MulDirection(*obj.Triangles[triangle_id].V0.Normal)
obj.Triangles[triangle_id].V0.Normal = &n0
}
if triangle.V1.Normal != nil {
n1 := translationMatrix.MulDirection(*triangle.V1.Normal)
triangle.V1.Normal = &n1
if obj.Triangles[triangle_id].V1.Normal != nil {
n1 := translationMatrix.MulDirection(*obj.Triangles[triangle_id].V1.Normal)
obj.Triangles[triangle_id].V1.Normal = &n1
}
if triangle.V2.Normal != nil {
n2 := translationMatrix.MulDirection(*triangle.V2.Normal)
triangle.V2.Normal = &n2
if obj.Triangles[triangle_id].V2.Normal != nil {
n2 := translationMatrix.MulDirection(*obj.Triangles[triangle_id].V2.Normal)
obj.Triangles[triangle_id].V2.Normal = &n2
}
}
}

func (obj *Mesh) calculateBoundingBox() Vector {
func (obj *Mesh) CalculateBoundingBox() Vector {
// init with first triangle to prohibit 0 values being min or max
if len(obj.Triangles) == 0 || obj.Triangles[0] == nil || obj.Triangles[0].V0 == nil {
if len(obj.Triangles) == 0 {
return Vector{}
}
min := obj.Triangles[0].V0.Position
max := obj.Triangles[0].V0.Position

for _, triangle := range obj.Triangles {
min = triangle.V0.Position.Min(&min)
max = triangle.V0.Position.Max(&max)
min = triangle.V0.Position.Min(min)
max = triangle.V0.Position.Max(max)

min = triangle.V1.Position.Min(&min)
max = triangle.V1.Position.Max(&max)
min = triangle.V1.Position.Min(min)
max = triangle.V1.Position.Max(max)

min = triangle.V2.Position.Min(&min)
max = triangle.V2.Position.Max(&max)
min = triangle.V2.Position.Min(min)
max = triangle.V2.Position.Max(max)
}
return Vector{
X: max.X - min.X,
Expand All @@ -76,26 +73,26 @@ func (obj *Mesh) calculateBoundingBox() Vector {
}

func (obj *Mesh) Scale(scaling Vector) error {
scaledVectors := make(map[*Vertex]struct{})
for _, triangle := range obj.Triangles {
scaledVectors := make(map[Vertex]struct{})
for triangle_id, triangle := range obj.Triangles {
if _, exists := scaledVectors[triangle.V0]; !exists {
triangle.V0.Position = triangle.V0.Position.Mult(scaling)
scaledVectors[triangle.V0] = struct{}{}
obj.Triangles[triangle_id].V0.Position = triangle.V0.Position.Mult(scaling)
scaledVectors[obj.Triangles[triangle_id].V0] = struct{}{}
}
if _, exists := scaledVectors[triangle.V1]; !exists {
triangle.V1.Position = triangle.V1.Position.Mult(scaling)
scaledVectors[triangle.V1] = struct{}{}
obj.Triangles[triangle_id].V1.Position = triangle.V1.Position.Mult(scaling)
scaledVectors[obj.Triangles[triangle_id].V1] = struct{}{}
}
if _, exists := scaledVectors[triangle.V2]; !exists {
triangle.V2.Position = triangle.V2.Position.Mult(scaling)
scaledVectors[triangle.V2] = struct{}{}
obj.Triangles[triangle_id].V2.Position = triangle.V2.Position.Mult(scaling)
scaledVectors[obj.Triangles[triangle_id].V2] = struct{}{}
}
}
return nil
}

func (obj *Mesh) ScaleToDimensions(desiredSize *Vector) error {
actual := obj.calculateBoundingBox()
actual := obj.CalculateBoundingBox()
if actual.X == 0 && actual.Y == 0 && actual.Z == 0 {
return fmt.Errorf("invalid Mesh with 0 dimension")
}
Expand Down
11 changes: 4 additions & 7 deletions pkg/MeshTypes/triangle.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package MeshTypes

type Triangle struct {
V0 *Vertex
V1 *Vertex
V2 *Vertex
V0 Vertex
V1 Vertex
V2 Vertex
}

func (t *Triangle) Normal() Vector {
Expand All @@ -13,8 +13,5 @@ func (t *Triangle) Normal() Vector {
}

func (obj *Triangle) Copy() Triangle {
V0 := obj.V0.Copy()
V1 := obj.V1.Copy()
V2 := obj.V2.Copy()
return Triangle{V0: &V0, V1: &V1, V2: &V2}
return Triangle{V0: obj.V0.Copy(), V1: obj.V1.Copy(), V2: obj.V2.Copy()}
}
8 changes: 4 additions & 4 deletions pkg/MeshTypes/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ func (a Vector) Normalize() Vector {
return Vector{a.X * r, a.Y * r, a.Z * r}
}

func (obj Vector) ToVertex(normal *Vector) *Vertex {
return &Vertex{
func (obj Vector) ToVertex(normal *Vector) Vertex {
return Vertex{
Position: obj,
Normal: normal,
}
}

func (a Vector) Min(b *Vector) Vector {
func (a Vector) Min(b Vector) Vector {
return Vector{
math.Min(a.X, b.X),
math.Min(a.Y, b.Y),
math.Min(a.Z, b.Z),
}
}

func (a Vector) Max(b *Vector) Vector {
func (a Vector) Max(b Vector) Vector {
return Vector{
math.Max(a.X, b.X),
math.Max(a.Y, b.Y),
Expand Down
20 changes: 10 additions & 10 deletions pkg/file_handlers/3ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func Load3DS(fileData *[]byte, desiredSize *MeshTypes.Vector) (*MeshTypes.Mesh,

file := bytes.NewReader(*fileData)

var vertices []*MeshTypes.Vertex
var faces []*MeshTypes.Triangle
var triangles []*MeshTypes.Triangle
var vertices []MeshTypes.Vertex
var faces []MeshTypes.Triangle
var triangles []MeshTypes.Triangle
for {
header := ChunkHeader{}
if err := binary.Read(file, binary.LittleEndian, &header); err != nil {
Expand Down Expand Up @@ -100,7 +100,7 @@ func Load3DS(fileData *[]byte, desiredSize *MeshTypes.Vector) (*MeshTypes.Mesh,
return mesh, nil
}

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

func readVertexList(file *bytes.Reader) ([]*MeshTypes.Vertex, error) {
func readVertexList(file *bytes.Reader) ([]MeshTypes.Vertex, error) {
var count uint16
if err := binary.Read(file, binary.LittleEndian, &count); err != nil {
return nil, err
}
result := make([]*MeshTypes.Vertex, count)
result := make([]MeshTypes.Vertex, count)
for i := range result {
var v [3]float32
if err := binary.Read(file, binary.LittleEndian, &v); err != nil {
return nil, err
}
result[i] = &MeshTypes.Vertex{
result[i] = MeshTypes.Vertex{
Position: MeshTypes.Vector{X: float64(v[0]), Y: float64(v[1]), Z: float64(v[2])},
}
}
return result, nil
}

func readFaceList(file *bytes.Reader, vertices []*MeshTypes.Vertex) ([]*MeshTypes.Triangle, error) {
func readFaceList(file *bytes.Reader, vertices []MeshTypes.Vertex) ([]MeshTypes.Triangle, error) {
var count uint16
if err := binary.Read(file, binary.LittleEndian, &count); err != nil {
return nil, err
}
result := make([]*MeshTypes.Triangle, count)
result := make([]MeshTypes.Triangle, count)
for i := range result {
var v [4]uint16
if err := binary.Read(file, binary.LittleEndian, &v); err != nil {
return nil, err
}
result[i] = &MeshTypes.Triangle{
result[i] = MeshTypes.Triangle{
V0: vertices[v[0]], V1: vertices[v[1]], V2: vertices[v[2]],
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/file_handlers/gltf.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func LoadGLTF(file io.Reader, desiredSize *Types.Vector) (*Types.Mesh, error) {
Y: posAccessor.Max[2],
Z: posAccessor.Max[1],
})
min = min.Min(&tempMin)
max = max.Max(&tempMax)
min = min.Min(tempMin)
max = max.Max(tempMax)
}
}

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

mesh.AddTriangle(&Types.Triangle{V0: v0, V1: v1, V2: v2})
mesh.AddTriangle(Types.Triangle{V0: v0, V1: v1, V2: v2})
}

meshes.Add(&mesh)
Expand Down
74 changes: 37 additions & 37 deletions pkg/primitives/cube.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,72 @@ func NewCube() Types.Mesh {
{X: -1, Y: -1, Z: -1}, {X: -1, Y: -1, Z: 1}, {X: 1, Y: -1, Z: 1}, {X: 1, Y: -1, Z: -1},
}
mesh := Types.Mesh{
Triangles: []*Types.Triangle{
Triangles: []Types.Triangle{
// top
{
V0: &Types.Vertex{Position: v[1], Normal: nil},
V1: &Types.Vertex{Position: v[2], Normal: nil},
V2: &Types.Vertex{Position: v[3], Normal: nil},
V0: Types.Vertex{Position: v[1], Normal: nil},
V1: Types.Vertex{Position: v[2], Normal: nil},
V2: Types.Vertex{Position: v[3], Normal: nil},
},
{
V0: &Types.Vertex{Position: v[0], Normal: nil},
V1: &Types.Vertex{Position: v[1], Normal: nil},
V2: &Types.Vertex{Position: v[3], Normal: nil},
V0: Types.Vertex{Position: v[0], Normal: nil},
V1: Types.Vertex{Position: v[1], Normal: nil},
V2: Types.Vertex{Position: v[3], Normal: nil},
},
// left
{
V0: &Types.Vertex{Position: v[4], Normal: nil},
V1: &Types.Vertex{Position: v[1], Normal: nil},
V2: &Types.Vertex{Position: v[0], Normal: nil},
V0: Types.Vertex{Position: v[4], Normal: nil},
V1: Types.Vertex{Position: v[1], Normal: nil},
V2: Types.Vertex{Position: v[0], Normal: nil},
},
{
V0: &Types.Vertex{Position: v[4], Normal: nil},
V1: &Types.Vertex{Position: v[5], Normal: nil},
V2: &Types.Vertex{Position: v[1], Normal: nil},
V0: Types.Vertex{Position: v[4], Normal: nil},
V1: Types.Vertex{Position: v[5], Normal: nil},
V2: Types.Vertex{Position: v[1], Normal: nil},
},
// front
{
V0: &Types.Vertex{Position: v[5], Normal: nil},
V1: &Types.Vertex{Position: v[2], Normal: nil},
V2: &Types.Vertex{Position: v[1], Normal: nil},
V0: Types.Vertex{Position: v[5], Normal: nil},
V1: Types.Vertex{Position: v[2], Normal: nil},
V2: Types.Vertex{Position: v[1], Normal: nil},
},
{
V0: &Types.Vertex{Position: v[5], Normal: nil},
V1: &Types.Vertex{Position: v[6], Normal: nil},
V2: &Types.Vertex{Position: v[2], Normal: nil},
V0: Types.Vertex{Position: v[5], Normal: nil},
V1: Types.Vertex{Position: v[6], Normal: nil},
V2: Types.Vertex{Position: v[2], Normal: nil},
},
// right
{
V0: &Types.Vertex{Position: v[7], Normal: nil},
V1: &Types.Vertex{Position: v[3], Normal: nil},
V2: &Types.Vertex{Position: v[2], Normal: nil},
V0: Types.Vertex{Position: v[7], Normal: nil},
V1: Types.Vertex{Position: v[3], Normal: nil},
V2: Types.Vertex{Position: v[2], Normal: nil},
},
{
V0: &Types.Vertex{Position: v[7], Normal: nil},
V1: &Types.Vertex{Position: v[2], Normal: nil},
V2: &Types.Vertex{Position: v[6], Normal: nil},
V0: Types.Vertex{Position: v[7], Normal: nil},
V1: Types.Vertex{Position: v[2], Normal: nil},
V2: Types.Vertex{Position: v[6], Normal: nil},
},
// back
{
V0: &Types.Vertex{Position: v[0], Normal: nil},
V1: &Types.Vertex{Position: v[3], Normal: nil},
V2: &Types.Vertex{Position: v[4], Normal: nil},
V0: Types.Vertex{Position: v[0], Normal: nil},
V1: Types.Vertex{Position: v[3], Normal: nil},
V2: Types.Vertex{Position: v[4], Normal: nil},
},
{
V0: &Types.Vertex{Position: v[7], Normal: nil},
V1: &Types.Vertex{Position: v[4], Normal: nil},
V2: &Types.Vertex{Position: v[3], Normal: nil},
V0: Types.Vertex{Position: v[7], Normal: nil},
V1: Types.Vertex{Position: v[4], Normal: nil},
V2: Types.Vertex{Position: v[3], Normal: nil},
},
// bottom
{
V0: &Types.Vertex{Position: v[6], Normal: nil},
V1: &Types.Vertex{Position: v[5], Normal: nil},
V2: &Types.Vertex{Position: v[4], Normal: nil},
V0: Types.Vertex{Position: v[6], Normal: nil},
V1: Types.Vertex{Position: v[5], Normal: nil},
V2: Types.Vertex{Position: v[4], Normal: nil},
},
{
V0: &Types.Vertex{Position: v[7], Normal: nil},
V1: &Types.Vertex{Position: v[6], Normal: nil},
V2: &Types.Vertex{Position: v[4], Normal: nil},
V0: Types.Vertex{Position: v[7], Normal: nil},
V1: Types.Vertex{Position: v[6], Normal: nil},
V2: Types.Vertex{Position: v[4], Normal: nil},
},
},
}
Expand Down
Loading