import simd /// Maps a horizontal drag on the paper view to the parameter of a `turnOver` or `rotate` step: the /// drag's rightward distance over the model's on-screen width. A leftward drag turns it back. /// Screen points are UIKit's, in a viewport of `viewportSize`. public struct ReorientMapping: Sendable { /// The width in points of the projected bounding box of `positions`, at least 44 points. public let width: Double private static let minimumWidth = 44.0 /// `positions` are the model's vertices in the frame the step starts from. public init(positions: [SIMD3], camera: Camera, viewportSize: SIMD2) { let viewProjection = camera.viewProjection(aspect: viewportSize.x / viewportSize.y) let xs = positions.map { p in let clip = viewProjection * SIMD4(p, 1) return (clip.x / clip.w + 1) / 2 * viewportSize.x } width = max(xs.max()! - xs.min()!, Self.minimumWidth) } /// The parameter, in 0...1, after a drag of `translation` points rightward from where the /// parameter was `current`. public func parameter(translation: Double, current: Double) -> Double { min(max(current + translation / width, 0), 1) } }