use glam::{DVec3, IVec3};
use roxlap_formats::material::material_for_color;
use roxlap_scene::{FogOfWar, GridId, Rgb, Scene, VoxColor};
mod backend;
mod cavity;
pub mod synth;
#[cfg(feature = "kira")]
mod kira_out;
pub use backend::{
AudioOut, SoundKey, SourceId, SourcePool, LISTENER_LOWPASS_OPEN_HZ,
LISTENER_LOWPASS_SUBMERGED_HZ,
};
pub use cavity::{probe_cavity, CavityConfig, CavityEstimator, CavityProbe, ListenerAcoustics};
pub use synth::SoundBuffer;
#[cfg(feature = "kira")]
pub use kira_out::{KiraAudio, DEFAULT_POOL};
#[derive(Debug, Clone, PartialEq)]
pub struct AcousticsConfig {
pub rays: u32,
pub jitter_radius: f64,
pub absorption_per_voxel: f64,
pub open_cutoff_hz: f32,
pub occluded_cutoff_hz: f32,
pub max_occlusion_db: f32,
pub send_occlusion_frac: f32,
pub max_distance: f64,
pub material_map: Vec<(Rgb, u8)>,
pub absorption: Vec<(u8, f32)>,
}
pub const OPEN_CUTOFF_HZ: f32 = 20_000.0;
impl Default for AcousticsConfig {
fn default() -> Self {
Self {
rays: 9,
jitter_radius: 1.5,
absorption_per_voxel: 0.25,
open_cutoff_hz: OPEN_CUTOFF_HZ,
occluded_cutoff_hz: 800.0,
max_occlusion_db: -24.0,
send_occlusion_frac: 0.5,
max_distance: 128.0,
material_map: Vec::new(),
absorption: Vec::new(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SourceAcoustics {
pub transmission: f32,
pub gain_db: f32,
pub lowpass_cutoff_hz: f32,
pub reverb_send_db: f32,
}
impl SourceAcoustics {
#[must_use]
pub fn clear(cfg: &AcousticsConfig) -> Self {
Self {
transmission: 1.0,
gain_db: 0.0,
lowpass_cutoff_hz: cfg.open_cutoff_hz,
reverb_send_db: 0.0,
}
}
}
pub const DEFAULT_SPEED_OF_SOUND: f64 = 90.0;
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub fn doppler_factor(
source_pos: DVec3,
source_vel: DVec3,
listener_pos: DVec3,
listener_vel: DVec3,
speed_of_sound: f64,
) -> f32 {
let seg = listener_pos - source_pos;
let dist = seg.length();
if dist < 1e-9 || speed_of_sound <= 1e-9 {
return 1.0;
}
let n = seg / dist;
let numer = (speed_of_sound - listener_vel.dot(n)).max(0.0);
let denom = (speed_of_sound - source_vel.dot(n)).max(speed_of_sound * 0.05);
((numer / denom) as f32).clamp(0.5, 2.0)
}
#[must_use]
pub fn path_thickness(scene: &Scene, a: DVec3, b: DVec3) -> f64 {
path_thickness_impl(scene, a, b, None)
}
#[must_use]
pub fn path_thickness_weighted(
scene: &Scene,
a: DVec3,
b: DVec3,
material_map: &[(Rgb, u8)],
absorption: &[(u8, f32)],
) -> f64 {
if absorption.is_empty() {
return path_thickness_impl(scene, a, b, None);
}
path_thickness_impl(
scene,
a,
b,
Some(&Weights {
map: material_map,
table: absorption,
}),
)
}
pub fn hear_source(
scene: &Scene,
fow: &mut FogOfWar,
fog_grid: GridId,
source: DVec3,
listener: DVec3,
base_loudness: f32,
cfg: &AcousticsConfig,
) -> f32 {
let Some((transform, footprint)) = scene
.grid(fog_grid)
.and_then(|g| g.footprint_cells().map(|fp| (g.transform, fp)))
else {
return 0.0;
};
let ac = source_acoustics(scene, source, listener, cfg);
let loudness = base_loudness * ac.transmission;
if loudness > 0.0 && fow.hear_world(&transform, footprint, source, loudness) {
loudness
} else {
0.0
}
}
fn path_thickness_impl(scene: &Scene, a: DVec3, b: DVec3, weights: Option<&Weights<'_>>) -> f64 {
let mut total = 0.0;
for (_, grid) in scene.query_grids() {
let vws = grid.transform.voxel_world_size;
let inv = grid.transform.rotation.inverse();
let la = (inv * (a - grid.transform.origin)) / vws;
let lb = (inv * (b - grid.transform.origin)) / vws;
total += grid_thickness(grid, la, lb, weights) * vws;
}
total
}
struct Weights<'a> {
map: &'a [(Rgb, u8)],
table: &'a [(u8, f32)],
}
impl Weights<'_> {
fn resolve(&self, c: VoxColor) -> f64 {
let id = material_for_color(self.map, c.0);
self.table
.iter()
.find(|&&(m, _)| m == id)
.map_or(1.0, |&(_, w)| f64::from(w))
}
}
fn grid_thickness(
grid: &roxlap_scene::Grid,
a: DVec3,
b: DVec3,
weights: Option<&Weights<'_>>,
) -> f64 {
let seg = b - a;
let len = seg.length();
if len < 1e-9 {
return 0.0;
}
let dir = seg / len;
let mut last_weight = 1.0f64;
#[allow(clippy::cast_possible_truncation)]
let mut cell = IVec3::new(a.x.floor() as i32, a.y.floor() as i32, a.z.floor() as i32);
let mut t_max = [f64::INFINITY; 3];
let mut t_delta = [f64::INFINITY; 3];
let mut step = [0i32; 3];
for ax in 0..3 {
let d = dir[ax];
if d.abs() < 1e-12 {
continue;
}
step[ax] = if d > 0.0 { 1 } else { -1 };
t_delta[ax] = 1.0 / d.abs();
let cell_f = f64::from(cell[ax]);
let next_boundary = if d > 0.0 { cell_f + 1.0 } else { cell_f };
t_max[ax] = (next_boundary - a[ax]) / d;
}
let mut thickness = 0.0;
let mut t_prev = 0.0;
let mut cached_idx = IVec3::MAX;
let mut cached_chunk = None;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let max_steps = (len * 3.0) as u32 + 8;
for _ in 0..max_steps {
let ax = min_axis(&t_max);
let t_next = t_max[ax].min(len);
let (chunk_idx, in_chunk) = roxlap_scene::voxel_split(cell);
if chunk_idx != cached_idx {
cached_idx = chunk_idx;
cached_chunk = grid.chunk(chunk_idx);
}
let solid =
cached_chunk.is_some_and(|vxl| roxlap_scene::Grid::chunk_voxel_solid(vxl, in_chunk));
if solid {
let w = match weights {
None => 1.0,
Some(ws) => {
if let Some(c) = cached_chunk
.and_then(|vxl| vxl.voxel_color(in_chunk.x, in_chunk.y, in_chunk.z))
{
last_weight = ws.resolve(c);
}
last_weight
}
};
thickness += (t_next - t_prev) * w;
}
if t_max[ax] >= len {
break;
}
t_prev = t_next;
cell[ax] += step[ax];
t_max[ax] += t_delta[ax];
}
thickness
}
fn min_axis(t: &[f64; 3]) -> usize {
if t[0] <= t[1] && t[0] <= t[2] {
0
} else if t[1] <= t[2] {
1
} else {
2
}
}
#[must_use]
pub fn source_acoustics(
scene: &Scene,
source: DVec3,
listener: DVec3,
cfg: &AcousticsConfig,
) -> SourceAcoustics {
let seg = listener - source;
let dist = seg.length();
if dist > cfg.max_distance || dist < 1e-9 {
return SourceAcoustics::clear(cfg);
}
let dir = seg / dist;
let helper = if dir.x.abs() < 0.9 {
DVec3::X
} else {
DVec3::Y
};
let u = dir.cross(helper).normalize();
let v = dir.cross(u);
let rays = cfg.rays.max(1);
let mut energy = 0.0f64;
for i in 0..rays {
let origin = if i == 0 {
source
} else {
let ang = std::f64::consts::TAU * f64::from(i - 1) / f64::from(rays - 1);
source + (u * ang.cos() + v * ang.sin()) * cfg.jitter_radius
};
let t =
path_thickness_weighted(scene, origin, listener, &cfg.material_map, &cfg.absorption);
energy += (-cfg.absorption_per_voxel * t).exp();
}
#[allow(clippy::cast_possible_truncation)]
let transmission = (energy / f64::from(rays)).clamp(0.0, 1.0) as f32;
let ratio = cfg.open_cutoff_hz / cfg.occluded_cutoff_hz;
let lowpass_cutoff_hz = cfg.occluded_cutoff_hz * ratio.powf(transmission);
let gain_db = cfg.max_occlusion_db * (1.0 - transmission);
SourceAcoustics {
transmission,
gain_db,
lowpass_cutoff_hz,
reverb_send_db: gain_db * cfg.send_occlusion_frac,
}
}
#[cfg(test)]
mod tests {
use super::*;
use roxlap_scene::{GridTransform, VoxColor};
const STONE: VoxColor = VoxColor(0x80_66_77_88);
fn scene_with(build: impl FnOnce(&mut roxlap_scene::Grid)) -> Scene {
let mut scene = Scene::new();
let id = scene.add_grid(GridTransform::identity());
build(scene.grid_mut(id).expect("grid just added"));
scene
}
fn wall(grid: &mut roxlap_scene::Grid, x0: i32, thick: i32) {
grid.set_rect(
IVec3::new(x0, 0, 100),
IVec3::new(x0 + thick - 1, 127, 180),
Some(STONE),
);
}
#[test]
fn open_air_is_clear() {
let scene = scene_with(|_| {});
let cfg = AcousticsConfig::default();
let a = source_acoustics(
&scene,
DVec3::new(10.0, 40.0, 140.0),
DVec3::new(60.0, 40.0, 140.0),
&cfg,
);
assert_eq!(a, SourceAcoustics::clear(&cfg));
}
#[test]
fn hear_source_scales_reveal_by_transmission() {
use roxlap_scene::{DeckBand, FogOfWar, VisionConfig};
let deck = DeckBand {
z_top: 90,
z_bottom: 200,
};
let mk_fow = || FogOfWar::new(VisionConfig::for_decks(vec![deck]));
let cfg = AcousticsConfig::default();
let src = DVec3::new(20.5, 5.5, 140.5);
let listener = DVec3::new(60.0, 5.0, 140.0);
let footprint =
|g: &mut roxlap_scene::Grid| g.gpu_residency_hint = Some(([0, 0, 0], [1, 1, 1]));
let mut open = Scene::new();
let gid = open.add_grid(GridTransform::identity());
footprint(open.grid_mut(gid).unwrap());
let mut fow = mk_fow();
let loud = hear_source(&open, &mut fow, gid, src, listener, 1.0, &cfg);
assert!(loud > 0.0, "open-air source is audible ({loud})");
let grid = open.grids().next().expect("one grid").1;
fow.update(
grid,
&roxlap_scene::FowObserver {
cell: glam::IVec2::new(0, 0),
facing: -glam::Vec2::X,
deck: 0,
eye_z: 102,
},
0.1,
);
assert_eq!(
fow.state(0, glam::IVec2::new(20, 5)).0,
roxlap_scene::CellState::Heard,
"the heard source lights its cell"
);
let mut walled = Scene::new();
let wgid = walled.add_grid(GridTransform::identity());
{
let g = walled.grid_mut(wgid).unwrap();
wall(g, 30, 8);
footprint(g);
}
let mut fow2 = mk_fow();
let loud2 = hear_source(&walled, &mut fow2, wgid, src, listener, 1.0, &cfg);
assert!(loud2 < loud, "walled source is muffled: {loud2} vs {loud}");
let mut fow3 = mk_fow();
let out = hear_source(
&open,
&mut fow3,
gid,
DVec3::new(500.5, 500.5, 140.5),
listener,
1.0,
&cfg,
);
assert_eq!(out, 0.0, "off-footprint source reveals nothing");
}
#[test]
fn thickness_is_exact_for_perpendicular_and_diagonal_crossings() {
let scene = scene_with(|g| wall(g, 30, 3));
let t = path_thickness(
&scene,
DVec3::new(10.0, 40.5, 140.5),
DVec3::new(60.0, 40.5, 140.5),
);
assert!((t - 3.0).abs() < 1e-6, "perpendicular thickness: {t}");
let t = path_thickness(
&scene,
DVec3::new(10.0, 20.5, 140.5),
DVec3::new(60.0, 70.5, 140.5),
);
assert!((t - 3.0 * 2.0f64.sqrt()).abs() < 1e-6, "diagonal: {t}");
}
#[test]
fn thicker_walls_muffle_monotonically() {
let cfg = AcousticsConfig::default();
let src = DVec3::new(10.0, 64.0, 140.5);
let dst = DVec3::new(60.0, 64.0, 140.5);
let mut prev = 1.0f32;
for thick in [1, 2, 5] {
let scene = scene_with(|g| wall(g, 30, thick));
let a = source_acoustics(&scene, src, dst, &cfg);
assert!(
a.transmission < prev,
"{thick}-voxel wall must transmit less than the previous ({} vs {prev})",
a.transmission
);
assert!(a.gain_db < 0.0 && a.gain_db >= cfg.max_occlusion_db);
assert!(a.lowpass_cutoff_hz < cfg.open_cutoff_hz);
assert!(a.lowpass_cutoff_hz > cfg.occluded_cutoff_hz);
prev = a.transmission;
}
}
#[test]
fn doorway_leaks_more_than_sealed_wall() {
let src = DVec3::new(10.0, 64.0, 140.5);
let dst = DVec3::new(60.0, 64.0, 140.5);
let cfg = AcousticsConfig::default();
let sealed = scene_with(|g| wall(g, 30, 2));
let sealed_a = source_acoustics(&sealed, src, dst, &cfg);
let doorway = scene_with(|g| {
wall(g, 30, 2);
g.set_rect(IVec3::new(30, 63, 139), IVec3::new(31, 63, 141), None);
});
let door_a = source_acoustics(&doorway, src, dst, &cfg);
assert!(
door_a.transmission > sealed_a.transmission,
"doorway must leak: {} vs sealed {}",
door_a.transmission,
sealed_a.transmission
);
assert!(
door_a.transmission < 1.0,
"off-axis doorway is not fully clear"
);
}
#[test]
fn cross_chunk_wall_counts_once() {
let scene = scene_with(|g| {
g.set_rect(
IVec3::new(126, 0, 100),
IVec3::new(129, 255, 180),
Some(STONE),
);
});
let t = path_thickness(
&scene,
DVec3::new(100.0, 64.5, 140.5),
DVec3::new(160.0, 64.5, 140.5),
);
assert!((t - 4.0).abs() < 1e-6, "seam-straddling wall: {t}");
}
#[test]
fn cross_grid_thickness_sums() {
let mut scene = Scene::new();
let id_a = scene.add_grid(GridTransform::identity());
wall(scene.grid_mut(id_a).expect("grid a"), 20, 2);
let id_b = scene.add_grid(GridTransform::at(DVec3::new(20.0, 0.0, 0.0)));
wall(scene.grid_mut(id_b).expect("grid b"), 20, 2); let t = path_thickness(
&scene,
DVec3::new(10.0, 64.5, 140.5),
DVec3::new(60.0, 64.5, 140.5),
);
assert!((t - 4.0).abs() < 1e-6, "two 2-voxel walls: {t}");
}
#[test]
fn beyond_max_distance_reports_clear_not_buried() {
let scene = scene_with(|g| wall(g, 30, 5));
let cfg = AcousticsConfig::default();
let a = source_acoustics(
&scene,
DVec3::new(10.0, 64.0, 140.5),
DVec3::new(10.0 + cfg.max_distance + 10.0, 64.0, 140.5),
&cfg,
);
assert_eq!(a, SourceAcoustics::clear(&cfg));
}
#[test]
fn rotated_grid_thickness_is_world_true() {
use glam::{DQuat, DVec3};
let mut scene = Scene::new();
let rot = DQuat::from_rotation_z(std::f64::consts::FRAC_PI_2);
let id = scene.add_grid(GridTransform {
origin: DVec3::new(0.0, 0.0, 0.0),
rotation: rot,
voxel_world_size: 1.0,
});
wall(scene.grid_mut(id).expect("rotated grid"), 30, 3);
let t = path_thickness(
&scene,
DVec3::new(-40.5, 10.0, 140.5),
DVec3::new(-40.5, 60.0, 140.5),
);
assert!(
(t - 3.0).abs() < 1e-6,
"rotated-grid wall must stay 3 world voxels thick: {t}"
);
let control = scene_with(|g| wall(g, 30, 3));
let t = path_thickness(
&control,
DVec3::new(-40.5, 10.0, 140.5),
DVec3::new(-40.5, 60.0, 140.5),
);
assert!(t.abs() < 1e-9, "identity control must miss: {t}");
}
#[test]
fn scaled_grid_thickness_is_world_scaled() {
let mut scene = Scene::new();
let id = scene.add_grid(GridTransform::at_scale(DVec3::ZERO, 2.0));
wall(scene.grid_mut(id).expect("scaled grid"), 30, 3);
let t = path_thickness(
&scene,
DVec3::new(40.0, 80.0, 280.0),
DVec3::new(140.0, 80.0, 280.0),
);
assert!(
(t - 6.0).abs() < 1e-6,
"vws=2 wall of 3 voxels must be 6 world units thick: {t}"
);
}
#[test]
fn empty_chunk_bottom_plane_is_acoustically_air() {
let scene = scene_with(|g| {
g.ensure_chunk(IVec3::ZERO);
});
let t = path_thickness(
&scene,
DVec3::new(10.0, 64.0, 255.5),
DVec3::new(20.0, 64.0, 255.5),
);
assert!(
t.abs() < 1e-9,
"empty chunk must be acoustically transparent, got thickness {t}"
);
}
#[test]
fn deterministic() {
let scene = scene_with(|g| wall(g, 30, 2));
let cfg = AcousticsConfig::default();
let src = DVec3::new(10.0, 64.0, 140.5);
let dst = DVec3::new(60.0, 64.0, 140.5);
let a = source_acoustics(&scene, src, dst, &cfg);
let b = source_acoustics(&scene, src, dst, &cfg);
assert_eq!(a, b);
}
const GLASS: VoxColor = VoxColor(0x80_40_C0_E0);
const GLASS_ID: u8 = 7;
fn thin_wall(grid: &mut roxlap_scene::Grid, x0: i32, thick: i32, c: VoxColor) {
grid.set_rect(
IVec3::new(x0, 0, 140),
IVec3::new(x0 + thick - 1, 127, 140),
Some(c),
);
}
#[test]
fn empty_tables_are_bit_identical() {
let scene = scene_with(|g| wall(g, 30, 3));
let src = DVec3::new(10.0, 64.0, 140.5);
let dst = DVec3::new(60.0, 64.0, 140.5);
let plain = path_thickness(&scene, src, dst);
let weighted =
path_thickness_weighted(&scene, src, dst, &[(GLASS.rgb_part(), GLASS_ID)], &[]);
assert_eq!(
plain.to_bits(),
weighted.to_bits(),
"empty absorption = legacy path"
);
let cfg = AcousticsConfig {
material_map: vec![(GLASS.rgb_part(), GLASS_ID)],
..AcousticsConfig::default()
};
let a = source_acoustics(&scene, src, dst, &AcousticsConfig::default());
let b = source_acoustics(&scene, src, dst, &cfg);
assert_eq!(a, b, "map without absorption changes nothing");
}
#[test]
fn glass_wall_transmits_more_than_stone() {
let scene = scene_with(|g| thin_wall(g, 30, 4, GLASS));
let src = DVec3::new(10.0, 64.0, 140.5);
let dst = DVec3::new(60.0, 64.0, 140.5);
let map = [(GLASS.rgb_part(), GLASS_ID)];
let table = [(GLASS_ID, 0.3f32)];
let plain = path_thickness(&scene, src, dst);
let weighted = path_thickness_weighted(&scene, src, dst, &map, &table);
assert!((plain - 4.0).abs() < 1e-6, "4 voxels of wall: {plain}");
assert!(
(weighted - plain * 0.3).abs() < 1e-6,
"glass counts at 0.3×: {weighted} vs {plain}"
);
let mut cfg = AcousticsConfig::default();
let base = source_acoustics(&scene, src, dst, &cfg);
cfg.material_map = map.to_vec();
cfg.absorption = table.to_vec();
let glass = source_acoustics(&scene, src, dst, &cfg);
assert!(
glass.transmission > base.transmission,
"glass passes more sound: {} vs {}",
glass.transmission,
base.transmission
);
cfg.absorption = vec![(GLASS_ID + 1, 0.3)];
let unmapped = source_acoustics(&scene, src, dst, &cfg);
assert_eq!(unmapped.transmission, base.transmission);
}
#[test]
#[ignore = "manual perf probe — cargo test -p roxlap-audio --release weighted_tables_probe -- --ignored --nocapture"]
fn weighted_tables_probe() {
let scene = scene_with(|g| {
for x in [20, 30, 40, 50] {
wall(g, x, 3);
}
});
let src = DVec3::new(10.0, 64.0, 140.5);
let dst = DVec3::new(60.0, 64.0, 140.5);
let plain_cfg = AcousticsConfig::default();
let glass_cfg = AcousticsConfig {
material_map: vec![(GLASS.rgb_part(), GLASS_ID)],
absorption: vec![(GLASS_ID, 0.35)],
..AcousticsConfig::default()
};
const N: u32 = 1_000;
let time = |cfg: &AcousticsConfig| {
let t0 = std::time::Instant::now();
for _ in 0..N {
std::hint::black_box(source_acoustics(&scene, src, dst, cfg));
}
t0.elapsed() / N
};
eprintln!(
"source_acoustics, 4 walls, tables empty: {:?}",
time(&plain_cfg)
);
eprintln!(
"source_acoustics, 4 walls, tables ACTIVE: {:?}",
time(&glass_cfg)
);
let cav_plain = crate::CavityConfig::default();
let cav_glass = crate::CavityConfig {
material_map: vec![(GLASS.rgb_part(), GLASS_ID)],
damping_override: vec![(GLASS_ID, 0.1)],
..crate::CavityConfig::default()
};
let listener = DVec3::new(25.0, 64.0, 140.5);
let time_cav = |cfg: &crate::CavityConfig| {
let t0 = std::time::Instant::now();
for _ in 0..N {
std::hint::black_box(crate::probe_cavity(&scene, listener, cfg));
}
t0.elapsed() / N
};
eprintln!("probe_cavity, tables empty: {:?}", time_cav(&cav_plain));
eprintln!("probe_cavity, tables ACTIVE: {:?}", time_cav(&cav_glass));
}
#[test]
fn doppler_signs_and_rest() {
let c = DEFAULT_SPEED_OF_SOUND;
let s = DVec3::new(0.0, 0.0, 0.0);
let l = DVec3::new(50.0, 0.0, 0.0);
let at_rest = doppler_factor(s, DVec3::ZERO, l, DVec3::ZERO, c);
assert_eq!(at_rest.to_bits(), 1.0f32.to_bits(), "rest is exactly 1");
let up = doppler_factor(s, DVec3::new(30.0, 0.0, 0.0), l, DVec3::ZERO, c);
let down = doppler_factor(s, DVec3::new(-30.0, 0.0, 0.0), l, DVec3::ZERO, c);
assert!(up > 1.0 && down < 1.0, "source motion: {up} / {down}");
let l_up = doppler_factor(s, DVec3::ZERO, l, DVec3::new(-30.0, 0.0, 0.0), c);
let l_down = doppler_factor(s, DVec3::ZERO, l, DVec3::new(30.0, 0.0, 0.0), c);
assert!(
l_up > 1.0 && l_down < 1.0,
"listener motion: {l_up} / {l_down}"
);
let perp = doppler_factor(s, DVec3::new(0.0, 30.0, 0.0), l, DVec3::ZERO, c);
assert!((perp - 1.0).abs() < 1e-6, "perpendicular ≈ neutral: {perp}");
}
#[test]
fn doppler_clamps_and_degenerates() {
let c = DEFAULT_SPEED_OF_SOUND;
let s = DVec3::ZERO;
let l = DVec3::new(50.0, 0.0, 0.0);
let mach2 = doppler_factor(s, DVec3::new(2.0 * c, 0.0, 0.0), l, DVec3::ZERO, c);
assert_eq!(mach2, 2.0);
let away = doppler_factor(s, DVec3::new(-3.0 * c, 0.0, 0.0), l, DVec3::ZERO, c);
assert_eq!(away, 0.5);
let flee = doppler_factor(s, DVec3::ZERO, l, DVec3::new(2.0 * c, 0.0, 0.0), c);
assert_eq!(flee, 0.5);
assert_eq!(doppler_factor(s, DVec3::X, s, DVec3::ZERO, c), 1.0);
assert_eq!(doppler_factor(s, DVec3::X, l, DVec3::ZERO, 0.0), 1.0);
}
#[test]
fn interior_inherits_surface_colour() {
let scene = scene_with(|g| {
g.set_rect(
IVec3::new(40, 60, 100),
IVec3::new(44, 68, 180),
Some(GLASS),
);
});
let g = scene.grids().next().expect("one grid").1;
assert_eq!(
g.voxel_color(IVec3::new(42, 64, 140)),
None,
"test premise: interior cells carry no colour"
);
let src = DVec3::new(42.5, 64.5, 90.0); let dst = DVec3::new(42.5, 64.5, 140.0); let map = [(GLASS.rgb_part(), GLASS_ID)];
let table = [(GLASS_ID, 0.3f32)];
let plain = path_thickness(&scene, src, dst);
let weighted = path_thickness_weighted(&scene, src, dst, &map, &table);
assert!((plain - 40.0).abs() < 1e-6, "40 buried voxels: {plain}");
assert!(
(weighted - plain * 0.3).abs() < 1e-6,
"interior inherited the glass surface weight: {weighted}"
);
}
}