-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCompareReebGraph.java
1433 lines (1102 loc) · 40.2 KB
/
CompareReebGraph.java
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
/******************************************************************************
* Topological similarity estimation for 3D models (triangle meshes) *
* using multiresolutional Reeb graphs. *
* *
* Copyright (C) 2004 Drexel University *
* Implemented by: Dmitriy Bespalov ([email protected]) *
* *
* This file is part of reeb_graph. *
* reeb_graph is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* reeb_graph is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with reeb_graph. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
import java.util.*;
import java.util.Random.*;
import java.io.*;
/*
| The method for estimating similarity of 3D mesh models based on
| multiresolutional Reeb graphs (MRG) was proposed by Hilaga et al. [1].
|
| ExtractReebGraph.java implements construction of MRGs
| for 3D mesh models (see Section 4 in [1]) stored in (pseudo-) VRML format.
|
| CompareReebGraph.java implements matching algorithm
| for a pair of MRGs (see Section 5 in [1])
|
|
| References:
| [1] Topology Matching for Fully Automatic Similarity Estimation of 3D Shapes
| Masaki Hilaga, Yoshihisa Shinagawa, Taku Kohmura and Tosiyasu L. Kunii
| SIGGRAPH, 2001
*/
/*
| Matching a pair of MRGs proceeds as following:
|
| 1. Two MRGs are stored in CompareReebGraph.MRG1 and CompareReebGraph.MRG2 vectors
| that contain RNode objects.
|
| 2. Matching is performed starting with coarsest resolution. As such, each RNode
| object maintains RNode.children vector to store R-edges between R-nodes
| from a coarser to a finear resolution of MRG.
|
| Note that a list RNode.children is the same as the list stored in
| ReebGraphElement.parents, where instances of RNode and ReebGraphElement
| contain data for the same R-node in MRG.
| ReebGraphElement object is used by ExtractReebGraph program
| (see comments in MRGConstrLight.java), while RNode object
| is used by CompareReebGraph program.
*/
public class CompareReebGraph {
public static String file_name;
public static int line_counter;
public static Vector attributes1;
public static Vector attributes2;
public static Vector MRG1;
public static Vector MRG2;
public static double w;
public static Vector NLIST;
public static Vector MPAIR;
public static double SIM_R_S;
public static PrintWriter out;
/*
| usage: java -Xmx512m CompareReebGraph <num_pts> <mu_coeff> <mrg_size> <sim_weight> <model_1>.wrl <model_2>.wrl ... <model_N>.wrl
|
| <num_pts> -- target number of vertices before MRG is extracted
| (triangle meshes in each 3D model are resampled to match <num_pts>)
|
| <mu_coeff> -- coefficient for calculating threshold parameter r=sqrt(mu_coeff * area(S)),
| used to approximate mu values
|
| <mrg_size> -- number of ranges in the finest resolution of MRG (parameter K in [1])
|
|
| <sim_weight> -- weight w used in similarity function (trade-off between attributes a and l)
|
| <model_i>.wrl -- a list of VRML models to compare, where i=[1,N].
| It is assumed that each VRML model was processed using ExtractReebGraph program,
| and MRG for model <model_i>.wrl is stored in text file <model_i>.mrg
*/
public static void main (String arg[]) throws IOException {
int points_number = (new Integer(arg[0])).intValue();
double mu_coeff = (new Double(arg[1])).doubleValue();
int MRG_number = (new Integer(arg[2])).intValue();
w = (new Double(arg[3])).doubleValue();
//the results of comparison will be saved in a file
out = new PrintWriter(new FileWriter("log_" + points_number
+ "_" + mu_coeff + "_" + MRG_number + "_" + w));
for(int i = 4; i < arg.length; i++){
for(int j = 4; j < arg.length; j++){
String argument[] = new String [2];
argument[0] = arg[i];
argument[1] = arg[j];
main_one(argument);
out.println("Similarity between " + arg[i] + " and " + arg[j] + " is " + SIM_R_S);
}
}
out.close();
}
//function that compares a pair of MRGs
public static void main_one (String arg[]) throws IOException {
//System.out.println("Comparing " + arg[0] + " and " + arg[1] + " ...");
attributes1 = null;
attributes2 = null;
MRG1 = null;
MRG2 = null;
//read both of the objects from mrg files
readOneFile(arg[0]);
readOneFile(arg[1]);
if(MRG1.size() != MRG2.size()){
System.out.println("ERROR: number of resolutions must match! Exiting...");
System.exit(1);
}
//calculate the attributes for the pair of MRGs
calculateRestAttributes(MRG1, attributes1);
calculateRestAttributes(MRG2, attributes2);
//computes parents in each MRG
computeParents();
//compare two MRGs
doComparison(arg);
//performs simple tests for both objects
//tester();
attributes1 = null;
attributes2 = null;
}
public static void doComparison(String str[]) throws IOException{
NLIST = new Vector();
MPAIR = new Vector();
Vector list1 = new Vector();
Vector list2 = new Vector();
SIM_R_S = 0;
//puts nodes from coarsest resolution graph in to NLIST
Vector reeb_graph = (Vector) MRG1.elementAt(MRG1.size() - 1);
for(int i = 0; i < reeb_graph.size(); i++){
Vector vec = (Vector) reeb_graph.elementAt(i);
RNode node = (RNode) vec.elementAt(0);
list1.addElement(node);
}
reeb_graph = (Vector) MRG2.elementAt(MRG2.size() - 1);
for(int i = 0; i < reeb_graph.size(); i++){
Vector vec = (Vector) reeb_graph.elementAt(i);
RNode node = (RNode) vec.elementAt(0);
list2.addElement(node);
}
NLIST.addElement(list1);
NLIST.addElement(list2);
while(list1.size() != 0 && list2.size() != 0){
lookForMatchingPair(str);
}
/*//this code is just for image****************************************
String image_filename1 = str[0].substring(0,str[0].indexOf(".wrl")) + "_"
+ str[1].substring(0,str[1].indexOf(".wrl")) + ".img" ;
String image_filename2 = str[1].substring(0,str[1].indexOf(".wrl")) + "_"
+ str[0].substring(0,str[0].indexOf(".wrl")) + ".img";
PrintWriter out_image1 = new PrintWriter(new FileWriter(image_filename1));
PrintWriter out_image2 = new PrintWriter(new FileWriter(image_filename2));
for(int i = 0; i < MPAIR.size(); i++){
MPairElement el = (MPairElement) MPAIR.elementAt(i);
if(calculateIndexInMRG1(el.node1) == 0
&& calculateIndexInMRG2(el.node2) == 0){
if(el.vector_nodes1 == null){
out_image1.println(el.node1.index);
}
else{
for(int j = 0; j < el.vector_nodes1.size(); j++){
RNode temp = (RNode) el.vector_nodes1.elementAt(j);
out_image1.print(temp.index + " ");
}
out_image1.println("");
}
if(el.vector_nodes2 == null){
out_image2.println(el.node2.index);
}
else{
for(int j = 0; j < el.vector_nodes2.size(); j++){
RNode temp = (RNode) el.vector_nodes2.elementAt(j);
out_image2.print(temp.index + " ");
}
out_image2.println("");
}
}
}
out_image2.close();
out_image1.close();
//this code is just for image****************************************
*/
//Computes total similarity
for(int i = 0; i < MPAIR.size(); i++){
MPairElement el = (MPairElement) MPAIR.elementAt(i);
SIM_R_S = SIM_R_S + sim(el.node1, el.node2);
}
System.out.print("Similarity between "+str[0]+" and "+str[1]+" is: ");
System.out.println(SIM_R_S);
}
//takes a node from NLIST that has the largest value of sim(m,m) and find a matching
//pair for this node
public static void lookForMatchingPair(String str[]){
Vector temp_vector, temp_vector2;
temp_vector = new Vector();
temp_vector2 = new Vector();
Vector list1 = (Vector) NLIST.elementAt(0);
Vector list2 = (Vector) NLIST.elementAt(1);
if(list1.size() == 0 || list2.size() == 0)
return;
boolean is_first_time = true;
int select_nlist2 = -1, index2 = -1;
int same_range_node_num;
int select_nlist, index;
MPairElement pair;
Vector tmp_v = findMaximumSim(); //returns a node that has the largest value for sim(m,m)
pair = (MPairElement) tmp_v.elementAt(0);
Integer select_nlist_o = (Integer) tmp_v.elementAt(1);
Integer index_o = (Integer) tmp_v.elementAt(2);
select_nlist = select_nlist_o.intValue();
index = index_o.intValue();
Vector n_list, vec_m, vec_n;
RNode node, matched_node, temp_node, m = null, n = null, temp_node2, temp_node3, temp_node4;
double range_diff, max_mat, temp_double;
int what_res_m = -1, what_res_n = -1;
Integer temp_int_m, temp_int_n;
max_mat = -1000000000;
//look for a matching pair
//if(pair.node1 == null){
if(select_nlist == 2){
n_list = (Vector) NLIST.elementAt(0);
node = pair.node2;
}
else{
n_list = (Vector) NLIST.elementAt(1);
node = pair.node1;
}
Vector vector_nodes;
boolean first = true;
int i = 0;
boolean check = false;
for(i = 0; i < n_list.size(); i++){
temp_node = (RNode) n_list.elementAt(i);
//both nodes have to be from the same range
if(temp_node.left_bound == node.left_bound
&& temp_node.right_bound == node.right_bound){
//if(pair.node1 == null){
if(select_nlist == 2){
m = temp_node;
n = node;
}
else{
m = node;
n = temp_node;
}
range_diff = m.right_bound - m.left_bound;
what_res_m = calculateIndexInMRG1(m);
what_res_n = calculateIndexInMRG2(n);
Vector temp_vec = (Vector) MRG1.elementAt(what_res_m);
vec_m = (Vector) temp_vec.elementAt(m.index);
temp_vec = (Vector) MRG2.elementAt(what_res_n);
vec_n = (Vector) temp_vec.elementAt(n.index);
//parents of those nodes have to match
if(parentsAreMatched(m, n)){
//MLISTs for two nodes must be the same
if(n.MLIST.equals(m.MLIST)){
int candidates_count = 1;
Vector match_candidates = new Vector();
RNode the_node = temp_node;
if(select_nlist == 1)
same_range_node_num = howManySameRangeNodesInNList(the_node, 2);
else
same_range_node_num = howManySameRangeNodesInNList(the_node, 1);
// takes into account sequence of R-nodes in order to account for cases when mu values are divided into different ranges
//select_nlist=1 indicate that CompareReebGraph.MRG2 has the largest sim(m,m) value
//select_nlist=0 indicate that CompareReebGraph.MRG1 has the largest sim(m,m) value
while(createMatchCandidates(the_node, n_list, match_candidates, candidates_count, select_nlist)){
for(int h = 0; h < match_candidates.size(); h++){
MatchCandidate candidate_element = (MatchCandidate) match_candidates.elementAt(h);
if(candidates_count != 1){
temp_node = new RNode();
temp_node.index = the_node.index;
temp_node.left_bound = the_node.left_bound;
temp_node.right_bound = the_node.right_bound;
temp_node.parent = the_node.parent;
temp_node.children = copyChildren(null, the_node.children);
temp_node.MLIST = new Vector();
temp_node.MLIST = (Vector) the_node.MLIST.clone();
temp_node.attribute = new AttributeElement();
temp_node.attribute.a = the_node.attribute.a;
temp_node.attribute.l = the_node.attribute.l;
for(int g = 1; g < candidate_element.vector.size(); g++){
Integer tmp_in = (Integer) candidate_element.vector.elementAt(g);
temp_node4 = (RNode) n_list.elementAt(tmp_in.intValue());
temp_node.children = copyChildren(temp_node.children, temp_node4.children);
temp_node.attribute.a = temp_node.attribute.a + temp_node4.attribute.a;
temp_node.attribute.l = temp_node.attribute.l + temp_node4.attribute.l;
}
vector_nodes = candidate_element.vector;
}
else{
vector_nodes = null;
temp_node = the_node;
}
if(select_nlist == 2){
m = temp_node;
n = node;
}
else{
m = node;
n = temp_node;
}
temp_double = mat(m, n);
if(max_mat < temp_double){
max_mat = temp_double;
if(select_nlist == 2){
pair.node1 = temp_node;
if(vector_nodes == null)
temp_vector = null;
else
temp_vector = (Vector) vector_nodes.clone();
select_nlist2 = 1;
index2 = i;
}
else{
pair.node2 = temp_node;
if(vector_nodes == null)
temp_vector = null;
else
temp_vector = (Vector) vector_nodes.clone();
select_nlist2 = 2;
index2 = i;
}
}
if(select_nlist == select_nlist2)
System.out.println("WARNING in CompareReebGraph.lookForMatchingPair(): select_nlist and select_nlist2 point to the same MRG!");
temp_int_m = (Integer) m.MLIST.elementAt(m.MLIST.size() - 1);
temp_int_n = (Integer) n.MLIST.elementAt(n.MLIST.size() - 1);
//propagate labels
if(vector_nodes == null){
for(int j = 1; j < vec_m.size(); j++){
temp_node2 = (RNode) vec_m.elementAt(j);
if(temp_node2.left_bound == m.right_bound
&& temp_node2.right_bound == m.right_bound + range_diff
&& temp_int_m.intValue() > 0){
temp_node2.MLIST.addElement(new Integer(temp_int_m.intValue() + 1));
}
if(temp_node2.right_bound == m.left_bound
&& temp_node2.left_bound == m.left_bound - range_diff
&& temp_int_m.intValue() < 0){
temp_node2.MLIST.addElement(new Integer(temp_int_m.intValue() - 1));
}
}
for(int j = 1; j < vec_n.size(); j++){
temp_node2 = (RNode) vec_n.elementAt(j);
if(temp_node2.left_bound == n.right_bound
&& temp_node2.right_bound == n.right_bound + range_diff
&& temp_int_n.intValue() > 0){
temp_node2.MLIST.addElement(new Integer(temp_int_n.intValue() + 1));
}
if(temp_node2.right_bound == n.left_bound
&& temp_node2.left_bound == n.left_bound - range_diff
&& temp_int_n.intValue() < 0){
temp_node2.MLIST.addElement(new Integer(temp_int_n.intValue() - 1));
}
}
}
}
match_candidates = new Vector();
// if(same_range_node_num > 20 && candidates_count > 2 && candidates_count < 6){
// candidates_count = 6;
// }
// else
candidates_count++;
}
}
}
}
}
// if a matching mair can not be found then R-node is removed from NLIST
// and lookForMatchingPair(str) is called recursively
if(pair.node1 == null || pair.node2 == null){
if(select_nlist == 1){
RNode nn = (RNode) list1.remove(index);
}
else{
RNode nn = (RNode) list2.remove(index);
}
pair = null;
lookForMatchingPair(str);
}
//removes matching nodes from NLISTs and add this pair to MPAIR list
else{
Vector new_vec = null;
if(temp_vector != null){
new_vec = new Vector();
for(int r = 0; r < temp_vector.size(); r++){
Integer ti = (Integer) temp_vector.elementAt(r);
RNode tn = (RNode) n_list.elementAt(ti.intValue());
new_vec.addElement(tn);
}
}
if(select_nlist == 1){
pair.vector_nodes1 = null;
if(temp_vector == null){
pair.vector_nodes2 = null;
}
else{
pair.vector_nodes2 = new_vec;
}
}
else{
pair.vector_nodes2 = null;
if(temp_vector == null){
pair.vector_nodes1 = null;
}
else{
pair.vector_nodes1 = new_vec;
}
}
MPAIR.addElement(pair);
m = pair.node1;
n = pair.node2;
if(select_nlist == 1)
list1.removeElementAt(index);
else
list2.removeElementAt(index);
if(temp_vector == null){
if(select_nlist2 == 1)
list1.removeElementAt(index2);
else
list2.removeElementAt(index2);
}
else{
for(int h = 0; h < temp_vector.size(); h++){
Integer tmp_i2 = (Integer) temp_vector.elementAt(h);
RNode ntemp = (RNode) n_list.elementAt(tmp_i2.intValue());
n_list.setElementAt(null, tmp_i2.intValue());
}
int h = 0;
while(h < n_list.size()){
RNode tmp_i2 = (RNode) n_list.elementAt(h);
if(tmp_i2 == null)
n_list.removeElementAt(h);
else
h++;
}
}
if(select_nlist == select_nlist2)
System.out.println("WARNING in CompareReebGraph.lookForMatchingPair(): select_nlist and select_nlist2 point to the same MRG!");
what_res_m = calculateIndexInMRG1(m);
what_res_n = calculateIndexInMRG2(n);
if(m.children != null && what_res_m > 0){
vec_m = (Vector) MRG1.elementAt(what_res_m - 1);
for(int z = 0; z < m.children.size(); z++){
Integer temp_int = (Integer) m.children.elementAt(z);
Vector tv = (Vector) vec_m.elementAt(temp_int.intValue());
temp_node = (RNode) tv.elementAt(0);
if(! list1.contains(temp_node))
list1.addElement(temp_node);
}
}
if(n.children != null && what_res_n > 0){
vec_n = (Vector) MRG2.elementAt(what_res_n - 1);
for(int z = 0; z < n.children.size(); z++){
Integer temp_int = (Integer) n.children.elementAt(z);
Vector tv = (Vector) vec_n.elementAt(temp_int.intValue());
temp_node = (RNode) tv.elementAt(0);
if(! list2.contains(temp_node))
list2.addElement(temp_node);
}
}
}
}
//calculates the number of nodes that are connected to R-node node
public static int howManySameRangeNodesInNList(RNode node, int select_nlist){
int counter = 1;
Vector nlist;
if(select_nlist == 1)
nlist = (Vector) NLIST.elementAt(0);
else
nlist = (Vector) NLIST.elementAt(1);
Vector vector_nodes = new Vector();
for(int i = 0; i < nlist.size(); i++){
RNode temp_node = (RNode) nlist.elementAt(i);
vector_nodes.addElement(new Integer(i));
if(temp_node.left_bound == node.left_bound
&& temp_node.right_bound == node.right_bound
&& isConnectedToOneNode(node, vector_nodes, nlist, select_nlist, 5)){
counter++;
}
else
vector_nodes.removeElementAt(vector_nodes.size() - 1);
}
return counter;
}
//select_nlist=1 indicate that CompareReebGraph.MRG2 has the largest sim(m,m) value
//select_nlist=0 indicate that CompareReebGraph.MRG1 has the largest sim(m,m) value
public static boolean createMatchCandidates(RNode node, Vector n_list, Vector match_candidates, int candidates_count, int select_nlist){
//only takes a look at the sequences that are no longer than 5 nodes
if(candidates_count > 6)
return false;
//this is used so that a sequence with the maximum length can be taken into account
if(candidates_count == 6){
int node_index_in_nlist = -1;
MatchCandidate element = new MatchCandidate();
RNode temp_node;
for(int i = 0; i < n_list.size(); i++){
temp_node = (RNode) n_list.elementAt(i);
if(temp_node.index == node.index
&& temp_node.left_bound == node.left_bound
&& temp_node.right_bound == node.right_bound
&& temp_node.attribute.a == node.attribute.a
&& temp_node.attribute.l == node.attribute.l){
node_index_in_nlist = i;
element.vector = new Vector();
element.vector.addElement(new Integer(node_index_in_nlist));
}
}
if(node_index_in_nlist == -1){
System.out.println("ERROR in CompareReebGraph.createMatchCandidates(): can not find R-node in n_list that match ranges of R-node node. Exiting... ");
System.exit(1);
}
for(int i = 0; i < n_list.size(); i++){
temp_node = (RNode) n_list.elementAt(i);
if(node_index_in_nlist != i
&& temp_node.left_bound == node.left_bound
&& temp_node.right_bound == node.right_bound){
int mrg_num;
if(select_nlist == 1)
mrg_num = 2;
else
mrg_num = 1;
Integer node_index = new Integer(i);
if(! element.vector.contains(node_index)
&& isConnectedToOneNode(temp_node, element.vector, n_list, mrg_num, candidates_count)){
element.vector.addElement(node_index);
}
}
}
match_candidates.addElement(element);
if(element.vector.size() > 5)
return true;
else
return false;
}
//computes sequences that are 1 to 5 nodes long
int counter = 0;
boolean stopper = false;
int node_index_in_nlist = -1;
MatchCandidate element = new MatchCandidate();
RNode temp_node;
for(int i = 0; i < n_list.size(); i++){
temp_node = (RNode) n_list.elementAt(i);
if(temp_node.index == node.index
&& temp_node.left_bound == node.left_bound
&& temp_node.right_bound == node.right_bound
&& temp_node.attribute.a == node.attribute.a
&& temp_node.attribute.l == node.attribute.l){
node_index_in_nlist = i;
element.vector = new Vector();
element.vector.addElement(new Integer(node_index_in_nlist));
}
}
if(node_index_in_nlist == -1){
System.out.println("ERROR in CompareReebGraph.createMatchCandidates(): can not find R-node in n_list that match ranges of R-node node. Exiting... ");
System.exit(1);
}
counter++;
match_candidates.addElement(element);
if(counter == candidates_count){
return true;
}
boolean found_one = false;
while(stopper != true){
match_candidates.addElement(null);
element = (MatchCandidate) match_candidates.remove(0);
found_one = false;
while(element != null){
for(int i = 0; i < n_list.size(); i++){
temp_node = (RNode) n_list.elementAt(i);
Integer tmp_int;
if(element.vector.size() > 1)
tmp_int = (Integer) element.vector.elementAt(element.vector.size() - 1);
else
tmp_int = new Integer(-1);
if(tmp_int.intValue() < i && node_index_in_nlist != i
&& temp_node.left_bound == node.left_bound
&& temp_node.right_bound == node.right_bound){
int mrg_num;
if(select_nlist == 1)
mrg_num = 2;
else
mrg_num = 1;
Integer node_index = new Integer(i);
if(! element.vector.contains(node_index)
&& isConnectedToOneNode(temp_node, element.vector, n_list, mrg_num, candidates_count)){
MatchCandidate new_el = new MatchCandidate();
new_el.cloneElement(element);
new_el.vector.addElement(node_index);
found_one = true;
match_candidates.addElement(new_el);
}
}
}
element = (MatchCandidate) match_candidates.remove(0);
}
if(found_one == true){
counter++;
}
else if(found_one == false){
stopper = true;
}
if(counter == candidates_count)
return true;
}
return false;
}
// returns union of two sets of integers (target and new_children)
public static Vector copyChildren(Vector target, Vector new_children){
Vector result = null;
if(target == null && new_children != null)
result = new Vector();
else if(target != null)
result = (Vector) target.clone();
if(new_children != null){
for(int i = 0; i < new_children.size(); i++){
Integer temp = (Integer) new_children.elementAt(i);
if(! result.contains(temp))
result.addElement(temp);
}
}
return result;
}
//checks that node and all R-nodes whos indices are stored in vector_nodes are connected to one R-node
public static boolean isConnectedToOneNode(RNode node, Vector vector_nodes, Vector n_list, int what_MRG, int candidates_count){
if(vector_nodes == null)
return false;
Integer temp_int = (Integer) vector_nodes.elementAt(0);
Vector reeb_graph, vec, temp_vec;
RNode main_node = (RNode) n_list.elementAt(temp_int.intValue());
RNode temp_node, temp_node2;
boolean is_connected = true;
if(what_MRG == 1)
reeb_graph = (Vector) MRG1.elementAt(calculateIndexInMRG1(main_node));
else
reeb_graph = (Vector) MRG2.elementAt(calculateIndexInMRG2(main_node));
vec = (Vector) reeb_graph.elementAt(main_node.index);
for(int i = 1; i < vec.size(); i++){
temp_node = (RNode) vec.elementAt(i);
for(int j = 1; j < vector_nodes.size(); j++){
temp_int = (Integer) vector_nodes.elementAt(j);
temp_node2 = (RNode) n_list.elementAt(temp_int.intValue());
temp_vec = (Vector) reeb_graph.elementAt(temp_node2.index);
if(! temp_vec.contains(temp_node)){
is_connected = false;
break;
}
}
if(is_connected == true){
Vector tmp = (Vector) reeb_graph.elementAt(node.index);
if(tmp.contains(temp_node) && ! node.equals(temp_node))
return true;
}
is_connected = true;
}
return false;
}
public static boolean allAlone(RNode node, Vector vector_nodes, Vector n_list, int what_MRG){
for(int i = 0; i < vector_nodes.size(); i++){
Integer temp_int = (Integer) vector_nodes.elementAt(i);
RNode temp = (RNode) n_list.elementAt(temp_int.intValue());
if(! isAlone(temp, what_MRG))
return false;
}
return isAlone(node, what_MRG);
}
public static boolean isAlone(RNode node, int what_MRG){
Vector mrg;
int index_in_mrg;
if(what_MRG == 1){
mrg = MRG1;
index_in_mrg = calculateIndexInMRG1(node);
}
else{
mrg = MRG2;
index_in_mrg = calculateIndexInMRG2(node);
}
Vector reeb_graph = (Vector) mrg.elementAt(index_in_mrg);
Vector adj_vec = (Vector) reeb_graph.elementAt(node.index);
RNode temp = (RNode) adj_vec.elementAt(0);
if(adj_vec.size() == 1 && temp.index == node.index
&& (node.children == null || node.children.size() == 1))
return true;
else
return false;
}
public static Vector findMaximumSim(){
Vector result = new Vector();
int select_nlist, index;
Vector NLIST1 = (Vector) NLIST.elementAt(0);
Vector NLIST2 = (Vector) NLIST.elementAt(1);
RNode temp_node, maximum_node;
double maximum_sim, temp;
int MRG_no = 1;
temp_node = (RNode) NLIST1.elementAt(0);
maximum_sim = sim(temp_node, temp_node);
maximum_node = temp_node;
index = 0;
for(int i = 1; i < NLIST1.size(); i++){
temp_node = (RNode) NLIST1.elementAt(i);
temp = sim(temp_node, temp_node);
if(temp > maximum_sim){
maximum_sim = temp;
maximum_node = temp_node;
MRG_no = 1;
index = i;
}
}
for(int i = 0; i < NLIST2.size(); i++){
temp_node = (RNode) NLIST2.elementAt(i);
temp = sim(temp_node, temp_node);
if(temp > maximum_sim){
maximum_sim = temp;
maximum_node = temp_node;
MRG_no = 2;
index = i;
}
}
MPairElement element = new MPairElement();
if(MRG_no == 1){
element.node1 = maximum_node;
element.node2 = null;
}
else{
element.node1 = null;
element.node2 = maximum_node;
}
select_nlist = MRG_no;
result.addElement(element);
result.addElement(new Integer(select_nlist));
result.addElement(new Integer(index));
return result;
}
//R-node m must be from MRG1, and R-node n is from MRG2
public static boolean parentsAreMatched(RNode m, RNode n){
if(m.parent == null || n.parent == null)
return true;
for(int i = 0; i < MPAIR.size(); i++){
MPairElement el = (MPairElement) MPAIR.elementAt(i);
if(el.vector_nodes1 == null && el.vector_nodes2 == null){
if(m.parent.equals(el.node1) && n.parent.equals(el.node2))
return true;
}
else{
if(el.vector_nodes1 != null){
if(el.vector_nodes1.contains(m.parent) && n.parent.equals(el.node2))
return true;
}
else{
if(el.vector_nodes2.contains(n.parent) && m.parent.equals(el.node1))
return true;
}
}
}
return false;
}
public static void computeParents(){
RNode element, element2;
Integer temp_int;
Vector reeb_graph, next_reeb_graph;
//calculating parent for each RNode in MRG1
for(int i = 1; i < MRG1.size(); i++){
reeb_graph = (Vector) MRG1.elementAt(i);
for(int j = 0; j < reeb_graph.size(); j++){
Vector vec = (Vector) reeb_graph.elementAt(j);
element = (RNode) vec.elementAt(0);
if(i == MRG1.size() - 1)
element.parent = null;
if(element.children != null){
for(int k = 0; k < element.children.size(); k++){
temp_int = (Integer) element.children.elementAt(k);