use crate::aggregate::Aggregate;
use crate::containers::mesh::{ElementType, Mesh, NodeId, SubMesh};
use crate::error::{PyrucastError, Result};
use crate::store::{insert, read};
const HEX8_TO_TET4: [[usize; 4]; 6] = [
[0, 1, 2, 6],
[0, 2, 3, 6],
[0, 3, 7, 6],
[0, 7, 4, 6],
[0, 4, 5, 6],
[0, 5, 1, 6],
];
fn split_of(src: ElementType, target: ElementType) -> Result<&'static [&'static [usize]]> {
match (src, target) {
(ElementType::QUA4, ElementType::TRI3) => Ok(&[&[0, 1, 2], &[0, 2, 3]]),
(ElementType::HEX8, ElementType::TET4) => Ok(&[
&HEX8_TO_TET4[0],
&HEX8_TO_TET4[1],
&HEX8_TO_TET4[2],
&HEX8_TO_TET4[3],
&HEX8_TO_TET4[4],
&HEX8_TO_TET4[5],
]),
(s, t) => Err(PyrucastError::Message(format!(
"convert: no {s} → {t} 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 (src, color, conn) = {
let s = read(sm_h)?;
(s.element_type(), s.face_color(), s.connectivity().to_vec())
};
let mut new_sm = SubMesh::new(coords.clone(), target);
new_sm.set_face_color(color);
if src == target {
let npc = src.nodes_per_cell();
for cell in conn.chunks(npc) {
new_sm.add_cell(cell)?;
}
} else {
let splits = split_of(src, target)?;
let npc = src.nodes_per_cell();
for cell in conn.chunks(npc) {
for sub in splits {
let nodes: Vec<NodeId> = sub.iter().map(|&i| cell[i]).collect();
new_sm.add_cell(&nodes)?;
}
}
}
result.add_sub(insert(new_sm))?;
}
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::containers::mesh::{Coords, Node};
use crate::store::insert;
#[test]
fn qua4_to_tri3_splits_each_quad_in_two() {
let coords = insert(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().unwrap(), 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 = insert(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().unwrap(), 6);
let live: usize = {
let c = read(&coords).unwrap();
n.iter().filter(|&&id| c.refcount(id) > 0).count()
};
assert_eq!(live, 8);
let vol = |ids: [NodeId; 4]| -> f64 {
let c = read(&coords).unwrap();
let p: Vec<Vec<f64>> = ids.iter().map(|&i| c.coord(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 = insert(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().unwrap(), 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 = insert(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::containers::mesh::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 = read(&tri.get(0).unwrap()).unwrap().face_color();
assert_eq!(out_color, color);
}
#[test]
fn rejects_unsupported_conversion() {
let coords = insert(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());
}
}