use serde_json::json;
use crate::raster::color::heat;
use crate::raster::deck::{Agent, DeckWorld};
use crate::raster::{Face, Node3, Rgb, SceneGraph3D, Vertex3};
use crate::viz::NodeStyle;
use super::cylinder::spectral_positions;
use super::InstrumentOpts;
pub const N: usize = 34;
pub const M: usize = 24;
pub const S: f64 = 1.35;
pub const EPS: f64 = 0.02;
const BLOC_COLORS: [Rgb; 3] = [Rgb(242, 197, 107), Rgb(233, 223, 201), Rgb(232, 90, 68)];
pub fn agent_tension(a: &Agent, repression: f64) -> f64 {
(repression * (a.subsistence - a.wealth)).max(0.0)
}
#[derive(Debug, Clone, Copy)]
pub struct TensionNode {
pub x: f64,
pub y: f64,
pub t: f64,
}
pub fn idw_height(x: f64, y: f64, nodes: &[TensionNode], tmax: f64) -> f64 {
let mut num = 0.0;
let mut den = 0.0;
for n in nodes {
let d2 = (x - n.x).powi(2) + (y - n.y).powi(2) + EPS;
num += n.t / d2;
den += 1.0 / d2;
}
(num / den) / tmax
}
pub(crate) fn bridge_cliff(_heightfield: &mut [Vec<f64>], _world: &DeckWorld) {}
pub fn build_terrain(world: &DeckWorld, _opts: &InstrumentOpts) -> SceneGraph3D {
let pos = spectral_positions(world);
let tension_nodes: Vec<TensionNode> = world
.agents
.iter()
.map(|a| {
let (x, y) = pos[a.id];
TensionNode {
x,
y,
t: agent_tension(a, world.repression),
}
})
.collect();
let tmax = tension_nodes
.iter()
.map(|n| n.t)
.fold(0.0_f64, f64::max)
.max(0.001);
let mut t_grid: Vec<Vec<f64>> = Vec::with_capacity(N + 1);
for i in 0..=N {
let mut row = Vec::with_capacity(M + 1);
for j in 0..=M {
let x = (i as f64 / N as f64 * 2.0 - 1.0) * S;
let z = (j as f64 / M as f64 * 2.0 - 1.0) * S;
row.push(idw_height(x / S, z / S, &tension_nodes, tmax));
}
t_grid.push(row);
}
bridge_cliff(&mut t_grid, world);
let vertex = |i: usize, j: usize| -> (Vertex3, f64) {
let x = (i as f64 / N as f64 * 2.0 - 1.0) * S;
let z = (j as f64 / M as f64 * 2.0 - 1.0) * S;
let t = t_grid[i][j];
(
Vertex3 {
x,
y: -0.55 + t * 0.95,
z,
},
t,
)
};
let mut faces = Vec::with_capacity(N * M * 2);
for i in 0..N {
for j in 0..M {
let (v00, t00) = vertex(i, j);
let (v10, _) = vertex(i + 1, j);
let (v11, t11) = vertex(i + 1, j + 1);
let (v01, _) = vertex(i, j + 1);
let t = (t00 + t11) / 2.0;
let fill = heat(t);
let opacity = 0.16 + t * 0.55;
faces.push(Face {
verts: [v00, v10, v11],
fill,
opacity,
});
faces.push(Face {
verts: [v00, v11, v01],
fill,
opacity,
});
}
}
let mut nodes = Vec::with_capacity(world.agents.len());
for a in &world.agents {
let (x, y) = pos[a.id];
let t = idw_height(x, y, &tension_nodes, tmax);
nodes.push(Node3 {
id: format!("agent-{}", a.id),
pos: Vertex3 {
x: x * S,
y: -0.53 + t * 0.95,
z: y * S,
},
radius: 0.024,
color: BLOC_COLORS[a.bloc as usize],
style: NodeStyle::default(),
attrs: json!({ "agent_id": a.id, "bloc": a.bloc, "t": t }),
});
}
let mut metadata = serde_json::Map::new();
metadata.insert("grid_vertices".to_string(), json!((N + 1) * (M + 1)));
metadata.insert("extent".to_string(), json!(S));
metadata.insert("tmax".to_string(), json!(tmax));
SceneGraph3D {
nodes,
faces,
struts: Vec::new(),
bounding_box: (-S, -0.55, -S, S, 0.4, S),
metadata,
}
}