Commit dcab232542
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/Notation/EdgeOcclusion.swift +33 −1
| @@ -9,7 +9,8 @@ import simd | |||
| 9 | /// `order.clusters` read looking along −`eye`. A face in neither of their clusters is above the | 9 | /// `order.clusters` read looking along −`eye`. A face in neither of their clusters is above the |
| 10 | /// edge where it lies nearer the eye; a face seen edge-on covers nothing. An edge on the outer | 10 | /// edge where it lies nearer the eye; a face seen edge-on covers nothing. An edge on the outer |
| 11 | /// boundary of the union stays visible; one under the seam between two upper faces, covered on | 11 | /// boundary of the union stays visible; one under the seam between two upper faces, covered on |
| 12 | /// both sides, does not. | 12 | /// both sides, does not. Where an upper face overlying one of the edge's faces meets a neighbour |
| 13 | /// across a flat edge along the edge, the neighbour covers its side of that seam. | ||
| 13 | public func visibleSegments(of edge: Int, frame: FoldFrame, file: FoldFile, order: LayerOrder, | 14 | public func visibleSegments(of edge: Int, frame: FoldFrame, file: FoldFile, order: LayerOrder, |
| 14 | viewpoint: Viewpoint) -> [(SIMD2<Double>, SIMD2<Double>)] { | 15 | viewpoint: Viewpoint) -> [(SIMD2<Double>, SIMD2<Double>)] { |
| 15 | Occlusion(frame: frame, file: file, order: order, viewpoint: viewpoint).visibleSegments(of: edge) | 16 | Occlusion(frame: frame, file: file, order: order, viewpoint: viewpoint).visibleSegments(of: edge) |
| @@ -42,6 +43,8 @@ struct Occlusion { | |||
| 42 | let above: [[Int]] | 43 | let above: [[Int]] |
| 43 | /// Each face's cluster. | 44 | /// Each face's cluster. |
| 44 | let cluster: [Int] | 45 | let cluster: [Int] |
| 46 | /// Each face's neighbours across an interior edge flat in the frame, with that edge. | ||
| 47 | let flatNeighbours: [[(face: Int, edge: Int)]] | ||
| 45 | let positions: [SIMD3<Double>] | 48 | let positions: [SIMD3<Double>] |
| 46 | let facesVertices: [[Int]] | 49 | let facesVertices: [[Int]] |
| 47 | let normals: [SIMD3<Double>] | 50 | let normals: [SIMD3<Double>] |
| @@ -71,6 +74,12 @@ struct Occlusion { | |||
| 71 | } | 74 | } |
| 72 | self.above = above | 75 | self.above = above |
| 73 | self.cluster = cluster | 76 | self.cluster = cluster |
| 77 | var flatNeighbours = Array(repeating: [(face: Int, edge: Int)](), count: file.facesVertices.count) | ||
| 78 | for (e, faces) in edgeFaces.enumerated() where faces.count == 2 && abs(frame.edgesFoldAngle[e]) <= 1e-9 { | ||
| 79 | flatNeighbours[faces[0]].append((faces[1], e)) | ||
| 80 | flatNeighbours[faces[1]].append((faces[0], e)) | ||
| 81 | } | ||
| 82 | self.flatNeighbours = flatNeighbours | ||
| 74 | positions = frame.verticesCoords | 83 | positions = frame.verticesCoords |
| 75 | facesVertices = file.facesVertices | 84 | facesVertices = file.facesVertices |
| 76 | normals = faceNormals(positions: frame.verticesCoords, faces: file.facesVertices) | 85 | normals = faceNormals(positions: frame.verticesCoords, faces: file.facesVertices) |
| @@ -89,6 +98,17 @@ struct Occlusion { | |||
| 89 | guard length(b - a) > tolerance else { return [] } | 98 | guard length(b - a) > tolerance else { return [] } |
| 90 | let occluders = Set(edgeFaces[edge].flatMap { above[$0] }) | 99 | let occluders = Set(edgeFaces[edge].flatMap { above[$0] }) |
| 91 | var covers = occluders.sorted().compactMap { cover(a, b, by: outlines[$0]) } | 100 | var covers = occluders.sorted().compactMap { cover(a, b, by: outlines[$0]) } |
| 101 | // an occluder lying over one of the edge's faces continues across a flat edge along the | ||
| 102 | // edge into the same layer of paper: under that seam the neighbour covers its side too | ||
| 103 | let overlying = Set(edgeFaces[edge].flatMap { h in | ||
| 104 | above[h].filter { convexPolygonsOverlap(outlines[$0], outlines[h], tolerance: tolerance) } | ||
| 105 | }) | ||
| 106 | let seamed = Set(overlying.flatMap { f in | ||
| 107 | flatNeighbours[f].filter { alongEdge($0.edge, edge) }.map(\.face) | ||
| 108 | }).subtracting(occluders).subtracting(edgeFaces[edge]) | ||
| 109 | covers += seamed.sorted().compactMap { f in | ||
| 110 | cover(a, b, by: outlines[f]).flatMap { $0.side == .both ? nil : $0 } | ||
| 111 | } | ||
| 92 | let own = Set(edgeFaces[edge].map { cluster[$0] }) | 112 | let own = Set(edgeFaces[edge].map { cluster[$0] }) |
| 93 | for f in outlines.indices where !own.contains(cluster[f]) { | 113 | 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 } | 114 | guard let c = cover(a, b, by: outlines[f]), let nearer = nearer(f, than: edge) else { continue } |
| @@ -115,6 +135,18 @@ struct Occlusion { | |||
| 115 | .map { (a + $0.lowerBound * (b - a), a + $0.upperBound * (b - a)) } | 135 | .map { (a + $0.lowerBound * (b - a), a + $0.upperBound * (b - a)) } |
| 116 | } | 136 | } |
| 117 | 137 | ||
| 138 | /// Whether edge `e` lies on the line of edge `other` in the frame and shares a stretch of it. | ||
| 139 | private func alongEdge(_ e: Int, _ other: Int) -> Bool { | ||
| 140 | let a = positions[edgesVertices[other][0]], b = positions[edgesVertices[other][1]] | ||
| 141 | let (p, q) = (positions[edgesVertices[e][0]], positions[edgesVertices[e][1]]) | ||
| 142 | let d = b - a, length2 = length_squared(d) | ||
| 143 | guard length2 > 0 else { return false } | ||
| 144 | let off = [p, q].map { length(cross(d, $0 - a)) / length2.squareRoot() } | ||
| 145 | guard off.allSatisfy({ $0 <= tolerance }) else { return false } | ||
| 146 | let s = dot(p - a, d) / length2, t = dot(q - a, d) / length2 | ||
| 147 | return min(max(s, t), 1) - max(min(s, t), 0) > tolerance / length2.squareRoot() | ||
| 148 | } | ||
| 149 | |||
| 118 | enum Side { case left, right, both } | 150 | enum Side { case left, right, both } |
| 119 | 151 | ||
| 120 | /// The parameters of `edge` at which face `f`'s plane lies nearer the eye along the view, or | 152 | /// The parameters of `edge` at which face `f`'s plane lies nearer the eye along the view, or |
Packages/FoldKit/Tests/NotationTests/DiagramTests.swift +24
| @@ -182,3 +182,27 @@ private func halfWithWallRaised() throws -> (FoldFrame, FoldFile) { | |||
| 182 | } | 182 | } |
| 183 | } | 183 | } |
| 184 | } | 184 | } |
| 185 | |||
| 186 | @Test func theCranesReverseFoldsDrawTheirCreasesBesideThePushArrow() throws { | ||
| 187 | // R82: the neck's crease on the top layer is drawn as a step crease, and the push arrow, whose | ||
| 188 | // tip is on it, does not hide it | ||
| 189 | let crane = try ModelLibrary.model(named: "crane") | ||
| 190 | let full = buildDiagram(file: crane, step: try readStep(13, in: crane)) | ||
| 191 | let creases = full.edges.filter { $0.style == .dashed || $0.style == .dashDot } | ||
| 192 | let others = full.edges.filter { $0.style != .dashed && $0.style != .dashDot } | ||
| 193 | let push = full.arrows.filter { if case .technique = $0 { true } else { false } } | ||
| 194 | #expect(!creases.isEmpty && push.count == 1) | ||
| 195 | let creaseInk = inkedPixels(render(Diagram(edges: full.edges, arrows: []))) | ||
| 196 | - inkedPixels(render(Diagram(edges: others, arrows: []))) | ||
| 197 | let creaseInkWithArrow = inkedPixels(render(Diagram(edges: full.edges, arrows: push))) | ||
| 198 | - inkedPixels(render(Diagram(edges: others, arrows: push))) | ||
| 199 | #expect(Double(creaseInkWithArrow) >= 0.9 * Double(creaseInk)) | ||
| 200 | } | ||
| 201 | |||
| 202 | @Test func anEdgeUnderAFlatSeamInTheTopLayerIsHidden() throws { | ||
| 203 | // the crane's preliminary base: the front layer is one flat piece of paper split along the | ||
| 204 | // centre line by bend lines, and the layers' diagonal beneath it does not show through | ||
| 205 | let crane = try ModelLibrary.model(named: "crane") | ||
| 206 | let edges = buildDiagram(file: crane, step: try readStep(5, in: crane)).edges | ||
| 207 | #expect(!edges.contains { abs($0.from.x) < 1e-6 && abs($0.to.x) < 1e-6 && min($0.from.y, $0.to.y) < 0.7 }) | ||
| 208 | } | ||
Packages/FoldKit/Tests/NotationTests/Snapshots/box-10.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/box-9.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/crane-10.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/crane-12.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/crane-5.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/crane-6.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/crane-7.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/crane-8.png
Binary file not shown.
Packages/FoldKit/Tests/NotationTests/Snapshots/crane-9.png
Binary file not shown.