|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use numpy::{Ix1, PyArray}; |
| 4 | +use pyo3::prelude::*; |
| 5 | + |
| 6 | +use crate::material::Material; |
| 7 | +use crate::shapes::Geometry; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +#[pyclass(frozen)] |
| 11 | +/// A `DrawableShape` is the input geometry for a pyraydeon scene. |
| 12 | +/// |
| 13 | +/// It is essentially some drawable geometry joined with a given material. |
| 14 | +pub(crate) struct DrawableShape { |
| 15 | + pub raydeon_drawable: raydeon::DrawableShape, |
| 16 | + pub pyobj: PyObject, |
| 17 | +} |
| 18 | + |
| 19 | +impl ::std::ops::Deref for DrawableShape { |
| 20 | + type Target = raydeon::DrawableShape; |
| 21 | + |
| 22 | + fn deref(&self) -> &Self::Target { |
| 23 | + &self.raydeon_drawable |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl DrawableShape { |
| 28 | + pub(crate) fn raydeon_drawable(&self) -> raydeon::DrawableShape { |
| 29 | + self.raydeon_drawable.clone() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<'py> FromPyObject<'py> for DrawableShape { |
| 34 | + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self> { |
| 35 | + let py = obj.py(); |
| 36 | + let shape: PyObject = obj.getattr("shape")?.extract()?; |
| 37 | + let material: Option<Material> = obj.getattr("material")?.extract()?; |
| 38 | + |
| 39 | + let raydeon_geometry = raydeon_geometry_from_py_object(py, &shape)?; |
| 40 | + let raydeon_drawable = raydeon::DrawableShape::new() |
| 41 | + .geometry(raydeon_geometry) |
| 42 | + .maybe_material(material.map(|m| m.0)) |
| 43 | + .build(); |
| 44 | + |
| 45 | + Ok(DrawableShape { |
| 46 | + raydeon_drawable, |
| 47 | + pyobj: shape, |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[pymethods] |
| 53 | +impl DrawableShape { |
| 54 | + #[new] |
| 55 | + #[pyo3(signature = (geometry, material=None))] |
| 56 | + fn new(py: Python, geometry: PyObject, material: Option<Material>) -> PyResult<Self> { |
| 57 | + let raydeon_geometry = raydeon_geometry_from_py_object(py, &geometry)?; |
| 58 | + let material = material.map(|m| m.0); |
| 59 | + let raydeon_drawable = raydeon::DrawableShape::new() |
| 60 | + .geometry(raydeon_geometry) |
| 61 | + .maybe_material(material) |
| 62 | + .build(); |
| 63 | + |
| 64 | + Ok(DrawableShape { |
| 65 | + raydeon_drawable, |
| 66 | + pyobj: geometry, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + #[getter] |
| 71 | + fn material(&self) -> Option<Material> { |
| 72 | + self.raydeon_drawable.material().map(Into::into) |
| 73 | + } |
| 74 | + |
| 75 | + #[getter] |
| 76 | + fn shape(&self, py: Python) -> PyObject { |
| 77 | + self.pyobj.clone_ref(py) |
| 78 | + } |
| 79 | + |
| 80 | + fn collision_geometry(&self, py: Python) -> PyResult<PyObject> { |
| 81 | + self.pyobj.call_method0(py, "collision_geometry") |
| 82 | + } |
| 83 | + |
| 84 | + fn paths(&self, py: Python) -> PyResult<PyObject> { |
| 85 | + self.pyobj.call_method0(py, "paths") |
| 86 | + } |
| 87 | + |
| 88 | + fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> { |
| 89 | + let class_name = slf.get_type().qualname()?; |
| 90 | + Ok(format!("{}<{:#?}>", class_name, slf.borrow())) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +pub(crate) fn raydeon_geometry_from_py_object( |
| 95 | + py: Python, |
| 96 | + g: &PyObject, |
| 97 | +) -> PyResult<Arc<dyn raydeon::Shape>> { |
| 98 | + let geom: Py<Geometry> = g.extract(py)?; |
| 99 | + let raydeon_shape = geom.borrow(py); |
| 100 | + let raydeon_shape = raydeon_shape.geometry(g.clone_ref(py)); |
| 101 | + Ok(raydeon_shape) |
| 102 | +} |
| 103 | + |
| 104 | +#[derive(Debug)] |
| 105 | +#[pyclass(frozen)] |
| 106 | +/// The output of a raydeon render. |
| 107 | +/// |
| 108 | +/// A line segment, associated with a shape. |
| 109 | +pub(crate) struct DrawableSegment { |
| 110 | + p1: [f64; 2], |
| 111 | + p2: [f64; 2], |
| 112 | + kind: SegmentKind, |
| 113 | + |
| 114 | + raydeon_drawable: Option<raydeon::DrawableShape>, |
| 115 | +} |
| 116 | + |
| 117 | +impl From<raydeon::DrawableSegment<'_>> for DrawableSegment { |
| 118 | + fn from(value: raydeon::DrawableSegment<'_>) -> Self { |
| 119 | + let p1 = value.p1.to_array(); |
| 120 | + let p2 = value.p2.to_array(); |
| 121 | + |
| 122 | + let raydeon_drawable = value.segment.get_shape().cloned(); |
| 123 | + |
| 124 | + let kind = value.kind.into(); |
| 125 | + |
| 126 | + Self { |
| 127 | + p1, |
| 128 | + p2, |
| 129 | + raydeon_drawable, |
| 130 | + kind, |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +#[pymethods] |
| 136 | +impl DrawableSegment { |
| 137 | + #[getter] |
| 138 | + fn p1<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray<f64, Ix1>> { |
| 139 | + PyArray::from_slice_bound(py, &self.p1) |
| 140 | + } |
| 141 | + |
| 142 | + #[getter] |
| 143 | + fn p2<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray<f64, Ix1>> { |
| 144 | + PyArray::from_slice_bound(py, &self.p2) |
| 145 | + } |
| 146 | + |
| 147 | + #[getter] |
| 148 | + fn material(&self) -> Option<Material> { |
| 149 | + self.raydeon_drawable |
| 150 | + .as_ref() |
| 151 | + .and_then(|d| d.material()) |
| 152 | + .map(|m| m.into()) |
| 153 | + } |
| 154 | + |
| 155 | + #[getter] |
| 156 | + fn kind(&self) -> SegmentKind { |
| 157 | + self.kind |
| 158 | + } |
| 159 | + |
| 160 | + fn __repr__(slf: &Bound<'_, Self>) -> PyResult<String> { |
| 161 | + let class_name = slf.get_type().qualname()?; |
| 162 | + Ok(format!("{}<{:#?}>", class_name, slf.borrow())) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +#[pyclass(eq)] |
| 167 | +#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 168 | +pub(crate) enum SegmentKind { |
| 169 | + VerticalHatch, |
| 170 | + DiagonalHatch, |
| 171 | + Path, |
| 172 | +} |
| 173 | + |
| 174 | +impl From<raydeon::SegmentKind> for SegmentKind { |
| 175 | + fn from(value: raydeon::SegmentKind) -> Self { |
| 176 | + match value { |
| 177 | + raydeon::SegmentKind::ScreenSpaceHatch(raydeon::ScreenSpaceHatchKind::Vertical) => { |
| 178 | + SegmentKind::VerticalHatch |
| 179 | + } |
| 180 | + raydeon::SegmentKind::ScreenSpaceHatch(raydeon::ScreenSpaceHatchKind::Diagonal60) => { |
| 181 | + SegmentKind::VerticalHatch |
| 182 | + } |
| 183 | + raydeon::SegmentKind::Path => SegmentKind::Path, |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +pub(crate) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { |
| 189 | + m.add_class::<DrawableShape>()?; |
| 190 | + m.add_class::<DrawableSegment>()?; |
| 191 | + Ok(()) |
| 192 | +} |
0 commit comments