indicatrix-editor 0.7.2

GUI-free faceting-design editor logic shared by the desktop editor and the wasm web app: the EditorSession (design, undo/redo with caller-supplied coalescing timestamps, selection, dirty tracking), tier/preform/material form parsing, plain-struct view models, the cutting sheet, Retarget, the guided walkthrough and the solve policy. No GUI toolkit, no threads, no clock, no filesystem.
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
//! The solve policy every editor front end shares.
//!
//! When a solve runs synchronously versus in the background, when an edit schedules a
//! debounced auto-solve, the "Solving..." and "auto-solve off" banner texts, the
//! cancellable solve with the solver's over-plane-cap fallback, the plane-cap diagnosis,
//! and the conversion of solved masts into viewport planes.
//!
//! Only the decisions live here -- no threads, timers or clock. The desktop runs
//! the solve on a worker thread and debounces with a `slint::Timer`; the web app
//! uses a Worker and `setTimeout`. Both ask these functions what to do.

use indicatrix::geometry::{
    GpuFacetPlane,
    meet_solver::{
        MeetConstraint, SolveControl, SolveError, SolveStrategy, SolvedTier, solve_meet_points,
    },
};
use indicatrix_cut_core::{Design, DesignSolveError};
use std::{
    sync::atomic::{AtomicBool, Ordering},
    time::Duration,
};

/// Solve cost is driven by plane count, not tier count.
///
/// A wide-orbit round-brilliant tier emits many planes at once (the corpus's largest
/// measured design is 103 tiers but 210 planes), so a small tier count can still be an
/// expensive, UI-blocking solve.
///
/// A rough estimate, not a re-measurement of the corpus: roughly double a 16-tier
/// reference point at the corpus's own ~2 planes-per-tier ratio, kept comfortably
/// under the 210-plane/5.9 s worst case.
pub const SYNC_SOLVE_PLANE_LIMIT: usize = 32;

/// Once a design has a real measured solve time, that measurement is a far better signal
/// than any plane-count estimate.
///
/// A design that solved in under this long most recently is still fine to solve again
/// synchronously, however many planes it has.
pub const SYNC_SOLVE_TIME_LIMIT: Duration = Duration::from_millis(500);

/// The most meet-derived tiers (every tier whose mast the solver has to find, as
/// opposed to an authored `ScaleReference` anchor) a design may have and still be
/// solved inline.
///
/// Anchored tiers are constants to the solver; meet-derived ones are what its
/// refinement sweeps iterate over, so this bounds the work a plane count alone
/// does not (many single-index meet tiers fit under
/// [`SYNC_SOLVE_PLANE_LIMIT`]).
pub const SYNC_SOLVE_MEET_TIER_LIMIT: usize = 8;

/// Debounce delay between an edit landing and an eligible auto-solve actually
/// dispatching.
///
/// Short enough that a burst of keystrokes (typing an angle, say) only ever triggers the
/// LAST one, long enough that it reads as "just happened," not "laggy."
pub const AUTO_SOLVE_DEBOUNCE: Duration = Duration::from_millis(150);

/// How long a "one edit behind" (partial) preview frame waits for a FOLLOW-UP edit
/// before concluding the edit stream went idle and asking for a full replan of its
/// own.
///
/// Longer than [`AUTO_SOLVE_DEBOUNCE`]: this waits for the edit stream itself
/// to quiet down, not just one keystroke's burst.
pub const IDLE_REPLAN_DEBOUNCE: Duration = Duration::from_millis(400);

/// How often a running background solve's banner updates its elapsed time.
pub const SOLVING_TICK_INTERVAL: Duration = Duration::from_millis(250);

/// Whether a solve for a design with `plane_count` planes should still run
/// synchronously (the New/Load/explicit-Solve fast path) rather than in the
/// background.
///
/// Prefers this design's own last REAL measured solve time when one
/// exists, since a real measurement beats any estimate; falls back to
/// `plane_count` only for a design that has never solved yet.
///
/// A measurement describes the design as it was when it was taken, so a fast one
/// can outlive the edits that made the design expensive. The desktop editor's
/// inline solve on its UI thread therefore asks [`should_solve_synchronously_for`]
/// instead, where a measurement can only veto an inline solve, never grant one.
#[must_use]
pub fn should_solve_synchronously(plane_count: usize, last_solve: Option<Duration>) -> bool {
    last_solve.map_or(plane_count <= SYNC_SOLVE_PLANE_LIMIT, |last| {
        last <= SYNC_SOLVE_TIME_LIMIT
    })
}

/// What a design will cost to solve, read off its tiers without solving.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SolveCostEstimate {
    /// The facet planes the schedule emits: one per index of every tier. Solve
    /// cost is driven by this, not by the tier count -- a wide-orbit tier emits
    /// many planes at once.
    pub planes: usize,
    /// Tiers whose mast the solver derives (meet an unspecified vertex, or meet
    /// named facets) rather than one anchored by an authored scale value.
    pub meet_derived_tiers: usize,
    /// Whether any tier carries an authoring-level target. Resolving one runs
    /// extra solves (a bootstrap plus a bisection) on top of the design's own.
    pub has_tier_targets: bool,
}

impl SolveCostEstimate {
    /// The estimate for `design`. Linear in the tier count; never solves.
    #[must_use]
    pub fn of(design: &Design) -> Self {
        Self {
            planes: design.tiers.iter().map(|t| t.indices.len()).sum(),
            meet_derived_tiers: design
                .tiers
                .iter()
                .filter(|t| !matches!(t.constraint, MeetConstraint::ScaleReference(_)))
                .count(),
            has_tier_targets: !design.tier_targets.is_empty(),
        }
    }
}

/// Whether a design of this `cost` may be solved inline on a UI thread.
///
/// Conservative on purpose, because an inline solve of a slow design freezes the
/// window (the corpus's worst case is 5.9 s). It is inline only when ALL hold:
/// at most [`SYNC_SOLVE_PLANE_LIMIT`] planes, at most [`SYNC_SOLVE_MEET_TIER_LIMIT`]
/// meet-derived tiers, no tier targets, and the design's last measured solve (if
/// it has one) took no longer than [`SYNC_SOLVE_TIME_LIMIT`]. Everything else runs
/// on the background worker, which can be cancelled.
///
/// The measurement is a veto, not a grant: it was taken on an earlier state of the
/// design, and the solver's cost is not monotonic in the edits between, so a fast
/// old measurement must not admit a design the estimate rules out.
#[must_use]
pub fn should_solve_synchronously_for(
    cost: SolveCostEstimate,
    last_solve: Option<Duration>,
) -> bool {
    cost.planes <= SYNC_SOLVE_PLANE_LIMIT
        && cost.meet_derived_tiers <= SYNC_SOLVE_MEET_TIER_LIMIT
        && !cost.has_tier_targets
        && last_solve.is_none_or(|last| last <= SYNC_SOLVE_TIME_LIMIT)
}

/// Whether an edit that just landed should schedule a debounced auto-solve.
///
/// `budget` `0` disables auto-solve outright (stale-marker-only behaviour); otherwise, a
/// design with no measurement YET (`last_solve: None` -- nothing has solved since the
/// last New/Load) is scheduled optimistically: a fresh design's schedule starts empty and
/// solves near-instantly.
///
/// Once a real measurement exists,
/// it alone decides.
#[must_use]
pub const fn should_schedule_auto_solve(last_solve: Option<Duration>, budget: Duration) -> bool {
    if budget.is_zero() {
        return false;
    }
    match last_solve {
        Some(last) => last.as_millis() < budget.as_millis(),
        None => true,
    }
}

/// The banner text shown while auto-solve is disabled FOR THIS DESIGN specifically
/// (as opposed to a `0` budget) -- this design's own last measured solve already
/// exceeds the configured budget.
#[must_use]
pub fn auto_solve_off_note(last_solve: Duration) -> String {
    format!(
        "Auto-solve off for this design: last solve took {:.1}s.",
        last_solve.as_secs_f32()
    )
}

/// The "Solving..." banner text a running background solve shows, ticked forward
/// every [`SOLVING_TICK_INTERVAL`].
#[must_use]
pub fn solving_banner(tier_count: usize, elapsed: Duration) -> String {
    format!(
        "Solving... ({tier_count} tier{}) -- {:.1}s elapsed",
        if tier_count == 1 { "" } else { "s" },
        elapsed.as_secs_f32()
    )
}

/// [`Design::solve`]'s cancellable counterpart for a background solve.
///
/// Same legacy [`SolveError::TooManyPlanes`] fallback [`Design::solve`] documents on
/// itself (reproduced here, since that method's own `SolveControl::default()` can never
/// observe a real cancel).
///
/// [`SolveError::Cancelled`] is returned as a real `Err`, not swallowed: the caller
/// treats it exactly like any other solve error.
///
/// # Errors
///
/// Whatever [`Design::solve_with`] returns, except `TooManyPlanes` (replaced by the
/// solver's own all-`Failed` fallback list); `Cancelled` when `cancel` is set.
pub fn solve_cancellably(
    design: &Design,
    cancel: &AtomicBool,
) -> Result<Vec<SolvedTier>, DesignSolveError> {
    match design.solve_with(&SolveControl::with_cancel(cancel)) {
        Err(DesignSolveError::Solve(SolveError::TooManyPlanes { .. })) => {
            // Threads `cancel` into this fallback too: a cancel observed right as the
            // design turns out to be over `MAX_PLANES` must still return `Cancelled`
            // rather than silently finishing the (legacy, always-`Ok`) fallback build.
            if cancel.load(Ordering::Relaxed) {
                return Err(DesignSolveError::Solve(SolveError::Cancelled));
            }
            Ok(solve_meet_points(
                design.meta.gear_teeth_abs(),
                &design.meet_tier_inputs(),
            ))
        }
        other => other,
    }
}

/// A CHEAP (no solve) pre-filter for [`too_many_planes_message`].
///
/// `true` iff `solved` carries the exact tell the solver stamps into every non-anchor
/// tier's own [`SolvedTier::detail`] for its all-`SolveStrategy::Failed`
/// over-`MAX_PLANES` fallback ("...
///
/// above the N-plane cap for candidate-vertex enumeration").
/// [`too_many_planes_message`]'s own `solve_with` re-check is the only
/// AUTHORITATIVE source of the numbers; this only lets a caller skip that re-check
/// for the overwhelming majority of `Degenerate`/`Unbounded` results that have
/// nothing to do with the plane cap. A false negative only means an ordinary
/// (unhelpful but not wrong) message shows instead of the plane-cap one.
#[must_use]
pub fn likely_hit_plane_cap(solved: &[SolvedTier]) -> bool {
    solved
        .iter()
        .any(|t| matches!(t.strategy, SolveStrategy::Failed) && t.detail.contains("plane cap"))
}

/// An over-cap design silently renders as an ordinary (misleading)
/// "Degenerate"/"Unbounded" status.
///
/// Since `Design::solve()`/[`solve_cancellably`] both reproduce the solver's own
/// all-`Failed` fallback rather than an error.
///
/// Runs `Design::solve_with` (which surfaces the real
/// [`SolveError::TooManyPlanes`]) purely to check for this one condition: `Some`
/// with a cutter-actionable sentence iff it applies, `None` otherwise.
#[must_use]
pub fn too_many_planes_message(design: &Design) -> Option<String> {
    match design.solve_with(&SolveControl::default()) {
        Err(DesignSolveError::Solve(SolveError::TooManyPlanes { planes, max })) => Some(format!(
            "This design has {planes} facet planes; the solver supports up to {max} -- reduce \
             symmetry or split the design."
        )),
        _ => None,
    }
}

/// Converts an already-solved mast list into the `GpuFacetPlane`s a viewport draws,
/// via [`Design::planes_from_solved`] -- the `n . x <= m` half-space convention
/// flipped to `GpuFacetPlane`'s `n . x + d = 0` (`d = -m`).
///
/// See
/// [`crate::view_model::solid_status::design_to_gpu_planes`] for the
/// solve-internally counterpart.
#[must_use]
pub fn design_to_gpu_planes_from_solved(
    design: &Design,
    solved: &[SolvedTier],
) -> Vec<GpuFacetPlane> {
    design
        .planes_from_solved(solved)
        .into_iter()
        .map(|(normal, offset)| GpuFacetPlane::new(normal.as_vec3(), -offset as f32))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use indicatrix_cut_core::{ConstraintTier, PreformSpec, ScheduleMeta};

    #[test]
    fn a_zero_budget_never_schedules_and_no_measurement_schedules_optimistically() {
        assert!(!should_schedule_auto_solve(None, Duration::ZERO));
        assert!(should_schedule_auto_solve(None, Duration::from_millis(1)));
        assert!(should_schedule_auto_solve(
            Some(Duration::from_millis(100)),
            Duration::from_millis(200)
        ));
        assert!(!should_schedule_auto_solve(
            Some(Duration::from_millis(200)),
            Duration::from_millis(200)
        ));
    }

    #[test]
    fn a_measurement_beats_the_plane_count_estimate() {
        assert!(should_solve_synchronously(SYNC_SOLVE_PLANE_LIMIT, None));
        assert!(!should_solve_synchronously(
            SYNC_SOLVE_PLANE_LIMIT + 1,
            None
        ));
        assert!(should_solve_synchronously(
            10_000,
            Some(SYNC_SOLVE_TIME_LIMIT)
        ));
        assert!(!should_solve_synchronously(
            1,
            Some(SYNC_SOLVE_TIME_LIMIT + Duration::from_millis(1))
        ));
    }

    const SMALL: SolveCostEstimate = SolveCostEstimate {
        planes: 4,
        meet_derived_tiers: 2,
        has_tier_targets: false,
    };

    #[test]
    fn a_small_design_solves_inline_unless_its_last_solve_was_slow() {
        assert!(should_solve_synchronously_for(SMALL, None));
        assert!(should_solve_synchronously_for(
            SMALL,
            Some(SYNC_SOLVE_TIME_LIMIT)
        ));
        assert!(!should_solve_synchronously_for(
            SMALL,
            Some(SYNC_SOLVE_TIME_LIMIT + Duration::from_millis(1))
        ));
    }

    #[test]
    fn a_fast_measurement_never_admits_a_design_the_estimate_rules_out() {
        let fast = Some(Duration::from_millis(1));
        let too_many_planes = SolveCostEstimate {
            planes: SYNC_SOLVE_PLANE_LIMIT + 1,
            ..SMALL
        };
        let too_many_meet_tiers = SolveCostEstimate {
            meet_derived_tiers: SYNC_SOLVE_MEET_TIER_LIMIT + 1,
            ..SMALL
        };
        let targeted = SolveCostEstimate {
            has_tier_targets: true,
            ..SMALL
        };
        for cost in [too_many_planes, too_many_meet_tiers, targeted] {
            assert!(!should_solve_synchronously_for(cost, None), "{cost:?}");
            assert!(!should_solve_synchronously_for(cost, fast), "{cost:?}");
        }
    }

    #[test]
    fn the_limits_themselves_are_still_inline() {
        let at_limits = SolveCostEstimate {
            planes: SYNC_SOLVE_PLANE_LIMIT,
            meet_derived_tiers: SYNC_SOLVE_MEET_TIER_LIMIT,
            has_tier_targets: false,
        };
        assert!(should_solve_synchronously_for(at_limits, None));
    }

    #[test]
    fn the_estimate_counts_indices_and_only_the_non_anchored_tiers() {
        let mut design = Design::new(
            PreformSpec::block(2.0, 1.0, 2.0),
            ScheduleMeta::standard_round_brilliant(),
            ConstraintTier::standard_round_brilliant(),
        );
        let expected_planes: usize = design.tiers.iter().map(|t| t.indices.len()).sum();
        let anchored = SolveCostEstimate::of(&design);
        assert_eq!(anchored.planes, expected_planes);
        assert!(anchored.planes > 0);
        assert_eq!(
            anchored.meet_derived_tiers, 0,
            "every template tier is anchored"
        );
        assert!(!anchored.has_tier_targets);

        design.tiers[0].constraint = MeetConstraint::MeetExisting;
        design.tiers[1].constraint = MeetConstraint::MeetNamed(vec!["P1".to_string()]);
        let mixed = SolveCostEstimate::of(&design);
        assert_eq!(mixed.meet_derived_tiers, 2);
        assert_eq!(
            mixed.planes, expected_planes,
            "a constraint change moves no plane"
        );
    }

    #[test]
    fn banners_read_as_sentences() {
        assert_eq!(
            solving_banner(1, Duration::from_millis(1300)),
            "Solving... (1 tier) -- 1.3s elapsed"
        );
        assert_eq!(
            solving_banner(3, Duration::ZERO),
            "Solving... (3 tiers) -- 0.0s elapsed"
        );
        assert_eq!(
            auto_solve_off_note(Duration::from_millis(2500)),
            "Auto-solve off for this design: last solve took 2.5s."
        );
    }

    #[test]
    fn a_set_cancel_flag_cancels_before_any_work() {
        let design = crate::EditorSession::fresh().design;
        let cancel = AtomicBool::new(false);
        assert!(solve_cancellably(&design, &cancel).is_ok());
        assert!(!likely_hit_plane_cap(&[]));
        assert_eq!(too_many_planes_message(&design), None);
    }
}