~ubuntu-branches/ubuntu/lucid/graphviz/lucid-updates

« back to all changes in this revision

Viewing changes to lib/topfish/triangle.c

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2008-06-19 20:23:23 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080619202323-ls23h96ntj9ny94m
Tags: 2.18-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build depend on liblualib50-dev instead of liblua5.1-0-dev.
  - Drop libttf-dev (libttf-dev is in universe) (LP: #174749).
  - Replace gs-common with ghostscript.
  - Build-depend on python-dev instead of python2.4-dev or python2.5-dev.
  - Mention the correct python version for the python bindings in the
    package description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
 
 
3
/*****************************************************************************/
 
4
/*                                                                           */
 
5
/*      888888888        ,o,                          / 888                  */
 
6
/*         888    88o88o  "    o8888o  88o8888o o88888o 888  o88888o         */
 
7
/*         888    888    888       88b 888  888 888 888 888 d888  88b        */
 
8
/*         888    888    888  o88^o888 888  888 "88888" 888 8888oo888        */
 
9
/*         888    888    888 C888  888 888  888  /      888 q888             */
 
10
/*         888    888    888  "88o^888 888  888 Cb      888  "88oooo"        */
 
11
/*                                              "8oo8D                       */
 
12
/*                                                                           */
 
13
/*  A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.      */
 
14
/*  (triangle.c)                                                             */
 
15
/*                                                                           */
 
16
/*  Version 1.3                                                              */
 
17
/*  July 19, 1996                                                            */
 
18
/*                                                                           */
 
19
/*  Copyright 1996                                                           */
 
20
/*  Jonathan Richard Shewchuk                                                */
 
21
/*  School of Computer Science                                               */
 
22
/*  Carnegie Mellon University                                               */
 
23
/*  5000 Forbes Avenue                                                       */
 
24
/*  Pittsburgh, Pennsylvania  15213-3891                                     */
 
25
/*  jrs@cs.cmu.edu                                                           */
 
26
/*                                                                           */
 
27
/*  This program may be freely redistributed under the condition that the    */
 
28
/*    copyright notices (including this entire header and the copyright      */
 
29
/*    notice printed when the `-h' switch is selected) are not removed, and  */
 
30
/*    no compensation is received.  Private, research, and institutional     */
 
31
/*    use is free.  You may distribute modified versions of this code UNDER  */
 
32
/*    THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE   */
 
33
/*    SAME FILE REMAIN UNDER COPYRIGHT OF THE ORIGINAL AUTHOR, BOTH SOURCE   */
 
34
/*    AND OBJECT CODE ARE MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR    */
 
35
/*    NOTICE IS GIVEN OF THE MODIFICATIONS.  Distribution of this code as    */
 
36
/*    part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT  */
 
37
/*    WITH THE AUTHOR.  (If you are not directly supplying this code to a    */
 
38
/*    customer, and you are instead telling them how they can obtain it for  */
 
39
/*    free, then you are not required to make any arrangement with me.)      */
 
40
/*                                                                           */
 
41
/*  Hypertext instructions for Triangle are available on the Web at          */
 
42
/*                                                                           */
 
43
/*      http://www.cs.cmu.edu/~quake/triangle.html                           */
 
44
/*                                                                           */
 
45
/*  Some of the references listed below are marked [*].  These are available */
 
46
/*    for downloading from the Web page                                      */
 
47
/*                                                                           */
 
48
/*      http://www.cs.cmu.edu/~quake/triangle.research.html                  */
 
49
/*                                                                           */
 
50
/*  A paper discussing some aspects of Triangle is available.  See Jonathan  */
 
51
/*    Richard Shewchuk, "Triangle:  Engineering a 2D Quality Mesh Generator  */
 
52
/*    and Delaunay Triangulator," First Workshop on Applied Computational    */
 
53
/*    Geometry, ACM, May 1996.  [*]                                          */
 
54
/*                                                                           */
 
55
/*  Triangle was created as part of the Archimedes project in the School of  */
 
56
/*    Computer Science at Carnegie Mellon University.  Archimedes is a       */
 
57
/*    system for compiling parallel finite element solvers.  For further     */
 
58
/*    information, see Anja Feldmann, Omar Ghattas, John R. Gilbert, Gary L. */
 
59
/*    Miller, David R. O'Hallaron, Eric J. Schwabe, Jonathan R. Shewchuk,    */
 
60
/*    and Shang-Hua Teng, "Automated Parallel Solution of Unstructured PDE   */
 
61
/*    Problems."  To appear in Communications of the ACM, we hope.           */
 
62
/*                                                                           */
 
63
/*  The quality mesh generation algorithm is due to Jim Ruppert, "A          */
 
64
/*    Delaunay Refinement Algorithm for Quality 2-Dimensional Mesh           */
 
65
/*    Generation," Journal of Algorithms 18(3):548-585, May 1995.  [*]       */
 
66
/*                                                                           */
 
67
/*  My implementation of the divide-and-conquer and incremental Delaunay     */
 
68
/*    triangulation algorithms follows closely the presentation of Guibas    */
 
69
/*    and Stolfi, even though I use a triangle-based data structure instead  */
 
70
/*    of their quad-edge data structure.  (In fact, I originally implemented */
 
71
/*    Triangle using the quad-edge data structure, but switching to a        */
 
72
/*    triangle-based data structure sped Triangle by a factor of two.)  The  */
 
73
/*    mesh manipulation primitives and the two aforementioned Delaunay       */
 
74
/*    triangulation algorithms are described by Leonidas J. Guibas and Jorge */
 
75
/*    Stolfi, "Primitives for the Manipulation of General Subdivisions and   */
 
76
/*    the Computation of Voronoi Diagrams," ACM Transactions on Graphics     */
 
77
/*    4(2):74-123, April 1985.                                               */
 
78
/*                                                                           */
 
79
/*  Their O(n log n) divide-and-conquer algorithm is adapted from Der-Tsai   */
 
80
/*    Lee and Bruce J. Schachter, "Two Algorithms for Constructing the       */
 
81
/*    Delaunay Triangulation," International Journal of Computer and         */
 
82
/*    Information Science 9(3):219-242, 1980.  The idea to improve the       */
 
83
/*    divide-and-conquer algorithm by alternating between vertical and       */
 
84
/*    horizontal cuts was introduced by Rex A. Dwyer, "A Faster Divide-and-  */
 
85
/*    Conquer Algorithm for Constructing Delaunay Triangulations,"           */
 
86
/*    Algorithmica 2(2):137-151, 1987.                                       */
 
87
/*                                                                           */
 
88
/*  The incremental insertion algorithm was first proposed by C. L. Lawson,  */
 
89
/*    "Software for C1 Surface Interpolation," in Mathematical Software III, */
 
90
/*    John R. Rice, editor, Academic Press, New York, pp. 161-194, 1977.     */
 
91
/*    For point location, I use the algorithm of Ernst P. Mucke, Isaac       */
 
92
/*    Saias, and Binhai Zhu, "Fast Randomized Point Location Without         */
 
93
/*    Preprocessing in Two- and Three-dimensional Delaunay Triangulations,"  */
 
94
/*    Proceedings of the Twelfth Annual Symposium on Computational Geometry, */
 
95
/*    ACM, May 1996.  [*]  If I were to randomize the order of point         */
 
96
/*    insertion (I currently don't bother), their result combined with the   */
 
97
/*    result of Leonidas J. Guibas, Donald E. Knuth, and Micha Sharir,       */
 
98
/*    "Randomized Incremental Construction of Delaunay and Voronoi           */
 
99
/*    Diagrams," Algorithmica 7(4):381-413, 1992, would yield an expected    */
 
100
/*    O(n^{4/3}) bound on running time.                                      */
 
101
/*                                                                           */
 
102
/*  The O(n log n) sweepline Delaunay triangulation algorithm is taken from  */
 
103
/*    Steven Fortune, "A Sweepline Algorithm for Voronoi Diagrams",          */
 
104
/*    Algorithmica 2(2):153-174, 1987.  A random sample of edges on the      */
 
105
/*    boundary of the triangulation are maintained in a splay tree for the   */
 
106
/*    purpose of point location.  Splay trees are described by Daniel        */
 
107
/*    Dominic Sleator and Robert Endre Tarjan, "Self-Adjusting Binary Search */
 
108
/*    Trees," Journal of the ACM 32(3):652-686, July 1985.                   */
 
109
/*                                                                           */
 
110
/*  The algorithms for exact computation of the signs of determinants are    */
 
111
/*    described in Jonathan Richard Shewchuk, "Adaptive Precision Floating-  */
 
112
/*    Point Arithmetic and Fast Robust Geometric Predicates," Technical      */
 
113
/*    Report CMU-CS-96-140, School of Computer Science, Carnegie Mellon      */
 
114
/*    University, Pittsburgh, Pennsylvania, May 1996.  [*]  (Submitted to    */
 
115
/*    Discrete & Computational Geometry.)  An abbreviated version appears as */
 
116
/*    Jonathan Richard Shewchuk, "Robust Adaptive Floating-Point Geometric   */
 
117
/*    Predicates," Proceedings of the Twelfth Annual Symposium on Computa-   */
 
118
/*    tional Geometry, ACM, May 1996.  [*]  Many of the ideas for my exact   */
 
119
/*    arithmetic routines originate with Douglas M. Priest, "Algorithms for  */
 
120
/*    Arbitrary Precision Floating Point Arithmetic," Tenth Symposium on     */
 
121
/*    Computer Arithmetic, 132-143, IEEE Computer Society Press, 1991.  [*]  */
 
122
/*    Many of the ideas for the correct evaluation of the signs of           */
 
123
/*    determinants are taken from Steven Fortune and Christopher J. Van Wyk, */
 
124
/*    "Efficient Exact Arithmetic for Computational Geometry," Proceedings   */
 
125
/*    of the Ninth Annual Symposium on Computational Geometry, ACM,          */
 
126
/*    pp. 163-172, May 1993, and from Steven Fortune, "Numerical Stability   */
 
127
/*    of Algorithms for 2D Delaunay Triangulations," International Journal   */
 
128
/*    of Computational Geometry & Applications 5(1-2):193-213, March-June    */
 
129
/*    1995.                                                                  */
 
130
/*                                                                           */
 
131
/*  For definitions of and results involving Delaunay triangulations,        */
 
132
/*    constrained and conforming versions thereof, and other aspects of      */
 
133
/*    triangular mesh generation, see the excellent survey by Marshall Bern  */
 
134
/*    and David Eppstein, "Mesh Generation and Optimal Triangulation," in    */
 
135
/*    Computing and Euclidean Geometry, Ding-Zhu Du and Frank Hwang,         */
 
136
/*    editors, World Scientific, Singapore, pp. 23-90, 1992.                 */
 
137
/*                                                                           */
 
138
/*  The time for incrementally adding PSLG (planar straight line graph)      */
 
139
/*    segments to create a constrained Delaunay triangulation is probably    */
 
140
/*    O(n^2) per segment in the worst case and O(n) per edge in the common   */
 
141
/*    case, where n is the number of triangles that intersect the segment    */
 
142
/*    before it is inserted.  This doesn't count point location, which can   */
 
143
/*    be much more expensive.  (This note does not apply to conforming       */
 
144
/*    Delaunay triangulations, for which a different method is used to       */
 
145
/*    insert segments.)                                                      */
 
146
/*                                                                           */
 
147
/*  The time for adding segments to a conforming Delaunay triangulation is   */
 
148
/*    not clear, but does not depend upon n alone.  In some cases, very      */
 
149
/*    small features (like a point lying next to a segment) can cause a      */
 
150
/*    single segment to be split an arbitrary number of times.  Of course,   */
 
151
/*    floating-point precision is a practical barrier to how much this can   */
 
152
/*    happen.                                                                */
 
153
/*                                                                           */
 
154
/*  The time for deleting a point from a Delaunay triangulation is O(n^2) in */
 
155
/*    the worst case and O(n) in the common case, where n is the degree of   */
 
156
/*    the point being deleted.  I could improve this to expected O(n) time   */
 
157
/*    by "inserting" the neighboring vertices in random order, but n is      */
 
158
/*    usually quite small, so it's not worth the bother.  (The O(n) time     */
 
159
/*    for random insertion follows from L. Paul Chew, "Building Voronoi      */
 
160
/*    Diagrams for Convex Polygons in Linear Expected Time," Technical       */
 
161
/*    Report PCS-TR90-147, Department of Mathematics and Computer Science,   */
 
162
/*    Dartmouth College, 1990.                                               */
 
163
/*                                                                           */
 
164
/*  Ruppert's Delaunay refinement algorithm typically generates triangles    */
 
165
/*    at a linear rate (constant time per triangle) after the initial        */
 
166
/*    triangulation is formed.  There may be pathological cases where more   */
 
167
/*    time is required, but these never arise in practice.                   */
 
168
/*                                                                           */
 
169
/*  The segment intersection formulae are straightforward.  If you want to   */
 
170
/*    see them derived, see Franklin Antonio.  "Faster Line Segment          */
 
171
/*    Intersection."  In Graphics Gems III (David Kirk, editor), pp. 199-    */
 
172
/*    202.  Academic Press, Boston, 1992.                                    */
 
173
/*                                                                           */
 
174
/*  If you make any improvements to this code, please please please let me   */
 
175
/*    know, so that I may obtain the improvements.  Even if you don't change */
 
176
/*    the code, I'd still love to hear what it's being used for.             */
 
177
/*                                                                           */
 
178
/*  Disclaimer:  Neither I nor Carnegie Mellon warrant this code in any way  */
 
179
/*    whatsoever.  This code is provided "as-is".  Use at your own risk.     */
 
180
/*                                                                           */
 
181
/*****************************************************************************/
 
182
 
 
183
/* For single precision (which will save some memory and reduce paging),     */
 
184
/*   define the symbol SINGLE by using the -DSINGLE compiler switch or by    */
 
185
/*   writing "#define SINGLE" below.                                         */
 
186
/*                                                                           */
 
187
/* For double precision (which will allow you to refine meshes to a smaller  */
 
188
/*   edge length), leave SINGLE undefined.                                   */
 
189
/*                                                                           */
 
190
/* Double precision uses more memory, but improves the resolution of the     */
 
191
/*   meshes you can generate with Triangle.  It also reduces the likelihood  */
 
192
/*   of a floating exception due to overflow.  Finally, it is much faster    */
 
193
/*   than single precision on 64-bit architectures like the DEC Alpha.  I    */
 
194
/*   recommend double precision unless you want to generate a mesh for which */
 
195
/*   you do not have enough memory.                                          */
 
196
 
 
197
/* #define SINGLE */
 
198
 
 
199
#ifdef SINGLE
 
200
#define REAL float
 
201
#else /* not SINGLE */
 
202
#define REAL double
 
203
#endif /* not SINGLE */
 
204
 
 
205
/* If yours is not a Unix system, define the NO_TIMER compiler switch to     */
 
206
/*   remove the Unix-specific timing code.                                   */
 
207
 
 
208
 #define NO_TIMER 
 
209
 
 
210
/* To insert lots of self-checks for internal errors, define the SELF_CHECK  */
 
211
/*   symbol.  This will slow down the program significantly.  It is best to  */
 
212
/*   define the symbol using the -DSELF_CHECK compiler switch, but you could */
 
213
/*   write "#define SELF_CHECK" below.  If you are modifying this code, I    */
 
214
/*   recommend you turn self-checks on.                                      */
 
215
 
 
216
/* #define SELF_CHECK */
 
217
 
 
218
/* To compile Triangle as a callable object library (triangle.o), define the */
 
219
/*   TRILIBRARY symbol.  Read the file triangle.h for details on how to call */
 
220
/*   the procedure triangulate() that results.                               */
 
221
 
 
222
 #define TRILIBRARY 
 
223
 
 
224
/* It is possible to generate a smaller version of Triangle using one or     */
 
225
/*   both of the following symbols.  Define the REDUCED symbol to eliminate  */
 
226
/*   all features that are primarily of research interest; specifically, the */
 
227
/*   -i, -F, -s, and -C switches.  Define the CDT_ONLY symbol to eliminate   */
 
228
/*   all meshing algorithms above and beyond constrained Delaunay            */
 
229
/*   triangulation; specifically, the -r, -q, -a, -S, and -s switches.       */
 
230
/*   These reductions are most likely to be useful when generating an object */
 
231
/*   library (triangle.o) by defining the TRILIBRARY symbol.                 */
 
232
 
 
233
/* #define REDUCED */
 
234
/* #define CDT_ONLY */
 
235
 
 
236
/* On some machines, the exact arithmetic routines might be defeated by the  */
 
237
/*   use of internal extended precision floating-point registers.  Sometimes */
 
238
/*   this problem can be fixed by defining certain values to be volatile,    */
 
239
/*   thus forcing them to be stored to memory and rounded off.  This isn't   */
 
240
/*   a great solution, though, as it slows Triangle down.                    */
 
241
/*                                                                           */
 
242
/* To try this out, write "#define INEXACT volatile" below.  Normally,       */
 
243
/*   however, INEXACT should be defined to be nothing.  ("#define INEXACT".) */
 
244
 
 
245
#define INEXACT /* Nothing */
 
246
/* #define INEXACT volatile */
 
247
 
 
248
/* Maximum number of characters in a file name (including the null).         */
 
249
 
 
250
#define FILENAMESIZE 512
 
251
 
 
252
/* Maximum number of characters in a line read from a file (including the    */
 
253
/*   null).                                                                  */
 
254
 
 
255
#define INPUTLINESIZE 512
 
256
 
 
257
/* For efficiency, a variety of data structures are allocated in bulk.  The  */
 
258
/*   following constants determine how many of each structure is allocated   */
 
259
/*   at once.                                                                */
 
260
 
 
261
#define TRIPERBLOCK 4092           /* Number of triangles allocated at once. */
 
262
#define SHELLEPERBLOCK 508       /* Number of shell edges allocated at once. */
 
263
#define POINTPERBLOCK 4092            /* Number of points allocated at once. */
 
264
#define VIRUSPERBLOCK 1020   /* Number of virus triangles allocated at once. */
 
265
/* Number of encroached segments allocated at once. */
 
266
#define BADSEGMENTPERBLOCK 252
 
267
/* Number of skinny triangles allocated at once. */
 
268
#define BADTRIPERBLOCK 4092
 
269
/* Number of splay tree nodes allocated at once. */
 
270
#define SPLAYNODEPERBLOCK 508
 
271
 
 
272
/* The point marker DEADPOINT is an arbitrary number chosen large enough to  */
 
273
/*   (hopefully) not conflict with user boundary markers.  Make sure that it */
 
274
/*   is small enough to fit into your machine's integer size.                */
 
275
 
 
276
#define DEADPOINT -1073741824
 
277
 
 
278
/* The next line is used to outsmart some very stupid compilers.  If your    */
 
279
/*   compiler is smarter, feel free to replace the "int" with "void".        */
 
280
/*   Not that it matters.                                                    */
 
281
 
 
282
#define VOID int
 
283
 
 
284
/* Two constants for algorithms based on random sampling.  Both constants    */
 
285
/*   have been chosen empirically to optimize their respective algorithms.   */
 
286
 
 
287
/* Used for the point location scheme of Mucke, Saias, and Zhu, to decide    */
 
288
/*   how large a random sample of triangles to inspect.                      */
 
289
#define SAMPLEFACTOR 11
 
290
/* Used in Fortune's sweepline Delaunay algorithm to determine what fraction */
 
291
/*   of boundary edges should be maintained in the splay tree for point      */
 
292
/*   location on the front.                                                  */
 
293
#define SAMPLERATE 10
 
294
 
 
295
/* A number that speaks for itself, every kissable digit.                    */
 
296
 
 
297
#define PI 3.141592653589793238462643383279502884197169399375105820974944592308
 
298
 
 
299
/* Another fave.                                                             */
 
300
 
 
301
#define SQUAREROOTTWO 1.4142135623730950488016887242096980785696718753769480732
 
302
 
 
303
/* And here's one for those of you who are intimidated by math.              */
 
304
 
 
305
#define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
 
306
 
 
307
#include <stdio.h>
 
308
#include <string.h>
 
309
#include <math.h>
 
310
#ifndef NO_TIMER
 
311
#include <sys/time.h>
 
312
#endif /* NO_TIMER */
 
313
#ifdef TRILIBRARY
 
314
#include "triangle.h"
 
315
#endif /* TRILIBRARY */
 
316
 
 
317
/* The following obscenity seems to be necessary to ensure that this program */
 
318
/* will port to Dec Alphas running OSF/1, because their stdio.h file commits */
 
319
/* the unpardonable sin of including stdlib.h.  Hence, malloc(), free(), and */
 
320
/* exit() may or may not already be defined at this point.  I declare these  */
 
321
/* functions explicitly because some non-ANSI C compilers lack stdlib.h.     */
 
322
 
 
323
#ifndef _STDLIB_H_
 
324
extern void *malloc();
 
325
extern void free();
 
326
extern void exit();
 
327
extern double strtod();
 
328
extern long strtol();
 
329
#endif /* _STDLIB_H_ */
 
330
 
 
331
/* A few forward declarations.                                               */
 
332
 
 
333
typedef struct memorypool memorypool;
 
334
void poolrestart(memorypool *pool);
 
335
#ifndef TRILIBRARY
 
336
char *readline();
 
337
char *findfield();
 
338
#endif /* not TRILIBRARY */
 
339
 
 
340
/* Labels that signify whether a record consists primarily of pointers or of */
 
341
/*   floating-point words.  Used to make decisions about data alignment.     */
 
342
 
 
343
enum wordtype {POINTER, FLOATINGPOINT};
 
344
 
 
345
/* Labels that signify the result of point location.  The result of a        */
 
346
/*   search indicates that the point falls in the interior of a triangle, on */
 
347
/*   an edge, on a vertex, or outside the mesh.                              */
 
348
 
 
349
enum locateresult {INTRIANGLE, ONEDGE, ONVERTEX, OUTSIDE};
 
350
 
 
351
/* Labels that signify the result of site insertion.  The result indicates   */
 
352
/*   that the point was inserted with complete success, was inserted but     */
 
353
/*   encroaches on a segment, was not inserted because it lies on a segment, */
 
354
/*   or was not inserted because another point occupies the same location.   */
 
355
 
 
356
enum insertsiteresult {SUCCESSFULPOINT, ENCROACHINGPOINT, VIOLATINGPOINT,
 
357
                       DUPLICATEPOINT};
 
358
 
 
359
/* Labels that signify the result of direction finding.  The result          */
 
360
/*   indicates that a segment connecting the two query points falls within   */
 
361
/*   the direction triangle, along the left edge of the direction triangle,  */
 
362
/*   or along the right edge of the direction triangle.                      */
 
363
 
 
364
enum finddirectionresult {WITHIN, LEFTCOLLINEAR, RIGHTCOLLINEAR};
 
365
 
 
366
/* Labels that signify the result of the circumcenter computation routine.   */
 
367
/*   The return value indicates which edge of the triangle is shortest.      */
 
368
 
 
369
enum circumcenterresult {OPPOSITEORG, OPPOSITEDEST, OPPOSITEAPEX};
 
370
 
 
371
/*****************************************************************************/
 
372
/*                                                                           */
 
373
/*  The basic mesh data structures                                           */
 
374
/*                                                                           */
 
375
/*  There are three:  points, triangles, and shell edges (abbreviated        */
 
376
/*  `shelle').  These three data structures, linked by pointers, comprise    */
 
377
/*  the mesh.  A point simply represents a point in space and its properties.*/
 
378
/*  A triangle is a triangle.  A shell edge is a special data structure used */
 
379
/*  to represent impenetrable segments in the mesh (including the outer      */
 
380
/*  boundary, boundaries of holes, and internal boundaries separating two    */
 
381
/*  triangulated regions).  Shell edges represent boundaries defined by the  */
 
382
/*  user that triangles may not lie across.                                  */
 
383
/*                                                                           */
 
384
/*  A triangle consists of a list of three vertices, a list of three         */
 
385
/*  adjoining triangles, a list of three adjoining shell edges (when shell   */
 
386
/*  edges are used), an arbitrary number of optional user-defined floating-  */
 
387
/*  point attributes, and an optional area constraint.  The latter is an     */
 
388
/*  upper bound on the permissible area of each triangle in a region, used   */
 
389
/*  for mesh refinement.                                                     */
 
390
/*                                                                           */
 
391
/*  For a triangle on a boundary of the mesh, some or all of the neighboring */
 
392
/*  triangles may not be present.  For a triangle in the interior of the     */
 
393
/*  mesh, often no neighboring shell edges are present.  Such absent         */
 
394
/*  triangles and shell edges are never represented by NULL pointers; they   */
 
395
/*  are represented by two special records:  `dummytri', the triangle that   */
 
396
/*  fills "outer space", and `dummysh', the omnipresent shell edge.          */
 
397
/*  `dummytri' and `dummysh' are used for several reasons; for instance,     */
 
398
/*  they can be dereferenced and their contents examined without causing the */
 
399
/*  memory protection exception that would occur if NULL were dereferenced.  */
 
400
/*                                                                           */
 
401
/*  However, it is important to understand that a triangle includes other    */
 
402
/*  information as well.  The pointers to adjoining vertices, triangles, and */
 
403
/*  shell edges are ordered in a way that indicates their geometric relation */
 
404
/*  to each other.  Furthermore, each of these pointers contains orientation */
 
405
/*  information.  Each pointer to an adjoining triangle indicates which face */
 
406
/*  of that triangle is contacted.  Similarly, each pointer to an adjoining  */
 
407
/*  shell edge indicates which side of that shell edge is contacted, and how */
 
408
/*  the shell edge is oriented relative to the triangle.                     */
 
409
/*                                                                           */
 
410
/*  Shell edges are found abutting edges of triangles; either sandwiched     */
 
411
/*  between two triangles, or resting against one triangle on an exterior    */
 
412
/*  boundary or hole boundary.                                               */
 
413
/*                                                                           */
 
414
/*  A shell edge consists of a list of two vertices, a list of two           */
 
415
/*  adjoining shell edges, and a list of two adjoining triangles.  One of    */
 
416
/*  the two adjoining triangles may not be present (though there should      */
 
417
/*  always be one), and neighboring shell edges might not be present.        */
 
418
/*  Shell edges also store a user-defined integer "boundary marker".         */
 
419
/*  Typically, this integer is used to indicate what sort of boundary        */
 
420
/*  conditions are to be applied at that location in a finite element        */
 
421
/*  simulation.                                                              */
 
422
/*                                                                           */
 
423
/*  Like triangles, shell edges maintain information about the relative      */
 
424
/*  orientation of neighboring objects.                                      */
 
425
/*                                                                           */
 
426
/*  Points are relatively simple.  A point is a list of floating point       */
 
427
/*  numbers, starting with the x, and y coordinates, followed by an          */
 
428
/*  arbitrary number of optional user-defined floating-point attributes,     */
 
429
/*  followed by an integer boundary marker.  During the segment insertion    */
 
430
/*  phase, there is also a pointer from each point to a triangle that may    */
 
431
/*  contain it.  Each pointer is not always correct, but when one is, it     */
 
432
/*  speeds up segment insertion.  These pointers are assigned values once    */
 
433
/*  at the beginning of the segment insertion phase, and are not used or     */
 
434
/*  updated at any other time.  Edge swapping during segment insertion will  */
 
435
/*  render some of them incorrect.  Hence, don't rely upon them for          */
 
436
/*  anything.  For the most part, points do not have any information about   */
 
437
/*  what triangles or shell edges they are linked to.                        */
 
438
/*                                                                           */
 
439
/*****************************************************************************/
 
440
 
 
441
/*****************************************************************************/
 
442
/*                                                                           */
 
443
/*  Handles                                                                  */
 
444
/*                                                                           */
 
445
/*  The oriented triangle (`triedge') and oriented shell edge (`edge') data  */
 
446
/*  structures defined below do not themselves store any part of the mesh.   */
 
447
/*  The mesh itself is made of `triangle's, `shelle's, and `point's.         */
 
448
/*                                                                           */
 
449
/*  Oriented triangles and oriented shell edges will usually be referred to  */
 
450
/*  as "handles".  A handle is essentially a pointer into the mesh; it       */
 
451
/*  allows you to "hold" one particular part of the mesh.  Handles are used  */
 
452
/*  to specify the regions in which one is traversing and modifying the mesh.*/
 
453
/*  A single `triangle' may be held by many handles, or none at all.  (The   */
 
454
/*  latter case is not a memory leak, because the triangle is still          */
 
455
/*  connected to other triangles in the mesh.)                               */
 
456
/*                                                                           */
 
457
/*  A `triedge' is a handle that holds a triangle.  It holds a specific side */
 
458
/*  of the triangle.  An `edge' is a handle that holds a shell edge.  It     */
 
459
/*  holds either the left or right side of the edge.                         */
 
460
/*                                                                           */
 
461
/*  Navigation about the mesh is accomplished through a set of mesh          */
 
462
/*  manipulation primitives, further below.  Many of these primitives take   */
 
463
/*  a handle and produce a new handle that holds the mesh near the first     */
 
464
/*  handle.  Other primitives take two handles and glue the corresponding    */
 
465
/*  parts of the mesh together.  The exact position of the handles is        */
 
466
/*  important.  For instance, when two triangles are glued together by the   */
 
467
/*  bond() primitive, they are glued by the sides on which the handles lie.  */
 
468
/*                                                                           */
 
469
/*  Because points have no information about which triangles they are        */
 
470
/*  attached to, I commonly represent a point by use of a handle whose       */
 
471
/*  origin is the point.  A single handle can simultaneously represent a     */
 
472
/*  triangle, an edge, and a point.                                          */
 
473
/*                                                                           */
 
474
/*****************************************************************************/
 
475
 
 
476
/* The triangle data structure.  Each triangle contains three pointers to    */
 
477
/*   adjoining triangles, plus three pointers to vertex points, plus three   */
 
478
/*   pointers to shell edges (defined below; these pointers are usually      */
 
479
/*   `dummysh').  It may or may not also contain user-defined attributes     */
 
480
/*   and/or a floating-point "area constraint".  It may also contain extra   */
 
481
/*   pointers for nodes, when the user asks for high-order elements.         */
 
482
/*   Because the size and structure of a `triangle' is not decided until     */
 
483
/*   runtime, I haven't simply defined the type `triangle' to be a struct.   */
 
484
 
 
485
typedef REAL **triangle;            /* Really:  typedef triangle *triangle   */
 
486
 
 
487
/* An oriented triangle:  includes a pointer to a triangle and orientation.  */
 
488
/*   The orientation denotes an edge of the triangle.  Hence, there are      */
 
489
/*   three possible orientations.  By convention, each edge is always        */
 
490
/*   directed to point counterclockwise about the corresponding triangle.    */
 
491
 
 
492
typedef struct triedge {
 
493
  triangle *tri;
 
494
  int orient;                                         /* Ranges from 0 to 2. */
 
495
} triedge;
 
496
 
 
497
/* The shell data structure.  Each shell edge contains two pointers to       */
 
498
/*   adjoining shell edges, plus two pointers to vertex points, plus two     */
 
499
/*   pointers to adjoining triangles, plus one shell marker.                 */
 
500
 
 
501
typedef REAL **shelle;                  /* Really:  typedef shelle *shelle   */
 
502
 
 
503
/* An oriented shell edge:  includes a pointer to a shell edge and an        */
 
504
/*   orientation.  The orientation denotes a side of the edge.  Hence, there */
 
505
/*   are two possible orientations.  By convention, the edge is always       */
 
506
/*   directed so that the "side" denoted is the right side of the edge.      */
 
507
 
 
508
struct edge {
 
509
  shelle *sh;
 
510
  int shorient;                                       /* Ranges from 0 to 1. */
 
511
};
 
512
 
 
513
/* The point data structure.  Each point is actually an array of REALs.      */
 
514
/*   The number of REALs is unknown until runtime.  An integer boundary      */
 
515
/*   marker, and sometimes a pointer to a triangle, is appended after the    */
 
516
/*   REALs.                                                                  */
 
517
 
 
518
typedef REAL *point;
 
519
 
 
520
/* A queue used to store encroached segments.  Each segment's vertices are   */
 
521
/*   stored so that one can check whether a segment is still the same.       */
 
522
 
 
523
struct badsegment {
 
524
  struct edge encsegment;                          /* An encroached segment. */
 
525
  point segorg, segdest;                                /* The two vertices. */
 
526
  struct badsegment *nextsegment;     /* Pointer to next encroached segment. */
 
527
};
 
528
 
 
529
/* A queue used to store bad triangles.  The key is the square of the cosine */
 
530
/*   of the smallest angle of the triangle.  Each triangle's vertices are    */
 
531
/*   stored so that one can check whether a triangle is still the same.      */
 
532
 
 
533
struct badface {
 
534
  struct triedge badfacetri;                              /* A bad triangle. */
 
535
  REAL key;                             /* cos^2 of smallest (apical) angle. */
 
536
  point faceorg, facedest, faceapex;                  /* The three vertices. */
 
537
  struct badface *nextface;                 /* Pointer to next bad triangle. */
 
538
};
 
539
 
 
540
/* A node in a heap used to store events for the sweepline Delaunay          */
 
541
/*   algorithm.  Nodes do not point directly to their parents or children in */
 
542
/*   the heap.  Instead, each node knows its position in the heap, and can   */
 
543
/*   look up its parent and children in a separate array.  The `eventptr'    */
 
544
/*   points either to a `point' or to a triangle (in encoded format, so that */
 
545
/*   an orientation is included).  In the latter case, the origin of the     */
 
546
/*   oriented triangle is the apex of a "circle event" of the sweepline      */
 
547
/*   algorithm.  To distinguish site events from circle events, all circle   */
 
548
/*   events are given an invalid (smaller than `xmin') x-coordinate `xkey'.  */
 
549
 
 
550
struct event {
 
551
  REAL xkey, ykey;                              /* Coordinates of the event. */
 
552
  VOID *eventptr;       /* Can be a point or the location of a circle event. */
 
553
  int heapposition;              /* Marks this event's position in the heap. */
 
554
};
 
555
 
 
556
/* A node in the splay tree.  Each node holds an oriented ghost triangle     */
 
557
/*   that represents a boundary edge of the growing triangulation.  When a   */
 
558
/*   circle event covers two boundary edges with a triangle, so that they    */
 
559
/*   are no longer boundary edges, those edges are not immediately deleted   */
 
560
/*   from the tree; rather, they are lazily deleted when they are next       */
 
561
/*   encountered.  (Since only a random sample of boundary edges are kept    */
 
562
/*   in the tree, lazy deletion is faster.)  `keydest' is used to verify     */
 
563
/*   that a triangle is still the same as when it entered the splay tree; if */
 
564
/*   it has been rotated (due to a circle event), it no longer represents a  */
 
565
/*   boundary edge and should be deleted.                                    */
 
566
 
 
567
struct splaynode {
 
568
  struct triedge keyedge;                  /* Lprev of an edge on the front. */
 
569
  point keydest;            /* Used to verify that splay node is still live. */
 
570
  struct splaynode *lchild, *rchild;              /* Children in splay tree. */
 
571
};
 
572
 
 
573
/* A type used to allocate memory.  firstblock is the first block of items.  */
 
574
/*   nowblock is the block from which items are currently being allocated.   */
 
575
/*   nextitem points to the next slab of free memory for an item.            */
 
576
/*   deaditemstack is the head of a linked list (stack) of deallocated items */
 
577
/*   that can be recycled.  unallocateditems is the number of items that     */
 
578
/*   remain to be allocated from nowblock.                                   */
 
579
/*                                                                           */
 
580
/* Traversal is the process of walking through the entire list of items, and */
 
581
/*   is separate from allocation.  Note that a traversal will visit items on */
 
582
/*   the "deaditemstack" stack as well as live items.  pathblock points to   */
 
583
/*   the block currently being traversed.  pathitem points to the next item  */
 
584
/*   to be traversed.  pathitemsleft is the number of items that remain to   */
 
585
/*   be traversed in pathblock.                                              */
 
586
/*                                                                           */
 
587
/* itemwordtype is set to POINTER or FLOATINGPOINT, and is used to suggest   */
 
588
/*   what sort of word the record is primarily made up of.  alignbytes       */
 
589
/*   determines how new records should be aligned in memory.  itembytes and  */
 
590
/*   itemwords are the length of a record in bytes (after rounding up) and   */
 
591
/*   words.  itemsperblock is the number of items allocated at once in a     */
 
592
/*   single block.  items is the number of currently allocated items.        */
 
593
/*   maxitems is the maximum number of items that have been allocated at     */
 
594
/*   once; it is the current number of items plus the number of records kept */
 
595
/*   on deaditemstack.                                                       */
 
596
 
 
597
struct memorypool {
 
598
  VOID **firstblock, **nowblock;
 
599
  VOID *nextitem;
 
600
  VOID *deaditemstack;
 
601
  VOID **pathblock;
 
602
  VOID *pathitem;
 
603
  enum wordtype itemwordtype;
 
604
  int alignbytes;
 
605
  int itembytes, itemwords;
 
606
  int itemsperblock;
 
607
  long items, maxitems;
 
608
  int unallocateditems;
 
609
  int pathitemsleft;
 
610
};
 
611
 
 
612
/* Variables used to allocate memory for triangles, shell edges, points,     */
 
613
/*   viri (triangles being eaten), bad (encroached) segments, bad (skinny    */
 
614
/*   or too large) triangles, and splay tree nodes.                          */
 
615
 
 
616
struct memorypool triangles;
 
617
struct memorypool shelles;
 
618
struct memorypool points;
 
619
struct memorypool viri;
 
620
struct memorypool badsegments;
 
621
struct memorypool badtriangles;
 
622
struct memorypool splaynodes;
 
623
 
 
624
/* Variables that maintain the bad triangle queues.  The tails are pointers  */
 
625
/*   to the pointers that have to be filled in to enqueue an item.           */
 
626
 
 
627
struct badface *queuefront[64];
 
628
struct badface **queuetail[64];
 
629
 
 
630
REAL xmin, xmax, ymin, ymax;                              /* x and y bounds. */
 
631
REAL xminextreme;        /* Nonexistent x value used as a flag in sweepline. */
 
632
int inpoints;                                     /* Number of input points. */
 
633
int inelements;                                /* Number of input triangles. */
 
634
int insegments;                                 /* Number of input segments. */
 
635
int holes;                                         /* Number of input holes. */
 
636
int regions;                                     /* Number of input regions. */
 
637
long edges;                                       /* Number of output edges. */
 
638
int mesh_dim;                                  /* Dimension (ought to be 2). */
 
639
int nextras;                              /* Number of attributes per point. */
 
640
int eextras;                           /* Number of attributes per triangle. */
 
641
long hullsize;                            /* Number of edges of convex hull. */
 
642
int triwords;                                   /* Total words per triangle. */
 
643
int shwords;                                  /* Total words per shell edge. */
 
644
int pointmarkindex;             /* Index to find boundary marker of a point. */
 
645
int point2triindex;         /* Index to find a triangle adjacent to a point. */
 
646
int highorderindex;    /* Index to find extra nodes for high-order elements. */
 
647
int elemattribindex;              /* Index to find attributes of a triangle. */
 
648
int areaboundindex;               /* Index to find area bound of a triangle. */
 
649
int checksegments;           /* Are there segments in the triangulation yet? */
 
650
int readnodefile;                             /* Has a .node file been read? */
 
651
long samples;                /* Number of random samples for point location. */
 
652
unsigned long randomseed;                     /* Current random number seed. */
 
653
 
 
654
REAL splitter;       /* Used to split REAL factors for exact multiplication. */
 
655
REAL epsilon;                             /* Floating-point machine epsilon. */
 
656
REAL resulterrbound;
 
657
REAL ccwerrboundA, ccwerrboundB, ccwerrboundC;
 
658
REAL iccerrboundA, iccerrboundB, iccerrboundC;
 
659
 
 
660
long incirclecount;                   /* Number of incircle tests performed. */
 
661
long counterclockcount;       /* Number of counterclockwise tests performed. */
 
662
long hyperbolacount;        /* Number of right-of-hyperbola tests performed. */
 
663
long circumcentercount;    /* Number of circumcenter calculations performed. */
 
664
long circletopcount;         /* Number of circle top calculations performed. */
 
665
 
 
666
/* Switches for the triangulator.                                            */
 
667
/*   poly: -p switch.  refine: -r switch.                                    */
 
668
/*   quality: -q switch.                                                     */
 
669
/*     minangle: minimum angle bound, specified after -q switch.             */
 
670
/*     goodangle: cosine squared of minangle.                                */
 
671
/*   vararea: -a switch without number.                                      */
 
672
/*   fixedarea: -a switch with number.                                       */
 
673
/*     maxarea: maximum area bound, specified after -a switch.               */
 
674
/*   regionattrib: -A switch.  convex: -c switch.                            */
 
675
/*   firstnumber: inverse of -z switch.  All items are numbered starting     */
 
676
/*     from firstnumber.                                                     */
 
677
/*   edgesout: -e switch.  voronoi: -v switch.                               */
 
678
/*   neighbors: -n switch.  geomview: -g switch.                             */
 
679
/*   nobound: -B switch.  nopolywritten: -P switch.                          */
 
680
/*   nonodewritten: -N switch.  noelewritten: -E switch.                     */
 
681
/*   noiterationnum: -I switch.  noholes: -O switch.                         */
 
682
/*   noexact: -X switch.                                                     */
 
683
/*   order: element order, specified after -o switch.                        */
 
684
/*   nobisect: count of how often -Y switch is selected.                     */
 
685
/*   steiner: maximum number of Steiner points, specified after -S switch.   */
 
686
/*     steinerleft: number of Steiner points not yet used.                   */
 
687
/*   incremental: -i switch.  sweepline: -F switch.                          */
 
688
/*   dwyer: inverse of -l switch.                                            */
 
689
/*   splitseg: -s switch.                                                    */
 
690
/*   docheck: -C switch.                                                     */
 
691
/*   quiet: -Q switch.  verbose: count of how often -V switch is selected.   */
 
692
/*   useshelles: -p, -r, -q, or -c switch; determines whether shell edges    */
 
693
/*     are used at all.                                                      */
 
694
/*                                                                           */
 
695
/* Read the instructions to find out the meaning of these switches.          */
 
696
 
 
697
int poly, refine, quality, vararea, fixedarea, regionattrib, convex;
 
698
int firstnumber;
 
699
int edgesout, voronoi, neighbors, geomview;
 
700
int nobound, nopolywritten, nonodewritten, noelewritten, noiterationnum;
 
701
int noholes, noexact;
 
702
int incremental, sweepline, dwyer;
 
703
int splitseg;
 
704
int docheck;
 
705
int quiet, verbose;
 
706
int useshelles;
 
707
int order;
 
708
int nobisect;
 
709
int steiner, steinerleft;
 
710
REAL minangle, goodangle;
 
711
REAL maxarea;
 
712
 
 
713
/* Variables for file names.                                                 */
 
714
 
 
715
#ifndef TRILIBRARY
 
716
char innodefilename[FILENAMESIZE];
 
717
char inelefilename[FILENAMESIZE];
 
718
char inpolyfilename[FILENAMESIZE];
 
719
char areafilename[FILENAMESIZE];
 
720
char outnodefilename[FILENAMESIZE];
 
721
char outelefilename[FILENAMESIZE];
 
722
char outpolyfilename[FILENAMESIZE];
 
723
char edgefilename[FILENAMESIZE];
 
724
char vnodefilename[FILENAMESIZE];
 
725
char vedgefilename[FILENAMESIZE];
 
726
char neighborfilename[FILENAMESIZE];
 
727
char offfilename[FILENAMESIZE];
 
728
#endif /* not TRILIBRARY */
 
729
 
 
730
/* Triangular bounding box points.                                           */
 
731
 
 
732
point infpoint1, infpoint2, infpoint3;
 
733
 
 
734
/* Pointer to the `triangle' that occupies all of "outer space".             */
 
735
 
 
736
triangle *dummytri;
 
737
triangle *dummytribase;      /* Keep base address so we can free() it later. */
 
738
 
 
739
/* Pointer to the omnipresent shell edge.  Referenced by any triangle or     */
 
740
/*   shell edge that isn't really connected to a shell edge at that          */
 
741
/*   location.                                                               */
 
742
 
 
743
shelle *dummysh;
 
744
shelle *dummyshbase;         /* Keep base address so we can free() it later. */
 
745
 
 
746
/* Pointer to a recently visited triangle.  Improves point location if       */
 
747
/*   proximate points are inserted sequentially.                             */
 
748
 
 
749
struct triedge recenttri;
 
750
 
 
751
/*****************************************************************************/
 
752
/*                                                                           */
 
753
/*  Mesh manipulation primitives.  Each triangle contains three pointers to  */
 
754
/*  other triangles, with orientations.  Each pointer points not to the      */
 
755
/*  first byte of a triangle, but to one of the first three bytes of a       */
 
756
/*  triangle.  It is necessary to extract both the triangle itself and the   */
 
757
/*  orientation.  To save memory, I keep both pieces of information in one   */
 
758
/*  pointer.  To make this possible, I assume that all triangles are aligned */
 
759
/*  to four-byte boundaries.  The `decode' routine below decodes a pointer,  */
 
760
/*  extracting an orientation (in the range 0 to 2) and a pointer to the     */
 
761
/*  beginning of a triangle.  The `encode' routine compresses a pointer to a */
 
762
/*  triangle and an orientation into a single pointer.  My assumptions that  */
 
763
/*  triangles are four-byte-aligned and that the `unsigned long' type is     */
 
764
/*  long enough to hold a pointer are two of the few kludges in this program.*/
 
765
/*                                                                           */
 
766
/*  Shell edges are manipulated similarly.  A pointer to a shell edge        */
 
767
/*  carries both an address and an orientation in the range 0 to 1.          */
 
768
/*                                                                           */
 
769
/*  The other primitives take an oriented triangle or oriented shell edge,   */
 
770
/*  and return an oriented triangle or oriented shell edge or point; or they */
 
771
/*  change the connections in the data structure.                            */
 
772
/*                                                                           */
 
773
/*****************************************************************************/
 
774
 
 
775
/********* Mesh manipulation primitives begin here                   *********/
 
776
/**                                                                         **/
 
777
/**                                                                         **/
 
778
 
 
779
/* Fast lookup arrays to speed some of the mesh manipulation primitives.     */
 
780
 
 
781
int plus1mod3[3] = {1, 2, 0};
 
782
int minus1mod3[3] = {2, 0, 1};
 
783
 
 
784
/********* Primitives for triangles                                  *********/
 
785
/*                                                                           */
 
786
/*                                                                           */
 
787
 
 
788
/* decode() converts a pointer to an oriented triangle.  The orientation is  */
 
789
/*   extracted from the two least significant bits of the pointer.           */
 
790
 
 
791
#define decode(ptr, triedge)                                                  \
 
792
  (triedge).orient = (int) ((unsigned long) (ptr) & (unsigned long) 3l);      \
 
793
  (triedge).tri = (triangle *)                                                \
 
794
                  ((unsigned long) (ptr) ^ (unsigned long) (triedge).orient)
 
795
 
 
796
/* encode() compresses an oriented triangle into a single pointer.  It       */
 
797
/*   relies on the assumption that all triangles are aligned to four-byte    */
 
798
/*   boundaries, so the two least significant bits of (triedge).tri are zero.*/
 
799
 
 
800
#define encode(triedge)                                                       \
 
801
  (triangle) ((unsigned long) (triedge).tri | (unsigned long) (triedge).orient)
 
802
 
 
803
/* The following edge manipulation primitives are all described by Guibas    */
 
804
/*   and Stolfi.  However, they use an edge-based data structure, whereas I  */
 
805
/*   am using a triangle-based data structure.                               */
 
806
 
 
807
/* sym() finds the abutting triangle, on the same edge.  Note that the       */
 
808
/*   edge direction is necessarily reversed, because triangle/edge handles   */
 
809
/*   are always directed counterclockwise around the triangle.               */
 
810
 
 
811
#define sym(triedge1, triedge2)                                               \
 
812
  ptr = (triedge1).tri[(triedge1).orient];                                    \
 
813
  decode(ptr, triedge2);
 
814
 
 
815
#define symself(triedge)                                                      \
 
816
  ptr = (triedge).tri[(triedge).orient];                                      \
 
817
  decode(ptr, triedge);
 
818
 
 
819
/* lnext() finds the next edge (counterclockwise) of a triangle.             */
 
820
 
 
821
#define lnext(triedge1, triedge2)                                             \
 
822
  (triedge2).tri = (triedge1).tri;                                            \
 
823
  (triedge2).orient = plus1mod3[(triedge1).orient]
 
824
 
 
825
#define lnextself(triedge)                                                    \
 
826
  (triedge).orient = plus1mod3[(triedge).orient]
 
827
 
 
828
/* lprev() finds the previous edge (clockwise) of a triangle.                */
 
829
 
 
830
#define lprev(triedge1, triedge2)                                             \
 
831
  (triedge2).tri = (triedge1).tri;                                            \
 
832
  (triedge2).orient = minus1mod3[(triedge1).orient]
 
833
 
 
834
#define lprevself(triedge)                                                    \
 
835
  (triedge).orient = minus1mod3[(triedge).orient]
 
836
 
 
837
/* onext() spins counterclockwise around a point; that is, it finds the next */
 
838
/*   edge with the same origin in the counterclockwise direction.  This edge */
 
839
/*   will be part of a different triangle.                                   */
 
840
 
 
841
#define onext(triedge1, triedge2)                                             \
 
842
  lprev(triedge1, triedge2);                                                  \
 
843
  symself(triedge2);
 
844
 
 
845
#define onextself(triedge)                                                    \
 
846
  lprevself(triedge);                                                         \
 
847
  symself(triedge);
 
848
 
 
849
/* oprev() spins clockwise around a point; that is, it finds the next edge   */
 
850
/*   with the same origin in the clockwise direction.  This edge will be     */
 
851
/*   part of a different triangle.                                           */
 
852
 
 
853
#define oprev(triedge1, triedge2)                                             \
 
854
  sym(triedge1, triedge2);                                                    \
 
855
  lnextself(triedge2);
 
856
 
 
857
#define oprevself(triedge)                                                    \
 
858
  symself(triedge);                                                           \
 
859
  lnextself(triedge);
 
860
 
 
861
/* dnext() spins counterclockwise around a point; that is, it finds the next */
 
862
/*   edge with the same destination in the counterclockwise direction.  This */
 
863
/*   edge will be part of a different triangle.                              */
 
864
 
 
865
#define dnext(triedge1, triedge2)                                             \
 
866
  sym(triedge1, triedge2);                                                    \
 
867
  lprevself(triedge2);
 
868
 
 
869
#define dnextself(triedge)                                                    \
 
870
  symself(triedge);                                                           \
 
871
  lprevself(triedge);
 
872
 
 
873
/* dprev() spins clockwise around a point; that is, it finds the next edge   */
 
874
/*   with the same destination in the clockwise direction.  This edge will   */
 
875
/*   be part of a different triangle.                                        */
 
876
 
 
877
#define dprev(triedge1, triedge2)                                             \
 
878
  lnext(triedge1, triedge2);                                                  \
 
879
  symself(triedge2);
 
880
 
 
881
#define dprevself(triedge)                                                    \
 
882
  lnextself(triedge);                                                         \
 
883
  symself(triedge);
 
884
 
 
885
/* rnext() moves one edge counterclockwise about the adjacent triangle.      */
 
886
/*   (It's best understood by reading Guibas and Stolfi.  It involves        */
 
887
/*   changing triangles twice.)                                              */
 
888
 
 
889
#define rnext(triedge1, triedge2)                                             \
 
890
  sym(triedge1, triedge2);                                                    \
 
891
  lnextself(triedge2);                                                        \
 
892
  symself(triedge2);
 
893
 
 
894
#define rnextself(triedge)                                                    \
 
895
  symself(triedge);                                                           \
 
896
  lnextself(triedge);                                                         \
 
897
  symself(triedge);
 
898
 
 
899
/* rnext() moves one edge clockwise about the adjacent triangle.             */
 
900
/*   (It's best understood by reading Guibas and Stolfi.  It involves        */
 
901
/*   changing triangles twice.)                                              */
 
902
 
 
903
#define rprev(triedge1, triedge2)                                             \
 
904
  sym(triedge1, triedge2);                                                    \
 
905
  lprevself(triedge2);                                                        \
 
906
  symself(triedge2);
 
907
 
 
908
#define rprevself(triedge)                                                    \
 
909
  symself(triedge);                                                           \
 
910
  lprevself(triedge);                                                         \
 
911
  symself(triedge);
 
912
 
 
913
/* These primitives determine or set the origin, destination, or apex of a   */
 
914
/* triangle.                                                                 */
 
915
 
 
916
#define org(triedge, pointptr)                                                \
 
917
  pointptr = (point) (triedge).tri[plus1mod3[(triedge).orient] + 3]
 
918
 
 
919
#define dest(triedge, pointptr)                                               \
 
920
  pointptr = (point) (triedge).tri[minus1mod3[(triedge).orient] + 3]
 
921
 
 
922
#define apex(triedge, pointptr)                                               \
 
923
  pointptr = (point) (triedge).tri[(triedge).orient + 3]
 
924
 
 
925
#define setorg(triedge, pointptr)                                             \
 
926
  (triedge).tri[plus1mod3[(triedge).orient] + 3] = (triangle) pointptr
 
927
 
 
928
#define setdest(triedge, pointptr)                                            \
 
929
  (triedge).tri[minus1mod3[(triedge).orient] + 3] = (triangle) pointptr
 
930
 
 
931
#define setapex(triedge, pointptr)                                            \
 
932
  (triedge).tri[(triedge).orient + 3] = (triangle) pointptr
 
933
 
 
934
#define setvertices2null(triedge)                                             \
 
935
  (triedge).tri[3] = (triangle) NULL;                                         \
 
936
  (triedge).tri[4] = (triangle) NULL;                                         \
 
937
  (triedge).tri[5] = (triangle) NULL;
 
938
 
 
939
/* Bond two triangles together.                                              */
 
940
 
 
941
#define bond(triedge1, triedge2)                                              \
 
942
  (triedge1).tri[(triedge1).orient] = encode(triedge2);                       \
 
943
  (triedge2).tri[(triedge2).orient] = encode(triedge1)
 
944
 
 
945
/* Dissolve a bond (from one side).  Note that the other triangle will still */
 
946
/*   think it's connected to this triangle.  Usually, however, the other     */
 
947
/*   triangle is being deleted entirely, or bonded to another triangle, so   */
 
948
/*   it doesn't matter.                                                      */
 
949
 
 
950
#define dissolve(triedge)                                                     \
 
951
  (triedge).tri[(triedge).orient] = (triangle) dummytri
 
952
 
 
953
/* Copy a triangle/edge handle.                                              */
 
954
 
 
955
#define triedgecopy(triedge1, triedge2)                                       \
 
956
  (triedge2).tri = (triedge1).tri;                                            \
 
957
  (triedge2).orient = (triedge1).orient
 
958
 
 
959
/* Test for equality of triangle/edge handles.                               */
 
960
 
 
961
#define triedgeequal(triedge1, triedge2)                                      \
 
962
  (((triedge1).tri == (triedge2).tri) &&                                      \
 
963
   ((triedge1).orient == (triedge2).orient))
 
964
 
 
965
/* Primitives to infect or cure a triangle with the virus.  These rely on    */
 
966
/*   the assumption that all shell edges are aligned to four-byte boundaries.*/
 
967
 
 
968
#define infect(triedge)                                                       \
 
969
  (triedge).tri[6] = (triangle)                                               \
 
970
                     ((unsigned long) (triedge).tri[6] | (unsigned long) 2l)
 
971
 
 
972
#define uninfect(triedge)                                                     \
 
973
  (triedge).tri[6] = (triangle)                                               \
 
974
                     ((unsigned long) (triedge).tri[6] & ~ (unsigned long) 2l)
 
975
 
 
976
/* Test a triangle for viral infection.                                      */
 
977
 
 
978
#define infected(triedge)                                                     \
 
979
  (((unsigned long) (triedge).tri[6] & (unsigned long) 2l) != 0)
 
980
 
 
981
/* Check or set a triangle's attributes.                                     */
 
982
 
 
983
#define elemattribute(triedge, attnum)                                        \
 
984
  ((REAL *) (triedge).tri)[elemattribindex + (attnum)]
 
985
 
 
986
#define setelemattribute(triedge, attnum, value)                              \
 
987
  ((REAL *) (triedge).tri)[elemattribindex + (attnum)] = value
 
988
 
 
989
/* Check or set a triangle's maximum area bound.                             */
 
990
 
 
991
#define areabound(triedge)  ((REAL *) (triedge).tri)[areaboundindex]
 
992
 
 
993
#define setareabound(triedge, value)                                          \
 
994
  ((REAL *) (triedge).tri)[areaboundindex] = value
 
995
 
 
996
/********* Primitives for shell edges                                *********/
 
997
/*                                                                           */
 
998
/*                                                                           */
 
999
 
 
1000
/* sdecode() converts a pointer to an oriented shell edge.  The orientation  */
 
1001
/*   is extracted from the least significant bit of the pointer.  The two    */
 
1002
/*   least significant bits (one for orientation, one for viral infection)   */
 
1003
/*   are masked out to produce the real pointer.                             */
 
1004
 
 
1005
#define sdecode(sptr, edge)                                                   \
 
1006
  (edge).shorient = (int) ((unsigned long) (sptr) & (unsigned long) 1l);      \
 
1007
  (edge).sh = (shelle *)                                                      \
 
1008
              ((unsigned long) (sptr) & ~ (unsigned long) 3l)
 
1009
 
 
1010
/* sencode() compresses an oriented shell edge into a single pointer.  It    */
 
1011
/*   relies on the assumption that all shell edges are aligned to two-byte   */
 
1012
/*   boundaries, so the least significant bit of (edge).sh is zero.          */
 
1013
 
 
1014
#define sencode(edge)                                                         \
 
1015
  (shelle) ((unsigned long) (edge).sh | (unsigned long) (edge).shorient)
 
1016
 
 
1017
/* ssym() toggles the orientation of a shell edge.                           */
 
1018
 
 
1019
#define ssym(edge1, edge2)                                                    \
 
1020
  (edge2).sh = (edge1).sh;                                                    \
 
1021
  (edge2).shorient = 1 - (edge1).shorient
 
1022
 
 
1023
#define ssymself(edge)                                                        \
 
1024
  (edge).shorient = 1 - (edge).shorient
 
1025
 
 
1026
/* spivot() finds the other shell edge (from the same segment) that shares   */
 
1027
/*   the same origin.                                                        */
 
1028
 
 
1029
#define spivot(edge1, edge2)                                                  \
 
1030
  sptr = (edge1).sh[(edge1).shorient];                                        \
 
1031
  sdecode(sptr, edge2)
 
1032
 
 
1033
#define spivotself(edge)                                                      \
 
1034
  sptr = (edge).sh[(edge).shorient];                                          \
 
1035
  sdecode(sptr, edge)
 
1036
 
 
1037
/* snext() finds the next shell edge (from the same segment) in sequence;    */
 
1038
/*   one whose origin is the input shell edge's destination.                 */
 
1039
 
 
1040
#define snext(edge1, edge2)                                                   \
 
1041
  sptr = (edge1).sh[1 - (edge1).shorient];                                    \
 
1042
  sdecode(sptr, edge2)
 
1043
 
 
1044
#define snextself(edge)                                                       \
 
1045
  sptr = (edge).sh[1 - (edge).shorient];                                      \
 
1046
  sdecode(sptr, edge)
 
1047
 
 
1048
/* These primitives determine or set the origin or destination of a shell    */
 
1049
/*   edge.                                                                   */
 
1050
 
 
1051
#define sorg(edge, pointptr)                                                  \
 
1052
  pointptr = (point) (edge).sh[2 + (edge).shorient]
 
1053
 
 
1054
#define sdest(edge, pointptr)                                                 \
 
1055
  pointptr = (point) (edge).sh[3 - (edge).shorient]
 
1056
 
 
1057
#define setsorg(edge, pointptr)                                               \
 
1058
  (edge).sh[2 + (edge).shorient] = (shelle) pointptr
 
1059
 
 
1060
#define setsdest(edge, pointptr)                                              \
 
1061
  (edge).sh[3 - (edge).shorient] = (shelle) pointptr
 
1062
 
 
1063
/* These primitives read or set a shell marker.  Shell markers are used to   */
 
1064
/*   hold user boundary information.                                         */
 
1065
 
 
1066
#define mark(edge)  (* (int *) ((edge).sh + 6))
 
1067
 
 
1068
#define setmark(edge, value)                                                  \
 
1069
  * (int *) ((edge).sh + 6) = value
 
1070
 
 
1071
/* Bond two shell edges together.                                            */
 
1072
 
 
1073
#define sbond(edge1, edge2)                                                   \
 
1074
  (edge1).sh[(edge1).shorient] = sencode(edge2);                              \
 
1075
  (edge2).sh[(edge2).shorient] = sencode(edge1)
 
1076
 
 
1077
/* Dissolve a shell edge bond (from one side).  Note that the other shell    */
 
1078
/*   edge will still think it's connected to this shell edge.                */
 
1079
 
 
1080
#define sdissolve(edge)                                                       \
 
1081
  (edge).sh[(edge).shorient] = (shelle) dummysh
 
1082
 
 
1083
/* Copy a shell edge.                                                        */
 
1084
 
 
1085
#define shellecopy(edge1, edge2)                                              \
 
1086
  (edge2).sh = (edge1).sh;                                                    \
 
1087
  (edge2).shorient = (edge1).shorient
 
1088
 
 
1089
/* Test for equality of shell edges.                                         */
 
1090
 
 
1091
#define shelleequal(edge1, edge2)                                             \
 
1092
  (((edge1).sh == (edge2).sh) &&                                              \
 
1093
   ((edge1).shorient == (edge2).shorient))
 
1094
 
 
1095
/********* Primitives for interacting triangles and shell edges      *********/
 
1096
/*                                                                           */
 
1097
/*                                                                           */
 
1098
 
 
1099
/* tspivot() finds a shell edge abutting a triangle.                         */
 
1100
 
 
1101
#define tspivot(triedge, edge)                                                \
 
1102
  sptr = (shelle) (triedge).tri[6 + (triedge).orient];                        \
 
1103
  sdecode(sptr, edge)
 
1104
 
 
1105
/* stpivot() finds a triangle abutting a shell edge.  It requires that the   */
 
1106
/*   variable `ptr' of type `triangle' be defined.                           */
 
1107
 
 
1108
#define stpivot(edge, triedge)                                                \
 
1109
  ptr = (triangle) (edge).sh[4 + (edge).shorient];                            \
 
1110
  decode(ptr, triedge)
 
1111
 
 
1112
/* Bond a triangle to a shell edge.                                          */
 
1113
 
 
1114
#define tsbond(triedge, edge)                                                 \
 
1115
  (triedge).tri[6 + (triedge).orient] = (triangle) sencode(edge);             \
 
1116
  (edge).sh[4 + (edge).shorient] = (shelle) encode(triedge)
 
1117
 
 
1118
/* Dissolve a bond (from the triangle side).                                 */
 
1119
 
 
1120
#define tsdissolve(triedge)                                                   \
 
1121
  (triedge).tri[6 + (triedge).orient] = (triangle) dummysh
 
1122
 
 
1123
/* Dissolve a bond (from the shell edge side).                               */
 
1124
 
 
1125
#define stdissolve(edge)                                                      \
 
1126
  (edge).sh[4 + (edge).shorient] = (shelle) dummytri
 
1127
 
 
1128
/********* Primitives for points                                     *********/
 
1129
/*                                                                           */
 
1130
/*                                                                           */
 
1131
 
 
1132
#define pointmark(pt)  ((int *) (pt))[pointmarkindex]
 
1133
 
 
1134
#define setpointmark(pt, value)                                               \
 
1135
  ((int *) (pt))[pointmarkindex] = value
 
1136
 
 
1137
#define point2tri(pt)  ((triangle *) (pt))[point2triindex]
 
1138
 
 
1139
#define setpoint2tri(pt, value)                                               \
 
1140
  ((triangle *) (pt))[point2triindex] = value
 
1141
 
 
1142
/**                                                                         **/
 
1143
/**                                                                         **/
 
1144
/********* Mesh manipulation primitives end here                     *********/
 
1145
 
 
1146
/********* User interaction routines begin here                      *********/
 
1147
/**                                                                         **/
 
1148
/**                                                                         **/
 
1149
 
 
1150
/*****************************************************************************/
 
1151
/*                                                                           */
 
1152
/*  syntax()   Print list of command line switches.                          */
 
1153
/*                                                                           */
 
1154
/*****************************************************************************/
 
1155
 
 
1156
#ifndef TRILIBRARY
 
1157
 
 
1158
void syntax()
 
1159
{
 
1160
#ifdef CDT_ONLY
 
1161
#ifdef REDUCED
 
1162
  printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n");
 
1163
#else /* not REDUCED */
 
1164
  printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n");
 
1165
#endif /* not REDUCED */
 
1166
#else /* not CDT_ONLY */
 
1167
#ifdef REDUCED
 
1168
  printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n");
 
1169
#else /* not REDUCED */
 
1170
  printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n");
 
1171
#endif /* not REDUCED */
 
1172
#endif /* not CDT_ONLY */
 
1173
 
 
1174
  printf("    -p  Triangulates a Planar Straight Line Graph (.poly file).\n");
 
1175
#ifndef CDT_ONLY
 
1176
  printf("    -r  Refines a previously generated mesh.\n");
 
1177
  printf(
 
1178
    "    -q  Quality mesh generation.  A minimum angle may be specified.\n");
 
1179
  printf("    -a  Applies a maximum triangle area constraint.\n");
 
1180
#endif /* not CDT_ONLY */
 
1181
  printf(
 
1182
    "    -A  Applies attributes to identify elements in certain regions.\n");
 
1183
  printf("    -c  Encloses the convex hull with segments.\n");
 
1184
  printf("    -e  Generates an edge list.\n");
 
1185
  printf("    -v  Generates a Voronoi diagram.\n");
 
1186
  printf("    -n  Generates a list of triangle neighbors.\n");
 
1187
  printf("    -g  Generates an .off file for Geomview.\n");
 
1188
  printf("    -B  Suppresses output of boundary information.\n");
 
1189
  printf("    -P  Suppresses output of .poly file.\n");
 
1190
  printf("    -N  Suppresses output of .node file.\n");
 
1191
  printf("    -E  Suppresses output of .ele file.\n");
 
1192
  printf("    -I  Suppresses mesh iteration numbers.\n");
 
1193
  printf("    -O  Ignores holes in .poly file.\n");
 
1194
  printf("    -X  Suppresses use of exact arithmetic.\n");
 
1195
  printf("    -z  Numbers all items starting from zero (rather than one).\n");
 
1196
  printf("    -o2 Generates second-order subparametric elements.\n");
 
1197
#ifndef CDT_ONLY
 
1198
  printf("    -Y  Suppresses boundary segment splitting.\n");
 
1199
  printf("    -S  Specifies maximum number of added Steiner points.\n");
 
1200
#endif /* not CDT_ONLY */
 
1201
#ifndef REDUCED
 
1202
  printf("    -i  Uses incremental method, rather than divide-and-conquer.\n");
 
1203
  printf("    -F  Uses Fortune's sweepline algorithm, rather than d-and-c.\n");
 
1204
#endif /* not REDUCED */
 
1205
  printf("    -l  Uses vertical cuts only, rather than alternating cuts.\n");
 
1206
#ifndef REDUCED
 
1207
#ifndef CDT_ONLY
 
1208
  printf(
 
1209
    "    -s  Force segments into mesh by splitting (instead of using CDT).\n");
 
1210
#endif /* not CDT_ONLY */
 
1211
  printf("    -C  Check consistency of final mesh.\n");
 
1212
#endif /* not REDUCED */
 
1213
  printf("    -Q  Quiet:  No terminal output except errors.\n");
 
1214
  printf("    -V  Verbose:  Detailed information on what I'm doing.\n");
 
1215
  printf("    -h  Help:  Detailed instructions for Triangle.\n");
 
1216
  exit(0);
 
1217
}
 
1218
 
 
1219
#endif /* not TRILIBRARY */
 
1220
 
 
1221
/*****************************************************************************/
 
1222
/*                                                                           */
 
1223
/*  info()   Print out complete instructions.                                */
 
1224
/*                                                                           */
 
1225
/*****************************************************************************/
 
1226
 
 
1227
#ifndef TRILIBRARY
 
1228
 
 
1229
void info()
 
1230
{
 
1231
  printf("Triangle\n");
 
1232
  printf(
 
1233
"A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\n");
 
1234
  printf("Version 1.3\n\n");
 
1235
  printf(
 
1236
"Copyright 1996 Jonathan Richard Shewchuk  (bugs/comments to jrs@cs.cmu.edu)\n"
 
1237
);
 
1238
  printf("School of Computer Science / Carnegie Mellon University\n");
 
1239
  printf("5000 Forbes Avenue / Pittsburgh, Pennsylvania  15213-3891\n");
 
1240
  printf(
 
1241
"Created as part of the Archimedes project (tools for parallel FEM).\n");
 
1242
  printf(
 
1243
"Supported in part by NSF Grant CMS-9318163 and an NSERC 1967 Scholarship.\n");
 
1244
  printf("There is no warranty whatsoever.  Use at your own risk.\n");
 
1245
#ifdef SINGLE
 
1246
  printf("This executable is compiled for single precision arithmetic.\n\n\n");
 
1247
#else /* not SINGLE */
 
1248
  printf("This executable is compiled for double precision arithmetic.\n\n\n");
 
1249
#endif /* not SINGLE */
 
1250
  printf(
 
1251
"Triangle generates exact Delaunay triangulations, constrained Delaunay\n");
 
1252
  printf(
 
1253
"triangulations, and quality conforming Delaunay triangulations.  The latter\n"
 
1254
);
 
1255
  printf(
 
1256
"can be generated with no small angles, and are thus suitable for finite\n");
 
1257
  printf(
 
1258
"element analysis.  If no command line switches are specified, your .node\n");
 
1259
  printf(
 
1260
"input file will be read, and the Delaunay triangulation will be returned in\n"
 
1261
);
 
1262
  printf(".node and .ele output files.  The command syntax is:\n\n");
 
1263
#ifdef CDT_ONLY
 
1264
#ifdef REDUCED
 
1265
  printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n\n");
 
1266
#else /* not REDUCED */
 
1267
  printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n\n");
 
1268
#endif /* not REDUCED */
 
1269
#else /* not CDT_ONLY */
 
1270
#ifdef REDUCED
 
1271
  printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n\n");
 
1272
#else /* not REDUCED */
 
1273
  printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n\n");
 
1274
#endif /* not REDUCED */
 
1275
#endif /* not CDT_ONLY */
 
1276
  printf(
 
1277
"Underscores indicate that numbers may optionally follow certain switches;\n");
 
1278
  printf(
 
1279
"do not leave any space between a switch and its numeric parameter.\n");
 
1280
  printf(
 
1281
"input_file must be a file with extension .node, or extension .poly if the\n");
 
1282
  printf(
 
1283
"-p switch is used.  If -r is used, you must supply .node and .ele files,\n");
 
1284
  printf(
 
1285
"and possibly a .poly file and .area file as well.  The formats of these\n");
 
1286
  printf("files are described below.\n\n");
 
1287
  printf("Command Line Switches:\n\n");
 
1288
  printf(
 
1289
"    -p  Reads a Planar Straight Line Graph (.poly file), which can specify\n"
 
1290
);
 
1291
  printf(
 
1292
"        points, segments, holes, and regional attributes and area\n");
 
1293
  printf(
 
1294
"        constraints.  Will generate a constrained Delaunay triangulation\n");
 
1295
  printf(
 
1296
"        fitting the input; or, if -s, -q, or -a is used, a conforming\n");
 
1297
  printf(
 
1298
"        Delaunay triangulation.  If -p is not used, Triangle reads a .node\n"
 
1299
);
 
1300
  printf("        file by default.\n");
 
1301
  printf(
 
1302
"    -r  Refines a previously generated mesh.  The mesh is read from a .node\n"
 
1303
);
 
1304
  printf(
 
1305
"        file and an .ele file.  If -p is also used, a .poly file is read\n");
 
1306
  printf(
 
1307
"        and used to constrain edges in the mesh.  Further details on\n");
 
1308
  printf("        refinement are given below.\n");
 
1309
  printf(
 
1310
"    -q  Quality mesh generation by Jim Ruppert's Delaunay refinement\n");
 
1311
  printf(
 
1312
"        algorithm.  Adds points to the mesh to ensure that no angles\n");
 
1313
  printf(
 
1314
"        smaller than 20 degrees occur.  An alternative minimum angle may be\n"
 
1315
);
 
1316
  printf(
 
1317
"        specified after the `q'.  If the minimum angle is 20.7 degrees or\n");
 
1318
  printf(
 
1319
"        smaller, the triangulation algorithm is theoretically guaranteed to\n"
 
1320
);
 
1321
  printf(
 
1322
"        terminate (assuming infinite precision arithmetic - Triangle may\n");
 
1323
  printf(
 
1324
"        fail to terminate if you run out of precision).  In practice, the\n");
 
1325
  printf(
 
1326
"        algorithm often succeeds for minimum angles up to 33.8 degrees.\n");
 
1327
  printf(
 
1328
"        For highly refined meshes, however, it may be necessary to reduce\n");
 
1329
  printf(
 
1330
"        the minimum angle to well below 20 to avoid problems associated\n");
 
1331
  printf(
 
1332
"        with insufficient floating-point precision.  The specified angle\n");
 
1333
  printf("        may include a decimal point.\n");
 
1334
  printf(
 
1335
"    -a  Imposes a maximum triangle area.  If a number follows the `a', no\n");
 
1336
  printf(
 
1337
"        triangle will be generated whose area is larger than that number.\n");
 
1338
  printf(
 
1339
"        If no number is specified, an .area file (if -r is used) or .poly\n");
 
1340
  printf(
 
1341
"        file (if -r is not used) specifies a number of maximum area\n");
 
1342
  printf(
 
1343
"        constraints.  An .area file contains a separate area constraint for\n"
 
1344
);
 
1345
  printf(
 
1346
"        each triangle, and is useful for refining a finite element mesh\n");
 
1347
  printf(
 
1348
"        based on a posteriori error estimates.  A .poly file can optionally\n"
 
1349
);
 
1350
  printf(
 
1351
"        contain an area constraint for each segment-bounded region, thereby\n"
 
1352
);
 
1353
  printf(
 
1354
"        enforcing triangle densities in a first triangulation.  You can\n");
 
1355
  printf(
 
1356
"        impose both a fixed area constraint and a varying area constraint\n");
 
1357
  printf(
 
1358
"        by invoking the -a switch twice, once with and once without a\n");
 
1359
  printf(
 
1360
"        number following.  Each area specified may include a decimal point.\n"
 
1361
);
 
1362
  printf(
 
1363
"    -A  Assigns an additional attribute to each triangle that identifies\n");
 
1364
  printf(
 
1365
"        what segment-bounded region each triangle belongs to.  Attributes\n");
 
1366
  printf(
 
1367
"        are assigned to regions by the .poly file.  If a region is not\n");
 
1368
  printf(
 
1369
"        explicitly marked by the .poly file, triangles in that region are\n");
 
1370
  printf(
 
1371
"        assigned an attribute of zero.  The -A switch has an effect only\n");
 
1372
  printf("        when the -p switch is used and the -r switch is not.\n");
 
1373
  printf(
 
1374
"    -c  Creates segments on the convex hull of the triangulation.  If you\n");
 
1375
  printf(
 
1376
"        are triangulating a point set, this switch causes a .poly file to\n");
 
1377
  printf(
 
1378
"        be written, containing all edges in the convex hull.  (By default,\n"
 
1379
);
 
1380
  printf(
 
1381
"        a .poly file is written only if a .poly file is read.)  If you are\n"
 
1382
);
 
1383
  printf(
 
1384
"        triangulating a PSLG, this switch specifies that the interior of\n");
 
1385
  printf(
 
1386
"        the convex hull of the PSLG should be triangulated.  If you do not\n"
 
1387
);
 
1388
  printf(
 
1389
"        use this switch when triangulating a PSLG, it is assumed that you\n");
 
1390
  printf(
 
1391
"        have identified the region to be triangulated by surrounding it\n");
 
1392
  printf(
 
1393
"        with segments of the input PSLG.  Beware:  if you are not careful,\n"
 
1394
);
 
1395
  printf(
 
1396
"        this switch can cause the introduction of an extremely thin angle\n");
 
1397
  printf(
 
1398
"        between a PSLG segment and a convex hull segment, which can cause\n");
 
1399
  printf(
 
1400
"        overrefinement or failure if Triangle runs out of precision.  If\n");
 
1401
  printf(
 
1402
"        you are refining a mesh, the -c switch works differently; it\n");
 
1403
  printf(
 
1404
"        generates the set of boundary edges of the mesh, rather than the\n");
 
1405
  printf("        convex hull.\n");
 
1406
  printf(
 
1407
"    -e  Outputs (to an .edge file) a list of edges of the triangulation.\n");
 
1408
  printf(
 
1409
"    -v  Outputs the Voronoi diagram associated with the triangulation.\n");
 
1410
  printf("        Does not attempt to detect degeneracies.\n");
 
1411
  printf(
 
1412
"    -n  Outputs (to a .neigh file) a list of triangles neighboring each\n");
 
1413
  printf("        triangle.\n");
 
1414
  printf(
 
1415
"    -g  Outputs the mesh to an Object File Format (.off) file, suitable for\n"
 
1416
);
 
1417
  printf("        viewing with the Geometry Center's Geomview package.\n");
 
1418
  printf(
 
1419
"    -B  No boundary markers in the output .node, .poly, and .edge output\n");
 
1420
  printf(
 
1421
"        files.  See the detailed discussion of boundary markers below.\n");
 
1422
  printf(
 
1423
"    -P  No output .poly file.  Saves disk space, but you lose the ability\n");
 
1424
  printf(
 
1425
"        to impose segment constraints on later refinements of the mesh.\n");
 
1426
  printf("    -N  No output .node file.\n");
 
1427
  printf("    -E  No output .ele file.\n");
 
1428
  printf(
 
1429
"    -I  No iteration numbers.  Suppresses the output of .node and .poly\n");
 
1430
  printf(
 
1431
"        files, so your input files won't be overwritten.  (If your input is\n"
 
1432
);
 
1433
  printf(
 
1434
"        a .poly file only, a .node file will be written.)  Cannot be used\n");
 
1435
  printf(
 
1436
"        with the -r switch, because that would overwrite your input .ele\n");
 
1437
  printf(
 
1438
"        file.  Shouldn't be used with the -s, -q, or -a switch if you are\n");
 
1439
  printf(
 
1440
"        using a .node file for input, because no .node file will be\n");
 
1441
  printf("        written, so there will be no record of any added points.\n");
 
1442
  printf("    -O  No holes.  Ignores the holes in the .poly file.\n");
 
1443
  printf(
 
1444
"    -X  No exact arithmetic.  Normally, Triangle uses exact floating-point\n"
 
1445
);
 
1446
  printf(
 
1447
"        arithmetic for certain tests if it thinks the inexact tests are not\n"
 
1448
);
 
1449
  printf(
 
1450
"        accurate enough.  Exact arithmetic ensures the robustness of the\n");
 
1451
  printf(
 
1452
"        triangulation algorithms, despite floating-point roundoff error.\n");
 
1453
  printf(
 
1454
"        Disabling exact arithmetic with the -X switch will cause a small\n");
 
1455
  printf(
 
1456
"        improvement in speed and create the possibility (albeit small) that\n"
 
1457
);
 
1458
  printf(
 
1459
"        Triangle will fail to produce a valid mesh.  Not recommended.\n");
 
1460
  printf(
 
1461
"    -z  Numbers all items starting from zero (rather than one).  Note that\n"
 
1462
);
 
1463
  printf(
 
1464
"        this switch is normally overrided by the value used to number the\n");
 
1465
  printf(
 
1466
"        first point of the input .node or .poly file.  However, this switch\n"
 
1467
);
 
1468
  printf("        is useful when calling Triangle from another program.\n");
 
1469
  printf(
 
1470
"    -o2 Generates second-order subparametric elements with six nodes each.\n"
 
1471
);
 
1472
  printf(
 
1473
"    -Y  No new points on the boundary.  This switch is useful when the mesh\n"
 
1474
);
 
1475
  printf(
 
1476
"        boundary must be preserved so that it conforms to some adjacent\n");
 
1477
  printf(
 
1478
"        mesh.  Be forewarned that you will probably sacrifice some of the\n");
 
1479
  printf(
 
1480
"        quality of the mesh; Triangle will try, but the resulting mesh may\n"
 
1481
);
 
1482
  printf(
 
1483
"        contain triangles of poor aspect ratio.  Works well if all the\n");
 
1484
  printf(
 
1485
"        boundary points are closely spaced.  Specify this switch twice\n");
 
1486
  printf(
 
1487
"        (`-YY') to prevent all segment splitting, including internal\n");
 
1488
  printf("        boundaries.\n");
 
1489
  printf(
 
1490
"    -S  Specifies the maximum number of Steiner points (points that are not\n"
 
1491
);
 
1492
  printf(
 
1493
"        in the input, but are added to meet the constraints of minimum\n");
 
1494
  printf(
 
1495
"        angle and maximum area).  The default is to allow an unlimited\n");
 
1496
  printf(
 
1497
"        number.  If you specify this switch with no number after it,\n");
 
1498
  printf(
 
1499
"        the limit is set to zero.  Triangle always adds points at segment\n");
 
1500
  printf(
 
1501
"        intersections, even if it needs to use more points than the limit\n");
 
1502
  printf(
 
1503
"        you set.  When Triangle inserts segments by splitting (-s), it\n");
 
1504
  printf(
 
1505
"        always adds enough points to ensure that all the segments appear in\n"
 
1506
);
 
1507
  printf(
 
1508
"        the triangulation, again ignoring the limit.  Be forewarned that\n");
 
1509
  printf(
 
1510
"        the -S switch may result in a conforming triangulation that is not\n"
 
1511
);
 
1512
  printf(
 
1513
"        truly Delaunay, because Triangle may be forced to stop adding\n");
 
1514
  printf(
 
1515
"        points when the mesh is in a state where a segment is non-Delaunay\n"
 
1516
);
 
1517
  printf(
 
1518
"        and needs to be split.  If so, Triangle will print a warning.\n");
 
1519
  printf(
 
1520
"    -i  Uses an incremental rather than divide-and-conquer algorithm to\n");
 
1521
  printf(
 
1522
"        form a Delaunay triangulation.  Try it if the divide-and-conquer\n");
 
1523
  printf("        algorithm fails.\n");
 
1524
  printf(
 
1525
"    -F  Uses Steven Fortune's sweepline algorithm to form a Delaunay\n");
 
1526
  printf(
 
1527
"        triangulation.  Warning:  does not use exact arithmetic for all\n");
 
1528
  printf("        calculations.  An exact result is not guaranteed.\n");
 
1529
  printf(
 
1530
"    -l  Uses only vertical cuts in the divide-and-conquer algorithm.  By\n");
 
1531
  printf(
 
1532
"        default, Triangle uses alternating vertical and horizontal cuts,\n");
 
1533
  printf(
 
1534
"        which usually improve the speed except with point sets that are\n");
 
1535
  printf(
 
1536
"        small or short and wide.  This switch is primarily of theoretical\n");
 
1537
  printf("        interest.\n");
 
1538
  printf(
 
1539
"    -s  Specifies that segments should be forced into the triangulation by\n"
 
1540
);
 
1541
  printf(
 
1542
"        recursively splitting them at their midpoints, rather than by\n");
 
1543
  printf(
 
1544
"        generating a constrained Delaunay triangulation.  Segment splitting\n"
 
1545
);
 
1546
  printf(
 
1547
"        is true to Ruppert's original algorithm, but can create needlessly\n"
 
1548
);
 
1549
  printf("        small triangles near external small features.\n");
 
1550
  printf(
 
1551
"    -C  Check the consistency of the final mesh.  Uses exact arithmetic for\n"
 
1552
);
 
1553
  printf(
 
1554
"        checking, even if the -X switch is used.  Useful if you suspect\n");
 
1555
  printf("        Triangle is buggy.\n");
 
1556
  printf(
 
1557
"    -Q  Quiet: Suppresses all explanation of what Triangle is doing, unless\n"
 
1558
);
 
1559
  printf("        an error occurs.\n");
 
1560
  printf(
 
1561
"    -V  Verbose: Gives detailed information about what Triangle is doing.\n");
 
1562
  printf(
 
1563
"        Add more `V's for increasing amount of detail.  `-V' gives\n");
 
1564
  printf(
 
1565
"        information on algorithmic progress and more detailed statistics.\n");
 
1566
  printf(
 
1567
"        `-VV' gives point-by-point details, and will print so much that\n");
 
1568
  printf(
 
1569
"        Triangle will run much more slowly.  `-VVV' gives information only\n"
 
1570
);
 
1571
  printf("        a debugger could love.\n");
 
1572
  printf("    -h  Help:  Displays these instructions.\n");
 
1573
  printf("\n");
 
1574
  printf("Definitions:\n");
 
1575
  printf("\n");
 
1576
  printf(
 
1577
"  A Delaunay triangulation of a point set is a triangulation whose vertices\n"
 
1578
);
 
1579
  printf(
 
1580
"  are the point set, having the property that no point in the point set\n");
 
1581
  printf(
 
1582
"  falls in the interior of the circumcircle (circle that passes through all\n"
 
1583
);
 
1584
  printf("  three vertices) of any triangle in the triangulation.\n\n");
 
1585
  printf(
 
1586
"  A Voronoi diagram of a point set is a subdivision of the plane into\n");
 
1587
  printf(
 
1588
"  polygonal regions (some of which may be infinite), where each region is\n");
 
1589
  printf(
 
1590
"  the set of points in the plane that are closer to some input point than\n");
 
1591
  printf(
 
1592
"  to any other input point.  (The Voronoi diagram is the geometric dual of\n"
 
1593
);
 
1594
  printf("  the Delaunay triangulation.)\n\n");
 
1595
  printf(
 
1596
"  A Planar Straight Line Graph (PSLG) is a collection of points and\n");
 
1597
  printf(
 
1598
"  segments.  Segments are simply edges, whose endpoints are points in the\n");
 
1599
  printf(
 
1600
"  PSLG.  The file format for PSLGs (.poly files) is described below.\n");
 
1601
  printf("\n");
 
1602
  printf(
 
1603
"  A constrained Delaunay triangulation of a PSLG is similar to a Delaunay\n");
 
1604
  printf(
 
1605
"  triangulation, but each PSLG segment is present as a single edge in the\n");
 
1606
  printf(
 
1607
"  triangulation.  (A constrained Delaunay triangulation is not truly a\n");
 
1608
  printf("  Delaunay triangulation.)\n\n");
 
1609
  printf(
 
1610
"  A conforming Delaunay triangulation of a PSLG is a true Delaunay\n");
 
1611
  printf(
 
1612
"  triangulation in which each PSLG segment may have been subdivided into\n");
 
1613
  printf(
 
1614
"  several edges by the insertion of additional points.  These inserted\n");
 
1615
  printf(
 
1616
"  points are necessary to allow the segments to exist in the mesh while\n");
 
1617
  printf("  maintaining the Delaunay property.\n\n");
 
1618
  printf("File Formats:\n\n");
 
1619
  printf(
 
1620
"  All files may contain comments prefixed by the character '#'.  Points,\n");
 
1621
  printf(
 
1622
"  triangles, edges, holes, and maximum area constraints must be numbered\n");
 
1623
  printf(
 
1624
"  consecutively, starting from either 1 or 0.  Whichever you choose, all\n");
 
1625
  printf(
 
1626
"  input files must be consistent; if the nodes are numbered from 1, so must\n"
 
1627
);
 
1628
  printf(
 
1629
"  be all other objects.  Triangle automatically detects your choice while\n");
 
1630
  printf(
 
1631
"  reading the .node (or .poly) file.  (When calling Triangle from another\n");
 
1632
  printf(
 
1633
"  program, use the -z switch if you wish to number objects from zero.)\n");
 
1634
  printf("  Examples of these file formats are given below.\n\n");
 
1635
  printf("  .node files:\n");
 
1636
  printf(
 
1637
"    First line:  <# of points> <dimension (must be 2)> <# of attributes>\n");
 
1638
  printf(
 
1639
"                                           <# of boundary markers (0 or 1)>\n"
 
1640
);
 
1641
  printf(
 
1642
"    Remaining lines:  <point #> <x> <y> [attributes] [boundary marker]\n");
 
1643
  printf("\n");
 
1644
  printf(
 
1645
"    The attributes, which are typically floating-point values of physical\n");
 
1646
  printf(
 
1647
"    quantities (such as mass or conductivity) associated with the nodes of\n"
 
1648
);
 
1649
  printf(
 
1650
"    a finite element mesh, are copied unchanged to the output mesh.  If -s,\n"
 
1651
);
 
1652
  printf(
 
1653
"    -q, or -a is selected, each new Steiner point added to the mesh will\n");
 
1654
  printf("    have attributes assigned to it by linear interpolation.\n\n");
 
1655
  printf(
 
1656
"    If the fourth entry of the first line is `1', the last column of the\n");
 
1657
  printf(
 
1658
"    remainder of the file is assumed to contain boundary markers.  Boundary\n"
 
1659
);
 
1660
  printf(
 
1661
"    markers are used to identify boundary points and points resting on PSLG\n"
 
1662
);
 
1663
  printf(
 
1664
"    segments; a complete description appears in a section below.  The .node\n"
 
1665
);
 
1666
  printf(
 
1667
"    file produced by Triangle will contain boundary markers in the last\n");
 
1668
  printf("    column unless they are suppressed by the -B switch.\n\n");
 
1669
  printf("  .ele files:\n");
 
1670
  printf(
 
1671
"    First line:  <# of triangles> <points per triangle> <# of attributes>\n");
 
1672
  printf(
 
1673
"    Remaining lines:  <triangle #> <point> <point> <point> ... [attributes]\n"
 
1674
);
 
1675
  printf("\n");
 
1676
  printf(
 
1677
"    Points are indices into the corresponding .node file.  The first three\n"
 
1678
);
 
1679
  printf(
 
1680
"    points are the corners, and are listed in counterclockwise order around\n"
 
1681
);
 
1682
  printf(
 
1683
"    each triangle.  (The remaining points, if any, depend on the type of\n");
 
1684
  printf(
 
1685
"    finite element used.)  The attributes are just like those of .node\n");
 
1686
  printf(
 
1687
"    files.  Because there is no simple mapping from input to output\n");
 
1688
  printf(
 
1689
"    triangles, an attempt is made to interpolate attributes, which may\n");
 
1690
  printf(
 
1691
"    result in a good deal of diffusion of attributes among nearby triangles\n"
 
1692
);
 
1693
  printf(
 
1694
"    as the triangulation is refined.  Diffusion does not occur across\n");
 
1695
  printf(
 
1696
"    segments, so attributes used to identify segment-bounded regions remain\n"
 
1697
);
 
1698
  printf(
 
1699
"    intact.  In output .ele files, all triangles have three points each\n");
 
1700
  printf(
 
1701
"    unless the -o2 switch is used, in which case they have six, and the\n");
 
1702
  printf(
 
1703
"    fourth, fifth, and sixth points lie on the midpoints of the edges\n");
 
1704
  printf("    opposite the first, second, and third corners.\n\n");
 
1705
  printf("  .poly files:\n");
 
1706
  printf(
 
1707
"    First line:  <# of points> <dimension (must be 2)> <# of attributes>\n");
 
1708
  printf(
 
1709
"                                           <# of boundary markers (0 or 1)>\n"
 
1710
);
 
1711
  printf(
 
1712
"    Following lines:  <point #> <x> <y> [attributes] [boundary marker]\n");
 
1713
  printf("    One line:  <# of segments> <# of boundary markers (0 or 1)>\n");
 
1714
  printf(
 
1715
"    Following lines:  <segment #> <endpoint> <endpoint> [boundary marker]\n");
 
1716
  printf("    One line:  <# of holes>\n");
 
1717
  printf("    Following lines:  <hole #> <x> <y>\n");
 
1718
  printf(
 
1719
"    Optional line:  <# of regional attributes and/or area constraints>\n");
 
1720
  printf(
 
1721
"    Optional following lines:  <constraint #> <x> <y> <attrib> <max area>\n");
 
1722
  printf("\n");
 
1723
  printf(
 
1724
"    A .poly file represents a PSLG, as well as some additional information.\n"
 
1725
);
 
1726
  printf(
 
1727
"    The first section lists all the points, and is identical to the format\n"
 
1728
);
 
1729
  printf(
 
1730
"    of .node files.  <# of points> may be set to zero to indicate that the\n"
 
1731
);
 
1732
  printf(
 
1733
"    points are listed in a separate .node file; .poly files produced by\n");
 
1734
  printf(
 
1735
"    Triangle always have this format.  This has the advantage that a point\n"
 
1736
);
 
1737
  printf(
 
1738
"    set may easily be triangulated with or without segments.  (The same\n");
 
1739
  printf(
 
1740
"    effect can be achieved, albeit using more disk space, by making a copy\n"
 
1741
);
 
1742
  printf(
 
1743
"    of the .poly file with the extension .node; all sections of the file\n");
 
1744
  printf("    but the first are ignored.)\n\n");
 
1745
  printf(
 
1746
"    The second section lists the segments.  Segments are edges whose\n");
 
1747
  printf(
 
1748
"    presence in the triangulation is enforced.  Each segment is specified\n");
 
1749
  printf(
 
1750
"    by listing the indices of its two endpoints.  This means that you must\n"
 
1751
);
 
1752
  printf(
 
1753
"    include its endpoints in the point list.  If -s, -q, and -a are not\n");
 
1754
  printf(
 
1755
"    selected, Triangle will produce a constrained Delaunay triangulation,\n");
 
1756
  printf(
 
1757
"    in which each segment appears as a single edge in the triangulation.\n");
 
1758
  printf(
 
1759
"    If -q or -a is selected, Triangle will produce a conforming Delaunay\n");
 
1760
  printf(
 
1761
"    triangulation, in which segments may be subdivided into smaller edges.\n"
 
1762
);
 
1763
  printf("    Each segment, like each point, may have a boundary marker.\n\n");
 
1764
  printf(
 
1765
"    The third section lists holes (and concavities, if -c is selected) in\n");
 
1766
  printf(
 
1767
"    the triangulation.  Holes are specified by identifying a point inside\n");
 
1768
  printf(
 
1769
"    each hole.  After the triangulation is formed, Triangle creates holes\n");
 
1770
  printf(
 
1771
"    by eating triangles, spreading out from each hole point until its\n");
 
1772
  printf(
 
1773
"    progress is blocked by PSLG segments; you must be careful to enclose\n");
 
1774
  printf(
 
1775
"    each hole in segments, or your whole triangulation may be eaten away.\n");
 
1776
  printf(
 
1777
"    If the two triangles abutting a segment are eaten, the segment itself\n");
 
1778
  printf(
 
1779
"    is also eaten.  Do not place a hole directly on a segment; if you do,\n");
 
1780
  printf("    Triangle will choose one side of the segment arbitrarily.\n\n");
 
1781
  printf(
 
1782
"    The optional fourth section lists regional attributes (to be assigned\n");
 
1783
  printf(
 
1784
"    to all triangles in a region) and regional constraints on the maximum\n");
 
1785
  printf(
 
1786
"    triangle area.  Triangle will read this section only if the -A switch\n");
 
1787
  printf(
 
1788
"    is used or the -a switch is used without a number following it, and the\n"
 
1789
);
 
1790
  printf(
 
1791
"    -r switch is not used.  Regional attributes and area constraints are\n");
 
1792
  printf(
 
1793
"    propagated in the same manner as holes; you specify a point for each\n");
 
1794
  printf(
 
1795
"    attribute and/or constraint, and the attribute and/or constraint will\n");
 
1796
  printf(
 
1797
"    affect the whole region (bounded by segments) containing the point.  If\n"
 
1798
);
 
1799
  printf(
 
1800
"    two values are written on a line after the x and y coordinate, the\n");
 
1801
  printf(
 
1802
"    former is assumed to be a regional attribute (but will only be applied\n"
 
1803
);
 
1804
  printf(
 
1805
"    if the -A switch is selected), and the latter is assumed to be a\n");
 
1806
  printf(
 
1807
"    regional area constraint (but will only be applied if the -a switch is\n"
 
1808
);
 
1809
  printf(
 
1810
"    selected).  You may also specify just one value after the coordinates,\n"
 
1811
);
 
1812
  printf(
 
1813
"    which can serve as both an attribute and an area constraint, depending\n"
 
1814
);
 
1815
  printf(
 
1816
"    on the choice of switches.  If you are using the -A and -a switches\n");
 
1817
  printf(
 
1818
"    simultaneously and wish to assign an attribute to some region without\n");
 
1819
  printf("    imposing an area constraint, use a negative maximum area.\n\n");
 
1820
  printf(
 
1821
"    When a triangulation is created from a .poly file, you must either\n");
 
1822
  printf(
 
1823
"    enclose the entire region to be triangulated in PSLG segments, or\n");
 
1824
  printf(
 
1825
"    use the -c switch, which encloses the convex hull of the input point\n");
 
1826
  printf(
 
1827
"    set.  If you do not use the -c switch, Triangle will eat all triangles\n"
 
1828
);
 
1829
  printf(
 
1830
"    on the outer boundary that are not protected by segments; if you are\n");
 
1831
  printf(
 
1832
"    not careful, your whole triangulation may be eaten away.  If you do\n");
 
1833
  printf(
 
1834
"    use the -c switch, you can still produce concavities by appropriate\n");
 
1835
  printf("    placement of holes just inside the convex hull.\n\n");
 
1836
  printf(
 
1837
"    An ideal PSLG has no intersecting segments, nor any points that lie\n");
 
1838
  printf(
 
1839
"    upon segments (except, of course, the endpoints of each segment.)  You\n"
 
1840
);
 
1841
  printf(
 
1842
"    aren't required to make your .poly files ideal, but you should be aware\n"
 
1843
);
 
1844
  printf(
 
1845
"    of what can go wrong.  Segment intersections are relatively safe -\n");
 
1846
  printf(
 
1847
"    Triangle will calculate the intersection points for you and add them to\n"
 
1848
);
 
1849
  printf(
 
1850
"    the triangulation - as long as your machine's floating-point precision\n"
 
1851
);
 
1852
  printf(
 
1853
"    doesn't become a problem.  You are tempting the fates if you have three\n"
 
1854
);
 
1855
  printf(
 
1856
"    segments that cross at the same location, and expect Triangle to figure\n"
 
1857
);
 
1858
  printf(
 
1859
"    out where the intersection point is.  Thanks to floating-point roundoff\n"
 
1860
);
 
1861
  printf(
 
1862
"    error, Triangle will probably decide that the three segments intersect\n"
 
1863
);
 
1864
  printf(
 
1865
"    at three different points, and you will find a minuscule triangle in\n");
 
1866
  printf(
 
1867
"    your output - unless Triangle tries to refine the tiny triangle, uses\n");
 
1868
  printf(
 
1869
"    up the last bit of machine precision, and fails to terminate at all.\n");
 
1870
  printf(
 
1871
"    You're better off putting the intersection point in the input files,\n");
 
1872
  printf(
 
1873
"    and manually breaking up each segment into two.  Similarly, if you\n");
 
1874
  printf(
 
1875
"    place a point at the middle of a segment, and hope that Triangle will\n");
 
1876
  printf(
 
1877
"    break up the segment at that point, you might get lucky.  On the other\n"
 
1878
);
 
1879
  printf(
 
1880
"    hand, Triangle might decide that the point doesn't lie precisely on the\n"
 
1881
);
 
1882
  printf(
 
1883
"    line, and you'll have a needle-sharp triangle in your output - or a lot\n"
 
1884
);
 
1885
  printf("    of tiny triangles if you're generating a quality mesh.\n\n");
 
1886
  printf(
 
1887
"    When Triangle reads a .poly file, it also writes a .poly file, which\n");
 
1888
  printf(
 
1889
"    includes all edges that are part of input segments.  If the -c switch\n");
 
1890
  printf(
 
1891
"    is used, the output .poly file will also include all of the edges on\n");
 
1892
  printf(
 
1893
"    the convex hull.  Hence, the output .poly file is useful for finding\n");
 
1894
  printf(
 
1895
"    edges associated with input segments and setting boundary conditions in\n"
 
1896
);
 
1897
  printf(
 
1898
"    finite element simulations.  More importantly, you will need it if you\n"
 
1899
);
 
1900
  printf(
 
1901
"    plan to refine the output mesh, and don't want segments to be missing\n");
 
1902
  printf("    in later triangulations.\n\n");
 
1903
  printf("  .area files:\n");
 
1904
  printf("    First line:  <# of triangles>\n");
 
1905
  printf("    Following lines:  <triangle #> <maximum area>\n\n");
 
1906
  printf(
 
1907
"    An .area file associates with each triangle a maximum area that is used\n"
 
1908
);
 
1909
  printf(
 
1910
"    for mesh refinement.  As with other file formats, every triangle must\n");
 
1911
  printf(
 
1912
"    be represented, and they must be numbered consecutively.  A triangle\n");
 
1913
  printf(
 
1914
"    may be left unconstrained by assigning it a negative maximum area.\n");
 
1915
  printf("\n");
 
1916
  printf("  .edge files:\n");
 
1917
  printf("    First line:  <# of edges> <# of boundary markers (0 or 1)>\n");
 
1918
  printf(
 
1919
"    Following lines:  <edge #> <endpoint> <endpoint> [boundary marker]\n");
 
1920
  printf("\n");
 
1921
  printf(
 
1922
"    Endpoints are indices into the corresponding .node file.  Triangle can\n"
 
1923
);
 
1924
  printf(
 
1925
"    produce .edge files (use the -e switch), but cannot read them.  The\n");
 
1926
  printf(
 
1927
"    optional column of boundary markers is suppressed by the -B switch.\n");
 
1928
  printf("\n");
 
1929
  printf(
 
1930
"    In Voronoi diagrams, one also finds a special kind of edge that is an\n");
 
1931
  printf(
 
1932
"    infinite ray with only one endpoint.  For these edges, a different\n");
 
1933
  printf("    format is used:\n\n");
 
1934
  printf("        <edge #> <endpoint> -1 <direction x> <direction y>\n\n");
 
1935
  printf(
 
1936
"    The `direction' is a floating-point vector that indicates the direction\n"
 
1937
);
 
1938
  printf("    of the infinite ray.\n\n");
 
1939
  printf("  .neigh files:\n");
 
1940
  printf(
 
1941
"    First line:  <# of triangles> <# of neighbors per triangle (always 3)>\n"
 
1942
);
 
1943
  printf(
 
1944
"    Following lines:  <triangle #> <neighbor> <neighbor> <neighbor>\n");
 
1945
  printf("\n");
 
1946
  printf(
 
1947
"    Neighbors are indices into the corresponding .ele file.  An index of -1\n"
 
1948
);
 
1949
  printf(
 
1950
"    indicates a mesh boundary, and therefore no neighbor.  Triangle can\n");
 
1951
  printf(
 
1952
"    produce .neigh files (use the -n switch), but cannot read them.\n");
 
1953
  printf("\n");
 
1954
  printf(
 
1955
"    The first neighbor of triangle i is opposite the first corner of\n");
 
1956
  printf("    triangle i, and so on.\n\n");
 
1957
  printf("Boundary Markers:\n\n");
 
1958
  printf(
 
1959
"  Boundary markers are tags used mainly to identify which output points and\n"
 
1960
);
 
1961
  printf(
 
1962
"  edges are associated with which PSLG segment, and to identify which\n");
 
1963
  printf(
 
1964
"  points and edges occur on a boundary of the triangulation.  A common use\n"
 
1965
);
 
1966
  printf(
 
1967
"  is to determine where boundary conditions should be applied to a finite\n");
 
1968
  printf(
 
1969
"  element mesh.  You can prevent boundary markers from being written into\n");
 
1970
  printf("  files produced by Triangle by using the -B switch.\n\n");
 
1971
  printf(
 
1972
"  The boundary marker associated with each segment in an output .poly file\n"
 
1973
);
 
1974
  printf("  or edge in an output .edge file is chosen as follows:\n");
 
1975
  printf(
 
1976
"    - If an output edge is part or all of a PSLG segment with a nonzero\n");
 
1977
  printf(
 
1978
"      boundary marker, then the edge is assigned the same marker.\n");
 
1979
  printf(
 
1980
"    - Otherwise, if the edge occurs on a boundary of the triangulation\n");
 
1981
  printf(
 
1982
"      (including boundaries of holes), then the edge is assigned the marker\n"
 
1983
);
 
1984
  printf("      one (1).\n");
 
1985
  printf("    - Otherwise, the edge is assigned the marker zero (0).\n");
 
1986
  printf(
 
1987
"  The boundary marker associated with each point in an output .node file is\n"
 
1988
);
 
1989
  printf("  chosen as follows:\n");
 
1990
  printf(
 
1991
"    - If a point is assigned a nonzero boundary marker in the input file,\n");
 
1992
  printf(
 
1993
"      then it is assigned the same marker in the output .node file.\n");
 
1994
  printf(
 
1995
"    - Otherwise, if the point lies on a PSLG segment (including the\n");
 
1996
  printf(
 
1997
"      segment's endpoints) with a nonzero boundary marker, then the point\n");
 
1998
  printf(
 
1999
"      is assigned the same marker.  If the point lies on several such\n");
 
2000
  printf("      segments, one of the markers is chosen arbitrarily.\n");
 
2001
  printf(
 
2002
"    - Otherwise, if the point occurs on a boundary of the triangulation,\n");
 
2003
  printf("      then the point is assigned the marker one (1).\n");
 
2004
  printf("    - Otherwise, the point is assigned the marker zero (0).\n");
 
2005
  printf("\n");
 
2006
  printf(
 
2007
"  If you want Triangle to determine for you which points and edges are on\n");
 
2008
  printf(
 
2009
"  the boundary, assign them the boundary marker zero (or use no markers at\n"
 
2010
);
 
2011
  printf(
 
2012
"  all) in your input files.  Alternatively, you can mark some of them and\n");
 
2013
  printf("  leave others marked zero, allowing Triangle to label them.\n\n");
 
2014
  printf("Triangulation Iteration Numbers:\n\n");
 
2015
  printf(
 
2016
"  Because Triangle can read and refine its own triangulations, input\n");
 
2017
  printf(
 
2018
"  and output files have iteration numbers.  For instance, Triangle might\n");
 
2019
  printf(
 
2020
"  read the files mesh.3.node, mesh.3.ele, and mesh.3.poly, refine the\n");
 
2021
  printf(
 
2022
"  triangulation, and output the files mesh.4.node, mesh.4.ele, and\n");
 
2023
  printf("  mesh.4.poly.  Files with no iteration number are treated as if\n");
 
2024
  printf(
 
2025
"  their iteration number is zero; hence, Triangle might read the file\n");
 
2026
  printf(
 
2027
"  points.node, triangulate it, and produce the files points.1.node and\n");
 
2028
  printf("  points.1.ele.\n\n");
 
2029
  printf(
 
2030
"  Iteration numbers allow you to create a sequence of successively finer\n");
 
2031
  printf(
 
2032
"  meshes suitable for multigrid methods.  They also allow you to produce a\n"
 
2033
);
 
2034
  printf(
 
2035
"  sequence of meshes using error estimate-driven mesh refinement.\n");
 
2036
  printf("\n");
 
2037
  printf(
 
2038
"  If you're not using refinement or quality meshing, and you don't like\n");
 
2039
  printf(
 
2040
"  iteration numbers, use the -I switch to disable them.  This switch will\n");
 
2041
  printf(
 
2042
"  also disable output of .node and .poly files to prevent your input files\n"
 
2043
);
 
2044
  printf(
 
2045
"  from being overwritten.  (If the input is a .poly file that contains its\n"
 
2046
);
 
2047
  printf("  own points, a .node file will be written.)\n\n");
 
2048
  printf("Examples of How to Use Triangle:\n\n");
 
2049
  printf(
 
2050
"  `triangle dots' will read points from dots.node, and write their Delaunay\n"
 
2051
);
 
2052
  printf(
 
2053
"  triangulation to dots.1.node and dots.1.ele.  (dots.1.node will be\n");
 
2054
  printf(
 
2055
"  identical to dots.node.)  `triangle -I dots' writes the triangulation to\n"
 
2056
);
 
2057
  printf(
 
2058
"  dots.ele instead.  (No additional .node file is needed, so none is\n");
 
2059
  printf("  written.)\n\n");
 
2060
  printf(
 
2061
"  `triangle -pe object.1' will read a PSLG from object.1.poly (and possibly\n"
 
2062
);
 
2063
  printf(
 
2064
"  object.1.node, if the points are omitted from object.1.poly) and write\n");
 
2065
  printf("  their constrained Delaunay triangulation to object.2.node and\n");
 
2066
  printf(
 
2067
"  object.2.ele.  The segments will be copied to object.2.poly, and all\n");
 
2068
  printf("  edges will be written to object.2.edge.\n\n");
 
2069
  printf(
 
2070
"  `triangle -pq31.5a.1 object' will read a PSLG from object.poly (and\n");
 
2071
  printf(
 
2072
"  possibly object.node), generate a mesh whose angles are all greater than\n"
 
2073
);
 
2074
  printf(
 
2075
"  31.5 degrees and whose triangles all have area smaller than 0.1, and\n");
 
2076
  printf(
 
2077
"  write the mesh to object.1.node and object.1.ele.  Each segment may have\n"
 
2078
);
 
2079
  printf(
 
2080
"  been broken up into multiple edges; the resulting constrained edges are\n");
 
2081
  printf("  written to object.1.poly.\n\n");
 
2082
  printf(
 
2083
"  Here is a sample file `box.poly' describing a square with a square hole:\n"
 
2084
);
 
2085
  printf("\n");
 
2086
  printf(
 
2087
"    # A box with eight points in 2D, no attributes, one boundary marker.\n");
 
2088
  printf("    8 2 0 1\n");
 
2089
  printf("    # Outer box has these vertices:\n");
 
2090
  printf("     1   0 0   0\n");
 
2091
  printf("     2   0 3   0\n");
 
2092
  printf("     3   3 0   0\n");
 
2093
  printf("     4   3 3   33     # A special marker for this point.\n");
 
2094
  printf("    # Inner square has these vertices:\n");
 
2095
  printf("     5   1 1   0\n");
 
2096
  printf("     6   1 2   0\n");
 
2097
  printf("     7   2 1   0\n");
 
2098
  printf("     8   2 2   0\n");
 
2099
  printf("    # Five segments with boundary markers.\n");
 
2100
  printf("    5 1\n");
 
2101
  printf("     1   1 2   5      # Left side of outer box.\n");
 
2102
  printf("     2   5 7   0      # Segments 2 through 5 enclose the hole.\n");
 
2103
  printf("     3   7 8   0\n");
 
2104
  printf("     4   8 6   10\n");
 
2105
  printf("     5   6 5   0\n");
 
2106
  printf("    # One hole in the middle of the inner square.\n");
 
2107
  printf("    1\n");
 
2108
  printf("     1   1.5 1.5\n\n");
 
2109
  printf(
 
2110
"  Note that some segments are missing from the outer square, so one must\n");
 
2111
  printf(
 
2112
"  use the `-c' switch.  After `triangle -pqc box.poly', here is the output\n"
 
2113
);
 
2114
  printf(
 
2115
"  file `box.1.node', with twelve points.  The last four points were added\n");
 
2116
  printf(
 
2117
"  to meet the angle constraint.  Points 1, 2, and 9 have markers from\n");
 
2118
  printf(
 
2119
"  segment 1.  Points 6 and 8 have markers from segment 4.  All the other\n");
 
2120
  printf(
 
2121
"  points but 4 have been marked to indicate that they lie on a boundary.\n");
 
2122
  printf("\n");
 
2123
  printf("    12  2  0  1\n");
 
2124
  printf("       1    0   0      5\n");
 
2125
  printf("       2    0   3      5\n");
 
2126
  printf("       3    3   0      1\n");
 
2127
  printf("       4    3   3     33\n");
 
2128
  printf("       5    1   1      1\n");
 
2129
  printf("       6    1   2     10\n");
 
2130
  printf("       7    2   1      1\n");
 
2131
  printf("       8    2   2     10\n");
 
2132
  printf("       9    0   1.5    5\n");
 
2133
  printf("      10    1.5   0    1\n");
 
2134
  printf("      11    3   1.5    1\n");
 
2135
  printf("      12    1.5   3    1\n");
 
2136
  printf("    # Generated by triangle -pqc box.poly\n\n");
 
2137
  printf("  Here is the output file `box.1.ele', with twelve triangles.\n\n");
 
2138
  printf("    12  3  0\n");
 
2139
  printf("       1     5   6   9\n");
 
2140
  printf("       2    10   3   7\n");
 
2141
  printf("       3     6   8  12\n");
 
2142
  printf("       4     9   1   5\n");
 
2143
  printf("       5     6   2   9\n");
 
2144
  printf("       6     7   3  11\n");
 
2145
  printf("       7    11   4   8\n");
 
2146
  printf("       8     7   5  10\n");
 
2147
  printf("       9    12   2   6\n");
 
2148
  printf("      10     8   7  11\n");
 
2149
  printf("      11     5   1  10\n");
 
2150
  printf("      12     8   4  12\n");
 
2151
  printf("    # Generated by triangle -pqc box.poly\n\n");
 
2152
  printf(
 
2153
"  Here is the output file `box.1.poly'.  Note that segments have been added\n"
 
2154
);
 
2155
  printf(
 
2156
"  to represent the convex hull, and some segments have been split by newly\n"
 
2157
);
 
2158
  printf(
 
2159
"  added points.  Note also that <# of points> is set to zero to indicate\n");
 
2160
  printf("  that the points should be read from the .node file.\n\n");
 
2161
  printf("    0  2  0  1\n");
 
2162
  printf("    12  1\n");
 
2163
  printf("       1     1   9     5\n");
 
2164
  printf("       2     5   7     1\n");
 
2165
  printf("       3     8   7     1\n");
 
2166
  printf("       4     6   8    10\n");
 
2167
  printf("       5     5   6     1\n");
 
2168
  printf("       6     3  10     1\n");
 
2169
  printf("       7     4  11     1\n");
 
2170
  printf("       8     2  12     1\n");
 
2171
  printf("       9     9   2     5\n");
 
2172
  printf("      10    10   1     1\n");
 
2173
  printf("      11    11   3     1\n");
 
2174
  printf("      12    12   4     1\n");
 
2175
  printf("    1\n");
 
2176
  printf("       1   1.5 1.5\n");
 
2177
  printf("    # Generated by triangle -pqc box.poly\n\n");
 
2178
  printf("Refinement and Area Constraints:\n\n");
 
2179
  printf(
 
2180
"  The -r switch causes a mesh (.node and .ele files) to be read and\n");
 
2181
  printf(
 
2182
"  refined.  If the -p switch is also used, a .poly file is read and used to\n"
 
2183
);
 
2184
  printf(
 
2185
"  specify edges that are constrained and cannot be eliminated (although\n");
 
2186
  printf(
 
2187
"  they can be divided into smaller edges) by the refinement process.\n");
 
2188
  printf("\n");
 
2189
  printf(
 
2190
"  When you refine a mesh, you generally want to impose tighter quality\n");
 
2191
  printf(
 
2192
"  constraints.  One way to accomplish this is to use -q with a larger\n");
 
2193
  printf(
 
2194
"  angle, or -a followed by a smaller area than you used to generate the\n");
 
2195
  printf(
 
2196
"  mesh you are refining.  Another way to do this is to create an .area\n");
 
2197
  printf(
 
2198
"  file, which specifies a maximum area for each triangle, and use the -a\n");
 
2199
  printf(
 
2200
"  switch (without a number following).  Each triangle's area constraint is\n"
 
2201
);
 
2202
  printf(
 
2203
"  applied to that triangle.  Area constraints tend to diffuse as the mesh\n");
 
2204
  printf(
 
2205
"  is refined, so if there are large variations in area constraint between\n");
 
2206
  printf("  adjacent triangles, you may not get the results you want.\n\n");
 
2207
  printf(
 
2208
"  If you are refining a mesh composed of linear (three-node) elements, the\n"
 
2209
);
 
2210
  printf(
 
2211
"  output mesh will contain all the nodes present in the input mesh, in the\n"
 
2212
);
 
2213
  printf(
 
2214
"  same order, with new nodes added at the end of the .node file.  However,\n"
 
2215
);
 
2216
  printf(
 
2217
"  there is no guarantee that each output element is contained in a single\n");
 
2218
  printf(
 
2219
"  input element.  Often, output elements will overlap two input elements,\n");
 
2220
  printf(
 
2221
"  and input edges are not present in the output mesh.  Hence, a sequence of\n"
 
2222
);
 
2223
  printf(
 
2224
"  refined meshes will form a hierarchy of nodes, but not a hierarchy of\n");
 
2225
  printf(
 
2226
"  elements.  If you a refining a mesh of higher-order elements, the\n");
 
2227
  printf(
 
2228
"  hierarchical property applies only to the nodes at the corners of an\n");
 
2229
  printf("  element; other nodes may not be present in the refined mesh.\n\n");
 
2230
  printf(
 
2231
"  It is important to understand that maximum area constraints in .poly\n");
 
2232
  printf(
 
2233
"  files are handled differently from those in .area files.  A maximum area\n"
 
2234
);
 
2235
  printf(
 
2236
"  in a .poly file applies to the whole (segment-bounded) region in which a\n"
 
2237
);
 
2238
  printf(
 
2239
"  point falls, whereas a maximum area in an .area file applies to only one\n"
 
2240
);
 
2241
  printf(
 
2242
"  triangle.  Area constraints in .poly files are used only when a mesh is\n");
 
2243
  printf(
 
2244
"  first generated, whereas area constraints in .area files are used only to\n"
 
2245
);
 
2246
  printf(
 
2247
"  refine an existing mesh, and are typically based on a posteriori error\n");
 
2248
  printf(
 
2249
"  estimates resulting from a finite element simulation on that mesh.\n");
 
2250
  printf("\n");
 
2251
  printf(
 
2252
"  `triangle -rq25 object.1' will read object.1.node and object.1.ele, then\n"
 
2253
);
 
2254
  printf(
 
2255
"  refine the triangulation to enforce a 25 degree minimum angle, and then\n");
 
2256
  printf(
 
2257
"  write the refined triangulation to object.2.node and object.2.ele.\n");
 
2258
  printf("\n");
 
2259
  printf(
 
2260
"  `triangle -rpaa6.2 z.3' will read z.3.node, z.3.ele, z.3.poly, and\n");
 
2261
  printf(
 
2262
"  z.3.area.  After reconstructing the mesh and its segments, Triangle will\n"
 
2263
);
 
2264
  printf(
 
2265
"  refine the mesh so that no triangle has area greater than 6.2, and\n");
 
2266
  printf(
 
2267
"  furthermore the triangles satisfy the maximum area constraints in\n");
 
2268
  printf(
 
2269
"  z.3.area.  The output is written to z.4.node, z.4.ele, and z.4.poly.\n");
 
2270
  printf("\n");
 
2271
  printf(
 
2272
"  The sequence `triangle -qa1 x', `triangle -rqa.3 x.1', `triangle -rqa.1\n");
 
2273
  printf(
 
2274
"  x.2' creates a sequence of successively finer meshes x.1, x.2, and x.3,\n");
 
2275
  printf("  suitable for multigrid.\n\n");
 
2276
  printf("Convex Hulls and Mesh Boundaries:\n\n");
 
2277
  printf(
 
2278
"  If the input is a point set (rather than a PSLG), Triangle produces its\n");
 
2279
  printf(
 
2280
"  convex hull as a by-product in the output .poly file if you use the -c\n");
 
2281
  printf(
 
2282
"  switch.  There are faster algorithms for finding a two-dimensional convex\n"
 
2283
);
 
2284
  printf(
 
2285
"  hull than triangulation, of course, but this one comes for free.  If the\n"
 
2286
);
 
2287
  printf(
 
2288
"  input is an unconstrained mesh (you are using the -r switch but not the\n");
 
2289
  printf(
 
2290
"  -p switch), Triangle produces a list of its boundary edges (including\n");
 
2291
  printf("  hole boundaries) as a by-product if you use the -c switch.\n\n");
 
2292
  printf("Voronoi Diagrams:\n\n");
 
2293
  printf(
 
2294
"  The -v switch produces a Voronoi diagram, in files suffixed .v.node and\n");
 
2295
  printf(
 
2296
"  .v.edge.  For example, `triangle -v points' will read points.node,\n");
 
2297
  printf(
 
2298
"  produce its Delaunay triangulation in points.1.node and points.1.ele,\n");
 
2299
  printf(
 
2300
"  and produce its Voronoi diagram in points.1.v.node and points.1.v.edge.\n");
 
2301
  printf(
 
2302
"  The .v.node file contains a list of all Voronoi vertices, and the .v.edge\n"
 
2303
);
 
2304
  printf(
 
2305
"  file contains a list of all Voronoi edges, some of which may be infinite\n"
 
2306
);
 
2307
  printf(
 
2308
"  rays.  (The choice of filenames makes it easy to run the set of Voronoi\n");
 
2309
  printf("  vertices through Triangle, if so desired.)\n\n");
 
2310
  printf(
 
2311
"  This implementation does not use exact arithmetic to compute the Voronoi\n"
 
2312
);
 
2313
  printf(
 
2314
"  vertices, and does not check whether neighboring vertices are identical.\n"
 
2315
);
 
2316
  printf(
 
2317
"  Be forewarned that if the Delaunay triangulation is degenerate or\n");
 
2318
  printf(
 
2319
"  near-degenerate, the Voronoi diagram may have duplicate points, crossing\n"
 
2320
);
 
2321
  printf(
 
2322
"  edges, or infinite rays whose direction vector is zero.  Also, if you\n");
 
2323
  printf(
 
2324
"  generate a constrained (as opposed to conforming) Delaunay triangulation,\n"
 
2325
);
 
2326
  printf(
 
2327
"  or if the triangulation has holes, the corresponding Voronoi diagram is\n");
 
2328
  printf("  likely to have crossing edges and unlikely to make sense.\n\n");
 
2329
  printf("Mesh Topology:\n\n");
 
2330
  printf(
 
2331
"  You may wish to know which triangles are adjacent to a certain Delaunay\n");
 
2332
  printf(
 
2333
"  edge in an .edge file, which Voronoi regions are adjacent to a certain\n");
 
2334
  printf(
 
2335
"  Voronoi edge in a .v.edge file, or which Voronoi regions are adjacent to\n"
 
2336
);
 
2337
  printf(
 
2338
"  each other.  All of this information can be found by cross-referencing\n");
 
2339
  printf(
 
2340
"  output files with the recollection that the Delaunay triangulation and\n");
 
2341
  printf("  the Voronoi diagrams are planar duals.\n\n");
 
2342
  printf(
 
2343
"  Specifically, edge i of an .edge file is the dual of Voronoi edge i of\n");
 
2344
  printf(
 
2345
"  the corresponding .v.edge file, and is rotated 90 degrees counterclock-\n");
 
2346
  printf(
 
2347
"  wise from the Voronoi edge.  Triangle j of an .ele file is the dual of\n");
 
2348
  printf(
 
2349
"  vertex j of the corresponding .v.node file; and Voronoi region k is the\n");
 
2350
  printf("  dual of point k of the corresponding .node file.\n\n");
 
2351
  printf(
 
2352
"  Hence, to find the triangles adjacent to a Delaunay edge, look at the\n");
 
2353
  printf(
 
2354
"  vertices of the corresponding Voronoi edge; their dual triangles are on\n");
 
2355
  printf(
 
2356
"  the left and right of the Delaunay edge, respectively.  To find the\n");
 
2357
  printf(
 
2358
"  Voronoi regions adjacent to a Voronoi edge, look at the endpoints of the\n"
 
2359
);
 
2360
  printf(
 
2361
"  corresponding Delaunay edge; their dual regions are on the right and left\n"
 
2362
);
 
2363
  printf(
 
2364
"  of the Voronoi edge, respectively.  To find which Voronoi regions are\n");
 
2365
  printf("  adjacent to each other, just read the list of Delaunay edges.\n");
 
2366
  printf("\n");
 
2367
  printf("Statistics:\n");
 
2368
  printf("\n");
 
2369
  printf(
 
2370
"  After generating a mesh, Triangle prints a count of the number of points,\n"
 
2371
);
 
2372
  printf(
 
2373
"  triangles, edges, boundary edges, and segments in the output mesh.  If\n");
 
2374
  printf(
 
2375
"  you've forgotten the statistics for an existing mesh, the -rNEP switches\n"
 
2376
);
 
2377
  printf(
 
2378
"  (or -rpNEP if you've got a .poly file for the existing mesh) will\n");
 
2379
  printf("  regenerate these statistics without writing any output.\n\n");
 
2380
  printf(
 
2381
"  The -V switch produces extended statistics, including a rough estimate\n");
 
2382
  printf(
 
2383
"  of memory use and a histogram of triangle aspect ratios and angles in the\n"
 
2384
);
 
2385
  printf("  mesh.\n\n");
 
2386
  printf("Exact Arithmetic:\n\n");
 
2387
  printf(
 
2388
"  Triangle uses adaptive exact arithmetic to perform what computational\n");
 
2389
  printf(
 
2390
"  geometers call the `orientation' and `incircle' tests.  If the floating-\n"
 
2391
);
 
2392
  printf(
 
2393
"  point arithmetic of your machine conforms to the IEEE 754 standard (as\n");
 
2394
  printf(
 
2395
"  most workstations do), and does not use extended precision internal\n");
 
2396
  printf(
 
2397
"  registers, then your output is guaranteed to be an absolutely true\n");
 
2398
  printf("  Delaunay or conforming Delaunay triangulation, roundoff error\n");
 
2399
  printf(
 
2400
"  notwithstanding.  The word `adaptive' implies that these arithmetic\n");
 
2401
  printf(
 
2402
"  routines compute the result only to the precision necessary to guarantee\n"
 
2403
);
 
2404
  printf(
 
2405
"  correctness, so they are usually nearly as fast as their approximate\n");
 
2406
  printf(
 
2407
"  counterparts.  The exact tests can be disabled with the -X switch.  On\n");
 
2408
  printf(
 
2409
"  most inputs, this switch will reduce the computation time by about eight\n"
 
2410
);
 
2411
  printf(
 
2412
"  percent - it's not worth the risk.  There are rare difficult inputs\n");
 
2413
  printf(
 
2414
"  (having many collinear and cocircular points), however, for which the\n");
 
2415
  printf(
 
2416
"  difference could be a factor of two.  These are precisely the inputs most\n"
 
2417
);
 
2418
  printf("  likely to cause errors if you use the -X switch.\n\n");
 
2419
  printf(
 
2420
"  Unfortunately, these routines don't solve every numerical problem.  Exact\n"
 
2421
);
 
2422
  printf(
 
2423
"  arithmetic is not used to compute the positions of points, because the\n");
 
2424
  printf(
 
2425
"  bit complexity of point coordinates would grow without bound.  Hence,\n");
 
2426
  printf(
 
2427
"  segment intersections aren't computed exactly; in very unusual cases,\n");
 
2428
  printf(
 
2429
"  roundoff error in computing an intersection point might actually lead to\n"
 
2430
);
 
2431
  printf(
 
2432
"  an inverted triangle and an invalid triangulation.  (This is one reason\n");
 
2433
  printf(
 
2434
"  to compute your own intersection points in your .poly files.)  Similarly,\n"
 
2435
);
 
2436
  printf(
 
2437
"  exact arithmetic is not used to compute the vertices of the Voronoi\n");
 
2438
  printf("  diagram.\n\n");
 
2439
  printf(
 
2440
"  Underflow and overflow can also cause difficulties; the exact arithmetic\n"
 
2441
);
 
2442
  printf(
 
2443
"  routines do not ameliorate out-of-bounds exponents, which can arise\n");
 
2444
  printf(
 
2445
"  during the orientation and incircle tests.  As a rule of thumb, you\n");
 
2446
  printf(
 
2447
"  should ensure that your input values are within a range such that their\n");
 
2448
  printf(
 
2449
"  third powers can be taken without underflow or overflow.  Underflow can\n");
 
2450
  printf(
 
2451
"  silently prevent the tests from being performed exactly, while overflow\n");
 
2452
  printf("  will typically cause a floating exception.\n\n");
 
2453
  printf("Calling Triangle from Another Program:\n\n");
 
2454
  printf("  Read the file triangle.h for details.\n\n");
 
2455
  printf("Troubleshooting:\n\n");
 
2456
  printf("  Please read this section before mailing me bugs.\n\n");
 
2457
  printf("  `My output mesh has no triangles!'\n\n");
 
2458
  printf(
 
2459
"    If you're using a PSLG, you've probably failed to specify a proper set\n"
 
2460
);
 
2461
  printf(
 
2462
"    of bounding segments, or forgotten to use the -c switch.  Or you may\n");
 
2463
  printf(
 
2464
"    have placed a hole badly.  To test these possibilities, try again with\n"
 
2465
);
 
2466
  printf(
 
2467
"    the -c and -O switches.  Alternatively, all your input points may be\n");
 
2468
  printf(
 
2469
"    collinear, in which case you can hardly expect to triangulate them.\n");
 
2470
  printf("\n");
 
2471
  printf("  `Triangle doesn't terminate, or just crashes.'\n");
 
2472
  printf("\n");
 
2473
  printf(
 
2474
"    Bad things can happen when triangles get so small that the distance\n");
 
2475
  printf(
 
2476
"    between their vertices isn't much larger than the precision of your\n");
 
2477
  printf(
 
2478
"    machine's arithmetic.  If you've compiled Triangle for single-precision\n"
 
2479
);
 
2480
  printf(
 
2481
"    arithmetic, you might do better by recompiling it for double-precision.\n"
 
2482
);
 
2483
  printf(
 
2484
"    Then again, you might just have to settle for more lenient constraints\n"
 
2485
);
 
2486
  printf(
 
2487
"    on the minimum angle and the maximum area than you had planned.\n");
 
2488
  printf("\n");
 
2489
  printf(
 
2490
"    You can minimize precision problems by ensuring that the origin lies\n");
 
2491
  printf(
 
2492
"    inside your point set, or even inside the densest part of your\n");
 
2493
  printf(
 
2494
"    mesh.  On the other hand, if you're triangulating an object whose x\n");
 
2495
  printf(
 
2496
"    coordinates all fall between 6247133 and 6247134, you're not leaving\n");
 
2497
  printf("    much floating-point precision for Triangle to work with.\n\n");
 
2498
  printf(
 
2499
"    Precision problems can occur covertly if the input PSLG contains two\n");
 
2500
  printf(
 
2501
"    segments that meet (or intersect) at a very small angle, or if such an\n"
 
2502
);
 
2503
  printf(
 
2504
"    angle is introduced by the -c switch, which may occur if a point lies\n");
 
2505
  printf(
 
2506
"    ever-so-slightly inside the convex hull, and is connected by a PSLG\n");
 
2507
  printf(
 
2508
"    segment to a point on the convex hull.  If you don't realize that a\n");
 
2509
  printf(
 
2510
"    small angle is being formed, you might never discover why Triangle is\n");
 
2511
  printf(
 
2512
"    crashing.  To check for this possibility, use the -S switch (with an\n");
 
2513
  printf(
 
2514
"    appropriate limit on the number of Steiner points, found by trial-and-\n"
 
2515
);
 
2516
  printf(
 
2517
"    error) to stop Triangle early, and view the output .poly file with\n");
 
2518
  printf(
 
2519
"    Show Me (described below).  Look carefully for small angles between\n");
 
2520
  printf(
 
2521
"    segments; zoom in closely, as such segments might look like a single\n");
 
2522
  printf("    segment from a distance.\n\n");
 
2523
  printf(
 
2524
"    If some of the input values are too large, Triangle may suffer a\n");
 
2525
  printf(
 
2526
"    floating exception due to overflow when attempting to perform an\n");
 
2527
  printf(
 
2528
"    orientation or incircle test.  (Read the section on exact arithmetic\n");
 
2529
  printf(
 
2530
"    above.)  Again, I recommend compiling Triangle for double (rather\n");
 
2531
  printf("    than single) precision arithmetic.\n\n");
 
2532
  printf(
 
2533
"  `The numbering of the output points doesn't match the input points.'\n");
 
2534
  printf("\n");
 
2535
  printf(
 
2536
"    You may have eaten some of your input points with a hole, or by placing\n"
 
2537
);
 
2538
  printf("    them outside the area enclosed by segments.\n\n");
 
2539
  printf(
 
2540
"  `Triangle executes without incident, but when I look at the resulting\n");
 
2541
  printf(
 
2542
"  mesh, it has overlapping triangles or other geometric inconsistencies.'\n");
 
2543
  printf("\n");
 
2544
  printf(
 
2545
"    If you select the -X switch, Triangle's divide-and-conquer Delaunay\n");
 
2546
  printf(
 
2547
"    triangulation algorithm occasionally makes mistakes due to floating-\n");
 
2548
  printf(
 
2549
"    point roundoff error.  Although these errors are rare, don't use the -X\n"
 
2550
);
 
2551
  printf("    switch.  If you still have problems, please report the bug.\n");
 
2552
  printf("\n");
 
2553
  printf(
 
2554
"  Strange things can happen if you've taken liberties with your PSLG.  Do\n");
 
2555
  printf(
 
2556
"  you have a point lying in the middle of a segment?  Triangle sometimes\n");
 
2557
  printf(
 
2558
"  copes poorly with that sort of thing.  Do you want to lay out a collinear\n"
 
2559
);
 
2560
  printf(
 
2561
"  row of evenly spaced, segment-connected points?  Have you simply defined\n"
 
2562
);
 
2563
  printf(
 
2564
"  one long segment connecting the leftmost point to the rightmost point,\n");
 
2565
  printf(
 
2566
"  and a bunch of points lying along it?  This method occasionally works,\n");
 
2567
  printf(
 
2568
"  especially with horizontal and vertical lines, but often it doesn't, and\n"
 
2569
);
 
2570
  printf(
 
2571
"  you'll have to connect each adjacent pair of points with a separate\n");
 
2572
  printf("  segment.  If you don't like it, tough.\n\n");
 
2573
  printf(
 
2574
"  Furthermore, if you have segments that intersect other than at their\n");
 
2575
  printf(
 
2576
"  endpoints, try not to let the intersections fall extremely close to PSLG\n"
 
2577
);
 
2578
  printf("  points or each other.\n\n");
 
2579
  printf(
 
2580
"  If you have problems refining a triangulation not produced by Triangle:\n");
 
2581
  printf(
 
2582
"  Are you sure the triangulation is geometrically valid?  Is it formatted\n");
 
2583
  printf(
 
2584
"  correctly for Triangle?  Are the triangles all listed so the first three\n"
 
2585
);
 
2586
  printf("  points are their corners in counterclockwise order?\n\n");
 
2587
  printf("Show Me:\n\n");
 
2588
  printf(
 
2589
"  Triangle comes with a separate program named `Show Me', whose primary\n");
 
2590
  printf(
 
2591
"  purpose is to draw meshes on your screen or in PostScript.  Its secondary\n"
 
2592
);
 
2593
  printf(
 
2594
"  purpose is to check the validity of your input files, and do so more\n");
 
2595
  printf(
 
2596
"  thoroughly than Triangle does.  Show Me requires that you have the X\n");
 
2597
  printf(
 
2598
"  Windows system.  If you didn't receive Show Me with Triangle, complain to\n"
 
2599
);
 
2600
  printf("  whomever you obtained Triangle from, then send me mail.\n\n");
 
2601
  printf("Triangle on the Web:\n\n");
 
2602
  printf(
 
2603
"  To see an illustrated, updated version of these instructions, check out\n");
 
2604
  printf("\n");
 
2605
  printf("    http://www.cs.cmu.edu/~quake/triangle.html\n");
 
2606
  printf("\n");
 
2607
  printf("A Brief Plea:\n");
 
2608
  printf("\n");
 
2609
  printf(
 
2610
"  If you use Triangle, and especially if you use it to accomplish real\n");
 
2611
  printf(
 
2612
"  work, I would like very much to hear from you.  A short letter or email\n");
 
2613
  printf(
 
2614
"  (to jrs@cs.cmu.edu) describing how you use Triangle will mean a lot to\n");
 
2615
  printf(
 
2616
"  me.  The more people I know are using this program, the more easily I can\n"
 
2617
);
 
2618
  printf(
 
2619
"  justify spending time on improvements and on the three-dimensional\n");
 
2620
  printf(
 
2621
"  successor to Triangle, which in turn will benefit you.  Also, I can put\n");
 
2622
  printf(
 
2623
"  you on a list to receive email whenever a new version of Triangle is\n");
 
2624
  printf("  available.\n\n");
 
2625
  printf(
 
2626
"  If you use a mesh generated by Triangle in a publication, please include\n"
 
2627
);
 
2628
  printf("  an acknowledgment as well.\n\n");
 
2629
  printf("Research credit:\n\n");
 
2630
  printf(
 
2631
"  Of course, I can take credit for only a fraction of the ideas that made\n");
 
2632
  printf(
 
2633
"  this mesh generator possible.  Triangle owes its existence to the efforts\n"
 
2634
);
 
2635
  printf(
 
2636
"  of many fine computational geometers and other researchers, including\n");
 
2637
  printf(
 
2638
"  Marshall Bern, L. Paul Chew, Boris Delaunay, Rex A. Dwyer, David\n");
 
2639
  printf(
 
2640
"  Eppstein, Steven Fortune, Leonidas J. Guibas, Donald E. Knuth, C. L.\n");
 
2641
  printf(
 
2642
"  Lawson, Der-Tsai Lee, Ernst P. Mucke, Douglas M. Priest, Jim Ruppert,\n");
 
2643
  printf(
 
2644
"  Isaac Saias, Bruce J. Schachter, Micha Sharir, Jorge Stolfi, Christopher\n"
 
2645
);
 
2646
  printf(
 
2647
"  J. Van Wyk, David F. Watson, and Binhai Zhu.  See the comments at the\n");
 
2648
  printf("  beginning of the source code for references.\n\n");
 
2649
  exit(0);
 
2650
}
 
2651
 
 
2652
#endif /* not TRILIBRARY */
 
2653
 
 
2654
/*****************************************************************************/
 
2655
/*                                                                           */
 
2656
/*  internalerror()   Ask the user to send me the defective product.  Exit.  */
 
2657
/*                                                                           */
 
2658
/*****************************************************************************/
 
2659
 
 
2660
void internalerror()
 
2661
{
 
2662
  printf("  Please report this bug to jrs@cs.cmu.edu\n");
 
2663
  printf("  Include the message above, your input data set, and the exact\n");
 
2664
  printf("    command line you used to run Triangle.\n");
 
2665
  exit(1);
 
2666
}
 
2667
 
 
2668
 
 
2669
/*****************************************************************************/
 
2670
/*                                                                           */
 
2671
/*  parsecommandline()   Read the command line, identify switches, and set   */
 
2672
/*                       up options and file names.                          */
 
2673
/*                                                                           */
 
2674
/*  The effects of this routine are felt entirely through global variables.  */
 
2675
/*                                                                           */
 
2676
/*****************************************************************************/
 
2677
 
 
2678
void parsecommandline(int argc, char **argv)
 
2679
{
 
2680
#ifdef TRILIBRARY
 
2681
#define STARTINDEX 0
 
2682
#else /* not TRILIBRARY */
 
2683
#define STARTINDEX 1
 
2684
  int increment;
 
2685
  int meshnumber;
 
2686
#endif /* not TRILIBRARY */
 
2687
  int i, j, k;
 
2688
  char workstring[FILENAMESIZE];
 
2689
 
 
2690
  poly = refine = quality = vararea = fixedarea = regionattrib = convex = 0;
 
2691
  firstnumber = 1;
 
2692
  edgesout = voronoi = neighbors = geomview = 0;
 
2693
  nobound = nopolywritten = nonodewritten = noelewritten = noiterationnum = 0;
 
2694
  noholes = noexact = 0;
 
2695
  incremental = sweepline = 0;
 
2696
  dwyer = 1;
 
2697
  splitseg = 0;
 
2698
  docheck = 0;
 
2699
  nobisect = 0;
 
2700
  steiner = -1;
 
2701
  order = 1;
 
2702
  minangle = 0.0;
 
2703
  maxarea = -1.0;
 
2704
  quiet = verbose = 0;
 
2705
#ifndef TRILIBRARY
 
2706
  innodefilename[0] = '\0';
 
2707
#endif /* not TRILIBRARY */
 
2708
 
 
2709
  for (i = STARTINDEX; i < argc; i++) {
 
2710
#ifndef TRILIBRARY
 
2711
    if (argv[i][0] == '-') {
 
2712
#endif /* not TRILIBRARY */
 
2713
      for (j = STARTINDEX; argv[i][j] != '\0'; j++) {
 
2714
        if (argv[i][j] == 'p') {
 
2715
          poly = 1;
 
2716
        }
 
2717
#ifndef CDT_ONLY
 
2718
        if (argv[i][j] == 'r') {
 
2719
          refine = 1;
 
2720
        }
 
2721
        if (argv[i][j] == 'q') {
 
2722
          quality = 1;
 
2723
          if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
 
2724
              (argv[i][j + 1] == '.')) {
 
2725
            k = 0;
 
2726
            while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
 
2727
                   (argv[i][j + 1] == '.')) {
 
2728
              j++;
 
2729
              workstring[k] = argv[i][j];
 
2730
              k++;
 
2731
            }
 
2732
            workstring[k] = '\0';
 
2733
            minangle = (REAL) strtod(workstring, (char **) NULL);
 
2734
          } else {
 
2735
            minangle = 20.0;
 
2736
          }
 
2737
        }
 
2738
        if (argv[i][j] == 'a') {
 
2739
          quality = 1;
 
2740
          if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
 
2741
              (argv[i][j + 1] == '.')) {
 
2742
            fixedarea = 1;
 
2743
            k = 0;
 
2744
            while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
 
2745
                   (argv[i][j + 1] == '.')) {
 
2746
              j++;
 
2747
              workstring[k] = argv[i][j];
 
2748
              k++;
 
2749
            }
 
2750
            workstring[k] = '\0';
 
2751
            maxarea = (REAL) strtod(workstring, (char **) NULL);
 
2752
            if (maxarea <= 0.0) {
 
2753
              printf("Error:  Maximum area must be greater than zero.\n");
 
2754
              exit(1);
 
2755
            }
 
2756
          } else {
 
2757
            vararea = 1;
 
2758
          }
 
2759
        }
 
2760
#endif /* not CDT_ONLY */
 
2761
        if (argv[i][j] == 'A') {
 
2762
          regionattrib = 1;
 
2763
        }
 
2764
        if (argv[i][j] == 'c') {
 
2765
          convex = 1;
 
2766
        }
 
2767
        if (argv[i][j] == 'z') {
 
2768
          firstnumber = 0;
 
2769
        }
 
2770
        if (argv[i][j] == 'e') {
 
2771
          edgesout = 1;
 
2772
        }
 
2773
        if (argv[i][j] == 'v') {
 
2774
          voronoi = 1;
 
2775
        }
 
2776
        if (argv[i][j] == 'n') {
 
2777
          neighbors = 1;
 
2778
        }
 
2779
        if (argv[i][j] == 'g') {
 
2780
          geomview = 1;
 
2781
        }
 
2782
        if (argv[i][j] == 'B') {
 
2783
          nobound = 1;
 
2784
        }
 
2785
        if (argv[i][j] == 'P') {
 
2786
          nopolywritten = 1;
 
2787
        }
 
2788
        if (argv[i][j] == 'N') {
 
2789
          nonodewritten = 1;
 
2790
        }
 
2791
        if (argv[i][j] == 'E') {
 
2792
          noelewritten = 1;
 
2793
        }
 
2794
#ifndef TRILIBRARY
 
2795
        if (argv[i][j] == 'I') {
 
2796
          noiterationnum = 1;
 
2797
        }
 
2798
#endif /* not TRILIBRARY */
 
2799
        if (argv[i][j] == 'O') {
 
2800
          noholes = 1;
 
2801
        }
 
2802
        if (argv[i][j] == 'X') {
 
2803
          noexact = 1;
 
2804
        }
 
2805
        if (argv[i][j] == 'o') {
 
2806
          if (argv[i][j + 1] == '2') {
 
2807
            j++;
 
2808
            order = 2;
 
2809
          }
 
2810
        }
 
2811
#ifndef CDT_ONLY
 
2812
        if (argv[i][j] == 'Y') {
 
2813
          nobisect++;
 
2814
        }
 
2815
        if (argv[i][j] == 'S') {
 
2816
          steiner = 0;
 
2817
          while ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) {
 
2818
            j++;
 
2819
            steiner = steiner * 10 + (int) (argv[i][j] - '0');
 
2820
          }
 
2821
        }
 
2822
#endif /* not CDT_ONLY */
 
2823
#ifndef REDUCED
 
2824
        if (argv[i][j] == 'i') {
 
2825
          incremental = 1;
 
2826
        }
 
2827
        if (argv[i][j] == 'F') {
 
2828
          sweepline = 1;
 
2829
        }
 
2830
#endif /* not REDUCED */
 
2831
        if (argv[i][j] == 'l') {
 
2832
          dwyer = 0;
 
2833
        }
 
2834
#ifndef REDUCED
 
2835
#ifndef CDT_ONLY
 
2836
        if (argv[i][j] == 's') {
 
2837
          splitseg = 1;
 
2838
        }
 
2839
#endif /* not CDT_ONLY */
 
2840
        if (argv[i][j] == 'C') {
 
2841
          docheck = 1;
 
2842
        }
 
2843
#endif /* not REDUCED */
 
2844
        if (argv[i][j] == 'Q') {
 
2845
          quiet = 1;
 
2846
        }
 
2847
        if (argv[i][j] == 'V') {
 
2848
          verbose++;
 
2849
        }
 
2850
#ifndef TRILIBRARY
 
2851
        if ((argv[i][j] == 'h') || (argv[i][j] == 'H') ||
 
2852
            (argv[i][j] == '?')) {
 
2853
          info();
 
2854
        }
 
2855
#endif /* not TRILIBRARY */
 
2856
      }
 
2857
#ifndef TRILIBRARY
 
2858
    } else {
 
2859
      strncpy(innodefilename, argv[i], FILENAMESIZE - 1);
 
2860
      innodefilename[FILENAMESIZE - 1] = '\0';
 
2861
    }
 
2862
#endif /* not TRILIBRARY */
 
2863
  }
 
2864
#ifndef TRILIBRARY
 
2865
  if (innodefilename[0] == '\0') {
 
2866
    syntax();
 
2867
  }
 
2868
  if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".node")) {
 
2869
    innodefilename[strlen(innodefilename) - 5] = '\0';
 
2870
  }
 
2871
  if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".poly")) {
 
2872
    innodefilename[strlen(innodefilename) - 5] = '\0';
 
2873
    poly = 1;
 
2874
  }
 
2875
#ifndef CDT_ONLY
 
2876
  if (!strcmp(&innodefilename[strlen(innodefilename) - 4], ".ele")) {
 
2877
    innodefilename[strlen(innodefilename) - 4] = '\0';
 
2878
    refine = 1;
 
2879
  }
 
2880
  if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".area")) {
 
2881
    innodefilename[strlen(innodefilename) - 5] = '\0';
 
2882
    refine = 1;
 
2883
    quality = 1;
 
2884
    vararea = 1;
 
2885
  }
 
2886
#endif /* not CDT_ONLY */
 
2887
#endif /* not TRILIBRARY */
 
2888
  steinerleft = steiner;
 
2889
  useshelles = poly || refine || quality || convex;
 
2890
  goodangle = cos(minangle * PI / 180.0);
 
2891
  goodangle *= goodangle;
 
2892
  if (refine && noiterationnum) {
 
2893
    printf(
 
2894
      "Error:  You cannot use the -I switch when refining a triangulation.\n");
 
2895
    exit(1);
 
2896
  }
 
2897
  /* Be careful not to allocate space for element area constraints that */
 
2898
  /*   will never be assigned any value (other than the default -1.0).  */
 
2899
  if (!refine && !poly) {
 
2900
    vararea = 0;
 
2901
  }
 
2902
  /* Be careful not to add an extra attribute to each element unless the */
 
2903
  /*   input supports it (PSLG in, but not refining a preexisting mesh). */
 
2904
  if (refine || !poly) {
 
2905
    regionattrib = 0;
 
2906
  }
 
2907
 
 
2908
#ifndef TRILIBRARY
 
2909
  strcpy(inpolyfilename, innodefilename);
 
2910
  strcpy(inelefilename, innodefilename);
 
2911
  strcpy(areafilename, innodefilename);
 
2912
  increment = 0;
 
2913
  strcpy(workstring, innodefilename);
 
2914
  j = 1;
 
2915
  while (workstring[j] != '\0') {
 
2916
    if ((workstring[j] == '.') && (workstring[j + 1] != '\0')) {
 
2917
      increment = j + 1;
 
2918
    }
 
2919
    j++;
 
2920
  }
 
2921
  meshnumber = 0;
 
2922
  if (increment > 0) {
 
2923
    j = increment;
 
2924
    do {
 
2925
      if ((workstring[j] >= '0') && (workstring[j] <= '9')) {
 
2926
        meshnumber = meshnumber * 10 + (int) (workstring[j] - '0');
 
2927
      } else {
 
2928
        increment = 0;
 
2929
      }
 
2930
      j++;
 
2931
    } while (workstring[j] != '\0');
 
2932
  }
 
2933
  if (noiterationnum) {
 
2934
    strcpy(outnodefilename, innodefilename);
 
2935
    strcpy(outelefilename, innodefilename);
 
2936
    strcpy(edgefilename, innodefilename);
 
2937
    strcpy(vnodefilename, innodefilename);
 
2938
    strcpy(vedgefilename, innodefilename);
 
2939
    strcpy(neighborfilename, innodefilename);
 
2940
    strcpy(offfilename, innodefilename);
 
2941
    strcat(outnodefilename, ".node");
 
2942
    strcat(outelefilename, ".ele");
 
2943
    strcat(edgefilename, ".edge");
 
2944
    strcat(vnodefilename, ".v.node");
 
2945
    strcat(vedgefilename, ".v.edge");
 
2946
    strcat(neighborfilename, ".neigh");
 
2947
    strcat(offfilename, ".off");
 
2948
  } else if (increment == 0) {
 
2949
    strcpy(outnodefilename, innodefilename);
 
2950
    strcpy(outpolyfilename, innodefilename);
 
2951
    strcpy(outelefilename, innodefilename);
 
2952
    strcpy(edgefilename, innodefilename);
 
2953
    strcpy(vnodefilename, innodefilename);
 
2954
    strcpy(vedgefilename, innodefilename);
 
2955
    strcpy(neighborfilename, innodefilename);
 
2956
    strcpy(offfilename, innodefilename);
 
2957
    strcat(outnodefilename, ".1.node");
 
2958
    strcat(outpolyfilename, ".1.poly");
 
2959
    strcat(outelefilename, ".1.ele");
 
2960
    strcat(edgefilename, ".1.edge");
 
2961
    strcat(vnodefilename, ".1.v.node");
 
2962
    strcat(vedgefilename, ".1.v.edge");
 
2963
    strcat(neighborfilename, ".1.neigh");
 
2964
    strcat(offfilename, ".1.off");
 
2965
  } else {
 
2966
    workstring[increment] = '%';
 
2967
    workstring[increment + 1] = 'd';
 
2968
    workstring[increment + 2] = '\0';
 
2969
    sprintf(outnodefilename, workstring, meshnumber + 1);
 
2970
    strcpy(outpolyfilename, outnodefilename);
 
2971
    strcpy(outelefilename, outnodefilename);
 
2972
    strcpy(edgefilename, outnodefilename);
 
2973
    strcpy(vnodefilename, outnodefilename);
 
2974
    strcpy(vedgefilename, outnodefilename);
 
2975
    strcpy(neighborfilename, outnodefilename);
 
2976
    strcpy(offfilename, outnodefilename);
 
2977
    strcat(outnodefilename, ".node");
 
2978
    strcat(outpolyfilename, ".poly");
 
2979
    strcat(outelefilename, ".ele");
 
2980
    strcat(edgefilename, ".edge");
 
2981
    strcat(vnodefilename, ".v.node");
 
2982
    strcat(vedgefilename, ".v.edge");
 
2983
    strcat(neighborfilename, ".neigh");
 
2984
    strcat(offfilename, ".off");
 
2985
  }
 
2986
  strcat(innodefilename, ".node");
 
2987
  strcat(inpolyfilename, ".poly");
 
2988
  strcat(inelefilename, ".ele");
 
2989
  strcat(areafilename, ".area");
 
2990
#endif /* not TRILIBRARY */
 
2991
}
 
2992
 
 
2993
/**                                                                         **/
 
2994
/**                                                                         **/
 
2995
/********* User interaction routines begin here                      *********/
 
2996
 
 
2997
/********* Debugging routines begin here                             *********/
 
2998
/**                                                                         **/
 
2999
/**                                                                         **/
 
3000
 
 
3001
/*****************************************************************************/
 
3002
/*                                                                           */
 
3003
/*  printtriangle()   Print out the details of a triangle/edge handle.       */
 
3004
/*                                                                           */
 
3005
/*  I originally wrote this procedure to simplify debugging; it can be       */
 
3006
/*  called directly from the debugger, and presents information about a      */
 
3007
/*  triangle/edge handle in digestible form.  It's also used when the        */
 
3008
/*  highest level of verbosity (`-VVV') is specified.                        */
 
3009
/*                                                                           */
 
3010
/*****************************************************************************/
 
3011
 
 
3012
void printtriangle(triedge *t)
 
3013
{
 
3014
  struct triedge printtri;
 
3015
  struct edge printsh;
 
3016
  point printpoint;
 
3017
 
 
3018
  printf("triangle x%lx with orientation %d:\n", (unsigned long) t->tri,
 
3019
         t->orient);
 
3020
  decode(t->tri[0], printtri);
 
3021
  if (printtri.tri == dummytri) {
 
3022
    printf("    [0] = Outer space\n");
 
3023
  } else {
 
3024
    printf("    [0] = x%lx  %d\n", (unsigned long) printtri.tri,
 
3025
           printtri.orient);
 
3026
  }
 
3027
  decode(t->tri[1], printtri);
 
3028
  if (printtri.tri == dummytri) {
 
3029
    printf("    [1] = Outer space\n");
 
3030
  } else {
 
3031
    printf("    [1] = x%lx  %d\n", (unsigned long) printtri.tri,
 
3032
           printtri.orient);
 
3033
  }
 
3034
  decode(t->tri[2], printtri);
 
3035
  if (printtri.tri == dummytri) {
 
3036
    printf("    [2] = Outer space\n");
 
3037
  } else {
 
3038
    printf("    [2] = x%lx  %d\n", (unsigned long) printtri.tri,
 
3039
           printtri.orient);
 
3040
  }
 
3041
  org(*t, printpoint);
 
3042
  if (printpoint == (point) NULL)
 
3043
    printf("    Origin[%d] = NULL\n", (t->orient + 1) % 3 + 3);
 
3044
  else
 
3045
    printf("    Origin[%d] = x%lx  (%.12g, %.12g)\n",
 
3046
           (t->orient + 1) % 3 + 3, (unsigned long) printpoint,
 
3047
           printpoint[0], printpoint[1]);
 
3048
  dest(*t, printpoint);
 
3049
  if (printpoint == (point) NULL)
 
3050
    printf("    Dest  [%d] = NULL\n", (t->orient + 2) % 3 + 3);
 
3051
  else
 
3052
    printf("    Dest  [%d] = x%lx  (%.12g, %.12g)\n",
 
3053
           (t->orient + 2) % 3 + 3, (unsigned long) printpoint,
 
3054
           printpoint[0], printpoint[1]);
 
3055
  apex(*t, printpoint);
 
3056
  if (printpoint == (point) NULL)
 
3057
    printf("    Apex  [%d] = NULL\n", t->orient + 3);
 
3058
  else
 
3059
    printf("    Apex  [%d] = x%lx  (%.12g, %.12g)\n",
 
3060
           t->orient + 3, (unsigned long) printpoint,
 
3061
           printpoint[0], printpoint[1]);
 
3062
  if (useshelles) {
 
3063
    sdecode(t->tri[6], printsh);
 
3064
    if (printsh.sh != dummysh) {
 
3065
      printf("    [6] = x%lx  %d\n", (unsigned long) printsh.sh,
 
3066
             printsh.shorient);
 
3067
    }
 
3068
    sdecode(t->tri[7], printsh);
 
3069
    if (printsh.sh != dummysh) {
 
3070
      printf("    [7] = x%lx  %d\n", (unsigned long) printsh.sh,
 
3071
             printsh.shorient);
 
3072
    }
 
3073
    sdecode(t->tri[8], printsh);
 
3074
    if (printsh.sh != dummysh) {
 
3075
      printf("    [8] = x%lx  %d\n", (unsigned long) printsh.sh,
 
3076
             printsh.shorient);
 
3077
    }
 
3078
  }
 
3079
  if (vararea) {
 
3080
    printf("    Area constraint:  %.4g\n", areabound(*t));
 
3081
  }
 
3082
}
 
3083
 
 
3084
/*****************************************************************************/
 
3085
/*                                                                           */
 
3086
/*  printshelle()   Print out the details of a shell edge handle.            */
 
3087
/*                                                                           */
 
3088
/*  I originally wrote this procedure to simplify debugging; it can be       */
 
3089
/*  called directly from the debugger, and presents information about a      */
 
3090
/*  shell edge handle in digestible form.  It's also used when the highest   */
 
3091
/*  level of verbosity (`-VVV') is specified.                                */
 
3092
/*                                                                           */
 
3093
/*****************************************************************************/
 
3094
 
 
3095
void printshelle(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(
 
3175
                                struct memorypool *pool,
 
3176
                                int bytecount,
 
3177
                                int itemcount,
 
3178
                                enum wordtype wtype,
 
3179
                                int alignment
 
3180
                         )
 
3181
{
 
3182
  int wordsize;
 
3183
 
 
3184
  /* Initialize values in the pool. */
 
3185
  pool->itemwordtype = wtype;
 
3186
  wordsize = (pool->itemwordtype == POINTER) ? sizeof(VOID *) : sizeof(REAL);
 
3187
  /* Find the proper alignment, which must be at least as large as:   */
 
3188
  /*   - The parameter `alignment'.                                   */
 
3189
  /*   - The primary word type, to avoid unaligned accesses.          */
 
3190
  /*   - sizeof(VOID *), so the stack of dead items can be maintained */
 
3191
  /*       without unaligned accesses.                                */
 
3192
  if (alignment > wordsize) {
 
3193
    pool->alignbytes = alignment;
 
3194
  } else {
 
3195
    pool->alignbytes = wordsize;
 
3196
  }
 
3197
  if (sizeof(VOID *) > pool->alignbytes) {
 
3198
    pool->alignbytes = sizeof(VOID *);
 
3199
  }
 
3200
  pool->itemwords = ((bytecount + pool->alignbytes - 1) / pool->alignbytes)
 
3201
                  * (pool->alignbytes / wordsize);
 
3202
  pool->itembytes = pool->itemwords * wordsize;
 
3203
  pool->itemsperblock = itemcount;
 
3204
 
 
3205
  /* Allocate a block of items.  Space for `itemsperblock' items and one    */
 
3206
  /*   pointer (to point to the next block) are allocated, as well as space */
 
3207
  /*   to ensure alignment of the items.                                    */
 
3208
  pool->firstblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
 
3209
                                      + sizeof(VOID *) + pool->alignbytes);
 
3210
  if (pool->firstblock == (VOID **) NULL) {
 
3211
    printf("Error:  Out of memory.\n");
 
3212
    exit(1);
 
3213
  }
 
3214
  /* Set the next block pointer to NULL. */
 
3215
  *(pool->firstblock) = (VOID *) NULL;
 
3216
  poolrestart(pool);
 
3217
}
 
3218
 
 
3219
/*****************************************************************************/
 
3220
/*                                                                           */
 
3221
/*  poolrestart()   Deallocate all items in a pool.                          */
 
3222
/*                                                                           */
 
3223
/*  The pool is returned to its starting state, except that no memory is     */
 
3224
/*  freed to the operating system.  Rather, the previously allocated blocks  */
 
3225
/*  are ready to be reused.                                                  */
 
3226
/*                                                                           */
 
3227
/*****************************************************************************/
 
3228
 
 
3229
void poolrestart(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(struct memorypool *pool)
 
3257
{
 
3258
  while (pool->firstblock != (VOID **) NULL) {
 
3259
    pool->nowblock = (VOID **) *(pool->firstblock);
 
3260
    free(pool->firstblock);
 
3261
    pool->firstblock = pool->nowblock;
 
3262
  }
 
3263
}
 
3264
 
 
3265
/*****************************************************************************/
 
3266
/*                                                                           */
 
3267
/*  poolalloc()   Allocate space for an item.                                */
 
3268
/*                                                                           */
 
3269
/*****************************************************************************/
 
3270
 
 
3271
VOID *poolalloc(struct memorypool *pool)
 
3272
{
 
3273
  VOID *newitem;
 
3274
  VOID **newblock;
 
3275
  unsigned long alignptr;
 
3276
 
 
3277
  /* First check the linked list of dead items.  If the list is not   */
 
3278
  /*   empty, allocate an item from the list rather than a fresh one. */
 
3279
  if (pool->deaditemstack != (VOID *) NULL) {
 
3280
    newitem = pool->deaditemstack;               /* Take first item in list. */
 
3281
    pool->deaditemstack = * (VOID **) pool->deaditemstack;
 
3282
  } else {
 
3283
    /* Check if there are any free items left in the current block. */
 
3284
    if (pool->unallocateditems == 0) {
 
3285
      /* Check if another block must be allocated. */
 
3286
      if (*(pool->nowblock) == (VOID *) NULL) {
 
3287
        /* Allocate a new block of items, pointed to by the previous block. */
 
3288
        newblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
 
3289
                                    + sizeof(VOID *) + pool->alignbytes);
 
3290
        if (newblock == (VOID **) NULL) {
 
3291
          printf("Error:  Out of memory.\n");
 
3292
          exit(1);
 
3293
        }
 
3294
        *(pool->nowblock) = (VOID *) newblock;
 
3295
        /* The next block pointer is NULL. */
 
3296
        *newblock = (VOID *) NULL;
 
3297
      }
 
3298
      /* Move to the new block. */
 
3299
      pool->nowblock = (VOID **) *(pool->nowblock);
 
3300
      /* Find the first item in the block.    */
 
3301
      /*   Increment by the size of (VOID *). */
 
3302
      alignptr = (unsigned long) (pool->nowblock + 1);
 
3303
      /* Align the item on an `alignbytes'-byte boundary. */
 
3304
      pool->nextitem = (VOID *)
 
3305
        (alignptr + (unsigned long) pool->alignbytes
 
3306
         - (alignptr % (unsigned long) pool->alignbytes));
 
3307
      /* There are lots of unallocated items left in this block. */
 
3308
      pool->unallocateditems = pool->itemsperblock;
 
3309
    }
 
3310
    /* Allocate a new item. */
 
3311
    newitem = pool->nextitem;
 
3312
    /* Advance `nextitem' pointer to next free item in block. */
 
3313
    if (pool->itemwordtype == POINTER) {
 
3314
      pool->nextitem = (VOID *) ((VOID **) pool->nextitem + pool->itemwords);
 
3315
    } else {
 
3316
      pool->nextitem = (VOID *) ((REAL *) pool->nextitem + pool->itemwords);
 
3317
    }
 
3318
    pool->unallocateditems--;
 
3319
    pool->maxitems++;
 
3320
  }
 
3321
  pool->items++;
 
3322
  return newitem;
 
3323
}
 
3324
 
 
3325
/*****************************************************************************/
 
3326
/*                                                                           */
 
3327
/*  pooldealloc()   Deallocate space for an item.                            */
 
3328
/*                                                                           */
 
3329
/*  The deallocated space is stored in a queue for later reuse.              */
 
3330
/*                                                                           */
 
3331
/*****************************************************************************/
 
3332
 
 
3333
void pooldealloc(struct memorypool *pool, VOID *dyingitem)
 
3334
{
 
3335
  /* Push freshly killed item onto stack. */
 
3336
  *((VOID **) dyingitem) = pool->deaditemstack;
 
3337
  pool->deaditemstack = dyingitem;
 
3338
  pool->items--;
 
3339
}
 
3340
 
 
3341
/*****************************************************************************/
 
3342
/*                                                                           */
 
3343
/*  traversalinit()   Prepare to traverse the entire list of items.          */
 
3344
/*                                                                           */
 
3345
/*  This routine is used in conjunction with traverse().                     */
 
3346
/*                                                                           */
 
3347
/*****************************************************************************/
 
3348
 
 
3349
void traversalinit(struct memorypool *pool)
 
3350
{
 
3351
  unsigned long alignptr;
 
3352
 
 
3353
  /* Begin the traversal in the first block. */
 
3354
  pool->pathblock = pool->firstblock;
 
3355
  /* Find the first item in the block.  Increment by the size of (VOID *). */
 
3356
  alignptr = (unsigned long) (pool->pathblock + 1);
 
3357
  /* Align with item on an `alignbytes'-byte boundary. */
 
3358
  pool->pathitem = (VOID *)
 
3359
    (alignptr + (unsigned long) pool->alignbytes
 
3360
     - (alignptr % (unsigned long) pool->alignbytes));
 
3361
  /* Set the number of items left in the current block. */
 
3362
  pool->pathitemsleft = pool->itemsperblock;
 
3363
}
 
3364
 
 
3365
/*****************************************************************************/
 
3366
/*                                                                           */
 
3367
/*  traverse()   Find the next item in the list.                             */
 
3368
/*                                                                           */
 
3369
/*  This routine is used in conjunction with traversalinit().  Be forewarned */
 
3370
/*  that this routine successively returns all items in the list, including  */
 
3371
/*  deallocated ones on the deaditemqueue.  It's up to you to figure out     */
 
3372
/*  which ones are actually dead.  Why?  I don't want to allocate extra      */
 
3373
/*  space just to demarcate dead items.  It can usually be done more         */
 
3374
/*  space-efficiently by a routine that knows something about the structure  */
 
3375
/*  of the item.                                                             */
 
3376
/*                                                                           */
 
3377
/*****************************************************************************/
 
3378
 
 
3379
VOID *traverse(struct memorypool *pool)
 
3380
{
 
3381
  VOID *newitem;
 
3382
  unsigned long alignptr;
 
3383
 
 
3384
  /* Stop upon exhausting the list of items. */
 
3385
  if (pool->pathitem == pool->nextitem) {
 
3386
    return (VOID *) NULL;
 
3387
  }
 
3388
  /* Check whether any untraversed items remain in the current block. */
 
3389
  if (pool->pathitemsleft == 0) {
 
3390
    /* Find the next block. */
 
3391
    pool->pathblock = (VOID **) *(pool->pathblock);
 
3392
    /* Find the first item in the block.  Increment by the size of (VOID *). */
 
3393
    alignptr = (unsigned long) (pool->pathblock + 1);
 
3394
    /* Align with item on an `alignbytes'-byte boundary. */
 
3395
    pool->pathitem = (VOID *)
 
3396
      (alignptr + (unsigned long) pool->alignbytes
 
3397
       - (alignptr % (unsigned long) pool->alignbytes));
 
3398
    /* Set the number of items left in the current block. */
 
3399
    pool->pathitemsleft = pool->itemsperblock;
 
3400
  }
 
3401
  newitem = pool->pathitem;
 
3402
  /* Find the next item in the block. */
 
3403
  if (pool->itemwordtype == POINTER) {
 
3404
    pool->pathitem = (VOID *) ((VOID **) pool->pathitem + pool->itemwords);
 
3405
  } else {
 
3406
    pool->pathitem = (VOID *) ((REAL *) pool->pathitem + pool->itemwords);
 
3407
  }
 
3408
  pool->pathitemsleft--;
 
3409
  return newitem;
 
3410
}
 
3411
 
 
3412
/*****************************************************************************/
 
3413
/*                                                                           */
 
3414
/*  dummyinit()   Initialize the triangle that fills "outer space" and the   */
 
3415
/*                omnipresent shell edge.                                    */
 
3416
/*                                                                           */
 
3417
/*  The triangle that fills "outer space", called `dummytri', is pointed to  */
 
3418
/*  by every triangle and shell edge on a boundary (be it outer or inner) of */
 
3419
/*  the triangulation.  Also, `dummytri' points to one of the triangles on   */
 
3420
/*  the convex hull (until the holes and concavities are carved), making it  */
 
3421
/*  possible to find a starting triangle for point location.                 */
 
3422
/*                                                                           */
 
3423
/*  The omnipresent shell edge, `dummysh', is pointed to by every triangle   */
 
3424
/*  or shell edge that doesn't have a full complement of real shell edges    */
 
3425
/*  to point to.                                                             */
 
3426
/*                                                                           */
 
3427
/*****************************************************************************/
 
3428
 
 
3429
void dummyinit(int trianglewords, int shellewords)
 
3430
{
 
3431
  unsigned long alignptr;
 
3432
 
 
3433
  /* `triwords' and `shwords' are used by the mesh manipulation primitives */
 
3434
  /*   to extract orientations of triangles and shell edges from pointers. */
 
3435
  triwords = trianglewords;       /* Initialize `triwords' once and for all. */
 
3436
  shwords = shellewords;           /* Initialize `shwords' once and for all. */
 
3437
 
 
3438
  /* Set up `dummytri', the `triangle' that occupies "outer space". */
 
3439
  dummytribase = (triangle *) malloc(triwords * sizeof(triangle)
 
3440
                                     + triangles.alignbytes);
 
3441
  if (dummytribase == (triangle *) NULL) {
 
3442
    printf("Error:  Out of memory.\n");
 
3443
    exit(1);
 
3444
  }
 
3445
  /* Align `dummytri' on a `triangles.alignbytes'-byte boundary. */
 
3446
  alignptr = (unsigned long) dummytribase;
 
3447
  dummytri = (triangle *)
 
3448
    (alignptr + (unsigned long) triangles.alignbytes
 
3449
     - (alignptr % (unsigned long) triangles.alignbytes));
 
3450
  /* Initialize the three adjoining triangles to be "outer space".  These  */
 
3451
  /*   will eventually be changed by various bonding operations, but their */
 
3452
  /*   values don't really matter, as long as they can legally be          */
 
3453
  /*   dereferenced.                                                       */
 
3454
  dummytri[0] = (triangle) dummytri;
 
3455
  dummytri[1] = (triangle) dummytri;
 
3456
  dummytri[2] = (triangle) dummytri;
 
3457
  /* Three NULL vertex points. */
 
3458
  dummytri[3] = (triangle) NULL;
 
3459
  dummytri[4] = (triangle) NULL;
 
3460
  dummytri[5] = (triangle) NULL;
 
3461
 
 
3462
  if (useshelles) {
 
3463
    /* Set up `dummysh', the omnipresent "shell edge" pointed to by any      */
 
3464
    /*   triangle side or shell edge end that isn't attached to a real shell */
 
3465
    /*   edge.                                                               */
 
3466
    dummyshbase = (shelle *) malloc(shwords * sizeof(shelle)
 
3467
                                    + shelles.alignbytes);
 
3468
    if (dummyshbase == (shelle *) NULL) {
 
3469
      printf("Error:  Out of memory.\n");
 
3470
      exit(1);
 
3471
    }
 
3472
    /* Align `dummysh' on a `shelles.alignbytes'-byte boundary. */
 
3473
    alignptr = (unsigned long) dummyshbase;
 
3474
    dummysh = (shelle *)
 
3475
      (alignptr + (unsigned long) shelles.alignbytes
 
3476
       - (alignptr % (unsigned long) shelles.alignbytes));
 
3477
    /* Initialize the two adjoining shell edges to be the omnipresent shell */
 
3478
    /*   edge.  These will eventually be changed by various bonding         */
 
3479
    /*   operations, but their values don't really matter, as long as they  */
 
3480
    /*   can legally be dereferenced.                                       */
 
3481
    dummysh[0] = (shelle) dummysh;
 
3482
    dummysh[1] = (shelle) dummysh;
 
3483
    /* Two NULL vertex points. */
 
3484
    dummysh[2] = (shelle) NULL;
 
3485
    dummysh[3] = (shelle) NULL;
 
3486
    /* Initialize the two adjoining triangles to be "outer space". */
 
3487
    dummysh[4] = (shelle) dummytri;
 
3488
    dummysh[5] = (shelle) dummytri;
 
3489
    /* Set the boundary marker to zero. */
 
3490
    * (int *) (dummysh + 6) = 0;
 
3491
 
 
3492
    /* Initialize the three adjoining shell edges of `dummytri' to be */
 
3493
    /*   the omnipresent shell edge.                                  */
 
3494
    dummytri[6] = (triangle) dummysh;
 
3495
    dummytri[7] = (triangle) dummysh;
 
3496
    dummytri[8] = (triangle) dummysh;
 
3497
  }
 
3498
}
 
3499
 
 
3500
/*****************************************************************************/
 
3501
/*                                                                           */
 
3502
/*  initializepointpool()   Calculate the size of the point data structure   */
 
3503
/*                          and initialize its memory pool.                  */
 
3504
/*                                                                           */
 
3505
/*  This routine also computes the `pointmarkindex' and `point2triindex'     */
 
3506
/*  indices used to find values within each point.                           */
 
3507
/*                                                                           */
 
3508
/*****************************************************************************/
 
3509
 
 
3510
void initializepointpool()
 
3511
{
 
3512
  int pointsize;
 
3513
 
 
3514
  /* The index within each point at which the boundary marker is found.  */
 
3515
  /*   Ensure the point marker is aligned to a sizeof(int)-byte address. */
 
3516
  pointmarkindex = ((mesh_dim + nextras) * sizeof(REAL) + sizeof(int) - 1)
 
3517
                 / sizeof(int);
 
3518
  pointsize = (pointmarkindex + 1) * sizeof(int);
 
3519
  if (poly) {
 
3520
    /* The index within each point at which a triangle pointer is found.   */
 
3521
    /*   Ensure the pointer is aligned to a sizeof(triangle)-byte address. */
 
3522
    point2triindex = (pointsize + sizeof(triangle) - 1) / sizeof(triangle);
 
3523
    pointsize = (point2triindex + 1) * sizeof(triangle);
 
3524
  }
 
3525
  /* Initialize the pool of points. */
 
3526
  poolinit(&points, pointsize, POINTPERBLOCK,
 
3527
           (sizeof(REAL) >= sizeof(triangle)) ? FLOATINGPOINT : POINTER, 0);
 
3528
}
 
3529
 
 
3530
/*****************************************************************************/
 
3531
/*                                                                           */
 
3532
/*  initializetrisegpools()   Calculate the sizes of the triangle and shell  */
 
3533
/*                            edge data structures and initialize their      */
 
3534
/*                            memory pools.                                  */
 
3535
/*                                                                           */
 
3536
/*  This routine also computes the `highorderindex', `elemattribindex', and  */
 
3537
/*  `areaboundindex' indices used to find values within each triangle.       */
 
3538
/*                                                                           */
 
3539
/*****************************************************************************/
 
3540
 
 
3541
void initializetrisegpools()
 
3542
{
 
3543
  int trisize;
 
3544
 
 
3545
  /* The index within each triangle at which the extra nodes (above three)  */
 
3546
  /*   associated with high order elements are found.  There are three      */
 
3547
  /*   pointers to other triangles, three pointers to corners, and possibly */
 
3548
  /*   three pointers to shell edges before the extra nodes.                */
 
3549
  highorderindex = 6 + (useshelles * 3);
 
3550
  /* The number of bytes occupied by a triangle. */
 
3551
  trisize = ((order + 1) * (order + 2) / 2 + (highorderindex - 3)) *
 
3552
            sizeof(triangle);
 
3553
  /* The index within each triangle at which its attributes are found, */
 
3554
  /*   where the index is measured in REALs.                           */
 
3555
  elemattribindex = (trisize + sizeof(REAL) - 1) / sizeof(REAL);
 
3556
  /* The index within each triangle at which the maximum area constraint  */
 
3557
  /*   is found, where the index is measured in REALs.  Note that if the  */
 
3558
  /*   `regionattrib' flag is set, an additional attribute will be added. */
 
3559
  areaboundindex = elemattribindex + eextras + regionattrib;
 
3560
  /* If triangle attributes or an area bound are needed, increase the number */
 
3561
  /*   of bytes occupied by a triangle.                                      */
 
3562
  if (vararea) {
 
3563
    trisize = (areaboundindex + 1) * sizeof(REAL);
 
3564
  } else if (eextras + regionattrib > 0) {
 
3565
    trisize = areaboundindex * sizeof(REAL);
 
3566
  }
 
3567
  /* If a Voronoi diagram or triangle neighbor graph is requested, make    */
 
3568
  /*   sure there's room to store an integer index in each triangle.  This */
 
3569
  /*   integer index can occupy the same space as the shell edges or       */
 
3570
  /*   attributes or area constraint or extra nodes.                       */
 
3571
  if ((voronoi || neighbors) &&
 
3572
      (trisize < 6 * sizeof(triangle) + sizeof(int))) {
 
3573
    trisize = 6 * sizeof(triangle) + sizeof(int);
 
3574
  }
 
3575
  /* Having determined the memory size of a triangle, initialize the pool. */
 
3576
  poolinit(&triangles, trisize, TRIPERBLOCK, POINTER, 4);
 
3577
 
 
3578
  if (useshelles) {
 
3579
    /* Initialize the pool of shell edges. */
 
3580
    poolinit(&shelles, 6 * sizeof(triangle) + sizeof(int), SHELLEPERBLOCK,
 
3581
             POINTER, 4);
 
3582
 
 
3583
    /* Initialize the "outer space" triangle and omnipresent shell edge. */
 
3584
    dummyinit(triangles.itemwords, shelles.itemwords);
 
3585
  } else {
 
3586
    /* Initialize the "outer space" triangle. */
 
3587
    dummyinit(triangles.itemwords, 0);
 
3588
  }
 
3589
}
 
3590
 
 
3591
/*****************************************************************************/
 
3592
/*                                                                           */
 
3593
/*  triangledealloc()   Deallocate space for a triangle, marking it dead.    */
 
3594
/*                                                                           */
 
3595
/*****************************************************************************/
 
3596
 
 
3597
void triangledealloc(triangle *dyingtriangle)
 
3598
{
 
3599
  /* Set triangle's vertices to NULL.  This makes it possible to        */
 
3600
  /*   detect dead triangles when traversing the list of all triangles. */
 
3601
  dyingtriangle[3] = (triangle) NULL;
 
3602
  dyingtriangle[4] = (triangle) NULL;
 
3603
  dyingtriangle[5] = (triangle) NULL;
 
3604
  pooldealloc(&triangles, (VOID *) dyingtriangle);
 
3605
}
 
3606
 
 
3607
/*****************************************************************************/
 
3608
/*                                                                           */
 
3609
/*  triangletraverse()   Traverse the triangles, skipping dead ones.         */
 
3610
/*                                                                           */
 
3611
/*****************************************************************************/
 
3612
 
 
3613
triangle *triangletraverse()
 
3614
{
 
3615
        triangle *newtriangle;
 
3616
        do {
 
3617
    newtriangle = (triangle *) traverse(&triangles);
 
3618
    if (newtriangle == (triangle *) NULL) {
 
3619
      return (triangle *) NULL;
 
3620
    }
 
3621
  } while (newtriangle[3] == (triangle) NULL);            /* Skip dead ones. */
 
3622
  return newtriangle;
 
3623
}
 
3624
 
 
3625
/*****************************************************************************/
 
3626
/*                                                                           */
 
3627
/*  shelledealloc()   Deallocate space for a shell edge, marking it dead.    */
 
3628
/*                                                                           */
 
3629
/*****************************************************************************/
 
3630
 
 
3631
void shelledealloc(shelle *dyingshelle)
 
3632
{
 
3633
  /* Set shell edge's vertices to NULL.  This makes it possible to */
 
3634
  /*   detect dead shells when traversing the list of all shells.  */
 
3635
  dyingshelle[2] = (shelle) NULL;
 
3636
  dyingshelle[3] = (shelle) NULL;
 
3637
  pooldealloc(&shelles, (VOID *) dyingshelle);
 
3638
}
 
3639
 
 
3640
/*****************************************************************************/
 
3641
/*                                                                           */
 
3642
/*  shelletraverse()   Traverse the shell edges, skipping dead ones.         */
 
3643
/*                                                                           */
 
3644
/*****************************************************************************/
 
3645
 
 
3646
shelle *shelletraverse()
 
3647
{
 
3648
  shelle *newshelle;
 
3649
 
 
3650
  do {
 
3651
    newshelle = (shelle *) traverse(&shelles);
 
3652
    if (newshelle == (shelle *) NULL) {
 
3653
      return (shelle *) NULL;
 
3654
    }
 
3655
  } while (newshelle[2] == (shelle) NULL);                /* Skip dead ones. */
 
3656
  return newshelle;
 
3657
}
 
3658
 
 
3659
/*****************************************************************************/
 
3660
/*                                                                           */
 
3661
/*  pointdealloc()   Deallocate space for a point, marking it dead.          */
 
3662
/*                                                                           */
 
3663
/*****************************************************************************/
 
3664
 
 
3665
void pointdealloc(point dyingpoint)
 
3666
{
 
3667
  /* Mark the point as dead.  This makes it possible to detect dead points */
 
3668
  /*   when traversing the list of all points.                             */
 
3669
  setpointmark(dyingpoint, DEADPOINT);
 
3670
  pooldealloc(&points, (VOID *) dyingpoint);
 
3671
}
 
3672
 
 
3673
/*****************************************************************************/
 
3674
/*                                                                           */
 
3675
/*  pointtraverse()   Traverse the points, skipping dead ones.               */
 
3676
/*                                                                           */
 
3677
/*****************************************************************************/
 
3678
 
 
3679
point pointtraverse()
 
3680
{
 
3681
  point newpoint;
 
3682
 
 
3683
  do {
 
3684
    newpoint = (point) traverse(&points);
 
3685
    if (newpoint == (point) NULL) {
 
3686
      return (point) NULL;
 
3687
    }
 
3688
  } while (pointmark(newpoint) == DEADPOINT);             /* Skip dead ones. */
 
3689
  return newpoint;
 
3690
}
 
3691
 
 
3692
/*****************************************************************************/
 
3693
/*                                                                           */
 
3694
/*  badsegmentdealloc()   Deallocate space for a bad segment, marking it     */
 
3695
/*                        dead.                                              */
 
3696
/*                                                                           */
 
3697
/*****************************************************************************/
 
3698
 
 
3699
#ifndef CDT_ONLY
 
3700
 
 
3701
void badsegmentdealloc(struct edge *dyingseg)
 
3702
{
 
3703
  /* Set segment's orientation to -1.  This makes it possible to      */
 
3704
  /*   detect dead segments when traversing the list of all segments. */
 
3705
  dyingseg->shorient = -1;
 
3706
  pooldealloc(&badsegments, (VOID *) dyingseg);
 
3707
}
 
3708
 
 
3709
#endif /* not CDT_ONLY */
 
3710
 
 
3711
/*****************************************************************************/
 
3712
/*                                                                           */
 
3713
/*  badsegmenttraverse()   Traverse the bad segments, skipping dead ones.    */
 
3714
/*                                                                           */
 
3715
/*****************************************************************************/
 
3716
 
 
3717
#ifndef CDT_ONLY
 
3718
 
 
3719
struct edge *badsegmenttraverse()
 
3720
{
 
3721
  struct edge *newseg;
 
3722
 
 
3723
  do {
 
3724
    newseg = (struct edge *) traverse(&badsegments);
 
3725
    if (newseg == (struct edge *) NULL) {
 
3726
      return (struct edge *) NULL;
 
3727
    }
 
3728
  } while (newseg->shorient == -1);                       /* Skip dead ones. */
 
3729
  return newseg;
 
3730
}
 
3731
 
 
3732
#endif /* not CDT_ONLY */
 
3733
 
 
3734
/*****************************************************************************/
 
3735
/*                                                                           */
 
3736
/*  getpoint()   Get a specific point, by number, from the list.             */
 
3737
/*                                                                           */
 
3738
/*  The first point is number 'firstnumber'.                                 */
 
3739
/*                                                                           */
 
3740
/*  Note that this takes O(n) time (with a small constant, if POINTPERBLOCK  */
 
3741
/*  is large).  I don't care to take the trouble to make it work in constant */
 
3742
/*  time.                                                                    */
 
3743
/*                                                                           */
 
3744
/*****************************************************************************/
 
3745
 
 
3746
point getpoint(int number)
 
3747
{
 
3748
  VOID **getblock;
 
3749
  point foundpoint;
 
3750
  unsigned long alignptr;
 
3751
  int current;
 
3752
 
 
3753
  getblock = points.firstblock;
 
3754
  current = firstnumber;
 
3755
  /* Find the right block. */
 
3756
  while (current + points.itemsperblock <= number) {
 
3757
    getblock = (VOID **) *getblock;
 
3758
    current += points.itemsperblock;
 
3759
  }
 
3760
  /* Now find the right point. */
 
3761
  alignptr = (unsigned long) (getblock + 1);
 
3762
  foundpoint = (point) (alignptr + (unsigned long) points.alignbytes
 
3763
                        - (alignptr % (unsigned long) points.alignbytes));
 
3764
  while (current < number) {
 
3765
    foundpoint += points.itemwords;
 
3766
    current++;
 
3767
  }
 
3768
  return foundpoint;
 
3769
}
 
3770
 
 
3771
/*****************************************************************************/
 
3772
/*                                                                           */
 
3773
/*  triangledeinit()   Free all remaining allocated memory.                  */
 
3774
/*                                                                           */
 
3775
/*****************************************************************************/
 
3776
 
 
3777
void triangledeinit()
 
3778
{
 
3779
  pooldeinit(&triangles);
 
3780
  free(dummytribase);
 
3781
  if (useshelles) {
 
3782
    pooldeinit(&shelles);
 
3783
    free(dummyshbase);
 
3784
  }
 
3785
  pooldeinit(&points);
 
3786
#ifndef CDT_ONLY
 
3787
  if (quality) {
 
3788
    pooldeinit(&badsegments);
 
3789
    if ((minangle > 0.0) || vararea || fixedarea) {
 
3790
      pooldeinit(&badtriangles);
 
3791
    }
 
3792
  }
 
3793
#endif /* not CDT_ONLY */
 
3794
}
 
3795
 
 
3796
/**                                                                         **/
 
3797
/**                                                                         **/
 
3798
/********* Memory management routines end here                       *********/
 
3799
 
 
3800
/********* Constructors begin here                                   *********/
 
3801
/**                                                                         **/
 
3802
/**                                                                         **/
 
3803
 
 
3804
/*****************************************************************************/
 
3805
/*                                                                           */
 
3806
/*  maketriangle()   Create a new triangle with orientation zero.            */
 
3807
/*                                                                           */
 
3808
/*****************************************************************************/
 
3809
 
 
3810
void maketriangle(struct triedge *newtriedge)
 
3811
{
 
3812
  int i;
 
3813
 
 
3814
  newtriedge->tri = (triangle *) poolalloc(&triangles);
 
3815
  /* Initialize the three adjoining triangles to be "outer space". */
 
3816
  newtriedge->tri[0] = (triangle) dummytri;
 
3817
  newtriedge->tri[1] = (triangle) dummytri;
 
3818
  newtriedge->tri[2] = (triangle) dummytri;
 
3819
  /* Three NULL vertex points. */
 
3820
  newtriedge->tri[3] = (triangle) NULL;
 
3821
  newtriedge->tri[4] = (triangle) NULL;
 
3822
  newtriedge->tri[5] = (triangle) NULL;
 
3823
  /* Initialize the three adjoining shell edges to be the omnipresent */
 
3824
  /*   shell edge.                                                    */
 
3825
  if (useshelles) {
 
3826
    newtriedge->tri[6] = (triangle) dummysh;
 
3827
    newtriedge->tri[7] = (triangle) dummysh;
 
3828
    newtriedge->tri[8] = (triangle) dummysh;
 
3829
  }
 
3830
  for (i = 0; i < eextras; i++) {
 
3831
    setelemattribute(*newtriedge, i, 0.0);
 
3832
  }
 
3833
  if (vararea) {
 
3834
    setareabound(*newtriedge, -1.0);
 
3835
  }
 
3836
 
 
3837
  newtriedge->orient = 0;
 
3838
}
 
3839
 
 
3840
/*****************************************************************************/
 
3841
/*                                                                           */
 
3842
/*  makeshelle()   Create a new shell edge with orientation zero.            */
 
3843
/*                                                                           */
 
3844
/*****************************************************************************/
 
3845
 
 
3846
void makeshelle(struct edge *newedge)
 
3847
{
 
3848
  newedge->sh = (shelle *) poolalloc(&shelles);
 
3849
  /* Initialize the two adjoining shell edges to be the omnipresent */
 
3850
  /*   shell edge.                                                  */
 
3851
  newedge->sh[0] = (shelle) dummysh;
 
3852
  newedge->sh[1] = (shelle) dummysh;
 
3853
  /* Two NULL vertex points. */
 
3854
  newedge->sh[2] = (shelle) NULL;
 
3855
  newedge->sh[3] = (shelle) NULL;
 
3856
  /* Initialize the two adjoining triangles to be "outer space". */
 
3857
  newedge->sh[4] = (shelle) dummytri;
 
3858
  newedge->sh[5] = (shelle) dummytri;
 
3859
  /* Set the boundary marker to zero. */
 
3860
  setmark(*newedge, 0);
 
3861
 
 
3862
  newedge->shorient = 0;
 
3863
}
 
3864
 
 
3865
/**                                                                         **/
 
3866
/**                                                                         **/
 
3867
/********* Constructors end here                                     *********/
 
3868
 
 
3869
/********* Determinant evaluation routines begin here                *********/
 
3870
/**                                                                         **/
 
3871
/**                                                                         **/
 
3872
 
 
3873
/* The adaptive exact arithmetic geometric predicates implemented herein are */
 
3874
/*   described in detail in my Technical Report CMU-CS-96-140.  The complete */
 
3875
/*   reference is given in the header.                                       */
 
3876
 
 
3877
/* Which of the following two methods of finding the absolute values is      */
 
3878
/*   fastest is compiler-dependent.  A few compilers can inline and optimize */
 
3879
/*   the fabs() call; but most will incur the overhead of a function call,   */
 
3880
/*   which is disastrously slow.  A faster way on IEEE machines might be to  */
 
3881
/*   mask the appropriate bit, but that's difficult to do in C.              */
 
3882
 
 
3883
#define Absolute(a)  ((a) >= 0.0 ? (a) : -(a))
 
3884
/* #define Absolute(a)  fabs(a) */
 
3885
 
 
3886
/* Many of the operations are broken up into two pieces, a main part that    */
 
3887
/*   performs an approximate operation, and a "tail" that computes the       */
 
3888
/*   roundoff error of that operation.                                       */
 
3889
/*                                                                           */
 
3890
/* The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(),    */
 
3891
/*   Split(), and Two_Product() are all implemented as described in the      */
 
3892
/*   reference.  Each of these macros requires certain variables to be       */
 
3893
/*   defined in the calling routine.  The variables `bvirt', `c', `abig',    */
 
3894
/*   `_i', `_j', `_k', `_l', `_m', and `_n' are declared `INEXACT' because   */
 
3895
/*   they store the result of an operation that may incur roundoff error.    */
 
3896
/*   The input parameter `x' (or the highest numbered `x_' parameter) must   */
 
3897
/*   also be declared `INEXACT'.                                             */
 
3898
 
 
3899
#define Fast_Two_Sum_Tail(a, b, x, y) \
 
3900
  bvirt = x - a; \
 
3901
  y = b - bvirt
 
3902
 
 
3903
#define Fast_Two_Sum(a, b, x, y) \
 
3904
  x = (REAL) (a + b); \
 
3905
  Fast_Two_Sum_Tail(a, b, x, y)
 
3906
 
 
3907
#define Two_Sum_Tail(a, b, x, y) \
 
3908
  bvirt = (REAL) (x - a); \
 
3909
  avirt = x - bvirt; \
 
3910
  bround = b - bvirt; \
 
3911
  around = a - avirt; \
 
3912
  y = around + bround
 
3913
 
 
3914
#define Two_Sum(a, b, x, y) \
 
3915
  x = (REAL) (a + b); \
 
3916
  Two_Sum_Tail(a, b, x, y)
 
3917
 
 
3918
#define Two_Diff_Tail(a, b, x, y) \
 
3919
  bvirt = (REAL) (a - x); \
 
3920
  avirt = x + bvirt; \
 
3921
  bround = bvirt - b; \
 
3922
  around = a - avirt; \
 
3923
  y = around + bround
 
3924
 
 
3925
#define Two_Diff(a, b, x, y) \
 
3926
  x = (REAL) (a - b); \
 
3927
  Two_Diff_Tail(a, b, x, y)
 
3928
 
 
3929
#define Split(a, ahi, alo) \
 
3930
  c = (REAL) (splitter * a); \
 
3931
  abig = (REAL) (c - a); \
 
3932
  ahi = c - abig; \
 
3933
  alo = a - ahi
 
3934
 
 
3935
#define Two_Product_Tail(a, b, x, y) \
 
3936
  Split(a, ahi, alo); \
 
3937
  Split(b, bhi, blo); \
 
3938
  err1 = x - (ahi * bhi); \
 
3939
  err2 = err1 - (alo * bhi); \
 
3940
  err3 = err2 - (ahi * blo); \
 
3941
  y = (alo * blo) - err3
 
3942
 
 
3943
#define Two_Product(a, b, x, y) \
 
3944
  x = (REAL) (a * b); \
 
3945
  Two_Product_Tail(a, b, x, y)
 
3946
 
 
3947
/* Two_Product_Presplit() is Two_Product() where one of the inputs has       */
 
3948
/*   already been split.  Avoids redundant splitting.                        */
 
3949
 
 
3950
#define Two_Product_Presplit(a, b, bhi, blo, x, y) \
 
3951
  x = (REAL) (a * b); \
 
3952
  Split(a, ahi, alo); \
 
3953
  err1 = x - (ahi * bhi); \
 
3954
  err2 = err1 - (alo * bhi); \
 
3955
  err3 = err2 - (ahi * blo); \
 
3956
  y = (alo * blo) - err3
 
3957
 
 
3958
/* Square() can be done more quickly than Two_Product().                     */
 
3959
 
 
3960
#define Square_Tail(a, x, y) \
 
3961
  Split(a, ahi, alo); \
 
3962
  err1 = x - (ahi * ahi); \
 
3963
  err3 = err1 - ((ahi + ahi) * alo); \
 
3964
  y = (alo * alo) - err3
 
3965
 
 
3966
#define Square(a, x, y) \
 
3967
  x = (REAL) (a * a); \
 
3968
  Square_Tail(a, x, y)
 
3969
 
 
3970
/* Macros for summing expansions of various fixed lengths.  These are all    */
 
3971
/*   unrolled versions of Expansion_Sum().                                   */
 
3972
 
 
3973
#define Two_One_Sum(a1, a0, b, x2, x1, x0) \
 
3974
  Two_Sum(a0, b , _i, x0); \
 
3975
  Two_Sum(a1, _i, x2, x1)
 
3976
 
 
3977
#define Two_One_Diff(a1, a0, b, x2, x1, x0) \
 
3978
  Two_Diff(a0, b , _i, x0); \
 
3979
  Two_Sum( a1, _i, x2, x1)
 
3980
 
 
3981
#define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0) \
 
3982
  Two_One_Sum(a1, a0, b0, _j, _0, x0); \
 
3983
  Two_One_Sum(_j, _0, b1, x3, x2, x1)
 
3984
 
 
3985
#define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0) \
 
3986
  Two_One_Diff(a1, a0, b0, _j, _0, x0); \
 
3987
  Two_One_Diff(_j, _0, b1, x3, x2, x1)
 
3988
 
 
3989
/*****************************************************************************/
 
3990
/*                                                                           */
 
3991
/*  exactinit()   Initialize the variables used for exact arithmetic.        */
 
3992
/*                                                                           */
 
3993
/*  `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in   */
 
3994
/*  floating-point arithmetic.  `epsilon' bounds the relative roundoff       */
 
3995
/*  error.  It is used for floating-point error analysis.                    */
 
3996
/*                                                                           */
 
3997
/*  `splitter' is used to split floating-point numbers into two half-        */
 
3998
/*  length significands for exact multiplication.                            */
 
3999
/*                                                                           */
 
4000
/*  I imagine that a highly optimizing compiler might be too smart for its   */
 
4001
/*  own good, and somehow cause this routine to fail, if it pretends that    */
 
4002
/*  floating-point arithmetic is too much like real arithmetic.              */
 
4003
/*                                                                           */
 
4004
/*  Don't change this routine unless you fully understand it.                */
 
4005
/*                                                                           */
 
4006
/*****************************************************************************/
 
4007
 
 
4008
void exactinit()
 
4009
{
 
4010
  REAL half;
 
4011
  REAL check, lastcheck;
 
4012
  int every_other;
 
4013
 
 
4014
  every_other = 1;
 
4015
  half = 0.5;
 
4016
  epsilon = 1.0;
 
4017
  splitter = 1.0;
 
4018
  check = 1.0;
 
4019
  /* Repeatedly divide `epsilon' by two until it is too small to add to      */
 
4020
  /*   one without causing roundoff.  (Also check if the sum is equal to     */
 
4021
  /*   the previous sum, for machines that round up instead of using exact   */
 
4022
  /*   rounding.  Not that these routines will work on such machines anyway. */
 
4023
  do {
 
4024
    lastcheck = check;
 
4025
    epsilon *= half;
 
4026
    if (every_other) {
 
4027
      splitter *= 2.0;
 
4028
    }
 
4029
    every_other = !every_other;
 
4030
    check = 1.0 + epsilon;
 
4031
  } while ((check != 1.0) && (check != lastcheck));
 
4032
  splitter += 1.0;
 
4033
  if (verbose > 1) {
 
4034
    printf("Floating point roundoff is of magnitude %.17g\n", epsilon);
 
4035
    printf("Floating point splitter is %.17g\n", splitter);
 
4036
  }
 
4037
  /* Error bounds for orientation and incircle tests. */
 
4038
  resulterrbound = (3.0 + 8.0 * epsilon) * epsilon;
 
4039
  ccwerrboundA = (3.0 + 16.0 * epsilon) * epsilon;
 
4040
  ccwerrboundB = (2.0 + 12.0 * epsilon) * epsilon;
 
4041
  ccwerrboundC = (9.0 + 64.0 * epsilon) * epsilon * epsilon;
 
4042
  iccerrboundA = (10.0 + 96.0 * epsilon) * epsilon;
 
4043
  iccerrboundB = (4.0 + 48.0 * epsilon) * epsilon;
 
4044
  iccerrboundC = (44.0 + 576.0 * epsilon) * epsilon * epsilon;
 
4045
}
 
4046
 
 
4047
/*****************************************************************************/
 
4048
/*                                                                           */
 
4049
/*  fast_expansion_sum_zeroelim()   Sum two expansions, eliminating zero     */
 
4050
/*                                  components from the output expansion.    */
 
4051
/*                                                                           */
 
4052
/*  Sets h = e + f.  See my Robust Predicates paper for details.             */
 
4053
/*                                                                           */
 
4054
/*  If round-to-even is used (as with IEEE 754), maintains the strongly      */
 
4055
/*  nonoverlapping property.  (That is, if e is strongly nonoverlapping, h   */
 
4056
/*  will be also.)  Does NOT maintain the nonoverlapping or nonadjacent      */
 
4057
/*  properties.                                                              */
 
4058
/*                                                                           */
 
4059
/*****************************************************************************/
 
4060
 
 
4061
int fast_expansion_sum_zeroelim( /* h cannot be e or f. */
 
4062
                                                                int elen,
 
4063
                                                                REAL *e,
 
4064
                                                                int flen,
 
4065
                                                                REAL *f,
 
4066
                                                                REAL *h
 
4067
                                                        )
 
4068
{
 
4069
  REAL Q;
 
4070
  INEXACT REAL Qnew;
 
4071
  INEXACT REAL hh;
 
4072
  INEXACT REAL bvirt;
 
4073
  REAL avirt, bround, around;
 
4074
  int eindex, findex, hindex;
 
4075
  REAL enow, fnow;
 
4076
 
 
4077
  enow = e[0];
 
4078
  fnow = f[0];
 
4079
  eindex = findex = 0;
 
4080
  if ((fnow > enow) == (fnow > -enow)) {
 
4081
    Q = enow;
 
4082
    enow = e[++eindex];
 
4083
  } else {
 
4084
    Q = fnow;
 
4085
    fnow = f[++findex];
 
4086
  }
 
4087
  hindex = 0;
 
4088
  if ((eindex < elen) && (findex < flen)) {
 
4089
    if ((fnow > enow) == (fnow > -enow)) {
 
4090
      Fast_Two_Sum(enow, Q, Qnew, hh);
 
4091
      enow = e[++eindex];
 
4092
    } else {
 
4093
      Fast_Two_Sum(fnow, Q, Qnew, hh);
 
4094
      fnow = f[++findex];
 
4095
    }
 
4096
    Q = Qnew;
 
4097
    if (hh != 0.0) {
 
4098
      h[hindex++] = hh;
 
4099
    }
 
4100
    while ((eindex < elen) && (findex < flen)) {
 
4101
      if ((fnow > enow) == (fnow > -enow)) {
 
4102
        Two_Sum(Q, enow, Qnew, hh);
 
4103
        enow = e[++eindex];
 
4104
      } else {
 
4105
        Two_Sum(Q, fnow, Qnew, hh);
 
4106
        fnow = f[++findex];
 
4107
      }
 
4108
      Q = Qnew;
 
4109
      if (hh != 0.0) {
 
4110
        h[hindex++] = hh;
 
4111
      }
 
4112
    }
 
4113
  }
 
4114
  while (eindex < elen) {
 
4115
    Two_Sum(Q, enow, Qnew, hh);
 
4116
    enow = e[++eindex];
 
4117
    Q = Qnew;
 
4118
    if (hh != 0.0) {
 
4119
      h[hindex++] = hh;
 
4120
    }
 
4121
  }
 
4122
  while (findex < flen) {
 
4123
    Two_Sum(Q, fnow, Qnew, hh);
 
4124
    fnow = f[++findex];
 
4125
    Q = Qnew;
 
4126
    if (hh != 0.0) {
 
4127
      h[hindex++] = hh;
 
4128
    }
 
4129
  }
 
4130
  if ((Q != 0.0) || (hindex == 0)) {
 
4131
    h[hindex++] = Q;
 
4132
  }
 
4133
  return hindex;
 
4134
}
 
4135
 
 
4136
/*****************************************************************************/
 
4137
/*                                                                           */
 
4138
/*  scale_expansion_zeroelim()   Multiply an expansion by a scalar,          */
 
4139
/*                               eliminating zero components from the        */
 
4140
/*                               output expansion.                           */
 
4141
/*                                                                           */
 
4142
/*  Sets h = be.  See my Robust Predicates paper for details.                */
 
4143
/*                                                                           */
 
4144
/*  Maintains the nonoverlapping property.  If round-to-even is used (as     */
 
4145
/*  with IEEE 754), maintains the strongly nonoverlapping and nonadjacent    */
 
4146
/*  properties as well.  (That is, if e has one of these properties, so      */
 
4147
/*  will h.)                                                                 */
 
4148
/*                                                                           */
 
4149
/*****************************************************************************/
 
4150
 
 
4151
int scale_expansion_zeroelim(  /* e and h cannot be the same. */
 
4152
                                                        int elen,
 
4153
                                                        REAL *e,
 
4154
                                                        REAL b,
 
4155
                                                        REAL *h
 
4156
                                                        )
 
4157
{
 
4158
  INEXACT REAL Q, sum;
 
4159
  REAL hh;
 
4160
  INEXACT REAL product1;
 
4161
  REAL product0;
 
4162
  int eindex, hindex;
 
4163
  REAL enow;
 
4164
  INEXACT REAL bvirt;
 
4165
  REAL avirt, bround, around;
 
4166
  INEXACT REAL c;
 
4167
  INEXACT REAL abig;
 
4168
  REAL ahi, alo, bhi, blo;
 
4169
  REAL err1, err2, err3;
 
4170
 
 
4171
  Split(b, bhi, blo);
 
4172
  Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
 
4173
  hindex = 0;
 
4174
  if (hh != 0) {
 
4175
    h[hindex++] = hh;
 
4176
  }
 
4177
  for (eindex = 1; eindex < elen; eindex++) {
 
4178
    enow = e[eindex];
 
4179
    Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
 
4180
    Two_Sum(Q, product0, sum, hh);
 
4181
    if (hh != 0) {
 
4182
      h[hindex++] = hh;
 
4183
    }
 
4184
    Fast_Two_Sum(product1, sum, Q, hh);
 
4185
    if (hh != 0) {
 
4186
      h[hindex++] = hh;
 
4187
    }
 
4188
  }
 
4189
  if ((Q != 0.0) || (hindex == 0)) {
 
4190
    h[hindex++] = Q;
 
4191
  }
 
4192
  return hindex;
 
4193
}
 
4194
 
 
4195
/*****************************************************************************/
 
4196
/*                                                                           */
 
4197
/*  estimate()   Produce a one-word estimate of an expansion's value.        */
 
4198
/*                                                                           */
 
4199
/*  See my Robust Predicates paper for details.                              */
 
4200
/*                                                                           */
 
4201
/*****************************************************************************/
 
4202
 
 
4203
REAL estimate(int elen,REAL *e)
 
4204
 
 
4205
{
 
4206
  REAL Q;
 
4207
  int eindex;
 
4208
 
 
4209
  Q = e[0];
 
4210
  for (eindex = 1; eindex < elen; eindex++) {
 
4211
    Q += e[eindex];
 
4212
  }
 
4213
  return Q;
 
4214
}
 
4215
 
 
4216
/*****************************************************************************/
 
4217
/*                                                                           */
 
4218
/*  counterclockwise()   Return a positive value if the points pa, pb, and   */
 
4219
/*                       pc occur in counterclockwise order; a negative      */
 
4220
/*                       value if they occur in clockwise order; and zero    */
 
4221
/*                       if they are collinear.  The result is also a rough  */
 
4222
/*                       approximation of twice the signed area of the       */
 
4223
/*                       triangle defined by the three points.               */
 
4224
/*                                                                           */
 
4225
/*  Uses exact arithmetic if necessary to ensure a correct answer.  The      */
 
4226
/*  result returned is the determinant of a matrix.  This determinant is     */
 
4227
/*  computed adaptively, in the sense that exact arithmetic is used only to  */
 
4228
/*  the degree it is needed to ensure that the returned value has the        */
 
4229
/*  correct sign.  Hence, this function is usually quite fast, but will run  */
 
4230
/*  more slowly when the input points are collinear or nearly so.            */
 
4231
/*                                                                           */
 
4232
/*  See my Robust Predicates paper for details.                              */
 
4233
/*                                                                           */
 
4234
/*****************************************************************************/
 
4235
 
 
4236
REAL counterclockwiseadapt(
 
4237
point pa,
 
4238
point pb,
 
4239
point pc,
 
4240
REAL detsum
 
4241
)
 
4242
{
 
4243
  INEXACT REAL acx, acy, bcx, bcy;
 
4244
  REAL acxtail, acytail, bcxtail, bcytail;
 
4245
  INEXACT REAL detleft, detright;
 
4246
  REAL detlefttail, detrighttail;
 
4247
  REAL det, errbound;
 
4248
  REAL B[4], C1[8], C2[12], D[16];
 
4249
  INEXACT REAL B3;
 
4250
  int C1length, C2length, Dlength;
 
4251
  REAL u[4];
 
4252
  INEXACT REAL u3;
 
4253
  INEXACT REAL s1, t1;
 
4254
  REAL s0, t0;
 
4255
 
 
4256
  INEXACT REAL bvirt;
 
4257
  REAL avirt, bround, around;
 
4258
  INEXACT REAL c;
 
4259
  INEXACT REAL abig;
 
4260
  REAL ahi, alo, bhi, blo;
 
4261
  REAL err1, err2, err3;
 
4262
  INEXACT REAL _i, _j;
 
4263
  REAL _0;
 
4264
 
 
4265
  acx = (REAL) (pa[0] - pc[0]);
 
4266
  bcx = (REAL) (pb[0] - pc[0]);
 
4267
  acy = (REAL) (pa[1] - pc[1]);
 
4268
  bcy = (REAL) (pb[1] - pc[1]);
 
4269
 
 
4270
  Two_Product(acx, bcy, detleft, detlefttail);
 
4271
  Two_Product(acy, bcx, detright, detrighttail);
 
4272
 
 
4273
  Two_Two_Diff(detleft, detlefttail, detright, detrighttail,
 
4274
               B3, B[2], B[1], B[0]);
 
4275
  B[3] = B3;
 
4276
 
 
4277
  det = estimate(4, B);
 
4278
  errbound = ccwerrboundB * detsum;
 
4279
  if ((det >= errbound) || (-det >= errbound)) {
 
4280
    return det;
 
4281
  }
 
4282
 
 
4283
  Two_Diff_Tail(pa[0], pc[0], acx, acxtail);
 
4284
  Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);
 
4285
  Two_Diff_Tail(pa[1], pc[1], acy, acytail);
 
4286
  Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);
 
4287
 
 
4288
  if ((acxtail == 0.0) && (acytail == 0.0)
 
4289
      && (bcxtail == 0.0) && (bcytail == 0.0)) {
 
4290
    return det;
 
4291
  }
 
4292
 
 
4293
  errbound = ccwerrboundC * detsum + resulterrbound * Absolute(det);
 
4294
  det += (acx * bcytail + bcy * acxtail)
 
4295
       - (acy * bcxtail + bcx * acytail);
 
4296
  if ((det >= errbound) || (-det >= errbound)) {
 
4297
    return det;
 
4298
  }
 
4299
 
 
4300
  Two_Product(acxtail, bcy, s1, s0);
 
4301
  Two_Product(acytail, bcx, t1, t0);
 
4302
  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
 
4303
  u[3] = u3;
 
4304
  C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);
 
4305
 
 
4306
  Two_Product(acx, bcytail, s1, s0);
 
4307
  Two_Product(acy, bcxtail, t1, t0);
 
4308
  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
 
4309
  u[3] = u3;
 
4310
  C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);
 
4311
 
 
4312
  Two_Product(acxtail, bcytail, s1, s0);
 
4313
  Two_Product(acytail, bcxtail, t1, t0);
 
4314
  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
 
4315
  u[3] = u3;
 
4316
  Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);
 
4317
 
 
4318
  return(D[Dlength - 1]);
 
4319
}
 
4320
 
 
4321
REAL counterclockwise(
 
4322
point pa,
 
4323
point pb,
 
4324
point pc
 
4325
)
 
4326
{
 
4327
  REAL detleft, detright, det;
 
4328
  REAL detsum, errbound;
 
4329
 
 
4330
  counterclockcount++;
 
4331
 
 
4332
  detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);
 
4333
  detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
 
4334
  det = detleft - detright;
 
4335
 
 
4336
  if (noexact) {
 
4337
    return det;
 
4338
  }
 
4339
 
 
4340
  if (detleft > 0.0) {
 
4341
    if (detright <= 0.0) {
 
4342
      return det;
 
4343
    } else {
 
4344
      detsum = detleft + detright;
 
4345
    }
 
4346
  } else if (detleft < 0.0) {
 
4347
    if (detright >= 0.0) {
 
4348
      return det;
 
4349
    } else {
 
4350
      detsum = -detleft - detright;
 
4351
    }
 
4352
  } else {
 
4353
    return det;
 
4354
  }
 
4355
 
 
4356
  errbound = ccwerrboundA * detsum;
 
4357
  if ((det >= errbound) || (-det >= errbound)) {
 
4358
    return det;
 
4359
  }
 
4360
 
 
4361
  return counterclockwiseadapt(pa, pb, pc, detsum);
 
4362
}
 
4363
 
 
4364
/*****************************************************************************/
 
4365
/*                                                                           */
 
4366
/*  incircle()   Return a positive value if the point pd lies inside the     */
 
4367
/*               circle passing through pa, pb, and pc; a negative value if  */
 
4368
/*               it lies outside; and zero if the four points are cocircular.*/
 
4369
/*               The points pa, pb, and pc must be in counterclockwise       */
 
4370
/*               order, or the sign of the result will be reversed.          */
 
4371
/*                                                                           */
 
4372
/*  Uses exact arithmetic if necessary to ensure a correct answer.  The      */
 
4373
/*  result returned is the determinant of a matrix.  This determinant is     */
 
4374
/*  computed adaptively, in the sense that exact arithmetic is used only to  */
 
4375
/*  the degree it is needed to ensure that the returned value has the        */
 
4376
/*  correct sign.  Hence, this function is usually quite fast, but will run  */
 
4377
/*  more slowly when the input points are cocircular or nearly so.           */
 
4378
/*                                                                           */
 
4379
/*  See my Robust Predicates paper for details.                              */
 
4380
/*                                                                           */
 
4381
/*****************************************************************************/
 
4382
 
 
4383
REAL incircleadapt(
 
4384
point pa,
 
4385
point pb,
 
4386
point pc,
 
4387
point pd,
 
4388
REAL permanent)
 
4389
{
 
4390
  INEXACT REAL adx, bdx, cdx, ady, bdy, cdy;
 
4391
  REAL det, errbound;
 
4392
 
 
4393
  INEXACT REAL bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
 
4394
  REAL bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
 
4395
  REAL bc[4], ca[4], ab[4];
 
4396
  INEXACT REAL bc3, ca3, ab3;
 
4397
  REAL axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];
 
4398
  int axbclen, axxbclen, aybclen, ayybclen, alen;
 
4399
  REAL bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];
 
4400
  int bxcalen, bxxcalen, bycalen, byycalen, blen;
 
4401
  REAL cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];
 
4402
  int cxablen, cxxablen, cyablen, cyyablen, clen;
 
4403
  REAL abdet[64];
 
4404
  int ablen;
 
4405
  REAL fin1[1152], fin2[1152];
 
4406
  REAL *finnow, *finother, *finswap;
 
4407
  int finlength;
 
4408
 
 
4409
  REAL adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;
 
4410
  INEXACT REAL adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;
 
4411
  REAL adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;
 
4412
  REAL aa[4], bb[4], cc[4];
 
4413
  INEXACT REAL aa3, bb3, cc3;
 
4414
  INEXACT REAL ti1, tj1;
 
4415
  REAL ti0, tj0;
 
4416
  REAL u[4], v[4];
 
4417
  INEXACT REAL u3, v3;
 
4418
  REAL temp8[8], temp16a[16], temp16b[16], temp16c[16];
 
4419
  REAL temp32a[32], temp32b[32], temp48[48], temp64[64];
 
4420
  int temp8len, temp16alen, temp16blen, temp16clen;
 
4421
  int temp32alen, temp32blen, temp48len, temp64len;
 
4422
  REAL axtbb[8], axtcc[8], aytbb[8], aytcc[8];
 
4423
  int axtbblen, axtcclen, aytbblen, aytcclen;
 
4424
  REAL bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];
 
4425
  int bxtaalen, bxtcclen, bytaalen, bytcclen;
 
4426
  REAL cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];
 
4427
  int cxtaalen, cxtbblen, cytaalen, cytbblen;
 
4428
  REAL axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];
 
4429
  int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;
 
4430
  REAL axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16], cytabt[16];
 
4431
  int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;
 
4432
  REAL axtbctt[8], aytbctt[8], bxtcatt[8];
 
4433
  REAL bytcatt[8], cxtabtt[8], cytabtt[8];
 
4434
  int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;
 
4435
  REAL abt[8], bct[8], cat[8];
 
4436
  int abtlen, bctlen, catlen;
 
4437
  REAL abtt[4], bctt[4], catt[4];
 
4438
  int abttlen, bcttlen, cattlen;
 
4439
  INEXACT REAL abtt3, bctt3, catt3;
 
4440
  REAL negate;
 
4441
 
 
4442
  INEXACT REAL bvirt;
 
4443
  REAL avirt, bround, around;
 
4444
  INEXACT REAL c;
 
4445
  INEXACT REAL abig;
 
4446
  REAL ahi, alo, bhi, blo;
 
4447
  REAL err1, err2, err3;
 
4448
  INEXACT REAL _i, _j;
 
4449
  REAL _0;
 
4450
 
 
4451
  adx = (REAL) (pa[0] - pd[0]);
 
4452
  bdx = (REAL) (pb[0] - pd[0]);
 
4453
  cdx = (REAL) (pc[0] - pd[0]);
 
4454
  ady = (REAL) (pa[1] - pd[1]);
 
4455
  bdy = (REAL) (pb[1] - pd[1]);
 
4456
  cdy = (REAL) (pc[1] - pd[1]);
 
4457
 
 
4458
  Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
 
4459
  Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
 
4460
  Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
 
4461
  bc[3] = bc3;
 
4462
  axbclen = scale_expansion_zeroelim(4, bc, adx, axbc);
 
4463
  axxbclen = scale_expansion_zeroelim(axbclen, axbc, adx, axxbc);
 
4464
  aybclen = scale_expansion_zeroelim(4, bc, ady, aybc);
 
4465
  ayybclen = scale_expansion_zeroelim(aybclen, aybc, ady, ayybc);
 
4466
  alen = fast_expansion_sum_zeroelim(axxbclen, axxbc, ayybclen, ayybc, adet);
 
4467
 
 
4468
  Two_Product(cdx, ady, cdxady1, cdxady0);
 
4469
  Two_Product(adx, cdy, adxcdy1, adxcdy0);
 
4470
  Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
 
4471
  ca[3] = ca3;
 
4472
  bxcalen = scale_expansion_zeroelim(4, ca, bdx, bxca);
 
4473
  bxxcalen = scale_expansion_zeroelim(bxcalen, bxca, bdx, bxxca);
 
4474
  bycalen = scale_expansion_zeroelim(4, ca, bdy, byca);
 
4475
  byycalen = scale_expansion_zeroelim(bycalen, byca, bdy, byyca);
 
4476
  blen = fast_expansion_sum_zeroelim(bxxcalen, bxxca, byycalen, byyca, bdet);
 
4477
 
 
4478
  Two_Product(adx, bdy, adxbdy1, adxbdy0);
 
4479
  Two_Product(bdx, ady, bdxady1, bdxady0);
 
4480
  Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
 
4481
  ab[3] = ab3;
 
4482
  cxablen = scale_expansion_zeroelim(4, ab, cdx, cxab);
 
4483
  cxxablen = scale_expansion_zeroelim(cxablen, cxab, cdx, cxxab);
 
4484
  cyablen = scale_expansion_zeroelim(4, ab, cdy, cyab);
 
4485
  cyyablen = scale_expansion_zeroelim(cyablen, cyab, cdy, cyyab);
 
4486
  clen = fast_expansion_sum_zeroelim(cxxablen, cxxab, cyyablen, cyyab, cdet);
 
4487
 
 
4488
  ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);
 
4489
  finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);
 
4490
 
 
4491
  det = estimate(finlength, fin1);
 
4492
  errbound = iccerrboundB * permanent;
 
4493
  if ((det >= errbound) || (-det >= errbound)) {
 
4494
    return det;
 
4495
  }
 
4496
 
 
4497
  Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
 
4498
  Two_Diff_Tail(pa[1], pd[1], ady, adytail);
 
4499
  Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
 
4500
  Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
 
4501
  Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
 
4502
  Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
 
4503
  if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0)
 
4504
      && (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0)) {
 
4505
    return det;
 
4506
  }
 
4507
 
 
4508
  errbound = iccerrboundC * permanent + resulterrbound * Absolute(det);
 
4509
  det += ((adx * adx + ady * ady) * ((bdx * cdytail + cdy * bdxtail)
 
4510
                                     - (bdy * cdxtail + cdx * bdytail))
 
4511
          + 2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx))
 
4512
       + ((bdx * bdx + bdy * bdy) * ((cdx * adytail + ady * cdxtail)
 
4513
                                     - (cdy * adxtail + adx * cdytail))
 
4514
          + 2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx))
 
4515
       + ((cdx * cdx + cdy * cdy) * ((adx * bdytail + bdy * adxtail)
 
4516
                                     - (ady * bdxtail + bdx * adytail))
 
4517
          + 2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx));
 
4518
  if ((det >= errbound) || (-det >= errbound)) {
 
4519
    return det;
 
4520
  }
 
4521
 
 
4522
  finnow = fin1;
 
4523
  finother = fin2;
 
4524
 
 
4525
  if ((bdxtail != 0.0) || (bdytail != 0.0)
 
4526
      || (cdxtail != 0.0) || (cdytail != 0.0)) {
 
4527
    Square(adx, adxadx1, adxadx0);
 
4528
    Square(ady, adyady1, adyady0);
 
4529
    Two_Two_Sum(adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);
 
4530
    aa[3] = aa3;
 
4531
  }
 
4532
  if ((cdxtail != 0.0) || (cdytail != 0.0)
 
4533
      || (adxtail != 0.0) || (adytail != 0.0)) {
 
4534
    Square(bdx, bdxbdx1, bdxbdx0);
 
4535
    Square(bdy, bdybdy1, bdybdy0);
 
4536
    Two_Two_Sum(bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);
 
4537
    bb[3] = bb3;
 
4538
  }
 
4539
  if ((adxtail != 0.0) || (adytail != 0.0)
 
4540
      || (bdxtail != 0.0) || (bdytail != 0.0)) {
 
4541
    Square(cdx, cdxcdx1, cdxcdx0);
 
4542
    Square(cdy, cdycdy1, cdycdy0);
 
4543
    Two_Two_Sum(cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);
 
4544
    cc[3] = cc3;
 
4545
  }
 
4546
 
 
4547
  if (adxtail != 0.0) {
 
4548
    axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);
 
4549
    temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx,
 
4550
                                          temp16a);
 
4551
 
 
4552
    axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);
 
4553
    temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);
 
4554
 
 
4555
    axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);
 
4556
    temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);
 
4557
 
 
4558
    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4559
                                            temp16blen, temp16b, temp32a);
 
4560
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
 
4561
                                            temp32alen, temp32a, temp48);
 
4562
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4563
                                            temp48, finother);
 
4564
    finswap = finnow; finnow = finother; finother = finswap;
 
4565
  }
 
4566
  if (adytail != 0.0) {
 
4567
    aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);
 
4568
    temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady,
 
4569
                                          temp16a);
 
4570
 
 
4571
    aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);
 
4572
    temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);
 
4573
 
 
4574
    aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);
 
4575
    temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);
 
4576
 
 
4577
    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4578
                                            temp16blen, temp16b, temp32a);
 
4579
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
 
4580
                                            temp32alen, temp32a, temp48);
 
4581
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4582
                                            temp48, finother);
 
4583
    finswap = finnow; finnow = finother; finother = finswap;
 
4584
  }
 
4585
  if (bdxtail != 0.0) {
 
4586
    bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);
 
4587
    temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx,
 
4588
                                          temp16a);
 
4589
 
 
4590
    bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);
 
4591
    temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);
 
4592
 
 
4593
    bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);
 
4594
    temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);
 
4595
 
 
4596
    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4597
                                            temp16blen, temp16b, temp32a);
 
4598
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
 
4599
                                            temp32alen, temp32a, temp48);
 
4600
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4601
                                            temp48, finother);
 
4602
    finswap = finnow; finnow = finother; finother = finswap;
 
4603
  }
 
4604
  if (bdytail != 0.0) {
 
4605
    bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);
 
4606
    temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy,
 
4607
                                          temp16a);
 
4608
 
 
4609
    bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);
 
4610
    temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);
 
4611
 
 
4612
    bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);
 
4613
    temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);
 
4614
 
 
4615
    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4616
                                            temp16blen, temp16b, temp32a);
 
4617
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
 
4618
                                            temp32alen, temp32a, temp48);
 
4619
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4620
                                            temp48, finother);
 
4621
    finswap = finnow; finnow = finother; finother = finswap;
 
4622
  }
 
4623
  if (cdxtail != 0.0) {
 
4624
    cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);
 
4625
    temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx,
 
4626
                                          temp16a);
 
4627
 
 
4628
    cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);
 
4629
    temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);
 
4630
 
 
4631
    cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);
 
4632
    temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);
 
4633
 
 
4634
    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4635
                                            temp16blen, temp16b, temp32a);
 
4636
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
 
4637
                                            temp32alen, temp32a, temp48);
 
4638
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4639
                                            temp48, finother);
 
4640
    finswap = finnow; finnow = finother; finother = finswap;
 
4641
  }
 
4642
  if (cdytail != 0.0) {
 
4643
    cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);
 
4644
    temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy,
 
4645
                                          temp16a);
 
4646
 
 
4647
    cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);
 
4648
    temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);
 
4649
 
 
4650
    cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);
 
4651
    temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);
 
4652
 
 
4653
    temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4654
                                            temp16blen, temp16b, temp32a);
 
4655
    temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
 
4656
                                            temp32alen, temp32a, temp48);
 
4657
    finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4658
                                            temp48, finother);
 
4659
    finswap = finnow; finnow = finother; finother = finswap;
 
4660
  }
 
4661
 
 
4662
  if ((adxtail != 0.0) || (adytail != 0.0)) {
 
4663
    if ((bdxtail != 0.0) || (bdytail != 0.0)
 
4664
        || (cdxtail != 0.0) || (cdytail != 0.0)) {
 
4665
      Two_Product(bdxtail, cdy, ti1, ti0);
 
4666
      Two_Product(bdx, cdytail, tj1, tj0);
 
4667
      Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
 
4668
      u[3] = u3;
 
4669
      negate = -bdy;
 
4670
      Two_Product(cdxtail, negate, ti1, ti0);
 
4671
      negate = -bdytail;
 
4672
      Two_Product(cdx, negate, tj1, tj0);
 
4673
      Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
 
4674
      v[3] = v3;
 
4675
      bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);
 
4676
 
 
4677
      Two_Product(bdxtail, cdytail, ti1, ti0);
 
4678
      Two_Product(cdxtail, bdytail, tj1, tj0);
 
4679
      Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);
 
4680
      bctt[3] = bctt3;
 
4681
      bcttlen = 4;
 
4682
    } else {
 
4683
      bct[0] = 0.0;
 
4684
      bctlen = 1;
 
4685
      bctt[0] = 0.0;
 
4686
      bcttlen = 1;
 
4687
    }
 
4688
 
 
4689
    if (adxtail != 0.0) {
 
4690
      temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, adxtail, temp16a);
 
4691
      axtbctlen = scale_expansion_zeroelim(bctlen, bct, adxtail, axtbct);
 
4692
      temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, 2.0 * adx,
 
4693
                                            temp32a);
 
4694
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4695
                                              temp32alen, temp32a, temp48);
 
4696
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4697
                                              temp48, finother);
 
4698
      finswap = finnow; finnow = finother; finother = finswap;
 
4699
      if (bdytail != 0.0) {
 
4700
        temp8len = scale_expansion_zeroelim(4, cc, adxtail, temp8);
 
4701
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
 
4702
                                              temp16a);
 
4703
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
 
4704
                                                temp16a, finother);
 
4705
        finswap = finnow; finnow = finother; finother = finswap;
 
4706
      }
 
4707
      if (cdytail != 0.0) {
 
4708
        temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);
 
4709
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
 
4710
                                              temp16a);
 
4711
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
 
4712
                                                temp16a, finother);
 
4713
        finswap = finnow; finnow = finother; finother = finswap;
 
4714
      }
 
4715
 
 
4716
      temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail,
 
4717
                                            temp32a);
 
4718
      axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);
 
4719
      temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx,
 
4720
                                            temp16a);
 
4721
      temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail,
 
4722
                                            temp16b);
 
4723
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4724
                                              temp16blen, temp16b, temp32b);
 
4725
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
 
4726
                                              temp32blen, temp32b, temp64);
 
4727
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
 
4728
                                              temp64, finother);
 
4729
      finswap = finnow; finnow = finother; finother = finswap;
 
4730
    }
 
4731
    if (adytail != 0.0) {
 
4732
      temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, adytail, temp16a);
 
4733
      aytbctlen = scale_expansion_zeroelim(bctlen, bct, adytail, aytbct);
 
4734
      temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, 2.0 * ady,
 
4735
                                            temp32a);
 
4736
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4737
                                              temp32alen, temp32a, temp48);
 
4738
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4739
                                              temp48, finother);
 
4740
      finswap = finnow; finnow = finother; finother = finswap;
 
4741
 
 
4742
 
 
4743
      temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail,
 
4744
                                            temp32a);
 
4745
      aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);
 
4746
      temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady,
 
4747
                                            temp16a);
 
4748
      temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail,
 
4749
                                            temp16b);
 
4750
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4751
                                              temp16blen, temp16b, temp32b);
 
4752
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
 
4753
                                              temp32blen, temp32b, temp64);
 
4754
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
 
4755
                                              temp64, finother);
 
4756
      finswap = finnow; finnow = finother; finother = finswap;
 
4757
    }
 
4758
  }
 
4759
  if ((bdxtail != 0.0) || (bdytail != 0.0)) {
 
4760
    if ((cdxtail != 0.0) || (cdytail != 0.0)
 
4761
        || (adxtail != 0.0) || (adytail != 0.0)) {
 
4762
      Two_Product(cdxtail, ady, ti1, ti0);
 
4763
      Two_Product(cdx, adytail, tj1, tj0);
 
4764
      Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
 
4765
      u[3] = u3;
 
4766
      negate = -cdy;
 
4767
      Two_Product(adxtail, negate, ti1, ti0);
 
4768
      negate = -cdytail;
 
4769
      Two_Product(adx, negate, tj1, tj0);
 
4770
      Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
 
4771
      v[3] = v3;
 
4772
      catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);
 
4773
 
 
4774
      Two_Product(cdxtail, adytail, ti1, ti0);
 
4775
      Two_Product(adxtail, cdytail, tj1, tj0);
 
4776
      Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);
 
4777
      catt[3] = catt3;
 
4778
      cattlen = 4;
 
4779
    } else {
 
4780
      cat[0] = 0.0;
 
4781
      catlen = 1;
 
4782
      catt[0] = 0.0;
 
4783
      cattlen = 1;
 
4784
    }
 
4785
 
 
4786
    if (bdxtail != 0.0) {
 
4787
      temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, bdxtail, temp16a);
 
4788
      bxtcatlen = scale_expansion_zeroelim(catlen, cat, bdxtail, bxtcat);
 
4789
      temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, 2.0 * bdx,
 
4790
                                            temp32a);
 
4791
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4792
                                              temp32alen, temp32a, temp48);
 
4793
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4794
                                              temp48, finother);
 
4795
      finswap = finnow; finnow = finother; finother = finswap;
 
4796
      if (cdytail != 0.0) {
 
4797
        temp8len = scale_expansion_zeroelim(4, aa, bdxtail, temp8);
 
4798
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
 
4799
                                              temp16a);
 
4800
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
 
4801
                                                temp16a, finother);
 
4802
        finswap = finnow; finnow = finother; finother = finswap;
 
4803
      }
 
4804
      if (adytail != 0.0) {
 
4805
        temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);
 
4806
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
 
4807
                                              temp16a);
 
4808
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
 
4809
                                                temp16a, finother);
 
4810
        finswap = finnow; finnow = finother; finother = finswap;
 
4811
      }
 
4812
 
 
4813
      temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail,
 
4814
                                            temp32a);
 
4815
      bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);
 
4816
      temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx,
 
4817
                                            temp16a);
 
4818
      temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail,
 
4819
                                            temp16b);
 
4820
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4821
                                              temp16blen, temp16b, temp32b);
 
4822
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
 
4823
                                              temp32blen, temp32b, temp64);
 
4824
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
 
4825
                                              temp64, finother);
 
4826
      finswap = finnow; finnow = finother; finother = finswap;
 
4827
    }
 
4828
    if (bdytail != 0.0) {
 
4829
      temp16alen = scale_expansion_zeroelim(bytcalen, bytca, bdytail, temp16a);
 
4830
      bytcatlen = scale_expansion_zeroelim(catlen, cat, bdytail, bytcat);
 
4831
      temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, 2.0 * bdy,
 
4832
                                            temp32a);
 
4833
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4834
                                              temp32alen, temp32a, temp48);
 
4835
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4836
                                              temp48, finother);
 
4837
      finswap = finnow; finnow = finother; finother = finswap;
 
4838
 
 
4839
 
 
4840
      temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail,
 
4841
                                            temp32a);
 
4842
      bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);
 
4843
      temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy,
 
4844
                                            temp16a);
 
4845
      temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail,
 
4846
                                            temp16b);
 
4847
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4848
                                              temp16blen, temp16b, temp32b);
 
4849
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
 
4850
                                              temp32blen, temp32b, temp64);
 
4851
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
 
4852
                                              temp64, finother);
 
4853
      finswap = finnow; finnow = finother; finother = finswap;
 
4854
    }
 
4855
  }
 
4856
  if ((cdxtail != 0.0) || (cdytail != 0.0)) {
 
4857
    if ((adxtail != 0.0) || (adytail != 0.0)
 
4858
        || (bdxtail != 0.0) || (bdytail != 0.0)) {
 
4859
      Two_Product(adxtail, bdy, ti1, ti0);
 
4860
      Two_Product(adx, bdytail, tj1, tj0);
 
4861
      Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
 
4862
      u[3] = u3;
 
4863
      negate = -ady;
 
4864
      Two_Product(bdxtail, negate, ti1, ti0);
 
4865
      negate = -adytail;
 
4866
      Two_Product(bdx, negate, tj1, tj0);
 
4867
      Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
 
4868
      v[3] = v3;
 
4869
      abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);
 
4870
 
 
4871
      Two_Product(adxtail, bdytail, ti1, ti0);
 
4872
      Two_Product(bdxtail, adytail, tj1, tj0);
 
4873
      Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);
 
4874
      abtt[3] = abtt3;
 
4875
      abttlen = 4;
 
4876
    } else {
 
4877
      abt[0] = 0.0;
 
4878
      abtlen = 1;
 
4879
      abtt[0] = 0.0;
 
4880
      abttlen = 1;
 
4881
    }
 
4882
 
 
4883
    if (cdxtail != 0.0) {
 
4884
      temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, cdxtail, temp16a);
 
4885
      cxtabtlen = scale_expansion_zeroelim(abtlen, abt, cdxtail, cxtabt);
 
4886
      temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, 2.0 * cdx,
 
4887
                                            temp32a);
 
4888
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4889
                                              temp32alen, temp32a, temp48);
 
4890
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4891
                                              temp48, finother);
 
4892
      finswap = finnow; finnow = finother; finother = finswap;
 
4893
      if (adytail != 0.0) {
 
4894
        temp8len = scale_expansion_zeroelim(4, bb, cdxtail, temp8);
 
4895
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
 
4896
                                              temp16a);
 
4897
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
 
4898
                                                temp16a, finother);
 
4899
        finswap = finnow; finnow = finother; finother = finswap;
 
4900
      }
 
4901
      if (bdytail != 0.0) {
 
4902
        temp8len = scale_expansion_zeroelim(4, aa, -cdxtail, temp8);
 
4903
        temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
 
4904
                                              temp16a);
 
4905
        finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
 
4906
                                                temp16a, finother);
 
4907
        finswap = finnow; finnow = finother; finother = finswap;
 
4908
      }
 
4909
 
 
4910
      temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, cdxtail,
 
4911
                                            temp32a);
 
4912
      cxtabttlen = scale_expansion_zeroelim(abttlen, abtt, cdxtail, cxtabtt);
 
4913
      temp16alen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, 2.0 * cdx,
 
4914
                                            temp16a);
 
4915
      temp16blen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, cdxtail,
 
4916
                                            temp16b);
 
4917
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4918
                                              temp16blen, temp16b, temp32b);
 
4919
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
 
4920
                                              temp32blen, temp32b, temp64);
 
4921
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
 
4922
                                              temp64, finother);
 
4923
      finswap = finnow; finnow = finother; finother = finswap;
 
4924
    }
 
4925
    if (cdytail != 0.0) {
 
4926
      temp16alen = scale_expansion_zeroelim(cytablen, cytab, cdytail, temp16a);
 
4927
      cytabtlen = scale_expansion_zeroelim(abtlen, abt, cdytail, cytabt);
 
4928
      temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, 2.0 * cdy,
 
4929
                                            temp32a);
 
4930
      temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4931
                                              temp32alen, temp32a, temp48);
 
4932
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
 
4933
                                              temp48, finother);
 
4934
      finswap = finnow; finnow = finother; finother = finswap;
 
4935
 
 
4936
 
 
4937
      temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, cdytail,
 
4938
                                            temp32a);
 
4939
      cytabttlen = scale_expansion_zeroelim(abttlen, abtt, cdytail, cytabtt);
 
4940
      temp16alen = scale_expansion_zeroelim(cytabttlen, cytabtt, 2.0 * cdy,
 
4941
                                            temp16a);
 
4942
      temp16blen = scale_expansion_zeroelim(cytabttlen, cytabtt, cdytail,
 
4943
                                            temp16b);
 
4944
      temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
 
4945
                                              temp16blen, temp16b, temp32b);
 
4946
      temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
 
4947
                                              temp32blen, temp32b, temp64);
 
4948
      finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
 
4949
                                              temp64, finother);
 
4950
      finswap = finnow; finnow = finother; finother = finswap;
 
4951
    }
 
4952
  }
 
4953
 
 
4954
  return finnow[finlength - 1];
 
4955
}
 
4956
 
 
4957
REAL incircle(
 
4958
point pa,
 
4959
point pb,
 
4960
point pc,
 
4961
point pd)
 
4962
{
 
4963
  REAL adx, bdx, cdx, ady, bdy, cdy;
 
4964
  REAL bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
 
4965
  REAL alift, blift, clift;
 
4966
  REAL det;
 
4967
  REAL permanent, errbound;
 
4968
 
 
4969
  incirclecount++;
 
4970
 
 
4971
  adx = pa[0] - pd[0];
 
4972
  bdx = pb[0] - pd[0];
 
4973
  cdx = pc[0] - pd[0];
 
4974
  ady = pa[1] - pd[1];
 
4975
  bdy = pb[1] - pd[1];
 
4976
  cdy = pc[1] - pd[1];
 
4977
 
 
4978
  bdxcdy = bdx * cdy;
 
4979
  cdxbdy = cdx * bdy;
 
4980
  alift = adx * adx + ady * ady;
 
4981
 
 
4982
  cdxady = cdx * ady;
 
4983
  adxcdy = adx * cdy;
 
4984
  blift = bdx * bdx + bdy * bdy;
 
4985
 
 
4986
  adxbdy = adx * bdy;
 
4987
  bdxady = bdx * ady;
 
4988
  clift = cdx * cdx + cdy * cdy;
 
4989
 
 
4990
  det = alift * (bdxcdy - cdxbdy)
 
4991
      + blift * (cdxady - adxcdy)
 
4992
      + clift * (adxbdy - bdxady);
 
4993
 
 
4994
  if (noexact) {
 
4995
    return det;
 
4996
  }
 
4997
 
 
4998
  permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * alift
 
4999
            + (Absolute(cdxady) + Absolute(adxcdy)) * blift
 
5000
            + (Absolute(adxbdy) + Absolute(bdxady)) * clift;
 
5001
  errbound = iccerrboundA * permanent;
 
5002
  if ((det > errbound) || (-det > errbound)) {
 
5003
    return det;
 
5004
  }
 
5005
 
 
5006
  return incircleadapt(pa, pb, pc, pd, permanent);
 
5007
}
 
5008
 
 
5009
/**                                                                         **/
 
5010
/**                                                                         **/
 
5011
/********* Determinant evaluation routines end here                  *********/
 
5012
 
 
5013
/*****************************************************************************/
 
5014
/*                                                                           */
 
5015
/*  triangleinit()   Initialize some variables.                              */
 
5016
/*                                                                           */
 
5017
/*****************************************************************************/
 
5018
 
 
5019
void triangleinit()
 
5020
{
 
5021
  points.maxitems = triangles.maxitems = shelles.maxitems = viri.maxitems =
 
5022
    badsegments.maxitems = badtriangles.maxitems = splaynodes.maxitems = 0l;
 
5023
  points.itembytes = triangles.itembytes = shelles.itembytes = viri.itembytes =
 
5024
    badsegments.itembytes = badtriangles.itembytes = splaynodes.itembytes = 0;
 
5025
  recenttri.tri = (triangle *) NULL;    /* No triangle has been visited yet. */
 
5026
  samples = 1;            /* Point location should take at least one sample. */
 
5027
  checksegments = 0;      /* There are no segments in the triangulation yet. */
 
5028
  incirclecount = counterclockcount = hyperbolacount = 0;
 
5029
  circumcentercount = circletopcount = 0;
 
5030
  randomseed = 1;
 
5031
 
 
5032
  exactinit();                     /* Initialize exact arithmetic constants. */
 
5033
}
 
5034
 
 
5035
/*****************************************************************************/
 
5036
/*                                                                           */
 
5037
/*  randomnation()   Generate a random number between 0 and `choices' - 1.   */
 
5038
/*                                                                           */
 
5039
/*  This is a simple linear congruential random number generator.  Hence, it */
 
5040
/*  is a bad random number generator, but good enough for most randomized    */
 
5041
/*  geometric algorithms.                                                    */
 
5042
/*                                                                           */
 
5043
/*****************************************************************************/
 
5044
 
 
5045
unsigned long randomnation(
 
5046
unsigned int choices)
 
5047
{
 
5048
  randomseed = (randomseed * 1366l + 150889l) % 714025l;
 
5049
  return randomseed / (714025l / choices + 1);
 
5050
}
 
5051
 
 
5052
/********* Mesh quality testing routines begin here                  *********/
 
5053
/**                                                                         **/
 
5054
/**                                                                         **/
 
5055
 
 
5056
/*****************************************************************************/
 
5057
/*                                                                           */
 
5058
/*  checkmesh()   Test the mesh for topological consistency.                 */
 
5059
/*                                                                           */
 
5060
/*****************************************************************************/
 
5061
 
 
5062
#ifndef REDUCED
 
5063
 
 
5064
void checkmesh()
 
5065
{
 
5066
  struct triedge triangleloop;
 
5067
  struct triedge oppotri, oppooppotri;
 
5068
  point triorg, tridest, triapex;
 
5069
  point oppoorg, oppodest;
 
5070
  int horrors;
 
5071
  int saveexact;
 
5072
  triangle ptr;                         /* Temporary variable used by sym(). */
 
5073
 
 
5074
  /* Temporarily turn on exact arithmetic if it's off. */
 
5075
  saveexact = noexact;
 
5076
  noexact = 0;
 
5077
  if (!quiet) {
 
5078
    printf("  Checking consistency of mesh...\n");
 
5079
  }
 
5080
  horrors = 0;
 
5081
  /* Run through the list of triangles, checking each one. */
 
5082
  traversalinit(&triangles);
 
5083
  triangleloop.tri = triangletraverse();
 
5084
  while (triangleloop.tri != (triangle *) NULL) {
 
5085
    /* Check all three edges of the triangle. */
 
5086
    for (triangleloop.orient = 0; triangleloop.orient < 3;
 
5087
         triangleloop.orient++) {
 
5088
      org(triangleloop, triorg);
 
5089
      dest(triangleloop, tridest);
 
5090
      if (triangleloop.orient == 0) {       /* Only test for inversion once. */
 
5091
        /* Test if the triangle is flat or inverted. */
 
5092
        apex(triangleloop, triapex);
 
5093
        if (counterclockwise(triorg, tridest, triapex) <= 0.0) {
 
5094
          printf("  !! !! Inverted ");
 
5095
          printtriangle(&triangleloop);
 
5096
          horrors++;
 
5097
        }
 
5098
      }
 
5099
      /* Find the neighboring triangle on this edge. */
 
5100
      sym(triangleloop, oppotri);
 
5101
      if (oppotri.tri != dummytri) {
 
5102
        /* Check that the triangle's neighbor knows it's a neighbor. */
 
5103
        sym(oppotri, oppooppotri);
 
5104
        if ((triangleloop.tri != oppooppotri.tri)
 
5105
            || (triangleloop.orient != oppooppotri.orient)) {
 
5106
          printf("  !! !! Asymmetric triangle-triangle bond:\n");
 
5107
          if (triangleloop.tri == oppooppotri.tri) {
 
5108
            printf("   (Right triangle, wrong orientation)\n");
 
5109
          }
 
5110
          printf("    First ");
 
5111
          printtriangle(&triangleloop);
 
5112
          printf("    Second (nonreciprocating) ");
 
5113
          printtriangle(&oppotri);
 
5114
          horrors++;
 
5115
        }
 
5116
        /* Check that both triangles agree on the identities */
 
5117
        /*   of their shared vertices.                       */
 
5118
        org(oppotri, oppoorg);
 
5119
        dest(oppotri, oppodest);
 
5120
        if ((triorg != oppodest) || (tridest != oppoorg)) {
 
5121
          printf("  !! !! Mismatched edge coordinates between two triangles:\n"
 
5122
                 );
 
5123
          printf("    First mismatched ");
 
5124
          printtriangle(&triangleloop);
 
5125
          printf("    Second mismatched ");
 
5126
          printtriangle(&oppotri);
 
5127
          horrors++;
 
5128
        }
 
5129
      }
 
5130
    }
 
5131
    triangleloop.tri = triangletraverse();
 
5132
  }
 
5133
  if (horrors == 0) {
 
5134
    if (!quiet) {
 
5135
      printf("  In my studied opinion, the mesh appears to be consistent.\n");
 
5136
    }
 
5137
  } else if (horrors == 1) {
 
5138
    printf("  !! !! !! !! Precisely one festering wound discovered.\n");
 
5139
  } else {
 
5140
    printf("  !! !! !! !! %d abominations witnessed.\n", horrors);
 
5141
  }
 
5142
  /* Restore the status of exact arithmetic. */
 
5143
  noexact = saveexact;
 
5144
}
 
5145
 
 
5146
#endif /* not REDUCED */
 
5147
 
 
5148
/*****************************************************************************/
 
5149
/*                                                                           */
 
5150
/*  checkdelaunay()   Ensure that the mesh is (constrained) Delaunay.        */
 
5151
/*                                                                           */
 
5152
/*****************************************************************************/
 
5153
 
 
5154
#ifndef REDUCED
 
5155
 
 
5156
void checkdelaunay()
 
5157
{
 
5158
  struct triedge triangleloop;
 
5159
  struct triedge oppotri;
 
5160
  struct edge opposhelle;
 
5161
  point triorg, tridest, triapex;
 
5162
  point oppoapex;
 
5163
  int shouldbedelaunay;
 
5164
  int horrors;
 
5165
  int saveexact;
 
5166
  triangle ptr;                         /* Temporary variable used by sym(). */
 
5167
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
5168
 
 
5169
  /* Temporarily turn on exact arithmetic if it's off. */
 
5170
  saveexact = noexact;
 
5171
  noexact = 0;
 
5172
  if (!quiet) {
 
5173
    printf("  Checking Delaunay property of mesh...\n");
 
5174
  }
 
5175
  horrors = 0;
 
5176
  /* Run through the list of triangles, checking each one. */
 
5177
  traversalinit(&triangles);
 
5178
  triangleloop.tri = triangletraverse();
 
5179
  while (triangleloop.tri != (triangle *) NULL) {
 
5180
    /* Check all three edges of the triangle. */
 
5181
    for (triangleloop.orient = 0; triangleloop.orient < 3;
 
5182
         triangleloop.orient++) {
 
5183
      org(triangleloop, triorg);
 
5184
      dest(triangleloop, tridest);
 
5185
      apex(triangleloop, triapex);
 
5186
      sym(triangleloop, oppotri);
 
5187
      apex(oppotri, oppoapex);
 
5188
      /* Only test that the edge is locally Delaunay if there is an   */
 
5189
      /*   adjoining triangle whose pointer is larger (to ensure that */
 
5190
      /*   each pair isn't tested twice).                             */
 
5191
      shouldbedelaunay = (oppotri.tri != dummytri)
 
5192
            && (triapex != (point) NULL) && (oppoapex != (point) NULL)
 
5193
            && (triangleloop.tri < oppotri.tri);
 
5194
      if (checksegments && shouldbedelaunay) {
 
5195
        /* If a shell edge separates the triangles, then the edge is */
 
5196
        /*   constrained, so no local Delaunay test should be done.  */
 
5197
        tspivot(triangleloop, opposhelle);
 
5198
        if (opposhelle.sh != dummysh){
 
5199
          shouldbedelaunay = 0;
 
5200
        }
 
5201
      }
 
5202
      if (shouldbedelaunay) {
 
5203
        if (incircle(triorg, tridest, triapex, oppoapex) > 0.0) {
 
5204
          printf("  !! !! Non-Delaunay pair of triangles:\n");
 
5205
          printf("    First non-Delaunay ");
 
5206
          printtriangle(&triangleloop);
 
5207
          printf("    Second non-Delaunay ");
 
5208
          printtriangle(&oppotri);
 
5209
          horrors++;
 
5210
        }
 
5211
      }
 
5212
    }
 
5213
    triangleloop.tri = triangletraverse();
 
5214
  }
 
5215
  if (horrors == 0) {
 
5216
    if (!quiet) {
 
5217
      printf(
 
5218
  "  By virtue of my perceptive intelligence, I declare the mesh Delaunay.\n");
 
5219
    }
 
5220
  } else if (horrors == 1) {
 
5221
    printf(
 
5222
         "  !! !! !! !! Precisely one terrifying transgression identified.\n");
 
5223
  } else {
 
5224
    printf("  !! !! !! !! %d obscenities viewed with horror.\n", horrors);
 
5225
  }
 
5226
  /* Restore the status of exact arithmetic. */
 
5227
  noexact = saveexact;
 
5228
}
 
5229
 
 
5230
#endif /* not REDUCED */
 
5231
 
 
5232
/*****************************************************************************/
 
5233
/*                                                                           */
 
5234
/*  enqueuebadtri()   Add a bad triangle to the end of a queue.              */
 
5235
/*                                                                           */
 
5236
/*  The queue is actually a set of 64 queues.  I use multiple queues to give */
 
5237
/*  priority to smaller angles.  I originally implemented a heap, but the    */
 
5238
/*  queues are (to my surprise) much faster.                                 */
 
5239
/*                                                                           */
 
5240
/*****************************************************************************/
 
5241
 
 
5242
#ifndef CDT_ONLY
 
5243
 
 
5244
void enqueuebadtri(
 
5245
struct triedge *instri,
 
5246
REAL angle,
 
5247
point insapex,
 
5248
point insorg,
 
5249
point insdest)
 
5250
{
 
5251
  struct badface *newface;
 
5252
  int queuenumber;
 
5253
 
 
5254
  if (verbose > 2) {
 
5255
    printf("  Queueing bad triangle:\n");
 
5256
    printf("    (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", insorg[0],
 
5257
           insorg[1], insdest[0], insdest[1], insapex[0], insapex[1]);
 
5258
  }
 
5259
  /* Allocate space for the bad triangle. */
 
5260
  newface = (struct badface *) poolalloc(&badtriangles);
 
5261
  triedgecopy(*instri, newface->badfacetri);
 
5262
  newface->key = angle;
 
5263
  newface->faceapex = insapex;
 
5264
  newface->faceorg = insorg;
 
5265
  newface->facedest = insdest;
 
5266
  newface->nextface = (struct badface *) NULL;
 
5267
  /* Determine the appropriate queue to put the bad triangle into. */
 
5268
  if (angle > 0.6) {
 
5269
    queuenumber = (int) (160.0 * (angle - 0.6));
 
5270
    if (queuenumber > 63) {
 
5271
      queuenumber = 63;
 
5272
    }
 
5273
  } else {
 
5274
    /* It's not a bad angle; put the triangle in the lowest-priority queue. */
 
5275
    queuenumber = 0;
 
5276
  }
 
5277
  /* Add the triangle to the end of a queue. */
 
5278
  *queuetail[queuenumber] = newface;
 
5279
  /* Maintain a pointer to the NULL pointer at the end of the queue. */
 
5280
  queuetail[queuenumber] = &newface->nextface;
 
5281
}
 
5282
 
 
5283
#endif /* not CDT_ONLY */
 
5284
 
 
5285
/*****************************************************************************/
 
5286
/*                                                                           */
 
5287
/*  dequeuebadtri()   Remove a triangle from the front of the queue.         */
 
5288
/*                                                                           */
 
5289
/*****************************************************************************/
 
5290
 
 
5291
#ifndef CDT_ONLY
 
5292
 
 
5293
struct badface *dequeuebadtri()
 
5294
{
 
5295
  struct badface *result;
 
5296
  int queuenumber;
 
5297
 
 
5298
  /* Look for a nonempty queue. */
 
5299
  for (queuenumber = 63; queuenumber >= 0; queuenumber--) {
 
5300
    result = queuefront[queuenumber];
 
5301
    if (result != (struct badface *) NULL) {
 
5302
      /* Remove the triangle from the queue. */
 
5303
      queuefront[queuenumber] = result->nextface;
 
5304
      /* Maintain a pointer to the NULL pointer at the end of the queue. */
 
5305
      if (queuefront[queuenumber] == (struct badface *) NULL) {
 
5306
        queuetail[queuenumber] = &queuefront[queuenumber];
 
5307
      }
 
5308
      return result;
 
5309
    }
 
5310
  }
 
5311
  return (struct badface *) NULL;
 
5312
}
 
5313
 
 
5314
#endif /* not CDT_ONLY */
 
5315
 
 
5316
/*****************************************************************************/
 
5317
/*                                                                           */
 
5318
/*  checkedge4encroach()   Check a segment to see if it is encroached; add   */
 
5319
/*                         it to the list if it is.                          */
 
5320
/*                                                                           */
 
5321
/*  An encroached segment is an unflippable edge that has a point in its     */
 
5322
/*  diametral circle (that is, it faces an angle greater than 90 degrees).   */
 
5323
/*  This definition is due to Ruppert.                                       */
 
5324
/*                                                                           */
 
5325
/*  Returns a nonzero value if the edge is encroached.                       */
 
5326
/*                                                                           */
 
5327
/*****************************************************************************/
 
5328
 
 
5329
#ifndef CDT_ONLY
 
5330
 
 
5331
int checkedge4encroach(
 
5332
struct edge *testedge)
 
5333
{
 
5334
  struct triedge neighbortri;
 
5335
  struct edge testsym;
 
5336
  struct edge *badedge;
 
5337
  int addtolist;
 
5338
  int sides;
 
5339
  point eorg, edest, eapex;
 
5340
  triangle ptr;                     /* Temporary variable used by stpivot(). */
 
5341
 
 
5342
  addtolist = 0;
 
5343
  sides = 0;
 
5344
 
 
5345
  sorg(*testedge, eorg);
 
5346
  sdest(*testedge, edest);
 
5347
  /* Check one neighbor of the shell edge. */
 
5348
  stpivot(*testedge, neighbortri);
 
5349
  /* Does the neighbor exist, or is this a boundary edge? */
 
5350
  if (neighbortri.tri != dummytri) {
 
5351
    sides++;
 
5352
    /* Find a vertex opposite this edge. */
 
5353
    apex(neighbortri, eapex);
 
5354
    /* Check whether the vertex is inside the diametral circle of the  */
 
5355
    /*   shell edge.  Pythagoras' Theorem is used to check whether the */
 
5356
    /*   angle at the vertex is greater than 90 degrees.               */
 
5357
    if (eapex[0] * (eorg[0] + edest[0]) + eapex[1] * (eorg[1] + edest[1]) >
 
5358
        eapex[0] * eapex[0] + eorg[0] * edest[0] +
 
5359
        eapex[1] * eapex[1] + eorg[1] * edest[1]) {
 
5360
      addtolist = 1;
 
5361
    }
 
5362
  }
 
5363
  /* Check the other neighbor of the shell edge. */
 
5364
  ssym(*testedge, testsym);
 
5365
  stpivot(testsym, neighbortri);
 
5366
  /* Does the neighbor exist, or is this a boundary edge? */
 
5367
  if (neighbortri.tri != dummytri) {
 
5368
    sides++;
 
5369
    /* Find the other vertex opposite this edge. */
 
5370
    apex(neighbortri, eapex);
 
5371
    /* Check whether the vertex is inside the diametral circle of the  */
 
5372
    /*   shell edge.  Pythagoras' Theorem is used to check whether the */
 
5373
    /*   angle at the vertex is greater than 90 degrees.               */
 
5374
    if (eapex[0] * (eorg[0] + edest[0]) +
 
5375
        eapex[1] * (eorg[1] + edest[1]) >
 
5376
        eapex[0] * eapex[0] + eorg[0] * edest[0] +
 
5377
        eapex[1] * eapex[1] + eorg[1] * edest[1]) {
 
5378
      addtolist += 2;
 
5379
    }
 
5380
  }
 
5381
 
 
5382
  if (addtolist && (!nobisect || ((nobisect == 1) && (sides == 2)))) {
 
5383
    if (verbose > 2) {
 
5384
      printf("  Queueing encroached segment (%.12g, %.12g) (%.12g, %.12g).\n",
 
5385
             eorg[0], eorg[1], edest[0], edest[1]);
 
5386
    }
 
5387
    /* Add the shell edge to the list of encroached segments. */
 
5388
    /*   Be sure to get the orientation right.                */
 
5389
    badedge = (struct edge *) poolalloc(&badsegments);
 
5390
    if (addtolist == 1) {
 
5391
      shellecopy(*testedge, *badedge);
 
5392
    } else {
 
5393
      shellecopy(testsym, *badedge);
 
5394
    }
 
5395
  }
 
5396
  return addtolist;
 
5397
}
 
5398
 
 
5399
#endif /* not CDT_ONLY */
 
5400
 
 
5401
/*****************************************************************************/
 
5402
/*                                                                           */
 
5403
/*  testtriangle()   Test a face for quality measures.                       */
 
5404
/*                                                                           */
 
5405
/*  Tests a triangle to see if it satisfies the minimum angle condition and  */
 
5406
/*  the maximum area condition.  Triangles that aren't up to spec are added  */
 
5407
/*  to the bad triangle queue.                                               */
 
5408
/*                                                                           */
 
5409
/*****************************************************************************/
 
5410
 
 
5411
#ifndef CDT_ONLY
 
5412
 
 
5413
void testtriangle(
 
5414
struct triedge *testtri)
 
5415
{
 
5416
  struct triedge sametesttri;
 
5417
  struct edge edge1, edge2;
 
5418
  point torg, tdest, tapex;
 
5419
  point anglevertex;
 
5420
  REAL dxod, dyod, dxda, dyda, dxao, dyao;
 
5421
  REAL dxod2, dyod2, dxda2, dyda2, dxao2, dyao2;
 
5422
  REAL apexlen, orglen, destlen;
 
5423
  REAL angle;
 
5424
  REAL area;
 
5425
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
5426
 
 
5427
  org(*testtri, torg);
 
5428
  dest(*testtri, tdest);
 
5429
  apex(*testtri, tapex);
 
5430
  dxod = torg[0] - tdest[0];
 
5431
  dyod = torg[1] - tdest[1];
 
5432
  dxda = tdest[0] - tapex[0];
 
5433
  dyda = tdest[1] - tapex[1];
 
5434
  dxao = tapex[0] - torg[0];
 
5435
  dyao = tapex[1] - torg[1];
 
5436
  dxod2 = dxod * dxod;
 
5437
  dyod2 = dyod * dyod;
 
5438
  dxda2 = dxda * dxda;
 
5439
  dyda2 = dyda * dyda;
 
5440
  dxao2 = dxao * dxao;
 
5441
  dyao2 = dyao * dyao;
 
5442
  /* Find the lengths of the triangle's three edges. */
 
5443
  apexlen = dxod2 + dyod2;
 
5444
  orglen = dxda2 + dyda2;
 
5445
  destlen = dxao2 + dyao2;
 
5446
  if ((apexlen < orglen) && (apexlen < destlen)) {
 
5447
    /* The edge opposite the apex is shortest. */
 
5448
    /* Find the square of the cosine of the angle at the apex. */
 
5449
    angle = dxda * dxao + dyda * dyao;
 
5450
    angle = angle * angle / (orglen * destlen);
 
5451
    anglevertex = tapex;
 
5452
    lnext(*testtri, sametesttri);
 
5453
    tspivot(sametesttri, edge1);
 
5454
    lnextself(sametesttri);
 
5455
    tspivot(sametesttri, edge2);
 
5456
  } else if (orglen < destlen) {
 
5457
    /* The edge opposite the origin is shortest. */
 
5458
    /* Find the square of the cosine of the angle at the origin. */
 
5459
    angle = dxod * dxao + dyod * dyao;
 
5460
    angle = angle * angle / (apexlen * destlen);
 
5461
    anglevertex = torg;
 
5462
    tspivot(*testtri, edge1);
 
5463
    lprev(*testtri, sametesttri);
 
5464
    tspivot(sametesttri, edge2);
 
5465
  } else {
 
5466
    /* The edge opposite the destination is shortest. */
 
5467
    /* Find the square of the cosine of the angle at the destination. */
 
5468
    angle = dxod * dxda + dyod * dyda;
 
5469
    angle = angle * angle / (apexlen * orglen);
 
5470
    anglevertex = tdest;
 
5471
    tspivot(*testtri, edge1);
 
5472
    lnext(*testtri, sametesttri);
 
5473
    tspivot(sametesttri, edge2);
 
5474
  }
 
5475
  /* Check if both edges that form the angle are segments. */
 
5476
  if ((edge1.sh != dummysh) && (edge2.sh != dummysh)) {
 
5477
    /* The angle is a segment intersection. */
 
5478
    if ((angle > 0.9924) && !quiet) {                  /* Roughly 5 degrees. */
 
5479
      if (angle > 1.0) {
 
5480
        /* Beware of a floating exception in acos(). */
 
5481
        angle = 1.0;
 
5482
      }
 
5483
      /* Find the actual angle in degrees, for printing. */
 
5484
      angle = acos(sqrt(angle)) * (180.0 / PI);
 
5485
      printf(
 
5486
      "Warning:  Small angle (%.4g degrees) between segments at point\n",
 
5487
             angle);
 
5488
      printf("  (%.12g, %.12g)\n", anglevertex[0], anglevertex[1]);
 
5489
    }
 
5490
    /* Don't add this bad triangle to the list; there's nothing that */
 
5491
    /*   can be done about a small angle between two segments.       */
 
5492
    angle = 0.0;
 
5493
  }
 
5494
  /* Check whether the angle is smaller than permitted. */
 
5495
  if (angle > goodangle) {
 
5496
    /* Add this triangle to the list of bad triangles. */
 
5497
    enqueuebadtri(testtri, angle, tapex, torg, tdest);
 
5498
    return;
 
5499
  }
 
5500
  if (vararea || fixedarea) {
 
5501
    /* Check whether the area is larger than permitted. */
 
5502
    area = 0.5 * (dxod * dyda - dyod * dxda);
 
5503
    if (fixedarea && (area > maxarea)) {
 
5504
      /* Add this triangle to the list of bad triangles. */
 
5505
      enqueuebadtri(testtri, angle, tapex, torg, tdest);
 
5506
    } else if (vararea) {
 
5507
      /* Nonpositive area constraints are treated as unconstrained. */
 
5508
      if ((area > areabound(*testtri)) && (areabound(*testtri) > 0.0)) {
 
5509
        /* Add this triangle to the list of bad triangles. */
 
5510
        enqueuebadtri(testtri, angle, tapex, torg, tdest);
 
5511
      }
 
5512
    }
 
5513
  }
 
5514
}
 
5515
 
 
5516
#endif /* not CDT_ONLY */
 
5517
 
 
5518
/**                                                                         **/
 
5519
/**                                                                         **/
 
5520
/********* Mesh quality testing routines end here                    *********/
 
5521
 
 
5522
/********* Point location routines begin here                        *********/
 
5523
/**                                                                         **/
 
5524
/**                                                                         **/
 
5525
 
 
5526
/*****************************************************************************/
 
5527
/*                                                                           */
 
5528
/*  makepointmap()   Construct a mapping from points to triangles to improve  */
 
5529
/*                  the speed of point location for segment insertion.       */
 
5530
/*                                                                           */
 
5531
/*  Traverses all the triangles, and provides each corner of each triangle   */
 
5532
/*  with a pointer to that triangle.  Of course, pointers will be            */
 
5533
/*  overwritten by other pointers because (almost) each point is a corner    */
 
5534
/*  of several triangles, but in the end every point will point to some      */
 
5535
/*  triangle that contains it.                                               */
 
5536
/*                                                                           */
 
5537
/*****************************************************************************/
 
5538
 
 
5539
void makepointmap()
 
5540
{
 
5541
  struct triedge triangleloop;
 
5542
  point triorg;
 
5543
 
 
5544
  if (verbose) {
 
5545
    printf("    Constructing mapping from points to triangles.\n");
 
5546
  }
 
5547
  traversalinit(&triangles);
 
5548
  triangleloop.tri = triangletraverse();
 
5549
  while (triangleloop.tri != (triangle *) NULL) {
 
5550
    /* Check all three points of the triangle. */
 
5551
    for (triangleloop.orient = 0; triangleloop.orient < 3;
 
5552
         triangleloop.orient++) {
 
5553
      org(triangleloop, triorg);
 
5554
      setpoint2tri(triorg, encode(triangleloop));
 
5555
    }
 
5556
    triangleloop.tri = triangletraverse();
 
5557
  }
 
5558
}
 
5559
 
 
5560
/*****************************************************************************/
 
5561
/*                                                                           */
 
5562
/*  preciselocate()   Find a triangle or edge containing a given point.      */
 
5563
/*                                                                           */
 
5564
/*  Begins its search from `searchtri'.  It is important that `searchtri'    */
 
5565
/*  be a handle with the property that `searchpoint' is strictly to the left */
 
5566
/*  of the edge denoted by `searchtri', or is collinear with that edge and   */
 
5567
/*  does not intersect that edge.  (In particular, `searchpoint' should not  */
 
5568
/*  be the origin or destination of that edge.)                              */
 
5569
/*                                                                           */
 
5570
/*  These conditions are imposed because preciselocate() is normally used in */
 
5571
/*  one of two situations:                                                   */
 
5572
/*                                                                           */
 
5573
/*  (1)  To try to find the location to insert a new point.  Normally, we    */
 
5574
/*       know an edge that the point is strictly to the left of.  In the     */
 
5575
/*       incremental Delaunay algorithm, that edge is a bounding box edge.   */
 
5576
/*       In Ruppert's Delaunay refinement algorithm for quality meshing,     */
 
5577
/*       that edge is the shortest edge of the triangle whose circumcenter   */
 
5578
/*       is being inserted.                                                  */
 
5579
/*                                                                           */
 
5580
/*  (2)  To try to find an existing point.  In this case, any edge on the    */
 
5581
/*       convex hull is a good starting edge.  The possibility that the      */
 
5582
/*       vertex one seeks is an endpoint of the starting edge must be        */
 
5583
/*       screened out before preciselocate() is called.                      */
 
5584
/*                                                                           */
 
5585
/*  On completion, `searchtri' is a triangle that contains `searchpoint'.    */
 
5586
/*                                                                           */
 
5587
/*  This implementation differs from that given by Guibas and Stolfi.  It    */
 
5588
/*  walks from triangle to triangle, crossing an edge only if `searchpoint'  */
 
5589
/*  is on the other side of the line containing that edge.  After entering   */
 
5590
/*  a triangle, there are two edges by which one can leave that triangle.    */
 
5591
/*  If both edges are valid (`searchpoint' is on the other side of both      */
 
5592
/*  edges), one of the two is chosen by drawing a line perpendicular to      */
 
5593
/*  the entry edge (whose endpoints are `forg' and `fdest') passing through  */
 
5594
/*  `fapex'.  Depending on which side of this perpendicular `searchpoint'    */
 
5595
/*  falls on, an exit edge is chosen.                                        */
 
5596
/*                                                                           */
 
5597
/*  This implementation is empirically faster than the Guibas and Stolfi     */
 
5598
/*  point location routine (which I originally used), which tends to spiral  */
 
5599
/*  in toward its target.                                                    */
 
5600
/*                                                                           */
 
5601
/*  Returns ONVERTEX if the point lies on an existing vertex.  `searchtri'   */
 
5602
/*  is a handle whose origin is the existing vertex.                         */
 
5603
/*                                                                           */
 
5604
/*  Returns ONEDGE if the point lies on a mesh edge.  `searchtri' is a       */
 
5605
/*  handle whose primary edge is the edge on which the point lies.           */
 
5606
/*                                                                           */
 
5607
/*  Returns INTRIANGLE if the point lies strictly within a triangle.         */
 
5608
/*  `searchtri' is a handle on the triangle that contains the point.         */
 
5609
/*                                                                           */
 
5610
/*  Returns OUTSIDE if the point lies outside the mesh.  `searchtri' is a    */
 
5611
/*  handle whose primary edge the point is to the right of.  This might      */
 
5612
/*  occur when the circumcenter of a triangle falls just slightly outside    */
 
5613
/*  the mesh due to floating-point roundoff error.  It also occurs when      */
 
5614
/*  seeking a hole or region point that a foolish user has placed outside    */
 
5615
/*  the mesh.                                                                */
 
5616
/*                                                                           */
 
5617
/*  WARNING:  This routine is designed for convex triangulations, and will   */
 
5618
/*  not generally work after the holes and concavities have been carved.     */
 
5619
/*  However, it can still be used to find the circumcenter of a triangle, as */
 
5620
/*  long as the search is begun from the triangle in question.               */
 
5621
/*                                                                           */
 
5622
/*****************************************************************************/
 
5623
 
 
5624
enum locateresult preciselocate(
 
5625
point searchpoint,
 
5626
struct triedge *searchtri)
 
5627
{
 
5628
  struct triedge backtracktri;
 
5629
  point forg, fdest, fapex;
 
5630
  point swappoint;
 
5631
  REAL orgorient, destorient;
 
5632
  int moveleft;
 
5633
  triangle ptr;                         /* Temporary variable used by sym(). */
 
5634
 
 
5635
  if (verbose > 2) {
 
5636
    printf("  Searching for point (%.12g, %.12g).\n",
 
5637
           searchpoint[0], searchpoint[1]);
 
5638
  }
 
5639
  /* Where are we? */
 
5640
  org(*searchtri, forg);
 
5641
  dest(*searchtri, fdest);
 
5642
  apex(*searchtri, fapex);
 
5643
  while (1) {
 
5644
    if (verbose > 2) {
 
5645
      printf("    At (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
 
5646
             forg[0], forg[1], fdest[0], fdest[1], fapex[0], fapex[1]);
 
5647
    }
 
5648
    /* Check whether the apex is the point we seek. */
 
5649
    if ((fapex[0] == searchpoint[0]) && (fapex[1] == searchpoint[1])) {
 
5650
      lprevself(*searchtri);
 
5651
      return ONVERTEX;
 
5652
    }
 
5653
    /* Does the point lie on the other side of the line defined by the */
 
5654
    /*   triangle edge opposite the triangle's destination?            */
 
5655
    destorient = counterclockwise(forg, fapex, searchpoint);
 
5656
    /* Does the point lie on the other side of the line defined by the */
 
5657
    /*   triangle edge opposite the triangle's origin?                 */
 
5658
    orgorient = counterclockwise(fapex, fdest, searchpoint);
 
5659
    if (destorient > 0.0) {
 
5660
      if (orgorient > 0.0) {
 
5661
        /* Move left if the inner product of (fapex - searchpoint) and  */
 
5662
        /*   (fdest - forg) is positive.  This is equivalent to drawing */
 
5663
        /*   a line perpendicular to the line (forg, fdest) passing     */
 
5664
        /*   through `fapex', and determining which side of this line   */
 
5665
        /*   `searchpoint' falls on.                                    */
 
5666
        moveleft = (fapex[0] - searchpoint[0]) * (fdest[0] - forg[0]) +
 
5667
                   (fapex[1] - searchpoint[1]) * (fdest[1] - forg[1]) > 0.0;
 
5668
      } else {
 
5669
        moveleft = 1;
 
5670
      }
 
5671
    } else {
 
5672
      if (orgorient > 0.0) {
 
5673
        moveleft = 0;
 
5674
      } else {
 
5675
        /* The point we seek must be on the boundary of or inside this */
 
5676
        /*   triangle.                                                 */
 
5677
        if (destorient == 0.0) {
 
5678
          lprevself(*searchtri);
 
5679
          return ONEDGE;
 
5680
        }
 
5681
        if (orgorient == 0.0) {
 
5682
          lnextself(*searchtri);
 
5683
          return ONEDGE;
 
5684
        }
 
5685
        return INTRIANGLE;
 
5686
      }
 
5687
    }
 
5688
 
 
5689
    /* Move to another triangle.  Leave a trace `backtracktri' in case */
 
5690
    /*   floating-point roundoff or some such bogey causes us to walk  */
 
5691
    /*   off a boundary of the triangulation.  We can just bounce off  */
 
5692
    /*   the boundary as if it were an elastic band.                   */
 
5693
    if (moveleft) {
 
5694
      lprev(*searchtri, backtracktri);
 
5695
      fdest = fapex;
 
5696
    } else {
 
5697
      lnext(*searchtri, backtracktri);
 
5698
      forg = fapex;
 
5699
    }
 
5700
    sym(backtracktri, *searchtri);
 
5701
 
 
5702
    /* Check for walking off the edge. */
 
5703
    if (searchtri->tri == dummytri) {
 
5704
      /* Turn around. */
 
5705
      triedgecopy(backtracktri, *searchtri);
 
5706
      swappoint = forg;
 
5707
      forg = fdest;
 
5708
      fdest = swappoint;
 
5709
      apex(*searchtri, fapex);
 
5710
      /* Check if the point really is beyond the triangulation boundary. */
 
5711
      destorient = counterclockwise(forg, fapex, searchpoint);
 
5712
      orgorient = counterclockwise(fapex, fdest, searchpoint);
 
5713
      if ((orgorient < 0.0) && (destorient < 0.0)) {
 
5714
        return OUTSIDE;
 
5715
      }
 
5716
    } else {
 
5717
      apex(*searchtri, fapex);
 
5718
    }
 
5719
  }
 
5720
}
 
5721
 
 
5722
/*****************************************************************************/
 
5723
/*                                                                           */
 
5724
/*  locate()   Find a triangle or edge containing a given point.             */
 
5725
/*                                                                           */
 
5726
/*  Searching begins from one of:  the input `searchtri', a recently         */
 
5727
/*  encountered triangle `recenttri', or from a triangle chosen from a       */
 
5728
/*  random sample.  The choice is made by determining which triangle's       */
 
5729
/*  origin is closest to the point we are searcing for.  Normally,           */
 
5730
/*  `searchtri' should be a handle on the convex hull of the triangulation.  */
 
5731
/*                                                                           */
 
5732
/*  Details on the random sampling method can be found in the Mucke, Saias,  */
 
5733
/*  and Zhu paper cited in the header of this code.                          */
 
5734
/*                                                                           */
 
5735
/*  On completion, `searchtri' is a triangle that contains `searchpoint'.    */
 
5736
/*                                                                           */
 
5737
/*  Returns ONVERTEX if the point lies on an existing vertex.  `searchtri'   */
 
5738
/*  is a handle whose origin is the existing vertex.                         */
 
5739
/*                                                                           */
 
5740
/*  Returns ONEDGE if the point lies on a mesh edge.  `searchtri' is a       */
 
5741
/*  handle whose primary edge is the edge on which the point lies.           */
 
5742
/*                                                                           */
 
5743
/*  Returns INTRIANGLE if the point lies strictly within a triangle.         */
 
5744
/*  `searchtri' is a handle on the triangle that contains the point.         */
 
5745
/*                                                                           */
 
5746
/*  Returns OUTSIDE if the point lies outside the mesh.  `searchtri' is a    */
 
5747
/*  handle whose primary edge the point is to the right of.  This might      */
 
5748
/*  occur when the circumcenter of a triangle falls just slightly outside    */
 
5749
/*  the mesh due to floating-point roundoff error.  It also occurs when      */
 
5750
/*  seeking a hole or region point that a foolish user has placed outside    */
 
5751
/*  the mesh.                                                                */
 
5752
/*                                                                           */
 
5753
/*  WARNING:  This routine is designed for convex triangulations, and will   */
 
5754
/*  not generally work after the holes and concavities have been carved.     */
 
5755
/*                                                                           */
 
5756
/*****************************************************************************/
 
5757
 
 
5758
enum locateresult locate(
 
5759
point searchpoint,
 
5760
struct triedge *searchtri)
 
5761
{
 
5762
  VOID **sampleblock;
 
5763
  triangle *firsttri;
 
5764
  struct triedge sampletri;
 
5765
  point torg, tdest;
 
5766
  unsigned long alignptr;
 
5767
  REAL searchdist, dist;
 
5768
  REAL ahead;
 
5769
  long sampleblocks, samplesperblock, samplenum;
 
5770
  long triblocks;
 
5771
  long i, j;
 
5772
  triangle ptr;                         /* Temporary variable used by sym(). */
 
5773
 
 
5774
  if (verbose > 2) {
 
5775
    printf("  Randomly sampling for a triangle near point (%.12g, %.12g).\n",
 
5776
           searchpoint[0], searchpoint[1]);
 
5777
  }
 
5778
  /* Record the distance from the suggested starting triangle to the */
 
5779
  /*   point we seek.                                                */
 
5780
  org(*searchtri, torg);
 
5781
  searchdist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
 
5782
             + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
 
5783
  if (verbose > 2) {
 
5784
    printf("    Boundary triangle has origin (%.12g, %.12g).\n",
 
5785
           torg[0], torg[1]);
 
5786
  }
 
5787
 
 
5788
  /* If a recently encountered triangle has been recorded and has not been */
 
5789
  /*   deallocated, test it as a good starting point.                      */
 
5790
  if (recenttri.tri != (triangle *) NULL) {
 
5791
    if (recenttri.tri[3] != (triangle) NULL) {
 
5792
      org(recenttri, torg);
 
5793
      if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
 
5794
        triedgecopy(recenttri, *searchtri);
 
5795
        return ONVERTEX;
 
5796
      }
 
5797
      dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
 
5798
           + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
 
5799
      if (dist < searchdist) {
 
5800
        triedgecopy(recenttri, *searchtri);
 
5801
        searchdist = dist;
 
5802
        if (verbose > 2) {
 
5803
          printf("    Choosing recent triangle with origin (%.12g, %.12g).\n",
 
5804
                 torg[0], torg[1]);
 
5805
        }
 
5806
      }
 
5807
    }
 
5808
  }
 
5809
 
 
5810
  /* The number of random samples taken is proportional to the cube root of */
 
5811
  /*   the number of triangles in the mesh.  The next bit of code assumes   */
 
5812
  /*   that the number of triangles increases monotonically.                */
 
5813
  while (SAMPLEFACTOR * samples * samples * samples < triangles.items) {
 
5814
    samples++;
 
5815
  }
 
5816
  triblocks = (triangles.maxitems + TRIPERBLOCK - 1) / TRIPERBLOCK;
 
5817
  samplesperblock = 1 + (samples / triblocks);
 
5818
  sampleblocks = samples / samplesperblock;
 
5819
  sampleblock = triangles.firstblock;
 
5820
  sampletri.orient = 0;
 
5821
  for (i = 0; i < sampleblocks; i++) {
 
5822
    alignptr = (unsigned long) (sampleblock + 1);
 
5823
    firsttri = (triangle *) (alignptr + (unsigned long) triangles.alignbytes
 
5824
                          - (alignptr % (unsigned long) triangles.alignbytes));
 
5825
    for (j = 0; j < samplesperblock; j++) {
 
5826
      if (i == triblocks - 1) {
 
5827
        samplenum = randomnation((int)
 
5828
                                 (triangles.maxitems - (i * TRIPERBLOCK)));
 
5829
      } else {
 
5830
        samplenum = randomnation(TRIPERBLOCK);
 
5831
      }
 
5832
      sampletri.tri = (triangle *)
 
5833
                      (firsttri + (samplenum * triangles.itemwords));
 
5834
      if (sampletri.tri[3] != (triangle) NULL) {
 
5835
        org(sampletri, torg);
 
5836
        dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
 
5837
             + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
 
5838
        if (dist < searchdist) {
 
5839
          triedgecopy(sampletri, *searchtri);
 
5840
          searchdist = dist;
 
5841
          if (verbose > 2) {
 
5842
            printf("    Choosing triangle with origin (%.12g, %.12g).\n",
 
5843
                   torg[0], torg[1]);
 
5844
          }
 
5845
        }
 
5846
      }
 
5847
    }
 
5848
    sampleblock = (VOID **) *sampleblock;
 
5849
  }
 
5850
  /* Where are we? */
 
5851
  org(*searchtri, torg);
 
5852
  dest(*searchtri, tdest);
 
5853
  /* Check the starting triangle's vertices. */
 
5854
  if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
 
5855
    return ONVERTEX;
 
5856
  }
 
5857
  if ((tdest[0] == searchpoint[0]) && (tdest[1] == searchpoint[1])) {
 
5858
    lnextself(*searchtri);
 
5859
    return ONVERTEX;
 
5860
  }
 
5861
  /* Orient `searchtri' to fit the preconditions of calling preciselocate(). */
 
5862
  ahead = counterclockwise(torg, tdest, searchpoint);
 
5863
  if (ahead < 0.0) {
 
5864
    /* Turn around so that `searchpoint' is to the left of the */
 
5865
    /*   edge specified by `searchtri'.                        */
 
5866
    symself(*searchtri);
 
5867
  } else if (ahead == 0.0) {
 
5868
    /* Check if `searchpoint' is between `torg' and `tdest'. */
 
5869
    if (((torg[0] < searchpoint[0]) == (searchpoint[0] < tdest[0]))
 
5870
        && ((torg[1] < searchpoint[1]) == (searchpoint[1] < tdest[1]))) {
 
5871
      return ONEDGE;
 
5872
    }
 
5873
  }
 
5874
  return preciselocate(searchpoint, searchtri);
 
5875
}
 
5876
 
 
5877
/**                                                                         **/
 
5878
/**                                                                         **/
 
5879
/********* Point location routines end here                          *********/
 
5880
 
 
5881
/********* Mesh transformation routines begin here                   *********/
 
5882
/**                                                                         **/
 
5883
/**                                                                         **/
 
5884
 
 
5885
/*****************************************************************************/
 
5886
/*                                                                           */
 
5887
/*  insertshelle()   Create a new shell edge and insert it between two       */
 
5888
/*                   triangles.                                              */
 
5889
/*                                                                           */
 
5890
/*  The new shell edge is inserted at the edge described by the handle       */
 
5891
/*  `tri'.  Its vertices are properly initialized.  The marker `shellemark'  */
 
5892
/*  is applied to the shell edge and, if appropriate, its vertices.          */
 
5893
/*                                                                           */
 
5894
/*****************************************************************************/
 
5895
 
 
5896
void insertshelle(
 
5897
struct triedge *tri,          /* Edge at which to insert the new shell edge. */
 
5898
int shellemark)                            /* Marker for the new shell edge. */
 
5899
{
 
5900
  struct triedge oppotri;
 
5901
  struct edge newshelle;
 
5902
  point triorg, tridest;
 
5903
  triangle ptr;                         /* Temporary variable used by sym(). */
 
5904
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
5905
 
 
5906
  /* Mark points if possible. */
 
5907
  org(*tri, triorg);
 
5908
  dest(*tri, tridest);
 
5909
  if (pointmark(triorg) == 0) {
 
5910
    setpointmark(triorg, shellemark);
 
5911
  }
 
5912
  if (pointmark(tridest) == 0) {
 
5913
    setpointmark(tridest, shellemark);
 
5914
  }
 
5915
  /* Check if there's already a shell edge here. */
 
5916
  tspivot(*tri, newshelle);
 
5917
  if (newshelle.sh == dummysh) {
 
5918
    /* Make new shell edge and initialize its vertices. */
 
5919
    makeshelle(&newshelle);
 
5920
    setsorg(newshelle, tridest);
 
5921
    setsdest(newshelle, triorg);
 
5922
    /* Bond new shell edge to the two triangles it is sandwiched between. */
 
5923
    /*   Note that the facing triangle `oppotri' might be equal to        */
 
5924
    /*   `dummytri' (outer space), but the new shell edge is bonded to it */
 
5925
    /*   all the same.                                                    */
 
5926
    tsbond(*tri, newshelle);
 
5927
    sym(*tri, oppotri);
 
5928
    ssymself(newshelle);
 
5929
    tsbond(oppotri, newshelle);
 
5930
    setmark(newshelle, shellemark);
 
5931
    if (verbose > 2) {
 
5932
      printf("  Inserting new ");
 
5933
      printshelle(&newshelle);
 
5934
    }
 
5935
  } else {
 
5936
    if (mark(newshelle) == 0) {
 
5937
      setmark(newshelle, shellemark);
 
5938
    }
 
5939
  }
 
5940
}
 
5941
 
 
5942
/*****************************************************************************/
 
5943
/*                                                                           */
 
5944
/*  Terminology                                                              */
 
5945
/*                                                                           */
 
5946
/*  A "local transformation" replaces a small set of triangles with another  */
 
5947
/*  set of triangles.  This may or may not involve inserting or deleting a   */
 
5948
/*  point.                                                                   */
 
5949
/*                                                                           */
 
5950
/*  The term "casing" is used to describe the set of triangles that are      */
 
5951
/*  attached to the triangles being transformed, but are not transformed     */
 
5952
/*  themselves.  Think of the casing as a fixed hollow structure inside      */
 
5953
/*  which all the action happens.  A "casing" is only defined relative to    */
 
5954
/*  a single transformation; each occurrence of a transformation will        */
 
5955
/*  involve a different casing.                                              */
 
5956
/*                                                                           */
 
5957
/*  A "shell" is similar to a "casing".  The term "shell" describes the set  */
 
5958
/*  of shell edges (if any) that are attached to the triangles being         */
 
5959
/*  transformed.  However, I sometimes use "shell" to refer to a single      */
 
5960
/*  shell edge, so don't get confused.                                       */
 
5961
/*                                                                           */
 
5962
/*****************************************************************************/
 
5963
 
 
5964
/*****************************************************************************/
 
5965
/*                                                                           */
 
5966
/*  flip()   Transform two triangles to two different triangles by flipping  */
 
5967
/*           an edge within a quadrilateral.                                 */
 
5968
/*                                                                           */
 
5969
/*  Imagine the original triangles, abc and bad, oriented so that the        */
 
5970
/*  shared edge ab lies in a horizontal plane, with the point b on the left  */
 
5971
/*  and the point a on the right.  The point c lies below the edge, and the  */
 
5972
/*  point d lies above the edge.  The `flipedge' handle holds the edge ab    */
 
5973
/*  of triangle abc, and is directed left, from vertex a to vertex b.        */
 
5974
/*                                                                           */
 
5975
/*  The triangles abc and bad are deleted and replaced by the triangles cdb  */
 
5976
/*  and dca.  The triangles that represent abc and bad are NOT deallocated;  */
 
5977
/*  they are reused for dca and cdb, respectively.  Hence, any handles that  */
 
5978
/*  may have held the original triangles are still valid, although not       */
 
5979
/*  directed as they were before.                                            */
 
5980
/*                                                                           */
 
5981
/*  Upon completion of this routine, the `flipedge' handle holds the edge    */
 
5982
/*  dc of triangle dca, and is directed down, from vertex d to vertex c.     */
 
5983
/*  (Hence, the two triangles have rotated counterclockwise.)                */
 
5984
/*                                                                           */
 
5985
/*  WARNING:  This transformation is geometrically valid only if the         */
 
5986
/*  quadrilateral adbc is convex.  Furthermore, this transformation is       */
 
5987
/*  valid only if there is not a shell edge between the triangles abc and    */
 
5988
/*  bad.  This routine does not check either of these preconditions, and     */
 
5989
/*  it is the responsibility of the calling routine to ensure that they are  */
 
5990
/*  met.  If they are not, the streets shall be filled with wailing and      */
 
5991
/*  gnashing of teeth.                                                       */
 
5992
/*                                                                           */
 
5993
/*****************************************************************************/
 
5994
 
 
5995
void flip(
 
5996
struct triedge *flipedge)                    /* Handle for the triangle abc. */
 
5997
{
 
5998
  struct triedge botleft, botright;
 
5999
  struct triedge topleft, topright;
 
6000
  struct triedge top;
 
6001
  struct triedge botlcasing, botrcasing;
 
6002
  struct triedge toplcasing, toprcasing;
 
6003
  struct edge botlshelle, botrshelle;
 
6004
  struct edge toplshelle, toprshelle;
 
6005
  point leftpoint, rightpoint, botpoint;
 
6006
  point farpoint;
 
6007
  triangle ptr;                         /* Temporary variable used by sym(). */
 
6008
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
6009
 
 
6010
  /* Identify the vertices of the quadrilateral. */
 
6011
  org(*flipedge, rightpoint);
 
6012
  dest(*flipedge, leftpoint);
 
6013
  apex(*flipedge, botpoint);
 
6014
  sym(*flipedge, top);
 
6015
#ifdef SELF_CHECK
 
6016
  if (top.tri == dummytri) {
 
6017
    printf("Internal error in flip():  Attempt to flip on boundary.\n");
 
6018
    lnextself(*flipedge);
 
6019
    return;
 
6020
  }
 
6021
  if (checksegments) {
 
6022
    tspivot(*flipedge, toplshelle);
 
6023
    if (toplshelle.sh != dummysh) {
 
6024
      printf("Internal error in flip():  Attempt to flip a segment.\n");
 
6025
      lnextself(*flipedge);
 
6026
      return;
 
6027
    }
 
6028
  }
 
6029
#endif /* SELF_CHECK */
 
6030
  apex(top, farpoint);
 
6031
 
 
6032
  /* Identify the casing of the quadrilateral. */
 
6033
  lprev(top, topleft);
 
6034
  sym(topleft, toplcasing);
 
6035
  lnext(top, topright);
 
6036
  sym(topright, toprcasing);
 
6037
  lnext(*flipedge, botleft);
 
6038
  sym(botleft, botlcasing);
 
6039
  lprev(*flipedge, botright);
 
6040
  sym(botright, botrcasing);
 
6041
  /* Rotate the quadrilateral one-quarter turn counterclockwise. */
 
6042
  bond(topleft, botlcasing);
 
6043
  bond(botleft, botrcasing);
 
6044
  bond(botright, toprcasing);
 
6045
  bond(topright, toplcasing);
 
6046
 
 
6047
  if (checksegments) {
 
6048
    /* Check for shell edges and rebond them to the quadrilateral. */
 
6049
    tspivot(topleft, toplshelle);
 
6050
    tspivot(botleft, botlshelle);
 
6051
    tspivot(botright, botrshelle);
 
6052
    tspivot(topright, toprshelle);
 
6053
    if (toplshelle.sh == dummysh) {
 
6054
      tsdissolve(topright);
 
6055
    } else {
 
6056
      tsbond(topright, toplshelle);
 
6057
    }
 
6058
    if (botlshelle.sh == dummysh) {
 
6059
      tsdissolve(topleft);
 
6060
    } else {
 
6061
      tsbond(topleft, botlshelle);
 
6062
    }
 
6063
    if (botrshelle.sh == dummysh) {
 
6064
      tsdissolve(botleft);
 
6065
    } else {
 
6066
      tsbond(botleft, botrshelle);
 
6067
    }
 
6068
    if (toprshelle.sh == dummysh) {
 
6069
      tsdissolve(botright);
 
6070
    } else {
 
6071
      tsbond(botright, toprshelle);
 
6072
    }
 
6073
  }
 
6074
 
 
6075
  /* New point assignments for the rotated quadrilateral. */
 
6076
  setorg(*flipedge, farpoint);
 
6077
  setdest(*flipedge, botpoint);
 
6078
  setapex(*flipedge, rightpoint);
 
6079
  setorg(top, botpoint);
 
6080
  setdest(top, farpoint);
 
6081
  setapex(top, leftpoint);
 
6082
  if (verbose > 2) {
 
6083
    printf("  Edge flip results in left ");
 
6084
    lnextself(topleft);
 
6085
    printtriangle(&topleft);
 
6086
    printf("  and right ");
 
6087
    printtriangle(flipedge);
 
6088
  }
 
6089
}
 
6090
 
 
6091
/*****************************************************************************/
 
6092
/*                                                                           */
 
6093
/*  insertsite()   Insert a vertex into a Delaunay triangulation,            */
 
6094
/*                 performing flips as necessary to maintain the Delaunay    */
 
6095
/*                 property.                                                 */
 
6096
/*                                                                           */
 
6097
/*  The point `insertpoint' is located.  If `searchtri.tri' is not NULL,     */
 
6098
/*  the search for the containing triangle begins from `searchtri'.  If      */
 
6099
/*  `searchtri.tri' is NULL, a full point location procedure is called.      */
 
6100
/*  If `insertpoint' is found inside a triangle, the triangle is split into  */
 
6101
/*  three; if `insertpoint' lies on an edge, the edge is split in two,       */
 
6102
/*  thereby splitting the two adjacent triangles into four.  Edge flips are  */
 
6103
/*  used to restore the Delaunay property.  If `insertpoint' lies on an      */
 
6104
/*  existing vertex, no action is taken, and the value DUPLICATEPOINT is     */
 
6105
/*  returned.  On return, `searchtri' is set to a handle whose origin is the */
 
6106
/*  existing vertex.                                                         */
 
6107
/*                                                                           */
 
6108
/*  Normally, the parameter `splitedge' is set to NULL, implying that no     */
 
6109
/*  segment should be split.  In this case, if `insertpoint' is found to     */
 
6110
/*  lie on a segment, no action is taken, and the value VIOLATINGPOINT is    */
 
6111
/*  returned.  On return, `searchtri' is set to a handle whose primary edge  */
 
6112
/*  is the violated segment.                                                 */
 
6113
/*                                                                           */
 
6114
/*  If the calling routine wishes to split a segment by inserting a point in */
 
6115
/*  it, the parameter `splitedge' should be that segment.  In this case,     */
 
6116
/*  `searchtri' MUST be the triangle handle reached by pivoting from that    */
 
6117
/*  segment; no point location is done.                                      */
 
6118
/*                                                                           */
 
6119
/*  `segmentflaws' and `triflaws' are flags that indicate whether or not     */
 
6120
/*  there should be checks for the creation of encroached segments or bad    */
 
6121
/*  quality faces.  If a newly inserted point encroaches upon segments,      */
 
6122
/*  these segments are added to the list of segments to be split if          */
 
6123
/*  `segmentflaws' is set.  If bad triangles are created, these are added    */
 
6124
/*  to the queue if `triflaws' is set.                                       */
 
6125
/*                                                                           */
 
6126
/*  If a duplicate point or violated segment does not prevent the point      */
 
6127
/*  from being inserted, the return value will be ENCROACHINGPOINT if the    */
 
6128
/*  point encroaches upon a segment (and checking is enabled), or            */
 
6129
/*  SUCCESSFULPOINT otherwise.  In either case, `searchtri' is set to a      */
 
6130
/*  handle whose origin is the newly inserted vertex.                        */
 
6131
/*                                                                           */
 
6132
/*  insertsite() does not use flip() for reasons of speed; some              */
 
6133
/*  information can be reused from edge flip to edge flip, like the          */
 
6134
/*  locations of shell edges.                                                */
 
6135
/*                                                                           */
 
6136
/*****************************************************************************/
 
6137
 
 
6138
enum insertsiteresult insertsite(
 
6139
point insertpoint,
 
6140
struct triedge *searchtri,
 
6141
struct edge *splitedge,
 
6142
int segmentflaws,
 
6143
int triflaws)
 
6144
{
 
6145
  struct triedge horiz;
 
6146
  struct triedge top;
 
6147
  struct triedge botleft, botright;
 
6148
  struct triedge topleft, topright;
 
6149
  struct triedge newbotleft, newbotright;
 
6150
  struct triedge newtopright;
 
6151
  struct triedge botlcasing, botrcasing;
 
6152
  struct triedge toplcasing, toprcasing;
 
6153
  struct triedge testtri;
 
6154
  struct edge botlshelle, botrshelle;
 
6155
  struct edge toplshelle, toprshelle;
 
6156
  struct edge brokenshelle;
 
6157
  struct edge checkshelle;
 
6158
  struct edge rightedge;
 
6159
  struct edge newedge;
 
6160
  struct edge *encroached;
 
6161
  point first;
 
6162
  point leftpoint, rightpoint, botpoint, toppoint, farpoint;
 
6163
  REAL attrib;
 
6164
  REAL area;
 
6165
  enum insertsiteresult success;
 
6166
  enum locateresult intersect;
 
6167
  int doflip;
 
6168
  int mirrorflag;
 
6169
  int i;
 
6170
  triangle ptr;                         /* Temporary variable used by sym(). */
 
6171
  shelle sptr;         /* Temporary variable used by spivot() and tspivot(). */
 
6172
 
 
6173
  if (verbose > 1) {
 
6174
    printf("  Inserting (%.12g, %.12g).\n", insertpoint[0], insertpoint[1]);
 
6175
  }
 
6176
  if (splitedge == (struct edge *) NULL) {
 
6177
    /* Find the location of the point to be inserted.  Check if a good */
 
6178
    /*   starting triangle has already been provided by the caller.    */
 
6179
    if (searchtri->tri == (triangle *) NULL) {
 
6180
      /* Find a boundary triangle. */
 
6181
      horiz.tri = dummytri;
 
6182
      horiz.orient = 0;
 
6183
      symself(horiz);
 
6184
      /* Search for a triangle containing `insertpoint'. */
 
6185
      intersect = locate(insertpoint, &horiz);
 
6186
    } else {
 
6187
      /* Start searching from the triangle provided by the caller. */
 
6188
      triedgecopy(*searchtri, horiz);
 
6189
      intersect = preciselocate(insertpoint, &horiz);
 
6190
    }
 
6191
  } else {
 
6192
    /* The calling routine provides the edge in which the point is inserted. */
 
6193
    triedgecopy(*searchtri, horiz);
 
6194
    intersect = ONEDGE;
 
6195
  }
 
6196
  if (intersect == ONVERTEX) {
 
6197
    /* There's already a vertex there.  Return in `searchtri' a triangle */
 
6198
    /*   whose origin is the existing vertex.                            */
 
6199
    triedgecopy(horiz, *searchtri);
 
6200
    triedgecopy(horiz, recenttri);
 
6201
    return DUPLICATEPOINT;
 
6202
  }
 
6203
  if ((intersect == ONEDGE) || (intersect == OUTSIDE)) {
 
6204
    /* The vertex falls on an edge or boundary. */
 
6205
    if (checksegments && (splitedge == (struct edge *) NULL)) {
 
6206
      /* Check whether the vertex falls on a shell edge. */
 
6207
      tspivot(horiz, brokenshelle);
 
6208
      if (brokenshelle.sh != dummysh) {
 
6209
        /* The vertex falls on a shell edge. */
 
6210
        if (segmentflaws) {
 
6211
          if (nobisect == 0) {
 
6212
            /* Add the shell edge to the list of encroached segments. */
 
6213
            encroached = (struct edge *) poolalloc(&badsegments);
 
6214
            shellecopy(brokenshelle, *encroached);
 
6215
          } else if ((nobisect == 1) && (intersect == ONEDGE)) {
 
6216
            /* This segment may be split only if it is an internal boundary. */
 
6217
            sym(horiz, testtri);
 
6218
            if (testtri.tri != dummytri) {
 
6219
              /* Add the shell edge to the list of encroached segments. */
 
6220
              encroached = (struct edge *) poolalloc(&badsegments);
 
6221
              shellecopy(brokenshelle, *encroached);
 
6222
            }
 
6223
          }
 
6224
        }
 
6225
        /* Return a handle whose primary edge contains the point, */
 
6226
        /*   which has not been inserted.                         */
 
6227
        triedgecopy(horiz, *searchtri);
 
6228
        triedgecopy(horiz, recenttri);
 
6229
        return VIOLATINGPOINT;
 
6230
      }
 
6231
    }
 
6232
    /* Insert the point on an edge, dividing one triangle into two (if */
 
6233
    /*   the edge lies on a boundary) or two triangles into four.      */
 
6234
    lprev(horiz, botright);
 
6235
    sym(botright, botrcasing);
 
6236
    sym(horiz, topright);
 
6237
    /* Is there a second triangle?  (Or does this edge lie on a boundary?) */
 
6238
    mirrorflag = topright.tri != dummytri;
 
6239
    if (mirrorflag) {
 
6240
      lnextself(topright);
 
6241
      sym(topright, toprcasing);
 
6242
      maketriangle(&newtopright);
 
6243
    } else {
 
6244
      /* Splitting the boundary edge increases the number of boundary edges. */
 
6245
      hullsize++;
 
6246
    }
 
6247
    maketriangle(&newbotright);
 
6248
 
 
6249
    /* Set the vertices of changed and new triangles. */
 
6250
    org(horiz, rightpoint);
 
6251
    dest(horiz, leftpoint);
 
6252
    apex(horiz, botpoint);
 
6253
    setorg(newbotright, botpoint);
 
6254
    setdest(newbotright, rightpoint);
 
6255
    setapex(newbotright, insertpoint);
 
6256
    setorg(horiz, insertpoint);
 
6257
    for (i = 0; i < eextras; i++) {
 
6258
      /* Set the element attributes of a new triangle. */
 
6259
      setelemattribute(newbotright, i, elemattribute(botright, i));
 
6260
    }
 
6261
    if (vararea) {
 
6262
      /* Set the area constraint of a new triangle. */
 
6263
      setareabound(newbotright, areabound(botright));
 
6264
    }
 
6265
    if (mirrorflag) {
 
6266
      dest(topright, toppoint);
 
6267
      setorg(newtopright, rightpoint);
 
6268
      setdest(newtopright, toppoint);
 
6269
      setapex(newtopright, insertpoint);
 
6270
      setorg(topright, insertpoint);
 
6271
      for (i = 0; i < eextras; i++) {
 
6272
        /* Set the element attributes of another new triangle. */
 
6273
        setelemattribute(newtopright, i, elemattribute(topright, i));
 
6274
      }
 
6275
      if (vararea) {
 
6276
        /* Set the area constraint of another new triangle. */
 
6277
        setareabound(newtopright, areabound(topright));
 
6278
      }
 
6279
    }
 
6280
 
 
6281
    /* There may be shell edges that need to be bonded */
 
6282
    /*   to the new triangle(s).                       */
 
6283
    if (checksegments) {
 
6284
      tspivot(botright, botrshelle);
 
6285
      if (botrshelle.sh != dummysh) {
 
6286
        tsdissolve(botright);
 
6287
        tsbond(newbotright, botrshelle);
 
6288
      }
 
6289
      if (mirrorflag) {
 
6290
        tspivot(topright, toprshelle);
 
6291
        if (toprshelle.sh != dummysh) {
 
6292
          tsdissolve(topright);
 
6293
          tsbond(newtopright, toprshelle);
 
6294
        }
 
6295
      }
 
6296
    }
 
6297
 
 
6298
    /* Bond the new triangle(s) to the surrounding triangles. */
 
6299
    bond(newbotright, botrcasing);
 
6300
    lprevself(newbotright);
 
6301
    bond(newbotright, botright);
 
6302
    lprevself(newbotright);
 
6303
    if (mirrorflag) {
 
6304
      bond(newtopright, toprcasing);
 
6305
      lnextself(newtopright);
 
6306
      bond(newtopright, topright);
 
6307
      lnextself(newtopright);
 
6308
      bond(newtopright, newbotright);
 
6309
    }
 
6310
 
 
6311
    if (splitedge != (struct edge *) NULL) {
 
6312
      /* Split the shell edge into two. */
 
6313
      setsdest(*splitedge, insertpoint);
 
6314
      ssymself(*splitedge);
 
6315
      spivot(*splitedge, rightedge);
 
6316
      insertshelle(&newbotright, mark(*splitedge));
 
6317
      tspivot(newbotright, newedge);
 
6318
      sbond(*splitedge, newedge);
 
6319
      ssymself(newedge);
 
6320
      sbond(newedge, rightedge);
 
6321
      ssymself(*splitedge);
 
6322
    }
 
6323
 
 
6324
#ifdef SELF_CHECK
 
6325
    if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
 
6326
      printf("Internal error in insertsite():\n");
 
6327
      printf("  Clockwise triangle prior to edge point insertion (bottom).\n");
 
6328
    }
 
6329
    if (mirrorflag) {
 
6330
      if (counterclockwise(leftpoint, rightpoint, toppoint) < 0.0) {
 
6331
        printf("Internal error in insertsite():\n");
 
6332
        printf("  Clockwise triangle prior to edge point insertion (top).\n");
 
6333
      }
 
6334
      if (counterclockwise(rightpoint, toppoint, insertpoint) < 0.0) {
 
6335
        printf("Internal error in insertsite():\n");
 
6336
        printf("  Clockwise triangle after edge point insertion (top right).\n"
 
6337
               );
 
6338
      }
 
6339
      if (counterclockwise(toppoint, leftpoint, insertpoint) < 0.0) {
 
6340
        printf("Internal error in insertsite():\n");
 
6341
        printf("  Clockwise triangle after edge point insertion (top left).\n"
 
6342
               );
 
6343
      }
 
6344
    }
 
6345
    if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
 
6346
      printf("Internal error in insertsite():\n");
 
6347
      printf("  Clockwise triangle after edge point insertion (bottom left).\n"
 
6348
             );
 
6349
    }
 
6350
    if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
 
6351
      printf("Internal error in insertsite():\n");
 
6352
      printf(
 
6353
        "  Clockwise triangle after edge point insertion (bottom right).\n");
 
6354
    }
 
6355
#endif /* SELF_CHECK */
 
6356
    if (verbose > 2) {
 
6357
      printf("  Updating bottom left ");
 
6358
      printtriangle(&botright);
 
6359
      if (mirrorflag) {
 
6360
        printf("  Updating top left ");
 
6361
        printtriangle(&topright);
 
6362
        printf("  Creating top right ");
 
6363
        printtriangle(&newtopright);
 
6364
      }
 
6365
      printf("  Creating bottom right ");
 
6366
      printtriangle(&newbotright);
 
6367
    }
 
6368
 
 
6369
    /* Position `horiz' on the first edge to check for */
 
6370
    /*   the Delaunay property.                        */
 
6371
    lnextself(horiz);
 
6372
  } else {
 
6373
    /* Insert the point in a triangle, splitting it into three. */
 
6374
    lnext(horiz, botleft);
 
6375
    lprev(horiz, botright);
 
6376
    sym(botleft, botlcasing);
 
6377
    sym(botright, botrcasing);
 
6378
    maketriangle(&newbotleft);
 
6379
    maketriangle(&newbotright);
 
6380
 
 
6381
    /* Set the vertices of changed and new triangles. */
 
6382
    org(horiz, rightpoint);
 
6383
    dest(horiz, leftpoint);
 
6384
    apex(horiz, botpoint);
 
6385
    setorg(newbotleft, leftpoint);
 
6386
    setdest(newbotleft, botpoint);
 
6387
    setapex(newbotleft, insertpoint);
 
6388
    setorg(newbotright, botpoint);
 
6389
    setdest(newbotright, rightpoint);
 
6390
    setapex(newbotright, insertpoint);
 
6391
    setapex(horiz, insertpoint);
 
6392
    for (i = 0; i < eextras; i++) {
 
6393
      /* Set the element attributes of the new triangles. */
 
6394
      attrib = elemattribute(horiz, i);
 
6395
      setelemattribute(newbotleft, i, attrib);
 
6396
      setelemattribute(newbotright, i, attrib);
 
6397
    }
 
6398
    if (vararea) {
 
6399
      /* Set the area constraint of the new triangles. */
 
6400
      area = areabound(horiz);
 
6401
      setareabound(newbotleft, area);
 
6402
      setareabound(newbotright, area);
 
6403
    }
 
6404
 
 
6405
    /* There may be shell edges that need to be bonded */
 
6406
    /*   to the new triangles.                         */
 
6407
    if (checksegments) {
 
6408
      tspivot(botleft, botlshelle);
 
6409
      if (botlshelle.sh != dummysh) {
 
6410
        tsdissolve(botleft);
 
6411
        tsbond(newbotleft, botlshelle);
 
6412
      }
 
6413
      tspivot(botright, botrshelle);
 
6414
      if (botrshelle.sh != dummysh) {
 
6415
        tsdissolve(botright);
 
6416
        tsbond(newbotright, botrshelle);
 
6417
      }
 
6418
    }
 
6419
 
 
6420
    /* Bond the new triangles to the surrounding triangles. */
 
6421
    bond(newbotleft, botlcasing);
 
6422
    bond(newbotright, botrcasing);
 
6423
    lnextself(newbotleft);
 
6424
    lprevself(newbotright);
 
6425
    bond(newbotleft, newbotright);
 
6426
    lnextself(newbotleft);
 
6427
    bond(botleft, newbotleft);
 
6428
    lprevself(newbotright);
 
6429
    bond(botright, newbotright);
 
6430
 
 
6431
#ifdef SELF_CHECK
 
6432
    if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
 
6433
      printf("Internal error in insertsite():\n");
 
6434
      printf("  Clockwise triangle prior to point insertion.\n");
 
6435
    }
 
6436
    if (counterclockwise(rightpoint, leftpoint, insertpoint) < 0.0) {
 
6437
      printf("Internal error in insertsite():\n");
 
6438
      printf("  Clockwise triangle after point insertion (top).\n");
 
6439
    }
 
6440
    if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
 
6441
      printf("Internal error in insertsite():\n");
 
6442
      printf("  Clockwise triangle after point insertion (left).\n");
 
6443
    }
 
6444
    if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
 
6445
      printf("Internal error in insertsite():\n");
 
6446
      printf("  Clockwise triangle after point insertion (right).\n");
 
6447
    }
 
6448
#endif /* SELF_CHECK */
 
6449
    if (verbose > 2) {
 
6450
      printf("  Updating top ");
 
6451
      printtriangle(&horiz);
 
6452
      printf("  Creating left ");
 
6453
      printtriangle(&newbotleft);
 
6454
      printf("  Creating right ");
 
6455
      printtriangle(&newbotright);
 
6456
    }
 
6457
  }
 
6458
 
 
6459
  /* The insertion is successful by default, unless an encroached */
 
6460
  /*   edge is found.                                             */
 
6461
  success = SUCCESSFULPOINT;
 
6462
  /* Circle around the newly inserted vertex, checking each edge opposite */
 
6463
  /*   it for the Delaunay property.  Non-Delaunay edges are flipped.     */
 
6464
  /*   `horiz' is always the edge being checked.  `first' marks where to  */
 
6465
  /*   stop circling.                                                     */
 
6466
  org(horiz, first);
 
6467
  rightpoint = first;
 
6468
  dest(horiz, leftpoint);
 
6469
  /* Circle until finished. */
 
6470
  while (1) {
 
6471
    /* By default, the edge will be flipped. */
 
6472
    doflip = 1;
 
6473
    if (checksegments) {
 
6474
      /* Check for a segment, which cannot be flipped. */
 
6475
      tspivot(horiz, checkshelle);
 
6476
      if (checkshelle.sh != dummysh) {
 
6477
        /* The edge is a segment and cannot be flipped. */
 
6478
        doflip = 0;
 
6479
#ifndef CDT_ONLY
 
6480
        if (segmentflaws) {
 
6481
          /* Does the new point encroach upon this segment? */
 
6482
          if (checkedge4encroach(&checkshelle)) {
 
6483
            success = ENCROACHINGPOINT;
 
6484
          }
 
6485
        }
 
6486
#endif /* not CDT_ONLY */
 
6487
      }
 
6488
    }
 
6489
    if (doflip) {
 
6490
      /* Check if the edge is a boundary edge. */
 
6491
      sym(horiz, top);
 
6492
      if (top.tri == dummytri) {
 
6493
        /* The edge is a boundary edge and cannot be flipped. */
 
6494
        doflip = 0;
 
6495
      } else {
 
6496
        /* Find the point on the other side of the edge. */
 
6497
        apex(top, farpoint);
 
6498
        /* In the incremental Delaunay triangulation algorithm, any of    */
 
6499
        /*   `leftpoint', `rightpoint', and `farpoint' could be vertices  */
 
6500
        /*   of the triangular bounding box.  These vertices must be      */
 
6501
        /*   treated as if they are infinitely distant, even though their */
 
6502
        /*   "coordinates" are not.                                       */
 
6503
        if ((leftpoint == infpoint1) || (leftpoint == infpoint2)
 
6504
                   || (leftpoint == infpoint3)) {
 
6505
          /* `leftpoint' is infinitely distant.  Check the convexity of */
 
6506
          /*   the boundary of the triangulation.  'farpoint' might be  */
 
6507
          /*   infinite as well, but trust me, this same condition      */
 
6508
          /*   should be applied.                                       */
 
6509
          doflip = counterclockwise(insertpoint, rightpoint, farpoint) > 0.0;
 
6510
        } else if ((rightpoint == infpoint1) || (rightpoint == infpoint2)
 
6511
                   || (rightpoint == infpoint3)) {
 
6512
          /* `rightpoint' is infinitely distant.  Check the convexity of */
 
6513
          /*   the boundary of the triangulation.  'farpoint' might be  */
 
6514
          /*   infinite as well, but trust me, this same condition      */
 
6515
          /*   should be applied.                                       */
 
6516
          doflip = counterclockwise(farpoint, leftpoint, insertpoint) > 0.0;
 
6517
        } else if ((farpoint == infpoint1) || (farpoint == infpoint2)
 
6518
            || (farpoint == infpoint3)) {
 
6519
          /* `farpoint' is infinitely distant and cannot be inside */
 
6520
          /*   the circumcircle of the triangle `horiz'.           */
 
6521
          doflip = 0;
 
6522
        } else {
 
6523
          /* Test whether the edge is locally Delaunay. */
 
6524
          doflip = incircle(leftpoint, insertpoint, rightpoint, farpoint)
 
6525
                   > 0.0;
 
6526
        }
 
6527
        if (doflip) {
 
6528
          /* We made it!  Flip the edge `horiz' by rotating its containing */
 
6529
          /*   quadrilateral (the two triangles adjacent to `horiz').      */
 
6530
          /* Identify the casing of the quadrilateral. */
 
6531
          lprev(top, topleft);
 
6532
          sym(topleft, toplcasing);
 
6533
          lnext(top, topright);
 
6534
          sym(topright, toprcasing);
 
6535
          lnext(horiz, botleft);
 
6536
          sym(botleft, botlcasing);
 
6537
          lprev(horiz, botright);
 
6538
          sym(botright, botrcasing);
 
6539
          /* Rotate the quadrilateral one-quarter turn counterclockwise. */
 
6540
          bond(topleft, botlcasing);
 
6541
          bond(botleft, botrcasing);
 
6542
          bond(botright, toprcasing);
 
6543
          bond(topright, toplcasing);
 
6544
          if (checksegments) {
 
6545
            /* Check for shell edges and rebond them to the quadrilateral. */
 
6546
            tspivot(topleft, toplshelle);
 
6547
            tspivot(botleft, botlshelle);
 
6548
            tspivot(botright, botrshelle);
 
6549
            tspivot(topright, toprshelle);
 
6550
            if (toplshelle.sh == dummysh) {
 
6551
              tsdissolve(topright);
 
6552
            } else {
 
6553
              tsbond(topright, toplshelle);
 
6554
            }
 
6555
            if (botlshelle.sh == dummysh) {
 
6556
              tsdissolve(topleft);
 
6557
            } else {
 
6558
              tsbond(topleft, botlshelle);
 
6559
            }
 
6560
            if (botrshelle.sh == dummysh) {
 
6561
              tsdissolve(botleft);
 
6562
            } else {
 
6563
              tsbond(botleft, botrshelle);
 
6564
            }
 
6565
            if (toprshelle.sh == dummysh) {
 
6566
              tsdissolve(botright);
 
6567
            } else {
 
6568
              tsbond(botright, toprshelle);
 
6569
            }
 
6570
          }
 
6571
          /* New point assignments for the rotated quadrilateral. */
 
6572
          setorg(horiz, farpoint);
 
6573
          setdest(horiz, insertpoint);
 
6574
          setapex(horiz, rightpoint);
 
6575
          setorg(top, insertpoint);
 
6576
          setdest(top, farpoint);
 
6577
          setapex(top, leftpoint);
 
6578
          for (i = 0; i < eextras; i++) {
 
6579
            /* Take the average of the two triangles' attributes. */
 
6580
            attrib = 0.5 * (elemattribute(top, i) + elemattribute(horiz, i));
 
6581
            setelemattribute(top, i, attrib);
 
6582
            setelemattribute(horiz, i, attrib);
 
6583
          }
 
6584
          if (vararea) {
 
6585
            if ((areabound(top) <= 0.0) || (areabound(horiz) <= 0.0)) {
 
6586
              area = -1.0;
 
6587
            } else {
 
6588
              /* Take the average of the two triangles' area constraints.    */
 
6589
              /*   This prevents small area constraints from migrating a     */
 
6590
              /*   long, long way from their original location due to flips. */
 
6591
              area = 0.5 * (areabound(top) + areabound(horiz));
 
6592
            }
 
6593
            setareabound(top, area);
 
6594
            setareabound(horiz, area);
 
6595
          }
 
6596
#ifdef SELF_CHECK
 
6597
          if (insertpoint != (point) NULL) {
 
6598
            if (counterclockwise(leftpoint, insertpoint, rightpoint) < 0.0) {
 
6599
              printf("Internal error in insertsite():\n");
 
6600
              printf("  Clockwise triangle prior to edge flip (bottom).\n");
 
6601
            }
 
6602
            /* The following test has been removed because constrainededge() */
 
6603
            /*   sometimes generates inverted triangles that insertsite()    */
 
6604
            /*   removes.                                                    */
 
6605
/*
 
6606
            if (counterclockwise(rightpoint, farpoint, leftpoint) < 0.0) {
 
6607
              printf("Internal error in insertsite():\n");
 
6608
              printf("  Clockwise triangle prior to edge flip (top).\n");
 
6609
            }
 
6610
*/
 
6611
            if (counterclockwise(farpoint, leftpoint, insertpoint) < 0.0) {
 
6612
              printf("Internal error in insertsite():\n");
 
6613
              printf("  Clockwise triangle after edge flip (left).\n");
 
6614
            }
 
6615
            if (counterclockwise(insertpoint, rightpoint, farpoint) < 0.0) {
 
6616
              printf("Internal error in insertsite():\n");
 
6617
              printf("  Clockwise triangle after edge flip (right).\n");
 
6618
            }
 
6619
          }
 
6620
#endif /* SELF_CHECK */
 
6621
          if (verbose > 2) {
 
6622
            printf("  Edge flip results in left ");
 
6623
            lnextself(topleft);
 
6624
            printtriangle(&topleft);
 
6625
            printf("  and right ");
 
6626
            printtriangle(&horiz);
 
6627
          }
 
6628
          /* On the next iterations, consider the two edges that were  */
 
6629
          /*   exposed (this is, are now visible to the newly inserted */
 
6630
          /*   point) by the edge flip.                                */
 
6631
          lprevself(horiz);
 
6632
          leftpoint = farpoint;
 
6633
        }
 
6634
      }
 
6635
    }
 
6636
    if (!doflip) {
 
6637
      /* The handle `horiz' is accepted as locally Delaunay. */
 
6638
#ifndef CDT_ONLY
 
6639
      if (triflaws) {
 
6640
        /* Check the triangle `horiz' for quality. */
 
6641
        testtriangle(&horiz);
 
6642
      }
 
6643
#endif /* not CDT_ONLY */
 
6644
      /* Look for the next edge around the newly inserted point. */
 
6645
      lnextself(horiz);
 
6646
      sym(horiz, testtri);
 
6647
      /* Check for finishing a complete revolution about the new point, or */
 
6648
      /*   falling off the edge of the triangulation.  The latter will     */
 
6649
      /*   happen when a point is inserted at a boundary.                  */
 
6650
      if ((leftpoint == first) || (testtri.tri == dummytri)) {
 
6651
        /* We're done.  Return a triangle whose origin is the new point. */
 
6652
        lnext(horiz, *searchtri);
 
6653
        lnext(horiz, recenttri);
 
6654
        return success;
 
6655
      }
 
6656
      /* Finish finding the next edge around the newly inserted point. */
 
6657
      lnext(testtri, horiz);
 
6658
      rightpoint = leftpoint;
 
6659
      dest(horiz, leftpoint);
 
6660
    }
 
6661
  }
 
6662
}
 
6663
 
 
6664
/*****************************************************************************/
 
6665
/*                                                                           */
 
6666
/*  triangulatepolygon()   Find the Delaunay triangulation of a polygon that */
 
6667
/*                         has a certain "nice" shape.  This includes the    */
 
6668
/*                         polygons that result from deletion of a point or  */
 
6669
/*                         insertion of a segment.                           */
 
6670
/*                                                                           */
 
6671
/*  This is a conceptually difficult routine.  The starting assumption is    */
 
6672
/*  that we have a polygon with n sides.  n - 1 of these sides are currently */
 
6673
/*  represented as edges in the mesh.  One side, called the "base", need not */
 
6674
/*  be.                                                                      */
 
6675
/*                                                                           */
 
6676
/*  Inside the polygon is a structure I call a "fan", consisting of n - 1    */
 
6677
/*  triangles that share a common origin.  For each of these triangles, the  */
 
6678
/*  edge opposite the origin is one of the sides of the polygon.  The        */
 
6679
/*  primary edge of each triangle is the edge directed from the origin to    */
 
6680
/*  the destination; note that this is not the same edge that is a side of   */
 
6681
/*  the polygon.  `firstedge' is the primary edge of the first triangle.     */
 
6682
/*  From there, the triangles follow in counterclockwise order about the     */
 
6683
/*  polygon, until `lastedge', the primary edge of the last triangle.        */
 
6684
/*  `firstedge' and `lastedge' are probably connected to other triangles     */
 
6685
/*  beyond the extremes of the fan, but their identity is not important, as  */
 
6686
/*  long as the fan remains connected to them.                               */
 
6687
/*                                                                           */
 
6688
/*  Imagine the polygon oriented so that its base is at the bottom.  This    */
 
6689
/*  puts `firstedge' on the far right, and `lastedge' on the far left.       */
 
6690
/*  The right vertex of the base is the destination of `firstedge', and the  */
 
6691
/*  left vertex of the base is the apex of `lastedge'.                       */
 
6692
/*                                                                           */
 
6693
/*  The challenge now is to find the right sequence of edge flips to         */
 
6694
/*  transform the fan into a Delaunay triangulation of the polygon.  Each    */
 
6695
/*  edge flip effectively removes one triangle from the fan, committing it   */
 
6696
/*  to the polygon.  The resulting polygon has one fewer edge.  If `doflip'  */
 
6697
/*  is set, the final flip will be performed, resulting in a fan of one      */
 
6698
/*  (useless?) triangle.  If `doflip' is not set, the final flip is not      */
 
6699
/*  performed, resulting in a fan of two triangles, and an unfinished        */
 
6700
/*  triangular polygon that is not yet filled out with a single triangle.    */
 
6701
/*  On completion of the routine, `lastedge' is the last remaining triangle, */
 
6702
/*  or the leftmost of the last two.                                         */
 
6703
/*                                                                           */
 
6704
/*  Although the flips are performed in the order described above, the       */
 
6705
/*  decisions about what flips to perform are made in precisely the reverse  */
 
6706
/*  order.  The recursive triangulatepolygon() procedure makes a decision,   */
 
6707
/*  uses up to two recursive calls to triangulate the "subproblems"          */
 
6708
/*  (polygons with fewer edges), and then performs an edge flip.             */
 
6709
/*                                                                           */
 
6710
/*  The "decision" it makes is which vertex of the polygon should be         */
 
6711
/*  connected to the base.  This decision is made by testing every possible  */
 
6712
/*  vertex.  Once the best vertex is found, the two edges that connect this  */
 
6713
/*  vertex to the base become the bases for two smaller polygons.  These     */
 
6714
/*  are triangulated recursively.  Unfortunately, this approach can take     */
 
6715
/*  O(n^2) time not only in the worst case, but in many common cases.  It's  */
 
6716
/*  rarely a big deal for point deletion, where n is rarely larger than ten, */
 
6717
/*  but it could be a big deal for segment insertion, especially if there's  */
 
6718
/*  a lot of long segments that each cut many triangles.  I ought to code    */
 
6719
/*  a faster algorithm some time.                                            */
 
6720
/*                                                                           */
 
6721
/*  The `edgecount' parameter is the number of sides of the polygon,         */
 
6722
/*  including its base.  `triflaws' is a flag that determines whether the    */
 
6723
/*  new triangles should be tested for quality, and enqueued if they are     */
 
6724
/*  bad.                                                                     */
 
6725
/*                                                                           */
 
6726
/*****************************************************************************/
 
6727
 
 
6728
void triangulatepolygon(
 
6729
struct triedge *firstedge,
 
6730
struct triedge *lastedge,
 
6731
int edgecount,
 
6732
int doflip,
 
6733
int triflaws)
 
6734
{
 
6735
  struct triedge testtri;
 
6736
  struct triedge besttri;
 
6737
  struct triedge tempedge;
 
6738
  point leftbasepoint, rightbasepoint;
 
6739
  point testpoint;
 
6740
  point bestpoint;
 
6741
  int bestnumber;
 
6742
  int i;
 
6743
  triangle ptr;   /* Temporary variable used by sym(), onext(), and oprev(). */
 
6744
 
 
6745
  /* Identify the base vertices. */
 
6746
  apex(*lastedge, leftbasepoint);
 
6747
  dest(*firstedge, rightbasepoint);
 
6748
  if (verbose > 2) {
 
6749
    printf("  Triangulating interior polygon at edge\n");
 
6750
    printf("    (%.12g, %.12g) (%.12g, %.12g)\n", leftbasepoint[0],
 
6751
           leftbasepoint[1], rightbasepoint[0], rightbasepoint[1]);
 
6752
  }
 
6753
  /* Find the best vertex to connect the base to. */
 
6754
  onext(*firstedge, besttri);
 
6755
  dest(besttri, bestpoint);
 
6756
  triedgecopy(besttri, testtri);
 
6757
  bestnumber = 1;
 
6758
  for (i = 2; i <= edgecount - 2; i++) {
 
6759
    onextself(testtri);
 
6760
    dest(testtri, testpoint);
 
6761
    /* Is this a better vertex? */
 
6762
    if (incircle(leftbasepoint, rightbasepoint, bestpoint, testpoint) > 0.0) {
 
6763
      triedgecopy(testtri, besttri);
 
6764
      bestpoint = testpoint;
 
6765
      bestnumber = i;
 
6766
    }
 
6767
  }
 
6768
  if (verbose > 2) {
 
6769
    printf("    Connecting edge to (%.12g, %.12g)\n", bestpoint[0],
 
6770
           bestpoint[1]);
 
6771
  }
 
6772
  if (bestnumber > 1) {
 
6773
    /* Recursively triangulate the smaller polygon on the right. */
 
6774
    oprev(besttri, tempedge);
 
6775
    triangulatepolygon(firstedge, &tempedge, bestnumber + 1, 1, triflaws);
 
6776
  }
 
6777
  if (bestnumber < edgecount - 2) {
 
6778
    /* Recursively triangulate the smaller polygon on the left. */
 
6779
    sym(besttri, tempedge);
 
6780
    triangulatepolygon(&besttri, lastedge, edgecount - bestnumber, 1,
 
6781
                       triflaws);
 
6782
    /* Find `besttri' again; it may have been lost to edge flips. */
 
6783
    sym(tempedge, besttri);
 
6784
  }
 
6785
  if (doflip) {
 
6786
    /* Do one final edge flip. */
 
6787
    flip(&besttri);
 
6788
#ifndef CDT_ONLY
 
6789
    if (triflaws) {
 
6790
      /* Check the quality of the newly committed triangle. */
 
6791
      sym(besttri, testtri);
 
6792
      testtriangle(&testtri);
 
6793
    }
 
6794
#endif /* not CDT_ONLY */
 
6795
  }
 
6796
  /* Return the base triangle. */
 
6797
  triedgecopy(besttri, *lastedge);
 
6798
}
 
6799
 
 
6800
/*****************************************************************************/
 
6801
/*                                                                           */
 
6802
/*  deletesite()   Delete a vertex from a Delaunay triangulation, ensuring   */
 
6803
/*                 that the triangulation remains Delaunay.                  */
 
6804
/*                                                                           */
 
6805
/*  The origin of `deltri' is deleted.  The union of the triangles adjacent  */
 
6806
/*  to this point is a polygon, for which the Delaunay triangulation is      */
 
6807
/*  found.  Two triangles are removed from the mesh.                         */
 
6808
/*                                                                           */
 
6809
/*  Only interior points that do not lie on segments (shell edges) or        */
 
6810
/*  boundaries may be deleted.                                               */
 
6811
/*                                                                           */
 
6812
/*****************************************************************************/
 
6813
 
 
6814
#ifndef CDT_ONLY
 
6815
 
 
6816
void deletesite(
 
6817
struct triedge *deltri)
 
6818
{
 
6819
  struct triedge countingtri;
 
6820
  struct triedge firstedge, lastedge;
 
6821
  struct triedge deltriright;
 
6822
  struct triedge lefttri, righttri;
 
6823
  struct triedge leftcasing, rightcasing;
 
6824
  struct edge leftshelle, rightshelle;
 
6825
  point delpoint;
 
6826
  point neworg;
 
6827
  int edgecount;
 
6828
  triangle ptr;   /* Temporary variable used by sym(), onext(), and oprev(). */
 
6829
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
6830
 
 
6831
  org(*deltri, delpoint);
 
6832
  if (verbose > 1) {
 
6833
    printf("  Deleting (%.12g, %.12g).\n", delpoint[0], delpoint[1]);
 
6834
  }
 
6835
  pointdealloc(delpoint);
 
6836
 
 
6837
  /* Count the degree of the point being deleted. */
 
6838
  onext(*deltri, countingtri);
 
6839
  edgecount = 1;
 
6840
  while (!triedgeequal(*deltri, countingtri)) {
 
6841
#ifdef SELF_CHECK
 
6842
    if (countingtri.tri == dummytri) {
 
6843
      printf("Internal error in deletesite():\n");
 
6844
      printf("  Attempt to delete boundary point.\n");
 
6845
      internalerror();
 
6846
    }
 
6847
#endif /* SELF_CHECK */
 
6848
    edgecount++;
 
6849
    onextself(countingtri);
 
6850
  }
 
6851
 
 
6852
#ifdef SELF_CHECK
 
6853
  if (edgecount < 3) {
 
6854
    printf("Internal error in deletesite():\n  Point has degree %d.\n",
 
6855
           edgecount);
 
6856
    internalerror();
 
6857
  }
 
6858
#endif /* SELF_CHECK */
 
6859
  if (edgecount > 3) {
 
6860
    /* Triangulate the polygon defined by the union of all triangles */
 
6861
    /*   adjacent to the point being deleted.  Check the quality of  */
 
6862
    /*   the resulting triangles.                                    */
 
6863
    onext(*deltri, firstedge);
 
6864
    oprev(*deltri, lastedge);
 
6865
    triangulatepolygon(&firstedge, &lastedge, edgecount, 0, !nobisect);
 
6866
  }
 
6867
  /* Splice out two triangles. */
 
6868
  lprev(*deltri, deltriright);
 
6869
  dnext(*deltri, lefttri);
 
6870
  sym(lefttri, leftcasing);
 
6871
  oprev(deltriright, righttri);
 
6872
  sym(righttri, rightcasing);
 
6873
  bond(*deltri, leftcasing);
 
6874
  bond(deltriright, rightcasing);
 
6875
  tspivot(lefttri, leftshelle);
 
6876
  if (leftshelle.sh != dummysh) {
 
6877
    tsbond(*deltri, leftshelle);
 
6878
  }
 
6879
  tspivot(righttri, rightshelle);
 
6880
  if (rightshelle.sh != dummysh) {
 
6881
    tsbond(deltriright, rightshelle);
 
6882
  }
 
6883
 
 
6884
  /* Set the new origin of `deltri' and check its quality. */
 
6885
  org(lefttri, neworg);
 
6886
  setorg(*deltri, neworg);
 
6887
  if (!nobisect) {
 
6888
    testtriangle(deltri);
 
6889
  }
 
6890
 
 
6891
  /* Delete the two spliced-out triangles. */
 
6892
  triangledealloc(lefttri.tri);
 
6893
  triangledealloc(righttri.tri);
 
6894
}
 
6895
 
 
6896
#endif /* not CDT_ONLY */
 
6897
 
 
6898
/**                                                                         **/
 
6899
/**                                                                         **/
 
6900
/********* Mesh transformation routines end here                     *********/
 
6901
 
 
6902
/********* Divide-and-conquer Delaunay triangulation begins here     *********/
 
6903
/**                                                                         **/
 
6904
/**                                                                         **/
 
6905
 
 
6906
/*****************************************************************************/
 
6907
/*                                                                           */
 
6908
/*  The divide-and-conquer bounding box                                      */
 
6909
/*                                                                           */
 
6910
/*  I originally implemented the divide-and-conquer and incremental Delaunay */
 
6911
/*  triangulations using the edge-based data structure presented by Guibas   */
 
6912
/*  and Stolfi.  Switching to a triangle-based data structure doubled the    */
 
6913
/*  speed.  However, I had to think of a few extra tricks to maintain the    */
 
6914
/*  elegance of the original algorithms.                                     */
 
6915
/*                                                                           */
 
6916
/*  The "bounding box" used by my variant of the divide-and-conquer          */
 
6917
/*  algorithm uses one triangle for each edge of the convex hull of the      */
 
6918
/*  triangulation.  These bounding triangles all share a common apical       */
 
6919
/*  vertex, which is represented by NULL and which represents nothing.       */
 
6920
/*  The bounding triangles are linked in a circular fan about this NULL      */
 
6921
/*  vertex, and the edges on the convex hull of the triangulation appear     */
 
6922
/*  opposite the NULL vertex.  You might find it easiest to imagine that     */
 
6923
/*  the NULL vertex is a point in 3D space behind the center of the          */
 
6924
/*  triangulation, and that the bounding triangles form a sort of cone.      */
 
6925
/*                                                                           */
 
6926
/*  This bounding box makes it easy to represent degenerate cases.  For      */
 
6927
/*  instance, the triangulation of two vertices is a single edge.  This edge */
 
6928
/*  is represented by two bounding box triangles, one on each "side" of the  */
 
6929
/*  edge.  These triangles are also linked together in a fan about the NULL  */
 
6930
/*  vertex.                                                                  */
 
6931
/*                                                                           */
 
6932
/*  The bounding box also makes it easy to traverse the convex hull, as the  */
 
6933
/*  divide-and-conquer algorithm needs to do.                                */
 
6934
/*                                                                           */
 
6935
/*****************************************************************************/
 
6936
 
 
6937
/*****************************************************************************/
 
6938
/*                                                                           */
 
6939
/*  pointsort()   Sort an array of points by x-coordinate, using the         */
 
6940
/*                y-coordinate as a secondary key.                           */
 
6941
/*                                                                           */
 
6942
/*  Uses quicksort.  Randomized O(n log n) time.  No, I did not make any of  */
 
6943
/*  the usual quicksort mistakes.                                            */
 
6944
/*                                                                           */
 
6945
/*****************************************************************************/
 
6946
 
 
6947
void pointsort(
 
6948
point *sortarray,
 
6949
int arraysize)
 
6950
{
 
6951
  int left, right;
 
6952
  int pivot;
 
6953
  REAL pivotx, pivoty;
 
6954
  point temp;
 
6955
 
 
6956
  if (arraysize == 2) {
 
6957
    /* Recursive base case. */
 
6958
    if ((sortarray[0][0] > sortarray[1][0]) ||
 
6959
        ((sortarray[0][0] == sortarray[1][0]) &&
 
6960
         (sortarray[0][1] > sortarray[1][1]))) {
 
6961
      temp = sortarray[1];
 
6962
      sortarray[1] = sortarray[0];
 
6963
      sortarray[0] = temp;
 
6964
    }
 
6965
    return;
 
6966
  }
 
6967
  /* Choose a random pivot to split the array. */
 
6968
  pivot = (int) randomnation(arraysize);
 
6969
  pivotx = sortarray[pivot][0];
 
6970
  pivoty = sortarray[pivot][1];
 
6971
  /* Split the array. */
 
6972
  left = -1;
 
6973
  right = arraysize;
 
6974
  while (left < right) {
 
6975
    /* Search for a point whose x-coordinate is too large for the left. */
 
6976
    do {
 
6977
      left++;
 
6978
    } while ((left <= right) && ((sortarray[left][0] < pivotx) ||
 
6979
                                 ((sortarray[left][0] == pivotx) &&
 
6980
                                  (sortarray[left][1] < pivoty))));
 
6981
    /* Search for a point whose x-coordinate is too small for the right. */
 
6982
    do {
 
6983
      right--;
 
6984
    } while ((left <= right) && ((sortarray[right][0] > pivotx) ||
 
6985
                                 ((sortarray[right][0] == pivotx) &&
 
6986
                                  (sortarray[right][1] > pivoty))));
 
6987
    if (left < right) {
 
6988
      /* Swap the left and right points. */
 
6989
      temp = sortarray[left];
 
6990
      sortarray[left] = sortarray[right];
 
6991
      sortarray[right] = temp;
 
6992
    }
 
6993
  }
 
6994
  if (left > 1) {
 
6995
    /* Recursively sort the left subset. */
 
6996
    pointsort(sortarray, left);
 
6997
  }
 
6998
  if (right < arraysize - 2) {
 
6999
    /* Recursively sort the right subset. */
 
7000
    pointsort(&sortarray[right + 1], arraysize - right - 1);
 
7001
  }
 
7002
}
 
7003
 
 
7004
/*****************************************************************************/
 
7005
/*                                                                           */
 
7006
/*  pointmedian()   An order statistic algorithm, almost.  Shuffles an array */
 
7007
/*                  of points so that the first `median' points occur        */
 
7008
/*                  lexicographically before the remaining points.           */
 
7009
/*                                                                           */
 
7010
/*  Uses the x-coordinate as the primary key if axis == 0; the y-coordinate  */
 
7011
/*  if axis == 1.  Very similar to the pointsort() procedure, but runs in    */
 
7012
/*  randomized linear time.                                                  */
 
7013
/*                                                                           */
 
7014
/*****************************************************************************/
 
7015
 
 
7016
void pointmedian(
 
7017
point *sortarray,
 
7018
int arraysize,
 
7019
int median,
 
7020
int axis)
 
7021
{
 
7022
  int left, right;
 
7023
  int pivot;
 
7024
  REAL pivot1, pivot2;
 
7025
  point temp;
 
7026
 
 
7027
  if (arraysize == 2) {
 
7028
    /* Recursive base case. */
 
7029
    if ((sortarray[0][axis] > sortarray[1][axis]) ||
 
7030
        ((sortarray[0][axis] == sortarray[1][axis]) &&
 
7031
         (sortarray[0][1 - axis] > sortarray[1][1 - axis]))) {
 
7032
      temp = sortarray[1];
 
7033
      sortarray[1] = sortarray[0];
 
7034
      sortarray[0] = temp;
 
7035
    }
 
7036
    return;
 
7037
  }
 
7038
  /* Choose a random pivot to split the array. */
 
7039
  pivot = (int) randomnation(arraysize);
 
7040
  pivot1 = sortarray[pivot][axis];
 
7041
  pivot2 = sortarray[pivot][1 - axis];
 
7042
  /* Split the array. */
 
7043
  left = -1;
 
7044
  right = arraysize;
 
7045
  while (left < right) {
 
7046
    /* Search for a point whose x-coordinate is too large for the left. */
 
7047
    do {
 
7048
      left++;
 
7049
    } while ((left <= right) && ((sortarray[left][axis] < pivot1) ||
 
7050
                                 ((sortarray[left][axis] == pivot1) &&
 
7051
                                  (sortarray[left][1 - axis] < pivot2))));
 
7052
    /* Search for a point whose x-coordinate is too small for the right. */
 
7053
    do {
 
7054
      right--;
 
7055
    } while ((left <= right) && ((sortarray[right][axis] > pivot1) ||
 
7056
                                 ((sortarray[right][axis] == pivot1) &&
 
7057
                                  (sortarray[right][1 - axis] > pivot2))));
 
7058
    if (left < right) {
 
7059
      /* Swap the left and right points. */
 
7060
      temp = sortarray[left];
 
7061
      sortarray[left] = sortarray[right];
 
7062
      sortarray[right] = temp;
 
7063
    }
 
7064
  }
 
7065
  /* Unlike in pointsort(), at most one of the following */
 
7066
  /*   conditionals is true.                             */
 
7067
  if (left > median) {
 
7068
    /* Recursively shuffle the left subset. */
 
7069
    pointmedian(sortarray, left, median, axis);
 
7070
  }
 
7071
  if (right < median - 1) {
 
7072
    /* Recursively shuffle the right subset. */
 
7073
    pointmedian(&sortarray[right + 1], arraysize - right - 1,
 
7074
                median - right - 1, axis);
 
7075
  }
 
7076
}
 
7077
 
 
7078
/*****************************************************************************/
 
7079
/*                                                                           */
 
7080
/*  alternateaxes()   Sorts the points as appropriate for the divide-and-    */
 
7081
/*                    conquer algorithm with alternating cuts.               */
 
7082
/*                                                                           */
 
7083
/*  Partitions by x-coordinate if axis == 0; by y-coordinate if axis == 1.   */
 
7084
/*  For the base case, subsets containing only two or three points are       */
 
7085
/*  always sorted by x-coordinate.                                           */
 
7086
/*                                                                           */
 
7087
/*****************************************************************************/
 
7088
 
 
7089
void alternateaxes(
 
7090
point *sortarray,
 
7091
int arraysize,
 
7092
int axis)
 
7093
{
 
7094
  int divider;
 
7095
 
 
7096
  divider = arraysize >> 1;
 
7097
  if (arraysize <= 3) {
 
7098
    /* Recursive base case:  subsets of two or three points will be      */
 
7099
    /*   handled specially, and should always be sorted by x-coordinate. */
 
7100
    axis = 0;
 
7101
  }
 
7102
  /* Partition with a horizontal or vertical cut. */
 
7103
  pointmedian(sortarray, arraysize, divider, axis);
 
7104
  /* Recursively partition the subsets with a cross cut. */
 
7105
  if (arraysize - divider >= 2) {
 
7106
    if (divider >= 2) {
 
7107
      alternateaxes(sortarray, divider, 1 - axis);
 
7108
    }
 
7109
    alternateaxes(&sortarray[divider], arraysize - divider, 1 - axis);
 
7110
  }
 
7111
}
 
7112
 
 
7113
/*****************************************************************************/
 
7114
/*                                                                           */
 
7115
/*  mergehulls()   Merge two adjacent Delaunay triangulations into a         */
 
7116
/*                 single Delaunay triangulation.                            */
 
7117
/*                                                                           */
 
7118
/*  This is similar to the algorithm given by Guibas and Stolfi, but uses    */
 
7119
/*  a triangle-based, rather than edge-based, data structure.                */
 
7120
/*                                                                           */
 
7121
/*  The algorithm walks up the gap between the two triangulations, knitting  */
 
7122
/*  them together.  As they are merged, some of their bounding triangles     */
 
7123
/*  are converted into real triangles of the triangulation.  The procedure   */
 
7124
/*  pulls each hull's bounding triangles apart, then knits them together     */
 
7125
/*  like the teeth of two gears.  The Delaunay property determines, at each  */
 
7126
/*  step, whether the next "tooth" is a bounding triangle of the left hull   */
 
7127
/*  or the right.  When a bounding triangle becomes real, its apex is        */
 
7128
/*  changed from NULL to a real point.                                       */
 
7129
/*                                                                           */
 
7130
/*  Only two new triangles need to be allocated.  These become new bounding  */
 
7131
/*  triangles at the top and bottom of the seam.  They are used to connect   */
 
7132
/*  the remaining bounding triangles (those that have not been converted     */
 
7133
/*  into real triangles) into a single fan.                                  */
 
7134
/*                                                                           */
 
7135
/*  On entry, `farleft' and `innerleft' are bounding triangles of the left   */
 
7136
/*  triangulation.  The origin of `farleft' is the leftmost vertex, and      */
 
7137
/*  the destination of `innerleft' is the rightmost vertex of the            */
 
7138
/*  triangulation.  Similarly, `innerright' and `farright' are bounding      */
 
7139
/*  triangles of the right triangulation.  The origin of `innerright' and    */
 
7140
/*  destination of `farright' are the leftmost and rightmost vertices.       */
 
7141
/*                                                                           */
 
7142
/*  On completion, the origin of `farleft' is the leftmost vertex of the     */
 
7143
/*  merged triangulation, and the destination of `farright' is the rightmost */
 
7144
/*  vertex.                                                                  */
 
7145
/*                                                                           */
 
7146
/*****************************************************************************/
 
7147
 
 
7148
void mergehulls(
 
7149
struct triedge *farleft,
 
7150
struct triedge *innerleft,
 
7151
struct triedge *innerright,
 
7152
struct triedge *farright,
 
7153
int axis)
 
7154
{
 
7155
  struct triedge leftcand, rightcand;
 
7156
  struct triedge baseedge;
 
7157
  struct triedge nextedge;
 
7158
  struct triedge sidecasing, topcasing, outercasing;
 
7159
  struct triedge checkedge;
 
7160
  point innerleftdest;
 
7161
  point innerrightorg;
 
7162
  point innerleftapex, innerrightapex;
 
7163
  point farleftpt, farrightpt;
 
7164
  point farleftapex, farrightapex;
 
7165
  point lowerleft, lowerright;
 
7166
  point upperleft, upperright;
 
7167
  point nextapex;
 
7168
  point checkvertex;
 
7169
  int changemade;
 
7170
  int badedge;
 
7171
  int leftfinished, rightfinished;
 
7172
  triangle ptr;                         /* Temporary variable used by sym(). */
 
7173
 
 
7174
  dest(*innerleft, innerleftdest);
 
7175
  apex(*innerleft, innerleftapex);
 
7176
  org(*innerright, innerrightorg);
 
7177
  apex(*innerright, innerrightapex);
 
7178
  /* Special treatment for horizontal cuts. */
 
7179
  if (dwyer && (axis == 1)) {
 
7180
    org(*farleft, farleftpt);
 
7181
    apex(*farleft, farleftapex);
 
7182
    dest(*farright, farrightpt);
 
7183
    apex(*farright, farrightapex);
 
7184
    /* The pointers to the extremal points are shifted to point to the */
 
7185
    /*   topmost and bottommost point of each hull, rather than the    */
 
7186
    /*   leftmost and rightmost points.                                */
 
7187
    while (farleftapex[1] < farleftpt[1]) {
 
7188
      lnextself(*farleft);
 
7189
      symself(*farleft);
 
7190
      farleftpt = farleftapex;
 
7191
      apex(*farleft, farleftapex);
 
7192
    }
 
7193
    sym(*innerleft, checkedge);
 
7194
    apex(checkedge, checkvertex);
 
7195
    while (checkvertex[1] > innerleftdest[1]) {
 
7196
      lnext(checkedge, *innerleft);
 
7197
      innerleftapex = innerleftdest;
 
7198
      innerleftdest = checkvertex;
 
7199
      sym(*innerleft, checkedge);
 
7200
      apex(checkedge, checkvertex);
 
7201
    }
 
7202
    while (innerrightapex[1] < innerrightorg[1]) {
 
7203
      lnextself(*innerright);
 
7204
      symself(*innerright);
 
7205
      innerrightorg = innerrightapex;
 
7206
      apex(*innerright, innerrightapex);
 
7207
    }
 
7208
    sym(*farright, checkedge);
 
7209
    apex(checkedge, checkvertex);
 
7210
    while (checkvertex[1] > farrightpt[1]) {
 
7211
      lnext(checkedge, *farright);
 
7212
      farrightapex = farrightpt;
 
7213
      farrightpt = checkvertex;
 
7214
      sym(*farright, checkedge);
 
7215
      apex(checkedge, checkvertex);
 
7216
    }
 
7217
  }
 
7218
  /* Find a line tangent to and below both hulls. */
 
7219
  do {
 
7220
    changemade = 0;
 
7221
    /* Make innerleftdest the "bottommost" point of the left hull. */
 
7222
    if (counterclockwise(innerleftdest, innerleftapex, innerrightorg) > 0.0) {
 
7223
      lprevself(*innerleft);
 
7224
      symself(*innerleft);
 
7225
      innerleftdest = innerleftapex;
 
7226
      apex(*innerleft, innerleftapex);
 
7227
      changemade = 1;
 
7228
    }
 
7229
    /* Make innerrightorg the "bottommost" point of the right hull. */
 
7230
    if (counterclockwise(innerrightapex, innerrightorg, innerleftdest) > 0.0) {
 
7231
      lnextself(*innerright);
 
7232
      symself(*innerright);
 
7233
      innerrightorg = innerrightapex;
 
7234
      apex(*innerright, innerrightapex);
 
7235
      changemade = 1;
 
7236
    }
 
7237
  } while (changemade);
 
7238
  /* Find the two candidates to be the next "gear tooth". */
 
7239
  sym(*innerleft, leftcand);
 
7240
  sym(*innerright, rightcand);
 
7241
  /* Create the bottom new bounding triangle. */
 
7242
  maketriangle(&baseedge);
 
7243
  /* Connect it to the bounding boxes of the left and right triangulations. */
 
7244
  bond(baseedge, *innerleft);
 
7245
  lnextself(baseedge);
 
7246
  bond(baseedge, *innerright);
 
7247
  lnextself(baseedge);
 
7248
  setorg(baseedge, innerrightorg);
 
7249
  setdest(baseedge, innerleftdest);
 
7250
  /* Apex is intentionally left NULL. */
 
7251
  if (verbose > 2) {
 
7252
    printf("  Creating base bounding ");
 
7253
    printtriangle(&baseedge);
 
7254
  }
 
7255
  /* Fix the extreme triangles if necessary. */
 
7256
  org(*farleft, farleftpt);
 
7257
  if (innerleftdest == farleftpt) {
 
7258
    lnext(baseedge, *farleft);
 
7259
  }
 
7260
  dest(*farright, farrightpt);
 
7261
  if (innerrightorg == farrightpt) {
 
7262
    lprev(baseedge, *farright);
 
7263
  }
 
7264
  /* The vertices of the current knitting edge. */
 
7265
  lowerleft = innerleftdest;
 
7266
  lowerright = innerrightorg;
 
7267
  /* The candidate vertices for knitting. */
 
7268
  apex(leftcand, upperleft);
 
7269
  apex(rightcand, upperright);
 
7270
  /* Walk up the gap between the two triangulations, knitting them together. */
 
7271
  while (1) {
 
7272
    /* Have we reached the top?  (This isn't quite the right question,       */
 
7273
    /*   because even though the left triangulation might seem finished now, */
 
7274
    /*   moving up on the right triangulation might reveal a new point of    */
 
7275
    /*   the left triangulation.  And vice-versa.)                           */
 
7276
    leftfinished = counterclockwise(upperleft, lowerleft, lowerright) <= 0.0;
 
7277
    rightfinished = counterclockwise(upperright, lowerleft, lowerright) <= 0.0;
 
7278
    if (leftfinished && rightfinished) {
 
7279
      /* Create the top new bounding triangle. */
 
7280
      maketriangle(&nextedge);
 
7281
      setorg(nextedge, lowerleft);
 
7282
      setdest(nextedge, lowerright);
 
7283
      /* Apex is intentionally left NULL. */
 
7284
      /* Connect it to the bounding boxes of the two triangulations. */
 
7285
      bond(nextedge, baseedge);
 
7286
      lnextself(nextedge);
 
7287
      bond(nextedge, rightcand);
 
7288
      lnextself(nextedge);
 
7289
      bond(nextedge, leftcand);
 
7290
      if (verbose > 2) {
 
7291
        printf("  Creating top bounding ");
 
7292
        printtriangle(&baseedge);
 
7293
      }
 
7294
      /* Special treatment for horizontal cuts. */
 
7295
      if (dwyer && (axis == 1)) {
 
7296
        org(*farleft, farleftpt);
 
7297
        apex(*farleft, farleftapex);
 
7298
        dest(*farright, farrightpt);
 
7299
        apex(*farright, farrightapex);
 
7300
        sym(*farleft, checkedge);
 
7301
        apex(checkedge, checkvertex);
 
7302
        /* The pointers to the extremal points are restored to the leftmost */
 
7303
        /*   and rightmost points (rather than topmost and bottommost).     */
 
7304
        while (checkvertex[0] < farleftpt[0]) {
 
7305
          lprev(checkedge, *farleft);
 
7306
          farleftapex = farleftpt;
 
7307
          farleftpt = checkvertex;
 
7308
          sym(*farleft, checkedge);
 
7309
          apex(checkedge, checkvertex);
 
7310
        }
 
7311
        while (farrightapex[0] > farrightpt[0]) {
 
7312
          lprevself(*farright);
 
7313
          symself(*farright);
 
7314
          farrightpt = farrightapex;
 
7315
          apex(*farright, farrightapex);
 
7316
        }
 
7317
      }
 
7318
      return;
 
7319
    }
 
7320
    /* Consider eliminating edges from the left triangulation. */
 
7321
    if (!leftfinished) {
 
7322
      /* What vertex would be exposed if an edge were deleted? */
 
7323
      lprev(leftcand, nextedge);
 
7324
      symself(nextedge);
 
7325
      apex(nextedge, nextapex);
 
7326
      /* If nextapex is NULL, then no vertex would be exposed; the */
 
7327
      /*   triangulation would have been eaten right through.      */
 
7328
      if (nextapex != (point) NULL) {
 
7329
        /* Check whether the edge is Delaunay. */
 
7330
        badedge = incircle(lowerleft, lowerright, upperleft, nextapex) > 0.0;
 
7331
        while (badedge) {
 
7332
          /* Eliminate the edge with an edge flip.  As a result, the    */
 
7333
          /*   left triangulation will have one more boundary triangle. */
 
7334
          lnextself(nextedge);
 
7335
          sym(nextedge, topcasing);
 
7336
          lnextself(nextedge);
 
7337
          sym(nextedge, sidecasing);
 
7338
          bond(nextedge, topcasing);
 
7339
          bond(leftcand, sidecasing);
 
7340
          lnextself(leftcand);
 
7341
          sym(leftcand, outercasing);
 
7342
          lprevself(nextedge);
 
7343
          bond(nextedge, outercasing);
 
7344
          /* Correct the vertices to reflect the edge flip. */
 
7345
          setorg(leftcand, lowerleft);
 
7346
          setdest(leftcand, NULL);
 
7347
          setapex(leftcand, nextapex);
 
7348
          setorg(nextedge, NULL);
 
7349
          setdest(nextedge, upperleft);
 
7350
          setapex(nextedge, nextapex);
 
7351
          /* Consider the newly exposed vertex. */
 
7352
          upperleft = nextapex;
 
7353
          /* What vertex would be exposed if another edge were deleted? */
 
7354
          triedgecopy(sidecasing, nextedge);
 
7355
          apex(nextedge, nextapex);
 
7356
          if (nextapex != (point) NULL) {
 
7357
            /* Check whether the edge is Delaunay. */
 
7358
            badedge = incircle(lowerleft, lowerright, upperleft, nextapex)
 
7359
                      > 0.0;
 
7360
          } else {
 
7361
            /* Avoid eating right through the triangulation. */
 
7362
            badedge = 0;
 
7363
          }
 
7364
        }
 
7365
      }
 
7366
    }
 
7367
    /* Consider eliminating edges from the right triangulation. */
 
7368
    if (!rightfinished) {
 
7369
      /* What vertex would be exposed if an edge were deleted? */
 
7370
      lnext(rightcand, nextedge);
 
7371
      symself(nextedge);
 
7372
      apex(nextedge, nextapex);
 
7373
      /* If nextapex is NULL, then no vertex would be exposed; the */
 
7374
      /*   triangulation would have been eaten right through.      */
 
7375
      if (nextapex != (point) NULL) {
 
7376
        /* Check whether the edge is Delaunay. */
 
7377
        badedge = incircle(lowerleft, lowerright, upperright, nextapex) > 0.0;
 
7378
        while (badedge) {
 
7379
          /* Eliminate the edge with an edge flip.  As a result, the     */
 
7380
          /*   right triangulation will have one more boundary triangle. */
 
7381
          lprevself(nextedge);
 
7382
          sym(nextedge, topcasing);
 
7383
          lprevself(nextedge);
 
7384
          sym(nextedge, sidecasing);
 
7385
          bond(nextedge, topcasing);
 
7386
          bond(rightcand, sidecasing);
 
7387
          lprevself(rightcand);
 
7388
          sym(rightcand, outercasing);
 
7389
          lnextself(nextedge);
 
7390
          bond(nextedge, outercasing);
 
7391
          /* Correct the vertices to reflect the edge flip. */
 
7392
          setorg(rightcand, NULL);
 
7393
          setdest(rightcand, lowerright);
 
7394
          setapex(rightcand, nextapex);
 
7395
          setorg(nextedge, upperright);
 
7396
          setdest(nextedge, NULL);
 
7397
          setapex(nextedge, nextapex);
 
7398
          /* Consider the newly exposed vertex. */
 
7399
          upperright = nextapex;
 
7400
          /* What vertex would be exposed if another edge were deleted? */
 
7401
          triedgecopy(sidecasing, nextedge);
 
7402
          apex(nextedge, nextapex);
 
7403
          if (nextapex != (point) NULL) {
 
7404
            /* Check whether the edge is Delaunay. */
 
7405
            badedge = incircle(lowerleft, lowerright, upperright, nextapex)
 
7406
                      > 0.0;
 
7407
          } else {
 
7408
            /* Avoid eating right through the triangulation. */
 
7409
            badedge = 0;
 
7410
          }
 
7411
        }
 
7412
      }
 
7413
    }
 
7414
    if (leftfinished || (!rightfinished &&
 
7415
           (incircle(upperleft, lowerleft, lowerright, upperright) > 0.0))) {
 
7416
      /* Knit the triangulations, adding an edge from `lowerleft' */
 
7417
      /*   to `upperright'.                                       */
 
7418
      bond(baseedge, rightcand);
 
7419
      lprev(rightcand, baseedge);
 
7420
      setdest(baseedge, lowerleft);
 
7421
      lowerright = upperright;
 
7422
      sym(baseedge, rightcand);
 
7423
      apex(rightcand, upperright);
 
7424
    } else {
 
7425
      /* Knit the triangulations, adding an edge from `upperleft' */
 
7426
      /*   to `lowerright'.                                       */
 
7427
      bond(baseedge, leftcand);
 
7428
      lnext(leftcand, baseedge);
 
7429
      setorg(baseedge, lowerright);
 
7430
      lowerleft = upperleft;
 
7431
      sym(baseedge, leftcand);
 
7432
      apex(leftcand, upperleft);
 
7433
    }
 
7434
    if (verbose > 2) {
 
7435
      printf("  Connecting ");
 
7436
      printtriangle(&baseedge);
 
7437
    }
 
7438
  }
 
7439
}
 
7440
 
 
7441
/*****************************************************************************/
 
7442
/*                                                                           */
 
7443
/*  divconqrecurse()   Recursively form a Delaunay triangulation by the      */
 
7444
/*                     divide-and-conquer method.                            */
 
7445
/*                                                                           */
 
7446
/*  Recursively breaks down the problem into smaller pieces, which are       */
 
7447
/*  knitted together by mergehulls().  The base cases (problems of two or    */
 
7448
/*  three points) are handled specially here.                                */
 
7449
/*                                                                           */
 
7450
/*  On completion, `farleft' and `farright' are bounding triangles such that */
 
7451
/*  the origin of `farleft' is the leftmost vertex (breaking ties by         */
 
7452
/*  choosing the highest leftmost vertex), and the destination of            */
 
7453
/*  `farright' is the rightmost vertex (breaking ties by choosing the        */
 
7454
/*  lowest rightmost vertex).                                                */
 
7455
/*                                                                           */
 
7456
/*****************************************************************************/
 
7457
 
 
7458
void divconqrecurse(
 
7459
point *sortarray,
 
7460
int vertices,
 
7461
int axis,
 
7462
struct triedge *farleft,
 
7463
struct triedge *farright)
 
7464
{
 
7465
  struct triedge midtri, tri1, tri2, tri3;
 
7466
  struct triedge innerleft, innerright;
 
7467
  REAL area;
 
7468
  int divider;
 
7469
 
 
7470
  if (verbose > 2) {
 
7471
    printf("  Triangulating %d points.\n", vertices);
 
7472
  }
 
7473
  if (vertices == 2) {
 
7474
    /* The triangulation of two vertices is an edge.  An edge is */
 
7475
    /*   represented by two bounding triangles.                  */
 
7476
    maketriangle(farleft);
 
7477
    setorg(*farleft, sortarray[0]);
 
7478
    setdest(*farleft, sortarray[1]);
 
7479
    /* The apex is intentionally left NULL. */
 
7480
    maketriangle(farright);
 
7481
    setorg(*farright, sortarray[1]);
 
7482
    setdest(*farright, sortarray[0]);
 
7483
    /* The apex is intentionally left NULL. */
 
7484
    bond(*farleft, *farright);
 
7485
    lprevself(*farleft);
 
7486
    lnextself(*farright);
 
7487
    bond(*farleft, *farright);
 
7488
    lprevself(*farleft);
 
7489
    lnextself(*farright);
 
7490
    bond(*farleft, *farright);
 
7491
    if (verbose > 2) {
 
7492
      printf("  Creating ");
 
7493
      printtriangle(farleft);
 
7494
      printf("  Creating ");
 
7495
      printtriangle(farright);
 
7496
    }
 
7497
    /* Ensure that the origin of `farleft' is sortarray[0]. */
 
7498
    lprev(*farright, *farleft);
 
7499
    return;
 
7500
  } else if (vertices == 3) {
 
7501
    /* The triangulation of three vertices is either a triangle (with */
 
7502
    /*   three bounding triangles) or two edges (with four bounding   */
 
7503
    /*   triangles).  In either case, four triangles are created.     */
 
7504
    maketriangle(&midtri);
 
7505
    maketriangle(&tri1);
 
7506
    maketriangle(&tri2);
 
7507
    maketriangle(&tri3);
 
7508
    area = counterclockwise(sortarray[0], sortarray[1], sortarray[2]);
 
7509
    if (area == 0.0) {
 
7510
      /* Three collinear points; the triangulation is two edges. */
 
7511
      setorg(midtri, sortarray[0]);
 
7512
      setdest(midtri, sortarray[1]);
 
7513
      setorg(tri1, sortarray[1]);
 
7514
      setdest(tri1, sortarray[0]);
 
7515
      setorg(tri2, sortarray[2]);
 
7516
      setdest(tri2, sortarray[1]);
 
7517
      setorg(tri3, sortarray[1]);
 
7518
      setdest(tri3, sortarray[2]);
 
7519
      /* All apices are intentionally left NULL. */
 
7520
      bond(midtri, tri1);
 
7521
      bond(tri2, tri3);
 
7522
      lnextself(midtri);
 
7523
      lprevself(tri1);
 
7524
      lnextself(tri2);
 
7525
      lprevself(tri3);
 
7526
      bond(midtri, tri3);
 
7527
      bond(tri1, tri2);
 
7528
      lnextself(midtri);
 
7529
      lprevself(tri1);
 
7530
      lnextself(tri2);
 
7531
      lprevself(tri3);
 
7532
      bond(midtri, tri1);
 
7533
      bond(tri2, tri3);
 
7534
      /* Ensure that the origin of `farleft' is sortarray[0]. */
 
7535
      triedgecopy(tri1, *farleft);
 
7536
      /* Ensure that the destination of `farright' is sortarray[2]. */
 
7537
      triedgecopy(tri2, *farright);
 
7538
    } else {
 
7539
      /* The three points are not collinear; the triangulation is one */
 
7540
      /*   triangle, namely `midtri'.                                 */
 
7541
      setorg(midtri, sortarray[0]);
 
7542
      setdest(tri1, sortarray[0]);
 
7543
      setorg(tri3, sortarray[0]);
 
7544
      /* Apices of tri1, tri2, and tri3 are left NULL. */
 
7545
      if (area > 0.0) {
 
7546
        /* The vertices are in counterclockwise order. */
 
7547
        setdest(midtri, sortarray[1]);
 
7548
        setorg(tri1, sortarray[1]);
 
7549
        setdest(tri2, sortarray[1]);
 
7550
        setapex(midtri, sortarray[2]);
 
7551
        setorg(tri2, sortarray[2]);
 
7552
        setdest(tri3, sortarray[2]);
 
7553
      } else {
 
7554
        /* The vertices are in clockwise order. */
 
7555
        setdest(midtri, sortarray[2]);
 
7556
        setorg(tri1, sortarray[2]);
 
7557
        setdest(tri2, sortarray[2]);
 
7558
        setapex(midtri, sortarray[1]);
 
7559
        setorg(tri2, sortarray[1]);
 
7560
        setdest(tri3, sortarray[1]);
 
7561
      }
 
7562
      /* The topology does not depend on how the vertices are ordered. */
 
7563
      bond(midtri, tri1);
 
7564
      lnextself(midtri);
 
7565
      bond(midtri, tri2);
 
7566
      lnextself(midtri);
 
7567
      bond(midtri, tri3);
 
7568
      lprevself(tri1);
 
7569
      lnextself(tri2);
 
7570
      bond(tri1, tri2);
 
7571
      lprevself(tri1);
 
7572
      lprevself(tri3);
 
7573
      bond(tri1, tri3);
 
7574
      lnextself(tri2);
 
7575
      lprevself(tri3);
 
7576
      bond(tri2, tri3);
 
7577
      /* Ensure that the origin of `farleft' is sortarray[0]. */
 
7578
      triedgecopy(tri1, *farleft);
 
7579
      /* Ensure that the destination of `farright' is sortarray[2]. */
 
7580
      if (area > 0.0) {
 
7581
        triedgecopy(tri2, *farright);
 
7582
      } else {
 
7583
        lnext(*farleft, *farright);
 
7584
      }
 
7585
    }
 
7586
    if (verbose > 2) {
 
7587
      printf("  Creating ");
 
7588
      printtriangle(&midtri);
 
7589
      printf("  Creating ");
 
7590
      printtriangle(&tri1);
 
7591
      printf("  Creating ");
 
7592
      printtriangle(&tri2);
 
7593
      printf("  Creating ");
 
7594
      printtriangle(&tri3);
 
7595
    }
 
7596
    return;
 
7597
  } else {
 
7598
    /* Split the vertices in half. */
 
7599
    divider = vertices >> 1;
 
7600
    /* Recursively triangulate each half. */
 
7601
    divconqrecurse(sortarray, divider, 1 - axis, farleft, &innerleft);
 
7602
    divconqrecurse(&sortarray[divider], vertices - divider, 1 - axis,
 
7603
                   &innerright, farright);
 
7604
    if (verbose > 1) {
 
7605
      printf("  Joining triangulations with %d and %d vertices.\n", divider,
 
7606
             vertices - divider);
 
7607
    }
 
7608
    /* Merge the two triangulations into one. */
 
7609
    mergehulls(farleft, &innerleft, &innerright, farright, axis);
 
7610
  }
 
7611
}
 
7612
 
 
7613
long removeghosts(
 
7614
struct triedge *startghost)
 
7615
{
 
7616
  struct triedge searchedge;
 
7617
  struct triedge dissolveedge;
 
7618
  struct triedge deadtri;
 
7619
  point markorg;
 
7620
  long hullsize;
 
7621
  triangle ptr;                         /* Temporary variable used by sym(). */
 
7622
 
 
7623
  if (verbose) {
 
7624
    printf("  Removing ghost triangles.\n");
 
7625
  }
 
7626
  /* Find an edge on the convex hull to start point location from. */
 
7627
  lprev(*startghost, searchedge);
 
7628
  symself(searchedge);
 
7629
  dummytri[0] = encode(searchedge);
 
7630
  /* Remove the bounding box and count the convex hull edges. */
 
7631
  triedgecopy(*startghost, dissolveedge);
 
7632
  hullsize = 0;
 
7633
  do {
 
7634
    hullsize++;
 
7635
    lnext(dissolveedge, deadtri);
 
7636
    lprevself(dissolveedge);
 
7637
    symself(dissolveedge);
 
7638
    /* If no PSLG is involved, set the boundary markers of all the points */
 
7639
    /*   on the convex hull.  If a PSLG is used, this step is done later. */
 
7640
    if (!poly) {
 
7641
      /* Watch out for the case where all the input points are collinear. */
 
7642
      if (dissolveedge.tri != dummytri) {
 
7643
        org(dissolveedge, markorg);
 
7644
        if (pointmark(markorg) == 0) {
 
7645
          setpointmark(markorg, 1);
 
7646
        }
 
7647
      }
 
7648
    }
 
7649
    /* Remove a bounding triangle from a convex hull triangle. */
 
7650
    dissolve(dissolveedge);
 
7651
    /* Find the next bounding triangle. */
 
7652
    sym(deadtri, dissolveedge);
 
7653
    /* Delete the bounding triangle. */
 
7654
    triangledealloc(deadtri.tri);
 
7655
  } while (!triedgeequal(dissolveedge, *startghost));
 
7656
  return hullsize;
 
7657
}
 
7658
 
 
7659
/*****************************************************************************/
 
7660
/*                                                                           */
 
7661
/*  divconqdelaunay()   Form a Delaunay triangulation by the divide-and-     */
 
7662
/*                      conquer method.                                      */
 
7663
/*                                                                           */
 
7664
/*  Sorts the points, calls a recursive procedure to triangulate them, and   */
 
7665
/*  removes the bounding box, setting boundary markers as appropriate.       */
 
7666
/*                                                                           */
 
7667
/*****************************************************************************/
 
7668
 
 
7669
long divconqdelaunay()
 
7670
{
 
7671
  point *sortarray;
 
7672
  struct triedge hullleft, hullright;
 
7673
  int divider;
 
7674
  int i, j;
 
7675
 
 
7676
  /* Allocate an array of pointers to points for sorting. */
 
7677
  sortarray = (point *) malloc(inpoints * sizeof(point));
 
7678
  if (sortarray == (point *) NULL) {
 
7679
    printf("Error:  Out of memory.\n");
 
7680
    exit(1);
 
7681
  }
 
7682
  traversalinit(&points);
 
7683
  for (i = 0; i < inpoints; i++) {
 
7684
    sortarray[i] = pointtraverse();
 
7685
  }
 
7686
  if (verbose) {
 
7687
    printf("  Sorting points.\n");
 
7688
  }
 
7689
  /* Sort the points. */
 
7690
  pointsort(sortarray, inpoints);
 
7691
  /* Discard duplicate points, which can really mess up the algorithm. */
 
7692
  i = 0;
 
7693
  for (j = 1; j < inpoints; j++) {
 
7694
    if ((sortarray[i][0] == sortarray[j][0])
 
7695
        && (sortarray[i][1] == sortarray[j][1])) {
 
7696
      if (!quiet) {
 
7697
        printf(
 
7698
"Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
 
7699
               sortarray[j][0], sortarray[j][1]);
 
7700
      }
 
7701
/*  Commented out - would eliminate point from output .node file, but causes
 
7702
    a failure if some segment has this point as an endpoint.
 
7703
      setpointmark(sortarray[j], DEADPOINT);
 
7704
*/
 
7705
    } else {
 
7706
      i++;
 
7707
      sortarray[i] = sortarray[j];
 
7708
    }
 
7709
  }
 
7710
  i++;
 
7711
  if (dwyer) {
 
7712
    /* Re-sort the array of points to accommodate alternating cuts. */
 
7713
    divider = i >> 1;
 
7714
    if (i - divider >= 2) {
 
7715
      if (divider >= 2) {
 
7716
        alternateaxes(sortarray, divider, 1);
 
7717
      }
 
7718
      alternateaxes(&sortarray[divider], i - divider, 1);
 
7719
    }
 
7720
  }
 
7721
  if (verbose) {
 
7722
    printf("  Forming triangulation.\n");
 
7723
  }
 
7724
  /* Form the Delaunay triangulation. */
 
7725
  divconqrecurse(sortarray, i, 0, &hullleft, &hullright);
 
7726
  free(sortarray);
 
7727
 
 
7728
  return removeghosts(&hullleft);
 
7729
}
 
7730
 
 
7731
/**                                                                         **/
 
7732
/**                                                                         **/
 
7733
/********* Divide-and-conquer Delaunay triangulation ends here       *********/
 
7734
 
 
7735
/********* Incremental Delaunay triangulation begins here            *********/
 
7736
/**                                                                         **/
 
7737
/**                                                                         **/
 
7738
 
 
7739
/*****************************************************************************/
 
7740
/*                                                                           */
 
7741
/*  boundingbox()   Form an "infinite" bounding triangle to insert points    */
 
7742
/*                  into.                                                    */
 
7743
/*                                                                           */
 
7744
/*  The points at "infinity" are assigned finite coordinates, which are used */
 
7745
/*  by the point location routines, but (mostly) ignored by the Delaunay     */
 
7746
/*  edge flip routines.                                                      */
 
7747
/*                                                                           */
 
7748
/*****************************************************************************/
 
7749
 
 
7750
#ifndef REDUCED
 
7751
 
 
7752
void boundingbox()
 
7753
{
 
7754
  struct triedge inftri;          /* Handle for the triangular bounding box. */
 
7755
  REAL width;
 
7756
 
 
7757
  if (verbose) {
 
7758
    printf("  Creating triangular bounding box.\n");
 
7759
  }
 
7760
  /* Find the width (or height, whichever is larger) of the triangulation. */
 
7761
  width = xmax - xmin;
 
7762
  if (ymax - ymin > width) {
 
7763
    width = ymax - ymin;
 
7764
  }
 
7765
  if (width == 0.0) {
 
7766
    width = 1.0;
 
7767
  }
 
7768
  /* Create the vertices of the bounding box. */
 
7769
  infpoint1 = (point) malloc(points.itembytes);
 
7770
  infpoint2 = (point) malloc(points.itembytes);
 
7771
  infpoint3 = (point) malloc(points.itembytes);
 
7772
  if ((infpoint1 == (point) NULL) || (infpoint2 == (point) NULL)
 
7773
      || (infpoint3 == (point) NULL)) {
 
7774
    printf("Error:  Out of memory.\n");
 
7775
    exit(1);
 
7776
  }
 
7777
  infpoint1[0] = xmin - 50.0 * width;
 
7778
  infpoint1[1] = ymin - 40.0 * width;
 
7779
  infpoint2[0] = xmax + 50.0 * width;
 
7780
  infpoint2[1] = ymin - 40.0 * width;
 
7781
  infpoint3[0] = 0.5 * (xmin + xmax);
 
7782
  infpoint3[1] = ymax + 60.0 * width;
 
7783
 
 
7784
  /* Create the bounding box. */
 
7785
  maketriangle(&inftri);
 
7786
  setorg(inftri, infpoint1);
 
7787
  setdest(inftri, infpoint2);
 
7788
  setapex(inftri, infpoint3);
 
7789
  /* Link dummytri to the bounding box so we can always find an */
 
7790
  /*   edge to begin searching (point location) from.           */
 
7791
  dummytri[0] = (triangle) inftri.tri;
 
7792
  if (verbose > 2) {
 
7793
    printf("  Creating ");
 
7794
    printtriangle(&inftri);
 
7795
  }
 
7796
}
 
7797
 
 
7798
#endif /* not REDUCED */
 
7799
 
 
7800
/*****************************************************************************/
 
7801
/*                                                                           */
 
7802
/*  removebox()   Remove the "infinite" bounding triangle, setting boundary  */
 
7803
/*                markers as appropriate.                                    */
 
7804
/*                                                                           */
 
7805
/*  The triangular bounding box has three boundary triangles (one for each   */
 
7806
/*  side of the bounding box), and a bunch of triangles fanning out from     */
 
7807
/*  the three bounding box vertices (one triangle for each edge of the       */
 
7808
/*  convex hull of the inner mesh).  This routine removes these triangles.   */
 
7809
/*                                                                           */
 
7810
/*****************************************************************************/
 
7811
 
 
7812
#ifndef REDUCED
 
7813
 
 
7814
long removebox()
 
7815
{
 
7816
  struct triedge deadtri;
 
7817
  struct triedge searchedge;
 
7818
  struct triedge checkedge;
 
7819
  struct triedge nextedge, finaledge, dissolveedge;
 
7820
  point markorg;
 
7821
  long hullsize;
 
7822
  triangle ptr;                         /* Temporary variable used by sym(). */
 
7823
 
 
7824
  if (verbose) {
 
7825
    printf("  Removing triangular bounding box.\n");
 
7826
  }
 
7827
  /* Find a boundary triangle. */
 
7828
  nextedge.tri = dummytri;
 
7829
  nextedge.orient = 0;
 
7830
  symself(nextedge);
 
7831
  /* Mark a place to stop. */
 
7832
  lprev(nextedge, finaledge);
 
7833
  lnextself(nextedge);
 
7834
  symself(nextedge);
 
7835
  /* Find a triangle (on the boundary of the point set) that isn't */
 
7836
  /*   a bounding box triangle.                                    */
 
7837
  lprev(nextedge, searchedge);
 
7838
  symself(searchedge);
 
7839
  /* Check whether nextedge is another boundary triangle */
 
7840
  /*   adjacent to the first one.                        */
 
7841
  lnext(nextedge, checkedge);
 
7842
  symself(checkedge);
 
7843
  if (checkedge.tri == dummytri) {
 
7844
    /* Go on to the next triangle.  There are only three boundary   */
 
7845
    /*   triangles, and this next triangle cannot be the third one, */
 
7846
    /*   so it's safe to stop here.                                 */
 
7847
    lprevself(searchedge);
 
7848
    symself(searchedge);
 
7849
  }
 
7850
  /* Find a new boundary edge to search from, as the current search */
 
7851
  /*   edge lies on a bounding box triangle and will be deleted.    */
 
7852
  dummytri[0] = encode(searchedge);
 
7853
  hullsize = -2l;
 
7854
  while (!triedgeequal(nextedge, finaledge)) {
 
7855
    hullsize++;
 
7856
    lprev(nextedge, dissolveedge);
 
7857
    symself(dissolveedge);
 
7858
    /* If not using a PSLG, the vertices should be marked now. */
 
7859
    /*   (If using a PSLG, markhull() will do the job.)        */
 
7860
    if (!poly) {
 
7861
      /* Be careful!  One must check for the case where all the input   */
 
7862
      /*   points are collinear, and thus all the triangles are part of */
 
7863
      /*   the bounding box.  Otherwise, the setpointmark() call below  */
 
7864
      /*   will cause a bad pointer reference.                          */
 
7865
      if (dissolveedge.tri != dummytri) {
 
7866
        org(dissolveedge, markorg);
 
7867
        if (pointmark(markorg) == 0) {
 
7868
          setpointmark(markorg, 1);
 
7869
        }
 
7870
      }
 
7871
    }
 
7872
    /* Disconnect the bounding box triangle from the mesh triangle. */
 
7873
    dissolve(dissolveedge);
 
7874
    lnext(nextedge, deadtri);
 
7875
    sym(deadtri, nextedge);
 
7876
    /* Get rid of the bounding box triangle. */
 
7877
    triangledealloc(deadtri.tri);
 
7878
    /* Do we need to turn the corner? */
 
7879
    if (nextedge.tri == dummytri) {
 
7880
      /* Turn the corner. */
 
7881
      triedgecopy(dissolveedge, nextedge);
 
7882
    }
 
7883
  }
 
7884
  triangledealloc(finaledge.tri);
 
7885
 
 
7886
  free(infpoint1);                  /* Deallocate the bounding box vertices. */
 
7887
  free(infpoint2);
 
7888
  free(infpoint3);
 
7889
 
 
7890
  return hullsize;
 
7891
}
 
7892
 
 
7893
#endif /* not REDUCED */
 
7894
 
 
7895
/*****************************************************************************/
 
7896
/*                                                                           */
 
7897
/*  incrementaldelaunay()   Form a Delaunay triangulation by incrementally   */
 
7898
/*                          adding vertices.                                 */
 
7899
/*                                                                           */
 
7900
/*****************************************************************************/
 
7901
 
 
7902
#ifndef REDUCED
 
7903
 
 
7904
long incrementaldelaunay()
 
7905
{
 
7906
  struct triedge starttri;
 
7907
  point pointloop;
 
7908
  int i;
 
7909
 
 
7910
  /* Create a triangular bounding box. */
 
7911
  boundingbox();
 
7912
  if (verbose) {
 
7913
    printf("  Incrementally inserting points.\n");
 
7914
  }
 
7915
  traversalinit(&points);
 
7916
  pointloop = pointtraverse();
 
7917
  i = 1;
 
7918
  while (pointloop != (point) NULL) {
 
7919
    /* Find a boundary triangle to search from. */
 
7920
    starttri.tri = (triangle *) NULL;
 
7921
    if (insertsite(pointloop, &starttri, (struct edge *) NULL, 0, 0) ==
 
7922
        DUPLICATEPOINT) {
 
7923
      if (!quiet) {
 
7924
        printf(
 
7925
"Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
 
7926
               pointloop[0], pointloop[1]);
 
7927
      }
 
7928
/*  Commented out - would eliminate point from output .node file.
 
7929
      setpointmark(pointloop, DEADPOINT);
 
7930
*/
 
7931
    }
 
7932
    pointloop = pointtraverse();
 
7933
    i++;
 
7934
  }
 
7935
  /* Remove the bounding box. */
 
7936
  return removebox();
 
7937
}
 
7938
 
 
7939
#endif /* not REDUCED */
 
7940
 
 
7941
/**                                                                         **/
 
7942
/**                                                                         **/
 
7943
/********* Incremental Delaunay triangulation ends here              *********/
 
7944
 
 
7945
/********* Sweepline Delaunay triangulation begins here              *********/
 
7946
/**                                                                         **/
 
7947
/**                                                                         **/
 
7948
 
 
7949
#ifndef REDUCED
 
7950
 
 
7951
void eventheapinsert(
 
7952
struct event **heap,
 
7953
int heapsize,
 
7954
struct event *newevent)
 
7955
{
 
7956
  REAL eventx, eventy;
 
7957
  int eventnum;
 
7958
  int parent;
 
7959
  int notdone;
 
7960
 
 
7961
  eventx = newevent->xkey;
 
7962
  eventy = newevent->ykey;
 
7963
  eventnum = heapsize;
 
7964
  notdone = eventnum > 0;
 
7965
  while (notdone) {
 
7966
    parent = (eventnum - 1) >> 1;
 
7967
    if ((heap[parent]->ykey < eventy) ||
 
7968
        ((heap[parent]->ykey == eventy)
 
7969
         && (heap[parent]->xkey <= eventx))) {
 
7970
      notdone = 0;
 
7971
    } else {
 
7972
      heap[eventnum] = heap[parent];
 
7973
      heap[eventnum]->heapposition = eventnum;
 
7974
 
 
7975
      eventnum = parent;
 
7976
      notdone = eventnum > 0;
 
7977
    }
 
7978
  }
 
7979
  heap[eventnum] = newevent;
 
7980
  newevent->heapposition = eventnum;
 
7981
}
 
7982
 
 
7983
#endif /* not REDUCED */
 
7984
 
 
7985
#ifndef REDUCED
 
7986
 
 
7987
void eventheapify(
 
7988
struct event **heap,
 
7989
int heapsize,
 
7990
int eventnum)
 
7991
{
 
7992
  struct event *thisevent;
 
7993
  REAL eventx, eventy;
 
7994
  int leftchild, rightchild;
 
7995
  int smallest;
 
7996
  int notdone;
 
7997
 
 
7998
  thisevent = heap[eventnum];
 
7999
  eventx = thisevent->xkey;
 
8000
  eventy = thisevent->ykey;
 
8001
  leftchild = 2 * eventnum + 1;
 
8002
  notdone = leftchild < heapsize;
 
8003
  while (notdone) {
 
8004
    if ((heap[leftchild]->ykey < eventy) ||
 
8005
        ((heap[leftchild]->ykey == eventy)
 
8006
         && (heap[leftchild]->xkey < eventx))) {
 
8007
      smallest = leftchild;
 
8008
    } else {
 
8009
      smallest = eventnum;
 
8010
    }
 
8011
    rightchild = leftchild + 1;
 
8012
    if (rightchild < heapsize) {
 
8013
      if ((heap[rightchild]->ykey < heap[smallest]->ykey) ||
 
8014
          ((heap[rightchild]->ykey == heap[smallest]->ykey)
 
8015
           && (heap[rightchild]->xkey < heap[smallest]->xkey))) {
 
8016
        smallest = rightchild;
 
8017
      }
 
8018
    }
 
8019
    if (smallest == eventnum) {
 
8020
      notdone = 0;
 
8021
    } else {
 
8022
      heap[eventnum] = heap[smallest];
 
8023
      heap[eventnum]->heapposition = eventnum;
 
8024
      heap[smallest] = thisevent;
 
8025
      thisevent->heapposition = smallest;
 
8026
 
 
8027
      eventnum = smallest;
 
8028
      leftchild = 2 * eventnum + 1;
 
8029
      notdone = leftchild < heapsize;
 
8030
    }
 
8031
  }
 
8032
}
 
8033
 
 
8034
#endif /* not REDUCED */
 
8035
 
 
8036
#ifndef REDUCED
 
8037
 
 
8038
void eventheapdelete(
 
8039
struct event **heap,
 
8040
int heapsize,
 
8041
int eventnum)
 
8042
{
 
8043
  struct event *moveevent;
 
8044
  REAL eventx, eventy;
 
8045
  int parent;
 
8046
  int notdone;
 
8047
 
 
8048
  moveevent = heap[heapsize - 1];
 
8049
  if (eventnum > 0) {
 
8050
    eventx = moveevent->xkey;
 
8051
    eventy = moveevent->ykey;
 
8052
    do {
 
8053
      parent = (eventnum - 1) >> 1;
 
8054
      if ((heap[parent]->ykey < eventy) ||
 
8055
          ((heap[parent]->ykey == eventy)
 
8056
           && (heap[parent]->xkey <= eventx))) {
 
8057
        notdone = 0;
 
8058
      } else {
 
8059
        heap[eventnum] = heap[parent];
 
8060
        heap[eventnum]->heapposition = eventnum;
 
8061
 
 
8062
        eventnum = parent;
 
8063
        notdone = eventnum > 0;
 
8064
      }
 
8065
    } while (notdone);
 
8066
  }
 
8067
  heap[eventnum] = moveevent;
 
8068
  moveevent->heapposition = eventnum;
 
8069
  eventheapify(heap, heapsize - 1, eventnum);
 
8070
}
 
8071
 
 
8072
#endif /* not REDUCED */
 
8073
 
 
8074
#ifndef REDUCED
 
8075
 
 
8076
void createeventheap(
 
8077
struct event ***eventheap,
 
8078
struct event **events,
 
8079
struct event **freeevents)
 
8080
{
 
8081
  point thispoint;
 
8082
  int maxevents;
 
8083
  int i;
 
8084
 
 
8085
  maxevents = (3 * inpoints) / 2;
 
8086
  *eventheap = (struct event **) malloc(maxevents * sizeof(struct event *));
 
8087
  if (*eventheap == (struct event **) NULL) {
 
8088
    printf("Error:  Out of memory.\n");
 
8089
    exit(1);
 
8090
  }
 
8091
  *events = (struct event *) malloc(maxevents * sizeof(struct event));
 
8092
  if (*events == (struct event *) NULL) {
 
8093
    printf("Error:  Out of memory.\n");
 
8094
    exit(1);
 
8095
  }
 
8096
  traversalinit(&points);
 
8097
  for (i = 0; i < inpoints; i++) {
 
8098
    thispoint = pointtraverse();
 
8099
    (*events)[i].eventptr = (VOID *) thispoint;
 
8100
    (*events)[i].xkey = thispoint[0];
 
8101
    (*events)[i].ykey = thispoint[1];
 
8102
    eventheapinsert(*eventheap, i, *events + i);
 
8103
  }
 
8104
  *freeevents = (struct event *) NULL;
 
8105
  for (i = maxevents - 1; i >= inpoints; i--) {
 
8106
    (*events)[i].eventptr = (VOID *) *freeevents;
 
8107
    *freeevents = *events + i;
 
8108
  }
 
8109
}
 
8110
 
 
8111
#endif /* not REDUCED */
 
8112
 
 
8113
#ifndef REDUCED
 
8114
 
 
8115
int rightofhyperbola(
 
8116
struct triedge *fronttri,
 
8117
point newsite)
 
8118
{
 
8119
  point leftpoint, rightpoint;
 
8120
  REAL dxa, dya, dxb, dyb;
 
8121
 
 
8122
  hyperbolacount++;
 
8123
 
 
8124
  dest(*fronttri, leftpoint);
 
8125
  apex(*fronttri, rightpoint);
 
8126
  if ((leftpoint[1] < rightpoint[1])
 
8127
      || ((leftpoint[1] == rightpoint[1]) && (leftpoint[0] < rightpoint[0]))) {
 
8128
    if (newsite[0] >= rightpoint[0]) {
 
8129
      return 1;
 
8130
    }
 
8131
  } else {
 
8132
    if (newsite[0] <= leftpoint[0]) {
 
8133
      return 0;
 
8134
    }
 
8135
  }
 
8136
  dxa = leftpoint[0] - newsite[0];
 
8137
  dya = leftpoint[1] - newsite[1];
 
8138
  dxb = rightpoint[0] - newsite[0];
 
8139
  dyb = rightpoint[1] - newsite[1];
 
8140
  return dya * (dxb * dxb + dyb * dyb) > dyb * (dxa * dxa + dya * dya);
 
8141
}
 
8142
 
 
8143
#endif /* not REDUCED */
 
8144
 
 
8145
#ifndef REDUCED
 
8146
 
 
8147
REAL circletop(
 
8148
point pa,
 
8149
point pb,
 
8150
point pc,
 
8151
REAL ccwabc)
 
8152
{
 
8153
  REAL xac, yac, xbc, ybc, xab, yab;
 
8154
  REAL aclen2, bclen2, ablen2;
 
8155
 
 
8156
  circletopcount++;
 
8157
 
 
8158
  xac = pa[0] - pc[0];
 
8159
  yac = pa[1] - pc[1];
 
8160
  xbc = pb[0] - pc[0];
 
8161
  ybc = pb[1] - pc[1];
 
8162
  xab = pa[0] - pb[0];
 
8163
  yab = pa[1] - pb[1];
 
8164
  aclen2 = xac * xac + yac * yac;
 
8165
  bclen2 = xbc * xbc + ybc * ybc;
 
8166
  ablen2 = xab * xab + yab * yab;
 
8167
  return pc[1] + (xac * bclen2 - xbc * aclen2 + sqrt(aclen2 * bclen2 * ablen2))
 
8168
               / (2.0 * ccwabc);
 
8169
}
 
8170
 
 
8171
#endif /* not REDUCED */
 
8172
 
 
8173
#ifndef REDUCED
 
8174
 
 
8175
void check4deadevent(
 
8176
struct triedge *checktri,
 
8177
struct event **freeevents,
 
8178
struct event **eventheap,
 
8179
int *heapsize)
 
8180
{
 
8181
  struct event *deadevent;
 
8182
  point eventpoint;
 
8183
  int eventnum;
 
8184
 
 
8185
  org(*checktri, eventpoint);
 
8186
  if (eventpoint != (point) NULL) {
 
8187
    deadevent = (struct event *) eventpoint;
 
8188
    eventnum = deadevent->heapposition;
 
8189
    deadevent->eventptr = (VOID *) *freeevents;
 
8190
    *freeevents = deadevent;
 
8191
    eventheapdelete(eventheap, *heapsize, eventnum);
 
8192
    (*heapsize)--;
 
8193
    setorg(*checktri, NULL);
 
8194
  }
 
8195
}
 
8196
 
 
8197
#endif /* not REDUCED */
 
8198
 
 
8199
#ifndef REDUCED
 
8200
 
 
8201
struct splaynode *splay(
 
8202
struct splaynode *splaytree,
 
8203
point searchpoint,
 
8204
struct triedge *searchtri)
 
8205
{
 
8206
  struct splaynode *child, *grandchild;
 
8207
  struct splaynode *lefttree, *righttree;
 
8208
  struct splaynode *leftright;
 
8209
  point checkpoint;
 
8210
  int rightofroot, rightofchild;
 
8211
 
 
8212
  if (splaytree == (struct splaynode *) NULL) {
 
8213
    return (struct splaynode *) NULL;
 
8214
  }
 
8215
  dest(splaytree->keyedge, checkpoint);
 
8216
  if (checkpoint == splaytree->keydest) {
 
8217
    rightofroot = rightofhyperbola(&splaytree->keyedge, searchpoint);
 
8218
    if (rightofroot) {
 
8219
      triedgecopy(splaytree->keyedge, *searchtri);
 
8220
      child = splaytree->rchild;
 
8221
    } else {
 
8222
      child = splaytree->lchild;
 
8223
    }
 
8224
    if (child == (struct splaynode *) NULL) {
 
8225
      return splaytree;
 
8226
    }
 
8227
    dest(child->keyedge, checkpoint);
 
8228
    if (checkpoint != child->keydest) {
 
8229
      child = splay(child, searchpoint, searchtri);
 
8230
      if (child == (struct splaynode *) NULL) {
 
8231
        if (rightofroot) {
 
8232
          splaytree->rchild = (struct splaynode *) NULL;
 
8233
        } else {
 
8234
          splaytree->lchild = (struct splaynode *) NULL;
 
8235
        }
 
8236
        return splaytree;
 
8237
      }
 
8238
    }
 
8239
    rightofchild = rightofhyperbola(&child->keyedge, searchpoint);
 
8240
    if (rightofchild) {
 
8241
      triedgecopy(child->keyedge, *searchtri);
 
8242
      grandchild = splay(child->rchild, searchpoint, searchtri);
 
8243
      child->rchild = grandchild;
 
8244
    } else {
 
8245
      grandchild = splay(child->lchild, searchpoint, searchtri);
 
8246
      child->lchild = grandchild;
 
8247
    }
 
8248
    if (grandchild == (struct splaynode *) NULL) {
 
8249
      if (rightofroot) {
 
8250
        splaytree->rchild = child->lchild;
 
8251
        child->lchild = splaytree;
 
8252
      } else {
 
8253
        splaytree->lchild = child->rchild;
 
8254
        child->rchild = splaytree;
 
8255
      }
 
8256
      return child;
 
8257
    }
 
8258
    if (rightofchild) {
 
8259
      if (rightofroot) {
 
8260
        splaytree->rchild = child->lchild;
 
8261
        child->lchild = splaytree;
 
8262
      } else {
 
8263
        splaytree->lchild = grandchild->rchild;
 
8264
        grandchild->rchild = splaytree;
 
8265
      }
 
8266
      child->rchild = grandchild->lchild;
 
8267
      grandchild->lchild = child;
 
8268
    } else {
 
8269
      if (rightofroot) {
 
8270
        splaytree->rchild = grandchild->lchild;
 
8271
        grandchild->lchild = splaytree;
 
8272
      } else {
 
8273
        splaytree->lchild = child->rchild;
 
8274
        child->rchild = splaytree;
 
8275
      }
 
8276
      child->lchild = grandchild->rchild;
 
8277
      grandchild->rchild = child;
 
8278
    }
 
8279
    return grandchild;
 
8280
  } else {
 
8281
    lefttree = splay(splaytree->lchild, searchpoint, searchtri);
 
8282
    righttree = splay(splaytree->rchild, searchpoint, searchtri);
 
8283
 
 
8284
    pooldealloc(&splaynodes, (VOID *) splaytree);
 
8285
    if (lefttree == (struct splaynode *) NULL) {
 
8286
      return righttree;
 
8287
    } else if (righttree == (struct splaynode *) NULL) {
 
8288
      return lefttree;
 
8289
    } else if (lefttree->rchild == (struct splaynode *) NULL) {
 
8290
      lefttree->rchild = righttree->lchild;
 
8291
      righttree->lchild = lefttree;
 
8292
      return righttree;
 
8293
    } else if (righttree->lchild == (struct splaynode *) NULL) {
 
8294
      righttree->lchild = lefttree->rchild;
 
8295
      lefttree->rchild = righttree;
 
8296
      return lefttree;
 
8297
    } else {
 
8298
/*      printf("Holy Toledo!!!\n"); */
 
8299
      leftright = lefttree->rchild;
 
8300
      while (leftright->rchild != (struct splaynode *) NULL) {
 
8301
        leftright = leftright->rchild;
 
8302
      }
 
8303
      leftright->rchild = righttree;
 
8304
      return lefttree;
 
8305
    }
 
8306
  }
 
8307
}
 
8308
 
 
8309
#endif /* not REDUCED */
 
8310
 
 
8311
#ifndef REDUCED
 
8312
 
 
8313
struct splaynode *splayinsert(
 
8314
struct splaynode *splayroot,
 
8315
struct triedge *newkey,
 
8316
point searchpoint)
 
8317
{
 
8318
  struct splaynode *newsplaynode;
 
8319
 
 
8320
  newsplaynode = (struct splaynode *) poolalloc(&splaynodes);
 
8321
  triedgecopy(*newkey, newsplaynode->keyedge);
 
8322
  dest(*newkey, newsplaynode->keydest);
 
8323
  if (splayroot == (struct splaynode *) NULL) {
 
8324
    newsplaynode->lchild = (struct splaynode *) NULL;
 
8325
    newsplaynode->rchild = (struct splaynode *) NULL;
 
8326
  } else if (rightofhyperbola(&splayroot->keyedge, searchpoint)) {
 
8327
    newsplaynode->lchild = splayroot;
 
8328
    newsplaynode->rchild = splayroot->rchild;
 
8329
    splayroot->rchild = (struct splaynode *) NULL;
 
8330
  } else {
 
8331
    newsplaynode->lchild = splayroot->lchild;
 
8332
    newsplaynode->rchild = splayroot;
 
8333
    splayroot->lchild = (struct splaynode *) NULL;
 
8334
  }
 
8335
  return newsplaynode;
 
8336
}
 
8337
 
 
8338
#endif /* not REDUCED */
 
8339
 
 
8340
#ifndef REDUCED
 
8341
 
 
8342
struct splaynode *circletopinsert(
 
8343
struct splaynode *splayroot,
 
8344
struct triedge *newkey,
 
8345
point pa,
 
8346
point pb,
 
8347
point pc,
 
8348
REAL topy)
 
8349
{
 
8350
  REAL ccwabc;
 
8351
  REAL xac, yac, xbc, ybc;
 
8352
  REAL aclen2, bclen2;
 
8353
  REAL searchpoint[2];
 
8354
  struct triedge dummytri;
 
8355
 
 
8356
  ccwabc = counterclockwise(pa, pb, pc);
 
8357
  xac = pa[0] - pc[0];
 
8358
  yac = pa[1] - pc[1];
 
8359
  xbc = pb[0] - pc[0];
 
8360
  ybc = pb[1] - pc[1];
 
8361
  aclen2 = xac * xac + yac * yac;
 
8362
  bclen2 = xbc * xbc + ybc * ybc;
 
8363
  searchpoint[0] = pc[0] - (yac * bclen2 - ybc * aclen2) / (2.0 * ccwabc);
 
8364
  searchpoint[1] = topy;
 
8365
  return splayinsert(splay(splayroot, (point) searchpoint, &dummytri), newkey,
 
8366
                     (point) searchpoint);
 
8367
}
 
8368
 
 
8369
#endif /* not REDUCED */
 
8370
 
 
8371
#ifndef REDUCED
 
8372
 
 
8373
struct splaynode *frontlocate(
 
8374
struct splaynode *splayroot,
 
8375
struct triedge *bottommost,
 
8376
point searchpoint,
 
8377
struct triedge *searchtri,
 
8378
int *farright)
 
8379
{
 
8380
  int farrightflag;
 
8381
  triangle ptr;                       /* Temporary variable used by onext(). */
 
8382
 
 
8383
  triedgecopy(*bottommost, *searchtri);
 
8384
  splayroot = splay(splayroot, searchpoint, searchtri);
 
8385
 
 
8386
  farrightflag = 0;
 
8387
  while (!farrightflag && rightofhyperbola(searchtri, searchpoint)) {
 
8388
    onextself(*searchtri);
 
8389
    farrightflag = triedgeequal(*searchtri, *bottommost);
 
8390
  }
 
8391
  *farright = farrightflag;
 
8392
  return splayroot;
 
8393
}
 
8394
 
 
8395
#endif /* not REDUCED */
 
8396
 
 
8397
#ifndef REDUCED
 
8398
 
 
8399
long sweeplinedelaunay()
 
8400
{
 
8401
  struct event **eventheap;
 
8402
  struct event *events;
 
8403
  struct event *freeevents;
 
8404
  struct event *nextevent;
 
8405
  struct event *newevent;
 
8406
  struct splaynode *splayroot;
 
8407
  struct triedge bottommost;
 
8408
  struct triedge searchtri;
 
8409
  struct triedge fliptri;
 
8410
  struct triedge lefttri, righttri, farlefttri, farrighttri;
 
8411
  struct triedge inserttri;
 
8412
  point firstpoint, secondpoint;
 
8413
  point nextpoint, lastpoint;
 
8414
  point connectpoint;
 
8415
  point leftpoint, midpoint, rightpoint;
 
8416
  REAL lefttest, righttest;
 
8417
  int heapsize;
 
8418
  int check4events, farrightflag;
 
8419
  triangle ptr;   /* Temporary variable used by sym(), onext(), and oprev(). */
 
8420
 
 
8421
  poolinit(&splaynodes, sizeof(struct splaynode), SPLAYNODEPERBLOCK, POINTER,
 
8422
           0);
 
8423
  splayroot = (struct splaynode *) NULL;
 
8424
 
 
8425
  if (verbose) {
 
8426
    printf("  Placing points in event heap.\n");
 
8427
  }
 
8428
  createeventheap(&eventheap, &events, &freeevents);
 
8429
  heapsize = inpoints;
 
8430
 
 
8431
  if (verbose) {
 
8432
    printf("  Forming triangulation.\n");
 
8433
  }
 
8434
  maketriangle(&lefttri);
 
8435
  maketriangle(&righttri);
 
8436
  bond(lefttri, righttri);
 
8437
  lnextself(lefttri);
 
8438
  lprevself(righttri);
 
8439
  bond(lefttri, righttri);
 
8440
  lnextself(lefttri);
 
8441
  lprevself(righttri);
 
8442
  bond(lefttri, righttri);
 
8443
  firstpoint = (point) eventheap[0]->eventptr;
 
8444
  eventheap[0]->eventptr = (VOID *) freeevents;
 
8445
  freeevents = eventheap[0];
 
8446
  eventheapdelete(eventheap, heapsize, 0);
 
8447
  heapsize--;
 
8448
  do {
 
8449
    if (heapsize == 0) {
 
8450
      printf("Error:  Input points are all identical.\n");
 
8451
      exit(1);
 
8452
    }
 
8453
    secondpoint = (point) eventheap[0]->eventptr;
 
8454
    eventheap[0]->eventptr = (VOID *) freeevents;
 
8455
    freeevents = eventheap[0];
 
8456
    eventheapdelete(eventheap, heapsize, 0);
 
8457
    heapsize--;
 
8458
    if ((firstpoint[0] == secondpoint[0])
 
8459
        && (firstpoint[1] == secondpoint[1])) {
 
8460
      printf(
 
8461
"Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
 
8462
             secondpoint[0], secondpoint[1]);
 
8463
/*  Commented out - would eliminate point from output .node file.
 
8464
      setpointmark(secondpoint, DEADPOINT);
 
8465
*/
 
8466
    }
 
8467
  } while ((firstpoint[0] == secondpoint[0])
 
8468
           && (firstpoint[1] == secondpoint[1]));
 
8469
  setorg(lefttri, firstpoint);
 
8470
  setdest(lefttri, secondpoint);
 
8471
  setorg(righttri, secondpoint);
 
8472
  setdest(righttri, firstpoint);
 
8473
  lprev(lefttri, bottommost);
 
8474
  lastpoint = secondpoint;
 
8475
  while (heapsize > 0) {
 
8476
    nextevent = eventheap[0];
 
8477
    eventheapdelete(eventheap, heapsize, 0);
 
8478
    heapsize--;
 
8479
    check4events = 1;
 
8480
    if (nextevent->xkey < xmin) {
 
8481
      decode(nextevent->eventptr, fliptri);
 
8482
      oprev(fliptri, farlefttri);
 
8483
      check4deadevent(&farlefttri, &freeevents, eventheap, &heapsize);
 
8484
      onext(fliptri, farrighttri);
 
8485
      check4deadevent(&farrighttri, &freeevents, eventheap, &heapsize);
 
8486
 
 
8487
      if (triedgeequal(farlefttri, bottommost)) {
 
8488
        lprev(fliptri, bottommost);
 
8489
      }
 
8490
      flip(&fliptri);
 
8491
      setapex(fliptri, NULL);
 
8492
      lprev(fliptri, lefttri);
 
8493
      lnext(fliptri, righttri);
 
8494
      sym(lefttri, farlefttri);
 
8495
 
 
8496
      if (randomnation(SAMPLERATE) == 0) {
 
8497
        symself(fliptri);
 
8498
        dest(fliptri, leftpoint);
 
8499
        apex(fliptri, midpoint);
 
8500
        org(fliptri, rightpoint);
 
8501
        splayroot = circletopinsert(splayroot, &lefttri, leftpoint, midpoint,
 
8502
                                    rightpoint, nextevent->ykey);
 
8503
      }
 
8504
    } else {
 
8505
      nextpoint = (point) nextevent->eventptr;
 
8506
      if ((nextpoint[0] == lastpoint[0]) && (nextpoint[1] == lastpoint[1])) {
 
8507
        printf(
 
8508
"Warning:  A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
 
8509
               nextpoint[0], nextpoint[1]);
 
8510
/*  Commented out - would eliminate point from output .node file.
 
8511
        setpointmark(nextpoint, DEADPOINT);
 
8512
*/
 
8513
        check4events = 0;
 
8514
      } else {
 
8515
        lastpoint = nextpoint;
 
8516
 
 
8517
        splayroot = frontlocate(splayroot, &bottommost, nextpoint, &searchtri,
 
8518
                                &farrightflag);
 
8519
/*
 
8520
        triedgecopy(bottommost, searchtri);
 
8521
        farrightflag = 0;
 
8522
        while (!farrightflag && rightofhyperbola(&searchtri, nextpoint)) {
 
8523
          onextself(searchtri);
 
8524
          farrightflag = triedgeequal(searchtri, bottommost);
 
8525
        }
 
8526
*/
 
8527
 
 
8528
        check4deadevent(&searchtri, &freeevents, eventheap, &heapsize);
 
8529
 
 
8530
        triedgecopy(searchtri, farrighttri);
 
8531
        sym(searchtri, farlefttri);
 
8532
        maketriangle(&lefttri);
 
8533
        maketriangle(&righttri);
 
8534
        dest(farrighttri, connectpoint);
 
8535
        setorg(lefttri, connectpoint);
 
8536
        setdest(lefttri, nextpoint);
 
8537
        setorg(righttri, nextpoint);
 
8538
        setdest(righttri, connectpoint);
 
8539
        bond(lefttri, righttri);
 
8540
        lnextself(lefttri);
 
8541
        lprevself(righttri);
 
8542
        bond(lefttri, righttri);
 
8543
        lnextself(lefttri);
 
8544
        lprevself(righttri);
 
8545
        bond(lefttri, farlefttri);
 
8546
        bond(righttri, farrighttri);
 
8547
        if (!farrightflag && triedgeequal(farrighttri, bottommost)) {
 
8548
          triedgecopy(lefttri, bottommost);
 
8549
        }
 
8550
 
 
8551
        if (randomnation(SAMPLERATE) == 0) {
 
8552
          splayroot = splayinsert(splayroot, &lefttri, nextpoint);
 
8553
        } else if (randomnation(SAMPLERATE) == 0) {
 
8554
          lnext(righttri, inserttri);
 
8555
          splayroot = splayinsert(splayroot, &inserttri, nextpoint);
 
8556
        }
 
8557
      }
 
8558
    }
 
8559
    nextevent->eventptr = (VOID *) freeevents;
 
8560
    freeevents = nextevent;
 
8561
 
 
8562
    if (check4events) {
 
8563
      apex(farlefttri, leftpoint);
 
8564
      dest(lefttri, midpoint);
 
8565
      apex(lefttri, rightpoint);
 
8566
      lefttest = counterclockwise(leftpoint, midpoint, rightpoint);
 
8567
      if (lefttest > 0.0) {
 
8568
        newevent = freeevents;
 
8569
        freeevents = (struct event *) freeevents->eventptr;
 
8570
        newevent->xkey = xminextreme;
 
8571
        newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
 
8572
                                   lefttest);
 
8573
        newevent->eventptr = (VOID *) encode(lefttri);
 
8574
        eventheapinsert(eventheap, heapsize, newevent);
 
8575
        heapsize++;
 
8576
        setorg(lefttri, newevent);
 
8577
      }
 
8578
      apex(righttri, leftpoint);
 
8579
      org(righttri, midpoint);
 
8580
      apex(farrighttri, rightpoint);
 
8581
      righttest = counterclockwise(leftpoint, midpoint, rightpoint);
 
8582
      if (righttest > 0.0) {
 
8583
        newevent = freeevents;
 
8584
        freeevents = (struct event *) freeevents->eventptr;
 
8585
        newevent->xkey = xminextreme;
 
8586
        newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
 
8587
                                   righttest);
 
8588
        newevent->eventptr = (VOID *) encode(farrighttri);
 
8589
        eventheapinsert(eventheap, heapsize, newevent);
 
8590
        heapsize++;
 
8591
        setorg(farrighttri, newevent);
 
8592
      }
 
8593
    }
 
8594
  }
 
8595
 
 
8596
  pooldeinit(&splaynodes);
 
8597
  lprevself(bottommost);
 
8598
  return removeghosts(&bottommost);
 
8599
}
 
8600
 
 
8601
#endif /* not REDUCED */
 
8602
 
 
8603
/**                                                                         **/
 
8604
/**                                                                         **/
 
8605
/********* Sweepline Delaunay triangulation ends here                *********/
 
8606
 
 
8607
/********* General mesh construction routines begin here             *********/
 
8608
/**                                                                         **/
 
8609
/**                                                                         **/
 
8610
 
 
8611
/*****************************************************************************/
 
8612
/*                                                                           */
 
8613
/*  delaunay()   Form a Delaunay triangulation.                              */
 
8614
/*                                                                           */
 
8615
/*****************************************************************************/
 
8616
 
 
8617
long delaunay()
 
8618
{
 
8619
  eextras = 0;
 
8620
  initializetrisegpools();
 
8621
 
 
8622
#ifdef REDUCED
 
8623
  if (!quiet) {
 
8624
    printf(
 
8625
      "Constructing Delaunay triangulation by divide-and-conquer method.\n");
 
8626
  }
 
8627
  return divconqdelaunay();
 
8628
#else /* not REDUCED */
 
8629
  if (!quiet) {
 
8630
    printf("Constructing Delaunay triangulation ");
 
8631
    if (incremental) {
 
8632
      printf("by incremental method.\n");
 
8633
    } else if (sweepline) {
 
8634
      printf("by sweepline method.\n");
 
8635
    } else {
 
8636
      printf("by divide-and-conquer method.\n");
 
8637
    }
 
8638
  }
 
8639
  if (incremental) {
 
8640
    return incrementaldelaunay();
 
8641
  } else if (sweepline) {
 
8642
    return sweeplinedelaunay();
 
8643
  } else {
 
8644
    return divconqdelaunay();
 
8645
  }
 
8646
#endif /* not REDUCED */
 
8647
}
 
8648
 
 
8649
/*****************************************************************************/
 
8650
/*                                                                           */
 
8651
/*  reconstruct()   Reconstruct a triangulation from its .ele (and possibly  */
 
8652
/*                  .poly) file.  Used when the -r switch is used.           */
 
8653
/*                                                                           */
 
8654
/*  Reads an .ele file and reconstructs the original mesh.  If the -p switch */
 
8655
/*  is used, this procedure will also read a .poly file and reconstruct the  */
 
8656
/*  shell edges of the original mesh.  If the -a switch is used, this        */
 
8657
/*  procedure will also read an .area file and set a maximum area constraint */
 
8658
/*  on each triangle.                                                        */
 
8659
/*                                                                           */
 
8660
/*  Points that are not corners of triangles, such as nodes on edges of      */
 
8661
/*  subparametric elements, are discarded.                                   */
 
8662
/*                                                                           */
 
8663
/*  This routine finds the adjacencies between triangles (and shell edges)   */
 
8664
/*  by forming one stack of triangles for each vertex.  Each triangle is on  */
 
8665
/*  three different stacks simultaneously.  Each triangle's shell edge       */
 
8666
/*  pointers are used to link the items in each stack.  This memory-saving   */
 
8667
/*  feature makes the code harder to read.  The most important thing to keep */
 
8668
/*  in mind is that each triangle is removed from a stack precisely when     */
 
8669
/*  the corresponding pointer is adjusted to refer to a shell edge rather    */
 
8670
/*  than the next triangle of the stack.                                     */
 
8671
/*                                                                           */
 
8672
/*****************************************************************************/
 
8673
 
 
8674
#ifndef CDT_ONLY
 
8675
 
 
8676
#ifdef TRILIBRARY
 
8677
 
 
8678
int reconstruct(
 
8679
int *trianglelist,
 
8680
REAL *triangleattriblist,
 
8681
REAL *trianglearealist,
 
8682
int elements,
 
8683
int corners,
 
8684
int attribs,
 
8685
int *segmentlist,
 
8686
int *segmentmarkerlist,
 
8687
int numberofsegments)
 
8688
 
 
8689
#else /* not TRILIBRARY */
 
8690
 
 
8691
long reconstruct(
 
8692
char *elefilename,
 
8693
char *areafilename,
 
8694
char *polyfilename,
 
8695
FILE *polyfile)
 
8696
 
 
8697
#endif /* not TRILIBRARY */
 
8698
 
 
8699
{
 
8700
#ifdef TRILIBRARY
 
8701
  int pointindex;
 
8702
  int attribindex;
 
8703
#else /* not TRILIBRARY */
 
8704
  FILE *elefile;
 
8705
  FILE *areafile;
 
8706
  char inputline[INPUTLINESIZE];
 
8707
  char *stringptr;
 
8708
  int areaelements;
 
8709
#endif /* not TRILIBRARY */
 
8710
  struct triedge triangleloop;
 
8711
  struct triedge triangleleft;
 
8712
  struct triedge checktri;
 
8713
  struct triedge checkleft;
 
8714
  struct triedge checkneighbor;
 
8715
  struct edge shelleloop;
 
8716
  triangle *vertexarray;
 
8717
  triangle *prevlink;
 
8718
  triangle nexttri;
 
8719
  point tdest, tapex;
 
8720
  point checkdest, checkapex;
 
8721
  point shorg;
 
8722
  point killpoint;
 
8723
  REAL area;
 
8724
  int corner[3];
 
8725
  int end[2];
 
8726
  int killpointindex;
 
8727
  int incorners;
 
8728
  int segmentmarkers;
 
8729
  int boundmarker;
 
8730
  int aroundpoint;
 
8731
  long hullsize;
 
8732
  int notfound;
 
8733
  int elementnumber, segmentnumber;
 
8734
  int i, j;
 
8735
  triangle ptr;                         /* Temporary variable used by sym(). */
 
8736
 
 
8737
#ifdef TRILIBRARY
 
8738
  inelements = elements;
 
8739
  incorners = corners;
 
8740
  if (incorners < 3) {
 
8741
    printf("Error:  Triangles must have at least 3 points.\n");
 
8742
    exit(1);
 
8743
  }
 
8744
  eextras = attribs;
 
8745
#else /* not TRILIBRARY */
 
8746
  /* Read the triangles from an .ele file. */
 
8747
  if (!quiet) {
 
8748
    printf("Opening %s.\n", elefilename);
 
8749
  }
 
8750
  elefile = fopen(elefilename, "r");
 
8751
  if (elefile == (FILE *) NULL) {
 
8752
    printf("  Error:  Cannot access file %s.\n", elefilename);
 
8753
    exit(1);
 
8754
  }
 
8755
  /* Read number of triangles, number of points per triangle, and */
 
8756
  /*   number of triangle attributes from .ele file.              */
 
8757
  stringptr = readline(inputline, elefile, elefilename);
 
8758
  inelements = (int) strtol (stringptr, &stringptr, 0);
 
8759
  stringptr = findfield(stringptr);
 
8760
  if (*stringptr == '\0') {
 
8761
    incorners = 3;
 
8762
  } else {
 
8763
    incorners = (int) strtol (stringptr, &stringptr, 0);
 
8764
    if (incorners < 3) {
 
8765
      printf("Error:  Triangles in %s must have at least 3 points.\n",
 
8766
             elefilename);
 
8767
      exit(1);
 
8768
    }
 
8769
  }
 
8770
  stringptr = findfield(stringptr);
 
8771
  if (*stringptr == '\0') {
 
8772
    eextras = 0;
 
8773
  } else {
 
8774
    eextras = (int) strtol (stringptr, &stringptr, 0);
 
8775
  }
 
8776
#endif /* not TRILIBRARY */
 
8777
 
 
8778
  initializetrisegpools();
 
8779
 
 
8780
  /* Create the triangles. */
 
8781
  for (elementnumber = 1; elementnumber <= inelements; elementnumber++) {
 
8782
    maketriangle(&triangleloop);
 
8783
    /* Mark the triangle as living. */
 
8784
    triangleloop.tri[3] = (triangle) triangleloop.tri;
 
8785
  }
 
8786
 
 
8787
  if (poly) {
 
8788
#ifdef TRILIBRARY
 
8789
    insegments = numberofsegments;
 
8790
    segmentmarkers = segmentmarkerlist != (int *) NULL;
 
8791
#else /* not TRILIBRARY */
 
8792
    /* Read number of segments and number of segment */
 
8793
    /*   boundary markers from .poly file.           */
 
8794
    stringptr = readline(inputline, polyfile, inpolyfilename);
 
8795
    insegments = (int) strtol (stringptr, &stringptr, 0);
 
8796
    stringptr = findfield(stringptr);
 
8797
    if (*stringptr == '\0') {
 
8798
      segmentmarkers = 0;
 
8799
    } else {
 
8800
      segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
 
8801
    }
 
8802
#endif /* not TRILIBRARY */
 
8803
 
 
8804
    /* Create the shell edges. */
 
8805
    for (segmentnumber = 1; segmentnumber <= insegments; segmentnumber++) {
 
8806
      makeshelle(&shelleloop);
 
8807
      /* Mark the shell edge as living. */
 
8808
      shelleloop.sh[2] = (shelle) shelleloop.sh;
 
8809
    }
 
8810
  }
 
8811
 
 
8812
#ifdef TRILIBRARY
 
8813
  pointindex = 0;
 
8814
  attribindex = 0;
 
8815
#else /* not TRILIBRARY */
 
8816
  if (vararea) {
 
8817
    /* Open an .area file, check for consistency with the .ele file. */
 
8818
    if (!quiet) {
 
8819
      printf("Opening %s.\n", areafilename);
 
8820
    }
 
8821
    areafile = fopen(areafilename, "r");
 
8822
    if (areafile == (FILE *) NULL) {
 
8823
      printf("  Error:  Cannot access file %s.\n", areafilename);
 
8824
      exit(1);
 
8825
    }
 
8826
    stringptr = readline(inputline, areafile, areafilename);
 
8827
    areaelements = (int) strtol (stringptr, &stringptr, 0);
 
8828
    if (areaelements != inelements) {
 
8829
      printf("Error:  %s and %s disagree on number of triangles.\n",
 
8830
             elefilename, areafilename);
 
8831
      exit(1);
 
8832
    }
 
8833
  }
 
8834
#endif /* not TRILIBRARY */
 
8835
 
 
8836
  if (!quiet) {
 
8837
    printf("Reconstructing mesh.\n");
 
8838
  }
 
8839
  /* Allocate a temporary array that maps each point to some adjacent  */
 
8840
  /*   triangle.  I took care to allocate all the permanent memory for */
 
8841
  /*   triangles and shell edges first.                                */
 
8842
  vertexarray = (triangle *) malloc(points.items * sizeof(triangle));
 
8843
  if (vertexarray == (triangle *) NULL) {
 
8844
    printf("Error:  Out of memory.\n");
 
8845
    exit(1);
 
8846
  }
 
8847
  /* Each point is initially unrepresented. */
 
8848
  for (i = 0; i < points.items; i++) {
 
8849
    vertexarray[i] = (triangle) dummytri;
 
8850
  }
 
8851
 
 
8852
  if (verbose) {
 
8853
    printf("  Assembling triangles.\n");
 
8854
  }
 
8855
  /* Read the triangles from the .ele file, and link */
 
8856
  /*   together those that share an edge.            */
 
8857
  traversalinit(&triangles);
 
8858
  triangleloop.tri = triangletraverse();
 
8859
  elementnumber = firstnumber;
 
8860
  while (triangleloop.tri != (triangle *) NULL) {
 
8861
#ifdef TRILIBRARY
 
8862
    /* Copy the triangle's three corners. */
 
8863
    for (j = 0; j < 3; j++) {
 
8864
      corner[j] = trianglelist[pointindex++];
 
8865
      if ((corner[j] < firstnumber) || (corner[j] >= firstnumber + inpoints)) {
 
8866
        printf("Error:  Triangle %d has an invalid vertex index.\n",
 
8867
               elementnumber);
 
8868
        exit(1);
 
8869
      }
 
8870
    }
 
8871
#else /* not TRILIBRARY */
 
8872
    /* Read triangle number and the triangle's three corners. */
 
8873
    stringptr = readline(inputline, elefile, elefilename);
 
8874
    for (j = 0; j < 3; j++) {
 
8875
      stringptr = findfield(stringptr);
 
8876
      if (*stringptr == '\0') {
 
8877
        printf("Error:  Triangle %d is missing point %d in %s.\n",
 
8878
               elementnumber, j + 1, elefilename);
 
8879
        exit(1);
 
8880
      } else {
 
8881
        corner[j] = (int) strtol (stringptr, &stringptr, 0);
 
8882
        if ((corner[j] < firstnumber) ||
 
8883
            (corner[j] >= firstnumber + inpoints)) {
 
8884
          printf("Error:  Triangle %d has an invalid vertex index.\n",
 
8885
                 elementnumber);
 
8886
          exit(1);
 
8887
        }
 
8888
      }
 
8889
    }
 
8890
#endif /* not TRILIBRARY */
 
8891
 
 
8892
    /* Find out about (and throw away) extra nodes. */
 
8893
    for (j = 3; j < incorners; j++) {
 
8894
#ifdef TRILIBRARY
 
8895
      killpointindex = trianglelist[pointindex++];
 
8896
#else /* not TRILIBRARY */
 
8897
      stringptr = findfield(stringptr);
 
8898
      if (*stringptr != '\0') {
 
8899
        killpointindex = (int) strtol (stringptr, &stringptr, 0);
 
8900
#endif /* not TRILIBRARY */
 
8901
        if ((killpointindex >= firstnumber) &&
 
8902
            (killpointindex < firstnumber + inpoints)) {
 
8903
          /* Delete the non-corner point if it's not already deleted. */
 
8904
          killpoint = getpoint(killpointindex);
 
8905
          if (pointmark(killpoint) != DEADPOINT) {
 
8906
            pointdealloc(killpoint);
 
8907
          }
 
8908
        }
 
8909
#ifndef TRILIBRARY
 
8910
      }
 
8911
#endif /* not TRILIBRARY */
 
8912
    }
 
8913
 
 
8914
    /* Read the triangle's attributes. */
 
8915
    for (j = 0; j < eextras; j++) {
 
8916
#ifdef TRILIBRARY
 
8917
      setelemattribute(triangleloop, j, triangleattriblist[attribindex++]);
 
8918
#else /* not TRILIBRARY */
 
8919
      stringptr = findfield(stringptr);
 
8920
      if (*stringptr == '\0') {
 
8921
        setelemattribute(triangleloop, j, 0);
 
8922
      } else {
 
8923
        setelemattribute(triangleloop, j,
 
8924
                         (REAL) strtod (stringptr, &stringptr));
 
8925
      }
 
8926
#endif /* not TRILIBRARY */
 
8927
    }
 
8928
 
 
8929
    if (vararea) {
 
8930
#ifdef TRILIBRARY
 
8931
      area = trianglearealist[elementnumber - firstnumber];
 
8932
#else /* not TRILIBRARY */
 
8933
      /* Read an area constraint from the .area file. */
 
8934
      stringptr = readline(inputline, areafile, areafilename);
 
8935
      stringptr = findfield(stringptr);
 
8936
      if (*stringptr == '\0') {
 
8937
        area = -1.0;                      /* No constraint on this triangle. */
 
8938
      } else {
 
8939
        area = (REAL) strtod(stringptr, &stringptr);
 
8940
      }
 
8941
#endif /* not TRILIBRARY */
 
8942
      setareabound(triangleloop, area);
 
8943
    }
 
8944
 
 
8945
    /* Set the triangle's vertices. */
 
8946
    triangleloop.orient = 0;
 
8947
    setorg(triangleloop, getpoint(corner[0]));
 
8948
    setdest(triangleloop, getpoint(corner[1]));
 
8949
    setapex(triangleloop, getpoint(corner[2]));
 
8950
    /* Try linking the triangle to others that share these vertices. */
 
8951
    for (triangleloop.orient = 0; triangleloop.orient < 3;
 
8952
         triangleloop.orient++) {
 
8953
      /* Take the number for the origin of triangleloop. */
 
8954
      aroundpoint = corner[triangleloop.orient];
 
8955
      /* Look for other triangles having this vertex. */
 
8956
      nexttri = vertexarray[aroundpoint - firstnumber];
 
8957
      /* Link the current triangle to the next one in the stack. */
 
8958
      triangleloop.tri[6 + triangleloop.orient] = nexttri;
 
8959
      /* Push the current triangle onto the stack. */
 
8960
      vertexarray[aroundpoint - firstnumber] = encode(triangleloop);
 
8961
      decode(nexttri, checktri);
 
8962
      if (checktri.tri != dummytri) {
 
8963
        dest(triangleloop, tdest);
 
8964
        apex(triangleloop, tapex);
 
8965
        /* Look for other triangles that share an edge. */
 
8966
        do {
 
8967
          dest(checktri, checkdest);
 
8968
          apex(checktri, checkapex);
 
8969
          if (tapex == checkdest) {
 
8970
            /* The two triangles share an edge; bond them together. */
 
8971
            lprev(triangleloop, triangleleft);
 
8972
            bond(triangleleft, checktri);
 
8973
          }
 
8974
          if (tdest == checkapex) {
 
8975
            /* The two triangles share an edge; bond them together. */
 
8976
            lprev(checktri, checkleft);
 
8977
            bond(triangleloop, checkleft);
 
8978
          }
 
8979
          /* Find the next triangle in the stack. */
 
8980
          nexttri = checktri.tri[6 + checktri.orient];
 
8981
          decode(nexttri, checktri);
 
8982
        } while (checktri.tri != dummytri);
 
8983
      }
 
8984
    }
 
8985
    triangleloop.tri = triangletraverse();
 
8986
    elementnumber++;
 
8987
  }
 
8988
 
 
8989
#ifdef TRILIBRARY
 
8990
  pointindex = 0;
 
8991
#else /* not TRILIBRARY */
 
8992
  fclose(elefile);
 
8993
  if (vararea) {
 
8994
    fclose(areafile);
 
8995
  }
 
8996
#endif /* not TRILIBRARY */
 
8997
 
 
8998
  hullsize = 0;                      /* Prepare to count the boundary edges. */
 
8999
  if (poly) {
 
9000
    if (verbose) {
 
9001
      printf("  Marking segments in triangulation.\n");
 
9002
    }
 
9003
    /* Read the segments from the .poly file, and link them */
 
9004
    /*   to their neighboring triangles.                    */
 
9005
    boundmarker = 0;
 
9006
    traversalinit(&shelles);
 
9007
    shelleloop.sh = shelletraverse();
 
9008
    segmentnumber = firstnumber;
 
9009
    while (shelleloop.sh != (shelle *) NULL) {
 
9010
#ifdef TRILIBRARY
 
9011
      end[0] = segmentlist[pointindex++];
 
9012
      end[1] = segmentlist[pointindex++];
 
9013
      if (segmentmarkers) {
 
9014
        boundmarker = segmentmarkerlist[segmentnumber - firstnumber];
 
9015
      }
 
9016
#else /* not TRILIBRARY */
 
9017
      /* Read the endpoints of each segment, and possibly a boundary marker. */
 
9018
      stringptr = readline(inputline, polyfile, inpolyfilename);
 
9019
      /* Skip the first (segment number) field. */
 
9020
      stringptr = findfield(stringptr);
 
9021
      if (*stringptr == '\0') {
 
9022
        printf("Error:  Segment %d has no endpoints in %s.\n", segmentnumber,
 
9023
               polyfilename);
 
9024
        exit(1);
 
9025
      } else {
 
9026
        end[0] = (int) strtol (stringptr, &stringptr, 0);
 
9027
      }
 
9028
      stringptr = findfield(stringptr);
 
9029
      if (*stringptr == '\0') {
 
9030
        printf("Error:  Segment %d is missing its second endpoint in %s.\n",
 
9031
               segmentnumber, polyfilename);
 
9032
        exit(1);
 
9033
      } else {
 
9034
        end[1] = (int) strtol (stringptr, &stringptr, 0);
 
9035
      }
 
9036
      if (segmentmarkers) {
 
9037
        stringptr = findfield(stringptr);
 
9038
        if (*stringptr == '\0') {
 
9039
          boundmarker = 0;
 
9040
        } else {
 
9041
          boundmarker = (int) strtol (stringptr, &stringptr, 0);
 
9042
        }
 
9043
      }
 
9044
#endif /* not TRILIBRARY */
 
9045
      for (j = 0; j < 2; j++) {
 
9046
        if ((end[j] < firstnumber) || (end[j] >= firstnumber + inpoints)) {
 
9047
          printf("Error:  Segment %d has an invalid vertex index.\n", 
 
9048
                 segmentnumber);
 
9049
          exit(1);
 
9050
        }
 
9051
      }
 
9052
 
 
9053
      /* set the shell edge's vertices. */
 
9054
      shelleloop.shorient = 0;
 
9055
      setsorg(shelleloop, getpoint(end[0]));
 
9056
      setsdest(shelleloop, getpoint(end[1]));
 
9057
      setmark(shelleloop, boundmarker);
 
9058
      /* Try linking the shell edge to triangles that share these vertices. */
 
9059
      for (shelleloop.shorient = 0; shelleloop.shorient < 2;
 
9060
           shelleloop.shorient++) {
 
9061
        /* Take the number for the destination of shelleloop. */
 
9062
        aroundpoint = end[1 - shelleloop.shorient];
 
9063
        /* Look for triangles having this vertex. */
 
9064
        prevlink = &vertexarray[aroundpoint - firstnumber];
 
9065
        nexttri = vertexarray[aroundpoint - firstnumber];
 
9066
        decode(nexttri, checktri);
 
9067
        sorg(shelleloop, shorg);
 
9068
        notfound = 1;
 
9069
        /* Look for triangles having this edge.  Note that I'm only       */
 
9070
        /*   comparing each triangle's destination with the shell edge;   */
 
9071
        /*   each triangle's apex is handled through a different vertex.  */
 
9072
        /*   Because each triangle appears on three vertices' lists, each */
 
9073
        /*   occurrence of a triangle on a list can (and does) represent  */
 
9074
        /*   an edge.  In this way, most edges are represented twice, and */
 
9075
        /*   every triangle-segment bond is represented once.             */
 
9076
        while (notfound && (checktri.tri != dummytri)) {
 
9077
          dest(checktri, checkdest);
 
9078
          if (shorg == checkdest) {
 
9079
            /* We have a match.  Remove this triangle from the list. */
 
9080
            *prevlink = checktri.tri[6 + checktri.orient];
 
9081
            /* Bond the shell edge to the triangle. */
 
9082
            tsbond(checktri, shelleloop);
 
9083
            /* Check if this is a boundary edge. */
 
9084
            sym(checktri, checkneighbor);
 
9085
            if (checkneighbor.tri == dummytri) {
 
9086
              /* The next line doesn't insert a shell edge (because there's */
 
9087
              /*   already one there), but it sets the boundary markers of  */
 
9088
              /*   the existing shell edge and its vertices.                */
 
9089
              insertshelle(&checktri, 1);
 
9090
              hullsize++;
 
9091
            }
 
9092
            notfound = 0;
 
9093
          }
 
9094
          /* Find the next triangle in the stack. */
 
9095
          prevlink = &checktri.tri[6 + checktri.orient];
 
9096
          nexttri = checktri.tri[6 + checktri.orient];
 
9097
          decode(nexttri, checktri);
 
9098
        }
 
9099
      }
 
9100
      shelleloop.sh = shelletraverse();
 
9101
      segmentnumber++;
 
9102
    }
 
9103
  }
 
9104
 
 
9105
  /* Mark the remaining edges as not being attached to any shell edge. */
 
9106
  /* Also, count the (yet uncounted) boundary edges.                   */
 
9107
  for (i = 0; i < points.items; i++) {
 
9108
    /* Search the stack of triangles adjacent to a point. */
 
9109
    nexttri = vertexarray[i];
 
9110
    decode(nexttri, checktri);
 
9111
    while (checktri.tri != dummytri) {
 
9112
      /* Find the next triangle in the stack before this */
 
9113
      /*   information gets overwritten.                 */
 
9114
      nexttri = checktri.tri[6 + checktri.orient];
 
9115
      /* No adjacent shell edge.  (This overwrites the stack info.) */
 
9116
      tsdissolve(checktri);
 
9117
      sym(checktri, checkneighbor);
 
9118
      if (checkneighbor.tri == dummytri) {
 
9119
        insertshelle(&checktri, 1);
 
9120
        hullsize++;
 
9121
      }
 
9122
      decode(nexttri, checktri);
 
9123
    }
 
9124
  }
 
9125
 
 
9126
  free(vertexarray);
 
9127
  return hullsize;
 
9128
}
 
9129
 
 
9130
#endif /* not CDT_ONLY */
 
9131
 
 
9132
/**                                                                         **/
 
9133
/**                                                                         **/
 
9134
/********* General mesh construction routines end here               *********/
 
9135
 
 
9136
/********* Segment (shell edge) insertion begins here                *********/
 
9137
/**                                                                         **/
 
9138
/**                                                                         **/
 
9139
 
 
9140
/*****************************************************************************/
 
9141
/*                                                                           */
 
9142
/*  finddirection()   Find the first triangle on the path from one point     */
 
9143
/*                    to another.                                            */
 
9144
/*                                                                           */
 
9145
/*  Finds the triangle that intersects a line segment drawn from the         */
 
9146
/*  origin of `searchtri' to the point `endpoint', and returns the result    */
 
9147
/*  in `searchtri'.  The origin of `searchtri' does not change, even though  */
 
9148
/*  the triangle returned may differ from the one passed in.  This routine   */
 
9149
/*  is used to find the direction to move in to get from one point to        */
 
9150
/*  another.                                                                 */
 
9151
/*                                                                           */
 
9152
/*  The return value notes whether the destination or apex of the found      */
 
9153
/*  triangle is collinear with the two points in question.                   */
 
9154
/*                                                                           */
 
9155
/*****************************************************************************/
 
9156
 
 
9157
enum finddirectionresult finddirection(
 
9158
struct triedge *searchtri,
 
9159
point endpoint)
 
9160
{
 
9161
  struct triedge checktri;
 
9162
  point startpoint;
 
9163
  point leftpoint, rightpoint;
 
9164
  REAL leftccw, rightccw;
 
9165
  int leftflag, rightflag;
 
9166
  triangle ptr;           /* Temporary variable used by onext() and oprev(). */
 
9167
 
 
9168
  org(*searchtri, startpoint);
 
9169
  dest(*searchtri, rightpoint);
 
9170
  apex(*searchtri, leftpoint);
 
9171
  /* Is `endpoint' to the left? */
 
9172
  leftccw = counterclockwise(endpoint, startpoint, leftpoint);
 
9173
  leftflag = leftccw > 0.0;
 
9174
  /* Is `endpoint' to the right? */
 
9175
  rightccw = counterclockwise(startpoint, endpoint, rightpoint);
 
9176
  rightflag = rightccw > 0.0;
 
9177
  if (leftflag && rightflag) {
 
9178
    /* `searchtri' faces directly away from `endpoint'.  We could go */
 
9179
    /*   left or right.  Ask whether it's a triangle or a boundary   */
 
9180
    /*   on the left.                                                */
 
9181
    onext(*searchtri, checktri);
 
9182
    if (checktri.tri == dummytri) {
 
9183
      leftflag = 0;
 
9184
    } else {
 
9185
      rightflag = 0;
 
9186
    }
 
9187
  }
 
9188
  while (leftflag) {
 
9189
    /* Turn left until satisfied. */
 
9190
    onextself(*searchtri);
 
9191
    if (searchtri->tri == dummytri) {
 
9192
      printf("Internal error in finddirection():  Unable to find a\n");
 
9193
      printf("  triangle leading from (%.12g, %.12g) to", startpoint[0],
 
9194
             startpoint[1]);
 
9195
      printf("  (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
 
9196
      internalerror();
 
9197
    }
 
9198
    apex(*searchtri, leftpoint);
 
9199
    rightccw = leftccw;
 
9200
    leftccw = counterclockwise(endpoint, startpoint, leftpoint);
 
9201
    leftflag = leftccw > 0.0;
 
9202
  }
 
9203
  while (rightflag) {
 
9204
    /* Turn right until satisfied. */
 
9205
    oprevself(*searchtri);
 
9206
    if (searchtri->tri == dummytri) {
 
9207
      printf("Internal error in finddirection():  Unable to find a\n");
 
9208
      printf("  triangle leading from (%.12g, %.12g) to", startpoint[0],
 
9209
             startpoint[1]);
 
9210
      printf("  (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
 
9211
      internalerror();
 
9212
    }
 
9213
    dest(*searchtri, rightpoint);
 
9214
    leftccw = rightccw;
 
9215
    rightccw = counterclockwise(startpoint, endpoint, rightpoint);
 
9216
    rightflag = rightccw > 0.0;
 
9217
  }
 
9218
  if (leftccw == 0.0) {
 
9219
    return LEFTCOLLINEAR;
 
9220
  } else if (rightccw == 0.0) {
 
9221
    return RIGHTCOLLINEAR;
 
9222
  } else {
 
9223
    return WITHIN;
 
9224
  }
 
9225
}
 
9226
 
 
9227
/*****************************************************************************/
 
9228
/*                                                                           */
 
9229
/*  segmentintersection()   Find the intersection of an existing segment     */
 
9230
/*                          and a segment that is being inserted.  Insert    */
 
9231
/*                          a point at the intersection, splitting an        */
 
9232
/*                          existing shell edge.                             */
 
9233
/*                                                                           */
 
9234
/*  The segment being inserted connects the apex of splittri to endpoint2.   */
 
9235
/*  splitshelle is the shell edge being split, and MUST be opposite          */
 
9236
/*  splittri.  Hence, the edge being split connects the origin and           */
 
9237
/*  destination of splittri.                                                 */
 
9238
/*                                                                           */
 
9239
/*  On completion, splittri is a handle having the newly inserted            */
 
9240
/*  intersection point as its origin, and endpoint1 as its destination.      */
 
9241
/*                                                                           */
 
9242
/*****************************************************************************/
 
9243
 
 
9244
void segmentintersection(
 
9245
struct triedge *splittri,
 
9246
struct edge *splitshelle,
 
9247
point endpoint2)
 
9248
{
 
9249
  point endpoint1;
 
9250
  point torg, tdest;
 
9251
  point leftpoint, rightpoint;
 
9252
  point newpoint;
 
9253
  enum insertsiteresult success;
 
9254
  enum finddirectionresult collinear;
 
9255
  REAL ex, ey;
 
9256
  REAL tx, ty;
 
9257
  REAL etx, ety;
 
9258
  REAL split, denom;
 
9259
  int i;
 
9260
  triangle ptr;                       /* Temporary variable used by onext(). */
 
9261
 
 
9262
  /* Find the other three segment endpoints. */
 
9263
  apex(*splittri, endpoint1);
 
9264
  org(*splittri, torg);
 
9265
  dest(*splittri, tdest);
 
9266
  /* Segment intersection formulae; see the Antonio reference. */
 
9267
  tx = tdest[0] - torg[0];
 
9268
  ty = tdest[1] - torg[1];
 
9269
  ex = endpoint2[0] - endpoint1[0];
 
9270
  ey = endpoint2[1] - endpoint1[1];
 
9271
  etx = torg[0] - endpoint2[0];
 
9272
  ety = torg[1] - endpoint2[1];
 
9273
  denom = ty * ex - tx * ey;
 
9274
  if (denom == 0.0) {
 
9275
    printf("Internal error in segmentintersection():");
 
9276
    printf("  Attempt to find intersection of parallel segments.\n");
 
9277
    internalerror();
 
9278
  }
 
9279
  split = (ey * etx - ex * ety) / denom;
 
9280
  /* Create the new point. */
 
9281
  newpoint = (point) poolalloc(&points);
 
9282
  /* Interpolate its coordinate and attributes. */
 
9283
  for (i = 0; i < 2 + nextras; i++) {
 
9284
    newpoint[i] = torg[i] + split * (tdest[i] - torg[i]);
 
9285
  }
 
9286
  setpointmark(newpoint, mark(*splitshelle));
 
9287
  if (verbose > 1) {
 
9288
    printf(
 
9289
    "  Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
 
9290
           torg[0], torg[1], tdest[0], tdest[1], newpoint[0], newpoint[1]);
 
9291
  }
 
9292
  /* Insert the intersection point.  This should always succeed. */
 
9293
  success = insertsite(newpoint, splittri, splitshelle, 0, 0);
 
9294
  if (success != SUCCESSFULPOINT) {
 
9295
    printf("Internal error in segmentintersection():\n");
 
9296
    printf("  Failure to split a segment.\n");
 
9297
    internalerror();
 
9298
  }
 
9299
  if (steinerleft > 0) {
 
9300
    steinerleft--;
 
9301
  }
 
9302
  /* Inserting the point may have caused edge flips.  We wish to rediscover */
 
9303
  /*   the edge connecting endpoint1 to the new intersection point.         */
 
9304
  collinear = finddirection(splittri, endpoint1);
 
9305
  dest(*splittri, rightpoint);
 
9306
  apex(*splittri, leftpoint);
 
9307
  if ((leftpoint[0] == endpoint1[0]) && (leftpoint[1] == endpoint1[1])) {
 
9308
    onextself(*splittri);
 
9309
  } else if ((rightpoint[0] != endpoint1[0]) ||
 
9310
             (rightpoint[1] != endpoint1[1])) {
 
9311
    printf("Internal error in segmentintersection():\n");
 
9312
    printf("  Topological inconsistency after splitting a segment.\n");
 
9313
    internalerror();
 
9314
  }
 
9315
  /* `splittri' should have destination endpoint1. */
 
9316
}
 
9317
 
 
9318
/*****************************************************************************/
 
9319
/*                                                                           */
 
9320
/*  scoutsegment()   Scout the first triangle on the path from one endpoint  */
 
9321
/*                   to another, and check for completion (reaching the      */
 
9322
/*                   second endpoint), a collinear point, and the            */
 
9323
/*                   intersection of two segments.                           */
 
9324
/*                                                                           */
 
9325
/*  Returns one if the entire segment is successfully inserted, and zero if  */
 
9326
/*  the job must be finished by conformingedge() or constrainededge().       */
 
9327
/*                                                                           */
 
9328
/*  If the first triangle on the path has the second endpoint as its         */
 
9329
/*  destination or apex, a shell edge is inserted and the job is done.       */
 
9330
/*                                                                           */
 
9331
/*  If the first triangle on the path has a destination or apex that lies on */
 
9332
/*  the segment, a shell edge is inserted connecting the first endpoint to   */
 
9333
/*  the collinear point, and the search is continued from the collinear      */
 
9334
/*  point.                                                                   */
 
9335
/*                                                                           */
 
9336
/*  If the first triangle on the path has a shell edge opposite its origin,  */
 
9337
/*  then there is a segment that intersects the segment being inserted.      */
 
9338
/*  Their intersection point is inserted, splitting the shell edge.          */
 
9339
/*                                                                           */
 
9340
/*  Otherwise, return zero.                                                  */
 
9341
/*                                                                           */
 
9342
/*****************************************************************************/
 
9343
 
 
9344
int scoutsegment(
 
9345
struct triedge *searchtri,
 
9346
point endpoint2,
 
9347
int newmark)
 
9348
{
 
9349
  struct triedge crosstri;
 
9350
  struct edge crossedge;
 
9351
  point leftpoint, rightpoint;
 
9352
  point endpoint1;
 
9353
  enum finddirectionresult collinear;
 
9354
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
9355
 
 
9356
  collinear = finddirection(searchtri, endpoint2);
 
9357
  dest(*searchtri, rightpoint);
 
9358
  apex(*searchtri, leftpoint);
 
9359
  if (((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) ||
 
9360
      ((rightpoint[0] == endpoint2[0]) && (rightpoint[1] == endpoint2[1]))) {
 
9361
    /* The segment is already an edge in the mesh. */
 
9362
    if ((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) {
 
9363
      lprevself(*searchtri);
 
9364
    }
 
9365
    /* Insert a shell edge, if there isn't already one there. */
 
9366
    insertshelle(searchtri, newmark);
 
9367
    return 1;
 
9368
  } else if (collinear == LEFTCOLLINEAR) {
 
9369
    /* We've collided with a point between the segment's endpoints. */
 
9370
    /* Make the collinear point be the triangle's origin. */
 
9371
    lprevself(*searchtri);
 
9372
    insertshelle(searchtri, newmark);
 
9373
    /* Insert the remainder of the segment. */
 
9374
    return scoutsegment(searchtri, endpoint2, newmark);
 
9375
  } else if (collinear == RIGHTCOLLINEAR) {
 
9376
    /* We've collided with a point between the segment's endpoints. */
 
9377
    insertshelle(searchtri, newmark);
 
9378
    /* Make the collinear point be the triangle's origin. */
 
9379
    lnextself(*searchtri);
 
9380
    /* Insert the remainder of the segment. */
 
9381
    return scoutsegment(searchtri, endpoint2, newmark);
 
9382
  } else {
 
9383
    lnext(*searchtri, crosstri);
 
9384
    tspivot(crosstri, crossedge);
 
9385
    /* Check for a crossing segment. */
 
9386
    if (crossedge.sh == dummysh) {
 
9387
      return 0;
 
9388
    } else {
 
9389
      org(*searchtri, endpoint1);
 
9390
      /* Insert a point at the intersection. */
 
9391
      segmentintersection(&crosstri, &crossedge, endpoint2);
 
9392
      triedgecopy(crosstri, *searchtri);
 
9393
      insertshelle(searchtri, newmark);
 
9394
      /* Insert the remainder of the segment. */
 
9395
      return scoutsegment(searchtri, endpoint2, newmark);
 
9396
    }
 
9397
  }
 
9398
}
 
9399
 
 
9400
/*****************************************************************************/
 
9401
/*                                                                           */
 
9402
/*  conformingedge()   Force a segment into a conforming Delaunay            */
 
9403
/*                     triangulation by inserting a point at its midpoint,   */
 
9404
/*                     and recursively forcing in the two half-segments if   */
 
9405
/*                     necessary.                                            */
 
9406
/*                                                                           */
 
9407
/*  Generates a sequence of edges connecting `endpoint1' to `endpoint2'.     */
 
9408
/*  `newmark' is the boundary marker of the segment, assigned to each new    */
 
9409
/*  splitting point and shell edge.                                          */
 
9410
/*                                                                           */
 
9411
/*  Note that conformingedge() does not always maintain the conforming       */
 
9412
/*  Delaunay property.  Once inserted, segments are locked into place;       */
 
9413
/*  points inserted later (to force other segments in) may render these      */
 
9414
/*  fixed segments non-Delaunay.  The conforming Delaunay property will be   */
 
9415
/*  restored by enforcequality() by splitting encroached segments.           */
 
9416
/*                                                                           */
 
9417
/*****************************************************************************/
 
9418
 
 
9419
#ifndef REDUCED
 
9420
#ifndef CDT_ONLY
 
9421
 
 
9422
void conformingedge(
 
9423
point endpoint1,
 
9424
point endpoint2,
 
9425
int newmark)
 
9426
{
 
9427
  struct triedge searchtri1, searchtri2;
 
9428
  struct edge brokenshelle;
 
9429
  point newpoint;
 
9430
  point midpoint1, midpoint2;
 
9431
  enum insertsiteresult success;
 
9432
  int result1, result2;
 
9433
  int i;
 
9434
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
9435
 
 
9436
  if (verbose > 2) {
 
9437
    printf("Forcing segment into triangulation by recursive splitting:\n");
 
9438
    printf("  (%.12g, %.12g) (%.12g, %.12g)\n", endpoint1[0], endpoint1[1],
 
9439
           endpoint2[0], endpoint2[1]);
 
9440
  }
 
9441
  /* Create a new point to insert in the middle of the segment. */
 
9442
  newpoint = (point) poolalloc(&points);
 
9443
  /* Interpolate coordinates and attributes. */
 
9444
  for (i = 0; i < 2 + nextras; i++) {
 
9445
    newpoint[i] = 0.5 * (endpoint1[i] + endpoint2[i]);
 
9446
  }
 
9447
  setpointmark(newpoint, newmark);
 
9448
  /* Find a boundary triangle to search from. */
 
9449
  searchtri1.tri = (triangle *) NULL;
 
9450
  /* Attempt to insert the new point. */
 
9451
  success = insertsite(newpoint, &searchtri1, (struct edge *) NULL, 0, 0);
 
9452
  if (success == DUPLICATEPOINT) {
 
9453
    if (verbose > 2) {
 
9454
      printf("  Segment intersects existing point (%.12g, %.12g).\n",
 
9455
             newpoint[0], newpoint[1]);
 
9456
    }
 
9457
    /* Use the point that's already there. */
 
9458
    pointdealloc(newpoint);
 
9459
    org(searchtri1, newpoint);
 
9460
  } else {
 
9461
    if (success == VIOLATINGPOINT) {
 
9462
      if (verbose > 2) {
 
9463
        printf("  Two segments intersect at (%.12g, %.12g).\n",
 
9464
               newpoint[0], newpoint[1]);
 
9465
      }
 
9466
      /* By fluke, we've landed right on another segment.  Split it. */
 
9467
      tspivot(searchtri1, brokenshelle);
 
9468
      success = insertsite(newpoint, &searchtri1, &brokenshelle, 0, 0);
 
9469
      if (success != SUCCESSFULPOINT) {
 
9470
        printf("Internal error in conformingedge():\n");
 
9471
        printf("  Failure to split a segment.\n");
 
9472
        internalerror();
 
9473
      }
 
9474
    }
 
9475
    /* The point has been inserted successfully. */
 
9476
    if (steinerleft > 0) {
 
9477
      steinerleft--;
 
9478
    }
 
9479
  }
 
9480
  triedgecopy(searchtri1, searchtri2);
 
9481
  result1 = scoutsegment(&searchtri1, endpoint1, newmark);
 
9482
  result2 = scoutsegment(&searchtri2, endpoint2, newmark);
 
9483
  if (!result1) {
 
9484
    /* The origin of searchtri1 may have changed if a collision with an */
 
9485
    /*   intervening vertex on the segment occurred.                    */
 
9486
    org(searchtri1, midpoint1);
 
9487
    conformingedge(midpoint1, endpoint1, newmark);
 
9488
  }
 
9489
  if (!result2) {
 
9490
    /* The origin of searchtri2 may have changed if a collision with an */
 
9491
    /*   intervening vertex on the segment occurred.                    */
 
9492
    org(searchtri2, midpoint2);
 
9493
    conformingedge(midpoint2, endpoint2, newmark);
 
9494
  }
 
9495
}
 
9496
 
 
9497
#endif /* not CDT_ONLY */
 
9498
#endif /* not REDUCED */
 
9499
 
 
9500
/*****************************************************************************/
 
9501
/*                                                                           */
 
9502
/*  delaunayfixup()   Enforce the Delaunay condition at an edge, fanning out */
 
9503
/*                    recursively from an existing point.  Pay special       */
 
9504
/*                    attention to stacking inverted triangles.              */
 
9505
/*                                                                           */
 
9506
/*  This is a support routine for inserting segments into a constrained      */
 
9507
/*  Delaunay triangulation.                                                  */
 
9508
/*                                                                           */
 
9509
/*  The origin of fixuptri is treated as if it has just been inserted, and   */
 
9510
/*  the local Delaunay condition needs to be enforced.  It is only enforced  */
 
9511
/*  in one sector, however, that being the angular range defined by          */
 
9512
/*  fixuptri.                                                                */
 
9513
/*                                                                           */
 
9514
/*  This routine also needs to make decisions regarding the "stacking" of    */
 
9515
/*  triangles.  (Read the description of constrainededge() below before      */
 
9516
/*  reading on here, so you understand the algorithm.)  If the position of   */
 
9517
/*  the new point (the origin of fixuptri) indicates that the vertex before  */
 
9518
/*  it on the polygon is a reflex vertex, then "stack" the triangle by       */
 
9519
/*  doing nothing.  (fixuptri is an inverted triangle, which is how stacked  */
 
9520
/*  triangles are identified.)                                               */
 
9521
/*                                                                           */
 
9522
/*  Otherwise, check whether the vertex before that was a reflex vertex.     */
 
9523
/*  If so, perform an edge flip, thereby eliminating an inverted triangle    */
 
9524
/*  (popping it off the stack).  The edge flip may result in the creation    */
 
9525
/*  of a new inverted triangle, depending on whether or not the new vertex   */
 
9526
/*  is visible to the vertex three edges behind on the polygon.              */
 
9527
/*                                                                           */
 
9528
/*  If neither of the two vertices behind the new vertex are reflex          */
 
9529
/*  vertices, fixuptri and fartri, the triangle opposite it, are not         */
 
9530
/*  inverted; hence, ensure that the edge between them is locally Delaunay.  */
 
9531
/*                                                                           */
 
9532
/*  `leftside' indicates whether or not fixuptri is to the left of the       */
 
9533
/*  segment being inserted.  (Imagine that the segment is pointing up from   */
 
9534
/*  endpoint1 to endpoint2.)                                                 */
 
9535
/*                                                                           */
 
9536
/*****************************************************************************/
 
9537
 
 
9538
void delaunayfixup(
 
9539
struct triedge *fixuptri,
 
9540
int leftside)
 
9541
{
 
9542
  struct triedge neartri;
 
9543
  struct triedge fartri;
 
9544
  struct edge faredge;
 
9545
  point nearpoint, leftpoint, rightpoint, farpoint;
 
9546
  triangle ptr;                         /* Temporary variable used by sym(). */
 
9547
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
9548
 
 
9549
  lnext(*fixuptri, neartri);
 
9550
  sym(neartri, fartri);
 
9551
  /* Check if the edge opposite the origin of fixuptri can be flipped. */
 
9552
  if (fartri.tri == dummytri) {
 
9553
    return;
 
9554
  }
 
9555
  tspivot(neartri, faredge);
 
9556
  if (faredge.sh != dummysh) {
 
9557
    return;
 
9558
  }
 
9559
  /* Find all the relevant vertices. */
 
9560
  apex(neartri, nearpoint);
 
9561
  org(neartri, leftpoint);
 
9562
  dest(neartri, rightpoint);
 
9563
  apex(fartri, farpoint);
 
9564
  /* Check whether the previous polygon vertex is a reflex vertex. */
 
9565
  if (leftside) {
 
9566
    if (counterclockwise(nearpoint, leftpoint, farpoint) <= 0.0) {
 
9567
      /* leftpoint is a reflex vertex too.  Nothing can */
 
9568
      /*   be done until a convex section is found.     */
 
9569
      return;
 
9570
    }
 
9571
  } else {
 
9572
    if (counterclockwise(farpoint, rightpoint, nearpoint) <= 0.0) {
 
9573
      /* rightpoint is a reflex vertex too.  Nothing can */
 
9574
      /*   be done until a convex section is found.      */
 
9575
      return;
 
9576
    }
 
9577
  }
 
9578
  if (counterclockwise(rightpoint, leftpoint, farpoint) > 0.0) {
 
9579
    /* fartri is not an inverted triangle, and farpoint is not a reflex */
 
9580
    /*   vertex.  As there are no reflex vertices, fixuptri isn't an    */
 
9581
    /*   inverted triangle, either.  Hence, test the edge between the   */
 
9582
    /*   triangles to ensure it is locally Delaunay.                    */
 
9583
    if (incircle(leftpoint, farpoint, rightpoint, nearpoint) <= 0.0) {
 
9584
      return;
 
9585
    }
 
9586
    /* Not locally Delaunay; go on to an edge flip. */
 
9587
  }        /* else fartri is inverted; remove it from the stack by flipping. */
 
9588
  flip(&neartri);
 
9589
  lprevself(*fixuptri);    /* Restore the origin of fixuptri after the flip. */
 
9590
  /* Recursively process the two triangles that result from the flip. */
 
9591
  delaunayfixup(fixuptri, leftside);
 
9592
  delaunayfixup(&fartri, leftside);
 
9593
}
 
9594
 
 
9595
/*****************************************************************************/
 
9596
/*                                                                           */
 
9597
/*  constrainededge()   Force a segment into a constrained Delaunay          */
 
9598
/*                      triangulation by deleting the triangles it           */
 
9599
/*                      intersects, and triangulating the polygons that      */
 
9600
/*                      form on each side of it.                             */
 
9601
/*                                                                           */
 
9602
/*  Generates a single edge connecting `endpoint1' to `endpoint2'.  The      */
 
9603
/*  triangle `starttri' has `endpoint1' as its origin.  `newmark' is the     */
 
9604
/*  boundary marker of the segment.                                          */
 
9605
/*                                                                           */
 
9606
/*  To insert a segment, every triangle whose interior intersects the        */
 
9607
/*  segment is deleted.  The union of these deleted triangles is a polygon   */
 
9608
/*  (which is not necessarily monotone, but is close enough), which is       */
 
9609
/*  divided into two polygons by the new segment.  This routine's task is    */
 
9610
/*  to generate the Delaunay triangulation of these two polygons.            */
 
9611
/*                                                                           */
 
9612
/*  You might think of this routine's behavior as a two-step process.  The   */
 
9613
/*  first step is to walk from endpoint1 to endpoint2, flipping each edge    */
 
9614
/*  encountered.  This step creates a fan of edges connected to endpoint1,   */
 
9615
/*  including the desired edge to endpoint2.  The second step enforces the   */
 
9616
/*  Delaunay condition on each side of the segment in an incremental manner: */
 
9617
/*  proceeding along the polygon from endpoint1 to endpoint2 (this is done   */
 
9618
/*  independently on each side of the segment), each vertex is "enforced"    */
 
9619
/*  as if it had just been inserted, but affecting only the previous         */
 
9620
/*  vertices.  The result is the same as if the vertices had been inserted   */
 
9621
/*  in the order they appear on the polygon, so the result is Delaunay.      */
 
9622
/*                                                                           */
 
9623
/*  In truth, constrainededge() interleaves these two steps.  The procedure  */
 
9624
/*  walks from endpoint1 to endpoint2, and each time an edge is encountered  */
 
9625
/*  and flipped, the newly exposed vertex (at the far end of the flipped     */
 
9626
/*  edge) is "enforced" upon the previously flipped edges, usually affecting */
 
9627
/*  only one side of the polygon (depending upon which side of the segment   */
 
9628
/*  the vertex falls on).                                                    */
 
9629
/*                                                                           */
 
9630
/*  The algorithm is complicated by the need to handle polygons that are not */
 
9631
/*  convex.  Although the polygon is not necessarily monotone, it can be     */
 
9632
/*  triangulated in a manner similar to the stack-based algorithms for       */
 
9633
/*  monotone polygons.  For each reflex vertex (local concavity) of the      */
 
9634
/*  polygon, there will be an inverted triangle formed by one of the edge    */
 
9635
/*  flips.  (An inverted triangle is one with negative area - that is, its   */
 
9636
/*  vertices are arranged in clockwise order - and is best thought of as a   */
 
9637
/*  wrinkle in the fabric of the mesh.)  Each inverted triangle can be       */
 
9638
/*  thought of as a reflex vertex pushed on the stack, waiting to be fixed   */
 
9639
/*  later.                                                                   */
 
9640
/*                                                                           */
 
9641
/*  A reflex vertex is popped from the stack when a vertex is inserted that  */
 
9642
/*  is visible to the reflex vertex.  (However, if the vertex behind the     */
 
9643
/*  reflex vertex is not visible to the reflex vertex, a new inverted        */
 
9644
/*  triangle will take its place on the stack.)  These details are handled   */
 
9645
/*  by the delaunayfixup() routine above.                                    */
 
9646
/*                                                                           */
 
9647
/*****************************************************************************/
 
9648
 
 
9649
void constrainededge(
 
9650
struct triedge *starttri,
 
9651
point endpoint2,
 
9652
int newmark)
 
9653
{
 
9654
  struct triedge fixuptri, fixuptri2;
 
9655
  struct edge fixupedge;
 
9656
  point endpoint1;
 
9657
  point farpoint;
 
9658
  REAL area;
 
9659
  int collision;
 
9660
  int done;
 
9661
  triangle ptr;             /* Temporary variable used by sym() and oprev(). */
 
9662
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
9663
 
 
9664
  org(*starttri, endpoint1);
 
9665
  lnext(*starttri, fixuptri);
 
9666
  flip(&fixuptri);
 
9667
  /* `collision' indicates whether we have found a point directly */
 
9668
  /*   between endpoint1 and endpoint2.                           */
 
9669
  collision = 0;
 
9670
  done = 0;
 
9671
  do {
 
9672
    org(fixuptri, farpoint);
 
9673
    /* `farpoint' is the extreme point of the polygon we are "digging" */
 
9674
    /*   to get from endpoint1 to endpoint2.                           */
 
9675
    if ((farpoint[0] == endpoint2[0]) && (farpoint[1] == endpoint2[1])) {
 
9676
      oprev(fixuptri, fixuptri2);
 
9677
      /* Enforce the Delaunay condition around endpoint2. */
 
9678
      delaunayfixup(&fixuptri, 0);
 
9679
      delaunayfixup(&fixuptri2, 1);
 
9680
      done = 1;
 
9681
    } else {
 
9682
      /* Check whether farpoint is to the left or right of the segment */
 
9683
      /*   being inserted, to decide which edge of fixuptri to dig     */
 
9684
      /*   through next.                                               */
 
9685
      area = counterclockwise(endpoint1, endpoint2, farpoint);
 
9686
      if (area == 0.0) {
 
9687
        /* We've collided with a point between endpoint1 and endpoint2. */
 
9688
        collision = 1;
 
9689
        oprev(fixuptri, fixuptri2);
 
9690
        /* Enforce the Delaunay condition around farpoint. */
 
9691
        delaunayfixup(&fixuptri, 0);
 
9692
        delaunayfixup(&fixuptri2, 1);
 
9693
        done = 1;
 
9694
      } else {
 
9695
        if (area > 0.0) {         /* farpoint is to the left of the segment. */
 
9696
          oprev(fixuptri, fixuptri2);
 
9697
          /* Enforce the Delaunay condition around farpoint, on the */
 
9698
          /*   left side of the segment only.                       */
 
9699
          delaunayfixup(&fixuptri2, 1);
 
9700
          /* Flip the edge that crosses the segment.  After the edge is */
 
9701
          /*   flipped, one of its endpoints is the fan vertex, and the */
 
9702
          /*   destination of fixuptri is the fan vertex.               */
 
9703
          lprevself(fixuptri);
 
9704
        } else {                 /* farpoint is to the right of the segment. */
 
9705
          delaunayfixup(&fixuptri, 0);
 
9706
          /* Flip the edge that crosses the segment.  After the edge is */
 
9707
          /*   flipped, one of its endpoints is the fan vertex, and the */
 
9708
          /*   destination of fixuptri is the fan vertex.               */
 
9709
          oprevself(fixuptri);
 
9710
        }
 
9711
        /* Check for two intersecting segments. */
 
9712
        tspivot(fixuptri, fixupedge);
 
9713
        if (fixupedge.sh == dummysh) {
 
9714
          flip(&fixuptri);   /* May create an inverted triangle on the left. */
 
9715
        } else {
 
9716
          /* We've collided with a segment between endpoint1 and endpoint2. */
 
9717
          collision = 1;
 
9718
          /* Insert a point at the intersection. */
 
9719
          segmentintersection(&fixuptri, &fixupedge, endpoint2);
 
9720
          done = 1;
 
9721
        }
 
9722
      }
 
9723
    }
 
9724
  } while (!done);
 
9725
  /* Insert a shell edge to make the segment permanent. */
 
9726
  insertshelle(&fixuptri, newmark);
 
9727
  /* If there was a collision with an interceding vertex, install another */
 
9728
  /*   segment connecting that vertex with endpoint2.                     */
 
9729
  if (collision) {
 
9730
    /* Insert the remainder of the segment. */
 
9731
    if (!scoutsegment(&fixuptri, endpoint2, newmark)) {
 
9732
      constrainededge(&fixuptri, endpoint2, newmark);
 
9733
    }
 
9734
  }
 
9735
}
 
9736
 
 
9737
/*****************************************************************************/
 
9738
/*                                                                           */
 
9739
/*  insertsegment()   Insert a PSLG segment into a triangulation.            */
 
9740
/*                                                                           */
 
9741
/*****************************************************************************/
 
9742
 
 
9743
void insertsegment(
 
9744
point endpoint1,
 
9745
point endpoint2,
 
9746
int newmark)
 
9747
{
 
9748
  struct triedge searchtri1, searchtri2;
 
9749
  triangle encodedtri;
 
9750
  point checkpoint;
 
9751
  triangle ptr;                         /* Temporary variable used by sym(). */
 
9752
 
 
9753
  if (verbose > 1) {
 
9754
    printf("  Connecting (%.12g, %.12g) to (%.12g, %.12g).\n",
 
9755
           endpoint1[0], endpoint1[1], endpoint2[0], endpoint2[1]);
 
9756
  }
 
9757
 
 
9758
  /* Find a triangle whose origin is the segment's first endpoint. */
 
9759
  checkpoint = (point) NULL;
 
9760
  encodedtri = point2tri(endpoint1);
 
9761
  if (encodedtri != (triangle) NULL) {
 
9762
    decode(encodedtri, searchtri1);
 
9763
    org(searchtri1, checkpoint);
 
9764
  }
 
9765
  if (checkpoint != endpoint1) {
 
9766
    /* Find a boundary triangle to search from. */
 
9767
    searchtri1.tri = dummytri;
 
9768
    searchtri1.orient = 0;
 
9769
    symself(searchtri1);
 
9770
    /* Search for the segment's first endpoint by point location. */
 
9771
    if (locate(endpoint1, &searchtri1) != ONVERTEX) {
 
9772
      printf(
 
9773
        "Internal error in insertsegment():  Unable to locate PSLG point\n");
 
9774
      printf("  (%.12g, %.12g) in triangulation.\n",
 
9775
             endpoint1[0], endpoint1[1]);
 
9776
      internalerror();
 
9777
    }
 
9778
  }
 
9779
  /* Remember this triangle to improve subsequent point location. */
 
9780
  triedgecopy(searchtri1, recenttri);
 
9781
  /* Scout the beginnings of a path from the first endpoint */
 
9782
  /*   toward the second.                                   */
 
9783
  if (scoutsegment(&searchtri1, endpoint2, newmark)) {
 
9784
    /* The segment was easily inserted. */
 
9785
    return;
 
9786
  }
 
9787
  /* The first endpoint may have changed if a collision with an intervening */
 
9788
  /*   vertex on the segment occurred.                                      */
 
9789
  org(searchtri1, endpoint1);
 
9790
 
 
9791
  /* Find a triangle whose origin is the segment's second endpoint. */
 
9792
  checkpoint = (point) NULL;
 
9793
  encodedtri = point2tri(endpoint2);
 
9794
  if (encodedtri != (triangle) NULL) {
 
9795
    decode(encodedtri, searchtri2);
 
9796
    org(searchtri2, checkpoint);
 
9797
  }
 
9798
  if (checkpoint != endpoint2) {
 
9799
    /* Find a boundary triangle to search from. */
 
9800
    searchtri2.tri = dummytri;
 
9801
    searchtri2.orient = 0;
 
9802
    symself(searchtri2);
 
9803
    /* Search for the segment's second endpoint by point location. */
 
9804
    if (locate(endpoint2, &searchtri2) != ONVERTEX) {
 
9805
      printf(
 
9806
        "Internal error in insertsegment():  Unable to locate PSLG point\n");
 
9807
      printf("  (%.12g, %.12g) in triangulation.\n",
 
9808
             endpoint2[0], endpoint2[1]);
 
9809
      internalerror();
 
9810
    }
 
9811
  }
 
9812
  /* Remember this triangle to improve subsequent point location. */
 
9813
  triedgecopy(searchtri2, recenttri);
 
9814
  /* Scout the beginnings of a path from the second endpoint */
 
9815
  /*   toward the first.                                     */
 
9816
  if (scoutsegment(&searchtri2, endpoint1, newmark)) {
 
9817
    /* The segment was easily inserted. */
 
9818
    return;
 
9819
  }
 
9820
  /* The second endpoint may have changed if a collision with an intervening */
 
9821
  /*   vertex on the segment occurred.                                       */
 
9822
  org(searchtri2, endpoint2);
 
9823
 
 
9824
#ifndef REDUCED
 
9825
#ifndef CDT_ONLY
 
9826
  if (splitseg) {
 
9827
    /* Insert vertices to force the segment into the triangulation. */
 
9828
    conformingedge(endpoint1, endpoint2, newmark);
 
9829
  } else {
 
9830
#endif /* not CDT_ONLY */
 
9831
#endif /* not REDUCED */
 
9832
    /* Insert the segment directly into the triangulation. */
 
9833
    constrainededge(&searchtri1, endpoint2, newmark);
 
9834
#ifndef REDUCED
 
9835
#ifndef CDT_ONLY
 
9836
  }
 
9837
#endif /* not CDT_ONLY */
 
9838
#endif /* not REDUCED */
 
9839
}
 
9840
 
 
9841
/*****************************************************************************/
 
9842
/*                                                                           */
 
9843
/*  markhull()   Cover the convex hull of a triangulation with shell edges.  */
 
9844
/*                                                                           */
 
9845
/*****************************************************************************/
 
9846
 
 
9847
void markhull()
 
9848
{
 
9849
  struct triedge hulltri;
 
9850
  struct triedge nexttri;
 
9851
  struct triedge starttri;
 
9852
  triangle ptr;             /* Temporary variable used by sym() and oprev(). */
 
9853
 
 
9854
  /* Find a triangle handle on the hull. */
 
9855
  hulltri.tri = dummytri;
 
9856
  hulltri.orient = 0;
 
9857
  symself(hulltri);
 
9858
  /* Remember where we started so we know when to stop. */
 
9859
  triedgecopy(hulltri, starttri);
 
9860
  /* Go once counterclockwise around the convex hull. */
 
9861
  do {
 
9862
    /* Create a shell edge if there isn't already one here. */
 
9863
    insertshelle(&hulltri, 1);
 
9864
    /* To find the next hull edge, go clockwise around the next vertex. */
 
9865
    lnextself(hulltri);
 
9866
    oprev(hulltri, nexttri);
 
9867
    while (nexttri.tri != dummytri) {
 
9868
      triedgecopy(nexttri, hulltri);
 
9869
      oprev(hulltri, nexttri);
 
9870
    }
 
9871
  } while (!triedgeequal(hulltri, starttri));
 
9872
}
 
9873
 
 
9874
/*****************************************************************************/
 
9875
/*                                                                           */
 
9876
/*  formskeleton()   Create the shell edges of a triangulation, including    */
 
9877
/*                   PSLG edges and edges on the convex hull.                */
 
9878
/*                                                                           */
 
9879
/*  The PSLG edges are read from a .poly file.  The return value is the      */
 
9880
/*  number of segments in the file.                                          */
 
9881
/*                                                                           */
 
9882
/*****************************************************************************/
 
9883
 
 
9884
#ifdef TRILIBRARY
 
9885
 
 
9886
int formskeleton(
 
9887
int *segmentlist,
 
9888
int *segmentmarkerlist,
 
9889
int numberofsegments)
 
9890
 
 
9891
#else /* not TRILIBRARY */
 
9892
 
 
9893
int formskeleton(polyfile, polyfilename)
 
9894
FILE *polyfile;
 
9895
char *polyfilename;
 
9896
 
 
9897
#endif /* not TRILIBRARY */
 
9898
 
 
9899
{
 
9900
#ifdef TRILIBRARY
 
9901
  char polyfilename[6];
 
9902
  int index;
 
9903
#else /* not TRILIBRARY */
 
9904
  char inputline[INPUTLINESIZE];
 
9905
  char *stringptr;
 
9906
#endif /* not TRILIBRARY */
 
9907
  point endpoint1, endpoint2;
 
9908
  int segments;
 
9909
  int segmentmarkers;
 
9910
  int end1, end2;
 
9911
  int boundmarker;
 
9912
  int i;
 
9913
 
 
9914
  if (poly) {
 
9915
    if (!quiet) {
 
9916
      printf("Inserting segments into Delaunay triangulation.\n");
 
9917
    }
 
9918
#ifdef TRILIBRARY
 
9919
    strcpy(polyfilename, "input");
 
9920
    segments = numberofsegments;
 
9921
    segmentmarkers = segmentmarkerlist != (int *) NULL;
 
9922
    index = 0;
 
9923
#else /* not TRILIBRARY */
 
9924
    /* Read the segments from a .poly file. */
 
9925
    /* Read number of segments and number of boundary markers. */
 
9926
    stringptr = readline(inputline, polyfile, polyfilename);
 
9927
    segments = (int) strtol (stringptr, &stringptr, 0);
 
9928
    stringptr = findfield(stringptr);
 
9929
    if (*stringptr == '\0') {
 
9930
      segmentmarkers = 0;
 
9931
    } else {
 
9932
      segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
 
9933
    }
 
9934
#endif /* not TRILIBRARY */
 
9935
    /* If segments are to be inserted, compute a mapping */
 
9936
    /*   from points to triangles.                       */
 
9937
    if (segments > 0) {
 
9938
      if (verbose) {
 
9939
        printf("  Inserting PSLG segments.\n");
 
9940
      }
 
9941
      makepointmap();
 
9942
    }
 
9943
 
 
9944
    boundmarker = 0;
 
9945
    /* Read and insert the segments. */
 
9946
    for (i = 1; i <= segments; i++) {
 
9947
#ifdef TRILIBRARY
 
9948
      end1 = segmentlist[index++];
 
9949
      end2 = segmentlist[index++];
 
9950
      if (segmentmarkers) {
 
9951
        boundmarker = segmentmarkerlist[i - 1];
 
9952
      }
 
9953
#else /* not TRILIBRARY */
 
9954
      stringptr = readline(inputline, polyfile, inpolyfilename);
 
9955
      stringptr = findfield(stringptr);
 
9956
      if (*stringptr == '\0') {
 
9957
        printf("Error:  Segment %d has no endpoints in %s.\n", i,
 
9958
               polyfilename);
 
9959
        exit(1);
 
9960
      } else {
 
9961
        end1 = (int) strtol (stringptr, &stringptr, 0);
 
9962
      }
 
9963
      stringptr = findfield(stringptr);
 
9964
      if (*stringptr == '\0') {
 
9965
        printf("Error:  Segment %d is missing its second endpoint in %s.\n", i,
 
9966
               polyfilename);
 
9967
        exit(1);
 
9968
      } else {
 
9969
        end2 = (int) strtol (stringptr, &stringptr, 0);
 
9970
      }
 
9971
      if (segmentmarkers) {
 
9972
        stringptr = findfield(stringptr);
 
9973
        if (*stringptr == '\0') {
 
9974
          boundmarker = 0;
 
9975
        } else {
 
9976
          boundmarker = (int) strtol (stringptr, &stringptr, 0);
 
9977
        }
 
9978
      }
 
9979
#endif /* not TRILIBRARY */
 
9980
      if ((end1 < firstnumber) || (end1 >= firstnumber + inpoints)) {
 
9981
        if (!quiet) {
 
9982
          printf("Warning:  Invalid first endpoint of segment %d in %s.\n", i,
 
9983
                 polyfilename);
 
9984
        }
 
9985
      } else if ((end2 < firstnumber) || (end2 >= firstnumber + inpoints)) {
 
9986
        if (!quiet) {
 
9987
          printf("Warning:  Invalid second endpoint of segment %d in %s.\n", i,
 
9988
                 polyfilename);
 
9989
        }
 
9990
      } else {
 
9991
        endpoint1 = getpoint(end1);
 
9992
        endpoint2 = getpoint(end2);
 
9993
        if ((endpoint1[0] == endpoint2[0]) && (endpoint1[1] == endpoint2[1])) {
 
9994
          if (!quiet) {
 
9995
            printf("Warning:  Endpoints of segment %d are coincident in %s.\n",
 
9996
                   i, polyfilename);
 
9997
          }
 
9998
        } else {
 
9999
          insertsegment(endpoint1, endpoint2, boundmarker);
 
10000
        }
 
10001
      }
 
10002
    }
 
10003
  } else {
 
10004
    segments = 0;
 
10005
  }
 
10006
  if (convex || !poly) {
 
10007
    /* Enclose the convex hull with shell edges. */
 
10008
    if (verbose) {
 
10009
      printf("  Enclosing convex hull with segments.\n");
 
10010
    }
 
10011
    markhull();
 
10012
  }
 
10013
  return segments;
 
10014
}
 
10015
 
 
10016
/**                                                                         **/
 
10017
/**                                                                         **/
 
10018
/********* Segment (shell edge) insertion ends here                  *********/
 
10019
 
 
10020
/********* Carving out holes and concavities begins here             *********/
 
10021
/**                                                                         **/
 
10022
/**                                                                         **/
 
10023
 
 
10024
/*****************************************************************************/
 
10025
/*                                                                           */
 
10026
/*  infecthull()   Virally infect all of the triangles of the convex hull    */
 
10027
/*                 that are not protected by shell edges.  Where there are   */
 
10028
/*                 shell edges, set boundary markers as appropriate.         */
 
10029
/*                                                                           */
 
10030
/*****************************************************************************/
 
10031
 
 
10032
void infecthull()
 
10033
{
 
10034
  struct triedge hulltri;
 
10035
  struct triedge nexttri;
 
10036
  struct triedge starttri;
 
10037
  struct edge hulledge;
 
10038
  triangle **deadtri;
 
10039
  point horg, hdest;
 
10040
  triangle ptr;                         /* Temporary variable used by sym(). */
 
10041
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
10042
 
 
10043
  if (verbose) {
 
10044
    printf("  Marking concavities (external triangles) for elimination.\n");
 
10045
  }
 
10046
  /* Find a triangle handle on the hull. */
 
10047
  hulltri.tri = dummytri;
 
10048
  hulltri.orient = 0;
 
10049
  symself(hulltri);
 
10050
  /* Remember where we started so we know when to stop. */
 
10051
  triedgecopy(hulltri, starttri);
 
10052
  /* Go once counterclockwise around the convex hull. */
 
10053
  do {
 
10054
    /* Ignore triangles that are already infected. */
 
10055
    if (!infected(hulltri)) {
 
10056
      /* Is the triangle protected by a shell edge? */
 
10057
      tspivot(hulltri, hulledge);
 
10058
      if (hulledge.sh == dummysh) {
 
10059
        /* The triangle is not protected; infect it. */
 
10060
        infect(hulltri);
 
10061
        deadtri = (triangle **) poolalloc(&viri);
 
10062
        *deadtri = hulltri.tri;
 
10063
      } else {
 
10064
        /* The triangle is protected; set boundary markers if appropriate. */
 
10065
        if (mark(hulledge) == 0) {
 
10066
          setmark(hulledge, 1);
 
10067
          org(hulltri, horg);
 
10068
          dest(hulltri, hdest);
 
10069
          if (pointmark(horg) == 0) {
 
10070
            setpointmark(horg, 1);
 
10071
          }
 
10072
          if (pointmark(hdest) == 0) {
 
10073
            setpointmark(hdest, 1);
 
10074
          }
 
10075
        }
 
10076
      }
 
10077
    }
 
10078
    /* To find the next hull edge, go clockwise around the next vertex. */
 
10079
    lnextself(hulltri);
 
10080
    oprev(hulltri, nexttri);
 
10081
    while (nexttri.tri != dummytri) {
 
10082
      triedgecopy(nexttri, hulltri);
 
10083
      oprev(hulltri, nexttri);
 
10084
    }
 
10085
  } while (!triedgeequal(hulltri, starttri));
 
10086
}
 
10087
 
 
10088
/*****************************************************************************/
 
10089
/*                                                                           */
 
10090
/*  plague()   Spread the virus from all infected triangles to any neighbors */
 
10091
/*             not protected by shell edges.  Delete all infected triangles. */
 
10092
/*                                                                           */
 
10093
/*  This is the procedure that actually creates holes and concavities.       */
 
10094
/*                                                                           */
 
10095
/*  This procedure operates in two phases.  The first phase identifies all   */
 
10096
/*  the triangles that will die, and marks them as infected.  They are       */
 
10097
/*  marked to ensure that each triangle is added to the virus pool only      */
 
10098
/*  once, so the procedure will terminate.                                   */
 
10099
/*                                                                           */
 
10100
/*  The second phase actually eliminates the infected triangles.  It also    */
 
10101
/*  eliminates orphaned points.                                              */
 
10102
/*                                                                           */
 
10103
/*****************************************************************************/
 
10104
 
 
10105
void plague()
 
10106
{
 
10107
  struct triedge testtri;
 
10108
  struct triedge neighbor;
 
10109
  triangle **virusloop;
 
10110
  triangle **deadtri;
 
10111
  struct edge neighborshelle;
 
10112
  point testpoint;
 
10113
  point norg, ndest;
 
10114
  point deadorg, deaddest, deadapex;
 
10115
  int killorg;
 
10116
  triangle ptr;             /* Temporary variable used by sym() and onext(). */
 
10117
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
10118
 
 
10119
  if (verbose) {
 
10120
    printf("  Marking neighbors of marked triangles.\n");
 
10121
  }
 
10122
  /* Loop through all the infected triangles, spreading the virus to */
 
10123
  /*   their neighbors, then to their neighbors' neighbors.          */
 
10124
  traversalinit(&viri);
 
10125
  virusloop = (triangle **) traverse(&viri);
 
10126
  while (virusloop != (triangle **) NULL) {
 
10127
    testtri.tri = *virusloop;
 
10128
    /* A triangle is marked as infected by messing with one of its shell */
 
10129
    /*   edges, setting it to an illegal value.  Hence, we have to       */
 
10130
    /*   temporarily uninfect this triangle so that we can examine its   */
 
10131
    /*   adjacent shell edges.                                           */
 
10132
    uninfect(testtri);
 
10133
    if (verbose > 2) {
 
10134
      /* Assign the triangle an orientation for convenience in */
 
10135
      /*   checking its points.                                */
 
10136
      testtri.orient = 0;
 
10137
      org(testtri, deadorg);
 
10138
      dest(testtri, deaddest);
 
10139
      apex(testtri, deadapex);
 
10140
      printf("    Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
 
10141
             deadorg[0], deadorg[1], deaddest[0], deaddest[1],
 
10142
             deadapex[0], deadapex[1]);
 
10143
    }
 
10144
    /* Check each of the triangle's three neighbors. */
 
10145
    for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
 
10146
      /* Find the neighbor. */
 
10147
      sym(testtri, neighbor);
 
10148
      /* Check for a shell between the triangle and its neighbor. */
 
10149
      tspivot(testtri, neighborshelle);
 
10150
      /* Check if the neighbor is nonexistent or already infected. */
 
10151
      if ((neighbor.tri == dummytri) || infected(neighbor)) {
 
10152
        if (neighborshelle.sh != dummysh) {
 
10153
          /* There is a shell edge separating the triangle from its */
 
10154
          /*   neighbor, but both triangles are dying, so the shell */
 
10155
          /*   edge dies too.                                       */
 
10156
          shelledealloc(neighborshelle.sh);
 
10157
          if (neighbor.tri != dummytri) {
 
10158
            /* Make sure the shell edge doesn't get deallocated again */
 
10159
            /*   later when the infected neighbor is visited.         */
 
10160
            uninfect(neighbor);
 
10161
            tsdissolve(neighbor);
 
10162
            infect(neighbor);
 
10163
          }
 
10164
        }
 
10165
      } else {                   /* The neighbor exists and is not infected. */
 
10166
        if (neighborshelle.sh == dummysh) {
 
10167
          /* There is no shell edge protecting the neighbor, so */
 
10168
          /*   the neighbor becomes infected.                   */
 
10169
          if (verbose > 2) {
 
10170
            org(neighbor, deadorg);
 
10171
            dest(neighbor, deaddest);
 
10172
            apex(neighbor, deadapex);
 
10173
            printf(
 
10174
              "    Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
 
10175
                   deadorg[0], deadorg[1], deaddest[0], deaddest[1],
 
10176
                   deadapex[0], deadapex[1]);
 
10177
          }
 
10178
          infect(neighbor);
 
10179
          /* Ensure that the neighbor's neighbors will be infected. */
 
10180
          deadtri = (triangle **) poolalloc(&viri);
 
10181
          *deadtri = neighbor.tri;
 
10182
        } else {               /* The neighbor is protected by a shell edge. */
 
10183
          /* Remove this triangle from the shell edge. */
 
10184
          stdissolve(neighborshelle);
 
10185
          /* The shell edge becomes a boundary.  Set markers accordingly. */
 
10186
          if (mark(neighborshelle) == 0) {
 
10187
            setmark(neighborshelle, 1);
 
10188
          }
 
10189
          org(neighbor, norg);
 
10190
          dest(neighbor, ndest);
 
10191
          if (pointmark(norg) == 0) {
 
10192
            setpointmark(norg, 1);
 
10193
          }
 
10194
          if (pointmark(ndest) == 0) {
 
10195
            setpointmark(ndest, 1);
 
10196
          }
 
10197
        }
 
10198
      }
 
10199
    }
 
10200
    /* Remark the triangle as infected, so it doesn't get added to the */
 
10201
    /*   virus pool again.                                             */
 
10202
    infect(testtri);
 
10203
    virusloop = (triangle **) traverse(&viri);
 
10204
  }
 
10205
 
 
10206
  if (verbose) {
 
10207
    printf("  Deleting marked triangles.\n");
 
10208
  }
 
10209
  traversalinit(&viri);
 
10210
  virusloop = (triangle **) traverse(&viri);
 
10211
  while (virusloop != (triangle **) NULL) {
 
10212
    testtri.tri = *virusloop;
 
10213
 
 
10214
    /* Check each of the three corners of the triangle for elimination. */
 
10215
    /*   This is done by walking around each point, checking if it is   */
 
10216
    /*   still connected to at least one live triangle.                 */
 
10217
    for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
 
10218
      org(testtri, testpoint);
 
10219
      /* Check if the point has already been tested. */
 
10220
      if (testpoint != (point) NULL) {
 
10221
        killorg = 1;
 
10222
        /* Mark the corner of the triangle as having been tested. */
 
10223
        setorg(testtri, NULL);
 
10224
        /* Walk counterclockwise about the point. */
 
10225
        onext(testtri, neighbor);
 
10226
        /* Stop upon reaching a boundary or the starting triangle. */
 
10227
        while ((neighbor.tri != dummytri)
 
10228
               && (!triedgeequal(neighbor, testtri))) {
 
10229
          if (infected(neighbor)) {
 
10230
            /* Mark the corner of this triangle as having been tested. */
 
10231
            setorg(neighbor, NULL);
 
10232
          } else {
 
10233
            /* A live triangle.  The point survives. */
 
10234
            killorg = 0;
 
10235
          }
 
10236
          /* Walk counterclockwise about the point. */
 
10237
          onextself(neighbor);
 
10238
        }
 
10239
        /* If we reached a boundary, we must walk clockwise as well. */
 
10240
        if (neighbor.tri == dummytri) {
 
10241
          /* Walk clockwise about the point. */
 
10242
          oprev(testtri, neighbor);
 
10243
          /* Stop upon reaching a boundary. */
 
10244
          while (neighbor.tri != dummytri) {
 
10245
            if (infected(neighbor)) {
 
10246
            /* Mark the corner of this triangle as having been tested. */
 
10247
              setorg(neighbor, NULL);
 
10248
            } else {
 
10249
              /* A live triangle.  The point survives. */
 
10250
              killorg = 0;
 
10251
            }
 
10252
            /* Walk clockwise about the point. */
 
10253
            oprevself(neighbor);
 
10254
          }
 
10255
        }
 
10256
        if (killorg) {
 
10257
          if (verbose > 1) {
 
10258
            printf("    Deleting point (%.12g, %.12g)\n",
 
10259
                   testpoint[0], testpoint[1]);
 
10260
          }
 
10261
          pointdealloc(testpoint);
 
10262
        }
 
10263
      }
 
10264
    }
 
10265
 
 
10266
    /* Record changes in the number of boundary edges, and disconnect */
 
10267
    /*   dead triangles from their neighbors.                         */
 
10268
    for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
 
10269
      sym(testtri, neighbor);
 
10270
      if (neighbor.tri == dummytri) {
 
10271
        /* There is no neighboring triangle on this edge, so this edge    */
 
10272
        /*   is a boundary edge.  This triangle is being deleted, so this */
 
10273
        /*   boundary edge is deleted.                                    */
 
10274
        hullsize--;
 
10275
      } else {
 
10276
        /* Disconnect the triangle from its neighbor. */
 
10277
        dissolve(neighbor);
 
10278
        /* There is a neighboring triangle on this edge, so this edge */
 
10279
        /*   becomes a boundary edge when this triangle is deleted.   */
 
10280
        hullsize++;
 
10281
      }
 
10282
    }
 
10283
    /* Return the dead triangle to the pool of triangles. */
 
10284
    triangledealloc(testtri.tri);
 
10285
    virusloop = (triangle **) traverse(&viri);
 
10286
  }
 
10287
  /* Empty the virus pool. */
 
10288
  poolrestart(&viri);
 
10289
}
 
10290
 
 
10291
/*****************************************************************************/
 
10292
/*                                                                           */
 
10293
/*  regionplague()   Spread regional attributes and/or area constraints      */
 
10294
/*                   (from a .poly file) throughout the mesh.                */
 
10295
/*                                                                           */
 
10296
/*  This procedure operates in two phases.  The first phase spreads an       */
 
10297
/*  attribute and/or an area constraint through a (segment-bounded) region.  */
 
10298
/*  The triangles are marked to ensure that each triangle is added to the    */
 
10299
/*  virus pool only once, so the procedure will terminate.                   */
 
10300
/*                                                                           */
 
10301
/*  The second phase uninfects all infected triangles, returning them to     */
 
10302
/*  normal.                                                                  */
 
10303
/*                                                                           */
 
10304
/*****************************************************************************/
 
10305
 
 
10306
void regionplague(
 
10307
REAL attribute,
 
10308
REAL area)
 
10309
{
 
10310
  struct triedge testtri;
 
10311
  struct triedge neighbor;
 
10312
  triangle **virusloop;
 
10313
  triangle **regiontri;
 
10314
  struct edge neighborshelle;
 
10315
  point regionorg, regiondest, regionapex;
 
10316
  triangle ptr;             /* Temporary variable used by sym() and onext(). */
 
10317
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
10318
 
 
10319
  if (verbose > 1) {
 
10320
    printf("  Marking neighbors of marked triangles.\n");
 
10321
  }
 
10322
  /* Loop through all the infected triangles, spreading the attribute      */
 
10323
  /*   and/or area constraint to their neighbors, then to their neighbors' */
 
10324
  /*   neighbors.                                                          */
 
10325
  traversalinit(&viri);
 
10326
  virusloop = (triangle **) traverse(&viri);
 
10327
  while (virusloop != (triangle **) NULL) {
 
10328
    testtri.tri = *virusloop;
 
10329
    /* A triangle is marked as infected by messing with one of its shell */
 
10330
    /*   edges, setting it to an illegal value.  Hence, we have to       */
 
10331
    /*   temporarily uninfect this triangle so that we can examine its   */
 
10332
    /*   adjacent shell edges.                                           */
 
10333
    uninfect(testtri);
 
10334
    if (regionattrib) {
 
10335
      /* Set an attribute. */
 
10336
      setelemattribute(testtri, eextras, attribute);
 
10337
    }
 
10338
    if (vararea) {
 
10339
      /* Set an area constraint. */
 
10340
      setareabound(testtri, area);
 
10341
    }
 
10342
    if (verbose > 2) {
 
10343
      /* Assign the triangle an orientation for convenience in */
 
10344
      /*   checking its points.                                */
 
10345
      testtri.orient = 0;
 
10346
      org(testtri, regionorg);
 
10347
      dest(testtri, regiondest);
 
10348
      apex(testtri, regionapex);
 
10349
      printf("    Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
 
10350
             regionorg[0], regionorg[1], regiondest[0], regiondest[1],
 
10351
             regionapex[0], regionapex[1]);
 
10352
    }
 
10353
    /* Check each of the triangle's three neighbors. */
 
10354
    for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
 
10355
      /* Find the neighbor. */
 
10356
      sym(testtri, neighbor);
 
10357
      /* Check for a shell between the triangle and its neighbor. */
 
10358
      tspivot(testtri, neighborshelle);
 
10359
      /* Make sure the neighbor exists, is not already infected, and */
 
10360
      /*   isn't protected by a shell edge.                          */
 
10361
      if ((neighbor.tri != dummytri) && !infected(neighbor)
 
10362
          && (neighborshelle.sh == dummysh)) {
 
10363
        if (verbose > 2) {
 
10364
          org(neighbor, regionorg);
 
10365
          dest(neighbor, regiondest);
 
10366
          apex(neighbor, regionapex);
 
10367
          printf("    Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
 
10368
                 regionorg[0], regionorg[1], regiondest[0], regiondest[1],
 
10369
                 regionapex[0], regionapex[1]);
 
10370
        }
 
10371
        /* Infect the neighbor. */
 
10372
        infect(neighbor);
 
10373
        /* Ensure that the neighbor's neighbors will be infected. */
 
10374
        regiontri = (triangle **) poolalloc(&viri);
 
10375
        *regiontri = neighbor.tri;
 
10376
      }
 
10377
    }
 
10378
    /* Remark the triangle as infected, so it doesn't get added to the */
 
10379
    /*   virus pool again.                                             */
 
10380
    infect(testtri);
 
10381
    virusloop = (triangle **) traverse(&viri);
 
10382
  }
 
10383
 
 
10384
  /* Uninfect all triangles. */
 
10385
  if (verbose > 1) {
 
10386
    printf("  Unmarking marked triangles.\n");
 
10387
  }
 
10388
  traversalinit(&viri);
 
10389
  virusloop = (triangle **) traverse(&viri);
 
10390
  while (virusloop != (triangle **) NULL) {
 
10391
    testtri.tri = *virusloop;
 
10392
    uninfect(testtri);
 
10393
    virusloop = (triangle **) traverse(&viri);
 
10394
  }
 
10395
  /* Empty the virus pool. */
 
10396
  poolrestart(&viri);
 
10397
}
 
10398
 
 
10399
/*****************************************************************************/
 
10400
/*                                                                           */
 
10401
/*  carveholes()   Find the holes and infect them.  Find the area            */
 
10402
/*                 constraints and infect them.  Infect the convex hull.     */
 
10403
/*                 Spread the infection and kill triangles.  Spread the      */
 
10404
/*                 area constraints.                                         */
 
10405
/*                                                                           */
 
10406
/*  This routine mainly calls other routines to carry out all these          */
 
10407
/*  functions.                                                               */
 
10408
/*                                                                           */
 
10409
/*****************************************************************************/
 
10410
 
 
10411
void carveholes(
 
10412
REAL *holelist,
 
10413
int holes,
 
10414
REAL *regionlist,
 
10415
int regions)
 
10416
{
 
10417
  struct triedge searchtri;
 
10418
  struct triedge triangleloop;
 
10419
  struct triedge *regiontris;
 
10420
  triangle **holetri;
 
10421
  triangle **regiontri;
 
10422
  point searchorg, searchdest;
 
10423
  enum locateresult intersect;
 
10424
  int i;
 
10425
  triangle ptr;                         /* Temporary variable used by sym(). */
 
10426
 
 
10427
  if (!(quiet || (noholes && convex))) {
 
10428
    printf("Removing unwanted triangles.\n");
 
10429
    if (verbose && (holes > 0)) {
 
10430
      printf("  Marking holes for elimination.\n");
 
10431
    }
 
10432
  }
 
10433
 
 
10434
  if (regions > 0) {
 
10435
    /* Allocate storage for the triangles in which region points fall. */
 
10436
    regiontris = (struct triedge *) malloc(regions * sizeof(struct triedge));
 
10437
    if (regiontris == (struct triedge *) NULL) {
 
10438
      printf("Error:  Out of memory.\n");
 
10439
      exit(1);
 
10440
    }
 
10441
  }
 
10442
 
 
10443
  if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
 
10444
    /* Initialize a pool of viri to be used for holes, concavities, */
 
10445
    /*   regional attributes, and/or regional area constraints.     */
 
10446
    poolinit(&viri, sizeof(triangle *), VIRUSPERBLOCK, POINTER, 0);
 
10447
  }
 
10448
 
 
10449
  if (!convex) {
 
10450
    /* Mark as infected any unprotected triangles on the boundary. */
 
10451
    /*   This is one way by which concavities are created.         */
 
10452
    infecthull();
 
10453
  }
 
10454
 
 
10455
  if ((holes > 0) && !noholes) {
 
10456
    /* Infect each triangle in which a hole lies. */
 
10457
    for (i = 0; i < 2 * holes; i += 2) {
 
10458
      /* Ignore holes that aren't within the bounds of the mesh. */
 
10459
      if ((holelist[i] >= xmin) && (holelist[i] <= xmax)
 
10460
          && (holelist[i + 1] >= ymin) && (holelist[i + 1] <= ymax)) {
 
10461
        /* Start searching from some triangle on the outer boundary. */
 
10462
        searchtri.tri = dummytri;
 
10463
        searchtri.orient = 0;
 
10464
        symself(searchtri);
 
10465
        /* Ensure that the hole is to the left of this boundary edge; */
 
10466
        /*   otherwise, locate() will falsely report that the hole    */
 
10467
        /*   falls within the starting triangle.                      */
 
10468
        org(searchtri, searchorg);
 
10469
        dest(searchtri, searchdest);
 
10470
        if (counterclockwise(searchorg, searchdest, &holelist[i]) > 0.0) {
 
10471
          /* Find a triangle that contains the hole. */
 
10472
          intersect = locate(&holelist[i], &searchtri);
 
10473
          if ((intersect != OUTSIDE) && (!infected(searchtri))) {
 
10474
            /* Infect the triangle.  This is done by marking the triangle */
 
10475
            /*   as infect and including the triangle in the virus pool.  */
 
10476
            infect(searchtri);
 
10477
            holetri = (triangle **) poolalloc(&viri);
 
10478
            *holetri = searchtri.tri;
 
10479
          }
 
10480
        }
 
10481
      }
 
10482
    }
 
10483
  }
 
10484
 
 
10485
  /* Now, we have to find all the regions BEFORE we carve the holes, because */
 
10486
  /*   locate() won't work when the triangulation is no longer convex.       */
 
10487
  /*   (Incidentally, this is the reason why regional attributes and area    */
 
10488
  /*   constraints can't be used when refining a preexisting mesh, which     */
 
10489
  /*   might not be convex; they can only be used with a freshly             */
 
10490
  /*   triangulated PSLG.)                                                   */
 
10491
  if (regions > 0) {
 
10492
    /* Find the starting triangle for each region. */
 
10493
    for (i = 0; i < regions; i++) {
 
10494
      regiontris[i].tri = dummytri;
 
10495
      /* Ignore region points that aren't within the bounds of the mesh. */
 
10496
      if ((regionlist[4 * i] >= xmin) && (regionlist[4 * i] <= xmax) &&
 
10497
          (regionlist[4 * i + 1] >= ymin) && (regionlist[4 * i + 1] <= ymax)) {
 
10498
        /* Start searching from some triangle on the outer boundary. */
 
10499
        searchtri.tri = dummytri;
 
10500
        searchtri.orient = 0;
 
10501
        symself(searchtri);
 
10502
        /* Ensure that the region point is to the left of this boundary */
 
10503
        /*   edge; otherwise, locate() will falsely report that the     */
 
10504
        /*   region point falls within the starting triangle.           */
 
10505
        org(searchtri, searchorg);
 
10506
        dest(searchtri, searchdest);
 
10507
        if (counterclockwise(searchorg, searchdest, &regionlist[4 * i]) >
 
10508
            0.0) {
 
10509
          /* Find a triangle that contains the region point. */
 
10510
          intersect = locate(&regionlist[4 * i], &searchtri);
 
10511
          if ((intersect != OUTSIDE) && (!infected(searchtri))) {
 
10512
            /* Record the triangle for processing after the */
 
10513
            /*   holes have been carved.                    */
 
10514
            triedgecopy(searchtri, regiontris[i]);
 
10515
          }
 
10516
        }
 
10517
      }
 
10518
    }
 
10519
  }
 
10520
 
 
10521
  if (viri.items > 0) {
 
10522
    /* Carve the holes and concavities. */
 
10523
    plague();
 
10524
  }
 
10525
  /* The virus pool should be empty now. */
 
10526
 
 
10527
  if (regions > 0) {
 
10528
    if (!quiet) {
 
10529
      if (regionattrib) {
 
10530
        if (vararea) {
 
10531
          printf("Spreading regional attributes and area constraints.\n");
 
10532
        } else {
 
10533
          printf("Spreading regional attributes.\n");
 
10534
        }
 
10535
      } else { 
 
10536
        printf("Spreading regional area constraints.\n");
 
10537
      }
 
10538
    }
 
10539
    if (regionattrib && !refine) {
 
10540
      /* Assign every triangle a regional attribute of zero. */
 
10541
      traversalinit(&triangles);
 
10542
      triangleloop.orient = 0;
 
10543
      triangleloop.tri = triangletraverse();
 
10544
      while (triangleloop.tri != (triangle *) NULL) {
 
10545
        setelemattribute(triangleloop, eextras, 0.0);
 
10546
        triangleloop.tri = triangletraverse();
 
10547
      }
 
10548
    }
 
10549
    for (i = 0; i < regions; i++) {
 
10550
      if (regiontris[i].tri != dummytri) {
 
10551
        /* Make sure the triangle under consideration still exists. */
 
10552
        /*   It may have been eaten by the virus.                   */
 
10553
        if (regiontris[i].tri[3] != (triangle) NULL) {
 
10554
          /* Put one triangle in the virus pool. */
 
10555
          infect(regiontris[i]);
 
10556
          regiontri = (triangle **) poolalloc(&viri);
 
10557
          *regiontri = regiontris[i].tri;
 
10558
          /* Apply one region's attribute and/or area constraint. */
 
10559
          regionplague(regionlist[4 * i + 2], regionlist[4 * i + 3]);
 
10560
          /* The virus pool should be empty now. */
 
10561
        }
 
10562
      }
 
10563
    }
 
10564
    if (regionattrib && !refine) {
 
10565
      /* Note the fact that each triangle has an additional attribute. */
 
10566
      eextras++;
 
10567
    }
 
10568
  }
 
10569
 
 
10570
  /* Free up memory. */
 
10571
  if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
 
10572
    pooldeinit(&viri);
 
10573
  }
 
10574
  if (regions > 0) {
 
10575
    free(regiontris);
 
10576
  }
 
10577
}
 
10578
 
 
10579
/**                                                                         **/
 
10580
/**                                                                         **/
 
10581
/********* Carving out holes and concavities ends here               *********/
 
10582
 
 
10583
/********* Mesh quality maintenance begins here                      *********/
 
10584
/**                                                                         **/
 
10585
/**                                                                         **/
 
10586
 
 
10587
/*****************************************************************************/
 
10588
/*                                                                           */
 
10589
/*  tallyencs()   Traverse the entire list of shell edges, check each edge   */
 
10590
/*                to see if it is encroached.  If so, add it to the list.    */
 
10591
/*                                                                           */
 
10592
/*****************************************************************************/
 
10593
 
 
10594
#ifndef CDT_ONLY
 
10595
 
 
10596
void tallyencs()
 
10597
{
 
10598
  struct edge edgeloop;
 
10599
  int dummy;
 
10600
 
 
10601
  traversalinit(&shelles);
 
10602
  edgeloop.shorient = 0;
 
10603
  edgeloop.sh = shelletraverse();
 
10604
  while (edgeloop.sh != (shelle *) NULL) {
 
10605
    /* If the segment is encroached, add it to the list. */
 
10606
    dummy = checkedge4encroach(&edgeloop);
 
10607
    edgeloop.sh = shelletraverse();
 
10608
  }
 
10609
}
 
10610
 
 
10611
#endif /* not CDT_ONLY */
 
10612
 
 
10613
/*****************************************************************************/
 
10614
/*                                                                           */
 
10615
/*  precisionerror()  Print an error message for precision problems.         */
 
10616
/*                                                                           */
 
10617
/*****************************************************************************/
 
10618
 
 
10619
#ifndef CDT_ONLY
 
10620
 
 
10621
void precisionerror()
 
10622
{
 
10623
  printf("Try increasing the area criterion and/or reducing the minimum\n");
 
10624
  printf("  allowable angle so that tiny triangles are not created.\n");
 
10625
#ifdef SINGLE
 
10626
  printf("Alternatively, try recompiling me with double precision\n");
 
10627
  printf("  arithmetic (by removing \"#define SINGLE\" from the\n");
 
10628
  printf("  source file or \"-DSINGLE\" from the makefile).\n");
 
10629
#endif /* SINGLE */
 
10630
}
 
10631
 
 
10632
#endif /* not CDT_ONLY */
 
10633
 
 
10634
/*****************************************************************************/
 
10635
/*                                                                           */
 
10636
/*  repairencs()   Find and repair all the encroached segments.              */
 
10637
/*                                                                           */
 
10638
/*  Encroached segments are repaired by splitting them by inserting a point  */
 
10639
/*  at or near their centers.                                                */
 
10640
/*                                                                           */
 
10641
/*  `flaws' is a flag that specifies whether one should take note of new     */
 
10642
/*  encroached segments and bad triangles that result from inserting points  */
 
10643
/*  to repair existing encroached segments.                                  */
 
10644
/*                                                                           */
 
10645
/*  When a segment is split, the two resulting subsegments are always        */
 
10646
/*  tested to see if they are encroached upon, regardless of the value       */
 
10647
/*  of `flaws'.                                                              */
 
10648
/*                                                                           */
 
10649
/*****************************************************************************/
 
10650
 
 
10651
#ifndef CDT_ONLY
 
10652
 
 
10653
void repairencs(
 
10654
int flaws)
 
10655
{
 
10656
  struct triedge enctri;
 
10657
  struct triedge testtri;
 
10658
  struct edge *encloop;
 
10659
  struct edge testsh;
 
10660
  point eorg, edest;
 
10661
  point newpoint;
 
10662
  enum insertsiteresult success;
 
10663
  REAL segmentlength, nearestpoweroftwo;
 
10664
  REAL split;
 
10665
  int acuteorg, acutedest;
 
10666
  int dummy;
 
10667
  int i;
 
10668
  triangle ptr;                     /* Temporary variable used by stpivot(). */
 
10669
  shelle sptr;                        /* Temporary variable used by snext(). */
 
10670
 
 
10671
  while ((badsegments.items > 0) && (steinerleft != 0)) {
 
10672
    traversalinit(&badsegments);
 
10673
    encloop = badsegmenttraverse();
 
10674
    while ((encloop != (struct edge *) NULL) && (steinerleft != 0)) {
 
10675
      /* To decide where to split a segment, we need to know if the  */
 
10676
      /*   segment shares an endpoint with an adjacent segment.      */
 
10677
      /*   The concern is that, if we simply split every encroached  */
 
10678
      /*   segment in its center, two adjacent segments with a small */
 
10679
      /*   angle between them might lead to an infinite loop; each   */
 
10680
      /*   point added to split one segment will encroach upon the   */
 
10681
      /*   other segment, which must then be split with a point that */
 
10682
      /*   will encroach upon the first segment, and so on forever.  */
 
10683
      /* To avoid this, imagine a set of concentric circles, whose   */
 
10684
      /*   radii are powers of two, about each segment endpoint.     */
 
10685
      /*   These concentric circles determine where the segment is   */
 
10686
      /*   split.  (If both endpoints are shared with adjacent       */
 
10687
      /*   segments, split the segment in the middle, and apply the  */
 
10688
      /*   concentric shells for later splittings.)                  */
 
10689
 
 
10690
      /* Is the origin shared with another segment? */
 
10691
      stpivot(*encloop, enctri);
 
10692
      lnext(enctri, testtri);
 
10693
      tspivot(testtri, testsh);
 
10694
      acuteorg = testsh.sh != dummysh;
 
10695
      /* Is the destination shared with another segment? */
 
10696
      lnextself(testtri);
 
10697
      tspivot(testtri, testsh);
 
10698
      acutedest = testsh.sh != dummysh;
 
10699
      /* Now, check the other side of the segment, if there's a triangle */
 
10700
      /*   there.                                                        */
 
10701
      sym(enctri, testtri);
 
10702
      if (testtri.tri != dummytri) {
 
10703
        /* Is the destination shared with another segment? */
 
10704
        lnextself(testtri);
 
10705
        tspivot(testtri, testsh);
 
10706
        acutedest = acutedest || (testsh.sh != dummysh);
 
10707
        /* Is the origin shared with another segment? */
 
10708
        lnextself(testtri);
 
10709
        tspivot(testtri, testsh);
 
10710
        acuteorg = acuteorg || (testsh.sh != dummysh);
 
10711
      }
 
10712
 
 
10713
      sorg(*encloop, eorg);
 
10714
      sdest(*encloop, edest);
 
10715
      /* Use the concentric circles if exactly one endpoint is shared */
 
10716
      /*   with another adjacent segment.                             */
 
10717
      if (acuteorg ^ acutedest) {
 
10718
        segmentlength = sqrt((edest[0] - eorg[0]) * (edest[0] - eorg[0])
 
10719
                             + (edest[1] - eorg[1]) * (edest[1] - eorg[1]));
 
10720
        /* Find the power of two nearest the segment's length. */
 
10721
        nearestpoweroftwo = 1.0;
 
10722
        while (segmentlength > SQUAREROOTTWO * nearestpoweroftwo) {
 
10723
          nearestpoweroftwo *= 2.0;
 
10724
        }
 
10725
        while (segmentlength < (0.5 * SQUAREROOTTWO) * nearestpoweroftwo) {
 
10726
          nearestpoweroftwo *= 0.5;
 
10727
        }
 
10728
        /* Where do we split the segment? */
 
10729
        split = 0.5 * nearestpoweroftwo / segmentlength;
 
10730
        if (acutedest) {
 
10731
          split = 1.0 - split;
 
10732
        }
 
10733
      } else {
 
10734
        /* If we're not worried about adjacent segments, split */
 
10735
        /*   this segment in the middle.                       */
 
10736
        split = 0.5;
 
10737
      }
 
10738
 
 
10739
      /* Create the new point. */
 
10740
      newpoint = (point) poolalloc(&points);
 
10741
      /* Interpolate its coordinate and attributes. */
 
10742
      for (i = 0; i < 2 + nextras; i++) {
 
10743
        newpoint[i] = (1.0 - split) * eorg[i] + split * edest[i];
 
10744
      }
 
10745
      setpointmark(newpoint, mark(*encloop));
 
10746
      if (verbose > 1) {
 
10747
        printf(
 
10748
        "  Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
 
10749
               eorg[0], eorg[1], edest[0], edest[1], newpoint[0], newpoint[1]);
 
10750
      }
 
10751
      /* Check whether the new point lies on an endpoint. */
 
10752
      if (((newpoint[0] == eorg[0]) && (newpoint[1] == eorg[1]))
 
10753
        || ((newpoint[0] == edest[0]) && (newpoint[1] == edest[1]))) {
 
10754
        printf("Error:  Ran out of precision at (%.12g, %.12g).\n",
 
10755
               newpoint[0], newpoint[1]);
 
10756
        printf("I attempted to split a segment to a smaller size than can\n");
 
10757
        printf("  be accommodated by the finite precision of floating point\n"
 
10758
               );
 
10759
        printf("  arithmetic.\n");
 
10760
        precisionerror();
 
10761
        exit(1);
 
10762
      }
 
10763
      /* Insert the splitting point.  This should always succeed. */
 
10764
      success = insertsite(newpoint, &enctri, encloop, flaws, flaws);
 
10765
      if ((success != SUCCESSFULPOINT) && (success != ENCROACHINGPOINT)) {
 
10766
        printf("Internal error in repairencs():\n");
 
10767
        printf("  Failure to split a segment.\n");
 
10768
        internalerror();
 
10769
      }
 
10770
      if (steinerleft > 0) {
 
10771
        steinerleft--;
 
10772
      }
 
10773
      /* Check the two new subsegments to see if they're encroached. */
 
10774
      dummy = checkedge4encroach(encloop);
 
10775
      snextself(*encloop);
 
10776
      dummy = checkedge4encroach(encloop);
 
10777
 
 
10778
      badsegmentdealloc(encloop);
 
10779
      encloop = badsegmenttraverse();
 
10780
    }
 
10781
  }
 
10782
}
 
10783
 
 
10784
#endif /* not CDT_ONLY */
 
10785
 
 
10786
/*****************************************************************************/
 
10787
/*                                                                           */
 
10788
/*  tallyfaces()   Test every triangle in the mesh for quality measures.     */
 
10789
/*                                                                           */
 
10790
/*****************************************************************************/
 
10791
 
 
10792
#ifndef CDT_ONLY
 
10793
 
 
10794
void tallyfaces()
 
10795
{
 
10796
  struct triedge triangleloop;
 
10797
 
 
10798
  if (verbose) {
 
10799
    printf("  Making a list of bad triangles.\n");
 
10800
  }
 
10801
  traversalinit(&triangles);
 
10802
  triangleloop.orient = 0;
 
10803
  triangleloop.tri = triangletraverse();
 
10804
  while (triangleloop.tri != (triangle *) NULL) {
 
10805
    /* If the triangle is bad, enqueue it. */
 
10806
    testtriangle(&triangleloop);
 
10807
    triangleloop.tri = triangletraverse();
 
10808
  }
 
10809
}
 
10810
 
 
10811
#endif /* not CDT_ONLY */
 
10812
 
 
10813
/*****************************************************************************/
 
10814
/*                                                                           */
 
10815
/*  findcircumcenter()   Find the circumcenter of a triangle.                */
 
10816
/*                                                                           */
 
10817
/*  The result is returned both in terms of x-y coordinates and xi-eta       */
 
10818
/*  coordinates.  The xi-eta coordinate system is defined in terms of the    */
 
10819
/*  triangle:  the origin of the triangle is the origin of the coordinate    */
 
10820
/*  system; the destination of the triangle is one unit along the xi axis;   */
 
10821
/*  and the apex of the triangle is one unit along the eta axis.             */
 
10822
/*                                                                           */
 
10823
/*  The return value indicates which edge of the triangle is shortest.       */
 
10824
/*                                                                           */
 
10825
/*****************************************************************************/
 
10826
 
 
10827
enum circumcenterresult findcircumcenter(
 
10828
point torg,
 
10829
point tdest,
 
10830
point tapex,
 
10831
point circumcenter,
 
10832
REAL *xi,
 
10833
REAL *eta)
 
10834
{
 
10835
  REAL xdo, ydo, xao, yao, xad, yad;
 
10836
  REAL dodist, aodist, addist;
 
10837
  REAL denominator;
 
10838
  REAL dx, dy;
 
10839
 
 
10840
  circumcentercount++;
 
10841
 
 
10842
  /* Compute the circumcenter of the triangle. */
 
10843
  xdo = tdest[0] - torg[0];
 
10844
  ydo = tdest[1] - torg[1];
 
10845
  xao = tapex[0] - torg[0];
 
10846
  yao = tapex[1] - torg[1];
 
10847
  dodist = xdo * xdo + ydo * ydo;
 
10848
  aodist = xao * xao + yao * yao;
 
10849
  if (noexact) {
 
10850
    denominator = 0.5 / (xdo * yao - xao * ydo);
 
10851
  } else {
 
10852
    /* Use the counterclockwise() routine to ensure a positive (and */
 
10853
    /*   reasonably accurate) result, avoiding any possibility of   */
 
10854
    /*   division by zero.                                          */
 
10855
    denominator = 0.5 / counterclockwise(tdest, tapex, torg);
 
10856
    /* Don't count the above as an orientation test. */
 
10857
    counterclockcount--;
 
10858
  }
 
10859
  circumcenter[0] = torg[0] - (ydo * aodist - yao * dodist) * denominator;  
 
10860
  circumcenter[1] = torg[1] + (xdo * aodist - xao * dodist) * denominator;  
 
10861
 
 
10862
  /* To interpolate point attributes for the new point inserted at  */
 
10863
  /*   the circumcenter, define a coordinate system with a xi-axis, */
 
10864
  /*   directed from the triangle's origin to its destination, and  */
 
10865
  /*   an eta-axis, directed from its origin to its apex.           */
 
10866
  /*   Calculate the xi and eta coordinates of the circumcenter.    */
 
10867
  dx = circumcenter[0] - torg[0];
 
10868
  dy = circumcenter[1] - torg[1];
 
10869
  *xi = (dx * yao - xao * dy) * (2.0 * denominator);
 
10870
  *eta = (xdo * dy - dx * ydo) * (2.0 * denominator);
 
10871
 
 
10872
  xad = tapex[0] - tdest[0];
 
10873
  yad = tapex[1] - tdest[1];
 
10874
  addist = xad * xad + yad * yad;
 
10875
  if ((addist < dodist) && (addist < aodist)) {
 
10876
    return OPPOSITEORG;
 
10877
  } else if (dodist < aodist) {
 
10878
    return OPPOSITEAPEX;
 
10879
  } else {
 
10880
    return OPPOSITEDEST;
 
10881
  }
 
10882
}
 
10883
 
 
10884
/*****************************************************************************/
 
10885
/*                                                                           */
 
10886
/*  splittriangle()   Inserts a point at the circumcenter of a triangle.     */
 
10887
/*                    Deletes the newly inserted point if it encroaches upon */
 
10888
/*                    a segment.                                             */
 
10889
/*                                                                           */
 
10890
/*****************************************************************************/
 
10891
 
 
10892
#ifndef CDT_ONLY
 
10893
 
 
10894
void splittriangle(
 
10895
struct badface *badtri)
 
10896
{
 
10897
  point borg, bdest, bapex;
 
10898
  point newpoint;
 
10899
  REAL xi, eta;
 
10900
  enum insertsiteresult success;
 
10901
  enum circumcenterresult shortedge;
 
10902
  int errorflag;
 
10903
  int i;
 
10904
 
 
10905
  org(badtri->badfacetri, borg);
 
10906
  dest(badtri->badfacetri, bdest);
 
10907
  apex(badtri->badfacetri, bapex);
 
10908
  /* Make sure that this triangle is still the same triangle it was      */
 
10909
  /*   when it was tested and determined to be of bad quality.           */
 
10910
  /*   Subsequent transformations may have made it a different triangle. */
 
10911
  if ((borg == badtri->faceorg) && (bdest == badtri->facedest) &&
 
10912
      (bapex == badtri->faceapex)) {
 
10913
    if (verbose > 1) {
 
10914
      printf("  Splitting this triangle at its circumcenter:\n");
 
10915
      printf("    (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", borg[0],
 
10916
             borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
 
10917
    }
 
10918
    errorflag = 0;
 
10919
    /* Create a new point at the triangle's circumcenter. */
 
10920
    newpoint = (point) poolalloc(&points);
 
10921
    shortedge = findcircumcenter(borg, bdest, bapex, newpoint, &xi, &eta);
 
10922
    /* Check whether the new point lies on a triangle vertex. */
 
10923
    if (((newpoint[0] == borg[0]) && (newpoint[1] == borg[1]))
 
10924
        || ((newpoint[0] == bdest[0]) && (newpoint[1] == bdest[1]))
 
10925
        || ((newpoint[0] == bapex[0]) && (newpoint[1] == bapex[1]))) {
 
10926
      if (!quiet) {
 
10927
        printf("Warning:  New point (%.12g, %.12g) falls on existing vertex.\n"
 
10928
               , newpoint[0], newpoint[1]);
 
10929
        errorflag = 1;
 
10930
      }
 
10931
      pointdealloc(newpoint);
 
10932
    } else {
 
10933
      for (i = 2; i < 2 + nextras; i++) {
 
10934
        /* Interpolate the point attributes at the circumcenter. */
 
10935
        newpoint[i] = borg[i] + xi * (bdest[i] - borg[i])
 
10936
                             + eta * (bapex[i] - borg[i]);
 
10937
      }
 
10938
      /* The new point must be in the interior, and have a marker of zero. */
 
10939
      setpointmark(newpoint, 0);
 
10940
      /* Ensure that the handle `badtri->badfacetri' represents the shortest */
 
10941
      /*   edge of the triangle.  This ensures that the circumcenter must    */
 
10942
      /*   fall to the left of this edge, so point location will work.       */
 
10943
      if (shortedge == OPPOSITEORG) {
 
10944
        lnextself(badtri->badfacetri);
 
10945
      } else if (shortedge == OPPOSITEDEST) {
 
10946
        lprevself(badtri->badfacetri);
 
10947
      }
 
10948
      /* Insert the circumcenter, searching from the edge of the triangle, */
 
10949
      /*   and maintain the Delaunay property of the triangulation.        */
 
10950
      success = insertsite(newpoint, &(badtri->badfacetri),
 
10951
                           (struct edge *) NULL, 1, 1);
 
10952
      if (success == SUCCESSFULPOINT) {
 
10953
        if (steinerleft > 0) {
 
10954
          steinerleft--;
 
10955
        }
 
10956
      } else if (success == ENCROACHINGPOINT) {
 
10957
        /* If the newly inserted point encroaches upon a segment, delete it. */
 
10958
        deletesite(&(badtri->badfacetri));
 
10959
      } else if (success == VIOLATINGPOINT) {
 
10960
        /* Failed to insert the new point, but some segment was */
 
10961
        /*   marked as being encroached.                        */
 
10962
        pointdealloc(newpoint);
 
10963
      } else {                                  /* success == DUPLICATEPOINT */
 
10964
        /* Failed to insert the new point because a vertex is already there. */
 
10965
        if (!quiet) {
 
10966
          printf(
 
10967
            "Warning:  New point (%.12g, %.12g) falls on existing vertex.\n"
 
10968
                 , newpoint[0], newpoint[1]);
 
10969
          errorflag = 1;
 
10970
        }
 
10971
        pointdealloc(newpoint);
 
10972
      }
 
10973
    }
 
10974
    if (errorflag) {
 
10975
      if (verbose) {
 
10976
        printf("  The new point is at the circumcenter of triangle\n");
 
10977
        printf("    (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
 
10978
               borg[0], borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
 
10979
      }
 
10980
      printf("This probably means that I am trying to refine triangles\n");
 
10981
      printf("  to a smaller size than can be accommodated by the finite\n");
 
10982
      printf("  precision of floating point arithmetic.  (You can be\n");
 
10983
      printf("  sure of this if I fail to terminate.)\n");
 
10984
      precisionerror();
 
10985
    }
 
10986
  }
 
10987
  /* Return the bad triangle to the pool. */
 
10988
  pooldealloc(&badtriangles, (VOID *) badtri);
 
10989
}
 
10990
 
 
10991
#endif /* not CDT_ONLY */
 
10992
 
 
10993
/*****************************************************************************/
 
10994
/*                                                                           */
 
10995
/*  enforcequality()   Remove all the encroached edges and bad triangles     */
 
10996
/*                     from the triangulation.                               */
 
10997
/*                                                                           */
 
10998
/*****************************************************************************/
 
10999
 
 
11000
#ifndef CDT_ONLY
 
11001
 
 
11002
void enforcequality()
 
11003
{
 
11004
  int i;
 
11005
 
 
11006
  if (!quiet) {
 
11007
    printf("Adding Steiner points to enforce quality.\n");
 
11008
  }
 
11009
  /* Initialize the pool of encroached segments. */
 
11010
  poolinit(&badsegments, sizeof(struct edge), BADSEGMENTPERBLOCK, POINTER, 0);
 
11011
  if (verbose) {
 
11012
    printf("  Looking for encroached segments.\n");
 
11013
  }
 
11014
  /* Test all segments to see if they're encroached. */
 
11015
  tallyencs();
 
11016
  if (verbose && (badsegments.items > 0)) {
 
11017
    printf("  Splitting encroached segments.\n");
 
11018
  }
 
11019
  /* Note that steinerleft == -1 if an unlimited number */
 
11020
  /*   of Steiner points is allowed.                    */
 
11021
  while ((badsegments.items > 0) && (steinerleft != 0)) {
 
11022
    /* Fix the segments without noting newly encroached segments or   */
 
11023
    /*   bad triangles.  The reason we don't want to note newly       */
 
11024
    /*   encroached segments is because some encroached segments are  */
 
11025
    /*   likely to be noted multiple times, and would then be blindly */
 
11026
    /*   split multiple times.  I should fix that some time.          */
 
11027
    repairencs(0);
 
11028
    /* Now, find all the segments that became encroached while adding */
 
11029
    /*   points to split encroached segments.                         */
 
11030
    tallyencs();
 
11031
  }
 
11032
  /* At this point, if we haven't run out of Steiner points, the */
 
11033
  /*   triangulation should be (conforming) Delaunay.            */
 
11034
 
 
11035
  /* Next, we worry about enforcing triangle quality. */
 
11036
  if ((minangle > 0.0) || vararea || fixedarea) {
 
11037
    /* Initialize the pool of bad triangles. */
 
11038
    poolinit(&badtriangles, sizeof(struct badface), BADTRIPERBLOCK, POINTER,
 
11039
             0);
 
11040
    /* Initialize the queues of bad triangles. */
 
11041
    for (i = 0; i < 64; i++) {
 
11042
      queuefront[i] = (struct badface *) NULL;
 
11043
      queuetail[i] = &queuefront[i];
 
11044
    }
 
11045
    /* Test all triangles to see if they're bad. */
 
11046
    tallyfaces();
 
11047
    if (verbose) {
 
11048
      printf("  Splitting bad triangles.\n");
 
11049
    }
 
11050
    while ((badtriangles.items > 0) && (steinerleft != 0)) {
 
11051
      /* Fix one bad triangle by inserting a point at its circumcenter. */
 
11052
      splittriangle(dequeuebadtri());
 
11053
      /* Fix any encroached segments that may have resulted.  Record */
 
11054
      /*   any new bad triangles or encroached segments that result. */
 
11055
      if (badsegments.items > 0) {
 
11056
        repairencs(1);
 
11057
      }
 
11058
    }
 
11059
  }
 
11060
  /* At this point, if we haven't run out of Steiner points, the */
 
11061
  /*   triangulation should be (conforming) Delaunay and have no */
 
11062
  /*   low-quality triangles.                                    */
 
11063
 
 
11064
  /* Might we have run out of Steiner points too soon? */
 
11065
  if (!quiet && (badsegments.items > 0) && (steinerleft == 0)) {
 
11066
    printf("\nWarning:  I ran out of Steiner points, but the mesh has\n");
 
11067
    if (badsegments.items == 1) {
 
11068
      printf("  an encroached segment, and therefore might not be truly\n");
 
11069
    } else {
 
11070
      printf("  %ld encroached segments, and therefore might not be truly\n",
 
11071
             badsegments.items);
 
11072
    }
 
11073
    printf("  Delaunay.  If the Delaunay property is important to you,\n");
 
11074
    printf("  try increasing the number of Steiner points (controlled by\n");
 
11075
    printf("  the -S switch) slightly and try again.\n\n");
 
11076
  }
 
11077
}
 
11078
 
 
11079
#endif /* not CDT_ONLY */
 
11080
 
 
11081
/**                                                                         **/
 
11082
/**                                                                         **/
 
11083
/********* Mesh quality maintenance ends here                        *********/
 
11084
 
 
11085
/*****************************************************************************/
 
11086
/*                                                                           */
 
11087
/*  highorder()   Create extra nodes for quadratic subparametric elements.   */
 
11088
/*                                                                           */
 
11089
/*****************************************************************************/
 
11090
 
 
11091
void highorder()
 
11092
{
 
11093
  struct triedge triangleloop, trisym;
 
11094
  struct edge checkmark;
 
11095
  point newpoint;
 
11096
  point torg, tdest;
 
11097
  int i;
 
11098
  triangle ptr;                         /* Temporary variable used by sym(). */
 
11099
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
11100
 
 
11101
  if (!quiet) {
 
11102
    printf("Adding vertices for second-order triangles.\n");
 
11103
  }
 
11104
  /* The following line ensures that dead items in the pool of nodes    */
 
11105
  /*   cannot be allocated for the extra nodes associated with high     */
 
11106
  /*   order elements.  This ensures that the primary nodes (at the     */
 
11107
  /*   corners of elements) will occur earlier in the output files, and */
 
11108
  /*   have lower indices, than the extra nodes.                        */
 
11109
  points.deaditemstack = (VOID *) NULL;
 
11110
 
 
11111
  traversalinit(&triangles);
 
11112
  triangleloop.tri = triangletraverse();
 
11113
  /* To loop over the set of edges, loop over all triangles, and look at   */
 
11114
  /*   the three edges of each triangle.  If there isn't another triangle  */
 
11115
  /*   adjacent to the edge, operate on the edge.  If there is another     */
 
11116
  /*   adjacent triangle, operate on the edge only if the current triangle */
 
11117
  /*   has a smaller pointer than its neighbor.  This way, each edge is    */
 
11118
  /*   considered only once.                                               */
 
11119
  while (triangleloop.tri != (triangle *) NULL) {
 
11120
    for (triangleloop.orient = 0; triangleloop.orient < 3;
 
11121
         triangleloop.orient++) {
 
11122
      sym(triangleloop, trisym);
 
11123
      if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
 
11124
        org(triangleloop, torg);
 
11125
        dest(triangleloop, tdest);
 
11126
        /* Create a new node in the middle of the edge.  Interpolate */
 
11127
        /*   its attributes.                                         */
 
11128
        newpoint = (point) poolalloc(&points);
 
11129
        for (i = 0; i < 2 + nextras; i++) {
 
11130
          newpoint[i] = 0.5 * (torg[i] + tdest[i]);
 
11131
        }
 
11132
        /* Set the new node's marker to zero or one, depending on */
 
11133
        /*   whether it lies on a boundary.                       */
 
11134
        setpointmark(newpoint, trisym.tri == dummytri);
 
11135
        if (useshelles) {
 
11136
          tspivot(triangleloop, checkmark);
 
11137
          /* If this edge is a segment, transfer the marker to the new node. */
 
11138
          if (checkmark.sh != dummysh) {
 
11139
            setpointmark(newpoint, mark(checkmark));
 
11140
          }
 
11141
        }
 
11142
        if (verbose > 1) {
 
11143
          printf("  Creating (%.12g, %.12g).\n", newpoint[0], newpoint[1]);
 
11144
        }
 
11145
        /* Record the new node in the (one or two) adjacent elements. */
 
11146
        triangleloop.tri[highorderindex + triangleloop.orient] =
 
11147
                (triangle) newpoint;
 
11148
        if (trisym.tri != dummytri) {
 
11149
          trisym.tri[highorderindex + trisym.orient] = (triangle) newpoint;
 
11150
        }
 
11151
      }
 
11152
    }
 
11153
    triangleloop.tri = triangletraverse();
 
11154
  }
 
11155
}
 
11156
 
 
11157
/********* File I/O routines begin here                              *********/
 
11158
/**                                                                         **/
 
11159
/**                                                                         **/
 
11160
 
 
11161
/*****************************************************************************/
 
11162
/*                                                                           */
 
11163
/*  readline()   Read a nonempty line from a file.                           */
 
11164
/*                                                                           */
 
11165
/*  A line is considered "nonempty" if it contains something that looks like */
 
11166
/*  a number.                                                                */
 
11167
/*                                                                           */
 
11168
/*****************************************************************************/
 
11169
 
 
11170
#ifndef TRILIBRARY
 
11171
 
 
11172
char *readline(string, infile, infilename)
 
11173
char *string;
 
11174
FILE *infile;
 
11175
char *infilename;
 
11176
{
 
11177
  char *result;
 
11178
 
 
11179
  /* Search for something that looks like a number. */
 
11180
  do {
 
11181
    result = fgets(string, INPUTLINESIZE, infile);
 
11182
    if (result == (char *) NULL) {
 
11183
      printf("  Error:  Unexpected end of file in %s.\n", infilename);
 
11184
      exit(1);
 
11185
    }
 
11186
    /* Skip anything that doesn't look like a number, a comment, */
 
11187
    /*   or the end of a line.                                   */
 
11188
    while ((*result != '\0') && (*result != '#')
 
11189
           && (*result != '.') && (*result != '+') && (*result != '-')
 
11190
           && ((*result < '0') || (*result > '9'))) {
 
11191
      result++;
 
11192
    }
 
11193
  /* If it's a comment or end of line, read another line and try again. */
 
11194
  } while ((*result == '#') || (*result == '\0'));
 
11195
  return result;
 
11196
}
 
11197
 
 
11198
#endif /* not TRILIBRARY */
 
11199
 
 
11200
/*****************************************************************************/
 
11201
/*                                                                           */
 
11202
/*  findfield()   Find the next field of a string.                           */
 
11203
/*                                                                           */
 
11204
/*  Jumps past the current field by searching for whitespace, then jumps     */
 
11205
/*  past the whitespace to find the next field.                              */
 
11206
/*                                                                           */
 
11207
/*****************************************************************************/
 
11208
 
 
11209
#ifndef TRILIBRARY
 
11210
 
 
11211
char *findfield(string)
 
11212
char *string;
 
11213
{
 
11214
  char *result;
 
11215
 
 
11216
  result = string;
 
11217
  /* Skip the current field.  Stop upon reaching whitespace. */
 
11218
  while ((*result != '\0') && (*result != '#')
 
11219
         && (*result != ' ') && (*result != '\t')) {
 
11220
    result++;
 
11221
  }
 
11222
  /* Now skip the whitespace and anything else that doesn't look like a */
 
11223
  /*   number, a comment, or the end of a line.                         */
 
11224
  while ((*result != '\0') && (*result != '#')
 
11225
         && (*result != '.') && (*result != '+') && (*result != '-')
 
11226
         && ((*result < '0') || (*result > '9'))) {
 
11227
    result++;
 
11228
  }
 
11229
  /* Check for a comment (prefixed with `#'). */
 
11230
  if (*result == '#') {
 
11231
    *result = '\0';
 
11232
  }
 
11233
  return result;
 
11234
}
 
11235
 
 
11236
#endif /* not TRILIBRARY */
 
11237
 
 
11238
/*****************************************************************************/
 
11239
/*                                                                           */
 
11240
/*  readnodes()   Read the points from a file, which may be a .node or .poly */
 
11241
/*                file.                                                      */
 
11242
/*                                                                           */
 
11243
/*****************************************************************************/
 
11244
 
 
11245
#ifndef TRILIBRARY
 
11246
 
 
11247
void readnodes(nodefilename, polyfilename, polyfile)
 
11248
char *nodefilename;
 
11249
char *polyfilename;
 
11250
FILE **polyfile;
 
11251
{
 
11252
  FILE *infile;
 
11253
  point pointloop;
 
11254
  char inputline[INPUTLINESIZE];
 
11255
  char *stringptr;
 
11256
  char *infilename;
 
11257
  REAL x, y;
 
11258
  int firstnode;
 
11259
  int nodemarkers;
 
11260
  int currentmarker;
 
11261
  int i, j;
 
11262
 
 
11263
  if (poly) {
 
11264
    /* Read the points from a .poly file. */
 
11265
    if (!quiet) {
 
11266
      printf("Opening %s.\n", polyfilename);
 
11267
    }
 
11268
    *polyfile = fopen(polyfilename, "r");
 
11269
    if (*polyfile == (FILE *) NULL) {
 
11270
      printf("  Error:  Cannot access file %s.\n", polyfilename);
 
11271
      exit(1);
 
11272
    }
 
11273
    /* Read number of points, number of dimensions, number of point */
 
11274
    /*   attributes, and number of boundary markers.                */
 
11275
    stringptr = readline(inputline, *polyfile, polyfilename);
 
11276
    inpoints = (int) strtol (stringptr, &stringptr, 0);
 
11277
    stringptr = findfield(stringptr);
 
11278
    if (*stringptr == '\0') {
 
11279
      mesh_dim = 2;
 
11280
    } else {
 
11281
      mesh_dim = (int) strtol (stringptr, &stringptr, 0);
 
11282
    }
 
11283
    stringptr = findfield(stringptr);
 
11284
    if (*stringptr == '\0') {
 
11285
      nextras = 0;
 
11286
    } else {
 
11287
      nextras = (int) strtol (stringptr, &stringptr, 0);
 
11288
    }
 
11289
    stringptr = findfield(stringptr);
 
11290
    if (*stringptr == '\0') {
 
11291
      nodemarkers = 0;
 
11292
    } else {
 
11293
      nodemarkers = (int) strtol (stringptr, &stringptr, 0);
 
11294
    }
 
11295
    if (inpoints > 0) {
 
11296
      infile = *polyfile;
 
11297
      infilename = polyfilename;
 
11298
      readnodefile = 0;
 
11299
    } else {
 
11300
      /* If the .poly file claims there are zero points, that means that */
 
11301
      /*   the points should be read from a separate .node file.         */
 
11302
      readnodefile = 1;
 
11303
      infilename = innodefilename;
 
11304
    }
 
11305
  } else {
 
11306
    readnodefile = 1;
 
11307
    infilename = innodefilename;
 
11308
    *polyfile = (FILE *) NULL;
 
11309
  }
 
11310
 
 
11311
  if (readnodefile) {
 
11312
    /* Read the points from a .node file. */
 
11313
    if (!quiet) {
 
11314
      printf("Opening %s.\n", innodefilename);
 
11315
    }
 
11316
    infile = fopen(innodefilename, "r");
 
11317
    if (infile == (FILE *) NULL) {
 
11318
      printf("  Error:  Cannot access file %s.\n", innodefilename);
 
11319
      exit(1);
 
11320
    }
 
11321
    /* Read number of points, number of dimensions, number of point */
 
11322
    /*   attributes, and number of boundary markers.                */
 
11323
    stringptr = readline(inputline, infile, innodefilename);
 
11324
    inpoints = (int) strtol (stringptr, &stringptr, 0);
 
11325
    stringptr = findfield(stringptr);
 
11326
    if (*stringptr == '\0') {
 
11327
      mesh_dim = 2;
 
11328
    } else {
 
11329
      mesh_dim = (int) strtol (stringptr, &stringptr, 0);
 
11330
    }
 
11331
    stringptr = findfield(stringptr);
 
11332
    if (*stringptr == '\0') {
 
11333
      nextras = 0;
 
11334
    } else {
 
11335
      nextras = (int) strtol (stringptr, &stringptr, 0);
 
11336
    }
 
11337
    stringptr = findfield(stringptr);
 
11338
    if (*stringptr == '\0') {
 
11339
      nodemarkers = 0;
 
11340
    } else {
 
11341
      nodemarkers = (int) strtol (stringptr, &stringptr, 0);
 
11342
    }
 
11343
  }
 
11344
 
 
11345
  if (inpoints < 3) {
 
11346
    printf("Error:  Input must have at least three input points.\n");
 
11347
    exit(1);
 
11348
  }
 
11349
  if (mesh_dim != 2) {
 
11350
    printf("Error:  Triangle only works with two-dimensional meshes.\n");
 
11351
    exit(1);
 
11352
  }
 
11353
 
 
11354
  initializepointpool();
 
11355
 
 
11356
  /* Read the points. */
 
11357
  for (i = 0; i < inpoints; i++) {
 
11358
    pointloop = (point) poolalloc(&points);
 
11359
    stringptr = readline(inputline, infile, infilename);
 
11360
    if (i == 0) {
 
11361
      firstnode = (int) strtol (stringptr, &stringptr, 0);
 
11362
      if ((firstnode == 0) || (firstnode == 1)) {
 
11363
        firstnumber = firstnode;
 
11364
      }
 
11365
    }
 
11366
    stringptr = findfield(stringptr);
 
11367
    if (*stringptr == '\0') {
 
11368
      printf("Error:  Point %d has no x coordinate.\n", firstnumber + i);
 
11369
      exit(1);
 
11370
    }
 
11371
    x = (REAL) strtod(stringptr, &stringptr);
 
11372
    stringptr = findfield(stringptr);
 
11373
    if (*stringptr == '\0') {
 
11374
      printf("Error:  Point %d has no y coordinate.\n", firstnumber + i);
 
11375
      exit(1);
 
11376
    }
 
11377
    y = (REAL) strtod(stringptr, &stringptr);
 
11378
    pointloop[0] = x;
 
11379
    pointloop[1] = y;
 
11380
    /* Read the point attributes. */
 
11381
    for (j = 2; j < 2 + nextras; j++) {
 
11382
      stringptr = findfield(stringptr);
 
11383
      if (*stringptr == '\0') {
 
11384
        pointloop[j] = 0.0;
 
11385
      } else {
 
11386
        pointloop[j] = (REAL) strtod(stringptr, &stringptr);
 
11387
      }
 
11388
    }
 
11389
    if (nodemarkers) {
 
11390
      /* Read a point marker. */
 
11391
      stringptr = findfield(stringptr);
 
11392
      if (*stringptr == '\0') {
 
11393
        setpointmark(pointloop, 0);
 
11394
      } else {
 
11395
        currentmarker = (int) strtol (stringptr, &stringptr, 0);
 
11396
        setpointmark(pointloop, currentmarker);
 
11397
      }
 
11398
    } else {
 
11399
      /* If no markers are specified in the file, they default to zero. */
 
11400
      setpointmark(pointloop, 0);
 
11401
    }
 
11402
    /* Determine the smallest and largest x and y coordinates. */
 
11403
    if (i == 0) {
 
11404
      xmin = xmax = x;
 
11405
      ymin = ymax = y;
 
11406
    } else {
 
11407
      xmin = (x < xmin) ? x : xmin;
 
11408
      xmax = (x > xmax) ? x : xmax;
 
11409
      ymin = (y < ymin) ? y : ymin;
 
11410
      ymax = (y > ymax) ? y : ymax;
 
11411
    }
 
11412
  }
 
11413
  if (readnodefile) {
 
11414
    fclose(infile);
 
11415
  }
 
11416
 
 
11417
  /* Nonexistent x value used as a flag to mark circle events in sweepline */
 
11418
  /*   Delaunay algorithm.                                                 */
 
11419
  xminextreme = 10 * xmin - 9 * xmax;
 
11420
}
 
11421
 
 
11422
#endif /* not TRILIBRARY */
 
11423
 
 
11424
/*****************************************************************************/
 
11425
/*                                                                           */
 
11426
/*  transfernodes()   Read the points from memory.                           */
 
11427
/*                                                                           */
 
11428
/*****************************************************************************/
 
11429
 
 
11430
#ifdef TRILIBRARY
 
11431
 
 
11432
void transfernodes(
 
11433
REAL *pointlist,
 
11434
REAL *pointattriblist,
 
11435
int *pointmarkerlist,
 
11436
int numberofpoints,
 
11437
int numberofpointattribs)
 
11438
{
 
11439
  point pointloop;
 
11440
  REAL x, y;
 
11441
  int i, j;
 
11442
  int coordindex;
 
11443
  int attribindex;
 
11444
 
 
11445
  inpoints = numberofpoints;
 
11446
  mesh_dim = 2;
 
11447
  nextras = numberofpointattribs;
 
11448
  readnodefile = 0;
 
11449
  if (inpoints < 3) {
 
11450
    printf("Error:  Input must have at least three input points.\n");
 
11451
    exit(1);
 
11452
  }
 
11453
 
 
11454
  initializepointpool();
 
11455
 
 
11456
  /* Read the points. */
 
11457
  coordindex = 0;
 
11458
  attribindex = 0;
 
11459
  for (i = 0; i < inpoints; i++) {
 
11460
    pointloop = (point) poolalloc(&points);
 
11461
    /* Read the point coordinates. */
 
11462
    x = pointloop[0] = pointlist[coordindex++];
 
11463
    y = pointloop[1] = pointlist[coordindex++];
 
11464
    /* Read the point attributes. */
 
11465
    for (j = 0; j < numberofpointattribs; j++) {
 
11466
      pointloop[2 + j] = pointattriblist[attribindex++];
 
11467
    }
 
11468
    if (pointmarkerlist != (int *) NULL) {
 
11469
      /* Read a point marker. */
 
11470
      setpointmark(pointloop, pointmarkerlist[i]);
 
11471
    } else {
 
11472
      /* If no markers are specified, they default to zero. */
 
11473
      setpointmark(pointloop, 0);
 
11474
    }
 
11475
    x = pointloop[0];
 
11476
    y = pointloop[1];
 
11477
    /* Determine the smallest and largest x and y coordinates. */
 
11478
    if (i == 0) {
 
11479
      xmin = xmax = x;
 
11480
      ymin = ymax = y;
 
11481
    } else {
 
11482
      xmin = (x < xmin) ? x : xmin;
 
11483
      xmax = (x > xmax) ? x : xmax;
 
11484
      ymin = (y < ymin) ? y : ymin;
 
11485
      ymax = (y > ymax) ? y : ymax;
 
11486
    }
 
11487
  }
 
11488
 
 
11489
  /* Nonexistent x value used as a flag to mark circle events in sweepline */
 
11490
  /*   Delaunay algorithm.                                                 */
 
11491
  xminextreme = 10 * xmin - 9 * xmax;
 
11492
}
 
11493
 
 
11494
#endif /* TRILIBRARY */
 
11495
 
 
11496
/*****************************************************************************/
 
11497
/*                                                                           */
 
11498
/*  readholes()   Read the holes, and possibly regional attributes and area  */
 
11499
/*                constraints, from a .poly file.                            */
 
11500
/*                                                                           */
 
11501
/*****************************************************************************/
 
11502
 
 
11503
#ifndef TRILIBRARY
 
11504
 
 
11505
void readholes(polyfile, polyfilename, hlist, holes, rlist, regions)
 
11506
FILE *polyfile;
 
11507
char *polyfilename;
 
11508
REAL **hlist;
 
11509
int *holes;
 
11510
REAL **rlist;
 
11511
int *regions;
 
11512
{
 
11513
  REAL *holelist;
 
11514
  REAL *regionlist;
 
11515
  char inputline[INPUTLINESIZE];
 
11516
  char *stringptr;
 
11517
  int index;
 
11518
  int i;
 
11519
 
 
11520
  /* Read the holes. */
 
11521
  stringptr = readline(inputline, polyfile, polyfilename);
 
11522
  *holes = (int) strtol (stringptr, &stringptr, 0);
 
11523
  if (*holes > 0) {
 
11524
    holelist = (REAL *) malloc(2 * *holes * sizeof(REAL));
 
11525
    *hlist = holelist;
 
11526
    if (holelist == (REAL *) NULL) {
 
11527
      printf("Error:  Out of memory.\n");
 
11528
      exit(1);
 
11529
    }
 
11530
    for (i = 0; i < 2 * *holes; i += 2) {
 
11531
      stringptr = readline(inputline, polyfile, polyfilename);
 
11532
      stringptr = findfield(stringptr);
 
11533
      if (*stringptr == '\0') {
 
11534
        printf("Error:  Hole %d has no x coordinate.\n",
 
11535
               firstnumber + (i >> 1));
 
11536
        exit(1);
 
11537
      } else {
 
11538
        holelist[i] = (REAL) strtod(stringptr, &stringptr);
 
11539
      }
 
11540
      stringptr = findfield(stringptr);
 
11541
      if (*stringptr == '\0') {
 
11542
        printf("Error:  Hole %d has no y coordinate.\n",
 
11543
               firstnumber + (i >> 1));
 
11544
        exit(1);
 
11545
      } else {
 
11546
        holelist[i + 1] = (REAL) strtod(stringptr, &stringptr);
 
11547
      }
 
11548
    }
 
11549
  } else {
 
11550
    *hlist = (REAL *) NULL;
 
11551
  }
 
11552
 
 
11553
#ifndef CDT_ONLY
 
11554
  if ((regionattrib || vararea) && !refine) {
 
11555
    /* Read the area constraints. */
 
11556
    stringptr = readline(inputline, polyfile, polyfilename);
 
11557
    *regions = (int) strtol (stringptr, &stringptr, 0);
 
11558
    if (*regions > 0) {
 
11559
      regionlist = (REAL *) malloc(4 * *regions * sizeof(REAL));
 
11560
      *rlist = regionlist;
 
11561
      if (regionlist == (REAL *) NULL) {
 
11562
        printf("Error:  Out of memory.\n");
 
11563
        exit(1);
 
11564
      }
 
11565
      index = 0;
 
11566
      for (i = 0; i < *regions; i++) {
 
11567
        stringptr = readline(inputline, polyfile, polyfilename);
 
11568
        stringptr = findfield(stringptr);
 
11569
        if (*stringptr == '\0') {
 
11570
          printf("Error:  Region %d has no x coordinate.\n",
 
11571
                 firstnumber + i);
 
11572
          exit(1);
 
11573
        } else {
 
11574
          regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
 
11575
        }
 
11576
        stringptr = findfield(stringptr);
 
11577
        if (*stringptr == '\0') {
 
11578
          printf("Error:  Region %d has no y coordinate.\n",
 
11579
                 firstnumber + i);
 
11580
          exit(1);
 
11581
        } else {
 
11582
          regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
 
11583
        }
 
11584
        stringptr = findfield(stringptr);
 
11585
        if (*stringptr == '\0') {
 
11586
          printf(
 
11587
            "Error:  Region %d has no region attribute or area constraint.\n",
 
11588
                 firstnumber + i);
 
11589
          exit(1);
 
11590
        } else {
 
11591
          regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
 
11592
        }
 
11593
        stringptr = findfield(stringptr);
 
11594
        if (*stringptr == '\0') {
 
11595
          regionlist[index] = regionlist[index - 1];
 
11596
        } else {
 
11597
          regionlist[index] = (REAL) strtod(stringptr, &stringptr);
 
11598
        }
 
11599
        index++;
 
11600
      }
 
11601
    }
 
11602
  } else {
 
11603
    /* Set `*regions' to zero to avoid an accidental free() later. */
 
11604
    *regions = 0;
 
11605
    *rlist = (REAL *) NULL;
 
11606
  }
 
11607
#endif /* not CDT_ONLY */
 
11608
 
 
11609
  fclose(polyfile);
 
11610
}
 
11611
 
 
11612
#endif /* not TRILIBRARY */
 
11613
 
 
11614
/*****************************************************************************/
 
11615
/*                                                                           */
 
11616
/*  finishfile()   Write the command line to the output file so the user     */
 
11617
/*                 can remember how the file was generated.  Close the file. */
 
11618
/*                                                                           */
 
11619
/*****************************************************************************/
 
11620
 
 
11621
#ifndef TRILIBRARY
 
11622
 
 
11623
void finishfile(outfile, argc, argv)
 
11624
FILE *outfile;
 
11625
int argc;
 
11626
char **argv;
 
11627
{
 
11628
  int i;
 
11629
 
 
11630
  fprintf(outfile, "# Generated by");
 
11631
  for (i = 0; i < argc; i++) {
 
11632
    fprintf(outfile, " ");
 
11633
    fputs(argv[i], outfile);
 
11634
  }
 
11635
  fprintf(outfile, "\n");
 
11636
  fclose(outfile);
 
11637
}
 
11638
 
 
11639
#endif /* not TRILIBRARY */
 
11640
 
 
11641
/*****************************************************************************/
 
11642
/*                                                                           */
 
11643
/*  writenodes()   Number the points and write them to a .node file.         */
 
11644
/*                                                                           */
 
11645
/*  To save memory, the point numbers are written over the shell markers     */
 
11646
/*  after the points are written to a file.                                  */
 
11647
/*                                                                           */
 
11648
/*****************************************************************************/
 
11649
 
 
11650
#ifdef TRILIBRARY
 
11651
 
 
11652
void writenodes(
 
11653
REAL **pointlist,
 
11654
REAL **pointattriblist,
 
11655
int **pointmarkerlist)
 
11656
 
 
11657
#else /* not TRILIBRARY */
 
11658
 
 
11659
void writenodes(
 
11660
char *nodefilename,
 
11661
int argc,
 
11662
char **argv)
 
11663
 
 
11664
#endif /* not TRILIBRARY */
 
11665
 
 
11666
{
 
11667
#ifdef TRILIBRARY
 
11668
  REAL *plist;
 
11669
  REAL *palist;
 
11670
  int *pmlist;
 
11671
  int coordindex;
 
11672
  int attribindex;
 
11673
#else /* not TRILIBRARY */
 
11674
  FILE *outfile;
 
11675
#endif /* not TRILIBRARY */
 
11676
  point pointloop;
 
11677
  int pointnumber;
 
11678
  int i;
 
11679
 
 
11680
#ifdef TRILIBRARY
 
11681
  if (!quiet) {
 
11682
    printf("Writing points.\n");
 
11683
  }
 
11684
  /* Allocate memory for output points if necessary. */
 
11685
  if (*pointlist == (REAL *) NULL) {
 
11686
    *pointlist = (REAL *) malloc(points.items * 2 * sizeof(REAL));
 
11687
    if (*pointlist == (REAL *) NULL) {
 
11688
      printf("Error:  Out of memory.\n");
 
11689
      exit(1);
 
11690
    }
 
11691
  }
 
11692
  /* Allocate memory for output point attributes if necessary. */
 
11693
  if ((nextras > 0) && (*pointattriblist == (REAL *) NULL)) {
 
11694
    *pointattriblist = (REAL *) malloc(points.items * nextras * sizeof(REAL));
 
11695
    if (*pointattriblist == (REAL *) NULL) {
 
11696
      printf("Error:  Out of memory.\n");
 
11697
      exit(1);
 
11698
    }
 
11699
  }
 
11700
  /* Allocate memory for output point markers if necessary. */
 
11701
  if (!nobound && (*pointmarkerlist == (int *) NULL)) {
 
11702
    *pointmarkerlist = (int *) malloc(points.items * sizeof(int));
 
11703
    if (*pointmarkerlist == (int *) NULL) {
 
11704
      printf("Error:  Out of memory.\n");
 
11705
      exit(1);
 
11706
    }
 
11707
  }
 
11708
  plist = *pointlist;
 
11709
  palist = *pointattriblist;
 
11710
  pmlist = *pointmarkerlist;
 
11711
  coordindex = 0;
 
11712
  attribindex = 0;
 
11713
#else /* not TRILIBRARY */
 
11714
  if (!quiet) {
 
11715
    printf("Writing %s.\n", nodefilename);
 
11716
  }
 
11717
  outfile = fopen(nodefilename, "w");
 
11718
  if (outfile == (FILE *) NULL) {
 
11719
    printf("  Error:  Cannot create file %s.\n", nodefilename);
 
11720
    exit(1);
 
11721
  }
 
11722
  /* Number of points, number of dimensions, number of point attributes, */
 
11723
  /*   and number of boundary markers (zero or one).                     */
 
11724
  fprintf(outfile, "%ld  %d  %d  %d\n", points.items, mesh_dim, nextras,
 
11725
          1 - nobound);
 
11726
#endif /* not TRILIBRARY */
 
11727
 
 
11728
  traversalinit(&points);
 
11729
  pointloop = pointtraverse();
 
11730
  pointnumber = firstnumber;
 
11731
  while (pointloop != (point) NULL) {
 
11732
#ifdef TRILIBRARY
 
11733
    /* X and y coordinates. */
 
11734
    plist[coordindex++] = pointloop[0];
 
11735
    plist[coordindex++] = pointloop[1];
 
11736
    /* Point attributes. */
 
11737
    for (i = 0; i < nextras; i++) {
 
11738
      palist[attribindex++] = pointloop[2 + i];
 
11739
    }
 
11740
    if (!nobound) {
 
11741
      /* Copy the boundary marker. */
 
11742
      pmlist[pointnumber - firstnumber] = pointmark(pointloop);
 
11743
    }
 
11744
#else /* not TRILIBRARY */
 
11745
    /* Point number, x and y coordinates. */
 
11746
    fprintf(outfile, "%4d    %.17g  %.17g", pointnumber, pointloop[0],
 
11747
            pointloop[1]);
 
11748
    for (i = 0; i < nextras; i++) {
 
11749
      /* Write an attribute. */
 
11750
      fprintf(outfile, "  %.17g", pointloop[i + 2]);
 
11751
    }
 
11752
    if (nobound) {
 
11753
      fprintf(outfile, "\n");
 
11754
    } else {
 
11755
      /* Write the boundary marker. */
 
11756
      fprintf(outfile, "    %d\n", pointmark(pointloop));
 
11757
    }
 
11758
#endif /* not TRILIBRARY */
 
11759
 
 
11760
    setpointmark(pointloop, pointnumber);
 
11761
    pointloop = pointtraverse();
 
11762
    pointnumber++;
 
11763
  }
 
11764
 
 
11765
#ifndef TRILIBRARY
 
11766
  finishfile(outfile, argc, argv);
 
11767
#endif /* not TRILIBRARY */
 
11768
}
 
11769
 
 
11770
/*****************************************************************************/
 
11771
/*                                                                           */
 
11772
/*  numbernodes()   Number the points.                                       */
 
11773
/*                                                                           */
 
11774
/*  Each point is assigned a marker equal to its number.                     */
 
11775
/*                                                                           */
 
11776
/*  Used when writenodes() is not called because no .node file is written.   */
 
11777
/*                                                                           */
 
11778
/*****************************************************************************/
 
11779
 
 
11780
void numbernodes()
 
11781
{
 
11782
  point pointloop;
 
11783
  int pointnumber;
 
11784
 
 
11785
  traversalinit(&points);
 
11786
  pointloop = pointtraverse();
 
11787
  pointnumber = firstnumber;
 
11788
  while (pointloop != (point) NULL) {
 
11789
    setpointmark(pointloop, pointnumber);
 
11790
    pointloop = pointtraverse();
 
11791
    pointnumber++;
 
11792
  }
 
11793
}
 
11794
 
 
11795
/*****************************************************************************/
 
11796
/*                                                                           */
 
11797
/*  writeelements()   Write the triangles to an .ele file.                   */
 
11798
/*                                                                           */
 
11799
/*****************************************************************************/
 
11800
 
 
11801
#ifdef TRILIBRARY
 
11802
 
 
11803
void writeelements(
 
11804
int **trianglelist,
 
11805
REAL **triangleattriblist)
 
11806
 
 
11807
#else /* not TRILIBRARY */
 
11808
 
 
11809
void writeelements(
 
11810
char *elefilename,
 
11811
int argc,
 
11812
char **argv)
 
11813
 
 
11814
#endif /* not TRILIBRARY */
 
11815
 
 
11816
{
 
11817
#ifdef TRILIBRARY
 
11818
  int *tlist;
 
11819
  REAL *talist;
 
11820
  int pointindex;
 
11821
  int attribindex;
 
11822
#else /* not TRILIBRARY */
 
11823
  FILE *outfile;
 
11824
#endif /* not TRILIBRARY */
 
11825
  struct triedge triangleloop;
 
11826
  point p1, p2, p3;
 
11827
  point mid1, mid2, mid3;
 
11828
  int elementnumber;
 
11829
  int i;
 
11830
 
 
11831
#ifdef TRILIBRARY
 
11832
  if (!quiet) {
 
11833
    printf("Writing triangles.\n");
 
11834
  }
 
11835
  /* Allocate memory for output triangles if necessary. */
 
11836
  if (*trianglelist == (int *) NULL) {
 
11837
    *trianglelist = (int *) malloc(triangles.items *
 
11838
                               ((order + 1) * (order + 2) / 2) * sizeof(int));
 
11839
    if (*trianglelist == (int *) NULL) {
 
11840
      printf("Error:  Out of memory.\n");
 
11841
      exit(1);
 
11842
    }
 
11843
  }
 
11844
  /* Allocate memory for output triangle attributes if necessary. */
 
11845
  if ((eextras > 0) && (*triangleattriblist == (REAL *) NULL)) {
 
11846
    *triangleattriblist = (REAL *) malloc(triangles.items * eextras *
 
11847
                                          sizeof(REAL));
 
11848
    if (*triangleattriblist == (REAL *) NULL) {
 
11849
      printf("Error:  Out of memory.\n");
 
11850
      exit(1);
 
11851
    }
 
11852
  }
 
11853
  tlist = *trianglelist;
 
11854
  talist = *triangleattriblist;
 
11855
  pointindex = 0;
 
11856
  attribindex = 0;
 
11857
#else /* not TRILIBRARY */
 
11858
  if (!quiet) {
 
11859
    printf("Writing %s.\n", elefilename);
 
11860
  }
 
11861
  outfile = fopen(elefilename, "w");
 
11862
  if (outfile == (FILE *) NULL) {
 
11863
    printf("  Error:  Cannot create file %s.\n", elefilename);
 
11864
    exit(1);
 
11865
  }
 
11866
  /* Number of triangles, points per triangle, attributes per triangle. */
 
11867
  fprintf(outfile, "%ld  %d  %d\n", triangles.items,
 
11868
          (order + 1) * (order + 2) / 2, eextras);
 
11869
#endif /* not TRILIBRARY */
 
11870
 
 
11871
  traversalinit(&triangles);
 
11872
  triangleloop.tri = triangletraverse();
 
11873
  triangleloop.orient = 0;
 
11874
  elementnumber = firstnumber;
 
11875
  while (triangleloop.tri != (triangle *) NULL) {
 
11876
    org(triangleloop, p1);
 
11877
    dest(triangleloop, p2);
 
11878
    apex(triangleloop, p3);
 
11879
    if (order == 1) {
 
11880
#ifdef TRILIBRARY
 
11881
      tlist[pointindex++] = pointmark(p1);
 
11882
      tlist[pointindex++] = pointmark(p2);
 
11883
      tlist[pointindex++] = pointmark(p3);
 
11884
#else /* not TRILIBRARY */
 
11885
      /* Triangle number, indices for three points. */
 
11886
      fprintf(outfile, "%4d    %4d  %4d  %4d", elementnumber,
 
11887
              pointmark(p1), pointmark(p2), pointmark(p3));
 
11888
#endif /* not TRILIBRARY */
 
11889
    } else {
 
11890
      mid1 = (point) triangleloop.tri[highorderindex + 1];
 
11891
      mid2 = (point) triangleloop.tri[highorderindex + 2];
 
11892
      mid3 = (point) triangleloop.tri[highorderindex];
 
11893
#ifdef TRILIBRARY
 
11894
      tlist[pointindex++] = pointmark(p1);
 
11895
      tlist[pointindex++] = pointmark(p2);
 
11896
      tlist[pointindex++] = pointmark(p3);
 
11897
      tlist[pointindex++] = pointmark(mid1);
 
11898
      tlist[pointindex++] = pointmark(mid2);
 
11899
      tlist[pointindex++] = pointmark(mid3);
 
11900
#else /* not TRILIBRARY */
 
11901
      /* Triangle number, indices for six points. */
 
11902
      fprintf(outfile, "%4d    %4d  %4d  %4d  %4d  %4d  %4d", elementnumber,
 
11903
              pointmark(p1), pointmark(p2), pointmark(p3), pointmark(mid1),
 
11904
              pointmark(mid2), pointmark(mid3));
 
11905
#endif /* not TRILIBRARY */
 
11906
    }
 
11907
 
 
11908
#ifdef TRILIBRARY
 
11909
    for (i = 0; i < eextras; i++) {
 
11910
      talist[attribindex++] = elemattribute(triangleloop, i);
 
11911
    }
 
11912
#else /* not TRILIBRARY */
 
11913
    for (i = 0; i < eextras; i++) {
 
11914
      fprintf(outfile, "  %.17g", elemattribute(triangleloop, i));
 
11915
    }
 
11916
    fprintf(outfile, "\n");
 
11917
#endif /* not TRILIBRARY */
 
11918
 
 
11919
    triangleloop.tri = triangletraverse();
 
11920
    elementnumber++;
 
11921
  }
 
11922
 
 
11923
#ifndef TRILIBRARY
 
11924
  finishfile(outfile, argc, argv);
 
11925
#endif /* not TRILIBRARY */
 
11926
}
 
11927
 
 
11928
/*****************************************************************************/
 
11929
/*                                                                           */
 
11930
/*  writepoly()   Write the segments and holes to a .poly file.              */
 
11931
/*                                                                           */
 
11932
/*****************************************************************************/
 
11933
 
 
11934
#ifdef TRILIBRARY
 
11935
 
 
11936
void writepoly(
 
11937
int **segmentlist,
 
11938
int **segmentmarkerlist)
 
11939
 
 
11940
#else /* not TRILIBRARY */
 
11941
 
 
11942
void writepoly(
 
11943
char *polyfilename,
 
11944
REAL *holelist,
 
11945
int holes,
 
11946
REAL *regionlist,
 
11947
int regions,
 
11948
int argc,
 
11949
char **argv)
 
11950
 
 
11951
#endif /* not TRILIBRARY */
 
11952
 
 
11953
{
 
11954
#ifdef TRILIBRARY
 
11955
  int *slist;
 
11956
  int *smlist;
 
11957
  int index;
 
11958
#else /* not TRILIBRARY */
 
11959
  FILE *outfile;
 
11960
  int i;
 
11961
#endif /* not TRILIBRARY */
 
11962
  struct edge shelleloop;
 
11963
  point endpoint1, endpoint2;
 
11964
  int shellenumber;
 
11965
 
 
11966
#ifdef TRILIBRARY
 
11967
  if (!quiet) {
 
11968
    printf("Writing segments.\n");
 
11969
  }
 
11970
  /* Allocate memory for output segments if necessary. */
 
11971
  if (*segmentlist == (int *) NULL) {
 
11972
    *segmentlist = (int *) malloc(shelles.items * 2 * sizeof(int));
 
11973
    if (*segmentlist == (int *) NULL) {
 
11974
      printf("Error:  Out of memory.\n");
 
11975
      exit(1);
 
11976
    }
 
11977
  }
 
11978
  /* Allocate memory for output segment markers if necessary. */
 
11979
  if (!nobound && (*segmentmarkerlist == (int *) NULL)) {
 
11980
    *segmentmarkerlist = (int *) malloc(shelles.items * sizeof(int));
 
11981
    if (*segmentmarkerlist == (int *) NULL) {
 
11982
      printf("Error:  Out of memory.\n");
 
11983
      exit(1);
 
11984
    }
 
11985
  }
 
11986
  slist = *segmentlist;
 
11987
  smlist = *segmentmarkerlist;
 
11988
  index = 0;
 
11989
#else /* not TRILIBRARY */
 
11990
  if (!quiet) {
 
11991
    printf("Writing %s.\n", polyfilename);
 
11992
  }
 
11993
  outfile = fopen(polyfilename, "w");
 
11994
  if (outfile == (FILE *) NULL) {
 
11995
    printf("  Error:  Cannot create file %s.\n", polyfilename);
 
11996
    exit(1);
 
11997
  }
 
11998
  /* The zero indicates that the points are in a separate .node file. */
 
11999
  /*   Followed by number of dimensions, number of point attributes,  */
 
12000
  /*   and number of boundary markers (zero or one).                  */
 
12001
  fprintf(outfile, "%d  %d  %d  %d\n", 0, mesh_dim, nextras, 1 - nobound);
 
12002
  /* Number of segments, number of boundary markers (zero or one). */
 
12003
  fprintf(outfile, "%ld  %d\n", shelles.items, 1 - nobound);
 
12004
#endif /* not TRILIBRARY */
 
12005
 
 
12006
  traversalinit(&shelles);
 
12007
  shelleloop.sh = shelletraverse();
 
12008
  shelleloop.shorient = 0;
 
12009
  shellenumber = firstnumber;
 
12010
  while (shelleloop.sh != (shelle *) NULL) {
 
12011
    sorg(shelleloop, endpoint1);
 
12012
    sdest(shelleloop, endpoint2);
 
12013
#ifdef TRILIBRARY
 
12014
    /* Copy indices of the segment's two endpoints. */
 
12015
    slist[index++] = pointmark(endpoint1);
 
12016
    slist[index++] = pointmark(endpoint2);
 
12017
    if (!nobound) {
 
12018
      /* Copy the boundary marker. */
 
12019
      smlist[shellenumber - firstnumber] = mark(shelleloop);
 
12020
    }
 
12021
#else /* not TRILIBRARY */
 
12022
    /* Segment number, indices of its two endpoints, and possibly a marker. */
 
12023
    if (nobound) {
 
12024
      fprintf(outfile, "%4d    %4d  %4d\n", shellenumber,
 
12025
              pointmark(endpoint1), pointmark(endpoint2));
 
12026
    } else {
 
12027
      fprintf(outfile, "%4d    %4d  %4d    %4d\n", shellenumber,
 
12028
              pointmark(endpoint1), pointmark(endpoint2), mark(shelleloop));
 
12029
    }
 
12030
#endif /* not TRILIBRARY */
 
12031
 
 
12032
    shelleloop.sh = shelletraverse();
 
12033
    shellenumber++;
 
12034
  }
 
12035
 
 
12036
#ifndef TRILIBRARY
 
12037
#ifndef CDT_ONLY
 
12038
  fprintf(outfile, "%d\n", holes);
 
12039
  if (holes > 0) {
 
12040
    for (i = 0; i < holes; i++) {
 
12041
      /* Hole number, x and y coordinates. */
 
12042
      fprintf(outfile, "%4d   %.17g  %.17g\n", firstnumber + i,
 
12043
              holelist[2 * i], holelist[2 * i + 1]);
 
12044
    }
 
12045
  }
 
12046
  if (regions > 0) {
 
12047
    fprintf(outfile, "%d\n", regions);
 
12048
    for (i = 0; i < regions; i++) {
 
12049
      /* Region number, x and y coordinates, attribute, maximum area. */
 
12050
      fprintf(outfile, "%4d   %.17g  %.17g  %.17g  %.17g\n", firstnumber + i,
 
12051
              regionlist[4 * i], regionlist[4 * i + 1],
 
12052
              regionlist[4 * i + 2], regionlist[4 * i + 3]);
 
12053
    }
 
12054
  }
 
12055
#endif /* not CDT_ONLY */
 
12056
 
 
12057
  finishfile(outfile, argc, argv);
 
12058
#endif /* not TRILIBRARY */
 
12059
}
 
12060
 
 
12061
/*****************************************************************************/
 
12062
/*                                                                           */
 
12063
/*  writeedges()   Write the edges to a .edge file.                          */
 
12064
/*                                                                           */
 
12065
/*****************************************************************************/
 
12066
 
 
12067
#ifdef TRILIBRARY
 
12068
 
 
12069
void writeedges(
 
12070
int **edgelist,
 
12071
int **edgemarkerlist)
 
12072
 
 
12073
#else /* not TRILIBRARY */
 
12074
 
 
12075
void writeedges(
 
12076
char *edgefilename,
 
12077
int argc,
 
12078
char **argv)
 
12079
 
 
12080
#endif /* not TRILIBRARY */
 
12081
 
 
12082
{
 
12083
#ifdef TRILIBRARY
 
12084
  int *elist;
 
12085
  int *emlist;
 
12086
  int index;
 
12087
#else /* not TRILIBRARY */
 
12088
  FILE *outfile;
 
12089
#endif /* not TRILIBRARY */
 
12090
  struct triedge triangleloop, trisym;
 
12091
  struct edge checkmark;
 
12092
  point p1, p2;
 
12093
  int edgenumber;
 
12094
  triangle ptr;                         /* Temporary variable used by sym(). */
 
12095
  shelle sptr;                      /* Temporary variable used by tspivot(). */
 
12096
 
 
12097
#ifdef TRILIBRARY
 
12098
  if (!quiet) {
 
12099
    printf("Writing edges.\n");
 
12100
  }
 
12101
  /* Allocate memory for edges if necessary. */
 
12102
  if (*edgelist == (int *) NULL) {
 
12103
    *edgelist = (int *) malloc(edges * 2 * sizeof(int));
 
12104
    if (*edgelist == (int *) NULL) {
 
12105
      printf("Error:  Out of memory.\n");
 
12106
      exit(1);
 
12107
    }
 
12108
  }
 
12109
  /* Allocate memory for edge markers if necessary. */
 
12110
  if (!nobound && (*edgemarkerlist == (int *) NULL)) {
 
12111
    *edgemarkerlist = (int *) malloc(edges * sizeof(int));
 
12112
    if (*edgemarkerlist == (int *) NULL) {
 
12113
      printf("Error:  Out of memory.\n");
 
12114
      exit(1);
 
12115
    }
 
12116
  }
 
12117
  elist = *edgelist;
 
12118
  emlist = *edgemarkerlist;
 
12119
  index = 0;
 
12120
#else /* not TRILIBRARY */
 
12121
  if (!quiet) {
 
12122
    printf("Writing %s.\n", edgefilename);
 
12123
  }
 
12124
  outfile = fopen(edgefilename, "w");
 
12125
  if (outfile == (FILE *) NULL) {
 
12126
    printf("  Error:  Cannot create file %s.\n", edgefilename);
 
12127
    exit(1);
 
12128
  }
 
12129
  /* Number of edges, number of boundary markers (zero or one). */
 
12130
  fprintf(outfile, "%ld  %d\n", edges, 1 - nobound);
 
12131
#endif /* not TRILIBRARY */
 
12132
 
 
12133
  traversalinit(&triangles);
 
12134
  triangleloop.tri = triangletraverse();
 
12135
  edgenumber = firstnumber;
 
12136
  /* To loop over the set of edges, loop over all triangles, and look at   */
 
12137
  /*   the three edges of each triangle.  If there isn't another triangle  */
 
12138
  /*   adjacent to the edge, operate on the edge.  If there is another     */
 
12139
  /*   adjacent triangle, operate on the edge only if the current triangle */
 
12140
  /*   has a smaller pointer than its neighbor.  This way, each edge is    */
 
12141
  /*   considered only once.                                               */
 
12142
  while (triangleloop.tri != (triangle *) NULL) {
 
12143
    for (triangleloop.orient = 0; triangleloop.orient < 3;
 
12144
         triangleloop.orient++) {
 
12145
      sym(triangleloop, trisym);
 
12146
      if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
 
12147
        org(triangleloop, p1);
 
12148
        dest(triangleloop, p2);
 
12149
#ifdef TRILIBRARY
 
12150
        elist[index++] = pointmark(p1);
 
12151
        elist[index++] = pointmark(p2);
 
12152
#endif /* TRILIBRARY */
 
12153
        if (nobound) {
 
12154
#ifndef TRILIBRARY
 
12155
          /* Edge number, indices of two endpoints. */
 
12156
          fprintf(outfile, "%4d   %d  %d\n", edgenumber,
 
12157
                  pointmark(p1), pointmark(p2));
 
12158
#endif /* not TRILIBRARY */
 
12159
        } else {
 
12160
          /* Edge number, indices of two endpoints, and a boundary marker. */
 
12161
          /*   If there's no shell edge, the boundary marker is zero.      */
 
12162
          if (useshelles) {
 
12163
            tspivot(triangleloop, checkmark);
 
12164
            if (checkmark.sh == dummysh) {
 
12165
#ifdef TRILIBRARY
 
12166
              emlist[edgenumber - firstnumber] = 0;
 
12167
#else /* not TRILIBRARY */
 
12168
              fprintf(outfile, "%4d   %d  %d  %d\n", edgenumber,
 
12169
                      pointmark(p1), pointmark(p2), 0);
 
12170
#endif /* not TRILIBRARY */
 
12171
            } else {
 
12172
#ifdef TRILIBRARY
 
12173
              emlist[edgenumber - firstnumber] = mark(checkmark);
 
12174
#else /* not TRILIBRARY */
 
12175
              fprintf(outfile, "%4d   %d  %d  %d\n", edgenumber,
 
12176
                      pointmark(p1), pointmark(p2), mark(checkmark));
 
12177
#endif /* not TRILIBRARY */
 
12178
            }
 
12179
          } else {
 
12180
#ifdef TRILIBRARY
 
12181
            emlist[edgenumber - firstnumber] = trisym.tri == dummytri;
 
12182
#else /* not TRILIBRARY */
 
12183
            fprintf(outfile, "%4d   %d  %d  %d\n", edgenumber,
 
12184
                    pointmark(p1), pointmark(p2), trisym.tri == dummytri);
 
12185
#endif /* not TRILIBRARY */
 
12186
          }
 
12187
        }
 
12188
        edgenumber++;
 
12189
      }
 
12190
    }
 
12191
    triangleloop.tri = triangletraverse();
 
12192
  }
 
12193
 
 
12194
#ifndef TRILIBRARY
 
12195
  finishfile(outfile, argc, argv);
 
12196
#endif /* not TRILIBRARY */
 
12197
}
 
12198
 
 
12199
/*****************************************************************************/
 
12200
/*                                                                           */
 
12201
/*  writevoronoi()   Write the Voronoi diagram to a .v.node and .v.edge      */
 
12202
/*                   file.                                                   */
 
12203
/*                                                                           */
 
12204
/*  The Voronoi diagram is the geometric dual of the Delaunay triangulation. */
 
12205
/*  Hence, the Voronoi vertices are listed by traversing the Delaunay        */
 
12206
/*  triangles, and the Voronoi edges are listed by traversing the Delaunay   */
 
12207
/*  edges.                                                                   */
 
12208
/*                                                                           */
 
12209
/*  WARNING:  In order to assign numbers to the Voronoi vertices, this       */
 
12210
/*  procedure messes up the shell edges or the extra nodes of every          */
 
12211
/*  element.  Hence, you should call this procedure last.                    */
 
12212
/*                                                                           */
 
12213
/*****************************************************************************/
 
12214
 
 
12215
#ifdef TRILIBRARY
 
12216
 
 
12217
void writevoronoi(
 
12218
REAL **vpointlist,
 
12219
REAL **vpointattriblist,
 
12220
int **vpointmarkerlist,
 
12221
int **vedgelist,
 
12222
int **vedgemarkerlist,
 
12223
REAL **vnormlist)
 
12224
 
 
12225
#else /* not TRILIBRARY */
 
12226
 
 
12227
void writevoronoi(
 
12228
char *vnodefilename,
 
12229
char *vedgefilename,
 
12230
int argc,
 
12231
char **argv)
 
12232
 
 
12233
#endif /* not TRILIBRARY */
 
12234
 
 
12235
{
 
12236
#ifdef TRILIBRARY
 
12237
  REAL *plist;
 
12238
  REAL *palist;
 
12239
  int *elist;
 
12240
  REAL *normlist;
 
12241
  int coordindex;
 
12242
  int attribindex;
 
12243
#else /* not TRILIBRARY */
 
12244
  FILE *outfile;
 
12245
#endif /* not TRILIBRARY */
 
12246
  struct triedge triangleloop, trisym;
 
12247
  point torg, tdest, tapex;
 
12248
  REAL circumcenter[2];
 
12249
  REAL xi, eta;
 
12250
  int vnodenumber, vedgenumber;
 
12251
  int p1, p2;
 
12252
  int i;
 
12253
  triangle ptr;                         /* Temporary variable used by sym(). */
 
12254
 
 
12255
#ifdef TRILIBRARY
 
12256
  if (!quiet) {
 
12257
    printf("Writing Voronoi vertices.\n");
 
12258
  }
 
12259
  /* Allocate memory for Voronoi vertices if necessary. */
 
12260
  if (*vpointlist == (REAL *) NULL) {
 
12261
    *vpointlist = (REAL *) malloc(triangles.items * 2 * sizeof(REAL));
 
12262
    if (*vpointlist == (REAL *) NULL) {
 
12263
      printf("Error:  Out of memory.\n");
 
12264
      exit(1);
 
12265
    }
 
12266
  }
 
12267
  /* Allocate memory for Voronoi vertex attributes if necessary. */
 
12268
  if (*vpointattriblist == (REAL *) NULL) {
 
12269
    *vpointattriblist = (REAL *) malloc(triangles.items * nextras *
 
12270
                                        sizeof(REAL));
 
12271
    if (*vpointattriblist == (REAL *) NULL) {
 
12272
      printf("Error:  Out of memory.\n");
 
12273
      exit(1);
 
12274
    }
 
12275
  }
 
12276
  *vpointmarkerlist = (int *) NULL;
 
12277
  plist = *vpointlist;
 
12278
  palist = *vpointattriblist;
 
12279
  coordindex = 0;
 
12280
  attribindex = 0;
 
12281
#else /* not TRILIBRARY */
 
12282
  if (!quiet) {
 
12283
    printf("Writing %s.\n", vnodefilename);
 
12284
  }
 
12285
  outfile = fopen(vnodefilename, "w");
 
12286
  if (outfile == (FILE *) NULL) {
 
12287
    printf("  Error:  Cannot create file %s.\n", vnodefilename);
 
12288
    exit(1);
 
12289
  }
 
12290
  /* Number of triangles, two dimensions, number of point attributes, */
 
12291
  /*   zero markers.                                                  */
 
12292
  fprintf(outfile, "%ld  %d  %d  %d\n", triangles.items, 2, nextras, 0);
 
12293
#endif /* not TRILIBRARY */
 
12294
 
 
12295
  traversalinit(&triangles);
 
12296
  triangleloop.tri = triangletraverse();
 
12297
  triangleloop.orient = 0;
 
12298
  vnodenumber = firstnumber;
 
12299
  while (triangleloop.tri != (triangle *) NULL) {
 
12300
    org(triangleloop, torg);
 
12301
    dest(triangleloop, tdest);
 
12302
    apex(triangleloop, tapex);
 
12303
    findcircumcenter(torg, tdest, tapex, circumcenter, &xi, &eta);
 
12304
#ifdef TRILIBRARY
 
12305
    /* X and y coordinates. */
 
12306
    plist[coordindex++] = circumcenter[0];
 
12307
    plist[coordindex++] = circumcenter[1];
 
12308
    for (i = 2; i < 2 + nextras; i++) {
 
12309
      /* Interpolate the point attributes at the circumcenter. */
 
12310
      palist[attribindex++] = torg[i] + xi * (tdest[i] - torg[i])
 
12311
                                     + eta * (tapex[i] - torg[i]);
 
12312
    }
 
12313
#else /* not TRILIBRARY */
 
12314
    /* Voronoi vertex number, x and y coordinates. */
 
12315
    fprintf(outfile, "%4d    %.17g  %.17g", vnodenumber, circumcenter[0],
 
12316
            circumcenter[1]);
 
12317
    for (i = 2; i < 2 + nextras; i++) {
 
12318
      /* Interpolate the point attributes at the circumcenter. */
 
12319
      fprintf(outfile, "  %.17g", torg[i] + xi * (tdest[i] - torg[i])
 
12320
                                         + eta * (tapex[i] - torg[i]));
 
12321
    }
 
12322
    fprintf(outfile, "\n");
 
12323
#endif /* not TRILIBRARY */
 
12324
 
 
12325
    * (int *) (triangleloop.tri + 6) = vnodenumber;
 
12326
    triangleloop.tri = triangletraverse();
 
12327
    vnodenumber++;
 
12328
  }
 
12329
 
 
12330
#ifndef TRILIBRARY
 
12331
  finishfile(outfile, argc, argv);
 
12332
#endif /* not TRILIBRARY */
 
12333
 
 
12334
#ifdef TRILIBRARY
 
12335
  if (!quiet) {
 
12336
    printf("Writing Voronoi edges.\n");
 
12337
  }
 
12338
  /* Allocate memory for output Voronoi edges if necessary. */
 
12339
  if (*vedgelist == (int *) NULL) {
 
12340
    *vedgelist = (int *) malloc(edges * 2 * sizeof(int));
 
12341
    if (*vedgelist == (int *) NULL) {
 
12342
      printf("Error:  Out of memory.\n");
 
12343
      exit(1);
 
12344
    }
 
12345
  }
 
12346
  *vedgemarkerlist = (int *) NULL;
 
12347
  /* Allocate memory for output Voronoi norms if necessary. */
 
12348
  if (*vnormlist == (REAL *) NULL) {
 
12349
    *vnormlist = (REAL *) malloc(edges * 2 * sizeof(REAL));
 
12350
    if (*vnormlist == (REAL *) NULL) {
 
12351
      printf("Error:  Out of memory.\n");
 
12352
      exit(1);
 
12353
    }
 
12354
  }
 
12355
  elist = *vedgelist;
 
12356
  normlist = *vnormlist;
 
12357
  coordindex = 0;
 
12358
#else /* not TRILIBRARY */
 
12359
  if (!quiet) {
 
12360
    printf("Writing %s.\n", vedgefilename);
 
12361
  }
 
12362
  outfile = fopen(vedgefilename, "w");
 
12363
  if (outfile == (FILE *) NULL) {
 
12364
    printf("  Error:  Cannot create file %s.\n", vedgefilename);
 
12365
    exit(1);
 
12366
  }
 
12367
  /* Number of edges, zero boundary markers. */
 
12368
  fprintf(outfile, "%ld  %d\n", edges, 0);
 
12369
#endif /* not TRILIBRARY */
 
12370
 
 
12371
  traversalinit(&triangles);
 
12372
  triangleloop.tri = triangletraverse();
 
12373
  vedgenumber = firstnumber;
 
12374
  /* To loop over the set of edges, loop over all triangles, and look at   */
 
12375
  /*   the three edges of each triangle.  If there isn't another triangle  */
 
12376
  /*   adjacent to the edge, operate on the edge.  If there is another     */
 
12377
  /*   adjacent triangle, operate on the edge only if the current triangle */
 
12378
  /*   has a smaller pointer than its neighbor.  This way, each edge is    */
 
12379
  /*   considered only once.                                               */
 
12380
  while (triangleloop.tri != (triangle *) NULL) {
 
12381
    for (triangleloop.orient = 0; triangleloop.orient < 3;
 
12382
         triangleloop.orient++) {
 
12383
      sym(triangleloop, trisym);
 
12384
      if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
 
12385
        /* Find the number of this triangle (and Voronoi vertex). */
 
12386
        p1 = * (int *) (triangleloop.tri + 6);
 
12387
        if (trisym.tri == dummytri) {
 
12388
          org(triangleloop, torg);
 
12389
          dest(triangleloop, tdest);
 
12390
#ifdef TRILIBRARY
 
12391
          /* Copy an infinite ray.  Index of one endpoint, and -1. */
 
12392
          elist[coordindex] = p1;
 
12393
          normlist[coordindex++] = tdest[1] - torg[1];
 
12394
          elist[coordindex] = -1;
 
12395
          normlist[coordindex++] = torg[0] - tdest[0];
 
12396
#else /* not TRILIBRARY */
 
12397
          /* Write an infinite ray.  Edge number, index of one endpoint, -1, */
 
12398
          /*   and x and y coordinates of a vector representing the          */
 
12399
          /*   direction of the ray.                                         */
 
12400
          fprintf(outfile, "%4d   %d  %d   %.17g  %.17g\n", vedgenumber,
 
12401
                  p1, -1, tdest[1] - torg[1], torg[0] - tdest[0]);
 
12402
#endif /* not TRILIBRARY */
 
12403
        } else {
 
12404
          /* Find the number of the adjacent triangle (and Voronoi vertex). */
 
12405
          p2 = * (int *) (trisym.tri + 6);
 
12406
          /* Finite edge.  Write indices of two endpoints. */
 
12407
#ifdef TRILIBRARY
 
12408
          elist[coordindex] = p1;
 
12409
          normlist[coordindex++] = 0.0;
 
12410
          elist[coordindex] = p2;
 
12411
          normlist[coordindex++] = 0.0;
 
12412
#else /* not TRILIBRARY */
 
12413
          fprintf(outfile, "%4d   %d  %d\n", vedgenumber, p1, p2);
 
12414
#endif /* not TRILIBRARY */
 
12415
        }
 
12416
        vedgenumber++;
 
12417
      }
 
12418
    }
 
12419
    triangleloop.tri = triangletraverse();
 
12420
  }
 
12421
 
 
12422
#ifndef TRILIBRARY
 
12423
  finishfile(outfile, argc, argv);
 
12424
#endif /* not TRILIBRARY */
 
12425
}
 
12426
 
 
12427
#ifdef TRILIBRARY
 
12428
 
 
12429
void writeneighbors(
 
12430
int **neighborlist)
 
12431
 
 
12432
#else /* not TRILIBRARY */
 
12433
 
 
12434
void writeneighbors(
 
12435
char *neighborfilename,
 
12436
int argc,
 
12437
char **argv)
 
12438
 
 
12439
#endif /* not TRILIBRARY */
 
12440
 
 
12441
{
 
12442
#ifdef TRILIBRARY
 
12443
  int *nlist;
 
12444
  int index;
 
12445
#else /* not TRILIBRARY */
 
12446
  FILE *outfile;
 
12447
#endif /* not TRILIBRARY */
 
12448
  struct triedge triangleloop, trisym;
 
12449
  int elementnumber;
 
12450
  int neighbor1, neighbor2, neighbor3;
 
12451
  triangle ptr;                         /* Temporary variable used by sym(). */
 
12452
 
 
12453
#ifdef TRILIBRARY
 
12454
  if (!quiet) {
 
12455
    printf("Writing neighbors.\n");
 
12456
  }
 
12457
  /* Allocate memory for neighbors if necessary. */
 
12458
  if (*neighborlist == (int *) NULL) {
 
12459
    *neighborlist = (int *) malloc(triangles.items * 3 * sizeof(int));
 
12460
    if (*neighborlist == (int *) NULL) {
 
12461
      printf("Error:  Out of memory.\n");
 
12462
      exit(1);
 
12463
    }
 
12464
  }
 
12465
  nlist = *neighborlist;
 
12466
  index = 0;
 
12467
#else /* not TRILIBRARY */
 
12468
  if (!quiet) {
 
12469
    printf("Writing %s.\n", neighborfilename);
 
12470
  }
 
12471
  outfile = fopen(neighborfilename, "w");
 
12472
  if (outfile == (FILE *) NULL) {
 
12473
    printf("  Error:  Cannot create file %s.\n", neighborfilename);
 
12474
    exit(1);
 
12475
  }
 
12476
  /* Number of triangles, three edges per triangle. */
 
12477
  fprintf(outfile, "%ld  %d\n", triangles.items, 3);
 
12478
#endif /* not TRILIBRARY */
 
12479
 
 
12480
  traversalinit(&triangles);
 
12481
  triangleloop.tri = triangletraverse();
 
12482
  triangleloop.orient = 0;
 
12483
  elementnumber = firstnumber;
 
12484
  while (triangleloop.tri != (triangle *) NULL) {
 
12485
    * (int *) (triangleloop.tri + 6) = elementnumber;
 
12486
    triangleloop.tri = triangletraverse();
 
12487
    elementnumber++;
 
12488
  }
 
12489
  * (int *) (dummytri + 6) = -1;
 
12490
 
 
12491
  traversalinit(&triangles);
 
12492
  triangleloop.tri = triangletraverse();
 
12493
  elementnumber = firstnumber;
 
12494
  while (triangleloop.tri != (triangle *) NULL) {
 
12495
    triangleloop.orient = 1;
 
12496
    sym(triangleloop, trisym);
 
12497
    neighbor1 = * (int *) (trisym.tri + 6);
 
12498
    triangleloop.orient = 2;
 
12499
    sym(triangleloop, trisym);
 
12500
    neighbor2 = * (int *) (trisym.tri + 6);
 
12501
    triangleloop.orient = 0;
 
12502
    sym(triangleloop, trisym);
 
12503
    neighbor3 = * (int *) (trisym.tri + 6);
 
12504
#ifdef TRILIBRARY
 
12505
    nlist[index++] = neighbor1;
 
12506
    nlist[index++] = neighbor2;
 
12507
    nlist[index++] = neighbor3;
 
12508
#else /* not TRILIBRARY */
 
12509
    /* Triangle number, neighboring triangle numbers. */
 
12510
    fprintf(outfile, "%4d    %d  %d  %d\n", elementnumber,
 
12511
            neighbor1, neighbor2, neighbor3);
 
12512
#endif /* not TRILIBRARY */
 
12513
 
 
12514
    triangleloop.tri = triangletraverse();
 
12515
    elementnumber++;
 
12516
  }
 
12517
 
 
12518
#ifndef TRILIBRARY
 
12519
  finishfile(outfile, argc, argv);
 
12520
#endif /* TRILIBRARY */
 
12521
}
 
12522
 
 
12523
/*****************************************************************************/
 
12524
/*                                                                           */
 
12525
/*  writeoff()   Write the triangulation to an .off file.                    */
 
12526
/*                                                                           */
 
12527
/*  OFF stands for the Object File Format, a format used by the Geometry     */
 
12528
/*  Center's Geomview package.                                               */
 
12529
/*                                                                           */
 
12530
/*****************************************************************************/
 
12531
 
 
12532
#ifndef TRILIBRARY
 
12533
 
 
12534
void writeoff(offfilename, argc, argv)
 
12535
char *offfilename;
 
12536
int argc;
 
12537
char **argv;
 
12538
{
 
12539
  FILE *outfile;
 
12540
  struct triedge triangleloop;
 
12541
  point pointloop;
 
12542
  point p1, p2, p3;
 
12543
 
 
12544
  if (!quiet) {
 
12545
    printf("Writing %s.\n", offfilename);
 
12546
  }
 
12547
  outfile = fopen(offfilename, "w");
 
12548
  if (outfile == (FILE *) NULL) {
 
12549
    printf("  Error:  Cannot create file %s.\n", offfilename);
 
12550
    exit(1);
 
12551
  }
 
12552
  /* Number of points, triangles, and edges. */
 
12553
  fprintf(outfile, "OFF\n%ld  %ld  %ld\n", points.items, triangles.items,
 
12554
          edges);
 
12555
 
 
12556
  /* Write the points. */
 
12557
  traversalinit(&points);
 
12558
  pointloop = pointtraverse();
 
12559
  while (pointloop != (point) NULL) {
 
12560
    /* The "0.0" is here because the OFF format uses 3D coordinates. */
 
12561
    fprintf(outfile, " %.17g  %.17g  %.17g\n", pointloop[0],
 
12562
            pointloop[1], 0.0);
 
12563
    pointloop = pointtraverse();
 
12564
  }
 
12565
 
 
12566
  /* Write the triangles. */
 
12567
  traversalinit(&triangles);
 
12568
  triangleloop.tri = triangletraverse();
 
12569
  triangleloop.orient = 0;
 
12570
  while (triangleloop.tri != (triangle *) NULL) {
 
12571
    org(triangleloop, p1);
 
12572
    dest(triangleloop, p2);
 
12573
    apex(triangleloop, p3);
 
12574
    /* The "3" means a three-vertex polygon. */
 
12575
    fprintf(outfile, " 3   %4d  %4d  %4d\n", pointmark(p1) - 1,
 
12576
            pointmark(p2) - 1, pointmark(p3) - 1);
 
12577
    triangleloop.tri = triangletraverse();
 
12578
  }
 
12579
  finishfile(outfile, argc, argv);
 
12580
}
 
12581
 
 
12582
#endif /* not TRILIBRARY */
 
12583
 
 
12584
/**                                                                         **/
 
12585
/**                                                                         **/
 
12586
/********* File I/O routines end here                                *********/
 
12587
 
 
12588
/*****************************************************************************/
 
12589
/*                                                                           */
 
12590
/*  quality_statistics()   Print statistics about the quality of the mesh.   */
 
12591
/*                                                                           */
 
12592
/*****************************************************************************/
 
12593
 
 
12594
void quality_statistics()
 
12595
{
 
12596
  struct triedge triangleloop;
 
12597
  point p[3];
 
12598
  REAL cossquaretable[8];
 
12599
  REAL ratiotable[16];
 
12600
  REAL dx[3], dy[3];
 
12601
  REAL edgelength[3];
 
12602
  REAL dotproduct;
 
12603
  REAL cossquare;
 
12604
  REAL triarea;
 
12605
  REAL shortest, longest;
 
12606
  REAL trilongest2;
 
12607
  REAL smallestarea, biggestarea;
 
12608
  REAL triminaltitude2;
 
12609
  REAL minaltitude;
 
12610
  REAL triaspect2;
 
12611
  REAL worstaspect;
 
12612
  REAL smallestangle, biggestangle;
 
12613
  REAL radconst, degconst;
 
12614
  int angletable[18];
 
12615
  int aspecttable[16];
 
12616
  int aspectindex;
 
12617
  int tendegree;
 
12618
  int acutebiggest;
 
12619
  int i, ii, j, k;
 
12620
 
 
12621
  printf("Mesh quality statistics:\n\n");
 
12622
  radconst = PI / 18.0;
 
12623
  degconst = 180.0 / PI;
 
12624
  for (i = 0; i < 8; i++) {
 
12625
    cossquaretable[i] = cos(radconst * (REAL) (i + 1));
 
12626
    cossquaretable[i] = cossquaretable[i] * cossquaretable[i];
 
12627
  }
 
12628
  for (i = 0; i < 18; i++) {
 
12629
    angletable[i] = 0;
 
12630
  }
 
12631
 
 
12632
  ratiotable[0]  =      1.5;      ratiotable[1]  =     2.0;
 
12633
  ratiotable[2]  =      2.5;      ratiotable[3]  =     3.0;
 
12634
  ratiotable[4]  =      4.0;      ratiotable[5]  =     6.0;
 
12635
  ratiotable[6]  =     10.0;      ratiotable[7]  =    15.0;
 
12636
  ratiotable[8]  =     25.0;      ratiotable[9]  =    50.0;
 
12637
  ratiotable[10] =    100.0;      ratiotable[11] =   300.0;
 
12638
  ratiotable[12] =   1000.0;      ratiotable[13] = 10000.0;
 
12639
  ratiotable[14] = 100000.0;      ratiotable[15] =     0.0;
 
12640
  for (i = 0; i < 16; i++) {
 
12641
    aspecttable[i] = 0;
 
12642
  }
 
12643
 
 
12644
  worstaspect = 0.0;
 
12645
  minaltitude = xmax - xmin + ymax - ymin;
 
12646
  minaltitude = minaltitude * minaltitude;
 
12647
  shortest = minaltitude;
 
12648
  longest = 0.0;
 
12649
  smallestarea = minaltitude;
 
12650
  biggestarea = 0.0;
 
12651
  worstaspect = 0.0;
 
12652
  smallestangle = 0.0;
 
12653
  biggestangle = 2.0;
 
12654
  acutebiggest = 1;
 
12655
 
 
12656
  traversalinit(&triangles);
 
12657
  triangleloop.tri = triangletraverse();
 
12658
  triangleloop.orient = 0;
 
12659
  while (triangleloop.tri != (triangle *) NULL) {
 
12660
    org(triangleloop, p[0]);
 
12661
    dest(triangleloop, p[1]);
 
12662
    apex(triangleloop, p[2]);
 
12663
    trilongest2 = 0.0;
 
12664
 
 
12665
    for (i = 0; i < 3; i++) {
 
12666
      j = plus1mod3[i];
 
12667
      k = minus1mod3[i];
 
12668
      dx[i] = p[j][0] - p[k][0];
 
12669
      dy[i] = p[j][1] - p[k][1];
 
12670
      edgelength[i] = dx[i] * dx[i] + dy[i] * dy[i];
 
12671
      if (edgelength[i] > trilongest2) {
 
12672
        trilongest2 = edgelength[i];
 
12673
      }
 
12674
      if (edgelength[i] > longest) {
 
12675
        longest = edgelength[i];
 
12676
      }
 
12677
      if (edgelength[i] < shortest) {
 
12678
        shortest = edgelength[i];
 
12679
      }
 
12680
    }
 
12681
 
 
12682
    triarea = counterclockwise(p[0], p[1], p[2]);
 
12683
    if (triarea < smallestarea) {
 
12684
      smallestarea = triarea;
 
12685
    }
 
12686
    if (triarea > biggestarea) {
 
12687
      biggestarea = triarea;
 
12688
    }
 
12689
    triminaltitude2 = triarea * triarea / trilongest2;
 
12690
    if (triminaltitude2 < minaltitude) {
 
12691
      minaltitude = triminaltitude2;
 
12692
    }
 
12693
    triaspect2 = trilongest2 / triminaltitude2;
 
12694
    if (triaspect2 > worstaspect) {
 
12695
      worstaspect = triaspect2;
 
12696
    }
 
12697
    aspectindex = 0;
 
12698
    while ((triaspect2 > ratiotable[aspectindex] * ratiotable[aspectindex])
 
12699
           && (aspectindex < 15)) {
 
12700
      aspectindex++;
 
12701
    }
 
12702
    aspecttable[aspectindex]++;
 
12703
 
 
12704
    for (i = 0; i < 3; i++) {
 
12705
      j = plus1mod3[i];
 
12706
      k = minus1mod3[i];
 
12707
      dotproduct = dx[j] * dx[k] + dy[j] * dy[k];
 
12708
      cossquare = dotproduct * dotproduct / (edgelength[j] * edgelength[k]);
 
12709
      tendegree = 8;
 
12710
      for (ii = 7; ii >= 0; ii--) {
 
12711
        if (cossquare > cossquaretable[ii]) {
 
12712
          tendegree = ii;
 
12713
        }
 
12714
      }
 
12715
      if (dotproduct <= 0.0) {
 
12716
        angletable[tendegree]++;
 
12717
        if (cossquare > smallestangle) {
 
12718
          smallestangle = cossquare;
 
12719
        }
 
12720
        if (acutebiggest && (cossquare < biggestangle)) {
 
12721
          biggestangle = cossquare;
 
12722
        }
 
12723
      } else {
 
12724
        angletable[17 - tendegree]++;
 
12725
        if (acutebiggest || (cossquare > biggestangle)) {
 
12726
          biggestangle = cossquare;
 
12727
          acutebiggest = 0;
 
12728
        }
 
12729
      }
 
12730
    }
 
12731
    triangleloop.tri = triangletraverse();
 
12732
  }
 
12733
 
 
12734
  shortest = sqrt(shortest);
 
12735
  longest = sqrt(longest);
 
12736
  minaltitude = sqrt(minaltitude);
 
12737
  worstaspect = sqrt(worstaspect);
 
12738
  smallestarea *= 2.0;
 
12739
  biggestarea *= 2.0;
 
12740
  if (smallestangle >= 1.0) {
 
12741
    smallestangle = 0.0;
 
12742
  } else {
 
12743
    smallestangle = degconst * acos(sqrt(smallestangle));
 
12744
  }
 
12745
  if (biggestangle >= 1.0) {
 
12746
    biggestangle = 180.0;
 
12747
  } else {
 
12748
    if (acutebiggest) {
 
12749
      biggestangle = degconst * acos(sqrt(biggestangle));
 
12750
    } else {
 
12751
      biggestangle = 180.0 - degconst * acos(sqrt(biggestangle));
 
12752
    }
 
12753
  }
 
12754
 
 
12755
  printf("  Smallest area: %16.5g   |  Largest area: %16.5g\n",
 
12756
         smallestarea, biggestarea);
 
12757
  printf("  Shortest edge: %16.5g   |  Longest edge: %16.5g\n",
 
12758
         shortest, longest);
 
12759
  printf("  Shortest altitude: %12.5g   |  Largest aspect ratio: %8.5g\n\n",
 
12760
         minaltitude, worstaspect);
 
12761
  printf("  Aspect ratio histogram:\n");
 
12762
  printf("  1.1547 - %-6.6g    :  %8d    | %6.6g - %-6.6g     :  %8d\n",
 
12763
         ratiotable[0], aspecttable[0], ratiotable[7], ratiotable[8],
 
12764
         aspecttable[8]);
 
12765
  for (i = 1; i < 7; i++) {
 
12766
    printf("  %6.6g - %-6.6g    :  %8d    | %6.6g - %-6.6g     :  %8d\n",
 
12767
           ratiotable[i - 1], ratiotable[i], aspecttable[i],
 
12768
           ratiotable[i + 7], ratiotable[i + 8], aspecttable[i + 8]);
 
12769
  }
 
12770
  printf("  %6.6g - %-6.6g    :  %8d    | %6.6g -            :  %8d\n",
 
12771
         ratiotable[6], ratiotable[7], aspecttable[7], ratiotable[14],
 
12772
         aspecttable[15]);
 
12773
  printf(
 
12774
"  (Triangle aspect ratio is longest edge divided by shortest altitude)\n\n");
 
12775
  printf("  Smallest angle: %15.5g   |  Largest angle: %15.5g\n\n",
 
12776
         smallestangle, biggestangle);
 
12777
  printf("  Angle histogram:\n");
 
12778
  for (i = 0; i < 9; i++) {
 
12779
    printf("    %3d - %3d degrees:  %8d    |    %3d - %3d degrees:  %8d\n",
 
12780
           i * 10, i * 10 + 10, angletable[i],
 
12781
           i * 10 + 90, i * 10 + 100, angletable[i + 9]);
 
12782
  }
 
12783
  printf("\n");
 
12784
}
 
12785
 
 
12786
/*****************************************************************************/
 
12787
/*                                                                           */
 
12788
/*  statistics()   Print all sorts of cool facts.                            */
 
12789
/*                                                                           */
 
12790
/*****************************************************************************/
 
12791
 
 
12792
void statistics()
 
12793
{
 
12794
  printf("\nStatistics:\n\n");
 
12795
  printf("  Input points: %d\n", inpoints);
 
12796
  if (refine) {
 
12797
    printf("  Input triangles: %d\n", inelements);
 
12798
  }
 
12799
  if (poly) {
 
12800
    printf("  Input segments: %d\n", insegments);
 
12801
    if (!refine) {
 
12802
      printf("  Input holes: %d\n", holes);
 
12803
    }
 
12804
  }
 
12805
 
 
12806
  printf("\n  Mesh points: %ld\n", points.items);
 
12807
  printf("  Mesh triangles: %ld\n", triangles.items);
 
12808
  printf("  Mesh edges: %ld\n", edges);
 
12809
  if (poly || refine) {
 
12810
    printf("  Mesh boundary edges: %ld\n", hullsize);
 
12811
    printf("  Mesh segments: %ld\n\n", shelles.items);
 
12812
  } else {
 
12813
    printf("  Mesh convex hull edges: %ld\n\n", hullsize);
 
12814
  }
 
12815
  if (verbose) {
 
12816
    quality_statistics();
 
12817
    printf("Memory allocation statistics:\n\n");
 
12818
    printf("  Maximum number of points: %ld\n", points.maxitems);
 
12819
    printf("  Maximum number of triangles: %ld\n", triangles.maxitems);
 
12820
    if (shelles.maxitems > 0) {
 
12821
      printf("  Maximum number of segments: %ld\n", shelles.maxitems);
 
12822
    }
 
12823
    if (viri.maxitems > 0) {
 
12824
      printf("  Maximum number of viri: %ld\n", viri.maxitems);
 
12825
    }
 
12826
    if (badsegments.maxitems > 0) {
 
12827
      printf("  Maximum number of encroached segments: %ld\n",
 
12828
             badsegments.maxitems);
 
12829
    }
 
12830
    if (badtriangles.maxitems > 0) {
 
12831
      printf("  Maximum number of bad triangles: %ld\n",
 
12832
             badtriangles.maxitems);
 
12833
    }
 
12834
    if (splaynodes.maxitems > 0) {
 
12835
      printf("  Maximum number of splay tree nodes: %ld\n",
 
12836
             splaynodes.maxitems);
 
12837
    }
 
12838
    printf("  Approximate heap memory use (bytes): %ld\n\n",
 
12839
           points.maxitems * points.itembytes
 
12840
           + triangles.maxitems * triangles.itembytes
 
12841
           + shelles.maxitems * shelles.itembytes
 
12842
           + viri.maxitems * viri.itembytes
 
12843
           + badsegments.maxitems * badsegments.itembytes
 
12844
           + badtriangles.maxitems * badtriangles.itembytes
 
12845
           + splaynodes.maxitems * splaynodes.itembytes);
 
12846
 
 
12847
    printf("Algorithmic statistics:\n\n");
 
12848
    printf("  Number of incircle tests: %ld\n", incirclecount);
 
12849
    printf("  Number of orientation tests: %ld\n", counterclockcount);
 
12850
    if (hyperbolacount > 0) {
 
12851
      printf("  Number of right-of-hyperbola tests: %ld\n",
 
12852
             hyperbolacount);
 
12853
    }
 
12854
    if (circumcentercount > 0) {
 
12855
      printf("  Number of circumcenter computations: %ld\n",
 
12856
             circumcentercount);
 
12857
    }
 
12858
    if (circletopcount > 0) {
 
12859
      printf("  Number of circle top computations: %ld\n",
 
12860
             circletopcount);
 
12861
    }
 
12862
    printf("\n");
 
12863
  }
 
12864
}
 
12865
 
 
12866
/*****************************************************************************/
 
12867
/*                                                                           */
 
12868
/*  main() or triangulate()   Gosh, do everything.                           */
 
12869
/*                                                                           */
 
12870
/*  The sequence is roughly as follows.  Many of these steps can be skipped, */
 
12871
/*  depending on the command line switches.                                  */
 
12872
/*                                                                           */
 
12873
/*  - Initialize constants and parse the command line.                       */
 
12874
/*  - Read the points from a file and either                                 */
 
12875
/*    - triangulate them (no -r), or                                         */
 
12876
/*    - read an old mesh from files and reconstruct it (-r).                 */
 
12877
/*  - Insert the PSLG segments (-p), and possibly segments on the convex     */
 
12878
/*      hull (-c).                                                           */
 
12879
/*  - Read the holes (-p), regional attributes (-pA), and regional area      */
 
12880
/*      constraints (-pa).  Carve the holes and concavities, and spread the  */
 
12881
/*      regional attributes and area constraints.                            */
 
12882
/*  - Enforce the constraints on minimum angle (-q) and maximum area (-a).   */
 
12883
/*      Also enforce the conforming Delaunay property (-q and -a).           */
 
12884
/*  - Compute the number of edges in the resulting mesh.                     */
 
12885
/*  - Promote the mesh's linear triangles to higher order elements (-o).     */
 
12886
/*  - Write the output files and print the statistics.                       */
 
12887
/*  - Check the consistency and Delaunay property of the mesh (-C).          */
 
12888
/*                                                                           */
 
12889
/*****************************************************************************/
 
12890
 
 
12891
#ifdef TRILIBRARY
 
12892
 
 
12893
void triangulate(
 
12894
char *triswitches,
 
12895
struct triangulateio *in,
 
12896
struct triangulateio *out,
 
12897
struct triangulateio *vorout)
 
12898
 
 
12899
#else /* not TRILIBRARY */
 
12900
 
 
12901
int main(
 
12902
int argc,
 
12903
char **argv)
 
12904
 
 
12905
#endif /* not TRILIBRARY */
 
12906
 
 
12907
{
 
12908
  REAL *holearray;                                        /* Array of holes. */
 
12909
  REAL *regionarray;   /* Array of regional attributes and area constraints. */
 
12910
#ifndef TRILIBRARY
 
12911
  FILE *polyfile;
 
12912
#endif /* not TRILIBRARY */
 
12913
#ifndef NO_TIMER
 
12914
  /* Variables for timing the performance of Triangle.  The types are */
 
12915
  /*   defined in sys/time.h.                                         */
 
12916
  struct timeval tv0, tv1, tv2, tv3, tv4, tv5, tv6;
 
12917
  struct timezone tz;
 
12918
#endif /* NO_TIMER */
 
12919
 
 
12920
#ifndef NO_TIMER
 
12921
  gettimeofday(&tv0, &tz);
 
12922
#endif /* NO_TIMER */
 
12923
 
 
12924
  triangleinit();
 
12925
#ifdef TRILIBRARY
 
12926
  parsecommandline(1, &triswitches);
 
12927
#else /* not TRILIBRARY */
 
12928
  parsecommandline(argc, argv);
 
12929
#endif /* not TRILIBRARY */
 
12930
 
 
12931
#ifdef TRILIBRARY
 
12932
  transfernodes(in->pointlist, in->pointattributelist, in->pointmarkerlist,
 
12933
                in->numberofpoints, in->numberofpointattributes);
 
12934
#else /* not TRILIBRARY */
 
12935
  readnodes(innodefilename, inpolyfilename, &polyfile);
 
12936
#endif /* not TRILIBRARY */
 
12937
 
 
12938
#ifndef NO_TIMER
 
12939
  if (!quiet) {
 
12940
    gettimeofday(&tv1, &tz);
 
12941
  }
 
12942
#endif /* NO_TIMER */
 
12943
 
 
12944
#ifdef CDT_ONLY
 
12945
  hullsize = delaunay();                          /* Triangulate the points. */
 
12946
#else /* not CDT_ONLY */
 
12947
  if (refine) {
 
12948
    /* Read and reconstruct a mesh. */
 
12949
#ifdef TRILIBRARY
 
12950
    hullsize = reconstruct(in->trianglelist, in->triangleattributelist,
 
12951
                           in->trianglearealist, in->numberoftriangles,
 
12952
                           in->numberofcorners, in->numberoftriangleattributes,
 
12953
                           in->segmentlist, in->segmentmarkerlist,
 
12954
                           in->numberofsegments);
 
12955
#else /* not TRILIBRARY */
 
12956
    hullsize = reconstruct(inelefilename, areafilename, inpolyfilename,
 
12957
                           polyfile);
 
12958
#endif /* not TRILIBRARY */
 
12959
  } else {
 
12960
    hullsize = delaunay();                        /* Triangulate the points. */
 
12961
  }
 
12962
#endif /* not CDT_ONLY */
 
12963
 
 
12964
#ifndef NO_TIMER
 
12965
  if (!quiet) {
 
12966
    gettimeofday(&tv2, &tz);
 
12967
    if (refine) {
 
12968
      printf("Mesh reconstruction");
 
12969
    } else {
 
12970
      printf("Delaunay");
 
12971
    }
 
12972
    printf(" milliseconds:  %ld\n", 1000l * (tv2.tv_sec - tv1.tv_sec)
 
12973
           + (tv2.tv_usec - tv1.tv_usec) / 1000l);
 
12974
  }
 
12975
#endif /* NO_TIMER */
 
12976
 
 
12977
  /* Ensure that no point can be mistaken for a triangular bounding */
 
12978
  /*   box point in insertsite().                                   */
 
12979
  infpoint1 = (point) NULL;
 
12980
  infpoint2 = (point) NULL;
 
12981
  infpoint3 = (point) NULL;
 
12982
 
 
12983
  if (useshelles) {
 
12984
    checksegments = 1;                  /* Segments will be introduced next. */
 
12985
    if (!refine) {
 
12986
      /* Insert PSLG segments and/or convex hull segments. */
 
12987
#ifdef TRILIBRARY
 
12988
      insegments = formskeleton(in->segmentlist, in->segmentmarkerlist,
 
12989
                                in->numberofsegments);
 
12990
#else /* not TRILIBRARY */
 
12991
      insegments = formskeleton(polyfile, inpolyfilename);
 
12992
#endif /* not TRILIBRARY */
 
12993
    }
 
12994
  }
 
12995
 
 
12996
#ifndef NO_TIMER
 
12997
  if (!quiet) {
 
12998
    gettimeofday(&tv3, &tz);
 
12999
    if (useshelles && !refine) {
 
13000
      printf("Segment milliseconds:  %ld\n",
 
13001
             1000l * (tv3.tv_sec - tv2.tv_sec)
 
13002
             + (tv3.tv_usec - tv2.tv_usec) / 1000l);
 
13003
    }
 
13004
  }
 
13005
#endif /* NO_TIMER */
 
13006
 
 
13007
  if (poly) {
 
13008
#ifdef TRILIBRARY
 
13009
    holearray = in->holelist;
 
13010
    holes = in->numberofholes;
 
13011
    regionarray = in->regionlist;
 
13012
    regions = in->numberofregions;
 
13013
#else /* not TRILIBRARY */
 
13014
    readholes(polyfile, inpolyfilename, &holearray, &holes,
 
13015
              &regionarray, &regions);
 
13016
#endif /* not TRILIBRARY */
 
13017
    if (!refine) {
 
13018
      /* Carve out holes and concavities. */
 
13019
      carveholes(holearray, holes, regionarray, regions);
 
13020
    }
 
13021
  } else {
 
13022
    /* Without a PSLG, there can be no holes or regional attributes   */
 
13023
    /*   or area constraints.  The following are set to zero to avoid */
 
13024
    /*   an accidental free() later.                                  */
 
13025
    holes = 0;
 
13026
    regions = 0;
 
13027
  }
 
13028
 
 
13029
#ifndef NO_TIMER
 
13030
  if (!quiet) {
 
13031
    gettimeofday(&tv4, &tz);
 
13032
    if (poly && !refine) {
 
13033
      printf("Hole milliseconds:  %ld\n", 1000l * (tv4.tv_sec - tv3.tv_sec)
 
13034
             + (tv4.tv_usec - tv3.tv_usec) / 1000l);
 
13035
    }
 
13036
  }
 
13037
#endif /* NO_TIMER */
 
13038
 
 
13039
#ifndef CDT_ONLY
 
13040
  if (quality) {
 
13041
    enforcequality();                 /* Enforce angle and area constraints. */
 
13042
  }
 
13043
#endif /* not CDT_ONLY */
 
13044
 
 
13045
#ifndef NO_TIMER
 
13046
  if (!quiet) {
 
13047
    gettimeofday(&tv5, &tz);
 
13048
#ifndef CDT_ONLY
 
13049
    if (quality) {
 
13050
      printf("Quality milliseconds:  %ld\n",
 
13051
             1000l * (tv5.tv_sec - tv4.tv_sec)
 
13052
             + (tv5.tv_usec - tv4.tv_usec) / 1000l);
 
13053
    }
 
13054
#endif /* not CDT_ONLY */
 
13055
  }
 
13056
#endif /* NO_TIMER */
 
13057
 
 
13058
  /* Compute the number of edges. */
 
13059
  edges = (3l * triangles.items + hullsize) / 2l;
 
13060
 
 
13061
  if (order > 1) {
 
13062
    highorder();             /* Promote elements to higher polynomial order. */
 
13063
  }
 
13064
  if (!quiet) {
 
13065
    printf("\n");
 
13066
  }
 
13067
 
 
13068
#ifdef TRILIBRARY
 
13069
  out->numberofpoints = points.items;
 
13070
  out->numberofpointattributes = nextras;
 
13071
  out->numberoftriangles = triangles.items;
 
13072
  out->numberofcorners = (order + 1) * (order + 2) / 2;
 
13073
  out->numberoftriangleattributes = eextras;
 
13074
  out->numberofedges = edges;
 
13075
  if (useshelles) {
 
13076
    out->numberofsegments = shelles.items;
 
13077
  } else {
 
13078
    out->numberofsegments = hullsize;
 
13079
  }
 
13080
  if (vorout != (struct triangulateio *) NULL) {
 
13081
    vorout->numberofpoints = triangles.items;
 
13082
    vorout->numberofpointattributes = nextras;
 
13083
    vorout->numberofedges = edges;
 
13084
  }
 
13085
#endif /* TRILIBRARY */
 
13086
  /* If not using iteration numbers, don't write a .node file if one was */
 
13087
  /*   read, because the original one would be overwritten!              */
 
13088
  if (nonodewritten || (noiterationnum && readnodefile)) {
 
13089
    if (!quiet) {
 
13090
#ifdef TRILIBRARY
 
13091
      printf("NOT writing points.\n");
 
13092
#else /* not TRILIBRARY */
 
13093
      printf("NOT writing a .node file.\n");
 
13094
#endif /* not TRILIBRARY */
 
13095
    }
 
13096
    numbernodes();                 /* We must remember to number the points. */
 
13097
  } else {
 
13098
#ifdef TRILIBRARY
 
13099
    writenodes(&out->pointlist, &out->pointattributelist,
 
13100
               &out->pointmarkerlist);
 
13101
#else /* not TRILIBRARY */
 
13102
    writenodes(outnodefilename, argc, argv);      /* Numbers the points too. */
 
13103
#endif /* TRILIBRARY */
 
13104
  }
 
13105
  if (noelewritten) {
 
13106
    if (!quiet) {
 
13107
#ifdef TRILIBRARY
 
13108
      printf("NOT writing triangles.\n");
 
13109
#else /* not TRILIBRARY */
 
13110
      printf("NOT writing an .ele file.\n");
 
13111
#endif /* not TRILIBRARY */
 
13112
    }
 
13113
  } else {
 
13114
#ifdef TRILIBRARY
 
13115
    writeelements(&out->trianglelist, &out->triangleattributelist);
 
13116
#else /* not TRILIBRARY */
 
13117
    writeelements(outelefilename, argc, argv);
 
13118
#endif /* not TRILIBRARY */
 
13119
  }
 
13120
  /* The -c switch (convex switch) causes a PSLG to be written */
 
13121
  /*   even if none was read.                                  */
 
13122
  if (poly || convex) {
 
13123
    /* If not using iteration numbers, don't overwrite the .poly file. */
 
13124
    if (nopolywritten || noiterationnum) {
 
13125
      if (!quiet) {
 
13126
#ifdef TRILIBRARY
 
13127
        printf("NOT writing segments.\n");
 
13128
#else /* not TRILIBRARY */
 
13129
        printf("NOT writing a .poly file.\n");
 
13130
#endif /* not TRILIBRARY */
 
13131
      }
 
13132
    } else {
 
13133
#ifdef TRILIBRARY
 
13134
      writepoly(&out->segmentlist, &out->segmentmarkerlist);
 
13135
      out->numberofholes = holes;
 
13136
      out->numberofregions = regions;
 
13137
      if (poly) {
 
13138
        out->holelist = in->holelist;
 
13139
        out->regionlist = in->regionlist;
 
13140
      } else {
 
13141
        out->holelist = (REAL *) NULL;
 
13142
        out->regionlist = (REAL *) NULL;
 
13143
      }
 
13144
#else /* not TRILIBRARY */
 
13145
      writepoly(outpolyfilename, holearray, holes, regionarray, regions,
 
13146
                argc, argv);
 
13147
#endif /* not TRILIBRARY */
 
13148
    }
 
13149
  }
 
13150
#ifndef TRILIBRARY
 
13151
#ifndef CDT_ONLY
 
13152
  if (regions > 0) {
 
13153
    free(regionarray);
 
13154
  }
 
13155
#endif /* not CDT_ONLY */
 
13156
  if (holes > 0) {
 
13157
    free(holearray);
 
13158
  }
 
13159
  if (geomview) {
 
13160
    writeoff(offfilename, argc, argv);
 
13161
  }
 
13162
#endif /* not TRILIBRARY */
 
13163
  if (edgesout) {
 
13164
#ifdef TRILIBRARY
 
13165
    writeedges(&out->edgelist, &out->edgemarkerlist);
 
13166
#else /* not TRILIBRARY */
 
13167
    writeedges(edgefilename, argc, argv);
 
13168
#endif /* not TRILIBRARY */
 
13169
  }
 
13170
  if (voronoi) {
 
13171
#ifdef TRILIBRARY
 
13172
    writevoronoi(&vorout->pointlist, &vorout->pointattributelist,
 
13173
                 &vorout->pointmarkerlist, &vorout->edgelist,
 
13174
                 &vorout->edgemarkerlist, &vorout->normlist);
 
13175
#else /* not TRILIBRARY */
 
13176
    writevoronoi(vnodefilename, vedgefilename, argc, argv);
 
13177
#endif /* not TRILIBRARY */
 
13178
  }
 
13179
  if (neighbors) {
 
13180
#ifdef TRILIBRARY
 
13181
    writeneighbors(&out->neighborlist);
 
13182
#else /* not TRILIBRARY */
 
13183
    writeneighbors(neighborfilename, argc, argv);
 
13184
#endif /* not TRILIBRARY */
 
13185
  }
 
13186
 
 
13187
  if (!quiet) {
 
13188
#ifndef NO_TIMER
 
13189
    gettimeofday(&tv6, &tz);
 
13190
    printf("\nOutput milliseconds:  %ld\n",
 
13191
           1000l * (tv6.tv_sec - tv5.tv_sec)
 
13192
           + (tv6.tv_usec - tv5.tv_usec) / 1000l);
 
13193
    printf("Total running milliseconds:  %ld\n",
 
13194
           1000l * (tv6.tv_sec - tv0.tv_sec)
 
13195
           + (tv6.tv_usec - tv0.tv_usec) / 1000l);
 
13196
#endif /* NO_TIMER */
 
13197
 
 
13198
    statistics();
 
13199
  }
 
13200
 
 
13201
#ifndef REDUCED
 
13202
  if (docheck) {
 
13203
    checkmesh();
 
13204
    checkdelaunay();
 
13205
  }
 
13206
#endif /* not REDUCED */
 
13207
 
 
13208
  triangledeinit();
 
13209
#ifndef TRILIBRARY
 
13210
  return 0;
 
13211
#endif /* not TRILIBRARY */
 
13212
}
 
13213
 
 
13214
//parsecommandline(