use alga::linear::{Rotation, Translation};
use na;
use math::{Point, Isometry};
#[derive(Clone)]
pub struct Polyline<P: Point> {
coords: Vec<P>,
normals: Option<Vec<P::Vector>>,
}
impl<P: Point> Polyline<P> {
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> {
pub fn unwrap(self) -> (Vec<P>, Option<Vec<P::Vector>>) {
(self.coords, self.normals)
}
#[inline]
pub fn coords(&self) -> &[P] {
&self.coords[..]
}
#[inline]
pub fn coords_mut(&mut self) -> &mut [P] {
&mut self.coords[..]
}
#[inline]
pub fn normals(&self) -> Option<&[P::Vector]> {
match self.normals {
Some(ref ns) => Some(&ns[..]),
None => None
}
}
#[inline]
pub fn normals_mut(&mut self) -> Option<&mut [P::Vector]> {
match self.normals {
Some(ref mut ns) => Some(&mut ns[..]),
None => None
}
}
pub fn translate_by<T: Translation<P>>(&mut self, t: &T) {
for c in self.coords.iter_mut() {
*c = t.transform_point(c);
}
}
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);
}
}
}
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);
}
}
}
pub fn scale_by_scalar(&mut self, s: &P::Real) {
for c in self.coords.iter_mut() {
*c = *c * *s
}
}
}
impl<P> Polyline<P>
where P: Point {
#[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];
}
}
}
}