-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstripack.f
6727 lines (6725 loc) · 204 KB
/
stripack.f
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
SUBROUTINE ADDNOD (NST,K,X,Y,Z, LIST,LPTR,LEND,
. LNEW, IER)
INTEGER NST, K, LIST(*), LPTR(*), LEND(K), LNEW, IER
REAL(kind=8) X(K), Y(K), Z(K)
C
C***********************************************************
C
C From STRIPACK
C Robert J. Renka
C Dept. of Computer Science
C Univ. of North Texas
C renka@cs.unt.edu
C 07/08/99
C
C This subroutine adds node K to a triangulation of the
C convex hull of nodes 1,...,K-1, producing a triangulation
C of the convex hull of nodes 1,...,K.
C
C The algorithm consists of the following steps: node K
C is located relative to the triangulation (TRFIND), its
C index is added to the data structure (INTADD or BDYADD),
C and a sequence of swaps (SWPTST and SWAP) are applied to
C the arcs opposite K so that all arcs incident on node K
C and opposite node K are locally optimal (satisfy the cir-
C cumcircle test). Thus, if a Delaunay triangulation is
C input, a Delaunay triangulation will result.
C
C
C On input:
C
C NST = Index of a node at which TRFIND begins its
C search. Search time depends on the proximity
C of this node to K. If NST < 1, the search is
C begun at node K-1.
C
C K = Nodal index (index for X, Y, Z, and LEND) of the
C new node to be added. K .GE. 4.
C
C X,Y,Z = Arrays of length .GE. K containing Car-
C tesian coordinates of the nodes.
C (X(I),Y(I),Z(I)) defines node I for
C I = 1,...,K.
C
C The above parameters are not altered by this routine.
C
C LIST,LPTR,LEND,LNEW = Data structure associated with
C the triangulation of nodes 1
C to K-1. The array lengths are
C assumed to be large enough to
C add node K. Refer to Subrou-
C tine TRMESH.
C
C On output:
C
C LIST,LPTR,LEND,LNEW = Data structure updated with
C the addition of node K as the
C last entry unless IER .NE. 0
C and IER .NE. -3, in which case
C the arrays are not altered.
C
C IER = Error indicator:
C IER = 0 if no errors were encountered.
C IER = -1 if K is outside its valid range
C on input.
C IER = -2 if all nodes (including K) are col-
C linear (lie on a common geodesic).
C IER = L if nodes L and K coincide for some
C L < K.
C
C Modules required by ADDNOD: BDYADD, COVSPH, INSERT,
C INTADD, JRAND, LSTPTR,
C STORE, SWAP, SWPTST,
C TRFIND
C
C Intrinsic function called by ADDNOD: ABS
C
C***********************************************************
C
INTEGER LSTPTR
INTEGER I1, I2, I3, IO1, IO2, IN1, IST, KK, KM1, L,
. LP, LPF, LPO1, LPO1S
LOGICAL SWPTST
REAL(kind=8) B1, B2, B3, P(3)
C
C Local parameters:
C
C B1,B2,B3 = Unnormalized barycentric coordinates returned
C by TRFIND.
C I1,I2,I3 = Vertex indexes of a triangle containing K
C IN1 = Vertex opposite K: first neighbor of IO2
C that precedes IO1. IN1,IO1,IO2 are in
C counterclockwise order.
C IO1,IO2 = Adjacent neighbors of K defining an arc to
C be tested for a swap
C IST = Index of node at which TRFIND begins its search
C KK = Local copy of K
C KM1 = K-1
C L = Vertex index (I1, I2, or I3) returned in IER
C if node K coincides with a vertex
C LP = LIST pointer
C LPF = LIST pointer to the first neighbor of K
C LPO1 = LIST pointer to IO1
C LPO1S = Saved value of LPO1
C P = Cartesian coordinates of node K
C
KK = K
IF (KK .LT. 4) GO TO 3
C
C Initialization:
C
KM1 = KK - 1
IST = NST
IF (IST .LT. 1) IST = KM1
P(1) = X(KK)
P(2) = Y(KK)
P(3) = Z(KK)
C
C Find a triangle (I1,I2,I3) containing K or the rightmost
C (I1) and leftmost (I2) visible boundary nodes as viewed
C from node K.
C
CALL TRFIND (IST,P,KM1,X,Y,Z,LIST,LPTR,LEND, B1,B2,B3,
. I1,I2,I3)
C
C Test for collinear or duplicate nodes.
C
IF (I1 .EQ. 0) GO TO 4
IF (I3 .NE. 0) THEN
L = I1
IF (P(1) .EQ. X(L) .AND. P(2) .EQ. Y(L) .AND.
. P(3) .EQ. Z(L)) THEN ! GO TO 5
PRINT '(A,I6,A,I6,A)', 'NODES ',KK,' AND ',L,'ARE DUPLICATES.'
GO TO 5
ENDIF
L = I2
IF (P(1) .EQ. X(L) .AND. P(2) .EQ. Y(L) .AND.
. P(3) .EQ. Z(L)) THEN !GO TO 5
PRINT '(A,I6,A,I6,A)', 'NODES ',KK,' AND ',L,'ARE DUPLICATES.'
GO TO 5
ENDIF
L = I3
IF (P(1) .EQ. X(L) .AND. P(2) .EQ. Y(L) .AND.
. P(3) .EQ. Z(L)) THEN !GO TO 5
PRINT '(A,I6,A,I6,A)', 'NODES ',KK,' AND ',L,'ARE DUPLICATES.'
GO TO 5
ENDIF
CALL INTADD (KK,I1,I2,I3, LIST,LPTR,LEND,LNEW )
ELSE
IF (I1 .NE. I2) THEN
CALL BDYADD (KK,I1,I2, LIST,LPTR,LEND,LNEW )
ELSE
CALL COVSPH (KK,I1, LIST,LPTR,LEND,LNEW )
ENDIF
ENDIF
IER = 0
C
C Initialize variables for optimization of the
C triangulation.
C
LP = LEND(KK)
LPF = LPTR(LP)
IO2 = LIST(LPF)
LPO1 = LPTR(LPF)
IO1 = ABS(LIST(LPO1))
C
C Begin loop: find the node opposite K.
C
1 LP = LSTPTR(LEND(IO1),IO2,LIST,LPTR)
IF (LIST(LP) .LT. 0) GO TO 2
LP = LPTR(LP)
IN1 = ABS(LIST(LP))
C
C Swap test: if a swap occurs, two new arcs are
C opposite K and must be tested.
C
LPO1S = LPO1
IF ( .NOT. SWPTST(IN1,KK,IO1,IO2,X,Y,Z) ) GO TO 2
CALL SWAP (IN1,KK,IO1,IO2, LIST,LPTR,LEND, LPO1)
IF (LPO1 .EQ. 0) THEN
C
C A swap is not possible because KK and IN1 are already
C adjacent. This error in SWPTST only occurs in the
C neutral case and when there are nearly duplicate
C nodes.
C
LPO1 = LPO1S
GO TO 2
ENDIF
IO1 = IN1
GO TO 1
C
C No swap occurred. Test for termination and reset
C IO2 and IO1.
C
2 IF (LPO1 .EQ. LPF .OR. LIST(LPO1) .LT. 0) RETURN
IO2 = IO1
LPO1 = LPTR(LPO1)
IO1 = ABS(LIST(LPO1))
GO TO 1
C
C KK < 4.
C
3 IER = -1
RETURN
C
C All nodes are collinear.
C
4 IER = -2
RETURN
C
C Nodes L and K coincide.
C
5 IER = L
RETURN
END
REAL(kind=8) FUNCTION AREAS (V1,V2,V3)
REAL(kind=8) V1(3), V2(3), V3(3)
C
C***********************************************************
C
C From STRIPACK
C Robert J. Renka
C Dept. of Computer Science
C Univ. of North Texas
C renka@cs.unt.edu
C 09/18/90
C
C This function returns the area of a spherical triangle
C on the unit sphere.
C
C
C On input:
C
C V1,V2,V3 = Arrays of length 3 containing the Carte-
C sian coordinates of unit vectors (the
C three triangle vertices in any order).
C These vectors, if nonzero, are implicitly
C scaled to have length 1.
C
C Input parameters are not altered by this function.
C
C On output:
C
C AREAS = Area of the spherical triangle defined by
C V1, V2, and V3 in the range 0 to 2*PI (the
C area of a hemisphere). AREAS = 0 (or 2*PI)
C if and only if V1, V2, and V3 lie in (or
C close to) a plane containing the origin.
C
C Modules required by AREAS: None
C
C Intrinsic functions called by AREAS: ACOS, DBLE, REAL,
C SQRT
C
C***********************************************************
C
DOUBLE PRECISION A1, A2, A3, CA1, CA2, CA3, DV1(3),
. DV2(3), DV3(3), S12, S23, S31,
. U12(3), U23(3), U31(3)
INTEGER I
C
C Local parameters:
C
C A1,A2,A3 = Interior angles of the spherical triangle
C CA1,CA2,CA3 = cos(A1), cos(A2), and cos(A3), respectively
C DV1,DV2,DV3 = Double Precision copies of V1, V2, and V3
C I = DO-loop index and index for Uij
C S12,S23,S31 = Sum of squared components of U12, U23, U31
C U12,U23,U31 = Unit normal vectors to the planes defined by
C pairs of triangle vertices
C
DO 1 I = 1,3
DV1(I) = DBLE(V1(I))
DV2(I) = DBLE(V2(I))
DV3(I) = DBLE(V3(I))
1 CONTINUE
C
C Compute cross products Uij = Vi X Vj.
C
U12(1) = DV1(2)*DV2(3) - DV1(3)*DV2(2)
U12(2) = DV1(3)*DV2(1) - DV1(1)*DV2(3)
U12(3) = DV1(1)*DV2(2) - DV1(2)*DV2(1)
C
U23(1) = DV2(2)*DV3(3) - DV2(3)*DV3(2)
U23(2) = DV2(3)*DV3(1) - DV2(1)*DV3(3)
U23(3) = DV2(1)*DV3(2) - DV2(2)*DV3(1)
C
U31(1) = DV3(2)*DV1(3) - DV3(3)*DV1(2)
U31(2) = DV3(3)*DV1(1) - DV3(1)*DV1(3)
U31(3) = DV3(1)*DV1(2) - DV3(2)*DV1(1)
C
C Normalize Uij to unit vectors.
C
S12 = 0.D0
S23 = 0.D0
S31 = 0.D0
DO 2 I = 1,3
S12 = S12 + U12(I)*U12(I)
S23 = S23 + U23(I)*U23(I)
S31 = S31 + U31(I)*U31(I)
2 CONTINUE
C
C Test for a degenerate triangle associated with collinear
C vertices.
C
IF (S12 .EQ. 0.D0 .OR. S23 .EQ. 0.D0 .OR.
. S31 .EQ. 0.D0) THEN
AREAS = 0.
RETURN
ENDIF
S12 = SQRT(S12)
S23 = SQRT(S23)
S31 = SQRT(S31)
DO 3 I = 1,3
U12(I) = U12(I)/S12
U23(I) = U23(I)/S23
U31(I) = U31(I)/S31
3 CONTINUE
C
C Compute interior angles Ai as the dihedral angles between
C planes:
C CA1 = cos(A1) = -<U12,U31>
C CA2 = cos(A2) = -<U23,U12>
C CA3 = cos(A3) = -<U31,U23>
C
CA1 = -U12(1)*U31(1)-U12(2)*U31(2)-U12(3)*U31(3)
CA2 = -U23(1)*U12(1)-U23(2)*U12(2)-U23(3)*U12(3)
CA3 = -U31(1)*U23(1)-U31(2)*U23(2)-U31(3)*U23(3)
IF (CA1 .LT. -1.D0) CA1 = -1.D0
IF (CA1 .GT. 1.D0) CA1 = 1.D0
IF (CA2 .LT. -1.D0) CA2 = -1.D0
IF (CA2 .GT. 1.D0) CA2 = 1.D0
IF (CA3 .LT. -1.D0) CA3 = -1.D0
IF (CA3 .GT. 1.D0) CA3 = 1.D0
A1 = ACOS(CA1)
A2 = ACOS(CA2)
A3 = ACOS(CA3)
C
C Compute AREAS = A1 + A2 + A3 - PI.
C
AREAS = REAL(A1 + A2 + A3 - ACOS(-1.D0),8)
IF (AREAS .LT. 0.) AREAS = 0.
RETURN
END
SUBROUTINE BDYADD (KK,I1,I2, LIST,LPTR,LEND,LNEW )
INTEGER KK, I1, I2, LIST(*), LPTR(*), LEND(*), LNEW
C
C***********************************************************
C
C From STRIPACK
C Robert J. Renka
C Dept. of Computer Science
C Univ. of North Texas
C renka@cs.unt.edu
C 07/11/96
C
C This subroutine adds a boundary node to a triangulation
C of a set of KK-1 points on the unit sphere. The data
C structure is updated with the insertion of node KK, but no
C optimization is performed.
C
C This routine is identical to the similarly named routine
C in TRIPACK.
C
C
C On input:
C
C KK = Index of a node to be connected to the sequence
C of all visible boundary nodes. KK .GE. 1 and
C KK must not be equal to I1 or I2.
C
C I1 = First (rightmost as viewed from KK) boundary
C node in the triangulation that is visible from
C node KK (the line segment KK-I1 intersects no
C arcs.
C
C I2 = Last (leftmost) boundary node that is visible
C from node KK. I1 and I2 may be determined by
C Subroutine TRFIND.
C
C The above parameters are not altered by this routine.
C
C LIST,LPTR,LEND,LNEW = Triangulation data structure
C created by Subroutine TRMESH.
C Nodes I1 and I2 must be in-
C cluded in the triangulation.
C
C On output:
C
C LIST,LPTR,LEND,LNEW = Data structure updated with
C the addition of node KK. Node
C KK is connected to I1, I2, and
C all boundary nodes in between.
C
C Module required by BDYADD: INSERT
C
C***********************************************************
C
INTEGER K, LP, LSAV, N1, N2, NEXT, NSAV
C
C Local parameters:
C
C K = Local copy of KK
C LP = LIST pointer
C LSAV = LIST pointer
C N1,N2 = Local copies of I1 and I2, respectively
C NEXT = Boundary node visible from K
C NSAV = Boundary node visible from K
C
K = KK
N1 = I1
N2 = I2
C
C Add K as the last neighbor of N1.
C
LP = LEND(N1)
LSAV = LPTR(LP)
LPTR(LP) = LNEW
LIST(LNEW) = -K
LPTR(LNEW) = LSAV
LEND(N1) = LNEW
LNEW = LNEW + 1
NEXT = -LIST(LP)
LIST(LP) = NEXT
NSAV = NEXT
C
C Loop on the remaining boundary nodes between N1 and N2,
C adding K as the first neighbor.
C
1 LP = LEND(NEXT)
CALL INSERT (K,LP, LIST,LPTR,LNEW )
IF (NEXT .EQ. N2) GO TO 2
NEXT = -LIST(LP)
LIST(LP) = NEXT
GO TO 1
C
C Add the boundary nodes between N1 and N2 as neighbors
C of node K.
C
2 LSAV = LNEW
LIST(LNEW) = N1
LPTR(LNEW) = LNEW + 1
LNEW = LNEW + 1
NEXT = NSAV
C
3 IF (NEXT .EQ. N2) GO TO 4
LIST(LNEW) = NEXT
LPTR(LNEW) = LNEW + 1
LNEW = LNEW + 1
LP = LEND(NEXT)
NEXT = LIST(LP)
GO TO 3
C
4 LIST(LNEW) = -N2
LPTR(LNEW) = LSAV
LEND(K) = LNEW
LNEW = LNEW + 1
RETURN
END
SUBROUTINE BNODES (N,LIST,LPTR,LEND, NODES,NB,NA,NT)
INTEGER N, LIST(*), LPTR(*), LEND(N), NODES(*), NB,
. NA, NT
C
C***********************************************************
C
C From STRIPACK
C Robert J. Renka
C Dept. of Computer Science
C Univ. of North Texas
C renka@cs.unt.edu
C 06/26/96
C
C Given a triangulation of N nodes on the unit sphere
C created by Subroutine TRMESH, this subroutine returns an
C array containing the indexes (if any) of the counterclock-
C wise-ordered sequence of boundary nodes -- the nodes on
C the boundary of the convex hull of the set of nodes. (The
C boundary is empty if the nodes do not lie in a single
C hemisphere.) The numbers of boundary nodes, arcs, and
C triangles are also returned.
C
C
C On input:
C
C N = Number of nodes in the triangulation. N .GE. 3.
C
C LIST,LPTR,LEND = Data structure defining the trian-
C gulation. Refer to Subroutine
C TRMESH.
C
C The above parameters are not altered by this routine.
C
C NODES = Integer array of length at least NB
C (NB .LE. N).
C
C On output:
C
C NODES = Ordered sequence of boundary node indexes
C in the range 1 to N (in the first NB loca-
C tions).
C
C NB = Number of boundary nodes.
C
C NA,NT = Number of arcs and triangles, respectively,
C in the triangulation.
C
C Modules required by BNODES: None
C
C***********************************************************
C
INTEGER K, LP, N0, NN, NST
C
C Local parameters:
C
C K = NODES index
C LP = LIST pointer
C N0 = Boundary node to be added to NODES
C NN = Local copy of N
C NST = First element of nodes (arbitrarily chosen to be
C the one with smallest index)
C
NN = N
C
C Search for a boundary node.
C
DO 1 NST = 1,NN
LP = LEND(NST)
IF (LIST(LP) .LT. 0) GO TO 2
1 CONTINUE
C
C The triangulation contains no boundary nodes.
C
NB = 0
NA = 3*(NN-2)
NT = 2*(NN-2)
RETURN
C
C NST is the first boundary node encountered. Initialize
C for traversal of the boundary.
C
2 NODES(1) = NST
K = 1
N0 = NST
C
C Traverse the boundary in counterclockwise order.
C
3 LP = LEND(N0)
LP = LPTR(LP)
N0 = LIST(LP)
IF (N0 .EQ. NST) GO TO 4
K = K + 1
NODES(K) = N0
GO TO 3
C
C Store the counts.
C
4 NB = K
NT = 2*N - NB - 2
NA = NT + N - 1
RETURN
END
SUBROUTINE CIRCUM (V1,V2,V3, C,IER)
INTEGER IER
REAL(kind=8) V1(3), V2(3), V3(3), C(3)
C
C***********************************************************
C
C From STRIPACK
C Robert J. Renka
C Dept. of Computer Science
C Univ. of North Texas
C renka@cs.unt.edu
C 06/29/95
C
C This subroutine returns the circumcenter of a spherical
C triangle on the unit sphere: the point on the sphere sur-
C face that is equally distant from the three triangle
C vertices and lies in the same hemisphere, where distance
C is taken to be arc-length on the sphere surface.
C
C
C On input:
C
C V1,V2,V3 = Arrays of length 3 containing the Carte-
C sian coordinates of the three triangle
C vertices (unit vectors) in CCW order.
C
C The above parameters are not altered by this routine.
C
C C = Array of length 3.
C
C On output:
C
C C = Cartesian coordinates of the circumcenter unless
C IER > 0, in which case C is not defined. C =
C (V2-V1) X (V3-V1) normalized to a unit vector.
C
C IER = Error indicator:
C IER = 0 if no errors were encountered.
C IER = 1 if V1, V2, and V3 lie on a common
C line: (V2-V1) X (V3-V1) = 0.
C (The vertices are not tested for validity.)
C
C Modules required by CIRCUM: None
C
C Intrinsic function called by CIRCUM: SQRT
C
C***********************************************************
C
INTEGER I
REAL(kind=8) CNORM, CU(3), E1(3), E2(3)
C
C Local parameters:
C
C CNORM = Norm of CU: used to compute C
C CU = Scalar multiple of C: E1 X E2
C E1,E2 = Edges of the underlying planar triangle:
C V2-V1 and V3-V1, respectively
C I = DO-loop index
C
DO 1 I = 1,3
E1(I) = V2(I) - V1(I)
E2(I) = V3(I) - V1(I)
1 CONTINUE
C
C Compute CU = E1 X E2 and CNORM**2.
C
CU(1) = E1(2)*E2(3) - E1(3)*E2(2)
CU(2) = E1(3)*E2(1) - E1(1)*E2(3)
CU(3) = E1(1)*E2(2) - E1(2)*E2(1)
CNORM = CU(1)*CU(1) + CU(2)*CU(2) + CU(3)*CU(3)
C
C The vertices lie on a common line if and only if CU is
C the zero vector.
C
IF (CNORM .NE. 0.) THEN
C
C No error: compute C.
C
CNORM = SQRT(CNORM)
DO 2 I = 1,3
C(I) = CU(I)/CNORM
2 CONTINUE
IER = 0
ELSE
C
C CU = 0.
C
IER = 1
ENDIF
RETURN
END
SUBROUTINE COVSPH (KK,N0, LIST,LPTR,LEND,LNEW )
INTEGER KK, N0, LIST(*), LPTR(*), LEND(*), LNEW
C
C***********************************************************
C
C From STRIPACK
C Robert J. Renka
C Dept. of Computer Science
C Univ. of North Texas
C renka@cs.unt.edu
C 07/17/96
C
C This subroutine connects an exterior node KK to all
C boundary nodes of a triangulation of KK-1 points on the
C unit sphere, producing a triangulation that covers the
C sphere. The data structure is updated with the addition
C of node KK, but no optimization is performed. All boun-
C dary nodes must be visible from node KK.
C
C
C On input:
C
C KK = Index of the node to be connected to the set of
C all boundary nodes. KK .GE. 4.
C
C N0 = Index of a boundary node (in the range 1 to
C KK-1). N0 may be determined by Subroutine
C TRFIND.
C
C The above parameters are not altered by this routine.
C
C LIST,LPTR,LEND,LNEW = Triangulation data structure
C created by Subroutine TRMESH.
C Node N0 must be included in
C the triangulation.
C
C On output:
C
C LIST,LPTR,LEND,LNEW = Data structure updated with
C the addition of node KK as the
C last entry. The updated
C triangulation contains no
C boundary nodes.
C
C Module required by COVSPH: INSERT
C
C***********************************************************
C
INTEGER K, LP, LSAV, NEXT, NST
C
C Local parameters:
C
C K = Local copy of KK
C LP = LIST pointer
C LSAV = LIST pointer
C NEXT = Boundary node visible from K
C NST = Local copy of N0
C
K = KK
NST = N0
C
C Traverse the boundary in clockwise order, inserting K as
C the first neighbor of each boundary node, and converting
C the boundary node to an interior node.
C
NEXT = NST
1 LP = LEND(NEXT)
CALL INSERT (K,LP, LIST,LPTR,LNEW )
NEXT = -LIST(LP)
LIST(LP) = NEXT
IF (NEXT .NE. NST) GO TO 1
C
C Traverse the boundary again, adding each node to K's
C adjacency list.
C
LSAV = LNEW
2 LP = LEND(NEXT)
LIST(LNEW) = NEXT
LPTR(LNEW) = LNEW + 1
LNEW = LNEW + 1
NEXT = LIST(LP)
IF (NEXT .NE. NST) GO TO 2
C
LPTR(LNEW-1) = LSAV
LEND(K) = LNEW - 1
RETURN
END
SUBROUTINE CRLIST (N,NCOL,X,Y,Z,LIST,LEND, LPTR,LNEW,
. LTRI, LISTC,NB,XC,YC,ZC,RC,IER)
INTEGER N, NCOL, LIST(*), LEND(N), LPTR(*), LNEW,
. LTRI(6,NCOL), LISTC(*), NB, IER
REAL(kind=8) X(N), Y(N), Z(N), XC(*), YC(*), ZC(*), RC(*)
C
C***********************************************************
C
C From STRIPACK
C Robert J. Renka
C Dept. of Computer Science
C Univ. of North Texas
C renka@cs.unt.edu
C 07/05/98
C
C Given a Delaunay triangulation of nodes on the surface
C of the unit sphere, this subroutine returns the set of
C triangle circumcenters corresponding to Voronoi vertices,
C along with the circumradii and a list of triangle indexes
C LISTC stored in one-to-one correspondence with LIST/LPTR
C entries.
C
C A triangle circumcenter is the point (unit vector) lying
C at the same angular distance from the three vertices and
C contained in the same hemisphere as the vertices. (Note
C that the negative of a circumcenter is also equidistant
C from the vertices.) If the triangulation covers the sur-
C face, the Voronoi vertices are the circumcenters of the
C triangles in the Delaunay triangulation. LPTR, LEND, and
C LNEW are not altered in this case.
C
C On the other hand, if the nodes are contained in a sin-
C gle hemisphere, the triangulation is implicitly extended
C to the entire surface by adding pseudo-arcs (of length
C greater than 180 degrees) between boundary nodes forming
C pseudo-triangles whose 'circumcenters' are included in the
C list. This extension to the triangulation actually con-
C sists of a triangulation of the set of boundary nodes in
C which the swap test is reversed (a non-empty circumcircle
C test). The negative circumcenters are stored as the
C pseudo-triangle 'circumcenters'. LISTC, LPTR, LEND, and
C LNEW contain a data structure corresponding to the ex-
C tended triangulation (Voronoi diagram), but LIST is not
C altered in this case. Thus, if it is necessary to retain
C the original (unextended) triangulation data structure,
C copies of LPTR and LNEW must be saved before calling this
C routine.
C
C
C On input:
C
C N = Number of nodes in the triangulation. N .GE. 3.
C Note that, if N = 3, there are only two Voronoi
C vertices separated by 180 degrees, and the
C Voronoi regions are not well defined.
C
C NCOL = Number of columns reserved for LTRI. This
C must be at least NB-2, where NB is the number
C of boundary nodes.
C
C X,Y,Z = Arrays of length N containing the Cartesian
C coordinates of the nodes (unit vectors).
C
C LIST = Integer array containing the set of adjacency
C lists. Refer to Subroutine TRMESH.
C
C LEND = Set of pointers to ends of adjacency lists.
C Refer to Subroutine TRMESH.
C
C The above parameters are not altered by this routine.
C
C LPTR = Array of pointers associated with LIST. Re-
C fer to Subroutine TRMESH.
C
C LNEW = Pointer to the first empty location in LIST
C and LPTR (list length plus one).
C
C LTRI = Integer work space array dimensioned 6 by
C NCOL, or unused dummy parameter if NB = 0.
C
C LISTC = Integer array of length at least 3*NT, where
C NT = 2*N-4 is the number of triangles in the
C triangulation (after extending it to cover
C the entire surface if necessary).
C
C XC,YC,ZC,RC = Arrays of length NT = 2*N-4.
C
C On output:
C
C LPTR = Array of pointers associated with LISTC:
C updated for the addition of pseudo-triangles
C if the original triangulation contains
C boundary nodes (NB > 0).
C
C LNEW = Pointer to the first empty location in LISTC
C and LPTR (list length plus one). LNEW is not
C altered if NB = 0.
C
C LTRI = Triangle list whose first NB-2 columns con-
C tain the indexes of a clockwise-ordered
C sequence of vertices (first three rows)
C followed by the LTRI column indexes of the
C triangles opposite the vertices (or 0
C denoting the exterior region) in the last
C three rows. This array is not generally of
C any use.
C
C LISTC = Array containing triangle indexes (indexes
C to XC, YC, ZC, and RC) stored in 1-1 corres-
C pondence with LIST/LPTR entries (or entries
C that would be stored in LIST for the
C extended triangulation): the index of tri-
C angle (N1,N2,N3) is stored in LISTC(K),
C LISTC(L), and LISTC(M), where LIST(K),
C LIST(L), and LIST(M) are the indexes of N2
C as a neighbor of N1, N3 as a neighbor of N2,
C and N1 as a neighbor of N3. The Voronoi
C region associated with a node is defined by
C the CCW-ordered sequence of circumcenters in
C one-to-one correspondence with its adjacency
C list (in the extended triangulation).
C
C NB = Number of boundary nodes unless IER = 1.
C
C XC,YC,ZC = Arrays containing the Cartesian coordi-
C nates of the triangle circumcenters
C (Voronoi vertices). XC(I)**2 + YC(I)**2
C + ZC(I)**2 = 1. The first NB-2 entries
C correspond to pseudo-triangles if NB > 0.
C
C RC = Array containing circumradii (the arc lengths
C or angles between the circumcenters and associ-
C ated triangle vertices) in 1-1 correspondence
C with circumcenters.
C
C IER = Error indicator:
C IER = 0 if no errors were encountered.
C IER = 1 if N < 3.
C IER = 2 if NCOL < NB-2.
C IER = 3 if a triangle is degenerate (has ver-
C tices lying on a common geodesic).
C
C Modules required by CRLIST: CIRCUM, LSTPTR, SWPTST
C
C Intrinsic functions called by CRLIST: ABS, ACOS
C
C***********************************************************
C
INTEGER LSTPTR
INTEGER I1, I2, I3, I4, IERR, KT, KT1, KT2, KT11,
. KT12, KT21, KT22, LP, LPL, LPN, N0, N1, N2,
. N3, N4, NM2, NN, NT
LOGICAL SWPTST
LOGICAL SWP
REAL(kind=8) C(3), T, V1(3), V2(3), V3(3)
C
C Local parameters:
C
C C = Circumcenter returned by Subroutine CIRCUM
C I1,I2,I3 = Permutation of (1,2,3): LTRI row indexes
C I4 = LTRI row index in the range 1 to 3
C IERR = Error flag for calls to CIRCUM
C KT = Triangle index
C KT1,KT2 = Indexes of a pair of adjacent pseudo-triangles
C KT11,KT12 = Indexes of the pseudo-triangles opposite N1
C and N2 as vertices of KT1
C KT21,KT22 = Indexes of the pseudo-triangles opposite N1
C and N2 as vertices of KT2
C LP,LPN = LIST pointers
C LPL = LIST pointer of the last neighbor of N1
C N0 = Index of the first boundary node (initial
C value of N1) in the loop on boundary nodes
C used to store the pseudo-triangle indexes
C in LISTC
C N1,N2,N3 = Nodal indexes defining a triangle (CCW order)
C or pseudo-triangle (clockwise order)
C N4 = Index of the node opposite N2 -> N1
C NM2 = N-2
C NN = Local copy of N
C NT = Number of pseudo-triangles: NB-2
C SWP = Logical variable set to TRUE in each optimiza-
C tion loop (loop on pseudo-arcs) iff a swap
C is performed
C V1,V2,V3 = Vertices of triangle KT = (N1,N2,N3) sent to
C Subroutine CIRCUM
C
NN = N
NB = 0
NT = 0
IF (NN .LT. 3) GO TO 21
C
C Search for a boundary node N1.
C
DO 1 N1 = 1,NN
LP = LEND(N1)
IF (LIST(LP) .LT. 0) GO TO 2
1 CONTINUE
C
C The triangulation already covers the sphere.
C
GO TO 9
C
C There are NB .GE. 3 boundary nodes. Add NB-2 pseudo-
C triangles (N1,N2,N3) by connecting N3 to the NB-3
C boundary nodes to which it is not already adjacent.
C
C Set N3 and N2 to the first and last neighbors,
C respectively, of N1.
C
2 N2 = -LIST(LP)
LP = LPTR(LP)
N3 = LIST(LP)
C
C Loop on boundary arcs N1 -> N2 in clockwise order,
C storing triangles (N1,N2,N3) in column NT of LTRI
C along with the indexes of the triangles opposite
C the vertices.
C
3 NT = NT + 1
IF (NT .LE. NCOL) THEN
LTRI(1,NT) = N1
LTRI(2,NT) = N2
LTRI(3,NT) = N3
LTRI(4,NT) = NT + 1
LTRI(5,NT) = NT - 1
LTRI(6,NT) = 0
ENDIF
N1 = N2
LP = LEND(N1)
N2 = -LIST(LP)
IF (N2 .NE. N3) GO TO 3
C
NB = NT + 2
IF (NCOL .LT. NT) GO TO 22
LTRI(4,NT) = 0
IF (NT .EQ. 1) GO TO 7
C
C Optimize the exterior triangulation (set of pseudo-
C triangles) by applying swaps to the pseudo-arcs N1-N2
C (pairs of adjacent pseudo-triangles KT1 and KT2 > KT1).
C The loop on pseudo-arcs is repeated until no swaps are
C performed.
C
4 SWP = .FALSE.
DO 6 KT1 = 1,NT-1
DO 5 I3 = 1,3
KT2 = LTRI(I3+3,KT1)
IF (KT2 .LE. KT1) GO TO 5
C
C The LTRI row indexes (I1,I2,I3) of triangle KT1 =
C (N1,N2,N3) are a cyclical permutation of (1,2,3).
C
IF (I3 .EQ. 1) THEN
I1 = 2
I2 = 3
ELSEIF (I3 .EQ. 2) THEN
I1 = 3
I2 = 1
ELSE
I1 = 1
I2 = 2