1 /*
2  * Software License Agreement (BSD License)
3  *
4  *  Point Cloud Library (PCL) - www.pointclouds.org
5  *  Copyright (c) 2010, Willow Garage, Inc.
6  *  Copyright (c) 2012-, Open Perception, Inc.
7  *
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *   * Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *   * Redistributions in binary form must reproduce the above
17  *     copyright notice, this list of conditions and the following
18  *     disclaimer in the documentation and/or other materials provided
19  *     with the distribution.
20  *   * Neither the name of Willow Garage, Inc. nor the names of its
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  *  POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #pragma once
40 
41 #include <vtkSmartPointer.h>
42 #include <vtkPoints.h>
43 #include <vtkPolygon.h>
44 #include <vtkUnstructuredGrid.h>
45 
46 
47 namespace pcl
48 {
49 
50 namespace visualization
51 {
52 
53 template <typename PointT> vtkSmartPointer<vtkDataSet>
createPolygon(const typename pcl::PointCloud<PointT>::ConstPtr & cloud)54 createPolygon (const typename pcl::PointCloud<PointT>::ConstPtr &cloud)
55 {
56   vtkSmartPointer<vtkUnstructuredGrid> poly_grid;
57   if (cloud->points.empty ())
58     return (poly_grid);
59 
60   vtkSmartPointer<vtkPoints> poly_points = vtkSmartPointer<vtkPoints>::New ();
61   vtkSmartPointer<vtkPolygon> polygon    = vtkSmartPointer<vtkPolygon>::New ();
62 
63   poly_points->SetNumberOfPoints (cloud->size ());
64   polygon->GetPointIds ()->SetNumberOfIds (cloud->size ());
65 
66   for (std::size_t i = 0; i < cloud->size (); ++i)
67   {
68     poly_points->SetPoint (i, (*cloud)[i].x, (*cloud)[i].y, (*cloud)[i].z);
69     polygon->GetPointIds ()->SetId (i, i);
70   }
71 
72   allocVtkUnstructuredGrid (poly_grid);
73   poly_grid->Allocate (1, 1);
74   poly_grid->InsertNextCell (polygon->GetCellType (), polygon->GetPointIds ());
75   poly_grid->SetPoints (poly_points);
76 
77   return (poly_grid);
78 }
79 
80 
81 template <typename PointT> vtkSmartPointer<vtkDataSet>
createPolygon(const pcl::PlanarPolygon<PointT> & planar_polygon)82 createPolygon (const pcl::PlanarPolygon<PointT> &planar_polygon)
83 {
84   vtkSmartPointer<vtkUnstructuredGrid> poly_grid;
85   if (planar_polygon.getContour ().empty ())
86     return (poly_grid);
87 
88   vtkSmartPointer<vtkPoints> poly_points = vtkSmartPointer<vtkPoints>::New ();
89   vtkSmartPointer<vtkPolygon> polygon    = vtkSmartPointer<vtkPolygon>::New ();
90 
91   poly_points->SetNumberOfPoints (planar_polygon.getContour ().size () + 1);
92   polygon->GetPointIds ()->SetNumberOfIds (planar_polygon.getContour ().size () + 1);
93 
94   for (std::size_t i = 0; i < planar_polygon.getContour ().size (); ++i)
95   {
96     poly_points->SetPoint (i, planar_polygon.getContour ()[i].x,
97                               planar_polygon.getContour ()[i].y,
98                               planar_polygon.getContour ()[i].z);
99     polygon->GetPointIds ()->SetId (i, i);
100   }
101 
102   std::size_t closingContourId = planar_polygon.getContour ().size ();
103   auto firstContour = planar_polygon.getContour ()[0];
104   poly_points->SetPoint (closingContourId, firstContour.x,
105                                            firstContour.y,
106                                            firstContour.z);
107   polygon->GetPointIds ()->SetId (closingContourId, closingContourId);
108 
109   allocVtkUnstructuredGrid (poly_grid);
110   poly_grid->Allocate (1, 1);
111   poly_grid->InsertNextCell (polygon->GetCellType (), polygon->GetPointIds ());
112   poly_grid->SetPoints (poly_points);
113 
114   return (poly_grid);
115 }
116 
117 } // namespace visualization
118 } // namespace pcl
119 
120