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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use alga::linear::{Rotation, Translation};
use na;
use math::{Isometry, Point};

/// Geometric description of a polyline.
#[derive(Clone)]
pub struct Polyline<P: Point> {
    /// Coordinates of the polyline vertices.
    coords: Vec<P>,
    /// Coordinates of the polyline normals.
    normals: Option<Vec<P::Vector>>,
}

impl<P: Point> Polyline<P> {
    /// Creates a new polyline.
    pub fn new(coords: Vec<P>, normals: Option<Vec<P::Vector>>) -> Polyline<P> {
        if let Some(ref ns) = normals {
            assert!(
                coords.len() == ns.len(),
                "There must be exactly one normal per vertex."
            );
        }

        Polyline {
            coords: coords,
            normals: normals,
        }
    }
}

impl<P: Point> Polyline<P> {
    /// Moves the polyline data out of it.
    pub fn unwrap(self) -> (Vec<P>, Option<Vec<P::Vector>>) {
        (self.coords, self.normals)
    }

    /// The coordinates of this polyline vertices.
    #[inline]
    pub fn coords(&self) -> &[P] {
        &self.coords[..]
    }

    /// The mutable coordinates of this polyline vertices.
    #[inline]
    pub fn coords_mut(&mut self) -> &mut [P] {
        &mut self.coords[..]
    }

    /// The normals of this polyline vertices.
    #[inline]
    pub fn normals(&self) -> Option<&[P::Vector]> {
        match self.normals {
            Some(ref ns) => Some(&ns[..]),
            None => None,
        }
    }

    /// The mutable normals of this polyline vertices.
    #[inline]
    pub fn normals_mut(&mut self) -> Option<&mut [P::Vector]> {
        match self.normals {
            Some(ref mut ns) => Some(&mut ns[..]),
            None => None,
        }
    }

    /// Translates each vertex of this polyline.
    pub fn translate_by<T: Translation<P>>(&mut self, t: &T) {
        for c in self.coords.iter_mut() {
            *c = t.transform_point(c);
        }
    }

    /// Rotates each vertex and normal of this polyline.
    pub fn rotate_by<R: Rotation<P>>(&mut self, r: &R) {
        for c in self.coords.iter_mut() {
            *c = r.transform_point(c);
        }

        for n in self.normals.iter_mut() {
            for n in n.iter_mut() {
                *n = r.transform_vector(n);
            }
        }
    }

    /// Transforms each vertex and rotates each normal of this polyline.
    pub fn transform_by<T: Isometry<P>>(&mut self, t: &T) {
        for c in self.coords.iter_mut() {
            *c = t.transform_point(c);
        }

        for n in self.normals.iter_mut() {
            for n in n.iter_mut() {
                *n = t.rotate_vector(n);
            }
        }
    }

    /// Scales each vertex of this polyline.
    pub fn scale_by_scalar(&mut self, s: &P::Real) {
        for c in self.coords.iter_mut() {
            *c = *c * *s
        }
        // FIXME: do something for the normals?
    }
}

impl<P> Polyline<P>
where
    P: Point,
{
    /// Scales each vertex of this mesh.
    #[inline]
    pub fn scale_by(&mut self, s: &P::Vector) {
        for c in self.coords.iter_mut() {
            for i in 0..na::dimension::<P::Vector>() {
                c[i] = (*c)[i] * s[i];
            }
        }
        // FIXME: do something for the normals?
    }
}