use std::cell::{Cell, RefCell};
use std::fmt;
use std::rc::Rc;
use super::curve::{compute_lengths, Curve, CurveVector};
use super::line_curve::{LineCurve, LineVector};
pub type CurveRef<P> = Rc<dyn Curve<Point = P>>;
pub struct CurvePath<P: CurveVector> {
pub curves: Vec<CurveRef<P>>,
pub auto_close: bool,
pub arc_length_divisions: usize,
pub needs_update: Cell<bool>,
cache_lengths: RefCell<Option<Vec<f64>>>,
cache_arc_lengths: RefCell<Option<Vec<f64>>>,
}
impl<P: CurveVector> Default for CurvePath<P> {
fn default() -> Self {
Self {
curves: Vec::new(),
auto_close: false,
arc_length_divisions: 200,
needs_update: Cell::new(false),
cache_lengths: RefCell::new(None),
cache_arc_lengths: RefCell::new(None),
}
}
}
impl<P: CurveVector> Clone for CurvePath<P> {
fn clone(&self) -> Self {
Self {
curves: self.curves.clone(),
auto_close: self.auto_close,
arc_length_divisions: self.arc_length_divisions,
..Self::default()
}
}
}
impl<P: CurveVector> fmt::Debug for CurvePath<P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CurvePath")
.field(
"curves",
&self
.curves
.iter()
.map(|c| c.type_name())
.collect::<Vec<_>>(),
)
.field("auto_close", &self.auto_close)
.finish()
}
}
impl<P: CurveVector> CurvePath<P> {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, curve: impl Curve<Point = P> + 'static) {
self.curves.push(Rc::new(curve));
}
pub fn update_arc_lengths(&self) {
self.needs_update.set(true);
*self.cache_lengths.borrow_mut() = None;
self.get_curve_lengths();
}
pub fn get_curve_lengths(&self) -> Vec<f64> {
if let Some(cache) = self.cache_lengths.borrow().as_ref() {
if cache.len() == self.curves.len() {
return cache.clone();
}
}
let mut lengths = Vec::new();
let mut sums = 0.0;
for curve in &self.curves {
sums += curve.get_length();
lengths.push(sums);
}
*self.cache_lengths.borrow_mut() = Some(lengths.clone());
lengths
}
}
impl<P: LineVector> CurvePath<P> {
pub fn close_path(&mut self) -> &mut Self {
let start_point = self.curves[0].get_point(0.0);
let end_point = self.curves[self.curves.len() - 1].get_point(1.0);
if !start_point.equals(&end_point) {
self.curves
.push(Rc::new(LineCurve::new(end_point, start_point)));
}
self
}
}
impl<P: CurveVector> Curve for CurvePath<P> {
type Point = P;
fn type_name(&self) -> &'static str {
"CurvePath"
}
fn arc_length_divisions(&self) -> usize {
self.arc_length_divisions
}
fn get_point(&self, t: f64) -> P {
let d = t * self.get_length();
let curve_lengths = self.get_curve_lengths();
for (i, &curve_length) in curve_lengths.iter().enumerate() {
if curve_length >= d {
let diff = curve_length - d;
let curve = &self.curves[i];
let segment_length = curve.get_length();
let u = if segment_length == 0.0 {
0.0
} else {
1.0 - diff / segment_length
};
return curve.get_point_at(u);
}
}
panic!("three-rs: CurvePath::get_point({t}) lies past the end of the path (three.js returns null)");
}
fn get_length(&self) -> f64 {
let lens = self.get_curve_lengths();
lens.last().copied().unwrap_or(f64::NAN)
}
fn get_lengths(&self, divisions: usize) -> Vec<f64> {
if let Some(cache) = self.cache_arc_lengths.borrow().as_ref() {
if cache.len() == divisions + 1 && !self.needs_update.get() {
return cache.clone();
}
}
self.needs_update.set(false);
let cache = compute_lengths(self, divisions);
*self.cache_arc_lengths.borrow_mut() = Some(cache.clone());
cache
}
fn get_spaced_points(&self, divisions: usize) -> Vec<P> {
let mut points = Vec::new();
for i in 0..=divisions {
points.push(self.get_point(i as f64 / divisions as f64));
}
if self.auto_close {
points.push(points[0]);
}
points
}
fn get_points(&self, divisions: usize) -> Vec<P> {
let mut points: Vec<P> = Vec::new();
let mut last: Option<P> = None;
for curve in &self.curves {
let resolution = curve.curve_path_resolution(divisions);
let pts = curve.get_points(resolution);
for point in pts {
if last.is_some_and(|last| last.equals(&point)) {
continue; }
points.push(point);
last = Some(point);
}
}
if self.auto_close && points.len() > 1 && !points[points.len() - 1].equals(&points[0]) {
points.push(points[0]);
}
points
}
}
macro_rules! delegate_curve_path {
($ty:ty, $field:ident, $point:ty, $name:literal) => {
impl $crate::extras::Curve for $ty {
type Point = $point;
fn type_name(&self) -> &'static str {
$name
}
fn arc_length_divisions(&self) -> usize {
$crate::extras::Curve::arc_length_divisions(&self.$field)
}
fn get_point(&self, t: f64) -> $point {
$crate::extras::Curve::get_point(&self.$field, t)
}
fn get_length(&self) -> f64 {
$crate::extras::Curve::get_length(&self.$field)
}
fn get_lengths(&self, divisions: usize) -> Vec<f64> {
$crate::extras::Curve::get_lengths(&self.$field, divisions)
}
fn get_spaced_points(&self, divisions: usize) -> Vec<$point> {
$crate::extras::Curve::get_spaced_points(&self.$field, divisions)
}
fn get_points(&self, divisions: usize) -> Vec<$point> {
$crate::extras::Curve::get_points(&self.$field, divisions)
}
}
};
}
pub(crate) use delegate_curve_path;