summaryrefslogtreecommitdiff
path: root/stl.go
diff options
context:
space:
mode:
Diffstat (limited to 'stl.go')
-rw-r--r--stl.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/stl.go b/stl.go
index ceee2b7..f1d0a60 100644
--- a/stl.go
+++ b/stl.go
@@ -1,5 +1,9 @@
// vim: shiftwidth=4 tabstop=4 noexpandtab
+/*
+Wikipedia about STL file format: https://en.wikipedia.org/wiki/STL_(file_format)
+*/
+
package main
import (
@@ -9,12 +13,14 @@ import (
"math"
)
+// representation of binary STL file content
type BinaryStl struct {
header []byte
numberOfTriangles uint32
surface Surface
}
+// read and parse a given binary STL file
func ReadBinaryStlFile(filePath string) (BinaryStl, error) {
fileContent, err := ioutil.ReadFile(filePath)
@@ -42,21 +48,26 @@ func ReadBinaryStlFile(filePath string) (BinaryStl, error) {
return model,nil
}
+// parse the 50 bytes of the STL file representing a triangle (surface normal is ignored)
func ParseBinaryStlTriangle(data []byte) *Triangle { // FIXME: This function should only accept 50 byte slices/arrays
+ // allocate a new triangle and three corner points on the heap
triangle := new(Triangle)
- triangle.a = new(Point)
- triangle.b = new(Point)
- triangle.c = new(Point)
+ triangle.a = new(Point) // corner point a
+ triangle.b = new(Point) // corner point b
+ triangle.c = new(Point) // corner point c
+ // parse x, y and z coordinate for corner point a
triangle.a.x = math.Float32frombits(binary.LittleEndian.Uint32(data[12:16]))
triangle.a.y = math.Float32frombits(binary.LittleEndian.Uint32(data[16:20]))
triangle.a.z = math.Float32frombits(binary.LittleEndian.Uint32(data[20:24]))
+ // parse x, y and z coordinate for corner point b
triangle.b.x = math.Float32frombits(binary.LittleEndian.Uint32(data[24:28]))
triangle.b.y = math.Float32frombits(binary.LittleEndian.Uint32(data[28:32]))
triangle.b.z = math.Float32frombits(binary.LittleEndian.Uint32(data[32:36]))
+ // parse x, y and z coordinate for corner point c
triangle.c.x = math.Float32frombits(binary.LittleEndian.Uint32(data[36:40]))
triangle.c.y = math.Float32frombits(binary.LittleEndian.Uint32(data[40:44]))
triangle.c.z = math.Float32frombits(binary.LittleEndian.Uint32(data[44:48]))