indicatrix-cut 0.2.0

Desktop faceting-design editor: library browsing, spectral 3D rendering, material retargeting, and a solid inspection view.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Which geometry to hand the solid preview after an edit, chosen automatically among
//! three tiers of freshness.
//!
//! Pure and independent of `preview_state`'s worker
//! thread/`RedrawGate` machinery -- [`plan_preview`] is a plain function callers run
//! on the UI thread before ever touching [`super::preview_state::SolidPreviewState`].
//!
//! # The three tiers
//!
//! 1. **Pinned.** Every tier is
//!    [`indicatrix::geometry::meet_solver::MeetConstraint::ScaleReference`] -- each
//!    mast is read directly off its own constraint, no solver call, so the design
//!    previews instantly regardless of tier count. Checked first, unconditionally.
//! 2. **Fresh.** At least one tier is free, `last_solved` is aligned with `design`'s
//!    current tier count (the precondition `resolve_dirty` panics on otherwise), and
//!    [`DirtySolver::resolve_dirty`] finishes within `budget`.
//! 3. **Stale.** Same call as tier 2, but wall time exceeds `budget`. The solve can't
//!    be interrupted mid-flight, so "over budget" is only known once the result is in
//!    hand. That fresh result is still chained forward as [`PreviewPlan::solved`] (so
//!    the NEXT call doesn't repeat the same slow resolve), but this frame draws the
//!    OLD (`previous`) planes so the user sees the last solid actually finished.
//!    `pending` names every dirty tier, for the caller's overlay.
//!
//! `last_solved` missing or misaligned (an `AddTier`/`RemoveTier` edit, which can
//! never resolve as a subgraph) falls back to a full [`indicatrix_cut_core::Design::solve`];
//! a successful full solve is authoritative regardless of timing, reported
//! [`Freshness::Fresh`] too. Either path failing with [`indicatrix_cut_core::MissingAnchor`]
//! reports [`Freshness::Unsolvable`], falling back to whatever `last_solved` planes
//! are available -- never an empty `PreviewPlan::planes` when a prior frame exists.

use glam::{DVec3, Vec3};
use indicatrix::geometry::meet_solver::{MeetConstraint, SolveStrategy, SolvedTier};
use indicatrix_cut_core::{Design, MissingAnchor};
use std::{
    collections::BTreeSet,
    time::{Duration, Instant},
};

/// Default over-budget threshold for [`plan_preview`]'s tier-2/3 decision.
///
/// `50` ms, a UI-responsiveness budget rather than a guarantee every design resolves
/// within it. Measured against the worst fixture available (CrackOtto-Step, 103
/// tiers, nearly all non-anchor): that resolve took **2.13 s**, confirming this is
/// exactly the shape tier 3 (`Freshness::Stale`) exists for -- a budget loose enough
/// to tolerate it would make ordinary ScaleReference-heavy edits feel just as
/// sluggish. See `tests::timing_resolve_dirty_on_crackotto_step_103_tier`.
pub const DEFAULT_PREVIEW_BUDGET: Duration = Duration::from_millis(50);

/// Abstracts [`indicatrix_cut_core::Design::resolve_dirty`] so [`plan_preview`] is testable:
/// a fake implementation can return canned results or sleep a controlled amount.
pub trait DirtySolver {
    /// # Errors
    ///
    /// See [`indicatrix_cut_core::Design::resolve_dirty`]'s own `# Errors` section.
    fn resolve_dirty(
        &self,
        design: &Design,
        previous: &[SolvedTier],
        dirty: &BTreeSet<usize>,
    ) -> Result<Vec<SolvedTier>, MissingAnchor>;
}

/// The real [`DirtySolver`], delegating straight to [`indicatrix_cut_core::Design::resolve_dirty`].
/// Every other implementation in this crate exists solely for this module's tests.
#[derive(Debug, Clone, Copy, Default)]
pub struct RealSolver;

impl DirtySolver for RealSolver {
    fn resolve_dirty(
        &self,
        design: &Design,
        previous: &[SolvedTier],
        dirty: &BTreeSet<usize>,
    ) -> Result<Vec<SolvedTier>, MissingAnchor> {
        design.resolve_dirty(previous, dirty)
    }
}

/// Which of the module doc comment's three tiers [`plan_preview`] chose, and why.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Freshness {
    /// No solver call was made.
    Pinned,
    /// Solved within budget (subgraph or full).
    Fresh,
    /// `pending` is the `dirty` set, for the caller's overlay/banner.
    Stale { pending: BTreeSet<usize> },
    /// See [`indicatrix_cut_core::MissingAnchor`].
    Unsolvable(MissingAnchor),
}

/// [`plan_preview`]'s result: what to draw, what to remember as `last_solved` next
/// call, and why.
#[derive(Debug, Clone)]
pub struct PreviewPlan {
    /// The plane arrangement to draw THIS frame, in the same `(normal, offset)` `f32`
    /// convention `mesh_cache::MeshCache::get_or_build` takes.
    pub planes: Vec<(Vec3, f32)>,
    /// The mast list to pass as `last_solved` next call -- can be newer than `planes`
    /// (see [`Freshness::Stale`]).
    pub solved: Option<Vec<SolvedTier>>,
    pub freshness: Freshness,
}

/// Narrows a [`indicatrix_cut_core::Design::planes_from_solved`] result to the `f32`
/// convention every other `solid_preview` module uses.
fn narrow_planes(planes: Vec<(DVec3, f64)>) -> Vec<(Vec3, f32)> {
    planes
        .into_iter()
        .map(|(n, m)| (Vec3::new(n.x as f32, n.y as f32, n.z as f32), m as f32))
        .collect()
}

/// Every tier's mast read directly off its own [`MeetConstraint::ScaleReference`] --
/// tier 1 of the module doc comment. Panics if any tier is not `ScaleReference`.
fn masts_from_pinned_tiers(design: &Design) -> Vec<SolvedTier> {
    design
        .tiers
        .iter()
        .map(|tier| {
            let MeetConstraint::ScaleReference(mast) = &tier.constraint else {
                unreachable!(
                    "masts_from_pinned_tiers: caller must confirm every tier is \
                     ScaleReference first"
                );
            };
            SolvedTier {
                mast: *mast,
                strategy: SolveStrategy::ScaleReference,
                detail: "pinned (ScaleReference)".to_string(),
            }
        })
        .collect()
}

/// Chooses which geometry to draw after an edit -- see the module doc comment for the
/// three tiers this implements.
///
/// `dirty` is the set of tier indices the triggering edit touched (see
/// `indicatrix_cut_core::resolve::affected_tiers`); `last_solved` is the previous call's
/// [`PreviewPlan::solved`], or `None` after an `AddTier`/`RemoveTier` edit.
#[must_use]
pub fn plan_preview(
    design: &Design,
    last_solved: Option<&[SolvedTier]>,
    dirty: &BTreeSet<usize>,
    budget: Duration,
    solver: &dyn DirtySolver,
) -> PreviewPlan {
    if design
        .tiers
        .iter()
        .all(|tier| matches!(tier.constraint, MeetConstraint::ScaleReference(_)))
    {
        let solved = masts_from_pinned_tiers(design);
        let planes = narrow_planes(design.planes_from_solved(&solved));
        return PreviewPlan {
            planes,
            solved: Some(solved),
            freshness: Freshness::Pinned,
        };
    }

    // Mismatch (or no previous solve) means an `AddTier`/`RemoveTier` edit happened;
    // a full solve is the only valid path.
    let aligned_previous = last_solved.filter(|previous| previous.len() == design.tiers.len());

    let Some(previous) = aligned_previous else {
        return match design.solve() {
            Ok(solved) => {
                let planes = narrow_planes(design.planes_from_solved(&solved));
                PreviewPlan {
                    planes,
                    solved: Some(solved),
                    freshness: Freshness::Fresh,
                }
            }
            Err(err) => {
                let planes = last_solved.map_or_else(Vec::new, |previous| {
                    narrow_planes(design.planes_from_solved(previous))
                });
                PreviewPlan {
                    planes,
                    solved: None,
                    freshness: Freshness::Unsolvable(err),
                }
            }
        };
    };

    let start = Instant::now();
    let result = solver.resolve_dirty(design, previous, dirty);
    let elapsed = start.elapsed();

    match result {
        Ok(new_solved) if elapsed <= budget => {
            let planes = narrow_planes(design.planes_from_solved(&new_solved));
            PreviewPlan {
                planes,
                solved: Some(new_solved),
                freshness: Freshness::Fresh,
            }
        }
        Ok(new_solved) => {
            // Over budget: show the OLD planes, but chain the fresh (late) result
            // forward as the next call's `last_solved`.
            let planes = narrow_planes(design.planes_from_solved(previous));
            PreviewPlan {
                planes,
                solved: Some(new_solved),
                freshness: Freshness::Stale {
                    pending: dirty.clone(),
                },
            }
        }
        Err(err) => {
            let planes = narrow_planes(design.planes_from_solved(previous));
            PreviewPlan {
                planes,
                solved: None,
                freshness: Freshness::Unsolvable(err),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use indicatrix::geometry::meet_solver::{Block, classify_blocks, meet_tier_inputs_from_asc};
    use indicatrix_cut_core::{ConstraintTier, PreformSpec, ScheduleMeta};

    /// Proves a branch of [`plan_preview`] never falls through to `resolve_dirty`.
    struct PanicSolver;

    impl DirtySolver for PanicSolver {
        fn resolve_dirty(
            &self,
            _design: &Design,
            _previous: &[SolvedTier],
            _dirty: &BTreeSet<usize>,
        ) -> Result<Vec<SolvedTier>, MissingAnchor> {
            panic!("DirtySolver::resolve_dirty must not be called on this branch");
        }
    }

    /// Returns `result` immediately -- the "resolve finished within budget" case.
    struct InstantSolver {
        result: Vec<SolvedTier>,
    }

    impl DirtySolver for InstantSolver {
        fn resolve_dirty(
            &self,
            _design: &Design,
            _previous: &[SolvedTier],
            _dirty: &BTreeSet<usize>,
        ) -> Result<Vec<SolvedTier>, MissingAnchor> {
            Ok(self.result.clone())
        }
    }

    /// Sleeps `sleep` before returning `result` -- simulates an over-budget resolve.
    struct SlowSolver {
        result: Vec<SolvedTier>,
        sleep: Duration,
    }

    impl DirtySolver for SlowSolver {
        fn resolve_dirty(
            &self,
            _design: &Design,
            _previous: &[SolvedTier],
            _dirty: &BTreeSet<usize>,
        ) -> Result<Vec<SolvedTier>, MissingAnchor> {
            std::thread::sleep(self.sleep);
            Ok(self.result.clone())
        }
    }

    fn tier(name: &str, angle_deg: f64, constraint: MeetConstraint) -> ConstraintTier {
        ConstraintTier {
            angle_deg,
            name: name.to_string(),
            indices: vec![0.0],
            constraint,
            imported_meet: None,
            detached: Vec::new(),
        }
    }

    fn pinned_design() -> Design {
        Design::new(
            PreformSpec::block(1.0, 1.0, 1.0),
            ScheduleMeta {
                gear_teeth: 96,
                ..ScheduleMeta::default()
            },
            vec![tier("Table", 0.0, MeetConstraint::ScaleReference(0.5))],
        )
    }

    /// One anchored (`ScaleReference`) tier plus one free (`MeetExisting`) tier in the
    /// same block, just enough real structure for `Design::solve()` to succeed.
    fn free_design() -> Design {
        Design::new(
            PreformSpec::block(2.0, 1.0, 2.0),
            ScheduleMeta {
                gear_teeth: 96,
                ..ScheduleMeta::default()
            },
            vec![
                tier("C1", 30.0, MeetConstraint::ScaleReference(0.6)),
                tier("C2", 40.0, MeetConstraint::MeetExisting),
            ],
        )
    }

    #[test]
    fn pinned_only_design_never_calls_the_solver() {
        let design = pinned_design();
        let plan = plan_preview(
            &design,
            None,
            &BTreeSet::new(),
            DEFAULT_PREVIEW_BUDGET,
            &PanicSolver,
        );
        assert_eq!(plan.freshness, Freshness::Pinned);
        assert_ne!(plan.planes, [] as [(Vec3, f32); 0]);
        assert_eq!(plan.solved.map(|s| s.len()), Some(1));
    }

    #[test]
    fn cheap_free_design_resolves_fresh_within_budget() {
        let design = free_design();
        let previous = design.solve().expect("fixture must solve");
        let solver = InstantSolver {
            result: previous.clone(),
        };
        let dirty = BTreeSet::from([1]);

        let plan = plan_preview(
            &design,
            Some(&previous),
            &dirty,
            Duration::from_millis(50),
            &solver,
        );
        assert_eq!(plan.freshness, Freshness::Fresh);
        assert!(plan.solved.is_some());
        assert_ne!(plan.planes, [] as [(Vec3, f32); 0]);
    }

    #[test]
    fn over_budget_solver_reports_stale_with_the_edited_tier_pending() {
        let design = free_design();
        let previous = design.solve().expect("fixture must solve");
        let solver = SlowSolver {
            result: previous.clone(),
            sleep: Duration::from_millis(40),
        };
        let dirty = BTreeSet::from([1]);
        let budget = Duration::from_millis(5);

        let plan = plan_preview(&design, Some(&previous), &dirty, budget, &solver);
        match plan.freshness {
            Freshness::Stale { pending } => assert_eq!(pending, dirty),
            other => panic!("expected Stale, got {other:?}"),
        }
        assert!(
            plan.solved.is_some(),
            "the fresh (late) result must still be chained forward as the next \
             call's last_solved, even though this frame reports Stale"
        );
    }

    #[test]
    fn misaligned_previous_falls_back_to_a_full_solve() {
        let design = free_design();
        // Wrong length: stands in for a `last_solved` left over from before an
        // `AddTier`/`RemoveTier` edit.
        let stale_previous: Vec<SolvedTier> = Vec::new();
        let dirty = BTreeSet::from([1]);

        let plan = plan_preview(
            &design,
            Some(&stale_previous),
            &dirty,
            DEFAULT_PREVIEW_BUDGET,
            &PanicSolver,
        );
        assert_eq!(plan.freshness, Freshness::Fresh);
        assert_eq!(plan.solved.map(|s| s.len()), Some(design.tiers.len()));
    }

    #[test]
    fn no_previous_at_all_also_falls_back_to_a_full_solve() {
        let design = free_design();
        let plan = plan_preview(
            &design,
            None,
            &BTreeSet::new(),
            DEFAULT_PREVIEW_BUDGET,
            &PanicSolver,
        );
        assert_eq!(plan.freshness, Freshness::Fresh);
        assert!(plan.solved.is_some());
    }

    /// `indicatrix-cut-core`'s "CrackOtto-Step" fixture (PC 05.115, 103 tiers), re-authored
    /// as a full [`Design`] rather than raw planes: every tier is implicit
    /// `MeetExisting`, so one `ScaleReference` anchor is bootstrapped per
    /// crown/pavilion/girdle block from that tier's real recorded mast, leaving the
    /// rest genuinely free.
    const CRACKOTTO_STEP_ASC: &str = include_str!(
        "../../../../../crates/indicatrix-cut-core/src/optimize_cost_probe_crackotto_step.asc"
    );

    fn crackotto_step_design() -> Design {
        let schedule =
            indicatrix_formats::asc::parse_asc(CRACKOTTO_STEP_ASC).expect("fixture must parse");
        let mut inputs = meet_tier_inputs_from_asc(&schedule);
        let blocks = classify_blocks(&inputs);
        for block in [Block::Crown, Block::Pavilion, Block::Girdle] {
            let anchored = inputs.iter().zip(&blocks).any(|(t, &b)| {
                b == block && matches!(t.constraint, MeetConstraint::ScaleReference(_))
            });
            if anchored {
                continue;
            }
            if let Some(i) = (0..inputs.len()).find(|&i| blocks[i] == block) {
                inputs[i].constraint = MeetConstraint::ScaleReference(schedule.tiers[i].mast);
            }
        }
        let tiers = inputs
            .into_iter()
            .zip(&schedule.tiers)
            .map(|(input, original)| ConstraintTier {
                angle_deg: input.angle_deg,
                name: original.name.clone(),
                indices: input.indices,
                constraint: input.constraint,
                imported_meet: None,
                detached: Vec::new(),
            })
            .collect();
        Design::new(
            PreformSpec::block(2.0, 1.0, 2.0),
            ScheduleMeta {
                gemcad_version: schedule.gemcad_version.clone(),
                gear_teeth: schedule.gear_teeth,
                gear_reference_angle: schedule.gear_reference_angle,
                symmetry_order: schedule.symmetry_order,
                mirror: schedule.mirror,
                refractive_index: schedule.refractive_index,
                headers: schedule.headers.clone(),
                footnotes: schedule.footnotes,
            },
            tiers,
        )
    }

    #[test]
    #[ignore = "timing measurement, not a correctness check -- run with \
                --release --ignored --nocapture"]
    fn timing_resolve_dirty_on_crackotto_step_103_tier() {
        let design = crackotto_step_design();
        assert_eq!(
            design.tiers.len(),
            103,
            "fixture must have its real tier count"
        );
        let baseline = design.solve().expect("must solve to get a starting point");

        let mut edited = design;
        edited.tiers[20].angle_deg += 0.5;
        let dirty = BTreeSet::from([20]);

        let start = Instant::now();
        let result = edited
            .resolve_dirty(&baseline, &dirty)
            .expect("subgraph resolve");
        let elapsed = start.elapsed();
        assert_eq!(result.len(), 103);

        println!(
            "CrackOtto-Step (103 tiers): resolve_dirty for a single-tier edit: {elapsed:?} \
             (DEFAULT_PREVIEW_BUDGET = {DEFAULT_PREVIEW_BUDGET:?})"
        );
    }
}