forked from rafapages/pointcloudmesher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
425 lines (292 loc) · 14.2 KB
/
main.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "pcMesher.h"
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/geometry/mesh_conversion.h>
#include <boost/make_shared.hpp>
#include <boost/program_options.hpp>
#include <boost/program_options/value_semantic.hpp>
typedef enum {ALL, SPLASH, NO_NORMAL, POISSON, FIXLIST, MESH} RunMode;
int main (int argc, char *argv[]){
RunMode mode = ALL;
float decimationPrc = 0.0;
// Parsing execution options
try {
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("help,h", "Print this help message")
("all,a", "Runs the complete system")
("splash,s", "Runs the complete system and setups the data for splash model creation")
("normal,n", "Runs the system but loads a point cloud where normals have already been roughly estimated")
("poisson,p", "Runs just the poisson reconstruction given a point cloud")
("meshtoclean,m", "Loads a mesh and a point cloud and perform operations on the mesh")
("fixlist,l", "Fixes the image list given another list with cameras with wrong texture")
("decimation", po::value<int>()->default_value(90), "Decimation percentage [0-99%]")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cerr << desc << std::endl;
return 0;
}
// Either complete system or just poisson will run
if (vm.count("normal")){
std::cerr << "Normals will be loaded from input point cloud" << std::endl;
mode = NO_NORMAL;
} else if (vm.count("poisson")){
std::cerr << "Only poisson reconstruction will now run." << std::endl;
mode = POISSON;
} else if (vm.count("meshtoclean")){
std::cerr << "Mesh will be cleaned, smoothed and decimated" << std::endl;
mode = MESH;
} else if (vm.count("fixlist")){
std::cerr << "Fixing the image list..." << std::endl;
mode = FIXLIST;
} else if (vm.count("splash")){
std::cerr << "Splash model creation system will now run..." << std::endl;
mode = SPLASH;
} else {
std::cerr << "Complete system will now run" << std::endl;
mode = ALL;
}
if (vm.count("decimation")){
float decim = (float) vm["decimation"].as<int>();
decimationPrc = 1 - decim * 0.01;
}
}
catch(std::exception& e) {
std::cerr << "error: " << e.what() << std::endl;
std::cerr << "Run: " << argv[0] << " -h if you need help" << std::endl;
return 1;
}
catch(...) {
std::cerr << "Exception of unknown type!" << std:: endl;
}
PcMesher cloud;
std::string nameout;
PointXYZRGBNormalCam normal;
float scale = 0.0;
//------------------------------------------------------------------------
// Complete system
//------------------------------------------------------------------------
if (mode == ALL){
// if ((argc != 3) && (argc != 4)){
if (argc < 3 || argc > 6) {
std::cerr << "Wrong number of input paremeters for complete mode" << std::endl;
std::cerr << "Usage: " << argv[0] << " [-a] [--decimation=<value>] <bundlerfile.out> <image-list.txt>" << std::endl;
return 1;
}
// int index = 1;
// if (argc == 4){
// index = 2;
// }
int index = argc - 2;
std::string namein(argv[index]);
std::string listname(argv[index+1]);
nameout = namein.substr(0, namein.size()-4);
// Reading input parameteres: blunder file and image list
cloud.bundlerReader(namein);
cloud.readImageList(listname);
cloud.writeCameraSetupFile(nameout + "_cameras.txt");
// We get the plane defined by the cameras
cloud.getPlaneDefinedByCameras(normal);
cloud.writeCloud(nameout + "_input.ply");
// Statistical outlier removal
cloud.removeAllOutliers();
// Estimating dimensions
Eigen::Vector3f dim = cloud.getDimensions(0);
scale = cbrt(dim(0) * dim(1) * dim(2));
std::cerr << "scale: " << scale << std::endl;
// We first estimate normals to get an initial orientation
// cloud.estimateAllNormals(scale*0.01);
cloud.estimateAllNormals(scale*0.005);
cloud.writeCloud(nameout + "_no_outliers.ply");
}
//------------------------------------------------------------------------
// In this case normals are not calculated because they are preloaded
//------------------------------------------------------------------------
if (mode == NO_NORMAL){
// if (argc != 5){
if (argc < 5 || argc > 7){
std::cerr << "Wrong number of input paremeters for no-normal mode" << std::endl;
std::cerr << "Usage: " << argv[0] << " -n [--decimation=<value>] <bundlerfile.out> <image-list.txt> <pointcloudwithnormals.ply>" << std::endl;
return 1;
}
int index = argc - 3;
std::string namein(argv[index]);
std::string listname(argv[index+1]);
std::string pcname(argv[index+2]);
nameout = namein.substr(0, namein.size()-4);
// Reading input parameteres: blunder file and image list
cloud.bundlerReader(namein); // Bundler file is read but point cloud info, discarded
cloud.readImageList(listname);
cloud.writeCameraSetupFile(nameout + "_cameras.txt");
cloud.clearPointClouds();
cloud.readPLYCloud(pcname);
// We get the plane defined by the cameras
cloud.getPlaneDefinedByCameras(normal);
// Estimating dimensions
Eigen::Vector3f dim = cloud.getDimensions(0);
scale = cbrt(dim(0) * dim(1) * dim(2));
std::cerr << "scale: " << scale << std::endl;
}
if (mode == ALL || mode == NO_NORMAL){
// Planes are segmented also using their normal info
// cloud.segmentPlanes(scale*0.005);
cloud.segmentPlanes(scale*0.001);
// Normals are estimated properly now (and their sense corrected)
// cloud.estimateAllNormals(scale*0.01);
cloud.estimateAllNormals(scale*0.01);
cloud.fixAllNormals();
// "Planar" point clouds are collected into a single point cloud
std::vector<PointCloud<PointXYZRGBNormalCam>::Ptr> pointclouds;
if (mode == SPLASH){
nameout += "-s";
for (unsigned int i = 1; i < cloud.getNClouds(); i++){
pointclouds.push_back(cloud.getPointCloudPtr(i));
}
} else {
nameout += "-a";
for (unsigned int i = 0; i < cloud.getNClouds(); i++){
pointclouds.push_back(cloud.getPointCloudPtr(i));
}
}
PointCloud<PointXYZRGBNormalCam> combinedCloud = cloud.combinePointClouds(pointclouds);
PointCloud<PointXYZRGBNormalCam>::Ptr combinedCloudPtr = boost::make_shared<PointCloud<PointXYZRGBNormalCam> >(combinedCloud);
io::savePLYFile(nameout + "_combined_planes.ply", combinedCloud);
PolygonMesh first_mesh = cloud.surfaceReconstruction(combinedCloudPtr);
io::savePLYFile(nameout + "_poisson.ply", first_mesh);
Mesh polyMesh;
pcl::geometry::toHalfEdgeMesh(first_mesh, polyMesh); // IMPORTANT!! this is how the conversion is done!
cloud.detectLargestComponent(polyMesh);
PointCloud<PointXYZRGBNormalCam>::Ptr sampledPtr (new PointCloud<PointXYZRGBNormalCam>);
cloud.downSample(cloud.getPointCloudPtr(0), sampledPtr);
cloud.openHole(polyMesh, normal);
cloud.cleanOpenMesh(sampledPtr, polyMesh);
cloud.detectLargestComponent(polyMesh);
cloud.openHole(polyMesh, normal);
cloud.cleanOpenMesh(sampledPtr, polyMesh);
cloud.detectLargestComponent(polyMesh);
PolygonMesh m;
pcl::geometry::toFaceVertexMesh(polyMesh, m);
// PolygonMesh m = cloud.deleteWrongVertices(combinedCloudPtr, first_mesh);
// cloud.assignCam2Mesh(m, combinedCloudPtr, nameout + "_meshcamera.txt");
PolygonMesh ms = cloud.smoothMeshLaplacian(m);
PolygonMesh simpleM = cloud.decimateMesh(ms, decimationPrc);
io::savePLYFile(nameout + "_poisson_clean.ply", m);
io::savePLYFile(nameout + "_poisson_clean_smooth.ply", ms);
io::savePLYFile(nameout + "_poisson_clean_smooth_decimated.ply", simpleM);
cloud.writeOBJMesh(nameout + ".obj", simpleM);
cloud.assignCam2Mesh(ms, combinedCloudPtr, nameout + "_meshcamera.txt");
cloud.assignCam2Mesh(simpleM, combinedCloudPtr, nameout + "_clean_decimated_meshcamera.txt");
// cloud.drawCameras();
cloud.writeCloud(nameout + "_output.ply");
}
//------------------------------------------------------------------------
// Just Poisson
//------------------------------------------------------------------------
if (mode == POISSON){
// if (argc != 4){
if (argc < 4 || argc > 6){
std::cerr << "Wrong number of input paremeters" << std::endl;
std::cerr << "Usage: " << argv[0] << " -p [--decimation=<value>] <pointCloud.ply> <bundlermodel.out>" << std::endl;
return 1;
}
// Reading input parameteres: blunder file and image list
int index = argc - 2;
cloud.readPLYCloud(argv[index]);
cloud.bundlerReadOnlyCameraInfo(argv[index+1]);
cloud.getPlaneDefinedByCameras(normal);
// // To fix the normals!!
// cloud.bundlerReader(argv[3]);
// cloud.fixAllNormals();
PolygonMesh first_mesh = cloud.surfaceReconstruction(cloud.getPointCloudPtr(0));
io::savePLYFile("poisson_v2.ply", first_mesh);
Mesh polyMesh;
pcl::geometry::toHalfEdgeMesh(first_mesh, polyMesh); // IMPORTANT!! this is how the conversion is done!
cloud.detectLargestComponent(polyMesh);
cloud.openHole(polyMesh, normal);
PointCloud<PointXYZRGBNormalCam>::Ptr sampledPtr (new PointCloud<PointXYZRGBNormalCam>);
cloud.downSample(cloud.getPointCloudPtr(0), sampledPtr);
cloud.cleanOpenMesh(sampledPtr, polyMesh);
cloud.detectLargestComponent(polyMesh);
PolygonMesh m;
pcl::geometry::toFaceVertexMesh(polyMesh, m);
// PolygonMesh m = cloud.deleteWrongVertices(cloud.getPointCloudPtr(0), first_mesh);
io::savePLYFile("poisson_clean.ply", m);
PolygonMesh ms = cloud.smoothMeshLaplacian(m);
PolygonMesh mss = cloud.decimateMesh(ms, decimationPrc);
io::savePLYFile("poisson_clean_smooth.ply", ms);
io::savePLYFile("poisson_clean_smooth_decim.ply", mss);
cloud.writeOBJMesh("poisson_clean_smooth_decim.obj", mss);
}
//---------------------------------------------------------------------------
// This one loads a mesh and cleans, smooths and decimates it
//---------------------------------------------------------------------------
if (mode == MESH){
// if (argc != 5){
if (argc < 5 || argc > 7){
std::cerr << "Wrong number of input paremeters for mesh cleaning mode" << std::endl;
std::cerr << "Usage: " << argv[0] << " -m [--decimation=<value>] <meshtoclean.ply> <pointcloudreference.ply> <bundlermodel.out>" << std::endl;
return 1;
}
PolygonMesh mesh;
int index = argc - 3;
cloud.readPLYMesh(argv[index], mesh);
cloud.readPLYCloud(argv[index+1]);
cloud.bundlerReadOnlyCameraInfo(argv[index+2]);
cloud.getPlaneDefinedByCameras(normal);
Mesh polyMesh;
pcl::geometry::toHalfEdgeMesh(mesh, polyMesh); // IMPORTANT!! this is how the conversion is done!
cloud.detectLargestComponent(polyMesh);
cloud.openHole(polyMesh, normal);
PointCloud<PointXYZRGBNormalCam>::Ptr sampledPtr (new PointCloud<PointXYZRGBNormalCam>);
cloud.downSample(cloud.getPointCloudPtr(0), sampledPtr);
cloud.cleanOpenMesh(sampledPtr, polyMesh);
cloud.detectLargestComponent(polyMesh);
PolygonMesh m;
pcl::geometry::toFaceVertexMesh(polyMesh, m);
// PolygonMesh m = cloud.deleteWrongVertices(cloud.getPointCloudPtr(0), mesh);
io::savePLYFile("mesh_clean.ply", m);
PolygonMesh ms = cloud.smoothMeshLaplacian(m);
PolygonMesh mss = cloud.decimateMesh(ms, decimationPrc);
io::savePLYFile("mesh_clean_smooth.ply", ms);
io::savePLYFile("mesh_clean_smooth_decim.ply", mss);
cloud.writeOBJMesh("mesh_clean_smooth_decim.obj", mss);
}
//---------------------------------------------------------------------------
// To fix the image list
//---------------------------------------------------------------------------
if (mode == FIXLIST){
if (argc != 5){
std::cerr << "Wrong number of input paremeters for fix-list mode" << std::endl;
std::cerr << "Usage: " << argv[0] << " -l <bundlerfile.out> <image-list.txt> <wrongcameras.txt>" << std::endl;
return 1;
}
PcMesher cloud;
cloud.bundlerReader(argv[2]);
cloud.readImageList(argv[3]);
cloud.deleteWrongCameras(argv[4]);
cloud.writeCameraSetupFile("clean_camera_setup.txt");
}
return 0;
//----------------------------------------------------------------------------
// Just camera per vertex of the mesh
//----------------------------------------------------------------------------
// if (argc!=3){
// std::cerr << "Wrong number of input parameters" << std::endl;
// std::cerr << "Usage: " << argv[0] << " <bundlerFile.out> <meshFile.{ply,obj}>" << std::endl;
// return 0;
// }
// PcMesher cloud;
// PolygonMesh mesh;
// cloud.bundlerReader(argv[1]);
// cloud.removeAllOutliers();
// std::string namein(argv[2]);
// std::string nameout = namein.substr(0, namein.size()-4);
// nameout += "_camvtx.txt";
// cloud.readOBJMesh(argv[2], mesh);
// cloud.assignCam2Mesh(mesh, cloud.getPointCloudPtr(0), nameout);
// return 0;
}