Skip to main content

geop_core_geometry/
exact.rs

1//! Curves and surfaces written exactly ([`Exact`]): their degrees, control
2//! points and knots, each scalar's bounds to the bit. Read back through
3//! their constructors, which check them and work out what is derived from
4//! them (the box around the control points) as they did when first made.
5
6use geop_core_math::{
7    exact::{Exact, Written},
8    scalars::Scalar,
9    vector::Vector,
10};
11use serde::{Deserialize, Deserializer, Serializer, de::Error, ser::SerializeStruct};
12
13use crate::{nurb_curve::NurbCurve, nurb_surface::NurbSurface};
14
15impl<const D: usize> Exact for NurbCurve<D> {
16    fn write<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
17        let mut curve = serializer.serialize_struct("NurbCurve", 3)?;
18        curve.serialize_field("degree", &self.degree)?;
19        curve.serialize_field("control_points", &Written(&self.control_points))?;
20        curve.serialize_field("knots", &Written(&self.knot_vector))?;
21        curve.end()
22    }
23
24    fn read<'de, De: Deserializer<'de>>(deserializer: De) -> Result<Self, De::Error> {
25        #[derive(Deserialize)]
26        struct Curve<const D: usize> {
27            degree: usize,
28            #[serde(with = "geop_core_math::exact")]
29            control_points: Vec<Vector<D>>,
30            #[serde(with = "geop_core_math::exact")]
31            knots: Vec<Scalar>,
32        }
33        let Curve {
34            degree,
35            control_points,
36            knots,
37        } = Curve::<D>::deserialize(deserializer)?;
38        NurbCurve::try_new(degree, control_points, knots).map_err(De::Error::custom)
39    }
40}
41
42impl<const D: usize> Exact for NurbSurface<D> {
43    fn write<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
44        let mut surface = serializer.serialize_struct("NurbSurface", 5)?;
45        surface.serialize_field("degree_u", &self.degree_u)?;
46        surface.serialize_field("degree_v", &self.degree_v)?;
47        surface.serialize_field("control_points", &Written(&self.control_points))?;
48        surface.serialize_field("knots_u", &Written(&self.knot_vector_u))?;
49        surface.serialize_field("knots_v", &Written(&self.knot_vector_v))?;
50        surface.end()
51    }
52
53    fn read<'de, De: Deserializer<'de>>(deserializer: De) -> Result<Self, De::Error> {
54        #[derive(Deserialize)]
55        struct Surface<const D: usize> {
56            degree_u: usize,
57            degree_v: usize,
58            #[serde(with = "geop_core_math::exact")]
59            control_points: Vec<Vector<D>>,
60            #[serde(with = "geop_core_math::exact")]
61            knots_u: Vec<Scalar>,
62            #[serde(with = "geop_core_math::exact")]
63            knots_v: Vec<Scalar>,
64        }
65        let Surface {
66            degree_u,
67            degree_v,
68            control_points,
69            knots_u,
70            knots_v,
71        } = Surface::<D>::deserialize(deserializer)?;
72        NurbSurface::try_new(degree_u, degree_v, control_points, knots_u, knots_v)
73            .map_err(De::Error::custom)
74    }
75}