use crate::aggregate::Aggregate;
use crate::atoms::ElementType;
use crate::atoms::NodeId;
use crate::containers::mesh::{Mesh, SubMesh};
use crate::handle::Handle;
use std::collections::HashSet;
pub fn consolidate(mesh: &Mesh) -> Mesh {
let mut result = Mesh::empty();
let Some(first) = mesh.iter().next() else {
return result;
};
let coords = first.read().coords();
let mut ordered_types: Vec<ElementType> = Vec::new();
for sm_handle in mesh {
let et = sm_handle.read().element_type();
if !ordered_types.contains(&et) {
ordered_types.push(et);
}
}
for et in ordered_types {
let npc = et.nodes_per_cell();
let zones: Vec<&Handle<SubMesh>> = mesh
.iter()
.filter(|h| h.read().element_type() == et)
.collect();
let mut kept: Vec<NodeId> = Vec::new();
let mut seen: HashSet<Vec<NodeId>> = HashSet::new();
for zone in &zones {
let s = zone.read();
for chunk in s.connectivity().chunks(npc) {
if seen.insert(chunk.to_vec()) {
kept.extend_from_slice(chunk);
}
}
}
if let [only] = zones.as_slice()
&& kept.len() == only.read().connectivity().len()
{
result.push((*only).clone());
result.post_push();
continue;
}
let first_color = zones[0].read().face_color();
let mut new_sm = SubMesh::from_connectivity(coords.clone(), et, kept)
.expect("whole cells of live nodes: the input zones hold them");
new_sm.set_face_color(first_color);
result.push(Handle::new(new_sm));
result.post_push();
}
result
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atoms::Node;
use crate::coords::Coords;
use crate::handle::Handle;
#[test]
fn merges_same_type_submeshes() {
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.5, 1.0]).unwrap();
let sm1 = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
Handle::new(sm)
};
let sm2 = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[b.id(), c.id(), a.id()]).unwrap(); Handle::new(sm)
};
let mut mesh = Mesh::empty();
mesh.add_sub(sm1).unwrap();
mesh.add_sub(sm2).unwrap();
assert_eq!(mesh.len(), 2);
let c2 = consolidate(&mesh);
assert_eq!(c2.len(), 1, "must merge the two TRI3 submeshes");
assert_eq!(c2.cell_count(), 2, "two distinct cells must be kept");
assert_eq!(c2.element_types().unwrap(), vec![ElementType::TRI3]);
}
#[test]
fn removes_duplicate_cells() {
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.5, 1.0]).unwrap();
let sm1 = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
Handle::new(sm)
};
let sm2 = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap(); Handle::new(sm)
};
let mut mesh = Mesh::empty();
mesh.add_sub(sm1).unwrap();
mesh.add_sub(sm2).unwrap();
let c2 = consolidate(&mesh);
assert_eq!(c2.len(), 1);
assert_eq!(c2.cell_count(), 1, "the duplicate must be removed");
}
#[test]
fn preserves_distinct_types() {
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.5, 1.0]).unwrap();
let sm_tri = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
Handle::new(sm)
};
let sm_poi = {
let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
sm.add_cell(&[a.id()]).unwrap();
Handle::new(sm)
};
let sm_tri2 = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
Handle::new(sm)
};
let mut mesh = Mesh::empty();
mesh.add_sub(sm_tri).unwrap();
mesh.add_sub(sm_poi).unwrap();
mesh.add_sub(sm_tri2).unwrap();
let c2 = consolidate(&mesh);
assert_eq!(c2.len(), 2, "TRI3 + POI1");
assert_eq!(
c2.element_types().unwrap(),
vec![ElementType::TRI3, ElementType::POI1],
"first-seen order"
);
assert_eq!(c2.cell_counts().unwrap(), vec![1, 1]);
}
#[test]
fn keeps_a_zone_with_nothing_to_merge() {
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.5, 1.0]).unwrap();
let mut tri = SubMesh::new(coords.clone(), ElementType::TRI3);
tri.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
let tri = Handle::new(tri);
let mut points = SubMesh::new(coords.clone(), ElementType::POI1);
points.add_cell(&[a.id()]).unwrap();
points.add_cell(&[b.id()]).unwrap();
let points = Handle::new(points);
let mut mesh = Mesh::empty();
mesh.add_sub(tri.clone()).unwrap();
mesh.add_sub(points.clone()).unwrap();
let refs_before = coords.read().refcount(a.id());
let c2 = consolidate(&mesh);
assert!(c2.get(0).unwrap().same_object(&tri));
assert!(c2.get(1).unwrap().same_object(&points));
assert_eq!(coords.read().refcount(a.id()), refs_before);
}
#[test]
fn rebuilds_a_single_zone_holding_a_duplicate_cell() {
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 mut seg = SubMesh::new(coords.clone(), ElementType::SEG2);
seg.add_cell(&[a.id(), b.id()]).unwrap();
seg.add_cell(&[a.id(), b.id()]).unwrap(); let seg = Handle::new(seg);
let mut mesh = Mesh::empty();
mesh.add_sub(seg.clone()).unwrap();
let c2 = consolidate(&mesh);
assert!(!c2.get(0).unwrap().same_object(&seg));
assert_eq!(c2.cell_count(), 1);
assert_eq!(seg.read().cell_count(), 2);
}
}