forked from alicevision/AliceVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoxelsGrid.cpp
More file actions
695 lines (588 loc) · 23.4 KB
/
VoxelsGrid.cpp
File metadata and controls
695 lines (588 loc) · 23.4 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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
// This file is part of the AliceVision project.
// Copyright (c) 2017 AliceVision contributors.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
#include "VoxelsGrid.hpp"
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/mvsData/Pixel.hpp>
#include <aliceVision/mvsUtils/common.hpp>
#include <aliceVision/mvsUtils/fileIO.hpp>
#include <aliceVision/fuseCut/delaunayGraphCutTypes.hpp>
#include <aliceVision/alicevision_omp.hpp>
#include <boost/filesystem.hpp>
namespace aliceVision {
namespace fuseCut {
namespace bfs = boost::filesystem;
VoxelsGrid::VoxelsGrid()
{
}
VoxelsGrid::VoxelsGrid(const Voxel& dimensions, Point3d* _space, mvsUtils::MultiViewParams* _mp, const std::string& _spaceRootDir, bool _doVisualize)
{
doVisualize = _doVisualize;
mp = _mp;
voxelDim = dimensions;
for(int k = 0; k < 8; k++)
{
space[k] = _space[k]; // TODO faca
}
voxels = mvsUtils::computeVoxels(space, dimensions);
spaceRootDir = _spaceRootDir;
bfs::create_directory(spaceRootDir);
spaceCamsTracksDir = _spaceRootDir + "camsTracks/";
bfs::create_directory(spaceCamsTracksDir);
}
VoxelsGrid* VoxelsGrid::clone(const std::string& _spaceRootDir)
{
VoxelsGrid* out = new VoxelsGrid();
out->doVisualize = doVisualize;
out->mp = mp;
out->voxelDim = voxelDim;
out->voxels = new StaticVector<Point3d>();
out->voxels->resize(voxels->size());
for(int i = 0; i < voxels->size(); i++)
{
out->voxels->push_back((*voxels)[i]);
}
for(int k = 0; k < 8; k++)
{
out->space[k] = space[k];
}
out->spaceRootDir = _spaceRootDir;
bfs::create_directory(out->spaceRootDir);
out->spaceCamsTracksDir = _spaceRootDir + "camsTracks/";
bfs::create_directory(out->spaceCamsTracksDir);
return out;
}
VoxelsGrid::~VoxelsGrid()
{
delete voxels;
}
StaticVector<int>* VoxelsGrid::getNVoxelsTracks()
{
ALICEVISION_LOG_DEBUG("reading number of tracks for each voxel file.");
StaticVector<int>* nVoxelsTracks = new StaticVector<int>();
nVoxelsTracks->reserve(voxels->size() / 8);
//long t1 = mvsUtils::initEstimate();
for(int i = 0; i < voxels->size() / 8; i++)
{
std::string folderName = getVoxelFolderName(i);
std::string fileNameTracksPts;
fileNameTracksPts = folderName + "tracksGridPts.bin";
int n = getArrayLengthFromFile(fileNameTracksPts);
// printf("%i %i\n",i,n);
nVoxelsTracks->push_back(n);
//mvsUtils::printfEstimate(i, voxels->size() / 8, t1);
}
//mvsUtils::finishEstimate();
return nVoxelsTracks;
}
unsigned long VoxelsGrid::getNTracks() const
{
ALICEVISION_LOG_DEBUG("computing number of all tracks.");
unsigned long ntracks = 0;
//long t1 = mvsUtils::initEstimate();
for(int i = 0; i < voxels->size() / 8; i++)
{
const std::string folderName = getVoxelFolderName(i);
std::string fileNameTracksPts;
fileNameTracksPts = folderName + "tracksGridPts.bin";
int n = getArrayLengthFromFile(fileNameTracksPts);
// printf("%i %i\n",i,n);
ntracks += (unsigned long)n;
//mvsUtils::printfEstimate(i, voxels->size() / 8, t1);
}
//mvsUtils::finishEstimate();
return ntracks;
}
int VoxelsGrid::getIdForVoxel(const Voxel& v) const
{
return v.x * voxelDim.y * voxelDim.z + v.y * voxelDim.z + v.z;
}
Voxel VoxelsGrid::getVoxelForId(int id) const
{
int xp = id / (voxelDim.y * voxelDim.z);
return Voxel(xp, (id - xp * voxelDim.y * voxelDim.z) / voxelDim.z,
(id - xp * voxelDim.y * voxelDim.z) % voxelDim.z);
}
bool VoxelsGrid::isValidVoxel(const Voxel& v)
{
return ((v.x >= 0) && (v.x < voxelDim.x) && (v.y >= 0) && (v.y < voxelDim.y) && (v.z >= 0) && (v.z < voxelDim.z));
}
std::string VoxelsGrid::getVoxelFolderName(int id) const
{
const Voxel v = getVoxelForId(id);
// std::string fnx = spaceRootDir + "X"+num2str(v.x)+"/";
// std::string fnxyz = fnx + "Y"+num2str(v.y)+"Z"+num2str(v.z)+"/";
// bfs::create_directory(fnx);
// bfs::create_directory(fnxyz);
std::string fnxyz = spaceRootDir + "X" + mvsUtils::num2str(v.x) + "Y" + mvsUtils::num2str(v.y) + "Z" + mvsUtils::num2str(v.z) + "/";
// bfs::create_directory(fnxyz);
// if (FolderExists(fnx)==false) {
// printf("Warning folder %s does not exist!\n",fnx.c_str());
//}
// if (FolderExists(fnxyz)==false) {
// printf("Warning folder %s does not exist!\n",fnxyz.c_str());
//}
return fnxyz;
}
StaticVector<OctreeTracks::trackStruct*>* VoxelsGrid::loadTracksFromVoxelFiles(StaticVector<int>** cams, int id)
{
const std::string folderName = getVoxelFolderName(id);
const std::string fileNameTracksCams = folderName + "tracksGridCams.bin";
const std::string fileNameTracksPts = folderName + "tracksGridPts.bin";
const std::string fileNameTracksPtsCams = folderName + "tracksGridPtsCams.bin";
const std::string fileNameTracksStat = folderName + "tracksGridStat.bin";
if(!mvsUtils::FileExists(fileNameTracksPts))
return nullptr;
StaticVector<Point3d>* tracksStat = loadArrayFromFile<Point3d>(fileNameTracksStat); // minPixSize, minSim, npts
StaticVector<Point3d>* tracksPoints = loadArrayFromFile<Point3d>(fileNameTracksPts);
StaticVector<StaticVector<Pixel>*>* tracksPointsCams = loadArrayOfArraysFromFile<Pixel>(fileNameTracksPtsCams);
*cams = loadArrayFromFile<int>(fileNameTracksCams);
StaticVector<OctreeTracks::trackStruct*>* tracks = new StaticVector<OctreeTracks::trackStruct*>();
tracks->reserve(tracksPoints->size());
for(int i = 0; i < tracksPoints->size(); i++)
{
StaticVector<Pixel>* tcams = (*tracksPointsCams)[i];
OctreeTracks::trackStruct* t = new OctreeTracks::trackStruct(0.0f, 0.0f, Point3d(), 0);
t->point = (*tracksPoints)[i];
t->minPixSize = (*tracksStat)[i].x;
t->minSim = (*tracksStat)[i].y;
t->npts = (int)(*tracksStat)[i].z;
t->cams = *tcams;
tracks->push_back(t);
}
delete tracksStat;
delete tracksPoints;
deleteArrayOfArrays<Pixel>(&tracksPointsCams);
return tracks;
}
bool VoxelsGrid::saveTracksToVoxelFiles(StaticVector<int>* cams, StaticVector<OctreeTracks::trackStruct*>* tracks,
int id)
{
if(sizeOfStaticVector<OctreeTracks::trackStruct*>(tracks) <= 10)
{
return false;
}
std::string folderName = getVoxelFolderName(id);
bfs::create_directory(folderName);
if(!mvsUtils::FolderExists(folderName))
{
ALICEVISION_LOG_WARNING("Folder '" << folderName << "' does not exist.");
}
StaticVector<Point3d>* tracksPoints = new StaticVector<Point3d>();
tracksPoints->reserve(tracks->size());
StaticVector<StaticVector<Pixel>*>* tracksPointsCams = new StaticVector<StaticVector<Pixel>*>();
tracksPointsCams->reserve(tracks->size());
StaticVector<Point3d>* tracksStat = new StaticVector<Point3d>();
tracksStat->reserve(tracks->size());
for(int j = 0; j < tracks->size(); j++)
{
OctreeTracks::trackStruct* info = (*tracks)[j];
tracksPoints->push_back(info->point);
tracksStat->push_back(Point3d(info->minPixSize, info->minSim, info->npts));
StaticVector<Pixel>* tcams = new StaticVector<Pixel>();
tcams->reserve(info->cams.size());
for(int k = 0; k < info->cams.size(); k++)
{
tcams->push_back(info->cams[k]);
}
tracksPointsCams->push_back(tcams);
}
// printf("SAVING %i-th VOXEL TRACKS\n",i);
std::string fileNameTracksCams, fileNameTracksPts,
fileNameTracksPtsCams, fileNameTracksStat;
fileNameTracksCams = folderName + "tracksGridCams.bin";
fileNameTracksPts = folderName + "tracksGridPts.bin";
fileNameTracksPtsCams = folderName + "tracksGridPtsCams.bin";
fileNameTracksStat = folderName + "tracksGridStat.bin";
saveArrayToFile<int>(fileNameTracksCams, cams);
saveArrayToFile<Point3d>(fileNameTracksPts, tracksPoints);
saveArrayToFile<Point3d>(fileNameTracksStat, tracksStat);
saveArrayOfArraysToFile<Pixel>(fileNameTracksPtsCams, tracksPointsCams);
delete tracksPoints;
delete tracksStat;
deleteArrayOfArrays<Pixel>(&tracksPointsCams);
return true;
}
void VoxelsGrid::generateTracksForEachVoxel(StaticVector<Point3d>* ReconstructionPlan, int numSubVoxs, int maxPts,
int level, int& maxlevel, const std::string& depthMapsPtsSimsTmpDir)
{
ALICEVISION_LOG_DEBUG("generateTracksForEachVoxel recursive "
<< "\t- numSubVoxs: " << numSubVoxs << std::endl
<< "\t- maxPts: " << maxPts << std::endl
<< "\t- level: " << level);
ALICEVISION_LOG_DEBUG("Voxel Dim: " << voxelDim.x << ", " << voxelDim.y << ", " << voxelDim.z);
maxlevel = std::max(maxlevel, level);
long tall = clock();
int nvoxs = voxels->size() / 8;
StaticVector<int>* toRecurse = new StaticVector<int>();
toRecurse->reserve(nvoxs);
#pragma omp parallel for
for(int i = 0; i < nvoxs; i++)
{
// printf("GENERATING TRIANGLUATION FOR %i-th VOXEL OF %i\n",i,nvoxs);
std::string folderName = getVoxelFolderName(i);
long t1 = clock();
OctreeTracks* ott = new OctreeTracks(&(*voxels)[i * 8], mp, Voxel(numSubVoxs, numSubVoxs, numSubVoxs));
StaticVector<OctreeTracks::trackStruct*>* tracks = ott->fillOctree(maxPts, depthMapsPtsSimsTmpDir);
if(mp->verbose)
mvsUtils::printfElapsedTime(t1, "fillOctree");
if(tracks == nullptr)
{
if(mp->verbose)
ALICEVISION_LOG_DEBUG("deleting OTT: " << i);
delete ott;
if(mp->verbose)
ALICEVISION_LOG_DEBUG("deleted " << i);
#pragma omp critical
{
toRecurse->push_back(i);
}
}
else
{
// printf("SAVING %i-th VOXEL TRACKS FILES\n",i);
if(tracks->size() > 10)
{
if(ReconstructionPlan != nullptr)
{
#pragma omp critical
{
for(int k = 0; k < 8; k++)
{
if(ReconstructionPlan->size() < ReconstructionPlan->capacity())
{
ReconstructionPlan->push_back((*voxels)[i * 8 + k]);
}
else
{
ALICEVISION_LOG_WARNING("reconstruction plan array is full.");
}
}
}
}
StaticVector<int>* cams = ott->getTracksCams(tracks);
saveTracksToVoxelFiles(cams, tracks, i);
delete cams;
}
delete tracks;
if(mp->verbose)
ALICEVISION_LOG_DEBUG("deleting OTT: " << i);
delete ott;
if(mp->verbose)
ALICEVISION_LOG_DEBUG("deleted " << i);
}
}
if(mp->verbose)
ALICEVISION_LOG_DEBUG("toRecurse " << toRecurse->size());
for(int j = 0; j < toRecurse->size(); j++)
{
int i = (*toRecurse)[j];
// recursion
std::string folderName = getVoxelFolderName(i);
// std::string subfn = folderName + "sub/";
std::string subfn = folderName;
bfs::create_directory(subfn);
// create file that indicates that the voxel has subvoxels
std::string subfnFileMark = folderName + "sub.txt";
FILE* f = fopen(subfnFileMark.c_str(), "w");
fclose(f);
VoxelsGrid* vgnew = new VoxelsGrid(Voxel(2, 2, 2), &(*voxels)[i * 8], mp, subfn, doVisualize);
vgnew->generateTracksForEachVoxel(ReconstructionPlan, numSubVoxs / 2, maxPts, level + 1, maxlevel,
depthMapsPtsSimsTmpDir);
delete vgnew;
}
delete toRecurse;
mvsUtils::printfElapsedTime(tall);
if(doVisualize)
vizualize();
}
void VoxelsGrid::generateSpace(VoxelsGrid* vgnew, const Voxel& LU, const Voxel& RD,
const std::string& depthMapsPtsSimsTmpDir)
{
if(mp->verbose)
ALICEVISION_LOG_DEBUG("generateSpace recursive: " << std::endl
<< "\t- LU: " << LU.x << " " << LU.y << " " << LU.z << std::endl
<< "\t- RD: " << RD.x << " " << RD.y << " " << RD.z);
int nvoxs = voxels->size() / 8;
for(int voxid = 0; voxid < nvoxs; voxid++)
{
std::string folderName = getVoxelFolderName(voxid);
// std::string subfn = folderName + "sub/";
std::string subfn = folderName;
std::string subfnFileMark = folderName + "sub.txt";
Voxel v = getVoxelForId(voxid);
Voxel ns = RD - LU;
ns.x /= voxelDim.x;
ns.y /= voxelDim.y;
ns.z /= voxelDim.z;
Voxel subLU = LU + v * ns;
Voxel subRD = LU + (v + 1) * ns;
// if (FolderExists(subfn)==true)
if(mvsUtils::FileExists(subfnFileMark))
{
VoxelsGrid* vgrec = new VoxelsGrid(Voxel(2, 2, 2), &(*voxels)[voxid * 8], mp, subfn, doVisualize);
vgrec->generateSpace(vgnew, subLU, subRD, depthMapsPtsSimsTmpDir);
delete vgrec;
}
else
{
Voxel part = subRD - subLU;
OctreeTracks* ott = new OctreeTracks(&(*voxels)[voxid * 8], mp, part);
VoxelsGrid* vgg = new VoxelsGrid(part, &(*voxels)[voxid * 8], mp, folderName, doVisualize);
StaticVector<int>* cams = nullptr;
StaticVector<OctreeTracks::trackStruct*>* tracks = loadTracksFromVoxelFiles(&cams, voxid);
Voxel vrel;
if((tracks != nullptr) && (tracks->size() > 0))
{
// estimate nubers // TODO FACA: remove costly estimation of numbers
StaticVector<int>* nnewVoxsTracks = new StaticVector<int>();
nnewVoxsTracks->reserve(part.x * part.y * part.z);
nnewVoxsTracks->resize_with(part.x * part.y * part.z, 0);
for(int i = 0; i < tracks->size(); i++)
{
if(ott->getVoxelOfOctreeFor3DPoint(vrel, (*tracks)[i]->point))
{
(*nnewVoxsTracks)[vgg->getIdForVoxel(vrel)] += 1;
}
}
// allocate
StaticVector<StaticVector<OctreeTracks::trackStruct*>*>* newVoxsTracks =
new StaticVector<StaticVector<OctreeTracks::trackStruct*>*>();
newVoxsTracks->reserve(part.x * part.y * part.z);
for(int i = 0; i < part.x * part.y * part.z; i++)
{
auto* newVoxTracks = new StaticVector<OctreeTracks::trackStruct*>();
newVoxTracks->reserve((*nnewVoxsTracks)[i]);
newVoxsTracks->push_back(newVoxTracks);
}
// fill
for(int i = 0; i < tracks->size(); i++)
{
if(ott->getVoxelOfOctreeFor3DPoint(vrel, (*tracks)[i]->point))
{
(*newVoxsTracks)[vgg->getIdForVoxel(vrel)]->push_back((*tracks)[i]);
}
}
for(int i = 0; i < part.x * part.y * part.z; i++)
{
Voxel vact = vgg->getVoxelForId(i);
Voxel vglob = subLU + vact;
int newid = vgnew->getIdForVoxel(vglob);
vgnew->saveTracksToVoxelFiles(cams, (*newVoxsTracks)[i], newid);
/*
printf("idlocal %i of %i, voxel local %i %i %i, voxel global %i %i %i, id global %i, ntracks %i \n",
i,part.x*part.y*part.z, vact.x, vact.y, vact.z, vglob.x, vglob.y, vglob.z, newid,
(*newVoxsTracks)[i]->size()
);
*/
}
// deallocate
delete newVoxsTracks;
delete nnewVoxsTracks;
}
if(cams != nullptr)
{
delete cams;
}
if(tracks != nullptr)
{
for(int i = 0; i < tracks->size(); i++)
{
delete(*tracks)[i];
}
delete tracks;
}
delete vgg;
delete ott;
}
}
}
void VoxelsGrid::cloneSpaceVoxel(int voxelId, int numSubVoxs, VoxelsGrid* newSpace)
{
std::string folderName = getVoxelFolderName(voxelId);
std::string fileNameTracksPts = folderName + "tracksGridPts.bin";
if(mvsUtils::FileExists(fileNameTracksPts))
{
OctreeTracks* ott =
new OctreeTracks(&(*voxels)[voxelId * 8], mp, Voxel(numSubVoxs, numSubVoxs, numSubVoxs));
StaticVector<int>* tcams;
StaticVector<OctreeTracks::trackStruct*>* tracksOld = loadTracksFromVoxelFiles(&tcams, voxelId);
StaticVector<OctreeTracks::trackStruct*>* tracksNew = ott->fillOctreeFromTracks(tracksOld);
for(int i = 0; i < tracksOld->size(); i++)
{
delete(*tracksOld)[i];
}
delete tracksOld;
newSpace->saveTracksToVoxelFiles(tcams, tracksNew, voxelId);
delete tcams;
delete tracksNew; // DO NOT NEEDED TO DELETE PARTICULAR POINTERS BECAUSE THEY POINT TO ott STRUCTURES
delete ott;
}
}
VoxelsGrid* VoxelsGrid::cloneSpace(int numSubVoxs, std::string newSpaceRootDir)
{
if(mp->verbose)
ALICEVISION_LOG_DEBUG("cloning space.");
VoxelsGrid* out = clone(newSpaceRootDir);
long t1 = mvsUtils::initEstimate();
for(int i = 0; i < voxels->size() / 8; i++)
{
cloneSpaceVoxel(i, numSubVoxs, out);
mvsUtils::printfEstimate(i, voxels->size() / 8, t1);
}
mvsUtils::finishEstimate();
if(doVisualize)
out->vizualize();
return out;
}
void VoxelsGrid::copySpaceVoxel(int voxelId, VoxelsGrid* newSpace)
{
std::string folderName = getVoxelFolderName(voxelId);
std::string fileNameTracksPts = folderName + "tracksGridPts.bin";
if(mvsUtils::FileExists(fileNameTracksPts))
{
StaticVector<int>* tcams;
StaticVector<OctreeTracks::trackStruct*>* tracksOld = loadTracksFromVoxelFiles(&tcams, voxelId);
newSpace->saveTracksToVoxelFiles(tcams, tracksOld, voxelId);
for(int i = 0; i < tracksOld->size(); i++)
{
delete(*tracksOld)[i];
}
delete tracksOld;
delete tcams;
}
}
VoxelsGrid* VoxelsGrid::copySpace(std::string newSpaceRootDir)
{
if(mp->verbose)
ALICEVISION_LOG_DEBUG("Copy space.");
VoxelsGrid* out = clone(newSpaceRootDir);
long t1 = mvsUtils::initEstimate();
for(int i = 0; i < voxels->size() / 8; i++)
{
copySpaceVoxel(i, out);
mvsUtils::printfEstimate(i, voxels->size() / 8, t1);
}
mvsUtils::finishEstimate();
if(doVisualize)
out->vizualize();
return out;
}
void VoxelsGrid::generateCamsPtsFromVoxelsTracks()
{
if(mp->verbose)
ALICEVISION_LOG_DEBUG("Distributing pts from voxels tracks to camera files.");
long t1 = mvsUtils::initEstimate();
int nvoxs = voxels->size() / 8;
for(int i = 0; i < nvoxs; i++)
{
std::string folderName = getVoxelFolderName(i);
std::string fileNameTracksCams, fileNameTracksPts,
fileNameTracksPtsCams;
fileNameTracksCams = folderName + "tracksGridCams.bin";
fileNameTracksPts = folderName + "tracksGridPts.bin";
fileNameTracksPtsCams = folderName + "tracksGridPtsCams.bin";
// printf("SAVING %i-th VOXEL POINTS TO CAMS FILES\n",i);
if(mvsUtils::FileExists(fileNameTracksPts))
{
StaticVector<Point3d>* tracksPoints = loadArrayFromFile<Point3d>(fileNameTracksPts);
StaticVector<StaticVector<Pixel>*>* tracksPointsCams =
loadArrayOfArraysFromFile<Pixel>(fileNameTracksPtsCams);
StaticVector<int>* cams = loadArrayFromFile<int>(fileNameTracksCams);
// printf("distributing %i tracks to %i camspts files \n", tracksPoints->size(), cams->size());
StaticVector<StaticVector<Pixel>*>* camsTracksPoints =
convertObjectsCamsToCamsObjects(mp, tracksPointsCams);
#pragma omp parallel for
for(int c = 0; c < cams->size(); c++)
{
int rc = (*cams)[c];
// open camPtsFile for append
std::string camPtsFileName = spaceCamsTracksDir + "camPtsGrid_" + std::to_string(mp->getViewId(rc)) + ".bin";
FILE* fin = fopen(camPtsFileName.c_str(), "ab");
StaticVector<Pixel>* camPtsIds = (*camsTracksPoints)[rc];
for(int j = 0; j < sizeOfStaticVector<Pixel>(camPtsIds); j++)
{
int ptid = (*camPtsIds)[j].x;
int nrc = (*camPtsIds)[j].y;
GC_camVertexInfo p;
p.point = (*tracksPoints)[ptid];
p.sim = -0.91; // TODO FACA: why??
p.nrc = nrc;
p.ncams = sizeOfStaticVector<Pixel>((*tracksPointsCams)[ptid]);
p.fwriteinfo(fin);
}
fclose(fin);
} // for cams
delete cams;
delete tracksPoints;
deleteArrayOfArrays<Pixel>(&camsTracksPoints);
deleteArrayOfArrays<Pixel>(&tracksPointsCams);
}
mvsUtils::printfEstimate(i, nvoxs, t1);
} // for i
mvsUtils::finishEstimate();
}
void VoxelsGrid::vizualize()
{
std::string spaceWrlFileName = spaceRootDir + "tracks.wrl";
FILE* f = fopen(spaceWrlFileName.c_str(), "w");
fprintf(f, "#VRML V2.0 utf8\n");
fprintf(f, "Background {\n skyColor 1 1 1 \n } \n");
int nvoxs = voxels->size() / 8;
for(int i = 0; i < nvoxs; i++)
{
std::string subFoldeName = getVoxelFolderName(i);
std::string fname = subFoldeName + "tracks.wrl";
if(mvsUtils::FolderExists(subFoldeName.c_str()))
{
fprintf(f, "Inline{ url [\"%s\"] \n }\n", fname.c_str());
}
}
fclose(f);
}
void VoxelsGrid::getHexah(Point3d* hexahOut, const Voxel& LUi, const Voxel& RDi)
{
Voxel LU, RD;
LU.x = std::min(LUi.x, RDi.x);
LU.y = std::min(LUi.y, RDi.y);
LU.z = std::min(LUi.z, RDi.z);
RD.x = std::max(LUi.x, RDi.x);
RD.y = std::max(LUi.y, RDi.y);
RD.z = std::max(LUi.z, RDi.z);
int luid = getIdForVoxel(LU);
// Point3d *vox = &(*voxels)[0];
// Point3d O = vox[0];
// Point3d vvx = vox[1]-vox[0];
// Point3d vvy = vox[3]-vox[0];
// Point3d vvz = vox[4]-vox[0];
Voxel X, Y, Z;
int Xid, Yid, Zid;
X.x = RD.x;
X.y = LU.y;
X.z = LU.z;
Xid = getIdForVoxel(X);
Y.x = LU.x;
Y.y = RD.y;
Y.z = LU.z;
Yid = getIdForVoxel(Y);
Z.x = LU.x;
Z.y = LU.y;
Z.z = RD.z;
Zid = getIdForVoxel(Z);
Point3d O = (*voxels)[luid * 8];
Point3d vvx = (*voxels)[Xid * 8 + 1] - O;
Point3d vvy = (*voxels)[Yid * 8 + 3] - O;
Point3d vvz = (*voxels)[Zid * 8 + 4] - O;
hexahOut[0] = O;
hexahOut[1] = O + vvx;
hexahOut[2] = O + vvx + vvy;
hexahOut[3] = O + vvy;
hexahOut[4] = O + vvz;
hexahOut[5] = O + vvz + vvx;
hexahOut[6] = O + vvz + vvx + vvy;
hexahOut[7] = O + vvz + vvy;
}
} // namespace fuseCut
} // namespace aliceVision