From 0c87cf3f007797714755b309ad31602cedb8af1e Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 20 May 2021 15:24:04 +0200 Subject: Update Documentation --- geometry.go | 3 +++ stl.go | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/geometry.go b/geometry.go index c9453a9..3c3ba83 100644 --- a/geometry.go +++ b/geometry.go @@ -2,18 +2,21 @@ package main +// representation of a three-dimensional point in space type Point struct { x float32 y float32 z float32 } +// a triangle consists of three points type Triangle struct { a *Point b *Point c *Point } +// a surface is made of a slice of triangles type Surface struct { triangles []*Triangle } 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])) -- cgit v1.2.3-70-g09d2