-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootGraph.cpp
More file actions
1989 lines (1851 loc) · 50.3 KB
/
RootGraph.cpp
File metadata and controls
1989 lines (1851 loc) · 50.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
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
#include "RootGraph.h"
#include <queue>
// 2012-09-20
//
// Vladimir Popov commented this include, when adjusting this code to Linux
//#include <hash_set>
#include <map>
#include <float.h>
#include "util.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// 2012-09-20
//
// Vladimir Popov added, when adjusting this code to Linux
//
// newline at the end of this file
// 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
RootGraph::RootGraph(void)
{
}
RootGraph::~RootGraph(void)
{
}
void RootGraph::init(void)
{
E.clear();
v_einc.clear();
elength.clear();
e_order.clear();
v_coord.clear();
arc_v.clear();
}
/**
* The function creates the minimum spanning tree by default
* (if the argument is true), otherwise if the argument is
* false the function creates maximum spanning tree.
* NB: the tree is created from the original object.
* The minimum spanning tree is constructed based on the edge length.
*/
void RootGraph::getSpanningTree(bool minsp)
{
multimap<float,int> q;
__gnu_cxx::hash_set<int> vset;
__gnu_cxx::hash_set<int>::iterator itset1;
__gnu_cxx::hash_set<int>::iterator itset2;
map<int,int> spanv;
map<int,int>::iterator hit;
map<int,int>::iterator itv1;
map<int,int>::iterator itv2;
int vc1, vc2;
int v1, v2;
set<int> edel;
set<int>::iterator eit;
map<int,pair<int,int> >::iterator e;
map<int,float>::iterator it;
map<int, coord>::iterator vit;
//constract a map ordered by edge length
for(it=elength.begin(); it!=elength.end(); it++)
{
e=E.find(it->first);
//remove self-edge
if(e->second.first==e->second.second)
edel.insert(e->first);
else
q.insert(make_pair(it->second,it->first));
}
//creat a set of vertices, they represent connceted components
for(vit=v_coord.begin(); vit!=v_coord.end(); vit++)
vset.insert(vit->first);
pair<float,int> ep;
int cc = (int)vset.size();
int cid=1;
//while not all edges were considered or there are more than one connected components
while(!q.empty() && cc!=1)
{
//retrieve the max or min edge from the queue
if(minsp==true){
ep=*(q.begin());
q.erase(q.begin());}
else
{
ep=*(--q.end());
q.erase(--q.end());
}
//q.erase(ep);
e=E.find(ep.second);
v1=e->second.first;
v2=e->second.second;
//try to find the id of the connected components of incident vertices v1 and v2
itset1=vset.find(v1);
if(itset1 == vset.end())
{
itv1=spanv.find(v1);
vc1=itv1->second;
}
itset2=vset.find(v2);
if(itset2 == vset.end())
{
itv2=spanv.find(v2);
vc2=itv2->second;
}
// if no conncted component was created for v1
if(itset1!=vset.end())
{
//if the two nodes were not connected before to anything, they form a new connected component
if(itset2!=vset.end())
{
spanv.insert(make_pair(v1,cid));
spanv.insert(make_pair(v2,cid));
cid++;
cc--;
vset.erase(v2);
}
//if one of the nodes is already inside another conn component, then the 1st vertex joins it
else
{
spanv.insert(make_pair(v1,vc2));
cc--;
}
vset.erase(v1);
}
else
{
//now the first vetex has been already connected
//if the second was not connected to anything, it joins the first
if(itset2!=vset.end())
{
spanv.insert(make_pair(v2,vc1));
cc--;
vset.erase(v2);
}
//both vertex belong to different connected components, check if components different -> reduce cc, other wise through away the edge
else
{
if(vc1==vc2)
edel.insert(e->first);
else
{
cc--;
//rename components
for(hit=spanv.begin(); hit!=spanv.end(); hit++)
if(hit->second==vc1)
hit->second=vc2;
}
}
}
}
//remove edges in the list "to delete" from the graph
//maps to be updated: E, v_inc, v_arc
for(eit=edel.begin(); eit!=edel.end(); eit++)
E.erase(*eit);
//update vertex incidence map
updateVertexIncidenceMap();
}
/**
* Create a map between the edges of the graph and average root radius.
* The created map is used later together with construction of
* the max Spanning Tree to maximize the conductivity. Conductivity is
* proportional to R^2 where R is the acerage radius of the edge
* (The average is computed over all voxels of the edge).
*/
void RootGraph::createDistEdgeMap(map<int,float> &cond, __gnu_cxx::hash_map<int,float> &dmap)
{
map<int,pair<int,int> >::iterator e;
map<int,set<int> >::iterator ev;
__gnu_cxx::hash_map<int,float>::iterator itmap;
set<int>::iterator it;
int v;
float avd;
int nv;
for(e=E.begin(); e!=E.end();e++)
{
avd=0;
ev=v_einc.find(e->first);
if(ev!=v_einc.end())
nv=ev->second.size();
else
{
cond.insert(make_pair(e->first,avd));
continue;
}
for(it=ev->second.begin(); it!=ev->second.end(); it++)
{
v=*it;
itmap=dmap.find(v);
if(itmap==dmap.end())
continue;
else
avd=avd+itmap->second;
}
avd=avd/nv;
cond.insert(make_pair(e->first,avd));
}
}
void RootGraph::orientArcs()
{
map<int, vector<int> >::iterator ait;
map<int, pair<int,int> >::iterator eit;
for(ait=arc_v.begin(); ait!=arc_v.end(); ait++)
{
eit=E.find(ait->first);
if(eit->second.first==ait->second[0])
continue;
else
{
int temp=eit->second.first;
eit->second.first=eit->second.second;
eit->second.second=temp;
}
}
}
void RootGraph::removeSelfLoops()
{
map<int,pair<int,int> >::iterator e;
set<int> edel;
set<int>::iterator edelit;
updateVertexIncidenceMap();
map<int,set<int> >::iterator incit;
for(e=E.begin(); e!=E.end();e++)
{
if(e->second.first==e->second.second)
edel.insert(e->first);
}
for(edelit=edel.begin(); edelit!=edel.end();edelit++)
{
e=E.find(*edelit);
incit=v_einc.find(e->second.first);
incit->second.erase(*edelit);
if(incit->second.size()==2)
{
//remove the node and merge adjacent arcs
int e1=*incit->second.begin();
int e2=*(++incit->second.begin());
merge_edges(e1, e2);
}
E.erase(*edelit);
arc_v.erase(*edelit);
elength.erase(*edelit);
}
}
void RootGraph::merge_edges(int e1, int e2)
{
map<int,pair<int,int> >::iterator eit1;
map<int,pair<int,int> >::iterator eit2;
map<int,vector<int> >::iterator va1;
map<int,vector<int> >::iterator va2;
map<int,float>::iterator el;
map<int,set<int> >::iterator vincit;
eit1=E.find(e1);
va1=arc_v.find(e1);
va2=arc_v.find(e2);
int v1=eit1->second.first;
int v2=eit1->second.second;
eit2=E.find(e2);
int v3=eit2->second.first;
int v4=eit2->second.second;
int vn1, vn2;
bool res=false;
vector<int> newarc(0);
if(v1==v2 || v3==v4)
return;
if(v1==v3)
{
newarc.insert(newarc.end(),va1->second.rbegin(),va1->second.rend());
newarc.insert(newarc.end(),++va2->second.begin(),va2->second.end());
res=true;
}
if(v1==v4)
{
newarc.insert(newarc.end(),va1->second.rbegin(),va1->second.rend());
newarc.insert(newarc.end(),++va2->second.rbegin(),va2->second.rend());
res=true;
}
if(v2==v3)
{
newarc.insert(newarc.end(),va1->second.begin(),va1->second.end());
newarc.insert(newarc.end(),++va2->second.begin(),va2->second.end());
res=true;
}
if(v2==v4)
{
newarc.insert(newarc.end(),va1->second.begin(),va1->second.end());
newarc.insert(newarc.end(),++va2->second.rbegin(),va2->second.rend());
res=true;
}
vn1=newarc[0];
vn2=newarc[newarc.size()-1];
eit1->second.first=vn1;
eit1->second.second=vn2;
va1->second.swap(newarc);
E.erase(e2);
float l=getEdgeLength(eit1->first);
el=elength.find(e1);
if(el==elength.end())
elength.insert(make_pair(e1,l));
else
el->second=l;
elength.erase(e2);
//updateVertexIncidenceMap();
vincit=v_einc.find(v3);
if(vincit!=v_einc.end())
vincit->second.erase(e2);
vincit=v_einc.find(v4);
if(vincit!=v_einc.end())
vincit->second.erase(e2);
vincit=v_einc.find(vn1);
if(vincit!=v_einc.end())
vincit->second.insert(e1);
vincit=v_einc.find(vn2);
if(vincit!=v_einc.end())
vincit->second.insert(e1);
}
/**
* The function creates the minimum spanning tree by default
* (if the argument is true), otherwise if the argument is
* false the function creates maximum spanning tree.
* NB: the tree is created from the original object.
* The minimum spanning tree is constructed based on the edge length.
*/
void RootGraph::getSpanningTree(bool minsp, map<int,float> feature)
{
multimap<float,int> q;
__gnu_cxx::hash_set<int> vset;
__gnu_cxx::hash_set<int>::iterator itset1;
__gnu_cxx::hash_set<int>::iterator itset2;
map<int,int> spanv;
map<int,int>::iterator hit;
map<int,int>::iterator itv1;
map<int,int>::iterator itv2;
int vc1, vc2;
int v1, v2;
set<int> edel;
set<int>::iterator eit;
map<int,pair<int,int> >::iterator e;
map<int,float>::iterator mapit;
map<int,float>::iterator it;
map<int, coord>::iterator vit;
//construct a map ordered by edge length
for(e=E.begin(); e!=E.end(); e++)
{
//remove self-edge
if(e->second.first==e->second.second)
edel.insert(e->first);
else
{
mapit=feature.find(e->first);
if(mapit==feature.end())
{
printf("getSpanningTree: No feature is associated with the edge %i\n",e->first);
continue;
}
float fval=mapit->second;
q.insert(make_pair(fval,e->first));
}
}
//creat a set of vertices, they represent connceted components
for(vit=v_coord.begin(); vit!=v_coord.end(); vit++)
vset.insert(vit->first);
pair<float,int> ep;
int cc = (int)vset.size();
int cid=1;
//while not all edges were considered or there are more than one connected components
while(!q.empty() && cc!=1)
{
//retrieve the max or min edge from the queue
if(minsp==true){
ep=*(q.begin());
q.erase(q.begin());}
else
{
ep=*(--q.end());
q.erase(--q.end());
}
//q.erase(ep);
e=E.find(ep.second);
v1=e->second.first;
v2=e->second.second;
//try to find the id of the connected components of incident vertices v1 and v2
itset1=vset.find(v1);
if(itset1 == vset.end())
{
itv1=spanv.find(v1);
vc1=itv1->second;
}
itset2=vset.find(v2);
if(itset2 == vset.end())
{
itv2=spanv.find(v2);
vc2=itv2->second;
}
// if no conncted component was created for v1
if(itset1!=vset.end())
{
//if the two nodes were not connected before to anything, they form a new connected component
if(itset2!=vset.end())
{
spanv.insert(make_pair(v1,cid));
spanv.insert(make_pair(v2,cid));
cid++;
cc--;
vset.erase(v2);
}
//if one of the nodes is already inside another conn component, then the 1st vertex joins it
else
{
spanv.insert(make_pair(v1,vc2));
cc--;
}
vset.erase(v1);
}
else
{
//now the first vetex has been already connected
//if the second was not connected to anything, it joins the first
if(itset2!=vset.end())
{
spanv.insert(make_pair(v2,vc1));
cc--;
vset.erase(v2);
}
//both vertex belong to different connected components, check if components different -> reduce cc, other wise through away the edge
else
{
if(vc1==vc2)
edel.insert(e->first);
else
{
cc--;
//rename components
for(hit=spanv.begin(); hit!=spanv.end(); hit++)
if(hit->second==vc1)
hit->second=vc2;
}
}
}
}
//remove edges in the list "to delete" from the graph
//maps to be updated: E, v_inc, v_arc
for(eit=edel.begin(); eit!=edel.end(); eit++)
E.erase(*eit);
//update vertex incidence map
updateVertexIncidenceMap();
}
/**
* This function creates the map v_einc between
* vertex indices and a set of id-s of the incidend edges
*/
void RootGraph::updateVertexIncidenceMap()
{
map<int,pair<int,int> >::iterator eit;
map<int,set<int> >::iterator vincit;
v_einc.clear();
int v1, v2;
//iterate through edges
for(eit=E.begin(); eit!=E.end(); eit++)
{
v1=eit->second.first;
v2=eit->second.second;
//for the first node
vincit=v_einc.find(v1);
if(vincit==v_einc.end())
{
set<int> inc;
inc.insert(eit->first);
v_einc.insert(make_pair(v1,inc));
}
else
vincit->second.insert(eit->first);
//for the second node
vincit=v_einc.find(v2);
if(vincit==v_einc.end())
{
set<int> inc;
inc.insert(eit->first);
v_einc.insert(make_pair(v2,inc));
}
else
vincit->second.insert(eit->first);
}
}
/**
* returns the id of the node with the smallest y-value + it should be bif node
*/
int RootGraph::getHighestNode()
{
map<int, coord>::iterator it;
map<int, set<int> >::iterator itinc;
float ymin = FLT_MAX;
int vid=0;
for(it=v_coord.begin(); it!=v_coord.end(); it++)
{
if(it->second.y<ymin)
{
itinc=v_einc.find(it->first);
if(itinc==v_einc.end()) continue;
if(itinc->second.size()>2)
{
ymin=it->second.y;
vid=it->first;
}
}
}
return vid;
}
/**
* returns the id of the node with the biggest y-value + it should be bif node
*/
int RootGraph::getLowestNode()
{
map<int, coord>::iterator it;
map<int, set<int> >::iterator itinc;
float ymax = -FLT_MAX;
int vid=0;
for(it=v_coord.begin(); it!=v_coord.end(); it++)
{
if(it->second.y>ymax)
{
itinc=v_einc.find(it->first);
if(itinc==v_einc.end()) continue;
if(itinc->second.size()>2)
{
ymax=it->second.y;
vid=it->first;
}
}
}
return vid;
}
/**
* find Horton ordering of the network:
* assign values the order to each edge in the graph;
* the values are store in e_order map;
*/
void RootGraph::HortonOrder()
{
// need to know the source node to start
int vtop=getLowestNode();
int vnum = (int)v_coord.size();
vector<bool> visited(vnum);
vector<int> vo(vnum);
vector<vector<int> > ve(vnum);
for(int i=0; i<vnum; i++)
ve[i].resize(vnum);
for(int i=0; i<vnum; i++)
for(int j=0; j<vnum; j++)
ve[i][j]=-1;
//recursive procedure to find Horton order stored in the matrix vo
findHortonOrder(visited,vtop,vo,ve);
//transfer edge order from matrix representation to the map e_order
map<int,pair<int,int> >::iterator eit;
int v1, v2;
for(eit=E.begin(); eit!=E.end(); eit++)
{
v1=eit->second.first;
v2=eit->second.second;
int ho=ve[v1][v2];
if(ho>=0)
e_order.insert(make_pair(eit->first,ho));
}
}
/**
* This is a recursice procedure to find the Horton order of an edge based on its dauter edges.
* The Horton order of pendant edges is 1. If the edges of different orders meet at a bifurcation point,
* the new successive edge will have an order of max of the incoming edges.
* If edges of the same order meet at the biffurcation point,
* the order of the new successive edge will encrease by 1.
*/
int RootGraph::findHortonOrder(vector<bool> &visited,int vid, vector<int> &vo, vector<vector<int> > &eo)
{
int cur_order;
set<int> vset;
set<int>::iterator vit;
int n_eq_o;
if(!visited[vid])
{
n_eq_o=0;
cur_order=0;
visited[vid]=1;
// find incident nodes
getIncidentVertices(vset,vid);
// iterate trough incident vertices which weren't visited and find their order
for(vit=vset.begin(); vit!=vset.end(); vit++)
{
if(visited[*vit]==true) continue;
int d_order=findHortonOrder(visited,*vit,vo,eo);
//assign order to the edge based on the order of the incident vertex
if (d_order>0)
{
eo[vid][*vit]=d_order;
eo[*vit][vid]=d_order;
}
//current order is the maximum order of the incident vertices
if (d_order>cur_order)
cur_order=d_order;
else
// if there are several incident vertices of the same order count their number
if (cur_order==d_order)
n_eq_o++;
}
// if the number of incident non-visited vertices is equal
// to the number of the vertices of the same order,
// the current order should be increased by 1
if(vset.size()-1==n_eq_o+1 && cur_order!=0 && vset.size()>2)
cur_order=cur_order+1;
cur_order=max(1,cur_order);
vo[vid]=cur_order;
}
else
{
cur_order=0;
}
return cur_order;
}
/**
* returns a set of the incident vertices to a given vertex with id vid
*/
void RootGraph::getIncidentVertices(set<int> &vset,int vid)
{
vset.clear();
map<int,pair<int,int> >::iterator eit;
map<int, set<int> >::iterator it;
set<int>::iterator itset;
it=v_einc.find(vid);
for(itset=it->second.begin(); itset!=it->second.end(); itset++)
{
eit=E.find(*itset);
if(eit->second.first!=vid)
vset.insert(eit->second.first);
else
vset.insert(eit->second.second);
}
}
/**
* get the maximum edge order in the network
*/
int RootGraph::getMaxHortonOrder()
{
int maxOrder=0;
map<int,int>::iterator it;
for(it=e_order.begin(); it!=e_order.end(); it++)
if(it->second>maxOrder) maxOrder=it->second;
return maxOrder;
}
/**
* This function creates an IV output file with the colored network.
* If the order is assigned to edges, the edges will be colored using heatmap colors
* from blue (low values) to red (high values).
* The output will be written to the file filename.iv which is viewable with ivview.exe viewer
* or any OpenInventor viewer. This format can be easily converted to VRML format as well.
* The arguments are: filename - the name of the output file;
* xs,ys,zs - width, hight, and depth of the model volume.
* These values are used to convert linear indices to coordinates.
*/
void RootGraph::writeHortonNetworkToIV(string filename, int xs, int ys, int zs)
{
//first find the maximum order in the network
int maxOrder=getMaxHortonOrder();
float x,y,z;
map<int,pair<int,vector<float> > > vinds;
//create the map of old to new vertex indices
map<int,int> old_new;
map<int,pair<int,vector<float> > >::iterator indit;
//create the map of new vertex indices + their coordinates
map<int, coord>::iterator vit;
map<int, pair<int,int> >::iterator eit;
int curi=0;
// collect only verticies incident to edges
for(eit=E.begin(); eit!=E.end(); eit++)
{
vit=v_coord.find(eit->second.first);
//if this vertex is not in the list - add it to the 2 lists
if(old_new.find(vit->first)==old_new.end())
{
vector<float> coord(3);
coord[0]=vit->second.x;
coord[1]=vit->second.y;
coord[2]=vit->second.z;
vinds.insert(make_pair(curi,make_pair(vit->first,coord)));
old_new.insert(make_pair(vit->first,curi));
curi++;
}
//same for the second vertex
vit=v_coord.find(eit->second.second);
//if this vertex is not in the list - add it to the 2 lists
if(old_new.find(vit->first)==old_new.end())
{
vector<float> coord(3);
coord[0]=vit->second.x;
coord[1]=vit->second.y;
coord[2]=vit->second.z;
vinds.insert(make_pair(curi,make_pair(vit->first,coord)));
old_new.insert(make_pair(vit->first,curi));
curi++;
}
}
//now add vertices of the arcs of the edges
map<int,vector<int> >::iterator evit;
for(eit=E.begin(); eit!=E.end(); eit++)
{
evit=arc_v.find(eit->first);
if(evit==arc_v.end()) continue;
int vn=evit->second.size();
for(int i=1; i<vn-1; i++)
{
int oldi=evit->second[i];
linind2subind(oldi,x,y,z,xs,ys,zs);
vector<float> coord(3);
coord[0]=x;
coord[1]=y;
coord[2]=z;
vinds.insert(make_pair(curi,make_pair(oldi,coord)));
old_new.insert(make_pair(oldi,curi));
curi++;
}
}
string fileout;
int pp=filename.find('.');
fileout=filename.substr(0,pp);
fileout=fileout+"_nw.iv";
// create the table of colors based on the available order of the network
vector<vector<float> > colors(maxOrder);
for(int i=0; i<maxOrder; i++)
{
colors[i].resize(3);
getRGBcolor4Interals(((float)(i+1)/(float)maxOrder),colors[i][0],colors[i][1], colors[i][2]);
}
FILE *f = fopen(fileout.c_str(),"w");
fprintf(f, "#Inventor V2.1 ascii\n Separator {\n");
fprintf(f, "LightModel { \n model PHONG \n}");
fprintf(f, "MaterialBinding { \n value PER_VERTEX_INDEXED \n}");
fprintf(f, "Coordinate3 { \n point [\n");
for(indit=vinds.begin(); indit!=vinds.end(); indit++)
fprintf(f, " %.2f %.2f %.2f,\n", indit->second.second[0], indit->second.second[1], indit->second.second[2]);
fprintf(f, "]\n }\n");
fprintf(f, "BaseColor { \n rgb [\n");
for(int i=0; i<maxOrder; i++)
fprintf(f, " %.2f %.2f %.2f,\n ", colors[i][0], colors[i][1], colors[i][2]);
fprintf(f, "]\n }\n");
fprintf(f, "IndexedLineSet { \ncoordIndex [\n");
map<int,int>::iterator mit;
map<int,int>::iterator mito;
int vnew;
int no;
vector<int> cind(0);
for(eit=E.begin(); eit!=E.end(); eit++)
{
evit=arc_v.find(eit->first);
if(evit==arc_v.end())
printf("Debug:something is wrong!!!");
mito=e_order.find(evit->first);
if(mito==e_order.end())
no=0;
else
no=mito->second-1;
int n=evit->second.size();
if(n<=1) continue;
int v0=eit->second.first;
mit=old_new.find(v0);
if(mit==old_new.end())
{
printf("Debug:something is wrong - first vertex!!!");
continue;
}
vnew=mit->second;
fprintf(f, " %i, ", vnew);
cind.push_back(no);
for(int i=1; i<=n-2;i++ )
{
mit=old_new.find(evit->second[i]);
if(mit==old_new.end())
{
printf("Debug:something is wrong - intermediate vertices!!!");
continue;
}
vnew=mit->second;
fprintf(f, " %i, ", vnew);
cind.push_back(no);
}
int vn=eit->second.second;
mit=old_new.find(vn);
if(mit==old_new.end())
{
printf("Debug:something is wrong - last vertex!!!");
continue;
}
vnew=mit->second;
fprintf(f, " %i, ", vnew);
cind.push_back(no);
fprintf(f, " -1,\n");
cind.push_back(-1);
}
fprintf(f, "]\n ");
fprintf(f, "\nmaterialIndex [\n");
for(int i=0; i<(int)cind.size(); i++)
fprintf(f, " %i, ", cind[i]);
fprintf(f, "]\n }\n");
fprintf(f, "}\n");
fclose(f);
}
/*
*
*this function is for visualization of the graph with correctly located edge-arcs
*/
void RootGraph::outputFullGraphToIV(string filename)
{
map<int, coord>::iterator vit;
map<int, pair<int,int> >::iterator eit;
map<int, vector<int> >::iterator eait;
vector<int>::iterator ait;
vector<int> e_inds(0);
set<int> nlist;
set<int>::iterator sit;
map<int,coord>::iterator mit;
get_node_list(nlist);
sit=nlist.begin();
map<int,int> inds;
map<int,int>::iterator indit;
int id=0;
for(vit=v_coord.begin(); vit!=v_coord.end(); vit++)
{
indit=inds.find(vit->first);
if(indit==inds.end())
{
inds.insert(make_pair(vit->first,id));
id++;
}
}
for(eit=E.begin(); eit!=E.end(); eit++)
{
eait=arc_v.find(eit->first);
if(eait==arc_v.end())
continue;
for(vector<int>::iterator vit=eait->second.begin(); vit!=eait->second.end(); vit++)
{
indit=inds.find(*vit);
e_inds.insert(e_inds.end(), indit->second);
}
e_inds.insert(e_inds.end(),-1);
}
////////////////////////
FILE *f = fopen(filename.c_str(),"w");
fprintf(f, "#VRML V2.0 utf8\n");
fprintf(f, "Transform {\n\t children [\n");
//first node
mit=v_coord.find(*sit);
if(mit==v_coord.end())
{
printf("Cannot find the node %d! No output.", *sit);
fclose(f);
return;
}
fprintf(f, "Transform {\n");
fprintf(f, "\t translation %.3f %.3f %.3f\n", mit->second.x, mit->second.y, mit->second.z);
fprintf(f, "\t children DEF Joe Shape { geometry Sphere { radius .7 } }\n");
fprintf(f, "}\n");
sit++;
for(;sit!=nlist.end(); sit++)
{
mit=v_coord.find(*sit);
if(mit==v_coord.end())
continue;
fprintf(f, "Transform {\n");
fprintf(f, "\t translation %.3f %.3f %.3f\n", mit->second.x, mit->second.y, mit->second.z);
fprintf(f, "\t children USE Joe\n");
fprintf(f, "}\n");
}
fprintf(f, "]\n}\n\n ");
fprintf(f, "IndexedLineSet {\n");
fprintf(f, "coord Coordinate {\n point [\n");
//fprintf(f, "Coordinate3 { \n point [\n");
for(vit=v_coord.begin(); vit!=v_coord.end(); vit++)
fprintf(f, " %.2f %.2f %.2f,\n", vit->second.x, vit->second.y, vit->second.z);
fprintf(f, "]\n }\n");
//fprintf(f, "IndexedLineSet { \ncoordIndex [\n");
fprintf(f, "coordIndex [\n");
for(ait=e_inds.begin(); ait!=e_inds.end(); ait++)
fprintf(f, " %i, ", *ait);
fprintf(f, "]\n}\n ");
fclose(f);
////////////////////////////////
/*FILE *f = fopen(filename.c_str(),"w");
fprintf(f, "#Inventor V2.1 ascii\n Separator {\n");
fprintf(f, "Coordinate3 { \n point [\n");
for(vit=v_coord.begin(); vit!=v_coord.end(); vit++)
fprintf(f, " %.2f %.2f %.2f,\n", vit->second.x, vit->second.y, vit->second.z);
fprintf(f, "]\n }\n");
fprintf(f, "IndexedLineSet { \ncoordIndex [\n");
for(ait=e_inds.begin(); ait!=e_inds.end(); ait++)
fprintf(f, " %i, ", *ait);
fprintf(f, "]\n}\n ");
fprintf(f, "}\n");
fclose(f); */
}
/**
* outputs the graph as adjacency matrix in txt
*/
void RootGraph::graph_adjacency_output(string filename)
{
__gnu_cxx::hash_map<int,int> indexmap;
__gnu_cxx::hash_map<int,int>::iterator it;
pair< __gnu_cxx::hash_map<int,int>::iterator, bool > res;
map<int,pair<int,int> >::iterator eit;
int ind=0;
for(eit=E.begin(); eit!=E.end(); eit++)
{
if(eit->second.first==eit->second.second)
{
//printf("Self edge for the node %d.\n", eit->second.first);
continue;
}
res=indexmap.insert(make_pair(eit->second.first,ind));
if(res.second==true)
ind++;
res=indexmap.insert(make_pair(eit->second.second,ind));
if(res.second==true)
ind++;