Debian Codegraph @

debian/external_deps/fetk/gamer/src/tetgen/src_c/tetgen.h

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 // //
3 // TetGen //
4 // //
5 // A Quality Tetrahedral Mesh Generator and 3D Delaunay Triangulator //
6 // //
7 // Version 1.4 //
8 // January 14, 2006 //
9 // //
10 // Copyright 2002, 2004, 2005, 2006 //
11 // Hang Si //
12 // Rathausstr. 9, 10178 Berlin, Germany //
13 // [email protected] //
14 // //
15 // You can obtain TetGen via internet: http://tetgen.berlios.de. It may be //
16 // freely copied, modified, and redistributed under the copyright notices //
17 // given in the file LICENSE. //
18 // //
19 // TetGen computes Delaunay tetrahedralizations, constrained Delaunay tetra- //
20 // hedralizations, and quality Delaunay tetrahedral meshes. The latter are //
21 // nicely graded and whose tetrahedra have radius-edge ratio bounded. Such //
22 // meshes are suitable for finite element and finite volume methods. //
23 // //
24 // TetGen incorporates a suit of geometrical and mesh generation algorithms. //
25 // A brief description of algorithms used in TetGen is found in the first //
26 // section of the user's manual. References are given for users who are //
27 // interesting in these approaches. The main references are given below: //
28 // //
29 // The efficient Delaunay tetrahedralization algorithm is: H. Edelsbrunner //
30 // and N. R. Shah, "Incremental Topological Flipping Works for Regular //
31 // Triangulations". Algorithmica 15: 223-241, 1996. //
32 // //
33 // The constrained Delaunay tetrahedralization algorithm is described in: //
34 // H. Si and K. Gaertner, "Meshing Piecewise Linear Complexes by Constr- //
35 // ained Delaunay Tetrahedralizations". In Proceeding of the 14th Inter- //
36 // national Meshing Roundtable. September 2005. //
37 // //
38 // The Delaunay refinement algorithm is from: Hang Si, "On Refinement of //
39 // Constrained Delaunay Tetrahedralizations". In Proceeding of the 15th //
40 // International Meshing Roundtable. September 2006. //
41 // //
42 // The mesh data structure of TetGen is a combination of two types of mesh //
43 // data structures. The tetrahedron-based mesh data structure introduced //
44 // by Shewchuk is eligible for tetrahedralization algorithms. The triangle //
45 // -edge data structure developed by Muecke is adopted for representing //
46 // boundary elements: subfaces and subsegments. Both data structures have //
47 // a set of fast mesh manipulation primitives. //
48 // //
49 // J. R. Shewchuk, "Delaunay Refinement Mesh Generation". PhD thesis, //
50 // Carnegie Mellon University, 1997. //
51 // //
52 // E. P. Muecke, "Shapes and Implementations in Three-Dimensional //
53 // Geometry". PhD thesis, Univ. of Illinois, Urbana, Illinois, 1993. //
54 // //
55 // The research of mesh generation is definitly on the move. Many State-of- //
56 // the-art algorithms need implementing and evaluating. I heartily welcome //
57 // any new algorithm especially for generating quality conforming Delaunay //
58 // meshes and anisotropic conforming Delaunay meshes. //
59 // //
60 // TetGen is supported by the "pdelib" project of Weierstrass Institute for //
61 // Applied Analysis and Stochastics (WIAS) in Berlin. It is a collection //
62 // of software components for solving non-linear partial differential //
63 // equations including 2D and 3D mesh generators, sparse matrix solvers, //
64 // and scientific visualization tools, etc. For more information please //
65 // visit: http://www.wias-berlin.de/software/pdelib. //
66 // //
67 ///////////////////////////////////////////////////////////////////////////////
68
69 ///////////////////////////////////////////////////////////////////////////////
70 // //
71 // tetgen.h //
72 // //
73 // Header file of the TetGen library. Also is the user-level header file. //
74 // //
75 ///////////////////////////////////////////////////////////////////////////////
76
77 ///////////////////////////////////////////////////////////////////////////////
78 // //
79 // TetGen Library Overview //
80 // //
81 // TetGen library is comprised by several data types and global functions. //
82 // //
83 // There are three main data types: tetgenio, tetgenbehavior, and tetgenmesh.//
84 // Tetgenio is used to pass data into and out of TetGen library; tetgenbeha- //
85 // vior keeps the runtime options and thus controls the behaviors of TetGen; //
86 // tetgenmesh, the biggest data type I've ever defined, contains mesh data //
87 // structures and mesh traversing and transformation operators. The meshing //
88 // algorithms are implemented on top of it. These data types are defined as //
89 // C++ classes. //
90 // //
91 // There are few global functions. tetrahedralize() is provided for calling //
92 // TetGen from another program. Two functions: orient3d() and insphere() are //
93 // incorporated from a public C code provided by Shewchuk. They performing //
94 // exact geometrical tests. //
95 // //
96 ///////////////////////////////////////////////////////////////////////////////
97
98 #ifndef tetgenH
99 #define tetgenH
100
101 // To compile TetGen as a library instead of an executable program, define
102 // the TETLIBRARY symbol.
103
104 // #define TETLIBRARY
105
106 // Uncomment the following line to disable assert macros. These macros are
107 // inserted in places where I hope to catch bugs.
108
109 // #define NDEBUG
110
111 // To insert lots of self-checks for internal errors, define the SELF_CHECK
112 // symbol. This will slow down the program significantly.
113
114 // #define SELF_CHECK
115
116 // For single precision ( which will save some memory and reduce paging ),
117 // define the symbol SINGLE by using the -DSINGLE compiler switch or by
118 // writing "#define SINGLE" below.
119 //
120 // For double precision ( which will allow you to refine meshes to a smaller
121 // edge length), leave SINGLE undefined.
122
123 // #define SINGLE
124
125 #ifdef SINGLE
126 #define REAL float
127 #else
128 #define REAL double
129 #endif // not defined SINGLE
130
131 // Here are the most general used head files for C/C++ programs.
132
133 #include <stdio.h> // Standard IO: FILE, NULL, EOF, printf(), ...
134 #include <stdlib.h> // Standard lib: abort(), system(), getenv(), ...
135 #include <string.h> // String lib: strcpy(), strcat(), strcmp(), ...
136 #include <math.h> // Math lib: sin(), sqrt(), pow(), ...
137 #include <assert.h>
138
139 ///////////////////////////////////////////////////////////////////////////////
140 // //
141 // The tetgenio data type //
142 // //
143 // Used to pass data into and out of the library of TetGen. //
144 // //
145 // If you want to program with the library of TetGen, it's necessary for you //
146 // to understand the tetgenio data type, while the other two data types can //
147 // be hidden through calling the global function "tetrahedralize()". As you //
148 // will see, that tetgenio is just a collection of arrays to storing points //
149 // (by coodinates), tetrahedra (by indexes), faces, boundary markers, and so //
150 // forth. Each array corresponds to a list of data in the file formats of //
151 // TetGen. It is necessary to understand TetGen's input/output file formats //
152 // (see user's manual) before using tetgenio objects. //
153 // //
154 // Once an object of tetgenio is declared (or created), all arrays of it are //
155 // automatically initialized to NULLs (by routine initialize()). Before they //
156 // can be used, one has to first allocate enough memory for them, i.e., use //
157 // either 'malloc()' or 'new' operator. On deletion of the object, one needs //
158 // to free the memory occupied by these arrays. Routine deinitialize() will //
159 // be automatically called. It will deallocate the memory for an array if it //
160 // is not a NULL. However, it assumes that the memory is allocated by 'new' //
161 // (C++ operator). If you use malloc(), you should free() them and set the //
162 // pointers to NULLs before reaching deinitialize(). //
163 // //
164 // In all cases, the first item in any array is stored starting at index [0].//
165 // However, that item is item number `firstnumber' which may be '0' or '1'. //
166 // Be sure to set the 'firstnumber' be '1' if your indices pointing into the //
167 // pointlist is starting from '1'. Default, it is initialized be '0'. //
168 // //
169 // Tetgenio also contains routines for reading and writing TetGen's files as //
170 // well. Both the library of TetGen and TetView use these routines to parse //
171 // input files, i.e., .node, .poly, .smesh, .ele, .face, and .edge files. //
172 // Other routines are provided mainly for debugging purpose. //
173 // //
174 ///////////////////////////////////////////////////////////////////////////////
175
176 class tetgenio {
177
178 public:
179
180 // Maximum number of characters in a file name (including the null).
181 enum {FILENAMESIZE = 1024};
182
183 // Maxi. numbers of chars in a line read from a file (incl. the null).
184 enum {INPUTLINESIZE = 1024};
185
186 // The polygon data structure. A "polygon" is a planar polygon. It can
187 // be arbitrary shaped (convex or non-convex) and bounded by non-
188 // crossing segments, i.e., the number of vertices it has indictes the
189 // same number of edges.
190 // 'vertexlist' is a list of vertex indices (integers), its length is
191 // indicated by 'numberofvertices'. The vertex indices are odered in
192 // either counterclockwise or clockwise way.
193 typedef struct {
194 int *vertexlist;
195 int numberofvertices;
196 } polygon;
197
198 static void init(polygon* p) {
199 p->vertexlist = (int *) NULL;
200 p->numberofvertices = 0;
201 }
202
203 // The facet data structure. A "facet" is a planar facet. It is used
204 // to represent a planar straight line graph (PSLG) in two dimension.
205 // A PSLG contains a list of polygons. It also may conatin holes in it,
206 // indicated by a list of hole points (their coordinates).
207 typedef struct {
208 polygon *polygonlist;
209 int numberofpolygons;
210 REAL *holelist;
211 int numberofholes;
212 } facet;
213
214 static void init(facet* f) {
215 f->polygonlist = (polygon *) NULL;
216 f->numberofpolygons = 0;
217 f->holelist = (REAL *) NULL;
218 f->numberofholes = 0;
219 }
220
221 // The periodic boundary condition group data structure. A "pbcgroup"
222 // contains the definition of a pbc and the list of pbc point pairs.
223 // 'fmark1' and 'fmark2' are the facetmarkers of the two pbc facets f1
224 // and f2, respectively. 'transmat' is the transformation matrix which
225 // maps a point in f1 into f2. An array of pbc point pairs are saved
226 // in 'pointpairlist'. The first point pair is at indices [0] and [1],
227 // followed by remaining pairs. Two integers per pair.
228 typedef struct {
229 int fmark1, fmark2;
230 REAL transmat[4][4];
231 int numberofpointpairs;
232 int *pointpairlist;
233 } pbcgroup;
234
235 public:
236
237 // Items are numbered starting from 'firstnumber' (0 or 1), default is 0.
238 int firstnumber;
239
240 // Dimension of the mesh (2 or 3), default is 3.
241 int mesh_dim;
242
243 // `pointlist': An array of point coordinates. The first point's x
244 // coordinate is at index [0] and its y coordinate at index [1], its
245 // z coordinate is at index [2], followed by the coordinates of the
246 // remaining points. Each point occupies three REALs.
247 // `pointattributelist': An array of point attributes. Each point's
248 // attributes occupy `numberofpointattributes' REALs.
249 // 'addpointlist': An array of additional point coordinates.
250 // 'addpointattributelist': An array of attributes for addition points.
251 // `pointmarkerlist': An array of point markers; one int per point.
252 REAL *pointlist;
253 REAL *pointattributelist;
254 REAL *addpointlist;
255 REAL *addpointattributelist;
256 int *pointmarkerlist;
257 int numberofpoints;
258 int numberofpointattributes;
259 int numberofaddpoints;
260
261 // `elementlist': An array of element (triangle or tetrahedron) corners.
262 // The first element's first corner is at index [0], followed by its
263 // other corners in counterclockwise order, followed by any other
264 // nodes if the element represents a nonlinear element. Each element
265 // occupies `numberofcorners' ints.
266 // `elementattributelist': An array of element attributes. Each
267 // element's attributes occupy `numberofelementattributes' REALs.
268 // `elementconstraintlist': An array of constraints, i.e. triangle's
269 // area or tetrahedron's volume; one REAL per element. Input only.
270 // `neighborlist': An array of element neighbors; 3 or 4 ints per
271 // element. Output only.
272 int *tetrahedronlist;
273 REAL *tetrahedronattributelist;
274 REAL *tetrahedronvolumelist;
275 int *neighborlist;
276 int numberoftetrahedra;
277 int numberofcorners;
278 int numberoftetrahedronattributes;
279
280 // `facetlist': An array of facets. Each entry is a structure of facet.
281 // `facetmarkerlist': An array of facet markers; one int per facet.
282 facet *facetlist;
283 int *facetmarkerlist;
284 int numberoffacets;
285
286 // `holelist': An array of holes. The first hole's x, y and z
287 // coordinates are at indices [0], [1] and [2], followed by the
288 // remaining holes. Three REALs per hole.
289 REAL *holelist;
290 int numberofholes;
291
292 // `regionlist': An array of regional attributes and volume constraints.
293 // The first constraint's x, y and z coordinates are at indices [0],
294 // [1] and [2], followed by the regional attribute at index [3], foll-
295 // owed by the maximum volume at index [4]. Five REALs per constraint.
296 // Note that each regional attribute is used only if you select the `A'
297 // switch, and each volume constraint is used only if you select the
298 // `a' switch (with no number following).
299 REAL *regionlist;
300 int numberofregions;
301
302 // `facetconstraintlist': An array of facet maximal area constraints.
303 // Two REALs per constraint. The first one is the facet marker (cast
304 // it to int), the second is its maximum area bound.
305 // Note the 'facetconstraintlist' is used only for the 'q' switch.
306 REAL *facetconstraintlist;
307 int numberoffacetconstraints;
308
309 // `segmentconstraintlist': An array of segment max. length constraints.
310 // Three REALs per constraint. The first two are the indices (pointing
311 // into 'pointlist') of the endpoints of the segment, the third is its
312 // maximum length bound.
313 // Note the 'segmentconstraintlist' is used only for the 'q' switch.
314 REAL *segmentconstraintlist;
315 int numberofsegmentconstraints;
316
317 // `nodeconstraintlist': An array of segment length constraints. Two
318 // REALs per constraint. The first one is the index (pointing into
319 // 'pointlist') of the node, the second is its edge length bound.
320 // Note the 'nodeconstraintlist' is used only for the 'q' switch.
321 REAL *nodeconstraintlist;
322 int numberofnodeconstraints;
323
324 // 'pbcgrouplist': An array of periodic boundary condition groups.
325 pbcgroup *pbcgrouplist;
326 int numberofpbcgroups;
327
328 // `trifacelist': An array of triangular face endpoints. The first
329 // face's endpoints are at indices [0], [1] and [2], followed by the
330 // remaining faces. Three ints per face.
331 // `adjtetlist': An array of adjacent tetrahedra to the faces of
332 // trifacelist. Each face has at most two adjacent tets, the first
333 // face's adjacent tets are at [0], [1]. Two ints per face. A '-1'
334 // indicates outside (no adj. tet). This list is output when '-n'
335 // switch is used.
336 // `trifacemarkerlist': An array of face markers; one int per face.
337 int *trifacelist;
338 int *adjtetlist;
339 int *trifacemarkerlist;
340 int numberoftrifaces;
341
342 // `edgelist': An array of edge endpoints. The first edge's endpoints
343 // are at indices [0] and [1], followed by the remaining edges. Two
344 // ints per edge.
345 // `edgemarkerlist': An array of edge markers; one int per edge.
346 int *edgelist;
347 int *edgemarkerlist;
348 int numberofedges;
349
350 public:
351
352 // Initialize routine.
353 void initialize();
354 void deinitialize();
355
356 // Input & output routines.
357 bool load_node_call(FILE* infile, int markers, char* nodefilename);
358 bool load_node(char* filename);
359 bool load_addnodes(char* filename);
360 bool load_pbc(char* filename);
361 bool load_var(char* filename);
362 bool load_mtr(char* filename);
363 bool load_poly(char* filename);
364 bool load_off(char* filename);
365 bool load_ply(char* filename);
366 bool load_stl(char* filename);
367 bool load_medit(char* filename);
368 bool load_plc(char* filename, int object);
369 bool load_tetmesh(char* filename);
370 void save_nodes(char* filename);
371 void save_elements(char* filename);
372 void save_faces(char* filename);
373 void save_edges(char* filename);
374 void save_neighbors(char* filename);
375 void save_poly(char* filename);
376
377 // Read line and parse string functions.
378 char *readline(char* string, FILE* infile, int *linenumber);
379 char *findnextfield(char* string);
380 char *readnumberline(char* string, FILE* infile, char* infilename);
381 char *findnextnumber(char* string);
382
383 // Constructor and destructor.
384 tetgenio() {initialize();}
385 ~tetgenio() {deinitialize();}
386 };
387
388 ///////////////////////////////////////////////////////////////////////////////
389 // //
390 // The tetgenbehavior data type //
391 // //
392 // Used to parse command line switches and file names. //
393 // //
394 // It includes a list of variables corresponding to the commandline switches //
395 // for control the behavior of TetGen. These varibales are all initialized //
396 // to their default values. //
397 // //
398 // parse_commandline() provides an simple interface to set the vaules of the //
399 // variables. It accepts the standard parameters (e.g., 'argc' and 'argv') //
400 // that pass to C/C++ main() function. Alternatively a string which contains //
401 // the command line options can be used as its parameter. //
402 // //
403 // You don't need to understand this data type. It can be implicitly called //
404 // by the global function "tetrahedralize()" defined below. The necessary //
405 // thing you need to know is the meaning of command line switches of TetGen. //
406 // They are described in the third section of the user's manual. //
407 // //
408 ///////////////////////////////////////////////////////////////////////////////
409
410 class tetgenbehavior {
411
412 public:
413
414 // Labels define the objects which are acceptable by TetGen. They are
415 // recognized by the file extensions.
416 // - NODES, a list of nodes (.node);
417 // - POLY, a piecewise linear complex (.poly or .smesh);
418 // - OFF, a polyhedron (.off, Geomview's file format);
419 // - PLY, a polyhedron (.ply, file format from gatech);
420 // - STL, a surface mesh (.stl, stereolithography format);
421 // - MEDIT, a surface mesh (.mesh, Medit's file format);
422 // - MESH, a tetrahedral mesh (.ele).
423 // If no extension is available, the imposed commandline switch
424 // (-p or -r) implies the object.
425
426 enum objecttype {NONE, NODES, POLY, OFF, PLY, STL, MEDIT, MESH};
427
428 // Variables of command line switches. Each variable is corresponding
429 // to a specific switch and will be properly initialized. Read the
430 // user's manul to find out the meaning of these switches.
431
432 int plc; // '-p' switch, 0.
433 int refine; // '-r' switch, 0.
434 int quality; // '-q' switch, 0.
435 int smooth; // '-s' switch, 0.
436 int metric; // '-m' switch, 0.
437 int bgmesh; // '-b' switch, 0.
438 int varvolume; // '-a' switch without number, 0.
439 int fixedvolume; // '-a' switch with number, 0.
440 int insertaddpoints; // '-i' switch, 0.
441 int regionattrib; // '-A' switch, 0.
442 int offcenter; // '-R' switch, 0.
443 int conformdel; // '-D' switch, 0.
444 int diagnose; // '-d' switch, 0.
445 int zeroindex; // '-z' switch, 0.
446 int order; // element order, specified after '-o' switch, 1.
447 int facesout; // '-f' switch, 0.
448 int edgesout; // '-e' switch, 0.
449 int neighout; // '-n' switch, 0.
450 int meditview; // '-g' switch, 0.
451 int gidview; // '-G' switch, 0.
452 int geomview; // '-O' switch, 0.
453 int nobound; // '-B' switch, 0.
454 int nonodewritten; // '-N' switch, 0.
455 int noelewritten; // '-E' switch, 0.
456 int nofacewritten; // '-F' switch, 0.
457 int noiterationnum; // '-I' switch, 0.
458 int nomerge; // count of how often '-M' switch is selected, 0.
459 int nobisect; // count of how often '-Y' switch is selected, 0.
460 int noflip; // do not perform flips. '-Y' switch. 0.
461 int nojettison; // do not jettison redundants nodes. '-J' switch. 0.
462 int steiner; // number after '-S' switch. 0.
463 int fliprepair; // '-X' switch, 1.
464 int docheck; // '-C' switch, 0.
465 int quiet; // '-Q' switch, 0.
466 int verbose; // count of how often '-V' switch is selected, 0.
467 int tol; // count of how often '-T' switch is selected, 0.
468 int useshelles; // '-p', '-r', '-q', '-d', or '-c' switch, 0.
469 REAL minratio; // number after '-q' switch, 2.0.
470 REAL goodratio; // number calculated from 'minratio', 0.0.
471 REAL minangle; // minimum angle bound, 20.0.
472 REAL goodangle; // cosine squared of minangle, 0.0.
473 REAL maxvolume; // number after '-a' switch, -1.0.
474 REAL maxdihedral; // number after '-s' switch, 175.0.
475 REAL alpha1; // number after '-R' switch, sqrt(2).
476 REAL alpha2; // number after '-RR' switch, 1/sqrt(2).
477 REAL alpha3; // number after '-RRR' switch, 0.6.
478 REAL epsilon; // number after '-T' switch, 1.0e-8.
479 REAL epsilon2; // number after '-TT' switch, 1.0e-5.
480 enum objecttype object; // determined by -p, or -r switch. NONE.
481
482 // Variables used to save command line switches and in/out file names.
483 char commandline[1024];
484 char infilename[1024];
485 char outfilename[1024];
486 char bgmeshfilename[1024];
487
488 tetgenbehavior();
489 ~tetgenbehavior() {}
490
491 void versioninfo();
492 void syntax();
493 void usage();
494
495 // Command line parse routine.
496 bool parse_commandline(int argc, char **argv);
497 bool parse_commandline(char *switches) {
498 return parse_commandline(0, &switches);
499 }
500 };
501
502 ///////////////////////////////////////////////////////////////////////////////
503 // //
504 // Geometric predicates //
505 // //
506 // Return one of the values +1, 0, and -1 on basic geometric questions such //
507 // as the orientation of point sets, in-circle, and in-sphere tests. They //
508 // are basic units for the composition of geometric algorithms. TetGen uses //
509 // two 3D geometric predicates, which are the orientation test and the in- //
510 // sphere test (e.g. the locally Deklaunay test). //
511 // //
512 // Orientation test: let a, b, c be a sequence of 3 non-collinear points in //
513 // R^3. They defines a unique hypeplane H. Let H+ and H- be the two spaces //
514 // separated by H, which are defined as follows (using the left-hand rule): //
515 // make a fist using your left hand in such a way that your fingers follow //
516 // the order of a, b and c, then your thumb is pointing to H+. Given any //
517 // point d in R^3, the orientation test returns +1 if d lies in H+, -1 if d //
518 // lies in H-, or 0 if d lies on H. //
519 // //
520 // In-sphere test: let a, b, c, d be 4 non-coplanar points in R^3. They //
521 // defines a unique circumsphere S. Given any point e in R^3, the in-sphere //
522 // test returns +1 if e lies inside S, or -1 if e lies outside S, or 0 if e //
523 // lies on S. //
524 // //
525 // The correctness of geometric predicates is crucial for the control flow //
526 // and hence for the correctness and robustness of an implementation of a //
527 // geometric algorithm. The following routines use arbitrary precision //
528 // floating-point arithmetic. They are fast and robust. It is provided by J. //
529 // Schewchuk in public domain (http://www.cs.cmu.edu/~quake/robust.html). //
530 // The source code are found in a separate file "predicates.cxx". //
531 // //
532 ///////////////////////////////////////////////////////////////////////////////
533
534 REAL exactinit();
535 REAL orient3d(REAL *pa, REAL *pb, REAL *pc, REAL *pd);
536 REAL insphere(REAL *pa, REAL *pb, REAL *pc, REAL *pd, REAL *pe);
537
538 ///////////////////////////////////////////////////////////////////////////////
539 // //
540 // The tetgenmesh data type //
541 // //
542 // Includes data types and mesh routines for creating tetrahedral meshes and //
543 // Delaunay tetrahedralizations, mesh input & output, and so on. //
544 // //
545 // An object of tetgenmesh can be used to store a triangular or tetrahedral //
546 // mesh and its settings. TetGen's functions operates on one mesh each time. //
547 // This type allows reusing of the same function for different meshes. //
548 // //
549 // The mesh data structure (tetrahedron-based and triangle-edge data struct- //
550 // ures) are declared. There are other accessary data type defined as well, //
551 // for efficient memory management and link list operations, etc. //
552 // //
553 // All algorithms TetGen used are implemented in this data type as member //
554 // functions. References of these algorithms can be found in user's manual. //
555 // //
556 // It's not necessary to understand this type. There is a global function //
557 // "tetrahedralize()" (defined at the end of this file) implicitly creates //
558 // the object and calls its member functions according to the command line //
559 // switches you specified. //
560 // //
561 ///////////////////////////////////////////////////////////////////////////////
562
563 class tetgenmesh {
564
565 public:
566
567 // Maximum number of characters in a file name (including the null).
568 enum {FILENAMESIZE = 1024};
569
570 // For efficiency, a variety of data structures are allocated in bulk.
571 // The following constants determine how many of each structure is
572 // allocated at once.
573 enum {VERPERBLOCK = 4092, SUBPERBLOCK = 4092, ELEPERBLOCK = 8188};
574
575 // Used for the point location scheme of Mucke, Saias, and Zhu, to
576 // decide how large a random sample of tetrahedra to inspect.
577 enum {SAMPLEFACTOR = 11};
578
579 // Labels that signify two edge rings of a triangle defined in Muecke's
580 // triangle-edge data structure, one (CCW) traversing edges in count-
581 // erclockwise direction and one (CW) in clockwise direction.
582 enum {CCW = 0, CW = 1};
583
584 // Labels that signify whether a record consists primarily of pointers
585 // or of floating-point words. Used to make decisions about data
586 // alignment.
587 enum wordtype {POINTER, FLOATINGPOINT};
588
589 // Labels that signify the type of a vertex. An UNUSEDVERTEX is a vertex
590 // read from input (.node file or tetgenio structure) or an isolated
591 // vertex (outside the mesh). It is the default type for a newpoint.
592 enum verttype {UNUSEDVERTEX, DUPLICATEDVERTEX, NACUTEVERTEX, ACUTEVERTEX,
593 FREESEGVERTEX, FACETVERTEX, FREESUBVERTEX, VOLVERTEX,
594 FREEVOLVERTEX, DEADVERTEX = -32768};
595
596 // Labels that signify the type of a subface/subsegment. A subface is
597 // SKINNY if it has two edges which are subsegments and form a small
598 // angle (e.g., 10 degree); a subsegment is a SHARP if it is between
599 // two facets which form an acute dihedral angle.
600 enum shestype {NSHARPNSKINNY, SHARP, SKINNY};
601
602 // Labels that signify the type of flips can be applied on a face.
603 // A flipable face has the one of the types T23, T32, T22, and T44.
604 // Types UNFLIPABLE, NONCONVEX are unflipable.
605 enum fliptype {T23, T32, T22, T44, UNFLIPABLE, FORBIDDENFACE,
606 FORBIDDENEDGE, NONCONVEX};
607
608 // Labels that signify the result of triangle-triangle intersection test.
609 // Two triangles are DISJOINT, or adjoint at a vertex SHAREVERTEX, or
610 // adjoint at an edge SHAREEDGE, or coincident SHAREFACE or INTERSECT.
611 enum interresult {DISJOINT, SHAREVERTEX, SHAREEDGE, SHAREFACE, INTERSECT};
612
613 // Labels that signify the result of point location. The result of a
614 // search indicates that the point falls inside a tetrahedron, inside
615 // a triangle, on an edge, on a vertex, or outside the mesh.
616 enum locateresult {INTETRAHEDRON, ONFACE, ONEDGE, ONVERTEX, OUTSIDE};
617
618 // Labels that signify the result of vertex insertion. The result
619 // indicates that the vertex was inserted with complete success, was
620 // inserted but encroaches upon a subsegment, was not inserted because
621 // it lies on a segment, or was not inserted because another vertex
622 // occupies the same location.
623 enum insertsiteresult {SUCCESSINTET, SUCCESSONFACE, SUCCESSONEDGE,
624 DUPLICATEPOINT, OUTSIDEPOINT};
625
626 // Labels that signify the result of direction finding. The result
627 // indicates that a segment connecting the two query points accross
628 // an edge of the direction triangle/tetrahedron, across a face of
629 // the direction tetrahedron, along the left edge of the direction
630 // triangle/tetrahedron, along the right edge of the direction
631 // triangle/tetrahedron, or along the top edge of the tetrahedron.
632 enum finddirectionresult {ACROSSEDGE, ACROSSFACE, LEFTCOLLINEAR,
633 RIGHTCOLLINEAR, TOPCOLLINEAR, BELOWHULL};
634
635 ///////////////////////////////////////////////////////////////////////////////
636 // //
637 // The basic mesh element data structures //
638 // //
639 // There are four types of mesh elements: tetrahedra, subfaces, subsegments, //
640 // and points, where subfaces and subsegments are triangles and edges which //
641 // appear on boundaries. A tetrahedralization of a 3D point set comprises //
642 // tetrahedra and points; a surface mesh of a 3D domain comprises subfaces //
643 // subsegments and points. The elements of all the four types consist of a //
644 // tetrahedral mesh of a 3D domain. However, TetGen uses three data types: //
645 // 'tetrahedron', 'shellface', and 'point'. A 'tetrahedron' is a tetrahedron;//
646 // while a 'shellface' can be either a subface or a subsegment; and a 'point'//
647 // is a point. These three data types, linked by pointers comprise a mesh. //
648 // //
649 // A tetrahedron primarily consists of a list of 4 pointers to its corners, //
650 // a list of 4 pointers to its adjoining tetrahedra, a list of 4 pointers to //
651 // its adjoining subfaces (when subfaces are needed). Optinoally, (depending //
652 // on the selected switches), it may contain an arbitrary number of user- //
653 // defined floating-point attributes, an optional maximum volume constraint //
654 // (for -a switch), and a pointer to a list of high-order nodes (-o2 switch).//
655 // Since the size of a tetrahedron is not determined until running time, it //
656 // is not simply declared as a structure. //
657 // //
658 // The data structure of tetrahedron also stores the geometrical information.//
659 // Let t be a tetrahedron, v0, v1, v2, and v3 be the 4 nodes corresponding //
660 // to the order of their storage in t. v3 always has a negative orientation //
661 // with respect to v0, v1, v2 (ie,, v3 lies above the oriented plane passes //
662 // through v0, v1, v2). Let the 4 faces of t be f0, f1, f2, and f3. Vertices //
663 // of each face are stipulated as follows: f0 (v0, v1, v2), f1 (v0, v3, v1), //
664 // f2 (v1, v3, v2), f3 (v2, v3, v0). //
665 // //
666 // A subface has 3 pointers to vertices, 3 pointers to adjoining subfaces, 3 //
667 // pointers to adjoining subsegments, 2 pointers to adjoining tetrahedra, a //
668 // boundary marker(an integer). Like a tetrahedron, the pointers to vertices,//
669 // subfaces, and subsegments are ordered in a way that indicates their geom- //
670 // etric relation. Let s be a subface, v0, v1 and v2 be the 3 nodes corres- //
671 // ponding to the order of their storage in s, e0, e1 and e2 be the 3 edges,//
672 // then we have: e0 (v0, v1), e1 (v1, v2), e2 (v2, v0). //
673 // //
674 // A subsegment has exactly the same data fields as a subface has, but only //
675 // uses some of them. It has 2 pointers to its endpoints, 2 pointers to its //
676 // adjoining (and collinear) subsegments, a pointer to a subface containing //
677 // it (there may exist any number of subfaces having it, choose one of them //
678 // arbitrarily). The geometric relation between its endpoints and adjoining //
679 // subsegments is kept with respect to the storing order of its endpoints. //
680 // //
681 // The data structure of point is relatively simple. A point is a list of //
682 // floating-point numbers, starting with the x, y, and z coords, followed by //
683 // an arbitrary number of optional user-defined floating-point attributes, //
684 // an integer boundary marker, an integer for the point type, and a pointer //
685 // to a tetrahedron (used for speeding up point location). //
686 // //
687 // For a tetrahedron on a boundary (or a hull) of the mesh, some or all of //
688 // the adjoining tetrahedra may not be present. For an interior tetrahedron, //
689 // often no neighboring subfaces are present, Such absent tetrahedra and //
690 // subfaces are never represented by the NULL pointers; they are represented //
691 // by two special records: `dummytet', the tetrahedron fills "outer space", //
692 // and `dummysh', the vacuous subfaces which are omnipresent. //
693 // //
694 // Tetrahedra and adjoining subfaces are glued together through the pointers //
695 // saved in each data fields of them. Subfaces and adjoining subsegments are //
696 // connected in the same fashion. However, there are no pointers directly //
697 // gluing tetrahedra and adjoining subsegments. For the purpose of saving //
698 // space, the connections between tetrahedra and subsegments are entirely //
699 // mediated through subfaces. The following part explains how subfaces are //
700 // connected in TetGen. //
701 // //
702 ///////////////////////////////////////////////////////////////////////////////
703
704 ///////////////////////////////////////////////////////////////////////////////
705 // //
706 // The subface-subface and subface-subsegment connections //
707 // //
708 // Adjoining subfaces sharing a common edge are connected in such a way that //
709 // they form a face ring around the edge. It is indeed a single linked list //
710 // which is cyclic, e.g., one can start from any subface in it and traverse //
711 // back. When the edge is not a subsegment, the ring only has two coplanar //
712 // subfaces which are pointing to each other. Otherwise, the face ring may //
713 // have any number of subfaces (and are not all coplanar). //
714 // //
715 // How is the face ring formed? Let s be a subsegment, f is one of subfaces //
716 // containing s as an edge. The direction of s is stipulated from its first //
717 // endpoint to its second (according to their storage in s). Once the dir of //
718 // s is determined, the other two edges of f are oriented to follow this dir.//
719 // The "directional normal" N_f is a vector formed from any point in f and a //
720 // points orthogonally above f. //
721 // //
722 // The face ring of s is a cyclic ordered set of subfaces containing s, i.e.,//
723 // F(s) = {f1, f2, ..., fn}, n >= 1. Where the order is defined as follows: //
724 // let fi, fj be two faces in F(s), the "normal-angle", NAngle(i,j) (range //
725 // from 0 to 360 degree) is the angle between the N_fi and N_fj; then fi is //
726 // in front of fj (or symbolically, fi < fj) if there exists another fk in //
727 // F(s), and NAangle(k, i) < NAngle(k, j). The face ring of s is: f1 < f2 < //
728 // ... < fn < f1. //
729 // //
730 // The easiest way to imagine how a face ring is formed is to use the right- //
731 // hand rule. Make a fist using your right hand with the thumb pointing to //
732 // the direction of the subsegment. The face ring is connected following the //
733 // direction of your fingers. //
734 // //
735 // The subface and subsegment are also connected through pointers stored in //
736 // their own data fields. Every subface has a pointer to its adjoining sub- //
737 // segment. However, a subsegment only has one pointer to a subface which is //
738 // containing it. Such subface can be chosen arbitrarily, other subfaces are //
739 // found through the face ring. //
740 // //
741 ///////////////////////////////////////////////////////////////////////////////
742
743 // The tetrahedron data structure. Fields of a tetrahedron contains:
744 // - a list of four adjoining tetrahedra;
745 // - a list of four vertices;
746 // - a list of four subfaces (optional, used for -p switch);
747 // - a list of user-defined floating-point attributes (optional);
748 // - a volume constraint (optional, used for -a switch);
749 // - an integer of element marker (optional, used for -n switch);
750 // - a pointer to a list of high-ordered nodes (optional, -o2 switch);
751
752 typedef REAL **tetrahedron;
753
754 // The shellface data structure. Fields of a shellface contains:
755 // - a list of three adjoining subfaces;
756 // - a list of three vertices;
757 // - a list of two adjoining tetrahedra;
758 // - a list of three adjoining subsegments;
759 // - a pointer to a badface containing it (used for -q);
760 // - an area constraint (optional, used for -q);
761 // - an integer for boundary marker;
762 // - an integer for type: SHARPSEGMENT, NONSHARPSEGMENT, ...;
763 // - an integer for pbc group (optional, if in->pbcgrouplist exists);
764
765 typedef REAL **shellface;
766
767 // The point data structure. It is actually an array of REALs:
768 // - x, y and z coordinates;
769 // - a list of user-defined point attributes (optional);
770 // - a REAL of local feature sizes (optional -p switch);
771 // - a pointer to a simplex (tet, tri, edge, or vertex);
772 // - a pointer to a parent (or duplicate) point;
773 // - a pointer to a tet in background mesh (optional);
774 // - a pointer to another pbc point (optional);
775 // - an integer for boundary marker;
776 // - an integer for verttype: INPUTVERTEX, FREEVERTEX, ...;
777
778 typedef REAL *point;
779
780 ///////////////////////////////////////////////////////////////////////////////
781 // //
782 // The mesh handle (triface, face) data types //
783 // //
784 // Two special data types, 'triface' and 'face' are defined for maintaining //
785 // and updating meshes. They are like pointers (or handles), which allow you //
786 // to hold one particular part of the mesh, i.e., a tetrahedron, a triangle, //
787 // an edge and a vertex. However, these data types do not themselves store //
788 // any part of the mesh. The mesh is made of the data types defined above. //
789 // //
790 // Muecke's "triangle-edge" data structure is the prototype for these data //
791 // types. It allows a universal representation for every tetrahedron, //
792 // triangle, edge and vertex. For understanding the following descriptions //
793 // of these handle data structures, readers are required to read both the //
794 // introduction and implementation detail of "triangle-edge" data structure //
795 // in Muecke's thesis. //
796 // //
797 // A 'triface' represents a face of a tetrahedron and an oriented edge of //
798 // the face simultaneously. It has a pointer 'tet' to a tetrahedron, an //
799 // integer 'loc' (range from 0 to 3) as the face index, and an integer 'ver' //
800 // (range from 0 to 5) as the edge version. A face of the tetrahedron can be //
801 // uniquly determined by the pair (tet, loc), and an oriented edge of this //
802 // face can be uniquly determined by the triple (tet, loc, ver). Therefore, //
803 // different usages of one triface are possible. If we only use the pair //
804 // (tet, loc), it refers to a face, and if we add the 'ver' additionally to //
805 // the pair, it is an oriented edge of this face. //
806 // //
807 // A 'face' represents a subface and an oriented edge of it simultaneously. //
808 // It has a pointer 'sh' to a subface, an integer 'shver'(range from 0 to 5) //
809 // as the edge version. The pair (sh, shver) determines a unique oriented //
810 // edge of this subface. A 'face' is also used to represent a subsegment, //
811 // in this case, 'sh' points to the subsegment, and 'shver' indicates the //
812 // one of two orientations of this subsegment, hence, it only can be 0 or 1. //
813 // //
814 // Mesh navigation and updating are accomplished through a set of mesh //
815 // manipulation primitives which operate on trifaces and faces. They are //
816 // introduced below. //
817 // //
818 ///////////////////////////////////////////////////////////////////////////////
819
820 class triface {
821
822 public:
823
824 tetrahedron* tet;
825 int loc, ver;
826
827 // Constructors;
828 triface() : tet(0), loc(0), ver(0) {}
829 // Operators;
830 triface& operator=(const triface& t) {
831 tet = t.tet; loc = t.loc; ver = t.ver;
832 return *this;
833 }
834 bool operator==(triface& t) {
835 return tet == t.tet && loc == t.loc && ver == t.ver;
836 }
837 bool operator!=(triface& t) {
838 return tet != t.tet || loc != t.loc || ver != t.ver;
839 }
840 };
841
842 class face {
843
844 public:
845
846 shellface *sh;
847 int shver;
848
849 // Constructors;
850 face() : sh(0), shver(0) {}
851 // Operators;
852 face& operator=(const face& s) {
853 sh = s.sh; shver = s.shver;
854 return *this;
855 }
856 bool operator==(face& s) {return (sh == s.sh) && (shver == s.shver);}
857 bool operator!=(face& s) {return (sh != s.sh) || (shver != s.shver);}
858 };
859
860 ///////////////////////////////////////////////////////////////////////////////
861 // //
862 // The badface structure //
863 // //
864 // A multiple usages structure. Despite of its name, a 'badface' can be used //
865 // to represent the following objects: //
866 // - a face of a tetrahedron which is (possibly) non-Delaunay; //
867 // - an encroached subsegment or subface; //
868 // - a bad-quality tetrahedron, i.e, has too large radius-edge ratio; //
869 // - a sliver, i.e., has good radius-edge ratio but nearly zero volume; //
870 // - a degenerate tetrahedron (see routine checkdegetet()). //
871 // - a recently flipped face (saved for undoing the flip later). //
872 // //
873 // It has the following fields: 'tt' holds a tetrahedron; 'ss' holds a sub- //
874 // segment or subface; 'cent' is the circumcent of 'tt' or 'ss', 'key' is a //
875 // special value depending on the use, it can be either the square of the //
876 // radius-edge ratio of 'tt' or the flipped type of 'tt'; 'forg', 'fdest', //
877 // 'fapex', and 'foppo' are vertices saved for checking the object in 'tt' //
878 // or 'ss' is still the same when it was stored; 'noppo' is the fifth vertex //
879 // of a degenerate point set. 'previtem' and 'nextitem' implement a double //
880 // link for managing many basfaces. //
881 // //
882 ///////////////////////////////////////////////////////////////////////////////
883
884 struct badface {
885 triface tt;
886 face ss;
887 REAL key;
888 REAL cent[3];
889 point forg, fdest, fapex, foppo;
890 point noppo;
891 struct badface *previtem, *nextitem;
892 };
893
894 ///////////////////////////////////////////////////////////////////////////////
895 // //
896 // The pbcdata structure //
897 // //
898 // A pbcdata stores data of a periodic boundary condition defined on a pair //
899 // of facets or segments. Let f1 and f2 define a pbcgroup. 'fmark' saves the //
900 // facet markers of f1 and f2; 'ss' contains two subfaces belong to f1 and //
901 // f2, respectively. Let s1 and s2 define a segment pbcgroup. 'segid' are //
902 // the segment ids of s1 and s2; 'ss' contains two segments belong to s1 and //
903 // s2, respectively. 'transmat' are two transformation matrices. transmat[0] //
904 // transforms a point of f1 (or s1) into a point of f2 (or s2), transmat[1] //
905 // does the inverse. //
906 // //
907 ///////////////////////////////////////////////////////////////////////////////
908
909 struct pbcdata {
910 int fmark[2];
911 int segid[2];
912 face ss[2];
913 REAL transmat[2][4][4];
914 };
915
916 ///////////////////////////////////////////////////////////////////////////////
917 // //
918 // The Metric tensor data structure //
919 // //
920 // A metric is a function that specifies the "distance" between two points //
921 // in a metric space E. Recall if d(p, q) is a metric of E, then we have: //
922 // (1) d(p, q) = d(q, p). (d is symmetric) //
923 // (2) d(p, q) = 0 if and only if p = q. //
924 // (3) d(p, x) + d(x, q) >= d(p, q). (d satisfies triangle inequality) //
925 // //
926 // In d dimensions, the metric tensor of a point p is a (dxd) symmetric //
927 // positive definie (non-degenerate) matrix M(p). Very roughly, it tells how //
928 // to compute the distance of p and other points in the metric space of p. //
929 // d_M(p, q) = \sqrt{(p - q)' M (p -q)}. //
930 // If for any point p, a metric tensor M(p) is given, the field of tensors //
931 // thus defines a Riemannian space. For example, if M(q) is a metric tensor //
932 // defined on q. Then the distance d(p, q) can be calculated by: //
933 // d(p, q) = \int_{0}{1} \sqrt((p - q)' M(t) (p - q)) dt. //
934 // where M(t) is the interpolation of metric tensors between p and q, M(0) = //
935 // M(p) and M(1) = M(q). //
936 // //
937 // A metric tensor in three dimension, for example, is a matrix: //
938 // | a b c | //
939 // M = | b d e | //
940 // | c e f | //
941 // such that a > 0, d > 0, f > 0, det(M) = adf + 2bce -ccd - eea - bbf > 0. //
942 // //
943 // It is defined as an array mat[6] = {a, b, c, d, e, f}. Operation on //
944 // tensors are defined as well. //
945 // //
946 ///////////////////////////////////////////////////////////////////////////////
947
948 class metric {
949
950 public:
951
952 REAL mat[6];
953
954 // Initialization.
955 void init() {for (int i = 0; i < 6; i++) mat[i] = 0.0;}
956 void set(REAL a, REAL b, REAL c, REAL d, REAL e, REAL f) {
957 mat[0] = a; mat[1] = b; mat[2] = c;
958 mat[3] = d; mat[4] = e;
959 mat[5] = f;
960 }
961 void set(REAL a, REAL d, REAL f) {
962 mat[0] = a; mat[1] = 0.0; mat[2] = 0.0;
963 mat[3] = d; mat[4] = 0.0;
964 mat[5] = f;
965 }
966
967 // Constructors.
968 metric() {init();}
969 };
970
971 ///////////////////////////////////////////////////////////////////////////////
972 // //
973 // The list, link and queue data structures //
974 // //
975 // These data types are used to manipulate a set of (same-typed) data items. //
976 // For a given set S = {a, b, c, ...}, a list stores the elements of S in a //
977 // piece of continuous memory. It allows quickly accessing each element of S,//
978 // thus is suitable for storing a fix-sized set. While a link stores its //
979 // elements incontinuously. It allows quickly inserting or deleting an item, //
980 // thus is suitable for storing a size-variable set. A queue is basically a //
981 // special case of a link where one data element joins the link at the end //
982 // and leaves in an ordered fashion at the other end. //
983 // //
984 ///////////////////////////////////////////////////////////////////////////////
985
986 // The compfunc data type. "compfunc" is a pointer to a linear-order
987 // function, which takes two 'void*' arguments and returning an 'int'.
988 //
989 // A function: int cmp(const T &, const T &), is said to realize a
990 // linear order on the type T if there is a linear order <= on T such
991 // that for all x and y in T satisfy the following relation:
992 // -1 if x < y.
993 // comp(x, y) = 0 if x is equivalent to y.
994 // +1 if x > y.
995 typedef int (*compfunc) (const void *, const void *);
996
997 // The predefined compare functions for primitive data types. They
998 // take two pointers of the corresponding date type, perform the
999 // comparation, and return -1, 0 or 1 indicating the default linear
1000 // order of them.
1001
1002 // Compare two 'integers'.
1003 static int compare_2_ints(const void* x, const void* y);
1004 // Compare two 'longs'.
1005 static int compare_2_longs(const void* x, const void* y);
1006 // Compare two 'unsigned longs'.
1007 static int compare_2_unsignedlongs(const void* x, const void* y);
1008
1009 // The function used to determine the size of primitive data types and
1010 // set the corresponding predefined linear order functions for them.
1011 static void set_compfunc(char* str, int* itembytes, compfunc* pcomp);
1012
1013 ///////////////////////////////////////////////////////////////////////////////
1014 // //
1015 // List data structure. //
1016 // //
1017 // A 'list' is an array of items with automatically reallocation of memory. //
1018 // It behaves like an array. //
1019 // //
1020 // 'base' is the starting address of the array; The memory unit in list is //
1021 // byte, i.e., sizeof(char). 'itembytes' is the size of each item in byte, //
1022 // so that the next item in list will be found at the next 'itembytes' //
1023 // counted from the current position. //
1024 // //
1025 // 'items' is the number of items stored in list. 'maxitems' indicates how //
1026 // many items can be stored in this list. 'expandsize' is the increasing //
1027 // size (items) when the list is full. //
1028 // //
1029 // 'comp' is a pointer pointing to a linear order function for the list. //
1030 // default it is set to 'NULL'. //
1031 // //
1032 // The index of list always starts from zero, i.e., for a list L contains //
1033 // n elements, the first element is L[0], and the last element is L[n-1]. //
1034 // This feature lets lists like C/C++ arrays. //
1035 // //
1036 ///////////////////////////////////////////////////////////////////////////////
1037
1038 class list {
1039
1040 public:
1041
1042 char *base;
1043 int itembytes;
1044 int items, maxitems, expandsize;
1045 compfunc comp;
1046
1047 public:
1048
1049 list(int itbytes, compfunc pcomp, int mitems = 256, int exsize = 128) {
1050 listinit(itbytes, pcomp, mitems, exsize);
1051 }
1052 list(char* str, int mitems = 256, int exsize = 128) {
1053 set_compfunc(str, &itembytes, &comp);
1054 listinit(itembytes, comp, mitems, exsize);
1055 }
1056 ~list() { free(base); }
1057
1058 void *operator[](int i) { return (void *) (base + i * itembytes); }
1059
1060 void listinit(int itbytes, compfunc pcomp, int mitems, int exsize);
1061 void setcomp(compfunc compf) { comp = compf; }
1062 void clear() { items = 0; }
1063 int len() { return items; }
1064 void *append(void* appitem);
1065 void *insert(int pos, void* insitem);
1066 void del(int pos, int order);
1067 int hasitem(void* checkitem);
1068 void sort();
1069 };
1070
1071 ///////////////////////////////////////////////////////////////////////////////
1072 // //
1073 // Memorypool data structure. //
1074 // //
1075 // A type used to allocate memory. (It is incorporated from Shewchuk's //
1076 // Triangle program) //
1077 // //
1078 // firstblock is the first block of items. nowblock is the block from which //
1079 // items are currently being allocated. nextitem points to the next slab //
1080 // of free memory for an item. deaditemstack is the head of a linked list //
1081 // (stack) of deallocated items that can be recycled. unallocateditems is //
1082 // the number of items that remain to be allocated from nowblock. //
1083 // //
1084 // Traversal is the process of walking through the entire list of items, and //
1085 // is separate from allocation. Note that a traversal will visit items on //
1086 // the "deaditemstack" stack as well as live items. pathblock points to //
1087 // the block currently being traversed. pathitem points to the next item //
1088 // to be traversed. pathitemsleft is the number of items that remain to //
1089 // be traversed in pathblock. //
1090 // //
1091 // itemwordtype is set to POINTER or FLOATINGPOINT, and is used to suggest //
1092 // what sort of word the record is primarily made up of. alignbytes //
1093 // determines how new records should be aligned in memory. itembytes and //
1094 // itemwords are the length of a record in bytes (after rounding up) and //
1095 // words. itemsperblock is the number of items allocated at once in a //
1096 // single block. items is the number of currently allocated items. //
1097 // maxitems is the maximum number of items that have been allocated at //
1098 // once; it is the current number of items plus the number of records kept //
1099 // on deaditemstack. //
1100 // //
1101 ///////////////////////////////////////////////////////////////////////////////
1102
1103 class memorypool {
1104
1105 public:
1106
1107 void **firstblock, **nowblock;
1108 void *nextitem;
1109 void *deaditemstack;
1110 void **pathblock;
1111 void *pathitem;
1112 wordtype itemwordtype;
1113 int alignbytes;
1114 int itembytes, itemwords;
1115 int itemsperblock;
1116 long items, maxitems;
1117 int unallocateditems;
1118 int pathitemsleft;
1119
1120 public:
1121
1122 memorypool();
1123 memorypool(int, int, enum wordtype, int);
1124 ~memorypool();
1125
1126 void poolinit(int, int, enum wordtype, int);
1127 void restart();
1128 void *alloc();
1129 void dealloc(void*);
1130 void traversalinit();
1131 void *traverse();
1132 };
1133
1134 ///////////////////////////////////////////////////////////////////////////////
1135 // //
1136 // Link data structure. //
1137 // //
1138 // A 'link' is a double linked nodes. It uses the memorypool data structure //
1139 // for memory management. Following is an image of a link. //
1140 // //
1141 // head-> ____0____ ____1____ ____2____ _________<-tail //
1142 // |__next___|--> |__next___|--> |__next___|--> |__NULL___| //
1143 // |__NULL___|<-- |__prev___|<-- |__prev___|<-- |__prev___| //
1144 // | | |_ _| |_ _| | | //
1145 // | | |_ Data1 _| |_ Data2 _| | | //
1146 // |_________| |_________| |_________| |_________| //
1147 // //
1148 // The unit size for storage is size of pointer, which may be 4-byte (in 32- //
1149 // bit machine) or 8-byte (in 64-bit machine). The real size of an item is //
1150 // stored in 'linkitembytes'. //
1151 // //
1152 // 'head' and 'tail' are pointers pointing to the first and last nodes. They //
1153 // do not conatin data (See above). //
1154 // //
1155 // 'nextlinkitem' is a pointer pointing to a node which is the next one will //
1156 // be traversed. 'curpos' remembers the position (1-based) of the current //
1157 // traversing node. //
1158 // //
1159 // 'linkitems' indicates how many items in link. Note it is different with //
1160 // 'items' of memorypool. //
1161 // //
1162 // The index of link starts from 1, i.e., for a link K contains n elements, //
1163 // the first element of the link is K[1], and the last element is K[n]. //
1164 // See the above figure. //
1165 // //
1166 ///////////////////////////////////////////////////////////////////////////////
1167
1168 class link : public memorypool {
1169
1170 public:
1171
1172 void **head, **tail;
1173 void *nextlinkitem;
1174 int linkitembytes;
1175 int linkitems;
1176 int curpos;
1177 compfunc comp;
1178
1179 public:
1180
1181 link(int _itembytes, compfunc _comp, int itemcount) {
1182 linkinit(_itembytes, _comp, itemcount);
1183 }
1184 link(char* str, int itemcount) {
1185 set_compfunc(str, &linkitembytes, &comp);
1186 linkinit(linkitembytes, comp, itemcount);
1187 }
1188
1189 void linkinit(int _itembytes, compfunc _comp, int itemcount);
1190 void setcomp(compfunc compf) { comp = compf; }
1191 void rewind() { nextlinkitem = *head; curpos = 1; }
1192 void goend() { nextlinkitem = *(tail + 1); curpos = linkitems; }
1193 long len() { return linkitems; }
1194 void clear();
1195 bool move(int numberofnodes);
1196 bool locate(int pos);
1197 void *add(void* newitem);
1198 void *insert(int pos, void* insitem);
1199 void *del(void* delitem);
1200 void *del(int pos);
1201 void *getitem();
1202 void *getnitem(int pos);
1203 int hasitem(void* checkitem);
1204 };
1205
1206 ///////////////////////////////////////////////////////////////////////////////
1207 // //
1208 // Queue data structure. //
1209 // //
1210 // A 'queue' is a basically a link. Following is an image of a queue. //
1211 // ___________ ___________ ___________ //
1212 // Pop() <-- |_ _|<--|_ _|<--|_ _| <-- Push() //
1213 // |_ Data0 _| |_ Data1 _| |_ Data2 _| //
1214 // |___________| |___________| |___________| //
1215 // queue head queue tail //
1216 // //
1217 ///////////////////////////////////////////////////////////////////////////////
1218
1219 class queue : public link {
1220
1221 public:
1222
1223 queue(int bytes, int count = 256) : link(bytes, NULL, count) {}
1224 queue(char* str, int count = 256) : link(str, count) {}
1225
1226 int empty() { return linkitems == 0; }
1227 void *push(void* newitem) { return link::add(newitem); }
1228 void *bot() { return link::getnitem(1); }
1229 void *pop() { return link::del(1); }
1230 };
1231
1232 ///////////////////////////////////////////////////////////////////////////////
1233 // //
1234 // Global variables used for miscellaneous purposes. //
1235 // //
1236 ///////////////////////////////////////////////////////////////////////////////
1237
1238 // Pointer to the input data (a set of nodes, a PLC, or a mesh).
1239 tetgenio *in;
1240 // Pointer to the options (and filenames).
1241 tetgenbehavior *b;
1242 // Pointer to a background mesh (contains size specification map).
1243 tetgenmesh *bgm;
1244
1245 // Variables used to allocate and access memory for tetrahedra, subfaces
1246 // subsegments, points, encroached subfaces, encroached subsegments,
1247 // bad-quality tetrahedra, and so on.
1248 memorypool *tetrahedrons;
1249 memorypool *subfaces;
1250 memorypool *subsegs;
1251 memorypool *points;
1252 memorypool *badsubsegs;
1253 memorypool *badsubfaces;
1254 memorypool *badtetrahedrons;
1255 memorypool *flipstackers;
1256
1257 // Pointer to the 'tetrahedron' that occupies all of "outer space".
1258 tetrahedron *dummytet;
1259 tetrahedron *dummytetbase; // Keep base address so we can free it later.
1260
1261 // Pointer to the omnipresent subface. Referenced by any tetrahedron,
1262 // or subface that isn't connected to a subface at that location.
1263 shellface *dummysh;
1264 shellface *dummyshbase; // Keep base address so we can free it later.
1265
1266 // A point above the plane in which the facet currently being used lies.
1267 // It is used as a reference point for orient3d().
1268 point *facetabovepointarray, abovepoint;
1269
1270 // Array (size = numberoftetrahedra * 6) for storing high-order nodes of
1271 // tetrahedra (only used when -o2 switch is selected).
1272 point *highordertable;
1273
1274 // Arrays for storing and searching pbc data. 'subpbcgrouptable', (size
1275 // is numberofpbcgroups) for pbcgroup of subfaces. 'segpbcgrouptable',
1276 // a list for pbcgroup of segments. Because a segment can have several
1277 // pbcgroup incident on it, its size is unknown on input, it will be
1278 // found in 'createsegpbcgrouptable()'.
1279 pbcdata *subpbcgrouptable;
1280 list *segpbcgrouptable;
1281 // A map for searching the pbcgroups of a given segment. 'idx2segpglist'
1282 // (size = number of input segments + 1), and 'segpglist'.
1283 int *idx2segpglist, *segpglist;
1284
1285 // Queues that maintain the bad (badly-shaped or too large) tetrahedra.
1286 // The tails are pointers to the pointers that have to be filled in to
1287 // enqueue an item. The queues are ordered from 63 (highest priority)
1288 // to 0 (lowest priority).
1289 badface *subquefront[3], **subquetail[3];
1290 badface *tetquefront[64], **tetquetail[64];
1291
1292 // Pointer to a recently visited tetrahedron. Improves point location
1293 // if proximate points are inserted sequentially.
1294 triface recenttet;
1295
1296 REAL xmax, xmin, ymax, ymin, zmax, zmin; // Bounding box of points.
1297 REAL longest; // The longest possible edge length.
1298 REAL lengthlimit; // The limiting length of a new edge.
1299 long hullsize; // Number of faces of convex hull.
1300 long insegments; // Number of input segments.
1301 int steinerleft; // Number of Steiner points not yet used.
1302 int pointlfsindex; // Index to find the local feature size of a point.
1303 int point2simindex; // Index to find a simplex adjacent to a point.
1304 int pointmarkindex; // Index to find boundary marker of a point.
1305 int point2pbcptindex; // Index to find a pbc point to a point.
1306 int highorderindex; // Index to find extra nodes for highorder elements.
1307 int elemattribindex; // Index to find attributes of a tetrahedron.
1308 int volumeboundindex; // Index to find volume bound of a tetrahedron.
1309 int elemmarkerindex; // Index to find marker of a tetrahedron.
1310 int shmarkindex; // Index to find boundary marker of a subface.
1311 int areaboundindex; // Index to find area bound of a subface.
1312 int checksubfaces; // Are there subfaces in the mesh yet?
1313 int checkpbcs; // Are there periodic boundary conditions?
1314 int varconstraint; // Are there variant (node, seg, facet) constraints?
1315 int nonconvex; // Is current mesh non-convex?
1316 int dupverts; // Are there duplicated vertices?
1317 int unuverts; // Are there unused vertices?
1318 int relverts; // The number of relocated vertices.
1319 int suprelverts; // The number of suppressed relocated vertices.
1320 int collapverts; // The number of collapsed relocated vertices.
1321 int unsupverts; // The number of unsuppressed vertices.
1322 int jettisoninverts; // The number of jettisoned input vertices.
1323 int symbolic; // Use symbolic insphere test.
1324 long samples; // Number of random samples for point location.
1325 unsigned long randomseed; // Current random number seed.
1326 REAL macheps; // The machine epsilon.
1327 REAL cosmaxdihed, cosmindihed; // The cosine values of max/min dihedral.
1328 int maxcavfaces, maxcavverts; // The size of the largest cavity.
1329 int expcavcount; // The times of expanding cavitys.
1330 long abovecount; // Number of abovepoints calculation.
1331 long bowatvolcount, bowatsubcount, bowatsegcount; // Bowyer-Watsons.
1332 long updvolcount, updsubcount, updsegcount; // Bow-Wat cavities updates.
1333 long failvolcount, failsubcount, failsegcount; // Bow-Wat fails.
1334 long repairflipcount; // Number of flips for repairing segments.
1335 long outbowatcircumcount; // Number of circumcenters outside Bowat-cav.
1336 long r1count, r2count, r3count, r4count; // Number of rules performed.
1337 long cdtenforcesegpts; // Number of CDT enforcement points.
1338 long rejsegpts, rejsubpts, rejtetpts; // Number of rejected points.
1339 long striptetcount, fliptetcount, unimprovecount; // Mesh smooth counts.
1340 long smoothcdtsegpt, smoothsegpt, smoothsubpt, smoothvolpt;
1341 long unsmoothcdtsegpt, unsmoothsegpt, unsmoothsubpt, unsmoothvolpt;
1342 long flip23s, flip32s, flip22s, flip44s; // Number of flips performed.
1343
1344 ///////////////////////////////////////////////////////////////////////////////
1345 // //
1346 // Fast lookup tables for mesh manipulation primitives. //
1347 // //
1348 // Mesh manipulation primitives (given below) are basic operations on mesh //
1349 // data structures. They answer basic queries on mesh handles, such as "what //
1350 // is the origin (or destination, or apex) of the face?", "what is the next //
1351 // (or previous) edge in the edge ring?", and "what is the next face in the //
1352 // face ring?", and so on. //
1353 // //
1354 // The implementation of teste basic queries can take advangtage of the fact //
1355 // that the mesh data structures additionally store geometric informations. //
1356 // For example, we have ordered the 4 vertices (from 0 to 3) and the 4 faces //
1357 // (from 0 to 3) of a tetrahedron, and for each face of the tetrahedron, a //
1358 // sequence of vertices has stipulated, therefore the origin of any face of //
1359 // the tetrahedron can be quickly determined by a table 'locver2org', which //
1360 // takes the index of the face and the edge version as inputs. A list of //
1361 // fast lookup tables are defined below. They're just like global variables. //
1362 // These tables are initialized at the runtime. //
1363 // //
1364 ///////////////////////////////////////////////////////////////////////////////
1365
1366 // For enext() primitive, uses 'ver' as the index.
1367 static int ve[6];
1368
1369 // For org(), dest() and apex() primitives, uses 'ver' as the index.
1370 static int vo[6], vd[6], va[6];
1371
1372 // For org(), dest() and apex() primitives, uses 'loc' as the first
1373 // index and 'ver' as the second index.
1374 static int locver2org[4][6];
1375 static int locver2dest[4][6];
1376 static int locver2apex[4][6];
1377
1378 // For oppo() primitives, uses 'loc' as the index.
1379 static int loc2oppo[4];
1380
1381 // For fnext() primitives, uses 'loc' as the first index and 'ver' as
1382 // the second index, returns an array containing a new 'loc' and a
1383 // new 'ver'. Note: Only valid for 'ver' equals one of {0, 2, 4}.
1384 static int locver2nextf[4][6][2];
1385
1386 // For enumerating three edges of a triangle.
1387 static int plus1mod3[3];
1388 static int minus1mod3[3];
1389
1390 ///////////////////////////////////////////////////////////////////////////////
1391 // //
1392 // Mesh manipulation primitives //
1393 // //
1394 // A serial of mesh operations such as topological maintenance, navigation, //
1395 // local modification, etc., is accomplished through a set of mesh manipul- //
1396 // ation primitives. These primitives are indeed very simple functions which //
1397 // take one or two handles ('triface's and 'face's) as parameters, perform //
1398 // basic operations such as "glue two tetrahedra at a face", "return the //
1399 // origin of a tetrahedron", "return the subface adjoining at the face of a //
1400 // tetrahedron", and so on. //
1401 // //
1402 ///////////////////////////////////////////////////////////////////////////////
1403
1404 // Primitives for tetrahedra.
1405 inline void decode(tetrahedron ptr, triface& t);
1406 inline tetrahedron encode(triface& t);
1407 inline void sym(triface& t1, triface& t2);
1408 inline void symself(triface& t);
1409 inline void bond(triface& t1, triface& t2);
1410 inline void dissolve(triface& t);
1411 inline point org(triface& t);
1412 inline point dest(triface& t);
1413 inline point apex(triface& t);
1414 inline point oppo(triface& t);
1415 inline void setorg(triface& t, point pointptr);
1416 inline void setdest(triface& t, point pointptr);
1417 inline void setapex(triface& t, point pointptr);
1418 inline void setoppo(triface& t, point pointptr);
1419 inline void esym(triface& t1, triface& t2);
1420 inline void esymself(triface& t);
1421 inline void enext(triface& t1, triface& t2);
1422 inline void enextself(triface& t);
1423 inline void enext2(triface& t1, triface& t2);
1424 inline void enext2self(triface& t);
1425 inline bool fnext(triface& t1, triface& t2);
1426 inline bool fnextself(triface& t);
1427 inline void enextfnext(triface& t1, triface& t2);
1428 inline void enextfnextself(triface& t);
1429 inline void enext2fnext(triface& t1, triface& t2);
1430 inline void enext2fnextself(triface& t);
1431 inline void infect(triface& t);
1432 inline void uninfect(triface& t);
1433 inline bool infected(triface& t);
1434 inline REAL elemattribute(tetrahedron* ptr, int attnum);
1435 inline void setelemattribute(tetrahedron* ptr, int attnum, REAL value);
1436 inline REAL volumebound(tetrahedron* ptr);
1437 inline void setvolumebound(tetrahedron* ptr, REAL value);
1438
1439 // Primitives for subfaces and subsegments.
1440 inline void sdecode(shellface sptr, face& s);
1441 inline shellface sencode(face& s);
1442 inline void spivot(face& s1, face& s2);
1443 inline void spivotself(face& s);
1444 inline void sbond(face& s1, face& s2);
1445 inline void sbond1(face& s1, face& s2);
1446 inline void sdissolve(face& s);
1447 inline point sorg(face& s);
1448 inline point sdest(face& s);
1449 inline point sapex(face& s);
1450 inline void setsorg(face& s, point pointptr);
1451 inline void setsdest(face& s, point pointptr);
1452 inline void setsapex(face& s, point pointptr);
1453 inline void sesym(face& s1, face& s2);
1454 inline void sesymself(face& s);
1455 inline void senext(face& s1, face& s2);
1456 inline void senextself(face& s);
1457 inline void senext2(face& s1, face& s2);
1458 inline void senext2self(face& s);
1459 inline void sfnext(face&, face&);
1460 inline void sfnextself(face&);
1461 inline badface* shell2badface(face& s);
1462 inline void setshell2badface(face& s, badface* value);
1463 inline REAL areabound(face& s);
1464 inline void setareabound(face& s, REAL value);
1465 inline int shellmark(face& s);
1466 inline void setshellmark(face& s, int value);
1467 inline enum shestype shelltype(face& s);
1468 inline void setshelltype(face& s, enum shestype value);
1469 inline int shellpbcgroup(face& s);
1470 inline void setshellpbcgroup(face& s, int value);
1471 inline void sinfect(face& s);
1472 inline void suninfect(face& s);
1473 inline bool sinfected(face& s);
1474
1475 // Primitives for interacting tetrahedra and subfaces.
1476 inline void tspivot(triface& t, face& s);
1477 inline void stpivot(face& s, triface& t);
1478 inline void tsbond(triface& t, face& s);
1479 inline void tsdissolve(triface& t);
1480 inline void stdissolve(face& s);
1481
1482 // Primitives for interacting subfaces and subsegs.
1483 inline void sspivot(face& s, face& edge);
1484 inline void ssbond(face& s, face& edge);
1485 inline void ssdissolve(face& s);
1486
1487 // Primitives for points.
1488 inline int pointmark(point pt);
1489 inline void setpointmark(point pt, int value);
1490 inline enum verttype pointtype(point pt);
1491 inline void setpointtype(point pt, enum verttype value);
1492 inline tetrahedron point2tet(point pt);
1493 inline void setpoint2tet(point pt, tetrahedron value);
1494 inline shellface point2sh(point pt);
1495 inline void setpoint2sh(point pt, shellface value);
1496 inline point point2ppt(point pt);
1497 inline void setpoint2ppt(point pt, point value);
1498 inline tetrahedron point2bgmtet(point pt);
1499 inline void setpoint2bgmtet(point pt, tetrahedron value);
1500 inline point point2pbcpt(point pt);
1501 inline void setpoint2pbcpt(point pt, point value);
1502
1503 // Advanced primitives.
1504 inline void adjustedgering(triface& t, int direction);
1505 inline void adjustedgering(face& s, int direction);
1506 inline bool isdead(triface* t);
1507 inline bool isdead(face* s);
1508 inline bool isfacehaspoint(triface* t, point testpoint);
1509 inline bool isfacehaspoint(face* t, point testpoint);
1510 inline bool isfacehasedge(face* s, point tend1, point tend2);
1511 inline bool issymexist(triface* t);
1512 bool getnextface(triface*, triface*);
1513 void getnextsface(face*, face*);
1514 void tsspivot(triface*, face*);
1515 void sstpivot(face*, triface*);
1516 bool findorg(triface* t, point dorg);
1517 bool findorg(face* s, point dorg);
1518 void findedge(triface* t, point eorg, point edest);
1519 void findedge(face* s, point eorg, point edest);
1520 void findface(triface *fface, point forg, point fdest, point fapex);
1521 void getonextseg(face* s, face* lseg);
1522 void getseghasorg(face* sseg, point dorg);
1523 point getsubsegfarorg(face* sseg);
1524 point getsubsegfardest(face* sseg);
1525 void printtet(triface*);
1526 void printsh(face*);
1527
1528 ///////////////////////////////////////////////////////////////////////////////
1529 // //
1530 // Triangle-triangle intersection test //
1531 // //
1532 // The triangle-triangle intersection test is implemented with exact arithm- //
1533 // etic. It exactly tells whether or not two triangles in three dimensions //
1534 // intersect. Before implementing this test myself, I tried two C codes //
1535 // (implemented by Thomas Moeller and Philippe Guigue, respectively), which //
1536 // are all public available. However both of them failed frequently. Another //
1537 // unconvenience is both codes only tell whether or not the two triangles //
1538 // intersect without distinguishing the cases whether they exactly intersect //
1539 // in interior or they just share a vertex or share an edge. The two latter //
1540 // cases are acceptable and should return not intersection in TetGen. //
1541 // //
1542 ///////////////////////////////////////////////////////////////////////////////
1543
1544 enum interresult edge_vert_col_inter(REAL*, REAL*, REAL*);
1545 enum interresult edge_edge_cop_inter(REAL*, REAL*, REAL*, REAL*, REAL*);
1546 enum interresult tri_vert_cop_inter(REAL*, REAL*, REAL*, REAL*, REAL*);
1547 enum interresult tri_edge_cop_inter(REAL*, REAL*, REAL*, REAL*, REAL*,
1548 REAL*);
1549 enum interresult tri_edge_inter_tail(REAL*, REAL*, REAL*, REAL*, REAL*,
1550 REAL, REAL);
1551 enum interresult tri_edge_inter(REAL*, REAL*, REAL*, REAL*, REAL*);
1552 enum interresult tri_tri_inter(REAL*, REAL*, REAL*, REAL*, REAL*, REAL*);
1553
1554 // Geometric predicates
1555 REAL insphere_sos(REAL*, REAL*, REAL*, REAL*, REAL*, int, int, int, int,
1556 int);
1557 bool iscollinear(REAL*, REAL*, REAL*, REAL eps);
1558 bool iscoplanar(REAL*, REAL*, REAL*, REAL*, REAL vol6, REAL eps);
1559 bool iscospheric(REAL*, REAL*, REAL*, REAL*, REAL*, REAL vol24, REAL eps);
1560
1561 // Linear algebra functions
1562 inline REAL dot(REAL* v1, REAL* v2);
1563 inline void cross(REAL* v1, REAL* v2, REAL* n);
1564 bool lu_decmp(REAL lu[4][4], int n, int* ps, REAL* d, int N);
1565 void lu_solve(REAL lu[4][4], int n, int* ps, REAL* b, int N);
1566
1567 // Geometric quantities calculators.
1568 inline REAL distance(REAL* p1, REAL* p2);
1569 REAL shortdistance(REAL* p, REAL* e1, REAL* e2);
1570 REAL shortdistance(REAL* p, REAL* e1, REAL* e2, REAL* e3);
1571 REAL interiorangle(REAL* o, REAL* p1, REAL* p2, REAL* n);
1572 void projpt2edge(REAL* p, REAL* e1, REAL* e2, REAL* prj);
1573 void projpt2face(REAL* p, REAL* f1, REAL* f2, REAL* f3, REAL* prj);
1574 void facenormal(REAL* pa, REAL* pb, REAL* pc, REAL* n, REAL* nlen);
1575 void edgeorthonormal(REAL* e1, REAL* e2, REAL* op, REAL* n);
1576 REAL facedihedral(REAL* pa, REAL* pb, REAL* pc1, REAL* pc2);
1577 void tetalldihedral(point, point, point, point, REAL*, REAL*, REAL*);
1578 void tetallnormal(point, point, point, point, REAL N[4][3], REAL* volume);
1579 bool circumsphere(REAL*, REAL*, REAL*, REAL*, REAL* cent, REAL* radius);
1580 void inscribedsphere(REAL*, REAL*, REAL*, REAL*, REAL* cent, REAL* radius);
1581 void rotatepoint(REAL* p, REAL rotangle, REAL* p1, REAL* p2);
1582 void spherelineint(REAL* p1, REAL* p2, REAL* C, REAL R, REAL p[7]);
1583 void linelineint(REAL *p1,REAL *p2, REAL *p3, REAL *p4, REAL p[7]);
1584 void planelineint(REAL*, REAL*, REAL*, REAL*, REAL*, REAL*, REAL*);
1585
1586 // Memory managment routines.
1587 void dummyinit(int, int);
1588 void initializepools();
1589 void tetrahedrondealloc(tetrahedron*);
1590 tetrahedron *tetrahedrontraverse();
1591 void shellfacedealloc(memorypool*, shellface*);
1592 shellface *shellfacetraverse(memorypool*);
1593 void badfacedealloc(memorypool*, badface*);
1594 badface *badfacetraverse(memorypool*);
1595 void pointdealloc(point);
1596 point pointtraverse();
1597 void maketetrahedron(triface*);
1598 void makeshellface(memorypool*, face*);
1599 void makepoint(point*);
1600
1601 // Mesh items searching routines.
1602 void makepoint2tetmap();
1603 void makeindex2pointmap(point*& idx2verlist);
1604 void makesegmentmap(int*& idx2seglist, shellface**& segsperverlist);
1605 void makesubfacemap(int*& idx2facelist, shellface**& facesperverlist);
1606 void maketetrahedronmap(int*& idx2tetlist, tetrahedron**& tetsperverlist);
1607
1608 // Point location routines.
1609 unsigned long randomnation(unsigned int choices);
1610 REAL distance2(tetrahedron* tetptr, point p);
1611 enum locateresult preciselocate(point searchpt, triface* searchtet, long);
1612 enum locateresult locate(point searchpt, triface* searchtet);
1613 enum locateresult adjustlocate(point, triface*, enum locateresult, REAL);
1614 enum locateresult locatesub(point searchpt, face* searchsh, int, REAL);
1615 enum locateresult adjustlocatesub(point, face*, enum locateresult, REAL);
1616 enum locateresult locateseg(point searchpt, face* searchseg);
1617 enum locateresult adjustlocateseg(point, face*, enum locateresult, REAL);
1618
1619 ///////////////////////////////////////////////////////////////////////////////
1620 // //
1621 // Mesh Local Transformation Operators //
1622 // //
1623 // These operators (including flips, insert & remove vertices and so on) are //
1624 // used to transform (or replace) a set of mesh elements into another set of //
1625 // mesh elements. //
1626 // //
1627 ///////////////////////////////////////////////////////////////////////////////
1628
1629 // Mesh transformation routines.
1630 enum fliptype categorizeface(triface& horiz);
1631 void enqueueflipface(triface& checkface, queue* flipqueue);
1632 void enqueueflipedge(face& checkedge, queue* flipqueue);
1633 void flip23(triface* flipface, queue* flipqueue);
1634 void flip32(triface* flipface, queue* flipqueue);
1635 void flip22(triface* flipface, queue* flipqueue);
1636 void flip22sub(face* flipedge, queue* flipqueue);
1637 long flip(queue* flipqueue, badface **plastflip);
1638 void undoflip(badface *lastflip);
1639 long flipsub(queue* flipqueue);
1640
1641 void splittetrahedron(point newpoint, triface* splittet, queue* flipqueue);
1642 void unsplittetrahedron(triface* splittet);
1643 void splittetface(point newpoint, triface* splittet, queue* flipqueue);
1644 void unsplittetface(triface* splittet);
1645 void splitsubface(point newpoint, face* splitface, queue* flipqueue);
1646 void unsplitsubface(face* splitsh);
1647 void splittetedge(point newpoint, triface* splittet, queue* flipqueue);
1648 void unsplittetedge(triface* splittet);
1649 void splitsubedge(point newpoint, face* splitsh, queue* flipqueue);
1650 void unsplitsubedge(face* splitsh);
1651 enum insertsiteresult insertsite(point newpoint, triface* searchtet,
1652 bool approx, queue* flipqueue);
1653 void undosite(enum insertsiteresult insresult, triface* splittet,
1654 point torg, point tdest, point tapex, point toppo);
1655
1656 void formbowatcavitysub(point, face*, list*, list*);
1657 void formbowatcavityquad(point, list*, list*);
1658 void formbowatcavitysegquad(point, list*, list*);
1659 void formbowatcavity(point bp, face* bpseg, face* bpsh, int* n, int* nmax,
1660 list** sublists, list** subceillists, list** tetlists,
1661 list** ceillists);
1662 void releasebowatcavity(face*, int, list**, list**, list**, list**);
1663 bool validatebowatcavityquad(point bp, list* ceillist, REAL maxcosd);
1664 void updatebowatcavityquad(list* tetlist, list* ceillist);
1665 void updatebowatcavitysub(list* sublist, list* subceillist, int* cutcount);
1666 bool trimbowatcavity(point bp, face* bpseg, int n, list** sublists,
1667 list** subceillists, list** tetlists,list** ceillists,
1668 REAL maxcosd);
1669 void bowatinsertsite(point bp, face* splitseg, int n, list** sublists,
1670 list** subceillists, list** tetlists,
1671 list** ceillists, list* verlist, queue* flipque,
1672 bool chkencseg, bool chkencsub, bool chkbadtet);
1673
1674 // Delaunay tetrahedralization routines.
1675 void formstarpolyhedron(point pt, list* tetlist, list* verlist, bool);
1676 bool unifypoint(point testpt, triface*, enum locateresult, REAL);
1677 void closeopenface(triface* openface, queue* flipque);
1678 void inserthullsite(point inspoint, triface* horiz, queue* flipque);
1679 void incrflipdelaunay(triface*, point*, long, bool, bool, REAL, queue*);
1680 long delaunizevertices();
1681
1682 // Surface triangulation routines.
1683 void formstarpolygon(point pt, list* trilist, list* verlist);
1684 void getfacetabovepoint(face* facetsh);
1685 void collectcavsubs(point newpoint, list* cavsublist);
1686 void collectvisiblesubs(int shmark, point inspoint, face* horiz, queue*);
1687 void incrflipdelaunaysub(int shmark, REAL eps, list*, int, REAL*, queue*);
1688 enum finddirectionresult finddirectionsub(face* searchsh, point tend);
1689 void insertsubseg(face* tri);
1690 bool scoutsegmentsub(face* searchsh, point tend);
1691 void flipedgerecursive(face* flipedge, queue* flipqueue);
1692 void constrainededge(face* startsh, point tend, queue* flipqueue);
1693 void recoversegment(point tstart, point tend, queue* flipqueue);
1694 void infecthullsub(memorypool* viri);
1695 void plaguesub(memorypool* viri);
1696 void carveholessub(int holes, REAL* holelist, memorypool* viri);
1697 void triangulate(int shmark, REAL eps, list* ptlist, list* conlist,
1698 int holes, REAL* holelist, memorypool* viri, queue*);
1699 void retrievenewsubs(list* newshlist, bool removeseg);
1700 void unifysegments();
1701 void mergefacets(queue* flipqueue);
1702 long meshsurface();
1703
1704 // Detect intersecting facets of PLC.
1705 void interecursive(shellface** subfacearray, int arraysize, int axis,
1706 REAL bxmin, REAL bxmax, REAL bymin, REAL bymax,
1707 REAL bzmin, REAL bzmax, int* internum);
1708 void detectinterfaces();
1709
1710 // Periodic boundary condition supporting routines.
1711 void createsubpbcgrouptable();
1712 void getsubpbcgroup(face* pbcsub, pbcdata** pd, int *f1, int *f2);
1713 enum locateresult getsubpbcsympoint(point, face*, point, face*);
1714 void createsegpbcgrouptable();
1715 enum locateresult getsegpbcsympoint(point, face*, point, face*, int);
1716
1717 // Vertex perturbation routines.
1718 REAL randgenerator(REAL range);
1719 bool checksub4cocir(face* testsub, REAL eps, bool once, bool enqflag);
1720 void tallcocirsubs(REAL eps, bool enqflag);
1721 bool tallencsegsfsubs(point testpt, list* cavsublist);
1722 void collectflipedges(point inspoint, face* splitseg, queue* flipqueue);
1723 void perturbrepairencsegs(queue* flipqueue);
1724 void perturbrepairencsubs(list* cavsublist, queue* flipqueue);
1725 void incrperturbvertices(REAL eps);
1726
1727 // Segment recovery routines.
1728 void markacutevertices(REAL acuteangle);
1729 enum finddirectionresult finddirection(triface* searchtet, point, long);
1730 void getsearchtet(point p1, point p2, triface* searchtet, point* tend);
1731 bool isedgeencroached(point p1, point p2, point testpt, bool degflag);
1732 point scoutrefpoint(triface* searchtet, point tend);
1733 point getsegmentorigin(face* splitseg);
1734 point getsplitpoint(face* splitseg, point refpoint);
1735 void delaunizesegments();
1736
1737 // Facets recovery routines.
1738 bool insertsubface(face* insertsh, triface* searchtet);
1739 bool tritritest(triface* checktet, point p1, point p2, point p3);
1740 void initializecavity(list* floorlist, list* ceillist, list* frontlist);
1741 void delaunizecavvertices(triface*, list*, list*, list*, queue*);
1742 void retrievenewtets(list* newtetlist);
1743 void insertauxsubface(triface* front, triface* idfront);
1744 bool scoutfront(triface* front, triface* idfront, list* newtetlist);
1745 void gluefronts(triface* front, triface* front1);
1746 bool identifyfronts(list* frontlist, list* misfrontlist, list* newtetlist);
1747 void detachauxsubfaces(list* newtetlist);
1748 void expandcavity(list* frontlist, list* misfrontlist, list* newtetlist,
1749 list* crosstetlist, queue* missingshqueue, queue*);
1750 void carvecavity(list* newtetlist, list* outtetlist, queue* flipque);
1751 void delaunizecavity(list* floorlist, list* ceillist, list* ceilptlist,
1752 list* floorptlist, list* frontlist,list* misfrontlist,
1753 list* newtetlist, list* crosstetlist, queue*, queue*);
1754 void formmissingregion(face* missingsh, list* missingshlist,
1755 list* equatptlist, int* worklist);
1756 void formcavity(list* missingshlist, list* crossedgelist,
1757 list* equatptlist, list* crossshlist, list* crosstetlist,
1758 list* belowfacelist, list* abovefacelist,
1759 list* horizptlist, list* belowptlist, list* aboveptlist,
1760 queue* missingshqueue, int* worklist);
1761 bool scoutcrossingedge(list* missingshlist, list* boundedgelist,
1762 list* crossedgelist, int* worklist);
1763 void rearrangesubfaces(list* missingshlist, list* boundedgelist,
1764 list* equatptlist, int* worklist);
1765 void insertallsubfaces(queue* missingshqueue);
1766 void constrainedfacets();
1767
1768 // Carving out holes and concavities routines.
1769 void infecthull(memorypool *viri);
1770 void plague(memorypool *viri);
1771 void regionplague(memorypool *viri, REAL attribute, REAL volume);
1772 void removeholetets(memorypool *viri);
1773 void assignregionattribs();
1774 void carveholes();
1775
1776 // Steiner points removing routines.
1777 void replacepolygonsubs(list* oldshlist, list* newshlist);
1778 void orientnewsubs(list* newshlist, face* orientsh, REAL* norm);
1779 bool constrainedflip(triface* flipface, triface* front, queue* flipque);
1780 bool recoverfront(triface* front, list* newtetlist, queue* flipque);
1781 void repairflips(queue* flipque);
1782 bool constrainedcavity(triface* oldtet, list* floorlist, list* ceillist,
1783 list* ptlist, list* frontlist, list* misfrontlist,
1784 list* newtetlist, queue* flipque);
1785 void expandsteinercavity(point steinpt, REAL eps, list* frontlist, list*);
1786 bool findrelocatepoint(point sp, point np, REAL* n, list*, list*);
1787 void relocatepoint(point steinpt, triface* oldtet, list*, list*, queue*);
1788 bool findcollapseedge(point suppt, point* conpt, list* oldtetlist, list*);
1789 void collapseedge(point suppt, point conpt, list* oldtetlist, list*);
1790 void deallocfaketets(list* frontlist);
1791 void restorepolyhedron(list* oldtetlist);
1792 bool suppressfacetpoint(face* supsh, list* frontlist, list* misfrontlist,
1793 list* ptlist, list* conlist, memorypool* viri,
1794 queue* flipque);
1795 bool suppresssegpoint(face* supseg, list* spinshlist, list* newsegshlist,
1796 list* frontlist, list* misfrontlist, list* ptlist,
1797 list* conlist, memorypool* viri, queue* flipque);
1798 bool suppressvolpoint(point suppt, list* frontlist, list* misfrontlist,
1799 list* ptlist, queue* flipque);
1800 bool collapseedgepoint(point colpt, list* oldtetlist, list* newtetlist,
1801 list* ptlist);
1802 void removesteiners();
1803
1804 // Mesh reconstruction rotuines.
1805 long reconstructmesh();
1806 bool intettest(point testpt, triface* testtet, REAL eps);
1807 void insertaddpoints();
1808
1809 // Background mesh operations.
1810 bool interpolatepointsize(point pt, triface* bgmtet, long *scount);
1811 void searchpointrecursive(triface *curtet, long *scount);
1812 void interpolatesizemap();
1813
1814 // Delaunay refinement routines.
1815 void calclocalfeaturesizes();
1816 void marksharpsubsegs(REAL dihedbound);
1817 void markskinnysubfaces(REAL anglebound);
1818 void enqueuebadtet(triface* tt, REAL key, REAL* cent);
1819 void enqueueencsub(face* ss, point encpt, int quenumber, REAL* cent);
1820 badface* dequeuebadtet();
1821 badface* dequeueencsub(int* quenumber);
1822 bool checkseg4encroach(face* testseg, point testpt, point*, bool enqflag);
1823 bool checksub4encroach(face* testsub, point testpt, bool enqflag);
1824 bool checkseg4badqual(face* testseg, bool enqflag);
1825 bool checksub4badqual(face* testsub, bool enqflag);
1826 bool checktet4badqual(triface* testtet, bool enqflag);
1827 bool acceptsegpt(point segpt, point refpt, face* splitseg);
1828 bool acceptfacpt(point facpt, list* subceillist, list* verlist);
1829 bool acceptvolpt(point volpt, list* ceillist, list* verlist);
1830 void getsplitpoint(point e1, point e2, point refpt, point newpt);
1831 void shepardinterpolate(point newpt, list* verlist);
1832 void setnewpointsize(point newpt, list* verlist);
1833 void splitencseg(point, face*, list*, list*, list*, queue*, bool, bool);
1834 bool tallencsegs(point testpt, int n, list** ceillists);
1835 bool tallencsubs(point testpt, int n, list** ceillists);
1836 void tallbadtetrahedrons();
1837 void repairencsegs(bool chkencsub, bool chkbadtet);
1838 void repairencsubs(bool chkbadtet);
1839 void repairbadtets();
1840 void enforcequality();
1841
1842 // Mesh Smoothing routines.
1843 bool checktet4ill(triface* testtet, bool enqflag);
1844 bool checktet4sliver(triface* testtet, bool chkill, bool enqflag);
1845 void removetetbystripoff(triface *striptet);
1846 void removetetbyflip32(triface *fliptet, bool enq, bool chkill);
1847 bool removetetbyrecon(badface* remtet, bool chkill);
1848 bool removetetbysplit(badface* remtet);
1849 void tallslivers(bool chkill);
1850 void repairmesh();
1851 void smoothmesh();
1852
1853 // I/O routines
1854 void transfernodes();
1855 void jettisonnodes();
1856 void highorder();
1857 void outnodes(tetgenio* out);
1858 void outmetrics(tetgenio* out);
1859 void outelements(tetgenio* out);
1860 void outfaces(tetgenio* out);
1861 void outhullfaces(tetgenio* out);
1862 void outsubfaces(tetgenio* out);
1863 void outsubsegments(tetgenio* out);
1864 void outneighbors(tetgenio* out);
1865 void outpbcnodes(tetgenio* out);
1866 void outsmesh(char* smfilename);
1867 void outmesh2medit(char* mfilename);
1868 void outmesh2gid(char* gfilename);
1869 void outmesh2off(char* ofilename);
1870
1871 // User interaction routines.
1872 void internalerror();
1873 void checkmesh();
1874 void checkshells();
1875 void checkdelaunay(REAL eps, queue* flipqueue);
1876 void checkdegeneracy(REAL eps);
1877 void checkconforming();
1878 void algorithmicstatistics();
1879 void qualitystatistics();
1880 void statistics();
1881
1882 public:
1883
1884 // Constructor and destructor.
1885 tetgenmesh();
1886 ~tetgenmesh();
1887
1888 }; // End of class tetgenmesh.
1889
1890 ///////////////////////////////////////////////////////////////////////////////
1891 // //
1892 // tetrahedralize() Interface for using TetGen's library to generate //
1893 // Delaunay tetrahedralizations, constrained Delaunay //
1894 // tetrahedralizations, quality tetrahedral meshes. //
1895 // //
1896 // Two functions (interfaces) are available. The difference is only the way //
1897 // of passing switches. One directly accepts an object of 'tetgenbehavior', //
1898 // while the other accepts a string which is the same as one can used in the //
1899 // command line. The latter may be more convenient for users who don't want //
1900 // to kown the 'tetgenbehavir' structure. //
1901 // //
1902 // 'in' is an object of 'tetgenio' which contains a PLC you want to tetrahed-//
1903 // ralize or a previously generated tetrahedral mesh you want to refine. It //
1904 // must not be a NULL. 'out' is another object of 'tetgenio' for storing the //
1905 // generated tetrahedral mesh. It can be a NULL. If so, the output will be //
1906 // saved to file(s). //
1907 // //
1908 ///////////////////////////////////////////////////////////////////////////////
1909
1910 void tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
1911 tetgenio *bgmesh = NULL);
1912 void tetrahedralize(char *switches, tetgenio *in, tetgenio *out,
1913 tetgenio *bgmesh = NULL);
1914
1915 #endif // #ifndef tetgenH