Skip to content

Commit d22275d

Browse files
committed
Simplify STL output and remove readMesh() C++ bridge
Compute face normals from vertex cross products directly instead of pre-calculating them via calculateNormals. Use meshGL().vertices for vertex access, removing the need for the readMesh() C++ bridging method that had relied on cadova.BulkReadMesh.
1 parent f57614d commit d22275d

File tree

3 files changed

+9
-56
lines changed

3 files changed

+9
-56
lines changed

Package.resolved

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/Cadova/Concrete Layer/ConcreteGeometry.swift

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Manifold3D
22
import Foundation
3-
internal import CadovaCPP
43

54
public protocol ConcreteGeometry {
65
associatedtype Vector
@@ -64,35 +63,3 @@ extension Vector3D {
6463
}
6564
}
6665

67-
extension Manifold {
68-
func readMesh() -> (vertices: [Vector3D], triangles: [Manifold3D.Triangle], originalIDs: [Manifold.OriginalID: IndexSet]) {
69-
var vertices: [Vector3D] = []
70-
var triangles: [Manifold3D.Triangle] = []
71-
var originalIDs: [Manifold.OriginalID: IndexSet] = [:]
72-
73-
cadova.BulkReadMesh(mesh, { properties, vertexCount, propertyCount in
74-
guard let properties else { return }
75-
for i in 0..<vertexCount {
76-
vertices.append(Vector3D(properties[propertyCount * i], properties[propertyCount * i + 1], properties[propertyCount * i + 2]))
77-
}
78-
}, { indices, triangleCount in
79-
guard let indices else { return }
80-
for i in 0..<triangleCount {
81-
triangles.append(Manifold3D.Triangle(Int(indices[i * 3]), Int(indices[i * 3 + 1]), Int(indices[i * 3 + 2])))
82-
}
83-
84-
}, { runIndex, runIndexCount, runOriginalIDs, runOriginalIDCount in
85-
guard let runIndex, let runOriginalIDs else { return }
86-
87-
let ranges = (0..<runIndexCount).paired().map { index1, index2 in
88-
Int(runIndex[index1] / 3)..<Int(runIndex[index2] / 3)
89-
}
90-
originalIDs = ranges.enumerated().reduce(into: [:]) { result, item in
91-
let originalID = Int(runOriginalIDs[item.offset])
92-
result[originalID, default: IndexSet()].insert(integersIn: item.element)
93-
}
94-
})
95-
96-
return (vertices, triangles, originalIDs)
97-
}
98-
}

Sources/Cadova/Concrete Layer/Output Providers/BinarySTLDataProvider.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ struct BinarySTLDataProvider: OutputDataProvider {
1414
let allParts = [result] + solidParts
1515
let union = GeometryNode.boolean(allParts.map(\.node), type: .union)
1616

17-
var concrete = try await context.result(for: union).concrete
18-
concrete = concrete.calculateNormals(channelIndex: 0)
17+
let concrete = try await context.result(for: union).concrete
1918
let meshGL = concrete.meshGL()
2019

2120
let metadata = options[Metadata.self]
@@ -27,16 +26,12 @@ struct BinarySTLDataProvider: OutputDataProvider {
2726
}
2827

2928
private func stlData(for meshGL: MeshGL, header: String) -> Data {
30-
let properties = meshGL.vertexProperties
31-
let propertyCount = meshGL.propertyCount
29+
let vertices = meshGL.vertices
3230
let triangles = meshGL.triangles
3331

34-
assert(properties.count == meshGL.vertexCount * 6) // XYZ + normals
35-
36-
func double(at vertexIndex: Int, offset: Int) -> Double { properties[vertexIndex * propertyCount + offset] }
37-
func position(at index: Int) -> Vector3D { Vector3D(x: double(at: index, offset: 0), y: double(at: index, offset: 1), z: double(at: index, offset: 2)) }
38-
func normal(at index: Int) -> Vector3D { Vector3D(x: double(at: index, offset: 3), y: double(at: index, offset: 4), z: double(at: index, offset: 5)) }
39-
func triangleNormal(_ triangle: Manifold3D.Triangle) -> Vector3D { (normal(at: triangle.a) + normal(at: triangle.b) + normal(at: triangle.c)).normalized }
32+
func triangleNormal(_ triangle: Manifold3D.Triangle) -> Vector3D {
33+
((vertices[triangle.b] - vertices[triangle.a]) × (vertices[triangle.c] - vertices[triangle.a])).normalized
34+
}
4035

4136
func append(_ int: UInt32) {
4237
var value = int.littleEndian
@@ -74,9 +69,9 @@ struct BinarySTLDataProvider: OutputDataProvider {
7469

7570
for triangle in triangles {
7671
append(triangleNormal(triangle))
77-
append(position(at: triangle.a))
78-
append(position(at: triangle.b))
79-
append(position(at: triangle.c))
72+
append(vertices[triangle.a])
73+
append(vertices[triangle.b])
74+
append(vertices[triangle.c])
8075
append(UInt16(0)) // attribute byte count
8176
}
8277

0 commit comments

Comments
 (0)