raasta 1.0.0

Raasta — navigation and pathfinding engine for AGNOS
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! 3D navigation volumes — voxel-based pathfinding for flying/swimming agents.

use std::cmp::Ordering;
use std::collections::BinaryHeap;

use hisab::Vec3;
use serde::{Deserialize, Serialize};

#[cfg(feature = "logging")]
use tracing::instrument;

/// A position in the voxel grid.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VoxelPos {
    pub x: i32,
    pub y: i32,
    pub z: i32,
}

impl VoxelPos {
    #[must_use]
    pub fn new(x: i32, y: i32, z: i32) -> Self {
        Self { x, y, z }
    }

    /// Chebyshev distance (diagonal movement in 3D).
    #[inline]
    #[must_use]
    pub fn chebyshev_distance(self, other: VoxelPos) -> i32 {
        let dx = (self.x - other.x).abs();
        let dy = (self.y - other.y).abs();
        let dz = (self.z - other.z).abs();
        dx.max(dy).max(dz)
    }

    /// Octile-like 3D distance heuristic.
    #[inline]
    #[must_use]
    pub fn distance_3d(self, other: VoxelPos) -> f32 {
        let dx = (self.x - other.x).abs() as f32;
        let dy = (self.y - other.y).abs() as f32;
        let dz = (self.z - other.z).abs() as f32;
        let mut sorted = [dx, dy, dz];
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
        // smallest + (sqrt(2)-1)*middle + (sqrt(3)-sqrt(2))*largest... simplified:
        sorted[2]
            + (std::f32::consts::SQRT_2 - 1.0) * sorted[1]
            + (3.0f32.sqrt() - std::f32::consts::SQRT_2) * sorted[0]
    }
}

/// A 3D navigation volume (voxel grid).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavVolume {
    width: usize,
    height: usize,
    depth: usize,
    cell_size: f32,
    /// Bit-packed navigability (1 = navigable).
    navigable: Vec<u64>,
    /// Per-cell cost multiplier.
    costs: Vec<f32>,
}

impl NavVolume {
    /// Create a new volume where all cells are navigable.
    #[must_use]
    pub fn new(width: usize, height: usize, depth: usize, cell_size: f32) -> Self {
        let cell_size = cell_size.max(f32::EPSILON);
        let len = width * height * depth;
        let bitset_len = len.div_ceil(64);
        Self {
            width,
            height,
            depth,
            cell_size,
            navigable: vec![u64::MAX; bitset_len],
            costs: vec![1.0; len],
        }
    }

    #[must_use]
    pub fn width(&self) -> usize {
        self.width
    }
    #[must_use]
    pub fn height(&self) -> usize {
        self.height
    }
    #[must_use]
    pub fn depth(&self) -> usize {
        self.depth
    }
    #[must_use]
    pub fn cell_size(&self) -> f32 {
        self.cell_size
    }

    /// Set whether a cell is navigable.
    pub fn set_navigable(&mut self, x: i32, y: i32, z: i32, nav: bool) {
        if let Some(idx) = self.index(x, y, z) {
            let word = idx / 64;
            let bit = idx % 64;
            if nav {
                self.navigable[word] |= 1u64 << bit;
            } else {
                self.navigable[word] &= !(1u64 << bit);
            }
        }
    }

    /// Check if a cell is navigable.
    #[inline]
    #[must_use]
    pub fn is_navigable(&self, x: i32, y: i32, z: i32) -> bool {
        self.index(x, y, z)
            .map(|idx| (self.navigable[idx / 64] >> (idx % 64)) & 1 == 1)
            .unwrap_or(false)
    }

    /// Set the traversal cost for a cell.
    pub fn set_cost(&mut self, x: i32, y: i32, z: i32, cost: f32) {
        if let Some(idx) = self.index(x, y, z) {
            self.costs[idx] = cost;
        }
    }

    /// Get the traversal cost of a cell.
    #[must_use]
    pub fn cost(&self, x: i32, y: i32, z: i32) -> f32 {
        self.index(x, y, z)
            .map(|i| self.costs[i])
            .unwrap_or(f32::INFINITY)
    }

    /// Convert voxel position to world position (center of cell).
    #[must_use]
    pub fn voxel_to_world(&self, pos: VoxelPos) -> Vec3 {
        Vec3::new(
            (pos.x as f32 + 0.5) * self.cell_size,
            (pos.y as f32 + 0.5) * self.cell_size,
            (pos.z as f32 + 0.5) * self.cell_size,
        )
    }

    /// Convert world position to voxel position.
    #[must_use]
    pub fn world_to_voxel(&self, world: Vec3) -> VoxelPos {
        VoxelPos::new(
            (world.x / self.cell_size).floor() as i32,
            (world.y / self.cell_size).floor() as i32,
            (world.z / self.cell_size).floor() as i32,
        )
    }

    /// Find a path using 3D A*.
    ///
    /// Supports 26-connected neighbors (cardinal + diagonal + vertical).
    #[cfg_attr(feature = "logging", instrument(skip(self)))]
    #[must_use]
    pub fn find_path(&self, start: VoxelPos, goal: VoxelPos) -> Option<Vec<VoxelPos>> {
        if !self.is_navigable(start.x, start.y, start.z)
            || !self.is_navigable(goal.x, goal.y, goal.z)
        {
            return None;
        }
        if start == goal {
            return Some(vec![start]);
        }

        let len = self.width * self.height * self.depth;
        let mut g_score = vec![f32::INFINITY; len];
        let mut came_from: Vec<Option<usize>> = vec![None; len];
        let mut closed = vec![false; len];
        let mut open = BinaryHeap::new();

        let start_idx = self.index(start.x, start.y, start.z)?;
        let goal_idx = self.index(goal.x, goal.y, goal.z)?;

        g_score[start_idx] = 0.0;
        open.push(VoxelNode {
            idx: start_idx,
            f_score: start.distance_3d(goal),
        });

        while let Some(current) = open.pop() {
            if current.idx == goal_idx {
                return Some(self.reconstruct_path(&came_from, goal_idx));
            }
            if closed[current.idx] {
                continue;
            }
            closed[current.idx] = true;

            let cx = (current.idx % self.width) as i32;
            let cy = ((current.idx / self.width) % self.height) as i32;
            let cz = (current.idx / (self.width * self.height)) as i32;

            // 26-connected neighbors
            for dz in -1..=1i32 {
                for dy in -1..=1i32 {
                    for dx in -1..=1i32 {
                        if dx == 0 && dy == 0 && dz == 0 {
                            continue;
                        }
                        let nx = cx + dx;
                        let ny = cy + dy;
                        let nz = cz + dz;
                        if !self.is_navigable(nx, ny, nz) {
                            continue;
                        }

                        let n_idx = match self.index(nx, ny, nz) {
                            Some(i) => i,
                            None => continue,
                        };
                        if closed[n_idx] {
                            continue;
                        }

                        let move_dist = match dx.abs() + dy.abs() + dz.abs() {
                            1 => 1.0,
                            2 => std::f32::consts::SQRT_2,
                            _ => 3.0f32.sqrt(), // 3D diagonal
                        };
                        let tentative_g = g_score[current.idx] + move_dist * self.costs[n_idx];

                        if tentative_g < g_score[n_idx] {
                            came_from[n_idx] = Some(current.idx);
                            g_score[n_idx] = tentative_g;
                            let np = VoxelPos::new(nx, ny, nz);
                            let h = np.distance_3d(goal);
                            open.push(VoxelNode {
                                idx: n_idx,
                                f_score: tentative_g + h,
                            });
                        }
                    }
                }
            }
        }

        None
    }

    #[inline]
    fn index(&self, x: i32, y: i32, z: i32) -> Option<usize> {
        if x >= 0
            && y >= 0
            && z >= 0
            && (x as usize) < self.width
            && (y as usize) < self.height
            && (z as usize) < self.depth
        {
            Some(z as usize * self.width * self.height + y as usize * self.width + x as usize)
        } else {
            None
        }
    }

    fn reconstruct_path(&self, came_from: &[Option<usize>], goal_idx: usize) -> Vec<VoxelPos> {
        let mut path = Vec::new();
        let mut current = goal_idx;
        let plane = self.width * self.height;
        loop {
            let x = (current % self.width) as i32;
            let y = ((current / self.width) % self.height) as i32;
            let z = (current / plane) as i32;
            path.push(VoxelPos::new(x, y, z));
            match came_from[current] {
                Some(p) => current = p,
                None => break,
            }
        }
        path.reverse();
        path
    }
}

#[derive(Clone, Copy)]
struct VoxelNode {
    idx: usize,
    f_score: f32,
}

impl PartialEq for VoxelNode {
    fn eq(&self, o: &Self) -> bool {
        self.f_score == o.f_score
    }
}

impl Eq for VoxelNode {}

impl PartialOrd for VoxelNode {
    fn partial_cmp(&self, o: &Self) -> Option<Ordering> {
        Some(self.cmp(o))
    }
}

impl Ord for VoxelNode {
    fn cmp(&self, o: &Self) -> Ordering {
        o.f_score
            .partial_cmp(&self.f_score)
            .unwrap_or(Ordering::Equal)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_3d_path() {
        let vol = NavVolume::new(10, 10, 10, 1.0);
        let path = vol.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(5, 5, 5));
        assert!(path.is_some());
        let path = path.unwrap();
        assert_eq!(*path.first().unwrap(), VoxelPos::new(0, 0, 0));
        assert_eq!(*path.last().unwrap(), VoxelPos::new(5, 5, 5));
    }

    #[test]
    fn same_start_goal() {
        let vol = NavVolume::new(5, 5, 5, 1.0);
        let path = vol.find_path(VoxelPos::new(2, 2, 2), VoxelPos::new(2, 2, 2));
        assert_eq!(path, Some(vec![VoxelPos::new(2, 2, 2)]));
    }

    #[test]
    fn blocked_path() {
        let mut vol = NavVolume::new(3, 1, 1, 1.0);
        // Block the middle cell
        vol.set_navigable(1, 0, 0, false);
        let path = vol.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(2, 0, 0));
        // 1D grid with blocked middle: no path
        assert!(path.is_none());
    }

    #[test]
    fn vertical_movement() {
        let vol = NavVolume::new(1, 1, 5, 1.0);
        let path = vol.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(0, 0, 4));
        assert!(path.is_some());
        let path = path.unwrap();
        assert_eq!(path.len(), 5); // straight line through z
        for (i, pos) in path.iter().enumerate() {
            assert_eq!(pos.x, 0);
            assert_eq!(pos.y, 0);
            assert_eq!(pos.z, i as i32);
        }
    }

    #[test]
    fn not_navigable_start_goal() {
        let mut vol = NavVolume::new(5, 5, 5, 1.0);
        vol.set_navigable(0, 0, 0, false);
        // Start not navigable
        assert!(
            vol.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(4, 4, 4))
                .is_none()
        );

        let mut vol2 = NavVolume::new(5, 5, 5, 1.0);
        vol2.set_navigable(4, 4, 4, false);
        // Goal not navigable
        assert!(
            vol2.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(4, 4, 4))
                .is_none()
        );
    }

    #[test]
    fn world_voxel_conversion_roundtrip() {
        let vol = NavVolume::new(10, 10, 10, 2.0);
        let pos = VoxelPos::new(3, 5, 7);
        let world = vol.voxel_to_world(pos);
        let back = vol.world_to_voxel(world);
        assert_eq!(back, pos);
    }

    #[test]
    fn serde_roundtrip() {
        let mut vol = NavVolume::new(4, 4, 4, 0.5);
        vol.set_navigable(1, 1, 1, false);
        vol.set_cost(2, 2, 2, 3.0);

        let json = serde_json::to_string(&vol).unwrap();
        let deserialized: NavVolume = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.width(), 4);
        assert_eq!(deserialized.height(), 4);
        assert_eq!(deserialized.depth(), 4);
        assert!(!deserialized.is_navigable(1, 1, 1));
        assert!(deserialized.is_navigable(0, 0, 0));
        assert!((deserialized.cost(2, 2, 2) - 3.0).abs() < f32::EPSILON);
    }

    #[test]
    fn voxel_1x1x1() {
        let vol = NavVolume::new(1, 1, 1, 1.0);
        let path = vol.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(0, 0, 0));
        assert_eq!(path, Some(vec![VoxelPos::new(0, 0, 0)]));
    }

    #[test]
    fn voxel_cost_aware() {
        let mut vol = NavVolume::new(10, 1, 10, 1.0);
        // Make a wall of expensive cells at x=5, except leave z=0 cheap
        for z in 1..10 {
            vol.set_cost(5, 0, z, 1000.0);
        }
        // Path from (0,0,5) to (9,0,5) should go around via z=0
        let path = vol.find_path(VoxelPos::new(0, 0, 5), VoxelPos::new(9, 0, 5));
        assert!(path.is_some());
        let path = path.unwrap();
        // Cells at x=5 with z>0 should be avoided (only x=5,z=0 is cheap)
        for p in &path {
            if p.x == 5 {
                assert!(
                    p.z == 0,
                    "Path should avoid expensive cells at x=5, z>0, but went through ({},{},{})",
                    p.x,
                    p.y,
                    p.z
                );
            }
        }
    }

    #[test]
    fn voxel_all_blocked() {
        let mut vol = NavVolume::new(3, 3, 3, 1.0);
        // Block everything except start and goal
        for z in 0..3 {
            for y in 0..3 {
                for x in 0..3 {
                    let is_start = x == 0 && y == 0 && z == 0;
                    let is_goal = x == 2 && y == 2 && z == 2;
                    if !is_start && !is_goal {
                        vol.set_navigable(x, y, z, false);
                    }
                }
            }
        }
        // No path possible (no connected neighbors)
        assert!(
            vol.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(2, 2, 2))
                .is_none()
        );
    }

    #[test]
    fn voxel_large_volume() {
        let vol = NavVolume::new(20, 20, 20, 1.0);
        let path = vol.find_path(VoxelPos::new(0, 0, 0), VoxelPos::new(19, 19, 19));
        assert!(path.is_some());
        let path = path.unwrap();
        assert_eq!(*path.first().unwrap(), VoxelPos::new(0, 0, 0));
        assert_eq!(*path.last().unwrap(), VoxelPos::new(19, 19, 19));
    }

    #[test]
    fn voxel_cell_size_clamped() {
        // cell_size should be clamped to positive
        let vol = NavVolume::new(5, 5, 5, 0.0);
        // Should not panic on coordinate conversion
        let _pos = vol.world_to_voxel(Vec3::new(1.0, 1.0, 1.0));
    }
}