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 {
2727 /// In step `step`, the face `moving` departs toward the still face `still` it overlaps in
2828 /// frame n − 1, or arrives from the side of it opposite the one frame n's order gives.
2929 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)
3033}
3134
3235/// Lengths are compared to within this fraction of the sheet's longer side.
@@ -84,6 +87,10 @@ public func validate(_ file: FoldFile) -> [FoldDefect] {
8487 if step.kind == .foldAndUnfold && frame.faceOrdersMidFold == nil {
8588 defects.append(.foldAndUnfoldWithoutPeakOrder(step: n))
8689 }
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)) }
8794 if step.nonRigid || step.pop != nil {
8895 let wellFormed = step.pop.map { pop in
8996 pop.popped.count == vertexCount && pop.phases.count == 2
@@ -92,7 +99,7 @@ public func validate(_ file: FoldFile) -> [FoldDefect] {
9299 if !wellFormed { defects.append(.popMalformed(step: n)) }
93100 }
94101 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) {
96103 defects.append(.overlappingMovingSetsWithoutMidFoldOrder(step: n))
97104 }
98105 defects += isometryDefects(file, step: n, tolerance: tolerance)
Packages/FoldKit/Tests/FoldFormatTests/ValidatorTests.swift +19
@@ -154,3 +154,22 @@ func acceptsAValidModel(_ name: String) throws {
154154 file.frames[1].step = withPop(popped: 6, phases: [1.0 / 3, 2.0 / 3])
155155 #expect(!validate(file).contains(.popMalformed(step: 1)))
156156}
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}