path_offset/path/point.rs
1//! Defines the canonical `Point` type and a generic conversion trait `PointConvert`.
2//!
3//! This module provides a standardized `Point` struct that serves as an intermediary
4//! for converting between different point representations from various geometry libraries.
5//! The `PointConvert` trait enables seamless, generic conversion of any point type
6//! that can be converted to and from the canonical `Point`.
7
8/// A canonical 2D point representation with `f64` precision.
9///
10/// This struct acts as a common ground for converting between point types
11/// from different libraries (e.g., `lyon::math::Point`, `flo_curves::bezier::Coord2`).
12#[derive(Debug, Clone, Copy)]
13pub struct Point(pub f64, pub f64);
14
15/// A trait for generically converting between different point types.
16///
17/// Any type that implements `Copy` and has `From` implementations to and from
18/// the canonical [`Point`] struct will automatically implement this trait.
19/// It provides a `use_as` method to convert an instance of a point type into another
20/// point type, using [`Point`] as the intermediary.
21pub trait PointConvert {
22 /// Converts the point into a different point type `T`.
23 ///
24 /// This method leverages the canonical [`Point`] struct as a bridge. The conversion
25 /// follows the path: `Self` -> `Point` -> `T`.
26 ///
27 /// # Type Parameters
28 ///
29 /// * `T`: The target point type. It must be convertible from [`Point`].
30 ///
31 /// # Constraints
32 ///
33 /// * `T` must implement `From<Point>`.
34 /// * The canonical [`Point`] must implement `From<Self>`.
35 /// * `Self` must be `Copy`.
36 fn use_as<T>(&self) -> T
37 where
38 T: From<Point>,
39 Point: From<Self>,
40 Self: Copy;
41}
42
43impl<P> PointConvert for P
44where
45 P: Copy,
46 Point: From<P>,
47{
48 fn use_as<T>(&self) -> T
49 where
50 T: From<Point>,
51 {
52 // The core logic: convert `self` to the canonical `Point`, then to the target type `T`.
53 let canonical_point = Point::from(*self);
54 T::from(canonical_point)
55 }
56}
57
58/// Converts a `lyon::math::Point` to the canonical `Point`.
59impl From<lyon::math::Point> for Point {
60 fn from(value: lyon::math::Point) -> Self {
61 Self(value.x as f64, value.y as f64)
62 }
63}
64
65/// Converts the canonical `Point` to a `lyon::math::Point`.
66impl From<Point> for lyon::math::Point {
67 fn from(point: Point) -> Self {
68 lyon::geom::euclid::point2(point.0 as f32, point.1 as f32)
69 }
70}
71
72/// Converts a `flo_curves::bezier::Coord2` to the canonical `Point`.
73impl From<flo_curves::bezier::Coord2> for Point {
74 fn from(value: flo_curves::bezier::Coord2) -> Self {
75 Self(value.0, value.1)
76 }
77}
78
79/// Converts the canonical `Point` to a `flo_curves::bezier::Coord2`.
80impl From<Point> for flo_curves::bezier::Coord2 {
81 fn from(point: Point) -> Self {
82 flo_curves::bezier::Coord2(point.0, point.1)
83 }
84}