]> git.mxchange.org Git - flightgear.git/blob - Triangle/triangle.c
Define -DANSI_DECLARATORS so that library get's built with ANSI function
[flightgear.git] / Triangle / triangle.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*      888888888        ,o,                          / 888                  */
4 /*         888    88o88o  "    o8888o  88o8888o o88888o 888  o88888o         */
5 /*         888    888    888       88b 888  888 888 888 888 d888  88b        */
6 /*         888    888    888  o88^o888 888  888 "88888" 888 8888oo888        */
7 /*         888    888    888 C888  888 888  888  /      888 q888             */
8 /*         888    888    888  "88o^888 888  888 Cb      888  "88oooo"        */
9 /*                                              "8oo8D                       */
10 /*                                                                           */
11 /*  A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.      */
12 /*  (triangle.c)                                                             */
13 /*                                                                           */
14 /*  Version 1.3                                                              */
15 /*  July 19, 1996                                                            */
16 /*                                                                           */
17 /*  Copyright 1996                                                           */
18 /*  Jonathan Richard Shewchuk                                                */
19 /*  School of Computer Science                                               */
20 /*  Carnegie Mellon University                                               */
21 /*  5000 Forbes Avenue                                                       */
22 /*  Pittsburgh, Pennsylvania  15213-3891                                     */
23 /*  jrs@cs.cmu.edu                                                           */
24 /*                                                                           */
25 /*  This program may be freely redistributed under the condition that the    */
26 /*    copyright notices (including this entire header and the copyright      */
27 /*    notice printed when the `-h' switch is selected) are not removed, and  */
28 /*    no compensation is received.  Private, research, and institutional     */
29 /*    use is free.  You may distribute modified versions of this code UNDER  */
30 /*    THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE   */
31 /*    SAME FILE REMAIN UNDER COPYRIGHT OF THE ORIGINAL AUTHOR, BOTH SOURCE   */
32 /*    AND OBJECT CODE ARE MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR    */
33 /*    NOTICE IS GIVEN OF THE MODIFICATIONS.  Distribution of this code as    */
34 /*    part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT  */
35 /*    WITH THE AUTHOR.  (If you are not directly supplying this code to a    */
36 /*    customer, and you are instead telling them how they can obtain it for  */
37 /*    free, then you are not required to make any arrangement with me.)      */
38 /*                                                                           */
39 /*  Hypertext instructions for Triangle are available on the Web at          */
40 /*                                                                           */
41 /*      http://www.cs.cmu.edu/~quake/triangle.html                           */
42 /*                                                                           */
43 /*  Some of the references listed below are marked [*].  These are available */
44 /*    for downloading from the Web page                                      */
45 /*                                                                           */
46 /*      http://www.cs.cmu.edu/~quake/triangle.research.html                  */
47 /*                                                                           */
48 /*  A paper discussing some aspects of Triangle is available.  See Jonathan  */
49 /*    Richard Shewchuk, "Triangle:  Engineering a 2D Quality Mesh Generator  */
50 /*    and Delaunay Triangulator," First Workshop on Applied Computational    */
51 /*    Geometry, ACM, May 1996.  [*]                                          */
52 /*                                                                           */
53 /*  Triangle was created as part of the Archimedes project in the School of  */
54 /*    Computer Science at Carnegie Mellon University.  Archimedes is a       */
55 /*    system for compiling parallel finite element solvers.  For further     */
56 /*    information, see Anja Feldmann, Omar Ghattas, John R. Gilbert, Gary L. */
57 /*    Miller, David R. O'Hallaron, Eric J. Schwabe, Jonathan R. Shewchuk,    */
58 /*    and Shang-Hua Teng, "Automated Parallel Solution of Unstructured PDE   */
59 /*    Problems."  To appear in Communications of the ACM, we hope.           */
60 /*                                                                           */
61 /*  The quality mesh generation algorithm is due to Jim Ruppert, "A          */
62 /*    Delaunay Refinement Algorithm for Quality 2-Dimensional Mesh           */
63 /*    Generation," Journal of Algorithms 18(3):548-585, May 1995.  [*]       */
64 /*                                                                           */
65 /*  My implementation of the divide-and-conquer and incremental Delaunay     */
66 /*    triangulation algorithms follows closely the presentation of Guibas    */
67 /*    and Stolfi, even though I use a triangle-based data structure instead  */
68 /*    of their quad-edge data structure.  (In fact, I originally implemented */
69 /*    Triangle using the quad-edge data structure, but switching to a        */
70 /*    triangle-based data structure sped Triangle by a factor of two.)  The  */
71 /*    mesh manipulation primitives and the two aforementioned Delaunay       */
72 /*    triangulation algorithms are described by Leonidas J. Guibas and Jorge */
73 /*    Stolfi, "Primitives for the Manipulation of General Subdivisions and   */
74 /*    the Computation of Voronoi Diagrams," ACM Transactions on Graphics     */
75 /*    4(2):74-123, April 1985.                                               */
76 /*                                                                           */
77 /*  Their O(n log n) divide-and-conquer algorithm is adapted from Der-Tsai   */
78 /*    Lee and Bruce J. Schachter, "Two Algorithms for Constructing the       */
79 /*    Delaunay Triangulation," International Journal of Computer and         */
80 /*    Information Science 9(3):219-242, 1980.  The idea to improve the       */
81 /*    divide-and-conquer algorithm by alternating between vertical and       */
82 /*    horizontal cuts was introduced by Rex A. Dwyer, "A Faster Divide-and-  */
83 /*    Conquer Algorithm for Constructing Delaunay Triangulations,"           */
84 /*    Algorithmica 2(2):137-151, 1987.                                       */
85 /*                                                                           */
86 /*  The incremental insertion algorithm was first proposed by C. L. Lawson,  */
87 /*    "Software for C1 Surface Interpolation," in Mathematical Software III, */
88 /*    John R. Rice, editor, Academic Press, New York, pp. 161-194, 1977.     */
89 /*    For point location, I use the algorithm of Ernst P. Mucke, Isaac       */
90 /*    Saias, and Binhai Zhu, "Fast Randomized Point Location Without         */
91 /*    Preprocessing in Two- and Three-dimensional Delaunay Triangulations,"  */
92 /*    Proceedings of the Twelfth Annual Symposium on Computational Geometry, */
93 /*    ACM, May 1996.  [*]  If I were to randomize the order of point         */
94 /*    insertion (I currently don't bother), their result combined with the   */
95 /*    result of Leonidas J. Guibas, Donald E. Knuth, and Micha Sharir,       */
96 /*    "Randomized Incremental Construction of Delaunay and Voronoi           */
97 /*    Diagrams," Algorithmica 7(4):381-413, 1992, would yield an expected    */
98 /*    O(n^{4/3}) bound on running time.                                      */
99 /*                                                                           */
100 /*  The O(n log n) sweepline Delaunay triangulation algorithm is taken from  */
101 /*    Steven Fortune, "A Sweepline Algorithm for Voronoi Diagrams",          */
102 /*    Algorithmica 2(2):153-174, 1987.  A random sample of edges on the      */
103 /*    boundary of the triangulation are maintained in a splay tree for the   */
104 /*    purpose of point location.  Splay trees are described by Daniel        */
105 /*    Dominic Sleator and Robert Endre Tarjan, "Self-Adjusting Binary Search */
106 /*    Trees," Journal of the ACM 32(3):652-686, July 1985.                   */
107 /*                                                                           */
108 /*  The algorithms for exact computation of the signs of determinants are    */
109 /*    described in Jonathan Richard Shewchuk, "Adaptive Precision Floating-  */
110 /*    Point Arithmetic and Fast Robust Geometric Predicates," Technical      */
111 /*    Report CMU-CS-96-140, School of Computer Science, Carnegie Mellon      */
112 /*    University, Pittsburgh, Pennsylvania, May 1996.  [*]  (Submitted to    */
113 /*    Discrete & Computational Geometry.)  An abbreviated version appears as */
114 /*    Jonathan Richard Shewchuk, "Robust Adaptive Floating-Point Geometric   */
115 /*    Predicates," Proceedings of the Twelfth Annual Symposium on Computa-   */
116 /*    tional Geometry, ACM, May 1996.  [*]  Many of the ideas for my exact   */
117 /*    arithmetic routines originate with Douglas M. Priest, "Algorithms for  */
118 /*    Arbitrary Precision Floating Point Arithmetic," Tenth Symposium on     */
119 /*    Computer Arithmetic, 132-143, IEEE Computer Society Press, 1991.  [*]  */
120 /*    Many of the ideas for the correct evaluation of the signs of           */
121 /*    determinants are taken from Steven Fortune and Christopher J. Van Wyk, */
122 /*    "Efficient Exact Arithmetic for Computational Geometry," Proceedings   */
123 /*    of the Ninth Annual Symposium on Computational Geometry, ACM,          */
124 /*    pp. 163-172, May 1993, and from Steven Fortune, "Numerical Stability   */
125 /*    of Algorithms for 2D Delaunay Triangulations," International Journal   */
126 /*    of Computational Geometry & Applications 5(1-2):193-213, March-June    */
127 /*    1995.                                                                  */
128 /*                                                                           */
129 /*  For definitions of and results involving Delaunay triangulations,        */
130 /*    constrained and conforming versions thereof, and other aspects of      */
131 /*    triangular mesh generation, see the excellent survey by Marshall Bern  */
132 /*    and David Eppstein, "Mesh Generation and Optimal Triangulation," in    */
133 /*    Computing and Euclidean Geometry, Ding-Zhu Du and Frank Hwang,         */
134 /*    editors, World Scientific, Singapore, pp. 23-90, 1992.                 */
135 /*                                                                           */
136 /*  The time for incrementally adding PSLG (planar straight line graph)      */
137 /*    segments to create a constrained Delaunay triangulation is probably    */
138 /*    O(n^2) per segment in the worst case and O(n) per edge in the common   */
139 /*    case, where n is the number of triangles that intersect the segment    */
140 /*    before it is inserted.  This doesn't count point location, which can   */
141 /*    be much more expensive.  (This note does not apply to conforming       */
142 /*    Delaunay triangulations, for which a different method is used to       */
143 /*    insert segments.)                                                      */
144 /*                                                                           */
145 /*  The time for adding segments to a conforming Delaunay triangulation is   */
146 /*    not clear, but does not depend upon n alone.  In some cases, very      */
147 /*    small features (like a point lying next to a segment) can cause a      */
148 /*    single segment to be split an arbitrary number of times.  Of course,   */
149 /*    floating-point precision is a practical barrier to how much this can   */
150 /*    happen.                                                                */
151 /*                                                                           */
152 /*  The time for deleting a point from a Delaunay triangulation is O(n^2) in */
153 /*    the worst case and O(n) in the common case, where n is the degree of   */
154 /*    the point being deleted.  I could improve this to expected O(n) time   */
155 /*    by "inserting" the neighboring vertices in random order, but n is      */
156 /*    usually quite small, so it's not worth the bother.  (The O(n) time     */
157 /*    for random insertion follows from L. Paul Chew, "Building Voronoi      */
158 /*    Diagrams for Convex Polygons in Linear Expected Time," Technical       */
159 /*    Report PCS-TR90-147, Department of Mathematics and Computer Science,   */
160 /*    Dartmouth College, 1990.                                               */
161 /*                                                                           */
162 /*  Ruppert's Delaunay refinement algorithm typically generates triangles    */
163 /*    at a linear rate (constant time per triangle) after the initial        */
164 /*    triangulation is formed.  There may be pathological cases where more   */
165 /*    time is required, but these never arise in practice.                   */
166 /*                                                                           */
167 /*  The segment intersection formulae are straightforward.  If you want to   */
168 /*    see them derived, see Franklin Antonio.  "Faster Line Segment          */
169 /*    Intersection."  In Graphics Gems III (David Kirk, editor), pp. 199-    */
170 /*    202.  Academic Press, Boston, 1992.                                    */
171 /*                                                                           */
172 /*  If you make any improvements to this code, please please please let me   */
173 /*    know, so that I may obtain the improvements.  Even if you don't change */
174 /*    the code, I'd still love to hear what it's being used for.             */
175 /*                                                                           */
176 /*  Disclaimer:  Neither I nor Carnegie Mellon warrant this code in any way  */
177 /*    whatsoever.  This code is provided "as-is".  Use at your own risk.     */
178 /*                                                                           */
179 /*****************************************************************************/
180
181 /* For single precision (which will save some memory and reduce paging),     */
182 /*   define the symbol SINGLE by using the -DSINGLE compiler switch or by    */
183 /*   writing "#define SINGLE" below.                                         */
184 /*                                                                           */
185 /* For double precision (which will allow you to refine meshes to a smaller  */
186 /*   edge length), leave SINGLE undefined.                                   */
187 /*                                                                           */
188 /* Double precision uses more memory, but improves the resolution of the     */
189 /*   meshes you can generate with Triangle.  It also reduces the likelihood  */
190 /*   of a floating exception due to overflow.  Finally, it is much faster    */
191 /*   than single precision on 64-bit architectures like the DEC Alpha.  I    */
192 /*   recommend double precision unless you want to generate a mesh for which */
193 /*   you do not have enough memory.                                          */
194
195 /* #define SINGLE */
196
197 #ifdef SINGLE
198 #define REAL float
199 #else /* not SINGLE */
200 #define REAL double
201 #endif /* not SINGLE */
202
203 /* If yours is not a Unix system, define the NO_TIMER compiler switch to     */
204 /*   remove the Unix-specific timing code.                                   */
205
206 /* #define NO_TIMER */
207
208 /* To insert lots of self-checks for internal errors, define the SELF_CHECK  */
209 /*   symbol.  This will slow down the program significantly.  It is best to  */
210 /*   define the symbol using the -DSELF_CHECK compiler switch, but you could */
211 /*   write "#define SELF_CHECK" below.  If you are modifying this code, I    */
212 /*   recommend you turn self-checks on.                                      */
213
214 /* #define SELF_CHECK */
215
216 /* To compile Triangle as a callable object library (triangle.o), define the */
217 /*   TRILIBRARY symbol.  Read the file triangle.h for details on how to call */
218 /*   the procedure triangulate() that results.                               */
219
220 /* #define TRILIBRARY */
221
222 /* It is possible to generate a smaller version of Triangle using one or     */
223 /*   both of the following symbols.  Define the REDUCED symbol to eliminate  */
224 /*   all features that are primarily of research interest; specifically, the */
225 /*   -i, -F, -s, and -C switches.  Define the CDT_ONLY symbol to eliminate   */
226 /*   all meshing algorithms above and beyond constrained Delaunay            */
227 /*   triangulation; specifically, the -r, -q, -a, -S, and -s switches.       */
228 /*   These reductions are most likely to be useful when generating an object */
229 /*   library (triangle.o) by defining the TRILIBRARY symbol.                 */
230
231 /* #define REDUCED */
232 /* #define CDT_ONLY */
233
234 /* On some machines, the exact arithmetic routines might be defeated by the  */
235 /*   use of internal extended precision floating-point registers.  Sometimes */
236 /*   this problem can be fixed by defining certain values to be volatile,    */
237 /*   thus forcing them to be stored to memory and rounded off.  This isn't   */
238 /*   a great solution, though, as it slows Triangle down.                    */
239 /*                                                                           */
240 /* To try this out, write "#define INEXACT volatile" below.  Normally,       */
241 /*   however, INEXACT should be defined to be nothing.  ("#define INEXACT".) */
242
243 #define INEXACT /* Nothing */
244 /* #define INEXACT volatile */
245
246 /* Maximum number of characters in a file name (including the null).         */
247
248 #define FILENAMESIZE 512
249
250 /* Maximum number of characters in a line read from a file (including the    */
251 /*   null).                                                                  */
252
253 #define INPUTLINESIZE 512
254
255 /* For efficiency, a variety of data structures are allocated in bulk.  The  */
256 /*   following constants determine how many of each structure is allocated   */
257 /*   at once.                                                                */
258
259 #define TRIPERBLOCK 4092           /* Number of triangles allocated at once. */
260 #define SHELLEPERBLOCK 508       /* Number of shell edges allocated at once. */
261 #define POINTPERBLOCK 4092            /* Number of points allocated at once. */
262 #define VIRUSPERBLOCK 1020   /* Number of virus triangles allocated at once. */
263 /* Number of encroached segments allocated at once. */
264 #define BADSEGMENTPERBLOCK 252
265 /* Number of skinny triangles allocated at once. */
266 #define BADTRIPERBLOCK 4092
267 /* Number of splay tree nodes allocated at once. */
268 #define SPLAYNODEPERBLOCK 508
269
270 /* The point marker DEADPOINT is an arbitrary number chosen large enough to  */
271 /*   (hopefully) not conflict with user boundary markers.  Make sure that it */
272 /*   is small enough to fit into your machine's integer size.                */
273
274 #define DEADPOINT -1073741824
275
276 /* The next line is used to outsmart some very stupid compilers.  If your    */
277 /*   compiler is smarter, feel free to replace the "int" with "void".        */
278 /*   Not that it matters.                                                    */
279
280 #define VOID int
281
282 /* Two constants for algorithms based on random sampling.  Both constants    */
283 /*   have been chosen empirically to optimize their respective algorithms.   */
284
285 /* Used for the point location scheme of Mucke, Saias, and Zhu, to decide    */
286 /*   how large a random sample of triangles to inspect.                      */
287 #define SAMPLEFACTOR 11
288 /* Used in Fortune's sweepline Delaunay algorithm to determine what fraction */
289 /*   of boundary edges should be maintained in the splay tree for point      */
290 /*   location on the front.                                                  */
291 #define SAMPLERATE 10
292
293 /* A number that speaks for itself, every kissable digit.                    */
294
295 #define PI 3.141592653589793238462643383279502884197169399375105820974944592308
296
297 /* Another fave.                                                             */
298
299 #define SQUAREROOTTWO 1.4142135623730950488016887242096980785696718753769480732
300
301 /* And here's one for those of you who are intimidated by math.              */
302
303 #define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
304
305 #include <stdio.h>
306 #include <string.h>
307 #include <math.h>
308 #ifndef NO_TIMER
309 #include <sys/time.h>
310 #endif /* NO_TIMER */
311 #ifdef TRILIBRARY
312 #include "triangle.h"
313 #endif /* TRILIBRARY */
314
315 /* The following obscenity seems to be necessary to ensure that this program */
316 /* will port to Dec Alphas running OSF/1, because their stdio.h file commits */
317 /* the unpardonable sin of including stdlib.h.  Hence, malloc(), free(), and */
318 /* exit() may or may not already be defined at this point.  I declare these  */
319 /* functions explicitly because some non-ANSI C compilers lack stdlib.h.     */
320
321 #ifndef _STDLIB_H_
322 extern void *malloc();
323 extern void free();
324 extern void exit();
325 extern double strtod();
326 extern long strtol();
327 #endif /* _STDLIB_H_ */
328
329 /* A few forward declarations.                                               */
330
331 void poolrestart();
332 #ifndef TRILIBRARY
333 char *readline();
334 char *findfield();
335 #endif /* not TRILIBRARY */
336
337 /* Labels that signify whether a record consists primarily of pointers or of */
338 /*   floating-point words.  Used to make decisions about data alignment.     */
339
340 enum wordtype {POINTER, FLOATINGPOINT};
341
342 /* Labels that signify the result of point location.  The result of a        */
343 /*   search indicates that the point falls in the interior of a triangle, on */
344 /*   an edge, on a vertex, or outside the mesh.                              */
345
346 enum locateresult {INTRIANGLE, ONEDGE, ONVERTEX, OUTSIDE};
347
348 /* Labels that signify the result of site insertion.  The result indicates   */
349 /*   that the point was inserted with complete success, was inserted but     */
350 /*   encroaches on a segment, was not inserted because it lies on a segment, */
351 /*   or was not inserted because another point occupies the same location.   */
352
353 enum insertsiteresult {SUCCESSFULPOINT, ENCROACHINGPOINT, VIOLATINGPOINT,
354                        DUPLICATEPOINT};
355
356 /* Labels that signify the result of direction finding.  The result          */
357 /*   indicates that a segment connecting the two query points falls within   */
358 /*   the direction triangle, along the left edge of the direction triangle,  */
359 /*   or along the right edge of the direction triangle.                      */
360
361 enum finddirectionresult {WITHIN, LEFTCOLLINEAR, RIGHTCOLLINEAR};
362
363 /* Labels that signify the result of the circumcenter computation routine.   */
364 /*   The return value indicates which edge of the triangle is shortest.      */
365
366 enum circumcenterresult {OPPOSITEORG, OPPOSITEDEST, OPPOSITEAPEX};
367
368 /*****************************************************************************/
369 /*                                                                           */
370 /*  The basic mesh data structures                                           */
371 /*                                                                           */
372 /*  There are three:  points, triangles, and shell edges (abbreviated        */
373 /*  `shelle').  These three data structures, linked by pointers, comprise    */
374 /*  the mesh.  A point simply represents a point in space and its properties.*/
375 /*  A triangle is a triangle.  A shell edge is a special data structure used */
376 /*  to represent impenetrable segments in the mesh (including the outer      */
377 /*  boundary, boundaries of holes, and internal boundaries separating two    */
378 /*  triangulated regions).  Shell edges represent boundaries defined by the  */
379 /*  user that triangles may not lie across.                                  */
380 /*                                                                           */
381 /*  A triangle consists of a list of three vertices, a list of three         */
382 /*  adjoining triangles, a list of three adjoining shell edges (when shell   */
383 /*  edges are used), an arbitrary number of optional user-defined floating-  */
384 /*  point attributes, and an optional area constraint.  The latter is an     */
385 /*  upper bound on the permissible area of each triangle in a region, used   */
386 /*  for mesh refinement.                                                     */
387 /*                                                                           */
388 /*  For a triangle on a boundary of the mesh, some or all of the neighboring */
389 /*  triangles may not be present.  For a triangle in the interior of the     */
390 /*  mesh, often no neighboring shell edges are present.  Such absent         */
391 /*  triangles and shell edges are never represented by NULL pointers; they   */
392 /*  are represented by two special records:  `dummytri', the triangle that   */
393 /*  fills "outer space", and `dummysh', the omnipresent shell edge.          */
394 /*  `dummytri' and `dummysh' are used for several reasons; for instance,     */
395 /*  they can be dereferenced and their contents examined without causing the */
396 /*  memory protection exception that would occur if NULL were dereferenced.  */
397 /*                                                                           */
398 /*  However, it is important to understand that a triangle includes other    */
399 /*  information as well.  The pointers to adjoining vertices, triangles, and */
400 /*  shell edges are ordered in a way that indicates their geometric relation */
401 /*  to each other.  Furthermore, each of these pointers contains orientation */
402 /*  information.  Each pointer to an adjoining triangle indicates which face */
403 /*  of that triangle is contacted.  Similarly, each pointer to an adjoining  */
404 /*  shell edge indicates which side of that shell edge is contacted, and how */
405 /*  the shell edge is oriented relative to the triangle.                     */
406 /*                                                                           */
407 /*  Shell edges are found abutting edges of triangles; either sandwiched     */
408 /*  between two triangles, or resting against one triangle on an exterior    */
409 /*  boundary or hole boundary.                                               */
410 /*                                                                           */
411 /*  A shell edge consists of a list of two vertices, a list of two           */
412 /*  adjoining shell edges, and a list of two adjoining triangles.  One of    */
413 /*  the two adjoining triangles may not be present (though there should      */
414 /*  always be one), and neighboring shell edges might not be present.        */
415 /*  Shell edges also store a user-defined integer "boundary marker".         */
416 /*  Typically, this integer is used to indicate what sort of boundary        */
417 /*  conditions are to be applied at that location in a finite element        */
418 /*  simulation.                                                              */
419 /*                                                                           */
420 /*  Like triangles, shell edges maintain information about the relative      */
421 /*  orientation of neighboring objects.                                      */
422 /*                                                                           */
423 /*  Points are relatively simple.  A point is a list of floating point       */
424 /*  numbers, starting with the x, and y coordinates, followed by an          */
425 /*  arbitrary number of optional user-defined floating-point attributes,     */
426 /*  followed by an integer boundary marker.  During the segment insertion    */
427 /*  phase, there is also a pointer from each point to a triangle that may    */
428 /*  contain it.  Each pointer is not always correct, but when one is, it     */
429 /*  speeds up segment insertion.  These pointers are assigned values once    */
430 /*  at the beginning of the segment insertion phase, and are not used or     */
431 /*  updated at any other time.  Edge swapping during segment insertion will  */
432 /*  render some of them incorrect.  Hence, don't rely upon them for          */
433 /*  anything.  For the most part, points do not have any information about   */
434 /*  what triangles or shell edges they are linked to.                        */
435 /*                                                                           */
436 /*****************************************************************************/
437
438 /*****************************************************************************/
439 /*                                                                           */
440 /*  Handles                                                                  */
441 /*                                                                           */
442 /*  The oriented triangle (`triedge') and oriented shell edge (`edge') data  */
443 /*  structures defined below do not themselves store any part of the mesh.   */
444 /*  The mesh itself is made of `triangle's, `shelle's, and `point's.         */
445 /*                                                                           */
446 /*  Oriented triangles and oriented shell edges will usually be referred to  */
447 /*  as "handles".  A handle is essentially a pointer into the mesh; it       */
448 /*  allows you to "hold" one particular part of the mesh.  Handles are used  */
449 /*  to specify the regions in which one is traversing and modifying the mesh.*/
450 /*  A single `triangle' may be held by many handles, or none at all.  (The   */
451 /*  latter case is not a memory leak, because the triangle is still          */
452 /*  connected to other triangles in the mesh.)                               */
453 /*                                                                           */
454 /*  A `triedge' is a handle that holds a triangle.  It holds a specific side */
455 /*  of the triangle.  An `edge' is a handle that holds a shell edge.  It     */
456 /*  holds either the left or right side of the edge.                         */
457 /*                                                                           */
458 /*  Navigation about the mesh is accomplished through a set of mesh          */
459 /*  manipulation primitives, further below.  Many of these primitives take   */
460 /*  a handle and produce a new handle that holds the mesh near the first     */
461 /*  handle.  Other primitives take two handles and glue the corresponding    */
462 /*  parts of the mesh together.  The exact position of the handles is        */
463 /*  important.  For instance, when two triangles are glued together by the   */
464 /*  bond() primitive, they are glued by the sides on which the handles lie.  */
465 /*                                                                           */
466 /*  Because points have no information about which triangles they are        */
467 /*  attached to, I commonly represent a point by use of a handle whose       */
468 /*  origin is the point.  A single handle can simultaneously represent a     */
469 /*  triangle, an edge, and a point.                                          */
470 /*                                                                           */
471 /*****************************************************************************/
472
473 /* The triangle data structure.  Each triangle contains three pointers to    */
474 /*   adjoining triangles, plus three pointers to vertex points, plus three   */
475 /*   pointers to shell edges (defined below; these pointers are usually      */
476 /*   `dummysh').  It may or may not also contain user-defined attributes     */
477 /*   and/or a floating-point "area constraint".  It may also contain extra   */
478 /*   pointers for nodes, when the user asks for high-order elements.         */
479 /*   Because the size and structure of a `triangle' is not decided until     */
480 /*   runtime, I haven't simply defined the type `triangle' to be a struct.   */
481
482 typedef REAL **triangle;            /* Really:  typedef triangle *triangle   */
483
484 /* An oriented triangle:  includes a pointer to a triangle and orientation.  */
485 /*   The orientation denotes an edge of the triangle.  Hence, there are      */
486 /*   three possible orientations.  By convention, each edge is always        */
487 /*   directed to point counterclockwise about the corresponding triangle.    */
488
489 struct triedge {
490   triangle *tri;
491   int orient;                                         /* Ranges from 0 to 2. */
492 };
493
494 /* The shell data structure.  Each shell edge contains two pointers to       */
495 /*   adjoining shell edges, plus two pointers to vertex points, plus two     */
496 /*   pointers to adjoining triangles, plus one shell marker.                 */
497
498 typedef REAL **shelle;                  /* Really:  typedef shelle *shelle   */
499
500 /* An oriented shell edge:  includes a pointer to a shell edge and an        */
501 /*   orientation.  The orientation denotes a side of the edge.  Hence, there */
502 /*   are two possible orientations.  By convention, the edge is always       */
503 /*   directed so that the "side" denoted is the right side of the edge.      */
504
505 struct edge {
506   shelle *sh;
507   int shorient;                                       /* Ranges from 0 to 1. */
508 };
509
510 /* The point data structure.  Each point is actually an array of REALs.      */
511 /*   The number of REALs is unknown until runtime.  An integer boundary      */
512 /*   marker, and sometimes a pointer to a triangle, is appended after the    */
513 /*   REALs.                                                                  */
514
515 typedef REAL *point;
516
517 /* A queue used to store encroached segments.  Each segment's vertices are   */
518 /*   stored so that one can check whether a segment is still the same.       */
519
520 struct badsegment {
521   struct edge encsegment;                          /* An encroached segment. */
522   point segorg, segdest;                                /* The two vertices. */
523   struct badsegment *nextsegment;     /* Pointer to next encroached segment. */
524 };
525
526 /* A queue used to store bad triangles.  The key is the square of the cosine */
527 /*   of the smallest angle of the triangle.  Each triangle's vertices are    */
528 /*   stored so that one can check whether a triangle is still the same.      */
529
530 struct badface {
531   struct triedge badfacetri;                              /* A bad triangle. */
532   REAL key;                             /* cos^2 of smallest (apical) angle. */
533   point faceorg, facedest, faceapex;                  /* The three vertices. */
534   struct badface *nextface;                 /* Pointer to next bad triangle. */
535 };
536
537 /* A node in a heap used to store events for the sweepline Delaunay          */
538 /*   algorithm.  Nodes do not point directly to their parents or children in */
539 /*   the heap.  Instead, each node knows its position in the heap, and can   */
540 /*   look up its parent and children in a separate array.  The `eventptr'    */
541 /*   points either to a `point' or to a triangle (in encoded format, so that */
542 /*   an orientation is included).  In the latter case, the origin of the     */
543 /*   oriented triangle is the apex of a "circle event" of the sweepline      */
544 /*   algorithm.  To distinguish site events from circle events, all circle   */
545 /*   events are given an invalid (smaller than `xmin') x-coordinate `xkey'.  */
546
547 struct event {
548   REAL xkey, ykey;                              /* Coordinates of the event. */
549   VOID *eventptr;       /* Can be a point or the location of a circle event. */
550   int heapposition;              /* Marks this event's position in the heap. */
551 };
552
553 /* A node in the splay tree.  Each node holds an oriented ghost triangle     */
554 /*   that represents a boundary edge of the growing triangulation.  When a   */
555 /*   circle event covers two boundary edges with a triangle, so that they    */
556 /*   are no longer boundary edges, those edges are not immediately deleted   */
557 /*   from the tree; rather, they are lazily deleted when they are next       */
558 /*   encountered.  (Since only a random sample of boundary edges are kept    */
559 /*   in the tree, lazy deletion is faster.)  `keydest' is used to verify     */
560 /*   that a triangle is still the same as when it entered the splay tree; if */
561 /*   it has been rotated (due to a circle event), it no longer represents a  */
562 /*   boundary edge and should be deleted.                                    */
563
564 struct splaynode {
565   struct triedge keyedge;                  /* Lprev of an edge on the front. */
566   point keydest;            /* Used to verify that splay node is still live. */
567   struct splaynode *lchild, *rchild;              /* Children in splay tree. */
568 };
569
570 /* A type used to allocate memory.  firstblock is the first block of items.  */
571 /*   nowblock is the block from which items are currently being allocated.   */
572 /*   nextitem points to the next slab of free memory for an item.            */
573 /*   deaditemstack is the head of a linked list (stack) of deallocated items */
574 /*   that can be recycled.  unallocateditems is the number of items that     */
575 /*   remain to be allocated from nowblock.                                   */
576 /*                                                                           */
577 /* Traversal is the process of walking through the entire list of items, and */
578 /*   is separate from allocation.  Note that a traversal will visit items on */
579 /*   the "deaditemstack" stack as well as live items.  pathblock points to   */
580 /*   the block currently being traversed.  pathitem points to the next item  */
581 /*   to be traversed.  pathitemsleft is the number of items that remain to   */
582 /*   be traversed in pathblock.                                              */
583 /*                                                                           */
584 /* itemwordtype is set to POINTER or FLOATINGPOINT, and is used to suggest   */
585 /*   what sort of word the record is primarily made up of.  alignbytes       */
586 /*   determines how new records should be aligned in memory.  itembytes and  */
587 /*   itemwords are the length of a record in bytes (after rounding up) and   */
588 /*   words.  itemsperblock is the number of items allocated at once in a     */
589 /*   single block.  items is the number of currently allocated items.        */
590 /*   maxitems is the maximum number of items that have been allocated at     */
591 /*   once; it is the current number of items plus the number of records kept */
592 /*   on deaditemstack.                                                       */
593
594 struct memorypool {
595   VOID **firstblock, **nowblock;
596   VOID *nextitem;
597   VOID *deaditemstack;
598   VOID **pathblock;
599   VOID *pathitem;
600   enum wordtype itemwordtype;
601   int alignbytes;
602   int itembytes, itemwords;
603   int itemsperblock;
604   long items, maxitems;
605   int unallocateditems;
606   int pathitemsleft;
607 };
608
609 /* Variables used to allocate memory for triangles, shell edges, points,     */
610 /*   viri (triangles being eaten), bad (encroached) segments, bad (skinny    */
611 /*   or too large) triangles, and splay tree nodes.                          */
612
613 struct memorypool triangles;
614 struct memorypool shelles;
615 struct memorypool points;
616 struct memorypool viri;
617 struct memorypool badsegments;
618 struct memorypool badtriangles;
619 struct memorypool splaynodes;
620
621 /* Variables that maintain the bad triangle queues.  The tails are pointers  */
622 /*   to the pointers that have to be filled in to enqueue an item.           */
623
624 struct badface *queuefront[64];
625 struct badface **queuetail[64];
626
627 REAL xmin, xmax, ymin, ymax;                              /* x and y bounds. */
628 REAL xminextreme;        /* Nonexistent x value used as a flag in sweepline. */
629 int inpoints;                                     /* Number of input points. */
630 int inelements;                                /* Number of input triangles. */
631 int insegments;                                 /* Number of input segments. */
632 int holes;                                         /* Number of input holes. */
633 int regions;                                     /* Number of input regions. */
634 long edges;                                       /* Number of output edges. */
635 int mesh_dim;                                  /* Dimension (ought to be 2). */
636 int nextras;                              /* Number of attributes per point. */
637 int eextras;                           /* Number of attributes per triangle. */
638 long hullsize;                            /* Number of edges of convex hull. */
639 int triwords;                                   /* Total words per triangle. */
640 int shwords;                                  /* Total words per shell edge. */
641 int pointmarkindex;             /* Index to find boundary marker of a point. */
642 int point2triindex;         /* Index to find a triangle adjacent to a point. */
643 int highorderindex;    /* Index to find extra nodes for high-order elements. */
644 int elemattribindex;              /* Index to find attributes of a triangle. */
645 int areaboundindex;               /* Index to find area bound of a triangle. */
646 int checksegments;           /* Are there segments in the triangulation yet? */
647 int readnodefile;                             /* Has a .node file been read? */
648 long samples;                /* Number of random samples for point location. */
649 unsigned long randomseed;                     /* Current random number seed. */
650
651 REAL splitter;       /* Used to split REAL factors for exact multiplication. */
652 REAL epsilon;                             /* Floating-point machine epsilon. */
653 REAL resulterrbound;
654 REAL ccwerrboundA, ccwerrboundB, ccwerrboundC;
655 REAL iccerrboundA, iccerrboundB, iccerrboundC;
656
657 long incirclecount;                   /* Number of incircle tests performed. */
658 long counterclockcount;       /* Number of counterclockwise tests performed. */
659 long hyperbolacount;        /* Number of right-of-hyperbola tests performed. */
660 long circumcentercount;    /* Number of circumcenter calculations performed. */
661 long circletopcount;         /* Number of circle top calculations performed. */
662
663 /* Switches for the triangulator.                                            */
664 /*   poly: -p switch.  refine: -r switch.                                    */
665 /*   quality: -q switch.                                                     */
666 /*     minangle: minimum angle bound, specified after -q switch.             */
667 /*     goodangle: cosine squared of minangle.                                */
668 /*   vararea: -a switch without number.                                      */
669 /*   fixedarea: -a switch with number.                                       */
670 /*     maxarea: maximum area bound, specified after -a switch.               */
671 /*   regionattrib: -A switch.  convex: -c switch.                            */
672 /*   firstnumber: inverse of -z switch.  All items are numbered starting     */
673 /*     from firstnumber.                                                     */
674 /*   edgesout: -e switch.  voronoi: -v switch.                               */
675 /*   neighbors: -n switch.  geomview: -g switch.                             */
676 /*   nobound: -B switch.  nopolywritten: -P switch.                          */
677 /*   nonodewritten: -N switch.  noelewritten: -E switch.                     */
678 /*   noiterationnum: -I switch.  noholes: -O switch.                         */
679 /*   noexact: -X switch.                                                     */
680 /*   order: element order, specified after -o switch.                        */
681 /*   nobisect: count of how often -Y switch is selected.                     */
682 /*   steiner: maximum number of Steiner points, specified after -S switch.   */
683 /*     steinerleft: number of Steiner points not yet used.                   */
684 /*   incremental: -i switch.  sweepline: -F switch.                          */
685 /*   dwyer: inverse of -l switch.                                            */
686 /*   splitseg: -s switch.                                                    */
687 /*   docheck: -C switch.                                                     */
688 /*   quiet: -Q switch.  verbose: count of how often -V switch is selected.   */
689 /*   useshelles: -p, -r, -q, or -c switch; determines whether shell edges    */
690 /*     are used at all.                                                      */
691 /*                                                                           */
692 /* Read the instructions to find out the meaning of these switches.          */
693
694 int poly, refine, quality, vararea, fixedarea, regionattrib, convex;
695 int firstnumber;
696 int edgesout, voronoi, neighbors, geomview;
697 int nobound, nopolywritten, nonodewritten, noelewritten, noiterationnum;
698 int noholes, noexact;
699 int incremental, sweepline, dwyer;
700 int splitseg;
701 int docheck;
702 int quiet, verbose;
703 int useshelles;
704 int order;
705 int nobisect;
706 int steiner, steinerleft;
707 REAL minangle, goodangle;
708 REAL maxarea;
709
710 /* Variables for file names.                                                 */
711
712 #ifndef TRILIBRARY
713 char innodefilename[FILENAMESIZE];
714 char inelefilename[FILENAMESIZE];
715 char inpolyfilename[FILENAMESIZE];
716 char areafilename[FILENAMESIZE];
717 char outnodefilename[FILENAMESIZE];
718 char outelefilename[FILENAMESIZE];
719 char outpolyfilename[FILENAMESIZE];
720 char edgefilename[FILENAMESIZE];
721 char vnodefilename[FILENAMESIZE];
722 char vedgefilename[FILENAMESIZE];
723 char neighborfilename[FILENAMESIZE];
724 char offfilename[FILENAMESIZE];
725 #endif /* not TRILIBRARY */
726
727 /* Triangular bounding box points.                                           */
728
729 point infpoint1, infpoint2, infpoint3;
730
731 /* Pointer to the `triangle' that occupies all of "outer space".             */
732
733 triangle *dummytri;
734 triangle *dummytribase;      /* Keep base address so we can free() it later. */
735
736 /* Pointer to the omnipresent shell edge.  Referenced by any triangle or     */
737 /*   shell edge that isn't really connected to a shell edge at that          */
738 /*   location.                                                               */
739
740 shelle *dummysh;
741 shelle *dummyshbase;         /* Keep base address so we can free() it later. */
742
743 /* Pointer to a recently visited triangle.  Improves point location if       */
744 /*   proximate points are inserted sequentially.                             */
745
746 struct triedge recenttri;
747
748 /*****************************************************************************/
749 /*                                                                           */
750 /*  Mesh manipulation primitives.  Each triangle contains three pointers to  */
751 /*  other triangles, with orientations.  Each pointer points not to the      */
752 /*  first byte of a triangle, but to one of the first three bytes of a       */
753 /*  triangle.  It is necessary to extract both the triangle itself and the   */
754 /*  orientation.  To save memory, I keep both pieces of information in one   */
755 /*  pointer.  To make this possible, I assume that all triangles are aligned */
756 /*  to four-byte boundaries.  The `decode' routine below decodes a pointer,  */
757 /*  extracting an orientation (in the range 0 to 2) and a pointer to the     */
758 /*  beginning of a triangle.  The `encode' routine compresses a pointer to a */
759 /*  triangle and an orientation into a single pointer.  My assumptions that  */
760 /*  triangles are four-byte-aligned and that the `unsigned long' type is     */
761 /*  long enough to hold a pointer are two of the few kludges in this program.*/
762 /*                                                                           */
763 /*  Shell edges are manipulated similarly.  A pointer to a shell edge        */
764 /*  carries both an address and an orientation in the range 0 to 1.          */
765 /*                                                                           */
766 /*  The other primitives take an oriented triangle or oriented shell edge,   */
767 /*  and return an oriented triangle or oriented shell edge or point; or they */
768 /*  change the connections in the data structure.                            */
769 /*                                                                           */
770 /*****************************************************************************/
771
772 /********* Mesh manipulation primitives begin here                   *********/
773 /**                                                                         **/
774 /**                                                                         **/
775
776 /* Fast lookup arrays to speed some of the mesh manipulation primitives.     */
777
778 int plus1mod3[3] = {1, 2, 0};
779 int minus1mod3[3] = {2, 0, 1};
780
781 /********* Primitives for triangles                                  *********/
782 /*                                                                           */
783 /*                                                                           */
784
785 /* decode() converts a pointer to an oriented triangle.  The orientation is  */
786 /*   extracted from the two least significant bits of the pointer.           */
787
788 #define decode(ptr, triedge)                                                  \
789   (triedge).orient = (int) ((unsigned long) (ptr) & (unsigned long) 3l);      \
790   (triedge).tri = (triangle *)                                                \
791                   ((unsigned long) (ptr) ^ (unsigned long) (triedge).orient)
792
793 /* encode() compresses an oriented triangle into a single pointer.  It       */
794 /*   relies on the assumption that all triangles are aligned to four-byte    */
795 /*   boundaries, so the two least significant bits of (triedge).tri are zero.*/
796
797 #define encode(triedge)                                                       \
798   (triangle) ((unsigned long) (triedge).tri | (unsigned long) (triedge).orient)
799
800 /* The following edge manipulation primitives are all described by Guibas    */
801 /*   and Stolfi.  However, they use an edge-based data structure, whereas I  */
802 /*   am using a triangle-based data structure.                               */
803
804 /* sym() finds the abutting triangle, on the same edge.  Note that the       */
805 /*   edge direction is necessarily reversed, because triangle/edge handles   */
806 /*   are always directed counterclockwise around the triangle.               */
807
808 #define sym(triedge1, triedge2)                                               \
809   ptr = (triedge1).tri[(triedge1).orient];                                    \
810   decode(ptr, triedge2);
811
812 #define symself(triedge)                                                      \
813   ptr = (triedge).tri[(triedge).orient];                                      \
814   decode(ptr, triedge);
815
816 /* lnext() finds the next edge (counterclockwise) of a triangle.             */
817
818 #define lnext(triedge1, triedge2)                                             \
819   (triedge2).tri = (triedge1).tri;                                            \
820   (triedge2).orient = plus1mod3[(triedge1).orient]
821
822 #define lnextself(triedge)                                                    \
823   (triedge).orient = plus1mod3[(triedge).orient]
824
825 /* lprev() finds the previous edge (clockwise) of a triangle.                */
826
827 #define lprev(triedge1, triedge2)                                             \
828   (triedge2).tri = (triedge1).tri;                                            \
829   (triedge2).orient = minus1mod3[(triedge1).orient]
830
831 #define lprevself(triedge)                                                    \
832   (triedge).orient = minus1mod3[(triedge).orient]
833
834 /* onext() spins counterclockwise around a point; that is, it finds the next */
835 /*   edge with the same origin in the counterclockwise direction.  This edge */
836 /*   will be part of a different triangle.                                   */
837
838 #define onext(triedge1, triedge2)                                             \
839   lprev(triedge1, triedge2);                                                  \
840   symself(triedge2);
841
842 #define onextself(triedge)                                                    \
843   lprevself(triedge);                                                         \
844   symself(triedge);
845
846 /* oprev() spins clockwise around a point; that is, it finds the next edge   */
847 /*   with the same origin in the clockwise direction.  This edge will be     */
848 /*   part of a different triangle.                                           */
849
850 #define oprev(triedge1, triedge2)                                             \
851   sym(triedge1, triedge2);                                                    \
852   lnextself(triedge2);
853
854 #define oprevself(triedge)                                                    \
855   symself(triedge);                                                           \
856   lnextself(triedge);
857
858 /* dnext() spins counterclockwise around a point; that is, it finds the next */
859 /*   edge with the same destination in the counterclockwise direction.  This */
860 /*   edge will be part of a different triangle.                              */
861
862 #define dnext(triedge1, triedge2)                                             \
863   sym(triedge1, triedge2);                                                    \
864   lprevself(triedge2);
865
866 #define dnextself(triedge)                                                    \
867   symself(triedge);                                                           \
868   lprevself(triedge);
869
870 /* dprev() spins clockwise around a point; that is, it finds the next edge   */
871 /*   with the same destination in the clockwise direction.  This edge will   */
872 /*   be part of a different triangle.                                        */
873
874 #define dprev(triedge1, triedge2)                                             \
875   lnext(triedge1, triedge2);                                                  \
876   symself(triedge2);
877
878 #define dprevself(triedge)                                                    \
879   lnextself(triedge);                                                         \
880   symself(triedge);
881
882 /* rnext() moves one edge counterclockwise about the adjacent triangle.      */
883 /*   (It's best understood by reading Guibas and Stolfi.  It involves        */
884 /*   changing triangles twice.)                                              */
885
886 #define rnext(triedge1, triedge2)                                             \
887   sym(triedge1, triedge2);                                                    \
888   lnextself(triedge2);                                                        \
889   symself(triedge2);
890
891 #define rnextself(triedge)                                                    \
892   symself(triedge);                                                           \
893   lnextself(triedge);                                                         \
894   symself(triedge);
895
896 /* rnext() moves one edge clockwise about the adjacent triangle.             */
897 /*   (It's best understood by reading Guibas and Stolfi.  It involves        */
898 /*   changing triangles twice.)                                              */
899
900 #define rprev(triedge1, triedge2)                                             \
901   sym(triedge1, triedge2);                                                    \
902   lprevself(triedge2);                                                        \
903   symself(triedge2);
904
905 #define rprevself(triedge)                                                    \
906   symself(triedge);                                                           \
907   lprevself(triedge);                                                         \
908   symself(triedge);
909
910 /* These primitives determine or set the origin, destination, or apex of a   */
911 /* triangle.                                                                 */
912
913 #define org(triedge, pointptr)                                                \
914   pointptr = (point) (triedge).tri[plus1mod3[(triedge).orient] + 3]
915
916 #define dest(triedge, pointptr)                                               \
917   pointptr = (point) (triedge).tri[minus1mod3[(triedge).orient] + 3]
918
919 #define apex(triedge, pointptr)                                               \
920   pointptr = (point) (triedge).tri[(triedge).orient + 3]
921
922 #define setorg(triedge, pointptr)                                             \
923   (triedge).tri[plus1mod3[(triedge).orient] + 3] = (triangle) pointptr
924
925 #define setdest(triedge, pointptr)                                            \
926   (triedge).tri[minus1mod3[(triedge).orient] + 3] = (triangle) pointptr
927
928 #define setapex(triedge, pointptr)                                            \
929   (triedge).tri[(triedge).orient + 3] = (triangle) pointptr
930
931 #define setvertices2null(triedge)                                             \
932   (triedge).tri[3] = (triangle) NULL;                                         \
933   (triedge).tri[4] = (triangle) NULL;                                         \
934   (triedge).tri[5] = (triangle) NULL;
935
936 /* Bond two triangles together.                                              */
937
938 #define bond(triedge1, triedge2)                                              \
939   (triedge1).tri[(triedge1).orient] = encode(triedge2);                       \
940   (triedge2).tri[(triedge2).orient] = encode(triedge1)
941
942 /* Dissolve a bond (from one side).  Note that the other triangle will still */
943 /*   think it's connected to this triangle.  Usually, however, the other     */
944 /*   triangle is being deleted entirely, or bonded to another triangle, so   */
945 /*   it doesn't matter.                                                      */
946
947 #define dissolve(triedge)                                                     \
948   (triedge).tri[(triedge).orient] = (triangle) dummytri
949
950 /* Copy a triangle/edge handle.                                              */
951
952 #define triedgecopy(triedge1, triedge2)                                       \
953   (triedge2).tri = (triedge1).tri;                                            \
954   (triedge2).orient = (triedge1).orient
955
956 /* Test for equality of triangle/edge handles.                               */
957
958 #define triedgeequal(triedge1, triedge2)                                      \
959   (((triedge1).tri == (triedge2).tri) &&                                      \
960    ((triedge1).orient == (triedge2).orient))
961
962 /* Primitives to infect or cure a triangle with the virus.  These rely on    */
963 /*   the assumption that all shell edges are aligned to four-byte boundaries.*/
964
965 #define infect(triedge)                                                       \
966   (triedge).tri[6] = (triangle)                                               \
967                      ((unsigned long) (triedge).tri[6] | (unsigned long) 2l)
968
969 #define uninfect(triedge)                                                     \
970   (triedge).tri[6] = (triangle)                                               \
971                      ((unsigned long) (triedge).tri[6] & ~ (unsigned long) 2l)
972
973 /* Test a triangle for viral infection.                                      */
974
975 #define infected(triedge)                                                     \
976   (((unsigned long) (triedge).tri[6] & (unsigned long) 2l) != 0)
977
978 /* Check or set a triangle's attributes.                                     */
979
980 #define elemattribute(triedge, attnum)                                        \
981   ((REAL *) (triedge).tri)[elemattribindex + (attnum)]
982
983 #define setelemattribute(triedge, attnum, value)                              \
984   ((REAL *) (triedge).tri)[elemattribindex + (attnum)] = value
985
986 /* Check or set a triangle's maximum area bound.                             */
987
988 #define areabound(triedge)  ((REAL *) (triedge).tri)[areaboundindex]
989
990 #define setareabound(triedge, value)                                          \
991   ((REAL *) (triedge).tri)[areaboundindex] = value
992
993 /********* Primitives for shell edges                                *********/
994 /*                                                                           */
995 /*                                                                           */
996
997 /* sdecode() converts a pointer to an oriented shell edge.  The orientation  */
998 /*   is extracted from the least significant bit of the pointer.  The two    */
999 /*   least significant bits (one for orientation, one for viral infection)   */
1000 /*   are masked out to produce the real pointer.                             */
1001
1002 #define sdecode(sptr, edge)                                                   \
1003   (edge).shorient = (int) ((unsigned long) (sptr) & (unsigned long) 1l);      \
1004   (edge).sh = (shelle *)                                                      \
1005               ((unsigned long) (sptr) & ~ (unsigned long) 3l)
1006
1007 /* sencode() compresses an oriented shell edge into a single pointer.  It    */
1008 /*   relies on the assumption that all shell edges are aligned to two-byte   */
1009 /*   boundaries, so the least significant bit of (edge).sh is zero.          */
1010
1011 #define sencode(edge)                                                         \
1012   (shelle) ((unsigned long) (edge).sh | (unsigned long) (edge).shorient)
1013
1014 /* ssym() toggles the orientation of a shell edge.                           */
1015
1016 #define ssym(edge1, edge2)                                                    \
1017   (edge2).sh = (edge1).sh;                                                    \
1018   (edge2).shorient = 1 - (edge1).shorient
1019
1020 #define ssymself(edge)                                                        \
1021   (edge).shorient = 1 - (edge).shorient
1022
1023 /* spivot() finds the other shell edge (from the same segment) that shares   */
1024 /*   the same origin.                                                        */
1025
1026 #define spivot(edge1, edge2)                                                  \
1027   sptr = (edge1).sh[(edge1).shorient];                                        \
1028   sdecode(sptr, edge2)
1029
1030 #define spivotself(edge)                                                      \
1031   sptr = (edge).sh[(edge).shorient];                                          \
1032   sdecode(sptr, edge)
1033
1034 /* snext() finds the next shell edge (from the same segment) in sequence;    */
1035 /*   one whose origin is the input shell edge's destination.                 */
1036
1037 #define snext(edge1, edge2)                                                   \
1038   sptr = (edge1).sh[1 - (edge1).shorient];                                    \
1039   sdecode(sptr, edge2)
1040
1041 #define snextself(edge)                                                       \
1042   sptr = (edge).sh[1 - (edge).shorient];                                      \
1043   sdecode(sptr, edge)
1044
1045 /* These primitives determine or set the origin or destination of a shell    */
1046 /*   edge.                                                                   */
1047
1048 #define sorg(edge, pointptr)                                                  \
1049   pointptr = (point) (edge).sh[2 + (edge).shorient]
1050
1051 #define sdest(edge, pointptr)                                                 \
1052   pointptr = (point) (edge).sh[3 - (edge).shorient]
1053
1054 #define setsorg(edge, pointptr)                                               \
1055   (edge).sh[2 + (edge).shorient] = (shelle) pointptr
1056
1057 #define setsdest(edge, pointptr)                                              \
1058   (edge).sh[3 - (edge).shorient] = (shelle) pointptr
1059
1060 /* These primitives read or set a shell marker.  Shell markers are used to   */
1061 /*   hold user boundary information.                                         */
1062
1063 #define mark(edge)  (* (int *) ((edge).sh + 6))
1064
1065 #define setmark(edge, value)                                                  \
1066   * (int *) ((edge).sh + 6) = value
1067
1068 /* Bond two shell edges together.                                            */
1069
1070 #define sbond(edge1, edge2)                                                   \
1071   (edge1).sh[(edge1).shorient] = sencode(edge2);                              \
1072   (edge2).sh[(edge2).shorient] = sencode(edge1)
1073
1074 /* Dissolve a shell edge bond (from one side).  Note that the other shell    */
1075 /*   edge will still think it's connected to this shell edge.                */
1076
1077 #define sdissolve(edge)                                                       \
1078   (edge).sh[(edge).shorient] = (shelle) dummysh
1079
1080 /* Copy a shell edge.                                                        */
1081
1082 #define shellecopy(edge1, edge2)                                              \
1083   (edge2).sh = (edge1).sh;                                                    \
1084   (edge2).shorient = (edge1).shorient
1085
1086 /* Test for equality of shell edges.                                         */
1087
1088 #define shelleequal(edge1, edge2)                                             \
1089   (((edge1).sh == (edge2).sh) &&                                              \
1090    ((edge1).shorient == (edge2).shorient))
1091
1092 /********* Primitives for interacting triangles and shell edges      *********/
1093 /*                                                                           */
1094 /*                                                                           */
1095
1096 /* tspivot() finds a shell edge abutting a triangle.                         */
1097
1098 #define tspivot(triedge, edge)                                                \
1099   sptr = (shelle) (triedge).tri[6 + (triedge).orient];                        \
1100   sdecode(sptr, edge)
1101
1102 /* stpivot() finds a triangle abutting a shell edge.  It requires that the   */
1103 /*   variable `ptr' of type `triangle' be defined.                           */
1104
1105 #define stpivot(edge, triedge)                                                \
1106   ptr = (triangle) (edge).sh[4 + (edge).shorient];                            \
1107   decode(ptr, triedge)
1108
1109 /* Bond a triangle to a shell edge.                                          */
1110
1111 #define tsbond(triedge, edge)                                                 \
1112   (triedge).tri[6 + (triedge).orient] = (triangle) sencode(edge);             \
1113   (edge).sh[4 + (edge).shorient] = (shelle) encode(triedge)
1114
1115 /* Dissolve a bond (from the triangle side).                                 */
1116
1117 #define tsdissolve(triedge)                                                   \
1118   (triedge).tri[6 + (triedge).orient] = (triangle) dummysh
1119
1120 /* Dissolve a bond (from the shell edge side).                               */
1121
1122 #define stdissolve(edge)                                                      \
1123   (edge).sh[4 + (edge).shorient] = (shelle) dummytri
1124
1125 /********* Primitives for points                                     *********/
1126 /*                                                                           */
1127 /*                                                                           */
1128
1129 #define pointmark(pt)  ((int *) (pt))[pointmarkindex]
1130
1131 #define setpointmark(pt, value)                                               \
1132   ((int *) (pt))[pointmarkindex] = value
1133
1134 #define point2tri(pt)  ((triangle *) (pt))[point2triindex]
1135
1136 #define setpoint2tri(pt, value)                                               \
1137   ((triangle *) (pt))[point2triindex] = value
1138
1139 /**                                                                         **/
1140 /**                                                                         **/
1141 /********* Mesh manipulation primitives end here                     *********/
1142
1143 /********* User interaction routines begin here                      *********/
1144 /**                                                                         **/
1145 /**                                                                         **/
1146
1147 /*****************************************************************************/
1148 /*                                                                           */
1149 /*  syntax()   Print list of command line switches.                          */
1150 /*                                                                           */
1151 /*****************************************************************************/
1152
1153 #ifndef TRILIBRARY
1154
1155 void syntax()
1156 {
1157 #ifdef CDT_ONLY
1158 #ifdef REDUCED
1159   printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n");
1160 #else /* not REDUCED */
1161   printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n");
1162 #endif /* not REDUCED */
1163 #else /* not CDT_ONLY */
1164 #ifdef REDUCED
1165   printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n");
1166 #else /* not REDUCED */
1167   printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n");
1168 #endif /* not REDUCED */
1169 #endif /* not CDT_ONLY */
1170
1171   printf("    -p  Triangulates a Planar Straight Line Graph (.poly file).\n");
1172 #ifndef CDT_ONLY
1173   printf("    -r  Refines a previously generated mesh.\n");
1174   printf(
1175     "    -q  Quality mesh generation.  A minimum angle may be specified.\n");
1176   printf("    -a  Applies a maximum triangle area constraint.\n");
1177 #endif /* not CDT_ONLY */
1178   printf(
1179     "    -A  Applies attributes to identify elements in certain regions.\n");
1180   printf("    -c  Encloses the convex hull with segments.\n");
1181   printf("    -e  Generates an edge list.\n");
1182   printf("    -v  Generates a Voronoi diagram.\n");
1183   printf("    -n  Generates a list of triangle neighbors.\n");
1184   printf("    -g  Generates an .off file for Geomview.\n");
1185   printf("    -B  Suppresses output of boundary information.\n");
1186   printf("    -P  Suppresses output of .poly file.\n");
1187   printf("    -N  Suppresses output of .node file.\n");
1188   printf("    -E  Suppresses output of .ele file.\n");
1189   printf("    -I  Suppresses mesh iteration numbers.\n");
1190   printf("    -O  Ignores holes in .poly file.\n");
1191   printf("    -X  Suppresses use of exact arithmetic.\n");
1192   printf("    -z  Numbers all items starting from zero (rather than one).\n");
1193   printf("    -o2 Generates second-order subparametric elements.\n");
1194 #ifndef CDT_ONLY
1195   printf("    -Y  Suppresses boundary segment splitting.\n");
1196   printf("    -S  Specifies maximum number of added Steiner points.\n");
1197 #endif /* not CDT_ONLY */
1198 #ifndef REDUCED
1199   printf("    -i  Uses incremental method, rather than divide-and-conquer.\n");
1200   printf("    -F  Uses Fortune's sweepline algorithm, rather than d-and-c.\n");
1201 #endif /* not REDUCED */
1202   printf("    -l  Uses vertical cuts only, rather than alternating cuts.\n");
1203 #ifndef REDUCED
1204 #ifndef CDT_ONLY
1205   printf(
1206     "    -s  Force segments into mesh by splitting (instead of using CDT).\n");
1207 #endif /* not CDT_ONLY */
1208   printf("    -C  Check consistency of final mesh.\n");
1209 #endif /* not REDUCED */
1210   printf("    -Q  Quiet:  No terminal output except errors.\n");
1211   printf("    -V  Verbose:  Detailed information on what I'm doing.\n");
1212   printf("    -h  Help:  Detailed instructions for Triangle.\n");
1213   exit(0);
1214 }
1215
1216 #endif /* not TRILIBRARY */
1217
1218 /*****************************************************************************/
1219 /*                                                                           */
1220 /*  info()   Print out complete instructions.                                */
1221 /*                                                                           */
1222 /*****************************************************************************/
1223
1224 #ifndef TRILIBRARY
1225
1226 void info()
1227 {
1228   printf("Triangle\n");
1229   printf(
1230 "A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\n");
1231   printf("Version 1.3\n\n");
1232   printf(
1233 "Copyright 1996 Jonathan Richard Shewchuk  (bugs/comments to jrs@cs.cmu.edu)\n"
1234 );
1235   printf("School of Computer Science / Carnegie Mellon University\n");
1236   printf("5000 Forbes Avenue / Pittsburgh, Pennsylvania  15213-3891\n");
1237   printf(
1238 "Created as part of the Archimedes project (tools for parallel FEM).\n");
1239   printf(
1240 "Supported in part by NSF Grant CMS-9318163 and an NSERC 1967 Scholarship.\n");
1241   printf("There is no warranty whatsoever.  Use at your own risk.\n");
1242 #ifdef SINGLE
1243   printf("This executable is compiled for single precision arithmetic.\n\n\n");
1244 #else /* not SINGLE */
1245   printf("This executable is compiled for double precision arithmetic.\n\n\n");
1246 #endif /* not SINGLE */
1247   printf(
1248 "Triangle generates exact Delaunay triangulations, constrained Delaunay\n");
1249   printf(
1250 "triangulations, and quality conforming Delaunay triangulations.  The latter\n"
1251 );
1252   printf(
1253 "can be generated with no small angles, and are thus suitable for finite\n");
1254   printf(
1255 "element analysis.  If no command line switches are specified, your .node\n");
1256   printf(
1257 "input file will be read, and the Delaunay triangulation will be returned in\n"
1258 );
1259   printf(".node and .ele output files.  The command syntax is:\n\n");
1260 #ifdef CDT_ONLY
1261 #ifdef REDUCED
1262   printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n\n");
1263 #else /* not REDUCED */
1264   printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n\n");
1265 #endif /* not REDUCED */
1266 #else /* not CDT_ONLY */
1267 #ifdef REDUCED
1268   printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n\n");
1269 #else /* not REDUCED */
1270   printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n\n");
1271 #endif /* not REDUCED */
1272 #endif /* not CDT_ONLY */
1273   printf(
1274 "Underscores indicate that numbers may optionally follow certain switches;\n");
1275   printf(
1276 "do not leave any space between a switch and its numeric parameter.\n");
1277   printf(
1278 "input_file must be a file with extension .node, or extension .poly if the\n");
1279   printf(
1280 "-p switch is used.  If -r is used, you must supply .node and .ele files,\n");
1281   printf(
1282 "and possibly a .poly file and .area file as well.  The formats of these\n");
1283   printf("files are described below.\n\n");
1284   printf("Command Line Switches:\n\n");
1285   printf(
1286 "    -p  Reads a Planar Straight Line Graph (.poly file), which can specify\n"
1287 );
1288   printf(
1289 "        points, segments, holes, and regional attributes and area\n");
1290   printf(
1291 "        constraints.  Will generate a constrained Delaunay triangulation\n");
1292   printf(
1293 "        fitting the input; or, if -s, -q, or -a is used, a conforming\n");
1294   printf(
1295 "        Delaunay triangulation.  If -p is not used, Triangle reads a .node\n"
1296 );
1297   printf("        file by default.\n");
1298   printf(
1299 "    -r  Refines a previously generated mesh.  The mesh is read from a .node\n"
1300 );
1301   printf(
1302 "        file and an .ele file.  If -p is also used, a .poly file is read\n");
1303   printf(
1304 "        and used to constrain edges in the mesh.  Further details on\n");
1305   printf("        refinement are given below.\n");
1306   printf(
1307 "    -q  Quality mesh generation by Jim Ruppert's Delaunay refinement\n");
1308   printf(
1309 "        algorithm.  Adds points to the mesh to ensure that no angles\n");
1310   printf(
1311 "        smaller than 20 degrees occur.  An alternative minimum angle may be\n"
1312 );
1313   printf(
1314 "        specified after the `q'.  If the minimum angle is 20.7 degrees or\n");
1315   printf(
1316 "        smaller, the triangulation algorithm is theoretically guaranteed to\n"
1317 );
1318   printf(
1319 "        terminate (assuming infinite precision arithmetic - Triangle may\n");
1320   printf(
1321 "        fail to terminate if you run out of precision).  In practice, the\n");
1322   printf(
1323 "        algorithm often succeeds for minimum angles up to 33.8 degrees.\n");
1324   printf(
1325 "        For highly refined meshes, however, it may be necessary to reduce\n");
1326   printf(
1327 "        the minimum angle to well below 20 to avoid problems associated\n");
1328   printf(
1329 "        with insufficient floating-point precision.  The specified angle\n");
1330   printf("        may include a decimal point.\n");
1331   printf(
1332 "    -a  Imposes a maximum triangle area.  If a number follows the `a', no\n");
1333   printf(
1334 "        triangle will be generated whose area is larger than that number.\n");
1335   printf(
1336 "        If no number is specified, an .area file (if -r is used) or .poly\n");
1337   printf(
1338 "        file (if -r is not used) specifies a number of maximum area\n");
1339   printf(
1340 "        constraints.  An .area file contains a separate area constraint for\n"
1341 );
1342   printf(
1343 "        each triangle, and is useful for refining a finite element mesh\n");
1344   printf(
1345 "        based on a posteriori error estimates.  A .poly file can optionally\n"
1346 );
1347   printf(
1348 "        contain an area constraint for each segment-bounded region, thereby\n"
1349 );
1350   printf(
1351 "        enforcing triangle densities in a first triangulation.  You can\n");
1352   printf(
1353 "        impose both a fixed area constraint and a varying area constraint\n");
1354   printf(
1355 "        by invoking the -a switch twice, once with and once without a\n");
1356   printf(
1357 "        number following.  Each area specified may include a decimal point.\n"
1358 );
1359   printf(
1360 "    -A  Assigns an additional attribute to each triangle that identifies\n");
1361   printf(
1362 "        what segment-bounded region each triangle belongs to.  Attributes\n");
1363   printf(
1364 "        are assigned to regions by the .poly file.  If a region is not\n");
1365   printf(
1366 "        explicitly marked by the .poly file, triangles in that region are\n");
1367   printf(
1368 "        assigned an attribute of zero.  The -A switch has an effect only\n");
1369   printf("        when the -p switch is used and the -r switch is not.\n");
1370   printf(
1371 "    -c  Creates segments on the convex hull of the triangulation.  If you\n");
1372   printf(
1373 "        are triangulating a point set, this switch causes a .poly file to\n");
1374   printf(
1375 "        be written, containing all edges in the convex hull.  (By default,\n"
1376 );
1377   printf(
1378 "        a .poly file is written only if a .poly file is read.)  If you are\n"
1379 );
1380   printf(
1381 "        triangulating a PSLG, this switch specifies that the interior of\n");
1382   printf(
1383 "        the convex hull of the PSLG should be triangulated.  If you do not\n"
1384 );
1385   printf(
1386 "        use this switch when triangulating a PSLG, it is assumed that you\n");
1387   printf(
1388 "        have identified the region to be triangulated by surrounding it\n");
1389   printf(
1390 "        with segments of the input PSLG.  Beware:  if you are not careful,\n"
1391 );
1392   printf(
1393 "        this switch can cause the introduction of an extremely thin angle\n");
1394   printf(
1395 "        between a PSLG segment and a convex hull segment, which can cause\n");
1396   printf(
1397 "        overrefinement or failure if Triangle runs out of precision.  If\n");
1398   printf(
1399 "        you are refining a mesh, the -c switch works differently; it\n");
1400   printf(
1401 "        generates the set of boundary edges of the mesh, rather than the\n");
1402   printf("        convex hull.\n");
1403   printf(
1404 "    -e  Outputs (to an .edge file) a list of edges of the triangulation.\n");
1405   printf(
1406 "    -v  Outputs the Voronoi diagram associated with the triangulation.\n");
1407   printf("        Does not attempt to detect degeneracies.\n");
1408   printf(
1409 "    -n  Outputs (to a .neigh file) a list of triangles neighboring each\n");
1410   printf("        triangle.\n");
1411   printf(
1412 "    -g  Outputs the mesh to an Object File Format (.off) file, suitable for\n"
1413 );
1414   printf("        viewing with the Geometry Center's Geomview package.\n");
1415   printf(
1416 "    -B  No boundary markers in the output .node, .poly, and .edge output\n");
1417   printf(
1418 "        files.  See the detailed discussion of boundary markers below.\n");
1419   printf(
1420 "    -P  No output .poly file.  Saves disk space, but you lose the ability\n");
1421   printf(
1422 "        to impose segment constraints on later refinements of the mesh.\n");
1423   printf("    -N  No output .node file.\n");
1424   printf("    -E  No output .ele file.\n");
1425   printf(
1426 "    -I  No iteration numbers.  Suppresses the output of .node and .poly\n");
1427   printf(
1428 "        files, so your input files won't be overwritten.  (If your input is\n"
1429 );
1430   printf(
1431 "        a .poly file only, a .node file will be written.)  Cannot be used\n");
1432   printf(
1433 "        with the -r switch, because that would overwrite your input .ele\n");
1434   printf(
1435 "        file.  Shouldn't be used with the -s, -q, or -a switch if you are\n");
1436   printf(
1437 "        using a .node file for input, because no .node file will be\n");
1438   printf("        written, so there will be no record of any added points.\n");
1439   printf("    -O  No holes.  Ignores the holes in the .poly file.\n");
1440   printf(
1441 "    -X  No exact arithmetic.  Normally, Triangle uses exact floating-point\n"
1442 );
1443   printf(
1444 "        arithmetic for certain tests if it thinks the inexact tests are not\n"
1445 );
1446   printf(
1447 "        accurate enough.  Exact arithmetic ensures the robustness of the\n");
1448   printf(
1449 "        triangulation algorithms, despite floating-point roundoff error.\n");
1450   printf(
1451 "        Disabling exact arithmetic with the -X switch will cause a small\n");
1452   printf(
1453 "        improvement in speed and create the possibility (albeit small) that\n"
1454 );
1455   printf(
1456 "        Triangle will fail to produce a valid mesh.  Not recommended.\n");
1457   printf(
1458 "    -z  Numbers all items starting from zero (rather than one).  Note that\n"
1459 );
1460   printf(
1461 "        this switch is normally overrided by the value used to number the\n");
1462   printf(
1463 "        first point of the input .node or .poly file.  However, this switch\n"
1464 );
1465   printf("        is useful when calling Triangle from another program.\n");
1466   printf(
1467 "    -o2 Generates second-order subparametric elements with six nodes each.\n"
1468 );
1469   printf(
1470 "    -Y  No new points on the boundary.  This switch is useful when the mesh\n"
1471 );
1472   printf(
1473 "        boundary must be preserved so that it conforms to some adjacent\n");
1474   printf(
1475 "        mesh.  Be forewarned that you will probably sacrifice some of the\n");
1476   printf(
1477 "        quality of the mesh; Triangle will try, but the resulting mesh may\n"
1478 );
1479   printf(
1480 "        contain triangles of poor aspect ratio.  Works well if all the\n");
1481   printf(
1482 "        boundary points are closely spaced.  Specify this switch twice\n");
1483   printf(
1484 "        (`-YY') to prevent all segment splitting, including internal\n");
1485   printf("        boundaries.\n");
1486   printf(
1487 "    -S  Specifies the maximum number of Steiner points (points that are not\n"
1488 );
1489   printf(
1490 "        in the input, but are added to meet the constraints of minimum\n");
1491   printf(
1492 "        angle and maximum area).  The default is to allow an unlimited\n");
1493   printf(
1494 "        number.  If you specify this switch with no number after it,\n");
1495   printf(
1496 "        the limit is set to zero.  Triangle always adds points at segment\n");
1497   printf(
1498 "        intersections, even if it needs to use more points than the limit\n");
1499   printf(
1500 "        you set.  When Triangle inserts segments by splitting (-s), it\n");
1501   printf(
1502 "        always adds enough points to ensure that all the segments appear in\n"
1503 );
1504   printf(
1505 "        the triangulation, again ignoring the limit.  Be forewarned that\n");
1506   printf(
1507 "        the -S switch may result in a conforming triangulation that is not\n"
1508 );
1509   printf(
1510 "        truly Delaunay, because Triangle may be forced to stop adding\n");
1511   printf(
1512 "        points when the mesh is in a state where a segment is non-Delaunay\n"
1513 );
1514   printf(
1515 "        and needs to be split.  If so, Triangle will print a warning.\n");
1516   printf(
1517 "    -i  Uses an incremental rather than divide-and-conquer algorithm to\n");
1518   printf(
1519 "        form a Delaunay triangulation.  Try it if the divide-and-conquer\n");
1520   printf("        algorithm fails.\n");
1521   printf(
1522 "    -F  Uses Steven Fortune's sweepline algorithm to form a Delaunay\n");
1523   printf(
1524 "        triangulation.  Warning:  does not use exact arithmetic for all\n");
1525   printf("        calculations.  An exact result is not guaranteed.\n");
1526   printf(
1527 "    -l  Uses only vertical cuts in the divide-and-conquer algorithm.  By\n");
1528   printf(
1529 "        default, Triangle uses alternating vertical and horizontal cuts,\n");
1530   printf(
1531 "        which usually improve the speed except with point sets that are\n");
1532   printf(
1533 "        small or short and wide.  This switch is primarily of theoretical\n");
1534   printf("        interest.\n");
1535   printf(
1536 "    -s  Specifies that segments should be forced into the triangulation by\n"
1537 );
1538   printf(
1539 "        recursively splitting them at their midpoints, rather than by\n");
1540   printf(
1541 "        generating a constrained Delaunay triangulation.  Segment splitting\n"
1542 );
1543   printf(
1544 "        is true to Ruppert's original algorithm, but can create needlessly\n"
1545 );
1546   printf("        small triangles near external small features.\n");
1547   printf(
1548 "    -C  Check the consistency of the final mesh.  Uses exact arithmetic for\n"
1549 );
1550   printf(
1551 "        checking, even if the -X switch is used.  Useful if you suspect\n");
1552   printf("        Triangle is buggy.\n");
1553   printf(
1554 "    -Q  Quiet: Suppresses all explanation of what Triangle is doing, unless\n"
1555 );
1556   printf("        an error occurs.\n");
1557   printf(
1558 "    -V  Verbose: Gives detailed information about what Triangle is doing.\n");
1559   printf(
1560 "        Add more `V's for increasing amount of detail.  `-V' gives\n");
1561   printf(
1562 "        information on algorithmic progress and more detailed statistics.\n");
1563   printf(
1564 "        `-VV' gives point-by-point details, and will print so much that\n");
1565   printf(
1566 "        Triangle will run much more slowly.  `-VVV' gives information only\n"
1567 );
1568   printf("        a debugger could love.\n");
1569   printf("    -h  Help:  Displays these instructions.\n");
1570   printf("\n");
1571   printf("Definitions:\n");
1572   printf("\n");
1573   printf(
1574 "  A Delaunay triangulation of a point set is a triangulation whose vertices\n"
1575 );
1576   printf(
1577 "  are the point set, having the property that no point in the point set\n");
1578   printf(
1579 "  falls in the interior of the circumcircle (circle that passes through all\n"
1580 );
1581   printf("  three vertices) of any triangle in the triangulation.\n\n");
1582   printf(
1583 "  A Voronoi diagram of a point set is a subdivision of the plane into\n");
1584   printf(
1585 "  polygonal regions (some of which may be infinite), where each region is\n");
1586   printf(
1587 "  the set of points in the plane that are closer to some input point than\n");
1588   printf(
1589 "  to any other input point.  (The Voronoi diagram is the geometric dual of\n"
1590 );
1591   printf("  the Delaunay triangulation.)\n\n");
1592   printf(
1593 "  A Planar Straight Line Graph (PSLG) is a collection of points and\n");
1594   printf(
1595 "  segments.  Segments are simply edges, whose endpoints are points in the\n");
1596   printf(
1597 "  PSLG.  The file format for PSLGs (.poly files) is described below.\n");
1598   printf("\n");
1599   printf(
1600 "  A constrained Delaunay triangulation of a PSLG is similar to a Delaunay\n");
1601   printf(
1602 "  triangulation, but each PSLG segment is present as a single edge in the\n");
1603   printf(
1604 "  triangulation.  (A constrained Delaunay triangulation is not truly a\n");
1605   printf("  Delaunay triangulation.)\n\n");
1606   printf(
1607 "  A conforming Delaunay triangulation of a PSLG is a true Delaunay\n");
1608   printf(
1609 "  triangulation in which each PSLG segment may have been subdivided into\n");
1610   printf(
1611 "  several edges by the insertion of additional points.  These inserted\n");
1612   printf(
1613 "  points are necessary to allow the segments to exist in the mesh while\n");
1614   printf("  maintaining the Delaunay property.\n\n");
1615   printf("File Formats:\n\n");
1616   printf(
1617 "  All files may contain comments prefixed by the character '#'.  Points,\n");
1618   printf(
1619 "  triangles, edges, holes, and maximum area constraints must be numbered\n");
1620   printf(
1621 "  consecutively, starting from either 1 or 0.  Whichever you choose, all\n");
1622   printf(
1623 "  input files must be consistent; if the nodes are numbered from 1, so must\n"
1624 );
1625   printf(
1626 "  be all other objects.  Triangle automatically detects your choice while\n");
1627   printf(
1628 "  reading the .node (or .poly) file.  (When calling Triangle from another\n");
1629   printf(
1630 "  program, use the -z switch if you wish to number objects from zero.)\n");
1631   printf("  Examples of these file formats are given below.\n\n");
1632   printf("  .node files:\n");
1633   printf(
1634 "    First line:  <# of points> <dimension (must be 2)> <# of attributes>\n");
1635   printf(
1636 "                                           <# of boundary markers (0 or 1)>\n"
1637 );
1638   printf(
1639 "    Remaining lines:  <point #> <x> <y> [attributes] [boundary marker]\n");
1640   printf("\n");
1641   printf(
1642 "    The attributes, which are typically floating-point values of physical\n");
1643   printf(
1644 "    quantities (such as mass or conductivity) associated with the nodes of\n"
1645 );
1646   printf(
1647 "    a finite element mesh, are copied unchanged to the output mesh.  If -s,\n"
1648 );
1649   printf(
1650 "    -q, or -a is selected, each new Steiner point added to the mesh will\n");
1651   printf("    have attributes assigned to it by linear interpolation.\n\n");
1652   printf(
1653 "    If the fourth entry of the first line is `1', the last column of the\n");
1654   printf(
1655 "    remainder of the file is assumed to contain boundary markers.  Boundary\n"
1656 );
1657   printf(
1658 "    markers are used to identify boundary points and points resting on PSLG\n"
1659 );
1660   printf(
1661 "    segments; a complete description appears in a section below.  The .node\n"
1662 );
1663   printf(
1664 "    file produced by Triangle will contain boundary markers in the last\n");
1665   printf("    column unless they are suppressed by the -B switch.\n\n");
1666   printf("  .ele files:\n");
1667   printf(
1668 "    First line:  <# of triangles> <points per triangle> <# of attributes>\n");
1669   printf(
1670 "    Remaining lines:  <triangle #> <point> <point> <point> ... [attributes]\n"
1671 );
1672   printf("\n");
1673   printf(
1674 "    Points are indices into the corresponding .node file.  The first three\n"
1675 );
1676   printf(
1677 "    points are the corners, and are listed in counterclockwise order around\n"
1678 );
1679   printf(
1680 "    each triangle.  (The remaining points, if any, depend on the type of\n");
1681   printf(
1682 "    finite element used.)  The attributes are just like those of .node\n");
1683   printf(
1684 "    files.  Because there is no simple mapping from input to output\n");
1685   printf(
1686 "    triangles, an attempt is made to interpolate attributes, which may\n");
1687   printf(
1688 "    result in a good deal of diffusion of attributes among nearby triangles\n"
1689 );
1690   printf(
1691 "    as the triangulation is refined.  Diffusion does not occur across\n");
1692   printf(
1693 "    segments, so attributes used to identify segment-bounded regions remain\n"
1694 );
1695   printf(
1696 "    intact.  In output .ele files, all triangles have three points each\n");
1697   printf(
1698 "    unless the -o2 switch is used, in which case they have six, and the\n");
1699   printf(
1700 "    fourth, fifth, and sixth points lie on the midpoints of the edges\n");
1701   printf("    opposite the first, second, and third corners.\n\n");
1702   printf("  .poly files:\n");
1703   printf(
1704 "    First line:  <# of points> <dimension (must be 2)> <# of attributes>\n");
1705   printf(
1706 "                                           <# of boundary markers (0 or 1)>\n"
1707 );
1708   printf(
1709 "    Following lines:  <point #> <x> <y> [attributes] [boundary marker]\n");
1710   printf("    One line:  <# of segments> <# of boundary markers (0 or 1)>\n");
1711   printf(
1712 "    Following lines:  <segment #> <endpoint> <endpoint> [boundary marker]\n");
1713   printf("    One line:  <# of holes>\n");
1714   printf("    Following lines:  <hole #> <x> <y>\n");
1715   printf(
1716 "    Optional line:  <# of regional attributes and/or area constraints>\n");
1717   printf(
1718 "    Optional following lines:  <constraint #> <x> <y> <attrib> <max area>\n");
1719   printf("\n");
1720   printf(
1721 "    A .poly file represents a PSLG, as well as some additional information.\n"
1722 );
1723   printf(
1724 "    The first section lists all the points, and is identical to the format\n"
1725 );
1726   printf(
1727 "    of .node files.  <# of points> may be set to zero to indicate that the\n"
1728 );
1729   printf(
1730 "    points are listed in a separate .node file; .poly files produced by\n");
1731   printf(
1732 "    Triangle always have this format.  This has the advantage that a point\n"
1733 );
1734   printf(
1735 "    set may easily be triangulated with or without segments.  (The same\n");
1736   printf(
1737 "    effect can be achieved, albeit using more disk space, by making a copy\n"
1738 );
1739   printf(
1740 "    of the .poly file with the extension .node; all sections of the file\n");
1741   printf("    but the first are ignored.)\n\n");
1742   printf(
1743 "    The second section lists the segments.  Segments are edges whose\n");
1744   printf(
1745 "    presence in the triangulation is enforced.  Each segment is specified\n");
1746   printf(
1747 "    by listing the indices of its two endpoints.  This means that you must\n"
1748 );
1749   printf(
1750 "    include its endpoints in the point list.  If -s, -q, and -a are not\n");
1751   printf(
1752 "    selected, Triangle will produce a constrained Delaunay triangulation,\n");
1753   printf(
1754 "    in which each segment appears as a single edge in the triangulation.\n");
1755   printf(
1756 "    If -q or -a is selected, Triangle will produce a conforming Delaunay\n");
1757   printf(
1758 "    triangulation, in which segments may be subdivided into smaller edges.\n"
1759 );
1760   printf("    Each segment, like each point, may have a boundary marker.\n\n");
1761   printf(
1762 "    The third section lists holes (and concavities, if -c is selected) in\n");
1763   printf(
1764 "    the triangulation.  Holes are specified by identifying a point inside\n");
1765   printf(
1766 "    each hole.  After the triangulation is formed, Triangle creates holes\n");
1767   printf(
1768 "    by eating triangles, spreading out from each hole point until its\n");
1769   printf(
1770 "    progress is blocked by PSLG segments; you must be careful to enclose\n");
1771   printf(
1772 "    each hole in segments, or your whole triangulation may be eaten away.\n");
1773   printf(
1774 "    If the two triangles abutting a segment are eaten, the segment itself\n");
1775   printf(
1776 "    is also eaten.  Do not place a hole directly on a segment; if you do,\n");
1777   printf("    Triangle will choose one side of the segment arbitrarily.\n\n");
1778   printf(
1779 "    The optional fourth section lists regional attributes (to be assigned\n");
1780   printf(
1781 "    to all triangles in a region) and regional constraints on the maximum\n");
1782   printf(
1783 "    triangle area.  Triangle will read this section only if the -A switch\n");
1784   printf(
1785 "    is used or the -a switch is used without a number following it, and the\n"
1786 );
1787   printf(
1788 "    -r switch is not used.  Regional attributes and area constraints are\n");
1789   printf(
1790 "    propagated in the same manner as holes; you specify a point for each\n");
1791   printf(
1792 "    attribute and/or constraint, and the attribute and/or constraint will\n");
1793   printf(
1794 "    affect the whole region (bounded by segments) containing the point.  If\n"
1795 );
1796   printf(
1797 "    two values are written on a line after the x and y coordinate, the\n");
1798   printf(
1799 "    former is assumed to be a regional attribute (but will only be applied\n"
1800 );
1801   printf(
1802 "    if the -A switch is selected), and the latter is assumed to be a\n");
1803   printf(
1804 "    regional area constraint (but will only be applied if the -a switch is\n"
1805 );
1806   printf(
1807 "    selected).  You may also specify just one value after the coordinates,\n"
1808 );
1809   printf(
1810 "    which can serve as both an attribute and an area constraint, depending\n"
1811 );
1812   printf(
1813 "    on the choice of switches.  If you are using the -A and -a switches\n");
1814   printf(
1815 "    simultaneously and wish to assign an attribute to some region without\n");
1816   printf("    imposing an area constraint, use a negative maximum area.\n\n");
1817   printf(
1818 "    When a triangulation is created from a .poly file, you must either\n");
1819   printf(
1820 "    enclose the entire region to be triangulated in PSLG segments, or\n");
1821   printf(
1822 "    use the -c switch, which encloses the convex hull of the input point\n");
1823   printf(
1824 "    set.  If you do not use the -c switch, Triangle will eat all triangles\n"
1825 );
1826   printf(
1827 "    on the outer boundary that are not protected by segments; if you are\n");
1828   printf(
1829 "    not careful, your whole triangulation may be eaten away.  If you do\n");
1830   printf(
1831 "    use the -c switch, you can still produce concavities by appropriate\n");
1832   printf("    placement of holes just inside the convex hull.\n\n");
1833   printf(
1834 "    An ideal PSLG has no intersecting segments, nor any points that lie\n");
1835   printf(
1836 "    upon segments (except, of course, the endpoints of each segment.)  You\n"
1837 );
1838   printf(
1839 "    aren't required to make your .poly files ideal, but you should be aware\n"
1840 );
1841   printf(
1842 "    of what can go wrong.  Segment intersections are relatively safe -\n");
1843   printf(
1844 "    Triangle will calculate the intersection points for you and add them to\n"
1845 );
1846   printf(
1847 "    the triangulation - as long as your machine's floating-point precision\n"
1848 );
1849   printf(
1850 "    doesn't become a problem.  You are tempting the fates if you have three\n"
1851 );
1852   printf(
1853 "    segments that cross at the same location, and expect Triangle to figure\n"
1854 );
1855   printf(
1856 "    out where the intersection point is.  Thanks to floating-point roundoff\n"
1857 );
1858   printf(
1859 "    error, Triangle will probably decide that the three segments intersect\n"
1860 );
1861   printf(
1862 "    at three different points, and you will find a minuscule triangle in\n");
1863   printf(
1864 "    your output - unless Triangle tries to refine the tiny triangle, uses\n");
1865   printf(
1866 "    up the last bit of machine precision, and fails to terminate at all.\n");
1867   printf(
1868 "    You're better off putting the intersection point in the input files,\n");
1869   printf(
1870 "    and manually breaking up each segment into two.  Similarly, if you\n");
1871   printf(
1872 "    place a point at the middle of a segment, and hope that Triangle will\n");
1873   printf(
1874 "    break up the segment at that point, you might get lucky.  On the other\n"
1875 );
1876   printf(
1877 "    hand, Triangle might decide that the point doesn't lie precisely on the\n"
1878 );
1879   printf(
1880 "    line, and you'll have a needle-sharp triangle in your output - or a lot\n"
1881 );
1882   printf("    of tiny triangles if you're generating a quality mesh.\n\n");
1883   printf(
1884 "    When Triangle reads a .poly file, it also writes a .poly file, which\n");
1885   printf(
1886 "    includes all edges that are part of input segments.  If the -c switch\n");
1887   printf(
1888 "    is used, the output .poly file will also include all of the edges on\n");
1889   printf(
1890 "    the convex hull.  Hence, the output .poly file is useful for finding\n");
1891   printf(
1892 "    edges associated with input segments and setting boundary conditions in\n"
1893 );
1894   printf(
1895 "    finite element simulations.  More importantly, you will need it if you\n"
1896 );
1897   printf(
1898 "    plan to refine the output mesh, and don't want segments to be missing\n");
1899   printf("    in later triangulations.\n\n");
1900   printf("  .area files:\n");
1901   printf("    First line:  <# of triangles>\n");
1902   printf("    Following lines:  <triangle #> <maximum area>\n\n");
1903   printf(
1904 "    An .area file associates with each triangle a maximum area that is used\n"
1905 );
1906   printf(
1907 "    for mesh refinement.  As with other file formats, every triangle must\n");
1908   printf(
1909 "    be represented, and they must be numbered consecutively.  A triangle\n");
1910   printf(
1911 "    may be left unconstrained by assigning it a negative maximum area.\n");
1912   printf("\n");
1913   printf("  .edge files:\n");
1914   printf("    First line:  <# of edges> <# of boundary markers (0 or 1)>\n");
1915   printf(
1916 "    Following lines:  <edge #> <endpoint> <endpoint> [boundary marker]\n");
1917   printf("\n");
1918   printf(
1919 "    Endpoints are indices into the corresponding .node file.  Triangle can\n"
1920 );
1921   printf(
1922 "    produce .edge files (use the -e switch), but cannot read them.  The\n");
1923   printf(
1924 "    optional column of boundary markers is suppressed by the -B switch.\n");
1925   printf("\n");
1926   printf(
1927 "    In Voronoi diagrams, one also finds a special kind of edge that is an\n");
1928   printf(
1929 "    infinite ray with only one endpoint.  For these edges, a different\n");
1930   printf("    format is used:\n\n");
1931   printf("        <edge #> <endpoint> -1 <direction x> <direction y>\n\n");
1932   printf(
1933 "    The `direction' is a floating-point vector that indicates the direction\n"
1934 );
1935   printf("    of the infinite ray.\n\n");
1936   printf("  .neigh files:\n");
1937   printf(
1938 "    First line:  <# of triangles> <# of neighbors per triangle (always 3)>\n"
1939 );
1940   printf(
1941 "    Following lines:  <triangle #> <neighbor> <neighbor> <neighbor>\n");
1942   printf("\n");
1943   printf(
1944 "    Neighbors are indices into the corresponding .ele file.  An index of -1\n"
1945 );
1946   printf(
1947 "    indicates a mesh boundary, and therefore no neighbor.  Triangle can\n");
1948   printf(
1949 "    produce .neigh files (use the -n switch), but cannot read them.\n");
1950   printf("\n");
1951   printf(
1952 "    The first neighbor of triangle i is opposite the first corner of\n");
1953   printf("    triangle i, and so on.\n\n");
1954   printf("Boundary Markers:\n\n");
1955   printf(
1956 "  Boundary markers are tags used mainly to identify which output points and\n"
1957 );
1958   printf(
1959 "  edges are associated with which PSLG segment, and to identify which\n");
1960   printf(
1961 "  points and edges occur on a boundary of the triangulation.  A common use\n"
1962 );
1963   printf(
1964 "  is to determine where boundary conditions should be applied to a finite\n");
1965   printf(
1966 "  element mesh.  You can prevent boundary markers from being written into\n");
1967   printf("  files produced by Triangle by using the -B switch.\n\n");
1968   printf(
1969 "  The boundary marker associated with each segment in an output .poly file\n"
1970 );
1971   printf("  or edge in an output .edge file is chosen as follows:\n");
1972   printf(
1973 "    - If an output edge is part or all of a PSLG segment with a nonzero\n");
1974   printf(
1975 "      boundary marker, then the edge is assigned the same marker.\n");
1976   printf(
1977 "    - Otherwise, if the edge occurs on a boundary of the triangulation\n");
1978   printf(
1979 "      (including boundaries of holes), then the edge is assigned the marker\n"
1980 );
1981   printf("      one (1).\n");
1982   printf("    - Otherwise, the edge is assigned the marker zero (0).\n");
1983   printf(
1984 "  The boundary marker associated with each point in an output .node file is\n"
1985 );
1986   printf("  chosen as follows:\n");
1987   printf(
1988 "    - If a point is assigned a nonzero boundary marker in the input file,\n");
1989   printf(
1990 "      then it is assigned the same marker in the output .node file.\n");
1991   printf(
1992 "    - Otherwise, if the point lies on a PSLG segment (including the\n");
1993   printf(
1994 "      segment's endpoints) with a nonzero boundary marker, then the point\n");
1995   printf(
1996 "      is assigned the same marker.  If the point lies on several such\n");
1997   printf("      segments, one of the markers is chosen arbitrarily.\n");
1998   printf(
1999 "    - Otherwise, if the point occurs on a boundary of the triangulation,\n");
2000   printf("      then the point is assigned the marker one (1).\n");
2001   printf("    - Otherwise, the point is assigned the marker zero (0).\n");
2002   printf("\n");
2003   printf(
2004 "  If you want Triangle to determine for you which points and edges are on\n");
2005   printf(
2006 "  the boundary, assign them the boundary marker zero (or use no markers at\n"
2007 );
2008   printf(
2009 "  all) in your input files.  Alternatively, you can mark some of them and\n");
2010   printf("  leave others marked zero, allowing Triangle to label them.\n\n");
2011   printf("Triangulation Iteration Numbers:\n\n");
2012   printf(
2013 "  Because Triangle can read and refine its own triangulations, input\n");
2014   printf(
2015 "  and output files have iteration numbers.  For instance, Triangle might\n");
2016   printf(
2017 "  read the files mesh.3.node, mesh.3.ele, and mesh.3.poly, refine the\n");
2018   printf(
2019 "  triangulation, and output the files mesh.4.node, mesh.4.ele, and\n");
2020   printf("  mesh.4.poly.  Files with no iteration number are treated as if\n");
2021   printf(
2022 "  their iteration number is zero; hence, Triangle might read the file\n");
2023   printf(
2024 "  points.node, triangulate it, and produce the files points.1.node and\n");
2025   printf("  points.1.ele.\n\n");
2026   printf(
2027 "  Iteration numbers allow you to create a sequence of successively finer\n");
2028   printf(
2029 "  meshes suitable for multigrid methods.  They also allow you to produce a\n"
2030 );
2031   printf(
2032 "  sequence of meshes using error estimate-driven mesh refinement.\n");
2033   printf("\n");
2034   printf(
2035 "  If you're not using refinement or quality meshing, and you don't like\n");
2036   printf(
2037 "  iteration numbers, use the -I switch to disable them.  This switch will\n");
2038   printf(
2039 "  also disable output of .node and .poly files to prevent your input files\n"
2040 );
2041   printf(
2042 "  from being overwritten.  (If the input is a .poly file that contains its\n"
2043 );
2044   printf("  own points, a .node file will be written.)\n\n");
2045   printf("Examples of How to Use Triangle:\n\n");
2046   printf(
2047 "  `triangle dots' will read points from dots.node, and write their Delaunay\n"
2048 );
2049   printf(
2050 "  triangulation to dots.1.node and dots.1.ele.  (dots.1.node will be\n");
2051   printf(
2052 "  identical to dots.node.)  `triangle -I dots' writes the triangulation to\n"
2053 );
2054   printf(
2055 "  dots.ele instead.  (No additional .node file is needed, so none is\n");
2056   printf("  written.)\n\n");
2057   printf(
2058 "  `triangle -pe object.1' will read a PSLG from object.1.poly (and possibly\n"
2059 );
2060   printf(
2061 "  object.1.node, if the points are omitted from object.1.poly) and write\n");
2062   printf("  their constrained Delaunay triangulation to object.2.node and\n");
2063   printf(
2064 "  object.2.ele.  The segments will be copied to object.2.poly, and all\n");
2065   printf("  edges will be written to object.2.edge.\n\n");
2066   printf(
2067 "  `triangle -pq31.5a.1 object' will read a PSLG from object.poly (and\n");
2068   printf(
2069 "  possibly object.node), generate a mesh whose angles are all greater than\n"
2070 );
2071   printf(
2072 "  31.5 degrees and whose triangles all have area smaller than 0.1, and\n");
2073   printf(
2074 "  write the mesh to object.1.node and object.1.ele.  Each segment may have\n"
2075 );
2076   printf(
2077 "  been broken up into multiple edges; the resulting constrained edges are\n");
2078   printf("  written to object.1.poly.\n\n");
2079   printf(
2080 "  Here is a sample file `box.poly' describing a square with a square hole:\n"
2081 );
2082   printf("\n");
2083   printf(
2084 "    # A box with eight points in 2D, no attributes, one boundary marker.\n");
2085   printf("    8 2 0 1\n");
2086   printf("    # Outer box has these vertices:\n");
2087   printf("     1   0 0   0\n");
2088   printf("     2   0 3   0\n");
2089   printf("     3   3 0   0\n");
2090   printf("     4   3 3   33     # A special marker for this point.\n");
2091   printf("    # Inner square has these vertices:\n");
2092   printf("     5   1 1   0\n");
2093   printf("     6   1 2   0\n");
2094   printf("     7   2 1   0\n");
2095   printf("     8   2 2   0\n");
2096   printf("    # Five segments with boundary markers.\n");
2097   printf("    5 1\n");
2098   printf("     1   1 2   5      # Left side of outer box.\n");
2099   printf("     2   5 7   0      # Segments 2 through 5 enclose the hole.\n");
2100   printf("     3   7 8   0\n");
2101   printf("     4   8 6   10\n");
2102   printf("     5   6 5   0\n");
2103   printf("    # One hole in the middle of the inner square.\n");
2104   printf("    1\n");
2105   printf("     1   1.5 1.5\n\n");
2106   printf(
2107 "  Note that some segments are missing from the outer square, so one must\n");
2108   printf(
2109 "  use the `-c' switch.  After `triangle -pqc box.poly', here is the output\n"
2110 );
2111   printf(
2112 "  file `box.1.node', with twelve points.  The last four points were added\n");
2113   printf(
2114 "  to meet the angle constraint.  Points 1, 2, and 9 have markers from\n");
2115   printf(
2116 "  segment 1.  Points 6 and 8 have markers from segment 4.  All the other\n");
2117   printf(
2118 "  points but 4 have been marked to indicate that they lie on a boundary.\n");
2119   printf("\n");
2120   printf("    12  2  0  1\n");
2121   printf("       1    0   0      5\n");
2122   printf("       2    0   3      5\n");
2123   printf("       3    3   0      1\n");
2124   printf("       4    3   3     33\n");
2125   printf("       5    1   1      1\n");
2126   printf("       6    1   2     10\n");
2127   printf("       7    2   1      1\n");
2128   printf("       8    2   2     10\n");
2129   printf("       9    0   1.5    5\n");
2130   printf("      10    1.5   0    1\n");
2131   printf("      11    3   1.5    1\n");
2132   printf("      12    1.5   3    1\n");
2133   printf("    # Generated by triangle -pqc box.poly\n\n");
2134   printf("  Here is the output file `box.1.ele', with twelve triangles.\n\n");
2135   printf("    12  3  0\n");
2136   printf("       1     5   6   9\n");
2137   printf("       2    10   3   7\n");
2138   printf("       3     6   8  12\n");
2139   printf("       4     9   1   5\n");
2140   printf("       5     6   2   9\n");
2141   printf("       6     7   3  11\n");
2142   printf("       7    11   4   8\n");
2143   printf("       8     7   5  10\n");
2144   printf("       9    12   2   6\n");
2145   printf("      10     8   7  11\n");
2146   printf("      11     5   1  10\n");
2147   printf("      12     8   4  12\n");
2148   printf("    # Generated by triangle -pqc box.poly\n\n");
2149   printf(
2150 "  Here is the output file `box.1.poly'.  Note that segments have been added\n"
2151 );
2152   printf(
2153 "  to represent the convex hull, and some segments have been split by newly\n"
2154 );
2155   printf(
2156 "  added points.  Note also that <# of points> is set to zero to indicate\n");
2157   printf("  that the points should be read from the .node file.\n\n");
2158   printf("    0  2  0  1\n");
2159   printf("    12  1\n");
2160   printf("       1     1   9     5\n");
2161   printf("       2     5   7     1\n");
2162   printf("       3     8   7     1\n");
2163   printf("       4     6   8    10\n");
2164   printf("       5     5   6     1\n");
2165   printf("       6     3  10     1\n");
2166   printf("       7     4  11     1\n");
2167   printf("       8     2  12     1\n");
2168   printf("       9     9   2     5\n");
2169   printf("      10    10   1     1\n");
2170   printf("      11    11   3     1\n");
2171   printf("      12    12   4     1\n");
2172   printf("    1\n");
2173   printf("       1   1.5 1.5\n");
2174   printf("    # Generated by triangle -pqc box.poly\n\n");
2175   printf("Refinement and Area Constraints:\n\n");
2176   printf(
2177 "  The -r switch causes a mesh (.node and .ele files) to be read and\n");
2178   printf(
2179 "  refined.  If the -p switch is also used, a .poly file is read and used to\n"
2180 );
2181   printf(
2182 "  specify edges that are constrained and cannot be eliminated (although\n");
2183   printf(
2184 "  they can be divided into smaller edges) by the refinement process.\n");
2185   printf("\n");
2186   printf(
2187 "  When you refine a mesh, you generally want to impose tighter quality\n");
2188   printf(
2189 "  constraints.  One way to accomplish this is to use -q with a larger\n");
2190   printf(
2191 "  angle, or -a followed by a smaller area than you used to generate the\n");
2192   printf(
2193 "  mesh you are refining.  Another way to do this is to create an .area\n");
2194   printf(
2195 "  file, which specifies a maximum area for each triangle, and use the -a\n");
2196   printf(
2197 "  switch (without a number following).  Each triangle's area constraint is\n"
2198 );
2199   printf(
2200 "  applied to that triangle.  Area constraints tend to diffuse as the mesh\n");
2201   printf(
2202 "  is refined, so if there are large variations in area constraint between\n");
2203   printf("  adjacent triangles, you may not get the results you want.\n\n");
2204   printf(
2205 "  If you are refining a mesh composed of linear (three-node) elements, the\n"
2206 );
2207   printf(
2208 "  output mesh will contain all the nodes present in the input mesh, in the\n"
2209 );
2210   printf(
2211 "  same order, with new nodes added at the end of the .node file.  However,\n"
2212 );
2213   printf(
2214 "  there is no guarantee that each output element is contained in a single\n");
2215   printf(
2216 "  input element.  Often, output elements will overlap two input elements,\n");
2217   printf(
2218 "  and input edges are not present in the output mesh.  Hence, a sequence of\n"
2219 );
2220   printf(
2221 "  refined meshes will form a hierarchy of nodes, but not a hierarchy of\n");
2222   printf(
2223 "  elements.  If you a refining a mesh of higher-order elements, the\n");
2224   printf(
2225 "  hierarchical property applies only to the nodes at the corners of an\n");
2226   printf("  element; other nodes may not be present in the refined mesh.\n\n");
2227   printf(
2228 "  It is important to understand that maximum area constraints in .poly\n");
2229   printf(
2230 "  files are handled differently from those in .area files.  A maximum area\n"
2231 );
2232   printf(
2233 "  in a .poly file applies to the whole (segment-bounded) region in which a\n"
2234 );
2235   printf(
2236 "  point falls, whereas a maximum area in an .area file applies to only one\n"
2237 );
2238   printf(
2239 "  triangle.  Area constraints in .poly files are used only when a mesh is\n");
2240   printf(
2241 "  first generated, whereas area constraints in .area files are used only to\n"
2242 );
2243   printf(
2244 "  refine an existing mesh, and are typically based on a posteriori error\n");
2245   printf(
2246 "  estimates resulting from a finite element simulation on that mesh.\n");
2247   printf("\n");
2248   printf(
2249 "  `triangle -rq25 object.1' will read object.1.node and object.1.ele, then\n"
2250 );
2251   printf(
2252 "  refine the triangulation to enforce a 25 degree minimum angle, and then\n");
2253   printf(
2254 "  write the refined triangulation to object.2.node and object.2.ele.\n");
2255   printf("\n");
2256   printf(
2257 "  `triangle -rpaa6.2 z.3' will read z.3.node, z.3.ele, z.3.poly, and\n");
2258   printf(
2259 "  z.3.area.  After reconstructing the mesh and its segments, Triangle will\n"
2260 );
2261   printf(
2262 "  refine the mesh so that no triangle has area greater than 6.2, and\n");
2263   printf(
2264 "  furthermore the triangles satisfy the maximum area constraints in\n");
2265   printf(
2266 "  z.3.area.  The output is written to z.4.node, z.4.ele, and z.4.poly.\n");
2267   printf("\n");
2268   printf(
2269 "  The sequence `triangle -qa1 x', `triangle -rqa.3 x.1', `triangle -rqa.1\n");
2270   printf(
2271 "  x.2' creates a sequence of successively finer meshes x.1, x.2, and x.3,\n");
2272   printf("  suitable for multigrid.\n\n");
2273   printf("Convex Hulls and Mesh Boundaries:\n\n");
2274   printf(
2275 "  If the input is a point set (rather than a PSLG), Triangle produces its\n");
2276   printf(
2277 "  convex hull as a by-product in the output .poly file if you use the -c\n");
2278   printf(
2279 "  switch.  There are faster algorithms for finding a two-dimensional convex\n"
2280 );
2281   printf(
2282 "  hull than triangulation, of course, but this one comes for free.  If the\n"
2283 );
2284   printf(
2285 "  input is an unconstrained mesh (you are using the -r switch but not the\n");
2286   printf(
2287 "  -p switch), Triangle produces a list of its boundary edges (including\n");
2288   printf("  hole boundaries) as a by-product if you use the -c switch.\n\n");
2289   printf("Voronoi Diagrams:\n\n");
2290   printf(
2291 "  The -v switch produces a Voronoi diagram, in files suffixed .v.node and\n");
2292   printf(
2293 "  .v.edge.  For example, `triangle -v points' will read points.node,\n");
2294   printf(
2295 "  produce its Delaunay triangulation in points.1.node and points.1.ele,\n");
2296   printf(
2297 "  and produce its Voronoi diagram in points.1.v.node and points.1.v.edge.\n");
2298   printf(
2299 "  The .v.node file contains a list of all Voronoi vertices, and the .v.edge\n"
2300 );
2301   printf(
2302 "  file contains a list of all Voronoi edges, some of which may be infinite\n"
2303 );
2304   printf(
2305 "  rays.  (The choice of filenames makes it easy to run the set of Voronoi\n");
2306   printf("  vertices through Triangle, if so desired.)\n\n");
2307   printf(
2308 "  This implementation does not use exact arithmetic to compute the Voronoi\n"
2309 );
2310   printf(
2311 "  vertices, and does not check whether neighboring vertices are identical.\n"
2312 );
2313   printf(
2314 "  Be forewarned that if the Delaunay triangulation is degenerate or\n");
2315   printf(
2316 "  near-degenerate, the Voronoi diagram may have duplicate points, crossing\n"
2317 );
2318   printf(
2319 "  edges, or infinite rays whose direction vector is zero.  Also, if you\n");
2320   printf(
2321 "  generate a constrained (as opposed to conforming) Delaunay triangulation,\n"
2322 );
2323   printf(
2324 "  or if the triangulation has holes, the corresponding Voronoi diagram is\n");
2325   printf("  likely to have crossing edges and unlikely to make sense.\n\n");
2326   printf("Mesh Topology:\n\n");
2327   printf(
2328 "  You may wish to know which triangles are adjacent to a certain Delaunay\n");
2329   printf(
2330 "  edge in an .edge file, which Voronoi regions are adjacent to a certain\n");
2331   printf(
2332 "  Voronoi edge in a .v.edge file, or which Voronoi regions are adjacent to\n"
2333 );
2334   printf(
2335 "  each other.  All of this information can be found by cross-referencing\n");
2336   printf(
2337 "  output files with the recollection that the Delaunay triangulation and\n");
2338   printf("  the Voronoi diagrams are planar duals.\n\n");
2339   printf(
2340 "  Specifically, edge i of an .edge file is the dual of Voronoi edge i of\n");
2341   printf(
2342 "  the corresponding .v.edge file, and is rotated 90 degrees counterclock-\n");
2343   printf(
2344 "  wise from the Voronoi edge.  Triangle j of an .ele file is the dual of\n");
2345   printf(
2346 "  vertex j of the corresponding .v.node file; and Voronoi region k is the\n");
2347   printf("  dual of point k of the corresponding .node file.\n\n");
2348   printf(
2349 "  Hence, to find the triangles adjacent to a Delaunay edge, look at the\n");
2350   printf(
2351 "  vertices of the corresponding Voronoi edge; their dual triangles are on\n");
2352   printf(
2353 "  the left and right of the Delaunay edge, respectively.  To find the\n");
2354   printf(
2355 "  Voronoi regions adjacent to a Voronoi edge, look at the endpoints of the\n"
2356 );
2357   printf(
2358 "  corresponding Delaunay edge; their dual regions are on the right and left\n"
2359 );
2360   printf(
2361 "  of the Voronoi edge, respectively.  To find which Voronoi regions are\n");
2362   printf("  adjacent to each other, just read the list of Delaunay edges.\n");
2363   printf("\n");
2364   printf("Statistics:\n");
2365   printf("\n");
2366   printf(
2367 "  After generating a mesh, Triangle prints a count of the number of points,\n"
2368 );
2369   printf(
2370 "  triangles, edges, boundary edges, and segments in the output mesh.  If\n");
2371   printf(
2372 "  you've forgotten the statistics for an existing mesh, the -rNEP switches\n"
2373 );
2374   printf(
2375 "  (or -rpNEP if you've got a .poly file for the existing mesh) will\n");
2376   printf("  regenerate these statistics without writing any output.\n\n");
2377   printf(
2378 "  The -V switch produces extended statistics, including a rough estimate\n");
2379   printf(
2380 "  of memory use and a histogram of triangle aspect ratios and angles in the\n"
2381 );
2382   printf("  mesh.\n\n");
2383   printf("Exact Arithmetic:\n\n");
2384   printf(
2385 "  Triangle uses adaptive exact arithmetic to perform what computational\n");
2386   printf(
2387 "  geometers call the `orientation' and `incircle' tests.  If the floating-\n"
2388 );
2389   printf(
2390 "  point arithmetic of your machine conforms to the IEEE 754 standard (as\n");
2391   printf(
2392 "  most workstations do), and does not use extended precision internal\n");
2393   printf(
2394 "  registers, then your output is guaranteed to be an absolutely true\n");
2395   printf("  Delaunay or conforming Delaunay triangulation, roundoff error\n");
2396   printf(
2397 "  notwithstanding.  The word `adaptive' implies that these arithmetic\n");
2398   printf(
2399 "  routines compute the result only to the precision necessary to guarantee\n"
2400 );
2401   printf(
2402 "  correctness, so they are usually nearly as fast as their approximate\n");
2403   printf(
2404 "  counterparts.  The exact tests can be disabled with the -X switch.  On\n");
2405   printf(
2406 "  most inputs, this switch will reduce the computation time by about eight\n"
2407 );
2408   printf(
2409 "  percent - it's not worth the risk.  There are rare difficult inputs\n");
2410   printf(
2411 "  (having many collinear and cocircular points), however, for which the\n");
2412   printf(
2413 "  difference could be a factor of two.  These are precisely the inputs most\n"
2414 );
2415   printf("  likely to cause errors if you use the -X switch.\n\n");
2416   printf(
2417 "  Unfortunately, these routines don't solve every numerical problem.  Exact\n"
2418 );
2419   printf(
2420 "  arithmetic is not used to compute the positions of points, because the\n");
2421   printf(
2422 "  bit complexity of point coordinates would grow without bound.  Hence,\n");
2423   printf(
2424 "  segment intersections aren't computed exactly; in very unusual cases,\n");
2425   printf(
2426 "  roundoff error in computing an intersection point might actually lead to\n"
2427 );
2428   printf(
2429 "  an inverted triangle and an invalid triangulation.  (This is one reason\n");
2430   printf(
2431 "  to compute your own intersection points in your .poly files.)  Similarly,\n"
2432 );
2433   printf(
2434 "  exact arithmetic is not used to compute the vertices of the Voronoi\n");
2435   printf("  diagram.\n\n");
2436   printf(
2437 "  Underflow and overflow can also cause difficulties; the exact arithmetic\n"
2438 );
2439   printf(
2440 "  routines do not ameliorate out-of-bounds exponents, which can arise\n");
2441   printf(
2442 "  during the orientation and incircle tests.  As a rule of thumb, you\n");
2443   printf(
2444 "  should ensure that your input values are within a range such that their\n");
2445   printf(
2446 "  third powers can be taken without underflow or overflow.  Underflow can\n");
2447   printf(
2448 "  silently prevent the tests from being performed exactly, while overflow\n");
2449   printf("  will typically cause a floating exception.\n\n");
2450   printf("Calling Triangle from Another Program:\n\n");
2451   printf("  Read the file triangle.h for details.\n\n");
2452   printf("Troubleshooting:\n\n");
2453   printf("  Please read this section before mailing me bugs.\n\n");
2454   printf("  `My output mesh has no triangles!'\n\n");
2455   printf(
2456 "    If you're using a PSLG, you've probably failed to specify a proper set\n"
2457 );
2458   printf(
2459 "    of bounding segments, or forgotten to use the -c switch.  Or you may\n");
2460   printf(
2461 "    have placed a hole badly.  To test these possibilities, try again with\n"
2462 );
2463   printf(
2464 "    the -c and -O switches.  Alternatively, all your input points may be\n");
2465   printf(
2466 "    collinear, in which case you can hardly expect to triangulate them.\n");
2467   printf("\n");
2468   printf("  `Triangle doesn't terminate, or just crashes.'\n");
2469   printf("\n");
2470   printf(
2471 "    Bad things can happen when triangles get so small that the distance\n");
2472   printf(
2473 "    between their vertices isn't much larger than the precision of your\n");
2474   printf(
2475 "    machine's arithmetic.  If you've compiled Triangle for single-precision\n"
2476 );
2477   printf(
2478 "    arithmetic, you might do better by recompiling it for double-precision.\n"
2479 );
2480   printf(
2481 "    Then again, you might just have to settle for more lenient constraints\n"
2482 );
2483   printf(
2484 "    on the minimum angle and the maximum area than you had planned.\n");
2485   printf("\n");
2486   printf(
2487 "    You can minimize precision problems by ensuring that the origin lies\n");
2488   printf(
2489 "    inside your point set, or even inside the densest part of your\n");
2490   printf(
2491 "    mesh.  On the other hand, if you're triangulating an object whose x\n");
2492   printf(
2493 "    coordinates all fall between 6247133 and 6247134, you're not leaving\n");
2494   printf("    much floating-point precision for Triangle to work with.\n\n");
2495   printf(
2496 "    Precision problems can occur covertly if the input PSLG contains two\n");
2497   printf(
2498 "    segments that meet (or intersect) at a very small angle, or if such an\n"
2499 );
2500   printf(
2501 "    angle is introduced by the -c switch, which may occur if a point lies\n");
2502   printf(
2503 "    ever-so-slightly inside the convex hull, and is connected by a PSLG\n");
2504   printf(
2505 "    segment to a point on the convex hull.  If you don't realize that a\n");
2506   printf(
2507 "    small angle is being formed, you might never discover why Triangle is\n");
2508   printf(
2509 "    crashing.  To check for this possibility, use the -S switch (with an\n");
2510   printf(
2511 "    appropriate limit on the number of Steiner points, found by trial-and-\n"
2512 );
2513   printf(
2514 "    error) to stop Triangle early, and view the output .poly file with\n");
2515   printf(
2516 "    Show Me (described below).  Look carefully for small angles between\n");
2517   printf(
2518 "    segments; zoom in closely, as such segments might look like a single\n");
2519   printf("    segment from a distance.\n\n");
2520   printf(
2521 "    If some of the input values are too large, Triangle may suffer a\n");
2522   printf(
2523 "    floating exception due to overflow when attempting to perform an\n");
2524   printf(
2525 "    orientation or incircle test.  (Read the section on exact arithmetic\n");
2526   printf(
2527 "    above.)  Again, I recommend compiling Triangle for double (rather\n");
2528   printf("    than single) precision arithmetic.\n\n");
2529   printf(
2530 "  `The numbering of the output points doesn't match the input points.'\n");
2531   printf("\n");
2532   printf(
2533 "    You may have eaten some of your input points with a hole, or by placing\n"
2534 );
2535   printf("    them outside the area enclosed by segments.\n\n");
2536   printf(
2537 "  `Triangle executes without incident, but when I look at the resulting\n");
2538   printf(
2539 "  mesh, it has overlapping triangles or other geometric inconsistencies.'\n");
2540   printf("\n");
2541   printf(
2542 "    If you select the -X switch, Triangle's divide-and-conquer Delaunay\n");
2543   printf(
2544 "    triangulation algorithm occasionally makes mistakes due to floating-\n");
2545   printf(
2546 "    point roundoff error.  Although these errors are rare, don't use the -X\n"
2547 );
2548   printf("    switch.  If you still have problems, please report the bug.\n");
2549   printf("\n");
2550   printf(
2551 "  Strange things can happen if you've taken liberties with your PSLG.  Do\n");
2552   printf(
2553 "  you have a point lying in the middle of a segment?  Triangle sometimes\n");
2554   printf(
2555 "  copes poorly with that sort of thing.  Do you want to lay out a collinear\n"
2556 );
2557   printf(
2558 "  row of evenly spaced, segment-connected points?  Have you simply defined\n"
2559 );
2560   printf(
2561 "  one long segment connecting the leftmost point to the rightmost point,\n");
2562   printf(
2563 "  and a bunch of points lying along it?  This method occasionally works,\n");
2564   printf(
2565 "  especially with horizontal and vertical lines, but often it doesn't, and\n"
2566 );
2567   printf(
2568 "  you'll have to connect each adjacent pair of points with a separate\n");
2569   printf("  segment.  If you don't like it, tough.\n\n");
2570   printf(
2571 "  Furthermore, if you have segments that intersect other than at their\n");
2572   printf(
2573 "  endpoints, try not to let the intersections fall extremely close to PSLG\n"
2574 );
2575   printf("  points or each other.\n\n");
2576   printf(
2577 "  If you have problems refining a triangulation not produced by Triangle:\n");
2578   printf(
2579 "  Are you sure the triangulation is geometrically valid?  Is it formatted\n");
2580   printf(
2581 "  correctly for Triangle?  Are the triangles all listed so the first three\n"
2582 );
2583   printf("  points are their corners in counterclockwise order?\n\n");
2584   printf("Show Me:\n\n");
2585   printf(
2586 "  Triangle comes with a separate program named `Show Me', whose primary\n");
2587   printf(
2588 "  purpose is to draw meshes on your screen or in PostScript.  Its secondary\n"
2589 );
2590   printf(
2591 "  purpose is to check the validity of your input files, and do so more\n");
2592   printf(
2593 "  thoroughly than Triangle does.  Show Me requires that you have the X\n");
2594   printf(
2595 "  Windows system.  If you didn't receive Show Me with Triangle, complain to\n"
2596 );
2597   printf("  whomever you obtained Triangle from, then send me mail.\n\n");
2598   printf("Triangle on the Web:\n\n");
2599   printf(
2600 "  To see an illustrated, updated version of these instructions, check out\n");
2601   printf("\n");
2602   printf("    http://www.cs.cmu.edu/~quake/triangle.html\n");
2603   printf("\n");
2604   printf("A Brief Plea:\n");
2605   printf("\n");
2606   printf(
2607 "  If you use Triangle, and especially if you use it to accomplish real\n");
2608   printf(
2609 "  work, I would like very much to hear from you.  A short letter or email\n");
2610   printf(
2611 "  (to jrs@cs.cmu.edu) describing how you use Triangle will mean a lot to\n");
2612   printf(
2613 "  me.  The more people I know are using this program, the more easily I can\n"
2614 );
2615   printf(
2616 "  justify spending time on improvements and on the three-dimensional\n");
2617   printf(
2618 "  successor to Triangle, which in turn will benefit you.  Also, I can put\n");
2619   printf(
2620 "  you on a list to receive email whenever a new version of Triangle is\n");
2621   printf("  available.\n\n");
2622   printf(
2623 "  If you use a mesh generated by Triangle in a publication, please include\n"
2624 );
2625   printf("  an acknowledgment as well.\n\n");
2626   printf("Research credit:\n\n");
2627   printf(
2628 "  Of course, I can take credit for only a fraction of the ideas that made\n");
2629   printf(
2630 "  this mesh generator possible.  Triangle owes its existence to the efforts\n"
2631 );
2632   printf(
2633 "  of many fine computational geometers and other researchers, including\n");
2634   printf(
2635 "  Marshall Bern, L. Paul Chew, Boris Delaunay, Rex A. Dwyer, David\n");
2636   printf(
2637 "  Eppstein, Steven Fortune, Leonidas J. Guibas, Donald E. Knuth, C. L.\n");
2638   printf(
2639 "  Lawson, Der-Tsai Lee, Ernst P. Mucke, Douglas M. Priest, Jim Ruppert,\n");
2640   printf(
2641 "  Isaac Saias, Bruce J. Schachter, Micha Sharir, Jorge Stolfi, Christopher\n"
2642 );
2643   printf(
2644 "  J. Van Wyk, David F. Watson, and Binhai Zhu.  See the comments at the\n");
2645   printf("  beginning of the source code for references.\n\n");
2646   exit(0);
2647 }
2648
2649 #endif /* not TRILIBRARY */
2650
2651 /*****************************************************************************/
2652 /*                                                                           */
2653 /*  internalerror()   Ask the user to send me the defective product.  Exit.  */
2654 /*                                                                           */
2655 /*****************************************************************************/
2656
2657 void internalerror()
2658 {
2659   printf("  Please report this bug to jrs@cs.cmu.edu\n");
2660   printf("  Include the message above, your input data set, and the exact\n");
2661   printf("    command line you used to run Triangle.\n");
2662   exit(1);
2663 }
2664
2665 /*****************************************************************************/
2666 /*                                                                           */
2667 /*  parsecommandline()   Read the command line, identify switches, and set   */
2668 /*                       up options and file names.                          */
2669 /*                                                                           */
2670 /*  The effects of this routine are felt entirely through global variables.  */
2671 /*                                                                           */
2672 /*****************************************************************************/
2673
2674 void parsecommandline(argc, argv)
2675 int argc;
2676 char **argv;
2677 {
2678 #ifdef TRILIBRARY
2679 #define STARTINDEX 0
2680 #else /* not TRILIBRARY */
2681 #define STARTINDEX 1
2682   int increment;
2683   int meshnumber;
2684 #endif /* not TRILIBRARY */
2685   int i, j, k;
2686   char workstring[FILENAMESIZE];
2687
2688   poly = refine = quality = vararea = fixedarea = regionattrib = convex = 0;
2689   firstnumber = 1;
2690   edgesout = voronoi = neighbors = geomview = 0;
2691   nobound = nopolywritten = nonodewritten = noelewritten = noiterationnum = 0;
2692   noholes = noexact = 0;
2693   incremental = sweepline = 0;
2694   dwyer = 1;
2695   splitseg = 0;
2696   docheck = 0;
2697   nobisect = 0;
2698   steiner = -1;
2699   order = 1;
2700   minangle = 0.0;
2701   maxarea = -1.0;
2702   quiet = verbose = 0;
2703 #ifndef TRILIBRARY
2704   innodefilename[0] = '\0';
2705 #endif /* not TRILIBRARY */
2706
2707   for (i = STARTINDEX; i < argc; i++) {
2708 #ifndef TRILIBRARY
2709     if (argv[i][0] == '-') {
2710 #endif /* not TRILIBRARY */
2711       for (j = STARTINDEX; argv[i][j] != '\0'; j++) {
2712         if (argv[i][j] == 'p') {
2713           poly = 1;
2714         }
2715 #ifndef CDT_ONLY
2716         if (argv[i][j] == 'r') {
2717           refine = 1;
2718         }
2719         if (argv[i][j] == 'q') {
2720           quality = 1;
2721           if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2722               (argv[i][j + 1] == '.')) {
2723             k = 0;
2724             while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2725                    (argv[i][j + 1] == '.')) {
2726               j++;
2727               workstring[k] = argv[i][j];
2728               k++;
2729             }
2730             workstring[k] = '\0';
2731             minangle = (REAL) strtod(workstring, (char **) NULL);
2732           } else {
2733             minangle = 20.0;
2734           }
2735         }
2736         if (argv[i][j] == 'a') {
2737           quality = 1;
2738           if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2739               (argv[i][j + 1] == '.')) {
2740             fixedarea = 1;
2741             k = 0;
2742             while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2743                    (argv[i][j + 1] == '.')) {
2744               j++;
2745               workstring[k] = argv[i][j];
2746               k++;
2747             }
2748             workstring[k] = '\0';
2749             maxarea = (REAL) strtod(workstring, (char **) NULL);
2750             if (maxarea <= 0.0) {
2751               printf("Error:  Maximum area must be greater than zero.\n");
2752               exit(1);
2753             }
2754           } else {
2755             vararea = 1;
2756           }
2757         }
2758 #endif /* not CDT_ONLY */
2759         if (argv[i][j] == 'A') {
2760           regionattrib = 1;
2761         }
2762         if (argv[i][j] == 'c') {
2763           convex = 1;
2764         }
2765         if (argv[i][j] == 'z') {
2766           firstnumber = 0;
2767         }
2768         if (argv[i][j] == 'e') {
2769           edgesout = 1;
2770         }
2771         if (argv[i][j] == 'v') {
2772           voronoi = 1;
2773         }
2774         if (argv[i][j] == 'n') {
2775           neighbors = 1;
2776         }
2777         if (argv[i][j] == 'g') {
2778           geomview = 1;
2779         }
2780         if (argv[i][j] == 'B') {
2781           nobound = 1;
2782         }
2783         if (argv[i][j] == 'P') {
2784           nopolywritten = 1;
2785         }
2786         if (argv[i][j] == 'N') {
2787           nonodewritten = 1;
2788         }
2789         if (argv[i][j] == 'E') {
2790           noelewritten = 1;
2791         }
2792 #ifndef TRILIBRARY
2793         if (argv[i][j] == 'I') {
2794           noiterationnum = 1;
2795         }
2796 #endif /* not TRILIBRARY */
2797         if (argv[i][j] == 'O') {
2798           noholes = 1;
2799         }
2800         if (argv[i][j] == 'X') {
2801           noexact = 1;
2802         }
2803         if (argv[i][j] == 'o') {
2804           if (argv[i][j + 1] == '2') {
2805             j++;
2806             order = 2;
2807           }
2808         }
2809 #ifndef CDT_ONLY
2810         if (argv[i][j] == 'Y') {
2811           nobisect++;
2812         }
2813         if (argv[i][j] == 'S') {
2814           steiner = 0;
2815           while ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) {
2816             j++;
2817             steiner = steiner * 10 + (int) (argv[i][j] - '0');
2818           }
2819         }
2820 #endif /* not CDT_ONLY */
2821 #ifndef REDUCED
2822         if (argv[i][j] == 'i') {
2823           incremental = 1;
2824         }
2825         if (argv[i][j] == 'F') {
2826           sweepline = 1;
2827         }
2828 #endif /* not REDUCED */
2829         if (argv[i][j] == 'l') {
2830           dwyer = 0;
2831         }
2832 #ifndef REDUCED
2833 #ifndef CDT_ONLY
2834         if (argv[i][j] == 's') {
2835           splitseg = 1;
2836         }
2837 #endif /* not CDT_ONLY */
2838         if (argv[i][j] == 'C') {
2839           docheck = 1;
2840         }
2841 #endif /* not REDUCED */
2842         if (argv[i][j] == 'Q') {
2843           quiet = 1;
2844         }
2845         if (argv[i][j] == 'V') {
2846           verbose++;
2847         }
2848 #ifndef TRILIBRARY
2849         if ((argv[i][j] == 'h') || (argv[i][j] == 'H') ||
2850             (argv[i][j] == '?')) {
2851           info();
2852         }
2853 #endif /* not TRILIBRARY */
2854       }
2855 #ifndef TRILIBRARY
2856     } else {
2857       strncpy(innodefilename, argv[i], FILENAMESIZE - 1);
2858       innodefilename[FILENAMESIZE - 1] = '\0';
2859     }
2860 #endif /* not TRILIBRARY */
2861   }
2862 #ifndef TRILIBRARY
2863   if (innodefilename[0] == '\0') {
2864     syntax();
2865   }
2866   if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".node")) {
2867     innodefilename[strlen(innodefilename) - 5] = '\0';
2868   }
2869   if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".poly")) {
2870     innodefilename[strlen(innodefilename) - 5] = '\0';
2871     poly = 1;
2872   }
2873 #ifndef CDT_ONLY
2874   if (!strcmp(&innodefilename[strlen(innodefilename) - 4], ".ele")) {
2875     innodefilename[strlen(innodefilename) - 4] = '\0';
2876     refine = 1;
2877   }
2878   if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".area")) {
2879     innodefilename[strlen(innodefilename) - 5] = '\0';
2880     refine = 1;
2881     quality = 1;
2882     vararea = 1;
2883   }
2884 #endif /* not CDT_ONLY */
2885 #endif /* not TRILIBRARY */
2886   steinerleft = steiner;
2887   useshelles = poly || refine || quality || convex;
2888   goodangle = cos(minangle * PI / 180.0);
2889   goodangle *= goodangle;
2890   if (refine && noiterationnum) {
2891     printf(
2892       "Error:  You cannot use the -I switch when refining a triangulation.\n");
2893     exit(1);
2894   }
2895   /* Be careful not to allocate space for element area constraints that */
2896   /*   will never be assigned any value (other than the default -1.0).  */
2897   if (!refine && !poly) {
2898     vararea = 0;
2899   }
2900   /* Be careful not to add an extra attribute to each element unless the */
2901   /*   input supports it (PSLG in, but not refining a preexisting mesh). */
2902   if (refine || !poly) {
2903     regionattrib = 0;
2904   }
2905
2906 #ifndef TRILIBRARY
2907   strcpy(inpolyfilename, innodefilename);
2908   strcpy(inelefilename, innodefilename);
2909   strcpy(areafilename, innodefilename);
2910   increment = 0;
2911   strcpy(workstring, innodefilename);
2912   j = 1;
2913   while (workstring[j] != '\0') {
2914     if ((workstring[j] == '.') && (workstring[j + 1] != '\0')) {
2915       increment = j + 1;
2916     }
2917     j++;
2918   }
2919   meshnumber = 0;
2920   if (increment > 0) {
2921     j = increment;
2922     do {
2923       if ((workstring[j] >= '0') && (workstring[j] <= '9')) {
2924         meshnumber = meshnumber * 10 + (int) (workstring[j] - '0');
2925       } else {
2926         increment = 0;
2927       }
2928       j++;
2929     } while (workstring[j] != '\0');
2930   }
2931   if (noiterationnum) {
2932     strcpy(outnodefilename, innodefilename);
2933     strcpy(outelefilename, innodefilename);
2934     strcpy(edgefilename, innodefilename);
2935     strcpy(vnodefilename, innodefilename);
2936     strcpy(vedgefilename, innodefilename);
2937     strcpy(neighborfilename, innodefilename);
2938     strcpy(offfilename, innodefilename);
2939     strcat(outnodefilename, ".node");
2940     strcat(outelefilename, ".ele");
2941     strcat(edgefilename, ".edge");
2942     strcat(vnodefilename, ".v.node");
2943     strcat(vedgefilename, ".v.edge");
2944     strcat(neighborfilename, ".neigh");
2945     strcat(offfilename, ".off");
2946   } else if (increment == 0) {
2947     strcpy(outnodefilename, innodefilename);
2948     strcpy(outpolyfilename, innodefilename);
2949     strcpy(outelefilename, innodefilename);
2950     strcpy(edgefilename, innodefilename);
2951     strcpy(vnodefilename, innodefilename);
2952     strcpy(vedgefilename, innodefilename);
2953     strcpy(neighborfilename, innodefilename);
2954     strcpy(offfilename, innodefilename);
2955     strcat(outnodefilename, ".1.node");
2956     strcat(outpolyfilename, ".1.poly");
2957     strcat(outelefilename, ".1.ele");
2958     strcat(edgefilename, ".1.edge");
2959     strcat(vnodefilename, ".1.v.node");
2960     strcat(vedgefilename, ".1.v.edge");
2961     strcat(neighborfilename, ".1.neigh");
2962     strcat(offfilename, ".1.off");
2963   } else {
2964     workstring[increment] = '%';
2965     workstring[increment + 1] = 'd';
2966     workstring[increment + 2] = '\0';
2967     sprintf(outnodefilename, workstring, meshnumber + 1);
2968     strcpy(outpolyfilename, outnodefilename);
2969     strcpy(outelefilename, outnodefilename);
2970     strcpy(edgefilename, outnodefilename);
2971     strcpy(vnodefilename, outnodefilename);
2972     strcpy(vedgefilename, outnodefilename);
2973     strcpy(neighborfilename, outnodefilename);
2974     strcpy(offfilename, outnodefilename);
2975     strcat(outnodefilename, ".node");
2976     strcat(outpolyfilename, ".poly");
2977     strcat(outelefilename, ".ele");
2978     strcat(edgefilename, ".edge");
2979     strcat(vnodefilename, ".v.node");
2980     strcat(vedgefilename, ".v.edge");
2981     strcat(neighborfilename, ".neigh");
2982     strcat(offfilename, ".off");
2983   }
2984   strcat(innodefilename, ".node");
2985   strcat(inpolyfilename, ".poly");
2986   strcat(inelefilename, ".ele");
2987   strcat(areafilename, ".area");
2988 #endif /* not TRILIBRARY */
2989 }
2990
2991 /**                                                                         **/
2992 /**                                                                         **/
2993 /********* User interaction routines begin here                      *********/
2994
2995 /********* Debugging routines begin here                             *********/
2996 /**                                                                         **/
2997 /**                                                                         **/
2998
2999 /*****************************************************************************/
3000 /*                                                                           */
3001 /*  printtriangle()   Print out the details of a triangle/edge handle.       */
3002 /*                                                                           */
3003 /*  I originally wrote this procedure to simplify debugging; it can be       */
3004 /*  called directly from the debugger, and presents information about a      */
3005 /*  triangle/edge handle in digestible form.  It's also used when the        */
3006 /*  highest level of verbosity (`-VVV') is specified.                        */
3007 /*                                                                           */
3008 /*****************************************************************************/
3009
3010 void printtriangle(t)
3011 struct triedge *t;
3012 {
3013   struct triedge printtri;
3014   struct edge printsh;
3015   point printpoint;
3016
3017   printf("triangle x%lx with orientation %d:\n", (unsigned long) t->tri,
3018          t->orient);
3019   decode(t->tri[0], printtri);
3020   if (printtri.tri == dummytri) {
3021     printf("    [0] = Outer space\n");
3022   } else {
3023     printf("    [0] = x%lx  %d\n", (unsigned long) printtri.tri,
3024            printtri.orient);
3025   }
3026   decode(t->tri[1], printtri);
3027   if (printtri.tri == dummytri) {
3028     printf("    [1] = Outer space\n");
3029   } else {
3030     printf("    [1] = x%lx  %d\n", (unsigned long) printtri.tri,
3031            printtri.orient);
3032   }
3033   decode(t->tri[2], printtri);
3034   if (printtri.tri == dummytri) {
3035     printf("    [2] = Outer space\n");
3036   } else {
3037     printf("    [2] = x%lx  %d\n", (unsigned long) printtri.tri,
3038            printtri.orient);
3039   }
3040   org(*t, printpoint);
3041   if (printpoint == (point) NULL)
3042     printf("    Origin[%d] = NULL\n", (t->orient + 1) % 3 + 3);
3043   else
3044     printf("    Origin[%d] = x%lx  (%.12g, %.12g)\n",
3045            (t->orient + 1) % 3 + 3, (unsigned long) printpoint,
3046            printpoint[0], printpoint[1]);
3047   dest(*t, printpoint);
3048   if (printpoint == (point) NULL)
3049     printf("    Dest  [%d] = NULL\n", (t->orient + 2) % 3 + 3);
3050   else
3051     printf("    Dest  [%d] = x%lx  (%.12g, %.12g)\n",
3052            (t->orient + 2) % 3 + 3, (unsigned long) printpoint,
3053            printpoint[0], printpoint[1]);
3054   apex(*t, printpoint);
3055   if (printpoint == (point) NULL)
3056     printf("    Apex  [%d] = NULL\n", t->orient + 3);
3057   else
3058     printf("    Apex  [%d] = x%lx  (%.12g, %.12g)\n",
3059            t->orient + 3, (unsigned long) printpoint,
3060            printpoint[0], printpoint[1]);
3061   if (useshelles) {
3062     sdecode(t->tri[6], printsh);
3063     if (printsh.sh != dummysh) {
3064       printf("    [6] = x%lx  %d\n", (unsigned long) printsh.sh,
3065              printsh.shorient);
3066     }
3067     sdecode(t->tri[7], printsh);
3068     if (printsh.sh != dummysh) {
3069       printf("    [7] = x%lx  %d\n", (unsigned long) printsh.sh,
3070              printsh.shorient);
3071     }
3072     sdecode(t->tri[8], printsh);
3073     if (printsh.sh != dummysh) {
3074       printf("    [8] = x%lx  %d\n", (unsigned long) printsh.sh,
3075              printsh.shorient);
3076     }
3077   }
3078   if (vararea) {
3079     printf("    Area constraint:  %.4g\n", areabound(*t));
3080   }
3081 }
3082
3083 /*****************************************************************************/
3084 /*                                                                           */
3085 /*  printshelle()   Print out the details of a shell edge handle.            */
3086 /*                                                                           */
3087 /*  I originally wrote this procedure to simplify debugging; it can be       */
3088 /*  called directly from the debugger, and presents information about a      */
3089 /*  shell edge handle in digestible form.  It's also used when the highest   */
3090 /*  level of verbosity (`-VVV') is specified.                                */
3091 /*                                                                           */
3092 /*****************************************************************************/
3093
3094 void printshelle(s)
3095 struct edge *s;
3096 {
3097   struct edge printsh;
3098   struct triedge printtri;
3099   point printpoint;
3100
3101   printf("shell edge x%lx with orientation %d and mark %d:\n",
3102          (unsigned long) s->sh, s->shorient, mark(*s));
3103   sdecode(s->sh[0], printsh);
3104   if (printsh.sh == dummysh) {
3105     printf("    [0] = No shell\n");
3106   } else {
3107     printf("    [0] = x%lx  %d\n", (unsigned long) printsh.sh,
3108            printsh.shorient);
3109   }
3110   sdecode(s->sh[1], printsh);
3111   if (printsh.sh == dummysh) {
3112     printf("    [1] = No shell\n");
3113   } else {
3114     printf("    [1] = x%lx  %d\n", (unsigned long) printsh.sh,
3115            printsh.shorient);
3116   }
3117   sorg(*s, printpoint);
3118   if (printpoint == (point) NULL)
3119     printf("    Origin[%d] = NULL\n", 2 + s->shorient);
3120   else
3121     printf("    Origin[%d] = x%lx  (%.12g, %.12g)\n",
3122            2 + s->shorient, (unsigned long) printpoint,
3123            printpoint[0], printpoint[1]);
3124   sdest(*s, printpoint);
3125   if (printpoint == (point) NULL)
3126     printf("    Dest  [%d] = NULL\n", 3 - s->shorient);
3127   else
3128     printf("    Dest  [%d] = x%lx  (%.12g, %.12g)\n",
3129            3 - s->shorient, (unsigned long) printpoint,
3130            printpoint[0], printpoint[1]);
3131   decode(s->sh[4], printtri);
3132   if (printtri.tri == dummytri) {
3133     printf("    [4] = Outer space\n");
3134   } else {
3135     printf("    [4] = x%lx  %d\n", (unsigned long) printtri.tri,
3136            printtri.orient);
3137   }
3138   decode(s->sh[5], printtri);
3139   if (printtri.tri == dummytri) {
3140     printf("    [5] = Outer space\n");
3141   } else {
3142     printf("    [5] = x%lx  %d\n", (unsigned long) printtri.tri,
3143            printtri.orient);
3144   }
3145 }
3146
3147 /**                                                                         **/
3148 /**                                                                         **/
3149 /********* Debugging routines end here                               *********/
3150
3151 /********* Memory management routines begin here                     *********/
3152 /**                                                                         **/
3153 /**                                                                         **/
3154
3155 /*****************************************************************************/
3156 /*                                                                           */
3157 /*  poolinit()   Initialize a pool of memory for allocation of items.        */
3158 /*                                                                           */
3159 /*  This routine initializes the machinery for allocating items.  A `pool'   */
3160 /*  is created whose records have size at least `bytecount'.  Items will be  */
3161 /*  allocated in `itemcount'-item blocks.  Each item is assumed to be a      */
3162 /*  collection of words, and either pointers or floating-point values are    */
3163 /*  assumed to be the "primary" word type.  (The "primary" word type is used */
3164 /*  to determine alignment of items.)  If `alignment' isn't zero, all items  */
3165 /*  will be `alignment'-byte aligned in memory.  `alignment' must be either  */
3166 /*  a multiple or a factor of the primary word size; powers of two are safe. */
3167 /*  `alignment' is normally used to create a few unused bits at the bottom   */
3168 /*  of each item's pointer, in which information may be stored.              */
3169 /*                                                                           */
3170 /*  Don't change this routine unless you understand it.                      */
3171 /*                                                                           */
3172 /*****************************************************************************/
3173
3174 void poolinit(pool, bytecount, itemcount, wtype, alignment)
3175 struct memorypool *pool;
3176 int bytecount;
3177 int itemcount;
3178 enum wordtype wtype;
3179 int alignment;
3180 {
3181   int wordsize;
3182
3183   /* Initialize values in the pool. */
3184   pool->itemwordtype = wtype;
3185   wordsize = (pool->itemwordtype == POINTER) ? sizeof(VOID *) : sizeof(REAL);
3186   /* Find the proper alignment, which must be at least as large as:   */
3187   /*   - The parameter `alignment'.                                   */
3188   /*   - The primary word type, to avoid unaligned accesses.          */
3189   /*   - sizeof(VOID *), so the stack of dead items can be maintained */
3190   /*       without unaligned accesses.                                */
3191   if (alignment > wordsize) {
3192     pool->alignbytes = alignment;
3193   } else {
3194     pool->alignbytes = wordsize;
3195   }
3196   if (sizeof(VOID *) > pool->alignbytes) {
3197     pool->alignbytes = sizeof(VOID *);
3198   }
3199   pool->itemwords = ((bytecount + pool->alignbytes - 1) / pool->alignbytes)
3200                   * (pool->alignbytes / wordsize);
3201   pool->itembytes = pool->itemwords * wordsize;
3202   pool->itemsperblock = itemcount;
3203
3204   /* Allocate a block of items.  Space for `itemsperblock' items and one    */
3205   /*   pointer (to point to the next block) are allocated, as well as space */
3206   /*   to ensure alignment of the items.                                    */
3207   pool->firstblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
3208                                       + sizeof(VOID *) + pool->alignbytes);
3209   if (pool->firstblock == (VOID **) NULL) {
3210     printf("Error:  Out of memory.\n");
3211     exit(1);
3212   }
3213   /* Set the next block pointer to NULL. */
3214   *(pool->firstblock) = (VOID *) NULL;
3215   poolrestart(pool);
3216 }
3217
3218 /*****************************************************************************/
3219 /*                                                                           */
3220 /*  poolrestart()   Deallocate all items in a pool.                          */
3221 /*                                                                           */
3222 /*  The pool is returned to its starting state, except that no memory is     */
3223 /*  freed to the operating system.  Rather, the previously allocated blocks  */
3224 /*  are ready to be reused.                                                  */
3225 /*                                                                           */
3226 /*****************************************************************************/
3227
3228 void poolrestart(pool)
3229 struct memorypool *pool;
3230 {
3231   unsigned long alignptr;
3232
3233   pool->items = 0;
3234   pool->maxitems = 0;
3235
3236   /* Set the currently active block. */
3237   pool->nowblock = pool->firstblock;
3238   /* Find the first item in the pool.  Increment by the size of (VOID *). */
3239   alignptr = (unsigned long) (pool->nowblock + 1);
3240   /* Align the item on an `alignbytes'-byte boundary. */
3241   pool->nextitem = (VOID *)
3242     (alignptr + (unsigned long) pool->alignbytes
3243      - (alignptr % (unsigned long) pool->alignbytes));
3244   /* There are lots of unallocated items left in this block. */
3245   pool->unallocateditems = pool->itemsperblock;
3246   /* The stack of deallocated items is empty. */
3247   pool->deaditemstack = (VOID *) NULL;
3248 }
3249
3250 /*****************************************************************************/
3251 /*                                                                           */
3252 /*  pooldeinit()   Free to the operating system all memory taken by a pool.  */
3253 /*                                                                           */
3254 /*****************************************************************************/
3255
3256 void pooldeinit(pool)
3257 struct memorypool *pool;
3258 {
3259   while (pool->firstblock != (VOID **) NULL) {
3260     pool->nowblock = (VOID **) *(pool->firstblock);
3261     free(pool->firstblock);
3262     pool->firstblock = pool->nowblock;
3263   }
3264 }
3265
3266 /*****************************************************************************/
3267 /*                                                                           */
3268 /*  poolalloc()   Allocate space for an item.                                */
3269 /*                                                                           */
3270 /*****************************************************************************/
3271
3272 VOID *poolalloc(pool)
3273 struct memorypool *pool;
3274 {
3275   VOID *newitem;
3276   VOID **newblock;
3277   unsigned long alignptr;
3278
3279   /* First check the linked list of dead items.  If the list is not   */
3280   /*   empty, allocate an item from the list rather than a fresh one. */
3281   if (pool->deaditemstack != (VOID *) NULL) {
3282     newitem = pool->deaditemstack;               /* Take first item in list. */
3283     pool->deaditemstack = * (VOID **) pool->deaditemstack;
3284   } else {
3285     /* Check if there are any free items left in the current block. */
3286     if (pool->unallocateditems == 0) {
3287       /* Check if another block must be allocated. */
3288       if (*(pool->nowblock) == (VOID *) NULL) {
3289         /* Allocate a new block of items, pointed to by the previous block. */
3290         newblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
3291                                     + sizeof(VOID *) + pool->alignbytes);
3292         if (newblock == (VOID **) NULL) {
3293           printf("Error:  Out of memory.\n");
3294           exit(1);
3295         }
3296         *(pool->nowblock) = (VOID *) newblock;
3297         /* The next block pointer is NULL. */
3298         *newblock = (VOID *) NULL;
3299       }
3300       /* Move to the new block. */
3301       pool->nowblock = (VOID **) *(pool->nowblock);
3302       /* Find the first item in the block.    */
3303       /*   Increment by the size of (VOID *). */
3304       alignptr = (unsigned long) (pool->nowblock + 1);
3305       /* Align the item on an `alignbytes'-byte boundary. */
3306       pool->nextitem = (VOID *)
3307         (alignptr + (unsigned long) pool->alignbytes
3308          - (alignptr % (unsigned long) pool->alignbytes));
3309       /* There are lots of unallocated items left in this block. */
3310       pool->unallocateditems = pool->itemsperblock;
3311     }
3312     /* Allocate a new item. */
3313     newitem = pool->nextitem;
3314     /* Advance `nextitem' pointer to next free item in block. */
3315     if (pool->itemwordtype == POINTER) {
3316       pool->nextitem = (VOID *) ((VOID **) pool->nextitem + pool->itemwords);
3317     } else {
3318       pool->nextitem = (VOID *) ((REAL *) pool->nextitem + pool->itemwords);
3319     }
3320     pool->unallocateditems--;
3321     pool->maxitems++;
3322   }
3323   pool->items++;
3324   return newitem;
3325 }
3326
3327 /*****************************************************************************/
3328 /*                                                                           */
3329 /*  pooldealloc()   Deallocate space for an item.                            */
3330 /*                                                                           */
3331 /*  The deallocated space is stored in a queue for later reuse.              */
3332 /*                                                                           */
3333 /*****************************************************************************/
3334
3335 void pooldealloc(pool, dyingitem)
3336 struct memorypool *pool;
3337 VOID *dyingitem;
3338 {
3339   /* Push freshly killed item onto stack. */
3340   *((VOID **) dyingitem) = pool->deaditemstack;
3341   pool->deaditemstack = dyingitem;
3342   pool->items--;
3343 }
3344
3345 /*****************************************************************************/
3346 /*                                                                           */
3347 /*  traversalinit()   Prepare to traverse the entire list of items.          */
3348 /*                                                                           */
3349 /*  This routine is used in conjunction with traverse().                     */
3350 /*                                                                           */
3351 /*****************************************************************************/
3352
3353 void traversalinit(pool)
3354 struct memorypool *pool;
3355 {
3356   unsigned long alignptr;
3357
3358   /* Begin the traversal in the first block. */
3359   pool->pathblock = pool->firstblock;
3360   /* Find the first item in the block.  Increment by the size of (VOID *). */
3361   alignptr = (unsigned long) (pool->pathblock + 1);
3362   /* Align with item on an `alignbytes'-byte boundary. */
3363   pool->pathitem = (VOID *)
3364     (alignptr + (unsigned long) pool->alignbytes
3365      - (alignptr % (unsigned long) pool->alignbytes));
3366   /* Set the number of items left in the current block. */
3367   pool->pathitemsleft = pool->itemsperblock;
3368 }
3369
3370 /*****************************************************************************/
3371 /*                                                                           */
3372 /*  traverse()   Find the next item in the list.                             */
3373 /*                                                                           */
3374 /*  This routine is used in conjunction with traversalinit().  Be forewarned */
3375 /*  that this routine successively returns all items in the list, including  */
3376 /*  deallocated ones on the deaditemqueue.  It's up to you to figure out     */
3377 /*  which ones are actually dead.  Why?  I don't want to allocate extra      */
3378 /*  space just to demarcate dead items.  It can usually be done more         */
3379 /*  space-efficiently by a routine that knows something about the structure  */
3380 /*  of the item.                                                             */
3381 /*                                                                           */
3382 /*****************************************************************************/
3383
3384 VOID *traverse(pool)
3385 struct memorypool *pool;
3386 {
3387   VOID *newitem;
3388   unsigned long alignptr;
3389
3390   /* Stop upon exhausting the list of items. */
3391   if (pool->pathitem == pool->nextitem) {
3392     return (VOID *) NULL;
3393   }
3394   /* Check whether any untraversed items remain in the current block. */
3395   if (pool->pathitemsleft == 0) {
3396     /* Find the next block. */
3397     pool->pathblock = (VOID **) *(pool->pathblock);
3398     /* Find the first item in the block.  Increment by the size of (VOID *). */
3399     alignptr = (unsigned long) (pool->pathblock + 1);
3400     /* Align with item on an `alignbytes'-byte boundary. */
3401     pool->pathitem = (VOID *)
3402       (alignptr + (unsigned long) pool->alignbytes
3403        - (alignptr % (unsigned long) pool->alignbytes));
3404     /* Set the number of items left in the current block. */
3405     pool->pathitemsleft = pool->itemsperblock;
3406   }
3407   newitem = pool->pathitem;
3408   /* Find the next item in the block. */
3409   if (pool->itemwordtype == POINTER) {
3410     pool->pathitem = (VOID *) ((VOID **) pool->pathitem + pool->itemwords);
3411   } else {
3412     pool->pathitem = (VOID *) ((REAL *) pool->pathitem + pool->itemwords);
3413   }
3414   pool->pathitemsleft--;
3415   return newitem;
3416 }
3417
3418 /*****************************************************************************/
3419 /*                                                                           */
3420 /*  dummyinit()   Initialize the triangle that fills "outer space" and the   */
3421 /*                omnipresent shell edge.                                    */
3422 /*                                                                           */
3423 /*  The triangle that fills "outer space", called `dummytri', is pointed to  */
3424 /*  by every triangle and shell edge on a boundary (be it outer or inner) of */
3425 /*  the triangulation.  Also, `dummytri' points to one of the triangles on   */
3426 /*  the convex hull (until the holes and concavities are carved), making it  */
3427 /*  possible to find a starting triangle for point location.                 */
3428 /*                                                                           */
3429 /*  The omnipresent shell edge, `dummysh', is pointed to by every triangle   */
3430 /*  or shell edge that doesn't have a full complement of real shell edges    */
3431 /*  to point to.                                                             */
3432 /*                                                                           */
3433 /*****************************************************************************/
3434
3435 void dummyinit(trianglewords, shellewords)
3436 int trianglewords;
3437 int shellewords;
3438 {
3439   unsigned long alignptr;
3440
3441   /* `triwords' and `shwords' are used by the mesh manipulation primitives */
3442   /*   to extract orientations of triangles and shell edges from pointers. */
3443   triwords = trianglewords;       /* Initialize `triwords' once and for all. */
3444   shwords = shellewords;           /* Initialize `shwords' once and for all. */
3445
3446   /* Set up `dummytri', the `triangle' that occupies "outer space". */
3447   dummytribase = (triangle *) malloc(triwords * sizeof(triangle)
3448                                      + triangles.alignbytes);
3449   if (dummytribase == (triangle *) NULL) {
3450     printf("Error:  Out of memory.\n");
3451     exit(1);
3452   }
3453   /* Align `dummytri' on a `triangles.alignbytes'-byte boundary. */
3454   alignptr = (unsigned long) dummytribase;
3455   dummytri = (triangle *)
3456     (alignptr + (unsigned long) triangles.alignbytes
3457      - (alignptr % (unsigned long) triangles.alignbytes));
3458   /* Initialize the three adjoining triangles to be "outer space".  These  */
3459   /*   will eventually be changed by various bonding operations, but their */
3460   /*   values don't really matter, as long as they can legally be          */
3461   /*   dereferenced.                                                       */
3462   dummytri[0] = (triangle) dummytri;
3463   dummytri[1] = (triangle) dummytri;
3464   dummytri[2] = (triangle) dummytri;
3465   /* Three NULL vertex points. */
3466   dummytri[3] = (triangle) NULL;
3467   dummytri[4] = (triangle) NULL;
3468   dummytri[5] = (triangle) NULL;
3469
3470   if (useshelles) {
3471     /* Set up `dummysh', the omnipresent "shell edge" pointed to by any      */
3472     /*   triangle side or shell edge end that isn't attached to a real shell */
3473     /*   edge.                                                               */
3474     dummyshbase = (shelle *) malloc(shwords * sizeof(shelle)
3475                                     + shelles.alignbytes);
3476     if (dummyshbase == (shelle *) NULL) {
3477       printf("Error:  Out of memory.\n");
3478       exit(1);
3479     }
3480     /* Align `dummysh' on a `shelles.alignbytes'-byte boundary. */
3481     alignptr = (unsigned long) dummyshbase;
3482     dummysh = (shelle *)
3483       (alignptr + (unsigned long) shelles.alignbytes
3484        - (alignptr % (unsigned long) shelles.alignbytes));
3485     /* Initialize the two adjoining shell edges to be the omnipresent shell */
3486     /*   edge.  These will eventually be changed by various bonding         */
3487     /*   operations, but their values don't really matter, as long as they  */
3488     /*   can legally be dereferenced.                                       */
3489     dummysh[0] = (shelle) dummysh;
3490     dummysh[1] = (shelle) dummysh;
3491     /* Two NULL vertex points. */
3492     dummysh[2] = (shelle) NULL;
3493     dummysh[3] = (shelle) NULL;
3494     /* Initialize the two adjoining triangles to be "outer space". */
3495     dummysh[4] = (shelle) dummytri;
3496     dummysh[5] = (shelle) dummytri;
3497     /* Set the boundary marker to zero. */
3498     * (int *) (dummysh + 6) = 0;
3499
3500     /* Initialize the three adjoining shell edges of `dummytri' to be */
3501     /*   the omnipresent shell edge.                                  */
3502     dummytri[6] = (triangle) dummysh;
3503     dummytri[7] = (triangle) dummysh;
3504     dummytri[8] = (triangle) dummysh;
3505   }
3506 }
3507
3508 /*****************************************************************************/
3509 /*                                                                           */
3510 /*  initializepointpool()   Calculate the size of the point data structure   */
3511 /*                          and initialize its memory pool.                  */
3512 /*                                                                           */
3513 /*  This routine also computes the `pointmarkindex' and `point2triindex'     */
3514 /*  indices used to find values within each point.                           */
3515 /*                                                                           */
3516 /*****************************************************************************/
3517
3518 void initializepointpool()
3519 {
3520   int pointsize;
3521
3522   /* The index within each point at which the boundary marker is found.  */
3523   /*   Ensure the point marker is aligned to a sizeof(int)-byte address. */
3524   pointmarkindex = ((mesh_dim + nextras) * sizeof(REAL) + sizeof(int) - 1)
3525                  / sizeof(int);
3526   pointsize = (pointmarkindex + 1) * sizeof(int);
3527   if (poly) {
3528     /* The index within each point at which a triangle pointer is found.   */
3529     /*   Ensure the pointer is aligned to a sizeof(triangle)-byte address. */
3530     point2triindex = (pointsize + sizeof(triangle) - 1) / sizeof(triangle);
3531     pointsize = (point2triindex + 1) * sizeof(triangle);
3532   }
3533   /* Initialize the pool of points. */
3534   poolinit(&points, pointsize, POINTPERBLOCK,
3535            (sizeof(REAL) >= sizeof(triangle)) ? FLOATINGPOINT : POINTER, 0);
3536 }
3537
3538 /*****************************************************************************/
3539 /*                                                                           */
3540 /*  initializetrisegpools()   Calculate the sizes of the triangle and shell  */
3541 /*                            edge data structures and initialize their      */
3542 /*                            memory pools.                                  */
3543 /*                                                                           */
3544 /*  This routine also computes the `highorderindex', `elemattribindex', and  */
3545 /*  `areaboundindex' indices used to find values within each triangle.       */
3546 /*                                                                           */
3547 /*****************************************************************************/
3548
3549 void initializetrisegpools()
3550 {
3551   int trisize;
3552
3553   /* The index within each triangle at which the extra nodes (above three)  */
3554   /*   associated with high order elements are found.  There are three      */
3555   /*   pointers to other triangles, three pointers to corners, and possibly */
3556   /*   three pointers to shell edges before the extra nodes.                */
3557   highorderindex = 6 + (useshelles * 3);
3558   /* The number of bytes occupied by a triangle. */
3559   trisize = ((order + 1) * (order + 2) / 2 + (highorderindex - 3)) *
3560             sizeof(triangle);
3561   /* The index within each triangle at which its attributes are found, */
3562   /*   where the index is measured in REALs.                           */
3563   elemattribindex = (trisize + sizeof(REAL) - 1) / sizeof(REAL);
3564   /* The index within each triangle at which the maximum area constraint  */
3565   /*   is found, where the index is measured in REALs.  Note that if the  */
3566   /*   `regionattrib' flag is set, an additional attribute will be added. */
3567   areaboundindex = elemattribindex + eextras + regionattrib;
3568   /* If triangle attributes or an area bound are needed, increase the number */
3569   /*   of bytes occupied by a triangle.                                      */
3570   if (vararea) {
3571     trisize = (areaboundindex + 1) * sizeof(REAL);
3572   } else if (eextras + regionattrib > 0) {
3573     trisize = areaboundindex * sizeof(REAL);
3574   }
3575   /* If a Voronoi diagram or triangle neighbor graph is requested, make    */
3576   /*   sure there's room to store an integer index in each triangle.  This */
3577   /*   integer index can occupy the same space as the shell edges or       */
3578   /*   attributes or area constraint or extra nodes.                       */
3579   if ((voronoi || neighbors) &&
3580       (trisize < 6 * sizeof(triangle) + sizeof(int))) {
3581     trisize = 6 * sizeof(triangle) + sizeof(int);
3582   }
3583   /* Having determined the memory size of a triangle, initialize the pool. */
3584   poolinit(&triangles, trisize, TRIPERBLOCK, POINTER, 4);
3585
3586   if (useshelles) {
3587     /* Initialize the pool of shell edges. */
3588     poolinit(&shelles, 6 * sizeof(triangle) + sizeof(int), SHELLEPERBLOCK,
3589              POINTER, 4);
3590
3591     /* Initialize the "outer space" triangle and omnipresent shell edge. */
3592     dummyinit(triangles.itemwords, shelles.itemwords);
3593   } else {
3594     /* Initialize the "outer space" triangle. */
3595     dummyinit(triangles.itemwords, 0);
3596   }
3597 }
3598
3599 /*****************************************************************************/
3600 /*                                                                           */
3601 /*  triangledealloc()   Deallocate space for a triangle, marking it dead.    */
3602 /*                                                                           */
3603 /*****************************************************************************/
3604
3605 void triangledealloc(dyingtriangle)
3606 triangle *dyingtriangle;
3607 {
3608   /* Set triangle's vertices to NULL.  This makes it possible to        */
3609   /*   detect dead triangles when traversing the list of all triangles. */
3610   dyingtriangle[3] = (triangle) NULL;
3611   dyingtriangle[4] = (triangle) NULL;
3612   dyingtriangle[5] = (triangle) NULL;
3613   pooldealloc(&triangles, (VOID *) dyingtriangle);
3614 }
3615
3616 /*****************************************************************************/
3617 /*                                                                           */
3618 /*  triangletraverse()   Traverse the triangles, skipping dead ones.         */
3619 /*                                                                           */
3620 /*****************************************************************************/
3621
3622 triangle *triangletraverse()
3623 {
3624   triangle *newtriangle;
3625
3626   do {
3627     newtriangle = (triangle *) traverse(&triangles);
3628     if (newtriangle == (triangle *) NULL) {
3629       return (triangle *) NULL;
3630     }
3631   } while (newtriangle[3] == (triangle) NULL);            /* Skip dead ones. */
3632   return newtriangle;
3633 }
3634
3635 /*****************************************************************************/
3636 /*                                                                           */
3637 /*  shelledealloc()   Deallocate space for a shell edge, marking it dead.    */
3638 /*                                                                           */
3639 /*****************************************************************************/
3640
3641 void shelledealloc(dyingshelle)
3642 shelle *dyingshelle;
3643 {
3644   /* Set shell edge's vertices to NULL.  This makes it possible to */
3645   /*   detect dead shells when traversing the list of all shells.  */
3646   dyingshelle[2] = (shelle) NULL;
3647   dyingshelle[3] = (shelle) NULL;
3648   pooldealloc(&shelles, (VOID *) dyingshelle);
3649 }
3650
3651 /*****************************************************************************/
3652 /*                                                                           */
3653 /*  shelletraverse()   Traverse the shell edges, skipping dead ones.         */
3654 /*                                                                           */
3655 /*****************************************************************************/
3656
3657 shelle *shelletraverse()
3658 {
3659   shelle *newshelle;
3660
3661   do {
3662     newshelle = (shelle *) traverse(&shelles);
3663     if (newshelle == (shelle *) NULL) {
3664       return (shelle *) NULL;
3665     }
3666   } while (newshelle[2] == (shelle) NULL);                /* Skip dead ones. */
3667   return newshelle;
3668 }
3669
3670 /*****************************************************************************/
3671 /*                                                                           */
3672 /*  pointdealloc()   Deallocate space for a point, marking it dead.          */
3673 /*                                                                           */
3674 /*****************************************************************************/
3675
3676 void pointdealloc(dyingpoint)
3677 point dyingpoint;
3678 {
3679   /* Mark the point as dead.  This makes it possible to detect dead points */
3680   /*   when traversing the list of all points.                             */
3681   setpointmark(dyingpoint, DEADPOINT);
3682   pooldealloc(&points, (VOID *) dyingpoint);
3683 }
3684
3685 /*****************************************************************************/
3686 /*                                                                           */
3687 /*  pointtraverse()   Traverse the points, skipping dead ones.               */
3688 /*                                                                           */
3689 /*****************************************************************************/
3690
3691 point pointtraverse()
3692 {
3693   point newpoint;
3694
3695   do {
3696     newpoint = (point) traverse(&points);
3697     if (newpoint == (point) NULL) {
3698       return (point) NULL;
3699     }
3700   } while (pointmark(newpoint) == DEADPOINT);             /* Skip dead ones. */
3701   return newpoint;
3702 }
3703
3704 /*****************************************************************************/
3705 /*                                                                           */
3706 /*  badsegmentdealloc()   Deallocate space for a bad segment, marking it     */
3707 /*                        dead.                                              */
3708 /*                                                                           */
3709 /*****************************************************************************/
3710
3711 #ifndef CDT_ONLY
3712
3713 void badsegmentdealloc(dyingseg)
3714 struct edge *dyingseg;
3715 {
3716   /* Set segment's orientation to -1.  This makes it possible to      */
3717   /*   detect dead segments when traversing the list of all segments. */
3718   dyingseg->shorient = -1;
3719   pooldealloc(&badsegments, (VOID *) dyingseg);
3720 }
3721
3722 #endif /* not CDT_ONLY */
3723
3724 /*****************************************************************************/
3725 /*                                                                           */
3726 /*  badsegmenttraverse()   Traverse the bad segments, skipping dead ones.    */
3727 /*                                                                           */
3728 /*****************************************************************************/
3729
3730 #ifndef CDT_ONLY
3731
3732 struct edge *badsegmenttraverse()
3733 {
3734   struct edge *newseg;
3735
3736   do {
3737     newseg = (struct edge *) traverse(&badsegments);
3738     if (newseg == (struct edge *) NULL) {
3739       return (struct edge *) NULL;
3740     }
3741   } while (newseg->shorient == -1);                       /* Skip dead ones. */
3742   return newseg;
3743 }
3744
3745 #endif /* not CDT_ONLY */
3746
3747 /*****************************************************************************/
3748 /*                                                                           */
3749 /*  getpoint()   Get a specific point, by number, from the list.             */
3750 /*                                                                           */
3751 /*  The first point is number 'firstnumber'.                                 */
3752 /*                                                                           */
3753 /*  Note that this takes O(n) time (with a small constant, if POINTPERBLOCK  */
3754 /*  is large).  I don't care to take the trouble to make it work in constant */
3755 /*  time.                                                                    */
3756 /*                                                                           */
3757 /*****************************************************************************/
3758
3759 point getpoint(number)
3760 int number;
3761 {
3762   VOID **getblock;
3763   point foundpoint;
3764   unsigned long alignptr;
3765   int current;
3766
3767   getblock = points.firstblock;
3768   current = firstnumber;
3769   /* Find the right block. */
3770   while (current + points.itemsperblock <= number) {
3771     getblock = (VOID **) *getblock;
3772     current += points.itemsperblock;
3773   }
3774   /* Now find the right point. */
3775   alignptr = (unsigned long) (getblock + 1);
3776   foundpoint = (point) (alignptr + (unsigned long) points.alignbytes
3777                         - (alignptr % (unsigned long) points.alignbytes));
3778   while (current < number) {
3779     foundpoint += points.itemwords;
3780     current++;
3781   }
3782   return foundpoint;
3783 }
3784
3785 /*****************************************************************************/
3786 /*                                                                           */
3787 /*  triangledeinit()   Free all remaining allocated memory.                  */
3788 /*                                                                           */
3789 /*****************************************************************************/
3790
3791 void triangledeinit()
3792 {
3793   pooldeinit(&triangles);
3794   free(dummytribase);
3795   if (useshelles) {
3796     pooldeinit(&shelles);
3797     free(dummyshbase);
3798   }
3799   pooldeinit(&points);
3800 #ifndef CDT_ONLY
3801   if (quality) {
3802     pooldeinit(&badsegments);
3803     if ((minangle > 0.0) || vararea || fixedarea) {
3804       pooldeinit(&badtriangles);
3805     }
3806   }
3807 #endif /* not CDT_ONLY */
3808 }
3809
3810 /**                                                                         **/
3811 /**                                                                         **/
3812 /********* Memory management routines end here                       *********/
3813
3814 /********* Constructors begin here                                   *********/
3815 /**                                                                         **/
3816 /**                                                                         **/
3817
3818 /*****************************************************************************/
3819 /*                                                                           */
3820 /*  maketriangle()   Create a new triangle with orientation zero.            */
3821 /*                                                                           */
3822 /*****************************************************************************/
3823
3824 void maketriangle(newtriedge)
3825 struct triedge *newtriedge;
3826 {
3827   int i;
3828
3829   newtriedge->tri = (triangle *) poolalloc(&triangles);
3830   /* Initialize the three adjoining triangles to be "outer space". */
3831   newtriedge->tri[0] = (triangle) dummytri;
3832   newtriedge->tri[1] = (triangle) dummytri;
3833   newtriedge->tri[2] = (triangle) dummytri;
3834   /* Three NULL vertex points. */
3835   newtriedge->tri[3] = (triangle) NULL;
3836   newtriedge->tri[4] = (triangle) NULL;
3837   newtriedge->tri[5] = (triangle) NULL;
3838   /* Initialize the three adjoining shell edges to be the omnipresent */
3839   /*   shell edge.                                                    */
3840   if (useshelles) {
3841     newtriedge->tri[6] = (triangle) dummysh;
3842     newtriedge->tri[7] = (triangle) dummysh;
3843     newtriedge->tri[8] = (triangle) dummysh;
3844   }
3845   for (i = 0; i < eextras; i++) {
3846     setelemattribute(*newtriedge, i, 0.0);
3847   }
3848   if (vararea) {
3849     setareabound(*newtriedge, -1.0);
3850   }
3851
3852   newtriedge->orient = 0;
3853 }
3854
3855 /*****************************************************************************/
3856 /*                                                                           */
3857 /*  makeshelle()   Create a new shell edge with orientation zero.            */
3858 /*                                                                           */
3859 /*****************************************************************************/
3860
3861 void makeshelle(newedge)
3862 struct edge *newedge;
3863 {
3864   newedge->sh = (shelle *) poolalloc(&shelles);
3865   /* Initialize the two adjoining shell edges to be the omnipresent */
3866   /*   shell edge.                                                  */
3867   newedge->sh[0] = (shelle) dummysh;
3868   newedge->sh[1] = (shelle) dummysh;
3869   /* Two NULL vertex points. */
3870   newedge->sh[2] = (shelle) NULL;
3871   newedge->sh[3] = (shelle) NULL;
3872   /* Initialize the two adjoining triangles to be "outer space". */
3873   newedge->sh[4] = (shelle) dummytri;
3874   newedge->sh[5] = (shelle) dummytri;
3875   /* Set the boundary marker to zero. */
3876   setmark(*newedge, 0);
3877
3878   newedge->shorient = 0;
3879 }
3880
3881 /**                                                                         **/
3882 /**                                                                         **/
3883 /********* Constructors end here                                     *********/
3884
3885 /********* Determinant evaluation routines begin here                *********/
3886 /**                                                                         **/
3887 /**                                                                         **/
3888
3889 /* The adaptive exact arithmetic geometric predicates implemented herein are */
3890 /*   described in detail in my Technical Report CMU-CS-96-140.  The complete */
3891 /*   reference is given in the header.                                       */
3892
3893 /* Which of the following two methods of finding the absolute values is      */
3894 /*   fastest is compiler-dependent.  A few compilers can inline and optimize */
3895 /*   the fabs() call; but most will incur the overhead of a function call,   */
3896 /*   which is disastrously slow.  A faster way on IEEE machines might be to  */
3897 /*   mask the appropriate bit, but that's difficult to do in C.              */
3898
3899 #define Absolute(a)  ((a) >= 0.0 ? (a) : -(a))
3900 /* #define Absolute(a)  fabs(a) */
3901
3902 /* Many of the operations are broken up into two pieces, a main part that    */
3903 /*   performs an approximate operation, and a "tail" that computes the       */
3904 /*   roundoff error of that operation.                                       */
3905 /*                                                                           */
3906 /* The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(),    */
3907 /*   Split(), and Two_Product() are all implemented as described in the      */
3908 /*   reference.  Each of these macros requires certain variables to be       */
3909 /*   defined in the calling routine.  The variables `bvirt', `c', `abig',    */
3910 /*   `_i', `_j', `_k', `_l', `_m', and `_n' are declared `INEXACT' because   */
3911 /*   they store the result of an operation that may incur roundoff error.    */
3912 /*   The input parameter `x' (or the highest numbered `x_' parameter) must   */
3913 /*   also be declared `INEXACT'.                                             */
3914
3915 #define Fast_Two_Sum_Tail(a, b, x, y) \
3916   bvirt = x - a; \
3917   y = b - bvirt
3918
3919 #define Fast_Two_Sum(a, b, x, y) \
3920   x = (REAL) (a + b); \
3921   Fast_Two_Sum_Tail(a, b, x, y)
3922
3923 #define Two_Sum_Tail(a, b, x, y) \
3924   bvirt = (REAL) (x - a); \
3925   avirt = x - bvirt; \
3926   bround = b - bvirt; \
3927   around = a - avirt; \
3928   y = around + bround
3929
3930 #define Two_Sum(a, b, x, y) \
3931   x = (REAL) (a + b); \
3932   Two_Sum_Tail(a, b, x, y)
3933
3934 #define Two_Diff_Tail(a, b, x, y) \
3935   bvirt = (REAL) (a - x); \
3936   avirt = x + bvirt; \
3937   bround = bvirt - b; \
3938   around = a - avirt; \
3939   y = around + bround
3940
3941 #define Two_Diff(a, b, x, y) \
3942   x = (REAL) (a - b); \
3943   Two_Diff_Tail(a, b, x, y)
3944
3945 #define Split(a, ahi, alo) \
3946   c = (REAL) (splitter * a); \
3947   abig = (REAL) (c - a); \
3948   ahi = c - abig; \
3949   alo = a - ahi
3950
3951 #define Two_Product_Tail(a, b, x, y) \
3952   Split(a, ahi, alo); \
3953   Split(b, bhi, blo); \
3954   err1 = x - (ahi * bhi); \
3955   err2 = err1 - (alo * bhi); \
3956   err3 = err2 - (ahi * blo); \
3957   y = (alo * blo) - err3
3958
3959 #define Two_Product(a, b, x, y) \
3960   x = (REAL) (a * b); \
3961   Two_Product_Tail(a, b, x, y)
3962
3963 /* Two_Product_Presplit() is Two_Product() where one of the inputs has       */
3964 /*   already been split.  Avoids redundant splitting.                        */
3965
3966 #define Two_Product_Presplit(a, b, bhi, blo, x, y) \
3967   x = (REAL) (a * b); \
3968   Split(a, ahi, alo); \
3969   err1 = x - (ahi * bhi); \
3970   err2 = err1 - (alo * bhi); \
3971   err3 = err2 - (ahi * blo); \
3972   y = (alo * blo) - err3
3973
3974 /* Square() can be done more quickly than Two_Product().                     */
3975
3976 #define Square_Tail(a, x, y) \
3977   Split(a, ahi, alo); \
3978   err1 = x - (ahi * ahi); \
3979   err3 = err1 - ((ahi + ahi) * alo); \
3980   y = (alo * alo) - err3
3981
3982 #define Square(a, x, y) \
3983   x = (REAL) (a * a); \
3984   Square_Tail(a, x, y)
3985
3986 /* Macros for summing expansions of various fixed lengths.  These are all    */
3987 /*   unrolled versions of Expansion_Sum().                                   */
3988
3989 #define Two_One_Sum(a1, a0, b, x2, x1, x0) \
3990   Two_Sum(a0, b , _i, x0); \
3991   Two_Sum(a1, _i, x2, x1)
3992
3993 #define Two_One_Diff(a1, a0, b, x2, x1, x0) \
3994   Two_Diff(a0, b , _i, x0); \
3995   Two_Sum( a1, _i, x2, x1)
3996
3997 #define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0) \
3998   Two_One_Sum(a1, a0, b0, _j, _0, x0); \
3999   Two_One_Sum(_j, _0, b1, x3, x2, x1)
4000
4001 #define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0) \
4002   Two_One_Diff(a1, a0, b0, _j, _0, x0); \
4003   Two_One_Diff(_j, _0, b1, x3, x2, x1)
4004
4005 /*****************************************************************************/
4006 /*                                                                           */
4007 /*  exactinit()   Initialize the variables used for exact arithmetic.        */
4008 /*                                                                           */
4009 /*  `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in   */
4010 /*  floating-point arithmetic.  `epsilon' bounds the relative roundoff       */
4011 /*  error.  It is used for floating-point error analysis.                    */
4012 /*                                                                           */
4013 /*  `splitter' is used to split floating-point numbers into two half-        */
4014 /*  length significands for exact multiplication.                            */
4015 /*                                                                           */
4016 /*  I imagine that a highly optimizing compiler might be too smart for its   */
4017 /*  own good, and somehow cause this routine to fail, if it pretends that    */
4018 /*  floating-point arithmetic is too much like real arithmetic.              */
4019 /*                                                                           */
4020 /*  Don't change this routine unless you fully understand it.                */
4021 /*                                                                           */
4022 /*****************************************************************************/
4023
4024 void exactinit()
4025 {
4026   REAL half;
4027   REAL check, lastcheck;
4028   int every_other;
4029
4030   every_other = 1;
4031   half = 0.5;
4032   epsilon = 1.0;
4033   splitter = 1.0;
4034   check = 1.0;
4035   /* Repeatedly divide `epsilon' by two until it is too small to add to      */
4036   /*   one without causing roundoff.  (Also check if the sum is equal to     */
4037   /*   the previous sum, for machines that round up instead of using exact   */
4038   /*   rounding.  Not that these routines will work on such machines anyway. */
4039   do {
4040     lastcheck = check;
4041     epsilon *= half;
4042     if (every_other) {
4043       splitter *= 2.0;
4044     }
4045     every_other = !every_other;
4046     check = 1.0 + epsilon;
4047   } while ((check != 1.0) && (check != lastcheck));
4048   splitter += 1.0;
4049   if (verbose > 1) {
4050     printf("Floating point roundoff is of magnitude %.17g\n", epsilon);
4051     printf("Floating point splitter is %.17g\n", splitter);
4052   }
4053   /* Error bounds for orientation and incircle tests. */
4054   resulterrbound = (3.0 + 8.0 * epsilon) * epsilon;
4055   ccwerrboundA = (3.0 + 16.0 * epsilon) * epsilon;
4056   ccwerrboundB = (2.0 + 12.0 * epsilon) * epsilon;
4057   ccwerrboundC = (9.0 + 64.0 * epsilon) * epsilon * epsilon;
4058   iccerrboundA = (10.0 + 96.0 * epsilon) * epsilon;
4059   iccerrboundB = (4.0 + 48.0 * epsilon) * epsilon;
4060   iccerrboundC = (44.0 + 576.0 * epsilon) * epsilon * epsilon;
4061 }
4062
4063 /*****************************************************************************/
4064 /*                                                                           */
4065 /*  fast_expansion_sum_zeroelim()   Sum two expansions, eliminating zero     */
4066 /*                                  components from the output expansion.    */
4067 /*                                                                           */
4068 /*  Sets h = e + f.  See my Robust Predicates paper for details.             */
4069 /*                                                                           */
4070 /*  If round-to-even is used (as with IEEE 754), maintains the strongly      */
4071 /*  nonoverlapping property.  (That is, if e is strongly nonoverlapping, h   */
4072 /*  will be also.)  Does NOT maintain the nonoverlapping or nonadjacent      */
4073 /*  properties.                                                              */
4074 /*                                                                           */
4075 /*****************************************************************************/
4076
4077 int fast_expansion_sum_zeroelim(elen, e, flen, f, h)  /* h cannot be e or f. */
4078 int elen;
4079 REAL *e;
4080 int flen;
4081 REAL *f;
4082 REAL *h;
4083 {
4084   REAL Q;
4085   INEXACT REAL Qnew;
4086   INEXACT REAL hh;
4087   INEXACT REAL bvirt;
4088   REAL avirt, bround, around;
4089   int eindex, findex, hindex;
4090   REAL enow, fnow;
4091
4092   enow = e[0];
4093   fnow = f[0];
4094   eindex = findex = 0;
4095   if ((fnow > enow) == (fnow > -enow)) {
4096     Q = enow;
4097     enow = e[++eindex];
4098   } else {
4099     Q = fnow;
4100     fnow = f[++findex];
4101   }
4102   hindex = 0;
4103   if ((eindex < elen) && (findex < flen)) {
4104     if ((fnow > enow) == (fnow > -enow)) {
4105       Fast_Two_Sum(enow, Q, Qnew, hh);
4106       enow = e[++eindex];
4107     } else {
4108       Fast_Two_Sum(fnow, Q, Qnew, hh);
4109       fnow = f[++findex];
4110     }
4111     Q = Qnew;
4112     if (hh != 0.0) {
4113       h[hindex++] = hh;
4114     }
4115     while ((eindex < elen) && (findex < flen)) {
4116       if ((fnow > enow) == (fnow > -enow)) {
4117         Two_Sum(Q, enow, Qnew, hh);
4118         enow = e[++eindex];
4119       } else {
4120         Two_Sum(Q, fnow, Qnew, hh);
4121         fnow = f[++findex];
4122       }
4123       Q = Qnew;
4124       if (hh != 0.0) {
4125         h[hindex++] = hh;
4126       }
4127     }
4128   }
4129   while (eindex < elen) {
4130     Two_Sum(Q, enow, Qnew, hh);
4131     enow = e[++eindex];
4132     Q = Qnew;
4133     if (hh != 0.0) {
4134       h[hindex++] = hh;
4135     }
4136   }
4137   while (findex < flen) {
4138     Two_Sum(Q, fnow, Qnew, hh);
4139     fnow = f[++findex];
4140     Q = Qnew;
4141     if (hh != 0.0) {
4142       h[hindex++] = hh;
4143     }
4144   }
4145   if ((Q != 0.0) || (hindex == 0)) {
4146     h[hindex++] = Q;
4147   }
4148   return hindex;
4149 }
4150
4151 /*****************************************************************************/
4152 /*                                                                           */
4153 /*  scale_expansion_zeroelim()   Multiply an expansion by a scalar,          */
4154 /*                               eliminating zero components from the        */
4155 /*                               output expansion.                           */
4156 /*                                                                           */
4157 /*  Sets h = be.  See my Robust Predicates paper for details.                */
4158 /*                                                                           */
4159 /*  Maintains the nonoverlapping property.  If round-to-even is used (as     */
4160 /*  with IEEE 754), maintains the strongly nonoverlapping and nonadjacent    */
4161 /*  properties as well.  (That is, if e has one of these properties, so      */
4162 /*  will h.)                                                                 */
4163 /*                                                                           */
4164 /*****************************************************************************/
4165
4166 int scale_expansion_zeroelim(elen, e, b, h)   /* e and h cannot be the same. */
4167 int elen;
4168 REAL *e;
4169 REAL b;
4170 REAL *h;
4171 {
4172   INEXACT REAL Q, sum;
4173   REAL hh;
4174   INEXACT REAL product1;
4175   REAL product0;
4176   int eindex, hindex;
4177   REAL enow;
4178   INEXACT REAL bvirt;
4179   REAL avirt, bround, around;
4180   INEXACT REAL c;
4181   INEXACT REAL abig;
4182   REAL ahi, alo, bhi, blo;
4183   REAL err1, err2, err3;
4184
4185   Split(b, bhi, blo);
4186   Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
4187   hindex = 0;
4188   if (hh != 0) {
4189     h[hindex++] = hh;
4190   }
4191   for (eindex = 1; eindex < elen; eindex++) {
4192     enow = e[eindex];
4193     Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
4194     Two_Sum(Q, product0, sum, hh);
4195     if (hh != 0) {
4196       h[hindex++] = hh;
4197     }
4198     Fast_Two_Sum(product1, sum, Q, hh);
4199     if (hh != 0) {
4200       h[hindex++] = hh;
4201     }
4202   }
4203   if ((Q != 0.0) || (hindex == 0)) {
4204     h[hindex++] = Q;
4205   }
4206   return hindex;
4207 }
4208
4209 /*****************************************************************************/
4210 /*                                                                           */
4211 /*  estimate()   Produce a one-word estimate of an expansion's value.        */
4212 /*                                                                           */
4213 /*  See my Robust Predicates paper for details.                              */
4214 /*                                                                           */
4215 /*****************************************************************************/
4216
4217 REAL estimate(elen, e)
4218 int elen;
4219 REAL *e;
4220 {
4221   REAL Q;
4222   int eindex;
4223
4224   Q = e[0];
4225   for (eindex = 1; eindex < elen; eindex++) {
4226     Q += e[eindex];
4227   }
4228   return Q;
4229 }
4230
4231 /*****************************************************************************/
4232 /*                                                                           */
4233 /*  counterclockwise()   Return a positive value if the points pa, pb, and   */
4234 /*                       pc occur in counterclockwise order; a negative      */
4235 /*                       value if they occur in clockwise order; and zero    */
4236 /*                       if they are collinear.  The result is also a rough  */
4237 /*                       approximation of twice the signed area of the       */
4238 /*                       triangle defined by the three points.               */
4239 /*                                                                           */
4240 /*  Uses exact arithmetic if necessary to ensure a correct answer.  The      */
4241 /*  result returned is the determinant of a matrix.  This determinant is     */
4242 /*  computed adaptively, in the sense that exact arithmetic is used only to  */
4243 /*  the degree it is needed to ensure that the returned value has the        */
4244 /*  correct sign.  Hence, this function is usually quite fast, but will run  */
4245 /*  more slowly when the input points are collinear or nearly so.            */
4246 /*                                                                           */
4247 /*  See my Robust Predicates paper for details.                              */
4248 /*                                                                           */
4249 /*****************************************************************************/
4250
4251 REAL counterclockwiseadapt(pa, pb, pc, detsum)
4252 point pa;
4253 point pb;
4254 point pc;
4255 REAL detsum;
4256 {
4257   INEXACT REAL acx, acy, bcx, bcy;
4258   REAL acxtail, acytail, bcxtail, bcytail;
4259   INEXACT REAL detleft, detright;
4260   REAL detlefttail, detrighttail;
4261   REAL det, errbound;
4262   REAL B[4], C1[8], C2[12], D[16];
4263   INEXACT REAL B3;
4264   int C1length, C2length, Dlength;
4265   REAL u[4];
4266   INEXACT REAL u3;
4267   INEXACT REAL s1, t1;
4268   REAL s0, t0;
4269
4270   INEXACT REAL bvirt;
4271   REAL avirt, bround, around;
4272   INEXACT REAL c;
4273   INEXACT REAL abig;
4274   REAL ahi, alo, bhi, blo;
4275   REAL err1, err2, err3;
4276   INEXACT REAL _i, _j;
4277   REAL _0;
4278
4279   acx = (REAL) (pa[0] - pc[0]);
4280   bcx = (REAL) (pb[0] - pc[0]);
4281   acy = (REAL) (pa[1] - pc[1]);
4282   bcy = (REAL) (pb[1] - pc[1]);
4283
4284   Two_Product(acx, bcy, detleft, detlefttail);
4285   Two_Product(acy, bcx, detright, detrighttail);
4286
4287   Two_Two_Diff(detleft, detlefttail, detright, detrighttail,
4288                B3, B[2], B[1], B[0]);
4289   B[3] = B3;
4290
4291   det = estimate(4, B);
4292   errbound = ccwerrboundB * detsum;
4293   if ((det >= errbound) || (-det >= errbound)) {
4294     return det;
4295   }
4296
4297   Two_Diff_Tail(pa[0], pc[0], acx, acxtail);
4298   Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);
4299   Two_Diff_Tail(pa[1], pc[1], acy, acytail);
4300   Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);
4301
4302   if ((acxtail == 0.0) && (acytail == 0.0)
4303       && (bcxtail == 0.0) && (bcytail == 0.0)) {
4304     return det;
4305   }
4306
4307   errbound = ccwerrboundC * detsum + resulterrbound * Absolute(det);
4308   det += (acx * bcytail + bcy * acxtail)
4309        - (acy * bcxtail + bcx * acytail);
4310   if ((det >= errbound) || (-det >= errbound)) {
4311     return det;
4312   }
4313
4314   Two_Product(acxtail, bcy, s1, s0);
4315   Two_Product(acytail, bcx, t1, t0);
4316   Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
4317   u[3] = u3;
4318   C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);
4319
4320   Two_Product(acx, bcytail, s1, s0);
4321   Two_Product(acy, bcxtail, t1, t0);
4322   Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
4323   u[3] = u3;
4324   C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);
4325
4326   Two_Product(acxtail, bcytail, s1, s0);
4327   Two_Product(acytail, bcxtail, t1, t0);
4328   Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
4329   u[3] = u3;
4330   Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);
4331
4332   return(D[Dlength - 1]);
4333 }
4334
4335 REAL counterclockwise(pa, pb, pc)
4336 point pa;
4337 point pb;
4338 point pc;
4339 {
4340   REAL detleft, detright, det;
4341   REAL detsum, errbound;
4342
4343   counterclockcount++;
4344
4345   detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);
4346   detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
4347   det = detleft - detright;
4348
4349   if (noexact) {
4350     return det;
4351   }
4352
4353   if (detleft > 0.0) {
4354     if (detright <= 0.0) {
4355       return det;
4356     } else {
4357       detsum = detleft + detright;
4358     }
4359   } else if (detleft < 0.0) {
4360     if (detright >= 0.0) {
4361       return det;
4362     } else {
4363       detsum = -detleft - detright;
4364     }
4365   } else {
4366     return det;
4367   }
4368
4369   errbound = ccwerrboundA * detsum;
4370   if ((det >= errbound) || (-det >= errbound)) {
4371     return det;
4372   }
4373
4374   return counterclockwiseadapt(pa, pb, pc, detsum);
4375 }
4376
4377 /*****************************************************************************/
4378 /*                                                                           */
4379 /*  incircle()   Return a positive value if the point pd lies inside the     */
4380 /*               circle passing through pa, pb, and pc; a negative value if  */
4381 /*               it lies outside; and zero if the four points are cocircular.*/
4382 /*               The points pa, pb, and pc must be in counterclockwise       */
4383 /*               order, or the sign of the result will be reversed.          */
4384 /*                                                                           */
4385 /*  Uses exact arithmetic if necessary to ensure a correct answer.  The      */
4386 /*  result returned is the determinant of a matrix.  This determinant is     */
4387 /*  computed adaptively, in the sense that exact arithmetic is used only to  */
4388 /*  the degree it is needed to ensure that the returned value has the        */
4389 /*  correct sign.  Hence, this function is usually quite fast, but will run  */
4390 /*  more slowly when the input points are cocircular or nearly so.           */
4391 /*                                                                           */
4392 /*  See my Robust Predicates paper for details.                              */
4393 /*                                                                           */
4394 /*****************************************************************************/
4395
4396 REAL incircleadapt(pa, pb, pc, pd, permanent)
4397 point pa;
4398 point pb;
4399 point pc;
4400 point pd;
4401 REAL permanent;
4402 {
4403   INEXACT REAL adx, bdx, cdx, ady, bdy, cdy;
4404   REAL det, errbound;
4405
4406   INEXACT REAL bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
4407   REAL bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
4408   REAL bc[4], ca[4], ab[4];
4409   INEXACT REAL bc3, ca3, ab3;
4410   REAL axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];
4411   int axbclen, axxbclen, aybclen, ayybclen, alen;
4412   REAL bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];
4413   int bxcalen, bxxcalen, bycalen, byycalen, blen;
4414   REAL cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];
4415   int cxablen, cxxablen, cyablen, cyyablen, clen;
4416   REAL abdet[64];
4417   int ablen;
4418   REAL fin1[1152], fin2[1152];
4419   REAL *finnow, *finother, *finswap;
4420   int finlength;
4421
4422   REAL adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;
4423   INEXACT REAL adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;
4424   REAL adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;
4425   REAL aa[4], bb[4], cc[4];
4426   INEXACT REAL aa3, bb3, cc3;
4427   INEXACT REAL ti1, tj1;
4428   REAL ti0, tj0;
4429   REAL u[4], v[4];
4430   INEXACT REAL u3, v3;
4431   REAL temp8[8], temp16a[16], temp16b[16], temp16c[16];
4432   REAL temp32a[32], temp32b[32], temp48[48], temp64[64];
4433   int temp8len, temp16alen, temp16blen, temp16clen;
4434   int temp32alen, temp32blen, temp48len, temp64len;
4435   REAL axtbb[8], axtcc[8], aytbb[8], aytcc[8];
4436   int axtbblen, axtcclen, aytbblen, aytcclen;
4437   REAL bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];
4438   int bxtaalen, bxtcclen, bytaalen, bytcclen;
4439   REAL cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];
4440   int cxtaalen, cxtbblen, cytaalen, cytbblen;
4441   REAL axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];
4442   int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;
4443   REAL axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16], cytabt[16];
4444   int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;
4445   REAL axtbctt[8], aytbctt[8], bxtcatt[8];
4446   REAL bytcatt[8], cxtabtt[8], cytabtt[8];
4447   int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;
4448   REAL abt[8], bct[8], cat[8];
4449   int abtlen, bctlen, catlen;
4450   REAL abtt[4], bctt[4], catt[4];
4451   int abttlen, bcttlen, cattlen;
4452   INEXACT REAL abtt3, bctt3, catt3;
4453   REAL negate;
4454
4455   INEXACT REAL bvirt;
4456   REAL avirt, bround, around;
4457   INEXACT REAL c;
4458   INEXACT REAL abig;
4459   REAL ahi, alo, bhi, blo;
4460   REAL err1, err2, err3;
4461   INEXACT REAL _i, _j;
4462   REAL _0;
4463
4464   adx = (REAL) (pa[0] - pd[0]);
4465   bdx = (REAL) (pb[0] - pd[0]);
4466   cdx = (REAL) (pc[0] - pd[0]);
4467   ady = (REAL) (pa[1] - pd[1]);
4468   bdy = (REAL) (pb[1] - pd[1]);
4469   cdy = (REAL) (pc[1] - pd[1]);
4470
4471   Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
4472   Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
4473   Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
4474   bc[3] = bc3;
4475   axbclen = scale_expansion_zeroelim(4, bc, adx, axbc);
4476   axxbclen = scale_expansion_zeroelim(axbclen, axbc, adx, axxbc);
4477   aybclen = scale_expansion_zeroelim(4, bc, ady, aybc);
4478   ayybclen = scale_expansion_zeroelim(aybclen, aybc, ady, ayybc);
4479   alen = fast_expansion_sum_zeroelim(axxbclen, axxbc, ayybclen, ayybc, adet);
4480
4481   Two_Product(cdx, ady, cdxady1, cdxady0);
4482   Two_Product(adx, cdy, adxcdy1, adxcdy0);
4483   Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
4484   ca[3] = ca3;
4485   bxcalen = scale_expansion_zeroelim(4, ca, bdx, bxca);
4486   bxxcalen = scale_expansion_zeroelim(bxcalen, bxca, bdx, bxxca);
4487   bycalen = scale_expansion_zeroelim(4, ca, bdy, byca);
4488   byycalen = scale_expansion_zeroelim(bycalen, byca, bdy, byyca);
4489   blen = fast_expansion_sum_zeroelim(bxxcalen, bxxca, byycalen, byyca, bdet);
4490
4491   Two_Product(adx, bdy, adxbdy1, adxbdy0);
4492   Two_Product(bdx, ady, bdxady1, bdxady0);
4493   Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
4494   ab[3] = ab3;
4495   cxablen = scale_expansion_zeroelim(4, ab, cdx, cxab);
4496   cxxablen = scale_expansion_zeroelim(cxablen, cxab, cdx, cxxab);
4497   cyablen = scale_expansion_zeroelim(4, ab, cdy, cyab);
4498   cyyablen = scale_expansion_zeroelim(cyablen, cyab, cdy, cyyab);
4499   clen = fast_expansion_sum_zeroelim(cxxablen, cxxab, cyyablen, cyyab, cdet);
4500
4501   ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);
4502   finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);
4503
4504   det = estimate(finlength, fin1);
4505   errbound = iccerrboundB * permanent;
4506   if ((det >= errbound) || (-det >= errbound)) {
4507     return det;
4508   }
4509
4510   Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
4511   Two_Diff_Tail(pa[1], pd[1], ady, adytail);
4512   Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
4513   Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
4514   Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
4515   Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
4516   if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0)
4517       && (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0)) {
4518     return det;
4519   }
4520
4521   errbound = iccerrboundC * permanent + resulterrbound * Absolute(det);
4522   det += ((adx * adx + ady * ady) * ((bdx * cdytail + cdy * bdxtail)
4523                                      - (bdy * cdxtail + cdx * bdytail))
4524           + 2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx))
4525        + ((bdx * bdx + bdy * bdy) * ((cdx * adytail + ady * cdxtail)
4526                                      - (cdy * adxtail + adx * cdytail))
4527           + 2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx))
4528        + ((cdx * cdx + cdy * cdy) * ((adx * bdytail + bdy * adxtail)
4529                                      - (ady * bdxtail + bdx * adytail))
4530           + 2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx));
4531   if ((det >= errbound) || (-det >= errbound)) {
4532     return det;
4533   }
4534
4535   finnow = fin1;
4536   finother = fin2;
4537
4538   if ((bdxtail != 0.0) || (bdytail != 0.0)
4539       || (cdxtail != 0.0) || (cdytail != 0.0)) {
4540     Square(adx, adxadx1, adxadx0);
4541     Square(ady, adyady1, adyady0);
4542     Two_Two_Sum(adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);
4543     aa[3] = aa3;
4544   }
4545   if ((cdxtail != 0.0) || (cdytail != 0.0)
4546       || (adxtail != 0.0) || (adytail != 0.0)) {
4547     Square(bdx, bdxbdx1, bdxbdx0);
4548     Square(bdy, bdybdy1, bdybdy0);
4549     Two_Two_Sum(bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);
4550     bb[3] = bb3;
4551   }
4552   if ((adxtail != 0.0) || (adytail != 0.0)
4553       || (bdxtail != 0.0) || (bdytail != 0.0)) {
4554     Square(cdx, cdxcdx1, cdxcdx0);
4555     Square(cdy, cdycdy1, cdycdy0);
4556     Two_Two_Sum(cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);
4557     cc[3] = cc3;
4558   }
4559
4560   if (adxtail != 0.0) {
4561     axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);
4562     temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx,
4563                                           temp16a);
4564
4565     axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);
4566     temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);
4567
4568     axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);
4569     temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);
4570
4571     temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4572                                             temp16blen, temp16b, temp32a);
4573     temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4574                                             temp32alen, temp32a, temp48);
4575     finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4576                                             temp48, finother);
4577     finswap = finnow; finnow = finother; finother = finswap;
4578   }
4579   if (adytail != 0.0) {
4580     aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);
4581     temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady,
4582                                           temp16a);
4583
4584     aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);
4585     temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);
4586
4587     aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);
4588     temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);
4589
4590     temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4591                                             temp16blen, temp16b, temp32a);
4592     temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4593                                             temp32alen, temp32a, temp48);
4594     finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4595                                             temp48, finother);
4596     finswap = finnow; finnow = finother; finother = finswap;
4597   }
4598   if (bdxtail != 0.0) {
4599     bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);
4600     temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx,
4601                                           temp16a);
4602
4603     bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);
4604     temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);
4605
4606     bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);
4607     temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);
4608
4609     temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4610                                             temp16blen, temp16b, temp32a);
4611     temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4612                                             temp32alen, temp32a, temp48);
4613     finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4614                                             temp48, finother);
4615     finswap = finnow; finnow = finother; finother = finswap;
4616   }
4617   if (bdytail != 0.0) {
4618     bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);
4619     temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy,
4620                                           temp16a);
4621
4622     bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);
4623     temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);
4624
4625     bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);
4626     temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);
4627
4628     temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4629                                             temp16blen, temp16b, temp32a);
4630     temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4631                                             temp32alen, temp32a, temp48);
4632     finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4633                                             temp48, finother);
4634     finswap = finnow; finnow = finother; finother = finswap;
4635   }
4636   if (cdxtail != 0.0) {
4637     cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);
4638     temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx,
4639                                           temp16a);
4640
4641     cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);
4642     temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);
4643
4644     cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);
4645     temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);
4646
4647     temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4648                                             temp16blen, temp16b, temp32a);
4649     temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4650                                             temp32alen, temp32a, temp48);
4651     finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4652                                             temp48, finother);
4653     finswap = finnow; finnow = finother; finother = finswap;
4654   }
4655   if (cdytail != 0.0) {
4656     cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);
4657     temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy,
4658                                           temp16a);
4659
4660     cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);
4661     temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);
4662
4663     cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);
4664     temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);
4665
4666     temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4667                                             temp16blen, temp16b, temp32a);
4668     temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4669                                             temp32alen, temp32a, temp48);
4670     finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4671                                             temp48, finother);
4672     finswap = finnow; finnow = finother; finother = finswap;
4673   }
4674
4675   if ((adxtail != 0.0) || (adytail != 0.0)) {
4676     if ((bdxtail != 0.0) || (bdytail != 0.0)
4677         || (cdxtail != 0.0) || (cdytail != 0.0)) {
4678       Two_Product(bdxtail, cdy, ti1, ti0);
4679       Two_Product(bdx, cdytail, tj1, tj0);
4680       Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
4681       u[3] = u3;
4682       negate = -bdy;
4683       Two_Product(cdxtail, negate, ti1, ti0);
4684       negate = -bdytail;
4685       Two_Product(cdx, negate, tj1, tj0);
4686       Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4687       v[3] = v3;
4688       bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);
4689
4690       Two_Product(bdxtail, cdytail, ti1, ti0);
4691       Two_Product(cdxtail, bdytail, tj1, tj0);
4692       Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);
4693       bctt[3] = bctt3;
4694       bcttlen = 4;
4695     } else {
4696       bct[0] = 0.0;
4697       bctlen = 1;
4698       bctt[0] = 0.0;
4699       bcttlen = 1;
4700     }
4701
4702     if (adxtail != 0.0) {
4703       temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, adxtail, temp16a);
4704       axtbctlen = scale_expansion_zeroelim(bctlen, bct, adxtail, axtbct);
4705       temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, 2.0 * adx,
4706                                             temp32a);
4707       temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4708                                               temp32alen, temp32a, temp48);
4709       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4710                                               temp48, finother);
4711       finswap = finnow; finnow = finother; finother = finswap;
4712       if (bdytail != 0.0) {
4713         temp8len = scale_expansion_zeroelim(4, cc, adxtail, temp8);
4714         temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
4715                                               temp16a);
4716         finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4717                                                 temp16a, finother);
4718         finswap = finnow; finnow = finother; finother = finswap;
4719       }
4720       if (cdytail != 0.0) {
4721         temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);
4722         temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
4723                                               temp16a);
4724         finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4725                                                 temp16a, finother);
4726         finswap = finnow; finnow = finother; finother = finswap;
4727       }
4728
4729       temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail,
4730                                             temp32a);
4731       axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);
4732       temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx,
4733                                             temp16a);
4734       temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail,
4735                                             temp16b);
4736       temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4737                                               temp16blen, temp16b, temp32b);
4738       temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4739                                               temp32blen, temp32b, temp64);
4740       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4741                                               temp64, finother);
4742       finswap = finnow; finnow = finother; finother = finswap;
4743     }
4744     if (adytail != 0.0) {
4745       temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, adytail, temp16a);
4746       aytbctlen = scale_expansion_zeroelim(bctlen, bct, adytail, aytbct);
4747       temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, 2.0 * ady,
4748                                             temp32a);
4749       temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4750                                               temp32alen, temp32a, temp48);
4751       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4752                                               temp48, finother);
4753       finswap = finnow; finnow = finother; finother = finswap;
4754
4755
4756       temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail,
4757                                             temp32a);
4758       aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);
4759       temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady,
4760                                             temp16a);
4761       temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail,
4762                                             temp16b);
4763       temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4764                                               temp16blen, temp16b, temp32b);
4765       temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4766                                               temp32blen, temp32b, temp64);
4767       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4768                                               temp64, finother);
4769       finswap = finnow; finnow = finother; finother = finswap;
4770     }
4771   }
4772   if ((bdxtail != 0.0) || (bdytail != 0.0)) {
4773     if ((cdxtail != 0.0) || (cdytail != 0.0)
4774         || (adxtail != 0.0) || (adytail != 0.0)) {
4775       Two_Product(cdxtail, ady, ti1, ti0);
4776       Two_Product(cdx, adytail, tj1, tj0);
4777       Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
4778       u[3] = u3;
4779       negate = -cdy;
4780       Two_Product(adxtail, negate, ti1, ti0);
4781       negate = -cdytail;
4782       Two_Product(adx, negate, tj1, tj0);
4783       Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4784       v[3] = v3;
4785       catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);
4786
4787       Two_Product(cdxtail, adytail, ti1, ti0);
4788       Two_Product(adxtail, cdytail, tj1, tj0);
4789       Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);
4790       catt[3] = catt3;
4791       cattlen = 4;
4792     } else {
4793       cat[0] = 0.0;
4794       catlen = 1;
4795       catt[0] = 0.0;
4796       cattlen = 1;
4797     }
4798
4799     if (bdxtail != 0.0) {
4800       temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, bdxtail, temp16a);
4801       bxtcatlen = scale_expansion_zeroelim(catlen, cat, bdxtail, bxtcat);
4802       temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, 2.0 * bdx,
4803                                             temp32a);
4804       temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4805                                               temp32alen, temp32a, temp48);
4806       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4807                                               temp48, finother);
4808       finswap = finnow; finnow = finother; finother = finswap;
4809       if (cdytail != 0.0) {
4810         temp8len = scale_expansion_zeroelim(4, aa, bdxtail, temp8);
4811         temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
4812                                               temp16a);
4813         finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4814                                                 temp16a, finother);
4815         finswap = finnow; finnow = finother; finother = finswap;
4816       }
4817       if (adytail != 0.0) {
4818         temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);
4819         temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
4820                                               temp16a);
4821         finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4822                                                 temp16a, finother);
4823         finswap = finnow; finnow = finother; finother = finswap;
4824       }
4825
4826       temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail,
4827                                             temp32a);
4828       bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);
4829       temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx,
4830                                             temp16a);
4831       temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail,
4832                                             temp16b);
4833       temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4834                                               temp16blen, temp16b, temp32b);
4835       temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4836                                               temp32blen, temp32b, temp64);
4837       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4838                                               temp64, finother);
4839       finswap = finnow; finnow = finother; finother = finswap;
4840     }
4841     if (bdytail != 0.0) {
4842       temp16alen = scale_expansion_zeroelim(bytcalen, bytca, bdytail, temp16a);
4843       bytcatlen = scale_expansion_zeroelim(catlen, cat, bdytail, bytcat);
4844       temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, 2.0 * bdy,
4845                                             temp32a);
4846       temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4847                                               temp32alen, temp32a, temp48);
4848       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4849                                               temp48, finother);
4850       finswap = finnow; finnow = finother; finother = finswap;
4851
4852
4853       temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail,
4854                                             temp32a);
4855       bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);
4856       temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy,
4857                                             temp16a);
4858       temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail,
4859                                             temp16b);
4860       temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4861                                               temp16blen, temp16b, temp32b);
4862       temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4863                                               temp32blen, temp32b, temp64);
4864       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4865                                               temp64, finother);
4866       finswap = finnow; finnow = finother; finother = finswap;
4867     }
4868   }
4869   if ((cdxtail != 0.0) || (cdytail != 0.0)) {
4870     if ((adxtail != 0.0) || (adytail != 0.0)
4871         || (bdxtail != 0.0) || (bdytail != 0.0)) {
4872       Two_Product(adxtail, bdy, ti1, ti0);
4873       Two_Product(adx, bdytail, tj1, tj0);
4874       Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
4875       u[3] = u3;
4876       negate = -ady;
4877       Two_Product(bdxtail, negate, ti1, ti0);
4878       negate = -adytail;
4879       Two_Product(bdx, negate, tj1, tj0);
4880       Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4881       v[3] = v3;
4882       abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);
4883
4884       Two_Product(adxtail, bdytail, ti1, ti0);
4885       Two_Product(bdxtail, adytail, tj1, tj0);
4886       Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);
4887       abtt[3] = abtt3;
4888       abttlen = 4;
4889     } else {
4890       abt[0] = 0.0;
4891       abtlen = 1;
4892       abtt[0] = 0.0;
4893       abttlen = 1;
4894     }
4895
4896     if (cdxtail != 0.0) {
4897       temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, cdxtail, temp16a);
4898       cxtabtlen = scale_expansion_zeroelim(abtlen, abt, cdxtail, cxtabt);
4899       temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, 2.0 * cdx,
4900                                             temp32a);
4901       temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4902                                               temp32alen, temp32a, temp48);
4903       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4904                                               temp48, finother);
4905       finswap = finnow; finnow = finother; finother = finswap;
4906       if (adytail != 0.0) {
4907         temp8len = scale_expansion_zeroelim(4, bb, cdxtail, temp8);
4908         temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
4909                                               temp16a);
4910         finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4911                                                 temp16a, finother);
4912         finswap = finnow; finnow = finother; finother = finswap;
4913       }
4914       if (bdytail != 0.0) {
4915         temp8len = scale_expansion_zeroelim(4, aa, -cdxtail, temp8);
4916         temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
4917                                               temp16a);
4918         finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4919                                                 temp16a, finother);
4920         finswap = finnow; finnow = finother; finother = finswap;
4921       }
4922
4923       temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, cdxtail,
4924                                             temp32a);
4925       cxtabttlen = scale_expansion_zeroelim(abttlen, abtt, cdxtail, cxtabtt);
4926       temp16alen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, 2.0 * cdx,
4927                                             temp16a);
4928       temp16blen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, cdxtail,
4929                                             temp16b);
4930       temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4931                                               temp16blen, temp16b, temp32b);
4932       temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4933                                               temp32blen, temp32b, temp64);
4934       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4935                                               temp64, finother);
4936       finswap = finnow; finnow = finother; finother = finswap;
4937     }
4938     if (cdytail != 0.0) {
4939       temp16alen = scale_expansion_zeroelim(cytablen, cytab, cdytail, temp16a);
4940       cytabtlen = scale_expansion_zeroelim(abtlen, abt, cdytail, cytabt);
4941       temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, 2.0 * cdy,
4942                                             temp32a);
4943       temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4944                                               temp32alen, temp32a, temp48);
4945       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4946                                               temp48, finother);
4947       finswap = finnow; finnow = finother; finother = finswap;
4948
4949
4950       temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, cdytail,
4951                                             temp32a);
4952       cytabttlen = scale_expansion_zeroelim(abttlen, abtt, cdytail, cytabtt);
4953       temp16alen = scale_expansion_zeroelim(cytabttlen, cytabtt, 2.0 * cdy,
4954                                             temp16a);
4955       temp16blen = scale_expansion_zeroelim(cytabttlen, cytabtt, cdytail,
4956                                             temp16b);
4957       temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4958                                               temp16blen, temp16b, temp32b);
4959       temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4960                                               temp32blen, temp32b, temp64);
4961       finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4962                                               temp64, finother);
4963       finswap = finnow; finnow = finother; finother = finswap;
4964     }
4965   }
4966
4967   return finnow[finlength - 1];
4968 }
4969
4970 REAL incircle(pa, pb, pc, pd)
4971 point pa;
4972 point pb;
4973 point pc;
4974 point pd;
4975 {
4976   REAL adx, bdx, cdx, ady, bdy, cdy;
4977   REAL bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
4978   REAL alift, blift, clift;
4979   REAL det;
4980   REAL permanent, errbound;
4981
4982   incirclecount++;
4983
4984   adx = pa[0] - pd[0];
4985   bdx = pb[0] - pd[0];
4986   cdx = pc[0] - pd[0];
4987   ady = pa[1] - pd[1];
4988   bdy = pb[1] - pd[1];
4989   cdy = pc[1] - pd[1];
4990
4991   bdxcdy = bdx * cdy;
4992   cdxbdy = cdx * bdy;
4993   alift = adx * adx + ady * ady;
4994
4995   cdxady = cdx * ady;
4996   adxcdy = adx * cdy;
4997   blift = bdx * bdx + bdy * bdy;
4998
4999   adxbdy = adx * bdy;
5000   bdxady = bdx * ady;
5001   clift = cdx * cdx + cdy * cdy;
5002
5003   det = alift * (bdxcdy - cdxbdy)
5004       + blift * (cdxady - adxcdy)
5005       + clift * (adxbdy - bdxady);
5006
5007   if (noexact) {
5008     return det;
5009   }
5010
5011   permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * alift
5012             + (Absolute(cdxady) + Absolute(adxcdy)) * blift
5013             + (Absolute(adxbdy) + Absolute(bdxady)) * clift;
5014   errbound = iccerrboundA * permanent;
5015   if ((det > errbound) || (-det > errbound)) {
5016     return det;
5017   }
5018
5019   return incircleadapt(pa, pb, pc, pd, permanent);
5020 }
5021
5022 /**                                                                         **/
5023 /**                                                                         **/
5024 /********* Determinant evaluation routines end here                  *********/
5025
5026 /*****************************************************************************/
5027 /*                                                                           */
5028 /*  triangleinit()   Initialize some variables.                              */
5029 /*                                                                           */
5030 /*****************************************************************************/
5031
5032 void triangleinit()
5033 {
5034   points.maxitems = triangles.maxitems = shelles.maxitems = viri.maxitems =
5035     badsegments.maxitems = badtriangles.maxitems = splaynodes.maxitems = 0l;
5036   points.itembytes = triangles.itembytes = shelles.itembytes = viri.itembytes =
5037     badsegments.itembytes = badtriangles.itembytes = splaynodes.itembytes = 0;
5038   recenttri.tri = (triangle *) NULL;    /* No triangle has been visited yet. */
5039   samples = 1;            /* Point location should take at least one sample. */
5040   checksegments = 0;      /* There are no segments in the triangulation yet. */
5041   incirclecount = counterclockcount = hyperbolacount = 0;
5042   circumcentercount = circletopcount = 0;
5043   randomseed = 1;
5044
5045   exactinit();                     /* Initialize exact arithmetic constants. */
5046 }
5047
5048 /*****************************************************************************/
5049 /*                                                                           */
5050 /*  randomnation()   Generate a random number between 0 and `choices' - 1.   */
5051 /*                                                                           */
5052 /*  This is a simple linear congruential random number generator.  Hence, it */
5053 /*  is a bad random number generator, but good enough for most randomized    */
5054 /*  geometric algorithms.                                                    */
5055 /*                                                                           */
5056 /*****************************************************************************/
5057
5058 unsigned long randomnation(choices)
5059 unsigned int choices;
5060 {
5061   randomseed = (randomseed * 1366l + 150889l) % 714025l;
5062   return randomseed / (714025l / choices + 1);
5063 }
5064
5065 /********* Mesh quality testing routines begin here                  *********/
5066 /**                                                                         **/
5067 /**                                                                         **/
5068
5069 /*****************************************************************************/
5070 /*                                                                           */
5071 /*  checkmesh()   Test the mesh for topological consistency.                 */
5072 /*                                                                           */
5073 /*****************************************************************************/
5074
5075 #ifndef REDUCED
5076
5077 void checkmesh()
5078 {
5079   struct triedge triangleloop;
5080   struct triedge oppotri, oppooppotri;
5081   point triorg, tridest, triapex;
5082   point oppoorg, oppodest;
5083   int horrors;
5084   int saveexact;
5085   triangle ptr;                         /* Temporary variable used by sym(). */
5086
5087   /* Temporarily turn on exact arithmetic if it's off. */
5088   saveexact = noexact;
5089   noexact = 0;
5090   if (!quiet) {
5091     printf("  Checking consistency of mesh...\n");
5092   }
5093   horrors = 0;
5094   /* Run through the list of triangles, checking each one. */
5095   traversalinit(&triangles);
5096   triangleloop.tri = triangletraverse();
5097   while (triangleloop.tri != (triangle *) NULL) {
5098     /* Check all three edges of the triangle. */
5099     for (triangleloop.orient = 0; triangleloop.orient < 3;
5100          triangleloop.orient++) {
5101       org(triangleloop, triorg);
5102       dest(triangleloop, tridest);
5103       if (triangleloop.orient == 0) {       /* Only test for inversion once. */
5104         /* Test if the triangle is flat or inverted. */
5105         apex(triangleloop, triapex);
5106         if (counterclockwise(triorg, tridest, triapex) <= 0.0) {
5107           printf("  !! !! Inverted ");
5108           printtriangle(&triangleloop);
5109           horrors++;
5110         }
5111       }
5112       /* Find the neighboring triangle on this edge. */
5113       sym(triangleloop, oppotri);
5114       if (oppotri.tri != dummytri) {
5115         /* Check that the triangle's neighbor knows it's a neighbor. */
5116         sym(oppotri, oppooppotri);
5117         if ((triangleloop.tri != oppooppotri.tri)
5118             || (triangleloop.orient != oppooppotri.orient)) {
5119           printf("  !! !! Asymmetric triangle-triangle bond:\n");
5120           if (triangleloop.tri == oppooppotri.tri) {
5121             printf("   (Right triangle, wrong orientation)\n");
5122           }
5123           printf("    First ");
5124           printtriangle(&triangleloop);
5125           printf("    Second (nonreciprocating) ");
5126           printtriangle(&oppotri);
5127           horrors++;
5128         }
5129         /* Check that both triangles agree on the identities */
5130         /*   of their shared vertices.                       */
5131         org(oppotri, oppoorg);
5132         dest(oppotri, oppodest);
5133         if ((triorg != oppodest) || (tridest != oppoorg)) {
5134           printf("  !! !! Mismatched edge coordinates between two triangles:\n"
5135                  );
5136           printf("    First mismatched ");
5137           printtriangle(&triangleloop);
5138           printf("    Second mismatched ");
5139           printtriangle(&oppotri);
5140           horrors++;
5141         }
5142       }
5143     }
5144     triangleloop.tri = triangletraverse();
5145   }
5146   if (horrors == 0) {
5147     if (!quiet) {
5148       printf("  In my studied opinion, the mesh appears to be consistent.\n");
5149     }
5150   } else if (horrors == 1) {
5151     printf("  !! !! !! !! Precisely one festering wound discovered.\n");
5152   } else {
5153     printf("  !! !! !! !! %d abominations witnessed.\n", horrors);
5154   }
5155   /* Restore the status of exact arithmetic. */
5156   noexact = saveexact;
5157 }
5158
5159 #endif /* not REDUCED */
5160
5161 /*****************************************************************************/
5162 /*                                                                           */
5163 /*  checkdelaunay()   Ensure that the mesh is (constrained) Delaunay.        */
5164 /*                                                                           */
5165 /*****************************************************************************/
5166
5167 #ifndef REDUCED
5168
5169 void checkdelaunay()
5170 {
5171   struct triedge triangleloop;
5172   struct triedge oppotri;
5173   struct edge opposhelle;
5174   point triorg, tridest, triapex;
5175   point oppoapex;
5176   int shouldbedelaunay;
5177   int horrors;
5178   int saveexact;
5179   triangle ptr;                         /* Temporary variable used by sym(). */
5180   shelle sptr;                      /* Temporary variable used by tspivot(). */
5181
5182   /* Temporarily turn on exact arithmetic if it's off. */
5183   saveexact = noexact;
5184   noexact = 0;
5185   if (!quiet) {
5186     printf("  Checking Delaunay property of mesh...\n");
5187   }
5188   horrors = 0;
5189   /* Run through the list of triangles, checking each one. */
5190   traversalinit(&triangles);
5191   triangleloop.tri = triangletraverse();
5192   while (triangleloop.tri != (triangle *) NULL) {
5193     /* Check all three edges of the triangle. */
5194     for (triangleloop.orient = 0; triangleloop.orient < 3;
5195          triangleloop.orient++) {
5196       org(triangleloop, triorg);
5197       dest(triangleloop, tridest);
5198       apex(triangleloop, triapex);
5199       sym(triangleloop, oppotri);
5200       apex(oppotri, oppoapex);
5201       /* Only test that the edge is locally Delaunay if there is an   */
5202       /*   adjoining triangle whose pointer is larger (to ensure that */
5203       /*   each pair isn't tested twice).                             */
5204       shouldbedelaunay = (oppotri.tri != dummytri)
5205             && (triapex != (point) NULL) && (oppoapex != (point) NULL)
5206             && (triangleloop.tri < oppotri.tri);
5207       if (checksegments && shouldbedelaunay) {
5208         /* If a shell edge separates the triangles, then the edge is */
5209         /*   constrained, so no local Delaunay test should be done.  */
5210         tspivot(triangleloop, opposhelle);
5211         if (opposhelle.sh != dummysh){
5212           shouldbedelaunay = 0;
5213         }
5214       }
5215       if (shouldbedelaunay) {
5216         if (incircle(triorg, tridest, triapex, oppoapex) > 0.0) {
5217           printf("  !! !! Non-Delaunay pair of triangles:\n");
5218           printf("    First non-Delaunay ");
5219           printtriangle(&triangleloop);
5220           printf("    Second non-Delaunay ");
5221           printtriangle(&oppotri);
5222           horrors++;
5223         }
5224       }
5225     }
5226     triangleloop.tri = triangletraverse();
5227   }
5228   if (horrors == 0) {
5229     if (!quiet) {
5230       printf(
5231   "  By virtue of my perceptive intelligence, I declare the mesh Delaunay.\n");
5232     }
5233   } else if (horrors == 1) {
5234     printf(
5235          "  !! !! !! !! Precisely one terrifying transgression identified.\n");
5236   } else {
5237     printf("  !! !! !! !! %d obscenities viewed with horror.\n", horrors);
5238   }
5239   /* Restore the status of exact arithmetic. */
5240   noexact = saveexact;
5241 }
5242
5243 #endif /* not REDUCED */
5244
5245 /*****************************************************************************/
5246 /*                                                                           */
5247 /*  enqueuebadtri()   Add a bad triangle to the end of a queue.              */
5248 /*                                                                           */
5249 /*  The queue is actually a set of 64 queues.  I use multiple queues to give */
5250 /*  priority to smaller angles.  I originally implemented a heap, but the    */
5251 /*  queues are (to my surprise) much faster.                                 */
5252 /*                                                                           */
5253 /*****************************************************************************/
5254
5255 #ifndef CDT_ONLY
5256
5257 void enqueuebadtri(instri, angle, insapex, insorg, insdest)
5258 struct triedge *instri;
5259 REAL angle;
5260 point insapex;
5261 point insorg;
5262 point insdest;
5263 {
5264   struct badface *newface;
5265   int queuenumber;
5266
5267   if (verbose > 2) {
5268     printf("  Queueing bad triangle:\n");
5269     printf("    (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", insorg[0],
5270            insorg[1], insdest[0], insdest[1], insapex[0], insapex[1]);
5271   }
5272   /* Allocate space for the bad triangle. */
5273   newface = (struct badface *) poolalloc(&badtriangles);
5274   triedgecopy(*instri, newface->badfacetri);
5275   newface->key = angle;
5276   newface->faceapex = insapex;
5277   newface->faceorg = insorg;
5278   newface->facedest = insdest;
5279   newface->nextface = (struct badface *) NULL;
5280   /* Determine the appropriate queue to put the bad triangle into. */
5281   if (angle > 0.6) {
5282     queuenumber = (int) (160.0 * (angle - 0.6));
5283     if (queuenumber > 63) {
5284       queuenumber = 63;
5285     }
5286   } else {
5287     /* It's not a bad angle; put the triangle in the lowest-priority queue. */
5288     queuenumber = 0;
5289   }
5290   /* Add the triangle to the end of a queue. */
5291   *queuetail[queuenumber] = newface;
5292   /* Maintain a pointer to the NULL pointer at the end of the queue. */
5293   queuetail[queuenumber] = &newface->nextface;
5294 }
5295
5296 #endif /* not CDT_ONLY */
5297
5298 /*****************************************************************************/
5299 /*                                                                           */
5300 /*  dequeuebadtri()   Remove a triangle from the front of the queue.         */
5301 /*                                                                           */
5302 /*****************************************************************************/
5303
5304 #ifndef CDT_ONLY
5305
5306 struct badface *dequeuebadtri()
5307 {
5308   struct badface *result;
5309   int queuenumber;
5310
5311   /* Look for a nonempty queue. */
5312   for (queuenumber = 63; queuenumber >= 0; queuenumber--) {
5313     result = queuefront[queuenumber];
5314     if (result != (struct badface *) NULL) {
5315       /* Remove the triangle from the queue. */
5316       queuefront[queuenumber] = result->nextface;
5317       /* Maintain a pointer to the NULL pointer at the end of the queue. */
5318       if (queuefront[queuenumber] == (struct badface *) NULL) {
5319         queuetail[queuenumber] = &queuefront[queuenumber];
5320       }
5321       return result;
5322     }
5323   }
5324   return (struct badface *) NULL;
5325 }
5326
5327 #endif /* not CDT_ONLY */
5328
5329 /*****************************************************************************/
5330 /*                                                                           */
5331 /*  checkedge4encroach()   Check a segment to see if it is encroached; add   */
5332 /*                         it to the list if it is.                          */
5333 /*                                                                           */
5334 /*  An encroached segment is an unflippable edge that has a point in its     */
5335 /*  diametral circle (that is, it faces an angle greater than 90 degrees).   */
5336 /*  This definition is due to Ruppert.                                       */
5337 /*                                                                           */
5338 /*  Returns a nonzero value if the edge is encroached.                       */
5339 /*                                                                           */
5340 /*****************************************************************************/
5341
5342 #ifndef CDT_ONLY
5343
5344 int checkedge4encroach(testedge)
5345 struct edge *testedge;
5346 {
5347   struct triedge neighbortri;
5348   struct edge testsym;
5349   struct edge *badedge;
5350   int addtolist;
5351   int sides;
5352   point eorg, edest, eapex;
5353   triangle ptr;                     /* Temporary variable used by stpivot(). */
5354
5355   addtolist = 0;
5356   sides = 0;
5357
5358   sorg(*testedge, eorg);
5359   sdest(*testedge, edest);
5360   /* Check one neighbor of the shell edge. */
5361   stpivot(*testedge, neighbortri);
5362   /* Does the neighbor exist, or is this a boundary edge? */
5363   if (neighbortri.tri != dummytri) {
5364     sides++;
5365     /* Find a vertex opposite this edge. */
5366     apex(neighbortri, eapex);
5367     /* Check whether the vertex is inside the diametral circle of the  */
5368     /*   shell edge.  Pythagoras' Theorem is used to check whether the */
5369     /*   angle at the vertex is greater than 90 degrees.               */
5370     if (eapex[0] * (eorg[0] + edest[0]) + eapex[1] * (eorg[1] + edest[1]) >
5371         eapex[0] * eapex[0] + eorg[0] * edest[0] +
5372         eapex[1] * eapex[1] + eorg[1] * edest[1]) {
5373       addtolist = 1;
5374     }
5375   }
5376   /* Check the other neighbor of the shell edge. */
5377   ssym(*testedge, testsym);
5378   stpivot(testsym, neighbortri);
5379   /* Does the neighbor exist, or is this a boundary edge? */
5380   if (neighbortri.tri != dummytri) {
5381     sides++;
5382     /* Find the other vertex opposite this edge. */
5383     apex(neighbortri, eapex);
5384     /* Check whether the vertex is inside the diametral circle of the  */
5385     /*   shell edge.  Pythagoras' Theorem is used to check whether the */
5386     /*   angle at the vertex is greater than 90 degrees.               */
5387     if (eapex[0] * (eorg[0] + edest[0]) +
5388         eapex[1] * (eorg[1] + edest[1]) >
5389         eapex[0] * eapex[0] + eorg[0] * edest[0] +
5390         eapex[1] * eapex[1] + eorg[1] * edest[1]) {
5391       addtolist += 2;
5392     }
5393   }
5394
5395   if (addtolist && (!nobisect || ((nobisect == 1) && (sides == 2)))) {
5396     if (verbose > 2) {
5397       printf("  Queueing encroached segment (%.12g, %.12g) (%.12g, %.12g).\n",
5398              eorg[0], eorg[1], edest[0], edest[1]);
5399     }
5400     /* Add the shell edge to the list of encroached segments. */
5401     /*   Be sure to get the orientation right.                */
5402     badedge = (struct edge *) poolalloc(&badsegments);
5403     if (addtolist == 1) {
5404       shellecopy(*testedge, *badedge);
5405     } else {
5406       shellecopy(testsym, *badedge);
5407     }
5408   }
5409   return addtolist;
5410 }
5411
5412 #endif /* not CDT_ONLY */
5413
5414 /*****************************************************************************/
5415 /*                                                                           */
5416 /*  testtriangle()   Test a face for quality measures.                       */
5417 /*                                                                           */
5418 /*  Tests a triangle to see if it satisfies the minimum angle condition and  */
5419 /*  the maximum area condition.  Triangles that aren't up to spec are added  */
5420 /*  to the bad triangle queue.                                               */
5421 /*                                                                           */
5422 /*****************************************************************************/
5423
5424 #ifndef CDT_ONLY
5425
5426 void testtriangle(testtri)
5427 struct triedge *testtri;
5428 {
5429   struct triedge sametesttri;
5430   struct edge edge1, edge2;
5431   point torg, tdest, tapex;
5432   point anglevertex;
5433   REAL dxod, dyod, dxda, dyda, dxao, dyao;
5434   REAL dxod2, dyod2, dxda2, dyda2, dxao2, dyao2;
5435   REAL apexlen, orglen, destlen;
5436   REAL angle;
5437   REAL area;
5438   shelle sptr;                      /* Temporary variable used by tspivot(). */
5439
5440   org(*testtri, torg);
5441   dest(*testtri, tdest);
5442   apex(*testtri, tapex);
5443   dxod = torg[0] - tdest[0];
5444   dyod = torg[1] - tdest[1];
5445   dxda = tdest[0] - tapex[0];
5446   dyda = tdest[1] - tapex[1];
5447   dxao = tapex[0] - torg[0];
5448   dyao = tapex[1] - torg[1];
5449   dxod2 = dxod * dxod;
5450   dyod2 = dyod * dyod;
5451   dxda2 = dxda * dxda;
5452   dyda2 = dyda * dyda;
5453   dxao2 = dxao * dxao;
5454   dyao2 = dyao * dyao;
5455   /* Find the lengths of the triangle's three edges. */
5456   apexlen = dxod2 + dyod2;
5457   orglen = dxda2 + dyda2;
5458   destlen = dxao2 + dyao2;
5459   if ((apexlen < orglen) && (apexlen < destlen)) {
5460     /* The edge opposite the apex is shortest. */
5461     /* Find the square of the cosine of the angle at the apex. */
5462     angle = dxda * dxao + dyda * dyao;
5463     angle = angle * angle / (orglen * destlen);
5464     anglevertex = tapex;
5465     lnext(*testtri, sametesttri);
5466     tspivot(sametesttri, edge1);
5467     lnextself(sametesttri);
5468     tspivot(sametesttri, edge2);
5469   } else if (orglen < destlen) {
5470     /* The edge opposite the origin is shortest. */
5471     /* Find the square of the cosine of the angle at the origin. */
5472     angle = dxod * dxao + dyod * dyao;
5473     angle = angle * angle / (apexlen * destlen);
5474     anglevertex = torg;
5475     tspivot(*testtri, edge1);
5476     lprev(*testtri, sametesttri);
5477     tspivot(sametesttri, edge2);
5478   } else {
5479     /* The edge opposite the destination is shortest. */
5480     /* Find the square of the cosine of the angle at the destination. */
5481     angle = dxod * dxda + dyod * dyda;
5482     angle = angle * angle / (apexlen * orglen);
5483     anglevertex = tdest;
5484     tspivot(*testtri, edge1);
5485     lnext(*testtri, sametesttri);
5486     tspivot(sametesttri, edge2);
5487   }
5488   /* Check if both edges that form the angle are segments. */
5489   if ((edge1.sh != dummysh) && (edge2.sh != dummysh)) {
5490     /* The angle is a segment intersection. */
5491     if ((angle > 0.9924) && !quiet) {                  /* Roughly 5 degrees. */
5492       if (angle > 1.0) {
5493         /* Beware of a floating exception in acos(). */
5494         angle = 1.0;
5495       }
5496       /* Find the actual angle in degrees, for printing. */
5497       angle = acos(sqrt(angle)) * (180.0 / PI);
5498       printf(
5499       "Warning:  Small angle (%.4g degrees) between segments at point\n",
5500              angle);
5501       printf("  (%.12g, %.12g)\n", anglevertex[0], anglevertex[1]);
5502     }
5503     /* Don't add this bad triangle to the list; there's nothing that */
5504     /*   can be done about a small angle between two segments.       */
5505     angle = 0.0;
5506   }
5507   /* Check whether the angle is smaller than permitted. */
5508   if (angle > goodangle) {
5509     /* Add this triangle to the list of bad triangles. */
5510     enqueuebadtri(testtri, angle, tapex, torg, tdest);
5511     return;
5512   }
5513   if (vararea || fixedarea) {
5514     /* Check whether the area is larger than permitted. */
5515     area = 0.5 * (dxod * dyda - dyod * dxda);
5516     if (fixedarea && (area > maxarea)) {
5517       /* Add this triangle to the list of bad triangles. */
5518       enqueuebadtri(testtri, angle, tapex, torg, tdest);
5519     } else if (vararea) {
5520       /* Nonpositive area constraints are treated as unconstrained. */
5521       if ((area > areabound(*testtri)) && (areabound(*testtri) > 0.0)) {
5522         /* Add this triangle to the list of bad triangles. */
5523         enqueuebadtri(testtri, angle, tapex, torg, tdest);
5524       }
5525     }
5526   }
5527 }
5528
5529 #endif /* not CDT_ONLY */
5530
5531 /**                                                                         **/
5532 /**                                                                         **/
5533 /********* Mesh quality testing routines end here                    *********/
5534
5535 /********* Point location routines begin here                        *********/
5536 /**                                                                         **/
5537 /**                                                                         **/
5538
5539 /*****************************************************************************/
5540 /*                                                                           */
5541 /*  makepointmap()   Construct a mapping from points to triangles to improve  */
5542 /*                  the speed of point location for segment insertion.       */
5543 /*                                                                           */
5544 /*  Traverses all the triangles, and provides each corner of each triangle   */
5545 /*  with a pointer to that triangle.  Of course, pointers will be            */
5546 /*  overwritten by other pointers because (almost) each point is a corner    */
5547 /*  of several triangles, but in the end every point will point to some      */
5548 /*  triangle that contains it.                                               */
5549 /*                                                                           */
5550 /*****************************************************************************/
5551
5552 void makepointmap()
5553 {
5554   struct triedge triangleloop;
5555   point triorg;
5556
5557   if (verbose) {
5558     printf("    Constructing mapping from points to triangles.\n");
5559   }
5560   traversalinit(&triangles);
5561   triangleloop.tri = triangletraverse();
5562   while (triangleloop.tri != (triangle *) NULL) {
5563     /* Check all three points of the triangle. */
5564     for (triangleloop.orient = 0; triangleloop.orient < 3;
5565          triangleloop.orient++) {
5566       org(triangleloop, triorg);
5567       setpoint2tri(triorg, encode(triangleloop));
5568     }
5569     triangleloop.tri = triangletraverse();
5570   }
5571 }
5572
5573 /*****************************************************************************/
5574 /*                                                                           */
5575 /*  preciselocate()   Find a triangle or edge containing a given point.      */
5576 /*                                                                           */
5577 /*  Begins its search from `searchtri'.  It is important that `searchtri'    */
5578 /*  be a handle with the property that `searchpoint' is strictly to the left */
5579 /*  of the edge denoted by `searchtri', or is collinear with that edge and   */
5580 /*  does not intersect that edge.  (In particular, `searchpoint' should not  */
5581 /*  be the origin or destination of that edge.)                              */
5582 /*                                                                           */
5583 /*  These conditions are imposed because preciselocate() is normally used in */
5584 /*  one of two situations:                                                   */
5585 /*                                                                           */
5586 /*  (1)  To try to find the location to insert a new point.  Normally, we    */
5587 /*       know an edge that the point is strictly to the left of.  In the     */
5588 /*       incremental Delaunay algorithm, that edge is a bounding box edge.   */
5589 /*       In Ruppert's Delaunay refinement algorithm for quality meshing,     */
5590 /*       that edge is the shortest edge of the triangle whose circumcenter   */
5591 /*       is being inserted.                                                  */
5592 /*                                                                           */
5593 /*  (2)  To try to find an existing point.  In this case, any edge on the    */
5594 /*       convex hull is a good starting edge.  The possibility that the      */
5595 /*       vertex one seeks is an endpoint of the starting edge must be        */
5596 /*       screened out before preciselocate() is called.                      */
5597 /*                                                                           */
5598 /*  On completion, `searchtri' is a triangle that contains `searchpoint'.    */
5599 /*                                                                           */
5600 /*  This implementation differs from that given by Guibas and Stolfi.  It    */
5601 /*  walks from triangle to triangle, crossing an edge only if `searchpoint'  */
5602 /*  is on the other side of the line containing that edge.  After entering   */
5603 /*  a triangle, there are two edges by which one can leave that triangle.    */
5604 /*  If both edges are valid (`searchpoint' is on the other side of both      */
5605 /*  edges), one of the two is chosen by drawing a line perpendicular to      */
5606 /*  the entry edge (whose endpoints are `forg' and `fdest') passing through  */
5607 /*  `fapex'.  Depending on which side of this perpendicular `searchpoint'    */
5608 /*  falls on, an exit edge is chosen.                                        */
5609 /*                                                                           */
5610 /*  This implementation is empirically faster than the Guibas and Stolfi     */
5611 /*  point location routine (which I originally used), which tends to spiral  */
5612 /*  in toward its target.                                                    */
5613 /*                                                                           */
5614 /*  Returns ONVERTEX if the point lies on an existing vertex.  `searchtri'   */
5615 /*  is a handle whose origin is the existing vertex.                         */
5616 /*                                                                           */
5617 /*  Returns ONEDGE if the point lies on a mesh edge.  `searchtri' is a       */
5618 /*  handle whose primary edge is the edge on which the point lies.           */
5619 /*                                                                           */
5620 /*  Returns INTRIANGLE if the point lies strictly within a triangle.         */
5621 /*  `searchtri' is a handle on the triangle that contains the point.         */
5622 /*                                                                           */
5623 /*  Returns OUTSIDE if the point lies outside the mesh.  `searchtri' is a    */
5624 /*  handle whose primary edge the point is to the right of.  This might      */
5625 /*  occur when the circumcenter of a triangle falls just slightly outside    */
5626 /*  the mesh due to floating-point roundoff error.  It also occurs when      */
5627 /*  seeking a hole or region point that a foolish user has placed outside    */
5628 /*  the mesh.                                                                */
5629 /*                                                                           */
5630 /*  WARNING:  This routine is designed for convex triangulations, and will   */
5631 /*  not generally work after the holes and concavities have been carved.     */
5632 /*  However, it can still be used to find the circumcenter of a triangle, as */
5633 /*  long as the search is begun from the triangle in question.               */
5634 /*                                                                           */
5635 /*****************************************************************************/
5636
5637 enum locateresult preciselocate(searchpoint, searchtri)
5638 point searchpoint;
5639 struct triedge *searchtri;
5640 {
5641   struct triedge backtracktri;
5642   point forg, fdest, fapex;
5643   point swappoint;
5644   REAL orgorient, destorient;
5645   int moveleft;
5646   triangle ptr;                         /* Temporary variable used by sym(). */
5647
5648   if (verbose > 2) {
5649     printf("  Searching for point (%.12g, %.12g).\n",
5650            searchpoint[0], searchpoint[1]);
5651   }
5652   /* Where are we? */
5653   org(*searchtri, forg);
5654   dest(*searchtri, fdest);
5655   apex(*searchtri, fapex);
5656   while (1) {
5657     if (verbose > 2) {
5658       printf("    At (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
5659              forg[0], forg[1], fdest[0], fdest[1], fapex[0], fapex[1]);
5660     }
5661     /* Check whether the apex is the point we seek. */
5662     if ((fapex[0] == searchpoint[0]) && (fapex[1] == searchpoint[1])) {
5663       lprevself(*searchtri);
5664       return ONVERTEX;
5665     }
5666     /* Does the point lie on the other side of the line defined by the */
5667     /*   triangle edge opposite the triangle's destination?            */
5668     destorient = counterclockwise(forg, fapex, searchpoint);
5669     /* Does the point lie on the other side of the line defined by the */
5670     /*   triangle edge opposite the triangle's origin?                 */
5671     orgorient = counterclockwise(fapex, fdest, searchpoint);
5672     if (destorient > 0.0) {
5673       if (orgorient > 0.0) {
5674         /* Move left if the inner product of (fapex - searchpoint) and  */
5675         /*   (fdest - forg) is positive.  This is equivalent to drawing */
5676         /*   a line perpendicular to the line (forg, fdest) passing     */
5677         /*   through `fapex', and determining which side of this line   */
5678         /*   `searchpoint' falls on.                                    */
5679         moveleft = (fapex[0] - searchpoint[0]) * (fdest[0] - forg[0]) +
5680                    (fapex[1] - searchpoint[1]) * (fdest[1] - forg[1]) > 0.0;
5681       } else {
5682         moveleft = 1;
5683       }
5684     } else {
5685       if (orgorient > 0.0) {
5686         moveleft = 0;
5687       } else {
5688         /* The point we seek must be on the boundary of or inside this */
5689         /*   triangle.                                                 */
5690         if (destorient == 0.0) {
5691           lprevself(*searchtri);
5692           return ONEDGE;
5693         }
5694         if (orgorient == 0.0) {
5695           lnextself(*searchtri);
5696           return ONEDGE;
5697         }
5698         return INTRIANGLE;
5699       }
5700     }
5701
5702     /* Move to another triangle.  Leave a trace `backtracktri' in case */
5703     /*   floating-point roundoff or some such bogey causes us to walk  */
5704     /*   off a boundary of the triangulation.  We can just bounce off  */
5705     /*   the boundary as if it were an elastic band.                   */
5706     if (moveleft) {
5707       lprev(*searchtri, backtracktri);
5708       fdest = fapex;
5709     } else {
5710       lnext(*searchtri, backtracktri);
5711       forg = fapex;
5712     }
5713     sym(backtracktri, *searchtri);
5714
5715     /* Check for walking off the edge. */
5716     if (searchtri->tri == dummytri) {
5717       /* Turn around. */
5718       triedgecopy(backtracktri, *searchtri);
5719       swappoint = forg;
5720       forg = fdest;
5721       fdest = swappoint;
5722       apex(*searchtri, fapex);
5723       /* Check if the point really is beyond the triangulation boundary. */
5724       destorient = counterclockwise(forg, fapex, searchpoint);
5725       orgorient = counterclockwise(fapex, fdest, searchpoint);
5726       if ((orgorient < 0.0) && (destorient < 0.0)) {
5727         return OUTSIDE;
5728       }
5729     } else {
5730       apex(*searchtri, fapex);
5731     }
5732   }
5733 }
5734
5735 /*****************************************************************************/
5736 /*                                                                           */
5737 /*  locate()   Find a triangle or edge containing a given point.             */
5738 /*                                                                           */
5739 /*  Searching begins from one of:  the input `searchtri', a recently         */
5740 /*  encountered triangle `recenttri', or from a triangle chosen from a       */
5741 /*  random sample.  The choice is made by determining which triangle's       */
5742 /*  origin is closest to the point we are searcing for.  Normally,           */
5743 /*  `searchtri' should be a handle on the convex hull of the triangulation.  */
5744 /*                                                                           */
5745 /*  Details on the random sampling method can be found in the Mucke, Saias,  */
5746 /*  and Zhu paper cited in the header of this code.                          */
5747 /*                                                                           */
5748 /*  On completion, `searchtri' is a triangle that contains `searchpoint'.    */
5749 /*                                                                           */
5750 /*  Returns ONVERTEX if the point lies on an existing vertex.  `searchtri'   */
5751 /*  is a handle whose origin is the existing vertex.                         */
5752 /*                                                                           */
5753 /*  Returns ONEDGE if the point lies on a mesh edge.  `searchtri' is a       */
5754 /*  handle whose primary edge is the edge on which the point lies.           */
5755 /*                                                                           */
5756 /*  Returns INTRIANGLE if the point lies strictly within a triangle.         */
5757 /*  `searchtri' is a handle on the triangle that contains the point.         */
5758 /*                                                                           */
5759 /*  Returns OUTSIDE if the point lies outside the mesh.  `searchtri' is a    */
5760 /*  handle whose primary edge the point is to the right of.  This might      */
5761 /*  occur when the circumcenter of a triangle falls just slightly outside    */
5762 /*  the mesh due to floating-point roundoff error.  It also occurs when      */
5763 /*  seeking a hole or region point that a foolish user has placed outside    */
5764 /*  the mesh.                                                                */
5765 /*                                                                           */
5766 /*  WARNING:  This routine is designed for convex triangulations, and will   */
5767 /*  not generally work after the holes and concavities have been carved.     */
5768 /*                                                                           */
5769 /*****************************************************************************/
5770
5771 enum locateresult locate(searchpoint, searchtri)
5772 point searchpoint;
5773 struct triedge *searchtri;
5774 {
5775   VOID **sampleblock;
5776   triangle *firsttri;
5777   struct triedge sampletri;
5778   point torg, tdest;
5779   unsigned long alignptr;
5780   REAL searchdist, dist;
5781   REAL ahead;
5782   long sampleblocks, samplesperblock, samplenum;
5783   long triblocks;
5784   long i, j;
5785   triangle ptr;                         /* Temporary variable used by sym(). */
5786
5787   if (verbose > 2) {
5788     printf("  Randomly sampling for a triangle near point (%.12g, %.12g).\n",
5789            searchpoint[0], searchpoint[1]);
5790   }
5791   /* Record the distance from the suggested starting triangle to the */
5792   /*   point we seek.                                                */
5793   org(*searchtri, torg);
5794   searchdist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
5795              + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
5796   if (verbose > 2) {
5797     printf("    Boundary triangle has origin (%.12g, %.12g).\n",
5798            torg[0], torg[1]);
5799   }
5800
5801   /* If a recently encountered triangle has been recorded and has not been */
5802   /*   deallocated, test it as a good starting point.                      */
5803   if (recenttri.tri != (triangle *) NULL) {
5804     if (recenttri.tri[3] != (triangle) NULL) {
5805       org(recenttri, torg);
5806       if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
5807         triedgecopy(recenttri, *searchtri);
5808         return ONVERTEX;
5809       }
5810       dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
5811            + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
5812       if (dist < searchdist) {
5813         triedgecopy(recenttri, *searchtri);
5814         searchdist = dist;
5815         if (verbose > 2) {
5816           printf("    Choosing recent triangle with origin (%.12g, %.12g).\n",
5817                  torg[0], torg[1]);
5818         }
5819       }
5820     }
5821   }
5822
5823   /* The number of random samples taken is proportional to the cube root of */
5824   /*   the number of triangles in the mesh.  The next bit of code assumes   */
5825   /*   that the number of triangles increases monotonically.                */
5826   while (SAMPLEFACTOR * samples * samples * samples < triangles.items) {
5827     samples++;
5828   }
5829   triblocks = (triangles.maxitems + TRIPERBLOCK - 1) / TRIPERBLOCK;
5830   samplesperblock = 1 + (samples / triblocks);
5831   sampleblocks = samples / samplesperblock;
5832   sampleblock = triangles.firstblock;
5833   sampletri.orient = 0;
5834   for (i = 0; i < sampleblocks; i++) {
5835     alignptr = (unsigned long) (sampleblock + 1);
5836     firsttri = (triangle *) (alignptr + (unsigned long) triangles.alignbytes
5837                           - (alignptr % (unsigned long) triangles.alignbytes));
5838     for (j = 0; j < samplesperblock; j++) {
5839       if (i == triblocks - 1) {
5840         samplenum = randomnation((int)
5841                                  (triangles.maxitems - (i * TRIPERBLOCK)));
5842       } else {
5843         samplenum = randomnation(TRIPERBLOCK);
5844       }
5845       sampletri.tri = (triangle *)
5846                       (firsttri + (samplenum * triangles.itemwords));
5847       if (sampletri.tri[3] != (triangle) NULL) {
5848         org(sampletri, torg);
5849         dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
5850              + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
5851         if (dist < searchdist) {
5852           triedgecopy(sampletri, *searchtri);
5853           searchdist = dist;
5854           if (verbose > 2) {
5855             printf("    Choosing triangle with origin (%.12g, %.12g).\n",
5856                    torg[0], torg[1]);
5857           }
5858         }
5859       }
5860     }
5861     sampleblock = (VOID **) *sampleblock;
5862   }
5863   /* Where are we? */
5864   org(*searchtri, torg);
5865   dest(*searchtri, tdest);
5866   /* Check the starting triangle's vertices. */
5867   if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
5868     return ONVERTEX;
5869   }
5870   if ((tdest[0] == searchpoint[0]) && (tdest[1] == searchpoint[1])) {
5871     lnextself(*searchtri);
5872     return ONVERTEX;
5873   }
5874   /* Orient `searchtri' to fit the preconditions of calling preciselocate(). */
5875   ahead = counterclockwise(torg, tdest, searchpoint);
5876   if (ahead < 0.0) {
5877     /* Turn around so that `searchpoint' is to the left of the */
5878     /*   edge specified by `searchtri'.                        */
5879     symself(*searchtri);
5880   } else if (ahead == 0.0) {
5881     /* Check if `searchpoint' is between `torg' and `tdest'. */
5882     if (((torg[0] < searchpoint[0]) == (searchpoint[0] < tdest[0]))
5883         && ((torg[1] < searchpoint[1]) == (searchpoint[1] < tdest[1]))) {
5884       return ONEDGE;
5885     }
5886   }
5887   return preciselocate(searchpoint, searchtri);
5888 }
5889
5890 /**                                                                         **/
5891 /**                                                                         **/
5892 /********* Point location routines end here                          *********/
5893
5894 /********* Mesh transformation routines begin here                   *********/
5895 /**                                                                         **/
5896 /**                                                                         **/
5897
5898 /*****************************************************************************/
5899 /*                                                                           */
5900 /*  insertshelle()   Create a new shell edge and insert it between two       */
5901 /*                   triangles.                                              */
5902 /*                                                                           */
5903 /*  The new shell edge is inserted at the edge described by the handle       */
5904 /*  `tri'.  Its vertices are properly initialized.  The marker `shellemark'  */
5905 /*  is applied to the shell edge and, if appropriate, its vertices.          */
5906 /*                                                                           */
5907 /*****************************************************************************/
5908
5909 void insertshelle(tri, shellemark)
5910 struct triedge *tri;          /* Edge at which to insert the new shell edge. */
5911 int shellemark;                            /* Marker for the new shell edge. */
5912 {
5913   struct triedge oppotri;
5914   struct edge newshelle;
5915   point triorg, tridest;
5916   triangle ptr;                         /* Temporary variable used by sym(). */
5917   shelle sptr;                      /* Temporary variable used by tspivot(). */
5918
5919   /* Mark points if possible. */
5920   org(*tri, triorg);
5921   dest(*tri, tridest);
5922   if (pointmark(triorg) == 0) {
5923     setpointmark(triorg, shellemark);
5924   }
5925   if (pointmark(tridest) == 0) {
5926     setpointmark(tridest, shellemark);
5927   }
5928   /* Check if there's already a shell edge here. */
5929   tspivot(*tri, newshelle);
5930   if (newshelle.sh == dummysh) {
5931     /* Make new shell edge and initialize its vertices. */
5932     makeshelle(&newshelle);
5933     setsorg(newshelle, tridest);
5934     setsdest(newshelle, triorg);
5935     /* Bond new shell edge to the two triangles it is sandwiched between. */
5936     /*   Note that the facing triangle `oppotri' might be equal to        */
5937     /*   `dummytri' (outer space), but the new shell edge is bonded to it */
5938     /*   all the same.                                                    */
5939     tsbond(*tri, newshelle);
5940     sym(*tri, oppotri);
5941     ssymself(newshelle);
5942     tsbond(oppotri, newshelle);
5943     setmark(newshelle, shellemark);
5944     if (verbose > 2) {
5945       printf("  Inserting new ");
5946       printshelle(&newshelle);
5947     }
5948   } else {
5949     if (mark(newshelle) == 0) {
5950       setmark(newshelle, shellemark);
5951     }
5952   }
5953 }
5954
5955 /*****************************************************************************/
5956 /*                                                                           */
5957 /*  Terminology                                                              */
5958 /*                                                                           */
5959 /*  A "local transformation" replaces a small set of triangles with another  */
5960 /*  set of triangles.  This may or may not involve inserting or deleting a   */
5961 /*  point.                                                                   */
5962 /*                                                                           */
5963 /*  The term "casing" is used to describe the set of triangles that are      */
5964 /*  attached to the triangles being transformed, but are not transformed     */
5965 /*  themselves.  Think of the casing as a fixed hollow structure inside      */
5966 /*  which all the action happens.  A "casing" is only defined relative to    */
5967 /*  a single transformation; each occurrence of a transformation will        */
5968 /*  involve a different casing.                                              */
5969 /*                                                                           */
5970 /*  A "shell" is similar to a "casing".  The term "shell" describes the set  */
5971 /*  of shell edges (if any) that are attached to the triangles being         */
5972 /*  transformed.  However, I sometimes use "shell" to refer to a single      */
5973 /*  shell edge, so don't get confused.                                       */
5974 /*                                                                           */
5975 /*****************************************************************************/
5976
5977 /*****************************************************************************/
5978 /*                                                                           */
5979 /*  flip()   Transform two triangles to two different triangles by flipping  */
5980 /*           an edge within a quadrilateral.                                 */
5981 /*                                                                           */
5982 /*  Imagine the original triangles, abc and bad, oriented so that the        */
5983 /*  shared edge ab lies in a horizontal plane, with the point b on the left  */
5984 /*  and the point a on the right.  The point c lies below the edge, and the  */
5985 /*  point d lies above the edge.  The `flipedge' handle holds the edge ab    */
5986 /*  of triangle abc, and is directed left, from vertex a to vertex b.        */
5987 /*                                                                           */
5988 /*  The triangles abc and bad are deleted and replaced by the triangles cdb  */
5989 /*  and dca.  The triangles that represent abc and bad are NOT deallocated;  */
5990 /*  they are reused for dca and cdb, respectively.  Hence, any handles that  */
5991 /*  may have held the original triangles are still valid, although not       */
5992 /*  directed as they were before.                                            */
5993 /*                                                                           */
5994 /*  Upon completion of this routine, the `flipedge' handle holds the edge    */
5995 /*  dc of triangle dca, and is directed down, from vertex d to vertex c.     */
5996 /*  (Hence, the two triangles have rotated counterclockwise.)                */
5997 /*                                                                           */
5998 /*  WARNING:  This transformation is geometrically valid only if the         */
5999 /*  quadrilateral adbc is convex.  Furthermore, this transformation is       */
6000 /*  valid only if there is not a shell edge between the triangles abc and    */
6001 /*  bad.  This routine does not check either of these preconditions, and     */
6002 /*  it is the responsibility of the calling routine to ensure that they are  */
6003 /*  met.  If they are not, the streets shall be filled with wailing and      */
6004 /*  gnashing of teeth.                                                       */
6005 /*                                                                           */
6006 /*****************************************************************************/
6007
6008 void flip(flipedge)
6009 struct triedge *flipedge;                    /* Handle for the triangle abc. */
6010 {
6011   struct triedge botleft, botright;
6012   struct triedge topleft, topright;
6013   struct triedge top;
6014   struct triedge botlcasing, botrcasing;
6015   struct triedge toplcasing, toprcasing;
6016   struct edge botlshelle, botrshelle;
6017   struct edge toplshelle, toprshelle;
6018   point leftpoint, rightpoint, botpoint;
6019   point farpoint;
6020   triangle ptr;                         /* Temporary variable used by sym(). */
6021   shelle sptr;                      /* Temporary variable used by tspivot(). */
6022
6023   /* Identify the vertices of the quadrilateral. */
6024   org(*flipedge, rightpoint);
6025   dest(*flipedge, leftpoint);
6026   apex(*flipedge, botpoint);
6027   sym(*flipedge, top);
6028 #ifdef SELF_CHECK
6029   if (top.tri == dummytri) {
6030     printf("Internal error in flip():  Attempt to flip on boundary.\n");
6031     lnextself(*flipedge);
6032     return;
6033   }
6034   if (checksegments) {
6035     tspivot(*flipedge, toplshelle);
6036     if (toplshelle.sh != dummysh) {
6037       printf("Internal error in flip():  Attempt to flip a segment.\n");
6038       lnextself(*flipedge);
6039       return;
6040     }
6041   }
6042 #endif /* SELF_CHECK */
6043   apex(top, farpoint);
6044
6045   /* Identify the casing of the quadrilateral. */
6046   lprev(top, topleft);
6047   sym(topleft, toplcasing);
6048   lnext(top, topright);
6049   sym(topright, toprcasing);
6050   lnext(*flipedge, botleft);
6051   sym(botleft, botlcasing);
6052   lprev(*flipedge, botright);
6053   sym(botright, botrcasing);
6054   /* Rotate the quadrilateral one-quarter turn counterclockwise. */
6055   bond(topleft, botlcasing);
6056   bond(botleft, botrcasing);
6057   bond(botright, toprcasing);
6058   bond(topright, toplcasing);
6059
6060   if (checksegments) {
6061     /* Check for shell edges and rebond them to the quadrilateral. */
6062     tspivot(topleft, toplshelle);
6063     tspivot(botleft, botlshelle);
6064     tspivot(botright, botrshelle);
6065     tspivot(topright, toprshelle);
6066     if (toplshelle.sh == dummysh) {
6067       tsdissolve(topright);
6068     } else {
6069       tsbond(topright, toplshelle);
6070     }
6071     if (botlshelle.sh == dummysh) {
6072       tsdissolve(topleft);
6073     } else {
6074       tsbond(topleft, botlshelle);
6075     }
6076     if (botrshelle.sh == dummysh) {
6077       tsdissolve(botleft);
6078     } else {
6079       tsbond(botleft, botrshelle);
6080     }
6081     if (toprshelle.sh == dummysh) {
6082       tsdissolve(botright);
6083     } else {
6084       tsbond(botright, toprshelle);
6085     }
6086   }
6087
6088   /* New point assignments for the rotated quadrilateral. */
6089   setorg(*flipedge, farpoint);
6090   setdest(*flipedge, botpoint);
6091   setapex(*flipedge, rightpoint);
6092   setorg(top, botpoint);
6093   setdest(top, farpoint);
6094   setapex(top, leftpoint);
6095   if (verbose > 2) {
6096     printf("  Edge flip results in left ");
6097     lnextself(topleft);
6098     printtriangle(&topleft);
6099     printf("  and right ");
6100     printtriangle(flipedge);
6101   }
6102 }
6103
6104 /*****************************************************************************/
6105 /*                                                                           */
6106 /*  insertsite()   Insert a vertex into a Delaunay triangulation,            */
6107 /*                 performing flips as necessary to maintain the Delaunay    */
6108 /*                 property.                                                 */
6109 /*                                                                           */
6110 /*  The point `insertpoint' is located.  If `searchtri.tri' is not NULL,     */
6111 /*  the search for the containing triangle begins from `searchtri'.  If      */
6112 /*  `searchtri.tri' is NULL, a full point location procedure is called.      */
6113 /*  If `insertpoint' is found inside a triangle, the triangle is split into  */
6114 /*  three; if `insertpoint' lies on an edge, the edge is split in two,       */
6115 /*  thereby splitting the two adjacent triangles into four.  Edge flips are  */
6116 /*  used to restore the Delaunay property.  If `insertpoint' lies on an      */
6117 /*  existing vertex, no action is taken, and the value DUPLICATEPOINT is     */
6118 /*  returned.  On return, `searchtri' is set to a handle whose origin is the */
6119 /*  existing vertex.                                                         */
6120 /*                                                                           */
6121 /*  Normally, the parameter `splitedge' is set to NULL, implying that no     */
6122 /*  segment should be split.  In this case, if `insertpoint' is found to     */
6123 /*  lie on a segment, no action is taken, and the value VIOLATINGPOINT is    */
6124 /*  returned.  On return, `searchtri' is set to a handle whose primary edge  */
6125 /*  is the violated segment.                                                 */
6126 /*                                                                           */
6127 /*  If the calling routine wishes to split a segment by inserting a point in */
6128 /*  it, the parameter `splitedge' should be that segment.  In this case,     */
6129 /*  `searchtri' MUST be the triangle handle reached by pivoting from that    */
6130 /*  segment; no point location is done.                                      */
6131 /*                                                                           */
6132 /*  `segmentflaws' and `triflaws' are flags that indicate whether or not     */
6133 /*  there should be checks for the creation of encroached segments or bad    */
6134 /*  quality faces.  If a newly inserted point encroaches upon segments,      */
6135 /*  these segments are added to the list of segments to be split if          */
6136 /*  `segmentflaws' is set.  If bad triangles are created, these are added    */
6137 /*  to the queue if `triflaws' is set.                                       */
6138 /*                                                                           */
6139 /*  If a duplicate point or violated segment does not prevent the point      */
6140 /*  from being inserted, the return value will be ENCROACHINGPOINT if the    */
6141 /*  point encroaches upon a segment (and checking is enabled), or            */
6142 /*  SUCCESSFULPOINT otherwise.  In either case, `searchtri' is set to a      */
6143 /*  handle whose origin is the newly inserted vertex.                        */
6144 /*                                                                           */
6145 /*  insertsite() does not use flip() for reasons of speed; some              */
6146 /*  information can be reused from edge flip to edge flip, like the          */
6147 /*  locations of shell edges.                                                */
6148 /*                                                                           */
6149 /*****************************************************************************/
6150
6151 enum insertsiteresult insertsite(insertpoint, searchtri, splitedge,
6152                                  segmentflaws, triflaws)
6153 point insertpoint;
6154 struct triedge *searchtri;
6155 struct edge *splitedge;
6156 int segmentflaws;
6157 int triflaws;
6158 {
6159   struct triedge horiz;
6160   struct triedge top;
6161   struct triedge botleft, botright;
6162   struct triedge topleft, topright;
6163   struct triedge newbotleft, newbotright;
6164   struct triedge newtopright;
6165   struct triedge botlcasing, botrcasing;
6166   struct triedge toplcasing, toprcasing;
6167   struct triedge testtri;
6168   struct edge botlshelle, botrshelle;
6169   struct edge toplshelle, toprshelle;
6170   struct edge brokenshelle;
6171   struct edge checkshelle;
6172   struct edge rightedge;
6173   struct edge newedge;
6174   struct edge *encroached;
6175   point first;
6176   point leftpoint, rightpoint, botpoint, toppoint, farpoint;
6177   REAL attrib;
6178   REAL area;
6179   enum insertsiteresult success;
6180   enum locateresult intersect;
6181   int doflip;
6182   int mirrorflag;
6183   int i;
6184   triangle ptr;                         /* Temporary variable used by sym(). */
6185   shelle sptr;         /* Temporary variable used by spivot() and tspivot(). */
6186
6187   if (verbose > 1) {
6188     printf("  Inserting (%.12g, %.12g).\n", insertpoint[0], insertpoint[1]);
6189   }
6190   if (splitedge == (struct edge *) NULL) {
6191     /* Find the location of the point to be inserted.  Check if a good */
6192     /*   starting triangle has already been provided by the caller.    */
6193     if (searchtri->tri == (triangle *) NULL) {
6194       /* Find a boundary triangle. */
6195       horiz.tri = dummytri;
6196       horiz.orient = 0;
6197       symself(horiz);
6198       /* Search for a triangle containing `insertpoint'. */
6199       intersect = locate(insertpoint, &horiz);
6200     } else {
6201       /* Start searching from the triangle provided by the caller. */
6202       triedgecopy(*searchtri, horiz);
6203       intersect = preciselocate(insertpoint, &horiz);
6204     }
6205   } else {
6206     /* The calling routine provides the edge in which the point is inserted. */
6207     triedgecopy(*searchtri, horiz);
6208     intersect = ONEDGE;
6209   }
6210   if (intersect == ONVERTEX) {
6211     /* There's already a vertex there.  Return in `searchtri' a triangle */
6212     /*   whose origin is the existing vertex.                            */
6213     triedgecopy(horiz, *searchtri);
6214     triedgecopy(horiz, recenttri);
6215     return DUPLICATEPOINT;
6216   }
6217   if ((intersect == ONEDGE) || (intersect == OUTSIDE)) {
6218     /* The vertex falls on an edge or boundary. */
6219     if (checksegments && (splitedge == (struct edge *) NULL)) {
6220       /* Check whether the vertex falls on a shell edge. */
6221       tspivot(horiz, brokenshelle);
6222       if (brokenshelle.sh != dummysh) {
6223         /* The vertex falls on a shell edge. */
6224         if (segmentflaws) {
6225           if (nobisect == 0) {
6226             /* Add the shell edge to the list of encroached segments. */
6227             encroached = (struct edge *) poolalloc(&badsegments);
6228             shellecopy(brokenshelle, *encroached);
6229           } else if ((nobisect == 1) && (intersect == ONEDGE)) {
6230             /* This segment may be split only if it is an internal boundary. */
6231             sym(horiz, testtri);
6232             if (testtri.tri != dummytri) {
6233               /* Add the shell edge to the list of encroached segments. */
6234               encroached = (struct edge *) poolalloc(&badsegments);
6235               shellecopy(brokenshelle, *encroached);
6236             }
6237           }
6238         }
6239         /* Return a handle whose primary edge contains the point, */
6240         /*   which has not been inserted.                         */
6241         triedgecopy(horiz, *searchtri);
6242         triedgecopy(horiz, recenttri);
6243         return VIOLATINGPOINT;
6244       }
6245     }
6246     /* Insert the point on an edge, dividing one triangle into two (if */
6247     /*   the edge lies on a boundary) or two triangles into four.      */
6248     lprev(horiz, botright);
6249     sym(botright, botrcasing);
6250     sym(horiz, topright);
6251     /* Is there a second triangle?  (Or does this edge lie on a boundary?) */
6252     mirrorflag = topright.tri != dummytri;
6253     if (mirrorflag) {
6254       lnextself(topright);
6255       sym(topright, toprcasing);
6256       maketriangle(&newtopright);
6257     } else {
6258       /* Splitting the boundary edge increases the number of boundary edges. */
6259       hullsize++;
6260     }
6261     maketriangle(&newbotright);
6262
6263     /* Set the vertices of changed and new triangles. */
6264     org(horiz, rightpoint);
6265     dest(horiz, leftpoint);
6266     apex(horiz, botpoint);
6267     setorg(newbotright, botpoint);
6268     setdest(newbotright, rightpoint);
6269     setapex(newbotright, insertpoint);
6270     setorg(horiz, insertpoint);
6271     for (i = 0; i < eextras; i++) {
6272       /* Set the element attributes of a new triangle. */
6273       setelemattribute(newbotright, i, elemattribute(botright, i));
6274     }
6275     if (vararea) {
6276       /* Set the area constraint of a new triangle. */
6277       setareabound(newbotright, areabound(botright));
6278     }
6279     if (mirrorflag) {
6280       dest(topright, toppoint);
6281       setorg(newtopright, rightpoint);
6282       setdest(newtopright, toppoint);
6283       setapex(newtopright, insertpoint);
6284       setorg(topright, insertpoint);
6285       for (i = 0; i < eextras; i++) {
6286         /* Set the element attributes of another new triangle. */
6287         setelemattribute(newtopright, i, elemattribute(topright, i));
6288       }
6289       if (vararea) {
6290         /* Set the area constraint of another new triangle. */
6291         setareabound(newtopright, areabound(topright));
6292       }
6293     }
6294
6295     /* There may be shell edges that need to be bonded */
6296     /*   to the new triangle(s).                       */
6297     if (checksegments) {
6298       tspivot(botright, botrshelle);
6299       if (botrshelle.sh != dummysh) {
6300         tsdissolve(botright);
6301         tsbond(newbotright, botrshelle);
6302       }
6303       if (mirrorflag) {
6304         tspivot(topright, toprshelle);
6305         if (toprshelle.sh != dummysh) {
6306           tsdissolve(topright);
6307           tsbond(newtopright, toprshelle);
6308         }
6309       }
6310     }
6311
6312     /* Bond the new triangle(s) to the surrounding triangles. */
6313     bond(newbotright, botrcasing);
6314     lprevself(newbotright);
6315     bond(newbotright, botright);
6316     lprevself(newbotright);
6317     if (mirrorflag) {
6318       bond(newtopright, toprcasing);
6319       lnextself(newtopright);
6320       bond(newtopright, topright);
6321       lnextself(newtopright);
6322       bond(newtopright, newbotright);
6323     }
6324
6325     if (splitedge != (struct edge *) NULL) {
6326       /* Split the shell edge into two. */
6327       setsdest(*splitedge, insertpoint);
6328       ssymself(*splitedge);
6329       spivot(*splitedge, rightedge);
6330       insertshelle(&newbotright, mark(*splitedge));
6331       tspivot(newbotright, newedge);
6332       sbond(*splitedge, newedge);
6333       ssymself(newedge);
6334       sbond(newedge, rightedge);
6335       ssymself(*splitedge);
6336     }
6337
6338 #ifdef SELF_CHECK
6339     if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
6340       printf("Internal error in insertsite():\n");
6341       printf("  Clockwise triangle prior to edge point insertion (bottom).\n");
6342     }
6343     if (mirrorflag) {
6344       if (counterclockwise(leftpoint, rightpoint, toppoint) < 0.0) {
6345         printf("Internal error in insertsite():\n");
6346         printf("  Clockwise triangle prior to edge point insertion (top).\n");
6347       }
6348       if (counterclockwise(rightpoint, toppoint, insertpoint) < 0.0) {
6349         printf("Internal error in insertsite():\n");
6350         printf("  Clockwise triangle after edge point insertion (top right).\n"
6351                );
6352       }
6353       if (counterclockwise(toppoint, leftpoint, insertpoint) < 0.0) {
6354         printf("Internal error in insertsite():\n");
6355         printf("  Clockwise triangle after edge point insertion (top left).\n"
6356                );
6357       }
6358     }
6359     if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
6360       printf("Internal error in insertsite():\n");
6361       printf("  Clockwise triangle after edge point insertion (bottom left).\n"
6362              );
6363     }
6364     if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
6365       printf("Internal error in insertsite():\n");
6366       printf(
6367         "  Clockwise triangle after edge point insertion (bottom right).\n");
6368     }
6369 #endif /* SELF_CHECK */
6370     if (verbose > 2) {
6371       printf("  Updating bottom left ");
6372       printtriangle(&botright);
6373       if (mirrorflag) {
6374         printf("  Updating top left ");
6375         printtriangle(&topright);
6376         printf("  Creating top right ");
6377         printtriangle(&newtopright);
6378       }
6379       printf("  Creating bottom right ");
6380       printtriangle(&newbotright);
6381     }
6382
6383     /* Position `horiz' on the first edge to check for */
6384     /*   the Delaunay property.                        */
6385     lnextself(horiz);
6386   } else {
6387     /* Insert the point in a triangle, splitting it into three. */
6388     lnext(horiz, botleft);
6389     lprev(horiz, botright);
6390     sym(botleft, botlcasing);
6391     sym(botright, botrcasing);
6392     maketriangle(&newbotleft);
6393     maketriangle(&newbotright);
6394
6395     /* Set the vertices of changed and new triangles. */
6396     org(horiz, rightpoint);
6397     dest(horiz, leftpoint);
6398     apex(horiz, botpoint);
6399     setorg(newbotleft, leftpoint);
6400     setdest(newbotleft, botpoint);
6401     setapex(newbotleft, insertpoint);
6402     setorg(newbotright, botpoint);
6403     setdest(newbotright, rightpoint);
6404     setapex(newbotright, insertpoint);
6405     setapex(horiz, insertpoint);
6406     for (i = 0; i < eextras; i++) {
6407       /* Set the element attributes of the new triangles. */
6408       attrib = elemattribute(horiz, i);
6409       setelemattribute(newbotleft, i, attrib);
6410       setelemattribute(newbotright, i, attrib);
6411     }
6412     if (vararea) {
6413       /* Set the area constraint of the new triangles. */
6414       area = areabound(horiz);
6415       setareabound(newbotleft, area);
6416       setareabound(newbotright, area);
6417     }
6418
6419     /* There may be shell edges that need to be bonded */
6420     /*   to the new triangles.                         */
6421     if (checksegments) {
6422       tspivot(botleft, botlshelle);
6423       if (botlshelle.sh != dummysh) {
6424         tsdissolve(botleft);
6425         tsbond(newbotleft, botlshelle);
6426       }
6427       tspivot(botright, botrshelle);
6428       if (botrshelle.sh != dummysh) {
6429         tsdissolve(botright);
6430         tsbond(newbotright, botrshelle);
6431       }
6432     }
6433
6434     /* Bond the new triangles to the surrounding triangles. */
6435     bond(newbotleft, botlcasing);
6436     bond(newbotright, botrcasing);
6437     lnextself(newbotleft);
6438     lprevself(newbotright);
6439     bond(newbotleft, newbotright);
6440     lnextself(newbotleft);
6441     bond(botleft, newbotleft);
6442     lprevself(newbotright);
6443     bond(botright, newbotright);
6444
6445 #ifdef SELF_CHECK
6446     if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
6447       printf("Internal error in insertsite():\n");
6448       printf("  Clockwise triangle prior to point insertion.\n");
6449     }
6450     if (counterclockwise(rightpoint, leftpoint, insertpoint) < 0.0) {
6451       printf("Internal error in insertsite():\n");
6452       printf("  Clockwise triangle after point insertion (top).\n");
6453     }
6454     if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
6455       printf("Internal error in insertsite():\n");
6456       printf("  Clockwise triangle after point insertion (left).\n");
6457     }
6458     if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
6459       printf("Internal error in insertsite():\n");
6460       printf("  Clockwise triangle after point insertion (right).\n");
6461     }
6462 #endif /* SELF_CHECK */
6463     if (verbose > 2) {
6464       printf("  Updating top ");
6465       printtriangle(&horiz);
6466       printf("  Creating left ");
6467       printtriangle(&newbotleft);
6468       printf("  Creating right ");
6469       printtriangle(&newbotright);
6470     }
6471   }
6472
6473   /* The insertion is successful by default, unless an encroached */
6474   /*   edge is found.                                             */
6475   success = SUCCESSFULPOINT;
6476   /* Circle around the newly inserted vertex, checking each edge opposite */
6477   /*   it for the Delaunay property.  Non-Delaunay edges are flipped.     */
6478   /*   `horiz' is always the edge being checked.  `first' marks where to  */
6479   /*   stop circling.                                                     */
6480   org(horiz, first);
6481   rightpoint = first;
6482   dest(horiz, leftpoint);
6483   /* Circle until finished. */
6484   while (1) {
6485     /* By default, the edge will be flipped. */
6486     doflip = 1;
6487     if (checksegments) {
6488       /* Check for a segment, which cannot be flipped. */
6489       tspivot(horiz, checkshelle);
6490       if (checkshelle.sh != dummysh) {
6491         /* The edge is a segment and cannot be flipped. */
6492         doflip = 0;
6493 #ifndef CDT_ONLY
6494         if (segmentflaws) {
6495           /* Does the new point encroach upon this segment? */
6496           if (checkedge4encroach(&checkshelle)) {
6497             success = ENCROACHINGPOINT;
6498           }
6499         }
6500 #endif /* not CDT_ONLY */
6501       }
6502     }
6503     if (doflip) {
6504       /* Check if the edge is a boundary edge. */
6505       sym(horiz, top);
6506       if (top.tri == dummytri) {
6507         /* The edge is a boundary edge and cannot be flipped. */
6508         doflip = 0;
6509       } else {
6510         /* Find the point on the other side of the edge. */
6511         apex(top, farpoint);
6512         /* In the incremental Delaunay triangulation algorithm, any of    */
6513         /*   `leftpoint', `rightpoint', and `farpoint' could be vertices  */
6514         /*   of the triangular bounding box.  These vertices must be      */
6515         /*   treated as if they are infinitely distant, even though their */
6516         /*   "coordinates" are not.                                       */
6517         if ((leftpoint == infpoint1) || (leftpoint == infpoint2)
6518                    || (leftpoint == infpoint3)) {
6519           /* `leftpoint' is infinitely distant.  Check the convexity of */
6520           /*   the boundary of the triangulation.  'farpoint' might be  */
6521           /*   infinite as well, but trust me, this same condition      */
6522           /*   should be applied.                                       */
6523           doflip = counterclockwise(insertpoint, rightpoint, farpoint) > 0.0;
6524         } else if ((rightpoint == infpoint1) || (rightpoint == infpoint2)
6525                    || (rightpoint == infpoint3)) {
6526           /* `rightpoint' is infinitely distant.  Check the convexity of */
6527           /*   the boundary of the triangulation.  'farpoint' might be  */
6528           /*   infinite as well, but trust me, this same condition      */
6529           /*   should be applied.                                       */
6530           doflip = counterclockwise(farpoint, leftpoint, insertpoint) > 0.0;
6531         } else if ((farpoint == infpoint1) || (farpoint == infpoint2)
6532             || (farpoint == infpoint3)) {
6533           /* `farpoint' is infinitely distant and cannot be inside */
6534           /*   the circumcircle of the triangle `horiz'.           */
6535           doflip = 0;
6536         } else {
6537           /* Test whether the edge is locally Delaunay. */
6538           doflip = incircle(leftpoint, insertpoint, rightpoint, farpoint)
6539                    > 0.0;
6540         }
6541         if (doflip) {
6542           /* We made it!  Flip the edge `horiz' by rotating its containing */
6543           /*   quadrilateral (the two triangles adjacent to `horiz').      */
6544           /* Identify the casing of the quadrilateral. */
6545           lprev(top, topleft);
6546           sym(topleft, toplcasing);
6547           lnext(top, topright);
6548           sym(topright, toprcasing);
6549           lnext(horiz, botleft);
6550           sym(botleft, botlcasing);
6551           lprev(horiz, botright);
6552           sym(botright, botrcasing);
6553           /* Rotate the quadrilateral one-quarter turn counterclockwise. */
6554           bond(topleft, botlcasing);
6555           bond(botleft, botrcasing);
6556           bond(botright, toprcasing);
6557           bond(topright, toplcasing);
6558           if (checksegments) {
6559             /* Check for shell edges and rebond them to the quadrilateral. */
6560             tspivot(topleft, toplshelle);
6561             tspivot(botleft, botlshelle);
6562             tspivot(botright, botrshelle);
6563             tspivot(topright, toprshelle);
6564             if (toplshelle.sh == dummysh) {
6565               tsdissolve(topright);
6566             } else {
6567               tsbond(topright, toplshelle);
6568             }
6569             if (botlshelle.sh == dummysh) {
6570               tsdissolve(topleft);
6571             } else {
6572               tsbond(topleft, botlshelle);
6573             }
6574             if (botrshelle.sh == dummysh) {
6575               tsdissolve(botleft);
6576             } else {
6577               tsbond(botleft, botrshelle);
6578             }
6579             if (toprshelle.sh == dummysh) {
6580               tsdissolve(botright);
6581             } else {
6582               tsbond(botright, toprshelle);
6583             }
6584           }
6585           /* New point assignments for the rotated quadrilateral. */
6586           setorg(horiz, farpoint);
6587           setdest(horiz, insertpoint);
6588           setapex(horiz, rightpoint);
6589           setorg(top, insertpoint);
6590           setdest(top, farpoint);
6591           setapex(top, leftpoint);
6592           for (i = 0; i < eextras; i++) {
6593             /* Take the average of the two triangles' attributes. */
6594             attrib = 0.5 * (elemattribute(top, i) + elemattribute(horiz, i));
6595             setelemattribute(top, i, attrib);
6596             setelemattribute(horiz, i, attrib);
6597           }
6598           if (vararea) {
6599             if ((areabound(top) <= 0.0) || (areabound(horiz) <= 0.0)) {
6600               area = -1.0;
6601             } else {
6602               /* Take the average of the two triangles' area constraints.    */
6603               /*   This prevents small area constraints from migrating a     */
6604               /*   long, long way from their original location due to flips. */
6605               area = 0.5 * (areabound(top) + areabound(horiz));
6606             }
6607             setareabound(top, area);
6608             setareabound(horiz, area);
6609           }
6610 #ifdef SELF_CHECK
6611           if (insertpoint != (point) NULL) {
6612             if (counterclockwise(leftpoint, insertpoint, rightpoint) < 0.0) {
6613               printf("Internal error in insertsite():\n");
6614               printf("  Clockwise triangle prior to edge flip (bottom).\n");
6615             }
6616             /* The following test has been removed because constrainededge() */
6617             /*   sometimes generates inverted triangles that insertsite()    */
6618             /*   removes.                                                    */
6619 /*
6620             if (counterclockwise(rightpoint, farpoint, leftpoint) < 0.0) {
6621               printf("Internal error in insertsite():\n");
6622               printf("  Clockwise triangle prior to edge flip (top).\n");
6623             }
6624 */
6625             if (counterclockwise(farpoint, leftpoint, insertpoint) < 0.0) {
6626               printf("Internal error in insertsite():\n");
6627               printf("  Clockwise triangle after edge flip (left).\n");
6628             }
6629             if (counterclockwise(insertpoint, rightpoint, farpoint) < 0.0) {
6630               printf("Internal error in insertsite():\n");
6631               printf("  Clockwise triangle after edge flip (right).\n");
6632             }
6633           }
6634 #endif /* SELF_CHECK */
6635           if (verbose > 2) {
6636             printf("  Edge flip results in left ");
6637             lnextself(topleft);
6638             printtriangle(&topleft);
6639             printf("  and right ");
6640             printtriangle(&horiz);
6641           }
6642           /* On the next iterations, consider the two edges that were  */
6643           /*   exposed (this is, are now visible to the newly inserted */
6644           /*   point) by the edge flip.                                */
6645           lprevself(horiz);
6646           leftpoint = farpoint;
6647         }
6648       }
6649     }
6650     if (!doflip) {
6651       /* The handle `horiz' is accepted as locally Delaunay. */
6652 #ifndef CDT_ONLY
6653       if (triflaws) {
6654         /* Check the triangle `horiz' for quality. */
6655         testtriangle(&horiz);
6656       }
6657 #endif /* not CDT_ONLY */
6658       /* Look for the next edge around the newly inserted point. */
6659       lnextself(horiz);
6660       sym(horiz, testtri);
6661       /* Check for finishing a complete revolution about the new point, or */
6662       /*   falling off the edge of the triangulation.  The latter will     */
6663       /*   happen when a point is inserted at a boundary.                  */
6664       if ((leftpoint == first) || (testtri.tri == dummytri)) {
6665         /* We're done.  Return a triangle whose origin is the new point. */
6666         lnext(horiz, *searchtri);
6667         lnext(horiz, recenttri);
6668         return success;
6669       }
6670       /* Finish finding the next edge around the newly inserted point. */
6671       lnext(testtri, horiz);
6672       rightpoint = leftpoint;
6673       dest(horiz, leftpoint);
6674     }
6675   }
6676 }
6677
6678 /*****************************************************************************/
6679 /*                                                                           */
6680 /*  triangulatepolygon()   Find the Delaunay triangulation of a polygon that */
6681 /*                         has a certain "nice" shape.  This includes the    */
6682 /*                         polygons that result from deletion of a point or  */
6683 /*                         insertion of a segment.                           */
6684 /*                                                                           */
6685 /*  This is a conceptually difficult routine.  The starting assumption is    */
6686 /*  that we have a polygon with n sides.  n - 1 of these sides are currently */
6687 /*  represented as edges in the mesh.  One side, called the "base", need not */
6688 /*  be.                                                                      */
6689 /*                                                                           */
6690 /*  Inside the polygon is a structure I call a "fan", consisting of n - 1    */
6691 /*  triangles that share a common origin.  For each of these triangles, the  */
6692 /*  edge opposite the origin is one of the sides of the polygon.  The        */
6693 /*  primary edge of each triangle is the edge directed from the origin to    */
6694 /*  the destination; note that this is not the same edge that is a side of   */
6695 /*  the polygon.  `firstedge' is the primary edge of the first triangle.     */
6696 /*  From there, the triangles follow in counterclockwise order about the     */
6697 /*  polygon, until `lastedge', the primary edge of the last triangle.        */
6698 /*  `firstedge' and `lastedge' are probably connected to other triangles     */
6699 /*  beyond the extremes of the fan, but their identity is not important, as  */
6700 /*  long as the fan remains connected to them.                               */
6701 /*                                                                           */
6702 /*  Imagine the polygon oriented so that its base is at the bottom.  This    */
6703 /*  puts `firstedge' on the far right, and `lastedge' on the far left.       */
6704 /*  The right vertex of the base is the destination of `firstedge', and the  */
6705 /*  left vertex of the base is the apex of `lastedge'.                       */
6706 /*                                                                           */
6707 /*  The challenge now is to find the right sequence of edge flips to         */
6708 /*  transform the fan into a Delaunay triangulation of the polygon.  Each    */
6709 /*  edge flip effectively removes one triangle from the fan, committing it   */
6710 /*  to the polygon.  The resulting polygon has one fewer edge.  If `doflip'  */
6711 /*  is set, the final flip will be performed, resulting in a fan of one      */
6712 /*  (useless?) triangle.  If `doflip' is not set, the final flip is not      */
6713 /*  performed, resulting in a fan of two triangles, and an unfinished        */
6714 /*  triangular polygon that is not yet filled out with a single triangle.    */
6715 /*  On completion of the routine, `lastedge' is the last remaining triangle, */
6716 /*  or the leftmost of the last two.                                         */
6717 /*                                                                           */
6718 /*  Although the flips are performed in the order described above, the       */
6719 /*  decisions about what flips to perform are made in precisely the reverse  */
6720 /*  order.  The recursive triangulatepolygon() procedure makes a decision,   */
6721 /*  uses up to two recursive calls to triangulate the "subproblems"          */
6722 /*  (polygons with fewer edges), and then performs an edge flip.             */
6723 /*                                                                           */
6724 /*  The "decision" it makes is which vertex of the polygon should be         */
6725 /*  connected to the base.  This decision is made by testing every possible  */
6726 /*  vertex.  Once the best vertex is found, the two edges that connect this  */
6727 /*  vertex to the base become the bases for two smaller polygons.  These     */
6728 /*  are triangulated recursively.  Unfortunately, this approach can take     */
6729 /*  O(n^2) time not only in the worst case, but in many common cases.  It's  */
6730 /*  rarely a big deal for point deletion, where n is rarely larger than ten, */
6731 /*  but it could be a big deal for segment insertion, especially if there's  */
6732 /*  a lot of long segments that each cut many triangles.  I ought to code    */
6733 /*  a faster algorithm some time.                                            */
6734 /*                                                                           */
6735 /*  The `edgecount' parameter is the number of sides of the polygon,         */
6736 /*  including its base.  `triflaws' is a flag that determines whether the    */
6737 /*  new triangles should be tested for quality, and enqueued if they are     */
6738 /*  bad.                                                                     */
6739 /*                                                                           */
6740 /*****************************************************************************/
6741
6742 void triangulatepolygon(firstedge, lastedge, edgecount, doflip, triflaws)
6743 struct triedge *firstedge;
6744 struct triedge *lastedge;
6745 int edgecount;
6746 int doflip;
6747 int triflaws;
6748 {
6749   struct triedge testtri;
6750   struct triedge besttri;
6751   struct triedge tempedge;
6752   point leftbasepoint, rightbasepoint;
6753   point testpoint;
6754   point bestpoint;
6755   int bestnumber;
6756   int i;
6757   triangle ptr;   /* Temporary variable used by sym(), onext(), and oprev(). */
6758
6759   /* Identify the base vertices. */
6760   apex(*lastedge, leftbasepoint);
6761   dest(*firstedge, rightbasepoint);
6762   if (verbose > 2) {
6763     printf("  Triangulating interior polygon at edge\n");
6764     printf("    (%.12g, %.12g) (%.12g, %.12g)\n", leftbasepoint[0],
6765            leftbasepoint[1], rightbasepoint[0], rightbasepoint[1]);
6766   }
6767   /* Find the best vertex to connect the base to. */
6768   onext(*firstedge, besttri);
6769   dest(besttri, bestpoint);
6770   triedgecopy(besttri, testtri);
6771   bestnumber = 1;
6772   for (i = 2; i <= edgecount - 2; i++) {
6773     onextself(testtri);
6774     dest(testtri, testpoint);
6775     /* Is this a better vertex? */
6776     if (incircle(leftbasepoint, rightbasepoint, bestpoint, testpoint) > 0.0) {
6777       triedgecopy(testtri, besttri);
6778       bestpoint = testpoint;
6779       bestnumber = i;
6780     }
6781   }
6782   if (verbose > 2) {
6783     printf("    Connecting edge to (%.12g, %.12g)\n", bestpoint[0],
6784            bestpoint[1]);
6785   }
6786   if (bestnumber > 1) {
6787     /* Recursively triangulate the smaller polygon on the right. */
6788     oprev(besttri, tempedge);
6789     triangulatepolygon(firstedge, &tempedge, bestnumber + 1, 1, triflaws);
6790   }
6791   if (bestnumber < edgecount - 2) {
6792     /* Recursively triangulate the smaller polygon on the left. */
6793     sym(besttri, tempedge);
6794     triangulatepolygon(&besttri, lastedge, edgecount - bestnumber, 1,
6795                        triflaws);
6796     /* Find `besttri' again; it may have been lost to edge flips. */
6797     sym(tempedge, besttri);
6798   }
6799   if (doflip) {
6800     /* Do one final edge flip. */
6801     flip(&besttri);
6802 #ifndef CDT_ONLY
6803     if (triflaws) {
6804       /* Check the quality of the newly committed triangle. */
6805       sym(besttri, testtri);
6806       testtriangle(&testtri);
6807     }
6808 #endif /* not CDT_ONLY */
6809   }
6810   /* Return the base triangle. */
6811   triedgecopy(besttri, *lastedge);
6812 }
6813
6814 /*****************************************************************************/
6815 /*                                                                           */
6816 /*  deletesite()   Delete a vertex from a Delaunay triangulation, ensuring   */
6817 /*                 that the triangulation remains Delaunay.                  */
6818 /*                                                                           */
6819 /*  The origin of `deltri' is deleted.  The union of the triangles adjacent  */
6820 /*  to this point is a polygon, for which the Delaunay triangulation is      */
6821 /*  found.  Two triangles are removed from the mesh.                         */
6822 /*                                                                           */
6823 /*  Only interior points that do not lie on segments (shell edges) or        */
6824 /*  boundaries may be deleted.                                               */
6825 /*                                                                           */
6826 /*****************************************************************************/
6827
6828 #ifndef CDT_ONLY
6829
6830 void deletesite(deltri)
6831 struct triedge *deltri;
6832 {
6833   struct triedge countingtri;
6834   struct triedge firstedge, lastedge;
6835   struct triedge deltriright;
6836   struct triedge lefttri, righttri;
6837   struct triedge leftcasing, rightcasing;
6838   struct edge leftshelle, rightshelle;
6839   point delpoint;
6840   point neworg;
6841   int edgecount;
6842   triangle ptr;   /* Temporary variable used by sym(), onext(), and oprev(). */
6843   shelle sptr;                      /* Temporary variable used by tspivot(). */
6844
6845   org(*deltri, delpoint);
6846   if (verbose > 1) {
6847     printf("  Deleting (%.12g, %.12g).\n", delpoint[0], delpoint[1]);
6848   }
6849   pointdealloc(delpoint);
6850
6851   /* Count the degree of the point being deleted. */
6852   onext(*deltri, countingtri);
6853   edgecount = 1;
6854   while (!triedgeequal(*deltri, countingtri)) {
6855 #ifdef SELF_CHECK
6856     if (countingtri.tri == dummytri) {
6857       printf("Internal error in deletesite():\n");
6858       printf("  Attempt to delete boundary point.\n");
6859       internalerror();
6860     }
6861 #endif /* SELF_CHECK */
6862     edgecount++;
6863     onextself(countingtri);
6864   }
6865
6866 #ifdef SELF_CHECK
6867   if (edgecount < 3) {
6868     printf("Internal error in deletesite():\n  Point has degree %d.\n",
6869            edgecount);
6870     internalerror();
6871   }
6872 #endif /* SELF_CHECK */
6873   if (edgecount > 3) {
6874     /* Triangulate the polygon defined by the union of all triangles */
6875     /*   adjacent to the point being deleted.  Check the quality of  */
6876     /*   the resulting triangles.                                    */
6877     onext(*deltri, firstedge);
6878     oprev(*deltri, lastedge);
6879     triangulatepolygon(&firstedge, &lastedge, edgecount, 0, !nobisect);
6880   }
6881   /* Splice out two triangles. */
6882   lprev(*deltri, deltriright);
6883   dnext(*deltri, lefttri);
6884   sym(lefttri, leftcasing);
6885   oprev(deltriright, righttri);
6886   sym(righttri, rightcasing);
6887   bond(*deltri, leftcasing);
6888   bond(deltriright, rightcasing);
6889   tspivot(lefttri, leftshelle);
6890   if (leftshelle.sh != dummysh) {
6891     tsbond(*deltri, leftshelle);
6892   }
6893   tspivot(righttri, rightshelle);
6894   if (rightshelle.sh != dummysh) {
6895     tsbond(deltriright, rightshelle);
6896   }
6897
6898   /* Set the new origin of `deltri' and check its quality. */
6899   org(lefttri, neworg);
6900   setorg(*deltri, neworg);
6901   if (!nobisect) {
6902     testtriangle(deltri);
6903   }
6904
6905   /* Delete the two spliced-out triangles. */
6906   triangledealloc(lefttri.tri);
6907   triangledealloc(righttri.tri);
6908 }
6909
6910 #endif /* not CDT_ONLY */
6911
6912 /**                                                                         **/
6913 /**                                                                         **/
6914 /********* Mesh transformation routines end here                     *********/
6915
6916 /********* Divide-and-conquer Delaunay triangulation begins here     *********/
6917 /**                                                                         **/
6918 /**                                                                         **/
6919
6920 /*****************************************************************************/
6921 /*                                                                           */
6922 /*  The divide-and-conquer bounding box                                      */
6923 /*                                                                           */
6924 /*  I originally implemented the divide-and-conquer and incremental Delaunay */
6925 /*  triangulations using the edge-based data structure presented by Guibas   */
6926 /*  and Stolfi.  Switching to a triangle-based data structure doubled the    */
6927 /*  speed.  However, I had to think of a few extra tricks to maintain the    */
6928 /*  elegance of the original algorithms.                                     */
6929 /*                                                                           */
6930 /*  The "bounding box" used by my variant of the divide-and-conquer          */
6931 /*  algorithm uses one triangle for each edge of the convex hull of the      */
6932 /*  triangulation.  These bounding triangles all share a common apical       */
6933 /*  vertex, which is represented by NULL and which represents nothing.       */
6934 /*  The bounding triangles are linked in a circular fan about this NULL      */
6935 /*  vertex, and the edges on the convex hull of the triangulation appear     */
6936 /*  opposite the NULL vertex.  You might find it easiest to imagine that     */
6937 /*  the NULL vertex is a point in 3D space behind the center of the          */
6938 /*  triangulation, and that the bounding triangles form a sort of cone.      */
6939 /*                                                                           */
6940 /*  This bounding box makes it easy to represent degenerate cases.  For      */
6941 /*  instance, the triangulation of two vertices is a single edge.  This edge */
6942 /*  is represented by two bounding box triangles, one on each "side" of the  */
6943 /*  edge.  These triangles are also linked together in a fan about the NULL  */
6944 /*  vertex.                                                                  */
6945 /*                                                                           */
6946 /*  The bounding box also makes it easy to traverse the convex hull, as the  */
6947 /*  divide-and-conquer algorithm needs to do.                                */
6948 /*                                                                           */
6949 /*****************************************************************************/
6950
6951 /*****************************************************************************/
6952 /*                                                                           */
6953 /*  pointsort()   Sort an array of points by x-coordinate, using the         */
6954 /*                y-coordinate as a secondary key.                           */
6955 /*                                                                           */
6956 /*  Uses quicksort.  Randomized O(n log n) time.  No, I did not make any of  */
6957 /*  the usual quicksort mistakes.                                            */
6958 /*                                                                           */
6959 /*****************************************************************************/
6960
6961 void pointsort(sortarray, arraysize)
6962 point *sortarray;
6963 int arraysize;
6964 {
6965   int left, right;
6966   int pivot;
6967   REAL pivotx, pivoty;
6968   point temp;
6969
6970   if (arraysize == 2) {
6971     /* Recursive base case. */
6972     if ((sortarray[0][0] > sortarray[1][0]) ||
6973         ((sortarray[0][0] == sortarray[1][0]) &&
6974          (sortarray[0][1] > sortarray[1][1]))) {
6975       temp = sortarray[1];
6976       sortarray[1] = sortarray[0];
6977       sortarray[0] = temp;
6978     }
6979     return;
6980   }
6981   /* Choose a random pivot to split the array. */
6982   pivot = (int) randomnation(arraysize);
6983   pivotx = sortarray[pivot][0];
6984   pivoty = sortarray[pivot][1];
6985   /* Split the array. */
6986   left = -1;
6987   right = arraysize;
6988   while (left < right) {
6989     /* Search for a point whose x-coordinate is too large for the left. */
6990     do {
6991       left++;
6992     } while ((left <= right) && ((sortarray[left][0] < pivotx) ||
6993                                  ((sortarray[left][0] == pivotx) &&
6994                                   (sortarray[left][1] < pivoty))));
6995     /* Search for a point whose x-coordinate is too small for the right. */
6996     do {
6997       right--;
6998     } while ((left <= right) && ((sortarray[right][0] > pivotx) ||
6999                                  ((sortarray[right][0] == pivotx) &&
7000                                   (sortarray[right][1] > pivoty))));
7001     if (left < right) {
7002       /* Swap the left and right points. */
7003       temp = sortarray[left];
7004       sortarray[left] = sortarray[right];
7005       sortarray[right] = temp;
7006     }
7007   }
7008   if (left > 1) {
7009     /* Recursively sort the left subset. */
7010     pointsort(sortarray, left);
7011   }
7012   if (right < arraysize - 2) {
7013     /* Recursively sort the right subset. */
7014     pointsort(&sortarray[right + 1], arraysize - right - 1);
7015   }
7016 }
7017
7018 /*****************************************************************************/
7019 /*                                                                           */
7020 /*  pointmedian()   An order statistic algorithm, almost.  Shuffles an array */
7021 /*                  of points so that the first `median' points occur        */
7022 /*                  lexicographically before the remaining points.           */
7023 /*                                                                           */
7024 /*  Uses the x-coordinate as the primary key if axis == 0; the y-coordinate  */
7025 /*  if axis == 1.  Very similar to the pointsort() procedure, but runs in    */
7026 /*  randomized linear time.                                                  */
7027 /*                                                                           */
7028 /*****************************************************************************/
7029
7030 void pointmedian(sortarray, arraysize, median, axis)
7031 point *sortarray;
7032 int arraysize;
7033 int median;
7034 int axis;
7035 {
7036   int left, right;
7037   int pivot;
7038   REAL pivot1, pivot2;
7039   point temp;
7040
7041   if (arraysize == 2) {
7042     /* Recursive base case. */
7043     if ((sortarray[0][axis] > sortarray[1][axis]) ||
7044         ((sortarray[0][axis] == sortarray[1][axis]) &&
7045          (sortarray[0][1 - axis] > sortarray[1][1 - axis]))) {
7046       temp = sortarray[1];
7047       sortarray[1] = sortarray[0];
7048       sortarray[0] = temp;
7049     }
7050     return;
7051   }
7052   /* Choose a random pivot to split the array. */
7053   pivot = (int) randomnation(arraysize);
7054   pivot1 = sortarray[pivot][axis];
7055   pivot2 = sortarray[pivot][1 - axis];
7056   /* Split the array. */
7057   left = -1;
7058   right = arraysize;
7059   while (left < right) {
7060     /* Search for a point whose x-coordinate is too large for the left. */
7061     do {
7062       left++;
7063     } while ((left <= right) && ((sortarray[left][axis] < pivot1) ||
7064                                  ((sortarray[left][axis] == pivot1) &&
7065                                   (sortarray[left][1 - axis] < pivot2))));
7066     /* Search for a point whose x-coordinate is too small for the right. */
7067     do {
7068       right--;
7069     } while ((left <= right) && ((sortarray[right][axis] > pivot1) ||
7070                                  ((sortarray[right][axis] == pivot1) &&
7071                                   (sortarray[right][1 - axis] > pivot2))));
7072     if (left < right) {
7073       /* Swap the left and right points. */
7074       temp = sortarray[left];
7075       sortarray[left] = sortarray[right];
7076       sortarray[right] = temp;
7077     }
7078   }
7079   /* Unlike in pointsort(), at most one of the following */
7080   /*   conditionals is true.                             */
7081   if (left > median) {
7082     /* Recursively shuffle the left subset. */
7083     pointmedian(sortarray, left, median, axis);
7084   }
7085   if (right < median - 1) {
7086     /* Recursively shuffle the right subset. */
7087     pointmedian(&sortarray[right + 1], arraysize - right - 1,
7088                 median - right - 1, axis);
7089   }
7090 }
7091
7092 /*****************************************************************************/
7093 /*                                                                           */
7094 /*  alternateaxes()   Sorts the points as appropriate for the divide-and-    */
7095 /*                    conquer algorithm with alternating cuts.               */
7096 /*                                                                           */
7097 /*  Partitions by x-coordinate if axis == 0; by y-coordinate if axis == 1.   */
7098 /*  For the base case, subsets containing only two or three points are       */
7099 /*  always sorted by x-coordinate.                                           */
7100 /*                                                                           */
7101 /*****************************************************************************/
7102
7103 void alternateaxes(sortarray, arraysize, axis)
7104 point *sortarray;
7105 int arraysize;
7106 int axis;
7107 {
7108   int divider;
7109
7110   divider = arraysize >> 1;
7111   if (arraysize <= 3) {
7112     /* Recursive base case:  subsets of two or three points will be      */
7113     /*   handled specially, and should always be sorted by x-coordinate. */
7114     axis = 0;
7115   }
7116   /* Partition with a horizontal or vertical cut. */
7117   pointmedian(sortarray, arraysize, divider, axis);
7118   /* Recursively partition the subsets with a cross cut. */
7119   if (arraysize - divider >= 2) {
7120     if (divider >= 2) {
7121       alternateaxes(sortarray, divider, 1 - axis);
7122     }
7123     alternateaxes(&sortarray[divider], arraysize - divider, 1 - axis);
7124   }
7125 }
7126
7127 /*****************************************************************************/
7128 /*                                                                           */
7129 /*  mergehulls()   Merge two adjacent Delaunay triangulations into a         */
7130 /*                 single Delaunay triangulation.                            */
7131 /*                                                                           */
7132 /*  This is similar to the algorithm given by Guibas and Stolfi, but uses    */
7133 /*  a triangle-based, rather than edge-based, data structure.                */
7134 /*                                                                           */
7135 /*  The algorithm walks up the gap between the two triangulations, knitting  */
7136 /*  them together.  As they are merged, some of their bounding triangles     */
7137 /*  are converted into real triangles of the triangulation.  The procedure   */
7138 /*  pulls each hull's bounding triangles apart, then knits them together     */
7139 /*  like the teeth of two gears.  The Delaunay property determines, at each  */
7140 /*  step, whether the next "tooth" is a bounding triangle of the left hull   */
7141 /*  or the right.  When a bounding triangle becomes real, its apex is        */
7142 /*  changed from NULL to a real point.                                       */
7143 /*                                                                           */
7144 /*  Only two new triangles need to be allocated.  These become new bounding  */
7145 /*  triangles at the top and bottom of the seam.  They are used to connect   */
7146 /*  the remaining bounding triangles (those that have not been converted     */
7147 /*  into real triangles) into a single fan.                                  */
7148 /*                                                                           */
7149 /*  On entry, `farleft' and `innerleft' are bounding triangles of the left   */
7150 /*  triangulation.  The origin of `farleft' is the leftmost vertex, and      */
7151 /*  the destination of `innerleft' is the rightmost vertex of the            */
7152 /*  triangulation.  Similarly, `innerright' and `farright' are bounding      */
7153 /*  triangles of the right triangulation.  The origin of `innerright' and    */
7154 /*  destination of `farright' are the leftmost and rightmost vertices.       */
7155 /*                                                                           */
7156 /*  On completion, the origin of `farleft' is the leftmost vertex of the     */
7157 /*  merged triangulation, and the destination of `farright' is the rightmost */
7158 /*  vertex.                                                                  */
7159 /*                                                                           */
7160 /*****************************************************************************/
7161
7162 void mergehulls(farleft, innerleft, innerright, farright, axis)
7163 struct triedge *farleft;
7164 struct triedge *innerleft;
7165 struct triedge *innerright;
7166 struct triedge *farright;
7167 int axis;
7168 {
7169   struct triedge leftcand, rightcand;
7170   struct triedge baseedge;
7171   struct triedge nextedge;
7172   struct triedge sidecasing, topcasing, outercasing;
7173   struct triedge checkedge;
7174   point innerleftdest;
7175   point innerrightorg;
7176   point innerleftapex, innerrightapex;
7177   point farleftpt, farrightpt;
7178   point farleftapex, farrightapex;
7179   point lowerleft, lowerright;
7180   point upperleft, upperright;
7181   point nextapex;
7182   point checkvertex;
7183   int changemade;
7184   int badedge;
7185   int leftfinished, rightfinished;
7186   triangle ptr;                         /* Temporary variable used by sym(). */
7187
7188   dest(*innerleft, innerleftdest);
7189   apex(*innerleft, innerleftapex);
7190   org(*innerright, innerrightorg);
7191   apex(*innerright, innerrightapex);
7192   /* Special treatment for horizontal cuts. */
7193   if (dwyer && (axis == 1)) {
7194     org(*farleft, farleftpt);
7195     apex(*farleft, farleftapex);
7196     dest(*farright, farrightpt);
7197     apex(*farright, farrightapex);
7198     /* The pointers to the extremal points are shifted to point to the */
7199     /*   topmost and bottommost point of each hull, rather than the    */
7200     /*   leftmost and rightmost points.                                */
7201     while (farleftapex[1] < farleftpt[1]) {
7202       lnextself(*farleft);
7203       symself(*farleft);
7204       farleftpt = farleftapex;
7205       apex(*farleft, farleftapex);
7206     }
7207     sym(*innerleft, checkedge);
7208     apex(checkedge, checkvertex);
7209     while (checkvertex[1] > innerleftdest[1]) {
7210       lnext(checkedge, *innerleft);
7211       innerleftapex = innerleftdest;
7212       innerleftdest = checkvertex;
7213       sym(*innerleft, checkedge);
7214       apex(checkedge, checkvertex);
7215     }
7216     while (innerrightapex[1] < innerrightorg[1]) {
7217       lnextself(*innerright);
7218       symself(*innerright);
7219       innerrightorg = innerrightapex;
7220       apex(*innerright, innerrightapex);
7221     }
7222     sym(*farright, checkedge);
7223     apex(checkedge, checkvertex);
7224     while (checkvertex[1] > farrightpt[1]) {
7225       lnext(checkedge, *farright);
7226       farrightapex = farrightpt;
7227       farrightpt = checkvertex;
7228       sym(*farright, checkedge);
7229       apex(checkedge, checkvertex);
7230     }
7231   }
7232   /* Find a line tangent to and below both hulls. */
7233   do {
7234     changemade = 0;
7235     /* Make innerleftdest the "bottommost" point of the left hull. */
7236     if (counterclockwise(innerleftdest, innerleftapex, innerrightorg) > 0.0) {
7237       lprevself(*innerleft);
7238       symself(*innerleft);
7239       innerleftdest = innerleftapex;
7240       apex(*innerleft, innerleftapex);
7241       changemade = 1;
7242     }
7243     /* Make innerrightorg the "bottommost" point of the right hull. */
7244     if (counterclockwise(innerrightapex, innerrightorg, innerleftdest) > 0.0) {
7245       lnextself(*innerright);
7246       symself(*innerright);
7247       innerrightorg = innerrightapex;
7248       apex(*innerright, innerrightapex);
7249       changemade = 1;
7250     }
7251   } while (changemade);
7252   /* Find the two candidates to be the next "gear tooth". */
7253   sym(*innerleft, leftcand);
7254   sym(*innerright, rightcand);
7255   /* Create the bottom new bounding triangle. */
7256   maketriangle(&baseedge);
7257   /* Connect it to the bounding boxes of the left and right triangulations. */
7258   bond(baseedge, *innerleft);
7259   lnextself(baseedge);
7260   bond(baseedge, *innerright);
7261   lnextself(baseedge);
7262   setorg(baseedge, innerrightorg);
7263   setdest(baseedge, innerleftdest);
7264   /* Apex is intentionally left NULL. */
7265   if (verbose > 2) {
7266     printf("  Creating base bounding ");
7267     printtriangle(&baseedge);
7268   }
7269   /* Fix the extreme triangles if necessary. */
7270   org(*farleft, farleftpt);
7271   if (innerleftdest == farleftpt) {
7272     lnext(baseedge, *farleft);
7273   }
7274   dest(*farright, farrightpt);
7275   if (innerrightorg == farrightpt) {
7276     lprev(baseedge, *farright);
7277   }
7278   /* The vertices of the current knitting edge. */
7279   lowerleft = innerleftdest;
7280   lowerright = innerrightorg;
7281   /* The candidate vertices for knitting. */
7282   apex(leftcand, upperleft);
7283   apex(rightcand, upperright);
7284   /* Walk up the gap between the two triangulations, knitting them together. */
7285   while (1) {
7286     /* Have we reached the top?  (This isn't quite the right question,       */
7287     /*   because even though the left triangulation might seem finished now, */
7288     /*   moving up on the right triangulation might reveal a new point of    */
7289     /*   the left triangulation.  And vice-versa.)                           */
7290     leftfinished = counterclockwise(upperleft, lowerleft, lowerright) <= 0.0;
7291     rightfinished = counterclockwise(upperright, lowerleft, lowerright) <= 0.0;
7292     if (leftfinished && rightfinished) {
7293       /* Create the top new bounding triangle. */
7294       maketriangle(&nextedge);
7295       setorg(nextedge, lowerleft);
7296       setdest(nextedge, lowerright);
7297       /* Apex is intentionally left NULL. */
7298       /* Connect it to the bounding boxes of the two triangulations. */
7299       bond(nextedge, baseedge);
7300       lnextself(nextedge);
7301       bond(nextedge, rightcand);
7302       lnextself(nextedge);
7303       bond(nextedge, leftcand);
7304       if (verbose > 2) {
7305         printf("  Creating top bounding ");
7306         printtriangle(&baseedge);
7307       }
7308       /* Special treatment for horizontal cuts. */
7309       if (dwyer && (axis == 1)) {
7310         org(*farleft, farleftpt);
7311         apex(*farleft, farleftapex);
7312         dest(*farright, farrightpt);
7313         apex(*farright, farrightapex);
7314         sym(*farleft, checkedge);
7315         apex(checkedge, checkvertex);
7316         /* The pointers to the extremal points are restored to the leftmost */
7317         /*   and rightmost points (rather than topmost and bottommost).     */
7318         while (checkvertex[0] < farleftpt[0]) {
7319           lprev(checkedge, *farleft);
7320           farleftapex = farleftpt;
7321           farleftpt = checkvertex;
7322           sym(*farleft, checkedge);
7323           apex(checkedge, checkvertex);
7324         }
7325         while (farrightapex[0] > farrightpt[0]) {
7326           lprevself(*farright);
7327           symself(*farright);
7328           farrightpt = farrightapex;
7329           apex(*farright, farrightapex);
7330         }
7331       }
7332       return;
7333     }
7334     /* Consider eliminating edges from the left triangulation. */
7335     if (!leftfinished) {
7336       /* What vertex would be exposed if an edge were deleted? */
7337       lprev(leftcand, nextedge);
7338       symself(nextedge);
7339       apex(nextedge, nextapex);
7340       /* If nextapex is NULL, then no vertex would be exposed; the */
7341       /*   triangulation would have been eaten right through.      */
7342       if (nextapex != (point) NULL) {
7343         /* Check whether the edge is Delaunay. */
7344         badedge = incircle(lowerleft, lowerright, upperleft, nextapex) > 0.0;
7345         while (badedge) {
7346           /* Eliminate the edge with an edge flip.  As a result, the    */
7347           /*   left triangulation will have one more boundary triangle. */
7348           lnextself(nextedge);
7349           sym(nextedge, topcasing);
7350           lnextself(nextedge);
7351           sym(nextedge, sidecasing);
7352           bond(nextedge, topcasing);
7353           bond(leftcand, sidecasing);
7354           lnextself(leftcand);
7355           sym(leftcand, outercasing);
7356           lprevself(nextedge);
7357           bond(nextedge, outercasing);
7358           /* Correct the vertices to reflect the edge flip. */
7359           setorg(leftcand, lowerleft);
7360           setdest(leftcand, NULL);
7361           setapex(leftcand, nextapex);
7362           setorg(nextedge, NULL);
7363           setdest(nextedge, upperleft);
7364           setapex(nextedge, nextapex);
7365           /* Consider the newly exposed vertex. */
7366           upperleft = nextapex;
7367           /* What vertex would be exposed if another edge were deleted? */
7368           triedgecopy(sidecasing, nextedge);
7369           apex(nextedge, nextapex);
7370           if (nextapex != (point) NULL) {
7371             /* Check whether the edge is Delaunay. */
7372             badedge = incircle(lowerleft, lowerright, upperleft, nextapex)
7373                       > 0.0;
7374           } else {
7375             /* Avoid eating right through the triangulation. */
7376             badedge = 0;
7377           }
7378         }
7379       }
7380     }
7381     /* Consider eliminating edges from the right triangulation. */
7382     if (!rightfinished) {
7383       /* What vertex would be exposed if an edge were deleted? */
7384       lnext(rightcand, nextedge);
7385       symself(nextedge);
7386       apex(nextedge, nextapex);
7387       /* If nextapex is NULL, then no vertex would be exposed; the */
7388       /*   triangulation would have been eaten right through.      */
7389       if (nextapex != (point) NULL) {
7390         /* Check whether the edge is Delaunay. */
7391         badedge = incircle(lowerleft, lowerright, upperright, nextapex) > 0.0;
7392         while (badedge) {
7393           /* Eliminate the edge with an edge flip.  As a result, the     */
7394           /*   right triangulation will have one more boundary triangle. */
7395           lprevself(nextedge);
7396           sym(nextedge, topcasing);
7397           lprevself(nextedge);
7398           sym(nextedge, sidecasing);
7399           bond(nextedge, topcasing);
7400           bond(rightcand, sidecasing);
7401           lprevself(rightcand);
7402           sym(rightcand, outercasing);
7403           lnextself(nextedge);
7404           bond(nextedge, outercasing);
7405           /* Correct the vertices to reflect the edge flip. */
7406           setorg(rightcand, NULL);
7407           setdest(rightcand, lowerright);
7408           setapex(rightcand, nextapex);
7409           setorg(nextedge, upperright);
7410           setdest(nextedge, NULL);
7411           setapex(nextedge, nextapex);
7412           /* Consider the newly exposed vertex. */
7413           upperright = nextapex;
7414           /* What vertex would be exposed if another edge were deleted? */
7415           triedgecopy(sidecasing, nextedge);
7416           apex(nextedge, nextapex);
7417           if (nextapex != (point) NULL) {
7418             /* Check whether the edge is Delaunay. */
7419             badedge = incircle(lowerleft, lowerright, upperright, nextapex)
7420                       > 0.0;
7421           } else {
7422             /* Avoid eating right through the triangulation. */
7423             badedge = 0;
7424           }
7425         }
7426       }
7427     }
7428     if (leftfinished || (!rightfinished &&
7429            (incircle(upperleft, lowerleft, lowerright, upperright) > 0.0))) {
7430       /* Knit the triangulations, adding an edge from `lowerleft' */
7431       /*   to `upperright'.                                       */
7432       bond(baseedge, rightcand);
7433       lprev(rightcand, baseedge);
7434       setdest(baseedge, lowerleft);
7435       lowerright = upperright;
7436       sym(baseedge, rightcand);
7437       apex(rightcand, upperright);
7438     } else {
7439       /* Knit the triangulations, adding an edge from `upperleft' */
7440       /*   to `lowerright'.                                       */
7441       bond(baseedge, leftcand);
7442       lnext(leftcand, baseedge);
7443       setorg(baseedge, lowerright);
7444       lowerleft = upperleft;
7445       sym(baseedge, leftcand);
7446       apex(leftcand, upperleft);
7447     }
7448     if (verbose > 2) {
7449       printf("  Connecting ");
7450       printtriangle(&baseedge);
7451     }
7452   }
7453 }
7454
7455 /*****************************************************************************/
7456 /*                                                                           */
7457 /*  divconqrecurse()   Recursively form a Delaunay triangulation by the      */
7458 /*                     divide-and-conquer method.                            */
7459 /*                                                                           */
7460 /*  Recursively breaks down the problem into smaller pieces, which are       */
7461 /*  knitted together by mergehulls().  The base cases (problems of two or    */
7462 /*  three points) are handled specially here.                                */
7463 /*                                                                           */
7464 /*  On completion, `farleft' and `farright' are bounding triangles such that */
7465 /*  the origin of `farleft' is the leftmost vertex (breaking ties by         */
7466 /*  choosing the highest leftmost vertex), and the destination of            */
7467 /*  `farright' is the rightmost vertex (breaking ties by choosing the        */
7468 /*  lowest rightmost vertex).                                                */
7469 /*                                                                           */
7470 /*****************************************************************************/
7471
7472 void divconqrecurse(sortarray, vertices, axis, farleft, farright)
7473 point *sortarray;
7474 int vertices;
7475 int axis;
7476 struct triedge *farleft;
7477 struct triedge *farright;
7478 {
7479   struct triedge midtri, tri1, tri2, tri3;
7480   struct triedge innerleft, innerright;
7481   REAL area;
7482   int divider;
7483
7484   if (verbose > 2) {
7485     printf("  Triangulating %d points.\n", vertices);
7486   }
7487   if (vertices == 2) {
7488     /* The triangulation of two vertices is an edge.  An edge is */
7489     /*   represented by two bounding triangles.                  */
7490     maketriangle(farleft);
7491     setorg(*farleft, sortarray[0]);
7492     setdest(*farleft, sortarray[1]);
7493     /* The apex is intentionally left NULL. */
7494     maketriangle(farright);
7495     setorg(*farright, sortarray[1]);
7496     setdest(*farright, sortarray[0]);
7497     /* The apex is intentionally left NULL. */
7498     bond(*farleft, *farright);
7499     lprevself(*farleft);
7500     lnextself(*farright);
7501     bond(*farleft, *farright);
7502     lprevself(*farleft);
7503     lnextself(*farright);
7504     bond(*farleft, *farright);
7505     if (verbose > 2) {
7506       printf("  Creating ");
7507       printtriangle(farleft);
7508       printf("  Creating ");
7509       printtriangle(farright);
7510     }
7511     /* Ensure that the origin of `farleft' is sortarray[0]. */
7512     lprev(*farright, *farleft);
7513     return;
7514   } else if (vertices == 3) {
7515     /* The triangulation of three vertices is either a triangle (with */
7516     /*   three bounding triangles) or two edges (with four bounding   */
7517     /*   triangles).  In either case, four triangles are created.     */
7518     maketriangle(&midtri);
7519     maketriangle(&tri1);
7520     maketriangle(&tri2);
7521     maketriangle(&tri3);
7522     area = counterclockwise(sortarray[0], sortarray[1], sortarray[2]);
7523     if (area == 0.0) {
7524       /* Three collinear points; the triangulation is two edges. */
7525       setorg(midtri, sortarray[0]);
7526       setdest(midtri, sortarray[1]);
7527       setorg(tri1, sortarray[1]);
7528       setdest(tri1, sortarray[0]);
7529       setorg(tri2, sortarray[2]);
7530       setdest(tri2, sortarray[1]);
7531       setorg(tri3, sortarray[1]);
7532       setdest(tri3, sortarray[2]);
7533       /* All apices are intentionally left NULL. */
7534       bond(midtri, tri1);
7535       bond(tri2, tri3);
7536       lnextself(midtri);
7537       lprevself(tri1);
7538       lnextself(tri2);
7539       lprevself(tri3);
7540       bond(midtri, tri3);
7541       bond(tri1, tri2);
7542       lnextself(midtri);
7543       lprevself(tri1);
7544       lnextself(tri2);
7545       lprevself(tri3);
7546       bond(midtri, tri1);
7547       bond(tri2, tri3);
7548       /* Ensure that the origin of `farleft' is sortarray[0]. */
7549       triedgecopy(tri1, *farleft);
7550       /* Ensure that the destination of `farright' is sortarray[2]. */
7551       triedgecopy(tri2, *farright);
7552     } else {
7553       /* The three points are not collinear; the triangulation is one */
7554       /*   triangle, namely `midtri'.                                 */
7555       setorg(midtri, sortarray[0]);
7556       setdest(tri1, sortarray[0]);
7557       setorg(tri3, sortarray[0]);
7558       /* Apices of tri1, tri2, and tri3 are left NULL. */
7559       if (area > 0.0) {
7560         /* The vertices are in counterclockwise order. */
7561         setdest(midtri, sortarray[1]);
7562         setorg(tri1, sortarray[1]);
7563         setdest(tri2, sortarray[1]);
7564         setapex(midtri, sortarray[2]);
7565         setorg(tri2, sortarray[2]);
7566         setdest(tri3, sortarray[2]);
7567       } else {
7568         /* The vertices are in clockwise order. */
7569         setdest(midtri, sortarray[2]);
7570         setorg(tri1, sortarray[2]);
7571         setdest(tri2, sortarray[2]);
7572         setapex(midtri, sortarray[1]);
7573         setorg(tri2, sortarray[1]);
7574         setdest(tri3, sortarray[1]);
7575       }
7576       /* The topology does not depend on how the vertices are ordered. */
7577       bond(midtri, tri1);
7578       lnextself(midtri);
7579       bond(midtri, tri2);
7580       lnextself(midtri);
7581       bond(midtri, tri3);
7582       lprevself(tri1);
7583       lnextself(tri2);
7584       bond(tri1, tri2);
7585       lprevself(tri1);
7586       lprevself(tri3);
7587       bond(tri1, tri3);
7588       lnextself(tri2);
7589       lprevself(tri3);
7590       bond(tri2, tri3);
7591       /* Ensure that the origin of `farleft' is sortarray[0]. */
7592       triedgecopy(tri1, *farleft);
7593       /* Ensure that the destination of `farright' is sortarray[2]. */
7594       if (area > 0.0) {
7595         triedgecopy(tri2, *farright);
7596       } else {
7597         lnext(*farleft, *farright);
7598       }
7599     }
7600     if (verbose > 2) {
7601       printf("  Creating ");
7602       printtriangle(&midtri);
7603       printf("  Creating ");
7604       printtriangle(&tri1);
7605       printf("  Creating ");
7606       printtriangle(&tri2);
7607       printf("  Creating ");
7608       printtriangle(&tri3);
7609     }
7610     return;
7611   } else {
7612     /* Split the vertices in half. */
7613     divider = vertices >> 1;
7614     /* Recursively triangulate each half. */
7615     divconqrecurse(sortarray, divider, 1 - axis, farleft, &innerleft);
7616     divconqrecurse(&sortarray[divider], vertices - divider, 1 - axis,
7617                    &innerright, farright);
7618     if (verbose > 1) {
7619       printf("  Joining triangulations with %d and %d vertices.\n", divider,
7620              vertices - divider);
7621     }
7622     /* Merge the two triangulations into one. */
7623     mergehulls(farleft, &innerleft, &innerright, farright, axis);
7624   }
7625 }
7626
7627 long removeghosts(startghost)
7628 struct triedge *startghost;
7629 {
7630   struct triedge searchedge;
7631   struct triedge dissolveedge;
7632   struct triedge deadtri;
7633   point markorg;
7634   long hullsize;
7635   triangle ptr;                         /* Temporary variable used by sym(). */
7636
7637   if (verbose) {
7638     printf("  Removing ghost triangles.\n");
7639   }
7640   /* Find an edge on the convex hull to start point location from. */
7641   lprev(*startghost, searchedge);
7642   symself(searchedge);
7643   dummytri[0] = encode(searchedge);
7644   /* Remove the bounding box and count the convex hull edges. */
7645   triedgecopy(*startghost, dissolveedge);
7646   hullsize = 0;
7647   do {
7648     hullsize++;
7649     lnext(dissolveedge, deadtri);
7650     lprevself(dissolveedge);
7651     symself(dissolveedge);
7652     /* If no PSLG is involved, set the boundary markers of all the points */
7653     /*   on the convex hull.  If a PSLG is used, this step is done later. */
7654     if (!poly) {
7655       /* Watch out for the case where all the input points are collinear. */
7656       if (dissolveedge.tri != dummytri) {
7657         org(dissolveedge, markorg);
7658         if (pointmark(markorg) == 0) {
7659           setpointmark(markorg, 1);
7660         }
7661       }
7662     }
7663     /* Remove a bounding triangle from a convex hull triangle. */
7664     dissolve(dissolveedge);
7665     /* Find the next bounding triangle. */
7666     sym(deadtri, dissolveedge);
7667     /* Delete the bounding triangle. */
7668     triangledealloc(deadtri.tri);
7669   } while (!triedgeequal(dissolveedge, *startghost));
7670   return hullsize;
7671 }
7672
7673 /*****************************************************************************/
7674 /*                                                                           */
7675 /*  divconqdelaunay()   Form a Delaunay triangulation by the divide-and-     */
7676 /*                      conquer method.                                      */
7677 /*                                                                           */
7678 /*  Sorts the points, calls a recursive procedure to triangulate them, and   */
7679 /*  removes the bounding box, setting boundary markers as appropriate.       */
7680 /*                                                                           */
7681 /*****************************************************************************/
7682
7683 long divconqdelaunay()
7684 {
7685   point *sortarray;
7686   struct triedge hullleft, hullright;
7687   int divider;
7688   int i, j;
7689
7690   /* Allocate an array of pointers to points for sorting. */
7691   sortarray = (point *) malloc(inpoints * sizeof(point));
7692   if (sortarray == (point *) NULL) {
7693     printf("Error:  Out of memory.\n");
7694     exit(1);
7695   }
7696   traversalinit(&points);
7697   for (i = 0; i < inpoints; i++) {
7698     sortarray[i] = pointtraverse();
7699   }
7700   if (verbose) {
7701     printf("  Sorting points.\n");
7702   }
7703   /* Sort the points. */
7704   pointsort(sortarray, inpoints);
7705   /* Discard duplicate points, which can really mess up the algorithm. */
7706   i = 0;
7707   for (j = 1; j < inpoints; j++) {
7708     if ((sortarray[i][0] == sortarray[j][0])
7709         && (sortarray[i][1] == sortarray[j][1])) {
7710       if (!quiet) {
7711         printf(
7712 "Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
7713                sortarray[j][0], sortarray[j][1]);
7714       }
7715 /*  Commented out - would eliminate point from output .node file, but causes
7716     a failure if some segment has this point as an endpoint.
7717       setpointmark(sortarray[j], DEADPOINT);
7718 */
7719     } else {
7720       i++;
7721       sortarray[i] = sortarray[j];
7722     }
7723   }
7724   i++;
7725   if (dwyer) {
7726     /* Re-sort the array of points to accommodate alternating cuts. */
7727     divider = i >> 1;
7728     if (i - divider >= 2) {
7729       if (divider >= 2) {
7730         alternateaxes(sortarray, divider, 1);
7731       }
7732       alternateaxes(&sortarray[divider], i - divider, 1);
7733     }
7734   }
7735   if (verbose) {
7736     printf("  Forming triangulation.\n");
7737   }
7738   /* Form the Delaunay triangulation. */
7739   divconqrecurse(sortarray, i, 0, &hullleft, &hullright);
7740   free(sortarray);
7741
7742   return removeghosts(&hullleft);
7743 }
7744
7745 /**                                                                         **/
7746 /**                                                                         **/
7747 /********* Divide-and-conquer Delaunay triangulation ends here       *********/
7748
7749 /********* Incremental Delaunay triangulation begins here            *********/
7750 /**                                                                         **/
7751 /**                                                                         **/
7752
7753 /*****************************************************************************/
7754 /*                                                                           */
7755 /*  boundingbox()   Form an "infinite" bounding triangle to insert points    */
7756 /*                  into.                                                    */
7757 /*                                                                           */
7758 /*  The points at "infinity" are assigned finite coordinates, which are used */
7759 /*  by the point location routines, but (mostly) ignored by the Delaunay     */
7760 /*  edge flip routines.                                                      */
7761 /*                                                                           */
7762 /*****************************************************************************/
7763
7764 #ifndef REDUCED
7765
7766 void boundingbox()
7767 {
7768   struct triedge inftri;          /* Handle for the triangular bounding box. */
7769   REAL width;
7770
7771   if (verbose) {
7772     printf("  Creating triangular bounding box.\n");
7773   }
7774   /* Find the width (or height, whichever is larger) of the triangulation. */
7775   width = xmax - xmin;
7776   if (ymax - ymin > width) {
7777     width = ymax - ymin;
7778   }
7779   if (width == 0.0) {
7780     width = 1.0;
7781   }
7782   /* Create the vertices of the bounding box. */
7783   infpoint1 = (point) malloc(points.itembytes);
7784   infpoint2 = (point) malloc(points.itembytes);
7785   infpoint3 = (point) malloc(points.itembytes);
7786   if ((infpoint1 == (point) NULL) || (infpoint2 == (point) NULL)
7787       || (infpoint3 == (point) NULL)) {
7788     printf("Error:  Out of memory.\n");
7789     exit(1);
7790   }
7791   infpoint1[0] = xmin - 50.0 * width;
7792   infpoint1[1] = ymin - 40.0 * width;
7793   infpoint2[0] = xmax + 50.0 * width;
7794   infpoint2[1] = ymin - 40.0 * width;
7795   infpoint3[0] = 0.5 * (xmin + xmax);
7796   infpoint3[1] = ymax + 60.0 * width;
7797
7798   /* Create the bounding box. */
7799   maketriangle(&inftri);
7800   setorg(inftri, infpoint1);
7801   setdest(inftri, infpoint2);
7802   setapex(inftri, infpoint3);
7803   /* Link dummytri to the bounding box so we can always find an */
7804   /*   edge to begin searching (point location) from.           */
7805   dummytri[0] = (triangle) inftri.tri;
7806   if (verbose > 2) {
7807     printf("  Creating ");
7808     printtriangle(&inftri);
7809   }
7810 }
7811
7812 #endif /* not REDUCED */
7813
7814 /*****************************************************************************/
7815 /*                                                                           */
7816 /*  removebox()   Remove the "infinite" bounding triangle, setting boundary  */
7817 /*                markers as appropriate.                                    */
7818 /*                                                                           */
7819 /*  The triangular bounding box has three boundary triangles (one for each   */
7820 /*  side of the bounding box), and a bunch of triangles fanning out from     */
7821 /*  the three bounding box vertices (one triangle for each edge of the       */
7822 /*  convex hull of the inner mesh).  This routine removes these triangles.   */
7823 /*                                                                           */
7824 /*****************************************************************************/
7825
7826 #ifndef REDUCED
7827
7828 long removebox()
7829 {
7830   struct triedge deadtri;
7831   struct triedge searchedge;
7832   struct triedge checkedge;
7833   struct triedge nextedge, finaledge, dissolveedge;
7834   point markorg;
7835   long hullsize;
7836   triangle ptr;                         /* Temporary variable used by sym(). */
7837
7838   if (verbose) {
7839     printf("  Removing triangular bounding box.\n");
7840   }
7841   /* Find a boundary triangle. */
7842   nextedge.tri = dummytri;
7843   nextedge.orient = 0;
7844   symself(nextedge);
7845   /* Mark a place to stop. */
7846   lprev(nextedge, finaledge);
7847   lnextself(nextedge);
7848   symself(nextedge);
7849   /* Find a triangle (on the boundary of the point set) that isn't */
7850   /*   a bounding box triangle.                                    */
7851   lprev(nextedge, searchedge);
7852   symself(searchedge);
7853   /* Check whether nextedge is another boundary triangle */
7854   /*   adjacent to the first one.                        */
7855   lnext(nextedge, checkedge);
7856   symself(checkedge);
7857   if (checkedge.tri == dummytri) {
7858     /* Go on to the next triangle.  There are only three boundary   */
7859     /*   triangles, and this next triangle cannot be the third one, */
7860     /*   so it's safe to stop here.                                 */
7861     lprevself(searchedge);
7862     symself(searchedge);
7863   }
7864   /* Find a new boundary edge to search from, as the current search */
7865   /*   edge lies on a bounding box triangle and will be deleted.    */
7866   dummytri[0] = encode(searchedge);
7867   hullsize = -2l;
7868   while (!triedgeequal(nextedge, finaledge)) {
7869     hullsize++;
7870     lprev(nextedge, dissolveedge);
7871     symself(dissolveedge);
7872     /* If not using a PSLG, the vertices should be marked now. */
7873     /*   (If using a PSLG, markhull() will do the job.)        */
7874     if (!poly) {
7875       /* Be careful!  One must check for the case where all the input   */
7876       /*   points are collinear, and thus all the triangles are part of */
7877       /*   the bounding box.  Otherwise, the setpointmark() call below  */
7878       /*   will cause a bad pointer reference.                          */
7879       if (dissolveedge.tri != dummytri) {
7880         org(dissolveedge, markorg);
7881         if (pointmark(markorg) == 0) {
7882           setpointmark(markorg, 1);
7883         }
7884       }
7885     }
7886     /* Disconnect the bounding box triangle from the mesh triangle. */
7887     dissolve(dissolveedge);
7888     lnext(nextedge, deadtri);
7889     sym(deadtri, nextedge);
7890     /* Get rid of the bounding box triangle. */
7891     triangledealloc(deadtri.tri);
7892     /* Do we need to turn the corner? */
7893     if (nextedge.tri == dummytri) {
7894       /* Turn the corner. */
7895       triedgecopy(dissolveedge, nextedge);
7896     }
7897   }
7898   triangledealloc(finaledge.tri);
7899
7900   free(infpoint1);                  /* Deallocate the bounding box vertices. */
7901   free(infpoint2);
7902   free(infpoint3);
7903
7904   return hullsize;
7905 }
7906
7907 #endif /* not REDUCED */
7908
7909 /*****************************************************************************/
7910 /*                                                                           */
7911 /*  incrementaldelaunay()   Form a Delaunay triangulation by incrementally   */
7912 /*                          adding vertices.                                 */
7913 /*                                                                           */
7914 /*****************************************************************************/
7915
7916 #ifndef REDUCED
7917
7918 long incrementaldelaunay()
7919 {
7920   struct triedge starttri;
7921   point pointloop;
7922   int i;
7923
7924   /* Create a triangular bounding box. */
7925   boundingbox();
7926   if (verbose) {
7927     printf("  Incrementally inserting points.\n");
7928   }
7929   traversalinit(&points);
7930   pointloop = pointtraverse();
7931   i = 1;
7932   while (pointloop != (point) NULL) {
7933     /* Find a boundary triangle to search from. */
7934     starttri.tri = (triangle *) NULL;
7935     if (insertsite(pointloop, &starttri, (struct edge *) NULL, 0, 0) ==
7936         DUPLICATEPOINT) {
7937       if (!quiet) {
7938         printf(
7939 "Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
7940                pointloop[0], pointloop[1]);
7941       }
7942 /*  Commented out - would eliminate point from output .node file.
7943       setpointmark(pointloop, DEADPOINT);
7944 */
7945     }
7946     pointloop = pointtraverse();
7947     i++;
7948   }
7949   /* Remove the bounding box. */
7950   return removebox();
7951 }
7952
7953 #endif /* not REDUCED */
7954
7955 /**                                                                         **/
7956 /**                                                                         **/
7957 /********* Incremental Delaunay triangulation ends here              *********/
7958
7959 /********* Sweepline Delaunay triangulation begins here              *********/
7960 /**                                                                         **/
7961 /**                                                                         **/
7962
7963 #ifndef REDUCED
7964
7965 void eventheapinsert(heap, heapsize, newevent)
7966 struct event **heap;
7967 int heapsize;
7968 struct event *newevent;
7969 {
7970   REAL eventx, eventy;
7971   int eventnum;
7972   int parent;
7973   int notdone;
7974
7975   eventx = newevent->xkey;
7976   eventy = newevent->ykey;
7977   eventnum = heapsize;
7978   notdone = eventnum > 0;
7979   while (notdone) {
7980     parent = (eventnum - 1) >> 1;
7981     if ((heap[parent]->ykey < eventy) ||
7982         ((heap[parent]->ykey == eventy)
7983          && (heap[parent]->xkey <= eventx))) {
7984       notdone = 0;
7985     } else {
7986       heap[eventnum] = heap[parent];
7987       heap[eventnum]->heapposition = eventnum;
7988
7989       eventnum = parent;
7990       notdone = eventnum > 0;
7991     }
7992   }
7993   heap[eventnum] = newevent;
7994   newevent->heapposition = eventnum;
7995 }
7996
7997 #endif /* not REDUCED */
7998
7999 #ifndef REDUCED
8000
8001 void eventheapify(heap, heapsize, eventnum)
8002 struct event **heap;
8003 int heapsize;
8004 int eventnum;
8005 {
8006   struct event *thisevent;
8007   REAL eventx, eventy;
8008   int leftchild, rightchild;
8009   int smallest;
8010   int notdone;
8011
8012   thisevent = heap[eventnum];
8013   eventx = thisevent->xkey;
8014   eventy = thisevent->ykey;
8015   leftchild = 2 * eventnum + 1;
8016   notdone = leftchild < heapsize;
8017   while (notdone) {
8018     if ((heap[leftchild]->ykey < eventy) ||
8019         ((heap[leftchild]->ykey == eventy)
8020          && (heap[leftchild]->xkey < eventx))) {
8021       smallest = leftchild;
8022     } else {
8023       smallest = eventnum;
8024     }
8025     rightchild = leftchild + 1;
8026     if (rightchild < heapsize) {
8027       if ((heap[rightchild]->ykey < heap[smallest]->ykey) ||
8028           ((heap[rightchild]->ykey == heap[smallest]->ykey)
8029            && (heap[rightchild]->xkey < heap[smallest]->xkey))) {
8030         smallest = rightchild;
8031       }
8032     }
8033     if (smallest == eventnum) {
8034       notdone = 0;
8035     } else {
8036       heap[eventnum] = heap[smallest];
8037       heap[eventnum]->heapposition = eventnum;
8038       heap[smallest] = thisevent;
8039       thisevent->heapposition = smallest;
8040
8041       eventnum = smallest;
8042       leftchild = 2 * eventnum + 1;
8043       notdone = leftchild < heapsize;
8044     }
8045   }
8046 }
8047
8048 #endif /* not REDUCED */
8049
8050 #ifndef REDUCED
8051
8052 void eventheapdelete(heap, heapsize, eventnum)
8053 struct event **heap;
8054 int heapsize;
8055 int eventnum;
8056 {
8057   struct event *moveevent;
8058   REAL eventx, eventy;
8059   int parent;
8060   int notdone;
8061
8062   moveevent = heap[heapsize - 1];
8063   if (eventnum > 0) {
8064     eventx = moveevent->xkey;
8065     eventy = moveevent->ykey;
8066     do {
8067       parent = (eventnum - 1) >> 1;
8068       if ((heap[parent]->ykey < eventy) ||
8069           ((heap[parent]->ykey == eventy)
8070            && (heap[parent]->xkey <= eventx))) {
8071         notdone = 0;
8072       } else {
8073         heap[eventnum] = heap[parent];
8074         heap[eventnum]->heapposition = eventnum;
8075
8076         eventnum = parent;
8077         notdone = eventnum > 0;
8078       }
8079     } while (notdone);
8080   }
8081   heap[eventnum] = moveevent;
8082   moveevent->heapposition = eventnum;
8083   eventheapify(heap, heapsize - 1, eventnum);
8084 }
8085
8086 #endif /* not REDUCED */
8087
8088 #ifndef REDUCED
8089
8090 void createeventheap(eventheap, events, freeevents)
8091 struct event ***eventheap;
8092 struct event **events;
8093 struct event **freeevents;
8094 {
8095   point thispoint;
8096   int maxevents;
8097   int i;
8098
8099   maxevents = (3 * inpoints) / 2;
8100   *eventheap = (struct event **) malloc(maxevents * sizeof(struct event *));
8101   if (*eventheap == (struct event **) NULL) {
8102     printf("Error:  Out of memory.\n");
8103     exit(1);
8104   }
8105   *events = (struct event *) malloc(maxevents * sizeof(struct event));
8106   if (*events == (struct event *) NULL) {
8107     printf("Error:  Out of memory.\n");
8108     exit(1);
8109   }
8110   traversalinit(&points);
8111   for (i = 0; i < inpoints; i++) {
8112     thispoint = pointtraverse();
8113     (*events)[i].eventptr = (VOID *) thispoint;
8114     (*events)[i].xkey = thispoint[0];
8115     (*events)[i].ykey = thispoint[1];
8116     eventheapinsert(*eventheap, i, *events + i);
8117   }
8118   *freeevents = (struct event *) NULL;
8119   for (i = maxevents - 1; i >= inpoints; i--) {
8120     (*events)[i].eventptr = (VOID *) *freeevents;
8121     *freeevents = *events + i;
8122   }
8123 }
8124
8125 #endif /* not REDUCED */
8126
8127 #ifndef REDUCED
8128
8129 int rightofhyperbola(fronttri, newsite)
8130 struct triedge *fronttri;
8131 point newsite;
8132 {
8133   point leftpoint, rightpoint;
8134   REAL dxa, dya, dxb, dyb;
8135
8136   hyperbolacount++;
8137
8138   dest(*fronttri, leftpoint);
8139   apex(*fronttri, rightpoint);
8140   if ((leftpoint[1] < rightpoint[1])
8141       || ((leftpoint[1] == rightpoint[1]) && (leftpoint[0] < rightpoint[0]))) {
8142     if (newsite[0] >= rightpoint[0]) {
8143       return 1;
8144     }
8145   } else {
8146     if (newsite[0] <= leftpoint[0]) {
8147       return 0;
8148     }
8149   }
8150   dxa = leftpoint[0] - newsite[0];
8151   dya = leftpoint[1] - newsite[1];
8152   dxb = rightpoint[0] - newsite[0];
8153   dyb = rightpoint[1] - newsite[1];
8154   return dya * (dxb * dxb + dyb * dyb) > dyb * (dxa * dxa + dya * dya);
8155 }
8156
8157 #endif /* not REDUCED */
8158
8159 #ifndef REDUCED
8160
8161 REAL circletop(pa, pb, pc, ccwabc)
8162 point pa;
8163 point pb;
8164 point pc;
8165 REAL ccwabc;
8166 {
8167   REAL xac, yac, xbc, ybc, xab, yab;
8168   REAL aclen2, bclen2, ablen2;
8169
8170   circletopcount++;
8171
8172   xac = pa[0] - pc[0];
8173   yac = pa[1] - pc[1];
8174   xbc = pb[0] - pc[0];
8175   ybc = pb[1] - pc[1];
8176   xab = pa[0] - pb[0];
8177   yab = pa[1] - pb[1];
8178   aclen2 = xac * xac + yac * yac;
8179   bclen2 = xbc * xbc + ybc * ybc;
8180   ablen2 = xab * xab + yab * yab;
8181   return pc[1] + (xac * bclen2 - xbc * aclen2 + sqrt(aclen2 * bclen2 * ablen2))
8182                / (2.0 * ccwabc);
8183 }
8184
8185 #endif /* not REDUCED */
8186
8187 #ifndef REDUCED
8188
8189 void check4deadevent(checktri, freeevents, eventheap, heapsize)
8190 struct triedge *checktri;
8191 struct event **freeevents;
8192 struct event **eventheap;
8193 int *heapsize;
8194 {
8195   struct event *deadevent;
8196   point eventpoint;
8197   int eventnum;
8198
8199   org(*checktri, eventpoint);
8200   if (eventpoint != (point) NULL) {
8201     deadevent = (struct event *) eventpoint;
8202     eventnum = deadevent->heapposition;
8203     deadevent->eventptr = (VOID *) *freeevents;
8204     *freeevents = deadevent;
8205     eventheapdelete(eventheap, *heapsize, eventnum);
8206     (*heapsize)--;
8207     setorg(*checktri, NULL);
8208   }
8209 }
8210
8211 #endif /* not REDUCED */
8212
8213 #ifndef REDUCED
8214
8215 struct splaynode *splay(splaytree, searchpoint, searchtri)
8216 struct splaynode *splaytree;
8217 point searchpoint;
8218 struct triedge *searchtri;
8219 {
8220   struct splaynode *child, *grandchild;
8221   struct splaynode *lefttree, *righttree;
8222   struct splaynode *leftright;
8223   point checkpoint;
8224   int rightofroot, rightofchild;
8225
8226   if (splaytree == (struct splaynode *) NULL) {
8227     return (struct splaynode *) NULL;
8228   }
8229   dest(splaytree->keyedge, checkpoint);
8230   if (checkpoint == splaytree->keydest) {
8231     rightofroot = rightofhyperbola(&splaytree->keyedge, searchpoint);
8232     if (rightofroot) {
8233       triedgecopy(splaytree->keyedge, *searchtri);
8234       child = splaytree->rchild;
8235     } else {
8236       child = splaytree->lchild;
8237     }
8238     if (child == (struct splaynode *) NULL) {
8239       return splaytree;
8240     }
8241     dest(child->keyedge, checkpoint);
8242     if (checkpoint != child->keydest) {
8243       child = splay(child, searchpoint, searchtri);
8244       if (child == (struct splaynode *) NULL) {
8245         if (rightofroot) {
8246           splaytree->rchild = (struct splaynode *) NULL;
8247         } else {
8248           splaytree->lchild = (struct splaynode *) NULL;
8249         }
8250         return splaytree;
8251       }
8252     }
8253     rightofchild = rightofhyperbola(&child->keyedge, searchpoint);
8254     if (rightofchild) {
8255       triedgecopy(child->keyedge, *searchtri);
8256       grandchild = splay(child->rchild, searchpoint, searchtri);
8257       child->rchild = grandchild;
8258     } else {
8259       grandchild = splay(child->lchild, searchpoint, searchtri);
8260       child->lchild = grandchild;
8261     }
8262     if (grandchild == (struct splaynode *) NULL) {
8263       if (rightofroot) {
8264         splaytree->rchild = child->lchild;
8265         child->lchild = splaytree;
8266       } else {
8267         splaytree->lchild = child->rchild;
8268         child->rchild = splaytree;
8269       }
8270       return child;
8271     }
8272     if (rightofchild) {
8273       if (rightofroot) {
8274         splaytree->rchild = child->lchild;
8275         child->lchild = splaytree;
8276       } else {
8277         splaytree->lchild = grandchild->rchild;
8278         grandchild->rchild = splaytree;
8279       }
8280       child->rchild = grandchild->lchild;
8281       grandchild->lchild = child;
8282     } else {
8283       if (rightofroot) {
8284         splaytree->rchild = grandchild->lchild;
8285         grandchild->lchild = splaytree;
8286       } else {
8287         splaytree->lchild = child->rchild;
8288         child->rchild = splaytree;
8289       }
8290       child->lchild = grandchild->rchild;
8291       grandchild->rchild = child;
8292     }
8293     return grandchild;
8294   } else {
8295     lefttree = splay(splaytree->lchild, searchpoint, searchtri);
8296     righttree = splay(splaytree->rchild, searchpoint, searchtri);
8297
8298     pooldealloc(&splaynodes, (VOID *) splaytree);
8299     if (lefttree == (struct splaynode *) NULL) {
8300       return righttree;
8301     } else if (righttree == (struct splaynode *) NULL) {
8302       return lefttree;
8303     } else if (lefttree->rchild == (struct splaynode *) NULL) {
8304       lefttree->rchild = righttree->lchild;
8305       righttree->lchild = lefttree;
8306       return righttree;
8307     } else if (righttree->lchild == (struct splaynode *) NULL) {
8308       righttree->lchild = lefttree->rchild;
8309       lefttree->rchild = righttree;
8310       return lefttree;
8311     } else {
8312 /*      printf("Holy Toledo!!!\n"); */
8313       leftright = lefttree->rchild;
8314       while (leftright->rchild != (struct splaynode *) NULL) {
8315         leftright = leftright->rchild;
8316       }
8317       leftright->rchild = righttree;
8318       return lefttree;
8319     }
8320   }
8321 }
8322
8323 #endif /* not REDUCED */
8324
8325 #ifndef REDUCED
8326
8327 struct splaynode *splayinsert(splayroot, newkey, searchpoint)
8328 struct splaynode *splayroot;
8329 struct triedge *newkey;
8330 point searchpoint;
8331 {
8332   struct splaynode *newsplaynode;
8333
8334   newsplaynode = (struct splaynode *) poolalloc(&splaynodes);
8335   triedgecopy(*newkey, newsplaynode->keyedge);
8336   dest(*newkey, newsplaynode->keydest);
8337   if (splayroot == (struct splaynode *) NULL) {
8338     newsplaynode->lchild = (struct splaynode *) NULL;
8339     newsplaynode->rchild = (struct splaynode *) NULL;
8340   } else if (rightofhyperbola(&splayroot->keyedge, searchpoint)) {
8341     newsplaynode->lchild = splayroot;
8342     newsplaynode->rchild = splayroot->rchild;
8343     splayroot->rchild = (struct splaynode *) NULL;
8344   } else {
8345     newsplaynode->lchild = splayroot->lchild;
8346     newsplaynode->rchild = splayroot;
8347     splayroot->lchild = (struct splaynode *) NULL;
8348   }
8349   return newsplaynode;
8350 }
8351
8352 #endif /* not REDUCED */
8353
8354 #ifndef REDUCED
8355
8356 struct splaynode *circletopinsert(splayroot, newkey, pa, pb, pc, topy)
8357 struct splaynode *splayroot;
8358 struct triedge *newkey;
8359 point pa;
8360 point pb;
8361 point pc;
8362 REAL topy;
8363 {
8364   REAL ccwabc;
8365   REAL xac, yac, xbc, ybc;
8366   REAL aclen2, bclen2;
8367   REAL searchpoint[2];
8368   struct triedge dummytri;
8369
8370   ccwabc = counterclockwise(pa, pb, pc);
8371   xac = pa[0] - pc[0];
8372   yac = pa[1] - pc[1];
8373   xbc = pb[0] - pc[0];
8374   ybc = pb[1] - pc[1];
8375   aclen2 = xac * xac + yac * yac;
8376   bclen2 = xbc * xbc + ybc * ybc;
8377   searchpoint[0] = pc[0] - (yac * bclen2 - ybc * aclen2) / (2.0 * ccwabc);
8378   searchpoint[1] = topy;
8379   return splayinsert(splay(splayroot, (point) searchpoint, &dummytri), newkey,
8380                      (point) searchpoint);
8381 }
8382
8383 #endif /* not REDUCED */
8384
8385 #ifndef REDUCED
8386
8387 struct splaynode *frontlocate(splayroot, bottommost, searchpoint, searchtri,
8388                               farright)
8389 struct splaynode *splayroot;
8390 struct triedge *bottommost;
8391 point searchpoint;
8392 struct triedge *searchtri;
8393 int *farright;
8394 {
8395   int farrightflag;
8396   triangle ptr;                       /* Temporary variable used by onext(). */
8397
8398   triedgecopy(*bottommost, *searchtri);
8399   splayroot = splay(splayroot, searchpoint, searchtri);
8400
8401   farrightflag = 0;
8402   while (!farrightflag && rightofhyperbola(searchtri, searchpoint)) {
8403     onextself(*searchtri);
8404     farrightflag = triedgeequal(*searchtri, *bottommost);
8405   }
8406   *farright = farrightflag;
8407   return splayroot;
8408 }
8409
8410 #endif /* not REDUCED */
8411
8412 #ifndef REDUCED
8413
8414 long sweeplinedelaunay()
8415 {
8416   struct event **eventheap;
8417   struct event *events;
8418   struct event *freeevents;
8419   struct event *nextevent;
8420   struct event *newevent;
8421   struct splaynode *splayroot;
8422   struct triedge bottommost;
8423   struct triedge searchtri;
8424   struct triedge fliptri;
8425   struct triedge lefttri, righttri, farlefttri, farrighttri;
8426   struct triedge inserttri;
8427   point firstpoint, secondpoint;
8428   point nextpoint, lastpoint;
8429   point connectpoint;
8430   point leftpoint, midpoint, rightpoint;
8431   REAL lefttest, righttest;
8432   int heapsize;
8433   int check4events, farrightflag;
8434   triangle ptr;   /* Temporary variable used by sym(), onext(), and oprev(). */
8435
8436   poolinit(&splaynodes, sizeof(struct splaynode), SPLAYNODEPERBLOCK, POINTER,
8437            0);
8438   splayroot = (struct splaynode *) NULL;
8439
8440   if (verbose) {
8441     printf("  Placing points in event heap.\n");
8442   }
8443   createeventheap(&eventheap, &events, &freeevents);
8444   heapsize = inpoints;
8445
8446   if (verbose) {
8447     printf("  Forming triangulation.\n");
8448   }
8449   maketriangle(&lefttri);
8450   maketriangle(&righttri);
8451   bond(lefttri, righttri);
8452   lnextself(lefttri);
8453   lprevself(righttri);
8454   bond(lefttri, righttri);
8455   lnextself(lefttri);
8456   lprevself(righttri);
8457   bond(lefttri, righttri);
8458   firstpoint = (point) eventheap[0]->eventptr;
8459   eventheap[0]->eventptr = (VOID *) freeevents;
8460   freeevents = eventheap[0];
8461   eventheapdelete(eventheap, heapsize, 0);
8462   heapsize--;
8463   do {
8464     if (heapsize == 0) {
8465       printf("Error:  Input points are all identical.\n");
8466       exit(1);
8467     }
8468     secondpoint = (point) eventheap[0]->eventptr;
8469     eventheap[0]->eventptr = (VOID *) freeevents;
8470     freeevents = eventheap[0];
8471     eventheapdelete(eventheap, heapsize, 0);
8472     heapsize--;
8473     if ((firstpoint[0] == secondpoint[0])
8474         && (firstpoint[1] == secondpoint[1])) {
8475       printf(
8476 "Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
8477              secondpoint[0], secondpoint[1]);
8478 /*  Commented out - would eliminate point from output .node file.
8479       setpointmark(secondpoint, DEADPOINT);
8480 */
8481     }
8482   } while ((firstpoint[0] == secondpoint[0])
8483            && (firstpoint[1] == secondpoint[1]));
8484   setorg(lefttri, firstpoint);
8485   setdest(lefttri, secondpoint);
8486   setorg(righttri, secondpoint);
8487   setdest(righttri, firstpoint);
8488   lprev(lefttri, bottommost);
8489   lastpoint = secondpoint;
8490   while (heapsize > 0) {
8491     nextevent = eventheap[0];
8492     eventheapdelete(eventheap, heapsize, 0);
8493     heapsize--;
8494     check4events = 1;
8495     if (nextevent->xkey < xmin) {
8496       decode(nextevent->eventptr, fliptri);
8497       oprev(fliptri, farlefttri);
8498       check4deadevent(&farlefttri, &freeevents, eventheap, &heapsize);
8499       onext(fliptri, farrighttri);
8500       check4deadevent(&farrighttri, &freeevents, eventheap, &heapsize);
8501
8502       if (triedgeequal(farlefttri, bottommost)) {
8503         lprev(fliptri, bottommost);
8504       }
8505       flip(&fliptri);
8506       setapex(fliptri, NULL);
8507       lprev(fliptri, lefttri);
8508       lnext(fliptri, righttri);
8509       sym(lefttri, farlefttri);
8510
8511       if (randomnation(SAMPLERATE) == 0) {
8512         symself(fliptri);
8513         dest(fliptri, leftpoint);
8514         apex(fliptri, midpoint);
8515         org(fliptri, rightpoint);
8516         splayroot = circletopinsert(splayroot, &lefttri, leftpoint, midpoint,
8517                                     rightpoint, nextevent->ykey);
8518       }
8519     } else {
8520       nextpoint = (point) nextevent->eventptr;
8521       if ((nextpoint[0] == lastpoint[0]) && (nextpoint[1] == lastpoint[1])) {
8522         printf(
8523 "Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
8524                nextpoint[0], nextpoint[1]);
8525 /*  Commented out - would eliminate point from output .node file.
8526         setpointmark(nextpoint, DEADPOINT);
8527 */
8528         check4events = 0;
8529       } else {
8530         lastpoint = nextpoint;
8531
8532         splayroot = frontlocate(splayroot, &bottommost, nextpoint, &searchtri,
8533                                 &farrightflag);
8534 /*
8535         triedgecopy(bottommost, searchtri);
8536         farrightflag = 0;
8537         while (!farrightflag && rightofhyperbola(&searchtri, nextpoint)) {
8538           onextself(searchtri);
8539           farrightflag = triedgeequal(searchtri, bottommost);
8540         }
8541 */
8542
8543         check4deadevent(&searchtri, &freeevents, eventheap, &heapsize);
8544
8545         triedgecopy(searchtri, farrighttri);
8546         sym(searchtri, farlefttri);
8547         maketriangle(&lefttri);
8548         maketriangle(&righttri);
8549         dest(farrighttri, connectpoint);
8550         setorg(lefttri, connectpoint);
8551         setdest(lefttri, nextpoint);
8552         setorg(righttri, nextpoint);
8553         setdest(righttri, connectpoint);
8554         bond(lefttri, righttri);
8555         lnextself(lefttri);
8556         lprevself(righttri);
8557         bond(lefttri, righttri);
8558         lnextself(lefttri);
8559         lprevself(righttri);
8560         bond(lefttri, farlefttri);
8561         bond(righttri, farrighttri);
8562         if (!farrightflag && triedgeequal(farrighttri, bottommost)) {
8563           triedgecopy(lefttri, bottommost);
8564         }
8565
8566         if (randomnation(SAMPLERATE) == 0) {
8567           splayroot = splayinsert(splayroot, &lefttri, nextpoint);
8568         } else if (randomnation(SAMPLERATE) == 0) {
8569           lnext(righttri, inserttri);
8570           splayroot = splayinsert(splayroot, &inserttri, nextpoint);
8571         }
8572       }
8573     }
8574     nextevent->eventptr = (VOID *) freeevents;
8575     freeevents = nextevent;
8576
8577     if (check4events) {
8578       apex(farlefttri, leftpoint);
8579       dest(lefttri, midpoint);
8580       apex(lefttri, rightpoint);
8581       lefttest = counterclockwise(leftpoint, midpoint, rightpoint);
8582       if (lefttest > 0.0) {
8583         newevent = freeevents;
8584         freeevents = (struct event *) freeevents->eventptr;
8585         newevent->xkey = xminextreme;
8586         newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
8587                                    lefttest);
8588         newevent->eventptr = (VOID *) encode(lefttri);
8589         eventheapinsert(eventheap, heapsize, newevent);
8590         heapsize++;
8591         setorg(lefttri, newevent);
8592       }
8593       apex(righttri, leftpoint);
8594       org(righttri, midpoint);
8595       apex(farrighttri, rightpoint);
8596       righttest = counterclockwise(leftpoint, midpoint, rightpoint);
8597       if (righttest > 0.0) {
8598         newevent = freeevents;
8599         freeevents = (struct event *) freeevents->eventptr;
8600         newevent->xkey = xminextreme;
8601         newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
8602                                    righttest);
8603         newevent->eventptr = (VOID *) encode(farrighttri);
8604         eventheapinsert(eventheap, heapsize, newevent);
8605         heapsize++;
8606         setorg(farrighttri, newevent);
8607       }
8608     }
8609   }
8610
8611   pooldeinit(&splaynodes);
8612   lprevself(bottommost);
8613   return removeghosts(&bottommost);
8614 }
8615
8616 #endif /* not REDUCED */
8617
8618 /**                                                                         **/
8619 /**                                                                         **/
8620 /********* Sweepline Delaunay triangulation ends here                *********/
8621
8622 /********* General mesh construction routines begin here             *********/
8623 /**                                                                         **/
8624 /**                                                                         **/
8625
8626 /*****************************************************************************/
8627 /*                                                                           */
8628 /*  delaunay()   Form a Delaunay triangulation.                              */
8629 /*                                                                           */
8630 /*****************************************************************************/
8631
8632 long delaunay()
8633 {
8634   eextras = 0;
8635   initializetrisegpools();
8636
8637 #ifdef REDUCED
8638   if (!quiet) {
8639     printf(
8640       "Constructing Delaunay triangulation by divide-and-conquer method.\n");
8641   }
8642   return divconqdelaunay();
8643 #else /* not REDUCED */
8644   if (!quiet) {
8645     printf("Constructing Delaunay triangulation ");
8646     if (incremental) {
8647       printf("by incremental method.\n");
8648     } else if (sweepline) {
8649       printf("by sweepline method.\n");
8650     } else {
8651       printf("by divide-and-conquer method.\n");
8652     }
8653   }
8654   if (incremental) {
8655     return incrementaldelaunay();
8656   } else if (sweepline) {
8657     return sweeplinedelaunay();
8658   } else {
8659     return divconqdelaunay();
8660   }
8661 #endif /* not REDUCED */
8662 }
8663
8664 /*****************************************************************************/
8665 /*                                                                           */
8666 /*  reconstruct()   Reconstruct a triangulation from its .ele (and possibly  */
8667 /*                  .poly) file.  Used when the -r switch is used.           */
8668 /*                                                                           */
8669 /*  Reads an .ele file and reconstructs the original mesh.  If the -p switch */
8670 /*  is used, this procedure will also read a .poly file and reconstruct the  */
8671 /*  shell edges of the original mesh.  If the -a switch is used, this        */
8672 /*  procedure will also read an .area file and set a maximum area constraint */
8673 /*  on each triangle.                                                        */
8674 /*                                                                           */
8675 /*  Points that are not corners of triangles, such as nodes on edges of      */
8676 /*  subparametric elements, are discarded.                                   */
8677 /*                                                                           */
8678 /*  This routine finds the adjacencies between triangles (and shell edges)   */
8679 /*  by forming one stack of triangles for each vertex.  Each triangle is on  */
8680 /*  three different stacks simultaneously.  Each triangle's shell edge       */
8681 /*  pointers are used to link the items in each stack.  This memory-saving   */
8682 /*  feature makes the code harder to read.  The most important thing to keep */
8683 /*  in mind is that each triangle is removed from a stack precisely when     */
8684 /*  the corresponding pointer is adjusted to refer to a shell edge rather    */
8685 /*  than the next triangle of the stack.                                     */
8686 /*                                                                           */
8687 /*****************************************************************************/
8688
8689 #ifndef CDT_ONLY
8690
8691 #ifdef TRILIBRARY
8692
8693 int reconstruct(trianglelist, triangleattriblist, trianglearealist, elements,
8694                 corners, attribs, segmentlist, segmentmarkerlist,
8695                 numberofsegments)
8696 int *trianglelist;
8697 REAL *triangleattriblist;
8698 REAL *trianglearealist;
8699 int elements;
8700 int corners;
8701 int attribs;
8702 int *segmentlist;
8703 int *segmentmarkerlist;
8704 int numberofsegments;
8705
8706 #else /* not TRILIBRARY */
8707
8708 long reconstruct(elefilename, areafilename, polyfilename, polyfile)
8709 char *elefilename;
8710 char *areafilename;
8711 char *polyfilename;
8712 FILE *polyfile;
8713
8714 #endif /* not TRILIBRARY */
8715
8716 {
8717 #ifdef TRILIBRARY
8718   int pointindex;
8719   int attribindex;
8720 #else /* not TRILIBRARY */
8721   FILE *elefile;
8722   FILE *areafile;
8723   char inputline[INPUTLINESIZE];
8724   char *stringptr;
8725   int areaelements;
8726 #endif /* not TRILIBRARY */
8727   struct triedge triangleloop;
8728   struct triedge triangleleft;
8729   struct triedge checktri;
8730   struct triedge checkleft;
8731   struct triedge checkneighbor;
8732   struct edge shelleloop;
8733   triangle *vertexarray;
8734   triangle *prevlink;
8735   triangle nexttri;
8736   point tdest, tapex;
8737   point checkdest, checkapex;
8738   point shorg;
8739   point killpoint;
8740   REAL area;
8741   int corner[3];
8742   int end[2];
8743   int killpointindex;
8744   int incorners;
8745   int segmentmarkers;
8746   int boundmarker;
8747   int aroundpoint;
8748   long hullsize;
8749   int notfound;
8750   int elementnumber, segmentnumber;
8751   int i, j;
8752   triangle ptr;                         /* Temporary variable used by sym(). */
8753
8754 #ifdef TRILIBRARY
8755   inelements = elements;
8756   incorners = corners;
8757   if (incorners < 3) {
8758     printf("Error:  Triangles must have at least 3 points.\n");
8759     exit(1);
8760   }
8761   eextras = attribs;
8762 #else /* not TRILIBRARY */
8763   /* Read the triangles from an .ele file. */
8764   if (!quiet) {
8765     printf("Opening %s.\n", elefilename);
8766   }
8767   elefile = fopen(elefilename, "r");
8768   if (elefile == (FILE *) NULL) {
8769     printf("  Error:  Cannot access file %s.\n", elefilename);
8770     exit(1);
8771   }
8772   /* Read number of triangles, number of points per triangle, and */
8773   /*   number of triangle attributes from .ele file.              */
8774   stringptr = readline(inputline, elefile, elefilename);
8775   inelements = (int) strtol (stringptr, &stringptr, 0);
8776   stringptr = findfield(stringptr);
8777   if (*stringptr == '\0') {
8778     incorners = 3;
8779   } else {
8780     incorners = (int) strtol (stringptr, &stringptr, 0);
8781     if (incorners < 3) {
8782       printf("Error:  Triangles in %s must have at least 3 points.\n",
8783              elefilename);
8784       exit(1);
8785     }
8786   }
8787   stringptr = findfield(stringptr);
8788   if (*stringptr == '\0') {
8789     eextras = 0;
8790   } else {
8791     eextras = (int) strtol (stringptr, &stringptr, 0);
8792   }
8793 #endif /* not TRILIBRARY */
8794
8795   initializetrisegpools();
8796
8797   /* Create the triangles. */
8798   for (elementnumber = 1; elementnumber <= inelements; elementnumber++) {
8799     maketriangle(&triangleloop);
8800     /* Mark the triangle as living. */
8801     triangleloop.tri[3] = (triangle) triangleloop.tri;
8802   }
8803
8804   if (poly) {
8805 #ifdef TRILIBRARY
8806     insegments = numberofsegments;
8807     segmentmarkers = segmentmarkerlist != (int *) NULL;
8808 #else /* not TRILIBRARY */
8809     /* Read number of segments and number of segment */
8810     /*   boundary markers from .poly file.           */
8811     stringptr = readline(inputline, polyfile, inpolyfilename);
8812     insegments = (int) strtol (stringptr, &stringptr, 0);
8813     stringptr = findfield(stringptr);
8814     if (*stringptr == '\0') {
8815       segmentmarkers = 0;
8816     } else {
8817       segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
8818     }
8819 #endif /* not TRILIBRARY */
8820
8821     /* Create the shell edges. */
8822     for (segmentnumber = 1; segmentnumber <= insegments; segmentnumber++) {
8823       makeshelle(&shelleloop);
8824       /* Mark the shell edge as living. */
8825       shelleloop.sh[2] = (shelle) shelleloop.sh;
8826     }
8827   }
8828
8829 #ifdef TRILIBRARY
8830   pointindex = 0;
8831   attribindex = 0;
8832 #else /* not TRILIBRARY */
8833   if (vararea) {
8834     /* Open an .area file, check for consistency with the .ele file. */
8835     if (!quiet) {
8836       printf("Opening %s.\n", areafilename);
8837     }
8838     areafile = fopen(areafilename, "r");
8839     if (areafile == (FILE *) NULL) {
8840       printf("  Error:  Cannot access file %s.\n", areafilename);
8841       exit(1);
8842     }
8843     stringptr = readline(inputline, areafile, areafilename);
8844     areaelements = (int) strtol (stringptr, &stringptr, 0);
8845     if (areaelements != inelements) {
8846       printf("Error:  %s and %s disagree on number of triangles.\n",
8847              elefilename, areafilename);
8848       exit(1);
8849     }
8850   }
8851 #endif /* not TRILIBRARY */
8852
8853   if (!quiet) {
8854     printf("Reconstructing mesh.\n");
8855   }
8856   /* Allocate a temporary array that maps each point to some adjacent  */
8857   /*   triangle.  I took care to allocate all the permanent memory for */
8858   /*   triangles and shell edges first.                                */
8859   vertexarray = (triangle *) malloc(points.items * sizeof(triangle));
8860   if (vertexarray == (triangle *) NULL) {
8861     printf("Error:  Out of memory.\n");
8862     exit(1);
8863   }
8864   /* Each point is initially unrepresented. */
8865   for (i = 0; i < points.items; i++) {
8866     vertexarray[i] = (triangle) dummytri;
8867   }
8868
8869   if (verbose) {
8870     printf("  Assembling triangles.\n");
8871   }
8872   /* Read the triangles from the .ele file, and link */
8873   /*   together those that share an edge.            */
8874   traversalinit(&triangles);
8875   triangleloop.tri = triangletraverse();
8876   elementnumber = firstnumber;
8877   while (triangleloop.tri != (triangle *) NULL) {
8878 #ifdef TRILIBRARY
8879     /* Copy the triangle's three corners. */
8880     for (j = 0; j < 3; j++) {
8881       corner[j] = trianglelist[pointindex++];
8882       if ((corner[j] < firstnumber) || (corner[j] >= firstnumber + inpoints)) {
8883         printf("Error:  Triangle %d has an invalid vertex index.\n",
8884                elementnumber);
8885         exit(1);
8886       }
8887     }
8888 #else /* not TRILIBRARY */
8889     /* Read triangle number and the triangle's three corners. */
8890     stringptr = readline(inputline, elefile, elefilename);
8891     for (j = 0; j < 3; j++) {
8892       stringptr = findfield(stringptr);
8893       if (*stringptr == '\0') {
8894         printf("Error:  Triangle %d is missing point %d in %s.\n",
8895                elementnumber, j + 1, elefilename);
8896         exit(1);
8897       } else {
8898         corner[j] = (int) strtol (stringptr, &stringptr, 0);
8899         if ((corner[j] < firstnumber) ||
8900             (corner[j] >= firstnumber + inpoints)) {
8901           printf("Error:  Triangle %d has an invalid vertex index.\n",
8902                  elementnumber);
8903           exit(1);
8904         }
8905       }
8906     }
8907 #endif /* not TRILIBRARY */
8908
8909     /* Find out about (and throw away) extra nodes. */
8910     for (j = 3; j < incorners; j++) {
8911 #ifdef TRILIBRARY
8912       killpointindex = trianglelist[pointindex++];
8913 #else /* not TRILIBRARY */
8914       stringptr = findfield(stringptr);
8915       if (*stringptr != '\0') {
8916         killpointindex = (int) strtol (stringptr, &stringptr, 0);
8917 #endif /* not TRILIBRARY */
8918         if ((killpointindex >= firstnumber) &&
8919             (killpointindex < firstnumber + inpoints)) {
8920           /* Delete the non-corner point if it's not already deleted. */
8921           killpoint = getpoint(killpointindex);
8922           if (pointmark(killpoint) != DEADPOINT) {
8923             pointdealloc(killpoint);
8924           }
8925         }
8926 #ifndef TRILIBRARY
8927       }
8928 #endif /* not TRILIBRARY */
8929     }
8930
8931     /* Read the triangle's attributes. */
8932     for (j = 0; j < eextras; j++) {
8933 #ifdef TRILIBRARY
8934       setelemattribute(triangleloop, j, triangleattriblist[attribindex++]);
8935 #else /* not TRILIBRARY */
8936       stringptr = findfield(stringptr);
8937       if (*stringptr == '\0') {
8938         setelemattribute(triangleloop, j, 0);
8939       } else {
8940         setelemattribute(triangleloop, j,
8941                          (REAL) strtod (stringptr, &stringptr));
8942       }
8943 #endif /* not TRILIBRARY */
8944     }
8945
8946     if (vararea) {
8947 #ifdef TRILIBRARY
8948       area = trianglearealist[elementnumber - firstnumber];
8949 #else /* not TRILIBRARY */
8950       /* Read an area constraint from the .area file. */
8951       stringptr = readline(inputline, areafile, areafilename);
8952       stringptr = findfield(stringptr);
8953       if (*stringptr == '\0') {
8954         area = -1.0;                      /* No constraint on this triangle. */
8955       } else {
8956         area = (REAL) strtod(stringptr, &stringptr);
8957       }
8958 #endif /* not TRILIBRARY */
8959       setareabound(triangleloop, area);
8960     }
8961
8962     /* Set the triangle's vertices. */
8963     triangleloop.orient = 0;
8964     setorg(triangleloop, getpoint(corner[0]));
8965     setdest(triangleloop, getpoint(corner[1]));
8966     setapex(triangleloop, getpoint(corner[2]));
8967     /* Try linking the triangle to others that share these vertices. */
8968     for (triangleloop.orient = 0; triangleloop.orient < 3;
8969          triangleloop.orient++) {
8970       /* Take the number for the origin of triangleloop. */
8971       aroundpoint = corner[triangleloop.orient];
8972       /* Look for other triangles having this vertex. */
8973       nexttri = vertexarray[aroundpoint - firstnumber];
8974       /* Link the current triangle to the next one in the stack. */
8975       triangleloop.tri[6 + triangleloop.orient] = nexttri;
8976       /* Push the current triangle onto the stack. */
8977       vertexarray[aroundpoint - firstnumber] = encode(triangleloop);
8978       decode(nexttri, checktri);
8979       if (checktri.tri != dummytri) {
8980         dest(triangleloop, tdest);
8981         apex(triangleloop, tapex);
8982         /* Look for other triangles that share an edge. */
8983         do {
8984           dest(checktri, checkdest);
8985           apex(checktri, checkapex);
8986           if (tapex == checkdest) {
8987             /* The two triangles share an edge; bond them together. */
8988             lprev(triangleloop, triangleleft);
8989             bond(triangleleft, checktri);
8990           }
8991           if (tdest == checkapex) {
8992             /* The two triangles share an edge; bond them together. */
8993             lprev(checktri, checkleft);
8994             bond(triangleloop, checkleft);
8995           }
8996           /* Find the next triangle in the stack. */
8997           nexttri = checktri.tri[6 + checktri.orient];
8998           decode(nexttri, checktri);
8999         } while (checktri.tri != dummytri);
9000       }
9001     }
9002     triangleloop.tri = triangletraverse();
9003     elementnumber++;
9004   }
9005
9006 #ifdef TRILIBRARY
9007   pointindex = 0;
9008 #else /* not TRILIBRARY */
9009   fclose(elefile);
9010   if (vararea) {
9011     fclose(areafile);
9012   }
9013 #endif /* not TRILIBRARY */
9014
9015   hullsize = 0;                      /* Prepare to count the boundary edges. */
9016   if (poly) {
9017     if (verbose) {
9018       printf("  Marking segments in triangulation.\n");
9019     }
9020     /* Read the segments from the .poly file, and link them */
9021     /*   to their neighboring triangles.                    */
9022     boundmarker = 0;
9023     traversalinit(&shelles);
9024     shelleloop.sh = shelletraverse();
9025     segmentnumber = firstnumber;
9026     while (shelleloop.sh != (shelle *) NULL) {
9027 #ifdef TRILIBRARY
9028       end[0] = segmentlist[pointindex++];
9029       end[1] = segmentlist[pointindex++];
9030       if (segmentmarkers) {
9031         boundmarker = segmentmarkerlist[segmentnumber - firstnumber];
9032       }
9033 #else /* not TRILIBRARY */
9034       /* Read the endpoints of each segment, and possibly a boundary marker. */
9035       stringptr = readline(inputline, polyfile, inpolyfilename);
9036       /* Skip the first (segment number) field. */
9037       stringptr = findfield(stringptr);
9038       if (*stringptr == '\0') {
9039         printf("Error:  Segment %d has no endpoints in %s.\n", segmentnumber,
9040                polyfilename);
9041         exit(1);
9042       } else {
9043         end[0] = (int) strtol (stringptr, &stringptr, 0);
9044       }
9045       stringptr = findfield(stringptr);
9046       if (*stringptr == '\0') {
9047         printf("Error:  Segment %d is missing its second endpoint in %s.\n",
9048                segmentnumber, polyfilename);
9049         exit(1);
9050       } else {
9051         end[1] = (int) strtol (stringptr, &stringptr, 0);
9052       }
9053       if (segmentmarkers) {
9054         stringptr = findfield(stringptr);
9055         if (*stringptr == '\0') {
9056           boundmarker = 0;
9057         } else {
9058           boundmarker = (int) strtol (stringptr, &stringptr, 0);
9059         }
9060       }
9061 #endif /* not TRILIBRARY */
9062       for (j = 0; j < 2; j++) {
9063         if ((end[j] < firstnumber) || (end[j] >= firstnumber + inpoints)) {
9064           printf("Error:  Segment %d has an invalid vertex index.\n", 
9065                  segmentnumber);
9066           exit(1);
9067         }
9068       }
9069
9070       /* set the shell edge's vertices. */
9071       shelleloop.shorient = 0;
9072       setsorg(shelleloop, getpoint(end[0]));
9073       setsdest(shelleloop, getpoint(end[1]));
9074       setmark(shelleloop, boundmarker);
9075       /* Try linking the shell edge to triangles that share these vertices. */
9076       for (shelleloop.shorient = 0; shelleloop.shorient < 2;
9077            shelleloop.shorient++) {
9078         /* Take the number for the destination of shelleloop. */
9079         aroundpoint = end[1 - shelleloop.shorient];
9080         /* Look for triangles having this vertex. */
9081         prevlink = &vertexarray[aroundpoint - firstnumber];
9082         nexttri = vertexarray[aroundpoint - firstnumber];
9083         decode(nexttri, checktri);
9084         sorg(shelleloop, shorg);
9085         notfound = 1;
9086         /* Look for triangles having this edge.  Note that I'm only       */
9087         /*   comparing each triangle's destination with the shell edge;   */
9088         /*   each triangle's apex is handled through a different vertex.  */
9089         /*   Because each triangle appears on three vertices' lists, each */
9090         /*   occurrence of a triangle on a list can (and does) represent  */
9091         /*   an edge.  In this way, most edges are represented twice, and */
9092         /*   every triangle-segment bond is represented once.             */
9093         while (notfound && (checktri.tri != dummytri)) {
9094           dest(checktri, checkdest);
9095           if (shorg == checkdest) {
9096             /* We have a match.  Remove this triangle from the list. */
9097             *prevlink = checktri.tri[6 + checktri.orient];
9098             /* Bond the shell edge to the triangle. */
9099             tsbond(checktri, shelleloop);
9100             /* Check if this is a boundary edge. */
9101             sym(checktri, checkneighbor);
9102             if (checkneighbor.tri == dummytri) {
9103               /* The next line doesn't insert a shell edge (because there's */
9104               /*   already one there), but it sets the boundary markers of  */
9105               /*   the existing shell edge and its vertices.                */
9106               insertshelle(&checktri, 1);
9107               hullsize++;
9108             }
9109             notfound = 0;
9110           }
9111           /* Find the next triangle in the stack. */
9112           prevlink = &checktri.tri[6 + checktri.orient];
9113           nexttri = checktri.tri[6 + checktri.orient];
9114           decode(nexttri, checktri);
9115         }
9116       }
9117       shelleloop.sh = shelletraverse();
9118       segmentnumber++;
9119     }
9120   }
9121
9122   /* Mark the remaining edges as not being attached to any shell edge. */
9123   /* Also, count the (yet uncounted) boundary edges.                   */
9124   for (i = 0; i < points.items; i++) {
9125     /* Search the stack of triangles adjacent to a point. */
9126     nexttri = vertexarray[i];
9127     decode(nexttri, checktri);
9128     while (checktri.tri != dummytri) {
9129       /* Find the next triangle in the stack before this */
9130       /*   information gets overwritten.                 */
9131       nexttri = checktri.tri[6 + checktri.orient];
9132       /* No adjacent shell edge.  (This overwrites the stack info.) */
9133       tsdissolve(checktri);
9134       sym(checktri, checkneighbor);
9135       if (checkneighbor.tri == dummytri) {
9136         insertshelle(&checktri, 1);
9137         hullsize++;
9138       }
9139       decode(nexttri, checktri);
9140     }
9141   }
9142
9143   free(vertexarray);
9144   return hullsize;
9145 }
9146
9147 #endif /* not CDT_ONLY */
9148
9149 /**                                                                         **/
9150 /**                                                                         **/
9151 /********* General mesh construction routines end here               *********/
9152
9153 /********* Segment (shell edge) insertion begins here                *********/
9154 /**                                                                         **/
9155 /**                                                                         **/
9156
9157 /*****************************************************************************/
9158 /*                                                                           */
9159 /*  finddirection()   Find the first triangle on the path from one point     */
9160 /*                    to another.                                            */
9161 /*                                                                           */
9162 /*  Finds the triangle that intersects a line segment drawn from the         */
9163 /*  origin of `searchtri' to the point `endpoint', and returns the result    */
9164 /*  in `searchtri'.  The origin of `searchtri' does not change, even though  */
9165 /*  the triangle returned may differ from the one passed in.  This routine   */
9166 /*  is used to find the direction to move in to get from one point to        */
9167 /*  another.                                                                 */
9168 /*                                                                           */
9169 /*  The return value notes whether the destination or apex of the found      */
9170 /*  triangle is collinear with the two points in question.                   */
9171 /*                                                                           */
9172 /*****************************************************************************/
9173
9174 enum finddirectionresult finddirection(searchtri, endpoint)
9175 struct triedge *searchtri;
9176 point endpoint;
9177 {
9178   struct triedge checktri;
9179   point startpoint;
9180   point leftpoint, rightpoint;
9181   REAL leftccw, rightccw;
9182   int leftflag, rightflag;
9183   triangle ptr;           /* Temporary variable used by onext() and oprev(). */
9184
9185   org(*searchtri, startpoint);
9186   dest(*searchtri, rightpoint);
9187   apex(*searchtri, leftpoint);
9188   /* Is `endpoint' to the left? */
9189   leftccw = counterclockwise(endpoint, startpoint, leftpoint);
9190   leftflag = leftccw > 0.0;
9191   /* Is `endpoint' to the right? */
9192   rightccw = counterclockwise(startpoint, endpoint, rightpoint);
9193   rightflag = rightccw > 0.0;
9194   if (leftflag && rightflag) {
9195     /* `searchtri' faces directly away from `endpoint'.  We could go */
9196     /*   left or right.  Ask whether it's a triangle or a boundary   */
9197     /*   on the left.                                                */
9198     onext(*searchtri, checktri);
9199     if (checktri.tri == dummytri) {
9200       leftflag = 0;
9201     } else {
9202       rightflag = 0;
9203     }
9204   }
9205   while (leftflag) {
9206     /* Turn left until satisfied. */
9207     onextself(*searchtri);
9208     if (searchtri->tri == dummytri) {
9209       printf("Internal error in finddirection():  Unable to find a\n");
9210       printf("  triangle leading from (%.12g, %.12g) to", startpoint[0],
9211              startpoint[1]);
9212       printf("  (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
9213       internalerror();
9214     }
9215     apex(*searchtri, leftpoint);
9216     rightccw = leftccw;
9217     leftccw = counterclockwise(endpoint, startpoint, leftpoint);
9218     leftflag = leftccw > 0.0;
9219   }
9220   while (rightflag) {
9221     /* Turn right until satisfied. */
9222     oprevself(*searchtri);
9223     if (searchtri->tri == dummytri) {
9224       printf("Internal error in finddirection():  Unable to find a\n");
9225       printf("  triangle leading from (%.12g, %.12g) to", startpoint[0],
9226              startpoint[1]);
9227       printf("  (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
9228       internalerror();
9229     }
9230     dest(*searchtri, rightpoint);
9231     leftccw = rightccw;
9232     rightccw = counterclockwise(startpoint, endpoint, rightpoint);
9233     rightflag = rightccw > 0.0;
9234   }
9235   if (leftccw == 0.0) {
9236     return LEFTCOLLINEAR;
9237   } else if (rightccw == 0.0) {
9238     return RIGHTCOLLINEAR;
9239   } else {
9240     return WITHIN;
9241   }
9242 }
9243
9244 /*****************************************************************************/
9245 /*                                                                           */
9246 /*  segmentintersection()   Find the intersection of an existing segment     */
9247 /*                          and a segment that is being inserted.  Insert    */
9248 /*                          a point at the intersection, splitting an        */
9249 /*                          existing shell edge.                             */
9250 /*                                                                           */
9251 /*  The segment being inserted connects the apex of splittri to endpoint2.   */
9252 /*  splitshelle is the shell edge being split, and MUST be opposite          */
9253 /*  splittri.  Hence, the edge being split connects the origin and           */
9254 /*  destination of splittri.                                                 */
9255 /*                                                                           */
9256 /*  On completion, splittri is a handle having the newly inserted            */
9257 /*  intersection point as its origin, and endpoint1 as its destination.      */
9258 /*                                                                           */
9259 /*****************************************************************************/
9260
9261 void segmentintersection(splittri, splitshelle, endpoint2)
9262 struct triedge *splittri;
9263 struct edge *splitshelle;
9264 point endpoint2;
9265 {
9266   point endpoint1;
9267   point torg, tdest;
9268   point leftpoint, rightpoint;
9269   point newpoint;
9270   enum insertsiteresult success;
9271   enum finddirectionresult collinear;
9272   REAL ex, ey;
9273   REAL tx, ty;
9274   REAL etx, ety;
9275   REAL split, denom;
9276   int i;
9277   triangle ptr;                       /* Temporary variable used by onext(). */
9278
9279   /* Find the other three segment endpoints. */
9280   apex(*splittri, endpoint1);
9281   org(*splittri, torg);
9282   dest(*splittri, tdest);
9283   /* Segment intersection formulae; see the Antonio reference. */
9284   tx = tdest[0] - torg[0];
9285   ty = tdest[1] - torg[1];
9286   ex = endpoint2[0] - endpoint1[0];
9287   ey = endpoint2[1] - endpoint1[1];
9288   etx = torg[0] - endpoint2[0];
9289   ety = torg[1] - endpoint2[1];
9290   denom = ty * ex - tx * ey;
9291   if (denom == 0.0) {
9292     printf("Internal error in segmentintersection():");
9293     printf("  Attempt to find intersection of parallel segments.\n");
9294     internalerror();
9295   }
9296   split = (ey * etx - ex * ety) / denom;
9297   /* Create the new point. */
9298   newpoint = (point) poolalloc(&points);
9299   /* Interpolate its coordinate and attributes. */
9300   for (i = 0; i < 2 + nextras; i++) {
9301     newpoint[i] = torg[i] + split * (tdest[i] - torg[i]);
9302   }
9303   setpointmark(newpoint, mark(*splitshelle));
9304   if (verbose > 1) {
9305     printf(
9306     "  Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
9307            torg[0], torg[1], tdest[0], tdest[1], newpoint[0], newpoint[1]);
9308   }
9309   /* Insert the intersection point.  This should always succeed. */
9310   success = insertsite(newpoint, splittri, splitshelle, 0, 0);
9311   if (success != SUCCESSFULPOINT) {
9312     printf("Internal error in segmentintersection():\n");
9313     printf("  Failure to split a segment.\n");
9314     internalerror();
9315   }
9316   if (steinerleft > 0) {
9317     steinerleft--;
9318   }
9319   /* Inserting the point may have caused edge flips.  We wish to rediscover */
9320   /*   the edge connecting endpoint1 to the new intersection point.         */
9321   collinear = finddirection(splittri, endpoint1);
9322   dest(*splittri, rightpoint);
9323   apex(*splittri, leftpoint);
9324   if ((leftpoint[0] == endpoint1[0]) && (leftpoint[1] == endpoint1[1])) {
9325     onextself(*splittri);
9326   } else if ((rightpoint[0] != endpoint1[0]) ||
9327              (rightpoint[1] != endpoint1[1])) {
9328     printf("Internal error in segmentintersection():\n");
9329     printf("  Topological inconsistency after splitting a segment.\n");
9330     internalerror();
9331   }
9332   /* `splittri' should have destination endpoint1. */
9333 }
9334
9335 /*****************************************************************************/
9336 /*                                                                           */
9337 /*  scoutsegment()   Scout the first triangle on the path from one endpoint  */
9338 /*                   to another, and check for completion (reaching the      */
9339 /*                   second endpoint), a collinear point, and the            */
9340 /*                   intersection of two segments.                           */
9341 /*                                                                           */
9342 /*  Returns one if the entire segment is successfully inserted, and zero if  */
9343 /*  the job must be finished by conformingedge() or constrainededge().       */
9344 /*                                                                           */
9345 /*  If the first triangle on the path has the second endpoint as its         */
9346 /*  destination or apex, a shell edge is inserted and the job is done.       */
9347 /*                                                                           */
9348 /*  If the first triangle on the path has a destination or apex that lies on */
9349 /*  the segment, a shell edge is inserted connecting the first endpoint to   */
9350 /*  the collinear point, and the search is continued from the collinear      */
9351 /*  point.                                                                   */
9352 /*                                                                           */
9353 /*  If the first triangle on the path has a shell edge opposite its origin,  */
9354 /*  then there is a segment that intersects the segment being inserted.      */
9355 /*  Their intersection point is inserted, splitting the shell edge.          */
9356 /*                                                                           */
9357 /*  Otherwise, return zero.                                                  */
9358 /*                                                                           */
9359 /*****************************************************************************/
9360
9361 int scoutsegment(searchtri, endpoint2, newmark)
9362 struct triedge *searchtri;
9363 point endpoint2;
9364 int newmark;
9365 {
9366   struct triedge crosstri;
9367   struct edge crossedge;
9368   point leftpoint, rightpoint;
9369   point endpoint1;
9370   enum finddirectionresult collinear;
9371   shelle sptr;                      /* Temporary variable used by tspivot(). */
9372
9373   collinear = finddirection(searchtri, endpoint2);
9374   dest(*searchtri, rightpoint);
9375   apex(*searchtri, leftpoint);
9376   if (((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) ||
9377       ((rightpoint[0] == endpoint2[0]) && (rightpoint[1] == endpoint2[1]))) {
9378     /* The segment is already an edge in the mesh. */
9379     if ((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) {
9380       lprevself(*searchtri);
9381     }
9382     /* Insert a shell edge, if there isn't already one there. */
9383     insertshelle(searchtri, newmark);
9384     return 1;
9385   } else if (collinear == LEFTCOLLINEAR) {
9386     /* We've collided with a point between the segment's endpoints. */
9387     /* Make the collinear point be the triangle's origin. */
9388     lprevself(*searchtri);
9389     insertshelle(searchtri, newmark);
9390     /* Insert the remainder of the segment. */
9391     return scoutsegment(searchtri, endpoint2, newmark);
9392   } else if (collinear == RIGHTCOLLINEAR) {
9393     /* We've collided with a point between the segment's endpoints. */
9394     insertshelle(searchtri, newmark);
9395     /* Make the collinear point be the triangle's origin. */
9396     lnextself(*searchtri);
9397     /* Insert the remainder of the segment. */
9398     return scoutsegment(searchtri, endpoint2, newmark);
9399   } else {
9400     lnext(*searchtri, crosstri);
9401     tspivot(crosstri, crossedge);
9402     /* Check for a crossing segment. */
9403     if (crossedge.sh == dummysh) {
9404       return 0;
9405     } else {
9406       org(*searchtri, endpoint1);
9407       /* Insert a point at the intersection. */
9408       segmentintersection(&crosstri, &crossedge, endpoint2);
9409       triedgecopy(crosstri, *searchtri);
9410       insertshelle(searchtri, newmark);
9411       /* Insert the remainder of the segment. */
9412       return scoutsegment(searchtri, endpoint2, newmark);
9413     }
9414   }
9415 }
9416
9417 /*****************************************************************************/
9418 /*                                                                           */
9419 /*  conformingedge()   Force a segment into a conforming Delaunay            */
9420 /*                     triangulation by inserting a point at its midpoint,   */
9421 /*                     and recursively forcing in the two half-segments if   */
9422 /*                     necessary.                                            */
9423 /*                                                                           */
9424 /*  Generates a sequence of edges connecting `endpoint1' to `endpoint2'.     */
9425 /*  `newmark' is the boundary marker of the segment, assigned to each new    */
9426 /*  splitting point and shell edge.                                          */
9427 /*                                                                           */
9428 /*  Note that conformingedge() does not always maintain the conforming       */
9429 /*  Delaunay property.  Once inserted, segments are locked into place;       */
9430 /*  points inserted later (to force other segments in) may render these      */
9431 /*  fixed segments non-Delaunay.  The conforming Delaunay property will be   */
9432 /*  restored by enforcequality() by splitting encroached segments.           */
9433 /*                                                                           */
9434 /*****************************************************************************/
9435
9436 #ifndef REDUCED
9437 #ifndef CDT_ONLY
9438
9439 void conformingedge(endpoint1, endpoint2, newmark)
9440 point endpoint1;
9441 point endpoint2;
9442 int newmark;
9443 {
9444   struct triedge searchtri1, searchtri2;
9445   struct edge brokenshelle;
9446   point newpoint;
9447   point midpoint1, midpoint2;
9448   enum insertsiteresult success;
9449   int result1, result2;
9450   int i;
9451   shelle sptr;                      /* Temporary variable used by tspivot(). */
9452
9453   if (verbose > 2) {
9454     printf("Forcing segment into triangulation by recursive splitting:\n");
9455     printf("  (%.12g, %.12g) (%.12g, %.12g)\n", endpoint1[0], endpoint1[1],
9456            endpoint2[0], endpoint2[1]);
9457   }
9458   /* Create a new point to insert in the middle of the segment. */
9459   newpoint = (point) poolalloc(&points);
9460   /* Interpolate coordinates and attributes. */
9461   for (i = 0; i < 2 + nextras; i++) {
9462     newpoint[i] = 0.5 * (endpoint1[i] + endpoint2[i]);
9463   }
9464   setpointmark(newpoint, newmark);
9465   /* Find a boundary triangle to search from. */
9466   searchtri1.tri = (triangle *) NULL;
9467   /* Attempt to insert the new point. */
9468   success = insertsite(newpoint, &searchtri1, (struct edge *) NULL, 0, 0);
9469   if (success == DUPLICATEPOINT) {
9470     if (verbose > 2) {
9471       printf("  Segment intersects existing point (%.12g, %.12g).\n",
9472              newpoint[0], newpoint[1]);
9473     }
9474     /* Use the point that's already there. */
9475     pointdealloc(newpoint);
9476     org(searchtri1, newpoint);
9477   } else {
9478     if (success == VIOLATINGPOINT) {
9479       if (verbose > 2) {
9480         printf("  Two segments intersect at (%.12g, %.12g).\n",
9481                newpoint[0], newpoint[1]);
9482       }
9483       /* By fluke, we've landed right on another segment.  Split it. */
9484       tspivot(searchtri1, brokenshelle);
9485       success = insertsite(newpoint, &searchtri1, &brokenshelle, 0, 0);
9486       if (success != SUCCESSFULPOINT) {
9487         printf("Internal error in conformingedge():\n");
9488         printf("  Failure to split a segment.\n");
9489         internalerror();
9490       }
9491     }
9492     /* The point has been inserted successfully. */
9493     if (steinerleft > 0) {
9494       steinerleft--;
9495     }
9496   }
9497   triedgecopy(searchtri1, searchtri2);
9498   result1 = scoutsegment(&searchtri1, endpoint1, newmark);
9499   result2 = scoutsegment(&searchtri2, endpoint2, newmark);
9500   if (!result1) {
9501     /* The origin of searchtri1 may have changed if a collision with an */
9502     /*   intervening vertex on the segment occurred.                    */
9503     org(searchtri1, midpoint1);
9504     conformingedge(midpoint1, endpoint1, newmark);
9505   }
9506   if (!result2) {
9507     /* The origin of searchtri2 may have changed if a collision with an */
9508     /*   intervening vertex on the segment occurred.                    */
9509     org(searchtri2, midpoint2);
9510     conformingedge(midpoint2, endpoint2, newmark);
9511   }
9512 }
9513
9514 #endif /* not CDT_ONLY */
9515 #endif /* not REDUCED */
9516
9517 /*****************************************************************************/
9518 /*                                                                           */
9519 /*  delaunayfixup()   Enforce the Delaunay condition at an edge, fanning out */
9520 /*                    recursively from an existing point.  Pay special       */
9521 /*                    attention to stacking inverted triangles.              */
9522 /*                                                                           */
9523 /*  This is a support routine for inserting segments into a constrained      */
9524 /*  Delaunay triangulation.                                                  */
9525 /*                                                                           */
9526 /*  The origin of fixuptri is treated as if it has just been inserted, and   */
9527 /*  the local Delaunay condition needs to be enforced.  It is only enforced  */
9528 /*  in one sector, however, that being the angular range defined by          */
9529 /*  fixuptri.                                                                */
9530 /*                                                                           */
9531 /*  This routine also needs to make decisions regarding the "stacking" of    */
9532 /*  triangles.  (Read the description of constrainededge() below before      */
9533 /*  reading on here, so you understand the algorithm.)  If the position of   */
9534 /*  the new point (the origin of fixuptri) indicates that the vertex before  */
9535 /*  it on the polygon is a reflex vertex, then "stack" the triangle by       */
9536 /*  doing nothing.  (fixuptri is an inverted triangle, which is how stacked  */
9537 /*  triangles are identified.)                                               */
9538 /*                                                                           */
9539 /*  Otherwise, check whether the vertex before that was a reflex vertex.     */
9540 /*  If so, perform an edge flip, thereby eliminating an inverted triangle    */
9541 /*  (popping it off the stack).  The edge flip may result in the creation    */
9542 /*  of a new inverted triangle, depending on whether or not the new vertex   */
9543 /*  is visible to the vertex three edges behind on the polygon.              */
9544 /*                                                                           */
9545 /*  If neither of the two vertices behind the new vertex are reflex          */
9546 /*  vertices, fixuptri and fartri, the triangle opposite it, are not         */
9547 /*  inverted; hence, ensure that the edge between them is locally Delaunay.  */
9548 /*                                                                           */
9549 /*  `leftside' indicates whether or not fixuptri is to the left of the       */
9550 /*  segment being inserted.  (Imagine that the segment is pointing up from   */
9551 /*  endpoint1 to endpoint2.)                                                 */
9552 /*                                                                           */
9553 /*****************************************************************************/
9554
9555 void delaunayfixup(fixuptri, leftside)
9556 struct triedge *fixuptri;
9557 int leftside;
9558 {
9559   struct triedge neartri;
9560   struct triedge fartri;
9561   struct edge faredge;
9562   point nearpoint, leftpoint, rightpoint, farpoint;
9563   triangle ptr;                         /* Temporary variable used by sym(). */
9564   shelle sptr;                      /* Temporary variable used by tspivot(). */
9565
9566   lnext(*fixuptri, neartri);
9567   sym(neartri, fartri);
9568   /* Check if the edge opposite the origin of fixuptri can be flipped. */
9569   if (fartri.tri == dummytri) {
9570     return;
9571   }
9572   tspivot(neartri, faredge);
9573   if (faredge.sh != dummysh) {
9574     return;
9575   }
9576   /* Find all the relevant vertices. */
9577   apex(neartri, nearpoint);
9578   org(neartri, leftpoint);
9579   dest(neartri, rightpoint);
9580   apex(fartri, farpoint);
9581   /* Check whether the previous polygon vertex is a reflex vertex. */
9582   if (leftside) {
9583     if (counterclockwise(nearpoint, leftpoint, farpoint) <= 0.0) {
9584       /* leftpoint is a reflex vertex too.  Nothing can */
9585       /*   be done until a convex section is found.     */
9586       return;
9587     }
9588   } else {
9589     if (counterclockwise(farpoint, rightpoint, nearpoint) <= 0.0) {
9590       /* rightpoint is a reflex vertex too.  Nothing can */
9591       /*   be done until a convex section is found.      */
9592       return;
9593     }
9594   }
9595   if (counterclockwise(rightpoint, leftpoint, farpoint) > 0.0) {
9596     /* fartri is not an inverted triangle, and farpoint is not a reflex */
9597     /*   vertex.  As there are no reflex vertices, fixuptri isn't an    */
9598     /*   inverted triangle, either.  Hence, test the edge between the   */
9599     /*   triangles to ensure it is locally Delaunay.                    */
9600     if (incircle(leftpoint, farpoint, rightpoint, nearpoint) <= 0.0) {
9601       return;
9602     }
9603     /* Not locally Delaunay; go on to an edge flip. */
9604   }        /* else fartri is inverted; remove it from the stack by flipping. */
9605   flip(&neartri);
9606   lprevself(*fixuptri);    /* Restore the origin of fixuptri after the flip. */
9607   /* Recursively process the two triangles that result from the flip. */
9608   delaunayfixup(fixuptri, leftside);
9609   delaunayfixup(&fartri, leftside);
9610 }
9611
9612 /*****************************************************************************/
9613 /*                                                                           */
9614 /*  constrainededge()   Force a segment into a constrained Delaunay          */
9615 /*                      triangulation by deleting the triangles it           */
9616 /*                      intersects, and triangulating the polygons that      */
9617 /*                      form on each side of it.                             */
9618 /*                                                                           */
9619 /*  Generates a single edge connecting `endpoint1' to `endpoint2'.  The      */
9620 /*  triangle `starttri' has `endpoint1' as its origin.  `newmark' is the     */
9621 /*  boundary marker of the segment.                                          */
9622 /*                                                                           */
9623 /*  To insert a segment, every triangle whose interior intersects the        */
9624 /*  segment is deleted.  The union of these deleted triangles is a polygon   */
9625 /*  (which is not necessarily monotone, but is close enough), which is       */
9626 /*  divided into two polygons by the new segment.  This routine's task is    */
9627 /*  to generate the Delaunay triangulation of these two polygons.            */
9628 /*                                                                           */
9629 /*  You might think of this routine's behavior as a two-step process.  The   */
9630 /*  first step is to walk from endpoint1 to endpoint2, flipping each edge    */
9631 /*  encountered.  This step creates a fan of edges connected to endpoint1,   */
9632 /*  including the desired edge to endpoint2.  The second step enforces the   */
9633 /*  Delaunay condition on each side of the segment in an incremental manner: */
9634 /*  proceeding along the polygon from endpoint1 to endpoint2 (this is done   */
9635 /*  independently on each side of the segment), each vertex is "enforced"    */
9636 /*  as if it had just been inserted, but affecting only the previous         */
9637 /*  vertices.  The result is the same as if the vertices had been inserted   */
9638 /*  in the order they appear on the polygon, so the result is Delaunay.      */
9639 /*                                                                           */
9640 /*  In truth, constrainededge() interleaves these two steps.  The procedure  */
9641 /*  walks from endpoint1 to endpoint2, and each time an edge is encountered  */
9642 /*  and flipped, the newly exposed vertex (at the far end of the flipped     */
9643 /*  edge) is "enforced" upon the previously flipped edges, usually affecting */
9644 /*  only one side of the polygon (depending upon which side of the segment   */
9645 /*  the vertex falls on).                                                    */
9646 /*                                                                           */
9647 /*  The algorithm is complicated by the need to handle polygons that are not */
9648 /*  convex.  Although the polygon is not necessarily monotone, it can be     */
9649 /*  triangulated in a manner similar to the stack-based algorithms for       */
9650 /*  monotone polygons.  For each reflex vertex (local concavity) of the      */
9651 /*  polygon, there will be an inverted triangle formed by one of the edge    */
9652 /*  flips.  (An inverted triangle is one with negative area - that is, its   */
9653 /*  vertices are arranged in clockwise order - and is best thought of as a   */
9654 /*  wrinkle in the fabric of the mesh.)  Each inverted triangle can be       */
9655 /*  thought of as a reflex vertex pushed on the stack, waiting to be fixed   */
9656 /*  later.                                                                   */
9657 /*                                                                           */
9658 /*  A reflex vertex is popped from the stack when a vertex is inserted that  */
9659 /*  is visible to the reflex vertex.  (However, if the vertex behind the     */
9660 /*  reflex vertex is not visible to the reflex vertex, a new inverted        */
9661 /*  triangle will take its place on the stack.)  These details are handled   */
9662 /*  by the delaunayfixup() routine above.                                    */
9663 /*                                                                           */
9664 /*****************************************************************************/
9665
9666 void constrainededge(starttri, endpoint2, newmark)
9667 struct triedge *starttri;
9668 point endpoint2;
9669 int newmark;
9670 {
9671   struct triedge fixuptri, fixuptri2;
9672   struct edge fixupedge;
9673   point endpoint1;
9674   point farpoint;
9675   REAL area;
9676   int collision;
9677   int done;
9678   triangle ptr;             /* Temporary variable used by sym() and oprev(). */
9679   shelle sptr;                      /* Temporary variable used by tspivot(). */
9680
9681   org(*starttri, endpoint1);
9682   lnext(*starttri, fixuptri);
9683   flip(&fixuptri);
9684   /* `collision' indicates whether we have found a point directly */
9685   /*   between endpoint1 and endpoint2.                           */
9686   collision = 0;
9687   done = 0;
9688   do {
9689     org(fixuptri, farpoint);
9690     /* `farpoint' is the extreme point of the polygon we are "digging" */
9691     /*   to get from endpoint1 to endpoint2.                           */
9692     if ((farpoint[0] == endpoint2[0]) && (farpoint[1] == endpoint2[1])) {
9693       oprev(fixuptri, fixuptri2);
9694       /* Enforce the Delaunay condition around endpoint2. */
9695       delaunayfixup(&fixuptri, 0);
9696       delaunayfixup(&fixuptri2, 1);
9697       done = 1;
9698     } else {
9699       /* Check whether farpoint is to the left or right of the segment */
9700       /*   being inserted, to decide which edge of fixuptri to dig     */
9701       /*   through next.                                               */
9702       area = counterclockwise(endpoint1, endpoint2, farpoint);
9703       if (area == 0.0) {
9704         /* We've collided with a point between endpoint1 and endpoint2. */
9705         collision = 1;
9706         oprev(fixuptri, fixuptri2);
9707         /* Enforce the Delaunay condition around farpoint. */
9708         delaunayfixup(&fixuptri, 0);
9709         delaunayfixup(&fixuptri2, 1);
9710         done = 1;
9711       } else {
9712         if (area > 0.0) {         /* farpoint is to the left of the segment. */
9713           oprev(fixuptri, fixuptri2);
9714           /* Enforce the Delaunay condition around farpoint, on the */
9715           /*   left side of the segment only.                       */
9716           delaunayfixup(&fixuptri2, 1);
9717           /* Flip the edge that crosses the segment.  After the edge is */
9718           /*   flipped, one of its endpoints is the fan vertex, and the */
9719           /*   destination of fixuptri is the fan vertex.               */
9720           lprevself(fixuptri);
9721         } else {                 /* farpoint is to the right of the segment. */
9722           delaunayfixup(&fixuptri, 0);
9723           /* Flip the edge that crosses the segment.  After the edge is */
9724           /*   flipped, one of its endpoints is the fan vertex, and the */
9725           /*   destination of fixuptri is the fan vertex.               */
9726           oprevself(fixuptri);
9727         }
9728         /* Check for two intersecting segments. */
9729         tspivot(fixuptri, fixupedge);
9730         if (fixupedge.sh == dummysh) {
9731           flip(&fixuptri);   /* May create an inverted triangle on the left. */
9732         } else {
9733           /* We've collided with a segment between endpoint1 and endpoint2. */
9734           collision = 1;
9735           /* Insert a point at the intersection. */
9736           segmentintersection(&fixuptri, &fixupedge, endpoint2);
9737           done = 1;
9738         }
9739       }
9740     }
9741   } while (!done);
9742   /* Insert a shell edge to make the segment permanent. */
9743   insertshelle(&fixuptri, newmark);
9744   /* If there was a collision with an interceding vertex, install another */
9745   /*   segment connecting that vertex with endpoint2.                     */
9746   if (collision) {
9747     /* Insert the remainder of the segment. */
9748     if (!scoutsegment(&fixuptri, endpoint2, newmark)) {
9749       constrainededge(&fixuptri, endpoint2, newmark);
9750     }
9751   }
9752 }
9753
9754 /*****************************************************************************/
9755 /*                                                                           */
9756 /*  insertsegment()   Insert a PSLG segment into a triangulation.            */
9757 /*                                                                           */
9758 /*****************************************************************************/
9759
9760 void insertsegment(endpoint1, endpoint2, newmark)
9761 point endpoint1;
9762 point endpoint2;
9763 int newmark;
9764 {
9765   struct triedge searchtri1, searchtri2;
9766   triangle encodedtri;
9767   point checkpoint;
9768   triangle ptr;                         /* Temporary variable used by sym(). */
9769
9770   if (verbose > 1) {
9771     printf("  Connecting (%.12g, %.12g) to (%.12g, %.12g).\n",
9772            endpoint1[0], endpoint1[1], endpoint2[0], endpoint2[1]);
9773   }
9774
9775   /* Find a triangle whose origin is the segment's first endpoint. */
9776   checkpoint = (point) NULL;
9777   encodedtri = point2tri(endpoint1);
9778   if (encodedtri != (triangle) NULL) {
9779     decode(encodedtri, searchtri1);
9780     org(searchtri1, checkpoint);
9781   }
9782   if (checkpoint != endpoint1) {
9783     /* Find a boundary triangle to search from. */
9784     searchtri1.tri = dummytri;
9785     searchtri1.orient = 0;
9786     symself(searchtri1);
9787     /* Search for the segment's first endpoint by point location. */
9788     if (locate(endpoint1, &searchtri1) != ONVERTEX) {
9789       printf(
9790         "Internal error in insertsegment():  Unable to locate PSLG point\n");
9791       printf("  (%.12g, %.12g) in triangulation.\n",
9792              endpoint1[0], endpoint1[1]);
9793       internalerror();
9794     }
9795   }
9796   /* Remember this triangle to improve subsequent point location. */
9797   triedgecopy(searchtri1, recenttri);
9798   /* Scout the beginnings of a path from the first endpoint */
9799   /*   toward the second.                                   */
9800   if (scoutsegment(&searchtri1, endpoint2, newmark)) {
9801     /* The segment was easily inserted. */
9802     return;
9803   }
9804   /* The first endpoint may have changed if a collision with an intervening */
9805   /*   vertex on the segment occurred.                                      */
9806   org(searchtri1, endpoint1);
9807
9808   /* Find a triangle whose origin is the segment's second endpoint. */
9809   checkpoint = (point) NULL;
9810   encodedtri = point2tri(endpoint2);
9811   if (encodedtri != (triangle) NULL) {
9812     decode(encodedtri, searchtri2);
9813     org(searchtri2, checkpoint);
9814   }
9815   if (checkpoint != endpoint2) {
9816     /* Find a boundary triangle to search from. */
9817     searchtri2.tri = dummytri;
9818     searchtri2.orient = 0;
9819     symself(searchtri2);
9820     /* Search for the segment's second endpoint by point location. */
9821     if (locate(endpoint2, &searchtri2) != ONVERTEX) {
9822       printf(
9823         "Internal error in insertsegment():  Unable to locate PSLG point\n");
9824       printf("  (%.12g, %.12g) in triangulation.\n",
9825              endpoint2[0], endpoint2[1]);
9826       internalerror();
9827     }
9828   }
9829   /* Remember this triangle to improve subsequent point location. */
9830   triedgecopy(searchtri2, recenttri);
9831   /* Scout the beginnings of a path from the second endpoint */
9832   /*   toward the first.                                     */
9833   if (scoutsegment(&searchtri2, endpoint1, newmark)) {
9834     /* The segment was easily inserted. */
9835     return;
9836   }
9837   /* The second endpoint may have changed if a collision with an intervening */
9838   /*   vertex on the segment occurred.                                       */
9839   org(searchtri2, endpoint2);
9840
9841 #ifndef REDUCED
9842 #ifndef CDT_ONLY
9843   if (splitseg) {
9844     /* Insert vertices to force the segment into the triangulation. */
9845     conformingedge(endpoint1, endpoint2, newmark);
9846   } else {
9847 #endif /* not CDT_ONLY */
9848 #endif /* not REDUCED */
9849     /* Insert the segment directly into the triangulation. */
9850     constrainededge(&searchtri1, endpoint2, newmark);
9851 #ifndef REDUCED
9852 #ifndef CDT_ONLY
9853   }
9854 #endif /* not CDT_ONLY */
9855 #endif /* not REDUCED */
9856 }
9857
9858 /*****************************************************************************/
9859 /*                                                                           */
9860 /*  markhull()   Cover the convex hull of a triangulation with shell edges.  */
9861 /*                                                                           */
9862 /*****************************************************************************/
9863
9864 void markhull()
9865 {
9866   struct triedge hulltri;
9867   struct triedge nexttri;
9868   struct triedge starttri;
9869   triangle ptr;             /* Temporary variable used by sym() and oprev(). */
9870
9871   /* Find a triangle handle on the hull. */
9872   hulltri.tri = dummytri;
9873   hulltri.orient = 0;
9874   symself(hulltri);
9875   /* Remember where we started so we know when to stop. */
9876   triedgecopy(hulltri, starttri);
9877   /* Go once counterclockwise around the convex hull. */
9878   do {
9879     /* Create a shell edge if there isn't already one here. */
9880     insertshelle(&hulltri, 1);
9881     /* To find the next hull edge, go clockwise around the next vertex. */
9882     lnextself(hulltri);
9883     oprev(hulltri, nexttri);
9884     while (nexttri.tri != dummytri) {
9885       triedgecopy(nexttri, hulltri);
9886       oprev(hulltri, nexttri);
9887     }
9888   } while (!triedgeequal(hulltri, starttri));
9889 }
9890
9891 /*****************************************************************************/
9892 /*                                                                           */
9893 /*  formskeleton()   Create the shell edges of a triangulation, including    */
9894 /*                   PSLG edges and edges on the convex hull.                */
9895 /*                                                                           */
9896 /*  The PSLG edges are read from a .poly file.  The return value is the      */
9897 /*  number of segments in the file.                                          */
9898 /*                                                                           */
9899 /*****************************************************************************/
9900
9901 #ifdef TRILIBRARY
9902
9903 int formskeleton(segmentlist, segmentmarkerlist, numberofsegments)
9904 int *segmentlist;
9905 int *segmentmarkerlist;
9906 int numberofsegments;
9907
9908 #else /* not TRILIBRARY */
9909
9910 int formskeleton(polyfile, polyfilename)
9911 FILE *polyfile;
9912 char *polyfilename;
9913
9914 #endif /* not TRILIBRARY */
9915
9916 {
9917 #ifdef TRILIBRARY
9918   char polyfilename[6];
9919   int index;
9920 #else /* not TRILIBRARY */
9921   char inputline[INPUTLINESIZE];
9922   char *stringptr;
9923 #endif /* not TRILIBRARY */
9924   point endpoint1, endpoint2;
9925   int segments;
9926   int segmentmarkers;
9927   int end1, end2;
9928   int boundmarker;
9929   int i;
9930
9931   if (poly) {
9932     if (!quiet) {
9933       printf("Inserting segments into Delaunay triangulation.\n");
9934     }
9935 #ifdef TRILIBRARY
9936     strcpy(polyfilename, "input");
9937     segments = numberofsegments;
9938     segmentmarkers = segmentmarkerlist != (int *) NULL;
9939     index = 0;
9940 #else /* not TRILIBRARY */
9941     /* Read the segments from a .poly file. */
9942     /* Read number of segments and number of boundary markers. */
9943     stringptr = readline(inputline, polyfile, polyfilename);
9944     segments = (int) strtol (stringptr, &stringptr, 0);
9945     stringptr = findfield(stringptr);
9946     if (*stringptr == '\0') {
9947       segmentmarkers = 0;
9948     } else {
9949       segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
9950     }
9951 #endif /* not TRILIBRARY */
9952     /* If segments are to be inserted, compute a mapping */
9953     /*   from points to triangles.                       */
9954     if (segments > 0) {
9955       if (verbose) {
9956         printf("  Inserting PSLG segments.\n");
9957       }
9958       makepointmap();
9959     }
9960
9961     boundmarker = 0;
9962     /* Read and insert the segments. */
9963     for (i = 1; i <= segments; i++) {
9964 #ifdef TRILIBRARY
9965       end1 = segmentlist[index++];
9966       end2 = segmentlist[index++];
9967       if (segmentmarkers) {
9968         boundmarker = segmentmarkerlist[i - 1];
9969       }
9970 #else /* not TRILIBRARY */
9971       stringptr = readline(inputline, polyfile, inpolyfilename);
9972       stringptr = findfield(stringptr);
9973       if (*stringptr == '\0') {
9974         printf("Error:  Segment %d has no endpoints in %s.\n", i,
9975                polyfilename);
9976         exit(1);
9977       } else {
9978         end1 = (int) strtol (stringptr, &stringptr, 0);
9979       }
9980       stringptr = findfield(stringptr);
9981       if (*stringptr == '\0') {
9982         printf("Error:  Segment %d is missing its second endpoint in %s.\n", i,
9983                polyfilename);
9984         exit(1);
9985       } else {
9986         end2 = (int) strtol (stringptr, &stringptr, 0);
9987       }
9988       if (segmentmarkers) {
9989         stringptr = findfield(stringptr);
9990         if (*stringptr == '\0') {
9991           boundmarker = 0;
9992         } else {
9993           boundmarker = (int) strtol (stringptr, &stringptr, 0);
9994         }
9995       }
9996 #endif /* not TRILIBRARY */
9997       if ((end1 < firstnumber) || (end1 >= firstnumber + inpoints)) {
9998         if (!quiet) {
9999           printf("Warning:  Invalid first endpoint of segment %d in %s.\n", i,
10000                  polyfilename);
10001         }
10002       } else if ((end2 < firstnumber) || (end2 >= firstnumber + inpoints)) {
10003         if (!quiet) {
10004           printf("Warning:  Invalid second endpoint of segment %d in %s.\n", i,
10005                  polyfilename);
10006         }
10007       } else {
10008         endpoint1 = getpoint(end1);
10009         endpoint2 = getpoint(end2);
10010         if ((endpoint1[0] == endpoint2[0]) && (endpoint1[1] == endpoint2[1])) {
10011           if (!quiet) {
10012             printf("Warning:  Endpoints of segment %d are coincident in %s.\n",
10013                    i, polyfilename);
10014           }
10015         } else {
10016           insertsegment(endpoint1, endpoint2, boundmarker);
10017         }
10018       }
10019     }
10020   } else {
10021     segments = 0;
10022   }
10023   if (convex || !poly) {
10024     /* Enclose the convex hull with shell edges. */
10025     if (verbose) {
10026       printf("  Enclosing convex hull with segments.\n");
10027     }
10028     markhull();
10029   }
10030   return segments;
10031 }
10032
10033 /**                                                                         **/
10034 /**                                                                         **/
10035 /********* Segment (shell edge) insertion ends here                  *********/
10036
10037 /********* Carving out holes and concavities begins here             *********/
10038 /**                                                                         **/
10039 /**                                                                         **/
10040
10041 /*****************************************************************************/
10042 /*                                                                           */
10043 /*  infecthull()   Virally infect all of the triangles of the convex hull    */
10044 /*                 that are not protected by shell edges.  Where there are   */
10045 /*                 shell edges, set boundary markers as appropriate.         */
10046 /*                                                                           */
10047 /*****************************************************************************/
10048
10049 void infecthull()
10050 {
10051   struct triedge hulltri;
10052   struct triedge nexttri;
10053   struct triedge starttri;
10054   struct edge hulledge;
10055   triangle **deadtri;
10056   point horg, hdest;
10057   triangle ptr;                         /* Temporary variable used by sym(). */
10058   shelle sptr;                      /* Temporary variable used by tspivot(). */
10059
10060   if (verbose) {
10061     printf("  Marking concavities (external triangles) for elimination.\n");
10062   }
10063   /* Find a triangle handle on the hull. */
10064   hulltri.tri = dummytri;
10065   hulltri.orient = 0;
10066   symself(hulltri);
10067   /* Remember where we started so we know when to stop. */
10068   triedgecopy(hulltri, starttri);
10069   /* Go once counterclockwise around the convex hull. */
10070   do {
10071     /* Ignore triangles that are already infected. */
10072     if (!infected(hulltri)) {
10073       /* Is the triangle protected by a shell edge? */
10074       tspivot(hulltri, hulledge);
10075       if (hulledge.sh == dummysh) {
10076         /* The triangle is not protected; infect it. */
10077         infect(hulltri);
10078         deadtri = (triangle **) poolalloc(&viri);
10079         *deadtri = hulltri.tri;
10080       } else {
10081         /* The triangle is protected; set boundary markers if appropriate. */
10082         if (mark(hulledge) == 0) {
10083           setmark(hulledge, 1);
10084           org(hulltri, horg);
10085           dest(hulltri, hdest);
10086           if (pointmark(horg) == 0) {
10087             setpointmark(horg, 1);
10088           }
10089           if (pointmark(hdest) == 0) {
10090             setpointmark(hdest, 1);
10091           }
10092         }
10093       }
10094     }
10095     /* To find the next hull edge, go clockwise around the next vertex. */
10096     lnextself(hulltri);
10097     oprev(hulltri, nexttri);
10098     while (nexttri.tri != dummytri) {
10099       triedgecopy(nexttri, hulltri);
10100       oprev(hulltri, nexttri);
10101     }
10102   } while (!triedgeequal(hulltri, starttri));
10103 }
10104
10105 /*****************************************************************************/
10106 /*                                                                           */
10107 /*  plague()   Spread the virus from all infected triangles to any neighbors */
10108 /*             not protected by shell edges.  Delete all infected triangles. */
10109 /*                                                                           */
10110 /*  This is the procedure that actually creates holes and concavities.       */
10111 /*                                                                           */
10112 /*  This procedure operates in two phases.  The first phase identifies all   */
10113 /*  the triangles that will die, and marks them as infected.  They are       */
10114 /*  marked to ensure that each triangle is added to the virus pool only      */
10115 /*  once, so the procedure will terminate.                                   */
10116 /*                                                                           */
10117 /*  The second phase actually eliminates the infected triangles.  It also    */
10118 /*  eliminates orphaned points.                                              */
10119 /*                                                                           */
10120 /*****************************************************************************/
10121
10122 void plague()
10123 {
10124   struct triedge testtri;
10125   struct triedge neighbor;
10126   triangle **virusloop;
10127   triangle **deadtri;
10128   struct edge neighborshelle;
10129   point testpoint;
10130   point norg, ndest;
10131   point deadorg, deaddest, deadapex;
10132   int killorg;
10133   triangle ptr;             /* Temporary variable used by sym() and onext(). */
10134   shelle sptr;                      /* Temporary variable used by tspivot(). */
10135
10136   if (verbose) {
10137     printf("  Marking neighbors of marked triangles.\n");
10138   }
10139   /* Loop through all the infected triangles, spreading the virus to */
10140   /*   their neighbors, then to their neighbors' neighbors.          */
10141   traversalinit(&viri);
10142   virusloop = (triangle **) traverse(&viri);
10143   while (virusloop != (triangle **) NULL) {
10144     testtri.tri = *virusloop;
10145     /* A triangle is marked as infected by messing with one of its shell */
10146     /*   edges, setting it to an illegal value.  Hence, we have to       */
10147     /*   temporarily uninfect this triangle so that we can examine its   */
10148     /*   adjacent shell edges.                                           */
10149     uninfect(testtri);
10150     if (verbose > 2) {
10151       /* Assign the triangle an orientation for convenience in */
10152       /*   checking its points.                                */
10153       testtri.orient = 0;
10154       org(testtri, deadorg);
10155       dest(testtri, deaddest);
10156       apex(testtri, deadapex);
10157       printf("    Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10158              deadorg[0], deadorg[1], deaddest[0], deaddest[1],
10159              deadapex[0], deadapex[1]);
10160     }
10161     /* Check each of the triangle's three neighbors. */
10162     for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10163       /* Find the neighbor. */
10164       sym(testtri, neighbor);
10165       /* Check for a shell between the triangle and its neighbor. */
10166       tspivot(testtri, neighborshelle);
10167       /* Check if the neighbor is nonexistent or already infected. */
10168       if ((neighbor.tri == dummytri) || infected(neighbor)) {
10169         if (neighborshelle.sh != dummysh) {
10170           /* There is a shell edge separating the triangle from its */
10171           /*   neighbor, but both triangles are dying, so the shell */
10172           /*   edge dies too.                                       */
10173           shelledealloc(neighborshelle.sh);
10174           if (neighbor.tri != dummytri) {
10175             /* Make sure the shell edge doesn't get deallocated again */
10176             /*   later when the infected neighbor is visited.         */
10177             uninfect(neighbor);
10178             tsdissolve(neighbor);
10179             infect(neighbor);
10180           }
10181         }
10182       } else {                   /* The neighbor exists and is not infected. */
10183         if (neighborshelle.sh == dummysh) {
10184           /* There is no shell edge protecting the neighbor, so */
10185           /*   the neighbor becomes infected.                   */
10186           if (verbose > 2) {
10187             org(neighbor, deadorg);
10188             dest(neighbor, deaddest);
10189             apex(neighbor, deadapex);
10190             printf(
10191               "    Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10192                    deadorg[0], deadorg[1], deaddest[0], deaddest[1],
10193                    deadapex[0], deadapex[1]);
10194           }
10195           infect(neighbor);
10196           /* Ensure that the neighbor's neighbors will be infected. */
10197           deadtri = (triangle **) poolalloc(&viri);
10198           *deadtri = neighbor.tri;
10199         } else {               /* The neighbor is protected by a shell edge. */
10200           /* Remove this triangle from the shell edge. */
10201           stdissolve(neighborshelle);
10202           /* The shell edge becomes a boundary.  Set markers accordingly. */
10203           if (mark(neighborshelle) == 0) {
10204             setmark(neighborshelle, 1);
10205           }
10206           org(neighbor, norg);
10207           dest(neighbor, ndest);
10208           if (pointmark(norg) == 0) {
10209             setpointmark(norg, 1);
10210           }
10211           if (pointmark(ndest) == 0) {
10212             setpointmark(ndest, 1);
10213           }
10214         }
10215       }
10216     }
10217     /* Remark the triangle as infected, so it doesn't get added to the */
10218     /*   virus pool again.                                             */
10219     infect(testtri);
10220     virusloop = (triangle **) traverse(&viri);
10221   }
10222
10223   if (verbose) {
10224     printf("  Deleting marked triangles.\n");
10225   }
10226   traversalinit(&viri);
10227   virusloop = (triangle **) traverse(&viri);
10228   while (virusloop != (triangle **) NULL) {
10229     testtri.tri = *virusloop;
10230
10231     /* Check each of the three corners of the triangle for elimination. */
10232     /*   This is done by walking around each point, checking if it is   */
10233     /*   still connected to at least one live triangle.                 */
10234     for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10235       org(testtri, testpoint);
10236       /* Check if the point has already been tested. */
10237       if (testpoint != (point) NULL) {
10238         killorg = 1;
10239         /* Mark the corner of the triangle as having been tested. */
10240         setorg(testtri, NULL);
10241         /* Walk counterclockwise about the point. */
10242         onext(testtri, neighbor);
10243         /* Stop upon reaching a boundary or the starting triangle. */
10244         while ((neighbor.tri != dummytri)
10245                && (!triedgeequal(neighbor, testtri))) {
10246           if (infected(neighbor)) {
10247             /* Mark the corner of this triangle as having been tested. */
10248             setorg(neighbor, NULL);
10249           } else {
10250             /* A live triangle.  The point survives. */
10251             killorg = 0;
10252           }
10253           /* Walk counterclockwise about the point. */
10254           onextself(neighbor);
10255         }
10256         /* If we reached a boundary, we must walk clockwise as well. */
10257         if (neighbor.tri == dummytri) {
10258           /* Walk clockwise about the point. */
10259           oprev(testtri, neighbor);
10260           /* Stop upon reaching a boundary. */
10261           while (neighbor.tri != dummytri) {
10262             if (infected(neighbor)) {
10263             /* Mark the corner of this triangle as having been tested. */
10264               setorg(neighbor, NULL);
10265             } else {
10266               /* A live triangle.  The point survives. */
10267               killorg = 0;
10268             }
10269             /* Walk clockwise about the point. */
10270             oprevself(neighbor);
10271           }
10272         }
10273         if (killorg) {
10274           if (verbose > 1) {
10275             printf("    Deleting point (%.12g, %.12g)\n",
10276                    testpoint[0], testpoint[1]);
10277           }
10278           pointdealloc(testpoint);
10279         }
10280       }
10281     }
10282
10283     /* Record changes in the number of boundary edges, and disconnect */
10284     /*   dead triangles from their neighbors.                         */
10285     for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10286       sym(testtri, neighbor);
10287       if (neighbor.tri == dummytri) {
10288         /* There is no neighboring triangle on this edge, so this edge    */
10289         /*   is a boundary edge.  This triangle is being deleted, so this */
10290         /*   boundary edge is deleted.                                    */
10291         hullsize--;
10292       } else {
10293         /* Disconnect the triangle from its neighbor. */
10294         dissolve(neighbor);
10295         /* There is a neighboring triangle on this edge, so this edge */
10296         /*   becomes a boundary edge when this triangle is deleted.   */
10297         hullsize++;
10298       }
10299     }
10300     /* Return the dead triangle to the pool of triangles. */
10301     triangledealloc(testtri.tri);
10302     virusloop = (triangle **) traverse(&viri);
10303   }
10304   /* Empty the virus pool. */
10305   poolrestart(&viri);
10306 }
10307
10308 /*****************************************************************************/
10309 /*                                                                           */
10310 /*  regionplague()   Spread regional attributes and/or area constraints      */
10311 /*                   (from a .poly file) throughout the mesh.                */
10312 /*                                                                           */
10313 /*  This procedure operates in two phases.  The first phase spreads an       */
10314 /*  attribute and/or an area constraint through a (segment-bounded) region.  */
10315 /*  The triangles are marked to ensure that each triangle is added to the    */
10316 /*  virus pool only once, so the procedure will terminate.                   */
10317 /*                                                                           */
10318 /*  The second phase uninfects all infected triangles, returning them to     */
10319 /*  normal.                                                                  */
10320 /*                                                                           */
10321 /*****************************************************************************/
10322
10323 void regionplague(attribute, area)
10324 REAL attribute;
10325 REAL area;
10326 {
10327   struct triedge testtri;
10328   struct triedge neighbor;
10329   triangle **virusloop;
10330   triangle **regiontri;
10331   struct edge neighborshelle;
10332   point regionorg, regiondest, regionapex;
10333   triangle ptr;             /* Temporary variable used by sym() and onext(). */
10334   shelle sptr;                      /* Temporary variable used by tspivot(). */
10335
10336   if (verbose > 1) {
10337     printf("  Marking neighbors of marked triangles.\n");
10338   }
10339   /* Loop through all the infected triangles, spreading the attribute      */
10340   /*   and/or area constraint to their neighbors, then to their neighbors' */
10341   /*   neighbors.                                                          */
10342   traversalinit(&viri);
10343   virusloop = (triangle **) traverse(&viri);
10344   while (virusloop != (triangle **) NULL) {
10345     testtri.tri = *virusloop;
10346     /* A triangle is marked as infected by messing with one of its shell */
10347     /*   edges, setting it to an illegal value.  Hence, we have to       */
10348     /*   temporarily uninfect this triangle so that we can examine its   */
10349     /*   adjacent shell edges.                                           */
10350     uninfect(testtri);
10351     if (regionattrib) {
10352       /* Set an attribute. */
10353       setelemattribute(testtri, eextras, attribute);
10354     }
10355     if (vararea) {
10356       /* Set an area constraint. */
10357       setareabound(testtri, area);
10358     }
10359     if (verbose > 2) {
10360       /* Assign the triangle an orientation for convenience in */
10361       /*   checking its points.                                */
10362       testtri.orient = 0;
10363       org(testtri, regionorg);
10364       dest(testtri, regiondest);
10365       apex(testtri, regionapex);
10366       printf("    Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10367              regionorg[0], regionorg[1], regiondest[0], regiondest[1],
10368              regionapex[0], regionapex[1]);
10369     }
10370     /* Check each of the triangle's three neighbors. */
10371     for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10372       /* Find the neighbor. */
10373       sym(testtri, neighbor);
10374       /* Check for a shell between the triangle and its neighbor. */
10375       tspivot(testtri, neighborshelle);
10376       /* Make sure the neighbor exists, is not already infected, and */
10377       /*   isn't protected by a shell edge.                          */
10378       if ((neighbor.tri != dummytri) && !infected(neighbor)
10379           && (neighborshelle.sh == dummysh)) {
10380         if (verbose > 2) {
10381           org(neighbor, regionorg);
10382           dest(neighbor, regiondest);
10383           apex(neighbor, regionapex);
10384           printf("    Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10385                  regionorg[0], regionorg[1], regiondest[0], regiondest[1],
10386                  regionapex[0], regionapex[1]);
10387         }
10388         /* Infect the neighbor. */
10389         infect(neighbor);
10390         /* Ensure that the neighbor's neighbors will be infected. */
10391         regiontri = (triangle **) poolalloc(&viri);
10392         *regiontri = neighbor.tri;
10393       }
10394     }
10395     /* Remark the triangle as infected, so it doesn't get added to the */
10396     /*   virus pool again.                                             */
10397     infect(testtri);
10398     virusloop = (triangle **) traverse(&viri);
10399   }
10400
10401   /* Uninfect all triangles. */
10402   if (verbose > 1) {
10403     printf("  Unmarking marked triangles.\n");
10404   }
10405   traversalinit(&viri);
10406   virusloop = (triangle **) traverse(&viri);
10407   while (virusloop != (triangle **) NULL) {
10408     testtri.tri = *virusloop;
10409     uninfect(testtri);
10410     virusloop = (triangle **) traverse(&viri);
10411   }
10412   /* Empty the virus pool. */
10413   poolrestart(&viri);
10414 }
10415
10416 /*****************************************************************************/
10417 /*                                                                           */
10418 /*  carveholes()   Find the holes and infect them.  Find the area            */
10419 /*                 constraints and infect them.  Infect the convex hull.     */
10420 /*                 Spread the infection and kill triangles.  Spread the      */
10421 /*                 area constraints.                                         */
10422 /*                                                                           */
10423 /*  This routine mainly calls other routines to carry out all these          */
10424 /*  functions.                                                               */
10425 /*                                                                           */
10426 /*****************************************************************************/
10427
10428 void carveholes(holelist, holes, regionlist, regions)
10429 REAL *holelist;
10430 int holes;
10431 REAL *regionlist;
10432 int regions;
10433 {
10434   struct triedge searchtri;
10435   struct triedge triangleloop;
10436   struct triedge *regiontris;
10437   triangle **holetri;
10438   triangle **regiontri;
10439   point searchorg, searchdest;
10440   enum locateresult intersect;
10441   int i;
10442   triangle ptr;                         /* Temporary variable used by sym(). */
10443
10444   if (!(quiet || (noholes && convex))) {
10445     printf("Removing unwanted triangles.\n");
10446     if (verbose && (holes > 0)) {
10447       printf("  Marking holes for elimination.\n");
10448     }
10449   }
10450
10451   if (regions > 0) {
10452     /* Allocate storage for the triangles in which region points fall. */
10453     regiontris = (struct triedge *) malloc(regions * sizeof(struct triedge));
10454     if (regiontris == (struct triedge *) NULL) {
10455       printf("Error:  Out of memory.\n");
10456       exit(1);
10457     }
10458   }
10459
10460   if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
10461     /* Initialize a pool of viri to be used for holes, concavities, */
10462     /*   regional attributes, and/or regional area constraints.     */
10463     poolinit(&viri, sizeof(triangle *), VIRUSPERBLOCK, POINTER, 0);
10464   }
10465
10466   if (!convex) {
10467     /* Mark as infected any unprotected triangles on the boundary. */
10468     /*   This is one way by which concavities are created.         */
10469     infecthull();
10470   }
10471
10472   if ((holes > 0) && !noholes) {
10473     /* Infect each triangle in which a hole lies. */
10474     for (i = 0; i < 2 * holes; i += 2) {
10475       /* Ignore holes that aren't within the bounds of the mesh. */
10476       if ((holelist[i] >= xmin) && (holelist[i] <= xmax)
10477           && (holelist[i + 1] >= ymin) && (holelist[i + 1] <= ymax)) {
10478         /* Start searching from some triangle on the outer boundary. */
10479         searchtri.tri = dummytri;
10480         searchtri.orient = 0;
10481         symself(searchtri);
10482         /* Ensure that the hole is to the left of this boundary edge; */
10483         /*   otherwise, locate() will falsely report that the hole    */
10484         /*   falls within the starting triangle.                      */
10485         org(searchtri, searchorg);
10486         dest(searchtri, searchdest);
10487         if (counterclockwise(searchorg, searchdest, &holelist[i]) > 0.0) {
10488           /* Find a triangle that contains the hole. */
10489           intersect = locate(&holelist[i], &searchtri);
10490           if ((intersect != OUTSIDE) && (!infected(searchtri))) {
10491             /* Infect the triangle.  This is done by marking the triangle */
10492             /*   as infect and including the triangle in the virus pool.  */
10493             infect(searchtri);
10494             holetri = (triangle **) poolalloc(&viri);
10495             *holetri = searchtri.tri;
10496           }
10497         }
10498       }
10499     }
10500   }
10501
10502   /* Now, we have to find all the regions BEFORE we carve the holes, because */
10503   /*   locate() won't work when the triangulation is no longer convex.       */
10504   /*   (Incidentally, this is the reason why regional attributes and area    */
10505   /*   constraints can't be used when refining a preexisting mesh, which     */
10506   /*   might not be convex; they can only be used with a freshly             */
10507   /*   triangulated PSLG.)                                                   */
10508   if (regions > 0) {
10509     /* Find the starting triangle for each region. */
10510     for (i = 0; i < regions; i++) {
10511       regiontris[i].tri = dummytri;
10512       /* Ignore region points that aren't within the bounds of the mesh. */
10513       if ((regionlist[4 * i] >= xmin) && (regionlist[4 * i] <= xmax) &&
10514           (regionlist[4 * i + 1] >= ymin) && (regionlist[4 * i + 1] <= ymax)) {
10515         /* Start searching from some triangle on the outer boundary. */
10516         searchtri.tri = dummytri;
10517         searchtri.orient = 0;
10518         symself(searchtri);
10519         /* Ensure that the region point is to the left of this boundary */
10520         /*   edge; otherwise, locate() will falsely report that the     */
10521         /*   region point falls within the starting triangle.           */
10522         org(searchtri, searchorg);
10523         dest(searchtri, searchdest);
10524         if (counterclockwise(searchorg, searchdest, &regionlist[4 * i]) >
10525             0.0) {
10526           /* Find a triangle that contains the region point. */
10527           intersect = locate(&regionlist[4 * i], &searchtri);
10528           if ((intersect != OUTSIDE) && (!infected(searchtri))) {
10529             /* Record the triangle for processing after the */
10530             /*   holes have been carved.                    */
10531             triedgecopy(searchtri, regiontris[i]);
10532           }
10533         }
10534       }
10535     }
10536   }
10537
10538   if (viri.items > 0) {
10539     /* Carve the holes and concavities. */
10540     plague();
10541   }
10542   /* The virus pool should be empty now. */
10543
10544   if (regions > 0) {
10545     if (!quiet) {
10546       if (regionattrib) {
10547         if (vararea) {
10548           printf("Spreading regional attributes and area constraints.\n");
10549         } else {
10550           printf("Spreading regional attributes.\n");
10551         }
10552       } else { 
10553         printf("Spreading regional area constraints.\n");
10554       }
10555     }
10556     if (regionattrib && !refine) {
10557       /* Assign every triangle a regional attribute of zero. */
10558       traversalinit(&triangles);
10559       triangleloop.orient = 0;
10560       triangleloop.tri = triangletraverse();
10561       while (triangleloop.tri != (triangle *) NULL) {
10562         setelemattribute(triangleloop, eextras, 0.0);
10563         triangleloop.tri = triangletraverse();
10564       }
10565     }
10566     for (i = 0; i < regions; i++) {
10567       if (regiontris[i].tri != dummytri) {
10568         /* Make sure the triangle under consideration still exists. */
10569         /*   It may have been eaten by the virus.                   */
10570         if (regiontris[i].tri[3] != (triangle) NULL) {
10571           /* Put one triangle in the virus pool. */
10572           infect(regiontris[i]);
10573           regiontri = (triangle **) poolalloc(&viri);
10574           *regiontri = regiontris[i].tri;
10575           /* Apply one region's attribute and/or area constraint. */
10576           regionplague(regionlist[4 * i + 2], regionlist[4 * i + 3]);
10577           /* The virus pool should be empty now. */
10578         }
10579       }
10580     }
10581     if (regionattrib && !refine) {
10582       /* Note the fact that each triangle has an additional attribute. */
10583       eextras++;
10584     }
10585   }
10586
10587   /* Free up memory. */
10588   if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
10589     pooldeinit(&viri);
10590   }
10591   if (regions > 0) {
10592     free(regiontris);
10593   }
10594 }
10595
10596 /**                                                                         **/
10597 /**                                                                         **/
10598 /********* Carving out holes and concavities ends here               *********/
10599
10600 /********* Mesh quality maintenance begins here                      *********/
10601 /**                                                                         **/
10602 /**                                                                         **/
10603
10604 /*****************************************************************************/
10605 /*                                                                           */
10606 /*  tallyencs()   Traverse the entire list of shell edges, check each edge   */
10607 /*                to see if it is encroached.  If so, add it to the list.    */
10608 /*                                                                           */
10609 /*****************************************************************************/
10610
10611 #ifndef CDT_ONLY
10612
10613 void tallyencs()
10614 {
10615   struct edge edgeloop;
10616   int dummy;
10617
10618   traversalinit(&shelles);
10619   edgeloop.shorient = 0;
10620   edgeloop.sh = shelletraverse();
10621   while (edgeloop.sh != (shelle *) NULL) {
10622     /* If the segment is encroached, add it to the list. */
10623     dummy = checkedge4encroach(&edgeloop);
10624     edgeloop.sh = shelletraverse();
10625   }
10626 }
10627
10628 #endif /* not CDT_ONLY */
10629
10630 /*****************************************************************************/
10631 /*                                                                           */
10632 /*  precisionerror()  Print an error message for precision problems.         */
10633 /*                                                                           */
10634 /*****************************************************************************/
10635
10636 #ifndef CDT_ONLY
10637
10638 void precisionerror()
10639 {
10640   printf("Try increasing the area criterion and/or reducing the minimum\n");
10641   printf("  allowable angle so that tiny triangles are not created.\n");
10642 #ifdef SINGLE
10643   printf("Alternatively, try recompiling me with double precision\n");
10644   printf("  arithmetic (by removing \"#define SINGLE\" from the\n");
10645   printf("  source file or \"-DSINGLE\" from the makefile).\n");
10646 #endif /* SINGLE */
10647 }
10648
10649 #endif /* not CDT_ONLY */
10650
10651 /*****************************************************************************/
10652 /*                                                                           */
10653 /*  repairencs()   Find and repair all the encroached segments.              */
10654 /*                                                                           */
10655 /*  Encroached segments are repaired by splitting them by inserting a point  */
10656 /*  at or near their centers.                                                */
10657 /*                                                                           */
10658 /*  `flaws' is a flag that specifies whether one should take note of new     */
10659 /*  encroached segments and bad triangles that result from inserting points  */
10660 /*  to repair existing encroached segments.                                  */
10661 /*                                                                           */
10662 /*  When a segment is split, the two resulting subsegments are always        */
10663 /*  tested to see if they are encroached upon, regardless of the value       */
10664 /*  of `flaws'.                                                              */
10665 /*                                                                           */
10666 /*****************************************************************************/
10667
10668 #ifndef CDT_ONLY
10669
10670 void repairencs(flaws)
10671 int flaws;
10672 {
10673   struct triedge enctri;
10674   struct triedge testtri;
10675   struct edge *encloop;
10676   struct edge testsh;
10677   point eorg, edest;
10678   point newpoint;
10679   enum insertsiteresult success;
10680   REAL segmentlength, nearestpoweroftwo;
10681   REAL split;
10682   int acuteorg, acutedest;
10683   int dummy;
10684   int i;
10685   triangle ptr;                     /* Temporary variable used by stpivot(). */
10686   shelle sptr;                        /* Temporary variable used by snext(). */
10687
10688   while ((badsegments.items > 0) && (steinerleft != 0)) {
10689     traversalinit(&badsegments);
10690     encloop = badsegmenttraverse();
10691     while ((encloop != (struct edge *) NULL) && (steinerleft != 0)) {
10692       /* To decide where to split a segment, we need to know if the  */
10693       /*   segment shares an endpoint with an adjacent segment.      */
10694       /*   The concern is that, if we simply split every encroached  */
10695       /*   segment in its center, two adjacent segments with a small */
10696       /*   angle between them might lead to an infinite loop; each   */
10697       /*   point added to split one segment will encroach upon the   */
10698       /*   other segment, which must then be split with a point that */
10699       /*   will encroach upon the first segment, and so on forever.  */
10700       /* To avoid this, imagine a set of concentric circles, whose   */
10701       /*   radii are powers of two, about each segment endpoint.     */
10702       /*   These concentric circles determine where the segment is   */
10703       /*   split.  (If both endpoints are shared with adjacent       */
10704       /*   segments, split the segment in the middle, and apply the  */
10705       /*   concentric shells for later splittings.)                  */
10706
10707       /* Is the origin shared with another segment? */
10708       stpivot(*encloop, enctri);
10709       lnext(enctri, testtri);
10710       tspivot(testtri, testsh);
10711       acuteorg = testsh.sh != dummysh;
10712       /* Is the destination shared with another segment? */
10713       lnextself(testtri);
10714       tspivot(testtri, testsh);
10715       acutedest = testsh.sh != dummysh;
10716       /* Now, check the other side of the segment, if there's a triangle */
10717       /*   there.                                                        */
10718       sym(enctri, testtri);
10719       if (testtri.tri != dummytri) {
10720         /* Is the destination shared with another segment? */
10721         lnextself(testtri);
10722         tspivot(testtri, testsh);
10723         acutedest = acutedest || (testsh.sh != dummysh);
10724         /* Is the origin shared with another segment? */
10725         lnextself(testtri);
10726         tspivot(testtri, testsh);
10727         acuteorg = acuteorg || (testsh.sh != dummysh);
10728       }
10729
10730       sorg(*encloop, eorg);
10731       sdest(*encloop, edest);
10732       /* Use the concentric circles if exactly one endpoint is shared */
10733       /*   with another adjacent segment.                             */
10734       if (acuteorg ^ acutedest) {
10735         segmentlength = sqrt((edest[0] - eorg[0]) * (edest[0] - eorg[0])
10736                              + (edest[1] - eorg[1]) * (edest[1] - eorg[1]));
10737         /* Find the power of two nearest the segment's length. */
10738         nearestpoweroftwo = 1.0;
10739         while (segmentlength > SQUAREROOTTWO * nearestpoweroftwo) {
10740           nearestpoweroftwo *= 2.0;
10741         }
10742         while (segmentlength < (0.5 * SQUAREROOTTWO) * nearestpoweroftwo) {
10743           nearestpoweroftwo *= 0.5;
10744         }
10745         /* Where do we split the segment? */
10746         split = 0.5 * nearestpoweroftwo / segmentlength;
10747         if (acutedest) {
10748           split = 1.0 - split;
10749         }
10750       } else {
10751         /* If we're not worried about adjacent segments, split */
10752         /*   this segment in the middle.                       */
10753         split = 0.5;
10754       }
10755
10756       /* Create the new point. */
10757       newpoint = (point) poolalloc(&points);
10758       /* Interpolate its coordinate and attributes. */
10759       for (i = 0; i < 2 + nextras; i++) {
10760         newpoint[i] = (1.0 - split) * eorg[i] + split * edest[i];
10761       }
10762       setpointmark(newpoint, mark(*encloop));
10763       if (verbose > 1) {
10764         printf(
10765         "  Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
10766                eorg[0], eorg[1], edest[0], edest[1], newpoint[0], newpoint[1]);
10767       }
10768       /* Check whether the new point lies on an endpoint. */
10769       if (((newpoint[0] == eorg[0]) && (newpoint[1] == eorg[1]))
10770         || ((newpoint[0] == edest[0]) && (newpoint[1] == edest[1]))) {
10771         printf("Error:  Ran out of precision at (%.12g, %.12g).\n",
10772                newpoint[0], newpoint[1]);
10773         printf("I attempted to split a segment to a smaller size than can\n");
10774         printf("  be accommodated by the finite precision of floating point\n"
10775                );
10776         printf("  arithmetic.\n");
10777         precisionerror();
10778         exit(1);
10779       }
10780       /* Insert the splitting point.  This should always succeed. */
10781       success = insertsite(newpoint, &enctri, encloop, flaws, flaws);
10782       if ((success != SUCCESSFULPOINT) && (success != ENCROACHINGPOINT)) {
10783         printf("Internal error in repairencs():\n");
10784         printf("  Failure to split a segment.\n");
10785         internalerror();
10786       }
10787       if (steinerleft > 0) {
10788         steinerleft--;
10789       }
10790       /* Check the two new subsegments to see if they're encroached. */
10791       dummy = checkedge4encroach(encloop);
10792       snextself(*encloop);
10793       dummy = checkedge4encroach(encloop);
10794
10795       badsegmentdealloc(encloop);
10796       encloop = badsegmenttraverse();
10797     }
10798   }
10799 }
10800
10801 #endif /* not CDT_ONLY */
10802
10803 /*****************************************************************************/
10804 /*                                                                           */
10805 /*  tallyfaces()   Test every triangle in the mesh for quality measures.     */
10806 /*                                                                           */
10807 /*****************************************************************************/
10808
10809 #ifndef CDT_ONLY
10810
10811 void tallyfaces()
10812 {
10813   struct triedge triangleloop;
10814
10815   if (verbose) {
10816     printf("  Making a list of bad triangles.\n");
10817   }
10818   traversalinit(&triangles);
10819   triangleloop.orient = 0;
10820   triangleloop.tri = triangletraverse();
10821   while (triangleloop.tri != (triangle *) NULL) {
10822     /* If the triangle is bad, enqueue it. */
10823     testtriangle(&triangleloop);
10824     triangleloop.tri = triangletraverse();
10825   }
10826 }
10827
10828 #endif /* not CDT_ONLY */
10829
10830 /*****************************************************************************/
10831 /*                                                                           */
10832 /*  findcircumcenter()   Find the circumcenter of a triangle.                */
10833 /*                                                                           */
10834 /*  The result is returned both in terms of x-y coordinates and xi-eta       */
10835 /*  coordinates.  The xi-eta coordinate system is defined in terms of the    */
10836 /*  triangle:  the origin of the triangle is the origin of the coordinate    */
10837 /*  system; the destination of the triangle is one unit along the xi axis;   */
10838 /*  and the apex of the triangle is one unit along the eta axis.             */
10839 /*                                                                           */
10840 /*  The return value indicates which edge of the triangle is shortest.       */
10841 /*                                                                           */
10842 /*****************************************************************************/
10843
10844 enum circumcenterresult findcircumcenter(torg, tdest, tapex, circumcenter,
10845                                          xi, eta)
10846 point torg;
10847 point tdest;
10848 point tapex;
10849 point circumcenter;
10850 REAL *xi;
10851 REAL *eta;
10852 {
10853   REAL xdo, ydo, xao, yao, xad, yad;
10854   REAL dodist, aodist, addist;
10855   REAL denominator;
10856   REAL dx, dy;
10857
10858   circumcentercount++;
10859
10860   /* Compute the circumcenter of the triangle. */
10861   xdo = tdest[0] - torg[0];
10862   ydo = tdest[1] - torg[1];
10863   xao = tapex[0] - torg[0];
10864   yao = tapex[1] - torg[1];
10865   dodist = xdo * xdo + ydo * ydo;
10866   aodist = xao * xao + yao * yao;
10867   if (noexact) {
10868     denominator = 0.5 / (xdo * yao - xao * ydo);
10869   } else {
10870     /* Use the counterclockwise() routine to ensure a positive (and */
10871     /*   reasonably accurate) result, avoiding any possibility of   */
10872     /*   division by zero.                                          */
10873     denominator = 0.5 / counterclockwise(tdest, tapex, torg);
10874     /* Don't count the above as an orientation test. */
10875     counterclockcount--;
10876   }
10877   circumcenter[0] = torg[0] - (ydo * aodist - yao * dodist) * denominator;  
10878   circumcenter[1] = torg[1] + (xdo * aodist - xao * dodist) * denominator;  
10879
10880   /* To interpolate point attributes for the new point inserted at  */
10881   /*   the circumcenter, define a coordinate system with a xi-axis, */
10882   /*   directed from the triangle's origin to its destination, and  */
10883   /*   an eta-axis, directed from its origin to its apex.           */
10884   /*   Calculate the xi and eta coordinates of the circumcenter.    */
10885   dx = circumcenter[0] - torg[0];
10886   dy = circumcenter[1] - torg[1];
10887   *xi = (dx * yao - xao * dy) * (2.0 * denominator);
10888   *eta = (xdo * dy - dx * ydo) * (2.0 * denominator);
10889
10890   xad = tapex[0] - tdest[0];
10891   yad = tapex[1] - tdest[1];
10892   addist = xad * xad + yad * yad;
10893   if ((addist < dodist) && (addist < aodist)) {
10894     return OPPOSITEORG;
10895   } else if (dodist < aodist) {
10896     return OPPOSITEAPEX;
10897   } else {
10898     return OPPOSITEDEST;
10899   }
10900 }
10901
10902 /*****************************************************************************/
10903 /*                                                                           */
10904 /*  splittriangle()   Inserts a point at the circumcenter of a triangle.     */
10905 /*                    Deletes the newly inserted point if it encroaches upon */
10906 /*                    a segment.                                             */
10907 /*                                                                           */
10908 /*****************************************************************************/
10909
10910 #ifndef CDT_ONLY
10911
10912 void splittriangle(badtri)
10913 struct badface *badtri;
10914 {
10915   point borg, bdest, bapex;
10916   point newpoint;
10917   REAL xi, eta;
10918   enum insertsiteresult success;
10919   enum circumcenterresult shortedge;
10920   int errorflag;
10921   int i;
10922
10923   org(badtri->badfacetri, borg);
10924   dest(badtri->badfacetri, bdest);
10925   apex(badtri->badfacetri, bapex);
10926   /* Make sure that this triangle is still the same triangle it was      */
10927   /*   when it was tested and determined to be of bad quality.           */
10928   /*   Subsequent transformations may have made it a different triangle. */
10929   if ((borg == badtri->faceorg) && (bdest == badtri->facedest) &&
10930       (bapex == badtri->faceapex)) {
10931     if (verbose > 1) {
10932       printf("  Splitting this triangle at its circumcenter:\n");
10933       printf("    (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", borg[0],
10934              borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
10935     }
10936     errorflag = 0;
10937     /* Create a new point at the triangle's circumcenter. */
10938     newpoint = (point) poolalloc(&points);
10939     shortedge = findcircumcenter(borg, bdest, bapex, newpoint, &xi, &eta);
10940     /* Check whether the new point lies on a triangle vertex. */
10941     if (((newpoint[0] == borg[0]) && (newpoint[1] == borg[1]))
10942         || ((newpoint[0] == bdest[0]) && (newpoint[1] == bdest[1]))
10943         || ((newpoint[0] == bapex[0]) && (newpoint[1] == bapex[1]))) {
10944       if (!quiet) {
10945         printf("Warning:  New point (%.12g, %.12g) falls on existing vertex.\n"
10946                , newpoint[0], newpoint[1]);
10947         errorflag = 1;
10948       }
10949       pointdealloc(newpoint);
10950     } else {
10951       for (i = 2; i < 2 + nextras; i++) {
10952         /* Interpolate the point attributes at the circumcenter. */
10953         newpoint[i] = borg[i] + xi * (bdest[i] - borg[i])
10954                              + eta * (bapex[i] - borg[i]);
10955       }
10956       /* The new point must be in the interior, and have a marker of zero. */
10957       setpointmark(newpoint, 0);
10958       /* Ensure that the handle `badtri->badfacetri' represents the shortest */
10959       /*   edge of the triangle.  This ensures that the circumcenter must    */
10960       /*   fall to the left of this edge, so point location will work.       */
10961       if (shortedge == OPPOSITEORG) {
10962         lnextself(badtri->badfacetri);
10963       } else if (shortedge == OPPOSITEDEST) {
10964         lprevself(badtri->badfacetri);
10965       }
10966       /* Insert the circumcenter, searching from the edge of the triangle, */
10967       /*   and maintain the Delaunay property of the triangulation.        */
10968       success = insertsite(newpoint, &(badtri->badfacetri),
10969                            (struct edge *) NULL, 1, 1);
10970       if (success == SUCCESSFULPOINT) {
10971         if (steinerleft > 0) {
10972           steinerleft--;
10973         }
10974       } else if (success == ENCROACHINGPOINT) {
10975         /* If the newly inserted point encroaches upon a segment, delete it. */
10976         deletesite(&(badtri->badfacetri));
10977       } else if (success == VIOLATINGPOINT) {
10978         /* Failed to insert the new point, but some segment was */
10979         /*   marked as being encroached.                        */
10980         pointdealloc(newpoint);
10981       } else {                                  /* success == DUPLICATEPOINT */
10982         /* Failed to insert the new point because a vertex is already there. */
10983         if (!quiet) {
10984           printf(
10985             "Warning:  New point (%.12g, %.12g) falls on existing vertex.\n"
10986                  , newpoint[0], newpoint[1]);
10987           errorflag = 1;
10988         }
10989         pointdealloc(newpoint);
10990       }
10991     }
10992     if (errorflag) {
10993       if (verbose) {
10994         printf("  The new point is at the circumcenter of triangle\n");
10995         printf("    (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10996                borg[0], borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
10997       }
10998       printf("This probably means that I am trying to refine triangles\n");
10999       printf("  to a smaller size than can be accommodated by the finite\n");
11000       printf("  precision of floating point arithmetic.  (You can be\n");
11001       printf("  sure of this if I fail to terminate.)\n");
11002       precisionerror();
11003     }
11004   }
11005   /* Return the bad triangle to the pool. */
11006   pooldealloc(&badtriangles, (VOID *) badtri);
11007 }
11008
11009 #endif /* not CDT_ONLY */
11010
11011 /*****************************************************************************/
11012 /*                                                                           */
11013 /*  enforcequality()   Remove all the encroached edges and bad triangles     */
11014 /*                     from the triangulation.                               */
11015 /*                                                                           */
11016 /*****************************************************************************/
11017
11018 #ifndef CDT_ONLY
11019
11020 void enforcequality()
11021 {
11022   int i;
11023
11024   if (!quiet) {
11025     printf("Adding Steiner points to enforce quality.\n");
11026   }
11027   /* Initialize the pool of encroached segments. */
11028   poolinit(&badsegments, sizeof(struct edge), BADSEGMENTPERBLOCK, POINTER, 0);
11029   if (verbose) {
11030     printf("  Looking for encroached segments.\n");
11031   }
11032   /* Test all segments to see if they're encroached. */
11033   tallyencs();
11034   if (verbose && (badsegments.items > 0)) {
11035     printf("  Splitting encroached segments.\n");
11036   }
11037   /* Note that steinerleft == -1 if an unlimited number */
11038   /*   of Steiner points is allowed.                    */
11039   while ((badsegments.items > 0) && (steinerleft != 0)) {
11040     /* Fix the segments without noting newly encroached segments or   */
11041     /*   bad triangles.  The reason we don't want to note newly       */
11042     /*   encroached segments is because some encroached segments are  */
11043     /*   likely to be noted multiple times, and would then be blindly */
11044     /*   split multiple times.  I should fix that some time.          */
11045     repairencs(0);
11046     /* Now, find all the segments that became encroached while adding */
11047     /*   points to split encroached segments.                         */
11048     tallyencs();
11049   }
11050   /* At this point, if we haven't run out of Steiner points, the */
11051   /*   triangulation should be (conforming) Delaunay.            */
11052
11053   /* Next, we worry about enforcing triangle quality. */
11054   if ((minangle > 0.0) || vararea || fixedarea) {
11055     /* Initialize the pool of bad triangles. */
11056     poolinit(&badtriangles, sizeof(struct badface), BADTRIPERBLOCK, POINTER,
11057              0);
11058     /* Initialize the queues of bad triangles. */
11059     for (i = 0; i < 64; i++) {
11060       queuefront[i] = (struct badface *) NULL;
11061       queuetail[i] = &queuefront[i];
11062     }
11063     /* Test all triangles to see if they're bad. */
11064     tallyfaces();
11065     if (verbose) {
11066       printf("  Splitting bad triangles.\n");
11067     }
11068     while ((badtriangles.items > 0) && (steinerleft != 0)) {
11069       /* Fix one bad triangle by inserting a point at its circumcenter. */
11070       splittriangle(dequeuebadtri());
11071       /* Fix any encroached segments that may have resulted.  Record */
11072       /*   any new bad triangles or encroached segments that result. */
11073       if (badsegments.items > 0) {
11074         repairencs(1);
11075       }
11076     }
11077   }
11078   /* At this point, if we haven't run out of Steiner points, the */
11079   /*   triangulation should be (conforming) Delaunay and have no */
11080   /*   low-quality triangles.                                    */
11081
11082   /* Might we have run out of Steiner points too soon? */
11083   if (!quiet && (badsegments.items > 0) && (steinerleft == 0)) {
11084     printf("\nWarning:  I ran out of Steiner points, but the mesh has\n");
11085     if (badsegments.items == 1) {
11086       printf("  an encroached segment, and therefore might not be truly\n");
11087     } else {
11088       printf("  %ld encroached segments, and therefore might not be truly\n",
11089              badsegments.items);
11090     }
11091     printf("  Delaunay.  If the Delaunay property is important to you,\n");
11092     printf("  try increasing the number of Steiner points (controlled by\n");
11093     printf("  the -S switch) slightly and try again.\n\n");
11094   }
11095 }
11096
11097 #endif /* not CDT_ONLY */
11098
11099 /**                                                                         **/
11100 /**                                                                         **/
11101 /********* Mesh quality maintenance ends here                        *********/
11102
11103 /*****************************************************************************/
11104 /*                                                                           */
11105 /*  highorder()   Create extra nodes for quadratic subparametric elements.   */
11106 /*                                                                           */
11107 /*****************************************************************************/
11108
11109 void highorder()
11110 {
11111   struct triedge triangleloop, trisym;
11112   struct edge checkmark;
11113   point newpoint;
11114   point torg, tdest;
11115   int i;
11116   triangle ptr;                         /* Temporary variable used by sym(). */
11117   shelle sptr;                      /* Temporary variable used by tspivot(). */
11118
11119   if (!quiet) {
11120     printf("Adding vertices for second-order triangles.\n");
11121   }
11122   /* The following line ensures that dead items in the pool of nodes    */
11123   /*   cannot be allocated for the extra nodes associated with high     */
11124   /*   order elements.  This ensures that the primary nodes (at the     */
11125   /*   corners of elements) will occur earlier in the output files, and */
11126   /*   have lower indices, than the extra nodes.                        */
11127   points.deaditemstack = (VOID *) NULL;
11128
11129   traversalinit(&triangles);
11130   triangleloop.tri = triangletraverse();
11131   /* To loop over the set of edges, loop over all triangles, and look at   */
11132   /*   the three edges of each triangle.  If there isn't another triangle  */
11133   /*   adjacent to the edge, operate on the edge.  If there is another     */
11134   /*   adjacent triangle, operate on the edge only if the current triangle */
11135   /*   has a smaller pointer than its neighbor.  This way, each edge is    */
11136   /*   considered only once.                                               */
11137   while (triangleloop.tri != (triangle *) NULL) {
11138     for (triangleloop.orient = 0; triangleloop.orient < 3;
11139          triangleloop.orient++) {
11140       sym(triangleloop, trisym);
11141       if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
11142         org(triangleloop, torg);
11143         dest(triangleloop, tdest);
11144         /* Create a new node in the middle of the edge.  Interpolate */
11145         /*   its attributes.                                         */
11146         newpoint = (point) poolalloc(&points);
11147         for (i = 0; i < 2 + nextras; i++) {
11148           newpoint[i] = 0.5 * (torg[i] + tdest[i]);
11149         }
11150         /* Set the new node's marker to zero or one, depending on */
11151         /*   whether it lies on a boundary.                       */
11152         setpointmark(newpoint, trisym.tri == dummytri);
11153         if (useshelles) {
11154           tspivot(triangleloop, checkmark);
11155           /* If this edge is a segment, transfer the marker to the new node. */
11156           if (checkmark.sh != dummysh) {
11157             setpointmark(newpoint, mark(checkmark));
11158           }
11159         }
11160         if (verbose > 1) {
11161           printf("  Creating (%.12g, %.12g).\n", newpoint[0], newpoint[1]);
11162         }
11163         /* Record the new node in the (one or two) adjacent elements. */
11164         triangleloop.tri[highorderindex + triangleloop.orient] =
11165                 (triangle) newpoint;
11166         if (trisym.tri != dummytri) {
11167           trisym.tri[highorderindex + trisym.orient] = (triangle) newpoint;
11168         }
11169       }
11170     }
11171     triangleloop.tri = triangletraverse();
11172   }
11173 }
11174
11175 /********* File I/O routines begin here                              *********/
11176 /**                                                                         **/
11177 /**                                                                         **/
11178
11179 /*****************************************************************************/
11180 /*                                                                           */
11181 /*  readline()   Read a nonempty line from a file.                           */
11182 /*                                                                           */
11183 /*  A line is considered "nonempty" if it contains something that looks like */
11184 /*  a number.                                                                */
11185 /*                                                                           */
11186 /*****************************************************************************/
11187
11188 #ifndef TRILIBRARY
11189
11190 char *readline(string, infile, infilename)
11191 char *string;
11192 FILE *infile;
11193 char *infilename;
11194 {
11195   char *result;
11196
11197   /* Search for something that looks like a number. */
11198   do {
11199     result = fgets(string, INPUTLINESIZE, infile);
11200     if (result == (char *) NULL) {
11201       printf("  Error:  Unexpected end of file in %s.\n", infilename);
11202       exit(1);
11203     }
11204     /* Skip anything that doesn't look like a number, a comment, */
11205     /*   or the end of a line.                                   */
11206     while ((*result != '\0') && (*result != '#')
11207            && (*result != '.') && (*result != '+') && (*result != '-')
11208            && ((*result < '0') || (*result > '9'))) {
11209       result++;
11210     }
11211   /* If it's a comment or end of line, read another line and try again. */
11212   } while ((*result == '#') || (*result == '\0'));
11213   return result;
11214 }
11215
11216 #endif /* not TRILIBRARY */
11217
11218 /*****************************************************************************/
11219 /*                                                                           */
11220 /*  findfield()   Find the next field of a string.                           */
11221 /*                                                                           */
11222 /*  Jumps past the current field by searching for whitespace, then jumps     */
11223 /*  past the whitespace to find the next field.                              */
11224 /*                                                                           */
11225 /*****************************************************************************/
11226
11227 #ifndef TRILIBRARY
11228
11229 char *findfield(string)
11230 char *string;
11231 {
11232   char *result;
11233
11234   result = string;
11235   /* Skip the current field.  Stop upon reaching whitespace. */
11236   while ((*result != '\0') && (*result != '#')
11237          && (*result != ' ') && (*result != '\t')) {
11238     result++;
11239   }
11240   /* Now skip the whitespace and anything else that doesn't look like a */
11241   /*   number, a comment, or the end of a line.                         */
11242   while ((*result != '\0') && (*result != '#')
11243          && (*result != '.') && (*result != '+') && (*result != '-')
11244          && ((*result < '0') || (*result > '9'))) {
11245     result++;
11246   }
11247   /* Check for a comment (prefixed with `#'). */
11248   if (*result == '#') {
11249     *result = '\0';
11250   }
11251   return result;
11252 }
11253
11254 #endif /* not TRILIBRARY */
11255
11256 /*****************************************************************************/
11257 /*                                                                           */
11258 /*  readnodes()   Read the points from a file, which may be a .node or .poly */
11259 /*                file.                                                      */
11260 /*                                                                           */
11261 /*****************************************************************************/
11262
11263 #ifndef TRILIBRARY
11264
11265 void readnodes(nodefilename, polyfilename, polyfile)
11266 char *nodefilename;
11267 char *polyfilename;
11268 FILE **polyfile;
11269 {
11270   FILE *infile;
11271   point pointloop;
11272   char inputline[INPUTLINESIZE];
11273   char *stringptr;
11274   char *infilename;
11275   REAL x, y;
11276   int firstnode;
11277   int nodemarkers;
11278   int currentmarker;
11279   int i, j;
11280
11281   if (poly) {
11282     /* Read the points from a .poly file. */
11283     if (!quiet) {
11284       printf("Opening %s.\n", polyfilename);
11285     }
11286     *polyfile = fopen(polyfilename, "r");
11287     if (*polyfile == (FILE *) NULL) {
11288       printf("  Error:  Cannot access file %s.\n", polyfilename);
11289       exit(1);
11290     }
11291     /* Read number of points, number of dimensions, number of point */
11292     /*   attributes, and number of boundary markers.                */
11293     stringptr = readline(inputline, *polyfile, polyfilename);
11294     inpoints = (int) strtol (stringptr, &stringptr, 0);
11295     stringptr = findfield(stringptr);
11296     if (*stringptr == '\0') {
11297       mesh_dim = 2;
11298     } else {
11299       mesh_dim = (int) strtol (stringptr, &stringptr, 0);
11300     }
11301     stringptr = findfield(stringptr);
11302     if (*stringptr == '\0') {
11303       nextras = 0;
11304     } else {
11305       nextras = (int) strtol (stringptr, &stringptr, 0);
11306     }
11307     stringptr = findfield(stringptr);
11308     if (*stringptr == '\0') {
11309       nodemarkers = 0;
11310     } else {
11311       nodemarkers = (int) strtol (stringptr, &stringptr, 0);
11312     }
11313     if (inpoints > 0) {
11314       infile = *polyfile;
11315       infilename = polyfilename;
11316       readnodefile = 0;
11317     } else {
11318       /* If the .poly file claims there are zero points, that means that */
11319       /*   the points should be read from a separate .node file.         */
11320       readnodefile = 1;
11321       infilename = innodefilename;
11322     }
11323   } else {
11324     readnodefile = 1;
11325     infilename = innodefilename;
11326     *polyfile = (FILE *) NULL;
11327   }
11328
11329   if (readnodefile) {
11330     /* Read the points from a .node file. */
11331     if (!quiet) {
11332       printf("Opening %s.\n", innodefilename);
11333     }
11334     infile = fopen(innodefilename, "r");
11335     if (infile == (FILE *) NULL) {
11336       printf("  Error:  Cannot access file %s.\n", innodefilename);
11337       exit(1);
11338     }
11339     /* Read number of points, number of dimensions, number of point */
11340     /*   attributes, and number of boundary markers.                */
11341     stringptr = readline(inputline, infile, innodefilename);
11342     inpoints = (int) strtol (stringptr, &stringptr, 0);
11343     stringptr = findfield(stringptr);
11344     if (*stringptr == '\0') {
11345       mesh_dim = 2;
11346     } else {
11347       mesh_dim = (int) strtol (stringptr, &stringptr, 0);
11348     }
11349     stringptr = findfield(stringptr);
11350     if (*stringptr == '\0') {
11351       nextras = 0;
11352     } else {
11353       nextras = (int) strtol (stringptr, &stringptr, 0);
11354     }
11355     stringptr = findfield(stringptr);
11356     if (*stringptr == '\0') {
11357       nodemarkers = 0;
11358     } else {
11359       nodemarkers = (int) strtol (stringptr, &stringptr, 0);
11360     }
11361   }
11362
11363   if (inpoints < 3) {
11364     printf("Error:  Input must have at least three input points.\n");
11365     exit(1);
11366   }
11367   if (mesh_dim != 2) {
11368     printf("Error:  Triangle only works with two-dimensional meshes.\n");
11369     exit(1);
11370   }
11371
11372   initializepointpool();
11373
11374   /* Read the points. */
11375   for (i = 0; i < inpoints; i++) {
11376     pointloop = (point) poolalloc(&points);
11377     stringptr = readline(inputline, infile, infilename);
11378     if (i == 0) {
11379       firstnode = (int) strtol (stringptr, &stringptr, 0);
11380       if ((firstnode == 0) || (firstnode == 1)) {
11381         firstnumber = firstnode;
11382       }
11383     }
11384     stringptr = findfield(stringptr);
11385     if (*stringptr == '\0') {
11386       printf("Error:  Point %d has no x coordinate.\n", firstnumber + i);
11387       exit(1);
11388     }
11389     x = (REAL) strtod(stringptr, &stringptr);
11390     stringptr = findfield(stringptr);
11391     if (*stringptr == '\0') {
11392       printf("Error:  Point %d has no y coordinate.\n", firstnumber + i);
11393       exit(1);
11394     }
11395     y = (REAL) strtod(stringptr, &stringptr);
11396     pointloop[0] = x;
11397     pointloop[1] = y;
11398     /* Read the point attributes. */
11399     for (j = 2; j < 2 + nextras; j++) {
11400       stringptr = findfield(stringptr);
11401       if (*stringptr == '\0') {
11402         pointloop[j] = 0.0;
11403       } else {
11404         pointloop[j] = (REAL) strtod(stringptr, &stringptr);
11405       }
11406     }
11407     if (nodemarkers) {
11408       /* Read a point marker. */
11409       stringptr = findfield(stringptr);
11410       if (*stringptr == '\0') {
11411         setpointmark(pointloop, 0);
11412       } else {
11413         currentmarker = (int) strtol (stringptr, &stringptr, 0);
11414         setpointmark(pointloop, currentmarker);
11415       }
11416     } else {
11417       /* If no markers are specified in the file, they default to zero. */
11418       setpointmark(pointloop, 0);
11419     }
11420     /* Determine the smallest and largest x and y coordinates. */
11421     if (i == 0) {
11422       xmin = xmax = x;
11423       ymin = ymax = y;
11424     } else {
11425       xmin = (x < xmin) ? x : xmin;
11426       xmax = (x > xmax) ? x : xmax;
11427       ymin = (y < ymin) ? y : ymin;
11428       ymax = (y > ymax) ? y : ymax;
11429     }
11430   }
11431   if (readnodefile) {
11432     fclose(infile);
11433   }
11434
11435   /* Nonexistent x value used as a flag to mark circle events in sweepline */
11436   /*   Delaunay algorithm.                                                 */
11437   xminextreme = 10 * xmin - 9 * xmax;
11438 }
11439
11440 #endif /* not TRILIBRARY */
11441
11442 /*****************************************************************************/
11443 /*                                                                           */
11444 /*  transfernodes()   Read the points from memory.                           */
11445 /*                                                                           */
11446 /*****************************************************************************/
11447
11448 #ifdef TRILIBRARY
11449
11450 void transfernodes(pointlist, pointattriblist, pointmarkerlist, numberofpoints,
11451                    numberofpointattribs)
11452 REAL *pointlist;
11453 REAL *pointattriblist;
11454 int *pointmarkerlist;
11455 int numberofpoints;
11456 int numberofpointattribs;
11457 {
11458   point pointloop;
11459   REAL x, y;
11460   int i, j;
11461   int coordindex;
11462   int attribindex;
11463
11464   inpoints = numberofpoints;
11465   mesh_dim = 2;
11466   nextras = numberofpointattribs;
11467   readnodefile = 0;
11468   if (inpoints < 3) {
11469     printf("Error:  Input must have at least three input points.\n");
11470     exit(1);
11471   }
11472
11473   initializepointpool();
11474
11475   /* Read the points. */
11476   coordindex = 0;
11477   attribindex = 0;
11478   for (i = 0; i < inpoints; i++) {
11479     pointloop = (point) poolalloc(&points);
11480     /* Read the point coordinates. */
11481     x = pointloop[0] = pointlist[coordindex++];
11482     y = pointloop[1] = pointlist[coordindex++];
11483     /* Read the point attributes. */
11484     for (j = 0; j < numberofpointattribs; j++) {
11485       pointloop[2 + j] = pointattriblist[attribindex++];
11486     }
11487     if (pointmarkerlist != (int *) NULL) {
11488       /* Read a point marker. */
11489       setpointmark(pointloop, pointmarkerlist[i]);
11490     } else {
11491       /* If no markers are specified, they default to zero. */
11492       setpointmark(pointloop, 0);
11493     }
11494     x = pointloop[0];
11495     y = pointloop[1];
11496     /* Determine the smallest and largest x and y coordinates. */
11497     if (i == 0) {
11498       xmin = xmax = x;
11499       ymin = ymax = y;
11500     } else {
11501       xmin = (x < xmin) ? x : xmin;
11502       xmax = (x > xmax) ? x : xmax;
11503       ymin = (y < ymin) ? y : ymin;
11504       ymax = (y > ymax) ? y : ymax;
11505     }
11506   }
11507
11508   /* Nonexistent x value used as a flag to mark circle events in sweepline */
11509   /*   Delaunay algorithm.                                                 */
11510   xminextreme = 10 * xmin - 9 * xmax;
11511 }
11512
11513 #endif /* TRILIBRARY */
11514
11515 /*****************************************************************************/
11516 /*                                                                           */
11517 /*  readholes()   Read the holes, and possibly regional attributes and area  */
11518 /*                constraints, from a .poly file.                            */
11519 /*                                                                           */
11520 /*****************************************************************************/
11521
11522 #ifndef TRILIBRARY
11523
11524 void readholes(polyfile, polyfilename, hlist, holes, rlist, regions)
11525 FILE *polyfile;
11526 char *polyfilename;
11527 REAL **hlist;
11528 int *holes;
11529 REAL **rlist;
11530 int *regions;
11531 {
11532   REAL *holelist;
11533   REAL *regionlist;
11534   char inputline[INPUTLINESIZE];
11535   char *stringptr;
11536   int index;
11537   int i;
11538
11539   /* Read the holes. */
11540   stringptr = readline(inputline, polyfile, polyfilename);
11541   *holes = (int) strtol (stringptr, &stringptr, 0);
11542   if (*holes > 0) {
11543     holelist = (REAL *) malloc(2 * *holes * sizeof(REAL));
11544     *hlist = holelist;
11545     if (holelist == (REAL *) NULL) {
11546       printf("Error:  Out of memory.\n");
11547       exit(1);
11548     }
11549     for (i = 0; i < 2 * *holes; i += 2) {
11550       stringptr = readline(inputline, polyfile, polyfilename);
11551       stringptr = findfield(stringptr);
11552       if (*stringptr == '\0') {
11553         printf("Error:  Hole %d has no x coordinate.\n",
11554                firstnumber + (i >> 1));
11555         exit(1);
11556       } else {
11557         holelist[i] = (REAL) strtod(stringptr, &stringptr);
11558       }
11559       stringptr = findfield(stringptr);
11560       if (*stringptr == '\0') {
11561         printf("Error:  Hole %d has no y coordinate.\n",
11562                firstnumber + (i >> 1));
11563         exit(1);
11564       } else {
11565         holelist[i + 1] = (REAL) strtod(stringptr, &stringptr);
11566       }
11567     }
11568   } else {
11569     *hlist = (REAL *) NULL;
11570   }
11571
11572 #ifndef CDT_ONLY
11573   if ((regionattrib || vararea) && !refine) {
11574     /* Read the area constraints. */
11575     stringptr = readline(inputline, polyfile, polyfilename);
11576     *regions = (int) strtol (stringptr, &stringptr, 0);
11577     if (*regions > 0) {
11578       regionlist = (REAL *) malloc(4 * *regions * sizeof(REAL));
11579       *rlist = regionlist;
11580       if (regionlist == (REAL *) NULL) {
11581         printf("Error:  Out of memory.\n");
11582         exit(1);
11583       }
11584       index = 0;
11585       for (i = 0; i < *regions; i++) {
11586         stringptr = readline(inputline, polyfile, polyfilename);
11587         stringptr = findfield(stringptr);
11588         if (*stringptr == '\0') {
11589           printf("Error:  Region %d has no x coordinate.\n",
11590                  firstnumber + i);
11591           exit(1);
11592         } else {
11593           regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11594         }
11595         stringptr = findfield(stringptr);
11596         if (*stringptr == '\0') {
11597           printf("Error:  Region %d has no y coordinate.\n",
11598                  firstnumber + i);
11599           exit(1);
11600         } else {
11601           regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11602         }
11603         stringptr = findfield(stringptr);
11604         if (*stringptr == '\0') {
11605           printf(
11606             "Error:  Region %d has no region attribute or area constraint.\n",
11607                  firstnumber + i);
11608           exit(1);
11609         } else {
11610           regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11611         }
11612         stringptr = findfield(stringptr);
11613         if (*stringptr == '\0') {
11614           regionlist[index] = regionlist[index - 1];
11615         } else {
11616           regionlist[index] = (REAL) strtod(stringptr, &stringptr);
11617         }
11618         index++;
11619       }
11620     }
11621   } else {
11622     /* Set `*regions' to zero to avoid an accidental free() later. */
11623     *regions = 0;
11624     *rlist = (REAL *) NULL;
11625   }
11626 #endif /* not CDT_ONLY */
11627
11628   fclose(polyfile);
11629 }
11630
11631 #endif /* not TRILIBRARY */
11632
11633 /*****************************************************************************/
11634 /*                                                                           */
11635 /*  finishfile()   Write the command line to the output file so the user     */
11636 /*                 can remember how the file was generated.  Close the file. */
11637 /*                                                                           */
11638 /*****************************************************************************/
11639
11640 #ifndef TRILIBRARY
11641
11642 void finishfile(outfile, argc, argv)
11643 FILE *outfile;
11644 int argc;
11645 char **argv;
11646 {
11647   int i;
11648
11649   fprintf(outfile, "# Generated by");
11650   for (i = 0; i < argc; i++) {
11651     fprintf(outfile, " ");
11652     fputs(argv[i], outfile);
11653   }
11654   fprintf(outfile, "\n");
11655   fclose(outfile);
11656 }
11657
11658 #endif /* not TRILIBRARY */
11659
11660 /*****************************************************************************/
11661 /*                                                                           */
11662 /*  writenodes()   Number the points and write them to a .node file.         */
11663 /*                                                                           */
11664 /*  To save memory, the point numbers are written over the shell markers     */
11665 /*  after the points are written to a file.                                  */
11666 /*                                                                           */
11667 /*****************************************************************************/
11668
11669 #ifdef TRILIBRARY
11670
11671 void writenodes(pointlist, pointattriblist, pointmarkerlist)
11672 REAL **pointlist;
11673 REAL **pointattriblist;
11674 int **pointmarkerlist;
11675
11676 #else /* not TRILIBRARY */
11677
11678 void writenodes(nodefilename, argc, argv)
11679 char *nodefilename;
11680 int argc;
11681 char **argv;
11682
11683 #endif /* not TRILIBRARY */
11684
11685 {
11686 #ifdef TRILIBRARY
11687   REAL *plist;
11688   REAL *palist;
11689   int *pmlist;
11690   int coordindex;
11691   int attribindex;
11692 #else /* not TRILIBRARY */
11693   FILE *outfile;
11694 #endif /* not TRILIBRARY */
11695   point pointloop;
11696   int pointnumber;
11697   int i;
11698
11699 #ifdef TRILIBRARY
11700   if (!quiet) {
11701     printf("Writing points.\n");
11702   }
11703   /* Allocate memory for output points if necessary. */
11704   if (*pointlist == (REAL *) NULL) {
11705     *pointlist = (REAL *) malloc(points.items * 2 * sizeof(REAL));
11706     if (*pointlist == (REAL *) NULL) {
11707       printf("Error:  Out of memory.\n");
11708       exit(1);
11709     }
11710   }
11711   /* Allocate memory for output point attributes if necessary. */
11712   if ((nextras > 0) && (*pointattriblist == (REAL *) NULL)) {
11713     *pointattriblist = (REAL *) malloc(points.items * nextras * sizeof(REAL));
11714     if (*pointattriblist == (REAL *) NULL) {
11715       printf("Error:  Out of memory.\n");
11716       exit(1);
11717     }
11718   }
11719   /* Allocate memory for output point markers if necessary. */
11720   if (!nobound && (*pointmarkerlist == (int *) NULL)) {
11721     *pointmarkerlist = (int *) malloc(points.items * sizeof(int));
11722     if (*pointmarkerlist == (int *) NULL) {
11723       printf("Error:  Out of memory.\n");
11724       exit(1);
11725     }
11726   }
11727   plist = *pointlist;
11728   palist = *pointattriblist;
11729   pmlist = *pointmarkerlist;
11730   coordindex = 0;
11731   attribindex = 0;
11732 #else /* not TRILIBRARY */
11733   if (!quiet) {
11734     printf("Writing %s.\n", nodefilename);
11735   }
11736   outfile = fopen(nodefilename, "w");
11737   if (outfile == (FILE *) NULL) {
11738     printf("  Error:  Cannot create file %s.\n", nodefilename);
11739     exit(1);
11740   }
11741   /* Number of points, number of dimensions, number of point attributes, */
11742   /*   and number of boundary markers (zero or one).                     */
11743   fprintf(outfile, "%ld  %d  %d  %d\n", points.items, mesh_dim, nextras,
11744           1 - nobound);
11745 #endif /* not TRILIBRARY */
11746
11747   traversalinit(&points);
11748   pointloop = pointtraverse();
11749   pointnumber = firstnumber;
11750   while (pointloop != (point) NULL) {
11751 #ifdef TRILIBRARY
11752     /* X and y coordinates. */
11753     plist[coordindex++] = pointloop[0];
11754     plist[coordindex++] = pointloop[1];
11755     /* Point attributes. */
11756     for (i = 0; i < nextras; i++) {
11757       palist[attribindex++] = pointloop[2 + i];
11758     }
11759     if (!nobound) {
11760       /* Copy the boundary marker. */
11761       pmlist[pointnumber - firstnumber] = pointmark(pointloop);
11762     }
11763 #else /* not TRILIBRARY */
11764     /* Point number, x and y coordinates. */
11765     fprintf(outfile, "%4d    %.17g  %.17g", pointnumber, pointloop[0],
11766             pointloop[1]);
11767     for (i = 0; i < nextras; i++) {
11768       /* Write an attribute. */
11769       fprintf(outfile, "  %.17g", pointloop[i + 2]);
11770     }
11771     if (nobound) {
11772       fprintf(outfile, "\n");
11773     } else {
11774       /* Write the boundary marker. */
11775       fprintf(outfile, "    %d\n", pointmark(pointloop));
11776     }
11777 #endif /* not TRILIBRARY */
11778
11779     setpointmark(pointloop, pointnumber);
11780     pointloop = pointtraverse();
11781     pointnumber++;
11782   }
11783
11784 #ifndef TRILIBRARY
11785   finishfile(outfile, argc, argv);
11786 #endif /* not TRILIBRARY */
11787 }
11788
11789 /*****************************************************************************/
11790 /*                                                                           */
11791 /*  numbernodes()   Number the points.                                       */
11792 /*                                                                           */
11793 /*  Each point is assigned a marker equal to its number.                     */
11794 /*                                                                           */
11795 /*  Used when writenodes() is not called because no .node file is written.   */
11796 /*                                                                           */
11797 /*****************************************************************************/
11798
11799 void numbernodes()
11800 {
11801   point pointloop;
11802   int pointnumber;
11803
11804   traversalinit(&points);
11805   pointloop = pointtraverse();
11806   pointnumber = firstnumber;
11807   while (pointloop != (point) NULL) {
11808     setpointmark(pointloop, pointnumber);
11809     pointloop = pointtraverse();
11810     pointnumber++;
11811   }
11812 }
11813
11814 /*****************************************************************************/
11815 /*                                                                           */
11816 /*  writeelements()   Write the triangles to an .ele file.                   */
11817 /*                                                                           */
11818 /*****************************************************************************/
11819
11820 #ifdef TRILIBRARY
11821
11822 void writeelements(trianglelist, triangleattriblist)
11823 int **trianglelist;
11824 REAL **triangleattriblist;
11825
11826 #else /* not TRILIBRARY */
11827
11828 void writeelements(elefilename, argc, argv)
11829 char *elefilename;
11830 int argc;
11831 char **argv;
11832
11833 #endif /* not TRILIBRARY */
11834
11835 {
11836 #ifdef TRILIBRARY
11837   int *tlist;
11838   REAL *talist;
11839   int pointindex;
11840   int attribindex;
11841 #else /* not TRILIBRARY */
11842   FILE *outfile;
11843 #endif /* not TRILIBRARY */
11844   struct triedge triangleloop;
11845   point p1, p2, p3;
11846   point mid1, mid2, mid3;
11847   int elementnumber;
11848   int i;
11849
11850 #ifdef TRILIBRARY
11851   if (!quiet) {
11852     printf("Writing triangles.\n");
11853   }
11854   /* Allocate memory for output triangles if necessary. */
11855   if (*trianglelist == (int *) NULL) {
11856     *trianglelist = (int *) malloc(triangles.items *
11857                                ((order + 1) * (order + 2) / 2) * sizeof(int));
11858     if (*trianglelist == (int *) NULL) {
11859       printf("Error:  Out of memory.\n");
11860       exit(1);
11861     }
11862   }
11863   /* Allocate memory for output triangle attributes if necessary. */
11864   if ((eextras > 0) && (*triangleattriblist == (REAL *) NULL)) {
11865     *triangleattriblist = (REAL *) malloc(triangles.items * eextras *
11866                                           sizeof(REAL));
11867     if (*triangleattriblist == (REAL *) NULL) {
11868       printf("Error:  Out of memory.\n");
11869       exit(1);
11870     }
11871   }
11872   tlist = *trianglelist;
11873   talist = *triangleattriblist;
11874   pointindex = 0;
11875   attribindex = 0;
11876 #else /* not TRILIBRARY */
11877   if (!quiet) {
11878     printf("Writing %s.\n", elefilename);
11879   }
11880   outfile = fopen(elefilename, "w");
11881   if (outfile == (FILE *) NULL) {
11882     printf("  Error:  Cannot create file %s.\n", elefilename);
11883     exit(1);
11884   }
11885   /* Number of triangles, points per triangle, attributes per triangle. */
11886   fprintf(outfile, "%ld  %d  %d\n", triangles.items,
11887           (order + 1) * (order + 2) / 2, eextras);
11888 #endif /* not TRILIBRARY */
11889
11890   traversalinit(&triangles);
11891   triangleloop.tri = triangletraverse();
11892   triangleloop.orient = 0;
11893   elementnumber = firstnumber;
11894   while (triangleloop.tri != (triangle *) NULL) {
11895     org(triangleloop, p1);
11896     dest(triangleloop, p2);
11897     apex(triangleloop, p3);
11898     if (order == 1) {
11899 #ifdef TRILIBRARY
11900       tlist[pointindex++] = pointmark(p1);
11901       tlist[pointindex++] = pointmark(p2);
11902       tlist[pointindex++] = pointmark(p3);
11903 #else /* not TRILIBRARY */
11904       /* Triangle number, indices for three points. */
11905       fprintf(outfile, "%4d    %4d  %4d  %4d", elementnumber,
11906               pointmark(p1), pointmark(p2), pointmark(p3));
11907 #endif /* not TRILIBRARY */
11908     } else {
11909       mid1 = (point) triangleloop.tri[highorderindex + 1];
11910       mid2 = (point) triangleloop.tri[highorderindex + 2];
11911       mid3 = (point) triangleloop.tri[highorderindex];
11912 #ifdef TRILIBRARY
11913       tlist[pointindex++] = pointmark(p1);
11914       tlist[pointindex++] = pointmark(p2);
11915       tlist[pointindex++] = pointmark(p3);
11916       tlist[pointindex++] = pointmark(mid1);
11917       tlist[pointindex++] = pointmark(mid2);
11918       tlist[pointindex++] = pointmark(mid3);
11919 #else /* not TRILIBRARY */
11920       /* Triangle number, indices for six points. */
11921       fprintf(outfile, "%4d    %4d  %4d  %4d  %4d  %4d  %4d", elementnumber,
11922               pointmark(p1), pointmark(p2), pointmark(p3), pointmark(mid1),
11923               pointmark(mid2), pointmark(mid3));
11924 #endif /* not TRILIBRARY */
11925     }
11926
11927 #ifdef TRILIBRARY
11928     for (i = 0; i < eextras; i++) {
11929       talist[attribindex++] = elemattribute(triangleloop, i);
11930     }
11931 #else /* not TRILIBRARY */
11932     for (i = 0; i < eextras; i++) {
11933       fprintf(outfile, "  %.17g", elemattribute(triangleloop, i));
11934     }
11935     fprintf(outfile, "\n");
11936 #endif /* not TRILIBRARY */
11937
11938     triangleloop.tri = triangletraverse();
11939     elementnumber++;
11940   }
11941
11942 #ifndef TRILIBRARY
11943   finishfile(outfile, argc, argv);
11944 #endif /* not TRILIBRARY */
11945 }
11946
11947 /*****************************************************************************/
11948 /*                                                                           */
11949 /*  writepoly()   Write the segments and holes to a .poly file.              */
11950 /*                                                                           */
11951 /*****************************************************************************/
11952
11953 #ifdef TRILIBRARY
11954
11955 void writepoly(segmentlist, segmentmarkerlist)
11956 int **segmentlist;
11957 int **segmentmarkerlist;
11958
11959 #else /* not TRILIBRARY */
11960
11961 void writepoly(polyfilename, holelist, holes, regionlist, regions, argc, argv)
11962 char *polyfilename;
11963 REAL *holelist;
11964 int holes;
11965 REAL *regionlist;
11966 int regions;
11967 int argc;
11968 char **argv;
11969
11970 #endif /* not TRILIBRARY */
11971
11972 {
11973 #ifdef TRILIBRARY
11974   int *slist;
11975   int *smlist;
11976   int index;
11977 #else /* not TRILIBRARY */
11978   FILE *outfile;
11979   int i;
11980 #endif /* not TRILIBRARY */
11981   struct edge shelleloop;
11982   point endpoint1, endpoint2;
11983   int shellenumber;
11984
11985 #ifdef TRILIBRARY
11986   if (!quiet) {
11987     printf("Writing segments.\n");
11988   }
11989   /* Allocate memory for output segments if necessary. */
11990   if (*segmentlist == (int *) NULL) {
11991     *segmentlist = (int *) malloc(shelles.items * 2 * sizeof(int));
11992     if (*segmentlist == (int *) NULL) {
11993       printf("Error:  Out of memory.\n");
11994       exit(1);
11995     }
11996   }
11997   /* Allocate memory for output segment markers if necessary. */
11998   if (!nobound && (*segmentmarkerlist == (int *) NULL)) {
11999     *segmentmarkerlist = (int *) malloc(shelles.items * sizeof(int));
12000     if (*segmentmarkerlist == (int *) NULL) {
12001       printf("Error:  Out of memory.\n");
12002       exit(1);
12003     }
12004   }
12005   slist = *segmentlist;
12006   smlist = *segmentmarkerlist;
12007   index = 0;
12008 #else /* not TRILIBRARY */
12009   if (!quiet) {
12010     printf("Writing %s.\n", polyfilename);
12011   }
12012   outfile = fopen(polyfilename, "w");
12013   if (outfile == (FILE *) NULL) {
12014     printf("  Error:  Cannot create file %s.\n", polyfilename);
12015     exit(1);
12016   }
12017   /* The zero indicates that the points are in a separate .node file. */
12018   /*   Followed by number of dimensions, number of point attributes,  */
12019   /*   and number of boundary markers (zero or one).                  */
12020   fprintf(outfile, "%d  %d  %d  %d\n", 0, mesh_dim, nextras, 1 - nobound);
12021   /* Number of segments, number of boundary markers (zero or one). */
12022   fprintf(outfile, "%ld  %d\n", shelles.items, 1 - nobound);
12023 #endif /* not TRILIBRARY */
12024
12025   traversalinit(&shelles);
12026   shelleloop.sh = shelletraverse();
12027   shelleloop.shorient = 0;
12028   shellenumber = firstnumber;
12029   while (shelleloop.sh != (shelle *) NULL) {
12030     sorg(shelleloop, endpoint1);
12031     sdest(shelleloop, endpoint2);
12032 #ifdef TRILIBRARY
12033     /* Copy indices of the segment's two endpoints. */
12034     slist[index++] = pointmark(endpoint1);
12035     slist[index++] = pointmark(endpoint2);
12036     if (!nobound) {
12037       /* Copy the boundary marker. */
12038       smlist[shellenumber - firstnumber] = mark(shelleloop);
12039     }
12040 #else /* not TRILIBRARY */
12041     /* Segment number, indices of its two endpoints, and possibly a marker. */
12042     if (nobound) {
12043       fprintf(outfile, "%4d    %4d  %4d\n", shellenumber,
12044               pointmark(endpoint1), pointmark(endpoint2));
12045     } else {
12046       fprintf(outfile, "%4d    %4d  %4d    %4d\n", shellenumber,
12047               pointmark(endpoint1), pointmark(endpoint2), mark(shelleloop));
12048     }
12049 #endif /* not TRILIBRARY */
12050
12051     shelleloop.sh = shelletraverse();
12052     shellenumber++;
12053   }
12054
12055 #ifndef TRILIBRARY
12056 #ifndef CDT_ONLY
12057   fprintf(outfile, "%d\n", holes);
12058   if (holes > 0) {
12059     for (i = 0; i < holes; i++) {
12060       /* Hole number, x and y coordinates. */
12061       fprintf(outfile, "%4d   %.17g  %.17g\n", firstnumber + i,
12062               holelist[2 * i], holelist[2 * i + 1]);
12063     }
12064   }
12065   if (regions > 0) {
12066     fprintf(outfile, "%d\n", regions);
12067     for (i = 0; i < regions; i++) {
12068       /* Region number, x and y coordinates, attribute, maximum area. */
12069       fprintf(outfile, "%4d   %.17g  %.17g  %.17g  %.17g\n", firstnumber + i,
12070               regionlist[4 * i], regionlist[4 * i + 1],
12071               regionlist[4 * i + 2], regionlist[4 * i + 3]);
12072     }
12073   }
12074 #endif /* not CDT_ONLY */
12075
12076   finishfile(outfile, argc, argv);
12077 #endif /* not TRILIBRARY */
12078 }
12079
12080 /*****************************************************************************/
12081 /*                                                                           */
12082 /*  writeedges()   Write the edges to a .edge file.                          */
12083 /*                                                                           */
12084 /*****************************************************************************/
12085
12086 #ifdef TRILIBRARY
12087
12088 void writeedges(edgelist, edgemarkerlist)
12089 int **edgelist;
12090 int **edgemarkerlist;
12091
12092 #else /* not TRILIBRARY */
12093
12094 void writeedges(edgefilename, argc, argv)
12095 char *edgefilename;
12096 int argc;
12097 char **argv;
12098
12099 #endif /* not TRILIBRARY */
12100
12101 {
12102 #ifdef TRILIBRARY
12103   int *elist;
12104   int *emlist;
12105   int index;
12106 #else /* not TRILIBRARY */
12107   FILE *outfile;
12108 #endif /* not TRILIBRARY */
12109   struct triedge triangleloop, trisym;
12110   struct edge checkmark;
12111   point p1, p2;
12112   int edgenumber;
12113   triangle ptr;                         /* Temporary variable used by sym(). */
12114   shelle sptr;                      /* Temporary variable used by tspivot(). */
12115
12116 #ifdef TRILIBRARY
12117   if (!quiet) {
12118     printf("Writing edges.\n");
12119   }
12120   /* Allocate memory for edges if necessary. */
12121   if (*edgelist == (int *) NULL) {
12122     *edgelist = (int *) malloc(edges * 2 * sizeof(int));
12123     if (*edgelist == (int *) NULL) {
12124       printf("Error:  Out of memory.\n");
12125       exit(1);
12126     }
12127   }
12128   /* Allocate memory for edge markers if necessary. */
12129   if (!nobound && (*edgemarkerlist == (int *) NULL)) {
12130     *edgemarkerlist = (int *) malloc(edges * sizeof(int));
12131     if (*edgemarkerlist == (int *) NULL) {
12132       printf("Error:  Out of memory.\n");
12133       exit(1);
12134     }
12135   }
12136   elist = *edgelist;
12137   emlist = *edgemarkerlist;
12138   index = 0;
12139 #else /* not TRILIBRARY */
12140   if (!quiet) {
12141     printf("Writing %s.\n", edgefilename);
12142   }
12143   outfile = fopen(edgefilename, "w");
12144   if (outfile == (FILE *) NULL) {
12145     printf("  Error:  Cannot create file %s.\n", edgefilename);
12146     exit(1);
12147   }
12148   /* Number of edges, number of boundary markers (zero or one). */
12149   fprintf(outfile, "%ld  %d\n", edges, 1 - nobound);
12150 #endif /* not TRILIBRARY */
12151
12152   traversalinit(&triangles);
12153   triangleloop.tri = triangletraverse();
12154   edgenumber = firstnumber;
12155   /* To loop over the set of edges, loop over all triangles, and look at   */
12156   /*   the three edges of each triangle.  If there isn't another triangle  */
12157   /*   adjacent to the edge, operate on the edge.  If there is another     */
12158   /*   adjacent triangle, operate on the edge only if the current triangle */
12159   /*   has a smaller pointer than its neighbor.  This way, each edge is    */
12160   /*   considered only once.                                               */
12161   while (triangleloop.tri != (triangle *) NULL) {
12162     for (triangleloop.orient = 0; triangleloop.orient < 3;
12163          triangleloop.orient++) {
12164       sym(triangleloop, trisym);
12165       if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
12166         org(triangleloop, p1);
12167         dest(triangleloop, p2);
12168 #ifdef TRILIBRARY
12169         elist[index++] = pointmark(p1);
12170         elist[index++] = pointmark(p2);
12171 #endif /* TRILIBRARY */
12172         if (nobound) {
12173 #ifndef TRILIBRARY
12174           /* Edge number, indices of two endpoints. */
12175           fprintf(outfile, "%4d   %d  %d\n", edgenumber,
12176                   pointmark(p1), pointmark(p2));
12177 #endif /* not TRILIBRARY */
12178         } else {
12179           /* Edge number, indices of two endpoints, and a boundary marker. */
12180           /*   If there's no shell edge, the boundary marker is zero.      */
12181           if (useshelles) {
12182             tspivot(triangleloop, checkmark);
12183             if (checkmark.sh == dummysh) {
12184 #ifdef TRILIBRARY
12185               emlist[edgenumber - firstnumber] = 0;
12186 #else /* not TRILIBRARY */
12187               fprintf(outfile, "%4d   %d  %d  %d\n", edgenumber,
12188                       pointmark(p1), pointmark(p2), 0);
12189 #endif /* not TRILIBRARY */
12190             } else {
12191 #ifdef TRILIBRARY
12192               emlist[edgenumber - firstnumber] = mark(checkmark);
12193 #else /* not TRILIBRARY */
12194               fprintf(outfile, "%4d   %d  %d  %d\n", edgenumber,
12195                       pointmark(p1), pointmark(p2), mark(checkmark));
12196 #endif /* not TRILIBRARY */
12197             }
12198           } else {
12199 #ifdef TRILIBRARY
12200             emlist[edgenumber - firstnumber] = trisym.tri == dummytri;
12201 #else /* not TRILIBRARY */
12202             fprintf(outfile, "%4d   %d  %d  %d\n", edgenumber,
12203                     pointmark(p1), pointmark(p2), trisym.tri == dummytri);
12204 #endif /* not TRILIBRARY */
12205           }
12206         }
12207         edgenumber++;
12208       }
12209     }
12210     triangleloop.tri = triangletraverse();
12211   }
12212
12213 #ifndef TRILIBRARY
12214   finishfile(outfile, argc, argv);
12215 #endif /* not TRILIBRARY */
12216 }
12217
12218 /*****************************************************************************/
12219 /*                                                                           */
12220 /*  writevoronoi()   Write the Voronoi diagram to a .v.node and .v.edge      */
12221 /*                   file.                                                   */
12222 /*                                                                           */
12223 /*  The Voronoi diagram is the geometric dual of the Delaunay triangulation. */
12224 /*  Hence, the Voronoi vertices are listed by traversing the Delaunay        */
12225 /*  triangles, and the Voronoi edges are listed by traversing the Delaunay   */
12226 /*  edges.                                                                   */
12227 /*                                                                           */
12228 /*  WARNING:  In order to assign numbers to the Voronoi vertices, this       */
12229 /*  procedure messes up the shell edges or the extra nodes of every          */
12230 /*  element.  Hence, you should call this procedure last.                    */
12231 /*                                                                           */
12232 /*****************************************************************************/
12233
12234 #ifdef TRILIBRARY
12235
12236 void writevoronoi(vpointlist, vpointattriblist, vpointmarkerlist, vedgelist,
12237                   vedgemarkerlist, vnormlist)
12238 REAL **vpointlist;
12239 REAL **vpointattriblist;
12240 int **vpointmarkerlist;
12241 int **vedgelist;
12242 int **vedgemarkerlist;
12243 REAL **vnormlist;
12244
12245 #else /* not TRILIBRARY */
12246
12247 void writevoronoi(vnodefilename, vedgefilename, argc, argv)
12248 char *vnodefilename;
12249 char *vedgefilename;
12250 int argc;
12251 char **argv;
12252
12253 #endif /* not TRILIBRARY */
12254
12255 {
12256 #ifdef TRILIBRARY
12257   REAL *plist;
12258   REAL *palist;
12259   int *elist;
12260   REAL *normlist;
12261   int coordindex;
12262   int attribindex;
12263 #else /* not TRILIBRARY */
12264   FILE *outfile;
12265 #endif /* not TRILIBRARY */
12266   struct triedge triangleloop, trisym;
12267   point torg, tdest, tapex;
12268   REAL circumcenter[2];
12269   REAL xi, eta;
12270   int vnodenumber, vedgenumber;
12271   int p1, p2;
12272   int i;
12273   triangle ptr;                         /* Temporary variable used by sym(). */
12274
12275 #ifdef TRILIBRARY
12276   if (!quiet) {
12277     printf("Writing Voronoi vertices.\n");
12278   }
12279   /* Allocate memory for Voronoi vertices if necessary. */
12280   if (*vpointlist == (REAL *) NULL) {
12281     *vpointlist = (REAL *) malloc(triangles.items * 2 * sizeof(REAL));
12282     if (*vpointlist == (REAL *) NULL) {
12283       printf("Error:  Out of memory.\n");
12284       exit(1);
12285     }
12286   }
12287   /* Allocate memory for Voronoi vertex attributes if necessary. */
12288   if (*vpointattriblist == (REAL *) NULL) {
12289     *vpointattriblist = (REAL *) malloc(triangles.items * nextras *
12290                                         sizeof(REAL));
12291     if (*vpointattriblist == (REAL *) NULL) {
12292       printf("Error:  Out of memory.\n");
12293       exit(1);
12294     }
12295   }
12296   *vpointmarkerlist = (int *) NULL;
12297   plist = *vpointlist;
12298   palist = *vpointattriblist;
12299   coordindex = 0;
12300   attribindex = 0;
12301 #else /* not TRILIBRARY */
12302   if (!quiet) {
12303     printf("Writing %s.\n", vnodefilename);
12304   }
12305   outfile = fopen(vnodefilename, "w");
12306   if (outfile == (FILE *) NULL) {
12307     printf("  Error:  Cannot create file %s.\n", vnodefilename);
12308     exit(1);
12309   }
12310   /* Number of triangles, two dimensions, number of point attributes, */
12311   /*   zero markers.                                                  */
12312   fprintf(outfile, "%ld  %d  %d  %d\n", triangles.items, 2, nextras, 0);
12313 #endif /* not TRILIBRARY */
12314
12315   traversalinit(&triangles);
12316   triangleloop.tri = triangletraverse();
12317   triangleloop.orient = 0;
12318   vnodenumber = firstnumber;
12319   while (triangleloop.tri != (triangle *) NULL) {
12320     org(triangleloop, torg);
12321     dest(triangleloop, tdest);
12322     apex(triangleloop, tapex);
12323     findcircumcenter(torg, tdest, tapex, circumcenter, &xi, &eta);
12324 #ifdef TRILIBRARY
12325     /* X and y coordinates. */
12326     plist[coordindex++] = circumcenter[0];
12327     plist[coordindex++] = circumcenter[1];
12328     for (i = 2; i < 2 + nextras; i++) {
12329       /* Interpolate the point attributes at the circumcenter. */
12330       palist[attribindex++] = torg[i] + xi * (tdest[i] - torg[i])
12331                                      + eta * (tapex[i] - torg[i]);
12332     }
12333 #else /* not TRILIBRARY */
12334     /* Voronoi vertex number, x and y coordinates. */
12335     fprintf(outfile, "%4d    %.17g  %.17g", vnodenumber, circumcenter[0],
12336             circumcenter[1]);
12337     for (i = 2; i < 2 + nextras; i++) {
12338       /* Interpolate the point attributes at the circumcenter. */
12339       fprintf(outfile, "  %.17g", torg[i] + xi * (tdest[i] - torg[i])
12340                                          + eta * (tapex[i] - torg[i]));
12341     }
12342     fprintf(outfile, "\n");
12343 #endif /* not TRILIBRARY */
12344
12345     * (int *) (triangleloop.tri + 6) = vnodenumber;
12346     triangleloop.tri = triangletraverse();
12347     vnodenumber++;
12348   }
12349
12350 #ifndef TRILIBRARY
12351   finishfile(outfile, argc, argv);
12352 #endif /* not TRILIBRARY */
12353
12354 #ifdef TRILIBRARY
12355   if (!quiet) {
12356     printf("Writing Voronoi edges.\n");
12357   }
12358   /* Allocate memory for output Voronoi edges if necessary. */
12359   if (*vedgelist == (int *) NULL) {
12360     *vedgelist = (int *) malloc(edges * 2 * sizeof(int));
12361     if (*vedgelist == (int *) NULL) {
12362       printf("Error:  Out of memory.\n");
12363       exit(1);
12364     }
12365   }
12366   *vedgemarkerlist = (int *) NULL;
12367   /* Allocate memory for output Voronoi norms if necessary. */
12368   if (*vnormlist == (REAL *) NULL) {
12369     *vnormlist = (REAL *) malloc(edges * 2 * sizeof(REAL));
12370     if (*vnormlist == (REAL *) NULL) {
12371       printf("Error:  Out of memory.\n");
12372       exit(1);
12373     }
12374   }
12375   elist = *vedgelist;
12376   normlist = *vnormlist;
12377   coordindex = 0;
12378 #else /* not TRILIBRARY */
12379   if (!quiet) {
12380     printf("Writing %s.\n", vedgefilename);
12381   }
12382   outfile = fopen(vedgefilename, "w");
12383   if (outfile == (FILE *) NULL) {
12384     printf("  Error:  Cannot create file %s.\n", vedgefilename);
12385     exit(1);
12386   }
12387   /* Number of edges, zero boundary markers. */
12388   fprintf(outfile, "%ld  %d\n", edges, 0);
12389 #endif /* not TRILIBRARY */
12390
12391   traversalinit(&triangles);
12392   triangleloop.tri = triangletraverse();
12393   vedgenumber = firstnumber;
12394   /* To loop over the set of edges, loop over all triangles, and look at   */
12395   /*   the three edges of each triangle.  If there isn't another triangle  */
12396   /*   adjacent to the edge, operate on the edge.  If there is another     */
12397   /*   adjacent triangle, operate on the edge only if the current triangle */
12398   /*   has a smaller pointer than its neighbor.  This way, each edge is    */
12399   /*   considered only once.                                               */
12400   while (triangleloop.tri != (triangle *) NULL) {
12401     for (triangleloop.orient = 0; triangleloop.orient < 3;
12402          triangleloop.orient++) {
12403       sym(triangleloop, trisym);
12404       if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
12405         /* Find the number of this triangle (and Voronoi vertex). */
12406         p1 = * (int *) (triangleloop.tri + 6);
12407         if (trisym.tri == dummytri) {
12408           org(triangleloop, torg);
12409           dest(triangleloop, tdest);
12410 #ifdef TRILIBRARY
12411           /* Copy an infinite ray.  Index of one endpoint, and -1. */
12412           elist[coordindex] = p1;
12413           normlist[coordindex++] = tdest[1] - torg[1];
12414           elist[coordindex] = -1;
12415           normlist[coordindex++] = torg[0] - tdest[0];
12416 #else /* not TRILIBRARY */
12417           /* Write an infinite ray.  Edge number, index of one endpoint, -1, */
12418           /*   and x and y coordinates of a vector representing the          */
12419           /*   direction of the ray.                                         */
12420           fprintf(outfile, "%4d   %d  %d   %.17g  %.17g\n", vedgenumber,
12421                   p1, -1, tdest[1] - torg[1], torg[0] - tdest[0]);
12422 #endif /* not TRILIBRARY */
12423         } else {
12424           /* Find the number of the adjacent triangle (and Voronoi vertex). */
12425           p2 = * (int *) (trisym.tri + 6);
12426           /* Finite edge.  Write indices of two endpoints. */
12427 #ifdef TRILIBRARY
12428           elist[coordindex] = p1;
12429           normlist[coordindex++] = 0.0;
12430           elist[coordindex] = p2;
12431           normlist[coordindex++] = 0.0;
12432 #else /* not TRILIBRARY */
12433           fprintf(outfile, "%4d   %d  %d\n", vedgenumber, p1, p2);
12434 #endif /* not TRILIBRARY */
12435         }
12436         vedgenumber++;
12437       }
12438     }
12439     triangleloop.tri = triangletraverse();
12440   }
12441
12442 #ifndef TRILIBRARY
12443   finishfile(outfile, argc, argv);
12444 #endif /* not TRILIBRARY */
12445 }
12446
12447 #ifdef TRILIBRARY
12448
12449 void writeneighbors(neighborlist)
12450 int **neighborlist;
12451
12452 #else /* not TRILIBRARY */
12453
12454 void writeneighbors(neighborfilename, argc, argv)
12455 char *neighborfilename;
12456 int argc;
12457 char **argv;
12458
12459 #endif /* not TRILIBRARY */
12460
12461 {
12462 #ifdef TRILIBRARY
12463   int *nlist;
12464   int index;
12465 #else /* not TRILIBRARY */
12466   FILE *outfile;
12467 #endif /* not TRILIBRARY */
12468   struct triedge triangleloop, trisym;
12469   int elementnumber;
12470   int neighbor1, neighbor2, neighbor3;
12471   triangle ptr;                         /* Temporary variable used by sym(). */
12472
12473 #ifdef TRILIBRARY
12474   if (!quiet) {
12475     printf("Writing neighbors.\n");
12476   }
12477   /* Allocate memory for neighbors if necessary. */
12478   if (*neighborlist == (int *) NULL) {
12479     *neighborlist = (int *) malloc(triangles.items * 3 * sizeof(int));
12480     if (*neighborlist == (int *) NULL) {
12481       printf("Error:  Out of memory.\n");
12482       exit(1);
12483     }
12484   }
12485   nlist = *neighborlist;
12486   index = 0;
12487 #else /* not TRILIBRARY */
12488   if (!quiet) {
12489     printf("Writing %s.\n", neighborfilename);
12490   }
12491   outfile = fopen(neighborfilename, "w");
12492   if (outfile == (FILE *) NULL) {
12493     printf("  Error:  Cannot create file %s.\n", neighborfilename);
12494     exit(1);
12495   }
12496   /* Number of triangles, three edges per triangle. */
12497   fprintf(outfile, "%ld  %d\n", triangles.items, 3);
12498 #endif /* not TRILIBRARY */
12499
12500   traversalinit(&triangles);
12501   triangleloop.tri = triangletraverse();
12502   triangleloop.orient = 0;
12503   elementnumber = firstnumber;
12504   while (triangleloop.tri != (triangle *) NULL) {
12505     * (int *) (triangleloop.tri + 6) = elementnumber;
12506     triangleloop.tri = triangletraverse();
12507     elementnumber++;
12508   }
12509   * (int *) (dummytri + 6) = -1;
12510
12511   traversalinit(&triangles);
12512   triangleloop.tri = triangletraverse();
12513   elementnumber = firstnumber;
12514   while (triangleloop.tri != (triangle *) NULL) {
12515     triangleloop.orient = 1;
12516     sym(triangleloop, trisym);
12517     neighbor1 = * (int *) (trisym.tri + 6);
12518     triangleloop.orient = 2;
12519     sym(triangleloop, trisym);
12520     neighbor2 = * (int *) (trisym.tri + 6);
12521     triangleloop.orient = 0;
12522     sym(triangleloop, trisym);
12523     neighbor3 = * (int *) (trisym.tri + 6);
12524 #ifdef TRILIBRARY
12525     nlist[index++] = neighbor1;
12526     nlist[index++] = neighbor2;
12527     nlist[index++] = neighbor3;
12528 #else /* not TRILIBRARY */
12529     /* Triangle number, neighboring triangle numbers. */
12530     fprintf(outfile, "%4d    %d  %d  %d\n", elementnumber,
12531             neighbor1, neighbor2, neighbor3);
12532 #endif /* not TRILIBRARY */
12533
12534     triangleloop.tri = triangletraverse();
12535     elementnumber++;
12536   }
12537
12538 #ifndef TRILIBRARY
12539   finishfile(outfile, argc, argv);
12540 #endif /* TRILIBRARY */
12541 }
12542
12543 /*****************************************************************************/
12544 /*                                                                           */
12545 /*  writeoff()   Write the triangulation to an .off file.                    */
12546 /*                                                                           */
12547 /*  OFF stands for the Object File Format, a format used by the Geometry     */
12548 /*  Center's Geomview package.                                               */
12549 /*                                                                           */
12550 /*****************************************************************************/
12551
12552 #ifndef TRILIBRARY
12553
12554 void writeoff(offfilename, argc, argv)
12555 char *offfilename;
12556 int argc;
12557 char **argv;
12558 {
12559   FILE *outfile;
12560   struct triedge triangleloop;
12561   point pointloop;
12562   point p1, p2, p3;
12563
12564   if (!quiet) {
12565     printf("Writing %s.\n", offfilename);
12566   }
12567   outfile = fopen(offfilename, "w");
12568   if (outfile == (FILE *) NULL) {
12569     printf("  Error:  Cannot create file %s.\n", offfilename);
12570     exit(1);
12571   }
12572   /* Number of points, triangles, and edges. */
12573   fprintf(outfile, "OFF\n%ld  %ld  %ld\n", points.items, triangles.items,
12574           edges);
12575
12576   /* Write the points. */
12577   traversalinit(&points);
12578   pointloop = pointtraverse();
12579   while (pointloop != (point) NULL) {
12580     /* The "0.0" is here because the OFF format uses 3D coordinates. */
12581     fprintf(outfile, " %.17g  %.17g  %.17g\n", pointloop[0],
12582             pointloop[1], 0.0);
12583     pointloop = pointtraverse();
12584   }
12585
12586   /* Write the triangles. */
12587   traversalinit(&triangles);
12588   triangleloop.tri = triangletraverse();
12589   triangleloop.orient = 0;
12590   while (triangleloop.tri != (triangle *) NULL) {
12591     org(triangleloop, p1);
12592     dest(triangleloop, p2);
12593     apex(triangleloop, p3);
12594     /* The "3" means a three-vertex polygon. */
12595     fprintf(outfile, " 3   %4d  %4d  %4d\n", pointmark(p1) - 1,
12596             pointmark(p2) - 1, pointmark(p3) - 1);
12597     triangleloop.tri = triangletraverse();
12598   }
12599   finishfile(outfile, argc, argv);
12600 }
12601
12602 #endif /* not TRILIBRARY */
12603
12604 /**                                                                         **/
12605 /**                                                                         **/
12606 /********* File I/O routines end here                                *********/
12607
12608 /*****************************************************************************/
12609 /*                                                                           */
12610 /*  quality_statistics()   Print statistics about the quality of the mesh.   */
12611 /*                                                                           */
12612 /*****************************************************************************/
12613
12614 void quality_statistics()
12615 {
12616   struct triedge triangleloop;
12617   point p[3];
12618   REAL cossquaretable[8];
12619   REAL ratiotable[16];
12620   REAL dx[3], dy[3];
12621   REAL edgelength[3];
12622   REAL dotproduct;
12623   REAL cossquare;
12624   REAL triarea;
12625   REAL shortest, longest;
12626   REAL trilongest2;
12627   REAL smallestarea, biggestarea;
12628   REAL triminaltitude2;
12629   REAL minaltitude;
12630   REAL triaspect2;
12631   REAL worstaspect;
12632   REAL smallestangle, biggestangle;
12633   REAL radconst, degconst;
12634   int angletable[18];
12635   int aspecttable[16];
12636   int aspectindex;
12637   int tendegree;
12638   int acutebiggest;
12639   int i, ii, j, k;
12640
12641   printf("Mesh quality statistics:\n\n");
12642   radconst = PI / 18.0;
12643   degconst = 180.0 / PI;
12644   for (i = 0; i < 8; i++) {
12645     cossquaretable[i] = cos(radconst * (REAL) (i + 1));
12646     cossquaretable[i] = cossquaretable[i] * cossquaretable[i];
12647   }
12648   for (i = 0; i < 18; i++) {
12649     angletable[i] = 0;
12650   }
12651
12652   ratiotable[0]  =      1.5;      ratiotable[1]  =     2.0;
12653   ratiotable[2]  =      2.5;      ratiotable[3]  =     3.0;
12654   ratiotable[4]  =      4.0;      ratiotable[5]  =     6.0;
12655   ratiotable[6]  =     10.0;      ratiotable[7]  =    15.0;
12656   ratiotable[8]  =     25.0;      ratiotable[9]  =    50.0;
12657   ratiotable[10] =    100.0;      ratiotable[11] =   300.0;
12658   ratiotable[12] =   1000.0;      ratiotable[13] = 10000.0;
12659   ratiotable[14] = 100000.0;      ratiotable[15] =     0.0;
12660   for (i = 0; i < 16; i++) {
12661     aspecttable[i] = 0;
12662   }
12663
12664   worstaspect = 0.0;
12665   minaltitude = xmax - xmin + ymax - ymin;
12666   minaltitude = minaltitude * minaltitude;
12667   shortest = minaltitude;
12668   longest = 0.0;
12669   smallestarea = minaltitude;
12670   biggestarea = 0.0;
12671   worstaspect = 0.0;
12672   smallestangle = 0.0;
12673   biggestangle = 2.0;
12674   acutebiggest = 1;
12675
12676   traversalinit(&triangles);
12677   triangleloop.tri = triangletraverse();
12678   triangleloop.orient = 0;
12679   while (triangleloop.tri != (triangle *) NULL) {
12680     org(triangleloop, p[0]);
12681     dest(triangleloop, p[1]);
12682     apex(triangleloop, p[2]);
12683     trilongest2 = 0.0;
12684
12685     for (i = 0; i < 3; i++) {
12686       j = plus1mod3[i];
12687       k = minus1mod3[i];
12688       dx[i] = p[j][0] - p[k][0];
12689       dy[i] = p[j][1] - p[k][1];
12690       edgelength[i] = dx[i] * dx[i] + dy[i] * dy[i];
12691       if (edgelength[i] > trilongest2) {
12692         trilongest2 = edgelength[i];
12693       }
12694       if (edgelength[i] > longest) {
12695         longest = edgelength[i];
12696       }
12697       if (edgelength[i] < shortest) {
12698         shortest = edgelength[i];
12699       }
12700     }
12701
12702     triarea = counterclockwise(p[0], p[1], p[2]);
12703     if (triarea < smallestarea) {
12704       smallestarea = triarea;
12705     }
12706     if (triarea > biggestarea) {
12707       biggestarea = triarea;
12708     }
12709     triminaltitude2 = triarea * triarea / trilongest2;
12710     if (triminaltitude2 < minaltitude) {
12711       minaltitude = triminaltitude2;
12712     }
12713     triaspect2 = trilongest2 / triminaltitude2;
12714     if (triaspect2 > worstaspect) {
12715       worstaspect = triaspect2;
12716     }
12717     aspectindex = 0;
12718     while ((triaspect2 > ratiotable[aspectindex] * ratiotable[aspectindex])
12719            && (aspectindex < 15)) {
12720       aspectindex++;
12721     }
12722     aspecttable[aspectindex]++;
12723
12724     for (i = 0; i < 3; i++) {
12725       j = plus1mod3[i];
12726       k = minus1mod3[i];
12727       dotproduct = dx[j] * dx[k] + dy[j] * dy[k];
12728       cossquare = dotproduct * dotproduct / (edgelength[j] * edgelength[k]);
12729       tendegree = 8;
12730       for (ii = 7; ii >= 0; ii--) {
12731         if (cossquare > cossquaretable[ii]) {
12732           tendegree = ii;
12733         }
12734       }
12735       if (dotproduct <= 0.0) {
12736         angletable[tendegree]++;
12737         if (cossquare > smallestangle) {
12738           smallestangle = cossquare;
12739         }
12740         if (acutebiggest && (cossquare < biggestangle)) {
12741           biggestangle = cossquare;
12742         }
12743       } else {
12744         angletable[17 - tendegree]++;
12745         if (acutebiggest || (cossquare > biggestangle)) {
12746           biggestangle = cossquare;
12747           acutebiggest = 0;
12748         }
12749       }
12750     }
12751     triangleloop.tri = triangletraverse();
12752   }
12753
12754   shortest = sqrt(shortest);
12755   longest = sqrt(longest);
12756   minaltitude = sqrt(minaltitude);
12757   worstaspect = sqrt(worstaspect);
12758   smallestarea *= 2.0;
12759   biggestarea *= 2.0;
12760   if (smallestangle >= 1.0) {
12761     smallestangle = 0.0;
12762   } else {
12763     smallestangle = degconst * acos(sqrt(smallestangle));
12764   }
12765   if (biggestangle >= 1.0) {
12766     biggestangle = 180.0;
12767   } else {
12768     if (acutebiggest) {
12769       biggestangle = degconst * acos(sqrt(biggestangle));
12770     } else {
12771       biggestangle = 180.0 - degconst * acos(sqrt(biggestangle));
12772     }
12773   }
12774
12775   printf("  Smallest area: %16.5g   |  Largest area: %16.5g\n",
12776          smallestarea, biggestarea);
12777   printf("  Shortest edge: %16.5g   |  Longest edge: %16.5g\n",
12778          shortest, longest);
12779   printf("  Shortest altitude: %12.5g   |  Largest aspect ratio: %8.5g\n\n",
12780          minaltitude, worstaspect);
12781   printf("  Aspect ratio histogram:\n");
12782   printf("  1.1547 - %-6.6g    :  %8d    | %6.6g - %-6.6g     :  %8d\n",
12783          ratiotable[0], aspecttable[0], ratiotable[7], ratiotable[8],
12784          aspecttable[8]);
12785   for (i = 1; i < 7; i++) {
12786     printf("  %6.6g - %-6.6g    :  %8d    | %6.6g - %-6.6g     :  %8d\n",
12787            ratiotable[i - 1], ratiotable[i], aspecttable[i],
12788            ratiotable[i + 7], ratiotable[i + 8], aspecttable[i + 8]);
12789   }
12790   printf("  %6.6g - %-6.6g    :  %8d    | %6.6g -            :  %8d\n",
12791          ratiotable[6], ratiotable[7], aspecttable[7], ratiotable[14],
12792          aspecttable[15]);
12793   printf(
12794 "  (Triangle aspect ratio is longest edge divided by shortest altitude)\n\n");
12795   printf("  Smallest angle: %15.5g   |  Largest angle: %15.5g\n\n",
12796          smallestangle, biggestangle);
12797   printf("  Angle histogram:\n");
12798   for (i = 0; i < 9; i++) {
12799     printf("    %3d - %3d degrees:  %8d    |    %3d - %3d degrees:  %8d\n",
12800            i * 10, i * 10 + 10, angletable[i],
12801            i * 10 + 90, i * 10 + 100, angletable[i + 9]);
12802   }
12803   printf("\n");
12804 }
12805
12806 /*****************************************************************************/
12807 /*                                                                           */
12808 /*  statistics()   Print all sorts of cool facts.                            */
12809 /*                                                                           */
12810 /*****************************************************************************/
12811
12812 void statistics()
12813 {
12814   printf("\nStatistics:\n\n");
12815   printf("  Input points: %d\n", inpoints);
12816   if (refine) {
12817     printf("  Input triangles: %d\n", inelements);
12818   }
12819   if (poly) {
12820     printf("  Input segments: %d\n", insegments);
12821     if (!refine) {
12822       printf("  Input holes: %d\n", holes);
12823     }
12824   }
12825
12826   printf("\n  Mesh points: %ld\n", points.items);
12827   printf("  Mesh triangles: %ld\n", triangles.items);
12828   printf("  Mesh edges: %ld\n", edges);
12829   if (poly || refine) {
12830     printf("  Mesh boundary edges: %ld\n", hullsize);
12831     printf("  Mesh segments: %ld\n\n", shelles.items);
12832   } else {
12833     printf("  Mesh convex hull edges: %ld\n\n", hullsize);
12834   }
12835   if (verbose) {
12836     quality_statistics();
12837     printf("Memory allocation statistics:\n\n");
12838     printf("  Maximum number of points: %ld\n", points.maxitems);
12839     printf("  Maximum number of triangles: %ld\n", triangles.maxitems);
12840     if (shelles.maxitems > 0) {
12841       printf("  Maximum number of segments: %ld\n", shelles.maxitems);
12842     }
12843     if (viri.maxitems > 0) {
12844       printf("  Maximum number of viri: %ld\n", viri.maxitems);
12845     }
12846     if (badsegments.maxitems > 0) {
12847       printf("  Maximum number of encroached segments: %ld\n",
12848              badsegments.maxitems);
12849     }
12850     if (badtriangles.maxitems > 0) {
12851       printf("  Maximum number of bad triangles: %ld\n",
12852              badtriangles.maxitems);
12853     }
12854     if (splaynodes.maxitems > 0) {
12855       printf("  Maximum number of splay tree nodes: %ld\n",
12856              splaynodes.maxitems);
12857     }
12858     printf("  Approximate heap memory use (bytes): %ld\n\n",
12859            points.maxitems * points.itembytes
12860            + triangles.maxitems * triangles.itembytes
12861            + shelles.maxitems * shelles.itembytes
12862            + viri.maxitems * viri.itembytes
12863            + badsegments.maxitems * badsegments.itembytes
12864            + badtriangles.maxitems * badtriangles.itembytes
12865            + splaynodes.maxitems * splaynodes.itembytes);
12866
12867     printf("Algorithmic statistics:\n\n");
12868     printf("  Number of incircle tests: %ld\n", incirclecount);
12869     printf("  Number of orientation tests: %ld\n", counterclockcount);
12870     if (hyperbolacount > 0) {
12871       printf("  Number of right-of-hyperbola tests: %ld\n",
12872              hyperbolacount);
12873     }
12874     if (circumcentercount > 0) {
12875       printf("  Number of circumcenter computations: %ld\n",
12876              circumcentercount);
12877     }
12878     if (circletopcount > 0) {
12879       printf("  Number of circle top computations: %ld\n",
12880              circletopcount);
12881     }
12882     printf("\n");
12883   }
12884 }
12885
12886 /*****************************************************************************/
12887 /*                                                                           */
12888 /*  main() or triangulate()   Gosh, do everything.                           */
12889 /*                                                                           */
12890 /*  The sequence is roughly as follows.  Many of these steps can be skipped, */
12891 /*  depending on the command line switches.                                  */
12892 /*                                                                           */
12893 /*  - Initialize constants and parse the command line.                       */
12894 /*  - Read the points from a file and either                                 */
12895 /*    - triangulate them (no -r), or                                         */
12896 /*    - read an old mesh from files and reconstruct it (-r).                 */
12897 /*  - Insert the PSLG segments (-p), and possibly segments on the convex     */
12898 /*      hull (-c).                                                           */
12899 /*  - Read the holes (-p), regional attributes (-pA), and regional area      */
12900 /*      constraints (-pa).  Carve the holes and concavities, and spread the  */
12901 /*      regional attributes and area constraints.                            */
12902 /*  - Enforce the constraints on minimum angle (-q) and maximum area (-a).   */
12903 /*      Also enforce the conforming Delaunay property (-q and -a).           */
12904 /*  - Compute the number of edges in the resulting mesh.                     */
12905 /*  - Promote the mesh's linear triangles to higher order elements (-o).     */
12906 /*  - Write the output files and print the statistics.                       */
12907 /*  - Check the consistency and Delaunay property of the mesh (-C).          */
12908 /*                                                                           */
12909 /*****************************************************************************/
12910
12911 #ifdef TRILIBRARY
12912
12913 void triangulate(triswitches, in, out, vorout)
12914 char *triswitches;
12915 struct triangulateio *in;
12916 struct triangulateio *out;
12917 struct triangulateio *vorout;
12918
12919 #else /* not TRILIBRARY */
12920
12921 int main(argc, argv)
12922 int argc;
12923 char **argv;
12924
12925 #endif /* not TRILIBRARY */
12926
12927 {
12928   REAL *holearray;                                        /* Array of holes. */
12929   REAL *regionarray;   /* Array of regional attributes and area constraints. */
12930 #ifndef TRILIBRARY
12931   FILE *polyfile;
12932 #endif /* not TRILIBRARY */
12933 #ifndef NO_TIMER
12934   /* Variables for timing the performance of Triangle.  The types are */
12935   /*   defined in sys/time.h.                                         */
12936   struct timeval tv0, tv1, tv2, tv3, tv4, tv5, tv6;
12937   struct timezone tz;
12938 #endif /* NO_TIMER */
12939
12940 #ifndef NO_TIMER
12941   gettimeofday(&tv0, &tz);
12942 #endif /* NO_TIMER */
12943
12944   triangleinit();
12945 #ifdef TRILIBRARY
12946   parsecommandline(1, &triswitches);
12947 #else /* not TRILIBRARY */
12948   parsecommandline(argc, argv);
12949 #endif /* not TRILIBRARY */
12950
12951 #ifdef TRILIBRARY
12952   transfernodes(in->pointlist, in->pointattributelist, in->pointmarkerlist,
12953                 in->numberofpoints, in->numberofpointattributes);
12954 #else /* not TRILIBRARY */
12955   readnodes(innodefilename, inpolyfilename, &polyfile);
12956 #endif /* not TRILIBRARY */
12957
12958 #ifndef NO_TIMER
12959   if (!quiet) {
12960     gettimeofday(&tv1, &tz);
12961   }
12962 #endif /* NO_TIMER */
12963
12964 #ifdef CDT_ONLY
12965   hullsize = delaunay();                          /* Triangulate the points. */
12966 #else /* not CDT_ONLY */
12967   if (refine) {
12968     /* Read and reconstruct a mesh. */
12969 #ifdef TRILIBRARY
12970     hullsize = reconstruct(in->trianglelist, in->triangleattributelist,
12971                            in->trianglearealist, in->numberoftriangles,
12972                            in->numberofcorners, in->numberoftriangleattributes,
12973                            in->segmentlist, in->segmentmarkerlist,
12974                            in->numberofsegments);
12975 #else /* not TRILIBRARY */
12976     hullsize = reconstruct(inelefilename, areafilename, inpolyfilename,
12977                            polyfile);
12978 #endif /* not TRILIBRARY */
12979   } else {
12980     hullsize = delaunay();                        /* Triangulate the points. */
12981   }
12982 #endif /* not CDT_ONLY */
12983
12984 #ifndef NO_TIMER
12985   if (!quiet) {
12986     gettimeofday(&tv2, &tz);
12987     if (refine) {
12988       printf("Mesh reconstruction");
12989     } else {
12990       printf("Delaunay");
12991     }
12992     printf(" milliseconds:  %ld\n", 1000l * (tv2.tv_sec - tv1.tv_sec)
12993            + (tv2.tv_usec - tv1.tv_usec) / 1000l);
12994   }
12995 #endif /* NO_TIMER */
12996
12997   /* Ensure that no point can be mistaken for a triangular bounding */
12998   /*   box point in insertsite().                                   */
12999   infpoint1 = (point) NULL;
13000   infpoint2 = (point) NULL;
13001   infpoint3 = (point) NULL;
13002
13003   if (useshelles) {
13004     checksegments = 1;                  /* Segments will be introduced next. */
13005     if (!refine) {
13006       /* Insert PSLG segments and/or convex hull segments. */
13007 #ifdef TRILIBRARY
13008       insegments = formskeleton(in->segmentlist, in->segmentmarkerlist,
13009                                 in->numberofsegments);
13010 #else /* not TRILIBRARY */
13011       insegments = formskeleton(polyfile, inpolyfilename);
13012 #endif /* not TRILIBRARY */
13013     }
13014   }
13015
13016 #ifndef NO_TIMER
13017   if (!quiet) {
13018     gettimeofday(&tv3, &tz);
13019     if (useshelles && !refine) {
13020       printf("Segment milliseconds:  %ld\n",
13021              1000l * (tv3.tv_sec - tv2.tv_sec)
13022              + (tv3.tv_usec - tv2.tv_usec) / 1000l);
13023     }
13024   }
13025 #endif /* NO_TIMER */
13026
13027   if (poly) {
13028 #ifdef TRILIBRARY
13029     holearray = in->holelist;
13030     holes = in->numberofholes;
13031     regionarray = in->regionlist;
13032     regions = in->numberofregions;
13033 #else /* not TRILIBRARY */
13034     readholes(polyfile, inpolyfilename, &holearray, &holes,
13035               &regionarray, &regions);
13036 #endif /* not TRILIBRARY */
13037     if (!refine) {
13038       /* Carve out holes and concavities. */
13039       carveholes(holearray, holes, regionarray, regions);
13040     }
13041   } else {
13042     /* Without a PSLG, there can be no holes or regional attributes   */
13043     /*   or area constraints.  The following are set to zero to avoid */
13044     /*   an accidental free() later.                                  */
13045     holes = 0;
13046     regions = 0;
13047   }
13048
13049 #ifndef NO_TIMER
13050   if (!quiet) {
13051     gettimeofday(&tv4, &tz);
13052     if (poly && !refine) {
13053       printf("Hole milliseconds:  %ld\n", 1000l * (tv4.tv_sec - tv3.tv_sec)
13054              + (tv4.tv_usec - tv3.tv_usec) / 1000l);
13055     }
13056   }
13057 #endif /* NO_TIMER */
13058
13059 #ifndef CDT_ONLY
13060   if (quality) {
13061     enforcequality();                 /* Enforce angle and area constraints. */
13062   }
13063 #endif /* not CDT_ONLY */
13064
13065 #ifndef NO_TIMER
13066   if (!quiet) {
13067     gettimeofday(&tv5, &tz);
13068 #ifndef CDT_ONLY
13069     if (quality) {
13070       printf("Quality milliseconds:  %ld\n",
13071              1000l * (tv5.tv_sec - tv4.tv_sec)
13072              + (tv5.tv_usec - tv4.tv_usec) / 1000l);
13073     }
13074 #endif /* not CDT_ONLY */
13075   }
13076 #endif /* NO_TIMER */
13077
13078   /* Compute the number of edges. */
13079   edges = (3l * triangles.items + hullsize) / 2l;
13080
13081   if (order > 1) {
13082     highorder();             /* Promote elements to higher polynomial order. */
13083   }
13084   if (!quiet) {
13085     printf("\n");
13086   }
13087
13088 #ifdef TRILIBRARY
13089   out->numberofpoints = points.items;
13090   out->numberofpointattributes = nextras;
13091   out->numberoftriangles = triangles.items;
13092   out->numberofcorners = (order + 1) * (order + 2) / 2;
13093   out->numberoftriangleattributes = eextras;
13094   out->numberofedges = edges;
13095   if (useshelles) {
13096     out->numberofsegments = shelles.items;
13097   } else {
13098     out->numberofsegments = hullsize;
13099   }
13100   if (vorout != (struct triangulateio *) NULL) {
13101     vorout->numberofpoints = triangles.items;
13102     vorout->numberofpointattributes = nextras;
13103     vorout->numberofedges = edges;
13104   }
13105 #endif /* TRILIBRARY */
13106   /* If not using iteration numbers, don't write a .node file if one was */
13107   /*   read, because the original one would be overwritten!              */
13108   if (nonodewritten || (noiterationnum && readnodefile)) {
13109     if (!quiet) {
13110 #ifdef TRILIBRARY
13111       printf("NOT writing points.\n");
13112 #else /* not TRILIBRARY */
13113       printf("NOT writing a .node file.\n");
13114 #endif /* not TRILIBRARY */
13115     }
13116     numbernodes();                 /* We must remember to number the points. */
13117   } else {
13118 #ifdef TRILIBRARY
13119     writenodes(&out->pointlist, &out->pointattributelist,
13120                &out->pointmarkerlist);
13121 #else /* not TRILIBRARY */
13122     writenodes(outnodefilename, argc, argv);      /* Numbers the points too. */
13123 #endif /* TRILIBRARY */
13124   }
13125   if (noelewritten) {
13126     if (!quiet) {
13127 #ifdef TRILIBRARY
13128       printf("NOT writing triangles.\n");
13129 #else /* not TRILIBRARY */
13130       printf("NOT writing an .ele file.\n");
13131 #endif /* not TRILIBRARY */
13132     }
13133   } else {
13134 #ifdef TRILIBRARY
13135     writeelements(&out->trianglelist, &out->triangleattributelist);
13136 #else /* not TRILIBRARY */
13137     writeelements(outelefilename, argc, argv);
13138 #endif /* not TRILIBRARY */
13139   }
13140   /* The -c switch (convex switch) causes a PSLG to be written */
13141   /*   even if none was read.                                  */
13142   if (poly || convex) {
13143     /* If not using iteration numbers, don't overwrite the .poly file. */
13144     if (nopolywritten || noiterationnum) {
13145       if (!quiet) {
13146 #ifdef TRILIBRARY
13147         printf("NOT writing segments.\n");
13148 #else /* not TRILIBRARY */
13149         printf("NOT writing a .poly file.\n");
13150 #endif /* not TRILIBRARY */
13151       }
13152     } else {
13153 #ifdef TRILIBRARY
13154       writepoly(&out->segmentlist, &out->segmentmarkerlist);
13155       out->numberofholes = holes;
13156       out->numberofregions = regions;
13157       if (poly) {
13158         out->holelist = in->holelist;
13159         out->regionlist = in->regionlist;
13160       } else {
13161         out->holelist = (REAL *) NULL;
13162         out->regionlist = (REAL *) NULL;
13163       }
13164 #else /* not TRILIBRARY */
13165       writepoly(outpolyfilename, holearray, holes, regionarray, regions,
13166                 argc, argv);
13167 #endif /* not TRILIBRARY */
13168     }
13169   }
13170 #ifndef TRILIBRARY
13171 #ifndef CDT_ONLY
13172   if (regions > 0) {
13173     free(regionarray);
13174   }
13175 #endif /* not CDT_ONLY */
13176   if (holes > 0) {
13177     free(holearray);
13178   }
13179   if (geomview) {
13180     writeoff(offfilename, argc, argv);
13181   }
13182 #endif /* not TRILIBRARY */
13183   if (edgesout) {
13184 #ifdef TRILIBRARY
13185     writeedges(&out->edgelist, &out->edgemarkerlist);
13186 #else /* not TRILIBRARY */
13187     writeedges(edgefilename, argc, argv);
13188 #endif /* not TRILIBRARY */
13189   }
13190   if (voronoi) {
13191 #ifdef TRILIBRARY
13192     writevoronoi(&vorout->pointlist, &vorout->pointattributelist,
13193                  &vorout->pointmarkerlist, &vorout->edgelist,
13194                  &vorout->edgemarkerlist, &vorout->normlist);
13195 #else /* not TRILIBRARY */
13196     writevoronoi(vnodefilename, vedgefilename, argc, argv);
13197 #endif /* not TRILIBRARY */
13198   }
13199   if (neighbors) {
13200 #ifdef TRILIBRARY
13201     writeneighbors(&out->neighborlist);
13202 #else /* not TRILIBRARY */
13203     writeneighbors(neighborfilename, argc, argv);
13204 #endif /* not TRILIBRARY */
13205   }
13206
13207   if (!quiet) {
13208 #ifndef NO_TIMER
13209     gettimeofday(&tv6, &tz);
13210     printf("\nOutput milliseconds:  %ld\n",
13211            1000l * (tv6.tv_sec - tv5.tv_sec)
13212            + (tv6.tv_usec - tv5.tv_usec) / 1000l);
13213     printf("Total running milliseconds:  %ld\n",
13214            1000l * (tv6.tv_sec - tv0.tv_sec)
13215            + (tv6.tv_usec - tv0.tv_usec) / 1000l);
13216 #endif /* NO_TIMER */
13217
13218     statistics();
13219   }
13220
13221 #ifndef REDUCED
13222   if (docheck) {
13223     checkmesh();
13224     checkdelaunay();
13225   }
13226 #endif /* not REDUCED */
13227
13228   triangledeinit();
13229 #ifndef TRILIBRARY
13230   return 0;
13231 #endif /* not TRILIBRARY */
13232 }