|
| 1 | +public extension Loft { |
| 2 | + /// A result builder for composing loft layers. |
| 3 | + typealias LayerBuilder = ArrayBuilder<Layer> |
| 4 | + |
| 5 | + /// A single cross-section in a lofted shape. |
| 6 | + /// |
| 7 | + /// Each layer defines a 2D shape at a specific Z height. Layers are created using the |
| 8 | + /// `layer(z:interpolation:shape:)` or `layer(zOffset:interpolation:shape:)` functions |
| 9 | + /// within a ``Loft`` builder. |
| 10 | + /// |
| 11 | + struct Layer: Sendable { |
| 12 | + internal enum ZSpecification: Sendable { |
| 13 | + case absolute(Double, upperBound: Double? = nil) |
| 14 | + case offset(Double, upperBound: Double? = nil) |
| 15 | + } |
| 16 | + |
| 17 | + internal let zSpec: ZSpecification |
| 18 | + internal let transition: LayerTransition? |
| 19 | + internal let geometry: @Sendable () -> any Geometry2D |
| 20 | + |
| 21 | + internal init(zSpec: ZSpecification, transition: LayerTransition?, geometry: @Sendable @escaping () -> any Geometry2D) { |
| 22 | + self.zSpec = zSpec |
| 23 | + self.transition = transition |
| 24 | + self.geometry = geometry |
| 25 | + } |
| 26 | + |
| 27 | + internal init(z: Double, transition: LayerTransition?, geometry: @Sendable @escaping () -> any Geometry2D) { |
| 28 | + self.init(zSpec: .absolute(z, upperBound: nil), transition: transition, geometry: geometry) |
| 29 | + } |
| 30 | + |
| 31 | + internal var z: Double { |
| 32 | + guard case .absolute(let z, _) = zSpec else { |
| 33 | + preconditionFailure("Layer Z has not been resolved — use layer(z:) or layer(zOffset:) inside a Loft builder") |
| 34 | + } |
| 35 | + return z |
| 36 | + } |
| 37 | + |
| 38 | + internal func resolved(lastZ: inout Double) -> [Layer] { |
| 39 | + switch zSpec { |
| 40 | + case .absolute(let lower, let upper): |
| 41 | + var result = [Layer(z: lower, transition: transition, geometry: geometry)] |
| 42 | + lastZ = lower |
| 43 | + if let upper { |
| 44 | + result.append(Layer(z: upper, transition: .interpolated(.linear), geometry: geometry)) |
| 45 | + lastZ = upper |
| 46 | + } |
| 47 | + return result |
| 48 | + case .offset(let lower, let upper): |
| 49 | + let baseZ = lastZ |
| 50 | + let lowerZ = baseZ + lower |
| 51 | + var result = [Layer(z: lowerZ, transition: transition, geometry: geometry)] |
| 52 | + lastZ = lowerZ |
| 53 | + if let upper { |
| 54 | + let upperZ = baseZ + upper |
| 55 | + result.append(Layer(z: upperZ, transition: .interpolated(.linear), geometry: geometry)) |
| 56 | + lastZ = upperZ |
| 57 | + } |
| 58 | + return result |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Creates a single layer in a lofted shape at the specified Z height. |
| 65 | +/// This function is intended to be used inside a `Loft` builder to define each horizontal cross-section. |
| 66 | +/// |
| 67 | +/// - Parameters: |
| 68 | +/// - z: The Z height at which to place the 2D shape. |
| 69 | +/// - shapingFunction: An optional shaping function that controls how the transition progresses between |
| 70 | +/// the previous layer and this one. If `nil`, the `Loft`'s own shaping function is used. |
| 71 | +/// - shape: A builder that returns the 2D geometry to use for this layer. |
| 72 | +/// |
| 73 | +public func layer( |
| 74 | + z: Double, |
| 75 | + interpolation shapingFunction: ShapingFunction? = nil, |
| 76 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 77 | +) -> Loft.Layer { |
| 78 | + Loft.Layer(z: z, transition: shapingFunction.map { .interpolated($0) }, geometry: shape) |
| 79 | +} |
| 80 | + |
| 81 | +/// Creates a single layer in a lofted shape at the specified Z height with a specified transition type. |
| 82 | +/// This function is intended to be used inside a `Loft` builder to define each horizontal cross-section. |
| 83 | +/// |
| 84 | +/// - Parameters: |
| 85 | +/// - z: The Z height at which to place the 2D shape. |
| 86 | +/// - transition: The transition type that controls how this layer connects to the previous one. |
| 87 | +/// Use `.interpolated(_:)` for shape interpolation or `.convexHull` for a convex hull connection. |
| 88 | +/// - shape: A builder that returns the 2D geometry to use for this layer. |
| 89 | +/// |
| 90 | +public func layer( |
| 91 | + z: Double, |
| 92 | + interpolation transition: LayerTransition, |
| 93 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 94 | +) -> Loft.Layer { |
| 95 | + Loft.Layer(z: z, transition: transition, geometry: shape) |
| 96 | +} |
| 97 | + |
| 98 | +/// Creates a single layer in a lofted shape at a Z height relative to the previous layer. |
| 99 | +/// |
| 100 | +/// The layer is placed at the Z position of the preceding layer plus the given offset. |
| 101 | +/// This is useful when building up a loft incrementally, where each layer's height |
| 102 | +/// is defined relative to the one before it rather than as an absolute position. |
| 103 | +/// |
| 104 | +/// - Parameters: |
| 105 | +/// - zOffset: The Z distance from the previous layer. Must be positive. |
| 106 | +/// - shapingFunction: An optional shaping function for the transition from the previous layer. |
| 107 | +/// If `nil`, the `Loft`'s own shaping function is used. |
| 108 | +/// - shape: A builder that returns the 2D geometry to use for this layer. |
| 109 | +/// |
| 110 | +public func layer( |
| 111 | + zOffset: Double, |
| 112 | + interpolation shapingFunction: ShapingFunction? = nil, |
| 113 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 114 | +) -> Loft.Layer { |
| 115 | + Loft.Layer(zSpec: .offset(zOffset), transition: shapingFunction.map { .interpolated($0) }, geometry: shape) |
| 116 | +} |
| 117 | + |
| 118 | +/// Creates a single layer in a lofted shape at a Z height relative to the previous layer, |
| 119 | +/// with a specified transition type. |
| 120 | +/// |
| 121 | +/// - Parameters: |
| 122 | +/// - zOffset: The Z distance from the previous layer. Must be positive. |
| 123 | +/// - transition: The transition type that controls how this layer connects to the previous one. |
| 124 | +/// - shape: A builder that returns the 2D geometry to use for this layer. |
| 125 | +/// |
| 126 | +public func layer( |
| 127 | + zOffset: Double, |
| 128 | + interpolation transition: LayerTransition, |
| 129 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 130 | +) -> Loft.Layer { |
| 131 | + Loft.Layer(zSpec: .offset(zOffset), transition: transition, geometry: shape) |
| 132 | +} |
| 133 | + |
| 134 | +/// Creates two layers spanning an offset range using the same 2D shape. |
| 135 | +/// |
| 136 | +/// This convenience overload generates a pair of `Loft.Layer` entries from a single shape: |
| 137 | +/// one at `previous + range.lowerBound` and one at `previous + range.upperBound`, both using |
| 138 | +/// the same shape. This is useful when you want a straight shape across the specified interval, |
| 139 | +/// defined relative to the previous layer rather than at an absolute Z position. |
| 140 | +/// |
| 141 | +/// - Parameters: |
| 142 | +/// - range: The Z offset range relative to the previous layer. |
| 143 | +/// - shapingFunction: An optional shaping function that controls how the transition progresses between |
| 144 | +/// the previous layer and the lower bound of this range. If `nil`, the `Loft`'s shaping |
| 145 | +/// function is used for the first layer. |
| 146 | +/// - shape: A builder that returns the 2D geometry to use for both layers. |
| 147 | +/// - Returns: Two `Loft.Layer` values, one at the lower bound offset and one at the upper bound offset. |
| 148 | +/// |
| 149 | +public func layer( |
| 150 | + zOffset range: Range<Double>, |
| 151 | + interpolation shapingFunction: ShapingFunction? = nil, |
| 152 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 153 | +) -> Loft.Layer { |
| 154 | + Loft.Layer(zSpec: .offset(range.lowerBound, upperBound: range.upperBound), transition: shapingFunction.map { .interpolated($0) }, geometry: shape) |
| 155 | +} |
| 156 | + |
| 157 | +/// Creates two layers spanning an offset range using the same 2D shape with a specified transition type. |
| 158 | +/// |
| 159 | +/// - Parameters: |
| 160 | +/// - range: The Z offset range relative to the previous layer. |
| 161 | +/// - transition: The transition type that controls how this layer connects to the previous one. |
| 162 | +/// - shape: A builder that returns the 2D geometry to use for both layers. |
| 163 | +/// |
| 164 | +public func layer( |
| 165 | + zOffset range: Range<Double>, |
| 166 | + interpolation transition: LayerTransition, |
| 167 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 168 | +) -> Loft.Layer { |
| 169 | + Loft.Layer(zSpec: .offset(range.lowerBound, upperBound: range.upperBound), transition: transition, geometry: shape) |
| 170 | +} |
| 171 | + |
| 172 | +/// Creates two layers spanning a Z range using the same 2D shape. |
| 173 | +/// |
| 174 | +/// This convenience overload generates a pair of `Loft.Layer` entries from a single shape: |
| 175 | +/// one at `range.lowerBound` using the provided `shapingFunction` (or the `Loft` default if `nil`), |
| 176 | +/// and one at `range.upperBound` using a linear shaping function. This is useful when you want a |
| 177 | +/// straight shape across the specified interval. |
| 178 | +/// |
| 179 | +/// - Parameters: |
| 180 | +/// - range: The Z range defining the lower and upper bounds where the shape will be placed. |
| 181 | +/// - shapingFunction: An optional shaping function that controls how the transition progresses between |
| 182 | +/// the previous layer and the lower bound of this range. If `nil`, the `Loft`'s shaping |
| 183 | +/// function is used for the first layer. |
| 184 | +/// - shape: A builder that returns the 2D geometry to use for both layers. |
| 185 | +/// - Returns: Two `Loft.Layer` values, one at the lower bound and one at the upper bound. |
| 186 | +/// |
| 187 | +public func layer( |
| 188 | + z range: Range<Double>, |
| 189 | + interpolation shapingFunction: ShapingFunction? = nil, |
| 190 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 191 | +) -> Loft.Layer { |
| 192 | + Loft.Layer(zSpec: .absolute(range.lowerBound, upperBound: range.upperBound), transition: shapingFunction.map { .interpolated($0) }, geometry: shape) |
| 193 | +} |
| 194 | + |
| 195 | +/// Creates two layers spanning a Z range using the same 2D shape with a specified transition type. |
| 196 | +/// |
| 197 | +/// This convenience overload generates a pair of `Loft.Layer` entries from a single shape: |
| 198 | +/// one at `range.lowerBound` using the provided transition, and one at `range.upperBound` using |
| 199 | +/// a linear interpolation. This is useful when you want a straight shape across the specified interval. |
| 200 | +/// |
| 201 | +/// - Parameters: |
| 202 | +/// - range: The Z range defining the lower and upper bounds where the shape will be placed. |
| 203 | +/// - transition: The transition type that controls how this layer connects to the previous one. |
| 204 | +/// Use `.interpolated(_:)` for shape interpolation or `.convexHull` for a convex hull connection. |
| 205 | +/// - shape: A builder that returns the 2D geometry to use for this layer. |
| 206 | +/// |
| 207 | +public func layer( |
| 208 | + z range: Range<Double>, |
| 209 | + interpolation transition: LayerTransition, |
| 210 | + @GeometryBuilder2D shape: @Sendable @escaping () -> any Geometry2D |
| 211 | +) -> Loft.Layer { |
| 212 | + Loft.Layer(zSpec: .absolute(range.lowerBound, upperBound: range.upperBound), transition: transition, geometry: shape) |
| 213 | +} |
0 commit comments