use crate::atoms::ElementType;
use crate::atoms::Interpolation;
use crate::error::Result;
pub(crate) struct FaceSubdivision {
pub weights: Vec<Vec<f64>>,
pub triangles: Vec<[usize; 3]>,
pub outline: Vec<Vec<f64>>,
}
pub(crate) enum CellSubdivision {
Points,
Segments {
weights: Vec<Vec<f64>>,
segments: Vec<[usize; 2]>,
},
Faces(Vec<FaceSubdivision>),
}
pub(crate) fn subdivide(
et: ElementType,
interp: Interpolation,
n: usize,
) -> Result<CellSubdivision> {
let n = n.max(1);
let k = et.as_kind();
let nodes = k.ref_nodes();
match k.topological_dim() {
0 => Ok(CellSubdivision::Points),
1 => {
let mut weights = Vec::with_capacity(n + 1);
for j in 0..=n {
let xi = -1.0 + 2.0 * j as f64 / n as f64;
weights.push(interp.shape(et, &[xi])?);
}
let segments = (0..n).map(|j| [j, j + 1]).collect();
Ok(CellSubdivision::Segments { weights, segments })
}
2 => {
let c: Vec<&[f64]> = (0..k.corner_count()).map(|j| nodes[j]).collect();
Ok(CellSubdivision::Faces(vec![face_of(et, interp, n, &c)?]))
}
_ => {
let mut faces = Vec::with_capacity(k.facets().len());
for f in k.facets() {
let c: Vec<&[f64]> = f.corners().iter().map(|&li| nodes[li]).collect();
faces.push(face_of(et, interp, n, &c)?);
}
Ok(CellSubdivision::Faces(faces))
}
}
}
fn face_of(
et: ElementType,
interp: Interpolation,
n: usize,
corners: &[&[f64]],
) -> Result<FaceSubdivision> {
match corners.len() {
3 => tri_face(et, interp, n, [corners[0], corners[1], corners[2]]),
4 => quad_face(
et,
interp,
n,
[corners[0], corners[1], corners[2], corners[3]],
),
other => Err(crate::error::PyrucastError::Message(format!(
"subdivide: {et} has a facet with {other} corners (expected 3 or 4)"
))),
}
}
fn combine(points: &[&[f64]], coeffs: &[f64]) -> Vec<f64> {
let dim = points[0].len();
let mut out = vec![0.0; dim];
for (p, &c) in points.iter().zip(coeffs) {
for d in 0..dim {
out[d] += c * p[d];
}
}
out
}
fn tri_face(
et: ElementType,
interp: Interpolation,
n: usize,
corners: [&[f64]; 3],
) -> Result<FaceSubdivision> {
let mut weights = Vec::new();
let mut index = vec![vec![0usize; n + 1]; n + 1];
for i in 0..=n {
for j in 0..=(n - i) {
let (u, v) = (i as f64 / n as f64, j as f64 / n as f64);
let xi = combine(&corners, &[1.0 - u - v, u, v]);
index[i][j] = weights.len();
weights.push(interp.shape(et, &xi)?);
}
}
let mut triangles = Vec::with_capacity(n * n);
for i in 0..n {
for j in 0..(n - i) {
triangles.push([index[i][j], index[i + 1][j], index[i][j + 1]]);
if i + j < n - 1 {
triangles.push([index[i + 1][j], index[i + 1][j + 1], index[i][j + 1]]);
}
}
}
let outline = corners
.iter()
.map(|c| interp.shape(et, c))
.collect::<Result<_>>()?;
Ok(FaceSubdivision {
weights,
triangles,
outline,
})
}
fn quad_face(
et: ElementType,
interp: Interpolation,
n: usize,
corners: [&[f64]; 4],
) -> Result<FaceSubdivision> {
let mut weights = Vec::with_capacity((n + 1) * (n + 1));
for j in 0..=n {
for i in 0..=n {
let s = -1.0 + 2.0 * i as f64 / n as f64;
let t = -1.0 + 2.0 * j as f64 / n as f64;
let coeffs = [
0.25 * (1.0 - s) * (1.0 - t),
0.25 * (1.0 + s) * (1.0 - t),
0.25 * (1.0 + s) * (1.0 + t),
0.25 * (1.0 - s) * (1.0 + t),
];
let xi = combine(&corners, &coeffs);
weights.push(interp.shape(et, &xi)?);
}
}
let idx = |i: usize, j: usize| j * (n + 1) + i;
let mut triangles = Vec::with_capacity(2 * n * n);
for j in 0..n {
for i in 0..n {
triangles.push([idx(i, j), idx(i + 1, j), idx(i + 1, j + 1)]);
triangles.push([idx(i, j), idx(i + 1, j + 1), idx(i, j + 1)]);
}
}
let outline = corners
.iter()
.map(|c| interp.shape(et, c))
.collect::<Result<_>>()?;
Ok(FaceSubdivision {
weights,
triangles,
outline,
})
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_partition_of_unity(weights: &[Vec<f64>]) {
for w in weights {
let s: f64 = w.iter().sum();
assert!((s - 1.0).abs() < 1e-12, "Σ N_i = {s} ≠ 1");
}
}
#[test]
fn every_element_type_subdivides() {
for &et in ElementType::ALL {
let k = et.as_kind();
let interp = k.degree().unwrap_or(Interpolation::Lagrange1);
let sub = subdivide(et, interp, 2).unwrap();
match (k.topological_dim(), sub) {
(0, CellSubdivision::Points) => {}
(1, CellSubdivision::Segments { weights, segments }) => {
assert_eq!(segments.len(), 2, "{et}: n segments");
assert_partition_of_unity(&weights);
}
(2, CellSubdivision::Faces(f)) => {
assert_eq!(f.len(), 1, "{et}: a surface draws as one face");
assert_partition_of_unity(&f[0].weights);
}
(3, CellSubdivision::Faces(f)) => {
assert_eq!(f.len(), k.facets().len(), "{et}: one face per facet");
for face in &f {
assert_partition_of_unity(&face.weights);
}
}
(d, _) => panic!("{et}: dimension {d} gave the wrong subdivision"),
}
}
}
#[test]
fn tri3_counts_and_unity() {
let n = 4;
let CellSubdivision::Faces(faces) =
subdivide(ElementType::TRI3, Interpolation::Lagrange1, n).unwrap()
else {
panic!("expected faces");
};
assert_eq!(faces.len(), 1);
assert_eq!(faces[0].triangles.len(), n * n);
assert_eq!(faces[0].weights.len(), (n + 1) * (n + 2) / 2);
assert_partition_of_unity(&faces[0].weights);
assert_eq!(faces[0].outline.len(), 3);
}
#[test]
fn qua4_counts_and_unity() {
let n = 3;
let CellSubdivision::Faces(faces) =
subdivide(ElementType::QUA4, Interpolation::Lagrange1, n).unwrap()
else {
panic!("expected faces");
};
assert_eq!(faces.len(), 1);
assert_eq!(faces[0].triangles.len(), 2 * n * n);
assert_eq!(faces[0].weights.len(), (n + 1) * (n + 1));
assert_partition_of_unity(&faces[0].weights);
assert_eq!(faces[0].outline.len(), 4);
}
#[test]
fn tet4_and_hex8_face_counts() {
let n = 2;
let CellSubdivision::Faces(tet) =
subdivide(ElementType::TET4, Interpolation::Lagrange1, n).unwrap()
else {
panic!("expected faces");
};
assert_eq!(tet.len(), 4);
for f in &tet {
assert_eq!(f.triangles.len(), n * n);
assert_partition_of_unity(&f.weights);
}
let CellSubdivision::Faces(hex) =
subdivide(ElementType::HEX8, Interpolation::Lagrange1, n).unwrap()
else {
panic!("expected faces");
};
assert_eq!(hex.len(), 6);
for f in &hex {
assert_eq!(f.triangles.len(), 2 * n * n);
assert_partition_of_unity(&f.weights);
}
}
#[test]
fn seg2_segments() {
let CellSubdivision::Segments { weights, segments } =
subdivide(ElementType::SEG2, Interpolation::Lagrange1, 5).unwrap()
else {
panic!("expected segments");
};
assert_eq!(weights.len(), 6);
assert_eq!(segments.len(), 5);
assert_partition_of_unity(&weights);
}
#[test]
fn corners_are_kronecker() {
let CellSubdivision::Faces(faces) =
subdivide(ElementType::TRI3, Interpolation::Lagrange1, 2).unwrap()
else {
panic!("expected faces");
};
let w0 = &faces[0].weights[0]; assert!((w0[0] - 1.0).abs() < 1e-12);
assert!(w0[1].abs() < 1e-12 && w0[2].abs() < 1e-12);
}
}