use crate::atoms::ElementType;
use crate::atoms::Node;
use crate::atoms::NodeId;
use crate::containers::mesh::{Mesh, SubMesh};
use crate::error::{PyrucastError, Result};
pub fn line(a: &Node, b: &Node, n_elems: usize, element_type: ElementType) -> Result<Mesh> {
if !matches!(element_type, ElementType::SEG2 | ElementType::SEG3) {
return Err(PyrucastError::Message(format!(
"line: unsupported element type {element_type} (expected SEG2 or SEG3)"
)));
}
if n_elems == 0 {
return Err(PyrucastError::Message("line: n_elems must be ≥ 1".into()));
}
let coords = a.coords();
let coords_b = b.coords();
if !coords.same_object(&coords_b) {
return Err(PyrucastError::Message(
"line: nodes belong to different Coords".into(),
));
}
let coords_a = a.position()?;
let coords_b = b.position()?;
if coords_a.len() != coords_b.len() {
return Err(PyrucastError::Message(
"line: nodes have incompatible dimensions".into(),
));
}
let mut flat: Vec<f64> = Vec::new();
for i in 1..n_elems {
let t = i as f64 / n_elems as f64;
flat.extend(
coords_a
.iter()
.zip(coords_b.iter())
.map(|(&ca, &cb)| ca + t * (cb - ca)),
);
}
let first = coords.write().add_nodes(&flat)?.start;
let at = |i: usize| -> NodeId {
if i == 0 {
a.id()
} else if i == n_elems {
b.id()
} else {
NodeId(first + (i - 1) as u32)
}
};
let conn: Vec<NodeId> = (0..n_elems).flat_map(|i| [at(i), at(i + 1)]).collect();
let sm = SubMesh::from_connectivity(coords.clone(), ElementType::SEG2, conn)?;
let owned: Vec<NodeId> = (first..first + n_elems.saturating_sub(1) as u32)
.map(NodeId)
.collect();
coords.write().decref_all(&owned)?;
let mut mesh = Mesh::from_submesh(sm);
if element_type == ElementType::SEG3 {
mesh = super::to_quadratic(&mesh)?;
}
Ok(mesh)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::atoms::ElementType;
use crate::atoms::Node;
use crate::coords::Coords;
use crate::handle::Handle;
#[test]
fn line_basic() {
let coords = Handle::new(Coords::new(1).unwrap());
let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[6.0]).unwrap();
let mesh = line(&a, &b, 3, ElementType::SEG2).unwrap();
assert_eq!(mesh.element_types().unwrap(), vec![ElementType::SEG2]);
assert_eq!(mesh.cell_count(), 3);
let n00 = mesh.node(0, 0, 0).unwrap();
let n10 = mesh.node(0, 1, 0).unwrap();
let n20 = mesh.node(0, 2, 0).unwrap();
assert_eq!(n00.position().unwrap(), vec![0.0]);
assert!((n10.position().unwrap()[0] - 2.0).abs() < 1e-12);
assert!((n20.position().unwrap()[0] - 4.0).abs() < 1e-12);
let n21 = mesh.node(0, 2, 1).unwrap();
assert_eq!(n21.id(), b.id());
}
#[test]
fn line_one_element() {
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, 1.0]).unwrap();
let mesh = line(&a, &b, 1, ElementType::SEG2).unwrap();
assert_eq!(mesh.cell_count(), 1);
assert_eq!(mesh.node(0, 0, 0).unwrap().id(), a.id());
assert_eq!(mesh.node(0, 0, 1).unwrap().id(), b.id());
}
#[test]
fn line_zero_elems_is_error() {
let coords = Handle::new(Coords::new(1).unwrap());
let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
assert!(line(&a, &b, 0, ElementType::SEG2).is_err());
}
#[test]
fn line_seg3_basic() {
let coords = Handle::new(Coords::new(1).unwrap());
let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[4.0]).unwrap();
let mesh = line(&a, &b, 2, ElementType::SEG3).unwrap();
assert_eq!(mesh.element_types().unwrap(), vec![ElementType::SEG3]);
assert_eq!(mesh.cell_count(), 2);
let n00 = mesh.node(0, 0, 0).unwrap();
let n01 = mesh.node(0, 0, 1).unwrap();
let n02 = mesh.node(0, 0, 2).unwrap();
assert_eq!(n00.position().unwrap(), vec![0.0]);
assert!((n01.position().unwrap()[0] - 2.0).abs() < 1e-12);
assert!((n02.position().unwrap()[0] - 1.0).abs() < 1e-12);
let n11 = mesh.node(0, 1, 1).unwrap();
assert_eq!(n11.id(), b.id());
}
#[test]
fn line_unsupported_element_type_is_error() {
let coords = Handle::new(Coords::new(1).unwrap());
let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
assert!(line(&a, &b, 1, ElementType::TRI3).is_err());
}
}