parry3d/query/contact_manifolds/
contact_manifolds_voxels_shape.rs

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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use crate::bounding_volume::Aabb;
use crate::math::{Isometry, Point, Real, Translation, Vector, DIM};
use crate::query::{
    ContactManifold, ContactManifoldsWorkspace, PersistentQueryDispatcher, PointQuery,
    TypedWorkspaceData, WorkspaceData,
};
use crate::shape::{
    AxisMask, Cuboid, RoundShape, Shape, SupportMap, VoxelPrimitiveGeometry, VoxelType, Voxels,
};
use crate::utils::hashmap::{Entry, HashMap};
use crate::utils::IsometryOpt;
use alloc::{boxed::Box, vec::Vec};

#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize),
    archive(check_bytes)
)]
#[derive(Clone)]
struct SubDetector {
    manifold_id: usize,
    selected_contacts: u32,
    timestamp: bool,
}

// NOTE: this is using a similar kind of cache as compound shape and height-field.
//       It is different from the trimesh cash though. Which one is better?
/// A workspace for collision-detection against voxels shape.
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Clone, Default)]
pub struct VoxelsShapeContactManifoldsWorkspace {
    timestamp: bool,
    sub_detectors: HashMap<[u32; 2], SubDetector>,
}

impl VoxelsShapeContactManifoldsWorkspace {
    /// A new empty workspace for collision-detection against voxels shape.
    pub fn new() -> Self {
        Self::default()
    }
}

impl WorkspaceData for VoxelsShapeContactManifoldsWorkspace {
    fn as_typed_workspace_data(&self) -> TypedWorkspaceData {
        TypedWorkspaceData::VoxelsShapeContactManifoldsWorkspace(self)
    }

    fn clone_dyn(&self) -> Box<dyn WorkspaceData> {
        Box::new(self.clone())
    }
}

fn ensure_workspace_exists(workspace: &mut Option<ContactManifoldsWorkspace>) {
    if workspace
        .as_ref()
        .and_then(|w| w.0.downcast_ref::<VoxelsShapeContactManifoldsWorkspace>())
        .is_some()
    {
        return;
    }

    *workspace = Some(ContactManifoldsWorkspace(Box::new(
        VoxelsShapeContactManifoldsWorkspace::new(),
    )));
}

/// Computes the contact manifold between a convex shape and a ball, both represented as a `Shape` trait-object.
pub fn contact_manifolds_voxels_shape_shapes<ManifoldData, ContactData>(
    dispatcher: &dyn PersistentQueryDispatcher<ManifoldData, ContactData>,
    pos12: &Isometry<Real>,
    shape1: &dyn Shape,
    shape2: &dyn Shape,
    prediction: Real,
    manifolds: &mut Vec<ContactManifold<ManifoldData, ContactData>>,
    workspace: &mut Option<ContactManifoldsWorkspace>,
) where
    ManifoldData: Default + Clone,
    ContactData: Default + Copy,
{
    if let Some(voxels1) = shape1.as_voxels() {
        contact_manifolds_voxels_shape(
            dispatcher, pos12, voxels1, shape2, prediction, manifolds, workspace, false,
        );
    } else if let Some(voxels2) = shape2.as_voxels() {
        contact_manifolds_voxels_shape(
            dispatcher,
            &pos12.inverse(),
            voxels2,
            shape1,
            prediction,
            manifolds,
            workspace,
            true,
        );
    }
}

/// Computes the contact manifold between a convex shape and a ball.
pub fn contact_manifolds_voxels_shape<ManifoldData, ContactData>(
    dispatcher: &dyn PersistentQueryDispatcher<ManifoldData, ContactData>,
    pos12: &Isometry<Real>,
    voxels1: &Voxels,
    shape2: &dyn Shape,
    prediction: Real,
    manifolds: &mut Vec<ContactManifold<ManifoldData, ContactData>>,
    workspace: &mut Option<ContactManifoldsWorkspace>,
    flipped: bool,
) where
    ManifoldData: Default + Clone,
    ContactData: Default + Copy,
{
    ensure_workspace_exists(workspace);
    let workspace: &mut VoxelsShapeContactManifoldsWorkspace =
        workspace.as_mut().unwrap().0.downcast_mut().unwrap();
    let new_timestamp = !workspace.timestamp;

    // TODO: avoid reallocating the new `manifolds` vec at each step.
    let mut old_manifolds = core::mem::take(manifolds);

    workspace.timestamp = new_timestamp;

    let radius1 = voxels1.voxel_size() / 2.0;

    let aabb1 = voxels1.local_aabb();
    let aabb2_1 = shape2.compute_aabb(pos12);
    let domain2_1 = Aabb {
        mins: aabb2_1.mins - radius1 * 10.0,
        maxs: aabb2_1.maxs + radius1 * 10.0,
    };

    if let Some(intersection_aabb1) = aabb1.intersection(&aabb2_1) {
        for vox1 in voxels1.voxels_intersecting_local_aabb(&intersection_aabb1) {
            let vox_type1 = vox1.state.voxel_type();

            // TODO: would be nice to have a strategy to handle interior voxels for depenetration.
            if vox_type1 == VoxelType::Empty || vox_type1 == VoxelType::Interior {
                continue;
            }

            let mut key_low = vox1.grid_coords;
            let mut key_high = key_low;
            let mins = voxels1.domain()[0];
            let maxs = voxels1.domain()[1] - Vector::repeat(1);
            let mask1 = vox1.state.free_faces();

            let adjust_canon = |axis: AxisMask, i: usize, key: &mut Point<i32>, val: i32| {
                if !mask1.contains(axis) {
                    key[i] = val;
                }
            };

            adjust_canon(AxisMask::X_POS, 0, &mut key_high, maxs[0]);
            adjust_canon(AxisMask::X_NEG, 0, &mut key_low, mins[0]);
            adjust_canon(AxisMask::Y_POS, 1, &mut key_high, maxs[1]);
            adjust_canon(AxisMask::Y_NEG, 1, &mut key_low, mins[1]);

            #[cfg(feature = "dim3")]
            {
                adjust_canon(AxisMask::Z_POS, 2, &mut key_high, maxs[2]);
                adjust_canon(AxisMask::Z_NEG, 2, &mut key_low, mins[2]);
            }

            let workspace_key = [
                voxels1.linear_index(key_low),
                voxels1.linear_index(key_high),
            ];

            // TODO: could we refactor the workspace system between Voxels, HeightField, and CompoundShape?
            //       (and maybe TriMesh too but it’s using a different approach).
            let (sub_detector, manifold_updated) =
                match workspace.sub_detectors.entry(workspace_key) {
                    Entry::Occupied(entry) => {
                        let sub_detector = entry.into_mut();

                        if sub_detector.timestamp != new_timestamp {
                            let manifold = old_manifolds[sub_detector.manifold_id].take();
                            *sub_detector = SubDetector {
                                manifold_id: manifolds.len(),
                                timestamp: new_timestamp,
                                selected_contacts: 0,
                            };

                            manifolds.push(manifold);
                            (sub_detector, false)
                        } else {
                            (sub_detector, true)
                        }
                    }
                    Entry::Vacant(entry) => {
                        let sub_detector = SubDetector {
                            manifold_id: manifolds.len(),
                            selected_contacts: 0,
                            timestamp: new_timestamp,
                        };

                        let vid = vox1.linear_id;
                        let (id1, id2) = if flipped { (0, vid) } else { (vid, 0) };
                        manifolds.push(ContactManifold::with_data(
                            id1,
                            id2,
                            ManifoldData::default(),
                        ));

                        (entry.insert(sub_detector), false)
                    }
                };

            /*
             * Update the contact manifold if needed.
             */
            let manifold = &mut manifolds[sub_detector.manifold_id];

            if !manifold_updated {
                let mut canonical_mins1 = voxels1.voxel_center(key_low);
                let mut canonical_maxs1 = voxels1.voxel_center(key_high);

                for k in 0..DIM {
                    if key_low[k] != vox1.grid_coords[k] {
                        canonical_mins1[k] = canonical_mins1[k].max(domain2_1.mins[k]);
                    }

                    if key_high[k] != vox1.grid_coords[k] {
                        canonical_maxs1[k] = canonical_maxs1[k].min(domain2_1.maxs[k]);
                    }
                }

                let canonical_half_extents1 = (canonical_maxs1 - canonical_mins1) / 2.0 + radius1;
                let canonical_center1 = na::center(&canonical_mins1, &canonical_maxs1);

                let canonical_pseudo_cube1 = Cuboid::new(canonical_half_extents1);
                let canonical_pseudo_ball1 = RoundShape {
                    inner_shape: Cuboid::new(canonical_half_extents1 - radius1),
                    border_radius: radius1.x,
                };

                let canonical_shape1 = match voxels1.primitive_geometry() {
                    VoxelPrimitiveGeometry::PseudoBall => &canonical_pseudo_ball1 as &dyn Shape,
                    VoxelPrimitiveGeometry::PseudoCube => &canonical_pseudo_cube1 as &dyn Shape,
                };

                let canonical_pos12 = Translation::from(-canonical_center1) * pos12;

                // If we already computed contacts in the previous simulation step, their
                // local points are relative to the previously calculated canonical shape
                // which might not have the same local center as the one computed in this
                // step (because it’s based on the position of shape2 relative to voxels1).
                // So we need to adjust the local points to account for the position difference
                // and keep the point at the same "canonica-shape-space" location as in the previous frame.
                let prev_center = if flipped {
                    manifold
                        .subshape_pos2
                        .as_ref()
                        .map(|p| p.translation.vector)
                        .unwrap_or_default()
                } else {
                    manifold
                        .subshape_pos1
                        .as_ref()
                        .map(|p| p.translation.vector)
                        .unwrap_or_default()
                };
                let delta_center = canonical_center1.coords - prev_center;

                if flipped {
                    for pt in &mut manifold.points {
                        pt.local_p2 -= delta_center;
                    }
                } else {
                    for pt in &mut manifold.points {
                        pt.local_p1 -= delta_center;
                    }
                }

                // Update contacts.
                if flipped {
                    manifold.subshape_pos2 = Some(Isometry::from(canonical_center1));
                    let _ = dispatcher.contact_manifold_convex_convex(
                        &canonical_pos12.inverse(),
                        shape2,
                        canonical_shape1,
                        None,
                        None,
                        prediction,
                        manifold,
                    );
                } else {
                    manifold.subshape_pos1 = Some(Isometry::from(canonical_center1));
                    let _ = dispatcher.contact_manifold_convex_convex(
                        &canonical_pos12,
                        canonical_shape1,
                        shape2,
                        None,
                        None,
                        prediction,
                        manifold,
                    );
                }
            }

            /*
             * Filter-out points that don’t belong to this block.
             */
            let test_voxel = Cuboid::new(radius1 + Vector::repeat(1.0e-2));
            let penetration_dir1 = if flipped {
                manifold.local_n2
            } else {
                manifold.local_n1
            };

            for (i, pt) in manifold.points.iter().enumerate() {
                if pt.dist < 0.0 {
                    // If this is a penetration, double-check that we are not hitting the
                    // interior of the infinitely expanded canonical shape by checking if
                    // the opposite normal would have led to a better vector.
                    let cuboid1 = Cuboid::new(radius1);
                    let sp1 = cuboid1.local_support_point(&-penetration_dir1) + vox1.center.coords;
                    let sm2 = shape2
                        .as_support_map()
                        .expect("Unsupported collision pair.");
                    let sp2 = sm2.support_point(pos12, &penetration_dir1);
                    let test_dist = (sp2 - sp1).dot(&-penetration_dir1);
                    let keep = test_dist < pt.dist;

                    if !keep {
                        // We don’t want to keep this point as it is an incorrect penetration
                        // caused by the canonical shape expansion.
                        continue;
                    }
                }

                let pt_in_voxel_space = if flipped {
                    manifold.subshape_pos2.transform_point(&pt.local_p2) - vox1.center.coords
                } else {
                    manifold.subshape_pos1.transform_point(&pt.local_p1) - vox1.center.coords
                };
                sub_detector.selected_contacts |=
                    (test_voxel.contains_local_point(&pt_in_voxel_space) as u32) << i;
            }
        }
    }

    // Remove contacts marked as ignored.
    for sub_detector in workspace.sub_detectors.values() {
        if sub_detector.timestamp == new_timestamp {
            let manifold = &mut manifolds[sub_detector.manifold_id];
            let mut k = 0;
            manifold.points.retain(|_| {
                let keep = (sub_detector.selected_contacts & (1 << k)) != 0;
                k += 1;
                keep
            });
        }
    }

    // Remove detectors which no longer index a valid manifold.
    workspace
        .sub_detectors
        .retain(|_, detector| detector.timestamp == new_timestamp);
}