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