00001 #ifndef NDT_VIZ_HH
00002 #define NDT_VIZ_HH
00003
00004 #include <ndt_map/ndt_map.h>
00005 #include <ndt_visualisation/NDTVizGlut.hh>
00006 #include <pthread.h>
00007
00008
00009
00010
00011 class NDTViz {
00012
00013 public:
00014 NDTVizGlut *win3D;
00015 NDTVizGlutPointCloudColor gl_points;
00016 NDTVizGlutPointCloudColor gl_particles;
00017 NDTVizGlutPointCloudColor gl_pointcloud;
00018 NDTVizGlutSetOfLines gl_laserlines;
00019 NDTVizGlutEllipsoids gl_ellipsoids;
00020
00021 NDTViz(bool allocate_new_window=true)
00022
00023 {
00024 if(allocate_new_window)
00025 {
00026 win3D = new NDTVizGlut();
00027 int argc=0;
00028 char** argv = NULL;
00029 win3D->win_run(&argc,argv);
00030 }
00031 else
00032 {
00033 win3D = NULL;
00034 }
00035
00036 gl_points.setPointSize(4.5);
00037 gl_particles.setPointSize(2.5);
00038
00039 }
00040 virtual ~NDTViz () {
00041 if(win3D!=NULL) {
00042 win3D->win_close();
00043 delete win3D;
00044 }
00045 }
00046
00047 void startEventLoop() {
00048
00049 }
00050
00051 void repaint(){
00052 win3D->repaint();
00053 }
00054
00055 void clear(){
00056 win3D->clearScene();
00057 }
00058
00059 void clearTrajectoryPoints(){
00060 gl_points.clear();
00061 }
00062
00063 void addTrajectoryPoint(float x, float y, float z, float R=1.0, float G = 1.0, float B = 1.0){
00064 gl_points.push_back(x, y, z, R ,G,B);
00065 }
00066 void displayTrajectory(){
00067 win3D->addObject(&gl_points);
00068 }
00069
00070 void clearParticles(){ gl_particles.clear();}
00071 void addParticle(float x, float y, float z, float R=1.0, float G = 1.0, float B = 1.0){
00072 gl_particles.push_back(x, y, z, R ,G,B);
00073 }
00074 void displayParticles(){
00075 win3D->addObject(&gl_particles);
00076 }
00077 void setCameraPointingToPoint(double x, double y, double z) {
00078 win3D->setCameraPointingToPoint(x,y,z);
00079 }
00080 void setCameraPointing(double x, double y, double z) {
00081 win3D->setCameraPointingToPoint(x,y,z);
00082 }
00083
00084
00085
00089 void addScan(Eigen::Vector3d orig, pcl::PointCloud<pcl::PointXYZ> &cloud, double R=1.0,double G=1.0,double B=1.0){
00090
00091 gl_laserlines.clear();
00092 for(unsigned int i=0;i<cloud.points.size();i+=2){
00093 gl_laserlines.appendLine(orig(0),orig(1),orig(2), cloud.points[i].x, cloud.points[i].y, cloud.points[i].z);
00094 }
00095 gl_laserlines.setColor(R,G,B);
00096 win3D->addObject(&gl_laserlines);
00097 }
00098 void addPointCloud(pcl::PointCloud<pcl::PointXYZ> &cloud, double R=1.0,double G=1.0,double B=1.0){
00099
00100 gl_pointcloud.setPointSize(2.5);
00101 for(unsigned int i=0;i<cloud.points.size();i+=2){
00102 gl_pointcloud.push_back(cloud.points[i].x, cloud.points[i].y, cloud.points[i].z,R,G,B);
00103 }
00104 win3D->addObject(&gl_pointcloud);
00105 }
00106
00107 void plotNDTMap(lslgeneric::NDTMap *map, double R=1.0,double G=1.0,double B=1.0, bool heightCoding=false, bool setCameraPos = true ){
00108 if(win3D == NULL) return;
00109 std::vector<lslgeneric::NDTCell*> global_ndts;
00110 global_ndts = map->getAllCells();
00111
00112 win3D->clearScene();
00113 gl_ellipsoids.clear();
00114
00115 unsigned int accepted_ndts=1;
00116 double x = 0,y=0,s=0;
00117
00118 for(unsigned int i=0;i<global_ndts.size();i++){
00119 Eigen::Vector3d m = global_ndts[i]->getMean();
00120 if(!global_ndts[i]->hasGaussian_) continue;
00121 x+=m[0];
00122 y+=m[1];
00123 s+=1;
00124
00125 accepted_ndts++;
00126 NDTVizGlutEllipsoid objEllip;
00127 Eigen::Matrix3d cov = global_ndts[i]->getCov();
00128 objEllip.setCovMatrix(cov);
00129 objEllip.setLocation(m[0], m[1], m[2]);
00130 double hval=1.0;
00131 if(heightCoding){
00132 hval = fabs(m[2]+1.5)/6.0;
00133 }
00134 if(global_ndts[i]->getOccupancy() > 0){
00135 objEllip.setColor(R/hval,G/hval,B/hval,1.0);
00136 }else{
00137 objEllip.setColor(1.0,0,0,1.0);
00138 }
00139
00140 objEllip.enableDrawSolid3D(true);
00141 gl_ellipsoids.push_back(objEllip);
00142 }
00143 win3D->addObject(&gl_ellipsoids);
00144 if(setCameraPos) win3D->setCameraPointingToPoint(x/s,y/s,3.0);
00145 win3D->repaint();
00146
00147 for(unsigned int i=0;i<global_ndts.size();i++) delete global_ndts[i];
00148
00149 }
00150
00151 void plotNDTSAccordingToCost(float occupancy, double MAX_COST, lslgeneric::NDTMap *map){
00152
00153 if(win3D == NULL) return;
00154 std::vector<lslgeneric::NDTCell*> global_ndts;
00155 global_ndts = map->getAllCells();
00156
00157
00158 win3D->clearScene();
00159 gl_ellipsoids.clear();
00160 unsigned int accepted_ndts=1;
00161 double x = 0,y=0,s=0;
00162 for(unsigned int i=0;i<global_ndts.size();i++){
00163 Eigen::Vector3d m = global_ndts[i]->getMean();
00164 if(!global_ndts[i]->hasGaussian_) continue;
00165 x+=m[0];
00166 y+=m[1];
00167 s+=1;
00168 if(global_ndts[i]->getOccupancy()>occupancy){
00169 accepted_ndts++;
00170 NDTVizGlutEllipsoid objEllip;
00171 Eigen::Matrix3d cov = global_ndts[i]->getCov();
00172 objEllip.setCovMatrix(cov);
00173 objEllip.setLocation(m[0], m[1], m[2]);
00174 double c = global_ndts[i]->cost;
00175 if(c > MAX_COST) c=MAX_COST;
00176 c = 1 - c/MAX_COST;
00177 objEllip.setColor(c,c,c,0.6);
00178
00179 objEllip.enableDrawSolid3D(true);
00180 gl_ellipsoids.push_back( objEllip );
00181 }else if(global_ndts[i]->getOccupancy()<-0){
00182 }
00183 }
00184 win3D->addObject(&gl_ellipsoids);
00185
00186 win3D->repaint();
00187 fprintf(stderr,"(%lf %lf) s=%lf\n",x/s,y/s,s);
00188 for(unsigned int i=0;i<global_ndts.size();i++) delete global_ndts[i];
00189
00190 }
00191
00192 void plotNDTSAccordingToClass(float occupancy, lslgeneric::NDTMap *map){
00193
00194 std::cout << "PLOT CLASS" << std::endl;
00195 if(win3D == NULL) return;
00196 std::vector<lslgeneric::NDTCell*> global_ndts;
00197 global_ndts = map->getAllCells();
00198
00199
00200 Eigen::Vector3d cFlat, cInclined, cRough, cVert, cUnknown, color;
00201 cFlat<<0,0.9,0;
00202 cInclined<<0.1,0.2,0.5;
00203 cRough<<0.9,0,0;
00204 cVert<<0,0,0.9;
00205 cUnknown<<0,0,0;
00206
00207 win3D->clearScene();
00208 gl_ellipsoids.clear();
00209
00210 unsigned int accepted_ndts=1;
00211 double x = 0,y=0,s=0;
00212 for(unsigned int i=0;i<global_ndts.size();i++){
00213 Eigen::Vector3d m = global_ndts[i]->getMean();
00214 if(!global_ndts[i]->hasGaussian_) continue;
00215 x+=m[0];
00216 y+=m[1];
00217 s+=1;
00218 if(global_ndts[i]->getOccupancy()>occupancy){
00219 accepted_ndts++;
00220 NDTVizGlutEllipsoid objEllip;
00221 Eigen::Matrix3d cov = global_ndts[i]->getCov();
00222 objEllip.setCovMatrix(cov);
00223 objEllip.setLocation(m[0], m[1], m[2]);
00224 switch(global_ndts[i]->getClass()) {
00225 case lslgeneric::NDTCell::HORIZONTAL :
00226 color = cFlat;
00227 break;
00228 case lslgeneric::NDTCell::VERTICAL :
00229 color = cVert;
00230 break;
00231 case lslgeneric::NDTCell::INCLINED :
00232 color = cInclined;
00233 break;
00234 case lslgeneric::NDTCell::ROUGH :
00235 color = cRough;
00236 break;
00237 default:
00238 color = cUnknown;
00239 }
00240 objEllip.setColor(color(0),color(1),color(2),0.6);
00241
00242 objEllip.enableDrawSolid3D(true);
00243 gl_ellipsoids.push_back(objEllip);
00244
00245 }else if(global_ndts[i]->getOccupancy()<-0){
00246
00247 }
00248 }
00249
00250 win3D->addObject(&gl_ellipsoids);
00251 win3D->repaint();
00252 fprintf(stderr,"(%lf %lf) s=%lf\n",x/s,y/s,s);
00253 for(unsigned int i=0;i<global_ndts.size();i++) delete global_ndts[i];
00254
00255 }
00256
00257 void plotNDTSAccordingToOccupancy(float occupancy, lslgeneric::NDTMap *map){
00258 if(win3D == NULL) return;
00259 std::vector<lslgeneric::NDTCell*> global_ndts;
00260 global_ndts = map->getAllCells();
00261
00262
00263 win3D->clearScene();
00264 gl_ellipsoids.clear();
00265
00266 unsigned int accepted_ndts=1;
00267 double x = 0,y=0,s=0;
00268 for(unsigned int i=0;i<global_ndts.size();i++){
00269 Eigen::Vector3d m = global_ndts[i]->getMean();
00270 if(!global_ndts[i]->hasGaussian_) continue;
00271 x+=m[0];
00272 y+=m[1];
00273 s+=1;
00274 if(global_ndts[i]->getOccupancy()>occupancy){
00275 accepted_ndts++;
00276 NDTVizGlutEllipsoid objEllip;
00277 Eigen::Matrix3d cov = global_ndts[i]->getCov();
00278 objEllip.setCovMatrix(cov);
00279 objEllip.setLocation(m[0], m[1], m[2]);
00280
00281 objEllip.setColor((m[2]+2.0)/10.0,0,0,0.6);
00282
00283 objEllip.enableDrawSolid3D(true);
00284 gl_ellipsoids.push_back(objEllip);
00285 }else if(global_ndts[i]->getOccupancy()<-0){
00286 }
00287 }
00288
00289 win3D->addObject(&gl_ellipsoids);
00290 win3D->repaint();
00291 fprintf(stderr,"(%lf %lf) s=%lf\n",x/s,y/s,s);
00292 for(unsigned int i=0;i<global_ndts.size();i++) delete global_ndts[i];
00293
00294 }
00295
00296 void plotLocalNDTMap(pcl::PointCloud<pcl::PointXYZ> &cloud, double resolution, double R=0, double G=1, double B=0, bool heightCoding=true){
00297 if(win3D == NULL) return;
00298
00299 lslgeneric::NDTMap ndlocal(new lslgeneric::LazyGrid(resolution));
00300 ndlocal.addPointCloudSimple(cloud);
00301 ndlocal.computeNDTCells(CELL_UPDATE_MODE_SAMPLE_VARIANCE);
00302
00303 std::vector<lslgeneric::NDTCell*> ndts;
00304 ndts = ndlocal.getAllCells();
00305 std::cout<<"LOCAL: "<<ndts.size()<<std::endl;
00306
00307 for(unsigned int i=0;i<ndts.size();i++){
00308 Eigen::Vector3d m = ndts[i]->getMean();
00309
00310
00311 NDTVizGlutEllipsoid objEllip;
00312 Eigen::Matrix3d cov = ndts[i]->getCov();
00313 objEllip.setCovMatrix(cov);
00314 objEllip.setLocation(m[0], m[1], m[2]);
00315 double hval=1.0;
00316 if(heightCoding){
00317 hval = fabs(m[2]+1.5)/20.0;
00318 }
00319 objEllip.setColor(R/hval,G/hval,B/hval,0.4);
00320 objEllip.enableDrawSolid3D(true);
00321 gl_ellipsoids.push_back(objEllip);
00322 }
00323 win3D->addObject(&gl_ellipsoids);
00324 win3D->repaint();
00325 for(unsigned int i=0;i<ndts.size();i++){
00326 delete ndts[i];
00327 }
00328
00329 }
00330
00331 void plotLocalConflictNDTMap(lslgeneric::NDTMap *map, pcl::PointCloud<pcl::PointXYZ> &cloud,
00332 double resolution, double R=1, double G=0, double B=0, bool heightCoding=false, double maxz=0){
00333 if(win3D == NULL) return;
00334
00335 lslgeneric::NDTMap ndlocal(new lslgeneric::LazyGrid(resolution));
00336
00337 ndlocal.addPointCloudSimple(cloud);
00338 ndlocal.computeNDTCells(CELL_UPDATE_MODE_SAMPLE_VARIANCE);
00339
00340 std::vector<lslgeneric::NDTCell*> ndts;
00341 ndts = ndlocal.getAllCells();
00342 gl_ellipsoids.clear();
00343 win3D->clearScene();
00344
00345 std::cout<<"LOCAL: "<<ndts.size()<<std::endl;
00346
00347 for(unsigned int i=0;i<ndts.size();i++){
00348
00349
00350 Eigen::Vector3d m = ndts[i]->getMean();
00351 if(m[2]>maxz) continue;
00352 pcl::PointXYZ p;
00353 p.x = m[0]; p.y=m[1]; p.z = m[2];
00354 lslgeneric::NDTCell *map_cell=NULL;
00355 map->getCellAtPoint(p, map_cell);
00356 if(map_cell == NULL) continue;
00357
00358 if(map_cell->getOccupancy()>0.5) continue;
00359
00360
00361 NDTVizGlutEllipsoid objEllip;
00362 Eigen::Matrix3d cov = ndts[i]->getCov();
00363 objEllip.setCovMatrix(cov);
00364 objEllip.setLocation(m[0], m[1], m[2]);
00365 double hval=1.0;
00366 if(heightCoding){
00367 hval = fabs(m[2]+1.5)/20.0;
00368 }
00369 objEllip.setColor(R/hval,G/hval,B/hval,0.6);
00370 objEllip.enableDrawSolid3D(true);
00371 gl_ellipsoids.push_back(objEllip);
00372 }
00373 win3D->addObject(&gl_ellipsoids);
00374 win3D->repaint();
00375 for(unsigned int i=0;i<ndts.size();i++){
00376 delete ndts[i];
00377 }
00378
00379 }
00380
00381 void plotNDTTraversabilityMap(lslgeneric::NDTMap *map){
00382 if(win3D == NULL) return;
00383 std::vector<lslgeneric::NDTCell*> global_ndts;
00384 global_ndts = map->getAllCells();
00385
00386 gl_ellipsoids.clear();
00387 win3D->clearScene();
00388
00389 for(unsigned int i=0;i<global_ndts.size();i++){
00390 Eigen::Vector3d m = global_ndts[i]->getMean();
00391 if(!global_ndts[i]->hasGaussian_) continue;
00392
00393 NDTVizGlutEllipsoid objEllip;
00394 Eigen::Matrix3d cov = global_ndts[i]->getCov();
00395 objEllip.setCovMatrix(cov);
00396 objEllip.setLocation(m[0], m[1], m[2]);
00397
00398
00399
00400
00401
00402
00403 if(global_ndts[i]->getClass() == lslgeneric::NDTCell::HORIZONTAL){
00404 objEllip.setColor(0,1.0,0,1.0);
00405 }else if(global_ndts[i]->getClass() == lslgeneric::NDTCell::VERTICAL){
00406 objEllip.setColor(1.0,0,0,1.0);
00407 }else if(global_ndts[i]->getClass() == lslgeneric::NDTCell::INCLINED){
00408 objEllip.setColor(1.0,1.0,0,1.0);
00409 }else if(global_ndts[i]->getClass() == lslgeneric::NDTCell::ROUGH){
00410 objEllip.setColor(0,0,1.0,1.0);
00411 }else{
00412 objEllip.setColor(1.0,1.0,1.0,1.0);
00413 }
00414
00415 objEllip.enableDrawSolid3D(true);
00416 gl_ellipsoids.push_back(objEllip);
00417 }
00418 win3D->addObject(&gl_ellipsoids);
00419 win3D->repaint();
00420 for(unsigned int i=0;i<global_ndts.size();i++) delete global_ndts[i];
00421
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00518
00519
00520
00521 };
00522
00523 #endif