-
Notifications
You must be signed in to change notification settings - Fork 0
/
MedialCurve.cpp
1873 lines (1750 loc) · 48.7 KB
/
MedialCurve.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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _USE_MATH_DEFINES
#include "MedialCurve.h"
#include <stack>
#include <queue>
#include <time.h>
#include <math.h>
#include "util.h"
#include "RootGraph.h"
#include "cc/voxel/VoxelFilter.h"
#include "cc/voxel/PalagyiFilter.h"
#ifdef __cplusplus /* identifies C++ */
extern "C" { // tell the C++ compiler that you have C decls
#include "libqhull.h"
}
#endif
// #define DEBUG
// 2012-09-20
//
// Vladimir Popov changed (if matches were found) below, when adjusting this code to Linux
//
// >> substituted by > > in all generic types (templates)
// for instance,map<int,pair<int,int>> changed to map<int,pair<int,int> >
//
// stdext:: substituted by __gnu_cxx::
//
// hash_map substituted by __gnu_cxx::hash_map
//
// hash_set substituted by __gnu_cxx::hash_set
MedialCurve::MedialCurve(void)
{
}
/*
* clone the object
*/
MedialCurve::MedialCurve(MedialCurve &rm)
{
xsize=rm.xsize;
ysize=rm.ysize;
zsize=rm.zsize;
name=rm.name;
// copy the hash map of voxel set
cout << "Copy the hash map of voxel set" << endl;
voxset.insert(rm.voxset.begin(), rm.voxset.end());
}
/*
* create a new object from the file: Randy Clark's format of the root representation RTVOX
*/
MedialCurve::MedialCurve(string filename, int off)
{
cout << "Instantiate MedialCurve using Randy Clark's format of the root representation" << endl;
ifstream f;
f.open(filename.c_str());
string line;
if(!f)
cerr << "Error during opening file!" << endl;
offset=off;
int a=0, b=0, c=0, zlen;
int p=0, pt=0, pstop;
int width, height, depth;
string zstr, xystr;
vector<string> ptr;
int k=0;
while(std::getline(f, line)) // Read line by line
{
k++;
//find the tag width
p=line.find("<Width>", p);
if(p>0) p =p+7;
else
{
cout<<"Can't find the value of width - exitting...";
exit(0);
}
pt=line.find("<\\Width>", p);
xystr=line.substr(p,pt-p);
width=atoi(xystr.c_str());
//printf("Width: %d\n", width);
//find the tag height
p=line.find("<Height>", p);
if(p>0) p =p+8;
else
{
cout<<"Can't find the value of height - exitting...";
exit(0);
}
pt=line.find("<\\Height>", p);
xystr=line.substr(p,pt-p);
height=atoi(xystr.c_str());
//printf("Heigth: %d\n", height);
//find the tag depth
p=line.find("<Depth>", p);
if(p>0) p =p+7;
else
{
cout<<"Can't find the value of depth - exitting...";
exit(0);
}
pt=line.find("<\\Depth>", p);
xystr=line.substr(p,pt-p);
depth=atoi(xystr.c_str());
//printf("Depth: %d\n", depth);
// add offset number of voxels on each side
xsize=width+2*offset;
ysize=height+2*offset;
zsize=depth+2*offset;
while(p>=0)
{
//slice number gives the hight values -> y coordinate
p=line.find("<Slice slc=\"", p);
if(p>0) p=p+12;
else
continue;
pt=line.find("\">", p);
zstr=line.substr(p,pt-p);
zlen=zstr.length();
b=atoi(zstr.c_str());
// the root representation is upside-down: reverse it
b=height-b-1+offset;
pstop =line.find("</Slice>", p);
while(pt<pstop-5)
{
//find x and z coordinated if a voxel
p=line.find("<RV>", p)+4;
pt=line.find("</RV>", p);
xystr=line.substr(p,pt-p);
tokenize(xystr, ptr, " ");
a=atoi(ptr[0].c_str());
a=a+offset;
c=atoi(ptr[1].c_str());
c=c+offset;
// convert to linear indices and insert into the map
voxset.insert(make_pair(subind2linind(a,b,c),1.f));
ptr.clear();
}
}
}
f.close();
name=filename;
}
/*
* Create a new object from the file: Ying's format of the root representation
*
* @param filename Point cloud (.OUT) filepath
* @param off
* @param ying Placeholder value to trigger this overloaded constructor based on paramters
*/
MedialCurve::MedialCurve(string filename, int off, bool ying)
{
cout << "Instantiate MedialCurve using Ying's format of the root representation" << endl;
ifstream f;
f.open(filename.c_str());
string line;
if (!f)
cerr << "Error during opening file: '" << filename << "'" << endl;
offset = off;
int a = 0, b = 0, c = 0;
int p = 0, pt = 0;
string zstr, xystr;
vector<string> ptr;
int k = 0;
int nvox; // total number of voxels
vector<vector<int>> coords;
int minx, maxx, miny, maxy, minz, maxz;
minx = INT_MAX;
miny = INT_MAX;
minz = INT_MAX;
maxx = INT_MIN;
maxy = INT_MIN;
maxz = INT_MIN;
// Skip version comment or cell size (i.e., 0.15)
std::getline(f, line);
//second line - is the number of non-empty cells
std::getline(f, line);
cout << "Number of non-empty cells (i.e., occupied voxels) for '" << filename << "': " << line << endl;
nvox = atoi(line.c_str());
//reserve the space for all the coordinates
coords.resize(nvox);
for (int i = 0; i < nvox; i++)
coords[i].resize(3);
// Checkpoint message: Load in point cloud data
// Read point cloud data
cout << "Loading "<< nvox << " points from '" << filename << "'...";
int ptCounter = 0;
while (std::getline(f, line)) // Read line by line
{
// TODO(tparker): Set some step value for when to report back how many have been read from file
ptCounter++;
if ((ptCounter - 1) % int(floor(nvox/200)) == 0 || ptCounter == nvox)
{
cout << "\rLoad point " << ptCounter << " of " << nvox << " from " << filename << flush;
}
// cout << endl;
tokenize(line, ptr, " ");
coords[k][0] = atoi(ptr[0].c_str());
coords[k][2] = atoi(ptr[1].c_str());
coords[k][1] = atoi(ptr[2].c_str());
ptr.clear();
//update min and max
if (minx > coords[k][0])
minx = coords[k][0];
if (miny > coords[k][1])
miny = coords[k][1];
if (minz > coords[k][2])
minz = coords[k][2];
if (maxx < coords[k][0])
maxx = coords[k][0];
if (maxy < coords[k][1])
maxy = coords[k][1];
if (maxz < coords[k][2])
maxz = coords[k][2];
k++;
}
cout << endl;
f.close();
// Checkpoint message: reposition system to center
cout << "Centering point in 3-D space..." << std::endl;
name = filename;
//find the middle place of the root
int midx = minx + (int)ceil((float)((maxx - minx) / 2));
int midy = miny + (int)ceil((float)((maxy - miny) / 2));
int midz = minz + (int)ceil((float)((maxz - minz) / 2));
xsize = maxx - minx + 1 + 2 * offset;
ysize = maxy - miny + 1 + 2 * offset;
zsize = maxz - minz + 1 + 2 * offset;
std::cout << "Describe 'X' <min, mid, max>: (" << minx << ", " << midx << ", " << maxx << ")" << std::endl;
std::cout << "Describe 'Y' <min, mid, max>: (" << miny << ", " << midy << ", " << maxy << ")" << std::endl;
std::cout << "Describe 'Z' <min, mid, max>: (" << minz << ", " << midz << ", " << maxz << ")" << std::endl;
std::cout << "<xsize, ysize, zsize> = (" << xsize << ", " << ysize << ", " << zsize << ")" << std::endl;
for (int i = 0; i < nvox; i++)
{
coords[i][0] = coords[i][0] - minx + offset;
coords[i][1] = coords[i][1] - miny + offset;
coords[i][2] = coords[i][2] - minz + offset;
}
/*xsize=2*midx+2*offset;
ysize=2*midy+2*offset;
zsize=2*midz+2*offset;*/
//now convert coordinates to linear indices;
// Checkpoint message: convert to voxel set pairs
cout << "Converting to voxel set pairs...";
ptCounter = 0;
for (int i = 0; i < nvox; i++)
{
voxset.insert(make_pair(subind2linind(coords[i][0], coords[i][1], coords[i][2]), 1.f));
ptCounter++;
if ((ptCounter - 1) % int(floor(nvox/200)) == 0 || ptCounter == nvox)
{
cout << "\rAdd voxel " << ptCounter << " of " << nvox << " from " << filename << flush;
}
}
cout << endl;
}
MedialCurve::~MedialCurve(void)
{
voxset.clear();
}
/**
* This function creates a vector of branches incident to tips.
* Each branch is represented by a vector of linear indices of voxels
*/
void MedialCurve::getTipBranches(vector<vector<int> > &tipbranches)
{
tipbranches.clear();
__gnu_cxx::hash_set<int> tips;
__gnu_cxx::hash_set<int>::iterator itt;
//get a set of indices of tip voxels
getTipsList(tips);
for(itt=tips.begin(); itt!=tips.end(); itt++)
{
vector<int> br;
//find an incident branch
getIncidentBranch(*itt,br);
tipbranches.push_back(br);
}
}
/**
* Given an index of a pendant voxel tip, the function returns a set of indices composing the incident branch.
* This function takes a tip voxel and visits incident voxel,
* one after another until it incounters a bifurcation voxel with # of incident voxels >2.
* The bifurcation voxel is included in the branch.
*/
void MedialCurve::getIncidentBranch(int tip, vector<int> &branch)
{
branch.clear();
int nb=1;
int v=tip;
__gnu_cxx::hash_set<int>visited;
while(nb==1)
{
branch.push_back(v);
visited.insert(v);
vector<int> nbv(0);
// try to find new neighbor
for(int k=-1; k<=1; k++)
for(int j=-1; j<=1; j++)
for(int i=-1; i<=1; i++)
{
int ind=v+k*ysize*xsize+j*xsize+i;
if(ind==v) continue;
//if such neighbor exists and it wasn't visited
if(voxset.find(ind)!=voxset.end() && visited.find(ind)==visited.end())
{
nbv.push_back(ind);
}
}
nb=nbv.size();
if(nb>0)
v=nbv[0];
}
}
/**
* get the length of the branch
*/
float MedialCurve::getBranchLength(vector<int> &br)
{
float len=0;
int n=br.size();
int x1, x2, y1, y2, z1, z2;
for(int i=0; i<n-1; i++)
{
linind2subind(br[i],x1,y1,z1);
linind2subind(br[i+1],x2,y2,z2);
len=len+(float)sqrt((float)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)));
}
return len;
}
/**
* construct a skeleton
*/
void MedialCurve::createSkeleton(float scale)
{
//create a map where erosion distance will be stored
__gnu_cxx::hash_map<int, float> distR;
//apply thinning with erosion distance computation
thinning(distR);
//filter the skeleton
//writeVisibleIVEff("root_skel_thin.iv",skel);
setScale();
scaleAxis((float)scale);
}
/**
* Compute Scale Axis - a filtered skeleton based on
* the comparison of the size of the edge with the size
* of the incident bifurcation region which is estimated
* using the erosion distance. Erosion distance is computed
* during the thinning algorithm, it is estimated by
* the number of the iterations which it takes to erode
* the shape til one-voxel wide curve.
*/
void MedialCurve::scaleAxis(float sc)
{
scale=sc;
bool removed_edges=true;
__gnu_cxx::hash_map<int,float> distR(voxset);
__gnu_cxx::hash_set<int> br_list;
__gnu_cxx::hash_set<int>::iterator brit;
setScale();
// continue filtering while the edges can be removed
while(removed_edges)
{
removed_edges=false;
//get all pendant branches - the branches incident to tip voxels
vector<vector<int> > tipbranches;
getTipBranches(tipbranches);
// try to filter pendant branches, return true if some branches were removed
int nbr=tipbranches.size();
for(int i=0; i<nbr; i++)
{
int node_branch=tipbranches[i].size();
//by branch construction the bif node should be the last
int bifnode=tipbranches[i][node_branch-1];
if(voxset.find(bifnode)==voxset.end()) continue;
// get the value of the erosion distance at the bifurcation node
float erd_bif=voxset.find(bifnode)->second;
//by branch construction the tip node should be the first
int tipnode=tipbranches[i][0];
// get the value of the erosion distance at the tip node
if(voxset.find(tipnode)==voxset.end()) continue;
float erd_tip=voxset.find(tipnode)->second;
//get the size of the branch
float elen =(float)tipbranches[i].size();
//float elen =getBranchLength(tipbranches[i]);
// if elen+erd_tip < erd_bif*sc then remove the branch
// compare the size of the branch with the scaled value
// of the erosion distance at the bifurcation node
if(elen+erd_tip < erd_bif*sc)
{
//erase all voxels except the last voxel,
//last voxels is bufurcation, its removal can disconnect the skeleton
for(int j=0; j<node_branch-1;j++)
voxset.erase(tipbranches[i][j]);
removed_edges=true;
}
}
distR=voxset;
//apply additional thinnging because after
//removing branches there might remain bifurcation clusters
thinning();
//updater the hash map with the erosion distances
copyDistances(distR);
}
//storeDTIVFile("scaleAxis", voxset, maxd);
}
/**
* copy distance values associated
* with the voxel indices in the map distR to the current object
*/
void MedialCurve::copyDistances(__gnu_cxx::hash_map<int,float> &distR)
{
__gnu_cxx::hash_map<int,float>::iterator it;
__gnu_cxx::hash_map<int,float>::iterator it1;
for(it=voxset.begin(); it!=voxset.end(); it++)
{
it1=distR.find(it->first);
it->second=it1->second;
}
}
/**
* Get the vector of 26 neighbors of the given voxel ind
*/
void MedialCurve::get_26_neighbours(int ind, vector<int> &nb)
{
nb.resize(26);
int s=0;
for(int k=-1; k<=1; k++)
for(int j=-1; j<=1; j++)
for(int i=-1; i<=1; i++)
{
int nbind=ind+k*ysize*xsize+j*xsize+i;
if(nbind!=ind)
{
nb[s]=nbind;
s++;
}
}
}
/**
* Get the vector of 6 face-sharing neighbors of the given voxel ind
*/
void MedialCurve::get_6_neighbours(int ind, vector<int> &nb)
{
nb.resize(6);
nb[0]=ind+ysize*zsize;
nb[1]=ind-ysize*zsize;
nb[2]=ind+zsize;
nb[3]=ind-zsize;
nb[4]=ind+1;
nb[5]=ind-1;
}
/***
* Get all the indices of bifurcation nodes stored in the hash_set
*/
void MedialCurve::getBiffurcationList(__gnu_cxx::hash_set<int> &br)
{
br.clear();
__gnu_cxx::hash_map<int,float>::iterator it;
for(it=voxset.begin(); it!=voxset.end(); ++it)
{
int nb=0;
//check if the current node is a branch
for(int k=-1; k<=1; k++)
for(int j=-1; j<=1; j++)
for(int i=-1; i<=1; i++)
{
int temp=it->first+k*ysize*xsize+j*xsize+i;
if(voxset.find(temp)!=voxset.end() && temp!=it->first)
nb++;
}
if(nb>2)
br.insert(it->first);
}
}
/**
* Get a vector of branches in the object.
* A one-voxel wide structure is assumed.
* Each edge is represented as a vector of ordered voxels indices.
*/
void MedialCurve::getEdges(vector<vector<int> > &edges)
{
edges.clear();
__gnu_cxx::hash_map<int,float>::iterator it;
__gnu_cxx::hash_map<int,float>::iterator itf;
__gnu_cxx::hash_set<int>::iterator its;
__gnu_cxx::hash_set<int> branches;
__gnu_cxx::hash_set<int> nonbr;
bool flag;
int n;
// in this loop I collect bifurcation and non-bif voxels into 2 different sets
for(it=voxset.begin(); it!=voxset.end(); it++)
{
n=0;
//check how many neighbours
for(int k=-1; k<=1; k++)
for(int j=-1; j<=1; j++)
for(int i=-1; i<=1; i++)
{
int ind=it->first+k*ysize*xsize+j*xsize+i;
if(ind==it->first) continue;
itf=voxset.find(ind);
if(itf!=voxset.end())
n++;
}
if(n>2)
branches.insert(it->first);
else
nonbr.insert(it->first);
}
queue<int> q;
vector<int> start_edge;
//while there are non-bifurcation voxels
while(!nonbr.empty())
{
int e=*nonbr.begin();
q.push(e);
vector<int> ne;
int br=0;
// collect all non-branching voxels belonging to the same branch
while(!q.empty())
{
e=q.front();
ne.push_back(e);
q.pop();
nonbr.erase(e);
n=0;
//try to find neighbors
for(int k=-1; k<=1; k++)
for(int j=-1; j<=1; j++)
for(int i=-1; i<=1; i++)
{
int ind=e+k*ysize*xsize+j*xsize+i;
if(ind==e) continue;
its=nonbr.find(ind);
if(its!=nonbr.end())
{
n++;
q.push(ind);
nonbr.erase(ind);
}
}
//if no neighbours - find adjacent branching point
if(n==0)
{
flag=false;
for(int k=-1; k<=1; k++)
{
for(int j=-1; j<=1; j++)
{
for(int i=-1; i<=1; i++)
{
int ind=e+k*ysize*xsize+j*xsize+i;
if(ind==e) continue;
its=branches.find(ind);
if(its!=branches.end())
{
br++;
ne.push_back(ind);
if(br==1)
{
start_edge.push_back(ind);
flag=true;
break;
}
}
}if(flag==true) break;
}
if(flag==true) break;
}
// if no neighbours at all, this is an ending point
if(br==0)
{
br=1;
start_edge.push_back(e);
flag=true;
}
}
}
edges.push_back(ne);
}
//now we have all indices of the voxels composing branches
// order the indices starting from a branching point
// starting branching points are stored in the start_edge
vector<int>edge;
for(int ii=0; ii<(int)edges.size(); ii++)
{
edge.clear();
__gnu_cxx::hash_set<int>edge_pool(edges[ii].begin(),edges[ii].end());
int v=start_edge[ii];
edge.push_back(v);
while(!edge_pool.empty())
{
edge_pool.erase(v);
flag=false;
for(int k=-1; k<=1; k++){
for(int j=-1; j<=1; j++){
for(int i=-1; i<=1; i++)
{
int ind=v+k*ysize*xsize+j*xsize+i;
if(ind==v) continue;
its=edge_pool.find(ind);
if(its!=edge_pool.end())
{
v=*its;
edge.push_back(v);
flag=true;
break;
}
}
if(flag==true) break;
}
if(flag==true) break;
}
}
edges[ii].swap(edge);
}
}
/**
* Compute features associate with skeleton edges.
* Only the pendant edges are concidered in order not to mix in different meanings
* The edge features are stored in the map features, the key is the name of the feature;
* Edge_num, Av_Edge_length
*/
void MedialCurve::getEdgesFeatures(vector<pair<string,double> > &features)
{
vector<vector<int> > edges;
// first get all edges
getEdges(edges);
__gnu_cxx::hash_set<int> tips;
//get the set of pendant voxels
getTipsList(tips);
vector<int>edge;
//compute features: number of edges, av length, len_distr
float av_len=0;
int nume=0;
//multiset<float> elen_hist;
for(int ii=0; ii<(int)edges.size(); ii++)
{
//recall that edges are ordered from a branching point
//check if the last index in the edge is the tip
int esize=(int)edges[ii].size();
//compute features for non-tip-incident edges
if(tips.find(edges[ii][esize-1])!=tips.end())
continue;
nume++;
float eleni = getBranchLength(edges[ii]);
av_len=av_len+eleni;
//elen_hist.insert(eleni);
}
av_len=av_len/nume;
//output_histogram("edge_len_hist.txt", elen_hist);
features.push_back(make_pair("Edge_num",nume));
features.push_back(make_pair("Av_Edge_length",av_len));
//printf("Edge length histogram was output into the file edge_len_hist.txt\n");
}
/**
* Compute the features associated with bifurcation clusters.
* The value of the features are stored in the map,
* where the key is the name of the feature:
* Number_bif_cl, Av_size_bif_cl
*/
void MedialCurve::getBifClusterFeatures(vector<pair<string, double> > &features)
{
//first compute conn components of bif clusters
set<set<int> > bifcl;
getBifClustersList(bifcl);
int nb=bifcl.size();
features.push_back(pair<string,double>("Number_bif_cl",nb));
//compute average size of the clusters
float brsize=0;
set<set<int> >::iterator it;
for(it=bifcl.begin(); it!=bifcl.end(); it++)
brsize=brsize+(*it).size();
brsize=brsize/nb;
features.push_back(pair<string,double>("Av_size_bif_cl",brsize));
//average incidence degree of bif cluster?
}
/**
* Get the set of connested components of the bifurcation clusters.
* Each bifurcation cluster is represented
* as a set of indices of bifurcation voxels.
*/
void MedialCurve::getBifClustersList(set<set<int> > &v_cl)
{
v_cl.clear();
vector<double> pos(3);
__gnu_cxx::hash_map<int,float>::iterator it;
__gnu_cxx::hash_map<int,float>::iterator itf;
vector<vector<int> > brcomp;
__gnu_cxx::hash_set<int>::iterator its;
__gnu_cxx::hash_set<int> branches;
int n;
//get all bifurcation voxels
for(it=voxset.begin(); it!=voxset.end(); it++)
{
n=0;
//check how many neighbours
for(int k=-1; k<=1; k++)
for(int j=-1; j<=1; j++)
for(int i=-1; i<=1; i++)
{
int ind=it->first+k*ysize*xsize+j*xsize+i;
if(ind==it->first) continue;
itf=voxset.find(ind);
if(itf!=voxset.end())
n++;
}
if(n>2)
branches.insert(it->first);
}
//collect branching nodes into connceted components
queue<int> q;
while(!branches.empty())
{
int node=*branches.begin();
q.push(node);
vector<int> brc;
while(!q.empty())
{
node=q.front();
brc.push_back(node);
q.pop();
branches.erase(node);
n=0;
//try to find neighbors
for(int k=-1; k<=1; k++)
for(int j=-1; j<=1; j++)
for(int i=-1; i<=1; i++)
{
int ind=node+k*ysize*xsize+j*xsize+i;
if(ind==node) continue;
its=branches.find(ind);
if(its!=branches.end())
{
n++;
q.push(ind);
branches.erase(ind);
}
}
}
brcomp.push_back(brc);
}
//convert from vector to set representation
for(int i=0; i<(int)brcomp.size();i++)
{
set<int> c;
for(int j=0; j<(int)brcomp[i].size();j++)
c.insert(brcomp[i][j]);
v_cl.insert(c);
}
}
/**
* Set scale: set the values mind and maxd
* to the min and max values stored in the map voxset
*/
void MedialCurve::setScale()
{
__gnu_cxx::hash_map<int,float>::iterator it;
mind=10000;
maxd=0;
for(it=voxset.begin(); it!=voxset.end(); ++it)
{
if(maxd<it->second) maxd=it->second;
if(mind>it->second) mind=it->second;
}
}
/**
* Apply thinning algorithm developed by Patrick Min.
* This function simply erodes boundary voxels of the object
* without changing its topology.
* This function computes erosion distance which is stored in the map distR.
*/
void MedialCurve::thinning(__gnu_cxx::hash_map<int, float> &distR)
{
distR.clear();
//create voxel structure for thinning
Voxels vv(xsize, ysize, zsize,0);
vv.set_voxels(voxset);
string filename;
string voxel_extension;
//int type;
//output to a file
//filename = "bin_test";
//voxel_extension = "binvox";
//VoxelFile vf(vv,filename);
//type = vf.get_filetype(voxel_extension);
//vf.open_for_write(type);
//vf.write_file();
VoxelFilter *filter = 0;
string filter_type = "palagyi";
if (filter_type.compare("palagyi") == 0) {
filter = new PalagyiFilter(vv); // , *voxel_dt);
}
distR.clear();
if (filter) {
#ifdef DEBUG
cout << "running filter [" << filter_type << "]" << endl;
#endif
filter->applyWdist(distR);
}
//put thinned voxel structure back into medialCurve object
__gnu_cxx::hash_map<int, float> newvs;
__gnu_cxx::hash_map<int, float>::iterator it;
int s=vv.get_size();
for(int i=0; i<s; i++)
if(vv[i]==1)
{
it=distR.find(i);
if(it==distR.end())
{
printf("Error in thinning, can't find erosion distance!\n");
continue;
}
newvs.insert(make_pair<int,float>(int(i),float(it->second)));
}
//output to a file
/*filename = "thin_test";
voxel_extension = "binvox";
VoxelFile vf2(vv,filename);
type = vf2.get_filetype(voxel_extension);
vf2.open_for_write(type);
vf2.write_file();*/
voxset.swap(newvs);
}
/**
* Returns #of faces to be removed = #non-shared faces
*/
int getFacesToBeRemoved(vector<bool> &nb26)
{
int frem=0;
if(nb26[4]==0) frem++;
if(nb26[10]==0) frem++;
if(nb26[12]==0) frem++;
if(nb26[13]==0) frem++;
if(nb26[15]==0) frem++;
if(nb26[21]==0) frem++;
return frem;
}
/**
* Returns #of edges to be removed = #non-shared edges
* In addition returns array of the #of edge with 0 corresponding to the removed edge
* Neighbor voxels are indexed in the following manner:
* 6 7 8 14 15 16 23 24 25
* buttom: 3 4 5 middle: 12 13 top: 20 21 22
* 0 1 2 9 10 11 17 18 19
* the edges are indexed in the following manner:
* /|-----10-----/|
* / | / |
* 11 | 9 |
* / | / |
* /----+--- 8---| |
* | | | |
* | 7 | 6
* 4 | 5 |
* | /---- 2--+----/
* | / | /
* | 3 | 1
* | / | /
* |------ 0-----|/
*/
int getEdgeToBeRemoved(vector<bool> &nb26, vector<bool> &earr)
{
int erem=0;
int s;
earr.assign(12,true);
s=nb26[10]+nb26[1]+nb26[4];
if(s==0) {erem++; earr[0]=0;}
s=nb26[4]+nb26[5]+nb26[13];
if(s==0) {erem++; earr[1]=0;}
s=nb26[4]+nb26[7]+nb26[15];
if(s==0) {erem++; earr[2]=0;}
s=nb26[4]+nb26[3]+nb26[12];
if(s==0) {erem++; earr[3]=0;}
s=nb26[12]+nb26[9]+nb26[10];
if(s==0) {erem++; earr[4]=0;}
s=nb26[10]+nb26[11]+nb26[13];
if(s==0) {erem++; earr[5]=0;}
s=nb26[13]+nb26[16]+nb26[15];
if(s==0) {erem++; earr[6]=0;}
s=nb26[12]+nb26[14]+nb26[15];
if(s==0) {erem++; earr[7]=0;}
s=nb26[10]+nb26[18]+nb26[21];
if(s==0) {erem++; earr[8]=0;}
s=nb26[13]+nb26[22]+nb26[21];
if(s==0) {erem++; earr[9]=0;}
s=nb26[15]+nb26[24]+nb26[21];
if(s==0) {erem++; earr[10]=0;}
s=nb26[12]+nb26[20]+nb26[21];
if(s==0) {erem++; earr[11]=0;}
return erem;
}
/**
* Returns #of vertices to be removed = #non-shared vertices
* Consider the array of edges returned by the above method.
* A vertex is non-shared if all three incident edges in a voxel
* are non-shared, i.e. there value is =0
*/
int getVertexToBeRemoved(vector<bool> &earr)
{
int vrem=0;
int s=0;
s=earr[0]+earr[3]+earr[4];
if(s==0) vrem++;
s=earr[0]+earr[1]+earr[5];
if(s==0) vrem++;
s=earr[1]+earr[2]+earr[6];
if(s==0) vrem++;
s=earr[2]+earr[3]+earr[7];
if(s==0) vrem++;
s=earr[4]+earr[6]+earr[11];
if(s==0) vrem++;
s=earr[5]+earr[8]+earr[9];
if(s==0) vrem++;
s=earr[6]+earr[9]+earr[10];
if(s==0) vrem++;
s=earr[7]+earr[10]+earr[11];
if(s==0) vrem++;
return vrem;
}
//
/**
* Apply thinning algorithm developed by Patrick Min.
* This function simply erodes boundary voxels of the object
* without changing its topology.
* This function does NOT compute erosion distance.
*/
void MedialCurve::thinning()