-#define ANSI_DECLARATORS\r
-/*****************************************************************************/\r
-/* */\r
-/* 888888888 ,o, / 888 */\r
-/* 888 88o88o " o8888o 88o8888o o88888o 888 o88888o */\r
-/* 888 888 888 88b 888 888 888 888 888 d888 88b */\r
-/* 888 888 888 o88^o888 888 888 "88888" 888 8888oo888 */\r
-/* 888 888 888 C888 888 888 888 / 888 q888 */\r
-/* 888 888 888 "88o^888 888 888 Cb 888 "88oooo" */\r
-/* "8oo8D */\r
-/* */\r
-/* A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator. */\r
-/* (triangle.c) */\r
-/* */\r
-/* Version 1.3 */\r
-/* July 19, 1996 */\r
-/* */\r
-/* Copyright 1996 */\r
-/* Jonathan Richard Shewchuk */\r
-/* School of Computer Science */\r
-/* Carnegie Mellon University */\r
-/* 5000 Forbes Avenue */\r
-/* Pittsburgh, Pennsylvania 15213-3891 */\r
-/* jrs@cs.cmu.edu */\r
-/* */\r
-/* This program may be freely redistributed under the condition that the */\r
-/* copyright notices (including this entire header and the copyright */\r
-/* notice printed when the `-h' switch is selected) are not removed, and */\r
-/* no compensation is received. Private, research, and institutional */\r
-/* use is free. You may distribute modified versions of this code UNDER */\r
-/* THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE */\r
-/* SAME FILE REMAIN UNDER COPYRIGHT OF THE ORIGINAL AUTHOR, BOTH SOURCE */\r
-/* AND OBJECT CODE ARE MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR */\r
-/* NOTICE IS GIVEN OF THE MODIFICATIONS. Distribution of this code as */\r
-/* part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT */\r
-/* WITH THE AUTHOR. (If you are not directly supplying this code to a */\r
-/* customer, and you are instead telling them how they can obtain it for */\r
-/* free, then you are not required to make any arrangement with me.) */\r
-/* */\r
-/* Hypertext instructions for Triangle are available on the Web at */\r
-/* */\r
-/* http://www.cs.cmu.edu/~quake/triangle.html */\r
-/* */\r
-/* Some of the references listed below are marked [*]. These are available */\r
-/* for downloading from the Web page */\r
-/* */\r
-/* http://www.cs.cmu.edu/~quake/triangle.research.html */\r
-/* */\r
-/* A paper discussing some aspects of Triangle is available. See Jonathan */\r
-/* Richard Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator */\r
-/* and Delaunay Triangulator," First Workshop on Applied Computational */\r
-/* Geometry, ACM, May 1996. [*] */\r
-/* */\r
-/* Triangle was created as part of the Archimedes project in the School of */\r
-/* Computer Science at Carnegie Mellon University. Archimedes is a */\r
-/* system for compiling parallel finite element solvers. For further */\r
-/* information, see Anja Feldmann, Omar Ghattas, John R. Gilbert, Gary L. */\r
-/* Miller, David R. O'Hallaron, Eric J. Schwabe, Jonathan R. Shewchuk, */\r
-/* and Shang-Hua Teng, "Automated Parallel Solution of Unstructured PDE */\r
-/* Problems." To appear in Communications of the ACM, we hope. */\r
-/* */\r
-/* The quality mesh generation algorithm is due to Jim Ruppert, "A */\r
-/* Delaunay Refinement Algorithm for Quality 2-Dimensional Mesh */\r
-/* Generation," Journal of Algorithms 18(3):548-585, May 1995. [*] */\r
-/* */\r
-/* My implementation of the divide-and-conquer and incremental Delaunay */\r
-/* triangulation algorithms follows closely the presentation of Guibas */\r
-/* and Stolfi, even though I use a triangle-based data structure instead */\r
-/* of their quad-edge data structure. (In fact, I originally implemented */\r
-/* Triangle using the quad-edge data structure, but switching to a */\r
-/* triangle-based data structure sped Triangle by a factor of two.) The */\r
-/* mesh manipulation primitives and the two aforementioned Delaunay */\r
-/* triangulation algorithms are described by Leonidas J. Guibas and Jorge */\r
-/* Stolfi, "Primitives for the Manipulation of General Subdivisions and */\r
-/* the Computation of Voronoi Diagrams," ACM Transactions on Graphics */\r
-/* 4(2):74-123, April 1985. */\r
-/* */\r
-/* Their O(n log n) divide-and-conquer algorithm is adapted from Der-Tsai */\r
-/* Lee and Bruce J. Schachter, "Two Algorithms for Constructing the */\r
-/* Delaunay Triangulation," International Journal of Computer and */\r
-/* Information Science 9(3):219-242, 1980. The idea to improve the */\r
-/* divide-and-conquer algorithm by alternating between vertical and */\r
-/* horizontal cuts was introduced by Rex A. Dwyer, "A Faster Divide-and- */\r
-/* Conquer Algorithm for Constructing Delaunay Triangulations," */\r
-/* Algorithmica 2(2):137-151, 1987. */\r
-/* */\r
-/* The incremental insertion algorithm was first proposed by C. L. Lawson, */\r
-/* "Software for C1 Surface Interpolation," in Mathematical Software III, */\r
-/* John R. Rice, editor, Academic Press, New York, pp. 161-194, 1977. */\r
-/* For point location, I use the algorithm of Ernst P. Mucke, Isaac */\r
-/* Saias, and Binhai Zhu, "Fast Randomized Point Location Without */\r
-/* Preprocessing in Two- and Three-dimensional Delaunay Triangulations," */\r
-/* Proceedings of the Twelfth Annual Symposium on Computational Geometry, */\r
-/* ACM, May 1996. [*] If I were to randomize the order of point */\r
-/* insertion (I currently don't bother), their result combined with the */\r
-/* result of Leonidas J. Guibas, Donald E. Knuth, and Micha Sharir, */\r
-/* "Randomized Incremental Construction of Delaunay and Voronoi */\r
-/* Diagrams," Algorithmica 7(4):381-413, 1992, would yield an expected */\r
-/* O(n^{4/3}) bound on running time. */\r
-/* */\r
-/* The O(n log n) sweepline Delaunay triangulation algorithm is taken from */\r
-/* Steven Fortune, "A Sweepline Algorithm for Voronoi Diagrams", */\r
-/* Algorithmica 2(2):153-174, 1987. A random sample of edges on the */\r
-/* boundary of the triangulation are maintained in a splay tree for the */\r
-/* purpose of point location. Splay trees are described by Daniel */\r
-/* Dominic Sleator and Robert Endre Tarjan, "Self-Adjusting Binary Search */\r
-/* Trees," Journal of the ACM 32(3):652-686, July 1985. */\r
-/* */\r
-/* The algorithms for exact computation of the signs of determinants are */\r
-/* described in Jonathan Richard Shewchuk, "Adaptive Precision Floating- */\r
-/* Point Arithmetic and Fast Robust Geometric Predicates," Technical */\r
-/* Report CMU-CS-96-140, School of Computer Science, Carnegie Mellon */\r
-/* University, Pittsburgh, Pennsylvania, May 1996. [*] (Submitted to */\r
-/* Discrete & Computational Geometry.) An abbreviated version appears as */\r
-/* Jonathan Richard Shewchuk, "Robust Adaptive Floating-Point Geometric */\r
-/* Predicates," Proceedings of the Twelfth Annual Symposium on Computa- */\r
-/* tional Geometry, ACM, May 1996. [*] Many of the ideas for my exact */\r
-/* arithmetic routines originate with Douglas M. Priest, "Algorithms for */\r
-/* Arbitrary Precision Floating Point Arithmetic," Tenth Symposium on */\r
-/* Computer Arithmetic, 132-143, IEEE Computer Society Press, 1991. [*] */\r
-/* Many of the ideas for the correct evaluation of the signs of */\r
-/* determinants are taken from Steven Fortune and Christopher J. Van Wyk, */\r
-/* "Efficient Exact Arithmetic for Computational Geometry," Proceedings */\r
-/* of the Ninth Annual Symposium on Computational Geometry, ACM, */\r
-/* pp. 163-172, May 1993, and from Steven Fortune, "Numerical Stability */\r
-/* of Algorithms for 2D Delaunay Triangulations," International Journal */\r
-/* of Computational Geometry & Applications 5(1-2):193-213, March-June */\r
-/* 1995. */\r
-/* */\r
-/* For definitions of and results involving Delaunay triangulations, */\r
-/* constrained and conforming versions thereof, and other aspects of */\r
-/* triangular mesh generation, see the excellent survey by Marshall Bern */\r
-/* and David Eppstein, "Mesh Generation and Optimal Triangulation," in */\r
-/* Computing and Euclidean Geometry, Ding-Zhu Du and Frank Hwang, */\r
-/* editors, World Scientific, Singapore, pp. 23-90, 1992. */\r
-/* */\r
-/* The time for incrementally adding PSLG (planar straight line graph) */\r
-/* segments to create a constrained Delaunay triangulation is probably */\r
-/* O(n^2) per segment in the worst case and O(n) per edge in the common */\r
-/* case, where n is the number of triangles that intersect the segment */\r
-/* before it is inserted. This doesn't count point location, which can */\r
-/* be much more expensive. (This note does not apply to conforming */\r
-/* Delaunay triangulations, for which a different method is used to */\r
-/* insert segments.) */\r
-/* */\r
-/* The time for adding segments to a conforming Delaunay triangulation is */\r
-/* not clear, but does not depend upon n alone. In some cases, very */\r
-/* small features (like a point lying next to a segment) can cause a */\r
-/* single segment to be split an arbitrary number of times. Of course, */\r
-/* floating-point precision is a practical barrier to how much this can */\r
-/* happen. */\r
-/* */\r
-/* The time for deleting a point from a Delaunay triangulation is O(n^2) in */\r
-/* the worst case and O(n) in the common case, where n is the degree of */\r
-/* the point being deleted. I could improve this to expected O(n) time */\r
-/* by "inserting" the neighboring vertices in random order, but n is */\r
-/* usually quite small, so it's not worth the bother. (The O(n) time */\r
-/* for random insertion follows from L. Paul Chew, "Building Voronoi */\r
-/* Diagrams for Convex Polygons in Linear Expected Time," Technical */\r
-/* Report PCS-TR90-147, Department of Mathematics and Computer Science, */\r
-/* Dartmouth College, 1990. */\r
-/* */\r
-/* Ruppert's Delaunay refinement algorithm typically generates triangles */\r
-/* at a linear rate (constant time per triangle) after the initial */\r
-/* triangulation is formed. There may be pathological cases where more */\r
-/* time is required, but these never arise in practice. */\r
-/* */\r
-/* The segment intersection formulae are straightforward. If you want to */\r
-/* see them derived, see Franklin Antonio. "Faster Line Segment */\r
-/* Intersection." In Graphics Gems III (David Kirk, editor), pp. 199- */\r
-/* 202. Academic Press, Boston, 1992. */\r
-/* */\r
-/* If you make any improvements to this code, please please please let me */\r
-/* know, so that I may obtain the improvements. Even if you don't change */\r
-/* the code, I'd still love to hear what it's being used for. */\r
-/* */\r
-/* Disclaimer: Neither I nor Carnegie Mellon warrant this code in any way */\r
-/* whatsoever. This code is provided "as-is". Use at your own risk. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-/* For single precision (which will save some memory and reduce paging), */\r
-/* define the symbol SINGLE by using the -DSINGLE compiler switch or by */\r
-/* writing "#define SINGLE" below. */\r
-/* */\r
-/* For double precision (which will allow you to refine meshes to a smaller */\r
-/* edge length), leave SINGLE undefined. */\r
-/* */\r
-/* Double precision uses more memory, but improves the resolution of the */\r
-/* meshes you can generate with Triangle. It also reduces the likelihood */\r
-/* of a floating exception due to overflow. Finally, it is much faster */\r
-/* than single precision on 64-bit architectures like the DEC Alpha. I */\r
-/* recommend double precision unless you want to generate a mesh for which */\r
-/* you do not have enough memory. */\r
-\r
-#define SINGLE\r
-\r
-#ifdef SINGLE\r
-#define REAL float\r
-#else /* not SINGLE */\r
-#define REAL double\r
-#endif /* not SINGLE */\r
-\r
-/* If yours is not a Unix system, define the NO_TIMER compiler switch to */\r
-/* remove the Unix-specific timing code. */\r
-\r
-#define NO_TIMER\r
-\r
-/* To insert lots of self-checks for internal errors, define the SELF_CHECK */\r
-/* symbol. This will slow down the program significantly. It is best to */\r
-/* define the symbol using the -DSELF_CHECK compiler switch, but you could */\r
-/* write "#define SELF_CHECK" below. If you are modifying this code, I */\r
-/* recommend you turn self-checks on. */\r
-\r
-/* #define SELF_CHECK */\r
-\r
-/* To compile Triangle as a callable object library (triangle.o), define the */\r
-/* TRILIBRARY symbol. Read the file triangle.h for details on how to call */\r
-/* the procedure triangulate() that results. */\r
-\r
-#define TRILIBRARY\r
-\r
-/* It is possible to generate a smaller version of Triangle using one or */\r
-/* both of the following symbols. Define the REDUCED symbol to eliminate */\r
-/* all features that are primarily of research interest; specifically, the */\r
-/* -i, -F, -s, and -C switches. Define the CDT_ONLY symbol to eliminate */\r
-/* all meshing algorithms above and beyond constrained Delaunay */\r
-/* triangulation; specifically, the -r, -q, -a, -S, and -s switches. */\r
-/* These reductions are most likely to be useful when generating an object */\r
-/* library (triangle.o) by defining the TRILIBRARY symbol. */\r
-\r
-#define REDUCED\r
-#define CDT_ONLY\r
-\r
-/* On some machines, the exact arithmetic routines might be defeated by the */\r
-/* use of internal extended precision floating-point registers. Sometimes */\r
-/* this problem can be fixed by defining certain values to be volatile, */\r
-/* thus forcing them to be stored to memory and rounded off. This isn't */\r
-/* a great solution, though, as it slows Triangle down. */\r
-/* */\r
-/* To try this out, write "#define INEXACT volatile" below. Normally, */\r
-/* however, INEXACT should be defined to be nothing. ("#define INEXACT".) */\r
-\r
-#define INEXACT /* Nothing */\r
-/* #define INEXACT volatile */\r
-\r
-/* Maximum number of characters in a file name (including the null). */\r
-\r
-#define FILENAMESIZE 512\r
-\r
-/* Maximum number of characters in a line read from a file (including the */\r
-/* null). */\r
-\r
-#define INPUTLINESIZE 512\r
-\r
-/* For efficiency, a variety of data structures are allocated in bulk. The */\r
-/* following constants determine how many of each structure is allocated */\r
-/* at once. */\r
-\r
-#define TRIPERBLOCK 4092 /* Number of triangles allocated at once. */\r
-#define SHELLEPERBLOCK 508 /* Number of shell edges allocated at once. */\r
-#define POINTPERBLOCK 4092 /* Number of points allocated at once. */\r
-#define VIRUSPERBLOCK 1020 /* Number of virus triangles allocated at once. */\r
-/* Number of encroached segments allocated at once. */\r
-#define BADSEGMENTPERBLOCK 252\r
-/* Number of skinny triangles allocated at once. */\r
-#define BADTRIPERBLOCK 4092\r
-/* Number of splay tree nodes allocated at once. */\r
-#define SPLAYNODEPERBLOCK 508\r
-\r
-/* The point marker DEADPOINT is an arbitrary number chosen large enough to */\r
-/* (hopefully) not conflict with user boundary markers. Make sure that it */\r
-/* is small enough to fit into your machine's integer size. */\r
-\r
-#define DEADPOINT -1073741824\r
-\r
-/* The next line is used to outsmart some very stupid compilers. If your */\r
-/* compiler is smarter, feel free to replace the "int" with "void". */\r
-/* Not that it matters. */\r
-\r
-#define VOID int\r
-\r
-/* Two constants for algorithms based on random sampling. Both constants */\r
-/* have been chosen empirically to optimize their respective algorithms. */\r
-\r
-/* Used for the point location scheme of Mucke, Saias, and Zhu, to decide */\r
-/* how large a random sample of triangles to inspect. */\r
-#define SAMPLEFACTOR 11\r
-/* Used in Fortune's sweepline Delaunay algorithm to determine what fraction */\r
-/* of boundary edges should be maintained in the splay tree for point */\r
-/* location on the front. */\r
-#define SAMPLERATE 10\r
-\r
-/* A number that speaks for itself, every kissable digit. */\r
-\r
-#define PI 3.141592653589793238462643383279502884197169399375105820974944592308\r
-\r
-/* Another fave. */\r
-\r
-#define SQUAREROOTTWO 1.4142135623730950488016887242096980785696718753769480732\r
-\r
-/* And here's one for those of you who are intimidated by math. */\r
-\r
-#define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <math.h>\r
-#ifndef NO_TIMER\r
-#include <sys/time.h>\r
-#endif /* NO_TIMER */\r
-#ifdef TRILIBRARY\r
-#include "triangle.h"\r
-#endif /* TRILIBRARY */\r
-\r
-/* The following obscenity seems to be necessary to ensure that this program */\r
-/* will port to Dec Alphas running OSF/1, because their stdio.h file commits */\r
-/* the unpardonable sin of including stdlib.h. Hence, malloc(), free(), and */\r
-/* exit() may or may not already be defined at this point. I declare these */\r
-/* functions explicitly because some non-ANSI C compilers lack stdlib.h. */\r
-\r
-#ifndef _STDLIB_H_\r
-extern void *malloc();\r
-extern void free();\r
-extern void exit();\r
-extern double strtod();\r
-extern long strtol();\r
-#endif /* _STDLIB_H_ */\r
-\r
-/* A few forward declarations. */\r
-\r
-void poolrestart();\r
-#ifndef TRILIBRARY\r
-char *readline();\r
-char *findfield();\r
-#endif /* not TRILIBRARY */\r
-\r
-/* Labels that signify whether a record consists primarily of pointers or of */\r
-/* floating-point words. Used to make decisions about data alignment. */\r
-\r
-enum wordtype {POINTER, FLOATINGPOINT};\r
-\r
-/* Labels that signify the result of point location. The result of a */\r
-/* search indicates that the point falls in the interior of a triangle, on */\r
-/* an edge, on a vertex, or outside the mesh. */\r
-\r
-enum locateresult {INTRIANGLE, ONEDGE, ONVERTEX, OUTSIDE};\r
-\r
-/* Labels that signify the result of site insertion. The result indicates */\r
-/* that the point was inserted with complete success, was inserted but */\r
-/* encroaches on a segment, was not inserted because it lies on a segment, */\r
-/* or was not inserted because another point occupies the same location. */\r
-\r
-enum insertsiteresult {SUCCESSFULPOINT, ENCROACHINGPOINT, VIOLATINGPOINT,\r
- DUPLICATEPOINT};\r
-\r
-/* Labels that signify the result of direction finding. The result */\r
-/* indicates that a segment connecting the two query points falls within */\r
-/* the direction triangle, along the left edge of the direction triangle, */\r
-/* or along the right edge of the direction triangle. */\r
-\r
-enum finddirectionresult {WITHIN, LEFTCOLLINEAR, RIGHTCOLLINEAR};\r
-\r
-/* Labels that signify the result of the circumcenter computation routine. */\r
-/* The return value indicates which edge of the triangle is shortest. */\r
-\r
-enum circumcenterresult {OPPOSITEORG, OPPOSITEDEST, OPPOSITEAPEX};\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* The basic mesh data structures */\r
-/* */\r
-/* There are three: points, triangles, and shell edges (abbreviated */\r
-/* `shelle'). These three data structures, linked by pointers, comprise */\r
-/* the mesh. A point simply represents a point in space and its properties.*/\r
-/* A triangle is a triangle. A shell edge is a special data structure used */\r
-/* to represent impenetrable segments in the mesh (including the outer */\r
-/* boundary, boundaries of holes, and internal boundaries separating two */\r
-/* triangulated regions). Shell edges represent boundaries defined by the */\r
-/* user that triangles may not lie across. */\r
-/* */\r
-/* A triangle consists of a list of three vertices, a list of three */\r
-/* adjoining triangles, a list of three adjoining shell edges (when shell */\r
-/* edges are used), an arbitrary number of optional user-defined floating- */\r
-/* point attributes, and an optional area constraint. The latter is an */\r
-/* upper bound on the permissible area of each triangle in a region, used */\r
-/* for mesh refinement. */\r
-/* */\r
-/* For a triangle on a boundary of the mesh, some or all of the neighboring */\r
-/* triangles may not be present. For a triangle in the interior of the */\r
-/* mesh, often no neighboring shell edges are present. Such absent */\r
-/* triangles and shell edges are never represented by NULL pointers; they */\r
-/* are represented by two special records: `dummytri', the triangle that */\r
-/* fills "outer space", and `dummysh', the omnipresent shell edge. */\r
-/* `dummytri' and `dummysh' are used for several reasons; for instance, */\r
-/* they can be dereferenced and their contents examined without causing the */\r
-/* memory protection exception that would occur if NULL were dereferenced. */\r
-/* */\r
-/* However, it is important to understand that a triangle includes other */\r
-/* information as well. The pointers to adjoining vertices, triangles, and */\r
-/* shell edges are ordered in a way that indicates their geometric relation */\r
-/* to each other. Furthermore, each of these pointers contains orientation */\r
-/* information. Each pointer to an adjoining triangle indicates which face */\r
-/* of that triangle is contacted. Similarly, each pointer to an adjoining */\r
-/* shell edge indicates which side of that shell edge is contacted, and how */\r
-/* the shell edge is oriented relative to the triangle. */\r
-/* */\r
-/* Shell edges are found abutting edges of triangles; either sandwiched */\r
-/* between two triangles, or resting against one triangle on an exterior */\r
-/* boundary or hole boundary. */\r
-/* */\r
-/* A shell edge consists of a list of two vertices, a list of two */\r
-/* adjoining shell edges, and a list of two adjoining triangles. One of */\r
-/* the two adjoining triangles may not be present (though there should */\r
-/* always be one), and neighboring shell edges might not be present. */\r
-/* Shell edges also store a user-defined integer "boundary marker". */\r
-/* Typically, this integer is used to indicate what sort of boundary */\r
-/* conditions are to be applied at that location in a finite element */\r
-/* simulation. */\r
-/* */\r
-/* Like triangles, shell edges maintain information about the relative */\r
-/* orientation of neighboring objects. */\r
-/* */\r
-/* Points are relatively simple. A point is a list of floating point */\r
-/* numbers, starting with the x, and y coordinates, followed by an */\r
-/* arbitrary number of optional user-defined floating-point attributes, */\r
-/* followed by an integer boundary marker. During the segment insertion */\r
-/* phase, there is also a pointer from each point to a triangle that may */\r
-/* contain it. Each pointer is not always correct, but when one is, it */\r
-/* speeds up segment insertion. These pointers are assigned values once */\r
-/* at the beginning of the segment insertion phase, and are not used or */\r
-/* updated at any other time. Edge swapping during segment insertion will */\r
-/* render some of them incorrect. Hence, don't rely upon them for */\r
-/* anything. For the most part, points do not have any information about */\r
-/* what triangles or shell edges they are linked to. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* Handles */\r
-/* */\r
-/* The oriented triangle (`triedge') and oriented shell edge (`edge') data */\r
-/* structures defined below do not themselves store any part of the mesh. */\r
-/* The mesh itself is made of `triangle's, `shelle's, and `point's. */\r
-/* */\r
-/* Oriented triangles and oriented shell edges will usually be referred to */\r
-/* as "handles". A handle is essentially a pointer into the mesh; it */\r
-/* allows you to "hold" one particular part of the mesh. Handles are used */\r
-/* to specify the regions in which one is traversing and modifying the mesh.*/\r
-/* A single `triangle' may be held by many handles, or none at all. (The */\r
-/* latter case is not a memory leak, because the triangle is still */\r
-/* connected to other triangles in the mesh.) */\r
-/* */\r
-/* A `triedge' is a handle that holds a triangle. It holds a specific side */\r
-/* of the triangle. An `edge' is a handle that holds a shell edge. It */\r
-/* holds either the left or right side of the edge. */\r
-/* */\r
-/* Navigation about the mesh is accomplished through a set of mesh */\r
-/* manipulation primitives, further below. Many of these primitives take */\r
-/* a handle and produce a new handle that holds the mesh near the first */\r
-/* handle. Other primitives take two handles and glue the corresponding */\r
-/* parts of the mesh together. The exact position of the handles is */\r
-/* important. For instance, when two triangles are glued together by the */\r
-/* bond() primitive, they are glued by the sides on which the handles lie. */\r
-/* */\r
-/* Because points have no information about which triangles they are */\r
-/* attached to, I commonly represent a point by use of a handle whose */\r
-/* origin is the point. A single handle can simultaneously represent a */\r
-/* triangle, an edge, and a point. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-/* The triangle data structure. Each triangle contains three pointers to */\r
-/* adjoining triangles, plus three pointers to vertex points, plus three */\r
-/* pointers to shell edges (defined below; these pointers are usually */\r
-/* `dummysh'). It may or may not also contain user-defined attributes */\r
-/* and/or a floating-point "area constraint". It may also contain extra */\r
-/* pointers for nodes, when the user asks for high-order elements. */\r
-/* Because the size and structure of a `triangle' is not decided until */\r
-/* runtime, I haven't simply defined the type `triangle' to be a struct. */\r
-\r
-typedef REAL **triangle; /* Really: typedef triangle *triangle */\r
-\r
-/* An oriented triangle: includes a pointer to a triangle and orientation. */\r
-/* The orientation denotes an edge of the triangle. Hence, there are */\r
-/* three possible orientations. By convention, each edge is always */\r
-/* directed to point counterclockwise about the corresponding triangle. */\r
-\r
-struct triedge {\r
- triangle *tri;\r
- int orient; /* Ranges from 0 to 2. */\r
-};\r
-\r
-/* The shell data structure. Each shell edge contains two pointers to */\r
-/* adjoining shell edges, plus two pointers to vertex points, plus two */\r
-/* pointers to adjoining triangles, plus one shell marker. */\r
-\r
-typedef REAL **shelle; /* Really: typedef shelle *shelle */\r
-\r
-/* An oriented shell edge: includes a pointer to a shell edge and an */\r
-/* orientation. The orientation denotes a side of the edge. Hence, there */\r
-/* are two possible orientations. By convention, the edge is always */\r
-/* directed so that the "side" denoted is the right side of the edge. */\r
-\r
-struct edge {\r
- shelle *sh;\r
- int shorient; /* Ranges from 0 to 1. */\r
-};\r
-\r
-/* The point data structure. Each point is actually an array of REALs. */\r
-/* The number of REALs is unknown until runtime. An integer boundary */\r
-/* marker, and sometimes a pointer to a triangle, is appended after the */\r
-/* REALs. */\r
-\r
-typedef REAL *point;\r
-\r
-/* A queue used to store encroached segments. Each segment's vertices are */\r
-/* stored so that one can check whether a segment is still the same. */\r
-\r
-struct badsegment {\r
- struct edge encsegment; /* An encroached segment. */\r
- point segorg, segdest; /* The two vertices. */\r
- struct badsegment *nextsegment; /* Pointer to next encroached segment. */\r
-};\r
-\r
-/* A queue used to store bad triangles. The key is the square of the cosine */\r
-/* of the smallest angle of the triangle. Each triangle's vertices are */\r
-/* stored so that one can check whether a triangle is still the same. */\r
-\r
-struct badface {\r
- struct triedge badfacetri; /* A bad triangle. */\r
- REAL key; /* cos^2 of smallest (apical) angle. */\r
- point faceorg, facedest, faceapex; /* The three vertices. */\r
- struct badface *nextface; /* Pointer to next bad triangle. */\r
-};\r
-\r
-/* A node in a heap used to store events for the sweepline Delaunay */\r
-/* algorithm. Nodes do not point directly to their parents or children in */\r
-/* the heap. Instead, each node knows its position in the heap, and can */\r
-/* look up its parent and children in a separate array. The `eventptr' */\r
-/* points either to a `point' or to a triangle (in encoded format, so that */\r
-/* an orientation is included). In the latter case, the origin of the */\r
-/* oriented triangle is the apex of a "circle event" of the sweepline */\r
-/* algorithm. To distinguish site events from circle events, all circle */\r
-/* events are given an invalid (smaller than `xmin') x-coordinate `xkey'. */\r
-\r
-struct event {\r
- REAL xkey, ykey; /* Coordinates of the event. */\r
- VOID *eventptr; /* Can be a point or the location of a circle event. */\r
- int heapposition; /* Marks this event's position in the heap. */\r
-};\r
-\r
-/* A node in the splay tree. Each node holds an oriented ghost triangle */\r
-/* that represents a boundary edge of the growing triangulation. When a */\r
-/* circle event covers two boundary edges with a triangle, so that they */\r
-/* are no longer boundary edges, those edges are not immediately deleted */\r
-/* from the tree; rather, they are lazily deleted when they are next */\r
-/* encountered. (Since only a random sample of boundary edges are kept */\r
-/* in the tree, lazy deletion is faster.) `keydest' is used to verify */\r
-/* that a triangle is still the same as when it entered the splay tree; if */\r
-/* it has been rotated (due to a circle event), it no longer represents a */\r
-/* boundary edge and should be deleted. */\r
-\r
-struct splaynode {\r
- struct triedge keyedge; /* Lprev of an edge on the front. */\r
- point keydest; /* Used to verify that splay node is still live. */\r
- struct splaynode *lchild, *rchild; /* Children in splay tree. */\r
-};\r
-\r
-/* A type used to allocate memory. firstblock is the first block of items. */\r
-/* nowblock is the block from which items are currently being allocated. */\r
-/* nextitem points to the next slab of free memory for an item. */\r
-/* deaditemstack is the head of a linked list (stack) of deallocated items */\r
-/* that can be recycled. unallocateditems is the number of items that */\r
-/* remain to be allocated from nowblock. */\r
-/* */\r
-/* Traversal is the process of walking through the entire list of items, and */\r
-/* is separate from allocation. Note that a traversal will visit items on */\r
-/* the "deaditemstack" stack as well as live items. pathblock points to */\r
-/* the block currently being traversed. pathitem points to the next item */\r
-/* to be traversed. pathitemsleft is the number of items that remain to */\r
-/* be traversed in pathblock. */\r
-/* */\r
-/* itemwordtype is set to POINTER or FLOATINGPOINT, and is used to suggest */\r
-/* what sort of word the record is primarily made up of. alignbytes */\r
-/* determines how new records should be aligned in memory. itembytes and */\r
-/* itemwords are the length of a record in bytes (after rounding up) and */\r
-/* words. itemsperblock is the number of items allocated at once in a */\r
-/* single block. items is the number of currently allocated items. */\r
-/* maxitems is the maximum number of items that have been allocated at */\r
-/* once; it is the current number of items plus the number of records kept */\r
-/* on deaditemstack. */\r
-\r
-struct memorypool {\r
- VOID **firstblock, **nowblock;\r
- VOID *nextitem;\r
- VOID *deaditemstack;\r
- VOID **pathblock;\r
- VOID *pathitem;\r
- enum wordtype itemwordtype;\r
- int alignbytes;\r
- int itembytes, itemwords;\r
- int itemsperblock;\r
- long items, maxitems;\r
- int unallocateditems;\r
- int pathitemsleft;\r
-};\r
-\r
-/* Variables used to allocate memory for triangles, shell edges, points, */\r
-/* viri (triangles being eaten), bad (encroached) segments, bad (skinny */\r
-/* or too large) triangles, and splay tree nodes. */\r
-\r
-static struct memorypool triangles;\r
-static struct memorypool shelles;\r
-static struct memorypool points;\r
-static struct memorypool viri;\r
-static struct memorypool badsegments;\r
-static struct memorypool badtriangles;\r
-static struct memorypool splaynodes;\r
-\r
-/* Variables that maintain the bad triangle queues. The tails are pointers */\r
-/* to the pointers that have to be filled in to enqueue an item. */\r
-\r
-static struct badface *queuefront[64];\r
-static struct badface **queuetail[64];\r
-\r
-static REAL xmin, xmax, ymin, ymax; /* x and y bounds. */\r
-static REAL xminextreme; /* Nonexistent x value used as a flag in sweepline. */\r
-static int inpoints; /* Number of input points. */\r
-static int inelements; /* Number of input triangles. */\r
-static int insegments; /* Number of input segments. */\r
-static int holes; /* Number of input holes. */\r
-static int regions; /* Number of input regions. */\r
-static long edges; /* Number of output edges. */\r
-static int mesh_dim; /* Dimension (ought to be 2). */\r
-static int nextras; /* Number of attributes per point. */\r
-static int eextras; /* Number of attributes per triangle. */\r
-static long hullsize; /* Number of edges of convex hull. */\r
-static int triwords; /* Total words per triangle. */\r
-static int shwords; /* Total words per shell edge. */\r
-static int pointmarkindex; /* Index to find boundary marker of a point. */\r
-static int point2triindex; /* Index to find a triangle adjacent to a point. */\r
-static int highorderindex; /* Index to find extra nodes for high-order elements. */\r
-static int elemattribindex; /* Index to find attributes of a triangle. */\r
-static int areaboundindex; /* Index to find area bound of a triangle. */\r
-static int checksegments; /* Are there segments in the triangulation yet? */\r
-static int readnodefile; /* Has a .node file been read? */\r
-static long samples; /* Number of random samples for point location. */\r
-static unsigned long randomseed; /* Current random number seed. */\r
-\r
-static REAL splitter; /* Used to split REAL factors for exact multiplication. */\r
-static REAL epsilon; /* Floating-point machine epsilon. */\r
-static REAL resulterrbound;\r
-static REAL ccwerrboundA, ccwerrboundB, ccwerrboundC;\r
-static REAL iccerrboundA, iccerrboundB, iccerrboundC;\r
-\r
-static long incirclecount; /* Number of incircle tests performed. */\r
-static long counterclockcount; /* Number of counterclockwise tests performed. */\r
-static long hyperbolacount; /* Number of right-of-hyperbola tests performed. */\r
-static long circumcentercount; /* Number of circumcenter calculations performed. */\r
-static long circletopcount; /* Number of circle top calculations performed. */\r
-\r
-/* Switches for the triangulator. */\r
-/* poly: -p switch. refine: -r switch. */\r
-/* quality: -q switch. */\r
-/* minangle: minimum angle bound, specified after -q switch. */\r
-/* goodangle: cosine squared of minangle. */\r
-/* vararea: -a switch without number. */\r
-/* fixedarea: -a switch with number. */\r
-/* maxarea: maximum area bound, specified after -a switch. */\r
-/* regionattrib: -A switch. convex: -c switch. */\r
-/* firstnumber: inverse of -z switch. All items are numbered starting */\r
-/* from firstnumber. */\r
-/* edgesout: -e switch. voronoi: -v switch. */\r
-/* neighbors: -n switch. geomview: -g switch. */\r
-/* nobound: -B switch. nopolywritten: -P switch. */\r
-/* nonodewritten: -N switch. noelewritten: -E switch. */\r
-/* noiterationnum: -I switch. noholes: -O switch. */\r
-/* noexact: -X switch. */\r
-/* order: element order, specified after -o switch. */\r
-/* nobisect: count of how often -Y switch is selected. */\r
-/* steiner: maximum number of Steiner points, specified after -S switch. */\r
-/* steinerleft: number of Steiner points not yet used. */\r
-/* incremental: -i switch. sweepline: -F switch. */\r
-/* dwyer: inverse of -l switch. */\r
-/* splitseg: -s switch. */\r
-/* docheck: -C switch. */\r
-/* quiet: -Q switch. verbose: count of how often -V switch is selected. */\r
-/* useshelles: -p, -r, -q, or -c switch; determines whether shell edges */\r
-/* are used at all. */\r
-/* */\r
-/* Read the instructions to find out the meaning of these switches. */\r
-\r
-static int poly, refine, quality, vararea, fixedarea, regionattrib, convex;\r
-static int firstnumber;\r
-static int edgesout, voronoi, neighbors, geomview;\r
-static int nobound, nopolywritten, nonodewritten, noelewritten, noiterationnum;\r
-static int noholes, noexact;\r
-static int incremental, sweepline, dwyer;\r
-static int splitseg;\r
-static int docheck;\r
-static int quiet, verbose;\r
-static int useshelles;\r
-static int order;\r
-static int nobisect;\r
-static int steiner, steinerleft;\r
-static REAL minangle, goodangle;\r
-static REAL maxarea;\r
-\r
-/* Variables for file names. */\r
-\r
-#ifndef TRILIBRARY\r
-char innodefilename[FILENAMESIZE];\r
-char inelefilename[FILENAMESIZE];\r
-char inpolyfilename[FILENAMESIZE];\r
-char areafilename[FILENAMESIZE];\r
-char outnodefilename[FILENAMESIZE];\r
-char outelefilename[FILENAMESIZE];\r
-char outpolyfilename[FILENAMESIZE];\r
-char edgefilename[FILENAMESIZE];\r
-char vnodefilename[FILENAMESIZE];\r
-char vedgefilename[FILENAMESIZE];\r
-char neighborfilename[FILENAMESIZE];\r
-char offfilename[FILENAMESIZE];\r
-#endif /* not TRILIBRARY */\r
-\r
-/* Triangular bounding box points. */\r
-\r
-static point infpoint1, infpoint2, infpoint3;\r
-\r
-/* Pointer to the `triangle' that occupies all of "outer space". */\r
-\r
-static triangle *dummytri;\r
-static triangle *dummytribase; /* Keep base address so we can free() it later. */\r
-\r
-/* Pointer to the omnipresent shell edge. Referenced by any triangle or */\r
-/* shell edge that isn't really connected to a shell edge at that */\r
-/* location. */\r
-\r
-static shelle *dummysh;\r
-static shelle *dummyshbase; /* Keep base address so we can free() it later. */\r
-\r
-/* Pointer to a recently visited triangle. Improves point location if */\r
-/* proximate points are inserted sequentially. */\r
-\r
-static struct triedge recenttri;\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* Mesh manipulation primitives. Each triangle contains three pointers to */\r
-/* other triangles, with orientations. Each pointer points not to the */\r
-/* first byte of a triangle, but to one of the first three bytes of a */\r
-/* triangle. It is necessary to extract both the triangle itself and the */\r
-/* orientation. To save memory, I keep both pieces of information in one */\r
-/* pointer. To make this possible, I assume that all triangles are aligned */\r
-/* to four-byte boundaries. The `decode' routine below decodes a pointer, */\r
-/* extracting an orientation (in the range 0 to 2) and a pointer to the */\r
-/* beginning of a triangle. The `encode' routine compresses a pointer to a */\r
-/* triangle and an orientation into a single pointer. My assumptions that */\r
-/* triangles are four-byte-aligned and that the `unsigned long' type is */\r
-/* long enough to hold a pointer are two of the few kludges in this program.*/\r
-/* */\r
-/* Shell edges are manipulated similarly. A pointer to a shell edge */\r
-/* carries both an address and an orientation in the range 0 to 1. */\r
-/* */\r
-/* The other primitives take an oriented triangle or oriented shell edge, */\r
-/* and return an oriented triangle or oriented shell edge or point; or they */\r
-/* change the connections in the data structure. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-/********* Mesh manipulation primitives begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/* Fast lookup arrays to speed some of the mesh manipulation primitives. */\r
-\r
-int plus1mod3[3] = {1, 2, 0};\r
-int minus1mod3[3] = {2, 0, 1};\r
-\r
-/********* Primitives for triangles *********/\r
-/* */\r
-/* */\r
-\r
-/* decode() converts a pointer to an oriented triangle. The orientation is */\r
-/* extracted from the two least significant bits of the pointer. */\r
-\r
-#define decode(ptr, triedge) \\r
- (triedge).orient = (int) ((unsigned long) (ptr) & (unsigned long) 3l); \\r
- (triedge).tri = (triangle *) \\r
- ((unsigned long) (ptr) ^ (unsigned long) (triedge).orient)\r
-\r
-/* encode() compresses an oriented triangle into a single pointer. It */\r
-/* relies on the assumption that all triangles are aligned to four-byte */\r
-/* boundaries, so the two least significant bits of (triedge).tri are zero.*/\r
-\r
-#define encode(triedge) \\r
- (triangle) ((unsigned long) (triedge).tri | (unsigned long) (triedge).orient)\r
-\r
-/* The following edge manipulation primitives are all described by Guibas */\r
-/* and Stolfi. However, they use an edge-based data structure, whereas I */\r
-/* am using a triangle-based data structure. */\r
-\r
-/* sym() finds the abutting triangle, on the same edge. Note that the */\r
-/* edge direction is necessarily reversed, because triangle/edge handles */\r
-/* are always directed counterclockwise around the triangle. */\r
-\r
-#define sym(triedge1, triedge2) \\r
- ptr = (triedge1).tri[(triedge1).orient]; \\r
- decode(ptr, triedge2);\r
-\r
-#define symself(triedge) \\r
- ptr = (triedge).tri[(triedge).orient]; \\r
- decode(ptr, triedge);\r
-\r
-/* lnext() finds the next edge (counterclockwise) of a triangle. */\r
-\r
-#define lnext(triedge1, triedge2) \\r
- (triedge2).tri = (triedge1).tri; \\r
- (triedge2).orient = plus1mod3[(triedge1).orient]\r
-\r
-#define lnextself(triedge) \\r
- (triedge).orient = plus1mod3[(triedge).orient]\r
-\r
-/* lprev() finds the previous edge (clockwise) of a triangle. */\r
-\r
-#define lprev(triedge1, triedge2) \\r
- (triedge2).tri = (triedge1).tri; \\r
- (triedge2).orient = minus1mod3[(triedge1).orient]\r
-\r
-#define lprevself(triedge) \\r
- (triedge).orient = minus1mod3[(triedge).orient]\r
-\r
-/* onext() spins counterclockwise around a point; that is, it finds the next */\r
-/* edge with the same origin in the counterclockwise direction. This edge */\r
-/* will be part of a different triangle. */\r
-\r
-#define onext(triedge1, triedge2) \\r
- lprev(triedge1, triedge2); \\r
- symself(triedge2);\r
-\r
-#define onextself(triedge) \\r
- lprevself(triedge); \\r
- symself(triedge);\r
-\r
-/* oprev() spins clockwise around a point; that is, it finds the next edge */\r
-/* with the same origin in the clockwise direction. This edge will be */\r
-/* part of a different triangle. */\r
-\r
-#define oprev(triedge1, triedge2) \\r
- sym(triedge1, triedge2); \\r
- lnextself(triedge2);\r
-\r
-#define oprevself(triedge) \\r
- symself(triedge); \\r
- lnextself(triedge);\r
-\r
-/* dnext() spins counterclockwise around a point; that is, it finds the next */\r
-/* edge with the same destination in the counterclockwise direction. This */\r
-/* edge will be part of a different triangle. */\r
-\r
-#define dnext(triedge1, triedge2) \\r
- sym(triedge1, triedge2); \\r
- lprevself(triedge2);\r
-\r
-#define dnextself(triedge) \\r
- symself(triedge); \\r
- lprevself(triedge);\r
-\r
-/* dprev() spins clockwise around a point; that is, it finds the next edge */\r
-/* with the same destination in the clockwise direction. This edge will */\r
-/* be part of a different triangle. */\r
-\r
-#define dprev(triedge1, triedge2) \\r
- lnext(triedge1, triedge2); \\r
- symself(triedge2);\r
-\r
-#define dprevself(triedge) \\r
- lnextself(triedge); \\r
- symself(triedge);\r
-\r
-/* rnext() moves one edge counterclockwise about the adjacent triangle. */\r
-/* (It's best understood by reading Guibas and Stolfi. It involves */\r
-/* changing triangles twice.) */\r
-\r
-#define rnext(triedge1, triedge2) \\r
- sym(triedge1, triedge2); \\r
- lnextself(triedge2); \\r
- symself(triedge2);\r
-\r
-#define rnextself(triedge) \\r
- symself(triedge); \\r
- lnextself(triedge); \\r
- symself(triedge);\r
-\r
-/* rnext() moves one edge clockwise about the adjacent triangle. */\r
-/* (It's best understood by reading Guibas and Stolfi. It involves */\r
-/* changing triangles twice.) */\r
-\r
-#define rprev(triedge1, triedge2) \\r
- sym(triedge1, triedge2); \\r
- lprevself(triedge2); \\r
- symself(triedge2);\r
-\r
-#define rprevself(triedge) \\r
- symself(triedge); \\r
- lprevself(triedge); \\r
- symself(triedge);\r
-\r
-/* These primitives determine or set the origin, destination, or apex of a */\r
-/* triangle. */\r
-\r
-#define org(triedge, pointptr) \\r
- pointptr = (point) (triedge).tri[plus1mod3[(triedge).orient] + 3]\r
-\r
-#define dest(triedge, pointptr) \\r
- pointptr = (point) (triedge).tri[minus1mod3[(triedge).orient] + 3]\r
-\r
-#define apex(triedge, pointptr) \\r
- pointptr = (point) (triedge).tri[(triedge).orient + 3]\r
-\r
-#define setorg(triedge, pointptr) \\r
- (triedge).tri[plus1mod3[(triedge).orient] + 3] = (triangle) pointptr\r
-\r
-#define setdest(triedge, pointptr) \\r
- (triedge).tri[minus1mod3[(triedge).orient] + 3] = (triangle) pointptr\r
-\r
-#define setapex(triedge, pointptr) \\r
- (triedge).tri[(triedge).orient + 3] = (triangle) pointptr\r
-\r
-#define setvertices2null(triedge) \\r
- (triedge).tri[3] = (triangle) NULL; \\r
- (triedge).tri[4] = (triangle) NULL; \\r
- (triedge).tri[5] = (triangle) NULL;\r
-\r
-/* Bond two triangles together. */\r
-\r
-#define bond(triedge1, triedge2) \\r
- (triedge1).tri[(triedge1).orient] = encode(triedge2); \\r
- (triedge2).tri[(triedge2).orient] = encode(triedge1)\r
-\r
-/* Dissolve a bond (from one side). Note that the other triangle will still */\r
-/* think it's connected to this triangle. Usually, however, the other */\r
-/* triangle is being deleted entirely, or bonded to another triangle, so */\r
-/* it doesn't matter. */\r
-\r
-#define dissolve(triedge) \\r
- (triedge).tri[(triedge).orient] = (triangle) dummytri\r
-\r
-/* Copy a triangle/edge handle. */\r
-\r
-#define triedgecopy(triedge1, triedge2) \\r
- (triedge2).tri = (triedge1).tri; \\r
- (triedge2).orient = (triedge1).orient\r
-\r
-/* Test for equality of triangle/edge handles. */\r
-\r
-#define triedgeequal(triedge1, triedge2) \\r
- (((triedge1).tri == (triedge2).tri) && \\r
- ((triedge1).orient == (triedge2).orient))\r
-\r
-/* Primitives to infect or cure a triangle with the virus. These rely on */\r
-/* the assumption that all shell edges are aligned to four-byte boundaries.*/\r
-\r
-#define infect(triedge) \\r
- (triedge).tri[6] = (triangle) \\r
- ((unsigned long) (triedge).tri[6] | (unsigned long) 2l)\r
-\r
-#define uninfect(triedge) \\r
- (triedge).tri[6] = (triangle) \\r
- ((unsigned long) (triedge).tri[6] & ~ (unsigned long) 2l)\r
-\r
-/* Test a triangle for viral infection. */\r
-\r
-#define infected(triedge) \\r
- (((unsigned long) (triedge).tri[6] & (unsigned long) 2l) != 0)\r
-\r
-/* Check or set a triangle's attributes. */\r
-\r
-#define elemattribute(triedge, attnum) \\r
- ((REAL *) (triedge).tri)[elemattribindex + (attnum)]\r
-\r
-#define setelemattribute(triedge, attnum, value) \\r
- ((REAL *) (triedge).tri)[elemattribindex + (attnum)] = (REAL)value\r
-\r
-/* Check or set a triangle's maximum area bound. */\r
-\r
-#define areabound(triedge) ((REAL *) (triedge).tri)[areaboundindex]\r
-\r
-#define setareabound(triedge, value) \\r
- ((REAL *) (triedge).tri)[areaboundindex] = (REAL)value\r
-\r
-/********* Primitives for shell edges *********/\r
-/* */\r
-/* */\r
-\r
-/* sdecode() converts a pointer to an oriented shell edge. The orientation */\r
-/* is extracted from the least significant bit of the pointer. The two */\r
-/* least significant bits (one for orientation, one for viral infection) */\r
-/* are masked out to produce the real pointer. */\r
-\r
-#define sdecode(sptr, edge) \\r
- (edge).shorient = (int) ((unsigned long) (sptr) & (unsigned long) 1l); \\r
- (edge).sh = (shelle *) \\r
- ((unsigned long) (sptr) & ~ (unsigned long) 3l)\r
-\r
-/* sencode() compresses an oriented shell edge into a single pointer. It */\r
-/* relies on the assumption that all shell edges are aligned to two-byte */\r
-/* boundaries, so the least significant bit of (edge).sh is zero. */\r
-\r
-#define sencode(edge) \\r
- (shelle) ((unsigned long) (edge).sh | (unsigned long) (edge).shorient)\r
-\r
-/* ssym() toggles the orientation of a shell edge. */\r
-\r
-#define ssym(edge1, edge2) \\r
- (edge2).sh = (edge1).sh; \\r
- (edge2).shorient = 1 - (edge1).shorient\r
-\r
-#define ssymself(edge) \\r
- (edge).shorient = 1 - (edge).shorient\r
-\r
-/* spivot() finds the other shell edge (from the same segment) that shares */\r
-/* the same origin. */\r
-\r
-#define spivot(edge1, edge2) \\r
- sptr = (edge1).sh[(edge1).shorient]; \\r
- sdecode(sptr, edge2)\r
-\r
-#define spivotself(edge) \\r
- sptr = (edge).sh[(edge).shorient]; \\r
- sdecode(sptr, edge)\r
-\r
-/* snext() finds the next shell edge (from the same segment) in sequence; */\r
-/* one whose origin is the input shell edge's destination. */\r
-\r
-#define snext(edge1, edge2) \\r
- sptr = (edge1).sh[1 - (edge1).shorient]; \\r
- sdecode(sptr, edge2)\r
-\r
-#define snextself(edge) \\r
- sptr = (edge).sh[1 - (edge).shorient]; \\r
- sdecode(sptr, edge)\r
-\r
-/* These primitives determine or set the origin or destination of a shell */\r
-/* edge. */\r
-\r
-#define sorg(edge, pointptr) \\r
- pointptr = (point) (edge).sh[2 + (edge).shorient]\r
-\r
-#define sdest(edge, pointptr) \\r
- pointptr = (point) (edge).sh[3 - (edge).shorient]\r
-\r
-#define setsorg(edge, pointptr) \\r
- (edge).sh[2 + (edge).shorient] = (shelle) pointptr\r
-\r
-#define setsdest(edge, pointptr) \\r
- (edge).sh[3 - (edge).shorient] = (shelle) pointptr\r
-\r
-/* These primitives read or set a shell marker. Shell markers are used to */\r
-/* hold user boundary information. */\r
-\r
-#define mark(edge) (* (int *) ((edge).sh + 6))\r
-\r
-#define setmark(edge, value) \\r
- * (int *) ((edge).sh + 6) = value\r
-\r
-/* Bond two shell edges together. */\r
-\r
-#define sbond(edge1, edge2) \\r
- (edge1).sh[(edge1).shorient] = sencode(edge2); \\r
- (edge2).sh[(edge2).shorient] = sencode(edge1)\r
-\r
-/* Dissolve a shell edge bond (from one side). Note that the other shell */\r
-/* edge will still think it's connected to this shell edge. */\r
-\r
-#define sdissolve(edge) \\r
- (edge).sh[(edge).shorient] = (shelle) dummysh\r
-\r
-/* Copy a shell edge. */\r
-\r
-#define shellecopy(edge1, edge2) \\r
- (edge2).sh = (edge1).sh; \\r
- (edge2).shorient = (edge1).shorient\r
-\r
-/* Test for equality of shell edges. */\r
-\r
-#define shelleequal(edge1, edge2) \\r
- (((edge1).sh == (edge2).sh) && \\r
- ((edge1).shorient == (edge2).shorient))\r
-\r
-/********* Primitives for interacting triangles and shell edges *********/\r
-/* */\r
-/* */\r
-\r
-/* tspivot() finds a shell edge abutting a triangle. */\r
-\r
-#define tspivot(triedge, edge) \\r
- sptr = (shelle) (triedge).tri[6 + (triedge).orient]; \\r
- sdecode(sptr, edge)\r
-\r
-/* stpivot() finds a triangle abutting a shell edge. It requires that the */\r
-/* variable `ptr' of type `triangle' be defined. */\r
-\r
-#define stpivot(edge, triedge) \\r
- ptr = (triangle) (edge).sh[4 + (edge).shorient]; \\r
- decode(ptr, triedge)\r
-\r
-/* Bond a triangle to a shell edge. */\r
-\r
-#define tsbond(triedge, edge) \\r
- (triedge).tri[6 + (triedge).orient] = (triangle) sencode(edge); \\r
- (edge).sh[4 + (edge).shorient] = (shelle) encode(triedge)\r
-\r
-/* Dissolve a bond (from the triangle side). */\r
-\r
-#define tsdissolve(triedge) \\r
- (triedge).tri[6 + (triedge).orient] = (triangle) dummysh\r
-\r
-/* Dissolve a bond (from the shell edge side). */\r
-\r
-#define stdissolve(edge) \\r
- (edge).sh[4 + (edge).shorient] = (shelle) dummytri\r
-\r
-/********* Primitives for points *********/\r
-/* */\r
-/* */\r
-\r
-#define pointmark(pt) ((int *) (pt))[pointmarkindex]\r
-\r
-#define setpointmark(pt, value) \\r
- ((int *) (pt))[pointmarkindex] = value\r
-\r
-#define point2tri(pt) ((triangle *) (pt))[point2triindex]\r
-\r
-#define setpoint2tri(pt, value) \\r
- ((triangle *) (pt))[point2triindex] = value\r
-\r
-/** **/\r
-/** **/\r
-/********* Mesh manipulation primitives end here *********/\r
-\r
-/********* User interaction routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* syntax() Print list of command line switches. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-void syntax()\r
-{\r
-#ifdef CDT_ONLY\r
-#ifdef REDUCED\r
- printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n");\r
-#else /* not REDUCED */\r
- printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n");\r
-#endif /* not REDUCED */\r
-#else /* not CDT_ONLY */\r
-#ifdef REDUCED\r
- printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n");\r
-#else /* not REDUCED */\r
- printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n");\r
-#endif /* not REDUCED */\r
-#endif /* not CDT_ONLY */\r
-\r
- printf(" -p Triangulates a Planar Straight Line Graph (.poly file).\n");\r
-#ifndef CDT_ONLY\r
- printf(" -r Refines a previously generated mesh.\n");\r
- printf(\r
- " -q Quality mesh generation. A minimum angle may be specified.\n");\r
- printf(" -a Applies a maximum triangle area constraint.\n");\r
-#endif /* not CDT_ONLY */\r
- printf(\r
- " -A Applies attributes to identify elements in certain regions.\n");\r
- printf(" -c Encloses the convex hull with segments.\n");\r
- printf(" -e Generates an edge list.\n");\r
- printf(" -v Generates a Voronoi diagram.\n");\r
- printf(" -n Generates a list of triangle neighbors.\n");\r
- printf(" -g Generates an .off file for Geomview.\n");\r
- printf(" -B Suppresses output of boundary information.\n");\r
- printf(" -P Suppresses output of .poly file.\n");\r
- printf(" -N Suppresses output of .node file.\n");\r
- printf(" -E Suppresses output of .ele file.\n");\r
- printf(" -I Suppresses mesh iteration numbers.\n");\r
- printf(" -O Ignores holes in .poly file.\n");\r
- printf(" -X Suppresses use of exact arithmetic.\n");\r
- printf(" -z Numbers all items starting from zero (rather than one).\n");\r
- printf(" -o2 Generates second-order subparametric elements.\n");\r
-#ifndef CDT_ONLY\r
- printf(" -Y Suppresses boundary segment splitting.\n");\r
- printf(" -S Specifies maximum number of added Steiner points.\n");\r
-#endif /* not CDT_ONLY */\r
-#ifndef REDUCED\r
- printf(" -i Uses incremental method, rather than divide-and-conquer.\n");\r
- printf(" -F Uses Fortune's sweepline algorithm, rather than d-and-c.\n");\r
-#endif /* not REDUCED */\r
- printf(" -l Uses vertical cuts only, rather than alternating cuts.\n");\r
-#ifndef REDUCED\r
-#ifndef CDT_ONLY\r
- printf(\r
- " -s Force segments into mesh by splitting (instead of using CDT).\n");\r
-#endif /* not CDT_ONLY */\r
- printf(" -C Check consistency of final mesh.\n");\r
-#endif /* not REDUCED */\r
- printf(" -Q Quiet: No terminal output except errors.\n");\r
- printf(" -V Verbose: Detailed information on what I'm doing.\n");\r
- printf(" -h Help: Detailed instructions for Triangle.\n");\r
- exit(0);\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* info() Print out complete instructions. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-void info()\r
-{\r
- printf("Triangle\n");\r
- printf(\r
-"A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\n");\r
- printf("Version 1.3\n\n");\r
- printf(\r
-"Copyright 1996 Jonathan Richard Shewchuk (bugs/comments to jrs@cs.cmu.edu)\n"\r
-);\r
- printf("School of Computer Science / Carnegie Mellon University\n");\r
- printf("5000 Forbes Avenue / Pittsburgh, Pennsylvania 15213-3891\n");\r
- printf(\r
-"Created as part of the Archimedes project (tools for parallel FEM).\n");\r
- printf(\r
-"Supported in part by NSF Grant CMS-9318163 and an NSERC 1967 Scholarship.\n");\r
- printf("There is no warranty whatsoever. Use at your own risk.\n");\r
-#ifdef SINGLE\r
- printf("This executable is compiled for single precision arithmetic.\n\n\n");\r
-#else /* not SINGLE */\r
- printf("This executable is compiled for double precision arithmetic.\n\n\n");\r
-#endif /* not SINGLE */\r
- printf(\r
-"Triangle generates exact Delaunay triangulations, constrained Delaunay\n");\r
- printf(\r
-"triangulations, and quality conforming Delaunay triangulations. The latter\n"\r
-);\r
- printf(\r
-"can be generated with no small angles, and are thus suitable for finite\n");\r
- printf(\r
-"element analysis. If no command line switches are specified, your .node\n");\r
- printf(\r
-"input file will be read, and the Delaunay triangulation will be returned in\n"\r
-);\r
- printf(".node and .ele output files. The command syntax is:\n\n");\r
-#ifdef CDT_ONLY\r
-#ifdef REDUCED\r
- printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n\n");\r
-#else /* not REDUCED */\r
- printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n\n");\r
-#endif /* not REDUCED */\r
-#else /* not CDT_ONLY */\r
-#ifdef REDUCED\r
- printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n\n");\r
-#else /* not REDUCED */\r
- printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n\n");\r
-#endif /* not REDUCED */\r
-#endif /* not CDT_ONLY */\r
- printf(\r
-"Underscores indicate that numbers may optionally follow certain switches;\n");\r
- printf(\r
-"do not leave any space between a switch and its numeric parameter.\n");\r
- printf(\r
-"input_file must be a file with extension .node, or extension .poly if the\n");\r
- printf(\r
-"-p switch is used. If -r is used, you must supply .node and .ele files,\n");\r
- printf(\r
-"and possibly a .poly file and .area file as well. The formats of these\n");\r
- printf("files are described below.\n\n");\r
- printf("Command Line Switches:\n\n");\r
- printf(\r
-" -p Reads a Planar Straight Line Graph (.poly file), which can specify\n"\r
-);\r
- printf(\r
-" points, segments, holes, and regional attributes and area\n");\r
- printf(\r
-" constraints. Will generate a constrained Delaunay triangulation\n");\r
- printf(\r
-" fitting the input; or, if -s, -q, or -a is used, a conforming\n");\r
- printf(\r
-" Delaunay triangulation. If -p is not used, Triangle reads a .node\n"\r
-);\r
- printf(" file by default.\n");\r
- printf(\r
-" -r Refines a previously generated mesh. The mesh is read from a .node\n"\r
-);\r
- printf(\r
-" file and an .ele file. If -p is also used, a .poly file is read\n");\r
- printf(\r
-" and used to constrain edges in the mesh. Further details on\n");\r
- printf(" refinement are given below.\n");\r
- printf(\r
-" -q Quality mesh generation by Jim Ruppert's Delaunay refinement\n");\r
- printf(\r
-" algorithm. Adds points to the mesh to ensure that no angles\n");\r
- printf(\r
-" smaller than 20 degrees occur. An alternative minimum angle may be\n"\r
-);\r
- printf(\r
-" specified after the `q'. If the minimum angle is 20.7 degrees or\n");\r
- printf(\r
-" smaller, the triangulation algorithm is theoretically guaranteed to\n"\r
-);\r
- printf(\r
-" terminate (assuming infinite precision arithmetic - Triangle may\n");\r
- printf(\r
-" fail to terminate if you run out of precision). In practice, the\n");\r
- printf(\r
-" algorithm often succeeds for minimum angles up to 33.8 degrees.\n");\r
- printf(\r
-" For highly refined meshes, however, it may be necessary to reduce\n");\r
- printf(\r
-" the minimum angle to well below 20 to avoid problems associated\n");\r
- printf(\r
-" with insufficient floating-point precision. The specified angle\n");\r
- printf(" may include a decimal point.\n");\r
- printf(\r
-" -a Imposes a maximum triangle area. If a number follows the `a', no\n");\r
- printf(\r
-" triangle will be generated whose area is larger than that number.\n");\r
- printf(\r
-" If no number is specified, an .area file (if -r is used) or .poly\n");\r
- printf(\r
-" file (if -r is not used) specifies a number of maximum area\n");\r
- printf(\r
-" constraints. An .area file contains a separate area constraint for\n"\r
-);\r
- printf(\r
-" each triangle, and is useful for refining a finite element mesh\n");\r
- printf(\r
-" based on a posteriori error estimates. A .poly file can optionally\n"\r
-);\r
- printf(\r
-" contain an area constraint for each segment-bounded region, thereby\n"\r
-);\r
- printf(\r
-" enforcing triangle densities in a first triangulation. You can\n");\r
- printf(\r
-" impose both a fixed area constraint and a varying area constraint\n");\r
- printf(\r
-" by invoking the -a switch twice, once with and once without a\n");\r
- printf(\r
-" number following. Each area specified may include a decimal point.\n"\r
-);\r
- printf(\r
-" -A Assigns an additional attribute to each triangle that identifies\n");\r
- printf(\r
-" what segment-bounded region each triangle belongs to. Attributes\n");\r
- printf(\r
-" are assigned to regions by the .poly file. If a region is not\n");\r
- printf(\r
-" explicitly marked by the .poly file, triangles in that region are\n");\r
- printf(\r
-" assigned an attribute of zero. The -A switch has an effect only\n");\r
- printf(" when the -p switch is used and the -r switch is not.\n");\r
- printf(\r
-" -c Creates segments on the convex hull of the triangulation. If you\n");\r
- printf(\r
-" are triangulating a point set, this switch causes a .poly file to\n");\r
- printf(\r
-" be written, containing all edges in the convex hull. (By default,\n"\r
-);\r
- printf(\r
-" a .poly file is written only if a .poly file is read.) If you are\n"\r
-);\r
- printf(\r
-" triangulating a PSLG, this switch specifies that the interior of\n");\r
- printf(\r
-" the convex hull of the PSLG should be triangulated. If you do not\n"\r
-);\r
- printf(\r
-" use this switch when triangulating a PSLG, it is assumed that you\n");\r
- printf(\r
-" have identified the region to be triangulated by surrounding it\n");\r
- printf(\r
-" with segments of the input PSLG. Beware: if you are not careful,\n"\r
-);\r
- printf(\r
-" this switch can cause the introduction of an extremely thin angle\n");\r
- printf(\r
-" between a PSLG segment and a convex hull segment, which can cause\n");\r
- printf(\r
-" overrefinement or failure if Triangle runs out of precision. If\n");\r
- printf(\r
-" you are refining a mesh, the -c switch works differently; it\n");\r
- printf(\r
-" generates the set of boundary edges of the mesh, rather than the\n");\r
- printf(" convex hull.\n");\r
- printf(\r
-" -e Outputs (to an .edge file) a list of edges of the triangulation.\n");\r
- printf(\r
-" -v Outputs the Voronoi diagram associated with the triangulation.\n");\r
- printf(" Does not attempt to detect degeneracies.\n");\r
- printf(\r
-" -n Outputs (to a .neigh file) a list of triangles neighboring each\n");\r
- printf(" triangle.\n");\r
- printf(\r
-" -g Outputs the mesh to an Object File Format (.off) file, suitable for\n"\r
-);\r
- printf(" viewing with the Geometry Center's Geomview package.\n");\r
- printf(\r
-" -B No boundary markers in the output .node, .poly, and .edge output\n");\r
- printf(\r
-" files. See the detailed discussion of boundary markers below.\n");\r
- printf(\r
-" -P No output .poly file. Saves disk space, but you lose the ability\n");\r
- printf(\r
-" to impose segment constraints on later refinements of the mesh.\n");\r
- printf(" -N No output .node file.\n");\r
- printf(" -E No output .ele file.\n");\r
- printf(\r
-" -I No iteration numbers. Suppresses the output of .node and .poly\n");\r
- printf(\r
-" files, so your input files won't be overwritten. (If your input is\n"\r
-);\r
- printf(\r
-" a .poly file only, a .node file will be written.) Cannot be used\n");\r
- printf(\r
-" with the -r switch, because that would overwrite your input .ele\n");\r
- printf(\r
-" file. Shouldn't be used with the -s, -q, or -a switch if you are\n");\r
- printf(\r
-" using a .node file for input, because no .node file will be\n");\r
- printf(" written, so there will be no record of any added points.\n");\r
- printf(" -O No holes. Ignores the holes in the .poly file.\n");\r
- printf(\r
-" -X No exact arithmetic. Normally, Triangle uses exact floating-point\n"\r
-);\r
- printf(\r
-" arithmetic for certain tests if it thinks the inexact tests are not\n"\r
-);\r
- printf(\r
-" accurate enough. Exact arithmetic ensures the robustness of the\n");\r
- printf(\r
-" triangulation algorithms, despite floating-point roundoff error.\n");\r
- printf(\r
-" Disabling exact arithmetic with the -X switch will cause a small\n");\r
- printf(\r
-" improvement in speed and create the possibility (albeit small) that\n"\r
-);\r
- printf(\r
-" Triangle will fail to produce a valid mesh. Not recommended.\n");\r
- printf(\r
-" -z Numbers all items starting from zero (rather than one). Note that\n"\r
-);\r
- printf(\r
-" this switch is normally overrided by the value used to number the\n");\r
- printf(\r
-" first point of the input .node or .poly file. However, this switch\n"\r
-);\r
- printf(" is useful when calling Triangle from another program.\n");\r
- printf(\r
-" -o2 Generates second-order subparametric elements with six nodes each.\n"\r
-);\r
- printf(\r
-" -Y No new points on the boundary. This switch is useful when the mesh\n"\r
-);\r
- printf(\r
-" boundary must be preserved so that it conforms to some adjacent\n");\r
- printf(\r
-" mesh. Be forewarned that you will probably sacrifice some of the\n");\r
- printf(\r
-" quality of the mesh; Triangle will try, but the resulting mesh may\n"\r
-);\r
- printf(\r
-" contain triangles of poor aspect ratio. Works well if all the\n");\r
- printf(\r
-" boundary points are closely spaced. Specify this switch twice\n");\r
- printf(\r
-" (`-YY') to prevent all segment splitting, including internal\n");\r
- printf(" boundaries.\n");\r
- printf(\r
-" -S Specifies the maximum number of Steiner points (points that are not\n"\r
-);\r
- printf(\r
-" in the input, but are added to meet the constraints of minimum\n");\r
- printf(\r
-" angle and maximum area). The default is to allow an unlimited\n");\r
- printf(\r
-" number. If you specify this switch with no number after it,\n");\r
- printf(\r
-" the limit is set to zero. Triangle always adds points at segment\n");\r
- printf(\r
-" intersections, even if it needs to use more points than the limit\n");\r
- printf(\r
-" you set. When Triangle inserts segments by splitting (-s), it\n");\r
- printf(\r
-" always adds enough points to ensure that all the segments appear in\n"\r
-);\r
- printf(\r
-" the triangulation, again ignoring the limit. Be forewarned that\n");\r
- printf(\r
-" the -S switch may result in a conforming triangulation that is not\n"\r
-);\r
- printf(\r
-" truly Delaunay, because Triangle may be forced to stop adding\n");\r
- printf(\r
-" points when the mesh is in a state where a segment is non-Delaunay\n"\r
-);\r
- printf(\r
-" and needs to be split. If so, Triangle will print a warning.\n");\r
- printf(\r
-" -i Uses an incremental rather than divide-and-conquer algorithm to\n");\r
- printf(\r
-" form a Delaunay triangulation. Try it if the divide-and-conquer\n");\r
- printf(" algorithm fails.\n");\r
- printf(\r
-" -F Uses Steven Fortune's sweepline algorithm to form a Delaunay\n");\r
- printf(\r
-" triangulation. Warning: does not use exact arithmetic for all\n");\r
- printf(" calculations. An exact result is not guaranteed.\n");\r
- printf(\r
-" -l Uses only vertical cuts in the divide-and-conquer algorithm. By\n");\r
- printf(\r
-" default, Triangle uses alternating vertical and horizontal cuts,\n");\r
- printf(\r
-" which usually improve the speed except with point sets that are\n");\r
- printf(\r
-" small or short and wide. This switch is primarily of theoretical\n");\r
- printf(" interest.\n");\r
- printf(\r
-" -s Specifies that segments should be forced into the triangulation by\n"\r
-);\r
- printf(\r
-" recursively splitting them at their midpoints, rather than by\n");\r
- printf(\r
-" generating a constrained Delaunay triangulation. Segment splitting\n"\r
-);\r
- printf(\r
-" is true to Ruppert's original algorithm, but can create needlessly\n"\r
-);\r
- printf(" small triangles near external small features.\n");\r
- printf(\r
-" -C Check the consistency of the final mesh. Uses exact arithmetic for\n"\r
-);\r
- printf(\r
-" checking, even if the -X switch is used. Useful if you suspect\n");\r
- printf(" Triangle is buggy.\n");\r
- printf(\r
-" -Q Quiet: Suppresses all explanation of what Triangle is doing, unless\n"\r
-);\r
- printf(" an error occurs.\n");\r
- printf(\r
-" -V Verbose: Gives detailed information about what Triangle is doing.\n");\r
- printf(\r
-" Add more `V's for increasing amount of detail. `-V' gives\n");\r
- printf(\r
-" information on algorithmic progress and more detailed statistics.\n");\r
- printf(\r
-" `-VV' gives point-by-point details, and will print so much that\n");\r
- printf(\r
-" Triangle will run much more slowly. `-VVV' gives information only\n"\r
-);\r
- printf(" a debugger could love.\n");\r
- printf(" -h Help: Displays these instructions.\n");\r
- printf("\n");\r
- printf("Definitions:\n");\r
- printf("\n");\r
- printf(\r
-" A Delaunay triangulation of a point set is a triangulation whose vertices\n"\r
-);\r
- printf(\r
-" are the point set, having the property that no point in the point set\n");\r
- printf(\r
-" falls in the interior of the circumcircle (circle that passes through all\n"\r
-);\r
- printf(" three vertices) of any triangle in the triangulation.\n\n");\r
- printf(\r
-" A Voronoi diagram of a point set is a subdivision of the plane into\n");\r
- printf(\r
-" polygonal regions (some of which may be infinite), where each region is\n");\r
- printf(\r
-" the set of points in the plane that are closer to some input point than\n");\r
- printf(\r
-" to any other input point. (The Voronoi diagram is the geometric dual of\n"\r
-);\r
- printf(" the Delaunay triangulation.)\n\n");\r
- printf(\r
-" A Planar Straight Line Graph (PSLG) is a collection of points and\n");\r
- printf(\r
-" segments. Segments are simply edges, whose endpoints are points in the\n");\r
- printf(\r
-" PSLG. The file format for PSLGs (.poly files) is described below.\n");\r
- printf("\n");\r
- printf(\r
-" A constrained Delaunay triangulation of a PSLG is similar to a Delaunay\n");\r
- printf(\r
-" triangulation, but each PSLG segment is present as a single edge in the\n");\r
- printf(\r
-" triangulation. (A constrained Delaunay triangulation is not truly a\n");\r
- printf(" Delaunay triangulation.)\n\n");\r
- printf(\r
-" A conforming Delaunay triangulation of a PSLG is a true Delaunay\n");\r
- printf(\r
-" triangulation in which each PSLG segment may have been subdivided into\n");\r
- printf(\r
-" several edges by the insertion of additional points. These inserted\n");\r
- printf(\r
-" points are necessary to allow the segments to exist in the mesh while\n");\r
- printf(" maintaining the Delaunay property.\n\n");\r
- printf("File Formats:\n\n");\r
- printf(\r
-" All files may contain comments prefixed by the character '#'. Points,\n");\r
- printf(\r
-" triangles, edges, holes, and maximum area constraints must be numbered\n");\r
- printf(\r
-" consecutively, starting from either 1 or 0. Whichever you choose, all\n");\r
- printf(\r
-" input files must be consistent; if the nodes are numbered from 1, so must\n"\r
-);\r
- printf(\r
-" be all other objects. Triangle automatically detects your choice while\n");\r
- printf(\r
-" reading the .node (or .poly) file. (When calling Triangle from another\n");\r
- printf(\r
-" program, use the -z switch if you wish to number objects from zero.)\n");\r
- printf(" Examples of these file formats are given below.\n\n");\r
- printf(" .node files:\n");\r
- printf(\r
-" First line: <# of points> <dimension (must be 2)> <# of attributes>\n");\r
- printf(\r
-" <# of boundary markers (0 or 1)>\n"\r
-);\r
- printf(\r
-" Remaining lines: <point #> <x> <y> [attributes] [boundary marker]\n");\r
- printf("\n");\r
- printf(\r
-" The attributes, which are typically floating-point values of physical\n");\r
- printf(\r
-" quantities (such as mass or conductivity) associated with the nodes of\n"\r
-);\r
- printf(\r
-" a finite element mesh, are copied unchanged to the output mesh. If -s,\n"\r
-);\r
- printf(\r
-" -q, or -a is selected, each new Steiner point added to the mesh will\n");\r
- printf(" have attributes assigned to it by linear interpolation.\n\n");\r
- printf(\r
-" If the fourth entry of the first line is `1', the last column of the\n");\r
- printf(\r
-" remainder of the file is assumed to contain boundary markers. Boundary\n"\r
-);\r
- printf(\r
-" markers are used to identify boundary points and points resting on PSLG\n"\r
-);\r
- printf(\r
-" segments; a complete description appears in a section below. The .node\n"\r
-);\r
- printf(\r
-" file produced by Triangle will contain boundary markers in the last\n");\r
- printf(" column unless they are suppressed by the -B switch.\n\n");\r
- printf(" .ele files:\n");\r
- printf(\r
-" First line: <# of triangles> <points per triangle> <# of attributes>\n");\r
- printf(\r
-" Remaining lines: <triangle #> <point> <point> <point> ... [attributes]\n"\r
-);\r
- printf("\n");\r
- printf(\r
-" Points are indices into the corresponding .node file. The first three\n"\r
-);\r
- printf(\r
-" points are the corners, and are listed in counterclockwise order around\n"\r
-);\r
- printf(\r
-" each triangle. (The remaining points, if any, depend on the type of\n");\r
- printf(\r
-" finite element used.) The attributes are just like those of .node\n");\r
- printf(\r
-" files. Because there is no simple mapping from input to output\n");\r
- printf(\r
-" triangles, an attempt is made to interpolate attributes, which may\n");\r
- printf(\r
-" result in a good deal of diffusion of attributes among nearby triangles\n"\r
-);\r
- printf(\r
-" as the triangulation is refined. Diffusion does not occur across\n");\r
- printf(\r
-" segments, so attributes used to identify segment-bounded regions remain\n"\r
-);\r
- printf(\r
-" intact. In output .ele files, all triangles have three points each\n");\r
- printf(\r
-" unless the -o2 switch is used, in which case they have six, and the\n");\r
- printf(\r
-" fourth, fifth, and sixth points lie on the midpoints of the edges\n");\r
- printf(" opposite the first, second, and third corners.\n\n");\r
- printf(" .poly files:\n");\r
- printf(\r
-" First line: <# of points> <dimension (must be 2)> <# of attributes>\n");\r
- printf(\r
-" <# of boundary markers (0 or 1)>\n"\r
-);\r
- printf(\r
-" Following lines: <point #> <x> <y> [attributes] [boundary marker]\n");\r
- printf(" One line: <# of segments> <# of boundary markers (0 or 1)>\n");\r
- printf(\r
-" Following lines: <segment #> <endpoint> <endpoint> [boundary marker]\n");\r
- printf(" One line: <# of holes>\n");\r
- printf(" Following lines: <hole #> <x> <y>\n");\r
- printf(\r
-" Optional line: <# of regional attributes and/or area constraints>\n");\r
- printf(\r
-" Optional following lines: <constraint #> <x> <y> <attrib> <max area>\n");\r
- printf("\n");\r
- printf(\r
-" A .poly file represents a PSLG, as well as some additional information.\n"\r
-);\r
- printf(\r
-" The first section lists all the points, and is identical to the format\n"\r
-);\r
- printf(\r
-" of .node files. <# of points> may be set to zero to indicate that the\n"\r
-);\r
- printf(\r
-" points are listed in a separate .node file; .poly files produced by\n");\r
- printf(\r
-" Triangle always have this format. This has the advantage that a point\n"\r
-);\r
- printf(\r
-" set may easily be triangulated with or without segments. (The same\n");\r
- printf(\r
-" effect can be achieved, albeit using more disk space, by making a copy\n"\r
-);\r
- printf(\r
-" of the .poly file with the extension .node; all sections of the file\n");\r
- printf(" but the first are ignored.)\n\n");\r
- printf(\r
-" The second section lists the segments. Segments are edges whose\n");\r
- printf(\r
-" presence in the triangulation is enforced. Each segment is specified\n");\r
- printf(\r
-" by listing the indices of its two endpoints. This means that you must\n"\r
-);\r
- printf(\r
-" include its endpoints in the point list. If -s, -q, and -a are not\n");\r
- printf(\r
-" selected, Triangle will produce a constrained Delaunay triangulation,\n");\r
- printf(\r
-" in which each segment appears as a single edge in the triangulation.\n");\r
- printf(\r
-" If -q or -a is selected, Triangle will produce a conforming Delaunay\n");\r
- printf(\r
-" triangulation, in which segments may be subdivided into smaller edges.\n"\r
-);\r
- printf(" Each segment, like each point, may have a boundary marker.\n\n");\r
- printf(\r
-" The third section lists holes (and concavities, if -c is selected) in\n");\r
- printf(\r
-" the triangulation. Holes are specified by identifying a point inside\n");\r
- printf(\r
-" each hole. After the triangulation is formed, Triangle creates holes\n");\r
- printf(\r
-" by eating triangles, spreading out from each hole point until its\n");\r
- printf(\r
-" progress is blocked by PSLG segments; you must be careful to enclose\n");\r
- printf(\r
-" each hole in segments, or your whole triangulation may be eaten away.\n");\r
- printf(\r
-" If the two triangles abutting a segment are eaten, the segment itself\n");\r
- printf(\r
-" is also eaten. Do not place a hole directly on a segment; if you do,\n");\r
- printf(" Triangle will choose one side of the segment arbitrarily.\n\n");\r
- printf(\r
-" The optional fourth section lists regional attributes (to be assigned\n");\r
- printf(\r
-" to all triangles in a region) and regional constraints on the maximum\n");\r
- printf(\r
-" triangle area. Triangle will read this section only if the -A switch\n");\r
- printf(\r
-" is used or the -a switch is used without a number following it, and the\n"\r
-);\r
- printf(\r
-" -r switch is not used. Regional attributes and area constraints are\n");\r
- printf(\r
-" propagated in the same manner as holes; you specify a point for each\n");\r
- printf(\r
-" attribute and/or constraint, and the attribute and/or constraint will\n");\r
- printf(\r
-" affect the whole region (bounded by segments) containing the point. If\n"\r
-);\r
- printf(\r
-" two values are written on a line after the x and y coordinate, the\n");\r
- printf(\r
-" former is assumed to be a regional attribute (but will only be applied\n"\r
-);\r
- printf(\r
-" if the -A switch is selected), and the latter is assumed to be a\n");\r
- printf(\r
-" regional area constraint (but will only be applied if the -a switch is\n"\r
-);\r
- printf(\r
-" selected). You may also specify just one value after the coordinates,\n"\r
-);\r
- printf(\r
-" which can serve as both an attribute and an area constraint, depending\n"\r
-);\r
- printf(\r
-" on the choice of switches. If you are using the -A and -a switches\n");\r
- printf(\r
-" simultaneously and wish to assign an attribute to some region without\n");\r
- printf(" imposing an area constraint, use a negative maximum area.\n\n");\r
- printf(\r
-" When a triangulation is created from a .poly file, you must either\n");\r
- printf(\r
-" enclose the entire region to be triangulated in PSLG segments, or\n");\r
- printf(\r
-" use the -c switch, which encloses the convex hull of the input point\n");\r
- printf(\r
-" set. If you do not use the -c switch, Triangle will eat all triangles\n"\r
-);\r
- printf(\r
-" on the outer boundary that are not protected by segments; if you are\n");\r
- printf(\r
-" not careful, your whole triangulation may be eaten away. If you do\n");\r
- printf(\r
-" use the -c switch, you can still produce concavities by appropriate\n");\r
- printf(" placement of holes just inside the convex hull.\n\n");\r
- printf(\r
-" An ideal PSLG has no intersecting segments, nor any points that lie\n");\r
- printf(\r
-" upon segments (except, of course, the endpoints of each segment.) You\n"\r
-);\r
- printf(\r
-" aren't required to make your .poly files ideal, but you should be aware\n"\r
-);\r
- printf(\r
-" of what can go wrong. Segment intersections are relatively safe -\n");\r
- printf(\r
-" Triangle will calculate the intersection points for you and add them to\n"\r
-);\r
- printf(\r
-" the triangulation - as long as your machine's floating-point precision\n"\r
-);\r
- printf(\r
-" doesn't become a problem. You are tempting the fates if you have three\n"\r
-);\r
- printf(\r
-" segments that cross at the same location, and expect Triangle to figure\n"\r
-);\r
- printf(\r
-" out where the intersection point is. Thanks to floating-point roundoff\n"\r
-);\r
- printf(\r
-" error, Triangle will probably decide that the three segments intersect\n"\r
-);\r
- printf(\r
-" at three different points, and you will find a minuscule triangle in\n");\r
- printf(\r
-" your output - unless Triangle tries to refine the tiny triangle, uses\n");\r
- printf(\r
-" up the last bit of machine precision, and fails to terminate at all.\n");\r
- printf(\r
-" You're better off putting the intersection point in the input files,\n");\r
- printf(\r
-" and manually breaking up each segment into two. Similarly, if you\n");\r
- printf(\r
-" place a point at the middle of a segment, and hope that Triangle will\n");\r
- printf(\r
-" break up the segment at that point, you might get lucky. On the other\n"\r
-);\r
- printf(\r
-" hand, Triangle might decide that the point doesn't lie precisely on the\n"\r
-);\r
- printf(\r
-" line, and you'll have a needle-sharp triangle in your output - or a lot\n"\r
-);\r
- printf(" of tiny triangles if you're generating a quality mesh.\n\n");\r
- printf(\r
-" When Triangle reads a .poly file, it also writes a .poly file, which\n");\r
- printf(\r
-" includes all edges that are part of input segments. If the -c switch\n");\r
- printf(\r
-" is used, the output .poly file will also include all of the edges on\n");\r
- printf(\r
-" the convex hull. Hence, the output .poly file is useful for finding\n");\r
- printf(\r
-" edges associated with input segments and setting boundary conditions in\n"\r
-);\r
- printf(\r
-" finite element simulations. More importantly, you will need it if you\n"\r
-);\r
- printf(\r
-" plan to refine the output mesh, and don't want segments to be missing\n");\r
- printf(" in later triangulations.\n\n");\r
- printf(" .area files:\n");\r
- printf(" First line: <# of triangles>\n");\r
- printf(" Following lines: <triangle #> <maximum area>\n\n");\r
- printf(\r
-" An .area file associates with each triangle a maximum area that is used\n"\r
-);\r
- printf(\r
-" for mesh refinement. As with other file formats, every triangle must\n");\r
- printf(\r
-" be represented, and they must be numbered consecutively. A triangle\n");\r
- printf(\r
-" may be left unconstrained by assigning it a negative maximum area.\n");\r
- printf("\n");\r
- printf(" .edge files:\n");\r
- printf(" First line: <# of edges> <# of boundary markers (0 or 1)>\n");\r
- printf(\r
-" Following lines: <edge #> <endpoint> <endpoint> [boundary marker]\n");\r
- printf("\n");\r
- printf(\r
-" Endpoints are indices into the corresponding .node file. Triangle can\n"\r
-);\r
- printf(\r
-" produce .edge files (use the -e switch), but cannot read them. The\n");\r
- printf(\r
-" optional column of boundary markers is suppressed by the -B switch.\n");\r
- printf("\n");\r
- printf(\r
-" In Voronoi diagrams, one also finds a special kind of edge that is an\n");\r
- printf(\r
-" infinite ray with only one endpoint. For these edges, a different\n");\r
- printf(" format is used:\n\n");\r
- printf(" <edge #> <endpoint> -1 <direction x> <direction y>\n\n");\r
- printf(\r
-" The `direction' is a floating-point vector that indicates the direction\n"\r
-);\r
- printf(" of the infinite ray.\n\n");\r
- printf(" .neigh files:\n");\r
- printf(\r
-" First line: <# of triangles> <# of neighbors per triangle (always 3)>\n"\r
-);\r
- printf(\r
-" Following lines: <triangle #> <neighbor> <neighbor> <neighbor>\n");\r
- printf("\n");\r
- printf(\r
-" Neighbors are indices into the corresponding .ele file. An index of -1\n"\r
-);\r
- printf(\r
-" indicates a mesh boundary, and therefore no neighbor. Triangle can\n");\r
- printf(\r
-" produce .neigh files (use the -n switch), but cannot read them.\n");\r
- printf("\n");\r
- printf(\r
-" The first neighbor of triangle i is opposite the first corner of\n");\r
- printf(" triangle i, and so on.\n\n");\r
- printf("Boundary Markers:\n\n");\r
- printf(\r
-" Boundary markers are tags used mainly to identify which output points and\n"\r
-);\r
- printf(\r
-" edges are associated with which PSLG segment, and to identify which\n");\r
- printf(\r
-" points and edges occur on a boundary of the triangulation. A common use\n"\r
-);\r
- printf(\r
-" is to determine where boundary conditions should be applied to a finite\n");\r
- printf(\r
-" element mesh. You can prevent boundary markers from being written into\n");\r
- printf(" files produced by Triangle by using the -B switch.\n\n");\r
- printf(\r
-" The boundary marker associated with each segment in an output .poly file\n"\r
-);\r
- printf(" or edge in an output .edge file is chosen as follows:\n");\r
- printf(\r
-" - If an output edge is part or all of a PSLG segment with a nonzero\n");\r
- printf(\r
-" boundary marker, then the edge is assigned the same marker.\n");\r
- printf(\r
-" - Otherwise, if the edge occurs on a boundary of the triangulation\n");\r
- printf(\r
-" (including boundaries of holes), then the edge is assigned the marker\n"\r
-);\r
- printf(" one (1).\n");\r
- printf(" - Otherwise, the edge is assigned the marker zero (0).\n");\r
- printf(\r
-" The boundary marker associated with each point in an output .node file is\n"\r
-);\r
- printf(" chosen as follows:\n");\r
- printf(\r
-" - If a point is assigned a nonzero boundary marker in the input file,\n");\r
- printf(\r
-" then it is assigned the same marker in the output .node file.\n");\r
- printf(\r
-" - Otherwise, if the point lies on a PSLG segment (including the\n");\r
- printf(\r
-" segment's endpoints) with a nonzero boundary marker, then the point\n");\r
- printf(\r
-" is assigned the same marker. If the point lies on several such\n");\r
- printf(" segments, one of the markers is chosen arbitrarily.\n");\r
- printf(\r
-" - Otherwise, if the point occurs on a boundary of the triangulation,\n");\r
- printf(" then the point is assigned the marker one (1).\n");\r
- printf(" - Otherwise, the point is assigned the marker zero (0).\n");\r
- printf("\n");\r
- printf(\r
-" If you want Triangle to determine for you which points and edges are on\n");\r
- printf(\r
-" the boundary, assign them the boundary marker zero (or use no markers at\n"\r
-);\r
- printf(\r
-" all) in your input files. Alternatively, you can mark some of them and\n");\r
- printf(" leave others marked zero, allowing Triangle to label them.\n\n");\r
- printf("Triangulation Iteration Numbers:\n\n");\r
- printf(\r
-" Because Triangle can read and refine its own triangulations, input\n");\r
- printf(\r
-" and output files have iteration numbers. For instance, Triangle might\n");\r
- printf(\r
-" read the files mesh.3.node, mesh.3.ele, and mesh.3.poly, refine the\n");\r
- printf(\r
-" triangulation, and output the files mesh.4.node, mesh.4.ele, and\n");\r
- printf(" mesh.4.poly. Files with no iteration number are treated as if\n");\r
- printf(\r
-" their iteration number is zero; hence, Triangle might read the file\n");\r
- printf(\r
-" points.node, triangulate it, and produce the files points.1.node and\n");\r
- printf(" points.1.ele.\n\n");\r
- printf(\r
-" Iteration numbers allow you to create a sequence of successively finer\n");\r
- printf(\r
-" meshes suitable for multigrid methods. They also allow you to produce a\n"\r
-);\r
- printf(\r
-" sequence of meshes using error estimate-driven mesh refinement.\n");\r
- printf("\n");\r
- printf(\r
-" If you're not using refinement or quality meshing, and you don't like\n");\r
- printf(\r
-" iteration numbers, use the -I switch to disable them. This switch will\n");\r
- printf(\r
-" also disable output of .node and .poly files to prevent your input files\n"\r
-);\r
- printf(\r
-" from being overwritten. (If the input is a .poly file that contains its\n"\r
-);\r
- printf(" own points, a .node file will be written.)\n\n");\r
- printf("Examples of How to Use Triangle:\n\n");\r
- printf(\r
-" `triangle dots' will read points from dots.node, and write their Delaunay\n"\r
-);\r
- printf(\r
-" triangulation to dots.1.node and dots.1.ele. (dots.1.node will be\n");\r
- printf(\r
-" identical to dots.node.) `triangle -I dots' writes the triangulation to\n"\r
-);\r
- printf(\r
-" dots.ele instead. (No additional .node file is needed, so none is\n");\r
- printf(" written.)\n\n");\r
- printf(\r
-" `triangle -pe object.1' will read a PSLG from object.1.poly (and possibly\n"\r
-);\r
- printf(\r
-" object.1.node, if the points are omitted from object.1.poly) and write\n");\r
- printf(" their constrained Delaunay triangulation to object.2.node and\n");\r
- printf(\r
-" object.2.ele. The segments will be copied to object.2.poly, and all\n");\r
- printf(" edges will be written to object.2.edge.\n\n");\r
- printf(\r
-" `triangle -pq31.5a.1 object' will read a PSLG from object.poly (and\n");\r
- printf(\r
-" possibly object.node), generate a mesh whose angles are all greater than\n"\r
-);\r
- printf(\r
-" 31.5 degrees and whose triangles all have area smaller than 0.1, and\n");\r
- printf(\r
-" write the mesh to object.1.node and object.1.ele. Each segment may have\n"\r
-);\r
- printf(\r
-" been broken up into multiple edges; the resulting constrained edges are\n");\r
- printf(" written to object.1.poly.\n\n");\r
- printf(\r
-" Here is a sample file `box.poly' describing a square with a square hole:\n"\r
-);\r
- printf("\n");\r
- printf(\r
-" # A box with eight points in 2D, no attributes, one boundary marker.\n");\r
- printf(" 8 2 0 1\n");\r
- printf(" # Outer box has these vertices:\n");\r
- printf(" 1 0 0 0\n");\r
- printf(" 2 0 3 0\n");\r
- printf(" 3 3 0 0\n");\r
- printf(" 4 3 3 33 # A special marker for this point.\n");\r
- printf(" # Inner square has these vertices:\n");\r
- printf(" 5 1 1 0\n");\r
- printf(" 6 1 2 0\n");\r
- printf(" 7 2 1 0\n");\r
- printf(" 8 2 2 0\n");\r
- printf(" # Five segments with boundary markers.\n");\r
- printf(" 5 1\n");\r
- printf(" 1 1 2 5 # Left side of outer box.\n");\r
- printf(" 2 5 7 0 # Segments 2 through 5 enclose the hole.\n");\r
- printf(" 3 7 8 0\n");\r
- printf(" 4 8 6 10\n");\r
- printf(" 5 6 5 0\n");\r
- printf(" # One hole in the middle of the inner square.\n");\r
- printf(" 1\n");\r
- printf(" 1 1.5 1.5\n\n");\r
- printf(\r
-" Note that some segments are missing from the outer square, so one must\n");\r
- printf(\r
-" use the `-c' switch. After `triangle -pqc box.poly', here is the output\n"\r
-);\r
- printf(\r
-" file `box.1.node', with twelve points. The last four points were added\n");\r
- printf(\r
-" to meet the angle constraint. Points 1, 2, and 9 have markers from\n");\r
- printf(\r
-" segment 1. Points 6 and 8 have markers from segment 4. All the other\n");\r
- printf(\r
-" points but 4 have been marked to indicate that they lie on a boundary.\n");\r
- printf("\n");\r
- printf(" 12 2 0 1\n");\r
- printf(" 1 0 0 5\n");\r
- printf(" 2 0 3 5\n");\r
- printf(" 3 3 0 1\n");\r
- printf(" 4 3 3 33\n");\r
- printf(" 5 1 1 1\n");\r
- printf(" 6 1 2 10\n");\r
- printf(" 7 2 1 1\n");\r
- printf(" 8 2 2 10\n");\r
- printf(" 9 0 1.5 5\n");\r
- printf(" 10 1.5 0 1\n");\r
- printf(" 11 3 1.5 1\n");\r
- printf(" 12 1.5 3 1\n");\r
- printf(" # Generated by triangle -pqc box.poly\n\n");\r
- printf(" Here is the output file `box.1.ele', with twelve triangles.\n\n");\r
- printf(" 12 3 0\n");\r
- printf(" 1 5 6 9\n");\r
- printf(" 2 10 3 7\n");\r
- printf(" 3 6 8 12\n");\r
- printf(" 4 9 1 5\n");\r
- printf(" 5 6 2 9\n");\r
- printf(" 6 7 3 11\n");\r
- printf(" 7 11 4 8\n");\r
- printf(" 8 7 5 10\n");\r
- printf(" 9 12 2 6\n");\r
- printf(" 10 8 7 11\n");\r
- printf(" 11 5 1 10\n");\r
- printf(" 12 8 4 12\n");\r
- printf(" # Generated by triangle -pqc box.poly\n\n");\r
- printf(\r
-" Here is the output file `box.1.poly'. Note that segments have been added\n"\r
-);\r
- printf(\r
-" to represent the convex hull, and some segments have been split by newly\n"\r
-);\r
- printf(\r
-" added points. Note also that <# of points> is set to zero to indicate\n");\r
- printf(" that the points should be read from the .node file.\n\n");\r
- printf(" 0 2 0 1\n");\r
- printf(" 12 1\n");\r
- printf(" 1 1 9 5\n");\r
- printf(" 2 5 7 1\n");\r
- printf(" 3 8 7 1\n");\r
- printf(" 4 6 8 10\n");\r
- printf(" 5 5 6 1\n");\r
- printf(" 6 3 10 1\n");\r
- printf(" 7 4 11 1\n");\r
- printf(" 8 2 12 1\n");\r
- printf(" 9 9 2 5\n");\r
- printf(" 10 10 1 1\n");\r
- printf(" 11 11 3 1\n");\r
- printf(" 12 12 4 1\n");\r
- printf(" 1\n");\r
- printf(" 1 1.5 1.5\n");\r
- printf(" # Generated by triangle -pqc box.poly\n\n");\r
- printf("Refinement and Area Constraints:\n\n");\r
- printf(\r
-" The -r switch causes a mesh (.node and .ele files) to be read and\n");\r
- printf(\r
-" refined. If the -p switch is also used, a .poly file is read and used to\n"\r
-);\r
- printf(\r
-" specify edges that are constrained and cannot be eliminated (although\n");\r
- printf(\r
-" they can be divided into smaller edges) by the refinement process.\n");\r
- printf("\n");\r
- printf(\r
-" When you refine a mesh, you generally want to impose tighter quality\n");\r
- printf(\r
-" constraints. One way to accomplish this is to use -q with a larger\n");\r
- printf(\r
-" angle, or -a followed by a smaller area than you used to generate the\n");\r
- printf(\r
-" mesh you are refining. Another way to do this is to create an .area\n");\r
- printf(\r
-" file, which specifies a maximum area for each triangle, and use the -a\n");\r
- printf(\r
-" switch (without a number following). Each triangle's area constraint is\n"\r
-);\r
- printf(\r
-" applied to that triangle. Area constraints tend to diffuse as the mesh\n");\r
- printf(\r
-" is refined, so if there are large variations in area constraint between\n");\r
- printf(" adjacent triangles, you may not get the results you want.\n\n");\r
- printf(\r
-" If you are refining a mesh composed of linear (three-node) elements, the\n"\r
-);\r
- printf(\r
-" output mesh will contain all the nodes present in the input mesh, in the\n"\r
-);\r
- printf(\r
-" same order, with new nodes added at the end of the .node file. However,\n"\r
-);\r
- printf(\r
-" there is no guarantee that each output element is contained in a single\n");\r
- printf(\r
-" input element. Often, output elements will overlap two input elements,\n");\r
- printf(\r
-" and input edges are not present in the output mesh. Hence, a sequence of\n"\r
-);\r
- printf(\r
-" refined meshes will form a hierarchy of nodes, but not a hierarchy of\n");\r
- printf(\r
-" elements. If you a refining a mesh of higher-order elements, the\n");\r
- printf(\r
-" hierarchical property applies only to the nodes at the corners of an\n");\r
- printf(" element; other nodes may not be present in the refined mesh.\n\n");\r
- printf(\r
-" It is important to understand that maximum area constraints in .poly\n");\r
- printf(\r
-" files are handled differently from those in .area files. A maximum area\n"\r
-);\r
- printf(\r
-" in a .poly file applies to the whole (segment-bounded) region in which a\n"\r
-);\r
- printf(\r
-" point falls, whereas a maximum area in an .area file applies to only one\n"\r
-);\r
- printf(\r
-" triangle. Area constraints in .poly files are used only when a mesh is\n");\r
- printf(\r
-" first generated, whereas area constraints in .area files are used only to\n"\r
-);\r
- printf(\r
-" refine an existing mesh, and are typically based on a posteriori error\n");\r
- printf(\r
-" estimates resulting from a finite element simulation on that mesh.\n");\r
- printf("\n");\r
- printf(\r
-" `triangle -rq25 object.1' will read object.1.node and object.1.ele, then\n"\r
-);\r
- printf(\r
-" refine the triangulation to enforce a 25 degree minimum angle, and then\n");\r
- printf(\r
-" write the refined triangulation to object.2.node and object.2.ele.\n");\r
- printf("\n");\r
- printf(\r
-" `triangle -rpaa6.2 z.3' will read z.3.node, z.3.ele, z.3.poly, and\n");\r
- printf(\r
-" z.3.area. After reconstructing the mesh and its segments, Triangle will\n"\r
-);\r
- printf(\r
-" refine the mesh so that no triangle has area greater than 6.2, and\n");\r
- printf(\r
-" furthermore the triangles satisfy the maximum area constraints in\n");\r
- printf(\r
-" z.3.area. The output is written to z.4.node, z.4.ele, and z.4.poly.\n");\r
- printf("\n");\r
- printf(\r
-" The sequence `triangle -qa1 x', `triangle -rqa.3 x.1', `triangle -rqa.1\n");\r
- printf(\r
-" x.2' creates a sequence of successively finer meshes x.1, x.2, and x.3,\n");\r
- printf(" suitable for multigrid.\n\n");\r
- printf("Convex Hulls and Mesh Boundaries:\n\n");\r
- printf(\r
-" If the input is a point set (rather than a PSLG), Triangle produces its\n");\r
- printf(\r
-" convex hull as a by-product in the output .poly file if you use the -c\n");\r
- printf(\r
-" switch. There are faster algorithms for finding a two-dimensional convex\n"\r
-);\r
- printf(\r
-" hull than triangulation, of course, but this one comes for free. If the\n"\r
-);\r
- printf(\r
-" input is an unconstrained mesh (you are using the -r switch but not the\n");\r
- printf(\r
-" -p switch), Triangle produces a list of its boundary edges (including\n");\r
- printf(" hole boundaries) as a by-product if you use the -c switch.\n\n");\r
- printf("Voronoi Diagrams:\n\n");\r
- printf(\r
-" The -v switch produces a Voronoi diagram, in files suffixed .v.node and\n");\r
- printf(\r
-" .v.edge. For example, `triangle -v points' will read points.node,\n");\r
- printf(\r
-" produce its Delaunay triangulation in points.1.node and points.1.ele,\n");\r
- printf(\r
-" and produce its Voronoi diagram in points.1.v.node and points.1.v.edge.\n");\r
- printf(\r
-" The .v.node file contains a list of all Voronoi vertices, and the .v.edge\n"\r
-);\r
- printf(\r
-" file contains a list of all Voronoi edges, some of which may be infinite\n"\r
-);\r
- printf(\r
-" rays. (The choice of filenames makes it easy to run the set of Voronoi\n");\r
- printf(" vertices through Triangle, if so desired.)\n\n");\r
- printf(\r
-" This implementation does not use exact arithmetic to compute the Voronoi\n"\r
-);\r
- printf(\r
-" vertices, and does not check whether neighboring vertices are identical.\n"\r
-);\r
- printf(\r
-" Be forewarned that if the Delaunay triangulation is degenerate or\n");\r
- printf(\r
-" near-degenerate, the Voronoi diagram may have duplicate points, crossing\n"\r
-);\r
- printf(\r
-" edges, or infinite rays whose direction vector is zero. Also, if you\n");\r
- printf(\r
-" generate a constrained (as opposed to conforming) Delaunay triangulation,\n"\r
-);\r
- printf(\r
-" or if the triangulation has holes, the corresponding Voronoi diagram is\n");\r
- printf(" likely to have crossing edges and unlikely to make sense.\n\n");\r
- printf("Mesh Topology:\n\n");\r
- printf(\r
-" You may wish to know which triangles are adjacent to a certain Delaunay\n");\r
- printf(\r
-" edge in an .edge file, which Voronoi regions are adjacent to a certain\n");\r
- printf(\r
-" Voronoi edge in a .v.edge file, or which Voronoi regions are adjacent to\n"\r
-);\r
- printf(\r
-" each other. All of this information can be found by cross-referencing\n");\r
- printf(\r
-" output files with the recollection that the Delaunay triangulation and\n");\r
- printf(" the Voronoi diagrams are planar duals.\n\n");\r
- printf(\r
-" Specifically, edge i of an .edge file is the dual of Voronoi edge i of\n");\r
- printf(\r
-" the corresponding .v.edge file, and is rotated 90 degrees counterclock-\n");\r
- printf(\r
-" wise from the Voronoi edge. Triangle j of an .ele file is the dual of\n");\r
- printf(\r
-" vertex j of the corresponding .v.node file; and Voronoi region k is the\n");\r
- printf(" dual of point k of the corresponding .node file.\n\n");\r
- printf(\r
-" Hence, to find the triangles adjacent to a Delaunay edge, look at the\n");\r
- printf(\r
-" vertices of the corresponding Voronoi edge; their dual triangles are on\n");\r
- printf(\r
-" the left and right of the Delaunay edge, respectively. To find the\n");\r
- printf(\r
-" Voronoi regions adjacent to a Voronoi edge, look at the endpoints of the\n"\r
-);\r
- printf(\r
-" corresponding Delaunay edge; their dual regions are on the right and left\n"\r
-);\r
- printf(\r
-" of the Voronoi edge, respectively. To find which Voronoi regions are\n");\r
- printf(" adjacent to each other, just read the list of Delaunay edges.\n");\r
- printf("\n");\r
- printf("Statistics:\n");\r
- printf("\n");\r
- printf(\r
-" After generating a mesh, Triangle prints a count of the number of points,\n"\r
-);\r
- printf(\r
-" triangles, edges, boundary edges, and segments in the output mesh. If\n");\r
- printf(\r
-" you've forgotten the statistics for an existing mesh, the -rNEP switches\n"\r
-);\r
- printf(\r
-" (or -rpNEP if you've got a .poly file for the existing mesh) will\n");\r
- printf(" regenerate these statistics without writing any output.\n\n");\r
- printf(\r
-" The -V switch produces extended statistics, including a rough estimate\n");\r
- printf(\r
-" of memory use and a histogram of triangle aspect ratios and angles in the\n"\r
-);\r
- printf(" mesh.\n\n");\r
- printf("Exact Arithmetic:\n\n");\r
- printf(\r
-" Triangle uses adaptive exact arithmetic to perform what computational\n");\r
- printf(\r
-" geometers call the `orientation' and `incircle' tests. If the floating-\n"\r
-);\r
- printf(\r
-" point arithmetic of your machine conforms to the IEEE 754 standard (as\n");\r
- printf(\r
-" most workstations do), and does not use extended precision internal\n");\r
- printf(\r
-" registers, then your output is guaranteed to be an absolutely true\n");\r
- printf(" Delaunay or conforming Delaunay triangulation, roundoff error\n");\r
- printf(\r
-" notwithstanding. The word `adaptive' implies that these arithmetic\n");\r
- printf(\r
-" routines compute the result only to the precision necessary to guarantee\n"\r
-);\r
- printf(\r
-" correctness, so they are usually nearly as fast as their approximate\n");\r
- printf(\r
-" counterparts. The exact tests can be disabled with the -X switch. On\n");\r
- printf(\r
-" most inputs, this switch will reduce the computation time by about eight\n"\r
-);\r
- printf(\r
-" percent - it's not worth the risk. There are rare difficult inputs\n");\r
- printf(\r
-" (having many collinear and cocircular points), however, for which the\n");\r
- printf(\r
-" difference could be a factor of two. These are precisely the inputs most\n"\r
-);\r
- printf(" likely to cause errors if you use the -X switch.\n\n");\r
- printf(\r
-" Unfortunately, these routines don't solve every numerical problem. Exact\n"\r
-);\r
- printf(\r
-" arithmetic is not used to compute the positions of points, because the\n");\r
- printf(\r
-" bit complexity of point coordinates would grow without bound. Hence,\n");\r
- printf(\r
-" segment intersections aren't computed exactly; in very unusual cases,\n");\r
- printf(\r
-" roundoff error in computing an intersection point might actually lead to\n"\r
-);\r
- printf(\r
-" an inverted triangle and an invalid triangulation. (This is one reason\n");\r
- printf(\r
-" to compute your own intersection points in your .poly files.) Similarly,\n"\r
-);\r
- printf(\r
-" exact arithmetic is not used to compute the vertices of the Voronoi\n");\r
- printf(" diagram.\n\n");\r
- printf(\r
-" Underflow and overflow can also cause difficulties; the exact arithmetic\n"\r
-);\r
- printf(\r
-" routines do not ameliorate out-of-bounds exponents, which can arise\n");\r
- printf(\r
-" during the orientation and incircle tests. As a rule of thumb, you\n");\r
- printf(\r
-" should ensure that your input values are within a range such that their\n");\r
- printf(\r
-" third powers can be taken without underflow or overflow. Underflow can\n");\r
- printf(\r
-" silently prevent the tests from being performed exactly, while overflow\n");\r
- printf(" will typically cause a floating exception.\n\n");\r
- printf("Calling Triangle from Another Program:\n\n");\r
- printf(" Read the file triangle.h for details.\n\n");\r
- printf("Troubleshooting:\n\n");\r
- printf(" Please read this section before mailing me bugs.\n\n");\r
- printf(" `My output mesh has no triangles!'\n\n");\r
- printf(\r
-" If you're using a PSLG, you've probably failed to specify a proper set\n"\r
-);\r
- printf(\r
-" of bounding segments, or forgotten to use the -c switch. Or you may\n");\r
- printf(\r
-" have placed a hole badly. To test these possibilities, try again with\n"\r
-);\r
- printf(\r
-" the -c and -O switches. Alternatively, all your input points may be\n");\r
- printf(\r
-" collinear, in which case you can hardly expect to triangulate them.\n");\r
- printf("\n");\r
- printf(" `Triangle doesn't terminate, or just crashes.'\n");\r
- printf("\n");\r
- printf(\r
-" Bad things can happen when triangles get so small that the distance\n");\r
- printf(\r
-" between their vertices isn't much larger than the precision of your\n");\r
- printf(\r
-" machine's arithmetic. If you've compiled Triangle for single-precision\n"\r
-);\r
- printf(\r
-" arithmetic, you might do better by recompiling it for double-precision.\n"\r
-);\r
- printf(\r
-" Then again, you might just have to settle for more lenient constraints\n"\r
-);\r
- printf(\r
-" on the minimum angle and the maximum area than you had planned.\n");\r
- printf("\n");\r
- printf(\r
-" You can minimize precision problems by ensuring that the origin lies\n");\r
- printf(\r
-" inside your point set, or even inside the densest part of your\n");\r
- printf(\r
-" mesh. On the other hand, if you're triangulating an object whose x\n");\r
- printf(\r
-" coordinates all fall between 6247133 and 6247134, you're not leaving\n");\r
- printf(" much floating-point precision for Triangle to work with.\n\n");\r
- printf(\r
-" Precision problems can occur covertly if the input PSLG contains two\n");\r
- printf(\r
-" segments that meet (or intersect) at a very small angle, or if such an\n"\r
-);\r
- printf(\r
-" angle is introduced by the -c switch, which may occur if a point lies\n");\r
- printf(\r
-" ever-so-slightly inside the convex hull, and is connected by a PSLG\n");\r
- printf(\r
-" segment to a point on the convex hull. If you don't realize that a\n");\r
- printf(\r
-" small angle is being formed, you might never discover why Triangle is\n");\r
- printf(\r
-" crashing. To check for this possibility, use the -S switch (with an\n");\r
- printf(\r
-" appropriate limit on the number of Steiner points, found by trial-and-\n"\r
-);\r
- printf(\r
-" error) to stop Triangle early, and view the output .poly file with\n");\r
- printf(\r
-" Show Me (described below). Look carefully for small angles between\n");\r
- printf(\r
-" segments; zoom in closely, as such segments might look like a single\n");\r
- printf(" segment from a distance.\n\n");\r
- printf(\r
-" If some of the input values are too large, Triangle may suffer a\n");\r
- printf(\r
-" floating exception due to overflow when attempting to perform an\n");\r
- printf(\r
-" orientation or incircle test. (Read the section on exact arithmetic\n");\r
- printf(\r
-" above.) Again, I recommend compiling Triangle for double (rather\n");\r
- printf(" than single) precision arithmetic.\n\n");\r
- printf(\r
-" `The numbering of the output points doesn't match the input points.'\n");\r
- printf("\n");\r
- printf(\r
-" You may have eaten some of your input points with a hole, or by placing\n"\r
-);\r
- printf(" them outside the area enclosed by segments.\n\n");\r
- printf(\r
-" `Triangle executes without incident, but when I look at the resulting\n");\r
- printf(\r
-" mesh, it has overlapping triangles or other geometric inconsistencies.'\n");\r
- printf("\n");\r
- printf(\r
-" If you select the -X switch, Triangle's divide-and-conquer Delaunay\n");\r
- printf(\r
-" triangulation algorithm occasionally makes mistakes due to floating-\n");\r
- printf(\r
-" point roundoff error. Although these errors are rare, don't use the -X\n"\r
-);\r
- printf(" switch. If you still have problems, please report the bug.\n");\r
- printf("\n");\r
- printf(\r
-" Strange things can happen if you've taken liberties with your PSLG. Do\n");\r
- printf(\r
-" you have a point lying in the middle of a segment? Triangle sometimes\n");\r
- printf(\r
-" copes poorly with that sort of thing. Do you want to lay out a collinear\n"\r
-);\r
- printf(\r
-" row of evenly spaced, segment-connected points? Have you simply defined\n"\r
-);\r
- printf(\r
-" one long segment connecting the leftmost point to the rightmost point,\n");\r
- printf(\r
-" and a bunch of points lying along it? This method occasionally works,\n");\r
- printf(\r
-" especially with horizontal and vertical lines, but often it doesn't, and\n"\r
-);\r
- printf(\r
-" you'll have to connect each adjacent pair of points with a separate\n");\r
- printf(" segment. If you don't like it, tough.\n\n");\r
- printf(\r
-" Furthermore, if you have segments that intersect other than at their\n");\r
- printf(\r
-" endpoints, try not to let the intersections fall extremely close to PSLG\n"\r
-);\r
- printf(" points or each other.\n\n");\r
- printf(\r
-" If you have problems refining a triangulation not produced by Triangle:\n");\r
- printf(\r
-" Are you sure the triangulation is geometrically valid? Is it formatted\n");\r
- printf(\r
-" correctly for Triangle? Are the triangles all listed so the first three\n"\r
-);\r
- printf(" points are their corners in counterclockwise order?\n\n");\r
- printf("Show Me:\n\n");\r
- printf(\r
-" Triangle comes with a separate program named `Show Me', whose primary\n");\r
- printf(\r
-" purpose is to draw meshes on your screen or in PostScript. Its secondary\n"\r
-);\r
- printf(\r
-" purpose is to check the validity of your input files, and do so more\n");\r
- printf(\r
-" thoroughly than Triangle does. Show Me requires that you have the X\n");\r
- printf(\r
-" Windows system. If you didn't receive Show Me with Triangle, complain to\n"\r
-);\r
- printf(" whomever you obtained Triangle from, then send me mail.\n\n");\r
- printf("Triangle on the Web:\n\n");\r
- printf(\r
-" To see an illustrated, updated version of these instructions, check out\n");\r
- printf("\n");\r
- printf(" http://www.cs.cmu.edu/~quake/triangle.html\n");\r
- printf("\n");\r
- printf("A Brief Plea:\n");\r
- printf("\n");\r
- printf(\r
-" If you use Triangle, and especially if you use it to accomplish real\n");\r
- printf(\r
-" work, I would like very much to hear from you. A short letter or email\n");\r
- printf(\r
-" (to jrs@cs.cmu.edu) describing how you use Triangle will mean a lot to\n");\r
- printf(\r
-" me. The more people I know are using this program, the more easily I can\n"\r
-);\r
- printf(\r
-" justify spending time on improvements and on the three-dimensional\n");\r
- printf(\r
-" successor to Triangle, which in turn will benefit you. Also, I can put\n");\r
- printf(\r
-" you on a list to receive email whenever a new version of Triangle is\n");\r
- printf(" available.\n\n");\r
- printf(\r
-" If you use a mesh generated by Triangle in a publication, please include\n"\r
-);\r
- printf(" an acknowledgment as well.\n\n");\r
- printf("Research credit:\n\n");\r
- printf(\r
-" Of course, I can take credit for only a fraction of the ideas that made\n");\r
- printf(\r
-" this mesh generator possible. Triangle owes its existence to the efforts\n"\r
-);\r
- printf(\r
-" of many fine computational geometers and other researchers, including\n");\r
- printf(\r
-" Marshall Bern, L. Paul Chew, Boris Delaunay, Rex A. Dwyer, David\n");\r
- printf(\r
-" Eppstein, Steven Fortune, Leonidas J. Guibas, Donald E. Knuth, C. L.\n");\r
- printf(\r
-" Lawson, Der-Tsai Lee, Ernst P. Mucke, Douglas M. Priest, Jim Ruppert,\n");\r
- printf(\r
-" Isaac Saias, Bruce J. Schachter, Micha Sharir, Jorge Stolfi, Christopher\n"\r
-);\r
- printf(\r
-" J. Van Wyk, David F. Watson, and Binhai Zhu. See the comments at the\n");\r
- printf(" beginning of the source code for references.\n\n");\r
- exit(0);\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* internalerror() Ask the user to send me the defective product. Exit. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void internalerror()\r
-{\r
- printf(" Please report this bug to jrs@cs.cmu.edu\n");\r
- printf(" Include the message above, your input data set, and the exact\n");\r
- printf(" command line you used to run Triangle.\n");\r
- exit(1);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* parsecommandline() Read the command line, identify switches, and set */\r
-/* up options and file names. */\r
-/* */\r
-/* The effects of this routine are felt entirely through global variables. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void parsecommandline(argc, argv)\r
-int argc;\r
-char **argv;\r
-{\r
-#ifdef TRILIBRARY\r
-#define STARTINDEX 0\r
-#else /* not TRILIBRARY */\r
-#define STARTINDEX 1\r
- int increment;\r
- int meshnumber;\r
-#endif /* not TRILIBRARY */\r
- int i, j;\r
-#ifndef CDT_ONLY\r
- int k;\r
- char workstring[FILENAMESIZE];\r
-#endif\r
-\r
- poly = refine = quality = vararea = fixedarea = regionattrib = convex = 0;\r
- firstnumber = 1;\r
- edgesout = voronoi = neighbors = geomview = 0;\r
- nobound = nopolywritten = nonodewritten = noelewritten = noiterationnum = 0;\r
- noholes = noexact = 0;\r
- incremental = sweepline = 0;\r
- dwyer = 1;\r
- splitseg = 0;\r
- docheck = 0;\r
- nobisect = 0;\r
- steiner = -1;\r
- order = 1;\r
- minangle = 0.0;\r
- maxarea = -1.0;\r
- quiet = verbose = 0;\r
-#ifndef TRILIBRARY\r
- innodefilename[0] = '\0';\r
-#endif /* not TRILIBRARY */\r
-\r
- for (i = STARTINDEX; i < argc; i++) {\r
-#ifndef TRILIBRARY\r
- if (argv[i][0] == '-') {\r
-#endif /* not TRILIBRARY */\r
- for (j = STARTINDEX; argv[i][j] != '\0'; j++) {\r
- if (argv[i][j] == 'p') {\r
- poly = 1;\r
- }\r
-#ifndef CDT_ONLY\r
- if (argv[i][j] == 'r') {\r
- refine = 1;\r
- }\r
- if (argv[i][j] == 'q') {\r
- quality = 1;\r
- if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||\r
- (argv[i][j + 1] == '.')) {\r
- k = 0;\r
- while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||\r
- (argv[i][j + 1] == '.')) {\r
- j++;\r
- workstring[k] = argv[i][j];\r
- k++;\r
- }\r
- workstring[k] = '\0';\r
- minangle = (REAL) strtod(workstring, (char **) NULL);\r
- } else {\r
- minangle = 20.0;\r
- }\r
- }\r
- if (argv[i][j] == 'a') {\r
- quality = 1;\r
- if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||\r
- (argv[i][j + 1] == '.')) {\r
- fixedarea = 1;\r
- k = 0;\r
- while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||\r
- (argv[i][j + 1] == '.')) {\r
- j++;\r
- workstring[k] = argv[i][j];\r
- k++;\r
- }\r
- workstring[k] = '\0';\r
- maxarea = (REAL) strtod(workstring, (char **) NULL);\r
- if (maxarea <= 0.0) {\r
- printf("Error: Maximum area must be greater than zero.\n");\r
- exit(1);\r
- }\r
- } else {\r
- vararea = 1;\r
- }\r
- }\r
-#endif /* not CDT_ONLY */\r
- if (argv[i][j] == 'A') {\r
- regionattrib = 1;\r
- }\r
- if (argv[i][j] == 'c') {\r
- convex = 1;\r
- }\r
- if (argv[i][j] == 'z') {\r
- firstnumber = 0;\r
- }\r
- if (argv[i][j] == 'e') {\r
- edgesout = 1;\r
- }\r
- if (argv[i][j] == 'v') {\r
- voronoi = 1;\r
- }\r
- if (argv[i][j] == 'n') {\r
- neighbors = 1;\r
- }\r
- if (argv[i][j] == 'g') {\r
- geomview = 1;\r
- }\r
- if (argv[i][j] == 'B') {\r
- nobound = 1;\r
- }\r
- if (argv[i][j] == 'P') {\r
- nopolywritten = 1;\r
- }\r
- if (argv[i][j] == 'N') {\r
- nonodewritten = 1;\r
- }\r
- if (argv[i][j] == 'E') {\r
- noelewritten = 1;\r
- }\r
-#ifndef TRILIBRARY\r
- if (argv[i][j] == 'I') {\r
- noiterationnum = 1;\r
- }\r
-#endif /* not TRILIBRARY */\r
- if (argv[i][j] == 'O') {\r
- noholes = 1;\r
- }\r
- if (argv[i][j] == 'X') {\r
- noexact = 1;\r
- }\r
- if (argv[i][j] == 'o') {\r
- if (argv[i][j + 1] == '2') {\r
- j++;\r
- order = 2;\r
- }\r
- }\r
-#ifndef CDT_ONLY\r
- if (argv[i][j] == 'Y') {\r
- nobisect++;\r
- }\r
- if (argv[i][j] == 'S') {\r
- steiner = 0;\r
- while ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) {\r
- j++;\r
- steiner = steiner * 10 + (int) (argv[i][j] - '0');\r
- }\r
- }\r
-#endif /* not CDT_ONLY */\r
-#ifndef REDUCED\r
- if (argv[i][j] == 'i') {\r
- incremental = 1;\r
- }\r
- if (argv[i][j] == 'F') {\r
- sweepline = 1;\r
- }\r
-#endif /* not REDUCED */\r
- if (argv[i][j] == 'l') {\r
- dwyer = 0;\r
- }\r
-#ifndef REDUCED\r
-#ifndef CDT_ONLY\r
- if (argv[i][j] == 's') {\r
- splitseg = 1;\r
- }\r
-#endif /* not CDT_ONLY */\r
- if (argv[i][j] == 'C') {\r
- docheck = 1;\r
- }\r
-#endif /* not REDUCED */\r
- if (argv[i][j] == 'Q') {\r
- quiet = 1;\r
- }\r
- if (argv[i][j] == 'V') {\r
- verbose++;\r
- }\r
-#ifndef TRILIBRARY\r
- if ((argv[i][j] == 'h') || (argv[i][j] == 'H') ||\r
- (argv[i][j] == '?')) {\r
- info();\r
- }\r
-#endif /* not TRILIBRARY */\r
- }\r
-#ifndef TRILIBRARY\r
- } else {\r
- strncpy(innodefilename, argv[i], FILENAMESIZE - 1);\r
- innodefilename[FILENAMESIZE - 1] = '\0';\r
- }\r
-#endif /* not TRILIBRARY */\r
- }\r
-#ifndef TRILIBRARY\r
- if (innodefilename[0] == '\0') {\r
- syntax();\r
- }\r
- if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".node")) {\r
- innodefilename[strlen(innodefilename) - 5] = '\0';\r
- }\r
- if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".poly")) {\r
- innodefilename[strlen(innodefilename) - 5] = '\0';\r
- poly = 1;\r
- }\r
-#ifndef CDT_ONLY\r
- if (!strcmp(&innodefilename[strlen(innodefilename) - 4], ".ele")) {\r
- innodefilename[strlen(innodefilename) - 4] = '\0';\r
- refine = 1;\r
- }\r
- if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".area")) {\r
- innodefilename[strlen(innodefilename) - 5] = '\0';\r
- refine = 1;\r
- quality = 1;\r
- vararea = 1;\r
- }\r
-#endif /* not CDT_ONLY */\r
-#endif /* not TRILIBRARY */\r
- steinerleft = steiner;\r
- useshelles = poly || refine || quality || convex;\r
- goodangle = (REAL)cos(minangle * PI / 180.0);\r
- goodangle *= goodangle;\r
- if (refine && noiterationnum) {\r
- printf(\r
- "Error: You cannot use the -I switch when refining a triangulation.\n");\r
- exit(1);\r
- }\r
- /* Be careful not to allocate space for element area constraints that */\r
- /* will never be assigned any value (other than the default -1.0). */\r
- if (!refine && !poly) {\r
- vararea = 0;\r
- }\r
- /* Be careful not to add an extra attribute to each element unless the */\r
- /* input supports it (PSLG in, but not refining a preexisting mesh). */\r
- if (refine || !poly) {\r
- regionattrib = 0;\r
- }\r
-\r
-#ifndef TRILIBRARY\r
- strcpy(inpolyfilename, innodefilename);\r
- strcpy(inelefilename, innodefilename);\r
- strcpy(areafilename, innodefilename);\r
- increment = 0;\r
- strcpy(workstring, innodefilename);\r
- j = 1;\r
- while (workstring[j] != '\0') {\r
- if ((workstring[j] == '.') && (workstring[j + 1] != '\0')) {\r
- increment = j + 1;\r
- }\r
- j++;\r
- }\r
- meshnumber = 0;\r
- if (increment > 0) {\r
- j = increment;\r
- do {\r
- if ((workstring[j] >= '0') && (workstring[j] <= '9')) {\r
- meshnumber = meshnumber * 10 + (int) (workstring[j] - '0');\r
- } else {\r
- increment = 0;\r
- }\r
- j++;\r
- } while (workstring[j] != '\0');\r
- }\r
- if (noiterationnum) {\r
- strcpy(outnodefilename, innodefilename);\r
- strcpy(outelefilename, innodefilename);\r
- strcpy(edgefilename, innodefilename);\r
- strcpy(vnodefilename, innodefilename);\r
- strcpy(vedgefilename, innodefilename);\r
- strcpy(neighborfilename, innodefilename);\r
- strcpy(offfilename, innodefilename);\r
- strcat(outnodefilename, ".node");\r
- strcat(outelefilename, ".ele");\r
- strcat(edgefilename, ".edge");\r
- strcat(vnodefilename, ".v.node");\r
- strcat(vedgefilename, ".v.edge");\r
- strcat(neighborfilename, ".neigh");\r
- strcat(offfilename, ".off");\r
- } else if (increment == 0) {\r
- strcpy(outnodefilename, innodefilename);\r
- strcpy(outpolyfilename, innodefilename);\r
- strcpy(outelefilename, innodefilename);\r
- strcpy(edgefilename, innodefilename);\r
- strcpy(vnodefilename, innodefilename);\r
- strcpy(vedgefilename, innodefilename);\r
- strcpy(neighborfilename, innodefilename);\r
- strcpy(offfilename, innodefilename);\r
- strcat(outnodefilename, ".1.node");\r
- strcat(outpolyfilename, ".1.poly");\r
- strcat(outelefilename, ".1.ele");\r
- strcat(edgefilename, ".1.edge");\r
- strcat(vnodefilename, ".1.v.node");\r
- strcat(vedgefilename, ".1.v.edge");\r
- strcat(neighborfilename, ".1.neigh");\r
- strcat(offfilename, ".1.off");\r
- } else {\r
- workstring[increment] = '%';\r
- workstring[increment + 1] = 'd';\r
- workstring[increment + 2] = '\0';\r
- sprintf(outnodefilename, workstring, meshnumber + 1);\r
- strcpy(outpolyfilename, outnodefilename);\r
- strcpy(outelefilename, outnodefilename);\r
- strcpy(edgefilename, outnodefilename);\r
- strcpy(vnodefilename, outnodefilename);\r
- strcpy(vedgefilename, outnodefilename);\r
- strcpy(neighborfilename, outnodefilename);\r
- strcpy(offfilename, outnodefilename);\r
- strcat(outnodefilename, ".node");\r
- strcat(outpolyfilename, ".poly");\r
- strcat(outelefilename, ".ele");\r
- strcat(edgefilename, ".edge");\r
- strcat(vnodefilename, ".v.node");\r
- strcat(vedgefilename, ".v.edge");\r
- strcat(neighborfilename, ".neigh");\r
- strcat(offfilename, ".off");\r
- }\r
- strcat(innodefilename, ".node");\r
- strcat(inpolyfilename, ".poly");\r
- strcat(inelefilename, ".ele");\r
- strcat(areafilename, ".area");\r
-#endif /* not TRILIBRARY */\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* User interaction routines begin here *********/\r
-\r
-/********* Debugging routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* printtriangle() Print out the details of a triangle/edge handle. */\r
-/* */\r
-/* I originally wrote this procedure to simplify debugging; it can be */\r
-/* called directly from the debugger, and presents information about a */\r
-/* triangle/edge handle in digestible form. It's also used when the */\r
-/* highest level of verbosity (`-VVV') is specified. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void printtriangle(t)\r
-struct triedge *t;\r
-{\r
- struct triedge printtri;\r
- struct edge printsh;\r
- point printpoint;\r
-\r
- printf("triangle x%lx with orientation %d:\n", (unsigned long) t->tri,\r
- t->orient);\r
- decode(t->tri[0], printtri);\r
- if (printtri.tri == dummytri) {\r
- printf(" [0] = Outer space\n");\r
- } else {\r
- printf(" [0] = x%lx %d\n", (unsigned long) printtri.tri,\r
- printtri.orient);\r
- }\r
- decode(t->tri[1], printtri);\r
- if (printtri.tri == dummytri) {\r
- printf(" [1] = Outer space\n");\r
- } else {\r
- printf(" [1] = x%lx %d\n", (unsigned long) printtri.tri,\r
- printtri.orient);\r
- }\r
- decode(t->tri[2], printtri);\r
- if (printtri.tri == dummytri) {\r
- printf(" [2] = Outer space\n");\r
- } else {\r
- printf(" [2] = x%lx %d\n", (unsigned long) printtri.tri,\r
- printtri.orient);\r
- }\r
- org(*t, printpoint);\r
- if (printpoint == (point) NULL)\r
- printf(" Origin[%d] = NULL\n", (t->orient + 1) % 3 + 3);\r
- else\r
- printf(" Origin[%d] = x%lx (%.12g, %.12g)\n",\r
- (t->orient + 1) % 3 + 3, (unsigned long) printpoint,\r
- printpoint[0], printpoint[1]);\r
- dest(*t, printpoint);\r
- if (printpoint == (point) NULL)\r
- printf(" Dest [%d] = NULL\n", (t->orient + 2) % 3 + 3);\r
- else\r
- printf(" Dest [%d] = x%lx (%.12g, %.12g)\n",\r
- (t->orient + 2) % 3 + 3, (unsigned long) printpoint,\r
- printpoint[0], printpoint[1]);\r
- apex(*t, printpoint);\r
- if (printpoint == (point) NULL)\r
- printf(" Apex [%d] = NULL\n", t->orient + 3);\r
- else\r
- printf(" Apex [%d] = x%lx (%.12g, %.12g)\n",\r
- t->orient + 3, (unsigned long) printpoint,\r
- printpoint[0], printpoint[1]);\r
- if (useshelles) {\r
- sdecode(t->tri[6], printsh);\r
- if (printsh.sh != dummysh) {\r
- printf(" [6] = x%lx %d\n", (unsigned long) printsh.sh,\r
- printsh.shorient);\r
- }\r
- sdecode(t->tri[7], printsh);\r
- if (printsh.sh != dummysh) {\r
- printf(" [7] = x%lx %d\n", (unsigned long) printsh.sh,\r
- printsh.shorient);\r
- }\r
- sdecode(t->tri[8], printsh);\r
- if (printsh.sh != dummysh) {\r
- printf(" [8] = x%lx %d\n", (unsigned long) printsh.sh,\r
- printsh.shorient);\r
- }\r
- }\r
- if (vararea) {\r
- printf(" Area constraint: %.4g\n", areabound(*t));\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* printshelle() Print out the details of a shell edge handle. */\r
-/* */\r
-/* I originally wrote this procedure to simplify debugging; it can be */\r
-/* called directly from the debugger, and presents information about a */\r
-/* shell edge handle in digestible form. It's also used when the highest */\r
-/* level of verbosity (`-VVV') is specified. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void printshelle(s)\r
-struct edge *s;\r
-{\r
- struct edge printsh;\r
- struct triedge printtri;\r
- point printpoint;\r
-\r
- printf("shell edge x%lx with orientation %d and mark %d:\n",\r
- (unsigned long) s->sh, s->shorient, mark(*s));\r
- sdecode(s->sh[0], printsh);\r
- if (printsh.sh == dummysh) {\r
- printf(" [0] = No shell\n");\r
- } else {\r
- printf(" [0] = x%lx %d\n", (unsigned long) printsh.sh,\r
- printsh.shorient);\r
- }\r
- sdecode(s->sh[1], printsh);\r
- if (printsh.sh == dummysh) {\r
- printf(" [1] = No shell\n");\r
- } else {\r
- printf(" [1] = x%lx %d\n", (unsigned long) printsh.sh,\r
- printsh.shorient);\r
- }\r
- sorg(*s, printpoint);\r
- if (printpoint == (point) NULL)\r
- printf(" Origin[%d] = NULL\n", 2 + s->shorient);\r
- else\r
- printf(" Origin[%d] = x%lx (%.12g, %.12g)\n",\r
- 2 + s->shorient, (unsigned long) printpoint,\r
- printpoint[0], printpoint[1]);\r
- sdest(*s, printpoint);\r
- if (printpoint == (point) NULL)\r
- printf(" Dest [%d] = NULL\n", 3 - s->shorient);\r
- else\r
- printf(" Dest [%d] = x%lx (%.12g, %.12g)\n",\r
- 3 - s->shorient, (unsigned long) printpoint,\r
- printpoint[0], printpoint[1]);\r
- decode(s->sh[4], printtri);\r
- if (printtri.tri == dummytri) {\r
- printf(" [4] = Outer space\n");\r
- } else {\r
- printf(" [4] = x%lx %d\n", (unsigned long) printtri.tri,\r
- printtri.orient);\r
- }\r
- decode(s->sh[5], printtri);\r
- if (printtri.tri == dummytri) {\r
- printf(" [5] = Outer space\n");\r
- } else {\r
- printf(" [5] = x%lx %d\n", (unsigned long) printtri.tri,\r
- printtri.orient);\r
- }\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Debugging routines end here *********/\r
-\r
-/********* Memory management routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* poolinit() Initialize a pool of memory for allocation of items. */\r
-/* */\r
-/* This routine initializes the machinery for allocating items. A `pool' */\r
-/* is created whose records have size at least `bytecount'. Items will be */\r
-/* allocated in `itemcount'-item blocks. Each item is assumed to be a */\r
-/* collection of words, and either pointers or floating-point values are */\r
-/* assumed to be the "primary" word type. (The "primary" word type is used */\r
-/* to determine alignment of items.) If `alignment' isn't zero, all items */\r
-/* will be `alignment'-byte aligned in memory. `alignment' must be either */\r
-/* a multiple or a factor of the primary word size; powers of two are safe. */\r
-/* `alignment' is normally used to create a few unused bits at the bottom */\r
-/* of each item's pointer, in which information may be stored. */\r
-/* */\r
-/* Don't change this routine unless you understand it. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void poolinit(pool, bytecount, itemcount, wtype, alignment)\r
-struct memorypool *pool;\r
-int bytecount;\r
-int itemcount;\r
-enum wordtype wtype;\r
-int alignment;\r
-{\r
- int wordsize;\r
-\r
- /* Initialize values in the pool. */\r
- pool->itemwordtype = wtype;\r
- wordsize = (pool->itemwordtype == POINTER) ? sizeof(VOID *) : sizeof(REAL);\r
- /* Find the proper alignment, which must be at least as large as: */\r
- /* - The parameter `alignment'. */\r
- /* - The primary word type, to avoid unaligned accesses. */\r
- /* - sizeof(VOID *), so the stack of dead items can be maintained */\r
- /* without unaligned accesses. */\r
- if (alignment > wordsize) {\r
- pool->alignbytes = alignment;\r
- } else {\r
- pool->alignbytes = wordsize;\r
- }\r
- if (sizeof(VOID *) > pool->alignbytes) {\r
- pool->alignbytes = sizeof(VOID *);\r
- }\r
- pool->itemwords = ((bytecount + pool->alignbytes - 1) / pool->alignbytes)\r
- * (pool->alignbytes / wordsize);\r
- pool->itembytes = pool->itemwords * wordsize;\r
- pool->itemsperblock = itemcount;\r
-\r
- /* Allocate a block of items. Space for `itemsperblock' items and one */\r
- /* pointer (to point to the next block) are allocated, as well as space */\r
- /* to ensure alignment of the items. */\r
- pool->firstblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes\r
- + sizeof(VOID *) + pool->alignbytes);\r
- if (pool->firstblock == (VOID **) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- /* Set the next block pointer to NULL. */\r
- *(pool->firstblock) = (VOID *) NULL;\r
- poolrestart(pool);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* poolrestart() Deallocate all items in a pool. */\r
-/* */\r
-/* The pool is returned to its starting state, except that no memory is */\r
-/* freed to the operating system. Rather, the previously allocated blocks */\r
-/* are ready to be reused. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void poolrestart(pool)\r
-struct memorypool *pool;\r
-{\r
- unsigned long alignptr;\r
-\r
- pool->items = 0;\r
- pool->maxitems = 0;\r
-\r
- /* Set the currently active block. */\r
- pool->nowblock = pool->firstblock;\r
- /* Find the first item in the pool. Increment by the size of (VOID *). */\r
- alignptr = (unsigned long) (pool->nowblock + 1);\r
- /* Align the item on an `alignbytes'-byte boundary. */\r
- pool->nextitem = (VOID *)\r
- (alignptr + (unsigned long) pool->alignbytes\r
- - (alignptr % (unsigned long) pool->alignbytes));\r
- /* There are lots of unallocated items left in this block. */\r
- pool->unallocateditems = pool->itemsperblock;\r
- /* The stack of deallocated items is empty. */\r
- pool->deaditemstack = (VOID *) NULL;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* pooldeinit() Free to the operating system all memory taken by a pool. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void pooldeinit(pool)\r
-struct memorypool *pool;\r
-{\r
- while (pool->firstblock != (VOID **) NULL) {\r
- pool->nowblock = (VOID **) *(pool->firstblock);\r
- free(pool->firstblock);\r
- pool->firstblock = pool->nowblock;\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* poolalloc() Allocate space for an item. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-VOID *poolalloc(pool)\r
-struct memorypool *pool;\r
-{\r
- VOID *newitem;\r
- VOID **newblock;\r
- unsigned long alignptr;\r
-\r
- /* First check the linked list of dead items. If the list is not */\r
- /* empty, allocate an item from the list rather than a fresh one. */\r
- if (pool->deaditemstack != (VOID *) NULL) {\r
- newitem = pool->deaditemstack; /* Take first item in list. */\r
- pool->deaditemstack = * (VOID **) pool->deaditemstack;\r
- } else {\r
- /* Check if there are any free items left in the current block. */\r
- if (pool->unallocateditems == 0) {\r
- /* Check if another block must be allocated. */\r
- if (*(pool->nowblock) == (VOID *) NULL) {\r
- /* Allocate a new block of items, pointed to by the previous block. */\r
- newblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes\r
- + sizeof(VOID *) + pool->alignbytes);\r
- if (newblock == (VOID **) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- *(pool->nowblock) = (VOID *) newblock;\r
- /* The next block pointer is NULL. */\r
- *newblock = (VOID *) NULL;\r
- }\r
- /* Move to the new block. */\r
- pool->nowblock = (VOID **) *(pool->nowblock);\r
- /* Find the first item in the block. */\r
- /* Increment by the size of (VOID *). */\r
- alignptr = (unsigned long) (pool->nowblock + 1);\r
- /* Align the item on an `alignbytes'-byte boundary. */\r
- pool->nextitem = (VOID *)\r
- (alignptr + (unsigned long) pool->alignbytes\r
- - (alignptr % (unsigned long) pool->alignbytes));\r
- /* There are lots of unallocated items left in this block. */\r
- pool->unallocateditems = pool->itemsperblock;\r
- }\r
- /* Allocate a new item. */\r
- newitem = pool->nextitem;\r
- /* Advance `nextitem' pointer to next free item in block. */\r
- if (pool->itemwordtype == POINTER) {\r
- pool->nextitem = (VOID *) ((VOID **) pool->nextitem + pool->itemwords);\r
- } else {\r
- pool->nextitem = (VOID *) ((REAL *) pool->nextitem + pool->itemwords);\r
- }\r
- pool->unallocateditems--;\r
- pool->maxitems++;\r
- }\r
- pool->items++;\r
- return newitem;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* pooldealloc() Deallocate space for an item. */\r
-/* */\r
-/* The deallocated space is stored in a queue for later reuse. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void pooldealloc(pool, dyingitem)\r
-struct memorypool *pool;\r
-VOID *dyingitem;\r
-{\r
- /* Push freshly killed item onto stack. */\r
- *((VOID **) dyingitem) = pool->deaditemstack;\r
- pool->deaditemstack = dyingitem;\r
- pool->items--;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* traversalinit() Prepare to traverse the entire list of items. */\r
-/* */\r
-/* This routine is used in conjunction with traverse(). */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void traversalinit(pool)\r
-struct memorypool *pool;\r
-{\r
- unsigned long alignptr;\r
-\r
- /* Begin the traversal in the first block. */\r
- pool->pathblock = pool->firstblock;\r
- /* Find the first item in the block. Increment by the size of (VOID *). */\r
- alignptr = (unsigned long) (pool->pathblock + 1);\r
- /* Align with item on an `alignbytes'-byte boundary. */\r
- pool->pathitem = (VOID *)\r
- (alignptr + (unsigned long) pool->alignbytes\r
- - (alignptr % (unsigned long) pool->alignbytes));\r
- /* Set the number of items left in the current block. */\r
- pool->pathitemsleft = pool->itemsperblock;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* traverse() Find the next item in the list. */\r
-/* */\r
-/* This routine is used in conjunction with traversalinit(). Be forewarned */\r
-/* that this routine successively returns all items in the list, including */\r
-/* deallocated ones on the deaditemqueue. It's up to you to figure out */\r
-/* which ones are actually dead. Why? I don't want to allocate extra */\r
-/* space just to demarcate dead items. It can usually be done more */\r
-/* space-efficiently by a routine that knows something about the structure */\r
-/* of the item. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-VOID *traverse(pool)\r
-struct memorypool *pool;\r
-{\r
- VOID *newitem;\r
- unsigned long alignptr;\r
-\r
- /* Stop upon exhausting the list of items. */\r
- if (pool->pathitem == pool->nextitem) {\r
- return (VOID *) NULL;\r
- }\r
- /* Check whether any untraversed items remain in the current block. */\r
- if (pool->pathitemsleft == 0) {\r
- /* Find the next block. */\r
- pool->pathblock = (VOID **) *(pool->pathblock);\r
- /* Find the first item in the block. Increment by the size of (VOID *). */\r
- alignptr = (unsigned long) (pool->pathblock + 1);\r
- /* Align with item on an `alignbytes'-byte boundary. */\r
- pool->pathitem = (VOID *)\r
- (alignptr + (unsigned long) pool->alignbytes\r
- - (alignptr % (unsigned long) pool->alignbytes));\r
- /* Set the number of items left in the current block. */\r
- pool->pathitemsleft = pool->itemsperblock;\r
- }\r
- newitem = pool->pathitem;\r
- /* Find the next item in the block. */\r
- if (pool->itemwordtype == POINTER) {\r
- pool->pathitem = (VOID *) ((VOID **) pool->pathitem + pool->itemwords);\r
- } else {\r
- pool->pathitem = (VOID *) ((REAL *) pool->pathitem + pool->itemwords);\r
- }\r
- pool->pathitemsleft--;\r
- return newitem;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* dummyinit() Initialize the triangle that fills "outer space" and the */\r
-/* omnipresent shell edge. */\r
-/* */\r
-/* The triangle that fills "outer space", called `dummytri', is pointed to */\r
-/* by every triangle and shell edge on a boundary (be it outer or inner) of */\r
-/* the triangulation. Also, `dummytri' points to one of the triangles on */\r
-/* the convex hull (until the holes and concavities are carved), making it */\r
-/* possible to find a starting triangle for point location. */\r
-/* */\r
-/* The omnipresent shell edge, `dummysh', is pointed to by every triangle */\r
-/* or shell edge that doesn't have a full complement of real shell edges */\r
-/* to point to. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void dummyinit(trianglewords, shellewords)\r
-int trianglewords;\r
-int shellewords;\r
-{\r
- unsigned long alignptr;\r
-\r
- /* `triwords' and `shwords' are used by the mesh manipulation primitives */\r
- /* to extract orientations of triangles and shell edges from pointers. */\r
- triwords = trianglewords; /* Initialize `triwords' once and for all. */\r
- shwords = shellewords; /* Initialize `shwords' once and for all. */\r
-\r
- /* Set up `dummytri', the `triangle' that occupies "outer space". */\r
- dummytribase = (triangle *) malloc(triwords * sizeof(triangle)\r
- + triangles.alignbytes);\r
- if (dummytribase == (triangle *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- /* Align `dummytri' on a `triangles.alignbytes'-byte boundary. */\r
- alignptr = (unsigned long) dummytribase;\r
- dummytri = (triangle *)\r
- (alignptr + (unsigned long) triangles.alignbytes\r
- - (alignptr % (unsigned long) triangles.alignbytes));\r
- /* Initialize the three adjoining triangles to be "outer space". These */\r
- /* will eventually be changed by various bonding operations, but their */\r
- /* values don't really matter, as long as they can legally be */\r
- /* dereferenced. */\r
- dummytri[0] = (triangle) dummytri;\r
- dummytri[1] = (triangle) dummytri;\r
- dummytri[2] = (triangle) dummytri;\r
- /* Three NULL vertex points. */\r
- dummytri[3] = (triangle) NULL;\r
- dummytri[4] = (triangle) NULL;\r
- dummytri[5] = (triangle) NULL;\r
-\r
- if (useshelles) {\r
- /* Set up `dummysh', the omnipresent "shell edge" pointed to by any */\r
- /* triangle side or shell edge end that isn't attached to a real shell */\r
- /* edge. */\r
- dummyshbase = (shelle *) malloc(shwords * sizeof(shelle)\r
- + shelles.alignbytes);\r
- if (dummyshbase == (shelle *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- /* Align `dummysh' on a `shelles.alignbytes'-byte boundary. */\r
- alignptr = (unsigned long) dummyshbase;\r
- dummysh = (shelle *)\r
- (alignptr + (unsigned long) shelles.alignbytes\r
- - (alignptr % (unsigned long) shelles.alignbytes));\r
- /* Initialize the two adjoining shell edges to be the omnipresent shell */\r
- /* edge. These will eventually be changed by various bonding */\r
- /* operations, but their values don't really matter, as long as they */\r
- /* can legally be dereferenced. */\r
- dummysh[0] = (shelle) dummysh;\r
- dummysh[1] = (shelle) dummysh;\r
- /* Two NULL vertex points. */\r
- dummysh[2] = (shelle) NULL;\r
- dummysh[3] = (shelle) NULL;\r
- /* Initialize the two adjoining triangles to be "outer space". */\r
- dummysh[4] = (shelle) dummytri;\r
- dummysh[5] = (shelle) dummytri;\r
- /* Set the boundary marker to zero. */\r
- * (int *) (dummysh + 6) = 0;\r
-\r
- /* Initialize the three adjoining shell edges of `dummytri' to be */\r
- /* the omnipresent shell edge. */\r
- dummytri[6] = (triangle) dummysh;\r
- dummytri[7] = (triangle) dummysh;\r
- dummytri[8] = (triangle) dummysh;\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* initializepointpool() Calculate the size of the point data structure */\r
-/* and initialize its memory pool. */\r
-/* */\r
-/* This routine also computes the `pointmarkindex' and `point2triindex' */\r
-/* indices used to find values within each point. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void initializepointpool()\r
-{\r
- int pointsize;\r
-\r
- /* The index within each point at which the boundary marker is found. */\r
- /* Ensure the point marker is aligned to a sizeof(int)-byte address. */\r
- pointmarkindex = ((mesh_dim + nextras) * sizeof(REAL) + sizeof(int) - 1)\r
- / sizeof(int);\r
- pointsize = (pointmarkindex + 1) * sizeof(int);\r
- if (poly) {\r
- /* The index within each point at which a triangle pointer is found. */\r
- /* Ensure the pointer is aligned to a sizeof(triangle)-byte address. */\r
- point2triindex = (pointsize + sizeof(triangle) - 1) / sizeof(triangle);\r
- pointsize = (point2triindex + 1) * sizeof(triangle);\r
- }\r
- /* Initialize the pool of points. */\r
- poolinit(&points, pointsize, POINTPERBLOCK,\r
- (sizeof(REAL) >= sizeof(triangle)) ? FLOATINGPOINT : POINTER, 0);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* initializetrisegpools() Calculate the sizes of the triangle and shell */\r
-/* edge data structures and initialize their */\r
-/* memory pools. */\r
-/* */\r
-/* This routine also computes the `highorderindex', `elemattribindex', and */\r
-/* `areaboundindex' indices used to find values within each triangle. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void initializetrisegpools()\r
-{\r
- int trisize;\r
-\r
- /* The index within each triangle at which the extra nodes (above three) */\r
- /* associated with high order elements are found. There are three */\r
- /* pointers to other triangles, three pointers to corners, and possibly */\r
- /* three pointers to shell edges before the extra nodes. */\r
- highorderindex = 6 + (useshelles * 3);\r
- /* The number of bytes occupied by a triangle. */\r
- trisize = ((order + 1) * (order + 2) / 2 + (highorderindex - 3)) *\r
- sizeof(triangle);\r
- /* The index within each triangle at which its attributes are found, */\r
- /* where the index is measured in REALs. */\r
- elemattribindex = (trisize + sizeof(REAL) - 1) / sizeof(REAL);\r
- /* The index within each triangle at which the maximum area constraint */\r
- /* is found, where the index is measured in REALs. Note that if the */\r
- /* `regionattrib' flag is set, an additional attribute will be added. */\r
- areaboundindex = elemattribindex + eextras + regionattrib;\r
- /* If triangle attributes or an area bound are needed, increase the number */\r
- /* of bytes occupied by a triangle. */\r
- if (vararea) {\r
- trisize = (areaboundindex + 1) * sizeof(REAL);\r
- } else if (eextras + regionattrib > 0) {\r
- trisize = areaboundindex * sizeof(REAL);\r
- }\r
- /* If a Voronoi diagram or triangle neighbor graph is requested, make */\r
- /* sure there's room to store an integer index in each triangle. This */\r
- /* integer index can occupy the same space as the shell edges or */\r
- /* attributes or area constraint or extra nodes. */\r
- if ((voronoi || neighbors) &&\r
- (trisize < 6 * sizeof(triangle) + sizeof(int))) {\r
- trisize = 6 * sizeof(triangle) + sizeof(int);\r
- }\r
- /* Having determined the memory size of a triangle, initialize the pool. */\r
- poolinit(&triangles, trisize, TRIPERBLOCK, POINTER, 4);\r
-\r
- if (useshelles) {\r
- /* Initialize the pool of shell edges. */\r
- poolinit(&shelles, 6 * sizeof(triangle) + sizeof(int), SHELLEPERBLOCK,\r
- POINTER, 4);\r
-\r
- /* Initialize the "outer space" triangle and omnipresent shell edge. */\r
- dummyinit(triangles.itemwords, shelles.itemwords);\r
- } else {\r
- /* Initialize the "outer space" triangle. */\r
- dummyinit(triangles.itemwords, 0);\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* triangledealloc() Deallocate space for a triangle, marking it dead. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void triangledealloc(dyingtriangle)\r
-triangle *dyingtriangle;\r
-{\r
- /* Set triangle's vertices to NULL. This makes it possible to */\r
- /* detect dead triangles when traversing the list of all triangles. */\r
- dyingtriangle[3] = (triangle) NULL;\r
- dyingtriangle[4] = (triangle) NULL;\r
- dyingtriangle[5] = (triangle) NULL;\r
- pooldealloc(&triangles, (VOID *) dyingtriangle);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* triangletraverse() Traverse the triangles, skipping dead ones. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-triangle *triangletraverse()\r
-{\r
- triangle *newtriangle;\r
-\r
- do {\r
- newtriangle = (triangle *) traverse(&triangles);\r
- if (newtriangle == (triangle *) NULL) {\r
- return (triangle *) NULL;\r
- }\r
- } while (newtriangle[3] == (triangle) NULL); /* Skip dead ones. */\r
- return newtriangle;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* shelledealloc() Deallocate space for a shell edge, marking it dead. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void shelledealloc(dyingshelle)\r
-shelle *dyingshelle;\r
-{\r
- /* Set shell edge's vertices to NULL. This makes it possible to */\r
- /* detect dead shells when traversing the list of all shells. */\r
- dyingshelle[2] = (shelle) NULL;\r
- dyingshelle[3] = (shelle) NULL;\r
- pooldealloc(&shelles, (VOID *) dyingshelle);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* shelletraverse() Traverse the shell edges, skipping dead ones. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-shelle *shelletraverse()\r
-{\r
- shelle *newshelle;\r
-\r
- do {\r
- newshelle = (shelle *) traverse(&shelles);\r
- if (newshelle == (shelle *) NULL) {\r
- return (shelle *) NULL;\r
- }\r
- } while (newshelle[2] == (shelle) NULL); /* Skip dead ones. */\r
- return newshelle;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* pointdealloc() Deallocate space for a point, marking it dead. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void pointdealloc(dyingpoint)\r
-point dyingpoint;\r
-{\r
- /* Mark the point as dead. This makes it possible to detect dead points */\r
- /* when traversing the list of all points. */\r
- setpointmark(dyingpoint, DEADPOINT);\r
- pooldealloc(&points, (VOID *) dyingpoint);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* pointtraverse() Traverse the points, skipping dead ones. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-point pointtraverse()\r
-{\r
- point newpoint;\r
-\r
- do {\r
- newpoint = (point) traverse(&points);\r
- if (newpoint == (point) NULL) {\r
- return (point) NULL;\r
- }\r
- } while (pointmark(newpoint) == DEADPOINT); /* Skip dead ones. */\r
- return newpoint;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* badsegmentdealloc() Deallocate space for a bad segment, marking it */\r
-/* dead. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void badsegmentdealloc(dyingseg)\r
-struct edge *dyingseg;\r
-{\r
- /* Set segment's orientation to -1. This makes it possible to */\r
- /* detect dead segments when traversing the list of all segments. */\r
- dyingseg->shorient = -1;\r
- pooldealloc(&badsegments, (VOID *) dyingseg);\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* badsegmenttraverse() Traverse the bad segments, skipping dead ones. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-struct edge *badsegmenttraverse()\r
-{\r
- struct edge *newseg;\r
-\r
- do {\r
- newseg = (struct edge *) traverse(&badsegments);\r
- if (newseg == (struct edge *) NULL) {\r
- return (struct edge *) NULL;\r
- }\r
- } while (newseg->shorient == -1); /* Skip dead ones. */\r
- return newseg;\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* getpoint() Get a specific point, by number, from the list. */\r
-/* */\r
-/* The first point is number 'firstnumber'. */\r
-/* */\r
-/* Note that this takes O(n) time (with a small constant, if POINTPERBLOCK */\r
-/* is large). I don't care to take the trouble to make it work in constant */\r
-/* time. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-point getpoint(number)\r
-int number;\r
-{\r
- VOID **getblock;\r
- point foundpoint;\r
- unsigned long alignptr;\r
- int current;\r
-\r
- getblock = points.firstblock;\r
- current = firstnumber;\r
- /* Find the right block. */\r
- while (current + points.itemsperblock <= number) {\r
- getblock = (VOID **) *getblock;\r
- current += points.itemsperblock;\r
- }\r
- /* Now find the right point. */\r
- alignptr = (unsigned long) (getblock + 1);\r
- foundpoint = (point) (alignptr + (unsigned long) points.alignbytes\r
- - (alignptr % (unsigned long) points.alignbytes));\r
- while (current < number) {\r
- foundpoint += points.itemwords;\r
- current++;\r
- }\r
- return foundpoint;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* triangledeinit() Free all remaining allocated memory. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void triangledeinit()\r
-{\r
- pooldeinit(&triangles);\r
- free(dummytribase);\r
- if (useshelles) {\r
- pooldeinit(&shelles);\r
- free(dummyshbase);\r
- }\r
- pooldeinit(&points);\r
-#ifndef CDT_ONLY\r
- if (quality) {\r
- pooldeinit(&badsegments);\r
- if ((minangle > 0.0) || vararea || fixedarea) {\r
- pooldeinit(&badtriangles);\r
- }\r
- }\r
-#endif /* not CDT_ONLY */\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Memory management routines end here *********/\r
-\r
-/********* Constructors begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* maketriangle() Create a new triangle with orientation zero. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void maketriangle(newtriedge)\r
-struct triedge *newtriedge;\r
-{\r
- int i;\r
-\r
- newtriedge->tri = (triangle *) poolalloc(&triangles);\r
- /* Initialize the three adjoining triangles to be "outer space". */\r
- newtriedge->tri[0] = (triangle) dummytri;\r
- newtriedge->tri[1] = (triangle) dummytri;\r
- newtriedge->tri[2] = (triangle) dummytri;\r
- /* Three NULL vertex points. */\r
- newtriedge->tri[3] = (triangle) NULL;\r
- newtriedge->tri[4] = (triangle) NULL;\r
- newtriedge->tri[5] = (triangle) NULL;\r
- /* Initialize the three adjoining shell edges to be the omnipresent */\r
- /* shell edge. */\r
- if (useshelles) {\r
- newtriedge->tri[6] = (triangle) dummysh;\r
- newtriedge->tri[7] = (triangle) dummysh;\r
- newtriedge->tri[8] = (triangle) dummysh;\r
- }\r
- for (i = 0; i < eextras; i++) {\r
- setelemattribute(*newtriedge, i, 0.0);\r
- }\r
- if (vararea) {\r
- setareabound(*newtriedge, -1.0);\r
- }\r
-\r
- newtriedge->orient = 0;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* makeshelle() Create a new shell edge with orientation zero. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void makeshelle(newedge)\r
-struct edge *newedge;\r
-{\r
- newedge->sh = (shelle *) poolalloc(&shelles);\r
- /* Initialize the two adjoining shell edges to be the omnipresent */\r
- /* shell edge. */\r
- newedge->sh[0] = (shelle) dummysh;\r
- newedge->sh[1] = (shelle) dummysh;\r
- /* Two NULL vertex points. */\r
- newedge->sh[2] = (shelle) NULL;\r
- newedge->sh[3] = (shelle) NULL;\r
- /* Initialize the two adjoining triangles to be "outer space". */\r
- newedge->sh[4] = (shelle) dummytri;\r
- newedge->sh[5] = (shelle) dummytri;\r
- /* Set the boundary marker to zero. */\r
- setmark(*newedge, 0);\r
-\r
- newedge->shorient = 0;\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Constructors end here *********/\r
-\r
-/********* Determinant evaluation routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/* The adaptive exact arithmetic geometric predicates implemented herein are */\r
-/* described in detail in my Technical Report CMU-CS-96-140. The complete */\r
-/* reference is given in the header. */\r
-\r
-/* Which of the following two methods of finding the absolute values is */\r
-/* fastest is compiler-dependent. A few compilers can inline and optimize */\r
-/* the fabs() call; but most will incur the overhead of a function call, */\r
-/* which is disastrously slow. A faster way on IEEE machines might be to */\r
-/* mask the appropriate bit, but that's difficult to do in C. */\r
-\r
-#define Absolute(a) ((a) >= 0.0 ? (a) : -(a))\r
-/* #define Absolute(a) fabs(a) */\r
-\r
-/* Many of the operations are broken up into two pieces, a main part that */\r
-/* performs an approximate operation, and a "tail" that computes the */\r
-/* roundoff error of that operation. */\r
-/* */\r
-/* The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(), */\r
-/* Split(), and Two_Product() are all implemented as described in the */\r
-/* reference. Each of these macros requires certain variables to be */\r
-/* defined in the calling routine. The variables `bvirt', `c', `abig', */\r
-/* `_i', `_j', `_k', `_l', `_m', and `_n' are declared `INEXACT' because */\r
-/* they store the result of an operation that may incur roundoff error. */\r
-/* The input parameter `x' (or the highest numbered `x_' parameter) must */\r
-/* also be declared `INEXACT'. */\r
-\r
-#define Fast_Two_Sum_Tail(a, b, x, y) \\r
- bvirt = x - a; \\r
- y = b - bvirt\r
-\r
-#define Fast_Two_Sum(a, b, x, y) \\r
- x = (REAL) (a + b); \\r
- Fast_Two_Sum_Tail(a, b, x, y)\r
-\r
-#define Two_Sum_Tail(a, b, x, y) \\r
- bvirt = (REAL) (x - a); \\r
- avirt = x - bvirt; \\r
- bround = b - bvirt; \\r
- around = a - avirt; \\r
- y = around + bround\r
-\r
-#define Two_Sum(a, b, x, y) \\r
- x = (REAL) (a + b); \\r
- Two_Sum_Tail(a, b, x, y)\r
-\r
-#define Two_Diff_Tail(a, b, x, y) \\r
- bvirt = (REAL) (a - x); \\r
- avirt = x + bvirt; \\r
- bround = bvirt - b; \\r
- around = a - avirt; \\r
- y = around + bround\r
-\r
-#define Two_Diff(a, b, x, y) \\r
- x = (REAL) (a - b); \\r
- Two_Diff_Tail(a, b, x, y)\r
-\r
-#define Split(a, ahi, alo) \\r
- c = (REAL) (splitter * a); \\r
- abig = (REAL) (c - a); \\r
- ahi = (REAL)(c - abig); \\r
- alo = (REAL)(a - ahi)\r
-\r
-#define Two_Product_Tail(a, b, x, y) \\r
- Split(a, ahi, alo); \\r
- Split(b, bhi, blo); \\r
- err1 = x - (ahi * bhi); \\r
- err2 = err1 - (alo * bhi); \\r
- err3 = err2 - (ahi * blo); \\r
- y = (alo * blo) - err3\r
-\r
-#define Two_Product(a, b, x, y) \\r
- x = (REAL) (a * b); \\r
- Two_Product_Tail(a, b, x, y)\r
-\r
-/* Two_Product_Presplit() is Two_Product() where one of the inputs has */\r
-/* already been split. Avoids redundant splitting. */\r
-\r
-#define Two_Product_Presplit(a, b, bhi, blo, x, y) \\r
- x = (REAL) (a * b); \\r
- Split(a, ahi, alo); \\r
- err1 = x - (ahi * bhi); \\r
- err2 = err1 - (alo * bhi); \\r
- err3 = err2 - (ahi * blo); \\r
- y = (alo * blo) - err3\r
-\r
-/* Square() can be done more quickly than Two_Product(). */\r
-\r
-#define Square_Tail(a, x, y) \\r
- Split(a, ahi, alo); \\r
- err1 = x - (ahi * ahi); \\r
- err3 = err1 - ((ahi + ahi) * alo); \\r
- y = (alo * alo) - err3\r
-\r
-#define Square(a, x, y) \\r
- x = (REAL) (a * a); \\r
- Square_Tail(a, x, y)\r
-\r
-/* Macros for summing expansions of various fixed lengths. These are all */\r
-/* unrolled versions of Expansion_Sum(). */\r
-\r
-#define Two_One_Sum(a1, a0, b, x2, x1, x0) \\r
- Two_Sum(a0, b , _i, x0); \\r
- Two_Sum(a1, _i, x2, x1)\r
-\r
-#define Two_One_Diff(a1, a0, b, x2, x1, x0) \\r
- Two_Diff(a0, b , _i, x0); \\r
- Two_Sum( a1, _i, x2, x1)\r
-\r
-#define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0) \\r
- Two_One_Sum(a1, a0, b0, _j, _0, x0); \\r
- Two_One_Sum(_j, _0, b1, x3, x2, x1)\r
-\r
-#define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0) \\r
- Two_One_Diff(a1, a0, b0, _j, _0, x0); \\r
- Two_One_Diff(_j, _0, b1, x3, x2, x1)\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* exactinit() Initialize the variables used for exact arithmetic. */\r
-/* */\r
-/* `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in */\r
-/* floating-point arithmetic. `epsilon' bounds the relative roundoff */\r
-/* error. It is used for floating-point error analysis. */\r
-/* */\r
-/* `splitter' is used to split floating-point numbers into two half- */\r
-/* length significands for exact multiplication. */\r
-/* */\r
-/* I imagine that a highly optimizing compiler might be too smart for its */\r
-/* own good, and somehow cause this routine to fail, if it pretends that */\r
-/* floating-point arithmetic is too much like real arithmetic. */\r
-/* */\r
-/* Don't change this routine unless you fully understand it. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void exactinit()\r
-{\r
- REAL half;\r
- REAL check, lastcheck;\r
- int every_other;\r
-\r
- every_other = 1;\r
- half = 0.5;\r
- epsilon = 1.0;\r
- splitter = 1.0;\r
- check = 1.0;\r
- /* Repeatedly divide `epsilon' by two until it is too small to add to */\r
- /* one without causing roundoff. (Also check if the sum is equal to */\r
- /* the previous sum, for machines that round up instead of using exact */\r
- /* rounding. Not that these routines will work on such machines anyway. */\r
- do {\r
- lastcheck = check;\r
- epsilon *= half;\r
- if (every_other) {\r
- splitter *= 2.0;\r
- }\r
- every_other = !every_other;\r
- check = (REAL)(1.0 + epsilon);\r
- } while ((check != 1.0) && (check != lastcheck));\r
- splitter += 1.0;\r
- if (verbose > 1) {\r
- printf("Floating point roundoff is of magnitude %.17g\n", epsilon);\r
- printf("Floating point splitter is %.17g\n", splitter);\r
- }\r
- /* Error bounds for orientation and incircle tests. */\r
- resulterrbound = (REAL)((3.0 + 8.0 * epsilon) * epsilon);\r
- ccwerrboundA = (REAL)((3.0 + 16.0 * epsilon) * epsilon);\r
- ccwerrboundB = (REAL)((2.0 + 12.0 * epsilon) * epsilon);\r
- ccwerrboundC = (REAL)((9.0 + 64.0 * epsilon) * epsilon * epsilon);\r
- iccerrboundA = (REAL)((10.0 + 96.0 * epsilon) * epsilon);\r
- iccerrboundB = (REAL)((4.0 + 48.0 * epsilon) * epsilon);\r
- iccerrboundC = (REAL)((44.0 + 576.0 * epsilon) * epsilon * epsilon);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* fast_expansion_sum_zeroelim() Sum two expansions, eliminating zero */\r
-/* components from the output expansion. */\r
-/* */\r
-/* Sets h = e + f. See my Robust Predicates paper for details. */\r
-/* */\r
-/* If round-to-even is used (as with IEEE 754), maintains the strongly */\r
-/* nonoverlapping property. (That is, if e is strongly nonoverlapping, h */\r
-/* will be also.) Does NOT maintain the nonoverlapping or nonadjacent */\r
-/* properties. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-int fast_expansion_sum_zeroelim(elen, e, flen, f, h) /* h cannot be e or f. */\r
-int elen;\r
-REAL *e;\r
-int flen;\r
-REAL *f;\r
-REAL *h;\r
-{\r
- REAL Q;\r
- INEXACT REAL Qnew;\r
- INEXACT REAL hh;\r
- INEXACT REAL bvirt;\r
- REAL avirt, bround, around;\r
- int eindex, findex, hindex;\r
- REAL enow, fnow;\r
-\r
- enow = e[0];\r
- fnow = f[0];\r
- eindex = findex = 0;\r
- if ((fnow > enow) == (fnow > -enow)) {\r
- Q = enow;\r
- enow = e[++eindex];\r
- } else {\r
- Q = fnow;\r
- fnow = f[++findex];\r
- }\r
- hindex = 0;\r
- if ((eindex < elen) && (findex < flen)) {\r
- if ((fnow > enow) == (fnow > -enow)) {\r
- Fast_Two_Sum(enow, Q, Qnew, hh);\r
- enow = e[++eindex];\r
- } else {\r
- Fast_Two_Sum(fnow, Q, Qnew, hh);\r
- fnow = f[++findex];\r
- }\r
- Q = Qnew;\r
- if (hh != 0.0) {\r
- h[hindex++] = hh;\r
- }\r
- while ((eindex < elen) && (findex < flen)) {\r
- if ((fnow > enow) == (fnow > -enow)) {\r
- Two_Sum(Q, enow, Qnew, hh);\r
- enow = e[++eindex];\r
- } else {\r
- Two_Sum(Q, fnow, Qnew, hh);\r
- fnow = f[++findex];\r
- }\r
- Q = Qnew;\r
- if (hh != 0.0) {\r
- h[hindex++] = hh;\r
- }\r
- }\r
- }\r
- while (eindex < elen) {\r
- Two_Sum(Q, enow, Qnew, hh);\r
- enow = e[++eindex];\r
- Q = Qnew;\r
- if (hh != 0.0) {\r
- h[hindex++] = hh;\r
- }\r
- }\r
- while (findex < flen) {\r
- Two_Sum(Q, fnow, Qnew, hh);\r
- fnow = f[++findex];\r
- Q = Qnew;\r
- if (hh != 0.0) {\r
- h[hindex++] = hh;\r
- }\r
- }\r
- if ((Q != 0.0) || (hindex == 0)) {\r
- h[hindex++] = Q;\r
- }\r
- return hindex;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* scale_expansion_zeroelim() Multiply an expansion by a scalar, */\r
-/* eliminating zero components from the */\r
-/* output expansion. */\r
-/* */\r
-/* Sets h = be. See my Robust Predicates paper for details. */\r
-/* */\r
-/* Maintains the nonoverlapping property. If round-to-even is used (as */\r
-/* with IEEE 754), maintains the strongly nonoverlapping and nonadjacent */\r
-/* properties as well. (That is, if e has one of these properties, so */\r
-/* will h.) */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-int scale_expansion_zeroelim(elen, e, b, h) /* e and h cannot be the same. */\r
-int elen;\r
-REAL *e;\r
-REAL b;\r
-REAL *h;\r
-{\r
- INEXACT REAL Q, sum;\r
- REAL hh;\r
- INEXACT REAL product1;\r
- REAL product0;\r
- int eindex, hindex;\r
- REAL enow;\r
- INEXACT REAL bvirt;\r
- REAL avirt, bround, around;\r
- INEXACT REAL c;\r
- INEXACT REAL abig;\r
- REAL ahi, alo, bhi, blo;\r
- REAL err1, err2, err3;\r
-\r
- Split(b, bhi, blo);\r
- Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);\r
- hindex = 0;\r
- if (hh != 0) {\r
- h[hindex++] = hh;\r
- }\r
- for (eindex = 1; eindex < elen; eindex++) {\r
- enow = e[eindex];\r
- Two_Product_Presplit(enow, b, bhi, blo, product1, product0);\r
- Two_Sum(Q, product0, sum, hh);\r
- if (hh != 0) {\r
- h[hindex++] = hh;\r
- }\r
- Fast_Two_Sum(product1, sum, Q, hh);\r
- if (hh != 0) {\r
- h[hindex++] = hh;\r
- }\r
- }\r
- if ((Q != 0.0) || (hindex == 0)) {\r
- h[hindex++] = Q;\r
- }\r
- return hindex;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* estimate() Produce a one-word estimate of an expansion's value. */\r
-/* */\r
-/* See my Robust Predicates paper for details. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-REAL estimate(elen, e)\r
-int elen;\r
-REAL *e;\r
-{\r
- REAL Q;\r
- int eindex;\r
-\r
- Q = e[0];\r
- for (eindex = 1; eindex < elen; eindex++) {\r
- Q += e[eindex];\r
- }\r
- return Q;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* counterclockwise() Return a positive value if the points pa, pb, and */\r
-/* pc occur in counterclockwise order; a negative */\r
-/* value if they occur in clockwise order; and zero */\r
-/* if they are collinear. The result is also a rough */\r
-/* approximation of twice the signed area of the */\r
-/* triangle defined by the three points. */\r
-/* */\r
-/* Uses exact arithmetic if necessary to ensure a correct answer. The */\r
-/* result returned is the determinant of a matrix. This determinant is */\r
-/* computed adaptively, in the sense that exact arithmetic is used only to */\r
-/* the degree it is needed to ensure that the returned value has the */\r
-/* correct sign. Hence, this function is usually quite fast, but will run */\r
-/* more slowly when the input points are collinear or nearly so. */\r
-/* */\r
-/* See my Robust Predicates paper for details. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-REAL counterclockwiseadapt(pa, pb, pc, detsum)\r
-point pa;\r
-point pb;\r
-point pc;\r
-REAL detsum;\r
-{\r
- INEXACT REAL acx, acy, bcx, bcy;\r
- REAL acxtail, acytail, bcxtail, bcytail;\r
- INEXACT REAL detleft, detright;\r
- REAL detlefttail, detrighttail;\r
- REAL det, errbound;\r
- REAL B[4], C1[8], C2[12], D[16];\r
- INEXACT REAL B3;\r
- int C1length, C2length, Dlength;\r
- REAL u[4];\r
- INEXACT REAL u3;\r
- INEXACT REAL s1, t1;\r
- REAL s0, t0;\r
-\r
- INEXACT REAL bvirt;\r
- REAL avirt, bround, around;\r
- INEXACT REAL c;\r
- INEXACT REAL abig;\r
- REAL ahi, alo, bhi, blo;\r
- REAL err1, err2, err3;\r
- INEXACT REAL _i, _j;\r
- REAL _0;\r
-\r
- acx = (REAL) (pa[0] - pc[0]);\r
- bcx = (REAL) (pb[0] - pc[0]);\r
- acy = (REAL) (pa[1] - pc[1]);\r
- bcy = (REAL) (pb[1] - pc[1]);\r
-\r
- Two_Product(acx, bcy, detleft, detlefttail);\r
- Two_Product(acy, bcx, detright, detrighttail);\r
-\r
- Two_Two_Diff(detleft, detlefttail, detright, detrighttail,\r
- B3, B[2], B[1], B[0]);\r
- B[3] = B3;\r
-\r
- det = estimate(4, B);\r
- errbound = (REAL)(ccwerrboundB * detsum);\r
- if ((det >= errbound) || (-det >= errbound)) {\r
- return det;\r
- }\r
-\r
- Two_Diff_Tail(pa[0], pc[0], acx, acxtail);\r
- Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);\r
- Two_Diff_Tail(pa[1], pc[1], acy, acytail);\r
- Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);\r
-\r
- if ((acxtail == 0.0) && (acytail == 0.0)\r
- && (bcxtail == 0.0) && (bcytail == 0.0)) {\r
- return det;\r
- }\r
-\r
- errbound = (REAL)(ccwerrboundC * detsum + resulterrbound * Absolute(det));\r
- det += (acx * bcytail + bcy * acxtail)\r
- - (acy * bcxtail + bcx * acytail);\r
- if ((det >= errbound) || (-det >= errbound)) {\r
- return det;\r
- }\r
-\r
- Two_Product(acxtail, bcy, s1, s0);\r
- Two_Product(acytail, bcx, t1, t0);\r
- Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);\r
- u[3] = u3;\r
- C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);\r
-\r
- Two_Product(acx, bcytail, s1, s0);\r
- Two_Product(acy, bcxtail, t1, t0);\r
- Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);\r
- u[3] = u3;\r
- C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);\r
-\r
- Two_Product(acxtail, bcytail, s1, s0);\r
- Two_Product(acytail, bcxtail, t1, t0);\r
- Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);\r
- u[3] = u3;\r
- Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);\r
-\r
- return(D[Dlength - 1]);\r
-}\r
-\r
-REAL counterclockwise(pa, pb, pc)\r
-point pa;\r
-point pb;\r
-point pc;\r
-{\r
- REAL detleft, detright, det;\r
- REAL detsum, errbound;\r
-\r
- counterclockcount++;\r
-\r
- detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);\r
- detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);\r
- det = detleft - detright;\r
-\r
- if (noexact) {\r
- return det;\r
- }\r
-\r
- if (detleft > 0.0) {\r
- if (detright <= 0.0) {\r
- return det;\r
- } else {\r
- detsum = detleft + detright;\r
- }\r
- } else if (detleft < 0.0) {\r
- if (detright >= 0.0) {\r
- return det;\r
- } else {\r
- detsum = -detleft - detright;\r
- }\r
- } else {\r
- return det;\r
- }\r
-\r
- errbound = ccwerrboundA * detsum;\r
- if ((det >= errbound) || (-det >= errbound)) {\r
- return det;\r
- }\r
-\r
- return counterclockwiseadapt(pa, pb, pc, detsum);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* incircle() Return a positive value if the point pd lies inside the */\r
-/* circle passing through pa, pb, and pc; a negative value if */\r
-/* it lies outside; and zero if the four points are cocircular.*/\r
-/* The points pa, pb, and pc must be in counterclockwise */\r
-/* order, or the sign of the result will be reversed. */\r
-/* */\r
-/* Uses exact arithmetic if necessary to ensure a correct answer. The */\r
-/* result returned is the determinant of a matrix. This determinant is */\r
-/* computed adaptively, in the sense that exact arithmetic is used only to */\r
-/* the degree it is needed to ensure that the returned value has the */\r
-/* correct sign. Hence, this function is usually quite fast, but will run */\r
-/* more slowly when the input points are cocircular or nearly so. */\r
-/* */\r
-/* See my Robust Predicates paper for details. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-REAL incircleadapt(pa, pb, pc, pd, permanent)\r
-point pa;\r
-point pb;\r
-point pc;\r
-point pd;\r
-REAL permanent;\r
-{\r
- INEXACT REAL adx, bdx, cdx, ady, bdy, cdy;\r
- REAL det, errbound;\r
-\r
- INEXACT REAL bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;\r
- REAL bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;\r
- REAL bc[4], ca[4], ab[4];\r
- INEXACT REAL bc3, ca3, ab3;\r
- REAL axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];\r
- int axbclen, axxbclen, aybclen, ayybclen, alen;\r
- REAL bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];\r
- int bxcalen, bxxcalen, bycalen, byycalen, blen;\r
- REAL cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];\r
- int cxablen, cxxablen, cyablen, cyyablen, clen;\r
- REAL abdet[64];\r
- int ablen;\r
- REAL fin1[1152], fin2[1152];\r
- REAL *finnow, *finother, *finswap;\r
- int finlength;\r
-\r
- REAL adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;\r
- INEXACT REAL adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;\r
- REAL adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;\r
- REAL aa[4], bb[4], cc[4];\r
- INEXACT REAL aa3, bb3, cc3;\r
- INEXACT REAL ti1, tj1;\r
- REAL ti0, tj0;\r
- REAL u[4], v[4];\r
- INEXACT REAL u3, v3;\r
- REAL temp8[8], temp16a[16], temp16b[16], temp16c[16];\r
- REAL temp32a[32], temp32b[32], temp48[48], temp64[64];\r
- int temp8len, temp16alen, temp16blen, temp16clen;\r
- int temp32alen, temp32blen, temp48len, temp64len;\r
- REAL axtbb[8], axtcc[8], aytbb[8], aytcc[8];\r
- int axtbblen, axtcclen, aytbblen, aytcclen;\r
- REAL bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];\r
- int bxtaalen, bxtcclen, bytaalen, bytcclen;\r
- REAL cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];\r
- int cxtaalen, cxtbblen, cytaalen, cytbblen;\r
- REAL axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];\r
- int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;\r
- REAL axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16], cytabt[16];\r
- int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;\r
- REAL axtbctt[8], aytbctt[8], bxtcatt[8];\r
- REAL bytcatt[8], cxtabtt[8], cytabtt[8];\r
- int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;\r
- REAL abt[8], bct[8], cat[8];\r
- int abtlen, bctlen, catlen;\r
- REAL abtt[4], bctt[4], catt[4];\r
- int abttlen, bcttlen, cattlen;\r
- INEXACT REAL abtt3, bctt3, catt3;\r
- REAL negate;\r
-\r
- INEXACT REAL bvirt;\r
- REAL avirt, bround, around;\r
- INEXACT REAL c;\r
- INEXACT REAL abig;\r
- REAL ahi, alo, bhi, blo;\r
- REAL err1, err2, err3;\r
- INEXACT REAL _i, _j;\r
- REAL _0;\r
-\r
- adx = (REAL) (pa[0] - pd[0]);\r
- bdx = (REAL) (pb[0] - pd[0]);\r
- cdx = (REAL) (pc[0] - pd[0]);\r
- ady = (REAL) (pa[1] - pd[1]);\r
- bdy = (REAL) (pb[1] - pd[1]);\r
- cdy = (REAL) (pc[1] - pd[1]);\r
-\r
- Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);\r
- Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);\r
- Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);\r
- bc[3] = bc3;\r
- axbclen = scale_expansion_zeroelim(4, bc, adx, axbc);\r
- axxbclen = scale_expansion_zeroelim(axbclen, axbc, adx, axxbc);\r
- aybclen = scale_expansion_zeroelim(4, bc, ady, aybc);\r
- ayybclen = scale_expansion_zeroelim(aybclen, aybc, ady, ayybc);\r
- alen = fast_expansion_sum_zeroelim(axxbclen, axxbc, ayybclen, ayybc, adet);\r
-\r
- Two_Product(cdx, ady, cdxady1, cdxady0);\r
- Two_Product(adx, cdy, adxcdy1, adxcdy0);\r
- Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);\r
- ca[3] = ca3;\r
- bxcalen = scale_expansion_zeroelim(4, ca, bdx, bxca);\r
- bxxcalen = scale_expansion_zeroelim(bxcalen, bxca, bdx, bxxca);\r
- bycalen = scale_expansion_zeroelim(4, ca, bdy, byca);\r
- byycalen = scale_expansion_zeroelim(bycalen, byca, bdy, byyca);\r
- blen = fast_expansion_sum_zeroelim(bxxcalen, bxxca, byycalen, byyca, bdet);\r
-\r
- Two_Product(adx, bdy, adxbdy1, adxbdy0);\r
- Two_Product(bdx, ady, bdxady1, bdxady0);\r
- Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);\r
- ab[3] = ab3;\r
- cxablen = scale_expansion_zeroelim(4, ab, cdx, cxab);\r
- cxxablen = scale_expansion_zeroelim(cxablen, cxab, cdx, cxxab);\r
- cyablen = scale_expansion_zeroelim(4, ab, cdy, cyab);\r
- cyyablen = scale_expansion_zeroelim(cyablen, cyab, cdy, cyyab);\r
- clen = fast_expansion_sum_zeroelim(cxxablen, cxxab, cyyablen, cyyab, cdet);\r
-\r
- ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);\r
- finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);\r
-\r
- det = estimate(finlength, fin1);\r
- errbound = (REAL)(iccerrboundB * permanent);\r
- if ((det >= errbound) || (-det >= errbound)) {\r
- return det;\r
- }\r
-\r
- Two_Diff_Tail(pa[0], pd[0], adx, adxtail);\r
- Two_Diff_Tail(pa[1], pd[1], ady, adytail);\r
- Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);\r
- Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);\r
- Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);\r
- Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);\r
- if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0)\r
- && (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0)) {\r
- return det;\r
- }\r
-\r
- errbound = (REAL)(iccerrboundC * permanent + resulterrbound * Absolute(det));\r
- det += (REAL)(((adx * adx + ady * ady) * ((bdx * cdytail + cdy * bdxtail)\r
- - (bdy * cdxtail + cdx * bdytail))\r
- + 2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx))\r
- + ((bdx * bdx + bdy * bdy) * ((cdx * adytail + ady * cdxtail)\r
- - (cdy * adxtail + adx * cdytail))\r
- + 2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx))\r
- + ((cdx * cdx + cdy * cdy) * ((adx * bdytail + bdy * adxtail)\r
- - (ady * bdxtail + bdx * adytail))\r
- + 2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx)));\r
- if ((det >= errbound) || (-det >= errbound)) {\r
- return det;\r
- }\r
-\r
- finnow = fin1;\r
- finother = fin2;\r
-\r
- if ((bdxtail != 0.0) || (bdytail != 0.0)\r
- || (cdxtail != 0.0) || (cdytail != 0.0)) {\r
- Square(adx, adxadx1, adxadx0);\r
- Square(ady, adyady1, adyady0);\r
- Two_Two_Sum(adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);\r
- aa[3] = aa3;\r
- }\r
- if ((cdxtail != 0.0) || (cdytail != 0.0)\r
- || (adxtail != 0.0) || (adytail != 0.0)) {\r
- Square(bdx, bdxbdx1, bdxbdx0);\r
- Square(bdy, bdybdy1, bdybdy0);\r
- Two_Two_Sum(bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);\r
- bb[3] = bb3;\r
- }\r
- if ((adxtail != 0.0) || (adytail != 0.0)\r
- || (bdxtail != 0.0) || (bdytail != 0.0)) {\r
- Square(cdx, cdxcdx1, cdxcdx0);\r
- Square(cdy, cdycdy1, cdycdy0);\r
- Two_Two_Sum(cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);\r
- cc[3] = cc3;\r
- }\r
-\r
- if (adxtail != 0.0) {\r
- axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);\r
- temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx,\r
- temp16a);\r
-\r
- axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);\r
- temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);\r
-\r
- axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);\r
- temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);\r
-\r
- temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (adytail != 0.0) {\r
- aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);\r
- temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady,\r
- temp16a);\r
-\r
- aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);\r
- temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);\r
-\r
- aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);\r
- temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);\r
-\r
- temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (bdxtail != 0.0) {\r
- bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);\r
- temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx,\r
- temp16a);\r
-\r
- bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);\r
- temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);\r
-\r
- bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);\r
- temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);\r
-\r
- temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (bdytail != 0.0) {\r
- bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);\r
- temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy,\r
- temp16a);\r
-\r
- bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);\r
- temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);\r
-\r
- bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);\r
- temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);\r
-\r
- temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (cdxtail != 0.0) {\r
- cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);\r
- temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx,\r
- temp16a);\r
-\r
- cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);\r
- temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);\r
-\r
- cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);\r
- temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);\r
-\r
- temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (cdytail != 0.0) {\r
- cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);\r
- temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy,\r
- temp16a);\r
-\r
- cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);\r
- temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);\r
-\r
- cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);\r
- temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);\r
-\r
- temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
-\r
- if ((adxtail != 0.0) || (adytail != 0.0)) {\r
- if ((bdxtail != 0.0) || (bdytail != 0.0)\r
- || (cdxtail != 0.0) || (cdytail != 0.0)) {\r
- Two_Product(bdxtail, cdy, ti1, ti0);\r
- Two_Product(bdx, cdytail, tj1, tj0);\r
- Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);\r
- u[3] = u3;\r
- negate = -bdy;\r
- Two_Product(cdxtail, negate, ti1, ti0);\r
- negate = -bdytail;\r
- Two_Product(cdx, negate, tj1, tj0);\r
- Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);\r
- v[3] = v3;\r
- bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);\r
-\r
- Two_Product(bdxtail, cdytail, ti1, ti0);\r
- Two_Product(cdxtail, bdytail, tj1, tj0);\r
- Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);\r
- bctt[3] = bctt3;\r
- bcttlen = 4;\r
- } else {\r
- bct[0] = 0.0;\r
- bctlen = 1;\r
- bctt[0] = 0.0;\r
- bcttlen = 1;\r
- }\r
-\r
- if (adxtail != 0.0) {\r
- temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, adxtail, temp16a);\r
- axtbctlen = scale_expansion_zeroelim(bctlen, bct, adxtail, axtbct);\r
- temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, 2.0 * adx,\r
- temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- if (bdytail != 0.0) {\r
- temp8len = scale_expansion_zeroelim(4, cc, adxtail, temp8);\r
- temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,\r
- temp16a);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,\r
- temp16a, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (cdytail != 0.0) {\r
- temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);\r
- temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,\r
- temp16a);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,\r
- temp16a, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
-\r
- temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail,\r
- temp32a);\r
- axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);\r
- temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx,\r
- temp16a);\r
- temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail,\r
- temp16b);\r
- temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32b);\r
- temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,\r
- temp32blen, temp32b, temp64);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,\r
- temp64, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (adytail != 0.0) {\r
- temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, adytail, temp16a);\r
- aytbctlen = scale_expansion_zeroelim(bctlen, bct, adytail, aytbct);\r
- temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, 2.0 * ady,\r
- temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
-\r
-\r
- temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail,\r
- temp32a);\r
- aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);\r
- temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady,\r
- temp16a);\r
- temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail,\r
- temp16b);\r
- temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32b);\r
- temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,\r
- temp32blen, temp32b, temp64);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,\r
- temp64, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- }\r
- if ((bdxtail != 0.0) || (bdytail != 0.0)) {\r
- if ((cdxtail != 0.0) || (cdytail != 0.0)\r
- || (adxtail != 0.0) || (adytail != 0.0)) {\r
- Two_Product(cdxtail, ady, ti1, ti0);\r
- Two_Product(cdx, adytail, tj1, tj0);\r
- Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);\r
- u[3] = u3;\r
- negate = -cdy;\r
- Two_Product(adxtail, negate, ti1, ti0);\r
- negate = -cdytail;\r
- Two_Product(adx, negate, tj1, tj0);\r
- Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);\r
- v[3] = v3;\r
- catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);\r
-\r
- Two_Product(cdxtail, adytail, ti1, ti0);\r
- Two_Product(adxtail, cdytail, tj1, tj0);\r
- Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);\r
- catt[3] = catt3;\r
- cattlen = 4;\r
- } else {\r
- cat[0] = 0.0;\r
- catlen = 1;\r
- catt[0] = 0.0;\r
- cattlen = 1;\r
- }\r
-\r
- if (bdxtail != 0.0) {\r
- temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, bdxtail, temp16a);\r
- bxtcatlen = scale_expansion_zeroelim(catlen, cat, bdxtail, bxtcat);\r
- temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, 2.0 * bdx,\r
- temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- if (cdytail != 0.0) {\r
- temp8len = scale_expansion_zeroelim(4, aa, bdxtail, temp8);\r
- temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,\r
- temp16a);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,\r
- temp16a, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (adytail != 0.0) {\r
- temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);\r
- temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,\r
- temp16a);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,\r
- temp16a, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
-\r
- temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail,\r
- temp32a);\r
- bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);\r
- temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx,\r
- temp16a);\r
- temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail,\r
- temp16b);\r
- temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32b);\r
- temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,\r
- temp32blen, temp32b, temp64);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,\r
- temp64, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (bdytail != 0.0) {\r
- temp16alen = scale_expansion_zeroelim(bytcalen, bytca, bdytail, temp16a);\r
- bytcatlen = scale_expansion_zeroelim(catlen, cat, bdytail, bytcat);\r
- temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, 2.0 * bdy,\r
- temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
-\r
-\r
- temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail,\r
- temp32a);\r
- bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);\r
- temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy,\r
- temp16a);\r
- temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail,\r
- temp16b);\r
- temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32b);\r
- temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,\r
- temp32blen, temp32b, temp64);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,\r
- temp64, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- }\r
- if ((cdxtail != 0.0) || (cdytail != 0.0)) {\r
- if ((adxtail != 0.0) || (adytail != 0.0)\r
- || (bdxtail != 0.0) || (bdytail != 0.0)) {\r
- Two_Product(adxtail, bdy, ti1, ti0);\r
- Two_Product(adx, bdytail, tj1, tj0);\r
- Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);\r
- u[3] = u3;\r
- negate = -ady;\r
- Two_Product(bdxtail, negate, ti1, ti0);\r
- negate = -adytail;\r
- Two_Product(bdx, negate, tj1, tj0);\r
- Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);\r
- v[3] = v3;\r
- abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);\r
-\r
- Two_Product(adxtail, bdytail, ti1, ti0);\r
- Two_Product(bdxtail, adytail, tj1, tj0);\r
- Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);\r
- abtt[3] = abtt3;\r
- abttlen = 4;\r
- } else {\r
- abt[0] = 0.0;\r
- abtlen = 1;\r
- abtt[0] = 0.0;\r
- abttlen = 1;\r
- }\r
-\r
- if (cdxtail != 0.0) {\r
- temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, cdxtail, temp16a);\r
- cxtabtlen = scale_expansion_zeroelim(abtlen, abt, cdxtail, cxtabt);\r
- temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, 2.0 * cdx,\r
- temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- if (adytail != 0.0) {\r
- temp8len = scale_expansion_zeroelim(4, bb, cdxtail, temp8);\r
- temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,\r
- temp16a);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,\r
- temp16a, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (bdytail != 0.0) {\r
- temp8len = scale_expansion_zeroelim(4, aa, -cdxtail, temp8);\r
- temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,\r
- temp16a);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,\r
- temp16a, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
-\r
- temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, cdxtail,\r
- temp32a);\r
- cxtabttlen = scale_expansion_zeroelim(abttlen, abtt, cdxtail, cxtabtt);\r
- temp16alen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, 2.0 * cdx,\r
- temp16a);\r
- temp16blen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, cdxtail,\r
- temp16b);\r
- temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32b);\r
- temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,\r
- temp32blen, temp32b, temp64);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,\r
- temp64, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- if (cdytail != 0.0) {\r
- temp16alen = scale_expansion_zeroelim(cytablen, cytab, cdytail, temp16a);\r
- cytabtlen = scale_expansion_zeroelim(abtlen, abt, cdytail, cytabt);\r
- temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, 2.0 * cdy,\r
- temp32a);\r
- temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp32alen, temp32a, temp48);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,\r
- temp48, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
-\r
-\r
- temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, cdytail,\r
- temp32a);\r
- cytabttlen = scale_expansion_zeroelim(abttlen, abtt, cdytail, cytabtt);\r
- temp16alen = scale_expansion_zeroelim(cytabttlen, cytabtt, 2.0 * cdy,\r
- temp16a);\r
- temp16blen = scale_expansion_zeroelim(cytabttlen, cytabtt, cdytail,\r
- temp16b);\r
- temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,\r
- temp16blen, temp16b, temp32b);\r
- temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,\r
- temp32blen, temp32b, temp64);\r
- finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,\r
- temp64, finother);\r
- finswap = finnow; finnow = finother; finother = finswap;\r
- }\r
- }\r
-\r
- return finnow[finlength - 1];\r
-}\r
-\r
-REAL incircle(pa, pb, pc, pd)\r
-point pa;\r
-point pb;\r
-point pc;\r
-point pd;\r
-{\r
- REAL adx, bdx, cdx, ady, bdy, cdy;\r
- REAL bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;\r
- REAL alift, blift, clift;\r
- REAL det;\r
- REAL permanent, errbound;\r
-\r
- incirclecount++;\r
-\r
- adx = pa[0] - pd[0];\r
- bdx = pb[0] - pd[0];\r
- cdx = pc[0] - pd[0];\r
- ady = pa[1] - pd[1];\r
- bdy = pb[1] - pd[1];\r
- cdy = pc[1] - pd[1];\r
-\r
- bdxcdy = bdx * cdy;\r
- cdxbdy = cdx * bdy;\r
- alift = adx * adx + ady * ady;\r
-\r
- cdxady = cdx * ady;\r
- adxcdy = adx * cdy;\r
- blift = bdx * bdx + bdy * bdy;\r
-\r
- adxbdy = adx * bdy;\r
- bdxady = bdx * ady;\r
- clift = cdx * cdx + cdy * cdy;\r
-\r
- det = alift * (bdxcdy - cdxbdy)\r
- + blift * (cdxady - adxcdy)\r
- + clift * (adxbdy - bdxady);\r
-\r
- if (noexact) {\r
- return det;\r
- }\r
-\r
- permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * alift\r
- + (Absolute(cdxady) + Absolute(adxcdy)) * blift\r
- + (Absolute(adxbdy) + Absolute(bdxady)) * clift;\r
- errbound = iccerrboundA * permanent;\r
- if ((det > errbound) || (-det > errbound)) {\r
- return det;\r
- }\r
-\r
- return incircleadapt(pa, pb, pc, pd, permanent);\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Determinant evaluation routines end here *********/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* triangleinit() Initialize some variables. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void triangleinit()\r
-{\r
- points.maxitems = triangles.maxitems = shelles.maxitems = viri.maxitems =\r
- badsegments.maxitems = badtriangles.maxitems = splaynodes.maxitems = 0l;\r
- points.itembytes = triangles.itembytes = shelles.itembytes = viri.itembytes =\r
- badsegments.itembytes = badtriangles.itembytes = splaynodes.itembytes = 0;\r
- recenttri.tri = (triangle *) NULL; /* No triangle has been visited yet. */\r
- samples = 1; /* Point location should take at least one sample. */\r
- checksegments = 0; /* There are no segments in the triangulation yet. */\r
- incirclecount = counterclockcount = hyperbolacount = 0;\r
- circumcentercount = circletopcount = 0;\r
- randomseed = 1;\r
-\r
- exactinit(); /* Initialize exact arithmetic constants. */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* randomnation() Generate a random number between 0 and `choices' - 1. */\r
-/* */\r
-/* This is a simple linear congruential random number generator. Hence, it */\r
-/* is a bad random number generator, but good enough for most randomized */\r
-/* geometric algorithms. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-unsigned long randomnation(choices)\r
-unsigned int choices;\r
-{\r
- randomseed = (randomseed * 1366l + 150889l) % 714025l;\r
- return randomseed / (714025l / choices + 1);\r
-}\r
-\r
-/********* Mesh quality testing routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* checkmesh() Test the mesh for topological consistency. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef REDUCED\r
-\r
-void checkmesh()\r
-{\r
- struct triedge triangleloop;\r
- struct triedge oppotri, oppooppotri;\r
- point triorg, tridest, triapex;\r
- point oppoorg, oppodest;\r
- int horrors;\r
- int saveexact;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- /* Temporarily turn on exact arithmetic if it's off. */\r
- saveexact = noexact;\r
- noexact = 0;\r
- if (!quiet) {\r
- printf(" Checking consistency of mesh...\n");\r
- }\r
- horrors = 0;\r
- /* Run through the list of triangles, checking each one. */\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- /* Check all three edges of the triangle. */\r
- for (triangleloop.orient = 0; triangleloop.orient < 3;\r
- triangleloop.orient++) {\r
- org(triangleloop, triorg);\r
- dest(triangleloop, tridest);\r
- if (triangleloop.orient == 0) { /* Only test for inversion once. */\r
- /* Test if the triangle is flat or inverted. */\r
- apex(triangleloop, triapex);\r
- if (counterclockwise(triorg, tridest, triapex) <= 0.0) {\r
- printf(" !! !! Inverted ");\r
- printtriangle(&triangleloop);\r
- horrors++;\r
- }\r
- }\r
- /* Find the neighboring triangle on this edge. */\r
- sym(triangleloop, oppotri);\r
- if (oppotri.tri != dummytri) {\r
- /* Check that the triangle's neighbor knows it's a neighbor. */\r
- sym(oppotri, oppooppotri);\r
- if ((triangleloop.tri != oppooppotri.tri)\r
- || (triangleloop.orient != oppooppotri.orient)) {\r
- printf(" !! !! Asymmetric triangle-triangle bond:\n");\r
- if (triangleloop.tri == oppooppotri.tri) {\r
- printf(" (Right triangle, wrong orientation)\n");\r
- }\r
- printf(" First ");\r
- printtriangle(&triangleloop);\r
- printf(" Second (nonreciprocating) ");\r
- printtriangle(&oppotri);\r
- horrors++;\r
- }\r
- /* Check that both triangles agree on the identities */\r
- /* of their shared vertices. */\r
- org(oppotri, oppoorg);\r
- dest(oppotri, oppodest);\r
- if ((triorg != oppodest) || (tridest != oppoorg)) {\r
- printf(" !! !! Mismatched edge coordinates between two triangles:\n"\r
- );\r
- printf(" First mismatched ");\r
- printtriangle(&triangleloop);\r
- printf(" Second mismatched ");\r
- printtriangle(&oppotri);\r
- horrors++;\r
- }\r
- }\r
- }\r
- triangleloop.tri = triangletraverse();\r
- }\r
- if (horrors == 0) {\r
- if (!quiet) {\r
- printf(" In my studied opinion, the mesh appears to be consistent.\n");\r
- }\r
- } else if (horrors == 1) {\r
- printf(" !! !! !! !! Precisely one festering wound discovered.\n");\r
- } else {\r
- printf(" !! !! !! !! %d abominations witnessed.\n", horrors);\r
- }\r
- /* Restore the status of exact arithmetic. */\r
- noexact = saveexact;\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* checkdelaunay() Ensure that the mesh is (constrained) Delaunay. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef REDUCED\r
-\r
-void checkdelaunay()\r
-{\r
- struct triedge triangleloop;\r
- struct triedge oppotri;\r
- struct edge opposhelle;\r
- point triorg, tridest, triapex;\r
- point oppoapex;\r
- int shouldbedelaunay;\r
- int horrors;\r
- int saveexact;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- /* Temporarily turn on exact arithmetic if it's off. */\r
- saveexact = noexact;\r
- noexact = 0;\r
- if (!quiet) {\r
- printf(" Checking Delaunay property of mesh...\n");\r
- }\r
- horrors = 0;\r
- /* Run through the list of triangles, checking each one. */\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- /* Check all three edges of the triangle. */\r
- for (triangleloop.orient = 0; triangleloop.orient < 3;\r
- triangleloop.orient++) {\r
- org(triangleloop, triorg);\r
- dest(triangleloop, tridest);\r
- apex(triangleloop, triapex);\r
- sym(triangleloop, oppotri);\r
- apex(oppotri, oppoapex);\r
- /* Only test that the edge is locally Delaunay if there is an */\r
- /* adjoining triangle whose pointer is larger (to ensure that */\r
- /* each pair isn't tested twice). */\r
- shouldbedelaunay = (oppotri.tri != dummytri)\r
- && (triapex != (point) NULL) && (oppoapex != (point) NULL)\r
- && (triangleloop.tri < oppotri.tri);\r
- if (checksegments && shouldbedelaunay) {\r
- /* If a shell edge separates the triangles, then the edge is */\r
- /* constrained, so no local Delaunay test should be done. */\r
- tspivot(triangleloop, opposhelle);\r
- if (opposhelle.sh != dummysh){\r
- shouldbedelaunay = 0;\r
- }\r
- }\r
- if (shouldbedelaunay) {\r
- if (incircle(triorg, tridest, triapex, oppoapex) > 0.0) {\r
- printf(" !! !! Non-Delaunay pair of triangles:\n");\r
- printf(" First non-Delaunay ");\r
- printtriangle(&triangleloop);\r
- printf(" Second non-Delaunay ");\r
- printtriangle(&oppotri);\r
- horrors++;\r
- }\r
- }\r
- }\r
- triangleloop.tri = triangletraverse();\r
- }\r
- if (horrors == 0) {\r
- if (!quiet) {\r
- printf(\r
- " By virtue of my perceptive intelligence, I declare the mesh Delaunay.\n");\r
- }\r
- } else if (horrors == 1) {\r
- printf(\r
- " !! !! !! !! Precisely one terrifying transgression identified.\n");\r
- } else {\r
- printf(" !! !! !! !! %d obscenities viewed with horror.\n", horrors);\r
- }\r
- /* Restore the status of exact arithmetic. */\r
- noexact = saveexact;\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* enqueuebadtri() Add a bad triangle to the end of a queue. */\r
-/* */\r
-/* The queue is actually a set of 64 queues. I use multiple queues to give */\r
-/* priority to smaller angles. I originally implemented a heap, but the */\r
-/* queues are (to my surprise) much faster. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void enqueuebadtri(instri, angle, insapex, insorg, insdest)\r
-struct triedge *instri;\r
-REAL angle;\r
-point insapex;\r
-point insorg;\r
-point insdest;\r
-{\r
- struct badface *newface;\r
- int queuenumber;\r
-\r
- if (verbose > 2) {\r
- printf(" Queueing bad triangle:\n");\r
- printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", insorg[0],\r
- insorg[1], insdest[0], insdest[1], insapex[0], insapex[1]);\r
- }\r
- /* Allocate space for the bad triangle. */\r
- newface = (struct badface *) poolalloc(&badtriangles);\r
- triedgecopy(*instri, newface->badfacetri);\r
- newface->key = angle;\r
- newface->faceapex = insapex;\r
- newface->faceorg = insorg;\r
- newface->facedest = insdest;\r
- newface->nextface = (struct badface *) NULL;\r
- /* Determine the appropriate queue to put the bad triangle into. */\r
- if (angle > 0.6) {\r
- queuenumber = (int) (160.0 * (angle - 0.6));\r
- if (queuenumber > 63) {\r
- queuenumber = 63;\r
- }\r
- } else {\r
- /* It's not a bad angle; put the triangle in the lowest-priority queue. */\r
- queuenumber = 0;\r
- }\r
- /* Add the triangle to the end of a queue. */\r
- *queuetail[queuenumber] = newface;\r
- /* Maintain a pointer to the NULL pointer at the end of the queue. */\r
- queuetail[queuenumber] = &newface->nextface;\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* dequeuebadtri() Remove a triangle from the front of the queue. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-struct badface *dequeuebadtri()\r
-{\r
- struct badface *result;\r
- int queuenumber;\r
-\r
- /* Look for a nonempty queue. */\r
- for (queuenumber = 63; queuenumber >= 0; queuenumber--) {\r
- result = queuefront[queuenumber];\r
- if (result != (struct badface *) NULL) {\r
- /* Remove the triangle from the queue. */\r
- queuefront[queuenumber] = result->nextface;\r
- /* Maintain a pointer to the NULL pointer at the end of the queue. */\r
- if (queuefront[queuenumber] == (struct badface *) NULL) {\r
- queuetail[queuenumber] = &queuefront[queuenumber];\r
- }\r
- return result;\r
- }\r
- }\r
- return (struct badface *) NULL;\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* checkedge4encroach() Check a segment to see if it is encroached; add */\r
-/* it to the list if it is. */\r
-/* */\r
-/* An encroached segment is an unflippable edge that has a point in its */\r
-/* diametral circle (that is, it faces an angle greater than 90 degrees). */\r
-/* This definition is due to Ruppert. */\r
-/* */\r
-/* Returns a nonzero value if the edge is encroached. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-int checkedge4encroach(testedge)\r
-struct edge *testedge;\r
-{\r
- struct triedge neighbortri;\r
- struct edge testsym;\r
- struct edge *badedge;\r
- int addtolist;\r
- int sides;\r
- point eorg, edest, eapex;\r
- triangle ptr; /* Temporary variable used by stpivot(). */\r
-\r
- addtolist = 0;\r
- sides = 0;\r
-\r
- sorg(*testedge, eorg);\r
- sdest(*testedge, edest);\r
- /* Check one neighbor of the shell edge. */\r
- stpivot(*testedge, neighbortri);\r
- /* Does the neighbor exist, or is this a boundary edge? */\r
- if (neighbortri.tri != dummytri) {\r
- sides++;\r
- /* Find a vertex opposite this edge. */\r
- apex(neighbortri, eapex);\r
- /* Check whether the vertex is inside the diametral circle of the */\r
- /* shell edge. Pythagoras' Theorem is used to check whether the */\r
- /* angle at the vertex is greater than 90 degrees. */\r
- if (eapex[0] * (eorg[0] + edest[0]) + eapex[1] * (eorg[1] + edest[1]) >\r
- eapex[0] * eapex[0] + eorg[0] * edest[0] +\r
- eapex[1] * eapex[1] + eorg[1] * edest[1]) {\r
- addtolist = 1;\r
- }\r
- }\r
- /* Check the other neighbor of the shell edge. */\r
- ssym(*testedge, testsym);\r
- stpivot(testsym, neighbortri);\r
- /* Does the neighbor exist, or is this a boundary edge? */\r
- if (neighbortri.tri != dummytri) {\r
- sides++;\r
- /* Find the other vertex opposite this edge. */\r
- apex(neighbortri, eapex);\r
- /* Check whether the vertex is inside the diametral circle of the */\r
- /* shell edge. Pythagoras' Theorem is used to check whether the */\r
- /* angle at the vertex is greater than 90 degrees. */\r
- if (eapex[0] * (eorg[0] + edest[0]) +\r
- eapex[1] * (eorg[1] + edest[1]) >\r
- eapex[0] * eapex[0] + eorg[0] * edest[0] +\r
- eapex[1] * eapex[1] + eorg[1] * edest[1]) {\r
- addtolist += 2;\r
- }\r
- }\r
-\r
- if (addtolist && (!nobisect || ((nobisect == 1) && (sides == 2)))) {\r
- if (verbose > 2) {\r
- printf(" Queueing encroached segment (%.12g, %.12g) (%.12g, %.12g).\n",\r
- eorg[0], eorg[1], edest[0], edest[1]);\r
- }\r
- /* Add the shell edge to the list of encroached segments. */\r
- /* Be sure to get the orientation right. */\r
- badedge = (struct edge *) poolalloc(&badsegments);\r
- if (addtolist == 1) {\r
- shellecopy(*testedge, *badedge);\r
- } else {\r
- shellecopy(testsym, *badedge);\r
- }\r
- }\r
- return addtolist;\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* testtriangle() Test a face for quality measures. */\r
-/* */\r
-/* Tests a triangle to see if it satisfies the minimum angle condition and */\r
-/* the maximum area condition. Triangles that aren't up to spec are added */\r
-/* to the bad triangle queue. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void testtriangle(testtri)\r
-struct triedge *testtri;\r
-{\r
- struct triedge sametesttri;\r
- struct edge edge1, edge2;\r
- point torg, tdest, tapex;\r
- point anglevertex;\r
- REAL dxod, dyod, dxda, dyda, dxao, dyao;\r
- REAL dxod2, dyod2, dxda2, dyda2, dxao2, dyao2;\r
- REAL apexlen, orglen, destlen;\r
- REAL angle;\r
- REAL area;\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- org(*testtri, torg);\r
- dest(*testtri, tdest);\r
- apex(*testtri, tapex);\r
- dxod = torg[0] - tdest[0];\r
- dyod = torg[1] - tdest[1];\r
- dxda = tdest[0] - tapex[0];\r
- dyda = tdest[1] - tapex[1];\r
- dxao = tapex[0] - torg[0];\r
- dyao = tapex[1] - torg[1];\r
- dxod2 = dxod * dxod;\r
- dyod2 = dyod * dyod;\r
- dxda2 = dxda * dxda;\r
- dyda2 = dyda * dyda;\r
- dxao2 = dxao * dxao;\r
- dyao2 = dyao * dyao;\r
- /* Find the lengths of the triangle's three edges. */\r
- apexlen = dxod2 + dyod2;\r
- orglen = dxda2 + dyda2;\r
- destlen = dxao2 + dyao2;\r
- if ((apexlen < orglen) && (apexlen < destlen)) {\r
- /* The edge opposite the apex is shortest. */\r
- /* Find the square of the cosine of the angle at the apex. */\r
- angle = dxda * dxao + dyda * dyao;\r
- angle = angle * angle / (orglen * destlen);\r
- anglevertex = tapex;\r
- lnext(*testtri, sametesttri);\r
- tspivot(sametesttri, edge1);\r
- lnextself(sametesttri);\r
- tspivot(sametesttri, edge2);\r
- } else if (orglen < destlen) {\r
- /* The edge opposite the origin is shortest. */\r
- /* Find the square of the cosine of the angle at the origin. */\r
- angle = dxod * dxao + dyod * dyao;\r
- angle = angle * angle / (apexlen * destlen);\r
- anglevertex = torg;\r
- tspivot(*testtri, edge1);\r
- lprev(*testtri, sametesttri);\r
- tspivot(sametesttri, edge2);\r
- } else {\r
- /* The edge opposite the destination is shortest. */\r
- /* Find the square of the cosine of the angle at the destination. */\r
- angle = dxod * dxda + dyod * dyda;\r
- angle = angle * angle / (apexlen * orglen);\r
- anglevertex = tdest;\r
- tspivot(*testtri, edge1);\r
- lnext(*testtri, sametesttri);\r
- tspivot(sametesttri, edge2);\r
- }\r
- /* Check if both edges that form the angle are segments. */\r
- if ((edge1.sh != dummysh) && (edge2.sh != dummysh)) {\r
- /* The angle is a segment intersection. */\r
- if ((angle > 0.9924) && !quiet) { /* Roughly 5 degrees. */\r
- if (angle > 1.0) {\r
- /* Beware of a floating exception in acos(). */\r
- angle = 1.0;\r
- }\r
- /* Find the actual angle in degrees, for printing. */\r
- angle = acos(sqrt(angle)) * (180.0 / PI);\r
- printf(\r
- "Warning: Small angle (%.4g degrees) between segments at point\n",\r
- angle);\r
- printf(" (%.12g, %.12g)\n", anglevertex[0], anglevertex[1]);\r
- }\r
- /* Don't add this bad triangle to the list; there's nothing that */\r
- /* can be done about a small angle between two segments. */\r
- angle = 0.0;\r
- }\r
- /* Check whether the angle is smaller than permitted. */\r
- if (angle > goodangle) {\r
- /* Add this triangle to the list of bad triangles. */\r
- enqueuebadtri(testtri, angle, tapex, torg, tdest);\r
- return;\r
- }\r
- if (vararea || fixedarea) {\r
- /* Check whether the area is larger than permitted. */\r
- area = 0.5 * (dxod * dyda - dyod * dxda);\r
- if (fixedarea && (area > maxarea)) {\r
- /* Add this triangle to the list of bad triangles. */\r
- enqueuebadtri(testtri, angle, tapex, torg, tdest);\r
- } else if (vararea) {\r
- /* Nonpositive area constraints are treated as unconstrained. */\r
- if ((area > areabound(*testtri)) && (areabound(*testtri) > 0.0)) {\r
- /* Add this triangle to the list of bad triangles. */\r
- enqueuebadtri(testtri, angle, tapex, torg, tdest);\r
- }\r
- }\r
- }\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/** **/\r
-/** **/\r
-/********* Mesh quality testing routines end here *********/\r
-\r
-/********* Point location routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* makepointmap() Construct a mapping from points to triangles to improve */\r
-/* the speed of point location for segment insertion. */\r
-/* */\r
-/* Traverses all the triangles, and provides each corner of each triangle */\r
-/* with a pointer to that triangle. Of course, pointers will be */\r
-/* overwritten by other pointers because (almost) each point is a corner */\r
-/* of several triangles, but in the end every point will point to some */\r
-/* triangle that contains it. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void makepointmap()\r
-{\r
- struct triedge triangleloop;\r
- point triorg;\r
-\r
- if (verbose) {\r
- printf(" Constructing mapping from points to triangles.\n");\r
- }\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- /* Check all three points of the triangle. */\r
- for (triangleloop.orient = 0; triangleloop.orient < 3;\r
- triangleloop.orient++) {\r
- org(triangleloop, triorg);\r
- setpoint2tri(triorg, encode(triangleloop));\r
- }\r
- triangleloop.tri = triangletraverse();\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* preciselocate() Find a triangle or edge containing a given point. */\r
-/* */\r
-/* Begins its search from `searchtri'. It is important that `searchtri' */\r
-/* be a handle with the property that `searchpoint' is strictly to the left */\r
-/* of the edge denoted by `searchtri', or is collinear with that edge and */\r
-/* does not intersect that edge. (In particular, `searchpoint' should not */\r
-/* be the origin or destination of that edge.) */\r
-/* */\r
-/* These conditions are imposed because preciselocate() is normally used in */\r
-/* one of two situations: */\r
-/* */\r
-/* (1) To try to find the location to insert a new point. Normally, we */\r
-/* know an edge that the point is strictly to the left of. In the */\r
-/* incremental Delaunay algorithm, that edge is a bounding box edge. */\r
-/* In Ruppert's Delaunay refinement algorithm for quality meshing, */\r
-/* that edge is the shortest edge of the triangle whose circumcenter */\r
-/* is being inserted. */\r
-/* */\r
-/* (2) To try to find an existing point. In this case, any edge on the */\r
-/* convex hull is a good starting edge. The possibility that the */\r
-/* vertex one seeks is an endpoint of the starting edge must be */\r
-/* screened out before preciselocate() is called. */\r
-/* */\r
-/* On completion, `searchtri' is a triangle that contains `searchpoint'. */\r
-/* */\r
-/* This implementation differs from that given by Guibas and Stolfi. It */\r
-/* walks from triangle to triangle, crossing an edge only if `searchpoint' */\r
-/* is on the other side of the line containing that edge. After entering */\r
-/* a triangle, there are two edges by which one can leave that triangle. */\r
-/* If both edges are valid (`searchpoint' is on the other side of both */\r
-/* edges), one of the two is chosen by drawing a line perpendicular to */\r
-/* the entry edge (whose endpoints are `forg' and `fdest') passing through */\r
-/* `fapex'. Depending on which side of this perpendicular `searchpoint' */\r
-/* falls on, an exit edge is chosen. */\r
-/* */\r
-/* This implementation is empirically faster than the Guibas and Stolfi */\r
-/* point location routine (which I originally used), which tends to spiral */\r
-/* in toward its target. */\r
-/* */\r
-/* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */\r
-/* is a handle whose origin is the existing vertex. */\r
-/* */\r
-/* Returns ONEDGE if the point lies on a mesh edge. `searchtri' is a */\r
-/* handle whose primary edge is the edge on which the point lies. */\r
-/* */\r
-/* Returns INTRIANGLE if the point lies strictly within a triangle. */\r
-/* `searchtri' is a handle on the triangle that contains the point. */\r
-/* */\r
-/* Returns OUTSIDE if the point lies outside the mesh. `searchtri' is a */\r
-/* handle whose primary edge the point is to the right of. This might */\r
-/* occur when the circumcenter of a triangle falls just slightly outside */\r
-/* the mesh due to floating-point roundoff error. It also occurs when */\r
-/* seeking a hole or region point that a foolish user has placed outside */\r
-/* the mesh. */\r
-/* */\r
-/* WARNING: This routine is designed for convex triangulations, and will */\r
-/* not generally work after the holes and concavities have been carved. */\r
-/* However, it can still be used to find the circumcenter of a triangle, as */\r
-/* long as the search is begun from the triangle in question. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-enum locateresult preciselocate(searchpoint, searchtri)\r
-point searchpoint;\r
-struct triedge *searchtri;\r
-{\r
- struct triedge backtracktri;\r
- point forg, fdest, fapex;\r
- point swappoint;\r
- REAL orgorient, destorient;\r
- int moveleft;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- if (verbose > 2) {\r
- printf(" Searching for point (%.12g, %.12g).\n",\r
- searchpoint[0], searchpoint[1]);\r
- }\r
- /* Where are we? */\r
- org(*searchtri, forg);\r
- dest(*searchtri, fdest);\r
- apex(*searchtri, fapex);\r
- while (1) {\r
- if (verbose > 2) {\r
- printf(" At (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",\r
- forg[0], forg[1], fdest[0], fdest[1], fapex[0], fapex[1]);\r
- }\r
- /* Check whether the apex is the point we seek. */\r
- if ((fapex[0] == searchpoint[0]) && (fapex[1] == searchpoint[1])) {\r
- lprevself(*searchtri);\r
- return ONVERTEX;\r
- }\r
- /* Does the point lie on the other side of the line defined by the */\r
- /* triangle edge opposite the triangle's destination? */\r
- destorient = counterclockwise(forg, fapex, searchpoint);\r
- /* Does the point lie on the other side of the line defined by the */\r
- /* triangle edge opposite the triangle's origin? */\r
- orgorient = counterclockwise(fapex, fdest, searchpoint);\r
- if (destorient > 0.0) {\r
- if (orgorient > 0.0) {\r
- /* Move left if the inner product of (fapex - searchpoint) and */\r
- /* (fdest - forg) is positive. This is equivalent to drawing */\r
- /* a line perpendicular to the line (forg, fdest) passing */\r
- /* through `fapex', and determining which side of this line */\r
- /* `searchpoint' falls on. */\r
- moveleft = (fapex[0] - searchpoint[0]) * (fdest[0] - forg[0]) +\r
- (fapex[1] - searchpoint[1]) * (fdest[1] - forg[1]) > 0.0;\r
- } else {\r
- moveleft = 1;\r
- }\r
- } else {\r
- if (orgorient > 0.0) {\r
- moveleft = 0;\r
- } else {\r
- /* The point we seek must be on the boundary of or inside this */\r
- /* triangle. */\r
- if (destorient == 0.0) {\r
- lprevself(*searchtri);\r
- return ONEDGE;\r
- }\r
- if (orgorient == 0.0) {\r
- lnextself(*searchtri);\r
- return ONEDGE;\r
- }\r
- return INTRIANGLE;\r
- }\r
- }\r
-\r
- /* Move to another triangle. Leave a trace `backtracktri' in case */\r
- /* floating-point roundoff or some such bogey causes us to walk */\r
- /* off a boundary of the triangulation. We can just bounce off */\r
- /* the boundary as if it were an elastic band. */\r
- if (moveleft) {\r
- lprev(*searchtri, backtracktri);\r
- fdest = fapex;\r
- } else {\r
- lnext(*searchtri, backtracktri);\r
- forg = fapex;\r
- }\r
- sym(backtracktri, *searchtri);\r
-\r
- /* Check for walking off the edge. */\r
- if (searchtri->tri == dummytri) {\r
- /* Turn around. */\r
- triedgecopy(backtracktri, *searchtri);\r
- swappoint = forg;\r
- forg = fdest;\r
- fdest = swappoint;\r
- apex(*searchtri, fapex);\r
- /* Check if the point really is beyond the triangulation boundary. */\r
- destorient = counterclockwise(forg, fapex, searchpoint);\r
- orgorient = counterclockwise(fapex, fdest, searchpoint);\r
- if ((orgorient < 0.0) && (destorient < 0.0)) {\r
- return OUTSIDE;\r
- }\r
- } else {\r
- apex(*searchtri, fapex);\r
- }\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* locate() Find a triangle or edge containing a given point. */\r
-/* */\r
-/* Searching begins from one of: the input `searchtri', a recently */\r
-/* encountered triangle `recenttri', or from a triangle chosen from a */\r
-/* random sample. The choice is made by determining which triangle's */\r
-/* origin is closest to the point we are searcing for. Normally, */\r
-/* `searchtri' should be a handle on the convex hull of the triangulation. */\r
-/* */\r
-/* Details on the random sampling method can be found in the Mucke, Saias, */\r
-/* and Zhu paper cited in the header of this code. */\r
-/* */\r
-/* On completion, `searchtri' is a triangle that contains `searchpoint'. */\r
-/* */\r
-/* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */\r
-/* is a handle whose origin is the existing vertex. */\r
-/* */\r
-/* Returns ONEDGE if the point lies on a mesh edge. `searchtri' is a */\r
-/* handle whose primary edge is the edge on which the point lies. */\r
-/* */\r
-/* Returns INTRIANGLE if the point lies strictly within a triangle. */\r
-/* `searchtri' is a handle on the triangle that contains the point. */\r
-/* */\r
-/* Returns OUTSIDE if the point lies outside the mesh. `searchtri' is a */\r
-/* handle whose primary edge the point is to the right of. This might */\r
-/* occur when the circumcenter of a triangle falls just slightly outside */\r
-/* the mesh due to floating-point roundoff error. It also occurs when */\r
-/* seeking a hole or region point that a foolish user has placed outside */\r
-/* the mesh. */\r
-/* */\r
-/* WARNING: This routine is designed for convex triangulations, and will */\r
-/* not generally work after the holes and concavities have been carved. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-enum locateresult locate(searchpoint, searchtri)\r
-point searchpoint;\r
-struct triedge *searchtri;\r
-{\r
- VOID **sampleblock;\r
- triangle *firsttri;\r
- struct triedge sampletri;\r
- point torg, tdest;\r
- unsigned long alignptr;\r
- REAL searchdist, dist;\r
- REAL ahead;\r
- long sampleblocks, samplesperblock, samplenum;\r
- long triblocks;\r
- long i, j;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- if (verbose > 2) {\r
- printf(" Randomly sampling for a triangle near point (%.12g, %.12g).\n",\r
- searchpoint[0], searchpoint[1]);\r
- }\r
- /* Record the distance from the suggested starting triangle to the */\r
- /* point we seek. */\r
- org(*searchtri, torg);\r
- searchdist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])\r
- + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);\r
- if (verbose > 2) {\r
- printf(" Boundary triangle has origin (%.12g, %.12g).\n",\r
- torg[0], torg[1]);\r
- }\r
-\r
- /* If a recently encountered triangle has been recorded and has not been */\r
- /* deallocated, test it as a good starting point. */\r
- if (recenttri.tri != (triangle *) NULL) {\r
- if (recenttri.tri[3] != (triangle) NULL) {\r
- org(recenttri, torg);\r
- if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {\r
- triedgecopy(recenttri, *searchtri);\r
- return ONVERTEX;\r
- }\r
- dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])\r
- + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);\r
- if (dist < searchdist) {\r
- triedgecopy(recenttri, *searchtri);\r
- searchdist = dist;\r
- if (verbose > 2) {\r
- printf(" Choosing recent triangle with origin (%.12g, %.12g).\n",\r
- torg[0], torg[1]);\r
- }\r
- }\r
- }\r
- }\r
-\r
- /* The number of random samples taken is proportional to the cube root of */\r
- /* the number of triangles in the mesh. The next bit of code assumes */\r
- /* that the number of triangles increases monotonically. */\r
- while (SAMPLEFACTOR * samples * samples * samples < triangles.items) {\r
- samples++;\r
- }\r
- triblocks = (triangles.maxitems + TRIPERBLOCK - 1) / TRIPERBLOCK;\r
- samplesperblock = 1 + (samples / triblocks);\r
- sampleblocks = samples / samplesperblock;\r
- sampleblock = triangles.firstblock;\r
- sampletri.orient = 0;\r
- for (i = 0; i < sampleblocks; i++) {\r
- alignptr = (unsigned long) (sampleblock + 1);\r
- firsttri = (triangle *) (alignptr + (unsigned long) triangles.alignbytes\r
- - (alignptr % (unsigned long) triangles.alignbytes));\r
- for (j = 0; j < samplesperblock; j++) {\r
- if (i == triblocks - 1) {\r
- samplenum = randomnation((int)\r
- (triangles.maxitems - (i * TRIPERBLOCK)));\r
- } else {\r
- samplenum = randomnation(TRIPERBLOCK);\r
- }\r
- sampletri.tri = (triangle *)\r
- (firsttri + (samplenum * triangles.itemwords));\r
- if (sampletri.tri[3] != (triangle) NULL) {\r
- org(sampletri, torg);\r
- dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])\r
- + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);\r
- if (dist < searchdist) {\r
- triedgecopy(sampletri, *searchtri);\r
- searchdist = dist;\r
- if (verbose > 2) {\r
- printf(" Choosing triangle with origin (%.12g, %.12g).\n",\r
- torg[0], torg[1]);\r
- }\r
- }\r
- }\r
- }\r
- sampleblock = (VOID **) *sampleblock;\r
- }\r
- /* Where are we? */\r
- org(*searchtri, torg);\r
- dest(*searchtri, tdest);\r
- /* Check the starting triangle's vertices. */\r
- if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {\r
- return ONVERTEX;\r
- }\r
- if ((tdest[0] == searchpoint[0]) && (tdest[1] == searchpoint[1])) {\r
- lnextself(*searchtri);\r
- return ONVERTEX;\r
- }\r
- /* Orient `searchtri' to fit the preconditions of calling preciselocate(). */\r
- ahead = counterclockwise(torg, tdest, searchpoint);\r
- if (ahead < 0.0) {\r
- /* Turn around so that `searchpoint' is to the left of the */\r
- /* edge specified by `searchtri'. */\r
- symself(*searchtri);\r
- } else if (ahead == 0.0) {\r
- /* Check if `searchpoint' is between `torg' and `tdest'. */\r
- if (((torg[0] < searchpoint[0]) == (searchpoint[0] < tdest[0]))\r
- && ((torg[1] < searchpoint[1]) == (searchpoint[1] < tdest[1]))) {\r
- return ONEDGE;\r
- }\r
- }\r
- return preciselocate(searchpoint, searchtri);\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Point location routines end here *********/\r
-\r
-/********* Mesh transformation routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* insertshelle() Create a new shell edge and insert it between two */\r
-/* triangles. */\r
-/* */\r
-/* The new shell edge is inserted at the edge described by the handle */\r
-/* `tri'. Its vertices are properly initialized. The marker `shellemark' */\r
-/* is applied to the shell edge and, if appropriate, its vertices. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void insertshelle(tri, shellemark)\r
-struct triedge *tri; /* Edge at which to insert the new shell edge. */\r
-int shellemark; /* Marker for the new shell edge. */\r
-{\r
- struct triedge oppotri;\r
- struct edge newshelle;\r
- point triorg, tridest;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- /* Mark points if possible. */\r
- org(*tri, triorg);\r
- dest(*tri, tridest);\r
- if (pointmark(triorg) == 0) {\r
- setpointmark(triorg, shellemark);\r
- }\r
- if (pointmark(tridest) == 0) {\r
- setpointmark(tridest, shellemark);\r
- }\r
- /* Check if there's already a shell edge here. */\r
- tspivot(*tri, newshelle);\r
- if (newshelle.sh == dummysh) {\r
- /* Make new shell edge and initialize its vertices. */\r
- makeshelle(&newshelle);\r
- setsorg(newshelle, tridest);\r
- setsdest(newshelle, triorg);\r
- /* Bond new shell edge to the two triangles it is sandwiched between. */\r
- /* Note that the facing triangle `oppotri' might be equal to */\r
- /* `dummytri' (outer space), but the new shell edge is bonded to it */\r
- /* all the same. */\r
- tsbond(*tri, newshelle);\r
- sym(*tri, oppotri);\r
- ssymself(newshelle);\r
- tsbond(oppotri, newshelle);\r
- setmark(newshelle, shellemark);\r
- if (verbose > 2) {\r
- printf(" Inserting new ");\r
- printshelle(&newshelle);\r
- }\r
- } else {\r
- if (mark(newshelle) == 0) {\r
- setmark(newshelle, shellemark);\r
- }\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* Terminology */\r
-/* */\r
-/* A "local transformation" replaces a small set of triangles with another */\r
-/* set of triangles. This may or may not involve inserting or deleting a */\r
-/* point. */\r
-/* */\r
-/* The term "casing" is used to describe the set of triangles that are */\r
-/* attached to the triangles being transformed, but are not transformed */\r
-/* themselves. Think of the casing as a fixed hollow structure inside */\r
-/* which all the action happens. A "casing" is only defined relative to */\r
-/* a single transformation; each occurrence of a transformation will */\r
-/* involve a different casing. */\r
-/* */\r
-/* A "shell" is similar to a "casing". The term "shell" describes the set */\r
-/* of shell edges (if any) that are attached to the triangles being */\r
-/* transformed. However, I sometimes use "shell" to refer to a single */\r
-/* shell edge, so don't get confused. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* flip() Transform two triangles to two different triangles by flipping */\r
-/* an edge within a quadrilateral. */\r
-/* */\r
-/* Imagine the original triangles, abc and bad, oriented so that the */\r
-/* shared edge ab lies in a horizontal plane, with the point b on the left */\r
-/* and the point a on the right. The point c lies below the edge, and the */\r
-/* point d lies above the edge. The `flipedge' handle holds the edge ab */\r
-/* of triangle abc, and is directed left, from vertex a to vertex b. */\r
-/* */\r
-/* The triangles abc and bad are deleted and replaced by the triangles cdb */\r
-/* and dca. The triangles that represent abc and bad are NOT deallocated; */\r
-/* they are reused for dca and cdb, respectively. Hence, any handles that */\r
-/* may have held the original triangles are still valid, although not */\r
-/* directed as they were before. */\r
-/* */\r
-/* Upon completion of this routine, the `flipedge' handle holds the edge */\r
-/* dc of triangle dca, and is directed down, from vertex d to vertex c. */\r
-/* (Hence, the two triangles have rotated counterclockwise.) */\r
-/* */\r
-/* WARNING: This transformation is geometrically valid only if the */\r
-/* quadrilateral adbc is convex. Furthermore, this transformation is */\r
-/* valid only if there is not a shell edge between the triangles abc and */\r
-/* bad. This routine does not check either of these preconditions, and */\r
-/* it is the responsibility of the calling routine to ensure that they are */\r
-/* met. If they are not, the streets shall be filled with wailing and */\r
-/* gnashing of teeth. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void flip(flipedge)\r
-struct triedge *flipedge; /* Handle for the triangle abc. */\r
-{\r
- struct triedge botleft, botright;\r
- struct triedge topleft, topright;\r
- struct triedge top;\r
- struct triedge botlcasing, botrcasing;\r
- struct triedge toplcasing, toprcasing;\r
- struct edge botlshelle, botrshelle;\r
- struct edge toplshelle, toprshelle;\r
- point leftpoint, rightpoint, botpoint;\r
- point farpoint;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- /* Identify the vertices of the quadrilateral. */\r
- org(*flipedge, rightpoint);\r
- dest(*flipedge, leftpoint);\r
- apex(*flipedge, botpoint);\r
- sym(*flipedge, top);\r
-#ifdef SELF_CHECK\r
- if (top.tri == dummytri) {\r
- printf("Internal error in flip(): Attempt to flip on boundary.\n");\r
- lnextself(*flipedge);\r
- return;\r
- }\r
- if (checksegments) {\r
- tspivot(*flipedge, toplshelle);\r
- if (toplshelle.sh != dummysh) {\r
- printf("Internal error in flip(): Attempt to flip a segment.\n");\r
- lnextself(*flipedge);\r
- return;\r
- }\r
- }\r
-#endif /* SELF_CHECK */\r
- apex(top, farpoint);\r
-\r
- /* Identify the casing of the quadrilateral. */\r
- lprev(top, topleft);\r
- sym(topleft, toplcasing);\r
- lnext(top, topright);\r
- sym(topright, toprcasing);\r
- lnext(*flipedge, botleft);\r
- sym(botleft, botlcasing);\r
- lprev(*flipedge, botright);\r
- sym(botright, botrcasing);\r
- /* Rotate the quadrilateral one-quarter turn counterclockwise. */\r
- bond(topleft, botlcasing);\r
- bond(botleft, botrcasing);\r
- bond(botright, toprcasing);\r
- bond(topright, toplcasing);\r
-\r
- if (checksegments) {\r
- /* Check for shell edges and rebond them to the quadrilateral. */\r
- tspivot(topleft, toplshelle);\r
- tspivot(botleft, botlshelle);\r
- tspivot(botright, botrshelle);\r
- tspivot(topright, toprshelle);\r
- if (toplshelle.sh == dummysh) {\r
- tsdissolve(topright);\r
- } else {\r
- tsbond(topright, toplshelle);\r
- }\r
- if (botlshelle.sh == dummysh) {\r
- tsdissolve(topleft);\r
- } else {\r
- tsbond(topleft, botlshelle);\r
- }\r
- if (botrshelle.sh == dummysh) {\r
- tsdissolve(botleft);\r
- } else {\r
- tsbond(botleft, botrshelle);\r
- }\r
- if (toprshelle.sh == dummysh) {\r
- tsdissolve(botright);\r
- } else {\r
- tsbond(botright, toprshelle);\r
- }\r
- }\r
-\r
- /* New point assignments for the rotated quadrilateral. */\r
- setorg(*flipedge, farpoint);\r
- setdest(*flipedge, botpoint);\r
- setapex(*flipedge, rightpoint);\r
- setorg(top, botpoint);\r
- setdest(top, farpoint);\r
- setapex(top, leftpoint);\r
- if (verbose > 2) {\r
- printf(" Edge flip results in left ");\r
- lnextself(topleft);\r
- printtriangle(&topleft);\r
- printf(" and right ");\r
- printtriangle(flipedge);\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* insertsite() Insert a vertex into a Delaunay triangulation, */\r
-/* performing flips as necessary to maintain the Delaunay */\r
-/* property. */\r
-/* */\r
-/* The point `insertpoint' is located. If `searchtri.tri' is not NULL, */\r
-/* the search for the containing triangle begins from `searchtri'. If */\r
-/* `searchtri.tri' is NULL, a full point location procedure is called. */\r
-/* If `insertpoint' is found inside a triangle, the triangle is split into */\r
-/* three; if `insertpoint' lies on an edge, the edge is split in two, */\r
-/* thereby splitting the two adjacent triangles into four. Edge flips are */\r
-/* used to restore the Delaunay property. If `insertpoint' lies on an */\r
-/* existing vertex, no action is taken, and the value DUPLICATEPOINT is */\r
-/* returned. On return, `searchtri' is set to a handle whose origin is the */\r
-/* existing vertex. */\r
-/* */\r
-/* Normally, the parameter `splitedge' is set to NULL, implying that no */\r
-/* segment should be split. In this case, if `insertpoint' is found to */\r
-/* lie on a segment, no action is taken, and the value VIOLATINGPOINT is */\r
-/* returned. On return, `searchtri' is set to a handle whose primary edge */\r
-/* is the violated segment. */\r
-/* */\r
-/* If the calling routine wishes to split a segment by inserting a point in */\r
-/* it, the parameter `splitedge' should be that segment. In this case, */\r
-/* `searchtri' MUST be the triangle handle reached by pivoting from that */\r
-/* segment; no point location is done. */\r
-/* */\r
-/* `segmentflaws' and `triflaws' are flags that indicate whether or not */\r
-/* there should be checks for the creation of encroached segments or bad */\r
-/* quality faces. If a newly inserted point encroaches upon segments, */\r
-/* these segments are added to the list of segments to be split if */\r
-/* `segmentflaws' is set. If bad triangles are created, these are added */\r
-/* to the queue if `triflaws' is set. */\r
-/* */\r
-/* If a duplicate point or violated segment does not prevent the point */\r
-/* from being inserted, the return value will be ENCROACHINGPOINT if the */\r
-/* point encroaches upon a segment (and checking is enabled), or */\r
-/* SUCCESSFULPOINT otherwise. In either case, `searchtri' is set to a */\r
-/* handle whose origin is the newly inserted vertex. */\r
-/* */\r
-/* insertsite() does not use flip() for reasons of speed; some */\r
-/* information can be reused from edge flip to edge flip, like the */\r
-/* locations of shell edges. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-enum insertsiteresult insertsite(insertpoint, searchtri, splitedge,\r
- segmentflaws, triflaws)\r
-point insertpoint;\r
-struct triedge *searchtri;\r
-struct edge *splitedge;\r
-int segmentflaws;\r
-int triflaws;\r
-{\r
- struct triedge horiz;\r
- struct triedge top;\r
- struct triedge botleft, botright;\r
- struct triedge topleft, topright;\r
- struct triedge newbotleft, newbotright;\r
- struct triedge newtopright;\r
- struct triedge botlcasing, botrcasing;\r
- struct triedge toplcasing, toprcasing;\r
- struct triedge testtri;\r
- struct edge botlshelle, botrshelle;\r
- struct edge toplshelle, toprshelle;\r
- struct edge brokenshelle;\r
- struct edge checkshelle;\r
- struct edge rightedge;\r
- struct edge newedge;\r
- struct edge *encroached;\r
- point first;\r
- point leftpoint, rightpoint, botpoint, toppoint, farpoint;\r
- REAL attrib;\r
- REAL area;\r
- enum insertsiteresult success;\r
- enum locateresult intersect;\r
- int doflip;\r
- int mirrorflag;\r
- int i;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by spivot() and tspivot(). */\r
-\r
- if (verbose > 1) {\r
- printf(" Inserting (%.12g, %.12g).\n", insertpoint[0], insertpoint[1]);\r
- }\r
- if (splitedge == (struct edge *) NULL) {\r
- /* Find the location of the point to be inserted. Check if a good */\r
- /* starting triangle has already been provided by the caller. */\r
- if (searchtri->tri == (triangle *) NULL) {\r
- /* Find a boundary triangle. */\r
- horiz.tri = dummytri;\r
- horiz.orient = 0;\r
- symself(horiz);\r
- /* Search for a triangle containing `insertpoint'. */\r
- intersect = locate(insertpoint, &horiz);\r
- } else {\r
- /* Start searching from the triangle provided by the caller. */\r
- triedgecopy(*searchtri, horiz);\r
- intersect = preciselocate(insertpoint, &horiz);\r
- }\r
- } else {\r
- /* The calling routine provides the edge in which the point is inserted. */\r
- triedgecopy(*searchtri, horiz);\r
- intersect = ONEDGE;\r
- }\r
- if (intersect == ONVERTEX) {\r
- /* There's already a vertex there. Return in `searchtri' a triangle */\r
- /* whose origin is the existing vertex. */\r
- triedgecopy(horiz, *searchtri);\r
- triedgecopy(horiz, recenttri);\r
- return DUPLICATEPOINT;\r
- }\r
- if ((intersect == ONEDGE) || (intersect == OUTSIDE)) {\r
- /* The vertex falls on an edge or boundary. */\r
- if (checksegments && (splitedge == (struct edge *) NULL)) {\r
- /* Check whether the vertex falls on a shell edge. */\r
- tspivot(horiz, brokenshelle);\r
- if (brokenshelle.sh != dummysh) {\r
- /* The vertex falls on a shell edge. */\r
- if (segmentflaws) {\r
- if (nobisect == 0) {\r
- /* Add the shell edge to the list of encroached segments. */\r
- encroached = (struct edge *) poolalloc(&badsegments);\r
- shellecopy(brokenshelle, *encroached);\r
- } else if ((nobisect == 1) && (intersect == ONEDGE)) {\r
- /* This segment may be split only if it is an internal boundary. */\r
- sym(horiz, testtri);\r
- if (testtri.tri != dummytri) {\r
- /* Add the shell edge to the list of encroached segments. */\r
- encroached = (struct edge *) poolalloc(&badsegments);\r
- shellecopy(brokenshelle, *encroached);\r
- }\r
- }\r
- }\r
- /* Return a handle whose primary edge contains the point, */\r
- /* which has not been inserted. */\r
- triedgecopy(horiz, *searchtri);\r
- triedgecopy(horiz, recenttri);\r
- return VIOLATINGPOINT;\r
- }\r
- }\r
- /* Insert the point on an edge, dividing one triangle into two (if */\r
- /* the edge lies on a boundary) or two triangles into four. */\r
- lprev(horiz, botright);\r
- sym(botright, botrcasing);\r
- sym(horiz, topright);\r
- /* Is there a second triangle? (Or does this edge lie on a boundary?) */\r
- mirrorflag = topright.tri != dummytri;\r
- if (mirrorflag) {\r
- lnextself(topright);\r
- sym(topright, toprcasing);\r
- maketriangle(&newtopright);\r
- } else {\r
- /* Splitting the boundary edge increases the number of boundary edges. */\r
- hullsize++;\r
- }\r
- maketriangle(&newbotright);\r
-\r
- /* Set the vertices of changed and new triangles. */\r
- org(horiz, rightpoint);\r
- dest(horiz, leftpoint);\r
- apex(horiz, botpoint);\r
- setorg(newbotright, botpoint);\r
- setdest(newbotright, rightpoint);\r
- setapex(newbotright, insertpoint);\r
- setorg(horiz, insertpoint);\r
- for (i = 0; i < eextras; i++) {\r
- /* Set the element attributes of a new triangle. */\r
- setelemattribute(newbotright, i, elemattribute(botright, i));\r
- }\r
- if (vararea) {\r
- /* Set the area constraint of a new triangle. */\r
- setareabound(newbotright, areabound(botright));\r
- }\r
- if (mirrorflag) {\r
- dest(topright, toppoint);\r
- setorg(newtopright, rightpoint);\r
- setdest(newtopright, toppoint);\r
- setapex(newtopright, insertpoint);\r
- setorg(topright, insertpoint);\r
- for (i = 0; i < eextras; i++) {\r
- /* Set the element attributes of another new triangle. */\r
- setelemattribute(newtopright, i, elemattribute(topright, i));\r
- }\r
- if (vararea) {\r
- /* Set the area constraint of another new triangle. */\r
- setareabound(newtopright, areabound(topright));\r
- }\r
- }\r
-\r
- /* There may be shell edges that need to be bonded */\r
- /* to the new triangle(s). */\r
- if (checksegments) {\r
- tspivot(botright, botrshelle);\r
- if (botrshelle.sh != dummysh) {\r
- tsdissolve(botright);\r
- tsbond(newbotright, botrshelle);\r
- }\r
- if (mirrorflag) {\r
- tspivot(topright, toprshelle);\r
- if (toprshelle.sh != dummysh) {\r
- tsdissolve(topright);\r
- tsbond(newtopright, toprshelle);\r
- }\r
- }\r
- }\r
-\r
- /* Bond the new triangle(s) to the surrounding triangles. */\r
- bond(newbotright, botrcasing);\r
- lprevself(newbotright);\r
- bond(newbotright, botright);\r
- lprevself(newbotright);\r
- if (mirrorflag) {\r
- bond(newtopright, toprcasing);\r
- lnextself(newtopright);\r
- bond(newtopright, topright);\r
- lnextself(newtopright);\r
- bond(newtopright, newbotright);\r
- }\r
-\r
- if (splitedge != (struct edge *) NULL) {\r
- /* Split the shell edge into two. */\r
- setsdest(*splitedge, insertpoint);\r
- ssymself(*splitedge);\r
- spivot(*splitedge, rightedge);\r
- insertshelle(&newbotright, mark(*splitedge));\r
- tspivot(newbotright, newedge);\r
- sbond(*splitedge, newedge);\r
- ssymself(newedge);\r
- sbond(newedge, rightedge);\r
- ssymself(*splitedge);\r
- }\r
-\r
-#ifdef SELF_CHECK\r
- if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle prior to edge point insertion (bottom).\n");\r
- }\r
- if (mirrorflag) {\r
- if (counterclockwise(leftpoint, rightpoint, toppoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle prior to edge point insertion (top).\n");\r
- }\r
- if (counterclockwise(rightpoint, toppoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after edge point insertion (top right).\n"\r
- );\r
- }\r
- if (counterclockwise(toppoint, leftpoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after edge point insertion (top left).\n"\r
- );\r
- }\r
- }\r
- if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after edge point insertion (bottom left).\n"\r
- );\r
- }\r
- if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(\r
- " Clockwise triangle after edge point insertion (bottom right).\n");\r
- }\r
-#endif /* SELF_CHECK */\r
- if (verbose > 2) {\r
- printf(" Updating bottom left ");\r
- printtriangle(&botright);\r
- if (mirrorflag) {\r
- printf(" Updating top left ");\r
- printtriangle(&topright);\r
- printf(" Creating top right ");\r
- printtriangle(&newtopright);\r
- }\r
- printf(" Creating bottom right ");\r
- printtriangle(&newbotright);\r
- }\r
-\r
- /* Position `horiz' on the first edge to check for */\r
- /* the Delaunay property. */\r
- lnextself(horiz);\r
- } else {\r
- /* Insert the point in a triangle, splitting it into three. */\r
- lnext(horiz, botleft);\r
- lprev(horiz, botright);\r
- sym(botleft, botlcasing);\r
- sym(botright, botrcasing);\r
- maketriangle(&newbotleft);\r
- maketriangle(&newbotright);\r
-\r
- /* Set the vertices of changed and new triangles. */\r
- org(horiz, rightpoint);\r
- dest(horiz, leftpoint);\r
- apex(horiz, botpoint);\r
- setorg(newbotleft, leftpoint);\r
- setdest(newbotleft, botpoint);\r
- setapex(newbotleft, insertpoint);\r
- setorg(newbotright, botpoint);\r
- setdest(newbotright, rightpoint);\r
- setapex(newbotright, insertpoint);\r
- setapex(horiz, insertpoint);\r
- for (i = 0; i < eextras; i++) {\r
- /* Set the element attributes of the new triangles. */\r
- attrib = elemattribute(horiz, i);\r
- setelemattribute(newbotleft, i, attrib);\r
- setelemattribute(newbotright, i, attrib);\r
- }\r
- if (vararea) {\r
- /* Set the area constraint of the new triangles. */\r
- area = areabound(horiz);\r
- setareabound(newbotleft, area);\r
- setareabound(newbotright, area);\r
- }\r
-\r
- /* There may be shell edges that need to be bonded */\r
- /* to the new triangles. */\r
- if (checksegments) {\r
- tspivot(botleft, botlshelle);\r
- if (botlshelle.sh != dummysh) {\r
- tsdissolve(botleft);\r
- tsbond(newbotleft, botlshelle);\r
- }\r
- tspivot(botright, botrshelle);\r
- if (botrshelle.sh != dummysh) {\r
- tsdissolve(botright);\r
- tsbond(newbotright, botrshelle);\r
- }\r
- }\r
-\r
- /* Bond the new triangles to the surrounding triangles. */\r
- bond(newbotleft, botlcasing);\r
- bond(newbotright, botrcasing);\r
- lnextself(newbotleft);\r
- lprevself(newbotright);\r
- bond(newbotleft, newbotright);\r
- lnextself(newbotleft);\r
- bond(botleft, newbotleft);\r
- lprevself(newbotright);\r
- bond(botright, newbotright);\r
-\r
-#ifdef SELF_CHECK\r
- if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle prior to point insertion.\n");\r
- }\r
- if (counterclockwise(rightpoint, leftpoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after point insertion (top).\n");\r
- }\r
- if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after point insertion (left).\n");\r
- }\r
- if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after point insertion (right).\n");\r
- }\r
-#endif /* SELF_CHECK */\r
- if (verbose > 2) {\r
- printf(" Updating top ");\r
- printtriangle(&horiz);\r
- printf(" Creating left ");\r
- printtriangle(&newbotleft);\r
- printf(" Creating right ");\r
- printtriangle(&newbotright);\r
- }\r
- }\r
-\r
- /* The insertion is successful by default, unless an encroached */\r
- /* edge is found. */\r
- success = SUCCESSFULPOINT;\r
- /* Circle around the newly inserted vertex, checking each edge opposite */\r
- /* it for the Delaunay property. Non-Delaunay edges are flipped. */\r
- /* `horiz' is always the edge being checked. `first' marks where to */\r
- /* stop circling. */\r
- org(horiz, first);\r
- rightpoint = first;\r
- dest(horiz, leftpoint);\r
- /* Circle until finished. */\r
- while (1) {\r
- /* By default, the edge will be flipped. */\r
- doflip = 1;\r
- if (checksegments) {\r
- /* Check for a segment, which cannot be flipped. */\r
- tspivot(horiz, checkshelle);\r
- if (checkshelle.sh != dummysh) {\r
- /* The edge is a segment and cannot be flipped. */\r
- doflip = 0;\r
-#ifndef CDT_ONLY\r
- if (segmentflaws) {\r
- /* Does the new point encroach upon this segment? */\r
- if (checkedge4encroach(&checkshelle)) {\r
- success = ENCROACHINGPOINT;\r
- }\r
- }\r
-#endif /* not CDT_ONLY */\r
- }\r
- }\r
- if (doflip) {\r
- /* Check if the edge is a boundary edge. */\r
- sym(horiz, top);\r
- if (top.tri == dummytri) {\r
- /* The edge is a boundary edge and cannot be flipped. */\r
- doflip = 0;\r
- } else {\r
- /* Find the point on the other side of the edge. */\r
- apex(top, farpoint);\r
- /* In the incremental Delaunay triangulation algorithm, any of */\r
- /* `leftpoint', `rightpoint', and `farpoint' could be vertices */\r
- /* of the triangular bounding box. These vertices must be */\r
- /* treated as if they are infinitely distant, even though their */\r
- /* "coordinates" are not. */\r
- if ((leftpoint == infpoint1) || (leftpoint == infpoint2)\r
- || (leftpoint == infpoint3)) {\r
- /* `leftpoint' is infinitely distant. Check the convexity of */\r
- /* the boundary of the triangulation. 'farpoint' might be */\r
- /* infinite as well, but trust me, this same condition */\r
- /* should be applied. */\r
- doflip = counterclockwise(insertpoint, rightpoint, farpoint) > 0.0;\r
- } else if ((rightpoint == infpoint1) || (rightpoint == infpoint2)\r
- || (rightpoint == infpoint3)) {\r
- /* `rightpoint' is infinitely distant. Check the convexity of */\r
- /* the boundary of the triangulation. 'farpoint' might be */\r
- /* infinite as well, but trust me, this same condition */\r
- /* should be applied. */\r
- doflip = counterclockwise(farpoint, leftpoint, insertpoint) > 0.0;\r
- } else if ((farpoint == infpoint1) || (farpoint == infpoint2)\r
- || (farpoint == infpoint3)) {\r
- /* `farpoint' is infinitely distant and cannot be inside */\r
- /* the circumcircle of the triangle `horiz'. */\r
- doflip = 0;\r
- } else {\r
- /* Test whether the edge is locally Delaunay. */\r
- doflip = incircle(leftpoint, insertpoint, rightpoint, farpoint)\r
- > 0.0;\r
- }\r
- if (doflip) {\r
- /* We made it! Flip the edge `horiz' by rotating its containing */\r
- /* quadrilateral (the two triangles adjacent to `horiz'). */\r
- /* Identify the casing of the quadrilateral. */\r
- lprev(top, topleft);\r
- sym(topleft, toplcasing);\r
- lnext(top, topright);\r
- sym(topright, toprcasing);\r
- lnext(horiz, botleft);\r
- sym(botleft, botlcasing);\r
- lprev(horiz, botright);\r
- sym(botright, botrcasing);\r
- /* Rotate the quadrilateral one-quarter turn counterclockwise. */\r
- bond(topleft, botlcasing);\r
- bond(botleft, botrcasing);\r
- bond(botright, toprcasing);\r
- bond(topright, toplcasing);\r
- if (checksegments) {\r
- /* Check for shell edges and rebond them to the quadrilateral. */\r
- tspivot(topleft, toplshelle);\r
- tspivot(botleft, botlshelle);\r
- tspivot(botright, botrshelle);\r
- tspivot(topright, toprshelle);\r
- if (toplshelle.sh == dummysh) {\r
- tsdissolve(topright);\r
- } else {\r
- tsbond(topright, toplshelle);\r
- }\r
- if (botlshelle.sh == dummysh) {\r
- tsdissolve(topleft);\r
- } else {\r
- tsbond(topleft, botlshelle);\r
- }\r
- if (botrshelle.sh == dummysh) {\r
- tsdissolve(botleft);\r
- } else {\r
- tsbond(botleft, botrshelle);\r
- }\r
- if (toprshelle.sh == dummysh) {\r
- tsdissolve(botright);\r
- } else {\r
- tsbond(botright, toprshelle);\r
- }\r
- }\r
- /* New point assignments for the rotated quadrilateral. */\r
- setorg(horiz, farpoint);\r
- setdest(horiz, insertpoint);\r
- setapex(horiz, rightpoint);\r
- setorg(top, insertpoint);\r
- setdest(top, farpoint);\r
- setapex(top, leftpoint);\r
- for (i = 0; i < eextras; i++) {\r
- /* Take the average of the two triangles' attributes. */\r
- attrib = (REAL)(0.5 * (elemattribute(top, i) + elemattribute(horiz, i)));\r
- setelemattribute(top, i, attrib);\r
- setelemattribute(horiz, i, attrib);\r
- }\r
- if (vararea) {\r
- if ((areabound(top) <= 0.0) || (areabound(horiz) <= 0.0)) {\r
- area = -1.0;\r
- } else {\r
- /* Take the average of the two triangles' area constraints. */\r
- /* This prevents small area constraints from migrating a */\r
- /* long, long way from their original location due to flips. */\r
- area = (REAL)(0.5 * (areabound(top) + areabound(horiz)));\r
- }\r
- setareabound(top, area);\r
- setareabound(horiz, area);\r
- }\r
-#ifdef SELF_CHECK\r
- if (insertpoint != (point) NULL) {\r
- if (counterclockwise(leftpoint, insertpoint, rightpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle prior to edge flip (bottom).\n");\r
- }\r
- /* The following test has been removed because constrainededge() */\r
- /* sometimes generates inverted triangles that insertsite() */\r
- /* removes. */\r
-/*\r
- if (counterclockwise(rightpoint, farpoint, leftpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle prior to edge flip (top).\n");\r
- }\r
-*/\r
- if (counterclockwise(farpoint, leftpoint, insertpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after edge flip (left).\n");\r
- }\r
- if (counterclockwise(insertpoint, rightpoint, farpoint) < 0.0) {\r
- printf("Internal error in insertsite():\n");\r
- printf(" Clockwise triangle after edge flip (right).\n");\r
- }\r
- }\r
-#endif /* SELF_CHECK */\r
- if (verbose > 2) {\r
- printf(" Edge flip results in left ");\r
- lnextself(topleft);\r
- printtriangle(&topleft);\r
- printf(" and right ");\r
- printtriangle(&horiz);\r
- }\r
- /* On the next iterations, consider the two edges that were */\r
- /* exposed (this is, are now visible to the newly inserted */\r
- /* point) by the edge flip. */\r
- lprevself(horiz);\r
- leftpoint = farpoint;\r
- }\r
- }\r
- }\r
- if (!doflip) {\r
- /* The handle `horiz' is accepted as locally Delaunay. */\r
-#ifndef CDT_ONLY\r
- if (triflaws) {\r
- /* Check the triangle `horiz' for quality. */\r
- testtriangle(&horiz);\r
- }\r
-#endif /* not CDT_ONLY */\r
- /* Look for the next edge around the newly inserted point. */\r
- lnextself(horiz);\r
- sym(horiz, testtri);\r
- /* Check for finishing a complete revolution about the new point, or */\r
- /* falling off the edge of the triangulation. The latter will */\r
- /* happen when a point is inserted at a boundary. */\r
- if ((leftpoint == first) || (testtri.tri == dummytri)) {\r
- /* We're done. Return a triangle whose origin is the new point. */\r
- lnext(horiz, *searchtri);\r
- lnext(horiz, recenttri);\r
- return success;\r
- }\r
- /* Finish finding the next edge around the newly inserted point. */\r
- lnext(testtri, horiz);\r
- rightpoint = leftpoint;\r
- dest(horiz, leftpoint);\r
- }\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* triangulatepolygon() Find the Delaunay triangulation of a polygon that */\r
-/* has a certain "nice" shape. This includes the */\r
-/* polygons that result from deletion of a point or */\r
-/* insertion of a segment. */\r
-/* */\r
-/* This is a conceptually difficult routine. The starting assumption is */\r
-/* that we have a polygon with n sides. n - 1 of these sides are currently */\r
-/* represented as edges in the mesh. One side, called the "base", need not */\r
-/* be. */\r
-/* */\r
-/* Inside the polygon is a structure I call a "fan", consisting of n - 1 */\r
-/* triangles that share a common origin. For each of these triangles, the */\r
-/* edge opposite the origin is one of the sides of the polygon. The */\r
-/* primary edge of each triangle is the edge directed from the origin to */\r
-/* the destination; note that this is not the same edge that is a side of */\r
-/* the polygon. `firstedge' is the primary edge of the first triangle. */\r
-/* From there, the triangles follow in counterclockwise order about the */\r
-/* polygon, until `lastedge', the primary edge of the last triangle. */\r
-/* `firstedge' and `lastedge' are probably connected to other triangles */\r
-/* beyond the extremes of the fan, but their identity is not important, as */\r
-/* long as the fan remains connected to them. */\r
-/* */\r
-/* Imagine the polygon oriented so that its base is at the bottom. This */\r
-/* puts `firstedge' on the far right, and `lastedge' on the far left. */\r
-/* The right vertex of the base is the destination of `firstedge', and the */\r
-/* left vertex of the base is the apex of `lastedge'. */\r
-/* */\r
-/* The challenge now is to find the right sequence of edge flips to */\r
-/* transform the fan into a Delaunay triangulation of the polygon. Each */\r
-/* edge flip effectively removes one triangle from the fan, committing it */\r
-/* to the polygon. The resulting polygon has one fewer edge. If `doflip' */\r
-/* is set, the final flip will be performed, resulting in a fan of one */\r
-/* (useless?) triangle. If `doflip' is not set, the final flip is not */\r
-/* performed, resulting in a fan of two triangles, and an unfinished */\r
-/* triangular polygon that is not yet filled out with a single triangle. */\r
-/* On completion of the routine, `lastedge' is the last remaining triangle, */\r
-/* or the leftmost of the last two. */\r
-/* */\r
-/* Although the flips are performed in the order described above, the */\r
-/* decisions about what flips to perform are made in precisely the reverse */\r
-/* order. The recursive triangulatepolygon() procedure makes a decision, */\r
-/* uses up to two recursive calls to triangulate the "subproblems" */\r
-/* (polygons with fewer edges), and then performs an edge flip. */\r
-/* */\r
-/* The "decision" it makes is which vertex of the polygon should be */\r
-/* connected to the base. This decision is made by testing every possible */\r
-/* vertex. Once the best vertex is found, the two edges that connect this */\r
-/* vertex to the base become the bases for two smaller polygons. These */\r
-/* are triangulated recursively. Unfortunately, this approach can take */\r
-/* O(n^2) time not only in the worst case, but in many common cases. It's */\r
-/* rarely a big deal for point deletion, where n is rarely larger than ten, */\r
-/* but it could be a big deal for segment insertion, especially if there's */\r
-/* a lot of long segments that each cut many triangles. I ought to code */\r
-/* a faster algorithm some time. */\r
-/* */\r
-/* The `edgecount' parameter is the number of sides of the polygon, */\r
-/* including its base. `triflaws' is a flag that determines whether the */\r
-/* new triangles should be tested for quality, and enqueued if they are */\r
-/* bad. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void triangulatepolygon(firstedge, lastedge, edgecount, doflip, triflaws)\r
-struct triedge *firstedge;\r
-struct triedge *lastedge;\r
-int edgecount;\r
-int doflip;\r
-int triflaws;\r
-{\r
- struct triedge testtri;\r
- struct triedge besttri;\r
- struct triedge tempedge;\r
- point leftbasepoint, rightbasepoint;\r
- point testpoint;\r
- point bestpoint;\r
- int bestnumber;\r
- int i;\r
- triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */\r
-\r
- /* Identify the base vertices. */\r
- apex(*lastedge, leftbasepoint);\r
- dest(*firstedge, rightbasepoint);\r
- if (verbose > 2) {\r
- printf(" Triangulating interior polygon at edge\n");\r
- printf(" (%.12g, %.12g) (%.12g, %.12g)\n", leftbasepoint[0],\r
- leftbasepoint[1], rightbasepoint[0], rightbasepoint[1]);\r
- }\r
- /* Find the best vertex to connect the base to. */\r
- onext(*firstedge, besttri);\r
- dest(besttri, bestpoint);\r
- triedgecopy(besttri, testtri);\r
- bestnumber = 1;\r
- for (i = 2; i <= edgecount - 2; i++) {\r
- onextself(testtri);\r
- dest(testtri, testpoint);\r
- /* Is this a better vertex? */\r
- if (incircle(leftbasepoint, rightbasepoint, bestpoint, testpoint) > 0.0) {\r
- triedgecopy(testtri, besttri);\r
- bestpoint = testpoint;\r
- bestnumber = i;\r
- }\r
- }\r
- if (verbose > 2) {\r
- printf(" Connecting edge to (%.12g, %.12g)\n", bestpoint[0],\r
- bestpoint[1]);\r
- }\r
- if (bestnumber > 1) {\r
- /* Recursively triangulate the smaller polygon on the right. */\r
- oprev(besttri, tempedge);\r
- triangulatepolygon(firstedge, &tempedge, bestnumber + 1, 1, triflaws);\r
- }\r
- if (bestnumber < edgecount - 2) {\r
- /* Recursively triangulate the smaller polygon on the left. */\r
- sym(besttri, tempedge);\r
- triangulatepolygon(&besttri, lastedge, edgecount - bestnumber, 1,\r
- triflaws);\r
- /* Find `besttri' again; it may have been lost to edge flips. */\r
- sym(tempedge, besttri);\r
- }\r
- if (doflip) {\r
- /* Do one final edge flip. */\r
- flip(&besttri);\r
-#ifndef CDT_ONLY\r
- if (triflaws) {\r
- /* Check the quality of the newly committed triangle. */\r
- sym(besttri, testtri);\r
- testtriangle(&testtri);\r
- }\r
-#endif /* not CDT_ONLY */\r
- }\r
- /* Return the base triangle. */\r
- triedgecopy(besttri, *lastedge);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* deletesite() Delete a vertex from a Delaunay triangulation, ensuring */\r
-/* that the triangulation remains Delaunay. */\r
-/* */\r
-/* The origin of `deltri' is deleted. The union of the triangles adjacent */\r
-/* to this point is a polygon, for which the Delaunay triangulation is */\r
-/* found. Two triangles are removed from the mesh. */\r
-/* */\r
-/* Only interior points that do not lie on segments (shell edges) or */\r
-/* boundaries may be deleted. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void deletesite(deltri)\r
-struct triedge *deltri;\r
-{\r
- struct triedge countingtri;\r
- struct triedge firstedge, lastedge;\r
- struct triedge deltriright;\r
- struct triedge lefttri, righttri;\r
- struct triedge leftcasing, rightcasing;\r
- struct edge leftshelle, rightshelle;\r
- point delpoint;\r
- point neworg;\r
- int edgecount;\r
- triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- org(*deltri, delpoint);\r
- if (verbose > 1) {\r
- printf(" Deleting (%.12g, %.12g).\n", delpoint[0], delpoint[1]);\r
- }\r
- pointdealloc(delpoint);\r
-\r
- /* Count the degree of the point being deleted. */\r
- onext(*deltri, countingtri);\r
- edgecount = 1;\r
- while (!triedgeequal(*deltri, countingtri)) {\r
-#ifdef SELF_CHECK\r
- if (countingtri.tri == dummytri) {\r
- printf("Internal error in deletesite():\n");\r
- printf(" Attempt to delete boundary point.\n");\r
- internalerror();\r
- }\r
-#endif /* SELF_CHECK */\r
- edgecount++;\r
- onextself(countingtri);\r
- }\r
-\r
-#ifdef SELF_CHECK\r
- if (edgecount < 3) {\r
- printf("Internal error in deletesite():\n Point has degree %d.\n",\r
- edgecount);\r
- internalerror();\r
- }\r
-#endif /* SELF_CHECK */\r
- if (edgecount > 3) {\r
- /* Triangulate the polygon defined by the union of all triangles */\r
- /* adjacent to the point being deleted. Check the quality of */\r
- /* the resulting triangles. */\r
- onext(*deltri, firstedge);\r
- oprev(*deltri, lastedge);\r
- triangulatepolygon(&firstedge, &lastedge, edgecount, 0, !nobisect);\r
- }\r
- /* Splice out two triangles. */\r
- lprev(*deltri, deltriright);\r
- dnext(*deltri, lefttri);\r
- sym(lefttri, leftcasing);\r
- oprev(deltriright, righttri);\r
- sym(righttri, rightcasing);\r
- bond(*deltri, leftcasing);\r
- bond(deltriright, rightcasing);\r
- tspivot(lefttri, leftshelle);\r
- if (leftshelle.sh != dummysh) {\r
- tsbond(*deltri, leftshelle);\r
- }\r
- tspivot(righttri, rightshelle);\r
- if (rightshelle.sh != dummysh) {\r
- tsbond(deltriright, rightshelle);\r
- }\r
-\r
- /* Set the new origin of `deltri' and check its quality. */\r
- org(lefttri, neworg);\r
- setorg(*deltri, neworg);\r
- if (!nobisect) {\r
- testtriangle(deltri);\r
- }\r
-\r
- /* Delete the two spliced-out triangles. */\r
- triangledealloc(lefttri.tri);\r
- triangledealloc(righttri.tri);\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/** **/\r
-/** **/\r
-/********* Mesh transformation routines end here *********/\r
-\r
-/********* Divide-and-conquer Delaunay triangulation begins here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* The divide-and-conquer bounding box */\r
-/* */\r
-/* I originally implemented the divide-and-conquer and incremental Delaunay */\r
-/* triangulations using the edge-based data structure presented by Guibas */\r
-/* and Stolfi. Switching to a triangle-based data structure doubled the */\r
-/* speed. However, I had to think of a few extra tricks to maintain the */\r
-/* elegance of the original algorithms. */\r
-/* */\r
-/* The "bounding box" used by my variant of the divide-and-conquer */\r
-/* algorithm uses one triangle for each edge of the convex hull of the */\r
-/* triangulation. These bounding triangles all share a common apical */\r
-/* vertex, which is represented by NULL and which represents nothing. */\r
-/* The bounding triangles are linked in a circular fan about this NULL */\r
-/* vertex, and the edges on the convex hull of the triangulation appear */\r
-/* opposite the NULL vertex. You might find it easiest to imagine that */\r
-/* the NULL vertex is a point in 3D space behind the center of the */\r
-/* triangulation, and that the bounding triangles form a sort of cone. */\r
-/* */\r
-/* This bounding box makes it easy to represent degenerate cases. For */\r
-/* instance, the triangulation of two vertices is a single edge. This edge */\r
-/* is represented by two bounding box triangles, one on each "side" of the */\r
-/* edge. These triangles are also linked together in a fan about the NULL */\r
-/* vertex. */\r
-/* */\r
-/* The bounding box also makes it easy to traverse the convex hull, as the */\r
-/* divide-and-conquer algorithm needs to do. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* pointsort() Sort an array of points by x-coordinate, using the */\r
-/* y-coordinate as a secondary key. */\r
-/* */\r
-/* Uses quicksort. Randomized O(n log n) time. No, I did not make any of */\r
-/* the usual quicksort mistakes. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void pointsort(sortarray, arraysize)\r
-point *sortarray;\r
-int arraysize;\r
-{\r
- int left, right;\r
- int pivot;\r
- REAL pivotx, pivoty;\r
- point temp;\r
-\r
- if (arraysize == 2) {\r
- /* Recursive base case. */\r
- if ((sortarray[0][0] > sortarray[1][0]) ||\r
- ((sortarray[0][0] == sortarray[1][0]) &&\r
- (sortarray[0][1] > sortarray[1][1]))) {\r
- temp = sortarray[1];\r
- sortarray[1] = sortarray[0];\r
- sortarray[0] = temp;\r
- }\r
- return;\r
- }\r
- /* Choose a random pivot to split the array. */\r
- pivot = (int) randomnation(arraysize);\r
- pivotx = sortarray[pivot][0];\r
- pivoty = sortarray[pivot][1];\r
- /* Split the array. */\r
- left = -1;\r
- right = arraysize;\r
- while (left < right) {\r
- /* Search for a point whose x-coordinate is too large for the left. */\r
- do {\r
- left++;\r
- } while ((left <= right) && ((sortarray[left][0] < pivotx) ||\r
- ((sortarray[left][0] == pivotx) &&\r
- (sortarray[left][1] < pivoty))));\r
- /* Search for a point whose x-coordinate is too small for the right. */\r
- do {\r
- right--;\r
- } while ((left <= right) && ((sortarray[right][0] > pivotx) ||\r
- ((sortarray[right][0] == pivotx) &&\r
- (sortarray[right][1] > pivoty))));\r
- if (left < right) {\r
- /* Swap the left and right points. */\r
- temp = sortarray[left];\r
- sortarray[left] = sortarray[right];\r
- sortarray[right] = temp;\r
- }\r
- }\r
- if (left > 1) {\r
- /* Recursively sort the left subset. */\r
- pointsort(sortarray, left);\r
- }\r
- if (right < arraysize - 2) {\r
- /* Recursively sort the right subset. */\r
- pointsort(&sortarray[right + 1], arraysize - right - 1);\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* pointmedian() An order statistic algorithm, almost. Shuffles an array */\r
-/* of points so that the first `median' points occur */\r
-/* lexicographically before the remaining points. */\r
-/* */\r
-/* Uses the x-coordinate as the primary key if axis == 0; the y-coordinate */\r
-/* if axis == 1. Very similar to the pointsort() procedure, but runs in */\r
-/* randomized linear time. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void pointmedian(sortarray, arraysize, median, axis)\r
-point *sortarray;\r
-int arraysize;\r
-int median;\r
-int axis;\r
-{\r
- int left, right;\r
- int pivot;\r
- REAL pivot1, pivot2;\r
- point temp;\r
-\r
- if (arraysize == 2) {\r
- /* Recursive base case. */\r
- if ((sortarray[0][axis] > sortarray[1][axis]) ||\r
- ((sortarray[0][axis] == sortarray[1][axis]) &&\r
- (sortarray[0][1 - axis] > sortarray[1][1 - axis]))) {\r
- temp = sortarray[1];\r
- sortarray[1] = sortarray[0];\r
- sortarray[0] = temp;\r
- }\r
- return;\r
- }\r
- /* Choose a random pivot to split the array. */\r
- pivot = (int) randomnation(arraysize);\r
- pivot1 = sortarray[pivot][axis];\r
- pivot2 = sortarray[pivot][1 - axis];\r
- /* Split the array. */\r
- left = -1;\r
- right = arraysize;\r
- while (left < right) {\r
- /* Search for a point whose x-coordinate is too large for the left. */\r
- do {\r
- left++;\r
- } while ((left <= right) && ((sortarray[left][axis] < pivot1) ||\r
- ((sortarray[left][axis] == pivot1) &&\r
- (sortarray[left][1 - axis] < pivot2))));\r
- /* Search for a point whose x-coordinate is too small for the right. */\r
- do {\r
- right--;\r
- } while ((left <= right) && ((sortarray[right][axis] > pivot1) ||\r
- ((sortarray[right][axis] == pivot1) &&\r
- (sortarray[right][1 - axis] > pivot2))));\r
- if (left < right) {\r
- /* Swap the left and right points. */\r
- temp = sortarray[left];\r
- sortarray[left] = sortarray[right];\r
- sortarray[right] = temp;\r
- }\r
- }\r
- /* Unlike in pointsort(), at most one of the following */\r
- /* conditionals is true. */\r
- if (left > median) {\r
- /* Recursively shuffle the left subset. */\r
- pointmedian(sortarray, left, median, axis);\r
- }\r
- if (right < median - 1) {\r
- /* Recursively shuffle the right subset. */\r
- pointmedian(&sortarray[right + 1], arraysize - right - 1,\r
- median - right - 1, axis);\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* alternateaxes() Sorts the points as appropriate for the divide-and- */\r
-/* conquer algorithm with alternating cuts. */\r
-/* */\r
-/* Partitions by x-coordinate if axis == 0; by y-coordinate if axis == 1. */\r
-/* For the base case, subsets containing only two or three points are */\r
-/* always sorted by x-coordinate. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void alternateaxes(sortarray, arraysize, axis)\r
-point *sortarray;\r
-int arraysize;\r
-int axis;\r
-{\r
- int divider;\r
-\r
- divider = arraysize >> 1;\r
- if (arraysize <= 3) {\r
- /* Recursive base case: subsets of two or three points will be */\r
- /* handled specially, and should always be sorted by x-coordinate. */\r
- axis = 0;\r
- }\r
- /* Partition with a horizontal or vertical cut. */\r
- pointmedian(sortarray, arraysize, divider, axis);\r
- /* Recursively partition the subsets with a cross cut. */\r
- if (arraysize - divider >= 2) {\r
- if (divider >= 2) {\r
- alternateaxes(sortarray, divider, 1 - axis);\r
- }\r
- alternateaxes(&sortarray[divider], arraysize - divider, 1 - axis);\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* mergehulls() Merge two adjacent Delaunay triangulations into a */\r
-/* single Delaunay triangulation. */\r
-/* */\r
-/* This is similar to the algorithm given by Guibas and Stolfi, but uses */\r
-/* a triangle-based, rather than edge-based, data structure. */\r
-/* */\r
-/* The algorithm walks up the gap between the two triangulations, knitting */\r
-/* them together. As they are merged, some of their bounding triangles */\r
-/* are converted into real triangles of the triangulation. The procedure */\r
-/* pulls each hull's bounding triangles apart, then knits them together */\r
-/* like the teeth of two gears. The Delaunay property determines, at each */\r
-/* step, whether the next "tooth" is a bounding triangle of the left hull */\r
-/* or the right. When a bounding triangle becomes real, its apex is */\r
-/* changed from NULL to a real point. */\r
-/* */\r
-/* Only two new triangles need to be allocated. These become new bounding */\r
-/* triangles at the top and bottom of the seam. They are used to connect */\r
-/* the remaining bounding triangles (those that have not been converted */\r
-/* into real triangles) into a single fan. */\r
-/* */\r
-/* On entry, `farleft' and `innerleft' are bounding triangles of the left */\r
-/* triangulation. The origin of `farleft' is the leftmost vertex, and */\r
-/* the destination of `innerleft' is the rightmost vertex of the */\r
-/* triangulation. Similarly, `innerright' and `farright' are bounding */\r
-/* triangles of the right triangulation. The origin of `innerright' and */\r
-/* destination of `farright' are the leftmost and rightmost vertices. */\r
-/* */\r
-/* On completion, the origin of `farleft' is the leftmost vertex of the */\r
-/* merged triangulation, and the destination of `farright' is the rightmost */\r
-/* vertex. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void mergehulls(farleft, innerleft, innerright, farright, axis)\r
-struct triedge *farleft;\r
-struct triedge *innerleft;\r
-struct triedge *innerright;\r
-struct triedge *farright;\r
-int axis;\r
-{\r
- struct triedge leftcand, rightcand;\r
- struct triedge baseedge;\r
- struct triedge nextedge;\r
- struct triedge sidecasing, topcasing, outercasing;\r
- struct triedge checkedge;\r
- point innerleftdest;\r
- point innerrightorg;\r
- point innerleftapex, innerrightapex;\r
- point farleftpt, farrightpt;\r
- point farleftapex, farrightapex;\r
- point lowerleft, lowerright;\r
- point upperleft, upperright;\r
- point nextapex;\r
- point checkvertex;\r
- int changemade;\r
- int badedge;\r
- int leftfinished, rightfinished;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- dest(*innerleft, innerleftdest);\r
- apex(*innerleft, innerleftapex);\r
- org(*innerright, innerrightorg);\r
- apex(*innerright, innerrightapex);\r
- /* Special treatment for horizontal cuts. */\r
- if (dwyer && (axis == 1)) {\r
- org(*farleft, farleftpt);\r
- apex(*farleft, farleftapex);\r
- dest(*farright, farrightpt);\r
- apex(*farright, farrightapex);\r
- /* The pointers to the extremal points are shifted to point to the */\r
- /* topmost and bottommost point of each hull, rather than the */\r
- /* leftmost and rightmost points. */\r
- while (farleftapex[1] < farleftpt[1]) {\r
- lnextself(*farleft);\r
- symself(*farleft);\r
- farleftpt = farleftapex;\r
- apex(*farleft, farleftapex);\r
- }\r
- sym(*innerleft, checkedge);\r
- apex(checkedge, checkvertex);\r
- while (checkvertex[1] > innerleftdest[1]) {\r
- lnext(checkedge, *innerleft);\r
- innerleftapex = innerleftdest;\r
- innerleftdest = checkvertex;\r
- sym(*innerleft, checkedge);\r
- apex(checkedge, checkvertex);\r
- }\r
- while (innerrightapex[1] < innerrightorg[1]) {\r
- lnextself(*innerright);\r
- symself(*innerright);\r
- innerrightorg = innerrightapex;\r
- apex(*innerright, innerrightapex);\r
- }\r
- sym(*farright, checkedge);\r
- apex(checkedge, checkvertex);\r
- while (checkvertex[1] > farrightpt[1]) {\r
- lnext(checkedge, *farright);\r
- farrightapex = farrightpt;\r
- farrightpt = checkvertex;\r
- sym(*farright, checkedge);\r
- apex(checkedge, checkvertex);\r
- }\r
- }\r
- /* Find a line tangent to and below both hulls. */\r
- do {\r
- changemade = 0;\r
- /* Make innerleftdest the "bottommost" point of the left hull. */\r
- if (counterclockwise(innerleftdest, innerleftapex, innerrightorg) > 0.0) {\r
- lprevself(*innerleft);\r
- symself(*innerleft);\r
- innerleftdest = innerleftapex;\r
- apex(*innerleft, innerleftapex);\r
- changemade = 1;\r
- }\r
- /* Make innerrightorg the "bottommost" point of the right hull. */\r
- if (counterclockwise(innerrightapex, innerrightorg, innerleftdest) > 0.0) {\r
- lnextself(*innerright);\r
- symself(*innerright);\r
- innerrightorg = innerrightapex;\r
- apex(*innerright, innerrightapex);\r
- changemade = 1;\r
- }\r
- } while (changemade);\r
- /* Find the two candidates to be the next "gear tooth". */\r
- sym(*innerleft, leftcand);\r
- sym(*innerright, rightcand);\r
- /* Create the bottom new bounding triangle. */\r
- maketriangle(&baseedge);\r
- /* Connect it to the bounding boxes of the left and right triangulations. */\r
- bond(baseedge, *innerleft);\r
- lnextself(baseedge);\r
- bond(baseedge, *innerright);\r
- lnextself(baseedge);\r
- setorg(baseedge, innerrightorg);\r
- setdest(baseedge, innerleftdest);\r
- /* Apex is intentionally left NULL. */\r
- if (verbose > 2) {\r
- printf(" Creating base bounding ");\r
- printtriangle(&baseedge);\r
- }\r
- /* Fix the extreme triangles if necessary. */\r
- org(*farleft, farleftpt);\r
- if (innerleftdest == farleftpt) {\r
- lnext(baseedge, *farleft);\r
- }\r
- dest(*farright, farrightpt);\r
- if (innerrightorg == farrightpt) {\r
- lprev(baseedge, *farright);\r
- }\r
- /* The vertices of the current knitting edge. */\r
- lowerleft = innerleftdest;\r
- lowerright = innerrightorg;\r
- /* The candidate vertices for knitting. */\r
- apex(leftcand, upperleft);\r
- apex(rightcand, upperright);\r
- /* Walk up the gap between the two triangulations, knitting them together. */\r
- while (1) {\r
- /* Have we reached the top? (This isn't quite the right question, */\r
- /* because even though the left triangulation might seem finished now, */\r
- /* moving up on the right triangulation might reveal a new point of */\r
- /* the left triangulation. And vice-versa.) */\r
- leftfinished = counterclockwise(upperleft, lowerleft, lowerright) <= 0.0;\r
- rightfinished = counterclockwise(upperright, lowerleft, lowerright) <= 0.0;\r
- if (leftfinished && rightfinished) {\r
- /* Create the top new bounding triangle. */\r
- maketriangle(&nextedge);\r
- setorg(nextedge, lowerleft);\r
- setdest(nextedge, lowerright);\r
- /* Apex is intentionally left NULL. */\r
- /* Connect it to the bounding boxes of the two triangulations. */\r
- bond(nextedge, baseedge);\r
- lnextself(nextedge);\r
- bond(nextedge, rightcand);\r
- lnextself(nextedge);\r
- bond(nextedge, leftcand);\r
- if (verbose > 2) {\r
- printf(" Creating top bounding ");\r
- printtriangle(&baseedge);\r
- }\r
- /* Special treatment for horizontal cuts. */\r
- if (dwyer && (axis == 1)) {\r
- org(*farleft, farleftpt);\r
- apex(*farleft, farleftapex);\r
- dest(*farright, farrightpt);\r
- apex(*farright, farrightapex);\r
- sym(*farleft, checkedge);\r
- apex(checkedge, checkvertex);\r
- /* The pointers to the extremal points are restored to the leftmost */\r
- /* and rightmost points (rather than topmost and bottommost). */\r
- while (checkvertex[0] < farleftpt[0]) {\r
- lprev(checkedge, *farleft);\r
- farleftapex = farleftpt;\r
- farleftpt = checkvertex;\r
- sym(*farleft, checkedge);\r
- apex(checkedge, checkvertex);\r
- }\r
- while (farrightapex[0] > farrightpt[0]) {\r
- lprevself(*farright);\r
- symself(*farright);\r
- farrightpt = farrightapex;\r
- apex(*farright, farrightapex);\r
- }\r
- }\r
- return;\r
- }\r
- /* Consider eliminating edges from the left triangulation. */\r
- if (!leftfinished) {\r
- /* What vertex would be exposed if an edge were deleted? */\r
- lprev(leftcand, nextedge);\r
- symself(nextedge);\r
- apex(nextedge, nextapex);\r
- /* If nextapex is NULL, then no vertex would be exposed; the */\r
- /* triangulation would have been eaten right through. */\r
- if (nextapex != (point) NULL) {\r
- /* Check whether the edge is Delaunay. */\r
- badedge = incircle(lowerleft, lowerright, upperleft, nextapex) > 0.0;\r
- while (badedge) {\r
- /* Eliminate the edge with an edge flip. As a result, the */\r
- /* left triangulation will have one more boundary triangle. */\r
- lnextself(nextedge);\r
- sym(nextedge, topcasing);\r
- lnextself(nextedge);\r
- sym(nextedge, sidecasing);\r
- bond(nextedge, topcasing);\r
- bond(leftcand, sidecasing);\r
- lnextself(leftcand);\r
- sym(leftcand, outercasing);\r
- lprevself(nextedge);\r
- bond(nextedge, outercasing);\r
- /* Correct the vertices to reflect the edge flip. */\r
- setorg(leftcand, lowerleft);\r
- setdest(leftcand, NULL);\r
- setapex(leftcand, nextapex);\r
- setorg(nextedge, NULL);\r
- setdest(nextedge, upperleft);\r
- setapex(nextedge, nextapex);\r
- /* Consider the newly exposed vertex. */\r
- upperleft = nextapex;\r
- /* What vertex would be exposed if another edge were deleted? */\r
- triedgecopy(sidecasing, nextedge);\r
- apex(nextedge, nextapex);\r
- if (nextapex != (point) NULL) {\r
- /* Check whether the edge is Delaunay. */\r
- badedge = incircle(lowerleft, lowerright, upperleft, nextapex)\r
- > 0.0;\r
- } else {\r
- /* Avoid eating right through the triangulation. */\r
- badedge = 0;\r
- }\r
- }\r
- }\r
- }\r
- /* Consider eliminating edges from the right triangulation. */\r
- if (!rightfinished) {\r
- /* What vertex would be exposed if an edge were deleted? */\r
- lnext(rightcand, nextedge);\r
- symself(nextedge);\r
- apex(nextedge, nextapex);\r
- /* If nextapex is NULL, then no vertex would be exposed; the */\r
- /* triangulation would have been eaten right through. */\r
- if (nextapex != (point) NULL) {\r
- /* Check whether the edge is Delaunay. */\r
- badedge = incircle(lowerleft, lowerright, upperright, nextapex) > 0.0;\r
- while (badedge) {\r
- /* Eliminate the edge with an edge flip. As a result, the */\r
- /* right triangulation will have one more boundary triangle. */\r
- lprevself(nextedge);\r
- sym(nextedge, topcasing);\r
- lprevself(nextedge);\r
- sym(nextedge, sidecasing);\r
- bond(nextedge, topcasing);\r
- bond(rightcand, sidecasing);\r
- lprevself(rightcand);\r
- sym(rightcand, outercasing);\r
- lnextself(nextedge);\r
- bond(nextedge, outercasing);\r
- /* Correct the vertices to reflect the edge flip. */\r
- setorg(rightcand, NULL);\r
- setdest(rightcand, lowerright);\r
- setapex(rightcand, nextapex);\r
- setorg(nextedge, upperright);\r
- setdest(nextedge, NULL);\r
- setapex(nextedge, nextapex);\r
- /* Consider the newly exposed vertex. */\r
- upperright = nextapex;\r
- /* What vertex would be exposed if another edge were deleted? */\r
- triedgecopy(sidecasing, nextedge);\r
- apex(nextedge, nextapex);\r
- if (nextapex != (point) NULL) {\r
- /* Check whether the edge is Delaunay. */\r
- badedge = incircle(lowerleft, lowerright, upperright, nextapex)\r
- > 0.0;\r
- } else {\r
- /* Avoid eating right through the triangulation. */\r
- badedge = 0;\r
- }\r
- }\r
- }\r
- }\r
- if (leftfinished || (!rightfinished &&\r
- (incircle(upperleft, lowerleft, lowerright, upperright) > 0.0))) {\r
- /* Knit the triangulations, adding an edge from `lowerleft' */\r
- /* to `upperright'. */\r
- bond(baseedge, rightcand);\r
- lprev(rightcand, baseedge);\r
- setdest(baseedge, lowerleft);\r
- lowerright = upperright;\r
- sym(baseedge, rightcand);\r
- apex(rightcand, upperright);\r
- } else {\r
- /* Knit the triangulations, adding an edge from `upperleft' */\r
- /* to `lowerright'. */\r
- bond(baseedge, leftcand);\r
- lnext(leftcand, baseedge);\r
- setorg(baseedge, lowerright);\r
- lowerleft = upperleft;\r
- sym(baseedge, leftcand);\r
- apex(leftcand, upperleft);\r
- }\r
- if (verbose > 2) {\r
- printf(" Connecting ");\r
- printtriangle(&baseedge);\r
- }\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* divconqrecurse() Recursively form a Delaunay triangulation by the */\r
-/* divide-and-conquer method. */\r
-/* */\r
-/* Recursively breaks down the problem into smaller pieces, which are */\r
-/* knitted together by mergehulls(). The base cases (problems of two or */\r
-/* three points) are handled specially here. */\r
-/* */\r
-/* On completion, `farleft' and `farright' are bounding triangles such that */\r
-/* the origin of `farleft' is the leftmost vertex (breaking ties by */\r
-/* choosing the highest leftmost vertex), and the destination of */\r
-/* `farright' is the rightmost vertex (breaking ties by choosing the */\r
-/* lowest rightmost vertex). */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void divconqrecurse(sortarray, vertices, axis, farleft, farright)\r
-point *sortarray;\r
-int vertices;\r
-int axis;\r
-struct triedge *farleft;\r
-struct triedge *farright;\r
-{\r
- struct triedge midtri, tri1, tri2, tri3;\r
- struct triedge innerleft, innerright;\r
- REAL area;\r
- int divider;\r
-\r
- if (verbose > 2) {\r
- printf(" Triangulating %d points.\n", vertices);\r
- }\r
- if (vertices == 2) {\r
- /* The triangulation of two vertices is an edge. An edge is */\r
- /* represented by two bounding triangles. */\r
- maketriangle(farleft);\r
- setorg(*farleft, sortarray[0]);\r
- setdest(*farleft, sortarray[1]);\r
- /* The apex is intentionally left NULL. */\r
- maketriangle(farright);\r
- setorg(*farright, sortarray[1]);\r
- setdest(*farright, sortarray[0]);\r
- /* The apex is intentionally left NULL. */\r
- bond(*farleft, *farright);\r
- lprevself(*farleft);\r
- lnextself(*farright);\r
- bond(*farleft, *farright);\r
- lprevself(*farleft);\r
- lnextself(*farright);\r
- bond(*farleft, *farright);\r
- if (verbose > 2) {\r
- printf(" Creating ");\r
- printtriangle(farleft);\r
- printf(" Creating ");\r
- printtriangle(farright);\r
- }\r
- /* Ensure that the origin of `farleft' is sortarray[0]. */\r
- lprev(*farright, *farleft);\r
- return;\r
- } else if (vertices == 3) {\r
- /* The triangulation of three vertices is either a triangle (with */\r
- /* three bounding triangles) or two edges (with four bounding */\r
- /* triangles). In either case, four triangles are created. */\r
- maketriangle(&midtri);\r
- maketriangle(&tri1);\r
- maketriangle(&tri2);\r
- maketriangle(&tri3);\r
- area = counterclockwise(sortarray[0], sortarray[1], sortarray[2]);\r
- if (area == 0.0) {\r
- /* Three collinear points; the triangulation is two edges. */\r
- setorg(midtri, sortarray[0]);\r
- setdest(midtri, sortarray[1]);\r
- setorg(tri1, sortarray[1]);\r
- setdest(tri1, sortarray[0]);\r
- setorg(tri2, sortarray[2]);\r
- setdest(tri2, sortarray[1]);\r
- setorg(tri3, sortarray[1]);\r
- setdest(tri3, sortarray[2]);\r
- /* All apices are intentionally left NULL. */\r
- bond(midtri, tri1);\r
- bond(tri2, tri3);\r
- lnextself(midtri);\r
- lprevself(tri1);\r
- lnextself(tri2);\r
- lprevself(tri3);\r
- bond(midtri, tri3);\r
- bond(tri1, tri2);\r
- lnextself(midtri);\r
- lprevself(tri1);\r
- lnextself(tri2);\r
- lprevself(tri3);\r
- bond(midtri, tri1);\r
- bond(tri2, tri3);\r
- /* Ensure that the origin of `farleft' is sortarray[0]. */\r
- triedgecopy(tri1, *farleft);\r
- /* Ensure that the destination of `farright' is sortarray[2]. */\r
- triedgecopy(tri2, *farright);\r
- } else {\r
- /* The three points are not collinear; the triangulation is one */\r
- /* triangle, namely `midtri'. */\r
- setorg(midtri, sortarray[0]);\r
- setdest(tri1, sortarray[0]);\r
- setorg(tri3, sortarray[0]);\r
- /* Apices of tri1, tri2, and tri3 are left NULL. */\r
- if (area > 0.0) {\r
- /* The vertices are in counterclockwise order. */\r
- setdest(midtri, sortarray[1]);\r
- setorg(tri1, sortarray[1]);\r
- setdest(tri2, sortarray[1]);\r
- setapex(midtri, sortarray[2]);\r
- setorg(tri2, sortarray[2]);\r
- setdest(tri3, sortarray[2]);\r
- } else {\r
- /* The vertices are in clockwise order. */\r
- setdest(midtri, sortarray[2]);\r
- setorg(tri1, sortarray[2]);\r
- setdest(tri2, sortarray[2]);\r
- setapex(midtri, sortarray[1]);\r
- setorg(tri2, sortarray[1]);\r
- setdest(tri3, sortarray[1]);\r
- }\r
- /* The topology does not depend on how the vertices are ordered. */\r
- bond(midtri, tri1);\r
- lnextself(midtri);\r
- bond(midtri, tri2);\r
- lnextself(midtri);\r
- bond(midtri, tri3);\r
- lprevself(tri1);\r
- lnextself(tri2);\r
- bond(tri1, tri2);\r
- lprevself(tri1);\r
- lprevself(tri3);\r
- bond(tri1, tri3);\r
- lnextself(tri2);\r
- lprevself(tri3);\r
- bond(tri2, tri3);\r
- /* Ensure that the origin of `farleft' is sortarray[0]. */\r
- triedgecopy(tri1, *farleft);\r
- /* Ensure that the destination of `farright' is sortarray[2]. */\r
- if (area > 0.0) {\r
- triedgecopy(tri2, *farright);\r
- } else {\r
- lnext(*farleft, *farright);\r
- }\r
- }\r
- if (verbose > 2) {\r
- printf(" Creating ");\r
- printtriangle(&midtri);\r
- printf(" Creating ");\r
- printtriangle(&tri1);\r
- printf(" Creating ");\r
- printtriangle(&tri2);\r
- printf(" Creating ");\r
- printtriangle(&tri3);\r
- }\r
- return;\r
- } else {\r
- /* Split the vertices in half. */\r
- divider = vertices >> 1;\r
- /* Recursively triangulate each half. */\r
- divconqrecurse(sortarray, divider, 1 - axis, farleft, &innerleft);\r
- divconqrecurse(&sortarray[divider], vertices - divider, 1 - axis,\r
- &innerright, farright);\r
- if (verbose > 1) {\r
- printf(" Joining triangulations with %d and %d vertices.\n", divider,\r
- vertices - divider);\r
- }\r
- /* Merge the two triangulations into one. */\r
- mergehulls(farleft, &innerleft, &innerright, farright, axis);\r
- }\r
-}\r
-\r
-long removeghosts(startghost)\r
-struct triedge *startghost;\r
-{\r
- struct triedge searchedge;\r
- struct triedge dissolveedge;\r
- struct triedge deadtri;\r
- point markorg;\r
- long hullsize;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- if (verbose) {\r
- printf(" Removing ghost triangles.\n");\r
- }\r
- /* Find an edge on the convex hull to start point location from. */\r
- lprev(*startghost, searchedge);\r
- symself(searchedge);\r
- dummytri[0] = encode(searchedge);\r
- /* Remove the bounding box and count the convex hull edges. */\r
- triedgecopy(*startghost, dissolveedge);\r
- hullsize = 0;\r
- do {\r
- hullsize++;\r
- lnext(dissolveedge, deadtri);\r
- lprevself(dissolveedge);\r
- symself(dissolveedge);\r
- /* If no PSLG is involved, set the boundary markers of all the points */\r
- /* on the convex hull. If a PSLG is used, this step is done later. */\r
- if (!poly) {\r
- /* Watch out for the case where all the input points are collinear. */\r
- if (dissolveedge.tri != dummytri) {\r
- org(dissolveedge, markorg);\r
- if (pointmark(markorg) == 0) {\r
- setpointmark(markorg, 1);\r
- }\r
- }\r
- }\r
- /* Remove a bounding triangle from a convex hull triangle. */\r
- dissolve(dissolveedge);\r
- /* Find the next bounding triangle. */\r
- sym(deadtri, dissolveedge);\r
- /* Delete the bounding triangle. */\r
- triangledealloc(deadtri.tri);\r
- } while (!triedgeequal(dissolveedge, *startghost));\r
- return hullsize;\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* divconqdelaunay() Form a Delaunay triangulation by the divide-and- */\r
-/* conquer method. */\r
-/* */\r
-/* Sorts the points, calls a recursive procedure to triangulate them, and */\r
-/* removes the bounding box, setting boundary markers as appropriate. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-long divconqdelaunay()\r
-{\r
- point *sortarray;\r
- struct triedge hullleft, hullright;\r
- int divider;\r
- int i, j;\r
-\r
- /* Allocate an array of pointers to points for sorting. */\r
- sortarray = (point *) malloc(inpoints * sizeof(point));\r
- if (sortarray == (point *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- traversalinit(&points);\r
- for (i = 0; i < inpoints; i++) {\r
- sortarray[i] = pointtraverse();\r
- }\r
- if (verbose) {\r
- printf(" Sorting points.\n");\r
- }\r
- /* Sort the points. */\r
- pointsort(sortarray, inpoints);\r
- /* Discard duplicate points, which can really mess up the algorithm. */\r
- i = 0;\r
- for (j = 1; j < inpoints; j++) {\r
- if ((sortarray[i][0] == sortarray[j][0])\r
- && (sortarray[i][1] == sortarray[j][1])) {\r
- if (!quiet) {\r
- printf(\r
-"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",\r
- sortarray[j][0], sortarray[j][1]);\r
- }\r
-/* Commented out - would eliminate point from output .node file, but causes\r
- a failure if some segment has this point as an endpoint.\r
- setpointmark(sortarray[j], DEADPOINT);\r
-*/\r
- } else {\r
- i++;\r
- sortarray[i] = sortarray[j];\r
- }\r
- }\r
- i++;\r
- if (dwyer) {\r
- /* Re-sort the array of points to accommodate alternating cuts. */\r
- divider = i >> 1;\r
- if (i - divider >= 2) {\r
- if (divider >= 2) {\r
- alternateaxes(sortarray, divider, 1);\r
- }\r
- alternateaxes(&sortarray[divider], i - divider, 1);\r
- }\r
- }\r
- if (verbose) {\r
- printf(" Forming triangulation.\n");\r
- }\r
- /* Form the Delaunay triangulation. */\r
- divconqrecurse(sortarray, i, 0, &hullleft, &hullright);\r
- free(sortarray);\r
-\r
- return removeghosts(&hullleft);\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Divide-and-conquer Delaunay triangulation ends here *********/\r
-\r
-/********* Incremental Delaunay triangulation begins here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* boundingbox() Form an "infinite" bounding triangle to insert points */\r
-/* into. */\r
-/* */\r
-/* The points at "infinity" are assigned finite coordinates, which are used */\r
-/* by the point location routines, but (mostly) ignored by the Delaunay */\r
-/* edge flip routines. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef REDUCED\r
-\r
-void boundingbox()\r
-{\r
- struct triedge inftri; /* Handle for the triangular bounding box. */\r
- REAL width;\r
-\r
- if (verbose) {\r
- printf(" Creating triangular bounding box.\n");\r
- }\r
- /* Find the width (or height, whichever is larger) of the triangulation. */\r
- width = xmax - xmin;\r
- if (ymax - ymin > width) {\r
- width = ymax - ymin;\r
- }\r
- if (width == 0.0) {\r
- width = 1.0;\r
- }\r
- /* Create the vertices of the bounding box. */\r
- infpoint1 = (point) malloc(points.itembytes);\r
- infpoint2 = (point) malloc(points.itembytes);\r
- infpoint3 = (point) malloc(points.itembytes);\r
- if ((infpoint1 == (point) NULL) || (infpoint2 == (point) NULL)\r
- || (infpoint3 == (point) NULL)) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- infpoint1[0] = xmin - 50.0 * width;\r
- infpoint1[1] = ymin - 40.0 * width;\r
- infpoint2[0] = xmax + 50.0 * width;\r
- infpoint2[1] = ymin - 40.0 * width;\r
- infpoint3[0] = 0.5 * (xmin + xmax);\r
- infpoint3[1] = ymax + 60.0 * width;\r
-\r
- /* Create the bounding box. */\r
- maketriangle(&inftri);\r
- setorg(inftri, infpoint1);\r
- setdest(inftri, infpoint2);\r
- setapex(inftri, infpoint3);\r
- /* Link dummytri to the bounding box so we can always find an */\r
- /* edge to begin searching (point location) from. */\r
- dummytri[0] = (triangle) inftri.tri;\r
- if (verbose > 2) {\r
- printf(" Creating ");\r
- printtriangle(&inftri);\r
- }\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* removebox() Remove the "infinite" bounding triangle, setting boundary */\r
-/* markers as appropriate. */\r
-/* */\r
-/* The triangular bounding box has three boundary triangles (one for each */\r
-/* side of the bounding box), and a bunch of triangles fanning out from */\r
-/* the three bounding box vertices (one triangle for each edge of the */\r
-/* convex hull of the inner mesh). This routine removes these triangles. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef REDUCED\r
-\r
-long removebox()\r
-{\r
- struct triedge deadtri;\r
- struct triedge searchedge;\r
- struct triedge checkedge;\r
- struct triedge nextedge, finaledge, dissolveedge;\r
- point markorg;\r
- long hullsize;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- if (verbose) {\r
- printf(" Removing triangular bounding box.\n");\r
- }\r
- /* Find a boundary triangle. */\r
- nextedge.tri = dummytri;\r
- nextedge.orient = 0;\r
- symself(nextedge);\r
- /* Mark a place to stop. */\r
- lprev(nextedge, finaledge);\r
- lnextself(nextedge);\r
- symself(nextedge);\r
- /* Find a triangle (on the boundary of the point set) that isn't */\r
- /* a bounding box triangle. */\r
- lprev(nextedge, searchedge);\r
- symself(searchedge);\r
- /* Check whether nextedge is another boundary triangle */\r
- /* adjacent to the first one. */\r
- lnext(nextedge, checkedge);\r
- symself(checkedge);\r
- if (checkedge.tri == dummytri) {\r
- /* Go on to the next triangle. There are only three boundary */\r
- /* triangles, and this next triangle cannot be the third one, */\r
- /* so it's safe to stop here. */\r
- lprevself(searchedge);\r
- symself(searchedge);\r
- }\r
- /* Find a new boundary edge to search from, as the current search */\r
- /* edge lies on a bounding box triangle and will be deleted. */\r
- dummytri[0] = encode(searchedge);\r
- hullsize = -2l;\r
- while (!triedgeequal(nextedge, finaledge)) {\r
- hullsize++;\r
- lprev(nextedge, dissolveedge);\r
- symself(dissolveedge);\r
- /* If not using a PSLG, the vertices should be marked now. */\r
- /* (If using a PSLG, markhull() will do the job.) */\r
- if (!poly) {\r
- /* Be careful! One must check for the case where all the input */\r
- /* points are collinear, and thus all the triangles are part of */\r
- /* the bounding box. Otherwise, the setpointmark() call below */\r
- /* will cause a bad pointer reference. */\r
- if (dissolveedge.tri != dummytri) {\r
- org(dissolveedge, markorg);\r
- if (pointmark(markorg) == 0) {\r
- setpointmark(markorg, 1);\r
- }\r
- }\r
- }\r
- /* Disconnect the bounding box triangle from the mesh triangle. */\r
- dissolve(dissolveedge);\r
- lnext(nextedge, deadtri);\r
- sym(deadtri, nextedge);\r
- /* Get rid of the bounding box triangle. */\r
- triangledealloc(deadtri.tri);\r
- /* Do we need to turn the corner? */\r
- if (nextedge.tri == dummytri) {\r
- /* Turn the corner. */\r
- triedgecopy(dissolveedge, nextedge);\r
- }\r
- }\r
- triangledealloc(finaledge.tri);\r
-\r
- free(infpoint1); /* Deallocate the bounding box vertices. */\r
- free(infpoint2);\r
- free(infpoint3);\r
-\r
- return hullsize;\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* incrementaldelaunay() Form a Delaunay triangulation by incrementally */\r
-/* adding vertices. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef REDUCED\r
-\r
-long incrementaldelaunay()\r
-{\r
- struct triedge starttri;\r
- point pointloop;\r
- int i;\r
-\r
- /* Create a triangular bounding box. */\r
- boundingbox();\r
- if (verbose) {\r
- printf(" Incrementally inserting points.\n");\r
- }\r
- traversalinit(&points);\r
- pointloop = pointtraverse();\r
- i = 1;\r
- while (pointloop != (point) NULL) {\r
- /* Find a boundary triangle to search from. */\r
- starttri.tri = (triangle *) NULL;\r
- if (insertsite(pointloop, &starttri, (struct edge *) NULL, 0, 0) ==\r
- DUPLICATEPOINT) {\r
- if (!quiet) {\r
- printf(\r
-"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",\r
- pointloop[0], pointloop[1]);\r
- }\r
-/* Commented out - would eliminate point from output .node file.\r
- setpointmark(pointloop, DEADPOINT);\r
-*/\r
- }\r
- pointloop = pointtraverse();\r
- i++;\r
- }\r
- /* Remove the bounding box. */\r
- return removebox();\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-/** **/\r
-/** **/\r
-/********* Incremental Delaunay triangulation ends here *********/\r
-\r
-/********* Sweepline Delaunay triangulation begins here *********/\r
-/** **/\r
-/** **/\r
-\r
-#ifndef REDUCED\r
-\r
-void eventheapinsert(heap, heapsize, newevent)\r
-struct event **heap;\r
-int heapsize;\r
-struct event *newevent;\r
-{\r
- REAL eventx, eventy;\r
- int eventnum;\r
- int parent;\r
- int notdone;\r
-\r
- eventx = newevent->xkey;\r
- eventy = newevent->ykey;\r
- eventnum = heapsize;\r
- notdone = eventnum > 0;\r
- while (notdone) {\r
- parent = (eventnum - 1) >> 1;\r
- if ((heap[parent]->ykey < eventy) ||\r
- ((heap[parent]->ykey == eventy)\r
- && (heap[parent]->xkey <= eventx))) {\r
- notdone = 0;\r
- } else {\r
- heap[eventnum] = heap[parent];\r
- heap[eventnum]->heapposition = eventnum;\r
-\r
- eventnum = parent;\r
- notdone = eventnum > 0;\r
- }\r
- }\r
- heap[eventnum] = newevent;\r
- newevent->heapposition = eventnum;\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-void eventheapify(heap, heapsize, eventnum)\r
-struct event **heap;\r
-int heapsize;\r
-int eventnum;\r
-{\r
- struct event *thisevent;\r
- REAL eventx, eventy;\r
- int leftchild, rightchild;\r
- int smallest;\r
- int notdone;\r
-\r
- thisevent = heap[eventnum];\r
- eventx = thisevent->xkey;\r
- eventy = thisevent->ykey;\r
- leftchild = 2 * eventnum + 1;\r
- notdone = leftchild < heapsize;\r
- while (notdone) {\r
- if ((heap[leftchild]->ykey < eventy) ||\r
- ((heap[leftchild]->ykey == eventy)\r
- && (heap[leftchild]->xkey < eventx))) {\r
- smallest = leftchild;\r
- } else {\r
- smallest = eventnum;\r
- }\r
- rightchild = leftchild + 1;\r
- if (rightchild < heapsize) {\r
- if ((heap[rightchild]->ykey < heap[smallest]->ykey) ||\r
- ((heap[rightchild]->ykey == heap[smallest]->ykey)\r
- && (heap[rightchild]->xkey < heap[smallest]->xkey))) {\r
- smallest = rightchild;\r
- }\r
- }\r
- if (smallest == eventnum) {\r
- notdone = 0;\r
- } else {\r
- heap[eventnum] = heap[smallest];\r
- heap[eventnum]->heapposition = eventnum;\r
- heap[smallest] = thisevent;\r
- thisevent->heapposition = smallest;\r
-\r
- eventnum = smallest;\r
- leftchild = 2 * eventnum + 1;\r
- notdone = leftchild < heapsize;\r
- }\r
- }\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-void eventheapdelete(heap, heapsize, eventnum)\r
-struct event **heap;\r
-int heapsize;\r
-int eventnum;\r
-{\r
- struct event *moveevent;\r
- REAL eventx, eventy;\r
- int parent;\r
- int notdone;\r
-\r
- moveevent = heap[heapsize - 1];\r
- if (eventnum > 0) {\r
- eventx = moveevent->xkey;\r
- eventy = moveevent->ykey;\r
- do {\r
- parent = (eventnum - 1) >> 1;\r
- if ((heap[parent]->ykey < eventy) ||\r
- ((heap[parent]->ykey == eventy)\r
- && (heap[parent]->xkey <= eventx))) {\r
- notdone = 0;\r
- } else {\r
- heap[eventnum] = heap[parent];\r
- heap[eventnum]->heapposition = eventnum;\r
-\r
- eventnum = parent;\r
- notdone = eventnum > 0;\r
- }\r
- } while (notdone);\r
- }\r
- heap[eventnum] = moveevent;\r
- moveevent->heapposition = eventnum;\r
- eventheapify(heap, heapsize - 1, eventnum);\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-void createeventheap(eventheap, events, freeevents)\r
-struct event ***eventheap;\r
-struct event **events;\r
-struct event **freeevents;\r
-{\r
- point thispoint;\r
- int maxevents;\r
- int i;\r
-\r
- maxevents = (3 * inpoints) / 2;\r
- *eventheap = (struct event **) malloc(maxevents * sizeof(struct event *));\r
- if (*eventheap == (struct event **) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- *events = (struct event *) malloc(maxevents * sizeof(struct event));\r
- if (*events == (struct event *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- traversalinit(&points);\r
- for (i = 0; i < inpoints; i++) {\r
- thispoint = pointtraverse();\r
- (*events)[i].eventptr = (VOID *) thispoint;\r
- (*events)[i].xkey = thispoint[0];\r
- (*events)[i].ykey = thispoint[1];\r
- eventheapinsert(*eventheap, i, *events + i);\r
- }\r
- *freeevents = (struct event *) NULL;\r
- for (i = maxevents - 1; i >= inpoints; i--) {\r
- (*events)[i].eventptr = (VOID *) *freeevents;\r
- *freeevents = *events + i;\r
- }\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-int rightofhyperbola(fronttri, newsite)\r
-struct triedge *fronttri;\r
-point newsite;\r
-{\r
- point leftpoint, rightpoint;\r
- REAL dxa, dya, dxb, dyb;\r
-\r
- hyperbolacount++;\r
-\r
- dest(*fronttri, leftpoint);\r
- apex(*fronttri, rightpoint);\r
- if ((leftpoint[1] < rightpoint[1])\r
- || ((leftpoint[1] == rightpoint[1]) && (leftpoint[0] < rightpoint[0]))) {\r
- if (newsite[0] >= rightpoint[0]) {\r
- return 1;\r
- }\r
- } else {\r
- if (newsite[0] <= leftpoint[0]) {\r
- return 0;\r
- }\r
- }\r
- dxa = leftpoint[0] - newsite[0];\r
- dya = leftpoint[1] - newsite[1];\r
- dxb = rightpoint[0] - newsite[0];\r
- dyb = rightpoint[1] - newsite[1];\r
- return dya * (dxb * dxb + dyb * dyb) > dyb * (dxa * dxa + dya * dya);\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-REAL circletop(pa, pb, pc, ccwabc)\r
-point pa;\r
-point pb;\r
-point pc;\r
-REAL ccwabc;\r
-{\r
- REAL xac, yac, xbc, ybc, xab, yab;\r
- REAL aclen2, bclen2, ablen2;\r
-\r
- circletopcount++;\r
-\r
- xac = pa[0] - pc[0];\r
- yac = pa[1] - pc[1];\r
- xbc = pb[0] - pc[0];\r
- ybc = pb[1] - pc[1];\r
- xab = pa[0] - pb[0];\r
- yab = pa[1] - pb[1];\r
- aclen2 = xac * xac + yac * yac;\r
- bclen2 = xbc * xbc + ybc * ybc;\r
- ablen2 = xab * xab + yab * yab;\r
- return pc[1] + (xac * bclen2 - xbc * aclen2 + sqrt(aclen2 * bclen2 * ablen2))\r
- / (2.0 * ccwabc);\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-void check4deadevent(checktri, freeevents, eventheap, heapsize)\r
-struct triedge *checktri;\r
-struct event **freeevents;\r
-struct event **eventheap;\r
-int *heapsize;\r
-{\r
- struct event *deadevent;\r
- point eventpoint;\r
- int eventnum;\r
-\r
- org(*checktri, eventpoint);\r
- if (eventpoint != (point) NULL) {\r
- deadevent = (struct event *) eventpoint;\r
- eventnum = deadevent->heapposition;\r
- deadevent->eventptr = (VOID *) *freeevents;\r
- *freeevents = deadevent;\r
- eventheapdelete(eventheap, *heapsize, eventnum);\r
- (*heapsize)--;\r
- setorg(*checktri, NULL);\r
- }\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-struct splaynode *splay(splaytree, searchpoint, searchtri)\r
-struct splaynode *splaytree;\r
-point searchpoint;\r
-struct triedge *searchtri;\r
-{\r
- struct splaynode *child, *grandchild;\r
- struct splaynode *lefttree, *righttree;\r
- struct splaynode *leftright;\r
- point checkpoint;\r
- int rightofroot, rightofchild;\r
-\r
- if (splaytree == (struct splaynode *) NULL) {\r
- return (struct splaynode *) NULL;\r
- }\r
- dest(splaytree->keyedge, checkpoint);\r
- if (checkpoint == splaytree->keydest) {\r
- rightofroot = rightofhyperbola(&splaytree->keyedge, searchpoint);\r
- if (rightofroot) {\r
- triedgecopy(splaytree->keyedge, *searchtri);\r
- child = splaytree->rchild;\r
- } else {\r
- child = splaytree->lchild;\r
- }\r
- if (child == (struct splaynode *) NULL) {\r
- return splaytree;\r
- }\r
- dest(child->keyedge, checkpoint);\r
- if (checkpoint != child->keydest) {\r
- child = splay(child, searchpoint, searchtri);\r
- if (child == (struct splaynode *) NULL) {\r
- if (rightofroot) {\r
- splaytree->rchild = (struct splaynode *) NULL;\r
- } else {\r
- splaytree->lchild = (struct splaynode *) NULL;\r
- }\r
- return splaytree;\r
- }\r
- }\r
- rightofchild = rightofhyperbola(&child->keyedge, searchpoint);\r
- if (rightofchild) {\r
- triedgecopy(child->keyedge, *searchtri);\r
- grandchild = splay(child->rchild, searchpoint, searchtri);\r
- child->rchild = grandchild;\r
- } else {\r
- grandchild = splay(child->lchild, searchpoint, searchtri);\r
- child->lchild = grandchild;\r
- }\r
- if (grandchild == (struct splaynode *) NULL) {\r
- if (rightofroot) {\r
- splaytree->rchild = child->lchild;\r
- child->lchild = splaytree;\r
- } else {\r
- splaytree->lchild = child->rchild;\r
- child->rchild = splaytree;\r
- }\r
- return child;\r
- }\r
- if (rightofchild) {\r
- if (rightofroot) {\r
- splaytree->rchild = child->lchild;\r
- child->lchild = splaytree;\r
- } else {\r
- splaytree->lchild = grandchild->rchild;\r
- grandchild->rchild = splaytree;\r
- }\r
- child->rchild = grandchild->lchild;\r
- grandchild->lchild = child;\r
- } else {\r
- if (rightofroot) {\r
- splaytree->rchild = grandchild->lchild;\r
- grandchild->lchild = splaytree;\r
- } else {\r
- splaytree->lchild = child->rchild;\r
- child->rchild = splaytree;\r
- }\r
- child->lchild = grandchild->rchild;\r
- grandchild->rchild = child;\r
- }\r
- return grandchild;\r
- } else {\r
- lefttree = splay(splaytree->lchild, searchpoint, searchtri);\r
- righttree = splay(splaytree->rchild, searchpoint, searchtri);\r
-\r
- pooldealloc(&splaynodes, (VOID *) splaytree);\r
- if (lefttree == (struct splaynode *) NULL) {\r
- return righttree;\r
- } else if (righttree == (struct splaynode *) NULL) {\r
- return lefttree;\r
- } else if (lefttree->rchild == (struct splaynode *) NULL) {\r
- lefttree->rchild = righttree->lchild;\r
- righttree->lchild = lefttree;\r
- return righttree;\r
- } else if (righttree->lchild == (struct splaynode *) NULL) {\r
- righttree->lchild = lefttree->rchild;\r
- lefttree->rchild = righttree;\r
- return lefttree;\r
- } else {\r
-/* printf("Holy Toledo!!!\n"); */\r
- leftright = lefttree->rchild;\r
- while (leftright->rchild != (struct splaynode *) NULL) {\r
- leftright = leftright->rchild;\r
- }\r
- leftright->rchild = righttree;\r
- return lefttree;\r
- }\r
- }\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-struct splaynode *splayinsert(splayroot, newkey, searchpoint)\r
-struct splaynode *splayroot;\r
-struct triedge *newkey;\r
-point searchpoint;\r
-{\r
- struct splaynode *newsplaynode;\r
-\r
- newsplaynode = (struct splaynode *) poolalloc(&splaynodes);\r
- triedgecopy(*newkey, newsplaynode->keyedge);\r
- dest(*newkey, newsplaynode->keydest);\r
- if (splayroot == (struct splaynode *) NULL) {\r
- newsplaynode->lchild = (struct splaynode *) NULL;\r
- newsplaynode->rchild = (struct splaynode *) NULL;\r
- } else if (rightofhyperbola(&splayroot->keyedge, searchpoint)) {\r
- newsplaynode->lchild = splayroot;\r
- newsplaynode->rchild = splayroot->rchild;\r
- splayroot->rchild = (struct splaynode *) NULL;\r
- } else {\r
- newsplaynode->lchild = splayroot->lchild;\r
- newsplaynode->rchild = splayroot;\r
- splayroot->lchild = (struct splaynode *) NULL;\r
- }\r
- return newsplaynode;\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-struct splaynode *circletopinsert(splayroot, newkey, pa, pb, pc, topy)\r
-struct splaynode *splayroot;\r
-struct triedge *newkey;\r
-point pa;\r
-point pb;\r
-point pc;\r
-REAL topy;\r
-{\r
- REAL ccwabc;\r
- REAL xac, yac, xbc, ybc;\r
- REAL aclen2, bclen2;\r
- REAL searchpoint[2];\r
- struct triedge dummytri;\r
-\r
- ccwabc = counterclockwise(pa, pb, pc);\r
- xac = pa[0] - pc[0];\r
- yac = pa[1] - pc[1];\r
- xbc = pb[0] - pc[0];\r
- ybc = pb[1] - pc[1];\r
- aclen2 = xac * xac + yac * yac;\r
- bclen2 = xbc * xbc + ybc * ybc;\r
- searchpoint[0] = pc[0] - (yac * bclen2 - ybc * aclen2) / (2.0 * ccwabc);\r
- searchpoint[1] = topy;\r
- return splayinsert(splay(splayroot, (point) searchpoint, &dummytri), newkey,\r
- (point) searchpoint);\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-struct splaynode *frontlocate(splayroot, bottommost, searchpoint, searchtri,\r
- farright)\r
-struct splaynode *splayroot;\r
-struct triedge *bottommost;\r
-point searchpoint;\r
-struct triedge *searchtri;\r
-int *farright;\r
-{\r
- int farrightflag;\r
- triangle ptr; /* Temporary variable used by onext(). */\r
-\r
- triedgecopy(*bottommost, *searchtri);\r
- splayroot = splay(splayroot, searchpoint, searchtri);\r
-\r
- farrightflag = 0;\r
- while (!farrightflag && rightofhyperbola(searchtri, searchpoint)) {\r
- onextself(*searchtri);\r
- farrightflag = triedgeequal(*searchtri, *bottommost);\r
- }\r
- *farright = farrightflag;\r
- return splayroot;\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-#ifndef REDUCED\r
-\r
-long sweeplinedelaunay()\r
-{\r
- struct event **eventheap;\r
- struct event *events;\r
- struct event *freeevents;\r
- struct event *nextevent;\r
- struct event *newevent;\r
- struct splaynode *splayroot;\r
- struct triedge bottommost;\r
- struct triedge searchtri;\r
- struct triedge fliptri;\r
- struct triedge lefttri, righttri, farlefttri, farrighttri;\r
- struct triedge inserttri;\r
- point firstpoint, secondpoint;\r
- point nextpoint, lastpoint;\r
- point connectpoint;\r
- point leftpoint, midpoint, rightpoint;\r
- REAL lefttest, righttest;\r
- int heapsize;\r
- int check4events, farrightflag;\r
- triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */\r
-\r
- poolinit(&splaynodes, sizeof(struct splaynode), SPLAYNODEPERBLOCK, POINTER,\r
- 0);\r
- splayroot = (struct splaynode *) NULL;\r
-\r
- if (verbose) {\r
- printf(" Placing points in event heap.\n");\r
- }\r
- createeventheap(&eventheap, &events, &freeevents);\r
- heapsize = inpoints;\r
-\r
- if (verbose) {\r
- printf(" Forming triangulation.\n");\r
- }\r
- maketriangle(&lefttri);\r
- maketriangle(&righttri);\r
- bond(lefttri, righttri);\r
- lnextself(lefttri);\r
- lprevself(righttri);\r
- bond(lefttri, righttri);\r
- lnextself(lefttri);\r
- lprevself(righttri);\r
- bond(lefttri, righttri);\r
- firstpoint = (point) eventheap[0]->eventptr;\r
- eventheap[0]->eventptr = (VOID *) freeevents;\r
- freeevents = eventheap[0];\r
- eventheapdelete(eventheap, heapsize, 0);\r
- heapsize--;\r
- do {\r
- if (heapsize == 0) {\r
- printf("Error: Input points are all identical.\n");\r
- exit(1);\r
- }\r
- secondpoint = (point) eventheap[0]->eventptr;\r
- eventheap[0]->eventptr = (VOID *) freeevents;\r
- freeevents = eventheap[0];\r
- eventheapdelete(eventheap, heapsize, 0);\r
- heapsize--;\r
- if ((firstpoint[0] == secondpoint[0])\r
- && (firstpoint[1] == secondpoint[1])) {\r
- printf(\r
-"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",\r
- secondpoint[0], secondpoint[1]);\r
-/* Commented out - would eliminate point from output .node file.\r
- setpointmark(secondpoint, DEADPOINT);\r
-*/\r
- }\r
- } while ((firstpoint[0] == secondpoint[0])\r
- && (firstpoint[1] == secondpoint[1]));\r
- setorg(lefttri, firstpoint);\r
- setdest(lefttri, secondpoint);\r
- setorg(righttri, secondpoint);\r
- setdest(righttri, firstpoint);\r
- lprev(lefttri, bottommost);\r
- lastpoint = secondpoint;\r
- while (heapsize > 0) {\r
- nextevent = eventheap[0];\r
- eventheapdelete(eventheap, heapsize, 0);\r
- heapsize--;\r
- check4events = 1;\r
- if (nextevent->xkey < xmin) {\r
- decode(nextevent->eventptr, fliptri);\r
- oprev(fliptri, farlefttri);\r
- check4deadevent(&farlefttri, &freeevents, eventheap, &heapsize);\r
- onext(fliptri, farrighttri);\r
- check4deadevent(&farrighttri, &freeevents, eventheap, &heapsize);\r
-\r
- if (triedgeequal(farlefttri, bottommost)) {\r
- lprev(fliptri, bottommost);\r
- }\r
- flip(&fliptri);\r
- setapex(fliptri, NULL);\r
- lprev(fliptri, lefttri);\r
- lnext(fliptri, righttri);\r
- sym(lefttri, farlefttri);\r
-\r
- if (randomnation(SAMPLERATE) == 0) {\r
- symself(fliptri);\r
- dest(fliptri, leftpoint);\r
- apex(fliptri, midpoint);\r
- org(fliptri, rightpoint);\r
- splayroot = circletopinsert(splayroot, &lefttri, leftpoint, midpoint,\r
- rightpoint, nextevent->ykey);\r
- }\r
- } else {\r
- nextpoint = (point) nextevent->eventptr;\r
- if ((nextpoint[0] == lastpoint[0]) && (nextpoint[1] == lastpoint[1])) {\r
- printf(\r
-"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",\r
- nextpoint[0], nextpoint[1]);\r
-/* Commented out - would eliminate point from output .node file.\r
- setpointmark(nextpoint, DEADPOINT);\r
-*/\r
- check4events = 0;\r
- } else {\r
- lastpoint = nextpoint;\r
-\r
- splayroot = frontlocate(splayroot, &bottommost, nextpoint, &searchtri,\r
- &farrightflag);\r
-/*\r
- triedgecopy(bottommost, searchtri);\r
- farrightflag = 0;\r
- while (!farrightflag && rightofhyperbola(&searchtri, nextpoint)) {\r
- onextself(searchtri);\r
- farrightflag = triedgeequal(searchtri, bottommost);\r
- }\r
-*/\r
-\r
- check4deadevent(&searchtri, &freeevents, eventheap, &heapsize);\r
-\r
- triedgecopy(searchtri, farrighttri);\r
- sym(searchtri, farlefttri);\r
- maketriangle(&lefttri);\r
- maketriangle(&righttri);\r
- dest(farrighttri, connectpoint);\r
- setorg(lefttri, connectpoint);\r
- setdest(lefttri, nextpoint);\r
- setorg(righttri, nextpoint);\r
- setdest(righttri, connectpoint);\r
- bond(lefttri, righttri);\r
- lnextself(lefttri);\r
- lprevself(righttri);\r
- bond(lefttri, righttri);\r
- lnextself(lefttri);\r
- lprevself(righttri);\r
- bond(lefttri, farlefttri);\r
- bond(righttri, farrighttri);\r
- if (!farrightflag && triedgeequal(farrighttri, bottommost)) {\r
- triedgecopy(lefttri, bottommost);\r
- }\r
-\r
- if (randomnation(SAMPLERATE) == 0) {\r
- splayroot = splayinsert(splayroot, &lefttri, nextpoint);\r
- } else if (randomnation(SAMPLERATE) == 0) {\r
- lnext(righttri, inserttri);\r
- splayroot = splayinsert(splayroot, &inserttri, nextpoint);\r
- }\r
- }\r
- }\r
- nextevent->eventptr = (VOID *) freeevents;\r
- freeevents = nextevent;\r
-\r
- if (check4events) {\r
- apex(farlefttri, leftpoint);\r
- dest(lefttri, midpoint);\r
- apex(lefttri, rightpoint);\r
- lefttest = counterclockwise(leftpoint, midpoint, rightpoint);\r
- if (lefttest > 0.0) {\r
- newevent = freeevents;\r
- freeevents = (struct event *) freeevents->eventptr;\r
- newevent->xkey = xminextreme;\r
- newevent->ykey = circletop(leftpoint, midpoint, rightpoint,\r
- lefttest);\r
- newevent->eventptr = (VOID *) encode(lefttri);\r
- eventheapinsert(eventheap, heapsize, newevent);\r
- heapsize++;\r
- setorg(lefttri, newevent);\r
- }\r
- apex(righttri, leftpoint);\r
- org(righttri, midpoint);\r
- apex(farrighttri, rightpoint);\r
- righttest = counterclockwise(leftpoint, midpoint, rightpoint);\r
- if (righttest > 0.0) {\r
- newevent = freeevents;\r
- freeevents = (struct event *) freeevents->eventptr;\r
- newevent->xkey = xminextreme;\r
- newevent->ykey = circletop(leftpoint, midpoint, rightpoint,\r
- righttest);\r
- newevent->eventptr = (VOID *) encode(farrighttri);\r
- eventheapinsert(eventheap, heapsize, newevent);\r
- heapsize++;\r
- setorg(farrighttri, newevent);\r
- }\r
- }\r
- }\r
-\r
- pooldeinit(&splaynodes);\r
- lprevself(bottommost);\r
- return removeghosts(&bottommost);\r
-}\r
-\r
-#endif /* not REDUCED */\r
-\r
-/** **/\r
-/** **/\r
-/********* Sweepline Delaunay triangulation ends here *********/\r
-\r
-/********* General mesh construction routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* delaunay() Form a Delaunay triangulation. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-long delaunay()\r
-{\r
- eextras = 0;\r
- initializetrisegpools();\r
-\r
-#ifdef REDUCED\r
- if (!quiet) {\r
- printf(\r
- "Constructing Delaunay triangulation by divide-and-conquer method.\n");\r
- }\r
- return divconqdelaunay();\r
-#else /* not REDUCED */\r
- if (!quiet) {\r
- printf("Constructing Delaunay triangulation ");\r
- if (incremental) {\r
- printf("by incremental method.\n");\r
- } else if (sweepline) {\r
- printf("by sweepline method.\n");\r
- } else {\r
- printf("by divide-and-conquer method.\n");\r
- }\r
- }\r
- if (incremental) {\r
- return incrementaldelaunay();\r
- } else if (sweepline) {\r
- return sweeplinedelaunay();\r
- } else {\r
- return divconqdelaunay();\r
- }\r
-#endif /* not REDUCED */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* reconstruct() Reconstruct a triangulation from its .ele (and possibly */\r
-/* .poly) file. Used when the -r switch is used. */\r
-/* */\r
-/* Reads an .ele file and reconstructs the original mesh. If the -p switch */\r
-/* is used, this procedure will also read a .poly file and reconstruct the */\r
-/* shell edges of the original mesh. If the -a switch is used, this */\r
-/* procedure will also read an .area file and set a maximum area constraint */\r
-/* on each triangle. */\r
-/* */\r
-/* Points that are not corners of triangles, such as nodes on edges of */\r
-/* subparametric elements, are discarded. */\r
-/* */\r
-/* This routine finds the adjacencies between triangles (and shell edges) */\r
-/* by forming one stack of triangles for each vertex. Each triangle is on */\r
-/* three different stacks simultaneously. Each triangle's shell edge */\r
-/* pointers are used to link the items in each stack. This memory-saving */\r
-/* feature makes the code harder to read. The most important thing to keep */\r
-/* in mind is that each triangle is removed from a stack precisely when */\r
-/* the corresponding pointer is adjusted to refer to a shell edge rather */\r
-/* than the next triangle of the stack. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-#ifdef TRILIBRARY\r
-\r
-int reconstruct(trianglelist, triangleattriblist, trianglearealist, elements,\r
- corners, attribs, segmentlist, segmentmarkerlist,\r
- numberofsegments)\r
-int *trianglelist;\r
-REAL *triangleattriblist;\r
-REAL *trianglearealist;\r
-int elements;\r
-int corners;\r
-int attribs;\r
-int *segmentlist;\r
-int *segmentmarkerlist;\r
-int numberofsegments;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-long reconstruct(elefilename, areafilename, polyfilename, polyfile)\r
-char *elefilename;\r
-char *areafilename;\r
-char *polyfilename;\r
-FILE *polyfile;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- int pointindex;\r
- int attribindex;\r
-#else /* not TRILIBRARY */\r
- FILE *elefile;\r
- FILE *areafile;\r
- char inputline[INPUTLINESIZE];\r
- char *stringptr;\r
- int areaelements;\r
-#endif /* not TRILIBRARY */\r
- struct triedge triangleloop;\r
- struct triedge triangleleft;\r
- struct triedge checktri;\r
- struct triedge checkleft;\r
- struct triedge checkneighbor;\r
- struct edge shelleloop;\r
- triangle *vertexarray;\r
- triangle *prevlink;\r
- triangle nexttri;\r
- point tdest, tapex;\r
- point checkdest, checkapex;\r
- point shorg;\r
- point killpoint;\r
- REAL area;\r
- int corner[3];\r
- int end[2];\r
- int killpointindex;\r
- int incorners;\r
- int segmentmarkers;\r
- int boundmarker;\r
- int aroundpoint;\r
- long hullsize;\r
- int notfound;\r
- int elementnumber, segmentnumber;\r
- int i, j;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
-#ifdef TRILIBRARY\r
- inelements = elements;\r
- incorners = corners;\r
- if (incorners < 3) {\r
- printf("Error: Triangles must have at least 3 points.\n");\r
- exit(1);\r
- }\r
- eextras = attribs;\r
-#else /* not TRILIBRARY */\r
- /* Read the triangles from an .ele file. */\r
- if (!quiet) {\r
- printf("Opening %s.\n", elefilename);\r
- }\r
- elefile = fopen(elefilename, "r");\r
- if (elefile == (FILE *) NULL) {\r
- printf(" Error: Cannot access file %s.\n", elefilename);\r
- exit(1);\r
- }\r
- /* Read number of triangles, number of points per triangle, and */\r
- /* number of triangle attributes from .ele file. */\r
- stringptr = readline(inputline, elefile, elefilename);\r
- inelements = (int) strtol (stringptr, &stringptr, 0);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- incorners = 3;\r
- } else {\r
- incorners = (int) strtol (stringptr, &stringptr, 0);\r
- if (incorners < 3) {\r
- printf("Error: Triangles in %s must have at least 3 points.\n",\r
- elefilename);\r
- exit(1);\r
- }\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- eextras = 0;\r
- } else {\r
- eextras = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
-#endif /* not TRILIBRARY */\r
-\r
- initializetrisegpools();\r
-\r
- /* Create the triangles. */\r
- for (elementnumber = 1; elementnumber <= inelements; elementnumber++) {\r
- maketriangle(&triangleloop);\r
- /* Mark the triangle as living. */\r
- triangleloop.tri[3] = (triangle) triangleloop.tri;\r
- }\r
-\r
- if (poly) {\r
-#ifdef TRILIBRARY\r
- insegments = numberofsegments;\r
- segmentmarkers = segmentmarkerlist != (int *) NULL;\r
-#else /* not TRILIBRARY */\r
- /* Read number of segments and number of segment */\r
- /* boundary markers from .poly file. */\r
- stringptr = readline(inputline, polyfile, inpolyfilename);\r
- insegments = (int) strtol (stringptr, &stringptr, 0);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- segmentmarkers = 0;\r
- } else {\r
- segmentmarkers = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
-#endif /* not TRILIBRARY */\r
-\r
- /* Create the shell edges. */\r
- for (segmentnumber = 1; segmentnumber <= insegments; segmentnumber++) {\r
- makeshelle(&shelleloop);\r
- /* Mark the shell edge as living. */\r
- shelleloop.sh[2] = (shelle) shelleloop.sh;\r
- }\r
- }\r
-\r
-#ifdef TRILIBRARY\r
- pointindex = 0;\r
- attribindex = 0;\r
-#else /* not TRILIBRARY */\r
- if (vararea) {\r
- /* Open an .area file, check for consistency with the .ele file. */\r
- if (!quiet) {\r
- printf("Opening %s.\n", areafilename);\r
- }\r
- areafile = fopen(areafilename, "r");\r
- if (areafile == (FILE *) NULL) {\r
- printf(" Error: Cannot access file %s.\n", areafilename);\r
- exit(1);\r
- }\r
- stringptr = readline(inputline, areafile, areafilename);\r
- areaelements = (int) strtol (stringptr, &stringptr, 0);\r
- if (areaelements != inelements) {\r
- printf("Error: %s and %s disagree on number of triangles.\n",\r
- elefilename, areafilename);\r
- exit(1);\r
- }\r
- }\r
-#endif /* not TRILIBRARY */\r
-\r
- if (!quiet) {\r
- printf("Reconstructing mesh.\n");\r
- }\r
- /* Allocate a temporary array that maps each point to some adjacent */\r
- /* triangle. I took care to allocate all the permanent memory for */\r
- /* triangles and shell edges first. */\r
- vertexarray = (triangle *) malloc(points.items * sizeof(triangle));\r
- if (vertexarray == (triangle *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- /* Each point is initially unrepresented. */\r
- for (i = 0; i < points.items; i++) {\r
- vertexarray[i] = (triangle) dummytri;\r
- }\r
-\r
- if (verbose) {\r
- printf(" Assembling triangles.\n");\r
- }\r
- /* Read the triangles from the .ele file, and link */\r
- /* together those that share an edge. */\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- elementnumber = firstnumber;\r
- while (triangleloop.tri != (triangle *) NULL) {\r
-#ifdef TRILIBRARY\r
- /* Copy the triangle's three corners. */\r
- for (j = 0; j < 3; j++) {\r
- corner[j] = trianglelist[pointindex++];\r
- if ((corner[j] < firstnumber) || (corner[j] >= firstnumber + inpoints)) {\r
- printf("Error: Triangle %d has an invalid vertex index.\n",\r
- elementnumber);\r
- exit(1);\r
- }\r
- }\r
-#else /* not TRILIBRARY */\r
- /* Read triangle number and the triangle's three corners. */\r
- stringptr = readline(inputline, elefile, elefilename);\r
- for (j = 0; j < 3; j++) {\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Triangle %d is missing point %d in %s.\n",\r
- elementnumber, j + 1, elefilename);\r
- exit(1);\r
- } else {\r
- corner[j] = (int) strtol (stringptr, &stringptr, 0);\r
- if ((corner[j] < firstnumber) ||\r
- (corner[j] >= firstnumber + inpoints)) {\r
- printf("Error: Triangle %d has an invalid vertex index.\n",\r
- elementnumber);\r
- exit(1);\r
- }\r
- }\r
- }\r
-#endif /* not TRILIBRARY */\r
-\r
- /* Find out about (and throw away) extra nodes. */\r
- for (j = 3; j < incorners; j++) {\r
-#ifdef TRILIBRARY\r
- killpointindex = trianglelist[pointindex++];\r
-#else /* not TRILIBRARY */\r
- stringptr = findfield(stringptr);\r
- if (*stringptr != '\0') {\r
- killpointindex = (int) strtol (stringptr, &stringptr, 0);\r
-#endif /* not TRILIBRARY */\r
- if ((killpointindex >= firstnumber) &&\r
- (killpointindex < firstnumber + inpoints)) {\r
- /* Delete the non-corner point if it's not already deleted. */\r
- killpoint = getpoint(killpointindex);\r
- if (pointmark(killpoint) != DEADPOINT) {\r
- pointdealloc(killpoint);\r
- }\r
- }\r
-#ifndef TRILIBRARY\r
- }\r
-#endif /* not TRILIBRARY */\r
- }\r
-\r
- /* Read the triangle's attributes. */\r
- for (j = 0; j < eextras; j++) {\r
-#ifdef TRILIBRARY\r
- setelemattribute(triangleloop, j, triangleattriblist[attribindex++]);\r
-#else /* not TRILIBRARY */\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- setelemattribute(triangleloop, j, 0);\r
- } else {\r
- setelemattribute(triangleloop, j,\r
- (REAL) strtod (stringptr, &stringptr));\r
- }\r
-#endif /* not TRILIBRARY */\r
- }\r
-\r
- if (vararea) {\r
-#ifdef TRILIBRARY\r
- area = trianglearealist[elementnumber - firstnumber];\r
-#else /* not TRILIBRARY */\r
- /* Read an area constraint from the .area file. */\r
- stringptr = readline(inputline, areafile, areafilename);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- area = -1.0; /* No constraint on this triangle. */\r
- } else {\r
- area = (REAL) strtod(stringptr, &stringptr);\r
- }\r
-#endif /* not TRILIBRARY */\r
- setareabound(triangleloop, area);\r
- }\r
-\r
- /* Set the triangle's vertices. */\r
- triangleloop.orient = 0;\r
- setorg(triangleloop, getpoint(corner[0]));\r
- setdest(triangleloop, getpoint(corner[1]));\r
- setapex(triangleloop, getpoint(corner[2]));\r
- /* Try linking the triangle to others that share these vertices. */\r
- for (triangleloop.orient = 0; triangleloop.orient < 3;\r
- triangleloop.orient++) {\r
- /* Take the number for the origin of triangleloop. */\r
- aroundpoint = corner[triangleloop.orient];\r
- /* Look for other triangles having this vertex. */\r
- nexttri = vertexarray[aroundpoint - firstnumber];\r
- /* Link the current triangle to the next one in the stack. */\r
- triangleloop.tri[6 + triangleloop.orient] = nexttri;\r
- /* Push the current triangle onto the stack. */\r
- vertexarray[aroundpoint - firstnumber] = encode(triangleloop);\r
- decode(nexttri, checktri);\r
- if (checktri.tri != dummytri) {\r
- dest(triangleloop, tdest);\r
- apex(triangleloop, tapex);\r
- /* Look for other triangles that share an edge. */\r
- do {\r
- dest(checktri, checkdest);\r
- apex(checktri, checkapex);\r
- if (tapex == checkdest) {\r
- /* The two triangles share an edge; bond them together. */\r
- lprev(triangleloop, triangleleft);\r
- bond(triangleleft, checktri);\r
- }\r
- if (tdest == checkapex) {\r
- /* The two triangles share an edge; bond them together. */\r
- lprev(checktri, checkleft);\r
- bond(triangleloop, checkleft);\r
- }\r
- /* Find the next triangle in the stack. */\r
- nexttri = checktri.tri[6 + checktri.orient];\r
- decode(nexttri, checktri);\r
- } while (checktri.tri != dummytri);\r
- }\r
- }\r
- triangleloop.tri = triangletraverse();\r
- elementnumber++;\r
- }\r
-\r
-#ifdef TRILIBRARY\r
- pointindex = 0;\r
-#else /* not TRILIBRARY */\r
- fclose(elefile);\r
- if (vararea) {\r
- fclose(areafile);\r
- }\r
-#endif /* not TRILIBRARY */\r
-\r
- hullsize = 0; /* Prepare to count the boundary edges. */\r
- if (poly) {\r
- if (verbose) {\r
- printf(" Marking segments in triangulation.\n");\r
- }\r
- /* Read the segments from the .poly file, and link them */\r
- /* to their neighboring triangles. */\r
- boundmarker = 0;\r
- traversalinit(&shelles);\r
- shelleloop.sh = shelletraverse();\r
- segmentnumber = firstnumber;\r
- while (shelleloop.sh != (shelle *) NULL) {\r
-#ifdef TRILIBRARY\r
- end[0] = segmentlist[pointindex++];\r
- end[1] = segmentlist[pointindex++];\r
- if (segmentmarkers) {\r
- boundmarker = segmentmarkerlist[segmentnumber - firstnumber];\r
- }\r
-#else /* not TRILIBRARY */\r
- /* Read the endpoints of each segment, and possibly a boundary marker. */\r
- stringptr = readline(inputline, polyfile, inpolyfilename);\r
- /* Skip the first (segment number) field. */\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Segment %d has no endpoints in %s.\n", segmentnumber,\r
- polyfilename);\r
- exit(1);\r
- } else {\r
- end[0] = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Segment %d is missing its second endpoint in %s.\n",\r
- segmentnumber, polyfilename);\r
- exit(1);\r
- } else {\r
- end[1] = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- if (segmentmarkers) {\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- boundmarker = 0;\r
- } else {\r
- boundmarker = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- }\r
-#endif /* not TRILIBRARY */\r
- for (j = 0; j < 2; j++) {\r
- if ((end[j] < firstnumber) || (end[j] >= firstnumber + inpoints)) {\r
- printf("Error: Segment %d has an invalid vertex index.\n", \r
- segmentnumber);\r
- exit(1);\r
- }\r
- }\r
-\r
- /* set the shell edge's vertices. */\r
- shelleloop.shorient = 0;\r
- setsorg(shelleloop, getpoint(end[0]));\r
- setsdest(shelleloop, getpoint(end[1]));\r
- setmark(shelleloop, boundmarker);\r
- /* Try linking the shell edge to triangles that share these vertices. */\r
- for (shelleloop.shorient = 0; shelleloop.shorient < 2;\r
- shelleloop.shorient++) {\r
- /* Take the number for the destination of shelleloop. */\r
- aroundpoint = end[1 - shelleloop.shorient];\r
- /* Look for triangles having this vertex. */\r
- prevlink = &vertexarray[aroundpoint - firstnumber];\r
- nexttri = vertexarray[aroundpoint - firstnumber];\r
- decode(nexttri, checktri);\r
- sorg(shelleloop, shorg);\r
- notfound = 1;\r
- /* Look for triangles having this edge. Note that I'm only */\r
- /* comparing each triangle's destination with the shell edge; */\r
- /* each triangle's apex is handled through a different vertex. */\r
- /* Because each triangle appears on three vertices' lists, each */\r
- /* occurrence of a triangle on a list can (and does) represent */\r
- /* an edge. In this way, most edges are represented twice, and */\r
- /* every triangle-segment bond is represented once. */\r
- while (notfound && (checktri.tri != dummytri)) {\r
- dest(checktri, checkdest);\r
- if (shorg == checkdest) {\r
- /* We have a match. Remove this triangle from the list. */\r
- *prevlink = checktri.tri[6 + checktri.orient];\r
- /* Bond the shell edge to the triangle. */\r
- tsbond(checktri, shelleloop);\r
- /* Check if this is a boundary edge. */\r
- sym(checktri, checkneighbor);\r
- if (checkneighbor.tri == dummytri) {\r
- /* The next line doesn't insert a shell edge (because there's */\r
- /* already one there), but it sets the boundary markers of */\r
- /* the existing shell edge and its vertices. */\r
- insertshelle(&checktri, 1);\r
- hullsize++;\r
- }\r
- notfound = 0;\r
- }\r
- /* Find the next triangle in the stack. */\r
- prevlink = &checktri.tri[6 + checktri.orient];\r
- nexttri = checktri.tri[6 + checktri.orient];\r
- decode(nexttri, checktri);\r
- }\r
- }\r
- shelleloop.sh = shelletraverse();\r
- segmentnumber++;\r
- }\r
- }\r
-\r
- /* Mark the remaining edges as not being attached to any shell edge. */\r
- /* Also, count the (yet uncounted) boundary edges. */\r
- for (i = 0; i < points.items; i++) {\r
- /* Search the stack of triangles adjacent to a point. */\r
- nexttri = vertexarray[i];\r
- decode(nexttri, checktri);\r
- while (checktri.tri != dummytri) {\r
- /* Find the next triangle in the stack before this */\r
- /* information gets overwritten. */\r
- nexttri = checktri.tri[6 + checktri.orient];\r
- /* No adjacent shell edge. (This overwrites the stack info.) */\r
- tsdissolve(checktri);\r
- sym(checktri, checkneighbor);\r
- if (checkneighbor.tri == dummytri) {\r
- insertshelle(&checktri, 1);\r
- hullsize++;\r
- }\r
- decode(nexttri, checktri);\r
- }\r
- }\r
-\r
- free(vertexarray);\r
- return hullsize;\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/** **/\r
-/** **/\r
-/********* General mesh construction routines end here *********/\r
-\r
-/********* Segment (shell edge) insertion begins here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* finddirection() Find the first triangle on the path from one point */\r
-/* to another. */\r
-/* */\r
-/* Finds the triangle that intersects a line segment drawn from the */\r
-/* origin of `searchtri' to the point `endpoint', and returns the result */\r
-/* in `searchtri'. The origin of `searchtri' does not change, even though */\r
-/* the triangle returned may differ from the one passed in. This routine */\r
-/* is used to find the direction to move in to get from one point to */\r
-/* another. */\r
-/* */\r
-/* The return value notes whether the destination or apex of the found */\r
-/* triangle is collinear with the two points in question. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-enum finddirectionresult finddirection(searchtri, endpoint)\r
-struct triedge *searchtri;\r
-point endpoint;\r
-{\r
- struct triedge checktri;\r
- point startpoint;\r
- point leftpoint, rightpoint;\r
- REAL leftccw, rightccw;\r
- int leftflag, rightflag;\r
- triangle ptr; /* Temporary variable used by onext() and oprev(). */\r
-\r
- org(*searchtri, startpoint);\r
- dest(*searchtri, rightpoint);\r
- apex(*searchtri, leftpoint);\r
- /* Is `endpoint' to the left? */\r
- leftccw = counterclockwise(endpoint, startpoint, leftpoint);\r
- leftflag = leftccw > 0.0;\r
- /* Is `endpoint' to the right? */\r
- rightccw = counterclockwise(startpoint, endpoint, rightpoint);\r
- rightflag = rightccw > 0.0;\r
- if (leftflag && rightflag) {\r
- /* `searchtri' faces directly away from `endpoint'. We could go */\r
- /* left or right. Ask whether it's a triangle or a boundary */\r
- /* on the left. */\r
- onext(*searchtri, checktri);\r
- if (checktri.tri == dummytri) {\r
- leftflag = 0;\r
- } else {\r
- rightflag = 0;\r
- }\r
- }\r
- while (leftflag) {\r
- /* Turn left until satisfied. */\r
- onextself(*searchtri);\r
- if (searchtri->tri == dummytri) {\r
- printf("Internal error in finddirection(): Unable to find a\n");\r
- printf(" triangle leading from (%.12g, %.12g) to", startpoint[0],\r
- startpoint[1]);\r
- printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);\r
- internalerror();\r
- }\r
- apex(*searchtri, leftpoint);\r
- rightccw = leftccw;\r
- leftccw = counterclockwise(endpoint, startpoint, leftpoint);\r
- leftflag = leftccw > 0.0;\r
- }\r
- while (rightflag) {\r
- /* Turn right until satisfied. */\r
- oprevself(*searchtri);\r
- if (searchtri->tri == dummytri) {\r
- printf("Internal error in finddirection(): Unable to find a\n");\r
- printf(" triangle leading from (%.12g, %.12g) to", startpoint[0],\r
- startpoint[1]);\r
- printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);\r
- internalerror();\r
- }\r
- dest(*searchtri, rightpoint);\r
- leftccw = rightccw;\r
- rightccw = counterclockwise(startpoint, endpoint, rightpoint);\r
- rightflag = rightccw > 0.0;\r
- }\r
- if (leftccw == 0.0) {\r
- return LEFTCOLLINEAR;\r
- } else if (rightccw == 0.0) {\r
- return RIGHTCOLLINEAR;\r
- } else {\r
- return WITHIN;\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* segmentintersection() Find the intersection of an existing segment */\r
-/* and a segment that is being inserted. Insert */\r
-/* a point at the intersection, splitting an */\r
-/* existing shell edge. */\r
-/* */\r
-/* The segment being inserted connects the apex of splittri to endpoint2. */\r
-/* splitshelle is the shell edge being split, and MUST be opposite */\r
-/* splittri. Hence, the edge being split connects the origin and */\r
-/* destination of splittri. */\r
-/* */\r
-/* On completion, splittri is a handle having the newly inserted */\r
-/* intersection point as its origin, and endpoint1 as its destination. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void segmentintersection(splittri, splitshelle, endpoint2)\r
-struct triedge *splittri;\r
-struct edge *splitshelle;\r
-point endpoint2;\r
-{\r
- point endpoint1;\r
- point torg, tdest;\r
- point leftpoint, rightpoint;\r
- point newpoint;\r
- enum insertsiteresult success;\r
- enum finddirectionresult collinear;\r
- REAL ex, ey;\r
- REAL tx, ty;\r
- REAL etx, ety;\r
- REAL split, denom;\r
- int i;\r
- triangle ptr; /* Temporary variable used by onext(). */\r
-\r
- /* Find the other three segment endpoints. */\r
- apex(*splittri, endpoint1);\r
- org(*splittri, torg);\r
- dest(*splittri, tdest);\r
- /* Segment intersection formulae; see the Antonio reference. */\r
- tx = tdest[0] - torg[0];\r
- ty = tdest[1] - torg[1];\r
- ex = endpoint2[0] - endpoint1[0];\r
- ey = endpoint2[1] - endpoint1[1];\r
- etx = torg[0] - endpoint2[0];\r
- ety = torg[1] - endpoint2[1];\r
- denom = ty * ex - tx * ey;\r
- if (denom == 0.0) {\r
- printf("Internal error in segmentintersection():");\r
- printf(" Attempt to find intersection of parallel segments.\n");\r
- internalerror();\r
- }\r
- split = (ey * etx - ex * ety) / denom;\r
- /* Create the new point. */\r
- newpoint = (point) poolalloc(&points);\r
- /* Interpolate its coordinate and attributes. */\r
- for (i = 0; i < 2 + nextras; i++) {\r
- newpoint[i] = torg[i] + split * (tdest[i] - torg[i]);\r
- }\r
- setpointmark(newpoint, mark(*splitshelle));\r
- if (verbose > 1) {\r
- printf(\r
- " Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",\r
- torg[0], torg[1], tdest[0], tdest[1], newpoint[0], newpoint[1]);\r
- }\r
- /* Insert the intersection point. This should always succeed. */\r
- success = insertsite(newpoint, splittri, splitshelle, 0, 0);\r
- if (success != SUCCESSFULPOINT) {\r
- printf("Internal error in segmentintersection():\n");\r
- printf(" Failure to split a segment.\n");\r
- internalerror();\r
- }\r
- if (steinerleft > 0) {\r
- steinerleft--;\r
- }\r
- /* Inserting the point may have caused edge flips. We wish to rediscover */\r
- /* the edge connecting endpoint1 to the new intersection point. */\r
- collinear = finddirection(splittri, endpoint1);\r
- dest(*splittri, rightpoint);\r
- apex(*splittri, leftpoint);\r
- if ((leftpoint[0] == endpoint1[0]) && (leftpoint[1] == endpoint1[1])) {\r
- onextself(*splittri);\r
- } else if ((rightpoint[0] != endpoint1[0]) ||\r
- (rightpoint[1] != endpoint1[1])) {\r
- printf("Internal error in segmentintersection():\n");\r
- printf(" Topological inconsistency after splitting a segment.\n");\r
- internalerror();\r
- }\r
- /* `splittri' should have destination endpoint1. */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* scoutsegment() Scout the first triangle on the path from one endpoint */\r
-/* to another, and check for completion (reaching the */\r
-/* second endpoint), a collinear point, and the */\r
-/* intersection of two segments. */\r
-/* */\r
-/* Returns one if the entire segment is successfully inserted, and zero if */\r
-/* the job must be finished by conformingedge() or constrainededge(). */\r
-/* */\r
-/* If the first triangle on the path has the second endpoint as its */\r
-/* destination or apex, a shell edge is inserted and the job is done. */\r
-/* */\r
-/* If the first triangle on the path has a destination or apex that lies on */\r
-/* the segment, a shell edge is inserted connecting the first endpoint to */\r
-/* the collinear point, and the search is continued from the collinear */\r
-/* point. */\r
-/* */\r
-/* If the first triangle on the path has a shell edge opposite its origin, */\r
-/* then there is a segment that intersects the segment being inserted. */\r
-/* Their intersection point is inserted, splitting the shell edge. */\r
-/* */\r
-/* Otherwise, return zero. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-int scoutsegment(searchtri, endpoint2, newmark)\r
-struct triedge *searchtri;\r
-point endpoint2;\r
-int newmark;\r
-{\r
- struct triedge crosstri;\r
- struct edge crossedge;\r
- point leftpoint, rightpoint;\r
- point endpoint1;\r
- enum finddirectionresult collinear;\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- collinear = finddirection(searchtri, endpoint2);\r
- dest(*searchtri, rightpoint);\r
- apex(*searchtri, leftpoint);\r
- if (((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) ||\r
- ((rightpoint[0] == endpoint2[0]) && (rightpoint[1] == endpoint2[1]))) {\r
- /* The segment is already an edge in the mesh. */\r
- if ((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) {\r
- lprevself(*searchtri);\r
- }\r
- /* Insert a shell edge, if there isn't already one there. */\r
- insertshelle(searchtri, newmark);\r
- return 1;\r
- } else if (collinear == LEFTCOLLINEAR) {\r
- /* We've collided with a point between the segment's endpoints. */\r
- /* Make the collinear point be the triangle's origin. */\r
- lprevself(*searchtri);\r
- insertshelle(searchtri, newmark);\r
- /* Insert the remainder of the segment. */\r
- return scoutsegment(searchtri, endpoint2, newmark);\r
- } else if (collinear == RIGHTCOLLINEAR) {\r
- /* We've collided with a point between the segment's endpoints. */\r
- insertshelle(searchtri, newmark);\r
- /* Make the collinear point be the triangle's origin. */\r
- lnextself(*searchtri);\r
- /* Insert the remainder of the segment. */\r
- return scoutsegment(searchtri, endpoint2, newmark);\r
- } else {\r
- lnext(*searchtri, crosstri);\r
- tspivot(crosstri, crossedge);\r
- /* Check for a crossing segment. */\r
- if (crossedge.sh == dummysh) {\r
- return 0;\r
- } else {\r
- org(*searchtri, endpoint1);\r
- /* Insert a point at the intersection. */\r
- segmentintersection(&crosstri, &crossedge, endpoint2);\r
- triedgecopy(crosstri, *searchtri);\r
- insertshelle(searchtri, newmark);\r
- /* Insert the remainder of the segment. */\r
- return scoutsegment(searchtri, endpoint2, newmark);\r
- }\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* conformingedge() Force a segment into a conforming Delaunay */\r
-/* triangulation by inserting a point at its midpoint, */\r
-/* and recursively forcing in the two half-segments if */\r
-/* necessary. */\r
-/* */\r
-/* Generates a sequence of edges connecting `endpoint1' to `endpoint2'. */\r
-/* `newmark' is the boundary marker of the segment, assigned to each new */\r
-/* splitting point and shell edge. */\r
-/* */\r
-/* Note that conformingedge() does not always maintain the conforming */\r
-/* Delaunay property. Once inserted, segments are locked into place; */\r
-/* points inserted later (to force other segments in) may render these */\r
-/* fixed segments non-Delaunay. The conforming Delaunay property will be */\r
-/* restored by enforcequality() by splitting encroached segments. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef REDUCED\r
-#ifndef CDT_ONLY\r
-\r
-void conformingedge(endpoint1, endpoint2, newmark)\r
-point endpoint1;\r
-point endpoint2;\r
-int newmark;\r
-{\r
- struct triedge searchtri1, searchtri2;\r
- struct edge brokenshelle;\r
- point newpoint;\r
- point midpoint1, midpoint2;\r
- enum insertsiteresult success;\r
- int result1, result2;\r
- int i;\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- if (verbose > 2) {\r
- printf("Forcing segment into triangulation by recursive splitting:\n");\r
- printf(" (%.12g, %.12g) (%.12g, %.12g)\n", endpoint1[0], endpoint1[1],\r
- endpoint2[0], endpoint2[1]);\r
- }\r
- /* Create a new point to insert in the middle of the segment. */\r
- newpoint = (point) poolalloc(&points);\r
- /* Interpolate coordinates and attributes. */\r
- for (i = 0; i < 2 + nextras; i++) {\r
- newpoint[i] = 0.5 * (endpoint1[i] + endpoint2[i]);\r
- }\r
- setpointmark(newpoint, newmark);\r
- /* Find a boundary triangle to search from. */\r
- searchtri1.tri = (triangle *) NULL;\r
- /* Attempt to insert the new point. */\r
- success = insertsite(newpoint, &searchtri1, (struct edge *) NULL, 0, 0);\r
- if (success == DUPLICATEPOINT) {\r
- if (verbose > 2) {\r
- printf(" Segment intersects existing point (%.12g, %.12g).\n",\r
- newpoint[0], newpoint[1]);\r
- }\r
- /* Use the point that's already there. */\r
- pointdealloc(newpoint);\r
- org(searchtri1, newpoint);\r
- } else {\r
- if (success == VIOLATINGPOINT) {\r
- if (verbose > 2) {\r
- printf(" Two segments intersect at (%.12g, %.12g).\n",\r
- newpoint[0], newpoint[1]);\r
- }\r
- /* By fluke, we've landed right on another segment. Split it. */\r
- tspivot(searchtri1, brokenshelle);\r
- success = insertsite(newpoint, &searchtri1, &brokenshelle, 0, 0);\r
- if (success != SUCCESSFULPOINT) {\r
- printf("Internal error in conformingedge():\n");\r
- printf(" Failure to split a segment.\n");\r
- internalerror();\r
- }\r
- }\r
- /* The point has been inserted successfully. */\r
- if (steinerleft > 0) {\r
- steinerleft--;\r
- }\r
- }\r
- triedgecopy(searchtri1, searchtri2);\r
- result1 = scoutsegment(&searchtri1, endpoint1, newmark);\r
- result2 = scoutsegment(&searchtri2, endpoint2, newmark);\r
- if (!result1) {\r
- /* The origin of searchtri1 may have changed if a collision with an */\r
- /* intervening vertex on the segment occurred. */\r
- org(searchtri1, midpoint1);\r
- conformingedge(midpoint1, endpoint1, newmark);\r
- }\r
- if (!result2) {\r
- /* The origin of searchtri2 may have changed if a collision with an */\r
- /* intervening vertex on the segment occurred. */\r
- org(searchtri2, midpoint2);\r
- conformingedge(midpoint2, endpoint2, newmark);\r
- }\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-#endif /* not REDUCED */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* delaunayfixup() Enforce the Delaunay condition at an edge, fanning out */\r
-/* recursively from an existing point. Pay special */\r
-/* attention to stacking inverted triangles. */\r
-/* */\r
-/* This is a support routine for inserting segments into a constrained */\r
-/* Delaunay triangulation. */\r
-/* */\r
-/* The origin of fixuptri is treated as if it has just been inserted, and */\r
-/* the local Delaunay condition needs to be enforced. It is only enforced */\r
-/* in one sector, however, that being the angular range defined by */\r
-/* fixuptri. */\r
-/* */\r
-/* This routine also needs to make decisions regarding the "stacking" of */\r
-/* triangles. (Read the description of constrainededge() below before */\r
-/* reading on here, so you understand the algorithm.) If the position of */\r
-/* the new point (the origin of fixuptri) indicates that the vertex before */\r
-/* it on the polygon is a reflex vertex, then "stack" the triangle by */\r
-/* doing nothing. (fixuptri is an inverted triangle, which is how stacked */\r
-/* triangles are identified.) */\r
-/* */\r
-/* Otherwise, check whether the vertex before that was a reflex vertex. */\r
-/* If so, perform an edge flip, thereby eliminating an inverted triangle */\r
-/* (popping it off the stack). The edge flip may result in the creation */\r
-/* of a new inverted triangle, depending on whether or not the new vertex */\r
-/* is visible to the vertex three edges behind on the polygon. */\r
-/* */\r
-/* If neither of the two vertices behind the new vertex are reflex */\r
-/* vertices, fixuptri and fartri, the triangle opposite it, are not */\r
-/* inverted; hence, ensure that the edge between them is locally Delaunay. */\r
-/* */\r
-/* `leftside' indicates whether or not fixuptri is to the left of the */\r
-/* segment being inserted. (Imagine that the segment is pointing up from */\r
-/* endpoint1 to endpoint2.) */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void delaunayfixup(fixuptri, leftside)\r
-struct triedge *fixuptri;\r
-int leftside;\r
-{\r
- struct triedge neartri;\r
- struct triedge fartri;\r
- struct edge faredge;\r
- point nearpoint, leftpoint, rightpoint, farpoint;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- lnext(*fixuptri, neartri);\r
- sym(neartri, fartri);\r
- /* Check if the edge opposite the origin of fixuptri can be flipped. */\r
- if (fartri.tri == dummytri) {\r
- return;\r
- }\r
- tspivot(neartri, faredge);\r
- if (faredge.sh != dummysh) {\r
- return;\r
- }\r
- /* Find all the relevant vertices. */\r
- apex(neartri, nearpoint);\r
- org(neartri, leftpoint);\r
- dest(neartri, rightpoint);\r
- apex(fartri, farpoint);\r
- /* Check whether the previous polygon vertex is a reflex vertex. */\r
- if (leftside) {\r
- if (counterclockwise(nearpoint, leftpoint, farpoint) <= 0.0) {\r
- /* leftpoint is a reflex vertex too. Nothing can */\r
- /* be done until a convex section is found. */\r
- return;\r
- }\r
- } else {\r
- if (counterclockwise(farpoint, rightpoint, nearpoint) <= 0.0) {\r
- /* rightpoint is a reflex vertex too. Nothing can */\r
- /* be done until a convex section is found. */\r
- return;\r
- }\r
- }\r
- if (counterclockwise(rightpoint, leftpoint, farpoint) > 0.0) {\r
- /* fartri is not an inverted triangle, and farpoint is not a reflex */\r
- /* vertex. As there are no reflex vertices, fixuptri isn't an */\r
- /* inverted triangle, either. Hence, test the edge between the */\r
- /* triangles to ensure it is locally Delaunay. */\r
- if (incircle(leftpoint, farpoint, rightpoint, nearpoint) <= 0.0) {\r
- return;\r
- }\r
- /* Not locally Delaunay; go on to an edge flip. */\r
- } /* else fartri is inverted; remove it from the stack by flipping. */\r
- flip(&neartri);\r
- lprevself(*fixuptri); /* Restore the origin of fixuptri after the flip. */\r
- /* Recursively process the two triangles that result from the flip. */\r
- delaunayfixup(fixuptri, leftside);\r
- delaunayfixup(&fartri, leftside);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* constrainededge() Force a segment into a constrained Delaunay */\r
-/* triangulation by deleting the triangles it */\r
-/* intersects, and triangulating the polygons that */\r
-/* form on each side of it. */\r
-/* */\r
-/* Generates a single edge connecting `endpoint1' to `endpoint2'. The */\r
-/* triangle `starttri' has `endpoint1' as its origin. `newmark' is the */\r
-/* boundary marker of the segment. */\r
-/* */\r
-/* To insert a segment, every triangle whose interior intersects the */\r
-/* segment is deleted. The union of these deleted triangles is a polygon */\r
-/* (which is not necessarily monotone, but is close enough), which is */\r
-/* divided into two polygons by the new segment. This routine's task is */\r
-/* to generate the Delaunay triangulation of these two polygons. */\r
-/* */\r
-/* You might think of this routine's behavior as a two-step process. The */\r
-/* first step is to walk from endpoint1 to endpoint2, flipping each edge */\r
-/* encountered. This step creates a fan of edges connected to endpoint1, */\r
-/* including the desired edge to endpoint2. The second step enforces the */\r
-/* Delaunay condition on each side of the segment in an incremental manner: */\r
-/* proceeding along the polygon from endpoint1 to endpoint2 (this is done */\r
-/* independently on each side of the segment), each vertex is "enforced" */\r
-/* as if it had just been inserted, but affecting only the previous */\r
-/* vertices. The result is the same as if the vertices had been inserted */\r
-/* in the order they appear on the polygon, so the result is Delaunay. */\r
-/* */\r
-/* In truth, constrainededge() interleaves these two steps. The procedure */\r
-/* walks from endpoint1 to endpoint2, and each time an edge is encountered */\r
-/* and flipped, the newly exposed vertex (at the far end of the flipped */\r
-/* edge) is "enforced" upon the previously flipped edges, usually affecting */\r
-/* only one side of the polygon (depending upon which side of the segment */\r
-/* the vertex falls on). */\r
-/* */\r
-/* The algorithm is complicated by the need to handle polygons that are not */\r
-/* convex. Although the polygon is not necessarily monotone, it can be */\r
-/* triangulated in a manner similar to the stack-based algorithms for */\r
-/* monotone polygons. For each reflex vertex (local concavity) of the */\r
-/* polygon, there will be an inverted triangle formed by one of the edge */\r
-/* flips. (An inverted triangle is one with negative area - that is, its */\r
-/* vertices are arranged in clockwise order - and is best thought of as a */\r
-/* wrinkle in the fabric of the mesh.) Each inverted triangle can be */\r
-/* thought of as a reflex vertex pushed on the stack, waiting to be fixed */\r
-/* later. */\r
-/* */\r
-/* A reflex vertex is popped from the stack when a vertex is inserted that */\r
-/* is visible to the reflex vertex. (However, if the vertex behind the */\r
-/* reflex vertex is not visible to the reflex vertex, a new inverted */\r
-/* triangle will take its place on the stack.) These details are handled */\r
-/* by the delaunayfixup() routine above. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void constrainededge(starttri, endpoint2, newmark)\r
-struct triedge *starttri;\r
-point endpoint2;\r
-int newmark;\r
-{\r
- struct triedge fixuptri, fixuptri2;\r
- struct edge fixupedge;\r
- point endpoint1;\r
- point farpoint;\r
- REAL area;\r
- int collision;\r
- int done;\r
- triangle ptr; /* Temporary variable used by sym() and oprev(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- org(*starttri, endpoint1);\r
- lnext(*starttri, fixuptri);\r
- flip(&fixuptri);\r
- /* `collision' indicates whether we have found a point directly */\r
- /* between endpoint1 and endpoint2. */\r
- collision = 0;\r
- done = 0;\r
- do {\r
- org(fixuptri, farpoint);\r
- /* `farpoint' is the extreme point of the polygon we are "digging" */\r
- /* to get from endpoint1 to endpoint2. */\r
- if ((farpoint[0] == endpoint2[0]) && (farpoint[1] == endpoint2[1])) {\r
- oprev(fixuptri, fixuptri2);\r
- /* Enforce the Delaunay condition around endpoint2. */\r
- delaunayfixup(&fixuptri, 0);\r
- delaunayfixup(&fixuptri2, 1);\r
- done = 1;\r
- } else {\r
- /* Check whether farpoint is to the left or right of the segment */\r
- /* being inserted, to decide which edge of fixuptri to dig */\r
- /* through next. */\r
- area = counterclockwise(endpoint1, endpoint2, farpoint);\r
- if (area == 0.0) {\r
- /* We've collided with a point between endpoint1 and endpoint2. */\r
- collision = 1;\r
- oprev(fixuptri, fixuptri2);\r
- /* Enforce the Delaunay condition around farpoint. */\r
- delaunayfixup(&fixuptri, 0);\r
- delaunayfixup(&fixuptri2, 1);\r
- done = 1;\r
- } else {\r
- if (area > 0.0) { /* farpoint is to the left of the segment. */\r
- oprev(fixuptri, fixuptri2);\r
- /* Enforce the Delaunay condition around farpoint, on the */\r
- /* left side of the segment only. */\r
- delaunayfixup(&fixuptri2, 1);\r
- /* Flip the edge that crosses the segment. After the edge is */\r
- /* flipped, one of its endpoints is the fan vertex, and the */\r
- /* destination of fixuptri is the fan vertex. */\r
- lprevself(fixuptri);\r
- } else { /* farpoint is to the right of the segment. */\r
- delaunayfixup(&fixuptri, 0);\r
- /* Flip the edge that crosses the segment. After the edge is */\r
- /* flipped, one of its endpoints is the fan vertex, and the */\r
- /* destination of fixuptri is the fan vertex. */\r
- oprevself(fixuptri);\r
- }\r
- /* Check for two intersecting segments. */\r
- tspivot(fixuptri, fixupedge);\r
- if (fixupedge.sh == dummysh) {\r
- flip(&fixuptri); /* May create an inverted triangle on the left. */\r
- } else {\r
- /* We've collided with a segment between endpoint1 and endpoint2. */\r
- collision = 1;\r
- /* Insert a point at the intersection. */\r
- segmentintersection(&fixuptri, &fixupedge, endpoint2);\r
- done = 1;\r
- }\r
- }\r
- }\r
- } while (!done);\r
- /* Insert a shell edge to make the segment permanent. */\r
- insertshelle(&fixuptri, newmark);\r
- /* If there was a collision with an interceding vertex, install another */\r
- /* segment connecting that vertex with endpoint2. */\r
- if (collision) {\r
- /* Insert the remainder of the segment. */\r
- if (!scoutsegment(&fixuptri, endpoint2, newmark)) {\r
- constrainededge(&fixuptri, endpoint2, newmark);\r
- }\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* insertsegment() Insert a PSLG segment into a triangulation. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void insertsegment(endpoint1, endpoint2, newmark)\r
-point endpoint1;\r
-point endpoint2;\r
-int newmark;\r
-{\r
- struct triedge searchtri1, searchtri2;\r
- triangle encodedtri;\r
- point checkpoint;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- if (verbose > 1) {\r
- printf(" Connecting (%.12g, %.12g) to (%.12g, %.12g).\n",\r
- endpoint1[0], endpoint1[1], endpoint2[0], endpoint2[1]);\r
- }\r
-\r
- /* Find a triangle whose origin is the segment's first endpoint. */\r
- checkpoint = (point) NULL;\r
- encodedtri = point2tri(endpoint1);\r
- if (encodedtri != (triangle) NULL) {\r
- decode(encodedtri, searchtri1);\r
- org(searchtri1, checkpoint);\r
- }\r
- if (checkpoint != endpoint1) {\r
- /* Find a boundary triangle to search from. */\r
- searchtri1.tri = dummytri;\r
- searchtri1.orient = 0;\r
- symself(searchtri1);\r
- /* Search for the segment's first endpoint by point location. */\r
- if (locate(endpoint1, &searchtri1) != ONVERTEX) {\r
- printf(\r
- "Internal error in insertsegment(): Unable to locate PSLG point\n");\r
- printf(" (%.12g, %.12g) in triangulation.\n",\r
- endpoint1[0], endpoint1[1]);\r
- internalerror();\r
- }\r
- }\r
- /* Remember this triangle to improve subsequent point location. */\r
- triedgecopy(searchtri1, recenttri);\r
- /* Scout the beginnings of a path from the first endpoint */\r
- /* toward the second. */\r
- if (scoutsegment(&searchtri1, endpoint2, newmark)) {\r
- /* The segment was easily inserted. */\r
- return;\r
- }\r
- /* The first endpoint may have changed if a collision with an intervening */\r
- /* vertex on the segment occurred. */\r
- org(searchtri1, endpoint1);\r
-\r
- /* Find a triangle whose origin is the segment's second endpoint. */\r
- checkpoint = (point) NULL;\r
- encodedtri = point2tri(endpoint2);\r
- if (encodedtri != (triangle) NULL) {\r
- decode(encodedtri, searchtri2);\r
- org(searchtri2, checkpoint);\r
- }\r
- if (checkpoint != endpoint2) {\r
- /* Find a boundary triangle to search from. */\r
- searchtri2.tri = dummytri;\r
- searchtri2.orient = 0;\r
- symself(searchtri2);\r
- /* Search for the segment's second endpoint by point location. */\r
- if (locate(endpoint2, &searchtri2) != ONVERTEX) {\r
- printf(\r
- "Internal error in insertsegment(): Unable to locate PSLG point\n");\r
- printf(" (%.12g, %.12g) in triangulation.\n",\r
- endpoint2[0], endpoint2[1]);\r
- internalerror();\r
- }\r
- }\r
- /* Remember this triangle to improve subsequent point location. */\r
- triedgecopy(searchtri2, recenttri);\r
- /* Scout the beginnings of a path from the second endpoint */\r
- /* toward the first. */\r
- if (scoutsegment(&searchtri2, endpoint1, newmark)) {\r
- /* The segment was easily inserted. */\r
- return;\r
- }\r
- /* The second endpoint may have changed if a collision with an intervening */\r
- /* vertex on the segment occurred. */\r
- org(searchtri2, endpoint2);\r
-\r
-#ifndef REDUCED\r
-#ifndef CDT_ONLY\r
- if (splitseg) {\r
- /* Insert vertices to force the segment into the triangulation. */\r
- conformingedge(endpoint1, endpoint2, newmark);\r
- } else {\r
-#endif /* not CDT_ONLY */\r
-#endif /* not REDUCED */\r
- /* Insert the segment directly into the triangulation. */\r
- constrainededge(&searchtri1, endpoint2, newmark);\r
-#ifndef REDUCED\r
-#ifndef CDT_ONLY\r
- }\r
-#endif /* not CDT_ONLY */\r
-#endif /* not REDUCED */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* markhull() Cover the convex hull of a triangulation with shell edges. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void markhull()\r
-{\r
- struct triedge hulltri;\r
- struct triedge nexttri;\r
- struct triedge starttri;\r
- triangle ptr; /* Temporary variable used by sym() and oprev(). */\r
-\r
- /* Find a triangle handle on the hull. */\r
- hulltri.tri = dummytri;\r
- hulltri.orient = 0;\r
- symself(hulltri);\r
- /* Remember where we started so we know when to stop. */\r
- triedgecopy(hulltri, starttri);\r
- /* Go once counterclockwise around the convex hull. */\r
- do {\r
- /* Create a shell edge if there isn't already one here. */\r
- insertshelle(&hulltri, 1);\r
- /* To find the next hull edge, go clockwise around the next vertex. */\r
- lnextself(hulltri);\r
- oprev(hulltri, nexttri);\r
- while (nexttri.tri != dummytri) {\r
- triedgecopy(nexttri, hulltri);\r
- oprev(hulltri, nexttri);\r
- }\r
- } while (!triedgeequal(hulltri, starttri));\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* formskeleton() Create the shell edges of a triangulation, including */\r
-/* PSLG edges and edges on the convex hull. */\r
-/* */\r
-/* The PSLG edges are read from a .poly file. The return value is the */\r
-/* number of segments in the file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-int formskeleton(segmentlist, segmentmarkerlist, numberofsegments)\r
-int *segmentlist;\r
-int *segmentmarkerlist;\r
-int numberofsegments;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-int formskeleton(polyfile, polyfilename)\r
-FILE *polyfile;\r
-char *polyfilename;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- char polyfilename[6];\r
- int index;\r
-#else /* not TRILIBRARY */\r
- char inputline[INPUTLINESIZE];\r
- char *stringptr;\r
-#endif /* not TRILIBRARY */\r
- point endpoint1, endpoint2;\r
- int segments;\r
- int segmentmarkers;\r
- int end1, end2;\r
- int boundmarker;\r
- int i;\r
-\r
- if (poly) {\r
- if (!quiet) {\r
- printf("Inserting segments into Delaunay triangulation.\n");\r
- }\r
-#ifdef TRILIBRARY\r
- strcpy(polyfilename, "input");\r
- segments = numberofsegments;\r
- segmentmarkers = segmentmarkerlist != (int *) NULL;\r
- index = 0;\r
-#else /* not TRILIBRARY */\r
- /* Read the segments from a .poly file. */\r
- /* Read number of segments and number of boundary markers. */\r
- stringptr = readline(inputline, polyfile, polyfilename);\r
- segments = (int) strtol (stringptr, &stringptr, 0);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- segmentmarkers = 0;\r
- } else {\r
- segmentmarkers = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
-#endif /* not TRILIBRARY */\r
- /* If segments are to be inserted, compute a mapping */\r
- /* from points to triangles. */\r
- if (segments > 0) {\r
- if (verbose) {\r
- printf(" Inserting PSLG segments.\n");\r
- }\r
- makepointmap();\r
- }\r
-\r
- boundmarker = 0;\r
- /* Read and insert the segments. */\r
- for (i = 1; i <= segments; i++) {\r
-#ifdef TRILIBRARY\r
- end1 = segmentlist[index++];\r
- end2 = segmentlist[index++];\r
- if (segmentmarkers) {\r
- boundmarker = segmentmarkerlist[i - 1];\r
- }\r
-#else /* not TRILIBRARY */\r
- stringptr = readline(inputline, polyfile, inpolyfilename);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Segment %d has no endpoints in %s.\n", i,\r
- polyfilename);\r
- exit(1);\r
- } else {\r
- end1 = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Segment %d is missing its second endpoint in %s.\n", i,\r
- polyfilename);\r
- exit(1);\r
- } else {\r
- end2 = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- if (segmentmarkers) {\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- boundmarker = 0;\r
- } else {\r
- boundmarker = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- }\r
-#endif /* not TRILIBRARY */\r
- if ((end1 < firstnumber) || (end1 >= firstnumber + inpoints)) {\r
- if (!quiet) {\r
- printf("Warning: Invalid first endpoint of segment %d in %s.\n", i,\r
- polyfilename);\r
- }\r
- } else if ((end2 < firstnumber) || (end2 >= firstnumber + inpoints)) {\r
- if (!quiet) {\r
- printf("Warning: Invalid second endpoint of segment %d in %s.\n", i,\r
- polyfilename);\r
- }\r
- } else {\r
- endpoint1 = getpoint(end1);\r
- endpoint2 = getpoint(end2);\r
- if ((endpoint1[0] == endpoint2[0]) && (endpoint1[1] == endpoint2[1])) {\r
- if (!quiet) {\r
- printf("Warning: Endpoints of segment %d are coincident in %s.\n",\r
- i, polyfilename);\r
- }\r
- } else {\r
- insertsegment(endpoint1, endpoint2, boundmarker);\r
- }\r
- }\r
- }\r
- } else {\r
- segments = 0;\r
- }\r
- if (convex || !poly) {\r
- /* Enclose the convex hull with shell edges. */\r
- if (verbose) {\r
- printf(" Enclosing convex hull with segments.\n");\r
- }\r
- markhull();\r
- }\r
- return segments;\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Segment (shell edge) insertion ends here *********/\r
-\r
-/********* Carving out holes and concavities begins here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* infecthull() Virally infect all of the triangles of the convex hull */\r
-/* that are not protected by shell edges. Where there are */\r
-/* shell edges, set boundary markers as appropriate. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void infecthull()\r
-{\r
- struct triedge hulltri;\r
- struct triedge nexttri;\r
- struct triedge starttri;\r
- struct edge hulledge;\r
- triangle **deadtri;\r
- point horg, hdest;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- if (verbose) {\r
- printf(" Marking concavities (external triangles) for elimination.\n");\r
- }\r
- /* Find a triangle handle on the hull. */\r
- hulltri.tri = dummytri;\r
- hulltri.orient = 0;\r
- symself(hulltri);\r
- /* Remember where we started so we know when to stop. */\r
- triedgecopy(hulltri, starttri);\r
- /* Go once counterclockwise around the convex hull. */\r
- do {\r
- /* Ignore triangles that are already infected. */\r
- if (!infected(hulltri)) {\r
- /* Is the triangle protected by a shell edge? */\r
- tspivot(hulltri, hulledge);\r
- if (hulledge.sh == dummysh) {\r
- /* The triangle is not protected; infect it. */\r
- infect(hulltri);\r
- deadtri = (triangle **) poolalloc(&viri);\r
- *deadtri = hulltri.tri;\r
- } else {\r
- /* The triangle is protected; set boundary markers if appropriate. */\r
- if (mark(hulledge) == 0) {\r
- setmark(hulledge, 1);\r
- org(hulltri, horg);\r
- dest(hulltri, hdest);\r
- if (pointmark(horg) == 0) {\r
- setpointmark(horg, 1);\r
- }\r
- if (pointmark(hdest) == 0) {\r
- setpointmark(hdest, 1);\r
- }\r
- }\r
- }\r
- }\r
- /* To find the next hull edge, go clockwise around the next vertex. */\r
- lnextself(hulltri);\r
- oprev(hulltri, nexttri);\r
- while (nexttri.tri != dummytri) {\r
- triedgecopy(nexttri, hulltri);\r
- oprev(hulltri, nexttri);\r
- }\r
- } while (!triedgeequal(hulltri, starttri));\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* plague() Spread the virus from all infected triangles to any neighbors */\r
-/* not protected by shell edges. Delete all infected triangles. */\r
-/* */\r
-/* This is the procedure that actually creates holes and concavities. */\r
-/* */\r
-/* This procedure operates in two phases. The first phase identifies all */\r
-/* the triangles that will die, and marks them as infected. They are */\r
-/* marked to ensure that each triangle is added to the virus pool only */\r
-/* once, so the procedure will terminate. */\r
-/* */\r
-/* The second phase actually eliminates the infected triangles. It also */\r
-/* eliminates orphaned points. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void plague()\r
-{\r
- struct triedge testtri;\r
- struct triedge neighbor;\r
- triangle **virusloop;\r
- triangle **deadtri;\r
- struct edge neighborshelle;\r
- point testpoint;\r
- point norg, ndest;\r
- point deadorg, deaddest, deadapex;\r
- int killorg;\r
- triangle ptr; /* Temporary variable used by sym() and onext(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- if (verbose) {\r
- printf(" Marking neighbors of marked triangles.\n");\r
- }\r
- /* Loop through all the infected triangles, spreading the virus to */\r
- /* their neighbors, then to their neighbors' neighbors. */\r
- traversalinit(&viri);\r
- virusloop = (triangle **) traverse(&viri);\r
- while (virusloop != (triangle **) NULL) {\r
- testtri.tri = *virusloop;\r
- /* A triangle is marked as infected by messing with one of its shell */\r
- /* edges, setting it to an illegal value. Hence, we have to */\r
- /* temporarily uninfect this triangle so that we can examine its */\r
- /* adjacent shell edges. */\r
- uninfect(testtri);\r
- if (verbose > 2) {\r
- /* Assign the triangle an orientation for convenience in */\r
- /* checking its points. */\r
- testtri.orient = 0;\r
- org(testtri, deadorg);\r
- dest(testtri, deaddest);\r
- apex(testtri, deadapex);\r
- printf(" Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",\r
- deadorg[0], deadorg[1], deaddest[0], deaddest[1],\r
- deadapex[0], deadapex[1]);\r
- }\r
- /* Check each of the triangle's three neighbors. */\r
- for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {\r
- /* Find the neighbor. */\r
- sym(testtri, neighbor);\r
- /* Check for a shell between the triangle and its neighbor. */\r
- tspivot(testtri, neighborshelle);\r
- /* Check if the neighbor is nonexistent or already infected. */\r
- if ((neighbor.tri == dummytri) || infected(neighbor)) {\r
- if (neighborshelle.sh != dummysh) {\r
- /* There is a shell edge separating the triangle from its */\r
- /* neighbor, but both triangles are dying, so the shell */\r
- /* edge dies too. */\r
- shelledealloc(neighborshelle.sh);\r
- if (neighbor.tri != dummytri) {\r
- /* Make sure the shell edge doesn't get deallocated again */\r
- /* later when the infected neighbor is visited. */\r
- uninfect(neighbor);\r
- tsdissolve(neighbor);\r
- infect(neighbor);\r
- }\r
- }\r
- } else { /* The neighbor exists and is not infected. */\r
- if (neighborshelle.sh == dummysh) {\r
- /* There is no shell edge protecting the neighbor, so */\r
- /* the neighbor becomes infected. */\r
- if (verbose > 2) {\r
- org(neighbor, deadorg);\r
- dest(neighbor, deaddest);\r
- apex(neighbor, deadapex);\r
- printf(\r
- " Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",\r
- deadorg[0], deadorg[1], deaddest[0], deaddest[1],\r
- deadapex[0], deadapex[1]);\r
- }\r
- infect(neighbor);\r
- /* Ensure that the neighbor's neighbors will be infected. */\r
- deadtri = (triangle **) poolalloc(&viri);\r
- *deadtri = neighbor.tri;\r
- } else { /* The neighbor is protected by a shell edge. */\r
- /* Remove this triangle from the shell edge. */\r
- stdissolve(neighborshelle);\r
- /* The shell edge becomes a boundary. Set markers accordingly. */\r
- if (mark(neighborshelle) == 0) {\r
- setmark(neighborshelle, 1);\r
- }\r
- org(neighbor, norg);\r
- dest(neighbor, ndest);\r
- if (pointmark(norg) == 0) {\r
- setpointmark(norg, 1);\r
- }\r
- if (pointmark(ndest) == 0) {\r
- setpointmark(ndest, 1);\r
- }\r
- }\r
- }\r
- }\r
- /* Remark the triangle as infected, so it doesn't get added to the */\r
- /* virus pool again. */\r
- infect(testtri);\r
- virusloop = (triangle **) traverse(&viri);\r
- }\r
-\r
- if (verbose) {\r
- printf(" Deleting marked triangles.\n");\r
- }\r
- traversalinit(&viri);\r
- virusloop = (triangle **) traverse(&viri);\r
- while (virusloop != (triangle **) NULL) {\r
- testtri.tri = *virusloop;\r
-\r
- /* Check each of the three corners of the triangle for elimination. */\r
- /* This is done by walking around each point, checking if it is */\r
- /* still connected to at least one live triangle. */\r
- for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {\r
- org(testtri, testpoint);\r
- /* Check if the point has already been tested. */\r
- if (testpoint != (point) NULL) {\r
- killorg = 1;\r
- /* Mark the corner of the triangle as having been tested. */\r
- setorg(testtri, NULL);\r
- /* Walk counterclockwise about the point. */\r
- onext(testtri, neighbor);\r
- /* Stop upon reaching a boundary or the starting triangle. */\r
- while ((neighbor.tri != dummytri)\r
- && (!triedgeequal(neighbor, testtri))) {\r
- if (infected(neighbor)) {\r
- /* Mark the corner of this triangle as having been tested. */\r
- setorg(neighbor, NULL);\r
- } else {\r
- /* A live triangle. The point survives. */\r
- killorg = 0;\r
- }\r
- /* Walk counterclockwise about the point. */\r
- onextself(neighbor);\r
- }\r
- /* If we reached a boundary, we must walk clockwise as well. */\r
- if (neighbor.tri == dummytri) {\r
- /* Walk clockwise about the point. */\r
- oprev(testtri, neighbor);\r
- /* Stop upon reaching a boundary. */\r
- while (neighbor.tri != dummytri) {\r
- if (infected(neighbor)) {\r
- /* Mark the corner of this triangle as having been tested. */\r
- setorg(neighbor, NULL);\r
- } else {\r
- /* A live triangle. The point survives. */\r
- killorg = 0;\r
- }\r
- /* Walk clockwise about the point. */\r
- oprevself(neighbor);\r
- }\r
- }\r
- if (killorg) {\r
- if (verbose > 1) {\r
- printf(" Deleting point (%.12g, %.12g)\n",\r
- testpoint[0], testpoint[1]);\r
- }\r
- pointdealloc(testpoint);\r
- }\r
- }\r
- }\r
-\r
- /* Record changes in the number of boundary edges, and disconnect */\r
- /* dead triangles from their neighbors. */\r
- for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {\r
- sym(testtri, neighbor);\r
- if (neighbor.tri == dummytri) {\r
- /* There is no neighboring triangle on this edge, so this edge */\r
- /* is a boundary edge. This triangle is being deleted, so this */\r
- /* boundary edge is deleted. */\r
- hullsize--;\r
- } else {\r
- /* Disconnect the triangle from its neighbor. */\r
- dissolve(neighbor);\r
- /* There is a neighboring triangle on this edge, so this edge */\r
- /* becomes a boundary edge when this triangle is deleted. */\r
- hullsize++;\r
- }\r
- }\r
- /* Return the dead triangle to the pool of triangles. */\r
- triangledealloc(testtri.tri);\r
- virusloop = (triangle **) traverse(&viri);\r
- }\r
- /* Empty the virus pool. */\r
- poolrestart(&viri);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* regionplague() Spread regional attributes and/or area constraints */\r
-/* (from a .poly file) throughout the mesh. */\r
-/* */\r
-/* This procedure operates in two phases. The first phase spreads an */\r
-/* attribute and/or an area constraint through a (segment-bounded) region. */\r
-/* The triangles are marked to ensure that each triangle is added to the */\r
-/* virus pool only once, so the procedure will terminate. */\r
-/* */\r
-/* The second phase uninfects all infected triangles, returning them to */\r
-/* normal. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void regionplague(attribute, area)\r
-REAL attribute;\r
-REAL area;\r
-{\r
- struct triedge testtri;\r
- struct triedge neighbor;\r
- triangle **virusloop;\r
- triangle **regiontri;\r
- struct edge neighborshelle;\r
- point regionorg, regiondest, regionapex;\r
- triangle ptr; /* Temporary variable used by sym() and onext(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- if (verbose > 1) {\r
- printf(" Marking neighbors of marked triangles.\n");\r
- }\r
- /* Loop through all the infected triangles, spreading the attribute */\r
- /* and/or area constraint to their neighbors, then to their neighbors' */\r
- /* neighbors. */\r
- traversalinit(&viri);\r
- virusloop = (triangle **) traverse(&viri);\r
- while (virusloop != (triangle **) NULL) {\r
- testtri.tri = *virusloop;\r
- /* A triangle is marked as infected by messing with one of its shell */\r
- /* edges, setting it to an illegal value. Hence, we have to */\r
- /* temporarily uninfect this triangle so that we can examine its */\r
- /* adjacent shell edges. */\r
- uninfect(testtri);\r
- if (regionattrib) {\r
- /* Set an attribute. */\r
- setelemattribute(testtri, eextras, attribute);\r
- }\r
- if (vararea) {\r
- /* Set an area constraint. */\r
- setareabound(testtri, area);\r
- }\r
- if (verbose > 2) {\r
- /* Assign the triangle an orientation for convenience in */\r
- /* checking its points. */\r
- testtri.orient = 0;\r
- org(testtri, regionorg);\r
- dest(testtri, regiondest);\r
- apex(testtri, regionapex);\r
- printf(" Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",\r
- regionorg[0], regionorg[1], regiondest[0], regiondest[1],\r
- regionapex[0], regionapex[1]);\r
- }\r
- /* Check each of the triangle's three neighbors. */\r
- for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {\r
- /* Find the neighbor. */\r
- sym(testtri, neighbor);\r
- /* Check for a shell between the triangle and its neighbor. */\r
- tspivot(testtri, neighborshelle);\r
- /* Make sure the neighbor exists, is not already infected, and */\r
- /* isn't protected by a shell edge. */\r
- if ((neighbor.tri != dummytri) && !infected(neighbor)\r
- && (neighborshelle.sh == dummysh)) {\r
- if (verbose > 2) {\r
- org(neighbor, regionorg);\r
- dest(neighbor, regiondest);\r
- apex(neighbor, regionapex);\r
- printf(" Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",\r
- regionorg[0], regionorg[1], regiondest[0], regiondest[1],\r
- regionapex[0], regionapex[1]);\r
- }\r
- /* Infect the neighbor. */\r
- infect(neighbor);\r
- /* Ensure that the neighbor's neighbors will be infected. */\r
- regiontri = (triangle **) poolalloc(&viri);\r
- *regiontri = neighbor.tri;\r
- }\r
- }\r
- /* Remark the triangle as infected, so it doesn't get added to the */\r
- /* virus pool again. */\r
- infect(testtri);\r
- virusloop = (triangle **) traverse(&viri);\r
- }\r
-\r
- /* Uninfect all triangles. */\r
- if (verbose > 1) {\r
- printf(" Unmarking marked triangles.\n");\r
- }\r
- traversalinit(&viri);\r
- virusloop = (triangle **) traverse(&viri);\r
- while (virusloop != (triangle **) NULL) {\r
- testtri.tri = *virusloop;\r
- uninfect(testtri);\r
- virusloop = (triangle **) traverse(&viri);\r
- }\r
- /* Empty the virus pool. */\r
- poolrestart(&viri);\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* carveholes() Find the holes and infect them. Find the area */\r
-/* constraints and infect them. Infect the convex hull. */\r
-/* Spread the infection and kill triangles. Spread the */\r
-/* area constraints. */\r
-/* */\r
-/* This routine mainly calls other routines to carry out all these */\r
-/* functions. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void carveholes(holelist, holes, regionlist, regions)\r
-REAL *holelist;\r
-int holes;\r
-REAL *regionlist;\r
-int regions;\r
-{\r
- struct triedge searchtri;\r
- struct triedge triangleloop;\r
- struct triedge *regiontris;\r
- triangle **holetri;\r
- triangle **regiontri;\r
- point searchorg, searchdest;\r
- enum locateresult intersect;\r
- int i;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
- if (!(quiet || (noholes && convex))) {\r
- printf("Removing unwanted triangles.\n");\r
- if (verbose && (holes > 0)) {\r
- printf(" Marking holes for elimination.\n");\r
- }\r
- }\r
-\r
- if (regions > 0) {\r
- /* Allocate storage for the triangles in which region points fall. */\r
- regiontris = (struct triedge *) malloc(regions * sizeof(struct triedge));\r
- if (regiontris == (struct triedge *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
-\r
- if (((holes > 0) && !noholes) || !convex || (regions > 0)) {\r
- /* Initialize a pool of viri to be used for holes, concavities, */\r
- /* regional attributes, and/or regional area constraints. */\r
- poolinit(&viri, sizeof(triangle *), VIRUSPERBLOCK, POINTER, 0);\r
- }\r
-\r
- if (!convex) {\r
- /* Mark as infected any unprotected triangles on the boundary. */\r
- /* This is one way by which concavities are created. */\r
- infecthull();\r
- }\r
-\r
- if ((holes > 0) && !noholes) {\r
- /* Infect each triangle in which a hole lies. */\r
- for (i = 0; i < 2 * holes; i += 2) {\r
- /* Ignore holes that aren't within the bounds of the mesh. */\r
- if ((holelist[i] >= xmin) && (holelist[i] <= xmax)\r
- && (holelist[i + 1] >= ymin) && (holelist[i + 1] <= ymax)) {\r
- /* Start searching from some triangle on the outer boundary. */\r
- searchtri.tri = dummytri;\r
- searchtri.orient = 0;\r
- symself(searchtri);\r
- /* Ensure that the hole is to the left of this boundary edge; */\r
- /* otherwise, locate() will falsely report that the hole */\r
- /* falls within the starting triangle. */\r
- org(searchtri, searchorg);\r
- dest(searchtri, searchdest);\r
- if (counterclockwise(searchorg, searchdest, &holelist[i]) > 0.0) {\r
- /* Find a triangle that contains the hole. */\r
- intersect = locate(&holelist[i], &searchtri);\r
- if ((intersect != OUTSIDE) && (!infected(searchtri))) {\r
- /* Infect the triangle. This is done by marking the triangle */\r
- /* as infect and including the triangle in the virus pool. */\r
- infect(searchtri);\r
- holetri = (triangle **) poolalloc(&viri);\r
- *holetri = searchtri.tri;\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- /* Now, we have to find all the regions BEFORE we carve the holes, because */\r
- /* locate() won't work when the triangulation is no longer convex. */\r
- /* (Incidentally, this is the reason why regional attributes and area */\r
- /* constraints can't be used when refining a preexisting mesh, which */\r
- /* might not be convex; they can only be used with a freshly */\r
- /* triangulated PSLG.) */\r
- if (regions > 0) {\r
- /* Find the starting triangle for each region. */\r
- for (i = 0; i < regions; i++) {\r
- regiontris[i].tri = dummytri;\r
- /* Ignore region points that aren't within the bounds of the mesh. */\r
- if ((regionlist[4 * i] >= xmin) && (regionlist[4 * i] <= xmax) &&\r
- (regionlist[4 * i + 1] >= ymin) && (regionlist[4 * i + 1] <= ymax)) {\r
- /* Start searching from some triangle on the outer boundary. */\r
- searchtri.tri = dummytri;\r
- searchtri.orient = 0;\r
- symself(searchtri);\r
- /* Ensure that the region point is to the left of this boundary */\r
- /* edge; otherwise, locate() will falsely report that the */\r
- /* region point falls within the starting triangle. */\r
- org(searchtri, searchorg);\r
- dest(searchtri, searchdest);\r
- if (counterclockwise(searchorg, searchdest, ®ionlist[4 * i]) >\r
- 0.0) {\r
- /* Find a triangle that contains the region point. */\r
- intersect = locate(®ionlist[4 * i], &searchtri);\r
- if ((intersect != OUTSIDE) && (!infected(searchtri))) {\r
- /* Record the triangle for processing after the */\r
- /* holes have been carved. */\r
- triedgecopy(searchtri, regiontris[i]);\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- if (viri.items > 0) {\r
- /* Carve the holes and concavities. */\r
- plague();\r
- }\r
- /* The virus pool should be empty now. */\r
-\r
- if (regions > 0) {\r
- if (!quiet) {\r
- if (regionattrib) {\r
- if (vararea) {\r
- printf("Spreading regional attributes and area constraints.\n");\r
- } else {\r
- printf("Spreading regional attributes.\n");\r
- }\r
- } else { \r
- printf("Spreading regional area constraints.\n");\r
- }\r
- }\r
- if (regionattrib && !refine) {\r
- /* Assign every triangle a regional attribute of zero. */\r
- traversalinit(&triangles);\r
- triangleloop.orient = 0;\r
- triangleloop.tri = triangletraverse();\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- setelemattribute(triangleloop, eextras, 0.0);\r
- triangleloop.tri = triangletraverse();\r
- }\r
- }\r
- for (i = 0; i < regions; i++) {\r
- if (regiontris[i].tri != dummytri) {\r
- /* Make sure the triangle under consideration still exists. */\r
- /* It may have been eaten by the virus. */\r
- if (regiontris[i].tri[3] != (triangle) NULL) {\r
- /* Put one triangle in the virus pool. */\r
- infect(regiontris[i]);\r
- regiontri = (triangle **) poolalloc(&viri);\r
- *regiontri = regiontris[i].tri;\r
- /* Apply one region's attribute and/or area constraint. */\r
- regionplague(regionlist[4 * i + 2], regionlist[4 * i + 3]);\r
- /* The virus pool should be empty now. */\r
- }\r
- }\r
- }\r
- if (regionattrib && !refine) {\r
- /* Note the fact that each triangle has an additional attribute. */\r
- eextras++;\r
- }\r
- }\r
-\r
- /* Free up memory. */\r
- if (((holes > 0) && !noholes) || !convex || (regions > 0)) {\r
- pooldeinit(&viri);\r
- }\r
- if (regions > 0) {\r
- free(regiontris);\r
- }\r
-}\r
-\r
-/** **/\r
-/** **/\r
-/********* Carving out holes and concavities ends here *********/\r
-\r
-/********* Mesh quality maintenance begins here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* tallyencs() Traverse the entire list of shell edges, check each edge */\r
-/* to see if it is encroached. If so, add it to the list. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void tallyencs()\r
-{\r
- struct edge edgeloop;\r
- int dummy;\r
-\r
- traversalinit(&shelles);\r
- edgeloop.shorient = 0;\r
- edgeloop.sh = shelletraverse();\r
- while (edgeloop.sh != (shelle *) NULL) {\r
- /* If the segment is encroached, add it to the list. */\r
- dummy = checkedge4encroach(&edgeloop);\r
- edgeloop.sh = shelletraverse();\r
- }\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* precisionerror() Print an error message for precision problems. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void precisionerror()\r
-{\r
- printf("Try increasing the area criterion and/or reducing the minimum\n");\r
- printf(" allowable angle so that tiny triangles are not created.\n");\r
-#ifdef SINGLE\r
- printf("Alternatively, try recompiling me with double precision\n");\r
- printf(" arithmetic (by removing \"#define SINGLE\" from the\n");\r
- printf(" source file or \"-DSINGLE\" from the makefile).\n");\r
-#endif /* SINGLE */\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* repairencs() Find and repair all the encroached segments. */\r
-/* */\r
-/* Encroached segments are repaired by splitting them by inserting a point */\r
-/* at or near their centers. */\r
-/* */\r
-/* `flaws' is a flag that specifies whether one should take note of new */\r
-/* encroached segments and bad triangles that result from inserting points */\r
-/* to repair existing encroached segments. */\r
-/* */\r
-/* When a segment is split, the two resulting subsegments are always */\r
-/* tested to see if they are encroached upon, regardless of the value */\r
-/* of `flaws'. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void repairencs(flaws)\r
-int flaws;\r
-{\r
- struct triedge enctri;\r
- struct triedge testtri;\r
- struct edge *encloop;\r
- struct edge testsh;\r
- point eorg, edest;\r
- point newpoint;\r
- enum insertsiteresult success;\r
- REAL segmentlength, nearestpoweroftwo;\r
- REAL split;\r
- int acuteorg, acutedest;\r
- int dummy;\r
- int i;\r
- triangle ptr; /* Temporary variable used by stpivot(). */\r
- shelle sptr; /* Temporary variable used by snext(). */\r
-\r
- while ((badsegments.items > 0) && (steinerleft != 0)) {\r
- traversalinit(&badsegments);\r
- encloop = badsegmenttraverse();\r
- while ((encloop != (struct edge *) NULL) && (steinerleft != 0)) {\r
- /* To decide where to split a segment, we need to know if the */\r
- /* segment shares an endpoint with an adjacent segment. */\r
- /* The concern is that, if we simply split every encroached */\r
- /* segment in its center, two adjacent segments with a small */\r
- /* angle between them might lead to an infinite loop; each */\r
- /* point added to split one segment will encroach upon the */\r
- /* other segment, which must then be split with a point that */\r
- /* will encroach upon the first segment, and so on forever. */\r
- /* To avoid this, imagine a set of concentric circles, whose */\r
- /* radii are powers of two, about each segment endpoint. */\r
- /* These concentric circles determine where the segment is */\r
- /* split. (If both endpoints are shared with adjacent */\r
- /* segments, split the segment in the middle, and apply the */\r
- /* concentric shells for later splittings.) */\r
-\r
- /* Is the origin shared with another segment? */\r
- stpivot(*encloop, enctri);\r
- lnext(enctri, testtri);\r
- tspivot(testtri, testsh);\r
- acuteorg = testsh.sh != dummysh;\r
- /* Is the destination shared with another segment? */\r
- lnextself(testtri);\r
- tspivot(testtri, testsh);\r
- acutedest = testsh.sh != dummysh;\r
- /* Now, check the other side of the segment, if there's a triangle */\r
- /* there. */\r
- sym(enctri, testtri);\r
- if (testtri.tri != dummytri) {\r
- /* Is the destination shared with another segment? */\r
- lnextself(testtri);\r
- tspivot(testtri, testsh);\r
- acutedest = acutedest || (testsh.sh != dummysh);\r
- /* Is the origin shared with another segment? */\r
- lnextself(testtri);\r
- tspivot(testtri, testsh);\r
- acuteorg = acuteorg || (testsh.sh != dummysh);\r
- }\r
-\r
- sorg(*encloop, eorg);\r
- sdest(*encloop, edest);\r
- /* Use the concentric circles if exactly one endpoint is shared */\r
- /* with another adjacent segment. */\r
- if (acuteorg ^ acutedest) {\r
- segmentlength = sqrt((edest[0] - eorg[0]) * (edest[0] - eorg[0])\r
- + (edest[1] - eorg[1]) * (edest[1] - eorg[1]));\r
- /* Find the power of two nearest the segment's length. */\r
- nearestpoweroftwo = 1.0;\r
- while (segmentlength > SQUAREROOTTWO * nearestpoweroftwo) {\r
- nearestpoweroftwo *= 2.0;\r
- }\r
- while (segmentlength < (0.5 * SQUAREROOTTWO) * nearestpoweroftwo) {\r
- nearestpoweroftwo *= 0.5;\r
- }\r
- /* Where do we split the segment? */\r
- split = 0.5 * nearestpoweroftwo / segmentlength;\r
- if (acutedest) {\r
- split = 1.0 - split;\r
- }\r
- } else {\r
- /* If we're not worried about adjacent segments, split */\r
- /* this segment in the middle. */\r
- split = 0.5;\r
- }\r
-\r
- /* Create the new point. */\r
- newpoint = (point) poolalloc(&points);\r
- /* Interpolate its coordinate and attributes. */\r
- for (i = 0; i < 2 + nextras; i++) {\r
- newpoint[i] = (1.0 - split) * eorg[i] + split * edest[i];\r
- }\r
- setpointmark(newpoint, mark(*encloop));\r
- if (verbose > 1) {\r
- printf(\r
- " Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",\r
- eorg[0], eorg[1], edest[0], edest[1], newpoint[0], newpoint[1]);\r
- }\r
- /* Check whether the new point lies on an endpoint. */\r
- if (((newpoint[0] == eorg[0]) && (newpoint[1] == eorg[1]))\r
- || ((newpoint[0] == edest[0]) && (newpoint[1] == edest[1]))) {\r
- printf("Error: Ran out of precision at (%.12g, %.12g).\n",\r
- newpoint[0], newpoint[1]);\r
- printf("I attempted to split a segment to a smaller size than can\n");\r
- printf(" be accommodated by the finite precision of floating point\n"\r
- );\r
- printf(" arithmetic.\n");\r
- precisionerror();\r
- exit(1);\r
- }\r
- /* Insert the splitting point. This should always succeed. */\r
- success = insertsite(newpoint, &enctri, encloop, flaws, flaws);\r
- if ((success != SUCCESSFULPOINT) && (success != ENCROACHINGPOINT)) {\r
- printf("Internal error in repairencs():\n");\r
- printf(" Failure to split a segment.\n");\r
- internalerror();\r
- }\r
- if (steinerleft > 0) {\r
- steinerleft--;\r
- }\r
- /* Check the two new subsegments to see if they're encroached. */\r
- dummy = checkedge4encroach(encloop);\r
- snextself(*encloop);\r
- dummy = checkedge4encroach(encloop);\r
-\r
- badsegmentdealloc(encloop);\r
- encloop = badsegmenttraverse();\r
- }\r
- }\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* tallyfaces() Test every triangle in the mesh for quality measures. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void tallyfaces()\r
-{\r
- struct triedge triangleloop;\r
-\r
- if (verbose) {\r
- printf(" Making a list of bad triangles.\n");\r
- }\r
- traversalinit(&triangles);\r
- triangleloop.orient = 0;\r
- triangleloop.tri = triangletraverse();\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- /* If the triangle is bad, enqueue it. */\r
- testtriangle(&triangleloop);\r
- triangleloop.tri = triangletraverse();\r
- }\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* findcircumcenter() Find the circumcenter of a triangle. */\r
-/* */\r
-/* The result is returned both in terms of x-y coordinates and xi-eta */\r
-/* coordinates. The xi-eta coordinate system is defined in terms of the */\r
-/* triangle: the origin of the triangle is the origin of the coordinate */\r
-/* system; the destination of the triangle is one unit along the xi axis; */\r
-/* and the apex of the triangle is one unit along the eta axis. */\r
-/* */\r
-/* The return value indicates which edge of the triangle is shortest. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-enum circumcenterresult findcircumcenter(torg, tdest, tapex, circumcenter,\r
- xi, eta)\r
-point torg;\r
-point tdest;\r
-point tapex;\r
-point circumcenter;\r
-REAL *xi;\r
-REAL *eta;\r
-{\r
- REAL xdo, ydo, xao, yao, xad, yad;\r
- REAL dodist, aodist, addist;\r
- REAL denominator;\r
- REAL dx, dy;\r
-\r
- circumcentercount++;\r
-\r
- /* Compute the circumcenter of the triangle. */\r
- xdo = tdest[0] - torg[0];\r
- ydo = tdest[1] - torg[1];\r
- xao = tapex[0] - torg[0];\r
- yao = tapex[1] - torg[1];\r
- dodist = xdo * xdo + ydo * ydo;\r
- aodist = xao * xao + yao * yao;\r
- if (noexact) {\r
- denominator = (REAL)(0.5 / (xdo * yao - xao * ydo));\r
- } else {\r
- /* Use the counterclockwise() routine to ensure a positive (and */\r
- /* reasonably accurate) result, avoiding any possibility of */\r
- /* division by zero. */\r
- denominator = (REAL)(0.5 / counterclockwise(tdest, tapex, torg));\r
- /* Don't count the above as an orientation test. */\r
- counterclockcount--;\r
- }\r
- circumcenter[0] = torg[0] - (ydo * aodist - yao * dodist) * denominator; \r
- circumcenter[1] = torg[1] + (xdo * aodist - xao * dodist) * denominator; \r
-\r
- /* To interpolate point attributes for the new point inserted at */\r
- /* the circumcenter, define a coordinate system with a xi-axis, */\r
- /* directed from the triangle's origin to its destination, and */\r
- /* an eta-axis, directed from its origin to its apex. */\r
- /* Calculate the xi and eta coordinates of the circumcenter. */\r
- dx = circumcenter[0] - torg[0];\r
- dy = circumcenter[1] - torg[1];\r
- *xi = (REAL)((dx * yao - xao * dy) * (2.0 * denominator));\r
- *eta = (REAL)((xdo * dy - dx * ydo) * (2.0 * denominator));\r
-\r
- xad = tapex[0] - tdest[0];\r
- yad = tapex[1] - tdest[1];\r
- addist = xad * xad + yad * yad;\r
- if ((addist < dodist) && (addist < aodist)) {\r
- return OPPOSITEORG;\r
- } else if (dodist < aodist) {\r
- return OPPOSITEAPEX;\r
- } else {\r
- return OPPOSITEDEST;\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* splittriangle() Inserts a point at the circumcenter of a triangle. */\r
-/* Deletes the newly inserted point if it encroaches upon */\r
-/* a segment. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void splittriangle(badtri)\r
-struct badface *badtri;\r
-{\r
- point borg, bdest, bapex;\r
- point newpoint;\r
- REAL xi, eta;\r
- enum insertsiteresult success;\r
- enum circumcenterresult shortedge;\r
- int errorflag;\r
- int i;\r
-\r
- org(badtri->badfacetri, borg);\r
- dest(badtri->badfacetri, bdest);\r
- apex(badtri->badfacetri, bapex);\r
- /* Make sure that this triangle is still the same triangle it was */\r
- /* when it was tested and determined to be of bad quality. */\r
- /* Subsequent transformations may have made it a different triangle. */\r
- if ((borg == badtri->faceorg) && (bdest == badtri->facedest) &&\r
- (bapex == badtri->faceapex)) {\r
- if (verbose > 1) {\r
- printf(" Splitting this triangle at its circumcenter:\n");\r
- printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", borg[0],\r
- borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);\r
- }\r
- errorflag = 0;\r
- /* Create a new point at the triangle's circumcenter. */\r
- newpoint = (point) poolalloc(&points);\r
- shortedge = findcircumcenter(borg, bdest, bapex, newpoint, &xi, &eta);\r
- /* Check whether the new point lies on a triangle vertex. */\r
- if (((newpoint[0] == borg[0]) && (newpoint[1] == borg[1]))\r
- || ((newpoint[0] == bdest[0]) && (newpoint[1] == bdest[1]))\r
- || ((newpoint[0] == bapex[0]) && (newpoint[1] == bapex[1]))) {\r
- if (!quiet) {\r
- printf("Warning: New point (%.12g, %.12g) falls on existing vertex.\n"\r
- , newpoint[0], newpoint[1]);\r
- errorflag = 1;\r
- }\r
- pointdealloc(newpoint);\r
- } else {\r
- for (i = 2; i < 2 + nextras; i++) {\r
- /* Interpolate the point attributes at the circumcenter. */\r
- newpoint[i] = borg[i] + xi * (bdest[i] - borg[i])\r
- + eta * (bapex[i] - borg[i]);\r
- }\r
- /* The new point must be in the interior, and have a marker of zero. */\r
- setpointmark(newpoint, 0);\r
- /* Ensure that the handle `badtri->badfacetri' represents the shortest */\r
- /* edge of the triangle. This ensures that the circumcenter must */\r
- /* fall to the left of this edge, so point location will work. */\r
- if (shortedge == OPPOSITEORG) {\r
- lnextself(badtri->badfacetri);\r
- } else if (shortedge == OPPOSITEDEST) {\r
- lprevself(badtri->badfacetri);\r
- }\r
- /* Insert the circumcenter, searching from the edge of the triangle, */\r
- /* and maintain the Delaunay property of the triangulation. */\r
- success = insertsite(newpoint, &(badtri->badfacetri),\r
- (struct edge *) NULL, 1, 1);\r
- if (success == SUCCESSFULPOINT) {\r
- if (steinerleft > 0) {\r
- steinerleft--;\r
- }\r
- } else if (success == ENCROACHINGPOINT) {\r
- /* If the newly inserted point encroaches upon a segment, delete it. */\r
- deletesite(&(badtri->badfacetri));\r
- } else if (success == VIOLATINGPOINT) {\r
- /* Failed to insert the new point, but some segment was */\r
- /* marked as being encroached. */\r
- pointdealloc(newpoint);\r
- } else { /* success == DUPLICATEPOINT */\r
- /* Failed to insert the new point because a vertex is already there. */\r
- if (!quiet) {\r
- printf(\r
- "Warning: New point (%.12g, %.12g) falls on existing vertex.\n"\r
- , newpoint[0], newpoint[1]);\r
- errorflag = 1;\r
- }\r
- pointdealloc(newpoint);\r
- }\r
- }\r
- if (errorflag) {\r
- if (verbose) {\r
- printf(" The new point is at the circumcenter of triangle\n");\r
- printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",\r
- borg[0], borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);\r
- }\r
- printf("This probably means that I am trying to refine triangles\n");\r
- printf(" to a smaller size than can be accommodated by the finite\n");\r
- printf(" precision of floating point arithmetic. (You can be\n");\r
- printf(" sure of this if I fail to terminate.)\n");\r
- precisionerror();\r
- }\r
- }\r
- /* Return the bad triangle to the pool. */\r
- pooldealloc(&badtriangles, (VOID *) badtri);\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* enforcequality() Remove all the encroached edges and bad triangles */\r
-/* from the triangulation. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef CDT_ONLY\r
-\r
-void enforcequality()\r
-{\r
- int i;\r
-\r
- if (!quiet) {\r
- printf("Adding Steiner points to enforce quality.\n");\r
- }\r
- /* Initialize the pool of encroached segments. */\r
- poolinit(&badsegments, sizeof(struct edge), BADSEGMENTPERBLOCK, POINTER, 0);\r
- if (verbose) {\r
- printf(" Looking for encroached segments.\n");\r
- }\r
- /* Test all segments to see if they're encroached. */\r
- tallyencs();\r
- if (verbose && (badsegments.items > 0)) {\r
- printf(" Splitting encroached segments.\n");\r
- }\r
- /* Note that steinerleft == -1 if an unlimited number */\r
- /* of Steiner points is allowed. */\r
- while ((badsegments.items > 0) && (steinerleft != 0)) {\r
- /* Fix the segments without noting newly encroached segments or */\r
- /* bad triangles. The reason we don't want to note newly */\r
- /* encroached segments is because some encroached segments are */\r
- /* likely to be noted multiple times, and would then be blindly */\r
- /* split multiple times. I should fix that some time. */\r
- repairencs(0);\r
- /* Now, find all the segments that became encroached while adding */\r
- /* points to split encroached segments. */\r
- tallyencs();\r
- }\r
- /* At this point, if we haven't run out of Steiner points, the */\r
- /* triangulation should be (conforming) Delaunay. */\r
-\r
- /* Next, we worry about enforcing triangle quality. */\r
- if ((minangle > 0.0) || vararea || fixedarea) {\r
- /* Initialize the pool of bad triangles. */\r
- poolinit(&badtriangles, sizeof(struct badface), BADTRIPERBLOCK, POINTER,\r
- 0);\r
- /* Initialize the queues of bad triangles. */\r
- for (i = 0; i < 64; i++) {\r
- queuefront[i] = (struct badface *) NULL;\r
- queuetail[i] = &queuefront[i];\r
- }\r
- /* Test all triangles to see if they're bad. */\r
- tallyfaces();\r
- if (verbose) {\r
- printf(" Splitting bad triangles.\n");\r
- }\r
- while ((badtriangles.items > 0) && (steinerleft != 0)) {\r
- /* Fix one bad triangle by inserting a point at its circumcenter. */\r
- splittriangle(dequeuebadtri());\r
- /* Fix any encroached segments that may have resulted. Record */\r
- /* any new bad triangles or encroached segments that result. */\r
- if (badsegments.items > 0) {\r
- repairencs(1);\r
- }\r
- }\r
- }\r
- /* At this point, if we haven't run out of Steiner points, the */\r
- /* triangulation should be (conforming) Delaunay and have no */\r
- /* low-quality triangles. */\r
-\r
- /* Might we have run out of Steiner points too soon? */\r
- if (!quiet && (badsegments.items > 0) && (steinerleft == 0)) {\r
- printf("\nWarning: I ran out of Steiner points, but the mesh has\n");\r
- if (badsegments.items == 1) {\r
- printf(" an encroached segment, and therefore might not be truly\n");\r
- } else {\r
- printf(" %ld encroached segments, and therefore might not be truly\n",\r
- badsegments.items);\r
- }\r
- printf(" Delaunay. If the Delaunay property is important to you,\n");\r
- printf(" try increasing the number of Steiner points (controlled by\n");\r
- printf(" the -S switch) slightly and try again.\n\n");\r
- }\r
-}\r
-\r
-#endif /* not CDT_ONLY */\r
-\r
-/** **/\r
-/** **/\r
-/********* Mesh quality maintenance ends here *********/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* highorder() Create extra nodes for quadratic subparametric elements. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void highorder()\r
-{\r
- struct triedge triangleloop, trisym;\r
- struct edge checkmark;\r
- point newpoint;\r
- point torg, tdest;\r
- int i;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
- if (!quiet) {\r
- printf("Adding vertices for second-order triangles.\n");\r
- }\r
- /* The following line ensures that dead items in the pool of nodes */\r
- /* cannot be allocated for the extra nodes associated with high */\r
- /* order elements. This ensures that the primary nodes (at the */\r
- /* corners of elements) will occur earlier in the output files, and */\r
- /* have lower indices, than the extra nodes. */\r
- points.deaditemstack = (VOID *) NULL;\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- /* To loop over the set of edges, loop over all triangles, and look at */\r
- /* the three edges of each triangle. If there isn't another triangle */\r
- /* adjacent to the edge, operate on the edge. If there is another */\r
- /* adjacent triangle, operate on the edge only if the current triangle */\r
- /* has a smaller pointer than its neighbor. This way, each edge is */\r
- /* considered only once. */\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- for (triangleloop.orient = 0; triangleloop.orient < 3;\r
- triangleloop.orient++) {\r
- sym(triangleloop, trisym);\r
- if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {\r
- org(triangleloop, torg);\r
- dest(triangleloop, tdest);\r
- /* Create a new node in the middle of the edge. Interpolate */\r
- /* its attributes. */\r
- newpoint = (point) poolalloc(&points);\r
- for (i = 0; i < 2 + nextras; i++) {\r
- newpoint[i] = (REAL)(0.5 * (torg[i] + tdest[i]));\r
- }\r
- /* Set the new node's marker to zero or one, depending on */\r
- /* whether it lies on a boundary. */\r
- setpointmark(newpoint, trisym.tri == dummytri);\r
- if (useshelles) {\r
- tspivot(triangleloop, checkmark);\r
- /* If this edge is a segment, transfer the marker to the new node. */\r
- if (checkmark.sh != dummysh) {\r
- setpointmark(newpoint, mark(checkmark));\r
- }\r
- }\r
- if (verbose > 1) {\r
- printf(" Creating (%.12g, %.12g).\n", newpoint[0], newpoint[1]);\r
- }\r
- /* Record the new node in the (one or two) adjacent elements. */\r
- triangleloop.tri[highorderindex + triangleloop.orient] =\r
- (triangle) newpoint;\r
- if (trisym.tri != dummytri) {\r
- trisym.tri[highorderindex + trisym.orient] = (triangle) newpoint;\r
- }\r
- }\r
- }\r
- triangleloop.tri = triangletraverse();\r
- }\r
-}\r
-\r
-/********* File I/O routines begin here *********/\r
-/** **/\r
-/** **/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* readline() Read a nonempty line from a file. */\r
-/* */\r
-/* A line is considered "nonempty" if it contains something that looks like */\r
-/* a number. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-char *readline(string, infile, infilename)\r
-char *string;\r
-FILE *infile;\r
-char *infilename;\r
-{\r
- char *result;\r
-\r
- /* Search for something that looks like a number. */\r
- do {\r
- result = fgets(string, INPUTLINESIZE, infile);\r
- if (result == (char *) NULL) {\r
- printf(" Error: Unexpected end of file in %s.\n", infilename);\r
- exit(1);\r
- }\r
- /* Skip anything that doesn't look like a number, a comment, */\r
- /* or the end of a line. */\r
- while ((*result != '\0') && (*result != '#')\r
- && (*result != '.') && (*result != '+') && (*result != '-')\r
- && ((*result < '0') || (*result > '9'))) {\r
- result++;\r
- }\r
- /* If it's a comment or end of line, read another line and try again. */\r
- } while ((*result == '#') || (*result == '\0'));\r
- return result;\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* findfield() Find the next field of a string. */\r
-/* */\r
-/* Jumps past the current field by searching for whitespace, then jumps */\r
-/* past the whitespace to find the next field. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-char *findfield(string)\r
-char *string;\r
-{\r
- char *result;\r
-\r
- result = string;\r
- /* Skip the current field. Stop upon reaching whitespace. */\r
- while ((*result != '\0') && (*result != '#')\r
- && (*result != ' ') && (*result != '\t')) {\r
- result++;\r
- }\r
- /* Now skip the whitespace and anything else that doesn't look like a */\r
- /* number, a comment, or the end of a line. */\r
- while ((*result != '\0') && (*result != '#')\r
- && (*result != '.') && (*result != '+') && (*result != '-')\r
- && ((*result < '0') || (*result > '9'))) {\r
- result++;\r
- }\r
- /* Check for a comment (prefixed with `#'). */\r
- if (*result == '#') {\r
- *result = '\0';\r
- }\r
- return result;\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* readnodes() Read the points from a file, which may be a .node or .poly */\r
-/* file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-void readnodes(nodefilename, polyfilename, polyfile)\r
-char *nodefilename;\r
-char *polyfilename;\r
-FILE **polyfile;\r
-{\r
- FILE *infile;\r
- point pointloop;\r
- char inputline[INPUTLINESIZE];\r
- char *stringptr;\r
- char *infilename;\r
- REAL x, y;\r
- int firstnode;\r
- int nodemarkers;\r
- int currentmarker;\r
- int i, j;\r
-\r
- if (poly) {\r
- /* Read the points from a .poly file. */\r
- if (!quiet) {\r
- printf("Opening %s.\n", polyfilename);\r
- }\r
- *polyfile = fopen(polyfilename, "r");\r
- if (*polyfile == (FILE *) NULL) {\r
- printf(" Error: Cannot access file %s.\n", polyfilename);\r
- exit(1);\r
- }\r
- /* Read number of points, number of dimensions, number of point */\r
- /* attributes, and number of boundary markers. */\r
- stringptr = readline(inputline, *polyfile, polyfilename);\r
- inpoints = (int) strtol (stringptr, &stringptr, 0);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- mesh_dim = 2;\r
- } else {\r
- mesh_dim = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- nextras = 0;\r
- } else {\r
- nextras = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- nodemarkers = 0;\r
- } else {\r
- nodemarkers = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- if (inpoints > 0) {\r
- infile = *polyfile;\r
- infilename = polyfilename;\r
- readnodefile = 0;\r
- } else {\r
- /* If the .poly file claims there are zero points, that means that */\r
- /* the points should be read from a separate .node file. */\r
- readnodefile = 1;\r
- infilename = innodefilename;\r
- }\r
- } else {\r
- readnodefile = 1;\r
- infilename = innodefilename;\r
- *polyfile = (FILE *) NULL;\r
- }\r
-\r
- if (readnodefile) {\r
- /* Read the points from a .node file. */\r
- if (!quiet) {\r
- printf("Opening %s.\n", innodefilename);\r
- }\r
- infile = fopen(innodefilename, "r");\r
- if (infile == (FILE *) NULL) {\r
- printf(" Error: Cannot access file %s.\n", innodefilename);\r
- exit(1);\r
- }\r
- /* Read number of points, number of dimensions, number of point */\r
- /* attributes, and number of boundary markers. */\r
- stringptr = readline(inputline, infile, innodefilename);\r
- inpoints = (int) strtol (stringptr, &stringptr, 0);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- mesh_dim = 2;\r
- } else {\r
- mesh_dim = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- nextras = 0;\r
- } else {\r
- nextras = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- nodemarkers = 0;\r
- } else {\r
- nodemarkers = (int) strtol (stringptr, &stringptr, 0);\r
- }\r
- }\r
-\r
- if (inpoints < 3) {\r
- printf("Error: Input must have at least three input points.\n");\r
- exit(1);\r
- }\r
- if (mesh_dim != 2) {\r
- printf("Error: Triangle only works with two-dimensional meshes.\n");\r
- exit(1);\r
- }\r
-\r
- initializepointpool();\r
-\r
- /* Read the points. */\r
- for (i = 0; i < inpoints; i++) {\r
- pointloop = (point) poolalloc(&points);\r
- stringptr = readline(inputline, infile, infilename);\r
- if (i == 0) {\r
- firstnode = (int) strtol (stringptr, &stringptr, 0);\r
- if ((firstnode == 0) || (firstnode == 1)) {\r
- firstnumber = firstnode;\r
- }\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Point %d has no x coordinate.\n", firstnumber + i);\r
- exit(1);\r
- }\r
- x = (REAL) strtod(stringptr, &stringptr);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Point %d has no y coordinate.\n", firstnumber + i);\r
- exit(1);\r
- }\r
- y = (REAL) strtod(stringptr, &stringptr);\r
- pointloop[0] = x;\r
- pointloop[1] = y;\r
- /* Read the point attributes. */\r
- for (j = 2; j < 2 + nextras; j++) {\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- pointloop[j] = 0.0;\r
- } else {\r
- pointloop[j] = (REAL) strtod(stringptr, &stringptr);\r
- }\r
- }\r
- if (nodemarkers) {\r
- /* Read a point marker. */\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- setpointmark(pointloop, 0);\r
- } else {\r
- currentmarker = (int) strtol (stringptr, &stringptr, 0);\r
- setpointmark(pointloop, currentmarker);\r
- }\r
- } else {\r
- /* If no markers are specified in the file, they default to zero. */\r
- setpointmark(pointloop, 0);\r
- }\r
- /* Determine the smallest and largest x and y coordinates. */\r
- if (i == 0) {\r
- xmin = xmax = x;\r
- ymin = ymax = y;\r
- } else {\r
- xmin = (x < xmin) ? x : xmin;\r
- xmax = (x > xmax) ? x : xmax;\r
- ymin = (y < ymin) ? y : ymin;\r
- ymax = (y > ymax) ? y : ymax;\r
- }\r
- }\r
- if (readnodefile) {\r
- fclose(infile);\r
- }\r
-\r
- /* Nonexistent x value used as a flag to mark circle events in sweepline */\r
- /* Delaunay algorithm. */\r
- xminextreme = 10 * xmin - 9 * xmax;\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* transfernodes() Read the points from memory. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void transfernodes(pointlist, pointattriblist, pointmarkerlist, numberofpoints,\r
- numberofpointattribs)\r
-REAL *pointlist;\r
-REAL *pointattriblist;\r
-int *pointmarkerlist;\r
-int numberofpoints;\r
-int numberofpointattribs;\r
-{\r
- point pointloop;\r
- REAL x, y;\r
- int i, j;\r
- int coordindex;\r
- int attribindex;\r
-\r
- inpoints = numberofpoints;\r
- mesh_dim = 2;\r
- nextras = numberofpointattribs;\r
- readnodefile = 0;\r
- if (inpoints < 3) {\r
- printf("Error: Input must have at least three input points.\n");\r
- exit(1);\r
- }\r
-\r
- initializepointpool();\r
-\r
- /* Read the points. */\r
- coordindex = 0;\r
- attribindex = 0;\r
- for (i = 0; i < inpoints; i++) {\r
- pointloop = (point) poolalloc(&points);\r
- /* Read the point coordinates. */\r
- x = pointloop[0] = pointlist[coordindex++];\r
- y = pointloop[1] = pointlist[coordindex++];\r
- /* Read the point attributes. */\r
- for (j = 0; j < numberofpointattribs; j++) {\r
- pointloop[2 + j] = pointattriblist[attribindex++];\r
- }\r
- if (pointmarkerlist != (int *) NULL) {\r
- /* Read a point marker. */\r
- setpointmark(pointloop, pointmarkerlist[i]);\r
- } else {\r
- /* If no markers are specified, they default to zero. */\r
- setpointmark(pointloop, 0);\r
- }\r
- x = pointloop[0];\r
- y = pointloop[1];\r
- /* Determine the smallest and largest x and y coordinates. */\r
- if (i == 0) {\r
- xmin = xmax = x;\r
- ymin = ymax = y;\r
- } else {\r
- xmin = (x < xmin) ? x : xmin;\r
- xmax = (x > xmax) ? x : xmax;\r
- ymin = (y < ymin) ? y : ymin;\r
- ymax = (y > ymax) ? y : ymax;\r
- }\r
- }\r
-\r
- /* Nonexistent x value used as a flag to mark circle events in sweepline */\r
- /* Delaunay algorithm. */\r
- xminextreme = 10 * xmin - 9 * xmax;\r
-}\r
-\r
-#endif /* TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* readholes() Read the holes, and possibly regional attributes and area */\r
-/* constraints, from a .poly file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-void readholes(polyfile, polyfilename, hlist, holes, rlist, regions)\r
-FILE *polyfile;\r
-char *polyfilename;\r
-REAL **hlist;\r
-int *holes;\r
-REAL **rlist;\r
-int *regions;\r
-{\r
- REAL *holelist;\r
- REAL *regionlist;\r
- char inputline[INPUTLINESIZE];\r
- char *stringptr;\r
- int index;\r
- int i;\r
-\r
- /* Read the holes. */\r
- stringptr = readline(inputline, polyfile, polyfilename);\r
- *holes = (int) strtol (stringptr, &stringptr, 0);\r
- if (*holes > 0) {\r
- holelist = (REAL *) malloc(2 * *holes * sizeof(REAL));\r
- *hlist = holelist;\r
- if (holelist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- for (i = 0; i < 2 * *holes; i += 2) {\r
- stringptr = readline(inputline, polyfile, polyfilename);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Hole %d has no x coordinate.\n",\r
- firstnumber + (i >> 1));\r
- exit(1);\r
- } else {\r
- holelist[i] = (REAL) strtod(stringptr, &stringptr);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Hole %d has no y coordinate.\n",\r
- firstnumber + (i >> 1));\r
- exit(1);\r
- } else {\r
- holelist[i + 1] = (REAL) strtod(stringptr, &stringptr);\r
- }\r
- }\r
- } else {\r
- *hlist = (REAL *) NULL;\r
- }\r
-\r
-#ifndef CDT_ONLY\r
- if ((regionattrib || vararea) && !refine) {\r
- /* Read the area constraints. */\r
- stringptr = readline(inputline, polyfile, polyfilename);\r
- *regions = (int) strtol (stringptr, &stringptr, 0);\r
- if (*regions > 0) {\r
- regionlist = (REAL *) malloc(4 * *regions * sizeof(REAL));\r
- *rlist = regionlist;\r
- if (regionlist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- index = 0;\r
- for (i = 0; i < *regions; i++) {\r
- stringptr = readline(inputline, polyfile, polyfilename);\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Region %d has no x coordinate.\n",\r
- firstnumber + i);\r
- exit(1);\r
- } else {\r
- regionlist[index++] = (REAL) strtod(stringptr, &stringptr);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf("Error: Region %d has no y coordinate.\n",\r
- firstnumber + i);\r
- exit(1);\r
- } else {\r
- regionlist[index++] = (REAL) strtod(stringptr, &stringptr);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- printf(\r
- "Error: Region %d has no region attribute or area constraint.\n",\r
- firstnumber + i);\r
- exit(1);\r
- } else {\r
- regionlist[index++] = (REAL) strtod(stringptr, &stringptr);\r
- }\r
- stringptr = findfield(stringptr);\r
- if (*stringptr == '\0') {\r
- regionlist[index] = regionlist[index - 1];\r
- } else {\r
- regionlist[index] = (REAL) strtod(stringptr, &stringptr);\r
- }\r
- index++;\r
- }\r
- }\r
- } else {\r
- /* Set `*regions' to zero to avoid an accidental free() later. */\r
- *regions = 0;\r
- *rlist = (REAL *) NULL;\r
- }\r
-#endif /* not CDT_ONLY */\r
-\r
- fclose(polyfile);\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* finishfile() Write the command line to the output file so the user */\r
-/* can remember how the file was generated. Close the file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-void finishfile(outfile, argc, argv)\r
-FILE *outfile;\r
-int argc;\r
-char **argv;\r
-{\r
- int i;\r
-\r
- fprintf(outfile, "# Generated by");\r
- for (i = 0; i < argc; i++) {\r
- fprintf(outfile, " ");\r
- fputs(argv[i], outfile);\r
- }\r
- fprintf(outfile, "\n");\r
- fclose(outfile);\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* writenodes() Number the points and write them to a .node file. */\r
-/* */\r
-/* To save memory, the point numbers are written over the shell markers */\r
-/* after the points are written to a file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void writenodes(pointlist, pointattriblist, pointmarkerlist)\r
-REAL **pointlist;\r
-REAL **pointattriblist;\r
-int **pointmarkerlist;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-void writenodes(nodefilename, argc, argv)\r
-char *nodefilename;\r
-int argc;\r
-char **argv;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- REAL *plist;\r
- REAL *palist;\r
- int *pmlist;\r
- int coordindex;\r
- int attribindex;\r
-#else /* not TRILIBRARY */\r
- FILE *outfile;\r
-#endif /* not TRILIBRARY */\r
- point pointloop;\r
- int pointnumber;\r
- int i;\r
-\r
-#ifdef TRILIBRARY\r
- if (!quiet) {\r
- printf("Writing points.\n");\r
- }\r
- /* Allocate memory for output points if necessary. */\r
- if (*pointlist == (REAL *) NULL) {\r
- *pointlist = (REAL *) malloc(points.items * 2 * sizeof(REAL));\r
- if (*pointlist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- /* Allocate memory for output point attributes if necessary. */\r
- if ((nextras > 0) && (*pointattriblist == (REAL *) NULL)) {\r
- *pointattriblist = (REAL *) malloc(points.items * nextras * sizeof(REAL));\r
- if (*pointattriblist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- /* Allocate memory for output point markers if necessary. */\r
- if (!nobound && (*pointmarkerlist == (int *) NULL)) {\r
- *pointmarkerlist = (int *) malloc(points.items * sizeof(int));\r
- if (*pointmarkerlist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- plist = *pointlist;\r
- palist = *pointattriblist;\r
- pmlist = *pointmarkerlist;\r
- coordindex = 0;\r
- attribindex = 0;\r
-#else /* not TRILIBRARY */\r
- if (!quiet) {\r
- printf("Writing %s.\n", nodefilename);\r
- }\r
- outfile = fopen(nodefilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", nodefilename);\r
- exit(1);\r
- }\r
- /* Number of points, number of dimensions, number of point attributes, */\r
- /* and number of boundary markers (zero or one). */\r
- fprintf(outfile, "%ld %d %d %d\n", points.items, mesh_dim, nextras,\r
- 1 - nobound);\r
-#endif /* not TRILIBRARY */\r
-\r
- traversalinit(&points);\r
- pointloop = pointtraverse();\r
- pointnumber = firstnumber;\r
- while (pointloop != (point) NULL) {\r
-#ifdef TRILIBRARY\r
- /* X and y coordinates. */\r
- plist[coordindex++] = pointloop[0];\r
- plist[coordindex++] = pointloop[1];\r
- /* Point attributes. */\r
- for (i = 0; i < nextras; i++) {\r
- palist[attribindex++] = pointloop[2 + i];\r
- }\r
- if (!nobound) {\r
- /* Copy the boundary marker. */\r
- pmlist[pointnumber - firstnumber] = pointmark(pointloop);\r
- }\r
-#else /* not TRILIBRARY */\r
- /* Point number, x and y coordinates. */\r
- fprintf(outfile, "%4d %.17g %.17g", pointnumber, pointloop[0],\r
- pointloop[1]);\r
- for (i = 0; i < nextras; i++) {\r
- /* Write an attribute. */\r
- fprintf(outfile, " %.17g", pointloop[i + 2]);\r
- }\r
- if (nobound) {\r
- fprintf(outfile, "\n");\r
- } else {\r
- /* Write the boundary marker. */\r
- fprintf(outfile, " %d\n", pointmark(pointloop));\r
- }\r
-#endif /* not TRILIBRARY */\r
-\r
- setpointmark(pointloop, pointnumber);\r
- pointloop = pointtraverse();\r
- pointnumber++;\r
- }\r
-\r
-#ifndef TRILIBRARY\r
- finishfile(outfile, argc, argv);\r
-#endif /* not TRILIBRARY */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* numbernodes() Number the points. */\r
-/* */\r
-/* Each point is assigned a marker equal to its number. */\r
-/* */\r
-/* Used when writenodes() is not called because no .node file is written. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void numbernodes()\r
-{\r
- point pointloop;\r
- int pointnumber;\r
-\r
- traversalinit(&points);\r
- pointloop = pointtraverse();\r
- pointnumber = firstnumber;\r
- while (pointloop != (point) NULL) {\r
- setpointmark(pointloop, pointnumber);\r
- pointloop = pointtraverse();\r
- pointnumber++;\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* writeelements() Write the triangles to an .ele file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void writeelements(trianglelist, triangleattriblist)\r
-int **trianglelist;\r
-REAL **triangleattriblist;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-void writeelements(elefilename, argc, argv)\r
-char *elefilename;\r
-int argc;\r
-char **argv;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- int *tlist;\r
- REAL *talist;\r
- int pointindex;\r
- int attribindex;\r
-#else /* not TRILIBRARY */\r
- FILE *outfile;\r
-#endif /* not TRILIBRARY */\r
- struct triedge triangleloop;\r
- point p1, p2, p3;\r
- point mid1, mid2, mid3;\r
- int elementnumber;\r
- int i;\r
-\r
-#ifdef TRILIBRARY\r
- if (!quiet) {\r
- printf("Writing triangles.\n");\r
- }\r
- /* Allocate memory for output triangles if necessary. */\r
- if (*trianglelist == (int *) NULL) {\r
- *trianglelist = (int *) malloc(triangles.items *\r
- ((order + 1) * (order + 2) / 2) * sizeof(int));\r
- if (*trianglelist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- /* Allocate memory for output triangle attributes if necessary. */\r
- if ((eextras > 0) && (*triangleattriblist == (REAL *) NULL)) {\r
- *triangleattriblist = (REAL *) malloc(triangles.items * eextras *\r
- sizeof(REAL));\r
- if (*triangleattriblist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- tlist = *trianglelist;\r
- talist = *triangleattriblist;\r
- pointindex = 0;\r
- attribindex = 0;\r
-#else /* not TRILIBRARY */\r
- if (!quiet) {\r
- printf("Writing %s.\n", elefilename);\r
- }\r
- outfile = fopen(elefilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", elefilename);\r
- exit(1);\r
- }\r
- /* Number of triangles, points per triangle, attributes per triangle. */\r
- fprintf(outfile, "%ld %d %d\n", triangles.items,\r
- (order + 1) * (order + 2) / 2, eextras);\r
-#endif /* not TRILIBRARY */\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- triangleloop.orient = 0;\r
- elementnumber = firstnumber;\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- org(triangleloop, p1);\r
- dest(triangleloop, p2);\r
- apex(triangleloop, p3);\r
- if (order == 1) {\r
-#ifdef TRILIBRARY\r
- tlist[pointindex++] = pointmark(p1);\r
- tlist[pointindex++] = pointmark(p2);\r
- tlist[pointindex++] = pointmark(p3);\r
-#else /* not TRILIBRARY */\r
- /* Triangle number, indices for three points. */\r
- fprintf(outfile, "%4d %4d %4d %4d", elementnumber,\r
- pointmark(p1), pointmark(p2), pointmark(p3));\r
-#endif /* not TRILIBRARY */\r
- } else {\r
- mid1 = (point) triangleloop.tri[highorderindex + 1];\r
- mid2 = (point) triangleloop.tri[highorderindex + 2];\r
- mid3 = (point) triangleloop.tri[highorderindex];\r
-#ifdef TRILIBRARY\r
- tlist[pointindex++] = pointmark(p1);\r
- tlist[pointindex++] = pointmark(p2);\r
- tlist[pointindex++] = pointmark(p3);\r
- tlist[pointindex++] = pointmark(mid1);\r
- tlist[pointindex++] = pointmark(mid2);\r
- tlist[pointindex++] = pointmark(mid3);\r
-#else /* not TRILIBRARY */\r
- /* Triangle number, indices for six points. */\r
- fprintf(outfile, "%4d %4d %4d %4d %4d %4d %4d", elementnumber,\r
- pointmark(p1), pointmark(p2), pointmark(p3), pointmark(mid1),\r
- pointmark(mid2), pointmark(mid3));\r
-#endif /* not TRILIBRARY */\r
- }\r
-\r
-#ifdef TRILIBRARY\r
- for (i = 0; i < eextras; i++) {\r
- talist[attribindex++] = elemattribute(triangleloop, i);\r
- }\r
-#else /* not TRILIBRARY */\r
- for (i = 0; i < eextras; i++) {\r
- fprintf(outfile, " %.17g", elemattribute(triangleloop, i));\r
- }\r
- fprintf(outfile, "\n");\r
-#endif /* not TRILIBRARY */\r
-\r
- triangleloop.tri = triangletraverse();\r
- elementnumber++;\r
- }\r
-\r
-#ifndef TRILIBRARY\r
- finishfile(outfile, argc, argv);\r
-#endif /* not TRILIBRARY */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* writepoly() Write the segments and holes to a .poly file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void writepoly(segmentlist, segmentmarkerlist)\r
-int **segmentlist;\r
-int **segmentmarkerlist;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-void writepoly(polyfilename, holelist, holes, regionlist, regions, argc, argv)\r
-char *polyfilename;\r
-REAL *holelist;\r
-int holes;\r
-REAL *regionlist;\r
-int regions;\r
-int argc;\r
-char **argv;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- int *slist;\r
- int *smlist;\r
- int index;\r
-#else /* not TRILIBRARY */\r
- FILE *outfile;\r
- int i;\r
-#endif /* not TRILIBRARY */\r
- struct edge shelleloop;\r
- point endpoint1, endpoint2;\r
- int shellenumber;\r
-\r
-#ifdef TRILIBRARY\r
- if (!quiet) {\r
- printf("Writing segments.\n");\r
- }\r
- /* Allocate memory for output segments if necessary. */\r
- if (*segmentlist == (int *) NULL) {\r
- *segmentlist = (int *) malloc(shelles.items * 2 * sizeof(int));\r
- if (*segmentlist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- /* Allocate memory for output segment markers if necessary. */\r
- if (!nobound && (*segmentmarkerlist == (int *) NULL)) {\r
- *segmentmarkerlist = (int *) malloc(shelles.items * sizeof(int));\r
- if (*segmentmarkerlist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- slist = *segmentlist;\r
- smlist = *segmentmarkerlist;\r
- index = 0;\r
-#else /* not TRILIBRARY */\r
- if (!quiet) {\r
- printf("Writing %s.\n", polyfilename);\r
- }\r
- outfile = fopen(polyfilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", polyfilename);\r
- exit(1);\r
- }\r
- /* The zero indicates that the points are in a separate .node file. */\r
- /* Followed by number of dimensions, number of point attributes, */\r
- /* and number of boundary markers (zero or one). */\r
- fprintf(outfile, "%d %d %d %d\n", 0, mesh_dim, nextras, 1 - nobound);\r
- /* Number of segments, number of boundary markers (zero or one). */\r
- fprintf(outfile, "%ld %d\n", shelles.items, 1 - nobound);\r
-#endif /* not TRILIBRARY */\r
-\r
- traversalinit(&shelles);\r
- shelleloop.sh = shelletraverse();\r
- shelleloop.shorient = 0;\r
- shellenumber = firstnumber;\r
- while (shelleloop.sh != (shelle *) NULL) {\r
- sorg(shelleloop, endpoint1);\r
- sdest(shelleloop, endpoint2);\r
-#ifdef TRILIBRARY\r
- /* Copy indices of the segment's two endpoints. */\r
- slist[index++] = pointmark(endpoint1);\r
- slist[index++] = pointmark(endpoint2);\r
- if (!nobound) {\r
- /* Copy the boundary marker. */\r
- smlist[shellenumber - firstnumber] = mark(shelleloop);\r
- }\r
-#else /* not TRILIBRARY */\r
- /* Segment number, indices of its two endpoints, and possibly a marker. */\r
- if (nobound) {\r
- fprintf(outfile, "%4d %4d %4d\n", shellenumber,\r
- pointmark(endpoint1), pointmark(endpoint2));\r
- } else {\r
- fprintf(outfile, "%4d %4d %4d %4d\n", shellenumber,\r
- pointmark(endpoint1), pointmark(endpoint2), mark(shelleloop));\r
- }\r
-#endif /* not TRILIBRARY */\r
-\r
- shelleloop.sh = shelletraverse();\r
- shellenumber++;\r
- }\r
-\r
-#ifndef TRILIBRARY\r
-#ifndef CDT_ONLY\r
- fprintf(outfile, "%d\n", holes);\r
- if (holes > 0) {\r
- for (i = 0; i < holes; i++) {\r
- /* Hole number, x and y coordinates. */\r
- fprintf(outfile, "%4d %.17g %.17g\n", firstnumber + i,\r
- holelist[2 * i], holelist[2 * i + 1]);\r
- }\r
- }\r
- if (regions > 0) {\r
- fprintf(outfile, "%d\n", regions);\r
- for (i = 0; i < regions; i++) {\r
- /* Region number, x and y coordinates, attribute, maximum area. */\r
- fprintf(outfile, "%4d %.17g %.17g %.17g %.17g\n", firstnumber + i,\r
- regionlist[4 * i], regionlist[4 * i + 1],\r
- regionlist[4 * i + 2], regionlist[4 * i + 3]);\r
- }\r
- }\r
-#endif /* not CDT_ONLY */\r
-\r
- finishfile(outfile, argc, argv);\r
-#endif /* not TRILIBRARY */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* writeedges() Write the edges to a .edge file. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void writeedges(edgelist, edgemarkerlist)\r
-int **edgelist;\r
-int **edgemarkerlist;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-void writeedges(edgefilename, argc, argv)\r
-char *edgefilename;\r
-int argc;\r
-char **argv;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- int *elist;\r
- int *emlist;\r
- int index;\r
-#else /* not TRILIBRARY */\r
- FILE *outfile;\r
-#endif /* not TRILIBRARY */\r
- struct triedge triangleloop, trisym;\r
- struct edge checkmark;\r
- point p1, p2;\r
- int edgenumber;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
- shelle sptr; /* Temporary variable used by tspivot(). */\r
-\r
-#ifdef TRILIBRARY\r
- if (!quiet) {\r
- printf("Writing edges.\n");\r
- }\r
- /* Allocate memory for edges if necessary. */\r
- if (*edgelist == (int *) NULL) {\r
- *edgelist = (int *) malloc(edges * 2 * sizeof(int));\r
- if (*edgelist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- /* Allocate memory for edge markers if necessary. */\r
- if (!nobound && (*edgemarkerlist == (int *) NULL)) {\r
- *edgemarkerlist = (int *) malloc(edges * sizeof(int));\r
- if (*edgemarkerlist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- elist = *edgelist;\r
- emlist = *edgemarkerlist;\r
- index = 0;\r
-#else /* not TRILIBRARY */\r
- if (!quiet) {\r
- printf("Writing %s.\n", edgefilename);\r
- }\r
- outfile = fopen(edgefilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", edgefilename);\r
- exit(1);\r
- }\r
- /* Number of edges, number of boundary markers (zero or one). */\r
- fprintf(outfile, "%ld %d\n", edges, 1 - nobound);\r
-#endif /* not TRILIBRARY */\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- edgenumber = firstnumber;\r
- /* To loop over the set of edges, loop over all triangles, and look at */\r
- /* the three edges of each triangle. If there isn't another triangle */\r
- /* adjacent to the edge, operate on the edge. If there is another */\r
- /* adjacent triangle, operate on the edge only if the current triangle */\r
- /* has a smaller pointer than its neighbor. This way, each edge is */\r
- /* considered only once. */\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- for (triangleloop.orient = 0; triangleloop.orient < 3;\r
- triangleloop.orient++) {\r
- sym(triangleloop, trisym);\r
- if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {\r
- org(triangleloop, p1);\r
- dest(triangleloop, p2);\r
-#ifdef TRILIBRARY\r
- elist[index++] = pointmark(p1);\r
- elist[index++] = pointmark(p2);\r
-#endif /* TRILIBRARY */\r
- if (nobound) {\r
-#ifndef TRILIBRARY\r
- /* Edge number, indices of two endpoints. */\r
- fprintf(outfile, "%4d %d %d\n", edgenumber,\r
- pointmark(p1), pointmark(p2));\r
-#endif /* not TRILIBRARY */\r
- } else {\r
- /* Edge number, indices of two endpoints, and a boundary marker. */\r
- /* If there's no shell edge, the boundary marker is zero. */\r
- if (useshelles) {\r
- tspivot(triangleloop, checkmark);\r
- if (checkmark.sh == dummysh) {\r
-#ifdef TRILIBRARY\r
- emlist[edgenumber - firstnumber] = 0;\r
-#else /* not TRILIBRARY */\r
- fprintf(outfile, "%4d %d %d %d\n", edgenumber,\r
- pointmark(p1), pointmark(p2), 0);\r
-#endif /* not TRILIBRARY */\r
- } else {\r
-#ifdef TRILIBRARY\r
- emlist[edgenumber - firstnumber] = mark(checkmark);\r
-#else /* not TRILIBRARY */\r
- fprintf(outfile, "%4d %d %d %d\n", edgenumber,\r
- pointmark(p1), pointmark(p2), mark(checkmark));\r
-#endif /* not TRILIBRARY */\r
- }\r
- } else {\r
-#ifdef TRILIBRARY\r
- emlist[edgenumber - firstnumber] = trisym.tri == dummytri;\r
-#else /* not TRILIBRARY */\r
- fprintf(outfile, "%4d %d %d %d\n", edgenumber,\r
- pointmark(p1), pointmark(p2), trisym.tri == dummytri);\r
-#endif /* not TRILIBRARY */\r
- }\r
- }\r
- edgenumber++;\r
- }\r
- }\r
- triangleloop.tri = triangletraverse();\r
- }\r
-\r
-#ifndef TRILIBRARY\r
- finishfile(outfile, argc, argv);\r
-#endif /* not TRILIBRARY */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* writevoronoi() Write the Voronoi diagram to a .v.node and .v.edge */\r
-/* file. */\r
-/* */\r
-/* The Voronoi diagram is the geometric dual of the Delaunay triangulation. */\r
-/* Hence, the Voronoi vertices are listed by traversing the Delaunay */\r
-/* triangles, and the Voronoi edges are listed by traversing the Delaunay */\r
-/* edges. */\r
-/* */\r
-/* WARNING: In order to assign numbers to the Voronoi vertices, this */\r
-/* procedure messes up the shell edges or the extra nodes of every */\r
-/* element. Hence, you should call this procedure last. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void writevoronoi(vpointlist, vpointattriblist, vpointmarkerlist, vedgelist,\r
- vedgemarkerlist, vnormlist)\r
-REAL **vpointlist;\r
-REAL **vpointattriblist;\r
-int **vpointmarkerlist;\r
-int **vedgelist;\r
-int **vedgemarkerlist;\r
-REAL **vnormlist;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-void writevoronoi(vnodefilename, vedgefilename, argc, argv)\r
-char *vnodefilename;\r
-char *vedgefilename;\r
-int argc;\r
-char **argv;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- REAL *plist;\r
- REAL *palist;\r
- int *elist;\r
- REAL *normlist;\r
- int coordindex;\r
- int attribindex;\r
-#else /* not TRILIBRARY */\r
- FILE *outfile;\r
-#endif /* not TRILIBRARY */\r
- struct triedge triangleloop, trisym;\r
- point torg, tdest, tapex;\r
- REAL circumcenter[2];\r
- REAL xi, eta;\r
- int vnodenumber, vedgenumber;\r
- int p1, p2;\r
- int i;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
-#ifdef TRILIBRARY\r
- if (!quiet) {\r
- printf("Writing Voronoi vertices.\n");\r
- }\r
- /* Allocate memory for Voronoi vertices if necessary. */\r
- if (*vpointlist == (REAL *) NULL) {\r
- *vpointlist = (REAL *) malloc(triangles.items * 2 * sizeof(REAL));\r
- if (*vpointlist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- /* Allocate memory for Voronoi vertex attributes if necessary. */\r
- if (*vpointattriblist == (REAL *) NULL) {\r
- *vpointattriblist = (REAL *) malloc(triangles.items * nextras *\r
- sizeof(REAL));\r
- if (*vpointattriblist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- *vpointmarkerlist = (int *) NULL;\r
- plist = *vpointlist;\r
- palist = *vpointattriblist;\r
- coordindex = 0;\r
- attribindex = 0;\r
-#else /* not TRILIBRARY */\r
- if (!quiet) {\r
- printf("Writing %s.\n", vnodefilename);\r
- }\r
- outfile = fopen(vnodefilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", vnodefilename);\r
- exit(1);\r
- }\r
- /* Number of triangles, two dimensions, number of point attributes, */\r
- /* zero markers. */\r
- fprintf(outfile, "%ld %d %d %d\n", triangles.items, 2, nextras, 0);\r
-#endif /* not TRILIBRARY */\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- triangleloop.orient = 0;\r
- vnodenumber = firstnumber;\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- org(triangleloop, torg);\r
- dest(triangleloop, tdest);\r
- apex(triangleloop, tapex);\r
- findcircumcenter(torg, tdest, tapex, circumcenter, &xi, &eta);\r
-#ifdef TRILIBRARY\r
- /* X and y coordinates. */\r
- plist[coordindex++] = circumcenter[0];\r
- plist[coordindex++] = circumcenter[1];\r
- for (i = 2; i < 2 + nextras; i++) {\r
- /* Interpolate the point attributes at the circumcenter. */\r
- palist[attribindex++] = torg[i] + xi * (tdest[i] - torg[i])\r
- + eta * (tapex[i] - torg[i]);\r
- }\r
-#else /* not TRILIBRARY */\r
- /* Voronoi vertex number, x and y coordinates. */\r
- fprintf(outfile, "%4d %.17g %.17g", vnodenumber, circumcenter[0],\r
- circumcenter[1]);\r
- for (i = 2; i < 2 + nextras; i++) {\r
- /* Interpolate the point attributes at the circumcenter. */\r
- fprintf(outfile, " %.17g", torg[i] + xi * (tdest[i] - torg[i])\r
- + eta * (tapex[i] - torg[i]));\r
- }\r
- fprintf(outfile, "\n");\r
-#endif /* not TRILIBRARY */\r
-\r
- * (int *) (triangleloop.tri + 6) = vnodenumber;\r
- triangleloop.tri = triangletraverse();\r
- vnodenumber++;\r
- }\r
-\r
-#ifndef TRILIBRARY\r
- finishfile(outfile, argc, argv);\r
-#endif /* not TRILIBRARY */\r
-\r
-#ifdef TRILIBRARY\r
- if (!quiet) {\r
- printf("Writing Voronoi edges.\n");\r
- }\r
- /* Allocate memory for output Voronoi edges if necessary. */\r
- if (*vedgelist == (int *) NULL) {\r
- *vedgelist = (int *) malloc(edges * 2 * sizeof(int));\r
- if (*vedgelist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- *vedgemarkerlist = (int *) NULL;\r
- /* Allocate memory for output Voronoi norms if necessary. */\r
- if (*vnormlist == (REAL *) NULL) {\r
- *vnormlist = (REAL *) malloc(edges * 2 * sizeof(REAL));\r
- if (*vnormlist == (REAL *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- elist = *vedgelist;\r
- normlist = *vnormlist;\r
- coordindex = 0;\r
-#else /* not TRILIBRARY */\r
- if (!quiet) {\r
- printf("Writing %s.\n", vedgefilename);\r
- }\r
- outfile = fopen(vedgefilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", vedgefilename);\r
- exit(1);\r
- }\r
- /* Number of edges, zero boundary markers. */\r
- fprintf(outfile, "%ld %d\n", edges, 0);\r
-#endif /* not TRILIBRARY */\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- vedgenumber = firstnumber;\r
- /* To loop over the set of edges, loop over all triangles, and look at */\r
- /* the three edges of each triangle. If there isn't another triangle */\r
- /* adjacent to the edge, operate on the edge. If there is another */\r
- /* adjacent triangle, operate on the edge only if the current triangle */\r
- /* has a smaller pointer than its neighbor. This way, each edge is */\r
- /* considered only once. */\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- for (triangleloop.orient = 0; triangleloop.orient < 3;\r
- triangleloop.orient++) {\r
- sym(triangleloop, trisym);\r
- if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {\r
- /* Find the number of this triangle (and Voronoi vertex). */\r
- p1 = * (int *) (triangleloop.tri + 6);\r
- if (trisym.tri == dummytri) {\r
- org(triangleloop, torg);\r
- dest(triangleloop, tdest);\r
-#ifdef TRILIBRARY\r
- /* Copy an infinite ray. Index of one endpoint, and -1. */\r
- elist[coordindex] = p1;\r
- normlist[coordindex++] = tdest[1] - torg[1];\r
- elist[coordindex] = -1;\r
- normlist[coordindex++] = torg[0] - tdest[0];\r
-#else /* not TRILIBRARY */\r
- /* Write an infinite ray. Edge number, index of one endpoint, -1, */\r
- /* and x and y coordinates of a vector representing the */\r
- /* direction of the ray. */\r
- fprintf(outfile, "%4d %d %d %.17g %.17g\n", vedgenumber,\r
- p1, -1, tdest[1] - torg[1], torg[0] - tdest[0]);\r
-#endif /* not TRILIBRARY */\r
- } else {\r
- /* Find the number of the adjacent triangle (and Voronoi vertex). */\r
- p2 = * (int *) (trisym.tri + 6);\r
- /* Finite edge. Write indices of two endpoints. */\r
-#ifdef TRILIBRARY\r
- elist[coordindex] = p1;\r
- normlist[coordindex++] = 0.0;\r
- elist[coordindex] = p2;\r
- normlist[coordindex++] = 0.0;\r
-#else /* not TRILIBRARY */\r
- fprintf(outfile, "%4d %d %d\n", vedgenumber, p1, p2);\r
-#endif /* not TRILIBRARY */\r
- }\r
- vedgenumber++;\r
- }\r
- }\r
- triangleloop.tri = triangletraverse();\r
- }\r
-\r
-#ifndef TRILIBRARY\r
- finishfile(outfile, argc, argv);\r
-#endif /* not TRILIBRARY */\r
-}\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void writeneighbors(neighborlist)\r
-int **neighborlist;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-void writeneighbors(neighborfilename, argc, argv)\r
-char *neighborfilename;\r
-int argc;\r
-char **argv;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
-#ifdef TRILIBRARY\r
- int *nlist;\r
- int index;\r
-#else /* not TRILIBRARY */\r
- FILE *outfile;\r
-#endif /* not TRILIBRARY */\r
- struct triedge triangleloop, trisym;\r
- int elementnumber;\r
- int neighbor1, neighbor2, neighbor3;\r
- triangle ptr; /* Temporary variable used by sym(). */\r
-\r
-#ifdef TRILIBRARY\r
- if (!quiet) {\r
- printf("Writing neighbors.\n");\r
- }\r
- /* Allocate memory for neighbors if necessary. */\r
- if (*neighborlist == (int *) NULL) {\r
- *neighborlist = (int *) malloc(triangles.items * 3 * sizeof(int));\r
- if (*neighborlist == (int *) NULL) {\r
- printf("Error: Out of memory.\n");\r
- exit(1);\r
- }\r
- }\r
- nlist = *neighborlist;\r
- index = 0;\r
-#else /* not TRILIBRARY */\r
- if (!quiet) {\r
- printf("Writing %s.\n", neighborfilename);\r
- }\r
- outfile = fopen(neighborfilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", neighborfilename);\r
- exit(1);\r
- }\r
- /* Number of triangles, three edges per triangle. */\r
- fprintf(outfile, "%ld %d\n", triangles.items, 3);\r
-#endif /* not TRILIBRARY */\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- triangleloop.orient = 0;\r
- elementnumber = firstnumber;\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- * (int *) (triangleloop.tri + 6) = elementnumber;\r
- triangleloop.tri = triangletraverse();\r
- elementnumber++;\r
- }\r
- * (int *) (dummytri + 6) = -1;\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- elementnumber = firstnumber;\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- triangleloop.orient = 1;\r
- sym(triangleloop, trisym);\r
- neighbor1 = * (int *) (trisym.tri + 6);\r
- triangleloop.orient = 2;\r
- sym(triangleloop, trisym);\r
- neighbor2 = * (int *) (trisym.tri + 6);\r
- triangleloop.orient = 0;\r
- sym(triangleloop, trisym);\r
- neighbor3 = * (int *) (trisym.tri + 6);\r
-#ifdef TRILIBRARY\r
- nlist[index++] = neighbor1;\r
- nlist[index++] = neighbor2;\r
- nlist[index++] = neighbor3;\r
-#else /* not TRILIBRARY */\r
- /* Triangle number, neighboring triangle numbers. */\r
- fprintf(outfile, "%4d %d %d %d\n", elementnumber,\r
- neighbor1, neighbor2, neighbor3);\r
-#endif /* not TRILIBRARY */\r
-\r
- triangleloop.tri = triangletraverse();\r
- elementnumber++;\r
- }\r
-\r
-#ifndef TRILIBRARY\r
- finishfile(outfile, argc, argv);\r
-#endif /* TRILIBRARY */\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* writeoff() Write the triangulation to an .off file. */\r
-/* */\r
-/* OFF stands for the Object File Format, a format used by the Geometry */\r
-/* Center's Geomview package. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifndef TRILIBRARY\r
-\r
-void writeoff(offfilename, argc, argv)\r
-char *offfilename;\r
-int argc;\r
-char **argv;\r
-{\r
- FILE *outfile;\r
- struct triedge triangleloop;\r
- point pointloop;\r
- point p1, p2, p3;\r
-\r
- if (!quiet) {\r
- printf("Writing %s.\n", offfilename);\r
- }\r
- outfile = fopen(offfilename, "w");\r
- if (outfile == (FILE *) NULL) {\r
- printf(" Error: Cannot create file %s.\n", offfilename);\r
- exit(1);\r
- }\r
- /* Number of points, triangles, and edges. */\r
- fprintf(outfile, "OFF\n%ld %ld %ld\n", points.items, triangles.items,\r
- edges);\r
-\r
- /* Write the points. */\r
- traversalinit(&points);\r
- pointloop = pointtraverse();\r
- while (pointloop != (point) NULL) {\r
- /* The "0.0" is here because the OFF format uses 3D coordinates. */\r
- fprintf(outfile, " %.17g %.17g %.17g\n", pointloop[0],\r
- pointloop[1], 0.0);\r
- pointloop = pointtraverse();\r
- }\r
-\r
- /* Write the triangles. */\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- triangleloop.orient = 0;\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- org(triangleloop, p1);\r
- dest(triangleloop, p2);\r
- apex(triangleloop, p3);\r
- /* The "3" means a three-vertex polygon. */\r
- fprintf(outfile, " 3 %4d %4d %4d\n", pointmark(p1) - 1,\r
- pointmark(p2) - 1, pointmark(p3) - 1);\r
- triangleloop.tri = triangletraverse();\r
- }\r
- finishfile(outfile, argc, argv);\r
-}\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-/** **/\r
-/** **/\r
-/********* File I/O routines end here *********/\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* quality_statistics() Print statistics about the quality of the mesh. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void quality_statistics()\r
-{\r
- struct triedge triangleloop;\r
- point p[3];\r
- REAL cossquaretable[8];\r
- REAL ratiotable[16];\r
- REAL dx[3], dy[3];\r
- REAL edgelength[3];\r
- REAL dotproduct;\r
- REAL cossquare;\r
- REAL triarea;\r
- REAL shortest, longest;\r
- REAL trilongest2;\r
- REAL smallestarea, biggestarea;\r
- REAL triminaltitude2;\r
- REAL minaltitude;\r
- REAL triaspect2;\r
- REAL worstaspect;\r
- REAL smallestangle, biggestangle;\r
- REAL radconst, degconst;\r
- int angletable[18];\r
- int aspecttable[16];\r
- int aspectindex;\r
- int tendegree;\r
- int acutebiggest;\r
- int i, ii, j, k;\r
-\r
- printf("Mesh quality statistics:\n\n");\r
- radconst = (REAL)(PI / 18.0);\r
- degconst = (REAL)(180.0 / PI);\r
- for (i = 0; i < 8; i++) {\r
- cossquaretable[i] = (REAL)(cos(radconst * (REAL) (i + 1)));\r
- cossquaretable[i] = cossquaretable[i] * cossquaretable[i];\r
- }\r
- for (i = 0; i < 18; i++) {\r
- angletable[i] = 0;\r
- }\r
-\r
- ratiotable[0] = 1.5; ratiotable[1] = 2.0;\r
- ratiotable[2] = 2.5; ratiotable[3] = 3.0;\r
- ratiotable[4] = 4.0; ratiotable[5] = 6.0;\r
- ratiotable[6] = 10.0; ratiotable[7] = 15.0;\r
- ratiotable[8] = 25.0; ratiotable[9] = 50.0;\r
- ratiotable[10] = 100.0; ratiotable[11] = 300.0;\r
- ratiotable[12] = 1000.0; ratiotable[13] = 10000.0;\r
- ratiotable[14] = 100000.0; ratiotable[15] = 0.0;\r
- for (i = 0; i < 16; i++) {\r
- aspecttable[i] = 0;\r
- }\r
-\r
- worstaspect = 0.0;\r
- minaltitude = xmax - xmin + ymax - ymin;\r
- minaltitude = minaltitude * minaltitude;\r
- shortest = minaltitude;\r
- longest = 0.0;\r
- smallestarea = minaltitude;\r
- biggestarea = 0.0;\r
- worstaspect = 0.0;\r
- smallestangle = 0.0;\r
- biggestangle = 2.0;\r
- acutebiggest = 1;\r
-\r
- traversalinit(&triangles);\r
- triangleloop.tri = triangletraverse();\r
- triangleloop.orient = 0;\r
- while (triangleloop.tri != (triangle *) NULL) {\r
- org(triangleloop, p[0]);\r
- dest(triangleloop, p[1]);\r
- apex(triangleloop, p[2]);\r
- trilongest2 = 0.0;\r
-\r
- for (i = 0; i < 3; i++) {\r
- j = plus1mod3[i];\r
- k = minus1mod3[i];\r
- dx[i] = p[j][0] - p[k][0];\r
- dy[i] = p[j][1] - p[k][1];\r
- edgelength[i] = dx[i] * dx[i] + dy[i] * dy[i];\r
- if (edgelength[i] > trilongest2) {\r
- trilongest2 = edgelength[i];\r
- }\r
- if (edgelength[i] > longest) {\r
- longest = edgelength[i];\r
- }\r
- if (edgelength[i] < shortest) {\r
- shortest = edgelength[i];\r
- }\r
- }\r
-\r
- triarea = counterclockwise(p[0], p[1], p[2]);\r
- if (triarea < smallestarea) {\r
- smallestarea = triarea;\r
- }\r
- if (triarea > biggestarea) {\r
- biggestarea = triarea;\r
- }\r
- triminaltitude2 = triarea * triarea / trilongest2;\r
- if (triminaltitude2 < minaltitude) {\r
- minaltitude = triminaltitude2;\r
- }\r
- triaspect2 = trilongest2 / triminaltitude2;\r
- if (triaspect2 > worstaspect) {\r
- worstaspect = triaspect2;\r
- }\r
- aspectindex = 0;\r
- while ((triaspect2 > ratiotable[aspectindex] * ratiotable[aspectindex])\r
- && (aspectindex < 15)) {\r
- aspectindex++;\r
- }\r
- aspecttable[aspectindex]++;\r
-\r
- for (i = 0; i < 3; i++) {\r
- j = plus1mod3[i];\r
- k = minus1mod3[i];\r
- dotproduct = dx[j] * dx[k] + dy[j] * dy[k];\r
- cossquare = dotproduct * dotproduct / (edgelength[j] * edgelength[k]);\r
- tendegree = 8;\r
- for (ii = 7; ii >= 0; ii--) {\r
- if (cossquare > cossquaretable[ii]) {\r
- tendegree = ii;\r
- }\r
- }\r
- if (dotproduct <= 0.0) {\r
- angletable[tendegree]++;\r
- if (cossquare > smallestangle) {\r
- smallestangle = cossquare;\r
- }\r
- if (acutebiggest && (cossquare < biggestangle)) {\r
- biggestangle = cossquare;\r
- }\r
- } else {\r
- angletable[17 - tendegree]++;\r
- if (acutebiggest || (cossquare > biggestangle)) {\r
- biggestangle = cossquare;\r
- acutebiggest = 0;\r
- }\r
- }\r
- }\r
- triangleloop.tri = triangletraverse();\r
- }\r
-\r
- shortest = (REAL)sqrt(shortest);\r
- longest = (REAL)sqrt(longest);\r
- minaltitude = (REAL)sqrt(minaltitude);\r
- worstaspect = (REAL)sqrt(worstaspect);\r
- smallestarea *= 2.0;\r
- biggestarea *= 2.0;\r
- if (smallestangle >= 1.0) {\r
- smallestangle = 0.0;\r
- } else {\r
- smallestangle = (REAL)(degconst * acos(sqrt(smallestangle)));\r
- }\r
- if (biggestangle >= 1.0) {\r
- biggestangle = 180.0;\r
- } else {\r
- if (acutebiggest) {\r
- biggestangle = (REAL)(degconst * acos(sqrt(biggestangle)));\r
- } else {\r
- biggestangle = (REAL)(180.0 - degconst * acos(sqrt(biggestangle)));\r
- }\r
- }\r
-\r
- printf(" Smallest area: %16.5g | Largest area: %16.5g\n",\r
- smallestarea, biggestarea);\r
- printf(" Shortest edge: %16.5g | Longest edge: %16.5g\n",\r
- shortest, longest);\r
- printf(" Shortest altitude: %12.5g | Largest aspect ratio: %8.5g\n\n",\r
- minaltitude, worstaspect);\r
- printf(" Aspect ratio histogram:\n");\r
- printf(" 1.1547 - %-6.6g : %8d | %6.6g - %-6.6g : %8d\n",\r
- ratiotable[0], aspecttable[0], ratiotable[7], ratiotable[8],\r
- aspecttable[8]);\r
- for (i = 1; i < 7; i++) {\r
- printf(" %6.6g - %-6.6g : %8d | %6.6g - %-6.6g : %8d\n",\r
- ratiotable[i - 1], ratiotable[i], aspecttable[i],\r
- ratiotable[i + 7], ratiotable[i + 8], aspecttable[i + 8]);\r
- }\r
- printf(" %6.6g - %-6.6g : %8d | %6.6g - : %8d\n",\r
- ratiotable[6], ratiotable[7], aspecttable[7], ratiotable[14],\r
- aspecttable[15]);\r
- printf(\r
-" (Triangle aspect ratio is longest edge divided by shortest altitude)\n\n");\r
- printf(" Smallest angle: %15.5g | Largest angle: %15.5g\n\n",\r
- smallestangle, biggestangle);\r
- printf(" Angle histogram:\n");\r
- for (i = 0; i < 9; i++) {\r
- printf(" %3d - %3d degrees: %8d | %3d - %3d degrees: %8d\n",\r
- i * 10, i * 10 + 10, angletable[i],\r
- i * 10 + 90, i * 10 + 100, angletable[i + 9]);\r
- }\r
- printf("\n");\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* statistics() Print all sorts of cool facts. */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-void statistics()\r
-{\r
- printf("\nStatistics:\n\n");\r
- printf(" Input points: %d\n", inpoints);\r
- if (refine) {\r
- printf(" Input triangles: %d\n", inelements);\r
- }\r
- if (poly) {\r
- printf(" Input segments: %d\n", insegments);\r
- if (!refine) {\r
- printf(" Input holes: %d\n", holes);\r
- }\r
- }\r
-\r
- printf("\n Mesh points: %ld\n", points.items);\r
- printf(" Mesh triangles: %ld\n", triangles.items);\r
- printf(" Mesh edges: %ld\n", edges);\r
- if (poly || refine) {\r
- printf(" Mesh boundary edges: %ld\n", hullsize);\r
- printf(" Mesh segments: %ld\n\n", shelles.items);\r
- } else {\r
- printf(" Mesh convex hull edges: %ld\n\n", hullsize);\r
- }\r
- if (verbose) {\r
- quality_statistics();\r
- printf("Memory allocation statistics:\n\n");\r
- printf(" Maximum number of points: %ld\n", points.maxitems);\r
- printf(" Maximum number of triangles: %ld\n", triangles.maxitems);\r
- if (shelles.maxitems > 0) {\r
- printf(" Maximum number of segments: %ld\n", shelles.maxitems);\r
- }\r
- if (viri.maxitems > 0) {\r
- printf(" Maximum number of viri: %ld\n", viri.maxitems);\r
- }\r
- if (badsegments.maxitems > 0) {\r
- printf(" Maximum number of encroached segments: %ld\n",\r
- badsegments.maxitems);\r
- }\r
- if (badtriangles.maxitems > 0) {\r
- printf(" Maximum number of bad triangles: %ld\n",\r
- badtriangles.maxitems);\r
- }\r
- if (splaynodes.maxitems > 0) {\r
- printf(" Maximum number of splay tree nodes: %ld\n",\r
- splaynodes.maxitems);\r
- }\r
- printf(" Approximate heap memory use (bytes): %ld\n\n",\r
- points.maxitems * points.itembytes\r
- + triangles.maxitems * triangles.itembytes\r
- + shelles.maxitems * shelles.itembytes\r
- + viri.maxitems * viri.itembytes\r
- + badsegments.maxitems * badsegments.itembytes\r
- + badtriangles.maxitems * badtriangles.itembytes\r
- + splaynodes.maxitems * splaynodes.itembytes);\r
-\r
- printf("Algorithmic statistics:\n\n");\r
- printf(" Number of incircle tests: %ld\n", incirclecount);\r
- printf(" Number of orientation tests: %ld\n", counterclockcount);\r
- if (hyperbolacount > 0) {\r
- printf(" Number of right-of-hyperbola tests: %ld\n",\r
- hyperbolacount);\r
- }\r
- if (circumcentercount > 0) {\r
- printf(" Number of circumcenter computations: %ld\n",\r
- circumcentercount);\r
- }\r
- if (circletopcount > 0) {\r
- printf(" Number of circle top computations: %ld\n",\r
- circletopcount);\r
- }\r
- printf("\n");\r
- }\r
-}\r
-\r
-/*****************************************************************************/\r
-/* */\r
-/* main() or triangulate() Gosh, do everything. */\r
-/* */\r
-/* The sequence is roughly as follows. Many of these steps can be skipped, */\r
-/* depending on the command line switches. */\r
-/* */\r
-/* - Initialize constants and parse the command line. */\r
-/* - Read the points from a file and either */\r
-/* - triangulate them (no -r), or */\r
-/* - read an old mesh from files and reconstruct it (-r). */\r
-/* - Insert the PSLG segments (-p), and possibly segments on the convex */\r
-/* hull (-c). */\r
-/* - Read the holes (-p), regional attributes (-pA), and regional area */\r
-/* constraints (-pa). Carve the holes and concavities, and spread the */\r
-/* regional attributes and area constraints. */\r
-/* - Enforce the constraints on minimum angle (-q) and maximum area (-a). */\r
-/* Also enforce the conforming Delaunay property (-q and -a). */\r
-/* - Compute the number of edges in the resulting mesh. */\r
-/* - Promote the mesh's linear triangles to higher order elements (-o). */\r
-/* - Write the output files and print the statistics. */\r
-/* - Check the consistency and Delaunay property of the mesh (-C). */\r
-/* */\r
-/*****************************************************************************/\r
-\r
-#ifdef TRILIBRARY\r
-\r
-void triangulate(triswitches, in, out, vorout)\r
-char *triswitches;\r
-struct triangulateio *in;\r
-struct triangulateio *out;\r
-struct triangulateio *vorout;\r
-\r
-#else /* not TRILIBRARY */\r
-\r
-int main(argc, argv)\r
-int argc;\r
-char **argv;\r
-\r
-#endif /* not TRILIBRARY */\r
-\r
-{\r
- REAL *holearray; /* Array of holes. */\r
- REAL *regionarray; /* Array of regional attributes and area constraints. */\r
-#ifndef TRILIBRARY\r
- FILE *polyfile;\r
-#endif /* not TRILIBRARY */\r
-#ifndef NO_TIMER\r
- /* Variables for timing the performance of Triangle. The types are */\r
- /* defined in sys/time.h. */\r
- struct timeval tv0, tv1, tv2, tv3, tv4, tv5, tv6;\r
- struct timezone tz;\r
-#endif /* NO_TIMER */\r
-\r
-#ifndef NO_TIMER\r
- gettimeofday(&tv0, &tz);\r
-#endif /* NO_TIMER */\r
-\r
- triangleinit();\r
-#ifdef TRILIBRARY\r
- parsecommandline(1, &triswitches);\r
-#else /* not TRILIBRARY */\r
- parsecommandline(argc, argv);\r
-#endif /* not TRILIBRARY */\r
-\r
-#ifdef TRILIBRARY\r
- transfernodes(in->pointlist, in->pointattributelist, in->pointmarkerlist,\r
- in->numberofpoints, in->numberofpointattributes);\r
-#else /* not TRILIBRARY */\r
- readnodes(innodefilename, inpolyfilename, &polyfile);\r
-#endif /* not TRILIBRARY */\r
-\r
-#ifndef NO_TIMER\r
- if (!quiet) {\r
- gettimeofday(&tv1, &tz);\r
- }\r
-#endif /* NO_TIMER */\r
-\r
-#ifdef CDT_ONLY\r
- hullsize = delaunay(); /* Triangulate the points. */\r
-#else /* not CDT_ONLY */\r
- if (refine) {\r
- /* Read and reconstruct a mesh. */\r
-#ifdef TRILIBRARY\r
- hullsize = reconstruct(in->trianglelist, in->triangleattributelist,\r
- in->trianglearealist, in->numberoftriangles,\r
- in->numberofcorners, in->numberoftriangleattributes,\r
- in->segmentlist, in->segmentmarkerlist,\r
- in->numberofsegments);\r
-#else /* not TRILIBRARY */\r
- hullsize = reconstruct(inelefilename, areafilename, inpolyfilename,\r
- polyfile);\r
-#endif /* not TRILIBRARY */\r
- } else {\r
- hullsize = delaunay(); /* Triangulate the points. */\r
- }\r
-#endif /* not CDT_ONLY */\r
-\r
-#ifndef NO_TIMER\r
- if (!quiet) {\r
- gettimeofday(&tv2, &tz);\r
- if (refine) {\r
- printf("Mesh reconstruction");\r
- } else {\r
- printf("Delaunay");\r
- }\r
- printf(" milliseconds: %ld\n", 1000l * (tv2.tv_sec - tv1.tv_sec)\r
- + (tv2.tv_usec - tv1.tv_usec) / 1000l);\r
- }\r
-#endif /* NO_TIMER */\r
-\r
- /* Ensure that no point can be mistaken for a triangular bounding */\r
- /* box point in insertsite(). */\r
- infpoint1 = (point) NULL;\r
- infpoint2 = (point) NULL;\r
- infpoint3 = (point) NULL;\r
-\r
- if (useshelles) {\r
- checksegments = 1; /* Segments will be introduced next. */\r
- if (!refine) {\r
- /* Insert PSLG segments and/or convex hull segments. */\r
-#ifdef TRILIBRARY\r
- insegments = formskeleton(in->segmentlist, in->segmentmarkerlist,\r
- in->numberofsegments);\r
-#else /* not TRILIBRARY */\r
- insegments = formskeleton(polyfile, inpolyfilename);\r
-#endif /* not TRILIBRARY */\r
- }\r
- }\r
-\r
-#ifndef NO_TIMER\r
- if (!quiet) {\r
- gettimeofday(&tv3, &tz);\r
- if (useshelles && !refine) {\r
- printf("Segment milliseconds: %ld\n",\r
- 1000l * (tv3.tv_sec - tv2.tv_sec)\r
- + (tv3.tv_usec - tv2.tv_usec) / 1000l);\r
- }\r
- }\r
-#endif /* NO_TIMER */\r
-\r
- if (poly) {\r
-#ifdef TRILIBRARY\r
- holearray = in->holelist;\r
- holes = in->numberofholes;\r
- regionarray = in->regionlist;\r
- regions = in->numberofregions;\r
-#else /* not TRILIBRARY */\r
- readholes(polyfile, inpolyfilename, &holearray, &holes,\r
- ®ionarray, ®ions);\r
-#endif /* not TRILIBRARY */\r
- if (!refine) {\r
- /* Carve out holes and concavities. */\r
- carveholes(holearray, holes, regionarray, regions);\r
- }\r
- } else {\r
- /* Without a PSLG, there can be no holes or regional attributes */\r
- /* or area constraints. The following are set to zero to avoid */\r
- /* an accidental free() later. */\r
- holes = 0;\r
- regions = 0;\r
- }\r
-\r
-#ifndef NO_TIMER\r
- if (!quiet) {\r
- gettimeofday(&tv4, &tz);\r
- if (poly && !refine) {\r
- printf("Hole milliseconds: %ld\n", 1000l * (tv4.tv_sec - tv3.tv_sec)\r
- + (tv4.tv_usec - tv3.tv_usec) / 1000l);\r
- }\r
- }\r
-#endif /* NO_TIMER */\r
-\r
-#ifndef CDT_ONLY\r
- if (quality) {\r
- enforcequality(); /* Enforce angle and area constraints. */\r
- }\r
-#endif /* not CDT_ONLY */\r
-\r
-#ifndef NO_TIMER\r
- if (!quiet) {\r
- gettimeofday(&tv5, &tz);\r
-#ifndef CDT_ONLY\r
- if (quality) {\r
- printf("Quality milliseconds: %ld\n",\r
- 1000l * (tv5.tv_sec - tv4.tv_sec)\r
- + (tv5.tv_usec - tv4.tv_usec) / 1000l);\r
- }\r
-#endif /* not CDT_ONLY */\r
- }\r
-#endif /* NO_TIMER */\r
-\r
- /* Compute the number of edges. */\r
- edges = (3l * triangles.items + hullsize) / 2l;\r
-\r
- if (order > 1) {\r
- highorder(); /* Promote elements to higher polynomial order. */\r
- }\r
- if (!quiet) {\r
- printf("\n");\r
- }\r
-\r
-#ifdef TRILIBRARY\r
- out->numberofpoints = points.items;\r
- out->numberofpointattributes = nextras;\r
- out->numberoftriangles = triangles.items;\r
- out->numberofcorners = (order + 1) * (order + 2) / 2;\r
- out->numberoftriangleattributes = eextras;\r
- out->numberofedges = edges;\r
- if (useshelles) {\r
- out->numberofsegments = shelles.items;\r
- } else {\r
- out->numberofsegments = hullsize;\r
- }\r
- if (vorout != (struct triangulateio *) NULL) {\r
- vorout->numberofpoints = triangles.items;\r
- vorout->numberofpointattributes = nextras;\r
- vorout->numberofedges = edges;\r
- }\r
-#endif /* TRILIBRARY */\r
- /* If not using iteration numbers, don't write a .node file if one was */\r
- /* read, because the original one would be overwritten! */\r
- if (nonodewritten || (noiterationnum && readnodefile)) {\r
- if (!quiet) {\r
-#ifdef TRILIBRARY\r
- printf("NOT writing points.\n");\r
-#else /* not TRILIBRARY */\r
- printf("NOT writing a .node file.\n");\r
-#endif /* not TRILIBRARY */\r
- }\r
- numbernodes(); /* We must remember to number the points. */\r
- } else {\r
-#ifdef TRILIBRARY\r
- writenodes(&out->pointlist, &out->pointattributelist,\r
- &out->pointmarkerlist);\r
-#else /* not TRILIBRARY */\r
- writenodes(outnodefilename, argc, argv); /* Numbers the points too. */\r
-#endif /* TRILIBRARY */\r
- }\r
- if (noelewritten) {\r
- if (!quiet) {\r
-#ifdef TRILIBRARY\r
- printf("NOT writing triangles.\n");\r
-#else /* not TRILIBRARY */\r
- printf("NOT writing an .ele file.\n");\r
-#endif /* not TRILIBRARY */\r
- }\r
- } else {\r
-#ifdef TRILIBRARY\r
- writeelements(&out->trianglelist, &out->triangleattributelist);\r
-#else /* not TRILIBRARY */\r
- writeelements(outelefilename, argc, argv);\r
-#endif /* not TRILIBRARY */\r
- }\r
- /* The -c switch (convex switch) causes a PSLG to be written */\r
- /* even if none was read. */\r
- if (poly || convex) {\r
- /* If not using iteration numbers, don't overwrite the .poly file. */\r
- if (nopolywritten || noiterationnum) {\r
- if (!quiet) {\r
-#ifdef TRILIBRARY\r
- printf("NOT writing segments.\n");\r
-#else /* not TRILIBRARY */\r
- printf("NOT writing a .poly file.\n");\r
-#endif /* not TRILIBRARY */\r
- }\r
- } else {\r
-#ifdef TRILIBRARY\r
- writepoly(&out->segmentlist, &out->segmentmarkerlist);\r
- out->numberofholes = holes;\r
- out->numberofregions = regions;\r
- if (poly) {\r
- out->holelist = in->holelist;\r
- out->regionlist = in->regionlist;\r
- } else {\r
- out->holelist = (REAL *) NULL;\r
- out->regionlist = (REAL *) NULL;\r
- }\r
-#else /* not TRILIBRARY */\r
- writepoly(outpolyfilename, holearray, holes, regionarray, regions,\r
- argc, argv);\r
-#endif /* not TRILIBRARY */\r
- }\r
- }\r
-#ifndef TRILIBRARY\r
-#ifndef CDT_ONLY\r
- if (regions > 0) {\r
- free(regionarray);\r
- }\r
-#endif /* not CDT_ONLY */\r
- if (holes > 0) {\r
- free(holearray);\r
- }\r
- if (geomview) {\r
- writeoff(offfilename, argc, argv);\r
- }\r
-#endif /* not TRILIBRARY */\r
- if (edgesout) {\r
-#ifdef TRILIBRARY\r
- writeedges(&out->edgelist, &out->edgemarkerlist);\r
-#else /* not TRILIBRARY */\r
- writeedges(edgefilename, argc, argv);\r
-#endif /* not TRILIBRARY */\r
- }\r
- if (voronoi) {\r
-#ifdef TRILIBRARY\r
- writevoronoi(&vorout->pointlist, &vorout->pointattributelist,\r
- &vorout->pointmarkerlist, &vorout->edgelist,\r
- &vorout->edgemarkerlist, &vorout->normlist);\r
-#else /* not TRILIBRARY */\r
- writevoronoi(vnodefilename, vedgefilename, argc, argv);\r
-#endif /* not TRILIBRARY */\r
- }\r
- if (neighbors) {\r
-#ifdef TRILIBRARY\r
- writeneighbors(&out->neighborlist);\r
-#else /* not TRILIBRARY */\r
- writeneighbors(neighborfilename, argc, argv);\r
-#endif /* not TRILIBRARY */\r
- }\r
-\r
- if (!quiet) {\r
-#ifndef NO_TIMER\r
- gettimeofday(&tv6, &tz);\r
- printf("\nOutput milliseconds: %ld\n",\r
- 1000l * (tv6.tv_sec - tv5.tv_sec)\r
- + (tv6.tv_usec - tv5.tv_usec) / 1000l);\r
- printf("Total running milliseconds: %ld\n",\r
- 1000l * (tv6.tv_sec - tv0.tv_sec)\r
- + (tv6.tv_usec - tv0.tv_usec) / 1000l);\r
-#endif /* NO_TIMER */\r
-\r
- statistics();\r
- }\r
-\r
-#ifndef REDUCED\r
- if (docheck) {\r
- checkmesh();\r
- checkdelaunay();\r
- }\r
-#endif /* not REDUCED */\r
-\r
- triangledeinit();\r
-#ifndef TRILIBRARY\r
- return 0;\r
-#endif /* not TRILIBRARY */\r
-}\r
+#define ANSI_DECLARATORS
+/*****************************************************************************/
+/* */
+/* 888888888 ,o, / 888 */
+/* 888 88o88o " o8888o 88o8888o o88888o 888 o88888o */
+/* 888 888 888 88b 888 888 888 888 888 d888 88b */
+/* 888 888 888 o88^o888 888 888 "88888" 888 8888oo888 */
+/* 888 888 888 C888 888 888 888 / 888 q888 */
+/* 888 888 888 "88o^888 888 888 Cb 888 "88oooo" */
+/* "8oo8D */
+/* */
+/* A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator. */
+/* (triangle.c) */
+/* */
+/* Version 1.3 */
+/* July 19, 1996 */
+/* */
+/* Copyright 1996 */
+/* Jonathan Richard Shewchuk */
+/* School of Computer Science */
+/* Carnegie Mellon University */
+/* 5000 Forbes Avenue */
+/* Pittsburgh, Pennsylvania 15213-3891 */
+/* jrs@cs.cmu.edu */
+/* */
+/* This program may be freely redistributed under the condition that the */
+/* copyright notices (including this entire header and the copyright */
+/* notice printed when the `-h' switch is selected) are not removed, and */
+/* no compensation is received. Private, research, and institutional */
+/* use is free. You may distribute modified versions of this code UNDER */
+/* THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE */
+/* SAME FILE REMAIN UNDER COPYRIGHT OF THE ORIGINAL AUTHOR, BOTH SOURCE */
+/* AND OBJECT CODE ARE MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR */
+/* NOTICE IS GIVEN OF THE MODIFICATIONS. Distribution of this code as */
+/* part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT */
+/* WITH THE AUTHOR. (If you are not directly supplying this code to a */
+/* customer, and you are instead telling them how they can obtain it for */
+/* free, then you are not required to make any arrangement with me.) */
+/* */
+/* Hypertext instructions for Triangle are available on the Web at */
+/* */
+/* http://www.cs.cmu.edu/~quake/triangle.html */
+/* */
+/* Some of the references listed below are marked [*]. These are available */
+/* for downloading from the Web page */
+/* */
+/* http://www.cs.cmu.edu/~quake/triangle.research.html */
+/* */
+/* A paper discussing some aspects of Triangle is available. See Jonathan */
+/* Richard Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator */
+/* and Delaunay Triangulator," First Workshop on Applied Computational */
+/* Geometry, ACM, May 1996. [*] */
+/* */
+/* Triangle was created as part of the Archimedes project in the School of */
+/* Computer Science at Carnegie Mellon University. Archimedes is a */
+/* system for compiling parallel finite element solvers. For further */
+/* information, see Anja Feldmann, Omar Ghattas, John R. Gilbert, Gary L. */
+/* Miller, David R. O'Hallaron, Eric J. Schwabe, Jonathan R. Shewchuk, */
+/* and Shang-Hua Teng, "Automated Parallel Solution of Unstructured PDE */
+/* Problems." To appear in Communications of the ACM, we hope. */
+/* */
+/* The quality mesh generation algorithm is due to Jim Ruppert, "A */
+/* Delaunay Refinement Algorithm for Quality 2-Dimensional Mesh */
+/* Generation," Journal of Algorithms 18(3):548-585, May 1995. [*] */
+/* */
+/* My implementation of the divide-and-conquer and incremental Delaunay */
+/* triangulation algorithms follows closely the presentation of Guibas */
+/* and Stolfi, even though I use a triangle-based data structure instead */
+/* of their quad-edge data structure. (In fact, I originally implemented */
+/* Triangle using the quad-edge data structure, but switching to a */
+/* triangle-based data structure sped Triangle by a factor of two.) The */
+/* mesh manipulation primitives and the two aforementioned Delaunay */
+/* triangulation algorithms are described by Leonidas J. Guibas and Jorge */
+/* Stolfi, "Primitives for the Manipulation of General Subdivisions and */
+/* the Computation of Voronoi Diagrams," ACM Transactions on Graphics */
+/* 4(2):74-123, April 1985. */
+/* */
+/* Their O(n log n) divide-and-conquer algorithm is adapted from Der-Tsai */
+/* Lee and Bruce J. Schachter, "Two Algorithms for Constructing the */
+/* Delaunay Triangulation," International Journal of Computer and */
+/* Information Science 9(3):219-242, 1980. The idea to improve the */
+/* divide-and-conquer algorithm by alternating between vertical and */
+/* horizontal cuts was introduced by Rex A. Dwyer, "A Faster Divide-and- */
+/* Conquer Algorithm for Constructing Delaunay Triangulations," */
+/* Algorithmica 2(2):137-151, 1987. */
+/* */
+/* The incremental insertion algorithm was first proposed by C. L. Lawson, */
+/* "Software for C1 Surface Interpolation," in Mathematical Software III, */
+/* John R. Rice, editor, Academic Press, New York, pp. 161-194, 1977. */
+/* For point location, I use the algorithm of Ernst P. Mucke, Isaac */
+/* Saias, and Binhai Zhu, "Fast Randomized Point Location Without */
+/* Preprocessing in Two- and Three-dimensional Delaunay Triangulations," */
+/* Proceedings of the Twelfth Annual Symposium on Computational Geometry, */
+/* ACM, May 1996. [*] If I were to randomize the order of point */
+/* insertion (I currently don't bother), their result combined with the */
+/* result of Leonidas J. Guibas, Donald E. Knuth, and Micha Sharir, */
+/* "Randomized Incremental Construction of Delaunay and Voronoi */
+/* Diagrams," Algorithmica 7(4):381-413, 1992, would yield an expected */
+/* O(n^{4/3}) bound on running time. */
+/* */
+/* The O(n log n) sweepline Delaunay triangulation algorithm is taken from */
+/* Steven Fortune, "A Sweepline Algorithm for Voronoi Diagrams", */
+/* Algorithmica 2(2):153-174, 1987. A random sample of edges on the */
+/* boundary of the triangulation are maintained in a splay tree for the */
+/* purpose of point location. Splay trees are described by Daniel */
+/* Dominic Sleator and Robert Endre Tarjan, "Self-Adjusting Binary Search */
+/* Trees," Journal of the ACM 32(3):652-686, July 1985. */
+/* */
+/* The algorithms for exact computation of the signs of determinants are */
+/* described in Jonathan Richard Shewchuk, "Adaptive Precision Floating- */
+/* Point Arithmetic and Fast Robust Geometric Predicates," Technical */
+/* Report CMU-CS-96-140, School of Computer Science, Carnegie Mellon */
+/* University, Pittsburgh, Pennsylvania, May 1996. [*] (Submitted to */
+/* Discrete & Computational Geometry.) An abbreviated version appears as */
+/* Jonathan Richard Shewchuk, "Robust Adaptive Floating-Point Geometric */
+/* Predicates," Proceedings of the Twelfth Annual Symposium on Computa- */
+/* tional Geometry, ACM, May 1996. [*] Many of the ideas for my exact */
+/* arithmetic routines originate with Douglas M. Priest, "Algorithms for */
+/* Arbitrary Precision Floating Point Arithmetic," Tenth Symposium on */
+/* Computer Arithmetic, 132-143, IEEE Computer Society Press, 1991. [*] */
+/* Many of the ideas for the correct evaluation of the signs of */
+/* determinants are taken from Steven Fortune and Christopher J. Van Wyk, */
+/* "Efficient Exact Arithmetic for Computational Geometry," Proceedings */
+/* of the Ninth Annual Symposium on Computational Geometry, ACM, */
+/* pp. 163-172, May 1993, and from Steven Fortune, "Numerical Stability */
+/* of Algorithms for 2D Delaunay Triangulations," International Journal */
+/* of Computational Geometry & Applications 5(1-2):193-213, March-June */
+/* 1995. */
+/* */
+/* For definitions of and results involving Delaunay triangulations, */
+/* constrained and conforming versions thereof, and other aspects of */
+/* triangular mesh generation, see the excellent survey by Marshall Bern */
+/* and David Eppstein, "Mesh Generation and Optimal Triangulation," in */
+/* Computing and Euclidean Geometry, Ding-Zhu Du and Frank Hwang, */
+/* editors, World Scientific, Singapore, pp. 23-90, 1992. */
+/* */
+/* The time for incrementally adding PSLG (planar straight line graph) */
+/* segments to create a constrained Delaunay triangulation is probably */
+/* O(n^2) per segment in the worst case and O(n) per edge in the common */
+/* case, where n is the number of triangles that intersect the segment */
+/* before it is inserted. This doesn't count point location, which can */
+/* be much more expensive. (This note does not apply to conforming */
+/* Delaunay triangulations, for which a different method is used to */
+/* insert segments.) */
+/* */
+/* The time for adding segments to a conforming Delaunay triangulation is */
+/* not clear, but does not depend upon n alone. In some cases, very */
+/* small features (like a point lying next to a segment) can cause a */
+/* single segment to be split an arbitrary number of times. Of course, */
+/* floating-point precision is a practical barrier to how much this can */
+/* happen. */
+/* */
+/* The time for deleting a point from a Delaunay triangulation is O(n^2) in */
+/* the worst case and O(n) in the common case, where n is the degree of */
+/* the point being deleted. I could improve this to expected O(n) time */
+/* by "inserting" the neighboring vertices in random order, but n is */
+/* usually quite small, so it's not worth the bother. (The O(n) time */
+/* for random insertion follows from L. Paul Chew, "Building Voronoi */
+/* Diagrams for Convex Polygons in Linear Expected Time," Technical */
+/* Report PCS-TR90-147, Department of Mathematics and Computer Science, */
+/* Dartmouth College, 1990. */
+/* */
+/* Ruppert's Delaunay refinement algorithm typically generates triangles */
+/* at a linear rate (constant time per triangle) after the initial */
+/* triangulation is formed. There may be pathological cases where more */
+/* time is required, but these never arise in practice. */
+/* */
+/* The segment intersection formulae are straightforward. If you want to */
+/* see them derived, see Franklin Antonio. "Faster Line Segment */
+/* Intersection." In Graphics Gems III (David Kirk, editor), pp. 199- */
+/* 202. Academic Press, Boston, 1992. */
+/* */
+/* If you make any improvements to this code, please please please let me */
+/* know, so that I may obtain the improvements. Even if you don't change */
+/* the code, I'd still love to hear what it's being used for. */
+/* */
+/* Disclaimer: Neither I nor Carnegie Mellon warrant this code in any way */
+/* whatsoever. This code is provided "as-is". Use at your own risk. */
+/* */
+/*****************************************************************************/
+
+/* For single precision (which will save some memory and reduce paging), */
+/* define the symbol SINGLE by using the -DSINGLE compiler switch or by */
+/* writing "#define SINGLE" below. */
+/* */
+/* For double precision (which will allow you to refine meshes to a smaller */
+/* edge length), leave SINGLE undefined. */
+/* */
+/* Double precision uses more memory, but improves the resolution of the */
+/* meshes you can generate with Triangle. It also reduces the likelihood */
+/* of a floating exception due to overflow. Finally, it is much faster */
+/* than single precision on 64-bit architectures like the DEC Alpha. I */
+/* recommend double precision unless you want to generate a mesh for which */
+/* you do not have enough memory. */
+
+#define SINGLE
+
+#ifdef SINGLE
+#define REAL float
+#else /* not SINGLE */
+#define REAL double
+#endif /* not SINGLE */
+
+/* If yours is not a Unix system, define the NO_TIMER compiler switch to */
+/* remove the Unix-specific timing code. */
+
+#define NO_TIMER
+
+/* To insert lots of self-checks for internal errors, define the SELF_CHECK */
+/* symbol. This will slow down the program significantly. It is best to */
+/* define the symbol using the -DSELF_CHECK compiler switch, but you could */
+/* write "#define SELF_CHECK" below. If you are modifying this code, I */
+/* recommend you turn self-checks on. */
+
+/* #define SELF_CHECK */
+
+/* To compile Triangle as a callable object library (triangle.o), define the */
+/* TRILIBRARY symbol. Read the file triangle.h for details on how to call */
+/* the procedure triangulate() that results. */
+
+#define TRILIBRARY
+
+/* It is possible to generate a smaller version of Triangle using one or */
+/* both of the following symbols. Define the REDUCED symbol to eliminate */
+/* all features that are primarily of research interest; specifically, the */
+/* -i, -F, -s, and -C switches. Define the CDT_ONLY symbol to eliminate */
+/* all meshing algorithms above and beyond constrained Delaunay */
+/* triangulation; specifically, the -r, -q, -a, -S, and -s switches. */
+/* These reductions are most likely to be useful when generating an object */
+/* library (triangle.o) by defining the TRILIBRARY symbol. */
+
+#define REDUCED
+#define CDT_ONLY
+
+/* On some machines, the exact arithmetic routines might be defeated by the */
+/* use of internal extended precision floating-point registers. Sometimes */
+/* this problem can be fixed by defining certain values to be volatile, */
+/* thus forcing them to be stored to memory and rounded off. This isn't */
+/* a great solution, though, as it slows Triangle down. */
+/* */
+/* To try this out, write "#define INEXACT volatile" below. Normally, */
+/* however, INEXACT should be defined to be nothing. ("#define INEXACT".) */
+
+#define INEXACT /* Nothing */
+/* #define INEXACT volatile */
+
+/* Maximum number of characters in a file name (including the null). */
+
+#define FILENAMESIZE 512
+
+/* Maximum number of characters in a line read from a file (including the */
+/* null). */
+
+#define INPUTLINESIZE 512
+
+/* For efficiency, a variety of data structures are allocated in bulk. The */
+/* following constants determine how many of each structure is allocated */
+/* at once. */
+
+#define TRIPERBLOCK 4092 /* Number of triangles allocated at once. */
+#define SHELLEPERBLOCK 508 /* Number of shell edges allocated at once. */
+#define POINTPERBLOCK 4092 /* Number of points allocated at once. */
+#define VIRUSPERBLOCK 1020 /* Number of virus triangles allocated at once. */
+/* Number of encroached segments allocated at once. */
+#define BADSEGMENTPERBLOCK 252
+/* Number of skinny triangles allocated at once. */
+#define BADTRIPERBLOCK 4092
+/* Number of splay tree nodes allocated at once. */
+#define SPLAYNODEPERBLOCK 508
+
+/* The point marker DEADPOINT is an arbitrary number chosen large enough to */
+/* (hopefully) not conflict with user boundary markers. Make sure that it */
+/* is small enough to fit into your machine's integer size. */
+
+#define DEADPOINT -1073741824
+
+/* The next line is used to outsmart some very stupid compilers. If your */
+/* compiler is smarter, feel free to replace the "int" with "void". */
+/* Not that it matters. */
+
+#define VOID int
+
+/* Two constants for algorithms based on random sampling. Both constants */
+/* have been chosen empirically to optimize their respective algorithms. */
+
+/* Used for the point location scheme of Mucke, Saias, and Zhu, to decide */
+/* how large a random sample of triangles to inspect. */
+#define SAMPLEFACTOR 11
+/* Used in Fortune's sweepline Delaunay algorithm to determine what fraction */
+/* of boundary edges should be maintained in the splay tree for point */
+/* location on the front. */
+#define SAMPLERATE 10
+
+/* A number that speaks for itself, every kissable digit. */
+
+#define PI 3.141592653589793238462643383279502884197169399375105820974944592308
+
+/* Another fave. */
+
+#define SQUAREROOTTWO 1.4142135623730950488016887242096980785696718753769480732
+
+/* And here's one for those of you who are intimidated by math. */
+
+#define ONETHIRD 0.333333333333333333333333333333333333333333333333333333333333
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#ifndef NO_TIMER
+#include <sys/time.h>
+#endif /* NO_TIMER */
+#ifdef TRILIBRARY
+#include "triangle.h"
+#endif /* TRILIBRARY */
+
+/* The following obscenity seems to be necessary to ensure that this program */
+/* will port to Dec Alphas running OSF/1, because their stdio.h file commits */
+/* the unpardonable sin of including stdlib.h. Hence, malloc(), free(), and */
+/* exit() may or may not already be defined at this point. I declare these */
+/* functions explicitly because some non-ANSI C compilers lack stdlib.h. */
+
+#ifndef _STDLIB_H_
+extern void *malloc();
+extern void free();
+extern void exit();
+extern double strtod();
+extern long strtol();
+#endif /* _STDLIB_H_ */
+
+/* A few forward declarations. */
+
+void poolrestart();
+#ifndef TRILIBRARY
+char *readline();
+char *findfield();
+#endif /* not TRILIBRARY */
+
+/* Labels that signify whether a record consists primarily of pointers or of */
+/* floating-point words. Used to make decisions about data alignment. */
+
+enum wordtype {POINTER, FLOATINGPOINT};
+
+/* Labels that signify the result of point location. The result of a */
+/* search indicates that the point falls in the interior of a triangle, on */
+/* an edge, on a vertex, or outside the mesh. */
+
+enum locateresult {INTRIANGLE, ONEDGE, ONVERTEX, OUTSIDE};
+
+/* Labels that signify the result of site insertion. The result indicates */
+/* that the point was inserted with complete success, was inserted but */
+/* encroaches on a segment, was not inserted because it lies on a segment, */
+/* or was not inserted because another point occupies the same location. */
+
+enum insertsiteresult {SUCCESSFULPOINT, ENCROACHINGPOINT, VIOLATINGPOINT,
+ DUPLICATEPOINT};
+
+/* Labels that signify the result of direction finding. The result */
+/* indicates that a segment connecting the two query points falls within */
+/* the direction triangle, along the left edge of the direction triangle, */
+/* or along the right edge of the direction triangle. */
+
+enum finddirectionresult {WITHIN, LEFTCOLLINEAR, RIGHTCOLLINEAR};
+
+/* Labels that signify the result of the circumcenter computation routine. */
+/* The return value indicates which edge of the triangle is shortest. */
+
+enum circumcenterresult {OPPOSITEORG, OPPOSITEDEST, OPPOSITEAPEX};
+
+/*****************************************************************************/
+/* */
+/* The basic mesh data structures */
+/* */
+/* There are three: points, triangles, and shell edges (abbreviated */
+/* `shelle'). These three data structures, linked by pointers, comprise */
+/* the mesh. A point simply represents a point in space and its properties.*/
+/* A triangle is a triangle. A shell edge is a special data structure used */
+/* to represent impenetrable segments in the mesh (including the outer */
+/* boundary, boundaries of holes, and internal boundaries separating two */
+/* triangulated regions). Shell edges represent boundaries defined by the */
+/* user that triangles may not lie across. */
+/* */
+/* A triangle consists of a list of three vertices, a list of three */
+/* adjoining triangles, a list of three adjoining shell edges (when shell */
+/* edges are used), an arbitrary number of optional user-defined floating- */
+/* point attributes, and an optional area constraint. The latter is an */
+/* upper bound on the permissible area of each triangle in a region, used */
+/* for mesh refinement. */
+/* */
+/* For a triangle on a boundary of the mesh, some or all of the neighboring */
+/* triangles may not be present. For a triangle in the interior of the */
+/* mesh, often no neighboring shell edges are present. Such absent */
+/* triangles and shell edges are never represented by NULL pointers; they */
+/* are represented by two special records: `dummytri', the triangle that */
+/* fills "outer space", and `dummysh', the omnipresent shell edge. */
+/* `dummytri' and `dummysh' are used for several reasons; for instance, */
+/* they can be dereferenced and their contents examined without causing the */
+/* memory protection exception that would occur if NULL were dereferenced. */
+/* */
+/* However, it is important to understand that a triangle includes other */
+/* information as well. The pointers to adjoining vertices, triangles, and */
+/* shell edges are ordered in a way that indicates their geometric relation */
+/* to each other. Furthermore, each of these pointers contains orientation */
+/* information. Each pointer to an adjoining triangle indicates which face */
+/* of that triangle is contacted. Similarly, each pointer to an adjoining */
+/* shell edge indicates which side of that shell edge is contacted, and how */
+/* the shell edge is oriented relative to the triangle. */
+/* */
+/* Shell edges are found abutting edges of triangles; either sandwiched */
+/* between two triangles, or resting against one triangle on an exterior */
+/* boundary or hole boundary. */
+/* */
+/* A shell edge consists of a list of two vertices, a list of two */
+/* adjoining shell edges, and a list of two adjoining triangles. One of */
+/* the two adjoining triangles may not be present (though there should */
+/* always be one), and neighboring shell edges might not be present. */
+/* Shell edges also store a user-defined integer "boundary marker". */
+/* Typically, this integer is used to indicate what sort of boundary */
+/* conditions are to be applied at that location in a finite element */
+/* simulation. */
+/* */
+/* Like triangles, shell edges maintain information about the relative */
+/* orientation of neighboring objects. */
+/* */
+/* Points are relatively simple. A point is a list of floating point */
+/* numbers, starting with the x, and y coordinates, followed by an */
+/* arbitrary number of optional user-defined floating-point attributes, */
+/* followed by an integer boundary marker. During the segment insertion */
+/* phase, there is also a pointer from each point to a triangle that may */
+/* contain it. Each pointer is not always correct, but when one is, it */
+/* speeds up segment insertion. These pointers are assigned values once */
+/* at the beginning of the segment insertion phase, and are not used or */
+/* updated at any other time. Edge swapping during segment insertion will */
+/* render some of them incorrect. Hence, don't rely upon them for */
+/* anything. For the most part, points do not have any information about */
+/* what triangles or shell edges they are linked to. */
+/* */
+/*****************************************************************************/
+
+/*****************************************************************************/
+/* */
+/* Handles */
+/* */
+/* The oriented triangle (`triedge') and oriented shell edge (`edge') data */
+/* structures defined below do not themselves store any part of the mesh. */
+/* The mesh itself is made of `triangle's, `shelle's, and `point's. */
+/* */
+/* Oriented triangles and oriented shell edges will usually be referred to */
+/* as "handles". A handle is essentially a pointer into the mesh; it */
+/* allows you to "hold" one particular part of the mesh. Handles are used */
+/* to specify the regions in which one is traversing and modifying the mesh.*/
+/* A single `triangle' may be held by many handles, or none at all. (The */
+/* latter case is not a memory leak, because the triangle is still */
+/* connected to other triangles in the mesh.) */
+/* */
+/* A `triedge' is a handle that holds a triangle. It holds a specific side */
+/* of the triangle. An `edge' is a handle that holds a shell edge. It */
+/* holds either the left or right side of the edge. */
+/* */
+/* Navigation about the mesh is accomplished through a set of mesh */
+/* manipulation primitives, further below. Many of these primitives take */
+/* a handle and produce a new handle that holds the mesh near the first */
+/* handle. Other primitives take two handles and glue the corresponding */
+/* parts of the mesh together. The exact position of the handles is */
+/* important. For instance, when two triangles are glued together by the */
+/* bond() primitive, they are glued by the sides on which the handles lie. */
+/* */
+/* Because points have no information about which triangles they are */
+/* attached to, I commonly represent a point by use of a handle whose */
+/* origin is the point. A single handle can simultaneously represent a */
+/* triangle, an edge, and a point. */
+/* */
+/*****************************************************************************/
+
+/* The triangle data structure. Each triangle contains three pointers to */
+/* adjoining triangles, plus three pointers to vertex points, plus three */
+/* pointers to shell edges (defined below; these pointers are usually */
+/* `dummysh'). It may or may not also contain user-defined attributes */
+/* and/or a floating-point "area constraint". It may also contain extra */
+/* pointers for nodes, when the user asks for high-order elements. */
+/* Because the size and structure of a `triangle' is not decided until */
+/* runtime, I haven't simply defined the type `triangle' to be a struct. */
+
+typedef REAL **triangle; /* Really: typedef triangle *triangle */
+
+/* An oriented triangle: includes a pointer to a triangle and orientation. */
+/* The orientation denotes an edge of the triangle. Hence, there are */
+/* three possible orientations. By convention, each edge is always */
+/* directed to point counterclockwise about the corresponding triangle. */
+
+struct triedge {
+ triangle *tri;
+ int orient; /* Ranges from 0 to 2. */
+};
+
+/* The shell data structure. Each shell edge contains two pointers to */
+/* adjoining shell edges, plus two pointers to vertex points, plus two */
+/* pointers to adjoining triangles, plus one shell marker. */
+
+typedef REAL **shelle; /* Really: typedef shelle *shelle */
+
+/* An oriented shell edge: includes a pointer to a shell edge and an */
+/* orientation. The orientation denotes a side of the edge. Hence, there */
+/* are two possible orientations. By convention, the edge is always */
+/* directed so that the "side" denoted is the right side of the edge. */
+
+struct edge {
+ shelle *sh;
+ int shorient; /* Ranges from 0 to 1. */
+};
+
+/* The point data structure. Each point is actually an array of REALs. */
+/* The number of REALs is unknown until runtime. An integer boundary */
+/* marker, and sometimes a pointer to a triangle, is appended after the */
+/* REALs. */
+
+typedef REAL *point;
+
+/* A queue used to store encroached segments. Each segment's vertices are */
+/* stored so that one can check whether a segment is still the same. */
+
+struct badsegment {
+ struct edge encsegment; /* An encroached segment. */
+ point segorg, segdest; /* The two vertices. */
+ struct badsegment *nextsegment; /* Pointer to next encroached segment. */
+};
+
+/* A queue used to store bad triangles. The key is the square of the cosine */
+/* of the smallest angle of the triangle. Each triangle's vertices are */
+/* stored so that one can check whether a triangle is still the same. */
+
+struct badface {
+ struct triedge badfacetri; /* A bad triangle. */
+ REAL key; /* cos^2 of smallest (apical) angle. */
+ point faceorg, facedest, faceapex; /* The three vertices. */
+ struct badface *nextface; /* Pointer to next bad triangle. */
+};
+
+/* A node in a heap used to store events for the sweepline Delaunay */
+/* algorithm. Nodes do not point directly to their parents or children in */
+/* the heap. Instead, each node knows its position in the heap, and can */
+/* look up its parent and children in a separate array. The `eventptr' */
+/* points either to a `point' or to a triangle (in encoded format, so that */
+/* an orientation is included). In the latter case, the origin of the */
+/* oriented triangle is the apex of a "circle event" of the sweepline */
+/* algorithm. To distinguish site events from circle events, all circle */
+/* events are given an invalid (smaller than `xmin') x-coordinate `xkey'. */
+
+struct event {
+ REAL xkey, ykey; /* Coordinates of the event. */
+ VOID *eventptr; /* Can be a point or the location of a circle event. */
+ int heapposition; /* Marks this event's position in the heap. */
+};
+
+/* A node in the splay tree. Each node holds an oriented ghost triangle */
+/* that represents a boundary edge of the growing triangulation. When a */
+/* circle event covers two boundary edges with a triangle, so that they */
+/* are no longer boundary edges, those edges are not immediately deleted */
+/* from the tree; rather, they are lazily deleted when they are next */
+/* encountered. (Since only a random sample of boundary edges are kept */
+/* in the tree, lazy deletion is faster.) `keydest' is used to verify */
+/* that a triangle is still the same as when it entered the splay tree; if */
+/* it has been rotated (due to a circle event), it no longer represents a */
+/* boundary edge and should be deleted. */
+
+struct splaynode {
+ struct triedge keyedge; /* Lprev of an edge on the front. */
+ point keydest; /* Used to verify that splay node is still live. */
+ struct splaynode *lchild, *rchild; /* Children in splay tree. */
+};
+
+/* A type used to allocate memory. firstblock is the first block of items. */
+/* nowblock is the block from which items are currently being allocated. */
+/* nextitem points to the next slab of free memory for an item. */
+/* deaditemstack is the head of a linked list (stack) of deallocated items */
+/* that can be recycled. unallocateditems is the number of items that */
+/* remain to be allocated from nowblock. */
+/* */
+/* Traversal is the process of walking through the entire list of items, and */
+/* is separate from allocation. Note that a traversal will visit items on */
+/* the "deaditemstack" stack as well as live items. pathblock points to */
+/* the block currently being traversed. pathitem points to the next item */
+/* to be traversed. pathitemsleft is the number of items that remain to */
+/* be traversed in pathblock. */
+/* */
+/* itemwordtype is set to POINTER or FLOATINGPOINT, and is used to suggest */
+/* what sort of word the record is primarily made up of. alignbytes */
+/* determines how new records should be aligned in memory. itembytes and */
+/* itemwords are the length of a record in bytes (after rounding up) and */
+/* words. itemsperblock is the number of items allocated at once in a */
+/* single block. items is the number of currently allocated items. */
+/* maxitems is the maximum number of items that have been allocated at */
+/* once; it is the current number of items plus the number of records kept */
+/* on deaditemstack. */
+
+struct memorypool {
+ VOID **firstblock, **nowblock;
+ VOID *nextitem;
+ VOID *deaditemstack;
+ VOID **pathblock;
+ VOID *pathitem;
+ enum wordtype itemwordtype;
+ int alignbytes;
+ int itembytes, itemwords;
+ int itemsperblock;
+ long items, maxitems;
+ int unallocateditems;
+ int pathitemsleft;
+};
+
+/* Variables used to allocate memory for triangles, shell edges, points, */
+/* viri (triangles being eaten), bad (encroached) segments, bad (skinny */
+/* or too large) triangles, and splay tree nodes. */
+
+static struct memorypool triangles;
+static struct memorypool shelles;
+static struct memorypool points;
+static struct memorypool viri;
+static struct memorypool badsegments;
+static struct memorypool badtriangles;
+static struct memorypool splaynodes;
+
+/* Variables that maintain the bad triangle queues. The tails are pointers */
+/* to the pointers that have to be filled in to enqueue an item. */
+
+static struct badface *queuefront[64];
+static struct badface **queuetail[64];
+
+static REAL xmin, xmax, ymin, ymax; /* x and y bounds. */
+static REAL xminextreme; /* Nonexistent x value used as a flag in sweepline. */
+static int inpoints; /* Number of input points. */
+static int inelements; /* Number of input triangles. */
+static int insegments; /* Number of input segments. */
+static int holes; /* Number of input holes. */
+static int regions; /* Number of input regions. */
+static long edges; /* Number of output edges. */
+static int mesh_dim; /* Dimension (ought to be 2). */
+static int nextras; /* Number of attributes per point. */
+static int eextras; /* Number of attributes per triangle. */
+static long hullsize; /* Number of edges of convex hull. */
+static int triwords; /* Total words per triangle. */
+static int shwords; /* Total words per shell edge. */
+static int pointmarkindex; /* Index to find boundary marker of a point. */
+static int point2triindex; /* Index to find a triangle adjacent to a point. */
+static int highorderindex; /* Index to find extra nodes for high-order elements. */
+static int elemattribindex; /* Index to find attributes of a triangle. */
+static int areaboundindex; /* Index to find area bound of a triangle. */
+static int checksegments; /* Are there segments in the triangulation yet? */
+static int readnodefile; /* Has a .node file been read? */
+static long samples; /* Number of random samples for point location. */
+static unsigned long randomseed; /* Current random number seed. */
+
+static REAL splitter; /* Used to split REAL factors for exact multiplication. */
+static REAL epsilon; /* Floating-point machine epsilon. */
+static REAL resulterrbound;
+static REAL ccwerrboundA, ccwerrboundB, ccwerrboundC;
+static REAL iccerrboundA, iccerrboundB, iccerrboundC;
+
+static long incirclecount; /* Number of incircle tests performed. */
+static long counterclockcount; /* Number of counterclockwise tests performed. */
+static long hyperbolacount; /* Number of right-of-hyperbola tests performed. */
+static long circumcentercount; /* Number of circumcenter calculations performed. */
+static long circletopcount; /* Number of circle top calculations performed. */
+
+/* Switches for the triangulator. */
+/* poly: -p switch. refine: -r switch. */
+/* quality: -q switch. */
+/* minangle: minimum angle bound, specified after -q switch. */
+/* goodangle: cosine squared of minangle. */
+/* vararea: -a switch without number. */
+/* fixedarea: -a switch with number. */
+/* maxarea: maximum area bound, specified after -a switch. */
+/* regionattrib: -A switch. convex: -c switch. */
+/* firstnumber: inverse of -z switch. All items are numbered starting */
+/* from firstnumber. */
+/* edgesout: -e switch. voronoi: -v switch. */
+/* neighbors: -n switch. geomview: -g switch. */
+/* nobound: -B switch. nopolywritten: -P switch. */
+/* nonodewritten: -N switch. noelewritten: -E switch. */
+/* noiterationnum: -I switch. noholes: -O switch. */
+/* noexact: -X switch. */
+/* order: element order, specified after -o switch. */
+/* nobisect: count of how often -Y switch is selected. */
+/* steiner: maximum number of Steiner points, specified after -S switch. */
+/* steinerleft: number of Steiner points not yet used. */
+/* incremental: -i switch. sweepline: -F switch. */
+/* dwyer: inverse of -l switch. */
+/* splitseg: -s switch. */
+/* docheck: -C switch. */
+/* quiet: -Q switch. verbose: count of how often -V switch is selected. */
+/* useshelles: -p, -r, -q, or -c switch; determines whether shell edges */
+/* are used at all. */
+/* */
+/* Read the instructions to find out the meaning of these switches. */
+
+static int poly, refine, quality, vararea, fixedarea, regionattrib, convex;
+static int firstnumber;
+static int edgesout, voronoi, neighbors, geomview;
+static int nobound, nopolywritten, nonodewritten, noelewritten, noiterationnum;
+static int noholes, noexact;
+static int incremental, sweepline, dwyer;
+static int splitseg;
+static int docheck;
+static int quiet, verbose;
+static int useshelles;
+static int order;
+static int nobisect;
+static int steiner, steinerleft;
+static REAL minangle, goodangle;
+static REAL maxarea;
+
+/* Variables for file names. */
+
+#ifndef TRILIBRARY
+char innodefilename[FILENAMESIZE];
+char inelefilename[FILENAMESIZE];
+char inpolyfilename[FILENAMESIZE];
+char areafilename[FILENAMESIZE];
+char outnodefilename[FILENAMESIZE];
+char outelefilename[FILENAMESIZE];
+char outpolyfilename[FILENAMESIZE];
+char edgefilename[FILENAMESIZE];
+char vnodefilename[FILENAMESIZE];
+char vedgefilename[FILENAMESIZE];
+char neighborfilename[FILENAMESIZE];
+char offfilename[FILENAMESIZE];
+#endif /* not TRILIBRARY */
+
+/* Triangular bounding box points. */
+
+static point infpoint1, infpoint2, infpoint3;
+
+/* Pointer to the `triangle' that occupies all of "outer space". */
+
+static triangle *dummytri;
+static triangle *dummytribase; /* Keep base address so we can free() it later. */
+
+/* Pointer to the omnipresent shell edge. Referenced by any triangle or */
+/* shell edge that isn't really connected to a shell edge at that */
+/* location. */
+
+static shelle *dummysh;
+static shelle *dummyshbase; /* Keep base address so we can free() it later. */
+
+/* Pointer to a recently visited triangle. Improves point location if */
+/* proximate points are inserted sequentially. */
+
+static struct triedge recenttri;
+
+/*****************************************************************************/
+/* */
+/* Mesh manipulation primitives. Each triangle contains three pointers to */
+/* other triangles, with orientations. Each pointer points not to the */
+/* first byte of a triangle, but to one of the first three bytes of a */
+/* triangle. It is necessary to extract both the triangle itself and the */
+/* orientation. To save memory, I keep both pieces of information in one */
+/* pointer. To make this possible, I assume that all triangles are aligned */
+/* to four-byte boundaries. The `decode' routine below decodes a pointer, */
+/* extracting an orientation (in the range 0 to 2) and a pointer to the */
+/* beginning of a triangle. The `encode' routine compresses a pointer to a */
+/* triangle and an orientation into a single pointer. My assumptions that */
+/* triangles are four-byte-aligned and that the `unsigned long' type is */
+/* long enough to hold a pointer are two of the few kludges in this program.*/
+/* */
+/* Shell edges are manipulated similarly. A pointer to a shell edge */
+/* carries both an address and an orientation in the range 0 to 1. */
+/* */
+/* The other primitives take an oriented triangle or oriented shell edge, */
+/* and return an oriented triangle or oriented shell edge or point; or they */
+/* change the connections in the data structure. */
+/* */
+/*****************************************************************************/
+
+/********* Mesh manipulation primitives begin here *********/
+/** **/
+/** **/
+
+/* Fast lookup arrays to speed some of the mesh manipulation primitives. */
+
+int plus1mod3[3] = {1, 2, 0};
+int minus1mod3[3] = {2, 0, 1};
+
+/********* Primitives for triangles *********/
+/* */
+/* */
+
+/* decode() converts a pointer to an oriented triangle. The orientation is */
+/* extracted from the two least significant bits of the pointer. */
+
+#define decode(ptr, triedge) \
+ (triedge).orient = (int) ((unsigned long) (ptr) & (unsigned long) 3l); \
+ (triedge).tri = (triangle *) \
+ ((unsigned long) (ptr) ^ (unsigned long) (triedge).orient)
+
+/* encode() compresses an oriented triangle into a single pointer. It */
+/* relies on the assumption that all triangles are aligned to four-byte */
+/* boundaries, so the two least significant bits of (triedge).tri are zero.*/
+
+#define encode(triedge) \
+ (triangle) ((unsigned long) (triedge).tri | (unsigned long) (triedge).orient)
+
+/* The following edge manipulation primitives are all described by Guibas */
+/* and Stolfi. However, they use an edge-based data structure, whereas I */
+/* am using a triangle-based data structure. */
+
+/* sym() finds the abutting triangle, on the same edge. Note that the */
+/* edge direction is necessarily reversed, because triangle/edge handles */
+/* are always directed counterclockwise around the triangle. */
+
+#define sym(triedge1, triedge2) \
+ ptr = (triedge1).tri[(triedge1).orient]; \
+ decode(ptr, triedge2);
+
+#define symself(triedge) \
+ ptr = (triedge).tri[(triedge).orient]; \
+ decode(ptr, triedge);
+
+/* lnext() finds the next edge (counterclockwise) of a triangle. */
+
+#define lnext(triedge1, triedge2) \
+ (triedge2).tri = (triedge1).tri; \
+ (triedge2).orient = plus1mod3[(triedge1).orient]
+
+#define lnextself(triedge) \
+ (triedge).orient = plus1mod3[(triedge).orient]
+
+/* lprev() finds the previous edge (clockwise) of a triangle. */
+
+#define lprev(triedge1, triedge2) \
+ (triedge2).tri = (triedge1).tri; \
+ (triedge2).orient = minus1mod3[(triedge1).orient]
+
+#define lprevself(triedge) \
+ (triedge).orient = minus1mod3[(triedge).orient]
+
+/* onext() spins counterclockwise around a point; that is, it finds the next */
+/* edge with the same origin in the counterclockwise direction. This edge */
+/* will be part of a different triangle. */
+
+#define onext(triedge1, triedge2) \
+ lprev(triedge1, triedge2); \
+ symself(triedge2);
+
+#define onextself(triedge) \
+ lprevself(triedge); \
+ symself(triedge);
+
+/* oprev() spins clockwise around a point; that is, it finds the next edge */
+/* with the same origin in the clockwise direction. This edge will be */
+/* part of a different triangle. */
+
+#define oprev(triedge1, triedge2) \
+ sym(triedge1, triedge2); \
+ lnextself(triedge2);
+
+#define oprevself(triedge) \
+ symself(triedge); \
+ lnextself(triedge);
+
+/* dnext() spins counterclockwise around a point; that is, it finds the next */
+/* edge with the same destination in the counterclockwise direction. This */
+/* edge will be part of a different triangle. */
+
+#define dnext(triedge1, triedge2) \
+ sym(triedge1, triedge2); \
+ lprevself(triedge2);
+
+#define dnextself(triedge) \
+ symself(triedge); \
+ lprevself(triedge);
+
+/* dprev() spins clockwise around a point; that is, it finds the next edge */
+/* with the same destination in the clockwise direction. This edge will */
+/* be part of a different triangle. */
+
+#define dprev(triedge1, triedge2) \
+ lnext(triedge1, triedge2); \
+ symself(triedge2);
+
+#define dprevself(triedge) \
+ lnextself(triedge); \
+ symself(triedge);
+
+/* rnext() moves one edge counterclockwise about the adjacent triangle. */
+/* (It's best understood by reading Guibas and Stolfi. It involves */
+/* changing triangles twice.) */
+
+#define rnext(triedge1, triedge2) \
+ sym(triedge1, triedge2); \
+ lnextself(triedge2); \
+ symself(triedge2);
+
+#define rnextself(triedge) \
+ symself(triedge); \
+ lnextself(triedge); \
+ symself(triedge);
+
+/* rnext() moves one edge clockwise about the adjacent triangle. */
+/* (It's best understood by reading Guibas and Stolfi. It involves */
+/* changing triangles twice.) */
+
+#define rprev(triedge1, triedge2) \
+ sym(triedge1, triedge2); \
+ lprevself(triedge2); \
+ symself(triedge2);
+
+#define rprevself(triedge) \
+ symself(triedge); \
+ lprevself(triedge); \
+ symself(triedge);
+
+/* These primitives determine or set the origin, destination, or apex of a */
+/* triangle. */
+
+#define org(triedge, pointptr) \
+ pointptr = (point) (triedge).tri[plus1mod3[(triedge).orient] + 3]
+
+#define dest(triedge, pointptr) \
+ pointptr = (point) (triedge).tri[minus1mod3[(triedge).orient] + 3]
+
+#define apex(triedge, pointptr) \
+ pointptr = (point) (triedge).tri[(triedge).orient + 3]
+
+#define setorg(triedge, pointptr) \
+ (triedge).tri[plus1mod3[(triedge).orient] + 3] = (triangle) pointptr
+
+#define setdest(triedge, pointptr) \
+ (triedge).tri[minus1mod3[(triedge).orient] + 3] = (triangle) pointptr
+
+#define setapex(triedge, pointptr) \
+ (triedge).tri[(triedge).orient + 3] = (triangle) pointptr
+
+#define setvertices2null(triedge) \
+ (triedge).tri[3] = (triangle) NULL; \
+ (triedge).tri[4] = (triangle) NULL; \
+ (triedge).tri[5] = (triangle) NULL;
+
+/* Bond two triangles together. */
+
+#define bond(triedge1, triedge2) \
+ (triedge1).tri[(triedge1).orient] = encode(triedge2); \
+ (triedge2).tri[(triedge2).orient] = encode(triedge1)
+
+/* Dissolve a bond (from one side). Note that the other triangle will still */
+/* think it's connected to this triangle. Usually, however, the other */
+/* triangle is being deleted entirely, or bonded to another triangle, so */
+/* it doesn't matter. */
+
+#define dissolve(triedge) \
+ (triedge).tri[(triedge).orient] = (triangle) dummytri
+
+/* Copy a triangle/edge handle. */
+
+#define triedgecopy(triedge1, triedge2) \
+ (triedge2).tri = (triedge1).tri; \
+ (triedge2).orient = (triedge1).orient
+
+/* Test for equality of triangle/edge handles. */
+
+#define triedgeequal(triedge1, triedge2) \
+ (((triedge1).tri == (triedge2).tri) && \
+ ((triedge1).orient == (triedge2).orient))
+
+/* Primitives to infect or cure a triangle with the virus. These rely on */
+/* the assumption that all shell edges are aligned to four-byte boundaries.*/
+
+#define infect(triedge) \
+ (triedge).tri[6] = (triangle) \
+ ((unsigned long) (triedge).tri[6] | (unsigned long) 2l)
+
+#define uninfect(triedge) \
+ (triedge).tri[6] = (triangle) \
+ ((unsigned long) (triedge).tri[6] & ~ (unsigned long) 2l)
+
+/* Test a triangle for viral infection. */
+
+#define infected(triedge) \
+ (((unsigned long) (triedge).tri[6] & (unsigned long) 2l) != 0)
+
+/* Check or set a triangle's attributes. */
+
+#define elemattribute(triedge, attnum) \
+ ((REAL *) (triedge).tri)[elemattribindex + (attnum)]
+
+#define setelemattribute(triedge, attnum, value) \
+ ((REAL *) (triedge).tri)[elemattribindex + (attnum)] = (REAL)value
+
+/* Check or set a triangle's maximum area bound. */
+
+#define areabound(triedge) ((REAL *) (triedge).tri)[areaboundindex]
+
+#define setareabound(triedge, value) \
+ ((REAL *) (triedge).tri)[areaboundindex] = (REAL)value
+
+/********* Primitives for shell edges *********/
+/* */
+/* */
+
+/* sdecode() converts a pointer to an oriented shell edge. The orientation */
+/* is extracted from the least significant bit of the pointer. The two */
+/* least significant bits (one for orientation, one for viral infection) */
+/* are masked out to produce the real pointer. */
+
+#define sdecode(sptr, edge) \
+ (edge).shorient = (int) ((unsigned long) (sptr) & (unsigned long) 1l); \
+ (edge).sh = (shelle *) \
+ ((unsigned long) (sptr) & ~ (unsigned long) 3l)
+
+/* sencode() compresses an oriented shell edge into a single pointer. It */
+/* relies on the assumption that all shell edges are aligned to two-byte */
+/* boundaries, so the least significant bit of (edge).sh is zero. */
+
+#define sencode(edge) \
+ (shelle) ((unsigned long) (edge).sh | (unsigned long) (edge).shorient)
+
+/* ssym() toggles the orientation of a shell edge. */
+
+#define ssym(edge1, edge2) \
+ (edge2).sh = (edge1).sh; \
+ (edge2).shorient = 1 - (edge1).shorient
+
+#define ssymself(edge) \
+ (edge).shorient = 1 - (edge).shorient
+
+/* spivot() finds the other shell edge (from the same segment) that shares */
+/* the same origin. */
+
+#define spivot(edge1, edge2) \
+ sptr = (edge1).sh[(edge1).shorient]; \
+ sdecode(sptr, edge2)
+
+#define spivotself(edge) \
+ sptr = (edge).sh[(edge).shorient]; \
+ sdecode(sptr, edge)
+
+/* snext() finds the next shell edge (from the same segment) in sequence; */
+/* one whose origin is the input shell edge's destination. */
+
+#define snext(edge1, edge2) \
+ sptr = (edge1).sh[1 - (edge1).shorient]; \
+ sdecode(sptr, edge2)
+
+#define snextself(edge) \
+ sptr = (edge).sh[1 - (edge).shorient]; \
+ sdecode(sptr, edge)
+
+/* These primitives determine or set the origin or destination of a shell */
+/* edge. */
+
+#define sorg(edge, pointptr) \
+ pointptr = (point) (edge).sh[2 + (edge).shorient]
+
+#define sdest(edge, pointptr) \
+ pointptr = (point) (edge).sh[3 - (edge).shorient]
+
+#define setsorg(edge, pointptr) \
+ (edge).sh[2 + (edge).shorient] = (shelle) pointptr
+
+#define setsdest(edge, pointptr) \
+ (edge).sh[3 - (edge).shorient] = (shelle) pointptr
+
+/* These primitives read or set a shell marker. Shell markers are used to */
+/* hold user boundary information. */
+
+#define mark(edge) (* (int *) ((edge).sh + 6))
+
+#define setmark(edge, value) \
+ * (int *) ((edge).sh + 6) = value
+
+/* Bond two shell edges together. */
+
+#define sbond(edge1, edge2) \
+ (edge1).sh[(edge1).shorient] = sencode(edge2); \
+ (edge2).sh[(edge2).shorient] = sencode(edge1)
+
+/* Dissolve a shell edge bond (from one side). Note that the other shell */
+/* edge will still think it's connected to this shell edge. */
+
+#define sdissolve(edge) \
+ (edge).sh[(edge).shorient] = (shelle) dummysh
+
+/* Copy a shell edge. */
+
+#define shellecopy(edge1, edge2) \
+ (edge2).sh = (edge1).sh; \
+ (edge2).shorient = (edge1).shorient
+
+/* Test for equality of shell edges. */
+
+#define shelleequal(edge1, edge2) \
+ (((edge1).sh == (edge2).sh) && \
+ ((edge1).shorient == (edge2).shorient))
+
+/********* Primitives for interacting triangles and shell edges *********/
+/* */
+/* */
+
+/* tspivot() finds a shell edge abutting a triangle. */
+
+#define tspivot(triedge, edge) \
+ sptr = (shelle) (triedge).tri[6 + (triedge).orient]; \
+ sdecode(sptr, edge)
+
+/* stpivot() finds a triangle abutting a shell edge. It requires that the */
+/* variable `ptr' of type `triangle' be defined. */
+
+#define stpivot(edge, triedge) \
+ ptr = (triangle) (edge).sh[4 + (edge).shorient]; \
+ decode(ptr, triedge)
+
+/* Bond a triangle to a shell edge. */
+
+#define tsbond(triedge, edge) \
+ (triedge).tri[6 + (triedge).orient] = (triangle) sencode(edge); \
+ (edge).sh[4 + (edge).shorient] = (shelle) encode(triedge)
+
+/* Dissolve a bond (from the triangle side). */
+
+#define tsdissolve(triedge) \
+ (triedge).tri[6 + (triedge).orient] = (triangle) dummysh
+
+/* Dissolve a bond (from the shell edge side). */
+
+#define stdissolve(edge) \
+ (edge).sh[4 + (edge).shorient] = (shelle) dummytri
+
+/********* Primitives for points *********/
+/* */
+/* */
+
+#define pointmark(pt) ((int *) (pt))[pointmarkindex]
+
+#define setpointmark(pt, value) \
+ ((int *) (pt))[pointmarkindex] = value
+
+#define point2tri(pt) ((triangle *) (pt))[point2triindex]
+
+#define setpoint2tri(pt, value) \
+ ((triangle *) (pt))[point2triindex] = value
+
+/** **/
+/** **/
+/********* Mesh manipulation primitives end here *********/
+
+/********* User interaction routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* syntax() Print list of command line switches. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+void syntax()
+{
+#ifdef CDT_ONLY
+#ifdef REDUCED
+ printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n");
+#else /* not REDUCED */
+ printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n");
+#endif /* not REDUCED */
+#else /* not CDT_ONLY */
+#ifdef REDUCED
+ printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n");
+#else /* not REDUCED */
+ printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n");
+#endif /* not REDUCED */
+#endif /* not CDT_ONLY */
+
+ printf(" -p Triangulates a Planar Straight Line Graph (.poly file).\n");
+#ifndef CDT_ONLY
+ printf(" -r Refines a previously generated mesh.\n");
+ printf(
+ " -q Quality mesh generation. A minimum angle may be specified.\n");
+ printf(" -a Applies a maximum triangle area constraint.\n");
+#endif /* not CDT_ONLY */
+ printf(
+ " -A Applies attributes to identify elements in certain regions.\n");
+ printf(" -c Encloses the convex hull with segments.\n");
+ printf(" -e Generates an edge list.\n");
+ printf(" -v Generates a Voronoi diagram.\n");
+ printf(" -n Generates a list of triangle neighbors.\n");
+ printf(" -g Generates an .off file for Geomview.\n");
+ printf(" -B Suppresses output of boundary information.\n");
+ printf(" -P Suppresses output of .poly file.\n");
+ printf(" -N Suppresses output of .node file.\n");
+ printf(" -E Suppresses output of .ele file.\n");
+ printf(" -I Suppresses mesh iteration numbers.\n");
+ printf(" -O Ignores holes in .poly file.\n");
+ printf(" -X Suppresses use of exact arithmetic.\n");
+ printf(" -z Numbers all items starting from zero (rather than one).\n");
+ printf(" -o2 Generates second-order subparametric elements.\n");
+#ifndef CDT_ONLY
+ printf(" -Y Suppresses boundary segment splitting.\n");
+ printf(" -S Specifies maximum number of added Steiner points.\n");
+#endif /* not CDT_ONLY */
+#ifndef REDUCED
+ printf(" -i Uses incremental method, rather than divide-and-conquer.\n");
+ printf(" -F Uses Fortune's sweepline algorithm, rather than d-and-c.\n");
+#endif /* not REDUCED */
+ printf(" -l Uses vertical cuts only, rather than alternating cuts.\n");
+#ifndef REDUCED
+#ifndef CDT_ONLY
+ printf(
+ " -s Force segments into mesh by splitting (instead of using CDT).\n");
+#endif /* not CDT_ONLY */
+ printf(" -C Check consistency of final mesh.\n");
+#endif /* not REDUCED */
+ printf(" -Q Quiet: No terminal output except errors.\n");
+ printf(" -V Verbose: Detailed information on what I'm doing.\n");
+ printf(" -h Help: Detailed instructions for Triangle.\n");
+ exit(0);
+}
+
+#endif /* not TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* info() Print out complete instructions. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+void info()
+{
+ printf("Triangle\n");
+ printf(
+"A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.\n");
+ printf("Version 1.3\n\n");
+ printf(
+"Copyright 1996 Jonathan Richard Shewchuk (bugs/comments to jrs@cs.cmu.edu)\n"
+);
+ printf("School of Computer Science / Carnegie Mellon University\n");
+ printf("5000 Forbes Avenue / Pittsburgh, Pennsylvania 15213-3891\n");
+ printf(
+"Created as part of the Archimedes project (tools for parallel FEM).\n");
+ printf(
+"Supported in part by NSF Grant CMS-9318163 and an NSERC 1967 Scholarship.\n");
+ printf("There is no warranty whatsoever. Use at your own risk.\n");
+#ifdef SINGLE
+ printf("This executable is compiled for single precision arithmetic.\n\n\n");
+#else /* not SINGLE */
+ printf("This executable is compiled for double precision arithmetic.\n\n\n");
+#endif /* not SINGLE */
+ printf(
+"Triangle generates exact Delaunay triangulations, constrained Delaunay\n");
+ printf(
+"triangulations, and quality conforming Delaunay triangulations. The latter\n"
+);
+ printf(
+"can be generated with no small angles, and are thus suitable for finite\n");
+ printf(
+"element analysis. If no command line switches are specified, your .node\n");
+ printf(
+"input file will be read, and the Delaunay triangulation will be returned in\n"
+);
+ printf(".node and .ele output files. The command syntax is:\n\n");
+#ifdef CDT_ONLY
+#ifdef REDUCED
+ printf("triangle [-pAcevngBPNEIOXzo_lQVh] input_file\n\n");
+#else /* not REDUCED */
+ printf("triangle [-pAcevngBPNEIOXzo_iFlCQVh] input_file\n\n");
+#endif /* not REDUCED */
+#else /* not CDT_ONLY */
+#ifdef REDUCED
+ printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__lQVh] input_file\n\n");
+#else /* not REDUCED */
+ printf("triangle [-prq__a__AcevngBPNEIOXzo_YS__iFlsCQVh] input_file\n\n");
+#endif /* not REDUCED */
+#endif /* not CDT_ONLY */
+ printf(
+"Underscores indicate that numbers may optionally follow certain switches;\n");
+ printf(
+"do not leave any space between a switch and its numeric parameter.\n");
+ printf(
+"input_file must be a file with extension .node, or extension .poly if the\n");
+ printf(
+"-p switch is used. If -r is used, you must supply .node and .ele files,\n");
+ printf(
+"and possibly a .poly file and .area file as well. The formats of these\n");
+ printf("files are described below.\n\n");
+ printf("Command Line Switches:\n\n");
+ printf(
+" -p Reads a Planar Straight Line Graph (.poly file), which can specify\n"
+);
+ printf(
+" points, segments, holes, and regional attributes and area\n");
+ printf(
+" constraints. Will generate a constrained Delaunay triangulation\n");
+ printf(
+" fitting the input; or, if -s, -q, or -a is used, a conforming\n");
+ printf(
+" Delaunay triangulation. If -p is not used, Triangle reads a .node\n"
+);
+ printf(" file by default.\n");
+ printf(
+" -r Refines a previously generated mesh. The mesh is read from a .node\n"
+);
+ printf(
+" file and an .ele file. If -p is also used, a .poly file is read\n");
+ printf(
+" and used to constrain edges in the mesh. Further details on\n");
+ printf(" refinement are given below.\n");
+ printf(
+" -q Quality mesh generation by Jim Ruppert's Delaunay refinement\n");
+ printf(
+" algorithm. Adds points to the mesh to ensure that no angles\n");
+ printf(
+" smaller than 20 degrees occur. An alternative minimum angle may be\n"
+);
+ printf(
+" specified after the `q'. If the minimum angle is 20.7 degrees or\n");
+ printf(
+" smaller, the triangulation algorithm is theoretically guaranteed to\n"
+);
+ printf(
+" terminate (assuming infinite precision arithmetic - Triangle may\n");
+ printf(
+" fail to terminate if you run out of precision). In practice, the\n");
+ printf(
+" algorithm often succeeds for minimum angles up to 33.8 degrees.\n");
+ printf(
+" For highly refined meshes, however, it may be necessary to reduce\n");
+ printf(
+" the minimum angle to well below 20 to avoid problems associated\n");
+ printf(
+" with insufficient floating-point precision. The specified angle\n");
+ printf(" may include a decimal point.\n");
+ printf(
+" -a Imposes a maximum triangle area. If a number follows the `a', no\n");
+ printf(
+" triangle will be generated whose area is larger than that number.\n");
+ printf(
+" If no number is specified, an .area file (if -r is used) or .poly\n");
+ printf(
+" file (if -r is not used) specifies a number of maximum area\n");
+ printf(
+" constraints. An .area file contains a separate area constraint for\n"
+);
+ printf(
+" each triangle, and is useful for refining a finite element mesh\n");
+ printf(
+" based on a posteriori error estimates. A .poly file can optionally\n"
+);
+ printf(
+" contain an area constraint for each segment-bounded region, thereby\n"
+);
+ printf(
+" enforcing triangle densities in a first triangulation. You can\n");
+ printf(
+" impose both a fixed area constraint and a varying area constraint\n");
+ printf(
+" by invoking the -a switch twice, once with and once without a\n");
+ printf(
+" number following. Each area specified may include a decimal point.\n"
+);
+ printf(
+" -A Assigns an additional attribute to each triangle that identifies\n");
+ printf(
+" what segment-bounded region each triangle belongs to. Attributes\n");
+ printf(
+" are assigned to regions by the .poly file. If a region is not\n");
+ printf(
+" explicitly marked by the .poly file, triangles in that region are\n");
+ printf(
+" assigned an attribute of zero. The -A switch has an effect only\n");
+ printf(" when the -p switch is used and the -r switch is not.\n");
+ printf(
+" -c Creates segments on the convex hull of the triangulation. If you\n");
+ printf(
+" are triangulating a point set, this switch causes a .poly file to\n");
+ printf(
+" be written, containing all edges in the convex hull. (By default,\n"
+);
+ printf(
+" a .poly file is written only if a .poly file is read.) If you are\n"
+);
+ printf(
+" triangulating a PSLG, this switch specifies that the interior of\n");
+ printf(
+" the convex hull of the PSLG should be triangulated. If you do not\n"
+);
+ printf(
+" use this switch when triangulating a PSLG, it is assumed that you\n");
+ printf(
+" have identified the region to be triangulated by surrounding it\n");
+ printf(
+" with segments of the input PSLG. Beware: if you are not careful,\n"
+);
+ printf(
+" this switch can cause the introduction of an extremely thin angle\n");
+ printf(
+" between a PSLG segment and a convex hull segment, which can cause\n");
+ printf(
+" overrefinement or failure if Triangle runs out of precision. If\n");
+ printf(
+" you are refining a mesh, the -c switch works differently; it\n");
+ printf(
+" generates the set of boundary edges of the mesh, rather than the\n");
+ printf(" convex hull.\n");
+ printf(
+" -e Outputs (to an .edge file) a list of edges of the triangulation.\n");
+ printf(
+" -v Outputs the Voronoi diagram associated with the triangulation.\n");
+ printf(" Does not attempt to detect degeneracies.\n");
+ printf(
+" -n Outputs (to a .neigh file) a list of triangles neighboring each\n");
+ printf(" triangle.\n");
+ printf(
+" -g Outputs the mesh to an Object File Format (.off) file, suitable for\n"
+);
+ printf(" viewing with the Geometry Center's Geomview package.\n");
+ printf(
+" -B No boundary markers in the output .node, .poly, and .edge output\n");
+ printf(
+" files. See the detailed discussion of boundary markers below.\n");
+ printf(
+" -P No output .poly file. Saves disk space, but you lose the ability\n");
+ printf(
+" to impose segment constraints on later refinements of the mesh.\n");
+ printf(" -N No output .node file.\n");
+ printf(" -E No output .ele file.\n");
+ printf(
+" -I No iteration numbers. Suppresses the output of .node and .poly\n");
+ printf(
+" files, so your input files won't be overwritten. (If your input is\n"
+);
+ printf(
+" a .poly file only, a .node file will be written.) Cannot be used\n");
+ printf(
+" with the -r switch, because that would overwrite your input .ele\n");
+ printf(
+" file. Shouldn't be used with the -s, -q, or -a switch if you are\n");
+ printf(
+" using a .node file for input, because no .node file will be\n");
+ printf(" written, so there will be no record of any added points.\n");
+ printf(" -O No holes. Ignores the holes in the .poly file.\n");
+ printf(
+" -X No exact arithmetic. Normally, Triangle uses exact floating-point\n"
+);
+ printf(
+" arithmetic for certain tests if it thinks the inexact tests are not\n"
+);
+ printf(
+" accurate enough. Exact arithmetic ensures the robustness of the\n");
+ printf(
+" triangulation algorithms, despite floating-point roundoff error.\n");
+ printf(
+" Disabling exact arithmetic with the -X switch will cause a small\n");
+ printf(
+" improvement in speed and create the possibility (albeit small) that\n"
+);
+ printf(
+" Triangle will fail to produce a valid mesh. Not recommended.\n");
+ printf(
+" -z Numbers all items starting from zero (rather than one). Note that\n"
+);
+ printf(
+" this switch is normally overrided by the value used to number the\n");
+ printf(
+" first point of the input .node or .poly file. However, this switch\n"
+);
+ printf(" is useful when calling Triangle from another program.\n");
+ printf(
+" -o2 Generates second-order subparametric elements with six nodes each.\n"
+);
+ printf(
+" -Y No new points on the boundary. This switch is useful when the mesh\n"
+);
+ printf(
+" boundary must be preserved so that it conforms to some adjacent\n");
+ printf(
+" mesh. Be forewarned that you will probably sacrifice some of the\n");
+ printf(
+" quality of the mesh; Triangle will try, but the resulting mesh may\n"
+);
+ printf(
+" contain triangles of poor aspect ratio. Works well if all the\n");
+ printf(
+" boundary points are closely spaced. Specify this switch twice\n");
+ printf(
+" (`-YY') to prevent all segment splitting, including internal\n");
+ printf(" boundaries.\n");
+ printf(
+" -S Specifies the maximum number of Steiner points (points that are not\n"
+);
+ printf(
+" in the input, but are added to meet the constraints of minimum\n");
+ printf(
+" angle and maximum area). The default is to allow an unlimited\n");
+ printf(
+" number. If you specify this switch with no number after it,\n");
+ printf(
+" the limit is set to zero. Triangle always adds points at segment\n");
+ printf(
+" intersections, even if it needs to use more points than the limit\n");
+ printf(
+" you set. When Triangle inserts segments by splitting (-s), it\n");
+ printf(
+" always adds enough points to ensure that all the segments appear in\n"
+);
+ printf(
+" the triangulation, again ignoring the limit. Be forewarned that\n");
+ printf(
+" the -S switch may result in a conforming triangulation that is not\n"
+);
+ printf(
+" truly Delaunay, because Triangle may be forced to stop adding\n");
+ printf(
+" points when the mesh is in a state where a segment is non-Delaunay\n"
+);
+ printf(
+" and needs to be split. If so, Triangle will print a warning.\n");
+ printf(
+" -i Uses an incremental rather than divide-and-conquer algorithm to\n");
+ printf(
+" form a Delaunay triangulation. Try it if the divide-and-conquer\n");
+ printf(" algorithm fails.\n");
+ printf(
+" -F Uses Steven Fortune's sweepline algorithm to form a Delaunay\n");
+ printf(
+" triangulation. Warning: does not use exact arithmetic for all\n");
+ printf(" calculations. An exact result is not guaranteed.\n");
+ printf(
+" -l Uses only vertical cuts in the divide-and-conquer algorithm. By\n");
+ printf(
+" default, Triangle uses alternating vertical and horizontal cuts,\n");
+ printf(
+" which usually improve the speed except with point sets that are\n");
+ printf(
+" small or short and wide. This switch is primarily of theoretical\n");
+ printf(" interest.\n");
+ printf(
+" -s Specifies that segments should be forced into the triangulation by\n"
+);
+ printf(
+" recursively splitting them at their midpoints, rather than by\n");
+ printf(
+" generating a constrained Delaunay triangulation. Segment splitting\n"
+);
+ printf(
+" is true to Ruppert's original algorithm, but can create needlessly\n"
+);
+ printf(" small triangles near external small features.\n");
+ printf(
+" -C Check the consistency of the final mesh. Uses exact arithmetic for\n"
+);
+ printf(
+" checking, even if the -X switch is used. Useful if you suspect\n");
+ printf(" Triangle is buggy.\n");
+ printf(
+" -Q Quiet: Suppresses all explanation of what Triangle is doing, unless\n"
+);
+ printf(" an error occurs.\n");
+ printf(
+" -V Verbose: Gives detailed information about what Triangle is doing.\n");
+ printf(
+" Add more `V's for increasing amount of detail. `-V' gives\n");
+ printf(
+" information on algorithmic progress and more detailed statistics.\n");
+ printf(
+" `-VV' gives point-by-point details, and will print so much that\n");
+ printf(
+" Triangle will run much more slowly. `-VVV' gives information only\n"
+);
+ printf(" a debugger could love.\n");
+ printf(" -h Help: Displays these instructions.\n");
+ printf("\n");
+ printf("Definitions:\n");
+ printf("\n");
+ printf(
+" A Delaunay triangulation of a point set is a triangulation whose vertices\n"
+);
+ printf(
+" are the point set, having the property that no point in the point set\n");
+ printf(
+" falls in the interior of the circumcircle (circle that passes through all\n"
+);
+ printf(" three vertices) of any triangle in the triangulation.\n\n");
+ printf(
+" A Voronoi diagram of a point set is a subdivision of the plane into\n");
+ printf(
+" polygonal regions (some of which may be infinite), where each region is\n");
+ printf(
+" the set of points in the plane that are closer to some input point than\n");
+ printf(
+" to any other input point. (The Voronoi diagram is the geometric dual of\n"
+);
+ printf(" the Delaunay triangulation.)\n\n");
+ printf(
+" A Planar Straight Line Graph (PSLG) is a collection of points and\n");
+ printf(
+" segments. Segments are simply edges, whose endpoints are points in the\n");
+ printf(
+" PSLG. The file format for PSLGs (.poly files) is described below.\n");
+ printf("\n");
+ printf(
+" A constrained Delaunay triangulation of a PSLG is similar to a Delaunay\n");
+ printf(
+" triangulation, but each PSLG segment is present as a single edge in the\n");
+ printf(
+" triangulation. (A constrained Delaunay triangulation is not truly a\n");
+ printf(" Delaunay triangulation.)\n\n");
+ printf(
+" A conforming Delaunay triangulation of a PSLG is a true Delaunay\n");
+ printf(
+" triangulation in which each PSLG segment may have been subdivided into\n");
+ printf(
+" several edges by the insertion of additional points. These inserted\n");
+ printf(
+" points are necessary to allow the segments to exist in the mesh while\n");
+ printf(" maintaining the Delaunay property.\n\n");
+ printf("File Formats:\n\n");
+ printf(
+" All files may contain comments prefixed by the character '#'. Points,\n");
+ printf(
+" triangles, edges, holes, and maximum area constraints must be numbered\n");
+ printf(
+" consecutively, starting from either 1 or 0. Whichever you choose, all\n");
+ printf(
+" input files must be consistent; if the nodes are numbered from 1, so must\n"
+);
+ printf(
+" be all other objects. Triangle automatically detects your choice while\n");
+ printf(
+" reading the .node (or .poly) file. (When calling Triangle from another\n");
+ printf(
+" program, use the -z switch if you wish to number objects from zero.)\n");
+ printf(" Examples of these file formats are given below.\n\n");
+ printf(" .node files:\n");
+ printf(
+" First line: <# of points> <dimension (must be 2)> <# of attributes>\n");
+ printf(
+" <# of boundary markers (0 or 1)>\n"
+);
+ printf(
+" Remaining lines: <point #> <x> <y> [attributes] [boundary marker]\n");
+ printf("\n");
+ printf(
+" The attributes, which are typically floating-point values of physical\n");
+ printf(
+" quantities (such as mass or conductivity) associated with the nodes of\n"
+);
+ printf(
+" a finite element mesh, are copied unchanged to the output mesh. If -s,\n"
+);
+ printf(
+" -q, or -a is selected, each new Steiner point added to the mesh will\n");
+ printf(" have attributes assigned to it by linear interpolation.\n\n");
+ printf(
+" If the fourth entry of the first line is `1', the last column of the\n");
+ printf(
+" remainder of the file is assumed to contain boundary markers. Boundary\n"
+);
+ printf(
+" markers are used to identify boundary points and points resting on PSLG\n"
+);
+ printf(
+" segments; a complete description appears in a section below. The .node\n"
+);
+ printf(
+" file produced by Triangle will contain boundary markers in the last\n");
+ printf(" column unless they are suppressed by the -B switch.\n\n");
+ printf(" .ele files:\n");
+ printf(
+" First line: <# of triangles> <points per triangle> <# of attributes>\n");
+ printf(
+" Remaining lines: <triangle #> <point> <point> <point> ... [attributes]\n"
+);
+ printf("\n");
+ printf(
+" Points are indices into the corresponding .node file. The first three\n"
+);
+ printf(
+" points are the corners, and are listed in counterclockwise order around\n"
+);
+ printf(
+" each triangle. (The remaining points, if any, depend on the type of\n");
+ printf(
+" finite element used.) The attributes are just like those of .node\n");
+ printf(
+" files. Because there is no simple mapping from input to output\n");
+ printf(
+" triangles, an attempt is made to interpolate attributes, which may\n");
+ printf(
+" result in a good deal of diffusion of attributes among nearby triangles\n"
+);
+ printf(
+" as the triangulation is refined. Diffusion does not occur across\n");
+ printf(
+" segments, so attributes used to identify segment-bounded regions remain\n"
+);
+ printf(
+" intact. In output .ele files, all triangles have three points each\n");
+ printf(
+" unless the -o2 switch is used, in which case they have six, and the\n");
+ printf(
+" fourth, fifth, and sixth points lie on the midpoints of the edges\n");
+ printf(" opposite the first, second, and third corners.\n\n");
+ printf(" .poly files:\n");
+ printf(
+" First line: <# of points> <dimension (must be 2)> <# of attributes>\n");
+ printf(
+" <# of boundary markers (0 or 1)>\n"
+);
+ printf(
+" Following lines: <point #> <x> <y> [attributes] [boundary marker]\n");
+ printf(" One line: <# of segments> <# of boundary markers (0 or 1)>\n");
+ printf(
+" Following lines: <segment #> <endpoint> <endpoint> [boundary marker]\n");
+ printf(" One line: <# of holes>\n");
+ printf(" Following lines: <hole #> <x> <y>\n");
+ printf(
+" Optional line: <# of regional attributes and/or area constraints>\n");
+ printf(
+" Optional following lines: <constraint #> <x> <y> <attrib> <max area>\n");
+ printf("\n");
+ printf(
+" A .poly file represents a PSLG, as well as some additional information.\n"
+);
+ printf(
+" The first section lists all the points, and is identical to the format\n"
+);
+ printf(
+" of .node files. <# of points> may be set to zero to indicate that the\n"
+);
+ printf(
+" points are listed in a separate .node file; .poly files produced by\n");
+ printf(
+" Triangle always have this format. This has the advantage that a point\n"
+);
+ printf(
+" set may easily be triangulated with or without segments. (The same\n");
+ printf(
+" effect can be achieved, albeit using more disk space, by making a copy\n"
+);
+ printf(
+" of the .poly file with the extension .node; all sections of the file\n");
+ printf(" but the first are ignored.)\n\n");
+ printf(
+" The second section lists the segments. Segments are edges whose\n");
+ printf(
+" presence in the triangulation is enforced. Each segment is specified\n");
+ printf(
+" by listing the indices of its two endpoints. This means that you must\n"
+);
+ printf(
+" include its endpoints in the point list. If -s, -q, and -a are not\n");
+ printf(
+" selected, Triangle will produce a constrained Delaunay triangulation,\n");
+ printf(
+" in which each segment appears as a single edge in the triangulation.\n");
+ printf(
+" If -q or -a is selected, Triangle will produce a conforming Delaunay\n");
+ printf(
+" triangulation, in which segments may be subdivided into smaller edges.\n"
+);
+ printf(" Each segment, like each point, may have a boundary marker.\n\n");
+ printf(
+" The third section lists holes (and concavities, if -c is selected) in\n");
+ printf(
+" the triangulation. Holes are specified by identifying a point inside\n");
+ printf(
+" each hole. After the triangulation is formed, Triangle creates holes\n");
+ printf(
+" by eating triangles, spreading out from each hole point until its\n");
+ printf(
+" progress is blocked by PSLG segments; you must be careful to enclose\n");
+ printf(
+" each hole in segments, or your whole triangulation may be eaten away.\n");
+ printf(
+" If the two triangles abutting a segment are eaten, the segment itself\n");
+ printf(
+" is also eaten. Do not place a hole directly on a segment; if you do,\n");
+ printf(" Triangle will choose one side of the segment arbitrarily.\n\n");
+ printf(
+" The optional fourth section lists regional attributes (to be assigned\n");
+ printf(
+" to all triangles in a region) and regional constraints on the maximum\n");
+ printf(
+" triangle area. Triangle will read this section only if the -A switch\n");
+ printf(
+" is used or the -a switch is used without a number following it, and the\n"
+);
+ printf(
+" -r switch is not used. Regional attributes and area constraints are\n");
+ printf(
+" propagated in the same manner as holes; you specify a point for each\n");
+ printf(
+" attribute and/or constraint, and the attribute and/or constraint will\n");
+ printf(
+" affect the whole region (bounded by segments) containing the point. If\n"
+);
+ printf(
+" two values are written on a line after the x and y coordinate, the\n");
+ printf(
+" former is assumed to be a regional attribute (but will only be applied\n"
+);
+ printf(
+" if the -A switch is selected), and the latter is assumed to be a\n");
+ printf(
+" regional area constraint (but will only be applied if the -a switch is\n"
+);
+ printf(
+" selected). You may also specify just one value after the coordinates,\n"
+);
+ printf(
+" which can serve as both an attribute and an area constraint, depending\n"
+);
+ printf(
+" on the choice of switches. If you are using the -A and -a switches\n");
+ printf(
+" simultaneously and wish to assign an attribute to some region without\n");
+ printf(" imposing an area constraint, use a negative maximum area.\n\n");
+ printf(
+" When a triangulation is created from a .poly file, you must either\n");
+ printf(
+" enclose the entire region to be triangulated in PSLG segments, or\n");
+ printf(
+" use the -c switch, which encloses the convex hull of the input point\n");
+ printf(
+" set. If you do not use the -c switch, Triangle will eat all triangles\n"
+);
+ printf(
+" on the outer boundary that are not protected by segments; if you are\n");
+ printf(
+" not careful, your whole triangulation may be eaten away. If you do\n");
+ printf(
+" use the -c switch, you can still produce concavities by appropriate\n");
+ printf(" placement of holes just inside the convex hull.\n\n");
+ printf(
+" An ideal PSLG has no intersecting segments, nor any points that lie\n");
+ printf(
+" upon segments (except, of course, the endpoints of each segment.) You\n"
+);
+ printf(
+" aren't required to make your .poly files ideal, but you should be aware\n"
+);
+ printf(
+" of what can go wrong. Segment intersections are relatively safe -\n");
+ printf(
+" Triangle will calculate the intersection points for you and add them to\n"
+);
+ printf(
+" the triangulation - as long as your machine's floating-point precision\n"
+);
+ printf(
+" doesn't become a problem. You are tempting the fates if you have three\n"
+);
+ printf(
+" segments that cross at the same location, and expect Triangle to figure\n"
+);
+ printf(
+" out where the intersection point is. Thanks to floating-point roundoff\n"
+);
+ printf(
+" error, Triangle will probably decide that the three segments intersect\n"
+);
+ printf(
+" at three different points, and you will find a minuscule triangle in\n");
+ printf(
+" your output - unless Triangle tries to refine the tiny triangle, uses\n");
+ printf(
+" up the last bit of machine precision, and fails to terminate at all.\n");
+ printf(
+" You're better off putting the intersection point in the input files,\n");
+ printf(
+" and manually breaking up each segment into two. Similarly, if you\n");
+ printf(
+" place a point at the middle of a segment, and hope that Triangle will\n");
+ printf(
+" break up the segment at that point, you might get lucky. On the other\n"
+);
+ printf(
+" hand, Triangle might decide that the point doesn't lie precisely on the\n"
+);
+ printf(
+" line, and you'll have a needle-sharp triangle in your output - or a lot\n"
+);
+ printf(" of tiny triangles if you're generating a quality mesh.\n\n");
+ printf(
+" When Triangle reads a .poly file, it also writes a .poly file, which\n");
+ printf(
+" includes all edges that are part of input segments. If the -c switch\n");
+ printf(
+" is used, the output .poly file will also include all of the edges on\n");
+ printf(
+" the convex hull. Hence, the output .poly file is useful for finding\n");
+ printf(
+" edges associated with input segments and setting boundary conditions in\n"
+);
+ printf(
+" finite element simulations. More importantly, you will need it if you\n"
+);
+ printf(
+" plan to refine the output mesh, and don't want segments to be missing\n");
+ printf(" in later triangulations.\n\n");
+ printf(" .area files:\n");
+ printf(" First line: <# of triangles>\n");
+ printf(" Following lines: <triangle #> <maximum area>\n\n");
+ printf(
+" An .area file associates with each triangle a maximum area that is used\n"
+);
+ printf(
+" for mesh refinement. As with other file formats, every triangle must\n");
+ printf(
+" be represented, and they must be numbered consecutively. A triangle\n");
+ printf(
+" may be left unconstrained by assigning it a negative maximum area.\n");
+ printf("\n");
+ printf(" .edge files:\n");
+ printf(" First line: <# of edges> <# of boundary markers (0 or 1)>\n");
+ printf(
+" Following lines: <edge #> <endpoint> <endpoint> [boundary marker]\n");
+ printf("\n");
+ printf(
+" Endpoints are indices into the corresponding .node file. Triangle can\n"
+);
+ printf(
+" produce .edge files (use the -e switch), but cannot read them. The\n");
+ printf(
+" optional column of boundary markers is suppressed by the -B switch.\n");
+ printf("\n");
+ printf(
+" In Voronoi diagrams, one also finds a special kind of edge that is an\n");
+ printf(
+" infinite ray with only one endpoint. For these edges, a different\n");
+ printf(" format is used:\n\n");
+ printf(" <edge #> <endpoint> -1 <direction x> <direction y>\n\n");
+ printf(
+" The `direction' is a floating-point vector that indicates the direction\n"
+);
+ printf(" of the infinite ray.\n\n");
+ printf(" .neigh files:\n");
+ printf(
+" First line: <# of triangles> <# of neighbors per triangle (always 3)>\n"
+);
+ printf(
+" Following lines: <triangle #> <neighbor> <neighbor> <neighbor>\n");
+ printf("\n");
+ printf(
+" Neighbors are indices into the corresponding .ele file. An index of -1\n"
+);
+ printf(
+" indicates a mesh boundary, and therefore no neighbor. Triangle can\n");
+ printf(
+" produce .neigh files (use the -n switch), but cannot read them.\n");
+ printf("\n");
+ printf(
+" The first neighbor of triangle i is opposite the first corner of\n");
+ printf(" triangle i, and so on.\n\n");
+ printf("Boundary Markers:\n\n");
+ printf(
+" Boundary markers are tags used mainly to identify which output points and\n"
+);
+ printf(
+" edges are associated with which PSLG segment, and to identify which\n");
+ printf(
+" points and edges occur on a boundary of the triangulation. A common use\n"
+);
+ printf(
+" is to determine where boundary conditions should be applied to a finite\n");
+ printf(
+" element mesh. You can prevent boundary markers from being written into\n");
+ printf(" files produced by Triangle by using the -B switch.\n\n");
+ printf(
+" The boundary marker associated with each segment in an output .poly file\n"
+);
+ printf(" or edge in an output .edge file is chosen as follows:\n");
+ printf(
+" - If an output edge is part or all of a PSLG segment with a nonzero\n");
+ printf(
+" boundary marker, then the edge is assigned the same marker.\n");
+ printf(
+" - Otherwise, if the edge occurs on a boundary of the triangulation\n");
+ printf(
+" (including boundaries of holes), then the edge is assigned the marker\n"
+);
+ printf(" one (1).\n");
+ printf(" - Otherwise, the edge is assigned the marker zero (0).\n");
+ printf(
+" The boundary marker associated with each point in an output .node file is\n"
+);
+ printf(" chosen as follows:\n");
+ printf(
+" - If a point is assigned a nonzero boundary marker in the input file,\n");
+ printf(
+" then it is assigned the same marker in the output .node file.\n");
+ printf(
+" - Otherwise, if the point lies on a PSLG segment (including the\n");
+ printf(
+" segment's endpoints) with a nonzero boundary marker, then the point\n");
+ printf(
+" is assigned the same marker. If the point lies on several such\n");
+ printf(" segments, one of the markers is chosen arbitrarily.\n");
+ printf(
+" - Otherwise, if the point occurs on a boundary of the triangulation,\n");
+ printf(" then the point is assigned the marker one (1).\n");
+ printf(" - Otherwise, the point is assigned the marker zero (0).\n");
+ printf("\n");
+ printf(
+" If you want Triangle to determine for you which points and edges are on\n");
+ printf(
+" the boundary, assign them the boundary marker zero (or use no markers at\n"
+);
+ printf(
+" all) in your input files. Alternatively, you can mark some of them and\n");
+ printf(" leave others marked zero, allowing Triangle to label them.\n\n");
+ printf("Triangulation Iteration Numbers:\n\n");
+ printf(
+" Because Triangle can read and refine its own triangulations, input\n");
+ printf(
+" and output files have iteration numbers. For instance, Triangle might\n");
+ printf(
+" read the files mesh.3.node, mesh.3.ele, and mesh.3.poly, refine the\n");
+ printf(
+" triangulation, and output the files mesh.4.node, mesh.4.ele, and\n");
+ printf(" mesh.4.poly. Files with no iteration number are treated as if\n");
+ printf(
+" their iteration number is zero; hence, Triangle might read the file\n");
+ printf(
+" points.node, triangulate it, and produce the files points.1.node and\n");
+ printf(" points.1.ele.\n\n");
+ printf(
+" Iteration numbers allow you to create a sequence of successively finer\n");
+ printf(
+" meshes suitable for multigrid methods. They also allow you to produce a\n"
+);
+ printf(
+" sequence of meshes using error estimate-driven mesh refinement.\n");
+ printf("\n");
+ printf(
+" If you're not using refinement or quality meshing, and you don't like\n");
+ printf(
+" iteration numbers, use the -I switch to disable them. This switch will\n");
+ printf(
+" also disable output of .node and .poly files to prevent your input files\n"
+);
+ printf(
+" from being overwritten. (If the input is a .poly file that contains its\n"
+);
+ printf(" own points, a .node file will be written.)\n\n");
+ printf("Examples of How to Use Triangle:\n\n");
+ printf(
+" `triangle dots' will read points from dots.node, and write their Delaunay\n"
+);
+ printf(
+" triangulation to dots.1.node and dots.1.ele. (dots.1.node will be\n");
+ printf(
+" identical to dots.node.) `triangle -I dots' writes the triangulation to\n"
+);
+ printf(
+" dots.ele instead. (No additional .node file is needed, so none is\n");
+ printf(" written.)\n\n");
+ printf(
+" `triangle -pe object.1' will read a PSLG from object.1.poly (and possibly\n"
+);
+ printf(
+" object.1.node, if the points are omitted from object.1.poly) and write\n");
+ printf(" their constrained Delaunay triangulation to object.2.node and\n");
+ printf(
+" object.2.ele. The segments will be copied to object.2.poly, and all\n");
+ printf(" edges will be written to object.2.edge.\n\n");
+ printf(
+" `triangle -pq31.5a.1 object' will read a PSLG from object.poly (and\n");
+ printf(
+" possibly object.node), generate a mesh whose angles are all greater than\n"
+);
+ printf(
+" 31.5 degrees and whose triangles all have area smaller than 0.1, and\n");
+ printf(
+" write the mesh to object.1.node and object.1.ele. Each segment may have\n"
+);
+ printf(
+" been broken up into multiple edges; the resulting constrained edges are\n");
+ printf(" written to object.1.poly.\n\n");
+ printf(
+" Here is a sample file `box.poly' describing a square with a square hole:\n"
+);
+ printf("\n");
+ printf(
+" # A box with eight points in 2D, no attributes, one boundary marker.\n");
+ printf(" 8 2 0 1\n");
+ printf(" # Outer box has these vertices:\n");
+ printf(" 1 0 0 0\n");
+ printf(" 2 0 3 0\n");
+ printf(" 3 3 0 0\n");
+ printf(" 4 3 3 33 # A special marker for this point.\n");
+ printf(" # Inner square has these vertices:\n");
+ printf(" 5 1 1 0\n");
+ printf(" 6 1 2 0\n");
+ printf(" 7 2 1 0\n");
+ printf(" 8 2 2 0\n");
+ printf(" # Five segments with boundary markers.\n");
+ printf(" 5 1\n");
+ printf(" 1 1 2 5 # Left side of outer box.\n");
+ printf(" 2 5 7 0 # Segments 2 through 5 enclose the hole.\n");
+ printf(" 3 7 8 0\n");
+ printf(" 4 8 6 10\n");
+ printf(" 5 6 5 0\n");
+ printf(" # One hole in the middle of the inner square.\n");
+ printf(" 1\n");
+ printf(" 1 1.5 1.5\n\n");
+ printf(
+" Note that some segments are missing from the outer square, so one must\n");
+ printf(
+" use the `-c' switch. After `triangle -pqc box.poly', here is the output\n"
+);
+ printf(
+" file `box.1.node', with twelve points. The last four points were added\n");
+ printf(
+" to meet the angle constraint. Points 1, 2, and 9 have markers from\n");
+ printf(
+" segment 1. Points 6 and 8 have markers from segment 4. All the other\n");
+ printf(
+" points but 4 have been marked to indicate that they lie on a boundary.\n");
+ printf("\n");
+ printf(" 12 2 0 1\n");
+ printf(" 1 0 0 5\n");
+ printf(" 2 0 3 5\n");
+ printf(" 3 3 0 1\n");
+ printf(" 4 3 3 33\n");
+ printf(" 5 1 1 1\n");
+ printf(" 6 1 2 10\n");
+ printf(" 7 2 1 1\n");
+ printf(" 8 2 2 10\n");
+ printf(" 9 0 1.5 5\n");
+ printf(" 10 1.5 0 1\n");
+ printf(" 11 3 1.5 1\n");
+ printf(" 12 1.5 3 1\n");
+ printf(" # Generated by triangle -pqc box.poly\n\n");
+ printf(" Here is the output file `box.1.ele', with twelve triangles.\n\n");
+ printf(" 12 3 0\n");
+ printf(" 1 5 6 9\n");
+ printf(" 2 10 3 7\n");
+ printf(" 3 6 8 12\n");
+ printf(" 4 9 1 5\n");
+ printf(" 5 6 2 9\n");
+ printf(" 6 7 3 11\n");
+ printf(" 7 11 4 8\n");
+ printf(" 8 7 5 10\n");
+ printf(" 9 12 2 6\n");
+ printf(" 10 8 7 11\n");
+ printf(" 11 5 1 10\n");
+ printf(" 12 8 4 12\n");
+ printf(" # Generated by triangle -pqc box.poly\n\n");
+ printf(
+" Here is the output file `box.1.poly'. Note that segments have been added\n"
+);
+ printf(
+" to represent the convex hull, and some segments have been split by newly\n"
+);
+ printf(
+" added points. Note also that <# of points> is set to zero to indicate\n");
+ printf(" that the points should be read from the .node file.\n\n");
+ printf(" 0 2 0 1\n");
+ printf(" 12 1\n");
+ printf(" 1 1 9 5\n");
+ printf(" 2 5 7 1\n");
+ printf(" 3 8 7 1\n");
+ printf(" 4 6 8 10\n");
+ printf(" 5 5 6 1\n");
+ printf(" 6 3 10 1\n");
+ printf(" 7 4 11 1\n");
+ printf(" 8 2 12 1\n");
+ printf(" 9 9 2 5\n");
+ printf(" 10 10 1 1\n");
+ printf(" 11 11 3 1\n");
+ printf(" 12 12 4 1\n");
+ printf(" 1\n");
+ printf(" 1 1.5 1.5\n");
+ printf(" # Generated by triangle -pqc box.poly\n\n");
+ printf("Refinement and Area Constraints:\n\n");
+ printf(
+" The -r switch causes a mesh (.node and .ele files) to be read and\n");
+ printf(
+" refined. If the -p switch is also used, a .poly file is read and used to\n"
+);
+ printf(
+" specify edges that are constrained and cannot be eliminated (although\n");
+ printf(
+" they can be divided into smaller edges) by the refinement process.\n");
+ printf("\n");
+ printf(
+" When you refine a mesh, you generally want to impose tighter quality\n");
+ printf(
+" constraints. One way to accomplish this is to use -q with a larger\n");
+ printf(
+" angle, or -a followed by a smaller area than you used to generate the\n");
+ printf(
+" mesh you are refining. Another way to do this is to create an .area\n");
+ printf(
+" file, which specifies a maximum area for each triangle, and use the -a\n");
+ printf(
+" switch (without a number following). Each triangle's area constraint is\n"
+);
+ printf(
+" applied to that triangle. Area constraints tend to diffuse as the mesh\n");
+ printf(
+" is refined, so if there are large variations in area constraint between\n");
+ printf(" adjacent triangles, you may not get the results you want.\n\n");
+ printf(
+" If you are refining a mesh composed of linear (three-node) elements, the\n"
+);
+ printf(
+" output mesh will contain all the nodes present in the input mesh, in the\n"
+);
+ printf(
+" same order, with new nodes added at the end of the .node file. However,\n"
+);
+ printf(
+" there is no guarantee that each output element is contained in a single\n");
+ printf(
+" input element. Often, output elements will overlap two input elements,\n");
+ printf(
+" and input edges are not present in the output mesh. Hence, a sequence of\n"
+);
+ printf(
+" refined meshes will form a hierarchy of nodes, but not a hierarchy of\n");
+ printf(
+" elements. If you a refining a mesh of higher-order elements, the\n");
+ printf(
+" hierarchical property applies only to the nodes at the corners of an\n");
+ printf(" element; other nodes may not be present in the refined mesh.\n\n");
+ printf(
+" It is important to understand that maximum area constraints in .poly\n");
+ printf(
+" files are handled differently from those in .area files. A maximum area\n"
+);
+ printf(
+" in a .poly file applies to the whole (segment-bounded) region in which a\n"
+);
+ printf(
+" point falls, whereas a maximum area in an .area file applies to only one\n"
+);
+ printf(
+" triangle. Area constraints in .poly files are used only when a mesh is\n");
+ printf(
+" first generated, whereas area constraints in .area files are used only to\n"
+);
+ printf(
+" refine an existing mesh, and are typically based on a posteriori error\n");
+ printf(
+" estimates resulting from a finite element simulation on that mesh.\n");
+ printf("\n");
+ printf(
+" `triangle -rq25 object.1' will read object.1.node and object.1.ele, then\n"
+);
+ printf(
+" refine the triangulation to enforce a 25 degree minimum angle, and then\n");
+ printf(
+" write the refined triangulation to object.2.node and object.2.ele.\n");
+ printf("\n");
+ printf(
+" `triangle -rpaa6.2 z.3' will read z.3.node, z.3.ele, z.3.poly, and\n");
+ printf(
+" z.3.area. After reconstructing the mesh and its segments, Triangle will\n"
+);
+ printf(
+" refine the mesh so that no triangle has area greater than 6.2, and\n");
+ printf(
+" furthermore the triangles satisfy the maximum area constraints in\n");
+ printf(
+" z.3.area. The output is written to z.4.node, z.4.ele, and z.4.poly.\n");
+ printf("\n");
+ printf(
+" The sequence `triangle -qa1 x', `triangle -rqa.3 x.1', `triangle -rqa.1\n");
+ printf(
+" x.2' creates a sequence of successively finer meshes x.1, x.2, and x.3,\n");
+ printf(" suitable for multigrid.\n\n");
+ printf("Convex Hulls and Mesh Boundaries:\n\n");
+ printf(
+" If the input is a point set (rather than a PSLG), Triangle produces its\n");
+ printf(
+" convex hull as a by-product in the output .poly file if you use the -c\n");
+ printf(
+" switch. There are faster algorithms for finding a two-dimensional convex\n"
+);
+ printf(
+" hull than triangulation, of course, but this one comes for free. If the\n"
+);
+ printf(
+" input is an unconstrained mesh (you are using the -r switch but not the\n");
+ printf(
+" -p switch), Triangle produces a list of its boundary edges (including\n");
+ printf(" hole boundaries) as a by-product if you use the -c switch.\n\n");
+ printf("Voronoi Diagrams:\n\n");
+ printf(
+" The -v switch produces a Voronoi diagram, in files suffixed .v.node and\n");
+ printf(
+" .v.edge. For example, `triangle -v points' will read points.node,\n");
+ printf(
+" produce its Delaunay triangulation in points.1.node and points.1.ele,\n");
+ printf(
+" and produce its Voronoi diagram in points.1.v.node and points.1.v.edge.\n");
+ printf(
+" The .v.node file contains a list of all Voronoi vertices, and the .v.edge\n"
+);
+ printf(
+" file contains a list of all Voronoi edges, some of which may be infinite\n"
+);
+ printf(
+" rays. (The choice of filenames makes it easy to run the set of Voronoi\n");
+ printf(" vertices through Triangle, if so desired.)\n\n");
+ printf(
+" This implementation does not use exact arithmetic to compute the Voronoi\n"
+);
+ printf(
+" vertices, and does not check whether neighboring vertices are identical.\n"
+);
+ printf(
+" Be forewarned that if the Delaunay triangulation is degenerate or\n");
+ printf(
+" near-degenerate, the Voronoi diagram may have duplicate points, crossing\n"
+);
+ printf(
+" edges, or infinite rays whose direction vector is zero. Also, if you\n");
+ printf(
+" generate a constrained (as opposed to conforming) Delaunay triangulation,\n"
+);
+ printf(
+" or if the triangulation has holes, the corresponding Voronoi diagram is\n");
+ printf(" likely to have crossing edges and unlikely to make sense.\n\n");
+ printf("Mesh Topology:\n\n");
+ printf(
+" You may wish to know which triangles are adjacent to a certain Delaunay\n");
+ printf(
+" edge in an .edge file, which Voronoi regions are adjacent to a certain\n");
+ printf(
+" Voronoi edge in a .v.edge file, or which Voronoi regions are adjacent to\n"
+);
+ printf(
+" each other. All of this information can be found by cross-referencing\n");
+ printf(
+" output files with the recollection that the Delaunay triangulation and\n");
+ printf(" the Voronoi diagrams are planar duals.\n\n");
+ printf(
+" Specifically, edge i of an .edge file is the dual of Voronoi edge i of\n");
+ printf(
+" the corresponding .v.edge file, and is rotated 90 degrees counterclock-\n");
+ printf(
+" wise from the Voronoi edge. Triangle j of an .ele file is the dual of\n");
+ printf(
+" vertex j of the corresponding .v.node file; and Voronoi region k is the\n");
+ printf(" dual of point k of the corresponding .node file.\n\n");
+ printf(
+" Hence, to find the triangles adjacent to a Delaunay edge, look at the\n");
+ printf(
+" vertices of the corresponding Voronoi edge; their dual triangles are on\n");
+ printf(
+" the left and right of the Delaunay edge, respectively. To find the\n");
+ printf(
+" Voronoi regions adjacent to a Voronoi edge, look at the endpoints of the\n"
+);
+ printf(
+" corresponding Delaunay edge; their dual regions are on the right and left\n"
+);
+ printf(
+" of the Voronoi edge, respectively. To find which Voronoi regions are\n");
+ printf(" adjacent to each other, just read the list of Delaunay edges.\n");
+ printf("\n");
+ printf("Statistics:\n");
+ printf("\n");
+ printf(
+" After generating a mesh, Triangle prints a count of the number of points,\n"
+);
+ printf(
+" triangles, edges, boundary edges, and segments in the output mesh. If\n");
+ printf(
+" you've forgotten the statistics for an existing mesh, the -rNEP switches\n"
+);
+ printf(
+" (or -rpNEP if you've got a .poly file for the existing mesh) will\n");
+ printf(" regenerate these statistics without writing any output.\n\n");
+ printf(
+" The -V switch produces extended statistics, including a rough estimate\n");
+ printf(
+" of memory use and a histogram of triangle aspect ratios and angles in the\n"
+);
+ printf(" mesh.\n\n");
+ printf("Exact Arithmetic:\n\n");
+ printf(
+" Triangle uses adaptive exact arithmetic to perform what computational\n");
+ printf(
+" geometers call the `orientation' and `incircle' tests. If the floating-\n"
+);
+ printf(
+" point arithmetic of your machine conforms to the IEEE 754 standard (as\n");
+ printf(
+" most workstations do), and does not use extended precision internal\n");
+ printf(
+" registers, then your output is guaranteed to be an absolutely true\n");
+ printf(" Delaunay or conforming Delaunay triangulation, roundoff error\n");
+ printf(
+" notwithstanding. The word `adaptive' implies that these arithmetic\n");
+ printf(
+" routines compute the result only to the precision necessary to guarantee\n"
+);
+ printf(
+" correctness, so they are usually nearly as fast as their approximate\n");
+ printf(
+" counterparts. The exact tests can be disabled with the -X switch. On\n");
+ printf(
+" most inputs, this switch will reduce the computation time by about eight\n"
+);
+ printf(
+" percent - it's not worth the risk. There are rare difficult inputs\n");
+ printf(
+" (having many collinear and cocircular points), however, for which the\n");
+ printf(
+" difference could be a factor of two. These are precisely the inputs most\n"
+);
+ printf(" likely to cause errors if you use the -X switch.\n\n");
+ printf(
+" Unfortunately, these routines don't solve every numerical problem. Exact\n"
+);
+ printf(
+" arithmetic is not used to compute the positions of points, because the\n");
+ printf(
+" bit complexity of point coordinates would grow without bound. Hence,\n");
+ printf(
+" segment intersections aren't computed exactly; in very unusual cases,\n");
+ printf(
+" roundoff error in computing an intersection point might actually lead to\n"
+);
+ printf(
+" an inverted triangle and an invalid triangulation. (This is one reason\n");
+ printf(
+" to compute your own intersection points in your .poly files.) Similarly,\n"
+);
+ printf(
+" exact arithmetic is not used to compute the vertices of the Voronoi\n");
+ printf(" diagram.\n\n");
+ printf(
+" Underflow and overflow can also cause difficulties; the exact arithmetic\n"
+);
+ printf(
+" routines do not ameliorate out-of-bounds exponents, which can arise\n");
+ printf(
+" during the orientation and incircle tests. As a rule of thumb, you\n");
+ printf(
+" should ensure that your input values are within a range such that their\n");
+ printf(
+" third powers can be taken without underflow or overflow. Underflow can\n");
+ printf(
+" silently prevent the tests from being performed exactly, while overflow\n");
+ printf(" will typically cause a floating exception.\n\n");
+ printf("Calling Triangle from Another Program:\n\n");
+ printf(" Read the file triangle.h for details.\n\n");
+ printf("Troubleshooting:\n\n");
+ printf(" Please read this section before mailing me bugs.\n\n");
+ printf(" `My output mesh has no triangles!'\n\n");
+ printf(
+" If you're using a PSLG, you've probably failed to specify a proper set\n"
+);
+ printf(
+" of bounding segments, or forgotten to use the -c switch. Or you may\n");
+ printf(
+" have placed a hole badly. To test these possibilities, try again with\n"
+);
+ printf(
+" the -c and -O switches. Alternatively, all your input points may be\n");
+ printf(
+" collinear, in which case you can hardly expect to triangulate them.\n");
+ printf("\n");
+ printf(" `Triangle doesn't terminate, or just crashes.'\n");
+ printf("\n");
+ printf(
+" Bad things can happen when triangles get so small that the distance\n");
+ printf(
+" between their vertices isn't much larger than the precision of your\n");
+ printf(
+" machine's arithmetic. If you've compiled Triangle for single-precision\n"
+);
+ printf(
+" arithmetic, you might do better by recompiling it for double-precision.\n"
+);
+ printf(
+" Then again, you might just have to settle for more lenient constraints\n"
+);
+ printf(
+" on the minimum angle and the maximum area than you had planned.\n");
+ printf("\n");
+ printf(
+" You can minimize precision problems by ensuring that the origin lies\n");
+ printf(
+" inside your point set, or even inside the densest part of your\n");
+ printf(
+" mesh. On the other hand, if you're triangulating an object whose x\n");
+ printf(
+" coordinates all fall between 6247133 and 6247134, you're not leaving\n");
+ printf(" much floating-point precision for Triangle to work with.\n\n");
+ printf(
+" Precision problems can occur covertly if the input PSLG contains two\n");
+ printf(
+" segments that meet (or intersect) at a very small angle, or if such an\n"
+);
+ printf(
+" angle is introduced by the -c switch, which may occur if a point lies\n");
+ printf(
+" ever-so-slightly inside the convex hull, and is connected by a PSLG\n");
+ printf(
+" segment to a point on the convex hull. If you don't realize that a\n");
+ printf(
+" small angle is being formed, you might never discover why Triangle is\n");
+ printf(
+" crashing. To check for this possibility, use the -S switch (with an\n");
+ printf(
+" appropriate limit on the number of Steiner points, found by trial-and-\n"
+);
+ printf(
+" error) to stop Triangle early, and view the output .poly file with\n");
+ printf(
+" Show Me (described below). Look carefully for small angles between\n");
+ printf(
+" segments; zoom in closely, as such segments might look like a single\n");
+ printf(" segment from a distance.\n\n");
+ printf(
+" If some of the input values are too large, Triangle may suffer a\n");
+ printf(
+" floating exception due to overflow when attempting to perform an\n");
+ printf(
+" orientation or incircle test. (Read the section on exact arithmetic\n");
+ printf(
+" above.) Again, I recommend compiling Triangle for double (rather\n");
+ printf(" than single) precision arithmetic.\n\n");
+ printf(
+" `The numbering of the output points doesn't match the input points.'\n");
+ printf("\n");
+ printf(
+" You may have eaten some of your input points with a hole, or by placing\n"
+);
+ printf(" them outside the area enclosed by segments.\n\n");
+ printf(
+" `Triangle executes without incident, but when I look at the resulting\n");
+ printf(
+" mesh, it has overlapping triangles or other geometric inconsistencies.'\n");
+ printf("\n");
+ printf(
+" If you select the -X switch, Triangle's divide-and-conquer Delaunay\n");
+ printf(
+" triangulation algorithm occasionally makes mistakes due to floating-\n");
+ printf(
+" point roundoff error. Although these errors are rare, don't use the -X\n"
+);
+ printf(" switch. If you still have problems, please report the bug.\n");
+ printf("\n");
+ printf(
+" Strange things can happen if you've taken liberties with your PSLG. Do\n");
+ printf(
+" you have a point lying in the middle of a segment? Triangle sometimes\n");
+ printf(
+" copes poorly with that sort of thing. Do you want to lay out a collinear\n"
+);
+ printf(
+" row of evenly spaced, segment-connected points? Have you simply defined\n"
+);
+ printf(
+" one long segment connecting the leftmost point to the rightmost point,\n");
+ printf(
+" and a bunch of points lying along it? This method occasionally works,\n");
+ printf(
+" especially with horizontal and vertical lines, but often it doesn't, and\n"
+);
+ printf(
+" you'll have to connect each adjacent pair of points with a separate\n");
+ printf(" segment. If you don't like it, tough.\n\n");
+ printf(
+" Furthermore, if you have segments that intersect other than at their\n");
+ printf(
+" endpoints, try not to let the intersections fall extremely close to PSLG\n"
+);
+ printf(" points or each other.\n\n");
+ printf(
+" If you have problems refining a triangulation not produced by Triangle:\n");
+ printf(
+" Are you sure the triangulation is geometrically valid? Is it formatted\n");
+ printf(
+" correctly for Triangle? Are the triangles all listed so the first three\n"
+);
+ printf(" points are their corners in counterclockwise order?\n\n");
+ printf("Show Me:\n\n");
+ printf(
+" Triangle comes with a separate program named `Show Me', whose primary\n");
+ printf(
+" purpose is to draw meshes on your screen or in PostScript. Its secondary\n"
+);
+ printf(
+" purpose is to check the validity of your input files, and do so more\n");
+ printf(
+" thoroughly than Triangle does. Show Me requires that you have the X\n");
+ printf(
+" Windows system. If you didn't receive Show Me with Triangle, complain to\n"
+);
+ printf(" whomever you obtained Triangle from, then send me mail.\n\n");
+ printf("Triangle on the Web:\n\n");
+ printf(
+" To see an illustrated, updated version of these instructions, check out\n");
+ printf("\n");
+ printf(" http://www.cs.cmu.edu/~quake/triangle.html\n");
+ printf("\n");
+ printf("A Brief Plea:\n");
+ printf("\n");
+ printf(
+" If you use Triangle, and especially if you use it to accomplish real\n");
+ printf(
+" work, I would like very much to hear from you. A short letter or email\n");
+ printf(
+" (to jrs@cs.cmu.edu) describing how you use Triangle will mean a lot to\n");
+ printf(
+" me. The more people I know are using this program, the more easily I can\n"
+);
+ printf(
+" justify spending time on improvements and on the three-dimensional\n");
+ printf(
+" successor to Triangle, which in turn will benefit you. Also, I can put\n");
+ printf(
+" you on a list to receive email whenever a new version of Triangle is\n");
+ printf(" available.\n\n");
+ printf(
+" If you use a mesh generated by Triangle in a publication, please include\n"
+);
+ printf(" an acknowledgment as well.\n\n");
+ printf("Research credit:\n\n");
+ printf(
+" Of course, I can take credit for only a fraction of the ideas that made\n");
+ printf(
+" this mesh generator possible. Triangle owes its existence to the efforts\n"
+);
+ printf(
+" of many fine computational geometers and other researchers, including\n");
+ printf(
+" Marshall Bern, L. Paul Chew, Boris Delaunay, Rex A. Dwyer, David\n");
+ printf(
+" Eppstein, Steven Fortune, Leonidas J. Guibas, Donald E. Knuth, C. L.\n");
+ printf(
+" Lawson, Der-Tsai Lee, Ernst P. Mucke, Douglas M. Priest, Jim Ruppert,\n");
+ printf(
+" Isaac Saias, Bruce J. Schachter, Micha Sharir, Jorge Stolfi, Christopher\n"
+);
+ printf(
+" J. Van Wyk, David F. Watson, and Binhai Zhu. See the comments at the\n");
+ printf(" beginning of the source code for references.\n\n");
+ exit(0);
+}
+
+#endif /* not TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* internalerror() Ask the user to send me the defective product. Exit. */
+/* */
+/*****************************************************************************/
+
+void internalerror()
+{
+ printf(" Please report this bug to jrs@cs.cmu.edu\n");
+ printf(" Include the message above, your input data set, and the exact\n");
+ printf(" command line you used to run Triangle.\n");
+ exit(1);
+}
+
+/*****************************************************************************/
+/* */
+/* parsecommandline() Read the command line, identify switches, and set */
+/* up options and file names. */
+/* */
+/* The effects of this routine are felt entirely through global variables. */
+/* */
+/*****************************************************************************/
+
+void parsecommandline(argc, argv)
+int argc;
+char **argv;
+{
+#ifdef TRILIBRARY
+#define STARTINDEX 0
+#else /* not TRILIBRARY */
+#define STARTINDEX 1
+ int increment;
+ int meshnumber;
+#endif /* not TRILIBRARY */
+ int i, j;
+#ifndef CDT_ONLY
+ int k;
+ char workstring[FILENAMESIZE];
+#endif
+
+ poly = refine = quality = vararea = fixedarea = regionattrib = convex = 0;
+ firstnumber = 1;
+ edgesout = voronoi = neighbors = geomview = 0;
+ nobound = nopolywritten = nonodewritten = noelewritten = noiterationnum = 0;
+ noholes = noexact = 0;
+ incremental = sweepline = 0;
+ dwyer = 1;
+ splitseg = 0;
+ docheck = 0;
+ nobisect = 0;
+ steiner = -1;
+ order = 1;
+ minangle = 0.0;
+ maxarea = -1.0;
+ quiet = verbose = 0;
+#ifndef TRILIBRARY
+ innodefilename[0] = '\0';
+#endif /* not TRILIBRARY */
+
+ for (i = STARTINDEX; i < argc; i++) {
+#ifndef TRILIBRARY
+ if (argv[i][0] == '-') {
+#endif /* not TRILIBRARY */
+ for (j = STARTINDEX; argv[i][j] != '\0'; j++) {
+ if (argv[i][j] == 'p') {
+ poly = 1;
+ }
+#ifndef CDT_ONLY
+ if (argv[i][j] == 'r') {
+ refine = 1;
+ }
+ if (argv[i][j] == 'q') {
+ quality = 1;
+ if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
+ (argv[i][j + 1] == '.')) {
+ k = 0;
+ while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
+ (argv[i][j + 1] == '.')) {
+ j++;
+ workstring[k] = argv[i][j];
+ k++;
+ }
+ workstring[k] = '\0';
+ minangle = (REAL) strtod(workstring, (char **) NULL);
+ } else {
+ minangle = 20.0;
+ }
+ }
+ if (argv[i][j] == 'a') {
+ quality = 1;
+ if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
+ (argv[i][j + 1] == '.')) {
+ fixedarea = 1;
+ k = 0;
+ while (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
+ (argv[i][j + 1] == '.')) {
+ j++;
+ workstring[k] = argv[i][j];
+ k++;
+ }
+ workstring[k] = '\0';
+ maxarea = (REAL) strtod(workstring, (char **) NULL);
+ if (maxarea <= 0.0) {
+ printf("Error: Maximum area must be greater than zero.\n");
+ exit(1);
+ }
+ } else {
+ vararea = 1;
+ }
+ }
+#endif /* not CDT_ONLY */
+ if (argv[i][j] == 'A') {
+ regionattrib = 1;
+ }
+ if (argv[i][j] == 'c') {
+ convex = 1;
+ }
+ if (argv[i][j] == 'z') {
+ firstnumber = 0;
+ }
+ if (argv[i][j] == 'e') {
+ edgesout = 1;
+ }
+ if (argv[i][j] == 'v') {
+ voronoi = 1;
+ }
+ if (argv[i][j] == 'n') {
+ neighbors = 1;
+ }
+ if (argv[i][j] == 'g') {
+ geomview = 1;
+ }
+ if (argv[i][j] == 'B') {
+ nobound = 1;
+ }
+ if (argv[i][j] == 'P') {
+ nopolywritten = 1;
+ }
+ if (argv[i][j] == 'N') {
+ nonodewritten = 1;
+ }
+ if (argv[i][j] == 'E') {
+ noelewritten = 1;
+ }
+#ifndef TRILIBRARY
+ if (argv[i][j] == 'I') {
+ noiterationnum = 1;
+ }
+#endif /* not TRILIBRARY */
+ if (argv[i][j] == 'O') {
+ noholes = 1;
+ }
+ if (argv[i][j] == 'X') {
+ noexact = 1;
+ }
+ if (argv[i][j] == 'o') {
+ if (argv[i][j + 1] == '2') {
+ j++;
+ order = 2;
+ }
+ }
+#ifndef CDT_ONLY
+ if (argv[i][j] == 'Y') {
+ nobisect++;
+ }
+ if (argv[i][j] == 'S') {
+ steiner = 0;
+ while ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) {
+ j++;
+ steiner = steiner * 10 + (int) (argv[i][j] - '0');
+ }
+ }
+#endif /* not CDT_ONLY */
+#ifndef REDUCED
+ if (argv[i][j] == 'i') {
+ incremental = 1;
+ }
+ if (argv[i][j] == 'F') {
+ sweepline = 1;
+ }
+#endif /* not REDUCED */
+ if (argv[i][j] == 'l') {
+ dwyer = 0;
+ }
+#ifndef REDUCED
+#ifndef CDT_ONLY
+ if (argv[i][j] == 's') {
+ splitseg = 1;
+ }
+#endif /* not CDT_ONLY */
+ if (argv[i][j] == 'C') {
+ docheck = 1;
+ }
+#endif /* not REDUCED */
+ if (argv[i][j] == 'Q') {
+ quiet = 1;
+ }
+ if (argv[i][j] == 'V') {
+ verbose++;
+ }
+#ifndef TRILIBRARY
+ if ((argv[i][j] == 'h') || (argv[i][j] == 'H') ||
+ (argv[i][j] == '?')) {
+ info();
+ }
+#endif /* not TRILIBRARY */
+ }
+#ifndef TRILIBRARY
+ } else {
+ strncpy(innodefilename, argv[i], FILENAMESIZE - 1);
+ innodefilename[FILENAMESIZE - 1] = '\0';
+ }
+#endif /* not TRILIBRARY */
+ }
+#ifndef TRILIBRARY
+ if (innodefilename[0] == '\0') {
+ syntax();
+ }
+ if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".node")) {
+ innodefilename[strlen(innodefilename) - 5] = '\0';
+ }
+ if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".poly")) {
+ innodefilename[strlen(innodefilename) - 5] = '\0';
+ poly = 1;
+ }
+#ifndef CDT_ONLY
+ if (!strcmp(&innodefilename[strlen(innodefilename) - 4], ".ele")) {
+ innodefilename[strlen(innodefilename) - 4] = '\0';
+ refine = 1;
+ }
+ if (!strcmp(&innodefilename[strlen(innodefilename) - 5], ".area")) {
+ innodefilename[strlen(innodefilename) - 5] = '\0';
+ refine = 1;
+ quality = 1;
+ vararea = 1;
+ }
+#endif /* not CDT_ONLY */
+#endif /* not TRILIBRARY */
+ steinerleft = steiner;
+ useshelles = poly || refine || quality || convex;
+ goodangle = (REAL)cos(minangle * PI / 180.0);
+ goodangle *= goodangle;
+ if (refine && noiterationnum) {
+ printf(
+ "Error: You cannot use the -I switch when refining a triangulation.\n");
+ exit(1);
+ }
+ /* Be careful not to allocate space for element area constraints that */
+ /* will never be assigned any value (other than the default -1.0). */
+ if (!refine && !poly) {
+ vararea = 0;
+ }
+ /* Be careful not to add an extra attribute to each element unless the */
+ /* input supports it (PSLG in, but not refining a preexisting mesh). */
+ if (refine || !poly) {
+ regionattrib = 0;
+ }
+
+#ifndef TRILIBRARY
+ strcpy(inpolyfilename, innodefilename);
+ strcpy(inelefilename, innodefilename);
+ strcpy(areafilename, innodefilename);
+ increment = 0;
+ strcpy(workstring, innodefilename);
+ j = 1;
+ while (workstring[j] != '\0') {
+ if ((workstring[j] == '.') && (workstring[j + 1] != '\0')) {
+ increment = j + 1;
+ }
+ j++;
+ }
+ meshnumber = 0;
+ if (increment > 0) {
+ j = increment;
+ do {
+ if ((workstring[j] >= '0') && (workstring[j] <= '9')) {
+ meshnumber = meshnumber * 10 + (int) (workstring[j] - '0');
+ } else {
+ increment = 0;
+ }
+ j++;
+ } while (workstring[j] != '\0');
+ }
+ if (noiterationnum) {
+ strcpy(outnodefilename, innodefilename);
+ strcpy(outelefilename, innodefilename);
+ strcpy(edgefilename, innodefilename);
+ strcpy(vnodefilename, innodefilename);
+ strcpy(vedgefilename, innodefilename);
+ strcpy(neighborfilename, innodefilename);
+ strcpy(offfilename, innodefilename);
+ strcat(outnodefilename, ".node");
+ strcat(outelefilename, ".ele");
+ strcat(edgefilename, ".edge");
+ strcat(vnodefilename, ".v.node");
+ strcat(vedgefilename, ".v.edge");
+ strcat(neighborfilename, ".neigh");
+ strcat(offfilename, ".off");
+ } else if (increment == 0) {
+ strcpy(outnodefilename, innodefilename);
+ strcpy(outpolyfilename, innodefilename);
+ strcpy(outelefilename, innodefilename);
+ strcpy(edgefilename, innodefilename);
+ strcpy(vnodefilename, innodefilename);
+ strcpy(vedgefilename, innodefilename);
+ strcpy(neighborfilename, innodefilename);
+ strcpy(offfilename, innodefilename);
+ strcat(outnodefilename, ".1.node");
+ strcat(outpolyfilename, ".1.poly");
+ strcat(outelefilename, ".1.ele");
+ strcat(edgefilename, ".1.edge");
+ strcat(vnodefilename, ".1.v.node");
+ strcat(vedgefilename, ".1.v.edge");
+ strcat(neighborfilename, ".1.neigh");
+ strcat(offfilename, ".1.off");
+ } else {
+ workstring[increment] = '%';
+ workstring[increment + 1] = 'd';
+ workstring[increment + 2] = '\0';
+ sprintf(outnodefilename, workstring, meshnumber + 1);
+ strcpy(outpolyfilename, outnodefilename);
+ strcpy(outelefilename, outnodefilename);
+ strcpy(edgefilename, outnodefilename);
+ strcpy(vnodefilename, outnodefilename);
+ strcpy(vedgefilename, outnodefilename);
+ strcpy(neighborfilename, outnodefilename);
+ strcpy(offfilename, outnodefilename);
+ strcat(outnodefilename, ".node");
+ strcat(outpolyfilename, ".poly");
+ strcat(outelefilename, ".ele");
+ strcat(edgefilename, ".edge");
+ strcat(vnodefilename, ".v.node");
+ strcat(vedgefilename, ".v.edge");
+ strcat(neighborfilename, ".neigh");
+ strcat(offfilename, ".off");
+ }
+ strcat(innodefilename, ".node");
+ strcat(inpolyfilename, ".poly");
+ strcat(inelefilename, ".ele");
+ strcat(areafilename, ".area");
+#endif /* not TRILIBRARY */
+}
+
+/** **/
+/** **/
+/********* User interaction routines begin here *********/
+
+/********* Debugging routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* printtriangle() Print out the details of a triangle/edge handle. */
+/* */
+/* I originally wrote this procedure to simplify debugging; it can be */
+/* called directly from the debugger, and presents information about a */
+/* triangle/edge handle in digestible form. It's also used when the */
+/* highest level of verbosity (`-VVV') is specified. */
+/* */
+/*****************************************************************************/
+
+void printtriangle(t)
+struct triedge *t;
+{
+ struct triedge printtri;
+ struct edge printsh;
+ point printpoint;
+
+ printf("triangle x%lx with orientation %d:\n", (unsigned long) t->tri,
+ t->orient);
+ decode(t->tri[0], printtri);
+ if (printtri.tri == dummytri) {
+ printf(" [0] = Outer space\n");
+ } else {
+ printf(" [0] = x%lx %d\n", (unsigned long) printtri.tri,
+ printtri.orient);
+ }
+ decode(t->tri[1], printtri);
+ if (printtri.tri == dummytri) {
+ printf(" [1] = Outer space\n");
+ } else {
+ printf(" [1] = x%lx %d\n", (unsigned long) printtri.tri,
+ printtri.orient);
+ }
+ decode(t->tri[2], printtri);
+ if (printtri.tri == dummytri) {
+ printf(" [2] = Outer space\n");
+ } else {
+ printf(" [2] = x%lx %d\n", (unsigned long) printtri.tri,
+ printtri.orient);
+ }
+ org(*t, printpoint);
+ if (printpoint == (point) NULL)
+ printf(" Origin[%d] = NULL\n", (t->orient + 1) % 3 + 3);
+ else
+ printf(" Origin[%d] = x%lx (%.12g, %.12g)\n",
+ (t->orient + 1) % 3 + 3, (unsigned long) printpoint,
+ printpoint[0], printpoint[1]);
+ dest(*t, printpoint);
+ if (printpoint == (point) NULL)
+ printf(" Dest [%d] = NULL\n", (t->orient + 2) % 3 + 3);
+ else
+ printf(" Dest [%d] = x%lx (%.12g, %.12g)\n",
+ (t->orient + 2) % 3 + 3, (unsigned long) printpoint,
+ printpoint[0], printpoint[1]);
+ apex(*t, printpoint);
+ if (printpoint == (point) NULL)
+ printf(" Apex [%d] = NULL\n", t->orient + 3);
+ else
+ printf(" Apex [%d] = x%lx (%.12g, %.12g)\n",
+ t->orient + 3, (unsigned long) printpoint,
+ printpoint[0], printpoint[1]);
+ if (useshelles) {
+ sdecode(t->tri[6], printsh);
+ if (printsh.sh != dummysh) {
+ printf(" [6] = x%lx %d\n", (unsigned long) printsh.sh,
+ printsh.shorient);
+ }
+ sdecode(t->tri[7], printsh);
+ if (printsh.sh != dummysh) {
+ printf(" [7] = x%lx %d\n", (unsigned long) printsh.sh,
+ printsh.shorient);
+ }
+ sdecode(t->tri[8], printsh);
+ if (printsh.sh != dummysh) {
+ printf(" [8] = x%lx %d\n", (unsigned long) printsh.sh,
+ printsh.shorient);
+ }
+ }
+ if (vararea) {
+ printf(" Area constraint: %.4g\n", areabound(*t));
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* printshelle() Print out the details of a shell edge handle. */
+/* */
+/* I originally wrote this procedure to simplify debugging; it can be */
+/* called directly from the debugger, and presents information about a */
+/* shell edge handle in digestible form. It's also used when the highest */
+/* level of verbosity (`-VVV') is specified. */
+/* */
+/*****************************************************************************/
+
+void printshelle(s)
+struct edge *s;
+{
+ struct edge printsh;
+ struct triedge printtri;
+ point printpoint;
+
+ printf("shell edge x%lx with orientation %d and mark %d:\n",
+ (unsigned long) s->sh, s->shorient, mark(*s));
+ sdecode(s->sh[0], printsh);
+ if (printsh.sh == dummysh) {
+ printf(" [0] = No shell\n");
+ } else {
+ printf(" [0] = x%lx %d\n", (unsigned long) printsh.sh,
+ printsh.shorient);
+ }
+ sdecode(s->sh[1], printsh);
+ if (printsh.sh == dummysh) {
+ printf(" [1] = No shell\n");
+ } else {
+ printf(" [1] = x%lx %d\n", (unsigned long) printsh.sh,
+ printsh.shorient);
+ }
+ sorg(*s, printpoint);
+ if (printpoint == (point) NULL)
+ printf(" Origin[%d] = NULL\n", 2 + s->shorient);
+ else
+ printf(" Origin[%d] = x%lx (%.12g, %.12g)\n",
+ 2 + s->shorient, (unsigned long) printpoint,
+ printpoint[0], printpoint[1]);
+ sdest(*s, printpoint);
+ if (printpoint == (point) NULL)
+ printf(" Dest [%d] = NULL\n", 3 - s->shorient);
+ else
+ printf(" Dest [%d] = x%lx (%.12g, %.12g)\n",
+ 3 - s->shorient, (unsigned long) printpoint,
+ printpoint[0], printpoint[1]);
+ decode(s->sh[4], printtri);
+ if (printtri.tri == dummytri) {
+ printf(" [4] = Outer space\n");
+ } else {
+ printf(" [4] = x%lx %d\n", (unsigned long) printtri.tri,
+ printtri.orient);
+ }
+ decode(s->sh[5], printtri);
+ if (printtri.tri == dummytri) {
+ printf(" [5] = Outer space\n");
+ } else {
+ printf(" [5] = x%lx %d\n", (unsigned long) printtri.tri,
+ printtri.orient);
+ }
+}
+
+/** **/
+/** **/
+/********* Debugging routines end here *********/
+
+/********* Memory management routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* poolinit() Initialize a pool of memory for allocation of items. */
+/* */
+/* This routine initializes the machinery for allocating items. A `pool' */
+/* is created whose records have size at least `bytecount'. Items will be */
+/* allocated in `itemcount'-item blocks. Each item is assumed to be a */
+/* collection of words, and either pointers or floating-point values are */
+/* assumed to be the "primary" word type. (The "primary" word type is used */
+/* to determine alignment of items.) If `alignment' isn't zero, all items */
+/* will be `alignment'-byte aligned in memory. `alignment' must be either */
+/* a multiple or a factor of the primary word size; powers of two are safe. */
+/* `alignment' is normally used to create a few unused bits at the bottom */
+/* of each item's pointer, in which information may be stored. */
+/* */
+/* Don't change this routine unless you understand it. */
+/* */
+/*****************************************************************************/
+
+void poolinit(pool, bytecount, itemcount, wtype, alignment)
+struct memorypool *pool;
+int bytecount;
+int itemcount;
+enum wordtype wtype;
+int alignment;
+{
+ int wordsize;
+
+ /* Initialize values in the pool. */
+ pool->itemwordtype = wtype;
+ wordsize = (pool->itemwordtype == POINTER) ? sizeof(VOID *) : sizeof(REAL);
+ /* Find the proper alignment, which must be at least as large as: */
+ /* - The parameter `alignment'. */
+ /* - The primary word type, to avoid unaligned accesses. */
+ /* - sizeof(VOID *), so the stack of dead items can be maintained */
+ /* without unaligned accesses. */
+ if (alignment > wordsize) {
+ pool->alignbytes = alignment;
+ } else {
+ pool->alignbytes = wordsize;
+ }
+ if (sizeof(VOID *) > pool->alignbytes) {
+ pool->alignbytes = sizeof(VOID *);
+ }
+ pool->itemwords = ((bytecount + pool->alignbytes - 1) / pool->alignbytes)
+ * (pool->alignbytes / wordsize);
+ pool->itembytes = pool->itemwords * wordsize;
+ pool->itemsperblock = itemcount;
+
+ /* Allocate a block of items. Space for `itemsperblock' items and one */
+ /* pointer (to point to the next block) are allocated, as well as space */
+ /* to ensure alignment of the items. */
+ pool->firstblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
+ + sizeof(VOID *) + pool->alignbytes);
+ if (pool->firstblock == (VOID **) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ /* Set the next block pointer to NULL. */
+ *(pool->firstblock) = (VOID *) NULL;
+ poolrestart(pool);
+}
+
+/*****************************************************************************/
+/* */
+/* poolrestart() Deallocate all items in a pool. */
+/* */
+/* The pool is returned to its starting state, except that no memory is */
+/* freed to the operating system. Rather, the previously allocated blocks */
+/* are ready to be reused. */
+/* */
+/*****************************************************************************/
+
+void poolrestart(pool)
+struct memorypool *pool;
+{
+ unsigned long alignptr;
+
+ pool->items = 0;
+ pool->maxitems = 0;
+
+ /* Set the currently active block. */
+ pool->nowblock = pool->firstblock;
+ /* Find the first item in the pool. Increment by the size of (VOID *). */
+ alignptr = (unsigned long) (pool->nowblock + 1);
+ /* Align the item on an `alignbytes'-byte boundary. */
+ pool->nextitem = (VOID *)
+ (alignptr + (unsigned long) pool->alignbytes
+ - (alignptr % (unsigned long) pool->alignbytes));
+ /* There are lots of unallocated items left in this block. */
+ pool->unallocateditems = pool->itemsperblock;
+ /* The stack of deallocated items is empty. */
+ pool->deaditemstack = (VOID *) NULL;
+}
+
+/*****************************************************************************/
+/* */
+/* pooldeinit() Free to the operating system all memory taken by a pool. */
+/* */
+/*****************************************************************************/
+
+void pooldeinit(pool)
+struct memorypool *pool;
+{
+ while (pool->firstblock != (VOID **) NULL) {
+ pool->nowblock = (VOID **) *(pool->firstblock);
+ free(pool->firstblock);
+ pool->firstblock = pool->nowblock;
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* poolalloc() Allocate space for an item. */
+/* */
+/*****************************************************************************/
+
+VOID *poolalloc(pool)
+struct memorypool *pool;
+{
+ VOID *newitem;
+ VOID **newblock;
+ unsigned long alignptr;
+
+ /* First check the linked list of dead items. If the list is not */
+ /* empty, allocate an item from the list rather than a fresh one. */
+ if (pool->deaditemstack != (VOID *) NULL) {
+ newitem = pool->deaditemstack; /* Take first item in list. */
+ pool->deaditemstack = * (VOID **) pool->deaditemstack;
+ } else {
+ /* Check if there are any free items left in the current block. */
+ if (pool->unallocateditems == 0) {
+ /* Check if another block must be allocated. */
+ if (*(pool->nowblock) == (VOID *) NULL) {
+ /* Allocate a new block of items, pointed to by the previous block. */
+ newblock = (VOID **) malloc(pool->itemsperblock * pool->itembytes
+ + sizeof(VOID *) + pool->alignbytes);
+ if (newblock == (VOID **) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ *(pool->nowblock) = (VOID *) newblock;
+ /* The next block pointer is NULL. */
+ *newblock = (VOID *) NULL;
+ }
+ /* Move to the new block. */
+ pool->nowblock = (VOID **) *(pool->nowblock);
+ /* Find the first item in the block. */
+ /* Increment by the size of (VOID *). */
+ alignptr = (unsigned long) (pool->nowblock + 1);
+ /* Align the item on an `alignbytes'-byte boundary. */
+ pool->nextitem = (VOID *)
+ (alignptr + (unsigned long) pool->alignbytes
+ - (alignptr % (unsigned long) pool->alignbytes));
+ /* There are lots of unallocated items left in this block. */
+ pool->unallocateditems = pool->itemsperblock;
+ }
+ /* Allocate a new item. */
+ newitem = pool->nextitem;
+ /* Advance `nextitem' pointer to next free item in block. */
+ if (pool->itemwordtype == POINTER) {
+ pool->nextitem = (VOID *) ((VOID **) pool->nextitem + pool->itemwords);
+ } else {
+ pool->nextitem = (VOID *) ((REAL *) pool->nextitem + pool->itemwords);
+ }
+ pool->unallocateditems--;
+ pool->maxitems++;
+ }
+ pool->items++;
+ return newitem;
+}
+
+/*****************************************************************************/
+/* */
+/* pooldealloc() Deallocate space for an item. */
+/* */
+/* The deallocated space is stored in a queue for later reuse. */
+/* */
+/*****************************************************************************/
+
+void pooldealloc(pool, dyingitem)
+struct memorypool *pool;
+VOID *dyingitem;
+{
+ /* Push freshly killed item onto stack. */
+ *((VOID **) dyingitem) = pool->deaditemstack;
+ pool->deaditemstack = dyingitem;
+ pool->items--;
+}
+
+/*****************************************************************************/
+/* */
+/* traversalinit() Prepare to traverse the entire list of items. */
+/* */
+/* This routine is used in conjunction with traverse(). */
+/* */
+/*****************************************************************************/
+
+void traversalinit(pool)
+struct memorypool *pool;
+{
+ unsigned long alignptr;
+
+ /* Begin the traversal in the first block. */
+ pool->pathblock = pool->firstblock;
+ /* Find the first item in the block. Increment by the size of (VOID *). */
+ alignptr = (unsigned long) (pool->pathblock + 1);
+ /* Align with item on an `alignbytes'-byte boundary. */
+ pool->pathitem = (VOID *)
+ (alignptr + (unsigned long) pool->alignbytes
+ - (alignptr % (unsigned long) pool->alignbytes));
+ /* Set the number of items left in the current block. */
+ pool->pathitemsleft = pool->itemsperblock;
+}
+
+/*****************************************************************************/
+/* */
+/* traverse() Find the next item in the list. */
+/* */
+/* This routine is used in conjunction with traversalinit(). Be forewarned */
+/* that this routine successively returns all items in the list, including */
+/* deallocated ones on the deaditemqueue. It's up to you to figure out */
+/* which ones are actually dead. Why? I don't want to allocate extra */
+/* space just to demarcate dead items. It can usually be done more */
+/* space-efficiently by a routine that knows something about the structure */
+/* of the item. */
+/* */
+/*****************************************************************************/
+
+VOID *traverse(pool)
+struct memorypool *pool;
+{
+ VOID *newitem;
+ unsigned long alignptr;
+
+ /* Stop upon exhausting the list of items. */
+ if (pool->pathitem == pool->nextitem) {
+ return (VOID *) NULL;
+ }
+ /* Check whether any untraversed items remain in the current block. */
+ if (pool->pathitemsleft == 0) {
+ /* Find the next block. */
+ pool->pathblock = (VOID **) *(pool->pathblock);
+ /* Find the first item in the block. Increment by the size of (VOID *). */
+ alignptr = (unsigned long) (pool->pathblock + 1);
+ /* Align with item on an `alignbytes'-byte boundary. */
+ pool->pathitem = (VOID *)
+ (alignptr + (unsigned long) pool->alignbytes
+ - (alignptr % (unsigned long) pool->alignbytes));
+ /* Set the number of items left in the current block. */
+ pool->pathitemsleft = pool->itemsperblock;
+ }
+ newitem = pool->pathitem;
+ /* Find the next item in the block. */
+ if (pool->itemwordtype == POINTER) {
+ pool->pathitem = (VOID *) ((VOID **) pool->pathitem + pool->itemwords);
+ } else {
+ pool->pathitem = (VOID *) ((REAL *) pool->pathitem + pool->itemwords);
+ }
+ pool->pathitemsleft--;
+ return newitem;
+}
+
+/*****************************************************************************/
+/* */
+/* dummyinit() Initialize the triangle that fills "outer space" and the */
+/* omnipresent shell edge. */
+/* */
+/* The triangle that fills "outer space", called `dummytri', is pointed to */
+/* by every triangle and shell edge on a boundary (be it outer or inner) of */
+/* the triangulation. Also, `dummytri' points to one of the triangles on */
+/* the convex hull (until the holes and concavities are carved), making it */
+/* possible to find a starting triangle for point location. */
+/* */
+/* The omnipresent shell edge, `dummysh', is pointed to by every triangle */
+/* or shell edge that doesn't have a full complement of real shell edges */
+/* to point to. */
+/* */
+/*****************************************************************************/
+
+void dummyinit(trianglewords, shellewords)
+int trianglewords;
+int shellewords;
+{
+ unsigned long alignptr;
+
+ /* `triwords' and `shwords' are used by the mesh manipulation primitives */
+ /* to extract orientations of triangles and shell edges from pointers. */
+ triwords = trianglewords; /* Initialize `triwords' once and for all. */
+ shwords = shellewords; /* Initialize `shwords' once and for all. */
+
+ /* Set up `dummytri', the `triangle' that occupies "outer space". */
+ dummytribase = (triangle *) malloc(triwords * sizeof(triangle)
+ + triangles.alignbytes);
+ if (dummytribase == (triangle *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ /* Align `dummytri' on a `triangles.alignbytes'-byte boundary. */
+ alignptr = (unsigned long) dummytribase;
+ dummytri = (triangle *)
+ (alignptr + (unsigned long) triangles.alignbytes
+ - (alignptr % (unsigned long) triangles.alignbytes));
+ /* Initialize the three adjoining triangles to be "outer space". These */
+ /* will eventually be changed by various bonding operations, but their */
+ /* values don't really matter, as long as they can legally be */
+ /* dereferenced. */
+ dummytri[0] = (triangle) dummytri;
+ dummytri[1] = (triangle) dummytri;
+ dummytri[2] = (triangle) dummytri;
+ /* Three NULL vertex points. */
+ dummytri[3] = (triangle) NULL;
+ dummytri[4] = (triangle) NULL;
+ dummytri[5] = (triangle) NULL;
+
+ if (useshelles) {
+ /* Set up `dummysh', the omnipresent "shell edge" pointed to by any */
+ /* triangle side or shell edge end that isn't attached to a real shell */
+ /* edge. */
+ dummyshbase = (shelle *) malloc(shwords * sizeof(shelle)
+ + shelles.alignbytes);
+ if (dummyshbase == (shelle *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ /* Align `dummysh' on a `shelles.alignbytes'-byte boundary. */
+ alignptr = (unsigned long) dummyshbase;
+ dummysh = (shelle *)
+ (alignptr + (unsigned long) shelles.alignbytes
+ - (alignptr % (unsigned long) shelles.alignbytes));
+ /* Initialize the two adjoining shell edges to be the omnipresent shell */
+ /* edge. These will eventually be changed by various bonding */
+ /* operations, but their values don't really matter, as long as they */
+ /* can legally be dereferenced. */
+ dummysh[0] = (shelle) dummysh;
+ dummysh[1] = (shelle) dummysh;
+ /* Two NULL vertex points. */
+ dummysh[2] = (shelle) NULL;
+ dummysh[3] = (shelle) NULL;
+ /* Initialize the two adjoining triangles to be "outer space". */
+ dummysh[4] = (shelle) dummytri;
+ dummysh[5] = (shelle) dummytri;
+ /* Set the boundary marker to zero. */
+ * (int *) (dummysh + 6) = 0;
+
+ /* Initialize the three adjoining shell edges of `dummytri' to be */
+ /* the omnipresent shell edge. */
+ dummytri[6] = (triangle) dummysh;
+ dummytri[7] = (triangle) dummysh;
+ dummytri[8] = (triangle) dummysh;
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* initializepointpool() Calculate the size of the point data structure */
+/* and initialize its memory pool. */
+/* */
+/* This routine also computes the `pointmarkindex' and `point2triindex' */
+/* indices used to find values within each point. */
+/* */
+/*****************************************************************************/
+
+void initializepointpool()
+{
+ int pointsize;
+
+ /* The index within each point at which the boundary marker is found. */
+ /* Ensure the point marker is aligned to a sizeof(int)-byte address. */
+ pointmarkindex = ((mesh_dim + nextras) * sizeof(REAL) + sizeof(int) - 1)
+ / sizeof(int);
+ pointsize = (pointmarkindex + 1) * sizeof(int);
+ if (poly) {
+ /* The index within each point at which a triangle pointer is found. */
+ /* Ensure the pointer is aligned to a sizeof(triangle)-byte address. */
+ point2triindex = (pointsize + sizeof(triangle) - 1) / sizeof(triangle);
+ pointsize = (point2triindex + 1) * sizeof(triangle);
+ }
+ /* Initialize the pool of points. */
+ poolinit(&points, pointsize, POINTPERBLOCK,
+ (sizeof(REAL) >= sizeof(triangle)) ? FLOATINGPOINT : POINTER, 0);
+}
+
+/*****************************************************************************/
+/* */
+/* initializetrisegpools() Calculate the sizes of the triangle and shell */
+/* edge data structures and initialize their */
+/* memory pools. */
+/* */
+/* This routine also computes the `highorderindex', `elemattribindex', and */
+/* `areaboundindex' indices used to find values within each triangle. */
+/* */
+/*****************************************************************************/
+
+void initializetrisegpools()
+{
+ int trisize;
+
+ /* The index within each triangle at which the extra nodes (above three) */
+ /* associated with high order elements are found. There are three */
+ /* pointers to other triangles, three pointers to corners, and possibly */
+ /* three pointers to shell edges before the extra nodes. */
+ highorderindex = 6 + (useshelles * 3);
+ /* The number of bytes occupied by a triangle. */
+ trisize = ((order + 1) * (order + 2) / 2 + (highorderindex - 3)) *
+ sizeof(triangle);
+ /* The index within each triangle at which its attributes are found, */
+ /* where the index is measured in REALs. */
+ elemattribindex = (trisize + sizeof(REAL) - 1) / sizeof(REAL);
+ /* The index within each triangle at which the maximum area constraint */
+ /* is found, where the index is measured in REALs. Note that if the */
+ /* `regionattrib' flag is set, an additional attribute will be added. */
+ areaboundindex = elemattribindex + eextras + regionattrib;
+ /* If triangle attributes or an area bound are needed, increase the number */
+ /* of bytes occupied by a triangle. */
+ if (vararea) {
+ trisize = (areaboundindex + 1) * sizeof(REAL);
+ } else if (eextras + regionattrib > 0) {
+ trisize = areaboundindex * sizeof(REAL);
+ }
+ /* If a Voronoi diagram or triangle neighbor graph is requested, make */
+ /* sure there's room to store an integer index in each triangle. This */
+ /* integer index can occupy the same space as the shell edges or */
+ /* attributes or area constraint or extra nodes. */
+ if ((voronoi || neighbors) &&
+ (trisize < 6 * sizeof(triangle) + sizeof(int))) {
+ trisize = 6 * sizeof(triangle) + sizeof(int);
+ }
+ /* Having determined the memory size of a triangle, initialize the pool. */
+ poolinit(&triangles, trisize, TRIPERBLOCK, POINTER, 4);
+
+ if (useshelles) {
+ /* Initialize the pool of shell edges. */
+ poolinit(&shelles, 6 * sizeof(triangle) + sizeof(int), SHELLEPERBLOCK,
+ POINTER, 4);
+
+ /* Initialize the "outer space" triangle and omnipresent shell edge. */
+ dummyinit(triangles.itemwords, shelles.itemwords);
+ } else {
+ /* Initialize the "outer space" triangle. */
+ dummyinit(triangles.itemwords, 0);
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* triangledealloc() Deallocate space for a triangle, marking it dead. */
+/* */
+/*****************************************************************************/
+
+void triangledealloc(dyingtriangle)
+triangle *dyingtriangle;
+{
+ /* Set triangle's vertices to NULL. This makes it possible to */
+ /* detect dead triangles when traversing the list of all triangles. */
+ dyingtriangle[3] = (triangle) NULL;
+ dyingtriangle[4] = (triangle) NULL;
+ dyingtriangle[5] = (triangle) NULL;
+ pooldealloc(&triangles, (VOID *) dyingtriangle);
+}
+
+/*****************************************************************************/
+/* */
+/* triangletraverse() Traverse the triangles, skipping dead ones. */
+/* */
+/*****************************************************************************/
+
+triangle *triangletraverse()
+{
+ triangle *newtriangle;
+
+ do {
+ newtriangle = (triangle *) traverse(&triangles);
+ if (newtriangle == (triangle *) NULL) {
+ return (triangle *) NULL;
+ }
+ } while (newtriangle[3] == (triangle) NULL); /* Skip dead ones. */
+ return newtriangle;
+}
+
+/*****************************************************************************/
+/* */
+/* shelledealloc() Deallocate space for a shell edge, marking it dead. */
+/* */
+/*****************************************************************************/
+
+void shelledealloc(dyingshelle)
+shelle *dyingshelle;
+{
+ /* Set shell edge's vertices to NULL. This makes it possible to */
+ /* detect dead shells when traversing the list of all shells. */
+ dyingshelle[2] = (shelle) NULL;
+ dyingshelle[3] = (shelle) NULL;
+ pooldealloc(&shelles, (VOID *) dyingshelle);
+}
+
+/*****************************************************************************/
+/* */
+/* shelletraverse() Traverse the shell edges, skipping dead ones. */
+/* */
+/*****************************************************************************/
+
+shelle *shelletraverse()
+{
+ shelle *newshelle;
+
+ do {
+ newshelle = (shelle *) traverse(&shelles);
+ if (newshelle == (shelle *) NULL) {
+ return (shelle *) NULL;
+ }
+ } while (newshelle[2] == (shelle) NULL); /* Skip dead ones. */
+ return newshelle;
+}
+
+/*****************************************************************************/
+/* */
+/* pointdealloc() Deallocate space for a point, marking it dead. */
+/* */
+/*****************************************************************************/
+
+void pointdealloc(dyingpoint)
+point dyingpoint;
+{
+ /* Mark the point as dead. This makes it possible to detect dead points */
+ /* when traversing the list of all points. */
+ setpointmark(dyingpoint, DEADPOINT);
+ pooldealloc(&points, (VOID *) dyingpoint);
+}
+
+/*****************************************************************************/
+/* */
+/* pointtraverse() Traverse the points, skipping dead ones. */
+/* */
+/*****************************************************************************/
+
+point pointtraverse()
+{
+ point newpoint;
+
+ do {
+ newpoint = (point) traverse(&points);
+ if (newpoint == (point) NULL) {
+ return (point) NULL;
+ }
+ } while (pointmark(newpoint) == DEADPOINT); /* Skip dead ones. */
+ return newpoint;
+}
+
+/*****************************************************************************/
+/* */
+/* badsegmentdealloc() Deallocate space for a bad segment, marking it */
+/* dead. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void badsegmentdealloc(dyingseg)
+struct edge *dyingseg;
+{
+ /* Set segment's orientation to -1. This makes it possible to */
+ /* detect dead segments when traversing the list of all segments. */
+ dyingseg->shorient = -1;
+ pooldealloc(&badsegments, (VOID *) dyingseg);
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* badsegmenttraverse() Traverse the bad segments, skipping dead ones. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+struct edge *badsegmenttraverse()
+{
+ struct edge *newseg;
+
+ do {
+ newseg = (struct edge *) traverse(&badsegments);
+ if (newseg == (struct edge *) NULL) {
+ return (struct edge *) NULL;
+ }
+ } while (newseg->shorient == -1); /* Skip dead ones. */
+ return newseg;
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* getpoint() Get a specific point, by number, from the list. */
+/* */
+/* The first point is number 'firstnumber'. */
+/* */
+/* Note that this takes O(n) time (with a small constant, if POINTPERBLOCK */
+/* is large). I don't care to take the trouble to make it work in constant */
+/* time. */
+/* */
+/*****************************************************************************/
+
+point getpoint(number)
+int number;
+{
+ VOID **getblock;
+ point foundpoint;
+ unsigned long alignptr;
+ int current;
+
+ getblock = points.firstblock;
+ current = firstnumber;
+ /* Find the right block. */
+ while (current + points.itemsperblock <= number) {
+ getblock = (VOID **) *getblock;
+ current += points.itemsperblock;
+ }
+ /* Now find the right point. */
+ alignptr = (unsigned long) (getblock + 1);
+ foundpoint = (point) (alignptr + (unsigned long) points.alignbytes
+ - (alignptr % (unsigned long) points.alignbytes));
+ while (current < number) {
+ foundpoint += points.itemwords;
+ current++;
+ }
+ return foundpoint;
+}
+
+/*****************************************************************************/
+/* */
+/* triangledeinit() Free all remaining allocated memory. */
+/* */
+/*****************************************************************************/
+
+void triangledeinit()
+{
+ pooldeinit(&triangles);
+ free(dummytribase);
+ if (useshelles) {
+ pooldeinit(&shelles);
+ free(dummyshbase);
+ }
+ pooldeinit(&points);
+#ifndef CDT_ONLY
+ if (quality) {
+ pooldeinit(&badsegments);
+ if ((minangle > 0.0) || vararea || fixedarea) {
+ pooldeinit(&badtriangles);
+ }
+ }
+#endif /* not CDT_ONLY */
+}
+
+/** **/
+/** **/
+/********* Memory management routines end here *********/
+
+/********* Constructors begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* maketriangle() Create a new triangle with orientation zero. */
+/* */
+/*****************************************************************************/
+
+void maketriangle(newtriedge)
+struct triedge *newtriedge;
+{
+ int i;
+
+ newtriedge->tri = (triangle *) poolalloc(&triangles);
+ /* Initialize the three adjoining triangles to be "outer space". */
+ newtriedge->tri[0] = (triangle) dummytri;
+ newtriedge->tri[1] = (triangle) dummytri;
+ newtriedge->tri[2] = (triangle) dummytri;
+ /* Three NULL vertex points. */
+ newtriedge->tri[3] = (triangle) NULL;
+ newtriedge->tri[4] = (triangle) NULL;
+ newtriedge->tri[5] = (triangle) NULL;
+ /* Initialize the three adjoining shell edges to be the omnipresent */
+ /* shell edge. */
+ if (useshelles) {
+ newtriedge->tri[6] = (triangle) dummysh;
+ newtriedge->tri[7] = (triangle) dummysh;
+ newtriedge->tri[8] = (triangle) dummysh;
+ }
+ for (i = 0; i < eextras; i++) {
+ setelemattribute(*newtriedge, i, 0.0);
+ }
+ if (vararea) {
+ setareabound(*newtriedge, -1.0);
+ }
+
+ newtriedge->orient = 0;
+}
+
+/*****************************************************************************/
+/* */
+/* makeshelle() Create a new shell edge with orientation zero. */
+/* */
+/*****************************************************************************/
+
+void makeshelle(newedge)
+struct edge *newedge;
+{
+ newedge->sh = (shelle *) poolalloc(&shelles);
+ /* Initialize the two adjoining shell edges to be the omnipresent */
+ /* shell edge. */
+ newedge->sh[0] = (shelle) dummysh;
+ newedge->sh[1] = (shelle) dummysh;
+ /* Two NULL vertex points. */
+ newedge->sh[2] = (shelle) NULL;
+ newedge->sh[3] = (shelle) NULL;
+ /* Initialize the two adjoining triangles to be "outer space". */
+ newedge->sh[4] = (shelle) dummytri;
+ newedge->sh[5] = (shelle) dummytri;
+ /* Set the boundary marker to zero. */
+ setmark(*newedge, 0);
+
+ newedge->shorient = 0;
+}
+
+/** **/
+/** **/
+/********* Constructors end here *********/
+
+/********* Determinant evaluation routines begin here *********/
+/** **/
+/** **/
+
+/* The adaptive exact arithmetic geometric predicates implemented herein are */
+/* described in detail in my Technical Report CMU-CS-96-140. The complete */
+/* reference is given in the header. */
+
+/* Which of the following two methods of finding the absolute values is */
+/* fastest is compiler-dependent. A few compilers can inline and optimize */
+/* the fabs() call; but most will incur the overhead of a function call, */
+/* which is disastrously slow. A faster way on IEEE machines might be to */
+/* mask the appropriate bit, but that's difficult to do in C. */
+
+#define Absolute(a) ((a) >= 0.0 ? (a) : -(a))
+/* #define Absolute(a) fabs(a) */
+
+/* Many of the operations are broken up into two pieces, a main part that */
+/* performs an approximate operation, and a "tail" that computes the */
+/* roundoff error of that operation. */
+/* */
+/* The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(), */
+/* Split(), and Two_Product() are all implemented as described in the */
+/* reference. Each of these macros requires certain variables to be */
+/* defined in the calling routine. The variables `bvirt', `c', `abig', */
+/* `_i', `_j', `_k', `_l', `_m', and `_n' are declared `INEXACT' because */
+/* they store the result of an operation that may incur roundoff error. */
+/* The input parameter `x' (or the highest numbered `x_' parameter) must */
+/* also be declared `INEXACT'. */
+
+#define Fast_Two_Sum_Tail(a, b, x, y) \
+ bvirt = x - a; \
+ y = b - bvirt
+
+#define Fast_Two_Sum(a, b, x, y) \
+ x = (REAL) (a + b); \
+ Fast_Two_Sum_Tail(a, b, x, y)
+
+#define Two_Sum_Tail(a, b, x, y) \
+ bvirt = (REAL) (x - a); \
+ avirt = x - bvirt; \
+ bround = b - bvirt; \
+ around = a - avirt; \
+ y = around + bround
+
+#define Two_Sum(a, b, x, y) \
+ x = (REAL) (a + b); \
+ Two_Sum_Tail(a, b, x, y)
+
+#define Two_Diff_Tail(a, b, x, y) \
+ bvirt = (REAL) (a - x); \
+ avirt = x + bvirt; \
+ bround = bvirt - b; \
+ around = a - avirt; \
+ y = around + bround
+
+#define Two_Diff(a, b, x, y) \
+ x = (REAL) (a - b); \
+ Two_Diff_Tail(a, b, x, y)
+
+#define Split(a, ahi, alo) \
+ c = (REAL) (splitter * a); \
+ abig = (REAL) (c - a); \
+ ahi = (REAL)(c - abig); \
+ alo = (REAL)(a - ahi)
+
+#define Two_Product_Tail(a, b, x, y) \
+ Split(a, ahi, alo); \
+ Split(b, bhi, blo); \
+ err1 = x - (ahi * bhi); \
+ err2 = err1 - (alo * bhi); \
+ err3 = err2 - (ahi * blo); \
+ y = (alo * blo) - err3
+
+#define Two_Product(a, b, x, y) \
+ x = (REAL) (a * b); \
+ Two_Product_Tail(a, b, x, y)
+
+/* Two_Product_Presplit() is Two_Product() where one of the inputs has */
+/* already been split. Avoids redundant splitting. */
+
+#define Two_Product_Presplit(a, b, bhi, blo, x, y) \
+ x = (REAL) (a * b); \
+ Split(a, ahi, alo); \
+ err1 = x - (ahi * bhi); \
+ err2 = err1 - (alo * bhi); \
+ err3 = err2 - (ahi * blo); \
+ y = (alo * blo) - err3
+
+/* Square() can be done more quickly than Two_Product(). */
+
+#define Square_Tail(a, x, y) \
+ Split(a, ahi, alo); \
+ err1 = x - (ahi * ahi); \
+ err3 = err1 - ((ahi + ahi) * alo); \
+ y = (alo * alo) - err3
+
+#define Square(a, x, y) \
+ x = (REAL) (a * a); \
+ Square_Tail(a, x, y)
+
+/* Macros for summing expansions of various fixed lengths. These are all */
+/* unrolled versions of Expansion_Sum(). */
+
+#define Two_One_Sum(a1, a0, b, x2, x1, x0) \
+ Two_Sum(a0, b , _i, x0); \
+ Two_Sum(a1, _i, x2, x1)
+
+#define Two_One_Diff(a1, a0, b, x2, x1, x0) \
+ Two_Diff(a0, b , _i, x0); \
+ Two_Sum( a1, _i, x2, x1)
+
+#define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0) \
+ Two_One_Sum(a1, a0, b0, _j, _0, x0); \
+ Two_One_Sum(_j, _0, b1, x3, x2, x1)
+
+#define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0) \
+ Two_One_Diff(a1, a0, b0, _j, _0, x0); \
+ Two_One_Diff(_j, _0, b1, x3, x2, x1)
+
+/*****************************************************************************/
+/* */
+/* exactinit() Initialize the variables used for exact arithmetic. */
+/* */
+/* `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in */
+/* floating-point arithmetic. `epsilon' bounds the relative roundoff */
+/* error. It is used for floating-point error analysis. */
+/* */
+/* `splitter' is used to split floating-point numbers into two half- */
+/* length significands for exact multiplication. */
+/* */
+/* I imagine that a highly optimizing compiler might be too smart for its */
+/* own good, and somehow cause this routine to fail, if it pretends that */
+/* floating-point arithmetic is too much like real arithmetic. */
+/* */
+/* Don't change this routine unless you fully understand it. */
+/* */
+/*****************************************************************************/
+
+void exactinit()
+{
+ REAL half;
+ REAL check, lastcheck;
+ int every_other;
+
+ every_other = 1;
+ half = 0.5;
+ epsilon = 1.0;
+ splitter = 1.0;
+ check = 1.0;
+ /* Repeatedly divide `epsilon' by two until it is too small to add to */
+ /* one without causing roundoff. (Also check if the sum is equal to */
+ /* the previous sum, for machines that round up instead of using exact */
+ /* rounding. Not that these routines will work on such machines anyway. */
+ do {
+ lastcheck = check;
+ epsilon *= half;
+ if (every_other) {
+ splitter *= 2.0;
+ }
+ every_other = !every_other;
+ check = (REAL)(1.0 + epsilon);
+ } while ((check != 1.0) && (check != lastcheck));
+ splitter += 1.0;
+ if (verbose > 1) {
+ printf("Floating point roundoff is of magnitude %.17g\n", epsilon);
+ printf("Floating point splitter is %.17g\n", splitter);
+ }
+ /* Error bounds for orientation and incircle tests. */
+ resulterrbound = (REAL)((3.0 + 8.0 * epsilon) * epsilon);
+ ccwerrboundA = (REAL)((3.0 + 16.0 * epsilon) * epsilon);
+ ccwerrboundB = (REAL)((2.0 + 12.0 * epsilon) * epsilon);
+ ccwerrboundC = (REAL)((9.0 + 64.0 * epsilon) * epsilon * epsilon);
+ iccerrboundA = (REAL)((10.0 + 96.0 * epsilon) * epsilon);
+ iccerrboundB = (REAL)((4.0 + 48.0 * epsilon) * epsilon);
+ iccerrboundC = (REAL)((44.0 + 576.0 * epsilon) * epsilon * epsilon);
+}
+
+/*****************************************************************************/
+/* */
+/* fast_expansion_sum_zeroelim() Sum two expansions, eliminating zero */
+/* components from the output expansion. */
+/* */
+/* Sets h = e + f. See my Robust Predicates paper for details. */
+/* */
+/* If round-to-even is used (as with IEEE 754), maintains the strongly */
+/* nonoverlapping property. (That is, if e is strongly nonoverlapping, h */
+/* will be also.) Does NOT maintain the nonoverlapping or nonadjacent */
+/* properties. */
+/* */
+/*****************************************************************************/
+
+int fast_expansion_sum_zeroelim(elen, e, flen, f, h) /* h cannot be e or f. */
+int elen;
+REAL *e;
+int flen;
+REAL *f;
+REAL *h;
+{
+ REAL Q;
+ INEXACT REAL Qnew;
+ INEXACT REAL hh;
+ INEXACT REAL bvirt;
+ REAL avirt, bround, around;
+ int eindex, findex, hindex;
+ REAL enow, fnow;
+
+ enow = e[0];
+ fnow = f[0];
+ eindex = findex = 0;
+ if ((fnow > enow) == (fnow > -enow)) {
+ Q = enow;
+ enow = e[++eindex];
+ } else {
+ Q = fnow;
+ fnow = f[++findex];
+ }
+ hindex = 0;
+ if ((eindex < elen) && (findex < flen)) {
+ if ((fnow > enow) == (fnow > -enow)) {
+ Fast_Two_Sum(enow, Q, Qnew, hh);
+ enow = e[++eindex];
+ } else {
+ Fast_Two_Sum(fnow, Q, Qnew, hh);
+ fnow = f[++findex];
+ }
+ Q = Qnew;
+ if (hh != 0.0) {
+ h[hindex++] = hh;
+ }
+ while ((eindex < elen) && (findex < flen)) {
+ if ((fnow > enow) == (fnow > -enow)) {
+ Two_Sum(Q, enow, Qnew, hh);
+ enow = e[++eindex];
+ } else {
+ Two_Sum(Q, fnow, Qnew, hh);
+ fnow = f[++findex];
+ }
+ Q = Qnew;
+ if (hh != 0.0) {
+ h[hindex++] = hh;
+ }
+ }
+ }
+ while (eindex < elen) {
+ Two_Sum(Q, enow, Qnew, hh);
+ enow = e[++eindex];
+ Q = Qnew;
+ if (hh != 0.0) {
+ h[hindex++] = hh;
+ }
+ }
+ while (findex < flen) {
+ Two_Sum(Q, fnow, Qnew, hh);
+ fnow = f[++findex];
+ Q = Qnew;
+ if (hh != 0.0) {
+ h[hindex++] = hh;
+ }
+ }
+ if ((Q != 0.0) || (hindex == 0)) {
+ h[hindex++] = Q;
+ }
+ return hindex;
+}
+
+/*****************************************************************************/
+/* */
+/* scale_expansion_zeroelim() Multiply an expansion by a scalar, */
+/* eliminating zero components from the */
+/* output expansion. */
+/* */
+/* Sets h = be. See my Robust Predicates paper for details. */
+/* */
+/* Maintains the nonoverlapping property. If round-to-even is used (as */
+/* with IEEE 754), maintains the strongly nonoverlapping and nonadjacent */
+/* properties as well. (That is, if e has one of these properties, so */
+/* will h.) */
+/* */
+/*****************************************************************************/
+
+int scale_expansion_zeroelim(elen, e, b, h) /* e and h cannot be the same. */
+int elen;
+REAL *e;
+REAL b;
+REAL *h;
+{
+ INEXACT REAL Q, sum;
+ REAL hh;
+ INEXACT REAL product1;
+ REAL product0;
+ int eindex, hindex;
+ REAL enow;
+ INEXACT REAL bvirt;
+ REAL avirt, bround, around;
+ INEXACT REAL c;
+ INEXACT REAL abig;
+ REAL ahi, alo, bhi, blo;
+ REAL err1, err2, err3;
+
+ Split(b, bhi, blo);
+ Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
+ hindex = 0;
+ if (hh != 0) {
+ h[hindex++] = hh;
+ }
+ for (eindex = 1; eindex < elen; eindex++) {
+ enow = e[eindex];
+ Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
+ Two_Sum(Q, product0, sum, hh);
+ if (hh != 0) {
+ h[hindex++] = hh;
+ }
+ Fast_Two_Sum(product1, sum, Q, hh);
+ if (hh != 0) {
+ h[hindex++] = hh;
+ }
+ }
+ if ((Q != 0.0) || (hindex == 0)) {
+ h[hindex++] = Q;
+ }
+ return hindex;
+}
+
+/*****************************************************************************/
+/* */
+/* estimate() Produce a one-word estimate of an expansion's value. */
+/* */
+/* See my Robust Predicates paper for details. */
+/* */
+/*****************************************************************************/
+
+REAL estimate(elen, e)
+int elen;
+REAL *e;
+{
+ REAL Q;
+ int eindex;
+
+ Q = e[0];
+ for (eindex = 1; eindex < elen; eindex++) {
+ Q += e[eindex];
+ }
+ return Q;
+}
+
+/*****************************************************************************/
+/* */
+/* counterclockwise() Return a positive value if the points pa, pb, and */
+/* pc occur in counterclockwise order; a negative */
+/* value if they occur in clockwise order; and zero */
+/* if they are collinear. The result is also a rough */
+/* approximation of twice the signed area of the */
+/* triangle defined by the three points. */
+/* */
+/* Uses exact arithmetic if necessary to ensure a correct answer. The */
+/* result returned is the determinant of a matrix. This determinant is */
+/* computed adaptively, in the sense that exact arithmetic is used only to */
+/* the degree it is needed to ensure that the returned value has the */
+/* correct sign. Hence, this function is usually quite fast, but will run */
+/* more slowly when the input points are collinear or nearly so. */
+/* */
+/* See my Robust Predicates paper for details. */
+/* */
+/*****************************************************************************/
+
+REAL counterclockwiseadapt(pa, pb, pc, detsum)
+point pa;
+point pb;
+point pc;
+REAL detsum;
+{
+ INEXACT REAL acx, acy, bcx, bcy;
+ REAL acxtail, acytail, bcxtail, bcytail;
+ INEXACT REAL detleft, detright;
+ REAL detlefttail, detrighttail;
+ REAL det, errbound;
+ REAL B[4], C1[8], C2[12], D[16];
+ INEXACT REAL B3;
+ int C1length, C2length, Dlength;
+ REAL u[4];
+ INEXACT REAL u3;
+ INEXACT REAL s1, t1;
+ REAL s0, t0;
+
+ INEXACT REAL bvirt;
+ REAL avirt, bround, around;
+ INEXACT REAL c;
+ INEXACT REAL abig;
+ REAL ahi, alo, bhi, blo;
+ REAL err1, err2, err3;
+ INEXACT REAL _i, _j;
+ REAL _0;
+
+ acx = (REAL) (pa[0] - pc[0]);
+ bcx = (REAL) (pb[0] - pc[0]);
+ acy = (REAL) (pa[1] - pc[1]);
+ bcy = (REAL) (pb[1] - pc[1]);
+
+ Two_Product(acx, bcy, detleft, detlefttail);
+ Two_Product(acy, bcx, detright, detrighttail);
+
+ Two_Two_Diff(detleft, detlefttail, detright, detrighttail,
+ B3, B[2], B[1], B[0]);
+ B[3] = B3;
+
+ det = estimate(4, B);
+ errbound = (REAL)(ccwerrboundB * detsum);
+ if ((det >= errbound) || (-det >= errbound)) {
+ return det;
+ }
+
+ Two_Diff_Tail(pa[0], pc[0], acx, acxtail);
+ Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);
+ Two_Diff_Tail(pa[1], pc[1], acy, acytail);
+ Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);
+
+ if ((acxtail == 0.0) && (acytail == 0.0)
+ && (bcxtail == 0.0) && (bcytail == 0.0)) {
+ return det;
+ }
+
+ errbound = (REAL)(ccwerrboundC * detsum + resulterrbound * Absolute(det));
+ det += (acx * bcytail + bcy * acxtail)
+ - (acy * bcxtail + bcx * acytail);
+ if ((det >= errbound) || (-det >= errbound)) {
+ return det;
+ }
+
+ Two_Product(acxtail, bcy, s1, s0);
+ Two_Product(acytail, bcx, t1, t0);
+ Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
+ u[3] = u3;
+ C1length = fast_expansion_sum_zeroelim(4, B, 4, u, C1);
+
+ Two_Product(acx, bcytail, s1, s0);
+ Two_Product(acy, bcxtail, t1, t0);
+ Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
+ u[3] = u3;
+ C2length = fast_expansion_sum_zeroelim(C1length, C1, 4, u, C2);
+
+ Two_Product(acxtail, bcytail, s1, s0);
+ Two_Product(acytail, bcxtail, t1, t0);
+ Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
+ u[3] = u3;
+ Dlength = fast_expansion_sum_zeroelim(C2length, C2, 4, u, D);
+
+ return(D[Dlength - 1]);
+}
+
+REAL counterclockwise(pa, pb, pc)
+point pa;
+point pb;
+point pc;
+{
+ REAL detleft, detright, det;
+ REAL detsum, errbound;
+
+ counterclockcount++;
+
+ detleft = (pa[0] - pc[0]) * (pb[1] - pc[1]);
+ detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
+ det = detleft - detright;
+
+ if (noexact) {
+ return det;
+ }
+
+ if (detleft > 0.0) {
+ if (detright <= 0.0) {
+ return det;
+ } else {
+ detsum = detleft + detright;
+ }
+ } else if (detleft < 0.0) {
+ if (detright >= 0.0) {
+ return det;
+ } else {
+ detsum = -detleft - detright;
+ }
+ } else {
+ return det;
+ }
+
+ errbound = ccwerrboundA * detsum;
+ if ((det >= errbound) || (-det >= errbound)) {
+ return det;
+ }
+
+ return counterclockwiseadapt(pa, pb, pc, detsum);
+}
+
+/*****************************************************************************/
+/* */
+/* incircle() Return a positive value if the point pd lies inside the */
+/* circle passing through pa, pb, and pc; a negative value if */
+/* it lies outside; and zero if the four points are cocircular.*/
+/* The points pa, pb, and pc must be in counterclockwise */
+/* order, or the sign of the result will be reversed. */
+/* */
+/* Uses exact arithmetic if necessary to ensure a correct answer. The */
+/* result returned is the determinant of a matrix. This determinant is */
+/* computed adaptively, in the sense that exact arithmetic is used only to */
+/* the degree it is needed to ensure that the returned value has the */
+/* correct sign. Hence, this function is usually quite fast, but will run */
+/* more slowly when the input points are cocircular or nearly so. */
+/* */
+/* See my Robust Predicates paper for details. */
+/* */
+/*****************************************************************************/
+
+REAL incircleadapt(pa, pb, pc, pd, permanent)
+point pa;
+point pb;
+point pc;
+point pd;
+REAL permanent;
+{
+ INEXACT REAL adx, bdx, cdx, ady, bdy, cdy;
+ REAL det, errbound;
+
+ INEXACT REAL bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
+ REAL bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
+ REAL bc[4], ca[4], ab[4];
+ INEXACT REAL bc3, ca3, ab3;
+ REAL axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];
+ int axbclen, axxbclen, aybclen, ayybclen, alen;
+ REAL bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];
+ int bxcalen, bxxcalen, bycalen, byycalen, blen;
+ REAL cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];
+ int cxablen, cxxablen, cyablen, cyyablen, clen;
+ REAL abdet[64];
+ int ablen;
+ REAL fin1[1152], fin2[1152];
+ REAL *finnow, *finother, *finswap;
+ int finlength;
+
+ REAL adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;
+ INEXACT REAL adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;
+ REAL adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;
+ REAL aa[4], bb[4], cc[4];
+ INEXACT REAL aa3, bb3, cc3;
+ INEXACT REAL ti1, tj1;
+ REAL ti0, tj0;
+ REAL u[4], v[4];
+ INEXACT REAL u3, v3;
+ REAL temp8[8], temp16a[16], temp16b[16], temp16c[16];
+ REAL temp32a[32], temp32b[32], temp48[48], temp64[64];
+ int temp8len, temp16alen, temp16blen, temp16clen;
+ int temp32alen, temp32blen, temp48len, temp64len;
+ REAL axtbb[8], axtcc[8], aytbb[8], aytcc[8];
+ int axtbblen, axtcclen, aytbblen, aytcclen;
+ REAL bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];
+ int bxtaalen, bxtcclen, bytaalen, bytcclen;
+ REAL cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];
+ int cxtaalen, cxtbblen, cytaalen, cytbblen;
+ REAL axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];
+ int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;
+ REAL axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16], cytabt[16];
+ int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;
+ REAL axtbctt[8], aytbctt[8], bxtcatt[8];
+ REAL bytcatt[8], cxtabtt[8], cytabtt[8];
+ int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;
+ REAL abt[8], bct[8], cat[8];
+ int abtlen, bctlen, catlen;
+ REAL abtt[4], bctt[4], catt[4];
+ int abttlen, bcttlen, cattlen;
+ INEXACT REAL abtt3, bctt3, catt3;
+ REAL negate;
+
+ INEXACT REAL bvirt;
+ REAL avirt, bround, around;
+ INEXACT REAL c;
+ INEXACT REAL abig;
+ REAL ahi, alo, bhi, blo;
+ REAL err1, err2, err3;
+ INEXACT REAL _i, _j;
+ REAL _0;
+
+ adx = (REAL) (pa[0] - pd[0]);
+ bdx = (REAL) (pb[0] - pd[0]);
+ cdx = (REAL) (pc[0] - pd[0]);
+ ady = (REAL) (pa[1] - pd[1]);
+ bdy = (REAL) (pb[1] - pd[1]);
+ cdy = (REAL) (pc[1] - pd[1]);
+
+ Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
+ Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
+ Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
+ bc[3] = bc3;
+ axbclen = scale_expansion_zeroelim(4, bc, adx, axbc);
+ axxbclen = scale_expansion_zeroelim(axbclen, axbc, adx, axxbc);
+ aybclen = scale_expansion_zeroelim(4, bc, ady, aybc);
+ ayybclen = scale_expansion_zeroelim(aybclen, aybc, ady, ayybc);
+ alen = fast_expansion_sum_zeroelim(axxbclen, axxbc, ayybclen, ayybc, adet);
+
+ Two_Product(cdx, ady, cdxady1, cdxady0);
+ Two_Product(adx, cdy, adxcdy1, adxcdy0);
+ Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
+ ca[3] = ca3;
+ bxcalen = scale_expansion_zeroelim(4, ca, bdx, bxca);
+ bxxcalen = scale_expansion_zeroelim(bxcalen, bxca, bdx, bxxca);
+ bycalen = scale_expansion_zeroelim(4, ca, bdy, byca);
+ byycalen = scale_expansion_zeroelim(bycalen, byca, bdy, byyca);
+ blen = fast_expansion_sum_zeroelim(bxxcalen, bxxca, byycalen, byyca, bdet);
+
+ Two_Product(adx, bdy, adxbdy1, adxbdy0);
+ Two_Product(bdx, ady, bdxady1, bdxady0);
+ Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
+ ab[3] = ab3;
+ cxablen = scale_expansion_zeroelim(4, ab, cdx, cxab);
+ cxxablen = scale_expansion_zeroelim(cxablen, cxab, cdx, cxxab);
+ cyablen = scale_expansion_zeroelim(4, ab, cdy, cyab);
+ cyyablen = scale_expansion_zeroelim(cyablen, cyab, cdy, cyyab);
+ clen = fast_expansion_sum_zeroelim(cxxablen, cxxab, cyyablen, cyyab, cdet);
+
+ ablen = fast_expansion_sum_zeroelim(alen, adet, blen, bdet, abdet);
+ finlength = fast_expansion_sum_zeroelim(ablen, abdet, clen, cdet, fin1);
+
+ det = estimate(finlength, fin1);
+ errbound = (REAL)(iccerrboundB * permanent);
+ if ((det >= errbound) || (-det >= errbound)) {
+ return det;
+ }
+
+ Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
+ Two_Diff_Tail(pa[1], pd[1], ady, adytail);
+ Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
+ Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
+ Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
+ Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
+ if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0)
+ && (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0)) {
+ return det;
+ }
+
+ errbound = (REAL)(iccerrboundC * permanent + resulterrbound * Absolute(det));
+ det += (REAL)(((adx * adx + ady * ady) * ((bdx * cdytail + cdy * bdxtail)
+ - (bdy * cdxtail + cdx * bdytail))
+ + 2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx))
+ + ((bdx * bdx + bdy * bdy) * ((cdx * adytail + ady * cdxtail)
+ - (cdy * adxtail + adx * cdytail))
+ + 2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx))
+ + ((cdx * cdx + cdy * cdy) * ((adx * bdytail + bdy * adxtail)
+ - (ady * bdxtail + bdx * adytail))
+ + 2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx)));
+ if ((det >= errbound) || (-det >= errbound)) {
+ return det;
+ }
+
+ finnow = fin1;
+ finother = fin2;
+
+ if ((bdxtail != 0.0) || (bdytail != 0.0)
+ || (cdxtail != 0.0) || (cdytail != 0.0)) {
+ Square(adx, adxadx1, adxadx0);
+ Square(ady, adyady1, adyady0);
+ Two_Two_Sum(adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);
+ aa[3] = aa3;
+ }
+ if ((cdxtail != 0.0) || (cdytail != 0.0)
+ || (adxtail != 0.0) || (adytail != 0.0)) {
+ Square(bdx, bdxbdx1, bdxbdx0);
+ Square(bdy, bdybdy1, bdybdy0);
+ Two_Two_Sum(bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);
+ bb[3] = bb3;
+ }
+ if ((adxtail != 0.0) || (adytail != 0.0)
+ || (bdxtail != 0.0) || (bdytail != 0.0)) {
+ Square(cdx, cdxcdx1, cdxcdx0);
+ Square(cdy, cdycdy1, cdycdy0);
+ Two_Two_Sum(cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);
+ cc[3] = cc3;
+ }
+
+ if (adxtail != 0.0) {
+ axtbclen = scale_expansion_zeroelim(4, bc, adxtail, axtbc);
+ temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, 2.0 * adx,
+ temp16a);
+
+ axtcclen = scale_expansion_zeroelim(4, cc, adxtail, axtcc);
+ temp16blen = scale_expansion_zeroelim(axtcclen, axtcc, bdy, temp16b);
+
+ axtbblen = scale_expansion_zeroelim(4, bb, adxtail, axtbb);
+ temp16clen = scale_expansion_zeroelim(axtbblen, axtbb, -cdy, temp16c);
+
+ temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (adytail != 0.0) {
+ aytbclen = scale_expansion_zeroelim(4, bc, adytail, aytbc);
+ temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, 2.0 * ady,
+ temp16a);
+
+ aytbblen = scale_expansion_zeroelim(4, bb, adytail, aytbb);
+ temp16blen = scale_expansion_zeroelim(aytbblen, aytbb, cdx, temp16b);
+
+ aytcclen = scale_expansion_zeroelim(4, cc, adytail, aytcc);
+ temp16clen = scale_expansion_zeroelim(aytcclen, aytcc, -bdx, temp16c);
+
+ temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (bdxtail != 0.0) {
+ bxtcalen = scale_expansion_zeroelim(4, ca, bdxtail, bxtca);
+ temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, 2.0 * bdx,
+ temp16a);
+
+ bxtaalen = scale_expansion_zeroelim(4, aa, bdxtail, bxtaa);
+ temp16blen = scale_expansion_zeroelim(bxtaalen, bxtaa, cdy, temp16b);
+
+ bxtcclen = scale_expansion_zeroelim(4, cc, bdxtail, bxtcc);
+ temp16clen = scale_expansion_zeroelim(bxtcclen, bxtcc, -ady, temp16c);
+
+ temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (bdytail != 0.0) {
+ bytcalen = scale_expansion_zeroelim(4, ca, bdytail, bytca);
+ temp16alen = scale_expansion_zeroelim(bytcalen, bytca, 2.0 * bdy,
+ temp16a);
+
+ bytcclen = scale_expansion_zeroelim(4, cc, bdytail, bytcc);
+ temp16blen = scale_expansion_zeroelim(bytcclen, bytcc, adx, temp16b);
+
+ bytaalen = scale_expansion_zeroelim(4, aa, bdytail, bytaa);
+ temp16clen = scale_expansion_zeroelim(bytaalen, bytaa, -cdx, temp16c);
+
+ temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (cdxtail != 0.0) {
+ cxtablen = scale_expansion_zeroelim(4, ab, cdxtail, cxtab);
+ temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, 2.0 * cdx,
+ temp16a);
+
+ cxtbblen = scale_expansion_zeroelim(4, bb, cdxtail, cxtbb);
+ temp16blen = scale_expansion_zeroelim(cxtbblen, cxtbb, ady, temp16b);
+
+ cxtaalen = scale_expansion_zeroelim(4, aa, cdxtail, cxtaa);
+ temp16clen = scale_expansion_zeroelim(cxtaalen, cxtaa, -bdy, temp16c);
+
+ temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (cdytail != 0.0) {
+ cytablen = scale_expansion_zeroelim(4, ab, cdytail, cytab);
+ temp16alen = scale_expansion_zeroelim(cytablen, cytab, 2.0 * cdy,
+ temp16a);
+
+ cytaalen = scale_expansion_zeroelim(4, aa, cdytail, cytaa);
+ temp16blen = scale_expansion_zeroelim(cytaalen, cytaa, bdx, temp16b);
+
+ cytbblen = scale_expansion_zeroelim(4, bb, cdytail, cytbb);
+ temp16clen = scale_expansion_zeroelim(cytbblen, cytbb, -adx, temp16c);
+
+ temp32alen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16clen, temp16c,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+
+ if ((adxtail != 0.0) || (adytail != 0.0)) {
+ if ((bdxtail != 0.0) || (bdytail != 0.0)
+ || (cdxtail != 0.0) || (cdytail != 0.0)) {
+ Two_Product(bdxtail, cdy, ti1, ti0);
+ Two_Product(bdx, cdytail, tj1, tj0);
+ Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
+ u[3] = u3;
+ negate = -bdy;
+ Two_Product(cdxtail, negate, ti1, ti0);
+ negate = -bdytail;
+ Two_Product(cdx, negate, tj1, tj0);
+ Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
+ v[3] = v3;
+ bctlen = fast_expansion_sum_zeroelim(4, u, 4, v, bct);
+
+ Two_Product(bdxtail, cdytail, ti1, ti0);
+ Two_Product(cdxtail, bdytail, tj1, tj0);
+ Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);
+ bctt[3] = bctt3;
+ bcttlen = 4;
+ } else {
+ bct[0] = 0.0;
+ bctlen = 1;
+ bctt[0] = 0.0;
+ bcttlen = 1;
+ }
+
+ if (adxtail != 0.0) {
+ temp16alen = scale_expansion_zeroelim(axtbclen, axtbc, adxtail, temp16a);
+ axtbctlen = scale_expansion_zeroelim(bctlen, bct, adxtail, axtbct);
+ temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, 2.0 * adx,
+ temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ if (bdytail != 0.0) {
+ temp8len = scale_expansion_zeroelim(4, cc, adxtail, temp8);
+ temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
+ temp16a);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
+ temp16a, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (cdytail != 0.0) {
+ temp8len = scale_expansion_zeroelim(4, bb, -adxtail, temp8);
+ temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
+ temp16a);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
+ temp16a, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+
+ temp32alen = scale_expansion_zeroelim(axtbctlen, axtbct, adxtail,
+ temp32a);
+ axtbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adxtail, axtbctt);
+ temp16alen = scale_expansion_zeroelim(axtbcttlen, axtbctt, 2.0 * adx,
+ temp16a);
+ temp16blen = scale_expansion_zeroelim(axtbcttlen, axtbctt, adxtail,
+ temp16b);
+ temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32b);
+ temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
+ temp32blen, temp32b, temp64);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
+ temp64, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (adytail != 0.0) {
+ temp16alen = scale_expansion_zeroelim(aytbclen, aytbc, adytail, temp16a);
+ aytbctlen = scale_expansion_zeroelim(bctlen, bct, adytail, aytbct);
+ temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, 2.0 * ady,
+ temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+
+
+ temp32alen = scale_expansion_zeroelim(aytbctlen, aytbct, adytail,
+ temp32a);
+ aytbcttlen = scale_expansion_zeroelim(bcttlen, bctt, adytail, aytbctt);
+ temp16alen = scale_expansion_zeroelim(aytbcttlen, aytbctt, 2.0 * ady,
+ temp16a);
+ temp16blen = scale_expansion_zeroelim(aytbcttlen, aytbctt, adytail,
+ temp16b);
+ temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32b);
+ temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
+ temp32blen, temp32b, temp64);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
+ temp64, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ }
+ if ((bdxtail != 0.0) || (bdytail != 0.0)) {
+ if ((cdxtail != 0.0) || (cdytail != 0.0)
+ || (adxtail != 0.0) || (adytail != 0.0)) {
+ Two_Product(cdxtail, ady, ti1, ti0);
+ Two_Product(cdx, adytail, tj1, tj0);
+ Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
+ u[3] = u3;
+ negate = -cdy;
+ Two_Product(adxtail, negate, ti1, ti0);
+ negate = -cdytail;
+ Two_Product(adx, negate, tj1, tj0);
+ Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
+ v[3] = v3;
+ catlen = fast_expansion_sum_zeroelim(4, u, 4, v, cat);
+
+ Two_Product(cdxtail, adytail, ti1, ti0);
+ Two_Product(adxtail, cdytail, tj1, tj0);
+ Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);
+ catt[3] = catt3;
+ cattlen = 4;
+ } else {
+ cat[0] = 0.0;
+ catlen = 1;
+ catt[0] = 0.0;
+ cattlen = 1;
+ }
+
+ if (bdxtail != 0.0) {
+ temp16alen = scale_expansion_zeroelim(bxtcalen, bxtca, bdxtail, temp16a);
+ bxtcatlen = scale_expansion_zeroelim(catlen, cat, bdxtail, bxtcat);
+ temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, 2.0 * bdx,
+ temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ if (cdytail != 0.0) {
+ temp8len = scale_expansion_zeroelim(4, aa, bdxtail, temp8);
+ temp16alen = scale_expansion_zeroelim(temp8len, temp8, cdytail,
+ temp16a);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
+ temp16a, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (adytail != 0.0) {
+ temp8len = scale_expansion_zeroelim(4, cc, -bdxtail, temp8);
+ temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
+ temp16a);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
+ temp16a, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+
+ temp32alen = scale_expansion_zeroelim(bxtcatlen, bxtcat, bdxtail,
+ temp32a);
+ bxtcattlen = scale_expansion_zeroelim(cattlen, catt, bdxtail, bxtcatt);
+ temp16alen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, 2.0 * bdx,
+ temp16a);
+ temp16blen = scale_expansion_zeroelim(bxtcattlen, bxtcatt, bdxtail,
+ temp16b);
+ temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32b);
+ temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
+ temp32blen, temp32b, temp64);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
+ temp64, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (bdytail != 0.0) {
+ temp16alen = scale_expansion_zeroelim(bytcalen, bytca, bdytail, temp16a);
+ bytcatlen = scale_expansion_zeroelim(catlen, cat, bdytail, bytcat);
+ temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, 2.0 * bdy,
+ temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+
+
+ temp32alen = scale_expansion_zeroelim(bytcatlen, bytcat, bdytail,
+ temp32a);
+ bytcattlen = scale_expansion_zeroelim(cattlen, catt, bdytail, bytcatt);
+ temp16alen = scale_expansion_zeroelim(bytcattlen, bytcatt, 2.0 * bdy,
+ temp16a);
+ temp16blen = scale_expansion_zeroelim(bytcattlen, bytcatt, bdytail,
+ temp16b);
+ temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32b);
+ temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
+ temp32blen, temp32b, temp64);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
+ temp64, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ }
+ if ((cdxtail != 0.0) || (cdytail != 0.0)) {
+ if ((adxtail != 0.0) || (adytail != 0.0)
+ || (bdxtail != 0.0) || (bdytail != 0.0)) {
+ Two_Product(adxtail, bdy, ti1, ti0);
+ Two_Product(adx, bdytail, tj1, tj0);
+ Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
+ u[3] = u3;
+ negate = -ady;
+ Two_Product(bdxtail, negate, ti1, ti0);
+ negate = -adytail;
+ Two_Product(bdx, negate, tj1, tj0);
+ Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
+ v[3] = v3;
+ abtlen = fast_expansion_sum_zeroelim(4, u, 4, v, abt);
+
+ Two_Product(adxtail, bdytail, ti1, ti0);
+ Two_Product(bdxtail, adytail, tj1, tj0);
+ Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);
+ abtt[3] = abtt3;
+ abttlen = 4;
+ } else {
+ abt[0] = 0.0;
+ abtlen = 1;
+ abtt[0] = 0.0;
+ abttlen = 1;
+ }
+
+ if (cdxtail != 0.0) {
+ temp16alen = scale_expansion_zeroelim(cxtablen, cxtab, cdxtail, temp16a);
+ cxtabtlen = scale_expansion_zeroelim(abtlen, abt, cdxtail, cxtabt);
+ temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, 2.0 * cdx,
+ temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ if (adytail != 0.0) {
+ temp8len = scale_expansion_zeroelim(4, bb, cdxtail, temp8);
+ temp16alen = scale_expansion_zeroelim(temp8len, temp8, adytail,
+ temp16a);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
+ temp16a, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (bdytail != 0.0) {
+ temp8len = scale_expansion_zeroelim(4, aa, -cdxtail, temp8);
+ temp16alen = scale_expansion_zeroelim(temp8len, temp8, bdytail,
+ temp16a);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp16alen,
+ temp16a, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+
+ temp32alen = scale_expansion_zeroelim(cxtabtlen, cxtabt, cdxtail,
+ temp32a);
+ cxtabttlen = scale_expansion_zeroelim(abttlen, abtt, cdxtail, cxtabtt);
+ temp16alen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, 2.0 * cdx,
+ temp16a);
+ temp16blen = scale_expansion_zeroelim(cxtabttlen, cxtabtt, cdxtail,
+ temp16b);
+ temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32b);
+ temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
+ temp32blen, temp32b, temp64);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
+ temp64, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ if (cdytail != 0.0) {
+ temp16alen = scale_expansion_zeroelim(cytablen, cytab, cdytail, temp16a);
+ cytabtlen = scale_expansion_zeroelim(abtlen, abt, cdytail, cytabt);
+ temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, 2.0 * cdy,
+ temp32a);
+ temp48len = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp32alen, temp32a, temp48);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp48len,
+ temp48, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+
+
+ temp32alen = scale_expansion_zeroelim(cytabtlen, cytabt, cdytail,
+ temp32a);
+ cytabttlen = scale_expansion_zeroelim(abttlen, abtt, cdytail, cytabtt);
+ temp16alen = scale_expansion_zeroelim(cytabttlen, cytabtt, 2.0 * cdy,
+ temp16a);
+ temp16blen = scale_expansion_zeroelim(cytabttlen, cytabtt, cdytail,
+ temp16b);
+ temp32blen = fast_expansion_sum_zeroelim(temp16alen, temp16a,
+ temp16blen, temp16b, temp32b);
+ temp64len = fast_expansion_sum_zeroelim(temp32alen, temp32a,
+ temp32blen, temp32b, temp64);
+ finlength = fast_expansion_sum_zeroelim(finlength, finnow, temp64len,
+ temp64, finother);
+ finswap = finnow; finnow = finother; finother = finswap;
+ }
+ }
+
+ return finnow[finlength - 1];
+}
+
+REAL incircle(pa, pb, pc, pd)
+point pa;
+point pb;
+point pc;
+point pd;
+{
+ REAL adx, bdx, cdx, ady, bdy, cdy;
+ REAL bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
+ REAL alift, blift, clift;
+ REAL det;
+ REAL permanent, errbound;
+
+ incirclecount++;
+
+ adx = pa[0] - pd[0];
+ bdx = pb[0] - pd[0];
+ cdx = pc[0] - pd[0];
+ ady = pa[1] - pd[1];
+ bdy = pb[1] - pd[1];
+ cdy = pc[1] - pd[1];
+
+ bdxcdy = bdx * cdy;
+ cdxbdy = cdx * bdy;
+ alift = adx * adx + ady * ady;
+
+ cdxady = cdx * ady;
+ adxcdy = adx * cdy;
+ blift = bdx * bdx + bdy * bdy;
+
+ adxbdy = adx * bdy;
+ bdxady = bdx * ady;
+ clift = cdx * cdx + cdy * cdy;
+
+ det = alift * (bdxcdy - cdxbdy)
+ + blift * (cdxady - adxcdy)
+ + clift * (adxbdy - bdxady);
+
+ if (noexact) {
+ return det;
+ }
+
+ permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * alift
+ + (Absolute(cdxady) + Absolute(adxcdy)) * blift
+ + (Absolute(adxbdy) + Absolute(bdxady)) * clift;
+ errbound = iccerrboundA * permanent;
+ if ((det > errbound) || (-det > errbound)) {
+ return det;
+ }
+
+ return incircleadapt(pa, pb, pc, pd, permanent);
+}
+
+/** **/
+/** **/
+/********* Determinant evaluation routines end here *********/
+
+/*****************************************************************************/
+/* */
+/* triangleinit() Initialize some variables. */
+/* */
+/*****************************************************************************/
+
+void triangleinit()
+{
+ points.maxitems = triangles.maxitems = shelles.maxitems = viri.maxitems =
+ badsegments.maxitems = badtriangles.maxitems = splaynodes.maxitems = 0l;
+ points.itembytes = triangles.itembytes = shelles.itembytes = viri.itembytes =
+ badsegments.itembytes = badtriangles.itembytes = splaynodes.itembytes = 0;
+ recenttri.tri = (triangle *) NULL; /* No triangle has been visited yet. */
+ samples = 1; /* Point location should take at least one sample. */
+ checksegments = 0; /* There are no segments in the triangulation yet. */
+ incirclecount = counterclockcount = hyperbolacount = 0;
+ circumcentercount = circletopcount = 0;
+ randomseed = 1;
+
+ exactinit(); /* Initialize exact arithmetic constants. */
+}
+
+/*****************************************************************************/
+/* */
+/* randomnation() Generate a random number between 0 and `choices' - 1. */
+/* */
+/* This is a simple linear congruential random number generator. Hence, it */
+/* is a bad random number generator, but good enough for most randomized */
+/* geometric algorithms. */
+/* */
+/*****************************************************************************/
+
+unsigned long randomnation(choices)
+unsigned int choices;
+{
+ randomseed = (randomseed * 1366l + 150889l) % 714025l;
+ return randomseed / (714025l / choices + 1);
+}
+
+/********* Mesh quality testing routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* checkmesh() Test the mesh for topological consistency. */
+/* */
+/*****************************************************************************/
+
+#ifndef REDUCED
+
+void checkmesh()
+{
+ struct triedge triangleloop;
+ struct triedge oppotri, oppooppotri;
+ point triorg, tridest, triapex;
+ point oppoorg, oppodest;
+ int horrors;
+ int saveexact;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ /* Temporarily turn on exact arithmetic if it's off. */
+ saveexact = noexact;
+ noexact = 0;
+ if (!quiet) {
+ printf(" Checking consistency of mesh...\n");
+ }
+ horrors = 0;
+ /* Run through the list of triangles, checking each one. */
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ while (triangleloop.tri != (triangle *) NULL) {
+ /* Check all three edges of the triangle. */
+ for (triangleloop.orient = 0; triangleloop.orient < 3;
+ triangleloop.orient++) {
+ org(triangleloop, triorg);
+ dest(triangleloop, tridest);
+ if (triangleloop.orient == 0) { /* Only test for inversion once. */
+ /* Test if the triangle is flat or inverted. */
+ apex(triangleloop, triapex);
+ if (counterclockwise(triorg, tridest, triapex) <= 0.0) {
+ printf(" !! !! Inverted ");
+ printtriangle(&triangleloop);
+ horrors++;
+ }
+ }
+ /* Find the neighboring triangle on this edge. */
+ sym(triangleloop, oppotri);
+ if (oppotri.tri != dummytri) {
+ /* Check that the triangle's neighbor knows it's a neighbor. */
+ sym(oppotri, oppooppotri);
+ if ((triangleloop.tri != oppooppotri.tri)
+ || (triangleloop.orient != oppooppotri.orient)) {
+ printf(" !! !! Asymmetric triangle-triangle bond:\n");
+ if (triangleloop.tri == oppooppotri.tri) {
+ printf(" (Right triangle, wrong orientation)\n");
+ }
+ printf(" First ");
+ printtriangle(&triangleloop);
+ printf(" Second (nonreciprocating) ");
+ printtriangle(&oppotri);
+ horrors++;
+ }
+ /* Check that both triangles agree on the identities */
+ /* of their shared vertices. */
+ org(oppotri, oppoorg);
+ dest(oppotri, oppodest);
+ if ((triorg != oppodest) || (tridest != oppoorg)) {
+ printf(" !! !! Mismatched edge coordinates between two triangles:\n"
+ );
+ printf(" First mismatched ");
+ printtriangle(&triangleloop);
+ printf(" Second mismatched ");
+ printtriangle(&oppotri);
+ horrors++;
+ }
+ }
+ }
+ triangleloop.tri = triangletraverse();
+ }
+ if (horrors == 0) {
+ if (!quiet) {
+ printf(" In my studied opinion, the mesh appears to be consistent.\n");
+ }
+ } else if (horrors == 1) {
+ printf(" !! !! !! !! Precisely one festering wound discovered.\n");
+ } else {
+ printf(" !! !! !! !! %d abominations witnessed.\n", horrors);
+ }
+ /* Restore the status of exact arithmetic. */
+ noexact = saveexact;
+}
+
+#endif /* not REDUCED */
+
+/*****************************************************************************/
+/* */
+/* checkdelaunay() Ensure that the mesh is (constrained) Delaunay. */
+/* */
+/*****************************************************************************/
+
+#ifndef REDUCED
+
+void checkdelaunay()
+{
+ struct triedge triangleloop;
+ struct triedge oppotri;
+ struct edge opposhelle;
+ point triorg, tridest, triapex;
+ point oppoapex;
+ int shouldbedelaunay;
+ int horrors;
+ int saveexact;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ /* Temporarily turn on exact arithmetic if it's off. */
+ saveexact = noexact;
+ noexact = 0;
+ if (!quiet) {
+ printf(" Checking Delaunay property of mesh...\n");
+ }
+ horrors = 0;
+ /* Run through the list of triangles, checking each one. */
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ while (triangleloop.tri != (triangle *) NULL) {
+ /* Check all three edges of the triangle. */
+ for (triangleloop.orient = 0; triangleloop.orient < 3;
+ triangleloop.orient++) {
+ org(triangleloop, triorg);
+ dest(triangleloop, tridest);
+ apex(triangleloop, triapex);
+ sym(triangleloop, oppotri);
+ apex(oppotri, oppoapex);
+ /* Only test that the edge is locally Delaunay if there is an */
+ /* adjoining triangle whose pointer is larger (to ensure that */
+ /* each pair isn't tested twice). */
+ shouldbedelaunay = (oppotri.tri != dummytri)
+ && (triapex != (point) NULL) && (oppoapex != (point) NULL)
+ && (triangleloop.tri < oppotri.tri);
+ if (checksegments && shouldbedelaunay) {
+ /* If a shell edge separates the triangles, then the edge is */
+ /* constrained, so no local Delaunay test should be done. */
+ tspivot(triangleloop, opposhelle);
+ if (opposhelle.sh != dummysh){
+ shouldbedelaunay = 0;
+ }
+ }
+ if (shouldbedelaunay) {
+ if (incircle(triorg, tridest, triapex, oppoapex) > 0.0) {
+ printf(" !! !! Non-Delaunay pair of triangles:\n");
+ printf(" First non-Delaunay ");
+ printtriangle(&triangleloop);
+ printf(" Second non-Delaunay ");
+ printtriangle(&oppotri);
+ horrors++;
+ }
+ }
+ }
+ triangleloop.tri = triangletraverse();
+ }
+ if (horrors == 0) {
+ if (!quiet) {
+ printf(
+ " By virtue of my perceptive intelligence, I declare the mesh Delaunay.\n");
+ }
+ } else if (horrors == 1) {
+ printf(
+ " !! !! !! !! Precisely one terrifying transgression identified.\n");
+ } else {
+ printf(" !! !! !! !! %d obscenities viewed with horror.\n", horrors);
+ }
+ /* Restore the status of exact arithmetic. */
+ noexact = saveexact;
+}
+
+#endif /* not REDUCED */
+
+/*****************************************************************************/
+/* */
+/* enqueuebadtri() Add a bad triangle to the end of a queue. */
+/* */
+/* The queue is actually a set of 64 queues. I use multiple queues to give */
+/* priority to smaller angles. I originally implemented a heap, but the */
+/* queues are (to my surprise) much faster. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void enqueuebadtri(instri, angle, insapex, insorg, insdest)
+struct triedge *instri;
+REAL angle;
+point insapex;
+point insorg;
+point insdest;
+{
+ struct badface *newface;
+ int queuenumber;
+
+ if (verbose > 2) {
+ printf(" Queueing bad triangle:\n");
+ printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", insorg[0],
+ insorg[1], insdest[0], insdest[1], insapex[0], insapex[1]);
+ }
+ /* Allocate space for the bad triangle. */
+ newface = (struct badface *) poolalloc(&badtriangles);
+ triedgecopy(*instri, newface->badfacetri);
+ newface->key = angle;
+ newface->faceapex = insapex;
+ newface->faceorg = insorg;
+ newface->facedest = insdest;
+ newface->nextface = (struct badface *) NULL;
+ /* Determine the appropriate queue to put the bad triangle into. */
+ if (angle > 0.6) {
+ queuenumber = (int) (160.0 * (angle - 0.6));
+ if (queuenumber > 63) {
+ queuenumber = 63;
+ }
+ } else {
+ /* It's not a bad angle; put the triangle in the lowest-priority queue. */
+ queuenumber = 0;
+ }
+ /* Add the triangle to the end of a queue. */
+ *queuetail[queuenumber] = newface;
+ /* Maintain a pointer to the NULL pointer at the end of the queue. */
+ queuetail[queuenumber] = &newface->nextface;
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* dequeuebadtri() Remove a triangle from the front of the queue. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+struct badface *dequeuebadtri()
+{
+ struct badface *result;
+ int queuenumber;
+
+ /* Look for a nonempty queue. */
+ for (queuenumber = 63; queuenumber >= 0; queuenumber--) {
+ result = queuefront[queuenumber];
+ if (result != (struct badface *) NULL) {
+ /* Remove the triangle from the queue. */
+ queuefront[queuenumber] = result->nextface;
+ /* Maintain a pointer to the NULL pointer at the end of the queue. */
+ if (queuefront[queuenumber] == (struct badface *) NULL) {
+ queuetail[queuenumber] = &queuefront[queuenumber];
+ }
+ return result;
+ }
+ }
+ return (struct badface *) NULL;
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* checkedge4encroach() Check a segment to see if it is encroached; add */
+/* it to the list if it is. */
+/* */
+/* An encroached segment is an unflippable edge that has a point in its */
+/* diametral circle (that is, it faces an angle greater than 90 degrees). */
+/* This definition is due to Ruppert. */
+/* */
+/* Returns a nonzero value if the edge is encroached. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+int checkedge4encroach(testedge)
+struct edge *testedge;
+{
+ struct triedge neighbortri;
+ struct edge testsym;
+ struct edge *badedge;
+ int addtolist;
+ int sides;
+ point eorg, edest, eapex;
+ triangle ptr; /* Temporary variable used by stpivot(). */
+
+ addtolist = 0;
+ sides = 0;
+
+ sorg(*testedge, eorg);
+ sdest(*testedge, edest);
+ /* Check one neighbor of the shell edge. */
+ stpivot(*testedge, neighbortri);
+ /* Does the neighbor exist, or is this a boundary edge? */
+ if (neighbortri.tri != dummytri) {
+ sides++;
+ /* Find a vertex opposite this edge. */
+ apex(neighbortri, eapex);
+ /* Check whether the vertex is inside the diametral circle of the */
+ /* shell edge. Pythagoras' Theorem is used to check whether the */
+ /* angle at the vertex is greater than 90 degrees. */
+ if (eapex[0] * (eorg[0] + edest[0]) + eapex[1] * (eorg[1] + edest[1]) >
+ eapex[0] * eapex[0] + eorg[0] * edest[0] +
+ eapex[1] * eapex[1] + eorg[1] * edest[1]) {
+ addtolist = 1;
+ }
+ }
+ /* Check the other neighbor of the shell edge. */
+ ssym(*testedge, testsym);
+ stpivot(testsym, neighbortri);
+ /* Does the neighbor exist, or is this a boundary edge? */
+ if (neighbortri.tri != dummytri) {
+ sides++;
+ /* Find the other vertex opposite this edge. */
+ apex(neighbortri, eapex);
+ /* Check whether the vertex is inside the diametral circle of the */
+ /* shell edge. Pythagoras' Theorem is used to check whether the */
+ /* angle at the vertex is greater than 90 degrees. */
+ if (eapex[0] * (eorg[0] + edest[0]) +
+ eapex[1] * (eorg[1] + edest[1]) >
+ eapex[0] * eapex[0] + eorg[0] * edest[0] +
+ eapex[1] * eapex[1] + eorg[1] * edest[1]) {
+ addtolist += 2;
+ }
+ }
+
+ if (addtolist && (!nobisect || ((nobisect == 1) && (sides == 2)))) {
+ if (verbose > 2) {
+ printf(" Queueing encroached segment (%.12g, %.12g) (%.12g, %.12g).\n",
+ eorg[0], eorg[1], edest[0], edest[1]);
+ }
+ /* Add the shell edge to the list of encroached segments. */
+ /* Be sure to get the orientation right. */
+ badedge = (struct edge *) poolalloc(&badsegments);
+ if (addtolist == 1) {
+ shellecopy(*testedge, *badedge);
+ } else {
+ shellecopy(testsym, *badedge);
+ }
+ }
+ return addtolist;
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* testtriangle() Test a face for quality measures. */
+/* */
+/* Tests a triangle to see if it satisfies the minimum angle condition and */
+/* the maximum area condition. Triangles that aren't up to spec are added */
+/* to the bad triangle queue. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void testtriangle(testtri)
+struct triedge *testtri;
+{
+ struct triedge sametesttri;
+ struct edge edge1, edge2;
+ point torg, tdest, tapex;
+ point anglevertex;
+ REAL dxod, dyod, dxda, dyda, dxao, dyao;
+ REAL dxod2, dyod2, dxda2, dyda2, dxao2, dyao2;
+ REAL apexlen, orglen, destlen;
+ REAL angle;
+ REAL area;
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ org(*testtri, torg);
+ dest(*testtri, tdest);
+ apex(*testtri, tapex);
+ dxod = torg[0] - tdest[0];
+ dyod = torg[1] - tdest[1];
+ dxda = tdest[0] - tapex[0];
+ dyda = tdest[1] - tapex[1];
+ dxao = tapex[0] - torg[0];
+ dyao = tapex[1] - torg[1];
+ dxod2 = dxod * dxod;
+ dyod2 = dyod * dyod;
+ dxda2 = dxda * dxda;
+ dyda2 = dyda * dyda;
+ dxao2 = dxao * dxao;
+ dyao2 = dyao * dyao;
+ /* Find the lengths of the triangle's three edges. */
+ apexlen = dxod2 + dyod2;
+ orglen = dxda2 + dyda2;
+ destlen = dxao2 + dyao2;
+ if ((apexlen < orglen) && (apexlen < destlen)) {
+ /* The edge opposite the apex is shortest. */
+ /* Find the square of the cosine of the angle at the apex. */
+ angle = dxda * dxao + dyda * dyao;
+ angle = angle * angle / (orglen * destlen);
+ anglevertex = tapex;
+ lnext(*testtri, sametesttri);
+ tspivot(sametesttri, edge1);
+ lnextself(sametesttri);
+ tspivot(sametesttri, edge2);
+ } else if (orglen < destlen) {
+ /* The edge opposite the origin is shortest. */
+ /* Find the square of the cosine of the angle at the origin. */
+ angle = dxod * dxao + dyod * dyao;
+ angle = angle * angle / (apexlen * destlen);
+ anglevertex = torg;
+ tspivot(*testtri, edge1);
+ lprev(*testtri, sametesttri);
+ tspivot(sametesttri, edge2);
+ } else {
+ /* The edge opposite the destination is shortest. */
+ /* Find the square of the cosine of the angle at the destination. */
+ angle = dxod * dxda + dyod * dyda;
+ angle = angle * angle / (apexlen * orglen);
+ anglevertex = tdest;
+ tspivot(*testtri, edge1);
+ lnext(*testtri, sametesttri);
+ tspivot(sametesttri, edge2);
+ }
+ /* Check if both edges that form the angle are segments. */
+ if ((edge1.sh != dummysh) && (edge2.sh != dummysh)) {
+ /* The angle is a segment intersection. */
+ if ((angle > 0.9924) && !quiet) { /* Roughly 5 degrees. */
+ if (angle > 1.0) {
+ /* Beware of a floating exception in acos(). */
+ angle = 1.0;
+ }
+ /* Find the actual angle in degrees, for printing. */
+ angle = acos(sqrt(angle)) * (180.0 / PI);
+ printf(
+ "Warning: Small angle (%.4g degrees) between segments at point\n",
+ angle);
+ printf(" (%.12g, %.12g)\n", anglevertex[0], anglevertex[1]);
+ }
+ /* Don't add this bad triangle to the list; there's nothing that */
+ /* can be done about a small angle between two segments. */
+ angle = 0.0;
+ }
+ /* Check whether the angle is smaller than permitted. */
+ if (angle > goodangle) {
+ /* Add this triangle to the list of bad triangles. */
+ enqueuebadtri(testtri, angle, tapex, torg, tdest);
+ return;
+ }
+ if (vararea || fixedarea) {
+ /* Check whether the area is larger than permitted. */
+ area = 0.5 * (dxod * dyda - dyod * dxda);
+ if (fixedarea && (area > maxarea)) {
+ /* Add this triangle to the list of bad triangles. */
+ enqueuebadtri(testtri, angle, tapex, torg, tdest);
+ } else if (vararea) {
+ /* Nonpositive area constraints are treated as unconstrained. */
+ if ((area > areabound(*testtri)) && (areabound(*testtri) > 0.0)) {
+ /* Add this triangle to the list of bad triangles. */
+ enqueuebadtri(testtri, angle, tapex, torg, tdest);
+ }
+ }
+ }
+}
+
+#endif /* not CDT_ONLY */
+
+/** **/
+/** **/
+/********* Mesh quality testing routines end here *********/
+
+/********* Point location routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* makepointmap() Construct a mapping from points to triangles to improve */
+/* the speed of point location for segment insertion. */
+/* */
+/* Traverses all the triangles, and provides each corner of each triangle */
+/* with a pointer to that triangle. Of course, pointers will be */
+/* overwritten by other pointers because (almost) each point is a corner */
+/* of several triangles, but in the end every point will point to some */
+/* triangle that contains it. */
+/* */
+/*****************************************************************************/
+
+void makepointmap()
+{
+ struct triedge triangleloop;
+ point triorg;
+
+ if (verbose) {
+ printf(" Constructing mapping from points to triangles.\n");
+ }
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ while (triangleloop.tri != (triangle *) NULL) {
+ /* Check all three points of the triangle. */
+ for (triangleloop.orient = 0; triangleloop.orient < 3;
+ triangleloop.orient++) {
+ org(triangleloop, triorg);
+ setpoint2tri(triorg, encode(triangleloop));
+ }
+ triangleloop.tri = triangletraverse();
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* preciselocate() Find a triangle or edge containing a given point. */
+/* */
+/* Begins its search from `searchtri'. It is important that `searchtri' */
+/* be a handle with the property that `searchpoint' is strictly to the left */
+/* of the edge denoted by `searchtri', or is collinear with that edge and */
+/* does not intersect that edge. (In particular, `searchpoint' should not */
+/* be the origin or destination of that edge.) */
+/* */
+/* These conditions are imposed because preciselocate() is normally used in */
+/* one of two situations: */
+/* */
+/* (1) To try to find the location to insert a new point. Normally, we */
+/* know an edge that the point is strictly to the left of. In the */
+/* incremental Delaunay algorithm, that edge is a bounding box edge. */
+/* In Ruppert's Delaunay refinement algorithm for quality meshing, */
+/* that edge is the shortest edge of the triangle whose circumcenter */
+/* is being inserted. */
+/* */
+/* (2) To try to find an existing point. In this case, any edge on the */
+/* convex hull is a good starting edge. The possibility that the */
+/* vertex one seeks is an endpoint of the starting edge must be */
+/* screened out before preciselocate() is called. */
+/* */
+/* On completion, `searchtri' is a triangle that contains `searchpoint'. */
+/* */
+/* This implementation differs from that given by Guibas and Stolfi. It */
+/* walks from triangle to triangle, crossing an edge only if `searchpoint' */
+/* is on the other side of the line containing that edge. After entering */
+/* a triangle, there are two edges by which one can leave that triangle. */
+/* If both edges are valid (`searchpoint' is on the other side of both */
+/* edges), one of the two is chosen by drawing a line perpendicular to */
+/* the entry edge (whose endpoints are `forg' and `fdest') passing through */
+/* `fapex'. Depending on which side of this perpendicular `searchpoint' */
+/* falls on, an exit edge is chosen. */
+/* */
+/* This implementation is empirically faster than the Guibas and Stolfi */
+/* point location routine (which I originally used), which tends to spiral */
+/* in toward its target. */
+/* */
+/* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */
+/* is a handle whose origin is the existing vertex. */
+/* */
+/* Returns ONEDGE if the point lies on a mesh edge. `searchtri' is a */
+/* handle whose primary edge is the edge on which the point lies. */
+/* */
+/* Returns INTRIANGLE if the point lies strictly within a triangle. */
+/* `searchtri' is a handle on the triangle that contains the point. */
+/* */
+/* Returns OUTSIDE if the point lies outside the mesh. `searchtri' is a */
+/* handle whose primary edge the point is to the right of. This might */
+/* occur when the circumcenter of a triangle falls just slightly outside */
+/* the mesh due to floating-point roundoff error. It also occurs when */
+/* seeking a hole or region point that a foolish user has placed outside */
+/* the mesh. */
+/* */
+/* WARNING: This routine is designed for convex triangulations, and will */
+/* not generally work after the holes and concavities have been carved. */
+/* However, it can still be used to find the circumcenter of a triangle, as */
+/* long as the search is begun from the triangle in question. */
+/* */
+/*****************************************************************************/
+
+enum locateresult preciselocate(searchpoint, searchtri)
+point searchpoint;
+struct triedge *searchtri;
+{
+ struct triedge backtracktri;
+ point forg, fdest, fapex;
+ point swappoint;
+ REAL orgorient, destorient;
+ int moveleft;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ if (verbose > 2) {
+ printf(" Searching for point (%.12g, %.12g).\n",
+ searchpoint[0], searchpoint[1]);
+ }
+ /* Where are we? */
+ org(*searchtri, forg);
+ dest(*searchtri, fdest);
+ apex(*searchtri, fapex);
+ while (1) {
+ if (verbose > 2) {
+ printf(" At (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
+ forg[0], forg[1], fdest[0], fdest[1], fapex[0], fapex[1]);
+ }
+ /* Check whether the apex is the point we seek. */
+ if ((fapex[0] == searchpoint[0]) && (fapex[1] == searchpoint[1])) {
+ lprevself(*searchtri);
+ return ONVERTEX;
+ }
+ /* Does the point lie on the other side of the line defined by the */
+ /* triangle edge opposite the triangle's destination? */
+ destorient = counterclockwise(forg, fapex, searchpoint);
+ /* Does the point lie on the other side of the line defined by the */
+ /* triangle edge opposite the triangle's origin? */
+ orgorient = counterclockwise(fapex, fdest, searchpoint);
+ if (destorient > 0.0) {
+ if (orgorient > 0.0) {
+ /* Move left if the inner product of (fapex - searchpoint) and */
+ /* (fdest - forg) is positive. This is equivalent to drawing */
+ /* a line perpendicular to the line (forg, fdest) passing */
+ /* through `fapex', and determining which side of this line */
+ /* `searchpoint' falls on. */
+ moveleft = (fapex[0] - searchpoint[0]) * (fdest[0] - forg[0]) +
+ (fapex[1] - searchpoint[1]) * (fdest[1] - forg[1]) > 0.0;
+ } else {
+ moveleft = 1;
+ }
+ } else {
+ if (orgorient > 0.0) {
+ moveleft = 0;
+ } else {
+ /* The point we seek must be on the boundary of or inside this */
+ /* triangle. */
+ if (destorient == 0.0) {
+ lprevself(*searchtri);
+ return ONEDGE;
+ }
+ if (orgorient == 0.0) {
+ lnextself(*searchtri);
+ return ONEDGE;
+ }
+ return INTRIANGLE;
+ }
+ }
+
+ /* Move to another triangle. Leave a trace `backtracktri' in case */
+ /* floating-point roundoff or some such bogey causes us to walk */
+ /* off a boundary of the triangulation. We can just bounce off */
+ /* the boundary as if it were an elastic band. */
+ if (moveleft) {
+ lprev(*searchtri, backtracktri);
+ fdest = fapex;
+ } else {
+ lnext(*searchtri, backtracktri);
+ forg = fapex;
+ }
+ sym(backtracktri, *searchtri);
+
+ /* Check for walking off the edge. */
+ if (searchtri->tri == dummytri) {
+ /* Turn around. */
+ triedgecopy(backtracktri, *searchtri);
+ swappoint = forg;
+ forg = fdest;
+ fdest = swappoint;
+ apex(*searchtri, fapex);
+ /* Check if the point really is beyond the triangulation boundary. */
+ destorient = counterclockwise(forg, fapex, searchpoint);
+ orgorient = counterclockwise(fapex, fdest, searchpoint);
+ if ((orgorient < 0.0) && (destorient < 0.0)) {
+ return OUTSIDE;
+ }
+ } else {
+ apex(*searchtri, fapex);
+ }
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* locate() Find a triangle or edge containing a given point. */
+/* */
+/* Searching begins from one of: the input `searchtri', a recently */
+/* encountered triangle `recenttri', or from a triangle chosen from a */
+/* random sample. The choice is made by determining which triangle's */
+/* origin is closest to the point we are searcing for. Normally, */
+/* `searchtri' should be a handle on the convex hull of the triangulation. */
+/* */
+/* Details on the random sampling method can be found in the Mucke, Saias, */
+/* and Zhu paper cited in the header of this code. */
+/* */
+/* On completion, `searchtri' is a triangle that contains `searchpoint'. */
+/* */
+/* Returns ONVERTEX if the point lies on an existing vertex. `searchtri' */
+/* is a handle whose origin is the existing vertex. */
+/* */
+/* Returns ONEDGE if the point lies on a mesh edge. `searchtri' is a */
+/* handle whose primary edge is the edge on which the point lies. */
+/* */
+/* Returns INTRIANGLE if the point lies strictly within a triangle. */
+/* `searchtri' is a handle on the triangle that contains the point. */
+/* */
+/* Returns OUTSIDE if the point lies outside the mesh. `searchtri' is a */
+/* handle whose primary edge the point is to the right of. This might */
+/* occur when the circumcenter of a triangle falls just slightly outside */
+/* the mesh due to floating-point roundoff error. It also occurs when */
+/* seeking a hole or region point that a foolish user has placed outside */
+/* the mesh. */
+/* */
+/* WARNING: This routine is designed for convex triangulations, and will */
+/* not generally work after the holes and concavities have been carved. */
+/* */
+/*****************************************************************************/
+
+enum locateresult locate(searchpoint, searchtri)
+point searchpoint;
+struct triedge *searchtri;
+{
+ VOID **sampleblock;
+ triangle *firsttri;
+ struct triedge sampletri;
+ point torg, tdest;
+ unsigned long alignptr;
+ REAL searchdist, dist;
+ REAL ahead;
+ long sampleblocks, samplesperblock, samplenum;
+ long triblocks;
+ long i, j;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ if (verbose > 2) {
+ printf(" Randomly sampling for a triangle near point (%.12g, %.12g).\n",
+ searchpoint[0], searchpoint[1]);
+ }
+ /* Record the distance from the suggested starting triangle to the */
+ /* point we seek. */
+ org(*searchtri, torg);
+ searchdist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
+ + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
+ if (verbose > 2) {
+ printf(" Boundary triangle has origin (%.12g, %.12g).\n",
+ torg[0], torg[1]);
+ }
+
+ /* If a recently encountered triangle has been recorded and has not been */
+ /* deallocated, test it as a good starting point. */
+ if (recenttri.tri != (triangle *) NULL) {
+ if (recenttri.tri[3] != (triangle) NULL) {
+ org(recenttri, torg);
+ if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
+ triedgecopy(recenttri, *searchtri);
+ return ONVERTEX;
+ }
+ dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
+ + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
+ if (dist < searchdist) {
+ triedgecopy(recenttri, *searchtri);
+ searchdist = dist;
+ if (verbose > 2) {
+ printf(" Choosing recent triangle with origin (%.12g, %.12g).\n",
+ torg[0], torg[1]);
+ }
+ }
+ }
+ }
+
+ /* The number of random samples taken is proportional to the cube root of */
+ /* the number of triangles in the mesh. The next bit of code assumes */
+ /* that the number of triangles increases monotonically. */
+ while (SAMPLEFACTOR * samples * samples * samples < triangles.items) {
+ samples++;
+ }
+ triblocks = (triangles.maxitems + TRIPERBLOCK - 1) / TRIPERBLOCK;
+ samplesperblock = 1 + (samples / triblocks);
+ sampleblocks = samples / samplesperblock;
+ sampleblock = triangles.firstblock;
+ sampletri.orient = 0;
+ for (i = 0; i < sampleblocks; i++) {
+ alignptr = (unsigned long) (sampleblock + 1);
+ firsttri = (triangle *) (alignptr + (unsigned long) triangles.alignbytes
+ - (alignptr % (unsigned long) triangles.alignbytes));
+ for (j = 0; j < samplesperblock; j++) {
+ if (i == triblocks - 1) {
+ samplenum = randomnation((int)
+ (triangles.maxitems - (i * TRIPERBLOCK)));
+ } else {
+ samplenum = randomnation(TRIPERBLOCK);
+ }
+ sampletri.tri = (triangle *)
+ (firsttri + (samplenum * triangles.itemwords));
+ if (sampletri.tri[3] != (triangle) NULL) {
+ org(sampletri, torg);
+ dist = (searchpoint[0] - torg[0]) * (searchpoint[0] - torg[0])
+ + (searchpoint[1] - torg[1]) * (searchpoint[1] - torg[1]);
+ if (dist < searchdist) {
+ triedgecopy(sampletri, *searchtri);
+ searchdist = dist;
+ if (verbose > 2) {
+ printf(" Choosing triangle with origin (%.12g, %.12g).\n",
+ torg[0], torg[1]);
+ }
+ }
+ }
+ }
+ sampleblock = (VOID **) *sampleblock;
+ }
+ /* Where are we? */
+ org(*searchtri, torg);
+ dest(*searchtri, tdest);
+ /* Check the starting triangle's vertices. */
+ if ((torg[0] == searchpoint[0]) && (torg[1] == searchpoint[1])) {
+ return ONVERTEX;
+ }
+ if ((tdest[0] == searchpoint[0]) && (tdest[1] == searchpoint[1])) {
+ lnextself(*searchtri);
+ return ONVERTEX;
+ }
+ /* Orient `searchtri' to fit the preconditions of calling preciselocate(). */
+ ahead = counterclockwise(torg, tdest, searchpoint);
+ if (ahead < 0.0) {
+ /* Turn around so that `searchpoint' is to the left of the */
+ /* edge specified by `searchtri'. */
+ symself(*searchtri);
+ } else if (ahead == 0.0) {
+ /* Check if `searchpoint' is between `torg' and `tdest'. */
+ if (((torg[0] < searchpoint[0]) == (searchpoint[0] < tdest[0]))
+ && ((torg[1] < searchpoint[1]) == (searchpoint[1] < tdest[1]))) {
+ return ONEDGE;
+ }
+ }
+ return preciselocate(searchpoint, searchtri);
+}
+
+/** **/
+/** **/
+/********* Point location routines end here *********/
+
+/********* Mesh transformation routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* insertshelle() Create a new shell edge and insert it between two */
+/* triangles. */
+/* */
+/* The new shell edge is inserted at the edge described by the handle */
+/* `tri'. Its vertices are properly initialized. The marker `shellemark' */
+/* is applied to the shell edge and, if appropriate, its vertices. */
+/* */
+/*****************************************************************************/
+
+void insertshelle(tri, shellemark)
+struct triedge *tri; /* Edge at which to insert the new shell edge. */
+int shellemark; /* Marker for the new shell edge. */
+{
+ struct triedge oppotri;
+ struct edge newshelle;
+ point triorg, tridest;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ /* Mark points if possible. */
+ org(*tri, triorg);
+ dest(*tri, tridest);
+ if (pointmark(triorg) == 0) {
+ setpointmark(triorg, shellemark);
+ }
+ if (pointmark(tridest) == 0) {
+ setpointmark(tridest, shellemark);
+ }
+ /* Check if there's already a shell edge here. */
+ tspivot(*tri, newshelle);
+ if (newshelle.sh == dummysh) {
+ /* Make new shell edge and initialize its vertices. */
+ makeshelle(&newshelle);
+ setsorg(newshelle, tridest);
+ setsdest(newshelle, triorg);
+ /* Bond new shell edge to the two triangles it is sandwiched between. */
+ /* Note that the facing triangle `oppotri' might be equal to */
+ /* `dummytri' (outer space), but the new shell edge is bonded to it */
+ /* all the same. */
+ tsbond(*tri, newshelle);
+ sym(*tri, oppotri);
+ ssymself(newshelle);
+ tsbond(oppotri, newshelle);
+ setmark(newshelle, shellemark);
+ if (verbose > 2) {
+ printf(" Inserting new ");
+ printshelle(&newshelle);
+ }
+ } else {
+ if (mark(newshelle) == 0) {
+ setmark(newshelle, shellemark);
+ }
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* Terminology */
+/* */
+/* A "local transformation" replaces a small set of triangles with another */
+/* set of triangles. This may or may not involve inserting or deleting a */
+/* point. */
+/* */
+/* The term "casing" is used to describe the set of triangles that are */
+/* attached to the triangles being transformed, but are not transformed */
+/* themselves. Think of the casing as a fixed hollow structure inside */
+/* which all the action happens. A "casing" is only defined relative to */
+/* a single transformation; each occurrence of a transformation will */
+/* involve a different casing. */
+/* */
+/* A "shell" is similar to a "casing". The term "shell" describes the set */
+/* of shell edges (if any) that are attached to the triangles being */
+/* transformed. However, I sometimes use "shell" to refer to a single */
+/* shell edge, so don't get confused. */
+/* */
+/*****************************************************************************/
+
+/*****************************************************************************/
+/* */
+/* flip() Transform two triangles to two different triangles by flipping */
+/* an edge within a quadrilateral. */
+/* */
+/* Imagine the original triangles, abc and bad, oriented so that the */
+/* shared edge ab lies in a horizontal plane, with the point b on the left */
+/* and the point a on the right. The point c lies below the edge, and the */
+/* point d lies above the edge. The `flipedge' handle holds the edge ab */
+/* of triangle abc, and is directed left, from vertex a to vertex b. */
+/* */
+/* The triangles abc and bad are deleted and replaced by the triangles cdb */
+/* and dca. The triangles that represent abc and bad are NOT deallocated; */
+/* they are reused for dca and cdb, respectively. Hence, any handles that */
+/* may have held the original triangles are still valid, although not */
+/* directed as they were before. */
+/* */
+/* Upon completion of this routine, the `flipedge' handle holds the edge */
+/* dc of triangle dca, and is directed down, from vertex d to vertex c. */
+/* (Hence, the two triangles have rotated counterclockwise.) */
+/* */
+/* WARNING: This transformation is geometrically valid only if the */
+/* quadrilateral adbc is convex. Furthermore, this transformation is */
+/* valid only if there is not a shell edge between the triangles abc and */
+/* bad. This routine does not check either of these preconditions, and */
+/* it is the responsibility of the calling routine to ensure that they are */
+/* met. If they are not, the streets shall be filled with wailing and */
+/* gnashing of teeth. */
+/* */
+/*****************************************************************************/
+
+void flip(flipedge)
+struct triedge *flipedge; /* Handle for the triangle abc. */
+{
+ struct triedge botleft, botright;
+ struct triedge topleft, topright;
+ struct triedge top;
+ struct triedge botlcasing, botrcasing;
+ struct triedge toplcasing, toprcasing;
+ struct edge botlshelle, botrshelle;
+ struct edge toplshelle, toprshelle;
+ point leftpoint, rightpoint, botpoint;
+ point farpoint;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ /* Identify the vertices of the quadrilateral. */
+ org(*flipedge, rightpoint);
+ dest(*flipedge, leftpoint);
+ apex(*flipedge, botpoint);
+ sym(*flipedge, top);
+#ifdef SELF_CHECK
+ if (top.tri == dummytri) {
+ printf("Internal error in flip(): Attempt to flip on boundary.\n");
+ lnextself(*flipedge);
+ return;
+ }
+ if (checksegments) {
+ tspivot(*flipedge, toplshelle);
+ if (toplshelle.sh != dummysh) {
+ printf("Internal error in flip(): Attempt to flip a segment.\n");
+ lnextself(*flipedge);
+ return;
+ }
+ }
+#endif /* SELF_CHECK */
+ apex(top, farpoint);
+
+ /* Identify the casing of the quadrilateral. */
+ lprev(top, topleft);
+ sym(topleft, toplcasing);
+ lnext(top, topright);
+ sym(topright, toprcasing);
+ lnext(*flipedge, botleft);
+ sym(botleft, botlcasing);
+ lprev(*flipedge, botright);
+ sym(botright, botrcasing);
+ /* Rotate the quadrilateral one-quarter turn counterclockwise. */
+ bond(topleft, botlcasing);
+ bond(botleft, botrcasing);
+ bond(botright, toprcasing);
+ bond(topright, toplcasing);
+
+ if (checksegments) {
+ /* Check for shell edges and rebond them to the quadrilateral. */
+ tspivot(topleft, toplshelle);
+ tspivot(botleft, botlshelle);
+ tspivot(botright, botrshelle);
+ tspivot(topright, toprshelle);
+ if (toplshelle.sh == dummysh) {
+ tsdissolve(topright);
+ } else {
+ tsbond(topright, toplshelle);
+ }
+ if (botlshelle.sh == dummysh) {
+ tsdissolve(topleft);
+ } else {
+ tsbond(topleft, botlshelle);
+ }
+ if (botrshelle.sh == dummysh) {
+ tsdissolve(botleft);
+ } else {
+ tsbond(botleft, botrshelle);
+ }
+ if (toprshelle.sh == dummysh) {
+ tsdissolve(botright);
+ } else {
+ tsbond(botright, toprshelle);
+ }
+ }
+
+ /* New point assignments for the rotated quadrilateral. */
+ setorg(*flipedge, farpoint);
+ setdest(*flipedge, botpoint);
+ setapex(*flipedge, rightpoint);
+ setorg(top, botpoint);
+ setdest(top, farpoint);
+ setapex(top, leftpoint);
+ if (verbose > 2) {
+ printf(" Edge flip results in left ");
+ lnextself(topleft);
+ printtriangle(&topleft);
+ printf(" and right ");
+ printtriangle(flipedge);
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* insertsite() Insert a vertex into a Delaunay triangulation, */
+/* performing flips as necessary to maintain the Delaunay */
+/* property. */
+/* */
+/* The point `insertpoint' is located. If `searchtri.tri' is not NULL, */
+/* the search for the containing triangle begins from `searchtri'. If */
+/* `searchtri.tri' is NULL, a full point location procedure is called. */
+/* If `insertpoint' is found inside a triangle, the triangle is split into */
+/* three; if `insertpoint' lies on an edge, the edge is split in two, */
+/* thereby splitting the two adjacent triangles into four. Edge flips are */
+/* used to restore the Delaunay property. If `insertpoint' lies on an */
+/* existing vertex, no action is taken, and the value DUPLICATEPOINT is */
+/* returned. On return, `searchtri' is set to a handle whose origin is the */
+/* existing vertex. */
+/* */
+/* Normally, the parameter `splitedge' is set to NULL, implying that no */
+/* segment should be split. In this case, if `insertpoint' is found to */
+/* lie on a segment, no action is taken, and the value VIOLATINGPOINT is */
+/* returned. On return, `searchtri' is set to a handle whose primary edge */
+/* is the violated segment. */
+/* */
+/* If the calling routine wishes to split a segment by inserting a point in */
+/* it, the parameter `splitedge' should be that segment. In this case, */
+/* `searchtri' MUST be the triangle handle reached by pivoting from that */
+/* segment; no point location is done. */
+/* */
+/* `segmentflaws' and `triflaws' are flags that indicate whether or not */
+/* there should be checks for the creation of encroached segments or bad */
+/* quality faces. If a newly inserted point encroaches upon segments, */
+/* these segments are added to the list of segments to be split if */
+/* `segmentflaws' is set. If bad triangles are created, these are added */
+/* to the queue if `triflaws' is set. */
+/* */
+/* If a duplicate point or violated segment does not prevent the point */
+/* from being inserted, the return value will be ENCROACHINGPOINT if the */
+/* point encroaches upon a segment (and checking is enabled), or */
+/* SUCCESSFULPOINT otherwise. In either case, `searchtri' is set to a */
+/* handle whose origin is the newly inserted vertex. */
+/* */
+/* insertsite() does not use flip() for reasons of speed; some */
+/* information can be reused from edge flip to edge flip, like the */
+/* locations of shell edges. */
+/* */
+/*****************************************************************************/
+
+enum insertsiteresult insertsite(insertpoint, searchtri, splitedge,
+ segmentflaws, triflaws)
+point insertpoint;
+struct triedge *searchtri;
+struct edge *splitedge;
+int segmentflaws;
+int triflaws;
+{
+ struct triedge horiz;
+ struct triedge top;
+ struct triedge botleft, botright;
+ struct triedge topleft, topright;
+ struct triedge newbotleft, newbotright;
+ struct triedge newtopright;
+ struct triedge botlcasing, botrcasing;
+ struct triedge toplcasing, toprcasing;
+ struct triedge testtri;
+ struct edge botlshelle, botrshelle;
+ struct edge toplshelle, toprshelle;
+ struct edge brokenshelle;
+ struct edge checkshelle;
+ struct edge rightedge;
+ struct edge newedge;
+ struct edge *encroached;
+ point first;
+ point leftpoint, rightpoint, botpoint, toppoint, farpoint;
+ REAL attrib;
+ REAL area;
+ enum insertsiteresult success;
+ enum locateresult intersect;
+ int doflip;
+ int mirrorflag;
+ int i;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by spivot() and tspivot(). */
+
+ if (verbose > 1) {
+ printf(" Inserting (%.12g, %.12g).\n", insertpoint[0], insertpoint[1]);
+ }
+ if (splitedge == (struct edge *) NULL) {
+ /* Find the location of the point to be inserted. Check if a good */
+ /* starting triangle has already been provided by the caller. */
+ if (searchtri->tri == (triangle *) NULL) {
+ /* Find a boundary triangle. */
+ horiz.tri = dummytri;
+ horiz.orient = 0;
+ symself(horiz);
+ /* Search for a triangle containing `insertpoint'. */
+ intersect = locate(insertpoint, &horiz);
+ } else {
+ /* Start searching from the triangle provided by the caller. */
+ triedgecopy(*searchtri, horiz);
+ intersect = preciselocate(insertpoint, &horiz);
+ }
+ } else {
+ /* The calling routine provides the edge in which the point is inserted. */
+ triedgecopy(*searchtri, horiz);
+ intersect = ONEDGE;
+ }
+ if (intersect == ONVERTEX) {
+ /* There's already a vertex there. Return in `searchtri' a triangle */
+ /* whose origin is the existing vertex. */
+ triedgecopy(horiz, *searchtri);
+ triedgecopy(horiz, recenttri);
+ return DUPLICATEPOINT;
+ }
+ if ((intersect == ONEDGE) || (intersect == OUTSIDE)) {
+ /* The vertex falls on an edge or boundary. */
+ if (checksegments && (splitedge == (struct edge *) NULL)) {
+ /* Check whether the vertex falls on a shell edge. */
+ tspivot(horiz, brokenshelle);
+ if (brokenshelle.sh != dummysh) {
+ /* The vertex falls on a shell edge. */
+ if (segmentflaws) {
+ if (nobisect == 0) {
+ /* Add the shell edge to the list of encroached segments. */
+ encroached = (struct edge *) poolalloc(&badsegments);
+ shellecopy(brokenshelle, *encroached);
+ } else if ((nobisect == 1) && (intersect == ONEDGE)) {
+ /* This segment may be split only if it is an internal boundary. */
+ sym(horiz, testtri);
+ if (testtri.tri != dummytri) {
+ /* Add the shell edge to the list of encroached segments. */
+ encroached = (struct edge *) poolalloc(&badsegments);
+ shellecopy(brokenshelle, *encroached);
+ }
+ }
+ }
+ /* Return a handle whose primary edge contains the point, */
+ /* which has not been inserted. */
+ triedgecopy(horiz, *searchtri);
+ triedgecopy(horiz, recenttri);
+ return VIOLATINGPOINT;
+ }
+ }
+ /* Insert the point on an edge, dividing one triangle into two (if */
+ /* the edge lies on a boundary) or two triangles into four. */
+ lprev(horiz, botright);
+ sym(botright, botrcasing);
+ sym(horiz, topright);
+ /* Is there a second triangle? (Or does this edge lie on a boundary?) */
+ mirrorflag = topright.tri != dummytri;
+ if (mirrorflag) {
+ lnextself(topright);
+ sym(topright, toprcasing);
+ maketriangle(&newtopright);
+ } else {
+ /* Splitting the boundary edge increases the number of boundary edges. */
+ hullsize++;
+ }
+ maketriangle(&newbotright);
+
+ /* Set the vertices of changed and new triangles. */
+ org(horiz, rightpoint);
+ dest(horiz, leftpoint);
+ apex(horiz, botpoint);
+ setorg(newbotright, botpoint);
+ setdest(newbotright, rightpoint);
+ setapex(newbotright, insertpoint);
+ setorg(horiz, insertpoint);
+ for (i = 0; i < eextras; i++) {
+ /* Set the element attributes of a new triangle. */
+ setelemattribute(newbotright, i, elemattribute(botright, i));
+ }
+ if (vararea) {
+ /* Set the area constraint of a new triangle. */
+ setareabound(newbotright, areabound(botright));
+ }
+ if (mirrorflag) {
+ dest(topright, toppoint);
+ setorg(newtopright, rightpoint);
+ setdest(newtopright, toppoint);
+ setapex(newtopright, insertpoint);
+ setorg(topright, insertpoint);
+ for (i = 0; i < eextras; i++) {
+ /* Set the element attributes of another new triangle. */
+ setelemattribute(newtopright, i, elemattribute(topright, i));
+ }
+ if (vararea) {
+ /* Set the area constraint of another new triangle. */
+ setareabound(newtopright, areabound(topright));
+ }
+ }
+
+ /* There may be shell edges that need to be bonded */
+ /* to the new triangle(s). */
+ if (checksegments) {
+ tspivot(botright, botrshelle);
+ if (botrshelle.sh != dummysh) {
+ tsdissolve(botright);
+ tsbond(newbotright, botrshelle);
+ }
+ if (mirrorflag) {
+ tspivot(topright, toprshelle);
+ if (toprshelle.sh != dummysh) {
+ tsdissolve(topright);
+ tsbond(newtopright, toprshelle);
+ }
+ }
+ }
+
+ /* Bond the new triangle(s) to the surrounding triangles. */
+ bond(newbotright, botrcasing);
+ lprevself(newbotright);
+ bond(newbotright, botright);
+ lprevself(newbotright);
+ if (mirrorflag) {
+ bond(newtopright, toprcasing);
+ lnextself(newtopright);
+ bond(newtopright, topright);
+ lnextself(newtopright);
+ bond(newtopright, newbotright);
+ }
+
+ if (splitedge != (struct edge *) NULL) {
+ /* Split the shell edge into two. */
+ setsdest(*splitedge, insertpoint);
+ ssymself(*splitedge);
+ spivot(*splitedge, rightedge);
+ insertshelle(&newbotright, mark(*splitedge));
+ tspivot(newbotright, newedge);
+ sbond(*splitedge, newedge);
+ ssymself(newedge);
+ sbond(newedge, rightedge);
+ ssymself(*splitedge);
+ }
+
+#ifdef SELF_CHECK
+ if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle prior to edge point insertion (bottom).\n");
+ }
+ if (mirrorflag) {
+ if (counterclockwise(leftpoint, rightpoint, toppoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle prior to edge point insertion (top).\n");
+ }
+ if (counterclockwise(rightpoint, toppoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after edge point insertion (top right).\n"
+ );
+ }
+ if (counterclockwise(toppoint, leftpoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after edge point insertion (top left).\n"
+ );
+ }
+ }
+ if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after edge point insertion (bottom left).\n"
+ );
+ }
+ if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(
+ " Clockwise triangle after edge point insertion (bottom right).\n");
+ }
+#endif /* SELF_CHECK */
+ if (verbose > 2) {
+ printf(" Updating bottom left ");
+ printtriangle(&botright);
+ if (mirrorflag) {
+ printf(" Updating top left ");
+ printtriangle(&topright);
+ printf(" Creating top right ");
+ printtriangle(&newtopright);
+ }
+ printf(" Creating bottom right ");
+ printtriangle(&newbotright);
+ }
+
+ /* Position `horiz' on the first edge to check for */
+ /* the Delaunay property. */
+ lnextself(horiz);
+ } else {
+ /* Insert the point in a triangle, splitting it into three. */
+ lnext(horiz, botleft);
+ lprev(horiz, botright);
+ sym(botleft, botlcasing);
+ sym(botright, botrcasing);
+ maketriangle(&newbotleft);
+ maketriangle(&newbotright);
+
+ /* Set the vertices of changed and new triangles. */
+ org(horiz, rightpoint);
+ dest(horiz, leftpoint);
+ apex(horiz, botpoint);
+ setorg(newbotleft, leftpoint);
+ setdest(newbotleft, botpoint);
+ setapex(newbotleft, insertpoint);
+ setorg(newbotright, botpoint);
+ setdest(newbotright, rightpoint);
+ setapex(newbotright, insertpoint);
+ setapex(horiz, insertpoint);
+ for (i = 0; i < eextras; i++) {
+ /* Set the element attributes of the new triangles. */
+ attrib = elemattribute(horiz, i);
+ setelemattribute(newbotleft, i, attrib);
+ setelemattribute(newbotright, i, attrib);
+ }
+ if (vararea) {
+ /* Set the area constraint of the new triangles. */
+ area = areabound(horiz);
+ setareabound(newbotleft, area);
+ setareabound(newbotright, area);
+ }
+
+ /* There may be shell edges that need to be bonded */
+ /* to the new triangles. */
+ if (checksegments) {
+ tspivot(botleft, botlshelle);
+ if (botlshelle.sh != dummysh) {
+ tsdissolve(botleft);
+ tsbond(newbotleft, botlshelle);
+ }
+ tspivot(botright, botrshelle);
+ if (botrshelle.sh != dummysh) {
+ tsdissolve(botright);
+ tsbond(newbotright, botrshelle);
+ }
+ }
+
+ /* Bond the new triangles to the surrounding triangles. */
+ bond(newbotleft, botlcasing);
+ bond(newbotright, botrcasing);
+ lnextself(newbotleft);
+ lprevself(newbotright);
+ bond(newbotleft, newbotright);
+ lnextself(newbotleft);
+ bond(botleft, newbotleft);
+ lprevself(newbotright);
+ bond(botright, newbotright);
+
+#ifdef SELF_CHECK
+ if (counterclockwise(rightpoint, leftpoint, botpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle prior to point insertion.\n");
+ }
+ if (counterclockwise(rightpoint, leftpoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after point insertion (top).\n");
+ }
+ if (counterclockwise(leftpoint, botpoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after point insertion (left).\n");
+ }
+ if (counterclockwise(botpoint, rightpoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after point insertion (right).\n");
+ }
+#endif /* SELF_CHECK */
+ if (verbose > 2) {
+ printf(" Updating top ");
+ printtriangle(&horiz);
+ printf(" Creating left ");
+ printtriangle(&newbotleft);
+ printf(" Creating right ");
+ printtriangle(&newbotright);
+ }
+ }
+
+ /* The insertion is successful by default, unless an encroached */
+ /* edge is found. */
+ success = SUCCESSFULPOINT;
+ /* Circle around the newly inserted vertex, checking each edge opposite */
+ /* it for the Delaunay property. Non-Delaunay edges are flipped. */
+ /* `horiz' is always the edge being checked. `first' marks where to */
+ /* stop circling. */
+ org(horiz, first);
+ rightpoint = first;
+ dest(horiz, leftpoint);
+ /* Circle until finished. */
+ while (1) {
+ /* By default, the edge will be flipped. */
+ doflip = 1;
+ if (checksegments) {
+ /* Check for a segment, which cannot be flipped. */
+ tspivot(horiz, checkshelle);
+ if (checkshelle.sh != dummysh) {
+ /* The edge is a segment and cannot be flipped. */
+ doflip = 0;
+#ifndef CDT_ONLY
+ if (segmentflaws) {
+ /* Does the new point encroach upon this segment? */
+ if (checkedge4encroach(&checkshelle)) {
+ success = ENCROACHINGPOINT;
+ }
+ }
+#endif /* not CDT_ONLY */
+ }
+ }
+ if (doflip) {
+ /* Check if the edge is a boundary edge. */
+ sym(horiz, top);
+ if (top.tri == dummytri) {
+ /* The edge is a boundary edge and cannot be flipped. */
+ doflip = 0;
+ } else {
+ /* Find the point on the other side of the edge. */
+ apex(top, farpoint);
+ /* In the incremental Delaunay triangulation algorithm, any of */
+ /* `leftpoint', `rightpoint', and `farpoint' could be vertices */
+ /* of the triangular bounding box. These vertices must be */
+ /* treated as if they are infinitely distant, even though their */
+ /* "coordinates" are not. */
+ if ((leftpoint == infpoint1) || (leftpoint == infpoint2)
+ || (leftpoint == infpoint3)) {
+ /* `leftpoint' is infinitely distant. Check the convexity of */
+ /* the boundary of the triangulation. 'farpoint' might be */
+ /* infinite as well, but trust me, this same condition */
+ /* should be applied. */
+ doflip = counterclockwise(insertpoint, rightpoint, farpoint) > 0.0;
+ } else if ((rightpoint == infpoint1) || (rightpoint == infpoint2)
+ || (rightpoint == infpoint3)) {
+ /* `rightpoint' is infinitely distant. Check the convexity of */
+ /* the boundary of the triangulation. 'farpoint' might be */
+ /* infinite as well, but trust me, this same condition */
+ /* should be applied. */
+ doflip = counterclockwise(farpoint, leftpoint, insertpoint) > 0.0;
+ } else if ((farpoint == infpoint1) || (farpoint == infpoint2)
+ || (farpoint == infpoint3)) {
+ /* `farpoint' is infinitely distant and cannot be inside */
+ /* the circumcircle of the triangle `horiz'. */
+ doflip = 0;
+ } else {
+ /* Test whether the edge is locally Delaunay. */
+ doflip = incircle(leftpoint, insertpoint, rightpoint, farpoint)
+ > 0.0;
+ }
+ if (doflip) {
+ /* We made it! Flip the edge `horiz' by rotating its containing */
+ /* quadrilateral (the two triangles adjacent to `horiz'). */
+ /* Identify the casing of the quadrilateral. */
+ lprev(top, topleft);
+ sym(topleft, toplcasing);
+ lnext(top, topright);
+ sym(topright, toprcasing);
+ lnext(horiz, botleft);
+ sym(botleft, botlcasing);
+ lprev(horiz, botright);
+ sym(botright, botrcasing);
+ /* Rotate the quadrilateral one-quarter turn counterclockwise. */
+ bond(topleft, botlcasing);
+ bond(botleft, botrcasing);
+ bond(botright, toprcasing);
+ bond(topright, toplcasing);
+ if (checksegments) {
+ /* Check for shell edges and rebond them to the quadrilateral. */
+ tspivot(topleft, toplshelle);
+ tspivot(botleft, botlshelle);
+ tspivot(botright, botrshelle);
+ tspivot(topright, toprshelle);
+ if (toplshelle.sh == dummysh) {
+ tsdissolve(topright);
+ } else {
+ tsbond(topright, toplshelle);
+ }
+ if (botlshelle.sh == dummysh) {
+ tsdissolve(topleft);
+ } else {
+ tsbond(topleft, botlshelle);
+ }
+ if (botrshelle.sh == dummysh) {
+ tsdissolve(botleft);
+ } else {
+ tsbond(botleft, botrshelle);
+ }
+ if (toprshelle.sh == dummysh) {
+ tsdissolve(botright);
+ } else {
+ tsbond(botright, toprshelle);
+ }
+ }
+ /* New point assignments for the rotated quadrilateral. */
+ setorg(horiz, farpoint);
+ setdest(horiz, insertpoint);
+ setapex(horiz, rightpoint);
+ setorg(top, insertpoint);
+ setdest(top, farpoint);
+ setapex(top, leftpoint);
+ for (i = 0; i < eextras; i++) {
+ /* Take the average of the two triangles' attributes. */
+ attrib = (REAL)(0.5 * (elemattribute(top, i) + elemattribute(horiz, i)));
+ setelemattribute(top, i, attrib);
+ setelemattribute(horiz, i, attrib);
+ }
+ if (vararea) {
+ if ((areabound(top) <= 0.0) || (areabound(horiz) <= 0.0)) {
+ area = -1.0;
+ } else {
+ /* Take the average of the two triangles' area constraints. */
+ /* This prevents small area constraints from migrating a */
+ /* long, long way from their original location due to flips. */
+ area = (REAL)(0.5 * (areabound(top) + areabound(horiz)));
+ }
+ setareabound(top, area);
+ setareabound(horiz, area);
+ }
+#ifdef SELF_CHECK
+ if (insertpoint != (point) NULL) {
+ if (counterclockwise(leftpoint, insertpoint, rightpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle prior to edge flip (bottom).\n");
+ }
+ /* The following test has been removed because constrainededge() */
+ /* sometimes generates inverted triangles that insertsite() */
+ /* removes. */
+/*
+ if (counterclockwise(rightpoint, farpoint, leftpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle prior to edge flip (top).\n");
+ }
+*/
+ if (counterclockwise(farpoint, leftpoint, insertpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after edge flip (left).\n");
+ }
+ if (counterclockwise(insertpoint, rightpoint, farpoint) < 0.0) {
+ printf("Internal error in insertsite():\n");
+ printf(" Clockwise triangle after edge flip (right).\n");
+ }
+ }
+#endif /* SELF_CHECK */
+ if (verbose > 2) {
+ printf(" Edge flip results in left ");
+ lnextself(topleft);
+ printtriangle(&topleft);
+ printf(" and right ");
+ printtriangle(&horiz);
+ }
+ /* On the next iterations, consider the two edges that were */
+ /* exposed (this is, are now visible to the newly inserted */
+ /* point) by the edge flip. */
+ lprevself(horiz);
+ leftpoint = farpoint;
+ }
+ }
+ }
+ if (!doflip) {
+ /* The handle `horiz' is accepted as locally Delaunay. */
+#ifndef CDT_ONLY
+ if (triflaws) {
+ /* Check the triangle `horiz' for quality. */
+ testtriangle(&horiz);
+ }
+#endif /* not CDT_ONLY */
+ /* Look for the next edge around the newly inserted point. */
+ lnextself(horiz);
+ sym(horiz, testtri);
+ /* Check for finishing a complete revolution about the new point, or */
+ /* falling off the edge of the triangulation. The latter will */
+ /* happen when a point is inserted at a boundary. */
+ if ((leftpoint == first) || (testtri.tri == dummytri)) {
+ /* We're done. Return a triangle whose origin is the new point. */
+ lnext(horiz, *searchtri);
+ lnext(horiz, recenttri);
+ return success;
+ }
+ /* Finish finding the next edge around the newly inserted point. */
+ lnext(testtri, horiz);
+ rightpoint = leftpoint;
+ dest(horiz, leftpoint);
+ }
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* triangulatepolygon() Find the Delaunay triangulation of a polygon that */
+/* has a certain "nice" shape. This includes the */
+/* polygons that result from deletion of a point or */
+/* insertion of a segment. */
+/* */
+/* This is a conceptually difficult routine. The starting assumption is */
+/* that we have a polygon with n sides. n - 1 of these sides are currently */
+/* represented as edges in the mesh. One side, called the "base", need not */
+/* be. */
+/* */
+/* Inside the polygon is a structure I call a "fan", consisting of n - 1 */
+/* triangles that share a common origin. For each of these triangles, the */
+/* edge opposite the origin is one of the sides of the polygon. The */
+/* primary edge of each triangle is the edge directed from the origin to */
+/* the destination; note that this is not the same edge that is a side of */
+/* the polygon. `firstedge' is the primary edge of the first triangle. */
+/* From there, the triangles follow in counterclockwise order about the */
+/* polygon, until `lastedge', the primary edge of the last triangle. */
+/* `firstedge' and `lastedge' are probably connected to other triangles */
+/* beyond the extremes of the fan, but their identity is not important, as */
+/* long as the fan remains connected to them. */
+/* */
+/* Imagine the polygon oriented so that its base is at the bottom. This */
+/* puts `firstedge' on the far right, and `lastedge' on the far left. */
+/* The right vertex of the base is the destination of `firstedge', and the */
+/* left vertex of the base is the apex of `lastedge'. */
+/* */
+/* The challenge now is to find the right sequence of edge flips to */
+/* transform the fan into a Delaunay triangulation of the polygon. Each */
+/* edge flip effectively removes one triangle from the fan, committing it */
+/* to the polygon. The resulting polygon has one fewer edge. If `doflip' */
+/* is set, the final flip will be performed, resulting in a fan of one */
+/* (useless?) triangle. If `doflip' is not set, the final flip is not */
+/* performed, resulting in a fan of two triangles, and an unfinished */
+/* triangular polygon that is not yet filled out with a single triangle. */
+/* On completion of the routine, `lastedge' is the last remaining triangle, */
+/* or the leftmost of the last two. */
+/* */
+/* Although the flips are performed in the order described above, the */
+/* decisions about what flips to perform are made in precisely the reverse */
+/* order. The recursive triangulatepolygon() procedure makes a decision, */
+/* uses up to two recursive calls to triangulate the "subproblems" */
+/* (polygons with fewer edges), and then performs an edge flip. */
+/* */
+/* The "decision" it makes is which vertex of the polygon should be */
+/* connected to the base. This decision is made by testing every possible */
+/* vertex. Once the best vertex is found, the two edges that connect this */
+/* vertex to the base become the bases for two smaller polygons. These */
+/* are triangulated recursively. Unfortunately, this approach can take */
+/* O(n^2) time not only in the worst case, but in many common cases. It's */
+/* rarely a big deal for point deletion, where n is rarely larger than ten, */
+/* but it could be a big deal for segment insertion, especially if there's */
+/* a lot of long segments that each cut many triangles. I ought to code */
+/* a faster algorithm some time. */
+/* */
+/* The `edgecount' parameter is the number of sides of the polygon, */
+/* including its base. `triflaws' is a flag that determines whether the */
+/* new triangles should be tested for quality, and enqueued if they are */
+/* bad. */
+/* */
+/*****************************************************************************/
+
+void triangulatepolygon(firstedge, lastedge, edgecount, doflip, triflaws)
+struct triedge *firstedge;
+struct triedge *lastedge;
+int edgecount;
+int doflip;
+int triflaws;
+{
+ struct triedge testtri;
+ struct triedge besttri;
+ struct triedge tempedge;
+ point leftbasepoint, rightbasepoint;
+ point testpoint;
+ point bestpoint;
+ int bestnumber;
+ int i;
+ triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
+
+ /* Identify the base vertices. */
+ apex(*lastedge, leftbasepoint);
+ dest(*firstedge, rightbasepoint);
+ if (verbose > 2) {
+ printf(" Triangulating interior polygon at edge\n");
+ printf(" (%.12g, %.12g) (%.12g, %.12g)\n", leftbasepoint[0],
+ leftbasepoint[1], rightbasepoint[0], rightbasepoint[1]);
+ }
+ /* Find the best vertex to connect the base to. */
+ onext(*firstedge, besttri);
+ dest(besttri, bestpoint);
+ triedgecopy(besttri, testtri);
+ bestnumber = 1;
+ for (i = 2; i <= edgecount - 2; i++) {
+ onextself(testtri);
+ dest(testtri, testpoint);
+ /* Is this a better vertex? */
+ if (incircle(leftbasepoint, rightbasepoint, bestpoint, testpoint) > 0.0) {
+ triedgecopy(testtri, besttri);
+ bestpoint = testpoint;
+ bestnumber = i;
+ }
+ }
+ if (verbose > 2) {
+ printf(" Connecting edge to (%.12g, %.12g)\n", bestpoint[0],
+ bestpoint[1]);
+ }
+ if (bestnumber > 1) {
+ /* Recursively triangulate the smaller polygon on the right. */
+ oprev(besttri, tempedge);
+ triangulatepolygon(firstedge, &tempedge, bestnumber + 1, 1, triflaws);
+ }
+ if (bestnumber < edgecount - 2) {
+ /* Recursively triangulate the smaller polygon on the left. */
+ sym(besttri, tempedge);
+ triangulatepolygon(&besttri, lastedge, edgecount - bestnumber, 1,
+ triflaws);
+ /* Find `besttri' again; it may have been lost to edge flips. */
+ sym(tempedge, besttri);
+ }
+ if (doflip) {
+ /* Do one final edge flip. */
+ flip(&besttri);
+#ifndef CDT_ONLY
+ if (triflaws) {
+ /* Check the quality of the newly committed triangle. */
+ sym(besttri, testtri);
+ testtriangle(&testtri);
+ }
+#endif /* not CDT_ONLY */
+ }
+ /* Return the base triangle. */
+ triedgecopy(besttri, *lastedge);
+}
+
+/*****************************************************************************/
+/* */
+/* deletesite() Delete a vertex from a Delaunay triangulation, ensuring */
+/* that the triangulation remains Delaunay. */
+/* */
+/* The origin of `deltri' is deleted. The union of the triangles adjacent */
+/* to this point is a polygon, for which the Delaunay triangulation is */
+/* found. Two triangles are removed from the mesh. */
+/* */
+/* Only interior points that do not lie on segments (shell edges) or */
+/* boundaries may be deleted. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void deletesite(deltri)
+struct triedge *deltri;
+{
+ struct triedge countingtri;
+ struct triedge firstedge, lastedge;
+ struct triedge deltriright;
+ struct triedge lefttri, righttri;
+ struct triedge leftcasing, rightcasing;
+ struct edge leftshelle, rightshelle;
+ point delpoint;
+ point neworg;
+ int edgecount;
+ triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ org(*deltri, delpoint);
+ if (verbose > 1) {
+ printf(" Deleting (%.12g, %.12g).\n", delpoint[0], delpoint[1]);
+ }
+ pointdealloc(delpoint);
+
+ /* Count the degree of the point being deleted. */
+ onext(*deltri, countingtri);
+ edgecount = 1;
+ while (!triedgeequal(*deltri, countingtri)) {
+#ifdef SELF_CHECK
+ if (countingtri.tri == dummytri) {
+ printf("Internal error in deletesite():\n");
+ printf(" Attempt to delete boundary point.\n");
+ internalerror();
+ }
+#endif /* SELF_CHECK */
+ edgecount++;
+ onextself(countingtri);
+ }
+
+#ifdef SELF_CHECK
+ if (edgecount < 3) {
+ printf("Internal error in deletesite():\n Point has degree %d.\n",
+ edgecount);
+ internalerror();
+ }
+#endif /* SELF_CHECK */
+ if (edgecount > 3) {
+ /* Triangulate the polygon defined by the union of all triangles */
+ /* adjacent to the point being deleted. Check the quality of */
+ /* the resulting triangles. */
+ onext(*deltri, firstedge);
+ oprev(*deltri, lastedge);
+ triangulatepolygon(&firstedge, &lastedge, edgecount, 0, !nobisect);
+ }
+ /* Splice out two triangles. */
+ lprev(*deltri, deltriright);
+ dnext(*deltri, lefttri);
+ sym(lefttri, leftcasing);
+ oprev(deltriright, righttri);
+ sym(righttri, rightcasing);
+ bond(*deltri, leftcasing);
+ bond(deltriright, rightcasing);
+ tspivot(lefttri, leftshelle);
+ if (leftshelle.sh != dummysh) {
+ tsbond(*deltri, leftshelle);
+ }
+ tspivot(righttri, rightshelle);
+ if (rightshelle.sh != dummysh) {
+ tsbond(deltriright, rightshelle);
+ }
+
+ /* Set the new origin of `deltri' and check its quality. */
+ org(lefttri, neworg);
+ setorg(*deltri, neworg);
+ if (!nobisect) {
+ testtriangle(deltri);
+ }
+
+ /* Delete the two spliced-out triangles. */
+ triangledealloc(lefttri.tri);
+ triangledealloc(righttri.tri);
+}
+
+#endif /* not CDT_ONLY */
+
+/** **/
+/** **/
+/********* Mesh transformation routines end here *********/
+
+/********* Divide-and-conquer Delaunay triangulation begins here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* The divide-and-conquer bounding box */
+/* */
+/* I originally implemented the divide-and-conquer and incremental Delaunay */
+/* triangulations using the edge-based data structure presented by Guibas */
+/* and Stolfi. Switching to a triangle-based data structure doubled the */
+/* speed. However, I had to think of a few extra tricks to maintain the */
+/* elegance of the original algorithms. */
+/* */
+/* The "bounding box" used by my variant of the divide-and-conquer */
+/* algorithm uses one triangle for each edge of the convex hull of the */
+/* triangulation. These bounding triangles all share a common apical */
+/* vertex, which is represented by NULL and which represents nothing. */
+/* The bounding triangles are linked in a circular fan about this NULL */
+/* vertex, and the edges on the convex hull of the triangulation appear */
+/* opposite the NULL vertex. You might find it easiest to imagine that */
+/* the NULL vertex is a point in 3D space behind the center of the */
+/* triangulation, and that the bounding triangles form a sort of cone. */
+/* */
+/* This bounding box makes it easy to represent degenerate cases. For */
+/* instance, the triangulation of two vertices is a single edge. This edge */
+/* is represented by two bounding box triangles, one on each "side" of the */
+/* edge. These triangles are also linked together in a fan about the NULL */
+/* vertex. */
+/* */
+/* The bounding box also makes it easy to traverse the convex hull, as the */
+/* divide-and-conquer algorithm needs to do. */
+/* */
+/*****************************************************************************/
+
+/*****************************************************************************/
+/* */
+/* pointsort() Sort an array of points by x-coordinate, using the */
+/* y-coordinate as a secondary key. */
+/* */
+/* Uses quicksort. Randomized O(n log n) time. No, I did not make any of */
+/* the usual quicksort mistakes. */
+/* */
+/*****************************************************************************/
+
+void pointsort(sortarray, arraysize)
+point *sortarray;
+int arraysize;
+{
+ int left, right;
+ int pivot;
+ REAL pivotx, pivoty;
+ point temp;
+
+ if (arraysize == 2) {
+ /* Recursive base case. */
+ if ((sortarray[0][0] > sortarray[1][0]) ||
+ ((sortarray[0][0] == sortarray[1][0]) &&
+ (sortarray[0][1] > sortarray[1][1]))) {
+ temp = sortarray[1];
+ sortarray[1] = sortarray[0];
+ sortarray[0] = temp;
+ }
+ return;
+ }
+ /* Choose a random pivot to split the array. */
+ pivot = (int) randomnation(arraysize);
+ pivotx = sortarray[pivot][0];
+ pivoty = sortarray[pivot][1];
+ /* Split the array. */
+ left = -1;
+ right = arraysize;
+ while (left < right) {
+ /* Search for a point whose x-coordinate is too large for the left. */
+ do {
+ left++;
+ } while ((left <= right) && ((sortarray[left][0] < pivotx) ||
+ ((sortarray[left][0] == pivotx) &&
+ (sortarray[left][1] < pivoty))));
+ /* Search for a point whose x-coordinate is too small for the right. */
+ do {
+ right--;
+ } while ((left <= right) && ((sortarray[right][0] > pivotx) ||
+ ((sortarray[right][0] == pivotx) &&
+ (sortarray[right][1] > pivoty))));
+ if (left < right) {
+ /* Swap the left and right points. */
+ temp = sortarray[left];
+ sortarray[left] = sortarray[right];
+ sortarray[right] = temp;
+ }
+ }
+ if (left > 1) {
+ /* Recursively sort the left subset. */
+ pointsort(sortarray, left);
+ }
+ if (right < arraysize - 2) {
+ /* Recursively sort the right subset. */
+ pointsort(&sortarray[right + 1], arraysize - right - 1);
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* pointmedian() An order statistic algorithm, almost. Shuffles an array */
+/* of points so that the first `median' points occur */
+/* lexicographically before the remaining points. */
+/* */
+/* Uses the x-coordinate as the primary key if axis == 0; the y-coordinate */
+/* if axis == 1. Very similar to the pointsort() procedure, but runs in */
+/* randomized linear time. */
+/* */
+/*****************************************************************************/
+
+void pointmedian(sortarray, arraysize, median, axis)
+point *sortarray;
+int arraysize;
+int median;
+int axis;
+{
+ int left, right;
+ int pivot;
+ REAL pivot1, pivot2;
+ point temp;
+
+ if (arraysize == 2) {
+ /* Recursive base case. */
+ if ((sortarray[0][axis] > sortarray[1][axis]) ||
+ ((sortarray[0][axis] == sortarray[1][axis]) &&
+ (sortarray[0][1 - axis] > sortarray[1][1 - axis]))) {
+ temp = sortarray[1];
+ sortarray[1] = sortarray[0];
+ sortarray[0] = temp;
+ }
+ return;
+ }
+ /* Choose a random pivot to split the array. */
+ pivot = (int) randomnation(arraysize);
+ pivot1 = sortarray[pivot][axis];
+ pivot2 = sortarray[pivot][1 - axis];
+ /* Split the array. */
+ left = -1;
+ right = arraysize;
+ while (left < right) {
+ /* Search for a point whose x-coordinate is too large for the left. */
+ do {
+ left++;
+ } while ((left <= right) && ((sortarray[left][axis] < pivot1) ||
+ ((sortarray[left][axis] == pivot1) &&
+ (sortarray[left][1 - axis] < pivot2))));
+ /* Search for a point whose x-coordinate is too small for the right. */
+ do {
+ right--;
+ } while ((left <= right) && ((sortarray[right][axis] > pivot1) ||
+ ((sortarray[right][axis] == pivot1) &&
+ (sortarray[right][1 - axis] > pivot2))));
+ if (left < right) {
+ /* Swap the left and right points. */
+ temp = sortarray[left];
+ sortarray[left] = sortarray[right];
+ sortarray[right] = temp;
+ }
+ }
+ /* Unlike in pointsort(), at most one of the following */
+ /* conditionals is true. */
+ if (left > median) {
+ /* Recursively shuffle the left subset. */
+ pointmedian(sortarray, left, median, axis);
+ }
+ if (right < median - 1) {
+ /* Recursively shuffle the right subset. */
+ pointmedian(&sortarray[right + 1], arraysize - right - 1,
+ median - right - 1, axis);
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* alternateaxes() Sorts the points as appropriate for the divide-and- */
+/* conquer algorithm with alternating cuts. */
+/* */
+/* Partitions by x-coordinate if axis == 0; by y-coordinate if axis == 1. */
+/* For the base case, subsets containing only two or three points are */
+/* always sorted by x-coordinate. */
+/* */
+/*****************************************************************************/
+
+void alternateaxes(sortarray, arraysize, axis)
+point *sortarray;
+int arraysize;
+int axis;
+{
+ int divider;
+
+ divider = arraysize >> 1;
+ if (arraysize <= 3) {
+ /* Recursive base case: subsets of two or three points will be */
+ /* handled specially, and should always be sorted by x-coordinate. */
+ axis = 0;
+ }
+ /* Partition with a horizontal or vertical cut. */
+ pointmedian(sortarray, arraysize, divider, axis);
+ /* Recursively partition the subsets with a cross cut. */
+ if (arraysize - divider >= 2) {
+ if (divider >= 2) {
+ alternateaxes(sortarray, divider, 1 - axis);
+ }
+ alternateaxes(&sortarray[divider], arraysize - divider, 1 - axis);
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* mergehulls() Merge two adjacent Delaunay triangulations into a */
+/* single Delaunay triangulation. */
+/* */
+/* This is similar to the algorithm given by Guibas and Stolfi, but uses */
+/* a triangle-based, rather than edge-based, data structure. */
+/* */
+/* The algorithm walks up the gap between the two triangulations, knitting */
+/* them together. As they are merged, some of their bounding triangles */
+/* are converted into real triangles of the triangulation. The procedure */
+/* pulls each hull's bounding triangles apart, then knits them together */
+/* like the teeth of two gears. The Delaunay property determines, at each */
+/* step, whether the next "tooth" is a bounding triangle of the left hull */
+/* or the right. When a bounding triangle becomes real, its apex is */
+/* changed from NULL to a real point. */
+/* */
+/* Only two new triangles need to be allocated. These become new bounding */
+/* triangles at the top and bottom of the seam. They are used to connect */
+/* the remaining bounding triangles (those that have not been converted */
+/* into real triangles) into a single fan. */
+/* */
+/* On entry, `farleft' and `innerleft' are bounding triangles of the left */
+/* triangulation. The origin of `farleft' is the leftmost vertex, and */
+/* the destination of `innerleft' is the rightmost vertex of the */
+/* triangulation. Similarly, `innerright' and `farright' are bounding */
+/* triangles of the right triangulation. The origin of `innerright' and */
+/* destination of `farright' are the leftmost and rightmost vertices. */
+/* */
+/* On completion, the origin of `farleft' is the leftmost vertex of the */
+/* merged triangulation, and the destination of `farright' is the rightmost */
+/* vertex. */
+/* */
+/*****************************************************************************/
+
+void mergehulls(farleft, innerleft, innerright, farright, axis)
+struct triedge *farleft;
+struct triedge *innerleft;
+struct triedge *innerright;
+struct triedge *farright;
+int axis;
+{
+ struct triedge leftcand, rightcand;
+ struct triedge baseedge;
+ struct triedge nextedge;
+ struct triedge sidecasing, topcasing, outercasing;
+ struct triedge checkedge;
+ point innerleftdest;
+ point innerrightorg;
+ point innerleftapex, innerrightapex;
+ point farleftpt, farrightpt;
+ point farleftapex, farrightapex;
+ point lowerleft, lowerright;
+ point upperleft, upperright;
+ point nextapex;
+ point checkvertex;
+ int changemade;
+ int badedge;
+ int leftfinished, rightfinished;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ dest(*innerleft, innerleftdest);
+ apex(*innerleft, innerleftapex);
+ org(*innerright, innerrightorg);
+ apex(*innerright, innerrightapex);
+ /* Special treatment for horizontal cuts. */
+ if (dwyer && (axis == 1)) {
+ org(*farleft, farleftpt);
+ apex(*farleft, farleftapex);
+ dest(*farright, farrightpt);
+ apex(*farright, farrightapex);
+ /* The pointers to the extremal points are shifted to point to the */
+ /* topmost and bottommost point of each hull, rather than the */
+ /* leftmost and rightmost points. */
+ while (farleftapex[1] < farleftpt[1]) {
+ lnextself(*farleft);
+ symself(*farleft);
+ farleftpt = farleftapex;
+ apex(*farleft, farleftapex);
+ }
+ sym(*innerleft, checkedge);
+ apex(checkedge, checkvertex);
+ while (checkvertex[1] > innerleftdest[1]) {
+ lnext(checkedge, *innerleft);
+ innerleftapex = innerleftdest;
+ innerleftdest = checkvertex;
+ sym(*innerleft, checkedge);
+ apex(checkedge, checkvertex);
+ }
+ while (innerrightapex[1] < innerrightorg[1]) {
+ lnextself(*innerright);
+ symself(*innerright);
+ innerrightorg = innerrightapex;
+ apex(*innerright, innerrightapex);
+ }
+ sym(*farright, checkedge);
+ apex(checkedge, checkvertex);
+ while (checkvertex[1] > farrightpt[1]) {
+ lnext(checkedge, *farright);
+ farrightapex = farrightpt;
+ farrightpt = checkvertex;
+ sym(*farright, checkedge);
+ apex(checkedge, checkvertex);
+ }
+ }
+ /* Find a line tangent to and below both hulls. */
+ do {
+ changemade = 0;
+ /* Make innerleftdest the "bottommost" point of the left hull. */
+ if (counterclockwise(innerleftdest, innerleftapex, innerrightorg) > 0.0) {
+ lprevself(*innerleft);
+ symself(*innerleft);
+ innerleftdest = innerleftapex;
+ apex(*innerleft, innerleftapex);
+ changemade = 1;
+ }
+ /* Make innerrightorg the "bottommost" point of the right hull. */
+ if (counterclockwise(innerrightapex, innerrightorg, innerleftdest) > 0.0) {
+ lnextself(*innerright);
+ symself(*innerright);
+ innerrightorg = innerrightapex;
+ apex(*innerright, innerrightapex);
+ changemade = 1;
+ }
+ } while (changemade);
+ /* Find the two candidates to be the next "gear tooth". */
+ sym(*innerleft, leftcand);
+ sym(*innerright, rightcand);
+ /* Create the bottom new bounding triangle. */
+ maketriangle(&baseedge);
+ /* Connect it to the bounding boxes of the left and right triangulations. */
+ bond(baseedge, *innerleft);
+ lnextself(baseedge);
+ bond(baseedge, *innerright);
+ lnextself(baseedge);
+ setorg(baseedge, innerrightorg);
+ setdest(baseedge, innerleftdest);
+ /* Apex is intentionally left NULL. */
+ if (verbose > 2) {
+ printf(" Creating base bounding ");
+ printtriangle(&baseedge);
+ }
+ /* Fix the extreme triangles if necessary. */
+ org(*farleft, farleftpt);
+ if (innerleftdest == farleftpt) {
+ lnext(baseedge, *farleft);
+ }
+ dest(*farright, farrightpt);
+ if (innerrightorg == farrightpt) {
+ lprev(baseedge, *farright);
+ }
+ /* The vertices of the current knitting edge. */
+ lowerleft = innerleftdest;
+ lowerright = innerrightorg;
+ /* The candidate vertices for knitting. */
+ apex(leftcand, upperleft);
+ apex(rightcand, upperright);
+ /* Walk up the gap between the two triangulations, knitting them together. */
+ while (1) {
+ /* Have we reached the top? (This isn't quite the right question, */
+ /* because even though the left triangulation might seem finished now, */
+ /* moving up on the right triangulation might reveal a new point of */
+ /* the left triangulation. And vice-versa.) */
+ leftfinished = counterclockwise(upperleft, lowerleft, lowerright) <= 0.0;
+ rightfinished = counterclockwise(upperright, lowerleft, lowerright) <= 0.0;
+ if (leftfinished && rightfinished) {
+ /* Create the top new bounding triangle. */
+ maketriangle(&nextedge);
+ setorg(nextedge, lowerleft);
+ setdest(nextedge, lowerright);
+ /* Apex is intentionally left NULL. */
+ /* Connect it to the bounding boxes of the two triangulations. */
+ bond(nextedge, baseedge);
+ lnextself(nextedge);
+ bond(nextedge, rightcand);
+ lnextself(nextedge);
+ bond(nextedge, leftcand);
+ if (verbose > 2) {
+ printf(" Creating top bounding ");
+ printtriangle(&baseedge);
+ }
+ /* Special treatment for horizontal cuts. */
+ if (dwyer && (axis == 1)) {
+ org(*farleft, farleftpt);
+ apex(*farleft, farleftapex);
+ dest(*farright, farrightpt);
+ apex(*farright, farrightapex);
+ sym(*farleft, checkedge);
+ apex(checkedge, checkvertex);
+ /* The pointers to the extremal points are restored to the leftmost */
+ /* and rightmost points (rather than topmost and bottommost). */
+ while (checkvertex[0] < farleftpt[0]) {
+ lprev(checkedge, *farleft);
+ farleftapex = farleftpt;
+ farleftpt = checkvertex;
+ sym(*farleft, checkedge);
+ apex(checkedge, checkvertex);
+ }
+ while (farrightapex[0] > farrightpt[0]) {
+ lprevself(*farright);
+ symself(*farright);
+ farrightpt = farrightapex;
+ apex(*farright, farrightapex);
+ }
+ }
+ return;
+ }
+ /* Consider eliminating edges from the left triangulation. */
+ if (!leftfinished) {
+ /* What vertex would be exposed if an edge were deleted? */
+ lprev(leftcand, nextedge);
+ symself(nextedge);
+ apex(nextedge, nextapex);
+ /* If nextapex is NULL, then no vertex would be exposed; the */
+ /* triangulation would have been eaten right through. */
+ if (nextapex != (point) NULL) {
+ /* Check whether the edge is Delaunay. */
+ badedge = incircle(lowerleft, lowerright, upperleft, nextapex) > 0.0;
+ while (badedge) {
+ /* Eliminate the edge with an edge flip. As a result, the */
+ /* left triangulation will have one more boundary triangle. */
+ lnextself(nextedge);
+ sym(nextedge, topcasing);
+ lnextself(nextedge);
+ sym(nextedge, sidecasing);
+ bond(nextedge, topcasing);
+ bond(leftcand, sidecasing);
+ lnextself(leftcand);
+ sym(leftcand, outercasing);
+ lprevself(nextedge);
+ bond(nextedge, outercasing);
+ /* Correct the vertices to reflect the edge flip. */
+ setorg(leftcand, lowerleft);
+ setdest(leftcand, NULL);
+ setapex(leftcand, nextapex);
+ setorg(nextedge, NULL);
+ setdest(nextedge, upperleft);
+ setapex(nextedge, nextapex);
+ /* Consider the newly exposed vertex. */
+ upperleft = nextapex;
+ /* What vertex would be exposed if another edge were deleted? */
+ triedgecopy(sidecasing, nextedge);
+ apex(nextedge, nextapex);
+ if (nextapex != (point) NULL) {
+ /* Check whether the edge is Delaunay. */
+ badedge = incircle(lowerleft, lowerright, upperleft, nextapex)
+ > 0.0;
+ } else {
+ /* Avoid eating right through the triangulation. */
+ badedge = 0;
+ }
+ }
+ }
+ }
+ /* Consider eliminating edges from the right triangulation. */
+ if (!rightfinished) {
+ /* What vertex would be exposed if an edge were deleted? */
+ lnext(rightcand, nextedge);
+ symself(nextedge);
+ apex(nextedge, nextapex);
+ /* If nextapex is NULL, then no vertex would be exposed; the */
+ /* triangulation would have been eaten right through. */
+ if (nextapex != (point) NULL) {
+ /* Check whether the edge is Delaunay. */
+ badedge = incircle(lowerleft, lowerright, upperright, nextapex) > 0.0;
+ while (badedge) {
+ /* Eliminate the edge with an edge flip. As a result, the */
+ /* right triangulation will have one more boundary triangle. */
+ lprevself(nextedge);
+ sym(nextedge, topcasing);
+ lprevself(nextedge);
+ sym(nextedge, sidecasing);
+ bond(nextedge, topcasing);
+ bond(rightcand, sidecasing);
+ lprevself(rightcand);
+ sym(rightcand, outercasing);
+ lnextself(nextedge);
+ bond(nextedge, outercasing);
+ /* Correct the vertices to reflect the edge flip. */
+ setorg(rightcand, NULL);
+ setdest(rightcand, lowerright);
+ setapex(rightcand, nextapex);
+ setorg(nextedge, upperright);
+ setdest(nextedge, NULL);
+ setapex(nextedge, nextapex);
+ /* Consider the newly exposed vertex. */
+ upperright = nextapex;
+ /* What vertex would be exposed if another edge were deleted? */
+ triedgecopy(sidecasing, nextedge);
+ apex(nextedge, nextapex);
+ if (nextapex != (point) NULL) {
+ /* Check whether the edge is Delaunay. */
+ badedge = incircle(lowerleft, lowerright, upperright, nextapex)
+ > 0.0;
+ } else {
+ /* Avoid eating right through the triangulation. */
+ badedge = 0;
+ }
+ }
+ }
+ }
+ if (leftfinished || (!rightfinished &&
+ (incircle(upperleft, lowerleft, lowerright, upperright) > 0.0))) {
+ /* Knit the triangulations, adding an edge from `lowerleft' */
+ /* to `upperright'. */
+ bond(baseedge, rightcand);
+ lprev(rightcand, baseedge);
+ setdest(baseedge, lowerleft);
+ lowerright = upperright;
+ sym(baseedge, rightcand);
+ apex(rightcand, upperright);
+ } else {
+ /* Knit the triangulations, adding an edge from `upperleft' */
+ /* to `lowerright'. */
+ bond(baseedge, leftcand);
+ lnext(leftcand, baseedge);
+ setorg(baseedge, lowerright);
+ lowerleft = upperleft;
+ sym(baseedge, leftcand);
+ apex(leftcand, upperleft);
+ }
+ if (verbose > 2) {
+ printf(" Connecting ");
+ printtriangle(&baseedge);
+ }
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* divconqrecurse() Recursively form a Delaunay triangulation by the */
+/* divide-and-conquer method. */
+/* */
+/* Recursively breaks down the problem into smaller pieces, which are */
+/* knitted together by mergehulls(). The base cases (problems of two or */
+/* three points) are handled specially here. */
+/* */
+/* On completion, `farleft' and `farright' are bounding triangles such that */
+/* the origin of `farleft' is the leftmost vertex (breaking ties by */
+/* choosing the highest leftmost vertex), and the destination of */
+/* `farright' is the rightmost vertex (breaking ties by choosing the */
+/* lowest rightmost vertex). */
+/* */
+/*****************************************************************************/
+
+void divconqrecurse(sortarray, vertices, axis, farleft, farright)
+point *sortarray;
+int vertices;
+int axis;
+struct triedge *farleft;
+struct triedge *farright;
+{
+ struct triedge midtri, tri1, tri2, tri3;
+ struct triedge innerleft, innerright;
+ REAL area;
+ int divider;
+
+ if (verbose > 2) {
+ printf(" Triangulating %d points.\n", vertices);
+ }
+ if (vertices == 2) {
+ /* The triangulation of two vertices is an edge. An edge is */
+ /* represented by two bounding triangles. */
+ maketriangle(farleft);
+ setorg(*farleft, sortarray[0]);
+ setdest(*farleft, sortarray[1]);
+ /* The apex is intentionally left NULL. */
+ maketriangle(farright);
+ setorg(*farright, sortarray[1]);
+ setdest(*farright, sortarray[0]);
+ /* The apex is intentionally left NULL. */
+ bond(*farleft, *farright);
+ lprevself(*farleft);
+ lnextself(*farright);
+ bond(*farleft, *farright);
+ lprevself(*farleft);
+ lnextself(*farright);
+ bond(*farleft, *farright);
+ if (verbose > 2) {
+ printf(" Creating ");
+ printtriangle(farleft);
+ printf(" Creating ");
+ printtriangle(farright);
+ }
+ /* Ensure that the origin of `farleft' is sortarray[0]. */
+ lprev(*farright, *farleft);
+ return;
+ } else if (vertices == 3) {
+ /* The triangulation of three vertices is either a triangle (with */
+ /* three bounding triangles) or two edges (with four bounding */
+ /* triangles). In either case, four triangles are created. */
+ maketriangle(&midtri);
+ maketriangle(&tri1);
+ maketriangle(&tri2);
+ maketriangle(&tri3);
+ area = counterclockwise(sortarray[0], sortarray[1], sortarray[2]);
+ if (area == 0.0) {
+ /* Three collinear points; the triangulation is two edges. */
+ setorg(midtri, sortarray[0]);
+ setdest(midtri, sortarray[1]);
+ setorg(tri1, sortarray[1]);
+ setdest(tri1, sortarray[0]);
+ setorg(tri2, sortarray[2]);
+ setdest(tri2, sortarray[1]);
+ setorg(tri3, sortarray[1]);
+ setdest(tri3, sortarray[2]);
+ /* All apices are intentionally left NULL. */
+ bond(midtri, tri1);
+ bond(tri2, tri3);
+ lnextself(midtri);
+ lprevself(tri1);
+ lnextself(tri2);
+ lprevself(tri3);
+ bond(midtri, tri3);
+ bond(tri1, tri2);
+ lnextself(midtri);
+ lprevself(tri1);
+ lnextself(tri2);
+ lprevself(tri3);
+ bond(midtri, tri1);
+ bond(tri2, tri3);
+ /* Ensure that the origin of `farleft' is sortarray[0]. */
+ triedgecopy(tri1, *farleft);
+ /* Ensure that the destination of `farright' is sortarray[2]. */
+ triedgecopy(tri2, *farright);
+ } else {
+ /* The three points are not collinear; the triangulation is one */
+ /* triangle, namely `midtri'. */
+ setorg(midtri, sortarray[0]);
+ setdest(tri1, sortarray[0]);
+ setorg(tri3, sortarray[0]);
+ /* Apices of tri1, tri2, and tri3 are left NULL. */
+ if (area > 0.0) {
+ /* The vertices are in counterclockwise order. */
+ setdest(midtri, sortarray[1]);
+ setorg(tri1, sortarray[1]);
+ setdest(tri2, sortarray[1]);
+ setapex(midtri, sortarray[2]);
+ setorg(tri2, sortarray[2]);
+ setdest(tri3, sortarray[2]);
+ } else {
+ /* The vertices are in clockwise order. */
+ setdest(midtri, sortarray[2]);
+ setorg(tri1, sortarray[2]);
+ setdest(tri2, sortarray[2]);
+ setapex(midtri, sortarray[1]);
+ setorg(tri2, sortarray[1]);
+ setdest(tri3, sortarray[1]);
+ }
+ /* The topology does not depend on how the vertices are ordered. */
+ bond(midtri, tri1);
+ lnextself(midtri);
+ bond(midtri, tri2);
+ lnextself(midtri);
+ bond(midtri, tri3);
+ lprevself(tri1);
+ lnextself(tri2);
+ bond(tri1, tri2);
+ lprevself(tri1);
+ lprevself(tri3);
+ bond(tri1, tri3);
+ lnextself(tri2);
+ lprevself(tri3);
+ bond(tri2, tri3);
+ /* Ensure that the origin of `farleft' is sortarray[0]. */
+ triedgecopy(tri1, *farleft);
+ /* Ensure that the destination of `farright' is sortarray[2]. */
+ if (area > 0.0) {
+ triedgecopy(tri2, *farright);
+ } else {
+ lnext(*farleft, *farright);
+ }
+ }
+ if (verbose > 2) {
+ printf(" Creating ");
+ printtriangle(&midtri);
+ printf(" Creating ");
+ printtriangle(&tri1);
+ printf(" Creating ");
+ printtriangle(&tri2);
+ printf(" Creating ");
+ printtriangle(&tri3);
+ }
+ return;
+ } else {
+ /* Split the vertices in half. */
+ divider = vertices >> 1;
+ /* Recursively triangulate each half. */
+ divconqrecurse(sortarray, divider, 1 - axis, farleft, &innerleft);
+ divconqrecurse(&sortarray[divider], vertices - divider, 1 - axis,
+ &innerright, farright);
+ if (verbose > 1) {
+ printf(" Joining triangulations with %d and %d vertices.\n", divider,
+ vertices - divider);
+ }
+ /* Merge the two triangulations into one. */
+ mergehulls(farleft, &innerleft, &innerright, farright, axis);
+ }
+}
+
+long removeghosts(startghost)
+struct triedge *startghost;
+{
+ struct triedge searchedge;
+ struct triedge dissolveedge;
+ struct triedge deadtri;
+ point markorg;
+ long hullsize;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ if (verbose) {
+ printf(" Removing ghost triangles.\n");
+ }
+ /* Find an edge on the convex hull to start point location from. */
+ lprev(*startghost, searchedge);
+ symself(searchedge);
+ dummytri[0] = encode(searchedge);
+ /* Remove the bounding box and count the convex hull edges. */
+ triedgecopy(*startghost, dissolveedge);
+ hullsize = 0;
+ do {
+ hullsize++;
+ lnext(dissolveedge, deadtri);
+ lprevself(dissolveedge);
+ symself(dissolveedge);
+ /* If no PSLG is involved, set the boundary markers of all the points */
+ /* on the convex hull. If a PSLG is used, this step is done later. */
+ if (!poly) {
+ /* Watch out for the case where all the input points are collinear. */
+ if (dissolveedge.tri != dummytri) {
+ org(dissolveedge, markorg);
+ if (pointmark(markorg) == 0) {
+ setpointmark(markorg, 1);
+ }
+ }
+ }
+ /* Remove a bounding triangle from a convex hull triangle. */
+ dissolve(dissolveedge);
+ /* Find the next bounding triangle. */
+ sym(deadtri, dissolveedge);
+ /* Delete the bounding triangle. */
+ triangledealloc(deadtri.tri);
+ } while (!triedgeequal(dissolveedge, *startghost));
+ return hullsize;
+}
+
+/*****************************************************************************/
+/* */
+/* divconqdelaunay() Form a Delaunay triangulation by the divide-and- */
+/* conquer method. */
+/* */
+/* Sorts the points, calls a recursive procedure to triangulate them, and */
+/* removes the bounding box, setting boundary markers as appropriate. */
+/* */
+/*****************************************************************************/
+
+long divconqdelaunay()
+{
+ point *sortarray;
+ struct triedge hullleft, hullright;
+ int divider;
+ int i, j;
+
+ /* Allocate an array of pointers to points for sorting. */
+ sortarray = (point *) malloc(inpoints * sizeof(point));
+ if (sortarray == (point *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ traversalinit(&points);
+ for (i = 0; i < inpoints; i++) {
+ sortarray[i] = pointtraverse();
+ }
+ if (verbose) {
+ printf(" Sorting points.\n");
+ }
+ /* Sort the points. */
+ pointsort(sortarray, inpoints);
+ /* Discard duplicate points, which can really mess up the algorithm. */
+ i = 0;
+ for (j = 1; j < inpoints; j++) {
+ if ((sortarray[i][0] == sortarray[j][0])
+ && (sortarray[i][1] == sortarray[j][1])) {
+ if (!quiet) {
+ printf(
+"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
+ sortarray[j][0], sortarray[j][1]);
+ }
+/* Commented out - would eliminate point from output .node file, but causes
+ a failure if some segment has this point as an endpoint.
+ setpointmark(sortarray[j], DEADPOINT);
+*/
+ } else {
+ i++;
+ sortarray[i] = sortarray[j];
+ }
+ }
+ i++;
+ if (dwyer) {
+ /* Re-sort the array of points to accommodate alternating cuts. */
+ divider = i >> 1;
+ if (i - divider >= 2) {
+ if (divider >= 2) {
+ alternateaxes(sortarray, divider, 1);
+ }
+ alternateaxes(&sortarray[divider], i - divider, 1);
+ }
+ }
+ if (verbose) {
+ printf(" Forming triangulation.\n");
+ }
+ /* Form the Delaunay triangulation. */
+ divconqrecurse(sortarray, i, 0, &hullleft, &hullright);
+ free(sortarray);
+
+ return removeghosts(&hullleft);
+}
+
+/** **/
+/** **/
+/********* Divide-and-conquer Delaunay triangulation ends here *********/
+
+/********* Incremental Delaunay triangulation begins here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* boundingbox() Form an "infinite" bounding triangle to insert points */
+/* into. */
+/* */
+/* The points at "infinity" are assigned finite coordinates, which are used */
+/* by the point location routines, but (mostly) ignored by the Delaunay */
+/* edge flip routines. */
+/* */
+/*****************************************************************************/
+
+#ifndef REDUCED
+
+void boundingbox()
+{
+ struct triedge inftri; /* Handle for the triangular bounding box. */
+ REAL width;
+
+ if (verbose) {
+ printf(" Creating triangular bounding box.\n");
+ }
+ /* Find the width (or height, whichever is larger) of the triangulation. */
+ width = xmax - xmin;
+ if (ymax - ymin > width) {
+ width = ymax - ymin;
+ }
+ if (width == 0.0) {
+ width = 1.0;
+ }
+ /* Create the vertices of the bounding box. */
+ infpoint1 = (point) malloc(points.itembytes);
+ infpoint2 = (point) malloc(points.itembytes);
+ infpoint3 = (point) malloc(points.itembytes);
+ if ((infpoint1 == (point) NULL) || (infpoint2 == (point) NULL)
+ || (infpoint3 == (point) NULL)) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ infpoint1[0] = xmin - 50.0 * width;
+ infpoint1[1] = ymin - 40.0 * width;
+ infpoint2[0] = xmax + 50.0 * width;
+ infpoint2[1] = ymin - 40.0 * width;
+ infpoint3[0] = 0.5 * (xmin + xmax);
+ infpoint3[1] = ymax + 60.0 * width;
+
+ /* Create the bounding box. */
+ maketriangle(&inftri);
+ setorg(inftri, infpoint1);
+ setdest(inftri, infpoint2);
+ setapex(inftri, infpoint3);
+ /* Link dummytri to the bounding box so we can always find an */
+ /* edge to begin searching (point location) from. */
+ dummytri[0] = (triangle) inftri.tri;
+ if (verbose > 2) {
+ printf(" Creating ");
+ printtriangle(&inftri);
+ }
+}
+
+#endif /* not REDUCED */
+
+/*****************************************************************************/
+/* */
+/* removebox() Remove the "infinite" bounding triangle, setting boundary */
+/* markers as appropriate. */
+/* */
+/* The triangular bounding box has three boundary triangles (one for each */
+/* side of the bounding box), and a bunch of triangles fanning out from */
+/* the three bounding box vertices (one triangle for each edge of the */
+/* convex hull of the inner mesh). This routine removes these triangles. */
+/* */
+/*****************************************************************************/
+
+#ifndef REDUCED
+
+long removebox()
+{
+ struct triedge deadtri;
+ struct triedge searchedge;
+ struct triedge checkedge;
+ struct triedge nextedge, finaledge, dissolveedge;
+ point markorg;
+ long hullsize;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ if (verbose) {
+ printf(" Removing triangular bounding box.\n");
+ }
+ /* Find a boundary triangle. */
+ nextedge.tri = dummytri;
+ nextedge.orient = 0;
+ symself(nextedge);
+ /* Mark a place to stop. */
+ lprev(nextedge, finaledge);
+ lnextself(nextedge);
+ symself(nextedge);
+ /* Find a triangle (on the boundary of the point set) that isn't */
+ /* a bounding box triangle. */
+ lprev(nextedge, searchedge);
+ symself(searchedge);
+ /* Check whether nextedge is another boundary triangle */
+ /* adjacent to the first one. */
+ lnext(nextedge, checkedge);
+ symself(checkedge);
+ if (checkedge.tri == dummytri) {
+ /* Go on to the next triangle. There are only three boundary */
+ /* triangles, and this next triangle cannot be the third one, */
+ /* so it's safe to stop here. */
+ lprevself(searchedge);
+ symself(searchedge);
+ }
+ /* Find a new boundary edge to search from, as the current search */
+ /* edge lies on a bounding box triangle and will be deleted. */
+ dummytri[0] = encode(searchedge);
+ hullsize = -2l;
+ while (!triedgeequal(nextedge, finaledge)) {
+ hullsize++;
+ lprev(nextedge, dissolveedge);
+ symself(dissolveedge);
+ /* If not using a PSLG, the vertices should be marked now. */
+ /* (If using a PSLG, markhull() will do the job.) */
+ if (!poly) {
+ /* Be careful! One must check for the case where all the input */
+ /* points are collinear, and thus all the triangles are part of */
+ /* the bounding box. Otherwise, the setpointmark() call below */
+ /* will cause a bad pointer reference. */
+ if (dissolveedge.tri != dummytri) {
+ org(dissolveedge, markorg);
+ if (pointmark(markorg) == 0) {
+ setpointmark(markorg, 1);
+ }
+ }
+ }
+ /* Disconnect the bounding box triangle from the mesh triangle. */
+ dissolve(dissolveedge);
+ lnext(nextedge, deadtri);
+ sym(deadtri, nextedge);
+ /* Get rid of the bounding box triangle. */
+ triangledealloc(deadtri.tri);
+ /* Do we need to turn the corner? */
+ if (nextedge.tri == dummytri) {
+ /* Turn the corner. */
+ triedgecopy(dissolveedge, nextedge);
+ }
+ }
+ triangledealloc(finaledge.tri);
+
+ free(infpoint1); /* Deallocate the bounding box vertices. */
+ free(infpoint2);
+ free(infpoint3);
+
+ return hullsize;
+}
+
+#endif /* not REDUCED */
+
+/*****************************************************************************/
+/* */
+/* incrementaldelaunay() Form a Delaunay triangulation by incrementally */
+/* adding vertices. */
+/* */
+/*****************************************************************************/
+
+#ifndef REDUCED
+
+long incrementaldelaunay()
+{
+ struct triedge starttri;
+ point pointloop;
+ int i;
+
+ /* Create a triangular bounding box. */
+ boundingbox();
+ if (verbose) {
+ printf(" Incrementally inserting points.\n");
+ }
+ traversalinit(&points);
+ pointloop = pointtraverse();
+ i = 1;
+ while (pointloop != (point) NULL) {
+ /* Find a boundary triangle to search from. */
+ starttri.tri = (triangle *) NULL;
+ if (insertsite(pointloop, &starttri, (struct edge *) NULL, 0, 0) ==
+ DUPLICATEPOINT) {
+ if (!quiet) {
+ printf(
+"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
+ pointloop[0], pointloop[1]);
+ }
+/* Commented out - would eliminate point from output .node file.
+ setpointmark(pointloop, DEADPOINT);
+*/
+ }
+ pointloop = pointtraverse();
+ i++;
+ }
+ /* Remove the bounding box. */
+ return removebox();
+}
+
+#endif /* not REDUCED */
+
+/** **/
+/** **/
+/********* Incremental Delaunay triangulation ends here *********/
+
+/********* Sweepline Delaunay triangulation begins here *********/
+/** **/
+/** **/
+
+#ifndef REDUCED
+
+void eventheapinsert(heap, heapsize, newevent)
+struct event **heap;
+int heapsize;
+struct event *newevent;
+{
+ REAL eventx, eventy;
+ int eventnum;
+ int parent;
+ int notdone;
+
+ eventx = newevent->xkey;
+ eventy = newevent->ykey;
+ eventnum = heapsize;
+ notdone = eventnum > 0;
+ while (notdone) {
+ parent = (eventnum - 1) >> 1;
+ if ((heap[parent]->ykey < eventy) ||
+ ((heap[parent]->ykey == eventy)
+ && (heap[parent]->xkey <= eventx))) {
+ notdone = 0;
+ } else {
+ heap[eventnum] = heap[parent];
+ heap[eventnum]->heapposition = eventnum;
+
+ eventnum = parent;
+ notdone = eventnum > 0;
+ }
+ }
+ heap[eventnum] = newevent;
+ newevent->heapposition = eventnum;
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+void eventheapify(heap, heapsize, eventnum)
+struct event **heap;
+int heapsize;
+int eventnum;
+{
+ struct event *thisevent;
+ REAL eventx, eventy;
+ int leftchild, rightchild;
+ int smallest;
+ int notdone;
+
+ thisevent = heap[eventnum];
+ eventx = thisevent->xkey;
+ eventy = thisevent->ykey;
+ leftchild = 2 * eventnum + 1;
+ notdone = leftchild < heapsize;
+ while (notdone) {
+ if ((heap[leftchild]->ykey < eventy) ||
+ ((heap[leftchild]->ykey == eventy)
+ && (heap[leftchild]->xkey < eventx))) {
+ smallest = leftchild;
+ } else {
+ smallest = eventnum;
+ }
+ rightchild = leftchild + 1;
+ if (rightchild < heapsize) {
+ if ((heap[rightchild]->ykey < heap[smallest]->ykey) ||
+ ((heap[rightchild]->ykey == heap[smallest]->ykey)
+ && (heap[rightchild]->xkey < heap[smallest]->xkey))) {
+ smallest = rightchild;
+ }
+ }
+ if (smallest == eventnum) {
+ notdone = 0;
+ } else {
+ heap[eventnum] = heap[smallest];
+ heap[eventnum]->heapposition = eventnum;
+ heap[smallest] = thisevent;
+ thisevent->heapposition = smallest;
+
+ eventnum = smallest;
+ leftchild = 2 * eventnum + 1;
+ notdone = leftchild < heapsize;
+ }
+ }
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+void eventheapdelete(heap, heapsize, eventnum)
+struct event **heap;
+int heapsize;
+int eventnum;
+{
+ struct event *moveevent;
+ REAL eventx, eventy;
+ int parent;
+ int notdone;
+
+ moveevent = heap[heapsize - 1];
+ if (eventnum > 0) {
+ eventx = moveevent->xkey;
+ eventy = moveevent->ykey;
+ do {
+ parent = (eventnum - 1) >> 1;
+ if ((heap[parent]->ykey < eventy) ||
+ ((heap[parent]->ykey == eventy)
+ && (heap[parent]->xkey <= eventx))) {
+ notdone = 0;
+ } else {
+ heap[eventnum] = heap[parent];
+ heap[eventnum]->heapposition = eventnum;
+
+ eventnum = parent;
+ notdone = eventnum > 0;
+ }
+ } while (notdone);
+ }
+ heap[eventnum] = moveevent;
+ moveevent->heapposition = eventnum;
+ eventheapify(heap, heapsize - 1, eventnum);
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+void createeventheap(eventheap, events, freeevents)
+struct event ***eventheap;
+struct event **events;
+struct event **freeevents;
+{
+ point thispoint;
+ int maxevents;
+ int i;
+
+ maxevents = (3 * inpoints) / 2;
+ *eventheap = (struct event **) malloc(maxevents * sizeof(struct event *));
+ if (*eventheap == (struct event **) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ *events = (struct event *) malloc(maxevents * sizeof(struct event));
+ if (*events == (struct event *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ traversalinit(&points);
+ for (i = 0; i < inpoints; i++) {
+ thispoint = pointtraverse();
+ (*events)[i].eventptr = (VOID *) thispoint;
+ (*events)[i].xkey = thispoint[0];
+ (*events)[i].ykey = thispoint[1];
+ eventheapinsert(*eventheap, i, *events + i);
+ }
+ *freeevents = (struct event *) NULL;
+ for (i = maxevents - 1; i >= inpoints; i--) {
+ (*events)[i].eventptr = (VOID *) *freeevents;
+ *freeevents = *events + i;
+ }
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+int rightofhyperbola(fronttri, newsite)
+struct triedge *fronttri;
+point newsite;
+{
+ point leftpoint, rightpoint;
+ REAL dxa, dya, dxb, dyb;
+
+ hyperbolacount++;
+
+ dest(*fronttri, leftpoint);
+ apex(*fronttri, rightpoint);
+ if ((leftpoint[1] < rightpoint[1])
+ || ((leftpoint[1] == rightpoint[1]) && (leftpoint[0] < rightpoint[0]))) {
+ if (newsite[0] >= rightpoint[0]) {
+ return 1;
+ }
+ } else {
+ if (newsite[0] <= leftpoint[0]) {
+ return 0;
+ }
+ }
+ dxa = leftpoint[0] - newsite[0];
+ dya = leftpoint[1] - newsite[1];
+ dxb = rightpoint[0] - newsite[0];
+ dyb = rightpoint[1] - newsite[1];
+ return dya * (dxb * dxb + dyb * dyb) > dyb * (dxa * dxa + dya * dya);
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+REAL circletop(pa, pb, pc, ccwabc)
+point pa;
+point pb;
+point pc;
+REAL ccwabc;
+{
+ REAL xac, yac, xbc, ybc, xab, yab;
+ REAL aclen2, bclen2, ablen2;
+
+ circletopcount++;
+
+ xac = pa[0] - pc[0];
+ yac = pa[1] - pc[1];
+ xbc = pb[0] - pc[0];
+ ybc = pb[1] - pc[1];
+ xab = pa[0] - pb[0];
+ yab = pa[1] - pb[1];
+ aclen2 = xac * xac + yac * yac;
+ bclen2 = xbc * xbc + ybc * ybc;
+ ablen2 = xab * xab + yab * yab;
+ return pc[1] + (xac * bclen2 - xbc * aclen2 + sqrt(aclen2 * bclen2 * ablen2))
+ / (2.0 * ccwabc);
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+void check4deadevent(checktri, freeevents, eventheap, heapsize)
+struct triedge *checktri;
+struct event **freeevents;
+struct event **eventheap;
+int *heapsize;
+{
+ struct event *deadevent;
+ point eventpoint;
+ int eventnum;
+
+ org(*checktri, eventpoint);
+ if (eventpoint != (point) NULL) {
+ deadevent = (struct event *) eventpoint;
+ eventnum = deadevent->heapposition;
+ deadevent->eventptr = (VOID *) *freeevents;
+ *freeevents = deadevent;
+ eventheapdelete(eventheap, *heapsize, eventnum);
+ (*heapsize)--;
+ setorg(*checktri, NULL);
+ }
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+struct splaynode *splay(splaytree, searchpoint, searchtri)
+struct splaynode *splaytree;
+point searchpoint;
+struct triedge *searchtri;
+{
+ struct splaynode *child, *grandchild;
+ struct splaynode *lefttree, *righttree;
+ struct splaynode *leftright;
+ point checkpoint;
+ int rightofroot, rightofchild;
+
+ if (splaytree == (struct splaynode *) NULL) {
+ return (struct splaynode *) NULL;
+ }
+ dest(splaytree->keyedge, checkpoint);
+ if (checkpoint == splaytree->keydest) {
+ rightofroot = rightofhyperbola(&splaytree->keyedge, searchpoint);
+ if (rightofroot) {
+ triedgecopy(splaytree->keyedge, *searchtri);
+ child = splaytree->rchild;
+ } else {
+ child = splaytree->lchild;
+ }
+ if (child == (struct splaynode *) NULL) {
+ return splaytree;
+ }
+ dest(child->keyedge, checkpoint);
+ if (checkpoint != child->keydest) {
+ child = splay(child, searchpoint, searchtri);
+ if (child == (struct splaynode *) NULL) {
+ if (rightofroot) {
+ splaytree->rchild = (struct splaynode *) NULL;
+ } else {
+ splaytree->lchild = (struct splaynode *) NULL;
+ }
+ return splaytree;
+ }
+ }
+ rightofchild = rightofhyperbola(&child->keyedge, searchpoint);
+ if (rightofchild) {
+ triedgecopy(child->keyedge, *searchtri);
+ grandchild = splay(child->rchild, searchpoint, searchtri);
+ child->rchild = grandchild;
+ } else {
+ grandchild = splay(child->lchild, searchpoint, searchtri);
+ child->lchild = grandchild;
+ }
+ if (grandchild == (struct splaynode *) NULL) {
+ if (rightofroot) {
+ splaytree->rchild = child->lchild;
+ child->lchild = splaytree;
+ } else {
+ splaytree->lchild = child->rchild;
+ child->rchild = splaytree;
+ }
+ return child;
+ }
+ if (rightofchild) {
+ if (rightofroot) {
+ splaytree->rchild = child->lchild;
+ child->lchild = splaytree;
+ } else {
+ splaytree->lchild = grandchild->rchild;
+ grandchild->rchild = splaytree;
+ }
+ child->rchild = grandchild->lchild;
+ grandchild->lchild = child;
+ } else {
+ if (rightofroot) {
+ splaytree->rchild = grandchild->lchild;
+ grandchild->lchild = splaytree;
+ } else {
+ splaytree->lchild = child->rchild;
+ child->rchild = splaytree;
+ }
+ child->lchild = grandchild->rchild;
+ grandchild->rchild = child;
+ }
+ return grandchild;
+ } else {
+ lefttree = splay(splaytree->lchild, searchpoint, searchtri);
+ righttree = splay(splaytree->rchild, searchpoint, searchtri);
+
+ pooldealloc(&splaynodes, (VOID *) splaytree);
+ if (lefttree == (struct splaynode *) NULL) {
+ return righttree;
+ } else if (righttree == (struct splaynode *) NULL) {
+ return lefttree;
+ } else if (lefttree->rchild == (struct splaynode *) NULL) {
+ lefttree->rchild = righttree->lchild;
+ righttree->lchild = lefttree;
+ return righttree;
+ } else if (righttree->lchild == (struct splaynode *) NULL) {
+ righttree->lchild = lefttree->rchild;
+ lefttree->rchild = righttree;
+ return lefttree;
+ } else {
+/* printf("Holy Toledo!!!\n"); */
+ leftright = lefttree->rchild;
+ while (leftright->rchild != (struct splaynode *) NULL) {
+ leftright = leftright->rchild;
+ }
+ leftright->rchild = righttree;
+ return lefttree;
+ }
+ }
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+struct splaynode *splayinsert(splayroot, newkey, searchpoint)
+struct splaynode *splayroot;
+struct triedge *newkey;
+point searchpoint;
+{
+ struct splaynode *newsplaynode;
+
+ newsplaynode = (struct splaynode *) poolalloc(&splaynodes);
+ triedgecopy(*newkey, newsplaynode->keyedge);
+ dest(*newkey, newsplaynode->keydest);
+ if (splayroot == (struct splaynode *) NULL) {
+ newsplaynode->lchild = (struct splaynode *) NULL;
+ newsplaynode->rchild = (struct splaynode *) NULL;
+ } else if (rightofhyperbola(&splayroot->keyedge, searchpoint)) {
+ newsplaynode->lchild = splayroot;
+ newsplaynode->rchild = splayroot->rchild;
+ splayroot->rchild = (struct splaynode *) NULL;
+ } else {
+ newsplaynode->lchild = splayroot->lchild;
+ newsplaynode->rchild = splayroot;
+ splayroot->lchild = (struct splaynode *) NULL;
+ }
+ return newsplaynode;
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+struct splaynode *circletopinsert(splayroot, newkey, pa, pb, pc, topy)
+struct splaynode *splayroot;
+struct triedge *newkey;
+point pa;
+point pb;
+point pc;
+REAL topy;
+{
+ REAL ccwabc;
+ REAL xac, yac, xbc, ybc;
+ REAL aclen2, bclen2;
+ REAL searchpoint[2];
+ struct triedge dummytri;
+
+ ccwabc = counterclockwise(pa, pb, pc);
+ xac = pa[0] - pc[0];
+ yac = pa[1] - pc[1];
+ xbc = pb[0] - pc[0];
+ ybc = pb[1] - pc[1];
+ aclen2 = xac * xac + yac * yac;
+ bclen2 = xbc * xbc + ybc * ybc;
+ searchpoint[0] = pc[0] - (yac * bclen2 - ybc * aclen2) / (2.0 * ccwabc);
+ searchpoint[1] = topy;
+ return splayinsert(splay(splayroot, (point) searchpoint, &dummytri), newkey,
+ (point) searchpoint);
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+struct splaynode *frontlocate(splayroot, bottommost, searchpoint, searchtri,
+ farright)
+struct splaynode *splayroot;
+struct triedge *bottommost;
+point searchpoint;
+struct triedge *searchtri;
+int *farright;
+{
+ int farrightflag;
+ triangle ptr; /* Temporary variable used by onext(). */
+
+ triedgecopy(*bottommost, *searchtri);
+ splayroot = splay(splayroot, searchpoint, searchtri);
+
+ farrightflag = 0;
+ while (!farrightflag && rightofhyperbola(searchtri, searchpoint)) {
+ onextself(*searchtri);
+ farrightflag = triedgeequal(*searchtri, *bottommost);
+ }
+ *farright = farrightflag;
+ return splayroot;
+}
+
+#endif /* not REDUCED */
+
+#ifndef REDUCED
+
+long sweeplinedelaunay()
+{
+ struct event **eventheap;
+ struct event *events;
+ struct event *freeevents;
+ struct event *nextevent;
+ struct event *newevent;
+ struct splaynode *splayroot;
+ struct triedge bottommost;
+ struct triedge searchtri;
+ struct triedge fliptri;
+ struct triedge lefttri, righttri, farlefttri, farrighttri;
+ struct triedge inserttri;
+ point firstpoint, secondpoint;
+ point nextpoint, lastpoint;
+ point connectpoint;
+ point leftpoint, midpoint, rightpoint;
+ REAL lefttest, righttest;
+ int heapsize;
+ int check4events, farrightflag;
+ triangle ptr; /* Temporary variable used by sym(), onext(), and oprev(). */
+
+ poolinit(&splaynodes, sizeof(struct splaynode), SPLAYNODEPERBLOCK, POINTER,
+ 0);
+ splayroot = (struct splaynode *) NULL;
+
+ if (verbose) {
+ printf(" Placing points in event heap.\n");
+ }
+ createeventheap(&eventheap, &events, &freeevents);
+ heapsize = inpoints;
+
+ if (verbose) {
+ printf(" Forming triangulation.\n");
+ }
+ maketriangle(&lefttri);
+ maketriangle(&righttri);
+ bond(lefttri, righttri);
+ lnextself(lefttri);
+ lprevself(righttri);
+ bond(lefttri, righttri);
+ lnextself(lefttri);
+ lprevself(righttri);
+ bond(lefttri, righttri);
+ firstpoint = (point) eventheap[0]->eventptr;
+ eventheap[0]->eventptr = (VOID *) freeevents;
+ freeevents = eventheap[0];
+ eventheapdelete(eventheap, heapsize, 0);
+ heapsize--;
+ do {
+ if (heapsize == 0) {
+ printf("Error: Input points are all identical.\n");
+ exit(1);
+ }
+ secondpoint = (point) eventheap[0]->eventptr;
+ eventheap[0]->eventptr = (VOID *) freeevents;
+ freeevents = eventheap[0];
+ eventheapdelete(eventheap, heapsize, 0);
+ heapsize--;
+ if ((firstpoint[0] == secondpoint[0])
+ && (firstpoint[1] == secondpoint[1])) {
+ printf(
+"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
+ secondpoint[0], secondpoint[1]);
+/* Commented out - would eliminate point from output .node file.
+ setpointmark(secondpoint, DEADPOINT);
+*/
+ }
+ } while ((firstpoint[0] == secondpoint[0])
+ && (firstpoint[1] == secondpoint[1]));
+ setorg(lefttri, firstpoint);
+ setdest(lefttri, secondpoint);
+ setorg(righttri, secondpoint);
+ setdest(righttri, firstpoint);
+ lprev(lefttri, bottommost);
+ lastpoint = secondpoint;
+ while (heapsize > 0) {
+ nextevent = eventheap[0];
+ eventheapdelete(eventheap, heapsize, 0);
+ heapsize--;
+ check4events = 1;
+ if (nextevent->xkey < xmin) {
+ decode(nextevent->eventptr, fliptri);
+ oprev(fliptri, farlefttri);
+ check4deadevent(&farlefttri, &freeevents, eventheap, &heapsize);
+ onext(fliptri, farrighttri);
+ check4deadevent(&farrighttri, &freeevents, eventheap, &heapsize);
+
+ if (triedgeequal(farlefttri, bottommost)) {
+ lprev(fliptri, bottommost);
+ }
+ flip(&fliptri);
+ setapex(fliptri, NULL);
+ lprev(fliptri, lefttri);
+ lnext(fliptri, righttri);
+ sym(lefttri, farlefttri);
+
+ if (randomnation(SAMPLERATE) == 0) {
+ symself(fliptri);
+ dest(fliptri, leftpoint);
+ apex(fliptri, midpoint);
+ org(fliptri, rightpoint);
+ splayroot = circletopinsert(splayroot, &lefttri, leftpoint, midpoint,
+ rightpoint, nextevent->ykey);
+ }
+ } else {
+ nextpoint = (point) nextevent->eventptr;
+ if ((nextpoint[0] == lastpoint[0]) && (nextpoint[1] == lastpoint[1])) {
+ printf(
+"Warning: A duplicate point at (%.12g, %.12g) appeared and was ignored.\n",
+ nextpoint[0], nextpoint[1]);
+/* Commented out - would eliminate point from output .node file.
+ setpointmark(nextpoint, DEADPOINT);
+*/
+ check4events = 0;
+ } else {
+ lastpoint = nextpoint;
+
+ splayroot = frontlocate(splayroot, &bottommost, nextpoint, &searchtri,
+ &farrightflag);
+/*
+ triedgecopy(bottommost, searchtri);
+ farrightflag = 0;
+ while (!farrightflag && rightofhyperbola(&searchtri, nextpoint)) {
+ onextself(searchtri);
+ farrightflag = triedgeequal(searchtri, bottommost);
+ }
+*/
+
+ check4deadevent(&searchtri, &freeevents, eventheap, &heapsize);
+
+ triedgecopy(searchtri, farrighttri);
+ sym(searchtri, farlefttri);
+ maketriangle(&lefttri);
+ maketriangle(&righttri);
+ dest(farrighttri, connectpoint);
+ setorg(lefttri, connectpoint);
+ setdest(lefttri, nextpoint);
+ setorg(righttri, nextpoint);
+ setdest(righttri, connectpoint);
+ bond(lefttri, righttri);
+ lnextself(lefttri);
+ lprevself(righttri);
+ bond(lefttri, righttri);
+ lnextself(lefttri);
+ lprevself(righttri);
+ bond(lefttri, farlefttri);
+ bond(righttri, farrighttri);
+ if (!farrightflag && triedgeequal(farrighttri, bottommost)) {
+ triedgecopy(lefttri, bottommost);
+ }
+
+ if (randomnation(SAMPLERATE) == 0) {
+ splayroot = splayinsert(splayroot, &lefttri, nextpoint);
+ } else if (randomnation(SAMPLERATE) == 0) {
+ lnext(righttri, inserttri);
+ splayroot = splayinsert(splayroot, &inserttri, nextpoint);
+ }
+ }
+ }
+ nextevent->eventptr = (VOID *) freeevents;
+ freeevents = nextevent;
+
+ if (check4events) {
+ apex(farlefttri, leftpoint);
+ dest(lefttri, midpoint);
+ apex(lefttri, rightpoint);
+ lefttest = counterclockwise(leftpoint, midpoint, rightpoint);
+ if (lefttest > 0.0) {
+ newevent = freeevents;
+ freeevents = (struct event *) freeevents->eventptr;
+ newevent->xkey = xminextreme;
+ newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
+ lefttest);
+ newevent->eventptr = (VOID *) encode(lefttri);
+ eventheapinsert(eventheap, heapsize, newevent);
+ heapsize++;
+ setorg(lefttri, newevent);
+ }
+ apex(righttri, leftpoint);
+ org(righttri, midpoint);
+ apex(farrighttri, rightpoint);
+ righttest = counterclockwise(leftpoint, midpoint, rightpoint);
+ if (righttest > 0.0) {
+ newevent = freeevents;
+ freeevents = (struct event *) freeevents->eventptr;
+ newevent->xkey = xminextreme;
+ newevent->ykey = circletop(leftpoint, midpoint, rightpoint,
+ righttest);
+ newevent->eventptr = (VOID *) encode(farrighttri);
+ eventheapinsert(eventheap, heapsize, newevent);
+ heapsize++;
+ setorg(farrighttri, newevent);
+ }
+ }
+ }
+
+ pooldeinit(&splaynodes);
+ lprevself(bottommost);
+ return removeghosts(&bottommost);
+}
+
+#endif /* not REDUCED */
+
+/** **/
+/** **/
+/********* Sweepline Delaunay triangulation ends here *********/
+
+/********* General mesh construction routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* delaunay() Form a Delaunay triangulation. */
+/* */
+/*****************************************************************************/
+
+long delaunay()
+{
+ eextras = 0;
+ initializetrisegpools();
+
+#ifdef REDUCED
+ if (!quiet) {
+ printf(
+ "Constructing Delaunay triangulation by divide-and-conquer method.\n");
+ }
+ return divconqdelaunay();
+#else /* not REDUCED */
+ if (!quiet) {
+ printf("Constructing Delaunay triangulation ");
+ if (incremental) {
+ printf("by incremental method.\n");
+ } else if (sweepline) {
+ printf("by sweepline method.\n");
+ } else {
+ printf("by divide-and-conquer method.\n");
+ }
+ }
+ if (incremental) {
+ return incrementaldelaunay();
+ } else if (sweepline) {
+ return sweeplinedelaunay();
+ } else {
+ return divconqdelaunay();
+ }
+#endif /* not REDUCED */
+}
+
+/*****************************************************************************/
+/* */
+/* reconstruct() Reconstruct a triangulation from its .ele (and possibly */
+/* .poly) file. Used when the -r switch is used. */
+/* */
+/* Reads an .ele file and reconstructs the original mesh. If the -p switch */
+/* is used, this procedure will also read a .poly file and reconstruct the */
+/* shell edges of the original mesh. If the -a switch is used, this */
+/* procedure will also read an .area file and set a maximum area constraint */
+/* on each triangle. */
+/* */
+/* Points that are not corners of triangles, such as nodes on edges of */
+/* subparametric elements, are discarded. */
+/* */
+/* This routine finds the adjacencies between triangles (and shell edges) */
+/* by forming one stack of triangles for each vertex. Each triangle is on */
+/* three different stacks simultaneously. Each triangle's shell edge */
+/* pointers are used to link the items in each stack. This memory-saving */
+/* feature makes the code harder to read. The most important thing to keep */
+/* in mind is that each triangle is removed from a stack precisely when */
+/* the corresponding pointer is adjusted to refer to a shell edge rather */
+/* than the next triangle of the stack. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+#ifdef TRILIBRARY
+
+int reconstruct(trianglelist, triangleattriblist, trianglearealist, elements,
+ corners, attribs, segmentlist, segmentmarkerlist,
+ numberofsegments)
+int *trianglelist;
+REAL *triangleattriblist;
+REAL *trianglearealist;
+int elements;
+int corners;
+int attribs;
+int *segmentlist;
+int *segmentmarkerlist;
+int numberofsegments;
+
+#else /* not TRILIBRARY */
+
+long reconstruct(elefilename, areafilename, polyfilename, polyfile)
+char *elefilename;
+char *areafilename;
+char *polyfilename;
+FILE *polyfile;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ int pointindex;
+ int attribindex;
+#else /* not TRILIBRARY */
+ FILE *elefile;
+ FILE *areafile;
+ char inputline[INPUTLINESIZE];
+ char *stringptr;
+ int areaelements;
+#endif /* not TRILIBRARY */
+ struct triedge triangleloop;
+ struct triedge triangleleft;
+ struct triedge checktri;
+ struct triedge checkleft;
+ struct triedge checkneighbor;
+ struct edge shelleloop;
+ triangle *vertexarray;
+ triangle *prevlink;
+ triangle nexttri;
+ point tdest, tapex;
+ point checkdest, checkapex;
+ point shorg;
+ point killpoint;
+ REAL area;
+ int corner[3];
+ int end[2];
+ int killpointindex;
+ int incorners;
+ int segmentmarkers;
+ int boundmarker;
+ int aroundpoint;
+ long hullsize;
+ int notfound;
+ int elementnumber, segmentnumber;
+ int i, j;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+#ifdef TRILIBRARY
+ inelements = elements;
+ incorners = corners;
+ if (incorners < 3) {
+ printf("Error: Triangles must have at least 3 points.\n");
+ exit(1);
+ }
+ eextras = attribs;
+#else /* not TRILIBRARY */
+ /* Read the triangles from an .ele file. */
+ if (!quiet) {
+ printf("Opening %s.\n", elefilename);
+ }
+ elefile = fopen(elefilename, "r");
+ if (elefile == (FILE *) NULL) {
+ printf(" Error: Cannot access file %s.\n", elefilename);
+ exit(1);
+ }
+ /* Read number of triangles, number of points per triangle, and */
+ /* number of triangle attributes from .ele file. */
+ stringptr = readline(inputline, elefile, elefilename);
+ inelements = (int) strtol (stringptr, &stringptr, 0);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ incorners = 3;
+ } else {
+ incorners = (int) strtol (stringptr, &stringptr, 0);
+ if (incorners < 3) {
+ printf("Error: Triangles in %s must have at least 3 points.\n",
+ elefilename);
+ exit(1);
+ }
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ eextras = 0;
+ } else {
+ eextras = (int) strtol (stringptr, &stringptr, 0);
+ }
+#endif /* not TRILIBRARY */
+
+ initializetrisegpools();
+
+ /* Create the triangles. */
+ for (elementnumber = 1; elementnumber <= inelements; elementnumber++) {
+ maketriangle(&triangleloop);
+ /* Mark the triangle as living. */
+ triangleloop.tri[3] = (triangle) triangleloop.tri;
+ }
+
+ if (poly) {
+#ifdef TRILIBRARY
+ insegments = numberofsegments;
+ segmentmarkers = segmentmarkerlist != (int *) NULL;
+#else /* not TRILIBRARY */
+ /* Read number of segments and number of segment */
+ /* boundary markers from .poly file. */
+ stringptr = readline(inputline, polyfile, inpolyfilename);
+ insegments = (int) strtol (stringptr, &stringptr, 0);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ segmentmarkers = 0;
+ } else {
+ segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
+ }
+#endif /* not TRILIBRARY */
+
+ /* Create the shell edges. */
+ for (segmentnumber = 1; segmentnumber <= insegments; segmentnumber++) {
+ makeshelle(&shelleloop);
+ /* Mark the shell edge as living. */
+ shelleloop.sh[2] = (shelle) shelleloop.sh;
+ }
+ }
+
+#ifdef TRILIBRARY
+ pointindex = 0;
+ attribindex = 0;
+#else /* not TRILIBRARY */
+ if (vararea) {
+ /* Open an .area file, check for consistency with the .ele file. */
+ if (!quiet) {
+ printf("Opening %s.\n", areafilename);
+ }
+ areafile = fopen(areafilename, "r");
+ if (areafile == (FILE *) NULL) {
+ printf(" Error: Cannot access file %s.\n", areafilename);
+ exit(1);
+ }
+ stringptr = readline(inputline, areafile, areafilename);
+ areaelements = (int) strtol (stringptr, &stringptr, 0);
+ if (areaelements != inelements) {
+ printf("Error: %s and %s disagree on number of triangles.\n",
+ elefilename, areafilename);
+ exit(1);
+ }
+ }
+#endif /* not TRILIBRARY */
+
+ if (!quiet) {
+ printf("Reconstructing mesh.\n");
+ }
+ /* Allocate a temporary array that maps each point to some adjacent */
+ /* triangle. I took care to allocate all the permanent memory for */
+ /* triangles and shell edges first. */
+ vertexarray = (triangle *) malloc(points.items * sizeof(triangle));
+ if (vertexarray == (triangle *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ /* Each point is initially unrepresented. */
+ for (i = 0; i < points.items; i++) {
+ vertexarray[i] = (triangle) dummytri;
+ }
+
+ if (verbose) {
+ printf(" Assembling triangles.\n");
+ }
+ /* Read the triangles from the .ele file, and link */
+ /* together those that share an edge. */
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ elementnumber = firstnumber;
+ while (triangleloop.tri != (triangle *) NULL) {
+#ifdef TRILIBRARY
+ /* Copy the triangle's three corners. */
+ for (j = 0; j < 3; j++) {
+ corner[j] = trianglelist[pointindex++];
+ if ((corner[j] < firstnumber) || (corner[j] >= firstnumber + inpoints)) {
+ printf("Error: Triangle %d has an invalid vertex index.\n",
+ elementnumber);
+ exit(1);
+ }
+ }
+#else /* not TRILIBRARY */
+ /* Read triangle number and the triangle's three corners. */
+ stringptr = readline(inputline, elefile, elefilename);
+ for (j = 0; j < 3; j++) {
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Triangle %d is missing point %d in %s.\n",
+ elementnumber, j + 1, elefilename);
+ exit(1);
+ } else {
+ corner[j] = (int) strtol (stringptr, &stringptr, 0);
+ if ((corner[j] < firstnumber) ||
+ (corner[j] >= firstnumber + inpoints)) {
+ printf("Error: Triangle %d has an invalid vertex index.\n",
+ elementnumber);
+ exit(1);
+ }
+ }
+ }
+#endif /* not TRILIBRARY */
+
+ /* Find out about (and throw away) extra nodes. */
+ for (j = 3; j < incorners; j++) {
+#ifdef TRILIBRARY
+ killpointindex = trianglelist[pointindex++];
+#else /* not TRILIBRARY */
+ stringptr = findfield(stringptr);
+ if (*stringptr != '\0') {
+ killpointindex = (int) strtol (stringptr, &stringptr, 0);
+#endif /* not TRILIBRARY */
+ if ((killpointindex >= firstnumber) &&
+ (killpointindex < firstnumber + inpoints)) {
+ /* Delete the non-corner point if it's not already deleted. */
+ killpoint = getpoint(killpointindex);
+ if (pointmark(killpoint) != DEADPOINT) {
+ pointdealloc(killpoint);
+ }
+ }
+#ifndef TRILIBRARY
+ }
+#endif /* not TRILIBRARY */
+ }
+
+ /* Read the triangle's attributes. */
+ for (j = 0; j < eextras; j++) {
+#ifdef TRILIBRARY
+ setelemattribute(triangleloop, j, triangleattriblist[attribindex++]);
+#else /* not TRILIBRARY */
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ setelemattribute(triangleloop, j, 0);
+ } else {
+ setelemattribute(triangleloop, j,
+ (REAL) strtod (stringptr, &stringptr));
+ }
+#endif /* not TRILIBRARY */
+ }
+
+ if (vararea) {
+#ifdef TRILIBRARY
+ area = trianglearealist[elementnumber - firstnumber];
+#else /* not TRILIBRARY */
+ /* Read an area constraint from the .area file. */
+ stringptr = readline(inputline, areafile, areafilename);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ area = -1.0; /* No constraint on this triangle. */
+ } else {
+ area = (REAL) strtod(stringptr, &stringptr);
+ }
+#endif /* not TRILIBRARY */
+ setareabound(triangleloop, area);
+ }
+
+ /* Set the triangle's vertices. */
+ triangleloop.orient = 0;
+ setorg(triangleloop, getpoint(corner[0]));
+ setdest(triangleloop, getpoint(corner[1]));
+ setapex(triangleloop, getpoint(corner[2]));
+ /* Try linking the triangle to others that share these vertices. */
+ for (triangleloop.orient = 0; triangleloop.orient < 3;
+ triangleloop.orient++) {
+ /* Take the number for the origin of triangleloop. */
+ aroundpoint = corner[triangleloop.orient];
+ /* Look for other triangles having this vertex. */
+ nexttri = vertexarray[aroundpoint - firstnumber];
+ /* Link the current triangle to the next one in the stack. */
+ triangleloop.tri[6 + triangleloop.orient] = nexttri;
+ /* Push the current triangle onto the stack. */
+ vertexarray[aroundpoint - firstnumber] = encode(triangleloop);
+ decode(nexttri, checktri);
+ if (checktri.tri != dummytri) {
+ dest(triangleloop, tdest);
+ apex(triangleloop, tapex);
+ /* Look for other triangles that share an edge. */
+ do {
+ dest(checktri, checkdest);
+ apex(checktri, checkapex);
+ if (tapex == checkdest) {
+ /* The two triangles share an edge; bond them together. */
+ lprev(triangleloop, triangleleft);
+ bond(triangleleft, checktri);
+ }
+ if (tdest == checkapex) {
+ /* The two triangles share an edge; bond them together. */
+ lprev(checktri, checkleft);
+ bond(triangleloop, checkleft);
+ }
+ /* Find the next triangle in the stack. */
+ nexttri = checktri.tri[6 + checktri.orient];
+ decode(nexttri, checktri);
+ } while (checktri.tri != dummytri);
+ }
+ }
+ triangleloop.tri = triangletraverse();
+ elementnumber++;
+ }
+
+#ifdef TRILIBRARY
+ pointindex = 0;
+#else /* not TRILIBRARY */
+ fclose(elefile);
+ if (vararea) {
+ fclose(areafile);
+ }
+#endif /* not TRILIBRARY */
+
+ hullsize = 0; /* Prepare to count the boundary edges. */
+ if (poly) {
+ if (verbose) {
+ printf(" Marking segments in triangulation.\n");
+ }
+ /* Read the segments from the .poly file, and link them */
+ /* to their neighboring triangles. */
+ boundmarker = 0;
+ traversalinit(&shelles);
+ shelleloop.sh = shelletraverse();
+ segmentnumber = firstnumber;
+ while (shelleloop.sh != (shelle *) NULL) {
+#ifdef TRILIBRARY
+ end[0] = segmentlist[pointindex++];
+ end[1] = segmentlist[pointindex++];
+ if (segmentmarkers) {
+ boundmarker = segmentmarkerlist[segmentnumber - firstnumber];
+ }
+#else /* not TRILIBRARY */
+ /* Read the endpoints of each segment, and possibly a boundary marker. */
+ stringptr = readline(inputline, polyfile, inpolyfilename);
+ /* Skip the first (segment number) field. */
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Segment %d has no endpoints in %s.\n", segmentnumber,
+ polyfilename);
+ exit(1);
+ } else {
+ end[0] = (int) strtol (stringptr, &stringptr, 0);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Segment %d is missing its second endpoint in %s.\n",
+ segmentnumber, polyfilename);
+ exit(1);
+ } else {
+ end[1] = (int) strtol (stringptr, &stringptr, 0);
+ }
+ if (segmentmarkers) {
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ boundmarker = 0;
+ } else {
+ boundmarker = (int) strtol (stringptr, &stringptr, 0);
+ }
+ }
+#endif /* not TRILIBRARY */
+ for (j = 0; j < 2; j++) {
+ if ((end[j] < firstnumber) || (end[j] >= firstnumber + inpoints)) {
+ printf("Error: Segment %d has an invalid vertex index.\n",
+ segmentnumber);
+ exit(1);
+ }
+ }
+
+ /* set the shell edge's vertices. */
+ shelleloop.shorient = 0;
+ setsorg(shelleloop, getpoint(end[0]));
+ setsdest(shelleloop, getpoint(end[1]));
+ setmark(shelleloop, boundmarker);
+ /* Try linking the shell edge to triangles that share these vertices. */
+ for (shelleloop.shorient = 0; shelleloop.shorient < 2;
+ shelleloop.shorient++) {
+ /* Take the number for the destination of shelleloop. */
+ aroundpoint = end[1 - shelleloop.shorient];
+ /* Look for triangles having this vertex. */
+ prevlink = &vertexarray[aroundpoint - firstnumber];
+ nexttri = vertexarray[aroundpoint - firstnumber];
+ decode(nexttri, checktri);
+ sorg(shelleloop, shorg);
+ notfound = 1;
+ /* Look for triangles having this edge. Note that I'm only */
+ /* comparing each triangle's destination with the shell edge; */
+ /* each triangle's apex is handled through a different vertex. */
+ /* Because each triangle appears on three vertices' lists, each */
+ /* occurrence of a triangle on a list can (and does) represent */
+ /* an edge. In this way, most edges are represented twice, and */
+ /* every triangle-segment bond is represented once. */
+ while (notfound && (checktri.tri != dummytri)) {
+ dest(checktri, checkdest);
+ if (shorg == checkdest) {
+ /* We have a match. Remove this triangle from the list. */
+ *prevlink = checktri.tri[6 + checktri.orient];
+ /* Bond the shell edge to the triangle. */
+ tsbond(checktri, shelleloop);
+ /* Check if this is a boundary edge. */
+ sym(checktri, checkneighbor);
+ if (checkneighbor.tri == dummytri) {
+ /* The next line doesn't insert a shell edge (because there's */
+ /* already one there), but it sets the boundary markers of */
+ /* the existing shell edge and its vertices. */
+ insertshelle(&checktri, 1);
+ hullsize++;
+ }
+ notfound = 0;
+ }
+ /* Find the next triangle in the stack. */
+ prevlink = &checktri.tri[6 + checktri.orient];
+ nexttri = checktri.tri[6 + checktri.orient];
+ decode(nexttri, checktri);
+ }
+ }
+ shelleloop.sh = shelletraverse();
+ segmentnumber++;
+ }
+ }
+
+ /* Mark the remaining edges as not being attached to any shell edge. */
+ /* Also, count the (yet uncounted) boundary edges. */
+ for (i = 0; i < points.items; i++) {
+ /* Search the stack of triangles adjacent to a point. */
+ nexttri = vertexarray[i];
+ decode(nexttri, checktri);
+ while (checktri.tri != dummytri) {
+ /* Find the next triangle in the stack before this */
+ /* information gets overwritten. */
+ nexttri = checktri.tri[6 + checktri.orient];
+ /* No adjacent shell edge. (This overwrites the stack info.) */
+ tsdissolve(checktri);
+ sym(checktri, checkneighbor);
+ if (checkneighbor.tri == dummytri) {
+ insertshelle(&checktri, 1);
+ hullsize++;
+ }
+ decode(nexttri, checktri);
+ }
+ }
+
+ free(vertexarray);
+ return hullsize;
+}
+
+#endif /* not CDT_ONLY */
+
+/** **/
+/** **/
+/********* General mesh construction routines end here *********/
+
+/********* Segment (shell edge) insertion begins here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* finddirection() Find the first triangle on the path from one point */
+/* to another. */
+/* */
+/* Finds the triangle that intersects a line segment drawn from the */
+/* origin of `searchtri' to the point `endpoint', and returns the result */
+/* in `searchtri'. The origin of `searchtri' does not change, even though */
+/* the triangle returned may differ from the one passed in. This routine */
+/* is used to find the direction to move in to get from one point to */
+/* another. */
+/* */
+/* The return value notes whether the destination or apex of the found */
+/* triangle is collinear with the two points in question. */
+/* */
+/*****************************************************************************/
+
+enum finddirectionresult finddirection(searchtri, endpoint)
+struct triedge *searchtri;
+point endpoint;
+{
+ struct triedge checktri;
+ point startpoint;
+ point leftpoint, rightpoint;
+ REAL leftccw, rightccw;
+ int leftflag, rightflag;
+ triangle ptr; /* Temporary variable used by onext() and oprev(). */
+
+ org(*searchtri, startpoint);
+ dest(*searchtri, rightpoint);
+ apex(*searchtri, leftpoint);
+ /* Is `endpoint' to the left? */
+ leftccw = counterclockwise(endpoint, startpoint, leftpoint);
+ leftflag = leftccw > 0.0;
+ /* Is `endpoint' to the right? */
+ rightccw = counterclockwise(startpoint, endpoint, rightpoint);
+ rightflag = rightccw > 0.0;
+ if (leftflag && rightflag) {
+ /* `searchtri' faces directly away from `endpoint'. We could go */
+ /* left or right. Ask whether it's a triangle or a boundary */
+ /* on the left. */
+ onext(*searchtri, checktri);
+ if (checktri.tri == dummytri) {
+ leftflag = 0;
+ } else {
+ rightflag = 0;
+ }
+ }
+ while (leftflag) {
+ /* Turn left until satisfied. */
+ onextself(*searchtri);
+ if (searchtri->tri == dummytri) {
+ printf("Internal error in finddirection(): Unable to find a\n");
+ printf(" triangle leading from (%.12g, %.12g) to", startpoint[0],
+ startpoint[1]);
+ printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
+ internalerror();
+ }
+ apex(*searchtri, leftpoint);
+ rightccw = leftccw;
+ leftccw = counterclockwise(endpoint, startpoint, leftpoint);
+ leftflag = leftccw > 0.0;
+ }
+ while (rightflag) {
+ /* Turn right until satisfied. */
+ oprevself(*searchtri);
+ if (searchtri->tri == dummytri) {
+ printf("Internal error in finddirection(): Unable to find a\n");
+ printf(" triangle leading from (%.12g, %.12g) to", startpoint[0],
+ startpoint[1]);
+ printf(" (%.12g, %.12g).\n", endpoint[0], endpoint[1]);
+ internalerror();
+ }
+ dest(*searchtri, rightpoint);
+ leftccw = rightccw;
+ rightccw = counterclockwise(startpoint, endpoint, rightpoint);
+ rightflag = rightccw > 0.0;
+ }
+ if (leftccw == 0.0) {
+ return LEFTCOLLINEAR;
+ } else if (rightccw == 0.0) {
+ return RIGHTCOLLINEAR;
+ } else {
+ return WITHIN;
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* segmentintersection() Find the intersection of an existing segment */
+/* and a segment that is being inserted. Insert */
+/* a point at the intersection, splitting an */
+/* existing shell edge. */
+/* */
+/* The segment being inserted connects the apex of splittri to endpoint2. */
+/* splitshelle is the shell edge being split, and MUST be opposite */
+/* splittri. Hence, the edge being split connects the origin and */
+/* destination of splittri. */
+/* */
+/* On completion, splittri is a handle having the newly inserted */
+/* intersection point as its origin, and endpoint1 as its destination. */
+/* */
+/*****************************************************************************/
+
+void segmentintersection(splittri, splitshelle, endpoint2)
+struct triedge *splittri;
+struct edge *splitshelle;
+point endpoint2;
+{
+ point endpoint1;
+ point torg, tdest;
+ point leftpoint, rightpoint;
+ point newpoint;
+ enum insertsiteresult success;
+ enum finddirectionresult collinear;
+ REAL ex, ey;
+ REAL tx, ty;
+ REAL etx, ety;
+ REAL split, denom;
+ int i;
+ triangle ptr; /* Temporary variable used by onext(). */
+
+ /* Find the other three segment endpoints. */
+ apex(*splittri, endpoint1);
+ org(*splittri, torg);
+ dest(*splittri, tdest);
+ /* Segment intersection formulae; see the Antonio reference. */
+ tx = tdest[0] - torg[0];
+ ty = tdest[1] - torg[1];
+ ex = endpoint2[0] - endpoint1[0];
+ ey = endpoint2[1] - endpoint1[1];
+ etx = torg[0] - endpoint2[0];
+ ety = torg[1] - endpoint2[1];
+ denom = ty * ex - tx * ey;
+ if (denom == 0.0) {
+ printf("Internal error in segmentintersection():");
+ printf(" Attempt to find intersection of parallel segments.\n");
+ internalerror();
+ }
+ split = (ey * etx - ex * ety) / denom;
+ /* Create the new point. */
+ newpoint = (point) poolalloc(&points);
+ /* Interpolate its coordinate and attributes. */
+ for (i = 0; i < 2 + nextras; i++) {
+ newpoint[i] = torg[i] + split * (tdest[i] - torg[i]);
+ }
+ setpointmark(newpoint, mark(*splitshelle));
+ if (verbose > 1) {
+ printf(
+ " Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
+ torg[0], torg[1], tdest[0], tdest[1], newpoint[0], newpoint[1]);
+ }
+ /* Insert the intersection point. This should always succeed. */
+ success = insertsite(newpoint, splittri, splitshelle, 0, 0);
+ if (success != SUCCESSFULPOINT) {
+ printf("Internal error in segmentintersection():\n");
+ printf(" Failure to split a segment.\n");
+ internalerror();
+ }
+ if (steinerleft > 0) {
+ steinerleft--;
+ }
+ /* Inserting the point may have caused edge flips. We wish to rediscover */
+ /* the edge connecting endpoint1 to the new intersection point. */
+ collinear = finddirection(splittri, endpoint1);
+ dest(*splittri, rightpoint);
+ apex(*splittri, leftpoint);
+ if ((leftpoint[0] == endpoint1[0]) && (leftpoint[1] == endpoint1[1])) {
+ onextself(*splittri);
+ } else if ((rightpoint[0] != endpoint1[0]) ||
+ (rightpoint[1] != endpoint1[1])) {
+ printf("Internal error in segmentintersection():\n");
+ printf(" Topological inconsistency after splitting a segment.\n");
+ internalerror();
+ }
+ /* `splittri' should have destination endpoint1. */
+}
+
+/*****************************************************************************/
+/* */
+/* scoutsegment() Scout the first triangle on the path from one endpoint */
+/* to another, and check for completion (reaching the */
+/* second endpoint), a collinear point, and the */
+/* intersection of two segments. */
+/* */
+/* Returns one if the entire segment is successfully inserted, and zero if */
+/* the job must be finished by conformingedge() or constrainededge(). */
+/* */
+/* If the first triangle on the path has the second endpoint as its */
+/* destination or apex, a shell edge is inserted and the job is done. */
+/* */
+/* If the first triangle on the path has a destination or apex that lies on */
+/* the segment, a shell edge is inserted connecting the first endpoint to */
+/* the collinear point, and the search is continued from the collinear */
+/* point. */
+/* */
+/* If the first triangle on the path has a shell edge opposite its origin, */
+/* then there is a segment that intersects the segment being inserted. */
+/* Their intersection point is inserted, splitting the shell edge. */
+/* */
+/* Otherwise, return zero. */
+/* */
+/*****************************************************************************/
+
+int scoutsegment(searchtri, endpoint2, newmark)
+struct triedge *searchtri;
+point endpoint2;
+int newmark;
+{
+ struct triedge crosstri;
+ struct edge crossedge;
+ point leftpoint, rightpoint;
+ point endpoint1;
+ enum finddirectionresult collinear;
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ collinear = finddirection(searchtri, endpoint2);
+ dest(*searchtri, rightpoint);
+ apex(*searchtri, leftpoint);
+ if (((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) ||
+ ((rightpoint[0] == endpoint2[0]) && (rightpoint[1] == endpoint2[1]))) {
+ /* The segment is already an edge in the mesh. */
+ if ((leftpoint[0] == endpoint2[0]) && (leftpoint[1] == endpoint2[1])) {
+ lprevself(*searchtri);
+ }
+ /* Insert a shell edge, if there isn't already one there. */
+ insertshelle(searchtri, newmark);
+ return 1;
+ } else if (collinear == LEFTCOLLINEAR) {
+ /* We've collided with a point between the segment's endpoints. */
+ /* Make the collinear point be the triangle's origin. */
+ lprevself(*searchtri);
+ insertshelle(searchtri, newmark);
+ /* Insert the remainder of the segment. */
+ return scoutsegment(searchtri, endpoint2, newmark);
+ } else if (collinear == RIGHTCOLLINEAR) {
+ /* We've collided with a point between the segment's endpoints. */
+ insertshelle(searchtri, newmark);
+ /* Make the collinear point be the triangle's origin. */
+ lnextself(*searchtri);
+ /* Insert the remainder of the segment. */
+ return scoutsegment(searchtri, endpoint2, newmark);
+ } else {
+ lnext(*searchtri, crosstri);
+ tspivot(crosstri, crossedge);
+ /* Check for a crossing segment. */
+ if (crossedge.sh == dummysh) {
+ return 0;
+ } else {
+ org(*searchtri, endpoint1);
+ /* Insert a point at the intersection. */
+ segmentintersection(&crosstri, &crossedge, endpoint2);
+ triedgecopy(crosstri, *searchtri);
+ insertshelle(searchtri, newmark);
+ /* Insert the remainder of the segment. */
+ return scoutsegment(searchtri, endpoint2, newmark);
+ }
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* conformingedge() Force a segment into a conforming Delaunay */
+/* triangulation by inserting a point at its midpoint, */
+/* and recursively forcing in the two half-segments if */
+/* necessary. */
+/* */
+/* Generates a sequence of edges connecting `endpoint1' to `endpoint2'. */
+/* `newmark' is the boundary marker of the segment, assigned to each new */
+/* splitting point and shell edge. */
+/* */
+/* Note that conformingedge() does not always maintain the conforming */
+/* Delaunay property. Once inserted, segments are locked into place; */
+/* points inserted later (to force other segments in) may render these */
+/* fixed segments non-Delaunay. The conforming Delaunay property will be */
+/* restored by enforcequality() by splitting encroached segments. */
+/* */
+/*****************************************************************************/
+
+#ifndef REDUCED
+#ifndef CDT_ONLY
+
+void conformingedge(endpoint1, endpoint2, newmark)
+point endpoint1;
+point endpoint2;
+int newmark;
+{
+ struct triedge searchtri1, searchtri2;
+ struct edge brokenshelle;
+ point newpoint;
+ point midpoint1, midpoint2;
+ enum insertsiteresult success;
+ int result1, result2;
+ int i;
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ if (verbose > 2) {
+ printf("Forcing segment into triangulation by recursive splitting:\n");
+ printf(" (%.12g, %.12g) (%.12g, %.12g)\n", endpoint1[0], endpoint1[1],
+ endpoint2[0], endpoint2[1]);
+ }
+ /* Create a new point to insert in the middle of the segment. */
+ newpoint = (point) poolalloc(&points);
+ /* Interpolate coordinates and attributes. */
+ for (i = 0; i < 2 + nextras; i++) {
+ newpoint[i] = 0.5 * (endpoint1[i] + endpoint2[i]);
+ }
+ setpointmark(newpoint, newmark);
+ /* Find a boundary triangle to search from. */
+ searchtri1.tri = (triangle *) NULL;
+ /* Attempt to insert the new point. */
+ success = insertsite(newpoint, &searchtri1, (struct edge *) NULL, 0, 0);
+ if (success == DUPLICATEPOINT) {
+ if (verbose > 2) {
+ printf(" Segment intersects existing point (%.12g, %.12g).\n",
+ newpoint[0], newpoint[1]);
+ }
+ /* Use the point that's already there. */
+ pointdealloc(newpoint);
+ org(searchtri1, newpoint);
+ } else {
+ if (success == VIOLATINGPOINT) {
+ if (verbose > 2) {
+ printf(" Two segments intersect at (%.12g, %.12g).\n",
+ newpoint[0], newpoint[1]);
+ }
+ /* By fluke, we've landed right on another segment. Split it. */
+ tspivot(searchtri1, brokenshelle);
+ success = insertsite(newpoint, &searchtri1, &brokenshelle, 0, 0);
+ if (success != SUCCESSFULPOINT) {
+ printf("Internal error in conformingedge():\n");
+ printf(" Failure to split a segment.\n");
+ internalerror();
+ }
+ }
+ /* The point has been inserted successfully. */
+ if (steinerleft > 0) {
+ steinerleft--;
+ }
+ }
+ triedgecopy(searchtri1, searchtri2);
+ result1 = scoutsegment(&searchtri1, endpoint1, newmark);
+ result2 = scoutsegment(&searchtri2, endpoint2, newmark);
+ if (!result1) {
+ /* The origin of searchtri1 may have changed if a collision with an */
+ /* intervening vertex on the segment occurred. */
+ org(searchtri1, midpoint1);
+ conformingedge(midpoint1, endpoint1, newmark);
+ }
+ if (!result2) {
+ /* The origin of searchtri2 may have changed if a collision with an */
+ /* intervening vertex on the segment occurred. */
+ org(searchtri2, midpoint2);
+ conformingedge(midpoint2, endpoint2, newmark);
+ }
+}
+
+#endif /* not CDT_ONLY */
+#endif /* not REDUCED */
+
+/*****************************************************************************/
+/* */
+/* delaunayfixup() Enforce the Delaunay condition at an edge, fanning out */
+/* recursively from an existing point. Pay special */
+/* attention to stacking inverted triangles. */
+/* */
+/* This is a support routine for inserting segments into a constrained */
+/* Delaunay triangulation. */
+/* */
+/* The origin of fixuptri is treated as if it has just been inserted, and */
+/* the local Delaunay condition needs to be enforced. It is only enforced */
+/* in one sector, however, that being the angular range defined by */
+/* fixuptri. */
+/* */
+/* This routine also needs to make decisions regarding the "stacking" of */
+/* triangles. (Read the description of constrainededge() below before */
+/* reading on here, so you understand the algorithm.) If the position of */
+/* the new point (the origin of fixuptri) indicates that the vertex before */
+/* it on the polygon is a reflex vertex, then "stack" the triangle by */
+/* doing nothing. (fixuptri is an inverted triangle, which is how stacked */
+/* triangles are identified.) */
+/* */
+/* Otherwise, check whether the vertex before that was a reflex vertex. */
+/* If so, perform an edge flip, thereby eliminating an inverted triangle */
+/* (popping it off the stack). The edge flip may result in the creation */
+/* of a new inverted triangle, depending on whether or not the new vertex */
+/* is visible to the vertex three edges behind on the polygon. */
+/* */
+/* If neither of the two vertices behind the new vertex are reflex */
+/* vertices, fixuptri and fartri, the triangle opposite it, are not */
+/* inverted; hence, ensure that the edge between them is locally Delaunay. */
+/* */
+/* `leftside' indicates whether or not fixuptri is to the left of the */
+/* segment being inserted. (Imagine that the segment is pointing up from */
+/* endpoint1 to endpoint2.) */
+/* */
+/*****************************************************************************/
+
+void delaunayfixup(fixuptri, leftside)
+struct triedge *fixuptri;
+int leftside;
+{
+ struct triedge neartri;
+ struct triedge fartri;
+ struct edge faredge;
+ point nearpoint, leftpoint, rightpoint, farpoint;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ lnext(*fixuptri, neartri);
+ sym(neartri, fartri);
+ /* Check if the edge opposite the origin of fixuptri can be flipped. */
+ if (fartri.tri == dummytri) {
+ return;
+ }
+ tspivot(neartri, faredge);
+ if (faredge.sh != dummysh) {
+ return;
+ }
+ /* Find all the relevant vertices. */
+ apex(neartri, nearpoint);
+ org(neartri, leftpoint);
+ dest(neartri, rightpoint);
+ apex(fartri, farpoint);
+ /* Check whether the previous polygon vertex is a reflex vertex. */
+ if (leftside) {
+ if (counterclockwise(nearpoint, leftpoint, farpoint) <= 0.0) {
+ /* leftpoint is a reflex vertex too. Nothing can */
+ /* be done until a convex section is found. */
+ return;
+ }
+ } else {
+ if (counterclockwise(farpoint, rightpoint, nearpoint) <= 0.0) {
+ /* rightpoint is a reflex vertex too. Nothing can */
+ /* be done until a convex section is found. */
+ return;
+ }
+ }
+ if (counterclockwise(rightpoint, leftpoint, farpoint) > 0.0) {
+ /* fartri is not an inverted triangle, and farpoint is not a reflex */
+ /* vertex. As there are no reflex vertices, fixuptri isn't an */
+ /* inverted triangle, either. Hence, test the edge between the */
+ /* triangles to ensure it is locally Delaunay. */
+ if (incircle(leftpoint, farpoint, rightpoint, nearpoint) <= 0.0) {
+ return;
+ }
+ /* Not locally Delaunay; go on to an edge flip. */
+ } /* else fartri is inverted; remove it from the stack by flipping. */
+ flip(&neartri);
+ lprevself(*fixuptri); /* Restore the origin of fixuptri after the flip. */
+ /* Recursively process the two triangles that result from the flip. */
+ delaunayfixup(fixuptri, leftside);
+ delaunayfixup(&fartri, leftside);
+}
+
+/*****************************************************************************/
+/* */
+/* constrainededge() Force a segment into a constrained Delaunay */
+/* triangulation by deleting the triangles it */
+/* intersects, and triangulating the polygons that */
+/* form on each side of it. */
+/* */
+/* Generates a single edge connecting `endpoint1' to `endpoint2'. The */
+/* triangle `starttri' has `endpoint1' as its origin. `newmark' is the */
+/* boundary marker of the segment. */
+/* */
+/* To insert a segment, every triangle whose interior intersects the */
+/* segment is deleted. The union of these deleted triangles is a polygon */
+/* (which is not necessarily monotone, but is close enough), which is */
+/* divided into two polygons by the new segment. This routine's task is */
+/* to generate the Delaunay triangulation of these two polygons. */
+/* */
+/* You might think of this routine's behavior as a two-step process. The */
+/* first step is to walk from endpoint1 to endpoint2, flipping each edge */
+/* encountered. This step creates a fan of edges connected to endpoint1, */
+/* including the desired edge to endpoint2. The second step enforces the */
+/* Delaunay condition on each side of the segment in an incremental manner: */
+/* proceeding along the polygon from endpoint1 to endpoint2 (this is done */
+/* independently on each side of the segment), each vertex is "enforced" */
+/* as if it had just been inserted, but affecting only the previous */
+/* vertices. The result is the same as if the vertices had been inserted */
+/* in the order they appear on the polygon, so the result is Delaunay. */
+/* */
+/* In truth, constrainededge() interleaves these two steps. The procedure */
+/* walks from endpoint1 to endpoint2, and each time an edge is encountered */
+/* and flipped, the newly exposed vertex (at the far end of the flipped */
+/* edge) is "enforced" upon the previously flipped edges, usually affecting */
+/* only one side of the polygon (depending upon which side of the segment */
+/* the vertex falls on). */
+/* */
+/* The algorithm is complicated by the need to handle polygons that are not */
+/* convex. Although the polygon is not necessarily monotone, it can be */
+/* triangulated in a manner similar to the stack-based algorithms for */
+/* monotone polygons. For each reflex vertex (local concavity) of the */
+/* polygon, there will be an inverted triangle formed by one of the edge */
+/* flips. (An inverted triangle is one with negative area - that is, its */
+/* vertices are arranged in clockwise order - and is best thought of as a */
+/* wrinkle in the fabric of the mesh.) Each inverted triangle can be */
+/* thought of as a reflex vertex pushed on the stack, waiting to be fixed */
+/* later. */
+/* */
+/* A reflex vertex is popped from the stack when a vertex is inserted that */
+/* is visible to the reflex vertex. (However, if the vertex behind the */
+/* reflex vertex is not visible to the reflex vertex, a new inverted */
+/* triangle will take its place on the stack.) These details are handled */
+/* by the delaunayfixup() routine above. */
+/* */
+/*****************************************************************************/
+
+void constrainededge(starttri, endpoint2, newmark)
+struct triedge *starttri;
+point endpoint2;
+int newmark;
+{
+ struct triedge fixuptri, fixuptri2;
+ struct edge fixupedge;
+ point endpoint1;
+ point farpoint;
+ REAL area;
+ int collision;
+ int done;
+ triangle ptr; /* Temporary variable used by sym() and oprev(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ org(*starttri, endpoint1);
+ lnext(*starttri, fixuptri);
+ flip(&fixuptri);
+ /* `collision' indicates whether we have found a point directly */
+ /* between endpoint1 and endpoint2. */
+ collision = 0;
+ done = 0;
+ do {
+ org(fixuptri, farpoint);
+ /* `farpoint' is the extreme point of the polygon we are "digging" */
+ /* to get from endpoint1 to endpoint2. */
+ if ((farpoint[0] == endpoint2[0]) && (farpoint[1] == endpoint2[1])) {
+ oprev(fixuptri, fixuptri2);
+ /* Enforce the Delaunay condition around endpoint2. */
+ delaunayfixup(&fixuptri, 0);
+ delaunayfixup(&fixuptri2, 1);
+ done = 1;
+ } else {
+ /* Check whether farpoint is to the left or right of the segment */
+ /* being inserted, to decide which edge of fixuptri to dig */
+ /* through next. */
+ area = counterclockwise(endpoint1, endpoint2, farpoint);
+ if (area == 0.0) {
+ /* We've collided with a point between endpoint1 and endpoint2. */
+ collision = 1;
+ oprev(fixuptri, fixuptri2);
+ /* Enforce the Delaunay condition around farpoint. */
+ delaunayfixup(&fixuptri, 0);
+ delaunayfixup(&fixuptri2, 1);
+ done = 1;
+ } else {
+ if (area > 0.0) { /* farpoint is to the left of the segment. */
+ oprev(fixuptri, fixuptri2);
+ /* Enforce the Delaunay condition around farpoint, on the */
+ /* left side of the segment only. */
+ delaunayfixup(&fixuptri2, 1);
+ /* Flip the edge that crosses the segment. After the edge is */
+ /* flipped, one of its endpoints is the fan vertex, and the */
+ /* destination of fixuptri is the fan vertex. */
+ lprevself(fixuptri);
+ } else { /* farpoint is to the right of the segment. */
+ delaunayfixup(&fixuptri, 0);
+ /* Flip the edge that crosses the segment. After the edge is */
+ /* flipped, one of its endpoints is the fan vertex, and the */
+ /* destination of fixuptri is the fan vertex. */
+ oprevself(fixuptri);
+ }
+ /* Check for two intersecting segments. */
+ tspivot(fixuptri, fixupedge);
+ if (fixupedge.sh == dummysh) {
+ flip(&fixuptri); /* May create an inverted triangle on the left. */
+ } else {
+ /* We've collided with a segment between endpoint1 and endpoint2. */
+ collision = 1;
+ /* Insert a point at the intersection. */
+ segmentintersection(&fixuptri, &fixupedge, endpoint2);
+ done = 1;
+ }
+ }
+ }
+ } while (!done);
+ /* Insert a shell edge to make the segment permanent. */
+ insertshelle(&fixuptri, newmark);
+ /* If there was a collision with an interceding vertex, install another */
+ /* segment connecting that vertex with endpoint2. */
+ if (collision) {
+ /* Insert the remainder of the segment. */
+ if (!scoutsegment(&fixuptri, endpoint2, newmark)) {
+ constrainededge(&fixuptri, endpoint2, newmark);
+ }
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* insertsegment() Insert a PSLG segment into a triangulation. */
+/* */
+/*****************************************************************************/
+
+void insertsegment(endpoint1, endpoint2, newmark)
+point endpoint1;
+point endpoint2;
+int newmark;
+{
+ struct triedge searchtri1, searchtri2;
+ triangle encodedtri;
+ point checkpoint;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ if (verbose > 1) {
+ printf(" Connecting (%.12g, %.12g) to (%.12g, %.12g).\n",
+ endpoint1[0], endpoint1[1], endpoint2[0], endpoint2[1]);
+ }
+
+ /* Find a triangle whose origin is the segment's first endpoint. */
+ checkpoint = (point) NULL;
+ encodedtri = point2tri(endpoint1);
+ if (encodedtri != (triangle) NULL) {
+ decode(encodedtri, searchtri1);
+ org(searchtri1, checkpoint);
+ }
+ if (checkpoint != endpoint1) {
+ /* Find a boundary triangle to search from. */
+ searchtri1.tri = dummytri;
+ searchtri1.orient = 0;
+ symself(searchtri1);
+ /* Search for the segment's first endpoint by point location. */
+ if (locate(endpoint1, &searchtri1) != ONVERTEX) {
+ printf(
+ "Internal error in insertsegment(): Unable to locate PSLG point\n");
+ printf(" (%.12g, %.12g) in triangulation.\n",
+ endpoint1[0], endpoint1[1]);
+ internalerror();
+ }
+ }
+ /* Remember this triangle to improve subsequent point location. */
+ triedgecopy(searchtri1, recenttri);
+ /* Scout the beginnings of a path from the first endpoint */
+ /* toward the second. */
+ if (scoutsegment(&searchtri1, endpoint2, newmark)) {
+ /* The segment was easily inserted. */
+ return;
+ }
+ /* The first endpoint may have changed if a collision with an intervening */
+ /* vertex on the segment occurred. */
+ org(searchtri1, endpoint1);
+
+ /* Find a triangle whose origin is the segment's second endpoint. */
+ checkpoint = (point) NULL;
+ encodedtri = point2tri(endpoint2);
+ if (encodedtri != (triangle) NULL) {
+ decode(encodedtri, searchtri2);
+ org(searchtri2, checkpoint);
+ }
+ if (checkpoint != endpoint2) {
+ /* Find a boundary triangle to search from. */
+ searchtri2.tri = dummytri;
+ searchtri2.orient = 0;
+ symself(searchtri2);
+ /* Search for the segment's second endpoint by point location. */
+ if (locate(endpoint2, &searchtri2) != ONVERTEX) {
+ printf(
+ "Internal error in insertsegment(): Unable to locate PSLG point\n");
+ printf(" (%.12g, %.12g) in triangulation.\n",
+ endpoint2[0], endpoint2[1]);
+ internalerror();
+ }
+ }
+ /* Remember this triangle to improve subsequent point location. */
+ triedgecopy(searchtri2, recenttri);
+ /* Scout the beginnings of a path from the second endpoint */
+ /* toward the first. */
+ if (scoutsegment(&searchtri2, endpoint1, newmark)) {
+ /* The segment was easily inserted. */
+ return;
+ }
+ /* The second endpoint may have changed if a collision with an intervening */
+ /* vertex on the segment occurred. */
+ org(searchtri2, endpoint2);
+
+#ifndef REDUCED
+#ifndef CDT_ONLY
+ if (splitseg) {
+ /* Insert vertices to force the segment into the triangulation. */
+ conformingedge(endpoint1, endpoint2, newmark);
+ } else {
+#endif /* not CDT_ONLY */
+#endif /* not REDUCED */
+ /* Insert the segment directly into the triangulation. */
+ constrainededge(&searchtri1, endpoint2, newmark);
+#ifndef REDUCED
+#ifndef CDT_ONLY
+ }
+#endif /* not CDT_ONLY */
+#endif /* not REDUCED */
+}
+
+/*****************************************************************************/
+/* */
+/* markhull() Cover the convex hull of a triangulation with shell edges. */
+/* */
+/*****************************************************************************/
+
+void markhull()
+{
+ struct triedge hulltri;
+ struct triedge nexttri;
+ struct triedge starttri;
+ triangle ptr; /* Temporary variable used by sym() and oprev(). */
+
+ /* Find a triangle handle on the hull. */
+ hulltri.tri = dummytri;
+ hulltri.orient = 0;
+ symself(hulltri);
+ /* Remember where we started so we know when to stop. */
+ triedgecopy(hulltri, starttri);
+ /* Go once counterclockwise around the convex hull. */
+ do {
+ /* Create a shell edge if there isn't already one here. */
+ insertshelle(&hulltri, 1);
+ /* To find the next hull edge, go clockwise around the next vertex. */
+ lnextself(hulltri);
+ oprev(hulltri, nexttri);
+ while (nexttri.tri != dummytri) {
+ triedgecopy(nexttri, hulltri);
+ oprev(hulltri, nexttri);
+ }
+ } while (!triedgeequal(hulltri, starttri));
+}
+
+/*****************************************************************************/
+/* */
+/* formskeleton() Create the shell edges of a triangulation, including */
+/* PSLG edges and edges on the convex hull. */
+/* */
+/* The PSLG edges are read from a .poly file. The return value is the */
+/* number of segments in the file. */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+int formskeleton(segmentlist, segmentmarkerlist, numberofsegments)
+int *segmentlist;
+int *segmentmarkerlist;
+int numberofsegments;
+
+#else /* not TRILIBRARY */
+
+int formskeleton(polyfile, polyfilename)
+FILE *polyfile;
+char *polyfilename;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ char polyfilename[6];
+ int index;
+#else /* not TRILIBRARY */
+ char inputline[INPUTLINESIZE];
+ char *stringptr;
+#endif /* not TRILIBRARY */
+ point endpoint1, endpoint2;
+ int segments;
+ int segmentmarkers;
+ int end1, end2;
+ int boundmarker;
+ int i;
+
+ if (poly) {
+ if (!quiet) {
+ printf("Inserting segments into Delaunay triangulation.\n");
+ }
+#ifdef TRILIBRARY
+ strcpy(polyfilename, "input");
+ segments = numberofsegments;
+ segmentmarkers = segmentmarkerlist != (int *) NULL;
+ index = 0;
+#else /* not TRILIBRARY */
+ /* Read the segments from a .poly file. */
+ /* Read number of segments and number of boundary markers. */
+ stringptr = readline(inputline, polyfile, polyfilename);
+ segments = (int) strtol (stringptr, &stringptr, 0);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ segmentmarkers = 0;
+ } else {
+ segmentmarkers = (int) strtol (stringptr, &stringptr, 0);
+ }
+#endif /* not TRILIBRARY */
+ /* If segments are to be inserted, compute a mapping */
+ /* from points to triangles. */
+ if (segments > 0) {
+ if (verbose) {
+ printf(" Inserting PSLG segments.\n");
+ }
+ makepointmap();
+ }
+
+ boundmarker = 0;
+ /* Read and insert the segments. */
+ for (i = 1; i <= segments; i++) {
+#ifdef TRILIBRARY
+ end1 = segmentlist[index++];
+ end2 = segmentlist[index++];
+ if (segmentmarkers) {
+ boundmarker = segmentmarkerlist[i - 1];
+ }
+#else /* not TRILIBRARY */
+ stringptr = readline(inputline, polyfile, inpolyfilename);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Segment %d has no endpoints in %s.\n", i,
+ polyfilename);
+ exit(1);
+ } else {
+ end1 = (int) strtol (stringptr, &stringptr, 0);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Segment %d is missing its second endpoint in %s.\n", i,
+ polyfilename);
+ exit(1);
+ } else {
+ end2 = (int) strtol (stringptr, &stringptr, 0);
+ }
+ if (segmentmarkers) {
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ boundmarker = 0;
+ } else {
+ boundmarker = (int) strtol (stringptr, &stringptr, 0);
+ }
+ }
+#endif /* not TRILIBRARY */
+ if ((end1 < firstnumber) || (end1 >= firstnumber + inpoints)) {
+ if (!quiet) {
+ printf("Warning: Invalid first endpoint of segment %d in %s.\n", i,
+ polyfilename);
+ }
+ } else if ((end2 < firstnumber) || (end2 >= firstnumber + inpoints)) {
+ if (!quiet) {
+ printf("Warning: Invalid second endpoint of segment %d in %s.\n", i,
+ polyfilename);
+ }
+ } else {
+ endpoint1 = getpoint(end1);
+ endpoint2 = getpoint(end2);
+ if ((endpoint1[0] == endpoint2[0]) && (endpoint1[1] == endpoint2[1])) {
+ if (!quiet) {
+ printf("Warning: Endpoints of segment %d are coincident in %s.\n",
+ i, polyfilename);
+ }
+ } else {
+ insertsegment(endpoint1, endpoint2, boundmarker);
+ }
+ }
+ }
+ } else {
+ segments = 0;
+ }
+ if (convex || !poly) {
+ /* Enclose the convex hull with shell edges. */
+ if (verbose) {
+ printf(" Enclosing convex hull with segments.\n");
+ }
+ markhull();
+ }
+ return segments;
+}
+
+/** **/
+/** **/
+/********* Segment (shell edge) insertion ends here *********/
+
+/********* Carving out holes and concavities begins here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* infecthull() Virally infect all of the triangles of the convex hull */
+/* that are not protected by shell edges. Where there are */
+/* shell edges, set boundary markers as appropriate. */
+/* */
+/*****************************************************************************/
+
+void infecthull()
+{
+ struct triedge hulltri;
+ struct triedge nexttri;
+ struct triedge starttri;
+ struct edge hulledge;
+ triangle **deadtri;
+ point horg, hdest;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ if (verbose) {
+ printf(" Marking concavities (external triangles) for elimination.\n");
+ }
+ /* Find a triangle handle on the hull. */
+ hulltri.tri = dummytri;
+ hulltri.orient = 0;
+ symself(hulltri);
+ /* Remember where we started so we know when to stop. */
+ triedgecopy(hulltri, starttri);
+ /* Go once counterclockwise around the convex hull. */
+ do {
+ /* Ignore triangles that are already infected. */
+ if (!infected(hulltri)) {
+ /* Is the triangle protected by a shell edge? */
+ tspivot(hulltri, hulledge);
+ if (hulledge.sh == dummysh) {
+ /* The triangle is not protected; infect it. */
+ infect(hulltri);
+ deadtri = (triangle **) poolalloc(&viri);
+ *deadtri = hulltri.tri;
+ } else {
+ /* The triangle is protected; set boundary markers if appropriate. */
+ if (mark(hulledge) == 0) {
+ setmark(hulledge, 1);
+ org(hulltri, horg);
+ dest(hulltri, hdest);
+ if (pointmark(horg) == 0) {
+ setpointmark(horg, 1);
+ }
+ if (pointmark(hdest) == 0) {
+ setpointmark(hdest, 1);
+ }
+ }
+ }
+ }
+ /* To find the next hull edge, go clockwise around the next vertex. */
+ lnextself(hulltri);
+ oprev(hulltri, nexttri);
+ while (nexttri.tri != dummytri) {
+ triedgecopy(nexttri, hulltri);
+ oprev(hulltri, nexttri);
+ }
+ } while (!triedgeequal(hulltri, starttri));
+}
+
+/*****************************************************************************/
+/* */
+/* plague() Spread the virus from all infected triangles to any neighbors */
+/* not protected by shell edges. Delete all infected triangles. */
+/* */
+/* This is the procedure that actually creates holes and concavities. */
+/* */
+/* This procedure operates in two phases. The first phase identifies all */
+/* the triangles that will die, and marks them as infected. They are */
+/* marked to ensure that each triangle is added to the virus pool only */
+/* once, so the procedure will terminate. */
+/* */
+/* The second phase actually eliminates the infected triangles. It also */
+/* eliminates orphaned points. */
+/* */
+/*****************************************************************************/
+
+void plague()
+{
+ struct triedge testtri;
+ struct triedge neighbor;
+ triangle **virusloop;
+ triangle **deadtri;
+ struct edge neighborshelle;
+ point testpoint;
+ point norg, ndest;
+ point deadorg, deaddest, deadapex;
+ int killorg;
+ triangle ptr; /* Temporary variable used by sym() and onext(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ if (verbose) {
+ printf(" Marking neighbors of marked triangles.\n");
+ }
+ /* Loop through all the infected triangles, spreading the virus to */
+ /* their neighbors, then to their neighbors' neighbors. */
+ traversalinit(&viri);
+ virusloop = (triangle **) traverse(&viri);
+ while (virusloop != (triangle **) NULL) {
+ testtri.tri = *virusloop;
+ /* A triangle is marked as infected by messing with one of its shell */
+ /* edges, setting it to an illegal value. Hence, we have to */
+ /* temporarily uninfect this triangle so that we can examine its */
+ /* adjacent shell edges. */
+ uninfect(testtri);
+ if (verbose > 2) {
+ /* Assign the triangle an orientation for convenience in */
+ /* checking its points. */
+ testtri.orient = 0;
+ org(testtri, deadorg);
+ dest(testtri, deaddest);
+ apex(testtri, deadapex);
+ printf(" Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
+ deadorg[0], deadorg[1], deaddest[0], deaddest[1],
+ deadapex[0], deadapex[1]);
+ }
+ /* Check each of the triangle's three neighbors. */
+ for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
+ /* Find the neighbor. */
+ sym(testtri, neighbor);
+ /* Check for a shell between the triangle and its neighbor. */
+ tspivot(testtri, neighborshelle);
+ /* Check if the neighbor is nonexistent or already infected. */
+ if ((neighbor.tri == dummytri) || infected(neighbor)) {
+ if (neighborshelle.sh != dummysh) {
+ /* There is a shell edge separating the triangle from its */
+ /* neighbor, but both triangles are dying, so the shell */
+ /* edge dies too. */
+ shelledealloc(neighborshelle.sh);
+ if (neighbor.tri != dummytri) {
+ /* Make sure the shell edge doesn't get deallocated again */
+ /* later when the infected neighbor is visited. */
+ uninfect(neighbor);
+ tsdissolve(neighbor);
+ infect(neighbor);
+ }
+ }
+ } else { /* The neighbor exists and is not infected. */
+ if (neighborshelle.sh == dummysh) {
+ /* There is no shell edge protecting the neighbor, so */
+ /* the neighbor becomes infected. */
+ if (verbose > 2) {
+ org(neighbor, deadorg);
+ dest(neighbor, deaddest);
+ apex(neighbor, deadapex);
+ printf(
+ " Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
+ deadorg[0], deadorg[1], deaddest[0], deaddest[1],
+ deadapex[0], deadapex[1]);
+ }
+ infect(neighbor);
+ /* Ensure that the neighbor's neighbors will be infected. */
+ deadtri = (triangle **) poolalloc(&viri);
+ *deadtri = neighbor.tri;
+ } else { /* The neighbor is protected by a shell edge. */
+ /* Remove this triangle from the shell edge. */
+ stdissolve(neighborshelle);
+ /* The shell edge becomes a boundary. Set markers accordingly. */
+ if (mark(neighborshelle) == 0) {
+ setmark(neighborshelle, 1);
+ }
+ org(neighbor, norg);
+ dest(neighbor, ndest);
+ if (pointmark(norg) == 0) {
+ setpointmark(norg, 1);
+ }
+ if (pointmark(ndest) == 0) {
+ setpointmark(ndest, 1);
+ }
+ }
+ }
+ }
+ /* Remark the triangle as infected, so it doesn't get added to the */
+ /* virus pool again. */
+ infect(testtri);
+ virusloop = (triangle **) traverse(&viri);
+ }
+
+ if (verbose) {
+ printf(" Deleting marked triangles.\n");
+ }
+ traversalinit(&viri);
+ virusloop = (triangle **) traverse(&viri);
+ while (virusloop != (triangle **) NULL) {
+ testtri.tri = *virusloop;
+
+ /* Check each of the three corners of the triangle for elimination. */
+ /* This is done by walking around each point, checking if it is */
+ /* still connected to at least one live triangle. */
+ for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
+ org(testtri, testpoint);
+ /* Check if the point has already been tested. */
+ if (testpoint != (point) NULL) {
+ killorg = 1;
+ /* Mark the corner of the triangle as having been tested. */
+ setorg(testtri, NULL);
+ /* Walk counterclockwise about the point. */
+ onext(testtri, neighbor);
+ /* Stop upon reaching a boundary or the starting triangle. */
+ while ((neighbor.tri != dummytri)
+ && (!triedgeequal(neighbor, testtri))) {
+ if (infected(neighbor)) {
+ /* Mark the corner of this triangle as having been tested. */
+ setorg(neighbor, NULL);
+ } else {
+ /* A live triangle. The point survives. */
+ killorg = 0;
+ }
+ /* Walk counterclockwise about the point. */
+ onextself(neighbor);
+ }
+ /* If we reached a boundary, we must walk clockwise as well. */
+ if (neighbor.tri == dummytri) {
+ /* Walk clockwise about the point. */
+ oprev(testtri, neighbor);
+ /* Stop upon reaching a boundary. */
+ while (neighbor.tri != dummytri) {
+ if (infected(neighbor)) {
+ /* Mark the corner of this triangle as having been tested. */
+ setorg(neighbor, NULL);
+ } else {
+ /* A live triangle. The point survives. */
+ killorg = 0;
+ }
+ /* Walk clockwise about the point. */
+ oprevself(neighbor);
+ }
+ }
+ if (killorg) {
+ if (verbose > 1) {
+ printf(" Deleting point (%.12g, %.12g)\n",
+ testpoint[0], testpoint[1]);
+ }
+ pointdealloc(testpoint);
+ }
+ }
+ }
+
+ /* Record changes in the number of boundary edges, and disconnect */
+ /* dead triangles from their neighbors. */
+ for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
+ sym(testtri, neighbor);
+ if (neighbor.tri == dummytri) {
+ /* There is no neighboring triangle on this edge, so this edge */
+ /* is a boundary edge. This triangle is being deleted, so this */
+ /* boundary edge is deleted. */
+ hullsize--;
+ } else {
+ /* Disconnect the triangle from its neighbor. */
+ dissolve(neighbor);
+ /* There is a neighboring triangle on this edge, so this edge */
+ /* becomes a boundary edge when this triangle is deleted. */
+ hullsize++;
+ }
+ }
+ /* Return the dead triangle to the pool of triangles. */
+ triangledealloc(testtri.tri);
+ virusloop = (triangle **) traverse(&viri);
+ }
+ /* Empty the virus pool. */
+ poolrestart(&viri);
+}
+
+/*****************************************************************************/
+/* */
+/* regionplague() Spread regional attributes and/or area constraints */
+/* (from a .poly file) throughout the mesh. */
+/* */
+/* This procedure operates in two phases. The first phase spreads an */
+/* attribute and/or an area constraint through a (segment-bounded) region. */
+/* The triangles are marked to ensure that each triangle is added to the */
+/* virus pool only once, so the procedure will terminate. */
+/* */
+/* The second phase uninfects all infected triangles, returning them to */
+/* normal. */
+/* */
+/*****************************************************************************/
+
+void regionplague(attribute, area)
+REAL attribute;
+REAL area;
+{
+ struct triedge testtri;
+ struct triedge neighbor;
+ triangle **virusloop;
+ triangle **regiontri;
+ struct edge neighborshelle;
+ point regionorg, regiondest, regionapex;
+ triangle ptr; /* Temporary variable used by sym() and onext(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ if (verbose > 1) {
+ printf(" Marking neighbors of marked triangles.\n");
+ }
+ /* Loop through all the infected triangles, spreading the attribute */
+ /* and/or area constraint to their neighbors, then to their neighbors' */
+ /* neighbors. */
+ traversalinit(&viri);
+ virusloop = (triangle **) traverse(&viri);
+ while (virusloop != (triangle **) NULL) {
+ testtri.tri = *virusloop;
+ /* A triangle is marked as infected by messing with one of its shell */
+ /* edges, setting it to an illegal value. Hence, we have to */
+ /* temporarily uninfect this triangle so that we can examine its */
+ /* adjacent shell edges. */
+ uninfect(testtri);
+ if (regionattrib) {
+ /* Set an attribute. */
+ setelemattribute(testtri, eextras, attribute);
+ }
+ if (vararea) {
+ /* Set an area constraint. */
+ setareabound(testtri, area);
+ }
+ if (verbose > 2) {
+ /* Assign the triangle an orientation for convenience in */
+ /* checking its points. */
+ testtri.orient = 0;
+ org(testtri, regionorg);
+ dest(testtri, regiondest);
+ apex(testtri, regionapex);
+ printf(" Checking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
+ regionorg[0], regionorg[1], regiondest[0], regiondest[1],
+ regionapex[0], regionapex[1]);
+ }
+ /* Check each of the triangle's three neighbors. */
+ for (testtri.orient = 0; testtri.orient < 3; testtri.orient++) {
+ /* Find the neighbor. */
+ sym(testtri, neighbor);
+ /* Check for a shell between the triangle and its neighbor. */
+ tspivot(testtri, neighborshelle);
+ /* Make sure the neighbor exists, is not already infected, and */
+ /* isn't protected by a shell edge. */
+ if ((neighbor.tri != dummytri) && !infected(neighbor)
+ && (neighborshelle.sh == dummysh)) {
+ if (verbose > 2) {
+ org(neighbor, regionorg);
+ dest(neighbor, regiondest);
+ apex(neighbor, regionapex);
+ printf(" Marking (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
+ regionorg[0], regionorg[1], regiondest[0], regiondest[1],
+ regionapex[0], regionapex[1]);
+ }
+ /* Infect the neighbor. */
+ infect(neighbor);
+ /* Ensure that the neighbor's neighbors will be infected. */
+ regiontri = (triangle **) poolalloc(&viri);
+ *regiontri = neighbor.tri;
+ }
+ }
+ /* Remark the triangle as infected, so it doesn't get added to the */
+ /* virus pool again. */
+ infect(testtri);
+ virusloop = (triangle **) traverse(&viri);
+ }
+
+ /* Uninfect all triangles. */
+ if (verbose > 1) {
+ printf(" Unmarking marked triangles.\n");
+ }
+ traversalinit(&viri);
+ virusloop = (triangle **) traverse(&viri);
+ while (virusloop != (triangle **) NULL) {
+ testtri.tri = *virusloop;
+ uninfect(testtri);
+ virusloop = (triangle **) traverse(&viri);
+ }
+ /* Empty the virus pool. */
+ poolrestart(&viri);
+}
+
+/*****************************************************************************/
+/* */
+/* carveholes() Find the holes and infect them. Find the area */
+/* constraints and infect them. Infect the convex hull. */
+/* Spread the infection and kill triangles. Spread the */
+/* area constraints. */
+/* */
+/* This routine mainly calls other routines to carry out all these */
+/* functions. */
+/* */
+/*****************************************************************************/
+
+void carveholes(holelist, holes, regionlist, regions)
+REAL *holelist;
+int holes;
+REAL *regionlist;
+int regions;
+{
+ struct triedge searchtri;
+ struct triedge triangleloop;
+ struct triedge *regiontris;
+ triangle **holetri;
+ triangle **regiontri;
+ point searchorg, searchdest;
+ enum locateresult intersect;
+ int i;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+ if (!(quiet || (noholes && convex))) {
+ printf("Removing unwanted triangles.\n");
+ if (verbose && (holes > 0)) {
+ printf(" Marking holes for elimination.\n");
+ }
+ }
+
+ if (regions > 0) {
+ /* Allocate storage for the triangles in which region points fall. */
+ regiontris = (struct triedge *) malloc(regions * sizeof(struct triedge));
+ if (regiontris == (struct triedge *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+
+ if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
+ /* Initialize a pool of viri to be used for holes, concavities, */
+ /* regional attributes, and/or regional area constraints. */
+ poolinit(&viri, sizeof(triangle *), VIRUSPERBLOCK, POINTER, 0);
+ }
+
+ if (!convex) {
+ /* Mark as infected any unprotected triangles on the boundary. */
+ /* This is one way by which concavities are created. */
+ infecthull();
+ }
+
+ if ((holes > 0) && !noholes) {
+ /* Infect each triangle in which a hole lies. */
+ for (i = 0; i < 2 * holes; i += 2) {
+ /* Ignore holes that aren't within the bounds of the mesh. */
+ if ((holelist[i] >= xmin) && (holelist[i] <= xmax)
+ && (holelist[i + 1] >= ymin) && (holelist[i + 1] <= ymax)) {
+ /* Start searching from some triangle on the outer boundary. */
+ searchtri.tri = dummytri;
+ searchtri.orient = 0;
+ symself(searchtri);
+ /* Ensure that the hole is to the left of this boundary edge; */
+ /* otherwise, locate() will falsely report that the hole */
+ /* falls within the starting triangle. */
+ org(searchtri, searchorg);
+ dest(searchtri, searchdest);
+ if (counterclockwise(searchorg, searchdest, &holelist[i]) > 0.0) {
+ /* Find a triangle that contains the hole. */
+ intersect = locate(&holelist[i], &searchtri);
+ if ((intersect != OUTSIDE) && (!infected(searchtri))) {
+ /* Infect the triangle. This is done by marking the triangle */
+ /* as infect and including the triangle in the virus pool. */
+ infect(searchtri);
+ holetri = (triangle **) poolalloc(&viri);
+ *holetri = searchtri.tri;
+ }
+ }
+ }
+ }
+ }
+
+ /* Now, we have to find all the regions BEFORE we carve the holes, because */
+ /* locate() won't work when the triangulation is no longer convex. */
+ /* (Incidentally, this is the reason why regional attributes and area */
+ /* constraints can't be used when refining a preexisting mesh, which */
+ /* might not be convex; they can only be used with a freshly */
+ /* triangulated PSLG.) */
+ if (regions > 0) {
+ /* Find the starting triangle for each region. */
+ for (i = 0; i < regions; i++) {
+ regiontris[i].tri = dummytri;
+ /* Ignore region points that aren't within the bounds of the mesh. */
+ if ((regionlist[4 * i] >= xmin) && (regionlist[4 * i] <= xmax) &&
+ (regionlist[4 * i + 1] >= ymin) && (regionlist[4 * i + 1] <= ymax)) {
+ /* Start searching from some triangle on the outer boundary. */
+ searchtri.tri = dummytri;
+ searchtri.orient = 0;
+ symself(searchtri);
+ /* Ensure that the region point is to the left of this boundary */
+ /* edge; otherwise, locate() will falsely report that the */
+ /* region point falls within the starting triangle. */
+ org(searchtri, searchorg);
+ dest(searchtri, searchdest);
+ if (counterclockwise(searchorg, searchdest, ®ionlist[4 * i]) >
+ 0.0) {
+ /* Find a triangle that contains the region point. */
+ intersect = locate(®ionlist[4 * i], &searchtri);
+ if ((intersect != OUTSIDE) && (!infected(searchtri))) {
+ /* Record the triangle for processing after the */
+ /* holes have been carved. */
+ triedgecopy(searchtri, regiontris[i]);
+ }
+ }
+ }
+ }
+ }
+
+ if (viri.items > 0) {
+ /* Carve the holes and concavities. */
+ plague();
+ }
+ /* The virus pool should be empty now. */
+
+ if (regions > 0) {
+ if (!quiet) {
+ if (regionattrib) {
+ if (vararea) {
+ printf("Spreading regional attributes and area constraints.\n");
+ } else {
+ printf("Spreading regional attributes.\n");
+ }
+ } else {
+ printf("Spreading regional area constraints.\n");
+ }
+ }
+ if (regionattrib && !refine) {
+ /* Assign every triangle a regional attribute of zero. */
+ traversalinit(&triangles);
+ triangleloop.orient = 0;
+ triangleloop.tri = triangletraverse();
+ while (triangleloop.tri != (triangle *) NULL) {
+ setelemattribute(triangleloop, eextras, 0.0);
+ triangleloop.tri = triangletraverse();
+ }
+ }
+ for (i = 0; i < regions; i++) {
+ if (regiontris[i].tri != dummytri) {
+ /* Make sure the triangle under consideration still exists. */
+ /* It may have been eaten by the virus. */
+ if (regiontris[i].tri[3] != (triangle) NULL) {
+ /* Put one triangle in the virus pool. */
+ infect(regiontris[i]);
+ regiontri = (triangle **) poolalloc(&viri);
+ *regiontri = regiontris[i].tri;
+ /* Apply one region's attribute and/or area constraint. */
+ regionplague(regionlist[4 * i + 2], regionlist[4 * i + 3]);
+ /* The virus pool should be empty now. */
+ }
+ }
+ }
+ if (regionattrib && !refine) {
+ /* Note the fact that each triangle has an additional attribute. */
+ eextras++;
+ }
+ }
+
+ /* Free up memory. */
+ if (((holes > 0) && !noholes) || !convex || (regions > 0)) {
+ pooldeinit(&viri);
+ }
+ if (regions > 0) {
+ free(regiontris);
+ }
+}
+
+/** **/
+/** **/
+/********* Carving out holes and concavities ends here *********/
+
+/********* Mesh quality maintenance begins here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* tallyencs() Traverse the entire list of shell edges, check each edge */
+/* to see if it is encroached. If so, add it to the list. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void tallyencs()
+{
+ struct edge edgeloop;
+ int dummy;
+
+ traversalinit(&shelles);
+ edgeloop.shorient = 0;
+ edgeloop.sh = shelletraverse();
+ while (edgeloop.sh != (shelle *) NULL) {
+ /* If the segment is encroached, add it to the list. */
+ dummy = checkedge4encroach(&edgeloop);
+ edgeloop.sh = shelletraverse();
+ }
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* precisionerror() Print an error message for precision problems. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void precisionerror()
+{
+ printf("Try increasing the area criterion and/or reducing the minimum\n");
+ printf(" allowable angle so that tiny triangles are not created.\n");
+#ifdef SINGLE
+ printf("Alternatively, try recompiling me with double precision\n");
+ printf(" arithmetic (by removing \"#define SINGLE\" from the\n");
+ printf(" source file or \"-DSINGLE\" from the makefile).\n");
+#endif /* SINGLE */
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* repairencs() Find and repair all the encroached segments. */
+/* */
+/* Encroached segments are repaired by splitting them by inserting a point */
+/* at or near their centers. */
+/* */
+/* `flaws' is a flag that specifies whether one should take note of new */
+/* encroached segments and bad triangles that result from inserting points */
+/* to repair existing encroached segments. */
+/* */
+/* When a segment is split, the two resulting subsegments are always */
+/* tested to see if they are encroached upon, regardless of the value */
+/* of `flaws'. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void repairencs(flaws)
+int flaws;
+{
+ struct triedge enctri;
+ struct triedge testtri;
+ struct edge *encloop;
+ struct edge testsh;
+ point eorg, edest;
+ point newpoint;
+ enum insertsiteresult success;
+ REAL segmentlength, nearestpoweroftwo;
+ REAL split;
+ int acuteorg, acutedest;
+ int dummy;
+ int i;
+ triangle ptr; /* Temporary variable used by stpivot(). */
+ shelle sptr; /* Temporary variable used by snext(). */
+
+ while ((badsegments.items > 0) && (steinerleft != 0)) {
+ traversalinit(&badsegments);
+ encloop = badsegmenttraverse();
+ while ((encloop != (struct edge *) NULL) && (steinerleft != 0)) {
+ /* To decide where to split a segment, we need to know if the */
+ /* segment shares an endpoint with an adjacent segment. */
+ /* The concern is that, if we simply split every encroached */
+ /* segment in its center, two adjacent segments with a small */
+ /* angle between them might lead to an infinite loop; each */
+ /* point added to split one segment will encroach upon the */
+ /* other segment, which must then be split with a point that */
+ /* will encroach upon the first segment, and so on forever. */
+ /* To avoid this, imagine a set of concentric circles, whose */
+ /* radii are powers of two, about each segment endpoint. */
+ /* These concentric circles determine where the segment is */
+ /* split. (If both endpoints are shared with adjacent */
+ /* segments, split the segment in the middle, and apply the */
+ /* concentric shells for later splittings.) */
+
+ /* Is the origin shared with another segment? */
+ stpivot(*encloop, enctri);
+ lnext(enctri, testtri);
+ tspivot(testtri, testsh);
+ acuteorg = testsh.sh != dummysh;
+ /* Is the destination shared with another segment? */
+ lnextself(testtri);
+ tspivot(testtri, testsh);
+ acutedest = testsh.sh != dummysh;
+ /* Now, check the other side of the segment, if there's a triangle */
+ /* there. */
+ sym(enctri, testtri);
+ if (testtri.tri != dummytri) {
+ /* Is the destination shared with another segment? */
+ lnextself(testtri);
+ tspivot(testtri, testsh);
+ acutedest = acutedest || (testsh.sh != dummysh);
+ /* Is the origin shared with another segment? */
+ lnextself(testtri);
+ tspivot(testtri, testsh);
+ acuteorg = acuteorg || (testsh.sh != dummysh);
+ }
+
+ sorg(*encloop, eorg);
+ sdest(*encloop, edest);
+ /* Use the concentric circles if exactly one endpoint is shared */
+ /* with another adjacent segment. */
+ if (acuteorg ^ acutedest) {
+ segmentlength = sqrt((edest[0] - eorg[0]) * (edest[0] - eorg[0])
+ + (edest[1] - eorg[1]) * (edest[1] - eorg[1]));
+ /* Find the power of two nearest the segment's length. */
+ nearestpoweroftwo = 1.0;
+ while (segmentlength > SQUAREROOTTWO * nearestpoweroftwo) {
+ nearestpoweroftwo *= 2.0;
+ }
+ while (segmentlength < (0.5 * SQUAREROOTTWO) * nearestpoweroftwo) {
+ nearestpoweroftwo *= 0.5;
+ }
+ /* Where do we split the segment? */
+ split = 0.5 * nearestpoweroftwo / segmentlength;
+ if (acutedest) {
+ split = 1.0 - split;
+ }
+ } else {
+ /* If we're not worried about adjacent segments, split */
+ /* this segment in the middle. */
+ split = 0.5;
+ }
+
+ /* Create the new point. */
+ newpoint = (point) poolalloc(&points);
+ /* Interpolate its coordinate and attributes. */
+ for (i = 0; i < 2 + nextras; i++) {
+ newpoint[i] = (1.0 - split) * eorg[i] + split * edest[i];
+ }
+ setpointmark(newpoint, mark(*encloop));
+ if (verbose > 1) {
+ printf(
+ " Splitting edge (%.12g, %.12g) (%.12g, %.12g) at (%.12g, %.12g).\n",
+ eorg[0], eorg[1], edest[0], edest[1], newpoint[0], newpoint[1]);
+ }
+ /* Check whether the new point lies on an endpoint. */
+ if (((newpoint[0] == eorg[0]) && (newpoint[1] == eorg[1]))
+ || ((newpoint[0] == edest[0]) && (newpoint[1] == edest[1]))) {
+ printf("Error: Ran out of precision at (%.12g, %.12g).\n",
+ newpoint[0], newpoint[1]);
+ printf("I attempted to split a segment to a smaller size than can\n");
+ printf(" be accommodated by the finite precision of floating point\n"
+ );
+ printf(" arithmetic.\n");
+ precisionerror();
+ exit(1);
+ }
+ /* Insert the splitting point. This should always succeed. */
+ success = insertsite(newpoint, &enctri, encloop, flaws, flaws);
+ if ((success != SUCCESSFULPOINT) && (success != ENCROACHINGPOINT)) {
+ printf("Internal error in repairencs():\n");
+ printf(" Failure to split a segment.\n");
+ internalerror();
+ }
+ if (steinerleft > 0) {
+ steinerleft--;
+ }
+ /* Check the two new subsegments to see if they're encroached. */
+ dummy = checkedge4encroach(encloop);
+ snextself(*encloop);
+ dummy = checkedge4encroach(encloop);
+
+ badsegmentdealloc(encloop);
+ encloop = badsegmenttraverse();
+ }
+ }
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* tallyfaces() Test every triangle in the mesh for quality measures. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void tallyfaces()
+{
+ struct triedge triangleloop;
+
+ if (verbose) {
+ printf(" Making a list of bad triangles.\n");
+ }
+ traversalinit(&triangles);
+ triangleloop.orient = 0;
+ triangleloop.tri = triangletraverse();
+ while (triangleloop.tri != (triangle *) NULL) {
+ /* If the triangle is bad, enqueue it. */
+ testtriangle(&triangleloop);
+ triangleloop.tri = triangletraverse();
+ }
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* findcircumcenter() Find the circumcenter of a triangle. */
+/* */
+/* The result is returned both in terms of x-y coordinates and xi-eta */
+/* coordinates. The xi-eta coordinate system is defined in terms of the */
+/* triangle: the origin of the triangle is the origin of the coordinate */
+/* system; the destination of the triangle is one unit along the xi axis; */
+/* and the apex of the triangle is one unit along the eta axis. */
+/* */
+/* The return value indicates which edge of the triangle is shortest. */
+/* */
+/*****************************************************************************/
+
+enum circumcenterresult findcircumcenter(torg, tdest, tapex, circumcenter,
+ xi, eta)
+point torg;
+point tdest;
+point tapex;
+point circumcenter;
+REAL *xi;
+REAL *eta;
+{
+ REAL xdo, ydo, xao, yao, xad, yad;
+ REAL dodist, aodist, addist;
+ REAL denominator;
+ REAL dx, dy;
+
+ circumcentercount++;
+
+ /* Compute the circumcenter of the triangle. */
+ xdo = tdest[0] - torg[0];
+ ydo = tdest[1] - torg[1];
+ xao = tapex[0] - torg[0];
+ yao = tapex[1] - torg[1];
+ dodist = xdo * xdo + ydo * ydo;
+ aodist = xao * xao + yao * yao;
+ if (noexact) {
+ denominator = (REAL)(0.5 / (xdo * yao - xao * ydo));
+ } else {
+ /* Use the counterclockwise() routine to ensure a positive (and */
+ /* reasonably accurate) result, avoiding any possibility of */
+ /* division by zero. */
+ denominator = (REAL)(0.5 / counterclockwise(tdest, tapex, torg));
+ /* Don't count the above as an orientation test. */
+ counterclockcount--;
+ }
+ circumcenter[0] = torg[0] - (ydo * aodist - yao * dodist) * denominator;
+ circumcenter[1] = torg[1] + (xdo * aodist - xao * dodist) * denominator;
+
+ /* To interpolate point attributes for the new point inserted at */
+ /* the circumcenter, define a coordinate system with a xi-axis, */
+ /* directed from the triangle's origin to its destination, and */
+ /* an eta-axis, directed from its origin to its apex. */
+ /* Calculate the xi and eta coordinates of the circumcenter. */
+ dx = circumcenter[0] - torg[0];
+ dy = circumcenter[1] - torg[1];
+ *xi = (REAL)((dx * yao - xao * dy) * (2.0 * denominator));
+ *eta = (REAL)((xdo * dy - dx * ydo) * (2.0 * denominator));
+
+ xad = tapex[0] - tdest[0];
+ yad = tapex[1] - tdest[1];
+ addist = xad * xad + yad * yad;
+ if ((addist < dodist) && (addist < aodist)) {
+ return OPPOSITEORG;
+ } else if (dodist < aodist) {
+ return OPPOSITEAPEX;
+ } else {
+ return OPPOSITEDEST;
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* splittriangle() Inserts a point at the circumcenter of a triangle. */
+/* Deletes the newly inserted point if it encroaches upon */
+/* a segment. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void splittriangle(badtri)
+struct badface *badtri;
+{
+ point borg, bdest, bapex;
+ point newpoint;
+ REAL xi, eta;
+ enum insertsiteresult success;
+ enum circumcenterresult shortedge;
+ int errorflag;
+ int i;
+
+ org(badtri->badfacetri, borg);
+ dest(badtri->badfacetri, bdest);
+ apex(badtri->badfacetri, bapex);
+ /* Make sure that this triangle is still the same triangle it was */
+ /* when it was tested and determined to be of bad quality. */
+ /* Subsequent transformations may have made it a different triangle. */
+ if ((borg == badtri->faceorg) && (bdest == badtri->facedest) &&
+ (bapex == badtri->faceapex)) {
+ if (verbose > 1) {
+ printf(" Splitting this triangle at its circumcenter:\n");
+ printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n", borg[0],
+ borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
+ }
+ errorflag = 0;
+ /* Create a new point at the triangle's circumcenter. */
+ newpoint = (point) poolalloc(&points);
+ shortedge = findcircumcenter(borg, bdest, bapex, newpoint, &xi, &eta);
+ /* Check whether the new point lies on a triangle vertex. */
+ if (((newpoint[0] == borg[0]) && (newpoint[1] == borg[1]))
+ || ((newpoint[0] == bdest[0]) && (newpoint[1] == bdest[1]))
+ || ((newpoint[0] == bapex[0]) && (newpoint[1] == bapex[1]))) {
+ if (!quiet) {
+ printf("Warning: New point (%.12g, %.12g) falls on existing vertex.\n"
+ , newpoint[0], newpoint[1]);
+ errorflag = 1;
+ }
+ pointdealloc(newpoint);
+ } else {
+ for (i = 2; i < 2 + nextras; i++) {
+ /* Interpolate the point attributes at the circumcenter. */
+ newpoint[i] = borg[i] + xi * (bdest[i] - borg[i])
+ + eta * (bapex[i] - borg[i]);
+ }
+ /* The new point must be in the interior, and have a marker of zero. */
+ setpointmark(newpoint, 0);
+ /* Ensure that the handle `badtri->badfacetri' represents the shortest */
+ /* edge of the triangle. This ensures that the circumcenter must */
+ /* fall to the left of this edge, so point location will work. */
+ if (shortedge == OPPOSITEORG) {
+ lnextself(badtri->badfacetri);
+ } else if (shortedge == OPPOSITEDEST) {
+ lprevself(badtri->badfacetri);
+ }
+ /* Insert the circumcenter, searching from the edge of the triangle, */
+ /* and maintain the Delaunay property of the triangulation. */
+ success = insertsite(newpoint, &(badtri->badfacetri),
+ (struct edge *) NULL, 1, 1);
+ if (success == SUCCESSFULPOINT) {
+ if (steinerleft > 0) {
+ steinerleft--;
+ }
+ } else if (success == ENCROACHINGPOINT) {
+ /* If the newly inserted point encroaches upon a segment, delete it. */
+ deletesite(&(badtri->badfacetri));
+ } else if (success == VIOLATINGPOINT) {
+ /* Failed to insert the new point, but some segment was */
+ /* marked as being encroached. */
+ pointdealloc(newpoint);
+ } else { /* success == DUPLICATEPOINT */
+ /* Failed to insert the new point because a vertex is already there. */
+ if (!quiet) {
+ printf(
+ "Warning: New point (%.12g, %.12g) falls on existing vertex.\n"
+ , newpoint[0], newpoint[1]);
+ errorflag = 1;
+ }
+ pointdealloc(newpoint);
+ }
+ }
+ if (errorflag) {
+ if (verbose) {
+ printf(" The new point is at the circumcenter of triangle\n");
+ printf(" (%.12g, %.12g) (%.12g, %.12g) (%.12g, %.12g)\n",
+ borg[0], borg[1], bdest[0], bdest[1], bapex[0], bapex[1]);
+ }
+ printf("This probably means that I am trying to refine triangles\n");
+ printf(" to a smaller size than can be accommodated by the finite\n");
+ printf(" precision of floating point arithmetic. (You can be\n");
+ printf(" sure of this if I fail to terminate.)\n");
+ precisionerror();
+ }
+ }
+ /* Return the bad triangle to the pool. */
+ pooldealloc(&badtriangles, (VOID *) badtri);
+}
+
+#endif /* not CDT_ONLY */
+
+/*****************************************************************************/
+/* */
+/* enforcequality() Remove all the encroached edges and bad triangles */
+/* from the triangulation. */
+/* */
+/*****************************************************************************/
+
+#ifndef CDT_ONLY
+
+void enforcequality()
+{
+ int i;
+
+ if (!quiet) {
+ printf("Adding Steiner points to enforce quality.\n");
+ }
+ /* Initialize the pool of encroached segments. */
+ poolinit(&badsegments, sizeof(struct edge), BADSEGMENTPERBLOCK, POINTER, 0);
+ if (verbose) {
+ printf(" Looking for encroached segments.\n");
+ }
+ /* Test all segments to see if they're encroached. */
+ tallyencs();
+ if (verbose && (badsegments.items > 0)) {
+ printf(" Splitting encroached segments.\n");
+ }
+ /* Note that steinerleft == -1 if an unlimited number */
+ /* of Steiner points is allowed. */
+ while ((badsegments.items > 0) && (steinerleft != 0)) {
+ /* Fix the segments without noting newly encroached segments or */
+ /* bad triangles. The reason we don't want to note newly */
+ /* encroached segments is because some encroached segments are */
+ /* likely to be noted multiple times, and would then be blindly */
+ /* split multiple times. I should fix that some time. */
+ repairencs(0);
+ /* Now, find all the segments that became encroached while adding */
+ /* points to split encroached segments. */
+ tallyencs();
+ }
+ /* At this point, if we haven't run out of Steiner points, the */
+ /* triangulation should be (conforming) Delaunay. */
+
+ /* Next, we worry about enforcing triangle quality. */
+ if ((minangle > 0.0) || vararea || fixedarea) {
+ /* Initialize the pool of bad triangles. */
+ poolinit(&badtriangles, sizeof(struct badface), BADTRIPERBLOCK, POINTER,
+ 0);
+ /* Initialize the queues of bad triangles. */
+ for (i = 0; i < 64; i++) {
+ queuefront[i] = (struct badface *) NULL;
+ queuetail[i] = &queuefront[i];
+ }
+ /* Test all triangles to see if they're bad. */
+ tallyfaces();
+ if (verbose) {
+ printf(" Splitting bad triangles.\n");
+ }
+ while ((badtriangles.items > 0) && (steinerleft != 0)) {
+ /* Fix one bad triangle by inserting a point at its circumcenter. */
+ splittriangle(dequeuebadtri());
+ /* Fix any encroached segments that may have resulted. Record */
+ /* any new bad triangles or encroached segments that result. */
+ if (badsegments.items > 0) {
+ repairencs(1);
+ }
+ }
+ }
+ /* At this point, if we haven't run out of Steiner points, the */
+ /* triangulation should be (conforming) Delaunay and have no */
+ /* low-quality triangles. */
+
+ /* Might we have run out of Steiner points too soon? */
+ if (!quiet && (badsegments.items > 0) && (steinerleft == 0)) {
+ printf("\nWarning: I ran out of Steiner points, but the mesh has\n");
+ if (badsegments.items == 1) {
+ printf(" an encroached segment, and therefore might not be truly\n");
+ } else {
+ printf(" %ld encroached segments, and therefore might not be truly\n",
+ badsegments.items);
+ }
+ printf(" Delaunay. If the Delaunay property is important to you,\n");
+ printf(" try increasing the number of Steiner points (controlled by\n");
+ printf(" the -S switch) slightly and try again.\n\n");
+ }
+}
+
+#endif /* not CDT_ONLY */
+
+/** **/
+/** **/
+/********* Mesh quality maintenance ends here *********/
+
+/*****************************************************************************/
+/* */
+/* highorder() Create extra nodes for quadratic subparametric elements. */
+/* */
+/*****************************************************************************/
+
+void highorder()
+{
+ struct triedge triangleloop, trisym;
+ struct edge checkmark;
+ point newpoint;
+ point torg, tdest;
+ int i;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+ if (!quiet) {
+ printf("Adding vertices for second-order triangles.\n");
+ }
+ /* The following line ensures that dead items in the pool of nodes */
+ /* cannot be allocated for the extra nodes associated with high */
+ /* order elements. This ensures that the primary nodes (at the */
+ /* corners of elements) will occur earlier in the output files, and */
+ /* have lower indices, than the extra nodes. */
+ points.deaditemstack = (VOID *) NULL;
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ /* To loop over the set of edges, loop over all triangles, and look at */
+ /* the three edges of each triangle. If there isn't another triangle */
+ /* adjacent to the edge, operate on the edge. If there is another */
+ /* adjacent triangle, operate on the edge only if the current triangle */
+ /* has a smaller pointer than its neighbor. This way, each edge is */
+ /* considered only once. */
+ while (triangleloop.tri != (triangle *) NULL) {
+ for (triangleloop.orient = 0; triangleloop.orient < 3;
+ triangleloop.orient++) {
+ sym(triangleloop, trisym);
+ if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
+ org(triangleloop, torg);
+ dest(triangleloop, tdest);
+ /* Create a new node in the middle of the edge. Interpolate */
+ /* its attributes. */
+ newpoint = (point) poolalloc(&points);
+ for (i = 0; i < 2 + nextras; i++) {
+ newpoint[i] = (REAL)(0.5 * (torg[i] + tdest[i]));
+ }
+ /* Set the new node's marker to zero or one, depending on */
+ /* whether it lies on a boundary. */
+ setpointmark(newpoint, trisym.tri == dummytri);
+ if (useshelles) {
+ tspivot(triangleloop, checkmark);
+ /* If this edge is a segment, transfer the marker to the new node. */
+ if (checkmark.sh != dummysh) {
+ setpointmark(newpoint, mark(checkmark));
+ }
+ }
+ if (verbose > 1) {
+ printf(" Creating (%.12g, %.12g).\n", newpoint[0], newpoint[1]);
+ }
+ /* Record the new node in the (one or two) adjacent elements. */
+ triangleloop.tri[highorderindex + triangleloop.orient] =
+ (triangle) newpoint;
+ if (trisym.tri != dummytri) {
+ trisym.tri[highorderindex + trisym.orient] = (triangle) newpoint;
+ }
+ }
+ }
+ triangleloop.tri = triangletraverse();
+ }
+}
+
+/********* File I/O routines begin here *********/
+/** **/
+/** **/
+
+/*****************************************************************************/
+/* */
+/* readline() Read a nonempty line from a file. */
+/* */
+/* A line is considered "nonempty" if it contains something that looks like */
+/* a number. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+char *readline(string, infile, infilename)
+char *string;
+FILE *infile;
+char *infilename;
+{
+ char *result;
+
+ /* Search for something that looks like a number. */
+ do {
+ result = fgets(string, INPUTLINESIZE, infile);
+ if (result == (char *) NULL) {
+ printf(" Error: Unexpected end of file in %s.\n", infilename);
+ exit(1);
+ }
+ /* Skip anything that doesn't look like a number, a comment, */
+ /* or the end of a line. */
+ while ((*result != '\0') && (*result != '#')
+ && (*result != '.') && (*result != '+') && (*result != '-')
+ && ((*result < '0') || (*result > '9'))) {
+ result++;
+ }
+ /* If it's a comment or end of line, read another line and try again. */
+ } while ((*result == '#') || (*result == '\0'));
+ return result;
+}
+
+#endif /* not TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* findfield() Find the next field of a string. */
+/* */
+/* Jumps past the current field by searching for whitespace, then jumps */
+/* past the whitespace to find the next field. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+char *findfield(string)
+char *string;
+{
+ char *result;
+
+ result = string;
+ /* Skip the current field. Stop upon reaching whitespace. */
+ while ((*result != '\0') && (*result != '#')
+ && (*result != ' ') && (*result != '\t')) {
+ result++;
+ }
+ /* Now skip the whitespace and anything else that doesn't look like a */
+ /* number, a comment, or the end of a line. */
+ while ((*result != '\0') && (*result != '#')
+ && (*result != '.') && (*result != '+') && (*result != '-')
+ && ((*result < '0') || (*result > '9'))) {
+ result++;
+ }
+ /* Check for a comment (prefixed with `#'). */
+ if (*result == '#') {
+ *result = '\0';
+ }
+ return result;
+}
+
+#endif /* not TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* readnodes() Read the points from a file, which may be a .node or .poly */
+/* file. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+void readnodes(nodefilename, polyfilename, polyfile)
+char *nodefilename;
+char *polyfilename;
+FILE **polyfile;
+{
+ FILE *infile;
+ point pointloop;
+ char inputline[INPUTLINESIZE];
+ char *stringptr;
+ char *infilename;
+ REAL x, y;
+ int firstnode;
+ int nodemarkers;
+ int currentmarker;
+ int i, j;
+
+ if (poly) {
+ /* Read the points from a .poly file. */
+ if (!quiet) {
+ printf("Opening %s.\n", polyfilename);
+ }
+ *polyfile = fopen(polyfilename, "r");
+ if (*polyfile == (FILE *) NULL) {
+ printf(" Error: Cannot access file %s.\n", polyfilename);
+ exit(1);
+ }
+ /* Read number of points, number of dimensions, number of point */
+ /* attributes, and number of boundary markers. */
+ stringptr = readline(inputline, *polyfile, polyfilename);
+ inpoints = (int) strtol (stringptr, &stringptr, 0);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ mesh_dim = 2;
+ } else {
+ mesh_dim = (int) strtol (stringptr, &stringptr, 0);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ nextras = 0;
+ } else {
+ nextras = (int) strtol (stringptr, &stringptr, 0);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ nodemarkers = 0;
+ } else {
+ nodemarkers = (int) strtol (stringptr, &stringptr, 0);
+ }
+ if (inpoints > 0) {
+ infile = *polyfile;
+ infilename = polyfilename;
+ readnodefile = 0;
+ } else {
+ /* If the .poly file claims there are zero points, that means that */
+ /* the points should be read from a separate .node file. */
+ readnodefile = 1;
+ infilename = innodefilename;
+ }
+ } else {
+ readnodefile = 1;
+ infilename = innodefilename;
+ *polyfile = (FILE *) NULL;
+ }
+
+ if (readnodefile) {
+ /* Read the points from a .node file. */
+ if (!quiet) {
+ printf("Opening %s.\n", innodefilename);
+ }
+ infile = fopen(innodefilename, "r");
+ if (infile == (FILE *) NULL) {
+ printf(" Error: Cannot access file %s.\n", innodefilename);
+ exit(1);
+ }
+ /* Read number of points, number of dimensions, number of point */
+ /* attributes, and number of boundary markers. */
+ stringptr = readline(inputline, infile, innodefilename);
+ inpoints = (int) strtol (stringptr, &stringptr, 0);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ mesh_dim = 2;
+ } else {
+ mesh_dim = (int) strtol (stringptr, &stringptr, 0);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ nextras = 0;
+ } else {
+ nextras = (int) strtol (stringptr, &stringptr, 0);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ nodemarkers = 0;
+ } else {
+ nodemarkers = (int) strtol (stringptr, &stringptr, 0);
+ }
+ }
+
+ if (inpoints < 3) {
+ printf("Error: Input must have at least three input points.\n");
+ exit(1);
+ }
+ if (mesh_dim != 2) {
+ printf("Error: Triangle only works with two-dimensional meshes.\n");
+ exit(1);
+ }
+
+ initializepointpool();
+
+ /* Read the points. */
+ for (i = 0; i < inpoints; i++) {
+ pointloop = (point) poolalloc(&points);
+ stringptr = readline(inputline, infile, infilename);
+ if (i == 0) {
+ firstnode = (int) strtol (stringptr, &stringptr, 0);
+ if ((firstnode == 0) || (firstnode == 1)) {
+ firstnumber = firstnode;
+ }
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Point %d has no x coordinate.\n", firstnumber + i);
+ exit(1);
+ }
+ x = (REAL) strtod(stringptr, &stringptr);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Point %d has no y coordinate.\n", firstnumber + i);
+ exit(1);
+ }
+ y = (REAL) strtod(stringptr, &stringptr);
+ pointloop[0] = x;
+ pointloop[1] = y;
+ /* Read the point attributes. */
+ for (j = 2; j < 2 + nextras; j++) {
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ pointloop[j] = 0.0;
+ } else {
+ pointloop[j] = (REAL) strtod(stringptr, &stringptr);
+ }
+ }
+ if (nodemarkers) {
+ /* Read a point marker. */
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ setpointmark(pointloop, 0);
+ } else {
+ currentmarker = (int) strtol (stringptr, &stringptr, 0);
+ setpointmark(pointloop, currentmarker);
+ }
+ } else {
+ /* If no markers are specified in the file, they default to zero. */
+ setpointmark(pointloop, 0);
+ }
+ /* Determine the smallest and largest x and y coordinates. */
+ if (i == 0) {
+ xmin = xmax = x;
+ ymin = ymax = y;
+ } else {
+ xmin = (x < xmin) ? x : xmin;
+ xmax = (x > xmax) ? x : xmax;
+ ymin = (y < ymin) ? y : ymin;
+ ymax = (y > ymax) ? y : ymax;
+ }
+ }
+ if (readnodefile) {
+ fclose(infile);
+ }
+
+ /* Nonexistent x value used as a flag to mark circle events in sweepline */
+ /* Delaunay algorithm. */
+ xminextreme = 10 * xmin - 9 * xmax;
+}
+
+#endif /* not TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* transfernodes() Read the points from memory. */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+void transfernodes(pointlist, pointattriblist, pointmarkerlist, numberofpoints,
+ numberofpointattribs)
+REAL *pointlist;
+REAL *pointattriblist;
+int *pointmarkerlist;
+int numberofpoints;
+int numberofpointattribs;
+{
+ point pointloop;
+ REAL x, y;
+ int i, j;
+ int coordindex;
+ int attribindex;
+
+ inpoints = numberofpoints;
+ mesh_dim = 2;
+ nextras = numberofpointattribs;
+ readnodefile = 0;
+ if (inpoints < 3) {
+ printf("Error: Input must have at least three input points.\n");
+ exit(1);
+ }
+
+ initializepointpool();
+
+ /* Read the points. */
+ coordindex = 0;
+ attribindex = 0;
+ for (i = 0; i < inpoints; i++) {
+ pointloop = (point) poolalloc(&points);
+ /* Read the point coordinates. */
+ x = pointloop[0] = pointlist[coordindex++];
+ y = pointloop[1] = pointlist[coordindex++];
+ /* Read the point attributes. */
+ for (j = 0; j < numberofpointattribs; j++) {
+ pointloop[2 + j] = pointattriblist[attribindex++];
+ }
+ if (pointmarkerlist != (int *) NULL) {
+ /* Read a point marker. */
+ setpointmark(pointloop, pointmarkerlist[i]);
+ } else {
+ /* If no markers are specified, they default to zero. */
+ setpointmark(pointloop, 0);
+ }
+ x = pointloop[0];
+ y = pointloop[1];
+ /* Determine the smallest and largest x and y coordinates. */
+ if (i == 0) {
+ xmin = xmax = x;
+ ymin = ymax = y;
+ } else {
+ xmin = (x < xmin) ? x : xmin;
+ xmax = (x > xmax) ? x : xmax;
+ ymin = (y < ymin) ? y : ymin;
+ ymax = (y > ymax) ? y : ymax;
+ }
+ }
+
+ /* Nonexistent x value used as a flag to mark circle events in sweepline */
+ /* Delaunay algorithm. */
+ xminextreme = 10 * xmin - 9 * xmax;
+}
+
+#endif /* TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* readholes() Read the holes, and possibly regional attributes and area */
+/* constraints, from a .poly file. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+void readholes(polyfile, polyfilename, hlist, holes, rlist, regions)
+FILE *polyfile;
+char *polyfilename;
+REAL **hlist;
+int *holes;
+REAL **rlist;
+int *regions;
+{
+ REAL *holelist;
+ REAL *regionlist;
+ char inputline[INPUTLINESIZE];
+ char *stringptr;
+ int index;
+ int i;
+
+ /* Read the holes. */
+ stringptr = readline(inputline, polyfile, polyfilename);
+ *holes = (int) strtol (stringptr, &stringptr, 0);
+ if (*holes > 0) {
+ holelist = (REAL *) malloc(2 * *holes * sizeof(REAL));
+ *hlist = holelist;
+ if (holelist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ for (i = 0; i < 2 * *holes; i += 2) {
+ stringptr = readline(inputline, polyfile, polyfilename);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Hole %d has no x coordinate.\n",
+ firstnumber + (i >> 1));
+ exit(1);
+ } else {
+ holelist[i] = (REAL) strtod(stringptr, &stringptr);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Hole %d has no y coordinate.\n",
+ firstnumber + (i >> 1));
+ exit(1);
+ } else {
+ holelist[i + 1] = (REAL) strtod(stringptr, &stringptr);
+ }
+ }
+ } else {
+ *hlist = (REAL *) NULL;
+ }
+
+#ifndef CDT_ONLY
+ if ((regionattrib || vararea) && !refine) {
+ /* Read the area constraints. */
+ stringptr = readline(inputline, polyfile, polyfilename);
+ *regions = (int) strtol (stringptr, &stringptr, 0);
+ if (*regions > 0) {
+ regionlist = (REAL *) malloc(4 * *regions * sizeof(REAL));
+ *rlist = regionlist;
+ if (regionlist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ index = 0;
+ for (i = 0; i < *regions; i++) {
+ stringptr = readline(inputline, polyfile, polyfilename);
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Region %d has no x coordinate.\n",
+ firstnumber + i);
+ exit(1);
+ } else {
+ regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf("Error: Region %d has no y coordinate.\n",
+ firstnumber + i);
+ exit(1);
+ } else {
+ regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ printf(
+ "Error: Region %d has no region attribute or area constraint.\n",
+ firstnumber + i);
+ exit(1);
+ } else {
+ regionlist[index++] = (REAL) strtod(stringptr, &stringptr);
+ }
+ stringptr = findfield(stringptr);
+ if (*stringptr == '\0') {
+ regionlist[index] = regionlist[index - 1];
+ } else {
+ regionlist[index] = (REAL) strtod(stringptr, &stringptr);
+ }
+ index++;
+ }
+ }
+ } else {
+ /* Set `*regions' to zero to avoid an accidental free() later. */
+ *regions = 0;
+ *rlist = (REAL *) NULL;
+ }
+#endif /* not CDT_ONLY */
+
+ fclose(polyfile);
+}
+
+#endif /* not TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* finishfile() Write the command line to the output file so the user */
+/* can remember how the file was generated. Close the file. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+void finishfile(outfile, argc, argv)
+FILE *outfile;
+int argc;
+char **argv;
+{
+ int i;
+
+ fprintf(outfile, "# Generated by");
+ for (i = 0; i < argc; i++) {
+ fprintf(outfile, " ");
+ fputs(argv[i], outfile);
+ }
+ fprintf(outfile, "\n");
+ fclose(outfile);
+}
+
+#endif /* not TRILIBRARY */
+
+/*****************************************************************************/
+/* */
+/* writenodes() Number the points and write them to a .node file. */
+/* */
+/* To save memory, the point numbers are written over the shell markers */
+/* after the points are written to a file. */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+void writenodes(pointlist, pointattriblist, pointmarkerlist)
+REAL **pointlist;
+REAL **pointattriblist;
+int **pointmarkerlist;
+
+#else /* not TRILIBRARY */
+
+void writenodes(nodefilename, argc, argv)
+char *nodefilename;
+int argc;
+char **argv;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ REAL *plist;
+ REAL *palist;
+ int *pmlist;
+ int coordindex;
+ int attribindex;
+#else /* not TRILIBRARY */
+ FILE *outfile;
+#endif /* not TRILIBRARY */
+ point pointloop;
+ int pointnumber;
+ int i;
+
+#ifdef TRILIBRARY
+ if (!quiet) {
+ printf("Writing points.\n");
+ }
+ /* Allocate memory for output points if necessary. */
+ if (*pointlist == (REAL *) NULL) {
+ *pointlist = (REAL *) malloc(points.items * 2 * sizeof(REAL));
+ if (*pointlist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ /* Allocate memory for output point attributes if necessary. */
+ if ((nextras > 0) && (*pointattriblist == (REAL *) NULL)) {
+ *pointattriblist = (REAL *) malloc(points.items * nextras * sizeof(REAL));
+ if (*pointattriblist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ /* Allocate memory for output point markers if necessary. */
+ if (!nobound && (*pointmarkerlist == (int *) NULL)) {
+ *pointmarkerlist = (int *) malloc(points.items * sizeof(int));
+ if (*pointmarkerlist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ plist = *pointlist;
+ palist = *pointattriblist;
+ pmlist = *pointmarkerlist;
+ coordindex = 0;
+ attribindex = 0;
+#else /* not TRILIBRARY */
+ if (!quiet) {
+ printf("Writing %s.\n", nodefilename);
+ }
+ outfile = fopen(nodefilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", nodefilename);
+ exit(1);
+ }
+ /* Number of points, number of dimensions, number of point attributes, */
+ /* and number of boundary markers (zero or one). */
+ fprintf(outfile, "%ld %d %d %d\n", points.items, mesh_dim, nextras,
+ 1 - nobound);
+#endif /* not TRILIBRARY */
+
+ traversalinit(&points);
+ pointloop = pointtraverse();
+ pointnumber = firstnumber;
+ while (pointloop != (point) NULL) {
+#ifdef TRILIBRARY
+ /* X and y coordinates. */
+ plist[coordindex++] = pointloop[0];
+ plist[coordindex++] = pointloop[1];
+ /* Point attributes. */
+ for (i = 0; i < nextras; i++) {
+ palist[attribindex++] = pointloop[2 + i];
+ }
+ if (!nobound) {
+ /* Copy the boundary marker. */
+ pmlist[pointnumber - firstnumber] = pointmark(pointloop);
+ }
+#else /* not TRILIBRARY */
+ /* Point number, x and y coordinates. */
+ fprintf(outfile, "%4d %.17g %.17g", pointnumber, pointloop[0],
+ pointloop[1]);
+ for (i = 0; i < nextras; i++) {
+ /* Write an attribute. */
+ fprintf(outfile, " %.17g", pointloop[i + 2]);
+ }
+ if (nobound) {
+ fprintf(outfile, "\n");
+ } else {
+ /* Write the boundary marker. */
+ fprintf(outfile, " %d\n", pointmark(pointloop));
+ }
+#endif /* not TRILIBRARY */
+
+ setpointmark(pointloop, pointnumber);
+ pointloop = pointtraverse();
+ pointnumber++;
+ }
+
+#ifndef TRILIBRARY
+ finishfile(outfile, argc, argv);
+#endif /* not TRILIBRARY */
+}
+
+/*****************************************************************************/
+/* */
+/* numbernodes() Number the points. */
+/* */
+/* Each point is assigned a marker equal to its number. */
+/* */
+/* Used when writenodes() is not called because no .node file is written. */
+/* */
+/*****************************************************************************/
+
+void numbernodes()
+{
+ point pointloop;
+ int pointnumber;
+
+ traversalinit(&points);
+ pointloop = pointtraverse();
+ pointnumber = firstnumber;
+ while (pointloop != (point) NULL) {
+ setpointmark(pointloop, pointnumber);
+ pointloop = pointtraverse();
+ pointnumber++;
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* writeelements() Write the triangles to an .ele file. */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+void writeelements(trianglelist, triangleattriblist)
+int **trianglelist;
+REAL **triangleattriblist;
+
+#else /* not TRILIBRARY */
+
+void writeelements(elefilename, argc, argv)
+char *elefilename;
+int argc;
+char **argv;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ int *tlist;
+ REAL *talist;
+ int pointindex;
+ int attribindex;
+#else /* not TRILIBRARY */
+ FILE *outfile;
+#endif /* not TRILIBRARY */
+ struct triedge triangleloop;
+ point p1, p2, p3;
+ point mid1, mid2, mid3;
+ int elementnumber;
+ int i;
+
+#ifdef TRILIBRARY
+ if (!quiet) {
+ printf("Writing triangles.\n");
+ }
+ /* Allocate memory for output triangles if necessary. */
+ if (*trianglelist == (int *) NULL) {
+ *trianglelist = (int *) malloc(triangles.items *
+ ((order + 1) * (order + 2) / 2) * sizeof(int));
+ if (*trianglelist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ /* Allocate memory for output triangle attributes if necessary. */
+ if ((eextras > 0) && (*triangleattriblist == (REAL *) NULL)) {
+ *triangleattriblist = (REAL *) malloc(triangles.items * eextras *
+ sizeof(REAL));
+ if (*triangleattriblist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ tlist = *trianglelist;
+ talist = *triangleattriblist;
+ pointindex = 0;
+ attribindex = 0;
+#else /* not TRILIBRARY */
+ if (!quiet) {
+ printf("Writing %s.\n", elefilename);
+ }
+ outfile = fopen(elefilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", elefilename);
+ exit(1);
+ }
+ /* Number of triangles, points per triangle, attributes per triangle. */
+ fprintf(outfile, "%ld %d %d\n", triangles.items,
+ (order + 1) * (order + 2) / 2, eextras);
+#endif /* not TRILIBRARY */
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ triangleloop.orient = 0;
+ elementnumber = firstnumber;
+ while (triangleloop.tri != (triangle *) NULL) {
+ org(triangleloop, p1);
+ dest(triangleloop, p2);
+ apex(triangleloop, p3);
+ if (order == 1) {
+#ifdef TRILIBRARY
+ tlist[pointindex++] = pointmark(p1);
+ tlist[pointindex++] = pointmark(p2);
+ tlist[pointindex++] = pointmark(p3);
+#else /* not TRILIBRARY */
+ /* Triangle number, indices for three points. */
+ fprintf(outfile, "%4d %4d %4d %4d", elementnumber,
+ pointmark(p1), pointmark(p2), pointmark(p3));
+#endif /* not TRILIBRARY */
+ } else {
+ mid1 = (point) triangleloop.tri[highorderindex + 1];
+ mid2 = (point) triangleloop.tri[highorderindex + 2];
+ mid3 = (point) triangleloop.tri[highorderindex];
+#ifdef TRILIBRARY
+ tlist[pointindex++] = pointmark(p1);
+ tlist[pointindex++] = pointmark(p2);
+ tlist[pointindex++] = pointmark(p3);
+ tlist[pointindex++] = pointmark(mid1);
+ tlist[pointindex++] = pointmark(mid2);
+ tlist[pointindex++] = pointmark(mid3);
+#else /* not TRILIBRARY */
+ /* Triangle number, indices for six points. */
+ fprintf(outfile, "%4d %4d %4d %4d %4d %4d %4d", elementnumber,
+ pointmark(p1), pointmark(p2), pointmark(p3), pointmark(mid1),
+ pointmark(mid2), pointmark(mid3));
+#endif /* not TRILIBRARY */
+ }
+
+#ifdef TRILIBRARY
+ for (i = 0; i < eextras; i++) {
+ talist[attribindex++] = elemattribute(triangleloop, i);
+ }
+#else /* not TRILIBRARY */
+ for (i = 0; i < eextras; i++) {
+ fprintf(outfile, " %.17g", elemattribute(triangleloop, i));
+ }
+ fprintf(outfile, "\n");
+#endif /* not TRILIBRARY */
+
+ triangleloop.tri = triangletraverse();
+ elementnumber++;
+ }
+
+#ifndef TRILIBRARY
+ finishfile(outfile, argc, argv);
+#endif /* not TRILIBRARY */
+}
+
+/*****************************************************************************/
+/* */
+/* writepoly() Write the segments and holes to a .poly file. */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+void writepoly(segmentlist, segmentmarkerlist)
+int **segmentlist;
+int **segmentmarkerlist;
+
+#else /* not TRILIBRARY */
+
+void writepoly(polyfilename, holelist, holes, regionlist, regions, argc, argv)
+char *polyfilename;
+REAL *holelist;
+int holes;
+REAL *regionlist;
+int regions;
+int argc;
+char **argv;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ int *slist;
+ int *smlist;
+ int index;
+#else /* not TRILIBRARY */
+ FILE *outfile;
+ int i;
+#endif /* not TRILIBRARY */
+ struct edge shelleloop;
+ point endpoint1, endpoint2;
+ int shellenumber;
+
+#ifdef TRILIBRARY
+ if (!quiet) {
+ printf("Writing segments.\n");
+ }
+ /* Allocate memory for output segments if necessary. */
+ if (*segmentlist == (int *) NULL) {
+ *segmentlist = (int *) malloc(shelles.items * 2 * sizeof(int));
+ if (*segmentlist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ /* Allocate memory for output segment markers if necessary. */
+ if (!nobound && (*segmentmarkerlist == (int *) NULL)) {
+ *segmentmarkerlist = (int *) malloc(shelles.items * sizeof(int));
+ if (*segmentmarkerlist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ slist = *segmentlist;
+ smlist = *segmentmarkerlist;
+ index = 0;
+#else /* not TRILIBRARY */
+ if (!quiet) {
+ printf("Writing %s.\n", polyfilename);
+ }
+ outfile = fopen(polyfilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", polyfilename);
+ exit(1);
+ }
+ /* The zero indicates that the points are in a separate .node file. */
+ /* Followed by number of dimensions, number of point attributes, */
+ /* and number of boundary markers (zero or one). */
+ fprintf(outfile, "%d %d %d %d\n", 0, mesh_dim, nextras, 1 - nobound);
+ /* Number of segments, number of boundary markers (zero or one). */
+ fprintf(outfile, "%ld %d\n", shelles.items, 1 - nobound);
+#endif /* not TRILIBRARY */
+
+ traversalinit(&shelles);
+ shelleloop.sh = shelletraverse();
+ shelleloop.shorient = 0;
+ shellenumber = firstnumber;
+ while (shelleloop.sh != (shelle *) NULL) {
+ sorg(shelleloop, endpoint1);
+ sdest(shelleloop, endpoint2);
+#ifdef TRILIBRARY
+ /* Copy indices of the segment's two endpoints. */
+ slist[index++] = pointmark(endpoint1);
+ slist[index++] = pointmark(endpoint2);
+ if (!nobound) {
+ /* Copy the boundary marker. */
+ smlist[shellenumber - firstnumber] = mark(shelleloop);
+ }
+#else /* not TRILIBRARY */
+ /* Segment number, indices of its two endpoints, and possibly a marker. */
+ if (nobound) {
+ fprintf(outfile, "%4d %4d %4d\n", shellenumber,
+ pointmark(endpoint1), pointmark(endpoint2));
+ } else {
+ fprintf(outfile, "%4d %4d %4d %4d\n", shellenumber,
+ pointmark(endpoint1), pointmark(endpoint2), mark(shelleloop));
+ }
+#endif /* not TRILIBRARY */
+
+ shelleloop.sh = shelletraverse();
+ shellenumber++;
+ }
+
+#ifndef TRILIBRARY
+#ifndef CDT_ONLY
+ fprintf(outfile, "%d\n", holes);
+ if (holes > 0) {
+ for (i = 0; i < holes; i++) {
+ /* Hole number, x and y coordinates. */
+ fprintf(outfile, "%4d %.17g %.17g\n", firstnumber + i,
+ holelist[2 * i], holelist[2 * i + 1]);
+ }
+ }
+ if (regions > 0) {
+ fprintf(outfile, "%d\n", regions);
+ for (i = 0; i < regions; i++) {
+ /* Region number, x and y coordinates, attribute, maximum area. */
+ fprintf(outfile, "%4d %.17g %.17g %.17g %.17g\n", firstnumber + i,
+ regionlist[4 * i], regionlist[4 * i + 1],
+ regionlist[4 * i + 2], regionlist[4 * i + 3]);
+ }
+ }
+#endif /* not CDT_ONLY */
+
+ finishfile(outfile, argc, argv);
+#endif /* not TRILIBRARY */
+}
+
+/*****************************************************************************/
+/* */
+/* writeedges() Write the edges to a .edge file. */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+void writeedges(edgelist, edgemarkerlist)
+int **edgelist;
+int **edgemarkerlist;
+
+#else /* not TRILIBRARY */
+
+void writeedges(edgefilename, argc, argv)
+char *edgefilename;
+int argc;
+char **argv;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ int *elist;
+ int *emlist;
+ int index;
+#else /* not TRILIBRARY */
+ FILE *outfile;
+#endif /* not TRILIBRARY */
+ struct triedge triangleloop, trisym;
+ struct edge checkmark;
+ point p1, p2;
+ int edgenumber;
+ triangle ptr; /* Temporary variable used by sym(). */
+ shelle sptr; /* Temporary variable used by tspivot(). */
+
+#ifdef TRILIBRARY
+ if (!quiet) {
+ printf("Writing edges.\n");
+ }
+ /* Allocate memory for edges if necessary. */
+ if (*edgelist == (int *) NULL) {
+ *edgelist = (int *) malloc(edges * 2 * sizeof(int));
+ if (*edgelist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ /* Allocate memory for edge markers if necessary. */
+ if (!nobound && (*edgemarkerlist == (int *) NULL)) {
+ *edgemarkerlist = (int *) malloc(edges * sizeof(int));
+ if (*edgemarkerlist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ elist = *edgelist;
+ emlist = *edgemarkerlist;
+ index = 0;
+#else /* not TRILIBRARY */
+ if (!quiet) {
+ printf("Writing %s.\n", edgefilename);
+ }
+ outfile = fopen(edgefilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", edgefilename);
+ exit(1);
+ }
+ /* Number of edges, number of boundary markers (zero or one). */
+ fprintf(outfile, "%ld %d\n", edges, 1 - nobound);
+#endif /* not TRILIBRARY */
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ edgenumber = firstnumber;
+ /* To loop over the set of edges, loop over all triangles, and look at */
+ /* the three edges of each triangle. If there isn't another triangle */
+ /* adjacent to the edge, operate on the edge. If there is another */
+ /* adjacent triangle, operate on the edge only if the current triangle */
+ /* has a smaller pointer than its neighbor. This way, each edge is */
+ /* considered only once. */
+ while (triangleloop.tri != (triangle *) NULL) {
+ for (triangleloop.orient = 0; triangleloop.orient < 3;
+ triangleloop.orient++) {
+ sym(triangleloop, trisym);
+ if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
+ org(triangleloop, p1);
+ dest(triangleloop, p2);
+#ifdef TRILIBRARY
+ elist[index++] = pointmark(p1);
+ elist[index++] = pointmark(p2);
+#endif /* TRILIBRARY */
+ if (nobound) {
+#ifndef TRILIBRARY
+ /* Edge number, indices of two endpoints. */
+ fprintf(outfile, "%4d %d %d\n", edgenumber,
+ pointmark(p1), pointmark(p2));
+#endif /* not TRILIBRARY */
+ } else {
+ /* Edge number, indices of two endpoints, and a boundary marker. */
+ /* If there's no shell edge, the boundary marker is zero. */
+ if (useshelles) {
+ tspivot(triangleloop, checkmark);
+ if (checkmark.sh == dummysh) {
+#ifdef TRILIBRARY
+ emlist[edgenumber - firstnumber] = 0;
+#else /* not TRILIBRARY */
+ fprintf(outfile, "%4d %d %d %d\n", edgenumber,
+ pointmark(p1), pointmark(p2), 0);
+#endif /* not TRILIBRARY */
+ } else {
+#ifdef TRILIBRARY
+ emlist[edgenumber - firstnumber] = mark(checkmark);
+#else /* not TRILIBRARY */
+ fprintf(outfile, "%4d %d %d %d\n", edgenumber,
+ pointmark(p1), pointmark(p2), mark(checkmark));
+#endif /* not TRILIBRARY */
+ }
+ } else {
+#ifdef TRILIBRARY
+ emlist[edgenumber - firstnumber] = trisym.tri == dummytri;
+#else /* not TRILIBRARY */
+ fprintf(outfile, "%4d %d %d %d\n", edgenumber,
+ pointmark(p1), pointmark(p2), trisym.tri == dummytri);
+#endif /* not TRILIBRARY */
+ }
+ }
+ edgenumber++;
+ }
+ }
+ triangleloop.tri = triangletraverse();
+ }
+
+#ifndef TRILIBRARY
+ finishfile(outfile, argc, argv);
+#endif /* not TRILIBRARY */
+}
+
+/*****************************************************************************/
+/* */
+/* writevoronoi() Write the Voronoi diagram to a .v.node and .v.edge */
+/* file. */
+/* */
+/* The Voronoi diagram is the geometric dual of the Delaunay triangulation. */
+/* Hence, the Voronoi vertices are listed by traversing the Delaunay */
+/* triangles, and the Voronoi edges are listed by traversing the Delaunay */
+/* edges. */
+/* */
+/* WARNING: In order to assign numbers to the Voronoi vertices, this */
+/* procedure messes up the shell edges or the extra nodes of every */
+/* element. Hence, you should call this procedure last. */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+void writevoronoi(vpointlist, vpointattriblist, vpointmarkerlist, vedgelist,
+ vedgemarkerlist, vnormlist)
+REAL **vpointlist;
+REAL **vpointattriblist;
+int **vpointmarkerlist;
+int **vedgelist;
+int **vedgemarkerlist;
+REAL **vnormlist;
+
+#else /* not TRILIBRARY */
+
+void writevoronoi(vnodefilename, vedgefilename, argc, argv)
+char *vnodefilename;
+char *vedgefilename;
+int argc;
+char **argv;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ REAL *plist;
+ REAL *palist;
+ int *elist;
+ REAL *normlist;
+ int coordindex;
+ int attribindex;
+#else /* not TRILIBRARY */
+ FILE *outfile;
+#endif /* not TRILIBRARY */
+ struct triedge triangleloop, trisym;
+ point torg, tdest, tapex;
+ REAL circumcenter[2];
+ REAL xi, eta;
+ int vnodenumber, vedgenumber;
+ int p1, p2;
+ int i;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+#ifdef TRILIBRARY
+ if (!quiet) {
+ printf("Writing Voronoi vertices.\n");
+ }
+ /* Allocate memory for Voronoi vertices if necessary. */
+ if (*vpointlist == (REAL *) NULL) {
+ *vpointlist = (REAL *) malloc(triangles.items * 2 * sizeof(REAL));
+ if (*vpointlist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ /* Allocate memory for Voronoi vertex attributes if necessary. */
+ if (*vpointattriblist == (REAL *) NULL) {
+ *vpointattriblist = (REAL *) malloc(triangles.items * nextras *
+ sizeof(REAL));
+ if (*vpointattriblist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ *vpointmarkerlist = (int *) NULL;
+ plist = *vpointlist;
+ palist = *vpointattriblist;
+ coordindex = 0;
+ attribindex = 0;
+#else /* not TRILIBRARY */
+ if (!quiet) {
+ printf("Writing %s.\n", vnodefilename);
+ }
+ outfile = fopen(vnodefilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", vnodefilename);
+ exit(1);
+ }
+ /* Number of triangles, two dimensions, number of point attributes, */
+ /* zero markers. */
+ fprintf(outfile, "%ld %d %d %d\n", triangles.items, 2, nextras, 0);
+#endif /* not TRILIBRARY */
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ triangleloop.orient = 0;
+ vnodenumber = firstnumber;
+ while (triangleloop.tri != (triangle *) NULL) {
+ org(triangleloop, torg);
+ dest(triangleloop, tdest);
+ apex(triangleloop, tapex);
+ findcircumcenter(torg, tdest, tapex, circumcenter, &xi, &eta);
+#ifdef TRILIBRARY
+ /* X and y coordinates. */
+ plist[coordindex++] = circumcenter[0];
+ plist[coordindex++] = circumcenter[1];
+ for (i = 2; i < 2 + nextras; i++) {
+ /* Interpolate the point attributes at the circumcenter. */
+ palist[attribindex++] = torg[i] + xi * (tdest[i] - torg[i])
+ + eta * (tapex[i] - torg[i]);
+ }
+#else /* not TRILIBRARY */
+ /* Voronoi vertex number, x and y coordinates. */
+ fprintf(outfile, "%4d %.17g %.17g", vnodenumber, circumcenter[0],
+ circumcenter[1]);
+ for (i = 2; i < 2 + nextras; i++) {
+ /* Interpolate the point attributes at the circumcenter. */
+ fprintf(outfile, " %.17g", torg[i] + xi * (tdest[i] - torg[i])
+ + eta * (tapex[i] - torg[i]));
+ }
+ fprintf(outfile, "\n");
+#endif /* not TRILIBRARY */
+
+ * (int *) (triangleloop.tri + 6) = vnodenumber;
+ triangleloop.tri = triangletraverse();
+ vnodenumber++;
+ }
+
+#ifndef TRILIBRARY
+ finishfile(outfile, argc, argv);
+#endif /* not TRILIBRARY */
+
+#ifdef TRILIBRARY
+ if (!quiet) {
+ printf("Writing Voronoi edges.\n");
+ }
+ /* Allocate memory for output Voronoi edges if necessary. */
+ if (*vedgelist == (int *) NULL) {
+ *vedgelist = (int *) malloc(edges * 2 * sizeof(int));
+ if (*vedgelist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ *vedgemarkerlist = (int *) NULL;
+ /* Allocate memory for output Voronoi norms if necessary. */
+ if (*vnormlist == (REAL *) NULL) {
+ *vnormlist = (REAL *) malloc(edges * 2 * sizeof(REAL));
+ if (*vnormlist == (REAL *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ elist = *vedgelist;
+ normlist = *vnormlist;
+ coordindex = 0;
+#else /* not TRILIBRARY */
+ if (!quiet) {
+ printf("Writing %s.\n", vedgefilename);
+ }
+ outfile = fopen(vedgefilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", vedgefilename);
+ exit(1);
+ }
+ /* Number of edges, zero boundary markers. */
+ fprintf(outfile, "%ld %d\n", edges, 0);
+#endif /* not TRILIBRARY */
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ vedgenumber = firstnumber;
+ /* To loop over the set of edges, loop over all triangles, and look at */
+ /* the three edges of each triangle. If there isn't another triangle */
+ /* adjacent to the edge, operate on the edge. If there is another */
+ /* adjacent triangle, operate on the edge only if the current triangle */
+ /* has a smaller pointer than its neighbor. This way, each edge is */
+ /* considered only once. */
+ while (triangleloop.tri != (triangle *) NULL) {
+ for (triangleloop.orient = 0; triangleloop.orient < 3;
+ triangleloop.orient++) {
+ sym(triangleloop, trisym);
+ if ((triangleloop.tri < trisym.tri) || (trisym.tri == dummytri)) {
+ /* Find the number of this triangle (and Voronoi vertex). */
+ p1 = * (int *) (triangleloop.tri + 6);
+ if (trisym.tri == dummytri) {
+ org(triangleloop, torg);
+ dest(triangleloop, tdest);
+#ifdef TRILIBRARY
+ /* Copy an infinite ray. Index of one endpoint, and -1. */
+ elist[coordindex] = p1;
+ normlist[coordindex++] = tdest[1] - torg[1];
+ elist[coordindex] = -1;
+ normlist[coordindex++] = torg[0] - tdest[0];
+#else /* not TRILIBRARY */
+ /* Write an infinite ray. Edge number, index of one endpoint, -1, */
+ /* and x and y coordinates of a vector representing the */
+ /* direction of the ray. */
+ fprintf(outfile, "%4d %d %d %.17g %.17g\n", vedgenumber,
+ p1, -1, tdest[1] - torg[1], torg[0] - tdest[0]);
+#endif /* not TRILIBRARY */
+ } else {
+ /* Find the number of the adjacent triangle (and Voronoi vertex). */
+ p2 = * (int *) (trisym.tri + 6);
+ /* Finite edge. Write indices of two endpoints. */
+#ifdef TRILIBRARY
+ elist[coordindex] = p1;
+ normlist[coordindex++] = 0.0;
+ elist[coordindex] = p2;
+ normlist[coordindex++] = 0.0;
+#else /* not TRILIBRARY */
+ fprintf(outfile, "%4d %d %d\n", vedgenumber, p1, p2);
+#endif /* not TRILIBRARY */
+ }
+ vedgenumber++;
+ }
+ }
+ triangleloop.tri = triangletraverse();
+ }
+
+#ifndef TRILIBRARY
+ finishfile(outfile, argc, argv);
+#endif /* not TRILIBRARY */
+}
+
+#ifdef TRILIBRARY
+
+void writeneighbors(neighborlist)
+int **neighborlist;
+
+#else /* not TRILIBRARY */
+
+void writeneighbors(neighborfilename, argc, argv)
+char *neighborfilename;
+int argc;
+char **argv;
+
+#endif /* not TRILIBRARY */
+
+{
+#ifdef TRILIBRARY
+ int *nlist;
+ int index;
+#else /* not TRILIBRARY */
+ FILE *outfile;
+#endif /* not TRILIBRARY */
+ struct triedge triangleloop, trisym;
+ int elementnumber;
+ int neighbor1, neighbor2, neighbor3;
+ triangle ptr; /* Temporary variable used by sym(). */
+
+#ifdef TRILIBRARY
+ if (!quiet) {
+ printf("Writing neighbors.\n");
+ }
+ /* Allocate memory for neighbors if necessary. */
+ if (*neighborlist == (int *) NULL) {
+ *neighborlist = (int *) malloc(triangles.items * 3 * sizeof(int));
+ if (*neighborlist == (int *) NULL) {
+ printf("Error: Out of memory.\n");
+ exit(1);
+ }
+ }
+ nlist = *neighborlist;
+ index = 0;
+#else /* not TRILIBRARY */
+ if (!quiet) {
+ printf("Writing %s.\n", neighborfilename);
+ }
+ outfile = fopen(neighborfilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", neighborfilename);
+ exit(1);
+ }
+ /* Number of triangles, three edges per triangle. */
+ fprintf(outfile, "%ld %d\n", triangles.items, 3);
+#endif /* not TRILIBRARY */
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ triangleloop.orient = 0;
+ elementnumber = firstnumber;
+ while (triangleloop.tri != (triangle *) NULL) {
+ * (int *) (triangleloop.tri + 6) = elementnumber;
+ triangleloop.tri = triangletraverse();
+ elementnumber++;
+ }
+ * (int *) (dummytri + 6) = -1;
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ elementnumber = firstnumber;
+ while (triangleloop.tri != (triangle *) NULL) {
+ triangleloop.orient = 1;
+ sym(triangleloop, trisym);
+ neighbor1 = * (int *) (trisym.tri + 6);
+ triangleloop.orient = 2;
+ sym(triangleloop, trisym);
+ neighbor2 = * (int *) (trisym.tri + 6);
+ triangleloop.orient = 0;
+ sym(triangleloop, trisym);
+ neighbor3 = * (int *) (trisym.tri + 6);
+#ifdef TRILIBRARY
+ nlist[index++] = neighbor1;
+ nlist[index++] = neighbor2;
+ nlist[index++] = neighbor3;
+#else /* not TRILIBRARY */
+ /* Triangle number, neighboring triangle numbers. */
+ fprintf(outfile, "%4d %d %d %d\n", elementnumber,
+ neighbor1, neighbor2, neighbor3);
+#endif /* not TRILIBRARY */
+
+ triangleloop.tri = triangletraverse();
+ elementnumber++;
+ }
+
+#ifndef TRILIBRARY
+ finishfile(outfile, argc, argv);
+#endif /* TRILIBRARY */
+}
+
+/*****************************************************************************/
+/* */
+/* writeoff() Write the triangulation to an .off file. */
+/* */
+/* OFF stands for the Object File Format, a format used by the Geometry */
+/* Center's Geomview package. */
+/* */
+/*****************************************************************************/
+
+#ifndef TRILIBRARY
+
+void writeoff(offfilename, argc, argv)
+char *offfilename;
+int argc;
+char **argv;
+{
+ FILE *outfile;
+ struct triedge triangleloop;
+ point pointloop;
+ point p1, p2, p3;
+
+ if (!quiet) {
+ printf("Writing %s.\n", offfilename);
+ }
+ outfile = fopen(offfilename, "w");
+ if (outfile == (FILE *) NULL) {
+ printf(" Error: Cannot create file %s.\n", offfilename);
+ exit(1);
+ }
+ /* Number of points, triangles, and edges. */
+ fprintf(outfile, "OFF\n%ld %ld %ld\n", points.items, triangles.items,
+ edges);
+
+ /* Write the points. */
+ traversalinit(&points);
+ pointloop = pointtraverse();
+ while (pointloop != (point) NULL) {
+ /* The "0.0" is here because the OFF format uses 3D coordinates. */
+ fprintf(outfile, " %.17g %.17g %.17g\n", pointloop[0],
+ pointloop[1], 0.0);
+ pointloop = pointtraverse();
+ }
+
+ /* Write the triangles. */
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ triangleloop.orient = 0;
+ while (triangleloop.tri != (triangle *) NULL) {
+ org(triangleloop, p1);
+ dest(triangleloop, p2);
+ apex(triangleloop, p3);
+ /* The "3" means a three-vertex polygon. */
+ fprintf(outfile, " 3 %4d %4d %4d\n", pointmark(p1) - 1,
+ pointmark(p2) - 1, pointmark(p3) - 1);
+ triangleloop.tri = triangletraverse();
+ }
+ finishfile(outfile, argc, argv);
+}
+
+#endif /* not TRILIBRARY */
+
+/** **/
+/** **/
+/********* File I/O routines end here *********/
+
+/*****************************************************************************/
+/* */
+/* quality_statistics() Print statistics about the quality of the mesh. */
+/* */
+/*****************************************************************************/
+
+void quality_statistics()
+{
+ struct triedge triangleloop;
+ point p[3];
+ REAL cossquaretable[8];
+ REAL ratiotable[16];
+ REAL dx[3], dy[3];
+ REAL edgelength[3];
+ REAL dotproduct;
+ REAL cossquare;
+ REAL triarea;
+ REAL shortest, longest;
+ REAL trilongest2;
+ REAL smallestarea, biggestarea;
+ REAL triminaltitude2;
+ REAL minaltitude;
+ REAL triaspect2;
+ REAL worstaspect;
+ REAL smallestangle, biggestangle;
+ REAL radconst, degconst;
+ int angletable[18];
+ int aspecttable[16];
+ int aspectindex;
+ int tendegree;
+ int acutebiggest;
+ int i, ii, j, k;
+
+ printf("Mesh quality statistics:\n\n");
+ radconst = (REAL)(PI / 18.0);
+ degconst = (REAL)(180.0 / PI);
+ for (i = 0; i < 8; i++) {
+ cossquaretable[i] = (REAL)(cos(radconst * (REAL) (i + 1)));
+ cossquaretable[i] = cossquaretable[i] * cossquaretable[i];
+ }
+ for (i = 0; i < 18; i++) {
+ angletable[i] = 0;
+ }
+
+ ratiotable[0] = 1.5; ratiotable[1] = 2.0;
+ ratiotable[2] = 2.5; ratiotable[3] = 3.0;
+ ratiotable[4] = 4.0; ratiotable[5] = 6.0;
+ ratiotable[6] = 10.0; ratiotable[7] = 15.0;
+ ratiotable[8] = 25.0; ratiotable[9] = 50.0;
+ ratiotable[10] = 100.0; ratiotable[11] = 300.0;
+ ratiotable[12] = 1000.0; ratiotable[13] = 10000.0;
+ ratiotable[14] = 100000.0; ratiotable[15] = 0.0;
+ for (i = 0; i < 16; i++) {
+ aspecttable[i] = 0;
+ }
+
+ worstaspect = 0.0;
+ minaltitude = xmax - xmin + ymax - ymin;
+ minaltitude = minaltitude * minaltitude;
+ shortest = minaltitude;
+ longest = 0.0;
+ smallestarea = minaltitude;
+ biggestarea = 0.0;
+ worstaspect = 0.0;
+ smallestangle = 0.0;
+ biggestangle = 2.0;
+ acutebiggest = 1;
+
+ traversalinit(&triangles);
+ triangleloop.tri = triangletraverse();
+ triangleloop.orient = 0;
+ while (triangleloop.tri != (triangle *) NULL) {
+ org(triangleloop, p[0]);
+ dest(triangleloop, p[1]);
+ apex(triangleloop, p[2]);
+ trilongest2 = 0.0;
+
+ for (i = 0; i < 3; i++) {
+ j = plus1mod3[i];
+ k = minus1mod3[i];
+ dx[i] = p[j][0] - p[k][0];
+ dy[i] = p[j][1] - p[k][1];
+ edgelength[i] = dx[i] * dx[i] + dy[i] * dy[i];
+ if (edgelength[i] > trilongest2) {
+ trilongest2 = edgelength[i];
+ }
+ if (edgelength[i] > longest) {
+ longest = edgelength[i];
+ }
+ if (edgelength[i] < shortest) {
+ shortest = edgelength[i];
+ }
+ }
+
+ triarea = counterclockwise(p[0], p[1], p[2]);
+ if (triarea < smallestarea) {
+ smallestarea = triarea;
+ }
+ if (triarea > biggestarea) {
+ biggestarea = triarea;
+ }
+ triminaltitude2 = triarea * triarea / trilongest2;
+ if (triminaltitude2 < minaltitude) {
+ minaltitude = triminaltitude2;
+ }
+ triaspect2 = trilongest2 / triminaltitude2;
+ if (triaspect2 > worstaspect) {
+ worstaspect = triaspect2;
+ }
+ aspectindex = 0;
+ while ((triaspect2 > ratiotable[aspectindex] * ratiotable[aspectindex])
+ && (aspectindex < 15)) {
+ aspectindex++;
+ }
+ aspecttable[aspectindex]++;
+
+ for (i = 0; i < 3; i++) {
+ j = plus1mod3[i];
+ k = minus1mod3[i];
+ dotproduct = dx[j] * dx[k] + dy[j] * dy[k];
+ cossquare = dotproduct * dotproduct / (edgelength[j] * edgelength[k]);
+ tendegree = 8;
+ for (ii = 7; ii >= 0; ii--) {
+ if (cossquare > cossquaretable[ii]) {
+ tendegree = ii;
+ }
+ }
+ if (dotproduct <= 0.0) {
+ angletable[tendegree]++;
+ if (cossquare > smallestangle) {
+ smallestangle = cossquare;
+ }
+ if (acutebiggest && (cossquare < biggestangle)) {
+ biggestangle = cossquare;
+ }
+ } else {
+ angletable[17 - tendegree]++;
+ if (acutebiggest || (cossquare > biggestangle)) {
+ biggestangle = cossquare;
+ acutebiggest = 0;
+ }
+ }
+ }
+ triangleloop.tri = triangletraverse();
+ }
+
+ shortest = (REAL)sqrt(shortest);
+ longest = (REAL)sqrt(longest);
+ minaltitude = (REAL)sqrt(minaltitude);
+ worstaspect = (REAL)sqrt(worstaspect);
+ smallestarea *= 2.0;
+ biggestarea *= 2.0;
+ if (smallestangle >= 1.0) {
+ smallestangle = 0.0;
+ } else {
+ smallestangle = (REAL)(degconst * acos(sqrt(smallestangle)));
+ }
+ if (biggestangle >= 1.0) {
+ biggestangle = 180.0;
+ } else {
+ if (acutebiggest) {
+ biggestangle = (REAL)(degconst * acos(sqrt(biggestangle)));
+ } else {
+ biggestangle = (REAL)(180.0 - degconst * acos(sqrt(biggestangle)));
+ }
+ }
+
+ printf(" Smallest area: %16.5g | Largest area: %16.5g\n",
+ smallestarea, biggestarea);
+ printf(" Shortest edge: %16.5g | Longest edge: %16.5g\n",
+ shortest, longest);
+ printf(" Shortest altitude: %12.5g | Largest aspect ratio: %8.5g\n\n",
+ minaltitude, worstaspect);
+ printf(" Aspect ratio histogram:\n");
+ printf(" 1.1547 - %-6.6g : %8d | %6.6g - %-6.6g : %8d\n",
+ ratiotable[0], aspecttable[0], ratiotable[7], ratiotable[8],
+ aspecttable[8]);
+ for (i = 1; i < 7; i++) {
+ printf(" %6.6g - %-6.6g : %8d | %6.6g - %-6.6g : %8d\n",
+ ratiotable[i - 1], ratiotable[i], aspecttable[i],
+ ratiotable[i + 7], ratiotable[i + 8], aspecttable[i + 8]);
+ }
+ printf(" %6.6g - %-6.6g : %8d | %6.6g - : %8d\n",
+ ratiotable[6], ratiotable[7], aspecttable[7], ratiotable[14],
+ aspecttable[15]);
+ printf(
+" (Triangle aspect ratio is longest edge divided by shortest altitude)\n\n");
+ printf(" Smallest angle: %15.5g | Largest angle: %15.5g\n\n",
+ smallestangle, biggestangle);
+ printf(" Angle histogram:\n");
+ for (i = 0; i < 9; i++) {
+ printf(" %3d - %3d degrees: %8d | %3d - %3d degrees: %8d\n",
+ i * 10, i * 10 + 10, angletable[i],
+ i * 10 + 90, i * 10 + 100, angletable[i + 9]);
+ }
+ printf("\n");
+}
+
+/*****************************************************************************/
+/* */
+/* statistics() Print all sorts of cool facts. */
+/* */
+/*****************************************************************************/
+
+void statistics()
+{
+ printf("\nStatistics:\n\n");
+ printf(" Input points: %d\n", inpoints);
+ if (refine) {
+ printf(" Input triangles: %d\n", inelements);
+ }
+ if (poly) {
+ printf(" Input segments: %d\n", insegments);
+ if (!refine) {
+ printf(" Input holes: %d\n", holes);
+ }
+ }
+
+ printf("\n Mesh points: %ld\n", points.items);
+ printf(" Mesh triangles: %ld\n", triangles.items);
+ printf(" Mesh edges: %ld\n", edges);
+ if (poly || refine) {
+ printf(" Mesh boundary edges: %ld\n", hullsize);
+ printf(" Mesh segments: %ld\n\n", shelles.items);
+ } else {
+ printf(" Mesh convex hull edges: %ld\n\n", hullsize);
+ }
+ if (verbose) {
+ quality_statistics();
+ printf("Memory allocation statistics:\n\n");
+ printf(" Maximum number of points: %ld\n", points.maxitems);
+ printf(" Maximum number of triangles: %ld\n", triangles.maxitems);
+ if (shelles.maxitems > 0) {
+ printf(" Maximum number of segments: %ld\n", shelles.maxitems);
+ }
+ if (viri.maxitems > 0) {
+ printf(" Maximum number of viri: %ld\n", viri.maxitems);
+ }
+ if (badsegments.maxitems > 0) {
+ printf(" Maximum number of encroached segments: %ld\n",
+ badsegments.maxitems);
+ }
+ if (badtriangles.maxitems > 0) {
+ printf(" Maximum number of bad triangles: %ld\n",
+ badtriangles.maxitems);
+ }
+ if (splaynodes.maxitems > 0) {
+ printf(" Maximum number of splay tree nodes: %ld\n",
+ splaynodes.maxitems);
+ }
+ printf(" Approximate heap memory use (bytes): %ld\n\n",
+ points.maxitems * points.itembytes
+ + triangles.maxitems * triangles.itembytes
+ + shelles.maxitems * shelles.itembytes
+ + viri.maxitems * viri.itembytes
+ + badsegments.maxitems * badsegments.itembytes
+ + badtriangles.maxitems * badtriangles.itembytes
+ + splaynodes.maxitems * splaynodes.itembytes);
+
+ printf("Algorithmic statistics:\n\n");
+ printf(" Number of incircle tests: %ld\n", incirclecount);
+ printf(" Number of orientation tests: %ld\n", counterclockcount);
+ if (hyperbolacount > 0) {
+ printf(" Number of right-of-hyperbola tests: %ld\n",
+ hyperbolacount);
+ }
+ if (circumcentercount > 0) {
+ printf(" Number of circumcenter computations: %ld\n",
+ circumcentercount);
+ }
+ if (circletopcount > 0) {
+ printf(" Number of circle top computations: %ld\n",
+ circletopcount);
+ }
+ printf("\n");
+ }
+}
+
+/*****************************************************************************/
+/* */
+/* main() or triangulate() Gosh, do everything. */
+/* */
+/* The sequence is roughly as follows. Many of these steps can be skipped, */
+/* depending on the command line switches. */
+/* */
+/* - Initialize constants and parse the command line. */
+/* - Read the points from a file and either */
+/* - triangulate them (no -r), or */
+/* - read an old mesh from files and reconstruct it (-r). */
+/* - Insert the PSLG segments (-p), and possibly segments on the convex */
+/* hull (-c). */
+/* - Read the holes (-p), regional attributes (-pA), and regional area */
+/* constraints (-pa). Carve the holes and concavities, and spread the */
+/* regional attributes and area constraints. */
+/* - Enforce the constraints on minimum angle (-q) and maximum area (-a). */
+/* Also enforce the conforming Delaunay property (-q and -a). */
+/* - Compute the number of edges in the resulting mesh. */
+/* - Promote the mesh's linear triangles to higher order elements (-o). */
+/* - Write the output files and print the statistics. */
+/* - Check the consistency and Delaunay property of the mesh (-C). */
+/* */
+/*****************************************************************************/
+
+#ifdef TRILIBRARY
+
+void triangulate(triswitches, in, out, vorout)
+char *triswitches;
+struct triangulateio *in;
+struct triangulateio *out;
+struct triangulateio *vorout;
+
+#else /* not TRILIBRARY */
+
+int main(argc, argv)
+int argc;
+char **argv;
+
+#endif /* not TRILIBRARY */
+
+{
+ REAL *holearray; /* Array of holes. */
+ REAL *regionarray; /* Array of regional attributes and area constraints. */
+#ifndef TRILIBRARY
+ FILE *polyfile;
+#endif /* not TRILIBRARY */
+#ifndef NO_TIMER
+ /* Variables for timing the performance of Triangle. The types are */
+ /* defined in sys/time.h. */
+ struct timeval tv0, tv1, tv2, tv3, tv4, tv5, tv6;
+ struct timezone tz;
+#endif /* NO_TIMER */
+
+#ifndef NO_TIMER
+ gettimeofday(&tv0, &tz);
+#endif /* NO_TIMER */
+
+ triangleinit();
+#ifdef TRILIBRARY
+ parsecommandline(1, &triswitches);
+#else /* not TRILIBRARY */
+ parsecommandline(argc, argv);
+#endif /* not TRILIBRARY */
+
+#ifdef TRILIBRARY
+ transfernodes(in->pointlist, in->pointattributelist, in->pointmarkerlist,
+ in->numberofpoints, in->numberofpointattributes);
+#else /* not TRILIBRARY */
+ readnodes(innodefilename, inpolyfilename, &polyfile);
+#endif /* not TRILIBRARY */
+
+#ifndef NO_TIMER
+ if (!quiet) {
+ gettimeofday(&tv1, &tz);
+ }
+#endif /* NO_TIMER */
+
+#ifdef CDT_ONLY
+ hullsize = delaunay(); /* Triangulate the points. */
+#else /* not CDT_ONLY */
+ if (refine) {
+ /* Read and reconstruct a mesh. */
+#ifdef TRILIBRARY
+ hullsize = reconstruct(in->trianglelist, in->triangleattributelist,
+ in->trianglearealist, in->numberoftriangles,
+ in->numberofcorners, in->numberoftriangleattributes,
+ in->segmentlist, in->segmentmarkerlist,
+ in->numberofsegments);
+#else /* not TRILIBRARY */
+ hullsize = reconstruct(inelefilename, areafilename, inpolyfilename,
+ polyfile);
+#endif /* not TRILIBRARY */
+ } else {
+ hullsize = delaunay(); /* Triangulate the points. */
+ }
+#endif /* not CDT_ONLY */
+
+#ifndef NO_TIMER
+ if (!quiet) {
+ gettimeofday(&tv2, &tz);
+ if (refine) {
+ printf("Mesh reconstruction");
+ } else {
+ printf("Delaunay");
+ }
+ printf(" milliseconds: %ld\n", 1000l * (tv2.tv_sec - tv1.tv_sec)
+ + (tv2.tv_usec - tv1.tv_usec) / 1000l);
+ }
+#endif /* NO_TIMER */
+
+ /* Ensure that no point can be mistaken for a triangular bounding */
+ /* box point in insertsite(). */
+ infpoint1 = (point) NULL;
+ infpoint2 = (point) NULL;
+ infpoint3 = (point) NULL;
+
+ if (useshelles) {
+ checksegments = 1; /* Segments will be introduced next. */
+ if (!refine) {
+ /* Insert PSLG segments and/or convex hull segments. */
+#ifdef TRILIBRARY
+ insegments = formskeleton(in->segmentlist, in->segmentmarkerlist,
+ in->numberofsegments);
+#else /* not TRILIBRARY */
+ insegments = formskeleton(polyfile, inpolyfilename);
+#endif /* not TRILIBRARY */
+ }
+ }
+
+#ifndef NO_TIMER
+ if (!quiet) {
+ gettimeofday(&tv3, &tz);
+ if (useshelles && !refine) {
+ printf("Segment milliseconds: %ld\n",
+ 1000l * (tv3.tv_sec - tv2.tv_sec)
+ + (tv3.tv_usec - tv2.tv_usec) / 1000l);
+ }
+ }
+#endif /* NO_TIMER */
+
+ if (poly) {
+#ifdef TRILIBRARY
+ holearray = in->holelist;
+ holes = in->numberofholes;
+ regionarray = in->regionlist;
+ regions = in->numberofregions;
+#else /* not TRILIBRARY */
+ readholes(polyfile, inpolyfilename, &holearray, &holes,
+ ®ionarray, ®ions);
+#endif /* not TRILIBRARY */
+ if (!refine) {
+ /* Carve out holes and concavities. */
+ carveholes(holearray, holes, regionarray, regions);
+ }
+ } else {
+ /* Without a PSLG, there can be no holes or regional attributes */
+ /* or area constraints. The following are set to zero to avoid */
+ /* an accidental free() later. */
+ holes = 0;
+ regions = 0;
+ }
+
+#ifndef NO_TIMER
+ if (!quiet) {
+ gettimeofday(&tv4, &tz);
+ if (poly && !refine) {
+ printf("Hole milliseconds: %ld\n", 1000l * (tv4.tv_sec - tv3.tv_sec)
+ + (tv4.tv_usec - tv3.tv_usec) / 1000l);
+ }
+ }
+#endif /* NO_TIMER */
+
+#ifndef CDT_ONLY
+ if (quality) {
+ enforcequality(); /* Enforce angle and area constraints. */
+ }
+#endif /* not CDT_ONLY */
+
+#ifndef NO_TIMER
+ if (!quiet) {
+ gettimeofday(&tv5, &tz);
+#ifndef CDT_ONLY
+ if (quality) {
+ printf("Quality milliseconds: %ld\n",
+ 1000l * (tv5.tv_sec - tv4.tv_sec)
+ + (tv5.tv_usec - tv4.tv_usec) / 1000l);
+ }
+#endif /* not CDT_ONLY */
+ }
+#endif /* NO_TIMER */
+
+ /* Compute the number of edges. */
+ edges = (3l * triangles.items + hullsize) / 2l;
+
+ if (order > 1) {
+ highorder(); /* Promote elements to higher polynomial order. */
+ }
+ if (!quiet) {
+ printf("\n");
+ }
+
+#ifdef TRILIBRARY
+ out->numberofpoints = points.items;
+ out->numberofpointattributes = nextras;
+ out->numberoftriangles = triangles.items;
+ out->numberofcorners = (order + 1) * (order + 2) / 2;
+ out->numberoftriangleattributes = eextras;
+ out->numberofedges = edges;
+ if (useshelles) {
+ out->numberofsegments = shelles.items;
+ } else {
+ out->numberofsegments = hullsize;
+ }
+ if (vorout != (struct triangulateio *) NULL) {
+ vorout->numberofpoints = triangles.items;
+ vorout->numberofpointattributes = nextras;
+ vorout->numberofedges = edges;
+ }
+#endif /* TRILIBRARY */
+ /* If not using iteration numbers, don't write a .node file if one was */
+ /* read, because the original one would be overwritten! */
+ if (nonodewritten || (noiterationnum && readnodefile)) {
+ if (!quiet) {
+#ifdef TRILIBRARY
+ printf("NOT writing points.\n");
+#else /* not TRILIBRARY */
+ printf("NOT writing a .node file.\n");
+#endif /* not TRILIBRARY */
+ }
+ numbernodes(); /* We must remember to number the points. */
+ } else {
+#ifdef TRILIBRARY
+ writenodes(&out->pointlist, &out->pointattributelist,
+ &out->pointmarkerlist);
+#else /* not TRILIBRARY */
+ writenodes(outnodefilename, argc, argv); /* Numbers the points too. */
+#endif /* TRILIBRARY */
+ }
+ if (noelewritten) {
+ if (!quiet) {
+#ifdef TRILIBRARY
+ printf("NOT writing triangles.\n");
+#else /* not TRILIBRARY */
+ printf("NOT writing an .ele file.\n");
+#endif /* not TRILIBRARY */
+ }
+ } else {
+#ifdef TRILIBRARY
+ writeelements(&out->trianglelist, &out->triangleattributelist);
+#else /* not TRILIBRARY */
+ writeelements(outelefilename, argc, argv);
+#endif /* not TRILIBRARY */
+ }
+ /* The -c switch (convex switch) causes a PSLG to be written */
+ /* even if none was read. */
+ if (poly || convex) {
+ /* If not using iteration numbers, don't overwrite the .poly file. */
+ if (nopolywritten || noiterationnum) {
+ if (!quiet) {
+#ifdef TRILIBRARY
+ printf("NOT writing segments.\n");
+#else /* not TRILIBRARY */
+ printf("NOT writing a .poly file.\n");
+#endif /* not TRILIBRARY */
+ }
+ } else {
+#ifdef TRILIBRARY
+ writepoly(&out->segmentlist, &out->segmentmarkerlist);
+ out->numberofholes = holes;
+ out->numberofregions = regions;
+ if (poly) {
+ out->holelist = in->holelist;
+ out->regionlist = in->regionlist;
+ } else {
+ out->holelist = (REAL *) NULL;
+ out->regionlist = (REAL *) NULL;
+ }
+#else /* not TRILIBRARY */
+ writepoly(outpolyfilename, holearray, holes, regionarray, regions,
+ argc, argv);
+#endif /* not TRILIBRARY */
+ }
+ }
+#ifndef TRILIBRARY
+#ifndef CDT_ONLY
+ if (regions > 0) {
+ free(regionarray);
+ }
+#endif /* not CDT_ONLY */
+ if (holes > 0) {
+ free(holearray);
+ }
+ if (geomview) {
+ writeoff(offfilename, argc, argv);
+ }
+#endif /* not TRILIBRARY */
+ if (edgesout) {
+#ifdef TRILIBRARY
+ writeedges(&out->edgelist, &out->edgemarkerlist);
+#else /* not TRILIBRARY */
+ writeedges(edgefilename, argc, argv);
+#endif /* not TRILIBRARY */
+ }
+ if (voronoi) {
+#ifdef TRILIBRARY
+ writevoronoi(&vorout->pointlist, &vorout->pointattributelist,
+ &vorout->pointmarkerlist, &vorout->edgelist,
+ &vorout->edgemarkerlist, &vorout->normlist);
+#else /* not TRILIBRARY */
+ writevoronoi(vnodefilename, vedgefilename, argc, argv);
+#endif /* not TRILIBRARY */
+ }
+ if (neighbors) {
+#ifdef TRILIBRARY
+ writeneighbors(&out->neighborlist);
+#else /* not TRILIBRARY */
+ writeneighbors(neighborfilename, argc, argv);
+#endif /* not TRILIBRARY */
+ }
+
+ if (!quiet) {
+#ifndef NO_TIMER
+ gettimeofday(&tv6, &tz);
+ printf("\nOutput milliseconds: %ld\n",
+ 1000l * (tv6.tv_sec - tv5.tv_sec)
+ + (tv6.tv_usec - tv5.tv_usec) / 1000l);
+ printf("Total running milliseconds: %ld\n",
+ 1000l * (tv6.tv_sec - tv0.tv_sec)
+ + (tv6.tv_usec - tv0.tv_usec) / 1000l);
+#endif /* NO_TIMER */
+
+ statistics();
+ }
+
+#ifndef REDUCED
+ if (docheck) {
+ checkmesh();
+ checkdelaunay();
+ }
+#endif /* not REDUCED */
+
+ triangledeinit();
+#ifndef TRILIBRARY
+ return 0;
+#endif /* not TRILIBRARY */
+}