Go to the documentation of this file.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
00037 #include <cstdio>
00038 #include <cstdlib>
00039 #include <ros/ros.h>
00040 #include <algorithm>
00041 #include <visualization_msgs/Marker.h>
00042 #include <geometric_shapes/bodies.h>
00043 #include <geometric_shapes/shape_operations.h>
00044
00045 const int TEST_TIMES = 3;
00046 const int TEST_POINTS = 100000;
00047
00048 class TestVM
00049 {
00050 public:
00051
00052 TestVM(void)
00053 {
00054 m_vmPub = m_nodeHandle.advertise<visualization_msgs::Marker>("visualization_marker", 10240);
00055 m_id = 1;
00056 }
00057
00058 virtual ~TestVM(void)
00059 {
00060 }
00061
00062 void sendPoint(double x, double y, double z)
00063 {
00064 visualization_msgs::Marker mk;
00065
00066 mk.header.stamp = m_tm;
00067
00068 mk.header.frame_id = "base_link";
00069
00070 mk.ns = "test_collision_space";
00071 mk.id = m_id++;
00072 mk.type = visualization_msgs::Marker::SPHERE;
00073 mk.action = visualization_msgs::Marker::ADD;
00074 mk.pose.position.x = x;
00075 mk.pose.position.y = y;
00076 mk.pose.position.z = z;
00077 mk.pose.orientation.w = 1.0;
00078
00079 mk.scale.x = mk.scale.y = mk.scale.z = 0.02;
00080
00081 mk.color.a = 1.0;
00082 mk.color.r = 1.0;
00083 mk.color.g = 0.04;
00084 mk.color.b = 0.04;
00085
00086 m_vmPub.publish(mk);
00087 }
00088
00089 void testShape(bodies::Body *s)
00090 {
00091 for (int i = 0 ; i < TEST_POINTS ; ++i)
00092 {
00093 double x = uniform(5.0);
00094 double y = uniform(5.0);
00095 double z = uniform(5.0);
00096 if (!s->containsPoint(x, y, z))
00097 continue;
00098 sendPoint(x, y, z);
00099 }
00100 }
00101
00102 void setShapeTransformAndMarker(bodies::Body *s,
00103 visualization_msgs::Marker &mk)
00104 {
00105 btTransform t;
00106
00107 double yaw = uniform(M_PI);
00108 double pitch = uniform(M_PI);
00109 double roll = uniform(M_PI);
00110
00111 double x = uniform(3.0);
00112 double y = uniform(3.0);
00113 double z = uniform(3.0);
00114
00115 t.setRotation(btQuaternion(yaw, pitch, roll));
00116 t.setOrigin(btVector3(btScalar(x), btScalar(y), btScalar(z)));
00117
00118 s->setPose(t);
00119 s->setScale(1.0);
00120
00121 mk.header.stamp = m_tm;
00122 mk.header.frame_id = "base_link";
00123 mk.ns = "test_collision_space";
00124 mk.id = m_id++;
00125
00126 mk.action = visualization_msgs::Marker::ADD;
00127
00128 mk.pose.position.x = x;
00129 mk.pose.position.y = y;
00130 mk.pose.position.z = z;
00131
00132 btQuaternion orient(yaw, pitch, roll);
00133 mk.pose.orientation.x = orient.x();
00134 mk.pose.orientation.y = orient.y();
00135 mk.pose.orientation.z = orient.z();
00136 mk.pose.orientation.w = orient.w();
00137
00138 mk.color.a = 0.55;
00139 mk.color.r = 0.0;
00140 mk.color.g = 0.4;
00141 mk.color.b = 1.0;
00142 }
00143
00144 void testSphere(void)
00145 {
00146 shapes::Sphere shape(2.0);
00147 bodies::Body *s = new bodies::Sphere(&shape);
00148 printf("Sphere volume = %f\n", s->computeVolume());
00149
00150 bodies::BoundingSphere sphere;
00151 s->computeBoundingSphere(sphere);
00152
00153 printf("Bounding radius = %f\n", sphere.radius);
00154 for (int i = 0 ; i < TEST_TIMES ; ++i)
00155 {
00156 visualization_msgs::Marker mk;
00157 setShapeTransformAndMarker(s, mk);
00158
00159 mk.type = visualization_msgs::Marker::SPHERE;
00160 mk.scale.x = shape.radius*2.0;
00161 mk.scale.y = shape.radius*2.0;
00162 mk.scale.z = shape.radius*2.0;
00163
00164 m_vmPub.publish(mk);
00165
00166 testShape(s);
00167 }
00168
00169 delete s;
00170 }
00171
00172 void testBox(void)
00173 {
00174 shapes::Box shape(2.0, 1.33, 1.5);
00175 bodies::Body *s = new bodies::Box(&shape);
00176 printf("Box volume = %f\n", s->computeVolume());
00177
00178 bodies::BoundingSphere sphere;
00179 s->computeBoundingSphere(sphere);
00180
00181 printf("Bounding radius = %f\n", sphere.radius);
00182
00183 for (int i = 0 ; i < TEST_TIMES ; ++i)
00184 {
00185 visualization_msgs::Marker mk;
00186 setShapeTransformAndMarker(s, mk);
00187
00188 mk.type = visualization_msgs::Marker::CUBE;
00189 mk.scale.x = shape.size[0];
00190 mk.scale.y = shape.size[1];
00191 mk.scale.z = shape.size[2];
00192
00193 m_vmPub.publish(mk);
00194
00195 testShape(s);
00196 }
00197
00198 delete s;
00199 }
00200
00201 void testCylinder(void)
00202 {
00203 shapes::Cylinder shape(0.5, 2.5);
00204 bodies::Body *s = new bodies::Cylinder(&shape);
00205 printf("Cylinder volume = %f\n", s->computeVolume());
00206
00207 bodies::BoundingSphere sphere;
00208 s->computeBoundingSphere(sphere);
00209
00210 printf("Bounding radius = %f\n", sphere.radius);
00211
00212 for (int i = 0 ; i < TEST_TIMES ; ++i)
00213 {
00214 visualization_msgs::Marker mk;
00215 setShapeTransformAndMarker(s, mk);
00216
00217 mk.type = visualization_msgs::Marker::CUBE;
00218 mk.scale.x = shape.radius * 2.0;
00219 mk.scale.y = shape.radius * 2.0;
00220 mk.scale.z = shape.length;
00221
00222 m_vmPub.publish(mk);
00223
00224 testShape(s);
00225 }
00226
00227 delete s;
00228 }
00229
00230 void testConvexMesh(void)
00231 {
00232 std::vector<btVector3> pts(12);
00233 btVector3 v1(0,0,1);
00234 btVector3 v2(1,0,-0.3);
00235 btVector3 v3(-0.5,0.8,-0.3);
00236 btVector3 v4(-0.5,-0.8,-0.3);
00237
00238 pts[0] = v1;
00239 pts[1] = v2;
00240 pts[2] = v3;
00241
00242 pts[3] = v1;
00243 pts[4] = v4;
00244 pts[5] = v3;
00245
00246 pts[6] = v1;
00247 pts[7] = v4;
00248 pts[8] = v2;
00249
00250 pts[9] = v4;
00251 pts[10] = v2;
00252 pts[11] = v3;
00253
00254
00255 shapes::Mesh *shape = shapes::createMeshFromVertices(pts);
00256 bodies::Body *s = new bodies::ConvexMesh(shape);
00257 printf("Mesh volume = %f\n", s->computeVolume());
00258
00259 bodies::BoundingSphere sphere;
00260 s->computeBoundingSphere(sphere);
00261
00262 printf("Bounding radius = %f\n", sphere.radius);
00263
00264 for (int i = 0 ; i < TEST_TIMES ; ++i)
00265 {
00266 visualization_msgs::Marker mk;
00267 setShapeTransformAndMarker(s, mk);
00268 testShape(s);
00269 }
00270
00271 delete s;
00272 delete shape;
00273 }
00274
00275 void run()
00276 {
00277 ros::Duration d;
00278 d.fromSec(2);
00279 d.sleep();
00280
00281 m_tm = ros::Time::now();
00282
00283
00284
00285 testCylinder();
00286
00287
00288 ROS_INFO("Idling...");
00289 ros::spin();
00290 }
00291
00292 protected:
00293
00294
00295
00296 double uniform(double magnitude)
00297 {
00298 return (2.0 * drand48() - 1.0) * magnitude;
00299 }
00300
00301 ros::NodeHandle m_nodeHandle;
00302 ros::Publisher m_vmPub;
00303 ros::Time m_tm;
00304 int m_id;
00305
00306 };
00307
00308
00309 int main(int argc, char **argv)
00310 {
00311 ros::init(argc, argv, "TVM");
00312
00313 TestVM tvm;
00314 tvm.run();
00315
00316 return 0;
00317 }