Skip to content

Commit

Permalink
updates to resolve swift6 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Jun 20, 2024
1 parent 4798c18 commit 610c6f0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.8
5.9
7 changes: 3 additions & 4 deletions Sources/Lindenmayer/Modules/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ public extension Module {
public extension Module {
/// A string representation of the module.
var description: String {
let resovledName: String
if name != "" {
resovledName = name
let resovledName: String = if name != "" {
name
} else {
resovledName = String(describing: type(of: self))
String(describing: type(of: self))
}
return resovledName
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/Lindenmayer/Rendering/SceneKitRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SceneKit
import SceneKitDebugTools
import simd

@MainActor
struct GrowthState {
var transform: simd_float4x4
var nodeRef: SCNNode
Expand Down Expand Up @@ -78,9 +79,10 @@ extension ColorRepresentation {
/// - ``rotationAroundZAxisTransform(angle:)``
///

@MainActor
public struct SceneKitRenderer {
/// Creates a new SceneKit rendering engine for L-systems.
public init() {}
public nonisolated init() {}

func rotateAroundHeadingToVertical(_ full_transform: simd_float4x4) -> Float {
// The interpretation of this symbol is a tricky beast. From pg 41 of
Expand Down Expand Up @@ -250,6 +252,7 @@ public struct SceneKitRenderer {
// print("Added cylinder (r=\(cmd.radius)) by \(cmd.length) at \(String(describing: node.simdTransform))")
// print("Moving +y by \(cmd.length) -> \(String(describing: currentState.transform))")
}

case TurtleCodes.cone.rawValue:
if let cmd = cmd as? RenderCommand.Cone {
let node = SCNNode(geometry: SCNCone(topRadius: cmd.radiusTop, bottomRadius: cmd.radiusBottom, height: cmd.length))
Expand All @@ -272,6 +275,7 @@ public struct SceneKitRenderer {
// print("Added cone (tr=\(cmd.radiusTop), br=\(cmd.radiusBottom) by \(cmd.length) at \(String(describing: node.simdTransform))")
// print("Moving +y by \(cmd.length) -> \(String(describing: currentState.transform))")
}

case TurtleCodes.sphere.rawValue:
if let cmd = cmd as? RenderCommand.Sphere {
let node = SCNNode(geometry: SCNSphere(radius: cmd.radius))
Expand All @@ -288,6 +292,7 @@ public struct SceneKitRenderer {
// print("Added sphere (r=\(cmd.radius)) at \(String(describing: node.simdTransform))")
// print("Moving +y by \(radius) -> \(String(describing: currentState.transform))")
}

default: // ignore
break
} // switch cmd.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SceneKitDebugTools
import simd
import SwiftUI

@MainActor
public struct RollToVerticalTestView: View {
let scene: SCNScene
let pointSphere0: SCNNode
Expand Down
12 changes: 6 additions & 6 deletions Sources/LindenmayerViews/Dynamic2DLSystemViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public struct Dynamic2DLSystemViews: View {
public var lsystem: LindenmayerSystem {
switch self {
case .algae:
return Examples2D.algae
Examples2D.algae
case .sierpinskiTriangle:
return Examples2D.sierpinskiTriangle
Examples2D.sierpinskiTriangle
case .kochCurve:
return Examples2D.kochCurve
Examples2D.kochCurve
case .dragonCurve:
return Examples2D.dragonCurve
Examples2D.dragonCurve
case .fractalTree:
return Examples2D.fractalTree
Examples2D.fractalTree
case .barnsleyFern:
return Examples2D.barnsleyFern
Examples2D.barnsleyFern
}
}
}
Expand Down
26 changes: 16 additions & 10 deletions Sources/LindenmayerViews/ViewComponents/StateSelectorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// Created by Joseph Heck on 1/10/22.
//

import Combine
import Lindenmayer
@preconcurrency import SwiftUI
import SwiftUI

/// A view that provides a visual representation of the states of an L-system and allows the person viewing it to select an index position from that L-system's state.
@available(macOS 12.0, iOS 15.0, *)
Expand All @@ -25,6 +26,8 @@ public struct StateSelectorView: View {
@State private var isLongPressingForward: Bool = false
@State private var forwardTimer: Timer?

let indexJumpPublisher: PassthroughSubject<Int, Never> = PassthroughSubject()

public var body: some View {
VStack {
ScrollViewReader { proxy in
Expand All @@ -37,14 +40,15 @@ public struct StateSelectorView: View {
if indexPosition < system.state.count - 1 {
indexPosition += 1
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
}
} label: {
Image(systemName: "plus.square")
}
Button {
if indexPosition > 1 {
indexPosition -= 1
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
}
} label: {
Image(systemName: "minus.square")
Expand All @@ -58,7 +62,7 @@ public struct StateSelectorView: View {
step: 1,
onEditingChanged: { _ in
indexPosition = Int(sliderPosition)
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
})
.onChange(of: indexPosition) { newValue in
sliderPosition = Double(newValue)
Expand All @@ -77,7 +81,6 @@ public struct StateSelectorView: View {
if sliderPosition >= 1.0 {
sliderPosition -= 1
indexPosition -= 1
proxy.scrollTo(indexPosition)
}
}
} label: {
Expand All @@ -93,7 +96,7 @@ public struct StateSelectorView: View {
if sliderPosition >= 1.0 {
sliderPosition -= 1
indexPosition -= 1
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
}
}
} label: {
Expand All @@ -114,7 +117,7 @@ public struct StateSelectorView: View {
if sliderPosition >= 1.0 {
sliderPosition -= 1
indexPosition -= 1
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
}
}
}
Expand All @@ -135,7 +138,7 @@ public struct StateSelectorView: View {
if sliderPosition < Double(system.state.count - 1) {
sliderPosition += 1
indexPosition += 1
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
}
}
} label: {
Expand All @@ -151,7 +154,7 @@ public struct StateSelectorView: View {
if sliderPosition < Double(system.state.count - 1) {
sliderPosition += 1
indexPosition += 1
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
}
}
} label: {
Expand All @@ -172,7 +175,7 @@ public struct StateSelectorView: View {
if sliderPosition < Double(system.state.count - 1) {
sliderPosition += 1
indexPosition += 1
proxy.scrollTo(indexPosition)
indexJumpPublisher.send(indexPosition)
}
}
}
Expand All @@ -181,10 +184,13 @@ public struct StateSelectorView: View {
.keyboardShortcut(KeyboardShortcut(.rightArrow))
#endif
}
.onReceive(indexJumpPublisher) { indexPosition in
proxy.scrollTo(indexPosition)
}
if _withDetailView {
ModuleDetailView(module: system.state(at: indexPosition))
}
}
} // proxy
}
}

Expand Down
21 changes: 20 additions & 1 deletion Tests/LindenmayerTests/RollUpToVerticalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ final class RollUpToVerticalTests: XCTestCase {
simd_float4(4.4409075, 13.833499, 8.220964, 1.0)
)

static var renderer = SceneKitRenderer()
static let renderer = SceneKitRenderer()

@MainActor
func testMatchingEulerAngles() throws {
let node = SCNNode()
node.simdTransform = RollUpToVerticalTests.transform_119
Expand All @@ -32,6 +33,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(node.simdEulerAngles.z, -3.031, accuracy: 0.001) // roll
}

@MainActor
func testApplyingAffineTransformsToA3DPoint() throws {
let pt = simd_float4(0, 0, 1, 1) // x=0, y=0, z=1
let result = matrix_multiply(RollUpToVerticalTests.transform_119, pt)
Expand All @@ -55,6 +57,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(result3.z, result.z - RollUpToVerticalTests.transform_119.columns.3.z, accuracy: 0.001)
}

@MainActor
func testApplyingKnownTransformsToA3DPoint() throws {
let pt = simd_float3(0, 0, 1) // x=0, y=0, z=1
let rotate_90_around_Y = SceneKitRenderer.rotationAroundYAxisTransform(angle: Angle(degrees: 90)).rotationTransform
Expand Down Expand Up @@ -86,6 +89,7 @@ final class RollUpToVerticalTests: XCTestCase {
// print(matrix_identity_float4x4.prettyPrintString())
}

@MainActor
func testHeadingVectorRotating45degToRight() throws {
// This rotation is a "right turn" from the starting position, straight up. We rotation 45° around
// the Z axis - negative rotation to make it to the right.
Expand All @@ -102,6 +106,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(heading_vector.z, 0, accuracy: 0.0001)
}

@MainActor
func testHeadingVectorRotating45degToLeft() throws {
// This rotation is a "right turn" from the starting position, straight up.
// We rotate 45° around the Z axis - negative rotation to make it to the right.
Expand All @@ -117,6 +122,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(heading_vector.z, 0, accuracy: 0.0001)
}

@MainActor
func testHeadingVectorRotating45degPitchDown() throws {
// This rotation is a "pitch up" from the starting position, straight up.
// We rotate 45° around the X axis - negative rotation to make it pitch 'down'.
Expand All @@ -128,6 +134,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(heading_vector.z, -0.7071067, accuracy: 0.0001)
}

@MainActor
func testHeadingVectorRotating45degPitchUp() throws {
// This rotation is a "pitch down" from the starting position, straight up.
// We rotate 45° around the X axis - positive rotation to make it to pitch 'up'.
Expand All @@ -138,6 +145,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(heading_vector.z, 0.7071067, accuracy: 0.0001)
}

@MainActor
func testHeadingVectorRotating45degYaw() throws {
// This rotation is a "pitch down" from the starting position, straight up.
// We rotate 45° around the X axis - positive rotation to make it to pitch 'up'.
Expand All @@ -148,6 +156,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(heading_vector.z, 0, accuracy: 0.0001)
}

@MainActor
func testUpVectorAfterRotation() throws {
let original_up_vector = simd_float3(x: 0, y: 0, z: 1)
// roll to the right
Expand All @@ -160,6 +169,7 @@ final class RollUpToVerticalTests: XCTestCase {

// MARK: - testing rotation angle calculations

@MainActor
func testRotatingIdentityMatrix() throws {
// Identity state vector indicates we've not moved from start, so the forward vector is +Y and the
// up vector is +Z. No amount of rotation will do us any good, since the plane we're rotating on (the X-Z plane)
Expand All @@ -169,6 +179,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(0, rotation_on_XZ_plane)
}

@MainActor
func testRotating45degToRightMatrix() throws {
// This rotation is a "right turn" from the starting position, straight up. We rotation 45° around
// the Z axis - negative rotation to make it to the right.
Expand All @@ -180,6 +191,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(Float.pi / 2, rotation_on_plane, accuracy: 0.0001)
}

@MainActor
func testRotating45degToLeftMatrix() throws {
// This rotation is a "right turn" from the starting position, straight up. We rotation 45° around
// the Z axis - negative rotation to make it to the right.
Expand All @@ -191,6 +203,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(-Float.pi / 2, rotation_on_plane, accuracy: 0.0001)
}

@MainActor
func testRotating45degPitchUpMatrix() throws {
// This rotation is a "pitch up" from the starting position, straight up. We rotate 45° around
// the X axis - negative rotation to make it pitch 'up'.
Expand All @@ -202,6 +215,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(Float.pi, rotation_on_plane, accuracy: 0.0001)
}

@MainActor
func testRotating45degPitchDownMatrix() throws {
// This rotation is a "pitch down" from the starting position, straight up. We rotate 45° around
// the X axis - positive rotation to make it to pitch 'down'.
Expand All @@ -213,6 +227,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(0, rotation_on_plane, accuracy: 0.0001)
}

@MainActor
func testRotating90degPitchUpMatrix() throws {
// Since we start facing straight up, pitching up another 90° results in us facing the +z direction
// with the "up" vector now rotated to point pretty much straight in the -Y direction.
Expand All @@ -223,6 +238,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(Float.pi, abs(rotation_on_plane), accuracy: 0.0001)
}

@MainActor
func testRotating90degPitchDownMatrix() throws {
// Since we start facing straight up, pitching up another 90° results in us facing the +z direction
// with the "up" vector now rotated to point pretty much straight in the -Y direction.
Expand All @@ -232,6 +248,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(0, rotation_on_plane, accuracy: 0.0001)
}

@MainActor
func testRotating90degToRight() throws {
// Since we start facing straight up, pitching up another 90° results in us facing the +z direction
// with the "up" vector now rotated to point pretty much straight in the -Y direction.
Expand All @@ -241,6 +258,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(Float.pi / 2, rotation_on_plane, accuracy: 0.0001)
}

@MainActor
func testRotating90degToLeft() throws {
// Since we start facing straight up, pitching up another 90° results in us facing the +z direction
// with the "up" vector now rotated to point pretty much straight in the -Y direction.
Expand All @@ -250,6 +268,7 @@ final class RollUpToVerticalTests: XCTestCase {
XCTAssertEqual(-Float.pi / 2, rotation_on_plane, accuracy: 0.0001)
}

@MainActor
func rotationToVerticalTestCodeOriginal(_ full_transform: simd_float4x4) throws -> Float {
// NOTE(heckj): Leaving this here - but the implementation doesn't pass the tests
// because it's not returning the correct "rotation angle" (positive/negative).
Expand Down
Loading

0 comments on commit 610c6f0

Please sign in to comment.