objtree_plugin.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * \file
00003  *
00004  * $Id:$
00005  *
00006  * Copyright (C) Brno University of Technology
00007  *
00008  * This file is part of software developed by dcgm-robotics@FIT group.
00009  *
00010  * Author: Jan Gorig (xgorig01@stud.fit.vutbr.cz)
00011  * Supervised by: Michal Spanel (spanel@fit.vutbr.cz)
00012  * Date: dd/mm/2012
00013  * 
00014  * This file is free software: you can redistribute it and/or modify
00015  * it under the terms of the GNU Lesser General Public License as published by
00016  * the Free Software Foundation, either version 3 of the License, or
00017  * (at your option) any later version.
00018  * 
00019  * This file is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public License
00025  * along with this file.  If not, see <http://www.gnu.org/licenses/>.
00026  */
00027 
00028 #include <Eigen/Geometry>
00029 
00030 #include <srs_env_model/services_list.h>
00031 #include <srs_env_model/topics_list.h>
00032 #include <srs_env_model/but_server/plugins/objtree_plugin.h>
00033 #include <srs_env_model/but_server/objtree/plane.h>
00034 #include <srs_env_model/but_server/objtree/gbbox.h>
00035 #include <srs_env_model/but_server/objtree/bbox.h>
00036 #include <srs_env_model/but_server/objtree/filter.h>
00037 
00038 #include <srs_interaction_primitives/bounding_box.h>
00039 #include <srs_interaction_primitives/plane.h>
00040 
00041 static const Eigen::Vector3f upVector(0.0f, 0.0f, 1.0f);
00042 
00043 
00044 namespace srs_env_model
00045 {
00046 
00047 CObjTreePlugin::CObjTreePlugin(const std::string &name)
00048     : CServerPluginBase(name), m_octree(objtree::Box(-10.0f, -10.0f, -10.0f, 20.0f, 20.0f, 20.0f))
00049 {
00050 }
00051 
00052 CObjTreePlugin::~CObjTreePlugin()
00053 {
00054     reset();
00055 }
00056 
00057 void CObjTreePlugin::init(ros::NodeHandle &node_handle)
00058 {
00059     //Advertise services
00060     m_serviceGetObjectsInBox = node_handle.advertiseService(GetObjectsInBox_SRV, &CObjTreePlugin::srvGetObjectsInBox, this);
00061     m_serviceGetObjectsInHalfspace = node_handle.advertiseService(GetObjectsInHalfspace_SRV, &CObjTreePlugin::srvGetObjectsInHalfspace, this);
00062     m_serviceGetObjectsInSphere = node_handle.advertiseService(GetObjectsInSphere_SRV, &CObjTreePlugin::srvGetObjectsInSphere, this);
00063     m_serviceGetPlane = node_handle.advertiseService(GetPlane_SRV, &CObjTreePlugin::srvGetPlane, this);
00064     m_serviceGetABox = node_handle.advertiseService(GetAlignedBox_SRV, &CObjTreePlugin::srvGetABox, this);
00065     m_serviceGetBBox = node_handle.advertiseService(GetBoundingBox_SRV, &CObjTreePlugin::srvGetBBox, this);
00066     m_serviceInsertPlane = node_handle.advertiseService(InsertPlane_SRV, &CObjTreePlugin::srvInsertPlane, this);
00067     m_serviceInsertPlaneByPosition = node_handle.advertiseService(InsertPlaneByPosition_SRV, &CObjTreePlugin::srvInsertPlaneByPosition, this);
00068     m_serviceGetSimilarPlane = node_handle.advertiseService(GetSimilarPlane_SRV, &CObjTreePlugin::srvGetSimilarPlane, this);
00069     m_serviceInsertPlanes = node_handle.advertiseService(InsertPlanes_SRV, &CObjTreePlugin::srvInsertPlanes, this);
00070     m_serviceInsertABox = node_handle.advertiseService(InsertAlignedBox_SRV, &CObjTreePlugin::srvInsertABox, this);
00071     m_serviceInsertABoxByPosition = node_handle.advertiseService(InsertAlignedBoxByPosition_SRV, &CObjTreePlugin::srvInsertABoxByPosition, this);
00072     m_serviceInsertBBox = node_handle.advertiseService(InsertBoundingBox_SRV, &CObjTreePlugin::srvInsertBBox, this);
00073     m_serviceInsertBBoxByPosition = node_handle.advertiseService(InsertBoundingBoxByPosition_SRV, &CObjTreePlugin::srvInsertBBoxByPosition, this);
00074     m_serviceGetSimilarABox = node_handle.advertiseService(GetSimilarAlignedBox_SRV, &CObjTreePlugin::srvGetSimilarABox, this);
00075     m_serviceGetSimilarBBox = node_handle.advertiseService(GetSimilarBoundingBox_SRV, &CObjTreePlugin::srvGetSimilarBBox, this);
00076     m_serviceRemoveObject = node_handle.advertiseService(RemoveObject_SRV, &CObjTreePlugin::srvRemoveObject, this);
00077     m_serviceShowObject = node_handle.advertiseService(ShowObject_SRV, &CObjTreePlugin::srvShowObject, this);
00078     m_serviceShowObjtree = node_handle.advertiseService(ShowObjtree_SRV, &CObjTreePlugin::srvShowObjtree, this);
00079 
00080     m_clientAddPlane = node_handle.serviceClient<srs_interaction_primitives::AddPlane>(srs_interaction_primitives::AddPlane_SRV);
00081     m_clientAddBoundingBox = node_handle.serviceClient<srs_interaction_primitives::AddBoundingBox>(srs_interaction_primitives::AddBoundingBox_SRV);
00082     m_clientRemovePrimitive = node_handle.serviceClient<srs_interaction_primitives::RemovePrimitive>(srs_interaction_primitives::RemovePrimitive_SRV);
00083 
00084     m_markerPub = node_handle.advertise<visualization_msgs::Marker>("visualization_marker", 5);
00085 
00086     printf("ObjTree plugin initialized!\n");
00087 }
00088 
00089 void CObjTreePlugin::reset()
00090 {
00091     std::map<unsigned int, objtree::Object*> objects(m_octree.objectsAll());
00092 
00093     for(std::map<unsigned int, objtree::Object*>::iterator i = objects.begin(); i != objects.end(); i++)
00094     {
00095         removePrimitiveMarker(i->second->id());
00096     }
00097 
00098     m_octree.clear();
00099 }
00100 
00101 bool CObjTreePlugin::srvInsertPlane(srs_env_model::InsertPlane::Request &req, srs_env_model::InsertPlane::Response &res)
00102 {
00103     res.object_id = insertPlane(req.plane, INSERT);
00104 
00105     showObject(res.object_id);
00106 
00107     return true;
00108 }
00109 
00110 bool CObjTreePlugin::srvInsertABox(srs_env_model::InsertAlignedBox::Request &req, srs_env_model::InsertAlignedBox::Response &res)
00111 {
00112     res.object_id = insertABox(req.object_id, req.position, req.scale, INSERT);
00113 
00114     showObject(res.object_id);
00115 
00116     return true;
00117 }
00118 
00119 bool CObjTreePlugin::srvInsertBBox(srs_env_model::InsertBoundingBox::Request &req, srs_env_model::InsertBoundingBox::Response &res)
00120 {
00121     res.object_id = insertBBox(req.object_id, req.pose, req.scale, INSERT);
00122 
00123     showObject(res.object_id);
00124 
00125     return true;
00126 }
00127 
00128 bool CObjTreePlugin::srvInsertPlaneByPosition(srs_env_model::InsertPlane::Request &req, srs_env_model::InsertPlane::Response &res)
00129 {
00130     if(m_octree.removeObject(req.plane.id))
00131         removePrimitiveMarker(req.plane.id);
00132 
00133     res.object_id = insertPlane(req.plane, UPDATE);
00134 
00135     if((unsigned int)req.plane.id != res.object_id)
00136         removePrimitiveMarker(res.object_id);
00137 
00138     showObject(res.object_id);
00139 
00140     return true;
00141 }
00142 
00143 bool CObjTreePlugin::srvInsertABoxByPosition(srs_env_model::InsertAlignedBox::Request &req, srs_env_model::InsertAlignedBox::Response &res)
00144 {
00145     if(m_octree.removeObject(req.object_id))
00146         removePrimitiveMarker(req.object_id);
00147 
00148     res.object_id = insertABox(req.object_id, req.position, req.scale, UPDATE);
00149 
00150     if((unsigned int)req.object_id != res.object_id)
00151         removePrimitiveMarker(res.object_id);
00152 
00153     showObject(res.object_id);
00154 
00155     return true;
00156 }
00157 
00158 bool CObjTreePlugin::srvInsertBBoxByPosition(srs_env_model::InsertBoundingBox::Request &req, srs_env_model::InsertBoundingBox::Response &res)
00159 {
00160     if(m_octree.removeObject(req.object_id))
00161         removePrimitiveMarker(req.object_id);
00162 
00163     res.object_id = insertBBox(req.object_id, req.pose, req.scale, UPDATE);
00164 
00165     if((unsigned int)req.object_id != res.object_id)
00166         removePrimitiveMarker(res.object_id);
00167 
00168     showObject(res.object_id);
00169 
00170     return true;
00171 }
00172 
00173 bool CObjTreePlugin::srvGetSimilarPlane(srs_env_model::InsertPlane::Request &req, srs_env_model::InsertPlane::Response &res)
00174 {
00175     res.object_id = insertPlane(req.plane, GET_SIMILAR);
00176 
00177     return true;
00178 }
00179 
00180 bool CObjTreePlugin::srvGetSimilarABox(srs_env_model::InsertAlignedBox::Request &req, srs_env_model::InsertAlignedBox::Response &res)
00181 {
00182     res.object_id = insertABox(req.object_id, req.position, req.scale, GET_SIMILAR);
00183 
00184     return true;
00185 }
00186 
00187 bool CObjTreePlugin::srvGetSimilarBBox(srs_env_model::InsertBoundingBox::Request &req, srs_env_model::InsertBoundingBox::Response &res)
00188 {
00189     res.object_id = insertBBox(req.object_id, req.pose, req.scale, GET_SIMILAR);
00190 
00191     return true;
00192 }
00193 
00194 bool CObjTreePlugin::srvInsertPlanes(srs_env_model::InsertPlanes::Request &req, srs_env_model::InsertPlanes::Response &res)
00195 {
00196     std::vector<srs_env_model_msgs::PlaneDesc>::iterator i;
00197     std::vector<srs_env_model_msgs::PlaneDesc> &planes(req.plane_array.planes);
00198 
00199     for(i = planes.begin(); i != planes.end(); i++)
00200     {
00201         unsigned int id = insertPlane(*i, INSERT);
00202         res.object_ids.push_back(id);
00203 
00204         showObject(id);
00205     }
00206 
00207     return true;
00208 }
00209 
00210 bool CObjTreePlugin::srvShowObject(srs_env_model::ShowObject::Request &req, srs_env_model::ShowObject::Response &res)
00211 {
00212     showObject(req.object_id);
00213 
00214     return true;
00215 }
00216 
00217 bool CObjTreePlugin::srvRemoveObject(srs_env_model::RemoveObject::Request &req, srs_env_model::RemoveObject::Response &res)
00218 {
00219     removeObject(req.object_id);
00220 
00221     return true;
00222 }
00223 
00224 bool CObjTreePlugin::srvShowObjtree(srs_env_model::ShowObjtree::Request &req, srs_env_model::ShowObjtree::Response &res)
00225 {
00226     showObjtree();
00227 
00228     return true;
00229 }
00230 
00231 bool CObjTreePlugin::srvGetPlane(srs_env_model::GetPlane::Request &req, srs_env_model::GetPlane::Response &res)
00232 {
00233     const objtree::Object *object = m_octree.object(req.object_id);
00234     //Object hasn't been found
00235     if(!object) return true;
00236     if(object->type() != objtree::Object::PLANE) return true;
00237 
00238     const objtree::Plane *plane = (const objtree::Plane*)object;
00239 
00240     res.plane.id = req.object_id;
00241     res.plane.pose.position.x = plane->pos().x;
00242     res.plane.pose.position.y = plane->pos().y;
00243     res.plane.pose.position.z = plane->pos().z;
00244 
00245     //Quaternion from normal
00246     Eigen::Vector3f normal(plane->normal().x, plane->normal().y, plane->normal().z);
00247     Eigen::Quaternionf q;
00248     q.setFromTwoVectors(upVector, normal.normalized());
00249 
00250     res.plane.pose.orientation.x = q.x();
00251     res.plane.pose.orientation.y = q.y();
00252     res.plane.pose.orientation.z = q.z();
00253     res.plane.pose.orientation.w = q.w();
00254 
00255     res.plane.scale.x = plane->boundingMax().x-plane->boundingMin().x;
00256     res.plane.scale.y = plane->boundingMax().y-plane->boundingMin().y;
00257     res.plane.scale.z = plane->boundingMax().z-plane->boundingMin().z;
00258 
00259     return true;
00260 }
00261 
00262 bool CObjTreePlugin::srvGetABox(srs_env_model::GetAlignedBox::Request &req, srs_env_model::GetAlignedBox::Response &res)
00263 {
00264     const objtree::Object *object = m_octree.object(req.object_id);
00265     //Object hasn't been found
00266     if(!object) return true;
00267     if(object->type() != objtree::Object::ALIGNED_BOUNDING_BOX) return true;
00268 
00269     const objtree::BBox *box = (const objtree::BBox*)object;
00270 
00271     res.position.x = box->box().x;
00272     res.position.y = box->box().y;
00273     res.position.z = box->box().z;
00274 
00275     res.scale.x = box->box().w;
00276     res.scale.y = box->box().h;
00277     res.scale.z = box->box().d;
00278 
00279     return true;
00280 }
00281 
00282 bool CObjTreePlugin::srvGetBBox(srs_env_model::GetBoundingBox::Request &req, srs_env_model::GetBoundingBox::Response &res)
00283 {
00284     const objtree::Object *object = m_octree.object(req.object_id);
00285     //Object hasn't been found
00286     if(!object) return true;
00287     if(object->type() != objtree::Object::GENERAL_BOUNDING_BOX) return true;
00288 
00289     const objtree::GBBox *box = (const objtree::GBBox*)object;
00290 
00291     res.pose.position.x = box->position().x;
00292     res.pose.position.y = box->position().y;
00293     res.pose.position.z = box->position().z;
00294 
00295     res.pose.orientation.x = box->orientation().x;
00296     res.pose.orientation.y = box->orientation().y;
00297     res.pose.orientation.z = box->orientation().z;
00298     res.pose.orientation.w = box->orientation().w;
00299 
00300     res.scale.x = box->scale().x;
00301     res.scale.y = box->scale().y;
00302     res.scale.z = box->scale().z;
00303 
00304     return true;
00305 }
00306 
00307 bool CObjTreePlugin::srvGetObjectsInBox(srs_env_model::GetObjectsInBox::Request &req, srs_env_model::GetObjectsInBox::Response &res)
00308 {
00309     objtree::FilterBox filter(objtree::Box(req.position.x, req.position.y, req.position.z, req.size.x, req.size.y, req.size.z));
00310     getObjects(&filter, res.object_ids);
00311 
00312     return true;
00313 }
00314 
00315 bool CObjTreePlugin::srvGetObjectsInHalfspace(srs_env_model::GetObjectsInHalfspace::Request &req, srs_env_model::GetObjectsInHalfspace::Response &res)
00316 {
00317     objtree::FilterPlane filter(req.position.x, req.position.y, req.position.z, req.normal.x, req.normal.y, req.normal.z);
00318     getObjects(&filter, res.object_ids);
00319 
00320     return true;
00321 }
00322 
00323 bool CObjTreePlugin::srvGetObjectsInSphere(srs_env_model::GetObjectsInSphere::Request &req, srs_env_model::GetObjectsInSphere::Response &res)
00324 {
00325     objtree::FilterSphere filter(req.position.x, req.position.y, req.position.z, req.radius);
00326     getObjects(&filter, res.object_ids);
00327 
00328     return true;
00329 }
00330 
00331 unsigned int CObjTreePlugin::insertPlane(const srs_env_model_msgs::PlaneDesc &plane, CObjTreePlugin::Operation op)
00332 {
00333     printf("insertPlane called, mode %d\n", op);
00334 
00335     if(op == INSERT && m_octree.removeObject(plane.id))
00336     {
00337         //Updating existing plane
00338         removePrimitiveMarker(plane.id);
00339     }
00340 
00341     objtree::Point pos(plane.pose.position.x, plane.pose.position.y, plane.pose.position.z);
00342     objtree::Point scale(plane.scale.x, plane.scale.y, plane.scale.z);
00343 
00344     //Normal from orientation - quaternion
00345     //Quaternion must be normalized!
00346     Eigen::Quaternionf q(plane.pose.orientation.w, plane.pose.orientation.x, plane.pose.orientation.y, plane.pose.orientation.z);
00347     Eigen::Vector3f normal = q*upVector;
00348 
00349     objtree::Plane *newPlane = new objtree::Plane(pos, objtree::Vector(normal.x(), normal.y(), normal.z()), scale);
00350     newPlane->setId(plane.id);
00351 
00352     switch(op)
00353     {
00354         case INSERT: return m_octree.insert(newPlane);
00355         case UPDATE: return m_octree.insertUpdate(newPlane);
00356         case GET_SIMILAR:
00357         {
00358             unsigned int id = -1;
00359             const objtree::Object *object = m_octree.getSimilarObject(newPlane);
00360             if(object)
00361             {
00362                 id = object->id();
00363             }
00364 
00365             delete newPlane;
00366             return id;
00367         }
00368     }
00369 
00370     return -1;
00371 }
00372 
00373 unsigned int CObjTreePlugin::insertABox(unsigned int id, const geometry_msgs::Point32 &position, const geometry_msgs::Vector3 &scale, CObjTreePlugin::Operation op)
00374 {
00375     printf("insertAlignedBox called, mode %d\n", op);
00376 
00377     if(op == INSERT && m_octree.removeObject(id))
00378     {
00379         //Updating existing box
00380         removePrimitiveMarker(id);
00381     }
00382 
00383     objtree::BBox *newBox = new objtree::BBox(objtree::Box(position.x, position.y, position.z, scale.x, scale.y, scale.z));
00384     newBox->setId(id);
00385 
00386     switch(op)
00387     {
00388         case INSERT: return m_octree.insert(newBox);
00389         case UPDATE: return m_octree.insertUpdate(newBox);
00390         case GET_SIMILAR:
00391         {
00392             unsigned int id = -1;
00393             const objtree::Object *object = m_octree.getSimilarObject(newBox);
00394             if(object)
00395             {
00396                 id = object->id();
00397             }
00398 
00399             delete newBox;
00400             return id;
00401         }
00402     }
00403 
00404     return -1;
00405 }
00406 
00407 unsigned int CObjTreePlugin::insertBBox(unsigned int id, const geometry_msgs::Pose &pose, const geometry_msgs::Vector3 &scale, CObjTreePlugin::Operation op)
00408 {
00409     printf("insertBoundingBox called, mode %d\n", op);
00410 
00411     if(op == INSERT && m_octree.removeObject(id))
00412     {
00413         //Updating existing box
00414         removePrimitiveMarker(id);
00415     }
00416 
00417     objtree::Point newPosition(pose.position.x, pose.position.y, pose.position.z);
00418     objtree::Vector4f newOrientation(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w);
00419     objtree::Point newScale(scale.x, scale.y, scale.z);
00420 
00421     //Compute minimal aligned bounding box
00422     Eigen::Vector3f min, max;
00423     Eigen::Vector3f vec[4];
00424     vec[0] = Eigen::Vector3f(-scale.x/2.0f, -scale.y/2.0f, -scale.z/2.0f);
00425     vec[1] = Eigen::Vector3f(-scale.x/2.0f,  scale.y/2.0f, -scale.z/2.0f);
00426     vec[2] = Eigen::Vector3f( scale.x/2.0f, -scale.y/2.0f, -scale.z/2.0f);
00427     vec[3] = Eigen::Vector3f( scale.x/2.0f,  scale.y/2.0f, -scale.z/2.0f);
00428 
00429     Eigen::Quaternionf q(pose.orientation.w, pose.orientation.x, pose.orientation.y, pose.orientation.z);
00430 
00431     min = max = q*vec[0];
00432 
00433     for(int i = 1; i < 4; i++)
00434     {
00435         vec[i] = q*vec[i];
00436 
00437         for(int j = 0; j < 3; j++)
00438         {
00439             if(min[j] > vec[i][j]) min[j] = vec[i][j];
00440             if(max[j] < vec[i][j]) max[j] = vec[i][j];
00441         }
00442     }
00443 
00444     for(int i = 0; i < 4; i++)
00445     {
00446         vec[i] = -vec[i];
00447 
00448         for(int j = 0; j < 3; j++)
00449         {
00450             if(min[j] > vec[i][j]) min[j] = vec[i][j];
00451             if(max[j] < vec[i][j]) max[j] = vec[i][j];
00452         }
00453     }
00454 
00455     Eigen::Vector3f alignedScale(max-min);
00456     objtree::Box alignedBox(min[0]+newPosition.x, min[1]+newPosition.y, min[2]+newPosition.z, alignedScale[0], alignedScale[1], alignedScale[2]);
00457 
00458     objtree::GBBox *newBox = new objtree::GBBox(newPosition, newOrientation, newScale, alignedBox);
00459     newBox->setId(id);
00460 
00461     switch(op)
00462     {
00463         case INSERT: return m_octree.insert(newBox);
00464         case UPDATE: return m_octree.insertUpdate(newBox);
00465         case GET_SIMILAR:
00466         {
00467             unsigned int id = -1;
00468             const objtree::Object *object = m_octree.getSimilarObject(newBox);
00469             if(object)
00470             {
00471                 id = object->id();
00472             }
00473 
00474             delete newBox;
00475             return id;
00476         }
00477     }
00478 
00479     return -1;
00480 }
00481 
00482 void CObjTreePlugin::showObject(unsigned int id)
00483 {
00484     const objtree::Object *object = m_octree.object(id);
00485     if(!object) return;
00486 
00487     char name[64];
00488     snprintf(name, sizeof(name), "imn%u", id);
00489 
00490     switch(object->type())
00491     {
00492         case objtree::Object::ALIGNED_BOUNDING_BOX:
00493         {
00494             objtree::BBox *box = (objtree::BBox*)object;
00495 
00496             srs_interaction_primitives::AddBoundingBox addBoxSrv;
00497 
00498             addBoxSrv.request.frame_id = IM_SERVER_FRAME_ID;
00499             addBoxSrv.request.name = name;
00500             addBoxSrv.request.description = name;
00501 
00502             addBoxSrv.request.pose.position.x = box->box().x+box->box().w/2;
00503             addBoxSrv.request.pose.position.y = box->box().y+box->box().h/2;
00504             addBoxSrv.request.pose.position.z = box->box().z+box->box().d/2;
00505 
00506             addBoxSrv.request.pose.orientation.x = 0.0f;
00507             addBoxSrv.request.pose.orientation.y = 0.0f;
00508             addBoxSrv.request.pose.orientation.z = 0.0f;
00509             addBoxSrv.request.pose.orientation.w = 1.0f;
00510 
00511             addBoxSrv.request.scale.x = box->box().w;
00512             addBoxSrv.request.scale.y = box->box().h;
00513             addBoxSrv.request.scale.z = box->box().d;
00514 
00515             addBoxSrv.request.color.r = 1.0;
00516             addBoxSrv.request.color.g = addBoxSrv.request.color.b = 0.0;
00517 
00518             addBoxSrv.request.color.a = 1.0;
00519 
00520             m_clientAddBoundingBox.call(addBoxSrv);
00521         }
00522         break;
00523 
00524         case objtree::Object::GENERAL_BOUNDING_BOX:
00525         {
00526             objtree::GBBox *box = (objtree::GBBox*)object;
00527 
00528             srs_interaction_primitives::AddBoundingBox addBoxSrv;
00529 
00530             addBoxSrv.request.frame_id = IM_SERVER_FRAME_ID;
00531             addBoxSrv.request.name = name;
00532             addBoxSrv.request.description = name;
00533 
00534             addBoxSrv.request.pose.position.x = box->position().x;
00535             addBoxSrv.request.pose.position.y = box->position().y;
00536             addBoxSrv.request.pose.position.z = box->position().z;
00537 
00538             addBoxSrv.request.pose.orientation.x = box->orientation().x;
00539             addBoxSrv.request.pose.orientation.y = box->orientation().y;
00540             addBoxSrv.request.pose.orientation.z = box->orientation().z;
00541             addBoxSrv.request.pose.orientation.w = box->orientation().w;
00542 
00543             addBoxSrv.request.scale.x = box->scale().x;
00544             addBoxSrv.request.scale.y = box->scale().y;
00545             addBoxSrv.request.scale.z = box->scale().z;
00546 
00547             addBoxSrv.request.color.r = 1.0;
00548             addBoxSrv.request.color.g = addBoxSrv.request.color.b = 0.0;
00549 
00550             addBoxSrv.request.color.a = 1.0;
00551 
00552             m_clientAddBoundingBox.call(addBoxSrv);
00553         }
00554         break;
00555 
00556         case objtree::Object::PLANE:
00557         {
00558             objtree::Plane *plane = (objtree::Plane*)object;
00559 
00560             srs_interaction_primitives::AddPlane addPlaneSrv;
00561 
00562             addPlaneSrv.request.frame_id = IM_SERVER_FRAME_ID;
00563             addPlaneSrv.request.name = name;
00564             addPlaneSrv.request.description = name;
00565 
00566             addPlaneSrv.request.pose.position.x = plane->pos().x;
00567             addPlaneSrv.request.pose.position.y = plane->pos().y;
00568             addPlaneSrv.request.pose.position.z = plane->pos().z;
00569 
00570             //Quaternion from normal
00571             Eigen::Vector3f normal(plane->normal().x, plane->normal().y, plane->normal().z);
00572             Eigen::Quaternionf q;
00573             q.setFromTwoVectors(upVector, normal.normalized());
00574 
00575             addPlaneSrv.request.pose.orientation.x = q.x();
00576             addPlaneSrv.request.pose.orientation.y = q.y();
00577             addPlaneSrv.request.pose.orientation.z = q.z();
00578             addPlaneSrv.request.pose.orientation.w = q.w();
00579 
00580             addPlaneSrv.request.scale.x = plane->boundingMax().x-plane->boundingMin().x;
00581             addPlaneSrv.request.scale.y = plane->boundingMax().y-plane->boundingMin().y;
00582             addPlaneSrv.request.scale.z = plane->boundingMax().z-plane->boundingMin().z;
00583 
00584             addPlaneSrv.request.color.r = 1.0;
00585             addPlaneSrv.request.color.g = addPlaneSrv.request.color.b = 0.0;
00586             addPlaneSrv.request.color.a = 1.0;
00587 
00588             m_clientAddPlane.call(addPlaneSrv);
00589         }
00590         break;
00591     }
00592 }
00593 
00594 void CObjTreePlugin::removeObject(unsigned int id)
00595 {
00596     m_octree.removeObject(id);
00597     removePrimitiveMarker(id);
00598 }
00599 
00600 void CObjTreePlugin::showObjtree()
00601 {
00602     std::set<objtree::Object*> objects;
00603     std::list<objtree::Box> nodes;
00604     objtree::FilterZero filter;
00605 
00606     m_octree.nodes(nodes, objects, &filter);
00607 
00608     publishOctree(nodes);
00609 
00610     printf("Number of objects %zd\n", objects.size());
00611 }
00612 
00613 void CObjTreePlugin::getObjects(const objtree::Filter *filter, std::vector<unsigned int> &output)
00614 {
00615     std::set<objtree::Object*> objects;
00616 
00617     m_octree.objects(objects, filter);
00618     output.resize(objects.size());
00619 
00620     unsigned int i = 0;
00621     for(std::set<objtree::Object*>::iterator it = objects.begin(); it != objects.end(); it++, i++)
00622     {
00623         output[i] = (*it)->id();
00624     }
00625 }
00626 
00627 //Methods for octree visualization
00628 
00629 void CObjTreePlugin::publishLine(visualization_msgs::Marker &lines, float x1, float y1, float z1, float x2, float y2, float z2)
00630 {
00631     geometry_msgs::Point p;
00632 
00633     p.x = x1;
00634     p.y = y1;
00635     p.z = z1;
00636 
00637     lines.points.push_back(p);
00638 
00639     p.x = x2;
00640     p.y = y2;
00641     p.z = z2;
00642 
00643     lines.points.push_back(p);
00644 }
00645 
00646 void CObjTreePlugin::publishCube(visualization_msgs::Marker &lines, float x, float y, float z, float w, float h, float d)
00647 {
00648     publishLine(lines, x, y, z, x+w, y, z);
00649     publishLine(lines, x, y+h, z, x+w, y+h, z);
00650     publishLine(lines, x, y, z+d, x+w, y, z+d);
00651     publishLine(lines, x, y+h, z+d, x+w, y+h, z+d);
00652 
00653     publishLine(lines, x, y, z, x, y+h, z);
00654     publishLine(lines, x+w, y, z, x+w, y+h, z);
00655     publishLine(lines, x, y, z+d, x, y+h, z+d);
00656     publishLine(lines, x+w, y, z+d, x+w, y+h, z+d);
00657 
00658     publishLine(lines, x, y, z, x, y, z+d);
00659     publishLine(lines, x+w, y, z, x+w, y, z+d);
00660     publishLine(lines, x, y+h, z, x, y+h, z+d);
00661     publishLine(lines, x+w, y+h, z, x+w, y+h, z+d);
00662 }
00663 
00664 void CObjTreePlugin::removePrimitiveMarker(unsigned int id)
00665 {
00666     srs_interaction_primitives::RemovePrimitive removePrimitiveSrv;
00667     char name[64];
00668     snprintf(name, sizeof(name), "imn%u", id);
00669     removePrimitiveSrv.request.name = name;
00670 
00671     m_clientRemovePrimitive.call(removePrimitiveSrv);
00672 }
00673 
00674 void CObjTreePlugin::publishOctree(const std::list<objtree::Box> &nodes)
00675 {
00676     visualization_msgs::Marker lines;
00677 
00678     lines.header.frame_id = IM_SERVER_FRAME_ID;
00679     lines.header.stamp = ros::Time::now();
00680     lines.ns = "objtree";
00681     lines.action = visualization_msgs::Marker::ADD;
00682     lines.pose.orientation.w = 1.0f;
00683 
00684     lines.id = 0;
00685     lines.type = visualization_msgs::Marker::LINE_LIST;
00686     lines.color.r = lines.color.g = lines.color.b = 0.75f;
00687     lines.color.a = 1.0f;
00688 
00689     lines.scale.x = 0.03f;
00690 
00691     for(std::list<objtree::Box>::const_iterator i = nodes.begin(); i != nodes.end(); i++)
00692     {
00693         publishCube(lines, i->x, i->y, i->z, i->w, i->h, i->d);
00694     }
00695 
00696     m_markerPub.publish(lines);
00697 }
00698 
00702 void CObjTreePlugin::pause( bool bPause, ros::NodeHandle & node_handle )
00703 {
00704         if( bPause )
00705                 m_markerPub.shutdown();
00706         else
00707                 m_markerPub = node_handle.advertise<visualization_msgs::Marker>("visualization_marker", 5);
00708 }
00709 
00710 }


srs_env_model
Author(s): Vit Stancl (stancl@fit.vutbr.cz), Tomas Lokaj, Jan Gorig, Michal Spanel (spanel@fit.vutbr.cz)
autogenerated on Mon Oct 6 2014 08:05:05