| @@ -0,0 +1,132 @@ |
| |
1 | import FoldFormat |
| |
2 | import simd |
| |
3 | |
| |
4 | /// Two faces that pass through each other. `t` is the first sample, `k / samples`, at which they |
| |
5 | /// do; `faceA < faceB`. |
| |
6 | public struct Intersection: Equatable, Sendable { |
| |
7 | public let faceA: Int |
| |
8 | public let faceB: Int |
| |
9 | public let t: Double |
| |
10 | |
| |
11 | public init(faceA: Int, faceB: Int, t: Double) { |
| |
12 | self.faceA = faceA |
| |
13 | self.faceB = faceB |
| |
14 | self.t = t |
| |
15 | } |
| |
16 | } |
| |
17 | |
| |
18 | /// Contact this close to a face's boundary does not count. |
| |
19 | private let contactTolerance = 1e-9 |
| |
20 | /// Coplanar faces: normals within this many radians (as lines), planes within this fraction of |
| |
21 | /// the sheet's size. |
| |
22 | private let coplanarAngle = 1e-6 |
| |
23 | private let coplanarOffset = 1e-6 |
| |
24 | |
| |
25 | /// The pairs of faces that interpenetrate at some sample `t = k / samples`, `k` in 0...samples, |
| |
26 | /// each reported once at the first such sample. |
| |
27 | /// |
| |
28 | /// Faces are convex. A pair interpenetrates when the line where their planes meet cuts both and |
| |
29 | /// the stretch common to both has its midpoint more than 1e-9 inside every edge of each. Contact |
| |
30 | /// along an edge or at a vertex, shared or coincident, does not count, but pairs that share a |
| |
31 | /// vertex or an edge are tested. Coplanar pairs are skipped; their overlap is the layer order's. |
| |
32 | /// The sheet's size is the longest side of the bounding box of the positions at `t = 0`. |
| |
33 | public func interpenetrations(_ interpolator: FoldInterpolator, faces: [[Int]], samples: Int) -> [Intersection] { |
| |
34 | precondition(samples > 0, "interpenetrations needs at least one sample") |
| |
35 | let size = boundingExtent(interpolator.positions(at: 0)).max() |
| |
36 | let usable = faces.indices.filter { faces[$0].count >= 3 } |
| |
37 | |
| |
38 | var found: [Intersection] = [] |
| |
39 | var reported = Set<SIMD2<Int>>() |
| |
40 | for k in 0...samples { |
| |
41 | let t = Double(k) / Double(samples) |
| |
42 | let positions = interpolator.positions(at: t) |
| |
43 | let normals = faceNormals(positions: positions, faces: faces) |
| |
44 | let polygons = faces.map { $0.map { positions[$0] } } |
| |
45 | let boxes = polygons.map(bounds) |
| |
46 | |
| |
47 | for (n, a) in usable.enumerated() where normals[a] != .zero { |
| |
48 | for b in usable[(n + 1)...] where normals[b] != .zero { |
| |
49 | let pair = SIMD2(a, b) |
| |
50 | guard !reported.contains(pair), boxesMeet(boxes[a], boxes[b]), |
| |
51 | !coplanar(polygons[a], normals[a], polygons[b], normals[b], size: size), |
| |
52 | passThrough(polygons[a], normals[a], polygons[b], normals[b]) |
| |
53 | else { continue } |
| |
54 | reported.insert(pair) |
| |
55 | found.append(Intersection(faceA: a, faceB: b, t: t)) |
| |
56 | } |
| |
57 | } |
| |
58 | } |
| |
59 | return found |
| |
60 | } |
| |
61 | |
| |
62 | private typealias Box = (lower: SIMD3<Double>, upper: SIMD3<Double>) |
| |
63 | |
| |
64 | private func bounds(_ points: [SIMD3<Double>]) -> Box { |
| |
65 | guard let first = points.first else { return (.zero, .zero) } |
| |
66 | return points.dropFirst().reduce((first, first)) { (simd_min($0.0, $1), simd_max($0.1, $1)) } |
| |
67 | } |
| |
68 | |
| |
69 | private func boundingExtent(_ points: [SIMD3<Double>]) -> SIMD3<Double> { |
| |
70 | let box = bounds(points) |
| |
71 | return box.upper - box.lower |
| |
72 | } |
| |
73 | |
| |
74 | /// Whether the boxes, grown by the contact tolerance, meet. Faces whose boxes do not meet cannot |
| |
75 | /// pass through each other. |
| |
76 | private func boxesMeet(_ a: Box, _ b: Box) -> Bool { |
| |
77 | all(a.lower - contactTolerance .<= b.upper) && all(b.lower - contactTolerance .<= a.upper) |
| |
78 | } |
| |
79 | |
| |
80 | private func coplanar(_ a: [SIMD3<Double>], _ na: SIMD3<Double>, _ b: [SIMD3<Double>], _ nb: SIMD3<Double>, |
| |
81 | size: Double) -> Bool { |
| |
82 | guard length(cross(na, nb)) <= coplanarAngle else { return false } |
| |
83 | let centre = b.reduce(.zero, +) / Double(b.count) |
| |
84 | return abs(dot(na, centre - a[0])) <= coplanarOffset * size |
| |
85 | } |
| |
86 | |
| |
87 | /// The inward unit normal of each edge of a counterclockwise polygon, in its plane, with a corner |
| |
88 | /// on that edge. |
| |
89 | private func edgeInwardNormals(_ polygon: [SIMD3<Double>], _ normal: SIMD3<Double>) |
| |
90 | -> [(corner: SIMD3<Double>, inward: SIMD3<Double>)] { |
| |
91 | polygon.indices.compactMap { i in |
| |
92 | let corner = polygon[i], edge = polygon[(i + 1) % polygon.count] - corner |
| |
93 | let size = length(edge) |
| |
94 | return size > 0 ? (corner, cross(normal, edge) / size) : nil |
| |
95 | } |
| |
96 | } |
| |
97 | |
| |
98 | /// The interval of `s` for which `p + s·d` lies in the polygon, `p` and `d` being in its plane. |
| |
99 | private func clip(_ edges: [(corner: SIMD3<Double>, inward: SIMD3<Double>)], _ p: SIMD3<Double>, |
| |
100 | _ d: SIMD3<Double>) -> ClosedRange<Double>? { |
| |
101 | var low = -Double.infinity, high = Double.infinity |
| |
102 | for (corner, inward) in edges { |
| |
103 | let distance = dot(inward, p - corner), rate = dot(inward, d) |
| |
104 | if abs(rate) < 1e-15 { |
| |
105 | if distance < 0 { return nil } |
| |
106 | continue |
| |
107 | } |
| |
108 | let s = -distance / rate |
| |
109 | if rate > 0 { low = max(low, s) } else { high = min(high, s) } |
| |
110 | } |
| |
111 | return low <= high ? low...high : nil |
| |
112 | } |
| |
113 | |
| |
114 | private func passThrough(_ a: [SIMD3<Double>], _ na: SIMD3<Double>, _ b: [SIMD3<Double>], _ nb: SIMD3<Double>) -> Bool { |
| |
115 | let along = cross(na, nb) |
| |
116 | let squared = dot(along, along) |
| |
117 | guard squared > 0 else { return false } |
| |
118 | let d = along / sqrt(squared) |
| |
119 | // a point on both planes |
| |
120 | let p = (dot(na, a[0]) * cross(nb, along) + dot(nb, b[0]) * cross(along, na)) / squared |
| |
121 | |
| |
122 | let edgesA = edgeInwardNormals(a, na), edgesB = edgeInwardNormals(b, nb) |
| |
123 | guard let inA = clip(edgesA, p, d), let inB = clip(edgesB, p, d) else { return false } |
| |
124 | let low = max(inA.lowerBound, inB.lowerBound), high = min(inA.upperBound, inB.upperBound) |
| |
125 | guard low < high else { return false } |
| |
126 | |
| |
127 | // A convex chord whose midpoint is on a face's boundary lies on it, so the midpoint decides. |
| |
128 | let middle = p + d * ((low + high) / 2) |
| |
129 | return [edgesA, edgesB].allSatisfy { edges in |
| |
130 | edges.allSatisfy { dot($0.inward, middle - $0.corner) > contactTolerance } |
| |
131 | } |
| |
132 | } |