From e9dfc766be5e391db2ffa8eeab3480baa892b69a Mon Sep 17 00:00:00 2001 From: Markus Winter Date: Thu, 7 Oct 2021 22:54:30 +0200 Subject: [PATCH] Move imaginary helper functions into a distinct file --- .../QuaternionModule/ImaginaryHelper.swift | 77 +++++++++++++++++++ Sources/QuaternionModule/Transformation.swift | 35 +-------- .../QuaternionTests/TransformationTests.swift | 8 +- 3 files changed, 81 insertions(+), 39 deletions(-) create mode 100644 Sources/QuaternionModule/ImaginaryHelper.swift diff --git a/Sources/QuaternionModule/ImaginaryHelper.swift b/Sources/QuaternionModule/ImaginaryHelper.swift new file mode 100644 index 0000000..f62f6a1 --- /dev/null +++ b/Sources/QuaternionModule/ImaginaryHelper.swift @@ -0,0 +1,77 @@ +//===--- ImaginaryHelper.swift --------------------------------*- swift -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2019-2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +// Provides common vector operations on SIMD3 to ease the use of the quaternions +// imaginary/vector components internally to the module, and in tests. +extension SIMD3 where Scalar: FloatingPoint { + /// Returns a vector with infinity in all lanes + @usableFromInline @inline(__always) + internal static var infinity: Self { + SIMD3(repeating: .infinity) + } + + /// Returns a vector with nan in all lanes + @usableFromInline @inline(__always) + internal static var nan: Self { + SIMD3(repeating: .nan) + } + + /// True if all values of this instance are finite + @usableFromInline @inline(__always) + internal var isFinite: Bool { + x.isFinite && y.isFinite && z.isFinite + } + + /// The ∞-norm of the value (`max(abs(x), abs(y), abs(z))`). + @usableFromInline @inline(__always) + internal var magnitude: Scalar { + max() + } + + /// The Euclidean norm (a.k.a. 2-norm, `sqrt(x*x + y*y + z*z)`). + @usableFromInline @inline(__always) + internal var length: Scalar { + let naive = lengthSquared + guard naive.isNormal else { return carefulLength } + return naive.squareRoot() + } + + // Implementation detail of `length`, moving slow path off of the + // inline function. + @usableFromInline + internal var carefulLength: Scalar { + guard isFinite else { return .infinity } + guard !magnitude.isZero else { return .zero } + // Unscale the vector, calculate its length and rescale the result + return (self / magnitude).length * magnitude + } + + /// Returns the squared length of this instance. + @usableFromInline @inline(__always) + internal var lengthSquared: Scalar { + dot(self) + } + + /// Returns the scalar/dot product of this vector with `other`. + @usableFromInline @inline(__always) + internal func dot(_ other: SIMD3) -> Scalar { + (self * other).sum() + } + + /// Returns the vector/cross product of this vector with `other`. + @usableFromInline @inline(__always) + internal func cross(_ other: SIMD3) -> SIMD3 { + let yzx = SIMD3(1,2,0) + let zxy = SIMD3(2,0,1) + return (self[yzx] * other[zxy]) - (self[zxy] * other[yzx]) + } +} diff --git a/Sources/QuaternionModule/Transformation.swift b/Sources/QuaternionModule/Transformation.swift index 0089901..acd5e19 100644 --- a/Sources/QuaternionModule/Transformation.swift +++ b/Sources/QuaternionModule/Transformation.swift @@ -381,7 +381,7 @@ extension Quaternion { /// [wiki]: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Using_quaternion_as_rotations @inlinable public func act(on vector: SIMD3) -> SIMD3 { - guard vector.isFinite else { return SIMD3(repeating: .infinity) } + guard vector.isFinite else { return .infinity } guard vector != .zero else { return .zero } // The following expression have been split up so the type-checker @@ -452,36 +452,3 @@ extension Quaternion { self.init(real: .cos(halfAngle), imaginary: unitAxis * .sin(halfAngle)) } } - -// MARK: - SIMD Helper -// -// Provides common vector operations on SIMD3 to ease the use of "imaginary" -// and *(x,y,z)* axis representations internally to the module. -extension SIMD3 where Scalar: FloatingPoint { - - /// True if all values of this instance are finite - @usableFromInline @inline(__always) - internal var isFinite: Bool { - x.isFinite && y.isFinite && z.isFinite - } - - /// Returns the squared length of this instance. - @usableFromInline @inline(__always) - internal var lengthSquared: Scalar { - dot(self) - } - - /// Returns the scalar/dot product of this vector with `other`. - @usableFromInline @inline(__always) - internal func dot(_ other: SIMD3) -> Scalar { - (self * other).sum() - } - - /// Returns the vector/cross product of this vector with `other`. - @usableFromInline @inline(__always) - internal func cross(_ other: SIMD3) -> SIMD3 { - let yzx = SIMD3(1,2,0) - let zxy = SIMD3(2,0,1) - return (self[yzx] * other[zxy]) - (self[zxy] * other[yzx]) - } -} diff --git a/Tests/QuaternionTests/TransformationTests.swift b/Tests/QuaternionTests/TransformationTests.swift index 6377a37..245ab51 100644 --- a/Tests/QuaternionTests/TransformationTests.swift +++ b/Tests/QuaternionTests/TransformationTests.swift @@ -306,9 +306,9 @@ final class TransformationTests: XCTestCase { let q = Quaternion(angle: .pi, axis: SIMD3(1,0,0)) XCTAssertEqual(q.act(on: .zero), .zero) XCTAssertEqual(q.act(on: -.zero), .zero) - XCTAssertEqual(q.act(on: .nan ), SIMD3(repeating: .infinity)) - XCTAssertEqual(q.act(on: .infinity), SIMD3(repeating: .infinity)) - XCTAssertEqual(q.act(on: -.infinity), SIMD3(repeating: .infinity)) + XCTAssertEqual(q.act(on: .nan ), .infinity) + XCTAssertEqual(q.act(on: .infinity), .infinity) + XCTAssertEqual(q.act(on: -.infinity), .infinity) } func testActOnVectorEdgeCase() { @@ -369,8 +369,6 @@ final class TransformationTests: XCTestCase { // MARK: - Helper extension SIMD3 where Scalar: FloatingPoint { - fileprivate static var infinity: Self { SIMD3(.infinity,0,0) } - fileprivate static var nan: Self { SIMD3(.nan,0,0) } fileprivate var isNaN: Bool { x.isNaN && y.isNaN && z.isNaN } }