jps 1.1.1

Jump Point Search Implementation for Path Finding.
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use super::base::{JPS3DNeib, State};
// use float_ord::{sort, FloatOrd};
use im::Vector;
use itertools::Itertools;
use ordered_float::OrderedFloat;
use priority_queue::PriorityQueue;
use std::cmp::Reverse;
use std::collections::{HashMap, HashSet};

#[derive(Debug, Default)]
/// `GraphSearch` is the struct type for path finding.
///
/// `pq_` :  a Priorty Queue for saving possible nodes.
///
/// `hm_` :  a HashMap for saving nodes that have been visited.
///
/// `path_`: the path in the forward order, i.e. from start to goal.
///
/// `turnings` : the turning points along the path in the forward order, including start and goal.
/// `turnings` can be differenced for constructing lines, e.g.  line[k] := (turnings[k], turnings[k+1])
///
///  `ns_` : all neighbors in terms of relative position.
///
///  `jn3d_`: all jps neighbors.
///
///  `fmap_`: HashSet containing all free grids.
///
///  `omap` : HashSet containing all occupied grids.
///
pub struct GraphSearch {
    pub(crate) pq_: PriorityQueue<State, Reverse<OrderedFloat<f32>>>,

    pub(crate) hm_: HashMap<(i32, i32, i32), State>,

    /// the path in the forward order, i.e. from start to goal.
    pub path_: Vec<State>,

    /// the turning points along the path in the forward order, including start and goal.
    ///
    /// `turnings` can be differenced for constructing lines, e.g.  line[k] := (turnings[k], turnings[k+1])
    pub turnings: Vec<(i32, i32, i32)>,

    pub(crate) ns_: Vector<(i32, i32, i32)>,
    pub(crate) jn3d_: JPS3DNeib,

    /// free map. if it's `None`, a grid will be free as long as it is within the box defined by xdim × ydim × zdim.
    ///
    /// otherwise, when it's `Some`, the check for being contained in the free map is additionally needed to check whether a grid is free.
    pub fmap_: Option<HashSet<(i32, i32, i32)>>,
    /// occupied map
    pub omap_: HashSet<(i32, i32, i32)>,
    /// x-, y-, z- dimensions
    pub xbound: [i32; 2],
    pub ybound: [i32; 2],
    pub zbound: [i32; 2],

    /// a scalar in calculating the costs
    pub(crate) eps_: f32,

    /// goal
    pub goal: (i32, i32, i32),

    /// use_jps
    pub use_jps_: bool,
}
impl GraphSearch {
    /// create a `GraphSearch` instance, with maps given in terms of HashMap.
    ///
    /// `fmap_` : free map, containing all coordinates of free grids.
    ///
    /// `omap_` : occupied map, containing all coordinates of occupied grids.
    ///
    /// `xdim`  : upper boundary on x-axis.
    ///
    /// `ydim`  : upper boundary on y-axis.
    ///
    /// `zdim`  : upper boundary on z-axis.
    ///
    /// `eps_`  : a scalar in calculating the costs.
    ///
    pub fn new_v1(
        fmap_: Option<HashSet<(i32, i32, i32)>>,
        omap_: HashSet<(i32, i32, i32)>,
        xbound: [i32; 2],
        ybound: [i32; 2],
        zbound: [i32; 2],
        eps_: f32,
        // goal: (i32, i32, i32),
        // use_jps_: bool,
    ) -> Self {
        // Set 3D neighbors
        let mut ns: Vector<(i32, i32, i32)> = Vector::new();
        for x in -1..2 {
            for y in -1..2 {
                for z in -1..2 {
                    if x == 0 && y == 0 && z == 0 {
                        continue;
                    }
                    ns.push_back((x, y, z));
                }
            }
        }

        Self {
            pq_: PriorityQueue::default(),
            hm_: HashMap::default(),
            path_: Vec::default(),
            turnings: Vec::default(),
            ns_: ns,
            jn3d_: JPS3DNeib::default(),
            fmap_: fmap_,
            omap_: omap_,
            xbound: xbound,
            ybound: ybound,
            zbound: zbound,
            eps_: eps_,
            ..Default::default() // goal: goal,
                                 // use_jps_: use_jps_,
        }
    }

    ///  create a `GraphSearch` instance, with the map given in terms of binary-valued array.
    ///
    /// `map`   : the map that is reshaped into 1d array, `true` means occupied, `false` means free.
    ///
    /// `xdim`  : upper boundary on x-axis.
    ///
    /// `ydim`  : upper boundary on y-axis.
    ///
    /// `zdim`  : upper boundary on z-axis.
    ///
    /// `eps_`  : a scalar in calculating the costs.
    ///
    pub fn new_v2(
        map: &[bool],
        xbound: [i32; 2],
        ybound: [i32; 2],
        zbound: [i32; 2],
        eps_: f32,
        // goal: (i32, i32, i32),
        // use_jps_: bool,
    ) -> Self {
        let mut occ_set: HashSet<(i32, i32, i32)> = HashSet::new();
        let mut free_set: HashSet<(i32, i32, i32)> = HashSet::new();

        for (b, idx) in map.iter().enumerate() {
            let z = b as i32 / 25;
            let y = (b as i32 - 25 * z) / 5;
            let x = b as i32 - 25 * z - 5 * y;
            if *idx {
                // occupied coordinates
                occ_set.insert((x, y, z));
            } else {
                // free coordinates
                free_set.insert((x, y, z));
            }
        }
        Self::new_v1(Some(free_set), occ_set, xbound, ybound, zbound, eps_)
    }
}

impl GraphSearch {
    #[allow(unused)]
    pub(crate) fn recover_path(
        &self,
        node: &State,
        start_id: (i32, i32, i32),
    ) -> (Vec<State>, Vec<(i32, i32, i32)>) {
        let mut path: Vec<State> = Vec::new();
        let mut turnings: Vec<(i32, i32, i32)> = Vec::new();
        let mut node_ref: Option<&State> = Some(node);
        while let Some(nd) = node_ref {
            path.push(nd.clone());
            if nd.id == start_id {
                break;
            } else {
                if let Some(par_id) = nd.parent_id {
                    node_ref = self.hm_.get(&par_id);
                }
            }
        }
        path.reverse();
        let len = path.len();
        let mut pre_dir: (i32, i32, i32) = (0, 0, 0);
        for (k, (pre, cur)) in path.iter().tuple_windows().enumerate() {
            // push goal
            if k == 0 {
                turnings.push(pre.id);
                pre_dir = (
                    cur.id.0 - pre.id.0,
                    cur.id.1 - pre.id.1,
                    cur.id.2 - pre.id.2,
                );
            } else {
                // push turnings
                let dir = (
                    cur.id.0 - pre.id.0,
                    cur.id.1 - pre.id.1,
                    cur.id.2 - pre.id.2,
                );

                if dir != pre_dir && k < len - 2 {
                    turnings.push(pre.id);
                    pre_dir = dir;
                }
            }
            // push start
            if k == len - 2 {
                turnings.push(cur.id);
            }
        }
        (path, turnings)
    }

    #[allow(unused)]
    /// the main entry for planning.
    ///
    /// `start` : starting grid
    ///
    /// `goal`  : goal grid
    ///
    /// `usejps`: `true` for using jps, `false` for using `A*`
    ///
    /// `max_expand` : max number of the depth in searching.  
    ///
    pub fn plan(
        &mut self,
        start: (i32, i32, i32),
        goal: (i32, i32, i32),
        usejps: bool,
        max_expand: i32,
    ) -> bool {
        self.pq_.clear();
        self.path_.clear();
        // Set jps
        self.use_jps_ = usejps;
        // Set goal
        self.goal = goal;
        // Set start node
        let mut current_node = State::new(start, start.0, start.1, start.2, 0, 0, 0);
        current_node.g = OrderedFloat(0.0);
        current_node.h = OrderedFloat(self.get_heur(&start));

        return self.plan_body(&mut current_node, max_expand, start, goal);
    }

    pub(crate) fn plan_body(
        &mut self,
        mut curr_node: &mut State,
        max_expand: i32,
        start_id: (i32, i32, i32),
        goal_id: (i32, i32, i32),
    ) -> bool {
        // Insert start node
        self.pq_
            .push(curr_node.clone(), Reverse(curr_node.g + curr_node.h));
        curr_node.opened = true;
        self.hm_.insert(start_id, curr_node.clone());
        let mut expand_iteration = 0;
        let mut curr_node: State;
        loop {
            expand_iteration += 1;
            // get element with smallest cost
            curr_node = self.pq_.pop().unwrap().0;
            curr_node.closed = true; // Add to closed list
            if curr_node.id == goal_id {
                // println!("Goal Reached!!!!!!");
                break;
            }
            let mut succ_ids: Vector<(i32, i32, i32)> = Vector::new();
            let mut succ_costs: Vector<f32> = Vector::new();
            // Get successors
            if !self.use_jps_ {
                self.get_succ(&curr_node, &mut succ_ids, &mut succ_costs);
            } else {
                self.get_jps_succ(&curr_node, &mut succ_ids, &mut succ_costs);
            }
            for s in 0..succ_ids.len() {
                // see if we can improve the value of succstate
                if let Some(child_ptr) = self.hm_.get_mut(&succ_ids[s]) {
                    // println!("child_ptr = {:?}", child_ptr);
                    let tentative_gval = curr_node.g + succ_costs[s];
                    if tentative_gval < child_ptr.g {
                        (*child_ptr).parent_id = Some(curr_node.id); // Assign new parent
                        (*child_ptr).g = tentative_gval; // Update gval

                        // if currently in OPEN, update
                        if child_ptr.opened && !child_ptr.closed {
                            child_ptr.dx = child_ptr.x - curr_node.x;
                            child_ptr.dy = child_ptr.y - curr_node.y;
                            child_ptr.dz = child_ptr.z - curr_node.z;
                            if child_ptr.dx != 0 {
                                child_ptr.dx /= child_ptr.dx.abs();
                            }
                            if child_ptr.dy != 0 {
                                child_ptr.dy /= child_ptr.dy.abs();
                            }
                            if child_ptr.dz != 0 {
                                child_ptr.dz /= child_ptr.dz.abs();
                            }
                            self.pq_
                                .push_increase(*child_ptr, Reverse(child_ptr.g + child_ptr.h));
                        }
                        // if currently in CLOSED
                        else if child_ptr.opened && child_ptr.closed {
                            println!("ASTAR ERROR!");
                        }
                        // new node, add to heap
                        else {
                            child_ptr.opened = true;
                            self.pq_
                                .push(child_ptr.clone(), Reverse(child_ptr.g + child_ptr.h));
                        }
                    }
                }
            }
            // println!("pq_ = {:?}", self.pq_);
            if max_expand > 0 && expand_iteration >= max_expand {
                // println!("MaxExpandStep {} Reached!!!!!!", max_expand);
                return false;
            }
            if self.pq_.is_empty() {
                // println!("Priority queue is empty!!!!!!");
                return false;
            }
        }
        // println!("goal g: {}, h: {}!", curr_node.g, curr_node.h);
        // println!("Expand {} nodes!", expand_iteration);

        // let curr_node = self.hm_.get(&curr_node.id).unwrap();

        (self.path_, self.turnings) = self.recover_path(&curr_node, start_id);
        true
    }

    pub(crate) fn get_succ(
        &mut self,
        curr: &State,
        succ_ids: &mut Vector<(i32, i32, i32)>,
        succ_costs: &mut Vector<f32>,
    ) {
        for d in self.ns_.iter() {
            let new_pos = (curr.x + d.0, curr.y + d.1, curr.z + d.2);
            if !self.is_free(&new_pos) {
                continue;
            }
            if !self.hm_.contains_key(&new_pos) {
                let mut new_state =
                    State::new(new_pos, new_pos.0, new_pos.1, new_pos.2, d.0, d.1, d.2);
                new_state.h = OrderedFloat(self.get_heur(&new_pos));
                self.hm_.insert(new_pos, new_state);
            }
            succ_ids.push_back(new_pos);
            succ_costs.push_back(((d.0.pow(2) + d.1.pow(2) + d.2.pow(2)) as f32).sqrt());
        }
    }

    pub(crate) fn get_jps_succ(
        &mut self,
        curr: &State,
        succ_ids: &mut Vector<(i32, i32, i32)>,
        succ_costs: &mut Vector<f32>,
    ) {
        let norm1 = curr.dx.abs() + curr.dy.abs() + curr.dz.abs();
        let num_neib = JPS3DNeib::nsz[norm1 as usize][0] as usize;
        let num_fneib = JPS3DNeib::nsz[norm1 as usize][1] as usize;
        let id = ((curr.dx + 1) + 3 * (curr.dy + 1) + 9 * (curr.dz + 1)) as usize;

        for dev in 0..num_neib + num_fneib {
            let (mut new_x, mut new_y, mut new_z) = (0, 0, 0);
            let (dx, dy, dz);
            if dev < num_neib {
                dx = self.jn3d_.ns[id][0][dev];
                dy = self.jn3d_.ns[id][1][dev];
                dz = self.jn3d_.ns[id][2][dev];
                if !self.jump(
                    curr.x, curr.y, curr.z, dx, dy, dz, &mut new_x, &mut new_y, &mut new_z,
                ) {
                    continue;
                }
            } else {
                let nx = curr.x + self.jn3d_.f1[id][0][dev - num_neib];
                let ny = curr.y + self.jn3d_.f1[id][1][dev - num_neib];
                let nz = curr.z + self.jn3d_.f1[id][2][dev - num_neib];
                if self.is_occupied(&(nx, ny, nz)) {
                    dx = self.jn3d_.f2[id][0][dev - num_neib];
                    dy = self.jn3d_.f2[id][1][dev - num_neib];
                    dz = self.jn3d_.f2[id][2][dev - num_neib];
                    if !self.jump(
                        curr.x, curr.y, curr.z, dx, dy, dz, &mut new_x, &mut new_y, &mut new_z,
                    ) {
                        continue;
                    }
                } else {
                    continue;
                }
            }
            let new_pos = (new_x, new_y, new_z);
            if !self.hm_.contains_key(&new_pos) {
                let mut new_state =
                    State::new(new_pos, new_pos.0, new_pos.1, new_pos.2, dx, dy, dz);
                new_state.h = OrderedFloat(self.get_heur(&new_pos));
                self.hm_.insert(new_pos, new_state);
            }
            succ_ids.push_back(new_pos);
            succ_costs.push_back(
                (((new_x - curr.x).pow(2) + (new_y - curr.y).pow(2) + (new_z - curr.z).pow(2))
                    as f32)
                    .sqrt(),
            );
            // println!(
            //     "num_neib = {}, num_fneib = {}, succ_ids = {:?}",
            //     num_neib, num_fneib, succ_ids
            // );
        }
    }

    pub(crate) fn jump(
        &self,
        x: i32,
        y: i32,
        z: i32,
        dx: i32,
        dy: i32,
        dz: i32,
        new_x: &mut i32,
        new_y: &mut i32,
        new_z: &mut i32,
    ) -> bool {
        *new_x = x + dx;
        *new_y = y + dy;
        *new_z = z + dz;
        if !self.is_free(&(*new_x, *new_y, *new_z)) {
            return false;
        }
        if (*new_x, *new_y, *new_z) == self.goal {
            return true;
        }
        if self.has_forced(*new_x, *new_y, *new_z, dx, dy, dz) {
            return true;
        }
        let id = ((dx + 1) + 3 * (dy + 1) + 9 * (dz + 1)) as usize;
        let norm1 = dx.abs() + dy.abs() + dz.abs();
        let num_neib = JPS3DNeib::nsz[norm1 as usize][0];
        for k in 0..num_neib as usize - 1 {
            let (mut new_new_x, mut new_new_y, mut new_new_z) = (0, 0, 0);
            if self.jump(
                *new_x,
                *new_y,
                *new_z,
                self.jn3d_.ns[id][0][k],
                self.jn3d_.ns[id][1][k],
                self.jn3d_.ns[id][2][k],
                &mut new_new_x,
                &mut new_new_y,
                &mut new_new_z,
            ) {
                return true;
            }
        }
        return self.jump(*new_x, *new_y, *new_z, dx, dy, dz, new_x, new_y, new_z);
    }

    #[inline]
    pub(crate) fn has_forced(&self, x: i32, y: i32, z: i32, dx: i32, dy: i32, dz: i32) -> bool {
        let norm1 = dx.abs() + dy.abs() + dz.abs();
        let id = ((dx + 1) + 3 * (dy + 1) + 9 * (dz + 1)) as usize;
        match norm1 {
            // 1-d move, check 8 neighbors
            1 => {
                for fk in 0..8_usize {
                    let nx = x + self.jn3d_.f1[id][0][fk];
                    let ny = y + self.jn3d_.f1[id][1][fk];
                    let nz = z + self.jn3d_.f1[id][2][fk];
                    if self.is_occupied(&(nx, ny, nz)) {
                        return true;
                    }
                }
                return false;
            }
            // 2-d move, check 8 neighbors
            2 => {
                for fk in 0..8_usize {
                    let nx = x + self.jn3d_.f1[id][0][fk];
                    let ny = y + self.jn3d_.f1[id][1][fk];
                    let nz = z + self.jn3d_.f1[id][2][fk];
                    if self.is_occupied(&(nx, ny, nz)) {
                        return true;
                    }
                }
                return false;
            }
            // 3-d move, check 6 neighbors
            3 => {
                for fk in 0..6_usize {
                    let nx = x + self.jn3d_.f1[id][0][fk];
                    let ny = y + self.jn3d_.f1[id][1][fk];
                    let nz = z + self.jn3d_.f1[id][2][fk];
                    if self.is_occupied(&(nx, ny, nz)) {
                        return true;
                    }
                }
                return false;
            }
            _ => {
                return false;
            }
        }
    }

    #[inline]
    pub(crate) fn is_free(&self, pos: &(i32, i32, i32)) -> bool {
        let is_inside = || {
            pos.0 >= self.xbound[0]
                && pos.0 < self.xbound[1]
                && pos.1 >= self.ybound[0]
                && pos.1 < self.ybound[1]
                && pos.2 >= self.zbound[0]
                && pos.2 < self.zbound[1]
        };
        match self.fmap_.as_ref() {
            Some(fmap) => fmap.contains(&pos) && is_inside(),
            None => is_inside() && !self.is_occupied(pos),
        }
    }

    #[inline]
    pub(crate) fn is_occupied(&self, pos: &(i32, i32, i32)) -> bool {
        pos.0 >= self.xbound[0]
            && pos.0 < self.xbound[1]
            && pos.1 >= self.ybound[0]
            && pos.1 < self.ybound[1]
            && pos.2 >= self.zbound[0]
            && pos.2 < self.zbound[1]
            && self.omap_.contains(&pos)
    }

    #[inline]
    pub(crate) fn get_heur(&self, pos: &(i32, i32, i32)) -> f32 {
        self.eps_
            * (((pos.0 - self.goal.0).pow(2)
                + (pos.1 - self.goal.1).pow(2)
                + (pos.2 - self.goal.2).pow(2)) as f32)
                .sqrt()
    }
}