-
Notifications
You must be signed in to change notification settings - Fork 0
/
plminor.c
2226 lines (2155 loc) · 77.6 KB
/
plminor.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 "nrutil.c" */
/****************************/
/* 'Voronoi' minor routines */
/****************************/
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * *V O R O R D E R ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: vororder(short i1, j1, ko[])
*
* Arguments: i1 = periodic index of first periodic box
* j1 = periodic index of second periodic box
* ko[] = an ordered list of nine periodic boxes
* beginning with the periodic boxes which lie
* closest to `i1' and `j1'.
*
* Return value: none
*
* Action: This is icing on the cake for the Voronoi routines.
* It facilitate the search for a third point of a
* triangle in routine `dltri1()' by listing the optimum
* order in which to search neighbouring periodic boxes.
* It is just an optimisation to improve efficiency.
*
* The periodic boxes which are considered in the Voronoi
* routine are just an array of 3x3 boxes given by
* integer indices (kx= {-1, 0, +1}, ky= {-1, 0, +1} ).
* The vectors which are encoded in the periodic indices
* `i1' and `j1' identify two periodic boxes out of these
* nine. The purpose of this routine is to list the nine
* periodic boxes in the order of which are closest to
* the two given boxes (e.g. the first two boxes in this
* list will inevitably be the two boxes `i1' and `j1'
* themselves). The resulting list of nine boxes are
* encoded as periodic indices and returned in the
* array `ko[0..8]'.
*
* Acknowledgements: Adapted from a routine by J. P. Kermode.
*
*****************************************************************************/
void vororder(i1, j1, ko)
short i1, j1, *ko;
{
short kx, ky, k1, k2, k3, jo[9], kk[9], d[9], d1, d2;
/*
* The array variables have the following meanings:
* kk[i] = records the order of indices 0..8
* jo[i] = records the periodic index of the i'th box
* (corresponding to integer vector (kx, ky) )
* d[i] = contains the `distance' of the i'th periodic box (= jo[i])
* from the periodic box pair `i1' and `j1'.
*
*/
/*
* Loop over all nine boxes (surrounding and including
* the central box (kx= 0, ky= 0) )...
*/
k1=0;
for (kx= -1; kx<=1; kx++) {
for (ky= -1; ky<=1; ky++) {
kk[k1]=k1;
jo[k1]=PERFN(kx,ky);
/*
* These next three lines effectively define the `metric'
* for the distance from the k1'th box to the pair ( i1 and j1 )
*/
d1=abs(PERX(i1)-kx)+abs(PERY(i1)-ky);
d2=abs(PERX(j1)-kx)+abs(PERY(j1)-ky);
d[k1]= (d1<d2) ? d1 : d2;
k1++;
}
}
/*
* Do a crude bubble sort (mea culpa !) on the list of indices `kk[i]'...
* (based on putting the distances `d[i]' into ascending order)
*/
for (k1=8; k1>0; k1--)
for (k2=k1-1; k2>=0; k2--)
if (d[kk[k1]]<d[kk[k2]]) {
k3=kk[k1]; kk[k1]=kk[k2]; kk[k2]=k3;
}
/*
* Copy the sorted list into the returned array `ko[0..8]'...
*/
for (k1=0; k1<9; k1++) ko[k1]=jo[kk[k1]];
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * T R I C E N ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: boolean tricen(REAL x1, y1, x2, y2, x3, y3, *xc, *yc)
*
* Arguments: (x1, y1) = first apex of triangle
* (x2, y2) = second apex of triangle
* (x3, y3) = third apex of triangle
* (*xc, *yc) = returned value of circumcentre
*
* Return value: FALSE if the three given points are *almost* exactly
* colinear; otherwise TRUE
*
* Action: Returns the circumcentre of the triangle given by
* (x1, y1), (x2, y2), (x3, y3) unless these three points
* are almost exactly colinear.
*
*****************************************************************************/
boolean tricen(x1, y1, x2, y2, x3, y3, xc, yc)
REAL x1, y1, x2, y2, x3, y3, *xc, *yc;
{
REAL x12, y12, x23, y23, m12, m23;
void plerror();
x12=(x1+x2)/2.0; y12=(y1+y2)/2.0;
x23=(x2+x3)/2.0; y23=(y2+y3)/2.0;
m12=y2-y1; m23=y3-y2;
if (fabs(m12)>1e-6) {
m12 = -(x2-x1)/m12;
if (fabs(m23)>1e-6) {
m23= -(x3-x2)/m23;
if (fabs(m12-m23)>fabs(1.0e-6*m23)) {
*xc=(m12*x12-m23*x23-y12+y23)/(m12-m23);
*yc=m12*(*xc-x12)+y12;
}
else { plerror("centre not found in routine tricen"); return FALSE; }
}
else { *xc=x23; *yc=m12*(x23-x12)+y12; }
}
else if (fabs(m23)>1e-6) {
m23= -(x3-x2)/m23;
*xc=x12; *yc=m23*(x12-x23)+y23;
} else { plerror("centre not found in routine tricen"); return FALSE; }
return TRUE;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * P T B O X ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: ptbox(REAL *xc, *yc, short *i3, *j3, *k3)
*
* Arguments: (*xc, *yc) = a given point which may need to be
* translated into the central periodic box
* *i3, *j3, *k3 = three periodic indices which we
* would like to be translated in tandem with
* the point (*xc, *yc) (if need be).
*
* Return value: none
*
* Action: It is used by the Voronoi routines effectively to move
* a triangle into the central periodic box.
* The (*xc, *yc) are the given coordinates of the
* triangle's centre and *i3, *j3, *k3 are the periodic
* indices of the triangle vertices. All of these given
* variables are changed so as to have (*xc, *yc) lie
* inside the central box.
*
*****************************************************************************/
void ptbox(xc, yc, i3, j3, k3)
REAL *xc, *yc;
short *i3, *j3, *k3;
{
if (*xc< -boxwid/2.0) {
*xc += boxwid;
*i3=PERINCX(*i3); *j3=PERINCX(*j3); *k3=PERINCX(*k3);
}
else if (*xc>boxwid/2.0) {
*xc -= boxwid;
*i3=PERDECX(*i3); *j3=PERDECX(*j3); *k3=PERDECX(*k3);
}
if (*yc< -boxhgt/2.0) {
*yc += boxhgt;
*i3=PERINCY(*i3); *j3=PERINCY(*j3); *k3=PERINCY(*k3);
}
else if (*yc>boxhgt/2.0) {
*yc -= boxhgt;
*i3=PERDECY(*i3); *j3=PERDECY(*j3); *k3=PERDECY(*k3);
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * I N B O X ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: boolean inbox(short i, i1)
*
* Arguments: i = index of cell centre
* (= Delauney triangle apex)
* i1 = periodic index associated with i
*
* Return value: Tells whether Delauney triangle apex `i', after
* translating by the periodic vector encoded in `i1',
* lies **in** the central periodic **box**
*
* Action: (Ditto `Return value')
*
*****************************************************************************/
boolean inbox(i, i1)
short i, i1;
{
REAL x, y;
x=cx[i]+boxwid*PERX(i1); y=cy[i]+boxhgt*PERY(i1);
if ((x<-boxwid/2.0) || (x>boxwid/2.0) || (y<-boxhgt/2.0) || (y>boxhgt/2.0)) return FALSE;
else return TRUE;
}
/******************/
/* minor routines */
/******************/
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * S Q F ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL sqf(REAL x)
*
* Arguments: x = any real number
*
* Return value: x*x
*
*****************************************************************************/
REAL sqf(x)
REAL x; { return (x*x); }
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * N O R M A L I Z E P ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: normalizep()
*
* Arguments: none
*
* Return value: none
*
* Action: Shifts all the cell pressures and border pressures by
* a constant amount so that the average cell pressure
* works out at zero.
*
* It is an arbitrary convention but it is useful because
* the (usually negative) values of Plateau border
* pressures tell us straightaway something about the
* curvature of Plateau border arcs.
* This routine is called whenever significant changes
* are made to the network in order to keep the pressures
* up to date.
*
*****************************************************************************/
void normalizep()
{
/* This routine normalises the average cell pressure to zero */
/* and sets 'bp' to have the appropriate negative value */
short i, ii;
REAL cptot=0.0, bptot=0.0, dbpav;
for (ii=0; ii<nc; ii++) {
i=clist[ii];
cptot += cp[i];
}
cptot /= nc;
/* now subtract this average pressure from everything! */
for (ii=0; ii<nc; ii++) {
i=clist[ii];
cp[i] -= cptot;
}
for (ii=0; ii<nb; ii++) {
i=blist[ii];
bp[i] -= cptot;
bptot += bp[i];
}
dbpav= -bpav;
bpav=bptot/nb;
dbpav += bpav;
for (ii=0; ii<nb; ii++) {
i=blist[ii];
/* here relax the discrepancies from 'bpav' */
bp[i] -= sbprelax*(bp[i]-bpav);
}
for (ii=0; ii<nbub; ii++) {
i=bublist[ii];
cp[i] += dbpav;
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * N O R M A L I Z E B A ( )* * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: normalizeba()
*
* Arguments: none
*
* Return value: none
*
* Action: Make sure that the total area of all the objects inside
* a periodic box is *equal* to the area of the periodic
* box (discrepancies could arise due to cumulative
* numerical errors). If an adjustment needs to be made
* then adjust the areas of the Plateau borders so that
* it all adds up properly.
*
*****************************************************************************/
void normalizeba()
{
/* This subroutine ensures that the total area fits in the box by */
/* fractionally adjusting the areas of the borders */
short i, ii;
REAL catot, batot, f;
catot=0.0;
for (ii=0; ii<nc; ii++) {
i=clist[ii];
catot += carea[i]+darea[i];
}
batot=0.0;
for (ii=0; ii<nb; ii++) {
i=blist[ii];
batot += barea[i];
}
f=1.0+(boxwid*boxhgt-(catot+batot))/batot;
for (ii=0; ii<nb; ii++) {
i=blist[ii];
barea[i] *= f;
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * *P U T I N B O X ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: putinbox(short i)
*
* Arguments: i = index of vertex in a wet foam
*
* Return value: none
*
* Action: Puts the vertex with index `i' back into the central
* periodic box (if it is not already in it).
* This ensures that the coordinates (vx[i], vy[i])
* always lie within the bounds of the central periodic
* box. It is also necessary to update the array
* `vper[][]' at the same time (which tells you how to
* translate your neighbours so as to move them into the
* same vicinity as yourself). The entries in `vper[][]'
* belonging to each of your three neighbours must also
* be updated (so that they still know how to move
* you into *their* vicinity).
*
*****************************************************************************/
void putinbox(i)
short i;
{
REAL xlim, ylim, x, y;
short j, k, k1;
xlim=boxwid/2.0; ylim= boxhgt/2.0;
x=vx[i]; y=vy[i];
if (x>xlim) {
vx[i] -= boxwid;
for (k=0; k<3; k++) {
vper[i][k]=PERDECX(vper[i][k]);
j=vnbr[i][k]; k1=knbr[k];
/*
* nbr moves in opposite direction...
*/
vper[j][k1]=PERINCX(vper[j][k1]);
}
}
else if (x<-xlim) {
vx[i] += boxwid;
for (k=0; k<3; k++) {
vper[i][k]=PERINCX(vper[i][k]);
j=vnbr[i][k]; k1=knbr[k];
vper[j][k1]=PERDECX(vper[j][k1]);
}
}
if (y>ylim) {
vy[i] -= boxhgt;
for (k=0; k<3; k++) {
vper[i][k]=PERDECY(vper[i][k]);
j=vnbr[i][k]; k1=knbr[k];
vper[j][k1]=PERINCY(vper[j][k1]);
}
}
else if (y<-ylim) {
vy[i] += boxhgt;
for (k=0; k<3; k++) {
vper[i][k]=PERINCY(vper[i][k]);
j=vnbr[i][k]; k1=knbr[k];
vper[j][k1]=PERDECY(vper[j][k1]);
}
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * T R A N S ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: trans(REAL x, y, short per, REAL *x1, *y1)
*
* Arguments: (x, y) = raw coordinate of vertex
* per = periodic index (into which is coded a
* two-dimensional integer vector (per_x, per_y)
* which tells by how many periodic boxes the
* point (x, y) ought be translated)
* (*x1, *y1) = the resulting coordinates after being
* translated by the amount specified in the
* periodic index `per'.
*
* Return value: none
*
* Action: Translates the raw coordinate (x, y) through an
* integer number of periodic boxes up/down and left/right
* according to the integral vector encoded in the
* periodic index `per'.
*
*****************************************************************************/
void trans(x, y, per, x1, y1)
short per;
REAL x, y, *x1, *y1;
{
*x1=x+boxwid*PERX(per);
*y1=y+boxhgt*PERY(per);
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * V N B R X Y ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: vnbrxy(short i, k, REAL *x1, *y1)
*
* Arguments: i = index of a vertex in the wet foam
* k = selects the k'th neighbour of vertex `i'
* (*x1, *y1) = the coordinates of the k'th nbr. of
* vertex `i', after having been translated into
* the immediate vicinity of vertex `i'.
*
* Return value: none
*
* Action: Return the coordinates of the k'th nbr. of vertex `i',
* after having moved the nbr. so as to lie in the
* immediate vicinity of vertex `i'.
*
* NB NB NB -- This very simple routine is of pivotal
* importance in implementing periodic boundary conditions
* in the wet foam: It is the most useful `black box'
* routine of all the periodic boundary routines.
* Whenever any geometrical calculations are done it is
* essential that all the points lie in the same vicinity
* (i.e. we shouldn't be using an image of the point
* which lies in the *wrong* periodic box). This little
* subroutine guarantees that the neighbouring point
* (*x1, *y1) lies in the same vicinity as (vx[i], vy[i])
* and is therefore an essential first step in any
* geometrical calculation.
*
*****************************************************************************/
void vnbrxy(i, k, x1, y1)
short i, k;
REAL *x1, *y1;
{
short j;
j=vnbr[i][k];
trans(vx[j], vy[j], vper[i][k], x1, y1);
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * *V O R T R A N S ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: vortrans(REAL x, y, xc, yc, *x1, *y1)
*
* Arguments: (x, y) = raw coordinate which may need to be moved
* (xc, yc) = coordinate of point we want to be near to
* (*x1, *y1) = translated version of (x, y) after having
* been moved close to (xc, yc)
*
* Return value: none
*
* Action: A crude method of translating the point (x, y) into
* the vicinity of (xc, yc) which has to be used in the
* Voronoi routine (in fact it is not guaranteed to work
* if the number of points is very small). Essentially
* (x, y) is moved by an integer number of periodic boxes
* until it lies within half a periodic box of (xc, yc).
* The resulting point close to (xc, yc) is returned as
* the point (*x1, *y1).
*
*****************************************************************************/
void vortrans(x, y, xc, yc, x1, y1)
REAL x, y, xc, yc, *x1, *y1;
{
REAL d, fsign();
*x1=x;
while (fabs(d= *x1-xc)>boxwid/2.0) *x1 -= fsign(d)*boxwid;
*y1=y;
while (fabs(d= *y1-yc)>boxhgt/2.0) *y1 -= fsign(d)*boxhgt;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * F S I G N ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL fsign(REAL x)
*
* Arguments: x = any real number
*
* Return value: x/|x|
*
*****************************************************************************/
REAL fsign(x)
REAL x;
{ return ((x>0) ? 1.0 : -1.0); }
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * V O R V N B R X Y ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: vorvnbrxy(short i, k, REAL *x1, *y1)
*
* Arguments: i = index of vertex in a dry foam
* k = selects the k'th nbr. of vertex `i'
* (*x1, *y1) = the coordinates of the k'th nbr. of
* vertex `i', after having been translated into
* the immediate vicinity of vertex `i'.
*
* Return value: none
*
* Action: Return the coordinates of the k'th nbr. of vertex `i',
* after having moved the nbr. so as to lie in the
* immediate vicinity of vertex `i'.
*
* This is very similar to `vnbrxy()' except that it
* operates on a dry foam topology instead.
* (Note that this routine does *not* call `vortrans()'
* but calls `trans()' since topological information
* is available in array `vorvper[][]'. Perhaps
* `vortrans()' would be better named ``crudetrans()'' ).
*
*****************************************************************************/
void vorvnbrxy(i, k, x1, y1)
short i, k;
REAL *x1, *y1;
{
void trans();
trans(vx[vorvnbr[i][k]], vy[vorvnbr[i][k]], vorvper[i][k], x1, y1);
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * V O R K I N D E X ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: short vorkindex(short i, j, j1)
*
* Arguments: i = index of first dry foam vertex
* j = index of second dry foam vertex
* j1 = periodic index from `vorvper[i][*]' which
* would translate vertex `j' near to `i'
*
* Return value: an index `k' such that `i = vorvnbr[j][k]'
*
* Action: This routine essentially gives us the `inverse' of the
* array `vorvnbr[i][kk]'. That is, suppose we know that
* `j' is the kk'th nbr. of `i' -- then this routine tells
* us that `i' is the k'th nbr. of `j', where `k' is the
* return value from the routine.
*
* The apparently extraneous value `j1', giving the
* periodic index `vorvper[i][kk]', is necessary to cover
* a tricky eventuality. It is theoretically possible
* that the *same* vertex `i' could be a neighbour of `j'
* more than once, since `j' could be connected to it in
* different periodic boxes. Therefore to specify the
* neighbour relationship uniquely it is necessary to
* specify in which periodic box the nbr. lies via the
* periodic index `j1'.
*
*****************************************************************************/
short vorkindex(i, j, j1)
short i, j, j1;
{
void plerror();
short k1;
j1=PERFN(-PERX(j1),-PERY(j1));
for (k1=0; k1<3; k1++)
if ((i==vorvnbr[j][k1]) && (j1==vorvper[j][k1])) return k1;
plerror("no index found in routine vorkindex");
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * *P E R I O D I C I N D I C E S * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutines: perfn(short ix, iy), perx(short per), pery(short per),
* perincx(short per), perdecx(short per),
* perincy(short per), perdecy(short per).
*
* Arguments: perfn(): (ix, iy) = an integer vector representing
* translation through a certain number of
8 periodic boxes.
* All others: per = a periodic index
*
* Return value: none
*
* Action: Throughout the program it is frequently necessary to
* translate a vertex point (vx[], vx[]) through a
* certain number of periodic boxes. This translation
* through a number of periodic boxes can be specified
* by a *periodic vector* (ix, iy) which is just a pair
* of integers each in the range [-8...+7]. However,
* these periodic vectors are not stored literally in
* arrays, instead they are combined and encoded into
* a *periodic index*.
*
* A periodic index has information encoded in its bits
* in the following format:
* _ _ _ _ _ _ _ _ _
* |8|7|6|5|4|3|2|1|0|
* - - - - - - - - -
* | | | |
* | | |
* | | |
* | | +--- x-vector component
* | +----------- y-vector component
* +----------------- large-arc flag
*
* The ensuing subroutines simply make up a convenient
* interface to this compactly stored information and
* involve some simple exercises in binary arithmetic.
*
* For example `perfn(ix, iy)' simply encodes the
* periodic vector (ix, iy) and returns a periodic index.
* The routines (perx(per), pery(per)) extract the
* periodic vector from the periodic index `per'.
*
* SETLARC() AND LARC():
* The routine `setlarc(per, la)' sets the large-arc flag
* bit of `per' to 0 or 1 according as `la' is true or
* false. The complementary routine `larc(i, k)' returns
* the value (TRUE or FALSE) of the large-arc flag, for
* the arc which runs between vertex `i' and its k'th
* neighbour.
*
*****************************************************************************/
short perfn(ix,iy)
short ix,iy;
{
return ( (iy & 0x0f)<<4 | (ix & 0x0f));
}
short perx(per)
short per;
{
return ( (per&0x08) ? (per&0x0f)-16 : (per&0x0f) );
}
short pery(per)
short per;
{
return ( (per&0x80) ? ((per&0xf0)>>4)-16 : (per&0xf0)>>4 );
}
short perincx(per)
short per;
{
return ( (per & ~0x0f) | ((per & 0x0f)+1)%16 );
}
short perdecx(per)
short per;
{
return ( (per & ~0x0f) | ((per & 0x0f)+15)%16 );
}
short perincy(per)
short per;
{
return ( (per & ~0xf0) | (((per & 0xf0)+0x10) & 0xf0) );
}
short perdecy(per)
short per;
{
return ( (per & ~0xf0) | (((per & 0xf0)+0xf0) & 0xf0) );
}
boolean larc(i, k)
short i, k;
{
return (boolean) (vper[i][k] & LARC)>>8;
}
void setlarc(i1, la)
short *i1;
boolean la;
{ *i1=(*i1 & PERMASK) | ((la) ? LARC : 0x00); }
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * L I S T S O F V A L I D O B J E C T S * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutines: short bgetindex(), short vgetindex(),
* vforgetindex(short i), cforgetindex(short i),
* bforgetindex(short i).
*
* Arguments: i = index of object to be forgotten.
*
* Return value: none
*
* Action: Since the topology of the network changes throughout
* its life, the number of objects of different types
* (cells, vertices etc.) will also change. The lists
* of valid indices for various objects are maintained
* in the following arrays:
* vlist[0..nv] = list of valid vertex indices
* clist[0..nc] = list of valid cell indices
* blist[0..nb] = list of valid border indices
* The indices *stored* within these arrays lie within
* the ranges [0..onv], [0..onc] and [0..onb]
* respectively, corresponding to the original (maximum)
* sizes of these arrays.
*
* The following few subroutines manage the lists of
* valid indices. The `*forgetindex(i)' routines delete
* index `i' from list `*list[]' and the `*getindex()'
* routines return an unused index from list `*list[]'
* for use as a new object.
*
*****************************************************************************/
short vgetindex()
{
void plerror();
short ii, i, j, kk;
j= -1;
if (nv<onv) {
for (ii=0; ii<nv+1; ii++) {
i=vlist[ii];
if (i>j+1 || ii==nv) {
for (kk=nv; kk>ii; kk--) vlist[kk]=vlist[kk-1];
vlist[ii]=j+1; nv++;
return(j+1);
}
j=i;
}
}
plerror("no index found in routine vgetindex");
}
short bgetindex()
{
void plerror();
short i, j, ii, kk;
j= -1;
if (nb<onb) {
for (ii=0; ii<nb+1; ii++) {
i=blist[ii];
if (i>j+1 || ii==nb) {
for (kk=nb; kk>ii; kk--) blist[kk]=blist[kk-1];
blist[ii]=j+1; nb++;
return(j+1);
}
j=i;
}
}
plerror("no index found in routine bgetindex");
}
void vforgetindex(i)
short i;
{
void plerror();
short ii, kk;
for (ii=0; ii<nv; ii++)
if (i==vlist[ii]) break;
if (ii==nv)
plerror("index not found in routine vforgetindex()");
else {
for (kk=ii; kk<nv-1; kk++) vlist[kk]=vlist[kk+1];
nv--;
vlist[nv]=onv+1;
}
}
void cforgetindex(i)
short i;
{
void plerror();
short ii, kk;
for (ii=0; ii<nc; ii++)
if (i==clist[ii]) break;
if (ii==nc)
plerror("index not found in routine cforgetindex()");
else {
for (kk=ii; kk<nc-1; kk++) clist[kk]=clist[kk+1];
nc--;
clist[nc]=onc+1;
}
}
void bforgetindex(i)
short i;
{
void plerror();
short ii, kk;
for (ii=0; ii<nb; ii++)
if (i==blist[ii]) break;
if (ii==nb)
plerror("index not found in routine bforgetindex()");
else {
for (kk=ii; kk<nb-1; kk++) blist[kk]=blist[kk+1];
nb--;
blist[nb]=onb+1;
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * *B U B I N B O X ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: bubinbox(REAL *xc, *yc)
*
* Arguments: (*xc, *yc) = centre of isolated bubble
*
* Return value: none
*
* Action: Translates the centre point of an isolated bubble into
* the central periodic box, if it is not already in
* there.
*
*****************************************************************************/
void bubinbox(xc, yc)
REAL *xc, *yc;
{
REAL halfwid, halfhgt;
REAL fsign();
halfwid=boxwid/2.0; halfhgt=boxhgt/2.0;
while (fabs(*xc)>halfwid) *xc -= fsign(*xc)*boxwid;
while (fabs(*yc)>halfhgt) *yc -= fsign(*yc)*boxhgt;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * P E R C O N C A T ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: short perconcat(short i1, j1)
*
* Arguments: i1, j1 = two periodic indices
*
* Return value: a `concatenated' periodic index
*
* Action: This routine adds the periodic vectors encoded in `i1'
* and `j1', then returns the sum encoded in a new
* periodic vector. (Note: any information stored in the
* `large-arc' flag bits of `i1' and `j1' is not carried
* over to the value returned).
*
*****************************************************************************/
short perconcat(i1, j1)
short i1, j1;
{
short ix, iy, jx, jy;
ix=PERX(i1); iy=PERY(i1);
jx=PERX(j1); jy=PERY(j1);
return PERFN(ix+jx, iy+jy);
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * P I M O D ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL pimod(REAL a)
*
* Arguments: a = given angle in radians
*
* Return value: angle in range [-PI,PI]
*
* Action: Takes the given angle `a' and maps it into the
* range [-PI,PI].
*
*****************************************************************************/
REAL pimod(a)
REAL a;
{
while (fabs(a)>PI) a -= 2.0*PI*( (a>0.0) ? 1.0 : -1.0);
return a;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * T W O P I M O D ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL twopimod(REAL a)
*
* Arguments: a = given angle in radians
*
* Return value: angle in range [0,2*PI]
*
* Action: Takes the given angle `a' and maps it into the
* range [0,2*PI].
*
*****************************************************************************/
REAL twopimod(a)
REAL a;
{
boolean lt=FALSE, gt=FALSE;
REAL pi2;
pi2=2.0*PI;
do {
a += pi2*(( (lt=(a<0.0)) ? 1.0 : 0.0) - ((gt=(a>pi2)) ? 1.0 : 0.0));
} while (lt || gt);
return a;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * L I N L E N ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL linlen(REAL x1, y1, x2, y2)
*
* Arguments: (x1, y1), (x2, y2) = endpoints of a line segment
*
* Return value: length of the line segment
*
*****************************************************************************/
REAL linlen(x1, y1, x2, y2)
REAL x1, y1, x2, y2;
{
return (sqrt(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))) );
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * L I N L E N 2 ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL linlen2(REAL x1, y1, x2, y2)
*
* Arguments: (x1, y1), (x2, y2) = endpoints of a line segment
*
* Return value: the square of the length of the line segment
*
*****************************************************************************/
REAL linlen2(x1, y1, x2, y2)
REAL x1, y1, x2, y2;
{
return((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * C A R C A N G L E ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL carcangle(REAL x1, y1, x2, y2, p1, p2)
*
* Arguments: (x1, y1) = first endpoint of cell-cell arc
* (x2, y2) = second endpoint of cell-cell arc
* p1, p2 = the pressures on either side of the
* cell-cell arc