use crate::atoms::ElementType;
use crate::containers::mesh::Mesh;
use crate::ops::mesh::{
CellBlock, CellLayout, CellValues, FrontRelax, GaussRule, NodeOrder, NodeValues,
};
use crate::py::coords::PyCoords;
use crate::py::element_field::{PyElementField, PySubElementField};
use crate::py::mesh::PyMesh;
use crate::py::node::PyNode;
use crate::py::node_field::{PyNodeField, PySubNodeField};
use crate::py::signals::PySignals;
use pyo3::exceptions::PyTypeError;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyrucast_macros::py_op;
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn from_live_nodes(coords: PyRef<PyCoords>) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::from_live_nodes(coords.handle.clone())?;
Ok(PyMesh { inner: mesh })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn poi1_from_nodes(nodes: Vec<PyRef<PyNode>>) -> PyResult<PyMesh> {
let ns: Vec<crate::atoms::Node> = nodes.iter().map(|n| n.as_node().clone()).collect();
let mesh = crate::ops::mesh::poi1_from_nodes(&ns)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn to_poi1(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::to_poi1(&mesh.inner)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn barycenter(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::barycenter(&mesh.inner)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, points, strict=true))]
pub fn elements_on(mesh: PyRef<PyMesh>, points: PyRef<PyMesh>, strict: bool) -> PyResult<PyMesh> {
let result = crate::ops::mesh::elements_on(&mesh.inner, &points.inner, strict)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, radius, tol=None))]
pub fn points_in_sphere(
mesh: PyRef<PyMesh>,
center: Vec<f64>,
radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_in_sphere(&mesh.inner, ¢er, radius, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, radius, tol=None))]
pub fn points_on_sphere(
mesh: PyRef<PyMesh>,
center: Vec<f64>,
radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_on_sphere(&mesh.inner, ¢er, radius, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, origin, normal, tol=None))]
pub fn points_on_plane(
mesh: PyRef<PyMesh>,
origin: Vec<f64>,
normal: Vec<f64>,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_on_plane(&mesh.inner, &origin, &normal, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, origin, normal, tol=None))]
pub fn points_below_plane(
mesh: PyRef<PyMesh>,
origin: Vec<f64>,
normal: Vec<f64>,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_below_plane(&mesh.inner, &origin, &normal, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, a, b, tol=None))]
pub fn points_on_line(
mesh: PyRef<PyMesh>,
a: Vec<f64>,
b: Vec<f64>,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_on_line(&mesh.inner, &a, &b, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, radius, tol=None))]
pub fn points_in_cylinder(
mesh: PyRef<PyMesh>,
base: Vec<f64>,
top: Vec<f64>,
radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_in_cylinder(&mesh.inner, &base, &top, radius, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, radius, tol=None))]
pub fn points_on_cylinder(
mesh: PyRef<PyMesh>,
base: Vec<f64>,
top: Vec<f64>,
radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_on_cylinder(&mesh.inner, &base, &top, radius, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, base_radius, top_radius=0.0, tol=None))]
pub fn points_in_cone(
mesh: PyRef<PyMesh>,
base: Vec<f64>,
top: Vec<f64>,
base_radius: f64,
top_radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result =
crate::ops::mesh::points_in_cone(&mesh.inner, &base, &top, base_radius, top_radius, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, base, top, base_radius, top_radius=0.0, tol=None))]
pub fn points_on_cone(
mesh: PyRef<PyMesh>,
base: Vec<f64>,
top: Vec<f64>,
base_radius: f64,
top_radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result =
crate::ops::mesh::points_on_cone(&mesh.inner, &base, &top, base_radius, top_radius, tol)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, axis, major_radius, minor_radius, tol=None))]
pub fn points_in_torus(
mesh: PyRef<PyMesh>,
center: Vec<f64>,
axis: Vec<f64>,
major_radius: f64,
minor_radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_in_torus(
&mesh.inner,
¢er,
&axis,
major_radius,
minor_radius,
tol,
)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, center, axis, major_radius, minor_radius, tol=None))]
pub fn points_on_torus(
mesh: PyRef<PyMesh>,
center: Vec<f64>,
axis: Vec<f64>,
major_radius: f64,
minor_radius: f64,
tol: Option<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::points_on_torus(
&mesh.inner,
¢er,
&axis,
major_radius,
minor_radius,
tol,
)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(name = "consolidate_mesh")]
pub fn consolidate(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
Ok(PyMesh {
inner: crate::ops::mesh::consolidate(&mesh.inner)?,
})
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, tol, in_place=false))]
pub fn merge_nodes(
py: Python<'_>,
mesh: Py<PyMesh>,
tol: f64,
in_place: bool,
) -> PyResult<Py<PyMesh>> {
let result = crate::ops::mesh::merge_nodes(&mesh.borrow(py).inner, tol, in_place)?;
if in_place {
return Ok(mesh);
}
Py::new(py, PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle_deg=None))]
pub fn border(mesh: PyRef<PyMesh>, angle_deg: Option<f64>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::border(&mesh.inner, angle_deg)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle_deg=None))]
pub fn skin(mesh: PyRef<PyMesh>, angle_deg: Option<f64>) -> PyResult<PyMesh> {
let angle_deg = angle_deg.unwrap_or(crate::ops::mesh::skin::DEFAULT_ANGLE_DEG);
let result = crate::ops::mesh::skin(&mesh.inner, angle_deg)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn orient(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::orient(&mesh.inner)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn chain(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::chain(&mesh.inner)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn invert(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::invert(&mesh.inner)?;
Ok(PyMesh { inner: result })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (a, b, n_elems, element_type=None))]
pub fn line(
a: PyRef<PyNode>,
b: PyRef<PyNode>,
n_elems: usize,
element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
let element_type = element_type.unwrap_or(ElementType::SEG2);
let mesh = crate::ops::mesh::line(a.as_node(), b.as_node(), n_elems, element_type)?;
Ok(PyMesh { inner: mesh })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (center, normal, radius, n_elems, element_type=None))]
pub fn circle(
center: PyRef<PyNode>,
normal: Vec<f64>,
radius: f64,
n_elems: usize,
element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
let element_type = element_type.unwrap_or(ElementType::SEG2);
let mesh = crate::ops::mesh::circle(center.as_node(), &normal, radius, n_elems, element_type)?;
Ok(PyMesh { inner: mesh })
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (a, center, b, n_elems, element_type=None))]
pub fn arc(
a: PyRef<PyNode>,
center: PyRef<PyNode>,
b: PyRef<PyNode>,
n_elems: usize,
element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
let element_type = element_type.unwrap_or(ElementType::SEG2);
let mesh = crate::ops::mesh::arc(
a.as_node(),
center.as_node(),
b.as_node(),
n_elems,
element_type,
)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh_a, mesh_b, n_layers, element_type=None))]
pub fn sweep(
mesh_a: PyRef<PyMesh>,
mesh_b: PyRef<PyMesh>,
n_layers: usize,
element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
let element_type = element_type.unwrap_or(ElementType::QUA4);
let mesh = crate::ops::mesh::sweep(&mesh_a.inner, &mesh_b.inner, n_layers, element_type)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (side1, side2, side3, side4, element_type=None))]
pub fn transfinite(
side1: PyRef<PyMesh>,
side2: PyRef<PyMesh>,
side3: PyRef<PyMesh>,
side4: PyRef<PyMesh>,
element_type: Option<ElementType>,
) -> PyResult<PyMesh> {
let element_type = element_type.unwrap_or(ElementType::QUA4);
let mesh = crate::ops::mesh::transfinite(
&side1.inner,
&side2.inner,
&side3.inner,
&side4.inner,
element_type,
)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn extrude(mesh: PyRef<PyMesh>, direction: Vec<f64>, n_layers: usize) -> PyResult<PyMesh> {
let result = crate::ops::mesh::extrude(&mesh.inner, &direction, n_layers)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle, n_layers, center, axis=None))]
pub fn revolve(
mesh: PyRef<PyMesh>,
angle: f64,
n_layers: usize,
center: Vec<f64>,
axis: Option<Vec<f64>>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::revolve(&mesh.inner, angle, n_layers, ¢er, axis.as_deref())?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn sweep_solid(
mesh_a: PyRef<PyMesh>,
mesh_b: PyRef<PyMesh>,
n_layers: usize,
) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::sweep_solid(&mesh_a.inner, &mesh_b.inner, n_layers)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn to_quadratic(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::to_quadratic(&mesh.inner)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn convert(mesh: PyRef<PyMesh>, element_type: ElementType) -> PyResult<PyMesh> {
let result = crate::ops::mesh::convert(&mesh.inner, element_type)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, new_nodes=true))]
pub fn copy(mesh: PyRef<PyMesh>, new_nodes: bool) -> PyResult<PyMesh> {
let result = crate::ops::mesh::copy(&mesh.inner, new_nodes)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn translate(mesh: PyRef<PyMesh>, vector: Vec<f64>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::translate(&mesh.inner, &vector)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, angle, center, axis=None))]
pub fn rotate(
mesh: PyRef<PyMesh>,
angle: f64,
center: Vec<f64>,
axis: Option<Vec<f64>>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::rotate(&mesh.inner, angle, ¢er, axis.as_deref())?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn symmetry_point(mesh: PyRef<PyMesh>, center: Vec<f64>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::symmetry_point(&mesh.inner, ¢er)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn symmetry_line(mesh: PyRef<PyMesh>, a: Vec<f64>, b: Vec<f64>) -> PyResult<PyMesh> {
let result = crate::ops::mesh::symmetry_line(&mesh.inner, &a, &b)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn symmetry_plane(
mesh: PyRef<PyMesh>,
a: Vec<f64>,
b: Vec<f64>,
c: Vec<f64>,
) -> PyResult<PyMesh> {
let result = crate::ops::mesh::symmetry_plane(&mesh.inner, &a, &b, &c)?;
Ok(PyMesh { inner: result })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None))]
pub fn triangulate_surface(
py: Python<'_>,
contour: PyRef<PyMesh>,
element_type: ElementType,
size: Option<f64>,
) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::triangulate_surface_cancellable(
&contour.inner,
element_type,
size,
&PySignals(py),
)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None, all_quad=false, relax=None))]
pub fn pave_surface(
py: Python<'_>,
contour: PyRef<PyMesh>,
element_type: ElementType,
size: Option<f64>,
all_quad: bool,
relax: Option<FrontRelax>,
) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::pave_surface_cancellable(
&contour.inner,
element_type,
size,
all_quad,
relax.unwrap_or_default(),
&PySignals(py),
)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (mesh, sweeps=20, angular=true, in_place=false))]
pub fn regularize(
mesh: PyRef<PyMesh>,
sweeps: usize,
angular: bool,
in_place: bool,
) -> PyResult<PyMesh> {
let out = crate::ops::mesh::regularize(&mesh.inner, sweeps, angular, in_place)?;
Ok(PyMesh { inner: out })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn cleanup(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let out = crate::ops::mesh::cleanup(&mesh.inner)?;
Ok(PyMesh { inner: out })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn merge_triangles(mesh: PyRef<PyMesh>) -> PyResult<PyMesh> {
let out = crate::ops::mesh::merge_triangles(&mesh.inner)?;
Ok(PyMesh { inner: out })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None, band=0, all_quad=false, relax=None))]
pub fn grid_surface(
py: Python<'_>,
contour: PyRef<PyMesh>,
element_type: ElementType,
size: Option<f64>,
band: usize,
all_quad: bool,
relax: Option<FrontRelax>,
) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::grid_surface_cancellable(
&contour.inner,
element_type,
size,
band,
all_quad,
relax.unwrap_or_default(),
&PySignals(py),
)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (contour, element_type, size=None, band=0, all_quad=false, relax=None))]
pub fn grid_surface2(
py: Python<'_>,
contour: PyRef<PyMesh>,
element_type: ElementType,
size: Option<f64>,
band: usize,
all_quad: bool,
relax: Option<FrontRelax>,
) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::grid_surface2_cancellable(
&contour.inner,
element_type,
size,
band,
all_quad,
relax.unwrap_or_default(),
&PySignals(py),
)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (envelope, layers=1, thickness=None, size=None))]
pub fn pave_volume(
py: Python<'_>,
envelope: PyRef<PyMesh>,
layers: usize,
thickness: Option<f64>,
size: Option<f64>,
) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::pave_volume_cancellable(
&envelope.inner,
layers,
thickness,
size,
&PySignals(py),
)?;
Ok(PyMesh { inner: mesh })
}
#[py_op(method_on = PyMesh)]
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (envelope, size=None, allow_surface_nodes=false))]
pub fn triangulate_volume(
py: Python<'_>,
envelope: PyRef<PyMesh>,
size: Option<f64>,
allow_surface_nodes: bool,
) -> PyResult<PyMesh> {
let mesh = crate::ops::mesh::triangulate_volume_cancellable(
&envelope.inner,
size,
allow_surface_nodes,
&PySignals(py),
)?;
Ok(PyMesh { inner: mesh })
}
fn groups_to_dict<'py>(
py: Python<'py>,
groups: Vec<(String, Mesh)>,
) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new(py);
for (name, mesh) in groups {
dict.set_item(name, Py::new(py, PyMesh { inner: mesh })?)?;
}
Ok(dict)
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn element_type_from_gmsh(code: u32) -> PyResult<&'static str> {
Ok(crate::ops::mesh::gmsh::element_type_from_gmsh(code)?.name())
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn gauss_to_external(
element_type: ElementType,
ref_nodes: Vec<f64>,
order: &str,
) -> PyResult<(Vec<f64>, Vec<f64>)> {
let order = crate::py::arrays::parse_order(order)?;
Ok(crate::ops::mesh::gauss_map::gauss_to_external(
element_type,
order,
&ref_nodes,
)?)
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn match_gauss(
element_type: ElementType,
ref_nodes: Vec<f64>,
xi: Vec<f64>,
weights: Vec<f64>,
order: &str,
) -> PyResult<Vec<usize>> {
let order = crate::py::arrays::parse_order(order)?;
Ok(crate::ops::mesh::gauss_map::match_gauss(
element_type,
order,
&ref_nodes,
&xi,
&weights,
)?)
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (coords, node_tags, node_coords, blocks, *, node_fields = Vec::new(), cell_fields = Vec::new(), order = "pyrucast"))]
#[allow(clippy::too_many_arguments)]
pub fn from_arrays<'py>(
py: Python<'py>,
coords: PyRef<PyCoords>,
node_tags: &Bound<'py, PyAny>,
node_coords: &Bound<'py, PyAny>,
blocks: &Bound<'py, PyAny>,
node_fields: Vec<Py<PyAny>>,
cell_fields: Vec<Py<PyAny>>,
order: &str,
) -> PyResult<(Bound<'py, PyDict>, Vec<PyNodeField>, Vec<PyElementField>)> {
let order = crate::py::arrays::parse_order(order)?;
let signed = pyo3::buffer::PyBuffer::<i64>::get(node_tags).is_ok();
let node_fields: Vec<Bound<'py, PyAny>> =
node_fields.into_iter().map(|f| f.into_bound(py)).collect();
let cell_fields: Vec<Bound<'py, PyAny>> =
cell_fields.into_iter().map(|f| f.into_bound(py)).collect();
let inputs = ExchangeInputs::unpack(blocks, &node_fields, &cell_fields)?;
let out = if signed {
inputs.import::<i64>(coords.handle.clone(), node_tags, node_coords, order)?
} else {
inputs.import::<u64>(coords.handle.clone(), node_tags, node_coords, order)?
};
Ok((
groups_to_dict(py, out.groups)?,
out.node_fields
.into_iter()
.map(|inner| PyNodeField { inner })
.collect(),
out.element_fields
.into_iter()
.map(|inner| PyElementField { inner })
.collect(),
))
}
struct ExchangeInputs<'py> {
blocks: Vec<BlockObjects<'py>>,
node_fields: Vec<(Vec<String>, Bound<'py, PyAny>, Bound<'py, PyAny>)>,
cell_fields: Vec<CellFieldObjects<'py>>,
}
type BlockObjects<'py> = (
ElementType,
Bound<'py, PyAny>,
Option<Bound<'py, PyAny>>,
Vec<String>,
);
type CellFieldObjects<'py> = (
Vec<String>,
Bound<'py, PyAny>,
Bound<'py, PyAny>,
Option<Vec<RuleObjects<'py>>>,
);
type RuleObjects<'py> = (
ElementType,
Bound<'py, PyAny>,
Bound<'py, PyAny>,
Bound<'py, PyAny>,
);
fn nth<'py>(item: &Bound<'py, PyAny>, k: usize, what: &str) -> PyResult<Bound<'py, PyAny>> {
item.get_item(k)
.map_err(|_| PyValueError::new_err(format!("{what}: missing entry {k}")))
}
impl<'py> ExchangeInputs<'py> {
fn unpack(
blocks: &Bound<'py, PyAny>,
node_fields: &[Bound<'py, PyAny>],
cell_fields: &[Bound<'py, PyAny>],
) -> PyResult<Self> {
let mut bs = Vec::new();
for (i, item) in blocks.try_iter()?.enumerate() {
let item = item?;
let what = format!("block {i}: expected (element_type, node_tags, cell_tags, groups)");
let (cells, groups) = match item.len()? {
3 => (None, nth(&item, 2, &what)?),
4 => (Some(nth(&item, 2, &what)?), nth(&item, 3, &what)?),
_ => return Err(PyValueError::new_err(what)),
};
bs.push((
nth(&item, 0, &what)?.extract()?,
nth(&item, 1, &what)?,
cells,
groups.extract()?,
));
}
let mut nfs = Vec::new();
for (i, item) in node_fields.iter().enumerate() {
let what = format!("node field {i}: expected (components, node_tags, values)");
nfs.push((
nth(item, 0, &what)?.extract()?,
nth(item, 1, &what)?,
nth(item, 2, &what)?,
));
}
let mut cfs = Vec::new();
for (i, item) in cell_fields.iter().enumerate() {
let what = format!("cell field {i}: expected (components, cell_tags, values, layout)");
let layout = nth(item, 3, &what)?;
let rules = if let Ok(s) = layout.extract::<String>() {
if !s.trim().eq_ignore_ascii_case("cell") {
return Err(PyValueError::new_err(format!(
"cell field {i}: layout '{s}' is neither 'cell' nor a list of Gauss rules"
)));
}
None
} else {
let mut rules = Vec::new();
for r in layout.try_iter()? {
let r = r?;
let what = format!("cell field {i}: expected Gauss rules (element_type, ref_nodes, xi, weights)");
rules.push((
nth(&r, 0, &what)?.extract()?,
nth(&r, 1, &what)?,
nth(&r, 2, &what)?,
nth(&r, 3, &what)?,
));
}
Some(rules)
};
cfs.push((
nth(item, 0, &what)?.extract()?,
nth(item, 1, &what)?,
nth(item, 2, &what)?,
rules,
));
}
Ok(Self {
blocks: bs,
node_fields: nfs,
cell_fields: cfs,
})
}
fn import<T>(
&self,
coords: crate::handle::Handle<crate::coords::Coords>,
node_tags: &Bound<'py, PyAny>,
node_coords: &Bound<'py, PyAny>,
order: NodeOrder,
) -> PyResult<crate::ops::mesh::Imported>
where
T: crate::ops::mesh::Tag + pyo3::buffer::Element + for<'b, 'p> FromPyObject<'b, 'p>,
{
use crate::py::arrays::borrow;
let tags = borrow::<T>(node_tags)?;
let xyz = borrow::<f64>(node_coords)?;
let conns = self
.blocks
.iter()
.map(|b| borrow::<T>(&b.1))
.collect::<PyResult<Vec<_>>>()?;
let cells = self
.blocks
.iter()
.map(|b| b.2.as_ref().map(borrow::<T>).transpose())
.collect::<PyResult<Vec<_>>>()?;
let blocks: Vec<CellBlock<'_, T>> = self
.blocks
.iter()
.zip(&conns)
.zip(&cells)
.map(|((b, conn), cells)| CellBlock {
element_type: b.0,
node_tags: conn.as_slice(),
cell_tags: cells.as_ref().map_or(&[], |c| c.as_slice()),
groups: &b.3,
})
.collect();
let nf_arrays = self
.node_fields
.iter()
.map(|f| Ok((borrow::<T>(&f.1)?, borrow::<f64>(&f.2)?)))
.collect::<PyResult<Vec<_>>>()?;
let node_values: Vec<NodeValues<'_, T>> = self
.node_fields
.iter()
.zip(&nf_arrays)
.map(|(f, (t, v))| NodeValues {
components: &f.0,
node_tags: t.as_slice(),
values: v.as_slice(),
})
.collect();
let cf_arrays = self
.cell_fields
.iter()
.map(|f| Ok((borrow::<T>(&f.1)?, borrow::<f64>(&f.2)?)))
.collect::<PyResult<Vec<_>>>()?;
let rule_arrays = self
.cell_fields
.iter()
.map(|f| {
f.3.iter()
.flatten()
.map(|r| {
Ok((
borrow::<f64>(&r.1)?,
borrow::<f64>(&r.2)?,
borrow::<f64>(&r.3)?,
))
})
.collect::<PyResult<Vec<_>>>()
})
.collect::<PyResult<Vec<_>>>()?;
let rules: Vec<Vec<GaussRule<'_>>> = self
.cell_fields
.iter()
.zip(&rule_arrays)
.map(|(f, arrays)| {
f.3.iter()
.flatten()
.zip(arrays)
.map(|(r, (refs, xi, w))| GaussRule {
element_type: r.0,
ref_nodes: refs.as_slice(),
xi: xi.as_slice(),
weights: w.as_slice(),
})
.collect()
})
.collect();
let cell_values: Vec<CellValues<'_, T>> = self
.cell_fields
.iter()
.zip(&cf_arrays)
.zip(&rules)
.map(|((f, (t, v)), rules)| CellValues {
components: &f.0,
cell_tags: t.as_slice(),
values: v.as_slice(),
layout: if f.3.is_some() {
CellLayout::Gauss(rules)
} else {
CellLayout::Cell
},
})
.collect();
Ok(crate::ops::mesh::from_arrays(
coords,
tags.as_slice(),
xyz.as_slice(),
&blocks,
&node_values,
&cell_values,
order,
)?)
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn read_gmsh<'py>(
py: Python<'py>,
coords: PyRef<PyCoords>,
path: &str,
) -> PyResult<Bound<'py, PyDict>> {
let groups = crate::ops::mesh::read_gmsh(coords.handle.clone(), std::path::Path::new(path))?;
groups_to_dict(py, groups)
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
pub fn read_gmsh_str<'py>(
py: Python<'py>,
coords: PyRef<PyCoords>,
text: &str,
) -> PyResult<Bound<'py, PyDict>> {
let groups = crate::ops::mesh::read_gmsh_str(coords.handle.clone(), text)?;
groups_to_dict(py, groups)
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyfunction)]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
#[allow(clippy::too_many_arguments)]
pub fn select(
field: &Bound<'_, PyAny>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
if let Ok(f) = field.extract::<PyRef<PyNodeField>>() {
select_nodes(f, ge, gt, le, lt, components)
} else if let Ok(f) = field.extract::<PyRef<PyElementField>>() {
select_cells(f, ge, gt, le, lt, components)
} else if let Ok(f) = field.extract::<PyRef<PySubNodeField>>() {
select_sub_nodes(f, ge, gt, le, lt, components)
} else if let Ok(f) = field.extract::<PyRef<PySubElementField>>() {
select_sub_cells(f, ge, gt, le, lt, components)
} else {
Err(PyTypeError::new_err(
"expected a NodeField, SubNodeField, ElementField or SubElementField",
))
}
}
#[py_op(method_on = PyNodeField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_nodes(
field: PyRef<PyNodeField>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
let band = crate::atoms::Band::new(ge, gt, le, lt)?;
Ok(PyMesh {
inner: crate::ops::mesh::select_nodes(&field.inner, &band, components)?,
})
}
#[py_op(method_on = PyElementField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_cells(
field: PyRef<PyElementField>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
let band = crate::atoms::Band::new(ge, gt, le, lt)?;
Ok(PyMesh {
inner: crate::ops::mesh::select_cells(&field.inner, &band, components)?,
})
}
#[py_op(method_on = PySubNodeField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_sub_nodes(
field: PyRef<PySubNodeField>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
let band = crate::atoms::Band::new(ge, gt, le, lt)?;
Ok(PyMesh {
inner: crate::ops::mesh::select_sub_nodes(&field.handle.read(), &band, components)?,
})
}
#[py_op(method_on = PySubElementField, name = "select")]
#[pyfunction]
#[pyo3(signature = (field, ge=None, gt=None, le=None, lt=None, components=None))]
pub fn select_sub_cells(
field: PyRef<PySubElementField>,
ge: Option<f64>,
gt: Option<f64>,
le: Option<f64>,
lt: Option<f64>,
components: Option<Vec<String>>,
) -> PyResult<PyMesh> {
let band = crate::atoms::Band::new(ge, gt, le, lt)?;
Ok(PyMesh {
inner: crate::ops::mesh::select_sub_cells(&field.handle.read(), &band, components)?,
})
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyMesh {
#[pyo3(signature = (tol, in_place=false))]
fn merge_nodes(
slf: Py<Self>,
py: Python<'_>,
tol: f64,
in_place: bool,
) -> PyResult<Py<PyMesh>> {
super::mesh::merge_nodes(py, slf, tol, in_place)
}
}