Skip to content

Commit

Permalink
Rename project() to projected()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Oct 9, 2024
1 parent a423406 commit 48ffd02
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/Mesh+CSG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions Sources/Path+Shapes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions Sources/Vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Vertex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions Tests/LineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 48ffd02

Please sign in to comment.