grasp
All Data Structures Namespaces Functions Modules
visualizationThread.cpp
1 /* Copyright: (C) 2014 iCub Facility - Istituto Italiano di Tecnologia
2  * Authors: Ilaria Gori
3  * email: ilaria.gori@iit.it
4  * Permission is granted to copy, distribute, and/or modify this program
5  * under the terms of the GNU General Public License, version 2 or any
6  * later version published by the Free Software Foundation.
7  *
8  * A copy of the license can be found in the file LICENSE located in the
9  * root directory.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details
15 */
16 
17 #include <string.h>
18 #include <sstream>
19 #include "visualizationThread.h"
20 
21 using namespace std;
22 using namespace yarp::os;
23 using namespace yarp::sig;
24 using namespace iCub::data3D;
25 
26 /************************************************************************/
27 VisualizationThread::VisualizationThread(DataToShow &_data) : data(_data)
28 {
29  running=false;
30 }
31 
32 /************************************************************************/
33 void VisualizationThread::setPosition(int x, int y)
34 {
35  this->x=x;
36  this->y=y;
37 }
38 
39 /************************************************************************/
40 void VisualizationThread::setSize(int sizex, int sizey)
41 {
42  this->sizex=sizex;
43  this->sizey=sizey;
44 }
45 
46 /************************************************************************/
47 void VisualizationThread::run()
48 {
49  boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
50  int scale=15;
51  viewer->initCameraParameters();
52 
53  viewer->setPosition(x,y);
54  if (sizex!=0 && sizey!=0)
55  viewer->setSize(sizex,sizey);
56 
57  BoundingBox bb(data.boundingBox.getBoundingBox());
58  bb.drawBoundingBox(viewer);
59  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPtr(new pcl::PointCloud<pcl::PointXYZRGB> (data.cloud));
60  pcl::PointCloud<pcl::Normal>::Ptr normalPtr(new pcl::PointCloud<pcl::Normal> (data.normals));
61  viewer->addPointCloudNormals<pcl::PointXYZRGB,pcl::Normal>(cloudPtr,normalPtr,5,0.01,"normals");
62  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloudPtr);
63  viewer->addPointCloud<pcl::PointXYZRGB> (cloudPtr, rgb, "rgb");
64  viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "rgb");
65  for (int i=0; i<data.sampled_cloud.size(); i++)
66  {
67  ostringstream temp;
68  temp<<i;
69  string s=temp.str();
70  pcl::PointXYZ point(data.sampled_cloud.at(i).x,data.sampled_cloud.at(i).y, data.sampled_cloud.at(i).z);
71  viewer->addSphere (point, 0.002, 0, 1, 0, "pointScored"+s);
72  }
73  viewer->resetCamera();
74  viewer->setCameraPosition(-0.0611749,-0.040113,0.00667606,-0.105521,0.0891437,0.990413);
75  running=true;
76 
77  while (!viewer->wasStopped())
78  {
79  if (!running)
80  {
81  viewer->close();
82  break;
83  }
84  viewer->spinOnce (100);
85  boost::this_thread::sleep (boost::posix_time::microseconds (100000));
86  }
87 }
88 
89 /************************************************************************/
90 void VisualizationThread::onStop()
91 {
92  running=false;
93 }
The Definition of the BoundingBox class.
Definition: boundingBox.h:61
void drawBoundingBox(boost::shared_ptr< pcl::visualization::PCLVisualizer > viewer, int viewport=0)
Draw a 3D box in a PCLVisualizer window.