Packages/FoldKit/Tests/FoldingTests/InterpenetrationTests.swift

main
fold/Packages/FoldKit/Tests/FoldingTests/InterpenetrationTests.swift history · blame · raw

73 lines · 3556 bytes

11 symbols in this file
 1import Foundation
 2import Testing
 3import simd
 4import FoldFormat
 5@testable import Folding
 6import FoldKitTestSupport
 7
 8@Test func aCleanFoldHasNoIntersections() throws {
 9    let (file, i) = try interpolator("half.fold", step: 1)
10    #expect(interpenetrations(i, faces: file.facesVertices, samples: 64).isEmpty)
11}
12
13@Test func twoFlapsFoldingIntoEachOtherAreDetected() throws {
14    // both thirds of the letter sheet valley-folded at once: they meet at the midline from ~120°
15    let (file, i) = try interpolatorForBothThirdsInFront()
16    #expect(!interpenetrations(i, faces: file.facesVertices, samples: 64).isEmpty)
17}
18
19@Test func staggeredFlapsDoNotCollide() throws {
20    // the same two flaps on separate windows of t
21    let (file, i) = try interpolator("letter-staggered.fold", step: 1)
22    #expect(interpenetrations(i, faces: file.facesVertices, samples: 64).isEmpty)
23}
24
25@Test func aFaceSwingingThroughAFaceItSharesAVertexWithIsDetected() throws {
26    // built in code: fixed triangle (0,0,0), (1,1.2,0.4), (-1,1.2,0.4); moving triangle
27    // (0,0,0), (0,2,0), (-1,1,0) hinged on its edge along the y-axis, folding toward +z
28    let (file, i) = try interpolatorForSharedVertexSweep()
29    #expect(!interpenetrations(i, faces: file.facesVertices, samples: 64).isEmpty)
30}
31
32@Test func coplanarFacesAreNotReportedAsIntersecting() throws {
33    let file = try FoldFile(contentsOf: fixture("letter-sequential.fold"))
34    let i = FoldInterpolator(file: file, step: try readSteps(file)[1])
35    #expect(interpenetrations(i, faces: file.facesVertices, samples: 16).isEmpty)
36}
37
38@Test func edgeAdjacentFacesAreNotReportedAsIntersecting() throws {
39    let (file, i) = try interpolator("quarters.fold", step: 3)
40    #expect(interpenetrations(i, faces: file.facesVertices, samples: 16).isEmpty)
41}
42
43@Test func theReportedParameterIsWhereTheIntersectionBegins() throws {
44    let (file, i) = try interpolatorForBothThirdsInFront()
45    let found = interpenetrations(i, faces: file.facesVertices, samples: 64)
46    #expect(!found.isEmpty)
47    #expect(found.allSatisfy { abs($0.t - 2.0 / 3) <= 1.0 / 64 })   // the flaps meet at 120°
48}
49
50/// Two unit squares, each one FOLD face of two triangles, the first flat at z = 0 and the second
51/// turned about its diagonal from (0, 1) to (1, 0) by `tilt(t)` radians, so that it crosses the first.
52private func squaresCrossing(tilt: @escaping @Sendable (Double) -> Double, pop: ClosedRange<Double>?) -> [Intersection] {
53    let square: [SIMD3<Double>] = [SIMD3(0, 0, 0), SIMD3(1, 0, 0), SIMD3(1, 1, 0), SIMD3(0, 1, 0)]
54    let at = { @Sendable (t: Double) -> [SIMD3<Double>] in
55        let q = simd_quatd(angle: tilt(t), axis: normalize(SIMD3(1, -1, 0)))
56        return square + square.map { q.act($0 - SIMD3(0.5, 0.5, 0)) + SIMD3(0.5, 0.5, 0) }
57    }
58    return meshIntersections(triangles: [(0, 1, 2), (0, 2, 3), (4, 5, 6), (4, 6, 7)], faceOf: [0, 0, 1, 1],
59                             faces: [[0, 1, 2, 3], [4, 5, 6, 7]], samples: 8, meshAt: at, foldAt: at, pop: pop)
60}
61
62@Test func coincidentLayersThatDriftApartDuringAPopAreTheLayerOrdersBusiness() {
63    // ~1e-5 of drift across the square, inside one near-coplanar cluster, during the pop
64    #expect(squaresCrossing(tilt: { 1e-5 * sin(.pi * $0) }, pop: 0.0...1.0).isEmpty)
65}
66
67@Test func theSameDriftOutsideAPopStillCounts() {
68    #expect(!squaresCrossing(tilt: { 1e-5 * sin(.pi * $0) }, pop: nil).isEmpty)
69}
70
71@Test func aFlapPassingThroughALayerOfAnotherClusterDuringAPopStillCounts() {
72    #expect(!squaresCrossing(tilt: { .pi / 4 * sin(.pi * $0) }, pop: 0.0...1.0).isEmpty)
73}