llevant 0.1.3

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
//! Repeated structures: hexahedral (`LAT=1`) and hexagonal (`LAT=2`) lattices.

use crate::bbox::Aabb;
use crate::expr;
use crate::surface::Shape;
use crate::transform::Affine;
use crate::vec3::{add, cross, dot, norm, scale, sub, Vec3};

/// What fills a cell or a lattice element: a universe, seen through a
/// transform from the filled cell's frame.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Target {
    /// Index into [`crate::Model::units`].
    pub(crate) unit: u32,
    pub(crate) transform: Option<Affine>,
}

impl Target {
    /// Index into [`Model::units`](crate::Model::units) of the universe.
    pub fn unit(&self) -> u32 {
        self.unit
    }

    /// The transform from the filled cell's frame to the universe's, when
    /// not the identity.
    pub fn transform(&self) -> Option<&Affine> {
        self.transform.as_ref()
    }

    /// A point of the filled cell's frame in the universe's frame.
    #[inline]
    pub fn apply(&self, p: Vec3) -> Vec3 {
        match &self.transform {
            Some(t) => t.apply(p),
            None => p,
        }
    }
}

/// How the elements of a lattice are filled.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub(crate) enum LatticeFill {
    /// Every element holds the same universe.
    Uniform(Target),
    /// A fill array over `lo ..= lo + size - 1`, `i` varying fastest. `None`
    /// is an element filled by the lattice cell itself (its own universe
    /// number in the array). Elements outside the range do not exist.
    Array {
        lo: [i32; 3],
        size: [i32; 3],
        elements: Vec<Option<Target>>,
    },
}

/// What lies in one lattice element.
#[derive(Clone, Copy, Debug)]
pub enum Element<'a> {
    Fill(&'a Target),
    /// Filled by the lattice cell itself (its material).
    Own,
    /// Outside the fill array: the element does not exist.
    Missing,
}

/// A lattice (`LAT=1` or `LAT=2`): element `[i,j,k]` is element `[0,0,0]` shifted by
/// `i·a + j·b + k·c`.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Lattice {
    pub(crate) hexagonal: bool,
    /// Centre of element `[0,0,0]`.
    pub(crate) center: Vec3,
    /// `a`, `b`, `c`; for a two-dimensional lattice `c` is the unit axis.
    pub(crate) basis: [Vec3; 3],
    /// `dual[i] · basis[j] = δ_ij`.
    pub(crate) dual: [Vec3; 3],
    /// False for the axis of a two-dimensional lattice.
    pub(crate) bounded: [bool; 3],
    /// Outward side planes of element `[0,0,0]` (`n·x ≤ d` inside), used to
    /// pick the hexagon a point falls in. The first of `faces`.
    pub(crate) sides: Vec<(Vec3, f64)>,
    /// Every outward plane of element `[0,0,0]` (sides, then the bases of a
    /// three-dimensional lattice) and the compiled surface of each. The
    /// planes tile space exactly and are where [`Lattice::index`] changes
    /// element (`tiling_faces`), not the input's rounded planes.
    pub(crate) faces: Vec<(Vec3, f64)>,
    pub(crate) face_surfaces: Vec<u32>,
    pub(crate) fill: LatticeFill,
}

impl Lattice {
    /// Whether the lattice is hexagonal (`LAT=2`).
    pub fn hexagonal(&self) -> bool {
        self.hexagonal
    }

    /// Index of the element containing `p` (in the lattice cell's frame).
    #[inline]
    pub fn index(&self, p: Vec3) -> [i32; 3] {
        let d = sub(p, self.center);
        let mut ix = [0i32; 3];
        for i in 0..3 {
            if self.bounded[i] {
                ix[i] = (dot(self.dual[i], d) + 0.5).floor() as i32;
            }
        }
        if self.hexagonal {
            // The parallelogram cell is not the hexagon: of the nearby
            // elements, take the one whose hexagon holds `p` most deeply.
            let (i0, j0) = (ix[0], ix[1]);
            let mut best = (f64::NEG_INFINITY, i0, j0);
            for di in -1..=1 {
                for dj in -1..=1 {
                    let (i, j) = (i0 + di, j0 + dj);
                    let q = sub(
                        sub(p, scale(self.basis[0], i as f64)),
                        scale(self.basis[1], j as f64),
                    );
                    let margin = self
                        .sides
                        .iter()
                        .map(|(n, dd)| dd - dot(*n, q))
                        .fold(f64::INFINITY, f64::min);
                    if margin > best.0 {
                        best = (margin, i, j);
                    }
                }
            }
            ix[0] = best.1;
            ix[1] = best.2;
        }
        ix
    }

    /// `p` moved into element `[0,0,0]`.
    #[inline]
    pub(crate) fn to_element(&self, p: Vec3, ix: [i32; 3]) -> Vec3 {
        let mut q = p;
        for i in 0..3 {
            if ix[i] != 0 {
                q = sub(q, scale(self.basis[i], ix[i] as f64));
            }
        }
        q
    }

    /// Box around element `ix` in the lattice cell's frame, padded outwards;
    /// unbounded along the world axes the axis of a two-dimensional lattice
    /// leans on. Corners of the element's planes, taken three at a time.
    pub(crate) fn element_box(&self, ix: [i32; 3]) -> Aabb {
        // Two planes across each unbounded axis, its extent dropped after.
        let mut planes = self.faces.clone();
        for i in 0..3 {
            if !self.bounded[i] {
                let a = self.basis[i];
                planes.push((a, dot(a, self.center) + 1.0));
                planes.push((scale(a, -1.0), 1.0 - dot(a, self.center)));
            }
        }
        let scale_len = planes
            .iter()
            .map(|(_, d)| d.abs())
            .fold(norm(self.center), f64::max)
            .max(1.0);
        let tol = 1e-9 * scale_len;
        let mut b = Aabb::EMPTY;
        let n = planes.len();
        for i in 0..n {
            for j in i + 1..n {
                for k in j + 1..n {
                    let Ok(p) = meet(planes[i], planes[j], planes[k]) else {
                        continue;
                    };
                    if planes.iter().all(|(m, d)| dot(*m, p) <= d + tol) {
                        b.grow_point(p);
                    }
                }
            }
        }
        if b.is_empty() {
            return Aabb::INFINITE;
        }
        let pad = 1e-6 * scale_len;
        let shift = (0..3).fold([0.0; 3], |s, i| add(s, scale(self.basis[i], ix[i] as f64)));
        let mut out = Aabb {
            lo: [0, 1, 2].map(|k| b.lo[k] - pad + shift[k]),
            hi: [0, 1, 2].map(|k| b.hi[k] + pad + shift[k]),
        };
        for i in 0..3 {
            if !self.bounded[i] {
                for k in 0..3 {
                    if self.basis[i][k].abs() > 1e-12 {
                        out.lo[k] = f64::NEG_INFINITY;
                        out.hi[k] = f64::INFINITY;
                    }
                }
            }
        }
        out
    }

    /// Range of `t` over which the line `o + t·d` lies in element `ix`, and
    /// the face it leaves through (an index into `faces`, `None` when it
    /// never leaves).
    pub(crate) fn element_range(
        &self,
        o: Vec3,
        d: Vec3,
        ix: [i32; 3],
    ) -> (f64, f64, Option<usize>) {
        let q = self.to_element(o, ix);
        let (mut lo, mut hi, mut exit) = (f64::NEG_INFINITY, f64::INFINITY, None);
        for (k, (n, off)) in self.faces.iter().enumerate() {
            let nd = dot(*n, d);
            let gap = off - dot(*n, q);
            if nd == 0.0 {
                if gap < 0.0 {
                    return (f64::INFINITY, f64::NEG_INFINITY, None);
                }
                continue;
            }
            let t = gap / nd;
            if nd > 0.0 {
                if t < hi {
                    hi = t;
                    exit = Some(k);
                }
            } else if t > lo {
                lo = t;
            }
        }
        (lo, hi, exit)
    }

    /// Elements around `ix` that share at least a corner with it.
    pub(crate) fn neighbours(&self, ix: [i32; 3]) -> impl Iterator<Item = [i32; 3]> + '_ {
        let k = if self.bounded[2] { 1 } else { 0 };
        (-1..=1).flat_map(move |di| {
            (-1..=1).flat_map(move |dj| {
                (-k..=k)
                    .filter(move |&dk| (di, dj, dk) != (0, 0, 0))
                    .map(move |dk| [ix[0] + di, ix[1] + dj, ix[2] + dk])
            })
        })
    }

    /// The fill array's lowest index and size along each axis, `None` for
    /// a lattice filled with one universe everywhere.
    pub fn fill_array(&self) -> Option<([i32; 3], [i32; 3])> {
        match &self.fill {
            LatticeFill::Uniform(_) => None,
            LatticeFill::Array { lo, size, .. } => Some((*lo, *size)),
        }
    }

    /// What lies in element `ix`.
    pub fn element(&self, ix: [i32; 3]) -> Element<'_> {
        match &self.fill {
            LatticeFill::Uniform(t) => Element::Fill(t),
            LatticeFill::Array { lo, size, elements } => {
                let mut flat = 0usize;
                for i in (0..3).rev() {
                    let k = ix[i] - lo[i];
                    if k < 0 || k >= size[i] {
                        return Element::Missing;
                    }
                    flat = flat * size[i] as usize + k as usize;
                }
                match &elements[flat] {
                    Some(t) => Element::Fill(t),
                    None => Element::Own,
                }
            }
        }
    }

    /// Every universe the lattice can hand points to, once per element
    /// that holds it.
    pub fn targets(&self) -> impl Iterator<Item = &Target> + '_ {
        let (one, array) = match &self.fill {
            LatticeFill::Uniform(t) => (Some(t), None),
            LatticeFill::Array { elements, .. } => (None, Some(elements)),
        };
        one.into_iter().chain(array.into_iter().flatten().flatten())
    }
}

/// Outward planes of a lattice element program, in MCNP surface order.
fn outward_planes(code: &[u32], shapes: &[Shape]) -> Result<Vec<(Vec3, f64)>, String> {
    if code
        .first()
        .is_some_and(|w| w & expr::NODE != 0 && w & expr::OR != 0)
    {
        return Err("lattice element must be an intersection of planes".into());
    }
    expr::leaves(code)
        .map(|(s, want_pos)| match shapes[s as usize] {
            Shape::Plane { n, d } if want_pos => Ok((scale(n, -1.0), -d)),
            Shape::Plane { n, d } => Ok((n, d)),
            _ => Err("lattice elements must be bounded by planes".to_string()),
        })
        .collect()
}

/// Point where three planes `n·x = d` meet.
fn meet(a: (Vec3, f64), b: (Vec3, f64), c: (Vec3, f64)) -> Result<Vec3, String> {
    let det = dot(a.0, cross(b.0, c.0));
    if det.abs() < 1e-12 {
        return Err("degenerate lattice element".into());
    }
    let v = add(
        add(scale(cross(b.0, c.0), a.1), scale(cross(c.0, a.0), b.1)),
        scale(cross(a.0, b.0), c.1),
    );
    Ok(scale(v, 1.0 / det))
}

fn dual_of(basis: &[Vec3; 3]) -> Result<[Vec3; 3], String> {
    let det = dot(basis[0], cross(basis[1], basis[2]));
    if det.abs() < 1e-12 {
        return Err("degenerate lattice element".into());
    }
    Ok([
        scale(cross(basis[1], basis[2]), 1.0 / det),
        scale(cross(basis[2], basis[0]), 1.0 / det),
        scale(cross(basis[0], basis[1]), 1.0 / det),
    ])
}

/// Mid-plane of two opposite outward planes: `n₁·x = (d₁ - d₂)/2`.
fn mid(p: (Vec3, f64), q: (Vec3, f64)) -> Result<(Vec3, f64), String> {
    if dot(p.0, q.0) > -1.0 + 1e-9 {
        return Err("lattice element sides must come in parallel opposite pairs".into());
    }
    Ok((p.0, 0.5 * (p.1 - q.1)))
}

/// Build a lattice from its element program. MCNP order: for `LAT=1` the
/// surfaces bound `[1,0,0]`, `[-1,0,0]`, `[0,1,0]`, `[0,-1,0]`, `[0,0,1]`,
/// `[0,0,-1]`; for `LAT=2`, `[1,0,0]`, `[-1,0,0]`, `[0,1,0]`, `[0,-1,0]`,
/// `[-1,1,0]`, `[1,-1,0]`, then `[0,0,1]`, `[0,0,-1]`. The last pair may be
/// missing (a two-dimensional lattice).
pub(crate) fn build(
    lat: u8,
    code: &[u32],
    shapes: &[Shape],
    fill: LatticeFill,
) -> Result<Lattice, String> {
    let planes = outward_planes(code, shapes)?;
    let face_surfaces: Vec<u32> = expr::leaves(code).map(|(s, _)| s).collect();
    let (sides, dims) = match (lat, planes.len()) {
        (1, 6) => (4, 3),
        (1, 4) => (4, 2),
        (2, 8) => (6, 3),
        (2, 6) => (6, 2),
        (_, n) => {
            return Err(format!(
                "LAT={lat} element with {n} surfaces is not supported"
            ))
        }
    };
    let m0 = mid(planes[0], planes[1])?;
    let m1 = mid(planes[2], planes[3])?;
    let axis = {
        let e = cross(planes[0].0, planes[2].0);
        scale(e, 1.0 / norm(e))
    };
    let (m2, axial) = if dims == 3 {
        let (top, bottom) = (planes[sides], planes[sides + 1]);
        (mid(top, bottom)?, Some((top, bottom)))
    } else {
        ((axis, 0.0), None)
    };
    let center = meet(m0, m1, m2)?;
    let step = |p: (Vec3, f64)| scale(p.0, 2.0 * (p.1 - dot(p.0, center)));
    let a = step(planes[0]);
    let b = step(planes[2]);
    let c = match axial {
        Some((top, bottom)) => {
            // Along the sides (the hexagon or rectangle axis), across the bases.
            let e = if dot(axis, top.0) < 0.0 {
                scale(axis, -1.0)
            } else {
                axis
            };
            scale(e, (top.1 + bottom.1) / dot(e, top.0))
        }
        None => axis,
    };
    let (hexagonal, center, basis, bounded) = if lat == 1 && dims == 3 {
        // A hexahedral element need not have its third translation along the
        // side axis: it is the edge vector between the two bases.
        let corner = meet(planes[1], planes[3], planes[5])?;
        let next = |k: usize| -> Result<Vec3, String> {
            let mut ps = [planes[1], planes[3], planes[5]];
            ps[k] = planes[2 * k];
            Ok(sub(meet(ps[0], ps[1], ps[2])?, corner))
        };
        let basis = [next(0)?, next(1)?, next(2)?];
        let center = add(corner, scale(add(add(basis[0], basis[1]), basis[2]), 0.5));
        (false, center, basis, [true, true, true])
    } else if lat == 1 {
        // Parallelogram prism: translations are its edges.
        let corner = meet(planes[1], planes[3], (axis, 0.0))?;
        let ea = sub(meet(planes[0], planes[3], (axis, 0.0))?, corner);
        let eb = sub(meet(planes[1], planes[2], (axis, 0.0))?, corner);
        let center = add(corner, scale(add(ea, eb), 0.5));
        (false, center, [ea, eb, axis], [true, true, false])
    } else {
        (true, center, [a, b, c], [true, true, dims == 3])
    };
    let dual = dual_of(&basis)?;
    let faces = tiling_faces(hexagonal, center, &basis, &dual, &planes);
    Ok(Lattice {
        hexagonal,
        center,
        dual,
        basis,
        bounded,
        sides: faces[..sides].to_vec(),
        faces,
        face_surfaces,
        fill,
    })
}

/// The element's faces as planes that tile space exactly, in the order of
/// `planes` (the input's, MCNP order). Element `[0,0,0]` translated by the
/// basis must leave no gap or overlap, and its faces must be where
/// [`Lattice::index`] changes element, or a line walked face to face (as
/// tracing does) and a point located on it can disagree near an edge. The
/// input's planes do neither exactly: their normals are rounded (a hexagon's
/// third pair of sides is not quite at 60° to the others). So each face is
/// rebuilt from the basis: across a translation `v` of a hexagon's sides,
/// the plane halfway to the neighbour's centre (`n = v/|v|`); across a
/// translation the index takes from the dual basis (hexahedral elements, the
/// bases of a hexagonal prism), the plane where that index changes. The
/// faces move by the rounding of the input, far below the tolerance.
fn tiling_faces(
    hexagonal: bool,
    center: Vec3,
    basis: &[Vec3; 3],
    dual: &[Vec3; 3],
    planes: &[(Vec3, f64)],
) -> Vec<(Vec3, f64)> {
    let halfway = |v: Vec3| {
        let n = scale(v, 1.0 / norm(v));
        (n, dot(n, center) + 0.5 * norm(v))
    };
    let across_dual = |k: usize, sign: f64| {
        let len = norm(dual[k]);
        let n = scale(dual[k], sign / len);
        (n, dot(n, center) + 0.5 / len)
    };
    let neg = |v: Vec3| scale(v, -1.0);
    let mut faces = Vec::with_capacity(planes.len());
    if hexagonal {
        let (a, b) = (basis[0], basis[1]);
        let third = sub(b, a);
        for v in [a, neg(a), b, neg(b), third, neg(third)] {
            faces.push(halfway(v));
        }
        if planes.len() == 8 {
            faces.push(across_dual(2, 1.0));
            faces.push(across_dual(2, -1.0));
        }
    } else {
        for k in 0..planes.len() / 2 {
            faces.push(across_dual(k, 1.0));
            faces.push(across_dual(k, -1.0));
        }
    }
    faces
}