summaryrefslogtreecommitdiff
path: root/stl.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2022-03-16 20:18:18 +0100
committerxengineering <me@xengineering.eu>2022-03-16 20:18:18 +0100
commit0340cb664f19ab8f14d893c8d82b6dead705a794 (patch)
treeb311ee5d1040a2beabda5aab25372b0145e89ea2 /stl.go
parentb38538bfd5a3ad3d55473a5c3970f0a46bc63970 (diff)
downloadstlscope-0340cb664f19ab8f14d893c8d82b6dead705a794.tar
stlscope-0340cb664f19ab8f14d893c8d82b6dead705a794.tar.zst
stlscope-0340cb664f19ab8f14d893c8d82b6dead705a794.zip
Reduce code width to <= 80 characters
Diffstat (limited to 'stl.go')
-rw-r--r--stl.go52
1 files changed, 35 insertions, 17 deletions
diff --git a/stl.go b/stl.go
index 60ada3e..851a003 100644
--- a/stl.go
+++ b/stl.go
@@ -50,8 +50,11 @@ func ReadBinaryStlFile(filePath string) (StlModel, 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
+// 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)
@@ -60,24 +63,34 @@ func ParseBinaryStlTriangle(data []byte) *Triangle { // FIXME: This function sh
triangle.points[2] = new(Point)
// parse x, y and z coordinate for corner point a
- triangle.points[0].scalars[0] = math.Float32frombits(binary.LittleEndian.Uint32(data[12:16]))
- triangle.points[0].scalars[1] = math.Float32frombits(binary.LittleEndian.Uint32(data[16:20]))
- triangle.points[0].scalars[2] = math.Float32frombits(binary.LittleEndian.Uint32(data[20:24]))
+ triangle.points[0].scalars[0] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[12:16]))
+ triangle.points[0].scalars[1] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[16:20]))
+ triangle.points[0].scalars[2] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[20:24]))
// parse x, y and z coordinate for corner point b
- triangle.points[1].scalars[0] = math.Float32frombits(binary.LittleEndian.Uint32(data[24:28]))
- triangle.points[1].scalars[1] = math.Float32frombits(binary.LittleEndian.Uint32(data[28:32]))
- triangle.points[1].scalars[2] = math.Float32frombits(binary.LittleEndian.Uint32(data[32:36]))
+ triangle.points[1].scalars[0] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[24:28]))
+ triangle.points[1].scalars[1] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[28:32]))
+ triangle.points[1].scalars[2] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[32:36]))
// parse x, y and z coordinate for corner point c
- triangle.points[2].scalars[0] = math.Float32frombits(binary.LittleEndian.Uint32(data[36:40]))
- triangle.points[2].scalars[1] = math.Float32frombits(binary.LittleEndian.Uint32(data[40:44]))
- triangle.points[2].scalars[2] = math.Float32frombits(binary.LittleEndian.Uint32(data[44:48]))
+ triangle.points[2].scalars[0] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[36:40]))
+ triangle.points[2].scalars[1] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[40:44]))
+ triangle.points[2].scalars[2] = math.Float32frombits(
+ binary.LittleEndian.Uint32(data[44:48]))
return triangle
}
-func (stl StlModel) toVertices() (vertex_position []float32, vertex_normal []float32) {
+func (stl StlModel) toVertices() (vertex_position []float32,
+ vertex_normal []float32) {
vertex_position = make([]float32, stl.numberOfTriangles * 9)
vertex_normal = make([]float32, stl.numberOfTriangles * 9)
@@ -90,20 +103,25 @@ func (stl StlModel) toVertices() (vertex_position []float32, vertex_normal []flo
for scalarIndex,scalar := range(point.scalars) {
- vertex_position[triangleIndex*9+pointIndex*3+scalarIndex] = scalar
+ i := triangleIndex * 9 + pointIndex * 3 + scalarIndex
+ vertex_position[i] = scalar
}
}
// calculate normal
- point0 = Vector3{triangle.points[0].scalars[0], triangle.points[0].scalars[1], triangle.points[0].scalars[2]}
- point1 = Vector3{triangle.points[1].scalars[0], triangle.points[1].scalars[1], triangle.points[1].scalars[2]}
- point2 = Vector3{triangle.points[2].scalars[0], triangle.points[2].scalars[1], triangle.points[2].scalars[2]}
+ point0 = Vector3{triangle.points[0].scalars[0],
+ triangle.points[0].scalars[1], triangle.points[0].scalars[2]}
+ point1 = Vector3{triangle.points[1].scalars[0],
+ triangle.points[1].scalars[1], triangle.points[1].scalars[2]}
+ point2 = Vector3{triangle.points[2].scalars[0],
+ triangle.points[2].scalars[1], triangle.points[2].scalars[2]}
edge0 = point1.subtract(point0)
edge1 = point2.subtract(point1)
normal = edge0.crossProduct(edge1)
- normal.divideScalar(2.0) // length of normal vector corresponds to triangle area
+ normal.divideScalar(2.0) // length of normal vector corresponds to
+ // triangle area
// save normal to each vertex of the triangle
for i := 0; i<3; i++ {