-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtet_mesh.cpp
7605 lines (7024 loc) · 173 KB
/
tet_mesh.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
# include <cstdlib>
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cmath>
# include <ctime>
using namespace std;
# include "tet_mesh.hpp"
//****************************************************************************80
int i4_max ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MAX returns the maximum of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 October 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, are two integers to be compared.
//
// Output, int I4_MAX, the larger of I1 and I2.
//
{
int value;
if ( i2 < i1 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
int i4_min ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MIN returns the minimum of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 October 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, two integers to be compared.
//
// Output, int I4_MIN, the smaller of I1 and I2.
//
{
int value;
if ( i1 < i2 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
void i4_swap ( int *i, int *j )
//****************************************************************************80
//
// Purpose:
//
// I4_SWAP switches two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 January 2002
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input/output, int *I, *J. On output, the values of I and
// J have been interchanged.
//
{
int k;
k = *i;
*i = *j;
*j = k;
return;
}
//****************************************************************************80
int i4_uniform_ab ( int a, int b, int *seed )
//****************************************************************************80
//
// Purpose:
//
// I4_UNIFORM_AB returns a scaled pseudorandom I4.
//
// Discussion:
//
// The pseudorandom number should be uniformly distributed
// between A and B.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 12 November 2006
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Paul Bratley, Bennett Fox, Linus Schrage,
// A Guide to Simulation,
// Springer Verlag, pages 201-202, 1983.
//
// Pierre L'Ecuyer,
// Random Number Generation,
// in Handbook of Simulation,
// edited by Jerry Banks,
// Wiley Interscience, page 95, 1998.
//
// Bennett Fox,
// Algorithm 647:
// Implementation and Relative Efficiency of Quasirandom
// Sequence Generators,
// ACM Transactions on Mathematical Software,
// Volume 12, Number 4, pages 362-376, 1986.
//
// Peter Lewis, Allen Goodman, James Miller
// A Pseudo-Random Number Generator for the System/360,
// IBM Systems Journal,
// Volume 8, pages 136-143, 1969.
//
// Parameters:
//
// Input, int A, B, the limits of the interval.
//
// Input/output, int *SEED, the "seed" value, which should NOT be 0.
// On output, SEED has been updated.
//
// Output, int I4_UNIFORM_AB, a number between A and B.
//
{
int k;
float r;
int value;
if ( *seed == 0 )
{
cerr << "\n";
cerr << "I4_UNIFORM_AB - Fatal error!\n";
cerr << " Input value of SEED = 0.\n";
exit ( 1 );
}
k = *seed / 127773;
*seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
if ( *seed < 0 )
{
*seed = *seed + 2147483647;
}
r = ( float ) ( *seed ) * 4.656612875E-10;
//
// Scale R to lie between A-0.5 and B+0.5.
//
r = ( 1.0 - r ) * ( ( float ) ( i4_min ( a, b ) ) - 0.5 )
+ r * ( ( float ) ( i4_max ( a, b ) ) + 0.5 );
//
// Use rounding to convert R to an integer between A and B.
//
value = r4_nint ( r );
value = i4_max ( value, i4_min ( a, b ) );
value = i4_min ( value, i4_max ( a, b ) );
return value;
}
//****************************************************************************80
int i4col_compare ( int m, int n, int a[], int i, int j )
//****************************************************************************80
//
// Purpose:
//
// I4COL_COMPARE compares columns I and J of an I4COL.
//
// Example:
//
// Input:
//
// M = 3, N = 4, I = 2, J = 4
//
// A = (
// 1 2 3 4
// 5 6 7 8
// 9 10 11 12 )
//
// Output:
//
// I4COL_COMPARE = -1
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 12 June 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input, int A[M*N], an array of N columns of vectors of length M.
//
// Input, int I, J, the columns to be compared.
// I and J must be between 1 and N.
//
// Output, int I4COL_COMPARE, the results of the comparison:
// -1, column I < column J,
// 0, column I = column J,
// +1, column J < column I.
//
{
int k;
//
// Check.
//
if ( i < 1 )
{
cout << "\n";
cout << "I4COL_COMPARE - Fatal error!\n";
cout << " Column index I = " << i << " is less than 1.\n";
exit ( 1 );
}
if ( n < i )
{
cout << "\n";
cout << "I4COL_COMPARE - Fatal error!\n";
cout << " N = " << n << " is less than column index I = " << i << ".\n";
exit ( 1 );
}
if ( j < 1 )
{
cout << "\n";
cout << "I4COL_COMPARE - Fatal error!\n";
cout << " Column index J = " << j << " is less than 1.\n";
exit ( 1 );
}
if ( n < j )
{
cout << "\n";
cout << "I4COL_COMPARE - Fatal error!\n";
cout << " N = " << n << " is less than column index J = " << j << ".\n";
exit ( 1 );
}
if ( i == j )
{
return 0;
}
k = 1;
while ( k <= m )
{
if ( a[k-1+(i-1)*m] < a[k-1+(j-1)*m] )
{
return (-1);
}
else if ( a[k-1+(j-1)*m] < a[k-1+(i-1)*m] )
{
return 1;
}
k = k + 1;
}
return 0;
}
//****************************************************************************80
void i4col_sort_a ( int m, int n, int a[] )
//****************************************************************************80
//
// Purpose:
//
// I4COL_SORT_A ascending sorts the columns of an I4COL.
//
// Discussion:
//
// In lexicographic order, the statement "X < Y", applied to two
// vectors X and Y of length M, means that there is some index I, with
// 1 <= I <= M, with the property that
//
// X(J) = Y(J) for J < I,
// and
// X(I) < Y(I).
//
// In other words, X is less than Y if, at the first index where they
// differ, the X value is less than the Y value.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 12 June 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the number of rows of A.
//
// Input, int N, the number of columns of A.
//
// Input/output, int A[M*N].
// On input, the array of N columns of M vectors;
// On output, the columns of A have been sorted in ascending
// lexicographic order.
//
{
int i;
int indx;
int isgn;
int j;
//
// Initialize.
//
i = 0;
indx = 0;
isgn = 0;
j = 0;
//
// Call the external heap sorter.
//
for ( ; ; )
{
sort_heap_external ( n, &indx, &i, &j, isgn );
//
// Interchange the I and J objects.
//
if ( 0 < indx )
{
i4col_swap ( m, n, a, i, j );
}
//
// Compare the I and J objects.
//
else if ( indx < 0 )
{
isgn = i4col_compare ( m, n, a, i, j );
}
else if ( indx == 0 )
{
break;
}
}
return;
}
//****************************************************************************80
void i4col_sort2_a ( int m, int n, int a[] )
//****************************************************************************80
//
// Purpose:
//
// I4COL_SORT2_A ascending sorts the elements of each column of an I4COL.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 January 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the number of rows of A.
//
// Input, int N, the number of columns of A, and the length
// of a vector of data.
//
// Input/output, int A[M*N].
// On input, the array of N columns of M vectors.
// On output, the elements of each column of A have been sorted in ascending
// order.
//
{
int col;
int i;
int indx;
int isgn;
int j;
int row;
int temp;
if ( m <= 1 )
{
return;
}
if ( n <= 0 )
{
return;
}
//
// Initialize.
//
for ( col = 0; col < n; col++ )
{
i = 0;
indx = 0;
isgn = 0;
j = 0;
//
// Call the external heap sorter.
//
for ( ; ; )
{
sort_heap_external ( m, &indx, &i, &j, isgn );
//
// Interchange the I and J objects.
//
if ( 0 < indx )
{
temp = a[i-1+col*m];
a[i-1+col*m] = a[j-1+col*m];
a[j-1+col*m] = temp;
}
//
// Compare the I and J objects.
//
else if ( indx < 0 )
{
if ( a[j-1+col*m] < a[i-1+col*m] )
{
isgn = +1;
}
else
{
isgn = -1;
}
}
else if ( indx == 0 )
{
break;
}
}
}
return;
}
//****************************************************************************80
int i4col_sorted_unique_count ( int m, int n, int a[] )
//****************************************************************************80
//
// Purpose:
//
// I4COL_SORTED_UNIQUE_COUNT counts unique elements in an I4COL.
//
// Discussion:
//
// The columns of the array may be ascending or descending sorted.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 17 February 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input, int A[M*N], a sorted array, containing
// N columns of data.
//
// Output, int I4COL_SORTED_UNIQUE_COUNT, the number of unique columns.
//
{
int i;
int j1;
int j2;
int unique_num;
if ( n <= 0 )
{
unique_num = 0;
return unique_num;
}
unique_num = 1;
j1 = 0;
for ( j2 = 1; j2 < n; j2++ )
{
for ( i = 0; i < m; i++ )
{
if ( a[i+j1*m] != a[i+j2*m] )
{
unique_num = unique_num + 1;
j1 = j2;
break;
}
}
}
return unique_num;
}
//****************************************************************************80
void i4col_swap ( int m, int n, int a[], int icol1, int icol2 )
//****************************************************************************80
//
// Purpose:
//
// I4COL_SWAP swaps two columns of an I4COL.
//
// Discussion:
//
// The two dimensional information is stored as a one dimensional
// array, by columns.
//
// The row indices are 1 based, NOT 0 based! However, a preprocessor
// variable, called OFFSET, can be reset from 1 to 0 if you wish to
// use 0-based indices.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 April 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input/output, int A[M*N], an array of data.
//
// Input, int ICOL1, ICOL2, the two columns to swap.
// These indices should be between 1 and N.
//
{
# define OFFSET 1
int i;
int t;
//
// Check.
//
if ( icol1 - OFFSET < 0 || n-1 < icol1 - OFFSET )
{
cout << "\n";
cout << "I4COL_SWAP - Fatal error!\n";
cout << " ICOL1 is out of range.\n";
exit ( 1 );
}
if ( icol2 - OFFSET < 0 || n-1 < icol2 - OFFSET )
{
cout << "\n";
cout << "I4COL_SWAP - Fatal error!\n";
cout << " ICOL2 is out of range.\n";
exit ( 1 );
}
if ( icol1 == icol2 )
{
return;
}
for ( i = 0; i < m; i++ )
{
t = a[i+(icol1-OFFSET)*m];
a[i+(icol1-OFFSET)*m] = a[i+(icol2-OFFSET)*m];
a[i+(icol2-OFFSET)*m] = t;
}
return;
# undef OFFSET
}
//****************************************************************************80
void i4i4_sort_a ( int i1, int i2, int *j1, int *j2 )
//****************************************************************************80
//
// Purpose:
//
// I4I4_SORT_A ascending sorts a pair of I4's.
//
// Discussion:
//
// The program allows the reasonable call:
//
// i4i4_sort_a ( i1, i2, &i1, &i2 );
//
// and this will return the reasonable result.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 11 October 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, the values to sort.
//
// Output, int J1, J2, the sorted values.
//
{
int k1;
int k2;
//
// Copy arguments, so that the user can make "reasonable" calls like:
//
// i4i4_sort_a ( i1, i2, &i1, &i2 );
//
k1 = i1;
k2 = i2;
*j1 = i4_min ( k1, k2 );
*j2 = i4_max ( k1, k2 );
return;
}
//****************************************************************************80
void i4i4i4_sort_a ( int i1, int i2, int i3, int *j1, int *j2, int *j3 )
//****************************************************************************80
//
// Purpose:
//
// I4I4I4_SORT_A ascending sorts a triple of I4's.
//
// Discussion:
//
// The program allows the reasonable call:
//
// i4i4i4_sort_a ( i1, i2, i3, &i1, &i2, &i3 );
//
// and this will return the reasonable result.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 12 October 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, I3, the values to sort.
//
// Output, int *J1, *J2, *J3, the sorted values.
//
{
int k1;
int k2;
int k3;
//
// Copy arguments, so that the user can make "reasonable" calls like:
//
// i4i4i4_sort_a ( i1, i2, i3, &i1, &i2, &i3 );
//
k1 = i1;
k2 = i2;
k3 = i3;
*j1 = i4_min ( i4_min ( k1, k2 ), i4_min ( k2, k3 ) );
*j2 = i4_min ( i4_max ( k1, k2 ),
i4_min ( i4_max ( k2, k3 ), i4_max ( k3, k1 ) ) );
*j3 = i4_max ( i4_max ( k1, k2 ), i4_max ( k2, k3 ) );
return;
}
//****************************************************************************80
void i4mat_transpose_print ( int m, int n, int a[], string title )
//****************************************************************************80
//
// Purpose:
//
// I4MAT_TRANSPOSE_PRINT prints an I4MAT, transposed.
//
// Discussion:
//
// An I4MAT is an MxN array of I4's, stored by (I,J) -> [I+J*M].
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 31 January 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the number of rows in A.
//
// Input, int N, the number of columns in A.
//
// Input, int A[M*N], the M by N matrix.
//
// Input, string TITLE, a title to be printed.
//
{
i4mat_transpose_print_some ( m, n, a, 1, 1, m, n, title );
return;
}
//****************************************************************************80
void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo,
int ihi, int jhi, string title )
//****************************************************************************80
//
// Purpose:
//
// I4MAT_TRANSPOSE_PRINT_SOME prints some of an I4MAT, transposed.
//
// Discussion:
//
// An I4MAT is an MxN array of I4's, stored by (I,J) -> [I+J*M].
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 14 June 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the number of rows of the matrix.
// M must be positive.
//
// Input, int N, the number of columns of the matrix.
// N must be positive.
//
// Input, int A[M*N], the matrix.
//
// Input, int ILO, JLO, IHI, JHI, designate the first row and
// column, and the last row and column to be printed.
//
// Input, string TITLE, a title for the matrix.
//
{
# define INCX 10
int i;
int i2hi;
int i2lo;
int j;
int j2hi;
int j2lo;
if ( 0 < s_len_trim ( title ) )
{
cout << "\n";
cout << title << "\n";
}
//
// Print the columns of the matrix, in strips of INCX.
//
for ( i2lo = ilo; i2lo <= ihi; i2lo = i2lo + INCX )
{
i2hi = i2lo + INCX - 1;
i2hi = i4_min ( i2hi, m );
i2hi = i4_min ( i2hi, ihi );
cout << "\n";
//
// For each row I in the current range...
//
// Write the header.
//
cout << " Row: ";
for ( i = i2lo; i <= i2hi; i++ )
{
cout << setw(6) << i << " ";
}
cout << "\n";
cout << " Col\n";
cout << "\n";
//
// Determine the range of the rows in this strip.
//
j2lo = i4_max ( jlo, 1 );
j2hi = i4_min ( jhi, n );
for ( j = j2lo; j <= j2hi; j++ )
{
//
// Print out (up to INCX) entries in column J, that lie in the current strip.
//
cout << setw(5) << j << " ";
for ( i = i2lo; i <= i2hi; i++ )
{
cout << setw(6) << a[i-1+(j-1)*m] << " ";
}
cout << "\n";
}
}
return;
# undef INCX
}
//****************************************************************************80
void i4vec_print ( int n, int a[], string title )
//****************************************************************************80
//
// Purpose:
//
// I4VEC_PRINT prints an I4VEC.
//
// Discussion:
//
// An I4VEC is a vector of I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 14 November 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of components of the vector.
//
// Input, int A[N], the vector to be printed.
//
// Input, string TITLE, a title to be printed first.
// TITLE may be blank.
//
{
int i;
if ( 0 < s_len_trim ( title ) )
{
cout << "\n";
cout << title << "\n";
}
cout << "\n";
for ( i = 0; i < n; i++ )
{
cout << " " << setw(8) << i
<< " " << setw(8) << a[i] << "\n";
}
return;
}
//****************************************************************************80
int i4vec_sum ( int n, int a[] )
//****************************************************************************80
//
// Purpose:
//
// I4VEC_SUM sums the entries of an I4VEC.
//
// Discussion:
//
// An I4VEC is a vector of I4's.
//
// Example:
//
// Input:
//
// A = ( 1, 2, 3, 4 )
//
// Output:
//
// I4VEC_SUM = 10
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified: