1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use truck_base::cgmath64::*;

mod curve;
pub use curve::*;
mod surface;
pub use surface::*;

/// Search parameter `t` such that `self.subs(t)` is near point.
pub trait SearchParameter {
    /// point
    type Point;
    /// curve => `f64`, surface => `(f64, f64)`
    type Parameter;
    /// Search parameter `t` such that `self.subs(t)` is near point.  
    /// Returns `None` if could not find such parameter.
    fn search_parameter(
        &self,
        point: Self::Point,
        hint: Option<Self::Parameter>,
        trial: usize,
    ) -> Option<Self::Parameter>;
}

impl<'a, T: SearchParameter> SearchParameter for &'a T {
    type Point = T::Point;
    type Parameter = T::Parameter;
    fn search_parameter(
        &self,
        point: Self::Point,
        hint: Option<Self::Parameter>,
        trial: usize,
    ) -> Option<Self::Parameter> {
        T::search_parameter(*self, point, hint, trial)
    }
}

/// Search parameter `t` such that `self.subs(t)` is nearest point.
pub trait SearchNearestParameter {
    /// point
    type Point;
    /// curve => `f64`, surface => `(f64, f64)`
    type Parameter;
    /// Search nearest parameter `t` such that `self.subs(t)` is nearest point.  
    /// Returns `None` if could not find such parameter.
    fn search_nearest_parameter(
        &self,
        point: Self::Point,
        hint: Option<Self::Parameter>,
        trial: usize,
    ) -> Option<Self::Parameter>;
}

impl<'a, T: SearchNearestParameter> SearchNearestParameter for &'a T {
    type Point = T::Point;
    type Parameter = T::Parameter;
    fn search_nearest_parameter(
        &self,
        point: Self::Point,
        hint: Option<Self::Parameter>,
        trial: usize,
    ) -> Option<Self::Parameter> {
        T::search_nearest_parameter(*self, point, hint, trial)
    }
}

/// Oriented and reversible
pub trait Invertible: Clone {
    /// Inverts `self`
    fn invert(&mut self);
    /// Returns the inverse.
    #[inline(always)]
    fn inverse(&self) -> Self {
        let mut res = self.clone();
        res.invert();
        res
    }
}

/// Transform geometry
pub trait Transformed<T>: Clone {
    /// transform by `trans`.
    fn transform_by(&mut self, trans: T);
    /// transformed geometry by `trans`.
    #[inline(always)]
    fn transformed(&self, trans: T) -> Self {
        let mut res = self.clone();
        res.transform_by(trans);
        res
    }
}

/// Implementation for the test of topological methods.
impl Invertible for (usize, usize) {
    fn invert(&mut self) { *self = (self.1, self.0); }
    fn inverse(&self) -> Self { (self.1, self.0) }
}

impl<P: Clone> Invertible for Vec<P> {
    #[inline(always)]
    fn invert(&mut self) { self.reverse(); }
    #[inline(always)]
    fn inverse(&self) -> Self { self.iter().rev().cloned().collect() }
}