example.cpp
Go to the documentation of this file.
00001 #include "dynamicvoronoi.h"
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 #include <string.h>
00006 
00007 void loadPGM( std::istream &is, int *sizeX, int *sizeY, bool ***map ) {
00008   std::string tag;
00009   is >> tag;
00010   if (tag!="P5") {
00011     std::cerr << "Awaiting 'P5' in pgm header, found " << tag << std::endl;
00012     exit(-1);
00013   }
00014   
00015   while (is.peek()==' ' || is.peek()=='\n') is.ignore();
00016   while (is.peek()=='#') is.ignore(255, '\n');
00017   is >> *sizeX;
00018   while (is.peek()=='#') is.ignore(255, '\n');
00019   is >> *sizeY;
00020   while (is.peek()=='#') is.ignore(255, '\n');
00021   is >> tag;
00022   if (tag!="255") {
00023     std::cerr << "Awaiting '255' in pgm header, found " << tag << std::endl;
00024     exit(-1);
00025   }
00026     
00027   *map = new bool*[*sizeX];
00028 
00029   for (int x=0; x<*sizeX; x++) {
00030     (*map)[x] = new bool[*sizeY];
00031   }
00032   for (int y=*sizeY-1; y>=0; y--) {
00033     for (int x=0; x<*sizeX; x++) {
00034       int c = is.get();
00035       if ((double)c<255-255*0.2) (*map)[x][y] = true; // cell is occupied
00036       else (*map)[x][y] = false; // cell is free
00037       if (!is.good()) {
00038         std::cerr << "Error reading pgm map.\n";
00039         exit(-1);
00040       }
00041     }
00042   }
00043 }
00044 
00045 
00046 int main( int argc, char *argv[] ) {
00047 
00048   if(argc<2 || argc>3 || (argc==3 && strcmp(argv[2],"prune")!=0)) {
00049     std::cerr<<"usage: "<<argv[0]<<" <pgm map> [prune]\n";
00050     exit(-1);
00051   }
00052 
00053   bool doPrune = false;
00054   if (argc==3) doPrune = true;
00055   // LOAD PGM MAP AND INITIALIZE THE VORONOI
00056   std::ifstream is(argv[1]);
00057   if (!is) {
00058     std::cerr << "Could not open map file for reading.\n";
00059     exit(-1);
00060   }
00061   
00062   bool **map=NULL;
00063   int sizeX, sizeY;
00064   loadPGM( is, &sizeX, &sizeY, &map );
00065   is.close();
00066   fprintf(stderr, "Map loaded (%dx%d).\n", sizeX, sizeY);
00067 
00068   // create the voronoi object and initialize it with the map
00069   DynamicVoronoi voronoi;
00070   voronoi.initializeMap(sizeX, sizeY, map);
00071   voronoi.update(); // update distance map and Voronoi diagram
00072   if (doPrune) voronoi.prune();  // prune the Voronoi
00073   voronoi.visualize("initial.ppm");
00074   std::cerr << "Generated initial frame.\n";
00075 
00076   // now perform some updates with random obstacles
00077   char filename[20];
00078   int numPoints = 10 + sizeX*sizeY*0.005;
00079   for (int frame=1; frame<=10; frame++) {
00080     std::vector<IntPoint> newObstacles;
00081     for (int i=0; i<numPoints; i++) {
00082       double x = 2+rand()/(double)RAND_MAX*(sizeX-4);
00083       double y = 2+rand()/(double)RAND_MAX*(sizeY-4);
00084       newObstacles.push_back(IntPoint(x,y));
00085     }
00086     voronoi.exchangeObstacles(newObstacles); // register the new obstacles (old ones will be removed)
00087     voronoi.update();
00088     if (doPrune) voronoi.prune();
00089     sprintf(filename, "update_%03d.ppm", frame);
00090     voronoi.visualize(filename);
00091     std::cerr << "Performed update with random obstacles.\n";
00092   }
00093 
00094   // now remove all random obstacles again.
00095   // final.pgm should be very similar to initial.pgm, except for ambiguous spots
00096   std::vector<IntPoint> empty;
00097   voronoi.exchangeObstacles(empty);
00098   voronoi.update();
00099   if (doPrune) voronoi.prune();
00100   voronoi.visualize("final.ppm");
00101   std::cerr << "Done with final update (all random obstacles removed).\n";
00102   std::cerr << "Check initial.ppm, update_???.ppm and final.ppm.\n";
00103   return 0;
00104 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


dynamicvoronoi
Author(s): Boris Lau, Christoph Sprunk, Wolfram Burgard
autogenerated on Wed Dec 26 2012 16:35:03