|
| 1 | +/* |
| 2 | + * Copyright © 2025 Dustin Collins (Strega's Gate) |
| 3 | + * All Rights Reserved. |
| 4 | + * |
| 5 | + * http://stregasgate.com |
| 6 | + */ |
| 7 | + |
| 8 | +/// Common opperations used on box types for surface calculations |
| 9 | +public protocol Rect3nSurfaceMath { |
| 10 | + typealias ScalarType = Vector3n.ScalarType |
| 11 | + associatedtype Scalar: ScalarType |
| 12 | + |
| 13 | + var center: Position3n<Scalar> { get } |
| 14 | + var radius: Size3n<Scalar> { get } |
| 15 | +} |
| 16 | + |
| 17 | +public extension Rect3nSurfaceMath { |
| 18 | + var minPosition: Position3n<Scalar> { |
| 19 | + return center - radius |
| 20 | + } |
| 21 | + |
| 22 | + var maxPosition: Position3n<Scalar> { |
| 23 | + return center + radius |
| 24 | + } |
| 25 | + |
| 26 | + var size: Size3n<Scalar> { |
| 27 | + return radius * 2 |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +public extension Rect3nSurfaceMath where Scalar: Comparable { |
| 32 | + /** |
| 33 | + Locates a position on the surface of this box that is as close to the given point as possible. |
| 34 | + - parameter position: A point in space to use as an reference |
| 35 | + - returns: The point on the box's surface that is nearest to `p` |
| 36 | + */ |
| 37 | + func nearestSurfacePosition(to position: Position3n<Scalar>) -> Position3n<Scalar> { |
| 38 | + let minPosition: Position3n<Scalar> = self.minPosition |
| 39 | + let maxPosition: Position3n<Scalar> = self.maxPosition |
| 40 | + |
| 41 | + var result: Position3n<Scalar> = .zero |
| 42 | + |
| 43 | + for i in 0 ..< 3 { |
| 44 | + var v: Scalar = position[i] |
| 45 | + let min: Scalar = minPosition[i] |
| 46 | + if v < min { |
| 47 | + v = min // v = max(v, b.min[i]) |
| 48 | + } |
| 49 | + let max: Scalar = maxPosition[i] |
| 50 | + if v > max { |
| 51 | + v = max // v = min(v, b.max[i]) |
| 52 | + } |
| 53 | + result[i] = v |
| 54 | + } |
| 55 | + |
| 56 | + return result |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +public extension Rect3nSurfaceMath where Scalar: Comparable { |
| 61 | + @inlinable |
| 62 | + func contains(_ rhs: Position3n<Scalar>) -> Bool { |
| 63 | + let min = self.minPosition |
| 64 | + guard rhs.x >= min.x && rhs.y >= min.y && rhs.z >= min.z else {return false} |
| 65 | + |
| 66 | + let max = self.maxPosition |
| 67 | + guard rhs.x < max.x && rhs.y < max.y && rhs.z < max.z else {return false} |
| 68 | + |
| 69 | + return true |
| 70 | + } |
| 71 | + |
| 72 | + @inlinable |
| 73 | + func contains<T: Rect3nSurfaceMath>(_ otherRect: T) -> Bool where T.Scalar == Scalar { |
| 74 | + guard self.contains(otherRect.minPosition) else {return false} |
| 75 | + guard self.contains(otherRect.maxPosition) else {return false} |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + @inlinable |
| 80 | + func contains(_ rhs: Position3n<Scalar>, withThreshold threshold: Scalar) -> Bool { |
| 81 | + let min = self.minPosition - threshold |
| 82 | + guard rhs.x >= min.x && rhs.y >= min.y && rhs.z >= min.z else {return false} |
| 83 | + |
| 84 | + let max = self.maxPosition + threshold |
| 85 | + guard rhs.x < max.x && rhs.y < max.y && rhs.z < max.z else {return false} |
| 86 | + |
| 87 | + return true |
| 88 | + } |
| 89 | + |
| 90 | + @inlinable |
| 91 | + func contains<T: Rect3nSurfaceMath>(_ otherRect: T, withThreshold threshold: Scalar) -> Bool where T.Scalar == Scalar { |
| 92 | + guard self.contains(otherRect.minPosition, withThreshold: threshold) else {return false} |
| 93 | + guard self.contains(otherRect.maxPosition, withThreshold: threshold) else {return false} |
| 94 | + return true |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// MARK: Ray3nIntersectable |
| 99 | +public extension Rect3nSurfaceMath where Scalar: Ray3nIntersectable.ScalarType { |
| 100 | + func intersection(of ray: Ray3n<Scalar>) -> Position3n<Scalar>? { |
| 101 | + let minPosition: Position3n<Scalar> = self.minPosition |
| 102 | + let maxPosition: Position3n<Scalar> = self.maxPosition |
| 103 | + |
| 104 | + var tmin: Scalar = (minPosition.x - ray.origin.x) / ray.direction.x |
| 105 | + var tmax: Scalar = (maxPosition.x - ray.origin.x) / ray.direction.x |
| 106 | + |
| 107 | + if tmin > tmax { |
| 108 | + Swift.swap(&tmin, &tmax) |
| 109 | + } |
| 110 | + |
| 111 | + var tymin: Scalar = (minPosition.y - ray.origin.y) / ray.direction.y |
| 112 | + var tymax: Scalar = (maxPosition.y - ray.origin.y) / ray.direction.y |
| 113 | + |
| 114 | + if tymin > tymax { |
| 115 | + Swift.swap(&tymin, &tymax) |
| 116 | + } |
| 117 | + |
| 118 | + if tmin > tymax || tymin > tmax { |
| 119 | + return nil |
| 120 | + } |
| 121 | + |
| 122 | + if tymin > tmin { |
| 123 | + tmin = tymin |
| 124 | + } |
| 125 | + |
| 126 | + if tymax < tmax { |
| 127 | + tmax = tymax |
| 128 | + } |
| 129 | + |
| 130 | + var tzmin: Scalar = (minPosition.z - ray.origin.z) / ray.direction.z |
| 131 | + var tzmax: Scalar = (maxPosition.z - ray.origin.z) / ray.direction.z |
| 132 | + |
| 133 | + if tzmin > tzmax { |
| 134 | + Swift.swap(&tzmin, &tzmax) |
| 135 | + } |
| 136 | + |
| 137 | + if tmin > tzmax || tzmin > tmax { |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + if tzmin > tmin { |
| 142 | + tmin = tzmin |
| 143 | + } |
| 144 | + |
| 145 | + if tzmax < tmax { |
| 146 | + tmax = tzmax |
| 147 | + } |
| 148 | + let t: Scalar = Swift.min(tmin, tmax) |
| 149 | + guard t > 0 else {return nil} |
| 150 | + return ray.origin.moved(t, toward: ray.direction) |
| 151 | + } |
| 152 | +} |
0 commit comments