use crate::aggregate::Aggregate;
use crate::containers::mesh::NodeId;
use crate::containers::mesh::{Coords, Mesh, SubMesh};
use crate::error::{PyrucastError, Result};
use crate::store::{insert, read};
use std::collections::HashMap;
pub fn merge_nodes(mesh: &Mesh, tol: f64) -> Result<Mesh> {
if tol < 0.0 {
return Err(PyrucastError::Message(format!(
"merge_nodes: tol must be ≥ 0, got {tol}"
)));
}
let coords_handle = mesh.coords()?;
let representative = build_representatives(mesh, &coords_handle, tol)?;
let mut result = Mesh::empty();
for sm_handle in mesh {
let (et, color, conn) = {
let s = read(sm_handle)?;
(s.element_type(), s.face_color(), s.connectivity().to_vec())
};
let npc = et.nodes_per_cell();
let mut new_sm = SubMesh::new(coords_handle.clone(), et);
new_sm.set_face_color(color);
for chunk in conn.chunks(npc) {
let mapped: Vec<NodeId> = chunk
.iter()
.map(|n| representative.get(n).copied().unwrap_or(*n))
.collect();
if is_degenerate(&mapped) {
continue;
}
new_sm.add_cell(&mapped)?;
}
result.add_sub(insert(new_sm))?;
}
Ok(result)
}
fn build_representatives(
mesh: &Mesh,
coords_handle: &crate::store::Handle<Coords>,
tol: f64,
) -> Result<HashMap<NodeId, NodeId>> {
let mut ids: Vec<NodeId> = Vec::new();
{
let mut seen = std::collections::HashSet::new();
for sm_handle in mesh {
for &n in read(sm_handle)?.connectivity() {
if seen.insert(n) {
ids.push(n);
}
}
}
}
ids.sort_by_key(|n| n.0);
let coords = read(coords_handle)?;
let dim = coords.dim() as usize;
let cell = if tol > 0.0 { tol } else { 1.0 };
let tol2 = tol * tol;
let offsets = neighbour_offsets(dim);
let mut grid: HashMap<Vec<i64>, Vec<NodeId>> = HashMap::new();
let mut representative: HashMap<NodeId, NodeId> = HashMap::new();
for &id in &ids {
let p = coords.coord(id)?;
let base: Vec<i64> = p.iter().map(|&x| (x / cell).floor() as i64).collect();
let mut rep = None;
'search: for off in &offsets {
let key: Vec<i64> = base.iter().zip(off).map(|(b, o)| b + o).collect();
if let Some(candidates) = grid.get(&key) {
for &c in candidates {
if dist2(p, coords.coord(c)?) <= tol2 {
rep = Some(c);
break 'search;
}
}
}
}
match rep {
Some(r) => {
representative.insert(id, r);
}
None => {
representative.insert(id, id);
grid.entry(base).or_default().push(id);
}
}
}
Ok(representative)
}
fn dist2(a: &[f64], b: &[f64]) -> f64 {
a.iter().zip(b).map(|(x, y)| (x - y) * (x - y)).sum()
}
fn is_degenerate(nodes: &[NodeId]) -> bool {
nodes
.iter()
.enumerate()
.any(|(i, n)| nodes[i + 1..].contains(n))
}
fn neighbour_offsets(dim: usize) -> Vec<Vec<i64>> {
let mut offsets = vec![Vec::new()];
for _ in 0..dim {
let mut next = Vec::with_capacity(offsets.len() * 3);
for prefix in &offsets {
for d in [-1i64, 0, 1] {
let mut v = prefix.clone();
v.push(d);
next.push(v);
}
}
offsets = next;
}
offsets
}
#[cfg(test)]
mod tests {
use super::*;
use crate::containers::mesh::ElementType;
use crate::containers::mesh::Node;
fn coords2() -> crate::store::Handle<Coords> {
insert(Coords::new(2).unwrap())
}
#[test]
fn welds_two_coincident_corners() {
let coords = coords2();
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 b2 = Node::create_in(coords.clone(), &[1.0 + 1e-9, 0.0]).unwrap();
let c2 = Node::create_in(coords.clone(), &[0.5, 1.0 - 1e-9]).unwrap();
let d = Node::create_in(coords.clone(), &[1.5, 1.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
mesh.add_cell(&[a.id(), b.id(), c.id()]).unwrap();
mesh.add_cell(&[b2.id(), d.id(), c2.id()]).unwrap();
let merged = merge_nodes(&mesh, 1e-6).unwrap();
assert_eq!(merged.cell_count().unwrap(), 2, "both triangles survive");
let tri1: Vec<_> = (0..3).map(|i| merged.node(0, 1, i).unwrap().id()).collect();
assert_eq!(tri1, vec![b.id(), d.id(), c.id()]);
}
#[test]
fn drops_degenerate_cell() {
let coords = coords2();
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(), &[1.0, 0.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
mesh.add_cell(&[a.id(), b.id()]).unwrap();
mesh.add_cell(&[b.id(), c.id()]).unwrap();
let merged = merge_nodes(&mesh, 1e-6).unwrap();
assert_eq!(
merged.cell_count().unwrap(),
1,
"the (b,c) segment is dropped"
);
}
#[test]
fn representative_is_smallest_id_and_keeps_its_coords() {
let coords = coords2();
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[5.0, 5.0]).unwrap();
let c = Node::create_in(coords.clone(), &[5.0 + 1e-9, 5.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
mesh.add_cell(&[a.id(), b.id()]).unwrap();
mesh.add_cell(&[a.id(), c.id()]).unwrap();
let merged = merge_nodes(&mesh, 1e-6).unwrap();
let welded = merged.node(0, 1, 1).unwrap();
assert_eq!(welded.id(), b.id());
assert_eq!(read(&coords).unwrap().coord(b.id()).unwrap(), &[5.0, 5.0]);
}
#[test]
fn tol_zero_welds_only_exact_duplicates() {
let coords = coords2();
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 exact = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let near = Node::create_in(coords.clone(), &[1.0 + 1e-9, 0.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
mesh.add_cell(&[a.id(), near.id()]).unwrap();
mesh.add_cell(&[b.id(), exact.id()]).unwrap();
let merged = merge_nodes(&mesh, 0.0).unwrap();
assert_eq!(merged.node(0, 0, 1).unwrap().id(), near.id());
assert_eq!(merged.node(0, 1, 1).unwrap().id(), a.id());
assert_ne!(near.id(), b.id());
}
#[test]
fn negative_tol_is_error() {
let coords = coords2();
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 mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
mesh.add_cell(&[a.id(), b.id()]).unwrap();
assert!(merge_nodes(&mesh, -1.0).is_err());
}
#[test]
fn leaves_input_untouched_and_increfs_result() {
let coords = coords2();
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 mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::SEG2));
mesh.add_cell(&[a.id(), b.id()]).unwrap();
assert_eq!(read(&coords).unwrap().refcount(a.id()), 2);
let merged = merge_nodes(&mesh, 1e-6).unwrap();
assert_eq!(read(&coords).unwrap().refcount(a.id()), 3);
drop(merged);
assert_eq!(read(&coords).unwrap().refcount(a.id()), 2);
}
}