Skip to content

Commit 4a299c7

Browse files
committed
Add common Geometry protocol
1 parent 151d16f commit 4a299c7

15 files changed

+66
-37
lines changed

Sources/Manifold3D/CrossSection/CrossSection+Operations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public extension CrossSection {
1111
Self(crossSection.Boolean(other.crossSection, op.manifoldOp))
1212
}
1313

14-
func warp(_ function: @escaping (V) -> V) -> Self {
14+
func warp(_ function: @escaping (Vector) -> Vector) -> Self {
1515
Self(bridge.Warp(crossSection) {
1616
$0.pointee = function(.init($0.pointee)).vec2
1717
})
@@ -35,7 +35,7 @@ public extension CrossSection {
3535
Self(manifold.CrossSection.Hull(.init(crossSections.map(\.crossSection))))
3636
}
3737

38-
static func hull(_ points: [V]) -> Self {
38+
static func hull(_ points: [Vector]) -> Self {
3939
Self(manifold.CrossSection.Hull(.init(points.map(\.vec2))))
4040
}
4141
}
@@ -45,7 +45,7 @@ public extension CrossSection {
4545
height: Double,
4646
divisions: Int = 0,
4747
twist twistDegrees: Double = 0,
48-
scaleTop: V? = nil
48+
scaleTop: Vector? = nil
4949
) -> Manifold<V3> {
5050
Manifold(manifold.Manifold.Extrude(
5151
crossSection.ToPolygons(), height, Int32(divisions), twistDegrees, scaleTop?.vec2 ?? .init(1, 1))

Sources/Manifold3D/CrossSection/CrossSection+Properties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public extension CrossSection {
55
crossSection.IsEmpty()
66
}
77

8-
var bounds: (min: V, max: V) {
8+
var bounds: (min: Vector, max: Vector) {
99
let box = crossSection.Bounds()
1010
return (.init(box.min), .init(box.max))
1111
}

Sources/Manifold3D/CrossSection/CrossSection+Shapes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public extension CrossSection {
55
Self(manifold.CrossSection())
66
}
77

8-
static func square(size: V) -> Self {
8+
static func square(size: Vector) -> Self {
99
Self(manifold.CrossSection.Square(size.vec2))
1010
}
1111

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import ManifoldCPP
22

33
public extension CrossSection {
4-
func transform<M: Matrix2x3>(_ transform: M) -> Self {
4+
func transform(_ transform: any Matrix2x3) -> Self {
55
Self(crossSection.Transform(transform.mat2x3))
66
}
77

8-
func translate(_ translation: V) -> Self {
8+
func translate(_ translation: Vector) -> Self {
99
Self(crossSection.Translate(translation.vec2))
1010
}
1111

12-
func scale(_ scale: V) -> Self {
12+
func scale(_ scale: Vector) -> Self {
1313
Self(crossSection.Scale(scale.vec2))
1414
}
1515

1616
func rotate(_ degrees: Double) -> Self {
1717
Self(crossSection.Rotate(degrees))
1818
}
1919

20-
func mirror(_ axis: V) -> Self {
20+
func mirror(_ axis: Vector) -> Self {
2121
Self(crossSection.Mirror(axis.vec2))
2222
}
2323
}

Sources/Manifold3D/CrossSection/CrossSection.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ManifoldCPP
22
import Cxx
33

4-
public struct CrossSection<V: Vector2>: @unchecked Sendable {
4+
public struct CrossSection<Vector: Vector2>: Geometry, @unchecked Sendable {
55
internal let crossSection: manifold.CrossSection
66

77
internal init(_ crossSection: manifold.CrossSection) {
@@ -10,11 +10,11 @@ public struct CrossSection<V: Vector2>: @unchecked Sendable {
1010
}
1111

1212
public extension CrossSection {
13-
init(polygons: [Polygon<V>], fillRule: FillRule) {
13+
init(polygons: [Polygon<Vector>], fillRule: FillRule) {
1414
self.init(manifold.CrossSection(Polygon.manifoldPolygons(polygons), fillRule.manifoldFillRule))
1515
}
1616

17-
func polygons() -> [Polygon<V>] {
17+
func polygons() -> [Polygon<Vector>] {
1818
crossSection.ToPolygons().map { Polygon($0) }
1919
}
2020

Sources/Manifold3D/Geometry.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Foundation
2+
3+
public protocol Geometry<Vector>: Sendable {
4+
associatedtype Vector
5+
associatedtype Rotation
6+
associatedtype Transform
7+
8+
static var empty: Self { get }
9+
init(composing: [Self])
10+
func decompose() -> [Self]
11+
12+
var isEmpty: Bool { get }
13+
var bounds: (min: Vector, max: Vector) { get }
14+
var vertexCount: Int { get }
15+
16+
func transform(_ transform: Transform) -> Self
17+
func translate(_ translation: Vector) -> Self
18+
func scale(_ scale: Vector) -> Self
19+
func rotate(_ rotation: Rotation) -> Self
20+
21+
func boolean(_ op: BooleanOperation, with other: Self) -> Self
22+
static func boolean(_ op: BooleanOperation, with children: [Self]) -> Self
23+
24+
func hull() -> Self
25+
static func hull(_ children: [Self]) -> Self
26+
static func hull(_ points: [Vector]) -> Self
27+
28+
func warp(_ function: @escaping (Vector) -> Vector) -> Self
29+
func simplify(epsilon: Double) -> Self
30+
}

Sources/Manifold3D/Manifold/Manifold+Operations.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public extension Manifold {
1010
Self(mesh.Boolean(other.mesh, op.manifoldOp))
1111
}
1212

13-
func warp(_ function: @escaping (V) -> V) -> Manifold {
13+
func warp(_ function: @escaping (Vector) -> Vector) -> Manifold {
1414
Self(bridge.Warp(mesh) {
1515
$0.pointee = function(.init($0.pointee)).vec3
1616
})
@@ -26,7 +26,7 @@ public extension Manifold {
2626
Self(manifold.Manifold.Hull(.init(meshes.map(\.mesh))))
2727
}
2828

29-
static func hull(_ points: [V]) -> Self {
29+
static func hull(_ points: [Vector]) -> Self {
3030
Self(manifold.Manifold.Hull(.init(points.map(\.vec3))))
3131
}
3232
}
@@ -37,12 +37,12 @@ public extension Manifold {
3737
return (Manifold(results.first), Manifold(results.second))
3838
}
3939

40-
func split(by plane: V, originOffset: Double) -> (Manifold, Manifold) {
40+
func split(by plane: Vector, originOffset: Double) -> (Manifold, Manifold) {
4141
let results = mesh.SplitByPlane(plane.vec3, originOffset)
4242
return (Manifold(results.first), Manifold(results.second))
4343
}
4444

45-
func trim(by plane: V, originOffset: Double) -> Manifold {
45+
func trim(by plane: Vector, originOffset: Double) -> Manifold {
4646
Manifold(mesh.TrimByPlane(plane.vec3, originOffset))
4747
}
4848
}

Sources/Manifold3D/Manifold/Manifold+Properties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public extension Manifold {
55
mesh.IsEmpty()
66
}
77

8-
var bounds: (min: V, max: V) {
8+
var bounds: (min: Vector, max: Vector) {
99
let box = mesh.BoundingBox()
1010
return (.init(box.min), .init(box.max))
1111
}

Sources/Manifold3D/Manifold/Manifold+Shapes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public extension Manifold {
2323
}
2424

2525
static func levelSet(
26-
bounds: (V, V),
26+
bounds: (Vector, Vector),
2727
edgeLength: Double,
2828
level: Double = 0,
2929
tolerance: Double = -1,
30-
functor: @escaping (V) -> Double
30+
functor: @escaping (Vector) -> Double
3131
) -> Self {
3232
Self(bridge.LevelSet({ functor(.init($0)) }, .init(bounds.0.vec3, bounds.1.vec3), edgeLength, level, tolerance, true))
3333
}

Sources/Manifold3D/Manifold/Manifold+Smoothing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ManifoldCPP
33
import ManifoldBridge
44

55
public extension Manifold {
6-
init(meshGL: MeshGL<V>, smoothEdges: [MeshGL<V>.EdgeReference: Double]) {
6+
init(meshGL: MeshGL<Vector>, smoothEdges: [MeshGL<Vector>.EdgeReference: Double]) {
77
self = Self(manifold.Manifold.Smooth(meshGL.meshGL, .init(smoothEdges.map {
88
manifold.Smoothness(halfedge: $0.key.index, smoothness: $0.value)
99
})))
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import ManifoldCPP
22

33
public extension Manifold {
4-
func transform<T: Matrix3x4>(_ transform: T) -> Self {
4+
func transform(_ transform: any Matrix3x4) -> Self {
55
Self(mesh.Transform(transform.mat3x4))
66
}
77

8-
func translate(_ translation: V) -> Self {
8+
func translate(_ translation: Vector) -> Self {
99
Self(mesh.Translate(translation.vec3))
1010
}
1111

12-
func scale(_ scale: V) -> Self {
12+
func scale(_ scale: Vector) -> Self {
1313
Self(mesh.Scale(scale.vec3))
1414
}
1515

16-
func rotate(_ rotation: V) -> Self {
16+
func rotate(_ rotation: Vector) -> Self {
1717
Self(mesh.Rotate(rotation.x, rotation.y, rotation.z))
1818
}
1919

20-
func mirror(_ normal: V) -> Self {
20+
func mirror(_ normal: Vector) -> Self {
2121
Self(mesh.Mirror(normal.vec3))
2222
}
2323
}

Sources/Manifold3D/Manifold/Manifold+VertexProperties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ManifoldCPP
33
import ManifoldBridge
44

55
public extension Manifold {
6-
func setProperties(channelCount: Int, getter function: @escaping (_ position: V, _ oldProperties: [Double]) -> [Double]) -> Self {
6+
func setProperties(channelCount: Int, getter function: @escaping (_ position: Vector, _ oldProperties: [Double]) -> [Double]) -> Self {
77
let oldCount = propertyCount
88
return Self(bridge.SetProperties(mesh, .init(channelCount)) { newProps, position, oldProps in
99
let props = function(.init(position), Array(UnsafeBufferPointer(start: oldProps, count: oldCount)))

Sources/Manifold3D/Manifold/Manifold.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ManifoldCPP
22

3-
public struct Manifold<V: Vector3>: @unchecked Sendable {
3+
public struct Manifold<Vector: Vector3>: Geometry, @unchecked Sendable {
44
internal let mesh: manifold.Manifold
55

66
internal init(_ mesh: manifold.Manifold) {
@@ -9,14 +9,14 @@ public struct Manifold<V: Vector3>: @unchecked Sendable {
99
}
1010

1111
public extension Manifold {
12-
init(_ meshGL: MeshGL<V>) throws(Error) {
12+
init(_ meshGL: MeshGL<Vector>) throws(Error) {
1313
self.init(manifold.Manifold(meshGL.meshGL))
1414
if isEmpty, let error = self.status {
1515
throw error
1616
}
1717
}
1818

19-
func meshGL(normalChannelIndex: Int = -1) -> MeshGL<V> {
19+
func meshGL(normalChannelIndex: Int = -1) -> MeshGL<Vector> {
2020
MeshGL(meshGL: mesh.GetMeshGL64(Int32(normalChannelIndex)))
2121
}
2222

Sources/Manifold3D/MeshGL.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import ManifoldCPP
33
import ManifoldBridge
44

5-
public struct MeshGL<V: Vector3> {
5+
public struct MeshGL<Vector: Vector3> {
66
internal let meshGL: manifold.MeshGL64
77

88
internal init(meshGL: manifold.MeshGL64) {
@@ -11,7 +11,7 @@ public struct MeshGL<V: Vector3> {
1111
}
1212

1313
public extension MeshGL {
14-
init(vertices: [V], triangles: [Triangle]) {
14+
init(vertices: [Vector], triangles: [Triangle]) {
1515
var meshGL = manifold.MeshGL64()
1616
meshGL.numProp = 3
1717
meshGL.vertProperties = .init(vertices.flatMap { [$0.x, $0.y, $0.z] })
@@ -30,9 +30,9 @@ public extension MeshGL {
3030
meshGL.faceID.map { Int($0) }
3131
}
3232

33-
var vertices: [V] {
33+
var vertices: [Vector] {
3434
(0..<Int(meshGL.NumVert())).map {
35-
V(meshGL.GetVertPos($0))
35+
Vector(meshGL.GetVertPos($0))
3636
}
3737
}
3838

Sources/Manifold3D/Types/Matrices.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import ManifoldCPP
22

3-
public protocol Matrix3x4 {
3+
public protocol Matrix {
44
subscript(_ row: Int, _ column: Int) -> Double { get }
55
}
66

7-
public protocol Matrix2x3 {
8-
subscript(_ row: Int, _ column: Int) -> Double { get }
9-
}
7+
public protocol Matrix3x4: Matrix {}
8+
public protocol Matrix2x3: Matrix {}
109

1110
internal extension Matrix3x4 {
1211
var mat3x4: manifold.mat3x4 {

0 commit comments

Comments
 (0)