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
use rapier::prelude::{Aabb, VoxelData, VoxelState, Voxels};
use crate::math::{IVect, Vect};
#[cfg(feature = "dim2")]
use bevy::math::bounding::Aabb2d as BevyAabb;
#[cfg(feature = "dim3")]
use bevy::math::bounding::Aabb3d as BevyAabb;
fn aabb_na_from_bevy(aabb: &BevyAabb) -> Aabb {
rapier::parry::bounding_volume::Aabb::new(aabb.min.into(), aabb.max.into())
}
fn aabb_bevy_from_na(aabb: &Aabb) -> BevyAabb {
BevyAabb {
min: aabb.mins.into(),
max: aabb.maxs.into(),
}
}
/// Read-only access to the properties of a [`Voxels`] shape.
#[derive(Copy, Clone)]
pub struct VoxelsView<'a> {
/// The raw shape from Rapier.
pub raw: &'a Voxels,
}
// TODO: implement those on VoxelsViewMut too.
macro_rules! impl_ref_methods(
($View: ident) => {
impl<'a> $View<'a> {
/// Shortcut to [`Voxels::total_memory_size`].
pub fn total_memory_size(&self) -> usize {
self.raw.total_memory_size()
}
/// Shortcut to [`Voxels::heap_memory_size`].
pub fn heap_memory_size(&self) -> usize {
self.raw.heap_memory_size()
}
/// Shortcut to [`Voxels::local_aabb`].
pub fn extents(&self) -> Vect {
self.raw.local_aabb().extents().into()
}
/// Shortcut to [`Voxels::local_aabb`].
pub fn domain_center(&self) -> Vect {
self.raw.local_aabb().center().coords.into()
}
/// Shortcut to [`Voxels::domain`].
pub fn is_voxel_in_bounds(&self, key: IVect) -> bool {
let [mins, maxs] = self.raw.domain();
#[cfg(feature = "dim2")]
{
key.x >= mins.x && key.y >= mins.y && key.x < maxs.x && key.y < maxs.y
}
#[cfg(feature = "dim3")]
{
key.x >= mins.x
&& key.y >= mins.y
&& key.z >= mins.z
&& key.x < maxs.x
&& key.y < maxs.y
&& key.z < maxs.z
}
}
/// Shortcut to [`Voxels::voxel_aabb`].
pub fn voxel_aabb(&self, key: IVect) -> BevyAabb {
aabb_bevy_from_na(&self.raw.voxel_aabb(key.into()))
}
/// Shortcut to [`Voxels::voxel_state`].
pub fn voxel_state(&self, key: IVect) -> Option<VoxelState> {
self.raw.voxel_state(key.into())
}
/// Shortcut to [`Voxels::voxel_at_point`].
pub fn voxel_at_point_unchecked(&self, point: Vect) -> IVect {
let p = self.raw.voxel_at_point(point.into());
#[cfg(feature = "dim2")]
{
IVect::new(p.x, p.y)
}
#[cfg(feature = "dim3")]
{
IVect::new(p.x, p.y, p.z)
}
}
/// Shortcut to [`Voxels::voxel_at_point`].
pub fn voxel_at_point(&self, pt: Vect) -> Option<IVect> {
let voxel = self.voxel_at_point_unchecked(pt);
if self.is_voxel_in_bounds(voxel) {
Some(voxel)
} else {
None
}
}
/// Shortcut to [`Voxels::domain`].
pub fn clamp_voxel(&self, key: IVect) -> IVect {
let [mins, maxs] = self.raw.domain();
#[cfg(feature = "dim2")]
{
IVect::new(
key.x.clamp(mins.x, maxs.x - 1),
key.y.clamp(mins.y, maxs.y - 1),
)
}
#[cfg(feature = "dim3")]
{
IVect::new(
key.x.clamp(mins.x, maxs.x - 1),
key.y.clamp(mins.y, maxs.y - 1),
key.z.clamp(mins.z, maxs.z - 1),
)
}
}
/// Shortcut to [`Voxels::voxel_range_intersecting_local_aabb`].
pub fn voxel_range_intersecting_local_aabb(&self, aabb: &BevyAabb) -> [IVect; 2] {
let res = self
.raw
.voxel_range_intersecting_local_aabb(&aabb_na_from_bevy(aabb));
[res[0].into(), res[1].into()]
}
/// Shortcut to [`Voxels::voxel_range_aabb`].
pub fn voxel_range_aabb(&self, mins: IVect, maxs: IVect) -> BevyAabb {
aabb_bevy_from_na(&self.raw.voxel_range_aabb(mins.into(), maxs.into()))
}
/// Shortcut to [`Voxels::align_aabb_to_grid`].
pub fn align_aabb_to_grid(&self, aabb: &BevyAabb) -> BevyAabb {
aabb_bevy_from_na(&self.raw.align_aabb_to_grid(&aabb_na_from_bevy(aabb)))
}
/// Shortcut to [`Voxels::voxels_intersecting_local_aabb`].
pub fn voxels_intersecting_local_aabb(
&self,
aabb: &BevyAabb,
) -> impl Iterator<Item = VoxelData> + '_ {
self.raw
.voxels_intersecting_local_aabb(&aabb_na_from_bevy(aabb))
}
/// Shortcut to [`Voxels::voxels`].
pub fn voxels(&self) -> impl Iterator<Item = VoxelData> + '_ {
self.raw.voxels()
}
/// Shortcut to [`Voxels::split_with_box`].
#[cfg(feature = "dim2")]
pub fn split_with_box(&self, aabb: &BevyAabb) -> (Option<Voxels>, Option<Voxels>) {
self.raw.split_with_box(&aabb_na_from_bevy(aabb))
}
/// Shortcut to [`Voxels::voxels_in_range`].
pub fn voxels_in_range(
&self,
mins: IVect,
maxs: IVect,
) -> impl Iterator<Item = VoxelData> + '_ {
self.raw.voxels_in_range(mins.into(), maxs.into())
}
}
}
);
impl_ref_methods!(VoxelsView);
/// Read-write access to the properties of a [`Voxels`].
pub struct VoxelsViewMut<'a> {
/// The raw shape from Rapier.
pub raw: &'a mut Voxels,
}
impl_ref_methods!(VoxelsViewMut);
impl<'a> VoxelsViewMut<'a> {
/// Shortcut to set voxel state with bounds checking.
pub fn try_set_voxel(&mut self, key: IVect, is_filled: bool) -> Option<VoxelState> {
if self.is_voxel_in_bounds(key) {
Some(self.raw.set_voxel(key.into(), is_filled))
} else {
None
}
}
/// Shortcut to to [`Voxels::set_voxel`].
pub fn set_voxel(&mut self, key: IVect, is_filled: bool) -> VoxelState {
self.raw.set_voxel(key.into(), is_filled)
}
/// Shortcut to [`Voxels::crop`].
pub fn crop(&mut self, domain_mins: IVect, domain_maxs: IVect) {
self.raw.crop(domain_mins.into(), domain_maxs.into());
}
}