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
#[cfg(feature = "decomp")]
#[allow(unused)]
mod tests {

    use yakf::{kf::One2OneMapSO3, lie::so3::SO3, linalg::Const};
    pub fn make_obstacles_in_cuboid(
        center: Vec3,
        a: f64,
        b: f64,
        c: f64,
        interval: f64,
        attitude: Grp3,
    ) -> Vec<Vec3> {
        let mut opoints: Vec<Vec3> = Vec::new();
        let step_x = (a / interval).floor() as i32;
        let step_y = (b / interval).floor() as i32;
        let step_z = (c / interval).floor() as i32;
        for i in -step_x..step_x + 1 as i32 {
            let x = -a + i as f64 * interval;
            for j in -step_y..step_y + 1 as i32 {
                let y = -b + j as f64 * interval;
                for k in -step_z..step_z + 1 as i32 {
                    let z = -c + k as f64 * interval;
                    // let p_b = Vec3::new(x, y, z);
                    // let p_w = attitude.transpose() * p_b + center;
                    // opoints.push(p_w)
                    let tmp = i.abs() + j.abs() + k.abs();
                    if tmp >= step_x || tmp >= step_y || tmp >= step_z {
                        let p_b = Vec3::new(x, y, z);
                        let p_w = attitude.transpose() * p_b + center;
                        opoints.push(p_w)
                    }
                }
            }
        }
        opoints
    }

    pub fn make_obstacles_in_cylinder(
        center: Vec3,
        r: f64,
        h: f64,
        interval: f64,
        attitude: Grp3,
    ) -> Vec<Vec3> {
        let mut opoints: Vec<Vec3> = Vec::new();

        let steps_r = (2.0 * PI * r / interval * 1.05).floor() as i32;
        let da = 2.0 * PI / steps_r as f64;
        let steps_h = (h / interval).floor() as i32;
        for i in 0..steps_r {
            let x = (da * i as f64).cos() * r;
            let y = (da * i as f64).sin() * r;
            for j in -steps_h..steps_h {
                let z = j as f64 * interval;

                let p_b = Vec3::new(x, y, z);
                let p_w = attitude.transpose() * p_b + center;
                opoints.push(p_w)
            }
        }
        opoints
    }

    #[test]
    pub fn test_map() {
        use crate::base::JPS3DNeib;
        use crate::graphsearch::GraphSearch;
        use std::collections::{HashMap, HashSet};
        let opoints = generate_simple_world_map(24.0, 24.0, 4.0);
        println!("opoints contains {} points", opoints.len());
        use std::fs::File;
        use std::io::Write;
        let mut w = File::create("opoints.txt").unwrap();
        for p in opoints.iter() {
            let s = format!("{},{},{}", p[0], p[1], p[2]);
            writeln!(&mut w, "{}", s).unwrap();
        }

        // Voxelization
        use crate::decomp_tool::{Decomp, Voxelable};
        use rand::prelude::*;
        #[derive(Debug, Hash, Clone, Copy)]
        pub struct Transformer {}
        impl Voxelable for Transformer {
            fn coordinate_to_grid(&self, pos: &Vec3) -> (i32, i32, i32) {
                (
                    (pos[0] / 0.45).round() as i32,
                    (pos[1] / 0.45).round() as i32,
                    (pos[2] / 0.45).round() as i32,
                )
            }
            fn grid_to_coordinate(&self, grid: &(i32, i32, i32)) -> Vec3 {
                Vec3::new(
                    grid.0 as f64 * 0.45,
                    grid.1 as f64 * 0.45,
                    grid.2 as f64 * 0.45,
                )
            }
            fn get_grid_size(&self) -> (f64, f64, f64) {
                (0.45, 0.45, 0.45)
            }
        }
        let a_max: f64 = 5.0;
        let v_max: f64 = 3.0;
        let r_robot: f64 = 0.2;
        let r_s = 0.5 * v_max.powi(2) / a_max;
        let trans = Transformer {};

        let mut occ_container_map: HashMap<(i32, i32, i32), Vec<&Vec3>> = HashMap::new();
        let mut occ_set: HashSet<(i32, i32, i32)> = HashSet::new();
        for p in opoints.iter() {
            let grid = trans.coordinate_to_grid(p);
            if occ_container_map.contains_key(&grid) {
                let v = occ_container_map.get_mut(&grid).unwrap();
                v.push(p);
            } else {
                occ_container_map.insert(grid, vec![p]);
            }
            let _ = occ_set.insert(grid);
        }

        // set the start and goal
        let mut grid_start = (0, 0, 0);
        let mut grid_goal = (0, 0, 0);
        'loop1: for x in -8..5 {
            for y in -5..5 {
                for z in -3..3 {
                    if !occ_set.contains(&(x, y, z)) {
                        grid_start = (x, y, z);
                        break 'loop1;
                    }
                }
            }
        }

        'loop2: for x in 50..60 {
            for y in 52..60 {
                for z in -1..1 {
                    if !occ_set.contains(&(x, y, z)) {
                        grid_goal = (x, y, z);
                        break 'loop2;
                    }
                }
            }
        }

        let mut start = trans.grid_to_coordinate(&grid_start);
        let mut goal = trans.grid_to_coordinate(&grid_goal);

        // use JPS the find line segments
        let mut graphsearch =
            GraphSearch::new_v1(None, occ_set, [-10, 80], [-10, 80], [-10, 80], 1.0);

        let t1 = std::time::Instant::now();
        if graphsearch.plan(grid_start, grid_goal, true, 5000) {
            let t2 = std::time::Instant::now();
            println!("path finding cost {} us", (t2 - t1).as_secs_f64() * 1e6);
            let path = &graphsearch.path_;
            let turnings = &graphsearch.turnings;
            let fmt_path = path.iter().enumerate().map(|(i, x)| {
                if i > 0 {
                    format!("âž¡ {:?}", x.id)
                } else {
                    format!("{:?} ", x.id)
                }
            });
            println!("path is :");
            for node in fmt_path {
                print!("{}", node);
            }
            let mut w = File::create("turnings.txt").unwrap();
            println!("");
            println!("turnings are :");
            for t in turnings.iter() {
                print!("{:?} ", t);
                let t_coord = trans.grid_to_coordinate(t);
                let s = format!("{},{},{}", t_coord[0], t_coord[1], t_coord[2]);
                writeln!(&mut w, "{}", s).unwrap();
            }
            println!("");

            // decomposition
            let mut decomps: Vec<Decomp<Transformer>> = Vec::new();
            let mut ellipsoids: Vec<Ellipsoid> = Vec::new();

            let t1 = std::time::Instant::now();
            for k in 1..turnings.len() {
                let grid_end_1 = &turnings[k - 1];
                let grid_end_2 = &turnings[k];
                let mut decomp = Decomp::init(grid_end_1, grid_end_2, trans, r_s, r_robot);
                let grids_to_check = decomp.find_grids_to_check(&graphsearch.omap_);
                let mut local_opoints: Vec<Vec3> = Vec::new();
                for g in grids_to_check.iter() {
                    let v = occ_container_map.get(g).unwrap();
                    for p in v.iter() {
                        local_opoints.push(*p.clone());
                    }
                }
                decomp.inflate_obstacles(&mut local_opoints, r_robot); // inflate_obstacles

                let (k1, k2) = decomp.best_fit_ellipsoid_for_occupied_points(&local_opoints);
                ellipsoids.push(decomp.ellipsoid.clone()); // need to save the ellipsoid now, because after being cut, it will be inflated.
                decomp.cut_into_polyhedron(local_opoints, k1, k2);
                decomps.push(decomp);
            }
            let t2 = std::time::Instant::now();
            println!(
                "finding ellipsoids and cutting, cost time {} us",
                (t2 - t1).as_secs_f64() * 1e6
            );

            // write all ellipsoid into txt for matlab check
            let mut w = File::create("ellipsoids.txt").unwrap();
            for (ellip, dec) in ellipsoids.iter().zip(decomps.iter()) {
                let ellip_center = ellip.center;
                let ellip_e = ellip.e.reshape_generic(Const::<9>, Const::<1>);
                let mut s = String::new();
                for i in 0..3 {
                    s += format!("{},", ellip_center[i]).as_str();
                }
                for i in 0..9 {
                    s += format!("{},", ellip_e[i]).as_str();
                }
                s += format!("{},", ellip.a).as_str();
                s += format!("{},", ellip.b).as_str();
                s += format!("{}", ellip.c).as_str();
                writeln!(&mut w, "{}", s).unwrap();
                println!(
                    "ellipsoid a,b,c = {:.3}, {:.3}, {:.3},  polyhedron has {} hyperplanes",
                    ellip.a,
                    ellip.b,
                    ellip.c,
                    dec.polyhedron.len(),
                );
                let e1 = dec.line_ends[0];
                let e2 = dec.line_ends[1];
                let ye1 = ellip.distance(&e1);
                let ye2 = ellip.distance(&e2);
                println!("ye1 = {:.4}, ye2 = {:.4}", ye1, ye2);
                if (ye1 - 1.0).abs() > 0.01 || (ye2 - 1.0).abs() > 0.01 {
                    assert!(1 == 2);
                }
            }
        } else {
            println!("path finding failed");
        }

        /// generate a world map with simple 3D shapes.
        pub fn generate_simple_world_map(length: f64, width: f64, height: f64) -> Vec<Vec3> {
            use rand::distributions::Uniform;
            let mut opoints: Vec<Vec3> = Vec::new();
            use rand::prelude::*;

            let mut x_rng = Uniform::new(0_f64, length);
            let mut y_rng = Uniform::new(0_f64, width);
            let mut z_rng = Uniform::new(0_f64, height);
            for i in 0..35 {
                let mut rng = thread_rng();
                let center = Vec3::new(
                    x_rng.sample(&mut rng),
                    y_rng.sample(&mut rng),
                    z_rng.sample(&mut rng),
                );
                let v = Vec3::new(x_rng.sample(&mut rng), rng.gen(), z_rng.sample(&mut rng));
                let attitude = SO3::from_vec(v).to_grp();
                match rng.gen_bool(0.5) {
                    true => {
                        let r = rng.gen_range(length / 25.0..length / 20.0);
                        let h = rng.gen_range(height / 20.0..height / 10.0);
                        let mut ops = make_obstacles_in_cylinder(center, r, h, 0.15, attitude);
                        opoints.append(&mut ops)
                    }
                    false => {
                        let a = rng.gen_range(length / 30.0..length / 25.0);
                        let b = rng.gen_range(width / 20.0..width / 15.0);
                        let c = rng.gen_range(height / 10.0..height / 5.0);
                        let mut ops = make_obstacles_in_cuboid(center, a, b, c, 0.15, attitude);
                        opoints.append(&mut ops)
                    }
                }
            }

            // let mut opoints: Vec<Vec3> = Vec::new();

            let steps_w = (width / 0.15 * 0.8).floor() as i32;
            let steps_h = (height / 0.15).floor() as i32;

            let x = 10.0;
            for i in -1..steps_w {
                let y = i as f64 * 0.15;
                for k in -2..steps_h {
                    let z = k as f64 * 0.15;
                    let p_b = Vec3::new(x, y, z);
                    opoints.push(p_b);
                }
            }

            opoints
        }
    }

    use std::f64::consts::PI;

    use crate::{
        decomp_tool::Ellipsoid,
        decomposition::base::decomp_tool::{
            self, find_perpendicular_direction, find_third_direction, get_attitude_from_one_vector,
            Grp3, Vec3,
        },
    };

    #[test]
    fn test_find_n2() {
        let mut n1 = Vec3::new(0.0, 5.0, -3.0);
        n1 = n1.normalize();
        let n2 = find_perpendicular_direction(&n1);
        println!("n2 = {:?}", n2.as_slice());

        assert!((n1.dot(&n2)).abs() < 1e-7);
        assert!((n2.norm() - 1.0).abs() < 1e-7);
    }
    #[test]
    fn test_find_n2n3() {
        let mut n1 = Vec3::new(0.0, 5.0, -3.0);
        n1 = n1.normalize();
        let n2 = find_perpendicular_direction(&n1);
        let n3 = find_third_direction(&n1, &n2);
        println!("n2 = {:?}", n2.as_slice());
        println!("n3 = {:?}", n3.as_slice());
        let r = get_attitude_from_one_vector(&n1);

        assert!((n1.dot(&n3)).abs() < 1e-7);
        assert!((n3.norm() - 1.0).abs() < 1e-7);
        assert!(((r.transpose() * r - Grp3::identity()).norm() < 1e-7));
    }

    /// This test also outputs some vectors for manual check
    #[cfg(feature = "decomp")]
    #[test]
    fn test_decomp_init() {
        use crate::decomp_tool::{Decomp, Voxelable};
        use rand::prelude::*;
        #[derive(Debug, Hash, Clone, Copy)]
        pub struct Transformer {}
        impl Voxelable for &Transformer {
            fn coordinate_to_grid(&self, pos: &Vec3) -> (i32, i32, i32) {
                (
                    (pos[0] / 0.55).round() as i32,
                    (pos[1] / 0.45).round() as i32,
                    (pos[2] / 0.6).round() as i32,
                )
            }
            fn grid_to_coordinate(&self, grid: &(i32, i32, i32)) -> Vec3 {
                Vec3::new(
                    grid.0 as f64 * 0.55,
                    grid.1 as f64 * 0.45,
                    grid.2 as f64 * 0.6,
                )
            }
            fn get_grid_size(&self) -> (f64, f64, f64) {
                (0.55, 0.45, 0.6)
            }
        }
        let a_max: f64 = 10.0;
        let v_max: f64 = 5.0;
        let r_robot: f64 = 0.2;
        let r_s = 0.5 * v_max.powi(2) / a_max;
        let trans = Transformer {};
        let grid_end_1 = (5, -2, 6);
        let grid_end_2 = (8, 1, 9);
        let mut decomp = Decomp::init(&grid_end_1, &grid_end_2, &trans, r_s, r_robot);
        let mut decomp2 = decomp.clone();

        println!("decomp.bbox.vertices = {:?}", decomp.bbox.vertices);
        println!("decomp.bbox.hplanes.n = ");
        for h in decomp.bbox.hplanes.iter() {
            println!("{:?},", h.n.as_slice());
        }
        println!("decomp.bbox.hplanes.p = ");
        for h in decomp.bbox.hplanes.iter() {
            println!("{:?},", h.p.as_slice());
        }

        println!(
            "[Before shrinking] decomp.ellipsoid.b , c = {} , {}",
            decomp.ellipsoid.b, decomp.ellipsoid.c
        );
        let p1 = &decomp.ellipsoid.center + Vec3::new(0.2, 0.1, 0.3);
        decomp.ellipsoid.shrink_two_axes_by_point(&p1);
        println!(
            "[After shrinking] decomp.ellipsoid.b , c = {} , {}",
            decomp.ellipsoid.b, decomp.ellipsoid.c
        );
        println!(
            "[After shrinking] distance between p1-ellipsoid = {}",
            decomp.ellipsoid.distance(&p1),
        );
        println!(
            "[After shrinking] ellipsoid strictly contains p1?  {}",
            decomp.ellipsoid.contains(&p1),
        );
        assert!((decomp.ellipsoid.distance(&p1) - 1.0).abs() < 1e-5);
        assert_eq!(decomp.ellipsoid.contains(&p1), false);

        // generate 50 random occupied points around the center
        let mut rng = thread_rng();
        let mut opoints: Vec<Vec3> = (0..50)
            .into_iter()
            .map(|i| {
                &decomp2.bbox.hplanes[2 + i as usize % 4].p
                    + 0.1
                        * Vec3::new(
                            rng.gen_range(-0.5_f64..0.5f64),
                            rng.gen_range(-0.8_f64..0.8f64),
                            rng.gen_range(-0.5_f64..0.5f64),
                        )
            })
            .collect();

        // test fit the ellipsoid
        let distances: Vec<&Vec3> = opoints
            .iter()
            .filter(|p| decomp2.ellipsoid.distance(p) < 0.9999)
            .collect();

        println!(
            "[Before fitting] ellipsoid a ={}, b ={}, c={}, contains {} opoints",
            decomp2.ellipsoid.a,
            decomp2.ellipsoid.b,
            decomp2.ellipsoid.c,
            distances.len()
        );
        let t_start = std::time::Instant::now();
        decomp2.inflate_obstacles(&mut opoints, r_robot); // inflate_obstacles
        let (k1, k2) = decomp2.best_fit_ellipsoid_for_occupied_points(&opoints);
        let t_end = std::time::Instant::now();
        let distances: Vec<&Vec3> = opoints
            .iter()
            .filter(|p| decomp2.ellipsoid.distance(p) < 0.9999)
            .collect();
        println!("cost {} us to fit", (t_end - t_start).as_secs_f64() * 1e6);
        println!(
            "[After fitting] ellipsoid a ={}, b ={}, c={}, contains {} opoints",
            decomp2.ellipsoid.a,
            decomp2.ellipsoid.b,
            decomp2.ellipsoid.c,
            distances.len()
        );
        assert!(distances.len() == 0);

        // cut_into_polyhedron
        println!(
            "[Before cutting] polyhedron has {} hyperplanes",
            decomp2.polyhedron.len(),
        );
        decomp2.cut_into_polyhedron(opoints, k1, k2);
        println!(
            "[After cutting] polyhedron has {} hyperplanes",
            decomp2.polyhedron.len(),
        );
        // the center of the ellipsoid should be inside the polyhedron
        assert_eq!(
            Decomp::<&Transformer>::is_at_inner_side(
                &decomp2.ellipsoid.center,
                &decomp2.polyhedron
            ),
            true
        );
    }

    // fn test_map
}