indicatrix-cut 0.7.1

Desktop faceting-design editor: library browsing, spectral 3D rendering, material retargeting, and a solid inspection view.
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! The gemological-metrics worker: evaluates the optical metrics and the angular profile
//! off the render thread, so a light, cut or material change never stalls tracing.
//!
//! One evaluation is an 18x18 grid of multi-bounce traces (see
//! `indicatrix::color::metrics`), and a change of light, facets or material needs twenty
//! of them (the pose plus the 19-point profile). Run inline, that held the render loop for
//! seconds. Here the loop only *requests* an evaluation when its inputs differ from the
//! last request, and keeps using the last published result until a newer one lands.
//!
//! # Scheduling
//!
//! Requests are debounced: the worker starts once [`DEBOUNCE`] has passed without a newer
//! request, so an orbit or a slider drag costs one evaluation after the gesture settles,
//! not one per intermediate pose. [`MAX_DEFER`] bounds that wait, so a gesture that never
//! settles still refreshes the numbers about twice a second. The very first request runs
//! immediately. The thread is an ordinary OS thread, not a rayon task, and does not touch
//! the trace thread pool.

use super::{
    display_thread::FrameMetricsSnapshot,
    frame_helpers::push_metrics_to_ui,
    metrics::{MetricsCache, compute_or_reuse_metrics_geom},
};
use indicatrix::{
    color::metrics::GemOpticalMetrics,
    geometry::{
        plane::GpuFacetPlane,
        tool::{StoneGeometry, ToolPrimitive},
    },
    optics::{
        materials::GemMaterial,
        raytracer::{EnvironmentSource, LightingPreset},
    },
    render_setup::hash_geometry,
    renderer::env_map::EnvironmentMap,
};
use slint::{ComponentHandle, Weak};
use std::{
    sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError},
    thread,
    time::{Duration, Instant},
};

/// Quiet time after the latest request before the worker starts evaluating it.
pub(super) const DEBOUNCE: Duration = Duration::from_millis(100);

/// Longest a pending request may wait while newer requests keep replacing it.
pub(super) const MAX_DEFER: Duration = Duration::from_millis(500);

/// One finished evaluation: the pose metrics and the three 19-point profile graphs.
#[derive(Clone, Copy)]
pub(super) struct EvaluatedMetrics {
    pub(super) metrics: GemOpticalMetrics,
    pub(super) graph_brilliance: [f32; 19],
    pub(super) graph_extinction: [f32; 19],
    pub(super) graph_windowing: [f32; 19],
}

impl EvaluatedMetrics {
    /// The display-cycle payload for these values at the current camera pitch.
    pub(super) const fn snapshot(self, cam_pitch_deg: f32) -> FrameMetricsSnapshot {
        FrameMetricsSnapshot {
            metrics: self.metrics,
            graph_brilliance: self.graph_brilliance,
            graph_extinction: self.graph_extinction,
            graph_windowing: self.graph_windowing,
            cam_pitch_deg,
        }
    }
}

/// The values shown before the first evaluation lands, equal to the UI's own initial
/// state.
const NOT_YET_EVALUATED: EvaluatedMetrics = EvaluatedMetrics {
    metrics: GemOpticalMetrics {
        brilliance_pct: 0.0,
        fire_index: 0.0,
        scintillation_pct: 0.0,
        windowing_pct: 0.0,
        extinction_pct: 0.0,
    },
    graph_brilliance: [0.0; 19],
    graph_extinction: [0.0; 19],
    graph_windowing: [0.0; 19],
};

/// Everything one evaluation reads.
struct Request {
    planes: Arc<Vec<GpuFacetPlane>>,
    /// The concave tools cut out of `planes`; empty for a planar stone.
    tools: Arc<Vec<ToolPrimitive>>,
    material: GemMaterial,
    /// `[yaw, pitch, light_yaw, light_pitch]`.
    pose: [f32; 4],
    /// The selected lighting preset: the metrics are scored under the radiance the viewport
    /// is lit with.
    lighting_preset: LightingPreset,
    /// The loaded HDR panorama, which replaces the preset's rig as the lighting.
    env_map: Option<Arc<EnvironmentMap>>,
}

impl Request {
    /// The environment this request is scored under: the loaded panorama if any, else the
    /// preset's rig at the request's light pose.
    fn environment(&self) -> EnvironmentSource<'_> {
        let [_, _, light_yaw, light_pitch] = self.pose;
        self.env_map.as_deref().map_or_else(
            || self.lighting_preset.studio(1.0, light_yaw, light_pitch),
            EnvironmentSource::HdrMap,
        )
    }
}

/// Whether two optional panoramas are the same loaded map (or both absent).
fn same_env_map(a: Option<&Arc<EnvironmentMap>>, b: Option<&Arc<EnvironmentMap>>) -> bool {
    match (a, b) {
        (None, None) => true,
        (Some(a), Some(b)) => Arc::ptr_eq(a, b),
        _ => false,
    }
}

/// A request waiting out its debounce.
struct Pending {
    request: Request,
    first_queued: Instant,
    last_changed: Instant,
}

impl Pending {
    /// When the worker may start: [`DEBOUNCE`] after the last change, but never later
    /// than [`MAX_DEFER`] after the request first queued, and at once while nothing has
    /// been published yet.
    fn due_at(&self, have_result: bool) -> Instant {
        if have_result {
            (self.last_changed + DEBOUNCE).min(self.first_queued + MAX_DEFER)
        } else {
            self.first_queued
        }
    }
}

#[derive(Default)]
struct State {
    pending: Option<Pending>,
    published: Option<EvaluatedMetrics>,
    /// Bumped by every publication; `0` until the first.
    version: u64,
    closed: bool,
}

struct Shared {
    state: Mutex<State>,
    wake: Condvar,
}

impl Shared {
    fn lock(&self) -> MutexGuard<'_, State> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Replaces the pending request, keeping the time the first one queued.
    fn submit(&self, request: Request) {
        let now = Instant::now();
        let mut state = self.lock();
        let first_queued = state
            .pending
            .as_ref()
            .map_or(now, |pending| pending.first_queued);
        state.pending = Some(Pending {
            request,
            first_queued,
            last_changed: now,
        });
        drop(state);
        self.wake.notify_one();
    }

    /// Blocks until a request is due and takes it; `None` once the handle is gone.
    fn next_due(&self) -> Option<Request> {
        let mut state = self.lock();
        loop {
            if state.closed {
                return None;
            }
            let have_result = state.published.is_some();
            let due = state
                .pending
                .as_ref()
                .map(|pending| pending.due_at(have_result));
            state = match due {
                None => self
                    .wake
                    .wait(state)
                    .unwrap_or_else(PoisonError::into_inner),
                Some(due) => {
                    let now = Instant::now();
                    if now >= due {
                        return state.pending.take().map(|pending| pending.request);
                    }
                    self.wake
                        .wait_timeout(state, due - now)
                        .unwrap_or_else(PoisonError::into_inner)
                        .0
                }
            };
        }
    }

    fn publish(&self, evaluated: EvaluatedMetrics) {
        let mut state = self.lock();
        state.published = Some(evaluated);
        state.version += 1;
    }
}

/// The worker thread's body: evaluate what is due, publish, repeat until closed.
fn run(shared: &Shared) {
    let mut cache: Option<MetricsCache> = None;
    while let Some(request) = shared.next_due() {
        let [yaw, pitch, ..] = request.pose;
        let (metrics, graph_brilliance, graph_extinction, graph_windowing) =
            compute_or_reuse_metrics_geom(
                &mut cache,
                StoneGeometry {
                    planes: &request.planes,
                    tools: &request.tools,
                },
                &request.material,
                yaw,
                pitch,
                request.environment(),
            );
        shared.publish(EvaluatedMetrics {
            metrics,
            graph_brilliance,
            graph_extinction,
            graph_windowing,
        });
    }
}

/// What the last queued request was built from, to tell whether the next inputs differ.
struct Submitted {
    /// `hash_geometry` of the planes and tools: the planes' own `hash_planes` for a
    /// planar stone, so nothing keyed on it moves.
    planes_hash: u64,
    material: GemMaterial,
    pose: [f32; 4],
    lighting_preset: LightingPreset,
    env_map: Option<Arc<EnvironmentMap>>,
}

/// The render thread's handle on the metrics worker.
pub(super) struct MetricsWorker {
    shared: Arc<Shared>,
    submitted: Option<Submitted>,
}

impl MetricsWorker {
    /// Starts the worker thread. If the OS refuses the thread, no metrics are ever
    /// published and the HUD keeps its initial values; rendering is unaffected.
    pub(super) fn spawn() -> Self {
        let shared = Arc::new(Shared {
            state: Mutex::new(State::default()),
            wake: Condvar::new(),
        });
        let worker_shared = Arc::clone(&shared);
        let spawned = thread::Builder::new()
            .name("indicatrix-metrics".to_string())
            .spawn(move || run(&worker_shared));
        if let Err(error) = spawned {
            tracing::warn!(%error, "could not start the metrics worker; HUD metrics stay at their defaults");
        }
        Self {
            shared,
            submitted: None,
        }
    }

    /// Queues an evaluation of these inputs unless they equal the last ones queued.
    ///
    /// `lighting_preset` and `env_map` are the lighting the viewport renders with (the
    /// panorama, when one is loaded, replaces the preset's rig).
    ///
    /// Costs one hash of the facet planes (and tools) and a material comparison on the calling
    /// thread; the material is cloned only when it actually changed.
    pub(super) fn request(
        &mut self,
        planes: &Arc<Vec<GpuFacetPlane>>,
        tools: &Arc<Vec<ToolPrimitive>>,
        material: &GemMaterial,
        pose: [f32; 4],
        lighting_preset: LightingPreset,
        env_map: Option<&Arc<EnvironmentMap>>,
    ) {
        let planes_hash = hash_geometry(StoneGeometry { planes, tools });
        if self.submitted.as_ref().is_some_and(|last| {
            last.planes_hash == planes_hash
                && last.pose == pose
                && last.material == *material
                && last.lighting_preset == lighting_preset
                && same_env_map(last.env_map.as_ref(), env_map)
        }) {
            return;
        }
        let last = self.submitted.get_or_insert_with(|| Submitted {
            planes_hash,
            material: material.clone(),
            pose,
            lighting_preset,
            env_map: env_map.cloned(),
        });
        last.planes_hash = planes_hash;
        last.pose = pose;
        last.lighting_preset = lighting_preset;
        last.env_map = env_map.cloned();
        if last.material != *material {
            last.material.clone_from(material);
        }
        self.shared.submit(Request {
            planes: Arc::clone(planes),
            tools: Arc::clone(tools),
            material: material.clone(),
            pose,
            lighting_preset,
            env_map: env_map.cloned(),
        });
    }

    /// The newest published values and their version (`0` while still the defaults).
    ///
    /// Possibly for inputs older than the last [`Self::request`]: the loop keeps showing
    /// these until the debounced evaluation replaces them.
    pub(super) fn latest(&self) -> (u64, EvaluatedMetrics) {
        let state = self.shared.lock();
        (state.version, state.published.unwrap_or(NOT_YET_EVALUATED))
    }
}

impl Drop for MetricsWorker {
    fn drop(&mut self) {
        self.shared.lock().closed = true;
        self.shared.wake.notify_all();
    }
}

/// Pushes the newest published metrics to the UI without an image, if `pushed_version` has
/// not seen them yet. This is what updates the HUD once the picture has converged and no
/// display cycle is left to carry them.
///
/// `ui_idle` must be `false` while a display cycle is in flight: that cycle carries its
/// own copy of the metrics, and pushing around it could let the older copy land last.
pub(super) fn push_if_fresh<T, M>(
    worker: &MetricsWorker,
    pushed_version: &mut u64,
    ui_idle: bool,
    ui_weak: &Weak<T>,
    update_metrics: &M,
    cam_pitch_deg: f32,
) where
    T: ComponentHandle + 'static,
    M: Fn(&T, f32, f32, f32, f32, f32, [f32; 19], [f32; 19], [f32; 19], f32)
        + Send
        + 'static
        + Clone,
{
    let (version, evaluated) = worker.latest();
    if ui_idle && version > *pushed_version {
        *pushed_version = version;
        push_metrics_to_ui(ui_weak, update_metrics, evaluated.snapshot(cam_pitch_deg));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn pending(first_queued: Instant, last_changed: Instant) -> Pending {
        Pending {
            request: Request {
                planes: Arc::new(Vec::new()),
                tools: Arc::new(Vec::new()),
                material: GemMaterial::diamond(),
                pose: [0.0; 4],
                lighting_preset: LightingPreset::RingLights,
                env_map: None,
            },
            first_queued,
            last_changed,
        }
    }

    #[test]
    fn a_settled_request_starts_after_the_debounce() {
        let t0 = Instant::now();
        assert_eq!(pending(t0, t0).due_at(true), t0 + DEBOUNCE);
    }

    #[test]
    fn a_request_that_keeps_changing_starts_no_later_than_the_max_defer() {
        let t0 = Instant::now();
        let last_changed = t0 + MAX_DEFER;
        assert_eq!(pending(t0, last_changed).due_at(true), t0 + MAX_DEFER);
    }

    #[test]
    fn the_first_request_starts_immediately() {
        let t0 = Instant::now();
        assert_eq!(pending(t0, t0 + DEBOUNCE).due_at(false), t0);
    }

    #[test]
    fn an_unchanged_request_is_not_queued_twice() {
        let mut worker = MetricsWorker {
            shared: Arc::new(Shared {
                state: Mutex::new(State::default()),
                wake: Condvar::new(),
            }),
            submitted: None,
        };
        let planes = Arc::new(Vec::new());
        let tools = Arc::new(Vec::new());
        let material = GemMaterial::diamond();
        let pose = [0.1, 0.2, 0.3, 0.4];
        let preset = LightingPreset::RingLights;

        worker.request(&planes, &tools, &material, pose, preset, None);
        let first = worker
            .shared
            .lock()
            .pending
            .as_ref()
            .map(|pending| pending.last_changed);
        assert!(first.is_some(), "the first request must queue");

        worker.request(&planes, &tools, &material, pose, preset, None);
        let second = worker
            .shared
            .lock()
            .pending
            .as_ref()
            .map(|pending| pending.last_changed);
        assert_eq!(first, second, "identical inputs must not re-queue");

        thread::sleep(Duration::from_millis(2));
        worker.request(
            &planes,
            &tools,
            &material,
            [0.1, 0.2, 0.3, 0.5],
            preset,
            None,
        );
        let third = worker
            .shared
            .lock()
            .pending
            .as_ref()
            .map(|pending| pending.last_changed);
        assert_ne!(second, third, "a changed light pose must re-queue");

        thread::sleep(Duration::from_millis(2));
        worker.request(
            &planes,
            &tools,
            &material,
            [0.1, 0.2, 0.3, 0.5],
            LightingPreset::LightTent,
            None,
        );
        let fourth = worker
            .shared
            .lock()
            .pending
            .as_ref()
            .map(|pending| pending.last_changed);
        assert_ne!(third, fourth, "a changed lighting preset must re-queue");

        thread::sleep(Duration::from_millis(2));
        let map = Arc::new(EnvironmentMap::uniform(4, 2, [1.0, 1.0, 1.0]));
        worker.request(
            &planes,
            &tools,
            &material,
            [0.1, 0.2, 0.3, 0.5],
            LightingPreset::LightTent,
            Some(&map),
        );
        let fifth = worker
            .shared
            .lock()
            .pending
            .as_ref()
            .map(|pending| pending.last_changed);
        assert_ne!(fourth, fifth, "a loaded HDR map must re-queue");

        thread::sleep(Duration::from_millis(2));
        let cut = Arc::new(vec![ToolPrimitive::ball(
            glam::Vec3::new(0.0, 0.3, 0.0),
            0.1,
        )]);
        worker.request(
            &planes,
            &cut,
            &material,
            [0.1, 0.2, 0.3, 0.5],
            LightingPreset::LightTent,
            Some(&map),
        );
        let sixth = worker
            .shared
            .lock()
            .pending
            .as_ref()
            .map(|pending| pending.last_changed);
        assert_ne!(
            fifth, sixth,
            "adding a concave tool changes the stone, so the metrics must be re-evaluated"
        );
    }
}