indicatrix 0.8.1

A physically-based spectral gemstone renderer that turns GemCAD-style cutting instructions into rendered output.
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
//! Exact CSG ray intersection against a stone: a convex polyhedron minus a
//! list of convex tool volumes.
//!
//! With no tools both entry points return exactly what
//! [`intersect_polyhedron`] / [`intersect_polyhedron_soa`] return, so a design
//! without concave facets is bit-identical to before. With tools the slab
//! interval `[a, b]` of the polyhedron is computed by the same loop (copied
//! here as `slab_interval`, because `intersect.rs` is deliberately untouched),
//! each tool contributes its analytic interval `[c, d]`, and the hit is the
//! first boundary of `[a, b] \ (union of [c, d])` past the `1e-4` floor.

use super::{
    camera::{HitRecord, Ray},
    intersect::{intersect_polyhedron, intersect_polyhedron_soa},
};
use crate::{
    geometry::{
        plane::GpuFacetPlane,
        tool::{MAX_TOOL_PRIMITIVES, StoneGeometry, ToolPrimitive},
    },
    simd::{PlanesSoA32, SlabScan, slab_scan},
};
use glam::Vec3;

/// Hits at or before this parameter are the ray's own origin surface.
const T_FLOOR: f32 = 1e-4;

/// The polyhedron's slab interval with the facets that bound it, mirroring the
/// running state of `intersect_polyhedron`'s loop.
#[derive(Debug, Clone, Copy)]
struct Slab {
    t_near: f32,
    near: Option<usize>,
    t_far: f32,
    far: Option<usize>,
}

/// The slab loop of `intersect_polyhedron`, returning both ends and both
/// facets instead of choosing between them. `None` when the ray misses.
fn slab_interval(ray: Ray, planes: &[GpuFacetPlane]) -> Option<Slab> {
    let mut slab = Slab {
        t_near: -1e30,
        near: None,
        t_far: 1e30,
        far: None,
    };
    for (i, p) in planes.iter().enumerate() {
        let n = Vec3::from_array(p.normal);
        let denom = n.dot(ray.dir);
        let side = p.d + n.dot(ray.origin);
        let numer = -side;
        if denom.abs() > 1e-7 {
            let t = numer / denom;
            if denom < 0.0 {
                if t > slab.t_near {
                    slab.t_near = t;
                    slab.near = Some(i);
                }
            } else if t < slab.t_far {
                slab.t_far = t;
                slab.far = Some(i);
            }
        } else if side > 0.0 {
            return None;
        }
    }
    (slab.t_near <= slab.t_far).then_some(slab)
}

/// One boundary on the ray. `key` orders ties: plane entry `0`, plane exit
/// `1`, then tool `k` as `2 + 2k` (entry) and `3 + 2k` (exit), i.e. a plane
/// beats a tool and a lower tool index beats a higher one.
#[derive(Clone, Copy)]
struct Boundary {
    t: f32,
    key: u32,
}

/// Walks the sorted boundaries of `[a, b]` and every tool interval and returns
/// the first change of the "inside the stone" state past [`T_FLOOR`].
fn carve(
    ray: Ray,
    slab: Slab,
    plane_count: usize,
    tools: &[ToolPrimitive],
    plane_normal: impl Fn(usize) -> Vec3,
) -> Option<HitRecord> {
    debug_assert!(tools.len() <= MAX_TOOL_PRIMITIVES, "too many tools");
    // Excess tools are ignored rather than overflowing the stack array; the
    // validators reject such scenes before they get here.
    let tools = &tools[..tools.len().min(MAX_TOOL_PRIMITIVES)];

    let mut events = [Boundary { t: 0.0, key: 0 }; 2 * MAX_TOOL_PRIMITIVES + 2];
    events[0] = Boundary {
        t: slab.t_near,
        key: 0,
    };
    events[1] = Boundary {
        t: slab.t_far,
        key: 1,
    };
    let mut n = 2;
    for (k, tool) in tools.iter().enumerate() {
        if let Some((c, d)) = tool.ray_interval(ray) {
            let key = 2 + 2 * u32::try_from(k).expect("a stone has few tool primitives");
            events[n] = Boundary { t: c, key };
            events[n + 1] = Boundary { t: d, key: key + 1 };
            n += 2;
        }
    }
    let events = &mut events[..n];
    events.sort_unstable_by(|x, y| x.t.total_cmp(&y.t).then(x.key.cmp(&y.key)));

    let mut plane_depth = 0i32;
    let mut tool_depth = 0i32;
    let mut inside = false;
    for ev in events.iter() {
        match ev.key {
            0 => plane_depth += 1,
            1 => plane_depth -= 1,
            k if k % 2 == 0 => tool_depth += 1,
            _ => tool_depth -= 1,
        }
        let now_inside = plane_depth > 0 && tool_depth <= 0;
        if now_inside == inside {
            continue;
        }
        inside = now_inside;
        if ev.t <= T_FLOOR {
            continue;
        }
        return Some(match ev.key {
            0 | 1 => {
                let idx = if ev.key == 0 { slab.near } else { slab.far };
                HitRecord {
                    t: ev.t,
                    normal: idx.map_or(Vec3::ZERO, &plane_normal),
                    facet_idx: idx.unwrap_or(0),
                }
            }
            k => {
                let tool_idx = ((k - 2) / 2) as usize;
                let point = ray.origin + ray.dir * ev.t;
                HitRecord {
                    t: ev.t,
                    // The tool's outward normal points into the stone material;
                    // the stone's outward normal points into the cavity.
                    normal: -tools[tool_idx].outward_normal(point),
                    facet_idx: plane_count + tool_idx,
                }
            }
        });
    }
    None
}

/// Intersects a ray with the stone `geom` (its polyhedron minus every tool).
///
/// Returns the first surface the ray meets: the entry from outside, or the
/// exit/next surface for a ray that starts inside the material.
///
/// With no tools the result is `intersect_polyhedron`'s, bit for bit,
/// including the `t = 1e30` sentinel hit for an empty plane list. A tool hit
/// has `facet_idx == planes.len() + k`. A boundary is reported without saying
/// whether it is an entry or an exit, matching [`HitRecord`] today.
#[must_use]
pub fn intersect_stone(ray: Ray, geom: StoneGeometry<'_>) -> Option<HitRecord> {
    if geom.is_convex() {
        return intersect_polyhedron(ray, geom.planes);
    }
    let slab = slab_interval(ray, geom.planes)?;
    carve(ray, slab, geom.planes.len(), geom.tools, |i| {
        Vec3::from_array(geom.planes[i].normal)
    })
}

/// `SoA` twin used by transport: identical result to [`intersect_stone`].
///
/// Step 1 is the (bit-identical) SIMD `slab_scan`, so only the tool pass is
/// scalar; tools are few and a SIMD twin waits for a profile that asks for it.
pub(crate) fn intersect_stone_soa(
    ray: Ray,
    plane_soa: &PlanesSoA32,
    planes_len: usize,
    tools: &[ToolPrimitive],
) -> Option<HitRecord> {
    if tools.is_empty() {
        return intersect_polyhedron_soa(ray, plane_soa);
    }
    let SlabScan::Slab {
        t_near,
        near_idx,
        t_far,
        far_idx,
    } = slab_scan(plane_soa, ray.origin, ray.dir)
    else {
        return None;
    };
    if t_near > t_far {
        return None;
    }
    let slab = Slab {
        t_near,
        near: usize::try_from(near_idx).ok(),
        t_far,
        far: usize::try_from(far_idx).ok(),
    };
    carve(ray, slab, planes_len, tools, |i| plane_soa.normal(i))
}

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

    fn unit_cube_planes() -> Vec<GpuFacetPlane> {
        [
            (Vec3::X, -0.5),
            (Vec3::NEG_X, -0.5),
            (Vec3::Y, -0.5),
            (Vec3::NEG_Y, -0.5),
            (Vec3::Z, -0.5),
            (Vec3::NEG_Z, -0.5),
        ]
        .into_iter()
        .map(|(n, d)| GpuFacetPlane::new(n, d))
        .collect()
    }

    /// xorshift32, as elsewhere in this workspace.
    struct Rng(u32);

    impl Rng {
        fn next_u32(&mut self) -> u32 {
            let mut x = self.0;
            x ^= x << 13;
            x ^= x >> 17;
            x ^= x << 5;
            self.0 = x;
            x
        }

        fn signed(&mut self) -> f32 {
            (self.next_u32() >> 8) as f32 / (1u32 << 23) as f32 - 1.0
        }

        fn unit_vec(&mut self) -> Vec3 {
            loop {
                let v = Vec3::new(self.signed(), self.signed(), self.signed());
                let l = v.length();
                if (0.1..=1.0).contains(&l) {
                    return v / l;
                }
            }
        }
    }

    fn same_hit(a: Option<HitRecord>, b: Option<HitRecord>) -> bool {
        match (a, b) {
            (None, None) => true,
            (Some(a), Some(b)) => {
                a.t.to_bits() == b.t.to_bits()
                    && a.facet_idx == b.facet_idx
                    && a.normal.x.to_bits() == b.normal.x.to_bits()
                    && a.normal.y.to_bits() == b.normal.y.to_bits()
                    && a.normal.z.to_bits() == b.normal.z.to_bits()
            }
            _ => false,
        }
    }

    #[test]
    fn intersect_stone_with_no_tools_is_bit_identical_to_intersect_polyhedron() {
        let cases: [Vec<GpuFacetPlane>; 3] = [
            unit_cube_planes(),
            StandardGemCuts::standard_round_brilliant(),
            Vec::new(),
        ];
        let mut rng = Rng(0xC0FF_EE11);
        for planes in &cases {
            let soa = crate::optics::raytracer::build_plane_soa(planes);
            for i in 0..2_000 {
                // Alternate outside origins and (for the closed cases) inside
                // ones so both the entry and the exit branch are covered.
                let radius = if i % 2 == 0 { 3.0 } else { 0.2 };
                let ray = Ray {
                    origin: rng.unit_vec() * radius,
                    dir: rng.unit_vec(),
                };
                let reference = intersect_polyhedron(ray, planes);
                let got = intersect_stone(ray, StoneGeometry::planes_only(planes));
                assert!(same_hit(reference, got), "{reference:?} vs {got:?}");
                let got_soa = intersect_stone_soa(ray, &soa, planes.len(), &[]);
                assert!(
                    same_hit(intersect_polyhedron_soa(ray, &soa), got_soa),
                    "soa {got_soa:?}"
                );
            }
        }
        let ray = Ray {
            origin: Vec3::ZERO,
            dir: Vec3::NEG_Y,
        };
        let sentinel = intersect_stone(ray, StoneGeometry::planes_only(&[]))
            .expect("empty planes keep the 1e30 sentinel hit");
        assert!((sentinel.t - 1e30).abs() < 1.0);
    }

    #[test]
    fn grazing_restart_from_a_small_cylinder_does_not_self_intersect() {
        // A cylinder of radius 0.05 along Z, cut out of the unit cube. A ray
        // restarted on its surface heading outward must not report the surface
        // it stands on.
        let planes = unit_cube_planes();
        let tools = [ToolPrimitive::cylinder(Vec3::ZERO, Vec3::Z, 0.05, 2.0)];
        let geom = StoneGeometry {
            planes: &planes,
            tools: &tools,
        };
        let mut rng = Rng(0x1234_5678);
        for _ in 0..5_000 {
            let phi = (rng.signed() + 1.0) * std::f32::consts::PI;
            let radial = Vec3::new(phi.cos(), phi.sin(), 0.0);
            let origin = radial * 0.05 + Vec3::Z * (0.3 * rng.signed());
            // Outward, including grazing directions near the tangent plane.
            let tangent = Vec3::new(-radial.y, radial.x, 0.0);
            let lean = rng.signed().abs();
            let dir = (radial * (0.05 + lean) + tangent * rng.signed() + Vec3::Z * rng.signed())
                .normalize();
            if dir.dot(radial) <= 0.0 {
                continue;
            }
            let hit = intersect_stone(Ray { origin, dir }, geom).expect("leaves through the cube");
            assert!(
                hit.facet_idx < planes.len(),
                "restart re-hit the cylinder: {hit:?}"
            );
        }
    }

    #[test]
    fn stone_hit_normal_on_a_tool_points_into_the_cavity() {
        // A groove of radius 0.2 along Y through the cube's top, centred at
        // y-axis x = 0, z = 0.5 (half sunk). A ray coming straight down enters
        // the groove and meets the groove's far wall.
        let planes = unit_cube_planes();
        let tools = [ToolPrimitive::cylinder(
            Vec3::new(0.0, 0.5, 0.0),
            Vec3::Z,
            0.2,
            2.0,
        )];
        let geom = StoneGeometry {
            planes: &planes,
            tools: &tools,
        };
        for x in [-0.15f32, -0.05, 0.0, 0.1, 0.18] {
            let ray = Ray {
                origin: Vec3::new(x, 2.0, 0.1),
                dir: Vec3::NEG_Y,
            };
            let hit = intersect_stone(ray, geom).expect("hits the groove floor");
            assert_eq!(hit.facet_idx, planes.len(), "tool facet id");
            assert!(
                hit.normal.dot(ray.dir) < 0.0,
                "normal {:?} does not face the incoming ray",
                hit.normal
            );
            let expected_y = 0.5 - x.mul_add(-x, 0.2f32 * 0.2).sqrt();
            let point = ray.origin + ray.dir * hit.t;
            assert!((point.y - expected_y).abs() < 1e-4, "{point:?}");
        }
        // Outside the groove the ray still lands on the top plane (+Y, id 2).
        let ray = Ray {
            origin: Vec3::new(0.4, 2.0, 0.1),
            dir: Vec3::NEG_Y,
        };
        let hit = intersect_stone(ray, geom).expect("hits the top plane");
        assert_eq!(hit.facet_idx, 2);
    }

    #[test]
    fn soa_twin_matches_the_scalar_kernel_with_tools_on_seeded_rays() {
        let planes = StandardGemCuts::standard_round_brilliant();
        let tools =
            [
                ToolPrimitive::ball(Vec3::new(0.2, 0.3, 0.1), 0.25),
                ToolPrimitive::frustum(Vec3::new(-0.3, 0.0, 0.2), Vec3::Y, 0.1, 0.2, 0.3)
                    .with_sweep(crate::geometry::tool::ToolSweep::AlongAxis, 0.1, Vec3::ZERO),
            ];
        let soa = crate::optics::raytracer::build_plane_soa(&planes);
        let geom = StoneGeometry {
            planes: &planes,
            tools: &tools,
        };
        let mut rng = Rng(0xDEAD_BEEF);
        for i in 0..3_000 {
            let radius = if i % 2 == 0 { 4.0 } else { 0.3 };
            let ray = Ray {
                origin: rng.unit_vec() * radius,
                dir: rng.unit_vec(),
            };
            let a = intersect_stone(ray, geom);
            let b = intersect_stone_soa(ray, &soa, planes.len(), &tools);
            assert!(same_hit(a, b), "{a:?} vs {b:?}");
        }
    }
}