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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::bounding_volume::AABB;
use crate::math::{Isometry, Point, Real, Vector};
use crate::partitioning::QBVH;
use crate::query::{PointProjection, PointQueryWithLocation};
use crate::shape::composite_shape::SimdCompositeShape;
use crate::shape::{FeatureId, Segment, SegmentPointLocation, Shape, TypedSimdCompositeShape};
#[cfg(not(feature = "std"))]
use na::ComplexField;
#[derive(Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct Polyline {
qbvh: QBVH<u32>,
vertices: Vec<Point<Real>>,
indices: Vec<[u32; 2]>,
}
impl Polyline {
pub fn new(vertices: Vec<Point<Real>>, indices: Option<Vec<[u32; 2]>>) -> Self {
let indices =
indices.unwrap_or_else(|| (0..vertices.len() as u32 - 1).map(|i| [i, i + 1]).collect());
let data = indices.iter().enumerate().map(|(i, idx)| {
let aabb =
Segment::new(vertices[idx[0] as usize], vertices[idx[1] as usize]).local_aabb();
(i as u32, aabb)
});
let mut qbvh = QBVH::new();
qbvh.clear_and_rebuild(data, 0.0);
Self {
qbvh,
vertices,
indices,
}
}
pub fn aabb(&self, pos: &Isometry<Real>) -> AABB {
self.qbvh.root_aabb().transform_by(pos)
}
pub fn local_aabb(&self) -> &AABB {
&self.qbvh.root_aabb()
}
pub(crate) fn qbvh(&self) -> &QBVH<u32> {
&self.qbvh
}
pub fn num_segments(&self) -> usize {
self.indices.len()
}
pub fn segments(&self) -> impl ExactSizeIterator<Item = Segment> + '_ {
self.indices.iter().map(move |ids| {
Segment::new(
self.vertices[ids[0] as usize],
self.vertices[ids[1] as usize],
)
})
}
pub fn segment(&self, i: u32) -> Segment {
let idx = self.indices[i as usize];
Segment::new(
self.vertices[idx[0] as usize],
self.vertices[idx[1] as usize],
)
}
pub fn segment_feature_to_polyline_feature(
&self,
segment: u32,
_feature: FeatureId,
) -> FeatureId {
#[cfg(feature = "dim2")]
return FeatureId::Face(segment);
#[cfg(feature = "dim3")]
return FeatureId::Edge(segment);
}
pub fn vertices(&self) -> &[Point<Real>] {
&self.vertices[..]
}
pub fn indices(&self) -> &[[u32; 2]] {
&self.indices
}
pub fn flat_indices(&self) -> &[u32] {
unsafe {
let len = self.indices.len() * 3;
let data = self.indices.as_ptr() as *const u32;
std::slice::from_raw_parts(data, len)
}
}
pub fn scaled(mut self, scale: &Vector<Real>) -> Self {
self.vertices
.iter_mut()
.for_each(|pt| pt.coords.component_mul_assign(scale));
Self {
qbvh: self.qbvh.scaled(scale),
vertices: self.vertices,
indices: self.indices,
}
}
pub fn reverse(&mut self) {
for idx in &mut self.indices {
idx.swap(0, 1);
}
self.indices.reverse();
for (_, seg_id) in self.qbvh.iter_data_mut() {
*seg_id = self.indices.len() as u32 - *seg_id - 1;
}
}
pub fn project_local_point_assuming_solid_interior_ccw(
&self,
point: Point<Real>,
#[cfg(feature = "dim3")] axis: u8,
) -> (PointProjection, (u32, SegmentPointLocation)) {
let mut proj = self.project_local_point_and_get_location(&point, false);
let segment1 = self.segment((proj.1).0);
#[cfg(feature = "dim2")]
let normal1 = segment1.normal();
#[cfg(feature = "dim3")]
let normal1 = segment1.planar_normal(axis);
if let Some(normal1) = normal1 {
proj.0.is_inside = match proj.1 .1 {
SegmentPointLocation::OnVertex(i) => {
let dir2 = if i == 0 {
let adj_seg = if proj.1 .0 == 0 {
self.indices().len() as u32 - 1
} else {
proj.1 .0 - 1
};
assert_eq!(segment1.a, self.segment(adj_seg).b);
-self.segment(adj_seg).scaled_direction()
} else {
assert_eq!(i, 1);
let adj_seg = (proj.1 .0 + 1) % self.indices().len() as u32;
assert_eq!(segment1.b, self.segment(adj_seg).a);
self.segment(adj_seg).scaled_direction()
};
let dot = normal1.dot(&dir2);
let threshold = 1.0e-3 * dir2.norm();
if dot.abs() > threshold {
dot >= 0.0
} else {
(point - proj.0.point).dot(&normal1) <= 0.0
}
}
SegmentPointLocation::OnEdge(_) => (point - proj.0.point).dot(&normal1) <= 0.0,
};
}
proj
}
}
impl SimdCompositeShape for Polyline {
fn map_part_at(&self, i: u32, f: &mut dyn FnMut(Option<&Isometry<Real>>, &dyn Shape)) {
let tri = self.segment(i);
f(None, &tri)
}
fn qbvh(&self) -> &QBVH<u32> {
&self.qbvh
}
}
impl TypedSimdCompositeShape for Polyline {
type PartShape = Segment;
type PartId = u32;
#[inline(always)]
fn map_typed_part_at(
&self,
i: u32,
mut f: impl FnMut(Option<&Isometry<Real>>, &Self::PartShape),
) {
let seg = self.segment(i);
f(None, &seg)
}
#[inline(always)]
fn map_untyped_part_at(&self, i: u32, mut f: impl FnMut(Option<&Isometry<Real>>, &dyn Shape)) {
let seg = self.segment(i);
f(None, &seg)
}
fn typed_qbvh(&self) -> &QBVH<u32> {
&self.qbvh
}
}