polygon_primitive_2dhulls.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
36 #ifndef _polygon_primitive_2dhulls_CPP_
37 #define _polygon_primitive_2dhulls_CPP_
38 
39 
40 #include "polygon_primitive.h"
41 
42 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
43 #include <CGAL/convex_hull_2.h>
44 #include<CGAL/Polygon_2.h>
45 
53 void c_polygon_primitive::compute_convex_hull(const pcl::PointCloud<pcl::PointXYZ>::Ptr pcin, const pcl::ModelCoefficients::Ptr coeff, pcl::PointCloud<pcl::PointXYZ>::Ptr pcout)
54 {
55  //To compute the convex hull the following steps are required:
56  //1. project the points from the input point cloud pc_in to the plane model (coeff)
57  //2. Compute the convex hull on these projected points and store to pcout
58 
59  pcl::PointCloud<pcl::PointXYZ>::Ptr pc_projected = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>); //create the projection cloud
60 
61  //STEP 1. the projection
62  project_pc_to_plane(pcin, coeff, pc_projected);
63 
64  //STEP 2. Conputing the convex hull
65  pcl::ConvexHull<pcl::PointXYZ> chull; //declare the object
66  chull.setInputCloud(pc_projected); //set the input point cloud
67  chull.reconstruct(*pcout); //compute the convex hull
68  pcout->width = pcin->width; //set the same width
69  pcout->height = pcin->height; //set the same height
70  pcout->header.frame_id = pcin->header.frame_id; //set the same frame_id
71 
72  pc_projected.reset(); //reset the pc_projected
73 }
74 
85 void c_polygon_primitive::compute_convex_hull_cgal(const pcl::PointCloud<pcl::PointXYZ>::Ptr pcin, const pcl::ModelCoefficients::Ptr coeff, pcl::PointCloud<pcl::PointXYZ>::Ptr pcout, tf::Transform *tr)
86 {
87  double area,solidity;
88  compute_convex_hull_cgal(pcin, coeff, pcout, tr, area, solidity);
89 }
90 
91 
92 void c_polygon_primitive::compute_convex_hull_cgal(const pcl::PointCloud<pcl::PointXYZ>::Ptr pcin, const pcl::ModelCoefficients::Ptr coeff, pcl::PointCloud<pcl::PointXYZ>::Ptr pcout, tf::Transform *tr, double &area, double &solidity,bool flg)
93 {
94  //To compute the convex hull the following steps are required:
95  //1. project the points from the input point cloud pc_in to the plane model (coeff)
96  //2. Compute the convex hull on these projected points and store to pcout
97 
98  pcl::PointCloud<pcl::PointXYZ>::Ptr pc_projected = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>); //create the projection cloud
99  pcl::PointCloud<pcl::PointXYZ>::Ptr pc_local = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
100  pcl::PointCloud<pcl::PointXYZ>::Ptr pc_ch_local = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>);
101 
102  //STEP 1. the projection
103  project_pc_to_plane(pcin, coeff, pc_projected);
104 
105  if(flg)
106  {
107  pointclouds.projected = pc_projected;
108  }
109 
110  //STEP2. transforming to local frame
111 
112  //the given transform goes from local to global. We want to change from global to local. Hence we use the inverse transform
113  pcl_ros::transformPointCloud(*pc_projected, *pc_local, data.frames.current.transform.inverse());
114 
115 
116  //printf("Reference coeff has A=%f B=%f C=%f D=%f\n",data.planes.current->values[0],data.planes.current->values[1],data.planes.current->values[2],data.planes.current->values[3]);
117  //printf("ALL has %d points\n",(int) pcin->points.size());
118  //printf("Projected has %d points\n",(int) pc_projected->points.size());
119  //printf("Local has %d points\n",(int) pc_local->points.size());
120  //for (size_t i=0; i<data.hulls.convex.polygon->points.size(); i++)
121  //{
122  //printf("ALL pt[%d] x=%3.5f y=%3.5f z=%3.5f\n",(int)i, pcin->points[i].x, pcin->points[i].y, pcin->points[i].z);
123  //printf("PROJECTED pt[%d] x=%3.5f y=%3.5f z=%3.5f\n",(int)i, pc_projected->points[i].x, pc_projected->points[i].y, pc_projected->points[i].z);
124  //printf("LOCAL pt[%d] x=%3.5f y=%3.5f z=%3.5f\n",(int)i, pc_local->points[i].x, pc_local->points[i].y, pc_local->points[i].z);
125 
126  //}
127 
128 
129  //STEP3. Copy the local to a pts vector
130  std::vector<CGAL::Exact_predicates_inexact_constructions_kernel::Point_2> pts; //The polygon
131 
132  //Setup the polygon with the points from local
133  for (int i=0; i< (int) pc_local->size(); i++)
134  {
135  pts.push_back(CGAL::Exact_predicates_inexact_constructions_kernel::Point_2(pc_local->points[i].x, pc_local->points[i].y));
136  }
137 
138 
139  std::vector<CGAL::Exact_predicates_inexact_constructions_kernel::Point_2> pts_ch; //The ch polygon
140 
141  //STEP4. Conpute the convex hull
142  area = compute_polygon_area(pc_ch_local);
143  data.hulls.convex.solidity = (double)(pcin->points.size())/area;
144 
145 
146 
147  CGAL::convex_hull_2( pts.begin(), pts.end(),std::back_inserter(pts_ch));
148 
149 
150 
151  for (int t = 0; t<(int)pts_ch.size(); t++ )
152  {
153  pc_ch_local->points.push_back(pcl::PointXYZ(pts_ch[t].x(), pts_ch[t].y(),0.0));
154  }
155 
156  //STEP4. Compute the area
157  data.hulls.convex.area = compute_polygon_area(pc_ch_local);
158  data.hulls.convex.solidity = (double)(pointclouds.all->points.size())/data.hulls.convex.area;
159 
160 
161 
162  //STEP 3. Transform pc local extended to pcout
163  pcl_ros::transformPointCloud(*pc_ch_local, *pcout, data.frames.current.transform);
164 
165  pc_projected.reset();
166  pc_local.reset();
167  pc_ch_local.reset();
168 }
169 
177 double c_polygon_primitive::compute_polygon_area(const pcl::PointCloud<pcl::PointXYZ>::Ptr pcin)
178 {
179  //It is assumed that input_cloud is a polygon with no z coordinates. Must be one of the local polygons
180  CGAL::Polygon_2<CGAL::Exact_predicates_inexact_constructions_kernel> star; //The polygon
181  //CGAL::Exact_predicates_inexact_constructions_kernel::Point_2
182 
183  //Setup the polygon with the points from pc.ch_l
184  for (int i=0; i< (int) pcin->size(); i++)
185  {
187  pcin->points[i].x,
188  pcin->points[i].y));
189  }
190 
191  return fabs(star.area());
192 
193 }
194 
201 void c_polygon_primitive::compute_concave_hull(pcl::PointCloud<pcl::PointXYZ>::Ptr pcin, pcl::PointCloud<pcl::PointXYZ>::Ptr pcout, const pcl::ModelCoefficients::Ptr coeff)
202 {
203  //ROS_INFO("Computing Concave hull");
204  pcl::PointCloud<pcl::PointXYZ>::Ptr pctmp (new pcl::PointCloud<pcl::PointXYZ>);
205  pcl::PointCloud<pcl::PointXYZ>::Ptr voronoi_centers (new pcl::PointCloud<pcl::PointXYZ>);
206 
207  pcl::PointCloud<pcl::PointXYZ>::Ptr pc_projected = pcl::PointCloud<pcl::PointXYZ>::Ptr(new pcl::PointCloud<pcl::PointXYZ>); //create the projection cloud
208 
209  //STEP 1. the projection
210  project_pc_to_plane(pcin, coeff, pc_projected);
211 
212 
213  std::vector< pcl::Vertices > V;
214  pcl::ConcaveHull<pcl::PointXYZ> chull;
215  chull.setInputCloud(pc_projected);
216  chull.setAlpha(6.9);
217  //chull.setKeepInformation(false);
218  //chull.setVoronoiCenters(voronoi_centers);
219 
220  chull.reconstruct(*pctmp, V);
221  pctmp->width = pcin->width;
222  pctmp->header.frame_id = pcin->header.frame_id;
223 
224 
225  //Delete the pcout and obtain a new pc with ordered points
226  //ROS_INFO("vertices has %d points", (int)V.size());
227  //ROS_INFO("vertices2 has %d points", (int)V[0].vertices.size());
228 
229  //ROS_INFO("Voronoi has %d points ",(int)voronoi_centers->points.size());
230  //ROS_INFO("Concave hull has %d points (pcin %d)",(int)pctmp->points.size(), (int)pcin->points.size());
231 
232  //Delete all points from pcout
233  pcout->points.erase(pcout->points.begin(), pcout->points.end());
234 
235  int i=0;
236  int v=0;
237  for (std::vector<pcl::Vertices>::iterator vit=V.begin(); vit != V.end(); vit++)
238  {
239  i=0;
240  for (std::vector<uint32_t>::iterator it=V[v].vertices.begin(); it != V[v].vertices.end(); it++)
241  {
242  pcl::PointXYZ p;
243  p.x = pctmp->points[V[v].vertices[i]].x;
244  p.y = pctmp->points[V[v].vertices[i]].y;
245  p.z = pctmp->points[V[v].vertices[i]].z;
246 
247  pcout->points.push_back(p);
248  i++;
249  }
250  v++;
251  }
252  //ROS_INFO("pcout %d points", (int)pcout->points.size());
253 
254  pcout->header.frame_id = pctmp->header.frame_id;
255  pcout->width = pcout->points.size();
256  pcout->height = 1;
257  //*pcout = *pctmp;
258 
259 }
260 #endif
261 
pcl::ModelCoefficients::Ptr current
void project_pc_to_plane(pcl::PointCloud< pcl::PointXYZ >::Ptr pcin, pcl::ModelCoefficients::Ptr coeff, pcl::PointCloud< pcl::PointXYZ >::Ptr pcout)
Projects all the points in pcin to a plane (along the normal to that plane) defined by coeff and stor...
struct c_polygon_primitive::@9 pointclouds
A structure containing all the point clouds needed to represent the polygon primitive.
void compute_convex_hull_cgal(const pcl::PointCloud< pcl::PointXYZ >::Ptr pcin, const pcl::ModelCoefficients::Ptr coeff, pcl::PointCloud< pcl::PointXYZ >::Ptr pcout, tf::Transform *tr, double &area, double &solidity, bool flg=false)
struct t_polygon_primitive_data::@5 frames
the axis frames
struct t_polygon_primitive_data::@3::@7 convex
data from the polygon convex hull
void compute_concave_hull(pcl::PointCloud< pcl::PointXYZ >::Ptr pcin, pcl::PointCloud< pcl::PointXYZ >::Ptr pcout, const pcl::ModelCoefficients::Ptr coeff)
Computes the concave_hull of a given point cloud.
t_polygon_primitive_data data
struct t_polygon_primitive_data::@3 hulls
Information about the several hulls used.
A class c_polygon_primitive that contains information about a detected polygon primitive as well as t...
double compute_polygon_area(const polygon_3d< pcl::PointXYZ >::Ptr pcin)
Computes the area of a polygon.
void compute_convex_hull(const pcl::PointCloud< pcl::PointXYZ >::Ptr pcin, const pcl::ModelCoefficients::Ptr coeff, pcl::PointCloud< pcl::PointXYZ >::Ptr pcout)
Uses pcl library to compute the convex hull of a set of points in pcin, by projecting them first to t...


polygon_primitives_extraction
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:32:42