3
/*****************************************************************************/
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" */
13
/* A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator. */
20
/* Jonathan Richard Shewchuk */
21
/* School of Computer Science */
22
/* Carnegie Mellon University */
23
/* 5000 Forbes Avenue */
24
/* Pittsburgh, Pennsylvania 15213-3891 */
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.) */
41
/* Hypertext instructions for Triangle are available on the Web at */
43
/* http://www.cs.cmu.edu/~quake/triangle.html */
45
/* Some of the references listed below are marked [*]. These are available */
46
/* for downloading from the Web page */
48
/* http://www.cs.cmu.edu/~quake/triangle.research.html */
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. [*] */
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. */
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. [*] */
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. */
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. */
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. */
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. */
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 */
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. */
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.) */
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 */
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. */
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. */
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. */
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. */
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. */
181
/*****************************************************************************/
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. */
187
/* For double precision (which will allow you to refine meshes to a smaller */
188
/* edge length), leave SINGLE undefined. */
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. */
201
#else /* not SINGLE */
203
#endif /* not SINGLE */
205
/* If yours is not a Unix system, define the NO_TIMER compiler switch to */
206
/* remove the Unix-specific timing code. */
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. */
216
/* #define SELF_CHECK */
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. */
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. */
233
/* #define REDUCED */
234
/* #define CDT_ONLY */
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. */
242
/* To try this out, write "#define INEXACT volatile" below. Normally, */
243
/* however, INEXACT should be defined to be nothing. ("#define INEXACT".) */
245
#define INEXACT /* Nothing */
246
/* #define INEXACT volatile */
248
/* Maximum number of characters in a file name (including the null). */
250
#define FILENAMESIZE 512
252
/* Maximum number of characters in a line read from a file (including the */
255
#define INPUTLINESIZE 512
257
/* For efficiency, a variety of data structures are allocated in bulk. The */
258
/* following constants determine how many of each structure is allocated */
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
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. */
276
#define DEADPOINT -1073741824
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. */
284
/* Two constants for algorithms based on random sampling. Both constants */
285
/* have been chosen empirically to optimize their respective algorithms. */
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
295
/* A number that speaks for itself, every kissable digit. */
297
#define PI 3.141592653589793238462643383279502884197169399375105820974944592308
301
#define SQUAREROOTTWO 1.4142135623730950488016887242096980785696718753769480732
303
/* And here's one for those of you who are intimidated by math. */
305
#define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
311
#include <sys/time.h>
312
#endif /* NO_TIMER */
314
#include "triangle.h"
315
#endif /* TRILIBRARY */
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. */
324
extern void *malloc();
327
extern double strtod();
328
extern long strtol();
329
#endif /* _STDLIB_H_ */
331
/* A few forward declarations. */
333
typedef struct memorypool memorypool;
334
void poolrestart(memorypool *pool);
338
#endif /* not TRILIBRARY */
340
/* Labels that signify whether a record consists primarily of pointers or of */
341
/* floating-point words. Used to make decisions about data alignment. */
343
enum wordtype {POINTER, FLOATINGPOINT};
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. */
349
enum locateresult {INTRIANGLE, ONEDGE, ONVERTEX, OUTSIDE};
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. */
356
enum insertsiteresult {SUCCESSFULPOINT, ENCROACHINGPOINT, VIOLATINGPOINT,
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. */
364
enum finddirectionresult {WITHIN, LEFTCOLLINEAR, RIGHTCOLLINEAR};
366
/* Labels that signify the result of the circumcenter computation routine. */
367
/* The return value indicates which edge of the triangle is shortest. */
369
enum circumcenterresult {OPPOSITEORG, OPPOSITEDEST, OPPOSITEAPEX};
371
/*****************************************************************************/
373
/* The basic mesh data structures */
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. */
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. */
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. */
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. */
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. */
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 */
423
/* Like triangles, shell edges maintain information about the relative */
424
/* orientation of neighboring objects. */
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. */
439
/*****************************************************************************/
441
/*****************************************************************************/
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. */
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.) */
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. */
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. */
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. */
474
/*****************************************************************************/
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. */
485
typedef REAL **triangle; /* Really: typedef triangle *triangle */
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. */
492
typedef struct triedge {
494
int orient; /* Ranges from 0 to 2. */
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. */
501
typedef REAL **shelle; /* Really: typedef shelle *shelle */
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. */
510
int shorient; /* Ranges from 0 to 1. */
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 */
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. */
524
struct edge encsegment; /* An encroached segment. */
525
point segorg, segdest; /* The two vertices. */
526
struct badsegment *nextsegment; /* Pointer to next encroached segment. */
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. */
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. */
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'. */
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. */
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. */
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. */
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. */
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. */
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. */
598
VOID **firstblock, **nowblock;
603
enum wordtype itemwordtype;
605
int itembytes, itemwords;
607
long items, maxitems;
608
int unallocateditems;
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. */
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;
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. */
627
struct badface *queuefront[64];
628
struct badface **queuetail[64];
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. */
654
REAL splitter; /* Used to split REAL factors for exact multiplication. */
655
REAL epsilon; /* Floating-point machine epsilon. */
657
REAL ccwerrboundA, ccwerrboundB, ccwerrboundC;
658
REAL iccerrboundA, iccerrboundB, iccerrboundC;
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. */
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. */
695
/* Read the instructions to find out the meaning of these switches. */
697
int poly, refine, quality, vararea, fixedarea, regionattrib, convex;
699
int edgesout, voronoi, neighbors, geomview;
700
int nobound, nopolywritten, nonodewritten, noelewritten, noiterationnum;
701
int noholes, noexact;
702
int incremental, sweepline, dwyer;
709
int steiner, steinerleft;
710
REAL minangle, goodangle;
713
/* Variables for file names. */
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 */
730
/* Triangular bounding box points. */
732
point infpoint1, infpoint2, infpoint3;
734
/* Pointer to the `triangle' that occupies all of "outer space". */
737
triangle *dummytribase; /* Keep base address so we can free() it later. */
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 */
744
shelle *dummyshbase; /* Keep base address so we can free() it later. */
746
/* Pointer to a recently visited triangle. Improves point location if */
747
/* proximate points are inserted sequentially. */
749
struct triedge recenttri;
751
/*****************************************************************************/
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.*/
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. */
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. */
773
/*****************************************************************************/
775
/********* Mesh manipulation primitives begin here *********/
779
/* Fast lookup arrays to speed some of the mesh manipulation primitives. */
781
int plus1mod3[3] = {1, 2, 0};
782
int minus1mod3[3] = {2, 0, 1};
784
/********* Primitives for triangles *********/
788
/* decode() converts a pointer to an oriented triangle. The orientation is */
789
/* extracted from the two least significant bits of the pointer. */
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)
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.*/
800
#define encode(triedge) \
801
(triangle) ((unsigned long) (triedge).tri | (unsigned long) (triedge).orient)
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. */
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. */
811
#define sym(triedge1, triedge2) \
812
ptr = (triedge1).tri[(triedge1).orient]; \
813
decode(ptr, triedge2);
815
#define symself(triedge) \
816
ptr = (triedge).tri[(triedge).orient]; \
817
decode(ptr, triedge);
819
/* lnext() finds the next edge (counterclockwise) of a triangle. */
821
#define lnext(triedge1, triedge2) \
822
(triedge2).tri = (triedge1).tri; \
823
(triedge2).orient = plus1mod3[(triedge1).orient]
825
#define lnextself(triedge) \
826
(triedge).orient = plus1mod3[(triedge).orient]
828
/* lprev() finds the previous edge (clockwise) of a triangle. */
830
#define lprev(triedge1, triedge2) \
831
(triedge2).tri = (triedge1).tri; \
832
(triedge2).orient = minus1mod3[(triedge1).orient]
834
#define lprevself(triedge) \
835
(triedge).orient = minus1mod3[(triedge).orient]
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. */
841
#define onext(triedge1, triedge2) \
842
lprev(triedge1, triedge2); \
845
#define onextself(triedge) \
846
lprevself(triedge); \
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. */
853
#define oprev(triedge1, triedge2) \
854
sym(triedge1, triedge2); \
857
#define oprevself(triedge) \
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. */
865
#define dnext(triedge1, triedge2) \
866
sym(triedge1, triedge2); \
869
#define dnextself(triedge) \
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. */
877
#define dprev(triedge1, triedge2) \
878
lnext(triedge1, triedge2); \
881
#define dprevself(triedge) \
882
lnextself(triedge); \
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.) */
889
#define rnext(triedge1, triedge2) \
890
sym(triedge1, triedge2); \
891
lnextself(triedge2); \
894
#define rnextself(triedge) \
896
lnextself(triedge); \
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.) */
903
#define rprev(triedge1, triedge2) \
904
sym(triedge1, triedge2); \
905
lprevself(triedge2); \
908
#define rprevself(triedge) \
910
lprevself(triedge); \
913
/* These primitives determine or set the origin, destination, or apex of a */
916
#define org(triedge, pointptr) \
917
pointptr = (point) (triedge).tri[plus1mod3[(triedge).orient] + 3]
919
#define dest(triedge, pointptr) \
920
pointptr = (point) (triedge).tri[minus1mod3[(triedge).orient] + 3]
922
#define apex(triedge, pointptr) \
923
pointptr = (point) (triedge).tri[(triedge).orient + 3]
925
#define setorg(triedge, pointptr) \
926
(triedge).tri[plus1mod3[(triedge).orient] + 3] = (triangle) pointptr
928
#define setdest(triedge, pointptr) \
929
(triedge).tri[minus1mod3[(triedge).orient] + 3] = (triangle) pointptr
931
#define setapex(triedge, pointptr) \
932
(triedge).tri[(triedge).orient + 3] = (triangle) pointptr
934
#define setvertices2null(triedge) \
935
(triedge).tri[3] = (triangle) NULL; \
936
(triedge).tri[4] = (triangle) NULL; \
937
(triedge).tri[5] = (triangle) NULL;
939
/* Bond two triangles together. */
941
#define bond(triedge1, triedge2) \
942
(triedge1).tri[(triedge1).orient] = encode(triedge2); \
943
(triedge2).tri[(triedge2).orient] = encode(triedge1)
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. */
950
#define dissolve(triedge) \
951
(triedge).tri[(triedge).orient] = (triangle) dummytri
953
/* Copy a triangle/edge handle. */
955
#define triedgecopy(triedge1, triedge2) \
956
(triedge2).tri = (triedge1).tri; \
957
(triedge2).orient = (triedge1).orient
959
/* Test for equality of triangle/edge handles. */
961
#define triedgeequal(triedge1, triedge2) \
962
(((triedge1).tri == (triedge2).tri) && \
963
((triedge1).orient == (triedge2).orient))
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.*/
968
#define infect(triedge) \
969
(triedge).tri[6] = (triangle) \
970
((unsigned long) (triedge).tri[6] | (unsigned long) 2l)
972
#define uninfect(triedge) \
973
(triedge).tri[6] = (triangle) \
974
((unsigned long) (triedge).tri[6] & ~ (unsigned long) 2l)
976
/* Test a triangle for viral infection. */
978
#define infected(triedge) \
979
(((unsigned long) (triedge).tri[6] & (unsigned long) 2l) != 0)
981
/* Check or set a triangle's attributes. */
983
#define elemattribute(triedge, attnum) \
984
((REAL *) (triedge).tri)[elemattribindex + (attnum)]
986
#define setelemattribute(triedge, attnum, value) \
987
((REAL *) (triedge).tri)[elemattribindex + (attnum)] = value
989
/* Check or set a triangle's maximum area bound. */
991
#define areabound(triedge) ((REAL *) (triedge).tri)[areaboundindex]
993
#define setareabound(triedge, value) \
994
((REAL *) (triedge).tri)[areaboundindex] = value
996
/********* Primitives for shell edges *********/
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. */
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)
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. */
1014
#define sencode(edge) \
1015
(shelle) ((unsigned long) (edge).sh | (unsigned long) (edge).shorient)
1017
/* ssym() toggles the orientation of a shell edge. */
1019
#define ssym(edge1, edge2) \
1020
(edge2).sh = (edge1).sh; \
1021
(edge2).shorient = 1 - (edge1).shorient
1023
#define ssymself(edge) \
1024
(edge).shorient = 1 - (edge).shorient
1026
/* spivot() finds the other shell edge (from the same segment) that shares */
1027
/* the same origin. */
1029
#define spivot(edge1, edge2) \
1030
sptr = (edge1).sh[(edge1).shorient]; \
1031
sdecode(sptr, edge2)
1033
#define spivotself(edge) \
1034
sptr = (edge).sh[(edge).shorient]; \
1037
/* snext() finds the next shell edge (from the same segment) in sequence; */
1038
/* one whose origin is the input shell edge's destination. */
1040
#define snext(edge1, edge2) \
1041
sptr = (edge1).sh[1 - (edge1).shorient]; \
1042
sdecode(sptr, edge2)
1044
#define snextself(edge) \
1045
sptr = (edge).sh[1 - (edge).shorient]; \
1048
/* These primitives determine or set the origin or destination of a shell */
1051
#define sorg(edge, pointptr) \
1052
pointptr = (point) (edge).sh[2 + (edge).shorient]
1054
#define sdest(edge, pointptr) \
1055
pointptr = (point) (edge).sh[3 - (edge).shorient]
1057
#define setsorg(edge, pointptr) \
1058
(edge).sh[2 + (edge).shorient] = (shelle) pointptr
1060
#define setsdest(edge, pointptr) \
1061
(edge).sh[3 - (edge).shorient] = (shelle) pointptr
1063
/* These primitives read or set a shell marker. Shell markers are used to */
1064
/* hold user boundary information. */
1066
#define mark(edge) (* (int *) ((edge).sh + 6))
1068
#define setmark(edge, value) \
1069
* (int *) ((edge).sh + 6) = value
1071
/* Bond two shell edges together. */
1073
#define sbond(edge1, edge2) \
1074
(edge1).sh[(edge1).shorient] = sencode(edge2); \
1075
(edge2).sh[(edge2).shorient] = sencode(edge1)
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. */
1080
#define sdissolve(edge) \
1081
(edge).sh[(edge).shorient] = (shelle) dummysh
1083
/* Copy a shell edge. */
1085
#define shellecopy(edge1, edge2) \
1086
(edge2).sh = (edge1).sh; \
1087
(edge2).shorient = (edge1).shorient
1089
/* Test for equality of shell edges. */
1091
#define shelleequal(edge1, edge2) \
1092
(((edge1).sh == (edge2).sh) && \
1093
((edge1).shorient == (edge2).shorient))
1095
/********* Primitives for interacting triangles and shell edges *********/
1099
/* tspivot() finds a shell edge abutting a triangle. */
1101
#define tspivot(triedge, edge) \
1102
sptr = (shelle) (triedge).tri[6 + (triedge).orient]; \
1105
/* stpivot() finds a triangle abutting a shell edge. It requires that the */
1106
/* variable `ptr' of type `triangle' be defined. */
1108
#define stpivot(edge, triedge) \
1109
ptr = (triangle) (edge).sh[4 + (edge).shorient]; \
1110
decode(ptr, triedge)
1112
/* Bond a triangle to a shell edge. */
1114
#define tsbond(triedge, edge) \
1115
(triedge).tri[6 + (triedge).orient] = (triangle) sencode(edge); \
1116
(edge).sh[4 + (edge).shorient] = (shelle) encode(triedge)
1118
/* Dissolve a bond (from the triangle side). */
1120
#define tsdissolve(triedge) \
1121
(triedge).tri[6 + (triedge).orient] = (triangle) dummysh
1123
/* Dissolve a bond (from the shell edge side). */
1125
#define stdissolve(edge) \
1126
(edge).sh[4 + (edge).shorient] = (shelle) dummytri
1128
/********* Primitives for points *********/
1132
#define pointmark(pt) ((int *) (pt))[pointmarkindex]
1134
#define setpointmark(pt, value) \
1135
((int *) (pt))[pointmarkindex] = value
1137
#define point2tri(pt) ((triangle *) (pt))[point2triindex]
1139
#define setpoint2tri(pt, value) \
1140
((triangle *) (pt))[point2triindex] = value
1144
/********* Mesh manipulation primitives end here *********/
1146
/********* User interaction routines begin here *********/
1150
/*****************************************************************************/
1152
/* syntax() Print list of command line switches. */
1154
/*****************************************************************************/
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 */
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 */
1174
printf(" -p Triangulates a Planar Straight Line Graph (.poly file).\n");
1176
printf(" -r Refines a previously generated mesh.\n");
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 */
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");
1198
printf(" -Y Suppresses boundary segment splitting.\n");
1199
printf(" -S Specifies maximum number of added Steiner points.\n");
1200
#endif /* not CDT_ONLY */
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");
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");
1219
#endif /* not TRILIBRARY */
1221
/*****************************************************************************/
1223
/* info() Print out complete instructions. */
1225
/*****************************************************************************/
1231
printf("Triangle\n");
1233
"A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\n");
1234
printf("Version 1.3\n\n");
1236
"Copyright 1996 Jonathan Richard Shewchuk (bugs/comments to jrs@cs.cmu.edu)\n"
1238
printf("School of Computer Science / Carnegie Mellon University\n");
1239
printf("5000 Forbes Avenue / Pittsburgh, Pennsylvania 15213-3891\n");
1241
"Created as part of the Archimedes project (tools for parallel FEM).\n");
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");
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 */
1251
"Triangle generates exact Delaunay triangulations, constrained Delaunay\n");
1253
"triangulations, and quality conforming Delaunay triangulations. The latter\n"
1256
"can be generated with no small angles, and are thus suitable for finite\n");
1258
"element analysis. If no command line switches are specified, your .node\n");
1260
"input file will be read, and the Delaunay triangulation will be returned in\n"
1262
printf(".node and .ele output files. The command syntax is:\n\n");
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 */
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 */
1277
"Underscores indicate that numbers may optionally follow certain switches;\n");
1279
"do not leave any space between a switch and its numeric parameter.\n");
1281
"input_file must be a file with extension .node, or extension .poly if the\n");
1283
"-p switch is used. If -r is used, you must supply .node and .ele files,\n");
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");
1289
" -p Reads a Planar Straight Line Graph (.poly file), which can specify\n"
1292
" points, segments, holes, and regional attributes and area\n");
1294
" constraints. Will generate a constrained Delaunay triangulation\n");
1296
" fitting the input; or, if -s, -q, or -a is used, a conforming\n");
1298
" Delaunay triangulation. If -p is not used, Triangle reads a .node\n"
1300
printf(" file by default.\n");
1302
" -r Refines a previously generated mesh. The mesh is read from a .node\n"
1305
" file and an .ele file. If -p is also used, a .poly file is read\n");
1307
" and used to constrain edges in the mesh. Further details on\n");
1308
printf(" refinement are given below.\n");
1310
" -q Quality mesh generation by Jim Ruppert's Delaunay refinement\n");
1312
" algorithm. Adds points to the mesh to ensure that no angles\n");
1314
" smaller than 20 degrees occur. An alternative minimum angle may be\n"
1317
" specified after the `q'. If the minimum angle is 20.7 degrees or\n");
1319
" smaller, the triangulation algorithm is theoretically guaranteed to\n"
1322
" terminate (assuming infinite precision arithmetic - Triangle may\n");
1324
" fail to terminate if you run out of precision). In practice, the\n");
1326
" algorithm often succeeds for minimum angles up to 33.8 degrees.\n");
1328
" For highly refined meshes, however, it may be necessary to reduce\n");
1330
" the minimum angle to well below 20 to avoid problems associated\n");
1332
" with insufficient floating-point precision. The specified angle\n");
1333
printf(" may include a decimal point.\n");
1335
" -a Imposes a maximum triangle area. If a number follows the `a', no\n");
1337
" triangle will be generated whose area is larger than that number.\n");
1339
" If no number is specified, an .area file (if -r is used) or .poly\n");
1341
" file (if -r is not used) specifies a number of maximum area\n");
1343
" constraints. An .area file contains a separate area constraint for\n"
1346
" each triangle, and is useful for refining a finite element mesh\n");
1348
" based on a posteriori error estimates. A .poly file can optionally\n"
1351
" contain an area constraint for each segment-bounded region, thereby\n"
1354
" enforcing triangle densities in a first triangulation. You can\n");
1356
" impose both a fixed area constraint and a varying area constraint\n");
1358
" by invoking the -a switch twice, once with and once without a\n");
1360
" number following. Each area specified may include a decimal point.\n"
1363
" -A Assigns an additional attribute to each triangle that identifies\n");
1365
" what segment-bounded region each triangle belongs to. Attributes\n");
1367
" are assigned to regions by the .poly file. If a region is not\n");
1369
" explicitly marked by the .poly file, triangles in that region are\n");
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");
1374
" -c Creates segments on the convex hull of the triangulation. If you\n");
1376
" are triangulating a point set, this switch causes a .poly file to\n");
1378
" be written, containing all edges in the convex hull. (By default,\n"
1381
" a .poly file is written only if a .poly file is read.) If you are\n"
1384
" triangulating a PSLG, this switch specifies that the interior of\n");
1386
" the convex hull of the PSLG should be triangulated. If you do not\n"
1389
" use this switch when triangulating a PSLG, it is assumed that you\n");
1391
" have identified the region to be triangulated by surrounding it\n");
1393
" with segments of the input PSLG. Beware: if you are not careful,\n"
1396
" this switch can cause the introduction of an extremely thin angle\n");
1398
" between a PSLG segment and a convex hull segment, which can cause\n");
1400
" overrefinement or failure if Triangle runs out of precision. If\n");
1402
" you are refining a mesh, the -c switch works differently; it\n");
1404
" generates the set of boundary edges of the mesh, rather than the\n");
1405
printf(" convex hull.\n");
1407
" -e Outputs (to an .edge file) a list of edges of the triangulation.\n");
1409
" -v Outputs the Voronoi diagram associated with the triangulation.\n");
1410
printf(" Does not attempt to detect degeneracies.\n");
1412
" -n Outputs (to a .neigh file) a list of triangles neighboring each\n");
1413
printf(" triangle.\n");
1415
" -g Outputs the mesh to an Object File Format (.off) file, suitable for\n"
1417
printf(" viewing with the Geometry Center's Geomview package.\n");
1419
" -B No boundary markers in the output .node, .poly, and .edge output\n");
1421
" files. See the detailed discussion of boundary markers below.\n");
1423
" -P No output .poly file. Saves disk space, but you lose the ability\n");
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");
1429
" -I No iteration numbers. Suppresses the output of .node and .poly\n");
1431
" files, so your input files won't be overwritten. (If your input is\n"
1434
" a .poly file only, a .node file will be written.) Cannot be used\n");
1436
" with the -r switch, because that would overwrite your input .ele\n");
1438
" file. Shouldn't be used with the -s, -q, or -a switch if you are\n");
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");
1444
" -X No exact arithmetic. Normally, Triangle uses exact floating-point\n"
1447
" arithmetic for certain tests if it thinks the inexact tests are not\n"
1450
" accurate enough. Exact arithmetic ensures the robustness of the\n");
1452
" triangulation algorithms, despite floating-point roundoff error.\n");
1454
" Disabling exact arithmetic with the -X switch will cause a small\n");
1456
" improvement in speed and create the possibility (albeit small) that\n"
1459
" Triangle will fail to produce a valid mesh. Not recommended.\n");
1461
" -z Numbers all items starting from zero (rather than one). Note that\n"
1464
" this switch is normally overrided by the value used to number the\n");
1466
" first point of the input .node or .poly file. However, this switch\n"
1468
printf(" is useful when calling Triangle from another program.\n");
1470
" -o2 Generates second-order subparametric elements with six nodes each.\n"
1473
" -Y No new points on the boundary. This switch is useful when the mesh\n"
1476
" boundary must be preserved so that it conforms to some adjacent\n");
1478
" mesh. Be forewarned that you will probably sacrifice some of the\n");
1480
" quality of the mesh; Triangle will try, but the resulting mesh may\n"
1483
" contain triangles of poor aspect ratio. Works well if all the\n");
1485
" boundary points are closely spaced. Specify this switch twice\n");
1487
" (`-YY') to prevent all segment splitting, including internal\n");
1488
printf(" boundaries.\n");
1490
" -S Specifies the maximum number of Steiner points (points that are not\n"
1493
" in the input, but are added to meet the constraints of minimum\n");
1495
" angle and maximum area). The default is to allow an unlimited\n");
1497
" number. If you specify this switch with no number after it,\n");
1499
" the limit is set to zero. Triangle always adds points at segment\n");
1501
" intersections, even if it needs to use more points than the limit\n");
1503
" you set. When Triangle inserts segments by splitting (-s), it\n");
1505
" always adds enough points to ensure that all the segments appear in\n"
1508
" the triangulation, again ignoring the limit. Be forewarned that\n");
1510
" the -S switch may result in a conforming triangulation that is not\n"
1513
" truly Delaunay, because Triangle may be forced to stop adding\n");
1515
" points when the mesh is in a state where a segment is non-Delaunay\n"
1518
" and needs to be split. If so, Triangle will print a warning.\n");
1520
" -i Uses an incremental rather than divide-and-conquer algorithm to\n");
1522
" form a Delaunay triangulation. Try it if the divide-and-conquer\n");
1523
printf(" algorithm fails.\n");
1525
" -F Uses Steven Fortune's sweepline algorithm to form a Delaunay\n");
1527
" triangulation. Warning: does not use exact arithmetic for all\n");
1528
printf(" calculations. An exact result is not guaranteed.\n");
1530
" -l Uses only vertical cuts in the divide-and-conquer algorithm. By\n");
1532
" default, Triangle uses alternating vertical and horizontal cuts,\n");
1534
" which usually improve the speed except with point sets that are\n");
1536
" small or short and wide. This switch is primarily of theoretical\n");
1537
printf(" interest.\n");
1539
" -s Specifies that segments should be forced into the triangulation by\n"
1542
" recursively splitting them at their midpoints, rather than by\n");
1544
" generating a constrained Delaunay triangulation. Segment splitting\n"
1547
" is true to Ruppert's original algorithm, but can create needlessly\n"
1549
printf(" small triangles near external small features.\n");
1551
" -C Check the consistency of the final mesh. Uses exact arithmetic for\n"
1554
" checking, even if the -X switch is used. Useful if you suspect\n");
1555
printf(" Triangle is buggy.\n");
1557
" -Q Quiet: Suppresses all explanation of what Triangle is doing, unless\n"
1559
printf(" an error occurs.\n");
1561
" -V Verbose: Gives detailed information about what Triangle is doing.\n");
1563
" Add more `V's for increasing amount of detail. `-V' gives\n");
1565
" information on algorithmic progress and more detailed statistics.\n");
1567
" `-VV' gives point-by-point details, and will print so much that\n");
1569
" Triangle will run much more slowly. `-VVV' gives information only\n"
1571
printf(" a debugger could love.\n");
1572
printf(" -h Help: Displays these instructions.\n");
1574
printf("Definitions:\n");
1577
" A Delaunay triangulation of a point set is a triangulation whose vertices\n"
1580
" are the point set, having the property that no point in the point set\n");
1582
" falls in the interior of the circumcircle (circle that passes through all\n"
1584
printf(" three vertices) of any triangle in the triangulation.\n\n");
1586
" A Voronoi diagram of a point set is a subdivision of the plane into\n");
1588
" polygonal regions (some of which may be infinite), where each region is\n");
1590
" the set of points in the plane that are closer to some input point than\n");
1592
" to any other input point. (The Voronoi diagram is the geometric dual of\n"
1594
printf(" the Delaunay triangulation.)\n\n");
1596
" A Planar Straight Line Graph (PSLG) is a collection of points and\n");
1598
" segments. Segments are simply edges, whose endpoints are points in the\n");
1600
" PSLG. The file format for PSLGs (.poly files) is described below.\n");
1603
" A constrained Delaunay triangulation of a PSLG is similar to a Delaunay\n");
1605
" triangulation, but each PSLG segment is present as a single edge in the\n");
1607
" triangulation. (A constrained Delaunay triangulation is not truly a\n");
1608
printf(" Delaunay triangulation.)\n\n");
1610
" A conforming Delaunay triangulation of a PSLG is a true Delaunay\n");
1612
" triangulation in which each PSLG segment may have been subdivided into\n");
1614
" several edges by the insertion of additional points. These inserted\n");
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");
1620
" All files may contain comments prefixed by the character '#'. Points,\n");
1622
" triangles, edges, holes, and maximum area constraints must be numbered\n");
1624
" consecutively, starting from either 1 or 0. Whichever you choose, all\n");
1626
" input files must be consistent; if the nodes are numbered from 1, so must\n"
1629
" be all other objects. Triangle automatically detects your choice while\n");
1631
" reading the .node (or .poly) file. (When calling Triangle from another\n");
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");
1637
" First line: <# of points> <dimension (must be 2)> <# of attributes>\n");
1639
" <# of boundary markers (0 or 1)>\n"
1642
" Remaining lines: <point #> <x> <y> [attributes] [boundary marker]\n");
1645
" The attributes, which are typically floating-point values of physical\n");
1647
" quantities (such as mass or conductivity) associated with the nodes of\n"
1650
" a finite element mesh, are copied unchanged to the output mesh. If -s,\n"
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");
1656
" If the fourth entry of the first line is `1', the last column of the\n");
1658
" remainder of the file is assumed to contain boundary markers. Boundary\n"
1661
" markers are used to identify boundary points and points resting on PSLG\n"
1664
" segments; a complete description appears in a section below. The .node\n"
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");
1671
" First line: <# of triangles> <points per triangle> <# of attributes>\n");
1673
" Remaining lines: <triangle #> <point> <point> <point> ... [attributes]\n"
1677
" Points are indices into the corresponding .node file. The first three\n"
1680
" points are the corners, and are listed in counterclockwise order around\n"
1683
" each triangle. (The remaining points, if any, depend on the type of\n");
1685
" finite element used.) The attributes are just like those of .node\n");
1687
" files. Because there is no simple mapping from input to output\n");
1689
" triangles, an attempt is made to interpolate attributes, which may\n");
1691
" result in a good deal of diffusion of attributes among nearby triangles\n"
1694
" as the triangulation is refined. Diffusion does not occur across\n");
1696
" segments, so attributes used to identify segment-bounded regions remain\n"
1699
" intact. In output .ele files, all triangles have three points each\n");
1701
" unless the -o2 switch is used, in which case they have six, and the\n");
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");
1707
" First line: <# of points> <dimension (must be 2)> <# of attributes>\n");
1709
" <# of boundary markers (0 or 1)>\n"
1712
" Following lines: <point #> <x> <y> [attributes] [boundary marker]\n");
1713
printf(" One line: <# of segments> <# of boundary markers (0 or 1)>\n");
1715
" Following lines: <segment #> <endpoint> <endpoint> [boundary marker]\n");
1716
printf(" One line: <# of holes>\n");
1717
printf(" Following lines: <hole #> <x> <y>\n");
1719
" Optional line: <# of regional attributes and/or area constraints>\n");
1721
" Optional following lines: <constraint #> <x> <y> <attrib> <max area>\n");
1724
" A .poly file represents a PSLG, as well as some additional information.\n"
1727
" The first section lists all the points, and is identical to the format\n"
1730
" of .node files. <# of points> may be set to zero to indicate that the\n"
1733
" points are listed in a separate .node file; .poly files produced by\n");
1735
" Triangle always have this format. This has the advantage that a point\n"
1738
" set may easily be triangulated with or without segments. (The same\n");
1740
" effect can be achieved, albeit using more disk space, by making a copy\n"
1743
" of the .poly file with the extension .node; all sections of the file\n");
1744
printf(" but the first are ignored.)\n\n");
1746
" The second section lists the segments. Segments are edges whose\n");
1748
" presence in the triangulation is enforced. Each segment is specified\n");
1750
" by listing the indices of its two endpoints. This means that you must\n"
1753
" include its endpoints in the point list. If -s, -q, and -a are not\n");
1755
" selected, Triangle will produce a constrained Delaunay triangulation,\n");
1757
" in which each segment appears as a single edge in the triangulation.\n");
1759
" If -q or -a is selected, Triangle will produce a conforming Delaunay\n");
1761
" triangulation, in which segments may be subdivided into smaller edges.\n"
1763
printf(" Each segment, like each point, may have a boundary marker.\n\n");
1765
" The third section lists holes (and concavities, if -c is selected) in\n");
1767
" the triangulation. Holes are specified by identifying a point inside\n");
1769
" each hole. After the triangulation is formed, Triangle creates holes\n");
1771
" by eating triangles, spreading out from each hole point until its\n");
1773
" progress is blocked by PSLG segments; you must be careful to enclose\n");
1775
" each hole in segments, or your whole triangulation may be eaten away.\n");
1777
" If the two triangles abutting a segment are eaten, the segment itself\n");
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");
1782
" The optional fourth section lists regional attributes (to be assigned\n");
1784
" to all triangles in a region) and regional constraints on the maximum\n");
1786
" triangle area. Triangle will read this section only if the -A switch\n");
1788
" is used or the -a switch is used without a number following it, and the\n"
1791
" -r switch is not used. Regional attributes and area constraints are\n");
1793
" propagated in the same manner as holes; you specify a point for each\n");
1795
" attribute and/or constraint, and the attribute and/or constraint will\n");
1797
" affect the whole region (bounded by segments) containing the point. If\n"
1800
" two values are written on a line after the x and y coordinate, the\n");
1802
" former is assumed to be a regional attribute (but will only be applied\n"
1805
" if the -A switch is selected), and the latter is assumed to be a\n");
1807
" regional area constraint (but will only be applied if the -a switch is\n"
1810
" selected). You may also specify just one value after the coordinates,\n"
1813
" which can serve as both an attribute and an area constraint, depending\n"
1816
" on the choice of switches. If you are using the -A and -a switches\n");
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");
1821
" When a triangulation is created from a .poly file, you must either\n");
1823
" enclose the entire region to be triangulated in PSLG segments, or\n");
1825
" use the -c switch, which encloses the convex hull of the input point\n");
1827
" set. If you do not use the -c switch, Triangle will eat all triangles\n"
1830
" on the outer boundary that are not protected by segments; if you are\n");
1832
" not careful, your whole triangulation may be eaten away. If you do\n");
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");
1837
" An ideal PSLG has no intersecting segments, nor any points that lie\n");
1839
" upon segments (except, of course, the endpoints of each segment.) You\n"
1842
" aren't required to make your .poly files ideal, but you should be aware\n"
1845
" of what can go wrong. Segment intersections are relatively safe -\n");
1847
" Triangle will calculate the intersection points for you and add them to\n"
1850
" the triangulation - as long as your machine's floating-point precision\n"
1853
" doesn't become a problem. You are tempting the fates if you have three\n"
1856
" segments that cross at the same location, and expect Triangle to figure\n"
1859
" out where the intersection point is. Thanks to floating-point roundoff\n"
1862
" error, Triangle will probably decide that the three segments intersect\n"
1865
" at three different points, and you will find a minuscule triangle in\n");
1867
" your output - unless Triangle tries to refine the tiny triangle, uses\n");
1869
" up the last bit of machine precision, and fails to terminate at all.\n");
1871
" You're better off putting the intersection point in the input files,\n");
1873
" and manually breaking up each segment into two. Similarly, if you\n");
1875
" place a point at the middle of a segment, and hope that Triangle will\n");
1877
" break up the segment at that point, you might get lucky. On the other\n"
1880
" hand, Triangle might decide that the point doesn't lie precisely on the\n"
1883
" line, and you'll have a needle-sharp triangle in your output - or a lot\n"
1885
printf(" of tiny triangles if you're generating a quality mesh.\n\n");
1887
" When Triangle reads a .poly file, it also writes a .poly file, which\n");
1889
" includes all edges that are part of input segments. If the -c switch\n");
1891
" is used, the output .poly file will also include all of the edges on\n");
1893
" the convex hull. Hence, the output .poly file is useful for finding\n");
1895
" edges associated with input segments and setting boundary conditions in\n"
1898
" finite element simulations. More importantly, you will need it if you\n"
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");
1907
" An .area file associates with each triangle a maximum area that is used\n"
1910
" for mesh refinement. As with other file formats, every triangle must\n");
1912
" be represented, and they must be numbered consecutively. A triangle\n");
1914
" may be left unconstrained by assigning it a negative maximum area.\n");
1916
printf(" .edge files:\n");
1917
printf(" First line: <# of edges> <# of boundary markers (0 or 1)>\n");
1919
" Following lines: <edge #> <endpoint> <endpoint> [boundary marker]\n");
1922
" Endpoints are indices into the corresponding .node file. Triangle can\n"
1925
" produce .edge files (use the -e switch), but cannot read them. The\n");
1927
" optional column of boundary markers is suppressed by the -B switch.\n");
1930
" In Voronoi diagrams, one also finds a special kind of edge that is an\n");
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");
1936
" The `direction' is a floating-point vector that indicates the direction\n"
1938
printf(" of the infinite ray.\n\n");
1939
printf(" .neigh files:\n");
1941
" First line: <# of triangles> <# of neighbors per triangle (always 3)>\n"
1944
" Following lines: <triangle #> <neighbor> <neighbor> <neighbor>\n");
1947
" Neighbors are indices into the corresponding .ele file. An index of -1\n"
1950
" indicates a mesh boundary, and therefore no neighbor. Triangle can\n");
1952
" produce .neigh files (use the -n switch), but cannot read them.\n");
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");
1959
" Boundary markers are tags used mainly to identify which output points and\n"
1962
" edges are associated with which PSLG segment, and to identify which\n");
1964
" points and edges occur on a boundary of the triangulation. A common use\n"
1967
" is to determine where boundary conditions should be applied to a finite\n");
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");
1972
" The boundary marker associated with each segment in an output .poly file\n"
1974
printf(" or edge in an output .edge file is chosen as follows:\n");
1976
" - If an output edge is part or all of a PSLG segment with a nonzero\n");
1978
" boundary marker, then the edge is assigned the same marker.\n");
1980
" - Otherwise, if the edge occurs on a boundary of the triangulation\n");
1982
" (including boundaries of holes), then the edge is assigned the marker\n"
1984
printf(" one (1).\n");
1985
printf(" - Otherwise, the edge is assigned the marker zero (0).\n");
1987
" The boundary marker associated with each point in an output .node file is\n"
1989
printf(" chosen as follows:\n");
1991
" - If a point is assigned a nonzero boundary marker in the input file,\n");
1993
" then it is assigned the same marker in the output .node file.\n");
1995
" - Otherwise, if the point lies on a PSLG segment (including the\n");
1997
" segment's endpoints) with a nonzero boundary marker, then the point\n");
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");
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");
2007
" If you want Triangle to determine for you which points and edges are on\n");
2009
" the boundary, assign them the boundary marker zero (or use no markers at\n"
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");
2016
" Because Triangle can read and refine its own triangulations, input\n");
2018
" and output files have iteration numbers. For instance, Triangle might\n");
2020
" read the files mesh.3.node, mesh.3.ele, and mesh.3.poly, refine the\n");
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");
2025
" their iteration number is zero; hence, Triangle might read the file\n");
2027
" points.node, triangulate it, and produce the files points.1.node and\n");
2028
printf(" points.1.ele.\n\n");
2030
" Iteration numbers allow you to create a sequence of successively finer\n");
2032
" meshes suitable for multigrid methods. They also allow you to produce a\n"
2035
" sequence of meshes using error estimate-driven mesh refinement.\n");
2038
" If you're not using refinement or quality meshing, and you don't like\n");
2040
" iteration numbers, use the -I switch to disable them. This switch will\n");
2042
" also disable output of .node and .poly files to prevent your input files\n"
2045
" from being overwritten. (If the input is a .poly file that contains its\n"
2047
printf(" own points, a .node file will be written.)\n\n");
2048
printf("Examples of How to Use Triangle:\n\n");
2050
" `triangle dots' will read points from dots.node, and write their Delaunay\n"
2053
" triangulation to dots.1.node and dots.1.ele. (dots.1.node will be\n");
2055
" identical to dots.node.) `triangle -I dots' writes the triangulation to\n"
2058
" dots.ele instead. (No additional .node file is needed, so none is\n");
2059
printf(" written.)\n\n");
2061
" `triangle -pe object.1' will read a PSLG from object.1.poly (and possibly\n"
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");
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");
2070
" `triangle -pq31.5a.1 object' will read a PSLG from object.poly (and\n");
2072
" possibly object.node), generate a mesh whose angles are all greater than\n"
2075
" 31.5 degrees and whose triangles all have area smaller than 0.1, and\n");
2077
" write the mesh to object.1.node and object.1.ele. Each segment may have\n"
2080
" been broken up into multiple edges; the resulting constrained edges are\n");
2081
printf(" written to object.1.poly.\n\n");
2083
" Here is a sample file `box.poly' describing a square with a square hole:\n"
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");
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");
2108
printf(" 1 1.5 1.5\n\n");
2110
" Note that some segments are missing from the outer square, so one must\n");
2112
" use the `-c' switch. After `triangle -pqc box.poly', here is the output\n"
2115
" file `box.1.node', with twelve points. The last four points were added\n");
2117
" to meet the angle constraint. Points 1, 2, and 9 have markers from\n");
2119
" segment 1. Points 6 and 8 have markers from segment 4. All the other\n");
2121
" points but 4 have been marked to indicate that they lie on a boundary.\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");
2153
" Here is the output file `box.1.poly'. Note that segments have been added\n"
2156
" to represent the convex hull, and some segments have been split by newly\n"
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");
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");
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");
2180
" The -r switch causes a mesh (.node and .ele files) to be read and\n");
2182
" refined. If the -p switch is also used, a .poly file is read and used to\n"
2185
" specify edges that are constrained and cannot be eliminated (although\n");
2187
" they can be divided into smaller edges) by the refinement process.\n");
2190
" When you refine a mesh, you generally want to impose tighter quality\n");
2192
" constraints. One way to accomplish this is to use -q with a larger\n");
2194
" angle, or -a followed by a smaller area than you used to generate the\n");
2196
" mesh you are refining. Another way to do this is to create an .area\n");
2198
" file, which specifies a maximum area for each triangle, and use the -a\n");
2200
" switch (without a number following). Each triangle's area constraint is\n"
2203
" applied to that triangle. Area constraints tend to diffuse as the mesh\n");
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");
2208
" If you are refining a mesh composed of linear (three-node) elements, the\n"
2211
" output mesh will contain all the nodes present in the input mesh, in the\n"
2214
" same order, with new nodes added at the end of the .node file. However,\n"
2217
" there is no guarantee that each output element is contained in a single\n");
2219
" input element. Often, output elements will overlap two input elements,\n");
2221
" and input edges are not present in the output mesh. Hence, a sequence of\n"
2224
" refined meshes will form a hierarchy of nodes, but not a hierarchy of\n");
2226
" elements. If you a refining a mesh of higher-order elements, the\n");
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");
2231
" It is important to understand that maximum area constraints in .poly\n");
2233
" files are handled differently from those in .area files. A maximum area\n"
2236
" in a .poly file applies to the whole (segment-bounded) region in which a\n"
2239
" point falls, whereas a maximum area in an .area file applies to only one\n"
2242
" triangle. Area constraints in .poly files are used only when a mesh is\n");
2244
" first generated, whereas area constraints in .area files are used only to\n"
2247
" refine an existing mesh, and are typically based on a posteriori error\n");
2249
" estimates resulting from a finite element simulation on that mesh.\n");
2252
" `triangle -rq25 object.1' will read object.1.node and object.1.ele, then\n"
2255
" refine the triangulation to enforce a 25 degree minimum angle, and then\n");
2257
" write the refined triangulation to object.2.node and object.2.ele.\n");
2260
" `triangle -rpaa6.2 z.3' will read z.3.node, z.3.ele, z.3.poly, and\n");
2262
" z.3.area. After reconstructing the mesh and its segments, Triangle will\n"
2265
" refine the mesh so that no triangle has area greater than 6.2, and\n");
2267
" furthermore the triangles satisfy the maximum area constraints in\n");
2269
" z.3.area. The output is written to z.4.node, z.4.ele, and z.4.poly.\n");
2272
" The sequence `triangle -qa1 x', `triangle -rqa.3 x.1', `triangle -rqa.1\n");
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");
2278
" If the input is a point set (rather than a PSLG), Triangle produces its\n");
2280
" convex hull as a by-product in the output .poly file if you use the -c\n");
2282
" switch. There are faster algorithms for finding a two-dimensional convex\n"
2285
" hull than triangulation, of course, but this one comes for free. If the\n"
2288
" input is an unconstrained mesh (you are using the -r switch but not the\n");
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");
2294
" The -v switch produces a Voronoi diagram, in files suffixed .v.node and\n");
2296
" .v.edge. For example, `triangle -v points' will read points.node,\n");
2298
" produce its Delaunay triangulation in points.1.node and points.1.ele,\n");
2300
" and produce its Voronoi diagram in points.1.v.node and points.1.v.edge.\n");
2302
" The .v.node file contains a list of all Voronoi vertices, and the .v.edge\n"
2305
" file contains a list of all Voronoi edges, some of which may be infinite\n"
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");
2311
" This implementation does not use exact arithmetic to compute the Voronoi\n"
2314
" vertices, and does not check whether neighboring vertices are identical.\n"
2317
" Be forewarned that if the Delaunay triangulation is degenerate or\n");
2319
" near-degenerate, the Voronoi diagram may have duplicate points, crossing\n"
2322
" edges, or infinite rays whose direction vector is zero. Also, if you\n");
2324
" generate a constrained (as opposed to conforming) Delaunay triangulation,\n"
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");
2331
" You may wish to know which triangles are adjacent to a certain Delaunay\n");
2333
" edge in an .edge file, which Voronoi regions are adjacent to a certain\n");
2335
" Voronoi edge in a .v.edge file, or which Voronoi regions are adjacent to\n"
2338
" each other. All of this information can be found by cross-referencing\n");
2340
" output files with the recollection that the Delaunay triangulation and\n");
2341
printf(" the Voronoi diagrams are planar duals.\n\n");
2343
" Specifically, edge i of an .edge file is the dual of Voronoi edge i of\n");
2345
" the corresponding .v.edge file, and is rotated 90 degrees counterclock-\n");
2347
" wise from the Voronoi edge. Triangle j of an .ele file is the dual of\n");
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");
2352
" Hence, to find the triangles adjacent to a Delaunay edge, look at the\n");
2354
" vertices of the corresponding Voronoi edge; their dual triangles are on\n");
2356
" the left and right of the Delaunay edge, respectively. To find the\n");
2358
" Voronoi regions adjacent to a Voronoi edge, look at the endpoints of the\n"
2361
" corresponding Delaunay edge; their dual regions are on the right and left\n"
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");
2367
printf("Statistics:\n");
2370
" After generating a mesh, Triangle prints a count of the number of points,\n"
2373
" triangles, edges, boundary edges, and segments in the output mesh. If\n");
2375
" you've forgotten the statistics for an existing mesh, the -rNEP switches\n"
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");
2381
" The -V switch produces extended statistics, including a rough estimate\n");
2383
" of memory use and a histogram of triangle aspect ratios and angles in the\n"
2385
printf(" mesh.\n\n");
2386
printf("Exact Arithmetic:\n\n");
2388
" Triangle uses adaptive exact arithmetic to perform what computational\n");
2390
" geometers call the `orientation' and `incircle' tests. If the floating-\n"
2393
" point arithmetic of your machine conforms to the IEEE 754 standard (as\n");
2395
" most workstations do), and does not use extended precision internal\n");
2397
" registers, then your output is guaranteed to be an absolutely true\n");
2398
printf(" Delaunay or conforming Delaunay triangulation, roundoff error\n");
2400
" notwithstanding. The word `adaptive' implies that these arithmetic\n");
2402
" routines compute the result only to the precision necessary to guarantee\n"
2405
" correctness, so they are usually nearly as fast as their approximate\n");
2407
" counterparts. The exact tests can be disabled with the -X switch. On\n");
2409
" most inputs, this switch will reduce the computation time by about eight\n"
2412
" percent - it's not worth the risk. There are rare difficult inputs\n");
2414
" (having many collinear and cocircular points), however, for which the\n");
2416
" difference could be a factor of two. These are precisely the inputs most\n"
2418
printf(" likely to cause errors if you use the -X switch.\n\n");
2420
" Unfortunately, these routines don't solve every numerical problem. Exact\n"
2423
" arithmetic is not used to compute the positions of points, because the\n");
2425
" bit complexity of point coordinates would grow without bound. Hence,\n");
2427
" segment intersections aren't computed exactly; in very unusual cases,\n");
2429
" roundoff error in computing an intersection point might actually lead to\n"
2432
" an inverted triangle and an invalid triangulation. (This is one reason\n");
2434
" to compute your own intersection points in your .poly files.) Similarly,\n"
2437
" exact arithmetic is not used to compute the vertices of the Voronoi\n");
2438
printf(" diagram.\n\n");
2440
" Underflow and overflow can also cause difficulties; the exact arithmetic\n"
2443
" routines do not ameliorate out-of-bounds exponents, which can arise\n");
2445
" during the orientation and incircle tests. As a rule of thumb, you\n");
2447
" should ensure that your input values are within a range such that their\n");
2449
" third powers can be taken without underflow or overflow. Underflow can\n");
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");
2459
" If you're using a PSLG, you've probably failed to specify a proper set\n"
2462
" of bounding segments, or forgotten to use the -c switch. Or you may\n");
2464
" have placed a hole badly. To test these possibilities, try again with\n"
2467
" the -c and -O switches. Alternatively, all your input points may be\n");
2469
" collinear, in which case you can hardly expect to triangulate them.\n");
2471
printf(" `Triangle doesn't terminate, or just crashes.'\n");
2474
" Bad things can happen when triangles get so small that the distance\n");
2476
" between their vertices isn't much larger than the precision of your\n");
2478
" machine's arithmetic. If you've compiled Triangle for single-precision\n"
2481
" arithmetic, you might do better by recompiling it for double-precision.\n"
2484
" Then again, you might just have to settle for more lenient constraints\n"
2487
" on the minimum angle and the maximum area than you had planned.\n");
2490
" You can minimize precision problems by ensuring that the origin lies\n");
2492
" inside your point set, or even inside the densest part of your\n");
2494
" mesh. On the other hand, if you're triangulating an object whose x\n");
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");
2499
" Precision problems can occur covertly if the input PSLG contains two\n");
2501
" segments that meet (or intersect) at a very small angle, or if such an\n"
2504
" angle is introduced by the -c switch, which may occur if a point lies\n");
2506
" ever-so-slightly inside the convex hull, and is connected by a PSLG\n");
2508
" segment to a point on the convex hull. If you don't realize that a\n");
2510
" small angle is being formed, you might never discover why Triangle is\n");
2512
" crashing. To check for this possibility, use the -S switch (with an\n");
2514
" appropriate limit on the number of Steiner points, found by trial-and-\n"
2517
" error) to stop Triangle early, and view the output .poly file with\n");
2519
" Show Me (described below). Look carefully for small angles between\n");
2521
" segments; zoom in closely, as such segments might look like a single\n");
2522
printf(" segment from a distance.\n\n");
2524
" If some of the input values are too large, Triangle may suffer a\n");
2526
" floating exception due to overflow when attempting to perform an\n");
2528
" orientation or incircle test. (Read the section on exact arithmetic\n");
2530
" above.) Again, I recommend compiling Triangle for double (rather\n");
2531
printf(" than single) precision arithmetic.\n\n");
2533
" `The numbering of the output points doesn't match the input points.'\n");
2536
" You may have eaten some of your input points with a hole, or by placing\n"
2538
printf(" them outside the area enclosed by segments.\n\n");
2540
" `Triangle executes without incident, but when I look at the resulting\n");
2542
" mesh, it has overlapping triangles or other geometric inconsistencies.'\n");
2545
" If you select the -X switch, Triangle's divide-and-conquer Delaunay\n");
2547
" triangulation algorithm occasionally makes mistakes due to floating-\n");
2549
" point roundoff error. Although these errors are rare, don't use the -X\n"
2551
printf(" switch. If you still have problems, please report the bug.\n");
2554
" Strange things can happen if you've taken liberties with your PSLG. Do\n");
2556
" you have a point lying in the middle of a segment? Triangle sometimes\n");
2558
" copes poorly with that sort of thing. Do you want to lay out a collinear\n"
2561
" row of evenly spaced, segment-connected points? Have you simply defined\n"
2564
" one long segment connecting the leftmost point to the rightmost point,\n");
2566
" and a bunch of points lying along it? This method occasionally works,\n");
2568
" especially with horizontal and vertical lines, but often it doesn't, and\n"
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");
2574
" Furthermore, if you have segments that intersect other than at their\n");
2576
" endpoints, try not to let the intersections fall extremely close to PSLG\n"
2578
printf(" points or each other.\n\n");
2580
" If you have problems refining a triangulation not produced by Triangle:\n");
2582
" Are you sure the triangulation is geometrically valid? Is it formatted\n");
2584
" correctly for Triangle? Are the triangles all listed so the first three\n"
2586
printf(" points are their corners in counterclockwise order?\n\n");
2587
printf("Show Me:\n\n");
2589
" Triangle comes with a separate program named `Show Me', whose primary\n");
2591
" purpose is to draw meshes on your screen or in PostScript. Its secondary\n"
2594
" purpose is to check the validity of your input files, and do so more\n");
2596
" thoroughly than Triangle does. Show Me requires that you have the X\n");
2598
" Windows system. If you didn't receive Show Me with Triangle, complain to\n"
2600
printf(" whomever you obtained Triangle from, then send me mail.\n\n");
2601
printf("Triangle on the Web:\n\n");
2603
" To see an illustrated, updated version of these instructions, check out\n");
2605
printf(" http://www.cs.cmu.edu/~quake/triangle.html\n");
2607
printf("A Brief Plea:\n");
2610
" If you use Triangle, and especially if you use it to accomplish real\n");
2612
" work, I would like very much to hear from you. A short letter or email\n");
2614
" (to jrs@cs.cmu.edu) describing how you use Triangle will mean a lot to\n");
2616
" me. The more people I know are using this program, the more easily I can\n"
2619
" justify spending time on improvements and on the three-dimensional\n");
2621
" successor to Triangle, which in turn will benefit you. Also, I can put\n");
2623
" you on a list to receive email whenever a new version of Triangle is\n");
2624
printf(" available.\n\n");
2626
" If you use a mesh generated by Triangle in a publication, please include\n"
2628
printf(" an acknowledgment as well.\n\n");
2629
printf("Research credit:\n\n");
2631
" Of course, I can take credit for only a fraction of the ideas that made\n");
2633
" this mesh generator possible. Triangle owes its existence to the efforts\n"
2636
" of many fine computational geometers and other researchers, including\n");
2638
" Marshall Bern, L. Paul Chew, Boris Delaunay, Rex A. Dwyer, David\n");
2640
" Eppstein, Steven Fortune, Leonidas J. Guibas, Donald E. Knuth, C. L.\n");
2642
" Lawson, Der-Tsai Lee, Ernst P. Mucke, Douglas M. Priest, Jim Ruppert,\n");
2644
" Isaac Saias, Bruce J. Schachter, Micha Sharir, Jorge Stolfi, Christopher\n"
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");
2652
#endif /* not TRILIBRARY */
2654
/*****************************************************************************/
2656
/* internalerror() Ask the user to send me the defective product. Exit. */
2658
/*****************************************************************************/
2660
void internalerror()
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");
2669
/*****************************************************************************/
2671
/* parsecommandline() Read the command line, identify switches, and set */
2672
/* up options and file names. */
2674
/* The effects of this routine are felt entirely through global variables. */
2676
/*****************************************************************************/
2678
void parsecommandline(int argc, char **argv)
2681
#define STARTINDEX 0
2682
#else /* not TRILIBRARY */
2683
#define STARTINDEX 1
2686
#endif /* not TRILIBRARY */
2688
char workstring[FILENAMESIZE];
2690
poly = refine = quality = vararea = fixedarea = regionattrib = convex = 0;
2692
edgesout = voronoi = neighbors = geomview = 0;
2693
nobound = nopolywritten = nonodewritten = noelewritten = noiterationnum = 0;
2694
noholes = noexact = 0;
2695
incremental = sweepline = 0;
2704
quiet = verbose = 0;
2706
innodefilename[0] = '\0';
2707
#endif /* not TRILIBRARY */
2709
for (i = STARTINDEX; i < argc; i++) {
2711
if (argv[i][0] == '-') {
2712
#endif /* not TRILIBRARY */
2713
for (j = STARTINDEX; argv[i][j] != '\0'; j++) {
2714
if (argv[i][j] == 'p') {
2718
if (argv[i][j] == 'r') {
2721
if (argv[i][j] == 'q') {
2723
if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2724
(argv[i][j + 1] == '.')) {
2726
while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2727
(argv[i][j + 1] == '.')) {
2729
workstring[k] = argv[i][j];
2732
workstring[k] = '\0';
2733
minangle = (REAL) strtod(workstring, (char **) NULL);
2738
if (argv[i][j] == 'a') {
2740
if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2741
(argv[i][j + 1] == '.')) {
2744
while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2745
(argv[i][j + 1] == '.')) {
2747
workstring[k] = argv[i][j];
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");
2760
#endif /* not CDT_ONLY */
2761
if (argv[i][j] == 'A') {
2764
if (argv[i][j] == 'c') {
2767
if (argv[i][j] == 'z') {
2770
if (argv[i][j] == 'e') {
2773
if (argv[i][j] == 'v') {
2776
if (argv[i][j] == 'n') {
2779
if (argv[i][j] == 'g') {
2782
if (argv[i][j] == 'B') {
2785
if (argv[i][j] == 'P') {
2788
if (argv[i][j] == 'N') {
2791
if (argv[i][j] == 'E') {
2795
if (argv[i][j] == 'I') {
2798
#endif /* not TRILIBRARY */
2799
if (argv[i][j] == 'O') {
2802
if (argv[i][j] == 'X') {
2805
if (argv[i][j] == 'o') {
2806
if (argv[i][j + 1] == '2') {
2812
if (argv[i][j] == 'Y') {
2815
if (argv[i][j] == 'S') {
2817
while ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) {
2819
steiner = steiner * 10 + (int) (argv[i][j] - '0');
2822
#endif /* not CDT_ONLY */
2824
if (argv[i][j] == 'i') {
2827
if (argv[i][j] == 'F') {
2830
#endif /* not REDUCED */
2831
if (argv[i][j] == 'l') {
2836
if (argv[i][j] == 's') {
2839
#endif /* not CDT_ONLY */
2840
if (argv[i][j] == 'C') {
2843
#endif /* not REDUCED */
2844
if (argv[i][j] == 'Q') {
2847
if (argv[i][j] == 'V') {
2851
if ((argv[i][j] == 'h') || (argv[i][j] == 'H') ||
2852
(argv[i][j] == '?')) {
2855
#endif /* not TRILIBRARY */
2859
strncpy(innodefilename, argv[i], FILENAMESIZE - 1);
2860
innodefilename[FILENAMESIZE - 1] = '\0';
2862
#endif /* not TRILIBRARY */
2865
if (innodefilename[0] == '\0') {
2868
if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".node")) {
2869
innodefilename[strlen(innodefilename) - 5] = '\0';
2871
if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".poly")) {
2872
innodefilename[strlen(innodefilename) - 5] = '\0';
2876
if (!strcmp(&innodefilename[strlen(innodefilename) - 4], ".ele")) {
2877
innodefilename[strlen(innodefilename) - 4] = '\0';
2880
if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".area")) {
2881
innodefilename[strlen(innodefilename) - 5] = '\0';
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) {
2894
"Error: You cannot use the -I switch when refining a triangulation.\n");
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) {
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) {
2909
strcpy(inpolyfilename, innodefilename);
2910
strcpy(inelefilename, innodefilename);
2911
strcpy(areafilename, innodefilename);
2913
strcpy(workstring, innodefilename);
2915
while (workstring[j] != '\0') {
2916
if ((workstring[j] == '.') && (workstring[j + 1] != '\0')) {
2922
if (increment > 0) {
2925
if ((workstring[j] >= '0') && (workstring[j] <= '9')) {
2926
meshnumber = meshnumber * 10 + (int) (workstring[j] - '0');
2931
} while (workstring[j] != '\0');
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");
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");
2986
strcat(innodefilename, ".node");
2987
strcat(inpolyfilename, ".poly");
2988
strcat(inelefilename, ".ele");
2989
strcat(areafilename, ".area");
2990
#endif /* not TRILIBRARY */
2995
/********* User interaction routines begin here *********/
2997
/********* Debugging routines begin here *********/
3001
/*****************************************************************************/
3003
/* printtriangle() Print out the details of a triangle/edge handle. */
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. */
3010
/*****************************************************************************/
3012
void printtriangle(triedge *t)
3014
struct triedge printtri;
3015
struct edge printsh;
3018
printf("triangle x%lx with orientation %d:\n", (unsigned long) t->tri,
3020
decode(t->tri[0], printtri);
3021
if (printtri.tri == dummytri) {
3022
printf(" [0] = Outer space\n");
3024
printf(" [0] = x%lx %d\n", (unsigned long) printtri.tri,
3027
decode(t->tri[1], printtri);
3028
if (printtri.tri == dummytri) {
3029
printf(" [1] = Outer space\n");
3031
printf(" [1] = x%lx %d\n", (unsigned long) printtri.tri,
3034
decode(t->tri[2], printtri);
3035
if (printtri.tri == dummytri) {
3036
printf(" [2] = Outer space\n");
3038
printf(" [2] = x%lx %d\n", (unsigned long) printtri.tri,
3041
org(*t, printpoint);
3042
if (printpoint == (point) NULL)
3043
printf(" Origin[%d] = NULL\n", (t->orient + 1) % 3 + 3);
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);
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);
3059
printf(" Apex [%d] = x%lx (%.12g, %.12g)\n",
3060
t->orient + 3, (unsigned long) printpoint,
3061
printpoint[0], printpoint[1]);
3063
sdecode(t->tri[6], printsh);
3064
if (printsh.sh != dummysh) {
3065
printf(" [6] = x%lx %d\n", (unsigned long) printsh.sh,
3068
sdecode(t->tri[7], printsh);
3069
if (printsh.sh != dummysh) {
3070
printf(" [7] = x%lx %d\n", (unsigned long) printsh.sh,
3073
sdecode(t->tri[8], printsh);
3074
if (printsh.sh != dummysh) {
3075
printf(" [8] = x%lx %d\n", (unsigned long) printsh.sh,
3080
printf(" Area constraint: %.4g\n", areabound(*t));
3084
/*****************************************************************************/
3086
/* printshelle() Print out the details of a shell edge handle. */
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. */
3093
/*****************************************************************************/
3095
void printshelle(struct edge *s)
3097
struct edge printsh;
3098
struct triedge printtri;
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");
3107
printf(" [0] = x%lx %d\n", (unsigned long) printsh.sh,
3110
sdecode(s->sh[1], printsh);
3111
if (printsh.sh == dummysh) {
3112
printf(" [1] = No shell\n");
3114
printf(" [1] = x%lx %d\n", (unsigned long) printsh.sh,
3117
sorg(*s, printpoint);
3118
if (printpoint == (point) NULL)
3119
printf(" Origin[%d] = NULL\n", 2 + s->shorient);
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);
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");
3135
printf(" [4] = x%lx %d\n", (unsigned long) printtri.tri,
3138
decode(s->sh[5], printtri);
3139
if (printtri.tri == dummytri) {
3140
printf(" [5] = Outer space\n");
3142
printf(" [5] = x%lx %d\n", (unsigned long) printtri.tri,
3149
/********* Debugging routines end here *********/
3151
/********* Memory management routines begin here *********/
3155
/*****************************************************************************/
3157
/* poolinit() Initialize a pool of memory for allocation of items. */
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. */
3170
/* Don't change this routine unless you understand it. */
3172
/*****************************************************************************/
3175
struct memorypool *pool,
3178
enum wordtype wtype,
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;
3195
pool->alignbytes = wordsize;
3197
if (sizeof(VOID *) > pool->alignbytes) {
3198
pool->alignbytes = sizeof(VOID *);
3200
pool->itemwords = ((bytecount + pool->alignbytes - 1) / pool->alignbytes)
3201
* (pool->alignbytes / wordsize);
3202
pool->itembytes = pool->itemwords * wordsize;
3203
pool->itemsperblock = itemcount;
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");
3214
/* Set the next block pointer to NULL. */
3215
*(pool->firstblock) = (VOID *) NULL;
3219
/*****************************************************************************/
3221
/* poolrestart() Deallocate all items in a pool. */
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. */
3227
/*****************************************************************************/
3229
void poolrestart(struct memorypool *pool)
3231
unsigned long alignptr;
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;
3250
/*****************************************************************************/
3252
/* pooldeinit() Free to the operating system all memory taken by a pool. */
3254
/*****************************************************************************/
3256
void pooldeinit(struct memorypool *pool)
3258
while (pool->firstblock != (VOID **) NULL) {
3259
pool->nowblock = (VOID **) *(pool->firstblock);
3260
free(pool->firstblock);
3261
pool->firstblock = pool->nowblock;
3265
/*****************************************************************************/
3267
/* poolalloc() Allocate space for an item. */
3269
/*****************************************************************************/
3271
VOID *poolalloc(struct memorypool *pool)
3275
unsigned long alignptr;
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;
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");
3294
*(pool->nowblock) = (VOID *) newblock;
3295
/* The next block pointer is NULL. */
3296
*newblock = (VOID *) NULL;
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;
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);
3316
pool->nextitem = (VOID *) ((REAL *) pool->nextitem + pool->itemwords);
3318
pool->unallocateditems--;
3325
/*****************************************************************************/
3327
/* pooldealloc() Deallocate space for an item. */
3329
/* The deallocated space is stored in a queue for later reuse. */
3331
/*****************************************************************************/
3333
void pooldealloc(struct memorypool *pool, VOID *dyingitem)
3335
/* Push freshly killed item onto stack. */
3336
*((VOID **) dyingitem) = pool->deaditemstack;
3337
pool->deaditemstack = dyingitem;
3341
/*****************************************************************************/
3343
/* traversalinit() Prepare to traverse the entire list of items. */
3345
/* This routine is used in conjunction with traverse(). */
3347
/*****************************************************************************/
3349
void traversalinit(struct memorypool *pool)
3351
unsigned long alignptr;
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;
3365
/*****************************************************************************/
3367
/* traverse() Find the next item in the list. */
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 */
3377
/*****************************************************************************/
3379
VOID *traverse(struct memorypool *pool)
3382
unsigned long alignptr;
3384
/* Stop upon exhausting the list of items. */
3385
if (pool->pathitem == pool->nextitem) {
3386
return (VOID *) NULL;
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;
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);
3406
pool->pathitem = (VOID *) ((REAL *) pool->pathitem + pool->itemwords);
3408
pool->pathitemsleft--;
3412
/*****************************************************************************/
3414
/* dummyinit() Initialize the triangle that fills "outer space" and the */
3415
/* omnipresent shell edge. */
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. */
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 */
3427
/*****************************************************************************/
3429
void dummyinit(int trianglewords, int shellewords)
3431
unsigned long alignptr;
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. */
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");
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 */
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;
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 */
3466
dummyshbase = (shelle *) malloc(shwords * sizeof(shelle)
3467
+ shelles.alignbytes);
3468
if (dummyshbase == (shelle *) NULL) {
3469
printf("Error: Out of memory.\n");
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;
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;
3500
/*****************************************************************************/
3502
/* initializepointpool() Calculate the size of the point data structure */
3503
/* and initialize its memory pool. */
3505
/* This routine also computes the `pointmarkindex' and `point2triindex' */
3506
/* indices used to find values within each point. */
3508
/*****************************************************************************/
3510
void initializepointpool()
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)
3518
pointsize = (pointmarkindex + 1) * sizeof(int);
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);
3525
/* Initialize the pool of points. */
3526
poolinit(&points, pointsize, POINTPERBLOCK,
3527
(sizeof(REAL) >= sizeof(triangle)) ? FLOATINGPOINT : POINTER, 0);
3530
/*****************************************************************************/
3532
/* initializetrisegpools() Calculate the sizes of the triangle and shell */
3533
/* edge data structures and initialize their */
3536
/* This routine also computes the `highorderindex', `elemattribindex', and */
3537
/* `areaboundindex' indices used to find values within each triangle. */
3539
/*****************************************************************************/
3541
void initializetrisegpools()
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)) *
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. */
3563
trisize = (areaboundindex + 1) * sizeof(REAL);
3564
} else if (eextras + regionattrib > 0) {
3565
trisize = areaboundindex * sizeof(REAL);
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);
3575
/* Having determined the memory size of a triangle, initialize the pool. */
3576
poolinit(&triangles, trisize, TRIPERBLOCK, POINTER, 4);
3579
/* Initialize the pool of shell edges. */
3580
poolinit(&shelles, 6 * sizeof(triangle) + sizeof(int), SHELLEPERBLOCK,
3583
/* Initialize the "outer space" triangle and omnipresent shell edge. */
3584
dummyinit(triangles.itemwords, shelles.itemwords);
3586
/* Initialize the "outer space" triangle. */
3587
dummyinit(triangles.itemwords, 0);
3591
/*****************************************************************************/
3593
/* triangledealloc() Deallocate space for a triangle, marking it dead. */
3595
/*****************************************************************************/
3597
void triangledealloc(triangle *dyingtriangle)
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);
3607
/*****************************************************************************/
3609
/* triangletraverse() Traverse the triangles, skipping dead ones. */
3611
/*****************************************************************************/
3613
triangle *triangletraverse()
3615
triangle *newtriangle;
3617
newtriangle = (triangle *) traverse(&triangles);
3618
if (newtriangle == (triangle *) NULL) {
3619
return (triangle *) NULL;
3621
} while (newtriangle[3] == (triangle) NULL); /* Skip dead ones. */
3625
/*****************************************************************************/
3627
/* shelledealloc() Deallocate space for a shell edge, marking it dead. */
3629
/*****************************************************************************/
3631
void shelledealloc(shelle *dyingshelle)
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);
3640
/*****************************************************************************/
3642
/* shelletraverse() Traverse the shell edges, skipping dead ones. */
3644
/*****************************************************************************/
3646
shelle *shelletraverse()
3651
newshelle = (shelle *) traverse(&shelles);
3652
if (newshelle == (shelle *) NULL) {
3653
return (shelle *) NULL;
3655
} while (newshelle[2] == (shelle) NULL); /* Skip dead ones. */
3659
/*****************************************************************************/
3661
/* pointdealloc() Deallocate space for a point, marking it dead. */
3663
/*****************************************************************************/
3665
void pointdealloc(point dyingpoint)
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);
3673
/*****************************************************************************/
3675
/* pointtraverse() Traverse the points, skipping dead ones. */
3677
/*****************************************************************************/
3679
point pointtraverse()
3684
newpoint = (point) traverse(&points);
3685
if (newpoint == (point) NULL) {
3686
return (point) NULL;
3688
} while (pointmark(newpoint) == DEADPOINT); /* Skip dead ones. */
3692
/*****************************************************************************/
3694
/* badsegmentdealloc() Deallocate space for a bad segment, marking it */
3697
/*****************************************************************************/
3701
void badsegmentdealloc(struct edge *dyingseg)
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);
3709
#endif /* not CDT_ONLY */
3711
/*****************************************************************************/
3713
/* badsegmenttraverse() Traverse the bad segments, skipping dead ones. */
3715
/*****************************************************************************/
3719
struct edge *badsegmenttraverse()
3721
struct edge *newseg;
3724
newseg = (struct edge *) traverse(&badsegments);
3725
if (newseg == (struct edge *) NULL) {
3726
return (struct edge *) NULL;
3728
} while (newseg->shorient == -1); /* Skip dead ones. */
3732
#endif /* not CDT_ONLY */
3734
/*****************************************************************************/
3736
/* getpoint() Get a specific point, by number, from the list. */
3738
/* The first point is number 'firstnumber'. */
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 */
3744
/*****************************************************************************/
3746
point getpoint(int number)
3750
unsigned long alignptr;
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;
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;
3771
/*****************************************************************************/
3773
/* triangledeinit() Free all remaining allocated memory. */
3775
/*****************************************************************************/
3777
void triangledeinit()
3779
pooldeinit(&triangles);
3782
pooldeinit(&shelles);
3785
pooldeinit(&points);
3788
pooldeinit(&badsegments);
3789
if ((minangle > 0.0) || vararea || fixedarea) {
3790
pooldeinit(&badtriangles);
3793
#endif /* not CDT_ONLY */
3798
/********* Memory management routines end here *********/
3800
/********* Constructors begin here *********/
3804
/*****************************************************************************/
3806
/* maketriangle() Create a new triangle with orientation zero. */
3808
/*****************************************************************************/
3810
void maketriangle(struct triedge *newtriedge)
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 */
3826
newtriedge->tri[6] = (triangle) dummysh;
3827
newtriedge->tri[7] = (triangle) dummysh;
3828
newtriedge->tri[8] = (triangle) dummysh;
3830
for (i = 0; i < eextras; i++) {
3831
setelemattribute(*newtriedge, i, 0.0);
3834
setareabound(*newtriedge, -1.0);
3837
newtriedge->orient = 0;
3840
/*****************************************************************************/
3842
/* makeshelle() Create a new shell edge with orientation zero. */
3844
/*****************************************************************************/
3846
void makeshelle(struct edge *newedge)
3848
newedge->sh = (shelle *) poolalloc(&shelles);
3849
/* Initialize the two adjoining shell edges to be the omnipresent */
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);
3862
newedge->shorient = 0;
3867
/********* Constructors end here *********/
3869
/********* Determinant evaluation routines begin here *********/
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. */
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. */
3883
#define Absolute(a) ((a) >= 0.0 ? (a) : -(a))
3884
/* #define Absolute(a) fabs(a) */
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. */
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'. */
3899
#define Fast_Two_Sum_Tail(a, b, x, y) \
3903
#define Fast_Two_Sum(a, b, x, y) \
3904
x = (REAL) (a + b); \
3905
Fast_Two_Sum_Tail(a, b, x, y)
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; \
3914
#define Two_Sum(a, b, x, y) \
3915
x = (REAL) (a + b); \
3916
Two_Sum_Tail(a, b, x, y)
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; \
3925
#define Two_Diff(a, b, x, y) \
3926
x = (REAL) (a - b); \
3927
Two_Diff_Tail(a, b, x, y)
3929
#define Split(a, ahi, alo) \
3930
c = (REAL) (splitter * a); \
3931
abig = (REAL) (c - a); \
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
3943
#define Two_Product(a, b, x, y) \
3944
x = (REAL) (a * b); \
3945
Two_Product_Tail(a, b, x, y)
3947
/* Two_Product_Presplit() is Two_Product() where one of the inputs has */
3948
/* already been split. Avoids redundant splitting. */
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
3958
/* Square() can be done more quickly than Two_Product(). */
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
3966
#define Square(a, x, y) \
3967
x = (REAL) (a * a); \
3968
Square_Tail(a, x, y)
3970
/* Macros for summing expansions of various fixed lengths. These are all */
3971
/* unrolled versions of Expansion_Sum(). */
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)
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)
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)
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)
3989
/*****************************************************************************/
3991
/* exactinit() Initialize the variables used for exact arithmetic. */
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. */
3997
/* `splitter' is used to split floating-point numbers into two half- */
3998
/* length significands for exact multiplication. */
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. */
4004
/* Don't change this routine unless you fully understand it. */
4006
/*****************************************************************************/
4011
REAL check, lastcheck;
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. */
4029
every_other = !every_other;
4030
check = 1.0 + epsilon;
4031
} while ((check != 1.0) && (check != lastcheck));
4034
printf("Floating point roundoff is of magnitude %.17g\n", epsilon);
4035
printf("Floating point splitter is %.17g\n", splitter);
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;
4047
/*****************************************************************************/
4049
/* fast_expansion_sum_zeroelim() Sum two expansions, eliminating zero */
4050
/* components from the output expansion. */
4052
/* Sets h = e + f. See my Robust Predicates paper for details. */
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 */
4059
/*****************************************************************************/
4061
int fast_expansion_sum_zeroelim( /* h cannot be e or f. */
4073
REAL avirt, bround, around;
4074
int eindex, findex, hindex;
4079
eindex = findex = 0;
4080
if ((fnow > enow) == (fnow > -enow)) {
4088
if ((eindex < elen) && (findex < flen)) {
4089
if ((fnow > enow) == (fnow > -enow)) {
4090
Fast_Two_Sum(enow, Q, Qnew, hh);
4093
Fast_Two_Sum(fnow, Q, Qnew, hh);
4100
while ((eindex < elen) && (findex < flen)) {
4101
if ((fnow > enow) == (fnow > -enow)) {
4102
Two_Sum(Q, enow, Qnew, hh);
4105
Two_Sum(Q, fnow, Qnew, hh);
4114
while (eindex < elen) {
4115
Two_Sum(Q, enow, Qnew, hh);
4122
while (findex < flen) {
4123
Two_Sum(Q, fnow, Qnew, hh);
4130
if ((Q != 0.0) || (hindex == 0)) {
4136
/*****************************************************************************/
4138
/* scale_expansion_zeroelim() Multiply an expansion by a scalar, */
4139
/* eliminating zero components from the */
4140
/* output expansion. */
4142
/* Sets h = be. See my Robust Predicates paper for details. */
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 */
4149
/*****************************************************************************/
4151
int scale_expansion_zeroelim( /* e and h cannot be the same. */
4158
INEXACT REAL Q, sum;
4160
INEXACT REAL product1;
4165
REAL avirt, bround, around;
4168
REAL ahi, alo, bhi, blo;
4169
REAL err1, err2, err3;
4172
Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
4177
for (eindex = 1; eindex < elen; eindex++) {
4179
Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
4180
Two_Sum(Q, product0, sum, hh);
4184
Fast_Two_Sum(product1, sum, Q, hh);
4189
if ((Q != 0.0) || (hindex == 0)) {
4195
/*****************************************************************************/
4197
/* estimate() Produce a one-word estimate of an expansion's value. */
4199
/* See my Robust Predicates paper for details. */
4201
/*****************************************************************************/
4203
REAL estimate(int elen,REAL *e)
4210
for (eindex = 1; eindex < elen; eindex++) {
4216
/*****************************************************************************/
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. */
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. */
4232
/* See my Robust Predicates paper for details. */
4234
/*****************************************************************************/
4236
REAL counterclockwiseadapt(
4243
INEXACT REAL acx, acy, bcx, bcy;
4244
REAL acxtail, acytail, bcxtail, bcytail;
4245
INEXACT REAL detleft, detright;
4246
REAL detlefttail, detrighttail;
4248
REAL B[4], C1[8], C2[12], D[16];
4250
int C1length, C2length, Dlength;
4253
INEXACT REAL s1, t1;
4257
REAL avirt, bround, around;
4260
REAL ahi, alo, bhi, blo;
4261
REAL err1, err2, err3;
4262
INEXACT REAL _i, _j;
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]);
4270
Two_Product(acx, bcy, detleft, detlefttail);
4271
Two_Product(acy, bcx, detright, detrighttail);
4273
Two_Two_Diff(detleft, detlefttail, detright, detrighttail,
4274
B3, B[2], B[1], B[0]);
4277
det = estimate(4, B);
4278
errbound = ccwerrboundB * detsum;
4279
if ((det >= errbound) || (-det >= errbound)) {
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);
4288
if ((acxtail == 0.0) && (acytail == 0.0)
4289
&& (bcxtail == 0.0) && (bcytail == 0.0)) {
4293
errbound = ccwerrboundC * detsum + resulterrbound * Absolute(det);
4294
det += (acx * bcytail + bcy * acxtail)
4295
- (acy * bcxtail + bcx * acytail);
4296
if ((det >= errbound) || (-det >= errbound)) {
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]);
4304
C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);
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]);
4310
C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);
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]);
4316
Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);
4318
return(D[Dlength - 1]);
4321
REAL counterclockwise(
4327
REAL detleft, detright, det;
4328
REAL detsum, errbound;
4330
counterclockcount++;
4332
detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);
4333
detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
4334
det = detleft - detright;
4340
if (detleft > 0.0) {
4341
if (detright <= 0.0) {
4344
detsum = detleft + detright;
4346
} else if (detleft < 0.0) {
4347
if (detright >= 0.0) {
4350
detsum = -detleft - detright;
4356
errbound = ccwerrboundA * detsum;
4357
if ((det >= errbound) || (-det >= errbound)) {
4361
return counterclockwiseadapt(pa, pb, pc, detsum);
4364
/*****************************************************************************/
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. */
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. */
4379
/* See my Robust Predicates paper for details. */
4381
/*****************************************************************************/
4390
INEXACT REAL adx, bdx, cdx, ady, bdy, cdy;
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;
4405
REAL fin1[1152], fin2[1152];
4406
REAL *finnow, *finother, *finswap;
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;
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;
4443
REAL avirt, bround, around;
4446
REAL ahi, alo, bhi, blo;
4447
REAL err1, err2, err3;
4448
INEXACT REAL _i, _j;
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]);
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]);
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);
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]);
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);
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]);
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);
4488
ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);
4489
finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);
4491
det = estimate(finlength, fin1);
4492
errbound = iccerrboundB * permanent;
4493
if ((det >= errbound) || (-det >= errbound)) {
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)) {
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)) {
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]);
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]);
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]);
4547
if (adxtail != 0.0) {
4548
axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);
4549
temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx,
4552
axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);
4553
temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);
4555
axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);
4556
temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);
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,
4564
finswap = finnow; finnow = finother; finother = finswap;
4566
if (adytail != 0.0) {
4567
aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);
4568
temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady,
4571
aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);
4572
temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);
4574
aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);
4575
temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);
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,
4583
finswap = finnow; finnow = finother; finother = finswap;
4585
if (bdxtail != 0.0) {
4586
bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);
4587
temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx,
4590
bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);
4591
temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);
4593
bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);
4594
temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);
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,
4602
finswap = finnow; finnow = finother; finother = finswap;
4604
if (bdytail != 0.0) {
4605
bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);
4606
temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy,
4609
bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);
4610
temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);
4612
bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);
4613
temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);
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,
4621
finswap = finnow; finnow = finother; finother = finswap;
4623
if (cdxtail != 0.0) {
4624
cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);
4625
temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx,
4628
cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);
4629
temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);
4631
cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);
4632
temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);
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,
4640
finswap = finnow; finnow = finother; finother = finswap;
4642
if (cdytail != 0.0) {
4643
cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);
4644
temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy,
4647
cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);
4648
temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);
4650
cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);
4651
temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);
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,
4659
finswap = finnow; finnow = finother; finother = finswap;
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]);
4670
Two_Product(cdxtail, negate, ti1, ti0);
4672
Two_Product(cdx, negate, tj1, tj0);
4673
Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4675
bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);
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]);
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,
4694
temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4695
temp32alen, temp32a, temp48);
4696
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
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,
4703
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4705
finswap = finnow; finnow = finother; finother = finswap;
4707
if (cdytail != 0.0) {
4708
temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);
4709
temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
4711
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4713
finswap = finnow; finnow = finother; finother = finswap;
4716
temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail,
4718
axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);
4719
temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx,
4721
temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail,
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,
4729
finswap = finnow; finnow = finother; finother = finswap;
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,
4736
temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4737
temp32alen, temp32a, temp48);
4738
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4740
finswap = finnow; finnow = finother; finother = finswap;
4743
temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail,
4745
aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);
4746
temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady,
4748
temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail,
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,
4756
finswap = finnow; finnow = finother; finother = finswap;
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]);
4767
Two_Product(adxtail, negate, ti1, ti0);
4769
Two_Product(adx, negate, tj1, tj0);
4770
Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4772
catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);
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]);
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,
4791
temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4792
temp32alen, temp32a, temp48);
4793
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
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,
4800
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4802
finswap = finnow; finnow = finother; finother = finswap;
4804
if (adytail != 0.0) {
4805
temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);
4806
temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
4808
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4810
finswap = finnow; finnow = finother; finother = finswap;
4813
temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail,
4815
bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);
4816
temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx,
4818
temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail,
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,
4826
finswap = finnow; finnow = finother; finother = finswap;
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,
4833
temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4834
temp32alen, temp32a, temp48);
4835
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4837
finswap = finnow; finnow = finother; finother = finswap;
4840
temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail,
4842
bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);
4843
temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy,
4845
temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail,
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,
4853
finswap = finnow; finnow = finother; finother = finswap;
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]);
4864
Two_Product(bdxtail, negate, ti1, ti0);
4866
Two_Product(bdx, negate, tj1, tj0);
4867
Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4869
abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);
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]);
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,
4888
temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4889
temp32alen, temp32a, temp48);
4890
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
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,
4897
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4899
finswap = finnow; finnow = finother; finother = finswap;
4901
if (bdytail != 0.0) {
4902
temp8len = scale_expansion_zeroelim(4, aa, -cdxtail, temp8);
4903
temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
4905
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4907
finswap = finnow; finnow = finother; finother = finswap;
4910
temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, cdxtail,
4912
cxtabttlen = scale_expansion_zeroelim(abttlen, abtt, cdxtail, cxtabtt);
4913
temp16alen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, 2.0 * cdx,
4915
temp16blen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, cdxtail,
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,
4923
finswap = finnow; finnow = finother; finother = finswap;
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,
4930
temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4931
temp32alen, temp32a, temp48);
4932
finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4934
finswap = finnow; finnow = finother; finother = finswap;
4937
temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, cdytail,
4939
cytabttlen = scale_expansion_zeroelim(abttlen, abtt, cdytail, cytabtt);
4940
temp16alen = scale_expansion_zeroelim(cytabttlen, cytabtt, 2.0 * cdy,
4942
temp16blen = scale_expansion_zeroelim(cytabttlen, cytabtt, cdytail,
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,
4950
finswap = finnow; finnow = finother; finother = finswap;
4954
return finnow[finlength - 1];
4963
REAL adx, bdx, cdx, ady, bdy, cdy;
4964
REAL bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
4965
REAL alift, blift, clift;
4967
REAL permanent, errbound;
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];
4980
alift = adx * adx + ady * ady;
4984
blift = bdx * bdx + bdy * bdy;
4988
clift = cdx * cdx + cdy * cdy;
4990
det = alift * (bdxcdy - cdxbdy)
4991
+ blift * (cdxady - adxcdy)
4992
+ clift * (adxbdy - bdxady);
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)) {
5006
return incircleadapt(pa, pb, pc, pd, permanent);
5011
/********* Determinant evaluation routines end here *********/
5013
/*****************************************************************************/
5015
/* triangleinit() Initialize some variables. */
5017
/*****************************************************************************/
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;
5032
exactinit(); /* Initialize exact arithmetic constants. */
5035
/*****************************************************************************/
5037
/* randomnation() Generate a random number between 0 and `choices' - 1. */
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. */
5043
/*****************************************************************************/
5045
unsigned long randomnation(
5046
unsigned int choices)
5048
randomseed = (randomseed * 1366l + 150889l) % 714025l;
5049
return randomseed / (714025l / choices + 1);
5052
/********* Mesh quality testing routines begin here *********/
5056
/*****************************************************************************/
5058
/* checkmesh() Test the mesh for topological consistency. */
5060
/*****************************************************************************/
5066
struct triedge triangleloop;
5067
struct triedge oppotri, oppooppotri;
5068
point triorg, tridest, triapex;
5069
point oppoorg, oppodest;
5072
triangle ptr; /* Temporary variable used by sym(). */
5074
/* Temporarily turn on exact arithmetic if it's off. */
5075
saveexact = noexact;
5078
printf(" Checking consistency of mesh...\n");
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);
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");
5111
printtriangle(&triangleloop);
5112
printf(" Second (nonreciprocating) ");
5113
printtriangle(&oppotri);
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"
5123
printf(" First mismatched ");
5124
printtriangle(&triangleloop);
5125
printf(" Second mismatched ");
5126
printtriangle(&oppotri);
5131
triangleloop.tri = triangletraverse();
5135
printf(" In my studied opinion, the mesh appears to be consistent.\n");
5137
} else if (horrors == 1) {
5138
printf(" !! !! !! !! Precisely one festering wound discovered.\n");
5140
printf(" !! !! !! !! %d abominations witnessed.\n", horrors);
5142
/* Restore the status of exact arithmetic. */
5143
noexact = saveexact;
5146
#endif /* not REDUCED */
5148
/*****************************************************************************/
5150
/* checkdelaunay() Ensure that the mesh is (constrained) Delaunay. */
5152
/*****************************************************************************/
5156
void checkdelaunay()
5158
struct triedge triangleloop;
5159
struct triedge oppotri;
5160
struct edge opposhelle;
5161
point triorg, tridest, triapex;
5163
int shouldbedelaunay;
5166
triangle ptr; /* Temporary variable used by sym(). */
5167
shelle sptr; /* Temporary variable used by tspivot(). */
5169
/* Temporarily turn on exact arithmetic if it's off. */
5170
saveexact = noexact;
5173
printf(" Checking Delaunay property of mesh...\n");
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;
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);
5213
triangleloop.tri = triangletraverse();
5218
" By virtue of my perceptive intelligence, I declare the mesh Delaunay.\n");
5220
} else if (horrors == 1) {
5222
" !! !! !! !! Precisely one terrifying transgression identified.\n");
5224
printf(" !! !! !! !! %d obscenities viewed with horror.\n", horrors);
5226
/* Restore the status of exact arithmetic. */
5227
noexact = saveexact;
5230
#endif /* not REDUCED */
5232
/*****************************************************************************/
5234
/* enqueuebadtri() Add a bad triangle to the end of a queue. */
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. */
5240
/*****************************************************************************/
5245
struct triedge *instri,
5251
struct badface *newface;
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]);
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. */
5269
queuenumber = (int) (160.0 * (angle - 0.6));
5270
if (queuenumber > 63) {
5274
/* It's not a bad angle; put the triangle in the lowest-priority queue. */
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;
5283
#endif /* not CDT_ONLY */
5285
/*****************************************************************************/
5287
/* dequeuebadtri() Remove a triangle from the front of the queue. */
5289
/*****************************************************************************/
5293
struct badface *dequeuebadtri()
5295
struct badface *result;
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];
5311
return (struct badface *) NULL;
5314
#endif /* not CDT_ONLY */
5316
/*****************************************************************************/
5318
/* checkedge4encroach() Check a segment to see if it is encroached; add */
5319
/* it to the list if it is. */
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. */
5325
/* Returns a nonzero value if the edge is encroached. */
5327
/*****************************************************************************/
5331
int checkedge4encroach(
5332
struct edge *testedge)
5334
struct triedge neighbortri;
5335
struct edge testsym;
5336
struct edge *badedge;
5339
point eorg, edest, eapex;
5340
triangle ptr; /* Temporary variable used by stpivot(). */
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) {
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]) {
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) {
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]) {
5382
if (addtolist && (!nobisect || ((nobisect == 1) && (sides == 2)))) {
5384
printf(" Queueing encroached segment (%.12g, %.12g) (%.12g, %.12g).\n",
5385
eorg[0], eorg[1], edest[0], edest[1]);
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);
5393
shellecopy(testsym, *badedge);
5399
#endif /* not CDT_ONLY */
5401
/*****************************************************************************/
5403
/* testtriangle() Test a face for quality measures. */
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. */
5409
/*****************************************************************************/
5414
struct triedge *testtri)
5416
struct triedge sametesttri;
5417
struct edge edge1, edge2;
5418
point torg, tdest, tapex;
5420
REAL dxod, dyod, dxda, dyda, dxao, dyao;
5421
REAL dxod2, dyod2, dxda2, dyda2, dxao2, dyao2;
5422
REAL apexlen, orglen, destlen;
5425
shelle sptr; /* Temporary variable used by tspivot(). */
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);
5462
tspivot(*testtri, edge1);
5463
lprev(*testtri, sametesttri);
5464
tspivot(sametesttri, edge2);
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);
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. */
5480
/* Beware of a floating exception in acos(). */
5483
/* Find the actual angle in degrees, for printing. */
5484
angle = acos(sqrt(angle)) * (180.0 / PI);
5486
"Warning: Small angle (%.4g degrees) between segments at point\n",
5488
printf(" (%.12g, %.12g)\n", anglevertex[0], anglevertex[1]);
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. */
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);
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);
5516
#endif /* not CDT_ONLY */
5520
/********* Mesh quality testing routines end here *********/
5522
/********* Point location routines begin here *********/
5526
/*****************************************************************************/
5528
/* makepointmap() Construct a mapping from points to triangles to improve */
5529
/* the speed of point location for segment insertion. */
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. */
5537
/*****************************************************************************/
5541
struct triedge triangleloop;
5545
printf(" Constructing mapping from points to triangles.\n");
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));
5556
triangleloop.tri = triangletraverse();
5560
/*****************************************************************************/
5562
/* preciselocate() Find a triangle or edge containing a given point. */
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.) */
5570
/* These conditions are imposed because preciselocate() is normally used in */
5571
/* one of two situations: */
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. */
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. */
5585
/* On completion, `searchtri' is a triangle that contains `searchpoint'. */
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. */
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. */
5601
/* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */
5602
/* is a handle whose origin is the existing vertex. */
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. */
5607
/* Returns INTRIANGLE if the point lies strictly within a triangle. */
5608
/* `searchtri' is a handle on the triangle that contains the point. */
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 */
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. */
5622
/*****************************************************************************/
5624
enum locateresult preciselocate(
5626
struct triedge *searchtri)
5628
struct triedge backtracktri;
5629
point forg, fdest, fapex;
5631
REAL orgorient, destorient;
5633
triangle ptr; /* Temporary variable used by sym(). */
5636
printf(" Searching for point (%.12g, %.12g).\n",
5637
searchpoint[0], searchpoint[1]);
5640
org(*searchtri, forg);
5641
dest(*searchtri, fdest);
5642
apex(*searchtri, fapex);
5645
printf(" At (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
5646
forg[0], forg[1], fdest[0], fdest[1], fapex[0], fapex[1]);
5648
/* Check whether the apex is the point we seek. */
5649
if ((fapex[0] == searchpoint[0]) && (fapex[1] == searchpoint[1])) {
5650
lprevself(*searchtri);
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;
5672
if (orgorient > 0.0) {
5675
/* The point we seek must be on the boundary of or inside this */
5677
if (destorient == 0.0) {
5678
lprevself(*searchtri);
5681
if (orgorient == 0.0) {
5682
lnextself(*searchtri);
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. */
5694
lprev(*searchtri, backtracktri);
5697
lnext(*searchtri, backtracktri);
5700
sym(backtracktri, *searchtri);
5702
/* Check for walking off the edge. */
5703
if (searchtri->tri == dummytri) {
5705
triedgecopy(backtracktri, *searchtri);
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)) {
5717
apex(*searchtri, fapex);
5722
/*****************************************************************************/
5724
/* locate() Find a triangle or edge containing a given point. */
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. */
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. */
5735
/* On completion, `searchtri' is a triangle that contains `searchpoint'. */
5737
/* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */
5738
/* is a handle whose origin is the existing vertex. */
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. */
5743
/* Returns INTRIANGLE if the point lies strictly within a triangle. */
5744
/* `searchtri' is a handle on the triangle that contains the point. */
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 */
5753
/* WARNING: This routine is designed for convex triangulations, and will */
5754
/* not generally work after the holes and concavities have been carved. */
5756
/*****************************************************************************/
5758
enum locateresult locate(
5760
struct triedge *searchtri)
5764
struct triedge sampletri;
5766
unsigned long alignptr;
5767
REAL searchdist, dist;
5769
long sampleblocks, samplesperblock, samplenum;
5772
triangle ptr; /* Temporary variable used by sym(). */
5775
printf(" Randomly sampling for a triangle near point (%.12g, %.12g).\n",
5776
searchpoint[0], searchpoint[1]);
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]);
5784
printf(" Boundary triangle has origin (%.12g, %.12g).\n",
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);
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);
5803
printf(" Choosing recent triangle with origin (%.12g, %.12g).\n",
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) {
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)));
5830
samplenum = randomnation(TRIPERBLOCK);
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);
5842
printf(" Choosing triangle with origin (%.12g, %.12g).\n",
5848
sampleblock = (VOID **) *sampleblock;
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])) {
5857
if ((tdest[0] == searchpoint[0]) && (tdest[1] == searchpoint[1])) {
5858
lnextself(*searchtri);
5861
/* Orient `searchtri' to fit the preconditions of calling preciselocate(). */
5862
ahead = counterclockwise(torg, tdest, searchpoint);
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]))) {
5874
return preciselocate(searchpoint, searchtri);
5879
/********* Point location routines end here *********/
5881
/********* Mesh transformation routines begin here *********/
5885
/*****************************************************************************/
5887
/* insertshelle() Create a new shell edge and insert it between two */
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. */
5894
/*****************************************************************************/
5897
struct triedge *tri, /* Edge at which to insert the new shell edge. */
5898
int shellemark) /* Marker for the new shell edge. */
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(). */
5906
/* Mark points if possible. */
5908
dest(*tri, tridest);
5909
if (pointmark(triorg) == 0) {
5910
setpointmark(triorg, shellemark);
5912
if (pointmark(tridest) == 0) {
5913
setpointmark(tridest, shellemark);
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 */
5926
tsbond(*tri, newshelle);
5928
ssymself(newshelle);
5929
tsbond(oppotri, newshelle);
5930
setmark(newshelle, shellemark);
5932
printf(" Inserting new ");
5933
printshelle(&newshelle);
5936
if (mark(newshelle) == 0) {
5937
setmark(newshelle, shellemark);
5942
/*****************************************************************************/
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 */
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. */
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. */
5962
/*****************************************************************************/
5964
/*****************************************************************************/
5966
/* flip() Transform two triangles to two different triangles by flipping */
5967
/* an edge within a quadrilateral. */
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. */
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. */
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.) */
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. */
5993
/*****************************************************************************/
5996
struct triedge *flipedge) /* Handle for the triangle abc. */
5998
struct triedge botleft, botright;
5999
struct triedge topleft, topright;
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;
6007
triangle ptr; /* Temporary variable used by sym(). */
6008
shelle sptr; /* Temporary variable used by tspivot(). */
6010
/* Identify the vertices of the quadrilateral. */
6011
org(*flipedge, rightpoint);
6012
dest(*flipedge, leftpoint);
6013
apex(*flipedge, botpoint);
6014
sym(*flipedge, top);
6016
if (top.tri == dummytri) {
6017
printf("Internal error in flip(): Attempt to flip on boundary.\n");
6018
lnextself(*flipedge);
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);
6029
#endif /* SELF_CHECK */
6030
apex(top, farpoint);
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);
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);
6056
tsbond(topright, toplshelle);
6058
if (botlshelle.sh == dummysh) {
6059
tsdissolve(topleft);
6061
tsbond(topleft, botlshelle);
6063
if (botrshelle.sh == dummysh) {
6064
tsdissolve(botleft);
6066
tsbond(botleft, botrshelle);
6068
if (toprshelle.sh == dummysh) {
6069
tsdissolve(botright);
6071
tsbond(botright, toprshelle);
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);
6083
printf(" Edge flip results in left ");
6085
printtriangle(&topleft);
6086
printf(" and right ");
6087
printtriangle(flipedge);
6091
/*****************************************************************************/
6093
/* insertsite() Insert a vertex into a Delaunay triangulation, */
6094
/* performing flips as necessary to maintain the Delaunay */
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. */
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. */
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. */
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. */
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. */
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. */
6136
/*****************************************************************************/
6138
enum insertsiteresult insertsite(
6140
struct triedge *searchtri,
6141
struct edge *splitedge,
6145
struct triedge horiz;
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;
6162
point leftpoint, rightpoint, botpoint, toppoint, farpoint;
6165
enum insertsiteresult success;
6166
enum locateresult intersect;
6170
triangle ptr; /* Temporary variable used by sym(). */
6171
shelle sptr; /* Temporary variable used by spivot() and tspivot(). */
6174
printf(" Inserting (%.12g, %.12g).\n", insertpoint[0], insertpoint[1]);
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;
6184
/* Search for a triangle containing `insertpoint'. */
6185
intersect = locate(insertpoint, &horiz);
6187
/* Start searching from the triangle provided by the caller. */
6188
triedgecopy(*searchtri, horiz);
6189
intersect = preciselocate(insertpoint, &horiz);
6192
/* The calling routine provides the edge in which the point is inserted. */
6193
triedgecopy(*searchtri, horiz);
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;
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. */
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);
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;
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;
6240
lnextself(topright);
6241
sym(topright, toprcasing);
6242
maketriangle(&newtopright);
6244
/* Splitting the boundary edge increases the number of boundary edges. */
6247
maketriangle(&newbotright);
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));
6262
/* Set the area constraint of a new triangle. */
6263
setareabound(newbotright, areabound(botright));
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));
6276
/* Set the area constraint of another new triangle. */
6277
setareabound(newtopright, areabound(topright));
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);
6290
tspivot(topright, toprshelle);
6291
if (toprshelle.sh != dummysh) {
6292
tsdissolve(topright);
6293
tsbond(newtopright, toprshelle);
6298
/* Bond the new triangle(s) to the surrounding triangles. */
6299
bond(newbotright, botrcasing);
6300
lprevself(newbotright);
6301
bond(newbotright, botright);
6302
lprevself(newbotright);
6304
bond(newtopright, toprcasing);
6305
lnextself(newtopright);
6306
bond(newtopright, topright);
6307
lnextself(newtopright);
6308
bond(newtopright, newbotright);
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);
6320
sbond(newedge, rightedge);
6321
ssymself(*splitedge);
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");
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");
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"
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"
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"
6350
if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
6351
printf("Internal error in insertsite():\n");
6353
" Clockwise triangle after edge point insertion (bottom right).\n");
6355
#endif /* SELF_CHECK */
6357
printf(" Updating bottom left ");
6358
printtriangle(&botright);
6360
printf(" Updating top left ");
6361
printtriangle(&topright);
6362
printf(" Creating top right ");
6363
printtriangle(&newtopright);
6365
printf(" Creating bottom right ");
6366
printtriangle(&newbotright);
6369
/* Position `horiz' on the first edge to check for */
6370
/* the Delaunay property. */
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);
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);
6399
/* Set the area constraint of the new triangles. */
6400
area = areabound(horiz);
6401
setareabound(newbotleft, area);
6402
setareabound(newbotright, area);
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);
6413
tspivot(botright, botrshelle);
6414
if (botrshelle.sh != dummysh) {
6415
tsdissolve(botright);
6416
tsbond(newbotright, botrshelle);
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);
6432
if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
6433
printf("Internal error in insertsite():\n");
6434
printf(" Clockwise triangle prior to point insertion.\n");
6436
if (counterclockwise(rightpoint, leftpoint, insertpoint) < 0.0) {
6437
printf("Internal error in insertsite():\n");
6438
printf(" Clockwise triangle after point insertion (top).\n");
6440
if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
6441
printf("Internal error in insertsite():\n");
6442
printf(" Clockwise triangle after point insertion (left).\n");
6444
if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
6445
printf("Internal error in insertsite():\n");
6446
printf(" Clockwise triangle after point insertion (right).\n");
6448
#endif /* SELF_CHECK */
6450
printf(" Updating top ");
6451
printtriangle(&horiz);
6452
printf(" Creating left ");
6453
printtriangle(&newbotleft);
6454
printf(" Creating right ");
6455
printtriangle(&newbotright);
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. */
6468
dest(horiz, leftpoint);
6469
/* Circle until finished. */
6471
/* By default, the edge will be flipped. */
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. */
6481
/* Does the new point encroach upon this segment? */
6482
if (checkedge4encroach(&checkshelle)) {
6483
success = ENCROACHINGPOINT;
6486
#endif /* not CDT_ONLY */
6490
/* Check if the edge is a boundary edge. */
6492
if (top.tri == dummytri) {
6493
/* The edge is a boundary edge and cannot be flipped. */
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'. */
6523
/* Test whether the edge is locally Delaunay. */
6524
doflip = incircle(leftpoint, insertpoint, rightpoint, farpoint)
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);
6553
tsbond(topright, toplshelle);
6555
if (botlshelle.sh == dummysh) {
6556
tsdissolve(topleft);
6558
tsbond(topleft, botlshelle);
6560
if (botrshelle.sh == dummysh) {
6561
tsdissolve(botleft);
6563
tsbond(botleft, botrshelle);
6565
if (toprshelle.sh == dummysh) {
6566
tsdissolve(botright);
6568
tsbond(botright, toprshelle);
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);
6585
if ((areabound(top) <= 0.0) || (areabound(horiz) <= 0.0)) {
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));
6593
setareabound(top, area);
6594
setareabound(horiz, area);
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");
6602
/* The following test has been removed because constrainededge() */
6603
/* sometimes generates inverted triangles that insertsite() */
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");
6611
if (counterclockwise(farpoint, leftpoint, insertpoint) < 0.0) {
6612
printf("Internal error in insertsite():\n");
6613
printf(" Clockwise triangle after edge flip (left).\n");
6615
if (counterclockwise(insertpoint, rightpoint, farpoint) < 0.0) {
6616
printf("Internal error in insertsite():\n");
6617
printf(" Clockwise triangle after edge flip (right).\n");
6620
#endif /* SELF_CHECK */
6622
printf(" Edge flip results in left ");
6624
printtriangle(&topleft);
6625
printf(" and right ");
6626
printtriangle(&horiz);
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. */
6632
leftpoint = farpoint;
6637
/* The handle `horiz' is accepted as locally Delaunay. */
6640
/* Check the triangle `horiz' for quality. */
6641
testtriangle(&horiz);
6643
#endif /* not CDT_ONLY */
6644
/* Look for the next edge around the newly inserted point. */
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);
6656
/* Finish finding the next edge around the newly inserted point. */
6657
lnext(testtri, horiz);
6658
rightpoint = leftpoint;
6659
dest(horiz, leftpoint);
6664
/*****************************************************************************/
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. */
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 */
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. */
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'. */
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. */
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. */
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. */
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 */
6726
/*****************************************************************************/
6728
void triangulatepolygon(
6729
struct triedge *firstedge,
6730
struct triedge *lastedge,
6735
struct triedge testtri;
6736
struct triedge besttri;
6737
struct triedge tempedge;
6738
point leftbasepoint, rightbasepoint;
6743
triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
6745
/* Identify the base vertices. */
6746
apex(*lastedge, leftbasepoint);
6747
dest(*firstedge, rightbasepoint);
6749
printf(" Triangulating interior polygon at edge\n");
6750
printf(" (%.12g, %.12g) (%.12g, %.12g)\n", leftbasepoint[0],
6751
leftbasepoint[1], rightbasepoint[0], rightbasepoint[1]);
6753
/* Find the best vertex to connect the base to. */
6754
onext(*firstedge, besttri);
6755
dest(besttri, bestpoint);
6756
triedgecopy(besttri, testtri);
6758
for (i = 2; i <= edgecount - 2; i++) {
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;
6769
printf(" Connecting edge to (%.12g, %.12g)\n", bestpoint[0],
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);
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,
6782
/* Find `besttri' again; it may have been lost to edge flips. */
6783
sym(tempedge, besttri);
6786
/* Do one final edge flip. */
6790
/* Check the quality of the newly committed triangle. */
6791
sym(besttri, testtri);
6792
testtriangle(&testtri);
6794
#endif /* not CDT_ONLY */
6796
/* Return the base triangle. */
6797
triedgecopy(besttri, *lastedge);
6800
/*****************************************************************************/
6802
/* deletesite() Delete a vertex from a Delaunay triangulation, ensuring */
6803
/* that the triangulation remains Delaunay. */
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. */
6809
/* Only interior points that do not lie on segments (shell edges) or */
6810
/* boundaries may be deleted. */
6812
/*****************************************************************************/
6817
struct triedge *deltri)
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;
6828
triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
6829
shelle sptr; /* Temporary variable used by tspivot(). */
6831
org(*deltri, delpoint);
6833
printf(" Deleting (%.12g, %.12g).\n", delpoint[0], delpoint[1]);
6835
pointdealloc(delpoint);
6837
/* Count the degree of the point being deleted. */
6838
onext(*deltri, countingtri);
6840
while (!triedgeequal(*deltri, countingtri)) {
6842
if (countingtri.tri == dummytri) {
6843
printf("Internal error in deletesite():\n");
6844
printf(" Attempt to delete boundary point.\n");
6847
#endif /* SELF_CHECK */
6849
onextself(countingtri);
6853
if (edgecount < 3) {
6854
printf("Internal error in deletesite():\n Point has degree %d.\n",
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);
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);
6879
tspivot(righttri, rightshelle);
6880
if (rightshelle.sh != dummysh) {
6881
tsbond(deltriright, rightshelle);
6884
/* Set the new origin of `deltri' and check its quality. */
6885
org(lefttri, neworg);
6886
setorg(*deltri, neworg);
6888
testtriangle(deltri);
6891
/* Delete the two spliced-out triangles. */
6892
triangledealloc(lefttri.tri);
6893
triangledealloc(righttri.tri);
6896
#endif /* not CDT_ONLY */
6900
/********* Mesh transformation routines end here *********/
6902
/********* Divide-and-conquer Delaunay triangulation begins here *********/
6906
/*****************************************************************************/
6908
/* The divide-and-conquer bounding box */
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. */
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. */
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 */
6932
/* The bounding box also makes it easy to traverse the convex hull, as the */
6933
/* divide-and-conquer algorithm needs to do. */
6935
/*****************************************************************************/
6937
/*****************************************************************************/
6939
/* pointsort() Sort an array of points by x-coordinate, using the */
6940
/* y-coordinate as a secondary key. */
6942
/* Uses quicksort. Randomized O(n log n) time. No, I did not make any of */
6943
/* the usual quicksort mistakes. */
6945
/*****************************************************************************/
6953
REAL pivotx, pivoty;
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;
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. */
6974
while (left < right) {
6975
/* Search for a point whose x-coordinate is too large for the 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. */
6984
} while ((left <= right) && ((sortarray[right][0] > pivotx) ||
6985
((sortarray[right][0] == pivotx) &&
6986
(sortarray[right][1] > pivoty))));
6988
/* Swap the left and right points. */
6989
temp = sortarray[left];
6990
sortarray[left] = sortarray[right];
6991
sortarray[right] = temp;
6995
/* Recursively sort the left subset. */
6996
pointsort(sortarray, left);
6998
if (right < arraysize - 2) {
6999
/* Recursively sort the right subset. */
7000
pointsort(&sortarray[right + 1], arraysize - right - 1);
7004
/*****************************************************************************/
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. */
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. */
7014
/*****************************************************************************/
7024
REAL pivot1, pivot2;
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;
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. */
7045
while (left < right) {
7046
/* Search for a point whose x-coordinate is too large for the 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. */
7055
} while ((left <= right) && ((sortarray[right][axis] > pivot1) ||
7056
((sortarray[right][axis] == pivot1) &&
7057
(sortarray[right][1 - axis] > pivot2))));
7059
/* Swap the left and right points. */
7060
temp = sortarray[left];
7061
sortarray[left] = sortarray[right];
7062
sortarray[right] = temp;
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);
7071
if (right < median - 1) {
7072
/* Recursively shuffle the right subset. */
7073
pointmedian(&sortarray[right + 1], arraysize - right - 1,
7074
median - right - 1, axis);
7078
/*****************************************************************************/
7080
/* alternateaxes() Sorts the points as appropriate for the divide-and- */
7081
/* conquer algorithm with alternating cuts. */
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. */
7087
/*****************************************************************************/
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. */
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) {
7107
alternateaxes(sortarray, divider, 1 - axis);
7109
alternateaxes(&sortarray[divider], arraysize - divider, 1 - axis);
7113
/*****************************************************************************/
7115
/* mergehulls() Merge two adjacent Delaunay triangulations into a */
7116
/* single Delaunay triangulation. */
7118
/* This is similar to the algorithm given by Guibas and Stolfi, but uses */
7119
/* a triangle-based, rather than edge-based, data structure. */
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. */
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. */
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. */
7142
/* On completion, the origin of `farleft' is the leftmost vertex of the */
7143
/* merged triangulation, and the destination of `farright' is the rightmost */
7146
/*****************************************************************************/
7149
struct triedge *farleft,
7150
struct triedge *innerleft,
7151
struct triedge *innerright,
7152
struct triedge *farright,
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;
7171
int leftfinished, rightfinished;
7172
triangle ptr; /* Temporary variable used by sym(). */
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);
7190
farleftpt = farleftapex;
7191
apex(*farleft, farleftapex);
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);
7202
while (innerrightapex[1] < innerrightorg[1]) {
7203
lnextself(*innerright);
7204
symself(*innerright);
7205
innerrightorg = innerrightapex;
7206
apex(*innerright, innerrightapex);
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);
7218
/* Find a line tangent to and below both hulls. */
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);
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);
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. */
7252
printf(" Creating base bounding ");
7253
printtriangle(&baseedge);
7255
/* Fix the extreme triangles if necessary. */
7256
org(*farleft, farleftpt);
7257
if (innerleftdest == farleftpt) {
7258
lnext(baseedge, *farleft);
7260
dest(*farright, farrightpt);
7261
if (innerrightorg == farrightpt) {
7262
lprev(baseedge, *farright);
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. */
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);
7291
printf(" Creating top bounding ");
7292
printtriangle(&baseedge);
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);
7311
while (farrightapex[0] > farrightpt[0]) {
7312
lprevself(*farright);
7314
farrightpt = farrightapex;
7315
apex(*farright, farrightapex);
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);
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;
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)
7361
/* Avoid eating right through the triangulation. */
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);
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;
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)
7408
/* Avoid eating right through the triangulation. */
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);
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);
7435
printf(" Connecting ");
7436
printtriangle(&baseedge);
7441
/*****************************************************************************/
7443
/* divconqrecurse() Recursively form a Delaunay triangulation by the */
7444
/* divide-and-conquer method. */
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. */
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). */
7456
/*****************************************************************************/
7458
void divconqrecurse(
7462
struct triedge *farleft,
7463
struct triedge *farright)
7465
struct triedge midtri, tri1, tri2, tri3;
7466
struct triedge innerleft, innerright;
7471
printf(" Triangulating %d points.\n", vertices);
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);
7492
printf(" Creating ");
7493
printtriangle(farleft);
7494
printf(" Creating ");
7495
printtriangle(farright);
7497
/* Ensure that the origin of `farleft' is sortarray[0]. */
7498
lprev(*farright, *farleft);
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]);
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. */
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);
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. */
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]);
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]);
7562
/* The topology does not depend on how the vertices are ordered. */
7577
/* Ensure that the origin of `farleft' is sortarray[0]. */
7578
triedgecopy(tri1, *farleft);
7579
/* Ensure that the destination of `farright' is sortarray[2]. */
7581
triedgecopy(tri2, *farright);
7583
lnext(*farleft, *farright);
7587
printf(" Creating ");
7588
printtriangle(&midtri);
7589
printf(" Creating ");
7590
printtriangle(&tri1);
7591
printf(" Creating ");
7592
printtriangle(&tri2);
7593
printf(" Creating ");
7594
printtriangle(&tri3);
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);
7605
printf(" Joining triangulations with %d and %d vertices.\n", divider,
7606
vertices - divider);
7608
/* Merge the two triangulations into one. */
7609
mergehulls(farleft, &innerleft, &innerright, farright, axis);
7614
struct triedge *startghost)
7616
struct triedge searchedge;
7617
struct triedge dissolveedge;
7618
struct triedge deadtri;
7621
triangle ptr; /* Temporary variable used by sym(). */
7624
printf(" Removing ghost triangles.\n");
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);
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. */
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);
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));
7659
/*****************************************************************************/
7661
/* divconqdelaunay() Form a Delaunay triangulation by the divide-and- */
7662
/* conquer method. */
7664
/* Sorts the points, calls a recursive procedure to triangulate them, and */
7665
/* removes the bounding box, setting boundary markers as appropriate. */
7667
/*****************************************************************************/
7669
long divconqdelaunay()
7672
struct triedge hullleft, hullright;
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");
7682
traversalinit(&points);
7683
for (i = 0; i < inpoints; i++) {
7684
sortarray[i] = pointtraverse();
7687
printf(" Sorting points.\n");
7689
/* Sort the points. */
7690
pointsort(sortarray, inpoints);
7691
/* Discard duplicate points, which can really mess up the algorithm. */
7693
for (j = 1; j < inpoints; j++) {
7694
if ((sortarray[i][0] == sortarray[j][0])
7695
&& (sortarray[i][1] == sortarray[j][1])) {
7698
"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
7699
sortarray[j][0], sortarray[j][1]);
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);
7707
sortarray[i] = sortarray[j];
7712
/* Re-sort the array of points to accommodate alternating cuts. */
7714
if (i - divider >= 2) {
7716
alternateaxes(sortarray, divider, 1);
7718
alternateaxes(&sortarray[divider], i - divider, 1);
7722
printf(" Forming triangulation.\n");
7724
/* Form the Delaunay triangulation. */
7725
divconqrecurse(sortarray, i, 0, &hullleft, &hullright);
7728
return removeghosts(&hullleft);
7733
/********* Divide-and-conquer Delaunay triangulation ends here *********/
7735
/********* Incremental Delaunay triangulation begins here *********/
7739
/*****************************************************************************/
7741
/* boundingbox() Form an "infinite" bounding triangle to insert points */
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. */
7748
/*****************************************************************************/
7754
struct triedge inftri; /* Handle for the triangular bounding box. */
7758
printf(" Creating triangular bounding box.\n");
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;
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");
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;
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;
7793
printf(" Creating ");
7794
printtriangle(&inftri);
7798
#endif /* not REDUCED */
7800
/*****************************************************************************/
7802
/* removebox() Remove the "infinite" bounding triangle, setting boundary */
7803
/* markers as appropriate. */
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. */
7810
/*****************************************************************************/
7816
struct triedge deadtri;
7817
struct triedge searchedge;
7818
struct triedge checkedge;
7819
struct triedge nextedge, finaledge, dissolveedge;
7822
triangle ptr; /* Temporary variable used by sym(). */
7825
printf(" Removing triangular bounding box.\n");
7827
/* Find a boundary triangle. */
7828
nextedge.tri = dummytri;
7829
nextedge.orient = 0;
7831
/* Mark a place to stop. */
7832
lprev(nextedge, finaledge);
7833
lnextself(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);
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);
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);
7854
while (!triedgeequal(nextedge, finaledge)) {
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.) */
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);
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);
7884
triangledealloc(finaledge.tri);
7886
free(infpoint1); /* Deallocate the bounding box vertices. */
7893
#endif /* not REDUCED */
7895
/*****************************************************************************/
7897
/* incrementaldelaunay() Form a Delaunay triangulation by incrementally */
7898
/* adding vertices. */
7900
/*****************************************************************************/
7904
long incrementaldelaunay()
7906
struct triedge starttri;
7910
/* Create a triangular bounding box. */
7913
printf(" Incrementally inserting points.\n");
7915
traversalinit(&points);
7916
pointloop = pointtraverse();
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) ==
7925
"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
7926
pointloop[0], pointloop[1]);
7928
/* Commented out - would eliminate point from output .node file.
7929
setpointmark(pointloop, DEADPOINT);
7932
pointloop = pointtraverse();
7935
/* Remove the bounding box. */
7939
#endif /* not REDUCED */
7943
/********* Incremental Delaunay triangulation ends here *********/
7945
/********* Sweepline Delaunay triangulation begins here *********/
7951
void eventheapinsert(
7952
struct event **heap,
7954
struct event *newevent)
7956
REAL eventx, eventy;
7961
eventx = newevent->xkey;
7962
eventy = newevent->ykey;
7963
eventnum = heapsize;
7964
notdone = eventnum > 0;
7966
parent = (eventnum - 1) >> 1;
7967
if ((heap[parent]->ykey < eventy) ||
7968
((heap[parent]->ykey == eventy)
7969
&& (heap[parent]->xkey <= eventx))) {
7972
heap[eventnum] = heap[parent];
7973
heap[eventnum]->heapposition = eventnum;
7976
notdone = eventnum > 0;
7979
heap[eventnum] = newevent;
7980
newevent->heapposition = eventnum;
7983
#endif /* not REDUCED */
7988
struct event **heap,
7992
struct event *thisevent;
7993
REAL eventx, eventy;
7994
int leftchild, rightchild;
7998
thisevent = heap[eventnum];
7999
eventx = thisevent->xkey;
8000
eventy = thisevent->ykey;
8001
leftchild = 2 * eventnum + 1;
8002
notdone = leftchild < heapsize;
8004
if ((heap[leftchild]->ykey < eventy) ||
8005
((heap[leftchild]->ykey == eventy)
8006
&& (heap[leftchild]->xkey < eventx))) {
8007
smallest = leftchild;
8009
smallest = eventnum;
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;
8019
if (smallest == eventnum) {
8022
heap[eventnum] = heap[smallest];
8023
heap[eventnum]->heapposition = eventnum;
8024
heap[smallest] = thisevent;
8025
thisevent->heapposition = smallest;
8027
eventnum = smallest;
8028
leftchild = 2 * eventnum + 1;
8029
notdone = leftchild < heapsize;
8034
#endif /* not REDUCED */
8038
void eventheapdelete(
8039
struct event **heap,
8043
struct event *moveevent;
8044
REAL eventx, eventy;
8048
moveevent = heap[heapsize - 1];
8050
eventx = moveevent->xkey;
8051
eventy = moveevent->ykey;
8053
parent = (eventnum - 1) >> 1;
8054
if ((heap[parent]->ykey < eventy) ||
8055
((heap[parent]->ykey == eventy)
8056
&& (heap[parent]->xkey <= eventx))) {
8059
heap[eventnum] = heap[parent];
8060
heap[eventnum]->heapposition = eventnum;
8063
notdone = eventnum > 0;
8067
heap[eventnum] = moveevent;
8068
moveevent->heapposition = eventnum;
8069
eventheapify(heap, heapsize - 1, eventnum);
8072
#endif /* not REDUCED */
8076
void createeventheap(
8077
struct event ***eventheap,
8078
struct event **events,
8079
struct event **freeevents)
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");
8091
*events = (struct event *) malloc(maxevents * sizeof(struct event));
8092
if (*events == (struct event *) NULL) {
8093
printf("Error: Out of memory.\n");
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);
8104
*freeevents = (struct event *) NULL;
8105
for (i = maxevents - 1; i >= inpoints; i--) {
8106
(*events)[i].eventptr = (VOID *) *freeevents;
8107
*freeevents = *events + i;
8111
#endif /* not REDUCED */
8115
int rightofhyperbola(
8116
struct triedge *fronttri,
8119
point leftpoint, rightpoint;
8120
REAL dxa, dya, dxb, dyb;
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]) {
8132
if (newsite[0] <= leftpoint[0]) {
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);
8143
#endif /* not REDUCED */
8153
REAL xac, yac, xbc, ybc, xab, yab;
8154
REAL aclen2, bclen2, ablen2;
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))
8171
#endif /* not REDUCED */
8175
void check4deadevent(
8176
struct triedge *checktri,
8177
struct event **freeevents,
8178
struct event **eventheap,
8181
struct event *deadevent;
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);
8193
setorg(*checktri, NULL);
8197
#endif /* not REDUCED */
8201
struct splaynode *splay(
8202
struct splaynode *splaytree,
8204
struct triedge *searchtri)
8206
struct splaynode *child, *grandchild;
8207
struct splaynode *lefttree, *righttree;
8208
struct splaynode *leftright;
8210
int rightofroot, rightofchild;
8212
if (splaytree == (struct splaynode *) NULL) {
8213
return (struct splaynode *) NULL;
8215
dest(splaytree->keyedge, checkpoint);
8216
if (checkpoint == splaytree->keydest) {
8217
rightofroot = rightofhyperbola(&splaytree->keyedge, searchpoint);
8219
triedgecopy(splaytree->keyedge, *searchtri);
8220
child = splaytree->rchild;
8222
child = splaytree->lchild;
8224
if (child == (struct splaynode *) NULL) {
8227
dest(child->keyedge, checkpoint);
8228
if (checkpoint != child->keydest) {
8229
child = splay(child, searchpoint, searchtri);
8230
if (child == (struct splaynode *) NULL) {
8232
splaytree->rchild = (struct splaynode *) NULL;
8234
splaytree->lchild = (struct splaynode *) NULL;
8239
rightofchild = rightofhyperbola(&child->keyedge, searchpoint);
8241
triedgecopy(child->keyedge, *searchtri);
8242
grandchild = splay(child->rchild, searchpoint, searchtri);
8243
child->rchild = grandchild;
8245
grandchild = splay(child->lchild, searchpoint, searchtri);
8246
child->lchild = grandchild;
8248
if (grandchild == (struct splaynode *) NULL) {
8250
splaytree->rchild = child->lchild;
8251
child->lchild = splaytree;
8253
splaytree->lchild = child->rchild;
8254
child->rchild = splaytree;
8260
splaytree->rchild = child->lchild;
8261
child->lchild = splaytree;
8263
splaytree->lchild = grandchild->rchild;
8264
grandchild->rchild = splaytree;
8266
child->rchild = grandchild->lchild;
8267
grandchild->lchild = child;
8270
splaytree->rchild = grandchild->lchild;
8271
grandchild->lchild = splaytree;
8273
splaytree->lchild = child->rchild;
8274
child->rchild = splaytree;
8276
child->lchild = grandchild->rchild;
8277
grandchild->rchild = child;
8281
lefttree = splay(splaytree->lchild, searchpoint, searchtri);
8282
righttree = splay(splaytree->rchild, searchpoint, searchtri);
8284
pooldealloc(&splaynodes, (VOID *) splaytree);
8285
if (lefttree == (struct splaynode *) NULL) {
8287
} else if (righttree == (struct splaynode *) NULL) {
8289
} else if (lefttree->rchild == (struct splaynode *) NULL) {
8290
lefttree->rchild = righttree->lchild;
8291
righttree->lchild = lefttree;
8293
} else if (righttree->lchild == (struct splaynode *) NULL) {
8294
righttree->lchild = lefttree->rchild;
8295
lefttree->rchild = righttree;
8298
/* printf("Holy Toledo!!!\n"); */
8299
leftright = lefttree->rchild;
8300
while (leftright->rchild != (struct splaynode *) NULL) {
8301
leftright = leftright->rchild;
8303
leftright->rchild = righttree;
8309
#endif /* not REDUCED */
8313
struct splaynode *splayinsert(
8314
struct splaynode *splayroot,
8315
struct triedge *newkey,
8318
struct splaynode *newsplaynode;
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;
8331
newsplaynode->lchild = splayroot->lchild;
8332
newsplaynode->rchild = splayroot;
8333
splayroot->lchild = (struct splaynode *) NULL;
8335
return newsplaynode;
8338
#endif /* not REDUCED */
8342
struct splaynode *circletopinsert(
8343
struct splaynode *splayroot,
8344
struct triedge *newkey,
8351
REAL xac, yac, xbc, ybc;
8352
REAL aclen2, bclen2;
8353
REAL searchpoint[2];
8354
struct triedge dummytri;
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);
8369
#endif /* not REDUCED */
8373
struct splaynode *frontlocate(
8374
struct splaynode *splayroot,
8375
struct triedge *bottommost,
8377
struct triedge *searchtri,
8381
triangle ptr; /* Temporary variable used by onext(). */
8383
triedgecopy(*bottommost, *searchtri);
8384
splayroot = splay(splayroot, searchpoint, searchtri);
8387
while (!farrightflag && rightofhyperbola(searchtri, searchpoint)) {
8388
onextself(*searchtri);
8389
farrightflag = triedgeequal(*searchtri, *bottommost);
8391
*farright = farrightflag;
8395
#endif /* not REDUCED */
8399
long sweeplinedelaunay()
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;
8415
point leftpoint, midpoint, rightpoint;
8416
REAL lefttest, righttest;
8418
int check4events, farrightflag;
8419
triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
8421
poolinit(&splaynodes, sizeof(struct splaynode), SPLAYNODEPERBLOCK, POINTER,
8423
splayroot = (struct splaynode *) NULL;
8426
printf(" Placing points in event heap.\n");
8428
createeventheap(&eventheap, &events, &freeevents);
8429
heapsize = inpoints;
8432
printf(" Forming triangulation.\n");
8434
maketriangle(&lefttri);
8435
maketriangle(&righttri);
8436
bond(lefttri, righttri);
8438
lprevself(righttri);
8439
bond(lefttri, righttri);
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);
8449
if (heapsize == 0) {
8450
printf("Error: Input points are all identical.\n");
8453
secondpoint = (point) eventheap[0]->eventptr;
8454
eventheap[0]->eventptr = (VOID *) freeevents;
8455
freeevents = eventheap[0];
8456
eventheapdelete(eventheap, heapsize, 0);
8458
if ((firstpoint[0] == secondpoint[0])
8459
&& (firstpoint[1] == secondpoint[1])) {
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);
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);
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);
8487
if (triedgeequal(farlefttri, bottommost)) {
8488
lprev(fliptri, bottommost);
8491
setapex(fliptri, NULL);
8492
lprev(fliptri, lefttri);
8493
lnext(fliptri, righttri);
8494
sym(lefttri, farlefttri);
8496
if (randomnation(SAMPLERATE) == 0) {
8498
dest(fliptri, leftpoint);
8499
apex(fliptri, midpoint);
8500
org(fliptri, rightpoint);
8501
splayroot = circletopinsert(splayroot, &lefttri, leftpoint, midpoint,
8502
rightpoint, nextevent->ykey);
8505
nextpoint = (point) nextevent->eventptr;
8506
if ((nextpoint[0] == lastpoint[0]) && (nextpoint[1] == lastpoint[1])) {
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);
8515
lastpoint = nextpoint;
8517
splayroot = frontlocate(splayroot, &bottommost, nextpoint, &searchtri,
8520
triedgecopy(bottommost, searchtri);
8522
while (!farrightflag && rightofhyperbola(&searchtri, nextpoint)) {
8523
onextself(searchtri);
8524
farrightflag = triedgeequal(searchtri, bottommost);
8528
check4deadevent(&searchtri, &freeevents, eventheap, &heapsize);
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);
8541
lprevself(righttri);
8542
bond(lefttri, righttri);
8544
lprevself(righttri);
8545
bond(lefttri, farlefttri);
8546
bond(righttri, farrighttri);
8547
if (!farrightflag && triedgeequal(farrighttri, bottommost)) {
8548
triedgecopy(lefttri, bottommost);
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);
8559
nextevent->eventptr = (VOID *) freeevents;
8560
freeevents = nextevent;
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,
8573
newevent->eventptr = (VOID *) encode(lefttri);
8574
eventheapinsert(eventheap, heapsize, newevent);
8576
setorg(lefttri, newevent);
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,
8588
newevent->eventptr = (VOID *) encode(farrighttri);
8589
eventheapinsert(eventheap, heapsize, newevent);
8591
setorg(farrighttri, newevent);
8596
pooldeinit(&splaynodes);
8597
lprevself(bottommost);
8598
return removeghosts(&bottommost);
8601
#endif /* not REDUCED */
8605
/********* Sweepline Delaunay triangulation ends here *********/
8607
/********* General mesh construction routines begin here *********/
8611
/*****************************************************************************/
8613
/* delaunay() Form a Delaunay triangulation. */
8615
/*****************************************************************************/
8620
initializetrisegpools();
8625
"Constructing Delaunay triangulation by divide-and-conquer method.\n");
8627
return divconqdelaunay();
8628
#else /* not REDUCED */
8630
printf("Constructing Delaunay triangulation ");
8632
printf("by incremental method.\n");
8633
} else if (sweepline) {
8634
printf("by sweepline method.\n");
8636
printf("by divide-and-conquer method.\n");
8640
return incrementaldelaunay();
8641
} else if (sweepline) {
8642
return sweeplinedelaunay();
8644
return divconqdelaunay();
8646
#endif /* not REDUCED */
8649
/*****************************************************************************/
8651
/* reconstruct() Reconstruct a triangulation from its .ele (and possibly */
8652
/* .poly) file. Used when the -r switch is used. */
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. */
8660
/* Points that are not corners of triangles, such as nodes on edges of */
8661
/* subparametric elements, are discarded. */
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. */
8672
/*****************************************************************************/
8680
REAL *triangleattriblist,
8681
REAL *trianglearealist,
8686
int *segmentmarkerlist,
8687
int numberofsegments)
8689
#else /* not TRILIBRARY */
8697
#endif /* not TRILIBRARY */
8703
#else /* not TRILIBRARY */
8706
char inputline[INPUTLINESIZE];
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;
8720
point checkdest, checkapex;
8733
int elementnumber, segmentnumber;
8735
triangle ptr; /* Temporary variable used by sym(). */
8738
inelements = elements;
8739
incorners = corners;
8740
if (incorners < 3) {
8741
printf("Error: Triangles must have at least 3 points.\n");
8745
#else /* not TRILIBRARY */
8746
/* Read the triangles from an .ele file. */
8748
printf("Opening %s.\n", elefilename);
8750
elefile = fopen(elefilename, "r");
8751
if (elefile == (FILE *) NULL) {
8752
printf(" Error: Cannot access file %s.\n", elefilename);
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') {
8763
incorners = (int) strtol (stringptr, &stringptr, 0);
8764
if (incorners < 3) {
8765
printf("Error: Triangles in %s must have at least 3 points.\n",
8770
stringptr = findfield(stringptr);
8771
if (*stringptr == '\0') {
8774
eextras = (int) strtol (stringptr, &stringptr, 0);
8776
#endif /* not TRILIBRARY */
8778
initializetrisegpools();
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;
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') {
8800
segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
8802
#endif /* not TRILIBRARY */
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;
8815
#else /* not TRILIBRARY */
8817
/* Open an .area file, check for consistency with the .ele file. */
8819
printf("Opening %s.\n", areafilename);
8821
areafile = fopen(areafilename, "r");
8822
if (areafile == (FILE *) NULL) {
8823
printf(" Error: Cannot access file %s.\n", areafilename);
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);
8834
#endif /* not TRILIBRARY */
8837
printf("Reconstructing mesh.\n");
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");
8847
/* Each point is initially unrepresented. */
8848
for (i = 0; i < points.items; i++) {
8849
vertexarray[i] = (triangle) dummytri;
8853
printf(" Assembling triangles.\n");
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) {
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",
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);
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",
8890
#endif /* not TRILIBRARY */
8892
/* Find out about (and throw away) extra nodes. */
8893
for (j = 3; j < incorners; j++) {
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);
8911
#endif /* not TRILIBRARY */
8914
/* Read the triangle's attributes. */
8915
for (j = 0; j < eextras; j++) {
8917
setelemattribute(triangleloop, j, triangleattriblist[attribindex++]);
8918
#else /* not TRILIBRARY */
8919
stringptr = findfield(stringptr);
8920
if (*stringptr == '\0') {
8921
setelemattribute(triangleloop, j, 0);
8923
setelemattribute(triangleloop, j,
8924
(REAL) strtod (stringptr, &stringptr));
8926
#endif /* not 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. */
8939
area = (REAL) strtod(stringptr, &stringptr);
8941
#endif /* not TRILIBRARY */
8942
setareabound(triangleloop, area);
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. */
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);
8974
if (tdest == checkapex) {
8975
/* The two triangles share an edge; bond them together. */
8976
lprev(checktri, checkleft);
8977
bond(triangleloop, checkleft);
8979
/* Find the next triangle in the stack. */
8980
nexttri = checktri.tri[6 + checktri.orient];
8981
decode(nexttri, checktri);
8982
} while (checktri.tri != dummytri);
8985
triangleloop.tri = triangletraverse();
8991
#else /* not TRILIBRARY */
8996
#endif /* not TRILIBRARY */
8998
hullsize = 0; /* Prepare to count the boundary edges. */
9001
printf(" Marking segments in triangulation.\n");
9003
/* Read the segments from the .poly file, and link them */
9004
/* to their neighboring triangles. */
9006
traversalinit(&shelles);
9007
shelleloop.sh = shelletraverse();
9008
segmentnumber = firstnumber;
9009
while (shelleloop.sh != (shelle *) NULL) {
9011
end[0] = segmentlist[pointindex++];
9012
end[1] = segmentlist[pointindex++];
9013
if (segmentmarkers) {
9014
boundmarker = segmentmarkerlist[segmentnumber - firstnumber];
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,
9026
end[0] = (int) strtol (stringptr, &stringptr, 0);
9028
stringptr = findfield(stringptr);
9029
if (*stringptr == '\0') {
9030
printf("Error: Segment %d is missing its second endpoint in %s.\n",
9031
segmentnumber, polyfilename);
9034
end[1] = (int) strtol (stringptr, &stringptr, 0);
9036
if (segmentmarkers) {
9037
stringptr = findfield(stringptr);
9038
if (*stringptr == '\0') {
9041
boundmarker = (int) strtol (stringptr, &stringptr, 0);
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",
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);
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);
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);
9100
shelleloop.sh = shelletraverse();
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);
9122
decode(nexttri, checktri);
9130
#endif /* not CDT_ONLY */
9134
/********* General mesh construction routines end here *********/
9136
/********* Segment (shell edge) insertion begins here *********/
9140
/*****************************************************************************/
9142
/* finddirection() Find the first triangle on the path from one point */
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 */
9152
/* The return value notes whether the destination or apex of the found */
9153
/* triangle is collinear with the two points in question. */
9155
/*****************************************************************************/
9157
enum finddirectionresult finddirection(
9158
struct triedge *searchtri,
9161
struct triedge checktri;
9163
point leftpoint, rightpoint;
9164
REAL leftccw, rightccw;
9165
int leftflag, rightflag;
9166
triangle ptr; /* Temporary variable used by onext() and oprev(). */
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 */
9181
onext(*searchtri, checktri);
9182
if (checktri.tri == dummytri) {
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],
9195
printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
9198
apex(*searchtri, leftpoint);
9200
leftccw = counterclockwise(endpoint, startpoint, leftpoint);
9201
leftflag = leftccw > 0.0;
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],
9210
printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
9213
dest(*searchtri, rightpoint);
9215
rightccw = counterclockwise(startpoint, endpoint, rightpoint);
9216
rightflag = rightccw > 0.0;
9218
if (leftccw == 0.0) {
9219
return LEFTCOLLINEAR;
9220
} else if (rightccw == 0.0) {
9221
return RIGHTCOLLINEAR;
9227
/*****************************************************************************/
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. */
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. */
9239
/* On completion, splittri is a handle having the newly inserted */
9240
/* intersection point as its origin, and endpoint1 as its destination. */
9242
/*****************************************************************************/
9244
void segmentintersection(
9245
struct triedge *splittri,
9246
struct edge *splitshelle,
9251
point leftpoint, rightpoint;
9253
enum insertsiteresult success;
9254
enum finddirectionresult collinear;
9260
triangle ptr; /* Temporary variable used by onext(). */
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;
9275
printf("Internal error in segmentintersection():");
9276
printf(" Attempt to find intersection of parallel segments.\n");
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]);
9286
setpointmark(newpoint, mark(*splitshelle));
9289
" Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
9290
torg[0], torg[1], tdest[0], tdest[1], newpoint[0], newpoint[1]);
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");
9299
if (steinerleft > 0) {
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");
9315
/* `splittri' should have destination endpoint1. */
9318
/*****************************************************************************/
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. */
9325
/* Returns one if the entire segment is successfully inserted, and zero if */
9326
/* the job must be finished by conformingedge() or constrainededge(). */
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. */
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 */
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. */
9340
/* Otherwise, return zero. */
9342
/*****************************************************************************/
9345
struct triedge *searchtri,
9349
struct triedge crosstri;
9350
struct edge crossedge;
9351
point leftpoint, rightpoint;
9353
enum finddirectionresult collinear;
9354
shelle sptr; /* Temporary variable used by tspivot(). */
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);
9365
/* Insert a shell edge, if there isn't already one there. */
9366
insertshelle(searchtri, newmark);
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);
9383
lnext(*searchtri, crosstri);
9384
tspivot(crosstri, crossedge);
9385
/* Check for a crossing segment. */
9386
if (crossedge.sh == dummysh) {
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);
9400
/*****************************************************************************/
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 */
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. */
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. */
9417
/*****************************************************************************/
9422
void conformingedge(
9427
struct triedge searchtri1, searchtri2;
9428
struct edge brokenshelle;
9430
point midpoint1, midpoint2;
9431
enum insertsiteresult success;
9432
int result1, result2;
9434
shelle sptr; /* Temporary variable used by tspivot(). */
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]);
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]);
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) {
9454
printf(" Segment intersects existing point (%.12g, %.12g).\n",
9455
newpoint[0], newpoint[1]);
9457
/* Use the point that's already there. */
9458
pointdealloc(newpoint);
9459
org(searchtri1, newpoint);
9461
if (success == VIOLATINGPOINT) {
9463
printf(" Two segments intersect at (%.12g, %.12g).\n",
9464
newpoint[0], newpoint[1]);
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");
9475
/* The point has been inserted successfully. */
9476
if (steinerleft > 0) {
9480
triedgecopy(searchtri1, searchtri2);
9481
result1 = scoutsegment(&searchtri1, endpoint1, newmark);
9482
result2 = scoutsegment(&searchtri2, endpoint2, newmark);
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);
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);
9497
#endif /* not CDT_ONLY */
9498
#endif /* not REDUCED */
9500
/*****************************************************************************/
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. */
9506
/* This is a support routine for inserting segments into a constrained */
9507
/* Delaunay triangulation. */
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 */
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.) */
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. */
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. */
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.) */
9536
/*****************************************************************************/
9539
struct triedge *fixuptri,
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(). */
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) {
9555
tspivot(neartri, faredge);
9556
if (faredge.sh != dummysh) {
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. */
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. */
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. */
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) {
9586
/* Not locally Delaunay; go on to an edge flip. */
9587
} /* else fartri is inverted; remove it from the stack by flipping. */
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);
9595
/*****************************************************************************/
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. */
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. */
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. */
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. */
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). */
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 */
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. */
9647
/*****************************************************************************/
9649
void constrainededge(
9650
struct triedge *starttri,
9654
struct triedge fixuptri, fixuptri2;
9655
struct edge fixupedge;
9661
triangle ptr; /* Temporary variable used by sym() and oprev(). */
9662
shelle sptr; /* Temporary variable used by tspivot(). */
9664
org(*starttri, endpoint1);
9665
lnext(*starttri, fixuptri);
9667
/* `collision' indicates whether we have found a point directly */
9668
/* between endpoint1 and endpoint2. */
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);
9682
/* Check whether farpoint is to the left or right of the segment */
9683
/* being inserted, to decide which edge of fixuptri to dig */
9685
area = counterclockwise(endpoint1, endpoint2, farpoint);
9687
/* We've collided with a point between endpoint1 and endpoint2. */
9689
oprev(fixuptri, fixuptri2);
9690
/* Enforce the Delaunay condition around farpoint. */
9691
delaunayfixup(&fixuptri, 0);
9692
delaunayfixup(&fixuptri2, 1);
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);
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. */
9716
/* We've collided with a segment between endpoint1 and endpoint2. */
9718
/* Insert a point at the intersection. */
9719
segmentintersection(&fixuptri, &fixupedge, endpoint2);
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. */
9730
/* Insert the remainder of the segment. */
9731
if (!scoutsegment(&fixuptri, endpoint2, newmark)) {
9732
constrainededge(&fixuptri, endpoint2, newmark);
9737
/*****************************************************************************/
9739
/* insertsegment() Insert a PSLG segment into a triangulation. */
9741
/*****************************************************************************/
9748
struct triedge searchtri1, searchtri2;
9749
triangle encodedtri;
9751
triangle ptr; /* Temporary variable used by sym(). */
9754
printf(" Connecting (%.12g, %.12g) to (%.12g, %.12g).\n",
9755
endpoint1[0], endpoint1[1], endpoint2[0], endpoint2[1]);
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);
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) {
9773
"Internal error in insertsegment(): Unable to locate PSLG point\n");
9774
printf(" (%.12g, %.12g) in triangulation.\n",
9775
endpoint1[0], endpoint1[1]);
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. */
9787
/* The first endpoint may have changed if a collision with an intervening */
9788
/* vertex on the segment occurred. */
9789
org(searchtri1, endpoint1);
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);
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) {
9806
"Internal error in insertsegment(): Unable to locate PSLG point\n");
9807
printf(" (%.12g, %.12g) in triangulation.\n",
9808
endpoint2[0], endpoint2[1]);
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. */
9820
/* The second endpoint may have changed if a collision with an intervening */
9821
/* vertex on the segment occurred. */
9822
org(searchtri2, endpoint2);
9827
/* Insert vertices to force the segment into the triangulation. */
9828
conformingedge(endpoint1, endpoint2, newmark);
9830
#endif /* not CDT_ONLY */
9831
#endif /* not REDUCED */
9832
/* Insert the segment directly into the triangulation. */
9833
constrainededge(&searchtri1, endpoint2, newmark);
9837
#endif /* not CDT_ONLY */
9838
#endif /* not REDUCED */
9841
/*****************************************************************************/
9843
/* markhull() Cover the convex hull of a triangulation with shell edges. */
9845
/*****************************************************************************/
9849
struct triedge hulltri;
9850
struct triedge nexttri;
9851
struct triedge starttri;
9852
triangle ptr; /* Temporary variable used by sym() and oprev(). */
9854
/* Find a triangle handle on the hull. */
9855
hulltri.tri = dummytri;
9858
/* Remember where we started so we know when to stop. */
9859
triedgecopy(hulltri, starttri);
9860
/* Go once counterclockwise around the convex hull. */
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. */
9866
oprev(hulltri, nexttri);
9867
while (nexttri.tri != dummytri) {
9868
triedgecopy(nexttri, hulltri);
9869
oprev(hulltri, nexttri);
9871
} while (!triedgeequal(hulltri, starttri));
9874
/*****************************************************************************/
9876
/* formskeleton() Create the shell edges of a triangulation, including */
9877
/* PSLG edges and edges on the convex hull. */
9879
/* The PSLG edges are read from a .poly file. The return value is the */
9880
/* number of segments in the file. */
9882
/*****************************************************************************/
9888
int *segmentmarkerlist,
9889
int numberofsegments)
9891
#else /* not TRILIBRARY */
9893
int formskeleton(polyfile, polyfilename)
9897
#endif /* not TRILIBRARY */
9901
char polyfilename[6];
9903
#else /* not TRILIBRARY */
9904
char inputline[INPUTLINESIZE];
9906
#endif /* not TRILIBRARY */
9907
point endpoint1, endpoint2;
9916
printf("Inserting segments into Delaunay triangulation.\n");
9919
strcpy(polyfilename, "input");
9920
segments = numberofsegments;
9921
segmentmarkers = segmentmarkerlist != (int *) NULL;
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') {
9932
segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
9934
#endif /* not TRILIBRARY */
9935
/* If segments are to be inserted, compute a mapping */
9936
/* from points to triangles. */
9939
printf(" Inserting PSLG segments.\n");
9945
/* Read and insert the segments. */
9946
for (i = 1; i <= segments; i++) {
9948
end1 = segmentlist[index++];
9949
end2 = segmentlist[index++];
9950
if (segmentmarkers) {
9951
boundmarker = segmentmarkerlist[i - 1];
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,
9961
end1 = (int) strtol (stringptr, &stringptr, 0);
9963
stringptr = findfield(stringptr);
9964
if (*stringptr == '\0') {
9965
printf("Error: Segment %d is missing its second endpoint in %s.\n", i,
9969
end2 = (int) strtol (stringptr, &stringptr, 0);
9971
if (segmentmarkers) {
9972
stringptr = findfield(stringptr);
9973
if (*stringptr == '\0') {
9976
boundmarker = (int) strtol (stringptr, &stringptr, 0);
9979
#endif /* not TRILIBRARY */
9980
if ((end1 < firstnumber) || (end1 >= firstnumber + inpoints)) {
9982
printf("Warning: Invalid first endpoint of segment %d in %s.\n", i,
9985
} else if ((end2 < firstnumber) || (end2 >= firstnumber + inpoints)) {
9987
printf("Warning: Invalid second endpoint of segment %d in %s.\n", i,
9991
endpoint1 = getpoint(end1);
9992
endpoint2 = getpoint(end2);
9993
if ((endpoint1[0] == endpoint2[0]) && (endpoint1[1] == endpoint2[1])) {
9995
printf("Warning: Endpoints of segment %d are coincident in %s.\n",
9999
insertsegment(endpoint1, endpoint2, boundmarker);
10006
if (convex || !poly) {
10007
/* Enclose the convex hull with shell edges. */
10009
printf(" Enclosing convex hull with segments.\n");
10018
/********* Segment (shell edge) insertion ends here *********/
10020
/********* Carving out holes and concavities begins here *********/
10024
/*****************************************************************************/
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. */
10030
/*****************************************************************************/
10034
struct triedge hulltri;
10035
struct triedge nexttri;
10036
struct triedge starttri;
10037
struct edge hulledge;
10038
triangle **deadtri;
10040
triangle ptr; /* Temporary variable used by sym(). */
10041
shelle sptr; /* Temporary variable used by tspivot(). */
10044
printf(" Marking concavities (external triangles) for elimination.\n");
10046
/* Find a triangle handle on the hull. */
10047
hulltri.tri = dummytri;
10048
hulltri.orient = 0;
10050
/* Remember where we started so we know when to stop. */
10051
triedgecopy(hulltri, starttri);
10052
/* Go once counterclockwise around the convex hull. */
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. */
10061
deadtri = (triangle **) poolalloc(&viri);
10062
*deadtri = hulltri.tri;
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);
10072
if (pointmark(hdest) == 0) {
10073
setpointmark(hdest, 1);
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);
10085
} while (!triedgeequal(hulltri, starttri));
10088
/*****************************************************************************/
10090
/* plague() Spread the virus from all infected triangles to any neighbors */
10091
/* not protected by shell edges. Delete all infected triangles. */
10093
/* This is the procedure that actually creates holes and concavities. */
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. */
10100
/* The second phase actually eliminates the infected triangles. It also */
10101
/* eliminates orphaned points. */
10103
/*****************************************************************************/
10107
struct triedge testtri;
10108
struct triedge neighbor;
10109
triangle **virusloop;
10110
triangle **deadtri;
10111
struct edge neighborshelle;
10114
point deadorg, deaddest, deadapex;
10116
triangle ptr; /* Temporary variable used by sym() and onext(). */
10117
shelle sptr; /* Temporary variable used by tspivot(). */
10120
printf(" Marking neighbors of marked triangles.\n");
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. */
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]);
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);
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. */
10170
org(neighbor, deadorg);
10171
dest(neighbor, deaddest);
10172
apex(neighbor, deadapex);
10174
" Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10175
deadorg[0], deadorg[1], deaddest[0], deaddest[1],
10176
deadapex[0], deadapex[1]);
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);
10189
org(neighbor, norg);
10190
dest(neighbor, ndest);
10191
if (pointmark(norg) == 0) {
10192
setpointmark(norg, 1);
10194
if (pointmark(ndest) == 0) {
10195
setpointmark(ndest, 1);
10200
/* Remark the triangle as infected, so it doesn't get added to the */
10201
/* virus pool again. */
10203
virusloop = (triangle **) traverse(&viri);
10207
printf(" Deleting marked triangles.\n");
10209
traversalinit(&viri);
10210
virusloop = (triangle **) traverse(&viri);
10211
while (virusloop != (triangle **) NULL) {
10212
testtri.tri = *virusloop;
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) {
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);
10233
/* A live triangle. The point survives. */
10236
/* Walk counterclockwise about the point. */
10237
onextself(neighbor);
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);
10249
/* A live triangle. The point survives. */
10252
/* Walk clockwise about the point. */
10253
oprevself(neighbor);
10258
printf(" Deleting point (%.12g, %.12g)\n",
10259
testpoint[0], testpoint[1]);
10261
pointdealloc(testpoint);
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. */
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. */
10283
/* Return the dead triangle to the pool of triangles. */
10284
triangledealloc(testtri.tri);
10285
virusloop = (triangle **) traverse(&viri);
10287
/* Empty the virus pool. */
10288
poolrestart(&viri);
10291
/*****************************************************************************/
10293
/* regionplague() Spread regional attributes and/or area constraints */
10294
/* (from a .poly file) throughout the mesh. */
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. */
10301
/* The second phase uninfects all infected triangles, returning them to */
10304
/*****************************************************************************/
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(). */
10320
printf(" Marking neighbors of marked triangles.\n");
10322
/* Loop through all the infected triangles, spreading the attribute */
10323
/* and/or area constraint to their neighbors, then to their 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. */
10334
if (regionattrib) {
10335
/* Set an attribute. */
10336
setelemattribute(testtri, eextras, attribute);
10339
/* Set an area constraint. */
10340
setareabound(testtri, area);
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]);
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)) {
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]);
10371
/* Infect the neighbor. */
10373
/* Ensure that the neighbor's neighbors will be infected. */
10374
regiontri = (triangle **) poolalloc(&viri);
10375
*regiontri = neighbor.tri;
10378
/* Remark the triangle as infected, so it doesn't get added to the */
10379
/* virus pool again. */
10381
virusloop = (triangle **) traverse(&viri);
10384
/* Uninfect all triangles. */
10386
printf(" Unmarking marked triangles.\n");
10388
traversalinit(&viri);
10389
virusloop = (triangle **) traverse(&viri);
10390
while (virusloop != (triangle **) NULL) {
10391
testtri.tri = *virusloop;
10393
virusloop = (triangle **) traverse(&viri);
10395
/* Empty the virus pool. */
10396
poolrestart(&viri);
10399
/*****************************************************************************/
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. */
10406
/* This routine mainly calls other routines to carry out all these */
10409
/*****************************************************************************/
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;
10425
triangle ptr; /* Temporary variable used by sym(). */
10427
if (!(quiet || (noholes && convex))) {
10428
printf("Removing unwanted triangles.\n");
10429
if (verbose && (holes > 0)) {
10430
printf(" Marking holes for elimination.\n");
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");
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);
10450
/* Mark as infected any unprotected triangles on the boundary. */
10451
/* This is one way by which concavities are created. */
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. */
10477
holetri = (triangle **) poolalloc(&viri);
10478
*holetri = searchtri.tri;
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.) */
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, ®ionlist[4 * i]) >
10509
/* Find a triangle that contains the region point. */
10510
intersect = locate(®ionlist[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]);
10521
if (viri.items > 0) {
10522
/* Carve the holes and concavities. */
10525
/* The virus pool should be empty now. */
10529
if (regionattrib) {
10531
printf("Spreading regional attributes and area constraints.\n");
10533
printf("Spreading regional attributes.\n");
10536
printf("Spreading regional area constraints.\n");
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();
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. */
10564
if (regionattrib && !refine) {
10565
/* Note the fact that each triangle has an additional attribute. */
10570
/* Free up memory. */
10571
if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
10581
/********* Carving out holes and concavities ends here *********/
10583
/********* Mesh quality maintenance begins here *********/
10587
/*****************************************************************************/
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. */
10592
/*****************************************************************************/
10598
struct edge edgeloop;
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();
10611
#endif /* not CDT_ONLY */
10613
/*****************************************************************************/
10615
/* precisionerror() Print an error message for precision problems. */
10617
/*****************************************************************************/
10621
void precisionerror()
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");
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 */
10632
#endif /* not CDT_ONLY */
10634
/*****************************************************************************/
10636
/* repairencs() Find and repair all the encroached segments. */
10638
/* Encroached segments are repaired by splitting them by inserting a point */
10639
/* at or near their centers. */
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. */
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 */
10649
/*****************************************************************************/
10656
struct triedge enctri;
10657
struct triedge testtri;
10658
struct edge *encloop;
10659
struct edge testsh;
10662
enum insertsiteresult success;
10663
REAL segmentlength, nearestpoweroftwo;
10665
int acuteorg, acutedest;
10668
triangle ptr; /* Temporary variable used by stpivot(). */
10669
shelle sptr; /* Temporary variable used by snext(). */
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.) */
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 */
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);
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;
10725
while (segmentlength < (0.5 * SQUAREROOTTWO) * nearestpoweroftwo) {
10726
nearestpoweroftwo *= 0.5;
10728
/* Where do we split the segment? */
10729
split = 0.5 * nearestpoweroftwo / segmentlength;
10731
split = 1.0 - split;
10734
/* If we're not worried about adjacent segments, split */
10735
/* this segment in the middle. */
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];
10745
setpointmark(newpoint, mark(*encloop));
10748
" Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
10749
eorg[0], eorg[1], edest[0], edest[1], newpoint[0], newpoint[1]);
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"
10759
printf(" arithmetic.\n");
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");
10770
if (steinerleft > 0) {
10773
/* Check the two new subsegments to see if they're encroached. */
10774
dummy = checkedge4encroach(encloop);
10775
snextself(*encloop);
10776
dummy = checkedge4encroach(encloop);
10778
badsegmentdealloc(encloop);
10779
encloop = badsegmenttraverse();
10784
#endif /* not CDT_ONLY */
10786
/*****************************************************************************/
10788
/* tallyfaces() Test every triangle in the mesh for quality measures. */
10790
/*****************************************************************************/
10796
struct triedge triangleloop;
10799
printf(" Making a list of bad triangles.\n");
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();
10811
#endif /* not CDT_ONLY */
10813
/*****************************************************************************/
10815
/* findcircumcenter() Find the circumcenter of a triangle. */
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. */
10823
/* The return value indicates which edge of the triangle is shortest. */
10825
/*****************************************************************************/
10827
enum circumcenterresult findcircumcenter(
10831
point circumcenter,
10835
REAL xdo, ydo, xao, yao, xad, yad;
10836
REAL dodist, aodist, addist;
10840
circumcentercount++;
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;
10850
denominator = 0.5 / (xdo * yao - xao * ydo);
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--;
10859
circumcenter[0] = torg[0] - (ydo * aodist - yao * dodist) * denominator;
10860
circumcenter[1] = torg[1] + (xdo * aodist - xao * dodist) * denominator;
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);
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;
10880
return OPPOSITEDEST;
10884
/*****************************************************************************/
10886
/* splittriangle() Inserts a point at the circumcenter of a triangle. */
10887
/* Deletes the newly inserted point if it encroaches upon */
10890
/*****************************************************************************/
10894
void splittriangle(
10895
struct badface *badtri)
10897
point borg, bdest, bapex;
10900
enum insertsiteresult success;
10901
enum circumcenterresult shortedge;
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)) {
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]);
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]))) {
10927
printf("Warning: New point (%.12g, %.12g) falls on existing vertex.\n"
10928
, newpoint[0], newpoint[1]);
10931
pointdealloc(newpoint);
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]);
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);
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) {
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. */
10967
"Warning: New point (%.12g, %.12g) falls on existing vertex.\n"
10968
, newpoint[0], newpoint[1]);
10971
pointdealloc(newpoint);
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]);
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");
10987
/* Return the bad triangle to the pool. */
10988
pooldealloc(&badtriangles, (VOID *) badtri);
10991
#endif /* not CDT_ONLY */
10993
/*****************************************************************************/
10995
/* enforcequality() Remove all the encroached edges and bad triangles */
10996
/* from the triangulation. */
10998
/*****************************************************************************/
11002
void enforcequality()
11007
printf("Adding Steiner points to enforce quality.\n");
11009
/* Initialize the pool of encroached segments. */
11010
poolinit(&badsegments, sizeof(struct edge), BADSEGMENTPERBLOCK, POINTER, 0);
11012
printf(" Looking for encroached segments.\n");
11014
/* Test all segments to see if they're encroached. */
11016
if (verbose && (badsegments.items > 0)) {
11017
printf(" Splitting encroached segments.\n");
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. */
11028
/* Now, find all the segments that became encroached while adding */
11029
/* points to split encroached segments. */
11032
/* At this point, if we haven't run out of Steiner points, the */
11033
/* triangulation should be (conforming) Delaunay. */
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,
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];
11045
/* Test all triangles to see if they're bad. */
11048
printf(" Splitting bad triangles.\n");
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) {
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. */
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");
11070
printf(" %ld encroached segments, and therefore might not be truly\n",
11071
badsegments.items);
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");
11079
#endif /* not CDT_ONLY */
11083
/********* Mesh quality maintenance ends here *********/
11085
/*****************************************************************************/
11087
/* highorder() Create extra nodes for quadratic subparametric elements. */
11089
/*****************************************************************************/
11093
struct triedge triangleloop, trisym;
11094
struct edge checkmark;
11098
triangle ptr; /* Temporary variable used by sym(). */
11099
shelle sptr; /* Temporary variable used by tspivot(). */
11102
printf("Adding vertices for second-order triangles.\n");
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;
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]);
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);
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));
11143
printf(" Creating (%.12g, %.12g).\n", newpoint[0], newpoint[1]);
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;
11153
triangleloop.tri = triangletraverse();
11157
/********* File I/O routines begin here *********/
11161
/*****************************************************************************/
11163
/* readline() Read a nonempty line from a file. */
11165
/* A line is considered "nonempty" if it contains something that looks like */
11168
/*****************************************************************************/
11172
char *readline(string, infile, infilename)
11179
/* Search for something that looks like a number. */
11181
result = fgets(string, INPUTLINESIZE, infile);
11182
if (result == (char *) NULL) {
11183
printf(" Error: Unexpected end of file in %s.\n", infilename);
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'))) {
11193
/* If it's a comment or end of line, read another line and try again. */
11194
} while ((*result == '#') || (*result == '\0'));
11198
#endif /* not TRILIBRARY */
11200
/*****************************************************************************/
11202
/* findfield() Find the next field of a string. */
11204
/* Jumps past the current field by searching for whitespace, then jumps */
11205
/* past the whitespace to find the next field. */
11207
/*****************************************************************************/
11211
char *findfield(string)
11217
/* Skip the current field. Stop upon reaching whitespace. */
11218
while ((*result != '\0') && (*result != '#')
11219
&& (*result != ' ') && (*result != '\t')) {
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'))) {
11229
/* Check for a comment (prefixed with `#'). */
11230
if (*result == '#') {
11236
#endif /* not TRILIBRARY */
11238
/*****************************************************************************/
11240
/* readnodes() Read the points from a file, which may be a .node or .poly */
11243
/*****************************************************************************/
11247
void readnodes(nodefilename, polyfilename, polyfile)
11248
char *nodefilename;
11249
char *polyfilename;
11254
char inputline[INPUTLINESIZE];
11264
/* Read the points from a .poly file. */
11266
printf("Opening %s.\n", polyfilename);
11268
*polyfile = fopen(polyfilename, "r");
11269
if (*polyfile == (FILE *) NULL) {
11270
printf(" Error: Cannot access file %s.\n", polyfilename);
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') {
11281
mesh_dim = (int) strtol (stringptr, &stringptr, 0);
11283
stringptr = findfield(stringptr);
11284
if (*stringptr == '\0') {
11287
nextras = (int) strtol (stringptr, &stringptr, 0);
11289
stringptr = findfield(stringptr);
11290
if (*stringptr == '\0') {
11293
nodemarkers = (int) strtol (stringptr, &stringptr, 0);
11295
if (inpoints > 0) {
11296
infile = *polyfile;
11297
infilename = polyfilename;
11300
/* If the .poly file claims there are zero points, that means that */
11301
/* the points should be read from a separate .node file. */
11303
infilename = innodefilename;
11307
infilename = innodefilename;
11308
*polyfile = (FILE *) NULL;
11311
if (readnodefile) {
11312
/* Read the points from a .node file. */
11314
printf("Opening %s.\n", innodefilename);
11316
infile = fopen(innodefilename, "r");
11317
if (infile == (FILE *) NULL) {
11318
printf(" Error: Cannot access file %s.\n", innodefilename);
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') {
11329
mesh_dim = (int) strtol (stringptr, &stringptr, 0);
11331
stringptr = findfield(stringptr);
11332
if (*stringptr == '\0') {
11335
nextras = (int) strtol (stringptr, &stringptr, 0);
11337
stringptr = findfield(stringptr);
11338
if (*stringptr == '\0') {
11341
nodemarkers = (int) strtol (stringptr, &stringptr, 0);
11345
if (inpoints < 3) {
11346
printf("Error: Input must have at least three input points.\n");
11349
if (mesh_dim != 2) {
11350
printf("Error: Triangle only works with two-dimensional meshes.\n");
11354
initializepointpool();
11356
/* Read the points. */
11357
for (i = 0; i < inpoints; i++) {
11358
pointloop = (point) poolalloc(&points);
11359
stringptr = readline(inputline, infile, infilename);
11361
firstnode = (int) strtol (stringptr, &stringptr, 0);
11362
if ((firstnode == 0) || (firstnode == 1)) {
11363
firstnumber = firstnode;
11366
stringptr = findfield(stringptr);
11367
if (*stringptr == '\0') {
11368
printf("Error: Point %d has no x coordinate.\n", firstnumber + i);
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);
11377
y = (REAL) strtod(stringptr, &stringptr);
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;
11386
pointloop[j] = (REAL) strtod(stringptr, &stringptr);
11390
/* Read a point marker. */
11391
stringptr = findfield(stringptr);
11392
if (*stringptr == '\0') {
11393
setpointmark(pointloop, 0);
11395
currentmarker = (int) strtol (stringptr, &stringptr, 0);
11396
setpointmark(pointloop, currentmarker);
11399
/* If no markers are specified in the file, they default to zero. */
11400
setpointmark(pointloop, 0);
11402
/* Determine the smallest and largest x and y coordinates. */
11407
xmin = (x < xmin) ? x : xmin;
11408
xmax = (x > xmax) ? x : xmax;
11409
ymin = (y < ymin) ? y : ymin;
11410
ymax = (y > ymax) ? y : ymax;
11413
if (readnodefile) {
11417
/* Nonexistent x value used as a flag to mark circle events in sweepline */
11418
/* Delaunay algorithm. */
11419
xminextreme = 10 * xmin - 9 * xmax;
11422
#endif /* not TRILIBRARY */
11424
/*****************************************************************************/
11426
/* transfernodes() Read the points from memory. */
11428
/*****************************************************************************/
11432
void transfernodes(
11434
REAL *pointattriblist,
11435
int *pointmarkerlist,
11436
int numberofpoints,
11437
int numberofpointattribs)
11445
inpoints = numberofpoints;
11447
nextras = numberofpointattribs;
11449
if (inpoints < 3) {
11450
printf("Error: Input must have at least three input points.\n");
11454
initializepointpool();
11456
/* Read the points. */
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++];
11468
if (pointmarkerlist != (int *) NULL) {
11469
/* Read a point marker. */
11470
setpointmark(pointloop, pointmarkerlist[i]);
11472
/* If no markers are specified, they default to zero. */
11473
setpointmark(pointloop, 0);
11477
/* Determine the smallest and largest x and y coordinates. */
11482
xmin = (x < xmin) ? x : xmin;
11483
xmax = (x > xmax) ? x : xmax;
11484
ymin = (y < ymin) ? y : ymin;
11485
ymax = (y > ymax) ? y : ymax;
11489
/* Nonexistent x value used as a flag to mark circle events in sweepline */
11490
/* Delaunay algorithm. */
11491
xminextreme = 10 * xmin - 9 * xmax;
11494
#endif /* TRILIBRARY */
11496
/*****************************************************************************/
11498
/* readholes() Read the holes, and possibly regional attributes and area */
11499
/* constraints, from a .poly file. */
11501
/*****************************************************************************/
11505
void readholes(polyfile, polyfilename, hlist, holes, rlist, regions)
11507
char *polyfilename;
11515
char inputline[INPUTLINESIZE];
11520
/* Read the holes. */
11521
stringptr = readline(inputline, polyfile, polyfilename);
11522
*holes = (int) strtol (stringptr, &stringptr, 0);
11524
holelist = (REAL *) malloc(2 * *holes * sizeof(REAL));
11526
if (holelist == (REAL *) NULL) {
11527
printf("Error: Out of memory.\n");
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));
11538
holelist[i] = (REAL) strtod(stringptr, &stringptr);
11540
stringptr = findfield(stringptr);
11541
if (*stringptr == '\0') {
11542
printf("Error: Hole %d has no y coordinate.\n",
11543
firstnumber + (i >> 1));
11546
holelist[i + 1] = (REAL) strtod(stringptr, &stringptr);
11550
*hlist = (REAL *) NULL;
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");
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",
11574
regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11576
stringptr = findfield(stringptr);
11577
if (*stringptr == '\0') {
11578
printf("Error: Region %d has no y coordinate.\n",
11582
regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11584
stringptr = findfield(stringptr);
11585
if (*stringptr == '\0') {
11587
"Error: Region %d has no region attribute or area constraint.\n",
11591
regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11593
stringptr = findfield(stringptr);
11594
if (*stringptr == '\0') {
11595
regionlist[index] = regionlist[index - 1];
11597
regionlist[index] = (REAL) strtod(stringptr, &stringptr);
11603
/* Set `*regions' to zero to avoid an accidental free() later. */
11605
*rlist = (REAL *) NULL;
11607
#endif /* not CDT_ONLY */
11612
#endif /* not TRILIBRARY */
11614
/*****************************************************************************/
11616
/* finishfile() Write the command line to the output file so the user */
11617
/* can remember how the file was generated. Close the file. */
11619
/*****************************************************************************/
11623
void finishfile(outfile, argc, argv)
11630
fprintf(outfile, "# Generated by");
11631
for (i = 0; i < argc; i++) {
11632
fprintf(outfile, " ");
11633
fputs(argv[i], outfile);
11635
fprintf(outfile, "\n");
11639
#endif /* not TRILIBRARY */
11641
/*****************************************************************************/
11643
/* writenodes() Number the points and write them to a .node file. */
11645
/* To save memory, the point numbers are written over the shell markers */
11646
/* after the points are written to a file. */
11648
/*****************************************************************************/
11654
REAL **pointattriblist,
11655
int **pointmarkerlist)
11657
#else /* not TRILIBRARY */
11660
char *nodefilename,
11664
#endif /* not TRILIBRARY */
11673
#else /* not TRILIBRARY */
11675
#endif /* not TRILIBRARY */
11682
printf("Writing points.\n");
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");
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");
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");
11708
plist = *pointlist;
11709
palist = *pointattriblist;
11710
pmlist = *pointmarkerlist;
11713
#else /* not TRILIBRARY */
11715
printf("Writing %s.\n", nodefilename);
11717
outfile = fopen(nodefilename, "w");
11718
if (outfile == (FILE *) NULL) {
11719
printf(" Error: Cannot create file %s.\n", nodefilename);
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,
11726
#endif /* not TRILIBRARY */
11728
traversalinit(&points);
11729
pointloop = pointtraverse();
11730
pointnumber = firstnumber;
11731
while (pointloop != (point) NULL) {
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];
11741
/* Copy the boundary marker. */
11742
pmlist[pointnumber - firstnumber] = pointmark(pointloop);
11744
#else /* not TRILIBRARY */
11745
/* Point number, x and y coordinates. */
11746
fprintf(outfile, "%4d %.17g %.17g", pointnumber, pointloop[0],
11748
for (i = 0; i < nextras; i++) {
11749
/* Write an attribute. */
11750
fprintf(outfile, " %.17g", pointloop[i + 2]);
11753
fprintf(outfile, "\n");
11755
/* Write the boundary marker. */
11756
fprintf(outfile, " %d\n", pointmark(pointloop));
11758
#endif /* not TRILIBRARY */
11760
setpointmark(pointloop, pointnumber);
11761
pointloop = pointtraverse();
11766
finishfile(outfile, argc, argv);
11767
#endif /* not TRILIBRARY */
11770
/*****************************************************************************/
11772
/* numbernodes() Number the points. */
11774
/* Each point is assigned a marker equal to its number. */
11776
/* Used when writenodes() is not called because no .node file is written. */
11778
/*****************************************************************************/
11785
traversalinit(&points);
11786
pointloop = pointtraverse();
11787
pointnumber = firstnumber;
11788
while (pointloop != (point) NULL) {
11789
setpointmark(pointloop, pointnumber);
11790
pointloop = pointtraverse();
11795
/*****************************************************************************/
11797
/* writeelements() Write the triangles to an .ele file. */
11799
/*****************************************************************************/
11803
void writeelements(
11804
int **trianglelist,
11805
REAL **triangleattriblist)
11807
#else /* not TRILIBRARY */
11809
void writeelements(
11814
#endif /* not TRILIBRARY */
11822
#else /* not TRILIBRARY */
11824
#endif /* not TRILIBRARY */
11825
struct triedge triangleloop;
11827
point mid1, mid2, mid3;
11833
printf("Writing triangles.\n");
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");
11844
/* Allocate memory for output triangle attributes if necessary. */
11845
if ((eextras > 0) && (*triangleattriblist == (REAL *) NULL)) {
11846
*triangleattriblist = (REAL *) malloc(triangles.items * eextras *
11848
if (*triangleattriblist == (REAL *) NULL) {
11849
printf("Error: Out of memory.\n");
11853
tlist = *trianglelist;
11854
talist = *triangleattriblist;
11857
#else /* not TRILIBRARY */
11859
printf("Writing %s.\n", elefilename);
11861
outfile = fopen(elefilename, "w");
11862
if (outfile == (FILE *) NULL) {
11863
printf(" Error: Cannot create file %s.\n", elefilename);
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 */
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);
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 */
11890
mid1 = (point) triangleloop.tri[highorderindex + 1];
11891
mid2 = (point) triangleloop.tri[highorderindex + 2];
11892
mid3 = (point) triangleloop.tri[highorderindex];
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 */
11909
for (i = 0; i < eextras; i++) {
11910
talist[attribindex++] = elemattribute(triangleloop, i);
11912
#else /* not TRILIBRARY */
11913
for (i = 0; i < eextras; i++) {
11914
fprintf(outfile, " %.17g", elemattribute(triangleloop, i));
11916
fprintf(outfile, "\n");
11917
#endif /* not TRILIBRARY */
11919
triangleloop.tri = triangletraverse();
11924
finishfile(outfile, argc, argv);
11925
#endif /* not TRILIBRARY */
11928
/*****************************************************************************/
11930
/* writepoly() Write the segments and holes to a .poly file. */
11932
/*****************************************************************************/
11938
int **segmentmarkerlist)
11940
#else /* not TRILIBRARY */
11943
char *polyfilename,
11951
#endif /* not TRILIBRARY */
11958
#else /* not TRILIBRARY */
11961
#endif /* not TRILIBRARY */
11962
struct edge shelleloop;
11963
point endpoint1, endpoint2;
11968
printf("Writing segments.\n");
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");
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");
11986
slist = *segmentlist;
11987
smlist = *segmentmarkerlist;
11989
#else /* not TRILIBRARY */
11991
printf("Writing %s.\n", polyfilename);
11993
outfile = fopen(polyfilename, "w");
11994
if (outfile == (FILE *) NULL) {
11995
printf(" Error: Cannot create file %s.\n", polyfilename);
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 */
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);
12014
/* Copy indices of the segment's two endpoints. */
12015
slist[index++] = pointmark(endpoint1);
12016
slist[index++] = pointmark(endpoint2);
12018
/* Copy the boundary marker. */
12019
smlist[shellenumber - firstnumber] = mark(shelleloop);
12021
#else /* not TRILIBRARY */
12022
/* Segment number, indices of its two endpoints, and possibly a marker. */
12024
fprintf(outfile, "%4d %4d %4d\n", shellenumber,
12025
pointmark(endpoint1), pointmark(endpoint2));
12027
fprintf(outfile, "%4d %4d %4d %4d\n", shellenumber,
12028
pointmark(endpoint1), pointmark(endpoint2), mark(shelleloop));
12030
#endif /* not TRILIBRARY */
12032
shelleloop.sh = shelletraverse();
12038
fprintf(outfile, "%d\n", holes);
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]);
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]);
12055
#endif /* not CDT_ONLY */
12057
finishfile(outfile, argc, argv);
12058
#endif /* not TRILIBRARY */
12061
/*****************************************************************************/
12063
/* writeedges() Write the edges to a .edge file. */
12065
/*****************************************************************************/
12071
int **edgemarkerlist)
12073
#else /* not TRILIBRARY */
12076
char *edgefilename,
12080
#endif /* not TRILIBRARY */
12087
#else /* not TRILIBRARY */
12089
#endif /* not TRILIBRARY */
12090
struct triedge triangleloop, trisym;
12091
struct edge checkmark;
12094
triangle ptr; /* Temporary variable used by sym(). */
12095
shelle sptr; /* Temporary variable used by tspivot(). */
12099
printf("Writing edges.\n");
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");
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");
12118
emlist = *edgemarkerlist;
12120
#else /* not TRILIBRARY */
12122
printf("Writing %s.\n", edgefilename);
12124
outfile = fopen(edgefilename, "w");
12125
if (outfile == (FILE *) NULL) {
12126
printf(" Error: Cannot create file %s.\n", edgefilename);
12129
/* Number of edges, number of boundary markers (zero or one). */
12130
fprintf(outfile, "%ld %d\n", edges, 1 - nobound);
12131
#endif /* not TRILIBRARY */
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);
12150
elist[index++] = pointmark(p1);
12151
elist[index++] = pointmark(p2);
12152
#endif /* 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 */
12160
/* Edge number, indices of two endpoints, and a boundary marker. */
12161
/* If there's no shell edge, the boundary marker is zero. */
12163
tspivot(triangleloop, checkmark);
12164
if (checkmark.sh == dummysh) {
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 */
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 */
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 */
12191
triangleloop.tri = triangletraverse();
12195
finishfile(outfile, argc, argv);
12196
#endif /* not TRILIBRARY */
12199
/*****************************************************************************/
12201
/* writevoronoi() Write the Voronoi diagram to a .v.node and .v.edge */
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 */
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. */
12213
/*****************************************************************************/
12219
REAL **vpointattriblist,
12220
int **vpointmarkerlist,
12222
int **vedgemarkerlist,
12225
#else /* not TRILIBRARY */
12228
char *vnodefilename,
12229
char *vedgefilename,
12233
#endif /* not TRILIBRARY */
12243
#else /* not TRILIBRARY */
12245
#endif /* not TRILIBRARY */
12246
struct triedge triangleloop, trisym;
12247
point torg, tdest, tapex;
12248
REAL circumcenter[2];
12250
int vnodenumber, vedgenumber;
12253
triangle ptr; /* Temporary variable used by sym(). */
12257
printf("Writing Voronoi vertices.\n");
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");
12267
/* Allocate memory for Voronoi vertex attributes if necessary. */
12268
if (*vpointattriblist == (REAL *) NULL) {
12269
*vpointattriblist = (REAL *) malloc(triangles.items * nextras *
12271
if (*vpointattriblist == (REAL *) NULL) {
12272
printf("Error: Out of memory.\n");
12276
*vpointmarkerlist = (int *) NULL;
12277
plist = *vpointlist;
12278
palist = *vpointattriblist;
12281
#else /* not TRILIBRARY */
12283
printf("Writing %s.\n", vnodefilename);
12285
outfile = fopen(vnodefilename, "w");
12286
if (outfile == (FILE *) NULL) {
12287
printf(" Error: Cannot create file %s.\n", vnodefilename);
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 */
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);
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]);
12313
#else /* not TRILIBRARY */
12314
/* Voronoi vertex number, x and y coordinates. */
12315
fprintf(outfile, "%4d %.17g %.17g", vnodenumber, circumcenter[0],
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]));
12322
fprintf(outfile, "\n");
12323
#endif /* not TRILIBRARY */
12325
* (int *) (triangleloop.tri + 6) = vnodenumber;
12326
triangleloop.tri = triangletraverse();
12331
finishfile(outfile, argc, argv);
12332
#endif /* not TRILIBRARY */
12336
printf("Writing Voronoi edges.\n");
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");
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");
12355
elist = *vedgelist;
12356
normlist = *vnormlist;
12358
#else /* not TRILIBRARY */
12360
printf("Writing %s.\n", vedgefilename);
12362
outfile = fopen(vedgefilename, "w");
12363
if (outfile == (FILE *) NULL) {
12364
printf(" Error: Cannot create file %s.\n", vedgefilename);
12367
/* Number of edges, zero boundary markers. */
12368
fprintf(outfile, "%ld %d\n", edges, 0);
12369
#endif /* not TRILIBRARY */
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);
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 */
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. */
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 */
12419
triangleloop.tri = triangletraverse();
12423
finishfile(outfile, argc, argv);
12424
#endif /* not TRILIBRARY */
12429
void writeneighbors(
12430
int **neighborlist)
12432
#else /* not TRILIBRARY */
12434
void writeneighbors(
12435
char *neighborfilename,
12439
#endif /* not TRILIBRARY */
12445
#else /* not TRILIBRARY */
12447
#endif /* not TRILIBRARY */
12448
struct triedge triangleloop, trisym;
12450
int neighbor1, neighbor2, neighbor3;
12451
triangle ptr; /* Temporary variable used by sym(). */
12455
printf("Writing neighbors.\n");
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");
12465
nlist = *neighborlist;
12467
#else /* not TRILIBRARY */
12469
printf("Writing %s.\n", neighborfilename);
12471
outfile = fopen(neighborfilename, "w");
12472
if (outfile == (FILE *) NULL) {
12473
printf(" Error: Cannot create file %s.\n", neighborfilename);
12476
/* Number of triangles, three edges per triangle. */
12477
fprintf(outfile, "%ld %d\n", triangles.items, 3);
12478
#endif /* not TRILIBRARY */
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();
12489
* (int *) (dummytri + 6) = -1;
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);
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 */
12514
triangleloop.tri = triangletraverse();
12519
finishfile(outfile, argc, argv);
12520
#endif /* TRILIBRARY */
12523
/*****************************************************************************/
12525
/* writeoff() Write the triangulation to an .off file. */
12527
/* OFF stands for the Object File Format, a format used by the Geometry */
12528
/* Center's Geomview package. */
12530
/*****************************************************************************/
12534
void writeoff(offfilename, argc, argv)
12540
struct triedge triangleloop;
12545
printf("Writing %s.\n", offfilename);
12547
outfile = fopen(offfilename, "w");
12548
if (outfile == (FILE *) NULL) {
12549
printf(" Error: Cannot create file %s.\n", offfilename);
12552
/* Number of points, triangles, and edges. */
12553
fprintf(outfile, "OFF\n%ld %ld %ld\n", points.items, triangles.items,
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();
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();
12579
finishfile(outfile, argc, argv);
12582
#endif /* not TRILIBRARY */
12586
/********* File I/O routines end here *********/
12588
/*****************************************************************************/
12590
/* quality_statistics() Print statistics about the quality of the mesh. */
12592
/*****************************************************************************/
12594
void quality_statistics()
12596
struct triedge triangleloop;
12598
REAL cossquaretable[8];
12599
REAL ratiotable[16];
12601
REAL edgelength[3];
12605
REAL shortest, longest;
12607
REAL smallestarea, biggestarea;
12608
REAL triminaltitude2;
12612
REAL smallestangle, biggestangle;
12613
REAL radconst, degconst;
12614
int angletable[18];
12615
int aspecttable[16];
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];
12628
for (i = 0; i < 18; i++) {
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;
12645
minaltitude = xmax - xmin + ymax - ymin;
12646
minaltitude = minaltitude * minaltitude;
12647
shortest = minaltitude;
12649
smallestarea = minaltitude;
12652
smallestangle = 0.0;
12653
biggestangle = 2.0;
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]);
12665
for (i = 0; i < 3; 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];
12674
if (edgelength[i] > longest) {
12675
longest = edgelength[i];
12677
if (edgelength[i] < shortest) {
12678
shortest = edgelength[i];
12682
triarea = counterclockwise(p[0], p[1], p[2]);
12683
if (triarea < smallestarea) {
12684
smallestarea = triarea;
12686
if (triarea > biggestarea) {
12687
biggestarea = triarea;
12689
triminaltitude2 = triarea * triarea / trilongest2;
12690
if (triminaltitude2 < minaltitude) {
12691
minaltitude = triminaltitude2;
12693
triaspect2 = trilongest2 / triminaltitude2;
12694
if (triaspect2 > worstaspect) {
12695
worstaspect = triaspect2;
12698
while ((triaspect2 > ratiotable[aspectindex] * ratiotable[aspectindex])
12699
&& (aspectindex < 15)) {
12702
aspecttable[aspectindex]++;
12704
for (i = 0; i < 3; i++) {
12707
dotproduct = dx[j] * dx[k] + dy[j] * dy[k];
12708
cossquare = dotproduct * dotproduct / (edgelength[j] * edgelength[k]);
12710
for (ii = 7; ii >= 0; ii--) {
12711
if (cossquare > cossquaretable[ii]) {
12715
if (dotproduct <= 0.0) {
12716
angletable[tendegree]++;
12717
if (cossquare > smallestangle) {
12718
smallestangle = cossquare;
12720
if (acutebiggest && (cossquare < biggestangle)) {
12721
biggestangle = cossquare;
12724
angletable[17 - tendegree]++;
12725
if (acutebiggest || (cossquare > biggestangle)) {
12726
biggestangle = cossquare;
12731
triangleloop.tri = triangletraverse();
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;
12743
smallestangle = degconst * acos(sqrt(smallestangle));
12745
if (biggestangle >= 1.0) {
12746
biggestangle = 180.0;
12748
if (acutebiggest) {
12749
biggestangle = degconst * acos(sqrt(biggestangle));
12751
biggestangle = 180.0 - degconst * acos(sqrt(biggestangle));
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],
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]);
12770
printf(" %6.6g - %-6.6g : %8d | %6.6g - : %8d\n",
12771
ratiotable[6], ratiotable[7], aspecttable[7], ratiotable[14],
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]);
12786
/*****************************************************************************/
12788
/* statistics() Print all sorts of cool facts. */
12790
/*****************************************************************************/
12794
printf("\nStatistics:\n\n");
12795
printf(" Input points: %d\n", inpoints);
12797
printf(" Input triangles: %d\n", inelements);
12800
printf(" Input segments: %d\n", insegments);
12802
printf(" Input holes: %d\n", holes);
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);
12813
printf(" Mesh convex hull edges: %ld\n\n", hullsize);
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);
12823
if (viri.maxitems > 0) {
12824
printf(" Maximum number of viri: %ld\n", viri.maxitems);
12826
if (badsegments.maxitems > 0) {
12827
printf(" Maximum number of encroached segments: %ld\n",
12828
badsegments.maxitems);
12830
if (badtriangles.maxitems > 0) {
12831
printf(" Maximum number of bad triangles: %ld\n",
12832
badtriangles.maxitems);
12834
if (splaynodes.maxitems > 0) {
12835
printf(" Maximum number of splay tree nodes: %ld\n",
12836
splaynodes.maxitems);
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);
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",
12854
if (circumcentercount > 0) {
12855
printf(" Number of circumcenter computations: %ld\n",
12856
circumcentercount);
12858
if (circletopcount > 0) {
12859
printf(" Number of circle top computations: %ld\n",
12866
/*****************************************************************************/
12868
/* main() or triangulate() Gosh, do everything. */
12870
/* The sequence is roughly as follows. Many of these steps can be skipped, */
12871
/* depending on the command line switches. */
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 */
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). */
12889
/*****************************************************************************/
12895
struct triangulateio *in,
12896
struct triangulateio *out,
12897
struct triangulateio *vorout)
12899
#else /* not TRILIBRARY */
12905
#endif /* not TRILIBRARY */
12908
REAL *holearray; /* Array of holes. */
12909
REAL *regionarray; /* Array of regional attributes and area constraints. */
12912
#endif /* not TRILIBRARY */
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 */
12921
gettimeofday(&tv0, &tz);
12922
#endif /* NO_TIMER */
12926
parsecommandline(1, &triswitches);
12927
#else /* not TRILIBRARY */
12928
parsecommandline(argc, argv);
12929
#endif /* not 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 */
12940
gettimeofday(&tv1, &tz);
12942
#endif /* NO_TIMER */
12945
hullsize = delaunay(); /* Triangulate the points. */
12946
#else /* not CDT_ONLY */
12948
/* Read and reconstruct a mesh. */
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,
12958
#endif /* not TRILIBRARY */
12960
hullsize = delaunay(); /* Triangulate the points. */
12962
#endif /* not CDT_ONLY */
12966
gettimeofday(&tv2, &tz);
12968
printf("Mesh reconstruction");
12970
printf("Delaunay");
12972
printf(" milliseconds: %ld\n", 1000l * (tv2.tv_sec - tv1.tv_sec)
12973
+ (tv2.tv_usec - tv1.tv_usec) / 1000l);
12975
#endif /* NO_TIMER */
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;
12984
checksegments = 1; /* Segments will be introduced next. */
12986
/* Insert PSLG segments and/or convex hull segments. */
12988
insegments = formskeleton(in->segmentlist, in->segmentmarkerlist,
12989
in->numberofsegments);
12990
#else /* not TRILIBRARY */
12991
insegments = formskeleton(polyfile, inpolyfilename);
12992
#endif /* not TRILIBRARY */
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);
13005
#endif /* NO_TIMER */
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
®ionarray, ®ions);
13016
#endif /* not TRILIBRARY */
13018
/* Carve out holes and concavities. */
13019
carveholes(holearray, holes, regionarray, regions);
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. */
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);
13037
#endif /* NO_TIMER */
13041
enforcequality(); /* Enforce angle and area constraints. */
13043
#endif /* not CDT_ONLY */
13047
gettimeofday(&tv5, &tz);
13050
printf("Quality milliseconds: %ld\n",
13051
1000l * (tv5.tv_sec - tv4.tv_sec)
13052
+ (tv5.tv_usec - tv4.tv_usec) / 1000l);
13054
#endif /* not CDT_ONLY */
13056
#endif /* NO_TIMER */
13058
/* Compute the number of edges. */
13059
edges = (3l * triangles.items + hullsize) / 2l;
13062
highorder(); /* Promote elements to higher polynomial order. */
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;
13076
out->numberofsegments = shelles.items;
13078
out->numberofsegments = hullsize;
13080
if (vorout != (struct triangulateio *) NULL) {
13081
vorout->numberofpoints = triangles.items;
13082
vorout->numberofpointattributes = nextras;
13083
vorout->numberofedges = edges;
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)) {
13091
printf("NOT writing points.\n");
13092
#else /* not TRILIBRARY */
13093
printf("NOT writing a .node file.\n");
13094
#endif /* not TRILIBRARY */
13096
numbernodes(); /* We must remember to number the points. */
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 */
13105
if (noelewritten) {
13108
printf("NOT writing triangles.\n");
13109
#else /* not TRILIBRARY */
13110
printf("NOT writing an .ele file.\n");
13111
#endif /* not TRILIBRARY */
13115
writeelements(&out->trianglelist, &out->triangleattributelist);
13116
#else /* not TRILIBRARY */
13117
writeelements(outelefilename, argc, argv);
13118
#endif /* not TRILIBRARY */
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) {
13127
printf("NOT writing segments.\n");
13128
#else /* not TRILIBRARY */
13129
printf("NOT writing a .poly file.\n");
13130
#endif /* not TRILIBRARY */
13134
writepoly(&out->segmentlist, &out->segmentmarkerlist);
13135
out->numberofholes = holes;
13136
out->numberofregions = regions;
13138
out->holelist = in->holelist;
13139
out->regionlist = in->regionlist;
13141
out->holelist = (REAL *) NULL;
13142
out->regionlist = (REAL *) NULL;
13144
#else /* not TRILIBRARY */
13145
writepoly(outpolyfilename, holearray, holes, regionarray, regions,
13147
#endif /* not TRILIBRARY */
13155
#endif /* not CDT_ONLY */
13160
writeoff(offfilename, argc, argv);
13162
#endif /* not TRILIBRARY */
13165
writeedges(&out->edgelist, &out->edgemarkerlist);
13166
#else /* not TRILIBRARY */
13167
writeedges(edgefilename, argc, argv);
13168
#endif /* not 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 */
13181
writeneighbors(&out->neighborlist);
13182
#else /* not TRILIBRARY */
13183
writeneighbors(neighborfilename, argc, argv);
13184
#endif /* not TRILIBRARY */
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 */
13206
#endif /* not REDUCED */
13211
#endif /* not TRILIBRARY */
13214
//parsecommandline(