BFS_3D.cpp
Go to the documentation of this file.
00001 #include <sbpl_interface/bfs3d/BFS_3D.h>
00002 
00003 namespace sbpl_interface {
00004 
00005 inline int BFS_3D::getNode(int x, int y, int z) {
00006   if (x < 0 || y < 0 || z < 0 || x >= dim_x - 2 || y >= dim_y - 2 || z >= dim_z - 2) {
00007     //error "Invalid coordinates"
00008     return -1;
00009   }
00010   return (z + 1) * dim_xy + (y + 1) * dim_x + (x + 1);
00011 }
00012 
00013 BFS_3D::BFS_3D(int width, int height, int length) {
00014   if (width <= 0 || height <= 0 || length <= 0) {
00015     //error "Invalid dimensions"
00016     return;
00017   }
00018 
00019   dim_x = width + 2;
00020   dim_y = height + 2;
00021   dim_z = length + 2;
00022 
00023   dim_xy = dim_x * dim_y;
00024   dim_xyz = dim_xy * dim_z;
00025 
00026   distance_grid = new int[dim_xyz];
00027   queue = new int[width * height * length];
00028 
00029   for (int node = 0; node < dim_xyz; node++) {
00030     int x = node % dim_x, y = node / dim_x % dim_y, z = node / dim_xy;
00031     if (x == 0 || x == dim_x - 1 || y == 0 || y == dim_y - 1 || z == 0 || z == dim_z - 1)
00032       distance_grid[node] = WALL;
00033     else
00034       distance_grid[node] = UNDISCOVERED;
00035   }
00036 
00037   running = false;
00038 }
00039 
00040 BFS_3D::~BFS_3D() {
00041   if(search_thread_) {
00042     search_thread_->interrupt();
00043     search_thread_->join();
00044   }
00045 
00046   delete[] distance_grid;
00047   delete[] queue;
00048 }
00049 
00050 void BFS_3D::getDimensions(int* width, int* height, int* length) {
00051   *width = dim_x - 2;
00052   *height = dim_y - 2;
00053   *length = dim_z - 2;
00054 }
00055 
00056 void BFS_3D::setWall(int x, int y, int z) {
00057   if (running) {
00058     //error "Cannot modify grid while search is running"
00059     return;
00060   }
00061 
00062   int node = getNode(x, y, z);
00063   distance_grid[node] = WALL;
00064 }
00065 
00066 bool BFS_3D::isWall(int x, int y, int z) {
00067   int node = getNode(x, y, z);
00068   return distance_grid[node] == WALL;
00069 }
00070 
00071 void BFS_3D::run(int x, int y, int z) {
00072   if (running) {
00073     //error "Search already running"
00074     return;
00075   }
00076 
00077   for (int i = 0; i < dim_xyz; i++)
00078     if (distance_grid[i] != WALL)
00079       distance_grid[i] = UNDISCOVERED;
00080 
00081   origin = getNode(x, y, z);
00082 
00083   queue_head = 0;
00084   queue_tail = 1;
00085   queue[0] = origin;
00086 
00087   distance_grid[origin] = 0;
00088 
00089   search_thread_.reset(new boost::thread(&BFS_3D::search, this, dim_x, dim_xy, distance_grid, queue, queue_head, queue_tail));
00090   running = true;
00091 }
00092 
00093 int BFS_3D::getDistance(int x, int y, int z) {
00094   int node = getNode(x, y, z);
00095   while (running && distance_grid[node] < 0)
00096     ;
00097   return distance_grid[node];
00098 }
00099 
00100 }


sbpl_interface
Author(s): Gil Jones
autogenerated on Wed Sep 16 2015 04:42:15