-
Notifications
You must be signed in to change notification settings - Fork 0
/
plinit.c
1532 lines (1514 loc) · 59.6 KB
/
plinit.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
#include "include.h"
#include <sys/time.h>
/*********************/
/* Hexagonal network */
/*********************/
#define hexv(i,j) ((i)*(jmax)+(j))
#define hexc(i,j) ((i)*(jmax/2)+((j)/2))
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * H E X N E T ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: hexnet(short nx, short ny, REAL f)
*
* Arguments: nx = number of hex cells vertically
* ny = 2 * number of hex cells horizontally
* f = fraction of cell edge covered by Plateau
* border (i.e. lies in range [0.0, 1.0] )
*
* Return value: none
*
* Action: Sets up a periodic network of regular hexagonal cells
* (i.e. a honeycomb) including Plateau borders. The
* network consists of 2 * ny cells horizontally, before
* the period wraps around, and `nx' cells vertically.
* The total size of the network will thus be `2*nx*ny'.
* Plateau borders have their size determined by
* `f' which is the fraction of a cell edge covered.
*
* The construction of the froth is done in two stages.
* Initially the network is constructed as a *dry* froth
* with no Plateau borders, and vertices placed at the
* apices of perfect hexagons. These vertices are
* organised approximately on a 2-d grid which is then
* indexed by the integers `i' and `j'. This 2-d
* indexing scheme is mapped to the 1-d index of the
* vertex arrays (e.g. vx[], vy[]) via the macro
* `hexv(i,j)' defined above. The slightly messy
* procedure of defining the topology is then taken care
* of within a 2-d loop (e.g. the topological arrays
* `vorvnbr[][]', `vorvper[][]' and `vorcadj[][]' are
* defined. Note that these arrays are specially used to
* store the topology of the dry network and are *not* the
* arrays ultimately used to store the Plateau border
* topology).
*
* The second stage is accomplished via calling the
* general routine `vorplateautopol()' which converts a
* dry froth to a froth with Plateau borders. The
* variables after this point refer to the Plateau border
* network topology.
*
*****************************************************************************/
void hexnet(nx,ny,f)
short nx,ny;
REAL f; /* f = fraction of edge covered by PB */
{
short nmax, imax, jmax, ii, k, c1, i, j, ij, ij1, id, jd, iu, ju, iuu, juu;
short csides();
REAL rt3, a, b, a0, b0, yshift, d, cp1, ca1, ba1, cellarea(), sqf();
boolean found[MCELL], arcbrk;
void vorplateautopol(), plerror();
nx= (nx>0) ? nx : 1;
ny= (ny>0) ? ny : 1;
if (f<0 || f>1.0) { plerror("hexnet: f out of range"); return; }
f= (f==0) ? 0.001 : f;
rt3=sqrt(3.0);
if ( (a=rt3*nx) > (b=((REAL) ny)) ) {
boxwid=1.0; boxhgt=b/a;
}
else {
boxwid=a/b; boxhgt=1.0;
}
a=boxwid/(2.0*nx);
b=boxhgt/(2.0*ny);
yshift= -0.1*b;
imax=2*nx; jmax=2*ny;
for (i=0; i<imax; i+=2)
for (j=0; j<jmax; j+=2) {
/* See defn. of hexv() above to see the relation */
/* between the 1- and the 2-dim indexing. */
a0=a*(i-nx+1); b0=b*(j-ny+1);
id=(i-1+imax)%imax; jd=(j-1+jmax)%jmax;
iu=(i+1)%imax; ju=(j+1)%jmax;
iuu=(i+2)%imax; juu=(j+2)%jmax;
vx[hexv(i,ju)]=a0-a/3.0;
vx[hexv(iu,ju)]=a0+a/3.0;
vy[hexv(i,ju)]=vy[hexv(iu,ju)]=b0+b+yshift;
vx[hexv(i,j)]=a0-2.0*a/3.0;
vx[hexv(iu,j)]=a0+2.0*a/3.0;
vy[hexv(i,j)]=vy[hexv(iu,j)]=b0+yshift;
ij=hexv(i,j);
vorvnbr[ij][0]=hexv(id,j);
vorvper[ij][0]= (id<i) ? 0 : PERFN(-1,0);
vorcadj[ij][0]=hexc(i,j);
vorvnbr[ij][1]=hexv(i,jd);
vorvper[ij][1]= (jd<j) ? 0 : PERFN(0,-1);
vorcadj[ij][1]=hexc(id,j);
vorvnbr[ij][2]=hexv(i,ju);
vorvper[ij][2]=0;
vorcadj[ij][2]=hexc(id,jd);
ij=hexv(i,ju);
vorvnbr[ij][0]=hexv(iu,ju);
vorvper[ij][0]=0;
vorcadj[ij][0]=hexc(id,j);
vorvnbr[ij][1]=hexv(i,juu);
vorvper[ij][1]= (juu>ju) ? 0 : PERFN(0,1);
vorcadj[ij][1]=hexc(i,j);
vorvnbr[ij][2]=hexv(i,j);
vorvper[ij][2]=0;
vorcadj[ij][2]=hexc(i,juu);
ij=hexv(iu,ju);
vorvnbr[ij][0]=hexv(i,ju);
vorvper[ij][0]=0;
vorcadj[ij][0]=hexc(iu,j);
vorvnbr[ij][1]=hexv(iu,j);
vorvper[ij][1]=0;
vorcadj[ij][1]=hexc(i,juu);
vorvnbr[ij][2]=hexv(iu,juu);
vorvper[ij][2]= (juu>ju) ? 0 : PERFN(0,1);
vorcadj[ij][2]=hexc(i,j);
ij=hexv(iu,j);
vorvnbr[ij][0]=hexv(iuu,j);
vorvper[ij][0]= (iuu>iu) ? 0 : 1;
vorcadj[ij][0]=hexc(i,j);
vorvnbr[ij][1]=hexv(iu,ju);
vorvper[ij][1]=0;
vorcadj[ij][1]=hexc(iu,jd);
vorvnbr[ij][2]=hexv(iu,jd);
vorvper[ij][2]= (jd<j) ? 0 : PERFN(0,-1);
vorcadj[ij][2]=hexc(iu,j);
}
nc=2*nx*ny; nv=2*nc;
d=f*a/3.0;
vorplateautopol(d);
for (i=0; i<nv; i++) vlist[i]=i;
for (i=0; i<nc; i++) clist[i]=i;
for (i=0; i<nb; i++) blist[i]=i;
onv=nv; onc=nc; onb=nb;
bpav= -tan(PI/6.0)/d;
for (i=0; i<nb; i++) bp[i]=bpav;
ba1=(sqrt(3.0)-PI/2.0)*sqf(BRADIUS(0.0,bpav));
for (i=0; i<nb; i++) barea[i]=ba1;
for (i=0; i<nc; i++) {
darea[i]=0.0; cp[i]=0.0; ncsides[i]=6;
}
/*ca1=boxwid*boxhgt/nc;*/
/*for (i=0; i<nc; i++) carea[i]=ca1;*/
for (i=0; i<onc; i++) found[i]=FALSE;
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
for (k=1; k<3; k++) {
if (!found[c1=cadj[i][k]]) {
found[c1]=TRUE;
carea[c1]=cellarea(i,k,&arcbrk);
if (arcbrk) { plerror("failure of routine hexnet"); return; }
}
}
}
henckyeps=0.0;
}
/**********************/
/* 'Voronoi' routines */
/**********************/
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * V O R S P R A Y ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: vorspray(REAL *x, REAL *y, REAL wid, REAL hgt,
* REAL rminfrac, short *pnc)
*
* Arguments: (x[], y[]) = Used to hold the coordinates of a random
* set of points (generated by this routine)
* (wid, hgt) = Dimensions of box in which the generated
* random points will be constrained to lie
* rminfrac = Parameter to determine degree of randomness
* of distribution of points (between 0 and 1)
* *pnc = requested number of random points
*
* Return value: none
*
* Action: Generates a random 2-d distribution of points within
* a box of size [-wid/2,wid/2]x[-hgt/2,hgt/2]. The
* degree of randomness can be tuned using the parameter
* `rminfrac'. A value of 0.0 gives complete
* randomness and a value of 1.0 corresponds to perfect
* (hexagonal) ordering of points. The variable
* `rminfrac' expresses the minimum distance between
* particles as a fraction of the value for hexagonal
* close packing.
*
* The number of points requested is passed in `*pnc'
* and the generated points are put into the arrays
* (x[], y[]). Note that it is impractical to request
* distributions with order `rminfrac' much greater
* than about 0.6
*
* Acknowledgements: Adapted from a routine by J. P. Kermode.
*
*****************************************************************************/
void vorspray(x, y, wid, hgt, rminfrac, pnc)
REAL *x, *y, wid, hgt, rminfrac;
short *pnc;
{
REAL d, rmin, x1, y1, x2, y2, ran3(), linlen();
int rseed;
short i, j, nt=0, np=0;
boolean flag;
void plerror(), vortrans();
struct timeval now;
rseed= (int) svorseed;
/* 'rminfrac' expresses the min radius as fraction of the seperation */
/* required for hexagonal close packing of circles. */
rmin=rminfrac*sqrt(8.0*wid*hgt/((*pnc)*sqrt(27.0)));
if (*pnc<1) { plerror("zero cells requested in routine vorspray"); return; }
if (rmin>wid || rmin>hgt) {
*pnc=0; plerror("hard disc too large in routine vorspray");
return;
}
if (rseed!=0) {
#ifndef FIXEDSEED
gettimeofday(&now,NULL);
rseed = now.tv_usec;
#endif
ran3(-rseed);
rseed=0; svorseed=0.0; }
x[0]=(ran3(0)-0.5)*wid; y[0]=(ran3(0)-0.5)*hgt; np++;
while (np<*pnc) {
nt=0;
do {
flag=TRUE;
x1=(ran3(0)-0.5)*wid; y1=(ran3(0)-0.5)*hgt; nt++;
for (i=0; i<np; i++) {
vortrans(x1,y1,x[i],y[i],&x2,&y2);
if ((d=linlen(x2,y2,x[i],y[i])) < rmin) { flag=FALSE; break; }
}
} while (!flag && nt<10000);
if (nt>=10000) {
*pnc=np;
plerror("too many attempts to place a point in routine vorspray");
return;
}
x[np]=x1; y[np]=y1; np++;
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * V O R D E L A U N E Y ( ) * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: boolean vordelauney()
*
* Arguments: none
*
* Return value: Returns FALSE if the construction gets stuck
*
* Action: This routine carries out a Delauney triangulation
* upon the random set of points in the periodic box (as
* obtained from the routine `vorspray()' ). A Delauney
* triangulation consists of joining each point to its
* nearest neighbours in such a way that the plane is
* completely covered by a perfect tiling of
* *non-overlapping* triangles. This tiling of triangles
* is then the dual graph of a dry froth. If we carry
* out the `duality' transformation from a network of
* triangles to a network of cells then we get the
* following mapping:
* triangle vertex <---> cell centre
* = (cx[i], cy[i])
* triangle centre <---> cell vertex
* = (vx[i], vy[i])
* no. of vertex nbrs. <---> no. of sides of cell
* no. of triangle vertices <---> no. of cells
* = `nc'
* no. of triangles <---> no. of cell vertices
* = `nv'
* Note that the names given to the variables we use
* reflect the structure of the dual (dry froth) network.
*
* THE `TRIANG[][][]' ARRAY:
* The array used to hold the topological structure of
* the triangulation is `triang[][][]' (this does not
* appear explicitly here but is accessed via the two
* subroutines `dltri1()' and `dltri2()' ) and
* geometrical information is stored in the arrays
* (vx[], vy[]) which store the centre points of the
* triangles. The format of the array `triang[][][]' is
* as follows:
* triang[triangle_index][nbr_list][info_type]
* where the `triangle_index' (in the range 0..nv)
* selects a particular triangle, `nbr_list' (in the
* range `k = 0, 1, 2') selects one of its three
* neighbours and `info_type' is one of:
* 0 => index of `nbr_list'th nbr.
* 1 => periodic index of `nbr_list'th nbr.
* (the business of periodic indices is a bit tricky,
* essentially telling you how to put the nbr. into the
* same periodic box as the first triangle -- consult the
* suite of routines `vorvnbrxy()', `PERFN()', `PERX()'
* and `PERY()' for more details). Thus, in summary,
* array `triang' essentially tells you who your
* neighbours are.
*
* PERIODIC INDICES:
* Throughout the Delauney routines it is important to
* keep in mind that an (index, periodic index) go
* together as a pair. Both indices, e.g. (i, i1), are
* essentially needed to uniquely identify a point
* (where `i1' essentially tells you which periodic box
* the point is in).
*
* THE ALGORITHM:
* The tricky aspect of the triangulation algorithm is
* that the triangles have to be chosen so as to be
* non-overlapping. There is however a criterion you
* can use when choosing a triangle that guarantees your
* tiling will not overlap. This careful `choice' of a
* triangle is farmed out to the two subroutines
* `dltri1()' and `dltri2()' (and see those routines for
* further details).
*
* We assume the existence of these two engines
* `dltri1()' and `dltri2()' which perform the following
* tasks:
*
* dltri1(&i,&i1,&j,&j1,&k,&k1) -- search for any three
* points, not fully enclosed within the (possibly) already
* partially formed network, which would make a
* satisfactory triangle for the tiling. The points are
* returned as three (index, periodic index) pairs viz.
* (i, i1), (j, j1), (k, k1).
*
* dltri2(i,i1,j,j1,&k,&k1,jj,jj1) -- given two points
* of a Delauney triangle, (i, i1) and (j, j1), find a
* third point (k, k1) *but* don't return with point
* (jj, jj1) because we already have that point.
*
* The algorithm goes as follows:
* (i) Find a seed triangle using `dltri1()' to get three
* points (i,i1), (j,j1), (k,k1).
*
* (ii) Construct a `wheel' using the array `wheel[][]'.
* Essentially we focus on point `i' (as found in (i) )
* and use it as a `pivot' point. A sequence of
* triangles are found (using `dltri2()' ) adding spokes
* to the wheel. All the points around the rim of the
* wheel are stored in
* (index= wheel[rim_index][0],
* periodic index= wheel[rim_index][1] )
* with the `pivot' point `i' remaining the centre of
* the wheel. This process ends when the first point
* of the rim (stored as (je,j1) ) equals the last
* point found.
*
* (iii) Once a wheel-ful of triangles has been found,
* try using one of the points on the rim of the wheel
* as a new pivot point (but check it has not been
* used as a pivot before by looking up the array
* `waspivot[]'). If a pivot point is found then do
* step (iv) else step (v).
*
* (iv) Since this next wheel overlaps with the last one,
* you only need to construct a partial wheel. Mark the
* expected endpoint in the variables (je, je1) and then
* go back to the beginning of the do loop...
*
* (v) Because of the periodic boundary conditions, it is
* perfectly possible that the untiled part can break
* into disconnected regions. Then the `wheel'
* construction will ultimately box itself into a corner.
* When that happens, a pivot point is not found in step
* (iii) and we have to search for a new seed triangle
* using `dltri1()'...then go back to the beginning
* of the do loop...
*
* Acknowledgements: Adapted from a routine by J. P. Kermode.
*
*****************************************************************************/
boolean vordelauney()
{
short wheel[MWHEEL][2];
short i, i1, j, j1, k, k1, jj, jj1, je, je1, l, ll, nwh, npv;
boolean flag, dltri1(), dltri2(), inbox();
void plerror();
for (i=0; i<nc; i++) waspivot[i]=FALSE;
npv=0; /* ...number of pivot points used so far */
i=i1=0;
/*
* Find a seed triangle to start things off...
*/
if (!dltri1(&i,&i1,&j,&j1,&k,&k1)) {
plerror("no seed found in routine vordelauney");
return FALSE;
}
je=j; je1=j1; nwh=0;
do {
/*
* Construct a `wheel' of triangles, storing the points
* along the rim in
* (index= wheel[rim_index][0], periodic index= wheel[rim_index][1] )
*/
do {
wheel[nwh][0]=j; wheel[nwh][1]=j1; nwh++;
jj=j; jj1=j1;
j=k; j1=k1;
dltri2(i,i1,j,j1,&k,&k1,jj,jj1);
if (nv>=(nc+nc)) return TRUE;
} while ((k>=0) && (k!=je || k1!=je1));
waspivot[i]=TRUE; npv++;
j=i; j1=i1; flag=FALSE;
/*
* Search the rim of the wheel for a new pivot point...
*/
for (l=1; (l<nwh-1) && !flag; l++) {
i=wheel[l][0]; i1=wheel[l][1];
if (!waspivot[i] && inbox(i,i1)) {
ll=(l+1)%nwh;
k=wheel[ll][0]; k1=wheel[ll][1];
ll=(l+nwh-1)%nwh;
je=wheel[ll][0]; je1=wheel[ll][1];
nwh=0;
wheel[nwh][0]=je; wheel[nwh][1]=je1; nwh++;
flag=TRUE;
break;
}
}
/*
* If a new pivot point could not be found on the rim
* then cast for a completely new seed triangle to
* get things going again...
*/
if (!flag) {
if (!dltri1(&i,&i1,&j,&j1,&k,&k1)) {
plerror("no seed found in routine vordelauney");
return FALSE;
}
if (nv>=(nc+nc)) return TRUE;
je=j; je1=j1; nwh=0;
}
} while (npv<nc);
return FALSE;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * D L T R I 1 ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: boolean dltri1(short *i, *i1, *j, *j1, *k, *k1)
*
* Arguments: (*i, *i1), (*j, *j1), (*k, *k1) = Three pairs of
* indices in the format (index, periodic index) are
* returned by this routine. These indices identify
* three points which form a Delauney triangle.
*
* Return value: TRUE if a triangle is found, otherwise FALSE.
*
* Action: Used to find the three coordinates of a `seed'
* Delauney triangle -- either to start the network off
* or to get it going again if it gets stuck. It returns
* an (index, periodic index) pair for the three apices
* of a triangle satisfying the Delauney condition.
* The condition states that when we draw a circumcircle
* around the triangle there should be *no* other points
* lying inside this circle.
*
* There are two different searches attempted. First of
* all a search is made of the existing network to find
* a triangle containing a point (*i, *i1) which has not
* yet been used as a pivot point (which implies that it
* lies on the boundary of the triangle network). If
* this succeeds then it is by far the quicker method.
*
* If the first method fails or there is no existing
* network, then a Delauney triangle has to be found from
* scratch. This involves a search using a hefty number
* of nested loops! (see accompanying comments).
*
* This routine guarantees that `waspivot[*i]=FALSE'
* whenever a triangle is returned.
*
* Acknowledgements: Adapted from a routine by J. P. Kermode.
*
*****************************************************************************/
boolean dltri1(i, i1, j, j1, k, k1)
short *i, *i1, *j, *j1, *k, *k1;
{
boolean flag, tricen();
short t, l, ll, l1, jo[9], ko[9], i3, j3, k3, m, m1, mm1, mo[9];
REAL x1, y1, x2, y2, x3, y3, xc, yc, r1, r2, wid=boxwid, hgt=boxhgt,
rr, xm, ym, vorrad, rrlim;
REAL linlen2();
void plerror(), vororder(), ptbox();
/* vorrad= VORRAD*sqrt(boxwid*boxhgt/((REAL) nc)); */
/*
* Set `vorrad' to be roughly equal to the distance between two
* points in the random network...
*/
vorrad=1.1*srfrac*sqrt(8.0*boxwid*boxhgt/(nc*sqrt(27.0)));
/*
* Upper limit for the search area (rather large)...
*/
rrlim=4000.0*vorrad*vorrad;
/*
* Lists the optimum order in which to search neighbouring periodic boxes
* for a third point in `mo[]', when the first two points are in the
* central periodic box...
*/
vororder(0,0,mo);
/*
* First do a search of the triangle network to see if you can
* find a triangle plus a point on this triangle which has not been used
* before as a pivot point. If we succeed then we're done...
*/
if (nv!=0) {
for (t=0; t<nv; t++) {
for (l=0; l<3; l++) {
*i=triang[t][l][0];
if (!waspivot[*i]) {
*i1=triang[t][l][1];
ll=(l+1)%3;
*j=triang[t][ll][0]; *j1=triang[t][ll][1];
ll=(l+2)%3; *k=triang[t][ll][0]; *k1=triang[t][ll][1];
return TRUE;
}
}
}
/*
* ...else take any ol' point (*i, *i1) which has not been used as a pivot point
* before...
*/
for (*i=0; *i<nc; (*i)++) { if (!waspivot[*i]) break; }
}
*i1=0;
rr=vorrad*vorrad; /* ...initial search radius */
x1=cx[*i]; y1=cy[*i];
/*
* Put optimised order to search periodic boxes into `jo[]' given that
* the first point has periodic index *i1
*/
vororder(*i1,*i1,jo);
/*
* `do' loop for ever increasing search area `rr'
* until a Delauney triangle is found...
*/
do {
/*
* Loop over all possible indices (*j, *j1) such that
* (i) (*i, *i1) not equiv (*j, *j1),
* (ii) *j has not been used as a pivot point
* (iii) point *j lies within the search circle
* then given this (*j, *j1), do more nested loops to find (*k, *k1) s.t.
* (i) (*k, *k1) distinct from both (*i, *i1) and (*j, *j1),
* (ii) *k has not been used as a pivot point
* (iii) point *k also lies within the search circle
* Now that we have the prospective triangle given by points
* (*i, *i1), (*j, *j1), (*k, *k1) we have to test for the Delauney
* condition: This state that if we draw a circle through the apices
* of the triangle then there must be no other points within this circle.
* To test this we loop through all other points indexed by (*m, *m1) s.t.
* (i) (*m, *m1) distinct from the three apices of the triangle
* and we require that for *all* of these points that they lie outside the
* circle of centre (xc, yc) and radius `r1', otherwise set flag=FALSE.
*
* If flag remains TRUE then the triangle has passed the Delauney criterion
* and the search has been successful.
*/
for (l=0, *j1=jo[0]; l<9; l++, *j1=jo[l]) {
for (*j=0; *j<nc; (*j)++) {
if ((*i!=*j || *i1!=*j1) && !waspivot[*j]) {
x2=cx[*j]+wid*PERX(*j1); y2=cy[*j]+hgt*PERY(*j1);
if (linlen2(x1,y1,x2,y2)<=rr) {
vororder(*i1,*j1,ko);
for (l1=0, *k1=ko[0]; l1<9; l1++, *k1=ko[l1]) {
for (*k=0; *k<nc; (*k)++) {
if ((*i!=*k || *i1!=*k1) && (*j!=*k || *j1!=*k1) &&
!waspivot[*k]) {
x3=cx[*k]+wid*PERX(*k1); y3=cy[*k]+hgt*PERY(*k1);
if (linlen2(x1,y1,x3,y3)<=rr) {
if (tricen(x1,y1,x2,y2,x3,y3,&xc,&yc)) {
r1=linlen2(xc,yc,x1,y1);
flag=TRUE;
for (m=0; (m<nc) && flag; m++)
for (m1=mo[mm1=0]; (mm1<9) && flag; m1=mo[++mm1])
if ((*i!=m || *i1!=m1) && (*j!=m || *j1!=m1) &&
(*k!=m || *k1!=m1)) {
xm=cx[m]+wid*PERX(m1); ym=cy[m]+hgt*PERY(m1);
r2=linlen2(xc,yc,xm,ym);
if (r2<r1) flag=FALSE;
}
if (flag) {
/*
* Delauney triangle has been successfully found so
* add it to the network here...
*/
/*
* A slightly tricky operation with periodic indices:
* If coordinates (xc, yc) are not in the central periodic
* box then `ptbox()' translates them into it, *and* at
* the same updates i3, j3, k3 so that they will translate
* the respective points into the vicinity of the new values
* of the coordinates (xc, yc).
*/
i3= *i1; j3= *j1; k3= *k1; ptbox(&xc,&yc,&i3,&j3,&k3);
/*
* Record the centre point of the triangle. Note that ultimately
* these coordinates will be the vertices of the dry foam network
*/
vx[nv]=xc; vy[nv]=yc;
triang[nv][0][0]= *i; triang[nv][0][1]=i3;
triang[nv][1][0]= *j; triang[nv][1][1]=j3;
triang[nv][2][0]= *k; triang[nv][2][1]=k3;
nv++;
return TRUE;
}
}
}
}
}
}
}
}
}
}
/*
* Crank up the search radius and try again...
*/
rr *= 1.1;
} while (rr<rrlim);
plerror("failure of routine dltri1");
return FALSE;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * D L T R I 2 ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: boolean dltri2(short i, i1, j, j1, *k, *k1, jj, jj1)
*
* Arguments: (i, i1), (j, j1) = the (index, periodic index) pairs
* for the first two apices of a Delauney triangle.
*
* (*k, *k1) = the (index, periodic index)
* pair for the third apex of a Delauney triangle.
*
* (jj, jj1) = an (index, periodic index) pair
* which is not to be returned in the result (*k, *k1)
* because this third apex has already been found.
*
* Return value: TRUE if successful, otherwise returns FALSE.
*
* Action: This routine is given two adjacent points indexed by
* (i, i1) and (j, j1) on the triangulation network. It
* then finds a third point (*k, *k1) such that all three
* points give a Delauney triangle, satisfying the
* requisite conditions. The extra indices (jj, jj1)
* specify a point which you do *not* want returned in
* (*k, *k1) because you already have this point (in a
* tiling of triangles, a given edge will be adjacent to
* exactly two triangles).
*
* Two different attempts can be made to find a suitable
* triangle. Firstly, the existing network of triangles
* is searched to see if a suitable triangle can be
* found there, with third vertex *k not equal to jj.
*
* Otherwise, a new triangle must be found. A search is
* made for a point (*k, *k1) which completes a Delauney
* triangle with (i, i1) and (j, j1).
*
* Acknowledgements: Adapted from a routine by J. P. Kermode.
*
*****************************************************************************/
boolean dltri2(i, i1, j, j1, k, k1, jj, jj1)
short i, i1, j, j1, *k, *k1, jj, jj1;
{
short ix, iy, ix1, iy1, l, m, n, t, ko[9], m1, mm1, mo[9], i3, j3, k3;
REAL rr, r1, r2, wid, hgt, x1, y1, x2, y2, x3, y3, xc, yc, xm, ym, vorrad,
rrlim;
REAL linlen2();
boolean flag, tricen();
void vororder(), ptbox(), plerror();
/* vorrad= VORRAD*sqrt(boxwid*boxhgt/((REAL) nc)); */
/*
* Set `vorrad' to be roughly equal to the distance between two
* points in the random network...
*/
vorrad=1.1*srfrac*sqrt(8.0*boxwid*boxhgt/(nc*sqrt(27.0)));
/*
* Upper limit for the search area (rather large)...
*/
rrlim=4000.0*vorrad*vorrad;
/*
* Lists the optimum order in which to search neighbouring periodic boxes
* for a third point in `mo[]', when the first two points are in the
* central periodic box...
*/
vororder(0,0,mo);
/*
* First of all try and find a triangle which lies within the existing
* Delauney network (if the network already partly exists)...
*/
if (nv>=1) {
/*
* Note that `PERX()' and `PERY()' are macros which turn a periodic
* index into the X and Y component of an integer vector. This
* integer vector tells you how many periodic boxes to move the point
* up/down or left/right.
*
* Here we are recording the periodic box difference-vector, pointing
* from `i' to `j', to use for comparisons later on...
*/
ix=PERX(j1)-PERX(i1); iy=PERY(j1)-PERY(i1);
/*
* Loop over all triangles `t', and neighbour points on that triangle
* given by `l = 0, 1, 2' and `m = 0, 1, 2' -- see if this triangle
* fits (i, i1), (j, j1) and (*k, *k1)...
*/
for (t=0; t<nv; t++)
for (l=0; l<3; l++)
for (m=0; m<3; m++)
if (m!=l)
if (i==triang[t][l][0] && j==triang[t][m][0])
if ((ix==(PERX(triang[t][m][1])-PERX(triang[t][l][1]))) &&
(iy==(PERY(triang[t][m][1])-PERY(triang[t][l][1])))) {
/*
* ...at this stage `i' and`j' are identical to two of the
* points on the triangle `t' ...
*/
n=3-(l+m); /* nbr. index of the third point on the triangle */
*k=triang[t][n][0]; *k1=triang[t][n][1];
if (*k!=jj
|| (PERX(*k1)-PERX(jj1))!=(PERX(triang[t][l][1])-PERX(i1))
|| (PERY(*k1)-PERY(jj1))!=(PERY(triang[t][l][1])-PERY(i1))) {
/*
* If point (*k, *k1) is not identical to (jj, jj1) then we
* have successfully found a triangle.
*
* Note that we cannot compare *k1 and jj1 directly, since we
* are not sure in which periodic box the points given in the
* argument list are centred. We get around this by
* translating the periodic indices into vectors (using
* PERX() and PERY() ), and then only comparing *relative*
* periodic vectors...
*/
/*
* The integer vector (ix1, iy1) tells us where the periodic
* box of the triangle `t' lies with respect to its image
* given by (i, i1), (j, jj1), (*k, *k1). (This complication
* comes about because we cannot assume that the given
* triangle lies in the same periodic box as the triangle `t'
* that we find). Note that (triang[t][l][0], triang[t][l][1])
* is the equivalent image of (i, i1).
*/
ix1=PERX(triang[t][l][1])-PERX(i1);
iy1=PERY(triang[t][l][1])-PERY(i1);
/*
* The vector (ix1, iy1) is then used to translate *k1 to a
* location which is consistent with (i, i1) and (j, j1).
*
* Note that PERFN() is used to translate a periodic vector
* back into a periodic index...
*/
*k1=PERFN(PERX(*k1)-ix1,PERY(*k1)-iy1);
return TRUE;
}
}
}
/* The initial search radius is `rr'... */
rr=vorrad*vorrad; wid=boxwid; hgt=boxhgt;
x1=cx[i]+wid*PERX(i1); y1=cy[i]+hgt*PERY(i1);
x2=cx[j]+wid*PERX(j1); y2=cy[j]+hgt*PERY(j1);
/*
* Lists the optimum order in which to search neighbouring periodic boxes
* for a third point into `ko[]', when the first two points are in the
* boxes determined by i1 and j1.
*/
vororder(i1,j1,ko);
/*
* ...if the first search failed...
* `do' loop over steadily increasing search radii...
*/
do {
/*
* Loop over all indices (*k, *k1)...
*/
for (l=0, *k1=ko[l]; l<9; l++, *k1=ko[l])
for (*k=0; *k<nc; (*k)++)
if ((i!=*k || i1!=*k1) && (j!=*k || j1!=*k1) && (jj!=*k || jj1!=*k1)
&& !waspivot[*k]) {
/*
* If (*k, *k1) is distinct and has not yet been used as pivot...
*/
x3=cx[*k]+wid*PERX(*k1); y3=cy[*k]+hgt*PERY(*k1);
if (linlen2(x1,y1,x3,y3)<=rr) {
/*
* ...and lies within search area, then do the test for the Delauney
* condition: This states that if we draw a circle through the apices
* of the triangle then there must be no other points within this circle.
* To test this we loop through all other points indexed by (*m, *m1) s.t.
* (i) (*m, *m1) distinct from the three apices of the triangle
* and we require that for *all* of these points that they lie outside the
* circle of centre (xc, yc) and radius `r1', otherwise set flag=FALSE.
*/
flag=FALSE;
if (tricen(x1,y1,x2,y2,x3,y3,&xc,&yc)) {
r1=linlen2(xc,yc,x1,y1);
flag=TRUE;
for (m1=mo[mm1=0]; (mm1<9) && flag; m1=mo[++mm1])
for (m=0; (m<nc) && flag; m++)
if ((i!=m || i1!=m1) && (j!=m || j1!=m1) && (*k!=m || *k1!=m1)) {
xm=cx[m]+wid*PERX(m1); ym=cy[m]+hgt*PERY(m1);
r2=linlen2(xc,yc,xm,ym);
if (r2<r1) flag=FALSE;
}
}
if (flag) {
/*
* Delauney triangle has been successfully found so
* add it to the network here...
*/
/*
* A slightly tricky operation with periodic indices:
* If coordinates (xc, yc) are not in the central periodic
* box then `ptbox()' translates them into it, *and* at
* the same updates i3, j3, k3 so that they will translate
* the respective points into the vicinity of the new values
* of the coordinates (xc, yc).
*/
i3=i1; j3=j1; k3= *k1; ptbox(&xc,&yc,&i3,&j3,&k3);
/*
* Record the centre point of the triangle. Note that ultimately
* these coordinates will be the vertices of the dry foam network
*/
vx[nv]=xc; vy[nv]=yc;
triang[nv][0][0]=i; triang[nv][0][1]=i3;
triang[nv][1][0]=j; triang[nv][1][1]=j3;
triang[nv][2][0]= *k; triang[nv][2][1]=k3;
nv++;
return TRUE;
}
}
}
/*
* Crank up the search radius and try again...
*/
rr *= 1.1;
} while (rr<rrlim);
*k = -1; /* ...value indicates error return */
plerror("failure of routine dltri2");
return FALSE;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * V O R D R Y T O P O L ( ) * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: vordrytopol()
*
* Arguments: none
*
* Return value: none
*
* Action: After the Delauney triangulation has been found it is
* necessary to convert the triangle topology, stored in
* `triang[][][]', into the topology of its dual graph
* to get a dry froth network. Under the duality
* transformation we have some equivalences:
*
* centre of triangle (vx[], vy[]) <--> new vertex coord
* index of triangle `i' <--> index of new vertex
* apex of triangle <--> centre of new cell
* index of triangle apex <--> index of new cell
*
* This routine has essentially to initialise the
* matrices `vorvnbr[][]', `vorcadj[][]' and
* `vorvper[][]' which define the topology of the dry
* froth network. The main loop of this routine runs
* through all possible pairs of triangles `i' and `j'
* in an attempt to find a pair which share one edge in
* common (which means we also have to loop over all of
* the edges `k = 0, 1, 2' of each of the triangles).
* Once an adjacent pair of triangles has been found we
* are able to fill in some information about the topology
* of the dry froth.
*
* Acknowledgements: Adapted from a routine by J. P. Kermode.
*
*****************************************************************************/
void vordrytopol()
{
short i, j, k, k1, px, py, px1, py1, (*t)[MBORD][3][2];
/*
* The matrix ik[][] gives a simple way of selecting neighbour indices
* `k = 0, 1, 2'. If we are given the k'th index, then the *other* two
* indices are the `ik[k][0]'th and the `ik[k][1]'th (out of the set
* {0, 1, 2} ).
*/
static short ik[3][2]={ {1, 2}, {2, 0}, {0, 1} };
REAL x1, y1, x2, y2, x3, y3, cross;
void vorvnbrxy(), plerror();
t= &triang; /* Abbreviation for the `triang[][][]' array */
for (i=0; i<nv; i++) for (k=0; k<3; k++) vorvnbr[i][k]= -1;
/*
* Convert the triangle topology into a dry froth topology.
* Note that the centre points of the triangle (vx[i], vy[i])
* will correspond to the new vertices of the dry froth.
* Therefore to say that "the k'th neighbour of triangle `i' is
* triangle `j' ", is equivalent to saying that the k'th neighbour
* of new vertex `i' is new vertex `j'.
*/
/*
* Loop over all triangles (vertices) `i' and all triangles (vertices) `j'.
* They each loop over their three neighbours indices `k' and `k1'
* respectively, to see if they are each others neighbour.
*/
for (i=0; i<nv-1; i++)
for (k=0; k<3; k++)
if (vorvnbr[i][k]<0)
for (j=i+1; j<nv; j++)
for (k1=0; k1<3; k1++)
if (vorvnbr[j][k1]<0)
if ((*t)[i][ik[k][0]][0]==(*t)[j][ik[k1][0]][0]) {
if ((*t)[i][ik[k][1]][0]==(*t)[j][ik[k1][1]][0]) {
/*
* ...implies two points on the boundary of triangle `i' and
* two points on the boundary of triangle `j' match
* up (in such a way that implies `j' is the k'th nbr. of
* `i' and `i' is the k1'th nbr. of `j')
*/
/*
* Calculate the periodic vector difference between two
* points on each of the triangles which are equivalent
* ==> vector (px, py) tells by how many periodic boxes
* the triangles (new vertices) must be shifted so as to
* line up.
*
* Note that calculating (px1, px2) as well is redundant
* (since it should be equal to (px, py) )
* and is only done as an elementary means of error checking.
*/
px=PERX((*t)[j][ik[k1][0]][1])-PERX((*t)[i][ik[k][0]][1]);
py=PERY((*t)[j][ik[k1][0]][1])-PERY((*t)[i][ik[k][0]][1]);
px1=PERX((*t)[j][ik[k1][1]][1])-PERX((*t)[i][ik[k][1]][1]);
py1=PERY((*t)[j][ik[k1][1]][1])-PERY((*t)[i][ik[k][1]][1]);
if (px==px1 && py==py1) {
vorvnbr[i][k]=j; vorvnbr[j][k1]=i;
/*
* The apices of the triangles are the dual of the cells
* of the dry froth network...so use their indices now as
* cell indices.
*/
vorcadj[i][k]=(*t)[i][k][0]; vorcadj[j][k1]=(*t)[j][k1][0];
vorvper[i][k]=PERFN(-px,-py);
vorvper[j][k1]=PERFN(px,py);
if (abs(px)>1 || abs(py)>1)
plerror("topological error in routine vordrytopol");
}
}
}
else if ((*t)[i][ik[k][0]][0]==(*t)[j][ik[k1][1]][0]) {
if ((*t)[i][ik[k][1]][0]==(*t)[j][ik[k1][0]][0]) {
/*
* ...two points on the boundary of triangle `i' and
* on the boundary of triangle `j' match up contrariwise.
* (in such a way that implies `j' is the k'th nbr. of
* `i' and `i' is the k1'th nbr. of `j')
*/
/* ...see comments for block above ... */
px=PERX((*t)[j][ik[k1][1]][1])-PERX((*t)[i][ik[k][0]][1]);
py=PERY((*t)[j][ik[k1][1]][1])-PERY((*t)[i][ik[k][0]][1]);
px1=PERX((*t)[j][ik[k1][0]][1])-PERX((*t)[i][ik[k][1]][1]);
py1=PERY((*t)[j][ik[k1][0]][1])-PERY((*t)[i][ik[k][1]][1]);
if (px==px1 && py==py1) {
vorvnbr[i][k]=j; vorvnbr[j][k1]=i;
vorcadj[i][k]=(*t)[i][k][0]; vorcadj[j][k1]=(*t)[j][k1][0];
vorvper[i][k]=PERFN(-px,-py);
vorvper[j][k1]=PERFN(px,py);
if (abs(px)>1 || abs(py)>1)
plerror("topological error in routine vordrytopol");
}
}
}
/*
* This loop essentially enforces the convention on how neighbouring