rviz_visual_tools_demo.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2016, University of Colorado, Boulder
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Univ of CO, Boulder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Dave Coleman
00036    Desc:   Demo implementation of rviz_visual_tools
00037            To use, add a Rviz Marker Display subscribed to topic /rviz_visual_tools
00038 */
00039 
00040 // ROS
00041 #include <ros/ros.h>
00042 
00043 // For visualizing things in rviz
00044 #include <rviz_visual_tools/rviz_visual_tools.h>
00045 
00046 // C++
00047 #include <string>
00048 
00049 namespace rvt = rviz_visual_tools;
00050 
00051 namespace rviz_visual_tools
00052 {
00053 class RvizVisualToolsDemo
00054 {
00055 private:
00056   // A shared node handle
00057   ros::NodeHandle nh_;
00058 
00059   // For visualizing things in rviz
00060   rvt::RvizVisualToolsPtr visual_tools_;
00061 
00062   std::string name_;
00063 
00064 public:
00068   RvizVisualToolsDemo() : name_("rviz_demo")
00069   {
00070     visual_tools_.reset(new rvt::RvizVisualTools("base", "/rviz_visual_tools"));
00071 
00072     ROS_INFO("Sleeping 5 seconds before running demo");
00073     ros::Duration(5.0).sleep();
00074 
00075     // Clear messages
00076     visual_tools_->deleteAllMarkers();
00077     visual_tools_->enableBatchPublishing();
00078   }
00079 
00080   void publishLabelHelper(const Eigen::Affine3d& pose, const std::string& label)
00081   {
00082     Eigen::Affine3d pose_copy = pose;
00083     pose_copy.translation().x() -= 0.2;
00084     visual_tools_->publishText(pose_copy, label, rvt::WHITE, rvt::REGULAR, false);
00085   }
00086 
00087   void runTests()
00088   {
00089     // Create pose
00090     Eigen::Affine3d pose1 = Eigen::Affine3d::Identity();
00091     Eigen::Affine3d pose2 = Eigen::Affine3d::Identity();
00092 
00093     double space_between_rows = 0.2;
00094     double y = 0;
00095     double step;
00096 
00097     // --------------------------------------------------------------------
00098     ROS_INFO_STREAM_NAMED(name_, "Displaying range of colors red->green");
00099     step = 0.02;
00100     for (double i = 0; i <= 1.0; i += 0.02)
00101     {
00102       geometry_msgs::Vector3 scale = visual_tools_->getScale(XLARGE, false, 0.05);
00103       std_msgs::ColorRGBA color = visual_tools_->getColorScale(i);
00104       visual_tools_->publishSphere(visual_tools_->convertPose(pose1), color, scale, "Sphere");
00105       if (!i)
00106         publishLabelHelper(pose1, "Sphere Color Range");
00107       pose1.translation().x() += step;
00108     }
00109     visual_tools_->triggerBatchPublish();
00110 
00111     // --------------------------------------------------------------------
00112     ROS_INFO_STREAM_NAMED(name_, "Displaying Coordinate Axis");
00113     pose1.translation().x() = 0;
00114     y += space_between_rows;
00115     pose1.translation().y() = y;
00116     step = 0.025;
00117     for (double i = 0; i <= 1.0; i += step)
00118     {
00119       visual_tools_->publishAxis(pose1);
00120       if (!i)
00121         publishLabelHelper(pose1, "Coordinate Axis");
00122 
00123       pose1.translation().x() += step;
00124       pose1 = pose1 * Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitX()) *
00125               Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitY()) *
00126               Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitZ());
00127     }
00128     visual_tools_->triggerBatchPublish();
00129 
00130     // --------------------------------------------------------------------
00131     ROS_INFO_STREAM_NAMED(name_, "Displaying Arrows");
00132     pose1 = Eigen::Affine3d::Identity();
00133     y += space_between_rows;
00134     pose1.translation().y() = y;
00135     step = 0.025;
00136     for (double i = 0; i <= 1.0; i += step)
00137     {
00138       visual_tools_->publishArrow(pose1, rvt::RAND);
00139       if (!i)
00140         publishLabelHelper(pose1, "Arrows");
00141 
00142       pose1.translation().x() += step;
00143       pose1 = pose1 * Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitZ());
00144     }
00145     visual_tools_->triggerBatchPublish();
00146 
00147     // --------------------------------------------------------------------
00148     ROS_INFO_STREAM_NAMED(name_, "Displaying Rectangular Cuboid");
00149     double cuboid_max_size = 0.075;
00150     double cuboid_min_size = 0.01;
00151     pose1 = Eigen::Affine3d::Identity();
00152     pose2 = Eigen::Affine3d::Identity();
00153     y += space_between_rows;
00154     pose1.translation().y() = y;
00155     pose2.translation().y() = y;
00156     step = 0.1;
00157     for (double i = 0; i <= 1.0; i += step)
00158     {
00159       pose2 = pose1;
00160       pose2.translation().x() += i * cuboid_max_size + cuboid_min_size;
00161       pose2.translation().y() += i * cuboid_max_size + cuboid_min_size;
00162       pose2.translation().z() += i * cuboid_max_size + cuboid_min_size;
00163       visual_tools_->publishCuboid(pose1.translation(), pose2.translation(), rvt::RAND);
00164 
00165       if (!i)
00166         publishLabelHelper(pose1, "Cuboid");
00167 
00168       pose1.translation().x() += step;
00169     }
00170     visual_tools_->triggerBatchPublish();
00171 
00172     // --------------------------------------------------------------------
00173     ROS_INFO_STREAM_NAMED(name_, "Displaying Lines");
00174     double line_max_size = 0.075;
00175     double line_min_size = 0.01;
00176     pose1 = Eigen::Affine3d::Identity();
00177     pose2 = Eigen::Affine3d::Identity();
00178     y += space_between_rows;
00179     pose1.translation().y() = y;
00180     pose2.translation().y() = y;
00181     step = 0.1;
00182     for (double i = 0; i <= 1.0; i += step)
00183     {
00184       pose2 = pose1;
00185       pose2.translation().x() += i * line_max_size + line_min_size;
00186       pose2.translation().y() += i * line_max_size + line_min_size;
00187       pose2.translation().z() += i * line_max_size + line_min_size;
00188       visual_tools_->publishLine(pose1.translation(), pose2.translation(), rvt::RAND);
00189 
00190       if (!i)
00191         publishLabelHelper(pose1, "Line");
00192 
00193       pose1.translation().x() += step;
00194     }
00195     visual_tools_->triggerBatchPublish();
00196 
00197     // --------------------------------------------------------------------
00198     ROS_INFO_STREAM_NAMED(name_, "Displaying Cylinder");
00199     pose1 = Eigen::Affine3d::Identity();
00200     y += space_between_rows;
00201     pose1.translation().y() = y;
00202     step = 0.025;
00203     for (double i = 0; i <= 1.0; i += step)
00204     {
00205       visual_tools_->publishCylinder(pose1, rvt::RAND);
00206       if (!i)
00207         publishLabelHelper(pose1, "Cylinder");
00208 
00209       pose1.translation().x() += step;
00210       pose1 = pose1 * Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitZ());
00211     }
00212     visual_tools_->triggerBatchPublish();
00213 
00214     // --------------------------------------------------------------------
00215     ROS_INFO_STREAM_NAMED(name_, "Displaying Axis Cone");
00216     pose1 = Eigen::Affine3d::Identity();
00217     y += space_between_rows;
00218     pose1.translation().y() = y;
00219     step = 0.025;
00220     for (double i = 0; i <= 1.0; i += step)
00221     {
00222       visual_tools_->publishCone(pose1, M_PI / 6.0, rvt::RAND, 0.05);
00223       if (!i)
00224         publishLabelHelper(pose1, "Cone");
00225 
00226       pose1.translation().x() += step;
00227       pose1 = pose1 * Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitZ());
00228     }
00229     visual_tools_->triggerBatchPublish();
00230 
00231     // --------------------------------------------------------------------
00232     ROS_INFO_STREAM_NAMED(name_, "Displaying Wireframe Cuboid");
00233     pose1 = Eigen::Affine3d::Identity();
00234     y += space_between_rows;
00235     pose1.translation().y() = y;
00236     step = 0.1;
00237     // TODO(davetcoleman): use generateRandomCuboid()
00238     Eigen::Vector3d min_point, max_point;
00239     min_point << -0.05, -0.05, -0.05;
00240     max_point << 0.05, 0.05, 0.05;
00241     for (double i = 0; i <= 1.0; i += step)
00242     {
00243       visual_tools_->publishWireframeCuboid(pose1, min_point, max_point, rvt::RAND);
00244       if (!i)
00245         publishLabelHelper(pose1, "Wireframe Cuboid");
00246 
00247       pose1.translation().x() += step;
00248       pose1 = pose1 * Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitZ());
00249     }
00250     visual_tools_->triggerBatchPublish();
00251 
00252     // --------------------------------------------------------------------
00253     ROS_INFO_STREAM_NAMED(name_, "Displaying Sized Wireframe Cuboid");
00254     pose1 = Eigen::Affine3d::Identity();
00255     y += space_between_rows;
00256     pose1.translation().y() = y;
00257     step = 0.1;
00258     double depth = 0.05, width = 0.05, height = 0.05;
00259     for (double i = 0; i <= 1.0; i += step)
00260     {
00261       visual_tools_->publishWireframeCuboid(pose1, depth, width, height, rvt::RAND);
00262       if (!i)
00263         publishLabelHelper(pose1, "Wireframe Cuboid");
00264 
00265       pose1.translation().x() += step;
00266       pose1 = pose1 * Eigen::AngleAxisd(step * 2 * M_PI, Eigen::Vector3d::UnitZ());
00267     }
00268     visual_tools_->triggerBatchPublish();
00269 
00270     // --------------------------------------------------------------------
00271     ROS_INFO_STREAM_NAMED(name_, "Displaying Planes");
00272     pose1 = Eigen::Affine3d::Identity();
00273     y += space_between_rows;
00274     pose1.translation().y() = y;
00275     step = 0.2;
00276     double max_plane_size = 0.075;
00277     double min_plane_size = 0.01;
00278     for (double i = 0; i <= 1.0; i += step)
00279     {
00280       visual_tools_->publishXYPlane(pose1, rvt::RED, i * max_plane_size + min_plane_size);
00281       visual_tools_->publishXZPlane(pose1, rvt::GREEN, i * max_plane_size + min_plane_size);
00282       visual_tools_->publishYZPlane(pose1, rvt::BLUE, i * max_plane_size + min_plane_size);
00283       if (!i)
00284         publishLabelHelper(pose1, "Planes");
00285 
00286       pose1.translation().x() += step;
00287     }
00288     visual_tools_->triggerBatchPublish();
00289 
00290     // --------------------------------------------------------------------
00291     ROS_INFO_STREAM_NAMED(name_, "Displaying Graph");
00292     pose1 = Eigen::Affine3d::Identity();
00293     y += space_between_rows;
00294     pose1.translation().y() = y;
00295     step = 0.1;
00296     graph_msgs::GeometryGraph graph;
00297     for (double i = 0; i <= 1.0; i += step)
00298     {
00299       graph.nodes.push_back(visual_tools_->convertPose(pose1).position);
00300       graph_msgs::Edges edges;
00301       if (i > 0)
00302         edges.node_ids.push_back(0);
00303       graph.edges.push_back(edges);
00304 
00305       if (!i)
00306         publishLabelHelper(pose1, "Graph");
00307 
00308       pose1.translation().x() += step;
00309       pose1.translation().z() += visual_tools_->dRand(-0.1, 0.1);
00310     }
00311     visual_tools_->publishGraph(graph, rvt::ORANGE, 0.005);
00312     visual_tools_->triggerBatchPublish();
00313 
00314     // --------------------------------------------------------------------
00315     // TODO(davetcoleman): publishMesh
00316 
00317     // --------------------------------------------------------------------
00318     // TODO(davetcoleman): publishPolygon
00319 
00320     // --------------------------------------------------------------------
00321     ROS_INFO_STREAM_NAMED(name_, "Displaying Labeled Coordinate Axis");
00322     pose1.translation().x() = 0;
00323     y += space_between_rows;
00324     pose1.translation().y() = y;
00325     pose1.translation().z() = 0;
00326     step = 0.2;
00327     for (double i = 0; i <= 1.0; i += step)
00328     {
00329       visual_tools_->publishAxisLabeled(pose1, "label of axis");
00330       if (!i)
00331         publishLabelHelper(pose1, "Labeled Coordinate Axis");
00332 
00333       pose1.translation().x() += step;
00334       pose1 = pose1 * Eigen::AngleAxisd(step*2*M_PI, Eigen::Vector3d::UnitX())
00335         * Eigen::AngleAxisd(step*2*M_PI, Eigen::Vector3d::UnitY())
00336         * Eigen::AngleAxisd(step*2*M_PI, Eigen::Vector3d::UnitZ());
00337     }
00338     visual_tools_->triggerBatchPublish();
00339   }
00340 
00344   ~RvizVisualToolsDemo()
00345   {
00346   }
00347 };  // end class
00348 
00349 }  // namespace rviz_visual_tools
00350 
00351 int main(int argc, char** argv)
00352 {
00353   ros::init(argc, argv, "visual_tools_demo");
00354   ROS_INFO_STREAM("Visual Tools Demo");
00355 
00356   // Allow the action server to recieve and send ros messages
00357   ros::AsyncSpinner spinner(1);
00358   spinner.start();
00359 
00360   rviz_visual_tools::RvizVisualToolsDemo demo;
00361   demo.runTests();
00362 
00363   ROS_INFO_STREAM("Shutting down.");
00364 
00365   return 0;
00366 }


rviz_visual_tools
Author(s): Dave Coleman
autogenerated on Sun Aug 7 2016 03:34:46