Commit 6bcf7bcc35
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/Notation/EdgeOcclusion.swift +45 −7
| @@ -6,9 +6,10 @@ import simd | |||
| 6 | /// union of the faces above either of its faces in `frame`. | 6 | /// union of the faces above either of its faces in `frame`. |
| 7 | /// | 7 | /// |
| 8 | /// A face is above an edge's face when it follows that face in their cluster's back-to-front list, | 8 | /// A face is above an edge's face when it follows that face in their cluster's back-to-front list, |
| 9 | /// `order.clusters` read looking along −`eye`; faces in other clusters do not occlude. An edge on | 9 | /// `order.clusters` read looking along −`eye`. A face in neither of their clusters is above the |
| 10 | /// the outer boundary of the union stays visible; one under the seam between two upper faces, | 10 | /// edge where it lies nearer the eye; a face seen edge-on covers nothing. An edge on the outer |
| 11 | /// covered on both sides, does not. | 11 | /// boundary of the union stays visible; one under the seam between two upper faces, covered on |
| 12 | /// both sides, does not. | ||
| 12 | public func visibleSegments(of edge: Int, frame: FoldFrame, file: FoldFile, order: LayerOrder, | 13 | public func visibleSegments(of edge: Int, frame: FoldFrame, file: FoldFile, order: LayerOrder, |
| 13 | viewpoint: Viewpoint) -> [(SIMD2<Double>, SIMD2<Double>)] { | 14 | viewpoint: Viewpoint) -> [(SIMD2<Double>, SIMD2<Double>)] { |
| 14 | Occlusion(frame: frame, file: file, order: order, viewpoint: viewpoint).visibleSegments(of: edge) | 15 | Occlusion(frame: frame, file: file, order: order, viewpoint: viewpoint).visibleSegments(of: edge) |
| @@ -39,6 +40,13 @@ struct Occlusion { | |||
| 39 | let outlines: [[SIMD2<Double>]] | 40 | let outlines: [[SIMD2<Double>]] |
| 40 | /// Each face's followers in its cluster. | 41 | /// Each face's followers in its cluster. |
| 41 | let above: [[Int]] | 42 | let above: [[Int]] |
| 43 | /// Each face's cluster. | ||
| 44 | let cluster: [Int] | ||
| 45 | let positions: [SIMD3<Double>] | ||
| 46 | let facesVertices: [[Int]] | ||
| 47 | let normals: [SIMD3<Double>] | ||
| 48 | /// The unit vector toward the eye. | ||
| 49 | let eye: SIMD3<Double> | ||
| 42 | let tolerance: Double | 50 | let tolerance: Double |
| 43 | 51 | ||
| 44 | init(frame: FoldFrame, file: FoldFile, order: LayerOrder, viewpoint: Viewpoint) { | 52 | init(frame: FoldFrame, file: FoldFile, order: LayerOrder, viewpoint: Viewpoint) { |
| @@ -52,14 +60,21 @@ struct Occlusion { | |||
| 52 | return signedArea(outline) < 0 ? outline.reversed() : outline | 60 | return signedArea(outline) < 0 ? outline.reversed() : outline |
| 53 | } | 61 | } |
| 54 | var above = Array(repeating: [Int](), count: file.facesVertices.count) | 62 | var above = Array(repeating: [Int](), count: file.facesVertices.count) |
| 63 | var cluster = Array(repeating: 0, count: file.facesVertices.count) | ||
| 55 | let clusters = order.clusters(positions: frame.verticesCoords, faces: file.facesVertices, | 64 | let clusters = order.clusters(positions: frame.verticesCoords, faces: file.facesVertices, |
| 56 | viewDirection: -viewpoint.eye) | 65 | viewDirection: -viewpoint.eye) |
| 57 | for cluster in clusters { | 66 | for (k, members) in clusters.enumerated() { |
| 58 | for (i, face) in cluster.faces.enumerated() { | 67 | for (i, face) in members.faces.enumerated() { |
| 59 | above[face] = Array(cluster.faces[(i + 1)...]) | 68 | above[face] = Array(members.faces[(i + 1)...]) |
| 69 | cluster[face] = k | ||
| 60 | } | 70 | } |
| 61 | } | 71 | } |
| 62 | self.above = above | 72 | self.above = above |
| 73 | self.cluster = cluster | ||
| 74 | positions = frame.verticesCoords | ||
| 75 | facesVertices = file.facesVertices | ||
| 76 | normals = faceNormals(positions: frame.verticesCoords, faces: file.facesVertices) | ||
| 77 | eye = normalize(viewpoint.eye) | ||
| 63 | let low = points.reduce(points.first ?? .zero, simd_min) | 78 | let low = points.reduce(points.first ?? .zero, simd_min) |
| 64 | let high = points.reduce(points.first ?? .zero, simd_max) | 79 | let high = points.reduce(points.first ?? .zero, simd_max) |
| 65 | tolerance = max(length(high - low), 1) * 1e-9 | 80 | tolerance = max(length(high - low), 1) * 1e-9 |
| @@ -73,7 +88,13 @@ struct Occlusion { | |||
| 73 | let (a, b) = endpoints(of: edge) | 88 | let (a, b) = endpoints(of: edge) |
| 74 | guard length(b - a) > tolerance else { return [] } | 89 | guard length(b - a) > tolerance else { return [] } |
| 75 | let occluders = Set(edgeFaces[edge].flatMap { above[$0] }) | 90 | let occluders = Set(edgeFaces[edge].flatMap { above[$0] }) |
| 76 | let covers = occluders.sorted().compactMap { cover(a, b, by: outlines[$0]) } | 91 | var covers = occluders.sorted().compactMap { cover(a, b, by: outlines[$0]) } |
| 92 | let own = Set(edgeFaces[edge].map { cluster[$0] }) | ||
| 93 | for f in outlines.indices where !own.contains(cluster[f]) { | ||
| 94 | guard let c = cover(a, b, by: outlines[f]), let nearer = nearer(f, than: edge) else { continue } | ||
| 95 | let low = max(c.range.lowerBound, nearer.lowerBound), high = min(c.range.upperBound, nearer.upperBound) | ||
| 96 | if high - low > tolerance / length(b - a) { covers.append((low...high, c.side)) } | ||
| 97 | } | ||
| 77 | 98 | ||
| 78 | // between consecutive cut points coverage is constant; test each piece's midpoint | 99 | // between consecutive cut points coverage is constant; test each piece's midpoint |
| 79 | let cuts = Set([0.0, 1.0] + covers.flatMap { [$0.range.lowerBound, $0.range.upperBound] }).sorted() | 100 | let cuts = Set([0.0, 1.0] + covers.flatMap { [$0.range.lowerBound, $0.range.upperBound] }).sorted() |
| @@ -96,6 +117,23 @@ struct Occlusion { | |||
| 96 | 117 | ||
| 97 | enum Side { case left, right, both } | 118 | enum Side { case left, right, both } |
| 98 | 119 | ||
| 120 | /// The parameters of `edge` at which face `f`'s plane lies nearer the eye along the view, or | ||
| 121 | /// nil where it nowhere does or `f` is seen edge-on. | ||
| 122 | private func nearer(_ f: Int, than edge: Int) -> ClosedRange<Double>? { | ||
| 123 | let n = normals[f], facing = dot(n, eye) | ||
| 124 | guard abs(facing) > 1e-9 else { return nil } | ||
| 125 | let corner = positions[facesVertices[f][0]] | ||
| 126 | let (p, q) = (positions[edgesVertices[edge][0]], positions[edgesVertices[edge][1]]) | ||
| 127 | // how far toward the eye the face's plane is from the edge's point at t: d0 + (d1 − d0) t | ||
| 128 | let d0 = dot(n, corner - p) / facing, d1 = dot(n, corner - q) / facing | ||
| 129 | switch (d0 > tolerance, d1 > tolerance) { | ||
| 130 | case (true, true): return 0...1 | ||
| 131 | case (false, false): return nil | ||
| 132 | case (true, false): return 0...((d0 - tolerance) / (d0 - d1)) | ||
| 133 | case (false, true): return ((tolerance - d0) / (d1 - d0))...1 | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 99 | /// The parameters of the segment `a`–`b` the counterclockwise convex `polygon` covers, and on | 137 | /// The parameters of the segment `a`–`b` the counterclockwise convex `polygon` covers, and on |
| 100 | /// which side: both sides where the segment crosses its open interior, one side where the | 138 | /// which side: both sides where the segment crosses its open interior, one side where the |
| 101 | /// segment lies along one of its sides. | 139 | /// segment lies along one of its sides. |
Packages/FoldKit/Tests/NotationTests/DiagramTests.swift +33
| @@ -56,6 +56,39 @@ import FoldKitTestSupport | |||
| 56 | #expect(abs(segmentLength(segments[0]) - 1) < 1e-9) | 56 | #expect(abs(segmentLength(segments[0]) - 1) < 1e-9) |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | @Test func anEdgeBehindAFaceOfAnotherClusterIsHidden() throws { | ||
| 60 | // half.fold's right half stood up at x = 1; seen from the +x side, the wall hides the far | ||
| 61 | // raw edge x = 0, and seen from the −x side it does not | ||
| 62 | let (frame, file) = try halfWithWallRaised() | ||
| 63 | let order = try LayerOrder(faceOrders: [], normals: faceNormals(frame, file)) | ||
| 64 | let behind = visibleSegments(of: 5, frame: frame, file: file, order: order, | ||
| 65 | viewpoint: Viewpoint(eye: SIMD3(1, 0, 0.5), up: SIMD3(0, 0, 1))) | ||
| 66 | #expect(behind.isEmpty) | ||
| 67 | let inFront = visibleSegments(of: 5, frame: frame, file: file, order: order, | ||
| 68 | viewpoint: Viewpoint(eye: SIMD3(-1, 0, 0.5), up: SIMD3(0, 0, 1))) | ||
| 69 | #expect(inFront.count == 1) | ||
| 70 | #expect(abs(segmentLength(inFront[0]) - 2) < 1e-9) | ||
| 71 | } | ||
| 72 | |||
| 73 | @Test func anEdgeInFrontOfAFaceOfAnotherClusterStaysVisible() throws { | ||
| 74 | // the wall's top edge, seen from the +x side, is nearer the eye than the floor behind it | ||
| 75 | let (frame, file) = try halfWithWallRaised() | ||
| 76 | let order = try LayerOrder(faceOrders: [], normals: faceNormals(frame, file)) | ||
| 77 | let top = visibleSegments(of: 2, frame: frame, file: file, order: order, | ||
| 78 | viewpoint: Viewpoint(eye: SIMD3(1, 0, 0.5), up: SIMD3(0, 0, 1))) | ||
| 79 | #expect(top.count == 1) | ||
| 80 | #expect(abs(segmentLength(top[0]) - 2) < 1e-9) | ||
| 81 | } | ||
| 82 | |||
| 83 | /// half.fold's frame 0 with f1 turned 90° about x = 1, up to z = 1. | ||
| 84 | private func halfWithWallRaised() throws -> (FoldFrame, FoldFile) { | ||
| 85 | let file = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 86 | var frame = file.frames[0] | ||
| 87 | frame.verticesCoords[2] = SIMD3(1, 0, 1) | ||
| 88 | frame.verticesCoords[3] = SIMD3(1, 2, 1) | ||
| 89 | return (frame, file) | ||
| 90 | } | ||
| 91 | |||
| 59 | @Test func aPlainMultiAxisStepGetsOneArrowPerAxis() throws { | 92 | @Test func aPlainMultiAxisStepGetsOneArrowPerAxis() throws { |
| 60 | let arrows = try diagram("letter.fold", step: 1).arrows | 93 | let arrows = try diagram("letter.fold", step: 1).arrows |
| 61 | #expect(arrows.filter { if case .valleyFold = $0 { true } else { false } }.count == 1) | 94 | #expect(arrows.filter { if case .valleyFold = $0 { true } else { false } }.count == 1) |