📄 triangle.cpp
字号:
/* 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 void
/* 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -