diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-05-20 12:16:36 +0200 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-05-20 12:20:19 +0200 |
commit | fbd513c7c2531c12fcd7e81bd4ce66c5ec5f6902 (patch) | |
tree | b564bbd69ab81e823c2153d57d0bbc40508458d7 /stl.go | |
parent | 904fa404b6e38564d1486b3b698ef1cb3d63ba8b (diff) | |
download | stlscope-fbd513c7c2531c12fcd7e81bd4ce66c5ec5f6902.tar stlscope-fbd513c7c2531c12fcd7e81bd4ce66c5ec5f6902.tar.zst stlscope-fbd513c7c2531c12fcd7e81bd4ce66c5ec5f6902.zip |
Implement STL Parsing
Diffstat (limited to 'stl.go')
-rw-r--r-- | stl.go | 66 |
1 files changed, 66 insertions, 0 deletions
@@ -0,0 +1,66 @@ +// vim: shiftwidth=4 tabstop=4 noexpandtab + +package main + +import ( + "io/ioutil" + "log" + "encoding/binary" + "math" +) + +type BinaryStl struct { + header []byte + numberOfTriangles uint32 + surface Surface +} + +func ReadBinaryStlFile(filePath string) (BinaryStl, error) { + + fileContent, err := ioutil.ReadFile(filePath) + if err != nil { + return BinaryStl{}, err + } + + model := BinaryStl{} + model.surface = Surface{} + + model.header = fileContent[0:80] + log.Printf("STL header: '%s'\n", string(model.header)) + + model.numberOfTriangles = binary.LittleEndian.Uint32(fileContent[80:84]) + log.Printf("STL model has %d triangles\n", model.numberOfTriangles) + + var i uint32 + for i = 0; i < model.numberOfTriangles; i++ { // for each expected triangle + start := 84 + i*50 // 50 bytes is length of one triangle + end := 84 + (i+1)*50 + model.surface.triangles = append(model.surface.triangles, + ParseBinaryStlTriangle(fileContent[start:end])) + } + + return model,nil +} + +func ParseBinaryStlTriangle(data []byte) *Triangle { // FIXME: This function should only accept 50 byte slices/arrays + + triangle := new(Triangle) + triangle.a = new(Point) + triangle.b = new(Point) + triangle.c = new(Point) + + 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])) + + 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])) + + 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])) + + return triangle +} + |