Module prm

Module prm 

Source
Expand description

Probabilistic Roadmap (PRM) implementation for pathfinding with obstacles

This module provides an implementation of the Probabilistic Roadmap (PRM) algorithm for path planning in robotics and other applications. PRM is a sampling-based motion planning algorithm that creates a roadmap of randomly sampled configurations connected by collision-free paths, and then uses graph search to find paths through this roadmap.

§Examples

use scirs2_core::ndarray::Array1;
use scirs2_spatial::pathplanning::{PRMPlanner, PRMConfig};

// Create a configuration for the PRM planner
let config = PRMConfig::new()
    .with_num_samples(1000)
    .with_connection_radius(0.5)
    .with_seed(42);

// Define the bounds of the configuration space
let lower_bounds = Array1::from_vec(vec![0.0, 0.0]);
let upper_bounds = Array1::from_vec(vec![10.0, 10.0]);

// Create a PRM planner with a simple collision checker
let mut planner = PRMPlanner::new(config, lower_bounds, upper_bounds)?;

// Add a collision checker function that treats a circle at (5,5) with radius 2 as an obstacle
planner.set_collision_checker(Box::new(|p: &Array1<f64>| {
    let dx = p[0] - 5.0;
    let dy = p[1] - 5.0;
    let dist_squared = dx * dx + dy * dy;
    dist_squared < 4.0 // Inside the circle is in collision
}));

// Find a path from start to goal
let start = Array1::from_vec(vec![1.0, 1.0]);
let goal = Array1::from_vec(vec![9.0, 9.0]);

// Build the roadmap
planner.build_roadmap().expect("Operation failed");

// Find a path
let path = planner.find_path(&start, &goal);

match path {
    Ok(Some(path)) => {
        println!("Path found with {} points", path.nodes.len());
        for point in &path.nodes {
            println!("  {:?}", point);
        }
    },
    Ok(None) => println!("No path found"),
    Err(e) => println!("Error: {}", e),
}

Structs§

PRM2DPlanner
A specialized PRM planner for 2D spaces with polygon obstacles
PRMConfig
Configuration for the PRM planner
PRMPlanner
A probabilistic roadmap planner for path planning