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::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().unwrap(), 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().unwrap(), 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().unwrap(), 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]);
}
#[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());
}
}