use crate::containers::mesh::Node;
use crate::containers::mesh::{Coords, NodeId};
use crate::py::mesh::PyMesh;
use crate::store::Handle;
use pyo3::prelude::*;
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "Node")]
pub struct PyNode {
node: Node,
}
impl PyNode {
pub(crate) fn from_raw(handle: Handle<Coords>, id: NodeId) -> Self {
Self {
node: Node::from_parts(handle, id),
}
}
pub(crate) fn from_node(node: Node) -> Self {
Self { node }
}
pub(crate) fn as_node(&self) -> &Node {
&self.node
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyNode {
#[getter]
fn id(&self) -> u32 {
self.node.id().0
}
fn coord(&self) -> PyResult<Vec<f64>> {
Ok(self.node.coord()?)
}
fn set_coord(&self, coords: Vec<f64>) -> PyResult<()> {
self.node.set_coord(&coords)?;
Ok(())
}
fn __or__(&self, py: Python<'_>, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok(o) = other.extract::<PyRef<'_, PyNode>>() {
let mesh = self.as_node().union(o.as_node())?;
return Ok(Py::new(py, PyMesh { inner: mesh })?.into_any());
}
Ok(py.NotImplemented())
}
fn __ror__(&self, py: Python<'_>, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok(m) = other.extract::<PyRef<'_, PyMesh>>() {
let mesh = m.inner.union_node(self.as_node())?;
return Ok(Py::new(py, PyMesh { inner: mesh })?.into_any());
}
Ok(py.NotImplemented())
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", self.node))
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("{}", self.node))
}
}
crate::impl_dump_pymethod!(value PyNode, node);