Skip to main content

three_rs/extras/
path.rs

1//! Port of `three.js/src/extras/core/Path.js`.
2
3use std::ops::{Deref, DerefMut};
4use std::rc::Rc;
5
6use super::bezier_curves::{CubicBezierCurve, QuadraticBezierCurve};
7use super::curve::Curve;
8use super::curve_path::{delegate_curve_path, CurvePath};
9use super::ellipse_curve::EllipseCurve;
10use super::line_curve::LineCurve;
11use super::spline_curve::SplineCurve;
12use crate::math::Vector2;
13
14/// `Path`: a 2D [`CurvePath`] with a canvas-like drawing API (`moveTo`,
15/// `lineTo`, `bezierCurveTo`, ...).
16///
17/// three.js' `Path extends CurvePath`; here a `Path` holds its `CurvePath`
18/// and derefs to it, so `path.curves`, `path.auto_close` and
19/// `path.close_path()` read as they do in JavaScript. The builder methods
20/// return `&mut Self` for chaining, as three.js' return `this`.
21#[derive(Clone, Debug, Default)]
22pub struct Path {
23    /// The inherited `CurvePath` state.
24    pub curve_path: CurvePath<Vector2>,
25    /// The current offset of the path. Any new curve added will start here.
26    pub current_point: Vector2,
27}
28
29impl Deref for Path {
30    type Target = CurvePath<Vector2>;
31    fn deref(&self) -> &Self::Target {
32        &self.curve_path
33    }
34}
35
36impl DerefMut for Path {
37    fn deref_mut(&mut self) -> &mut Self::Target {
38        &mut self.curve_path
39    }
40}
41
42delegate_curve_path!(Path, curve_path, Vector2, "Path");
43
44impl Path {
45    /// `new Path()`.
46    pub fn new() -> Self {
47        Self::default()
48    }
49
50    /// `new Path( points )`: `setFromPoints( points )` on a new path.
51    pub fn from_points(points: &[Vector2]) -> Self {
52        let mut path = Self::new();
53        path.set_from_points(points);
54        path
55    }
56
57    /// `Path.setFromPoints( points )`: a `moveTo` to the first point and a
58    /// `lineTo` to each of the rest.
59    pub fn set_from_points(&mut self, points: &[Vector2]) -> &mut Self {
60        self.move_to(points[0].x, points[0].y);
61
62        for point in &points[1..] {
63            self.line_to(point.x, point.y);
64        }
65
66        self
67    }
68
69    /// `Path.moveTo( x, y )`.
70    pub fn move_to(&mut self, x: f64, y: f64) -> &mut Self {
71        self.current_point.set(x, y); // TODO consider referencing vectors instead of copying?
72
73        self
74    }
75
76    /// `Path.lineTo( x, y )`.
77    pub fn line_to(&mut self, x: f64, y: f64) -> &mut Self {
78        let curve = LineCurve::new(self.current_point, Vector2::new(x, y));
79        self.curve_path.curves.push(Rc::new(curve));
80
81        self.current_point.set(x, y);
82
83        self
84    }
85
86    /// `Path.quadraticCurveTo( aCPx, aCPy, aX, aY )`.
87    pub fn quadratic_curve_to(
88        &mut self,
89        a_c_px: f64,
90        a_c_py: f64,
91        a_x: f64,
92        a_y: f64,
93    ) -> &mut Self {
94        let curve = QuadraticBezierCurve::new(
95            self.current_point,
96            Vector2::new(a_c_px, a_c_py),
97            Vector2::new(a_x, a_y),
98        );
99
100        self.curve_path.curves.push(Rc::new(curve));
101
102        self.current_point.set(a_x, a_y);
103
104        self
105    }
106
107    /// `Path.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY )`.
108    pub fn bezier_curve_to(
109        &mut self,
110        a_cp1x: f64,
111        a_cp1y: f64,
112        a_cp2x: f64,
113        a_cp2y: f64,
114        a_x: f64,
115        a_y: f64,
116    ) -> &mut Self {
117        let curve = CubicBezierCurve::new(
118            self.current_point,
119            Vector2::new(a_cp1x, a_cp1y),
120            Vector2::new(a_cp2x, a_cp2y),
121            Vector2::new(a_x, a_y),
122        );
123
124        self.curve_path.curves.push(Rc::new(curve));
125
126        self.current_point.set(a_x, a_y);
127
128        self
129    }
130
131    /// `Path.splineThru( pts )`: a [`SplineCurve`] from the current point
132    /// through `pts`.
133    pub fn spline_thru(&mut self, pts: &[Vector2]) -> &mut Self {
134        let mut npts = vec![self.current_point];
135        npts.extend_from_slice(pts);
136
137        let curve = SplineCurve::new(npts);
138        self.curve_path.curves.push(Rc::new(curve));
139
140        self.current_point.copy(&pts[pts.len() - 1]);
141
142        self
143    }
144
145    /// `Path.arc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise )`:
146    /// [`absarc`](Self::absarc) with the center relative to the current point.
147    pub fn arc(
148        &mut self,
149        a_x: f64,
150        a_y: f64,
151        a_radius: f64,
152        a_start_angle: f64,
153        a_end_angle: f64,
154        a_clockwise: bool,
155    ) -> &mut Self {
156        let x0 = self.current_point.x;
157        let y0 = self.current_point.y;
158
159        self.absarc(
160            a_x + x0,
161            a_y + y0,
162            a_radius,
163            a_start_angle,
164            a_end_angle,
165            a_clockwise,
166        );
167
168        self
169    }
170
171    /// `Path.absarc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise )`.
172    pub fn absarc(
173        &mut self,
174        a_x: f64,
175        a_y: f64,
176        a_radius: f64,
177        a_start_angle: f64,
178        a_end_angle: f64,
179        a_clockwise: bool,
180    ) -> &mut Self {
181        self.absellipse(
182            a_x,
183            a_y,
184            a_radius,
185            a_radius,
186            a_start_angle,
187            a_end_angle,
188            a_clockwise,
189            0.0,
190        );
191
192        self
193    }
194
195    /// `Path.ellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle,
196    /// aClockwise, aRotation )`: [`absellipse`](Self::absellipse) with the
197    /// center relative to the current point.
198    #[allow(clippy::too_many_arguments)]
199    pub fn ellipse(
200        &mut self,
201        a_x: f64,
202        a_y: f64,
203        x_radius: f64,
204        y_radius: f64,
205        a_start_angle: f64,
206        a_end_angle: f64,
207        a_clockwise: bool,
208        a_rotation: f64,
209    ) -> &mut Self {
210        let x0 = self.current_point.x;
211        let y0 = self.current_point.y;
212
213        self.absellipse(
214            a_x + x0,
215            a_y + y0,
216            x_radius,
217            y_radius,
218            a_start_angle,
219            a_end_angle,
220            a_clockwise,
221            a_rotation,
222        );
223
224        self
225    }
226
227    /// `Path.absellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle,
228    /// aClockwise, aRotation )`. Joins the previous curve to the ellipse's
229    /// start with a line when they do not meet.
230    #[allow(clippy::too_many_arguments)]
231    pub fn absellipse(
232        &mut self,
233        a_x: f64,
234        a_y: f64,
235        x_radius: f64,
236        y_radius: f64,
237        a_start_angle: f64,
238        a_end_angle: f64,
239        a_clockwise: bool,
240        a_rotation: f64,
241    ) -> &mut Self {
242        let curve = EllipseCurve::new(
243            a_x,
244            a_y,
245            x_radius,
246            y_radius,
247            a_start_angle,
248            a_end_angle,
249            a_clockwise,
250            a_rotation,
251        );
252
253        if !self.curve_path.curves.is_empty() {
254            // if a previous curve is present, attempt to join
255            let first_point = curve.get_point(0.0);
256
257            if !first_point.equals(&self.current_point) {
258                self.line_to(first_point.x, first_point.y);
259            }
260        }
261
262        let last_point = curve.get_point(1.0);
263        self.curve_path.curves.push(Rc::new(curve));
264
265        self.current_point.copy(&last_point);
266
267        self
268    }
269}