-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymers.c
1075 lines (900 loc) · 31.5 KB
/
polymers.c
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
/* Pivot Algorithm for Polymers
* Author: Florian Seidler, Jonathan Zopes
* Date: 14.02.2013
* Issue: Computational Physics WT 12/13 , Bonn University */
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <fstream>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#define THREE_DIMENSIONS //Specify Dimension
#define MERSENNE_MAX 4294967295
/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0df /* constant vector a */
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
#define LOWER_MASK 0x7fffffff /* least significant r bits */
/* Tempering parameters */
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_U(y) (y >> 11)
#define TEMPERING_SHIFT_S(y) (y << 7)
#define TEMPERING_SHIFT_T(y) (y << 15)
#define TEMPERING_SHIFT_L(y) (y >> 18)
static unsigned long mt[N]; /* the array for the state vector */
static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
using namespace std;
#ifdef TWO_DIMENSIONS
const int dimension=2;
#endif
#ifdef THREE_DIMENSIONS
const int dimension=3;
#endif
#ifdef FOUR_DIMENSIONS
const int dimension=4;
#endif
/*Tree nodes*/
struct saw_node {
int n; //Number of Sites
saw_node *left; //Left node pointer
saw_node *right; //Right node pointer
saw_node *parent; //Parent node pointer
int** q; //Transformation Matrix
int X[dimension]; //End Vector
int B[2*dimension]; //Bounding box
};
/*-------------------*/
/* Random Number Gen */
/*-------------------*/
/* initializing the array with a NONZERO seed */
void
sgenrand(unsigned long seed)
{
/* setting initial seeds to mt[N] using */
/* the generator Line 25 of Table 1 in */
/* [KNUTH 1981, The Art of Computer Programming */
/* Vol. 2 (2nd Ed.), pp102] */
mt[0]= seed & 0xffffffff;
for (mti=1; mti<N; mti++)
mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
}
unsigned long
genrand()
{
unsigned long y;
static unsigned long mag01[2]={0x0, MATRIX_A};
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= N) { /* generate N words at one time */
int kk;
if (mti == N+1) /* if sgenrand() has not been called, */
sgenrand(4357); /* a default initial seed is used */
for (kk=0;kk<N-M;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (;kk<N-1;kk++) {
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
mti = 0;
}
y = mt[mti++];
y ^= TEMPERING_SHIFT_U(y);
y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
y ^= TEMPERING_SHIFT_L(y);
return y;
}
/* Returns an integer in the interval [a,b) selected uniformly at random */
int random_integer_uniform(int a, int b) {
if(b<=a) {
cout<<"b is smaller than a, error!"<<endl;
return 0;
}
double r = b - a + 1;
return a + (int)(r * genrand()/MERSENNE_MAX);
}
int random_symmetry() {
int sym;
#ifdef TWO_DIMENSIONS
int sym_num = 7;
#endif // TWO_DIMENSIONS
#ifdef THREE_DIMENSIONS
int sym_num = 47;
#endif // THREE_DIMENSIONS
#ifdef FOUR_DIMENSIONS
int sym_num = 383;
#endif // FOUR_DIMENSIONS
sym=random_integer_uniform(2,sym_num);
return sym;
}
/*-------------------*/
/*Internal Operations*/
/*-------------------*/
/*Creates new quadratic matrix*/
int** new_sqmat() {
int** q;
q = new int*[dimension];
for(int i = 0; i < dimension; i++) {
q[i] = new int[dimension];
}
return q;
}
/*Deletes quadratic matrix*/
void delete_sqmat(int** q) {
for(int i = 0; i < dimension; i++) delete[] q[i];
delete[] q;
}
/*Sets single array to constant value*/
void set_single_array(int c, int* array) {
for(int i=0;i<dimension;i++) {
array[i]=c;
}
}
/*Sets double array to constant value*/
void set_array(int c,int** array) {
for(int i=0;i<dimension;i++) {
for(int j=0;j<dimension;j++) {
array[i][j]=c;
}
}
}
/*Copy array to array elementwise*/
void copy_array(int** input, int** result) {
for(int i=0; i<dimension; i++)
{
for(int j=0; j<dimension; j++)
{
result[i][j]=input[i][j];
}
}
}
/*Copy single array to single array elementwise*/
void copy_single_array(int* input, int* result) {
for(int i=0; i<dimension; i++) {
result[i]=input[i];
}
}
/*Set bounding box elements to constant value*/
void set_box(int c, int* array) {
for(int i=0;i<2*dimension;i++) {
array[i]=c;
}
}
/*Matrix multiplication of two quadratic matrices*/
void mat_mul(int** left_op, int** right_op, int** product) {
for(int i = 0; i<dimension; i++) {
for(int j = 0; j<dimension; j++) {
product[i][j] = 0; //product does not need to be initialized to 0.
for(int k = 0; k<dimension; k++) {
product[i][j] += left_op[i][k]*right_op[k][j];
}
}
}
}
/*Matrix with vector multiplication*/
void mat_vec_mul(int** mat, int* vec, int* result) {
for(int i=0; i<dimension; i++) {
result[i] = 0; //result does not need to be initialized to 0.
for(int j=0; j<dimension; j++) {
result[i]+=mat[i][j]*vec[j];
}
}
}
/*Transpose Matrix method*/
void mat_transpose(int** mat, int**result) {
for(int i=0; i<dimension; i++) {
for(int j=0; j<dimension; j++) {
result[i][j]=mat[j][i];
}
}
}
/*Sets up the transformation matrix specified by the number q.*/
void create_transformation_matrix(int q,int** result) {
set_array(0,result); //initialize to zero
#ifdef TWO_DIMENSIONS
//in total 8 symmetry operations
switch(q) {
case 0: result[0][0]++; result[1][1]++; break; //identity -> do nothing
case 1: result[0][1]++; result[1][0]++; break; //reflect in 45deg
case 2: result[0][1]--; result[1][0]--; break; //reflect in -45deg
case 3: result[0][1]++; result[1][0]--; break; //rotate by 90deg clockwise
case 4: result[0][0]--; result[1][1]--; break; //rotate by 180deg clockwise
case 5: result[0][1]--; result[1][0]++; break; //rotate by 270deg clockwise
case 6: result[0][0]++; result[1][1]--; break; //reflect at x-axis
case 7: result[0][0]--; result[1][1]++; break; //reflect at y-axis
default:
cout<<"Wrong symmetry operation: "<<q<<endl;
break;
}
#endif
#ifdef THREE_DIMENSIONS
//in total 48 symmetry operations
switch(q%6) {
case 0: result[0][0]++; result[1][1]++; result[2][2]++; break; // identity
case 1: result[0][1]++; result[1][0]++; result[2][2]++; break; // (xy)
case 2: result[0][0]++; result[1][2]++; result[2][1]++; break; // (yz)
case 3: result[0][2]++; result[1][1]++; result[2][0]++; break; // (xz)
case 4: result[0][2]++; result[1][0]++; result[2][1]++; break; // (xyz)
case 5: result[0][1]++; result[1][2]++; result[2][0]++; break; // (xzy)
default:
cout<<"Wrong symmetry operation: "<<q<<endl;
break;
}
switch(q/6) {
case 0: break; // +++
case 1: result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; break; // ++-
case 2: result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; break; // +-+
case 3: result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; break; // +--
case 4: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; break; // -++
case 5: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; break; // -+-
case 6: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1;
result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; break; // --+
case 7: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1;
result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; break; // ---
default:
cout<<"Wrong symmetry operation: "<<q<<endl;
break;
}
#endif
#ifdef FOUR_DIMENSIONS
switch(q%24) {
case 0 : result[0][0]++; result[1][1]++; result[2][2]++; result[3][3]++; break;
case 1 : result[0][0]++; result[1][1]++; result[2][3]++; result[3][2]++; break;
case 2 : result[0][0]++; result[1][2]++; result[2][1]++; result[3][3]++; break;
case 3 : result[0][0]++; result[1][2]++; result[2][3]++; result[3][1]++; break;
case 4 : result[0][0]++; result[1][3]++; result[2][1]++; result[3][2]++; break;
case 5 : result[0][0]++; result[1][3]++; result[2][2]++; result[3][1]++; break;
case 6 : result[0][1]++; result[1][0]++; result[2][3]++; result[3][2]++; break;
case 7 : result[0][1]++; result[1][0]++; result[2][2]++; result[3][3]++; break;
case 8 : result[0][1]++; result[1][2]++; result[2][0]++; result[3][3]++; break;
case 9 : result[0][1]++; result[1][2]++; result[2][3]++; result[3][0]++; break;
case 10: result[0][1]++; result[1][3]++; result[2][0]++; result[3][2]++; break;
case 11: result[0][1]++; result[1][3]++; result[2][2]++; result[3][0]++; break;
case 12: result[0][2]++; result[1][0]++; result[2][1]++; result[3][3]++; break;
case 13: result[0][2]++; result[1][0]++; result[2][3]++; result[3][1]++; break;
case 14: result[0][2]++; result[1][1]++; result[2][0]++; result[3][3]++; break;
case 15: result[0][2]++; result[1][1]++; result[2][3]++; result[3][0]++; break;
case 16: result[0][2]++; result[1][3]++; result[2][0]++; result[3][1]++; break;
case 17: result[0][2]++; result[1][3]++; result[2][1]++; result[3][0]++; break;
case 18: result[0][3]++; result[1][0]++; result[2][1]++; result[3][2]++; break;
case 19: result[0][3]++; result[1][0]++; result[2][2]++; result[3][1]++; break;
case 20: result[0][3]++; result[1][1]++; result[2][0]++; result[3][2]++; break;
case 21: result[0][3]++; result[1][1]++; result[2][2]++; result[3][0]++; break;
case 22: result[0][3]++; result[1][2]++; result[2][0]++; result[3][1]++; break;
case 23: result[0][3]++; result[1][2]++; result[2][1]++; result[3][0]++; break;
default:
cout<<"Wrong symmetry operation: "<<q<<endl;
break;
}
switch(q/24) {
case 0: break;
case 1: result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // +++-
case 2: result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1; break; // ++-+
case 3: result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1;
result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // ++--
case 4: result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1; break; // +-++
case 5: result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1;
result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // +-+-
case 6: result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1; break; // +--+
case 7: result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1;
result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // +---
case 8: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1; break; // -+++
case 9: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1;
result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // -++-
case 10: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1; break; // -+-+
case 11: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1;
result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // -+--
case 12: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1;
result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1; break; // --++
case 13: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1;
result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1;
result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // --+-
case 14: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1;
result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1; break; // ---+
case 15: result[0][0]*=-1; result[0][1]*=-1; result[0][2]*=-1; result[0][3]*=-1;
result[1][0]*=-1; result[1][1]*=-1; result[1][2]*=-1; result[1][3]*=-1;
result[2][0]*=-1; result[2][1]*=-1; result[2][2]*=-1; result[2][3]*=-1;
result[3][0]*=-1; result[3][1]*=-1; result[3][2]*=-1; result[3][3]*=-1; break; // ----
default:
cout<<"Wrong symmetry operation: "<<q<<endl;
break;
}
#endif
}
/*Adds vectors together*/
void addX(int* point1, int* point2, int* result) {
for(int i=0; i<dimension; i++) {
result[i]=point1[i]+point2[i];
}
}
/*Translation by vector X applied to bounding box B.*/
void transB(int* X, int* B, int* result) {
for(int i=0; i<dimension; i++) {
result[i]=B[i]+X[i];
result[i+dimension]=B[i+dimension]+X[i];
}
}
/* Fuses the boxes B1 and B2.*/
void fuseB(int* B1, int* B2, int* result) {
for(int i=0;i<dimension;i++) {
result[i]=fmin(B1[i],B2[i]);
result[i+dimension]=fmax(B1[i+dimension],B2[i+dimension]);
}
}
/* Checks bounding boxes for overlap. */
bool overlapB(int* B1, int* B2) {
for(int i=0; i<dimension; i++) {
if(fmax(B1[i],B2[i])>fmin(B1[i+dimension],B2[i+dimension])) return false;
}
return true;
}
void printB(int* B) {
for(int i=0;i<dimension;i++) {
cout<<B[i]<<"\t";
}
cout<<endl;
for(int i=dimension;i<2*dimension;i++) {
cout<<B[i]<<"\t";
}
cout<<endl;
}
void printQ(int** q) {
for(int i=0;i<dimension;i++) {
cout<<"(";
for(int j=0;j<dimension;j++) {
cout<<q[i][j]<<" ";
}
cout<<")"<<endl;
}
cout<<endl;
}
void printX(int* X) {
for(int i=0; i<dimension; i++) {
cout<<X[i]<<endl;
}
}
/*-----------------------------------------*/
/*Tree Structure and Operations on the Tree*/
/*-----------------------------------------*/
/*Construct saw nodes with preset values*/
struct saw_node* construct_saw_node(saw_node* parentL, int nL, int** qL, int* XL, int* BL) {
saw_node* construct=new saw_node;
construct->parent=parentL;
construct->n=nL;
construct->left=NULL;
construct->right=NULL;
construct->q = new_sqmat();
for(int i=0; i<dimension; i++) {
for(int j=0; j<dimension; j++) {
construct->q[i][j]=qL[i][j];
}
}
for(int i=0; i<dimension; i++) {
construct->X[i]=XL[i];
}
for(int i=0; i<2*dimension; i++) {
construct->B[i]=BL[i];
}
return construct;
}
/*Construct trivial trees*/
int trivial_tree(saw_node* root, saw_node* leaf) {
int n;
int X[dimension];
int B[2*dimension];
set_single_array(0,X);
set_box(0,B);
int **q=new_sqmat();
create_transformation_matrix(0,q);
saw_node* curr=root;
n=curr->n;
if (n>2) {
n/=2;
X[0]=n;
B[0]=1; // Test to initialize the correct bounding box.
B[dimension]=X[0];
curr->left = construct_saw_node(curr,n,q,X,B);
curr->right = construct_saw_node(curr,n,q,X,B);
trivial_tree(curr->left,leaf);
trivial_tree(curr->right,leaf);
} else {
curr->left=leaf;
curr->right=leaf;
}
delete_sqmat(q);
return 0;
}
/*Generates saw tree with n nodes.*/
struct saw_node *generate_saw_tree(int n) {
if(n<=0){
cout<<"invalid saw-tree size:"<<n<<endl;
return NULL;
}
int D,L;
int X[dimension];
int B[2*dimension];
set_single_array(0,X);
set_box(0,B);
X[0]=n;
B[0]=1; // Test to initialize the correct bounding box.
B[dimension]=n;
int **q=new_sqmat();
create_transformation_matrix(0,q);
saw_node *root = construct_saw_node(NULL,n,q,X,B);
X[0]=1;
B[0]=1; // Test to initialize the correct bounding box.
B[dimension]=1;
saw_node *leaf = construct_saw_node(NULL,1,q,X,B);
D=ceil(log((double)n)/log(2.0));
saw_node* curr = root;
for(L=1;L<D-1;L++) {
if(n-rint(ldexp(1,D-L))>= rint(ldexp(1,D-L-1))) {
X[0] = rint(ldexp(1,D-L));
B[0] = 1; // Test to initialize the correct bounding box.
B[dimension]=X[0];
curr->left = construct_saw_node(curr,X[0],q,X,B);
X[0] = n-rint(ldexp(1,D-L));
B[0] = 1; // Test to initialize the correct bounding box.
B[dimension]=X[0];
curr->right = construct_saw_node(curr,X[0],q,X,B);
//Bounding box initial
trivial_tree(curr->left, leaf);
curr=curr->right;
} else {
X[0] = n-rint(ldexp(1,D-L-1));
B[0] = 1; // Test to initialize the correct bounding box.
B[dimension]=X[0];
curr->left = construct_saw_node(curr,X[0],q,X,B);
X[0] = rint(ldexp(1,D-L-1));
B[0] = 1; // Test to initialize the correct bounding box.
B[dimension]=X[0];
curr->right = construct_saw_node(curr,X[0],q,X,B);
trivial_tree(curr->right, leaf);
curr=curr->left;
}
n=curr->n;
}
switch(n) {
case 1: curr=leaf;
case 2: curr->left = leaf;
curr->right = leaf;
break;
case 3: X[0] = 2;
B[0] = 1; // Test to initialize the correct bounding box.
B[dimension]=X[0];
curr->left = construct_saw_node(curr,X[0],q,X,B);
curr->right = leaf;
curr->left->left = leaf;
curr->left->right = leaf;
break;
case 4: trivial_tree(curr, leaf);
break;
default: cout << "Error in tree generation." << endl;
break;
}
delete_sqmat(q);
return root;
}
/*Clear tree structure and free memory*/
saw_node* clear_internal(saw_node* node) {
saw_node* leaf = NULL;
if(node->right || node->left){
leaf = clear_internal(node->left);
clear_internal(node->right);
} else {
return node;
}
delete_sqmat(node->q);
delete node;
return leaf;
}
/*Clear the tree structure method*/
void clear_saw_tree(saw_node* node) {
saw_node* leaf;
leaf= clear_internal(node);
delete_sqmat(leaf->q);
delete leaf;
}
/*Merge tree wl and wr in tree node w*/
void merge_saw_tree(saw_node* wl, saw_node* wr, saw_node* w) {
//Allocate buffer for transformations
int bufferX1[dimension];
int bufferX2[dimension];
int bufferY1[dimension];
int bufferY2[dimension];
int bufferB1[2*dimension];
int bufferB2[2*dimension];
//Set pointers
w->left=wl;
w->right=wr;
//n=n_l+n_r
w->n=wl->n+wr->n;
//X=X_l + q*X_r
//copy_array(qL,w->q);
mat_vec_mul(w->q,wr->X,bufferX1);
addX(wl->X,bufferX1,w->X);
//B=B_l u (X_l + q*B_r)
//B_r stored in 2 vectors which will be transformed
for(int i=0; i<dimension;i++) {
bufferX1[i]=w->right->B[i];
bufferX2[i]=w->right->B[i+dimension];
}
set_single_array(0,bufferY1);
set_single_array(0,bufferY2);
mat_vec_mul(w->q,bufferX1,bufferY1);
mat_vec_mul(w->q,bufferX2,bufferY2);
for(int i=0; i<dimension;i++) {
bufferB1[i]=fmin(bufferY1[i],bufferY2[i]);
bufferB1[i+dimension]=fmax(bufferY1[i],bufferY2[i]);
}
//Translate bufferB1 by X_l and store in bufferB2
transB(wl->X,bufferB1,bufferB2);
//Fuse bufferB2 with B_l and store in B
fuseB(bufferB2,wl->B,w->B);
}
/*Recursive print call*/
int rec_print(saw_node* node, saw_node* parent, bool right_side, saw_node** node_Arr, int node_Arr_1stfree, fstream& outfile) {
int XL[dimension], XLL[dimension];
if(right_side) {
node_Arr[node_Arr_1stfree] = parent;
node_Arr_1stfree++;
}
if(node->right || node->left) {//not leaf
rec_print(node->left, node, false, node_Arr, node_Arr_1stfree, outfile);
rec_print(node->right, node, true, node_Arr, node_Arr_1stfree, outfile);
} else {
copy(node->X, node->X+dimension, XL);
for(int i = node_Arr_1stfree-1; i >= 0; i--) {
mat_vec_mul(node_Arr[i]->q, XL, XLL);
addX(XLL, node_Arr[i]->left->X, XL);
}
for(int k = 0; k < dimension; k++) outfile << XL[k] << " ";
outfile << endl;
}
if(right_side) {
node_Arr[node_Arr_1stfree-1] = NULL;
node_Arr_1stfree--;
}
return 0;
}
/*Actual print function*/
int print_tree(saw_node* root, char* outfile_name) {
saw_node** node_Arr = new saw_node*[root->n];
for(int i = 0; i < root->n; i++) node_Arr[i] = NULL;
fstream outfile(outfile_name, fstream::out);
rec_print(root, root->parent, false, node_Arr, 0, outfile);
outfile.close();
delete[] node_Arr;
return 0;
}
/*write an Array with spaces terminated by a tab*/
int write_Array(int Arr[], int length, ofstream& outfile) {
for(int i = 0; i < length; i++){
outfile << Arr[i] << " ";
}
outfile << "\t";
return 0;
}
/*Save tree by writing node to a file recursively*/
int save_tree_rec(saw_node* node, ofstream& outfile) {
//write data
outfile << node->n << "\t";
for(int i = 0; i < dimension; i++){
write_Array(node->q[i], dimension, outfile);
}
write_Array(node->X, dimension, outfile);
write_Array(node->B, 2*dimension, outfile);
outfile << endl;
//recursion
if(node->right || node->left){//not leaf
save_tree_rec(node->left, outfile);
save_tree_rec(node->right, outfile);
}
return 0;
}
/*Actual save function*/
int save_tree(saw_node* root, char* outfile_name){
ofstream outfile(outfile_name, ios::out);
save_tree_rec(root, outfile);
outfile.close();
return 0;
}
/*read an Array seperated by spaces*/
int read_Array(int Arr[], int length, ifstream& infile) {
for(int i = 0; i < length; i++){
infile >> Arr[i];
}
return 0;
}
/*Load a tree recursively by reading a file*/
int load_tree_rec(saw_node* &node, saw_node* parentL, saw_node** leaf, ifstream& infile) {
//read data into node
node = new saw_node;
node->parent = parentL;
infile >> node->n;
node->q = new_sqmat();
for(int i = 0; i < dimension; i++){
read_Array(node->q[i], dimension, infile);
}
read_Array(node->X, dimension, infile);
read_Array(node->B, 2*dimension, infile);
if(node->n > 1){//not leaf
//recursion
load_tree_rec(node->left, node, leaf, infile);
load_tree_rec(node->right, node, leaf, infile);
}
else if(!*leaf) {
node->parent = NULL;
node->left = NULL;
node->right = NULL;
leaf = &node;
}
else {
delete_sqmat(node->q);
delete node;
node = *leaf;
}
return 0;
}
/*Actual load function*/
int load_tree(saw_node* &root, char* infile_name) {
ifstream infile("test.txt", ios::in);
saw_node** leaf = new saw_node*;
*leaf = NULL;
load_tree_rec(root, NULL, leaf, infile);
delete leaf;
infile.close();
return 0;
}
/*Left tree-rotation applied to w*/
void LR(saw_node* w) {
int** qt=new_sqmat();
saw_node* wt;
wt=w->right;
w->right=wt->right;
wt->right=wt->left;
wt->left=w->left;
w->left=wt;
//set parents
w->left->left->parent=w->left;
w->right->parent=w;
//change symmetry transformations
copy_array(w->q,qt);
mat_mul(qt,w->left->q,w->q);
copy_array(qt,w->left->q);
//merge(wll,wlr,wl->q)
merge_saw_tree(w->left->left,w->left->right,w->left);
//free memory
delete_sqmat(qt);
}
/*Right tree-rotation applied to w*/
void RR(saw_node* w) {
int** qbuffer=new_sqmat();
int** qt=new_sqmat();
saw_node* wt;
wt=w->left;
w->left=wt->left;
wt->left=wt->right;
wt->right=w->right;
w->right=wt;
//set parents
w->right->right->parent=w->right;
w->left->parent=w;
//change symmetry transformations
copy_array(w->q,qt);
copy_array(w->right->q,w->q);
mat_transpose(w->q,qbuffer);
mat_mul(qbuffer,qt,w->right->q);
merge_saw_tree(w->right->left,w->right->right, w->right);
//free memory
delete_sqmat(qbuffer);
delete_sqmat(qt);
}
/*Shuffle up applied to tree w*/
void shuffle_up(int n0, saw_node* w) {
if(n0<w->left->n) {
shuffle_up(n0,w->left);
RR(w);
}
else if(n0>w->left->n) {
shuffle_up(n0-w->left->n,w->right);
LR(w);
}
return;
}
/*Shuffle down applied to tree w*/
void shuffle_down(saw_node* w) {
int nt;
nt=(w->n+1)/2; //integer division replaces floor function
if(nt<w->left->n) {
RR(w);
shuffle_down(w->right);
}
else if(nt>w->left->n) {
LR(w);
shuffle_down(w->left);
}
return;
}
/*Intersection test function*/
bool intersect(int* xl_abs, int** ql_abs, saw_node* wl, int* xr_abs, int** qr_abs, saw_node* wr)
{
bool result;
//Allocate temporary memory
int bufferX1[dimension];
int bufferX2[dimension];
int bufferY1[dimension];
int bufferY2[dimension];
int bufferB1[2*dimension];
int bufferB2[2*dimension];
int bufferB3[2*dimension];
int** qbuffer=new_sqmat();
//B_l transformed and translated and then stored in bufferB2
for(int i=0; i<dimension;i++) {
bufferX1[i]=wl->B[i];
bufferX2[i]=wl->B[i+dimension];
}
mat_vec_mul(ql_abs,bufferX1,bufferY1);
mat_vec_mul(ql_abs,bufferX2,bufferY2);
for(int i=0; i<dimension;i++) {
bufferB1[i]=fmin(bufferY1[i],bufferY2[i]);
bufferB1[i+dimension]=fmax(bufferY1[i],bufferY2[i]);
}
//Translate bufferB1 by X_l and store in bufferB2
transB(xl_abs,bufferB1,bufferB2);
//B_r transformed and translated and then stored in bufferB3
for(int i=0; i<dimension;i++) {
bufferX1[i]=wr->B[i];
bufferX2[i]=wr->B[i+dimension];
}
mat_vec_mul(qr_abs,bufferX1,bufferY1);
mat_vec_mul(qr_abs,bufferX2,bufferY2);
for(int i=0; i<dimension;i++) {
bufferB1[i]=fmin(bufferY1[i],bufferY2[i]);
bufferB1[i+dimension]=fmax(bufferY1[i],bufferY2[i]);
}
//Translate bufferB1 by X_l and store in bufferB3
transB(xr_abs,bufferB1,bufferB3);
//Check for overlap
if(!overlapB(bufferB2,bufferB3)) {
delete_sqmat(qbuffer);
return false;
}
if(wl->n<=2 && wr->n<=2) {
delete_sqmat(qbuffer);
return true;
}
if(wl->n>=wr->n) {
//ql_abs*X_ll and store in bufferX1
mat_vec_mul(ql_abs,wl->left->X,bufferX1);
//xl_abs+bufferX1 and store in bufferX2
addX(xl_abs,bufferX1,bufferX2);
//ql_abs*wl->q and store in qbuffer
mat_mul(ql_abs,wl->q,qbuffer);
if(intersect(bufferX2,qbuffer,wl->right,xr_abs,qr_abs,wr)) {
delete_sqmat(qbuffer);
return true;
}
delete_sqmat(qbuffer);
return intersect(xl_abs,ql_abs,wl->left,xr_abs,qr_abs,wr);
}
else {
if(intersect(xl_abs,ql_abs,wl,xr_abs,qr_abs,wr->left)){
delete_sqmat(qbuffer);
return true;
}
//qr_abs*X_rl and store in bufferX1
mat_vec_mul(qr_abs,wr->left->X,bufferX1);
//xr_abs+bufferX1 and store in bufferX2
addX(xr_abs,bufferX1,bufferX2);
//qr_abs*qr and store in qbuffer
mat_mul(qr_abs,wr->q,qbuffer);
result=intersect(xl_abs,ql_abs,wl,bufferX2,qbuffer,wr->right);
delete_sqmat(qbuffer);
return result;
}
}
/*Attempt pivot operation*/
bool attempt_pivot(saw_node* w, int nt, int** qt) {
//allocate memory
int** qbuffer1=new_sqmat();
int** qbuffer2=new_sqmat();
int X[dimension];
bool intersection;
shuffle_up(nt,w);
//q<=q*qt
mat_mul(w->q,qt,qbuffer1);
copy_array(qbuffer1,w->q);
//Store identity matrix in qbuffer2 and origin vector in X
create_transformation_matrix(0,qbuffer1);
set_single_array(0,X);
//intersect(0,id,wl,wl->X,q,wr)
intersection=intersect(X,qbuffer1,w->left,w->left->X,w->q,w->right);
//reset the initial walk parameters if the transformation lead to intersection
if(intersection){
mat_transpose(qt,qbuffer1);