use crate::core::PolygonSet;
use crate::foundation::{
BBox, GeoError, GridGeometry, HasHistory, OperationHistory, Result, Stats,
};
use ndarray::Array2;
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct StructuredMeshSurface {
ncol: usize,
nrow: usize,
x: Array2<f64>,
y: Array2<f64>,
values: Array2<f64>,
nominal_geometry: Option<GridGeometry>,
edge: PolygonSet,
#[serde(default)]
history: OperationHistory,
}
impl StructuredMeshSurface {
pub fn new(
x: Array2<f64>,
y: Array2<f64>,
values: Array2<f64>,
nominal_geometry: Option<GridGeometry>,
edge: PolygonSet,
) -> Result<Self> {
let shape = x.dim();
if shape.0 == 0 || shape.1 == 0 {
return Err(GeoError::GeometryMismatch(
"StructuredMeshSurface::new: shape must be non-empty".into(),
));
}
if y.dim() != shape || values.dim() != shape {
return Err(GeoError::GeometryMismatch(format!(
"StructuredMeshSurface::new: x/y/values shapes differ: x={:?}, y={:?}, values={:?}",
x.dim(),
y.dim(),
values.dim()
)));
}
Ok(Self {
ncol: shape.0,
nrow: shape.1,
x,
y,
values,
nominal_geometry,
edge,
history: OperationHistory::from_entry("structured_surface.new"),
})
}
pub fn kind(&self) -> &'static str {
"structured_mesh"
}
pub fn ncol(&self) -> usize {
self.ncol
}
pub fn nrow(&self) -> usize {
self.nrow
}
pub fn x(&self) -> &Array2<f64> {
&self.x
}
pub fn y(&self) -> &Array2<f64> {
&self.y
}
pub fn values(&self) -> &Array2<f64> {
&self.values
}
pub fn nominal_geometry(&self) -> Option<&GridGeometry> {
self.nominal_geometry.as_ref()
}
pub fn edge(&self) -> &PolygonSet {
&self.edge
}
pub fn node_xy(&self, i: usize, j: usize) -> Result<(f64, f64)> {
self.check_node(i, j)?;
Ok((self.x[[i, j]], self.y[[i, j]]))
}
pub fn z(&self, i: usize, j: usize) -> Result<f64> {
self.check_node(i, j)?;
Ok(self.values[[i, j]])
}
pub fn stats(&self) -> Stats {
Stats::of(&self.values.iter().copied().collect::<Vec<_>>())
}
pub fn bbox(&self) -> BBox {
let mut b = BBox {
xmin: f64::INFINITY,
ymin: f64::INFINITY,
xmax: f64::NEG_INFINITY,
ymax: f64::NEG_INFINITY,
};
let mut any = false;
for (x, y) in self.x.iter().zip(self.y.iter()) {
if x.is_finite() && y.is_finite() {
any = true;
b.xmin = b.xmin.min(*x);
b.xmax = b.xmax.max(*x);
b.ymin = b.ymin.min(*y);
b.ymax = b.ymax.max(*y);
}
}
if any {
b
} else {
BBox {
xmin: f64::NAN,
ymin: f64::NAN,
xmax: f64::NAN,
ymax: f64::NAN,
}
}
}
pub fn history(&self) -> &[String] {
self.history.entries()
}
pub(crate) fn set_history(&mut self, history: impl Into<OperationHistory>) {
self.history = history.into();
}
fn check_node(&self, i: usize, j: usize) -> Result<()> {
if i >= self.ncol || j >= self.nrow {
return Err(GeoError::OutOfRange(format!(
"structured surface node ({i}, {j}) outside shape (ncol={}, nrow={})",
self.ncol, self.nrow
)));
}
Ok(())
}
}
impl HasHistory for StructuredMeshSurface {
fn operation_history(&self) -> &OperationHistory {
&self.history
}
fn operation_history_mut(&mut self) -> &mut OperationHistory {
&mut self.history
}
}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::array;
#[test]
fn keeps_explicit_shifted_node_coordinates() {
let x = array![[0.0, 0.0], [10.0, 12.0]];
let y = array![[0.0, 10.0], [0.0, 10.0]];
let z = array![[100.0, 110.0], [120.0, 130.0]];
let edge =
PolygonSet::convex_hull_xy(vec![[0.0, 0.0], [10.0, 0.0], [12.0, 10.0], [0.0, 10.0]])
.unwrap();
let s = StructuredMeshSurface::new(x, y, z, None, edge).unwrap();
assert_eq!(s.kind(), "structured_mesh");
assert_eq!(s.ncol(), 2);
assert_eq!(s.nrow(), 2);
assert_eq!(s.node_xy(1, 1).unwrap(), (12.0, 10.0));
assert_eq!(s.z(1, 1).unwrap(), 130.0);
assert_eq!(s.stats().count, 4);
}
}