pub mod cleanup;
pub mod merge_triangles;
pub mod regularize;
pub use cleanup::cleanup;
pub use merge_triangles::merge_triangles;
pub use regularize::regularize;
use crate::aggregate::Aggregate;
use crate::atoms::{ElementType, Node, NodeId, Point2};
use crate::containers::mesh::{Mesh, SubMesh};
use crate::coords::Coords;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use std::collections::HashMap;
pub(crate) struct Surface {
pub pts: Vec<Point2>,
pub quads: Vec<[u32; 4]>,
pub tris: Vec<[u32; 3]>,
pub movable: Vec<bool>,
ids: Vec<NodeId>,
coords: Handle<Coords>,
pts0: Vec<Point2>,
clockwise: bool,
}
impl Surface {
pub(crate) fn read(mesh: &Mesh, op: &str) -> Result<Surface> {
let coords = mesh.coords()?;
let dim = coords.read().dim();
if dim != 2 {
return Err(PyrucastError::Message(format!(
"{op}: only 2-D surface meshes are supported, got dim={dim}"
)));
}
let mut index: HashMap<NodeId, u32> = HashMap::new();
let mut surf = Surface {
pts: Vec::new(),
quads: Vec::new(),
tris: Vec::new(),
movable: Vec::new(),
ids: Vec::new(),
coords: coords.clone(),
pts0: Vec::new(),
clockwise: false,
};
let (mut cw, mut ccw) = (0usize, 0usize);
let guard = coords.read();
let mut cells = 0usize;
for sm in mesh {
let s = sm.read();
let (et, conn) = (s.element_type(), s.connectivity());
match et {
ElementType::POI1 | ElementType::SEG2 => continue,
ElementType::TRI3 | ElementType::QUA4 => {}
other => {
return Err(PyrucastError::Message(format!(
"{op}: only TRI3 and QUA4 cells can be improved, got {other}"
)));
}
}
let npc = et.nodes_per_cell();
for cell in conn.chunks(npc) {
let mut local = [0u32; 4];
for (k, &id) in cell.iter().enumerate() {
local[k] = match index.get(&id) {
Some(&i) => i,
None => {
let p = guard.position(id)?;
let i = surf.pts.len() as u32;
surf.pts.push(Point2::new(p[0], p[1]));
surf.movable.push(true);
surf.ids.push(id);
index.insert(id, i);
i
}
};
}
let ring: Vec<Point2> =
local[..npc].iter().map(|&i| surf.pts[i as usize]).collect();
if crate::ops::mesh::triangulation::signed_area(&ring) < 0.0 {
local[..npc].reverse();
cw += 1;
} else {
ccw += 1;
}
if npc == 3 {
surf.tris.push([local[0], local[1], local[2]]);
} else {
surf.quads.push(local);
}
cells += 1;
}
}
drop(guard);
surf.pts0 = surf.pts.clone();
surf.clockwise = cw > ccw;
if cells == 0 {
return Err(PyrucastError::Message(format!(
"{op}: mesh has no surface cells (TRI3/QUA4)"
)));
}
surf.pin_boundary();
Ok(surf)
}
fn pin_boundary(&mut self) {
let mut counts: HashMap<(u32, u32), u32> = HashMap::new();
let bump = |a: u32, b: u32, c: &mut HashMap<(u32, u32), u32>| {
*c.entry((a.min(b), a.max(b))).or_insert(0) += 1;
};
for q in &self.quads {
for i in 0..4 {
bump(q[i], q[(i + 1) % 4], &mut counts);
}
}
for t in &self.tris {
for i in 0..3 {
bump(t[i], t[(i + 1) % 3], &mut counts);
}
}
for ((a, b), n) in counts {
if n == 1 {
self.movable[a as usize] = false;
self.movable[b as usize] = false;
}
}
}
pub(crate) fn pinned(&self) -> usize {
self.movable.iter().filter(|m| !**m).count()
}
pub(crate) fn write_positions(&self, mesh: &Mesh) -> Result<Mesh> {
for (i, &id) in self.ids.iter().enumerate() {
if !self.movable[i] {
continue;
}
let node = Node::acquire(self.coords.clone(), id)?;
node.set_position(&[self.pts[i].x, self.pts[i].y])?;
}
mesh.subset(0..mesh.len())
}
pub(crate) fn to_mesh(&self, op: &str) -> Result<Mesh> {
let mut ids = self.ids.clone();
let mut moved: Vec<usize> = Vec::new();
let mut flat: Vec<f64> = Vec::new();
for i in 0..ids.len() {
if !self.movable[i] || self.pts[i] == self.pts0[i] {
continue;
}
flat.extend_from_slice(&[self.pts[i].x, self.pts[i].y]);
moved.push(i);
}
let first = self.coords.write().add_nodes(&flat)?.start;
for (rank, &i) in moved.iter().enumerate() {
ids[i] = NodeId(first + rank as u32);
}
let mesh = self.emit(&ids, op)?;
let owned: Vec<NodeId> = (first..first + moved.len() as u32).map(NodeId).collect();
self.coords.write().decref_all(&owned)?;
Ok(mesh)
}
pub(crate) fn to_mesh_same_nodes(&self, op: &str) -> Result<Mesh> {
let ids = self.ids.clone();
self.emit(&ids, op)
}
fn emit(&self, ids: &[NodeId], op: &str) -> Result<Mesh> {
let mut mesh = Mesh::empty();
if !self.quads.is_empty() {
let conn: Vec<NodeId> = self
.quads
.iter()
.flat_map(|q| {
let mut c = q.map(|v| ids[v as usize]);
if self.clockwise {
c.reverse();
}
c
})
.collect();
mesh.add_sub(Handle::new(SubMesh::from_connectivity(
self.coords.clone(),
ElementType::QUA4,
conn,
)?))?;
}
if !self.tris.is_empty() {
let conn: Vec<NodeId> = self
.tris
.iter()
.flat_map(|t| {
let mut c = t.map(|v| ids[v as usize]);
if self.clockwise {
c.reverse();
}
c
})
.collect();
mesh.add_sub(Handle::new(SubMesh::from_connectivity(
self.coords.clone(),
ElementType::TRI3,
conn,
)?))?;
}
if mesh.is_empty() {
return Err(PyrucastError::Message(format!("{op}: produced no cell")));
}
Ok(mesh)
}
}
#[cfg(test)]
mod tests {
use super::cleanup::cleanup;
use super::merge_triangles::merge_triangles;
use super::regularize::regularize;
use super::*;
use crate::handle::Handle;
use crate::ops::mesh::grid_surface;
fn paved_circle() -> Mesh {
let coords = Handle::new(Coords::new(2).unwrap());
let pts: Vec<(f64, f64)> = (0..60)
.map(|i| {
let t = i as f64 / 60.0 * std::f64::consts::TAU;
(t.cos(), t.sin())
})
.collect();
let n = pts.len();
let mut fine = Vec::new();
for i in 0..n {
let (a, b) = (pts[i], pts[(i + 1) % n]);
let len = ((b.0 - a.0).powi(2) + (b.1 - a.1).powi(2)).sqrt();
let k = ((len / 0.05).round() as usize).max(1);
for j in 0..k {
let t = j as f64 / k as f64;
fine.push((a.0 + t * (b.0 - a.0), a.1 + t * (b.1 - a.1)));
}
}
let ids: Vec<NodeId> = fine
.iter()
.map(|&(x, y)| Node::create_in(coords.clone(), &[x, y]).unwrap().id())
.collect();
let mut sm = SubMesh::new(coords, ElementType::SEG2);
let m = ids.len();
for i in 0..m {
sm.add_cell(&[ids[i], ids[(i + 1) % m]]).unwrap();
}
let contour = Mesh::from_submesh(sm);
grid_surface(
&contour,
ElementType::QUA4,
Some(0.05),
0,
false,
crate::ops::mesh::FrontRelax::Free,
)
.unwrap()
}
fn look(mesh: &Mesh) -> (usize, usize, f64, f64, Vec<(NodeId, NodeId)>) {
let (mut cells, mut tris) = (0usize, 0usize);
let mut qs: Vec<f64> = Vec::new();
let mut used: HashMap<(NodeId, NodeId), usize> = HashMap::new();
for si in 0..mesh.len() {
for cell in mesh.cells(si).unwrap() {
let nodes = cell.nodes().unwrap();
let p: Vec<Point2> = nodes
.iter()
.map(|n| {
let v = n.position().unwrap();
Point2::new(v[0], v[1])
})
.collect();
let k = p.len();
cells += 1;
if k == 3 {
tris += 1;
}
for i in 0..k {
let (a, b) = (nodes[i].id(), nodes[(i + 1) % k].id());
let key = if a.0 < b.0 { (a, b) } else { (b, a) };
*used.entry(key).or_insert(0) += 1;
}
let signed: f64 = 0.5
* (0..k)
.map(|i| p[i].x * p[(i + 1) % k].y - p[(i + 1) % k].x * p[i].y)
.sum::<f64>();
let p: Vec<Point2> = if signed < 0.0 {
p.iter().rev().copied().collect()
} else {
p
};
let mut w = 1.0f64;
for i in 0..k {
let u = p[(i + 1) % k] - p[i];
let v = p[(i + k - 1) % k] - p[i];
w = w.min((u.x * v.y - u.y * v.x) / (u.norm() * v.norm()));
}
qs.push(w);
}
}
qs.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mut boundary: Vec<(NodeId, NodeId)> = used
.iter()
.filter(|(_, v)| **v == 1)
.map(|(k, _)| *k)
.collect();
boundary.sort_unstable();
(cells, tris, qs[0], qs[qs.len() / 100], boundary)
}
#[test]
fn regularize_improves_the_mesh_and_keeps_its_boundary() {
let mesh = paved_circle();
let (cells, tris, worst, p1, boundary) = look(&mesh);
let out = regularize(&mesh, 40, true, false).unwrap();
let (cells2, tris2, worst2, p12, boundary2) = look(&out);
assert_eq!(
(cells, tris),
(cells2, tris2),
"nothing may be added or lost"
);
assert!(worst2 > worst, "worst cell: {worst} → {worst2}");
assert!(p12 > p1, "1st percentile: {p1} → {p12}");
assert_eq!(boundary, boundary2);
}
#[test]
fn regularize_in_place_moves_the_caller_s_own_nodes() {
let mesh = paved_circle();
let (_, _, worst, _, boundary) = look(&mesh);
let same = regularize(&mesh, 40, true, true).unwrap();
let (_, _, worst2, _, boundary2) = look(&same);
assert!(worst2 > worst, "{worst} → {worst2}");
assert_eq!(boundary, boundary2);
let (_, _, worst3, _, _) = look(&mesh);
assert_eq!(worst2, worst3);
}
#[test]
fn the_angular_rule_and_the_laplacian_both_hold_the_line() {
let mesh = paved_circle();
let (_, _, worst, _, _) = look(&mesh);
for angular in [true, false] {
let out = regularize(&mesh, 40, angular, false).unwrap();
let (_, _, w, _, _) = look(&out);
assert!(w > worst, "angular={angular}: {worst} → {w}");
assert!(w > 0.0, "angular={angular} inverted a cell");
}
}
#[test]
fn cleanup_keeps_the_mesh_conforming_and_the_boundary_whole() {
let mesh = paved_circle();
let (cells, _, _, _, boundary) = look(&mesh);
let out = cleanup(&mesh).unwrap();
let (cells2, _, worst2, _, boundary2) = look(&out);
assert!(cells2 <= cells, "cleanup may only remove cells");
assert!(worst2 > 0.0, "cleanup inverted a cell");
assert_eq!(boundary, boundary2);
}
#[test]
fn merge_triangles_removes_them_two_at_a_time_and_never_inverts() {
let mesh = paved_circle();
let (_, tris, _, _, boundary) = look(&mesh);
assert!(tris >= 2, "the fixture must have triangles to remove");
let out = merge_triangles(&mesh).unwrap();
let (_, tris2, worst2, _, boundary2) = look(&out);
assert!(tris2 < tris, "triangles: {tris} → {tris2}");
assert_eq!(
(tris - tris2) % 2,
0,
"{tris} → {tris2} is not a whole number of pairs"
);
assert!(worst2 > 0.0, "a merge was forced through an invalid cell");
assert_eq!(boundary, boundary2);
}
fn wound_the_other_way(mesh: &Mesh) -> Mesh {
let coords = mesh.coords().unwrap();
let mut out = Mesh::empty();
for si in 0..mesh.len() {
let et = mesh.get(si).unwrap().read().element_type();
let mut sub = SubMesh::new(coords.clone(), et);
for cell in mesh.cells(si).unwrap() {
let mut ids: Vec<NodeId> = cell.nodes().unwrap().iter().map(|n| n.id()).collect();
ids.reverse();
sub.add_cell(&ids).unwrap();
}
out.add_sub(Handle::new(sub)).unwrap();
}
out
}
#[test]
fn cleanup_leaves_the_caller_s_own_nodes_alone_outside_the_rings_it_relaxes() {
let mesh = paved_circle();
let mut before: Vec<(NodeId, [f64; 2])> = Vec::new();
for si in 0..mesh.len() {
for cell in mesh.cells(si).unwrap() {
for n in cell.nodes().unwrap() {
let p = n.position().unwrap();
before.push((n.id(), [p[0], p[1]]));
}
}
}
before.sort_by_key(|(id, _)| *id);
before.dedup_by_key(|(id, _)| *id);
let out = cleanup(&mesh).unwrap();
assert!(
look(&out).0 < look(&mesh).0,
"the fixture must give it work"
);
let (mut shared, mut fresh) = (0usize, 0usize);
for si in 0..out.len() {
for cell in out.cells(si).unwrap() {
for n in cell.nodes().unwrap() {
match before.binary_search_by_key(&n.id(), |(id, _)| *id) {
Ok(k) => {
let p = n.position().unwrap();
assert_eq!([p[0], p[1]], before[k].1, "a shared node moved");
shared += 1;
}
Err(_) => fresh += 1,
}
}
}
}
assert!(fresh > 0, "the rings it collapsed must have been relaxed");
assert!(
fresh * 20 < shared,
"only the collapsed rings may move: {fresh} fresh for {shared} shared"
);
for (id, was) in &before {
let n = Node::acquire(mesh.coords().unwrap(), *id).unwrap();
let p = n.position().unwrap();
assert_eq!([p[0], p[1]], *was);
}
}
#[test]
fn a_clockwise_mesh_is_improved_just_as_much_as_a_counter_clockwise_one() {
let mesh = paved_circle();
let flipped = wound_the_other_way(&mesh);
let (cells, tris, worst, p1, _) = look(&mesh);
let (cells_f, tris_f, worst_f, p1_f, _) = look(&flipped);
assert_eq!((cells, tris), (cells_f, tris_f), "the fixture must match");
let round = |m: &Mesh| {
let m = merge_triangles(m).unwrap();
let m = cleanup(&m).unwrap();
regularize(&m, 30, true, false).unwrap()
};
let (c1, t1, w1, q1, _) = look(&round(&mesh));
let (c2, t2, w2, q2, _) = look(&round(&flipped));
assert!(
w1 > worst && w2 > worst_f,
"both must improve the worst cell"
);
assert!(q1 > p1 && q2 > p1_f, "both must improve the 1st percentile");
assert_eq!((c1, t1), (c2, t2), "and reach the very same mesh");
assert!((w1 - w2).abs() < 1e-12, "{w1} vs {w2}");
let out = cleanup(&flipped).unwrap();
let (n_out, _, _, _, _) = look(&out);
let mut cw = 0usize;
for si in 0..out.len() {
for cell in out.cells(si).unwrap() {
let p: Vec<Point2> = cell
.nodes()
.unwrap()
.iter()
.map(|n| {
let v = n.position().unwrap();
Point2::new(v[0], v[1])
})
.collect();
if crate::ops::mesh::triangulation::signed_area(&p) < 0.0 {
cw += 1;
}
}
}
assert_eq!(cw, n_out, "every cell must come back clockwise");
}
#[test]
fn the_three_compose_and_the_composition_converges() {
let mut mesh = paved_circle();
let (_, tris0, worst0, p10, boundary) = look(&mesh);
let mut seen = Vec::new();
for _ in 0..4 {
mesh = merge_triangles(&mesh).unwrap();
mesh = cleanup(&mesh).unwrap();
mesh = regularize(&mesh, 30, true, false).unwrap();
mesh = regularize(&mesh, 15, false, false).unwrap();
let (_, t, w, _, b) = look(&mesh);
assert!(w > 0.0, "a round left an invalid cell");
assert_eq!(boundary, b, "a round changed the boundary");
seen.push(t);
}
let (_, tris1, worst1, p11, _) = look(&mesh);
assert!(tris1 < tris0 / 2, "triangles: {tris0} → {tris1}");
assert!(worst1 > worst0, "worst cell: {worst0} → {worst1}");
assert!(p11 > p10, "1st percentile: {p10} → {p11}");
assert_eq!(seen[2], seen[3], "the composition must settle: {seen:?}");
}
}