Commit 216be23aa7

216be23aa7b01cdc18e92964cbb65531ff6c2232

parent: e7ef09b655

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-20 08:43 UTC

Reject a viewpoint whose up is along its eye

Ref #6

Layout: unified · split

Packages/FoldKit/Sources/FoldFormat/FoldValidator.swift +8 −1
@@ -27,6 +27,9 @@ public enum FoldDefect: Equatable, Sendable {
27 /// In step `step`, the face `moving` departs toward the still face `still` it overlaps in 27 /// In step `step`, the face `moving` departs toward the still face `still` it overlaps in
28 /// frame n − 1, or arrives from the side of it opposite the one frame n's order gives. 28 /// frame n − 1, or arrives from the side of it opposite the one frame n's order gives.
29 case movesThroughStillFace(step: Int, moving: Int, still: Int) 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/// Lengths are compared to within this fraction of the sheet's longer side. 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 if step.kind == .foldAndUnfold && frame.faceOrdersMidFold == nil { 87 if step.kind == .foldAndUnfold && frame.faceOrdersMidFold == nil {
85 defects.append(.foldAndUnfoldWithoutPeakOrder(step: n)) 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 if step.nonRigid || step.pop != nil { 94 if step.nonRigid || step.pop != nil {
88 let wellFormed = step.pop.map { pop in 95 let wellFormed = step.pop.map { pop in
89 pop.popped.count == vertexCount && pop.phases.count == 2 96 pop.popped.count == vertexCount && pop.phases.count == 2
@@ -92,7 +99,7 @@ public func validate(_ file: FoldFile) -> [FoldDefect] {
92 if !wellFormed { defects.append(.popMalformed(step: n)) } 99 if !wellFormed { defects.append(.popMalformed(step: n)) }
93 } 100 }
94 guard geometryIsSound[n - 1] && geometryIsSound[n] else { continue } 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 defects.append(.overlappingMovingSetsWithoutMidFoldOrder(step: n)) 103 defects.append(.overlappingMovingSetsWithoutMidFoldOrder(step: n))
97 } 104 }
98 defects += isometryDefects(file, step: n, tolerance: tolerance) 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 file.frames[1].step = withPop(popped: 6, phases: [1.0 / 3, 2.0 / 3]) 154 file.frames[1].step = withPop(popped: 6, phases: [1.0 / 3, 2.0 / 3])
155 #expect(!validate(file).contains(.popMalformed(step: 1))) 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}