Commit 216be23aa7
Verified · cmc
Layout: unified · split
Packages/FoldKit/Sources/FoldFormat/FoldValidator.swift +8 −1
| @@ -27,6 +27,9 @@ public enum FoldDefect: Equatable, Sendable { | ||
| 27 | 27 | /// In step `step`, the face `moving` departs toward the still face `still` it overlaps in |
| 28 | 28 | /// frame n − 1, or arrives from the side of it opposite the one frame n's order gives. |
| 29 | 29 | case movesThroughStillFace(step: Int, moving: Int, still: Int) |
| 30 | /// Step `step`'s viewpoint has an `eye` or `up` of zero length, or an `up` along its `eye`, so | |
| 31 | /// it names no screen-up. | |
| 32 | case viewpointDegenerate(step: Int) | |
| 30 | 33 | } |
| 31 | 34 | |
| 32 | 35 | /// Lengths are compared to within this fraction of the sheet's longer side. |
| @@ -84,6 +87,10 @@ public func validate(_ file: FoldFile) -> [FoldDefect] { | ||
| 84 | 87 | if step.kind == .foldAndUnfold && frame.faceOrdersMidFold == nil { |
| 85 | 88 | defects.append(.foldAndUnfoldWithoutPeakOrder(step: n)) |
| 86 | 89 | } |
| 90 | let eye = step.viewpoint.eye, up = step.viewpoint.up | |
| 91 | let viewpointIsSound = length(eye) > 0 && length(up) > 0 | |
| 92 | && abs(dot(normalize(eye), normalize(up))) < parallelCosine | |
| 93 | if !viewpointIsSound { defects.append(.viewpointDegenerate(step: n)) } | |
| 87 | 94 | if step.nonRigid || step.pop != nil { |
| 88 | 95 | let wellFormed = step.pop.map { pop in |
| 89 | 96 | pop.popped.count == vertexCount && pop.phases.count == 2 |
| @@ -92,7 +99,7 @@ public func validate(_ file: FoldFile) -> [FoldDefect] { | ||
| 92 | 99 | if !wellFormed { defects.append(.popMalformed(step: n)) } |
| 93 | 100 | } |
| 94 | 101 | guard geometryIsSound[n - 1] && geometryIsSound[n] else { continue } |
| 95 | if frame.faceOrdersMidFold == nil && movingSetsOverlapInProjection(step: n, file) { | |
| 102 | if viewpointIsSound && frame.faceOrdersMidFold == nil && movingSetsOverlapInProjection(step: n, file) { | |
| 96 | 103 | defects.append(.overlappingMovingSetsWithoutMidFoldOrder(step: n)) |
| 97 | 104 | } |
| 98 | 105 | defects += isometryDefects(file, step: n, tolerance: tolerance) |
Packages/FoldKit/Tests/FoldFormatTests/ValidatorTests.swift +19
| @@ -154,3 +154,22 @@ func acceptsAValidModel(_ name: String) throws { | ||
| 154 | 154 | file.frames[1].step = withPop(popped: 6, phases: [1.0 / 3, 2.0 / 3]) |
| 155 | 155 | #expect(!validate(file).contains(.popMalformed(step: 1))) |
| 156 | 156 | } |
| 157 | ||
| 158 | @Test func rejectsAViewpointWhoseUpIsAlongItsEye() throws { | |
| 159 | var file = try FoldFile(contentsOf: fixture("half.fold")) | |
| 160 | let record = file.frames[1].step! | |
| 161 | func viewing(eye: SIMD3<Double>, up: SIMD3<Double>) -> StepRecord { | |
| 162 | StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes, | |
| 163 | viewpoint: Viewpoint(eye: eye, up: up)) | |
| 164 | } | |
| 165 | file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(0, 0, -2)) | |
| 166 | #expect(validate(file).contains(.viewpointDegenerate(step: 1))) | |
| 167 | file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(1e-6, 0, 1)) | |
| 168 | #expect(validate(file).contains(.viewpointDegenerate(step: 1))) | |
| 169 | file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: .zero) | |
| 170 | #expect(validate(file).contains(.viewpointDegenerate(step: 1))) | |
| 171 | file.frames[1].step = viewing(eye: .zero, up: SIMD3(0, 1, 0)) | |
| 172 | #expect(validate(file).contains(.viewpointDegenerate(step: 1))) | |
| 173 | file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 1)) | |
| 174 | #expect(!validate(file).contains(.viewpointDegenerate(step: 1))) | |
| 175 | } | |