00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <ros/ros.h>
00041 #include <visualization_msgs/Marker.h>
00042
00043
00044 #include <boost/numeric/ublas/matrix.hpp>
00045 #include <boost/numeric/ublas/io.hpp>
00046
00047
00048 #include <ompl/base/SpaceInformation.h>
00049 #include <ompl/base/spaces/SE3StateSpace.h>
00050 #include <ompl/geometric/PathGeometric.h>
00051 #include <ompl/base/PlannerData.h>
00052 #include <ompl/config.h>
00053
00054 namespace ob = ompl::base;
00055 namespace og = ompl::geometric;
00056 namespace bnu = boost::numeric::ublas;
00057
00058 namespace ompl_rviz_viewer
00059 {
00060
00061 static const std::string BASE_FRAME = "/world";
00062
00063
00064
00065
00066 int nat_round(double x)
00067 {
00068 return static_cast<int>(floor(x + 0.5f));
00069 }
00070
00071
00072 double getCost(const geometry_msgs::Point &point, bnu::matrix<int> &cost)
00073 {
00074 return double(cost( nat_round(point.y), nat_round(point.x) )) / 2.0;
00075 }
00076
00077 double getCostHeight(const geometry_msgs::Point &point, bnu::matrix<int> &cost)
00078 {
00079
00080
00081
00082 if (floor(point.x) == point.x && floor(point.y) == point.y)
00083 return getCost(point, cost);
00084
00085
00086
00087
00088
00089 geometry_msgs::Point a;
00090 a.x = floor(point.x);
00091 a.y = ceil(point.y);
00092 a.z = getCost(a, cost);
00093
00094
00095 geometry_msgs::Point b;
00096 b.x = floor(point.x);
00097 b.y = floor(point.y);
00098 b.z = getCost(b, cost);
00099
00100
00101 geometry_msgs::Point c;
00102 c.x = ceil(point.x);
00103 c.y = floor(point.y);
00104 c.z = getCost(c, cost);
00105
00106
00107 geometry_msgs::Point d;
00108 d.x = ceil(point.x);
00109 d.y = ceil(point.y);
00110 d.z = getCost(d, cost);
00111
00112
00113
00114
00115
00116
00117
00118 double R1 = ((c.x - point.x)/(c.x - b.x))*b.z + ((point.x - b.x)/(c.x - b.x))*c.z;
00119 double R2 = ((c.x - point.x)/(c.x - b.x))*a.z + ((point.x - b.x)/(c.x - b.x))*d.z;
00120
00121
00122
00123
00124
00125 double val;
00126
00127 if ( a.y - b.y == 0)
00128 val = R1;
00129 else
00130 val = ((a.y - point.y)/(a.y - b.y))*R1 + ((point.y - b.y)/(a.y - b.y))*R2;
00131
00132
00133 return val + 0.2;
00134 }
00135
00136
00137 class OmplRvizViewer
00138 {
00139 private:
00140
00141
00142 ros::NodeHandle nh_;
00143
00144
00145 bool verbose_;
00146
00147
00148 ros::Publisher marker_pub_;
00149
00150 public:
00151
00156 OmplRvizViewer(bool verbose)
00157 : verbose_(verbose)
00158 {
00159
00160 marker_pub_ = nh_.advertise<visualization_msgs::Marker>("ompl_rviz_markers", 1);
00161 ros::Duration(1).sleep();
00162
00163 ROS_INFO_STREAM_NAMED("ompl_rviz_viewer","OmplRvizViewer Ready.");
00164 }
00165
00169 ~OmplRvizViewer()
00170 {
00171
00172 }
00173
00177 std::vector<std::pair<double, double> > convertSolutionToVector(og::PathGeometric& path)
00178 {
00179
00180 std::vector<std::pair<double, double> > coordinates;
00181
00182
00183 const std::vector<ob::State*>& states = path.getStates();
00184
00185
00186
00187
00188 for( size_t state_id = 0; state_id < states.size(); ++state_id )
00189 {
00190 const ob::State *state = states[ state_id ];
00191
00192 if (!state)
00193 continue;
00194
00195
00196 const ob::RealVectorStateSpace::StateType *real_state =
00197 static_cast<const ob::RealVectorStateSpace::StateType*>(state);
00198
00199
00200 coordinates.push_back( std::pair<double,double>( real_state->values[0], real_state->values[1] ) );
00201 }
00202
00203 return coordinates;
00204 }
00205
00209 void displayTriangles(PPMImage *image, bnu::matrix<int> &cost)
00210 {
00211 visualization_msgs::Marker marker;
00212
00213 marker.header.frame_id = BASE_FRAME;
00214 marker.header.stamp = ros::Time::now();
00215
00216
00217 marker.ns = "cost_map";
00218
00219
00220 marker.type = visualization_msgs::Marker::TRIANGLE_LIST;
00221
00222
00223 marker.action = visualization_msgs::Marker::ADD;
00224 marker.id = 1;
00225
00226 marker.pose.position.x = 0;
00227 marker.pose.position.y = 0;
00228 marker.pose.position.z = 0;
00229 marker.pose.orientation.x = 0.0;
00230 marker.pose.orientation.y = 0.0;
00231 marker.pose.orientation.z = 0.0;
00232 marker.pose.orientation.w = 1.0;
00233 marker.scale.x = 1.0;
00234 marker.scale.y = 1.0;
00235 marker.scale.z = 1.0;
00236 marker.color.r = 255;
00237 marker.color.g = 0;
00238 marker.color.b = 0;
00239 marker.color.a = 1.0;
00240
00241
00242 for( size_t marker_id = 0; marker_id < image->getSize(); ++marker_id )
00243 {
00244
00245 unsigned int x = marker_id % image->x;
00246 unsigned int y = marker_id / image->x;
00247
00248
00249
00250 if( ! (x + 1 >= image->x || y + 1 >= image->y ) )
00251 {
00252 addPoint( x, y, &marker, image, cost );
00253 addPoint( x+1, y, &marker, image, cost );
00254 addPoint( x, y+1, &marker, image, cost );
00255 }
00256
00257
00258
00259 if( ! ( int(x) - 1 < 0 || y + 1 >= image->y ) )
00260 {
00261 addPoint( x, y, &marker, image, cost );
00262 addPoint( x, y+1, &marker, image, cost );
00263 addPoint( x-1, y+1, &marker, image, cost );
00264 }
00265
00266 }
00267
00268 marker_pub_.publish( marker );
00269 }
00270
00271
00272
00273
00274 void addPoint( int x, int y, visualization_msgs::Marker* marker, PPMImage *image, bnu::matrix<int> &cost )
00275 {
00276
00277 geometry_msgs::Point point;
00278 point.x = x;
00279 point.y = y;
00280 point.z = getCost(point, cost);
00281 marker->points.push_back( point );
00282
00283
00284 std_msgs::ColorRGBA color;
00285 color.r = image->data[ image->getID( x, y ) ].red / 255.0;
00286 color.g = image->data[ image->getID( x, y ) ].green / 255.0;
00287 color.b = image->data[ image->getID( x, y ) ].blue / 255.0;
00288 color.a = 1.0;
00289 marker->colors.push_back( color );
00290 }
00291
00292
00293
00294
00295 void interpolateLine( const geometry_msgs::Point &p1, const geometry_msgs::Point &p2, visualization_msgs::Marker* marker,
00296 std_msgs::ColorRGBA &color, bnu::matrix<int> &cost )
00297 {
00298
00299 geometry_msgs::Point point_a = p1;
00300 geometry_msgs::Point point_b = p2;
00301
00302
00303 point_a.z = getCostHeight(point_a, cost);
00304 point_b.z = getCostHeight(point_b, cost);
00305
00306 ROS_INFO_STREAM("a is: " << point_a);
00307 ROS_INFO_STREAM("b is: " << point_b);
00308
00309
00310 if( point_a.x > point_b.x )
00311 {
00312
00313 geometry_msgs::Point point_temp = point_a;
00314 point_a = point_b;
00315 point_b = point_temp;
00316 }
00317
00318
00319 std_msgs::ColorRGBA color2 = color;
00320 color2.r = 1;
00321
00322
00323 if( false )
00324 {
00325
00326 color.g = 0.8;
00327
00328
00329 marker->points.push_back( point_a );
00330 marker->points.push_back( point_b );
00331 marker->colors.push_back( color );
00332 marker->colors.push_back( color );
00333
00334
00335 publishSphere(point_a, color2);
00336 publishSphere(point_b, color2);
00337 }
00338
00339
00340 color.g = 0.0;
00341
00342
00343 double m = (point_b.y - point_a.y)/(point_b.x - point_a.x);
00344
00345
00346 double b = point_a.y - m * point_a.x;
00347
00348
00349 double interval = 0.25;
00350
00351
00352 geometry_msgs::Point temp_a = point_a;
00353 geometry_msgs::Point temp_b = point_a;
00354
00355
00356 for( temp_b.x = point_a.x + interval; temp_b.x < point_b.x; temp_b.x += interval )
00357 {
00358 publishSphere(temp_a, color2);
00359
00360
00361 temp_b.y = m*temp_b.x + b;
00362
00363
00364 temp_a.z = getCostHeight(temp_a, cost);
00365 temp_b.z = getCostHeight(temp_b, cost);
00366
00367
00368 marker->points.push_back( temp_a );
00369 marker->points.push_back( temp_b );
00370
00371
00372 marker->colors.push_back( color );
00373 marker->colors.push_back( color );
00374
00375
00376 temp_a = temp_b;
00377 }
00378
00379
00380 marker->points.push_back( temp_a );
00381 marker->points.push_back( point_b );
00382
00383
00384 marker->colors.push_back( color );
00385 marker->colors.push_back( color );
00386
00387 }
00388
00389
00390
00391
00392 void displayGraph(bnu::matrix<int> &cost, ob::PlannerDataPtr planner_data)
00393 {
00394 visualization_msgs::Marker marker;
00395
00396 marker.header.frame_id = BASE_FRAME;
00397 marker.header.stamp = ros::Time();
00398
00399
00400 marker.ns = "space_exploration";
00401
00402
00403 marker.type = visualization_msgs::Marker::LINE_LIST;
00404
00405
00406 marker.action = visualization_msgs::Marker::ADD;
00407 marker.id = 0;
00408
00409 marker.pose.position.x = 0.0;
00410 marker.pose.position.y = 0.0;
00411 marker.pose.position.z = 0.0;
00412
00413 marker.pose.orientation.x = 0.0;
00414 marker.pose.orientation.y = 0.0;
00415 marker.pose.orientation.z = 0.0;
00416 marker.pose.orientation.w = 1.0;
00417
00418 marker.scale.x = 0.2;
00419 marker.scale.y = 1.0;
00420 marker.scale.z = 1.0;
00421
00422 marker.color.r = 1.0;
00423 marker.color.g = 0.0;
00424 marker.color.b = 0.0;
00425 marker.color.a = 1.0;
00426
00427 std::pair<double, double> this_vertex;
00428 std::pair<double, double> next_vertex;
00429
00430
00431 std_msgs::ColorRGBA color;
00432 color.r = 0.0;
00433 color.g = 0.0;
00434 color.b = 1.0;
00435 color.a = 1.0;
00436
00437 ROS_INFO("Publishing Graph");
00438
00439
00440
00441 geometry_msgs::Point p1;
00442 p1.x = 0;
00443 p1.y = 0;
00444 geometry_msgs::Point p2;
00445 p2.x = 6;
00446 p2.y = 0;
00447 interpolateLine( p1, p2, &marker, color, cost );
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 marker_pub_.publish( marker );
00475 }
00476
00477
00478
00479
00480 void displaySamples(bnu::matrix<int> &cost, ob::PlannerDataPtr planner_data)
00481 {
00482 visualization_msgs::Marker marker;
00483
00484 marker.header.frame_id = BASE_FRAME;
00485 marker.header.stamp = ros::Time();
00486
00487
00488 marker.ns = "sample_locations";
00489
00490
00491 marker.type = visualization_msgs::Marker::SPHERE_LIST;
00492
00493
00494 marker.action = visualization_msgs::Marker::ADD;
00495 marker.id = 0;
00496
00497 marker.pose.position.x = 1.0;
00498 marker.pose.position.y = 1.0;
00499 marker.pose.position.z = 1.0;
00500
00501 marker.pose.orientation.x = 0.0;
00502 marker.pose.orientation.y = 0.0;
00503 marker.pose.orientation.z = 0.0;
00504 marker.pose.orientation.w = 1.0;
00505
00506 marker.scale.x = 0.4;
00507 marker.scale.y = 0.4;
00508 marker.scale.z = 0.4;
00509
00510 marker.color.r = 1.0;
00511 marker.color.g = 0.0;
00512 marker.color.b = 0.0;
00513 marker.color.a = 1.0;
00514
00515 std::pair<double, double> this_vertex;
00516
00517
00518 std_msgs::ColorRGBA color;
00519 color.r = 0.8;
00520 color.g = 0.1;
00521 color.b = 0.1;
00522 color.a = 1.0;
00523
00524
00525 geometry_msgs::Point point_a;
00526
00527 ROS_INFO("Publishing Spheres");
00528
00529
00530 for( int vertex_id = 0; vertex_id < int( planner_data->numVertices() ); ++vertex_id )
00531 {
00532
00533 this_vertex = getCoordinates( vertex_id, planner_data );
00534
00535
00536 point_a.x = this_vertex.first;
00537 point_a.y = this_vertex.second;
00538 point_a.z = getCostHeight(point_a, cost);
00539
00540
00541 marker.points.push_back( point_a );
00542 marker.colors.push_back( color );
00543 }
00544
00545
00546 marker_pub_.publish( marker );
00547 }
00548
00549 void publishSphere(const geometry_msgs::Point &point, const std_msgs::ColorRGBA &color)
00550 {
00551 visualization_msgs::Marker sphere_marker;
00552
00553 sphere_marker.header.frame_id = BASE_FRAME;
00554
00555
00556 sphere_marker.ns = "Sphere";
00557
00558 sphere_marker.type = visualization_msgs::Marker::SPHERE_LIST;
00559
00560 sphere_marker.action = visualization_msgs::Marker::ADD;
00561
00562 sphere_marker.pose.position.x = 0;
00563 sphere_marker.pose.position.y = 0;
00564 sphere_marker.pose.position.z = 0;
00565 sphere_marker.pose.orientation.x = 0.0;
00566 sphere_marker.pose.orientation.y = 0.0;
00567 sphere_marker.pose.orientation.z = 0.0;
00568 sphere_marker.pose.orientation.w = 1.0;
00569
00570
00571 sphere_marker.points.push_back( point );
00572 sphere_marker.colors.push_back( color );
00573
00574
00575
00576
00577 sphere_marker.header.stamp = ros::Time::now();
00578
00579 static int sphere_id_ = 0;
00580 sphere_marker.id = ++sphere_id_;
00581 sphere_marker.color = color;
00582 sphere_marker.scale.x = 0.3;
00583 sphere_marker.scale.y = 0.3;
00584 sphere_marker.scale.z = 0.3;
00585
00586
00587 sphere_marker.points[0] = point;
00588 sphere_marker.colors[0] = color;
00589
00590
00591 marker_pub_.publish( sphere_marker );
00592 ros::spinOnce();
00593 }
00594
00595
00596
00597
00598
00599 void displayResult( og::PathGeometric& path, std_msgs::ColorRGBA* color, bnu::matrix<int> &cost )
00600 {
00601 const std::vector<std::pair<double, double> > coordinates = convertSolutionToVector(path);
00602
00603 visualization_msgs::Marker marker;
00604
00605 marker.header.frame_id = BASE_FRAME;
00606 marker.header.stamp = ros::Time();
00607
00608
00609 marker.ns = "result_path";
00610
00611
00612 marker.type = visualization_msgs::Marker::LINE_LIST;
00613
00614
00615 marker.action = visualization_msgs::Marker::ADD;
00616
00617
00618 static int result_id = 0;
00619 marker.id = result_id++;
00620
00621 marker.pose.position.x = 1.0;
00622 marker.pose.position.y = 1.0;
00623 marker.pose.position.z = 1.0;
00624
00625 marker.pose.orientation.x = 0.0;
00626 marker.pose.orientation.y = 0.0;
00627 marker.pose.orientation.z = 0.0;
00628 marker.pose.orientation.w = 1.0;
00629
00630 marker.scale.x = 0.4;
00631 marker.scale.y = 1.0;
00632 marker.scale.z = 1.0;
00633
00634 marker.color.r = 1.0;
00635 marker.color.g = 0.0;
00636 marker.color.b = 0.0;
00637 marker.color.a = 1.0;
00638
00639 ROS_INFO("Publishing result");
00640
00641
00642 double x1 = coordinates[0].first;
00643 double y1 = coordinates[0].second;
00644
00645 double x2;
00646 double y2;
00647
00648
00649 for( unsigned int i = 1; i < coordinates.size(); ++i )
00650 {
00651 x2 = coordinates[i].first;
00652 y2 = coordinates[i].second;
00653
00654
00655 geometry_msgs::Point point_a;
00656 geometry_msgs::Point point_b;
00657
00658
00659 point_a.x = x1;
00660 point_a.y = y1;
00661 point_a.z = getCostHeight(point_a, cost);
00662
00663
00664 point_b.x = x2;
00665 point_b.y = y2;
00666 point_b.z = getCostHeight(point_b, cost);
00667
00668
00669 marker.points.push_back( point_a );
00670 marker.points.push_back( point_b );
00671 marker.colors.push_back( *color );
00672 marker.colors.push_back( *color );
00673
00674
00675 x1 = x2;
00676 y1 = y2;
00677 }
00678
00679
00680 marker_pub_.publish( marker );
00681 }
00682
00683
00684
00685
00686 std::pair<double, double> getCoordinates( int vertex_id, ob::PlannerDataPtr planner_data )
00687 {
00688 ob::PlannerDataVertex vertex = planner_data->getVertex( vertex_id );
00689
00690
00691 const ob::State *state = vertex.getState();
00692
00693 if (!state)
00694 {
00695 ROS_ERROR("No state found for a vertex");
00696 exit(1);
00697 }
00698
00699
00700 const ob::RealVectorStateSpace::StateType *real_state =
00701 static_cast<const ob::RealVectorStateSpace::StateType*>(state);
00702
00703 return std::pair<double, double>( real_state->values[0], real_state->values[1] );
00704 }
00705
00706
00707 };
00708
00709 typedef boost::shared_ptr<OmplRvizViewer> OmplRvizViewerPtr;
00710 typedef boost::shared_ptr<const OmplRvizViewer> OmplRvizViewerConstPtr;
00711
00712 }