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 bevy::prelude::*;
use slotmap::SlotMap;
slotmap::new_key_type! {
pub(crate) struct SpanKey;
}
#[derive(Deref, DerefMut)]
pub(crate) struct Spans(SlotMap<SpanKey, Span>);
struct SpanKeyReflect(slotmap::KeyData);
impl Spans {
const DEFAULT_CAPACITY: usize = 1024;
pub(crate) fn with_min_capacity(min_capacity: usize) -> Self {
let capacity = min_capacity.max(Self::DEFAULT_CAPACITY);
Self(SlotMap::with_capacity_and_key(capacity))
}
}
pub(crate) struct SpanBuilder {
pub(crate) min: u16,
pub(crate) max: u16,
pub(crate) area: u8,
pub(crate) next: Option<SpanKey>,
}
impl SpanBuilder {
pub(crate) fn build(self) -> Span {
let mut span = Span {
data: [0; 32],
next: self.next,
};
span.set_min(self.min);
span.set_max(self.max);
span.set_area(self.area);
span
}
}
impl From<SpanBuilder> for Span {
fn from(builder: SpanBuilder) -> Self {
builder.build()
}
}
/// Corresponds to <https://github.com/recastnavigation/recastnavigation/blob/bd98d84c274ee06842bf51a4088ca82ac71f8c2d/Recast/Include/Recast.h#L294>
/// Build with [`SpanBuilder`]
#[derive(Clone, PartialEq, Eq, Debug)]
pub(crate) struct Span {
/// - 13 bits for min
/// - 13 bits for max
/// - 6 bits for area
data: [u8; 32],
/// The key of the next-higher span in the column
next: Option<SpanKey>,
}
impl Span {
const MIN_BITS: usize = 13;
const MAX_BITS: usize = 13;
const AREA_BITS: usize = 6;
#[inline]
pub(crate) fn min(&self) -> u16 {
// Safety: we are only indexing known constant indices
// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
// - 0 + 13 + 7 = 20 / 8 = 3
// - `bit_count` must be <= 16.
// - 13 <= 16
unsafe { Self::read_bits(&self.data, 0, Self::MIN_BITS) }
}
#[inline]
pub(crate) fn set_min(&mut self, min: u16) {
// Safety: we are only indexing known constant indices
// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
// - 0 + 13 + 7 = 20 / 8 = 3
// - `bit_count` must be <= 16.
// - 13 <= 16
// - `val` must fit into `bit_count` bits (higher bits truncated).
// - 13 <= 16
unsafe { Self::write_bits(&mut self.data, 0, Self::MIN_BITS, min) };
}
#[inline]
pub(crate) fn max(&self) -> u16 {
// Safety: we are only indexing known constant indices
// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
// - 13 + 13 + 7 = 33 / 8 = 4
// - `bit_count` must be <= 16.
// - 13 <= 16
unsafe { Self::read_bits(&self.data, Self::MIN_BITS, Self::MAX_BITS) }
}
#[inline]
pub(crate) fn set_max(&mut self, max: u16) {
// Safety: we are only indexing known constant indices
// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
// - 13 + 13 + 7 = 33 / 8 = 4
// - `bit_count` must be <= 16.
// - 13 <= 16
// - `val` must fit into `bit_count` bits (higher bits truncated).
// - 13 <= 16
unsafe { Self::write_bits(&mut self.data, Self::MIN_BITS, Self::MAX_BITS, max) };
}
#[inline]
pub(crate) fn area(&self) -> u8 {
// Safety: we are only indexing known constant indices
// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
// - 13 + 13 + 6 + 7 = 49 / 8 = 6
// - `bit_count` must be <= 16.
// - 6 <= 16
unsafe {
Self::read_bits(&self.data, Self::MIN_BITS + Self::MAX_BITS, Self::AREA_BITS) as u8
}
}
#[inline]
pub(crate) fn set_area(&mut self, area: u8) {
// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
// - 13 + 13 + 6 + 7 = 49 / 8 = 6
// - `bit_count` must be <= 16.
// - 6 <= 16
// - `val` must fit into `bit_count` bits (higher bits truncated).
// - 6 <= 16
unsafe {
Self::write_bits(
&mut self.data,
Self::MIN_BITS + Self::MAX_BITS,
Self::AREA_BITS,
area as u16,
)
};
}
/// Reads `bit_count` bits from `data` starting at `bit_offset` and returns them as a `u16`.
///
/// # Safety
/// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
/// - `bit_count` must be <= 16.
///
/// The function reads up to 3 bytes starting from the byte containing `bit_offset`,
/// assembles them into a `u32`, then shifts and masks to extract the desired bits.
///
/// No bounds checks or validation are performed for performance, so misuse can cause undefined behavior.
#[inline]
unsafe fn read_bits(data: &[u8], bit_offset: usize, bit_count: usize) -> u16 {
let byte_offset = bit_offset / 8;
let bit_in_byte = bit_offset % 8;
// Read 3 bytes starting at byte_offset into a u32 for bit manipulation
let val = (data[byte_offset] as u32)
| ((data[byte_offset + 1] as u32) << 8)
| ((data[byte_offset + 2] as u32) << 16);
// Shift right to discard unwanted lower bits, then mask to keep only bit_count bits
((val >> bit_in_byte) & ((1 << bit_count) - 1)) as u16
}
/// Writes `bit_count` bits from `val` into `data` starting at `bit_offset`.
///
/// # Safety
/// - Caller must ensure `data` has at least `(bit_offset + bit_count + 7) / 8` bytes.
/// - `bit_count` must be <= 16.
/// - `val` must fit into `bit_count` bits (higher bits truncated).
#[inline]
unsafe fn write_bits(data: &mut [u8], bit_offset: usize, bit_count: usize, val: u16) {
let byte_offset = bit_offset / 8;
let bit_in_byte = bit_offset % 8;
// Read 3 bytes into u32 to avoid partial overwrite issues
let mut current = (data[byte_offset] as u32)
| ((data[byte_offset + 1] as u32) << 8)
| ((data[byte_offset + 2] as u32) << 16);
// Create mask for the bits we're gonna overwrite
let mask = ((1u32 << bit_count) - 1) << bit_in_byte;
// Clear the target bits
current &= !mask;
// Set the bits from val (mask val to be safe)
current |= (val as u32 & ((1 << bit_count) - 1)) << bit_in_byte;
// Write back the bytes
data[byte_offset] = current as u8;
data[byte_offset + 1] = (current >> 8) as u8;
data[byte_offset + 2] = (current >> 16) as u8;
}
#[inline]
pub(crate) fn next(&self) -> Option<SpanKey> {
self.next
}
#[inline]
pub(crate) fn set_next(&mut self, next: impl Into<Option<SpanKey>>) {
self.next = next.into();
}
}
#[cfg(test)]
mod tests {
use super::*;
fn span() -> Span {
SpanBuilder {
min: 2,
max: 10,
area: 4,
next: None,
}
.build()
}
#[test]
fn can_retrieve_span_data_after_building() {
let span = span();
assert_eq!(span.min(), 2);
assert_eq!(span.max(), 10);
assert_eq!(span.area(), 4);
assert_eq!(span.next(), None);
}
#[test]
fn can_retrieve_span_data_after_setting() {
let mut span = span();
let mut slotmap = SlotMap::with_key();
let span_key: SpanKey = slotmap.insert(span.clone());
span.set_min(1);
span.set_max(4);
span.set_area(3);
span.set_next(span_key);
assert_eq!(span.min(), 1);
assert_eq!(span.max(), 4);
assert_eq!(span.area(), 3);
assert_eq!(span.next(), Some(span_key));
}
}