From 48ffd0234b92ff5f72fb746304f08391a55c7709 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Wed, 9 Oct 2024 17:20:26 +0100 Subject: [PATCH] Rename project() to projected() --- Sources/Mesh+CSG.swift | 2 +- Sources/Path+Shapes.swift | 8 ++++---- Sources/Vector.swift | 16 ++++++++++++++-- Sources/Vertex.swift | 4 ++-- Tests/LineTests.swift | 10 +++++----- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Sources/Mesh+CSG.swift b/Sources/Mesh+CSG.swift index e40263ce..0f590e43 100644 --- a/Sources/Mesh+CSG.swift +++ b/Sources/Mesh+CSG.swift @@ -443,7 +443,7 @@ public extension Mesh { // Project each corner of mesh bounds onto plane to find radius var radius = 0.0 for corner in mesh.bounds.corners { - let p = corner.project(onto: plane) + let p = corner.projected(onto: plane) radius = max(radius, p.lengthSquared) } radius = radius.squareRoot() diff --git a/Sources/Path+Shapes.swift b/Sources/Path+Shapes.swift index 99af2da1..1f167edc 100644 --- a/Sources/Path+Shapes.swift +++ b/Sources/Path+Shapes.swift @@ -457,7 +457,7 @@ public extension Path { var p0 = points[0] for p1 in points.dropFirst() { let v = p1.position - p0.position - let l = v.project(onto: pathPlane.rawValue).length + let l = v.projected(onto: pathPlane.rawValue).length if l < v.length * 0.9 { aligned = false break @@ -479,11 +479,11 @@ public extension Path { var p1 = points[1] var p0p1 = (p1.position - p0.position) if align == .axis { - p0p1 = p0p1.project(onto: pathPlane.rawValue) + p0p1 = p0p1.projected(onto: pathPlane.rawValue) } rotateShape(by: rotationBetweenVectors(p0p1, shapeNormal)) if align != .axis, axisAligned { - p0p1 = p0p1.project(onto: pathPlane.rawValue) + p0p1 = p0p1.projected(onto: pathPlane.rawValue) } func rotationBetween(_ a: Path?, _ b: Path, checkSign: Bool = true) -> Rotation { @@ -527,7 +527,7 @@ public extension Path { func addShape(_ p2: PathPoint) { var p1p2 = (p2.position - p1.position) if axisAligned { - p1p2 = p1p2.project(onto: pathPlane.rawValue) + p1p2 = p1p2.projected(onto: pathPlane.rawValue) } let r = rotationBetweenVectors(p1p2, p0p1) / 2 rotateShape(by: r) diff --git a/Sources/Vector.swift b/Sources/Vector.swift index 8aca9373..bfe45810 100644 --- a/Sources/Vector.swift +++ b/Sources/Vector.swift @@ -281,10 +281,16 @@ public extension Vector { /// Returns the nearest point on the specified plane to the vector (representing a position in space). /// - Parameter plane: The plane to project onto. /// - Returns: The nearest point in 3D space that lies on the plane. - func project(onto plane: Plane) -> Vector { + func projected(onto plane: Plane) -> Vector { self - plane.normal * distance(from: plane) } + /// Deprecated. + @available(*, deprecated, renamed: "projected(onto:)") + func project(onto plane: Plane) -> Vector { + projected(onto: plane) + } + /// Returns the distance between the vector (representing a position in space) from the specified line. /// - Parameter line: The line to compare with. /// - Returns: The absolute perpendicular distance between the point and line. @@ -295,9 +301,15 @@ public extension Vector { /// Returns the nearest point on the specified line to the vector (representing a position in space). /// - Parameter line: The line to project onto. /// - Returns: The nearest point in 3D space that lies on the line. - func project(onto line: Line) -> Vector { + func projected(onto line: Line) -> Vector { line.direction * (self - line.origin).dot(line.direction) - line.origin } + + /// Deprecated. + @available(*, deprecated, renamed: "projected(onto:)") + func project(onto line: Line) -> Vector { + projected(onto: line) + } } extension Vector: UnkeyedCodable { diff --git a/Sources/Vertex.swift b/Sources/Vertex.swift index 9bc19208..b0eb7e11 100644 --- a/Sources/Vertex.swift +++ b/Sources/Vertex.swift @@ -175,12 +175,12 @@ public extension Vertex { /// - Parameter plane: The ``Plane`` against which the vertices are to be reflected. /// - Returns: A ``Vertex`` representing the reflected vertex. func reflected(along plane: Plane) -> Vertex { - let p = position.project(onto: plane) + let p = position.projected(onto: plane) let d = position - p let reflectedPosition = p - d let np = position + normal - let n = np.project(onto: plane) + let n = np.projected(onto: plane) let nd = np - n let reflectedNormalPosition = n - nd let reflectedNormal = reflectedPosition - reflectedNormalPosition diff --git a/Tests/LineTests.swift b/Tests/LineTests.swift index 3e13aa4d..5358e016 100644 --- a/Tests/LineTests.swift +++ b/Tests/LineTests.swift @@ -29,31 +29,31 @@ class LineTests: XCTestCase { func testProjectPointDown() { let l = Line(unchecked: .zero, direction: .unitX) let p = Vector(2, -2, 0) - XCTAssertEqual(p.project(onto: l), Vector(2, 0, 0)) + XCTAssertEqual(p.projected(onto: l), Vector(2, 0, 0)) } func testProjectPointUp() { let l = Line(unchecked: .zero, direction: .unitX) let p = Vector(3, 1, 0) - XCTAssertEqual(p.project(onto: l), Vector(3, 0, 0)) + XCTAssertEqual(p.projected(onto: l), Vector(3, 0, 0)) } func testProjectPointRight() { let l = Line(unchecked: .zero, direction: .unitY) let p = Vector(-3, 1, 0) - XCTAssertEqual(p.project(onto: l), .unitY) + XCTAssertEqual(p.projected(onto: l), .unitY) } func testProjectPointLeft() { let l = Line(unchecked: .zero, direction: .unitY) let p = Vector(3, -5, 0) - XCTAssertEqual(p.project(onto: l), Vector(0, -5, 0)) + XCTAssertEqual(p.projected(onto: l), Vector(0, -5, 0)) } func testProjectPointDiagonal() { let l = Line(unchecked: .zero, direction: Vector(1, 1, 0).normalized()) let p = Vector(0, 2, 0) - XCTAssert(p.project(onto: l).isEqual(to: Vector(1, 1, 0))) + XCTAssert(p.projected(onto: l).isEqual(to: Vector(1, 1, 0))) } // MARK: Line intersection