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
124
use crate::math::{Isometry, Point, Vector};
use alga::linear::{Rotation, Translation};
use na::{self, RealField};
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Polyline<N: RealField> {
coords: Vec<Point<N>>,
normals: Option<Vec<Vector<N>>>,
}
impl<N: RealField> Polyline<N> {
pub fn new(coords: Vec<Point<N>>, normals: Option<Vec<Vector<N>>>) -> Polyline<N> {
if let Some(ref ns) = normals {
assert!(
coords.len() == ns.len(),
"There must be exactly one normal per vertex."
);
}
Polyline { coords, normals }
}
}
impl<N: RealField> Polyline<N> {
pub fn unwrap(self) -> (Vec<Point<N>>, Option<Vec<Vector<N>>>) {
(self.coords, self.normals)
}
#[inline]
pub fn coords(&self) -> &[Point<N>] {
&self.coords[..]
}
#[inline]
pub fn coords_mut(&mut self) -> &mut [Point<N>] {
&mut self.coords[..]
}
#[inline]
pub fn normals(&self) -> Option<&[Vector<N>]> {
self.normals.as_ref().map(Vec::as_slice)
}
#[inline]
pub fn normals_mut(&mut self) -> Option<&mut [Vector<N>]> {
self.normals.as_mut().map(Vec::as_mut_slice)
}
pub fn translate_by<T: Translation<Point<N>>>(&mut self, t: &T) {
for c in self.coords.iter_mut() {
*c = t.transform_point(c);
}
}
pub fn rotate_by<R: Rotation<Point<N>>>(&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(&mut self, t: &Isometry<N>) {
for c in self.coords.iter_mut() {
*c = t * *c;
}
for n in self.normals.iter_mut() {
for n in n.iter_mut() {
*n = t * &*n;
}
}
}
#[inline]
pub fn transformed(mut self, t: &Isometry<N>) -> Self {
self.transform_by(t);
self
}
pub fn scale_by_scalar(&mut self, s: &N) {
for c in self.coords.iter_mut() {
*c = *c * *s
}
}
#[inline]
pub fn scale_by(&mut self, s: &Vector<N>) {
for c in self.coords.iter_mut() {
for i in 0..na::dimension::<Vector<N>>() {
c[i] = (*c)[i] * s[i];
}
}
}
#[inline]
pub fn scaled(mut self, s: &Vector<N>) -> Self {
self.scale_by(s);
self
}
}