llevant 0.1.1

Fast MCNP constructive solid geometry engine
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
//! Batch point classification through universes.

use rayon::prelude::*;

use crate::bbox::Aabb;
use crate::expr::{self, Tri};
use crate::intern;
use crate::lattice::Element;
use crate::model::Fill;
use crate::model::Unit;
use crate::surface::Side;
use crate::vec3::Vec3;
use crate::work::Work;
use crate::Model;

/// How a point was classified.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Status {
    /// Inside exactly one cell at every level.
    Inside,
    /// No cell of the universe at the last level contains the point.
    Gap,
    /// Several cells of the universe at the last level contain it.
    Overlap,
    /// On a cell boundary: within the tolerance of a surface that decides it.
    OnSurface,
    /// No supported cell contains the point, and a cell that might uses an
    /// unsupported feature.
    Unsupported,
    /// The universe nesting is deeper than the limit (a cycle, or absurd input).
    TooDeep,
}

impl Status {
    /// Short name: `inside`, `gap`, `overlap`, `on-surface`, `unsupported`
    /// or `too-deep`.
    pub fn name(self) -> &'static str {
        match self {
            Status::Inside => "inside",
            Status::Gap => "gap",
            Status::Overlap => "overlap",
            Status::OnSurface => "on-surface",
            Status::Unsupported => "unsupported",
            Status::TooDeep => "too-deep",
        }
    }

    /// Overlaps, unsupported cells and too-deep nesting: what a check
    /// reports besides gaps.
    pub fn is_problem(self) -> bool {
        matches!(
            self,
            Status::Overlap | Status::Unsupported | Status::TooDeep
        )
    }
}

/// One level of a path: a cell, and the lattice element when the cell is a
/// lattice.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Step {
    /// Index into [`Model::cells`].
    pub cell: u32,
    pub lattice: Option<[i32; 3]>,
}

/// The full description of where a point is.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Label {
    pub status: Status,
    /// Cells from level 0 down. For [`Status::Inside`] the last step is the
    /// cell that holds the point (a lattice cell, with its element, when the
    /// element is filled by the lattice cell itself); otherwise the path ends
    /// at the filled cell whose universe has the problem (empty at level 0),
    /// or at a lattice element outside its fill array ([`Status::Gap`]).
    pub path: Vec<Step>,
    /// For overlaps, on-surface and unsupported points: the cells involved
    /// at the last level.
    pub involved: Vec<u32>,
}

/// Classification of a batch of points: one label id per point, into `labels`.
#[derive(Clone, Debug, Default)]
pub struct Classification {
    pub ids: Vec<u32>,
    pub labels: Vec<Label>,
}

pub(crate) const MAX_DEPTH: usize = 64;

/// A candidate found without an octree leaf: its whole program counts.
pub(crate) const NO_ITEM: u32 = u32::MAX;

const CHUNK: usize = 2048;

/// Per-thread buffers.
#[derive(Default)]
struct Scratch {
    candidates: Vec<u32>,
    /// Per candidate: its octree item (for its program specialized to the
    /// leaf), or `NO_ITEM`.
    items: Vec<u32>,
    inside: Vec<u32>,
    unknown: Vec<u32>,
    unsupported: Vec<u32>,
    key: Vec<u32>,
    /// Counters, when the work is wanted (`Model::work_points`).
    work: Option<Work>,
    /// Deepest level classified ([`Depth::Level`]); `None` for every level.
    level: Option<usize>,
}

/// How deep labels go: down to the cells that are not filled, or to a
/// universe level at most, as MCNP's plotter shows `LEVEL n`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Depth {
    /// Every level: paths end at a cell that is not filled.
    #[default]
    All,
    /// Down to universe level `n` (0 is the top): a filled cell at that
    /// level is a cell like any other (`Inside`, its path ending with it,
    /// and with the lattice element for a lattice cell), and nothing below
    /// it is looked at, so gaps and problems below are not seen.
    Level(usize),
}

impl Depth {
    /// The deepest level, `None` for every level.
    pub fn level(self) -> Option<usize> {
        match self {
            Depth::All => None,
            Depth::Level(n) => Some(n),
        }
    }
}

impl From<Option<usize>> for Depth {
    /// `None` is every level.
    fn from(level: Option<usize>) -> Depth {
        level.map_or(Depth::All, Depth::Level)
    }
}

impl Model {
    /// Classify points given in the global frame, down to `depth`: with
    /// [`Depth::Level`], labels are the full ones cut to `level + 1` steps
    /// (as [`Model::trace`]).
    ///
    /// Results are identical for any number of threads: label ids are
    /// assigned in point order.
    pub fn classify(&self, points: &[Vec3], depth: Depth) -> Classification {
        self.classify_impl(points, false, depth.level())
    }

    /// [`Model::classify`], or with `brute` the reference
    /// ([`crate::reference::classify`]).
    pub(crate) fn classify_impl(
        &self,
        points: &[Vec3],
        brute: bool,
        level: Option<usize>,
    ) -> Classification {
        // Each chunk interns its own labels; chunks are merged in parallel,
        // with ids numbered by first occurrence in point order.
        let scratch = || Scratch {
            level,
            ..Scratch::default()
        };
        let chunks: Vec<intern::Chunk> = points
            .par_chunks(CHUNK)
            .map_init(scratch, |s, chunk| {
                let mut out = intern::Chunk::default();
                for &p in chunk {
                    self.classify_into(p, s, brute);
                    out.push(&s.key);
                }
                out
            })
            .collect();
        let (ids, keys) = intern::merge(&chunks);
        let labels = keys.par_iter().map(|k| decode(k)).collect();
        Classification { ids, labels }
    }

    /// Classify one point (convenience for tests and small jobs).
    pub fn classify_point(&self, p: Vec3) -> Label {
        let mut s = Scratch::default();
        self.classify_into(p, &mut s, false);
        decode(&s.key)
    }

    /// Point `p`, whose label is `label`, in the frame of the universe that
    /// holds step `level` of its path (through the fills and lattice
    /// elements above it, as classification goes down); `p` itself for
    /// level 0. `None` if the path is not that deep.
    pub fn point_at_level(&self, label: &Label, level: usize, p: Vec3) -> Option<Vec3> {
        if level >= label.path.len().max(1) {
            return None;
        }
        let mut q = p;
        for s in &label.path[..level] {
            match self.descend(s.cell, q) {
                Descend::Into { point, .. } => q = point,
                _ => return None,
            }
        }
        Some(q)
    }

    /// Classify `p` as [`Model::classify`] does, adding its work to `w`.
    #[cfg(feature = "bench")]
    pub(crate) fn classify_counted(&self, p: Vec3, w: &mut Work) {
        let mut s = Scratch {
            work: Some(std::mem::take(w)),
            ..Scratch::default()
        };
        self.classify_into(p, &mut s, false);
        *w = s.work.take().unwrap();
    }

    /// Classify `p`, leaving its encoded label in `s.key`:
    /// `[status, nsteps, (cell, has_lattice, i, j, k)*, cells*]`.
    fn classify_into(&self, p: Vec3, s: &mut Scratch, brute: bool) {
        s.key.clear();
        s.key.push(0);
        s.key.push(0);
        let mut q = p;
        let mut unit = 0u32;
        for depth in 0..MAX_DEPTH {
            let u = &self.units[unit as usize];
            s.candidates.clear();
            s.items.clear();
            // Node boxes are only exact inside the region they were built for.
            let in_roi = u.roi.contains(q);
            let use_boxes = !brute && in_roi;
            if !brute && in_roi {
                self.candidates(u, q, &mut s.candidates, &mut s.items);
            } else {
                // Outside the region the boxes were clipped to (only possible
                // at level 0): every cell, or beyond the problem box only its
                // `IMP:N=0` cells (brute force follows the same rule).
                let cells = if in_roi { &u.cells } else { u.outside() };
                s.candidates.extend_from_slice(cells);
                s.items.resize(s.candidates.len(), NO_ITEM);
            }
            s.inside.clear();
            s.unknown.clear();
            s.unsupported.clear();
            if let Some(w) = s.work.as_mut() {
                let l = w.level(depth);
                l.visits += 1;
                l.candidates += s.candidates.len() as u64;
            }
            for (&c, &item) in s.candidates.iter().zip(&s.items) {
                let cell = &self.cells[c as usize];
                if cell.unsupported.is_some() {
                    s.unsupported.push(c);
                    continue;
                }
                if self.is_lattice(c) {
                    // A lattice fills all of its universe.
                    s.inside.push(c);
                    continue;
                }
                // The program specialized to the octree leaf holding `q`:
                // none when the cell is true over the whole leaf.
                let code = match (item, &u.grid) {
                    (NO_ITEM, _) | (_, None) => Some(self.code(c)),
                    (k, Some(g)) => g.prog(k as usize, self.code(c)),
                };
                let boxes = use_boxes.then(|| self.node_boxes_of(c));
                let v = match (code, s.work.as_mut()) {
                    (None, _) => Tri::True,
                    (Some(code), None) => self.eval_code(code, q, boxes),
                    (Some(code), Some(w)) => {
                        let mut n = 0;
                        let v = self.eval_code_counted(code, q, boxes, &mut n);
                        w.level(depth).surface_evals += n;
                        v
                    }
                };
                match v {
                    Tri::True => s.inside.push(c),
                    Tri::Unknown => s.unknown.push(c),
                    Tri::False => {}
                }
            }
            // Unsupported cells cannot be evaluated: they only decide points
            // that no supported cell claims (in a valid model a point claimed
            // by a supported cell cannot also be in them).
            let status = if s.inside.len() > 1 {
                Some((Status::Overlap, &mut s.inside))
            } else if s.inside.len() == 1 {
                None
            } else if !s.unsupported.is_empty() {
                Some((Status::Unsupported, &mut s.unsupported))
            } else if !s.unknown.is_empty() {
                Some((Status::OnSurface, &mut s.unknown))
            } else {
                Some((Status::Gap, &mut s.unknown))
            };
            if let Some((status, cells)) = status {
                cells.sort_unstable();
                s.key[0] = status as u32;
                s.key.extend_from_slice(cells);
                return;
            }
            let c = s.inside[0];
            s.key[1] += 1;
            s.key.push(c);
            match self.descend(c, q) {
                Descend::Stop => {
                    s.key.extend_from_slice(&[0, 0, 0, 0]);
                    s.key[0] = Status::Inside as u32;
                    return;
                }
                Descend::Element(ix, status) => {
                    // A lattice element filled by the lattice cell itself,
                    // or one outside the fill array.
                    s.key
                        .extend_from_slice(&[1, ix[0] as u32, ix[1] as u32, ix[2] as u32]);
                    s.key[0] = status as u32;
                    return;
                }
                Descend::Into {
                    unit: u,
                    point,
                    lattice,
                } => {
                    match lattice {
                        Some(ix) => {
                            s.key
                                .extend_from_slice(&[1, ix[0] as u32, ix[1] as u32, ix[2] as u32])
                        }
                        None => s.key.extend_from_slice(&[0, 0, 0, 0]),
                    }
                    // The deepest level asked for: the filled cell is a cell.
                    if s.level.is_some_and(|k| depth >= k) {
                        s.key[0] = Status::Inside as u32;
                        return;
                    }
                    q = point;
                    unit = u;
                }
            }
        }
        s.key[0] = Status::TooDeep as u32;
    }
}

impl Model {
    /// Value of a cell's program (whole, or specialized to an octree leaf
    /// holding `q`) at `q`.
    #[inline]
    pub(crate) fn eval_code(&self, code: &[u32], q: Vec3, boxes: Option<&[[Aabb; 2]]>) -> Tri {
        let tol = self.tolerance;
        expr::eval(code, q, boxes, &mut |surf, want_pos| match (
            self.shapes[surf as usize].side(q, tol),
            want_pos,
        ) {
            (Side::On, _) => Tri::Unknown,
            (Side::Pos, true) | (Side::Neg, false) => Tri::True,
            _ => Tri::False,
        })
    }

    /// [`Model::eval_code`], adding the surfaces it tests to `n`.
    pub(crate) fn eval_code_counted(
        &self,
        code: &[u32],
        q: Vec3,
        boxes: Option<&[[Aabb; 2]]>,
        n: &mut u64,
    ) -> Tri {
        let tol = self.tolerance;
        expr::eval(code, q, boxes, &mut |surf, want_pos| {
            *n += 1;
            match (self.shapes[surf as usize].side(q, tol), want_pos) {
                (Side::On, _) => Tri::Unknown,
                (Side::Pos, true) | (Side::Neg, false) => Tri::True,
                _ => Tri::False,
            }
        })
    }

    /// Cells of `u` that may hold `q` (in the unit's region): the octree
    /// leaf or the BVH, plus the cells checked everywhere; with, for each,
    /// its octree item or `NO_ITEM`.
    #[inline]
    fn candidates(&self, u: &Unit, q: Vec3, out: &mut Vec<u32>, items: &mut Vec<u32>) {
        match u.grid.as_ref().and_then(|g| g.leaf_span(q).map(|s| (g, s))) {
            Some((g, (first, n))) => {
                for k in first..first + n {
                    let c = g.items[k];
                    if self.cells[c as usize].bbox.contains(q) {
                        out.push(c);
                        items.push(k as u32);
                    }
                }
            }
            None => u.bvh.query(q, |c| out.push(c)),
        }
        out.extend_from_slice(&u.always);
        items.resize(out.len(), NO_ITEM);
    }
}

/// Where a point goes after the cell holding it.
enum Descend {
    /// The cell is not filled.
    Stop,
    /// A lattice element that ends the path, with the status to report.
    Element([i32; 3], Status),
    Into {
        unit: u32,
        point: Vec3,
        lattice: Option<[i32; 3]>,
    },
}

impl Model {
    #[inline]
    fn descend(&self, c: u32, q: Vec3) -> Descend {
        match &self.cells[c as usize].fill {
            None => Descend::Stop,
            Some(Fill::Universe(t)) => Descend::Into {
                unit: t.unit,
                point: t.apply(q),
                lattice: None,
            },
            Some(Fill::Lattice(l)) => {
                let lat = &self.lattices[*l as usize];
                let ix = lat.index(q);
                match lat.element(ix) {
                    Element::Fill(t) => Descend::Into {
                        unit: t.unit,
                        point: t.apply(lat.to_element(q, ix)),
                        lattice: Some(ix),
                    },
                    Element::Own => Descend::Element(ix, Status::Inside),
                    Element::Missing => Descend::Element(ix, Status::Gap),
                }
            }
        }
    }
}

pub(crate) fn decode(k: &[u32]) -> Label {
    let status = match k[0] {
        0 => Status::Inside,
        1 => Status::Gap,
        2 => Status::Overlap,
        3 => Status::OnSurface,
        4 => Status::Unsupported,
        _ => Status::TooDeep,
    };
    let n = k[1] as usize;
    let path = (0..n)
        .map(|i| {
            let w = &k[2 + 5 * i..7 + 5 * i];
            Step {
                cell: w[0],
                lattice: (w[1] == 1).then_some([w[2] as i32, w[3] as i32, w[4] as i32]),
            }
        })
        .collect();
    Label {
        status,
        path,
        involved: k[2 + 5 * n..].to_vec(),
    }
}

impl Label {
    /// The cell holding the point, for [`Status::Inside`].
    pub fn cell(&self) -> Option<u32> {
        (self.status == Status::Inside)
            .then(|| self.path.last().map(|s| s.cell))
            .flatten()
    }
}