use crate::containers::mesh::Mesh;
use crate::error::Result;
pub fn extrude(mesh: &Mesh, direction: &[f64], n_layers: usize) -> Result<Mesh> {
crate::ops::mesh::sweep_kernel::extrude(mesh, direction, n_layers)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::Aggregate;
use crate::atoms::ElementType;
use crate::atoms::Node;
use crate::containers::mesh::{Mesh, SubMesh};
use crate::coords::Coords;
use crate::handle::Handle;
use crate::ops::mesh::line::line;
#[test]
fn extrude_seg2_to_qua4() {
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(), &[2.0, 0.0]).unwrap();
let seg = line(&a, &b, 2, ElementType::SEG2).unwrap();
let qua = extrude(&seg, &[0.0, 3.0], 3).unwrap();
assert_eq!(qua.element_types().unwrap(), vec![ElementType::QUA4]);
assert_eq!(qua.cell_count(), 6);
let n = qua.node(0, 0, 0).unwrap();
assert_eq!(n.position().unwrap(), vec![0.0, 0.0]);
let n = qua.node(0, 0, 3).unwrap();
assert!((n.position().unwrap()[0]).abs() < 1e-12);
assert!((n.position().unwrap()[1] - 1.0).abs() < 1e-12);
}
#[test]
fn extrude_seg2_shared_nodes_stay_shared() {
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(), &[2.0, 0.0]).unwrap();
let seg = line(&a, &b, 2, ElementType::SEG2).unwrap();
let qua = extrude(&seg, &[0.0, 1.0], 1).unwrap();
let mid_cell0 = qua.node(0, 0, 1).unwrap();
let mid_cell1 = qua.node(0, 1, 0).unwrap();
assert_eq!(mid_cell0.id(), mid_cell1.id());
}
#[test]
fn extrude_qua4_to_hex8() {
let coords = Handle::new(Coords::new(3).unwrap());
let n0 = Node::create_in(coords.clone(), &[0.0, 0.0, 0.0]).unwrap();
let n1 = Node::create_in(coords.clone(), &[1.0, 0.0, 0.0]).unwrap();
let n2 = Node::create_in(coords.clone(), &[1.0, 1.0, 0.0]).unwrap();
let n3 = Node::create_in(coords.clone(), &[0.0, 1.0, 0.0]).unwrap();
let mut qua_mesh = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::QUA4));
qua_mesh
.add_cell(&[n0.id(), n1.id(), n2.id(), n3.id()])
.unwrap();
let hex = extrude(&qua_mesh, &[0.0, 0.0, 2.0], 1).unwrap();
assert_eq!(hex.element_types().unwrap(), vec![ElementType::HEX8]);
assert_eq!(hex.cell_count(), 1);
assert_eq!(hex.node(0, 0, 0).unwrap().id(), n0.id());
assert_eq!(hex.node(0, 0, 1).unwrap().id(), n1.id());
assert_eq!(hex.node(0, 0, 2).unwrap().id(), n2.id());
assert_eq!(hex.node(0, 0, 3).unwrap().id(), n3.id());
let top0 = hex.node(0, 0, 4).unwrap();
assert_eq!(top0.position().unwrap(), vec![0.0, 0.0, 2.0]);
}
#[test]
fn extrude_tri3_to_penta6() {
let coords = Handle::new(Coords::new(3).unwrap());
let n0 = Node::create_in(coords.clone(), &[0.0, 0.0, 0.0]).unwrap();
let n1 = Node::create_in(coords.clone(), &[1.0, 0.0, 0.0]).unwrap();
let n2 = Node::create_in(coords.clone(), &[0.0, 1.0, 0.0]).unwrap();
let mut tri = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
tri.add_cell(&[n0.id(), n1.id(), n2.id()]).unwrap();
let penta = extrude(&tri, &[0.0, 0.0, 2.0], 2).unwrap();
assert_eq!(penta.element_types().unwrap(), vec![ElementType::PENTA6]);
assert_eq!(penta.cell_count(), 2);
assert_eq!(penta.node(0, 0, 0).unwrap().id(), n0.id());
assert_eq!(penta.node(0, 0, 1).unwrap().id(), n1.id());
assert_eq!(penta.node(0, 0, 2).unwrap().id(), n2.id());
let top0 = penta.node(0, 0, 3).unwrap();
assert_eq!(top0.position().unwrap(), vec![0.0, 0.0, 1.0]);
}
fn cell_positions(mesh: &Mesh) -> Vec<Vec<Vec<f64>>> {
let mut out = Vec::new();
for (z, sm) in mesh.into_iter().enumerate() {
let (n_cells, npc) = {
let s = sm.read();
(s.cell_count(), s.element_type().nodes_per_cell())
};
for c in 0..n_cells {
out.push(
(0..npc)
.map(|k| mesh.node(z, c, k).unwrap().position().unwrap())
.collect(),
);
}
}
out
}
#[test]
fn quadratic_extrusion_matches_the_promoted_linear_one() {
use crate::ops::mesh::to_quadratic;
let coords = Handle::new(Coords::new(3).unwrap());
let p = |x: &[f64]| Node::create_in(coords.clone(), x).unwrap();
let n: Vec<Node> = [
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.2, 0.9, 0.0],
[0.1, 1.0, 0.0],
]
.iter()
.map(|x| p(x))
.collect();
let seg = line(&n[0], &n[1], 2, ElementType::SEG2).unwrap();
let mut qua = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::QUA4));
qua.add_cell(&[n[0].id(), n[1].id(), n[2].id(), n[3].id()])
.unwrap();
let mut tri = Mesh::from_submesh(SubMesh::new(coords.clone(), ElementType::TRI3));
tri.add_cell(&[n[0].id(), n[1].id(), n[2].id()]).unwrap();
for (linear, expected) in [
(seg, ElementType::QUA8),
(tri, ElementType::PENTA15),
(qua, ElementType::HEX20),
] {
let direction = if expected == ElementType::QUA8 {
[0.0, 1.5, 0.0]
} else {
[0.0, 0.0, 1.5]
};
let native = extrude(&to_quadratic(&linear).unwrap(), &direction, 3).unwrap();
let promoted = to_quadratic(&extrude(&linear, &direction, 3).unwrap()).unwrap();
assert_eq!(native.element_types().unwrap(), vec![expected]);
let (a, b) = (cell_positions(&native), cell_positions(&promoted));
assert_eq!(a.len(), b.len());
for (ca, cb) in a.iter().zip(b.iter()) {
for (x, y) in ca.iter().zip(cb.iter()) {
for d in 0..3 {
assert!((x[d] - y[d]).abs() < 1e-12, "{expected}: {x:?} vs {y:?}");
}
}
}
}
}
#[test]
fn extrude_refuses_linear_and_quadratic_zones_together() {
use crate::ops::mesh::to_quadratic;
let coords = Handle::new(Coords::new(2).unwrap());
let p = |x: &[f64]| Node::create_in(coords.clone(), x).unwrap();
let seg2 = line(&p(&[0.0, 0.0]), &p(&[1.0, 0.0]), 1, ElementType::SEG2).unwrap();
let seg3 =
to_quadratic(&line(&p(&[2.0, 0.0]), &p(&[3.0, 0.0]), 1, ElementType::SEG2).unwrap())
.unwrap();
let mixed = seg2.union(&seg3).unwrap();
let err = extrude(&mixed, &[0.0, 1.0], 1).unwrap_err();
assert!(err.to_string().contains("linear and quadratic"));
}
#[test]
fn extrude_rejects_zero_layers() {
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 seg = line(&a, &b, 1, ElementType::SEG2).unwrap();
assert!(extrude(&seg, &[0.0, 1.0], 0).is_err());
}
#[test]
fn extrude_rejects_wrong_direction_dim() {
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 seg = line(&a, &b, 1, ElementType::SEG2).unwrap();
assert!(extrude(&seg, &[0.0, 1.0, 0.0], 1).is_err());
}
#[test]
fn extrude_rejects_unsupported_element_type() {
let coords = Handle::new(Coords::new(2).unwrap());
let a = Node::create_in(coords.clone(), &[0.0, 0.0]).unwrap();
let mut pts = Mesh::from_submesh(SubMesh::new(coords, ElementType::POI1));
pts.add_cell(&[a.id()]).unwrap();
assert!(extrude(&pts, &[0.0, 1.0], 1).is_err());
}
}