-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsw_dataIO.cpp
More file actions
621 lines (481 loc) · 16.3 KB
/
Copy pathsw_dataIO.cpp
File metadata and controls
621 lines (481 loc) · 16.3 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include "sw_dataIO.h"
#include <QFile>
#include <QTextStream>
#include <QProgressDialog>
#include <QApplication>
#include <QMessageBox>
#include <QDir>
#include<QImage>
//-------------------------------------------getPLYFileDir-------------------------------//
QVector<QString> SW::DATAIO::getPLYFileDirs()
{
QVector<QString> ply_file_names;
QDir dir;
if(!dir.exists(folder_name_ + tr("/models"))) return ply_file_names;
QString path = folder_name_ + tr("/models");
dir.setPath(path);
dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
QStringList filter;
filter<<"*.ply";
// ply files
QFileInfoList fileList = dir.entryInfoList(filter);
int nFiles = fileList.size();
for(int i=0; i< nFiles; i++)
{
ply_file_names.append( fileList.at(i).filePath());
}
return ply_file_names;
}
//-------------------------------------------getVisFileDir-------------------------------//
QVector<QString> SW::DATAIO::getVisFileDirs()
{
QVector<QString> patch_file_names;
QDir dir;
if(!dir.exists(folder_name_ + tr("/models"))) return patch_file_names;
QString path = folder_name_ + tr("/models");
dir.setPath(path);
dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
QStringList filter;
filter<<"*.patch";
// ply files
QFileInfoList fileList = dir.entryInfoList(filter);
int nFiles = fileList.size();
for(int i=0; i< nFiles; i++)
{
patch_file_names.append( fileList.at(i).filePath());
}
return patch_file_names;
}
//-------------------------------------------getImageFolderDir---------------------------//
QVector<QString> SW::DATAIO::getImageDirs()
{
QVector<QString> img_file_names;
QDir dir;
if(!dir.exists(folder_name_ + tr("/visualize"))) return img_file_names;
QString path = folder_name_ + tr("/visualize");
dir.setPath(path);
dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
QStringList filter;
filter<<QString("*.jpg")<<QString("*.jpeg")<<QString("*.png")<<QString("*.JPG");
// ply files
QFileInfoList fileList = dir.entryInfoList(filter);
int nFiles = fileList.size();
for(int i=0; i< nFiles; i++)
{
img_file_names.append( fileList.at(i).filePath());
}
return img_file_names;
}
//-------------------------------------------getCameraFolderDir-------------------------//
QVector<QString> SW::DATAIO::getCameraDirs()
{
QVector<QString> cam_file_names;
QDir dir;
if(!dir.exists(folder_name_ + tr("/txt"))) return cam_file_names;
QString path = folder_name_ + tr("/txt");
dir.setPath(path);
dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
QStringList filter;
filter<<"*.txt";
// ply files
QFileInfoList fileList = dir.entryInfoList(filter);
int nFiles = fileList.size();
for(int i=0; i< nFiles; i++)
{
cam_file_names.append( fileList.at(i).filePath());
}
return cam_file_names;
}
//-------------------------------------------loadPointsFromPLY----------------------------//
// can read multi files
bool SW::DATAIO::loadPointsFromPLY(PointCloud & pc)
{
QVector< QString> file_names = getPLYFileDirs();
int counter = 0;
foreach(QString file_name, file_names)
{
QFile file(file_name);
if(!file.open(QIODevice::ReadOnly))
{
emit statusBar("Fail to load points from " + file_name);
return false;
}
// create a progress dialog
QProgressDialog progress;
progress.setLabelText(QString("%1 / %2").arg(counter+1).arg(file_names.size()));
progress.setWindowModality(Qt::WindowModal);
bool start_read = false;
QTextStream in(&file);
int counter = 0;
QVector<Point> sub_cluster;
while(!in.atEnd())
{
// get the line data
QString line = in.readLine();
QStringList fields = line.split(" ");
// line date begins with "ply", "comment" or "format" will be ignored, because these lines
// containes no useful information
if (line.startsWith("ply") || line.startsWith("comment") || line.startsWith("format"))
{
continue;
}
// nummber of vetices
if (line.startsWith("element vertex"))
{
fields.takeFirst();
fields.takeFirst();
int vertex_num = fields.takeFirst().toInt();
sub_cluster.resize(vertex_num);
progress.setRange(0, vertex_num);
continue;
}
// end of header
if (line.startsWith("end_header"))
{
start_read = true;
counter =0;
continue;
}
// read vertices
if (start_read == true )
{
// 3D world coordinate
sub_cluster[counter].x = fields.takeFirst().toFloat();
sub_cluster[counter].y = fields.takeFirst().toFloat();
sub_cluster[counter].z = fields.takeFirst().toFloat();
// normal
sub_cluster[counter].normal_x = fields.takeFirst().toFloat();
sub_cluster[counter].normal_y = fields.takeFirst().toFloat();
sub_cluster[counter].normal_z = fields.takeFirst().toFloat();
// color
sub_cluster[counter].r = fields.takeFirst().toFloat();
sub_cluster[counter].g = fields.takeFirst().toFloat();
sub_cluster[counter].b = fields.takeFirst().toFloat();
counter ++;
// set the value of progress diaog
progress.setValue(counter);
qApp->processEvents();
if (progress.wasCanceled())
{
pc.p_points_.clear();
return false;
}
}
}
foreach(Point pt, sub_cluster)
{
pc.p_points_.append(pt);
}
sub_cluster.clear();
counter ++;
}
return true;
}
//--------------------------------------------savePointsToPLY-----------------------------//
bool SW::DATAIO::savePointsToPLY(QVector<Point> & vertices, QString file_name)
{
QFile file(file_name);
if (!file.open(QIODevice::WriteOnly))
{
emit statusBar(tr("Fail to Save PLY!"));
return false;
}
// create a progress dialog
QProgressDialog progress;
progress.setLabelText(tr("Saving dense points..."));
progress.setWindowModality(Qt::WindowModal);
progress.setRange(0, vertices.size());
QTextStream out(&file);
out << "ply" << endl;
out << "format ascii 1.0" << endl;
out << "element vertex " << vertices.size() << endl;
out << "property float x" << endl;
out << "property float y" << endl;
out << "property float z" << endl;
out << "property float nx" << endl;
out << "property float ny" << endl;
out << "property float nz" << endl;
out <<"property uchar diffuse_red"<<endl;
out <<"property uchar diffuse_green"<<endl;
out <<"property uchar diffuse_blue"<<endl;
out << "end_header" << endl;
for(int i=0; i< vertices.size(); i++)
{
out << vertices[i].x << " " << vertices[i].y << " " << vertices[i].z << " ";
out << vertices[i].normal_x << " " << vertices[i].normal_y << " " << vertices[i].normal_z << " ";
out << vertices[i].r << " " << vertices[i].g << " " << vertices[i].b <<endl;
progress.setValue(i);
qApp->processEvents();
if (progress.wasCanceled())
{
return false;
}
}
QMessageBox::warning(NULL, tr("Information"), tr("Save Completed!"));
return true;
}
//--------------------------------------------load images----------------------------------//
// QImage::load() is much faster than cv::Mat:: imread();
bool SW::DATAIO::loadImages(QMap<QString, cv::Mat_<cv::Vec3b> > & images)
{
QVector<QString> img_dirs = getImageDirs();
QProgressDialog progress;
progress.setLabelText(tr("Loading Images..."));
progress.setRange(0, img_dirs.size());
progress.setWindowModality(Qt::WindowModal);
int nSteps = 0;
// QImage img;
cv::Mat_<cv::Vec3b> img;
//load images
foreach(QString img_dir, img_dirs)
{
// set the value of progress diaog
progress.setValue(nSteps);
qApp->processEvents();
if (progress.wasCanceled())
{
images.clear();
return false;
}
if (!img_dir.isEmpty())
{
//----------------------------------CRASH!!-----------------------------------------------//
// There is a problem unexpectedly
cout<<img_dir.toStdString()<<endl;
//img.load(img_dir);
img = cv::imread(img_dir.toStdString().c_str());
QStringList fields = img_dir.split("/");
QString name = fields.takeLast();
images.insert(name, img);
}
nSteps++;
}
return true;
}
//---------------------------------------------load cameras-------------------------------//
bool SW::DATAIO::loadCameras(QMap<QString, Camera> & cameras)
{
QVector<QString> cam_dirs = getCameraDirs();
QProgressDialog progress;
progress.setLabelText(tr("Load Cameras..."));
progress.setRange(0, cam_dirs.size());
progress.setWindowModality(Qt::WindowModal);
int counter =0;
foreach(QString cam_dir, cam_dirs)
{
// set the value of progress diaog
// set the value of progress diaog
progress.setValue(counter);
qApp->processEvents();
if (progress.wasCanceled())
{
cameras.clear();
return false;
}
// cout<<txt_name.toStdString()<<endl;
QFile file(cam_dir);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return false;
Camera cam;
QTextStream in(&file);
int line_num = 0;
while (!in.atEnd())
{
QString line = in.readLine();
// cout<<line.toStdString()<<endl;
if (line_num > 0)
{
QStringList fields = line.split(" ");
for (int i = 0; i < 4; i++)
{
cam.project_.at<float>(line_num - 1, i) = fields.takeFirst().toFloat();
}
}
line_num++;
}
// get the rotation matrix, translation vector, focal, and camera axises of the
// cameras
cam.decomposeProjMats();
// assign a color to the camera for displaying
cam.color_= QColor((int)rand()&255, (int)rand()&255,(int)rand()&255);
// get the name of the image which the camera the is correspoding to
QStringList fields = cam_dir.split("/");
QString name = fields.takeLast();
name = name.replace("txt", "jpg");
cameras.insert(name, cam);
counter++;
}
return true;
}
//---------------------------------------------load visibility-----------------------------//
bool SW::DATAIO::loadVisiblities(QVector<Point> & vertices)
{
if(vertices.size() == 0)
{
QMessageBox::warning(0, tr("Warning"), tr("Loading Points First!"));
return false;
}
// get the dirs of the patch files
QVector<QString> vis_dirs = getVisFileDirs();
QVector<QVector<uint> > all_vis;
QVector<QVector<uint> > sub_vis;
QVector<uint> tmp;
int file_id = 0;
foreach(QString vis_dir, vis_dirs)
{
sub_vis.clear();
// create a progress dialog
QProgressDialog progress;
progress.setLabelText(QString("%1 / %2").arg(file_id+1).arg(vis_dirs.size()));
progress.setWindowModality(Qt::WindowModal);
// cout<<txt_name.toStdString()<<endl;
// load vis from each file
QFile file(vis_dir);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return false;
QTextStream in(&file);
int line_num = 0;
int counter = 0;
while (!in.atEnd())
{
QString line = in.readLine();
QStringList fields = line.split(" ");
if (line_num == 1)
{
uint points_num = fields.takeFirst().toUInt();
progress.setRange(0, points_num);
//vis.resize(points_num);
}
if (line_num > 1)
{
if (line.startsWith("PATCH"))
{
counter = 0;
continue;
}
if (counter == 5){
while (fields.size() != 0)
{
tmp.append(fields.takeFirst().toUInt());
}
tmp.pop_back();
sub_vis.append(tmp);
}
counter++;
}
// if progress dialog is canceled ....
progress.setValue(sub_vis.size());
qApp->processEvents();
if (progress.wasCanceled())
{
all_vis.clear();
return false;
}
line_num++;
}
// collect all the visibility data
foreach(QVector<uint> vis, sub_vis)
{
all_vis.append(vis);
}
file_id++;
}
if(all_vis.size()!= vertices.size())
{
QMessageBox:: warning(0, tr("Error"), tr("Number of Visibility Colud not Match Vertices!"));
return false;
}
int pt_id = 0;
foreach(QVector<uint> vis, all_vis)
{
foreach(uint id, vis)
{
vertices[pt_id].vis.append(id);
}
pt_id ++;
}
}
//* 11/25/2014 add functions
//-------------------------------------load mesh from OFF file------------------------------//
bool SW::DATAIO::loadModelFromOFF(Mesh & mesh , QString filename)
{
QFile file( filename);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
cerr<<"Error to Read OFF File!"<<endl;
return false;
}
// get vertices and facets from files
QTextStream in(&file);
int pts_num = 0;
int facet_num = 0;
int line_num = 0;
while(!in.atEnd())
{
QString line = in.readLine();
// get the number of the vertices and facets
if(line_num ==1 )
{
QStringList fields = line.split(" ");
pts_num = fields.takeFirst().toInt();
facet_num = fields.takeFirst().toInt();
// cout<<"pts_num: "<<pts_num<<endl
// cout<<"facet_num: "<<facet_num<<endl;
}
if(line_num >=2 && line_num < 2 + pts_num)
{
QStringList fields = line.split(" ");
Vec3 pt;
pt.x_ = fields.takeFirst().toFloat();
pt.y_ = fields.takeFirst().toFloat();
pt.z_ = fields.takeFirst().toFloat();
mesh.m_vertices_.append(pt);
}
if(line_num >= 2+ pts_num)
{
QStringList fields = line.split(" ");
int num = fields.takeFirst().toInt();
QVector<uint> facet;
for(int i=0; i< num; i++)
{
facet.append(fields.takeFirst().toUInt());
}
mesh.m_facets_.append(facet);
}
line_num++;
}
// compute the edges of the mesh
mesh.computeEdges();
return true;
}
//------------------------------------save mesh to OFF file--------------------------------//
bool SW::DATAIO::saveMeshToOFF(Mesh & mesh, QString filename)
{
// open the file
QFile file( filename);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
cerr<<"Error to save OFF File!"<<endl;
return false;
}
// get vertices and facets from files
QTextStream out(&file);
uint v_num = mesh.m_vertices_.size();
uint f_num = mesh.m_facets_.size();
out<<"OFF"<<endl;
out<<v_num<<" "<< f_num <<endl;
//write the coordinates of each vertex
foreach(Vec3 v, mesh.m_vertices_)
{
out<<v.x_<<" "<<v.y_<<" "<<v.z_<<endl;
}
// write the facets
foreach(QVector<uint> facet, mesh.m_facets_)
{
out<<facet.size()<<" ";
foreach(uint id, facet)
{
out<<id<<" ";
}
out<<endl;
}
return true;
}