1 /*****************************************************************************/
3 /* 888888888 ,o, / 888 */
4 /* 888 88o88o " o8888o 88o8888o o88888o 888 o88888o */
5 /* 888 888 888 88b 888 888 888 888 888 d888 88b */
6 /* 888 888 888 o88^o888 888 888 "88888" 888 8888oo888 */
7 /* 888 888 888 C888 888 888 888 / 888 q888 */
8 /* 888 888 888 "88o^888 888 888 Cb 888 "88oooo" */
11 /* A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator. */
18 /* Jonathan Richard Shewchuk */
19 /* School of Computer Science */
20 /* Carnegie Mellon University */
21 /* 5000 Forbes Avenue */
22 /* Pittsburgh, Pennsylvania 15213-3891 */
25 /* This program may be freely redistributed under the condition that the */
26 /* copyright notices (including this entire header and the copyright */
27 /* notice printed when the `-h' switch is selected) are not removed, and */
28 /* no compensation is received. Private, research, and institutional */
29 /* use is free. You may distribute modified versions of this code UNDER */
30 /* THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE */
31 /* SAME FILE REMAIN UNDER COPYRIGHT OF THE ORIGINAL AUTHOR, BOTH SOURCE */
32 /* AND OBJECT CODE ARE MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR */
33 /* NOTICE IS GIVEN OF THE MODIFICATIONS. Distribution of this code as */
34 /* part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT */
35 /* WITH THE AUTHOR. (If you are not directly supplying this code to a */
36 /* customer, and you are instead telling them how they can obtain it for */
37 /* free, then you are not required to make any arrangement with me.) */
39 /* Hypertext instructions for Triangle are available on the Web at */
41 /* http://www.cs.cmu.edu/~quake/triangle.html */
43 /* Some of the references listed below are marked [*]. These are available */
44 /* for downloading from the Web page */
46 /* http://www.cs.cmu.edu/~quake/triangle.research.html */
48 /* A paper discussing some aspects of Triangle is available. See Jonathan */
49 /* Richard Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator */
50 /* and Delaunay Triangulator," First Workshop on Applied Computational */
51 /* Geometry, ACM, May 1996. [*] */
53 /* Triangle was created as part of the Archimedes project in the School of */
54 /* Computer Science at Carnegie Mellon University. Archimedes is a */
55 /* system for compiling parallel finite element solvers. For further */
56 /* information, see Anja Feldmann, Omar Ghattas, John R. Gilbert, Gary L. */
57 /* Miller, David R. O'Hallaron, Eric J. Schwabe, Jonathan R. Shewchuk, */
58 /* and Shang-Hua Teng, "Automated Parallel Solution of Unstructured PDE */
59 /* Problems." To appear in Communications of the ACM, we hope. */
61 /* The quality mesh generation algorithm is due to Jim Ruppert, "A */
62 /* Delaunay Refinement Algorithm for Quality 2-Dimensional Mesh */
63 /* Generation," Journal of Algorithms 18(3):548-585, May 1995. [*] */
65 /* My implementation of the divide-and-conquer and incremental Delaunay */
66 /* triangulation algorithms follows closely the presentation of Guibas */
67 /* and Stolfi, even though I use a triangle-based data structure instead */
68 /* of their quad-edge data structure. (In fact, I originally implemented */
69 /* Triangle using the quad-edge data structure, but switching to a */
70 /* triangle-based data structure sped Triangle by a factor of two.) The */
71 /* mesh manipulation primitives and the two aforementioned Delaunay */
72 /* triangulation algorithms are described by Leonidas J. Guibas and Jorge */
73 /* Stolfi, "Primitives for the Manipulation of General Subdivisions and */
74 /* the Computation of Voronoi Diagrams," ACM Transactions on Graphics */
75 /* 4(2):74-123, April 1985. */
77 /* Their O(n log n) divide-and-conquer algorithm is adapted from Der-Tsai */
78 /* Lee and Bruce J. Schachter, "Two Algorithms for Constructing the */
79 /* Delaunay Triangulation," International Journal of Computer and */
80 /* Information Science 9(3):219-242, 1980. The idea to improve the */
81 /* divide-and-conquer algorithm by alternating between vertical and */
82 /* horizontal cuts was introduced by Rex A. Dwyer, "A Faster Divide-and- */
83 /* Conquer Algorithm for Constructing Delaunay Triangulations," */
84 /* Algorithmica 2(2):137-151, 1987. */
86 /* The incremental insertion algorithm was first proposed by C. L. Lawson, */
87 /* "Software for C1 Surface Interpolation," in Mathematical Software III, */
88 /* John R. Rice, editor, Academic Press, New York, pp. 161-194, 1977. */
89 /* For point location, I use the algorithm of Ernst P. Mucke, Isaac */
90 /* Saias, and Binhai Zhu, "Fast Randomized Point Location Without */
91 /* Preprocessing in Two- and Three-dimensional Delaunay Triangulations," */
92 /* Proceedings of the Twelfth Annual Symposium on Computational Geometry, */
93 /* ACM, May 1996. [*] If I were to randomize the order of point */
94 /* insertion (I currently don't bother), their result combined with the */
95 /* result of Leonidas J. Guibas, Donald E. Knuth, and Micha Sharir, */
96 /* "Randomized Incremental Construction of Delaunay and Voronoi */
97 /* Diagrams," Algorithmica 7(4):381-413, 1992, would yield an expected */
98 /* O(n^{4/3}) bound on running time. */
100 /* The O(n log n) sweepline Delaunay triangulation algorithm is taken from */
101 /* Steven Fortune, "A Sweepline Algorithm for Voronoi Diagrams", */
102 /* Algorithmica 2(2):153-174, 1987. A random sample of edges on the */
103 /* boundary of the triangulation are maintained in a splay tree for the */
104 /* purpose of point location. Splay trees are described by Daniel */
105 /* Dominic Sleator and Robert Endre Tarjan, "Self-Adjusting Binary Search */
106 /* Trees," Journal of the ACM 32(3):652-686, July 1985. */
108 /* The algorithms for exact computation of the signs of determinants are */
109 /* described in Jonathan Richard Shewchuk, "Adaptive Precision Floating- */
110 /* Point Arithmetic and Fast Robust Geometric Predicates," Technical */
111 /* Report CMU-CS-96-140, School of Computer Science, Carnegie Mellon */
112 /* University, Pittsburgh, Pennsylvania, May 1996. [*] (Submitted to */
113 /* Discrete & Computational Geometry.) An abbreviated version appears as */
114 /* Jonathan Richard Shewchuk, "Robust Adaptive Floating-Point Geometric */
115 /* Predicates," Proceedings of the Twelfth Annual Symposium on Computa- */
116 /* tional Geometry, ACM, May 1996. [*] Many of the ideas for my exact */
117 /* arithmetic routines originate with Douglas M. Priest, "Algorithms for */
118 /* Arbitrary Precision Floating Point Arithmetic," Tenth Symposium on */
119 /* Computer Arithmetic, 132-143, IEEE Computer Society Press, 1991. [*] */
120 /* Many of the ideas for the correct evaluation of the signs of */
121 /* determinants are taken from Steven Fortune and Christopher J. Van Wyk, */
122 /* "Efficient Exact Arithmetic for Computational Geometry," Proceedings */
123 /* of the Ninth Annual Symposium on Computational Geometry, ACM, */
124 /* pp. 163-172, May 1993, and from Steven Fortune, "Numerical Stability */
125 /* of Algorithms for 2D Delaunay Triangulations," International Journal */
126 /* of Computational Geometry & Applications 5(1-2):193-213, March-June */
129 /* For definitions of and results involving Delaunay triangulations, */
130 /* constrained and conforming versions thereof, and other aspects of */
131 /* triangular mesh generation, see the excellent survey by Marshall Bern */
132 /* and David Eppstein, "Mesh Generation and Optimal Triangulation," in */
133 /* Computing and Euclidean Geometry, Ding-Zhu Du and Frank Hwang, */
134 /* editors, World Scientific, Singapore, pp. 23-90, 1992. */
136 /* The time for incrementally adding PSLG (planar straight line graph) */
137 /* segments to create a constrained Delaunay triangulation is probably */
138 /* O(n^2) per segment in the worst case and O(n) per edge in the common */
139 /* case, where n is the number of triangles that intersect the segment */
140 /* before it is inserted. This doesn't count point location, which can */
141 /* be much more expensive. (This note does not apply to conforming */
142 /* Delaunay triangulations, for which a different method is used to */
143 /* insert segments.) */
145 /* The time for adding segments to a conforming Delaunay triangulation is */
146 /* not clear, but does not depend upon n alone. In some cases, very */
147 /* small features (like a point lying next to a segment) can cause a */
148 /* single segment to be split an arbitrary number of times. Of course, */
149 /* floating-point precision is a practical barrier to how much this can */
152 /* The time for deleting a point from a Delaunay triangulation is O(n^2) in */
153 /* the worst case and O(n) in the common case, where n is the degree of */
154 /* the point being deleted. I could improve this to expected O(n) time */
155 /* by "inserting" the neighboring vertices in random order, but n is */
156 /* usually quite small, so it's not worth the bother. (The O(n) time */
157 /* for random insertion follows from L. Paul Chew, "Building Voronoi */
158 /* Diagrams for Convex Polygons in Linear Expected Time," Technical */
159 /* Report PCS-TR90-147, Department of Mathematics and Computer Science, */
160 /* Dartmouth College, 1990. */
162 /* Ruppert's Delaunay refinement algorithm typically generates triangles */
163 /* at a linear rate (constant time per triangle) after the initial */
164 /* triangulation is formed. There may be pathological cases where more */
165 /* time is required, but these never arise in practice. */
167 /* The segment intersection formulae are straightforward. If you want to */
168 /* see them derived, see Franklin Antonio. "Faster Line Segment */
169 /* Intersection." In Graphics Gems III (David Kirk, editor), pp. 199- */
170 /* 202. Academic Press, Boston, 1992. */
172 /* If you make any improvements to this code, please please please let me */
173 /* know, so that I may obtain the improvements. Even if you don't change */
174 /* the code, I'd still love to hear what it's being used for. */
176 /* Disclaimer: Neither I nor Carnegie Mellon warrant this code in any way */
177 /* whatsoever. This code is provided "as-is". Use at your own risk. */
179 /*****************************************************************************/
181 /* For single precision (which will save some memory and reduce paging), */
182 /* define the symbol SINGLE by using the -DSINGLE compiler switch or by */
183 /* writing "#define SINGLE" below. */
185 /* For double precision (which will allow you to refine meshes to a smaller */
186 /* edge length), leave SINGLE undefined. */
188 /* Double precision uses more memory, but improves the resolution of the */
189 /* meshes you can generate with Triangle. It also reduces the likelihood */
190 /* of a floating exception due to overflow. Finally, it is much faster */
191 /* than single precision on 64-bit architectures like the DEC Alpha. I */
192 /* recommend double precision unless you want to generate a mesh for which */
193 /* you do not have enough memory. */
199 #else /* not SINGLE */
201 #endif /* not SINGLE */
203 /* If yours is not a Unix system, define the NO_TIMER compiler switch to */
204 /* remove the Unix-specific timing code. */
206 /* #define NO_TIMER */
208 /* To insert lots of self-checks for internal errors, define the SELF_CHECK */
209 /* symbol. This will slow down the program significantly. It is best to */
210 /* define the symbol using the -DSELF_CHECK compiler switch, but you could */
211 /* write "#define SELF_CHECK" below. If you are modifying this code, I */
212 /* recommend you turn self-checks on. */
214 /* #define SELF_CHECK */
216 /* To compile Triangle as a callable object library (triangle.o), define the */
217 /* TRILIBRARY symbol. Read the file triangle.h for details on how to call */
218 /* the procedure triangulate() that results. */
220 /* #define TRILIBRARY */
222 /* It is possible to generate a smaller version of Triangle using one or */
223 /* both of the following symbols. Define the REDUCED symbol to eliminate */
224 /* all features that are primarily of research interest; specifically, the */
225 /* -i, -F, -s, and -C switches. Define the CDT_ONLY symbol to eliminate */
226 /* all meshing algorithms above and beyond constrained Delaunay */
227 /* triangulation; specifically, the -r, -q, -a, -S, and -s switches. */
228 /* These reductions are most likely to be useful when generating an object */
229 /* library (triangle.o) by defining the TRILIBRARY symbol. */
231 /* #define REDUCED */
232 /* #define CDT_ONLY */
234 /* On some machines, the exact arithmetic routines might be defeated by the */
235 /* use of internal extended precision floating-point registers. Sometimes */
236 /* this problem can be fixed by defining certain values to be volatile, */
237 /* thus forcing them to be stored to memory and rounded off. This isn't */
238 /* a great solution, though, as it slows Triangle down. */
240 /* To try this out, write "#define INEXACT volatile" below. Normally, */
241 /* however, INEXACT should be defined to be nothing. ("#define INEXACT".) */
243 #define INEXACT /* Nothing */
244 /* #define INEXACT volatile */
246 /* Maximum number of characters in a file name (including the null). */
248 #define FILENAMESIZE 512
250 /* Maximum number of characters in a line read from a file (including the */
253 #define INPUTLINESIZE 512
255 /* For efficiency, a variety of data structures are allocated in bulk. The */
256 /* following constants determine how many of each structure is allocated */
259 #define TRIPERBLOCK 4092 /* Number of triangles allocated at once. */
260 #define SHELLEPERBLOCK 508 /* Number of shell edges allocated at once. */
261 #define POINTPERBLOCK 4092 /* Number of points allocated at once. */
262 #define VIRUSPERBLOCK 1020 /* Number of virus triangles allocated at once. */
263 /* Number of encroached segments allocated at once. */
264 #define BADSEGMENTPERBLOCK 252
265 /* Number of skinny triangles allocated at once. */
266 #define BADTRIPERBLOCK 4092
267 /* Number of splay tree nodes allocated at once. */
268 #define SPLAYNODEPERBLOCK 508
270 /* The point marker DEADPOINT is an arbitrary number chosen large enough to */
271 /* (hopefully) not conflict with user boundary markers. Make sure that it */
272 /* is small enough to fit into your machine's integer size. */
274 #define DEADPOINT -1073741824
276 /* The next line is used to outsmart some very stupid compilers. If your */
277 /* compiler is smarter, feel free to replace the "int" with "void". */
278 /* Not that it matters. */
282 /* Two constants for algorithms based on random sampling. Both constants */
283 /* have been chosen empirically to optimize their respective algorithms. */
285 /* Used for the point location scheme of Mucke, Saias, and Zhu, to decide */
286 /* how large a random sample of triangles to inspect. */
287 #define SAMPLEFACTOR 11
288 /* Used in Fortune's sweepline Delaunay algorithm to determine what fraction */
289 /* of boundary edges should be maintained in the splay tree for point */
290 /* location on the front. */
291 #define SAMPLERATE 10
293 /* A number that speaks for itself, every kissable digit. */
295 #define PI 3.141592653589793238462643383279502884197169399375105820974944592308
299 #define SQUAREROOTTWO 1.4142135623730950488016887242096980785696718753769480732
301 /* And here's one for those of you who are intimidated by math. */
303 #define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
309 #include <sys/time.h>
310 #endif /* NO_TIMER */
312 #include "triangle.h"
313 #endif /* TRILIBRARY */
315 /* The following obscenity seems to be necessary to ensure that this program */
316 /* will port to Dec Alphas running OSF/1, because their stdio.h file commits */
317 /* the unpardonable sin of including stdlib.h. Hence, malloc(), free(), and */
318 /* exit() may or may not already be defined at this point. I declare these */
319 /* functions explicitly because some non-ANSI C compilers lack stdlib.h. */
322 extern void *malloc();
325 extern double strtod();
326 extern long strtol();
327 #endif /* _STDLIB_H_ */
329 /* A few forward declarations. */
335 #endif /* not TRILIBRARY */
337 /* Labels that signify whether a record consists primarily of pointers or of */
338 /* floating-point words. Used to make decisions about data alignment. */
340 enum wordtype {POINTER, FLOATINGPOINT};
342 /* Labels that signify the result of point location. The result of a */
343 /* search indicates that the point falls in the interior of a triangle, on */
344 /* an edge, on a vertex, or outside the mesh. */
346 enum locateresult {INTRIANGLE, ONEDGE, ONVERTEX, OUTSIDE};
348 /* Labels that signify the result of site insertion. The result indicates */
349 /* that the point was inserted with complete success, was inserted but */
350 /* encroaches on a segment, was not inserted because it lies on a segment, */
351 /* or was not inserted because another point occupies the same location. */
353 enum insertsiteresult {SUCCESSFULPOINT, ENCROACHINGPOINT, VIOLATINGPOINT,
356 /* Labels that signify the result of direction finding. The result */
357 /* indicates that a segment connecting the two query points falls within */
358 /* the direction triangle, along the left edge of the direction triangle, */
359 /* or along the right edge of the direction triangle. */
361 enum finddirectionresult {WITHIN, LEFTCOLLINEAR, RIGHTCOLLINEAR};
363 /* Labels that signify the result of the circumcenter computation routine. */
364 /* The return value indicates which edge of the triangle is shortest. */
366 enum circumcenterresult {OPPOSITEORG, OPPOSITEDEST, OPPOSITEAPEX};
368 /*****************************************************************************/
370 /* The basic mesh data structures */
372 /* There are three: points, triangles, and shell edges (abbreviated */
373 /* `shelle'). These three data structures, linked by pointers, comprise */
374 /* the mesh. A point simply represents a point in space and its properties.*/
375 /* A triangle is a triangle. A shell edge is a special data structure used */
376 /* to represent impenetrable segments in the mesh (including the outer */
377 /* boundary, boundaries of holes, and internal boundaries separating two */
378 /* triangulated regions). Shell edges represent boundaries defined by the */
379 /* user that triangles may not lie across. */
381 /* A triangle consists of a list of three vertices, a list of three */
382 /* adjoining triangles, a list of three adjoining shell edges (when shell */
383 /* edges are used), an arbitrary number of optional user-defined floating- */
384 /* point attributes, and an optional area constraint. The latter is an */
385 /* upper bound on the permissible area of each triangle in a region, used */
386 /* for mesh refinement. */
388 /* For a triangle on a boundary of the mesh, some or all of the neighboring */
389 /* triangles may not be present. For a triangle in the interior of the */
390 /* mesh, often no neighboring shell edges are present. Such absent */
391 /* triangles and shell edges are never represented by NULL pointers; they */
392 /* are represented by two special records: `dummytri', the triangle that */
393 /* fills "outer space", and `dummysh', the omnipresent shell edge. */
394 /* `dummytri' and `dummysh' are used for several reasons; for instance, */
395 /* they can be dereferenced and their contents examined without causing the */
396 /* memory protection exception that would occur if NULL were dereferenced. */
398 /* However, it is important to understand that a triangle includes other */
399 /* information as well. The pointers to adjoining vertices, triangles, and */
400 /* shell edges are ordered in a way that indicates their geometric relation */
401 /* to each other. Furthermore, each of these pointers contains orientation */
402 /* information. Each pointer to an adjoining triangle indicates which face */
403 /* of that triangle is contacted. Similarly, each pointer to an adjoining */
404 /* shell edge indicates which side of that shell edge is contacted, and how */
405 /* the shell edge is oriented relative to the triangle. */
407 /* Shell edges are found abutting edges of triangles; either sandwiched */
408 /* between two triangles, or resting against one triangle on an exterior */
409 /* boundary or hole boundary. */
411 /* A shell edge consists of a list of two vertices, a list of two */
412 /* adjoining shell edges, and a list of two adjoining triangles. One of */
413 /* the two adjoining triangles may not be present (though there should */
414 /* always be one), and neighboring shell edges might not be present. */
415 /* Shell edges also store a user-defined integer "boundary marker". */
416 /* Typically, this integer is used to indicate what sort of boundary */
417 /* conditions are to be applied at that location in a finite element */
420 /* Like triangles, shell edges maintain information about the relative */
421 /* orientation of neighboring objects. */
423 /* Points are relatively simple. A point is a list of floating point */
424 /* numbers, starting with the x, and y coordinates, followed by an */
425 /* arbitrary number of optional user-defined floating-point attributes, */
426 /* followed by an integer boundary marker. During the segment insertion */
427 /* phase, there is also a pointer from each point to a triangle that may */
428 /* contain it. Each pointer is not always correct, but when one is, it */
429 /* speeds up segment insertion. These pointers are assigned values once */
430 /* at the beginning of the segment insertion phase, and are not used or */
431 /* updated at any other time. Edge swapping during segment insertion will */
432 /* render some of them incorrect. Hence, don't rely upon them for */
433 /* anything. For the most part, points do not have any information about */
434 /* what triangles or shell edges they are linked to. */
436 /*****************************************************************************/
438 /*****************************************************************************/
442 /* The oriented triangle (`triedge') and oriented shell edge (`edge') data */
443 /* structures defined below do not themselves store any part of the mesh. */
444 /* The mesh itself is made of `triangle's, `shelle's, and `point's. */
446 /* Oriented triangles and oriented shell edges will usually be referred to */
447 /* as "handles". A handle is essentially a pointer into the mesh; it */
448 /* allows you to "hold" one particular part of the mesh. Handles are used */
449 /* to specify the regions in which one is traversing and modifying the mesh.*/
450 /* A single `triangle' may be held by many handles, or none at all. (The */
451 /* latter case is not a memory leak, because the triangle is still */
452 /* connected to other triangles in the mesh.) */
454 /* A `triedge' is a handle that holds a triangle. It holds a specific side */
455 /* of the triangle. An `edge' is a handle that holds a shell edge. It */
456 /* holds either the left or right side of the edge. */
458 /* Navigation about the mesh is accomplished through a set of mesh */
459 /* manipulation primitives, further below. Many of these primitives take */
460 /* a handle and produce a new handle that holds the mesh near the first */
461 /* handle. Other primitives take two handles and glue the corresponding */
462 /* parts of the mesh together. The exact position of the handles is */
463 /* important. For instance, when two triangles are glued together by the */
464 /* bond() primitive, they are glued by the sides on which the handles lie. */
466 /* Because points have no information about which triangles they are */
467 /* attached to, I commonly represent a point by use of a handle whose */
468 /* origin is the point. A single handle can simultaneously represent a */
469 /* triangle, an edge, and a point. */
471 /*****************************************************************************/
473 /* The triangle data structure. Each triangle contains three pointers to */
474 /* adjoining triangles, plus three pointers to vertex points, plus three */
475 /* pointers to shell edges (defined below; these pointers are usually */
476 /* `dummysh'). It may or may not also contain user-defined attributes */
477 /* and/or a floating-point "area constraint". It may also contain extra */
478 /* pointers for nodes, when the user asks for high-order elements. */
479 /* Because the size and structure of a `triangle' is not decided until */
480 /* runtime, I haven't simply defined the type `triangle' to be a struct. */
482 typedef REAL **triangle; /* Really: typedef triangle *triangle */
484 /* An oriented triangle: includes a pointer to a triangle and orientation. */
485 /* The orientation denotes an edge of the triangle. Hence, there are */
486 /* three possible orientations. By convention, each edge is always */
487 /* directed to point counterclockwise about the corresponding triangle. */
491 int orient; /* Ranges from 0 to 2. */
494 /* The shell data structure. Each shell edge contains two pointers to */
495 /* adjoining shell edges, plus two pointers to vertex points, plus two */
496 /* pointers to adjoining triangles, plus one shell marker. */
498 typedef REAL **shelle; /* Really: typedef shelle *shelle */
500 /* An oriented shell edge: includes a pointer to a shell edge and an */
501 /* orientation. The orientation denotes a side of the edge. Hence, there */
502 /* are two possible orientations. By convention, the edge is always */
503 /* directed so that the "side" denoted is the right side of the edge. */
507 int shorient; /* Ranges from 0 to 1. */
510 /* The point data structure. Each point is actually an array of REALs. */
511 /* The number of REALs is unknown until runtime. An integer boundary */
512 /* marker, and sometimes a pointer to a triangle, is appended after the */
517 /* A queue used to store encroached segments. Each segment's vertices are */
518 /* stored so that one can check whether a segment is still the same. */
521 struct edge encsegment; /* An encroached segment. */
522 point segorg, segdest; /* The two vertices. */
523 struct badsegment *nextsegment; /* Pointer to next encroached segment. */
526 /* A queue used to store bad triangles. The key is the square of the cosine */
527 /* of the smallest angle of the triangle. Each triangle's vertices are */
528 /* stored so that one can check whether a triangle is still the same. */
531 struct triedge badfacetri; /* A bad triangle. */
532 REAL key; /* cos^2 of smallest (apical) angle. */
533 point faceorg, facedest, faceapex; /* The three vertices. */
534 struct badface *nextface; /* Pointer to next bad triangle. */
537 /* A node in a heap used to store events for the sweepline Delaunay */
538 /* algorithm. Nodes do not point directly to their parents or children in */
539 /* the heap. Instead, each node knows its position in the heap, and can */
540 /* look up its parent and children in a separate array. The `eventptr' */
541 /* points either to a `point' or to a triangle (in encoded format, so that */
542 /* an orientation is included). In the latter case, the origin of the */
543 /* oriented triangle is the apex of a "circle event" of the sweepline */
544 /* algorithm. To distinguish site events from circle events, all circle */
545 /* events are given an invalid (smaller than `xmin') x-coordinate `xkey'. */
548 REAL xkey, ykey; /* Coordinates of the event. */
549 VOID *eventptr; /* Can be a point or the location of a circle event. */
550 int heapposition; /* Marks this event's position in the heap. */
553 /* A node in the splay tree. Each node holds an oriented ghost triangle */
554 /* that represents a boundary edge of the growing triangulation. When a */
555 /* circle event covers two boundary edges with a triangle, so that they */
556 /* are no longer boundary edges, those edges are not immediately deleted */
557 /* from the tree; rather, they are lazily deleted when they are next */
558 /* encountered. (Since only a random sample of boundary edges are kept */
559 /* in the tree, lazy deletion is faster.) `keydest' is used to verify */
560 /* that a triangle is still the same as when it entered the splay tree; if */
561 /* it has been rotated (due to a circle event), it no longer represents a */
562 /* boundary edge and should be deleted. */
565 struct triedge keyedge; /* Lprev of an edge on the front. */
566 point keydest; /* Used to verify that splay node is still live. */
567 struct splaynode *lchild, *rchild; /* Children in splay tree. */
570 /* A type used to allocate memory. firstblock is the first block of items. */
571 /* nowblock is the block from which items are currently being allocated. */
572 /* nextitem points to the next slab of free memory for an item. */
573 /* deaditemstack is the head of a linked list (stack) of deallocated items */
574 /* that can be recycled. unallocateditems is the number of items that */
575 /* remain to be allocated from nowblock. */
577 /* Traversal is the process of walking through the entire list of items, and */
578 /* is separate from allocation. Note that a traversal will visit items on */
579 /* the "deaditemstack" stack as well as live items. pathblock points to */
580 /* the block currently being traversed. pathitem points to the next item */
581 /* to be traversed. pathitemsleft is the number of items that remain to */
582 /* be traversed in pathblock. */
584 /* itemwordtype is set to POINTER or FLOATINGPOINT, and is used to suggest */
585 /* what sort of word the record is primarily made up of. alignbytes */
586 /* determines how new records should be aligned in memory. itembytes and */
587 /* itemwords are the length of a record in bytes (after rounding up) and */
588 /* words. itemsperblock is the number of items allocated at once in a */
589 /* single block. items is the number of currently allocated items. */
590 /* maxitems is the maximum number of items that have been allocated at */
591 /* once; it is the current number of items plus the number of records kept */
592 /* on deaditemstack. */
595 VOID **firstblock, **nowblock;
600 enum wordtype itemwordtype;
602 int itembytes, itemwords;
604 long items, maxitems;
605 int unallocateditems;
609 /* Variables used to allocate memory for triangles, shell edges, points, */
610 /* viri (triangles being eaten), bad (encroached) segments, bad (skinny */
611 /* or too large) triangles, and splay tree nodes. */
613 struct memorypool triangles;
614 struct memorypool shelles;
615 struct memorypool points;
616 struct memorypool viri;
617 struct memorypool badsegments;
618 struct memorypool badtriangles;
619 struct memorypool splaynodes;
621 /* Variables that maintain the bad triangle queues. The tails are pointers */
622 /* to the pointers that have to be filled in to enqueue an item. */
624 struct badface *queuefront[64];
625 struct badface **queuetail[64];
627 REAL xmin, xmax, ymin, ymax; /* x and y bounds. */
628 REAL xminextreme; /* Nonexistent x value used as a flag in sweepline. */
629 int inpoints; /* Number of input points. */
630 int inelements; /* Number of input triangles. */
631 int insegments; /* Number of input segments. */
632 int holes; /* Number of input holes. */
633 int regions; /* Number of input regions. */
634 long edges; /* Number of output edges. */
635 int mesh_dim; /* Dimension (ought to be 2). */
636 int nextras; /* Number of attributes per point. */
637 int eextras; /* Number of attributes per triangle. */
638 long hullsize; /* Number of edges of convex hull. */
639 int triwords; /* Total words per triangle. */
640 int shwords; /* Total words per shell edge. */
641 int pointmarkindex; /* Index to find boundary marker of a point. */
642 int point2triindex; /* Index to find a triangle adjacent to a point. */
643 int highorderindex; /* Index to find extra nodes for high-order elements. */
644 int elemattribindex; /* Index to find attributes of a triangle. */
645 int areaboundindex; /* Index to find area bound of a triangle. */
646 int checksegments; /* Are there segments in the triangulation yet? */
647 int readnodefile; /* Has a .node file been read? */
648 long samples; /* Number of random samples for point location. */
649 unsigned long randomseed; /* Current random number seed. */
651 REAL splitter; /* Used to split REAL factors for exact multiplication. */
652 REAL epsilon; /* Floating-point machine epsilon. */
654 REAL ccwerrboundA, ccwerrboundB, ccwerrboundC;
655 REAL iccerrboundA, iccerrboundB, iccerrboundC;
657 long incirclecount; /* Number of incircle tests performed. */
658 long counterclockcount; /* Number of counterclockwise tests performed. */
659 long hyperbolacount; /* Number of right-of-hyperbola tests performed. */
660 long circumcentercount; /* Number of circumcenter calculations performed. */
661 long circletopcount; /* Number of circle top calculations performed. */
663 /* Switches for the triangulator. */
664 /* poly: -p switch. refine: -r switch. */
665 /* quality: -q switch. */
666 /* minangle: minimum angle bound, specified after -q switch. */
667 /* goodangle: cosine squared of minangle. */
668 /* vararea: -a switch without number. */
669 /* fixedarea: -a switch with number. */
670 /* maxarea: maximum area bound, specified after -a switch. */
671 /* regionattrib: -A switch. convex: -c switch. */
672 /* firstnumber: inverse of -z switch. All items are numbered starting */
673 /* from firstnumber. */
674 /* edgesout: -e switch. voronoi: -v switch. */
675 /* neighbors: -n switch. geomview: -g switch. */
676 /* nobound: -B switch. nopolywritten: -P switch. */
677 /* nonodewritten: -N switch. noelewritten: -E switch. */
678 /* noiterationnum: -I switch. noholes: -O switch. */
679 /* noexact: -X switch. */
680 /* order: element order, specified after -o switch. */
681 /* nobisect: count of how often -Y switch is selected. */
682 /* steiner: maximum number of Steiner points, specified after -S switch. */
683 /* steinerleft: number of Steiner points not yet used. */
684 /* incremental: -i switch. sweepline: -F switch. */
685 /* dwyer: inverse of -l switch. */
686 /* splitseg: -s switch. */
687 /* docheck: -C switch. */
688 /* quiet: -Q switch. verbose: count of how often -V switch is selected. */
689 /* useshelles: -p, -r, -q, or -c switch; determines whether shell edges */
690 /* are used at all. */
692 /* Read the instructions to find out the meaning of these switches. */
694 int poly, refine, quality, vararea, fixedarea, regionattrib, convex;
696 int edgesout, voronoi, neighbors, geomview;
697 int nobound, nopolywritten, nonodewritten, noelewritten, noiterationnum;
698 int noholes, noexact;
699 int incremental, sweepline, dwyer;
706 int steiner, steinerleft;
707 REAL minangle, goodangle;
710 /* Variables for file names. */
713 char innodefilename[FILENAMESIZE];
714 char inelefilename[FILENAMESIZE];
715 char inpolyfilename[FILENAMESIZE];
716 char areafilename[FILENAMESIZE];
717 char outnodefilename[FILENAMESIZE];
718 char outelefilename[FILENAMESIZE];
719 char outpolyfilename[FILENAMESIZE];
720 char edgefilename[FILENAMESIZE];
721 char vnodefilename[FILENAMESIZE];
722 char vedgefilename[FILENAMESIZE];
723 char neighborfilename[FILENAMESIZE];
724 char offfilename[FILENAMESIZE];
725 #endif /* not TRILIBRARY */
727 /* Triangular bounding box points. */
729 point infpoint1, infpoint2, infpoint3;
731 /* Pointer to the `triangle' that occupies all of "outer space". */
734 triangle *dummytribase; /* Keep base address so we can free() it later. */
736 /* Pointer to the omnipresent shell edge. Referenced by any triangle or */
737 /* shell edge that isn't really connected to a shell edge at that */
741 shelle *dummyshbase; /* Keep base address so we can free() it later. */
743 /* Pointer to a recently visited triangle. Improves point location if */
744 /* proximate points are inserted sequentially. */
746 struct triedge recenttri;
748 /*****************************************************************************/
750 /* Mesh manipulation primitives. Each triangle contains three pointers to */
751 /* other triangles, with orientations. Each pointer points not to the */
752 /* first byte of a triangle, but to one of the first three bytes of a */
753 /* triangle. It is necessary to extract both the triangle itself and the */
754 /* orientation. To save memory, I keep both pieces of information in one */
755 /* pointer. To make this possible, I assume that all triangles are aligned */
756 /* to four-byte boundaries. The `decode' routine below decodes a pointer, */
757 /* extracting an orientation (in the range 0 to 2) and a pointer to the */
758 /* beginning of a triangle. The `encode' routine compresses a pointer to a */
759 /* triangle and an orientation into a single pointer. My assumptions that */
760 /* triangles are four-byte-aligned and that the `unsigned long' type is */
761 /* long enough to hold a pointer are two of the few kludges in this program.*/
763 /* Shell edges are manipulated similarly. A pointer to a shell edge */
764 /* carries both an address and an orientation in the range 0 to 1. */
766 /* The other primitives take an oriented triangle or oriented shell edge, */
767 /* and return an oriented triangle or oriented shell edge or point; or they */
768 /* change the connections in the data structure. */
770 /*****************************************************************************/
772 /********* Mesh manipulation primitives begin here *********/
776 /* Fast lookup arrays to speed some of the mesh manipulation primitives. */
778 int plus1mod3[3] = {1, 2, 0};
779 int minus1mod3[3] = {2, 0, 1};
781 /********* Primitives for triangles *********/
785 /* decode() converts a pointer to an oriented triangle. The orientation is */
786 /* extracted from the two least significant bits of the pointer. */
788 #define decode(ptr, triedge) \
789 (triedge).orient = (int) ((unsigned long) (ptr) & (unsigned long) 3l); \
790 (triedge).tri = (triangle *) \
791 ((unsigned long) (ptr) ^ (unsigned long) (triedge).orient)
793 /* encode() compresses an oriented triangle into a single pointer. It */
794 /* relies on the assumption that all triangles are aligned to four-byte */
795 /* boundaries, so the two least significant bits of (triedge).tri are zero.*/
797 #define encode(triedge) \
798 (triangle) ((unsigned long) (triedge).tri | (unsigned long) (triedge).orient)
800 /* The following edge manipulation primitives are all described by Guibas */
801 /* and Stolfi. However, they use an edge-based data structure, whereas I */
802 /* am using a triangle-based data structure. */
804 /* sym() finds the abutting triangle, on the same edge. Note that the */
805 /* edge direction is necessarily reversed, because triangle/edge handles */
806 /* are always directed counterclockwise around the triangle. */
808 #define sym(triedge1, triedge2) \
809 ptr = (triedge1).tri[(triedge1).orient]; \
810 decode(ptr, triedge2);
812 #define symself(triedge) \
813 ptr = (triedge).tri[(triedge).orient]; \
814 decode(ptr, triedge);
816 /* lnext() finds the next edge (counterclockwise) of a triangle. */
818 #define lnext(triedge1, triedge2) \
819 (triedge2).tri = (triedge1).tri; \
820 (triedge2).orient = plus1mod3[(triedge1).orient]
822 #define lnextself(triedge) \
823 (triedge).orient = plus1mod3[(triedge).orient]
825 /* lprev() finds the previous edge (clockwise) of a triangle. */
827 #define lprev(triedge1, triedge2) \
828 (triedge2).tri = (triedge1).tri; \
829 (triedge2).orient = minus1mod3[(triedge1).orient]
831 #define lprevself(triedge) \
832 (triedge).orient = minus1mod3[(triedge).orient]
834 /* onext() spins counterclockwise around a point; that is, it finds the next */
835 /* edge with the same origin in the counterclockwise direction. This edge */
836 /* will be part of a different triangle. */
838 #define onext(triedge1, triedge2) \
839 lprev(triedge1, triedge2); \
842 #define onextself(triedge) \
843 lprevself(triedge); \
846 /* oprev() spins clockwise around a point; that is, it finds the next edge */
847 /* with the same origin in the clockwise direction. This edge will be */
848 /* part of a different triangle. */
850 #define oprev(triedge1, triedge2) \
851 sym(triedge1, triedge2); \
854 #define oprevself(triedge) \
858 /* dnext() spins counterclockwise around a point; that is, it finds the next */
859 /* edge with the same destination in the counterclockwise direction. This */
860 /* edge will be part of a different triangle. */
862 #define dnext(triedge1, triedge2) \
863 sym(triedge1, triedge2); \
866 #define dnextself(triedge) \
870 /* dprev() spins clockwise around a point; that is, it finds the next edge */
871 /* with the same destination in the clockwise direction. This edge will */
872 /* be part of a different triangle. */
874 #define dprev(triedge1, triedge2) \
875 lnext(triedge1, triedge2); \
878 #define dprevself(triedge) \
879 lnextself(triedge); \
882 /* rnext() moves one edge counterclockwise about the adjacent triangle. */
883 /* (It's best understood by reading Guibas and Stolfi. It involves */
884 /* changing triangles twice.) */
886 #define rnext(triedge1, triedge2) \
887 sym(triedge1, triedge2); \
888 lnextself(triedge2); \
891 #define rnextself(triedge) \
893 lnextself(triedge); \
896 /* rnext() moves one edge clockwise about the adjacent triangle. */
897 /* (It's best understood by reading Guibas and Stolfi. It involves */
898 /* changing triangles twice.) */
900 #define rprev(triedge1, triedge2) \
901 sym(triedge1, triedge2); \
902 lprevself(triedge2); \
905 #define rprevself(triedge) \
907 lprevself(triedge); \
910 /* These primitives determine or set the origin, destination, or apex of a */
913 #define org(triedge, pointptr) \
914 pointptr = (point) (triedge).tri[plus1mod3[(triedge).orient] + 3]
916 #define dest(triedge, pointptr) \
917 pointptr = (point) (triedge).tri[minus1mod3[(triedge).orient] + 3]
919 #define apex(triedge, pointptr) \
920 pointptr = (point) (triedge).tri[(triedge).orient + 3]
922 #define setorg(triedge, pointptr) \
923 (triedge).tri[plus1mod3[(triedge).orient] + 3] = (triangle) pointptr
925 #define setdest(triedge, pointptr) \
926 (triedge).tri[minus1mod3[(triedge).orient] + 3] = (triangle) pointptr
928 #define setapex(triedge, pointptr) \
929 (triedge).tri[(triedge).orient + 3] = (triangle) pointptr
931 #define setvertices2null(triedge) \
932 (triedge).tri[3] = (triangle) NULL; \
933 (triedge).tri[4] = (triangle) NULL; \
934 (triedge).tri[5] = (triangle) NULL;
936 /* Bond two triangles together. */
938 #define bond(triedge1, triedge2) \
939 (triedge1).tri[(triedge1).orient] = encode(triedge2); \
940 (triedge2).tri[(triedge2).orient] = encode(triedge1)
942 /* Dissolve a bond (from one side). Note that the other triangle will still */
943 /* think it's connected to this triangle. Usually, however, the other */
944 /* triangle is being deleted entirely, or bonded to another triangle, so */
945 /* it doesn't matter. */
947 #define dissolve(triedge) \
948 (triedge).tri[(triedge).orient] = (triangle) dummytri
950 /* Copy a triangle/edge handle. */
952 #define triedgecopy(triedge1, triedge2) \
953 (triedge2).tri = (triedge1).tri; \
954 (triedge2).orient = (triedge1).orient
956 /* Test for equality of triangle/edge handles. */
958 #define triedgeequal(triedge1, triedge2) \
959 (((triedge1).tri == (triedge2).tri) && \
960 ((triedge1).orient == (triedge2).orient))
962 /* Primitives to infect or cure a triangle with the virus. These rely on */
963 /* the assumption that all shell edges are aligned to four-byte boundaries.*/
965 #define infect(triedge) \
966 (triedge).tri[6] = (triangle) \
967 ((unsigned long) (triedge).tri[6] | (unsigned long) 2l)
969 #define uninfect(triedge) \
970 (triedge).tri[6] = (triangle) \
971 ((unsigned long) (triedge).tri[6] & ~ (unsigned long) 2l)
973 /* Test a triangle for viral infection. */
975 #define infected(triedge) \
976 (((unsigned long) (triedge).tri[6] & (unsigned long) 2l) != 0)
978 /* Check or set a triangle's attributes. */
980 #define elemattribute(triedge, attnum) \
981 ((REAL *) (triedge).tri)[elemattribindex + (attnum)]
983 #define setelemattribute(triedge, attnum, value) \
984 ((REAL *) (triedge).tri)[elemattribindex + (attnum)] = value
986 /* Check or set a triangle's maximum area bound. */
988 #define areabound(triedge) ((REAL *) (triedge).tri)[areaboundindex]
990 #define setareabound(triedge, value) \
991 ((REAL *) (triedge).tri)[areaboundindex] = value
993 /********* Primitives for shell edges *********/
997 /* sdecode() converts a pointer to an oriented shell edge. The orientation */
998 /* is extracted from the least significant bit of the pointer. The two */
999 /* least significant bits (one for orientation, one for viral infection) */
1000 /* are masked out to produce the real pointer. */
1002 #define sdecode(sptr, edge) \
1003 (edge).shorient = (int) ((unsigned long) (sptr) & (unsigned long) 1l); \
1004 (edge).sh = (shelle *) \
1005 ((unsigned long) (sptr) & ~ (unsigned long) 3l)
1007 /* sencode() compresses an oriented shell edge into a single pointer. It */
1008 /* relies on the assumption that all shell edges are aligned to two-byte */
1009 /* boundaries, so the least significant bit of (edge).sh is zero. */
1011 #define sencode(edge) \
1012 (shelle) ((unsigned long) (edge).sh | (unsigned long) (edge).shorient)
1014 /* ssym() toggles the orientation of a shell edge. */
1016 #define ssym(edge1, edge2) \
1017 (edge2).sh = (edge1).sh; \
1018 (edge2).shorient = 1 - (edge1).shorient
1020 #define ssymself(edge) \
1021 (edge).shorient = 1 - (edge).shorient
1023 /* spivot() finds the other shell edge (from the same segment) that shares */
1024 /* the same origin. */
1026 #define spivot(edge1, edge2) \
1027 sptr = (edge1).sh[(edge1).shorient]; \
1028 sdecode(sptr, edge2)
1030 #define spivotself(edge) \
1031 sptr = (edge).sh[(edge).shorient]; \
1034 /* snext() finds the next shell edge (from the same segment) in sequence; */
1035 /* one whose origin is the input shell edge's destination. */
1037 #define snext(edge1, edge2) \
1038 sptr = (edge1).sh[1 - (edge1).shorient]; \
1039 sdecode(sptr, edge2)
1041 #define snextself(edge) \
1042 sptr = (edge).sh[1 - (edge).shorient]; \
1045 /* These primitives determine or set the origin or destination of a shell */
1048 #define sorg(edge, pointptr) \
1049 pointptr = (point) (edge).sh[2 + (edge).shorient]
1051 #define sdest(edge, pointptr) \
1052 pointptr = (point) (edge).sh[3 - (edge).shorient]
1054 #define setsorg(edge, pointptr) \
1055 (edge).sh[2 + (edge).shorient] = (shelle) pointptr
1057 #define setsdest(edge, pointptr) \
1058 (edge).sh[3 - (edge).shorient] = (shelle) pointptr
1060 /* These primitives read or set a shell marker. Shell markers are used to */
1061 /* hold user boundary information. */
1063 #define mark(edge) (* (int *) ((edge).sh + 6))
1065 #define setmark(edge, value) \
1066 * (int *) ((edge).sh + 6) = value
1068 /* Bond two shell edges together. */
1070 #define sbond(edge1, edge2) \
1071 (edge1).sh[(edge1).shorient] = sencode(edge2); \
1072 (edge2).sh[(edge2).shorient] = sencode(edge1)
1074 /* Dissolve a shell edge bond (from one side). Note that the other shell */
1075 /* edge will still think it's connected to this shell edge. */
1077 #define sdissolve(edge) \
1078 (edge).sh[(edge).shorient] = (shelle) dummysh
1080 /* Copy a shell edge. */
1082 #define shellecopy(edge1, edge2) \
1083 (edge2).sh = (edge1).sh; \
1084 (edge2).shorient = (edge1).shorient
1086 /* Test for equality of shell edges. */
1088 #define shelleequal(edge1, edge2) \
1089 (((edge1).sh == (edge2).sh) && \
1090 ((edge1).shorient == (edge2).shorient))
1092 /********* Primitives for interacting triangles and shell edges *********/
1096 /* tspivot() finds a shell edge abutting a triangle. */
1098 #define tspivot(triedge, edge) \
1099 sptr = (shelle) (triedge).tri[6 + (triedge).orient]; \
1102 /* stpivot() finds a triangle abutting a shell edge. It requires that the */
1103 /* variable `ptr' of type `triangle' be defined. */
1105 #define stpivot(edge, triedge) \
1106 ptr = (triangle) (edge).sh[4 + (edge).shorient]; \
1107 decode(ptr, triedge)
1109 /* Bond a triangle to a shell edge. */
1111 #define tsbond(triedge, edge) \
1112 (triedge).tri[6 + (triedge).orient] = (triangle) sencode(edge); \
1113 (edge).sh[4 + (edge).shorient] = (shelle) encode(triedge)
1115 /* Dissolve a bond (from the triangle side). */
1117 #define tsdissolve(triedge) \
1118 (triedge).tri[6 + (triedge).orient] = (triangle) dummysh
1120 /* Dissolve a bond (from the shell edge side). */
1122 #define stdissolve(edge) \
1123 (edge).sh[4 + (edge).shorient] = (shelle) dummytri
1125 /********* Primitives for points *********/
1129 #define pointmark(pt) ((int *) (pt))[pointmarkindex]
1131 #define setpointmark(pt, value) \
1132 ((int *) (pt))[pointmarkindex] = value
1134 #define point2tri(pt) ((triangle *) (pt))[point2triindex]
1136 #define setpoint2tri(pt, value) \
1137 ((triangle *) (pt))[point2triindex] = value
1141 /********* Mesh manipulation primitives end here *********/
1143 /********* User interaction routines begin here *********/
1147 /*****************************************************************************/
1149 /* syntax() Print list of command line switches. */
1151 /*****************************************************************************/
1159 printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n");
1160 #else /* not REDUCED */
1161 printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n");
1162 #endif /* not REDUCED */
1163 #else /* not CDT_ONLY */
1165 printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n");
1166 #else /* not REDUCED */
1167 printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n");
1168 #endif /* not REDUCED */
1169 #endif /* not CDT_ONLY */
1171 printf(" -p Triangulates a Planar Straight Line Graph (.poly file).\n");
1173 printf(" -r Refines a previously generated mesh.\n");
1175 " -q Quality mesh generation. A minimum angle may be specified.\n");
1176 printf(" -a Applies a maximum triangle area constraint.\n");
1177 #endif /* not CDT_ONLY */
1179 " -A Applies attributes to identify elements in certain regions.\n");
1180 printf(" -c Encloses the convex hull with segments.\n");
1181 printf(" -e Generates an edge list.\n");
1182 printf(" -v Generates a Voronoi diagram.\n");
1183 printf(" -n Generates a list of triangle neighbors.\n");
1184 printf(" -g Generates an .off file for Geomview.\n");
1185 printf(" -B Suppresses output of boundary information.\n");
1186 printf(" -P Suppresses output of .poly file.\n");
1187 printf(" -N Suppresses output of .node file.\n");
1188 printf(" -E Suppresses output of .ele file.\n");
1189 printf(" -I Suppresses mesh iteration numbers.\n");
1190 printf(" -O Ignores holes in .poly file.\n");
1191 printf(" -X Suppresses use of exact arithmetic.\n");
1192 printf(" -z Numbers all items starting from zero (rather than one).\n");
1193 printf(" -o2 Generates second-order subparametric elements.\n");
1195 printf(" -Y Suppresses boundary segment splitting.\n");
1196 printf(" -S Specifies maximum number of added Steiner points.\n");
1197 #endif /* not CDT_ONLY */
1199 printf(" -i Uses incremental method, rather than divide-and-conquer.\n");
1200 printf(" -F Uses Fortune's sweepline algorithm, rather than d-and-c.\n");
1201 #endif /* not REDUCED */
1202 printf(" -l Uses vertical cuts only, rather than alternating cuts.\n");
1206 " -s Force segments into mesh by splitting (instead of using CDT).\n");
1207 #endif /* not CDT_ONLY */
1208 printf(" -C Check consistency of final mesh.\n");
1209 #endif /* not REDUCED */
1210 printf(" -Q Quiet: No terminal output except errors.\n");
1211 printf(" -V Verbose: Detailed information on what I'm doing.\n");
1212 printf(" -h Help: Detailed instructions for Triangle.\n");
1216 #endif /* not TRILIBRARY */
1218 /*****************************************************************************/
1220 /* info() Print out complete instructions. */
1222 /*****************************************************************************/
1228 printf("Triangle\n");
1230 "A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\n");
1231 printf("Version 1.3\n\n");
1233 "Copyright 1996 Jonathan Richard Shewchuk (bugs/comments to jrs@cs.cmu.edu)\n"
1235 printf("School of Computer Science / Carnegie Mellon University\n");
1236 printf("5000 Forbes Avenue / Pittsburgh, Pennsylvania 15213-3891\n");
1238 "Created as part of the Archimedes project (tools for parallel FEM).\n");
1240 "Supported in part by NSF Grant CMS-9318163 and an NSERC 1967 Scholarship.\n");
1241 printf("There is no warranty whatsoever. Use at your own risk.\n");
1243 printf("This executable is compiled for single precision arithmetic.\n\n\n");
1244 #else /* not SINGLE */
1245 printf("This executable is compiled for double precision arithmetic.\n\n\n");
1246 #endif /* not SINGLE */
1248 "Triangle generates exact Delaunay triangulations, constrained Delaunay\n");
1250 "triangulations, and quality conforming Delaunay triangulations. The latter\n"
1253 "can be generated with no small angles, and are thus suitable for finite\n");
1255 "element analysis. If no command line switches are specified, your .node\n");
1257 "input file will be read, and the Delaunay triangulation will be returned in\n"
1259 printf(".node and .ele output files. The command syntax is:\n\n");
1262 printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n\n");
1263 #else /* not REDUCED */
1264 printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n\n");
1265 #endif /* not REDUCED */
1266 #else /* not CDT_ONLY */
1268 printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n\n");
1269 #else /* not REDUCED */
1270 printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n\n");
1271 #endif /* not REDUCED */
1272 #endif /* not CDT_ONLY */
1274 "Underscores indicate that numbers may optionally follow certain switches;\n");
1276 "do not leave any space between a switch and its numeric parameter.\n");
1278 "input_file must be a file with extension .node, or extension .poly if the\n");
1280 "-p switch is used. If -r is used, you must supply .node and .ele files,\n");
1282 "and possibly a .poly file and .area file as well. The formats of these\n");
1283 printf("files are described below.\n\n");
1284 printf("Command Line Switches:\n\n");
1286 " -p Reads a Planar Straight Line Graph (.poly file), which can specify\n"
1289 " points, segments, holes, and regional attributes and area\n");
1291 " constraints. Will generate a constrained Delaunay triangulation\n");
1293 " fitting the input; or, if -s, -q, or -a is used, a conforming\n");
1295 " Delaunay triangulation. If -p is not used, Triangle reads a .node\n"
1297 printf(" file by default.\n");
1299 " -r Refines a previously generated mesh. The mesh is read from a .node\n"
1302 " file and an .ele file. If -p is also used, a .poly file is read\n");
1304 " and used to constrain edges in the mesh. Further details on\n");
1305 printf(" refinement are given below.\n");
1307 " -q Quality mesh generation by Jim Ruppert's Delaunay refinement\n");
1309 " algorithm. Adds points to the mesh to ensure that no angles\n");
1311 " smaller than 20 degrees occur. An alternative minimum angle may be\n"
1314 " specified after the `q'. If the minimum angle is 20.7 degrees or\n");
1316 " smaller, the triangulation algorithm is theoretically guaranteed to\n"
1319 " terminate (assuming infinite precision arithmetic - Triangle may\n");
1321 " fail to terminate if you run out of precision). In practice, the\n");
1323 " algorithm often succeeds for minimum angles up to 33.8 degrees.\n");
1325 " For highly refined meshes, however, it may be necessary to reduce\n");
1327 " the minimum angle to well below 20 to avoid problems associated\n");
1329 " with insufficient floating-point precision. The specified angle\n");
1330 printf(" may include a decimal point.\n");
1332 " -a Imposes a maximum triangle area. If a number follows the `a', no\n");
1334 " triangle will be generated whose area is larger than that number.\n");
1336 " If no number is specified, an .area file (if -r is used) or .poly\n");
1338 " file (if -r is not used) specifies a number of maximum area\n");
1340 " constraints. An .area file contains a separate area constraint for\n"
1343 " each triangle, and is useful for refining a finite element mesh\n");
1345 " based on a posteriori error estimates. A .poly file can optionally\n"
1348 " contain an area constraint for each segment-bounded region, thereby\n"
1351 " enforcing triangle densities in a first triangulation. You can\n");
1353 " impose both a fixed area constraint and a varying area constraint\n");
1355 " by invoking the -a switch twice, once with and once without a\n");
1357 " number following. Each area specified may include a decimal point.\n"
1360 " -A Assigns an additional attribute to each triangle that identifies\n");
1362 " what segment-bounded region each triangle belongs to. Attributes\n");
1364 " are assigned to regions by the .poly file. If a region is not\n");
1366 " explicitly marked by the .poly file, triangles in that region are\n");
1368 " assigned an attribute of zero. The -A switch has an effect only\n");
1369 printf(" when the -p switch is used and the -r switch is not.\n");
1371 " -c Creates segments on the convex hull of the triangulation. If you\n");
1373 " are triangulating a point set, this switch causes a .poly file to\n");
1375 " be written, containing all edges in the convex hull. (By default,\n"
1378 " a .poly file is written only if a .poly file is read.) If you are\n"
1381 " triangulating a PSLG, this switch specifies that the interior of\n");
1383 " the convex hull of the PSLG should be triangulated. If you do not\n"
1386 " use this switch when triangulating a PSLG, it is assumed that you\n");
1388 " have identified the region to be triangulated by surrounding it\n");
1390 " with segments of the input PSLG. Beware: if you are not careful,\n"
1393 " this switch can cause the introduction of an extremely thin angle\n");
1395 " between a PSLG segment and a convex hull segment, which can cause\n");
1397 " overrefinement or failure if Triangle runs out of precision. If\n");
1399 " you are refining a mesh, the -c switch works differently; it\n");
1401 " generates the set of boundary edges of the mesh, rather than the\n");
1402 printf(" convex hull.\n");
1404 " -e Outputs (to an .edge file) a list of edges of the triangulation.\n");
1406 " -v Outputs the Voronoi diagram associated with the triangulation.\n");
1407 printf(" Does not attempt to detect degeneracies.\n");
1409 " -n Outputs (to a .neigh file) a list of triangles neighboring each\n");
1410 printf(" triangle.\n");
1412 " -g Outputs the mesh to an Object File Format (.off) file, suitable for\n"
1414 printf(" viewing with the Geometry Center's Geomview package.\n");
1416 " -B No boundary markers in the output .node, .poly, and .edge output\n");
1418 " files. See the detailed discussion of boundary markers below.\n");
1420 " -P No output .poly file. Saves disk space, but you lose the ability\n");
1422 " to impose segment constraints on later refinements of the mesh.\n");
1423 printf(" -N No output .node file.\n");
1424 printf(" -E No output .ele file.\n");
1426 " -I No iteration numbers. Suppresses the output of .node and .poly\n");
1428 " files, so your input files won't be overwritten. (If your input is\n"
1431 " a .poly file only, a .node file will be written.) Cannot be used\n");
1433 " with the -r switch, because that would overwrite your input .ele\n");
1435 " file. Shouldn't be used with the -s, -q, or -a switch if you are\n");
1437 " using a .node file for input, because no .node file will be\n");
1438 printf(" written, so there will be no record of any added points.\n");
1439 printf(" -O No holes. Ignores the holes in the .poly file.\n");
1441 " -X No exact arithmetic. Normally, Triangle uses exact floating-point\n"
1444 " arithmetic for certain tests if it thinks the inexact tests are not\n"
1447 " accurate enough. Exact arithmetic ensures the robustness of the\n");
1449 " triangulation algorithms, despite floating-point roundoff error.\n");
1451 " Disabling exact arithmetic with the -X switch will cause a small\n");
1453 " improvement in speed and create the possibility (albeit small) that\n"
1456 " Triangle will fail to produce a valid mesh. Not recommended.\n");
1458 " -z Numbers all items starting from zero (rather than one). Note that\n"
1461 " this switch is normally overrided by the value used to number the\n");
1463 " first point of the input .node or .poly file. However, this switch\n"
1465 printf(" is useful when calling Triangle from another program.\n");
1467 " -o2 Generates second-order subparametric elements with six nodes each.\n"
1470 " -Y No new points on the boundary. This switch is useful when the mesh\n"
1473 " boundary must be preserved so that it conforms to some adjacent\n");
1475 " mesh. Be forewarned that you will probably sacrifice some of the\n");
1477 " quality of the mesh; Triangle will try, but the resulting mesh may\n"
1480 " contain triangles of poor aspect ratio. Works well if all the\n");
1482 " boundary points are closely spaced. Specify this switch twice\n");
1484 " (`-YY') to prevent all segment splitting, including internal\n");
1485 printf(" boundaries.\n");
1487 " -S Specifies the maximum number of Steiner points (points that are not\n"
1490 " in the input, but are added to meet the constraints of minimum\n");
1492 " angle and maximum area). The default is to allow an unlimited\n");
1494 " number. If you specify this switch with no number after it,\n");
1496 " the limit is set to zero. Triangle always adds points at segment\n");
1498 " intersections, even if it needs to use more points than the limit\n");
1500 " you set. When Triangle inserts segments by splitting (-s), it\n");
1502 " always adds enough points to ensure that all the segments appear in\n"
1505 " the triangulation, again ignoring the limit. Be forewarned that\n");
1507 " the -S switch may result in a conforming triangulation that is not\n"
1510 " truly Delaunay, because Triangle may be forced to stop adding\n");
1512 " points when the mesh is in a state where a segment is non-Delaunay\n"
1515 " and needs to be split. If so, Triangle will print a warning.\n");
1517 " -i Uses an incremental rather than divide-and-conquer algorithm to\n");
1519 " form a Delaunay triangulation. Try it if the divide-and-conquer\n");
1520 printf(" algorithm fails.\n");
1522 " -F Uses Steven Fortune's sweepline algorithm to form a Delaunay\n");
1524 " triangulation. Warning: does not use exact arithmetic for all\n");
1525 printf(" calculations. An exact result is not guaranteed.\n");
1527 " -l Uses only vertical cuts in the divide-and-conquer algorithm. By\n");
1529 " default, Triangle uses alternating vertical and horizontal cuts,\n");
1531 " which usually improve the speed except with point sets that are\n");
1533 " small or short and wide. This switch is primarily of theoretical\n");
1534 printf(" interest.\n");
1536 " -s Specifies that segments should be forced into the triangulation by\n"
1539 " recursively splitting them at their midpoints, rather than by\n");
1541 " generating a constrained Delaunay triangulation. Segment splitting\n"
1544 " is true to Ruppert's original algorithm, but can create needlessly\n"
1546 printf(" small triangles near external small features.\n");
1548 " -C Check the consistency of the final mesh. Uses exact arithmetic for\n"
1551 " checking, even if the -X switch is used. Useful if you suspect\n");
1552 printf(" Triangle is buggy.\n");
1554 " -Q Quiet: Suppresses all explanation of what Triangle is doing, unless\n"
1556 printf(" an error occurs.\n");
1558 " -V Verbose: Gives detailed information about what Triangle is doing.\n");
1560 " Add more `V's for increasing amount of detail. `-V' gives\n");
1562 " information on algorithmic progress and more detailed statistics.\n");
1564 " `-VV' gives point-by-point details, and will print so much that\n");
1566 " Triangle will run much more slowly. `-VVV' gives information only\n"
1568 printf(" a debugger could love.\n");
1569 printf(" -h Help: Displays these instructions.\n");
1571 printf("Definitions:\n");
1574 " A Delaunay triangulation of a point set is a triangulation whose vertices\n"
1577 " are the point set, having the property that no point in the point set\n");
1579 " falls in the interior of the circumcircle (circle that passes through all\n"
1581 printf(" three vertices) of any triangle in the triangulation.\n\n");
1583 " A Voronoi diagram of a point set is a subdivision of the plane into\n");
1585 " polygonal regions (some of which may be infinite), where each region is\n");
1587 " the set of points in the plane that are closer to some input point than\n");
1589 " to any other input point. (The Voronoi diagram is the geometric dual of\n"
1591 printf(" the Delaunay triangulation.)\n\n");
1593 " A Planar Straight Line Graph (PSLG) is a collection of points and\n");
1595 " segments. Segments are simply edges, whose endpoints are points in the\n");
1597 " PSLG. The file format for PSLGs (.poly files) is described below.\n");
1600 " A constrained Delaunay triangulation of a PSLG is similar to a Delaunay\n");
1602 " triangulation, but each PSLG segment is present as a single edge in the\n");
1604 " triangulation. (A constrained Delaunay triangulation is not truly a\n");
1605 printf(" Delaunay triangulation.)\n\n");
1607 " A conforming Delaunay triangulation of a PSLG is a true Delaunay\n");
1609 " triangulation in which each PSLG segment may have been subdivided into\n");
1611 " several edges by the insertion of additional points. These inserted\n");
1613 " points are necessary to allow the segments to exist in the mesh while\n");
1614 printf(" maintaining the Delaunay property.\n\n");
1615 printf("File Formats:\n\n");
1617 " All files may contain comments prefixed by the character '#'. Points,\n");
1619 " triangles, edges, holes, and maximum area constraints must be numbered\n");
1621 " consecutively, starting from either 1 or 0. Whichever you choose, all\n");
1623 " input files must be consistent; if the nodes are numbered from 1, so must\n"
1626 " be all other objects. Triangle automatically detects your choice while\n");
1628 " reading the .node (or .poly) file. (When calling Triangle from another\n");
1630 " program, use the -z switch if you wish to number objects from zero.)\n");
1631 printf(" Examples of these file formats are given below.\n\n");
1632 printf(" .node files:\n");
1634 " First line: <# of points> <dimension (must be 2)> <# of attributes>\n");
1636 " <# of boundary markers (0 or 1)>\n"
1639 " Remaining lines: <point #> <x> <y> [attributes] [boundary marker]\n");
1642 " The attributes, which are typically floating-point values of physical\n");
1644 " quantities (such as mass or conductivity) associated with the nodes of\n"
1647 " a finite element mesh, are copied unchanged to the output mesh. If -s,\n"
1650 " -q, or -a is selected, each new Steiner point added to the mesh will\n");
1651 printf(" have attributes assigned to it by linear interpolation.\n\n");
1653 " If the fourth entry of the first line is `1', the last column of the\n");
1655 " remainder of the file is assumed to contain boundary markers. Boundary\n"
1658 " markers are used to identify boundary points and points resting on PSLG\n"
1661 " segments; a complete description appears in a section below. The .node\n"
1664 " file produced by Triangle will contain boundary markers in the last\n");
1665 printf(" column unless they are suppressed by the -B switch.\n\n");
1666 printf(" .ele files:\n");
1668 " First line: <# of triangles> <points per triangle> <# of attributes>\n");
1670 " Remaining lines: <triangle #> <point> <point> <point> ... [attributes]\n"
1674 " Points are indices into the corresponding .node file. The first three\n"
1677 " points are the corners, and are listed in counterclockwise order around\n"
1680 " each triangle. (The remaining points, if any, depend on the type of\n");
1682 " finite element used.) The attributes are just like those of .node\n");
1684 " files. Because there is no simple mapping from input to output\n");
1686 " triangles, an attempt is made to interpolate attributes, which may\n");
1688 " result in a good deal of diffusion of attributes among nearby triangles\n"
1691 " as the triangulation is refined. Diffusion does not occur across\n");
1693 " segments, so attributes used to identify segment-bounded regions remain\n"
1696 " intact. In output .ele files, all triangles have three points each\n");
1698 " unless the -o2 switch is used, in which case they have six, and the\n");
1700 " fourth, fifth, and sixth points lie on the midpoints of the edges\n");
1701 printf(" opposite the first, second, and third corners.\n\n");
1702 printf(" .poly files:\n");
1704 " First line: <# of points> <dimension (must be 2)> <# of attributes>\n");
1706 " <# of boundary markers (0 or 1)>\n"
1709 " Following lines: <point #> <x> <y> [attributes] [boundary marker]\n");
1710 printf(" One line: <# of segments> <# of boundary markers (0 or 1)>\n");
1712 " Following lines: <segment #> <endpoint> <endpoint> [boundary marker]\n");
1713 printf(" One line: <# of holes>\n");
1714 printf(" Following lines: <hole #> <x> <y>\n");
1716 " Optional line: <# of regional attributes and/or area constraints>\n");
1718 " Optional following lines: <constraint #> <x> <y> <attrib> <max area>\n");
1721 " A .poly file represents a PSLG, as well as some additional information.\n"
1724 " The first section lists all the points, and is identical to the format\n"
1727 " of .node files. <# of points> may be set to zero to indicate that the\n"
1730 " points are listed in a separate .node file; .poly files produced by\n");
1732 " Triangle always have this format. This has the advantage that a point\n"
1735 " set may easily be triangulated with or without segments. (The same\n");
1737 " effect can be achieved, albeit using more disk space, by making a copy\n"
1740 " of the .poly file with the extension .node; all sections of the file\n");
1741 printf(" but the first are ignored.)\n\n");
1743 " The second section lists the segments. Segments are edges whose\n");
1745 " presence in the triangulation is enforced. Each segment is specified\n");
1747 " by listing the indices of its two endpoints. This means that you must\n"
1750 " include its endpoints in the point list. If -s, -q, and -a are not\n");
1752 " selected, Triangle will produce a constrained Delaunay triangulation,\n");
1754 " in which each segment appears as a single edge in the triangulation.\n");
1756 " If -q or -a is selected, Triangle will produce a conforming Delaunay\n");
1758 " triangulation, in which segments may be subdivided into smaller edges.\n"
1760 printf(" Each segment, like each point, may have a boundary marker.\n\n");
1762 " The third section lists holes (and concavities, if -c is selected) in\n");
1764 " the triangulation. Holes are specified by identifying a point inside\n");
1766 " each hole. After the triangulation is formed, Triangle creates holes\n");
1768 " by eating triangles, spreading out from each hole point until its\n");
1770 " progress is blocked by PSLG segments; you must be careful to enclose\n");
1772 " each hole in segments, or your whole triangulation may be eaten away.\n");
1774 " If the two triangles abutting a segment are eaten, the segment itself\n");
1776 " is also eaten. Do not place a hole directly on a segment; if you do,\n");
1777 printf(" Triangle will choose one side of the segment arbitrarily.\n\n");
1779 " The optional fourth section lists regional attributes (to be assigned\n");
1781 " to all triangles in a region) and regional constraints on the maximum\n");
1783 " triangle area. Triangle will read this section only if the -A switch\n");
1785 " is used or the -a switch is used without a number following it, and the\n"
1788 " -r switch is not used. Regional attributes and area constraints are\n");
1790 " propagated in the same manner as holes; you specify a point for each\n");
1792 " attribute and/or constraint, and the attribute and/or constraint will\n");
1794 " affect the whole region (bounded by segments) containing the point. If\n"
1797 " two values are written on a line after the x and y coordinate, the\n");
1799 " former is assumed to be a regional attribute (but will only be applied\n"
1802 " if the -A switch is selected), and the latter is assumed to be a\n");
1804 " regional area constraint (but will only be applied if the -a switch is\n"
1807 " selected). You may also specify just one value after the coordinates,\n"
1810 " which can serve as both an attribute and an area constraint, depending\n"
1813 " on the choice of switches. If you are using the -A and -a switches\n");
1815 " simultaneously and wish to assign an attribute to some region without\n");
1816 printf(" imposing an area constraint, use a negative maximum area.\n\n");
1818 " When a triangulation is created from a .poly file, you must either\n");
1820 " enclose the entire region to be triangulated in PSLG segments, or\n");
1822 " use the -c switch, which encloses the convex hull of the input point\n");
1824 " set. If you do not use the -c switch, Triangle will eat all triangles\n"
1827 " on the outer boundary that are not protected by segments; if you are\n");
1829 " not careful, your whole triangulation may be eaten away. If you do\n");
1831 " use the -c switch, you can still produce concavities by appropriate\n");
1832 printf(" placement of holes just inside the convex hull.\n\n");
1834 " An ideal PSLG has no intersecting segments, nor any points that lie\n");
1836 " upon segments (except, of course, the endpoints of each segment.) You\n"
1839 " aren't required to make your .poly files ideal, but you should be aware\n"
1842 " of what can go wrong. Segment intersections are relatively safe -\n");
1844 " Triangle will calculate the intersection points for you and add them to\n"
1847 " the triangulation - as long as your machine's floating-point precision\n"
1850 " doesn't become a problem. You are tempting the fates if you have three\n"
1853 " segments that cross at the same location, and expect Triangle to figure\n"
1856 " out where the intersection point is. Thanks to floating-point roundoff\n"
1859 " error, Triangle will probably decide that the three segments intersect\n"
1862 " at three different points, and you will find a minuscule triangle in\n");
1864 " your output - unless Triangle tries to refine the tiny triangle, uses\n");
1866 " up the last bit of machine precision, and fails to terminate at all.\n");
1868 " You're better off putting the intersection point in the input files,\n");
1870 " and manually breaking up each segment into two. Similarly, if you\n");
1872 " place a point at the middle of a segment, and hope that Triangle will\n");
1874 " break up the segment at that point, you might get lucky. On the other\n"
1877 " hand, Triangle might decide that the point doesn't lie precisely on the\n"
1880 " line, and you'll have a needle-sharp triangle in your output - or a lot\n"
1882 printf(" of tiny triangles if you're generating a quality mesh.\n\n");
1884 " When Triangle reads a .poly file, it also writes a .poly file, which\n");
1886 " includes all edges that are part of input segments. If the -c switch\n");
1888 " is used, the output .poly file will also include all of the edges on\n");
1890 " the convex hull. Hence, the output .poly file is useful for finding\n");
1892 " edges associated with input segments and setting boundary conditions in\n"
1895 " finite element simulations. More importantly, you will need it if you\n"
1898 " plan to refine the output mesh, and don't want segments to be missing\n");
1899 printf(" in later triangulations.\n\n");
1900 printf(" .area files:\n");
1901 printf(" First line: <# of triangles>\n");
1902 printf(" Following lines: <triangle #> <maximum area>\n\n");
1904 " An .area file associates with each triangle a maximum area that is used\n"
1907 " for mesh refinement. As with other file formats, every triangle must\n");
1909 " be represented, and they must be numbered consecutively. A triangle\n");
1911 " may be left unconstrained by assigning it a negative maximum area.\n");
1913 printf(" .edge files:\n");
1914 printf(" First line: <# of edges> <# of boundary markers (0 or 1)>\n");
1916 " Following lines: <edge #> <endpoint> <endpoint> [boundary marker]\n");
1919 " Endpoints are indices into the corresponding .node file. Triangle can\n"
1922 " produce .edge files (use the -e switch), but cannot read them. The\n");
1924 " optional column of boundary markers is suppressed by the -B switch.\n");
1927 " In Voronoi diagrams, one also finds a special kind of edge that is an\n");
1929 " infinite ray with only one endpoint. For these edges, a different\n");
1930 printf(" format is used:\n\n");
1931 printf(" <edge #> <endpoint> -1 <direction x> <direction y>\n\n");
1933 " The `direction' is a floating-point vector that indicates the direction\n"
1935 printf(" of the infinite ray.\n\n");
1936 printf(" .neigh files:\n");
1938 " First line: <# of triangles> <# of neighbors per triangle (always 3)>\n"
1941 " Following lines: <triangle #> <neighbor> <neighbor> <neighbor>\n");
1944 " Neighbors are indices into the corresponding .ele file. An index of -1\n"
1947 " indicates a mesh boundary, and therefore no neighbor. Triangle can\n");
1949 " produce .neigh files (use the -n switch), but cannot read them.\n");
1952 " The first neighbor of triangle i is opposite the first corner of\n");
1953 printf(" triangle i, and so on.\n\n");
1954 printf("Boundary Markers:\n\n");
1956 " Boundary markers are tags used mainly to identify which output points and\n"
1959 " edges are associated with which PSLG segment, and to identify which\n");
1961 " points and edges occur on a boundary of the triangulation. A common use\n"
1964 " is to determine where boundary conditions should be applied to a finite\n");
1966 " element mesh. You can prevent boundary markers from being written into\n");
1967 printf(" files produced by Triangle by using the -B switch.\n\n");
1969 " The boundary marker associated with each segment in an output .poly file\n"
1971 printf(" or edge in an output .edge file is chosen as follows:\n");
1973 " - If an output edge is part or all of a PSLG segment with a nonzero\n");
1975 " boundary marker, then the edge is assigned the same marker.\n");
1977 " - Otherwise, if the edge occurs on a boundary of the triangulation\n");
1979 " (including boundaries of holes), then the edge is assigned the marker\n"
1981 printf(" one (1).\n");
1982 printf(" - Otherwise, the edge is assigned the marker zero (0).\n");
1984 " The boundary marker associated with each point in an output .node file is\n"
1986 printf(" chosen as follows:\n");
1988 " - If a point is assigned a nonzero boundary marker in the input file,\n");
1990 " then it is assigned the same marker in the output .node file.\n");
1992 " - Otherwise, if the point lies on a PSLG segment (including the\n");
1994 " segment's endpoints) with a nonzero boundary marker, then the point\n");
1996 " is assigned the same marker. If the point lies on several such\n");
1997 printf(" segments, one of the markers is chosen arbitrarily.\n");
1999 " - Otherwise, if the point occurs on a boundary of the triangulation,\n");
2000 printf(" then the point is assigned the marker one (1).\n");
2001 printf(" - Otherwise, the point is assigned the marker zero (0).\n");
2004 " If you want Triangle to determine for you which points and edges are on\n");
2006 " the boundary, assign them the boundary marker zero (or use no markers at\n"
2009 " all) in your input files. Alternatively, you can mark some of them and\n");
2010 printf(" leave others marked zero, allowing Triangle to label them.\n\n");
2011 printf("Triangulation Iteration Numbers:\n\n");
2013 " Because Triangle can read and refine its own triangulations, input\n");
2015 " and output files have iteration numbers. For instance, Triangle might\n");
2017 " read the files mesh.3.node, mesh.3.ele, and mesh.3.poly, refine the\n");
2019 " triangulation, and output the files mesh.4.node, mesh.4.ele, and\n");
2020 printf(" mesh.4.poly. Files with no iteration number are treated as if\n");
2022 " their iteration number is zero; hence, Triangle might read the file\n");
2024 " points.node, triangulate it, and produce the files points.1.node and\n");
2025 printf(" points.1.ele.\n\n");
2027 " Iteration numbers allow you to create a sequence of successively finer\n");
2029 " meshes suitable for multigrid methods. They also allow you to produce a\n"
2032 " sequence of meshes using error estimate-driven mesh refinement.\n");
2035 " If you're not using refinement or quality meshing, and you don't like\n");
2037 " iteration numbers, use the -I switch to disable them. This switch will\n");
2039 " also disable output of .node and .poly files to prevent your input files\n"
2042 " from being overwritten. (If the input is a .poly file that contains its\n"
2044 printf(" own points, a .node file will be written.)\n\n");
2045 printf("Examples of How to Use Triangle:\n\n");
2047 " `triangle dots' will read points from dots.node, and write their Delaunay\n"
2050 " triangulation to dots.1.node and dots.1.ele. (dots.1.node will be\n");
2052 " identical to dots.node.) `triangle -I dots' writes the triangulation to\n"
2055 " dots.ele instead. (No additional .node file is needed, so none is\n");
2056 printf(" written.)\n\n");
2058 " `triangle -pe object.1' will read a PSLG from object.1.poly (and possibly\n"
2061 " object.1.node, if the points are omitted from object.1.poly) and write\n");
2062 printf(" their constrained Delaunay triangulation to object.2.node and\n");
2064 " object.2.ele. The segments will be copied to object.2.poly, and all\n");
2065 printf(" edges will be written to object.2.edge.\n\n");
2067 " `triangle -pq31.5a.1 object' will read a PSLG from object.poly (and\n");
2069 " possibly object.node), generate a mesh whose angles are all greater than\n"
2072 " 31.5 degrees and whose triangles all have area smaller than 0.1, and\n");
2074 " write the mesh to object.1.node and object.1.ele. Each segment may have\n"
2077 " been broken up into multiple edges; the resulting constrained edges are\n");
2078 printf(" written to object.1.poly.\n\n");
2080 " Here is a sample file `box.poly' describing a square with a square hole:\n"
2084 " # A box with eight points in 2D, no attributes, one boundary marker.\n");
2085 printf(" 8 2 0 1\n");
2086 printf(" # Outer box has these vertices:\n");
2087 printf(" 1 0 0 0\n");
2088 printf(" 2 0 3 0\n");
2089 printf(" 3 3 0 0\n");
2090 printf(" 4 3 3 33 # A special marker for this point.\n");
2091 printf(" # Inner square has these vertices:\n");
2092 printf(" 5 1 1 0\n");
2093 printf(" 6 1 2 0\n");
2094 printf(" 7 2 1 0\n");
2095 printf(" 8 2 2 0\n");
2096 printf(" # Five segments with boundary markers.\n");
2098 printf(" 1 1 2 5 # Left side of outer box.\n");
2099 printf(" 2 5 7 0 # Segments 2 through 5 enclose the hole.\n");
2100 printf(" 3 7 8 0\n");
2101 printf(" 4 8 6 10\n");
2102 printf(" 5 6 5 0\n");
2103 printf(" # One hole in the middle of the inner square.\n");
2105 printf(" 1 1.5 1.5\n\n");
2107 " Note that some segments are missing from the outer square, so one must\n");
2109 " use the `-c' switch. After `triangle -pqc box.poly', here is the output\n"
2112 " file `box.1.node', with twelve points. The last four points were added\n");
2114 " to meet the angle constraint. Points 1, 2, and 9 have markers from\n");
2116 " segment 1. Points 6 and 8 have markers from segment 4. All the other\n");
2118 " points but 4 have been marked to indicate that they lie on a boundary.\n");
2120 printf(" 12 2 0 1\n");
2121 printf(" 1 0 0 5\n");
2122 printf(" 2 0 3 5\n");
2123 printf(" 3 3 0 1\n");
2124 printf(" 4 3 3 33\n");
2125 printf(" 5 1 1 1\n");
2126 printf(" 6 1 2 10\n");
2127 printf(" 7 2 1 1\n");
2128 printf(" 8 2 2 10\n");
2129 printf(" 9 0 1.5 5\n");
2130 printf(" 10 1.5 0 1\n");
2131 printf(" 11 3 1.5 1\n");
2132 printf(" 12 1.5 3 1\n");
2133 printf(" # Generated by triangle -pqc box.poly\n\n");
2134 printf(" Here is the output file `box.1.ele', with twelve triangles.\n\n");
2135 printf(" 12 3 0\n");
2136 printf(" 1 5 6 9\n");
2137 printf(" 2 10 3 7\n");
2138 printf(" 3 6 8 12\n");
2139 printf(" 4 9 1 5\n");
2140 printf(" 5 6 2 9\n");
2141 printf(" 6 7 3 11\n");
2142 printf(" 7 11 4 8\n");
2143 printf(" 8 7 5 10\n");
2144 printf(" 9 12 2 6\n");
2145 printf(" 10 8 7 11\n");
2146 printf(" 11 5 1 10\n");
2147 printf(" 12 8 4 12\n");
2148 printf(" # Generated by triangle -pqc box.poly\n\n");
2150 " Here is the output file `box.1.poly'. Note that segments have been added\n"
2153 " to represent the convex hull, and some segments have been split by newly\n"
2156 " added points. Note also that <# of points> is set to zero to indicate\n");
2157 printf(" that the points should be read from the .node file.\n\n");
2158 printf(" 0 2 0 1\n");
2160 printf(" 1 1 9 5\n");
2161 printf(" 2 5 7 1\n");
2162 printf(" 3 8 7 1\n");
2163 printf(" 4 6 8 10\n");
2164 printf(" 5 5 6 1\n");
2165 printf(" 6 3 10 1\n");
2166 printf(" 7 4 11 1\n");
2167 printf(" 8 2 12 1\n");
2168 printf(" 9 9 2 5\n");
2169 printf(" 10 10 1 1\n");
2170 printf(" 11 11 3 1\n");
2171 printf(" 12 12 4 1\n");
2173 printf(" 1 1.5 1.5\n");
2174 printf(" # Generated by triangle -pqc box.poly\n\n");
2175 printf("Refinement and Area Constraints:\n\n");
2177 " The -r switch causes a mesh (.node and .ele files) to be read and\n");
2179 " refined. If the -p switch is also used, a .poly file is read and used to\n"
2182 " specify edges that are constrained and cannot be eliminated (although\n");
2184 " they can be divided into smaller edges) by the refinement process.\n");
2187 " When you refine a mesh, you generally want to impose tighter quality\n");
2189 " constraints. One way to accomplish this is to use -q with a larger\n");
2191 " angle, or -a followed by a smaller area than you used to generate the\n");
2193 " mesh you are refining. Another way to do this is to create an .area\n");
2195 " file, which specifies a maximum area for each triangle, and use the -a\n");
2197 " switch (without a number following). Each triangle's area constraint is\n"
2200 " applied to that triangle. Area constraints tend to diffuse as the mesh\n");
2202 " is refined, so if there are large variations in area constraint between\n");
2203 printf(" adjacent triangles, you may not get the results you want.\n\n");
2205 " If you are refining a mesh composed of linear (three-node) elements, the\n"
2208 " output mesh will contain all the nodes present in the input mesh, in the\n"
2211 " same order, with new nodes added at the end of the .node file. However,\n"
2214 " there is no guarantee that each output element is contained in a single\n");
2216 " input element. Often, output elements will overlap two input elements,\n");
2218 " and input edges are not present in the output mesh. Hence, a sequence of\n"
2221 " refined meshes will form a hierarchy of nodes, but not a hierarchy of\n");
2223 " elements. If you a refining a mesh of higher-order elements, the\n");
2225 " hierarchical property applies only to the nodes at the corners of an\n");
2226 printf(" element; other nodes may not be present in the refined mesh.\n\n");
2228 " It is important to understand that maximum area constraints in .poly\n");
2230 " files are handled differently from those in .area files. A maximum area\n"
2233 " in a .poly file applies to the whole (segment-bounded) region in which a\n"
2236 " point falls, whereas a maximum area in an .area file applies to only one\n"
2239 " triangle. Area constraints in .poly files are used only when a mesh is\n");
2241 " first generated, whereas area constraints in .area files are used only to\n"
2244 " refine an existing mesh, and are typically based on a posteriori error\n");
2246 " estimates resulting from a finite element simulation on that mesh.\n");
2249 " `triangle -rq25 object.1' will read object.1.node and object.1.ele, then\n"
2252 " refine the triangulation to enforce a 25 degree minimum angle, and then\n");
2254 " write the refined triangulation to object.2.node and object.2.ele.\n");
2257 " `triangle -rpaa6.2 z.3' will read z.3.node, z.3.ele, z.3.poly, and\n");
2259 " z.3.area. After reconstructing the mesh and its segments, Triangle will\n"
2262 " refine the mesh so that no triangle has area greater than 6.2, and\n");
2264 " furthermore the triangles satisfy the maximum area constraints in\n");
2266 " z.3.area. The output is written to z.4.node, z.4.ele, and z.4.poly.\n");
2269 " The sequence `triangle -qa1 x', `triangle -rqa.3 x.1', `triangle -rqa.1\n");
2271 " x.2' creates a sequence of successively finer meshes x.1, x.2, and x.3,\n");
2272 printf(" suitable for multigrid.\n\n");
2273 printf("Convex Hulls and Mesh Boundaries:\n\n");
2275 " If the input is a point set (rather than a PSLG), Triangle produces its\n");
2277 " convex hull as a by-product in the output .poly file if you use the -c\n");
2279 " switch. There are faster algorithms for finding a two-dimensional convex\n"
2282 " hull than triangulation, of course, but this one comes for free. If the\n"
2285 " input is an unconstrained mesh (you are using the -r switch but not the\n");
2287 " -p switch), Triangle produces a list of its boundary edges (including\n");
2288 printf(" hole boundaries) as a by-product if you use the -c switch.\n\n");
2289 printf("Voronoi Diagrams:\n\n");
2291 " The -v switch produces a Voronoi diagram, in files suffixed .v.node and\n");
2293 " .v.edge. For example, `triangle -v points' will read points.node,\n");
2295 " produce its Delaunay triangulation in points.1.node and points.1.ele,\n");
2297 " and produce its Voronoi diagram in points.1.v.node and points.1.v.edge.\n");
2299 " The .v.node file contains a list of all Voronoi vertices, and the .v.edge\n"
2302 " file contains a list of all Voronoi edges, some of which may be infinite\n"
2305 " rays. (The choice of filenames makes it easy to run the set of Voronoi\n");
2306 printf(" vertices through Triangle, if so desired.)\n\n");
2308 " This implementation does not use exact arithmetic to compute the Voronoi\n"
2311 " vertices, and does not check whether neighboring vertices are identical.\n"
2314 " Be forewarned that if the Delaunay triangulation is degenerate or\n");
2316 " near-degenerate, the Voronoi diagram may have duplicate points, crossing\n"
2319 " edges, or infinite rays whose direction vector is zero. Also, if you\n");
2321 " generate a constrained (as opposed to conforming) Delaunay triangulation,\n"
2324 " or if the triangulation has holes, the corresponding Voronoi diagram is\n");
2325 printf(" likely to have crossing edges and unlikely to make sense.\n\n");
2326 printf("Mesh Topology:\n\n");
2328 " You may wish to know which triangles are adjacent to a certain Delaunay\n");
2330 " edge in an .edge file, which Voronoi regions are adjacent to a certain\n");
2332 " Voronoi edge in a .v.edge file, or which Voronoi regions are adjacent to\n"
2335 " each other. All of this information can be found by cross-referencing\n");
2337 " output files with the recollection that the Delaunay triangulation and\n");
2338 printf(" the Voronoi diagrams are planar duals.\n\n");
2340 " Specifically, edge i of an .edge file is the dual of Voronoi edge i of\n");
2342 " the corresponding .v.edge file, and is rotated 90 degrees counterclock-\n");
2344 " wise from the Voronoi edge. Triangle j of an .ele file is the dual of\n");
2346 " vertex j of the corresponding .v.node file; and Voronoi region k is the\n");
2347 printf(" dual of point k of the corresponding .node file.\n\n");
2349 " Hence, to find the triangles adjacent to a Delaunay edge, look at the\n");
2351 " vertices of the corresponding Voronoi edge; their dual triangles are on\n");
2353 " the left and right of the Delaunay edge, respectively. To find the\n");
2355 " Voronoi regions adjacent to a Voronoi edge, look at the endpoints of the\n"
2358 " corresponding Delaunay edge; their dual regions are on the right and left\n"
2361 " of the Voronoi edge, respectively. To find which Voronoi regions are\n");
2362 printf(" adjacent to each other, just read the list of Delaunay edges.\n");
2364 printf("Statistics:\n");
2367 " After generating a mesh, Triangle prints a count of the number of points,\n"
2370 " triangles, edges, boundary edges, and segments in the output mesh. If\n");
2372 " you've forgotten the statistics for an existing mesh, the -rNEP switches\n"
2375 " (or -rpNEP if you've got a .poly file for the existing mesh) will\n");
2376 printf(" regenerate these statistics without writing any output.\n\n");
2378 " The -V switch produces extended statistics, including a rough estimate\n");
2380 " of memory use and a histogram of triangle aspect ratios and angles in the\n"
2382 printf(" mesh.\n\n");
2383 printf("Exact Arithmetic:\n\n");
2385 " Triangle uses adaptive exact arithmetic to perform what computational\n");
2387 " geometers call the `orientation' and `incircle' tests. If the floating-\n"
2390 " point arithmetic of your machine conforms to the IEEE 754 standard (as\n");
2392 " most workstations do), and does not use extended precision internal\n");
2394 " registers, then your output is guaranteed to be an absolutely true\n");
2395 printf(" Delaunay or conforming Delaunay triangulation, roundoff error\n");
2397 " notwithstanding. The word `adaptive' implies that these arithmetic\n");
2399 " routines compute the result only to the precision necessary to guarantee\n"
2402 " correctness, so they are usually nearly as fast as their approximate\n");
2404 " counterparts. The exact tests can be disabled with the -X switch. On\n");
2406 " most inputs, this switch will reduce the computation time by about eight\n"
2409 " percent - it's not worth the risk. There are rare difficult inputs\n");
2411 " (having many collinear and cocircular points), however, for which the\n");
2413 " difference could be a factor of two. These are precisely the inputs most\n"
2415 printf(" likely to cause errors if you use the -X switch.\n\n");
2417 " Unfortunately, these routines don't solve every numerical problem. Exact\n"
2420 " arithmetic is not used to compute the positions of points, because the\n");
2422 " bit complexity of point coordinates would grow without bound. Hence,\n");
2424 " segment intersections aren't computed exactly; in very unusual cases,\n");
2426 " roundoff error in computing an intersection point might actually lead to\n"
2429 " an inverted triangle and an invalid triangulation. (This is one reason\n");
2431 " to compute your own intersection points in your .poly files.) Similarly,\n"
2434 " exact arithmetic is not used to compute the vertices of the Voronoi\n");
2435 printf(" diagram.\n\n");
2437 " Underflow and overflow can also cause difficulties; the exact arithmetic\n"
2440 " routines do not ameliorate out-of-bounds exponents, which can arise\n");
2442 " during the orientation and incircle tests. As a rule of thumb, you\n");
2444 " should ensure that your input values are within a range such that their\n");
2446 " third powers can be taken without underflow or overflow. Underflow can\n");
2448 " silently prevent the tests from being performed exactly, while overflow\n");
2449 printf(" will typically cause a floating exception.\n\n");
2450 printf("Calling Triangle from Another Program:\n\n");
2451 printf(" Read the file triangle.h for details.\n\n");
2452 printf("Troubleshooting:\n\n");
2453 printf(" Please read this section before mailing me bugs.\n\n");
2454 printf(" `My output mesh has no triangles!'\n\n");
2456 " If you're using a PSLG, you've probably failed to specify a proper set\n"
2459 " of bounding segments, or forgotten to use the -c switch. Or you may\n");
2461 " have placed a hole badly. To test these possibilities, try again with\n"
2464 " the -c and -O switches. Alternatively, all your input points may be\n");
2466 " collinear, in which case you can hardly expect to triangulate them.\n");
2468 printf(" `Triangle doesn't terminate, or just crashes.'\n");
2471 " Bad things can happen when triangles get so small that the distance\n");
2473 " between their vertices isn't much larger than the precision of your\n");
2475 " machine's arithmetic. If you've compiled Triangle for single-precision\n"
2478 " arithmetic, you might do better by recompiling it for double-precision.\n"
2481 " Then again, you might just have to settle for more lenient constraints\n"
2484 " on the minimum angle and the maximum area than you had planned.\n");
2487 " You can minimize precision problems by ensuring that the origin lies\n");
2489 " inside your point set, or even inside the densest part of your\n");
2491 " mesh. On the other hand, if you're triangulating an object whose x\n");
2493 " coordinates all fall between 6247133 and 6247134, you're not leaving\n");
2494 printf(" much floating-point precision for Triangle to work with.\n\n");
2496 " Precision problems can occur covertly if the input PSLG contains two\n");
2498 " segments that meet (or intersect) at a very small angle, or if such an\n"
2501 " angle is introduced by the -c switch, which may occur if a point lies\n");
2503 " ever-so-slightly inside the convex hull, and is connected by a PSLG\n");
2505 " segment to a point on the convex hull. If you don't realize that a\n");
2507 " small angle is being formed, you might never discover why Triangle is\n");
2509 " crashing. To check for this possibility, use the -S switch (with an\n");
2511 " appropriate limit on the number of Steiner points, found by trial-and-\n"
2514 " error) to stop Triangle early, and view the output .poly file with\n");
2516 " Show Me (described below). Look carefully for small angles between\n");
2518 " segments; zoom in closely, as such segments might look like a single\n");
2519 printf(" segment from a distance.\n\n");
2521 " If some of the input values are too large, Triangle may suffer a\n");
2523 " floating exception due to overflow when attempting to perform an\n");
2525 " orientation or incircle test. (Read the section on exact arithmetic\n");
2527 " above.) Again, I recommend compiling Triangle for double (rather\n");
2528 printf(" than single) precision arithmetic.\n\n");
2530 " `The numbering of the output points doesn't match the input points.'\n");
2533 " You may have eaten some of your input points with a hole, or by placing\n"
2535 printf(" them outside the area enclosed by segments.\n\n");
2537 " `Triangle executes without incident, but when I look at the resulting\n");
2539 " mesh, it has overlapping triangles or other geometric inconsistencies.'\n");
2542 " If you select the -X switch, Triangle's divide-and-conquer Delaunay\n");
2544 " triangulation algorithm occasionally makes mistakes due to floating-\n");
2546 " point roundoff error. Although these errors are rare, don't use the -X\n"
2548 printf(" switch. If you still have problems, please report the bug.\n");
2551 " Strange things can happen if you've taken liberties with your PSLG. Do\n");
2553 " you have a point lying in the middle of a segment? Triangle sometimes\n");
2555 " copes poorly with that sort of thing. Do you want to lay out a collinear\n"
2558 " row of evenly spaced, segment-connected points? Have you simply defined\n"
2561 " one long segment connecting the leftmost point to the rightmost point,\n");
2563 " and a bunch of points lying along it? This method occasionally works,\n");
2565 " especially with horizontal and vertical lines, but often it doesn't, and\n"
2568 " you'll have to connect each adjacent pair of points with a separate\n");
2569 printf(" segment. If you don't like it, tough.\n\n");
2571 " Furthermore, if you have segments that intersect other than at their\n");
2573 " endpoints, try not to let the intersections fall extremely close to PSLG\n"
2575 printf(" points or each other.\n\n");
2577 " If you have problems refining a triangulation not produced by Triangle:\n");
2579 " Are you sure the triangulation is geometrically valid? Is it formatted\n");
2581 " correctly for Triangle? Are the triangles all listed so the first three\n"
2583 printf(" points are their corners in counterclockwise order?\n\n");
2584 printf("Show Me:\n\n");
2586 " Triangle comes with a separate program named `Show Me', whose primary\n");
2588 " purpose is to draw meshes on your screen or in PostScript. Its secondary\n"
2591 " purpose is to check the validity of your input files, and do so more\n");
2593 " thoroughly than Triangle does. Show Me requires that you have the X\n");
2595 " Windows system. If you didn't receive Show Me with Triangle, complain to\n"
2597 printf(" whomever you obtained Triangle from, then send me mail.\n\n");
2598 printf("Triangle on the Web:\n\n");
2600 " To see an illustrated, updated version of these instructions, check out\n");
2602 printf(" http://www.cs.cmu.edu/~quake/triangle.html\n");
2604 printf("A Brief Plea:\n");
2607 " If you use Triangle, and especially if you use it to accomplish real\n");
2609 " work, I would like very much to hear from you. A short letter or email\n");
2611 " (to jrs@cs.cmu.edu) describing how you use Triangle will mean a lot to\n");
2613 " me. The more people I know are using this program, the more easily I can\n"
2616 " justify spending time on improvements and on the three-dimensional\n");
2618 " successor to Triangle, which in turn will benefit you. Also, I can put\n");
2620 " you on a list to receive email whenever a new version of Triangle is\n");
2621 printf(" available.\n\n");
2623 " If you use a mesh generated by Triangle in a publication, please include\n"
2625 printf(" an acknowledgment as well.\n\n");
2626 printf("Research credit:\n\n");
2628 " Of course, I can take credit for only a fraction of the ideas that made\n");
2630 " this mesh generator possible. Triangle owes its existence to the efforts\n"
2633 " of many fine computational geometers and other researchers, including\n");
2635 " Marshall Bern, L. Paul Chew, Boris Delaunay, Rex A. Dwyer, David\n");
2637 " Eppstein, Steven Fortune, Leonidas J. Guibas, Donald E. Knuth, C. L.\n");
2639 " Lawson, Der-Tsai Lee, Ernst P. Mucke, Douglas M. Priest, Jim Ruppert,\n");
2641 " Isaac Saias, Bruce J. Schachter, Micha Sharir, Jorge Stolfi, Christopher\n"
2644 " J. Van Wyk, David F. Watson, and Binhai Zhu. See the comments at the\n");
2645 printf(" beginning of the source code for references.\n\n");
2649 #endif /* not TRILIBRARY */
2651 /*****************************************************************************/
2653 /* internalerror() Ask the user to send me the defective product. Exit. */
2655 /*****************************************************************************/
2657 void internalerror()
2659 printf(" Please report this bug to jrs@cs.cmu.edu\n");
2660 printf(" Include the message above, your input data set, and the exact\n");
2661 printf(" command line you used to run Triangle.\n");
2665 /*****************************************************************************/
2667 /* parsecommandline() Read the command line, identify switches, and set */
2668 /* up options and file names. */
2670 /* The effects of this routine are felt entirely through global variables. */
2672 /*****************************************************************************/
2674 void parsecommandline(argc, argv)
2679 #define STARTINDEX 0
2680 #else /* not TRILIBRARY */
2681 #define STARTINDEX 1
2684 #endif /* not TRILIBRARY */
2686 char workstring[FILENAMESIZE];
2688 poly = refine = quality = vararea = fixedarea = regionattrib = convex = 0;
2690 edgesout = voronoi = neighbors = geomview = 0;
2691 nobound = nopolywritten = nonodewritten = noelewritten = noiterationnum = 0;
2692 noholes = noexact = 0;
2693 incremental = sweepline = 0;
2702 quiet = verbose = 0;
2704 innodefilename[0] = '\0';
2705 #endif /* not TRILIBRARY */
2707 for (i = STARTINDEX; i < argc; i++) {
2709 if (argv[i][0] == '-') {
2710 #endif /* not TRILIBRARY */
2711 for (j = STARTINDEX; argv[i][j] != '\0'; j++) {
2712 if (argv[i][j] == 'p') {
2716 if (argv[i][j] == 'r') {
2719 if (argv[i][j] == 'q') {
2721 if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2722 (argv[i][j + 1] == '.')) {
2724 while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2725 (argv[i][j + 1] == '.')) {
2727 workstring[k] = argv[i][j];
2730 workstring[k] = '\0';
2731 minangle = (REAL) strtod(workstring, (char **) NULL);
2736 if (argv[i][j] == 'a') {
2738 if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2739 (argv[i][j + 1] == '.')) {
2742 while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
2743 (argv[i][j + 1] == '.')) {
2745 workstring[k] = argv[i][j];
2748 workstring[k] = '\0';
2749 maxarea = (REAL) strtod(workstring, (char **) NULL);
2750 if (maxarea <= 0.0) {
2751 printf("Error: Maximum area must be greater than zero.\n");
2758 #endif /* not CDT_ONLY */
2759 if (argv[i][j] == 'A') {
2762 if (argv[i][j] == 'c') {
2765 if (argv[i][j] == 'z') {
2768 if (argv[i][j] == 'e') {
2771 if (argv[i][j] == 'v') {
2774 if (argv[i][j] == 'n') {
2777 if (argv[i][j] == 'g') {
2780 if (argv[i][j] == 'B') {
2783 if (argv[i][j] == 'P') {
2786 if (argv[i][j] == 'N') {
2789 if (argv[i][j] == 'E') {
2793 if (argv[i][j] == 'I') {
2796 #endif /* not TRILIBRARY */
2797 if (argv[i][j] == 'O') {
2800 if (argv[i][j] == 'X') {
2803 if (argv[i][j] == 'o') {
2804 if (argv[i][j + 1] == '2') {
2810 if (argv[i][j] == 'Y') {
2813 if (argv[i][j] == 'S') {
2815 while ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) {
2817 steiner = steiner * 10 + (int) (argv[i][j] - '0');
2820 #endif /* not CDT_ONLY */
2822 if (argv[i][j] == 'i') {
2825 if (argv[i][j] == 'F') {
2828 #endif /* not REDUCED */
2829 if (argv[i][j] == 'l') {
2834 if (argv[i][j] == 's') {
2837 #endif /* not CDT_ONLY */
2838 if (argv[i][j] == 'C') {
2841 #endif /* not REDUCED */
2842 if (argv[i][j] == 'Q') {
2845 if (argv[i][j] == 'V') {
2849 if ((argv[i][j] == 'h') || (argv[i][j] == 'H') ||
2850 (argv[i][j] == '?')) {
2853 #endif /* not TRILIBRARY */
2857 strncpy(innodefilename, argv[i], FILENAMESIZE - 1);
2858 innodefilename[FILENAMESIZE - 1] = '\0';
2860 #endif /* not TRILIBRARY */
2863 if (innodefilename[0] == '\0') {
2866 if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".node")) {
2867 innodefilename[strlen(innodefilename) - 5] = '\0';
2869 if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".poly")) {
2870 innodefilename[strlen(innodefilename) - 5] = '\0';
2874 if (!strcmp(&innodefilename[strlen(innodefilename) - 4], ".ele")) {
2875 innodefilename[strlen(innodefilename) - 4] = '\0';
2878 if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".area")) {
2879 innodefilename[strlen(innodefilename) - 5] = '\0';
2884 #endif /* not CDT_ONLY */
2885 #endif /* not TRILIBRARY */
2886 steinerleft = steiner;
2887 useshelles = poly || refine || quality || convex;
2888 goodangle = cos(minangle * PI / 180.0);
2889 goodangle *= goodangle;
2890 if (refine && noiterationnum) {
2892 "Error: You cannot use the -I switch when refining a triangulation.\n");
2895 /* Be careful not to allocate space for element area constraints that */
2896 /* will never be assigned any value (other than the default -1.0). */
2897 if (!refine && !poly) {
2900 /* Be careful not to add an extra attribute to each element unless the */
2901 /* input supports it (PSLG in, but not refining a preexisting mesh). */
2902 if (refine || !poly) {
2907 strcpy(inpolyfilename, innodefilename);
2908 strcpy(inelefilename, innodefilename);
2909 strcpy(areafilename, innodefilename);
2911 strcpy(workstring, innodefilename);
2913 while (workstring[j] != '\0') {
2914 if ((workstring[j] == '.') && (workstring[j + 1] != '\0')) {
2920 if (increment > 0) {
2923 if ((workstring[j] >= '0') && (workstring[j] <= '9')) {
2924 meshnumber = meshnumber * 10 + (int) (workstring[j] - '0');
2929 } while (workstring[j] != '\0');
2931 if (noiterationnum) {
2932 strcpy(outnodefilename, innodefilename);
2933 strcpy(outelefilename, innodefilename);
2934 strcpy(edgefilename, innodefilename);
2935 strcpy(vnodefilename, innodefilename);
2936 strcpy(vedgefilename, innodefilename);
2937 strcpy(neighborfilename, innodefilename);
2938 strcpy(offfilename, innodefilename);
2939 strcat(outnodefilename, ".node");
2940 strcat(outelefilename, ".ele");
2941 strcat(edgefilename, ".edge");
2942 strcat(vnodefilename, ".v.node");
2943 strcat(vedgefilename, ".v.edge");
2944 strcat(neighborfilename, ".neigh");
2945 strcat(offfilename, ".off");
2946 } else if (increment == 0) {
2947 strcpy(outnodefilename, innodefilename);
2948 strcpy(outpolyfilename, innodefilename);
2949 strcpy(outelefilename, innodefilename);
2950 strcpy(edgefilename, innodefilename);
2951 strcpy(vnodefilename, innodefilename);
2952 strcpy(vedgefilename, innodefilename);
2953 strcpy(neighborfilename, innodefilename);
2954 strcpy(offfilename, innodefilename);
2955 strcat(outnodefilename, ".1.node");
2956 strcat(outpolyfilename, ".1.poly");
2957 strcat(outelefilename, ".1.ele");
2958 strcat(edgefilename, ".1.edge");
2959 strcat(vnodefilename, ".1.v.node");
2960 strcat(vedgefilename, ".1.v.edge");
2961 strcat(neighborfilename, ".1.neigh");
2962 strcat(offfilename, ".1.off");
2964 workstring[increment] = '%';
2965 workstring[increment + 1] = 'd';
2966 workstring[increment + 2] = '\0';
2967 sprintf(outnodefilename, workstring, meshnumber + 1);
2968 strcpy(outpolyfilename, outnodefilename);
2969 strcpy(outelefilename, outnodefilename);
2970 strcpy(edgefilename, outnodefilename);
2971 strcpy(vnodefilename, outnodefilename);
2972 strcpy(vedgefilename, outnodefilename);
2973 strcpy(neighborfilename, outnodefilename);
2974 strcpy(offfilename, outnodefilename);
2975 strcat(outnodefilename, ".node");
2976 strcat(outpolyfilename, ".poly");
2977 strcat(outelefilename, ".ele");
2978 strcat(edgefilename, ".edge");
2979 strcat(vnodefilename, ".v.node");
2980 strcat(vedgefilename, ".v.edge");
2981 strcat(neighborfilename, ".neigh");
2982 strcat(offfilename, ".off");
2984 strcat(innodefilename, ".node");
2985 strcat(inpolyfilename, ".poly");
2986 strcat(inelefilename, ".ele");
2987 strcat(areafilename, ".area");
2988 #endif /* not TRILIBRARY */
2993 /********* User interaction routines begin here *********/
2995 /********* Debugging routines begin here *********/
2999 /*****************************************************************************/
3001 /* printtriangle() Print out the details of a triangle/edge handle. */
3003 /* I originally wrote this procedure to simplify debugging; it can be */
3004 /* called directly from the debugger, and presents information about a */
3005 /* triangle/edge handle in digestible form. It's also used when the */
3006 /* highest level of verbosity (`-VVV') is specified. */
3008 /*****************************************************************************/
3010 void printtriangle(t)
3013 struct triedge printtri;
3014 struct edge printsh;
3017 printf("triangle x%lx with orientation %d:\n", (unsigned long) t->tri,
3019 decode(t->tri[0], printtri);
3020 if (printtri.tri == dummytri) {
3021 printf(" [0] = Outer space\n");
3023 printf(" [0] = x%lx %d\n", (unsigned long) printtri.tri,
3026 decode(t->tri[1], printtri);
3027 if (printtri.tri == dummytri) {
3028 printf(" [1] = Outer space\n");
3030 printf(" [1] = x%lx %d\n", (unsigned long) printtri.tri,
3033 decode(t->tri[2], printtri);
3034 if (printtri.tri == dummytri) {
3035 printf(" [2] = Outer space\n");
3037 printf(" [2] = x%lx %d\n", (unsigned long) printtri.tri,
3040 org(*t, printpoint);
3041 if (printpoint == (point) NULL)
3042 printf(" Origin[%d] = NULL\n", (t->orient + 1) % 3 + 3);
3044 printf(" Origin[%d] = x%lx (%.12g, %.12g)\n",
3045 (t->orient + 1) % 3 + 3, (unsigned long) printpoint,
3046 printpoint[0], printpoint[1]);
3047 dest(*t, printpoint);
3048 if (printpoint == (point) NULL)
3049 printf(" Dest [%d] = NULL\n", (t->orient + 2) % 3 + 3);
3051 printf(" Dest [%d] = x%lx (%.12g, %.12g)\n",
3052 (t->orient + 2) % 3 + 3, (unsigned long) printpoint,
3053 printpoint[0], printpoint[1]);
3054 apex(*t, printpoint);
3055 if (printpoint == (point) NULL)
3056 printf(" Apex [%d] = NULL\n", t->orient + 3);
3058 printf(" Apex [%d] = x%lx (%.12g, %.12g)\n",
3059 t->orient + 3, (unsigned long) printpoint,
3060 printpoint[0], printpoint[1]);
3062 sdecode(t->tri[6], printsh);
3063 if (printsh.sh != dummysh) {
3064 printf(" [6] = x%lx %d\n", (unsigned long) printsh.sh,
3067 sdecode(t->tri[7], printsh);
3068 if (printsh.sh != dummysh) {
3069 printf(" [7] = x%lx %d\n", (unsigned long) printsh.sh,
3072 sdecode(t->tri[8], printsh);
3073 if (printsh.sh != dummysh) {
3074 printf(" [8] = x%lx %d\n", (unsigned long) printsh.sh,
3079 printf(" Area constraint: %.4g\n", areabound(*t));
3083 /*****************************************************************************/
3085 /* printshelle() Print out the details of a shell edge handle. */
3087 /* I originally wrote this procedure to simplify debugging; it can be */
3088 /* called directly from the debugger, and presents information about a */
3089 /* shell edge handle in digestible form. It's also used when the highest */
3090 /* level of verbosity (`-VVV') is specified. */
3092 /*****************************************************************************/
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 /*****************************************************************************/
3174 void poolinit(pool, bytecount, itemcount, wtype, alignment)
3175 struct memorypool *pool;
3178 enum wordtype wtype;
3183 /* Initialize values in the pool. */
3184 pool->itemwordtype = wtype;
3185 wordsize = (pool->itemwordtype == POINTER) ? sizeof(VOID *) : sizeof(REAL);
3186 /* Find the proper alignment, which must be at least as large as: */
3187 /* - The parameter `alignment'. */
3188 /* - The primary word type, to avoid unaligned accesses. */
3189 /* - sizeof(VOID *), so the stack of dead items can be maintained */
3190 /* without unaligned accesses. */
3191 if (alignment > wordsize) {
3192 pool->alignbytes = alignment;
3194 pool->alignbytes = wordsize;
3196 if (sizeof(VOID *) > pool->alignbytes) {
3197 pool->alignbytes = sizeof(VOID *);
3199 pool->itemwords = ((bytecount + pool->alignbytes - 1) / pool->alignbytes)
3200 * (pool->alignbytes / wordsize);
3201 pool->itembytes = pool->itemwords * wordsize;
3202 pool->itemsperblock = itemcount;
3204 /* Allocate a block of items. Space for `itemsperblock' items and one */
3205 /* pointer (to point to the next block) are allocated, as well as space */
3206 /* to ensure alignment of the items. */
3207 pool->firstblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
3208 + sizeof(VOID *) + pool->alignbytes);
3209 if (pool->firstblock == (VOID **) NULL) {
3210 printf("Error: Out of memory.\n");
3213 /* Set the next block pointer to NULL. */
3214 *(pool->firstblock) = (VOID *) NULL;
3218 /*****************************************************************************/
3220 /* poolrestart() Deallocate all items in a pool. */
3222 /* The pool is returned to its starting state, except that no memory is */
3223 /* freed to the operating system. Rather, the previously allocated blocks */
3224 /* are ready to be reused. */
3226 /*****************************************************************************/
3228 void poolrestart(pool)
3229 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(pool)
3257 struct memorypool *pool;
3259 while (pool->firstblock != (VOID **) NULL) {
3260 pool->nowblock = (VOID **) *(pool->firstblock);
3261 free(pool->firstblock);
3262 pool->firstblock = pool->nowblock;
3266 /*****************************************************************************/
3268 /* poolalloc() Allocate space for an item. */
3270 /*****************************************************************************/
3272 VOID *poolalloc(pool)
3273 struct memorypool *pool;
3277 unsigned long alignptr;
3279 /* First check the linked list of dead items. If the list is not */
3280 /* empty, allocate an item from the list rather than a fresh one. */
3281 if (pool->deaditemstack != (VOID *) NULL) {
3282 newitem = pool->deaditemstack; /* Take first item in list. */
3283 pool->deaditemstack = * (VOID **) pool->deaditemstack;
3285 /* Check if there are any free items left in the current block. */
3286 if (pool->unallocateditems == 0) {
3287 /* Check if another block must be allocated. */
3288 if (*(pool->nowblock) == (VOID *) NULL) {
3289 /* Allocate a new block of items, pointed to by the previous block. */
3290 newblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
3291 + sizeof(VOID *) + pool->alignbytes);
3292 if (newblock == (VOID **) NULL) {
3293 printf("Error: Out of memory.\n");
3296 *(pool->nowblock) = (VOID *) newblock;
3297 /* The next block pointer is NULL. */
3298 *newblock = (VOID *) NULL;
3300 /* Move to the new block. */
3301 pool->nowblock = (VOID **) *(pool->nowblock);
3302 /* Find the first item in the block. */
3303 /* Increment by the size of (VOID *). */
3304 alignptr = (unsigned long) (pool->nowblock + 1);
3305 /* Align the item on an `alignbytes'-byte boundary. */
3306 pool->nextitem = (VOID *)
3307 (alignptr + (unsigned long) pool->alignbytes
3308 - (alignptr % (unsigned long) pool->alignbytes));
3309 /* There are lots of unallocated items left in this block. */
3310 pool->unallocateditems = pool->itemsperblock;
3312 /* Allocate a new item. */
3313 newitem = pool->nextitem;
3314 /* Advance `nextitem' pointer to next free item in block. */
3315 if (pool->itemwordtype == POINTER) {
3316 pool->nextitem = (VOID *) ((VOID **) pool->nextitem + pool->itemwords);
3318 pool->nextitem = (VOID *) ((REAL *) pool->nextitem + pool->itemwords);
3320 pool->unallocateditems--;
3327 /*****************************************************************************/
3329 /* pooldealloc() Deallocate space for an item. */
3331 /* The deallocated space is stored in a queue for later reuse. */
3333 /*****************************************************************************/
3335 void pooldealloc(pool, dyingitem)
3336 struct memorypool *pool;
3339 /* Push freshly killed item onto stack. */
3340 *((VOID **) dyingitem) = pool->deaditemstack;
3341 pool->deaditemstack = dyingitem;
3345 /*****************************************************************************/
3347 /* traversalinit() Prepare to traverse the entire list of items. */
3349 /* This routine is used in conjunction with traverse(). */
3351 /*****************************************************************************/
3353 void traversalinit(pool)
3354 struct memorypool *pool;
3356 unsigned long alignptr;
3358 /* Begin the traversal in the first block. */
3359 pool->pathblock = pool->firstblock;
3360 /* Find the first item in the block. Increment by the size of (VOID *). */
3361 alignptr = (unsigned long) (pool->pathblock + 1);
3362 /* Align with item on an `alignbytes'-byte boundary. */
3363 pool->pathitem = (VOID *)
3364 (alignptr + (unsigned long) pool->alignbytes
3365 - (alignptr % (unsigned long) pool->alignbytes));
3366 /* Set the number of items left in the current block. */
3367 pool->pathitemsleft = pool->itemsperblock;
3370 /*****************************************************************************/
3372 /* traverse() Find the next item in the list. */
3374 /* This routine is used in conjunction with traversalinit(). Be forewarned */
3375 /* that this routine successively returns all items in the list, including */
3376 /* deallocated ones on the deaditemqueue. It's up to you to figure out */
3377 /* which ones are actually dead. Why? I don't want to allocate extra */
3378 /* space just to demarcate dead items. It can usually be done more */
3379 /* space-efficiently by a routine that knows something about the structure */
3382 /*****************************************************************************/
3384 VOID *traverse(pool)
3385 struct memorypool *pool;
3388 unsigned long alignptr;
3390 /* Stop upon exhausting the list of items. */
3391 if (pool->pathitem == pool->nextitem) {
3392 return (VOID *) NULL;
3394 /* Check whether any untraversed items remain in the current block. */
3395 if (pool->pathitemsleft == 0) {
3396 /* Find the next block. */
3397 pool->pathblock = (VOID **) *(pool->pathblock);
3398 /* Find the first item in the block. Increment by the size of (VOID *). */
3399 alignptr = (unsigned long) (pool->pathblock + 1);
3400 /* Align with item on an `alignbytes'-byte boundary. */
3401 pool->pathitem = (VOID *)
3402 (alignptr + (unsigned long) pool->alignbytes
3403 - (alignptr % (unsigned long) pool->alignbytes));
3404 /* Set the number of items left in the current block. */
3405 pool->pathitemsleft = pool->itemsperblock;
3407 newitem = pool->pathitem;
3408 /* Find the next item in the block. */
3409 if (pool->itemwordtype == POINTER) {
3410 pool->pathitem = (VOID *) ((VOID **) pool->pathitem + pool->itemwords);
3412 pool->pathitem = (VOID *) ((REAL *) pool->pathitem + pool->itemwords);
3414 pool->pathitemsleft--;
3418 /*****************************************************************************/
3420 /* dummyinit() Initialize the triangle that fills "outer space" and the */
3421 /* omnipresent shell edge. */
3423 /* The triangle that fills "outer space", called `dummytri', is pointed to */
3424 /* by every triangle and shell edge on a boundary (be it outer or inner) of */
3425 /* the triangulation. Also, `dummytri' points to one of the triangles on */
3426 /* the convex hull (until the holes and concavities are carved), making it */
3427 /* possible to find a starting triangle for point location. */
3429 /* The omnipresent shell edge, `dummysh', is pointed to by every triangle */
3430 /* or shell edge that doesn't have a full complement of real shell edges */
3433 /*****************************************************************************/
3435 void dummyinit(trianglewords, shellewords)
3439 unsigned long alignptr;
3441 /* `triwords' and `shwords' are used by the mesh manipulation primitives */
3442 /* to extract orientations of triangles and shell edges from pointers. */
3443 triwords = trianglewords; /* Initialize `triwords' once and for all. */
3444 shwords = shellewords; /* Initialize `shwords' once and for all. */
3446 /* Set up `dummytri', the `triangle' that occupies "outer space". */
3447 dummytribase = (triangle *) malloc(triwords * sizeof(triangle)
3448 + triangles.alignbytes);
3449 if (dummytribase == (triangle *) NULL) {
3450 printf("Error: Out of memory.\n");
3453 /* Align `dummytri' on a `triangles.alignbytes'-byte boundary. */
3454 alignptr = (unsigned long) dummytribase;
3455 dummytri = (triangle *)
3456 (alignptr + (unsigned long) triangles.alignbytes
3457 - (alignptr % (unsigned long) triangles.alignbytes));
3458 /* Initialize the three adjoining triangles to be "outer space". These */
3459 /* will eventually be changed by various bonding operations, but their */
3460 /* values don't really matter, as long as they can legally be */
3462 dummytri[0] = (triangle) dummytri;
3463 dummytri[1] = (triangle) dummytri;
3464 dummytri[2] = (triangle) dummytri;
3465 /* Three NULL vertex points. */
3466 dummytri[3] = (triangle) NULL;
3467 dummytri[4] = (triangle) NULL;
3468 dummytri[5] = (triangle) NULL;
3471 /* Set up `dummysh', the omnipresent "shell edge" pointed to by any */
3472 /* triangle side or shell edge end that isn't attached to a real shell */
3474 dummyshbase = (shelle *) malloc(shwords * sizeof(shelle)
3475 + shelles.alignbytes);
3476 if (dummyshbase == (shelle *) NULL) {
3477 printf("Error: Out of memory.\n");
3480 /* Align `dummysh' on a `shelles.alignbytes'-byte boundary. */
3481 alignptr = (unsigned long) dummyshbase;
3482 dummysh = (shelle *)
3483 (alignptr + (unsigned long) shelles.alignbytes
3484 - (alignptr % (unsigned long) shelles.alignbytes));
3485 /* Initialize the two adjoining shell edges to be the omnipresent shell */
3486 /* edge. These will eventually be changed by various bonding */
3487 /* operations, but their values don't really matter, as long as they */
3488 /* can legally be dereferenced. */
3489 dummysh[0] = (shelle) dummysh;
3490 dummysh[1] = (shelle) dummysh;
3491 /* Two NULL vertex points. */
3492 dummysh[2] = (shelle) NULL;
3493 dummysh[3] = (shelle) NULL;
3494 /* Initialize the two adjoining triangles to be "outer space". */
3495 dummysh[4] = (shelle) dummytri;
3496 dummysh[5] = (shelle) dummytri;
3497 /* Set the boundary marker to zero. */
3498 * (int *) (dummysh + 6) = 0;
3500 /* Initialize the three adjoining shell edges of `dummytri' to be */
3501 /* the omnipresent shell edge. */
3502 dummytri[6] = (triangle) dummysh;
3503 dummytri[7] = (triangle) dummysh;
3504 dummytri[8] = (triangle) dummysh;
3508 /*****************************************************************************/
3510 /* initializepointpool() Calculate the size of the point data structure */
3511 /* and initialize its memory pool. */
3513 /* This routine also computes the `pointmarkindex' and `point2triindex' */
3514 /* indices used to find values within each point. */
3516 /*****************************************************************************/
3518 void initializepointpool()
3522 /* The index within each point at which the boundary marker is found. */
3523 /* Ensure the point marker is aligned to a sizeof(int)-byte address. */
3524 pointmarkindex = ((mesh_dim + nextras) * sizeof(REAL) + sizeof(int) - 1)
3526 pointsize = (pointmarkindex + 1) * sizeof(int);
3528 /* The index within each point at which a triangle pointer is found. */
3529 /* Ensure the pointer is aligned to a sizeof(triangle)-byte address. */
3530 point2triindex = (pointsize + sizeof(triangle) - 1) / sizeof(triangle);
3531 pointsize = (point2triindex + 1) * sizeof(triangle);
3533 /* Initialize the pool of points. */
3534 poolinit(&points, pointsize, POINTPERBLOCK,
3535 (sizeof(REAL) >= sizeof(triangle)) ? FLOATINGPOINT : POINTER, 0);
3538 /*****************************************************************************/
3540 /* initializetrisegpools() Calculate the sizes of the triangle and shell */
3541 /* edge data structures and initialize their */
3544 /* This routine also computes the `highorderindex', `elemattribindex', and */
3545 /* `areaboundindex' indices used to find values within each triangle. */
3547 /*****************************************************************************/
3549 void initializetrisegpools()
3553 /* The index within each triangle at which the extra nodes (above three) */
3554 /* associated with high order elements are found. There are three */
3555 /* pointers to other triangles, three pointers to corners, and possibly */
3556 /* three pointers to shell edges before the extra nodes. */
3557 highorderindex = 6 + (useshelles * 3);
3558 /* The number of bytes occupied by a triangle. */
3559 trisize = ((order + 1) * (order + 2) / 2 + (highorderindex - 3)) *
3561 /* The index within each triangle at which its attributes are found, */
3562 /* where the index is measured in REALs. */
3563 elemattribindex = (trisize + sizeof(REAL) - 1) / sizeof(REAL);
3564 /* The index within each triangle at which the maximum area constraint */
3565 /* is found, where the index is measured in REALs. Note that if the */
3566 /* `regionattrib' flag is set, an additional attribute will be added. */
3567 areaboundindex = elemattribindex + eextras + regionattrib;
3568 /* If triangle attributes or an area bound are needed, increase the number */
3569 /* of bytes occupied by a triangle. */
3571 trisize = (areaboundindex + 1) * sizeof(REAL);
3572 } else if (eextras + regionattrib > 0) {
3573 trisize = areaboundindex * sizeof(REAL);
3575 /* If a Voronoi diagram or triangle neighbor graph is requested, make */
3576 /* sure there's room to store an integer index in each triangle. This */
3577 /* integer index can occupy the same space as the shell edges or */
3578 /* attributes or area constraint or extra nodes. */
3579 if ((voronoi || neighbors) &&
3580 (trisize < 6 * sizeof(triangle) + sizeof(int))) {
3581 trisize = 6 * sizeof(triangle) + sizeof(int);
3583 /* Having determined the memory size of a triangle, initialize the pool. */
3584 poolinit(&triangles, trisize, TRIPERBLOCK, POINTER, 4);
3587 /* Initialize the pool of shell edges. */
3588 poolinit(&shelles, 6 * sizeof(triangle) + sizeof(int), SHELLEPERBLOCK,
3591 /* Initialize the "outer space" triangle and omnipresent shell edge. */
3592 dummyinit(triangles.itemwords, shelles.itemwords);
3594 /* Initialize the "outer space" triangle. */
3595 dummyinit(triangles.itemwords, 0);
3599 /*****************************************************************************/
3601 /* triangledealloc() Deallocate space for a triangle, marking it dead. */
3603 /*****************************************************************************/
3605 void triangledealloc(dyingtriangle)
3606 triangle *dyingtriangle;
3608 /* Set triangle's vertices to NULL. This makes it possible to */
3609 /* detect dead triangles when traversing the list of all triangles. */
3610 dyingtriangle[3] = (triangle) NULL;
3611 dyingtriangle[4] = (triangle) NULL;
3612 dyingtriangle[5] = (triangle) NULL;
3613 pooldealloc(&triangles, (VOID *) dyingtriangle);
3616 /*****************************************************************************/
3618 /* triangletraverse() Traverse the triangles, skipping dead ones. */
3620 /*****************************************************************************/
3622 triangle *triangletraverse()
3624 triangle *newtriangle;
3627 newtriangle = (triangle *) traverse(&triangles);
3628 if (newtriangle == (triangle *) NULL) {
3629 return (triangle *) NULL;
3631 } while (newtriangle[3] == (triangle) NULL); /* Skip dead ones. */
3635 /*****************************************************************************/
3637 /* shelledealloc() Deallocate space for a shell edge, marking it dead. */
3639 /*****************************************************************************/
3641 void shelledealloc(dyingshelle)
3642 shelle *dyingshelle;
3644 /* Set shell edge's vertices to NULL. This makes it possible to */
3645 /* detect dead shells when traversing the list of all shells. */
3646 dyingshelle[2] = (shelle) NULL;
3647 dyingshelle[3] = (shelle) NULL;
3648 pooldealloc(&shelles, (VOID *) dyingshelle);
3651 /*****************************************************************************/
3653 /* shelletraverse() Traverse the shell edges, skipping dead ones. */
3655 /*****************************************************************************/
3657 shelle *shelletraverse()
3662 newshelle = (shelle *) traverse(&shelles);
3663 if (newshelle == (shelle *) NULL) {
3664 return (shelle *) NULL;
3666 } while (newshelle[2] == (shelle) NULL); /* Skip dead ones. */
3670 /*****************************************************************************/
3672 /* pointdealloc() Deallocate space for a point, marking it dead. */
3674 /*****************************************************************************/
3676 void pointdealloc(dyingpoint)
3679 /* Mark the point as dead. This makes it possible to detect dead points */
3680 /* when traversing the list of all points. */
3681 setpointmark(dyingpoint, DEADPOINT);
3682 pooldealloc(&points, (VOID *) dyingpoint);
3685 /*****************************************************************************/
3687 /* pointtraverse() Traverse the points, skipping dead ones. */
3689 /*****************************************************************************/
3691 point pointtraverse()
3696 newpoint = (point) traverse(&points);
3697 if (newpoint == (point) NULL) {
3698 return (point) NULL;
3700 } while (pointmark(newpoint) == DEADPOINT); /* Skip dead ones. */
3704 /*****************************************************************************/
3706 /* badsegmentdealloc() Deallocate space for a bad segment, marking it */
3709 /*****************************************************************************/
3713 void badsegmentdealloc(dyingseg)
3714 struct edge *dyingseg;
3716 /* Set segment's orientation to -1. This makes it possible to */
3717 /* detect dead segments when traversing the list of all segments. */
3718 dyingseg->shorient = -1;
3719 pooldealloc(&badsegments, (VOID *) dyingseg);
3722 #endif /* not CDT_ONLY */
3724 /*****************************************************************************/
3726 /* badsegmenttraverse() Traverse the bad segments, skipping dead ones. */
3728 /*****************************************************************************/
3732 struct edge *badsegmenttraverse()
3734 struct edge *newseg;
3737 newseg = (struct edge *) traverse(&badsegments);
3738 if (newseg == (struct edge *) NULL) {
3739 return (struct edge *) NULL;
3741 } while (newseg->shorient == -1); /* Skip dead ones. */
3745 #endif /* not CDT_ONLY */
3747 /*****************************************************************************/
3749 /* getpoint() Get a specific point, by number, from the list. */
3751 /* The first point is number 'firstnumber'. */
3753 /* Note that this takes O(n) time (with a small constant, if POINTPERBLOCK */
3754 /* is large). I don't care to take the trouble to make it work in constant */
3757 /*****************************************************************************/
3759 point getpoint(number)
3764 unsigned long alignptr;
3767 getblock = points.firstblock;
3768 current = firstnumber;
3769 /* Find the right block. */
3770 while (current + points.itemsperblock <= number) {
3771 getblock = (VOID **) *getblock;
3772 current += points.itemsperblock;
3774 /* Now find the right point. */
3775 alignptr = (unsigned long) (getblock + 1);
3776 foundpoint = (point) (alignptr + (unsigned long) points.alignbytes
3777 - (alignptr % (unsigned long) points.alignbytes));
3778 while (current < number) {
3779 foundpoint += points.itemwords;
3785 /*****************************************************************************/
3787 /* triangledeinit() Free all remaining allocated memory. */
3789 /*****************************************************************************/
3791 void triangledeinit()
3793 pooldeinit(&triangles);
3796 pooldeinit(&shelles);
3799 pooldeinit(&points);
3802 pooldeinit(&badsegments);
3803 if ((minangle > 0.0) || vararea || fixedarea) {
3804 pooldeinit(&badtriangles);
3807 #endif /* not CDT_ONLY */
3812 /********* Memory management routines end here *********/
3814 /********* Constructors begin here *********/
3818 /*****************************************************************************/
3820 /* maketriangle() Create a new triangle with orientation zero. */
3822 /*****************************************************************************/
3824 void maketriangle(newtriedge)
3825 struct triedge *newtriedge;
3829 newtriedge->tri = (triangle *) poolalloc(&triangles);
3830 /* Initialize the three adjoining triangles to be "outer space". */
3831 newtriedge->tri[0] = (triangle) dummytri;
3832 newtriedge->tri[1] = (triangle) dummytri;
3833 newtriedge->tri[2] = (triangle) dummytri;
3834 /* Three NULL vertex points. */
3835 newtriedge->tri[3] = (triangle) NULL;
3836 newtriedge->tri[4] = (triangle) NULL;
3837 newtriedge->tri[5] = (triangle) NULL;
3838 /* Initialize the three adjoining shell edges to be the omnipresent */
3841 newtriedge->tri[6] = (triangle) dummysh;
3842 newtriedge->tri[7] = (triangle) dummysh;
3843 newtriedge->tri[8] = (triangle) dummysh;
3845 for (i = 0; i < eextras; i++) {
3846 setelemattribute(*newtriedge, i, 0.0);
3849 setareabound(*newtriedge, -1.0);
3852 newtriedge->orient = 0;
3855 /*****************************************************************************/
3857 /* makeshelle() Create a new shell edge with orientation zero. */
3859 /*****************************************************************************/
3861 void makeshelle(newedge)
3862 struct edge *newedge;
3864 newedge->sh = (shelle *) poolalloc(&shelles);
3865 /* Initialize the two adjoining shell edges to be the omnipresent */
3867 newedge->sh[0] = (shelle) dummysh;
3868 newedge->sh[1] = (shelle) dummysh;
3869 /* Two NULL vertex points. */
3870 newedge->sh[2] = (shelle) NULL;
3871 newedge->sh[3] = (shelle) NULL;
3872 /* Initialize the two adjoining triangles to be "outer space". */
3873 newedge->sh[4] = (shelle) dummytri;
3874 newedge->sh[5] = (shelle) dummytri;
3875 /* Set the boundary marker to zero. */
3876 setmark(*newedge, 0);
3878 newedge->shorient = 0;
3883 /********* Constructors end here *********/
3885 /********* Determinant evaluation routines begin here *********/
3889 /* The adaptive exact arithmetic geometric predicates implemented herein are */
3890 /* described in detail in my Technical Report CMU-CS-96-140. The complete */
3891 /* reference is given in the header. */
3893 /* Which of the following two methods of finding the absolute values is */
3894 /* fastest is compiler-dependent. A few compilers can inline and optimize */
3895 /* the fabs() call; but most will incur the overhead of a function call, */
3896 /* which is disastrously slow. A faster way on IEEE machines might be to */
3897 /* mask the appropriate bit, but that's difficult to do in C. */
3899 #define Absolute(a) ((a) >= 0.0 ? (a) : -(a))
3900 /* #define Absolute(a) fabs(a) */
3902 /* Many of the operations are broken up into two pieces, a main part that */
3903 /* performs an approximate operation, and a "tail" that computes the */
3904 /* roundoff error of that operation. */
3906 /* The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(), */
3907 /* Split(), and Two_Product() are all implemented as described in the */
3908 /* reference. Each of these macros requires certain variables to be */
3909 /* defined in the calling routine. The variables `bvirt', `c', `abig', */
3910 /* `_i', `_j', `_k', `_l', `_m', and `_n' are declared `INEXACT' because */
3911 /* they store the result of an operation that may incur roundoff error. */
3912 /* The input parameter `x' (or the highest numbered `x_' parameter) must */
3913 /* also be declared `INEXACT'. */
3915 #define Fast_Two_Sum_Tail(a, b, x, y) \
3919 #define Fast_Two_Sum(a, b, x, y) \
3920 x = (REAL) (a + b); \
3921 Fast_Two_Sum_Tail(a, b, x, y)
3923 #define Two_Sum_Tail(a, b, x, y) \
3924 bvirt = (REAL) (x - a); \
3925 avirt = x - bvirt; \
3926 bround = b - bvirt; \
3927 around = a - avirt; \
3930 #define Two_Sum(a, b, x, y) \
3931 x = (REAL) (a + b); \
3932 Two_Sum_Tail(a, b, x, y)
3934 #define Two_Diff_Tail(a, b, x, y) \
3935 bvirt = (REAL) (a - x); \
3936 avirt = x + bvirt; \
3937 bround = bvirt - b; \
3938 around = a - avirt; \
3941 #define Two_Diff(a, b, x, y) \
3942 x = (REAL) (a - b); \
3943 Two_Diff_Tail(a, b, x, y)
3945 #define Split(a, ahi, alo) \
3946 c = (REAL) (splitter * a); \
3947 abig = (REAL) (c - a); \
3951 #define Two_Product_Tail(a, b, x, y) \
3952 Split(a, ahi, alo); \
3953 Split(b, bhi, blo); \
3954 err1 = x - (ahi * bhi); \
3955 err2 = err1 - (alo * bhi); \
3956 err3 = err2 - (ahi * blo); \
3957 y = (alo * blo) - err3
3959 #define Two_Product(a, b, x, y) \
3960 x = (REAL) (a * b); \
3961 Two_Product_Tail(a, b, x, y)
3963 /* Two_Product_Presplit() is Two_Product() where one of the inputs has */
3964 /* already been split. Avoids redundant splitting. */
3966 #define Two_Product_Presplit(a, b, bhi, blo, x, y) \
3967 x = (REAL) (a * b); \
3968 Split(a, ahi, alo); \
3969 err1 = x - (ahi * bhi); \
3970 err2 = err1 - (alo * bhi); \
3971 err3 = err2 - (ahi * blo); \
3972 y = (alo * blo) - err3
3974 /* Square() can be done more quickly than Two_Product(). */
3976 #define Square_Tail(a, x, y) \
3977 Split(a, ahi, alo); \
3978 err1 = x - (ahi * ahi); \
3979 err3 = err1 - ((ahi + ahi) * alo); \
3980 y = (alo * alo) - err3
3982 #define Square(a, x, y) \
3983 x = (REAL) (a * a); \
3984 Square_Tail(a, x, y)
3986 /* Macros for summing expansions of various fixed lengths. These are all */
3987 /* unrolled versions of Expansion_Sum(). */
3989 #define Two_One_Sum(a1, a0, b, x2, x1, x0) \
3990 Two_Sum(a0, b , _i, x0); \
3991 Two_Sum(a1, _i, x2, x1)
3993 #define Two_One_Diff(a1, a0, b, x2, x1, x0) \
3994 Two_Diff(a0, b , _i, x0); \
3995 Two_Sum( a1, _i, x2, x1)
3997 #define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0) \
3998 Two_One_Sum(a1, a0, b0, _j, _0, x0); \
3999 Two_One_Sum(_j, _0, b1, x3, x2, x1)
4001 #define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0) \
4002 Two_One_Diff(a1, a0, b0, _j, _0, x0); \
4003 Two_One_Diff(_j, _0, b1, x3, x2, x1)
4005 /*****************************************************************************/
4007 /* exactinit() Initialize the variables used for exact arithmetic. */
4009 /* `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in */
4010 /* floating-point arithmetic. `epsilon' bounds the relative roundoff */
4011 /* error. It is used for floating-point error analysis. */
4013 /* `splitter' is used to split floating-point numbers into two half- */
4014 /* length significands for exact multiplication. */
4016 /* I imagine that a highly optimizing compiler might be too smart for its */
4017 /* own good, and somehow cause this routine to fail, if it pretends that */
4018 /* floating-point arithmetic is too much like real arithmetic. */
4020 /* Don't change this routine unless you fully understand it. */
4022 /*****************************************************************************/
4027 REAL check, lastcheck;
4035 /* Repeatedly divide `epsilon' by two until it is too small to add to */
4036 /* one without causing roundoff. (Also check if the sum is equal to */
4037 /* the previous sum, for machines that round up instead of using exact */
4038 /* rounding. Not that these routines will work on such machines anyway. */
4045 every_other = !every_other;
4046 check = 1.0 + epsilon;
4047 } while ((check != 1.0) && (check != lastcheck));
4050 printf("Floating point roundoff is of magnitude %.17g\n", epsilon);
4051 printf("Floating point splitter is %.17g\n", splitter);
4053 /* Error bounds for orientation and incircle tests. */
4054 resulterrbound = (3.0 + 8.0 * epsilon) * epsilon;
4055 ccwerrboundA = (3.0 + 16.0 * epsilon) * epsilon;
4056 ccwerrboundB = (2.0 + 12.0 * epsilon) * epsilon;
4057 ccwerrboundC = (9.0 + 64.0 * epsilon) * epsilon * epsilon;
4058 iccerrboundA = (10.0 + 96.0 * epsilon) * epsilon;
4059 iccerrboundB = (4.0 + 48.0 * epsilon) * epsilon;
4060 iccerrboundC = (44.0 + 576.0 * epsilon) * epsilon * epsilon;
4063 /*****************************************************************************/
4065 /* fast_expansion_sum_zeroelim() Sum two expansions, eliminating zero */
4066 /* components from the output expansion. */
4068 /* Sets h = e + f. See my Robust Predicates paper for details. */
4070 /* If round-to-even is used (as with IEEE 754), maintains the strongly */
4071 /* nonoverlapping property. (That is, if e is strongly nonoverlapping, h */
4072 /* will be also.) Does NOT maintain the nonoverlapping or nonadjacent */
4075 /*****************************************************************************/
4077 int fast_expansion_sum_zeroelim(elen, e, flen, f, h) /* h cannot be e or f. */
4088 REAL avirt, bround, around;
4089 int eindex, findex, hindex;
4094 eindex = findex = 0;
4095 if ((fnow > enow) == (fnow > -enow)) {
4103 if ((eindex < elen) && (findex < flen)) {
4104 if ((fnow > enow) == (fnow > -enow)) {
4105 Fast_Two_Sum(enow, Q, Qnew, hh);
4108 Fast_Two_Sum(fnow, Q, Qnew, hh);
4115 while ((eindex < elen) && (findex < flen)) {
4116 if ((fnow > enow) == (fnow > -enow)) {
4117 Two_Sum(Q, enow, Qnew, hh);
4120 Two_Sum(Q, fnow, Qnew, hh);
4129 while (eindex < elen) {
4130 Two_Sum(Q, enow, Qnew, hh);
4137 while (findex < flen) {
4138 Two_Sum(Q, fnow, Qnew, hh);
4145 if ((Q != 0.0) || (hindex == 0)) {
4151 /*****************************************************************************/
4153 /* scale_expansion_zeroelim() Multiply an expansion by a scalar, */
4154 /* eliminating zero components from the */
4155 /* output expansion. */
4157 /* Sets h = be. See my Robust Predicates paper for details. */
4159 /* Maintains the nonoverlapping property. If round-to-even is used (as */
4160 /* with IEEE 754), maintains the strongly nonoverlapping and nonadjacent */
4161 /* properties as well. (That is, if e has one of these properties, so */
4164 /*****************************************************************************/
4166 int scale_expansion_zeroelim(elen, e, b, h) /* e and h cannot be the same. */
4172 INEXACT REAL Q, sum;
4174 INEXACT REAL product1;
4179 REAL avirt, bround, around;
4182 REAL ahi, alo, bhi, blo;
4183 REAL err1, err2, err3;
4186 Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
4191 for (eindex = 1; eindex < elen; eindex++) {
4193 Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
4194 Two_Sum(Q, product0, sum, hh);
4198 Fast_Two_Sum(product1, sum, Q, hh);
4203 if ((Q != 0.0) || (hindex == 0)) {
4209 /*****************************************************************************/
4211 /* estimate() Produce a one-word estimate of an expansion's value. */
4213 /* See my Robust Predicates paper for details. */
4215 /*****************************************************************************/
4217 REAL estimate(elen, e)
4225 for (eindex = 1; eindex < elen; eindex++) {
4231 /*****************************************************************************/
4233 /* counterclockwise() Return a positive value if the points pa, pb, and */
4234 /* pc occur in counterclockwise order; a negative */
4235 /* value if they occur in clockwise order; and zero */
4236 /* if they are collinear. The result is also a rough */
4237 /* approximation of twice the signed area of the */
4238 /* triangle defined by the three points. */
4240 /* Uses exact arithmetic if necessary to ensure a correct answer. The */
4241 /* result returned is the determinant of a matrix. This determinant is */
4242 /* computed adaptively, in the sense that exact arithmetic is used only to */
4243 /* the degree it is needed to ensure that the returned value has the */
4244 /* correct sign. Hence, this function is usually quite fast, but will run */
4245 /* more slowly when the input points are collinear or nearly so. */
4247 /* See my Robust Predicates paper for details. */
4249 /*****************************************************************************/
4251 REAL counterclockwiseadapt(pa, pb, pc, detsum)
4257 INEXACT REAL acx, acy, bcx, bcy;
4258 REAL acxtail, acytail, bcxtail, bcytail;
4259 INEXACT REAL detleft, detright;
4260 REAL detlefttail, detrighttail;
4262 REAL B[4], C1[8], C2[12], D[16];
4264 int C1length, C2length, Dlength;
4267 INEXACT REAL s1, t1;
4271 REAL avirt, bround, around;
4274 REAL ahi, alo, bhi, blo;
4275 REAL err1, err2, err3;
4276 INEXACT REAL _i, _j;
4279 acx = (REAL) (pa[0] - pc[0]);
4280 bcx = (REAL) (pb[0] - pc[0]);
4281 acy = (REAL) (pa[1] - pc[1]);
4282 bcy = (REAL) (pb[1] - pc[1]);
4284 Two_Product(acx, bcy, detleft, detlefttail);
4285 Two_Product(acy, bcx, detright, detrighttail);
4287 Two_Two_Diff(detleft, detlefttail, detright, detrighttail,
4288 B3, B[2], B[1], B[0]);
4291 det = estimate(4, B);
4292 errbound = ccwerrboundB * detsum;
4293 if ((det >= errbound) || (-det >= errbound)) {
4297 Two_Diff_Tail(pa[0], pc[0], acx, acxtail);
4298 Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);
4299 Two_Diff_Tail(pa[1], pc[1], acy, acytail);
4300 Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);
4302 if ((acxtail == 0.0) && (acytail == 0.0)
4303 && (bcxtail == 0.0) && (bcytail == 0.0)) {
4307 errbound = ccwerrboundC * detsum + resulterrbound * Absolute(det);
4308 det += (acx * bcytail + bcy * acxtail)
4309 - (acy * bcxtail + bcx * acytail);
4310 if ((det >= errbound) || (-det >= errbound)) {
4314 Two_Product(acxtail, bcy, s1, s0);
4315 Two_Product(acytail, bcx, t1, t0);
4316 Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
4318 C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);
4320 Two_Product(acx, bcytail, s1, s0);
4321 Two_Product(acy, bcxtail, t1, t0);
4322 Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
4324 C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);
4326 Two_Product(acxtail, bcytail, s1, s0);
4327 Two_Product(acytail, bcxtail, t1, t0);
4328 Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
4330 Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);
4332 return(D[Dlength - 1]);
4335 REAL counterclockwise(pa, pb, pc)
4340 REAL detleft, detright, det;
4341 REAL detsum, errbound;
4343 counterclockcount++;
4345 detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);
4346 detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
4347 det = detleft - detright;
4353 if (detleft > 0.0) {
4354 if (detright <= 0.0) {
4357 detsum = detleft + detright;
4359 } else if (detleft < 0.0) {
4360 if (detright >= 0.0) {
4363 detsum = -detleft - detright;
4369 errbound = ccwerrboundA * detsum;
4370 if ((det >= errbound) || (-det >= errbound)) {
4374 return counterclockwiseadapt(pa, pb, pc, detsum);
4377 /*****************************************************************************/
4379 /* incircle() Return a positive value if the point pd lies inside the */
4380 /* circle passing through pa, pb, and pc; a negative value if */
4381 /* it lies outside; and zero if the four points are cocircular.*/
4382 /* The points pa, pb, and pc must be in counterclockwise */
4383 /* order, or the sign of the result will be reversed. */
4385 /* Uses exact arithmetic if necessary to ensure a correct answer. The */
4386 /* result returned is the determinant of a matrix. This determinant is */
4387 /* computed adaptively, in the sense that exact arithmetic is used only to */
4388 /* the degree it is needed to ensure that the returned value has the */
4389 /* correct sign. Hence, this function is usually quite fast, but will run */
4390 /* more slowly when the input points are cocircular or nearly so. */
4392 /* See my Robust Predicates paper for details. */
4394 /*****************************************************************************/
4396 REAL incircleadapt(pa, pb, pc, pd, permanent)
4403 INEXACT REAL adx, bdx, cdx, ady, bdy, cdy;
4406 INEXACT REAL bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
4407 REAL bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
4408 REAL bc[4], ca[4], ab[4];
4409 INEXACT REAL bc3, ca3, ab3;
4410 REAL axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];
4411 int axbclen, axxbclen, aybclen, ayybclen, alen;
4412 REAL bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];
4413 int bxcalen, bxxcalen, bycalen, byycalen, blen;
4414 REAL cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];
4415 int cxablen, cxxablen, cyablen, cyyablen, clen;
4418 REAL fin1[1152], fin2[1152];
4419 REAL *finnow, *finother, *finswap;
4422 REAL adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;
4423 INEXACT REAL adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;
4424 REAL adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;
4425 REAL aa[4], bb[4], cc[4];
4426 INEXACT REAL aa3, bb3, cc3;
4427 INEXACT REAL ti1, tj1;
4430 INEXACT REAL u3, v3;
4431 REAL temp8[8], temp16a[16], temp16b[16], temp16c[16];
4432 REAL temp32a[32], temp32b[32], temp48[48], temp64[64];
4433 int temp8len, temp16alen, temp16blen, temp16clen;
4434 int temp32alen, temp32blen, temp48len, temp64len;
4435 REAL axtbb[8], axtcc[8], aytbb[8], aytcc[8];
4436 int axtbblen, axtcclen, aytbblen, aytcclen;
4437 REAL bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];
4438 int bxtaalen, bxtcclen, bytaalen, bytcclen;
4439 REAL cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];
4440 int cxtaalen, cxtbblen, cytaalen, cytbblen;
4441 REAL axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];
4442 int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;
4443 REAL axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16], cytabt[16];
4444 int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;
4445 REAL axtbctt[8], aytbctt[8], bxtcatt[8];
4446 REAL bytcatt[8], cxtabtt[8], cytabtt[8];
4447 int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;
4448 REAL abt[8], bct[8], cat[8];
4449 int abtlen, bctlen, catlen;
4450 REAL abtt[4], bctt[4], catt[4];
4451 int abttlen, bcttlen, cattlen;
4452 INEXACT REAL abtt3, bctt3, catt3;
4456 REAL avirt, bround, around;
4459 REAL ahi, alo, bhi, blo;
4460 REAL err1, err2, err3;
4461 INEXACT REAL _i, _j;
4464 adx = (REAL) (pa[0] - pd[0]);
4465 bdx = (REAL) (pb[0] - pd[0]);
4466 cdx = (REAL) (pc[0] - pd[0]);
4467 ady = (REAL) (pa[1] - pd[1]);
4468 bdy = (REAL) (pb[1] - pd[1]);
4469 cdy = (REAL) (pc[1] - pd[1]);
4471 Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
4472 Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
4473 Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
4475 axbclen = scale_expansion_zeroelim(4, bc, adx, axbc);
4476 axxbclen = scale_expansion_zeroelim(axbclen, axbc, adx, axxbc);
4477 aybclen = scale_expansion_zeroelim(4, bc, ady, aybc);
4478 ayybclen = scale_expansion_zeroelim(aybclen, aybc, ady, ayybc);
4479 alen = fast_expansion_sum_zeroelim(axxbclen, axxbc, ayybclen, ayybc, adet);
4481 Two_Product(cdx, ady, cdxady1, cdxady0);
4482 Two_Product(adx, cdy, adxcdy1, adxcdy0);
4483 Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
4485 bxcalen = scale_expansion_zeroelim(4, ca, bdx, bxca);
4486 bxxcalen = scale_expansion_zeroelim(bxcalen, bxca, bdx, bxxca);
4487 bycalen = scale_expansion_zeroelim(4, ca, bdy, byca);
4488 byycalen = scale_expansion_zeroelim(bycalen, byca, bdy, byyca);
4489 blen = fast_expansion_sum_zeroelim(bxxcalen, bxxca, byycalen, byyca, bdet);
4491 Two_Product(adx, bdy, adxbdy1, adxbdy0);
4492 Two_Product(bdx, ady, bdxady1, bdxady0);
4493 Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
4495 cxablen = scale_expansion_zeroelim(4, ab, cdx, cxab);
4496 cxxablen = scale_expansion_zeroelim(cxablen, cxab, cdx, cxxab);
4497 cyablen = scale_expansion_zeroelim(4, ab, cdy, cyab);
4498 cyyablen = scale_expansion_zeroelim(cyablen, cyab, cdy, cyyab);
4499 clen = fast_expansion_sum_zeroelim(cxxablen, cxxab, cyyablen, cyyab, cdet);
4501 ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);
4502 finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);
4504 det = estimate(finlength, fin1);
4505 errbound = iccerrboundB * permanent;
4506 if ((det >= errbound) || (-det >= errbound)) {
4510 Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
4511 Two_Diff_Tail(pa[1], pd[1], ady, adytail);
4512 Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
4513 Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
4514 Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
4515 Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
4516 if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0)
4517 && (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0)) {
4521 errbound = iccerrboundC * permanent + resulterrbound * Absolute(det);
4522 det += ((adx * adx + ady * ady) * ((bdx * cdytail + cdy * bdxtail)
4523 - (bdy * cdxtail + cdx * bdytail))
4524 + 2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx))
4525 + ((bdx * bdx + bdy * bdy) * ((cdx * adytail + ady * cdxtail)
4526 - (cdy * adxtail + adx * cdytail))
4527 + 2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx))
4528 + ((cdx * cdx + cdy * cdy) * ((adx * bdytail + bdy * adxtail)
4529 - (ady * bdxtail + bdx * adytail))
4530 + 2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx));
4531 if ((det >= errbound) || (-det >= errbound)) {
4538 if ((bdxtail != 0.0) || (bdytail != 0.0)
4539 || (cdxtail != 0.0) || (cdytail != 0.0)) {
4540 Square(adx, adxadx1, adxadx0);
4541 Square(ady, adyady1, adyady0);
4542 Two_Two_Sum(adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);
4545 if ((cdxtail != 0.0) || (cdytail != 0.0)
4546 || (adxtail != 0.0) || (adytail != 0.0)) {
4547 Square(bdx, bdxbdx1, bdxbdx0);
4548 Square(bdy, bdybdy1, bdybdy0);
4549 Two_Two_Sum(bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);
4552 if ((adxtail != 0.0) || (adytail != 0.0)
4553 || (bdxtail != 0.0) || (bdytail != 0.0)) {
4554 Square(cdx, cdxcdx1, cdxcdx0);
4555 Square(cdy, cdycdy1, cdycdy0);
4556 Two_Two_Sum(cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);
4560 if (adxtail != 0.0) {
4561 axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);
4562 temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx,
4565 axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);
4566 temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);
4568 axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);
4569 temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);
4571 temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4572 temp16blen, temp16b, temp32a);
4573 temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4574 temp32alen, temp32a, temp48);
4575 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4577 finswap = finnow; finnow = finother; finother = finswap;
4579 if (adytail != 0.0) {
4580 aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);
4581 temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady,
4584 aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);
4585 temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);
4587 aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);
4588 temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);
4590 temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4591 temp16blen, temp16b, temp32a);
4592 temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4593 temp32alen, temp32a, temp48);
4594 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4596 finswap = finnow; finnow = finother; finother = finswap;
4598 if (bdxtail != 0.0) {
4599 bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);
4600 temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx,
4603 bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);
4604 temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);
4606 bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);
4607 temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);
4609 temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4610 temp16blen, temp16b, temp32a);
4611 temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4612 temp32alen, temp32a, temp48);
4613 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4615 finswap = finnow; finnow = finother; finother = finswap;
4617 if (bdytail != 0.0) {
4618 bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);
4619 temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy,
4622 bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);
4623 temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);
4625 bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);
4626 temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);
4628 temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4629 temp16blen, temp16b, temp32a);
4630 temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4631 temp32alen, temp32a, temp48);
4632 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4634 finswap = finnow; finnow = finother; finother = finswap;
4636 if (cdxtail != 0.0) {
4637 cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);
4638 temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx,
4641 cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);
4642 temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);
4644 cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);
4645 temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);
4647 temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4648 temp16blen, temp16b, temp32a);
4649 temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4650 temp32alen, temp32a, temp48);
4651 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4653 finswap = finnow; finnow = finother; finother = finswap;
4655 if (cdytail != 0.0) {
4656 cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);
4657 temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy,
4660 cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);
4661 temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);
4663 cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);
4664 temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);
4666 temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4667 temp16blen, temp16b, temp32a);
4668 temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
4669 temp32alen, temp32a, temp48);
4670 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4672 finswap = finnow; finnow = finother; finother = finswap;
4675 if ((adxtail != 0.0) || (adytail != 0.0)) {
4676 if ((bdxtail != 0.0) || (bdytail != 0.0)
4677 || (cdxtail != 0.0) || (cdytail != 0.0)) {
4678 Two_Product(bdxtail, cdy, ti1, ti0);
4679 Two_Product(bdx, cdytail, tj1, tj0);
4680 Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
4683 Two_Product(cdxtail, negate, ti1, ti0);
4685 Two_Product(cdx, negate, tj1, tj0);
4686 Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4688 bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);
4690 Two_Product(bdxtail, cdytail, ti1, ti0);
4691 Two_Product(cdxtail, bdytail, tj1, tj0);
4692 Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);
4702 if (adxtail != 0.0) {
4703 temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, adxtail, temp16a);
4704 axtbctlen = scale_expansion_zeroelim(bctlen, bct, adxtail, axtbct);
4705 temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, 2.0 * adx,
4707 temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4708 temp32alen, temp32a, temp48);
4709 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4711 finswap = finnow; finnow = finother; finother = finswap;
4712 if (bdytail != 0.0) {
4713 temp8len = scale_expansion_zeroelim(4, cc, adxtail, temp8);
4714 temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
4716 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4718 finswap = finnow; finnow = finother; finother = finswap;
4720 if (cdytail != 0.0) {
4721 temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);
4722 temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
4724 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4726 finswap = finnow; finnow = finother; finother = finswap;
4729 temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail,
4731 axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);
4732 temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx,
4734 temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail,
4736 temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4737 temp16blen, temp16b, temp32b);
4738 temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4739 temp32blen, temp32b, temp64);
4740 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4742 finswap = finnow; finnow = finother; finother = finswap;
4744 if (adytail != 0.0) {
4745 temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, adytail, temp16a);
4746 aytbctlen = scale_expansion_zeroelim(bctlen, bct, adytail, aytbct);
4747 temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, 2.0 * ady,
4749 temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4750 temp32alen, temp32a, temp48);
4751 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4753 finswap = finnow; finnow = finother; finother = finswap;
4756 temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail,
4758 aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);
4759 temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady,
4761 temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail,
4763 temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4764 temp16blen, temp16b, temp32b);
4765 temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4766 temp32blen, temp32b, temp64);
4767 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4769 finswap = finnow; finnow = finother; finother = finswap;
4772 if ((bdxtail != 0.0) || (bdytail != 0.0)) {
4773 if ((cdxtail != 0.0) || (cdytail != 0.0)
4774 || (adxtail != 0.0) || (adytail != 0.0)) {
4775 Two_Product(cdxtail, ady, ti1, ti0);
4776 Two_Product(cdx, adytail, tj1, tj0);
4777 Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
4780 Two_Product(adxtail, negate, ti1, ti0);
4782 Two_Product(adx, negate, tj1, tj0);
4783 Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4785 catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);
4787 Two_Product(cdxtail, adytail, ti1, ti0);
4788 Two_Product(adxtail, cdytail, tj1, tj0);
4789 Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);
4799 if (bdxtail != 0.0) {
4800 temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, bdxtail, temp16a);
4801 bxtcatlen = scale_expansion_zeroelim(catlen, cat, bdxtail, bxtcat);
4802 temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, 2.0 * bdx,
4804 temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4805 temp32alen, temp32a, temp48);
4806 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4808 finswap = finnow; finnow = finother; finother = finswap;
4809 if (cdytail != 0.0) {
4810 temp8len = scale_expansion_zeroelim(4, aa, bdxtail, temp8);
4811 temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
4813 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4815 finswap = finnow; finnow = finother; finother = finswap;
4817 if (adytail != 0.0) {
4818 temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);
4819 temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
4821 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4823 finswap = finnow; finnow = finother; finother = finswap;
4826 temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail,
4828 bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);
4829 temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx,
4831 temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail,
4833 temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4834 temp16blen, temp16b, temp32b);
4835 temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4836 temp32blen, temp32b, temp64);
4837 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4839 finswap = finnow; finnow = finother; finother = finswap;
4841 if (bdytail != 0.0) {
4842 temp16alen = scale_expansion_zeroelim(bytcalen, bytca, bdytail, temp16a);
4843 bytcatlen = scale_expansion_zeroelim(catlen, cat, bdytail, bytcat);
4844 temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, 2.0 * bdy,
4846 temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4847 temp32alen, temp32a, temp48);
4848 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4850 finswap = finnow; finnow = finother; finother = finswap;
4853 temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail,
4855 bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);
4856 temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy,
4858 temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail,
4860 temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4861 temp16blen, temp16b, temp32b);
4862 temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4863 temp32blen, temp32b, temp64);
4864 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4866 finswap = finnow; finnow = finother; finother = finswap;
4869 if ((cdxtail != 0.0) || (cdytail != 0.0)) {
4870 if ((adxtail != 0.0) || (adytail != 0.0)
4871 || (bdxtail != 0.0) || (bdytail != 0.0)) {
4872 Two_Product(adxtail, bdy, ti1, ti0);
4873 Two_Product(adx, bdytail, tj1, tj0);
4874 Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
4877 Two_Product(bdxtail, negate, ti1, ti0);
4879 Two_Product(bdx, negate, tj1, tj0);
4880 Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
4882 abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);
4884 Two_Product(adxtail, bdytail, ti1, ti0);
4885 Two_Product(bdxtail, adytail, tj1, tj0);
4886 Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);
4896 if (cdxtail != 0.0) {
4897 temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, cdxtail, temp16a);
4898 cxtabtlen = scale_expansion_zeroelim(abtlen, abt, cdxtail, cxtabt);
4899 temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, 2.0 * cdx,
4901 temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4902 temp32alen, temp32a, temp48);
4903 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4905 finswap = finnow; finnow = finother; finother = finswap;
4906 if (adytail != 0.0) {
4907 temp8len = scale_expansion_zeroelim(4, bb, cdxtail, temp8);
4908 temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
4910 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4912 finswap = finnow; finnow = finother; finother = finswap;
4914 if (bdytail != 0.0) {
4915 temp8len = scale_expansion_zeroelim(4, aa, -cdxtail, temp8);
4916 temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
4918 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
4920 finswap = finnow; finnow = finother; finother = finswap;
4923 temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, cdxtail,
4925 cxtabttlen = scale_expansion_zeroelim(abttlen, abtt, cdxtail, cxtabtt);
4926 temp16alen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, 2.0 * cdx,
4928 temp16blen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, cdxtail,
4930 temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4931 temp16blen, temp16b, temp32b);
4932 temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4933 temp32blen, temp32b, temp64);
4934 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4936 finswap = finnow; finnow = finother; finother = finswap;
4938 if (cdytail != 0.0) {
4939 temp16alen = scale_expansion_zeroelim(cytablen, cytab, cdytail, temp16a);
4940 cytabtlen = scale_expansion_zeroelim(abtlen, abt, cdytail, cytabt);
4941 temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, 2.0 * cdy,
4943 temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4944 temp32alen, temp32a, temp48);
4945 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
4947 finswap = finnow; finnow = finother; finother = finswap;
4950 temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, cdytail,
4952 cytabttlen = scale_expansion_zeroelim(abttlen, abtt, cdytail, cytabtt);
4953 temp16alen = scale_expansion_zeroelim(cytabttlen, cytabtt, 2.0 * cdy,
4955 temp16blen = scale_expansion_zeroelim(cytabttlen, cytabtt, cdytail,
4957 temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
4958 temp16blen, temp16b, temp32b);
4959 temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
4960 temp32blen, temp32b, temp64);
4961 finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
4963 finswap = finnow; finnow = finother; finother = finswap;
4967 return finnow[finlength - 1];
4970 REAL incircle(pa, pb, pc, pd)
4976 REAL adx, bdx, cdx, ady, bdy, cdy;
4977 REAL bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
4978 REAL alift, blift, clift;
4980 REAL permanent, errbound;
4984 adx = pa[0] - pd[0];
4985 bdx = pb[0] - pd[0];
4986 cdx = pc[0] - pd[0];
4987 ady = pa[1] - pd[1];
4988 bdy = pb[1] - pd[1];
4989 cdy = pc[1] - pd[1];
4993 alift = adx * adx + ady * ady;
4997 blift = bdx * bdx + bdy * bdy;
5001 clift = cdx * cdx + cdy * cdy;
5003 det = alift * (bdxcdy - cdxbdy)
5004 + blift * (cdxady - adxcdy)
5005 + clift * (adxbdy - bdxady);
5011 permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * alift
5012 + (Absolute(cdxady) + Absolute(adxcdy)) * blift
5013 + (Absolute(adxbdy) + Absolute(bdxady)) * clift;
5014 errbound = iccerrboundA * permanent;
5015 if ((det > errbound) || (-det > errbound)) {
5019 return incircleadapt(pa, pb, pc, pd, permanent);
5024 /********* Determinant evaluation routines end here *********/
5026 /*****************************************************************************/
5028 /* triangleinit() Initialize some variables. */
5030 /*****************************************************************************/
5034 points.maxitems = triangles.maxitems = shelles.maxitems = viri.maxitems =
5035 badsegments.maxitems = badtriangles.maxitems = splaynodes.maxitems = 0l;
5036 points.itembytes = triangles.itembytes = shelles.itembytes = viri.itembytes =
5037 badsegments.itembytes = badtriangles.itembytes = splaynodes.itembytes = 0;
5038 recenttri.tri = (triangle *) NULL; /* No triangle has been visited yet. */
5039 samples = 1; /* Point location should take at least one sample. */
5040 checksegments = 0; /* There are no segments in the triangulation yet. */
5041 incirclecount = counterclockcount = hyperbolacount = 0;
5042 circumcentercount = circletopcount = 0;
5045 exactinit(); /* Initialize exact arithmetic constants. */
5048 /*****************************************************************************/
5050 /* randomnation() Generate a random number between 0 and `choices' - 1. */
5052 /* This is a simple linear congruential random number generator. Hence, it */
5053 /* is a bad random number generator, but good enough for most randomized */
5054 /* geometric algorithms. */
5056 /*****************************************************************************/
5058 unsigned long randomnation(choices)
5059 unsigned int choices;
5061 randomseed = (randomseed * 1366l + 150889l) % 714025l;
5062 return randomseed / (714025l / choices + 1);
5065 /********* Mesh quality testing routines begin here *********/
5069 /*****************************************************************************/
5071 /* checkmesh() Test the mesh for topological consistency. */
5073 /*****************************************************************************/
5079 struct triedge triangleloop;
5080 struct triedge oppotri, oppooppotri;
5081 point triorg, tridest, triapex;
5082 point oppoorg, oppodest;
5085 triangle ptr; /* Temporary variable used by sym(). */
5087 /* Temporarily turn on exact arithmetic if it's off. */
5088 saveexact = noexact;
5091 printf(" Checking consistency of mesh...\n");
5094 /* Run through the list of triangles, checking each one. */
5095 traversalinit(&triangles);
5096 triangleloop.tri = triangletraverse();
5097 while (triangleloop.tri != (triangle *) NULL) {
5098 /* Check all three edges of the triangle. */
5099 for (triangleloop.orient = 0; triangleloop.orient < 3;
5100 triangleloop.orient++) {
5101 org(triangleloop, triorg);
5102 dest(triangleloop, tridest);
5103 if (triangleloop.orient == 0) { /* Only test for inversion once. */
5104 /* Test if the triangle is flat or inverted. */
5105 apex(triangleloop, triapex);
5106 if (counterclockwise(triorg, tridest, triapex) <= 0.0) {
5107 printf(" !! !! Inverted ");
5108 printtriangle(&triangleloop);
5112 /* Find the neighboring triangle on this edge. */
5113 sym(triangleloop, oppotri);
5114 if (oppotri.tri != dummytri) {
5115 /* Check that the triangle's neighbor knows it's a neighbor. */
5116 sym(oppotri, oppooppotri);
5117 if ((triangleloop.tri != oppooppotri.tri)
5118 || (triangleloop.orient != oppooppotri.orient)) {
5119 printf(" !! !! Asymmetric triangle-triangle bond:\n");
5120 if (triangleloop.tri == oppooppotri.tri) {
5121 printf(" (Right triangle, wrong orientation)\n");
5124 printtriangle(&triangleloop);
5125 printf(" Second (nonreciprocating) ");
5126 printtriangle(&oppotri);
5129 /* Check that both triangles agree on the identities */
5130 /* of their shared vertices. */
5131 org(oppotri, oppoorg);
5132 dest(oppotri, oppodest);
5133 if ((triorg != oppodest) || (tridest != oppoorg)) {
5134 printf(" !! !! Mismatched edge coordinates between two triangles:\n"
5136 printf(" First mismatched ");
5137 printtriangle(&triangleloop);
5138 printf(" Second mismatched ");
5139 printtriangle(&oppotri);
5144 triangleloop.tri = triangletraverse();
5148 printf(" In my studied opinion, the mesh appears to be consistent.\n");
5150 } else if (horrors == 1) {
5151 printf(" !! !! !! !! Precisely one festering wound discovered.\n");
5153 printf(" !! !! !! !! %d abominations witnessed.\n", horrors);
5155 /* Restore the status of exact arithmetic. */
5156 noexact = saveexact;
5159 #endif /* not REDUCED */
5161 /*****************************************************************************/
5163 /* checkdelaunay() Ensure that the mesh is (constrained) Delaunay. */
5165 /*****************************************************************************/
5169 void checkdelaunay()
5171 struct triedge triangleloop;
5172 struct triedge oppotri;
5173 struct edge opposhelle;
5174 point triorg, tridest, triapex;
5176 int shouldbedelaunay;
5179 triangle ptr; /* Temporary variable used by sym(). */
5180 shelle sptr; /* Temporary variable used by tspivot(). */
5182 /* Temporarily turn on exact arithmetic if it's off. */
5183 saveexact = noexact;
5186 printf(" Checking Delaunay property of mesh...\n");
5189 /* Run through the list of triangles, checking each one. */
5190 traversalinit(&triangles);
5191 triangleloop.tri = triangletraverse();
5192 while (triangleloop.tri != (triangle *) NULL) {
5193 /* Check all three edges of the triangle. */
5194 for (triangleloop.orient = 0; triangleloop.orient < 3;
5195 triangleloop.orient++) {
5196 org(triangleloop, triorg);
5197 dest(triangleloop, tridest);
5198 apex(triangleloop, triapex);
5199 sym(triangleloop, oppotri);
5200 apex(oppotri, oppoapex);
5201 /* Only test that the edge is locally Delaunay if there is an */
5202 /* adjoining triangle whose pointer is larger (to ensure that */
5203 /* each pair isn't tested twice). */
5204 shouldbedelaunay = (oppotri.tri != dummytri)
5205 && (triapex != (point) NULL) && (oppoapex != (point) NULL)
5206 && (triangleloop.tri < oppotri.tri);
5207 if (checksegments && shouldbedelaunay) {
5208 /* If a shell edge separates the triangles, then the edge is */
5209 /* constrained, so no local Delaunay test should be done. */
5210 tspivot(triangleloop, opposhelle);
5211 if (opposhelle.sh != dummysh){
5212 shouldbedelaunay = 0;
5215 if (shouldbedelaunay) {
5216 if (incircle(triorg, tridest, triapex, oppoapex) > 0.0) {
5217 printf(" !! !! Non-Delaunay pair of triangles:\n");
5218 printf(" First non-Delaunay ");
5219 printtriangle(&triangleloop);
5220 printf(" Second non-Delaunay ");
5221 printtriangle(&oppotri);
5226 triangleloop.tri = triangletraverse();
5231 " By virtue of my perceptive intelligence, I declare the mesh Delaunay.\n");
5233 } else if (horrors == 1) {
5235 " !! !! !! !! Precisely one terrifying transgression identified.\n");
5237 printf(" !! !! !! !! %d obscenities viewed with horror.\n", horrors);
5239 /* Restore the status of exact arithmetic. */
5240 noexact = saveexact;
5243 #endif /* not REDUCED */
5245 /*****************************************************************************/
5247 /* enqueuebadtri() Add a bad triangle to the end of a queue. */
5249 /* The queue is actually a set of 64 queues. I use multiple queues to give */
5250 /* priority to smaller angles. I originally implemented a heap, but the */
5251 /* queues are (to my surprise) much faster. */
5253 /*****************************************************************************/
5257 void enqueuebadtri(instri, angle, insapex, insorg, insdest)
5258 struct triedge *instri;
5264 struct badface *newface;
5268 printf(" Queueing bad triangle:\n");
5269 printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", insorg[0],
5270 insorg[1], insdest[0], insdest[1], insapex[0], insapex[1]);
5272 /* Allocate space for the bad triangle. */
5273 newface = (struct badface *) poolalloc(&badtriangles);
5274 triedgecopy(*instri, newface->badfacetri);
5275 newface->key = angle;
5276 newface->faceapex = insapex;
5277 newface->faceorg = insorg;
5278 newface->facedest = insdest;
5279 newface->nextface = (struct badface *) NULL;
5280 /* Determine the appropriate queue to put the bad triangle into. */
5282 queuenumber = (int) (160.0 * (angle - 0.6));
5283 if (queuenumber > 63) {
5287 /* It's not a bad angle; put the triangle in the lowest-priority queue. */
5290 /* Add the triangle to the end of a queue. */
5291 *queuetail[queuenumber] = newface;
5292 /* Maintain a pointer to the NULL pointer at the end of the queue. */
5293 queuetail[queuenumber] = &newface->nextface;
5296 #endif /* not CDT_ONLY */
5298 /*****************************************************************************/
5300 /* dequeuebadtri() Remove a triangle from the front of the queue. */
5302 /*****************************************************************************/
5306 struct badface *dequeuebadtri()
5308 struct badface *result;
5311 /* Look for a nonempty queue. */
5312 for (queuenumber = 63; queuenumber >= 0; queuenumber--) {
5313 result = queuefront[queuenumber];
5314 if (result != (struct badface *) NULL) {
5315 /* Remove the triangle from the queue. */
5316 queuefront[queuenumber] = result->nextface;
5317 /* Maintain a pointer to the NULL pointer at the end of the queue. */
5318 if (queuefront[queuenumber] == (struct badface *) NULL) {
5319 queuetail[queuenumber] = &queuefront[queuenumber];
5324 return (struct badface *) NULL;
5327 #endif /* not CDT_ONLY */
5329 /*****************************************************************************/
5331 /* checkedge4encroach() Check a segment to see if it is encroached; add */
5332 /* it to the list if it is. */
5334 /* An encroached segment is an unflippable edge that has a point in its */
5335 /* diametral circle (that is, it faces an angle greater than 90 degrees). */
5336 /* This definition is due to Ruppert. */
5338 /* Returns a nonzero value if the edge is encroached. */
5340 /*****************************************************************************/
5344 int checkedge4encroach(testedge)
5345 struct edge *testedge;
5347 struct triedge neighbortri;
5348 struct edge testsym;
5349 struct edge *badedge;
5352 point eorg, edest, eapex;
5353 triangle ptr; /* Temporary variable used by stpivot(). */
5358 sorg(*testedge, eorg);
5359 sdest(*testedge, edest);
5360 /* Check one neighbor of the shell edge. */
5361 stpivot(*testedge, neighbortri);
5362 /* Does the neighbor exist, or is this a boundary edge? */
5363 if (neighbortri.tri != dummytri) {
5365 /* Find a vertex opposite this edge. */
5366 apex(neighbortri, eapex);
5367 /* Check whether the vertex is inside the diametral circle of the */
5368 /* shell edge. Pythagoras' Theorem is used to check whether the */
5369 /* angle at the vertex is greater than 90 degrees. */
5370 if (eapex[0] * (eorg[0] + edest[0]) + eapex[1] * (eorg[1] + edest[1]) >
5371 eapex[0] * eapex[0] + eorg[0] * edest[0] +
5372 eapex[1] * eapex[1] + eorg[1] * edest[1]) {
5376 /* Check the other neighbor of the shell edge. */
5377 ssym(*testedge, testsym);
5378 stpivot(testsym, neighbortri);
5379 /* Does the neighbor exist, or is this a boundary edge? */
5380 if (neighbortri.tri != dummytri) {
5382 /* Find the other vertex opposite this edge. */
5383 apex(neighbortri, eapex);
5384 /* Check whether the vertex is inside the diametral circle of the */
5385 /* shell edge. Pythagoras' Theorem is used to check whether the */
5386 /* angle at the vertex is greater than 90 degrees. */
5387 if (eapex[0] * (eorg[0] + edest[0]) +
5388 eapex[1] * (eorg[1] + edest[1]) >
5389 eapex[0] * eapex[0] + eorg[0] * edest[0] +
5390 eapex[1] * eapex[1] + eorg[1] * edest[1]) {
5395 if (addtolist && (!nobisect || ((nobisect == 1) && (sides == 2)))) {
5397 printf(" Queueing encroached segment (%.12g, %.12g) (%.12g, %.12g).\n",
5398 eorg[0], eorg[1], edest[0], edest[1]);
5400 /* Add the shell edge to the list of encroached segments. */
5401 /* Be sure to get the orientation right. */
5402 badedge = (struct edge *) poolalloc(&badsegments);
5403 if (addtolist == 1) {
5404 shellecopy(*testedge, *badedge);
5406 shellecopy(testsym, *badedge);
5412 #endif /* not CDT_ONLY */
5414 /*****************************************************************************/
5416 /* testtriangle() Test a face for quality measures. */
5418 /* Tests a triangle to see if it satisfies the minimum angle condition and */
5419 /* the maximum area condition. Triangles that aren't up to spec are added */
5420 /* to the bad triangle queue. */
5422 /*****************************************************************************/
5426 void testtriangle(testtri)
5427 struct triedge *testtri;
5429 struct triedge sametesttri;
5430 struct edge edge1, edge2;
5431 point torg, tdest, tapex;
5433 REAL dxod, dyod, dxda, dyda, dxao, dyao;
5434 REAL dxod2, dyod2, dxda2, dyda2, dxao2, dyao2;
5435 REAL apexlen, orglen, destlen;
5438 shelle sptr; /* Temporary variable used by tspivot(). */
5440 org(*testtri, torg);
5441 dest(*testtri, tdest);
5442 apex(*testtri, tapex);
5443 dxod = torg[0] - tdest[0];
5444 dyod = torg[1] - tdest[1];
5445 dxda = tdest[0] - tapex[0];
5446 dyda = tdest[1] - tapex[1];
5447 dxao = tapex[0] - torg[0];
5448 dyao = tapex[1] - torg[1];
5449 dxod2 = dxod * dxod;
5450 dyod2 = dyod * dyod;
5451 dxda2 = dxda * dxda;
5452 dyda2 = dyda * dyda;
5453 dxao2 = dxao * dxao;
5454 dyao2 = dyao * dyao;
5455 /* Find the lengths of the triangle's three edges. */
5456 apexlen = dxod2 + dyod2;
5457 orglen = dxda2 + dyda2;
5458 destlen = dxao2 + dyao2;
5459 if ((apexlen < orglen) && (apexlen < destlen)) {
5460 /* The edge opposite the apex is shortest. */
5461 /* Find the square of the cosine of the angle at the apex. */
5462 angle = dxda * dxao + dyda * dyao;
5463 angle = angle * angle / (orglen * destlen);
5464 anglevertex = tapex;
5465 lnext(*testtri, sametesttri);
5466 tspivot(sametesttri, edge1);
5467 lnextself(sametesttri);
5468 tspivot(sametesttri, edge2);
5469 } else if (orglen < destlen) {
5470 /* The edge opposite the origin is shortest. */
5471 /* Find the square of the cosine of the angle at the origin. */
5472 angle = dxod * dxao + dyod * dyao;
5473 angle = angle * angle / (apexlen * destlen);
5475 tspivot(*testtri, edge1);
5476 lprev(*testtri, sametesttri);
5477 tspivot(sametesttri, edge2);
5479 /* The edge opposite the destination is shortest. */
5480 /* Find the square of the cosine of the angle at the destination. */
5481 angle = dxod * dxda + dyod * dyda;
5482 angle = angle * angle / (apexlen * orglen);
5483 anglevertex = tdest;
5484 tspivot(*testtri, edge1);
5485 lnext(*testtri, sametesttri);
5486 tspivot(sametesttri, edge2);
5488 /* Check if both edges that form the angle are segments. */
5489 if ((edge1.sh != dummysh) && (edge2.sh != dummysh)) {
5490 /* The angle is a segment intersection. */
5491 if ((angle > 0.9924) && !quiet) { /* Roughly 5 degrees. */
5493 /* Beware of a floating exception in acos(). */
5496 /* Find the actual angle in degrees, for printing. */
5497 angle = acos(sqrt(angle)) * (180.0 / PI);
5499 "Warning: Small angle (%.4g degrees) between segments at point\n",
5501 printf(" (%.12g, %.12g)\n", anglevertex[0], anglevertex[1]);
5503 /* Don't add this bad triangle to the list; there's nothing that */
5504 /* can be done about a small angle between two segments. */
5507 /* Check whether the angle is smaller than permitted. */
5508 if (angle > goodangle) {
5509 /* Add this triangle to the list of bad triangles. */
5510 enqueuebadtri(testtri, angle, tapex, torg, tdest);
5513 if (vararea || fixedarea) {
5514 /* Check whether the area is larger than permitted. */
5515 area = 0.5 * (dxod * dyda - dyod * dxda);
5516 if (fixedarea && (area > maxarea)) {
5517 /* Add this triangle to the list of bad triangles. */
5518 enqueuebadtri(testtri, angle, tapex, torg, tdest);
5519 } else if (vararea) {
5520 /* Nonpositive area constraints are treated as unconstrained. */
5521 if ((area > areabound(*testtri)) && (areabound(*testtri) > 0.0)) {
5522 /* Add this triangle to the list of bad triangles. */
5523 enqueuebadtri(testtri, angle, tapex, torg, tdest);
5529 #endif /* not CDT_ONLY */
5533 /********* Mesh quality testing routines end here *********/
5535 /********* Point location routines begin here *********/
5539 /*****************************************************************************/
5541 /* makepointmap() Construct a mapping from points to triangles to improve */
5542 /* the speed of point location for segment insertion. */
5544 /* Traverses all the triangles, and provides each corner of each triangle */
5545 /* with a pointer to that triangle. Of course, pointers will be */
5546 /* overwritten by other pointers because (almost) each point is a corner */
5547 /* of several triangles, but in the end every point will point to some */
5548 /* triangle that contains it. */
5550 /*****************************************************************************/
5554 struct triedge triangleloop;
5558 printf(" Constructing mapping from points to triangles.\n");
5560 traversalinit(&triangles);
5561 triangleloop.tri = triangletraverse();
5562 while (triangleloop.tri != (triangle *) NULL) {
5563 /* Check all three points of the triangle. */
5564 for (triangleloop.orient = 0; triangleloop.orient < 3;
5565 triangleloop.orient++) {
5566 org(triangleloop, triorg);
5567 setpoint2tri(triorg, encode(triangleloop));
5569 triangleloop.tri = triangletraverse();
5573 /*****************************************************************************/
5575 /* preciselocate() Find a triangle or edge containing a given point. */
5577 /* Begins its search from `searchtri'. It is important that `searchtri' */
5578 /* be a handle with the property that `searchpoint' is strictly to the left */
5579 /* of the edge denoted by `searchtri', or is collinear with that edge and */
5580 /* does not intersect that edge. (In particular, `searchpoint' should not */
5581 /* be the origin or destination of that edge.) */
5583 /* These conditions are imposed because preciselocate() is normally used in */
5584 /* one of two situations: */
5586 /* (1) To try to find the location to insert a new point. Normally, we */
5587 /* know an edge that the point is strictly to the left of. In the */
5588 /* incremental Delaunay algorithm, that edge is a bounding box edge. */
5589 /* In Ruppert's Delaunay refinement algorithm for quality meshing, */
5590 /* that edge is the shortest edge of the triangle whose circumcenter */
5591 /* is being inserted. */
5593 /* (2) To try to find an existing point. In this case, any edge on the */
5594 /* convex hull is a good starting edge. The possibility that the */
5595 /* vertex one seeks is an endpoint of the starting edge must be */
5596 /* screened out before preciselocate() is called. */
5598 /* On completion, `searchtri' is a triangle that contains `searchpoint'. */
5600 /* This implementation differs from that given by Guibas and Stolfi. It */
5601 /* walks from triangle to triangle, crossing an edge only if `searchpoint' */
5602 /* is on the other side of the line containing that edge. After entering */
5603 /* a triangle, there are two edges by which one can leave that triangle. */
5604 /* If both edges are valid (`searchpoint' is on the other side of both */
5605 /* edges), one of the two is chosen by drawing a line perpendicular to */
5606 /* the entry edge (whose endpoints are `forg' and `fdest') passing through */
5607 /* `fapex'. Depending on which side of this perpendicular `searchpoint' */
5608 /* falls on, an exit edge is chosen. */
5610 /* This implementation is empirically faster than the Guibas and Stolfi */
5611 /* point location routine (which I originally used), which tends to spiral */
5612 /* in toward its target. */
5614 /* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */
5615 /* is a handle whose origin is the existing vertex. */
5617 /* Returns ONEDGE if the point lies on a mesh edge. `searchtri' is a */
5618 /* handle whose primary edge is the edge on which the point lies. */
5620 /* Returns INTRIANGLE if the point lies strictly within a triangle. */
5621 /* `searchtri' is a handle on the triangle that contains the point. */
5623 /* Returns OUTSIDE if the point lies outside the mesh. `searchtri' is a */
5624 /* handle whose primary edge the point is to the right of. This might */
5625 /* occur when the circumcenter of a triangle falls just slightly outside */
5626 /* the mesh due to floating-point roundoff error. It also occurs when */
5627 /* seeking a hole or region point that a foolish user has placed outside */
5630 /* WARNING: This routine is designed for convex triangulations, and will */
5631 /* not generally work after the holes and concavities have been carved. */
5632 /* However, it can still be used to find the circumcenter of a triangle, as */
5633 /* long as the search is begun from the triangle in question. */
5635 /*****************************************************************************/
5637 enum locateresult preciselocate(searchpoint, searchtri)
5639 struct triedge *searchtri;
5641 struct triedge backtracktri;
5642 point forg, fdest, fapex;
5644 REAL orgorient, destorient;
5646 triangle ptr; /* Temporary variable used by sym(). */
5649 printf(" Searching for point (%.12g, %.12g).\n",
5650 searchpoint[0], searchpoint[1]);
5653 org(*searchtri, forg);
5654 dest(*searchtri, fdest);
5655 apex(*searchtri, fapex);
5658 printf(" At (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
5659 forg[0], forg[1], fdest[0], fdest[1], fapex[0], fapex[1]);
5661 /* Check whether the apex is the point we seek. */
5662 if ((fapex[0] == searchpoint[0]) && (fapex[1] == searchpoint[1])) {
5663 lprevself(*searchtri);
5666 /* Does the point lie on the other side of the line defined by the */
5667 /* triangle edge opposite the triangle's destination? */
5668 destorient = counterclockwise(forg, fapex, searchpoint);
5669 /* Does the point lie on the other side of the line defined by the */
5670 /* triangle edge opposite the triangle's origin? */
5671 orgorient = counterclockwise(fapex, fdest, searchpoint);
5672 if (destorient > 0.0) {
5673 if (orgorient > 0.0) {
5674 /* Move left if the inner product of (fapex - searchpoint) and */
5675 /* (fdest - forg) is positive. This is equivalent to drawing */
5676 /* a line perpendicular to the line (forg, fdest) passing */
5677 /* through `fapex', and determining which side of this line */
5678 /* `searchpoint' falls on. */
5679 moveleft = (fapex[0] - searchpoint[0]) * (fdest[0] - forg[0]) +
5680 (fapex[1] - searchpoint[1]) * (fdest[1] - forg[1]) > 0.0;
5685 if (orgorient > 0.0) {
5688 /* The point we seek must be on the boundary of or inside this */
5690 if (destorient == 0.0) {
5691 lprevself(*searchtri);
5694 if (orgorient == 0.0) {
5695 lnextself(*searchtri);
5702 /* Move to another triangle. Leave a trace `backtracktri' in case */
5703 /* floating-point roundoff or some such bogey causes us to walk */
5704 /* off a boundary of the triangulation. We can just bounce off */
5705 /* the boundary as if it were an elastic band. */
5707 lprev(*searchtri, backtracktri);
5710 lnext(*searchtri, backtracktri);
5713 sym(backtracktri, *searchtri);
5715 /* Check for walking off the edge. */
5716 if (searchtri->tri == dummytri) {
5718 triedgecopy(backtracktri, *searchtri);
5722 apex(*searchtri, fapex);
5723 /* Check if the point really is beyond the triangulation boundary. */
5724 destorient = counterclockwise(forg, fapex, searchpoint);
5725 orgorient = counterclockwise(fapex, fdest, searchpoint);
5726 if ((orgorient < 0.0) && (destorient < 0.0)) {
5730 apex(*searchtri, fapex);
5735 /*****************************************************************************/
5737 /* locate() Find a triangle or edge containing a given point. */
5739 /* Searching begins from one of: the input `searchtri', a recently */
5740 /* encountered triangle `recenttri', or from a triangle chosen from a */
5741 /* random sample. The choice is made by determining which triangle's */
5742 /* origin is closest to the point we are searcing for. Normally, */
5743 /* `searchtri' should be a handle on the convex hull of the triangulation. */
5745 /* Details on the random sampling method can be found in the Mucke, Saias, */
5746 /* and Zhu paper cited in the header of this code. */
5748 /* On completion, `searchtri' is a triangle that contains `searchpoint'. */
5750 /* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */
5751 /* is a handle whose origin is the existing vertex. */
5753 /* Returns ONEDGE if the point lies on a mesh edge. `searchtri' is a */
5754 /* handle whose primary edge is the edge on which the point lies. */
5756 /* Returns INTRIANGLE if the point lies strictly within a triangle. */
5757 /* `searchtri' is a handle on the triangle that contains the point. */
5759 /* Returns OUTSIDE if the point lies outside the mesh. `searchtri' is a */
5760 /* handle whose primary edge the point is to the right of. This might */
5761 /* occur when the circumcenter of a triangle falls just slightly outside */
5762 /* the mesh due to floating-point roundoff error. It also occurs when */
5763 /* seeking a hole or region point that a foolish user has placed outside */
5766 /* WARNING: This routine is designed for convex triangulations, and will */
5767 /* not generally work after the holes and concavities have been carved. */
5769 /*****************************************************************************/
5771 enum locateresult locate(searchpoint, searchtri)
5773 struct triedge *searchtri;
5777 struct triedge sampletri;
5779 unsigned long alignptr;
5780 REAL searchdist, dist;
5782 long sampleblocks, samplesperblock, samplenum;
5785 triangle ptr; /* Temporary variable used by sym(). */
5788 printf(" Randomly sampling for a triangle near point (%.12g, %.12g).\n",
5789 searchpoint[0], searchpoint[1]);
5791 /* Record the distance from the suggested starting triangle to the */
5792 /* point we seek. */
5793 org(*searchtri, torg);
5794 searchdist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
5795 + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
5797 printf(" Boundary triangle has origin (%.12g, %.12g).\n",
5801 /* If a recently encountered triangle has been recorded and has not been */
5802 /* deallocated, test it as a good starting point. */
5803 if (recenttri.tri != (triangle *) NULL) {
5804 if (recenttri.tri[3] != (triangle) NULL) {
5805 org(recenttri, torg);
5806 if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
5807 triedgecopy(recenttri, *searchtri);
5810 dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
5811 + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
5812 if (dist < searchdist) {
5813 triedgecopy(recenttri, *searchtri);
5816 printf(" Choosing recent triangle with origin (%.12g, %.12g).\n",
5823 /* The number of random samples taken is proportional to the cube root of */
5824 /* the number of triangles in the mesh. The next bit of code assumes */
5825 /* that the number of triangles increases monotonically. */
5826 while (SAMPLEFACTOR * samples * samples * samples < triangles.items) {
5829 triblocks = (triangles.maxitems + TRIPERBLOCK - 1) / TRIPERBLOCK;
5830 samplesperblock = 1 + (samples / triblocks);
5831 sampleblocks = samples / samplesperblock;
5832 sampleblock = triangles.firstblock;
5833 sampletri.orient = 0;
5834 for (i = 0; i < sampleblocks; i++) {
5835 alignptr = (unsigned long) (sampleblock + 1);
5836 firsttri = (triangle *) (alignptr + (unsigned long) triangles.alignbytes
5837 - (alignptr % (unsigned long) triangles.alignbytes));
5838 for (j = 0; j < samplesperblock; j++) {
5839 if (i == triblocks - 1) {
5840 samplenum = randomnation((int)
5841 (triangles.maxitems - (i * TRIPERBLOCK)));
5843 samplenum = randomnation(TRIPERBLOCK);
5845 sampletri.tri = (triangle *)
5846 (firsttri + (samplenum * triangles.itemwords));
5847 if (sampletri.tri[3] != (triangle) NULL) {
5848 org(sampletri, torg);
5849 dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
5850 + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
5851 if (dist < searchdist) {
5852 triedgecopy(sampletri, *searchtri);
5855 printf(" Choosing triangle with origin (%.12g, %.12g).\n",
5861 sampleblock = (VOID **) *sampleblock;
5864 org(*searchtri, torg);
5865 dest(*searchtri, tdest);
5866 /* Check the starting triangle's vertices. */
5867 if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
5870 if ((tdest[0] == searchpoint[0]) && (tdest[1] == searchpoint[1])) {
5871 lnextself(*searchtri);
5874 /* Orient `searchtri' to fit the preconditions of calling preciselocate(). */
5875 ahead = counterclockwise(torg, tdest, searchpoint);
5877 /* Turn around so that `searchpoint' is to the left of the */
5878 /* edge specified by `searchtri'. */
5879 symself(*searchtri);
5880 } else if (ahead == 0.0) {
5881 /* Check if `searchpoint' is between `torg' and `tdest'. */
5882 if (((torg[0] < searchpoint[0]) == (searchpoint[0] < tdest[0]))
5883 && ((torg[1] < searchpoint[1]) == (searchpoint[1] < tdest[1]))) {
5887 return preciselocate(searchpoint, searchtri);
5892 /********* Point location routines end here *********/
5894 /********* Mesh transformation routines begin here *********/
5898 /*****************************************************************************/
5900 /* insertshelle() Create a new shell edge and insert it between two */
5903 /* The new shell edge is inserted at the edge described by the handle */
5904 /* `tri'. Its vertices are properly initialized. The marker `shellemark' */
5905 /* is applied to the shell edge and, if appropriate, its vertices. */
5907 /*****************************************************************************/
5909 void insertshelle(tri, shellemark)
5910 struct triedge *tri; /* Edge at which to insert the new shell edge. */
5911 int shellemark; /* Marker for the new shell edge. */
5913 struct triedge oppotri;
5914 struct edge newshelle;
5915 point triorg, tridest;
5916 triangle ptr; /* Temporary variable used by sym(). */
5917 shelle sptr; /* Temporary variable used by tspivot(). */
5919 /* Mark points if possible. */
5921 dest(*tri, tridest);
5922 if (pointmark(triorg) == 0) {
5923 setpointmark(triorg, shellemark);
5925 if (pointmark(tridest) == 0) {
5926 setpointmark(tridest, shellemark);
5928 /* Check if there's already a shell edge here. */
5929 tspivot(*tri, newshelle);
5930 if (newshelle.sh == dummysh) {
5931 /* Make new shell edge and initialize its vertices. */
5932 makeshelle(&newshelle);
5933 setsorg(newshelle, tridest);
5934 setsdest(newshelle, triorg);
5935 /* Bond new shell edge to the two triangles it is sandwiched between. */
5936 /* Note that the facing triangle `oppotri' might be equal to */
5937 /* `dummytri' (outer space), but the new shell edge is bonded to it */
5939 tsbond(*tri, newshelle);
5941 ssymself(newshelle);
5942 tsbond(oppotri, newshelle);
5943 setmark(newshelle, shellemark);
5945 printf(" Inserting new ");
5946 printshelle(&newshelle);
5949 if (mark(newshelle) == 0) {
5950 setmark(newshelle, shellemark);
5955 /*****************************************************************************/
5959 /* A "local transformation" replaces a small set of triangles with another */
5960 /* set of triangles. This may or may not involve inserting or deleting a */
5963 /* The term "casing" is used to describe the set of triangles that are */
5964 /* attached to the triangles being transformed, but are not transformed */
5965 /* themselves. Think of the casing as a fixed hollow structure inside */
5966 /* which all the action happens. A "casing" is only defined relative to */
5967 /* a single transformation; each occurrence of a transformation will */
5968 /* involve a different casing. */
5970 /* A "shell" is similar to a "casing". The term "shell" describes the set */
5971 /* of shell edges (if any) that are attached to the triangles being */
5972 /* transformed. However, I sometimes use "shell" to refer to a single */
5973 /* shell edge, so don't get confused. */
5975 /*****************************************************************************/
5977 /*****************************************************************************/
5979 /* flip() Transform two triangles to two different triangles by flipping */
5980 /* an edge within a quadrilateral. */
5982 /* Imagine the original triangles, abc and bad, oriented so that the */
5983 /* shared edge ab lies in a horizontal plane, with the point b on the left */
5984 /* and the point a on the right. The point c lies below the edge, and the */
5985 /* point d lies above the edge. The `flipedge' handle holds the edge ab */
5986 /* of triangle abc, and is directed left, from vertex a to vertex b. */
5988 /* The triangles abc and bad are deleted and replaced by the triangles cdb */
5989 /* and dca. The triangles that represent abc and bad are NOT deallocated; */
5990 /* they are reused for dca and cdb, respectively. Hence, any handles that */
5991 /* may have held the original triangles are still valid, although not */
5992 /* directed as they were before. */
5994 /* Upon completion of this routine, the `flipedge' handle holds the edge */
5995 /* dc of triangle dca, and is directed down, from vertex d to vertex c. */
5996 /* (Hence, the two triangles have rotated counterclockwise.) */
5998 /* WARNING: This transformation is geometrically valid only if the */
5999 /* quadrilateral adbc is convex. Furthermore, this transformation is */
6000 /* valid only if there is not a shell edge between the triangles abc and */
6001 /* bad. This routine does not check either of these preconditions, and */
6002 /* it is the responsibility of the calling routine to ensure that they are */
6003 /* met. If they are not, the streets shall be filled with wailing and */
6004 /* gnashing of teeth. */
6006 /*****************************************************************************/
6009 struct triedge *flipedge; /* Handle for the triangle abc. */
6011 struct triedge botleft, botright;
6012 struct triedge topleft, topright;
6014 struct triedge botlcasing, botrcasing;
6015 struct triedge toplcasing, toprcasing;
6016 struct edge botlshelle, botrshelle;
6017 struct edge toplshelle, toprshelle;
6018 point leftpoint, rightpoint, botpoint;
6020 triangle ptr; /* Temporary variable used by sym(). */
6021 shelle sptr; /* Temporary variable used by tspivot(). */
6023 /* Identify the vertices of the quadrilateral. */
6024 org(*flipedge, rightpoint);
6025 dest(*flipedge, leftpoint);
6026 apex(*flipedge, botpoint);
6027 sym(*flipedge, top);
6029 if (top.tri == dummytri) {
6030 printf("Internal error in flip(): Attempt to flip on boundary.\n");
6031 lnextself(*flipedge);
6034 if (checksegments) {
6035 tspivot(*flipedge, toplshelle);
6036 if (toplshelle.sh != dummysh) {
6037 printf("Internal error in flip(): Attempt to flip a segment.\n");
6038 lnextself(*flipedge);
6042 #endif /* SELF_CHECK */
6043 apex(top, farpoint);
6045 /* Identify the casing of the quadrilateral. */
6046 lprev(top, topleft);
6047 sym(topleft, toplcasing);
6048 lnext(top, topright);
6049 sym(topright, toprcasing);
6050 lnext(*flipedge, botleft);
6051 sym(botleft, botlcasing);
6052 lprev(*flipedge, botright);
6053 sym(botright, botrcasing);
6054 /* Rotate the quadrilateral one-quarter turn counterclockwise. */
6055 bond(topleft, botlcasing);
6056 bond(botleft, botrcasing);
6057 bond(botright, toprcasing);
6058 bond(topright, toplcasing);
6060 if (checksegments) {
6061 /* Check for shell edges and rebond them to the quadrilateral. */
6062 tspivot(topleft, toplshelle);
6063 tspivot(botleft, botlshelle);
6064 tspivot(botright, botrshelle);
6065 tspivot(topright, toprshelle);
6066 if (toplshelle.sh == dummysh) {
6067 tsdissolve(topright);
6069 tsbond(topright, toplshelle);
6071 if (botlshelle.sh == dummysh) {
6072 tsdissolve(topleft);
6074 tsbond(topleft, botlshelle);
6076 if (botrshelle.sh == dummysh) {
6077 tsdissolve(botleft);
6079 tsbond(botleft, botrshelle);
6081 if (toprshelle.sh == dummysh) {
6082 tsdissolve(botright);
6084 tsbond(botright, toprshelle);
6088 /* New point assignments for the rotated quadrilateral. */
6089 setorg(*flipedge, farpoint);
6090 setdest(*flipedge, botpoint);
6091 setapex(*flipedge, rightpoint);
6092 setorg(top, botpoint);
6093 setdest(top, farpoint);
6094 setapex(top, leftpoint);
6096 printf(" Edge flip results in left ");
6098 printtriangle(&topleft);
6099 printf(" and right ");
6100 printtriangle(flipedge);
6104 /*****************************************************************************/
6106 /* insertsite() Insert a vertex into a Delaunay triangulation, */
6107 /* performing flips as necessary to maintain the Delaunay */
6110 /* The point `insertpoint' is located. If `searchtri.tri' is not NULL, */
6111 /* the search for the containing triangle begins from `searchtri'. If */
6112 /* `searchtri.tri' is NULL, a full point location procedure is called. */
6113 /* If `insertpoint' is found inside a triangle, the triangle is split into */
6114 /* three; if `insertpoint' lies on an edge, the edge is split in two, */
6115 /* thereby splitting the two adjacent triangles into four. Edge flips are */
6116 /* used to restore the Delaunay property. If `insertpoint' lies on an */
6117 /* existing vertex, no action is taken, and the value DUPLICATEPOINT is */
6118 /* returned. On return, `searchtri' is set to a handle whose origin is the */
6119 /* existing vertex. */
6121 /* Normally, the parameter `splitedge' is set to NULL, implying that no */
6122 /* segment should be split. In this case, if `insertpoint' is found to */
6123 /* lie on a segment, no action is taken, and the value VIOLATINGPOINT is */
6124 /* returned. On return, `searchtri' is set to a handle whose primary edge */
6125 /* is the violated segment. */
6127 /* If the calling routine wishes to split a segment by inserting a point in */
6128 /* it, the parameter `splitedge' should be that segment. In this case, */
6129 /* `searchtri' MUST be the triangle handle reached by pivoting from that */
6130 /* segment; no point location is done. */
6132 /* `segmentflaws' and `triflaws' are flags that indicate whether or not */
6133 /* there should be checks for the creation of encroached segments or bad */
6134 /* quality faces. If a newly inserted point encroaches upon segments, */
6135 /* these segments are added to the list of segments to be split if */
6136 /* `segmentflaws' is set. If bad triangles are created, these are added */
6137 /* to the queue if `triflaws' is set. */
6139 /* If a duplicate point or violated segment does not prevent the point */
6140 /* from being inserted, the return value will be ENCROACHINGPOINT if the */
6141 /* point encroaches upon a segment (and checking is enabled), or */
6142 /* SUCCESSFULPOINT otherwise. In either case, `searchtri' is set to a */
6143 /* handle whose origin is the newly inserted vertex. */
6145 /* insertsite() does not use flip() for reasons of speed; some */
6146 /* information can be reused from edge flip to edge flip, like the */
6147 /* locations of shell edges. */
6149 /*****************************************************************************/
6151 enum insertsiteresult insertsite(insertpoint, searchtri, splitedge,
6152 segmentflaws, triflaws)
6154 struct triedge *searchtri;
6155 struct edge *splitedge;
6159 struct triedge horiz;
6161 struct triedge botleft, botright;
6162 struct triedge topleft, topright;
6163 struct triedge newbotleft, newbotright;
6164 struct triedge newtopright;
6165 struct triedge botlcasing, botrcasing;
6166 struct triedge toplcasing, toprcasing;
6167 struct triedge testtri;
6168 struct edge botlshelle, botrshelle;
6169 struct edge toplshelle, toprshelle;
6170 struct edge brokenshelle;
6171 struct edge checkshelle;
6172 struct edge rightedge;
6173 struct edge newedge;
6174 struct edge *encroached;
6176 point leftpoint, rightpoint, botpoint, toppoint, farpoint;
6179 enum insertsiteresult success;
6180 enum locateresult intersect;
6184 triangle ptr; /* Temporary variable used by sym(). */
6185 shelle sptr; /* Temporary variable used by spivot() and tspivot(). */
6188 printf(" Inserting (%.12g, %.12g).\n", insertpoint[0], insertpoint[1]);
6190 if (splitedge == (struct edge *) NULL) {
6191 /* Find the location of the point to be inserted. Check if a good */
6192 /* starting triangle has already been provided by the caller. */
6193 if (searchtri->tri == (triangle *) NULL) {
6194 /* Find a boundary triangle. */
6195 horiz.tri = dummytri;
6198 /* Search for a triangle containing `insertpoint'. */
6199 intersect = locate(insertpoint, &horiz);
6201 /* Start searching from the triangle provided by the caller. */
6202 triedgecopy(*searchtri, horiz);
6203 intersect = preciselocate(insertpoint, &horiz);
6206 /* The calling routine provides the edge in which the point is inserted. */
6207 triedgecopy(*searchtri, horiz);
6210 if (intersect == ONVERTEX) {
6211 /* There's already a vertex there. Return in `searchtri' a triangle */
6212 /* whose origin is the existing vertex. */
6213 triedgecopy(horiz, *searchtri);
6214 triedgecopy(horiz, recenttri);
6215 return DUPLICATEPOINT;
6217 if ((intersect == ONEDGE) || (intersect == OUTSIDE)) {
6218 /* The vertex falls on an edge or boundary. */
6219 if (checksegments && (splitedge == (struct edge *) NULL)) {
6220 /* Check whether the vertex falls on a shell edge. */
6221 tspivot(horiz, brokenshelle);
6222 if (brokenshelle.sh != dummysh) {
6223 /* The vertex falls on a shell edge. */
6225 if (nobisect == 0) {
6226 /* Add the shell edge to the list of encroached segments. */
6227 encroached = (struct edge *) poolalloc(&badsegments);
6228 shellecopy(brokenshelle, *encroached);
6229 } else if ((nobisect == 1) && (intersect == ONEDGE)) {
6230 /* This segment may be split only if it is an internal boundary. */
6231 sym(horiz, testtri);
6232 if (testtri.tri != dummytri) {
6233 /* Add the shell edge to the list of encroached segments. */
6234 encroached = (struct edge *) poolalloc(&badsegments);
6235 shellecopy(brokenshelle, *encroached);
6239 /* Return a handle whose primary edge contains the point, */
6240 /* which has not been inserted. */
6241 triedgecopy(horiz, *searchtri);
6242 triedgecopy(horiz, recenttri);
6243 return VIOLATINGPOINT;
6246 /* Insert the point on an edge, dividing one triangle into two (if */
6247 /* the edge lies on a boundary) or two triangles into four. */
6248 lprev(horiz, botright);
6249 sym(botright, botrcasing);
6250 sym(horiz, topright);
6251 /* Is there a second triangle? (Or does this edge lie on a boundary?) */
6252 mirrorflag = topright.tri != dummytri;
6254 lnextself(topright);
6255 sym(topright, toprcasing);
6256 maketriangle(&newtopright);
6258 /* Splitting the boundary edge increases the number of boundary edges. */
6261 maketriangle(&newbotright);
6263 /* Set the vertices of changed and new triangles. */
6264 org(horiz, rightpoint);
6265 dest(horiz, leftpoint);
6266 apex(horiz, botpoint);
6267 setorg(newbotright, botpoint);
6268 setdest(newbotright, rightpoint);
6269 setapex(newbotright, insertpoint);
6270 setorg(horiz, insertpoint);
6271 for (i = 0; i < eextras; i++) {
6272 /* Set the element attributes of a new triangle. */
6273 setelemattribute(newbotright, i, elemattribute(botright, i));
6276 /* Set the area constraint of a new triangle. */
6277 setareabound(newbotright, areabound(botright));
6280 dest(topright, toppoint);
6281 setorg(newtopright, rightpoint);
6282 setdest(newtopright, toppoint);
6283 setapex(newtopright, insertpoint);
6284 setorg(topright, insertpoint);
6285 for (i = 0; i < eextras; i++) {
6286 /* Set the element attributes of another new triangle. */
6287 setelemattribute(newtopright, i, elemattribute(topright, i));
6290 /* Set the area constraint of another new triangle. */
6291 setareabound(newtopright, areabound(topright));
6295 /* There may be shell edges that need to be bonded */
6296 /* to the new triangle(s). */
6297 if (checksegments) {
6298 tspivot(botright, botrshelle);
6299 if (botrshelle.sh != dummysh) {
6300 tsdissolve(botright);
6301 tsbond(newbotright, botrshelle);
6304 tspivot(topright, toprshelle);
6305 if (toprshelle.sh != dummysh) {
6306 tsdissolve(topright);
6307 tsbond(newtopright, toprshelle);
6312 /* Bond the new triangle(s) to the surrounding triangles. */
6313 bond(newbotright, botrcasing);
6314 lprevself(newbotright);
6315 bond(newbotright, botright);
6316 lprevself(newbotright);
6318 bond(newtopright, toprcasing);
6319 lnextself(newtopright);
6320 bond(newtopright, topright);
6321 lnextself(newtopright);
6322 bond(newtopright, newbotright);
6325 if (splitedge != (struct edge *) NULL) {
6326 /* Split the shell edge into two. */
6327 setsdest(*splitedge, insertpoint);
6328 ssymself(*splitedge);
6329 spivot(*splitedge, rightedge);
6330 insertshelle(&newbotright, mark(*splitedge));
6331 tspivot(newbotright, newedge);
6332 sbond(*splitedge, newedge);
6334 sbond(newedge, rightedge);
6335 ssymself(*splitedge);
6339 if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
6340 printf("Internal error in insertsite():\n");
6341 printf(" Clockwise triangle prior to edge point insertion (bottom).\n");
6344 if (counterclockwise(leftpoint, rightpoint, toppoint) < 0.0) {
6345 printf("Internal error in insertsite():\n");
6346 printf(" Clockwise triangle prior to edge point insertion (top).\n");
6348 if (counterclockwise(rightpoint, toppoint, insertpoint) < 0.0) {
6349 printf("Internal error in insertsite():\n");
6350 printf(" Clockwise triangle after edge point insertion (top right).\n"
6353 if (counterclockwise(toppoint, leftpoint, insertpoint) < 0.0) {
6354 printf("Internal error in insertsite():\n");
6355 printf(" Clockwise triangle after edge point insertion (top left).\n"
6359 if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
6360 printf("Internal error in insertsite():\n");
6361 printf(" Clockwise triangle after edge point insertion (bottom left).\n"
6364 if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
6365 printf("Internal error in insertsite():\n");
6367 " Clockwise triangle after edge point insertion (bottom right).\n");
6369 #endif /* SELF_CHECK */
6371 printf(" Updating bottom left ");
6372 printtriangle(&botright);
6374 printf(" Updating top left ");
6375 printtriangle(&topright);
6376 printf(" Creating top right ");
6377 printtriangle(&newtopright);
6379 printf(" Creating bottom right ");
6380 printtriangle(&newbotright);
6383 /* Position `horiz' on the first edge to check for */
6384 /* the Delaunay property. */
6387 /* Insert the point in a triangle, splitting it into three. */
6388 lnext(horiz, botleft);
6389 lprev(horiz, botright);
6390 sym(botleft, botlcasing);
6391 sym(botright, botrcasing);
6392 maketriangle(&newbotleft);
6393 maketriangle(&newbotright);
6395 /* Set the vertices of changed and new triangles. */
6396 org(horiz, rightpoint);
6397 dest(horiz, leftpoint);
6398 apex(horiz, botpoint);
6399 setorg(newbotleft, leftpoint);
6400 setdest(newbotleft, botpoint);
6401 setapex(newbotleft, insertpoint);
6402 setorg(newbotright, botpoint);
6403 setdest(newbotright, rightpoint);
6404 setapex(newbotright, insertpoint);
6405 setapex(horiz, insertpoint);
6406 for (i = 0; i < eextras; i++) {
6407 /* Set the element attributes of the new triangles. */
6408 attrib = elemattribute(horiz, i);
6409 setelemattribute(newbotleft, i, attrib);
6410 setelemattribute(newbotright, i, attrib);
6413 /* Set the area constraint of the new triangles. */
6414 area = areabound(horiz);
6415 setareabound(newbotleft, area);
6416 setareabound(newbotright, area);
6419 /* There may be shell edges that need to be bonded */
6420 /* to the new triangles. */
6421 if (checksegments) {
6422 tspivot(botleft, botlshelle);
6423 if (botlshelle.sh != dummysh) {
6424 tsdissolve(botleft);
6425 tsbond(newbotleft, botlshelle);
6427 tspivot(botright, botrshelle);
6428 if (botrshelle.sh != dummysh) {
6429 tsdissolve(botright);
6430 tsbond(newbotright, botrshelle);
6434 /* Bond the new triangles to the surrounding triangles. */
6435 bond(newbotleft, botlcasing);
6436 bond(newbotright, botrcasing);
6437 lnextself(newbotleft);
6438 lprevself(newbotright);
6439 bond(newbotleft, newbotright);
6440 lnextself(newbotleft);
6441 bond(botleft, newbotleft);
6442 lprevself(newbotright);
6443 bond(botright, newbotright);
6446 if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
6447 printf("Internal error in insertsite():\n");
6448 printf(" Clockwise triangle prior to point insertion.\n");
6450 if (counterclockwise(rightpoint, leftpoint, insertpoint) < 0.0) {
6451 printf("Internal error in insertsite():\n");
6452 printf(" Clockwise triangle after point insertion (top).\n");
6454 if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
6455 printf("Internal error in insertsite():\n");
6456 printf(" Clockwise triangle after point insertion (left).\n");
6458 if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
6459 printf("Internal error in insertsite():\n");
6460 printf(" Clockwise triangle after point insertion (right).\n");
6462 #endif /* SELF_CHECK */
6464 printf(" Updating top ");
6465 printtriangle(&horiz);
6466 printf(" Creating left ");
6467 printtriangle(&newbotleft);
6468 printf(" Creating right ");
6469 printtriangle(&newbotright);
6473 /* The insertion is successful by default, unless an encroached */
6474 /* edge is found. */
6475 success = SUCCESSFULPOINT;
6476 /* Circle around the newly inserted vertex, checking each edge opposite */
6477 /* it for the Delaunay property. Non-Delaunay edges are flipped. */
6478 /* `horiz' is always the edge being checked. `first' marks where to */
6479 /* stop circling. */
6482 dest(horiz, leftpoint);
6483 /* Circle until finished. */
6485 /* By default, the edge will be flipped. */
6487 if (checksegments) {
6488 /* Check for a segment, which cannot be flipped. */
6489 tspivot(horiz, checkshelle);
6490 if (checkshelle.sh != dummysh) {
6491 /* The edge is a segment and cannot be flipped. */
6495 /* Does the new point encroach upon this segment? */
6496 if (checkedge4encroach(&checkshelle)) {
6497 success = ENCROACHINGPOINT;
6500 #endif /* not CDT_ONLY */
6504 /* Check if the edge is a boundary edge. */
6506 if (top.tri == dummytri) {
6507 /* The edge is a boundary edge and cannot be flipped. */
6510 /* Find the point on the other side of the edge. */
6511 apex(top, farpoint);
6512 /* In the incremental Delaunay triangulation algorithm, any of */
6513 /* `leftpoint', `rightpoint', and `farpoint' could be vertices */
6514 /* of the triangular bounding box. These vertices must be */
6515 /* treated as if they are infinitely distant, even though their */
6516 /* "coordinates" are not. */
6517 if ((leftpoint == infpoint1) || (leftpoint == infpoint2)
6518 || (leftpoint == infpoint3)) {
6519 /* `leftpoint' is infinitely distant. Check the convexity of */
6520 /* the boundary of the triangulation. 'farpoint' might be */
6521 /* infinite as well, but trust me, this same condition */
6522 /* should be applied. */
6523 doflip = counterclockwise(insertpoint, rightpoint, farpoint) > 0.0;
6524 } else if ((rightpoint == infpoint1) || (rightpoint == infpoint2)
6525 || (rightpoint == infpoint3)) {
6526 /* `rightpoint' is infinitely distant. Check the convexity of */
6527 /* the boundary of the triangulation. 'farpoint' might be */
6528 /* infinite as well, but trust me, this same condition */
6529 /* should be applied. */
6530 doflip = counterclockwise(farpoint, leftpoint, insertpoint) > 0.0;
6531 } else if ((farpoint == infpoint1) || (farpoint == infpoint2)
6532 || (farpoint == infpoint3)) {
6533 /* `farpoint' is infinitely distant and cannot be inside */
6534 /* the circumcircle of the triangle `horiz'. */
6537 /* Test whether the edge is locally Delaunay. */
6538 doflip = incircle(leftpoint, insertpoint, rightpoint, farpoint)
6542 /* We made it! Flip the edge `horiz' by rotating its containing */
6543 /* quadrilateral (the two triangles adjacent to `horiz'). */
6544 /* Identify the casing of the quadrilateral. */
6545 lprev(top, topleft);
6546 sym(topleft, toplcasing);
6547 lnext(top, topright);
6548 sym(topright, toprcasing);
6549 lnext(horiz, botleft);
6550 sym(botleft, botlcasing);
6551 lprev(horiz, botright);
6552 sym(botright, botrcasing);
6553 /* Rotate the quadrilateral one-quarter turn counterclockwise. */
6554 bond(topleft, botlcasing);
6555 bond(botleft, botrcasing);
6556 bond(botright, toprcasing);
6557 bond(topright, toplcasing);
6558 if (checksegments) {
6559 /* Check for shell edges and rebond them to the quadrilateral. */
6560 tspivot(topleft, toplshelle);
6561 tspivot(botleft, botlshelle);
6562 tspivot(botright, botrshelle);
6563 tspivot(topright, toprshelle);
6564 if (toplshelle.sh == dummysh) {
6565 tsdissolve(topright);
6567 tsbond(topright, toplshelle);
6569 if (botlshelle.sh == dummysh) {
6570 tsdissolve(topleft);
6572 tsbond(topleft, botlshelle);
6574 if (botrshelle.sh == dummysh) {
6575 tsdissolve(botleft);
6577 tsbond(botleft, botrshelle);
6579 if (toprshelle.sh == dummysh) {
6580 tsdissolve(botright);
6582 tsbond(botright, toprshelle);
6585 /* New point assignments for the rotated quadrilateral. */
6586 setorg(horiz, farpoint);
6587 setdest(horiz, insertpoint);
6588 setapex(horiz, rightpoint);
6589 setorg(top, insertpoint);
6590 setdest(top, farpoint);
6591 setapex(top, leftpoint);
6592 for (i = 0; i < eextras; i++) {
6593 /* Take the average of the two triangles' attributes. */
6594 attrib = 0.5 * (elemattribute(top, i) + elemattribute(horiz, i));
6595 setelemattribute(top, i, attrib);
6596 setelemattribute(horiz, i, attrib);
6599 if ((areabound(top) <= 0.0) || (areabound(horiz) <= 0.0)) {
6602 /* Take the average of the two triangles' area constraints. */
6603 /* This prevents small area constraints from migrating a */
6604 /* long, long way from their original location due to flips. */
6605 area = 0.5 * (areabound(top) + areabound(horiz));
6607 setareabound(top, area);
6608 setareabound(horiz, area);
6611 if (insertpoint != (point) NULL) {
6612 if (counterclockwise(leftpoint, insertpoint, rightpoint) < 0.0) {
6613 printf("Internal error in insertsite():\n");
6614 printf(" Clockwise triangle prior to edge flip (bottom).\n");
6616 /* The following test has been removed because constrainededge() */
6617 /* sometimes generates inverted triangles that insertsite() */
6620 if (counterclockwise(rightpoint, farpoint, leftpoint) < 0.0) {
6621 printf("Internal error in insertsite():\n");
6622 printf(" Clockwise triangle prior to edge flip (top).\n");
6625 if (counterclockwise(farpoint, leftpoint, insertpoint) < 0.0) {
6626 printf("Internal error in insertsite():\n");
6627 printf(" Clockwise triangle after edge flip (left).\n");
6629 if (counterclockwise(insertpoint, rightpoint, farpoint) < 0.0) {
6630 printf("Internal error in insertsite():\n");
6631 printf(" Clockwise triangle after edge flip (right).\n");
6634 #endif /* SELF_CHECK */
6636 printf(" Edge flip results in left ");
6638 printtriangle(&topleft);
6639 printf(" and right ");
6640 printtriangle(&horiz);
6642 /* On the next iterations, consider the two edges that were */
6643 /* exposed (this is, are now visible to the newly inserted */
6644 /* point) by the edge flip. */
6646 leftpoint = farpoint;
6651 /* The handle `horiz' is accepted as locally Delaunay. */
6654 /* Check the triangle `horiz' for quality. */
6655 testtriangle(&horiz);
6657 #endif /* not CDT_ONLY */
6658 /* Look for the next edge around the newly inserted point. */
6660 sym(horiz, testtri);
6661 /* Check for finishing a complete revolution about the new point, or */
6662 /* falling off the edge of the triangulation. The latter will */
6663 /* happen when a point is inserted at a boundary. */
6664 if ((leftpoint == first) || (testtri.tri == dummytri)) {
6665 /* We're done. Return a triangle whose origin is the new point. */
6666 lnext(horiz, *searchtri);
6667 lnext(horiz, recenttri);
6670 /* Finish finding the next edge around the newly inserted point. */
6671 lnext(testtri, horiz);
6672 rightpoint = leftpoint;
6673 dest(horiz, leftpoint);
6678 /*****************************************************************************/
6680 /* triangulatepolygon() Find the Delaunay triangulation of a polygon that */
6681 /* has a certain "nice" shape. This includes the */
6682 /* polygons that result from deletion of a point or */
6683 /* insertion of a segment. */
6685 /* This is a conceptually difficult routine. The starting assumption is */
6686 /* that we have a polygon with n sides. n - 1 of these sides are currently */
6687 /* represented as edges in the mesh. One side, called the "base", need not */
6690 /* Inside the polygon is a structure I call a "fan", consisting of n - 1 */
6691 /* triangles that share a common origin. For each of these triangles, the */
6692 /* edge opposite the origin is one of the sides of the polygon. The */
6693 /* primary edge of each triangle is the edge directed from the origin to */
6694 /* the destination; note that this is not the same edge that is a side of */
6695 /* the polygon. `firstedge' is the primary edge of the first triangle. */
6696 /* From there, the triangles follow in counterclockwise order about the */
6697 /* polygon, until `lastedge', the primary edge of the last triangle. */
6698 /* `firstedge' and `lastedge' are probably connected to other triangles */
6699 /* beyond the extremes of the fan, but their identity is not important, as */
6700 /* long as the fan remains connected to them. */
6702 /* Imagine the polygon oriented so that its base is at the bottom. This */
6703 /* puts `firstedge' on the far right, and `lastedge' on the far left. */
6704 /* The right vertex of the base is the destination of `firstedge', and the */
6705 /* left vertex of the base is the apex of `lastedge'. */
6707 /* The challenge now is to find the right sequence of edge flips to */
6708 /* transform the fan into a Delaunay triangulation of the polygon. Each */
6709 /* edge flip effectively removes one triangle from the fan, committing it */
6710 /* to the polygon. The resulting polygon has one fewer edge. If `doflip' */
6711 /* is set, the final flip will be performed, resulting in a fan of one */
6712 /* (useless?) triangle. If `doflip' is not set, the final flip is not */
6713 /* performed, resulting in a fan of two triangles, and an unfinished */
6714 /* triangular polygon that is not yet filled out with a single triangle. */
6715 /* On completion of the routine, `lastedge' is the last remaining triangle, */
6716 /* or the leftmost of the last two. */
6718 /* Although the flips are performed in the order described above, the */
6719 /* decisions about what flips to perform are made in precisely the reverse */
6720 /* order. The recursive triangulatepolygon() procedure makes a decision, */
6721 /* uses up to two recursive calls to triangulate the "subproblems" */
6722 /* (polygons with fewer edges), and then performs an edge flip. */
6724 /* The "decision" it makes is which vertex of the polygon should be */
6725 /* connected to the base. This decision is made by testing every possible */
6726 /* vertex. Once the best vertex is found, the two edges that connect this */
6727 /* vertex to the base become the bases for two smaller polygons. These */
6728 /* are triangulated recursively. Unfortunately, this approach can take */
6729 /* O(n^2) time not only in the worst case, but in many common cases. It's */
6730 /* rarely a big deal for point deletion, where n is rarely larger than ten, */
6731 /* but it could be a big deal for segment insertion, especially if there's */
6732 /* a lot of long segments that each cut many triangles. I ought to code */
6733 /* a faster algorithm some time. */
6735 /* The `edgecount' parameter is the number of sides of the polygon, */
6736 /* including its base. `triflaws' is a flag that determines whether the */
6737 /* new triangles should be tested for quality, and enqueued if they are */
6740 /*****************************************************************************/
6742 void triangulatepolygon(firstedge, lastedge, edgecount, doflip, triflaws)
6743 struct triedge *firstedge;
6744 struct triedge *lastedge;
6749 struct triedge testtri;
6750 struct triedge besttri;
6751 struct triedge tempedge;
6752 point leftbasepoint, rightbasepoint;
6757 triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
6759 /* Identify the base vertices. */
6760 apex(*lastedge, leftbasepoint);
6761 dest(*firstedge, rightbasepoint);
6763 printf(" Triangulating interior polygon at edge\n");
6764 printf(" (%.12g, %.12g) (%.12g, %.12g)\n", leftbasepoint[0],
6765 leftbasepoint[1], rightbasepoint[0], rightbasepoint[1]);
6767 /* Find the best vertex to connect the base to. */
6768 onext(*firstedge, besttri);
6769 dest(besttri, bestpoint);
6770 triedgecopy(besttri, testtri);
6772 for (i = 2; i <= edgecount - 2; i++) {
6774 dest(testtri, testpoint);
6775 /* Is this a better vertex? */
6776 if (incircle(leftbasepoint, rightbasepoint, bestpoint, testpoint) > 0.0) {
6777 triedgecopy(testtri, besttri);
6778 bestpoint = testpoint;
6783 printf(" Connecting edge to (%.12g, %.12g)\n", bestpoint[0],
6786 if (bestnumber > 1) {
6787 /* Recursively triangulate the smaller polygon on the right. */
6788 oprev(besttri, tempedge);
6789 triangulatepolygon(firstedge, &tempedge, bestnumber + 1, 1, triflaws);
6791 if (bestnumber < edgecount - 2) {
6792 /* Recursively triangulate the smaller polygon on the left. */
6793 sym(besttri, tempedge);
6794 triangulatepolygon(&besttri, lastedge, edgecount - bestnumber, 1,
6796 /* Find `besttri' again; it may have been lost to edge flips. */
6797 sym(tempedge, besttri);
6800 /* Do one final edge flip. */
6804 /* Check the quality of the newly committed triangle. */
6805 sym(besttri, testtri);
6806 testtriangle(&testtri);
6808 #endif /* not CDT_ONLY */
6810 /* Return the base triangle. */
6811 triedgecopy(besttri, *lastedge);
6814 /*****************************************************************************/
6816 /* deletesite() Delete a vertex from a Delaunay triangulation, ensuring */
6817 /* that the triangulation remains Delaunay. */
6819 /* The origin of `deltri' is deleted. The union of the triangles adjacent */
6820 /* to this point is a polygon, for which the Delaunay triangulation is */
6821 /* found. Two triangles are removed from the mesh. */
6823 /* Only interior points that do not lie on segments (shell edges) or */
6824 /* boundaries may be deleted. */
6826 /*****************************************************************************/
6830 void deletesite(deltri)
6831 struct triedge *deltri;
6833 struct triedge countingtri;
6834 struct triedge firstedge, lastedge;
6835 struct triedge deltriright;
6836 struct triedge lefttri, righttri;
6837 struct triedge leftcasing, rightcasing;
6838 struct edge leftshelle, rightshelle;
6842 triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
6843 shelle sptr; /* Temporary variable used by tspivot(). */
6845 org(*deltri, delpoint);
6847 printf(" Deleting (%.12g, %.12g).\n", delpoint[0], delpoint[1]);
6849 pointdealloc(delpoint);
6851 /* Count the degree of the point being deleted. */
6852 onext(*deltri, countingtri);
6854 while (!triedgeequal(*deltri, countingtri)) {
6856 if (countingtri.tri == dummytri) {
6857 printf("Internal error in deletesite():\n");
6858 printf(" Attempt to delete boundary point.\n");
6861 #endif /* SELF_CHECK */
6863 onextself(countingtri);
6867 if (edgecount < 3) {
6868 printf("Internal error in deletesite():\n Point has degree %d.\n",
6872 #endif /* SELF_CHECK */
6873 if (edgecount > 3) {
6874 /* Triangulate the polygon defined by the union of all triangles */
6875 /* adjacent to the point being deleted. Check the quality of */
6876 /* the resulting triangles. */
6877 onext(*deltri, firstedge);
6878 oprev(*deltri, lastedge);
6879 triangulatepolygon(&firstedge, &lastedge, edgecount, 0, !nobisect);
6881 /* Splice out two triangles. */
6882 lprev(*deltri, deltriright);
6883 dnext(*deltri, lefttri);
6884 sym(lefttri, leftcasing);
6885 oprev(deltriright, righttri);
6886 sym(righttri, rightcasing);
6887 bond(*deltri, leftcasing);
6888 bond(deltriright, rightcasing);
6889 tspivot(lefttri, leftshelle);
6890 if (leftshelle.sh != dummysh) {
6891 tsbond(*deltri, leftshelle);
6893 tspivot(righttri, rightshelle);
6894 if (rightshelle.sh != dummysh) {
6895 tsbond(deltriright, rightshelle);
6898 /* Set the new origin of `deltri' and check its quality. */
6899 org(lefttri, neworg);
6900 setorg(*deltri, neworg);
6902 testtriangle(deltri);
6905 /* Delete the two spliced-out triangles. */
6906 triangledealloc(lefttri.tri);
6907 triangledealloc(righttri.tri);
6910 #endif /* not CDT_ONLY */
6914 /********* Mesh transformation routines end here *********/
6916 /********* Divide-and-conquer Delaunay triangulation begins here *********/
6920 /*****************************************************************************/
6922 /* The divide-and-conquer bounding box */
6924 /* I originally implemented the divide-and-conquer and incremental Delaunay */
6925 /* triangulations using the edge-based data structure presented by Guibas */
6926 /* and Stolfi. Switching to a triangle-based data structure doubled the */
6927 /* speed. However, I had to think of a few extra tricks to maintain the */
6928 /* elegance of the original algorithms. */
6930 /* The "bounding box" used by my variant of the divide-and-conquer */
6931 /* algorithm uses one triangle for each edge of the convex hull of the */
6932 /* triangulation. These bounding triangles all share a common apical */
6933 /* vertex, which is represented by NULL and which represents nothing. */
6934 /* The bounding triangles are linked in a circular fan about this NULL */
6935 /* vertex, and the edges on the convex hull of the triangulation appear */
6936 /* opposite the NULL vertex. You might find it easiest to imagine that */
6937 /* the NULL vertex is a point in 3D space behind the center of the */
6938 /* triangulation, and that the bounding triangles form a sort of cone. */
6940 /* This bounding box makes it easy to represent degenerate cases. For */
6941 /* instance, the triangulation of two vertices is a single edge. This edge */
6942 /* is represented by two bounding box triangles, one on each "side" of the */
6943 /* edge. These triangles are also linked together in a fan about the NULL */
6946 /* The bounding box also makes it easy to traverse the convex hull, as the */
6947 /* divide-and-conquer algorithm needs to do. */
6949 /*****************************************************************************/
6951 /*****************************************************************************/
6953 /* pointsort() Sort an array of points by x-coordinate, using the */
6954 /* y-coordinate as a secondary key. */
6956 /* Uses quicksort. Randomized O(n log n) time. No, I did not make any of */
6957 /* the usual quicksort mistakes. */
6959 /*****************************************************************************/
6961 void pointsort(sortarray, arraysize)
6967 REAL pivotx, pivoty;
6970 if (arraysize == 2) {
6971 /* Recursive base case. */
6972 if ((sortarray[0][0] > sortarray[1][0]) ||
6973 ((sortarray[0][0] == sortarray[1][0]) &&
6974 (sortarray[0][1] > sortarray[1][1]))) {
6975 temp = sortarray[1];
6976 sortarray[1] = sortarray[0];
6977 sortarray[0] = temp;
6981 /* Choose a random pivot to split the array. */
6982 pivot = (int) randomnation(arraysize);
6983 pivotx = sortarray[pivot][0];
6984 pivoty = sortarray[pivot][1];
6985 /* Split the array. */
6988 while (left < right) {
6989 /* Search for a point whose x-coordinate is too large for the left. */
6992 } while ((left <= right) && ((sortarray[left][0] < pivotx) ||
6993 ((sortarray[left][0] == pivotx) &&
6994 (sortarray[left][1] < pivoty))));
6995 /* Search for a point whose x-coordinate is too small for the right. */
6998 } while ((left <= right) && ((sortarray[right][0] > pivotx) ||
6999 ((sortarray[right][0] == pivotx) &&
7000 (sortarray[right][1] > pivoty))));
7002 /* Swap the left and right points. */
7003 temp = sortarray[left];
7004 sortarray[left] = sortarray[right];
7005 sortarray[right] = temp;
7009 /* Recursively sort the left subset. */
7010 pointsort(sortarray, left);
7012 if (right < arraysize - 2) {
7013 /* Recursively sort the right subset. */
7014 pointsort(&sortarray[right + 1], arraysize - right - 1);
7018 /*****************************************************************************/
7020 /* pointmedian() An order statistic algorithm, almost. Shuffles an array */
7021 /* of points so that the first `median' points occur */
7022 /* lexicographically before the remaining points. */
7024 /* Uses the x-coordinate as the primary key if axis == 0; the y-coordinate */
7025 /* if axis == 1. Very similar to the pointsort() procedure, but runs in */
7026 /* randomized linear time. */
7028 /*****************************************************************************/
7030 void pointmedian(sortarray, arraysize, median, axis)
7038 REAL pivot1, pivot2;
7041 if (arraysize == 2) {
7042 /* Recursive base case. */
7043 if ((sortarray[0][axis] > sortarray[1][axis]) ||
7044 ((sortarray[0][axis] == sortarray[1][axis]) &&
7045 (sortarray[0][1 - axis] > sortarray[1][1 - axis]))) {
7046 temp = sortarray[1];
7047 sortarray[1] = sortarray[0];
7048 sortarray[0] = temp;
7052 /* Choose a random pivot to split the array. */
7053 pivot = (int) randomnation(arraysize);
7054 pivot1 = sortarray[pivot][axis];
7055 pivot2 = sortarray[pivot][1 - axis];
7056 /* Split the array. */
7059 while (left < right) {
7060 /* Search for a point whose x-coordinate is too large for the left. */
7063 } while ((left <= right) && ((sortarray[left][axis] < pivot1) ||
7064 ((sortarray[left][axis] == pivot1) &&
7065 (sortarray[left][1 - axis] < pivot2))));
7066 /* Search for a point whose x-coordinate is too small for the right. */
7069 } while ((left <= right) && ((sortarray[right][axis] > pivot1) ||
7070 ((sortarray[right][axis] == pivot1) &&
7071 (sortarray[right][1 - axis] > pivot2))));
7073 /* Swap the left and right points. */
7074 temp = sortarray[left];
7075 sortarray[left] = sortarray[right];
7076 sortarray[right] = temp;
7079 /* Unlike in pointsort(), at most one of the following */
7080 /* conditionals is true. */
7081 if (left > median) {
7082 /* Recursively shuffle the left subset. */
7083 pointmedian(sortarray, left, median, axis);
7085 if (right < median - 1) {
7086 /* Recursively shuffle the right subset. */
7087 pointmedian(&sortarray[right + 1], arraysize - right - 1,
7088 median - right - 1, axis);
7092 /*****************************************************************************/
7094 /* alternateaxes() Sorts the points as appropriate for the divide-and- */
7095 /* conquer algorithm with alternating cuts. */
7097 /* Partitions by x-coordinate if axis == 0; by y-coordinate if axis == 1. */
7098 /* For the base case, subsets containing only two or three points are */
7099 /* always sorted by x-coordinate. */
7101 /*****************************************************************************/
7103 void alternateaxes(sortarray, arraysize, axis)
7110 divider = arraysize >> 1;
7111 if (arraysize <= 3) {
7112 /* Recursive base case: subsets of two or three points will be */
7113 /* handled specially, and should always be sorted by x-coordinate. */
7116 /* Partition with a horizontal or vertical cut. */
7117 pointmedian(sortarray, arraysize, divider, axis);
7118 /* Recursively partition the subsets with a cross cut. */
7119 if (arraysize - divider >= 2) {
7121 alternateaxes(sortarray, divider, 1 - axis);
7123 alternateaxes(&sortarray[divider], arraysize - divider, 1 - axis);
7127 /*****************************************************************************/
7129 /* mergehulls() Merge two adjacent Delaunay triangulations into a */
7130 /* single Delaunay triangulation. */
7132 /* This is similar to the algorithm given by Guibas and Stolfi, but uses */
7133 /* a triangle-based, rather than edge-based, data structure. */
7135 /* The algorithm walks up the gap between the two triangulations, knitting */
7136 /* them together. As they are merged, some of their bounding triangles */
7137 /* are converted into real triangles of the triangulation. The procedure */
7138 /* pulls each hull's bounding triangles apart, then knits them together */
7139 /* like the teeth of two gears. The Delaunay property determines, at each */
7140 /* step, whether the next "tooth" is a bounding triangle of the left hull */
7141 /* or the right. When a bounding triangle becomes real, its apex is */
7142 /* changed from NULL to a real point. */
7144 /* Only two new triangles need to be allocated. These become new bounding */
7145 /* triangles at the top and bottom of the seam. They are used to connect */
7146 /* the remaining bounding triangles (those that have not been converted */
7147 /* into real triangles) into a single fan. */
7149 /* On entry, `farleft' and `innerleft' are bounding triangles of the left */
7150 /* triangulation. The origin of `farleft' is the leftmost vertex, and */
7151 /* the destination of `innerleft' is the rightmost vertex of the */
7152 /* triangulation. Similarly, `innerright' and `farright' are bounding */
7153 /* triangles of the right triangulation. The origin of `innerright' and */
7154 /* destination of `farright' are the leftmost and rightmost vertices. */
7156 /* On completion, the origin of `farleft' is the leftmost vertex of the */
7157 /* merged triangulation, and the destination of `farright' is the rightmost */
7160 /*****************************************************************************/
7162 void mergehulls(farleft, innerleft, innerright, farright, axis)
7163 struct triedge *farleft;
7164 struct triedge *innerleft;
7165 struct triedge *innerright;
7166 struct triedge *farright;
7169 struct triedge leftcand, rightcand;
7170 struct triedge baseedge;
7171 struct triedge nextedge;
7172 struct triedge sidecasing, topcasing, outercasing;
7173 struct triedge checkedge;
7174 point innerleftdest;
7175 point innerrightorg;
7176 point innerleftapex, innerrightapex;
7177 point farleftpt, farrightpt;
7178 point farleftapex, farrightapex;
7179 point lowerleft, lowerright;
7180 point upperleft, upperright;
7185 int leftfinished, rightfinished;
7186 triangle ptr; /* Temporary variable used by sym(). */
7188 dest(*innerleft, innerleftdest);
7189 apex(*innerleft, innerleftapex);
7190 org(*innerright, innerrightorg);
7191 apex(*innerright, innerrightapex);
7192 /* Special treatment for horizontal cuts. */
7193 if (dwyer && (axis == 1)) {
7194 org(*farleft, farleftpt);
7195 apex(*farleft, farleftapex);
7196 dest(*farright, farrightpt);
7197 apex(*farright, farrightapex);
7198 /* The pointers to the extremal points are shifted to point to the */
7199 /* topmost and bottommost point of each hull, rather than the */
7200 /* leftmost and rightmost points. */
7201 while (farleftapex[1] < farleftpt[1]) {
7202 lnextself(*farleft);
7204 farleftpt = farleftapex;
7205 apex(*farleft, farleftapex);
7207 sym(*innerleft, checkedge);
7208 apex(checkedge, checkvertex);
7209 while (checkvertex[1] > innerleftdest[1]) {
7210 lnext(checkedge, *innerleft);
7211 innerleftapex = innerleftdest;
7212 innerleftdest = checkvertex;
7213 sym(*innerleft, checkedge);
7214 apex(checkedge, checkvertex);
7216 while (innerrightapex[1] < innerrightorg[1]) {
7217 lnextself(*innerright);
7218 symself(*innerright);
7219 innerrightorg = innerrightapex;
7220 apex(*innerright, innerrightapex);
7222 sym(*farright, checkedge);
7223 apex(checkedge, checkvertex);
7224 while (checkvertex[1] > farrightpt[1]) {
7225 lnext(checkedge, *farright);
7226 farrightapex = farrightpt;
7227 farrightpt = checkvertex;
7228 sym(*farright, checkedge);
7229 apex(checkedge, checkvertex);
7232 /* Find a line tangent to and below both hulls. */
7235 /* Make innerleftdest the "bottommost" point of the left hull. */
7236 if (counterclockwise(innerleftdest, innerleftapex, innerrightorg) > 0.0) {
7237 lprevself(*innerleft);
7238 symself(*innerleft);
7239 innerleftdest = innerleftapex;
7240 apex(*innerleft, innerleftapex);
7243 /* Make innerrightorg the "bottommost" point of the right hull. */
7244 if (counterclockwise(innerrightapex, innerrightorg, innerleftdest) > 0.0) {
7245 lnextself(*innerright);
7246 symself(*innerright);
7247 innerrightorg = innerrightapex;
7248 apex(*innerright, innerrightapex);
7251 } while (changemade);
7252 /* Find the two candidates to be the next "gear tooth". */
7253 sym(*innerleft, leftcand);
7254 sym(*innerright, rightcand);
7255 /* Create the bottom new bounding triangle. */
7256 maketriangle(&baseedge);
7257 /* Connect it to the bounding boxes of the left and right triangulations. */
7258 bond(baseedge, *innerleft);
7259 lnextself(baseedge);
7260 bond(baseedge, *innerright);
7261 lnextself(baseedge);
7262 setorg(baseedge, innerrightorg);
7263 setdest(baseedge, innerleftdest);
7264 /* Apex is intentionally left NULL. */
7266 printf(" Creating base bounding ");
7267 printtriangle(&baseedge);
7269 /* Fix the extreme triangles if necessary. */
7270 org(*farleft, farleftpt);
7271 if (innerleftdest == farleftpt) {
7272 lnext(baseedge, *farleft);
7274 dest(*farright, farrightpt);
7275 if (innerrightorg == farrightpt) {
7276 lprev(baseedge, *farright);
7278 /* The vertices of the current knitting edge. */
7279 lowerleft = innerleftdest;
7280 lowerright = innerrightorg;
7281 /* The candidate vertices for knitting. */
7282 apex(leftcand, upperleft);
7283 apex(rightcand, upperright);
7284 /* Walk up the gap between the two triangulations, knitting them together. */
7286 /* Have we reached the top? (This isn't quite the right question, */
7287 /* because even though the left triangulation might seem finished now, */
7288 /* moving up on the right triangulation might reveal a new point of */
7289 /* the left triangulation. And vice-versa.) */
7290 leftfinished = counterclockwise(upperleft, lowerleft, lowerright) <= 0.0;
7291 rightfinished = counterclockwise(upperright, lowerleft, lowerright) <= 0.0;
7292 if (leftfinished && rightfinished) {
7293 /* Create the top new bounding triangle. */
7294 maketriangle(&nextedge);
7295 setorg(nextedge, lowerleft);
7296 setdest(nextedge, lowerright);
7297 /* Apex is intentionally left NULL. */
7298 /* Connect it to the bounding boxes of the two triangulations. */
7299 bond(nextedge, baseedge);
7300 lnextself(nextedge);
7301 bond(nextedge, rightcand);
7302 lnextself(nextedge);
7303 bond(nextedge, leftcand);
7305 printf(" Creating top bounding ");
7306 printtriangle(&baseedge);
7308 /* Special treatment for horizontal cuts. */
7309 if (dwyer && (axis == 1)) {
7310 org(*farleft, farleftpt);
7311 apex(*farleft, farleftapex);
7312 dest(*farright, farrightpt);
7313 apex(*farright, farrightapex);
7314 sym(*farleft, checkedge);
7315 apex(checkedge, checkvertex);
7316 /* The pointers to the extremal points are restored to the leftmost */
7317 /* and rightmost points (rather than topmost and bottommost). */
7318 while (checkvertex[0] < farleftpt[0]) {
7319 lprev(checkedge, *farleft);
7320 farleftapex = farleftpt;
7321 farleftpt = checkvertex;
7322 sym(*farleft, checkedge);
7323 apex(checkedge, checkvertex);
7325 while (farrightapex[0] > farrightpt[0]) {
7326 lprevself(*farright);
7328 farrightpt = farrightapex;
7329 apex(*farright, farrightapex);
7334 /* Consider eliminating edges from the left triangulation. */
7335 if (!leftfinished) {
7336 /* What vertex would be exposed if an edge were deleted? */
7337 lprev(leftcand, nextedge);
7339 apex(nextedge, nextapex);
7340 /* If nextapex is NULL, then no vertex would be exposed; the */
7341 /* triangulation would have been eaten right through. */
7342 if (nextapex != (point) NULL) {
7343 /* Check whether the edge is Delaunay. */
7344 badedge = incircle(lowerleft, lowerright, upperleft, nextapex) > 0.0;
7346 /* Eliminate the edge with an edge flip. As a result, the */
7347 /* left triangulation will have one more boundary triangle. */
7348 lnextself(nextedge);
7349 sym(nextedge, topcasing);
7350 lnextself(nextedge);
7351 sym(nextedge, sidecasing);
7352 bond(nextedge, topcasing);
7353 bond(leftcand, sidecasing);
7354 lnextself(leftcand);
7355 sym(leftcand, outercasing);
7356 lprevself(nextedge);
7357 bond(nextedge, outercasing);
7358 /* Correct the vertices to reflect the edge flip. */
7359 setorg(leftcand, lowerleft);
7360 setdest(leftcand, NULL);
7361 setapex(leftcand, nextapex);
7362 setorg(nextedge, NULL);
7363 setdest(nextedge, upperleft);
7364 setapex(nextedge, nextapex);
7365 /* Consider the newly exposed vertex. */
7366 upperleft = nextapex;
7367 /* What vertex would be exposed if another edge were deleted? */
7368 triedgecopy(sidecasing, nextedge);
7369 apex(nextedge, nextapex);
7370 if (nextapex != (point) NULL) {
7371 /* Check whether the edge is Delaunay. */
7372 badedge = incircle(lowerleft, lowerright, upperleft, nextapex)
7375 /* Avoid eating right through the triangulation. */
7381 /* Consider eliminating edges from the right triangulation. */
7382 if (!rightfinished) {
7383 /* What vertex would be exposed if an edge were deleted? */
7384 lnext(rightcand, nextedge);
7386 apex(nextedge, nextapex);
7387 /* If nextapex is NULL, then no vertex would be exposed; the */
7388 /* triangulation would have been eaten right through. */
7389 if (nextapex != (point) NULL) {
7390 /* Check whether the edge is Delaunay. */
7391 badedge = incircle(lowerleft, lowerright, upperright, nextapex) > 0.0;
7393 /* Eliminate the edge with an edge flip. As a result, the */
7394 /* right triangulation will have one more boundary triangle. */
7395 lprevself(nextedge);
7396 sym(nextedge, topcasing);
7397 lprevself(nextedge);
7398 sym(nextedge, sidecasing);
7399 bond(nextedge, topcasing);
7400 bond(rightcand, sidecasing);
7401 lprevself(rightcand);
7402 sym(rightcand, outercasing);
7403 lnextself(nextedge);
7404 bond(nextedge, outercasing);
7405 /* Correct the vertices to reflect the edge flip. */
7406 setorg(rightcand, NULL);
7407 setdest(rightcand, lowerright);
7408 setapex(rightcand, nextapex);
7409 setorg(nextedge, upperright);
7410 setdest(nextedge, NULL);
7411 setapex(nextedge, nextapex);
7412 /* Consider the newly exposed vertex. */
7413 upperright = nextapex;
7414 /* What vertex would be exposed if another edge were deleted? */
7415 triedgecopy(sidecasing, nextedge);
7416 apex(nextedge, nextapex);
7417 if (nextapex != (point) NULL) {
7418 /* Check whether the edge is Delaunay. */
7419 badedge = incircle(lowerleft, lowerright, upperright, nextapex)
7422 /* Avoid eating right through the triangulation. */
7428 if (leftfinished || (!rightfinished &&
7429 (incircle(upperleft, lowerleft, lowerright, upperright) > 0.0))) {
7430 /* Knit the triangulations, adding an edge from `lowerleft' */
7431 /* to `upperright'. */
7432 bond(baseedge, rightcand);
7433 lprev(rightcand, baseedge);
7434 setdest(baseedge, lowerleft);
7435 lowerright = upperright;
7436 sym(baseedge, rightcand);
7437 apex(rightcand, upperright);
7439 /* Knit the triangulations, adding an edge from `upperleft' */
7440 /* to `lowerright'. */
7441 bond(baseedge, leftcand);
7442 lnext(leftcand, baseedge);
7443 setorg(baseedge, lowerright);
7444 lowerleft = upperleft;
7445 sym(baseedge, leftcand);
7446 apex(leftcand, upperleft);
7449 printf(" Connecting ");
7450 printtriangle(&baseedge);
7455 /*****************************************************************************/
7457 /* divconqrecurse() Recursively form a Delaunay triangulation by the */
7458 /* divide-and-conquer method. */
7460 /* Recursively breaks down the problem into smaller pieces, which are */
7461 /* knitted together by mergehulls(). The base cases (problems of two or */
7462 /* three points) are handled specially here. */
7464 /* On completion, `farleft' and `farright' are bounding triangles such that */
7465 /* the origin of `farleft' is the leftmost vertex (breaking ties by */
7466 /* choosing the highest leftmost vertex), and the destination of */
7467 /* `farright' is the rightmost vertex (breaking ties by choosing the */
7468 /* lowest rightmost vertex). */
7470 /*****************************************************************************/
7472 void divconqrecurse(sortarray, vertices, axis, farleft, farright)
7476 struct triedge *farleft;
7477 struct triedge *farright;
7479 struct triedge midtri, tri1, tri2, tri3;
7480 struct triedge innerleft, innerright;
7485 printf(" Triangulating %d points.\n", vertices);
7487 if (vertices == 2) {
7488 /* The triangulation of two vertices is an edge. An edge is */
7489 /* represented by two bounding triangles. */
7490 maketriangle(farleft);
7491 setorg(*farleft, sortarray[0]);
7492 setdest(*farleft, sortarray[1]);
7493 /* The apex is intentionally left NULL. */
7494 maketriangle(farright);
7495 setorg(*farright, sortarray[1]);
7496 setdest(*farright, sortarray[0]);
7497 /* The apex is intentionally left NULL. */
7498 bond(*farleft, *farright);
7499 lprevself(*farleft);
7500 lnextself(*farright);
7501 bond(*farleft, *farright);
7502 lprevself(*farleft);
7503 lnextself(*farright);
7504 bond(*farleft, *farright);
7506 printf(" Creating ");
7507 printtriangle(farleft);
7508 printf(" Creating ");
7509 printtriangle(farright);
7511 /* Ensure that the origin of `farleft' is sortarray[0]. */
7512 lprev(*farright, *farleft);
7514 } else if (vertices == 3) {
7515 /* The triangulation of three vertices is either a triangle (with */
7516 /* three bounding triangles) or two edges (with four bounding */
7517 /* triangles). In either case, four triangles are created. */
7518 maketriangle(&midtri);
7519 maketriangle(&tri1);
7520 maketriangle(&tri2);
7521 maketriangle(&tri3);
7522 area = counterclockwise(sortarray[0], sortarray[1], sortarray[2]);
7524 /* Three collinear points; the triangulation is two edges. */
7525 setorg(midtri, sortarray[0]);
7526 setdest(midtri, sortarray[1]);
7527 setorg(tri1, sortarray[1]);
7528 setdest(tri1, sortarray[0]);
7529 setorg(tri2, sortarray[2]);
7530 setdest(tri2, sortarray[1]);
7531 setorg(tri3, sortarray[1]);
7532 setdest(tri3, sortarray[2]);
7533 /* All apices are intentionally left NULL. */
7548 /* Ensure that the origin of `farleft' is sortarray[0]. */
7549 triedgecopy(tri1, *farleft);
7550 /* Ensure that the destination of `farright' is sortarray[2]. */
7551 triedgecopy(tri2, *farright);
7553 /* The three points are not collinear; the triangulation is one */
7554 /* triangle, namely `midtri'. */
7555 setorg(midtri, sortarray[0]);
7556 setdest(tri1, sortarray[0]);
7557 setorg(tri3, sortarray[0]);
7558 /* Apices of tri1, tri2, and tri3 are left NULL. */
7560 /* The vertices are in counterclockwise order. */
7561 setdest(midtri, sortarray[1]);
7562 setorg(tri1, sortarray[1]);
7563 setdest(tri2, sortarray[1]);
7564 setapex(midtri, sortarray[2]);
7565 setorg(tri2, sortarray[2]);
7566 setdest(tri3, sortarray[2]);
7568 /* The vertices are in clockwise order. */
7569 setdest(midtri, sortarray[2]);
7570 setorg(tri1, sortarray[2]);
7571 setdest(tri2, sortarray[2]);
7572 setapex(midtri, sortarray[1]);
7573 setorg(tri2, sortarray[1]);
7574 setdest(tri3, sortarray[1]);
7576 /* The topology does not depend on how the vertices are ordered. */
7591 /* Ensure that the origin of `farleft' is sortarray[0]. */
7592 triedgecopy(tri1, *farleft);
7593 /* Ensure that the destination of `farright' is sortarray[2]. */
7595 triedgecopy(tri2, *farright);
7597 lnext(*farleft, *farright);
7601 printf(" Creating ");
7602 printtriangle(&midtri);
7603 printf(" Creating ");
7604 printtriangle(&tri1);
7605 printf(" Creating ");
7606 printtriangle(&tri2);
7607 printf(" Creating ");
7608 printtriangle(&tri3);
7612 /* Split the vertices in half. */
7613 divider = vertices >> 1;
7614 /* Recursively triangulate each half. */
7615 divconqrecurse(sortarray, divider, 1 - axis, farleft, &innerleft);
7616 divconqrecurse(&sortarray[divider], vertices - divider, 1 - axis,
7617 &innerright, farright);
7619 printf(" Joining triangulations with %d and %d vertices.\n", divider,
7620 vertices - divider);
7622 /* Merge the two triangulations into one. */
7623 mergehulls(farleft, &innerleft, &innerright, farright, axis);
7627 long removeghosts(startghost)
7628 struct triedge *startghost;
7630 struct triedge searchedge;
7631 struct triedge dissolveedge;
7632 struct triedge deadtri;
7635 triangle ptr; /* Temporary variable used by sym(). */
7638 printf(" Removing ghost triangles.\n");
7640 /* Find an edge on the convex hull to start point location from. */
7641 lprev(*startghost, searchedge);
7642 symself(searchedge);
7643 dummytri[0] = encode(searchedge);
7644 /* Remove the bounding box and count the convex hull edges. */
7645 triedgecopy(*startghost, dissolveedge);
7649 lnext(dissolveedge, deadtri);
7650 lprevself(dissolveedge);
7651 symself(dissolveedge);
7652 /* If no PSLG is involved, set the boundary markers of all the points */
7653 /* on the convex hull. If a PSLG is used, this step is done later. */
7655 /* Watch out for the case where all the input points are collinear. */
7656 if (dissolveedge.tri != dummytri) {
7657 org(dissolveedge, markorg);
7658 if (pointmark(markorg) == 0) {
7659 setpointmark(markorg, 1);
7663 /* Remove a bounding triangle from a convex hull triangle. */
7664 dissolve(dissolveedge);
7665 /* Find the next bounding triangle. */
7666 sym(deadtri, dissolveedge);
7667 /* Delete the bounding triangle. */
7668 triangledealloc(deadtri.tri);
7669 } while (!triedgeequal(dissolveedge, *startghost));
7673 /*****************************************************************************/
7675 /* divconqdelaunay() Form a Delaunay triangulation by the divide-and- */
7676 /* conquer method. */
7678 /* Sorts the points, calls a recursive procedure to triangulate them, and */
7679 /* removes the bounding box, setting boundary markers as appropriate. */
7681 /*****************************************************************************/
7683 long divconqdelaunay()
7686 struct triedge hullleft, hullright;
7690 /* Allocate an array of pointers to points for sorting. */
7691 sortarray = (point *) malloc(inpoints * sizeof(point));
7692 if (sortarray == (point *) NULL) {
7693 printf("Error: Out of memory.\n");
7696 traversalinit(&points);
7697 for (i = 0; i < inpoints; i++) {
7698 sortarray[i] = pointtraverse();
7701 printf(" Sorting points.\n");
7703 /* Sort the points. */
7704 pointsort(sortarray, inpoints);
7705 /* Discard duplicate points, which can really mess up the algorithm. */
7707 for (j = 1; j < inpoints; j++) {
7708 if ((sortarray[i][0] == sortarray[j][0])
7709 && (sortarray[i][1] == sortarray[j][1])) {
7712 "Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
7713 sortarray[j][0], sortarray[j][1]);
7715 /* Commented out - would eliminate point from output .node file, but causes
7716 a failure if some segment has this point as an endpoint.
7717 setpointmark(sortarray[j], DEADPOINT);
7721 sortarray[i] = sortarray[j];
7726 /* Re-sort the array of points to accommodate alternating cuts. */
7728 if (i - divider >= 2) {
7730 alternateaxes(sortarray, divider, 1);
7732 alternateaxes(&sortarray[divider], i - divider, 1);
7736 printf(" Forming triangulation.\n");
7738 /* Form the Delaunay triangulation. */
7739 divconqrecurse(sortarray, i, 0, &hullleft, &hullright);
7742 return removeghosts(&hullleft);
7747 /********* Divide-and-conquer Delaunay triangulation ends here *********/
7749 /********* Incremental Delaunay triangulation begins here *********/
7753 /*****************************************************************************/
7755 /* boundingbox() Form an "infinite" bounding triangle to insert points */
7758 /* The points at "infinity" are assigned finite coordinates, which are used */
7759 /* by the point location routines, but (mostly) ignored by the Delaunay */
7760 /* edge flip routines. */
7762 /*****************************************************************************/
7768 struct triedge inftri; /* Handle for the triangular bounding box. */
7772 printf(" Creating triangular bounding box.\n");
7774 /* Find the width (or height, whichever is larger) of the triangulation. */
7775 width = xmax - xmin;
7776 if (ymax - ymin > width) {
7777 width = ymax - ymin;
7782 /* Create the vertices of the bounding box. */
7783 infpoint1 = (point) malloc(points.itembytes);
7784 infpoint2 = (point) malloc(points.itembytes);
7785 infpoint3 = (point) malloc(points.itembytes);
7786 if ((infpoint1 == (point) NULL) || (infpoint2 == (point) NULL)
7787 || (infpoint3 == (point) NULL)) {
7788 printf("Error: Out of memory.\n");
7791 infpoint1[0] = xmin - 50.0 * width;
7792 infpoint1[1] = ymin - 40.0 * width;
7793 infpoint2[0] = xmax + 50.0 * width;
7794 infpoint2[1] = ymin - 40.0 * width;
7795 infpoint3[0] = 0.5 * (xmin + xmax);
7796 infpoint3[1] = ymax + 60.0 * width;
7798 /* Create the bounding box. */
7799 maketriangle(&inftri);
7800 setorg(inftri, infpoint1);
7801 setdest(inftri, infpoint2);
7802 setapex(inftri, infpoint3);
7803 /* Link dummytri to the bounding box so we can always find an */
7804 /* edge to begin searching (point location) from. */
7805 dummytri[0] = (triangle) inftri.tri;
7807 printf(" Creating ");
7808 printtriangle(&inftri);
7812 #endif /* not REDUCED */
7814 /*****************************************************************************/
7816 /* removebox() Remove the "infinite" bounding triangle, setting boundary */
7817 /* markers as appropriate. */
7819 /* The triangular bounding box has three boundary triangles (one for each */
7820 /* side of the bounding box), and a bunch of triangles fanning out from */
7821 /* the three bounding box vertices (one triangle for each edge of the */
7822 /* convex hull of the inner mesh). This routine removes these triangles. */
7824 /*****************************************************************************/
7830 struct triedge deadtri;
7831 struct triedge searchedge;
7832 struct triedge checkedge;
7833 struct triedge nextedge, finaledge, dissolveedge;
7836 triangle ptr; /* Temporary variable used by sym(). */
7839 printf(" Removing triangular bounding box.\n");
7841 /* Find a boundary triangle. */
7842 nextedge.tri = dummytri;
7843 nextedge.orient = 0;
7845 /* Mark a place to stop. */
7846 lprev(nextedge, finaledge);
7847 lnextself(nextedge);
7849 /* Find a triangle (on the boundary of the point set) that isn't */
7850 /* a bounding box triangle. */
7851 lprev(nextedge, searchedge);
7852 symself(searchedge);
7853 /* Check whether nextedge is another boundary triangle */
7854 /* adjacent to the first one. */
7855 lnext(nextedge, checkedge);
7857 if (checkedge.tri == dummytri) {
7858 /* Go on to the next triangle. There are only three boundary */
7859 /* triangles, and this next triangle cannot be the third one, */
7860 /* so it's safe to stop here. */
7861 lprevself(searchedge);
7862 symself(searchedge);
7864 /* Find a new boundary edge to search from, as the current search */
7865 /* edge lies on a bounding box triangle and will be deleted. */
7866 dummytri[0] = encode(searchedge);
7868 while (!triedgeequal(nextedge, finaledge)) {
7870 lprev(nextedge, dissolveedge);
7871 symself(dissolveedge);
7872 /* If not using a PSLG, the vertices should be marked now. */
7873 /* (If using a PSLG, markhull() will do the job.) */
7875 /* Be careful! One must check for the case where all the input */
7876 /* points are collinear, and thus all the triangles are part of */
7877 /* the bounding box. Otherwise, the setpointmark() call below */
7878 /* will cause a bad pointer reference. */
7879 if (dissolveedge.tri != dummytri) {
7880 org(dissolveedge, markorg);
7881 if (pointmark(markorg) == 0) {
7882 setpointmark(markorg, 1);
7886 /* Disconnect the bounding box triangle from the mesh triangle. */
7887 dissolve(dissolveedge);
7888 lnext(nextedge, deadtri);
7889 sym(deadtri, nextedge);
7890 /* Get rid of the bounding box triangle. */
7891 triangledealloc(deadtri.tri);
7892 /* Do we need to turn the corner? */
7893 if (nextedge.tri == dummytri) {
7894 /* Turn the corner. */
7895 triedgecopy(dissolveedge, nextedge);
7898 triangledealloc(finaledge.tri);
7900 free(infpoint1); /* Deallocate the bounding box vertices. */
7907 #endif /* not REDUCED */
7909 /*****************************************************************************/
7911 /* incrementaldelaunay() Form a Delaunay triangulation by incrementally */
7912 /* adding vertices. */
7914 /*****************************************************************************/
7918 long incrementaldelaunay()
7920 struct triedge starttri;
7924 /* Create a triangular bounding box. */
7927 printf(" Incrementally inserting points.\n");
7929 traversalinit(&points);
7930 pointloop = pointtraverse();
7932 while (pointloop != (point) NULL) {
7933 /* Find a boundary triangle to search from. */
7934 starttri.tri = (triangle *) NULL;
7935 if (insertsite(pointloop, &starttri, (struct edge *) NULL, 0, 0) ==
7939 "Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
7940 pointloop[0], pointloop[1]);
7942 /* Commented out - would eliminate point from output .node file.
7943 setpointmark(pointloop, DEADPOINT);
7946 pointloop = pointtraverse();
7949 /* Remove the bounding box. */
7953 #endif /* not REDUCED */
7957 /********* Incremental Delaunay triangulation ends here *********/
7959 /********* Sweepline Delaunay triangulation begins here *********/
7965 void eventheapinsert(heap, heapsize, newevent)
7966 struct event **heap;
7968 struct event *newevent;
7970 REAL eventx, eventy;
7975 eventx = newevent->xkey;
7976 eventy = newevent->ykey;
7977 eventnum = heapsize;
7978 notdone = eventnum > 0;
7980 parent = (eventnum - 1) >> 1;
7981 if ((heap[parent]->ykey < eventy) ||
7982 ((heap[parent]->ykey == eventy)
7983 && (heap[parent]->xkey <= eventx))) {
7986 heap[eventnum] = heap[parent];
7987 heap[eventnum]->heapposition = eventnum;
7990 notdone = eventnum > 0;
7993 heap[eventnum] = newevent;
7994 newevent->heapposition = eventnum;
7997 #endif /* not REDUCED */
8001 void eventheapify(heap, heapsize, eventnum)
8002 struct event **heap;
8006 struct event *thisevent;
8007 REAL eventx, eventy;
8008 int leftchild, rightchild;
8012 thisevent = heap[eventnum];
8013 eventx = thisevent->xkey;
8014 eventy = thisevent->ykey;
8015 leftchild = 2 * eventnum + 1;
8016 notdone = leftchild < heapsize;
8018 if ((heap[leftchild]->ykey < eventy) ||
8019 ((heap[leftchild]->ykey == eventy)
8020 && (heap[leftchild]->xkey < eventx))) {
8021 smallest = leftchild;
8023 smallest = eventnum;
8025 rightchild = leftchild + 1;
8026 if (rightchild < heapsize) {
8027 if ((heap[rightchild]->ykey < heap[smallest]->ykey) ||
8028 ((heap[rightchild]->ykey == heap[smallest]->ykey)
8029 && (heap[rightchild]->xkey < heap[smallest]->xkey))) {
8030 smallest = rightchild;
8033 if (smallest == eventnum) {
8036 heap[eventnum] = heap[smallest];
8037 heap[eventnum]->heapposition = eventnum;
8038 heap[smallest] = thisevent;
8039 thisevent->heapposition = smallest;
8041 eventnum = smallest;
8042 leftchild = 2 * eventnum + 1;
8043 notdone = leftchild < heapsize;
8048 #endif /* not REDUCED */
8052 void eventheapdelete(heap, heapsize, eventnum)
8053 struct event **heap;
8057 struct event *moveevent;
8058 REAL eventx, eventy;
8062 moveevent = heap[heapsize - 1];
8064 eventx = moveevent->xkey;
8065 eventy = moveevent->ykey;
8067 parent = (eventnum - 1) >> 1;
8068 if ((heap[parent]->ykey < eventy) ||
8069 ((heap[parent]->ykey == eventy)
8070 && (heap[parent]->xkey <= eventx))) {
8073 heap[eventnum] = heap[parent];
8074 heap[eventnum]->heapposition = eventnum;
8077 notdone = eventnum > 0;
8081 heap[eventnum] = moveevent;
8082 moveevent->heapposition = eventnum;
8083 eventheapify(heap, heapsize - 1, eventnum);
8086 #endif /* not REDUCED */
8090 void createeventheap(eventheap, events, freeevents)
8091 struct event ***eventheap;
8092 struct event **events;
8093 struct event **freeevents;
8099 maxevents = (3 * inpoints) / 2;
8100 *eventheap = (struct event **) malloc(maxevents * sizeof(struct event *));
8101 if (*eventheap == (struct event **) NULL) {
8102 printf("Error: Out of memory.\n");
8105 *events = (struct event *) malloc(maxevents * sizeof(struct event));
8106 if (*events == (struct event *) NULL) {
8107 printf("Error: Out of memory.\n");
8110 traversalinit(&points);
8111 for (i = 0; i < inpoints; i++) {
8112 thispoint = pointtraverse();
8113 (*events)[i].eventptr = (VOID *) thispoint;
8114 (*events)[i].xkey = thispoint[0];
8115 (*events)[i].ykey = thispoint[1];
8116 eventheapinsert(*eventheap, i, *events + i);
8118 *freeevents = (struct event *) NULL;
8119 for (i = maxevents - 1; i >= inpoints; i--) {
8120 (*events)[i].eventptr = (VOID *) *freeevents;
8121 *freeevents = *events + i;
8125 #endif /* not REDUCED */
8129 int rightofhyperbola(fronttri, newsite)
8130 struct triedge *fronttri;
8133 point leftpoint, rightpoint;
8134 REAL dxa, dya, dxb, dyb;
8138 dest(*fronttri, leftpoint);
8139 apex(*fronttri, rightpoint);
8140 if ((leftpoint[1] < rightpoint[1])
8141 || ((leftpoint[1] == rightpoint[1]) && (leftpoint[0] < rightpoint[0]))) {
8142 if (newsite[0] >= rightpoint[0]) {
8146 if (newsite[0] <= leftpoint[0]) {
8150 dxa = leftpoint[0] - newsite[0];
8151 dya = leftpoint[1] - newsite[1];
8152 dxb = rightpoint[0] - newsite[0];
8153 dyb = rightpoint[1] - newsite[1];
8154 return dya * (dxb * dxb + dyb * dyb) > dyb * (dxa * dxa + dya * dya);
8157 #endif /* not REDUCED */
8161 REAL circletop(pa, pb, pc, ccwabc)
8167 REAL xac, yac, xbc, ybc, xab, yab;
8168 REAL aclen2, bclen2, ablen2;
8172 xac = pa[0] - pc[0];
8173 yac = pa[1] - pc[1];
8174 xbc = pb[0] - pc[0];
8175 ybc = pb[1] - pc[1];
8176 xab = pa[0] - pb[0];
8177 yab = pa[1] - pb[1];
8178 aclen2 = xac * xac + yac * yac;
8179 bclen2 = xbc * xbc + ybc * ybc;
8180 ablen2 = xab * xab + yab * yab;
8181 return pc[1] + (xac * bclen2 - xbc * aclen2 + sqrt(aclen2 * bclen2 * ablen2))
8185 #endif /* not REDUCED */
8189 void check4deadevent(checktri, freeevents, eventheap, heapsize)
8190 struct triedge *checktri;
8191 struct event **freeevents;
8192 struct event **eventheap;
8195 struct event *deadevent;
8199 org(*checktri, eventpoint);
8200 if (eventpoint != (point) NULL) {
8201 deadevent = (struct event *) eventpoint;
8202 eventnum = deadevent->heapposition;
8203 deadevent->eventptr = (VOID *) *freeevents;
8204 *freeevents = deadevent;
8205 eventheapdelete(eventheap, *heapsize, eventnum);
8207 setorg(*checktri, NULL);
8211 #endif /* not REDUCED */
8215 struct splaynode *splay(splaytree, searchpoint, searchtri)
8216 struct splaynode *splaytree;
8218 struct triedge *searchtri;
8220 struct splaynode *child, *grandchild;
8221 struct splaynode *lefttree, *righttree;
8222 struct splaynode *leftright;
8224 int rightofroot, rightofchild;
8226 if (splaytree == (struct splaynode *) NULL) {
8227 return (struct splaynode *) NULL;
8229 dest(splaytree->keyedge, checkpoint);
8230 if (checkpoint == splaytree->keydest) {
8231 rightofroot = rightofhyperbola(&splaytree->keyedge, searchpoint);
8233 triedgecopy(splaytree->keyedge, *searchtri);
8234 child = splaytree->rchild;
8236 child = splaytree->lchild;
8238 if (child == (struct splaynode *) NULL) {
8241 dest(child->keyedge, checkpoint);
8242 if (checkpoint != child->keydest) {
8243 child = splay(child, searchpoint, searchtri);
8244 if (child == (struct splaynode *) NULL) {
8246 splaytree->rchild = (struct splaynode *) NULL;
8248 splaytree->lchild = (struct splaynode *) NULL;
8253 rightofchild = rightofhyperbola(&child->keyedge, searchpoint);
8255 triedgecopy(child->keyedge, *searchtri);
8256 grandchild = splay(child->rchild, searchpoint, searchtri);
8257 child->rchild = grandchild;
8259 grandchild = splay(child->lchild, searchpoint, searchtri);
8260 child->lchild = grandchild;
8262 if (grandchild == (struct splaynode *) NULL) {
8264 splaytree->rchild = child->lchild;
8265 child->lchild = splaytree;
8267 splaytree->lchild = child->rchild;
8268 child->rchild = splaytree;
8274 splaytree->rchild = child->lchild;
8275 child->lchild = splaytree;
8277 splaytree->lchild = grandchild->rchild;
8278 grandchild->rchild = splaytree;
8280 child->rchild = grandchild->lchild;
8281 grandchild->lchild = child;
8284 splaytree->rchild = grandchild->lchild;
8285 grandchild->lchild = splaytree;
8287 splaytree->lchild = child->rchild;
8288 child->rchild = splaytree;
8290 child->lchild = grandchild->rchild;
8291 grandchild->rchild = child;
8295 lefttree = splay(splaytree->lchild, searchpoint, searchtri);
8296 righttree = splay(splaytree->rchild, searchpoint, searchtri);
8298 pooldealloc(&splaynodes, (VOID *) splaytree);
8299 if (lefttree == (struct splaynode *) NULL) {
8301 } else if (righttree == (struct splaynode *) NULL) {
8303 } else if (lefttree->rchild == (struct splaynode *) NULL) {
8304 lefttree->rchild = righttree->lchild;
8305 righttree->lchild = lefttree;
8307 } else if (righttree->lchild == (struct splaynode *) NULL) {
8308 righttree->lchild = lefttree->rchild;
8309 lefttree->rchild = righttree;
8312 /* printf("Holy Toledo!!!\n"); */
8313 leftright = lefttree->rchild;
8314 while (leftright->rchild != (struct splaynode *) NULL) {
8315 leftright = leftright->rchild;
8317 leftright->rchild = righttree;
8323 #endif /* not REDUCED */
8327 struct splaynode *splayinsert(splayroot, newkey, searchpoint)
8328 struct splaynode *splayroot;
8329 struct triedge *newkey;
8332 struct splaynode *newsplaynode;
8334 newsplaynode = (struct splaynode *) poolalloc(&splaynodes);
8335 triedgecopy(*newkey, newsplaynode->keyedge);
8336 dest(*newkey, newsplaynode->keydest);
8337 if (splayroot == (struct splaynode *) NULL) {
8338 newsplaynode->lchild = (struct splaynode *) NULL;
8339 newsplaynode->rchild = (struct splaynode *) NULL;
8340 } else if (rightofhyperbola(&splayroot->keyedge, searchpoint)) {
8341 newsplaynode->lchild = splayroot;
8342 newsplaynode->rchild = splayroot->rchild;
8343 splayroot->rchild = (struct splaynode *) NULL;
8345 newsplaynode->lchild = splayroot->lchild;
8346 newsplaynode->rchild = splayroot;
8347 splayroot->lchild = (struct splaynode *) NULL;
8349 return newsplaynode;
8352 #endif /* not REDUCED */
8356 struct splaynode *circletopinsert(splayroot, newkey, pa, pb, pc, topy)
8357 struct splaynode *splayroot;
8358 struct triedge *newkey;
8365 REAL xac, yac, xbc, ybc;
8366 REAL aclen2, bclen2;
8367 REAL searchpoint[2];
8368 struct triedge dummytri;
8370 ccwabc = counterclockwise(pa, pb, pc);
8371 xac = pa[0] - pc[0];
8372 yac = pa[1] - pc[1];
8373 xbc = pb[0] - pc[0];
8374 ybc = pb[1] - pc[1];
8375 aclen2 = xac * xac + yac * yac;
8376 bclen2 = xbc * xbc + ybc * ybc;
8377 searchpoint[0] = pc[0] - (yac * bclen2 - ybc * aclen2) / (2.0 * ccwabc);
8378 searchpoint[1] = topy;
8379 return splayinsert(splay(splayroot, (point) searchpoint, &dummytri), newkey,
8380 (point) searchpoint);
8383 #endif /* not REDUCED */
8387 struct splaynode *frontlocate(splayroot, bottommost, searchpoint, searchtri,
8389 struct splaynode *splayroot;
8390 struct triedge *bottommost;
8392 struct triedge *searchtri;
8396 triangle ptr; /* Temporary variable used by onext(). */
8398 triedgecopy(*bottommost, *searchtri);
8399 splayroot = splay(splayroot, searchpoint, searchtri);
8402 while (!farrightflag && rightofhyperbola(searchtri, searchpoint)) {
8403 onextself(*searchtri);
8404 farrightflag = triedgeequal(*searchtri, *bottommost);
8406 *farright = farrightflag;
8410 #endif /* not REDUCED */
8414 long sweeplinedelaunay()
8416 struct event **eventheap;
8417 struct event *events;
8418 struct event *freeevents;
8419 struct event *nextevent;
8420 struct event *newevent;
8421 struct splaynode *splayroot;
8422 struct triedge bottommost;
8423 struct triedge searchtri;
8424 struct triedge fliptri;
8425 struct triedge lefttri, righttri, farlefttri, farrighttri;
8426 struct triedge inserttri;
8427 point firstpoint, secondpoint;
8428 point nextpoint, lastpoint;
8430 point leftpoint, midpoint, rightpoint;
8431 REAL lefttest, righttest;
8433 int check4events, farrightflag;
8434 triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
8436 poolinit(&splaynodes, sizeof(struct splaynode), SPLAYNODEPERBLOCK, POINTER,
8438 splayroot = (struct splaynode *) NULL;
8441 printf(" Placing points in event heap.\n");
8443 createeventheap(&eventheap, &events, &freeevents);
8444 heapsize = inpoints;
8447 printf(" Forming triangulation.\n");
8449 maketriangle(&lefttri);
8450 maketriangle(&righttri);
8451 bond(lefttri, righttri);
8453 lprevself(righttri);
8454 bond(lefttri, righttri);
8456 lprevself(righttri);
8457 bond(lefttri, righttri);
8458 firstpoint = (point) eventheap[0]->eventptr;
8459 eventheap[0]->eventptr = (VOID *) freeevents;
8460 freeevents = eventheap[0];
8461 eventheapdelete(eventheap, heapsize, 0);
8464 if (heapsize == 0) {
8465 printf("Error: Input points are all identical.\n");
8468 secondpoint = (point) eventheap[0]->eventptr;
8469 eventheap[0]->eventptr = (VOID *) freeevents;
8470 freeevents = eventheap[0];
8471 eventheapdelete(eventheap, heapsize, 0);
8473 if ((firstpoint[0] == secondpoint[0])
8474 && (firstpoint[1] == secondpoint[1])) {
8476 "Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
8477 secondpoint[0], secondpoint[1]);
8478 /* Commented out - would eliminate point from output .node file.
8479 setpointmark(secondpoint, DEADPOINT);
8482 } while ((firstpoint[0] == secondpoint[0])
8483 && (firstpoint[1] == secondpoint[1]));
8484 setorg(lefttri, firstpoint);
8485 setdest(lefttri, secondpoint);
8486 setorg(righttri, secondpoint);
8487 setdest(righttri, firstpoint);
8488 lprev(lefttri, bottommost);
8489 lastpoint = secondpoint;
8490 while (heapsize > 0) {
8491 nextevent = eventheap[0];
8492 eventheapdelete(eventheap, heapsize, 0);
8495 if (nextevent->xkey < xmin) {
8496 decode(nextevent->eventptr, fliptri);
8497 oprev(fliptri, farlefttri);
8498 check4deadevent(&farlefttri, &freeevents, eventheap, &heapsize);
8499 onext(fliptri, farrighttri);
8500 check4deadevent(&farrighttri, &freeevents, eventheap, &heapsize);
8502 if (triedgeequal(farlefttri, bottommost)) {
8503 lprev(fliptri, bottommost);
8506 setapex(fliptri, NULL);
8507 lprev(fliptri, lefttri);
8508 lnext(fliptri, righttri);
8509 sym(lefttri, farlefttri);
8511 if (randomnation(SAMPLERATE) == 0) {
8513 dest(fliptri, leftpoint);
8514 apex(fliptri, midpoint);
8515 org(fliptri, rightpoint);
8516 splayroot = circletopinsert(splayroot, &lefttri, leftpoint, midpoint,
8517 rightpoint, nextevent->ykey);
8520 nextpoint = (point) nextevent->eventptr;
8521 if ((nextpoint[0] == lastpoint[0]) && (nextpoint[1] == lastpoint[1])) {
8523 "Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
8524 nextpoint[0], nextpoint[1]);
8525 /* Commented out - would eliminate point from output .node file.
8526 setpointmark(nextpoint, DEADPOINT);
8530 lastpoint = nextpoint;
8532 splayroot = frontlocate(splayroot, &bottommost, nextpoint, &searchtri,
8535 triedgecopy(bottommost, searchtri);
8537 while (!farrightflag && rightofhyperbola(&searchtri, nextpoint)) {
8538 onextself(searchtri);
8539 farrightflag = triedgeequal(searchtri, bottommost);
8543 check4deadevent(&searchtri, &freeevents, eventheap, &heapsize);
8545 triedgecopy(searchtri, farrighttri);
8546 sym(searchtri, farlefttri);
8547 maketriangle(&lefttri);
8548 maketriangle(&righttri);
8549 dest(farrighttri, connectpoint);
8550 setorg(lefttri, connectpoint);
8551 setdest(lefttri, nextpoint);
8552 setorg(righttri, nextpoint);
8553 setdest(righttri, connectpoint);
8554 bond(lefttri, righttri);
8556 lprevself(righttri);
8557 bond(lefttri, righttri);
8559 lprevself(righttri);
8560 bond(lefttri, farlefttri);
8561 bond(righttri, farrighttri);
8562 if (!farrightflag && triedgeequal(farrighttri, bottommost)) {
8563 triedgecopy(lefttri, bottommost);
8566 if (randomnation(SAMPLERATE) == 0) {
8567 splayroot = splayinsert(splayroot, &lefttri, nextpoint);
8568 } else if (randomnation(SAMPLERATE) == 0) {
8569 lnext(righttri, inserttri);
8570 splayroot = splayinsert(splayroot, &inserttri, nextpoint);
8574 nextevent->eventptr = (VOID *) freeevents;
8575 freeevents = nextevent;
8578 apex(farlefttri, leftpoint);
8579 dest(lefttri, midpoint);
8580 apex(lefttri, rightpoint);
8581 lefttest = counterclockwise(leftpoint, midpoint, rightpoint);
8582 if (lefttest > 0.0) {
8583 newevent = freeevents;
8584 freeevents = (struct event *) freeevents->eventptr;
8585 newevent->xkey = xminextreme;
8586 newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
8588 newevent->eventptr = (VOID *) encode(lefttri);
8589 eventheapinsert(eventheap, heapsize, newevent);
8591 setorg(lefttri, newevent);
8593 apex(righttri, leftpoint);
8594 org(righttri, midpoint);
8595 apex(farrighttri, rightpoint);
8596 righttest = counterclockwise(leftpoint, midpoint, rightpoint);
8597 if (righttest > 0.0) {
8598 newevent = freeevents;
8599 freeevents = (struct event *) freeevents->eventptr;
8600 newevent->xkey = xminextreme;
8601 newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
8603 newevent->eventptr = (VOID *) encode(farrighttri);
8604 eventheapinsert(eventheap, heapsize, newevent);
8606 setorg(farrighttri, newevent);
8611 pooldeinit(&splaynodes);
8612 lprevself(bottommost);
8613 return removeghosts(&bottommost);
8616 #endif /* not REDUCED */
8620 /********* Sweepline Delaunay triangulation ends here *********/
8622 /********* General mesh construction routines begin here *********/
8626 /*****************************************************************************/
8628 /* delaunay() Form a Delaunay triangulation. */
8630 /*****************************************************************************/
8635 initializetrisegpools();
8640 "Constructing Delaunay triangulation by divide-and-conquer method.\n");
8642 return divconqdelaunay();
8643 #else /* not REDUCED */
8645 printf("Constructing Delaunay triangulation ");
8647 printf("by incremental method.\n");
8648 } else if (sweepline) {
8649 printf("by sweepline method.\n");
8651 printf("by divide-and-conquer method.\n");
8655 return incrementaldelaunay();
8656 } else if (sweepline) {
8657 return sweeplinedelaunay();
8659 return divconqdelaunay();
8661 #endif /* not REDUCED */
8664 /*****************************************************************************/
8666 /* reconstruct() Reconstruct a triangulation from its .ele (and possibly */
8667 /* .poly) file. Used when the -r switch is used. */
8669 /* Reads an .ele file and reconstructs the original mesh. If the -p switch */
8670 /* is used, this procedure will also read a .poly file and reconstruct the */
8671 /* shell edges of the original mesh. If the -a switch is used, this */
8672 /* procedure will also read an .area file and set a maximum area constraint */
8673 /* on each triangle. */
8675 /* Points that are not corners of triangles, such as nodes on edges of */
8676 /* subparametric elements, are discarded. */
8678 /* This routine finds the adjacencies between triangles (and shell edges) */
8679 /* by forming one stack of triangles for each vertex. Each triangle is on */
8680 /* three different stacks simultaneously. Each triangle's shell edge */
8681 /* pointers are used to link the items in each stack. This memory-saving */
8682 /* feature makes the code harder to read. The most important thing to keep */
8683 /* in mind is that each triangle is removed from a stack precisely when */
8684 /* the corresponding pointer is adjusted to refer to a shell edge rather */
8685 /* than the next triangle of the stack. */
8687 /*****************************************************************************/
8693 int reconstruct(trianglelist, triangleattriblist, trianglearealist, elements,
8694 corners, attribs, segmentlist, segmentmarkerlist,
8697 REAL *triangleattriblist;
8698 REAL *trianglearealist;
8703 int *segmentmarkerlist;
8704 int numberofsegments;
8706 #else /* not TRILIBRARY */
8708 long reconstruct(elefilename, areafilename, polyfilename, polyfile)
8714 #endif /* not TRILIBRARY */
8720 #else /* not TRILIBRARY */
8723 char inputline[INPUTLINESIZE];
8726 #endif /* not TRILIBRARY */
8727 struct triedge triangleloop;
8728 struct triedge triangleleft;
8729 struct triedge checktri;
8730 struct triedge checkleft;
8731 struct triedge checkneighbor;
8732 struct edge shelleloop;
8733 triangle *vertexarray;
8737 point checkdest, checkapex;
8750 int elementnumber, segmentnumber;
8752 triangle ptr; /* Temporary variable used by sym(). */
8755 inelements = elements;
8756 incorners = corners;
8757 if (incorners < 3) {
8758 printf("Error: Triangles must have at least 3 points.\n");
8762 #else /* not TRILIBRARY */
8763 /* Read the triangles from an .ele file. */
8765 printf("Opening %s.\n", elefilename);
8767 elefile = fopen(elefilename, "r");
8768 if (elefile == (FILE *) NULL) {
8769 printf(" Error: Cannot access file %s.\n", elefilename);
8772 /* Read number of triangles, number of points per triangle, and */
8773 /* number of triangle attributes from .ele file. */
8774 stringptr = readline(inputline, elefile, elefilename);
8775 inelements = (int) strtol (stringptr, &stringptr, 0);
8776 stringptr = findfield(stringptr);
8777 if (*stringptr == '\0') {
8780 incorners = (int) strtol (stringptr, &stringptr, 0);
8781 if (incorners < 3) {
8782 printf("Error: Triangles in %s must have at least 3 points.\n",
8787 stringptr = findfield(stringptr);
8788 if (*stringptr == '\0') {
8791 eextras = (int) strtol (stringptr, &stringptr, 0);
8793 #endif /* not TRILIBRARY */
8795 initializetrisegpools();
8797 /* Create the triangles. */
8798 for (elementnumber = 1; elementnumber <= inelements; elementnumber++) {
8799 maketriangle(&triangleloop);
8800 /* Mark the triangle as living. */
8801 triangleloop.tri[3] = (triangle) triangleloop.tri;
8806 insegments = numberofsegments;
8807 segmentmarkers = segmentmarkerlist != (int *) NULL;
8808 #else /* not TRILIBRARY */
8809 /* Read number of segments and number of segment */
8810 /* boundary markers from .poly file. */
8811 stringptr = readline(inputline, polyfile, inpolyfilename);
8812 insegments = (int) strtol (stringptr, &stringptr, 0);
8813 stringptr = findfield(stringptr);
8814 if (*stringptr == '\0') {
8817 segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
8819 #endif /* not TRILIBRARY */
8821 /* Create the shell edges. */
8822 for (segmentnumber = 1; segmentnumber <= insegments; segmentnumber++) {
8823 makeshelle(&shelleloop);
8824 /* Mark the shell edge as living. */
8825 shelleloop.sh[2] = (shelle) shelleloop.sh;
8832 #else /* not TRILIBRARY */
8834 /* Open an .area file, check for consistency with the .ele file. */
8836 printf("Opening %s.\n", areafilename);
8838 areafile = fopen(areafilename, "r");
8839 if (areafile == (FILE *) NULL) {
8840 printf(" Error: Cannot access file %s.\n", areafilename);
8843 stringptr = readline(inputline, areafile, areafilename);
8844 areaelements = (int) strtol (stringptr, &stringptr, 0);
8845 if (areaelements != inelements) {
8846 printf("Error: %s and %s disagree on number of triangles.\n",
8847 elefilename, areafilename);
8851 #endif /* not TRILIBRARY */
8854 printf("Reconstructing mesh.\n");
8856 /* Allocate a temporary array that maps each point to some adjacent */
8857 /* triangle. I took care to allocate all the permanent memory for */
8858 /* triangles and shell edges first. */
8859 vertexarray = (triangle *) malloc(points.items * sizeof(triangle));
8860 if (vertexarray == (triangle *) NULL) {
8861 printf("Error: Out of memory.\n");
8864 /* Each point is initially unrepresented. */
8865 for (i = 0; i < points.items; i++) {
8866 vertexarray[i] = (triangle) dummytri;
8870 printf(" Assembling triangles.\n");
8872 /* Read the triangles from the .ele file, and link */
8873 /* together those that share an edge. */
8874 traversalinit(&triangles);
8875 triangleloop.tri = triangletraverse();
8876 elementnumber = firstnumber;
8877 while (triangleloop.tri != (triangle *) NULL) {
8879 /* Copy the triangle's three corners. */
8880 for (j = 0; j < 3; j++) {
8881 corner[j] = trianglelist[pointindex++];
8882 if ((corner[j] < firstnumber) || (corner[j] >= firstnumber + inpoints)) {
8883 printf("Error: Triangle %d has an invalid vertex index.\n",
8888 #else /* not TRILIBRARY */
8889 /* Read triangle number and the triangle's three corners. */
8890 stringptr = readline(inputline, elefile, elefilename);
8891 for (j = 0; j < 3; j++) {
8892 stringptr = findfield(stringptr);
8893 if (*stringptr == '\0') {
8894 printf("Error: Triangle %d is missing point %d in %s.\n",
8895 elementnumber, j + 1, elefilename);
8898 corner[j] = (int) strtol (stringptr, &stringptr, 0);
8899 if ((corner[j] < firstnumber) ||
8900 (corner[j] >= firstnumber + inpoints)) {
8901 printf("Error: Triangle %d has an invalid vertex index.\n",
8907 #endif /* not TRILIBRARY */
8909 /* Find out about (and throw away) extra nodes. */
8910 for (j = 3; j < incorners; j++) {
8912 killpointindex = trianglelist[pointindex++];
8913 #else /* not TRILIBRARY */
8914 stringptr = findfield(stringptr);
8915 if (*stringptr != '\0') {
8916 killpointindex = (int) strtol (stringptr, &stringptr, 0);
8917 #endif /* not TRILIBRARY */
8918 if ((killpointindex >= firstnumber) &&
8919 (killpointindex < firstnumber + inpoints)) {
8920 /* Delete the non-corner point if it's not already deleted. */
8921 killpoint = getpoint(killpointindex);
8922 if (pointmark(killpoint) != DEADPOINT) {
8923 pointdealloc(killpoint);
8928 #endif /* not TRILIBRARY */
8931 /* Read the triangle's attributes. */
8932 for (j = 0; j < eextras; j++) {
8934 setelemattribute(triangleloop, j, triangleattriblist[attribindex++]);
8935 #else /* not TRILIBRARY */
8936 stringptr = findfield(stringptr);
8937 if (*stringptr == '\0') {
8938 setelemattribute(triangleloop, j, 0);
8940 setelemattribute(triangleloop, j,
8941 (REAL) strtod (stringptr, &stringptr));
8943 #endif /* not TRILIBRARY */
8948 area = trianglearealist[elementnumber - firstnumber];
8949 #else /* not TRILIBRARY */
8950 /* Read an area constraint from the .area file. */
8951 stringptr = readline(inputline, areafile, areafilename);
8952 stringptr = findfield(stringptr);
8953 if (*stringptr == '\0') {
8954 area = -1.0; /* No constraint on this triangle. */
8956 area = (REAL) strtod(stringptr, &stringptr);
8958 #endif /* not TRILIBRARY */
8959 setareabound(triangleloop, area);
8962 /* Set the triangle's vertices. */
8963 triangleloop.orient = 0;
8964 setorg(triangleloop, getpoint(corner[0]));
8965 setdest(triangleloop, getpoint(corner[1]));
8966 setapex(triangleloop, getpoint(corner[2]));
8967 /* Try linking the triangle to others that share these vertices. */
8968 for (triangleloop.orient = 0; triangleloop.orient < 3;
8969 triangleloop.orient++) {
8970 /* Take the number for the origin of triangleloop. */
8971 aroundpoint = corner[triangleloop.orient];
8972 /* Look for other triangles having this vertex. */
8973 nexttri = vertexarray[aroundpoint - firstnumber];
8974 /* Link the current triangle to the next one in the stack. */
8975 triangleloop.tri[6 + triangleloop.orient] = nexttri;
8976 /* Push the current triangle onto the stack. */
8977 vertexarray[aroundpoint - firstnumber] = encode(triangleloop);
8978 decode(nexttri, checktri);
8979 if (checktri.tri != dummytri) {
8980 dest(triangleloop, tdest);
8981 apex(triangleloop, tapex);
8982 /* Look for other triangles that share an edge. */
8984 dest(checktri, checkdest);
8985 apex(checktri, checkapex);
8986 if (tapex == checkdest) {
8987 /* The two triangles share an edge; bond them together. */
8988 lprev(triangleloop, triangleleft);
8989 bond(triangleleft, checktri);
8991 if (tdest == checkapex) {
8992 /* The two triangles share an edge; bond them together. */
8993 lprev(checktri, checkleft);
8994 bond(triangleloop, checkleft);
8996 /* Find the next triangle in the stack. */
8997 nexttri = checktri.tri[6 + checktri.orient];
8998 decode(nexttri, checktri);
8999 } while (checktri.tri != dummytri);
9002 triangleloop.tri = triangletraverse();
9008 #else /* not TRILIBRARY */
9013 #endif /* not TRILIBRARY */
9015 hullsize = 0; /* Prepare to count the boundary edges. */
9018 printf(" Marking segments in triangulation.\n");
9020 /* Read the segments from the .poly file, and link them */
9021 /* to their neighboring triangles. */
9023 traversalinit(&shelles);
9024 shelleloop.sh = shelletraverse();
9025 segmentnumber = firstnumber;
9026 while (shelleloop.sh != (shelle *) NULL) {
9028 end[0] = segmentlist[pointindex++];
9029 end[1] = segmentlist[pointindex++];
9030 if (segmentmarkers) {
9031 boundmarker = segmentmarkerlist[segmentnumber - firstnumber];
9033 #else /* not TRILIBRARY */
9034 /* Read the endpoints of each segment, and possibly a boundary marker. */
9035 stringptr = readline(inputline, polyfile, inpolyfilename);
9036 /* Skip the first (segment number) field. */
9037 stringptr = findfield(stringptr);
9038 if (*stringptr == '\0') {
9039 printf("Error: Segment %d has no endpoints in %s.\n", segmentnumber,
9043 end[0] = (int) strtol (stringptr, &stringptr, 0);
9045 stringptr = findfield(stringptr);
9046 if (*stringptr == '\0') {
9047 printf("Error: Segment %d is missing its second endpoint in %s.\n",
9048 segmentnumber, polyfilename);
9051 end[1] = (int) strtol (stringptr, &stringptr, 0);
9053 if (segmentmarkers) {
9054 stringptr = findfield(stringptr);
9055 if (*stringptr == '\0') {
9058 boundmarker = (int) strtol (stringptr, &stringptr, 0);
9061 #endif /* not TRILIBRARY */
9062 for (j = 0; j < 2; j++) {
9063 if ((end[j] < firstnumber) || (end[j] >= firstnumber + inpoints)) {
9064 printf("Error: Segment %d has an invalid vertex index.\n",
9070 /* set the shell edge's vertices. */
9071 shelleloop.shorient = 0;
9072 setsorg(shelleloop, getpoint(end[0]));
9073 setsdest(shelleloop, getpoint(end[1]));
9074 setmark(shelleloop, boundmarker);
9075 /* Try linking the shell edge to triangles that share these vertices. */
9076 for (shelleloop.shorient = 0; shelleloop.shorient < 2;
9077 shelleloop.shorient++) {
9078 /* Take the number for the destination of shelleloop. */
9079 aroundpoint = end[1 - shelleloop.shorient];
9080 /* Look for triangles having this vertex. */
9081 prevlink = &vertexarray[aroundpoint - firstnumber];
9082 nexttri = vertexarray[aroundpoint - firstnumber];
9083 decode(nexttri, checktri);
9084 sorg(shelleloop, shorg);
9086 /* Look for triangles having this edge. Note that I'm only */
9087 /* comparing each triangle's destination with the shell edge; */
9088 /* each triangle's apex is handled through a different vertex. */
9089 /* Because each triangle appears on three vertices' lists, each */
9090 /* occurrence of a triangle on a list can (and does) represent */
9091 /* an edge. In this way, most edges are represented twice, and */
9092 /* every triangle-segment bond is represented once. */
9093 while (notfound && (checktri.tri != dummytri)) {
9094 dest(checktri, checkdest);
9095 if (shorg == checkdest) {
9096 /* We have a match. Remove this triangle from the list. */
9097 *prevlink = checktri.tri[6 + checktri.orient];
9098 /* Bond the shell edge to the triangle. */
9099 tsbond(checktri, shelleloop);
9100 /* Check if this is a boundary edge. */
9101 sym(checktri, checkneighbor);
9102 if (checkneighbor.tri == dummytri) {
9103 /* The next line doesn't insert a shell edge (because there's */
9104 /* already one there), but it sets the boundary markers of */
9105 /* the existing shell edge and its vertices. */
9106 insertshelle(&checktri, 1);
9111 /* Find the next triangle in the stack. */
9112 prevlink = &checktri.tri[6 + checktri.orient];
9113 nexttri = checktri.tri[6 + checktri.orient];
9114 decode(nexttri, checktri);
9117 shelleloop.sh = shelletraverse();
9122 /* Mark the remaining edges as not being attached to any shell edge. */
9123 /* Also, count the (yet uncounted) boundary edges. */
9124 for (i = 0; i < points.items; i++) {
9125 /* Search the stack of triangles adjacent to a point. */
9126 nexttri = vertexarray[i];
9127 decode(nexttri, checktri);
9128 while (checktri.tri != dummytri) {
9129 /* Find the next triangle in the stack before this */
9130 /* information gets overwritten. */
9131 nexttri = checktri.tri[6 + checktri.orient];
9132 /* No adjacent shell edge. (This overwrites the stack info.) */
9133 tsdissolve(checktri);
9134 sym(checktri, checkneighbor);
9135 if (checkneighbor.tri == dummytri) {
9136 insertshelle(&checktri, 1);
9139 decode(nexttri, checktri);
9147 #endif /* not CDT_ONLY */
9151 /********* General mesh construction routines end here *********/
9153 /********* Segment (shell edge) insertion begins here *********/
9157 /*****************************************************************************/
9159 /* finddirection() Find the first triangle on the path from one point */
9162 /* Finds the triangle that intersects a line segment drawn from the */
9163 /* origin of `searchtri' to the point `endpoint', and returns the result */
9164 /* in `searchtri'. The origin of `searchtri' does not change, even though */
9165 /* the triangle returned may differ from the one passed in. This routine */
9166 /* is used to find the direction to move in to get from one point to */
9169 /* The return value notes whether the destination or apex of the found */
9170 /* triangle is collinear with the two points in question. */
9172 /*****************************************************************************/
9174 enum finddirectionresult finddirection(searchtri, endpoint)
9175 struct triedge *searchtri;
9178 struct triedge checktri;
9180 point leftpoint, rightpoint;
9181 REAL leftccw, rightccw;
9182 int leftflag, rightflag;
9183 triangle ptr; /* Temporary variable used by onext() and oprev(). */
9185 org(*searchtri, startpoint);
9186 dest(*searchtri, rightpoint);
9187 apex(*searchtri, leftpoint);
9188 /* Is `endpoint' to the left? */
9189 leftccw = counterclockwise(endpoint, startpoint, leftpoint);
9190 leftflag = leftccw > 0.0;
9191 /* Is `endpoint' to the right? */
9192 rightccw = counterclockwise(startpoint, endpoint, rightpoint);
9193 rightflag = rightccw > 0.0;
9194 if (leftflag && rightflag) {
9195 /* `searchtri' faces directly away from `endpoint'. We could go */
9196 /* left or right. Ask whether it's a triangle or a boundary */
9198 onext(*searchtri, checktri);
9199 if (checktri.tri == dummytri) {
9206 /* Turn left until satisfied. */
9207 onextself(*searchtri);
9208 if (searchtri->tri == dummytri) {
9209 printf("Internal error in finddirection(): Unable to find a\n");
9210 printf(" triangle leading from (%.12g, %.12g) to", startpoint[0],
9212 printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
9215 apex(*searchtri, leftpoint);
9217 leftccw = counterclockwise(endpoint, startpoint, leftpoint);
9218 leftflag = leftccw > 0.0;
9221 /* Turn right until satisfied. */
9222 oprevself(*searchtri);
9223 if (searchtri->tri == dummytri) {
9224 printf("Internal error in finddirection(): Unable to find a\n");
9225 printf(" triangle leading from (%.12g, %.12g) to", startpoint[0],
9227 printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
9230 dest(*searchtri, rightpoint);
9232 rightccw = counterclockwise(startpoint, endpoint, rightpoint);
9233 rightflag = rightccw > 0.0;
9235 if (leftccw == 0.0) {
9236 return LEFTCOLLINEAR;
9237 } else if (rightccw == 0.0) {
9238 return RIGHTCOLLINEAR;
9244 /*****************************************************************************/
9246 /* segmentintersection() Find the intersection of an existing segment */
9247 /* and a segment that is being inserted. Insert */
9248 /* a point at the intersection, splitting an */
9249 /* existing shell edge. */
9251 /* The segment being inserted connects the apex of splittri to endpoint2. */
9252 /* splitshelle is the shell edge being split, and MUST be opposite */
9253 /* splittri. Hence, the edge being split connects the origin and */
9254 /* destination of splittri. */
9256 /* On completion, splittri is a handle having the newly inserted */
9257 /* intersection point as its origin, and endpoint1 as its destination. */
9259 /*****************************************************************************/
9261 void segmentintersection(splittri, splitshelle, endpoint2)
9262 struct triedge *splittri;
9263 struct edge *splitshelle;
9268 point leftpoint, rightpoint;
9270 enum insertsiteresult success;
9271 enum finddirectionresult collinear;
9277 triangle ptr; /* Temporary variable used by onext(). */
9279 /* Find the other three segment endpoints. */
9280 apex(*splittri, endpoint1);
9281 org(*splittri, torg);
9282 dest(*splittri, tdest);
9283 /* Segment intersection formulae; see the Antonio reference. */
9284 tx = tdest[0] - torg[0];
9285 ty = tdest[1] - torg[1];
9286 ex = endpoint2[0] - endpoint1[0];
9287 ey = endpoint2[1] - endpoint1[1];
9288 etx = torg[0] - endpoint2[0];
9289 ety = torg[1] - endpoint2[1];
9290 denom = ty * ex - tx * ey;
9292 printf("Internal error in segmentintersection():");
9293 printf(" Attempt to find intersection of parallel segments.\n");
9296 split = (ey * etx - ex * ety) / denom;
9297 /* Create the new point. */
9298 newpoint = (point) poolalloc(&points);
9299 /* Interpolate its coordinate and attributes. */
9300 for (i = 0; i < 2 + nextras; i++) {
9301 newpoint[i] = torg[i] + split * (tdest[i] - torg[i]);
9303 setpointmark(newpoint, mark(*splitshelle));
9306 " Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
9307 torg[0], torg[1], tdest[0], tdest[1], newpoint[0], newpoint[1]);
9309 /* Insert the intersection point. This should always succeed. */
9310 success = insertsite(newpoint, splittri, splitshelle, 0, 0);
9311 if (success != SUCCESSFULPOINT) {
9312 printf("Internal error in segmentintersection():\n");
9313 printf(" Failure to split a segment.\n");
9316 if (steinerleft > 0) {
9319 /* Inserting the point may have caused edge flips. We wish to rediscover */
9320 /* the edge connecting endpoint1 to the new intersection point. */
9321 collinear = finddirection(splittri, endpoint1);
9322 dest(*splittri, rightpoint);
9323 apex(*splittri, leftpoint);
9324 if ((leftpoint[0] == endpoint1[0]) && (leftpoint[1] == endpoint1[1])) {
9325 onextself(*splittri);
9326 } else if ((rightpoint[0] != endpoint1[0]) ||
9327 (rightpoint[1] != endpoint1[1])) {
9328 printf("Internal error in segmentintersection():\n");
9329 printf(" Topological inconsistency after splitting a segment.\n");
9332 /* `splittri' should have destination endpoint1. */
9335 /*****************************************************************************/
9337 /* scoutsegment() Scout the first triangle on the path from one endpoint */
9338 /* to another, and check for completion (reaching the */
9339 /* second endpoint), a collinear point, and the */
9340 /* intersection of two segments. */
9342 /* Returns one if the entire segment is successfully inserted, and zero if */
9343 /* the job must be finished by conformingedge() or constrainededge(). */
9345 /* If the first triangle on the path has the second endpoint as its */
9346 /* destination or apex, a shell edge is inserted and the job is done. */
9348 /* If the first triangle on the path has a destination or apex that lies on */
9349 /* the segment, a shell edge is inserted connecting the first endpoint to */
9350 /* the collinear point, and the search is continued from the collinear */
9353 /* If the first triangle on the path has a shell edge opposite its origin, */
9354 /* then there is a segment that intersects the segment being inserted. */
9355 /* Their intersection point is inserted, splitting the shell edge. */
9357 /* Otherwise, return zero. */
9359 /*****************************************************************************/
9361 int scoutsegment(searchtri, endpoint2, newmark)
9362 struct triedge *searchtri;
9366 struct triedge crosstri;
9367 struct edge crossedge;
9368 point leftpoint, rightpoint;
9370 enum finddirectionresult collinear;
9371 shelle sptr; /* Temporary variable used by tspivot(). */
9373 collinear = finddirection(searchtri, endpoint2);
9374 dest(*searchtri, rightpoint);
9375 apex(*searchtri, leftpoint);
9376 if (((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) ||
9377 ((rightpoint[0] == endpoint2[0]) && (rightpoint[1] == endpoint2[1]))) {
9378 /* The segment is already an edge in the mesh. */
9379 if ((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) {
9380 lprevself(*searchtri);
9382 /* Insert a shell edge, if there isn't already one there. */
9383 insertshelle(searchtri, newmark);
9385 } else if (collinear == LEFTCOLLINEAR) {
9386 /* We've collided with a point between the segment's endpoints. */
9387 /* Make the collinear point be the triangle's origin. */
9388 lprevself(*searchtri);
9389 insertshelle(searchtri, newmark);
9390 /* Insert the remainder of the segment. */
9391 return scoutsegment(searchtri, endpoint2, newmark);
9392 } else if (collinear == RIGHTCOLLINEAR) {
9393 /* We've collided with a point between the segment's endpoints. */
9394 insertshelle(searchtri, newmark);
9395 /* Make the collinear point be the triangle's origin. */
9396 lnextself(*searchtri);
9397 /* Insert the remainder of the segment. */
9398 return scoutsegment(searchtri, endpoint2, newmark);
9400 lnext(*searchtri, crosstri);
9401 tspivot(crosstri, crossedge);
9402 /* Check for a crossing segment. */
9403 if (crossedge.sh == dummysh) {
9406 org(*searchtri, endpoint1);
9407 /* Insert a point at the intersection. */
9408 segmentintersection(&crosstri, &crossedge, endpoint2);
9409 triedgecopy(crosstri, *searchtri);
9410 insertshelle(searchtri, newmark);
9411 /* Insert the remainder of the segment. */
9412 return scoutsegment(searchtri, endpoint2, newmark);
9417 /*****************************************************************************/
9419 /* conformingedge() Force a segment into a conforming Delaunay */
9420 /* triangulation by inserting a point at its midpoint, */
9421 /* and recursively forcing in the two half-segments if */
9424 /* Generates a sequence of edges connecting `endpoint1' to `endpoint2'. */
9425 /* `newmark' is the boundary marker of the segment, assigned to each new */
9426 /* splitting point and shell edge. */
9428 /* Note that conformingedge() does not always maintain the conforming */
9429 /* Delaunay property. Once inserted, segments are locked into place; */
9430 /* points inserted later (to force other segments in) may render these */
9431 /* fixed segments non-Delaunay. The conforming Delaunay property will be */
9432 /* restored by enforcequality() by splitting encroached segments. */
9434 /*****************************************************************************/
9439 void conformingedge(endpoint1, endpoint2, newmark)
9444 struct triedge searchtri1, searchtri2;
9445 struct edge brokenshelle;
9447 point midpoint1, midpoint2;
9448 enum insertsiteresult success;
9449 int result1, result2;
9451 shelle sptr; /* Temporary variable used by tspivot(). */
9454 printf("Forcing segment into triangulation by recursive splitting:\n");
9455 printf(" (%.12g, %.12g) (%.12g, %.12g)\n", endpoint1[0], endpoint1[1],
9456 endpoint2[0], endpoint2[1]);
9458 /* Create a new point to insert in the middle of the segment. */
9459 newpoint = (point) poolalloc(&points);
9460 /* Interpolate coordinates and attributes. */
9461 for (i = 0; i < 2 + nextras; i++) {
9462 newpoint[i] = 0.5 * (endpoint1[i] + endpoint2[i]);
9464 setpointmark(newpoint, newmark);
9465 /* Find a boundary triangle to search from. */
9466 searchtri1.tri = (triangle *) NULL;
9467 /* Attempt to insert the new point. */
9468 success = insertsite(newpoint, &searchtri1, (struct edge *) NULL, 0, 0);
9469 if (success == DUPLICATEPOINT) {
9471 printf(" Segment intersects existing point (%.12g, %.12g).\n",
9472 newpoint[0], newpoint[1]);
9474 /* Use the point that's already there. */
9475 pointdealloc(newpoint);
9476 org(searchtri1, newpoint);
9478 if (success == VIOLATINGPOINT) {
9480 printf(" Two segments intersect at (%.12g, %.12g).\n",
9481 newpoint[0], newpoint[1]);
9483 /* By fluke, we've landed right on another segment. Split it. */
9484 tspivot(searchtri1, brokenshelle);
9485 success = insertsite(newpoint, &searchtri1, &brokenshelle, 0, 0);
9486 if (success != SUCCESSFULPOINT) {
9487 printf("Internal error in conformingedge():\n");
9488 printf(" Failure to split a segment.\n");
9492 /* The point has been inserted successfully. */
9493 if (steinerleft > 0) {
9497 triedgecopy(searchtri1, searchtri2);
9498 result1 = scoutsegment(&searchtri1, endpoint1, newmark);
9499 result2 = scoutsegment(&searchtri2, endpoint2, newmark);
9501 /* The origin of searchtri1 may have changed if a collision with an */
9502 /* intervening vertex on the segment occurred. */
9503 org(searchtri1, midpoint1);
9504 conformingedge(midpoint1, endpoint1, newmark);
9507 /* The origin of searchtri2 may have changed if a collision with an */
9508 /* intervening vertex on the segment occurred. */
9509 org(searchtri2, midpoint2);
9510 conformingedge(midpoint2, endpoint2, newmark);
9514 #endif /* not CDT_ONLY */
9515 #endif /* not REDUCED */
9517 /*****************************************************************************/
9519 /* delaunayfixup() Enforce the Delaunay condition at an edge, fanning out */
9520 /* recursively from an existing point. Pay special */
9521 /* attention to stacking inverted triangles. */
9523 /* This is a support routine for inserting segments into a constrained */
9524 /* Delaunay triangulation. */
9526 /* The origin of fixuptri is treated as if it has just been inserted, and */
9527 /* the local Delaunay condition needs to be enforced. It is only enforced */
9528 /* in one sector, however, that being the angular range defined by */
9531 /* This routine also needs to make decisions regarding the "stacking" of */
9532 /* triangles. (Read the description of constrainededge() below before */
9533 /* reading on here, so you understand the algorithm.) If the position of */
9534 /* the new point (the origin of fixuptri) indicates that the vertex before */
9535 /* it on the polygon is a reflex vertex, then "stack" the triangle by */
9536 /* doing nothing. (fixuptri is an inverted triangle, which is how stacked */
9537 /* triangles are identified.) */
9539 /* Otherwise, check whether the vertex before that was a reflex vertex. */
9540 /* If so, perform an edge flip, thereby eliminating an inverted triangle */
9541 /* (popping it off the stack). The edge flip may result in the creation */
9542 /* of a new inverted triangle, depending on whether or not the new vertex */
9543 /* is visible to the vertex three edges behind on the polygon. */
9545 /* If neither of the two vertices behind the new vertex are reflex */
9546 /* vertices, fixuptri and fartri, the triangle opposite it, are not */
9547 /* inverted; hence, ensure that the edge between them is locally Delaunay. */
9549 /* `leftside' indicates whether or not fixuptri is to the left of the */
9550 /* segment being inserted. (Imagine that the segment is pointing up from */
9551 /* endpoint1 to endpoint2.) */
9553 /*****************************************************************************/
9555 void delaunayfixup(fixuptri, leftside)
9556 struct triedge *fixuptri;
9559 struct triedge neartri;
9560 struct triedge fartri;
9561 struct edge faredge;
9562 point nearpoint, leftpoint, rightpoint, farpoint;
9563 triangle ptr; /* Temporary variable used by sym(). */
9564 shelle sptr; /* Temporary variable used by tspivot(). */
9566 lnext(*fixuptri, neartri);
9567 sym(neartri, fartri);
9568 /* Check if the edge opposite the origin of fixuptri can be flipped. */
9569 if (fartri.tri == dummytri) {
9572 tspivot(neartri, faredge);
9573 if (faredge.sh != dummysh) {
9576 /* Find all the relevant vertices. */
9577 apex(neartri, nearpoint);
9578 org(neartri, leftpoint);
9579 dest(neartri, rightpoint);
9580 apex(fartri, farpoint);
9581 /* Check whether the previous polygon vertex is a reflex vertex. */
9583 if (counterclockwise(nearpoint, leftpoint, farpoint) <= 0.0) {
9584 /* leftpoint is a reflex vertex too. Nothing can */
9585 /* be done until a convex section is found. */
9589 if (counterclockwise(farpoint, rightpoint, nearpoint) <= 0.0) {
9590 /* rightpoint is a reflex vertex too. Nothing can */
9591 /* be done until a convex section is found. */
9595 if (counterclockwise(rightpoint, leftpoint, farpoint) > 0.0) {
9596 /* fartri is not an inverted triangle, and farpoint is not a reflex */
9597 /* vertex. As there are no reflex vertices, fixuptri isn't an */
9598 /* inverted triangle, either. Hence, test the edge between the */
9599 /* triangles to ensure it is locally Delaunay. */
9600 if (incircle(leftpoint, farpoint, rightpoint, nearpoint) <= 0.0) {
9603 /* Not locally Delaunay; go on to an edge flip. */
9604 } /* else fartri is inverted; remove it from the stack by flipping. */
9606 lprevself(*fixuptri); /* Restore the origin of fixuptri after the flip. */
9607 /* Recursively process the two triangles that result from the flip. */
9608 delaunayfixup(fixuptri, leftside);
9609 delaunayfixup(&fartri, leftside);
9612 /*****************************************************************************/
9614 /* constrainededge() Force a segment into a constrained Delaunay */
9615 /* triangulation by deleting the triangles it */
9616 /* intersects, and triangulating the polygons that */
9617 /* form on each side of it. */
9619 /* Generates a single edge connecting `endpoint1' to `endpoint2'. The */
9620 /* triangle `starttri' has `endpoint1' as its origin. `newmark' is the */
9621 /* boundary marker of the segment. */
9623 /* To insert a segment, every triangle whose interior intersects the */
9624 /* segment is deleted. The union of these deleted triangles is a polygon */
9625 /* (which is not necessarily monotone, but is close enough), which is */
9626 /* divided into two polygons by the new segment. This routine's task is */
9627 /* to generate the Delaunay triangulation of these two polygons. */
9629 /* You might think of this routine's behavior as a two-step process. The */
9630 /* first step is to walk from endpoint1 to endpoint2, flipping each edge */
9631 /* encountered. This step creates a fan of edges connected to endpoint1, */
9632 /* including the desired edge to endpoint2. The second step enforces the */
9633 /* Delaunay condition on each side of the segment in an incremental manner: */
9634 /* proceeding along the polygon from endpoint1 to endpoint2 (this is done */
9635 /* independently on each side of the segment), each vertex is "enforced" */
9636 /* as if it had just been inserted, but affecting only the previous */
9637 /* vertices. The result is the same as if the vertices had been inserted */
9638 /* in the order they appear on the polygon, so the result is Delaunay. */
9640 /* In truth, constrainededge() interleaves these two steps. The procedure */
9641 /* walks from endpoint1 to endpoint2, and each time an edge is encountered */
9642 /* and flipped, the newly exposed vertex (at the far end of the flipped */
9643 /* edge) is "enforced" upon the previously flipped edges, usually affecting */
9644 /* only one side of the polygon (depending upon which side of the segment */
9645 /* the vertex falls on). */
9647 /* The algorithm is complicated by the need to handle polygons that are not */
9648 /* convex. Although the polygon is not necessarily monotone, it can be */
9649 /* triangulated in a manner similar to the stack-based algorithms for */
9650 /* monotone polygons. For each reflex vertex (local concavity) of the */
9651 /* polygon, there will be an inverted triangle formed by one of the edge */
9652 /* flips. (An inverted triangle is one with negative area - that is, its */
9653 /* vertices are arranged in clockwise order - and is best thought of as a */
9654 /* wrinkle in the fabric of the mesh.) Each inverted triangle can be */
9655 /* thought of as a reflex vertex pushed on the stack, waiting to be fixed */
9658 /* A reflex vertex is popped from the stack when a vertex is inserted that */
9659 /* is visible to the reflex vertex. (However, if the vertex behind the */
9660 /* reflex vertex is not visible to the reflex vertex, a new inverted */
9661 /* triangle will take its place on the stack.) These details are handled */
9662 /* by the delaunayfixup() routine above. */
9664 /*****************************************************************************/
9666 void constrainededge(starttri, endpoint2, newmark)
9667 struct triedge *starttri;
9671 struct triedge fixuptri, fixuptri2;
9672 struct edge fixupedge;
9678 triangle ptr; /* Temporary variable used by sym() and oprev(). */
9679 shelle sptr; /* Temporary variable used by tspivot(). */
9681 org(*starttri, endpoint1);
9682 lnext(*starttri, fixuptri);
9684 /* `collision' indicates whether we have found a point directly */
9685 /* between endpoint1 and endpoint2. */
9689 org(fixuptri, farpoint);
9690 /* `farpoint' is the extreme point of the polygon we are "digging" */
9691 /* to get from endpoint1 to endpoint2. */
9692 if ((farpoint[0] == endpoint2[0]) && (farpoint[1] == endpoint2[1])) {
9693 oprev(fixuptri, fixuptri2);
9694 /* Enforce the Delaunay condition around endpoint2. */
9695 delaunayfixup(&fixuptri, 0);
9696 delaunayfixup(&fixuptri2, 1);
9699 /* Check whether farpoint is to the left or right of the segment */
9700 /* being inserted, to decide which edge of fixuptri to dig */
9702 area = counterclockwise(endpoint1, endpoint2, farpoint);
9704 /* We've collided with a point between endpoint1 and endpoint2. */
9706 oprev(fixuptri, fixuptri2);
9707 /* Enforce the Delaunay condition around farpoint. */
9708 delaunayfixup(&fixuptri, 0);
9709 delaunayfixup(&fixuptri2, 1);
9712 if (area > 0.0) { /* farpoint is to the left of the segment. */
9713 oprev(fixuptri, fixuptri2);
9714 /* Enforce the Delaunay condition around farpoint, on the */
9715 /* left side of the segment only. */
9716 delaunayfixup(&fixuptri2, 1);
9717 /* Flip the edge that crosses the segment. After the edge is */
9718 /* flipped, one of its endpoints is the fan vertex, and the */
9719 /* destination of fixuptri is the fan vertex. */
9720 lprevself(fixuptri);
9721 } else { /* farpoint is to the right of the segment. */
9722 delaunayfixup(&fixuptri, 0);
9723 /* Flip the edge that crosses the segment. After the edge is */
9724 /* flipped, one of its endpoints is the fan vertex, and the */
9725 /* destination of fixuptri is the fan vertex. */
9726 oprevself(fixuptri);
9728 /* Check for two intersecting segments. */
9729 tspivot(fixuptri, fixupedge);
9730 if (fixupedge.sh == dummysh) {
9731 flip(&fixuptri); /* May create an inverted triangle on the left. */
9733 /* We've collided with a segment between endpoint1 and endpoint2. */
9735 /* Insert a point at the intersection. */
9736 segmentintersection(&fixuptri, &fixupedge, endpoint2);
9742 /* Insert a shell edge to make the segment permanent. */
9743 insertshelle(&fixuptri, newmark);
9744 /* If there was a collision with an interceding vertex, install another */
9745 /* segment connecting that vertex with endpoint2. */
9747 /* Insert the remainder of the segment. */
9748 if (!scoutsegment(&fixuptri, endpoint2, newmark)) {
9749 constrainededge(&fixuptri, endpoint2, newmark);
9754 /*****************************************************************************/
9756 /* insertsegment() Insert a PSLG segment into a triangulation. */
9758 /*****************************************************************************/
9760 void insertsegment(endpoint1, endpoint2, newmark)
9765 struct triedge searchtri1, searchtri2;
9766 triangle encodedtri;
9768 triangle ptr; /* Temporary variable used by sym(). */
9771 printf(" Connecting (%.12g, %.12g) to (%.12g, %.12g).\n",
9772 endpoint1[0], endpoint1[1], endpoint2[0], endpoint2[1]);
9775 /* Find a triangle whose origin is the segment's first endpoint. */
9776 checkpoint = (point) NULL;
9777 encodedtri = point2tri(endpoint1);
9778 if (encodedtri != (triangle) NULL) {
9779 decode(encodedtri, searchtri1);
9780 org(searchtri1, checkpoint);
9782 if (checkpoint != endpoint1) {
9783 /* Find a boundary triangle to search from. */
9784 searchtri1.tri = dummytri;
9785 searchtri1.orient = 0;
9786 symself(searchtri1);
9787 /* Search for the segment's first endpoint by point location. */
9788 if (locate(endpoint1, &searchtri1) != ONVERTEX) {
9790 "Internal error in insertsegment(): Unable to locate PSLG point\n");
9791 printf(" (%.12g, %.12g) in triangulation.\n",
9792 endpoint1[0], endpoint1[1]);
9796 /* Remember this triangle to improve subsequent point location. */
9797 triedgecopy(searchtri1, recenttri);
9798 /* Scout the beginnings of a path from the first endpoint */
9799 /* toward the second. */
9800 if (scoutsegment(&searchtri1, endpoint2, newmark)) {
9801 /* The segment was easily inserted. */
9804 /* The first endpoint may have changed if a collision with an intervening */
9805 /* vertex on the segment occurred. */
9806 org(searchtri1, endpoint1);
9808 /* Find a triangle whose origin is the segment's second endpoint. */
9809 checkpoint = (point) NULL;
9810 encodedtri = point2tri(endpoint2);
9811 if (encodedtri != (triangle) NULL) {
9812 decode(encodedtri, searchtri2);
9813 org(searchtri2, checkpoint);
9815 if (checkpoint != endpoint2) {
9816 /* Find a boundary triangle to search from. */
9817 searchtri2.tri = dummytri;
9818 searchtri2.orient = 0;
9819 symself(searchtri2);
9820 /* Search for the segment's second endpoint by point location. */
9821 if (locate(endpoint2, &searchtri2) != ONVERTEX) {
9823 "Internal error in insertsegment(): Unable to locate PSLG point\n");
9824 printf(" (%.12g, %.12g) in triangulation.\n",
9825 endpoint2[0], endpoint2[1]);
9829 /* Remember this triangle to improve subsequent point location. */
9830 triedgecopy(searchtri2, recenttri);
9831 /* Scout the beginnings of a path from the second endpoint */
9832 /* toward the first. */
9833 if (scoutsegment(&searchtri2, endpoint1, newmark)) {
9834 /* The segment was easily inserted. */
9837 /* The second endpoint may have changed if a collision with an intervening */
9838 /* vertex on the segment occurred. */
9839 org(searchtri2, endpoint2);
9844 /* Insert vertices to force the segment into the triangulation. */
9845 conformingedge(endpoint1, endpoint2, newmark);
9847 #endif /* not CDT_ONLY */
9848 #endif /* not REDUCED */
9849 /* Insert the segment directly into the triangulation. */
9850 constrainededge(&searchtri1, endpoint2, newmark);
9854 #endif /* not CDT_ONLY */
9855 #endif /* not REDUCED */
9858 /*****************************************************************************/
9860 /* markhull() Cover the convex hull of a triangulation with shell edges. */
9862 /*****************************************************************************/
9866 struct triedge hulltri;
9867 struct triedge nexttri;
9868 struct triedge starttri;
9869 triangle ptr; /* Temporary variable used by sym() and oprev(). */
9871 /* Find a triangle handle on the hull. */
9872 hulltri.tri = dummytri;
9875 /* Remember where we started so we know when to stop. */
9876 triedgecopy(hulltri, starttri);
9877 /* Go once counterclockwise around the convex hull. */
9879 /* Create a shell edge if there isn't already one here. */
9880 insertshelle(&hulltri, 1);
9881 /* To find the next hull edge, go clockwise around the next vertex. */
9883 oprev(hulltri, nexttri);
9884 while (nexttri.tri != dummytri) {
9885 triedgecopy(nexttri, hulltri);
9886 oprev(hulltri, nexttri);
9888 } while (!triedgeequal(hulltri, starttri));
9891 /*****************************************************************************/
9893 /* formskeleton() Create the shell edges of a triangulation, including */
9894 /* PSLG edges and edges on the convex hull. */
9896 /* The PSLG edges are read from a .poly file. The return value is the */
9897 /* number of segments in the file. */
9899 /*****************************************************************************/
9903 int formskeleton(segmentlist, segmentmarkerlist, numberofsegments)
9905 int *segmentmarkerlist;
9906 int numberofsegments;
9908 #else /* not TRILIBRARY */
9910 int formskeleton(polyfile, polyfilename)
9914 #endif /* not TRILIBRARY */
9918 char polyfilename[6];
9920 #else /* not TRILIBRARY */
9921 char inputline[INPUTLINESIZE];
9923 #endif /* not TRILIBRARY */
9924 point endpoint1, endpoint2;
9933 printf("Inserting segments into Delaunay triangulation.\n");
9936 strcpy(polyfilename, "input");
9937 segments = numberofsegments;
9938 segmentmarkers = segmentmarkerlist != (int *) NULL;
9940 #else /* not TRILIBRARY */
9941 /* Read the segments from a .poly file. */
9942 /* Read number of segments and number of boundary markers. */
9943 stringptr = readline(inputline, polyfile, polyfilename);
9944 segments = (int) strtol (stringptr, &stringptr, 0);
9945 stringptr = findfield(stringptr);
9946 if (*stringptr == '\0') {
9949 segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
9951 #endif /* not TRILIBRARY */
9952 /* If segments are to be inserted, compute a mapping */
9953 /* from points to triangles. */
9956 printf(" Inserting PSLG segments.\n");
9962 /* Read and insert the segments. */
9963 for (i = 1; i <= segments; i++) {
9965 end1 = segmentlist[index++];
9966 end2 = segmentlist[index++];
9967 if (segmentmarkers) {
9968 boundmarker = segmentmarkerlist[i - 1];
9970 #else /* not TRILIBRARY */
9971 stringptr = readline(inputline, polyfile, inpolyfilename);
9972 stringptr = findfield(stringptr);
9973 if (*stringptr == '\0') {
9974 printf("Error: Segment %d has no endpoints in %s.\n", i,
9978 end1 = (int) strtol (stringptr, &stringptr, 0);
9980 stringptr = findfield(stringptr);
9981 if (*stringptr == '\0') {
9982 printf("Error: Segment %d is missing its second endpoint in %s.\n", i,
9986 end2 = (int) strtol (stringptr, &stringptr, 0);
9988 if (segmentmarkers) {
9989 stringptr = findfield(stringptr);
9990 if (*stringptr == '\0') {
9993 boundmarker = (int) strtol (stringptr, &stringptr, 0);
9996 #endif /* not TRILIBRARY */
9997 if ((end1 < firstnumber) || (end1 >= firstnumber + inpoints)) {
9999 printf("Warning: Invalid first endpoint of segment %d in %s.\n", i,
10002 } else if ((end2 < firstnumber) || (end2 >= firstnumber + inpoints)) {
10004 printf("Warning: Invalid second endpoint of segment %d in %s.\n", i,
10008 endpoint1 = getpoint(end1);
10009 endpoint2 = getpoint(end2);
10010 if ((endpoint1[0] == endpoint2[0]) && (endpoint1[1] == endpoint2[1])) {
10012 printf("Warning: Endpoints of segment %d are coincident in %s.\n",
10016 insertsegment(endpoint1, endpoint2, boundmarker);
10023 if (convex || !poly) {
10024 /* Enclose the convex hull with shell edges. */
10026 printf(" Enclosing convex hull with segments.\n");
10035 /********* Segment (shell edge) insertion ends here *********/
10037 /********* Carving out holes and concavities begins here *********/
10041 /*****************************************************************************/
10043 /* infecthull() Virally infect all of the triangles of the convex hull */
10044 /* that are not protected by shell edges. Where there are */
10045 /* shell edges, set boundary markers as appropriate. */
10047 /*****************************************************************************/
10051 struct triedge hulltri;
10052 struct triedge nexttri;
10053 struct triedge starttri;
10054 struct edge hulledge;
10055 triangle **deadtri;
10057 triangle ptr; /* Temporary variable used by sym(). */
10058 shelle sptr; /* Temporary variable used by tspivot(). */
10061 printf(" Marking concavities (external triangles) for elimination.\n");
10063 /* Find a triangle handle on the hull. */
10064 hulltri.tri = dummytri;
10065 hulltri.orient = 0;
10067 /* Remember where we started so we know when to stop. */
10068 triedgecopy(hulltri, starttri);
10069 /* Go once counterclockwise around the convex hull. */
10071 /* Ignore triangles that are already infected. */
10072 if (!infected(hulltri)) {
10073 /* Is the triangle protected by a shell edge? */
10074 tspivot(hulltri, hulledge);
10075 if (hulledge.sh == dummysh) {
10076 /* The triangle is not protected; infect it. */
10078 deadtri = (triangle **) poolalloc(&viri);
10079 *deadtri = hulltri.tri;
10081 /* The triangle is protected; set boundary markers if appropriate. */
10082 if (mark(hulledge) == 0) {
10083 setmark(hulledge, 1);
10084 org(hulltri, horg);
10085 dest(hulltri, hdest);
10086 if (pointmark(horg) == 0) {
10087 setpointmark(horg, 1);
10089 if (pointmark(hdest) == 0) {
10090 setpointmark(hdest, 1);
10095 /* To find the next hull edge, go clockwise around the next vertex. */
10096 lnextself(hulltri);
10097 oprev(hulltri, nexttri);
10098 while (nexttri.tri != dummytri) {
10099 triedgecopy(nexttri, hulltri);
10100 oprev(hulltri, nexttri);
10102 } while (!triedgeequal(hulltri, starttri));
10105 /*****************************************************************************/
10107 /* plague() Spread the virus from all infected triangles to any neighbors */
10108 /* not protected by shell edges. Delete all infected triangles. */
10110 /* This is the procedure that actually creates holes and concavities. */
10112 /* This procedure operates in two phases. The first phase identifies all */
10113 /* the triangles that will die, and marks them as infected. They are */
10114 /* marked to ensure that each triangle is added to the virus pool only */
10115 /* once, so the procedure will terminate. */
10117 /* The second phase actually eliminates the infected triangles. It also */
10118 /* eliminates orphaned points. */
10120 /*****************************************************************************/
10124 struct triedge testtri;
10125 struct triedge neighbor;
10126 triangle **virusloop;
10127 triangle **deadtri;
10128 struct edge neighborshelle;
10131 point deadorg, deaddest, deadapex;
10133 triangle ptr; /* Temporary variable used by sym() and onext(). */
10134 shelle sptr; /* Temporary variable used by tspivot(). */
10137 printf(" Marking neighbors of marked triangles.\n");
10139 /* Loop through all the infected triangles, spreading the virus to */
10140 /* their neighbors, then to their neighbors' neighbors. */
10141 traversalinit(&viri);
10142 virusloop = (triangle **) traverse(&viri);
10143 while (virusloop != (triangle **) NULL) {
10144 testtri.tri = *virusloop;
10145 /* A triangle is marked as infected by messing with one of its shell */
10146 /* edges, setting it to an illegal value. Hence, we have to */
10147 /* temporarily uninfect this triangle so that we can examine its */
10148 /* adjacent shell edges. */
10151 /* Assign the triangle an orientation for convenience in */
10152 /* checking its points. */
10153 testtri.orient = 0;
10154 org(testtri, deadorg);
10155 dest(testtri, deaddest);
10156 apex(testtri, deadapex);
10157 printf(" Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10158 deadorg[0], deadorg[1], deaddest[0], deaddest[1],
10159 deadapex[0], deadapex[1]);
10161 /* Check each of the triangle's three neighbors. */
10162 for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10163 /* Find the neighbor. */
10164 sym(testtri, neighbor);
10165 /* Check for a shell between the triangle and its neighbor. */
10166 tspivot(testtri, neighborshelle);
10167 /* Check if the neighbor is nonexistent or already infected. */
10168 if ((neighbor.tri == dummytri) || infected(neighbor)) {
10169 if (neighborshelle.sh != dummysh) {
10170 /* There is a shell edge separating the triangle from its */
10171 /* neighbor, but both triangles are dying, so the shell */
10172 /* edge dies too. */
10173 shelledealloc(neighborshelle.sh);
10174 if (neighbor.tri != dummytri) {
10175 /* Make sure the shell edge doesn't get deallocated again */
10176 /* later when the infected neighbor is visited. */
10177 uninfect(neighbor);
10178 tsdissolve(neighbor);
10182 } else { /* The neighbor exists and is not infected. */
10183 if (neighborshelle.sh == dummysh) {
10184 /* There is no shell edge protecting the neighbor, so */
10185 /* the neighbor becomes infected. */
10187 org(neighbor, deadorg);
10188 dest(neighbor, deaddest);
10189 apex(neighbor, deadapex);
10191 " Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10192 deadorg[0], deadorg[1], deaddest[0], deaddest[1],
10193 deadapex[0], deadapex[1]);
10196 /* Ensure that the neighbor's neighbors will be infected. */
10197 deadtri = (triangle **) poolalloc(&viri);
10198 *deadtri = neighbor.tri;
10199 } else { /* The neighbor is protected by a shell edge. */
10200 /* Remove this triangle from the shell edge. */
10201 stdissolve(neighborshelle);
10202 /* The shell edge becomes a boundary. Set markers accordingly. */
10203 if (mark(neighborshelle) == 0) {
10204 setmark(neighborshelle, 1);
10206 org(neighbor, norg);
10207 dest(neighbor, ndest);
10208 if (pointmark(norg) == 0) {
10209 setpointmark(norg, 1);
10211 if (pointmark(ndest) == 0) {
10212 setpointmark(ndest, 1);
10217 /* Remark the triangle as infected, so it doesn't get added to the */
10218 /* virus pool again. */
10220 virusloop = (triangle **) traverse(&viri);
10224 printf(" Deleting marked triangles.\n");
10226 traversalinit(&viri);
10227 virusloop = (triangle **) traverse(&viri);
10228 while (virusloop != (triangle **) NULL) {
10229 testtri.tri = *virusloop;
10231 /* Check each of the three corners of the triangle for elimination. */
10232 /* This is done by walking around each point, checking if it is */
10233 /* still connected to at least one live triangle. */
10234 for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10235 org(testtri, testpoint);
10236 /* Check if the point has already been tested. */
10237 if (testpoint != (point) NULL) {
10239 /* Mark the corner of the triangle as having been tested. */
10240 setorg(testtri, NULL);
10241 /* Walk counterclockwise about the point. */
10242 onext(testtri, neighbor);
10243 /* Stop upon reaching a boundary or the starting triangle. */
10244 while ((neighbor.tri != dummytri)
10245 && (!triedgeequal(neighbor, testtri))) {
10246 if (infected(neighbor)) {
10247 /* Mark the corner of this triangle as having been tested. */
10248 setorg(neighbor, NULL);
10250 /* A live triangle. The point survives. */
10253 /* Walk counterclockwise about the point. */
10254 onextself(neighbor);
10256 /* If we reached a boundary, we must walk clockwise as well. */
10257 if (neighbor.tri == dummytri) {
10258 /* Walk clockwise about the point. */
10259 oprev(testtri, neighbor);
10260 /* Stop upon reaching a boundary. */
10261 while (neighbor.tri != dummytri) {
10262 if (infected(neighbor)) {
10263 /* Mark the corner of this triangle as having been tested. */
10264 setorg(neighbor, NULL);
10266 /* A live triangle. The point survives. */
10269 /* Walk clockwise about the point. */
10270 oprevself(neighbor);
10275 printf(" Deleting point (%.12g, %.12g)\n",
10276 testpoint[0], testpoint[1]);
10278 pointdealloc(testpoint);
10283 /* Record changes in the number of boundary edges, and disconnect */
10284 /* dead triangles from their neighbors. */
10285 for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10286 sym(testtri, neighbor);
10287 if (neighbor.tri == dummytri) {
10288 /* There is no neighboring triangle on this edge, so this edge */
10289 /* is a boundary edge. This triangle is being deleted, so this */
10290 /* boundary edge is deleted. */
10293 /* Disconnect the triangle from its neighbor. */
10294 dissolve(neighbor);
10295 /* There is a neighboring triangle on this edge, so this edge */
10296 /* becomes a boundary edge when this triangle is deleted. */
10300 /* Return the dead triangle to the pool of triangles. */
10301 triangledealloc(testtri.tri);
10302 virusloop = (triangle **) traverse(&viri);
10304 /* Empty the virus pool. */
10305 poolrestart(&viri);
10308 /*****************************************************************************/
10310 /* regionplague() Spread regional attributes and/or area constraints */
10311 /* (from a .poly file) throughout the mesh. */
10313 /* This procedure operates in two phases. The first phase spreads an */
10314 /* attribute and/or an area constraint through a (segment-bounded) region. */
10315 /* The triangles are marked to ensure that each triangle is added to the */
10316 /* virus pool only once, so the procedure will terminate. */
10318 /* The second phase uninfects all infected triangles, returning them to */
10321 /*****************************************************************************/
10323 void regionplague(attribute, area)
10327 struct triedge testtri;
10328 struct triedge neighbor;
10329 triangle **virusloop;
10330 triangle **regiontri;
10331 struct edge neighborshelle;
10332 point regionorg, regiondest, regionapex;
10333 triangle ptr; /* Temporary variable used by sym() and onext(). */
10334 shelle sptr; /* Temporary variable used by tspivot(). */
10337 printf(" Marking neighbors of marked triangles.\n");
10339 /* Loop through all the infected triangles, spreading the attribute */
10340 /* and/or area constraint to their neighbors, then to their neighbors' */
10342 traversalinit(&viri);
10343 virusloop = (triangle **) traverse(&viri);
10344 while (virusloop != (triangle **) NULL) {
10345 testtri.tri = *virusloop;
10346 /* A triangle is marked as infected by messing with one of its shell */
10347 /* edges, setting it to an illegal value. Hence, we have to */
10348 /* temporarily uninfect this triangle so that we can examine its */
10349 /* adjacent shell edges. */
10351 if (regionattrib) {
10352 /* Set an attribute. */
10353 setelemattribute(testtri, eextras, attribute);
10356 /* Set an area constraint. */
10357 setareabound(testtri, area);
10360 /* Assign the triangle an orientation for convenience in */
10361 /* checking its points. */
10362 testtri.orient = 0;
10363 org(testtri, regionorg);
10364 dest(testtri, regiondest);
10365 apex(testtri, regionapex);
10366 printf(" Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10367 regionorg[0], regionorg[1], regiondest[0], regiondest[1],
10368 regionapex[0], regionapex[1]);
10370 /* Check each of the triangle's three neighbors. */
10371 for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
10372 /* Find the neighbor. */
10373 sym(testtri, neighbor);
10374 /* Check for a shell between the triangle and its neighbor. */
10375 tspivot(testtri, neighborshelle);
10376 /* Make sure the neighbor exists, is not already infected, and */
10377 /* isn't protected by a shell edge. */
10378 if ((neighbor.tri != dummytri) && !infected(neighbor)
10379 && (neighborshelle.sh == dummysh)) {
10381 org(neighbor, regionorg);
10382 dest(neighbor, regiondest);
10383 apex(neighbor, regionapex);
10384 printf(" Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10385 regionorg[0], regionorg[1], regiondest[0], regiondest[1],
10386 regionapex[0], regionapex[1]);
10388 /* Infect the neighbor. */
10390 /* Ensure that the neighbor's neighbors will be infected. */
10391 regiontri = (triangle **) poolalloc(&viri);
10392 *regiontri = neighbor.tri;
10395 /* Remark the triangle as infected, so it doesn't get added to the */
10396 /* virus pool again. */
10398 virusloop = (triangle **) traverse(&viri);
10401 /* Uninfect all triangles. */
10403 printf(" Unmarking marked triangles.\n");
10405 traversalinit(&viri);
10406 virusloop = (triangle **) traverse(&viri);
10407 while (virusloop != (triangle **) NULL) {
10408 testtri.tri = *virusloop;
10410 virusloop = (triangle **) traverse(&viri);
10412 /* Empty the virus pool. */
10413 poolrestart(&viri);
10416 /*****************************************************************************/
10418 /* carveholes() Find the holes and infect them. Find the area */
10419 /* constraints and infect them. Infect the convex hull. */
10420 /* Spread the infection and kill triangles. Spread the */
10421 /* area constraints. */
10423 /* This routine mainly calls other routines to carry out all these */
10426 /*****************************************************************************/
10428 void carveholes(holelist, holes, regionlist, regions)
10434 struct triedge searchtri;
10435 struct triedge triangleloop;
10436 struct triedge *regiontris;
10437 triangle **holetri;
10438 triangle **regiontri;
10439 point searchorg, searchdest;
10440 enum locateresult intersect;
10442 triangle ptr; /* Temporary variable used by sym(). */
10444 if (!(quiet || (noholes && convex))) {
10445 printf("Removing unwanted triangles.\n");
10446 if (verbose && (holes > 0)) {
10447 printf(" Marking holes for elimination.\n");
10452 /* Allocate storage for the triangles in which region points fall. */
10453 regiontris = (struct triedge *) malloc(regions * sizeof(struct triedge));
10454 if (regiontris == (struct triedge *) NULL) {
10455 printf("Error: Out of memory.\n");
10460 if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
10461 /* Initialize a pool of viri to be used for holes, concavities, */
10462 /* regional attributes, and/or regional area constraints. */
10463 poolinit(&viri, sizeof(triangle *), VIRUSPERBLOCK, POINTER, 0);
10467 /* Mark as infected any unprotected triangles on the boundary. */
10468 /* This is one way by which concavities are created. */
10472 if ((holes > 0) && !noholes) {
10473 /* Infect each triangle in which a hole lies. */
10474 for (i = 0; i < 2 * holes; i += 2) {
10475 /* Ignore holes that aren't within the bounds of the mesh. */
10476 if ((holelist[i] >= xmin) && (holelist[i] <= xmax)
10477 && (holelist[i + 1] >= ymin) && (holelist[i + 1] <= ymax)) {
10478 /* Start searching from some triangle on the outer boundary. */
10479 searchtri.tri = dummytri;
10480 searchtri.orient = 0;
10481 symself(searchtri);
10482 /* Ensure that the hole is to the left of this boundary edge; */
10483 /* otherwise, locate() will falsely report that the hole */
10484 /* falls within the starting triangle. */
10485 org(searchtri, searchorg);
10486 dest(searchtri, searchdest);
10487 if (counterclockwise(searchorg, searchdest, &holelist[i]) > 0.0) {
10488 /* Find a triangle that contains the hole. */
10489 intersect = locate(&holelist[i], &searchtri);
10490 if ((intersect != OUTSIDE) && (!infected(searchtri))) {
10491 /* Infect the triangle. This is done by marking the triangle */
10492 /* as infect and including the triangle in the virus pool. */
10494 holetri = (triangle **) poolalloc(&viri);
10495 *holetri = searchtri.tri;
10502 /* Now, we have to find all the regions BEFORE we carve the holes, because */
10503 /* locate() won't work when the triangulation is no longer convex. */
10504 /* (Incidentally, this is the reason why regional attributes and area */
10505 /* constraints can't be used when refining a preexisting mesh, which */
10506 /* might not be convex; they can only be used with a freshly */
10507 /* triangulated PSLG.) */
10509 /* Find the starting triangle for each region. */
10510 for (i = 0; i < regions; i++) {
10511 regiontris[i].tri = dummytri;
10512 /* Ignore region points that aren't within the bounds of the mesh. */
10513 if ((regionlist[4 * i] >= xmin) && (regionlist[4 * i] <= xmax) &&
10514 (regionlist[4 * i + 1] >= ymin) && (regionlist[4 * i + 1] <= ymax)) {
10515 /* Start searching from some triangle on the outer boundary. */
10516 searchtri.tri = dummytri;
10517 searchtri.orient = 0;
10518 symself(searchtri);
10519 /* Ensure that the region point is to the left of this boundary */
10520 /* edge; otherwise, locate() will falsely report that the */
10521 /* region point falls within the starting triangle. */
10522 org(searchtri, searchorg);
10523 dest(searchtri, searchdest);
10524 if (counterclockwise(searchorg, searchdest, ®ionlist[4 * i]) >
10526 /* Find a triangle that contains the region point. */
10527 intersect = locate(®ionlist[4 * i], &searchtri);
10528 if ((intersect != OUTSIDE) && (!infected(searchtri))) {
10529 /* Record the triangle for processing after the */
10530 /* holes have been carved. */
10531 triedgecopy(searchtri, regiontris[i]);
10538 if (viri.items > 0) {
10539 /* Carve the holes and concavities. */
10542 /* The virus pool should be empty now. */
10546 if (regionattrib) {
10548 printf("Spreading regional attributes and area constraints.\n");
10550 printf("Spreading regional attributes.\n");
10553 printf("Spreading regional area constraints.\n");
10556 if (regionattrib && !refine) {
10557 /* Assign every triangle a regional attribute of zero. */
10558 traversalinit(&triangles);
10559 triangleloop.orient = 0;
10560 triangleloop.tri = triangletraverse();
10561 while (triangleloop.tri != (triangle *) NULL) {
10562 setelemattribute(triangleloop, eextras, 0.0);
10563 triangleloop.tri = triangletraverse();
10566 for (i = 0; i < regions; i++) {
10567 if (regiontris[i].tri != dummytri) {
10568 /* Make sure the triangle under consideration still exists. */
10569 /* It may have been eaten by the virus. */
10570 if (regiontris[i].tri[3] != (triangle) NULL) {
10571 /* Put one triangle in the virus pool. */
10572 infect(regiontris[i]);
10573 regiontri = (triangle **) poolalloc(&viri);
10574 *regiontri = regiontris[i].tri;
10575 /* Apply one region's attribute and/or area constraint. */
10576 regionplague(regionlist[4 * i + 2], regionlist[4 * i + 3]);
10577 /* The virus pool should be empty now. */
10581 if (regionattrib && !refine) {
10582 /* Note the fact that each triangle has an additional attribute. */
10587 /* Free up memory. */
10588 if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
10598 /********* Carving out holes and concavities ends here *********/
10600 /********* Mesh quality maintenance begins here *********/
10604 /*****************************************************************************/
10606 /* tallyencs() Traverse the entire list of shell edges, check each edge */
10607 /* to see if it is encroached. If so, add it to the list. */
10609 /*****************************************************************************/
10615 struct edge edgeloop;
10618 traversalinit(&shelles);
10619 edgeloop.shorient = 0;
10620 edgeloop.sh = shelletraverse();
10621 while (edgeloop.sh != (shelle *) NULL) {
10622 /* If the segment is encroached, add it to the list. */
10623 dummy = checkedge4encroach(&edgeloop);
10624 edgeloop.sh = shelletraverse();
10628 #endif /* not CDT_ONLY */
10630 /*****************************************************************************/
10632 /* precisionerror() Print an error message for precision problems. */
10634 /*****************************************************************************/
10638 void precisionerror()
10640 printf("Try increasing the area criterion and/or reducing the minimum\n");
10641 printf(" allowable angle so that tiny triangles are not created.\n");
10643 printf("Alternatively, try recompiling me with double precision\n");
10644 printf(" arithmetic (by removing \"#define SINGLE\" from the\n");
10645 printf(" source file or \"-DSINGLE\" from the makefile).\n");
10646 #endif /* SINGLE */
10649 #endif /* not CDT_ONLY */
10651 /*****************************************************************************/
10653 /* repairencs() Find and repair all the encroached segments. */
10655 /* Encroached segments are repaired by splitting them by inserting a point */
10656 /* at or near their centers. */
10658 /* `flaws' is a flag that specifies whether one should take note of new */
10659 /* encroached segments and bad triangles that result from inserting points */
10660 /* to repair existing encroached segments. */
10662 /* When a segment is split, the two resulting subsegments are always */
10663 /* tested to see if they are encroached upon, regardless of the value */
10666 /*****************************************************************************/
10670 void repairencs(flaws)
10673 struct triedge enctri;
10674 struct triedge testtri;
10675 struct edge *encloop;
10676 struct edge testsh;
10679 enum insertsiteresult success;
10680 REAL segmentlength, nearestpoweroftwo;
10682 int acuteorg, acutedest;
10685 triangle ptr; /* Temporary variable used by stpivot(). */
10686 shelle sptr; /* Temporary variable used by snext(). */
10688 while ((badsegments.items > 0) && (steinerleft != 0)) {
10689 traversalinit(&badsegments);
10690 encloop = badsegmenttraverse();
10691 while ((encloop != (struct edge *) NULL) && (steinerleft != 0)) {
10692 /* To decide where to split a segment, we need to know if the */
10693 /* segment shares an endpoint with an adjacent segment. */
10694 /* The concern is that, if we simply split every encroached */
10695 /* segment in its center, two adjacent segments with a small */
10696 /* angle between them might lead to an infinite loop; each */
10697 /* point added to split one segment will encroach upon the */
10698 /* other segment, which must then be split with a point that */
10699 /* will encroach upon the first segment, and so on forever. */
10700 /* To avoid this, imagine a set of concentric circles, whose */
10701 /* radii are powers of two, about each segment endpoint. */
10702 /* These concentric circles determine where the segment is */
10703 /* split. (If both endpoints are shared with adjacent */
10704 /* segments, split the segment in the middle, and apply the */
10705 /* concentric shells for later splittings.) */
10707 /* Is the origin shared with another segment? */
10708 stpivot(*encloop, enctri);
10709 lnext(enctri, testtri);
10710 tspivot(testtri, testsh);
10711 acuteorg = testsh.sh != dummysh;
10712 /* Is the destination shared with another segment? */
10713 lnextself(testtri);
10714 tspivot(testtri, testsh);
10715 acutedest = testsh.sh != dummysh;
10716 /* Now, check the other side of the segment, if there's a triangle */
10718 sym(enctri, testtri);
10719 if (testtri.tri != dummytri) {
10720 /* Is the destination shared with another segment? */
10721 lnextself(testtri);
10722 tspivot(testtri, testsh);
10723 acutedest = acutedest || (testsh.sh != dummysh);
10724 /* Is the origin shared with another segment? */
10725 lnextself(testtri);
10726 tspivot(testtri, testsh);
10727 acuteorg = acuteorg || (testsh.sh != dummysh);
10730 sorg(*encloop, eorg);
10731 sdest(*encloop, edest);
10732 /* Use the concentric circles if exactly one endpoint is shared */
10733 /* with another adjacent segment. */
10734 if (acuteorg ^ acutedest) {
10735 segmentlength = sqrt((edest[0] - eorg[0]) * (edest[0] - eorg[0])
10736 + (edest[1] - eorg[1]) * (edest[1] - eorg[1]));
10737 /* Find the power of two nearest the segment's length. */
10738 nearestpoweroftwo = 1.0;
10739 while (segmentlength > SQUAREROOTTWO * nearestpoweroftwo) {
10740 nearestpoweroftwo *= 2.0;
10742 while (segmentlength < (0.5 * SQUAREROOTTWO) * nearestpoweroftwo) {
10743 nearestpoweroftwo *= 0.5;
10745 /* Where do we split the segment? */
10746 split = 0.5 * nearestpoweroftwo / segmentlength;
10748 split = 1.0 - split;
10751 /* If we're not worried about adjacent segments, split */
10752 /* this segment in the middle. */
10756 /* Create the new point. */
10757 newpoint = (point) poolalloc(&points);
10758 /* Interpolate its coordinate and attributes. */
10759 for (i = 0; i < 2 + nextras; i++) {
10760 newpoint[i] = (1.0 - split) * eorg[i] + split * edest[i];
10762 setpointmark(newpoint, mark(*encloop));
10765 " Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
10766 eorg[0], eorg[1], edest[0], edest[1], newpoint[0], newpoint[1]);
10768 /* Check whether the new point lies on an endpoint. */
10769 if (((newpoint[0] == eorg[0]) && (newpoint[1] == eorg[1]))
10770 || ((newpoint[0] == edest[0]) && (newpoint[1] == edest[1]))) {
10771 printf("Error: Ran out of precision at (%.12g, %.12g).\n",
10772 newpoint[0], newpoint[1]);
10773 printf("I attempted to split a segment to a smaller size than can\n");
10774 printf(" be accommodated by the finite precision of floating point\n"
10776 printf(" arithmetic.\n");
10780 /* Insert the splitting point. This should always succeed. */
10781 success = insertsite(newpoint, &enctri, encloop, flaws, flaws);
10782 if ((success != SUCCESSFULPOINT) && (success != ENCROACHINGPOINT)) {
10783 printf("Internal error in repairencs():\n");
10784 printf(" Failure to split a segment.\n");
10787 if (steinerleft > 0) {
10790 /* Check the two new subsegments to see if they're encroached. */
10791 dummy = checkedge4encroach(encloop);
10792 snextself(*encloop);
10793 dummy = checkedge4encroach(encloop);
10795 badsegmentdealloc(encloop);
10796 encloop = badsegmenttraverse();
10801 #endif /* not CDT_ONLY */
10803 /*****************************************************************************/
10805 /* tallyfaces() Test every triangle in the mesh for quality measures. */
10807 /*****************************************************************************/
10813 struct triedge triangleloop;
10816 printf(" Making a list of bad triangles.\n");
10818 traversalinit(&triangles);
10819 triangleloop.orient = 0;
10820 triangleloop.tri = triangletraverse();
10821 while (triangleloop.tri != (triangle *) NULL) {
10822 /* If the triangle is bad, enqueue it. */
10823 testtriangle(&triangleloop);
10824 triangleloop.tri = triangletraverse();
10828 #endif /* not CDT_ONLY */
10830 /*****************************************************************************/
10832 /* findcircumcenter() Find the circumcenter of a triangle. */
10834 /* The result is returned both in terms of x-y coordinates and xi-eta */
10835 /* coordinates. The xi-eta coordinate system is defined in terms of the */
10836 /* triangle: the origin of the triangle is the origin of the coordinate */
10837 /* system; the destination of the triangle is one unit along the xi axis; */
10838 /* and the apex of the triangle is one unit along the eta axis. */
10840 /* The return value indicates which edge of the triangle is shortest. */
10842 /*****************************************************************************/
10844 enum circumcenterresult findcircumcenter(torg, tdest, tapex, circumcenter,
10849 point circumcenter;
10853 REAL xdo, ydo, xao, yao, xad, yad;
10854 REAL dodist, aodist, addist;
10858 circumcentercount++;
10860 /* Compute the circumcenter of the triangle. */
10861 xdo = tdest[0] - torg[0];
10862 ydo = tdest[1] - torg[1];
10863 xao = tapex[0] - torg[0];
10864 yao = tapex[1] - torg[1];
10865 dodist = xdo * xdo + ydo * ydo;
10866 aodist = xao * xao + yao * yao;
10868 denominator = 0.5 / (xdo * yao - xao * ydo);
10870 /* Use the counterclockwise() routine to ensure a positive (and */
10871 /* reasonably accurate) result, avoiding any possibility of */
10872 /* division by zero. */
10873 denominator = 0.5 / counterclockwise(tdest, tapex, torg);
10874 /* Don't count the above as an orientation test. */
10875 counterclockcount--;
10877 circumcenter[0] = torg[0] - (ydo * aodist - yao * dodist) * denominator;
10878 circumcenter[1] = torg[1] + (xdo * aodist - xao * dodist) * denominator;
10880 /* To interpolate point attributes for the new point inserted at */
10881 /* the circumcenter, define a coordinate system with a xi-axis, */
10882 /* directed from the triangle's origin to its destination, and */
10883 /* an eta-axis, directed from its origin to its apex. */
10884 /* Calculate the xi and eta coordinates of the circumcenter. */
10885 dx = circumcenter[0] - torg[0];
10886 dy = circumcenter[1] - torg[1];
10887 *xi = (dx * yao - xao * dy) * (2.0 * denominator);
10888 *eta = (xdo * dy - dx * ydo) * (2.0 * denominator);
10890 xad = tapex[0] - tdest[0];
10891 yad = tapex[1] - tdest[1];
10892 addist = xad * xad + yad * yad;
10893 if ((addist < dodist) && (addist < aodist)) {
10894 return OPPOSITEORG;
10895 } else if (dodist < aodist) {
10896 return OPPOSITEAPEX;
10898 return OPPOSITEDEST;
10902 /*****************************************************************************/
10904 /* splittriangle() Inserts a point at the circumcenter of a triangle. */
10905 /* Deletes the newly inserted point if it encroaches upon */
10908 /*****************************************************************************/
10912 void splittriangle(badtri)
10913 struct badface *badtri;
10915 point borg, bdest, bapex;
10918 enum insertsiteresult success;
10919 enum circumcenterresult shortedge;
10923 org(badtri->badfacetri, borg);
10924 dest(badtri->badfacetri, bdest);
10925 apex(badtri->badfacetri, bapex);
10926 /* Make sure that this triangle is still the same triangle it was */
10927 /* when it was tested and determined to be of bad quality. */
10928 /* Subsequent transformations may have made it a different triangle. */
10929 if ((borg == badtri->faceorg) && (bdest == badtri->facedest) &&
10930 (bapex == badtri->faceapex)) {
10932 printf(" Splitting this triangle at its circumcenter:\n");
10933 printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", borg[0],
10934 borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
10937 /* Create a new point at the triangle's circumcenter. */
10938 newpoint = (point) poolalloc(&points);
10939 shortedge = findcircumcenter(borg, bdest, bapex, newpoint, &xi, &eta);
10940 /* Check whether the new point lies on a triangle vertex. */
10941 if (((newpoint[0] == borg[0]) && (newpoint[1] == borg[1]))
10942 || ((newpoint[0] == bdest[0]) && (newpoint[1] == bdest[1]))
10943 || ((newpoint[0] == bapex[0]) && (newpoint[1] == bapex[1]))) {
10945 printf("Warning: New point (%.12g, %.12g) falls on existing vertex.\n"
10946 , newpoint[0], newpoint[1]);
10949 pointdealloc(newpoint);
10951 for (i = 2; i < 2 + nextras; i++) {
10952 /* Interpolate the point attributes at the circumcenter. */
10953 newpoint[i] = borg[i] + xi * (bdest[i] - borg[i])
10954 + eta * (bapex[i] - borg[i]);
10956 /* The new point must be in the interior, and have a marker of zero. */
10957 setpointmark(newpoint, 0);
10958 /* Ensure that the handle `badtri->badfacetri' represents the shortest */
10959 /* edge of the triangle. This ensures that the circumcenter must */
10960 /* fall to the left of this edge, so point location will work. */
10961 if (shortedge == OPPOSITEORG) {
10962 lnextself(badtri->badfacetri);
10963 } else if (shortedge == OPPOSITEDEST) {
10964 lprevself(badtri->badfacetri);
10966 /* Insert the circumcenter, searching from the edge of the triangle, */
10967 /* and maintain the Delaunay property of the triangulation. */
10968 success = insertsite(newpoint, &(badtri->badfacetri),
10969 (struct edge *) NULL, 1, 1);
10970 if (success == SUCCESSFULPOINT) {
10971 if (steinerleft > 0) {
10974 } else if (success == ENCROACHINGPOINT) {
10975 /* If the newly inserted point encroaches upon a segment, delete it. */
10976 deletesite(&(badtri->badfacetri));
10977 } else if (success == VIOLATINGPOINT) {
10978 /* Failed to insert the new point, but some segment was */
10979 /* marked as being encroached. */
10980 pointdealloc(newpoint);
10981 } else { /* success == DUPLICATEPOINT */
10982 /* Failed to insert the new point because a vertex is already there. */
10985 "Warning: New point (%.12g, %.12g) falls on existing vertex.\n"
10986 , newpoint[0], newpoint[1]);
10989 pointdealloc(newpoint);
10994 printf(" The new point is at the circumcenter of triangle\n");
10995 printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
10996 borg[0], borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
10998 printf("This probably means that I am trying to refine triangles\n");
10999 printf(" to a smaller size than can be accommodated by the finite\n");
11000 printf(" precision of floating point arithmetic. (You can be\n");
11001 printf(" sure of this if I fail to terminate.)\n");
11005 /* Return the bad triangle to the pool. */
11006 pooldealloc(&badtriangles, (VOID *) badtri);
11009 #endif /* not CDT_ONLY */
11011 /*****************************************************************************/
11013 /* enforcequality() Remove all the encroached edges and bad triangles */
11014 /* from the triangulation. */
11016 /*****************************************************************************/
11020 void enforcequality()
11025 printf("Adding Steiner points to enforce quality.\n");
11027 /* Initialize the pool of encroached segments. */
11028 poolinit(&badsegments, sizeof(struct edge), BADSEGMENTPERBLOCK, POINTER, 0);
11030 printf(" Looking for encroached segments.\n");
11032 /* Test all segments to see if they're encroached. */
11034 if (verbose && (badsegments.items > 0)) {
11035 printf(" Splitting encroached segments.\n");
11037 /* Note that steinerleft == -1 if an unlimited number */
11038 /* of Steiner points is allowed. */
11039 while ((badsegments.items > 0) && (steinerleft != 0)) {
11040 /* Fix the segments without noting newly encroached segments or */
11041 /* bad triangles. The reason we don't want to note newly */
11042 /* encroached segments is because some encroached segments are */
11043 /* likely to be noted multiple times, and would then be blindly */
11044 /* split multiple times. I should fix that some time. */
11046 /* Now, find all the segments that became encroached while adding */
11047 /* points to split encroached segments. */
11050 /* At this point, if we haven't run out of Steiner points, the */
11051 /* triangulation should be (conforming) Delaunay. */
11053 /* Next, we worry about enforcing triangle quality. */
11054 if ((minangle > 0.0) || vararea || fixedarea) {
11055 /* Initialize the pool of bad triangles. */
11056 poolinit(&badtriangles, sizeof(struct badface), BADTRIPERBLOCK, POINTER,
11058 /* Initialize the queues of bad triangles. */
11059 for (i = 0; i < 64; i++) {
11060 queuefront[i] = (struct badface *) NULL;
11061 queuetail[i] = &queuefront[i];
11063 /* Test all triangles to see if they're bad. */
11066 printf(" Splitting bad triangles.\n");
11068 while ((badtriangles.items > 0) && (steinerleft != 0)) {
11069 /* Fix one bad triangle by inserting a point at its circumcenter. */
11070 splittriangle(dequeuebadtri());
11071 /* Fix any encroached segments that may have resulted. Record */
11072 /* any new bad triangles or encroached segments that result. */
11073 if (badsegments.items > 0) {
11078 /* At this point, if we haven't run out of Steiner points, the */
11079 /* triangulation should be (conforming) Delaunay and have no */
11080 /* low-quality triangles. */
11082 /* Might we have run out of Steiner points too soon? */
11083 if (!quiet && (badsegments.items > 0) && (steinerleft == 0)) {
11084 printf("\nWarning: I ran out of Steiner points, but the mesh has\n");
11085 if (badsegments.items == 1) {
11086 printf(" an encroached segment, and therefore might not be truly\n");
11088 printf(" %ld encroached segments, and therefore might not be truly\n",
11089 badsegments.items);
11091 printf(" Delaunay. If the Delaunay property is important to you,\n");
11092 printf(" try increasing the number of Steiner points (controlled by\n");
11093 printf(" the -S switch) slightly and try again.\n\n");
11097 #endif /* not CDT_ONLY */
11101 /********* Mesh quality maintenance ends here *********/
11103 /*****************************************************************************/
11105 /* highorder() Create extra nodes for quadratic subparametric elements. */
11107 /*****************************************************************************/
11111 struct triedge triangleloop, trisym;
11112 struct edge checkmark;
11116 triangle ptr; /* Temporary variable used by sym(). */
11117 shelle sptr; /* Temporary variable used by tspivot(). */
11120 printf("Adding vertices for second-order triangles.\n");
11122 /* The following line ensures that dead items in the pool of nodes */
11123 /* cannot be allocated for the extra nodes associated with high */
11124 /* order elements. This ensures that the primary nodes (at the */
11125 /* corners of elements) will occur earlier in the output files, and */
11126 /* have lower indices, than the extra nodes. */
11127 points.deaditemstack = (VOID *) NULL;
11129 traversalinit(&triangles);
11130 triangleloop.tri = triangletraverse();
11131 /* To loop over the set of edges, loop over all triangles, and look at */
11132 /* the three edges of each triangle. If there isn't another triangle */
11133 /* adjacent to the edge, operate on the edge. If there is another */
11134 /* adjacent triangle, operate on the edge only if the current triangle */
11135 /* has a smaller pointer than its neighbor. This way, each edge is */
11136 /* considered only once. */
11137 while (triangleloop.tri != (triangle *) NULL) {
11138 for (triangleloop.orient = 0; triangleloop.orient < 3;
11139 triangleloop.orient++) {
11140 sym(triangleloop, trisym);
11141 if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
11142 org(triangleloop, torg);
11143 dest(triangleloop, tdest);
11144 /* Create a new node in the middle of the edge. Interpolate */
11145 /* its attributes. */
11146 newpoint = (point) poolalloc(&points);
11147 for (i = 0; i < 2 + nextras; i++) {
11148 newpoint[i] = 0.5 * (torg[i] + tdest[i]);
11150 /* Set the new node's marker to zero or one, depending on */
11151 /* whether it lies on a boundary. */
11152 setpointmark(newpoint, trisym.tri == dummytri);
11154 tspivot(triangleloop, checkmark);
11155 /* If this edge is a segment, transfer the marker to the new node. */
11156 if (checkmark.sh != dummysh) {
11157 setpointmark(newpoint, mark(checkmark));
11161 printf(" Creating (%.12g, %.12g).\n", newpoint[0], newpoint[1]);
11163 /* Record the new node in the (one or two) adjacent elements. */
11164 triangleloop.tri[highorderindex + triangleloop.orient] =
11165 (triangle) newpoint;
11166 if (trisym.tri != dummytri) {
11167 trisym.tri[highorderindex + trisym.orient] = (triangle) newpoint;
11171 triangleloop.tri = triangletraverse();
11175 /********* File I/O routines begin here *********/
11179 /*****************************************************************************/
11181 /* readline() Read a nonempty line from a file. */
11183 /* A line is considered "nonempty" if it contains something that looks like */
11186 /*****************************************************************************/
11190 char *readline(string, infile, infilename)
11197 /* Search for something that looks like a number. */
11199 result = fgets(string, INPUTLINESIZE, infile);
11200 if (result == (char *) NULL) {
11201 printf(" Error: Unexpected end of file in %s.\n", infilename);
11204 /* Skip anything that doesn't look like a number, a comment, */
11205 /* or the end of a line. */
11206 while ((*result != '\0') && (*result != '#')
11207 && (*result != '.') && (*result != '+') && (*result != '-')
11208 && ((*result < '0') || (*result > '9'))) {
11211 /* If it's a comment or end of line, read another line and try again. */
11212 } while ((*result == '#') || (*result == '\0'));
11216 #endif /* not TRILIBRARY */
11218 /*****************************************************************************/
11220 /* findfield() Find the next field of a string. */
11222 /* Jumps past the current field by searching for whitespace, then jumps */
11223 /* past the whitespace to find the next field. */
11225 /*****************************************************************************/
11229 char *findfield(string)
11235 /* Skip the current field. Stop upon reaching whitespace. */
11236 while ((*result != '\0') && (*result != '#')
11237 && (*result != ' ') && (*result != '\t')) {
11240 /* Now skip the whitespace and anything else that doesn't look like a */
11241 /* number, a comment, or the end of a line. */
11242 while ((*result != '\0') && (*result != '#')
11243 && (*result != '.') && (*result != '+') && (*result != '-')
11244 && ((*result < '0') || (*result > '9'))) {
11247 /* Check for a comment (prefixed with `#'). */
11248 if (*result == '#') {
11254 #endif /* not TRILIBRARY */
11256 /*****************************************************************************/
11258 /* readnodes() Read the points from a file, which may be a .node or .poly */
11261 /*****************************************************************************/
11265 void readnodes(nodefilename, polyfilename, polyfile)
11266 char *nodefilename;
11267 char *polyfilename;
11272 char inputline[INPUTLINESIZE];
11282 /* Read the points from a .poly file. */
11284 printf("Opening %s.\n", polyfilename);
11286 *polyfile = fopen(polyfilename, "r");
11287 if (*polyfile == (FILE *) NULL) {
11288 printf(" Error: Cannot access file %s.\n", polyfilename);
11291 /* Read number of points, number of dimensions, number of point */
11292 /* attributes, and number of boundary markers. */
11293 stringptr = readline(inputline, *polyfile, polyfilename);
11294 inpoints = (int) strtol (stringptr, &stringptr, 0);
11295 stringptr = findfield(stringptr);
11296 if (*stringptr == '\0') {
11299 mesh_dim = (int) strtol (stringptr, &stringptr, 0);
11301 stringptr = findfield(stringptr);
11302 if (*stringptr == '\0') {
11305 nextras = (int) strtol (stringptr, &stringptr, 0);
11307 stringptr = findfield(stringptr);
11308 if (*stringptr == '\0') {
11311 nodemarkers = (int) strtol (stringptr, &stringptr, 0);
11313 if (inpoints > 0) {
11314 infile = *polyfile;
11315 infilename = polyfilename;
11318 /* If the .poly file claims there are zero points, that means that */
11319 /* the points should be read from a separate .node file. */
11321 infilename = innodefilename;
11325 infilename = innodefilename;
11326 *polyfile = (FILE *) NULL;
11329 if (readnodefile) {
11330 /* Read the points from a .node file. */
11332 printf("Opening %s.\n", innodefilename);
11334 infile = fopen(innodefilename, "r");
11335 if (infile == (FILE *) NULL) {
11336 printf(" Error: Cannot access file %s.\n", innodefilename);
11339 /* Read number of points, number of dimensions, number of point */
11340 /* attributes, and number of boundary markers. */
11341 stringptr = readline(inputline, infile, innodefilename);
11342 inpoints = (int) strtol (stringptr, &stringptr, 0);
11343 stringptr = findfield(stringptr);
11344 if (*stringptr == '\0') {
11347 mesh_dim = (int) strtol (stringptr, &stringptr, 0);
11349 stringptr = findfield(stringptr);
11350 if (*stringptr == '\0') {
11353 nextras = (int) strtol (stringptr, &stringptr, 0);
11355 stringptr = findfield(stringptr);
11356 if (*stringptr == '\0') {
11359 nodemarkers = (int) strtol (stringptr, &stringptr, 0);
11363 if (inpoints < 3) {
11364 printf("Error: Input must have at least three input points.\n");
11367 if (mesh_dim != 2) {
11368 printf("Error: Triangle only works with two-dimensional meshes.\n");
11372 initializepointpool();
11374 /* Read the points. */
11375 for (i = 0; i < inpoints; i++) {
11376 pointloop = (point) poolalloc(&points);
11377 stringptr = readline(inputline, infile, infilename);
11379 firstnode = (int) strtol (stringptr, &stringptr, 0);
11380 if ((firstnode == 0) || (firstnode == 1)) {
11381 firstnumber = firstnode;
11384 stringptr = findfield(stringptr);
11385 if (*stringptr == '\0') {
11386 printf("Error: Point %d has no x coordinate.\n", firstnumber + i);
11389 x = (REAL) strtod(stringptr, &stringptr);
11390 stringptr = findfield(stringptr);
11391 if (*stringptr == '\0') {
11392 printf("Error: Point %d has no y coordinate.\n", firstnumber + i);
11395 y = (REAL) strtod(stringptr, &stringptr);
11398 /* Read the point attributes. */
11399 for (j = 2; j < 2 + nextras; j++) {
11400 stringptr = findfield(stringptr);
11401 if (*stringptr == '\0') {
11402 pointloop[j] = 0.0;
11404 pointloop[j] = (REAL) strtod(stringptr, &stringptr);
11408 /* Read a point marker. */
11409 stringptr = findfield(stringptr);
11410 if (*stringptr == '\0') {
11411 setpointmark(pointloop, 0);
11413 currentmarker = (int) strtol (stringptr, &stringptr, 0);
11414 setpointmark(pointloop, currentmarker);
11417 /* If no markers are specified in the file, they default to zero. */
11418 setpointmark(pointloop, 0);
11420 /* Determine the smallest and largest x and y coordinates. */
11425 xmin = (x < xmin) ? x : xmin;
11426 xmax = (x > xmax) ? x : xmax;
11427 ymin = (y < ymin) ? y : ymin;
11428 ymax = (y > ymax) ? y : ymax;
11431 if (readnodefile) {
11435 /* Nonexistent x value used as a flag to mark circle events in sweepline */
11436 /* Delaunay algorithm. */
11437 xminextreme = 10 * xmin - 9 * xmax;
11440 #endif /* not TRILIBRARY */
11442 /*****************************************************************************/
11444 /* transfernodes() Read the points from memory. */
11446 /*****************************************************************************/
11450 void transfernodes(pointlist, pointattriblist, pointmarkerlist, numberofpoints,
11451 numberofpointattribs)
11453 REAL *pointattriblist;
11454 int *pointmarkerlist;
11455 int numberofpoints;
11456 int numberofpointattribs;
11464 inpoints = numberofpoints;
11466 nextras = numberofpointattribs;
11468 if (inpoints < 3) {
11469 printf("Error: Input must have at least three input points.\n");
11473 initializepointpool();
11475 /* Read the points. */
11478 for (i = 0; i < inpoints; i++) {
11479 pointloop = (point) poolalloc(&points);
11480 /* Read the point coordinates. */
11481 x = pointloop[0] = pointlist[coordindex++];
11482 y = pointloop[1] = pointlist[coordindex++];
11483 /* Read the point attributes. */
11484 for (j = 0; j < numberofpointattribs; j++) {
11485 pointloop[2 + j] = pointattriblist[attribindex++];
11487 if (pointmarkerlist != (int *) NULL) {
11488 /* Read a point marker. */
11489 setpointmark(pointloop, pointmarkerlist[i]);
11491 /* If no markers are specified, they default to zero. */
11492 setpointmark(pointloop, 0);
11496 /* Determine the smallest and largest x and y coordinates. */
11501 xmin = (x < xmin) ? x : xmin;
11502 xmax = (x > xmax) ? x : xmax;
11503 ymin = (y < ymin) ? y : ymin;
11504 ymax = (y > ymax) ? y : ymax;
11508 /* Nonexistent x value used as a flag to mark circle events in sweepline */
11509 /* Delaunay algorithm. */
11510 xminextreme = 10 * xmin - 9 * xmax;
11513 #endif /* TRILIBRARY */
11515 /*****************************************************************************/
11517 /* readholes() Read the holes, and possibly regional attributes and area */
11518 /* constraints, from a .poly file. */
11520 /*****************************************************************************/
11524 void readholes(polyfile, polyfilename, hlist, holes, rlist, regions)
11526 char *polyfilename;
11534 char inputline[INPUTLINESIZE];
11539 /* Read the holes. */
11540 stringptr = readline(inputline, polyfile, polyfilename);
11541 *holes = (int) strtol (stringptr, &stringptr, 0);
11543 holelist = (REAL *) malloc(2 * *holes * sizeof(REAL));
11545 if (holelist == (REAL *) NULL) {
11546 printf("Error: Out of memory.\n");
11549 for (i = 0; i < 2 * *holes; i += 2) {
11550 stringptr = readline(inputline, polyfile, polyfilename);
11551 stringptr = findfield(stringptr);
11552 if (*stringptr == '\0') {
11553 printf("Error: Hole %d has no x coordinate.\n",
11554 firstnumber + (i >> 1));
11557 holelist[i] = (REAL) strtod(stringptr, &stringptr);
11559 stringptr = findfield(stringptr);
11560 if (*stringptr == '\0') {
11561 printf("Error: Hole %d has no y coordinate.\n",
11562 firstnumber + (i >> 1));
11565 holelist[i + 1] = (REAL) strtod(stringptr, &stringptr);
11569 *hlist = (REAL *) NULL;
11573 if ((regionattrib || vararea) && !refine) {
11574 /* Read the area constraints. */
11575 stringptr = readline(inputline, polyfile, polyfilename);
11576 *regions = (int) strtol (stringptr, &stringptr, 0);
11577 if (*regions > 0) {
11578 regionlist = (REAL *) malloc(4 * *regions * sizeof(REAL));
11579 *rlist = regionlist;
11580 if (regionlist == (REAL *) NULL) {
11581 printf("Error: Out of memory.\n");
11585 for (i = 0; i < *regions; i++) {
11586 stringptr = readline(inputline, polyfile, polyfilename);
11587 stringptr = findfield(stringptr);
11588 if (*stringptr == '\0') {
11589 printf("Error: Region %d has no x coordinate.\n",
11593 regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11595 stringptr = findfield(stringptr);
11596 if (*stringptr == '\0') {
11597 printf("Error: Region %d has no y coordinate.\n",
11601 regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11603 stringptr = findfield(stringptr);
11604 if (*stringptr == '\0') {
11606 "Error: Region %d has no region attribute or area constraint.\n",
11610 regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
11612 stringptr = findfield(stringptr);
11613 if (*stringptr == '\0') {
11614 regionlist[index] = regionlist[index - 1];
11616 regionlist[index] = (REAL) strtod(stringptr, &stringptr);
11622 /* Set `*regions' to zero to avoid an accidental free() later. */
11624 *rlist = (REAL *) NULL;
11626 #endif /* not CDT_ONLY */
11631 #endif /* not TRILIBRARY */
11633 /*****************************************************************************/
11635 /* finishfile() Write the command line to the output file so the user */
11636 /* can remember how the file was generated. Close the file. */
11638 /*****************************************************************************/
11642 void finishfile(outfile, argc, argv)
11649 fprintf(outfile, "# Generated by");
11650 for (i = 0; i < argc; i++) {
11651 fprintf(outfile, " ");
11652 fputs(argv[i], outfile);
11654 fprintf(outfile, "\n");
11658 #endif /* not TRILIBRARY */
11660 /*****************************************************************************/
11662 /* writenodes() Number the points and write them to a .node file. */
11664 /* To save memory, the point numbers are written over the shell markers */
11665 /* after the points are written to a file. */
11667 /*****************************************************************************/
11671 void writenodes(pointlist, pointattriblist, pointmarkerlist)
11673 REAL **pointattriblist;
11674 int **pointmarkerlist;
11676 #else /* not TRILIBRARY */
11678 void writenodes(nodefilename, argc, argv)
11679 char *nodefilename;
11683 #endif /* not TRILIBRARY */
11692 #else /* not TRILIBRARY */
11694 #endif /* not TRILIBRARY */
11701 printf("Writing points.\n");
11703 /* Allocate memory for output points if necessary. */
11704 if (*pointlist == (REAL *) NULL) {
11705 *pointlist = (REAL *) malloc(points.items * 2 * sizeof(REAL));
11706 if (*pointlist == (REAL *) NULL) {
11707 printf("Error: Out of memory.\n");
11711 /* Allocate memory for output point attributes if necessary. */
11712 if ((nextras > 0) && (*pointattriblist == (REAL *) NULL)) {
11713 *pointattriblist = (REAL *) malloc(points.items * nextras * sizeof(REAL));
11714 if (*pointattriblist == (REAL *) NULL) {
11715 printf("Error: Out of memory.\n");
11719 /* Allocate memory for output point markers if necessary. */
11720 if (!nobound && (*pointmarkerlist == (int *) NULL)) {
11721 *pointmarkerlist = (int *) malloc(points.items * sizeof(int));
11722 if (*pointmarkerlist == (int *) NULL) {
11723 printf("Error: Out of memory.\n");
11727 plist = *pointlist;
11728 palist = *pointattriblist;
11729 pmlist = *pointmarkerlist;
11732 #else /* not TRILIBRARY */
11734 printf("Writing %s.\n", nodefilename);
11736 outfile = fopen(nodefilename, "w");
11737 if (outfile == (FILE *) NULL) {
11738 printf(" Error: Cannot create file %s.\n", nodefilename);
11741 /* Number of points, number of dimensions, number of point attributes, */
11742 /* and number of boundary markers (zero or one). */
11743 fprintf(outfile, "%ld %d %d %d\n", points.items, mesh_dim, nextras,
11745 #endif /* not TRILIBRARY */
11747 traversalinit(&points);
11748 pointloop = pointtraverse();
11749 pointnumber = firstnumber;
11750 while (pointloop != (point) NULL) {
11752 /* X and y coordinates. */
11753 plist[coordindex++] = pointloop[0];
11754 plist[coordindex++] = pointloop[1];
11755 /* Point attributes. */
11756 for (i = 0; i < nextras; i++) {
11757 palist[attribindex++] = pointloop[2 + i];
11760 /* Copy the boundary marker. */
11761 pmlist[pointnumber - firstnumber] = pointmark(pointloop);
11763 #else /* not TRILIBRARY */
11764 /* Point number, x and y coordinates. */
11765 fprintf(outfile, "%4d %.17g %.17g", pointnumber, pointloop[0],
11767 for (i = 0; i < nextras; i++) {
11768 /* Write an attribute. */
11769 fprintf(outfile, " %.17g", pointloop[i + 2]);
11772 fprintf(outfile, "\n");
11774 /* Write the boundary marker. */
11775 fprintf(outfile, " %d\n", pointmark(pointloop));
11777 #endif /* not TRILIBRARY */
11779 setpointmark(pointloop, pointnumber);
11780 pointloop = pointtraverse();
11785 finishfile(outfile, argc, argv);
11786 #endif /* not TRILIBRARY */
11789 /*****************************************************************************/
11791 /* numbernodes() Number the points. */
11793 /* Each point is assigned a marker equal to its number. */
11795 /* Used when writenodes() is not called because no .node file is written. */
11797 /*****************************************************************************/
11804 traversalinit(&points);
11805 pointloop = pointtraverse();
11806 pointnumber = firstnumber;
11807 while (pointloop != (point) NULL) {
11808 setpointmark(pointloop, pointnumber);
11809 pointloop = pointtraverse();
11814 /*****************************************************************************/
11816 /* writeelements() Write the triangles to an .ele file. */
11818 /*****************************************************************************/
11822 void writeelements(trianglelist, triangleattriblist)
11823 int **trianglelist;
11824 REAL **triangleattriblist;
11826 #else /* not TRILIBRARY */
11828 void writeelements(elefilename, argc, argv)
11833 #endif /* not TRILIBRARY */
11841 #else /* not TRILIBRARY */
11843 #endif /* not TRILIBRARY */
11844 struct triedge triangleloop;
11846 point mid1, mid2, mid3;
11852 printf("Writing triangles.\n");
11854 /* Allocate memory for output triangles if necessary. */
11855 if (*trianglelist == (int *) NULL) {
11856 *trianglelist = (int *) malloc(triangles.items *
11857 ((order + 1) * (order + 2) / 2) * sizeof(int));
11858 if (*trianglelist == (int *) NULL) {
11859 printf("Error: Out of memory.\n");
11863 /* Allocate memory for output triangle attributes if necessary. */
11864 if ((eextras > 0) && (*triangleattriblist == (REAL *) NULL)) {
11865 *triangleattriblist = (REAL *) malloc(triangles.items * eextras *
11867 if (*triangleattriblist == (REAL *) NULL) {
11868 printf("Error: Out of memory.\n");
11872 tlist = *trianglelist;
11873 talist = *triangleattriblist;
11876 #else /* not TRILIBRARY */
11878 printf("Writing %s.\n", elefilename);
11880 outfile = fopen(elefilename, "w");
11881 if (outfile == (FILE *) NULL) {
11882 printf(" Error: Cannot create file %s.\n", elefilename);
11885 /* Number of triangles, points per triangle, attributes per triangle. */
11886 fprintf(outfile, "%ld %d %d\n", triangles.items,
11887 (order + 1) * (order + 2) / 2, eextras);
11888 #endif /* not TRILIBRARY */
11890 traversalinit(&triangles);
11891 triangleloop.tri = triangletraverse();
11892 triangleloop.orient = 0;
11893 elementnumber = firstnumber;
11894 while (triangleloop.tri != (triangle *) NULL) {
11895 org(triangleloop, p1);
11896 dest(triangleloop, p2);
11897 apex(triangleloop, p3);
11900 tlist[pointindex++] = pointmark(p1);
11901 tlist[pointindex++] = pointmark(p2);
11902 tlist[pointindex++] = pointmark(p3);
11903 #else /* not TRILIBRARY */
11904 /* Triangle number, indices for three points. */
11905 fprintf(outfile, "%4d %4d %4d %4d", elementnumber,
11906 pointmark(p1), pointmark(p2), pointmark(p3));
11907 #endif /* not TRILIBRARY */
11909 mid1 = (point) triangleloop.tri[highorderindex + 1];
11910 mid2 = (point) triangleloop.tri[highorderindex + 2];
11911 mid3 = (point) triangleloop.tri[highorderindex];
11913 tlist[pointindex++] = pointmark(p1);
11914 tlist[pointindex++] = pointmark(p2);
11915 tlist[pointindex++] = pointmark(p3);
11916 tlist[pointindex++] = pointmark(mid1);
11917 tlist[pointindex++] = pointmark(mid2);
11918 tlist[pointindex++] = pointmark(mid3);
11919 #else /* not TRILIBRARY */
11920 /* Triangle number, indices for six points. */
11921 fprintf(outfile, "%4d %4d %4d %4d %4d %4d %4d", elementnumber,
11922 pointmark(p1), pointmark(p2), pointmark(p3), pointmark(mid1),
11923 pointmark(mid2), pointmark(mid3));
11924 #endif /* not TRILIBRARY */
11928 for (i = 0; i < eextras; i++) {
11929 talist[attribindex++] = elemattribute(triangleloop, i);
11931 #else /* not TRILIBRARY */
11932 for (i = 0; i < eextras; i++) {
11933 fprintf(outfile, " %.17g", elemattribute(triangleloop, i));
11935 fprintf(outfile, "\n");
11936 #endif /* not TRILIBRARY */
11938 triangleloop.tri = triangletraverse();
11943 finishfile(outfile, argc, argv);
11944 #endif /* not TRILIBRARY */
11947 /*****************************************************************************/
11949 /* writepoly() Write the segments and holes to a .poly file. */
11951 /*****************************************************************************/
11955 void writepoly(segmentlist, segmentmarkerlist)
11957 int **segmentmarkerlist;
11959 #else /* not TRILIBRARY */
11961 void writepoly(polyfilename, holelist, holes, regionlist, regions, argc, argv)
11962 char *polyfilename;
11970 #endif /* not TRILIBRARY */
11977 #else /* not TRILIBRARY */
11980 #endif /* not TRILIBRARY */
11981 struct edge shelleloop;
11982 point endpoint1, endpoint2;
11987 printf("Writing segments.\n");
11989 /* Allocate memory for output segments if necessary. */
11990 if (*segmentlist == (int *) NULL) {
11991 *segmentlist = (int *) malloc(shelles.items * 2 * sizeof(int));
11992 if (*segmentlist == (int *) NULL) {
11993 printf("Error: Out of memory.\n");
11997 /* Allocate memory for output segment markers if necessary. */
11998 if (!nobound && (*segmentmarkerlist == (int *) NULL)) {
11999 *segmentmarkerlist = (int *) malloc(shelles.items * sizeof(int));
12000 if (*segmentmarkerlist == (int *) NULL) {
12001 printf("Error: Out of memory.\n");
12005 slist = *segmentlist;
12006 smlist = *segmentmarkerlist;
12008 #else /* not TRILIBRARY */
12010 printf("Writing %s.\n", polyfilename);
12012 outfile = fopen(polyfilename, "w");
12013 if (outfile == (FILE *) NULL) {
12014 printf(" Error: Cannot create file %s.\n", polyfilename);
12017 /* The zero indicates that the points are in a separate .node file. */
12018 /* Followed by number of dimensions, number of point attributes, */
12019 /* and number of boundary markers (zero or one). */
12020 fprintf(outfile, "%d %d %d %d\n", 0, mesh_dim, nextras, 1 - nobound);
12021 /* Number of segments, number of boundary markers (zero or one). */
12022 fprintf(outfile, "%ld %d\n", shelles.items, 1 - nobound);
12023 #endif /* not TRILIBRARY */
12025 traversalinit(&shelles);
12026 shelleloop.sh = shelletraverse();
12027 shelleloop.shorient = 0;
12028 shellenumber = firstnumber;
12029 while (shelleloop.sh != (shelle *) NULL) {
12030 sorg(shelleloop, endpoint1);
12031 sdest(shelleloop, endpoint2);
12033 /* Copy indices of the segment's two endpoints. */
12034 slist[index++] = pointmark(endpoint1);
12035 slist[index++] = pointmark(endpoint2);
12037 /* Copy the boundary marker. */
12038 smlist[shellenumber - firstnumber] = mark(shelleloop);
12040 #else /* not TRILIBRARY */
12041 /* Segment number, indices of its two endpoints, and possibly a marker. */
12043 fprintf(outfile, "%4d %4d %4d\n", shellenumber,
12044 pointmark(endpoint1), pointmark(endpoint2));
12046 fprintf(outfile, "%4d %4d %4d %4d\n", shellenumber,
12047 pointmark(endpoint1), pointmark(endpoint2), mark(shelleloop));
12049 #endif /* not TRILIBRARY */
12051 shelleloop.sh = shelletraverse();
12057 fprintf(outfile, "%d\n", holes);
12059 for (i = 0; i < holes; i++) {
12060 /* Hole number, x and y coordinates. */
12061 fprintf(outfile, "%4d %.17g %.17g\n", firstnumber + i,
12062 holelist[2 * i], holelist[2 * i + 1]);
12066 fprintf(outfile, "%d\n", regions);
12067 for (i = 0; i < regions; i++) {
12068 /* Region number, x and y coordinates, attribute, maximum area. */
12069 fprintf(outfile, "%4d %.17g %.17g %.17g %.17g\n", firstnumber + i,
12070 regionlist[4 * i], regionlist[4 * i + 1],
12071 regionlist[4 * i + 2], regionlist[4 * i + 3]);
12074 #endif /* not CDT_ONLY */
12076 finishfile(outfile, argc, argv);
12077 #endif /* not TRILIBRARY */
12080 /*****************************************************************************/
12082 /* writeedges() Write the edges to a .edge file. */
12084 /*****************************************************************************/
12088 void writeedges(edgelist, edgemarkerlist)
12090 int **edgemarkerlist;
12092 #else /* not TRILIBRARY */
12094 void writeedges(edgefilename, argc, argv)
12095 char *edgefilename;
12099 #endif /* not TRILIBRARY */
12106 #else /* not TRILIBRARY */
12108 #endif /* not TRILIBRARY */
12109 struct triedge triangleloop, trisym;
12110 struct edge checkmark;
12113 triangle ptr; /* Temporary variable used by sym(). */
12114 shelle sptr; /* Temporary variable used by tspivot(). */
12118 printf("Writing edges.\n");
12120 /* Allocate memory for edges if necessary. */
12121 if (*edgelist == (int *) NULL) {
12122 *edgelist = (int *) malloc(edges * 2 * sizeof(int));
12123 if (*edgelist == (int *) NULL) {
12124 printf("Error: Out of memory.\n");
12128 /* Allocate memory for edge markers if necessary. */
12129 if (!nobound && (*edgemarkerlist == (int *) NULL)) {
12130 *edgemarkerlist = (int *) malloc(edges * sizeof(int));
12131 if (*edgemarkerlist == (int *) NULL) {
12132 printf("Error: Out of memory.\n");
12137 emlist = *edgemarkerlist;
12139 #else /* not TRILIBRARY */
12141 printf("Writing %s.\n", edgefilename);
12143 outfile = fopen(edgefilename, "w");
12144 if (outfile == (FILE *) NULL) {
12145 printf(" Error: Cannot create file %s.\n", edgefilename);
12148 /* Number of edges, number of boundary markers (zero or one). */
12149 fprintf(outfile, "%ld %d\n", edges, 1 - nobound);
12150 #endif /* not TRILIBRARY */
12152 traversalinit(&triangles);
12153 triangleloop.tri = triangletraverse();
12154 edgenumber = firstnumber;
12155 /* To loop over the set of edges, loop over all triangles, and look at */
12156 /* the three edges of each triangle. If there isn't another triangle */
12157 /* adjacent to the edge, operate on the edge. If there is another */
12158 /* adjacent triangle, operate on the edge only if the current triangle */
12159 /* has a smaller pointer than its neighbor. This way, each edge is */
12160 /* considered only once. */
12161 while (triangleloop.tri != (triangle *) NULL) {
12162 for (triangleloop.orient = 0; triangleloop.orient < 3;
12163 triangleloop.orient++) {
12164 sym(triangleloop, trisym);
12165 if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
12166 org(triangleloop, p1);
12167 dest(triangleloop, p2);
12169 elist[index++] = pointmark(p1);
12170 elist[index++] = pointmark(p2);
12171 #endif /* TRILIBRARY */
12174 /* Edge number, indices of two endpoints. */
12175 fprintf(outfile, "%4d %d %d\n", edgenumber,
12176 pointmark(p1), pointmark(p2));
12177 #endif /* not TRILIBRARY */
12179 /* Edge number, indices of two endpoints, and a boundary marker. */
12180 /* If there's no shell edge, the boundary marker is zero. */
12182 tspivot(triangleloop, checkmark);
12183 if (checkmark.sh == dummysh) {
12185 emlist[edgenumber - firstnumber] = 0;
12186 #else /* not TRILIBRARY */
12187 fprintf(outfile, "%4d %d %d %d\n", edgenumber,
12188 pointmark(p1), pointmark(p2), 0);
12189 #endif /* not TRILIBRARY */
12192 emlist[edgenumber - firstnumber] = mark(checkmark);
12193 #else /* not TRILIBRARY */
12194 fprintf(outfile, "%4d %d %d %d\n", edgenumber,
12195 pointmark(p1), pointmark(p2), mark(checkmark));
12196 #endif /* not TRILIBRARY */
12200 emlist[edgenumber - firstnumber] = trisym.tri == dummytri;
12201 #else /* not TRILIBRARY */
12202 fprintf(outfile, "%4d %d %d %d\n", edgenumber,
12203 pointmark(p1), pointmark(p2), trisym.tri == dummytri);
12204 #endif /* not TRILIBRARY */
12210 triangleloop.tri = triangletraverse();
12214 finishfile(outfile, argc, argv);
12215 #endif /* not TRILIBRARY */
12218 /*****************************************************************************/
12220 /* writevoronoi() Write the Voronoi diagram to a .v.node and .v.edge */
12223 /* The Voronoi diagram is the geometric dual of the Delaunay triangulation. */
12224 /* Hence, the Voronoi vertices are listed by traversing the Delaunay */
12225 /* triangles, and the Voronoi edges are listed by traversing the Delaunay */
12228 /* WARNING: In order to assign numbers to the Voronoi vertices, this */
12229 /* procedure messes up the shell edges or the extra nodes of every */
12230 /* element. Hence, you should call this procedure last. */
12232 /*****************************************************************************/
12236 void writevoronoi(vpointlist, vpointattriblist, vpointmarkerlist, vedgelist,
12237 vedgemarkerlist, vnormlist)
12239 REAL **vpointattriblist;
12240 int **vpointmarkerlist;
12242 int **vedgemarkerlist;
12245 #else /* not TRILIBRARY */
12247 void writevoronoi(vnodefilename, vedgefilename, argc, argv)
12248 char *vnodefilename;
12249 char *vedgefilename;
12253 #endif /* not TRILIBRARY */
12263 #else /* not TRILIBRARY */
12265 #endif /* not TRILIBRARY */
12266 struct triedge triangleloop, trisym;
12267 point torg, tdest, tapex;
12268 REAL circumcenter[2];
12270 int vnodenumber, vedgenumber;
12273 triangle ptr; /* Temporary variable used by sym(). */
12277 printf("Writing Voronoi vertices.\n");
12279 /* Allocate memory for Voronoi vertices if necessary. */
12280 if (*vpointlist == (REAL *) NULL) {
12281 *vpointlist = (REAL *) malloc(triangles.items * 2 * sizeof(REAL));
12282 if (*vpointlist == (REAL *) NULL) {
12283 printf("Error: Out of memory.\n");
12287 /* Allocate memory for Voronoi vertex attributes if necessary. */
12288 if (*vpointattriblist == (REAL *) NULL) {
12289 *vpointattriblist = (REAL *) malloc(triangles.items * nextras *
12291 if (*vpointattriblist == (REAL *) NULL) {
12292 printf("Error: Out of memory.\n");
12296 *vpointmarkerlist = (int *) NULL;
12297 plist = *vpointlist;
12298 palist = *vpointattriblist;
12301 #else /* not TRILIBRARY */
12303 printf("Writing %s.\n", vnodefilename);
12305 outfile = fopen(vnodefilename, "w");
12306 if (outfile == (FILE *) NULL) {
12307 printf(" Error: Cannot create file %s.\n", vnodefilename);
12310 /* Number of triangles, two dimensions, number of point attributes, */
12311 /* zero markers. */
12312 fprintf(outfile, "%ld %d %d %d\n", triangles.items, 2, nextras, 0);
12313 #endif /* not TRILIBRARY */
12315 traversalinit(&triangles);
12316 triangleloop.tri = triangletraverse();
12317 triangleloop.orient = 0;
12318 vnodenumber = firstnumber;
12319 while (triangleloop.tri != (triangle *) NULL) {
12320 org(triangleloop, torg);
12321 dest(triangleloop, tdest);
12322 apex(triangleloop, tapex);
12323 findcircumcenter(torg, tdest, tapex, circumcenter, &xi, &eta);
12325 /* X and y coordinates. */
12326 plist[coordindex++] = circumcenter[0];
12327 plist[coordindex++] = circumcenter[1];
12328 for (i = 2; i < 2 + nextras; i++) {
12329 /* Interpolate the point attributes at the circumcenter. */
12330 palist[attribindex++] = torg[i] + xi * (tdest[i] - torg[i])
12331 + eta * (tapex[i] - torg[i]);
12333 #else /* not TRILIBRARY */
12334 /* Voronoi vertex number, x and y coordinates. */
12335 fprintf(outfile, "%4d %.17g %.17g", vnodenumber, circumcenter[0],
12337 for (i = 2; i < 2 + nextras; i++) {
12338 /* Interpolate the point attributes at the circumcenter. */
12339 fprintf(outfile, " %.17g", torg[i] + xi * (tdest[i] - torg[i])
12340 + eta * (tapex[i] - torg[i]));
12342 fprintf(outfile, "\n");
12343 #endif /* not TRILIBRARY */
12345 * (int *) (triangleloop.tri + 6) = vnodenumber;
12346 triangleloop.tri = triangletraverse();
12351 finishfile(outfile, argc, argv);
12352 #endif /* not TRILIBRARY */
12356 printf("Writing Voronoi edges.\n");
12358 /* Allocate memory for output Voronoi edges if necessary. */
12359 if (*vedgelist == (int *) NULL) {
12360 *vedgelist = (int *) malloc(edges * 2 * sizeof(int));
12361 if (*vedgelist == (int *) NULL) {
12362 printf("Error: Out of memory.\n");
12366 *vedgemarkerlist = (int *) NULL;
12367 /* Allocate memory for output Voronoi norms if necessary. */
12368 if (*vnormlist == (REAL *) NULL) {
12369 *vnormlist = (REAL *) malloc(edges * 2 * sizeof(REAL));
12370 if (*vnormlist == (REAL *) NULL) {
12371 printf("Error: Out of memory.\n");
12375 elist = *vedgelist;
12376 normlist = *vnormlist;
12378 #else /* not TRILIBRARY */
12380 printf("Writing %s.\n", vedgefilename);
12382 outfile = fopen(vedgefilename, "w");
12383 if (outfile == (FILE *) NULL) {
12384 printf(" Error: Cannot create file %s.\n", vedgefilename);
12387 /* Number of edges, zero boundary markers. */
12388 fprintf(outfile, "%ld %d\n", edges, 0);
12389 #endif /* not TRILIBRARY */
12391 traversalinit(&triangles);
12392 triangleloop.tri = triangletraverse();
12393 vedgenumber = firstnumber;
12394 /* To loop over the set of edges, loop over all triangles, and look at */
12395 /* the three edges of each triangle. If there isn't another triangle */
12396 /* adjacent to the edge, operate on the edge. If there is another */
12397 /* adjacent triangle, operate on the edge only if the current triangle */
12398 /* has a smaller pointer than its neighbor. This way, each edge is */
12399 /* considered only once. */
12400 while (triangleloop.tri != (triangle *) NULL) {
12401 for (triangleloop.orient = 0; triangleloop.orient < 3;
12402 triangleloop.orient++) {
12403 sym(triangleloop, trisym);
12404 if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
12405 /* Find the number of this triangle (and Voronoi vertex). */
12406 p1 = * (int *) (triangleloop.tri + 6);
12407 if (trisym.tri == dummytri) {
12408 org(triangleloop, torg);
12409 dest(triangleloop, tdest);
12411 /* Copy an infinite ray. Index of one endpoint, and -1. */
12412 elist[coordindex] = p1;
12413 normlist[coordindex++] = tdest[1] - torg[1];
12414 elist[coordindex] = -1;
12415 normlist[coordindex++] = torg[0] - tdest[0];
12416 #else /* not TRILIBRARY */
12417 /* Write an infinite ray. Edge number, index of one endpoint, -1, */
12418 /* and x and y coordinates of a vector representing the */
12419 /* direction of the ray. */
12420 fprintf(outfile, "%4d %d %d %.17g %.17g\n", vedgenumber,
12421 p1, -1, tdest[1] - torg[1], torg[0] - tdest[0]);
12422 #endif /* not TRILIBRARY */
12424 /* Find the number of the adjacent triangle (and Voronoi vertex). */
12425 p2 = * (int *) (trisym.tri + 6);
12426 /* Finite edge. Write indices of two endpoints. */
12428 elist[coordindex] = p1;
12429 normlist[coordindex++] = 0.0;
12430 elist[coordindex] = p2;
12431 normlist[coordindex++] = 0.0;
12432 #else /* not TRILIBRARY */
12433 fprintf(outfile, "%4d %d %d\n", vedgenumber, p1, p2);
12434 #endif /* not TRILIBRARY */
12439 triangleloop.tri = triangletraverse();
12443 finishfile(outfile, argc, argv);
12444 #endif /* not TRILIBRARY */
12449 void writeneighbors(neighborlist)
12450 int **neighborlist;
12452 #else /* not TRILIBRARY */
12454 void writeneighbors(neighborfilename, argc, argv)
12455 char *neighborfilename;
12459 #endif /* not TRILIBRARY */
12465 #else /* not TRILIBRARY */
12467 #endif /* not TRILIBRARY */
12468 struct triedge triangleloop, trisym;
12470 int neighbor1, neighbor2, neighbor3;
12471 triangle ptr; /* Temporary variable used by sym(). */
12475 printf("Writing neighbors.\n");
12477 /* Allocate memory for neighbors if necessary. */
12478 if (*neighborlist == (int *) NULL) {
12479 *neighborlist = (int *) malloc(triangles.items * 3 * sizeof(int));
12480 if (*neighborlist == (int *) NULL) {
12481 printf("Error: Out of memory.\n");
12485 nlist = *neighborlist;
12487 #else /* not TRILIBRARY */
12489 printf("Writing %s.\n", neighborfilename);
12491 outfile = fopen(neighborfilename, "w");
12492 if (outfile == (FILE *) NULL) {
12493 printf(" Error: Cannot create file %s.\n", neighborfilename);
12496 /* Number of triangles, three edges per triangle. */
12497 fprintf(outfile, "%ld %d\n", triangles.items, 3);
12498 #endif /* not TRILIBRARY */
12500 traversalinit(&triangles);
12501 triangleloop.tri = triangletraverse();
12502 triangleloop.orient = 0;
12503 elementnumber = firstnumber;
12504 while (triangleloop.tri != (triangle *) NULL) {
12505 * (int *) (triangleloop.tri + 6) = elementnumber;
12506 triangleloop.tri = triangletraverse();
12509 * (int *) (dummytri + 6) = -1;
12511 traversalinit(&triangles);
12512 triangleloop.tri = triangletraverse();
12513 elementnumber = firstnumber;
12514 while (triangleloop.tri != (triangle *) NULL) {
12515 triangleloop.orient = 1;
12516 sym(triangleloop, trisym);
12517 neighbor1 = * (int *) (trisym.tri + 6);
12518 triangleloop.orient = 2;
12519 sym(triangleloop, trisym);
12520 neighbor2 = * (int *) (trisym.tri + 6);
12521 triangleloop.orient = 0;
12522 sym(triangleloop, trisym);
12523 neighbor3 = * (int *) (trisym.tri + 6);
12525 nlist[index++] = neighbor1;
12526 nlist[index++] = neighbor2;
12527 nlist[index++] = neighbor3;
12528 #else /* not TRILIBRARY */
12529 /* Triangle number, neighboring triangle numbers. */
12530 fprintf(outfile, "%4d %d %d %d\n", elementnumber,
12531 neighbor1, neighbor2, neighbor3);
12532 #endif /* not TRILIBRARY */
12534 triangleloop.tri = triangletraverse();
12539 finishfile(outfile, argc, argv);
12540 #endif /* TRILIBRARY */
12543 /*****************************************************************************/
12545 /* writeoff() Write the triangulation to an .off file. */
12547 /* OFF stands for the Object File Format, a format used by the Geometry */
12548 /* Center's Geomview package. */
12550 /*****************************************************************************/
12554 void writeoff(offfilename, argc, argv)
12560 struct triedge triangleloop;
12565 printf("Writing %s.\n", offfilename);
12567 outfile = fopen(offfilename, "w");
12568 if (outfile == (FILE *) NULL) {
12569 printf(" Error: Cannot create file %s.\n", offfilename);
12572 /* Number of points, triangles, and edges. */
12573 fprintf(outfile, "OFF\n%ld %ld %ld\n", points.items, triangles.items,
12576 /* Write the points. */
12577 traversalinit(&points);
12578 pointloop = pointtraverse();
12579 while (pointloop != (point) NULL) {
12580 /* The "0.0" is here because the OFF format uses 3D coordinates. */
12581 fprintf(outfile, " %.17g %.17g %.17g\n", pointloop[0],
12582 pointloop[1], 0.0);
12583 pointloop = pointtraverse();
12586 /* Write the triangles. */
12587 traversalinit(&triangles);
12588 triangleloop.tri = triangletraverse();
12589 triangleloop.orient = 0;
12590 while (triangleloop.tri != (triangle *) NULL) {
12591 org(triangleloop, p1);
12592 dest(triangleloop, p2);
12593 apex(triangleloop, p3);
12594 /* The "3" means a three-vertex polygon. */
12595 fprintf(outfile, " 3 %4d %4d %4d\n", pointmark(p1) - 1,
12596 pointmark(p2) - 1, pointmark(p3) - 1);
12597 triangleloop.tri = triangletraverse();
12599 finishfile(outfile, argc, argv);
12602 #endif /* not TRILIBRARY */
12606 /********* File I/O routines end here *********/
12608 /*****************************************************************************/
12610 /* quality_statistics() Print statistics about the quality of the mesh. */
12612 /*****************************************************************************/
12614 void quality_statistics()
12616 struct triedge triangleloop;
12618 REAL cossquaretable[8];
12619 REAL ratiotable[16];
12621 REAL edgelength[3];
12625 REAL shortest, longest;
12627 REAL smallestarea, biggestarea;
12628 REAL triminaltitude2;
12632 REAL smallestangle, biggestangle;
12633 REAL radconst, degconst;
12634 int angletable[18];
12635 int aspecttable[16];
12641 printf("Mesh quality statistics:\n\n");
12642 radconst = PI / 18.0;
12643 degconst = 180.0 / PI;
12644 for (i = 0; i < 8; i++) {
12645 cossquaretable[i] = cos(radconst * (REAL) (i + 1));
12646 cossquaretable[i] = cossquaretable[i] * cossquaretable[i];
12648 for (i = 0; i < 18; i++) {
12652 ratiotable[0] = 1.5; ratiotable[1] = 2.0;
12653 ratiotable[2] = 2.5; ratiotable[3] = 3.0;
12654 ratiotable[4] = 4.0; ratiotable[5] = 6.0;
12655 ratiotable[6] = 10.0; ratiotable[7] = 15.0;
12656 ratiotable[8] = 25.0; ratiotable[9] = 50.0;
12657 ratiotable[10] = 100.0; ratiotable[11] = 300.0;
12658 ratiotable[12] = 1000.0; ratiotable[13] = 10000.0;
12659 ratiotable[14] = 100000.0; ratiotable[15] = 0.0;
12660 for (i = 0; i < 16; i++) {
12661 aspecttable[i] = 0;
12665 minaltitude = xmax - xmin + ymax - ymin;
12666 minaltitude = minaltitude * minaltitude;
12667 shortest = minaltitude;
12669 smallestarea = minaltitude;
12672 smallestangle = 0.0;
12673 biggestangle = 2.0;
12676 traversalinit(&triangles);
12677 triangleloop.tri = triangletraverse();
12678 triangleloop.orient = 0;
12679 while (triangleloop.tri != (triangle *) NULL) {
12680 org(triangleloop, p[0]);
12681 dest(triangleloop, p[1]);
12682 apex(triangleloop, p[2]);
12685 for (i = 0; i < 3; i++) {
12688 dx[i] = p[j][0] - p[k][0];
12689 dy[i] = p[j][1] - p[k][1];
12690 edgelength[i] = dx[i] * dx[i] + dy[i] * dy[i];
12691 if (edgelength[i] > trilongest2) {
12692 trilongest2 = edgelength[i];
12694 if (edgelength[i] > longest) {
12695 longest = edgelength[i];
12697 if (edgelength[i] < shortest) {
12698 shortest = edgelength[i];
12702 triarea = counterclockwise(p[0], p[1], p[2]);
12703 if (triarea < smallestarea) {
12704 smallestarea = triarea;
12706 if (triarea > biggestarea) {
12707 biggestarea = triarea;
12709 triminaltitude2 = triarea * triarea / trilongest2;
12710 if (triminaltitude2 < minaltitude) {
12711 minaltitude = triminaltitude2;
12713 triaspect2 = trilongest2 / triminaltitude2;
12714 if (triaspect2 > worstaspect) {
12715 worstaspect = triaspect2;
12718 while ((triaspect2 > ratiotable[aspectindex] * ratiotable[aspectindex])
12719 && (aspectindex < 15)) {
12722 aspecttable[aspectindex]++;
12724 for (i = 0; i < 3; i++) {
12727 dotproduct = dx[j] * dx[k] + dy[j] * dy[k];
12728 cossquare = dotproduct * dotproduct / (edgelength[j] * edgelength[k]);
12730 for (ii = 7; ii >= 0; ii--) {
12731 if (cossquare > cossquaretable[ii]) {
12735 if (dotproduct <= 0.0) {
12736 angletable[tendegree]++;
12737 if (cossquare > smallestangle) {
12738 smallestangle = cossquare;
12740 if (acutebiggest && (cossquare < biggestangle)) {
12741 biggestangle = cossquare;
12744 angletable[17 - tendegree]++;
12745 if (acutebiggest || (cossquare > biggestangle)) {
12746 biggestangle = cossquare;
12751 triangleloop.tri = triangletraverse();
12754 shortest = sqrt(shortest);
12755 longest = sqrt(longest);
12756 minaltitude = sqrt(minaltitude);
12757 worstaspect = sqrt(worstaspect);
12758 smallestarea *= 2.0;
12759 biggestarea *= 2.0;
12760 if (smallestangle >= 1.0) {
12761 smallestangle = 0.0;
12763 smallestangle = degconst * acos(sqrt(smallestangle));
12765 if (biggestangle >= 1.0) {
12766 biggestangle = 180.0;
12768 if (acutebiggest) {
12769 biggestangle = degconst * acos(sqrt(biggestangle));
12771 biggestangle = 180.0 - degconst * acos(sqrt(biggestangle));
12775 printf(" Smallest area: %16.5g | Largest area: %16.5g\n",
12776 smallestarea, biggestarea);
12777 printf(" Shortest edge: %16.5g | Longest edge: %16.5g\n",
12778 shortest, longest);
12779 printf(" Shortest altitude: %12.5g | Largest aspect ratio: %8.5g\n\n",
12780 minaltitude, worstaspect);
12781 printf(" Aspect ratio histogram:\n");
12782 printf(" 1.1547 - %-6.6g : %8d | %6.6g - %-6.6g : %8d\n",
12783 ratiotable[0], aspecttable[0], ratiotable[7], ratiotable[8],
12785 for (i = 1; i < 7; i++) {
12786 printf(" %6.6g - %-6.6g : %8d | %6.6g - %-6.6g : %8d\n",
12787 ratiotable[i - 1], ratiotable[i], aspecttable[i],
12788 ratiotable[i + 7], ratiotable[i + 8], aspecttable[i + 8]);
12790 printf(" %6.6g - %-6.6g : %8d | %6.6g - : %8d\n",
12791 ratiotable[6], ratiotable[7], aspecttable[7], ratiotable[14],
12794 " (Triangle aspect ratio is longest edge divided by shortest altitude)\n\n");
12795 printf(" Smallest angle: %15.5g | Largest angle: %15.5g\n\n",
12796 smallestangle, biggestangle);
12797 printf(" Angle histogram:\n");
12798 for (i = 0; i < 9; i++) {
12799 printf(" %3d - %3d degrees: %8d | %3d - %3d degrees: %8d\n",
12800 i * 10, i * 10 + 10, angletable[i],
12801 i * 10 + 90, i * 10 + 100, angletable[i + 9]);
12806 /*****************************************************************************/
12808 /* statistics() Print all sorts of cool facts. */
12810 /*****************************************************************************/
12814 printf("\nStatistics:\n\n");
12815 printf(" Input points: %d\n", inpoints);
12817 printf(" Input triangles: %d\n", inelements);
12820 printf(" Input segments: %d\n", insegments);
12822 printf(" Input holes: %d\n", holes);
12826 printf("\n Mesh points: %ld\n", points.items);
12827 printf(" Mesh triangles: %ld\n", triangles.items);
12828 printf(" Mesh edges: %ld\n", edges);
12829 if (poly || refine) {
12830 printf(" Mesh boundary edges: %ld\n", hullsize);
12831 printf(" Mesh segments: %ld\n\n", shelles.items);
12833 printf(" Mesh convex hull edges: %ld\n\n", hullsize);
12836 quality_statistics();
12837 printf("Memory allocation statistics:\n\n");
12838 printf(" Maximum number of points: %ld\n", points.maxitems);
12839 printf(" Maximum number of triangles: %ld\n", triangles.maxitems);
12840 if (shelles.maxitems > 0) {
12841 printf(" Maximum number of segments: %ld\n", shelles.maxitems);
12843 if (viri.maxitems > 0) {
12844 printf(" Maximum number of viri: %ld\n", viri.maxitems);
12846 if (badsegments.maxitems > 0) {
12847 printf(" Maximum number of encroached segments: %ld\n",
12848 badsegments.maxitems);
12850 if (badtriangles.maxitems > 0) {
12851 printf(" Maximum number of bad triangles: %ld\n",
12852 badtriangles.maxitems);
12854 if (splaynodes.maxitems > 0) {
12855 printf(" Maximum number of splay tree nodes: %ld\n",
12856 splaynodes.maxitems);
12858 printf(" Approximate heap memory use (bytes): %ld\n\n",
12859 points.maxitems * points.itembytes
12860 + triangles.maxitems * triangles.itembytes
12861 + shelles.maxitems * shelles.itembytes
12862 + viri.maxitems * viri.itembytes
12863 + badsegments.maxitems * badsegments.itembytes
12864 + badtriangles.maxitems * badtriangles.itembytes
12865 + splaynodes.maxitems * splaynodes.itembytes);
12867 printf("Algorithmic statistics:\n\n");
12868 printf(" Number of incircle tests: %ld\n", incirclecount);
12869 printf(" Number of orientation tests: %ld\n", counterclockcount);
12870 if (hyperbolacount > 0) {
12871 printf(" Number of right-of-hyperbola tests: %ld\n",
12874 if (circumcentercount > 0) {
12875 printf(" Number of circumcenter computations: %ld\n",
12876 circumcentercount);
12878 if (circletopcount > 0) {
12879 printf(" Number of circle top computations: %ld\n",
12886 /*****************************************************************************/
12888 /* main() or triangulate() Gosh, do everything. */
12890 /* The sequence is roughly as follows. Many of these steps can be skipped, */
12891 /* depending on the command line switches. */
12893 /* - Initialize constants and parse the command line. */
12894 /* - Read the points from a file and either */
12895 /* - triangulate them (no -r), or */
12896 /* - read an old mesh from files and reconstruct it (-r). */
12897 /* - Insert the PSLG segments (-p), and possibly segments on the convex */
12899 /* - Read the holes (-p), regional attributes (-pA), and regional area */
12900 /* constraints (-pa). Carve the holes and concavities, and spread the */
12901 /* regional attributes and area constraints. */
12902 /* - Enforce the constraints on minimum angle (-q) and maximum area (-a). */
12903 /* Also enforce the conforming Delaunay property (-q and -a). */
12904 /* - Compute the number of edges in the resulting mesh. */
12905 /* - Promote the mesh's linear triangles to higher order elements (-o). */
12906 /* - Write the output files and print the statistics. */
12907 /* - Check the consistency and Delaunay property of the mesh (-C). */
12909 /*****************************************************************************/
12913 void triangulate(triswitches, in, out, vorout)
12915 struct triangulateio *in;
12916 struct triangulateio *out;
12917 struct triangulateio *vorout;
12919 #else /* not TRILIBRARY */
12921 int main(argc, argv)
12925 #endif /* not TRILIBRARY */
12928 REAL *holearray; /* Array of holes. */
12929 REAL *regionarray; /* Array of regional attributes and area constraints. */
12932 #endif /* not TRILIBRARY */
12934 /* Variables for timing the performance of Triangle. The types are */
12935 /* defined in sys/time.h. */
12936 struct timeval tv0, tv1, tv2, tv3, tv4, tv5, tv6;
12937 struct timezone tz;
12938 #endif /* NO_TIMER */
12941 gettimeofday(&tv0, &tz);
12942 #endif /* NO_TIMER */
12946 parsecommandline(1, &triswitches);
12947 #else /* not TRILIBRARY */
12948 parsecommandline(argc, argv);
12949 #endif /* not TRILIBRARY */
12952 transfernodes(in->pointlist, in->pointattributelist, in->pointmarkerlist,
12953 in->numberofpoints, in->numberofpointattributes);
12954 #else /* not TRILIBRARY */
12955 readnodes(innodefilename, inpolyfilename, &polyfile);
12956 #endif /* not TRILIBRARY */
12960 gettimeofday(&tv1, &tz);
12962 #endif /* NO_TIMER */
12965 hullsize = delaunay(); /* Triangulate the points. */
12966 #else /* not CDT_ONLY */
12968 /* Read and reconstruct a mesh. */
12970 hullsize = reconstruct(in->trianglelist, in->triangleattributelist,
12971 in->trianglearealist, in->numberoftriangles,
12972 in->numberofcorners, in->numberoftriangleattributes,
12973 in->segmentlist, in->segmentmarkerlist,
12974 in->numberofsegments);
12975 #else /* not TRILIBRARY */
12976 hullsize = reconstruct(inelefilename, areafilename, inpolyfilename,
12978 #endif /* not TRILIBRARY */
12980 hullsize = delaunay(); /* Triangulate the points. */
12982 #endif /* not CDT_ONLY */
12986 gettimeofday(&tv2, &tz);
12988 printf("Mesh reconstruction");
12990 printf("Delaunay");
12992 printf(" milliseconds: %ld\n", 1000l * (tv2.tv_sec - tv1.tv_sec)
12993 + (tv2.tv_usec - tv1.tv_usec) / 1000l);
12995 #endif /* NO_TIMER */
12997 /* Ensure that no point can be mistaken for a triangular bounding */
12998 /* box point in insertsite(). */
12999 infpoint1 = (point) NULL;
13000 infpoint2 = (point) NULL;
13001 infpoint3 = (point) NULL;
13004 checksegments = 1; /* Segments will be introduced next. */
13006 /* Insert PSLG segments and/or convex hull segments. */
13008 insegments = formskeleton(in->segmentlist, in->segmentmarkerlist,
13009 in->numberofsegments);
13010 #else /* not TRILIBRARY */
13011 insegments = formskeleton(polyfile, inpolyfilename);
13012 #endif /* not TRILIBRARY */
13018 gettimeofday(&tv3, &tz);
13019 if (useshelles && !refine) {
13020 printf("Segment milliseconds: %ld\n",
13021 1000l * (tv3.tv_sec - tv2.tv_sec)
13022 + (tv3.tv_usec - tv2.tv_usec) / 1000l);
13025 #endif /* NO_TIMER */
13029 holearray = in->holelist;
13030 holes = in->numberofholes;
13031 regionarray = in->regionlist;
13032 regions = in->numberofregions;
13033 #else /* not TRILIBRARY */
13034 readholes(polyfile, inpolyfilename, &holearray, &holes,
13035 ®ionarray, ®ions);
13036 #endif /* not TRILIBRARY */
13038 /* Carve out holes and concavities. */
13039 carveholes(holearray, holes, regionarray, regions);
13042 /* Without a PSLG, there can be no holes or regional attributes */
13043 /* or area constraints. The following are set to zero to avoid */
13044 /* an accidental free() later. */
13051 gettimeofday(&tv4, &tz);
13052 if (poly && !refine) {
13053 printf("Hole milliseconds: %ld\n", 1000l * (tv4.tv_sec - tv3.tv_sec)
13054 + (tv4.tv_usec - tv3.tv_usec) / 1000l);
13057 #endif /* NO_TIMER */
13061 enforcequality(); /* Enforce angle and area constraints. */
13063 #endif /* not CDT_ONLY */
13067 gettimeofday(&tv5, &tz);
13070 printf("Quality milliseconds: %ld\n",
13071 1000l * (tv5.tv_sec - tv4.tv_sec)
13072 + (tv5.tv_usec - tv4.tv_usec) / 1000l);
13074 #endif /* not CDT_ONLY */
13076 #endif /* NO_TIMER */
13078 /* Compute the number of edges. */
13079 edges = (3l * triangles.items + hullsize) / 2l;
13082 highorder(); /* Promote elements to higher polynomial order. */
13089 out->numberofpoints = points.items;
13090 out->numberofpointattributes = nextras;
13091 out->numberoftriangles = triangles.items;
13092 out->numberofcorners = (order + 1) * (order + 2) / 2;
13093 out->numberoftriangleattributes = eextras;
13094 out->numberofedges = edges;
13096 out->numberofsegments = shelles.items;
13098 out->numberofsegments = hullsize;
13100 if (vorout != (struct triangulateio *) NULL) {
13101 vorout->numberofpoints = triangles.items;
13102 vorout->numberofpointattributes = nextras;
13103 vorout->numberofedges = edges;
13105 #endif /* TRILIBRARY */
13106 /* If not using iteration numbers, don't write a .node file if one was */
13107 /* read, because the original one would be overwritten! */
13108 if (nonodewritten || (noiterationnum && readnodefile)) {
13111 printf("NOT writing points.\n");
13112 #else /* not TRILIBRARY */
13113 printf("NOT writing a .node file.\n");
13114 #endif /* not TRILIBRARY */
13116 numbernodes(); /* We must remember to number the points. */
13119 writenodes(&out->pointlist, &out->pointattributelist,
13120 &out->pointmarkerlist);
13121 #else /* not TRILIBRARY */
13122 writenodes(outnodefilename, argc, argv); /* Numbers the points too. */
13123 #endif /* TRILIBRARY */
13125 if (noelewritten) {
13128 printf("NOT writing triangles.\n");
13129 #else /* not TRILIBRARY */
13130 printf("NOT writing an .ele file.\n");
13131 #endif /* not TRILIBRARY */
13135 writeelements(&out->trianglelist, &out->triangleattributelist);
13136 #else /* not TRILIBRARY */
13137 writeelements(outelefilename, argc, argv);
13138 #endif /* not TRILIBRARY */
13140 /* The -c switch (convex switch) causes a PSLG to be written */
13141 /* even if none was read. */
13142 if (poly || convex) {
13143 /* If not using iteration numbers, don't overwrite the .poly file. */
13144 if (nopolywritten || noiterationnum) {
13147 printf("NOT writing segments.\n");
13148 #else /* not TRILIBRARY */
13149 printf("NOT writing a .poly file.\n");
13150 #endif /* not TRILIBRARY */
13154 writepoly(&out->segmentlist, &out->segmentmarkerlist);
13155 out->numberofholes = holes;
13156 out->numberofregions = regions;
13158 out->holelist = in->holelist;
13159 out->regionlist = in->regionlist;
13161 out->holelist = (REAL *) NULL;
13162 out->regionlist = (REAL *) NULL;
13164 #else /* not TRILIBRARY */
13165 writepoly(outpolyfilename, holearray, holes, regionarray, regions,
13167 #endif /* not TRILIBRARY */
13175 #endif /* not CDT_ONLY */
13180 writeoff(offfilename, argc, argv);
13182 #endif /* not TRILIBRARY */
13185 writeedges(&out->edgelist, &out->edgemarkerlist);
13186 #else /* not TRILIBRARY */
13187 writeedges(edgefilename, argc, argv);
13188 #endif /* not TRILIBRARY */
13192 writevoronoi(&vorout->pointlist, &vorout->pointattributelist,
13193 &vorout->pointmarkerlist, &vorout->edgelist,
13194 &vorout->edgemarkerlist, &vorout->normlist);
13195 #else /* not TRILIBRARY */
13196 writevoronoi(vnodefilename, vedgefilename, argc, argv);
13197 #endif /* not TRILIBRARY */
13201 writeneighbors(&out->neighborlist);
13202 #else /* not TRILIBRARY */
13203 writeneighbors(neighborfilename, argc, argv);
13204 #endif /* not TRILIBRARY */
13209 gettimeofday(&tv6, &tz);
13210 printf("\nOutput milliseconds: %ld\n",
13211 1000l * (tv6.tv_sec - tv5.tv_sec)
13212 + (tv6.tv_usec - tv5.tv_usec) / 1000l);
13213 printf("Total running milliseconds: %ld\n",
13214 1000l * (tv6.tv_sec - tv0.tv_sec)
13215 + (tv6.tv_usec - tv0.tv_usec) / 1000l);
13216 #endif /* NO_TIMER */
13226 #endif /* not REDUCED */
13231 #endif /* not TRILIBRARY */