Skip to content

Commit

Permalink
Add Environment.Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasf committed Feb 18, 2024
1 parent 3cefaaa commit 84ba85b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Sources/SwiftSCAD/Environment/EnvironmentReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ public func EnvironmentReader(@UnionBuilder3D body: @escaping (Environment) -> a
}

public extension Geometry2D {
func readingEnvironment(@UnionBuilder2D _ body: @escaping (Environment) -> any Geometry2D) -> any Geometry2D {
EnvironmentReader2D(body: body)
func readingEnvironment(@UnionBuilder2D _ body: @escaping (any Geometry2D, Environment) -> any Geometry2D) -> any Geometry2D {
EnvironmentReader2D { environment in
body(self, environment)
}
}
}

public extension Geometry3D {
func readingEnvironment(@UnionBuilder3D _ body: @escaping (Environment) -> any Geometry3D) -> any Geometry3D {
EnvironmentReader3D(body: body)
func readingEnvironment(@UnionBuilder3D _ body: @escaping (any Geometry3D, Environment) -> any Geometry3D) -> any Geometry3D {
EnvironmentReader3D { environment in
body(self, environment)
}
}
}
38 changes: 38 additions & 0 deletions Sources/SwiftSCAD/Environment/Operation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

public extension Environment {
private static let key = Environment.ValueKey(rawValue: "SwiftSCAD.Operation")

enum Operation {
case addition
case subtraction

static prefix func !(_ op: Operation) -> Operation {
op == .addition ? .subtraction : .addition
}
}

var operation: Operation {
self[Self.key] as? Operation ?? .addition
}

internal func invertingOperation() -> Environment {
setting(key: Self.key, value: !operation)
}
}

internal extension Geometry2D {
func invertingOperation() -> any Geometry2D {
withEnvironment { environment in
environment.invertingOperation()
}
}
}

internal extension Geometry3D {
func invertingOperation() -> any Geometry3D {
withEnvironment { environment in
environment.invertingOperation()
}
}
}
2 changes: 2 additions & 0 deletions Sources/SwiftSCAD/Operations/Boolean/Difference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct Difference3D: CoreGeometry3D {
init(positive: any Geometry3D, negative: any Geometry3D) {
self.positive = positive
self.negative = negative
.invertingOperation()
}

func call(in environment: Environment) -> SCADCall {
Expand Down Expand Up @@ -46,6 +47,7 @@ struct Difference2D: CoreGeometry2D {
init(positive: any Geometry2D, negative: any Geometry2D) {
self.positive = positive
self.negative = negative
.invertingOperation()
}

func call(in environment: Environment) -> SCADCall {
Expand Down
27 changes: 27 additions & 0 deletions Tests/Tests/3DTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,31 @@ final class Geometry3DTests: XCTestCase {
.adding {}
.assertEqual(toFile: "empty3d")
}

func testOperations() {
let testGeometry = Box([1,2,3])
.assertOperation(.addition)
.subtracting {
Sphere(diameter: 3)
.assertOperation(.subtraction)
.subtracting {
Box([1,2,3])
.assertOperation(.addition)
}
.assertOperation(.subtraction)
}
.assertOperation(.addition)

_ = testGeometry
.scadString(in: .defaultEnvironment)
}
}

extension Geometry3D {
func assertOperation(_ expectedOperation: Environment.Operation) -> any Geometry3D {
readingEnvironment { geo, environment in
XCTAssertEqual(environment.operation, expectedOperation)
geo
}
}
}

0 comments on commit 84ba85b

Please sign in to comment.