From ba60bdc5efe865cfce846edd570f0a10c45701ab Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Thu, 12 Jun 2025 14:22:59 +0200 Subject: [PATCH 1/3] Fix contact manifold voxels by changing the caching mechanism for keys --- .../contact_manifolds_voxels_shape.rs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs b/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs index 4af938c9..750ea50f 100644 --- a/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs +++ b/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs @@ -349,11 +349,28 @@ impl CanonicalVoxelShape { adjust_canon(AxisMask::Z_NEG, 2, &mut key_low, mins[2]); } + /// The linearized index associated to the given voxel key. + /// + /// This function allows to override the dimensions, that would lead to out of bounds index. + /// + /// It can be useful to keep a cache on voxels information when it would not fit the original dimension. + pub fn linear_index_for_dimensions( + voxel_key: Point, + domain_mins: Point, + domain_maxs: Point, + ) -> u32 { + let dims = (domain_maxs - domain_mins).map(|e| e as u32); + let rel_key = voxel_key - domain_mins; + (rel_key.x + rel_key.y * dims[0] as i32) as u32 + } + + let mut domain_dilated: [Point; 2] = voxels.domain().map(|v| *v); + domain_dilated[0].coords -= Vector::repeat(1); Self { range: [key_low, key_high], workspace_key: Vector2::new( - voxels.linear_index(key_low), - voxels.linear_index(key_high), + linear_index_for_dimensions(key_low, domain_dilated[0], domain_dilated[1]), + linear_index_for_dimensions(key_high, domain_dilated[0], domain_dilated[1]), ), } } From e7491d0383c36a1d3ce1502a41d4539cae079367 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 23 Jul 2025 11:31:21 +0200 Subject: [PATCH 2/3] add more comments --- .../contact_manifolds_voxels_shape.rs | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs b/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs index 72e7ce84..e38f214a 100644 --- a/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs +++ b/src/query/contact_manifolds/contact_manifolds_voxels_shape.rs @@ -316,6 +316,7 @@ pub fn contact_manifolds_voxels_shape( #[derive(Copy, Clone, Debug)] pub(crate) struct CanonicalVoxelShape { pub range: [Point; 2], + /// See [Self::linear_index_for_dimensions] pub workspace_key: Vector2, } @@ -348,32 +349,32 @@ impl CanonicalVoxelShape { adjust_canon(AxisMask::Z_NEG, 2, &mut key_low, mins[2]); } - /// The linearized index associated to the given voxel key. - /// - /// This function allows to override the dimensions, that would lead to out of bounds index. - /// - /// It can be useful to keep a cache on voxels information when it would not fit the original dimension. - pub fn linear_index_for_dimensions( - voxel_key: Point, - domain_mins: Point, - domain_maxs: Point, - ) -> u32 { - let dims = (domain_maxs - domain_mins).map(|e| e as u32); - let rel_key = voxel_key - domain_mins; - (rel_key.x + rel_key.y * dims[0] as i32) as u32 - } - let mut domain_dilated: [Point; 2] = voxels.domain().map(|v| *v); domain_dilated[0].coords -= Vector::repeat(1); Self { range: [key_low, key_high], workspace_key: Vector2::new( - linear_index_for_dimensions(key_low, domain_dilated[0], domain_dilated[1]), - linear_index_for_dimensions(key_high, domain_dilated[0], domain_dilated[1]), + Self::linear_index_for_dimensions(key_low, domain_dilated[0], domain_dilated[1]), + Self::linear_index_for_dimensions(key_high, domain_dilated[0], domain_dilated[1]), ), } } + /// The linearized index associated to the given voxel key. + /// + /// This function allows to override the dimensions, that would lead to out of bounds index. + /// + /// It can be useful to keep a cache on voxels information when it would not fit the original dimension. + pub fn linear_index_for_dimensions( + voxel_key: Point, + domain_mins: Point, + domain_maxs: Point, + ) -> u32 { + let dims = (domain_maxs - domain_mins).map(|e| e as u32); + let rel_key = voxel_key - domain_mins; + (rel_key.x + rel_key.y * dims[0] as i32) as u32 + } + pub fn cuboid( &self, voxels: &Voxels, From 0f78e4c10d7fda9f4d4c9dea3b56f1756bcbb414 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 23 Jul 2025 11:31:37 +0200 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59d35eec..4eef481a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## Unreleased + +### Fixed + +- Fix contact manifold voxels cached key overflowing. ([#346](https://github.com/dimforge/parry/pull/346)) + # 0.22.0-beta.1 ### Fixed