point_cloud_tools.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * \file
00003  *
00004  * $Id: PointCloudTools.cpp 556 2012-04-11 16:10:40Z xlokaj03 $
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: Tomas Lokaj (xlokaj03@stud.fit.vutbr.cz)
00011  * Supervised by: Michal Spanel (spanel@fit.vutbr.cz)
00012  * Date: 18/02/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 <srs_ui_but/but_services/point_cloud_tools.h>
00029 
00030 #include <srs_ui_but/topics_list.h>
00031 #include <srs_ui_but/services_list.h>
00032 
00033 namespace srs_ui_but
00034 {
00035 
00036 PointCloudTools::PointCloudTools()
00037 {
00038   tfListener = new tf::TransformListener();
00039 
00040   transform_filter = new tf::MessageFilter<sensor_msgs::PointCloud2>(point_cloud, *tfListener, "/map", 1);
00041 
00042   //point_cloud.subscribe(threaded_nh_, CAMERA_TOPIC, 1);
00043 
00044   transform_filter->registerCallback(boost::bind(&PointCloudTools::incomingCloudCallback, this, _1));
00045 }
00046 
00047 void PointCloudTools::incomingCloudCallback(const sensor_msgs::PointCloud2ConstPtr& cloud)
00048 {
00049   // Store time stamp
00050   pointCloud_stamp = cloud->header.stamp;
00051 
00052   // Transfotm PointCloud2 to PCL PointCloud
00053   pcl::fromROSMsg(*cloud, pcl_pointCloud);
00054 }
00055 
00056 srs_ui_but::ClosestPoint PointCloudTools::getClosestPoint(std::string link)
00057 {
00058   closestPoint.time_stamp = pointCloud_stamp;
00059   closestPoint.status = false;
00060 
00061   try
00062   {
00063     tfListener->waitForTransform(link, CAMERA_LINK, pointCloud_stamp, ros::Duration(0.2));
00064     tfListener->lookupTransform(link, CAMERA_LINK, pointCloud_stamp, sensorToLinkTf);
00065     tfListener->waitForTransform(CAMERA_LINK, link, pointCloud_stamp, ros::Duration(0.2));
00066     tfListener->lookupTransform(CAMERA_LINK, link, pointCloud_stamp, linkToSensorTf);
00067   }
00068   catch (tf::TransformException& ex)
00069   {
00070     ROS_WARN("Transform ERROR");
00071     return closestPoint;
00072   }
00073   transformer.setTransform(linkToSensorTf);
00074 
00075   // Transform link to camera
00076   tf::Stamped<btVector3> p;
00077   p.setX(0);
00078   p.setY(0);
00079   p.setZ(0);
00080   p.frame_id_ = link;
00081   transformer.transformPoint(CAMERA_LINK, p, p);
00082 
00083   float d;
00084   closestPoint.distance = FLT_MAX;
00085 
00086   // Get closest point and distance
00087   BOOST_FOREACH (const pcl::PointXYZ& pt, pcl_pointCloud.points)
00088   {
00089     d = pow((pt.x - p.getX()), 2) + pow((pt.y - p.getY()), 2) + pow((pt.z - p.getZ()), 2);
00090 
00091     if (d < closestPoint.distance)
00092     {
00093       closestPoint.distance = d;
00094       closestPoint.position.x = pt.x;
00095       closestPoint.position.y = pt.y;
00096       closestPoint.position.z = pt.z;
00097     }
00098   }
00099 
00100 // Transform closest point back to link
00101   transformer.setTransform(sensorToLinkTf);
00102   p.setX(closestPoint.position.x);
00103   p.setY(closestPoint.position.y);
00104   p.setZ(closestPoint.position.z);
00105   p.frame_id_ = CAMERA_LINK;
00106   transformer.transformPoint(link, p, p);
00107 
00108   closestPoint.position.x = p.getX();
00109   closestPoint.position.y = p.getY();
00110   closestPoint.position.z = p.getZ();
00111   closestPoint.distance = sqrt(closestPoint.distance);
00112   closestPoint.status = true;
00113 
00114   return closestPoint;
00115 }
00116 }


srs_ui_but
Author(s): Vit Stancl (stancl@fit.vutbr.cz), Michal Spanel (spanel@fit.vutbr.cz), Tomas Lokaj
autogenerated on Mon Oct 6 2014 08:49:30