Logo ROOT  
Reference Guide
triangle.c
Go to the documentation of this file.
1 /*****************************************************************************/
2 /* */
3 /* 888888888 ,o, / 888 */
4 /* 888 88o88o " o8888o 88o8888o o88888o 888 o88888o */
5 /* 888 888 888 88b 888 888 888 888 888 d888 88b */
6 /* 888 888 888 o88^o888 888 888 "88888" 888 8888oo888 */
7 /* 888 888 888 C888 888 888 888 / 888 q888 */
8 /* 888 888 888 "88o^888 888 888 Cb 888 "88oooo" */
9 /* "8oo8D */
10 /* */
11 /* A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator. */
12 /* (triangle.c) */
13 /* */
14 /* Version 1.6 */
15 /* July 28, 2005 */
16 /* */
17 /* Copyright 1993, 1995, 1997, 1998, 2002, 2005 */
18 /* Jonathan Richard Shewchuk */
19 /* 2360 Woolsey #H */
20 /* Berkeley, California 94705-1927 */
21 /* jrs@cs.berkeley.edu */
22 /* */
23 /* This program may be freely redistributed under the condition that the */
24 /* copyright notices (including this entire header and the copyright */
25 /* notice printed when the `-h' switch is selected) are not removed, and */
26 /* no compensation is received. Private, research, and institutional */
27 /* use is free. You may distribute modified versions of this code UNDER */
28 /* THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE */
29 /* SAME FILE REMAIN UNDER COPYRIGHT OF THE ORIGINAL AUTHOR, BOTH SOURCE */
30 /* AND OBJECT CODE ARE MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR */
31 /* NOTICE IS GIVEN OF THE MODIFICATIONS. Distribution of this code as */
32 /* part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT */
33 /* WITH THE AUTHOR. (If you are not directly supplying this code to a */
34 /* customer, and you are instead telling them how they can obtain it for */
35 /* free, then you are not required to make any arrangement with me.) */
36 /* */
37 /* Hypertext instructions for Triangle are available on the Web at */
38 /* */
39 /* http://www.cs.cmu.edu/~quake/triangle.html */
40 /* */
41 /* Disclaimer: Neither I nor Carnegie Mellon warrant this code in any way */
42 /* whatsoever. This code is provided "as-is". Use at your own risk. */
43 /* */
44 /* Some of the references listed below are marked with an asterisk. [*] */
45 /* These references are available for downloading from the Web page */
46 /* */
47 /* http://www.cs.cmu.edu/~quake/triangle.research.html */
48 /* */
49 /* Three papers discussing aspects of Triangle are available. A short */
50 /* overview appears in "Triangle: Engineering a 2D Quality Mesh */
51 /* Generator and Delaunay Triangulator," in Applied Computational */
52 /* Geometry: Towards Geometric Engineering, Ming C. Lin and Dinesh */
53 /* Manocha, editors, Lecture Notes in Computer Science volume 1148, */
54 /* pages 203-222, Springer-Verlag, Berlin, May 1996 (from the First ACM */
55 /* Workshop on Applied Computational Geometry). [*] */
56 /* */
57 /* The algorithms are discussed in the greatest detail in "Delaunay */
58 /* Refinement Algorithms for Triangular Mesh Generation," Computational */
59 /* Geometry: Theory and Applications 22(1-3):21-74, May 2002. [*] */
60 /* */
61 /* More detail about the data structures may be found in my dissertation: */
62 /* "Delaunay Refinement Mesh Generation," Ph.D. thesis, Technical Report */
63 /* CMU-CS-97-137, School of Computer Science, Carnegie Mellon University, */
64 /* Pittsburgh, Pennsylvania, 18 May 1997. [*] */
65 /* */
66 /* Triangle was created as part of the Quake Project in the School of */
67 /* Computer Science at Carnegie Mellon University. For further */
68 /* information, see Hesheng Bao, Jacobo Bielak, Omar Ghattas, Loukas F. */
69 /* Kallivokas, David R. O'Hallaron, Jonathan R. Shewchuk, and Jifeng Xu, */
70 /* "Large-scale Simulation of Elastic Wave Propagation in Heterogeneous */
71 /* Media on Parallel Computers," Computer Methods in Applied Mechanics */
72 /* and Engineering 152(1-2):85-102, 22 January 1998. */
73 /* */
74 /* Triangle's Delaunay refinement algorithm for quality mesh generation is */
75 /* a hybrid of one due to Jim Ruppert, "A Delaunay Refinement Algorithm */
76 /* for Quality 2-Dimensional Mesh Generation," Journal of Algorithms */
77 /* 18(3):548-585, May 1995 [*], and one due to L. Paul Chew, "Guaranteed- */
78 /* Quality Mesh Generation for Curved Surfaces," Proceedings of the Ninth */
79 /* Annual Symposium on Computational Geometry (San Diego, California), */
80 /* pages 274-280, Association for Computing Machinery, May 1993, */
81 /* http://portal.acm.org/citation.cfm?id=161150 . */
82 /* */
83 /* The Delaunay refinement algorithm has been modified so that it meshes */
84 /* domains with small input angles well, as described in Gary L. Miller, */
85 /* Steven E. Pav, and Noel J. Walkington, "When and Why Ruppert's */
86 /* Algorithm Works," Twelfth International Meshing Roundtable, pages */
87 /* 91-102, Sandia National Laboratories, September 2003. [*] */
88 /* */
89 /* My implementation of the divide-and-conquer and incremental Delaunay */
90 /* triangulation algorithms follows closely the presentation of Guibas */
91 /* and Stolfi, even though I use a triangle-based data structure instead */
92 /* of their quad-edge data structure. (In fact, I originally implemented */
93 /* Triangle using the quad-edge data structure, but the switch to a */
94 /* triangle-based data structure sped Triangle by a factor of two.) The */
95 /* mesh manipulation primitives and the two aforementioned Delaunay */
96 /* triangulation algorithms are described by Leonidas J. Guibas and Jorge */
97 /* Stolfi, "Primitives for the Manipulation of General Subdivisions and */
98 /* the Computation of Voronoi Diagrams," ACM Transactions on Graphics */
99 /* 4(2):74-123, April 1985, http://portal.acm.org/citation.cfm?id=282923 .*/
100 /* */
101 /* Their O(n log n) divide-and-conquer algorithm is adapted from Der-Tsai */
102 /* Lee and Bruce J. Schachter, "Two Algorithms for Constructing the */
103 /* Delaunay Triangulation," International Journal of Computer and */
104 /* Information Science 9(3):219-242, 1980. Triangle's improvement of the */
105 /* divide-and-conquer algorithm by alternating between vertical and */
106 /* horizontal cuts was introduced by Rex A. Dwyer, "A Faster Divide-and- */
107 /* Conquer Algorithm for Constructing Delaunay Triangulations," */
108 /* Algorithmica 2(2):137-151, 1987. */
109 /* */
110 /* The incremental insertion algorithm was first proposed by C. L. Lawson, */
111 /* "Software for C1 Surface Interpolation," in Mathematical Software III, */
112 /* John R. Rice, editor, Academic Press, New York, pp. 161-194, 1977. */
113 /* For point location, I use the algorithm of Ernst P. Mucke, Isaac */
114 /* Saias, and Binhai Zhu, "Fast Randomized Point Location Without */
115 /* Preprocessing in Two- and Three-Dimensional Delaunay Triangulations," */
116 /* Proceedings of the Twelfth Annual Symposium on Computational Geometry, */
117 /* ACM, May 1996. [*] If I were to randomize the order of vertex */
118 /* insertion (I currently don't bother), their result combined with the */
119 /* result of Kenneth L. Clarkson and Peter W. Shor, "Applications of */
120 /* Random Sampling in Computational Geometry II," Discrete & */
121 /* Computational Geometry 4(1):387-421, 1989, would yield an expected */
122 /* O(n^{4/3}) bound on running time. */
123 /* */
124 /* The O(n log n) sweepline Delaunay triangulation algorithm is taken from */
125 /* Steven Fortune, "A Sweepline Algorithm for Voronoi Diagrams", */
126 /* Algorithmica 2(2):153-174, 1987. A random sample of edges on the */
127 /* boundary of the triangulation are maintained in a splay tree for the */
128 /* purpose of point location. Splay trees are described by Daniel */
129 /* Dominic Sleator and Robert Endre Tarjan, "Self-Adjusting Binary Search */
130 /* Trees," Journal of the ACM 32(3):652-686, July 1985, */
131 /* http://portal.acm.org/citation.cfm?id=3835 . */
132 /* */
133 /* The algorithms for exact computation of the signs of determinants are */
134 /* described in Jonathan Richard Shewchuk, "Adaptive Precision Floating- */
135 /* Point Arithmetic and Fast Robust Geometric Predicates," Discrete & */
136 /* Computational Geometry 18(3):305-363, October 1997. (Also available */
137 /* as Technical Report CMU-CS-96-140, School of Computer Science, */
138 /* Carnegie Mellon University, Pittsburgh, Pennsylvania, May 1996.) [*] */
139 /* An abbreviated version appears as Jonathan Richard Shewchuk, "Robust */
140 /* Adaptive Floating-Point Geometric Predicates," Proceedings of the */
141 /* Twelfth Annual Symposium on Computational Geometry, ACM, May 1996. [*] */
142 /* Many of the ideas for my exact arithmetic routines originate with */
143 /* Douglas M. Priest, "Algorithms for Arbitrary Precision Floating Point */
144 /* Arithmetic," Tenth Symposium on Computer Arithmetic, pp. 132-143, IEEE */
145 /* Computer Society Press, 1991. [*] Many of the ideas for the correct */
146 /* evaluation of the signs of determinants are taken from Steven Fortune */
147 /* and Christopher J. Van Wyk, "Efficient Exact Arithmetic for Computa- */
148 /* tional Geometry," Proceedings of the Ninth Annual Symposium on */
149 /* Computational Geometry, ACM, pp. 163-172, May 1993, and from Steven */
150 /* Fortune, "Numerical Stability of Algorithms for 2D Delaunay Triangu- */
151 /* lations," International Journal of Computational Geometry & Applica- */
152 /* tions 5(1-2):193-213, March-June 1995. */
153 /* */
154 /* The method of inserting new vertices off-center (not precisely at the */
155 /* circumcenter of every poor-quality triangle) is from Alper Ungor, */
156 /* "Off-centers: A New Type of Steiner Points for Computing Size-Optimal */
157 /* Quality-Guaranteed Delaunay Triangulations," Proceedings of LATIN */
158 /* 2004 (Buenos Aires, Argentina), April 2004. */
159 /* */
160 /* For definitions of and results involving Delaunay triangulations, */
161 /* constrained and conforming versions thereof, and other aspects of */
162 /* triangular mesh generation, see the excellent survey by Marshall Bern */
163 /* and David Eppstein, "Mesh Generation and Optimal Triangulation," in */
164 /* Computing and Euclidean Geometry, Ding-Zhu Du and Frank Hwang, */
165 /* editors, World Scientific, Singapore, pp. 23-90, 1992. [*] */
166 /* */
167 /* The time for incrementally adding PSLG (planar straight line graph) */
168 /* segments to create a constrained Delaunay triangulation is probably */
169 /* O(t^2) per segment in the worst case and O(t) per segment in the */
170 /* common case, where t is the number of triangles that intersect the */
171 /* segment before it is inserted. This doesn't count point location, */
172 /* which can be much more expensive. I could improve this to O(d log d) */
173 /* time, but d is usually quite small, so it's not worth the bother. */
174 /* (This note does not apply when the -s switch is used, invoking a */
175 /* different method is used to insert segments.) */
176 /* */
177 /* The time for deleting a vertex from a Delaunay triangulation is O(d^2) */
178 /* in the worst case and O(d) in the common case, where d is the degree */
179 /* of the vertex being deleted. I could improve this to O(d log d) time, */
180 /* but d is usually quite small, so it's not worth the bother. */
181 /* */
182 /* Ruppert's Delaunay refinement algorithm typically generates triangles */
183 /* at a linear rate (constant time per triangle) after the initial */
184 /* triangulation is formed. There may be pathological cases where */
185 /* quadratic time is required, but these never arise in practice. */
186 /* */
187 /* The geometric predicates (circumcenter calculations, segment */
188 /* intersection formulae, etc.) appear in my "Lecture Notes on Geometric */
189 /* Robustness" at http://www.cs.berkeley.edu/~jrs/mesh . */
190 /* */
191 /* If you make any improvements to this code, please please please let me */
192 /* know, so that I may obtain the improvements. Even if you don't change */
193 /* the code, I'd still love to hear what it's being used for. */
194 /* */
195 /*****************************************************************************/
196 
197 /* If yours is not a Unix system, define the NO_TIMER compiler switch to */
198 /* remove the Unix-specific timing code. */
199 
200 #define NO_TIMER
201 
202 /* To insert lots of self-checks for internal errors, define the SELF_CHECK */
203 /* symbol. This will slow down the program significantly. It is best to */
204 /* define the symbol using the -DSELF_CHECK compiler switch, but you could */
205 /* write "#define SELF_CHECK" below. If you are modifying this code, I */
206 /* recommend you turn self-checks on until your work is debugged. */
207 
208 /* #define SELF_CHECK */
209 
210 /* To compile Triangle as a callable object library (triangle.o), define the */
211 /* TRILIBRARY symbol. Read the file triangle.h for details on how to call */
212 /* the procedure triangulate() that results. */
213 
214 #define TRILIBRARY
215 
216 /* It is possible to generate a smaller version of Triangle using one or */
217 /* both of the following symbols. Define the REDUCED symbol to eliminate */
218 /* all features that are primarily of research interest; specifically, the */
219 /* -i, -F, -s, and -C switches. Define the CDT_ONLY symbol to eliminate */
220 /* all meshing algorithms above and beyond constrained Delaunay */
221 /* triangulation; specifically, the -r, -q, -a, -u, -D, -S, and -s */
222 /* switches. These reductions are most likely to be useful when */
223 /* generating an object library (triangle.o) by defining the TRILIBRARY */
224 /* symbol. */
225 
226 #define REDUCED
227 #define CDT_ONLY
228 
229 /* On some machines, my exact arithmetic routines might be defeated by the */
230 /* use of internal extended precision floating-point registers. The best */
231 /* way to solve this problem is to set the floating-point registers to use */
232 /* single or double precision internally. On 80x86 processors, this may */
233 /* be accomplished by setting the CPU86 symbol for the Microsoft C */
234 /* compiler, or the LINUX symbol for the gcc compiler running on Linux. */
235 /* */
236 /* An inferior solution is to declare certain values as `volatile', thus */
237 /* forcing them to be stored to memory and rounded off. Unfortunately, */
238 /* this solution might slow Triangle down quite a bit. To use volatile */
239 /* values, write "#define INEXACT volatile" below. Normally, however, */
240 /* INEXACT should be defined to be nothing. ("#define INEXACT".) */
241 /* */
242 /* For more discussion, see http://www.cs.cmu.edu/~quake/robust.pc.html . */
243 /* For yet more discussion, see Section 5 of my paper, "Adaptive Precision */
244 /* Floating-Point Arithmetic and Fast Robust Geometric Predicates" (also */
245 /* available as Section 6.6 of my dissertation). */
246 
247 /* #define CPU86 */
248 /* #define LINUX */
249 
250 #define INEXACT /* Nothing */
251 /* #define INEXACT volatile */
252 
253 /* Maximum number of characters in a file name (including the null). */
254 
255 #define FILENAMESIZE 2048
256 
257 /* Maximum number of characters in a line read from a file (including the */
258 /* null). */
259 
260 #define INPUTLINESIZE 1024
261 
262 /* For efficiency, a variety of data structures are allocated in bulk. The */
263 /* following constants determine how many of each structure is allocated */
264 /* at once. */
265 
266 #define TRIPERBLOCK 4092 /* Number of triangles allocated at once. */
267 #define SUBSEGPERBLOCK 508 /* Number of subsegments allocated at once. */
268 #define VERTEXPERBLOCK 4092 /* Number of vertices allocated at once. */
269 #define VIRUSPERBLOCK 1020 /* Number of virus triangles allocated at once. */
270 /* Number of encroached subsegments allocated at once. */
271 #define BADSUBSEGPERBLOCK 252
272 /* Number of skinny triangles allocated at once. */
273 #define BADTRIPERBLOCK 4092
274 /* Number of flipped triangles allocated at once. */
275 #define FLIPSTACKERPERBLOCK 252
276 /* Number of splay tree nodes allocated at once. */
277 #define SPLAYNODEPERBLOCK 508
278 
279 /* The vertex types. A DEADVERTEX has been deleted entirely. An */
280 /* UNDEADVERTEX is not part of the mesh, but is written to the output */
281 /* .node file and affects the node indexing in the other output files. */
282 
283 #define INPUTVERTEX 0
284 #define SEGMENTVERTEX 1
285 #define FREEVERTEX 2
286 #define DEADVERTEX -32768
287 #define UNDEADVERTEX -32767
288 
289 /* Two constants for algorithms based on random sampling. Both constants */
290 /* have been chosen empirically to optimize their respective algorithms. */
291 
292 /* Used for the point location scheme of Mucke, Saias, and Zhu, to decide */
293 /* how large a random sample of triangles to inspect. */
294 
295 #define SAMPLEFACTOR 11
296 
297 /* Used in Fortune's sweepline Delaunay algorithm to determine what fraction */
298 /* of boundary edges should be maintained in the splay tree for point */
299 /* location on the front. */
300 
301 #define SAMPLERATE 10
302 
303 /* A number that speaks for itself, every kissable digit. */
304 
305 #define PI 3.141592653589793238462643383279502884197169399375105820974944592308
306 
307 /* Another fave. */
308 
309 #define SQUAREROOTTWO 1.4142135623730950488016887242096980785696718753769480732
310 
311 /* And here's one for those of you who are intimidated by math. */
312 
313 #define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
314 
315 #include <stdio.h>
316 #include <stdlib.h>
317 #include <string.h>
318 #include <math.h>
319 #include <stdint.h>
320 #ifndef NO_TIMER
321 #include <sys/time.h>
322 #endif /* not NO_TIMER */
323 #ifdef CPU86
324 #include <float.h>
325 #endif /* CPU86 */
326 #ifdef LINUX
327 #include <fpu_control.h>
328 #endif /* LINUX */
329 #ifdef TRILIBRARY
330 #include "triangle.h"
331 #endif /* TRILIBRARY */
332 
333 /* A few forward declarations. */
334 
335 #ifndef TRILIBRARY
336 char *readline();
337 char *findfield();
338 #endif /* not TRILIBRARY */
339 
340 /* Labels that signify the result of point location. The result of a */
341 /* search indicates that the point falls in the interior of a triangle, on */
342 /* an edge, on a vertex, or outside the mesh. */
343 
345 
346 /* Labels that signify the result of vertex insertion. The result indicates */
347 /* that the vertex was inserted with complete success, was inserted but */
348 /* encroaches upon a subsegment, was not inserted because it lies on a */
349 /* segment, or was not inserted because another vertex occupies the same */
350 /* location. */
351 
354 
355 /* Labels that signify the result of direction finding. The result */
356 /* indicates that a segment connecting the two query points falls within */
357 /* the direction triangle, along the left edge of the direction triangle, */
358 /* or along the right edge of the direction triangle. */
359 
361 
362 /*****************************************************************************/
363 /* */
364 /* The basic mesh data structures */
365 /* */
366 /* There are three: vertices, triangles, and subsegments (abbreviated */
367 /* `subseg'). These three data structures, linked by pointers, comprise */
368 /* the mesh. A vertex simply represents a mesh vertex and its properties. */
369 /* A triangle is a triangle. A subsegment is a special data structure used */
370 /* to represent an impenetrable edge of the mesh (perhaps on the outer */
371 /* boundary, on the boundary of a hole, or part of an internal boundary */
372 /* separating two triangulated regions). Subsegments represent boundaries, */
373 /* defined by the user, that triangles may not lie across. */
374 /* */
375 /* A triangle consists of a list of three vertices, a list of three */
376 /* adjoining triangles, a list of three adjoining subsegments (when */
377 /* segments exist), an arbitrary number of optional user-defined */
378 /* floating-point attributes, and an optional area constraint. The latter */
379 /* is an upper bound on the permissible area of each triangle in a region, */
380 /* used for mesh refinement. */
381 /* */
382 /* For a triangle on a boundary of the mesh, some or all of the neighboring */
383 /* triangles may not be present. For a triangle in the interior of the */
384 /* mesh, often no neighboring subsegments are present. Such absent */
385 /* triangles and subsegments are never represented by NULL pointers; they */
386 /* are represented by two special records: `dummytri', the triangle that */
387 /* fills "outer space", and `dummysub', the omnipresent subsegment. */
388 /* `dummytri' and `dummysub' are used for several reasons; for instance, */
389 /* they can be dereferenced and their contents examined without violating */
390 /* protected memory. */
391 /* */
392 /* However, it is important to understand that a triangle includes other */
393 /* information as well. The pointers to adjoining vertices, triangles, and */
394 /* subsegments are ordered in a way that indicates their geometric relation */
395 /* to each other. Furthermore, each of these pointers contains orientation */
396 /* information. Each pointer to an adjoining triangle indicates which face */
397 /* of that triangle is contacted. Similarly, each pointer to an adjoining */
398 /* subsegment indicates which side of that subsegment is contacted, and how */
399 /* the subsegment is oriented relative to the triangle. */
400 /* */
401 /* The data structure representing a subsegment may be thought to be */
402 /* abutting the edge of one or two triangle data structures: either */
403 /* sandwiched between two triangles, or resting against one triangle on an */
404 /* exterior boundary or hole boundary. */
405 /* */
406 /* A subsegment consists of a list of four vertices--the vertices of the */
407 /* subsegment, and the vertices of the segment it is a part of--a list of */
408 /* two adjoining subsegments, and a list of two adjoining triangles. One */
409 /* of the two adjoining triangles may not be present (though there should */
410 /* always be one), and neighboring subsegments might not be present. */
411 /* Subsegments also store a user-defined integer "boundary marker". */
412 /* Typically, this integer is used to indicate what boundary conditions are */
413 /* to be applied at that location in a finite element simulation. */
414 /* */
415 /* Like triangles, subsegments maintain information about the relative */
416 /* orientation of neighboring objects. */
417 /* */
418 /* Vertices are relatively simple. A vertex is a list of floating-point */
419 /* numbers, starting with the x, and y coordinates, followed by an */
420 /* arbitrary number of optional user-defined floating-point attributes, */
421 /* followed by an integer boundary marker. During the segment insertion */
422 /* phase, there is also a pointer from each vertex to a triangle that may */
423 /* contain it. Each pointer is not always correct, but when one is, it */
424 /* speeds up segment insertion. These pointers are assigned values once */
425 /* at the beginning of the segment insertion phase, and are not used or */
426 /* updated except during this phase. Edge flipping during segment */
427 /* insertion will render some of them incorrect. Hence, don't rely upon */
428 /* them for anything. */
429 /* */
430 /* Other than the exception mentioned above, vertices have no information */
431 /* about what triangles, subfacets, or subsegments they are linked to. */
432 /* */
433 /*****************************************************************************/
434 
435 /*****************************************************************************/
436 /* */
437 /* Handles */
438 /* */
439 /* The oriented triangle (`otri') and oriented subsegment (`osub') data */
440 /* structures defined below do not themselves store any part of the mesh. */
441 /* The mesh itself is made of `triangle's, `subseg's, and `vertex's. */
442 /* */
443 /* Oriented triangles and oriented subsegments will usually be referred to */
444 /* as "handles." A handle is essentially a pointer into the mesh; it */
445 /* allows you to "hold" one particular part of the mesh. Handles are used */
446 /* to specify the regions in which one is traversing and modifying the mesh.*/
447 /* A single `triangle' may be held by many handles, or none at all. (The */
448 /* latter case is not a memory leak, because the triangle is still */
449 /* connected to other triangles in the mesh.) */
450 /* */
451 /* An `otri' is a handle that holds a triangle. It holds a specific edge */
452 /* of the triangle. An `osub' is a handle that holds a subsegment. It */
453 /* holds either the left or right side of the subsegment. */
454 /* */
455 /* Navigation about the mesh is accomplished through a set of mesh */
456 /* manipulation primitives, further below. Many of these primitives take */
457 /* a handle and produce a new handle that holds the mesh near the first */
458 /* handle. Other primitives take two handles and glue the corresponding */
459 /* parts of the mesh together. The orientation of the handles is */
460 /* important. For instance, when two triangles are glued together by the */
461 /* bond() primitive, they are glued at the edges on which the handles lie. */
462 /* */
463 /* Because vertices have no information about which triangles they are */
464 /* attached to, I commonly represent a vertex by use of a handle whose */
465 /* origin is the vertex. A single handle can simultaneously represent a */
466 /* triangle, an edge, and a vertex. */
467 /* */
468 /*****************************************************************************/
469 
470 /* The triangle data structure. Each triangle contains three pointers to */
471 /* adjoining triangles, plus three pointers to vertices, plus three */
472 /* pointers to subsegments (declared below; these pointers are usually */
473 /* `dummysub'). It may or may not also contain user-defined attributes */
474 /* and/or a floating-point "area constraint." It may also contain extra */
475 /* pointers for nodes, when the user asks for high-order elements. */
476 /* Because the size and structure of a `triangle' is not decided until */
477 /* runtime, I haven't simply declared the type `triangle' as a struct. */
478 
479 typedef REAL **triangle; /* Really: typedef triangle *triangle */
480 
481 /* An oriented triangle: includes a pointer to a triangle and orientation. */
482 /* The orientation denotes an edge of the triangle. Hence, there are */
483 /* three possible orientations. By convention, each edge always points */
484 /* counterclockwise about the corresponding triangle. */
485 
486 struct otri {
488  int orient; /* Ranges from 0 to 2. */
489 };
490 
491 /* The subsegment data structure. Each subsegment contains two pointers to */
492 /* adjoining subsegments, plus four pointers to vertices, plus two */
493 /* pointers to adjoining triangles, plus one boundary marker, plus one */
494 /* segment number. */
495 
496 typedef REAL **subseg; /* Really: typedef subseg *subseg */
497 
498 /* An oriented subsegment: includes a pointer to a subsegment and an */
499 /* orientation. The orientation denotes a side of the edge. Hence, there */
500 /* are two possible orientations. By convention, the edge is always */
501 /* directed so that the "side" denoted is the right side of the edge. */
502 
503 struct osub {
505  int ssorient; /* Ranges from 0 to 1. */
506 };
507 
508 /* The vertex data structure. Each vertex is actually an array of REALs. */
509 /* The number of REALs is unknown until runtime. An integer boundary */
510 /* marker, and sometimes a pointer to a triangle, is appended after the */
511 /* REALs. */
512 
513 typedef REAL *vertex;
514 
515 /* A queue used to store encroached subsegments. Each subsegment's vertices */
516 /* are stored so that we can check whether a subsegment is still the same. */
517 
518 struct badsubseg {
519  subseg encsubseg; /* An encroached subsegment. */
520  vertex subsegorg, subsegdest; /* Its two vertices. */
521 };
522 
523 /* A queue used to store bad triangles. The key is the square of the cosine */
524 /* of the smallest angle of the triangle. Each triangle's vertices are */
525 /* stored so that one can check whether a triangle is still the same. */
526 
527 struct badtriang {
528  triangle poortri; /* A skinny or too-large triangle. */
529  REAL key; /* cos^2 of smallest (apical) angle. */
530  vertex triangorg, triangdest, triangapex; /* Its three vertices. */
531  struct badtriang *nexttriang; /* Pointer to next bad triangle. */
532 };
533 
534 /* A stack of triangles flipped during the most recent vertex insertion. */
535 /* The stack is used to undo the vertex insertion if the vertex encroaches */
536 /* upon a subsegment. */
537 
538 struct flipstacker {
539  triangle flippedtri; /* A recently flipped triangle. */
540  struct flipstacker *prevflip; /* Previous flip in the stack. */
541 };
542 
543 /* A node in a heap used to store events for the sweepline Delaunay */
544 /* algorithm. Nodes do not point directly to their parents or children in */
545 /* the heap. Instead, each node knows its position in the heap, and can */
546 /* look up its parent and children in a separate array. The `eventptr' */
547 /* points either to a `vertex' or to a triangle (in encoded format, so */
548 /* that an orientation is included). In the latter case, the origin of */
549 /* the oriented triangle is the apex of a "circle event" of the sweepline */
550 /* algorithm. To distinguish site events from circle events, all circle */
551 /* events are given an invalid (smaller than `xmin') x-coordinate `xkey'. */
552 
553 struct event {
554  REAL xkey, ykey; /* Coordinates of the event. */
555  VOID *eventptr; /* Can be a vertex or the location of a circle event. */
556  int heapposition; /* Marks this event's position in the heap. */
557 };
558 
559 /* A node in the splay tree. Each node holds an oriented ghost triangle */
560 /* that represents a boundary edge of the growing triangulation. When a */
561 /* circle event covers two boundary edges with a triangle, so that they */
562 /* are no longer boundary edges, those edges are not immediately deleted */
563 /* from the tree; rather, they are lazily deleted when they are next */
564 /* encountered. (Since only a random sample of boundary edges are kept */
565 /* in the tree, lazy deletion is faster.) `keydest' is used to verify */
566 /* that a triangle is still the same as when it entered the splay tree; if */
567 /* it has been rotated (due to a circle event), it no longer represents a */
568 /* boundary edge and should be deleted. */
569 
570 struct splaynode {
571  struct otri keyedge; /* Lprev of an edge on the front. */
572  vertex keydest; /* Used to verify that splay node is still live. */
573  struct splaynode *lchild, *rchild; /* Children in splay tree. */
574 };
575 
576 /* A type used to allocate memory. firstblock is the first block of items. */
577 /* nowblock is the block from which items are currently being allocated. */
578 /* nextitem points to the next slab of free memory for an item. */
579 /* deaditemstack is the head of a linked list (stack) of deallocated items */
580 /* that can be recycled. unallocateditems is the number of items that */
581 /* remain to be allocated from nowblock. */
582 /* */
583 /* Traversal is the process of walking through the entire list of items, and */
584 /* is separate from allocation. Note that a traversal will visit items on */
585 /* the "deaditemstack" stack as well as live items. pathblock points to */
586 /* the block currently being traversed. pathitem points to the next item */
587 /* to be traversed. pathitemsleft is the number of items that remain to */
588 /* be traversed in pathblock. */
589 /* */
590 /* alignbytes determines how new records should be aligned in memory. */
591 /* itembytes is the length of a record in bytes (after rounding up). */
592 /* itemsperblock is the number of items allocated at once in a single */
593 /* block. itemsfirstblock is the number of items in the first block, */
594 /* which can vary from the others. items is the number of currently */
595 /* allocated items. maxitems is the maximum number of items that have */
596 /* been allocated at once; it is the current number of items plus the */
597 /* number of records kept on deaditemstack. */
598 
599 struct memorypool {
612 };
613 
614 
615 /* Global constants. */
616 
617 REAL splitter; /* Used to split REAL factors for exact multiplication. */
618 REAL epsilon; /* Floating-point machine epsilon. */
623 
624 /* Random number seed is not constant, but I've made it global anyway. */
625 
626 uintptr_t randomseed; /* Current random number seed. */
627 
628 
629 /* Mesh data structure. Triangle operates on only one mesh, but the mesh */
630 /* structure is used (instead of global variables) to allow reentrancy. */
631 
632 struct mesh {
633 
634 /* Variables used to allocate memory for triangles, subsegments, vertices, */
635 /* viri (triangles being eaten), encroached segments, bad (skinny or too */
636 /* large) triangles, and splay tree nodes. */
637 
638  struct memorypool triangles;
639  struct memorypool subsegs;
640  struct memorypool vertices;
641  struct memorypool viri;
642  struct memorypool badsubsegs;
643  struct memorypool badtriangles;
644  struct memorypool flipstackers;
645  struct memorypool splaynodes;
646 
647 /* Variables that maintain the bad triangle queues. The queues are */
648 /* ordered from 4095 (highest priority) to 0 (lowest priority). */
649 
650  struct badtriang *queuefront[4096];
651  struct badtriang *queuetail[4096];
652  int nextnonemptyq[4096];
654 
655 /* Variable that maintains the stack of recently flipped triangles. */
656 
658 
659 /* Other variables. */
660 
661  REAL xmin, xmax, ymin, ymax; /* x and y bounds. */
662  REAL xminextreme; /* Nonexistent x value used as a flag in sweepline. */
663  int invertices; /* Number of input vertices. */
664  int inelements; /* Number of input triangles. */
665  int insegments; /* Number of input segments. */
666  int holes; /* Number of input holes. */
667  int regions; /* Number of input regions. */
668  int undeads; /* Number of input vertices that don't appear in the mesh. */
669  long edges; /* Number of output edges. */
670  int mesh_dim; /* Dimension (ought to be 2). */
671  int nextras; /* Number of attributes per vertex. */
672  int eextras; /* Number of attributes per triangle. */
673  long hullsize; /* Number of edges in convex hull. */
674  int steinerleft; /* Number of Steiner points not yet used. */
675  int vertexmarkindex; /* Index to find boundary marker of a vertex. */
676  int vertex2triindex; /* Index to find a triangle adjacent to a vertex. */
677  int highorderindex; /* Index to find extra nodes for high-order elements. */
678  int elemattribindex; /* Index to find attributes of a triangle. */
679  int areaboundindex; /* Index to find area bound of a triangle. */
680  int checksegments; /* Are there segments in the triangulation yet? */
681  int checkquality; /* Has quality triangulation begun yet? */
682  int readnodefile; /* Has a .node file been read? */
683  long samples; /* Number of random samples for point location. */
684 
685  long incirclecount; /* Number of incircle tests performed. */
686  long counterclockcount; /* Number of counterclockwise tests performed. */
687  long orient3dcount; /* Number of 3D orientation tests performed. */
688  long hyperbolacount; /* Number of right-of-hyperbola tests performed. */
689  long circumcentercount; /* Number of circumcenter calculations performed. */
690  long circletopcount; /* Number of circle top calculations performed. */
691 
692 /* Triangular bounding box vertices. */
693 
695 
696 /* Pointer to the `triangle' that occupies all of "outer space." */
697 
699  triangle *dummytribase; /* Keep base address so we can free() it later. */
700 
701 /* Pointer to the omnipresent subsegment. Referenced by any triangle or */
702 /* subsegment that isn't really connected to a subsegment at that */
703 /* location. */
704 
706  subseg *dummysubbase; /* Keep base address so we can free() it later. */
707 
708 /* Pointer to a recently visited triangle. Improves point location if */
709 /* proximate vertices are inserted sequentially. */
710 
711  struct otri recenttri;
712 
713 }; /* End of `struct mesh'. */
714 
715 
716 /* Data structure for command line switches and file names. This structure */
717 /* is used (instead of global variables) to allow reentrancy. */
718 
719 struct behavior {
720 
721 /* Switches for the triangulator. */
722 /* poly: -p switch. refine: -r switch. */
723 /* quality: -q switch. */
724 /* minangle: minimum angle bound, specified after -q switch. */
725 /* goodangle: cosine squared of minangle. */
726 /* offconstant: constant used to place off-center Steiner points. */
727 /* vararea: -a switch without number. */
728 /* fixedarea: -a switch with number. */
729 /* maxarea: maximum area bound, specified after -a switch. */
730 /* usertest: -u switch. */
731 /* regionattrib: -A switch. convex: -c switch. */
732 /* weighted: 1 for -w switch, 2 for -W switch. jettison: -j switch */
733 /* firstnumber: inverse of -z switch. All items are numbered starting */
734 /* from `firstnumber'. */
735 /* edgesout: -e switch. voronoi: -v switch. */
736 /* neighbors: -n switch. geomview: -g switch. */
737 /* nobound: -B switch. nopolywritten: -P switch. */
738 /* nonodewritten: -N switch. noelewritten: -E switch. */
739 /* noiterationnum: -I switch. noholes: -O switch. */
740 /* noexact: -X switch. */
741 /* order: element order, specified after -o switch. */
742 /* nobisect: count of how often -Y switch is selected. */
743 /* steiner: maximum number of Steiner points, specified after -S switch. */
744 /* incremental: -i switch. sweepline: -F switch. */
745 /* dwyer: inverse of -l switch. */
746 /* splitseg: -s switch. */
747 /* conformdel: -D switch. docheck: -C switch. */
748 /* quiet: -Q switch. verbose: count of how often -V switch is selected. */
749 /* usesegments: -p, -r, -q, or -c switch; determines whether segments are */
750 /* used at all. */
751 /* */
752 /* Read the instructions to find out the meaning of these switches. */
753 
761  int splitseg;
762  int docheck;
765  int order;
766  int nobisect;
767  int steiner;
770 
771 /* Variables for file names. */
772 
773 #ifndef TRILIBRARY
774  char innodefilename[FILENAMESIZE];
775  char inelefilename[FILENAMESIZE];
776  char inpolyfilename[FILENAMESIZE];
777  char areafilename[FILENAMESIZE];
778  char outnodefilename[FILENAMESIZE];
779  char outelefilename[FILENAMESIZE];
780  char outpolyfilename[FILENAMESIZE];
781  char edgefilename[FILENAMESIZE];
782  char vnodefilename[FILENAMESIZE];
783  char vedgefilename[FILENAMESIZE];
784  char neighborfilename[FILENAMESIZE];
785  char offfilename[FILENAMESIZE];
786 #endif /* not TRILIBRARY */
787 
788 }; /* End of `struct behavior'. */
789 
790 
791 /*****************************************************************************/
792 /* */
793 /* Mesh manipulation primitives. Each triangle contains three pointers to */
794 /* other triangles, with orientations. Each pointer points not to the */
795 /* first byte of a triangle, but to one of the first three bytes of a */
796 /* triangle. It is necessary to extract both the triangle itself and the */
797 /* orientation. To save memory, I keep both pieces of information in one */
798 /* pointer. To make this possible, I assume that all triangles are aligned */
799 /* to four-byte boundaries. The decode() routine below decodes a pointer, */
800 /* extracting an orientation (in the range 0 to 2) and a pointer to the */
801 /* beginning of a triangle. The encode() routine compresses a pointer to a */
802 /* triangle and an orientation into a single pointer. My assumptions that */
803 /* triangles are four-byte-aligned and that the `uintptr_t' type is */
804 /* long enough to hold a pointer are two of the few kludges in this program.*/
805 /* */
806 /* Subsegments are manipulated similarly. A pointer to a subsegment */
807 /* carries both an address and an orientation in the range 0 to 1. */
808 /* */
809 /* The other primitives take an oriented triangle or oriented subsegment, */
810 /* and return an oriented triangle or oriented subsegment or vertex; or */
811 /* they change the connections in the data structure. */
812 /* */
813 /* Below, triangles and subsegments are denoted by their vertices. The */
814 /* triangle abc has origin (org) a, destination (dest) b, and apex (apex) */
815 /* c. These vertices occur in counterclockwise order about the triangle. */
816 /* The handle abc may simultaneously denote vertex a, edge ab, and triangle */
817 /* abc. */
818 /* */
819 /* Similarly, the subsegment ab has origin (sorg) a and destination (sdest) */
820 /* b. If ab is thought to be directed upward (with b directly above a), */
821 /* then the handle ab is thought to grasp the right side of ab, and may */
822 /* simultaneously denote vertex a and edge ab. */
823 /* */
824 /* An asterisk (*) denotes a vertex whose identity is unknown. */
825 /* */
826 /* Given this notation, a partial list of mesh manipulation primitives */
827 /* follows. */
828 /* */
829 /* */
830 /* For triangles: */
831 /* */
832 /* sym: Find the abutting triangle; same edge. */
833 /* sym(abc) -> ba* */
834 /* */
835 /* lnext: Find the next edge (counterclockwise) of a triangle. */
836 /* lnext(abc) -> bca */
837 /* */
838 /* lprev: Find the previous edge (clockwise) of a triangle. */
839 /* lprev(abc) -> cab */
840 /* */
841 /* onext: Find the next edge counterclockwise with the same origin. */
842 /* onext(abc) -> ac* */
843 /* */
844 /* oprev: Find the next edge clockwise with the same origin. */
845 /* oprev(abc) -> a*b */
846 /* */
847 /* dnext: Find the next edge counterclockwise with the same destination. */
848 /* dnext(abc) -> *ba */
849 /* */
850 /* dprev: Find the next edge clockwise with the same destination. */
851 /* dprev(abc) -> cb* */
852 /* */
853 /* rnext: Find the next edge (counterclockwise) of the adjacent triangle. */
854 /* rnext(abc) -> *a* */
855 /* */
856 /* rprev: Find the previous edge (clockwise) of the adjacent triangle. */
857 /* rprev(abc) -> b** */
858 /* */
859 /* org: Origin dest: Destination apex: Apex */
860 /* org(abc) -> a dest(abc) -> b apex(abc) -> c */
861 /* */
862 /* bond: Bond two triangles together at the resepective handles. */
863 /* bond(abc, bad) */
864 /* */
865 /* */
866 /* For subsegments: */
867 /* */
868 /* ssym: Reverse the orientation of a subsegment. */
869 /* ssym(ab) -> ba */
870 /* */
871 /* spivot: Find adjoining subsegment with the same origin. */
872 /* spivot(ab) -> a* */
873 /* */
874 /* snext: Find next subsegment in sequence. */
875 /* snext(ab) -> b* */
876 /* */
877 /* sorg: Origin sdest: Destination */
878 /* sorg(ab) -> a sdest(ab) -> b */
879 /* */
880 /* sbond: Bond two subsegments together at the respective origins. */
881 /* sbond(ab, ac) */
882 /* */
883 /* */
884 /* For interacting tetrahedra and subfacets: */
885 /* */
886 /* tspivot: Find a subsegment abutting a triangle. */
887 /* tspivot(abc) -> ba */
888 /* */
889 /* stpivot: Find a triangle abutting a subsegment. */
890 /* stpivot(ab) -> ba* */
891 /* */
892 /* tsbond: Bond a triangle to a subsegment. */
893 /* tsbond(abc, ba) */
894 /* */
895 /*****************************************************************************/
896 
897 /********* Mesh manipulation primitives begin here *********/
898 /** **/
899 /** **/
900 
901 /* Fast lookup arrays to speed some of the mesh manipulation primitives. */
902 
903 int plus1mod3[3] = {1, 2, 0};
904 int minus1mod3[3] = {2, 0, 1};
905 
906 /********* Primitives for triangles *********/
907 /* */
908 /* */
909 
910 /* decode() converts a pointer to an oriented triangle. The orientation is */
911 /* extracted from the two least significant bits of the pointer. */
912 
913 #define decode(ptr, otri) \
914  (otri).orient = (int) ((uintptr_t) (ptr) & (uintptr_t) 3l); \
915  (otri).tri = (triangle *) \
916  ((uintptr_t) (ptr) ^ (uintptr_t) (otri).orient)
917 
918 /* encode() compresses an oriented triangle into a single pointer. It */
919 /* relies on the assumption that all triangles are aligned to four-byte */
920 /* boundaries, so the two least significant bits of (otri).tri are zero. */
921 
922 #define encode(otri) \
923  (triangle) ((uintptr_t) (otri).tri | (uintptr_t) (otri).orient)
924 
925 /* The following handle manipulation primitives are all described by Guibas */
926 /* and Stolfi. However, Guibas and Stolfi use an edge-based data */
927 /* structure, whereas I use a triangle-based data structure. */
928 
929 /* sym() finds the abutting triangle, on the same edge. Note that the edge */
930 /* direction is necessarily reversed, because the handle specified by an */
931 /* oriented triangle is directed counterclockwise around the triangle. */
932 
933 #define sym(otri1, otri2) \
934  ptr = (otri1).tri[(otri1).orient]; \
935  decode(ptr, otri2);
936 
937 #define symself(otri) \
938  ptr = (otri).tri[(otri).orient]; \
939  decode(ptr, otri);
940 
941 /* lnext() finds the next edge (counterclockwise) of a triangle. */
942 
943 #define lnext(otri1, otri2) \
944  (otri2).tri = (otri1).tri; \
945  (otri2).orient = plus1mod3[(otri1).orient]
946 
947 #define lnextself(otri) \
948  (otri).orient = plus1mod3[(otri).orient]
949 
950 /* lprev() finds the previous edge (clockwise) of a triangle. */
951 
952 #define lprev(otri1, otri2) \
953  (otri2).tri = (otri1).tri; \
954  (otri2).orient = minus1mod3[(otri1).orient]
955 
956 #define lprevself(otri) \
957  (otri).orient = minus1mod3[(otri).orient]
958 
959 /* onext() spins counterclockwise around a vertex; that is, it finds the */
960 /* next edge with the same origin in the counterclockwise direction. This */
961 /* edge is part of a different triangle. */
962 
963 #define onext(otri1, otri2) \
964  lprev(otri1, otri2); \
965  symself(otri2);
966 
967 #define onextself(otri) \
968  lprevself(otri); \
969  symself(otri);
970 
971 /* oprev() spins clockwise around a vertex; that is, it finds the next edge */
972 /* with the same origin in the clockwise direction. This edge is part of */
973 /* a different triangle. */
974 
975 #define oprev(otri1, otri2) \
976  sym(otri1, otri2); \
977  lnextself(otri2);
978 
979 #define oprevself(otri) \
980  symself(otri); \
981  lnextself(otri);
982 
983 /* dnext() spins counterclockwise around a vertex; that is, it finds the */
984 /* next edge with the same destination in the counterclockwise direction. */
985 /* This edge is part of a different triangle. */
986 
987 #define dnext(otri1, otri2) \
988  sym(otri1, otri2); \
989  lprevself(otri2);
990 
991 #define dnextself(otri) \
992  symself(otri); \
993  lprevself(otri);
994 
995 /* dprev() spins clockwise around a vertex; that is, it finds the next edge */
996 /* with the same destination in the clockwise direction. This edge is */
997 /* part of a different triangle. */
998 
999 #define dprev(otri1, otri2) \
1000  lnext(otri1, otri2); \
1001  symself(otri2);
1002 
1003 #define dprevself(otri) \
1004  lnextself(otri); \
1005  symself(otri);
1006 
1007 /* rnext() moves one edge counterclockwise about the adjacent triangle. */
1008 /* (It's best understood by reading Guibas and Stolfi. It involves */
1009 /* changing triangles twice.) */
1010 
1011 #define rnext(otri1, otri2) \
1012  sym(otri1, otri2); \
1013  lnextself(otri2); \
1014  symself(otri2);
1015 
1016 #define rnextself(otri) \
1017  symself(otri); \
1018  lnextself(otri); \
1019  symself(otri);
1020 
1021 /* rprev() moves one edge clockwise about the adjacent triangle. */
1022 /* (It's best understood by reading Guibas and Stolfi. It involves */
1023 /* changing triangles twice.) */
1024 
1025 #define rprev(otri1, otri2) \
1026  sym(otri1, otri2); \
1027  lprevself(otri2); \
1028  symself(otri2);
1029 
1030 #define rprevself(otri) \
1031  symself(otri); \
1032  lprevself(otri); \
1033  symself(otri);
1034 
1035 /* These primitives determine or set the origin, destination, or apex of a */
1036 /* triangle. */
1037 
1038 #define org(otri, vertexptr) \
1039  vertexptr = (vertex) (otri).tri[plus1mod3[(otri).orient] + 3]
1040 
1041 #define dest(otri, vertexptr) \
1042  vertexptr = (vertex) (otri).tri[minus1mod3[(otri).orient] + 3]
1043 
1044 #define apex(otri, vertexptr) \
1045  vertexptr = (vertex) (otri).tri[(otri).orient + 3]
1046 
1047 #define setorg(otri, vertexptr) \
1048  (otri).tri[plus1mod3[(otri).orient] + 3] = (triangle) vertexptr
1049 
1050 #define setdest(otri, vertexptr) \
1051  (otri).tri[minus1mod3[(otri).orient] + 3] = (triangle) vertexptr
1052 
1053 #define setapex(otri, vertexptr) \
1054  (otri).tri[(otri).orient + 3] = (triangle) vertexptr
1055 
1056 /* Bond two triangles together. */
1057 
1058 #define bond(otri1, otri2) \
1059  (otri1).tri[(otri1).orient] = encode(otri2); \
1060  (otri2).tri[(otri2).orient] = encode(otri1)
1061 
1062 /* Dissolve a bond (from one side). Note that the other triangle will still */
1063 /* think it's connected to this triangle. Usually, however, the other */
1064 /* triangle is being deleted entirely, or bonded to another triangle, so */
1065 /* it doesn't matter. */
1066 
1067 #define dissolve(otri) \
1068  (otri).tri[(otri).orient] = (triangle) m->dummytri
1069 
1070 /* Copy an oriented triangle. */
1071 
1072 #define otricopy(otri1, otri2) \
1073  (otri2).tri = (otri1).tri; \
1074  (otri2).orient = (otri1).orient
1075 
1076 /* Test for equality of oriented triangles. */
1077 
1078 #define otriequal(otri1, otri2) \
1079  (((otri1).tri == (otri2).tri) && \
1080  ((otri1).orient == (otri2).orient))
1081 
1082 /* Primitives to infect or cure a triangle with the virus. These rely on */
1083 /* the assumption that all subsegments are aligned to four-byte boundaries.*/
1084 
1085 #define infect(otri) \
1086  (otri).tri[6] = (triangle) \
1087  ((uintptr_t) (otri).tri[6] | (uintptr_t) 2l)
1088 
1089 #define uninfect(otri) \
1090  (otri).tri[6] = (triangle) \
1091  ((uintptr_t) (otri).tri[6] & ~ (uintptr_t) 2l)
1092 
1093 /* Test a triangle for viral infection. */
1094 
1095 #define infected(otri) \
1096  (((uintptr_t) (otri).tri[6] & (uintptr_t) 2l) != 0l)
1097 
1098 /* Check or set a triangle's attributes. */
1099 
1100 #define elemattribute(otri, attnum) \
1101  ((REAL *) (otri).tri)[m->elemattribindex + (attnum)]
1102 
1103 #define setelemattribute(otri, attnum, value) \
1104  ((REAL *) (otri).tri)[m->elemattribindex + (attnum)] = value
1105 
1106 /* Check or set a triangle's maximum area bound. */
1107 
1108 #define areabound(otri) ((REAL *) (otri).tri)[m->areaboundindex]
1109 
1110 #define setareabound(otri, value) \
1111  ((REAL *) (otri).tri)[m->areaboundindex] = value
1112 
1113 /* Check or set a triangle's deallocation. Its second pointer is set to */
1114 /* NULL to indicate that it is not allocated. (Its first pointer is used */
1115 /* for the stack of dead items.) Its fourth pointer (its first vertex) */
1116 /* is set to NULL in case a `badtriang' structure points to it. */
1117 
1118 #define deadtri(tria) ((tria)[1] == (triangle) NULL)
1119 
1120 #define killtri(tria) \
1121  (tria)[1] = (triangle) NULL; \
1122  (tria)[3] = (triangle) NULL
1123 
1124 /********* Primitives for subsegments *********/
1125 /* */
1126 /* */
1127 
1128 /* sdecode() converts a pointer to an oriented subsegment. The orientation */
1129 /* is extracted from the least significant bit of the pointer. The two */
1130 /* least significant bits (one for orientation, one for viral infection) */
1131 /* are masked out to produce the real pointer. */
1132 
1133 #define sdecode(sptr, osub) \
1134  (osub).ssorient = (int) ((uintptr_t) (sptr) & (uintptr_t) 1l); \
1135  (osub).ss = (subseg *) \
1136  ((uintptr_t) (sptr) & ~ (uintptr_t) 3l)
1137 
1138 /* sencode() compresses an oriented subsegment into a single pointer. It */
1139 /* relies on the assumption that all subsegments are aligned to two-byte */
1140 /* boundaries, so the least significant bit of (osub).ss is zero. */
1141 
1142 #define sencode(osub) \
1143  (subseg) ((uintptr_t) (osub).ss | (uintptr_t) (osub).ssorient)
1144 
1145 /* ssym() toggles the orientation of a subsegment. */
1146 
1147 #define ssym(osub1, osub2) \
1148  (osub2).ss = (osub1).ss; \
1149  (osub2).ssorient = 1 - (osub1).ssorient
1150 
1151 #define ssymself(osub) \
1152  (osub).ssorient = 1 - (osub).ssorient
1153 
1154 /* spivot() finds the other subsegment (from the same segment) that shares */
1155 /* the same origin. */
1156 
1157 #define spivot(osub1, osub2) \
1158  sptr = (osub1).ss[(osub1).ssorient]; \
1159  sdecode(sptr, osub2)
1160 
1161 #define spivotself(osub) \
1162  sptr = (osub).ss[(osub).ssorient]; \
1163  sdecode(sptr, osub)
1164 
1165 /* snext() finds the next subsegment (from the same segment) in sequence; */
1166 /* one whose origin is the input subsegment's destination. */
1167 
1168 #define snext(osub1, osub2) \
1169  sptr = (osub1).ss[1 - (osub1).ssorient]; \
1170  sdecode(sptr, osub2)
1171 
1172 #define snextself(osub) \
1173  sptr = (osub).ss[1 - (osub).ssorient]; \
1174  sdecode(sptr, osub)
1175 
1176 /* These primitives determine or set the origin or destination of a */
1177 /* subsegment or the segment that includes it. */
1178 
1179 #define sorg(osub, vertexptr) \
1180  vertexptr = (vertex) (osub).ss[2 + (osub).ssorient]
1181 
1182 #define sdest(osub, vertexptr) \
1183  vertexptr = (vertex) (osub).ss[3 - (osub).ssorient]
1184 
1185 #define setsorg(osub, vertexptr) \
1186  (osub).ss[2 + (osub).ssorient] = (subseg) vertexptr
1187 
1188 #define setsdest(osub, vertexptr) \
1189  (osub).ss[3 - (osub).ssorient] = (subseg) vertexptr
1190 
1191 #define segorg(osub, vertexptr) \
1192  vertexptr = (vertex) (osub).ss[4 + (osub).ssorient]
1193 
1194 #define segdest(osub, vertexptr) \
1195  vertexptr = (vertex) (osub).ss[5 - (osub).ssorient]
1196 
1197 #define setsegorg(osub, vertexptr) \
1198  (osub).ss[4 + (osub).ssorient] = (subseg) vertexptr
1199 
1200 #define setsegdest(osub, vertexptr) \
1201  (osub).ss[5 - (osub).ssorient] = (subseg) vertexptr
1202 
1203 /* These primitives read or set a boundary marker. Boundary markers are */
1204 /* used to hold user-defined tags for setting boundary conditions in */
1205 /* finite element solvers. */
1206 
1207 #define mark(osub) (* (int *) ((osub).ss + 8))
1208 
1209 #define setmark(osub, value) \
1210  * (int *) ((osub).ss + 8) = value
1211 
1212 /* Bond two subsegments together. */
1213 
1214 #define sbond(osub1, osub2) \
1215  (osub1).ss[(osub1).ssorient] = sencode(osub2); \
1216  (osub2).ss[(osub2).ssorient] = sencode(osub1)
1217 
1218 /* Dissolve a subsegment bond (from one side). Note that the other */
1219 /* subsegment will still think it's connected to this subsegment. */
1220 
1221 #define sdissolve(osub) \
1222  (osub).ss[(osub).ssorient] = (subseg) m->dummysub
1223 
1224 /* Copy a subsegment. */
1225 
1226 #define subsegcopy(osub1, osub2) \
1227  (osub2).ss = (osub1).ss; \
1228  (osub2).ssorient = (osub1).ssorient
1229 
1230 /* Test for equality of subsegments. */
1231 
1232 #define subsegequal(osub1, osub2) \
1233  (((osub1).ss == (osub2).ss) && \
1234  ((osub1).ssorient == (osub2).ssorient))
1235 
1236 /* Check or set a subsegment's deallocation. Its second pointer is set to */
1237 /* NULL to indicate that it is not allocated. (Its first pointer is used */
1238 /* for the stack of dead items.) Its third pointer (its first vertex) */
1239 /* is set to NULL in case a `badsubseg' structure points to it. */
1240 
1241 #define deadsubseg(sub) ((sub)[1] == (subseg) NULL)
1242 
1243 #define killsubseg(sub) \
1244  (sub)[1] = (subseg) NULL; \
1245  (sub)[2] = (subseg) NULL
1246 
1247 /********* Primitives for interacting triangles and subsegments *********/
1248 /* */
1249 /* */
1250 
1251 /* tspivot() finds a subsegment abutting a triangle. */
1252 
1253 #define tspivot(otri, osub) \
1254  sptr = (subseg) (otri).tri[6 + (otri).orient]; \
1255  sdecode(sptr, osub)
1256 
1257 /* stpivot() finds a triangle abutting a subsegment. It requires that the */
1258 /* variable `ptr' of type `triangle' be defined. */
1259 
1260 #define stpivot(osub, otri) \
1261  ptr = (triangle) (osub).ss[6 + (osub).ssorient]; \
1262  decode(ptr, otri)
1263 
1264 /* Bond a triangle to a subsegment. */
1265 
1266 #define tsbond(otri, osub) \
1267  (otri).tri[6 + (otri).orient] = (triangle) sencode(osub); \
1268  (osub).ss[6 + (osub).ssorient] = (subseg) encode(otri)
1269 
1270 /* Dissolve a bond (from the triangle side). */
1271 
1272 #define tsdissolve(otri) \
1273  (otri).tri[6 + (otri).orient] = (triangle) m->dummysub
1274 
1275 /* Dissolve a bond (from the subsegment side). */
1276 
1277 #define stdissolve(osub) \
1278  (osub).ss[6 + (osub).ssorient] = (subseg) m->dummytri
1279 
1280 /********* Primitives for vertices *********/
1281 /* */
1282 /* */
1283 
1284 #define vertexmark(vx) ((int *) (vx))[m->vertexmarkindex]
1285 
1286 #define setvertexmark(vx, value) \
1287  ((int *) (vx))[m->vertexmarkindex] = value
1288 
1289 #define vertextype(vx) ((int *) (vx))[m->vertexmarkindex + 1]
1290 
1291 #define setvertextype(vx, value) \
1292  ((int *) (vx))[m->vertexmarkindex + 1] = value
1293 
1294 #define vertex2tri(vx) ((triangle *) (vx))[m->vertex2triindex]
1295 
1296 #define setvertex2tri(vx, value) \
1297  ((triangle *) (vx))[m->vertex2triindex] = value
1298 
1299 /** **/
1300 /** **/
1301 /********* Mesh manipulation primitives end here *********/
1302 
1303 /********* User-defined triangle evaluation routine begins here *********/
1304 /** **/
1305 /** **/
1306 
1307 /*****************************************************************************/
1308 /* */
1309 /* triunsuitable() Determine if a triangle is unsuitable, and thus must */
1310 /* be further refined. */
1311 /* */
1312 /* You may write your own procedure that decides whether or not a selected */
1313 /* triangle is too big (and needs to be refined). There are two ways to do */
1314 /* this. */
1315 /* */
1316 /* (1) Modify the procedure `triunsuitable' below, then recompile */
1317 /* Triangle. */
1318 /* */
1319 /* (2) Define the symbol EXTERNAL_TEST (either by adding the definition */
1320 /* to this file, or by using the appropriate compiler switch). This way, */
1321 /* you can compile triangle.c separately from your test. Write your own */
1322 /* `triunsuitable' procedure in a separate C file (using the same prototype */
1323 /* as below). Compile it and link the object code with triangle.o. */
1324 /* */
1325 /* This procedure returns 1 if the triangle is too large and should be */
1326 /* refined; 0 otherwise. */
1327 /* */
1328 /*****************************************************************************/
1329 
1330 #ifdef EXTERNAL_TEST
1331 
1332 int triunsuitable();
1333 
1334 #else /* not EXTERNAL_TEST */
1335 
1336 #ifdef ANSI_DECLARATORS
1337 int triunsuitable(vertex triorg, vertex tridest, vertex triapex, REAL area )
1338 #else /* not ANSI_DECLARATORS */
1339 int triunsuitable(triorg, tridest, triapex, area)
1340 vertex triorg; /* The triangle's origin vertex. */
1341 vertex tridest; /* The triangle's destination vertex. */
1342 vertex triapex; /* The triangle's apex vertex. */
1343 REAL area; /* The area of the triangle. */
1344 #endif /* not ANSI_DECLARATORS */
1345 
1346 {
1347  REAL dxoa, dxda, dxod;
1348  REAL dyoa, dyda, dyod;
1349  REAL oalen, dalen, odlen;
1350  REAL maxlen;
1351 
1352  (void)area; /*LM: added to suppress warning */
1353 
1354  dxoa = triorg[0] - triapex[0];
1355  dyoa = triorg[1] - triapex[1];
1356  dxda = tridest[0] - triapex[0];
1357  dyda = tridest[1] - triapex[1];
1358  dxod = triorg[0] - tridest[0];
1359  dyod = triorg[1] - tridest[1];
1360  /* Find the squares of the lengths of the triangle's three edges. */
1361  oalen = dxoa * dxoa + dyoa * dyoa;
1362  dalen = dxda * dxda + dyda * dyda;
1363  odlen = dxod * dxod + dyod * dyod;
1364  /* Find the square of the length of the longest edge. */
1365  maxlen = (dalen > oalen) ? dalen : oalen;
1366  maxlen = (odlen > maxlen) ? odlen : maxlen;
1367 
1368  if (maxlen > 0.05 * (triorg[0] * triorg[0] + triorg[1] * triorg[1]) + 0.02) {
1369  return 1;
1370  } else {
1371  return 0;
1372  }
1373 }
1374 
1375 #endif /* not EXTERNAL_TEST */
1376 
1377 /** **/
1378 /** **/
1379 /********* User-defined triangle evaluation routine ends here *********/
1380 
1381 /********* Memory allocation and program exit wrappers begin here *********/
1382 /** **/
1383 /** **/
1384 
1385 #ifdef ANSI_DECLARATORS
1386 void triexit(int status)
1387 #else /* not ANSI_DECLARATORS */
1388 void triexit(status)
1389 int status;
1390 #endif /* not ANSI_DECLARATORS */
1391 
1392 {
1393  exit(status);
1394 }
1395 
1396 #ifdef ANSI_DECLARATORS
1398 #else /* not ANSI_DECLARATORS */
1400 int size;
1401 #endif /* not ANSI_DECLARATORS */
1402 
1403 {
1404  VOID *memptr;
1405 
1406  memptr = (VOID *) malloc((unsigned int) size);
1407  if (memptr == (VOID *) NULL) {
1408  printf("Error: Out of memory.\n");
1409  triexit(1);
1410  }
1411  return(memptr);
1412 }
1413 
1414 #ifdef ANSI_DECLARATORS
1415 void trifree(VOID *memptr)
1416 #else /* not ANSI_DECLARATORS */
1417 void trifree(memptr)
1418 VOID *memptr;
1419 #endif /* not ANSI_DECLARATORS */
1420 
1421 {
1422  free(memptr);
1423 }
1424 
1425 /** **/
1426 /** **/
1427 /********* Memory allocation and program exit wrappers end here *********/
1428 
1429 /********* User interaction routines begin here *********/
1430 /** **/
1431 /** **/
1432 
1433 /*****************************************************************************/
1434 /* */
1435 /* syntax() Print list of command line switches. */
1436 /* */
1437 /*****************************************************************************/
1438 
1439 #ifndef TRILIBRARY
1440 
1441 void syntax()
1442 {
1443 #ifdef CDT_ONLY
1444 #ifdef REDUCED
1445  printf("triangle [-pAcjevngBPNEIOXzo_lQVh] input_file\n");
1446 #else /* not REDUCED */
1447  printf("triangle [-pAcjevngBPNEIOXzo_iFlCQVh] input_file\n");
1448 #endif /* not REDUCED */
1449 #else /* not CDT_ONLY */
1450 #ifdef REDUCED
1451  printf("triangle [-prq__a__uAcDjevngBPNEIOXzo_YS__lQVh] input_file\n");
1452 #else /* not REDUCED */
1453  printf("triangle [-prq__a__uAcDjevngBPNEIOXzo_YS__iFlsCQVh] input_file\n");
1454 #endif /* not REDUCED */
1455 #endif /* not CDT_ONLY */
1456 
1457  printf(" -p Triangulates a Planar Straight Line Graph (.poly file).\n");
1458 #ifndef CDT_ONLY
1459  printf(" -r Refines a previously generated mesh.\n");
1460  printf(
1461  " -q Quality mesh generation. A minimum angle may be specified.\n");
1462  printf(" -a Applies a maximum triangle area constraint.\n");
1463  printf(" -u Applies a user-defined triangle constraint.\n");
1464 #endif /* not CDT_ONLY */
1465  printf(
1466  " -A Applies attributes to identify triangles in certain regions.\n");
1467  printf(" -c Encloses the convex hull with segments.\n");
1468 #ifndef CDT_ONLY
1469  printf(" -D Conforming Delaunay: all triangles are truly Delaunay.\n");
1470 #endif /* not CDT_ONLY */
1471 /*
1472  printf(" -w Weighted Delaunay triangulation.\n");
1473  printf(" -W Regular triangulation (lower hull of a height field).\n");
1474 */
1475  printf(" -j Jettison unused vertices from output .node file.\n");
1476  printf(" -e Generates an edge list.\n");
1477  printf(" -v Generates a Voronoi diagram.\n");
1478  printf(" -n Generates a list of triangle neighbors.\n");
1479  printf(" -g Generates an .off file for Geomview.\n");
1480  printf(" -B Suppresses output of boundary information.\n");
1481  printf(" -P Suppresses output of .poly file.\n");
1482  printf(" -N Suppresses output of .node file.\n");
1483  printf(" -E Suppresses output of .ele file.\n");
1484  printf(" -I Suppresses mesh iteration numbers.\n");
1485  printf(" -O Ignores holes in .poly file.\n");
1486  printf(" -X Suppresses use of exact arithmetic.\n");
1487  printf(" -z Numbers all items starting from zero (rather than one).\n");
1488  printf(" -o2 Generates second-order subparametric elements.\n");
1489 #ifndef CDT_ONLY
1490  printf(" -Y Suppresses boundary segment splitting.\n");
1491  printf(" -S Specifies maximum number of added Steiner points.\n");
1492 #endif /* not CDT_ONLY */
1493 #ifndef REDUCED
1494  printf(" -i Uses incremental method, rather than divide-and-conquer.\n");
1495  printf(" -F Uses Fortune's sweepline algorithm, rather than d-and-c.\n");
1496 #endif /* not REDUCED */
1497  printf(" -l Uses vertical cuts only, rather than alternating cuts.\n");
1498 #ifndef REDUCED
1499 #ifndef CDT_ONLY
1500  printf(
1501  " -s Force segments into mesh by splitting (instead of using CDT).\n");
1502 #endif /* not CDT_ONLY */
1503  printf(" -C Check consistency of final mesh.\n");
1504 #endif /* not REDUCED */
1505  printf(" -Q Quiet: No terminal output except errors.\n");
1506  printf(" -V Verbose: Detailed information on what I'm doing.\n");
1507  printf(" -h Help: Detailed instructions for Triangle.\n");
1508  triexit(0);
1509 }
1510 
1511 #endif /* not TRILIBRARY */
1512 
1513 /*****************************************************************************/
1514 /* */
1515 /* info() Print out complete instructions. */
1516 /* */
1517 /*****************************************************************************/
1518 
1519 #ifndef TRILIBRARY
1520 
1521 void info()
1522 {
1523  printf("Triangle\n");
1524  printf(
1525 "A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\n");
1526  printf("Version 1.6\n\n");
1527  printf(
1528 "Copyright 1993, 1995, 1997, 1998, 2002, 2005 Jonathan Richard Shewchuk\n");
1529  printf("2360 Woolsey #H / Berkeley, California 94705-1927\n");
1530  printf("Bugs/comments to jrs@cs.berkeley.edu\n");
1531  printf(
1532 "Created as part of the Quake project (tools for earthquake simulation).\n");
1533  printf(
1534 "Supported in part by NSF Grant CMS-9318163 and an NSERC 1967 Scholarship.\n");
1535  printf("There is no warranty whatsoever. Use at your own risk.\n");
1536 #ifdef SINGLE
1537  printf("This executable is compiled for single precision arithmetic.\n\n\n");
1538 #else /* not SINGLE */
1539  printf("This executable is compiled for double precision arithmetic.\n\n\n");
1540 #endif /* not SINGLE */
1541  printf(
1542 "Triangle generates exact Delaunay triangulations, constrained Delaunay\n");
1543  printf(
1544 "triangulations, conforming Delaunay triangulations, Voronoi diagrams, and\n");
1545  printf(
1546 "high-quality triangular meshes. The latter can be generated with no small\n"
1547 );
1548  printf(
1549 "or large angles, and are thus suitable for finite element analysis. If no\n"
1550 );
1551  printf(
1552 "command line switch is specified, your .node input file is read, and the\n");
1553  printf(
1554 "Delaunay triangulation is returned in .node and .ele output files. The\n");
1555  printf("command syntax is:\n\n");
1556  printf("triangle [-prq__a__uAcDjevngBPNEIOXzo_YS__iFlsCQVh] input_file\n\n");
1557  printf(
1558 "Underscores indicate that numbers may optionally follow certain switches.\n");
1559  printf(
1560 "Do not leave any space between a switch and its numeric parameter.\n");
1561  printf(
1562 "input_file must be a file with extension .node, or extension .poly if the\n");
1563  printf(
1564 "-p switch is used. If -r is used, you must supply .node and .ele files,\n");
1565  printf(
1566 "and possibly a .poly file and an .area file as well. The formats of these\n"
1567 );
1568  printf("files are described below.\n\n");
1569  printf("Command Line Switches:\n\n");
1570  printf(
1571 " -p Reads a Planar Straight Line Graph (.poly file), which can specify\n"
1572 );
1573  printf(
1574 " vertices, segments, holes, regional attributes, and regional area\n");
1575  printf(
1576 " constraints. Generates a constrained Delaunay triangulation (CDT)\n"
1577 );
1578  printf(
1579 " fitting the input; or, if -s, -q, -a, or -u is used, a conforming\n");
1580  printf(
1581 " constrained Delaunay triangulation (CCDT). If you want a truly\n");
1582  printf(
1583 " Delaunay (not just constrained Delaunay) triangulation, use -D as\n");
1584  printf(
1585 " well. When -p is not used, Triangle reads a .node file by default.\n"
1586 );
1587  printf(
1588 " -r Refines a previously generated mesh. The mesh is read from a .node\n"
1589 );
1590  printf(
1591 " file and an .ele file. If -p is also used, a .poly file is read\n");
1592  printf(
1593 " and used to constrain segments in the mesh. If -a is also used\n");
1594  printf(
1595 " (with no number following), an .area file is read and used to\n");
1596  printf(
1597 " impose area constraints on the mesh. Further details on refinement\n"
1598 );
1599  printf(" appear below.\n");
1600  printf(
1601 " -q Quality mesh generation by Delaunay refinement (a hybrid of Paul\n");
1602  printf(
1603 " Chew's and Jim Ruppert's algorithms). Adds vertices to the mesh to\n"
1604 );
1605  printf(
1606 " ensure that all angles are between 20 and 140 degrees. An\n");
1607  printf(
1608 " alternative bound on the minimum angle, replacing 20 degrees, may\n");
1609  printf(
1610 " be specified after the `q'. The specified angle may include a\n");
1611  printf(
1612 " decimal point, but not exponential notation. Note that a bound of\n"
1613 );
1614  printf(
1615 " theta degrees on the smallest angle also implies a bound of\n");
1616  printf(
1617 " (180 - 2 theta) on the largest angle. If the minimum angle is 28.6\n"
1618 );
1619  printf(
1620 " degrees or smaller, Triangle is mathematically guaranteed to\n");
1621  printf(
1622 " terminate (assuming infinite precision arithmetic--Triangle may\n");
1623  printf(
1624 " fail to terminate if you run out of precision). In practice,\n");
1625  printf(
1626 " Triangle often succeeds for minimum angles up to 34 degrees. For\n");
1627  printf(
1628 " some meshes, however, you might need to reduce the minimum angle to\n"
1629 );
1630  printf(
1631 " avoid problems associated with insufficient floating-point\n");
1632  printf(" precision.\n");
1633  printf(
1634 " -a Imposes a maximum triangle area. If a number follows the `a', no\n");
1635  printf(
1636 " triangle is generated whose area is larger than that number. If no\n"
1637 );
1638  printf(
1639 " number is specified, an .area file (if -r is used) or .poly file\n");
1640  printf(
1641 " (if -r is not used) specifies a set of maximum area constraints.\n");
1642  printf(
1643 " An .area file contains a separate area constraint for each\n");
1644  printf(
1645 " triangle, and is useful for refining a finite element mesh based on\n"
1646 );
1647  printf(
1648 " a posteriori error estimates. A .poly file can optionally contain\n"
1649 );
1650  printf(
1651 " an area constraint for each segment-bounded region, thereby\n");
1652  printf(
1653 " controlling triangle densities in a first triangulation of a PSLG.\n"
1654 );
1655  printf(
1656 " You can impose both a fixed area constraint and a varying area\n");
1657  printf(
1658 " constraint by invoking the -a switch twice, once with and once\n");
1659  printf(
1660 " without a number following. Each area specified may include a\n");
1661  printf(" decimal point.\n");
1662  printf(
1663 " -u Imposes a user-defined constraint on triangle size. There are two\n"
1664 );
1665  printf(
1666 " ways to use this feature. One is to edit the triunsuitable()\n");
1667  printf(
1668 " procedure in triangle.c to encode any constraint you like, then\n");
1669  printf(
1670 " recompile Triangle. The other is to compile triangle.c with the\n");
1671  printf(
1672 " EXTERNAL_TEST symbol set (compiler switch -DEXTERNAL_TEST), then\n");
1673  printf(
1674 " link Triangle with a separate object file that implements\n");
1675  printf(
1676 " triunsuitable(). In either case, the -u switch causes the user-\n");
1677  printf(" defined test to be applied to every triangle.\n");
1678  printf(
1679 " -A Assigns an additional floating-point attribute to each triangle\n");
1680  printf(
1681 " that identifies what segment-bounded region each triangle belongs\n");
1682  printf(
1683 " to. Attributes are assigned to regions by the .poly file. If a\n");
1684  printf(
1685 " region is not explicitly marked by the .poly file, triangles in\n");
1686  printf(
1687 " that region are assigned an attribute of zero. The -A switch has\n");
1688  printf(
1689 " an effect only when the -p switch is used and the -r switch is not.\n"
1690 );
1691  printf(
1692 " -c Creates segments on the convex hull of the triangulation. If you\n");
1693  printf(
1694 " are triangulating a vertex set, this switch causes a .poly file to\n"
1695 );
1696  printf(
1697 " be written, containing all edges of the convex hull. If you are\n");
1698  printf(
1699 " triangulating a PSLG, this switch specifies that the whole convex\n");
1700  printf(
1701 " hull of the PSLG should be triangulated, regardless of what\n");
1702  printf(
1703 " segments the PSLG has. If you do not use this switch when\n");
1704  printf(
1705 " triangulating a PSLG, Triangle assumes that you have identified the\n"
1706 );
1707  printf(
1708 " region to be triangulated by surrounding it with segments of the\n");
1709  printf(
1710 " input PSLG. Beware: if you are not careful, this switch can cause\n"
1711 );
1712  printf(
1713 " the introduction of an extremely thin angle between a PSLG segment\n"
1714 );
1715  printf(
1716 " and a convex hull segment, which can cause overrefinement (and\n");
1717  printf(
1718 " possibly failure if Triangle runs out of precision). If you are\n");
1719  printf(
1720 " refining a mesh, the -c switch works differently: it causes a\n");
1721  printf(
1722 " .poly file to be written containing the boundary edges of the mesh\n"
1723 );
1724  printf(" (useful if no .poly file was read).\n");
1725  printf(
1726 " -D Conforming Delaunay triangulation: use this switch if you want to\n"
1727 );
1728  printf(
1729 " ensure that all the triangles in the mesh are Delaunay, and not\n");
1730  printf(
1731 " merely constrained Delaunay; or if you want to ensure that all the\n"
1732 );
1733  printf(
1734 " Voronoi vertices lie within the triangulation. (Some finite volume\n"
1735 );
1736  printf(
1737 " methods have this requirement.) This switch invokes Ruppert's\n");
1738  printf(
1739 " original algorithm, which splits every subsegment whose diametral\n");
1740  printf(
1741 " circle is encroached. It usually increases the number of vertices\n"
1742 );
1743  printf(" and triangles.\n");
1744  printf(
1745 " -j Jettisons vertices that are not part of the final triangulation\n");
1746  printf(
1747 " from the output .node file. By default, Triangle copies all\n");
1748  printf(
1749 " vertices in the input .node file to the output .node file, in the\n");
1750  printf(
1751 " same order, so their indices do not change. The -j switch prevents\n"
1752 );
1753  printf(
1754 " duplicated input vertices, or vertices `eaten' by holes, from\n");
1755  printf(
1756 " appearing in the output .node file. Thus, if two input vertices\n");
1757  printf(
1758 " have exactly the same coordinates, only the first appears in the\n");
1759  printf(
1760 " output. If any vertices are jettisoned, the vertex numbering in\n");
1761  printf(
1762 " the output .node file differs from that of the input .node file.\n");
1763  printf(
1764 " -e Outputs (to an .edge file) a list of edges of the triangulation.\n");
1765  printf(
1766 " -v Outputs the Voronoi diagram associated with the triangulation.\n");
1767  printf(
1768 " Does not attempt to detect degeneracies, so some Voronoi vertices\n");
1769  printf(
1770 " may be duplicated. See the discussion of Voronoi diagrams below.\n");
1771  printf(
1772 " -n Outputs (to a .neigh file) a list of triangles neighboring each\n");
1773  printf(" triangle.\n");
1774  printf(
1775 " -g Outputs the mesh to an Object File Format (.off) file, suitable for\n"
1776 );
1777  printf(" viewing with the Geometry Center's Geomview package.\n");
1778  printf(
1779 " -B No boundary markers in the output .node, .poly, and .edge output\n");
1780  printf(
1781 " files. See the detailed discussion of boundary markers below.\n");
1782  printf(
1783 " -P No output .poly file. Saves disk space, but you lose the ability\n");
1784  printf(
1785 " to maintain constraining segments on later refinements of the mesh.\n"
1786 );
1787  printf(" -N No output .node file.\n");
1788  printf(" -E No output .ele file.\n");
1789  printf(
1790 " -I No iteration numbers. Suppresses the output of .node and .poly\n");
1791  printf(
1792 " files, so your input files won't be overwritten. (If your input is\n"
1793 );
1794  printf(
1795 " a .poly file only, a .node file is written.) Cannot be used with\n");
1796  printf(
1797 " the -r switch, because that would overwrite your input .ele file.\n");
1798  printf(
1799 " Shouldn't be used with the -q, -a, -u, or -s switch if you are\n");
1800  printf(
1801 " using a .node file for input, because no .node file is written, so\n"
1802 );
1803  printf(" there is no record of any added Steiner points.\n");
1804  printf(" -O No holes. Ignores the holes in the .poly file.\n");
1805  printf(
1806 " -X No exact arithmetic. Normally, Triangle uses exact floating-point\n"
1807 );
1808  printf(
1809 " arithmetic for certain tests if it thinks the inexact tests are not\n"
1810 );
1811  printf(
1812 " accurate enough. Exact arithmetic ensures the robustness of the\n");
1813  printf(
1814 " triangulation algorithms, despite floating-point roundoff error.\n");
1815  printf(
1816 " Disabling exact arithmetic with the -X switch causes a small\n");
1817  printf(
1818 " improvement in speed and creates the possibility that Triangle will\n"
1819 );
1820  printf(" fail to produce a valid mesh. Not recommended.\n");
1821  printf(
1822 " -z Numbers all items starting from zero (rather than one). Note that\n"
1823 );
1824  printf(
1825 " this switch is normally overridden by the value used to number the\n"
1826 );
1827  printf(
1828 " first vertex of the input .node or .poly file. However, this\n");
1829  printf(
1830 " switch is useful when calling Triangle from another program.\n");
1831  printf(
1832 " -o2 Generates second-order subparametric elements with six nodes each.\n"
1833 );
1834  printf(
1835 " -Y No new vertices on the boundary. This switch is useful when the\n");
1836  printf(
1837 " mesh boundary must be preserved so that it conforms to some\n");
1838  printf(
1839 " adjacent mesh. Be forewarned that you will probably sacrifice much\n"
1840 );
1841  printf(
1842 " of the quality of the mesh; Triangle will try, but the resulting\n");
1843  printf(
1844 " mesh may contain poorly shaped triangles. Works well if all the\n");
1845  printf(
1846 " boundary vertices are closely spaced. Specify this switch twice\n");
1847  printf(
1848 " (`-YY') to prevent all segment splitting, including internal\n");
1849  printf(" boundaries.\n");
1850  printf(
1851 " -S Specifies the maximum number of Steiner points (vertices that are\n");
1852  printf(
1853 " not in the input, but are added to meet the constraints on minimum\n"
1854 );
1855  printf(
1856 " angle and maximum area). The default is to allow an unlimited\n");
1857  printf(
1858 " number. If you specify this switch with no number after it,\n");
1859  printf(
1860 " the limit is set to zero. Triangle always adds vertices at segment\n"
1861 );
1862  printf(
1863 " intersections, even if it needs to use more vertices than the limit\n"
1864 );
1865  printf(
1866 " you set. When Triangle inserts segments by splitting (-s), it\n");
1867  printf(
1868 " always adds enough vertices to ensure that all the segments of the\n"
1869 );
1870  printf(" PLSG are recovered, ignoring the limit if necessary.\n");
1871  printf(
1872 " -i Uses an incremental rather than a divide-and-conquer algorithm to\n");
1873  printf(
1874 " construct a Delaunay triangulation. Try it if the divide-and-\n");
1875  printf(" conquer algorithm fails.\n");
1876  printf(
1877 " -F Uses Steven Fortune's sweepline algorithm to construct a Delaunay\n");
1878  printf(
1879 " triangulation. Warning: does not use exact arithmetic for all\n");
1880  printf(" calculations. An exact result is not guaranteed.\n");
1881  printf(
1882 " -l Uses only vertical cuts in the divide-and-conquer algorithm. By\n");
1883  printf(
1884 " default, Triangle alternates between vertical and horizontal cuts,\n"
1885 );
1886  printf(
1887 " which usually improve the speed except with vertex sets that are\n");
1888  printf(
1889 " small or short and wide. This switch is primarily of theoretical\n");
1890  printf(" interest.\n");
1891  printf(
1892 " -s Specifies that segments should be forced into the triangulation by\n"
1893 );
1894  printf(
1895 " recursively splitting them at their midpoints, rather than by\n");
1896  printf(
1897 " generating a constrained Delaunay triangulation. Segment splitting\n"
1898 );
1899  printf(
1900 " is true to Ruppert's original algorithm, but can create needlessly\n"
1901 );
1902  printf(
1903 " small triangles. This switch is primarily of theoretical interest.\n"
1904 );
1905  printf(
1906 " -C Check the consistency of the final mesh. Uses exact arithmetic for\n"
1907 );
1908  printf(
1909 " checking, even if the -X switch is used. Useful if you suspect\n");
1910  printf(" Triangle is buggy.\n");
1911  printf(
1912 " -Q Quiet: Suppresses all explanation of what Triangle is doing,\n");
1913  printf(" unless an error occurs.\n");
1914  printf(
1915 " -V Verbose: Gives detailed information about what Triangle is doing.\n"
1916 );
1917  printf(
1918 " Add more `V's for increasing amount of detail. `-V' is most\n");
1919  printf(
1920 " useful; itgives information on algorithmic progress and much more\n");
1921  printf(
1922 " detailed statistics. `-VV' gives vertex-by-vertex details, and\n");
1923  printf(
1924 " prints so much that Triangle runs much more slowly. `-VVVV' gives\n"
1925 );
1926  printf(" information only a debugger could love.\n");
1927  printf(" -h Help: Displays these instructions.\n");
1928  printf("\n");
1929  printf("Definitions:\n");
1930  printf("\n");
1931  printf(
1932 " A Delaunay triangulation of a vertex set is a triangulation whose\n");
1933  printf(
1934 " vertices are the vertex set, that covers the convex hull of the vertex\n");
1935  printf(
1936 " set. A Delaunay triangulation has the property that no vertex lies\n");
1937  printf(
1938 " inside the circumscribing circle (circle that passes through all three\n");
1939  printf(" vertices) of any triangle in the triangulation.\n\n");
1940  printf(
1941 " A Voronoi diagram of a vertex set is a subdivision of the plane into\n");
1942  printf(
1943 " polygonal cells (some of which may be unbounded, meaning infinitely\n");
1944  printf(
1945 " large), where each cell is the set of points in the plane that are closer\n"
1946 );
1947  printf(
1948 " to some input vertex than to any other input vertex. The Voronoi diagram\n"
1949 );
1950  printf(" is a geometric dual of the Delaunay triangulation.\n\n");
1951  printf(
1952 " A Planar Straight Line Graph (PSLG) is a set of vertices and segments.\n");
1953  printf(
1954 " Segments are simply edges, whose endpoints are all vertices in the PSLG.\n"
1955 );
1956  printf(
1957 " Segments may intersect each other only at their endpoints. The file\n");
1958  printf(" format for PSLGs (.poly files) is described below.\n\n");
1959  printf(
1960 " A constrained Delaunay triangulation (CDT) of a PSLG is similar to a\n");
1961  printf(
1962 " Delaunay triangulation, but each PSLG segment is present as a single edge\n"
1963 );
1964  printf(
1965 " of the CDT. (A constrained Delaunay triangulation is not truly a\n");
1966  printf(
1967 " Delaunay triangulation, because some of its triangles might not be\n");
1968  printf(
1969 " Delaunay.) By definition, a CDT does not have any vertices other than\n");
1970  printf(
1971 " those specified in the input PSLG. Depending on context, a CDT might\n");
1972  printf(
1973 " cover the convex hull of the PSLG, or it might cover only a segment-\n");
1974  printf(" bounded region (e.g. a polygon).\n\n");
1975  printf(
1976 " A conforming Delaunay triangulation of a PSLG is a triangulation in which\n"
1977 );
1978  printf(
1979 " each triangle is truly Delaunay, and each PSLG segment is represented by\n"
1980 );
1981  printf(
1982 " a linear contiguous sequence of edges of the triangulation. New vertices\n"
1983 );
1984  printf(
1985 " (not part of the PSLG) may appear, and each input segment may have been\n");
1986  printf(
1987 " subdivided into shorter edges (subsegments) by these additional vertices.\n"
1988 );
1989  printf(
1990 " The new vertices are frequently necessary to maintain the Delaunay\n");
1991  printf(" property while ensuring that every segment is represented.\n\n");
1992  printf(
1993 " A conforming constrained Delaunay triangulation (CCDT) of a PSLG is a\n");
1994  printf(
1995 " triangulation of a PSLG whose triangles are constrained Delaunay. New\n");
1996  printf(" vertices may appear, and input segments may be subdivided into\n");
1997  printf(
1998 " subsegments, but not to guarantee that segments are respected; rather, to\n"
1999 );
2000  printf(
2001 " improve the quality of the triangles. The high-quality meshes produced\n");
2002  printf(
2003 " by the -q switch are usually CCDTs, but can be made conforming Delaunay\n");
2004  printf(" with the -D switch.\n\n");
2005  printf("File Formats:\n\n");
2006  printf(
2007 " All files may contain comments prefixed by the character '#'. Vertices,\n"
2008 );
2009  printf(
2010 " triangles, edges, holes, and maximum area constraints must be numbered\n");
2011  printf(
2012 " consecutively, starting from either 1 or 0. Whichever you choose, all\n");
2013  printf(
2014 " input files must be consistent; if the vertices are numbered from 1, so\n");
2015  printf(
2016 " must be all other objects. Triangle automatically detects your choice\n");
2017  printf(
2018 " while reading the .node (or .poly) file. (When calling Triangle from\n");
2019  printf(
2020 " another program, use the -z switch if you wish to number objects from\n");
2021  printf(" zero.) Examples of these file formats are given below.\n\n");
2022  printf(" .node files:\n");
2023  printf(
2024 " First line: <# of vertices> <dimension (must be 2)> <# of attributes>\n"
2025 );
2026  printf(
2027 " <# of boundary markers (0 or 1)>\n"
2028 );
2029  printf(
2030 " Remaining lines: <vertex #> <x> <y> [attributes] [boundary marker]\n");
2031  printf("\n");
2032  printf(
2033 " The attributes, which are typically floating-point values of physical\n");
2034  printf(
2035 " quantities (such as mass or conductivity) associated with the nodes of\n"
2036 );
2037  printf(
2038 " a finite element mesh, are copied unchanged to the output mesh. If -q,\n"
2039 );
2040  printf(
2041 " -a, -u, -D, or -s is selected, each new Steiner point added to the mesh\n"
2042 );
2043  printf(" has attributes assigned to it by linear interpolation.\n\n");
2044  printf(
2045 " If the fourth entry of the first line is `1', the last column of the\n");
2046  printf(
2047 " remainder of the file is assumed to contain boundary markers. Boundary\n"
2048 );
2049  printf(
2050 " markers are used to identify boundary vertices and vertices resting on\n"
2051 );
2052  printf(
2053 " PSLG segments; a complete description appears in a section below. The\n"
2054 );
2055  printf(
2056 " .node file produced by Triangle contains boundary markers in the last\n");
2057  printf(" column unless they are suppressed by the -B switch.\n\n");
2058  printf(" .ele files:\n");
2059  printf(
2060 " First line: <# of triangles> <nodes per triangle> <# of attributes>\n");
2061  printf(
2062 " Remaining lines: <triangle #> <node> <node> <node> ... [attributes]\n");
2063  printf("\n");
2064  printf(
2065 " Nodes are indices into the corresponding .node file. The first three\n");
2066  printf(
2067 " nodes are the corner vertices, and are listed in counterclockwise order\n"
2068 );
2069  printf(
2070 " around each triangle. (The remaining nodes, if any, depend on the type\n"
2071 );
2072  printf(" of finite element used.)\n\n");
2073  printf(
2074 " The attributes are just like those of .node files. Because there is no\n"
2075 );
2076  printf(
2077 " simple mapping from input to output triangles, Triangle attempts to\n");
2078  printf(
2079 " interpolate attributes, and may cause a lot of diffusion of attributes\n"
2080 );
2081  printf(
2082 " among nearby triangles as the triangulation is refined. Attributes do\n"
2083 );
2084  printf(" not diffuse across segments, so attributes used to identify\n");
2085  printf(" segment-bounded regions remain intact.\n\n");
2086  printf(
2087 " In .ele files produced by Triangle, each triangular element has three\n");
2088  printf(
2089 " nodes (vertices) unless the -o2 switch is used, in which case\n");
2090  printf(
2091 " subparametric quadratic elements with six nodes each are generated.\n");
2092  printf(
2093 " The first three nodes are the corners in counterclockwise order, and\n");
2094  printf(
2095 " the fourth, fifth, and sixth nodes lie on the midpoints of the edges\n");
2096  printf(
2097 " opposite the first, second, and third vertices, respectively.\n");
2098  printf("\n");
2099  printf(" .poly files:\n");
2100  printf(
2101 " First line: <# of vertices> <dimension (must be 2)> <# of attributes>\n"
2102 );
2103  printf(
2104 " <# of boundary markers (0 or 1)>\n"
2105 );
2106  printf(
2107 " Following lines: <vertex #> <x> <y> [attributes] [boundary marker]\n");
2108  printf(" One line: <# of segments> <# of boundary markers (0 or 1)>\n");
2109  printf(
2110 " Following lines: <segment #> <endpoint> <endpoint> [boundary marker]\n");
2111  printf(" One line: <# of holes>\n");
2112  printf(" Following lines: <hole #> <x> <y>\n");
2113  printf(
2114 " Optional line: <# of regional attributes and/or area constraints>\n");
2115  printf(
2116 " Optional following lines: <region #> <x> <y> <attribute> <max area>\n");
2117  printf("\n");
2118  printf(
2119 " A .poly file represents a PSLG, as well as some additional information.\n"
2120 );
2121  printf(
2122 " The first section lists all the vertices, and is identical to the\n");
2123  printf(
2124 " format of .node files. <# of vertices> may be set to zero to indicate\n"
2125 );
2126  printf(
2127 " that the vertices are listed in a separate .node file; .poly files\n");
2128  printf(
2129 " produced by Triangle always have this format. A vertex set represented\n"
2130 );
2131  printf(
2132 " this way has the advantage that it may easily be triangulated with or\n");
2133  printf(
2134 " without segments (depending on whether the -p switch is invoked).\n");
2135  printf("\n");
2136  printf(
2137 " The second section lists the segments. Segments are edges whose\n");
2138  printf(
2139 " presence in the triangulation is enforced. (Depending on the choice of\n"
2140 );
2141  printf(
2142 " switches, segment might be subdivided into smaller edges). Each\n");
2143  printf(
2144 " segment is specified by listing the indices of its two endpoints. This\n"
2145 );
2146  printf(
2147 " means that you must include its endpoints in the vertex list. Each\n");
2148  printf(" segment, like each point, may have a boundary marker.\n\n");
2149  printf(
2150 " If -q, -a, -u, and -s are not selected, Triangle produces a constrained\n"
2151 );
2152  printf(
2153 " Delaunay triangulation (CDT), in which each segment appears as a single\n"
2154 );
2155  printf(
2156 " edge in the triangulation. If -q, -a, -u, or -s is selected, Triangle\n"
2157 );
2158  printf(
2159 " produces a conforming constrained Delaunay triangulation (CCDT), in\n");
2160  printf(
2161 " which segments may be subdivided into smaller edges. If -D is\n");
2162  printf(
2163 " selected, Triangle produces a conforming Delaunay triangulation, so\n");
2164  printf(
2165 " that every triangle is Delaunay, and not just constrained Delaunay.\n");
2166  printf("\n");
2167  printf(
2168 " The third section lists holes (and concavities, if -c is selected) in\n");
2169  printf(
2170 " the triangulation. Holes are specified by identifying a point inside\n");
2171  printf(
2172 " each hole. After the triangulation is formed, Triangle creates holes\n");
2173  printf(
2174 " by eating triangles, spreading out from each hole point until its\n");
2175  printf(
2176 " progress is blocked by segments in the PSLG. You must be careful to\n");
2177  printf(
2178 " enclose each hole in segments, or your whole triangulation might be\n");
2179  printf(
2180 " eaten away. If the two triangles abutting a segment are eaten, the\n");
2181  printf(
2182 " segment itself is also eaten. Do not place a hole directly on a\n");
2183  printf(" segment; if you do, Triangle chooses one side of the segment\n");
2184  printf(" arbitrarily.\n\n");
2185  printf(
2186 " The optional fourth section lists regional attributes (to be assigned\n");
2187  printf(
2188 " to all triangles in a region) and regional constraints on the maximum\n");
2189  printf(
2190 " triangle area. Triangle reads this section only if the -A switch is\n");
2191  printf(
2192 " used or the -a switch is used without a number following it, and the -r\n"
2193 );
2194  printf(
2195 " switch is not used. Regional attributes and area constraints are\n");
2196  printf(
2197 " propagated in the same manner as holes: you specify a point for each\n");
2198  printf(
2199 " attribute and/or constraint, and the attribute and/or constraint\n");
2200  printf(
2201 " affects the whole region (bounded by segments) containing the point.\n");
2202  printf(
2203 " If two values are written on a line after the x and y coordinate, the\n");
2204  printf(
2205 " first such value is assumed to be a regional attribute (but is only\n");
2206  printf(
2207 " applied if the -A switch is selected), and the second value is assumed\n"
2208 );
2209  printf(
2210 " to be a regional area constraint (but is only applied if the -a switch\n"
2211 );
2212  printf(
2213 " is selected). You may specify just one value after the coordinates,\n");
2214  printf(
2215 " which can serve as both an attribute and an area constraint, depending\n"
2216 );
2217  printf(
2218 " on the choice of switches. If you are using the -A and -a switches\n");
2219  printf(
2220 " simultaneously and wish to assign an attribute to some region without\n");
2221  printf(" imposing an area constraint, use a negative maximum area.\n\n");
2222  printf(
2223 " When a triangulation is created from a .poly file, you must either\n");
2224  printf(
2225 " enclose the entire region to be triangulated in PSLG segments, or\n");
2226  printf(
2227 " use the -c switch, which automatically creates extra segments that\n");
2228  printf(
2229 " enclose the convex hull of the PSLG. If you do not use the -c switch,\n"
2230 );
2231  printf(
2232 " Triangle eats all triangles that are not enclosed by segments; if you\n");
2233  printf(
2234 " are not careful, your whole triangulation may be eaten away. If you do\n"
2235 );
2236  printf(
2237 " use the -c switch, you can still produce concavities by the appropriate\n"
2238 );
2239  printf(
2240 " placement of holes just inside the boundary of the convex hull.\n");
2241  printf("\n");
2242  printf(
2243 " An ideal PSLG has no intersecting segments, nor any vertices that lie\n");
2244  printf(
2245 " upon segments (except, of course, the endpoints of each segment). You\n"
2246 );
2247  printf(
2248 " aren't required to make your .poly files ideal, but you should be aware\n"
2249 );
2250  printf(
2251 " of what can go wrong. Segment intersections are relatively safe--\n");
2252  printf(
2253 " Triangle calculates the intersection points for you and adds them to\n");
2254  printf(
2255 " the triangulation--as long as your machine's floating-point precision\n");
2256  printf(
2257 " doesn't become a problem. You are tempting the fates if you have three\n"
2258 );
2259  printf(
2260 " segments that cross at the same location, and expect Triangle to figure\n"
2261 );
2262  printf(
2263 " out where the intersection point is. Thanks to floating-point roundoff\n"
2264 );
2265  printf(
2266 " error, Triangle will probably decide that the three segments intersect\n"
2267 );
2268  printf(
2269 " at three different points, and you will find a minuscule triangle in\n");
2270  printf(
2271 " your output--unless Triangle tries to refine the tiny triangle, uses\n");
2272  printf(
2273 " up the last bit of machine precision, and fails to terminate at all.\n");
2274  printf(
2275 " You're better off putting the intersection point in the input files,\n");
2276  printf(
2277 " and manually breaking up each segment into two. Similarly, if you\n");
2278  printf(
2279 " place a vertex at the middle of a segment, and hope that Triangle will\n"
2280 );
2281  printf(
2282 " break up the segment at that vertex, you might get lucky. On the other\n"
2283 );
2284  printf(
2285 " hand, Triangle might decide that the vertex doesn't lie precisely on\n");
2286  printf(
2287 " the segment, and you'll have a needle-sharp triangle in your output--or\n"
2288 );
2289  printf(" a lot of tiny triangles if you're generating a quality mesh.\n");
2290  printf("\n");
2291  printf(
2292 " When Triangle reads a .poly file, it also writes a .poly file, which\n");
2293  printf(
2294 " includes all the subsegments--the edges that are parts of input\n");
2295  printf(
2296 " segments. If the -c switch is used, the output .poly file also\n");
2297  printf(
2298 " includes all of the edges on the convex hull. Hence, the output .poly\n"
2299 );
2300  printf(
2301 " file is useful for finding edges associated with input segments and for\n"
2302 );
2303  printf(
2304 " setting boundary conditions in finite element simulations. Moreover,\n");
2305  printf(
2306 " you will need the output .poly file if you plan to refine the output\n");
2307  printf(
2308 " mesh, and don't want segments to be missing in later triangulations.\n");
2309  printf("\n");
2310  printf(" .area files:\n");
2311  printf(" First line: <# of triangles>\n");
2312  printf(" Following lines: <triangle #> <maximum area>\n");
2313  printf("\n");
2314  printf(
2315 " An .area file associates with each triangle a maximum area that is used\n"
2316 );
2317  printf(
2318 " for mesh refinement. As with other file formats, every triangle must\n");
2319  printf(
2320 " be represented, and the triangles must be numbered consecutively. A\n");
2321  printf(
2322 " triangle may be left unconstrained by assigning it a negative maximum\n");
2323  printf(" area.\n\n");
2324  printf(" .edge files:\n");
2325  printf(" First line: <# of edges> <# of boundary markers (0 or 1)>\n");
2326  printf(
2327 " Following lines: <edge #> <endpoint> <endpoint> [boundary marker]\n");
2328  printf("\n");
2329  printf(
2330 " Endpoints are indices into the corresponding .node file. Triangle can\n"
2331 );
2332  printf(
2333 " produce .edge files (use the -e switch), but cannot read them. The\n");
2334  printf(
2335 " optional column of boundary markers is suppressed by the -B switch.\n");
2336  printf("\n");
2337  printf(
2338 " In Voronoi diagrams, one also finds a special kind of edge that is an\n");
2339  printf(
2340 " infinite ray with only one endpoint. For these edges, a different\n");
2341  printf(" format is used:\n\n");
2342  printf(" <edge #> <endpoint> -1 <direction x> <direction y>\n\n");
2343  printf(
2344 " The `direction' is a floating-point vector that indicates the direction\n"
2345 );
2346  printf(" of the infinite ray.\n\n");
2347  printf(" .neigh files:\n");
2348  printf(
2349 " First line: <# of triangles> <# of neighbors per triangle (always 3)>\n"
2350 );
2351  printf(
2352 " Following lines: <triangle #> <neighbor> <neighbor> <neighbor>\n");
2353  printf("\n");
2354  printf(
2355 " Neighbors are indices into the corresponding .ele file. An index of -1\n"
2356 );
2357  printf(
2358 " indicates no neighbor (because the triangle is on an exterior\n");
2359  printf(
2360 " boundary). The first neighbor of triangle i is opposite the first\n");
2361  printf(" corner of triangle i, and so on.\n\n");
2362  printf(
2363 " Triangle can produce .neigh files (use the -n switch), but cannot read\n"
2364 );
2365  printf(" them.\n\n");
2366  printf("Boundary Markers:\n\n");
2367  printf(
2368 " Boundary markers are tags used mainly to identify which output vertices\n");
2369  printf(
2370 " and edges are associated with which PSLG segment, and to identify which\n");
2371  printf(
2372 " vertices and edges occur on a boundary of the triangulation. A common\n");
2373  printf(
2374 " use is to determine where boundary conditions should be applied to a\n");
2375  printf(
2376 " finite element mesh. You can prevent boundary markers from being written\n"
2377 );
2378  printf(" into files produced by Triangle by using the -B switch.\n\n");
2379  printf(
2380 " The boundary marker associated with each segment in an output .poly file\n"
2381 );
2382  printf(" and each edge in an output .edge file is chosen as follows:\n");
2383  printf(
2384 " - If an output edge is part or all of a PSLG segment with a nonzero\n");
2385  printf(
2386 " boundary marker, then the edge is assigned the same marker.\n");
2387  printf(
2388 " - Otherwise, if the edge lies on a boundary of the triangulation\n");
2389  printf(
2390 " (even the boundary of a hole), then the edge is assigned the marker\n");
2391  printf(" one (1).\n");
2392  printf(" - Otherwise, the edge is assigned the marker zero (0).\n");
2393  printf(
2394 " The boundary marker associated with each vertex in an output .node file\n");
2395  printf(" is chosen as follows:\n");
2396  printf(
2397 " - If a vertex is assigned a nonzero boundary marker in the input file,\n"
2398 );
2399  printf(
2400 " then it is assigned the same marker in the output .node file.\n");
2401  printf(
2402 " - Otherwise, if the vertex lies on a PSLG segment (even if it is an\n");
2403  printf(
2404 " endpoint of the segment) with a nonzero boundary marker, then the\n");
2405  printf(
2406 " vertex is assigned the same marker. If the vertex lies on several\n");
2407  printf(" such segments, one of the markers is chosen arbitrarily.\n");
2408  printf(
2409 " - Otherwise, if the vertex occurs on a boundary of the triangulation,\n");
2410  printf(" then the vertex is assigned the marker one (1).\n");
2411  printf(" - Otherwise, the vertex is assigned the marker zero (0).\n");
2412  printf("\n");
2413  printf(
2414 " If you want Triangle to determine for you which vertices and edges are on\n"
2415 );
2416  printf(
2417 " the boundary, assign them the boundary marker zero (or use no markers at\n"
2418 );
2419  printf(
2420 " all) in your input files. In the output files, all boundary vertices,\n");
2421  printf(" edges, and segments will be assigned the value one.\n\n");
2422  printf("Triangulation Iteration Numbers:\n\n");
2423  printf(
2424 " Because Triangle can read and refine its own triangulations, input\n");
2425  printf(
2426 " and output files have iteration numbers. For instance, Triangle might\n");
2427  printf(
2428 " read the files mesh.3.node, mesh.3.ele, and mesh.3.poly, refine the\n");
2429  printf(
2430 " triangulation, and output the files mesh.4.node, mesh.4.ele, and\n");
2431  printf(" mesh.4.poly. Files with no iteration number are treated as if\n");
2432  printf(
2433 " their iteration number is zero; hence, Triangle might read the file\n");
2434  printf(
2435 " points.node, triangulate it, and produce the files points.1.node and\n");
2436  printf(" points.1.ele.\n\n");
2437  printf(
2438 " Iteration numbers allow you to create a sequence of successively finer\n");
2439  printf(
2440 " meshes suitable for multigrid methods. They also allow you to produce a\n"
2441 );
2442  printf(
2443 " sequence of meshes using error estimate-driven mesh refinement.\n");
2444  printf("\n");
2445  printf(
2446 " If you're not using refinement or quality meshing, and you don't like\n");
2447  printf(
2448 " iteration numbers, use the -I switch to disable them. This switch also\n");
2449  printf(
2450 " disables output of .node and .poly files to prevent your input files from\n"
2451 );
2452  printf(
2453 " being overwritten. (If the input is a .poly file that contains its own\n");
2454  printf(
2455 " points, a .node file is written. This can be quite convenient for\n");
2456  printf(" computing CDTs or quality meshes.)\n\n");
2457  printf("Examples of How to Use Triangle:\n\n");
2458  printf(
2459 " `triangle dots' reads vertices from dots.node, and writes their Delaunay\n"
2460 );
2461  printf(
2462 " triangulation to dots.1.node and dots.1.ele. (dots.1.node is identical\n");
2463  printf(
2464 " to dots.node.) `triangle -I dots' writes the triangulation to dots.ele\n");
2465  printf(
2466 " instead. (No additional .node file is needed, so none is written.)\n");
2467  printf("\n");
2468  printf(
2469 " `triangle -pe object.1' reads a PSLG from object.1.poly (and possibly\n");
2470  printf(
2471 " object.1.node, if the vertices are omitted from object.1.poly) and writes\n"
2472 );
2473  printf(
2474 " its constrained Delaunay triangulation to object.2.node and object.2.ele.\n"
2475 );
2476  printf(
2477 " The segments are copied to object.2.poly, and all edges are written to\n");
2478  printf(" object.2.edge.\n\n");
2479  printf(
2480 " `triangle -pq31.5a.1 object' reads a PSLG from object.poly (and possibly\n"
2481 );
2482  printf(
2483 " object.node), generates a mesh whose angles are all between 31.5 and 117\n"
2484 );
2485  printf(
2486 " degrees and whose triangles all have areas of 0.1 or less, and writes the\n"
2487 );
2488  printf(
2489 " mesh to object.1.node and object.1.ele. Each segment may be broken up\n");
2490  printf(" into multiple subsegments; these are written to object.1.poly.\n");
2491  printf("\n");
2492  printf(
2493 " Here is a sample file `box.poly' describing a square with a square hole:\n"
2494 );
2495  printf("\n");
2496  printf(
2497 " # A box with eight vertices in 2D, no attributes, one boundary marker.\n"
2498 );
2499  printf(" 8 2 0 1\n");
2500  printf(" # Outer box has these vertices:\n");
2501  printf(" 1 0 0 0\n");
2502  printf(" 2 0 3 0\n");
2503  printf(" 3 3 0 0\n");
2504  printf(" 4 3 3 33 # A special marker for this vertex.\n");
2505  printf(" # Inner square has these vertices:\n");
2506  printf(" 5 1 1 0\n");
2507  printf(" 6 1 2 0\n");
2508  printf(" 7 2 1 0\n");
2509  printf(" 8 2 2 0\n");
2510  printf(" # Five segments with boundary markers.\n");
2511  printf(" 5 1\n");
2512  printf(" 1 1 2 5 # Left side of outer box.\n");
2513  printf(" # Square hole has these segments:\n");
2514  printf(" 2 5 7 0\n");
2515  printf(" 3 7 8 0\n");
2516  printf(" 4 8 6 10\n");
2517  printf(" 5 6 5 0\n");
2518  printf(" # One hole in the middle of the inner square.\n");
2519  printf(" 1\n");
2520  printf(" 1 1.5 1.5\n");
2521  printf("\n");
2522  printf(
2523 " Note that some segments are missing from the outer square, so you must\n");
2524  printf(
2525 " use the `-c' switch. After `triangle -pqc box.poly', here is the output\n"
2526 );
2527  printf(
2528 " file `box.1.node', with twelve vertices. The last four vertices were\n");
2529  printf(
2530 " added to meet the angle constraint. Vertices 1, 2, and 9 have markers\n");
2531  printf(
2532 " from segment 1. Vertices 6 and 8 have markers from segment 4. All the\n");
2533  printf(
2534 " other vertices but 4 have been marked to indicate that they lie on a\n");
2535  printf(" boundary.\n\n");
2536  printf(" 12 2 0 1\n");
2537  printf(" 1 0 0 5\n");
2538  printf(" 2 0 3 5\n");
2539  printf(" 3 3 0 1\n");
2540  printf(" 4 3 3 33\n");
2541  printf(" 5 1 1 1\n");
2542  printf(" 6 1 2 10\n");
2543  printf(" 7 2 1 1\n");
2544  printf(" 8 2 2 10\n");
2545  printf(" 9 0 1.5 5\n");
2546  printf(" 10 1.5 0 1\n");
2547  printf(" 11 3 1.5 1\n");
2548  printf(" 12 1.5 3 1\n");
2549  printf(" # Generated by triangle -pqc box.poly\n");
2550  printf("\n");
2551  printf(" Here is the output file `box.1.ele', with twelve triangles.\n");
2552  printf("\n");
2553  printf(" 12 3 0\n");
2554  printf(" 1 5 6 9\n");
2555  printf(" 2 10 3 7\n");
2556  printf(" 3 6 8 12\n");
2557  printf(" 4 9 1 5\n");
2558  printf(" 5 6 2 9\n");
2559  printf(" 6 7 3 11\n");
2560  printf(" 7 11 4 8\n");
2561  printf(" 8 7 5 10\n");
2562  printf(" 9 12 2 6\n");
2563  printf(" 10 8 7 11\n");
2564  printf(" 11 5 1 10\n");
2565  printf(" 12 8 4 12\n");
2566  printf(" # Generated by triangle -pqc box.poly\n\n");
2567  printf(
2568 " Here is the output file `box.1.poly'. Note that segments have been added\n"
2569 );
2570  printf(
2571 " to represent the convex hull, and some segments have been subdivided by\n");
2572  printf(
2573 " newly added vertices. Note also that <# of vertices> is set to zero to\n");
2574  printf(" indicate that the vertices should be read from the .node file.\n");
2575  printf("\n");
2576  printf(" 0 2 0 1\n");
2577  printf(" 12 1\n");
2578  printf(" 1 1 9 5\n");
2579  printf(" 2 5 7 1\n");
2580  printf(" 3 8 7 1\n");
2581  printf(" 4 6 8 10\n");
2582  printf(" 5 5 6 1\n");
2583  printf(" 6 3 10 1\n");
2584  printf(" 7 4 11 1\n");
2585  printf(" 8 2 12 1\n");
2586  printf(" 9 9 2 5\n");
2587  printf(" 10 10 1 1\n");
2588  printf(" 11 11 3 1\n");
2589  printf(" 12 12 4 1\n");
2590  printf(" 1\n");
2591  printf(" 1 1.5 1.5\n");
2592  printf(" # Generated by triangle -pqc box.poly\n");
2593  printf("\n");
2594  printf("Refinement and Area Constraints:\n");
2595  printf("\n");
2596  printf(
2597 " The -r switch causes a mesh (.node and .ele files) to be read and\n");
2598  printf(
2599 " refined. If the -p switch is also used, a .poly file is read and used to\n"
2600 );
2601  printf(
2602 " specify edges that are constrained and cannot be eliminated (although\n");
2603  printf(
2604 " they can be subdivided into smaller edges) by the refinement process.\n");
2605  printf("\n");
2606  printf(
2607 " When you refine a mesh, you generally want to impose tighter constraints.\n"
2608 );
2609  printf(
2610 " One way to accomplish this is to use -q with a larger angle, or -a\n");
2611  printf(
2612 " followed by a smaller area than you used to generate the mesh you are\n");
2613  printf(
2614 " refining. Another way to do this is to create an .area file, which\n");
2615  printf(
2616 " specifies a maximum area for each triangle, and use the -a switch\n");
2617  printf(
2618 " (without a number following). Each triangle's area constraint is applied\n"
2619 );
2620  printf(
2621 " to that triangle. Area constraints tend to diffuse as the mesh is\n");
2622  printf(
2623 " refined, so if there are large variations in area constraint between\n");
2624  printf(
2625 " adjacent triangles, you may not get the results you want. In that case,\n"
2626 );
2627  printf(
2628 " consider instead using the -u switch and writing a C procedure that\n");
2629  printf(" determines which triangles are too large.\n\n");
2630  printf(
2631 " If you are refining a mesh composed of linear (three-node) elements, the\n"
2632 );
2633  printf(
2634 " output mesh contains all the nodes present in the input mesh, in the same\n"
2635 );
2636  printf(
2637 " order, with new nodes added at the end of the .node file. However, the\n");
2638  printf(
2639 " refinement is not hierarchical: there is no guarantee that each output\n");
2640  printf(
2641 " element is contained in a single input element. Often, an output element\n"
2642 );
2643  printf(
2644 " can overlap two or three input elements, and some input edges are not\n");
2645  printf(
2646 " present in the output mesh. Hence, a sequence of refined meshes forms a\n"
2647 );
2648  printf(
2649 " hierarchy of nodes, but not a hierarchy of elements. If you refine a\n");
2650  printf(
2651 " mesh of higher-order elements, the hierarchical property applies only to\n"
2652 );
2653  printf(
2654 " the nodes at the corners of an element; the midpoint nodes on each edge\n");
2655  printf(" are discarded before the mesh is refined.\n\n");
2656  printf(
2657 " Maximum area constraints in .poly files operate differently from those in\n"
2658 );
2659  printf(
2660 " .area files. A maximum area in a .poly file applies to the whole\n");
2661  printf(
2662 " (segment-bounded) region in which a point falls, whereas a maximum area\n");
2663  printf(
2664 " in an .area file applies to only one triangle. Area constraints in .poly\n"
2665 );
2666  printf(
2667 " files are used only when a mesh is first generated, whereas area\n");
2668  printf(
2669 " constraints in .area files are used only to refine an existing mesh, and\n"
2670 );
2671  printf(
2672 " are typically based on a posteriori error estimates resulting from a\n");
2673  printf(" finite element simulation on that mesh.\n\n");
2674  printf(
2675 " `triangle -rq25 object.1' reads object.1.node and object.1.ele, then\n");
2676  printf(
2677 " refines the triangulation to enforce a 25 degree minimum angle, and then\n"
2678 );
2679  printf(
2680 " writes the refined triangulation to object.2.node and object.2.ele.\n");
2681  printf("\n");
2682  printf(
2683 " `triangle -rpaa6.2 z.3' reads z.3.node, z.3.ele, z.3.poly, and z.3.area.\n"
2684 );
2685  printf(
2686 " After reconstructing the mesh and its subsegments, Triangle refines the\n");
2687  printf(
2688 " mesh so that no triangle has area greater than 6.2, and furthermore the\n");
2689  printf(
2690 " triangles satisfy the maximum area constraints in z.3.area. No angle\n");
2691  printf(
2692 " bound is imposed at all. The output is written to z.4.node, z.4.ele, and\n"
2693 );
2694  printf(" z.4.poly.\n\n");
2695  printf(
2696 " The sequence `triangle -qa1 x', `triangle -rqa.3 x.1', `triangle -rqa.1\n");
2697  printf(
2698 " x.2' creates a sequence of successively finer meshes x.1, x.2, and x.3,\n");
2699  printf(" suitable for multigrid.\n\n");
2700  printf("Convex Hulls and Mesh Boundaries:\n\n");
2701  printf(
2702 " If the input is a vertex set (not a PSLG), Triangle produces its convex\n");
2703  printf(
2704 " hull as a by-product in the output .poly file if you use the -c switch.\n");
2705  printf(
2706 " There are faster algorithms for finding a two-dimensional convex hull\n");
2707  printf(" than triangulation, of course, but this one comes for free.\n\n");
2708  printf(
2709 " If the input is an unconstrained mesh (you are using the -r switch but\n");
2710  printf(
2711 " not the -p switch), Triangle produces a list of its boundary edges\n");
2712  printf(
2713 " (including hole boundaries) as a by-product when you use the -c switch.\n");
2714  printf(
2715 " If you also use the -p switch, the output .poly file contains all the\n");
2716  printf(" segments from the input .poly file as well.\n\n");
2717  printf("Voronoi Diagrams:\n\n");
2718  printf(
2719 " The -v switch produces a Voronoi diagram, in files suffixed .v.node and\n");
2720  printf(
2721 " .v.edge. For example, `triangle -v points' reads points.node, produces\n");
2722  printf(
2723 " its Delaunay triangulation in points.1.node and points.1.ele, and\n");
2724  printf(
2725 " produces its Voronoi diagram in points.1.v.node and points.1.v.edge. The\n"
2726 );
2727  printf(
2728 " .v.node file contains a list of all Voronoi vertices, and the .v.edge\n");
2729  printf(
2730 " file contains a list of all Voronoi edges, some of which may be infinite\n"
2731 );
2732  printf(
2733 " rays. (The choice of filenames makes it easy to run the set of Voronoi\n");
2734  printf(" vertices through Triangle, if so desired.)\n\n");
2735  printf(
2736 " This implementation does not use exact arithmetic to compute the Voronoi\n"
2737 );
2738  printf(
2739 " vertices, and does not check whether neighboring vertices are identical.\n"
2740 );
2741  printf(
2742 " Be forewarned that if the Delaunay triangulation is degenerate or\n");
2743  printf(
2744 " near-degenerate, the Voronoi diagram may have duplicate vertices or\n");
2745  printf(" crossing edges.\n\n");
2746  printf(
2747 " The result is a valid Voronoi diagram only if Triangle's output is a true\n"
2748 );
2749  printf(
2750 " Delaunay triangulation. The Voronoi output is usually meaningless (and\n");
2751  printf(
2752 " may contain crossing edges and other pathology) if the output is a CDT or\n"
2753 );
2754  printf(
2755 " CCDT, or if it has holes or concavities. If the triangulated domain is\n");
2756  printf(
2757 " convex and has no holes, you can use -D switch to force Triangle to\n");
2758  printf(
2759 " construct a conforming Delaunay triangulation instead of a CCDT, so the\n");
2760  printf(" Voronoi diagram will be valid.\n\n");
2761  printf("Mesh Topology:\n\n");
2762  printf(
2763 " You may wish to know which triangles are adjacent to a certain Delaunay\n");
2764  printf(
2765 " edge in an .edge file, which Voronoi cells are adjacent to a certain\n");
2766  printf(
2767 " Voronoi edge in a .v.edge file, or which Voronoi cells are adjacent to\n");
2768  printf(
2769 " each other. All of this information can be found by cross-referencing\n");
2770  printf(
2771 " output files with the recollection that the Delaunay triangulation and\n");
2772  printf(" the Voronoi diagram are planar duals.\n\n");
2773  printf(
2774 " Specifically, edge i of an .edge file is the dual of Voronoi edge i of\n");
2775  printf(
2776 " the corresponding .v.edge file, and is rotated 90 degrees counterclock-\n");
2777  printf(
2778 " wise from the Voronoi edge. Triangle j of an .ele file is the dual of\n");
2779  printf(
2780 " vertex j of the corresponding .v.node file. Voronoi cell k is the dual\n");
2781  printf(" of vertex k of the corresponding .node file.\n\n");
2782  printf(
2783 " Hence, to find the triangles adjacent to a Delaunay edge, look at the\n");
2784  printf(
2785 " vertices of the corresponding Voronoi edge. If the endpoints of a\n");
2786  printf(
2787 " Voronoi edge are Voronoi vertices 2 and 6 respectively, then triangles 2\n"
2788 );
2789  printf(
2790 " and 6 adjoin the left and right sides of the corresponding Delaunay edge,\n"
2791 );
2792  printf(
2793 " respectively. To find the Voronoi cells adjacent to a Voronoi edge, look\n"
2794 );
2795  printf(
2796 " at the endpoints of the corresponding Delaunay edge. If the endpoints of\n"
2797 );
2798  printf(
2799 " a Delaunay edge are input vertices 7 and 12, then Voronoi cells 7 and 12\n"
2800 );
2801  printf(
2802 " adjoin the right and left sides of the corresponding Voronoi edge,\n");
2803  printf(
2804 " respectively. To find which Voronoi cells are adjacent to each other,\n");
2805  printf(" just read the list of Delaunay edges.\n\n");
2806  printf(
2807 " Triangle does not write a list of the edges adjoining each Voronoi cell,\n"
2808 );
2809  printf(
2810 " but you can reconstructed it straightforwardly. For instance, to find\n");
2811  printf(
2812 " all the edges of Voronoi cell 1, search the output .edge file for every\n");
2813  printf(
2814 " edge that has input vertex 1 as an endpoint. The corresponding dual\n");
2815  printf(
2816 " edges in the output .v.edge file form the boundary of Voronoi cell 1.\n");
2817  printf("\n");
2818  printf(
2819 " For each Voronoi vertex, the .neigh file gives a list of the three\n");
2820  printf(
2821 " Voronoi vertices attached to it. You might find this more convenient\n");
2822  printf(" than the .v.edge file.\n\n");
2823  printf("Quadratic Elements:\n\n");
2824  printf(
2825 " Triangle generates meshes with subparametric quadratic elements if the\n");
2826  printf(
2827 " -o2 switch is specified. Quadratic elements have six nodes per element,\n"
2828 );
2829  printf(
2830 " rather than three. `Subparametric' means that the edges of the triangles\n"
2831 );
2832  printf(
2833 " are always straight, so that subparametric quadratic elements are\n");
2834  printf(
2835 " geometrically identical to linear elements, even though they can be used\n"
2836 );
2837  printf(
2838 " with quadratic interpolating functions. The three extra nodes of an\n");
2839  printf(
2840 " element fall at the midpoints of the three edges, with the fourth, fifth,\n"
2841 );
2842  printf(
2843 " and sixth nodes appearing opposite the first, second, and third corners\n");
2844  printf(" respectively.\n\n");
2845  printf("Domains with Small Angles:\n\n");
2846  printf(
2847 " If two input segments adjoin each other at a small angle, clearly the -q\n"
2848 );
2849  printf(
2850 " switch cannot remove the small angle. Moreover, Triangle may have no\n");
2851  printf(
2852 " choice but to generate additional triangles whose smallest angles are\n");
2853  printf(
2854 " smaller than the specified bound. However, these triangles only appear\n");
2855  printf(
2856 " between input segments separated by small angles. Moreover, if you\n");
2857  printf(
2858 " request a minimum angle of theta degrees, Triangle will generally produce\n"
2859 );
2860  printf(
2861 " no angle larger than 180 - 2 theta, even if it is forced to compromise on\n"
2862 );
2863  printf(" the minimum angle.\n\n");
2864  printf("Statistics:\n\n");
2865  printf(
2866 " After generating a mesh, Triangle prints a count of entities in the\n");
2867  printf(
2868 " output mesh, including the number of vertices, triangles, edges, exterior\n"
2869 );
2870  printf(
2871 " boundary edges (i.e. subsegments on the boundary of the triangulation,\n");
2872  printf(
2873 " including hole boundaries), interior boundary edges (i.e. subsegments of\n"
2874 );
2875  printf(
2876 " input segments not on the boundary), and total subsegments. If you've\n");
2877  printf(
2878 " forgotten the statistics for an existing mesh, run Triangle on that mesh\n"
2879 );
2880  printf(
2881 " with the -rNEP switches to read the mesh and print the statistics without\n"
2882 );
2883  printf(
2884 " writing any files. Use -rpNEP if you've got a .poly file for the mesh.\n");
2885  printf("\n");
2886  printf(
2887 " The -V switch produces extended statistics, including a rough estimate\n");
2888  printf(
2889 " of memory use, the number of calls to geometric predicates, and\n");
2890  printf(
2891 " histograms of the angles and the aspect ratios of the triangles in the\n");
2892  printf(" mesh.\n\n");
2893  printf("Exact Arithmetic:\n\n");
2894  printf(
2895 " Triangle uses adaptive exact arithmetic to perform what computational\n");
2896  printf(
2897 " geometers call the `orientation' and `incircle' tests. If the floating-\n"
2898 );
2899  printf(
2900 " point arithmetic of your machine conforms to the IEEE 754 standard (as\n");
2901  printf(
2902 " most workstations do), and does not use extended precision internal\n");
2903  printf(
2904 " floating-point registers, then your output is guaranteed to be an\n");
2905  printf(
2906 " absolutely true Delaunay or constrained Delaunay triangulation, roundoff\n"
2907 );
2908  printf(
2909 " error notwithstanding. The word `adaptive' implies that these arithmetic\n"
2910 );
2911  printf(
2912 " routines compute the result only to the precision necessary to guarantee\n"
2913 );
2914  printf(
2915 " correctness, so they are usually nearly as fast as their approximate\n");
2916  printf(" counterparts.\n\n");
2917  printf(
2918 " May CPUs, including Intel x86 processors, have extended precision\n");
2919  printf(
2920 " floating-point registers. These must be reconfigured so their precision\n"
2921 );
2922  printf(
2923 " is reduced to memory precision. Triangle does this if it is compiled\n");
2924  printf(" correctly. See the makefile for details.\n\n");
2925  printf(
2926 " The exact tests can be disabled with the -X switch. On most inputs, this\n"
2927 );
2928  printf(
2929 " switch reduces the computation time by about eight percent--it's not\n");
2930  printf(
2931 " worth the risk. There are rare difficult inputs (having many collinear\n");
2932  printf(
2933 " and cocircular vertices), however, for which the difference in speed\n");
2934  printf(
2935 " could be a factor of two. Be forewarned that these are precisely the\n");
2936  printf(
2937 " inputs most likely to cause errors if you use the -X switch. Hence, the\n"
2938 );
2939  printf(" -X switch is not recommended.\n\n");
2940  printf(
2941 " Unfortunately, the exact tests don't solve every numerical problem.\n");
2942  printf(
2943 " Exact arithmetic is not used to compute the positions of new vertices,\n");
2944  printf(
2945 " because the bit complexity of vertex coordinates would grow without\n");
2946  printf(
2947 " bound. Hence, segment intersections aren't computed exactly; in very\n");
2948  printf(
2949 " unusual cases, roundoff error in computing an intersection point might\n");
2950  printf(
2951 " actually lead to an inverted triangle and an invalid triangulation.\n");
2952  printf(
2953 " (This is one reason to specify your own intersection points in your .poly\n"
2954 );
2955  printf(
2956 " files.) Similarly, exact arithmetic is not used to compute the vertices\n"
2957 );
2958  printf(" of the Voronoi diagram.\n\n");
2959  printf(
2960 " Another pair of problems not solved by the exact arithmetic routines is\n");
2961  printf(
2962 " underflow and overflow. If Triangle is compiled for double precision\n");
2963  printf(
2964 " arithmetic, I believe that Triangle's geometric predicates work correctly\n"
2965 );
2966  printf(
2967 " if the exponent of every input coordinate falls in the range [-148, 201].\n"
2968 );
2969  printf(
2970 " Underflow can silently prevent the orientation and incircle tests from\n");
2971  printf(
2972 " being performed exactly, while overflow typically causes a floating\n");
2973  printf(" exception.\n\n");
2974  printf("Calling Triangle from Another Program:\n\n");
2975  printf(" Read the file triangle.h for details.\n\n");
2976  printf("Troubleshooting:\n\n");
2977  printf(" Please read this section before mailing me bugs.\n\n");
2978  printf(" `My output mesh has no triangles!'\n\n");
2979  printf(
2980 " If you're using a PSLG, you've probably failed to specify a proper set\n"
2981 );
2982  printf(
2983 " of bounding segments, or forgotten to use the -c switch. Or you may\n");
2984  printf(
2985 " have placed a hole badly, thereby eating all your triangles. To test\n");
2986  printf(" these possibilities, try again with the -c and -O switches.\n");
2987  printf(
2988 " Alternatively, all your input vertices may be collinear, in which case\n"
2989 );
2990  printf(" you can hardly expect to triangulate them.\n\n");
2991  printf(" `Triangle doesn't terminate, or just crashes.'\n\n");
2992  printf(
2993 " Bad things can happen when triangles get so small that the distance\n");
2994  printf(
2995 " between their vertices isn't much larger than the precision of your\n");
2996  printf(
2997 " machine's arithmetic. If you've compiled Triangle for single-precision\n"
2998 );
2999  printf(
3000 " arithmetic, you might do better by recompiling it for double-precision.\n"
3001 );
3002  printf(
3003 " Then again, you might just have to settle for more lenient constraints\n"
3004 );
3005  printf(
3006 " on the minimum angle and the maximum area than you had planned.\n");
3007  printf("\n");
3008  printf(
3009 " You can minimize precision problems by ensuring that the origin lies\n");
3010  printf(
3011 " inside your vertex set, or even inside the densest part of your\n");
3012  printf(
3013 " mesh. If you're triangulating an object whose x-coordinates all fall\n");
3014  printf(
3015 " between 6247133 and 6247134, you're not leaving much floating-point\n");
3016  printf(" precision for Triangle to work with.\n\n");
3017  printf(
3018 " Precision problems can occur covertly if the input PSLG contains two\n");
3019  printf(
3020 " segments that meet (or intersect) at an extremely small angle, or if\n");
3021  printf(
3022 " such an angle is introduced by the -c switch. If you don't realize\n");
3023  printf(
3024 " that a tiny angle is being formed, you might never discover why\n");
3025  printf(
3026 " Triangle is crashing. To check for this possibility, use the -S switch\n"
3027 );
3028  printf(
3029 " (with an appropriate limit on the number of Steiner points, found by\n");
3030  printf(
3031 " trial-and-error) to stop Triangle early, and view the output .poly file\n"
3032 );
3033  printf(
3034 " with Show Me (described below). Look carefully for regions where dense\n"
3035 );
3036  printf(
3037 " clusters of vertices are forming and for small angles between segments.\n"
3038 );
3039  printf(
3040 " Zoom in closely, as such segments might look like a single segment from\n"
3041 );
3042  printf(" a distance.\n\n");
3043  printf(
3044 " If some of the input values are too large, Triangle may suffer a\n");
3045  printf(
3046 " floating exception due to overflow when attempting to perform an\n");
3047  printf(
3048 " orientation or incircle test. (Read the section on exact arithmetic\n");
3049  printf(
3050 " above.) Again, I recommend compiling Triangle for double (rather\n");
3051  printf(" than single) precision arithmetic.\n\n");
3052  printf(
3053 " Unexpected problems can arise if you use quality meshing (-q, -a, or\n");
3054  printf(
3055 " -u) with an input that is not segment-bounded--that is, if your input\n");
3056  printf(
3057 " is a vertex set, or you're using the -c switch. If the convex hull of\n"
3058 );
3059  printf(
3060 " your input vertices has collinear vertices on its boundary, an input\n");
3061  printf(
3062 " vertex that you think lies on the convex hull might actually lie just\n");
3063  printf(
3064 " inside the convex hull. If so, the vertex and the nearby convex hull\n");
3065  printf(
3066 " edge form an extremely thin triangle. When Triangle tries to refine\n");
3067  printf(
3068 " the mesh to enforce angle and area constraints, Triangle might generate\n"
3069 );
3070  printf(
3071 " extremely tiny triangles, or it might fail because of insufficient\n");
3072  printf(" floating-point precision.\n\n");
3073  printf(
3074 " `The numbering of the output vertices doesn't match the input vertices.'\n"
3075 );
3076  printf("\n");
3077  printf(
3078 " You may have had duplicate input vertices, or you may have eaten some\n");
3079  printf(
3080 " of your input vertices with a hole, or by placing them outside the area\n"
3081 );
3082  printf(
3083 " enclosed by segments. In any case, you can solve the problem by not\n");
3084  printf(" using the -j switch.\n\n");
3085  printf(
3086 " `Triangle executes without incident, but when I look at the resulting\n");
3087  printf(
3088 " mesh, it has overlapping triangles or other geometric inconsistencies.'\n");
3089  printf("\n");
3090  printf(
3091 " If you select the -X switch, Triangle occasionally makes mistakes due\n");
3092  printf(
3093 " to floating-point roundoff error. Although these errors are rare,\n");
3094  printf(
3095 " don't use the -X switch. If you still have problems, please report the\n"
3096 );
3097  printf(" bug.\n\n");
3098  printf(
3099 " `Triangle executes without incident, but when I look at the resulting\n");
3100  printf(" Voronoi diagram, it has overlapping edges or other geometric\n");
3101  printf(" inconsistencies.'\n");
3102  printf("\n");
3103  printf(
3104 " If your input is a PSLG (-p), you can only expect a meaningful Voronoi\n"
3105 );
3106  printf(
3107 " diagram if the domain you are triangulating is convex and free of\n");
3108  printf(
3109 " holes, and you use the -D switch to construct a conforming Delaunay\n");
3110  printf(" triangulation (instead of a CDT or CCDT).\n\n");
3111  printf(
3112 " Strange things can happen if you've taken liberties with your PSLG. Do\n");
3113  printf(
3114 " you have a vertex lying in the middle of a segment? Triangle sometimes\n");
3115  printf(
3116 " copes poorly with that sort of thing. Do you want to lay out a collinear\n"
3117 );
3118  printf(
3119 " row of evenly spaced, segment-connected vertices? Have you simply\n");
3120  printf(
3121 " defined one long segment connecting the leftmost vertex to the rightmost\n"
3122 );
3123  printf(
3124 " vertex, and a bunch of vertices lying along it? This method occasionally\n"
3125 );
3126  printf(
3127 " works, especially with horizontal and vertical lines, but often it\n");
3128  printf(
3129 " doesn't, and you'll have to connect each adjacent pair of vertices with a\n"
3130 );
3131  printf(" separate segment. If you don't like it, tough.\n\n");
3132  printf(
3133 " Furthermore, if you have segments that intersect other than at their\n");
3134  printf(
3135 " endpoints, try not to let the intersections fall extremely close to PSLG\n"
3136 );
3137  printf(" vertices or each other.\n\n");
3138  printf(
3139 " If you have problems refining a triangulation not produced by Triangle:\n");
3140  printf(
3141 " Are you sure the triangulation is geometrically valid? Is it formatted\n");
3142  printf(
3143 " correctly for Triangle? Are the triangles all listed so the first three\n"
3144 );
3145  printf(
3146 " vertices are their corners in counterclockwise order? Are all of the\n");
3147  printf(
3148 " triangles constrained Delaunay? Triangle's Delaunay refinement algorithm\n"
3149 );
3150  printf(" assumes that it starts with a CDT.\n\n");
3151  printf("Show Me:\n\n");
3152  printf(
3153 " Triangle comes with a separate program named `Show Me', whose primary\n");
3154  printf(
3155 " purpose is to draw meshes on your screen or in PostScript. Its secondary\n"
3156 );
3157  printf(
3158 " purpose is to check the validity of your input files, and do so more\n");
3159  printf(
3160 " thoroughly than Triangle does. Unlike Triangle, Show Me requires that\n");
3161  printf(
3162 " you have the X Windows system. Sorry, Microsoft Windows users.\n");
3163  printf("\n");
3164  printf("Triangle on the Web:\n");
3165  printf("\n");
3166  printf(" To see an illustrated version of these instructions, check out\n");
3167  printf("\n");
3168  printf(" http://www.cs.cmu.edu/~quake/triangle.html\n");
3169  printf("\n");
3170  printf("A Brief Plea:\n");
3171  printf("\n");
3172  printf(
3173 " If you use Triangle, and especially if you use it to accomplish real\n");
3174  printf(
3175 " work, I would like very much to hear from you. A short letter or email\n");
3176  printf(
3177 " (to jrs@cs.berkeley.edu) describing how you use Triangle will mean a lot\n"
3178 );
3179  printf(
3180 " to me. The more people I know are using this program, the more easily I\n"
3181 );
3182  printf(
3183 " can justify spending time on improvements, which in turn will benefit\n");
3184  printf(
3185 " you. Also, I can put you on a list to receive email whenever a new\n");
3186  printf(" version of Triangle is available.\n\n");
3187  printf(
3188 " If you use a mesh generated by Triangle in a publication, please include\n"
3189 );
3190  printf(
3191 " an acknowledgment as well. And please spell Triangle with a capital `T'!\n"
3192 );
3193  printf(
3194 " If you want to include a citation, use `Jonathan Richard Shewchuk,\n");
3195  printf(
3196 " ``Triangle: Engineering a 2D Quality Mesh Generator and Delaunay\n");
3197  printf(
3198 " Triangulator,'' in Applied Computational Geometry: Towards Geometric\n");
3199  printf(
3200 " Engineering (Ming C. Lin and Dinesh Manocha, editors), volume 1148 of\n");
3201  printf(
3202 " Lecture Notes in Computer Science, pages 203-222, Springer-Verlag,\n");
3203  printf(
3204 " Berlin, May 1996. (From the First ACM Workshop on Applied Computational\n"
3205 );
3206  printf(" Geometry.)'\n\n");
3207  printf("Research credit:\n\n");
3208  printf(
3209 " Of course, I can take credit for only a fraction of the ideas that made\n");
3210  printf(
3211 " this mesh generator possible. Triangle owes its existence to the efforts\n"
3212 );
3213  printf(
3214 " of many fine computational geometers and other researchers, including\n");
3215  printf(
3216 " Marshall Bern, L. Paul Chew, Kenneth L. Clarkson, Boris Delaunay, Rex A.\n"
3217 );
3218  printf(
3219 " Dwyer, David Eppstein, Steven Fortune, Leonidas J. Guibas, Donald E.\n");
3220  printf(
3221 " Knuth, Charles L. Lawson, Der-Tsai Lee, Gary L. Miller, Ernst P. Mucke,\n");
3222  printf(
3223 " Steven E. Pav, Douglas M. Priest, Jim Ruppert, Isaac Saias, Bruce J.\n");
3224  printf(
3225 " Schachter, Micha Sharir, Peter W. Shor, Daniel D. Sleator, Jorge Stolfi,\n"
3226 );
3227  printf(" Robert E. Tarjan, Alper Ungor, Christopher J. Van Wyk, Noel J.\n");
3228  printf(
3229 " Walkington, and Binhai Zhu. See the comments at the beginning of the\n");
3230  printf(" source code for references.\n\n");
3231  triexit(0);
3232 }
3233 
3234 #endif /* not TRILIBRARY */
3235 
3236 /*****************************************************************************/
3237 /* */
3238 /* internalerror() Ask the user to send me the defective product. Exit. */
3239 /* */
3240 /*****************************************************************************/
3241 
3243 {
3244  printf(" Please report this bug to jrs@cs.berkeley.edu\n");
3245  printf(" Include the message above, your input data set, and the exact\n");
3246  printf(" command line you used to run Triangle.\n");
3247  triexit(1);
3248 }
3249 
3250 /*****************************************************************************/
3251 /* */
3252 /* parsecommandline() Read the command line, identify switches, and set */
3253 /* up options and file names. */
3254 /* */
3255 /*****************************************************************************/
3256 
3257 #ifdef ANSI_DECLARATORS
3258 void parsecommandline(int argc, char **argv, struct behavior *b)
3259 #else /* not ANSI_DECLARATORS */
3260 void parsecommandline(argc, argv, b)
3261 int argc;
3262 char **argv;
3263 struct behavior *b;
3264 #endif /* not ANSI_DECLARATORS */
3265 
3266 {
3267 #ifdef TRILIBRARY
3268 #define STARTINDEX 0
3269 #else /* not TRILIBRARY */
3270 #define STARTINDEX 1
3271  int increment;
3272  int meshnumber;
3273 #endif /* not TRILIBRARY */
3274  /* int i, j, k; */
3275  int i, j;
3276  /* char workstring[FILENAMESIZE]; */
3277 
3278  b->poly = b->refine = b->quality = 0;
3279  b->vararea = b->fixedarea = b->usertest = 0;
3280  b->regionattrib = b->convex = b->weighted = b->jettison = 0;
3281  b->firstnumber = 1;
3282  b->edgesout = b->voronoi = b->neighbors = b->geomview = 0;
3283  b->nobound = b->nopolywritten = b->nonodewritten = b->noelewritten = 0;
3284  b->noiterationnum = 0;
3285  b->noholes = b->noexact = 0;
3286  b->incremental = b->sweepline = 0;
3287  b->dwyer = 1;
3288  b->splitseg = 0;
3289  b->docheck = 0;
3290  b->nobisect = 0;
3291  b->conformdel = 0;
3292  b->steiner = -1;
3293  b->order = 1;
3294  b->minangle = 0.0;
3295  b->maxarea = -1.0;
3296  b->quiet = b->verbose = 0;
3297 #ifndef TRILIBRARY
3298  b->innodefilename[0] = '\0';
3299 #endif /* not TRILIBRARY */
3300 
3301  for (i = STARTINDEX; i < argc; i++) {
3302 #ifndef TRILIBRARY
3303  if (argv[i][0] == '-') {
3304 #endif /* not TRILIBRARY */
3305  for (j = STARTINDEX; argv[i][j] != '\0'; j++) {
3306  if (argv[i][j] == 'p') {
3307  b->poly = 1;
3308  }
3309 #ifndef CDT_ONLY
3310  if (argv[i][j] == 'r') {
3311  b->refine = 1;
3312  }
3313  if (argv[i][j] == 'q') {
3314  b->quality = 1;
3315  if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
3316  (argv[i][j + 1] == '.')) {
3317  k = 0;
3318  while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
3319  (argv[i][j + 1] == '.')) {
3320  j++;
3321  workstring[k] = argv[i][j];
3322  k++;
3323  }
3324  workstring[k] = '\0';
3325  b->minangle = (REAL) strtod(workstring, (char **) NULL);
3326  } else {
3327  b->minangle = 20.0;
3328  }
3329  }
3330  if (argv[i][j] == 'a') {
3331  b->quality = 1;
3332  if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
3333  (argv[i][j + 1] == '.')) {
3334  b->fixedarea = 1;
3335  k = 0;
3336  while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
3337  (argv[i][j + 1] == '.')) {
3338  j++;
3339  workstring[k] = argv[i][j];
3340  k++;
3341  }
3342  workstring[k] = '\0';
3343  b->maxarea = (REAL) strtod(workstring, (char **) NULL);
3344  if (b->maxarea <= 0.0) {
3345  printf("Error: Maximum area must be greater than zero.\n");
3346  triexit(1);
3347  }
3348  } else {
3349  b->vararea = 1;
3350  }
3351  }
3352  if (argv[i][j] == 'u') {
3353  b->quality = 1;
3354  b->usertest = 1;
3355  }
3356 #endif /* not CDT_ONLY */
3357  if (argv[i][j] == 'A') {
3358  b->regionattrib = 1;
3359  }
3360  if (argv[i][j] == 'c') {
3361  b->convex = 1;
3362  }
3363  if (argv[i][j] == 'w') {
3364  b->weighted = 1;
3365  }
3366  if (argv[i][j] == 'W') {
3367  b->weighted = 2;
3368  }
3369  if (argv[i][j] == 'j') {
3370  b->jettison = 1;
3371  }
3372  if (argv[i][j] == 'z') {
3373  b->firstnumber = 0;
3374  }
3375  if (argv[i][j] == 'e') {
3376  b->edgesout = 1;
3377  }
3378  if (argv[i][j] == 'v') {
3379  b->voronoi = 1;
3380  }
3381  if (argv[i][j] == 'n') {
3382  b->neighbors = 1;
3383  }
3384  if (argv[i][j] == 'g') {
3385  b->geomview = 1;
3386  }
3387  if (argv[i][j] == 'B') {
3388  b->nobound = 1;
3389  }
3390  if (argv[i][j] == 'P') {
3391  b->nopolywritten = 1;
3392  }
3393  if (argv[i][j] == 'N') {
3394  b->nonodewritten = 1;
3395  }
3396  if (argv[i][j] == 'E') {
3397  b->noelewritten = 1;
3398  }
3399 #ifndef TRILIBRARY
3400  if (argv[i][j] == 'I') {
3401  b->noiterationnum = 1;
3402  }
3403 #endif /* not TRILIBRARY */
3404  if (argv[i][j] == 'O') {
3405  b->noholes = 1;
3406  }
3407  if (argv[i][j] == 'X') {
3408  b->noexact = 1;
3409  }
3410  if (argv[i][j] == 'o') {
3411  if (argv[i][j + 1] == '2') {
3412  j++;
3413  b->order = 2;
3414  }
3415  }
3416 #ifndef CDT_ONLY
3417  if (argv[i][j] == 'Y') {
3418  b->nobisect++;
3419  }
3420  if (argv[i][j] == 'S') {
3421  b->steiner = 0;
3422  while ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) {
3423  j++;
3424  b->steiner = b->steiner * 10 + (int) (argv[i][j] - '0');
3425  }
3426  }
3427 #endif /* not CDT_ONLY */
3428 #ifndef REDUCED
3429  if (argv[i][j] == 'i') {
3430  b->incremental = 1;
3431  }
3432  if (argv[i][j] == 'F') {
3433  b->sweepline = 1;
3434  }
3435 #endif /* not REDUCED */
3436  if (argv[i][j] == 'l') {
3437  b->dwyer = 0;
3438  }
3439 #ifndef REDUCED
3440 #ifndef CDT_ONLY
3441  if (argv[i][j] == 's') {
3442  b->splitseg = 1;
3443  }
3444  if ((argv[i][j] == 'D') || (argv[i][j] == 'L')) {
3445  b->quality = 1;
3446  b->conformdel = 1;
3447  }
3448 #endif /* not CDT_ONLY */
3449  if (argv[i][j] == 'C') {
3450  b->docheck = 1;
3451  }
3452 #endif /* not REDUCED */
3453  if (argv[i][j] == 'Q') {
3454  b->quiet = 1;
3455  }
3456  if (argv[i][j] == 'V') {
3457  b->verbose++;
3458  }
3459 #ifndef TRILIBRARY
3460  if ((argv[i][j] == 'h') || (argv[i][j] == 'H') ||
3461  (argv[i][j] == '?')) {
3462  info();
3463  }
3464 #endif /* not TRILIBRARY */
3465  }
3466 #ifndef TRILIBRARY
3467  } else {
3468  strncpy(b->innodefilename, argv[i], FILENAMESIZE - 1);
3469  b->innodefilename[FILENAMESIZE - 1] = '\0';
3470  }
3471 #endif /* not TRILIBRARY */
3472  }
3473 #ifndef TRILIBRARY
3474  if (b->innodefilename[0] == '\0') {
3475  syntax();
3476  }
3477  if (!strcmp(&b->innodefilename[strlen(b->innodefilename) - 5], ".node")) {
3478  b->innodefilename[strlen(b->innodefilename) - 5] = '\0';
3479  }
3480  if (!strcmp(&b->innodefilename[strlen(b->innodefilename) - 5], ".poly")) {
3481  b->innodefilename[strlen(b->innodefilename) - 5] = '\0';
3482  b->poly = 1;
3483  }
3484 #ifndef CDT_ONLY
3485  if (!strcmp(&b->innodefilename[strlen(b->innodefilename) - 4], ".ele")) {
3486  b->innodefilename[strlen(b->innodefilename) - 4] = '\0';
3487  b->refine = 1;
3488  }
3489  if (!strcmp(&b->innodefilename[strlen(b->innodefilename) - 5], ".area")) {
3490  b->innodefilename[strlen(b->innodefilename) - 5] = '\0';
3491  b->refine = 1;
3492  b->quality = 1;
3493  b->vararea = 1;
3494  }
3495 #endif /* not CDT_ONLY */
3496 #endif /* not TRILIBRARY */
3497  b->usesegments = b->poly || b->refine || b->quality || b->convex;
3498  b->goodangle = cos(b->minangle * PI / 180.0);
3499  if (b->goodangle == 1.0) {
3500  b->offconstant = 0.0;
3501  } else {
3502  b->offconstant = 0.475 * sqrt((1.0 + b->goodangle) / (1.0 - b->goodangle));
3503  }
3504  b->goodangle *= b->goodangle;
3505  if (b->refine && b->noiterationnum) {
3506  printf(
3507  "Error: You cannot use the -I switch when refining a triangulation.\n");
3508  triexit(1);
3509  }
3510  /* Be careful not to allocate space for element area constraints that */
3511  /* will never be assigned any value (other than the default -1.0). */
3512  if (!b->refine && !b->poly) {
3513  b->vararea = 0;
3514  }
3515  /* Be careful not to add an extra attribute to each element unless the */
3516  /* input supports it (PSLG in, but not refining a preexisting mesh). */
3517  if (b->refine || !b->poly) {
3518  b->regionattrib = 0;
3519  }
3520  /* Regular/weighted triangulations are incompatible with PSLGs */
3521  /* and meshing. */
3522  if (b->weighted && (b->poly || b->quality)) {
3523  b->weighted = 0;
3524  if (!b->quiet) {
3525  printf("Warning: weighted triangulations (-w, -W) are incompatible\n");
3526  printf(" with PSLGs (-p) and meshing (-q, -a, -u). Weights ignored.\n"
3527  );
3528  }
3529  }
3530  if (b->jettison && b->nonodewritten && !b->quiet) {
3531  printf("Warning: -j and -N switches are somewhat incompatible.\n");
3532  printf(" If any vertices are jettisoned, you will need the output\n");
3533  printf(" .node file to reconstruct the new node indices.");
3534  }
3535 
3536 #ifndef TRILIBRARY
3537  strcpy(b->inpolyfilename, b->innodefilename);
3538  strcpy(b->inelefilename, b->innodefilename);
3539  strcpy(b->areafilename, b->innodefilename);
3540  increment = 0;
3541  strcpy(workstring, b->innodefilename);
3542  j = 1;
3543  while (workstring[j] != '\0') {
3544  if ((workstring[j] == '.') && (workstring[j + 1] != '\0')) {
3545  increment = j + 1;
3546  }
3547  j++;
3548  }
3549  meshnumber = 0;
3550  if (increment > 0) {
3551  j = increment;
3552  do {
3553  if ((workstring[j] >= '0') && (workstring[j] <= '9')) {
3554  meshnumber = meshnumber * 10 + (int) (workstring[j] - '0');
3555  } else {
3556  increment = 0;
3557  }
3558  j++;
3559  } while (workstring[j] != '\0');
3560  }
3561  if (b->noiterationnum) {
3562  strcpy(b->outnodefilename, b->innodefilename);
3563  strcpy(b->outelefilename, b->innodefilename);
3564  strcpy(b->edgefilename, b->innodefilename);
3565  strcpy(b->vnodefilename, b->innodefilename);
3566  strcpy(b->vedgefilename, b->innodefilename);
3567  strcpy(b->neighborfilename, b->innodefilename);
3568  strcpy(b->offfilename, b->innodefilename);
3569  strcat(b->outnodefilename, ".node");
3570  strcat(b->outelefilename, ".ele");
3571  strcat(b->edgefilename, ".edge");
3572  strcat(b->vnodefilename, ".v.node");
3573  strcat(b->vedgefilename, ".v.edge");
3574  strcat(b->neighborfilename, ".neigh");
3575  strcat(b->offfilename, ".off");
3576  } else if (increment == 0) {
3577  strcpy(b->outnodefilename, b->innodefilename);
3578  strcpy(b->outpolyfilename, b->innodefilename);
3579  strcpy(b->outelefilename, b->innodefilename);
3580  strcpy(b->edgefilename, b->innodefilename);
3581  strcpy(b->vnodefilename, b->innodefilename);
3582  strcpy(b->vedgefilename, b->innodefilename);
3583  strcpy(b->neighborfilename, b->innodefilename);
3584  strcpy(b->offfilename, b->innodefilename);
3585  strcat(b->outnodefilename, ".1.node");
3586  strcat(b->outpolyfilename, ".1.poly");
3587  strcat(b->outelefilename, ".1.ele");
3588  strcat(b->edgefilename, ".1.edge");
3589  strcat(b->vnodefilename, ".1.v.node");
3590  strcat(b->vedgefilename, ".1.v.edge");
3591  strcat(b->neighborfilename, ".1.neigh");
3592  strcat(b->offfilename, ".1.off");
3593  } else {
3594  workstring[increment] = '%';
3595  workstring[increment + 1] = 'd';
3596  workstring[increment + 2] = '\0';
3597  sprintf(b->outnodefilename, workstring, meshnumber + 1);
3598  strcpy(b->outpolyfilename, b->outnodefilename);
3599  strcpy(b->outelefilename, b->outnodefilename);
3600  strcpy(b->edgefilename, b->outnodefilename);
3601  strcpy(b->vnodefilename, b->outnodefilename);
3602  strcpy(b->vedgefilename, b->outnodefilename);
3603  strcpy(b->neighborfilename, b->outnodefilename);
3604  strcpy(b->offfilename, b->outnodefilename);
3605  strcat(b->outnodefilename, ".node");
3606  strcat(b->outpolyfilename, ".poly");
3607  strcat(b->outelefilename, ".ele");
3608  strcat(b->edgefilename, ".edge");
3609  strcat(b->vnodefilename, ".v.node");
3610  strcat(b->vedgefilename, ".v.edge");
3611  strcat(b->neighborfilename, ".neigh");
3612  strcat(b->offfilename, ".off");
3613  }
3614  strcat(b->innodefilename, ".node");
3615  strcat(b->inpolyfilename, ".poly");
3616  strcat(b->inelefilename, ".ele");
3617  strcat(b->areafilename, ".area");
3618 #endif /* not TRILIBRARY */
3619 }
3620 
3621 /** **/
3622 /** **/
3623 /********* User interaction routines begin here *********/
3624 
3625 /********* Debugging routines begin here *********/
3626 /** **/
3627 /** **/
3628 
3629 /*****************************************************************************/
3630 /* */
3631 /* printtriangle() Print out the details of an oriented triangle. */
3632 /* */
3633 /* I originally wrote this procedure to simplify debugging; it can be */
3634 /* called directly from the debugger, and presents information about an */
3635 /* oriented triangle in digestible form. It's also used when the */
3636 /* highest level of verbosity (`-VVV') is specified. */
3637 /* */
3638 /*****************************************************************************/
3639 
3640 #ifdef ANSI_DECLARATORS
3641 void printtriangle(struct mesh *m, struct behavior *b, struct otri *t)
3642 #else /* not ANSI_DECLARATORS */
3643 void printtriangle(m, b, t)
3644 struct mesh *m;
3645 struct behavior *b;
3646 struct otri *t;
3647 #endif /* not ANSI_DECLARATORS */
3648 
3649 {
3650  struct otri printtri;
3651  struct osub printsh;
3652  vertex printvertex;
3653 
3654  printf("triangle x%zx with orientation %d:\n", (size_t) t->tri,
3655  t->orient);
3656  decode(t->tri[0], printtri);
3657  if (printtri.tri == m->dummytri) {
3658  printf(" [0] = Outer space\n");
3659  } else {
3660  printf(" [0] = x%zx %d\n", (size_t) printtri.tri,
3661  printtri.orient);
3662  }
3663  decode(t->tri[1], printtri);
3664  if (printtri.tri == m->dummytri) {
3665  printf(" [1] = Outer space\n");
3666  } else {
3667  printf(" [1] = x%zx %d\n", (size_t) printtri.tri,
3668  printtri.orient);
3669  }
3670  decode(t->tri[2], printtri);
3671  if (printtri.tri == m->dummytri) {
3672  printf(" [2] = Outer space\n");
3673  } else {
3674  printf(" [2] = x%zx %d\n", (size_t) printtri.tri,
3675  printtri.orient);
3676  }
3677 
3678  org(*t, printvertex);
3679  if (printvertex == (vertex) NULL)
3680  printf(" Origin[%d] = NULL\n", (t->orient + 1) % 3 + 3);
3681  else
3682  printf(" Origin[%d] = x%zx (%.12g, %.12g)\n",
3683  (t->orient + 1) % 3 + 3, (size_t) printvertex,
3684  printvertex[0], printvertex[1]);
3685  dest(*t, printvertex);
3686  if (printvertex == (vertex) NULL)
3687  printf(" Dest [%d] = NULL\n", (t->orient + 2) % 3 + 3);
3688  else
3689  printf(" Dest [%d] = x%zx (%.12g, %.12g)\n",
3690  (t->orient + 2) % 3 + 3, (size_t) printvertex,
3691  printvertex[0], printvertex[1]);
3692  apex(*t, printvertex);
3693  if (printvertex == (vertex) NULL)
3694  printf(" Apex [%d] = NULL\n", t->orient + 3);
3695  else
3696  printf(" Apex [%d] = x%zx (%.12g, %.12g)\n",
3697  t->orient + 3, (size_t) printvertex,
3698  printvertex[0], printvertex[1]);
3699 
3700  if (b->usesegments) {
3701  sdecode(t->tri[6], printsh);
3702  if (printsh.ss != m->dummysub) {
3703  printf(" [6] = x%zx %d\n", (size_t) printsh.ss,
3704  printsh.ssorient);
3705  }
3706  sdecode(t->tri[7], printsh);
3707  if (printsh.ss != m->dummysub) {
3708  printf(" [7] = x%zx %d\n", (size_t) printsh.ss,
3709  printsh.ssorient);
3710  }
3711  sdecode(t->tri[8], printsh);
3712  if (printsh.ss != m->dummysub) {
3713  printf(" [8] = x%zx %d\n", (size_t) printsh.ss,
3714  printsh.ssorient);
3715  }
3716  }
3717 
3718  if (b->vararea) {
3719  printf(" Area constraint: %.4g\n", areabound(*t));
3720  }
3721 }
3722 
3723 /*****************************************************************************/
3724 /* */
3725 /* printsubseg() Print out the details of an oriented subsegment. */
3726 /* */
3727 /* I originally wrote this procedure to simplify debugging; it can be */
3728 /* called directly from the debugger, and presents information about an */
3729 /* oriented subsegment in digestible form. It's also used when the highest */
3730 /* level of verbosity (`-VVV') is specified. */
3731 /* */
3732 /*****************************************************************************/
3733 
3734 #ifdef ANSI_DECLARATORS
3735 void printsubseg(struct mesh *m, struct behavior *b, struct osub *s)
3736 #else /* not ANSI_DECLARATORS */
3737 void printsubseg(m, b, s)
3738 struct mesh *m;
3739 struct behavior *b;
3740 struct osub *s;
3741 #endif /* not ANSI_DECLARATORS */
3742 
3743 {
3744  struct osub printsh;
3745  struct otri printtri;
3746  vertex printvertex;
3747 
3748  (void)b; /*LM: added to suppress warning */
3749 
3750  printf("subsegment x%zx with orientation %d and mark %d:\n",
3751  (size_t) s->ss, s->ssorient, mark(*s));
3752  sdecode(s->ss[0], printsh);
3753  if (printsh.ss == m->dummysub) {
3754  printf(" [0] = No subsegment\n");
3755  } else {
3756  printf(" [0] = x%zx %d\n", (size_t) printsh.ss,
3757  printsh.ssorient);
3758  }
3759  sdecode(s->ss[1], printsh);
3760  if (printsh.ss == m->dummysub) {
3761  printf(" [1] = No subsegment\n");
3762  } else {
3763  printf(" [1] = x%zx %d\n", (size_t) printsh.ss,
3764  printsh.ssorient);
3765  }
3766 
3767  sorg(*s, printvertex);
3768  if (printvertex == (vertex) NULL)
3769  printf(" Origin[%d] = NULL\n", 2 + s->ssorient);
3770  else
3771  printf(" Origin[%d] = x%zx (%.12g, %.12g)\n",
3772  2 + s->ssorient, (size_t) printvertex,
3773  printvertex[0], printvertex[1]);
3774  sdest(*s, printvertex);
3775  if (printvertex == (vertex) NULL)
3776  printf(" Dest [%d] = NULL\n", 3 - s->ssorient);
3777  else
3778  printf(" Dest [%d] = x%zx (%.12g, %.12g)\n",
3779  3 - s->ssorient, (size_t) printvertex,
3780  printvertex[0], printvertex[1]);
3781 
3782  decode(s->ss[6], printtri);
3783  if (printtri.tri == m->dummytri) {
3784  printf(" [6] = Outer space\n");
3785  } else {
3786  printf(" [6] = x%zx %d\n", (size_t) printtri.tri,
3787  printtri.orient);
3788  }
3789  decode(s->ss[7], printtri);
3790  if (printtri.tri == m->dummytri) {
3791  printf(" [7] = Outer space\n");
3792  } else {
3793  printf(" [7] = x%zx %d\n", (size_t) printtri.tri,
3794  printtri.orient);
3795  }
3796 
3797  segorg(*s, printvertex);
3798  if (printvertex == (vertex) NULL)
3799  printf(" Segment origin[%d] = NULL\n", 4 + s->ssorient);
3800  else
3801  printf(" Segment origin[%d] = x%zx (%.12g, %.12g)\n",
3802  4 + s->ssorient, (size_t) printvertex,
3803  printvertex[0], printvertex[1]);
3804  segdest(*s, printvertex);
3805  if (printvertex == (vertex) NULL)
3806  printf(" Segment dest [%d] = NULL\n", 5 - s->ssorient);
3807  else
3808  printf(" Segment dest [%d] = x%zx (%.12g, %.12g)\n",
3809  5 - s->ssorient, (size_t) printvertex,
3810  printvertex[0], printvertex[1]);
3811 }
3812 
3813 /** **/
3814 /** **/
3815 /********* Debugging routines end here *********/
3816 
3817 /********* Memory management routines begin here *********/
3818 /** **/
3819 /** **/
3820 
3821 /*****************************************************************************/
3822 /* */
3823 /* poolzero() Set all of a pool's fields to zero. */
3824 /* */
3825 /* This procedure should never be called on a pool that has any memory */
3826 /* allocated to it, as that memory would leak. */
3827 /* */
3828 /*****************************************************************************/
3829 
3830 #ifdef ANSI_DECLARATORS
3831 void poolzero(struct memorypool *pool)
3832 #else /* not ANSI_DECLARATORS */
3833 void poolzero(pool)
3834 struct memorypool *pool;
3835 #endif /* not ANSI_DECLARATORS */
3836 
3837 {
3838  pool->firstblock = (VOID **) NULL;
3839  pool->nowblock = (VOID **) NULL;
3840  pool->nextitem = (VOID *) NULL;
3841  pool->deaditemstack = (VOID *) NULL;
3842  pool->pathblock = (VOID **) NULL;
3843  pool->pathitem = (VOID *) NULL;
3844  pool->alignbytes = 0;
3845  pool->itembytes = 0;
3846  pool->itemsperblock = 0;
3847  pool->itemsfirstblock = 0;
3848  pool->items = 0;
3849  pool->maxitems = 0;
3850  pool->unallocateditems = 0;
3851  pool->pathitemsleft = 0;
3852 }
3853 
3854 /*****************************************************************************/
3855 /* */
3856 /* poolrestart() Deallocate all items in a pool. */
3857 /* */
3858 /* The pool is returned to its starting state, except that no memory is */
3859 /* freed to the operating system. Rather, the previously allocated blocks */
3860 /* are ready to be reused. */
3861 /* */
3862 /*****************************************************************************/
3863 
3864 #ifdef ANSI_DECLARATORS
3865 void poolrestart(struct memorypool *pool)
3866 #else /* not ANSI_DECLARATORS */
3867 void poolrestart(pool)
3868 struct memorypool *pool;
3869 #endif /* not ANSI_DECLARATORS */
3870 
3871 {
3872  uintptr_t alignptr;
3873 
3874  pool->items = 0;
3875  pool->maxitems = 0;
3876 
3877  /* Set the currently active block. */
3878  pool->nowblock = pool->firstblock;
3879  /* Find the first item in the pool. Increment by the size of (VOID *). */
3880  alignptr = (uintptr_t) (pool->nowblock + 1);
3881  /* Align the item on an `alignbytes'-byte boundary. */
3882  pool->nextitem = (VOID *)
3883  (alignptr + (uintptr_t) pool->alignbytes -
3884  (alignptr % (uintptr_t) pool->alignbytes));
3885  /* There are lots of unallocated items left in this block. */
3886  pool->unallocateditems = pool->itemsfirstblock;
3887  /* The stack of deallocated items is empty. */
3888  pool->deaditemstack = (VOID *) NULL;
3889 }
3890 
3891 /*****************************************************************************/
3892 /* */
3893 /* poolinit() Initialize a pool of memory for allocation of items. */
3894 /* */
3895 /* This routine initializes the machinery for allocating items. A `pool' */
3896 /* is created whose records have size at least `bytecount'. Items will be */
3897 /* allocated in `itemcount'-item blocks. Each item is assumed to be a */
3898 /* collection of words, and either pointers or floating-point values are */
3899 /* assumed to be the "primary" word type. (The "primary" word type is used */
3900 /* to determine alignment of items.) If `alignment' isn't zero, all items */
3901 /* will be `alignment'-byte aligned in memory. `alignment' must be either */
3902 /* a multiple or a factor of the primary word size; powers of two are safe. */
3903 /* `alignment' is normally used to create a few unused bits at the bottom */
3904 /* of each item's pointer, in which information may be stored. */
3905 /* */
3906 /* Don't change this routine unless you understand it. */
3907 /* */
3908 /*****************************************************************************/
3909 
3910 #ifdef ANSI_DECLARATORS
3911 void poolinit(struct memorypool *pool, int bytecount, int itemcount,
3912  int firstitemcount, int alignment)
3913 #else /* not ANSI_DECLARATORS */
3914 void poolinit(pool, bytecount, itemcount, firstitemcount, alignment)
3915 struct memorypool *pool;
3916 int bytecount;
3917 int itemcount;
3918 int firstitemcount;
3919 int alignment;
3920 #endif /* not ANSI_DECLARATORS */
3921 
3922 {
3923  /* Find the proper alignment, which must be at least as large as: */
3924  /* - The parameter `alignment'. */
3925  /* - sizeof(VOID *), so the stack of dead items can be maintained */
3926  /* without unaligned accesses. */
3927  if (alignment > (int) sizeof(VOID *)) {
3928  pool->alignbytes = alignment;
3929  } else {
3930  pool->alignbytes = sizeof(VOID *);
3931  }
3932  pool->itembytes = ((bytecount - 1) / pool->alignbytes + 1) *
3933  pool->alignbytes;
3934  pool->itemsperblock = itemcount;
3935  if (firstitemcount == 0) {
3936  pool->itemsfirstblock = itemcount;
3937  } else {
3938  pool->itemsfirstblock = firstitemcount;
3939  }
3940 
3941  /* Allocate a block of items. Space for `itemsfirstblock' items and one */
3942  /* pointer (to point to the next block) are allocated, as well as space */
3943  /* to ensure alignment of the items. */
3944  pool->firstblock = (VOID **)
3945  trimalloc(pool->itemsfirstblock * pool->itembytes + (int) sizeof(VOID *) +
3946  pool->alignbytes);
3947  /* Set the next block pointer to NULL. */
3948  *(pool->firstblock) = (VOID *) NULL;
3949  poolrestart(pool);
3950 }
3951 
3952 /*****************************************************************************/
3953 /* */
3954 /* pooldeinit() Free to the operating system all memory taken by a pool. */
3955 /* */
3956 /*****************************************************************************/
3957 
3958 #ifdef ANSI_DECLARATORS
3959 void pooldeinit(struct memorypool *pool)
3960 #else /* not ANSI_DECLARATORS */
3961 void pooldeinit(pool)
3962 struct memorypool *pool;
3963 #endif /* not ANSI_DECLARATORS */
3964 
3965 {
3966  while (pool->firstblock != (VOID **) NULL) {
3967  pool->nowblock = (VOID **) *(pool->firstblock);
3968  trifree((VOID *) pool->firstblock);
3969  pool->firstblock = pool->nowblock;
3970  }
3971 }
3972 
3973 /*****************************************************************************/
3974 /* */
3975 /* poolalloc() Allocate space for an item. */
3976 /* */
3977 /*****************************************************************************/
3978 
3979 #ifdef ANSI_DECLARATORS
3980 VOID *poolalloc(struct memorypool *pool)
3981 #else /* not ANSI_DECLARATORS */
3982 VOID *poolalloc(pool)
3983 struct memorypool *pool;
3984 #endif /* not ANSI_DECLARATORS */
3985 
3986 {
3987  VOID *newitem;
3988  VOID **newblock;
3989  uintptr_t alignptr;
3990 
3991  /* First check the linked list of dead items. If the list is not */
3992  /* empty, allocate an item from the list rather than a fresh one. */
3993  if (pool->deaditemstack != (VOID *) NULL) {
3994  newitem = pool->deaditemstack; /* Take first item in list. */
3995  pool->deaditemstack = * (VOID **) pool->deaditemstack;
3996  } else {
3997  /* Check if there are any free items left in the current block. */
3998  if (pool->unallocateditems == 0) {
3999  /* Check if another block must be allocated. */
4000  if (*(pool->nowblock) == (VOID *) NULL) {
4001  /* Allocate a new block of items, pointed to by the previous block. */
4002  newblock = (VOID **) trimalloc(pool->itemsperblock * pool->itembytes +
4003  (int) sizeof(VOID *) +
4004  pool->alignbytes);
4005  *(pool->nowblock) = (VOID *) newblock;
4006  /* The next block pointer is NULL. */
4007  *newblock = (VOID *) NULL;
4008  }
4009 
4010  /* Move to the new block. */
4011  pool->nowblock = (VOID **) *(pool->nowblock);
4012  /* Find the first item in the block. */
4013  /* Increment by the size of (VOID *). */
4014  alignptr = (uintptr_t) (pool->nowblock + 1);
4015  /* Align the item on an `alignbytes'-byte boundary. */
4016  pool->nextitem = (VOID *)
4017  (alignptr + (uintptr_t) pool->alignbytes -
4018  (alignptr % (uintptr_t) pool->alignbytes));
4019  /* There are lots of unallocated items left in this block. */
4020  pool->unallocateditems = pool->itemsperblock;
4021  }
4022 
4023  /* Allocate a new item. */
4024  newitem = pool->nextitem;
4025  /* Advance `nextitem' pointer to next free item in block. */
4026  pool->nextitem = (VOID *) ((char *) pool->nextitem + pool->itembytes);
4027  pool->unallocateditems--;
4028  pool->maxitems++;
4029  }
4030  pool->items++;
4031  return newitem;
4032 }
4033 
4034 /*****************************************************************************/
4035 /* */
4036 /* pooldealloc() Deallocate space for an item. */
4037 /* */
4038 /* The deallocated space is stored in a queue for later reuse. */
4039 /* */
4040 /*****************************************************************************/
4041 
4042 #ifdef ANSI_DECLARATORS
4043 void pooldealloc(struct memorypool *pool, VOID *dyingitem)
4044 #else /* not ANSI_DECLARATORS */
4045 void pooldealloc(pool, dyingitem)
4046 struct memorypool *pool;
4047 VOID *dyingitem;
4048 #endif /* not ANSI_DECLARATORS */
4049 
4050 {
4051  /* Push freshly killed item onto stack. */
4052  *((VOID **) dyingitem) = pool->deaditemstack;
4053  pool->deaditemstack = dyingitem;
4054  pool->items--;
4055 }
4056 
4057 /*****************************************************************************/
4058 /* */
4059 /* traversalinit() Prepare to traverse the entire list of items. */
4060 /* */
4061 /* This routine is used in conjunction with traverse(). */
4062 /* */
4063 /*****************************************************************************/
4064 
4065 #ifdef ANSI_DECLARATORS
4066 void traversalinit(struct memorypool *pool)
4067 #else /* not ANSI_DECLARATORS */
4068 void traversalinit(pool)
4069 struct memorypool *pool;
4070 #endif /* not ANSI_DECLARATORS */
4071 
4072 {
4073  uintptr_t alignptr;
4074 
4075  /* Begin the traversal in the first block. */
4076  pool->pathblock = pool->firstblock;
4077  /* Find the first item in the block. Increment by the size of (VOID *). */
4078  alignptr = (uintptr_t) (pool->pathblock + 1);
4079  /* Align with item on an `alignbytes'-byte boundary. */
4080  pool->pathitem = (VOID *)
4081  (alignptr + (uintptr_t) pool->alignbytes -
4082  (alignptr % (uintptr_t) pool->alignbytes));
4083  /* Set the number of items left in the current block. */
4084  pool->pathitemsleft = pool->itemsfirstblock;
4085 }
4086 
4087 /*****************************************************************************/
4088 /* */
4089 /* traverse() Find the next item in the list. */
4090 /* */
4091 /* This routine is used in conjunction with traversalinit(). Be forewarned */
4092 /* that this routine successively returns all items in the list, including */
4093 /* deallocated ones on the deaditemqueue. It's up to you to figure out */
4094 /* which ones are actually dead. Why? I don't want to allocate extra */
4095 /* space just to demarcate dead items. It can usually be done more */
4096 /* space-efficiently by a routine that knows something about the structure */
4097 /* of the item. */
4098 /* */
4099 /*****************************************************************************/
4100 
4101 #ifdef ANSI_DECLARATORS
4102 VOID *traverse(struct memorypool *pool)
4103 #else /* not ANSI_DECLARATORS */
4104 VOID *traverse(pool)
4105 struct memorypool *pool;
4106 #endif /* not ANSI_DECLARATORS */
4107 
4108 {
4109  VOID *newitem;
4110  uintptr_t alignptr;
4111 
4112  /* Stop upon exhausting the list of items. */
4113  if (pool->pathitem == pool->nextitem) {
4114  return (VOID *) NULL;
4115  }
4116 
4117  /* Check whether any untraversed items remain in the current block. */
4118  if (pool->pathitemsleft == 0) {
4119  /* Find the next block. */
4120  pool->pathblock = (VOID **) *(pool->pathblock);
4121  /* Find the first item in the block. Increment by the size of (VOID *). */
4122  alignptr = (uintptr_t) (pool->pathblock + 1);
4123  /* Align with item on an `alignbytes'-byte boundary. */
4124  pool->pathitem = (VOID *)
4125  (alignptr + (uintptr_t) pool->alignbytes -
4126  (alignptr % (uintptr_t) pool->alignbytes));
4127  /* Set the number of items left in the current block. */
4128  pool->pathitemsleft = pool->itemsperblock;
4129  }
4130 
4131  newitem = pool->pathitem;
4132  /* Find the next item in the block. */
4133  pool->pathitem = (VOID *) ((char *) pool->pathitem + pool->itembytes);
4134  pool->pathitemsleft--;
4135  return newitem;
4136 }
4137 
4138 /*****************************************************************************/
4139 /* */
4140 /* dummyinit() Initialize the triangle that fills "outer space" and the */
4141 /* omnipresent subsegment. */
4142 /* */
4143 /* The triangle that fills "outer space," called `dummytri', is pointed to */
4144 /* by every triangle and subsegment on a boundary (be it outer or inner) of */
4145 /* the triangulation. Also, `dummytri' points to one of the triangles on */
4146 /* the convex hull (until the holes and concavities are carved), making it */
4147 /* possible to find a starting triangle for point location. */
4148 /* */
4149 /* The omnipresent subsegment, `dummysub', is pointed to by every triangle */
4150 /* or subsegment that doesn't have a full complement of real subsegments */
4151 /* to point to. */
4152 /* */
4153 /* `dummytri' and `dummysub' are generally required to fulfill only a few */
4154 /* invariants: their vertices must remain NULL and `dummytri' must always */
4155 /* be bonded (at offset zero) to some triangle on the convex hull of the */
4156 /* mesh, via a boundary edge. Otherwise, the connections of `dummytri' and */
4157 /* `dummysub' may change willy-nilly. This makes it possible to avoid */
4158 /* writing a good deal of special-case code (in the edge flip, for example) */
4159 /* for dealing with the boundary of the mesh, places where no subsegment is */
4160 /* present, and so forth. Other entities are frequently bonded to */
4161 /* `dummytri' and `dummysub' as if they were real mesh entities, with no */
4162 /* harm done. */
4163 /* */
4164 /*****************************************************************************/
4165 
4166 #ifdef ANSI_DECLARATORS
4167 void dummyinit(struct mesh *m, struct behavior *b, int trianglebytes,
4168  int subsegbytes)
4169 #else /* not ANSI_DECLARATORS */
4170 void dummyinit(m, b, trianglebytes, subsegbytes)
4171 struct mesh *m;
4172 struct behavior *b;
4173 int trianglebytes;
4174 int subsegbytes;
4175 #endif /* not ANSI_DECLARATORS */
4176 
4177 {
4178  uintptr_t alignptr;
4179 
4180  /* Set up `dummytri', the `triangle' that occupies "outer space." */
4181  m->dummytribase = (triangle *) trimalloc(trianglebytes +
4182  m->triangles.alignbytes);
4183  /* Align `dummytri' on a `triangles.alignbytes'-byte boundary. */
4184  alignptr = (uintptr_t) m->dummytribase;
4185  m->dummytri = (triangle *)
4186  (alignptr + (uintptr_t) m->triangles.alignbytes -
4187  (alignptr % (uintptr_t) m->triangles.alignbytes));
4188  /* Initialize the three adjoining triangles to be "outer space." These */
4189  /* will eventually be changed by various bonding operations, but their */
4190  /* values don't really matter, as long as they can legally be */
4191  /* dereferenced. */
4192  m->dummytri[0] = (triangle) m->dummytri;
4193  m->dummytri[1] = (triangle) m->dummytri;
4194  m->dummytri[2] = (triangle) m->dummytri;
4195  /* Three NULL vertices. */
4196  m->dummytri[3] = (triangle) NULL;
4197  m->dummytri[4] = (triangle) NULL;
4198  m->dummytri[5] = (triangle) NULL;
4199 
4200  if (b->usesegments) {
4201  /* Set up `dummysub', the omnipresent subsegment pointed to by any */
4202  /* triangle side or subsegment end that isn't attached to a real */
4203  /* subsegment. */
4204  m->dummysubbase = (subseg *) trimalloc(subsegbytes +
4205  m->subsegs.alignbytes);
4206  /* Align `dummysub' on a `subsegs.alignbytes'-byte boundary. */
4207  alignptr = (uintptr_t) m->dummysubbase;
4208  m->dummysub = (subseg *)
4209  (alignptr + (uintptr_t) m->subsegs.alignbytes -
4210  (alignptr % (uintptr_t) m->subsegs.alignbytes));
4211  /* Initialize the two adjoining subsegments to be the omnipresent */
4212  /* subsegment. These will eventually be changed by various bonding */
4213  /* operations, but their values don't really matter, as long as they */
4214  /* can legally be dereferenced. */
4215  m->dummysub[0] = (subseg) m->dummysub;
4216  m->dummysub[1] = (subseg) m->dummysub;
4217  /* Four NULL vertices. */
4218  m->dummysub[2] = (subseg) NULL;
4219  m->dummysub[3] = (subseg) NULL;
4220  m->dummysub[4] = (subseg) NULL;
4221  m->dummysub[5] = (subseg) NULL;
4222  /* Initialize the two adjoining triangles to be "outer space." */
4223  m->dummysub[6] = (subseg) m->dummytri;
4224  m->dummysub[7] = (subseg) m->dummytri;
4225  /* Set the boundary marker to zero. */
4226  * (int *) (m->dummysub + 8) = 0;
4227 
4228  /* Initialize the three adjoining subsegments of `dummytri' to be */
4229  /* the omnipresent subsegment. */
4230  m->dummytri[6] = (triangle) m->dummysub;
4231  m->dummytri[7] = (triangle) m->dummysub;
4232  m->dummytri[8] = (triangle) m->dummysub;
4233  }
4234 }
4235 
4236 /*****************************************************************************/
4237 /* */
4238 /* initializevertexpool() Calculate the size of the vertex data structure */
4239 /* and initialize its memory pool. */
4240 /* */
4241 /* This routine also computes the `vertexmarkindex' and `vertex2triindex' */
4242 /* indices used to find values within each vertex. */
4243 /* */
4244 /*****************************************************************************/
4245 
4246 #ifdef ANSI_DECLARATORS
4247 void initializevertexpool(struct mesh *m, struct behavior *b)
4248 #else /* not ANSI_DECLARATORS */
4249 void initializevertexpool(m, b)
4250 struct mesh *m;
4251 struct behavior *b;
4252 #endif /* not ANSI_DECLARATORS */
4253 
4254 {
4255  int vertexsize;
4256 
4257  /* The index within each vertex at which the boundary marker is found, */
4258  /* followed by the vertex type. Ensure the vertex marker is aligned to */
4259  /* a sizeof(int)-byte address. */
4260  m->vertexmarkindex = ((m->mesh_dim + m->nextras) * sizeof(REAL) +
4261  sizeof(int) - 1) /
4262  sizeof(int);
4263  vertexsize = (m->vertexmarkindex + 2) * sizeof(int);
4264  if (b->poly) {
4265  /* The index within each vertex at which a triangle pointer is found. */
4266  /* Ensure the pointer is aligned to a sizeof(triangle)-byte address. */
4267  m->vertex2triindex = (vertexsize + sizeof(triangle) - 1) /
4268  sizeof(triangle);
4269  vertexsize = (m->vertex2triindex + 1) * sizeof(triangle);
4270  }
4271 
4272  /* Initialize the pool of vertices. */
4273  poolinit(&m->vertices, vertexsize, VERTEXPERBLOCK,
4274  m->invertices > VERTEXPERBLOCK ? m->invertices : VERTEXPERBLOCK,
4275  sizeof(REAL));
4276 }
4277 
4278 /*****************************************************************************/
4279 /* */
4280 /* initializetrisubpools() Calculate the sizes of the triangle and */
4281 /* subsegment data structures and initialize */
4282 /* their memory pools. */
4283 /* */
4284 /* This routine also computes the `highorderindex', `elemattribindex', and */
4285 /* `areaboundindex' indices used to find values within each triangle. */
4286 /* */
4287 /*****************************************************************************/
4288 
4289 #ifdef ANSI_DECLARATORS
4290 void initializetrisubpools(struct mesh *m, struct behavior *b)
4291 #else /* not ANSI_DECLARATORS */
4292 void initializetrisubpools(m, b)
4293 struct mesh *m;
4294 struct behavior *b;
4295 #endif /* not ANSI_DECLARATORS */
4296 
4297 {
4298  int trisize;
4299 
4300  /* The index within each triangle at which the extra nodes (above three) */
4301  /* associated with high order elements are found. There are three */
4302  /* pointers to other triangles, three pointers to corners, and possibly */
4303  /* three pointers to subsegments before the extra nodes. */
4304  m->highorderindex = 6 + (b->usesegments * 3);
4305  /* The number of bytes occupied by a triangle. */
4306  trisize = ((b->order + 1) * (b->order + 2) / 2 + (m->highorderindex - 3)) *
4307  sizeof(triangle);
4308  /* The index within each triangle at which its attributes are found, */
4309  /* where the index is measured in REALs. */
4310  m->elemattribindex = (trisize + sizeof(REAL) - 1) / sizeof(REAL);
4311  /* The index within each triangle at which the maximum area constraint */
4312  /* is found, where the index is measured in REALs. Note that if the */
4313  /* `regionattrib' flag is set, an additional attribute will be added. */
4314  m->areaboundindex = m->elemattribindex + m->eextras + b->regionattrib;
4315  /* If triangle attributes or an area bound are needed, increase the number */
4316  /* of bytes occupied by a triangle. */
4317  if (b->vararea) {
4318  trisize = (m->areaboundindex + 1) * sizeof(REAL);
4319  } else if (m->eextras + b->regionattrib > 0) {
4320  trisize = m->areaboundindex * sizeof(REAL);
4321  }
4322  /* If a Voronoi diagram or triangle neighbor graph is requested, make */
4323  /* sure there's room to store an integer index in each triangle. This */
4324  /* integer index can occupy the same space as the subsegment pointers */
4325  /* or attributes or area constraint or extra nodes. */
4326  if ((b->voronoi || b->neighbors) &&
4327  (trisize < (int) ( 6 * sizeof(triangle) + sizeof(int)))) {
4328  trisize = 6 * sizeof(triangle) + sizeof(int);
4329  }
4330 
4331  /* Having determined the memory size of a triangle, initialize the pool. */
4332  poolinit(&m->triangles, trisize, TRIPERBLOCK,
4333  (2 * m->invertices - 2) > TRIPERBLOCK ? (2 * m->invertices - 2) :
4334  TRIPERBLOCK, 4);
4335 
4336  if (b->usesegments) {
4337  /* Initialize the pool of subsegments. Take into account all eight */
4338  /* pointers and one boundary marker. */
4339  poolinit(&m->subsegs, 8 * sizeof(triangle) + sizeof(int),
4341 
4342  /* Initialize the "outer space" triangle and omnipresent subsegment. */
4343  dummyinit(m, b, m->triangles.itembytes, m->subsegs.itembytes);
4344  } else {
4345  /* Initialize the "outer space" triangle. */
4346  dummyinit(m, b, m->triangles.itembytes, 0);
4347  }
4348 }
4349 
4350 /*****************************************************************************/
4351 /* */
4352 /* triangledealloc() Deallocate space for a triangle, marking it dead. */
4353 /* */
4354 /*****************************************************************************/
4355 
4356 #ifdef ANSI_DECLARATORS
4357 void triangledealloc(struct mesh *m, triangle *dyingtriangle)
4358 #else /* not ANSI_DECLARATORS */
4359 void triangledealloc(m, dyingtriangle)
4360 struct mesh *m;
4361 triangle *dyingtriangle;
4362 #endif /* not ANSI_DECLARATORS */
4363 
4364 {
4365  /* Mark the triangle as dead. This makes it possible to detect dead */
4366  /* triangles when traversing the list of all triangles. */
4367  killtri(dyingtriangle);
4368  pooldealloc(&m->triangles, (VOID *) dyingtriangle);
4369 }
4370 
4371 /*****************************************************************************/
4372 /* */
4373 /* triangletraverse() Traverse the triangles, skipping dead ones. */
4374 /* */
4375 /*****************************************************************************/
4376 
4377 #ifdef ANSI_DECLARATORS
4379 #else /* not ANSI_DECLARATORS */
4381 struct mesh *m;
4382 #endif /* not ANSI_DECLARATORS */
4383 
4384 {
4385  triangle *newtriangle;
4386 
4387  do {
4388  newtriangle = (triangle *) traverse(&m->triangles);
4389  if (newtriangle == (triangle *) NULL) {
4390  return (triangle *) NULL;
4391  }
4392  } while (deadtri(newtriangle)); /* Skip dead ones. */
4393  return newtriangle;
4394 }
4395 
4396 /*****************************************************************************/
4397 /* */
4398 /* subsegdealloc() Deallocate space for a subsegment, marking it dead. */
4399 /* */
4400 /*****************************************************************************/
4401 
4402 #ifdef ANSI_DECLARATORS
4403 void subsegdealloc(struct mesh *m, subseg *dyingsubseg)
4404 #else /* not ANSI_DECLARATORS */
4405 void subsegdealloc(m, dyingsubseg)
4406 struct mesh *m;
4407 subseg *dyingsubseg;
4408 #endif /* not ANSI_DECLARATORS */
4409 
4410 {
4411  /* Mark the subsegment as dead. This makes it possible to detect dead */
4412  /* subsegments when traversing the list of all subsegments. */
4413  killsubseg(dyingsubseg);
4414  pooldealloc(&m->subsegs, (VOID *) dyingsubseg);
4415 }
4416 
4417 /*****************************************************************************/
4418 /* */
4419 /* subsegtraverse() Traverse the subsegments, skipping dead ones. */
4420 /* */
4421 /*****************************************************************************/
4422 
4423 #ifdef ANSI_DECLARATORS
4425 #else /* not ANSI_DECLARATORS */
4427 struct mesh *m;
4428 #endif /* not ANSI_DECLARATORS */
4429 
4430 {
4431  subseg *newsubseg;
4432 
4433  do {
4434  newsubseg = (subseg *) traverse(&m->subsegs);
4435  if (newsubseg == (subseg *) NULL) {
4436  return (subseg *) NULL;
4437  }
4438  } while (deadsubseg(newsubseg)); /* Skip dead ones. */
4439  return newsubseg;
4440 }
4441 
4442 /*****************************************************************************/
4443 /* */
4444 /* vertexdealloc() Deallocate space for a vertex, marking it dead. */
4445 /* */
4446 /*****************************************************************************/
4447 
4448 #ifdef ANSI_DECLARATORS
4449 void vertexdealloc(struct mesh *m, vertex dyingvertex)
4450 #else /* not ANSI_DECLARATORS */
4451 void vertexdealloc(m, dyingvertex)
4452 struct mesh *m;
4453 vertex dyingvertex;
4454 #endif /* not ANSI_DECLARATORS */
4455 
4456 {
4457  /* Mark the vertex as dead. This makes it possible to detect dead */
4458  /* vertices when traversing the list of all vertices. */
4459  setvertextype(dyingvertex, DEADVERTEX);
4460  pooldealloc(&m->vertices, (VOID *) dyingvertex);
4461 }
4462 
4463 /*****************************************************************************/
4464 /* */
4465 /* vertextraverse() Traverse the vertices, skipping dead ones. */
4466 /* */
4467 /*****************************************************************************/
4468 
4469 #ifdef ANSI_DECLARATORS
4471 #else /* not ANSI_DECLARATORS */
4473 struct mesh *m;
4474 #endif /* not ANSI_DECLARATORS */
4475 
4476 {
4477  vertex newvertex;
4478 
4479  do {
4480  newvertex = (vertex) traverse(&m->vertices);
4481  if (newvertex == (vertex) NULL) {
4482  return (vertex) NULL;
4483  }
4484  } while (vertextype(newvertex) == DEADVERTEX); /* Skip dead ones. */
4485  return newvertex;
4486 }
4487 
4488 /*****************************************************************************/
4489 /* */
4490 /* badsubsegdealloc() Deallocate space for a bad subsegment, marking it */
4491 /* dead. */
4492 /* */
4493 /*****************************************************************************/
4494 
4495 #ifndef CDT_ONLY
4496 
4497 #ifdef ANSI_DECLARATORS
4498 void badsubsegdealloc(struct mesh *m, struct badsubseg *dyingseg)
4499 #else /* not ANSI_DECLARATORS */
4500 void badsubsegdealloc(m, dyingseg)
4501 struct mesh *m;
4502 struct badsubseg *dyingseg;
4503 #endif /* not ANSI_DECLARATORS */
4504 
4505 {
4506  /* Set subsegment's origin to NULL. This makes it possible to detect dead */
4507  /* badsubsegs when traversing the list of all badsubsegs . */
4508  dyingseg->subsegorg = (vertex) NULL;
4509  pooldealloc(&m->badsubsegs, (VOID *) dyingseg);
4510 }
4511 
4512 #endif /* not CDT_ONLY */
4513 
4514 /*****************************************************************************/
4515 /* */
4516 /* badsubsegtraverse() Traverse the bad subsegments, skipping dead ones. */
4517 /* */
4518 /*****************************************************************************/
4519 
4520 #ifndef CDT_ONLY
4521 
4522 #ifdef ANSI_DECLARATORS
4523 struct badsubseg *badsubsegtraverse(struct mesh *m)
4524 #else /* not ANSI_DECLARATORS */
4525 struct badsubseg *badsubsegtraverse(m)
4526 struct mesh *m;
4527 #endif /* not ANSI_DECLARATORS */
4528 
4529 {
4530  struct badsubseg *newseg;
4531 
4532  do {
4533  newseg = (struct badsubseg *) traverse(&m->badsubsegs);
4534  if (newseg == (struct badsubseg *) NULL) {
4535  return (struct badsubseg *) NULL;
4536  }
4537  } while (newseg->subsegorg == (vertex) NULL); /* Skip dead ones. */
4538  return newseg;
4539 }
4540 
4541 #endif /* not CDT_ONLY */
4542 
4543 /*****************************************************************************/
4544 /* */
4545 /* getvertex() Get a specific vertex, by number, from the list. */
4546 /* */
4547 /* The first vertex is number 'firstnumber'. */
4548 /* */
4549 /* Note that this takes O(n) time (with a small constant, if VERTEXPERBLOCK */
4550 /* is large). I don't care to take the trouble to make it work in constant */
4551 /* time. */
4552 /* */
4553 /*****************************************************************************/
4554 
4555 #ifdef ANSI_DECLARATORS
4556 vertex getvertex(struct mesh *m, struct behavior *b, int number)
4557 #else /* not ANSI_DECLARATORS */
4558 vertex getvertex(m, b, number)
4559 struct mesh *m;
4560 struct behavior *b;
4561 int number;
4562 #endif /* not ANSI_DECLARATORS */
4563 
4564 {
4565  VOID **getblock;
4566  char *foundvertex;
4567  uintptr_t alignptr;
4568  int current;
4569 
4570  getblock = m->vertices.firstblock;
4571  current = b->firstnumber;
4572 
4573  /* Find the right block. */
4574  if (current + m->vertices.itemsfirstblock <= number) {
4575  getblock = (VOID **) *getblock;
4576  current += m->vertices.itemsfirstblock;
4577  while (current + m->vertices.itemsperblock <= number) {
4578  getblock = (VOID **) *getblock;
4579  current += m->vertices.itemsperblock;
4580  }
4581  }
4582 
4583  /* Now find the right vertex. */
4584  alignptr = (uintptr_t) (getblock + 1);
4585  foundvertex = (char *) (alignptr + (uintptr_t) m->vertices.alignbytes -
4586  (alignptr % (uintptr_t) m->vertices.alignbytes));
4587  return (vertex) (foundvertex + m->vertices.itembytes * (number - current));
4588 }
4589 
4590 /*****************************************************************************/
4591 /* */
4592 /* triangledeinit() Free all remaining allocated memory. */
4593 /* */
4594 /*****************************************************************************/
4595 
4596 #ifdef ANSI_DECLARATORS
4597 void triangledeinit(struct mesh *m, struct behavior *b)
4598 #else /* not ANSI_DECLARATORS */
4599 void triangledeinit(m, b)
4600 struct mesh *m;
4601 struct behavior *b;
4602 #endif /* not ANSI_DECLARATORS */
4603 
4604 {
4605  pooldeinit(&m->triangles);
4606  trifree((VOID *) m->dummytribase);
4607  if (b->usesegments) {
4608  pooldeinit(&m->subsegs);
4609  trifree((VOID *) m->dummysubbase);
4610  }
4611  pooldeinit(&m->vertices);
4612 #ifndef CDT_ONLY
4613  if (b->quality) {
4614  pooldeinit(&m->badsubsegs);
4615  if ((b->minangle > 0.0) || b->vararea || b->fixedarea || b->usertest) {
4616  pooldeinit(&m->badtriangles);
4617  pooldeinit(&m->flipstackers);
4618  }
4619  }
4620 #endif /* not CDT_ONLY */
4621 }
4622 
4623 /** **/
4624 /** **/
4625 /********* Memory management routines end here *********/
4626 
4627 /********* Constructors begin here *********/
4628 /** **/
4629 /** **/
4630 
4631 /*****************************************************************************/
4632 /* */
4633 /* maketriangle() Create a new triangle with orientation zero. */
4634 /* */
4635 /*****************************************************************************/
4636 
4637 #ifdef ANSI_DECLARATORS
4638 void maketriangle(struct mesh *m, struct behavior *b, struct otri *newotri)
4639 #else /* not ANSI_DECLARATORS */
4640 void maketriangle(m, b, newotri)
4641 struct mesh *m;
4642 struct behavior *b;
4643 struct otri *newotri;
4644 #endif /* not ANSI_DECLARATORS */
4645 
4646 {
4647  int i;
4648 
4649  newotri->tri = (triangle *) poolalloc(&m->triangles);
4650  /* Initialize the three adjoining triangles to be "outer space". */
4651  newotri->tri[0] = (triangle) m->dummytri;
4652  newotri->tri[1] = (triangle) m->dummytri;
4653  newotri->tri[2] = (triangle) m->dummytri;
4654  /* Three NULL vertices. */
4655  newotri->tri[3] = (triangle) NULL;
4656  newotri->tri[4] = (triangle) NULL;
4657  newotri->tri[5] = (triangle) NULL;
4658  if (b->usesegments) {
4659  /* Initialize the three adjoining subsegments to be the omnipresent */
4660  /* subsegment. */
4661  newotri->tri[6] = (triangle) m->dummysub;
4662  newotri->tri[7] = (triangle) m->dummysub;
4663  newotri->tri[8] = (triangle) m->dummysub;
4664  }
4665  for (i = 0; i < m->eextras; i++) {
4666  setelemattribute(*newotri, i, 0.0);
4667  }
4668  if (b->vararea) {
4669  setareabound(*newotri, -1.0);
4670  }
4671 
4672  newotri->orient = 0;
4673 }
4674 
4675 /*****************************************************************************/
4676 /* */
4677 /* makesubseg() Create a new subsegment with orientation zero. */
4678 /* */
4679 /*****************************************************************************/
4680 
4681 #ifdef ANSI_DECLARATORS
4682 void makesubseg(struct mesh *m, struct osub *newsubseg)
4683 #else /* not ANSI_DECLARATORS */
4684 void makesubseg(m, newsubseg)
4685 struct mesh *m;
4686 struct osub *newsubseg;
4687 #endif /* not ANSI_DECLARATORS */
4688 
4689 {
4690  newsubseg->ss = (subseg *) poolalloc(&m->subsegs);
4691  /* Initialize the two adjoining subsegments to be the omnipresent */
4692  /* subsegment. */
4693  newsubseg->ss[0] = (subseg) m->dummysub;
4694  newsubseg->ss[1] = (subseg) m->dummysub;
4695  /* Four NULL vertices. */
4696  newsubseg->ss[2] = (subseg) NULL;
4697  newsubseg->ss[3] = (subseg) NULL;
4698  newsubseg->ss[4] = (subseg) NULL;
4699  newsubseg->ss[5] = (subseg) NULL;
4700  /* Initialize the two adjoining triangles to be "outer space." */
4701  newsubseg->ss[6] = (subseg) m->dummytri;
4702  newsubseg->ss[7] = (subseg) m->dummytri;
4703  /* Set the boundary marker to zero. */
4704  setmark(*newsubseg, 0);
4705 
4706  newsubseg->ssorient = 0;
4707 }
4708 
4709 /** **/
4710 /** **/
4711 /********* Constructors end here *********/
4712 
4713 /********* Geometric primitives begin here *********/
4714 /** **/
4715 /** **/
4716 
4717 /* The adaptive exact arithmetic geometric predicates implemented herein are */
4718 /* described in detail in my paper, "Adaptive Precision Floating-Point */
4719 /* Arithmetic and Fast Robust Geometric Predicates." See the header for a */
4720 /* full citation. */
4721 
4722 /* Which of the following two methods of finding the absolute values is */
4723 /* fastest is compiler-dependent. A few compilers can inline and optimize */
4724 /* the fabs() call; but most will incur the overhead of a function call, */
4725 /* which is disastrously slow. A faster way on IEEE machines might be to */
4726 /* mask the appropriate bit, but that's difficult to do in C without */
4727 /* forcing the value to be stored to memory (rather than be kept in the */
4728 /* register to which the optimizer assigned it). */
4729 
4730 #define Absolute(a) ((a) >= 0.0 ? (a) : -(a))
4731 /* #define Absolute(a) fabs(a) */
4732 
4733 /* Many of the operations are broken up into two pieces, a main part that */
4734 /* performs an approximate operation, and a "tail" that computes the */
4735 /* roundoff error of that operation. */
4736 /* */
4737 /* The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(), */
4738 /* Split(), and Two_Product() are all implemented as described in the */
4739 /* reference. Each of these macros requires certain variables to be */
4740 /* defined in the calling routine. The variables `bvirt', `c', `abig', */
4741 /* `_i', `_j', `_k', `_l', `_m', and `_n' are declared `INEXACT' because */
4742 /* they store the result of an operation that may incur roundoff error. */
4743 /* The input parameter `x' (or the highest numbered `x_' parameter) must */
4744 /* also be declared `INEXACT'. */
4745 
4746 #define Fast_Two_Sum_Tail(a, b, x, y) \
4747  bvirt = x - a; \
4748  y = b - bvirt
4749 
4750 #define Fast_Two_Sum(a, b, x, y) \
4751  x = (REAL) (a + b); \
4752  Fast_Two_Sum_Tail(a, b, x, y)
4753 
4754 #define Two_Sum_Tail(a, b, x, y) \
4755  bvirt = (REAL) (x - a); \
4756  avirt = x - bvirt; \
4757  bround = b - bvirt; \
4758  around = a - avirt; \
4759  y = around + bround
4760 
4761 #define Two_Sum(a, b, x, y) \
4762  x = (REAL) (a + b); \
4763  Two_Sum_Tail(a, b, x, y)
4764 
4765 #define Two_Diff_Tail(a, b, x, y) \
4766  bvirt = (REAL) (a - x); \
4767  avirt = x + bvirt; \
4768  bround = bvirt - b; \
4769  around = a - avirt; \
4770  y = around + bround
4771 
4772 #define Two_Diff(a, b, x, y) \
4773  x = (REAL) (a - b); \
4774  Two_Diff_Tail(a, b, x, y)
4775 
4776 #define Split(a, ahi, alo) \
4777  c = (REAL) (splitter * a); \
4778  abig = (REAL) (c - a); \
4779  ahi = c - abig; \
4780  alo = a - ahi
4781 
4782 #define Two_Product_Tail(a, b, x, y) \
4783  Split(a, ahi, alo); \
4784  Split(b, bhi, blo); \
4785  err1 = x - (ahi * bhi); \
4786  err2 = err1 - (alo * bhi); \
4787  err3 = err2 - (ahi * blo); \
4788  y = (alo * blo) - err3
4789 
4790 #define Two_Product(a, b, x, y) \
4791  x = (REAL) (a * b); \
4792  Two_Product_Tail(a, b, x, y)
4793 
4794 /* Two_Product_Presplit() is Two_Product() where one of the inputs has */
4795 /* already been split. Avoids redundant splitting. */
4796 
4797 #define Two_Product_Presplit(a, b, bhi, blo, x, y) \
4798  x = (REAL) (a * b); \
4799  Split(a, ahi, alo); \
4800  err1 = x - (ahi * bhi); \
4801  err2 = err1 - (alo * bhi); \
4802  err3 = err2 - (ahi * blo); \
4803  y = (alo * blo) - err3
4804 
4805 /* Square() can be done more quickly than Two_Product(). */
4806 
4807 #define Square_Tail(a, x, y) \
4808  Split(a, ahi, alo); \
4809  err1 = x - (ahi * ahi); \
4810  err3 = err1 - ((ahi + ahi) * alo); \
4811  y = (alo * alo) - err3
4812 
4813 #define Square(a, x, y) \
4814  x = (REAL) (a * a); \
4815  Square_Tail(a, x, y)
4816 
4817 /* Macros for summing expansions of various fixed lengths. These are all */
4818 /* unrolled versions of Expansion_Sum(). */
4819 
4820 #define Two_One_Sum(a1, a0, b, x2, x1, x0) \
4821  Two_Sum(a0, b , _i, x0); \
4822  Two_Sum(a1, _i, x2, x1)
4823 
4824 #define Two_One_Diff(a1, a0, b, x2, x1, x0) \
4825  Two_Diff(a0, b , _i, x0); \
4826  Two_Sum( a1, _i, x2, x1)
4827 
4828 #define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0) \
4829  Two_One_Sum(a1, a0, b0, _j, _0, x0); \
4830  Two_One_Sum(_j, _0, b1, x3, x2, x1)
4831 
4832 #define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0) \
4833  Two_One_Diff(a1, a0, b0, _j, _0, x0); \
4834  Two_One_Diff(_j, _0, b1, x3, x2, x1)
4835 
4836 /* Macro for multiplying a two-component expansion by a single component. */
4837 
4838 #define Two_One_Product(a1, a0, b, x3, x2, x1, x0) \
4839  Split(b, bhi, blo); \
4840  Two_Product_Presplit(a0, b, bhi, blo, _i, x0); \
4841  Two_Product_Presplit(a1, b, bhi, blo, _j, _0); \
4842  Two_Sum(_i, _0, _k, x1); \
4843  Fast_Two_Sum(_j, _k, x3, x2)
4844 
4845 /*****************************************************************************/
4846 /* */
4847 /* exactinit() Initialize the variables used for exact arithmetic. */
4848 /* */
4849 /* `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in */
4850 /* floating-point arithmetic. `epsilon' bounds the relative roundoff */
4851 /* error. It is used for floating-point error analysis. */
4852 /* */
4853 /* `splitter' is used to split floating-point numbers into two half- */
4854 /* length significands for exact multiplication. */
4855 /* */
4856 /* I imagine that a highly optimizing compiler might be too smart for its */
4857 /* own good, and somehow cause this routine to fail, if it pretends that */
4858 /* floating-point arithmetic is too much like real arithmetic. */
4859 /* */
4860 /* Don't change this routine unless you fully understand it. */
4861 /* */
4862 /*****************************************************************************/
4863 
4865 {
4866  REAL half;
4867  REAL check, lastcheck;
4868  int every_other;
4869 #ifdef LINUX
4870  int cword;
4871 #endif /* LINUX */
4872 
4873 #ifdef CPU86
4874 #ifdef SINGLE
4875  _control87(_PC_24, _MCW_PC); /* Set FPU control word for single precision. */
4876 #else /* not SINGLE */
4877  _control87(_PC_53, _MCW_PC); /* Set FPU control word for double precision. */
4878 #endif /* not SINGLE */
4879 #endif /* CPU86 */
4880 #ifdef LINUX
4881 #ifdef SINGLE
4882  /* cword = 4223; */
4883  cword = 4210; /* set FPU control word for single precision */
4884 #else /* not SINGLE */
4885  /* cword = 4735; */
4886  cword = 4722; /* set FPU control word for double precision */
4887 #endif /* not SINGLE */
4888  _FPU_SETCW(cword);
4889 #endif /* LINUX */
4890 
4891  every_other = 1;
4892  half = 0.5;
4893  epsilon = 1.0;
4894  splitter = 1.0;
4895  check = 1.0;
4896  /* Repeatedly divide `epsilon' by two until it is too small to add to */
4897  /* one without causing roundoff. (Also check if the sum is equal to */
4898  /* the previous sum, for machines that round up instead of using exact */
4899  /* rounding. Not that these routines will work on such machines.) */
4900  do {
4901  lastcheck = check;
4902  epsilon *= half;
4903  if (every_other) {
4904  splitter *= 2.0;
4905  }
4906  every_other = !every_other;
4907  check = 1.0 + epsilon;
4908  } while ((check != 1.0) && (check != lastcheck));
4909  splitter += 1.0;
4910  /* Error bounds for orientation and incircle tests. */
4911  resulterrbound = (3.0 + 8.0 * epsilon) * epsilon;
4912  ccwerrboundA = (3.0 + 16.0 * epsilon) * epsilon;
4913  ccwerrboundB = (2.0 + 12.0 * epsilon) * epsilon;
4914  ccwerrboundC = (9.0 + 64.0 * epsilon) * epsilon * epsilon;
4915  iccerrboundA = (10.0 + 96.0 * epsilon) * epsilon;
4916  iccerrboundB = (4.0 + 48.0 * epsilon) * epsilon;
4917  iccerrboundC = (44.0 + 576.0 * epsilon) * epsilon * epsilon;
4918  o3derrboundA = (7.0 + 56.0 * epsilon) * epsilon;
4919  o3derrboundB = (3.0 + 28.0 * epsilon) * epsilon;
4920  o3derrboundC = (26.0 + 288.0 * epsilon) * epsilon * epsilon;
4921 }
4922 
4923 /*****************************************************************************/
4924 /* */
4925 /* fast_expansion_sum_zeroelim() Sum two expansions, eliminating zero */
4926 /* components from the output expansion. */
4927 /* */
4928 /* Sets h = e + f. See my Robust Predicates paper for details. */
4929 /* */
4930 /* If round-to-even is used (as with IEEE 754), maintains the strongly */
4931 /* nonoverlapping property. (That is, if e is strongly nonoverlapping, h */
4932 /* will be also.) Does NOT maintain the nonoverlapping or nonadjacent */
4933 /* properties. */
4934 /* */
4935 /*****************************************************************************/
4936 
4937 #ifdef ANSI_DECLARATORS
4938 int fast_expansion_sum_zeroelim(int elen, REAL *e, int flen, REAL *f, REAL *h)
4939 #else /* not ANSI_DECLARATORS */
4940 int fast_expansion_sum_zeroelim(elen, e, flen, f, h) /* h cannot be e or f. */
4941 int elen;
4942 REAL *e;
4943 int flen;
4944 REAL *f;
4945 REAL *h;
4946 #endif /* not ANSI_DECLARATORS */
4947 
4948 {
4949  REAL Q;
4950  INEXACT REAL Qnew;
4951  INEXACT REAL hh;
4952  INEXACT REAL bvirt;
4953  REAL avirt, bround, around;
4954  int eindex, findex, hindex;
4955  REAL enow, fnow;
4956 
4957  enow = e[0];
4958  fnow = f[0];
4959  eindex = findex = 0;
4960  if ((fnow > enow) == (fnow > -enow)) {
4961  Q = enow;
4962  enow = e[++eindex];
4963  } else {
4964  Q = fnow;
4965  fnow = f[++findex];
4966  }
4967  hindex = 0;
4968  if ((eindex < elen) && (findex < flen)) {
4969  if ((fnow > enow) == (fnow > -enow)) {
4970  Fast_Two_Sum(enow, Q, Qnew, hh);
4971  enow = e[++eindex];
4972  } else {
4973  Fast_Two_Sum(fnow, Q, Qnew, hh);
4974  fnow = f[++findex];
4975  }
4976  Q = Qnew;
4977  if (hh != 0.0) {
4978  h[hindex++] = hh;
4979  }
4980  while ((eindex < elen) && (findex < flen)) {
4981  if ((fnow > enow) == (fnow > -enow)) {
4982  Two_Sum(Q, enow, Qnew, hh);
4983  enow = e[++eindex];
4984  } else {
4985  Two_Sum(Q, fnow, Qnew, hh);
4986  fnow = f[++findex];
4987  }
4988  Q = Qnew;
4989  if (hh != 0.0) {
4990  h[hindex++] = hh;
4991  }
4992  }
4993  }
4994  while (eindex < elen) {
4995  Two_Sum(Q, enow, Qnew, hh);
4996  enow = e[++eindex];
4997  Q = Qnew;
4998  if (hh != 0.0) {
4999  h[hindex++] = hh;
5000  }
5001  }
5002  while (findex < flen) {
5003  Two_Sum(Q, fnow, Qnew, hh);
5004  fnow = f[++findex];
5005  Q = Qnew;
5006  if (hh != 0.0) {
5007  h[hindex++] = hh;
5008  }
5009  }
5010  if ((Q != 0.0) || (hindex == 0)) {
5011  h[hindex++] = Q;
5012  }
5013  return hindex;
5014 }
5015 
5016 /*****************************************************************************/
5017 /* */
5018 /* scale_expansion_zeroelim() Multiply an expansion by a scalar, */
5019 /* eliminating zero components from the */
5020 /* output expansion. */
5021 /* */
5022 /* Sets h = be. See my Robust Predicates paper for details. */
5023 /* */
5024 /* Maintains the nonoverlapping property. If round-to-even is used (as */
5025 /* with IEEE 754), maintains the strongly nonoverlapping and nonadjacent */
5026 /* properties as well. (That is, if e has one of these properties, so */
5027 /* will h.) */
5028 /* */
5029 /*****************************************************************************/
5030 
5031 #ifdef ANSI_DECLARATORS
5033 #else /* not ANSI_DECLARATORS */
5034 int scale_expansion_zeroelim(elen, e, b, h) /* e and h cannot be the same. */
5035 int elen;
5036 REAL *e;
5037 REAL b;
5038 REAL *h;
5039 #endif /* not ANSI_DECLARATORS */
5040 
5041 {
5042  INEXACT REAL Q, sum;
5043  REAL hh;
5044  INEXACT REAL product1;
5045  REAL product0;
5046  int eindex, hindex;
5047  REAL enow;
5048  INEXACT REAL bvirt;
5049  REAL avirt, bround, around;
5050  INEXACT REAL c;
5051  INEXACT REAL abig;
5052  REAL ahi, alo, bhi, blo;
5053  REAL err1, err2, err3;
5054 
5055  Split(b, bhi, blo);
5056  Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
5057  hindex = 0;
5058  if (hh != 0) {
5059  h[hindex++] = hh;
5060  }
5061  for (eindex = 1; eindex < elen; eindex++) {
5062  enow = e[eindex];
5063  Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
5064  Two_Sum(Q, product0, sum, hh);
5065  if (hh != 0) {
5066  h[hindex++] = hh;
5067  }
5068  Fast_Two_Sum(product1, sum, Q, hh);
5069  if (hh != 0) {
5070  h[hindex++] = hh;
5071  }
5072  }
5073  if ((Q != 0.0) || (hindex == 0)) {
5074  h[hindex++] = Q;
5075  }
5076  return hindex;
5077 }
5078 
5079 /*****************************************************************************/
5080 /* */
5081 /* estimate() Produce a one-word estimate of an expansion's value. */
5082 /* */
5083 /* See my Robust Predicates paper for details. */
5084 /* */
5085 /*****************************************************************************/
5086 
5087 #ifdef ANSI_DECLARATORS
5088 REAL estimate(int elen, REAL *e)
5089 #else /* not ANSI_DECLARATORS */
5090 REAL estimate(elen, e)
5091 int elen;
5092 REAL *e;
5093 #endif /* not ANSI_DECLARATORS */
5094 
5095 {
5096  REAL Q;
5097  int eindex;
5098 
5099  Q = e[0];
5100  for (eindex = 1; eindex < elen; eindex++) {
5101  Q += e[eindex];
5102  }
5103  return Q;
5104 }
5105 
5106 /*****************************************************************************/
5107 /* */
5108 /* counterclockwise() Return a positive value if the points pa, pb, and */
5109 /* pc occur in counterclockwise order; a negative */
5110 /* value if they occur in clockwise order; and zero */
5111 /* if they are collinear. The result is also a rough */
5112 /* approximation of twice the signed area of the */
5113 /* triangle defined by the three points. */
5114 /* */
5115 /* Uses exact arithmetic if necessary to ensure a correct answer. The */
5116 /* result returned is the determinant of a matrix. This determinant is */
5117 /* computed adaptively, in the sense that exact arithmetic is used only to */
5118 /* the degree it is needed to ensure that the returned value has the */
5119 /* correct sign. Hence, this function is usually quite fast, but will run */
5120 /* more slowly when the input points are collinear or nearly so. */
5121 /* */
5122 /* See my Robust Predicates paper for details. */
5123 /* */
5124 /*****************************************************************************/
5125 
5126 #ifdef ANSI_DECLARATORS
5128 #else /* not ANSI_DECLARATORS */
5129 REAL counterclockwiseadapt(pa, pb, pc, detsum)
5130 vertex pa;
5131 vertex pb;
5132 vertex pc;
5133 REAL detsum;
5134 #endif /* not ANSI_DECLARATORS */
5135 
5136 {
5137  INEXACT REAL acx, acy, bcx, bcy;
5138  REAL acxtail, acytail, bcxtail, bcytail;
5139  INEXACT REAL detleft, detright;
5140  REAL detlefttail, detrighttail;
5141  REAL det, errbound;
5142  REAL B[4], C1[8], C2[12], D[16];
5143  INEXACT REAL B3;
5144  int C1length, C2length, Dlength;
5145  REAL u[4];
5146  INEXACT REAL u3;
5147  INEXACT REAL s1, t1;
5148  REAL s0, t0;
5149 
5150  INEXACT REAL bvirt;
5151  REAL avirt, bround, around;
5152  INEXACT REAL c;
5153  INEXACT REAL abig;
5154  REAL ahi, alo, bhi, blo;
5155  REAL err1, err2, err3;
5156  INEXACT REAL _i, _j;
5157  REAL _0;
5158 
5159  acx = (REAL) (pa[0] - pc[0]);
5160  bcx = (REAL) (pb[0] - pc[0]);
5161  acy = (REAL) (pa[1] - pc[1]);
5162  bcy = (REAL) (pb[1] - pc[1]);
5163 
5164  Two_Product(acx, bcy, detleft, detlefttail);
5165  Two_Product(acy, bcx, detright, detrighttail);
5166 
5167  Two_Two_Diff(detleft, detlefttail, detright, detrighttail,
5168  B3, B[2], B[1], B[0]);
5169  B[3] = B3;
5170 
5171  det = estimate(4, B);
5172  errbound = ccwerrboundB * detsum;
5173  if ((det >= errbound) || (-det >= errbound)) {
5174  return det;
5175  }
5176 
5177  Two_Diff_Tail(pa[0], pc[0], acx, acxtail);
5178  Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);
5179  Two_Diff_Tail(pa[1], pc[1], acy, acytail);
5180  Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);
5181 
5182  if ((acxtail == 0.0) && (acytail == 0.0)
5183  && (bcxtail == 0.0) && (bcytail == 0.0)) {
5184  return det;
5185  }
5186 
5187  errbound = ccwerrboundC * detsum + resulterrbound * Absolute(det);
5188  det += (acx * bcytail + bcy * acxtail)
5189  - (acy * bcxtail + bcx * acytail);
5190  if ((det >= errbound) || (-det >= errbound)) {
5191  return det;
5192  }
5193 
5194  Two_Product(acxtail, bcy, s1, s0);
5195  Two_Product(acytail, bcx, t1, t0);
5196  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
5197  u[3] = u3;
5198  C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);
5199 
5200  Two_Product(acx, bcytail, s1, s0);
5201  Two_Product(acy, bcxtail, t1, t0);
5202  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
5203  u[3] = u3;
5204  C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);
5205 
5206  Two_Product(acxtail, bcytail, s1, s0);
5207  Two_Product(acytail, bcxtail, t1, t0);
5208  Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
5209  u[3] = u3;
5210  Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);
5211 
5212  return(D[Dlength - 1]);
5213 }
5214 
5215 #ifdef ANSI_DECLARATORS
5217  vertex pa, vertex pb, vertex pc)
5218 #else /* not ANSI_DECLARATORS */
5219 REAL counterclockwise(m, b, pa, pb, pc)
5220 struct mesh *m;
5221 struct behavior *b;
5222 vertex pa;
5223 vertex pb;
5224 vertex pc;
5225 #endif /* not ANSI_DECLARATORS */
5226 
5227 {
5228  REAL detleft, detright, det;
5229  REAL detsum, errbound;
5230 
5231  m->counterclockcount++;
5232 
5233  detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);
5234  detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
5235  det = detleft - detright;
5236 
5237  if (b->noexact) {
5238  return det;
5239  }
5240 
5241  if (detleft > 0.0) {
5242  if (detright <= 0.0) {
5243  return det;
5244  } else {
5245  detsum = detleft + detright;
5246  }
5247  } else if (detleft < 0.0) {
5248  if (detright >= 0.0) {
5249  return det;
5250  } else {
5251  detsum = -detleft - detright;
5252  }
5253  } else {
5254  return det;
5255  }
5256 
5257  errbound = ccwerrboundA * detsum;
5258  if ((det >= errbound) || (-det >= errbound)) {
5259  return det;
5260  }
5261 
5262  return counterclockwiseadapt(pa, pb, pc, detsum);
5263 }
5264 
5265 /*****************************************************************************/
5266 /* */
5267 /* incircle() Return a positive value if the point pd lies inside the */
5268 /* circle passing through pa, pb, and pc; a negative value if */
5269 /* it lies outside; and zero if the four points are cocircular.*/
5270 /* The points pa, pb, and pc must be in counterclockwise */
5271 /* order, or the sign of the result will be reversed. */
5272 /* */
5273 /* Uses exact arithmetic if necessary to ensure a correct answer. The */
5274 /* result returned is the determinant of a matrix. This determinant is */
5275 /* computed adaptively, in the sense that exact arithmetic is used only to */
5276 /* the degree it is needed to ensure that the returned value has the */
5277 /* correct sign. Hence, this function is usually quite fast, but will run */
5278 /* more slowly when the input points are cocircular or nearly so. */
5279 /* */
5280 /* See my Robust Predicates paper for details. */
5281 /* */
5282 /*****************************************************************************/
5283 
5284 #ifdef ANSI_DECLARATORS
5286 #else /* not ANSI_DECLARATORS */
5287 REAL incircleadapt(pa, pb, pc, pd, permanent)
5288 vertex pa;
5289 vertex pb;
5290 vertex pc;
5291 vertex pd;
5292 REAL permanent;
5293 #endif /* not ANSI_DECLARATORS */
5294 
5295 {
5296  INEXACT REAL adx, bdx, cdx, ady, bdy, cdy;
5297  REAL det, errbound;
5298 
5299  INEXACT REAL bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
5300  REAL bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
5301  REAL bc[4], ca[4], ab[4];
5302  INEXACT REAL bc3, ca3, ab3;
5303  REAL axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];
5304  int axbclen, axxbclen, aybclen, ayybclen, alen;
5305  REAL bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];
5306  int bxcalen, bxxcalen, bycalen, byycalen, blen;
5307  REAL cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];
5308  int cxablen, cxxablen, cyablen, cyyablen, clen;
5309  REAL abdet[64];
5310  int ablen;
5311  REAL fin1[1152], fin2[1152];
5312  REAL *finnow, *finother, *finswap;
5313  int finlength;
5314 
5315  REAL adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;
5316  INEXACT REAL adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;
5317  REAL adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;
5318  REAL aa[4], bb[4], cc[4];
5319  INEXACT REAL aa3, bb3, cc3;
5320  INEXACT REAL ti1, tj1;
5321  REAL ti0, tj0;
5322  REAL u[4], v[4];
5323  INEXACT REAL u3, v3;
5324  REAL temp8[8], temp16a[16], temp16b[16], temp16c[16];
5325  REAL temp32a[32], temp32b[32], temp48[48], temp64[64];
5326  int temp8len, temp16alen, temp16blen, temp16clen;
5327  int temp32alen, temp32blen, temp48len, temp64len;
5328  REAL axtbb[8], axtcc[8], aytbb[8], aytcc[8];
5329  int axtbblen, axtcclen, aytbblen, aytcclen;
5330  REAL bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];
5331  int bxtaalen, bxtcclen, bytaalen, bytcclen;
5332  REAL cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];
5333  int cxtaalen, cxtbblen, cytaalen, cytbblen;
5334  REAL axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];
5335  int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;
5336  REAL axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16], cytabt[16];
5337  int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;
5338  REAL axtbctt[8], aytbctt[8], bxtcatt[8];
5339  REAL bytcatt[8], cxtabtt[8], cytabtt[8];
5340  int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;
5341  REAL abt[8], bct[8], cat[8];
5342  int abtlen, bctlen, catlen;
5343  REAL abtt[4], bctt[4], catt[4];
5344  int abttlen, bcttlen, cattlen;
5345  INEXACT REAL abtt3, bctt3, catt3;
5346  REAL negate;
5347 
5348  INEXACT REAL bvirt;
5349  REAL avirt, bround, around;
5350  INEXACT REAL c;
5351  INEXACT REAL abig;
5352  REAL ahi, alo, bhi, blo;
5353  REAL err1, err2, err3;
5354  INEXACT REAL _i, _j;
5355  REAL _0;
5356 
5357  adx = (REAL) (pa[0] - pd[0]);
5358  bdx = (REAL) (pb[0] - pd[0]);
5359  cdx = (REAL) (pc[0] - pd[0]);
5360  ady = (REAL) (pa[1] - pd[1]);
5361  bdy = (REAL) (pb[1] - pd[1]);
5362  cdy = (REAL) (pc[1] - pd[1]);
5363 
5364  Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
5365  Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
5366  Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
5367  bc[3] = bc3;
5368  axbclen = scale_expansion_zeroelim(4, bc, adx, axbc);
5369  axxbclen = scale_expansion_zeroelim(axbclen, axbc, adx, axxbc);
5370  aybclen = scale_expansion_zeroelim(4, bc, ady, aybc);
5371  ayybclen = scale_expansion_zeroelim(aybclen, aybc, ady, ayybc);
5372  alen = fast_expansion_sum_zeroelim(axxbclen, axxbc, ayybclen, ayybc, adet);
5373 
5374  Two_Product(cdx, ady, cdxady1, cdxady0);
5375  Two_Product(adx, cdy, adxcdy1, adxcdy0);
5376  Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
5377  ca[3] = ca3;
5378  bxcalen = scale_expansion_zeroelim(4, ca, bdx, bxca);
5379  bxxcalen = scale_expansion_zeroelim(bxcalen, bxca, bdx, bxxca);
5380  bycalen = scale_expansion_zeroelim(4, ca, bdy, byca);
5381  byycalen = scale_expansion_zeroelim(bycalen, byca, bdy, byyca);
5382  blen = fast_expansion_sum_zeroelim(bxxcalen, bxxca, byycalen, byyca, bdet);
5383 
5384  Two_Product(adx, bdy, adxbdy1, adxbdy0);
5385  Two_Product(bdx, ady, bdxady1, bdxady0);
5386  Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
5387  ab[3] = ab3;
5388  cxablen = scale_expansion_zeroelim(4, ab, cdx, cxab);
5389  cxxablen = scale_expansion_zeroelim(cxablen, cxab, cdx, cxxab);
5390  cyablen = scale_expansion_zeroelim(4, ab, cdy, cyab);
5391  cyyablen = scale_expansion_zeroelim(cyablen, cyab, cdy, cyyab);
5392  clen = fast_expansion_sum_zeroelim(cxxablen, cxxab, cyyablen, cyyab, cdet);
5393 
5394  ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);
5395  finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);
5396 
5397  det = estimate(finlength, fin1);
5398  errbound = iccerrboundB * permanent;
5399  if ((det >= errbound) || (-det >= errbound)) {
5400  return det;
5401  }
5402 
5403  Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
5404  Two_Diff_Tail(pa[1], pd[1], ady, adytail);
5405  Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
5406  Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
5407  Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
5408  Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
5409  if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0)
5410  && (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0)) {
5411  return det;
5412  }
5413 
5414  errbound = iccerrboundC * permanent + resulterrbound * Absolute(det);
5415  det += ((adx * adx + ady * ady) * ((bdx * cdytail + cdy * bdxtail)
5416  - (bdy * cdxtail + cdx * bdytail))
5417  + 2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx))
5418  + ((bdx * bdx + bdy * bdy) * ((cdx * adytail + ady * cdxtail)
5419  - (cdy * adxtail + adx * cdytail))
5420  + 2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx))
5421  + ((cdx * cdx + cdy * cdy) * ((adx * bdytail + bdy * adxtail)
5422  - (ady * bdxtail + bdx * adytail))
5423  + 2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx));
5424  if ((det >= errbound) || (-det >= errbound)) {
5425  return det;
5426  }
5427 
5428  finnow = fin1;
5429  finother = fin2;
5430 
5431  if ((bdxtail != 0.0) || (bdytail != 0.0)
5432  || (cdxtail != 0.0) || (cdytail != 0.0)) {
5433  Square(adx, adxadx1, adxadx0);
5434  Square(ady, adyady1, adyady0);
5435  Two_Two_Sum(adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);
5436  aa[3] = aa3;
5437  }
5438  if ((cdxtail != 0.0) || (cdytail != 0.0)
5439  || (adxtail != 0.0) || (adytail != 0.0)) {
5440  Square(bdx, bdxbdx1, bdxbdx0);
5441  Square(bdy, bdybdy1, bdybdy0);
5442  Two_Two_Sum(bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);
5443  bb[3] = bb3;
5444  }
5445  if ((adxtail != 0.0) || (adytail != 0.0)
5446  || (bdxtail != 0.0) || (bdytail != 0.0)) {
5447  Square(cdx, cdxcdx1, cdxcdx0);
5448  Square(cdy, cdycdy1, cdycdy0);
5449  Two_Two_Sum(cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);
5450  cc[3] = cc3;
5451  }
5452 
5453  if (adxtail != 0.0) {
5454  axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);
5455  temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx,
5456  temp16a);
5457 
5458  axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);
5459  temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);
5460 
5461  axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);
5462  temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);
5463 
5464  temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5465  temp16blen, temp16b, temp32a);
5466  temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
5467  temp32alen, temp32a, temp48);
5468  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5469  temp48, finother);
5470  finswap = finnow; finnow = finother; finother = finswap;
5471  }
5472  if (adytail != 0.0) {
5473  aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);
5474  temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady,
5475  temp16a);
5476 
5477  aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);
5478  temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);
5479 
5480  aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);
5481  temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);
5482 
5483  temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5484  temp16blen, temp16b, temp32a);
5485  temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
5486  temp32alen, temp32a, temp48);
5487  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5488  temp48, finother);
5489  finswap = finnow; finnow = finother; finother = finswap;
5490  }
5491  if (bdxtail != 0.0) {
5492  bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);
5493  temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx,
5494  temp16a);
5495 
5496  bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);
5497  temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);
5498 
5499  bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);
5500  temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);
5501 
5502  temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5503  temp16blen, temp16b, temp32a);
5504  temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
5505  temp32alen, temp32a, temp48);
5506  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5507  temp48, finother);
5508  finswap = finnow; finnow = finother; finother = finswap;
5509  }
5510  if (bdytail != 0.0) {
5511  bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);
5512  temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy,
5513  temp16a);
5514 
5515  bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);
5516  temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);
5517 
5518  bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);
5519  temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);
5520 
5521  temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5522  temp16blen, temp16b, temp32a);
5523  temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
5524  temp32alen, temp32a, temp48);
5525  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5526  temp48, finother);
5527  finswap = finnow; finnow = finother; finother = finswap;
5528  }
5529  if (cdxtail != 0.0) {
5530  cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);
5531  temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx,
5532  temp16a);
5533 
5534  cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);
5535  temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);
5536 
5537  cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);
5538  temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);
5539 
5540  temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5541  temp16blen, temp16b, temp32a);
5542  temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
5543  temp32alen, temp32a, temp48);
5544  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5545  temp48, finother);
5546  finswap = finnow; finnow = finother; finother = finswap;
5547  }
5548  if (cdytail != 0.0) {
5549  cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);
5550  temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy,
5551  temp16a);
5552 
5553  cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);
5554  temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);
5555 
5556  cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);
5557  temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);
5558 
5559  temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5560  temp16blen, temp16b, temp32a);
5561  temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
5562  temp32alen, temp32a, temp48);
5563  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5564  temp48, finother);
5565  finswap = finnow; finnow = finother; finother = finswap;
5566  }
5567 
5568  if ((adxtail != 0.0) || (adytail != 0.0)) {
5569  if ((bdxtail != 0.0) || (bdytail != 0.0)
5570  || (cdxtail != 0.0) || (cdytail != 0.0)) {
5571  Two_Product(bdxtail, cdy, ti1, ti0);
5572  Two_Product(bdx, cdytail, tj1, tj0);
5573  Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
5574  u[3] = u3;
5575  negate = -bdy;
5576  Two_Product(cdxtail, negate, ti1, ti0);
5577  negate = -bdytail;
5578  Two_Product(cdx, negate, tj1, tj0);
5579  Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
5580  v[3] = v3;
5581  bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);
5582 
5583  Two_Product(bdxtail, cdytail, ti1, ti0);
5584  Two_Product(cdxtail, bdytail, tj1, tj0);
5585  Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);
5586  bctt[3] = bctt3;
5587  bcttlen = 4;
5588  } else {
5589  bct[0] = 0.0;
5590  bctlen = 1;
5591  bctt[0] = 0.0;
5592  bcttlen = 1;
5593  }
5594 
5595  if (adxtail != 0.0) {
5596  temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, adxtail, temp16a);
5597  axtbctlen = scale_expansion_zeroelim(bctlen, bct, adxtail, axtbct);
5598  temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, 2.0 * adx,
5599  temp32a);
5600  temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5601  temp32alen, temp32a, temp48);
5602  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5603  temp48, finother);
5604  finswap = finnow; finnow = finother; finother = finswap;
5605  if (bdytail != 0.0) {
5606  temp8len = scale_expansion_zeroelim(4, cc, adxtail, temp8);
5607  temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
5608  temp16a);
5609  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
5610  temp16a, finother);
5611  finswap = finnow; finnow = finother; finother = finswap;
5612  }
5613  if (cdytail != 0.0) {
5614  temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);
5615  temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
5616  temp16a);
5617  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
5618  temp16a, finother);
5619  finswap = finnow; finnow = finother; finother = finswap;
5620  }
5621 
5622  temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail,
5623  temp32a);
5624  axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);
5625  temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx,
5626  temp16a);
5627  temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail,
5628  temp16b);
5629  temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5630  temp16blen, temp16b, temp32b);
5631  temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
5632  temp32blen, temp32b, temp64);
5633  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
5634  temp64, finother);
5635  finswap = finnow; finnow = finother; finother = finswap;
5636  }
5637  if (adytail != 0.0) {
5638  temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, adytail, temp16a);
5639  aytbctlen = scale_expansion_zeroelim(bctlen, bct, adytail, aytbct);
5640  temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, 2.0 * ady,
5641  temp32a);
5642  temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5643  temp32alen, temp32a, temp48);
5644  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5645  temp48, finother);
5646  finswap = finnow; finnow = finother; finother = finswap;
5647 
5648 
5649  temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail,
5650  temp32a);
5651  aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);
5652  temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady,
5653  temp16a);
5654  temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail,
5655  temp16b);
5656  temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5657  temp16blen, temp16b, temp32b);
5658  temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
5659  temp32blen, temp32b, temp64);
5660  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
5661  temp64, finother);
5662  finswap = finnow; finnow = finother; finother = finswap;
5663  }
5664  }
5665  if ((bdxtail != 0.0) || (bdytail != 0.0)) {
5666  if ((cdxtail != 0.0) || (cdytail != 0.0)
5667  || (adxtail != 0.0) || (adytail != 0.0)) {
5668  Two_Product(cdxtail, ady, ti1, ti0);
5669  Two_Product(cdx, adytail, tj1, tj0);
5670  Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
5671  u[3] = u3;
5672  negate = -cdy;
5673  Two_Product(adxtail, negate, ti1, ti0);
5674  negate = -cdytail;
5675  Two_Product(adx, negate, tj1, tj0);
5676  Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
5677  v[3] = v3;
5678  catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);
5679 
5680  Two_Product(cdxtail, adytail, ti1, ti0);
5681  Two_Product(adxtail, cdytail, tj1, tj0);
5682  Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);
5683  catt[3] = catt3;
5684  cattlen = 4;
5685  } else {
5686  cat[0] = 0.0;
5687  catlen = 1;
5688  catt[0] = 0.0;
5689  cattlen = 1;
5690  }
5691 
5692  if (bdxtail != 0.0) {
5693  temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, bdxtail, temp16a);
5694  bxtcatlen = scale_expansion_zeroelim(catlen, cat, bdxtail, bxtcat);
5695  temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, 2.0 * bdx,
5696  temp32a);
5697  temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5698  temp32alen, temp32a, temp48);
5699  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5700  temp48, finother);
5701  finswap = finnow; finnow = finother; finother = finswap;
5702  if (cdytail != 0.0) {
5703  temp8len = scale_expansion_zeroelim(4, aa, bdxtail, temp8);
5704  temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
5705  temp16a);
5706  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
5707  temp16a, finother);
5708  finswap = finnow; finnow = finother; finother = finswap;
5709  }
5710  if (adytail != 0.0) {
5711  temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);
5712  temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
5713  temp16a);
5714  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
5715  temp16a, finother);
5716  finswap = finnow; finnow = finother; finother = finswap;
5717  }
5718 
5719  temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail,
5720  temp32a);
5721  bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);
5722  temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx,
5723  temp16a);
5724  temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail,
5725  temp16b);
5726  temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5727  temp16blen, temp16b, temp32b);
5728  temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
5729  temp32blen, temp32b, temp64);
5730  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
5731  temp64, finother);
5732  finswap = finnow; finnow = finother; finother = finswap;
5733  }
5734  if (bdytail != 0.0) {
5735  temp16alen = scale_expansion_zeroelim(bytcalen, bytca, bdytail, temp16a);
5736  bytcatlen = scale_expansion_zeroelim(catlen, cat, bdytail, bytcat);
5737  temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, 2.0 * bdy,
5738  temp32a);
5739  temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5740  temp32alen, temp32a, temp48);
5741  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5742  temp48, finother);
5743  finswap = finnow; finnow = finother; finother = finswap;
5744 
5745 
5746  temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail,
5747  temp32a);
5748  bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);
5749  temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy,
5750  temp16a);
5751  temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail,
5752  temp16b);
5753  temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5754  temp16blen, temp16b, temp32b);
5755  temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
5756  temp32blen, temp32b, temp64);
5757  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
5758  temp64, finother);
5759  finswap = finnow; finnow = finother; finother = finswap;
5760  }
5761  }
5762  if ((cdxtail != 0.0) || (cdytail != 0.0)) {
5763  if ((adxtail != 0.0) || (adytail != 0.0)
5764  || (bdxtail != 0.0) || (bdytail != 0.0)) {
5765  Two_Product(adxtail, bdy, ti1, ti0);
5766  Two_Product(adx, bdytail, tj1, tj0);
5767  Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
5768  u[3] = u3;
5769  negate = -ady;
5770  Two_Product(bdxtail, negate, ti1, ti0);
5771  negate = -adytail;
5772  Two_Product(bdx, negate, tj1, tj0);
5773  Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
5774  v[3] = v3;
5775  abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);
5776 
5777  Two_Product(adxtail, bdytail, ti1, ti0);
5778  Two_Product(bdxtail, adytail, tj1, tj0);
5779  Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);
5780  abtt[3] = abtt3;
5781  abttlen = 4;
5782  } else {
5783  abt[0] = 0.0;
5784  abtlen = 1;
5785  abtt[0] = 0.0;
5786  abttlen = 1;
5787  }
5788 
5789  if (cdxtail != 0.0) {
5790  temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, cdxtail, temp16a);
5791  cxtabtlen = scale_expansion_zeroelim(abtlen, abt, cdxtail, cxtabt);
5792  temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, 2.0 * cdx,
5793  temp32a);
5794  temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
5795  temp32alen, temp32a, temp48);
5796  finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
5797  temp48, finother);
5798  finswap = finnow; finnow = finother; finother = finswap;
5799  if (adytail != 0.0) {
5800  temp8len = scale_expansion_zeroelim(4, bb, cdxtail, temp8);
5801  temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
5802  temp16a);
5803  finlength =