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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use crate::math::{Isometry, Point, Real, Vector};
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct TrackedContact<Data> {
pub local_p1: Point<Real>,
pub local_p2: Point<Real>,
pub dist: Real,
pub fid1: u32,
pub fid2: u32,
pub data: Data,
}
impl<Data: Default + Copy> TrackedContact<Data> {
pub fn new(
local_p1: Point<Real>,
local_p2: Point<Real>,
fid1: u32,
fid2: u32,
dist: Real,
) -> Self {
Self {
local_p1,
local_p2,
fid1,
fid2,
dist,
data: Data::default(),
}
}
pub fn flipped(
local_p1: Point<Real>,
local_p2: Point<Real>,
fid1: u32,
fid2: u32,
dist: Real,
flipped: bool,
) -> Self {
if !flipped {
Self::new(local_p1, local_p2, fid1, fid2, dist)
} else {
Self::new(local_p2, local_p1, fid2, fid1, dist)
}
}
pub fn copy_geometry_from(&mut self, contact: Self) {
self.local_p1 = contact.local_p1;
self.local_p2 = contact.local_p2;
self.fid1 = contact.fid1;
self.fid2 = contact.fid2;
self.dist = contact.dist;
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct ContactManifold<ManifoldData, ContactData> {
#[cfg(feature = "dim2")]
pub points: arrayvec::ArrayVec<TrackedContact<ContactData>, 2>,
#[cfg(feature = "dim3")]
pub points: Vec<TrackedContact<ContactData>>,
pub local_n1: Vector<Real>,
pub local_n2: Vector<Real>,
pub subshape1: u32,
pub subshape2: u32,
pub subshape_pos1: Option<Isometry<Real>>,
pub subshape_pos2: Option<Isometry<Real>>,
pub data: ManifoldData,
}
impl<ManifoldData, ContactData: Default + Copy> ContactManifold<ManifoldData, ContactData> {
pub fn new() -> Self
where
ManifoldData: Default,
{
Self::with_data(0, 0, ManifoldData::default())
}
pub fn with_data(subshape1: u32, subshape2: u32, data: ManifoldData) -> Self {
Self {
#[cfg(feature = "dim2")]
points: arrayvec::ArrayVec::new(),
#[cfg(feature = "dim3")]
points: Vec::new(),
local_n1: Vector::zeros(),
local_n2: Vector::zeros(),
subshape1,
subshape2,
subshape_pos1: None,
subshape_pos2: None,
data,
}
}
pub fn take(&mut self) -> Self
where
ManifoldData: Clone,
{
#[cfg(feature = "dim2")]
let points = self.points.clone();
#[cfg(feature = "dim3")]
let points = std::mem::replace(&mut self.points, Vec::new());
self.points.clear();
ContactManifold {
points,
local_n1: self.local_n1,
local_n2: self.local_n2,
subshape1: self.subshape1,
subshape2: self.subshape2,
subshape_pos1: self.subshape_pos1,
subshape_pos2: self.subshape_pos2,
data: self.data.clone(),
}
}
#[inline]
pub fn contacts(&self) -> &[TrackedContact<ContactData>] {
&self.points
}
#[inline]
pub fn try_update_contacts(&mut self, pos12: &Isometry<Real>) -> bool {
const DOT_THRESHOLD: Real = crate::utils::COS_5_DEGREES;
const DIST_SQ_THRESHOLD: Real = 0.001;
self.try_update_contacts_eps(pos12, DOT_THRESHOLD, DIST_SQ_THRESHOLD)
}
#[inline]
pub fn try_update_contacts_eps(
&mut self,
pos12: &Isometry<Real>,
angle_dot_threshold: Real,
dist_sq_threshold: Real,
) -> bool {
if self.points.len() == 0 {
return false;
}
let local_n2 = pos12 * self.local_n2;
if -self.local_n1.dot(&local_n2) < angle_dot_threshold {
return false;
}
for pt in &mut self.points {
let local_p2 = pos12 * pt.local_p2;
let dpt = local_p2 - pt.local_p1;
let dist = dpt.dot(&self.local_n1);
if dist * pt.dist < 0.0 {
return false;
}
let new_local_p1 = local_p2 - self.local_n1 * dist;
if na::distance_squared(&pt.local_p1, &new_local_p1) > dist_sq_threshold {
return false;
}
pt.dist = dist;
pt.local_p1 = new_local_p1;
}
true
}
pub fn match_contacts(&mut self, old_contacts: &[TrackedContact<ContactData>]) {
for contact in &mut self.points {
for old_contact in old_contacts {
if contact.fid1 == old_contact.fid1 && contact.fid2 == old_contact.fid2 {
contact.data = old_contact.data;
}
}
}
}
pub fn match_contacts_using_positions(
&mut self,
old_contacts: &[TrackedContact<ContactData>],
dist_threshold: Real,
) {
let sq_threshold = dist_threshold * dist_threshold;
for contact in &mut self.points {
for old_contact in old_contacts {
if na::distance_squared(&contact.local_p1, &old_contact.local_p1) < sq_threshold
&& na::distance_squared(&contact.local_p2, &old_contact.local_p2) < sq_threshold
{
contact.data = old_contact.data;
}
}
}
}
pub fn clear(&mut self) {
self.points.clear();
}
pub fn find_deepest_contact(&self) -> Option<&TrackedContact<ContactData>> {
let mut deepest = self.points.get(0)?;
for pt in &self.points {
if pt.dist < deepest.dist {
deepest = pt;
}
}
Some(deepest)
}
}