-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
mesh_sampling.cpp
314 lines (286 loc) · 11.5 KB
/
mesh_sampling.cpp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2010-2011, Willow Garage, 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.
*
*/
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/vtk/pcl_vtk_compatibility.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <vtkVersion.h>
#include <vtkOBJReader.h>
#include <vtkTriangle.h>
#include <vtkTriangleFilter.h>
#include <vtkPolyDataMapper.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
inline double
uniform_deviate (int seed)
{
double ran = seed * (1.0 / (RAND_MAX + 1.0));
return ran;
}
inline void
randomPointTriangle (float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3,
float r1, float r2, Eigen::Vector3f& p)
{
float r1sqr = std::sqrt (r1);
float OneMinR1Sqr = (1 - r1sqr);
float OneMinR2 = (1 - r2);
a1 *= OneMinR1Sqr;
a2 *= OneMinR1Sqr;
a3 *= OneMinR1Sqr;
b1 *= OneMinR2;
b2 *= OneMinR2;
b3 *= OneMinR2;
c1 = r1sqr * (r2 * c1 + b1) + a1;
c2 = r1sqr * (r2 * c2 + b2) + a2;
c3 = r1sqr * (r2 * c3 + b3) + a3;
p[0] = c1;
p[1] = c2;
p[2] = c3;
}
inline void
randPSurface (vtkPolyData * polydata, std::vector<double> * cumulativeAreas, double totalArea, Eigen::Vector3f& p, bool calcNormal, Eigen::Vector3f& n, bool calcColor, Eigen::Vector3f& c)
{
float r = static_cast<float> (uniform_deviate (rand ()) * totalArea);
auto low = std::lower_bound (cumulativeAreas->begin (), cumulativeAreas->end (), r);
vtkIdType el = static_cast<vtkIdType>(low - cumulativeAreas->begin ());
double A[3], B[3], C[3];
vtkIdType npts = 0;
vtkCellPtsPtr ptIds = nullptr;
polydata->GetCellPoints (el, npts, ptIds);
polydata->GetPoint (ptIds[0], A);
polydata->GetPoint (ptIds[1], B);
polydata->GetPoint (ptIds[2], C);
if (calcNormal)
{
// OBJ: Vertices are stored in a counter-clockwise order by default
Eigen::Vector3f v1 = Eigen::Vector3f (A[0], A[1], A[2]) - Eigen::Vector3f (C[0], C[1], C[2]);
Eigen::Vector3f v2 = Eigen::Vector3f (B[0], B[1], B[2]) - Eigen::Vector3f (C[0], C[1], C[2]);
n = v1.cross (v2);
n.normalize ();
}
float r1 = static_cast<float> (uniform_deviate (rand ()));
float r2 = static_cast<float> (uniform_deviate (rand ()));
randomPointTriangle (static_cast<float>(A[0]), static_cast<float>(A[1]), static_cast<float>(A[2]),
static_cast<float>(B[0]), static_cast<float>(B[1]), static_cast<float>(B[2]),
static_cast<float>(C[0]), static_cast<float>(C[1]), static_cast<float>(C[2]), r1, r2, p);
if (calcColor)
{
vtkUnsignedCharArray *const colors = vtkUnsignedCharArray::SafeDownCast (polydata->GetPointData ()->GetScalars ());
if (colors && colors->GetNumberOfComponents () == 3)
{
double cA[3], cB[3], cC[3];
colors->GetTuple (ptIds[0], cA);
colors->GetTuple (ptIds[1], cB);
colors->GetTuple (ptIds[2], cC);
randomPointTriangle (static_cast<float>(cA[0]), static_cast<float>(cA[1]), static_cast<float>(cA[2]),
static_cast<float>(cB[0]), static_cast<float>(cB[1]), static_cast<float>(cB[2]),
static_cast<float>(cC[0]), static_cast<float>(cC[1]), static_cast<float>(cC[2]), r1, r2, c);
}
else
{
static bool printed_once = false;
if (!printed_once)
PCL_WARN ("Mesh has no vertex colors, or vertex colors are not RGB!\n");
printed_once = true;
}
}
}
void
uniform_sampling (vtkSmartPointer<vtkPolyData> polydata, std::size_t n_samples, bool calc_normal, bool calc_color, pcl::PointCloud<pcl::PointXYZRGBNormal> & cloud_out)
{
polydata->BuildCells ();
vtkSmartPointer<vtkCellArray> cells = polydata->GetPolys ();
double p1[3], p2[3], p3[3], totalArea = 0;
std::vector<double> cumulativeAreas (cells->GetNumberOfCells (), 0);
vtkIdType npts = 0;
vtkCellPtsPtr ptIds = nullptr;
std::size_t cellId = 0;
for (cells->InitTraversal (); cells->GetNextCell (npts, ptIds); cellId++)
{
polydata->GetPoint (ptIds[0], p1);
polydata->GetPoint (ptIds[1], p2);
polydata->GetPoint (ptIds[2], p3);
totalArea += vtkTriangle::TriangleArea (p1, p2, p3);
cumulativeAreas[cellId] = totalArea;
}
cloud_out.resize (n_samples);
cloud_out.width = static_cast<std::uint32_t> (n_samples);
cloud_out.height = 1;
for (std::size_t i = 0; i < n_samples; i++)
{
Eigen::Vector3f p;
Eigen::Vector3f n (0, 0, 0);
Eigen::Vector3f c (0, 0, 0);
randPSurface (polydata, &cumulativeAreas, totalArea, p, calc_normal, n, calc_color, c);
cloud_out[i].x = p[0];
cloud_out[i].y = p[1];
cloud_out[i].z = p[2];
if (calc_normal)
{
cloud_out[i].normal_x = n[0];
cloud_out[i].normal_y = n[1];
cloud_out[i].normal_z = n[2];
}
if (calc_color)
{
cloud_out[i].r = static_cast<std::uint8_t>(c[0]);
cloud_out[i].g = static_cast<std::uint8_t>(c[1]);
cloud_out[i].b = static_cast<std::uint8_t>(c[2]);
}
}
}
using namespace pcl;
using namespace pcl::io;
using namespace pcl::console;
constexpr int default_number_samples = 100000;
constexpr float default_leaf_size = 0.01f;
void
printHelp (int, char **argv)
{
print_error ("Syntax is: %s input.{ply,obj} output.pcd <options>\n", argv[0]);
print_info (" where options are:\n");
print_info (" -n_samples X = number of samples (default: ");
print_value ("%d", default_number_samples);
print_info (")\n");
print_info (
" -leaf_size X = the XYZ leaf size for the VoxelGrid -- for data reduction (default: ");
print_value ("%f", default_leaf_size);
print_info (" m)\n");
print_info (" -write_normals = flag to write normals to the output pcd\n");
print_info (" -write_colors = flag to write colors to the output pcd\n");
print_info (
" -no_vis_result = flag to stop visualizing the generated pcd\n");
}
/* ---[ */
int
main (int argc, char **argv)
{
print_info ("Convert a CAD model to a point cloud using uniform sampling. For more information, use: %s -h\n",
argv[0]);
if (argc < 3)
{
printHelp (argc, argv);
return (-1);
}
// Parse command line arguments
int SAMPLE_POINTS_ = default_number_samples;
parse_argument (argc, argv, "-n_samples", SAMPLE_POINTS_);
float leaf_size = default_leaf_size;
parse_argument (argc, argv, "-leaf_size", leaf_size);
bool vis_result = ! find_switch (argc, argv, "-no_vis_result");
const bool write_normals = find_switch (argc, argv, "-write_normals");
const bool write_colors = find_switch (argc, argv, "-write_colors");
// Parse the command line arguments for .ply and PCD files
std::vector<int> pcd_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
if (pcd_file_indices.size () != 1)
{
print_error ("Need a single output PCD file to continue.\n");
return (-1);
}
std::vector<int> ply_file_indices = parse_file_extension_argument (argc, argv, ".ply");
std::vector<int> obj_file_indices = parse_file_extension_argument (argc, argv, ".obj");
if (ply_file_indices.size () != 1 && obj_file_indices.size () != 1)
{
print_error ("Need a single input PLY/OBJ file to continue.\n");
return (-1);
}
vtkSmartPointer<vtkPolyData> polydata1 = vtkSmartPointer<vtkPolyData>::New ();
if (ply_file_indices.size () == 1)
{
pcl::PolygonMesh mesh;
pcl::io::loadPLYFile (argv[ply_file_indices[0]], mesh);
pcl::io::mesh2vtk (mesh, polydata1);
}
else if (obj_file_indices.size () == 1)
{
vtkSmartPointer<vtkOBJReader> readerQuery = vtkSmartPointer<vtkOBJReader>::New ();
readerQuery->SetFileName (argv[obj_file_indices[0]]);
readerQuery->Update ();
polydata1 = readerQuery->GetOutput ();
}
//make sure that the polygons are triangles!
vtkSmartPointer<vtkTriangleFilter> triangleFilter = vtkSmartPointer<vtkTriangleFilter>::New ();
triangleFilter->SetInputData (polydata1);
triangleFilter->Update ();
vtkSmartPointer<vtkPolyDataMapper> triangleMapper = vtkSmartPointer<vtkPolyDataMapper>::New ();
triangleMapper->SetInputConnection (triangleFilter->GetOutputPort ());
triangleMapper->Update ();
polydata1 = triangleMapper->GetInput ();
pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud_1 (new pcl::PointCloud<pcl::PointXYZRGBNormal>);
uniform_sampling (polydata1, SAMPLE_POINTS_, write_normals, write_colors, *cloud_1);
// Voxelgrid
VoxelGrid<PointXYZRGBNormal> grid_;
grid_.setInputCloud (cloud_1);
grid_.setLeafSize (leaf_size, leaf_size, leaf_size);
pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr voxel_cloud (new pcl::PointCloud<pcl::PointXYZRGBNormal>);
grid_.filter (*voxel_cloud);
if (vis_result)
{
visualization::PCLVisualizer vis3 ("VOXELIZED SAMPLES CLOUD");
vis3.addPointCloud<pcl::PointXYZRGBNormal> (voxel_cloud);
if (write_normals)
vis3.addPointCloudNormals<pcl::PointXYZRGBNormal> (voxel_cloud, 1, 0.02f, "cloud_normals");
vis3.spin ();
}
if (write_normals && write_colors)
{
savePCDFileASCII (argv[pcd_file_indices[0]], *voxel_cloud);
}
else if (write_normals)
{
pcl::PointCloud<pcl::PointNormal>::Ptr cloud_xyzn (new pcl::PointCloud<pcl::PointNormal>);
// Strip uninitialized colors from cloud:
pcl::copyPointCloud (*voxel_cloud, *cloud_xyzn);
savePCDFileASCII (argv[pcd_file_indices[0]], *cloud_xyzn);
}
else if (write_colors)
{
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_xyzrgb (new pcl::PointCloud<pcl::PointXYZRGB>);
// Strip uninitialized normals from cloud:
pcl::copyPointCloud (*voxel_cloud, *cloud_xyzrgb);
savePCDFileASCII (argv[pcd_file_indices[0]], *cloud_xyzrgb);
}
else // !write_normals && !write_colors
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointXYZ>);
// Strip uninitialized normals and colors from cloud:
pcl::copyPointCloud (*voxel_cloud, *cloud_xyz);
savePCDFileASCII (argv[pcd_file_indices[0]], *cloud_xyz);
}
}