File: test_ear_clipping.cpp

package info (click to toggle)
pcl 1.13.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 143,524 kB
  • sloc: cpp: 518,578; xml: 28,792; ansic: 13,676; python: 334; lisp: 93; sh: 49; makefile: 30
file content (245 lines) | stat: -rw-r--r-- 8,120 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Software License Agreement (BSD License)
 *
 *  Point Cloud Library (PCL) - www.pointclouds.org
 *  Copyright (c) 2012-, Open Perception, Inc.
 *
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of the copyright holder(s) nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: test_surface.cpp 6579 2012-07-27 18:57:32Z rusu $
 *
 */

#include <pcl/test/gtest.h>

#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_io.h>
#include <pcl/features/normal_3d.h>
#include <pcl/surface/ear_clipping.h>
#include <pcl/common/common.h>

using namespace pcl;
using namespace pcl::io;

PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ>);
PointCloud<PointNormal>::Ptr cloud_with_normals (new PointCloud<PointNormal>);
search::KdTree<PointXYZ>::Ptr tree;
search::KdTree<PointNormal>::Ptr tree2;

// add by ktran to test update functions
PointCloud<PointXYZ>::Ptr cloud1 (new PointCloud<PointXYZ>);
PointCloud<PointNormal>::Ptr cloud_with_normals1 (new PointCloud<PointNormal>);
search::KdTree<PointXYZ>::Ptr tree3;
search::KdTree<PointNormal>::Ptr tree4;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TEST (PCL, EarClipping)
{
  PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ>());
  cloud->height = 1;
  cloud->points.emplace_back( 0.f, 0.f, 0.5f);
  cloud->points.emplace_back( 5.f, 0.f, 0.6f);
  cloud->points.emplace_back( 9.f, 4.f, 0.5f);
  cloud->points.emplace_back( 4.f, 7.f, 0.5f);
  cloud->points.emplace_back( 2.f, 5.f, 0.5f);
  cloud->points.emplace_back(-1.f, 8.f, 0.5f);
  cloud->width = cloud->size ();

  Vertices vertices;
  vertices.vertices.resize (cloud->size ());
  for (int i = 0; i < static_cast<int> (vertices.vertices.size ()); ++i)
    vertices.vertices[i] = i;

  PolygonMesh::Ptr mesh (new PolygonMesh);
  toPCLPointCloud2 (*cloud, mesh->cloud);
  mesh->polygons.push_back (vertices);

  EarClipping clipper;
  PolygonMesh::ConstPtr mesh_aux (mesh);
  clipper.setInputMesh (mesh_aux);

  PolygonMesh triangulated_mesh;
  clipper.process (triangulated_mesh);

  EXPECT_EQ (triangulated_mesh.polygons.size (), 4);
  for (const auto &polygon : triangulated_mesh.polygons)
    EXPECT_EQ (polygon.vertices.size (), 3);

  const int truth[][3] = { {5, 0, 1},
                           {2, 3, 4},
                           {4, 5, 1},
                           {1, 2, 4} };

  for (int pi = 0; pi < static_cast<int> (triangulated_mesh.polygons.size ()); ++pi)
  for (int vi = 0; vi < 3; ++vi)
  {
    EXPECT_EQ (triangulated_mesh.polygons[pi].vertices[vi], truth[pi][vi]);
  }
}

TEST (PCL, EarClippingCubeTest)
{
  PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ>());
  cloud->height = 1;
  //bottom of cube (z=0)
  cloud->points.emplace_back( 0.f, 0.f, 0.f);
  cloud->points.emplace_back( 1.f, 0.f, 0.f);
  cloud->points.emplace_back( 1.f, 1.f, 0.f);
  cloud->points.emplace_back( 0.f, 1.f, 0.f);
  //top of cube (z=1.0)
  cloud->points.emplace_back( 0.f, 0.f, 1.f);
  cloud->points.emplace_back( 1.f, 0.f, 1.f);
  cloud->points.emplace_back( 1.f, 1.f, 1.f);
  cloud->points.emplace_back( 0.f, 1.f, 1.f);
  cloud->width = cloud->size ();

  Vertices vertices;
  vertices.vertices.resize(4);

  const int squares[][4] = { {1, 5, 6, 2},
                           {2, 6, 7, 3},
                           {3, 7, 4, 0},
                           {0, 4, 5, 1},
                           {4, 7, 6, 5},       
                           {0, 1, 2, 3} };

  const int truth[][3] = { {2, 1, 5},
                           {6, 2, 5},
                           {3, 2, 6}, 
                           {7, 3, 6},
                           {0, 3, 7}, 
                           {4, 0, 7},
                           {1, 0, 4}, 
                           {5, 1, 4},
                           {5, 4, 7}, 
                           {6, 5, 7},       
                           {3, 0, 1}, 
                           {2, 3, 1} };

  PolygonMesh::Ptr mesh (new PolygonMesh);
  toPCLPointCloud2 (*cloud, mesh->cloud);

  for (const auto &square : squares)
  {
    vertices.vertices[0] = square[0];
    vertices.vertices[1] = square[1];
    vertices.vertices[2] = square[2];
    vertices.vertices[3] = square[3];
    mesh->polygons.push_back (vertices);
  }

  EarClipping clipper;
  PolygonMesh::ConstPtr mesh_aux (mesh);
  clipper.setInputMesh (mesh_aux);

  PolygonMesh triangulated_mesh;
  clipper.process (triangulated_mesh);

  EXPECT_EQ (triangulated_mesh.polygons.size (), 12);
  for (const auto &polygon : triangulated_mesh.polygons)
    EXPECT_EQ (polygon.vertices.size (), 3);

  

  for (int pi = 0; pi < static_cast<int> (triangulated_mesh.polygons.size ()); ++pi)
  {
      for (int vi = 0; vi < 3; ++vi)
      {
        EXPECT_EQ (triangulated_mesh.polygons[pi].vertices[vi], truth[pi][vi]);
      }
  }
}

/* ---[ */
int
main (int argc, char** argv)
{
  if (argc < 2)
  {
    std::cerr << "No test file given. Please download `bun0.pcd` and pass its path to the test." << std::endl;
    return (-1);
  }

  // Load file
  pcl::PCLPointCloud2 cloud_blob;
  loadPCDFile (argv[1], cloud_blob);
  fromPCLPointCloud2 (cloud_blob, *cloud);

  // Create search tree
  tree.reset (new search::KdTree<PointXYZ> (false));
  tree->setInputCloud (cloud);

  // Normal estimation
  NormalEstimation<PointXYZ, Normal> n;
  PointCloud<Normal>::Ptr normals (new PointCloud<Normal> ());
  n.setInputCloud (cloud);
  //n.setIndices (indices[B);
  n.setSearchMethod (tree);
  n.setKSearch (20);
  n.compute (*normals);

  // Concatenate XYZ and normal information
  pcl::concatenateFields (*cloud, *normals, *cloud_with_normals);
      
  // Create search tree
  tree2.reset (new search::KdTree<PointNormal>);
  tree2->setInputCloud (cloud_with_normals);

  // Process for update cloud
  if(argc == 3){
    pcl::PCLPointCloud2 cloud_blob1;
    loadPCDFile (argv[2], cloud_blob1);
    fromPCLPointCloud2 (cloud_blob1, *cloud1);
        // Create search tree
    tree3.reset (new search::KdTree<PointXYZ> (false));
    tree3->setInputCloud (cloud1);

    // Normal estimation
    NormalEstimation<PointXYZ, Normal> n1;
    PointCloud<Normal>::Ptr normals1 (new PointCloud<Normal> ());
    n1.setInputCloud (cloud1);

    n1.setSearchMethod (tree3);
    n1.setKSearch (20);
    n1.compute (*normals1);

    // Concatenate XYZ and normal information
    pcl::concatenateFields (*cloud1, *normals1, *cloud_with_normals1);
    // Create search tree
    tree4.reset (new search::KdTree<PointNormal>);
    tree4->setInputCloud (cloud_with_normals1);
  }

  // Testing
  testing::InitGoogleTest (&argc, argv);
  return (RUN_ALL_TESTS ());
}
/* ]--- */