use crate::aggregate::Aggregate;
use crate::atoms::NodeId;
use crate::containers::mesh::{Mesh, SubMesh};
use crate::handle::{Handle, ReadGuard};
use crate::parallel::*;
use std::collections::HashSet;
pub fn intersection(a: &Mesh, b: &Mesh) -> Mesh {
let mut out = Mesh::empty();
for zone in a {
out.add_sub(keep_cells(zone, b, true))
.expect("the zones of `a` share its Coords");
}
out
}
pub fn difference(a: &Mesh, b: &Mesh) -> Mesh {
let mut out = Mesh::empty();
for zone in a {
out.add_sub(keep_cells(zone, b, false))
.expect("the zones of `a` share its Coords");
}
out
}
pub(crate) fn keep_cells(zone: &Handle<SubMesh>, b: &Mesh, in_b: bool) -> Handle<SubMesh> {
let (coords, element_type, color, total) = {
let z = zone.read();
(
z.coords(),
z.element_type(),
z.face_color(),
z.connectivity().len(),
)
};
let whole = b.iter().any(|h| h.same_object(zone));
if whole && in_b {
return zone.clone();
}
let kept: Vec<NodeId> = if whole {
Vec::new()
} else {
let z = zone.read();
let npc = element_type.nodes_per_cell();
let candidates: Vec<ReadGuard<SubMesh>> = b
.iter()
.map(|h| h.read())
.filter(|s| s.element_type() == element_type && s.coords().same_object(&coords))
.collect();
let held: HashSet<&[NodeId]> = candidates
.iter()
.flat_map(|s| s.connectivity().chunks_exact(npc))
.collect();
z.connectivity()
.par_chunks_exact(npc)
.with_min_len(MIN_PARALLEL_LEN)
.filter(|cell| held.contains(cell) == in_b)
.flat_map_iter(|cell| cell.iter().copied())
.collect()
};
if kept.len() == total {
return zone.clone();
}
let mut part = SubMesh::from_connectivity(coords, element_type, kept)
.expect("the kept cells are cells of `zone`, on its Coords");
part.set_face_color(color);
Handle::new(part)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atoms::{ElementType, Node};
use crate::coords::Coords;
fn make_grid() -> (Handle<Coords>, Vec<Node>, Mesh, Mesh) {
let coords = Handle::new(Coords::new(2).unwrap());
let n: Vec<Node> = (0..9)
.map(|k| Node::create_in(coords.clone(), &[(k % 3) as f64, (k / 3) as f64]).unwrap())
.collect();
let mut grid = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::QUA4));
for (i, j) in [(0, 0), (1, 0), (0, 1), (1, 1)] {
let k = 3 * j + i;
grid.add_cell(&[n[k].id(), n[k + 1].id(), n[k + 4].id(), n[k + 3].id()])
.unwrap();
}
let left: Vec<Node> = [0, 1, 3, 4, 6, 7].iter().map(|&k| n[k].clone()).collect();
let left = crate::ops::mesh::poi1_from_nodes(&left).unwrap();
let sel = crate::ops::mesh::elements_on(&grid, &left, true).unwrap();
(coords, n, grid, sel)
}
fn cells(mesh: &Mesh) -> Vec<Vec<NodeId>> {
let s = mesh.get(0).unwrap().read();
let npc = s.element_type().nodes_per_cell();
s.connectivity()
.chunks_exact(npc)
.map(|c| c.to_vec())
.collect()
}
#[test]
fn the_grid_splits_into_its_two_columns() {
let (_c, _n, grid, sel) = make_grid();
let all = cells(&grid);
assert_eq!(cells(&sel), vec![all[0].clone(), all[2].clone()]);
assert_eq!(
cells(&intersection(&grid, &sel)),
vec![all[0].clone(), all[2].clone()]
);
assert_eq!(
cells(&difference(&grid, &sel)),
vec![all[1].clone(), all[3].clone()]
);
let (inside, outside) = (intersection(&grid, &sel), difference(&grid, &sel));
assert_eq!(
inside.cell_count() + outside.cell_count(),
grid.cell_count()
);
assert_eq!(intersection(&inside, &outside).cell_count(), 0);
}
#[test]
fn a_zone_kept_whole_is_the_zone_itself() {
let (_c, _n, grid, sel) = make_grid();
let zone = grid.get(0).unwrap();
assert!(intersection(&grid, &grid)
.get(0)
.unwrap()
.same_object(&zone));
assert_eq!(
difference(&grid, &grid).get(0).unwrap().read().cell_count(),
0
);
let copy = intersection(&sel, &grid);
assert!(copy.get(0).unwrap().same_object(&sel.get(0).unwrap()));
let other = difference(&grid, &sel);
assert!(difference(&other, &sel)
.get(0)
.unwrap()
.same_object(&other.get(0).unwrap()));
}
#[test]
fn a_cell_numbered_from_another_node_is_not_the_same() {
let (coords, _n, grid, _sel) = make_grid();
let first = cells(&grid)[0].clone();
let mut turned = SubMesh::new(coords.clone(), ElementType::QUA4);
turned
.add_cell(&[first[1], first[2], first[3], first[0]])
.unwrap();
let turned = Mesh::from_submesh(turned);
assert_eq!(intersection(&grid, &turned).cell_count(), 0);
let mut same = SubMesh::new(coords, ElementType::QUA4);
same.add_cell(&first).unwrap();
assert_eq!(
intersection(&grid, &Mesh::from_submesh(same)).cell_count(),
1
);
}
#[test]
fn a_zone_without_common_cell_stays_empty_in_place() {
let (coords, n, grid, _sel) = make_grid();
let mut a = Mesh::empty();
a.add_sub(grid.get(0).unwrap()).unwrap();
let mut seg = SubMesh::new(coords, ElementType::SEG2);
seg.add_cell(&[n[0].id(), n[1].id()]).unwrap();
a.add_sub(Handle::new(seg)).unwrap();
let r = intersection(&a, &grid);
assert_eq!(r.len(), 2);
assert_eq!(r.cell_counts().unwrap(), vec![4, 0]);
assert_eq!(
r.element_types().unwrap(),
vec![ElementType::QUA4, ElementType::SEG2]
);
}
#[test]
fn another_coords_shares_no_cell() {
let (_c, _n, grid, _sel) = make_grid();
let (_c2, _n2, other, _sel2) = make_grid();
assert_eq!(intersection(&grid, &other).cell_count(), 0);
let rest = difference(&grid, &other);
assert!(rest.get(0).unwrap().same_object(&grid.get(0).unwrap()));
}
#[test]
fn a_new_zone_keeps_the_face_colour() {
let (_c, _n, grid, sel) = make_grid();
let color = crate::atoms::RgbColor::new(10, 20, 30);
grid.get(0).unwrap().write().set_face_color(color);
assert_eq!(
difference(&grid, &sel).get(0).unwrap().read().face_color(),
color
);
}
}