use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, NodeId};
use crate::containers::mesh::{Mesh, SubMesh};
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
fn split_of(src: ElementType, target: ElementType) -> Result<&'static [&'static [usize]]> {
src.as_kind().split_into(target).ok_or_else(|| {
PyrucastError::Message(format!(
"convert: no {src} → {target} conversion (supported: identity, \
QUA4 → TRI3, HEX8 → TET4)"
))
})
}
pub fn convert(mesh: &Mesh, target: ElementType) -> Result<Mesh> {
let coords = mesh.coords()?;
let mut result = Mesh::empty();
for sm_h in mesh {
let (color, out) = {
let s = sm_h.read();
let src = s.element_type();
let conn = s.connectivity();
let out: Vec<NodeId> = if src == target {
conn.to_vec()
} else {
let splits = split_of(src, target)?;
let npc = src.nodes_per_cell();
conn.chunks(npc)
.flat_map(|cell| {
splits
.iter()
.flat_map(move |sub| sub.iter().map(|&i| cell[i]))
})
.collect()
};
(s.face_color(), out)
};
let mut new_sm = SubMesh::from_connectivity(coords.clone(), target, out)?;
new_sm.set_face_color(color);
result.add_sub(Handle::new(new_sm))?;
}
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atoms::Node;
use crate::coords::Coords;
use crate::handle::Handle;
#[test]
fn qua4_to_tri3_splits_each_quad_in_two() {
let coords = Handle::new(Coords::new(2).unwrap());
let n: Vec<NodeId> = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]
.iter()
.map(|p| Node::create_in(coords.clone(), p).unwrap().id())
.collect();
let mut m = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::QUA4));
m.add_cell(&n).unwrap();
let tri = convert(&m, ElementType::TRI3).unwrap();
assert_eq!(tri.element_types().unwrap(), vec![ElementType::TRI3]);
assert_eq!(tri.cell_count(), 2);
assert_eq!(tri.node(0, 0, 0).unwrap().id(), n[0]);
assert_eq!(tri.node(0, 0, 1).unwrap().id(), n[1]);
assert_eq!(tri.node(0, 0, 2).unwrap().id(), n[2]);
assert_eq!(tri.node(0, 1, 0).unwrap().id(), n[0]);
assert_eq!(tri.node(0, 1, 1).unwrap().id(), n[2]);
assert_eq!(tri.node(0, 1, 2).unwrap().id(), n[3]);
}
#[test]
fn hex8_to_tet4_gives_six_tets_no_new_node() {
let coords = Handle::new(Coords::new(3).unwrap());
let n: Vec<NodeId> = [
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
]
.iter()
.map(|p| Node::create_in(coords.clone(), p).unwrap().id())
.collect();
let mut m = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::HEX8));
m.add_cell(&n).unwrap();
let tets = convert(&m, ElementType::TET4).unwrap();
assert_eq!(tets.element_types().unwrap(), vec![ElementType::TET4]);
assert_eq!(tets.cell_count(), 6);
let live: usize = {
let c = coords.read();
n.iter().filter(|&&id| c.refcount(id) > 0).count()
};
assert_eq!(live, 8);
let vol = |ids: [NodeId; 4]| -> f64 {
let c = coords.read();
let p: Vec<Vec<f64>> = ids
.iter()
.map(|&i| c.position(i).unwrap().to_vec())
.collect();
let e1 = [p[1][0] - p[0][0], p[1][1] - p[0][1], p[1][2] - p[0][2]];
let e2 = [p[2][0] - p[0][0], p[2][1] - p[0][1], p[2][2] - p[0][2]];
let e3 = [p[3][0] - p[0][0], p[3][1] - p[0][1], p[3][2] - p[0][2]];
let det = e1[0] * (e2[1] * e3[2] - e2[2] * e3[1])
- e1[1] * (e2[0] * e3[2] - e2[2] * e3[0])
+ e1[2] * (e2[0] * e3[1] - e2[1] * e3[0]);
det.abs() / 6.0
};
let total: f64 = (0..6)
.map(|k| {
vol([
tets.node(0, k, 0).unwrap().id(),
tets.node(0, k, 1).unwrap().id(),
tets.node(0, k, 2).unwrap().id(),
tets.node(0, k, 3).unwrap().id(),
])
})
.sum();
assert!((total - 1.0).abs() < 1e-12, "six tets must tile the cube");
}
#[test]
fn identity_copies_verbatim() {
let coords = Handle::new(Coords::new(2).unwrap());
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
let mut m = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
m.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
let out = convert(&m, ElementType::TRI3).unwrap();
assert_eq!(out.element_types().unwrap(), vec![ElementType::TRI3]);
assert_eq!(out.cell_count(), 1);
assert_eq!(out.node(0, 0, 0).unwrap().id(), a.id());
assert_eq!(out.node(0, 0, 1).unwrap().id(), b.id());
assert_eq!(out.node(0, 0, 2).unwrap().id(), c.id());
}
#[test]
fn preserves_face_color() {
let coords = Handle::new(Coords::new(2).unwrap());
let n: Vec<NodeId> = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]
.iter()
.map(|p| Node::create_in(coords.clone(), p).unwrap().id())
.collect();
let mut sm = SubMesh::new(coords.clone(), ElementType::QUA4);
let color = crate::atoms::RgbColor::new(10, 20, 30);
sm.set_face_color(color);
let mut m = Mesh::from_submesh(sm);
m.add_cell(&n).unwrap();
let tri = convert(&m, ElementType::TRI3).unwrap();
let out_color = tri.get(0).unwrap().read().face_color();
assert_eq!(out_color, color);
}
#[test]
fn rejects_unsupported_conversion() {
let coords = Handle::new(Coords::new(2).unwrap());
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0, 0.0]).unwrap();
let c = Node::create_in(coords.clone(), &[0.0, 1.0]).unwrap();
let mut m = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
m.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
assert!(convert(&m, ElementType::QUA4).is_err());
}
}