Triangle.cpp 377 KB
Newer Older
Michael Turner's avatar
files    
Michael Turner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
////////////////////////////////////////////////////////////////////////////////
//
//  File: Triangle.cpp
//
//  For more information, please see: http://www.nektar.info/
//
//  The MIT License
//
//  Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
//  Department of Aeronautics, Imperial College London (UK), and Scientific
//  Computing and Imaging Institute, University of Utah (USA).
//
//  License for the specific language governing rights and limitations under
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included
//  in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
//  Description: class for triangle, originally the code of Jonathan Shewchuk
//               but heavily modified.
//               original file header below
//
////////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/*                                                                           */
/*      888888888        ,o,                          / 888                  */
/*         888    88o88o  "    o8888o  88o8888o o88888o 888  o88888o         */
/*         888    888    888       88b 888  888 888 888 888 d888  88b        */
/*         888    888    888  o88^o888 888  888 "88888" 888 8888oo888        */
/*         888    888    888 C888  888 888  888  /      888 q888             */
/*         888    888    888  "88o^888 888  888 Cb      888  "88oooo"        */
/*                                              "8oo8D                       */
/*                                                                           */
/*  A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator.      */
/*  (triangle.c)                                                             */
/*                                                                           */
/*  Version 1.6                                                              */
/*  July 28, 2005                                                            */
/*                                                                           */
/*  Copyright 1993, 1995, 1997, 1998, 2002, 2005                             */
/*  Jonathan Richard Shewchuk                                                */
/*  2360 Woolsey #H                                                          */
/*  Berkeley, California  94705-1927                                         */
/*  jrs@cs.berkeley.edu                                                      */
/*                                                                           */
/*  This program may be freely redistributed under the condition that the    */
/*    copyright notices (including this entire header and the copyright      */
/*    notice printed when the `-h' switch is selected) are not removed, and  */
/*    no compensation is received.  Private, research, and institutional     */
/*    use is free.  You may distribute modified versions of this code UNDER  */
/*    THE CONDITION THAT THIS CODE AND ANY MODIFICATIONS MADE TO IT IN THE   */
/*    SAME FILE REMAIN UNDER COPYRIGHT OF THE ORIGINAL AUTHOR, BOTH SOURCE   */
/*    AND OBJECT CODE ARE MADE FREELY AVAILABLE WITHOUT CHARGE, AND CLEAR    */
/*    NOTICE IS GIVEN OF THE MODIFICATIONS.  Distribution of this code as    */
/*    part of a commercial system is permissible ONLY BY DIRECT ARRANGEMENT  */
/*    WITH THE AUTHOR.  (If you are not directly supplying this code to a    */
/*    customer, and you are instead telling them how they can obtain it for  */
/*    free, then you are not required to make any arrangement with me.)      */
/*                                                                           */
/*  Hypertext instructions for Triangle are available on the Web at          */
/*                                                                           */
/*      http://www.cs.cmu.edu/~quake/triangle.html                           */
/*                                                                           */
/*  Disclaimer:  Neither I nor Carnegie Mellon warrant this code in any way  */
/*    whatsoever.  This code is provided "as-is".  Use at your own risk.     */
/*                                                                           */
/*  Some of the references listed below are marked with an asterisk.  [*]    */
/*    These references are available for downloading from the Web page       */
/*                                                                           */
/*      http://www.cs.cmu.edu/~quake/triangle.research.html                  */
/*                                                                           */
/*  Three papers discussing aspects of Triangle are available.  A short      */
/*    overview appears in "Triangle:  Engineering a 2D Quality Mesh          */
/*    Generator and Delaunay Triangulator," in Applied Computational         */
/*    Geometry:  Towards Geometric Engineering, Ming C. Lin and Dinesh       */
/*    Manocha, editors, Lecture Notes in Computer Science volume 1148,       */
/*    pages 203-222, Springer-Verlag, Berlin, May 1996 (from the First ACM   */
/*    Workshop on Applied Computational Geometry).  [*]                      */
/*                                                                           */
/*    The algorithms are discussed in the greatest detail in "Delaunay       */
/*    Refinement Algorithms for Triangular Mesh Generation," Computational   */
/*    Geometry:  Theory and Applications 22(1-3):21-74, May 2002.  [*]       */
/*                                                                           */
/*    More detail about the data structures may be found in my dissertation: */
/*    "Delaunay Refinement Mesh Generation," Ph.D. thesis, Technical Report  */
/*    CMU-CS-97-137, School of Computer Science, Carnegie Mellon University, */
/*    Pittsburgh, Pennsylvania, 18 May 1997.  [*]                            */
/*                                                                           */
/*  Triangle was created as part of the Quake Project in the School of       */
/*    Computer Science at Carnegie Mellon University.  For further           */
/*    information, see Hesheng Bao, Jacobo Bielak, Omar Ghattas, Loukas F.   */
/*    Kallivokas, David R. O'Hallaron, Jonathan R. Shewchuk, and Jifeng Xu,  */
/*    "Large-scale Simulation of Elastic Wave Propagation in Heterogeneous   */
/*    Media on Parallel Computers," Computer Methods in Applied Mechanics    */
/*    and Engineering 152(1-2):85-102, 22 January 1998.                      */
/*                                                                           */
/*  Triangle's Delaunay refinement algorithm for quality mesh generation is  */
/*    a hybrid of one due to Jim Ruppert, "A Delaunay Refinement Algorithm   */
/*    for Quality 2-Dimensional Mesh Generation," Journal of Algorithms      */
/*    18(3):548-585, May 1995 [*], and one due to L. Paul Chew, "Guaranteed- */
/*    Quality Mesh Generation for Curved Surfaces," Proceedings of the Ninth */
/*    Annual Symposium on Computational Geometry (San Diego, California),    */
/*    pages 274-280, Association for Computing Machinery, May 1993,          */
/*    http://portal.acm.org/citation.cfm?id=161150 .                         */
/*                                                                           */
/*  The Delaunay refinement algorithm has been modified so that it meshes    */
/*    domains with small input angles well, as described in Gary L. Miller,  */
/*    Steven E. Pav, and Noel J. Walkington, "When and Why Ruppert's         */
/*    Algorithm Works," Twelfth International Meshing Roundtable, pages      */
/*    91-102, Sandia National Laboratories, September 2003.  [*]             */
/*                                                                           */
/*  My implementation of the divide-and-conquer and incremental Delaunay     */
/*    triangulation algorithms follows closely the presentation of Guibas    */
/*    and Stolfi, even though I use a triangle-based data structure instead  */
/*    of their quad-edge data structure.  (In fact, I originally implemented */
/*    Triangle using the quad-edge data structure, but the switch to a       */
/*    triangle-based data structure sped Triangle by a factor of two.)  The  */
/*    mesh manipulation primitives and the two aforementioned Delaunay       */
/*    triangulation algorithms are described by Leonidas J. Guibas and Jorge */
/*    Stolfi, "Primitives for the Manipulation of General Subdivisions and   */
/*    the Computation of Voronoi Diagrams," ACM Transactions on Graphics     */
/*    4(2):74-123, April 1985, http://portal.acm.org/citation.cfm?id=282923 .*/
/*                                                                           */
/*  Their O(n log n) divide-and-conquer algorithm is adapted from Der-Tsai   */
/*    Lee and Bruce J. Schachter, "Two Algorithms for Constructing the       */
/*    Delaunay Triangulation," International Journal of Computer and         */
/*    Information Science 9(3):219-242, 1980.  Triangle's improvement of the */
/*    divide-and-conquer algorithm by alternating between vertical and       */
/*    horizontal cuts was introduced by Rex A. Dwyer, "A Faster Divide-and-  */
/*    Conquer Algorithm for Constructing Delaunay Triangulations,"           */
/*    Algorithmica 2(2):137-151, 1987.                                       */
/*                                                                           */
/*  The incremental insertion algorithm was first proposed by C. L. Lawson,  */
/*    "Software for C1 Surface Interpolation," in Mathematical Software III, */
/*    John R. Rice, editor, Academic Press, New York, pp. 161-194, 1977.     */
/*    For point location, I use the algorithm of Ernst P. Mucke, Isaac       */
/*    Saias, and Binhai Zhu, "Fast Randomized Point Location Without         */
/*    Preprocessing in Two- and Three-Dimensional Delaunay Triangulations,"  */
/*    Proceedings of the Twelfth Annual Symposium on Computational Geometry, */
/*    ACM, May 1996.  [*]  If I were to randomize the order of vertex        */
/*    insertion (I currently don't bother), their result combined with the   */
/*    result of Kenneth L. Clarkson and Peter W. Shor, "Applications of      */
/*    Random Sampling in Computational Geometry II," Discrete &              */
/*    Computational Geometry 4(1):387-421, 1989, would yield an expected     */
/*    O(n^{4/3}) bound on running time.                                      */
/*                                                                           */
/*  The O(n log n) sweepline Delaunay triangulation algorithm is taken from  */
/*    Steven Fortune, "A Sweepline Algorithm for Voronoi Diagrams",          */
/*    Algorithmica 2(2):153-174, 1987.  A random sample of edges on the      */
/*    boundary of the triangulation are maintained in a splay tree for the   */
/*    purpose of point location.  Splay trees are described by Daniel        */
/*    Dominic Sleator and Robert Endre Tarjan, "Self-Adjusting Binary Search */
/*    Trees," Journal of the ACM 32(3):652-686, July 1985,                   */
/*    http://portal.acm.org/citation.cfm?id=3835 .                           */
/*                                                                           */
/*  The algorithms for exact computation of the signs of determinants are    */
/*    described in Jonathan Richard Shewchuk, "Adaptive Precision Floating-  */
/*    Point Arithmetic and Fast Robust Geometric Predicates," Discrete &     */
/*    Computational Geometry 18(3):305-363, October 1997.  (Also available   */
/*    as Technical Report CMU-CS-96-140, School of Computer Science,         */
/*    Carnegie Mellon University, Pittsburgh, Pennsylvania, May 1996.)  [*]  */
/*    An abbreviated version appears as Jonathan Richard Shewchuk, "Robust   */
/*    Adaptive Floating-Point Geometric Predicates," Proceedings of the      */
/*    Twelfth Annual Symposium on Computational Geometry, ACM, May 1996. [*] */
/*    Many of the ideas for my exact arithmetic routines originate with      */
/*    Douglas M. Priest, "Algorithms for Arbitrary Precision Floating Point  */
/*    Arithmetic," Tenth Symposium on Computer Arithmetic, pp. 132-143, IEEE */
/*    Computer Society Press, 1991.  [*]  Many of the ideas for the correct  */
/*    evaluation of the signs of determinants are taken from Steven Fortune  */
/*    and Christopher J. Van Wyk, "Efficient Exact Arithmetic for Computa-   */
/*    tional Geometry," Proceedings of the Ninth Annual Symposium on         */
/*    Computational Geometry, ACM, pp. 163-172, May 1993, and from Steven    */
/*    Fortune, "Numerical Stability of Algorithms for 2D Delaunay Triangu-   */
/*    lations," International Journal of Computational Geometry & Applica-   */
/*    tions 5(1-2):193-213, March-June 1995.                                 */
/*                                                                           */
/*  The method of inserting new vertices off-center (not precisely at the    */
/*    circumcenter of every poor-quality triangle) is from Alper Ungor,      */
/*    "Off-centers:  A New Type of Steiner Points for Computing Size-Optimal */
/*    Quality-Guaranteed Delaunay Triangulations," Proceedings of LATIN      */
/*    2004 (Buenos Aires, Argentina), April 2004.                            */
/*                                                                           */
/*  For definitions of and results involving Delaunay triangulations,        */
/*    constrained and conforming versions thereof, and other aspects of      */
/*    triangular mesh generation, see the excellent survey by Marshall Bern  */
/*    and David Eppstein, "Mesh Generation and Optimal Triangulation," in    */
/*    Computing and Euclidean Geometry, Ding-Zhu Du and Frank Hwang,         */
/*    editors, World Scientific, Singapore, pp. 23-90, 1992.  [*]            */
/*                                                                           */
/*  The time for incrementally adding PSLG (planar straight line graph)      */
/*    segments to create a constrained Delaunay triangulation is probably    */
/*    O(t^2) per segment in the worst case and O(t) per segment in the       */
/*    common case, where t is the number of triangles that intersect the     */
/*    segment before it is inserted.  This doesn't count point location,     */
/*    which can be much more expensive.  I could improve this to O(d log d)  */
/*    time, but d is usually quite small, so it's not worth the bother.      */
/*    (This note does not apply when the -s switch is used, invoking a       */
/*    different method is used to insert segments.)                          */
/*                                                                           */
/*  The time for deleting a vertex from a Delaunay triangulation is O(d^2)   */
/*    in the worst case and O(d) in the common case, where d is the degree   */
/*    of the vertex being deleted.  I could improve this to O(d log d) time, */
/*    but d is usually quite small, so it's not worth the bother.            */
/*                                                                           */
/*  Ruppert's Delaunay refinement algorithm typically generates triangles    */
/*    at a linear rate (constant time per triangle) after the initial        */
/*    triangulation is formed.  There may be pathological cases where        */
/*    quadratic time is required, but these never arise in practice.         */
/*                                                                           */
/*  The geometric predicates (circumcenter calculations, segment             */
/*    intersection formulae, etc.) appear in my "Lecture Notes on Geometric  */
/*    Robustness" at http://www.cs.berkeley.edu/~jrs/mesh .                  */
/*                                                                           */
/*  If you make any improvements to this code, please please please let me   */
/*    know, so that I may obtain the improvements.  Even if you don't change */
/*    the code, I'd still love to hear what it's being used for.             */
/*                                                                           */
/*****************************************************************************/

Michael Turner's avatar
Michael Turner committed
233
#include <NekMeshUtils/Triangle/Triangle.h>
Michael Turner's avatar
files    
Michael Turner committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

/*****************************************************************************/
/*                                                                           */
/*  Mesh manipulation primitives.  Each triangle contains three pointers to  */
/*  other triangles, with orientations.  Each pointer points not to the      */
/*  first byte of a triangle, but to one of the first three bytes of a       */
/*  triangle.  It is necessary to extract both the triangle itself and the   */
/*  orientation.  To save memory, I keep both pieces of information in one   */
/*  pointer.  To make this possible, I assume that all triangles are aligned */
/*  to four-byte boundaries.  The decode() routine below decodes a pointer,  */
/*  extracting an orientation (in the range 0 to 2) and a pointer to the     */
/*  beginning of a triangle.  The encode() routine compresses a pointer to a */
/*  triangle and an orientation into a single pointer.  My assumptions that  */
/*  triangles are four-byte-aligned and that the `unsigned long' type is     */
/*  long enough to hold a pointer are two of the few kludges in this program.*/
/*                                                                           */
/*  Subsegments are manipulated similarly.  A pointer to a subsegment        */
/*  carries both an address and an orientation in the range 0 to 1.          */
/*                                                                           */
/*  The other primitives take an oriented triangle or oriented subsegment,   */
/*  and return an oriented triangle or oriented subsegment or vertex; or     */
/*  they change the connections in the data structure.                       */
/*                                                                           */
/*  Below, triangles and subsegments are denoted by their vertices.  The     */
/*  triangle abc has origin (org) a, destination (dest) b, and apex (apex)   */
/*  c.  These vertices occur in counterclockwise order about the triangle.   */
/*  The handle abc may simultaneously denote vertex a, edge ab, and triangle */
/*  abc.                                                                     */
/*                                                                           */
/*  Similarly, the subsegment ab has origin (sorg) a and destination (sdest) */
/*  b.  If ab is thought to be directed upward (with b directly above a),    */
/*  then the handle ab is thought to grasp the right side of ab, and may     */
/*  simultaneously denote vertex a and edge ab.                              */
/*                                                                           */
/*  An asterisk (*) denotes a vertex whose identity is unknown.              */
/*                                                                           */
/*  Given this notation, a partial list of mesh manipulation primitives      */
/*  follows.                                                                 */
/*                                                                           */
/*                                                                           */
/*  For triangles:                                                           */
/*                                                                           */
/*  sym:  Find the abutting triangle; same edge.                             */
/*  sym(abc) -> ba*                                                          */
/*                                                                           */
/*  lnext:  Find the next edge (counterclockwise) of a triangle.             */
/*  lnext(abc) -> bca                                                        */
/*                                                                           */
/*  lprev:  Find the previous edge (clockwise) of a triangle.                */
/*  lprev(abc) -> cab                                                        */
/*                                                                           */
/*  onext:  Find the next edge counterclockwise with the same origin.        */
/*  onext(abc) -> ac*                                                        */
/*                                                                           */
/*  oprev:  Find the next edge clockwise with the same origin.               */
/*  oprev(abc) -> a*b                                                        */
/*                                                                           */
/*  dnext:  Find the next edge counterclockwise with the same destination.   */
/*  dnext(abc) -> *ba                                                        */
/*                                                                           */
/*  dprev:  Find the next edge clockwise with the same destination.          */
/*  dprev(abc) -> cb*                                                        */
/*                                                                           */
/*  rnext:  Find the next edge (counterclockwise) of the adjacent triangle.  */
/*  rnext(abc) -> *a*                                                        */
/*                                                                           */
/*  rprev:  Find the previous edge (clockwise) of the adjacent triangle.     */
/*  rprev(abc) -> b**                                                        */
/*                                                                           */
/*  org:  Origin          dest:  Destination          apex:  Apex            */
/*  org(abc) -> a         dest(abc) -> b              apex(abc) -> c         */
/*                                                                           */
/*  bond:  Bond two triangles together at the resepective handles.           */
/*  bond(abc, bad)                                                           */
/*                                                                           */
/*                                                                           */
/*  For subsegments:                                                         */
/*                                                                           */
/*  ssym:  Reverse the orientation of a subsegment.                          */
/*  ssym(ab) -> ba                                                           */
/*                                                                           */
/*  spivot:  Find adjoining subsegment with the same origin.                 */
/*  spivot(ab) -> a*                                                         */
/*                                                                           */
/*  snext:  Find next subsegment in sequence.                                */
/*  snext(ab) -> b*                                                          */
/*                                                                           */
/*  sorg:  Origin                      sdest:  Destination                   */
/*  sorg(ab) -> a                      sdest(ab) -> b                        */
/*                                                                           */
/*  sbond:  Bond two subsegments together at the respective origins.         */
/*  sbond(ab, ac)                                                            */
/*                                                                           */
/*                                                                           */
/*  For interacting tetrahedra and subfacets:                                */
/*                                                                           */
/*  tspivot:  Find a subsegment abutting a triangle.                         */
/*  tspivot(abc) -> ba                                                       */
/*                                                                           */
/*  stpivot:  Find a triangle abutting a subsegment.                         */
/*  stpivot(ab) -> ba*                                                       */
/*                                                                           */
/*  tsbond:  Bond a triangle to a subsegment.                                */
/*  tsbond(abc, ba)                                                          */
/*                                                                           */
/*****************************************************************************/

Michael Turner's avatar
Michael Turner committed
341
342
343
344
345
namespace Nektar
{
namespace NekMeshUtils
{

Michael Turner's avatar
files    
Michael Turner committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/********* User-defined triangle evaluation routine begins here      *********/
/**                                                                         **/
/**                                                                         **/

/*****************************************************************************/
/*                                                                           */
/*  triunsuitable()   Determine if a triangle is unsuitable, and thus must   */
/*                    be further refined.                                    */
/*                                                                           */
/*  You may write your own procedure that decides whether or not a selected  */
/*  triangle is too big (and needs to be refined).  There are two ways to do */
/*  this.                                                                    */
/*                                                                           */
/*  (1)  Modify the procedure `triunsuitable' below, then recompile          */
/*  Triangle.                                                                */
/*                                                                           */
/*  (2)  Define the symbol EXTERNAL_TEST (either by adding the definition    */
/*  to this file, or by using the appropriate compiler switch).  This way,   */
/*  you can compile triangle.c separately from your test.  Write your own    */
/*  `triunsuitable' procedure in a separate C file (using the same prototype */
/*  as below).  Compile it and link the object code with triangle.o.         */
/*                                                                           */
/*  This procedure returns 1 if the triangle is too large and should be      */
/*  refined; 0 otherwise.                                                    */
/*                                                                           */
/*****************************************************************************/

#ifdef EXTERNAL_TEST

int triunsuitable();

#else /* not EXTERNAL_TEST */

379
int DelaunayTriangle::triunsuitable(vertex triorg, vertex tridest, vertex triapex, double area)
Michael Turner's avatar
files    
Michael Turner committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
{
    double dxoa, dxda, dxod;
    double dyoa, dyda, dyod;
    double oalen, dalen, odlen;
    double maxlen;

    dxoa = triorg[0] - triapex[0];
    dyoa = triorg[1] - triapex[1];
    dxda = tridest[0] - triapex[0];
    dyda = tridest[1] - triapex[1];
    dxod = triorg[0] - tridest[0];
    dyod = triorg[1] - tridest[1];
    /* Find the squares of the lengths of the triangle's three edges. */
    oalen = dxoa * dxoa + dyoa * dyoa;
    dalen = dxda * dxda + dyda * dyda;
    odlen = dxod * dxod + dyod * dyod;
    /* Find the square of the length of the longest edge. */
    maxlen = (dalen > oalen) ? dalen : oalen;
    maxlen = (odlen > maxlen) ? odlen : maxlen;

    if (maxlen > 0.05 * (triorg[0] * triorg[0] + triorg[1] * triorg[1]) + 0.02)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

#endif /* not EXTERNAL_TEST */

/**                                                                         **/
/**                                                                         **/
/********* User-defined triangle evaluation routine ends here        *********/

/********* Memory allocation and program exit wrappers begin here    *********/
/**                                                                         **/
/**                                                                         **/

420
void DelaunayTriangle::triexit(int status)
Michael Turner's avatar
files    
Michael Turner committed
421
422
423
424
{
    exit(status);
}

425
void *DelaunayTriangle::trimalloc(int size)
Michael Turner's avatar
files    
Michael Turner committed
426
427
428
429
430
431
432
433
434
435
436
437
{
    void *memptr;

    memptr = (void *)malloc((unsigned int)size);
    if (memptr == (void *)NULL)
    {
        printf("Error:  Out of memory.\n");
        triexit(1);
    }
    return (memptr);
}

438
void DelaunayTriangle::trifree(void *memptr)
Michael Turner's avatar
files    
Michael Turner committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
{
    free(memptr);
}

/**                                                                         **/
/**                                                                         **/
/********* Memory allocation and program exit wrappers end here      *********/

/********* User interaction routines begin here                      *********/
/**                                                                         **/
/**                                                                         **/

/*****************************************************************************/
/*                                                                           */
/*  internalerror()   Ask the user to send me the defective product.  Exit.  */
/*                                                                           */
/*****************************************************************************/

457
void DelaunayTriangle::internalerror()
Michael Turner's avatar
files    
Michael Turner committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
{
    printf("  Please report this bug to jrs@cs.berkeley.edu\n");
    printf("  Include the message above, your input data set, and the exact\n");
    printf("    command line you used to run Triangle.\n");
    triexit(1);
}

/*****************************************************************************/
/*                                                                           */
/*  parsecommandline()   Read the command line, identify switches, and set   */
/*                       up options and file names.                          */
/*                                                                           */
/*****************************************************************************/

472
void DelaunayTriangle::parsecommandline(int argc, char **argv, struct behavior *b)
Michael Turner's avatar
files    
Michael Turner committed
473
474
475
476
{
    int i, j, k;
    char workstring[2048];

Michael Turner's avatar
Michael Turner committed
477
478
    b->poly = b->quality = 0;
    b->usertest = 0;
479
    b->weighted = b->jettison = 0;
Michael Turner's avatar
files    
Michael Turner committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    b->nobisect                   = 0;
    b->minangle                   = 0.0;

    for (i = 0; i < argc; i++)
    {
        for (j = 0; argv[i][j] != '\0'; j++)
        {
            if (argv[i][j] == 'p')
            {
                b->poly = 1;
            }
            if (argv[i][j] == 'q')
            {
                b->quality = 1;
                if (((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
                    (argv[i][j + 1] == '.'))
                {
                    k = 0;
                    while (
                        ((argv[i][j + 1] >= '0') && (argv[i][j + 1] <= '9')) ||
                        (argv[i][j + 1] == '.'))
                    {
                        j++;
                        workstring[k] = argv[i][j];
                        k++;
                    }
                    workstring[k] = '\0';
                    b->minangle   = (double)strtod(workstring, (char **)NULL);
                }
                else
                {
                    b->minangle = 20.0;
                }
            }
            if (argv[i][j] == 'u')
            {
                b->quality  = 1;
                b->usertest = 1;
            }
            if (argv[i][j] == 'w')
            {
                b->weighted = 1;
            }
            if (argv[i][j] == 'W')
            {
                b->weighted = 2;
            }
            if (argv[i][j] == 'j')
            {
                b->jettison = 1;
            }
            if (argv[i][j] == 'Y')
            {
                b->nobisect++;
            }
        }
    }

538
    b->usesegments = b->poly || b->quality;
Michael Turner's avatar
files    
Michael Turner committed
539
540
541
542
543
544
545
546
547
548
549
    b->goodangle   = cos(b->minangle * PI / 180.0);
    if (b->goodangle == 1.0)
    {
        b->offconstant = 0.0;
    }
    else
    {
        b->offconstant =
            0.475 * sqrt((1.0 + b->goodangle) / (1.0 - b->goodangle));
    }
    b->goodangle *= b->goodangle;
Michael Turner's avatar
Michael Turner committed
550

Michael Turner's avatar
files    
Michael Turner committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
    /* Regular/weighted triangulations are incompatible with PSLGs */
    /*   and meshing.                                              */
    if (b->weighted && (b->poly || b->quality))
    {
        b->weighted = 0;
    }
}

/********* Memory management routines begin here                     *********/
/**                                                                         **/
/**                                                                         **/

/*****************************************************************************/
/*                                                                           */
/*  poolzero()   Set all of a pool's fields to zero.                         */
/*                                                                           */
/*  This procedure should never be called on a pool that has any memory      */
/*  allocated to it, as that memory would leak.                              */
/*                                                                           */
/*****************************************************************************/

572
void DelaunayTriangle::poolzero(struct memorypool *pool)
Michael Turner's avatar
files    
Michael Turner committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
{
    pool->firstblock       = (void **)NULL;
    pool->nowblock         = (void **)NULL;
    pool->nextitem         = (void *)NULL;
    pool->deaditemstack    = (void *)NULL;
    pool->pathblock        = (void **)NULL;
    pool->pathitem         = (void *)NULL;
    pool->alignbytes       = 0;
    pool->itembytes        = 0;
    pool->itemsperblock    = 0;
    pool->itemsfirstblock  = 0;
    pool->items            = 0;
    pool->maxitems         = 0;
    pool->unallocateditems = 0;
    pool->pathitemsleft    = 0;
}

/*****************************************************************************/
/*                                                                           */
/*  poolrestart()   Deallocate all items in a pool.                          */
/*                                                                           */
/*  The pool is returned to its starting state, except that no memory is     */
/*  freed to the operating system.  Rather, the previously allocated blocks  */
/*  are ready to be reused.                                                  */
/*                                                                           */
/*****************************************************************************/

600
void DelaunayTriangle::poolrestart(struct memorypool *pool)
Michael Turner's avatar
files    
Michael Turner committed
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
{
    unsigned long alignptr;

    pool->items    = 0;
    pool->maxitems = 0;

    /* Set the currently active block. */
    pool->nowblock = pool->firstblock;
    /* Find the first item in the pool.  Increment by the size of (void *). */
    alignptr = (unsigned long)(pool->nowblock + 1);
    /* Align the item on an `alignbytes'-byte boundary. */
    pool->nextitem = (void *)(alignptr + (unsigned long)pool->alignbytes -
                              (alignptr % (unsigned long)pool->alignbytes));
    /* There are lots of unallocated items left in this block. */
    pool->unallocateditems = pool->itemsfirstblock;
    /* The stack of deallocated items is empty. */
    pool->deaditemstack = (void *)NULL;
}

/*****************************************************************************/
/*                                                                           */
/*  poolinit()   Initialize a pool of memory for allocation of items.        */
/*                                                                           */
/*  This routine initializes the machinery for allocating items.  A `pool'   */
/*  is created whose records have size at least `bytecount'.  Items will be  */
/*  allocated in `itemcount'-item blocks.  Each item is assumed to be a      */
/*  collection of words, and either pointers or floating-point values are    */
/*  assumed to be the "primary" word type.  (The "primary" word type is used */
/*  to determine alignment of items.)  If `alignment' isn't zero, all items  */
/*  will be `alignment'-byte aligned in memory.  `alignment' must be either  */
/*  a multiple or a factor of the primary word size; powers of two are safe. */
/*  `alignment' is normally used to create a few unused bits at the bottom   */
/*  of each item's pointer, in which information may be stored.              */
/*                                                                           */
/*  Don't change this routine unless you understand it.                      */
/*                                                                           */
/*****************************************************************************/

639
void DelaunayTriangle::poolinit(struct memorypool *pool,
Michael Turner's avatar
files    
Michael Turner committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
              int bytecount,
              int itemcount,
              int firstitemcount,
              int alignment)
{
    /* Find the proper alignment, which must be at least as large as:   */
    /*   - The parameter `alignment'.                                   */
    /*   - sizeof(void *), so the stack of dead items can be maintained */
    /*       without unaligned accesses.                                */
    if (alignment > sizeof(void *))
    {
        pool->alignbytes = alignment;
    }
    else
    {
        pool->alignbytes = sizeof(void *);
    }
    pool->itembytes =
        ((bytecount - 1) / pool->alignbytes + 1) * pool->alignbytes;
    pool->itemsperblock = itemcount;
    if (firstitemcount == 0)
    {
        pool->itemsfirstblock = itemcount;
    }
    else
    {
        pool->itemsfirstblock = firstitemcount;
    }

    /* Allocate a block of items.  Space for `itemsfirstblock' items and one  */
    /*   pointer (to point to the next block) are allocated, as well as space */
    /*   to ensure alignment of the items.                                    */
    pool->firstblock =
        (void **)trimalloc(pool->itemsfirstblock * pool->itembytes +
                           (int)sizeof(void *) + pool->alignbytes);
    /* Set the next block pointer to NULL. */
    *(pool->firstblock) = (void *)NULL;
    poolrestart(pool);
}

/*****************************************************************************/
/*                                                                           */
/*  pooldeinit()   Free to the operating system all memory taken by a pool.  */
/*                                                                           */
/*****************************************************************************/

686
void DelaunayTriangle::pooldeinit(struct memorypool *pool)
Michael Turner's avatar
files    
Michael Turner committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
{
    while (pool->firstblock != (void **)NULL)
    {
        pool->nowblock = (void **)*(pool->firstblock);
        trifree((void *)pool->firstblock);
        pool->firstblock = pool->nowblock;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  poolalloc()   Allocate space for an item.                                */
/*                                                                           */
/*****************************************************************************/

702
void *DelaunayTriangle::poolalloc(struct memorypool *pool)
Michael Turner's avatar
files    
Michael Turner committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
{
    void *newitem;
    void **newblock;
    unsigned long alignptr;

    /* First check the linked list of dead items.  If the list is not   */
    /*   empty, allocate an item from the list rather than a fresh one. */
    if (pool->deaditemstack != (void *)NULL)
    {
        newitem = pool->deaditemstack; /* Take first item in list. */
        pool->deaditemstack = *(void **)pool->deaditemstack;
    }
    else
    {
        /* Check if there are any free items left in the current block. */
        if (pool->unallocateditems == 0)
        {
            /* Check if another block must be allocated. */
            if (*(pool->nowblock) == (void *)NULL)
            {
                /* Allocate a new block of items, pointed to by the previous
                 * block. */
                newblock =
                    (void **)trimalloc(pool->itemsperblock * pool->itembytes +
                                       (int)sizeof(void *) + pool->alignbytes);
                *(pool->nowblock) = (void *)newblock;
                /* The next block pointer is NULL. */
                *newblock = (void *)NULL;
            }

            /* Move to the new block. */
            pool->nowblock = (void **)*(pool->nowblock);
            /* Find the first item in the block.    */
            /*   Increment by the size of (void *). */
            alignptr = (unsigned long)(pool->nowblock + 1);
            /* Align the item on an `alignbytes'-byte boundary. */
            pool->nextitem =
                (void *)(alignptr + (unsigned long)pool->alignbytes -
                         (alignptr % (unsigned long)pool->alignbytes));
            /* There are lots of unallocated items left in this block. */
            pool->unallocateditems = pool->itemsperblock;
        }

        /* Allocate a new item. */
        newitem = pool->nextitem;
        /* Advance `nextitem' pointer to next free item in block. */
        pool->nextitem = (void *)((char *)pool->nextitem + pool->itembytes);
        pool->unallocateditems--;
        pool->maxitems++;
    }
    pool->items++;
    return newitem;
}

/*****************************************************************************/
/*                                                                           */
/*  pooldealloc()   Deallocate space for an item.                            */
/*                                                                           */
/*  The deallocated space is stored in a queue for later reuse.              */
/*                                                                           */
/*****************************************************************************/

765
void DelaunayTriangle::pooldealloc(struct memorypool *pool, void *dyingitem)
Michael Turner's avatar
files    
Michael Turner committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
{
    /* Push freshly killed item onto stack. */
    *((void **)dyingitem) = pool->deaditemstack;
    pool->deaditemstack   = dyingitem;
    pool->items--;
}

/*****************************************************************************/
/*                                                                           */
/*  traversalinit()   Prepare to traverse the entire list of items.          */
/*                                                                           */
/*  This routine is used in conjunction with traverse().                     */
/*                                                                           */
/*****************************************************************************/

781
void DelaunayTriangle::traversalinit(struct memorypool *pool)
Michael Turner's avatar
files    
Michael Turner committed
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
{
    unsigned long alignptr;

    /* Begin the traversal in the first block. */
    pool->pathblock = pool->firstblock;
    /* Find the first item in the block.  Increment by the size of (void *). */
    alignptr = (unsigned long)(pool->pathblock + 1);
    /* Align with item on an `alignbytes'-byte boundary. */
    pool->pathitem = (void *)(alignptr + (unsigned long)pool->alignbytes -
                              (alignptr % (unsigned long)pool->alignbytes));
    /* Set the number of items left in the current block. */
    pool->pathitemsleft = pool->itemsfirstblock;
}

/*****************************************************************************/
/*                                                                           */
/*  traverse()   Find the next item in the list.                             */
/*                                                                           */
/*  This routine is used in conjunction with traversalinit().  Be forewarned */
/*  that this routine successively returns all items in the list, including  */
/*  deallocated ones on the deaditemqueue.  It's up to you to figure out     */
/*  which ones are actually dead.  Why?  I don't want to allocate extra      */
/*  space just to demarcate dead items.  It can usually be done more         */
/*  space-efficiently by a routine that knows something about the structure  */
/*  of the item.                                                             */
/*                                                                           */
/*****************************************************************************/

810
void *DelaunayTriangle::traverse(struct memorypool *pool)
Michael Turner's avatar
files    
Michael Turner committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
{
    void *newitem;
    unsigned long alignptr;

    /* Stop upon exhausting the list of items. */
    if (pool->pathitem == pool->nextitem)
    {
        return (void *)NULL;
    }

    /* Check whether any untraversed items remain in the current block. */
    if (pool->pathitemsleft == 0)
    {
        /* Find the next block. */
        pool->pathblock = (void **)*(pool->pathblock);
        /* Find the first item in the block.  Increment by the size of (void *).
         */
        alignptr = (unsigned long)(pool->pathblock + 1);
        /* Align with item on an `alignbytes'-byte boundary. */
        pool->pathitem = (void *)(alignptr + (unsigned long)pool->alignbytes -
                                  (alignptr % (unsigned long)pool->alignbytes));
        /* Set the number of items left in the current block. */
        pool->pathitemsleft = pool->itemsperblock;
    }

    newitem = pool->pathitem;
    /* Find the next item in the block. */
    pool->pathitem = (void *)((char *)pool->pathitem + pool->itembytes);
    pool->pathitemsleft--;
    return newitem;
}

/*****************************************************************************/
/*                                                                           */
/*  dummyinit()   Initialize the triangle that fills "outer space" and the   */
/*                omnipresent subsegment.                                    */
/*                                                                           */
/*  The triangle that fills "outer space," called `dummytri', is pointed to  */
/*  by every triangle and subsegment on a boundary (be it outer or inner) of */
/*  the triangulation.  Also, `dummytri' points to one of the triangles on   */
/*  the convex hull (until the holes and concavities are carved), making it  */
/*  possible to find a starting triangle for point location.                 */
/*                                                                           */
/*  The omnipresent subsegment, `dummysub', is pointed to by every triangle  */
/*  or subsegment that doesn't have a full complement of double subsegments */
/*  to point to.                                                             */
/*                                                                           */
/*  `dummytri' and `dummysub' are generally required to fulfill only a few   */
/*  invariants:  their vertices must remain NULL and `dummytri' must always  */
/*  be bonded (at offset zero) to some triangle on the convex hull of the    */
/*  mesh, via a boundary edge.  Otherwise, the connections of `dummytri' and */
/*  `dummysub' may change willy-nilly.  This makes it possible to avoid      */
/*  writing a good deal of special-case code (in the edge flip, for example) */
/*  for dealing with the boundary of the mesh, places where no subsegment is */
/*  present, and so forth.  Other entities are frequently bonded to          */
/*  `dummytri' and `dummysub' as if they were double mesh entities, with no */
/*  harm done.                                                               */
/*                                                                           */
/*****************************************************************************/

871
void DelaunayTriangle::dummyinit(struct mesh *m,
Michael Turner's avatar
files    
Michael Turner committed
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
               struct behavior *b,
               int trianglebytes,
               int subsegbytes)
{
    unsigned long alignptr;

    /* Set up `dummytri', the `triangle' that occupies "outer space." */
    m->dummytribase =
        (triangle *)trimalloc(trianglebytes + m->triangles.alignbytes);
    /* Align `dummytri' on a `triangles.alignbytes'-byte boundary. */
    alignptr = (unsigned long)m->dummytribase;
    m->dummytri =
        (triangle *)(alignptr + (unsigned long)m->triangles.alignbytes -
                     (alignptr % (unsigned long)m->triangles.alignbytes));
    /* Initialize the three adjoining triangles to be "outer space."  These  */
    /*   will eventually be changed by various bonding operations, but their */
    /*   values don't really matter, as long as they can legally be          */
    /*   dereferenced.                                                       */
    m->dummytri[0] = (triangle)m->dummytri;
    m->dummytri[1] = (triangle)m->dummytri;
    m->dummytri[2] = (triangle)m->dummytri;
    /* Three NULL vertices. */
    m->dummytri[3] = (triangle)NULL;
    m->dummytri[4] = (triangle)NULL;
    m->dummytri[5] = (triangle)NULL;

    if (b->usesegments)
    {
        /* Set up `dummysub', the omnipresent subsegment pointed to by any */
        /*   triangle side or subsegment end that isn't attached to a double */
        /*   subsegment.                                                   */
        m->dummysubbase =
            (subseg *)trimalloc(subsegbytes + m->subsegs.alignbytes);
        /* Align `dummysub' on a `subsegs.alignbytes'-byte boundary. */
        alignptr = (unsigned long)m->dummysubbase;
        m->dummysub =
            (subseg *)(alignptr + (unsigned long)m->subsegs.alignbytes -
                       (alignptr % (unsigned long)m->subsegs.alignbytes));
        /* Initialize the two adjoining subsegments to be the omnipresent */
        /*   subsegment.  These will eventually be changed by various bonding */
        /*   operations, but their values don't really matter, as long as they
         */
        /*   can legally be dereferenced. */
        m->dummysub[0] = (subseg)m->dummysub;
        m->dummysub[1] = (subseg)m->dummysub;
        /* Four NULL vertices. */
        m->dummysub[2] = (subseg)NULL;
        m->dummysub[3] = (subseg)NULL;
        m->dummysub[4] = (subseg)NULL;
        m->dummysub[5] = (subseg)NULL;
        /* Initialize the two adjoining triangles to be "outer space." */
        m->dummysub[6] = (subseg)m->dummytri;
        m->dummysub[7] = (subseg)m->dummytri;
        /* Set the boundary marker to zero. */
        *(int *)(m->dummysub + 8) = 0;

        /* Initialize the three adjoining subsegments of `dummytri' to be */
        /*   the omnipresent subsegment.                                  */
        m->dummytri[6] = (triangle)m->dummysub;
        m->dummytri[7] = (triangle)m->dummysub;
        m->dummytri[8] = (triangle)m->dummysub;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  initializevertexpool()   Calculate the size of the vertex data structure */
/*                           and initialize its memory pool.                 */
/*                                                                           */
/*  This routine also computes the `vertexmarkindex' and `vertex2triindex'   */
/*  indices used to find values within each vertex.                          */
/*                                                                           */
/*****************************************************************************/

946
void DelaunayTriangle::initializevertexpool(struct mesh *m, struct behavior *b)
Michael Turner's avatar
files    
Michael Turner committed
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
{
    int vertexsize;

    /* The index within each vertex at which the boundary marker is found,    */
    /*   followed by the vertex type.  Ensure the vertex marker is aligned to */
    /*   a sizeof(int)-byte address.                                          */
    m->vertexmarkindex =
        ((m->mesh_dim + m->nextras) * sizeof(double) + sizeof(int) - 1) /
        sizeof(int);
    vertexsize = (m->vertexmarkindex + 2) * sizeof(int);
    if (b->poly)
    {
        /* The index within each vertex at which a triangle pointer is found. */
        /*   Ensure the pointer is aligned to a sizeof(triangle)-byte address.
         */
        m->vertex2triindex =
            (vertexsize + sizeof(triangle) - 1) / sizeof(triangle);
        vertexsize = (m->vertex2triindex + 1) * sizeof(triangle);
    }

    /* Initialize the pool of vertices. */
    poolinit(&m->vertices,
             vertexsize,
             VERTEXPERBLOCK,
             m->invertices > VERTEXPERBLOCK ? m->invertices : VERTEXPERBLOCK,
             sizeof(double));
}

/*****************************************************************************/
/*                                                                           */
/*  initializetrisubpools()   Calculate the sizes of the triangle and        */
/*                            subsegment data structures and initialize      */
/*                            their memory pools.                            */
/*                                                                           */
/*  This routine also computes the `highorderindex', `elemattribindex', and  */
/*  `areaboundindex' indices used to find values within each triangle.       */
/*                                                                           */
/*****************************************************************************/

986
void DelaunayTriangle::initializetrisubpools(struct mesh *m, struct behavior *b)
Michael Turner's avatar
files    
Michael Turner committed
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
{
    int trisize;

    /* The index within each triangle at which the extra nodes (above three)  */
    /*   associated with high order elements are found.  There are three      */
    /*   pointers to other triangles, three pointers to corners, and possibly */
    /*   three pointers to subsegments before the extra nodes.                */
    m->highorderindex = 6 + (b->usesegments * 3);
    /* The number of bytes occupied by a triangle. */
    trisize = (3 + (m->highorderindex - 3)) *
              sizeof(triangle);
    /* The index within each triangle at which its attributes are found, */
    /*   where the index is measured in doubles.                           */
    m->elemattribindex = (trisize + sizeof(double) - 1) / sizeof(double);
    /* The index within each triangle at which the maximum area constraint  */
    /*   is found, where the index is measured in doubles.  Note that if the  */
    /*   `regionattrib' flag is set, an additional attribute will be added. */
1004
    m->areaboundindex = m->elemattribindex + m->eextras;
Michael Turner's avatar
files    
Michael Turner committed
1005
1006
1007
    /* If triangle attributes or an area bound are needed, increase the number
     */
    /*   of bytes occupied by a triangle. */
Michael Turner's avatar
Michael Turner committed
1008
    trisize = m->areaboundindex * sizeof(double);
Michael Turner's avatar
files    
Michael Turner committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043

    /* Having determined the memory size of a triangle, initialize the pool. */
    poolinit(&m->triangles,
             trisize,
             TRIPERBLOCK,
             (2 * m->invertices - 2) > TRIPERBLOCK ? (2 * m->invertices - 2)
                                                   : TRIPERBLOCK,
             4);

    if (b->usesegments)
    {
        /* Initialize the pool of subsegments.  Take into account all eight */
        /*   pointers and one boundary marker.                              */
        poolinit(&m->subsegs,
                 8 * sizeof(triangle) + sizeof(int),
                 SUBSEGPERBLOCK,
                 SUBSEGPERBLOCK,
                 4);

        /* Initialize the "outer space" triangle and omnipresent subsegment. */
        dummyinit(m, b, m->triangles.itembytes, m->subsegs.itembytes);
    }
    else
    {
        /* Initialize the "outer space" triangle. */
        dummyinit(m, b, m->triangles.itembytes, 0);
    }
}

/*****************************************************************************/
/*                                                                           */
/*  triangledealloc()   Deallocate space for a triangle, marking it dead.    */
/*                                                                           */
/*****************************************************************************/

1044
void DelaunayTriangle::triangledealloc(struct mesh *m, triangle *dyingtriangle)
Michael Turner's avatar
files    
Michael Turner committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
{
    /* Mark the triangle as dead.  This makes it possible to detect dead */
    /*   triangles when traversing the list of all triangles.            */
    killtri(dyingtriangle);
    pooldealloc(&m->triangles, (void *)dyingtriangle);
}

/*****************************************************************************/
/*                                                                           */
/*  triangletraverse()   Traverse the triangles, skipping dead ones.         */
/*                                                                           */
/*****************************************************************************/

1058
triangle *DelaunayTriangle::triangletraverse(struct mesh *m)
Michael Turner's avatar
files    
Michael Turner committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
{
    triangle *newtriangle;

    do
    {
        newtriangle = (triangle *)traverse(&m->triangles);
        if (newtriangle == (triangle *)NULL)
        {
            return (triangle *)NULL;
        }
    } while (deadtri(newtriangle)); /* Skip dead ones. */
    return newtriangle;
}

/*****************************************************************************/
/*                                                                           */
/*  subsegdealloc()   Deallocate space for a subsegment, marking it dead.    */
/*                                                                           */
/*****************************************************************************/

1079
void DelaunayTriangle::subsegdealloc(struct mesh *m, subseg *dyingsubseg)
Michael Turner's avatar
files    
Michael Turner committed
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
{
    /* Mark the subsegment as dead.  This makes it possible to detect dead */
    /*   subsegments when traversing the list of all subsegments.          */
    killsubseg(dyingsubseg);
    pooldealloc(&m->subsegs, (void *)dyingsubseg);
}

/*****************************************************************************/
/*                                                                           */
/*  subsegtraverse()   Traverse the subsegments, skipping dead ones.         */
/*                                                                           */
/*****************************************************************************/

1093
subseg *DelaunayTriangle::subsegtraverse(struct mesh *m)
Michael Turner's avatar
files    
Michael Turner committed
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
{
    subseg *newsubseg;

    do
    {
        newsubseg = (subseg *)traverse(&m->subsegs);
        if (newsubseg == (subseg *)NULL)
        {
            return (subseg *)NULL;
        }
    } while (deadsubseg(newsubseg)); /* Skip dead ones. */
    return newsubseg;
}

/*****************************************************************************/
/*                                                                           */
/*  vertexdealloc()   Deallocate space for a vertex, marking it dead.        */
/*                                                                           */
/*****************************************************************************/

1114
void DelaunayTriangle::vertexdealloc(struct mesh *m, vertex dyingvertex)
Michael Turner's avatar
files    
Michael Turner committed
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
{
    /* Mark the vertex as dead.  This makes it possible to detect dead */
    /*   vertices when traversing the list of all vertices.            */
    setvertextype(dyingvertex, DEADVERTEX);
    pooldealloc(&m->vertices, (void *)dyingvertex);
}

/*****************************************************************************/
/*                                                                           */
/*  vertextraverse()   Traverse the vertices, skipping dead ones.            */
/*                                                                           */
/*****************************************************************************/

1128
vertex DelaunayTriangle::vertextraverse(struct mesh *m)
Michael Turner's avatar
files    
Michael Turner committed
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
{
    vertex newvertex;

    do
    {
        newvertex = (vertex)traverse(&m->vertices);
        if (newvertex == (vertex)NULL)
        {
            return (vertex)NULL;
        }
    } while (vertextype(newvertex) == DEADVERTEX); /* Skip dead ones. */
    return newvertex;
}

/*****************************************************************************/
/*                                                                           */
/*  badsubsegdealloc()   Deallocate space for a bad subsegment, marking it   */
/*                       dead.                                               */
/*                                                                           */
/*****************************************************************************/

1150
void DelaunayTriangle::badsubsegdealloc(struct mesh *m, struct badsubseg *dyingseg)
Michael Turner's avatar
files    
Michael Turner committed
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
{
    /* Set subsegment's origin to NULL.  This makes it possible to detect dead
     */
    /*   badsubsegs when traversing the list of all badsubsegs             . */
    dyingseg->subsegorg = (vertex)NULL;
    pooldealloc(&m->badsubsegs, (void *)dyingseg);
}

/*****************************************************************************/
/*                                                                           */
/*  badsubsegtraverse()   Traverse the bad subsegments, skipping dead ones.  */
/*                                                                           */
/*****************************************************************************/

1165
struct badsubseg *DelaunayTriangle::badsubsegtraverse(struct mesh *m)
Michael Turner's avatar
files    
Michael Turner committed
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
{
    struct badsubseg *newseg;

    do
    {
        newseg = (struct badsubseg *)traverse(&m->badsubsegs);
        if (newseg == (struct badsubseg *)NULL)
        {
            return (struct badsubseg *)NULL;
        }
    } while (newseg->subsegorg == (vertex)NULL); /* Skip dead ones. */
    return newseg;
}

/*****************************************************************************/
/*                                                                           */
/*  getvertex()   Get a specific vertex, by number, from the list.           */
/*                                                                           */
/*  The first vertex is number 'firstnumber'.                                */
/*                                                                           */
/*  Note that this takes O(n) time (with a small constant, if VERTEXPERBLOCK */
/*  is large).  I don't care to take the trouble to make it work in constant */
/*  time.                                                                    */
/*                                                                           */
/*****************************************************************************/

1192
vertex DelaunayTriangle::getvertex(struct mesh *m, struct behavior *b, int number)
Michael Turner's avatar
files    
Michael Turner committed
1193
1194
1195
1196
1197
1198
1199
{
    void **getblock;
    char *foundvertex;
    unsigned long alignptr;
    int current;

    getblock = m->vertices.firstblock;
1200
    current  = 0;
Michael Turner's avatar
files    
Michael Turner committed
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226

    /* Find the right block. */
    if (current + m->vertices.itemsfirstblock <= number)
    {
        getblock = (void **)*getblock;
        current += m->vertices.itemsfirstblock;
        while (current + m->vertices.itemsperblock <= number)
        {
            getblock = (void **)*getblock;
            current += m->vertices.itemsperblock;
        }
    }

    /* Now find the right vertex. */
    alignptr    = (unsigned long)(getblock + 1);
    foundvertex = (char *)(alignptr + (unsigned long)m->vertices.alignbytes -
                           (alignptr % (unsigned long)m->vertices.alignbytes));
    return (vertex)(foundvertex + m->vertices.itembytes * (number - current));
}

/*****************************************************************************/
/*                                                                           */
/*  triangledeinit()   Free all remaining allocated memory.                  */
/*                                                                           */
/*****************************************************************************/

1227
void DelaunayTriangle::triangledeinit(struct mesh *m, struct behavior *b)
Michael Turner's avatar
files    
Michael Turner committed
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
{
    pooldeinit(&m->triangles);
    trifree((void *)m->dummytribase);
    if (b->usesegments)
    {
        pooldeinit(&m->subsegs);
        trifree((void *)m->dummysubbase);
    }
    pooldeinit(&m->vertices);
    if (b->quality)
    {
        pooldeinit(&m->badsubsegs);
Michael Turner's avatar
Michael Turner committed
1240
        if ((b->minangle > 0.0) || b->usertest)
Michael Turner's avatar
files    
Michael Turner committed
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
        {
            pooldeinit(&m->badtriangles);
            pooldeinit(&m->flipstackers);
        }
    }
}

/**                                                                         **/
/**                                                                         **/
/********* Memory management routines end here                       *********/

/********* Constructors begin here                                   *********/
/**                                                                         **/
/**                                                                         **/

/*****************************************************************************/
/*                                                                           */
/*  maketriangle()   Create a new triangle with orientation zero.            */
/*                                                                           */
/*****************************************************************************/

1262
void DelaunayTriangle::maketriangle(struct mesh *m, struct behavior *b, struct otri *newotri)
Michael Turner's avatar
files    
Michael Turner committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
{
    int i;

    newotri->tri = (triangle *)poolalloc(&m->triangles);
    /* Initialize the three adjoining triangles to be "outer space". */
    newotri->tri[0] = (triangle)m->dummytri;
    newotri->tri[1] = (triangle)m->dummytri;
    newotri->tri[2] = (triangle)m->dummytri;
    /* Three NULL vertices. */
    newotri->tri[3] = (triangle)NULL;
    newotri->tri[4] = (triangle)NULL;
    newotri->tri[5] = (triangle)NULL;
    if (b->usesegments)
    {
        /* Initialize the three adjoining subsegments to be the omnipresent */
        /*   subsegment.                                                    */
        newotri->tri[6] = (triangle)m->dummysub;
        newotri->tri[7] = (triangle)m->dummysub;
        newotri->tri[8] = (triangle)m->dummysub;
    }
    for (i = 0; i < m->eextras; i++)
    {
        setelemattribute(*newotri, i, 0.0);
    }

    newotri->orient = 0;
}

/*****************************************************************************/
/*                                                                           */
/*  makesubseg()   Create a new subsegment with orientation zero.            */
/*                                                                           */
/*****************************************************************************/

1297
void DelaunayTriangle::makesubseg(struct mesh *m, struct osub *newsubseg)
Michael Turner's avatar
files    
Michael Turner committed
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
{
    newsubseg->ss = (subseg *)poolalloc(&m->subsegs);
    /* Initialize the two adjoining subsegments to be the omnipresent */
    /*   subsegment.                                                  */
    newsubseg->ss[0] = (subseg)m->dummysub;
    newsubseg->ss[1] = (subseg)m->dummysub;
    /* Four NULL vertices. */
    newsubseg->ss[2] = (subseg)NULL;
    newsubseg->ss[3] = (subseg)NULL;
    newsubseg->ss[4] = (subseg)NULL;
    newsubseg->ss[5] = (subseg)NULL;
    /* Initialize the two adjoining triangles to be "outer space." */
    newsubseg->ss[6] = (subseg)m->dummytri;
    newsubseg->ss[7] = (subseg)m->dummytri;
    /* Set the boundary marker to zero. */
    setmark(*newsubseg, 0);

    newsubseg->ssorient = 0;
}

/**                                                                         **/
/**                                                                         **/
/********* Constructors end here                                     *********/

/********* Geometric primitives begin here                           *********/
/**                                                                         **/
/**                                                                         **/

/* The adaptive exact arithmetic geometric predicates implemented herein are */
/*   described in detail in my paper, "Adaptive Precision Floating-Point     */
/*   Arithmetic and Fast Robust Geometric Predicates."  See the header for a */
/*   full citation.                                                          */

/* Which of the following two methods of finding the absolute values is      */
/*   fastest is compiler-dependent.  A few compilers can inline and optimize */
/*   the fabs() call; but most will incur the overhead of a function call,   */
/*   which is disastrously slow.  A faster way on IEEE machines might be to  */
/*   mask the appropriate bit, but that's difficult to do in C without       */
/*   forcing the value to be stored to memory (rather than be kept in the    */
/*   register to which the optimizer assigned it).                           */

#define Absolute(a) ((a) >= 0.0 ? (a) : -(a))
/* #define Absolute(a)  fabs(a) */

/* Many of the operations are broken up into two pieces, a main part that    */
/*   performs an approximate operation, and a "tail" that computes the       */
/*   roundoff error of that operation.                                       */
/*                                                                           */
/* The operations Fast_Two_Sum(), Fast_Two_Diff(), Two_Sum(), Two_Diff(),    */
/*   Split(), and Two_Product() are all implemented as described in the      */
/*   reference.  Each of these macros requires certain variables to be       */
/*   defined in the calling routine.  The variables `bvirt', `c', `abig',    */
/*   `_i', `_j', `_k', `_l', `_m', and `_n' are declared `' because   */
/*   they store the result of an operation that may incur roundoff error.    */
/*   The input parameter `x' (or the highest numbered `x_' parameter) must   */
/*   also be declared `'.                                             */

#define Fast_Two_Sum_Tail(a, b, x, y)                                          \
    bvirt = x - a;                                                             \
    y     = b - bvirt

#define Fast_Two_Sum(a, b, x, y)                                               \
    x = (double)(a + b);                                                       \
    Fast_Two_Sum_Tail(a, b, x, y)

#define Two_Sum_Tail(a, b, x, y)                                               \
    bvirt  = (double)(x - a);                                                  \
    avirt  = x - bvirt;                                                        \
    bround = b - bvirt;                                                        \
    around = a - avirt;                                                        \
    y      = around + bround

#define Two_Sum(a, b, x, y)                                                    \
    x = (double)(a + b);                                                       \
    Two_Sum_Tail(a, b, x, y)

#define Two_Diff_Tail(a, b, x, y)                                              \
    bvirt  = (double)(a - x);                                                  \
    avirt  = x + bvirt;                                                        \
    bround = bvirt - b;                                                        \
    around = a - avirt;                                                        \
    y      = around + bround

#define Two_Diff(a, b, x, y)                                                   \
    x = (double)(a - b);                                                       \
    Two_Diff_Tail(a, b, x, y)

#define Split(a, ahi, alo)                                                     \
    c    = (double)(splitter * a);                                             \
    abig = (double)(c - a);                                                    \
    ahi  = c - abig;                                                           \
    alo  = a - ahi

#define Two_Product_Tail(a, b, x, y)                                           \
    Split(a, ahi, alo);                                                        \
    Split(b, bhi, blo);                                                        \
    err1 = x - (ahi * bhi);                                                    \
    err2 = err1 - (alo * bhi);                                                 \
    err3 = err2 - (ahi * blo);                                                 \
    y    = (alo * blo) - err3

#define Two_Product(a, b, x, y)                                                \
    x = (double)(a * b);                                                       \
    Two_Product_Tail(a, b, x, y)

/* Two_Product_Presplit() is Two_Product() where one of the inputs has       */
/*   already been split.  Avoids redundant splitting.                        */

#define Two_Product_Presplit(a, b, bhi, blo, x, y)                             \
    x = (double)(a * b);                                                       \
    Split(a, ahi, alo);                                                        \
    err1 = x - (ahi * bhi);                                                    \
    err2 = err1 - (alo * bhi);                                                 \
    err3 = err2 - (ahi * blo);                                                 \
    y    = (alo * blo) - err3

/* Square() can be done more quickly than Two_Product().                     */

#define Square_Tail(a, x, y)                                                   \
    Split(a, ahi, alo);                                                        \
    err1 = x - (ahi * ahi);                                                    \
    err3 = err1 - ((ahi + ahi) * alo);                                         \
    y    = (alo * alo) - err3

#define Square(a, x, y)                                                        \
    x = (double)(a * a);                                                       \
    Square_Tail(a, x, y)

/* Macros for summing expansions of various fixed lengths.  These are all    */
/*   unrolled versions of Expansion_Sum().                                   */

#define Two_One_Sum(a1, a0, b, x2, x1, x0)                                     \
    Two_Sum(a0, b, _i, x0);                                                    \
    Two_Sum(a1, _i, x2, x1)

#define Two_One_Diff(a1, a0, b, x2, x1, x0)                                    \
    Two_Diff(a0, b, _i, x0);                                                   \
    Two_Sum(a1, _i, x2, x1)

#define Two_Two_Sum(a1, a0, b1, b0, x3, x2, x1, x0)                            \
    Two_One_Sum(a1, a0, b0, _j, _0, x0);                                       \
    Two_One_Sum(_j, _0, b1, x3, x2, x1)

#define Two_Two_Diff(a1, a0, b1, b0, x3, x2, x1, x0)                           \
    Two_One_Diff(a1, a0, b0, _j, _0, x0);                                      \
    Two_One_Diff(_j, _0, b1, x3, x2, x1)

/* Macro for multiplying a two-component expansion by a single component.    */

#define Two_One_Product(a1, a0, b, x3, x2, x1, x0)                             \
    Split(b, bhi, blo);                                                        \
    Two_Product_Presplit(a0, b, bhi, blo, _i, x0);                             \
    Two_Product_Presplit(a1, b, bhi, blo, _j, _0);                             \
    Two_Sum(_i, _0, _k, x1);                                                   \
    Fast_Two_Sum(_j, _k, x3, x2)

/*****************************************************************************/
/*                                                                           */
/*  exactinit()   Initialize the variables used for exact arithmetic.        */
/*                                                                           */
/*  `epsilon' is the largest power of two such that 1.0 + epsilon = 1.0 in   */
/*  floating-point arithmetic.  `epsilon' bounds the relative roundoff       */
/*  error.  It is used for floating-point error analysis.                    */
/*                                                                           */
/*  `splitter' is used to split floating-point numbers into two half-        */
/*  length significands for exact multiplication.                            */
/*                                                                           */
/*  I imagine that a highly optimizing compiler might be too smart for its   */
/*  own good, and somehow cause this routine to fail, if it pretends that    */
/*  floating-point arithmetic is too much like double arithmetic. */
/*                                                                           */
/*  Don't change this routine unless you fully understand it.                */
/*                                                                           */
/*****************************************************************************/

1473
void DelaunayTriangle::exactinit()
Michael Turner's avatar
files    
Michael Turner committed
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
{
    double half;
    double check, lastcheck;
    int every_other;

    every_other = 1;
    half        = 0.5;
    epsilon     = 1.0;
    splitter    = 1.0;
    check       = 1.0;
    /* Repeatedly divide `epsilon' by two until it is too small to add to */
    /*   one without causing roundoff.  (Also check if the sum is equal to */
    /*   the previous sum, for machines that round up instead of using exact */
    /*   rounding.  Not that these routines will work on such machines.) */
    do
    {
        lastcheck = check;
        epsilon *= half;
        if (every_other)
        {
            splitter *= 2.0;
        }
        every_other = !every_other;
        check       = 1.0 + epsilon;
    } while ((check != 1.0) && (check != lastcheck));
    splitter += 1.0;
    /* Error bounds for orientation and incircle tests. */
    resulterrbound = (3.0 + 8.0 * epsilon) * epsilon;
    ccwerrboundA   = (3.0 + 16.0 * epsilon) * epsilon;
    ccwerrboundB   = (2.0 + 12.0 * epsilon) * epsilon;
    ccwerrboundC   = (9.0 + 64.0 * epsilon) * epsilon * epsilon;
    iccerrboundA   = (10.0 + 96.0 * epsilon) * epsilon;
    iccerrboundB   = (4.0 + 48.0 * epsilon) * epsilon;
    iccerrboundC   = (44.0 + 576.0 * epsilon) * epsilon * epsilon;
    o3derrboundA   = (7.0 + 56.0 * epsilon) * epsilon;
    o3derrboundB   = (3.0 + 28.0 * epsilon) * epsilon;
    o3derrboundC   = (26.0 + 288.0 * epsilon) * epsilon * epsilon;
}

/*****************************************************************************/
/*                                                                           */
/*  fast_expansion_sum_zeroelimTRI()   Sum two expansions, eliminating zero     */
/*                                  components from the output expansion.    */
/*                                                                           */
/*  Sets h = e + f.  See my Robust Predicates paper for details.             */
/*                                                                           */
/*  If round-to-even is used (as with IEEE 754), maintains the strongly      */
/*  nonoverlapping property.  (That is, if e is strongly nonoverlapping, h   */
/*  will be also.)  Does NOT maintain the nonoverlapping or nonadjacent      */
/*  properties.                                                              */
/*                                                                           */
/*****************************************************************************/

1527
int DelaunayTriangle::fast_expansion_sum_zeroelimTRI(
Michael Turner's avatar
files    
Michael Turner committed
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
    int elen, double *e, int flen, double *f, double *h)
{
    double Q;
     double Qnew;
     double hh;
     double bvirt;
    double avirt, bround, around;
    int eindex, findex, hindex;
    double enow, fnow;

    enow   = e[0];
    fnow   = f[0];
    eindex = findex = 0;
    if ((fnow > enow) == (fnow > -enow))
    {
        Q    = enow;
        enow = e[++eindex];
    }
    else
    {
        Q    = fnow;
        fnow = f[++findex];
    }
    hindex = 0;
    if ((eindex < elen) && (findex < flen))
    {
        if ((fnow > enow) == (fnow > -enow))
        {
            Fast_Two_Sum(enow, Q, Qnew, hh);
            enow = e[++eindex];
        }
        else
        {
            Fast_Two_Sum(fnow, Q, Qnew, hh);
            fnow = f[++findex];
        }
        Q = Qnew;
        if (hh != 0.0)
        {
            h[hindex++] = hh;
        }
        while ((eindex < elen) && (findex < flen))
        {
            if ((fnow > enow) == (fnow > -enow))
            {
                Two_Sum(Q, enow, Qnew, hh);
                enow = e[++eindex];
            }
            else
            {
                Two_Sum(Q, fnow, Qnew, hh);
                fnow = f[++findex];
            }
            Q = Qnew;
            if (hh != 0.0)
            {
                h[hindex++] = hh;
            }
        }
    }
    while (eindex < elen)
    {
        Two_Sum(Q, enow, Qnew, hh);
        enow = e[++eindex];
        Q    = Qnew;
        if (hh != 0.0)
        {
            h[hindex++] = hh;
        }
    }
    while (findex < flen)
    {
        Two_Sum(Q, fnow, Qnew, hh);
        fnow = f[++findex];
        Q    = Qnew;
        if (hh != 0.0)
        {
            h[hindex++] = hh;
        }
    }
    if ((Q != 0.0) || (hindex == 0))
    {
        h[hindex++] = Q;
    }
    return hindex;
}

/*****************************************************************************/
/*                                                                           */
/*  scale_expansion_zeroelimTRI()   Multiply an expansion by a scalar,          */
/*                               eliminating zero components from the        */
/*                               output expansion.                           */
/*                                                                           */
/*  Sets h = be.  See my Robust Predicates paper for details.                */
/*                                                                           */
/*  Maintains the nonoverlapping property.  If round-to-even is used (as     */
/*  with IEEE 754), maintains the strongly nonoverlapping and nonadjacent    */
/*  properties as well.  (That is, if e has one of these properties, so      */
/*  will h.)                                                                 */
/*                                                                           */
/*****************************************************************************/

1630
int DelaunayTriangle::scale_expansion_zeroelimTRI(int elen, double *e, double b, double *h)
Michael Turner's avatar
files    
Michael Turner committed
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
{
     double Q, sum;
    double hh;
     double product1;
    double product0;
    int eindex, hindex;
    double enow;
     double bvirt;
    double avirt, bround, around;
     double c;
     double abig;
    double ahi, alo, bhi, blo;
    double err1, err2, err3;

    Split(b, bhi, blo);
    Two_Product_Presplit(e[0], b, bhi, blo, Q, hh);
    hindex = 0;
    if (hh != 0)
    {
        h[hindex++] = hh;
    }
    for (eindex = 1; eindex < elen; eindex++)
    {
        enow = e[eindex];
        Two_Product_Presplit(enow, b, bhi, blo, product1, product0);
        Two_Sum(Q, product0, sum, hh);
        if (hh != 0)
        {
            h[hindex++] = hh;
        }
        Fast_Two_Sum(product1, sum, Q, hh);
        if (hh != 0)
        {
            h[hindex++] = hh;
        }
    }
    if ((Q != 0.0) || (hindex == 0))
    {
        h[hindex++] = Q;
    }
    return hindex;
}

/*****************************************************************************/
/*                                                                           */
/*  estimateTRI()   Produce a one-word estimateTRI of an expansion's value.        */
/*                                                                           */
/*  See my Robust Predicates paper for details.                              */
/*                                                                           */
/*****************************************************************************/

1682
double DelaunayTriangle::estimateTRI(int elen, double *e)
Michael Turner's avatar
files    
Michael Turner committed
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
{
    double Q;
    int eindex;

    Q = e[0];
    for (eindex = 1; eindex < elen; eindex++)
    {
        Q += e[eindex];
    }
    return Q;
}

/*****************************************************************************/
/*                                                                           */
/*  counterclockwise()   Return a positive value if the points pa, pb, and   */
/*                       pc occur in counterclockwise order; a negative      */
/*                       value if they occur in clockwise order; and zero    */
/*                       if they are collinear.  The result is also a rough  */
/*                       approximation of twice the signed area of the       */
/*                       triangle defined by the three points.               */
/*                                                                           */
/*  Uses exact arithmetic if necessary to ensure a correct answer.  The      */
/*  result returned is the determinant of a matrix.  This determinant is     */
/*  computed adaptively, in the sense that exact arithmetic is used only to  */
/*  the degree it is needed to ensure that the returned value has the        */
/*  correct sign.  Hence, this function is usually quite fast, but will run  */
/*  more slowly when the input points are collinear or nearly so.            */
/*                                                                           */
/*  See my Robust Predicates paper for details.                              */
/*                                                                           */
/*****************************************************************************/

1715
double DelaunayTriangle::counterclockwiseadapt(vertex pa, vertex pb, vertex pc, double detsum)
Michael Turner's avatar
files    
Michael Turner committed
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
{
     double acx, acy, bcx, bcy;
    double acxtail, acytail, bcxtail, bcytail;
     double detleft, detright;
    double detlefttail, detrighttail;
    double det, errbound;
    double B[4], C1[8], C2[12], D[16];
     double B3;
    int C1length, C2length, Dlength;
    double u[4];
     double u3;
     double s1, t1;
    double s0, t0;

     double bvirt;
    double avirt, bround, around;
     double c;
     double abig;
    double ahi, alo, bhi, blo;
    double err1, err2, err3;
     double _i, _j;
    double _0;

    acx = (double)(pa[0] - pc[0]);
    bcx = (double)(pb[0] - pc[0]);
    acy = (double)(pa[1] - pc[1]);
    bcy = (double)(pb[1] - pc[1]);

    Two_Product(acx, bcy, detleft, detlefttail);
    Two_Product(acy, bcx, detright, detrighttail);

    Two_Two_Diff(
        detleft, detlefttail, detright, detrighttail, B3, B[2], B[1], B[0]);
    B[3] = B3;

    det      = estimateTRI(4, B);
    errbound = ccwerrboundB * detsum;
    if ((det >= errbound) || (-det >= errbound))
    {
        return det;
    }

    Two_Diff_Tail(pa[0], pc[0], acx, acxtail);
    Two_Diff_Tail(pb[0], pc[0], bcx, bcxtail);
    Two_Diff_Tail(pa[1], pc[1], acy, acytail);
    Two_Diff_Tail(pb[1], pc[1], bcy, bcytail);

    if ((acxtail == 0.0) && (acytail == 0.0) && (bcxtail == 0.0) &&
        (bcytail == 0.0))
    {
        return det;
    }

    errbound = ccwerrboundC * detsum + resulterrbound * Absolute(det);
    det += (acx * bcytail + bcy * acxtail) - (acy * bcxtail + bcx * acytail);
    if ((det >= errbound) || (-det >= errbound))
    {
        return det;
    }

    Two_Product(acxtail, bcy, s1, s0);
    Two_Product(acytail, bcx, t1, t0);
    Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
    u[3]     = u3;
    C1length = fast_expansion_sum_zeroelimTRI(4, B, 4, u, C1);

    Two_Product(acx, bcytail, s1, s0);
    Two_Product(acy, bcxtail, t1, t0);
    Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
    u[3]     = u3;
    C2length = fast_expansion_sum_zeroelimTRI(C1length, C1, 4, u, C2);

    Two_Product(acxtail, bcytail, s1, s0);
    Two_Product(acytail, bcxtail, t1, t0);
    Two_Two_Diff(s1, s0, t1, t0, u3, u[2], u[1], u[0]);
    u[3]    = u3;
    Dlength = fast_expansion_sum_zeroelimTRI(C2length, C2, 4, u, D);

    return (D[Dlength - 1]);
}

1797
double DelaunayTriangle::counterclockwise(
Michael Turner's avatar
files    
Michael Turner committed
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
    struct mesh *m, struct behavior *b, vertex pa, vertex pb, vertex pc)
{
    double detleft, detright, det;
    double detsum, errbound;

    m->counterclockcount++;

    detleft  = (pa[0] - pc[0]) * (pb[1] - pc[1]);
    detright = (pa[1] - pc[1]) * (pb[0] - pc[0]);
    det      = detleft - detright;

    if (detleft > 0.0)
    {
        if (detright <= 0.0)
        {
            return det;
        }
        else
        {
            detsum = detleft + detright;
        }
    }
    else if (detleft < 0.0)
    {
        if (detright >= 0.0)
        {
            return det;
        }
        else
        {
            detsum = -detleft - detright;
        }
    }
    else
    {
        return det;
    }

    errbound = ccwerrboundA * detsum;
    if ((det >= errbound) || (-det >= errbound))
    {
        return det;
    }

    return counterclockwiseadapt(pa, pb, pc, detsum);
}

/*****************************************************************************/
/*                                                                           */
/*  incircle()   Return a positive value if the point pd lies inside the     */
/*               circle passing through pa, pb, and pc; a negative value if  */
/*               it lies outside; and zero if the four points are cocircular.*/
/*               The points pa, pb, and pc must be in counterclockwise       */
/*               order, or the sign of the result will be reversed.          */
/*                                                                           */
/*  Uses exact arithmetic if necessary to ensure a correct answer.  The      */
/*  result returned is the determinant of a matrix.  This determinant is     */
/*  computed adaptively, in the sense that exact arithmetic is used only to  */
/*  the degree it is needed to ensure that the returned value has the        */
/*  correct sign.  Hence, this function is usually quite fast, but will run  */
/*  more slowly when the input points are cocircular or nearly so.           */
/*                                                                           */
/*  See my Robust Predicates paper for details.                              */
/*                                                                           */
/*****************************************************************************/

1864
double DelaunayTriangle::incircleadaptTRI(
Michael Turner's avatar
files    
Michael Turner committed
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
    vertex pa, vertex pb, vertex pc, vertex pd, double permanent)
{
     double adx, bdx, cdx, ady, bdy, cdy;
    double det, errbound;

     double bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
    double bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
    double bc[4], ca[4], ab[4];
     double bc3, ca3, ab3;
    double axbc[8], axxbc[16], aybc[8], ayybc[16], adet[32];
    int axbclen, axxbclen, aybclen, ayybclen, alen;
    double bxca[8], bxxca[16], byca[8], byyca[16], bdet[32];
    int bxcalen, bxxcalen, bycalen, byycalen, blen;
    double cxab[8], cxxab[16], cyab[8], cyyab[16], cdet[32];
    int cxablen, cxxablen, cyablen, cyyablen, clen;
    double abdet[64];
    int ablen;
    double fin1[1152], fin2[1152];
    double *finnow, *finother, *finswap;
    int finlength;

    double adxtail, bdxtail, cdxtail, adytail, bdytail, cdytail;
     double adxadx1, adyady1, bdxbdx1, bdybdy1, cdxcdx1, cdycdy1;
    double adxadx0, adyady0, bdxbdx0, bdybdy0, cdxcdx0, cdycdy0;
    double aa[4], bb[4], cc[4];
     double aa3, bb3, cc3;
     double ti1, tj1;
    double ti0, tj0;
    double u[4], v[4];
     double u3, v3;
    double temp8[8], temp16a[16], temp16b[16], temp16c[16];
    double temp32a[32], temp32b[32], temp48[48], temp64[64];
    int temp8len, temp16alen, temp16blen, temp16clen;
    int temp32alen, temp32blen, temp48len, temp64len;
    double axtbb[8], axtcc[8], aytbb[8], aytcc[8];
    int axtbblen, axtcclen, aytbblen, aytcclen;
    double bxtaa[8], bxtcc[8], bytaa[8], bytcc[8];
    int bxtaalen, bxtcclen, bytaalen, bytcclen;
    double cxtaa[8], cxtbb[8], cytaa[8], cytbb[8];
    int cxtaalen, cxtbblen, cytaalen, cytbblen;
    double axtbc[8], aytbc[8], bxtca[8], bytca[8], cxtab[8], cytab[8];
    int axtbclen, aytbclen, bxtcalen, bytcalen, cxtablen, cytablen;
    double axtbct[16], aytbct[16], bxtcat[16], bytcat[16], cxtabt[16],
        cytabt[16];
    int axtbctlen, aytbctlen, bxtcatlen, bytcatlen, cxtabtlen, cytabtlen;
    double axtbctt[8], aytbctt[8], bxtcatt[8];
    double bytcatt[8], cxtabtt[8], cytabtt[8];
    int axtbcttlen, aytbcttlen, bxtcattlen, bytcattlen, cxtabttlen, cytabttlen;
    double abt[8], bct[8], cat[8];
    int abtlen, bctlen, catlen;
    double abtt[4], bctt[4], catt[4];
    int abttlen, bcttlen, cattlen;
     double abtt3, bctt3, catt3;
    double negate;

     double bvirt;
    double avirt, bround, around;
     double c;
     double abig;
    double ahi, alo, bhi, blo;
    double err1, err2, err3;
     double _i, _j;
    double _0;

    adx = (double)(pa[0] - pd[0]);
    bdx = (double)(pb[0] - pd[0]);
    cdx = (double)(pc[0] - pd[0]);
    ady = (double)(pa[1] - pd[1]);
    bdy = (double)(pb[1] - pd[1]);
    cdy = (double)(pc[1] - pd[1]);

    Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
    Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
    Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
    bc[3]    = bc3;
    axbclen  = scale_expansion_zeroelimTRI(4, bc, adx, axbc);
    axxbclen = scale_expansion_zeroelimTRI(axbclen, axbc, adx, axxbc);
    aybclen  = scale_expansion_zeroelimTRI(4, bc, ady, aybc);
    ayybclen = scale_expansion_zeroelimTRI(aybclen, aybc, ady, ayybc);
    alen = fast_expansion_sum_zeroelimTRI(axxbclen, axxbc, ayybclen, ayybc, adet);

    Two_Product(cdx, ady, cdxady1, cdxady0);
    Two_Product(adx, cdy, adxcdy1, adxcdy0);
    Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
    ca[3]    = ca3;
    bxcalen  = scale_expansion_zeroelimTRI(4, ca, bdx, bxca);
    bxxcalen = scale_expansion_zeroelimTRI(bxcalen, bxca, bdx, bxxca);
    bycalen  = scale_expansion_zeroelimTRI(4, ca, bdy, byca);
    byycalen = scale_expansion_zeroelimTRI(bycalen, byca, bdy, byyca);
    blen = fast_expansion_sum_zeroelimTRI(bxxcalen, bxxca, byycalen, byyca, bdet);

    Two_Product(adx, bdy, adxbdy1, adxbdy0);
    Two_Product(bdx, ady, bdxady1, bdxady0);
    Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
    ab[3]    = ab3;
    cxablen  = scale_expansion_zeroelimTRI(4, ab, cdx, cxab);
    cxxablen = scale_expansion_zeroelimTRI(cxablen, cxab, cdx, cxxab);
    cyablen  = scale_expansion_zeroelimTRI(4, ab, cdy, cyab);
    cyyablen = scale_expansion_zeroelimTRI(cyablen, cyab, cdy, cyyab);
    clen = fast_expansion_sum_zeroelimTRI(cxxablen, cxxab, cyyablen, cyyab, cdet);

    ablen     = fast_expansion_sum_zeroelimTRI(alen, adet, blen, bdet, abdet);
    finlength = fast_expansion_sum_zeroelimTRI(ablen, abdet, clen, cdet, fin1);

    det      = estimateTRI(finlength, fin1);
    errbound = iccerrboundB * permanent;
    if ((det >= errbound) || (-det >= errbound))
    {
        return det;
    }

    Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
    Two_Diff_Tail(pa[1], pd[1], ady, adytail);
    Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
    Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
    Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
    Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
    if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0) &&
        (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0))
    {
        return det;
    }

    errbound = iccerrboundC * permanent + resulterrbound * Absolute(det);
    det += ((adx * adx + ady * ady) * ((bdx * cdytail + cdy * bdxtail) -
                                       (bdy * cdxtail + cdx * bdytail)) +
            2.0 * (adx * adxtail + ady * adytail) * (bdx * cdy - bdy * cdx)) +
           ((bdx * bdx + bdy * bdy) * ((cdx * adytail + ady * cdxtail) -
                                       (cdy * adxtail + adx * cdytail)) +
            2.0 * (bdx * bdxtail + bdy * bdytail) * (cdx * ady - cdy * adx)) +
           ((cdx * cdx + cdy * cdy) * ((adx * bdytail + bdy * adxtail) -
                                       (ady * bdxtail + bdx * adytail)) +
            2.0 * (cdx * cdxtail + cdy * cdytail) * (adx * bdy - ady * bdx));
    if ((det >= errbound) || (-det >= errbound))
    {
        return det;
    }

    finnow   = fin1;
    finother = fin2;

    if ((bdxtail != 0.0) || (bdytail != 0.0) || (cdxtail != 0.0) ||
        (cdytail != 0.0))
    {
        Square(adx, adxadx1, adxadx0);
        Square(ady, adyady1, adyady0);
        Two_Two_Sum(
            adxadx1, adxadx0, adyady1, adyady0, aa3, aa[2], aa[1], aa[0]);
        aa[3] = aa3;
    }
    if ((cdxtail != 0.0) || (cdytail != 0.0) || (adxtail != 0.0) ||
        (adytail != 0.0))
    {
        Square(bdx, bdxbdx1, bdxbdx0);
        Square(bdy, bdybdy1, bdybdy0);
        Two_Two_Sum(
            bdxbdx1, bdxbdx0, bdybdy1, bdybdy0, bb3, bb[2], bb[1], bb[0]);
        bb[3] = bb3;
    }
    if ((adxtail != 0.0) || (adytail != 0.0) || (bdxtail != 0.0) ||
        (bdytail != 0.0))
    {
        Square(cdx, cdxcdx1, cdxcdx0);
        Square(cdy, cdycdy1, cdycdy0);
        Two_Two_Sum(
            cdxcdx1, cdxcdx0, cdycdy1, cdycdy0, cc3, cc[2], cc[1], cc[0]);
        cc[3] = cc3;
    }

    if (adxtail != 0.0)
    {
        axtbclen = scale_expansion_zeroelimTRI(4, bc, adxtail, axtbc);
        temp16alen =
            scale_expansion_zeroelimTRI(axtbclen, axtbc, 2.0 * adx, temp16a);

        axtcclen   = scale_expansion_zeroelimTRI(4, cc, adxtail, axtcc);
        temp16blen = scale_expansion_zeroelimTRI(axtcclen, axtcc, bdy, temp16b);

        axtbblen   = scale_expansion_zeroelimTRI(4, bb, adxtail, axtbb);
        temp16clen = scale_expansion_zeroelimTRI(axtbblen, axtbb, -cdy, temp16c);

        temp32alen = fast_expansion_sum_zeroelimTRI(
            temp16alen, temp16a, temp16blen, temp16b, temp32a);
        temp48len = fast_expansion_sum_zeroelimTRI(
            temp16clen, temp16c, temp32alen, temp32a, temp48);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, temp48len, temp48, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (adytail != 0.0)
    {
        aytbclen = scale_expansion_zeroelimTRI(4, bc, adytail, aytbc);
        temp16alen =
            scale_expansion_zeroelimTRI(aytbclen, aytbc, 2.0 * ady, temp16a);

        aytbblen   = scale_expansion_zeroelimTRI(4, bb, adytail, aytbb);
        temp16blen = scale_expansion_zeroelimTRI(aytbblen, aytbb, cdx, temp16b);

        aytcclen   = scale_expansion_zeroelimTRI(4, cc, adytail, aytcc);
        temp16clen = scale_expansion_zeroelimTRI(aytcclen, aytcc, -bdx, temp16c);

        temp32alen = fast_expansion_sum_zeroelimTRI(
            temp16alen, temp16a, temp16blen, temp16b, temp32a);
        temp48len = fast_expansion_sum_zeroelimTRI(
            temp16clen, temp16c, temp32alen, temp32a, temp48);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, temp48len, temp48, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (bdxtail != 0.0)
    {
        bxtcalen = scale_expansion_zeroelimTRI(4, ca, bdxtail, bxtca);
        temp16alen =
            scale_expansion_zeroelimTRI(bxtcalen, bxtca, 2.0 * bdx, temp16a);

        bxtaalen   = scale_expansion_zeroelimTRI(4, aa, bdxtail, bxtaa);
        temp16blen = scale_expansion_zeroelimTRI(bxtaalen, bxtaa, cdy, temp16b);

        bxtcclen   = scale_expansion_zeroelimTRI(4, cc, bdxtail, bxtcc);
        temp16clen = scale_expansion_zeroelimTRI(bxtcclen, bxtcc, -ady, temp16c);

        temp32alen = fast_expansion_sum_zeroelimTRI(
            temp16alen, temp16a, temp16blen, temp16b, temp32a);
        temp48len = fast_expansion_sum_zeroelimTRI(
            temp16clen, temp16c, temp32alen, temp32a, temp48);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, temp48len, temp48, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (bdytail != 0.0)
    {
        bytcalen = scale_expansion_zeroelimTRI(4, ca, bdytail, bytca);
        temp16alen =
            scale_expansion_zeroelimTRI(bytcalen, bytca, 2.0 * bdy, temp16a);

        bytcclen   = scale_expansion_zeroelimTRI(4, cc, bdytail, bytcc);
        temp16blen = scale_expansion_zeroelimTRI(bytcclen, bytcc, adx, temp16b);

        bytaalen   = scale_expansion_zeroelimTRI(4, aa, bdytail, bytaa);
        temp16clen = scale_expansion_zeroelimTRI(bytaalen, bytaa, -cdx, temp16c);

        temp32alen = fast_expansion_sum_zeroelimTRI(
            temp16alen, temp16a, temp16blen, temp16b, temp32a);
        temp48len = fast_expansion_sum_zeroelimTRI(
            temp16clen, temp16c, temp32alen, temp32a, temp48);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, temp48len, temp48, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (cdxtail != 0.0)
    {
        cxtablen = scale_expansion_zeroelimTRI(4, ab, cdxtail, cxtab);
        temp16alen =
            scale_expansion_zeroelimTRI(cxtablen, cxtab, 2.0 * cdx, temp16a);

        cxtbblen   = scale_expansion_zeroelimTRI(4, bb, cdxtail, cxtbb);
        temp16blen = scale_expansion_zeroelimTRI(cxtbblen, cxtbb, ady, temp16b);

        cxtaalen   = scale_expansion_zeroelimTRI(4, aa, cdxtail, cxtaa);
        temp16clen = scale_expansion_zeroelimTRI(cxtaalen, cxtaa, -bdy, temp16c);

        temp32alen = fast_expansion_sum_zeroelimTRI(
            temp16alen, temp16a, temp16blen, temp16b, temp32a);
        temp48len = fast_expansion_sum_zeroelimTRI(
            temp16clen, temp16c, temp32alen, temp32a, temp48);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, temp48len, temp48, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (cdytail != 0.0)
    {
        cytablen = scale_expansion_zeroelimTRI(4, ab, cdytail, cytab);
        temp16alen =
            scale_expansion_zeroelimTRI(cytablen, cytab, 2.0 * cdy, temp16a);

        cytaalen   = scale_expansion_zeroelimTRI(4, aa, cdytail, cytaa);
        temp16blen = scale_expansion_zeroelimTRI(cytaalen, cytaa, bdx, temp16b);

        cytbblen   = scale_expansion_zeroelimTRI(4, bb, cdytail, cytbb);
        temp16clen = scale_expansion_zeroelimTRI(cytbblen, cytbb, -adx, temp16c);

        temp32alen = fast_expansion_sum_zeroelimTRI(
            temp16alen, temp16a, temp16blen, temp16b, temp32a);
        temp48len = fast_expansion_sum_zeroelimTRI(
            temp16clen, temp16c, temp32alen, temp32a, temp48);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, temp48len, temp48, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }

    if ((adxtail != 0.0) || (adytail != 0.0))
    {
        if ((bdxtail != 0.0) || (bdytail != 0.0) || (cdxtail != 0.0) ||
            (cdytail != 0.0))
        {
            Two_Product(bdxtail, cdy, ti1, ti0);
            Two_Product(bdx, cdytail, tj1, tj0);
            Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
            u[3]   = u3;
            negate = -bdy;
            Two_Product(cdxtail, negate, ti1, ti0);
            negate = -bdytail;
            Two_Product(cdx, negate, tj1, tj0);
            Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
            v[3]   = v3;
            bctlen = fast_expansion_sum_zeroelimTRI(4, u, 4, v, bct);

            Two_Product(bdxtail, cdytail, ti1, ti0);
            Two_Product(cdxtail, bdytail, tj1, tj0);
            Two_Two_Diff(ti1, ti0, tj1, tj0, bctt3, bctt[2], bctt[1], bctt[0]);
            bctt[3] = bctt3;
            bcttlen = 4;
        }
        else
        {
            bct[0]  = 0.0;
            bctlen  = 1;
            bctt[0] = 0.0;
            bcttlen = 1;
        }

        if (adxtail != 0.0)
        {
            temp16alen =
                scale_expansion_zeroelimTRI(axtbclen, axtbc, adxtail, temp16a);
            axtbctlen = scale_expansion_zeroelimTRI(bctlen, bct, adxtail, axtbct);
            temp32alen =
                scale_expansion_zeroelimTRI(axtbctlen, axtbct, 2.0 * adx, temp32a);
            temp48len = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp32alen, temp32a, temp48);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp48len, temp48, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (bdytail != 0.0)
            {
                temp8len = scale_expansion_zeroelimTRI(4, cc, adxtail, temp8);
                temp16alen =
                    scale_expansion_zeroelimTRI(temp8len, temp8, bdytail, temp16a);
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, temp16alen, temp16a, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
            if (cdytail != 0.0)
            {
                temp8len = scale_expansion_zeroelimTRI(4, bb, -adxtail, temp8);
                temp16alen =
                    scale_expansion_zeroelimTRI(temp8len, temp8, cdytail, temp16a);
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, temp16alen, temp16a, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }

            temp32alen =
                scale_expansion_zeroelimTRI(axtbctlen, axtbct, adxtail, temp32a);
            axtbcttlen =
                scale_expansion_zeroelimTRI(bcttlen, bctt, adxtail, axtbctt);
            temp16alen = scale_expansion_zeroelimTRI(
                axtbcttlen, axtbctt, 2.0 * adx, temp16a);
            temp16blen =
                scale_expansion_zeroelimTRI(axtbcttlen, axtbctt, adxtail, temp16b);
            temp32blen = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp16blen, temp16b, temp32b);
            temp64len = fast_expansion_sum_zeroelimTRI(
                temp32alen, temp32a, temp32blen, temp32b, temp64);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp64len, temp64, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
        }
        if (adytail != 0.0)
        {
            temp16alen =
                scale_expansion_zeroelimTRI(aytbclen, aytbc, adytail, temp16a);
            aytbctlen = scale_expansion_zeroelimTRI(bctlen, bct, adytail, aytbct);
            temp32alen =
                scale_expansion_zeroelimTRI(aytbctlen, aytbct, 2.0 * ady, temp32a);
            temp48len = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp32alen, temp32a, temp48);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp48len, temp48, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;

            temp32alen =
                scale_expansion_zeroelimTRI(aytbctlen, aytbct, adytail, temp32a);
            aytbcttlen =
                scale_expansion_zeroelimTRI(bcttlen, bctt, adytail, aytbctt);
            temp16alen = scale_expansion_zeroelimTRI(
                aytbcttlen, aytbctt, 2.0 * ady, temp16a);
            temp16blen =
                scale_expansion_zeroelimTRI(aytbcttlen, aytbctt, adytail, temp16b);
            temp32blen = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp16blen, temp16b, temp32b);
            temp64len = fast_expansion_sum_zeroelimTRI(
                temp32alen, temp32a, temp32blen, temp32b, temp64);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp64len, temp64, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
        }
    }
    if ((bdxtail != 0.0) || (bdytail != 0.0))
    {
        if ((cdxtail != 0.0) || (cdytail != 0.0) || (adxtail != 0.0) ||
            (adytail != 0.0))
        {
            Two_Product(cdxtail, ady, ti1, ti0);
            Two_Product(cdx, adytail, tj1, tj0);
            Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
            u[3]   = u3;
            negate = -cdy;
            Two_Product(adxtail, negate, ti1, ti0);
            negate = -cdytail;
            Two_Product(adx, negate, tj1, tj0);
            Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
            v[3]   = v3;
            catlen = fast_expansion_sum_zeroelimTRI(4, u, 4, v, cat);

            Two_Product(cdxtail, adytail, ti1, ti0);
            Two_Product(adxtail, cdytail, tj1, tj0);
            Two_Two_Diff(ti1, ti0, tj1, tj0, catt3, catt[2], catt[1], catt[0]);
            catt[3] = catt3;
            cattlen = 4;
        }
        else
        {
            cat[0]  = 0.0;
            catlen  = 1;
            catt[0] = 0.0;
            cattlen = 1;
        }

        if (bdxtail != 0.0)
        {
            temp16alen =
                scale_expansion_zeroelimTRI(bxtcalen, bxtca, bdxtail, temp16a);
            bxtcatlen = scale_expansion_zeroelimTRI(catlen, cat, bdxtail, bxtcat);
            temp32alen =
                scale_expansion_zeroelimTRI(bxtcatlen, bxtcat, 2.0 * bdx, temp32a);
            temp48len = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp32alen, temp32a, temp48);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp48len, temp48, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (cdytail != 0.0)
            {
                temp8len = scale_expansion_zeroelimTRI(4, aa, bdxtail, temp8);
                temp16alen =
                    scale_expansion_zeroelimTRI(temp8len, temp8, cdytail, temp16a);
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, temp16alen, temp16a, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
            if (adytail != 0.0)
            {
                temp8len = scale_expansion_zeroelimTRI(4, cc, -bdxtail, temp8);
                temp16alen =
                    scale_expansion_zeroelimTRI(temp8len, temp8, adytail, temp16a);
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, temp16alen, temp16a, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }

            temp32alen =
                scale_expansion_zeroelimTRI(bxtcatlen, bxtcat, bdxtail, temp32a);
            bxtcattlen =
                scale_expansion_zeroelimTRI(cattlen, catt, bdxtail, bxtcatt);
            temp16alen = scale_expansion_zeroelimTRI(
                bxtcattlen, bxtcatt, 2.0 * bdx, temp16a);
            temp16blen =
                scale_expansion_zeroelimTRI(bxtcattlen, bxtcatt, bdxtail, temp16b);
            temp32blen = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp16blen, temp16b, temp32b);
            temp64len = fast_expansion_sum_zeroelimTRI(
                temp32alen, temp32a, temp32blen, temp32b, temp64);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp64len, temp64, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
        }
        if (bdytail != 0.0)
        {
            temp16alen =
                scale_expansion_zeroelimTRI(bytcalen, bytca, bdytail, temp16a);
            bytcatlen = scale_expansion_zeroelimTRI(catlen, cat, bdytail, bytcat);
            temp32alen =
                scale_expansion_zeroelimTRI(bytcatlen, bytcat, 2.0 * bdy, temp32a);
            temp48len = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp32alen, temp32a, temp48);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp48len, temp48, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;

            temp32alen =
                scale_expansion_zeroelimTRI(bytcatlen, bytcat, bdytail, temp32a);
            bytcattlen =
                scale_expansion_zeroelimTRI(cattlen, catt, bdytail, bytcatt);
            temp16alen = scale_expansion_zeroelimTRI(
                bytcattlen, bytcatt, 2.0 * bdy, temp16a);
            temp16blen =
                scale_expansion_zeroelimTRI(bytcattlen, bytcatt, bdytail, temp16b);
            temp32blen = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp16blen, temp16b, temp32b);
            temp64len = fast_expansion_sum_zeroelimTRI(
                temp32alen, temp32a, temp32blen, temp32b, temp64);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp64len, temp64, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
        }
    }
    if ((cdxtail != 0.0) || (cdytail != 0.0))
    {
        if ((adxtail != 0.0) || (adytail != 0.0) || (bdxtail != 0.0) ||
            (bdytail != 0.0))
        {
            Two_Product(adxtail, bdy, ti1, ti0);
            Two_Product(adx, bdytail, tj1, tj0);
            Two_Two_Sum(ti1, ti0, tj1, tj0, u3, u[2], u[1], u[0]);
            u[3]   = u3;
            negate = -ady;
            Two_Product(bdxtail, negate, ti1, ti0);
            negate = -adytail;
            Two_Product(bdx, negate, tj1, tj0);
            Two_Two_Sum(ti1, ti0, tj1, tj0, v3, v[2], v[1], v[0]);
            v[3]   = v3;
            abtlen = fast_expansion_sum_zeroelimTRI(4, u, 4, v, abt);

            Two_Product(adxtail, bdytail, ti1, ti0);
            Two_Product(bdxtail, adytail, tj1, tj0);
            Two_Two_Diff(ti1, ti0, tj1, tj0, abtt3, abtt[2], abtt[1], abtt[0]);
            abtt[3] = abtt3;
            abttlen = 4;
        }
        else
        {
            abt[0]  = 0.0;
            abtlen  = 1;
            abtt[0] = 0.0;
            abttlen = 1;
        }

        if (cdxtail != 0.0)
        {
            temp16alen =
                scale_expansion_zeroelimTRI(cxtablen, cxtab, cdxtail, temp16a);
            cxtabtlen = scale_expansion_zeroelimTRI(abtlen, abt, cdxtail, cxtabt);
            temp32alen =
                scale_expansion_zeroelimTRI(cxtabtlen, cxtabt, 2.0 * cdx, temp32a);
            temp48len = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp32alen, temp32a, temp48);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp48len, temp48, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (adytail != 0.0)
            {
                temp8len = scale_expansion_zeroelimTRI(4, bb, cdxtail, temp8);
                temp16alen =
                    scale_expansion_zeroelimTRI(temp8len, temp8, adytail, temp16a);
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, temp16alen, temp16a, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
            if (bdytail != 0.0)
            {
                temp8len = scale_expansion_zeroelimTRI(4, aa, -cdxtail, temp8);
                temp16alen =
                    scale_expansion_zeroelimTRI(temp8len, temp8, bdytail, temp16a);
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, temp16alen, temp16a, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }

            temp32alen =
                scale_expansion_zeroelimTRI(cxtabtlen, cxtabt, cdxtail, temp32a);
            cxtabttlen =
                scale_expansion_zeroelimTRI(abttlen, abtt, cdxtail, cxtabtt);
            temp16alen = scale_expansion_zeroelimTRI(
                cxtabttlen, cxtabtt, 2.0 * cdx, temp16a);
            temp16blen =
                scale_expansion_zeroelimTRI(cxtabttlen, cxtabtt, cdxtail, temp16b);
            temp32blen = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp16blen, temp16b, temp32b);
            temp64len = fast_expansion_sum_zeroelimTRI(
                temp32alen, temp32a, temp32blen, temp32b, temp64);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp64len, temp64, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
        }
        if (cdytail != 0.0)
        {
            temp16alen =
                scale_expansion_zeroelimTRI(cytablen, cytab, cdytail, temp16a);
            cytabtlen = scale_expansion_zeroelimTRI(abtlen, abt, cdytail, cytabt);
            temp32alen =
                scale_expansion_zeroelimTRI(cytabtlen, cytabt, 2.0 * cdy, temp32a);
            temp48len = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp32alen, temp32a, temp48);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp48len, temp48, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;

            temp32alen =
                scale_expansion_zeroelimTRI(cytabtlen, cytabt, cdytail, temp32a);
            cytabttlen =
                scale_expansion_zeroelimTRI(abttlen, abtt, cdytail, cytabtt);
            temp16alen = scale_expansion_zeroelimTRI(
                cytabttlen, cytabtt, 2.0 * cdy, temp16a);
            temp16blen =
                scale_expansion_zeroelimTRI(cytabttlen, cytabtt, cdytail, temp16b);
            temp32blen = fast_expansion_sum_zeroelimTRI(
                temp16alen, temp16a, temp16blen, temp16b, temp32b);
            temp64len = fast_expansion_sum_zeroelimTRI(
                temp32alen, temp32a, temp32blen, temp32b, temp64);
            finlength = fast_expansion_sum_zeroelimTRI(
                finlength, finnow, temp64len, temp64, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
        }
    }

    return finnow[finlength - 1];
}

2531
double DelaunayTriangle::incircle(struct mesh *m,
Michael Turner's avatar
files    
Michael Turner committed
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
                struct behavior *b,
                vertex pa,
                vertex pb,
                vertex pc,
                vertex pd)
{
    double adx, bdx, cdx, ady, bdy, cdy;
    double bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
    double alift, blift, clift;
    double det;
    double permanent, errbound;

    m->incirclecount++;

    adx = pa[0] - pd[0];
    bdx = pb[0] - pd[0];
    cdx = pc[0] - pd[0];
    ady = pa[1] - pd[1];
    bdy = pb[1] - pd[1];
    cdy = pc[1] - pd[1];

    bdxcdy = bdx * cdy;
    cdxbdy = cdx * bdy;
    alift  = adx * adx + ady * ady;

    cdxady = cdx * ady;
    adxcdy = adx * cdy;
    blift  = bdx * bdx + bdy * bdy;

    adxbdy = adx * bdy;
    bdxady = bdx * ady;
    clift  = cdx * cdx + cdy * cdy;

    det = alift * (bdxcdy - cdxbdy) + blift * (cdxady - adxcdy) +
          clift * (adxbdy - bdxady);

    permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * alift +
                (Absolute(cdxady) + Absolute(adxcdy)) * blift +
                (Absolute(adxbdy) + Absolute(bdxady)) * clift;
    errbound = iccerrboundA * permanent;
    if ((det > errbound) || (-det > errbound))
    {
        return det;
    }

    return incircleadaptTRI(pa, pb, pc, pd, permanent);
}

/*****************************************************************************/
/*                                                                           */
/*  orient3d()   Return a positive value if the point pd lies below the      */
/*               plane passing through pa, pb, and pc; "below" is defined so */
/*               that pa, pb, and pc appear in counterclockwise order when   */
/*               viewed from above the plane.  Returns a negative value if   */
/*               pd lies above the plane.  Returns zero if the points are    */
/*               coplanar.  The result is also a rough approximation of six  */
/*               times the signed volume of the tetrahedron defined by the   */
/*               four points.                                                */
/*                                                                           */
/*  Uses exact arithmetic if necessary to ensure a correct answer.  The      */
/*  result returned is the determinant of a matrix.  This determinant is     */
/*  computed adaptively, in the sense that exact arithmetic is used only to  */
/*  the degree it is needed to ensure that the returned value has the        */
/*  correct sign.  Hence, this function is usually quite fast, but will run  */
/*  more slowly when the input points are coplanar or nearly so.             */
/*                                                                           */
/*  See my Robust Predicates paper for details.                              */
/*                                                                           */
/*****************************************************************************/

2602
double DelaunayTriangle::orient3dadapt(vertex pa,
Michael Turner's avatar
files    
Michael Turner committed
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
                     vertex pb,
                     vertex pc,
                     vertex pd,
                     double aheight,
                     double bheight,
                     double cheight,
                     double dheight,
                     double permanent)
{
     double adx, bdx, cdx, ady, bdy, cdy, adheight, bdheight, cdheight;
    double det, errbound;

     double bdxcdy1, cdxbdy1, cdxady1, adxcdy1, adxbdy1, bdxady1;
    double bdxcdy0, cdxbdy0, cdxady0, adxcdy0, adxbdy0, bdxady0;
    double bc[4], ca[4], ab[4];
     double bc3, ca3, ab3;
    double adet[8], bdet[8], cdet[8];
    int alen, blen, clen;
    double abdet[16];
    int ablen;
    double *finnow, *finother, *finswap;
    double fin1[192], fin2[192];
    int finlength;

    double adxtail, bdxtail, cdxtail;
    double adytail, bdytail, cdytail;
    double adheighttail, bdheighttail, cdheighttail;
     double at_blarge, at_clarge;
     double bt_clarge, bt_alarge;
     double ct_alarge, ct_blarge;
    double at_b[4], at_c[4], bt_c[4], bt_a[4], ct_a[4], ct_b[4];
    int at_blen, at_clen, bt_clen, bt_alen, ct_alen, ct_blen;
     double bdxt_cdy1, cdxt_bdy1, cdxt_ady1;
     double adxt_cdy1, adxt_bdy1, bdxt_ady1;
    double bdxt_cdy0, cdxt_bdy0, cdxt_ady0;
    double adxt_cdy0, adxt_bdy0, bdxt_ady0;
     double bdyt_cdx1, cdyt_bdx1, cdyt_adx1;
     double adyt_cdx1, adyt_bdx1, bdyt_adx1;
    double bdyt_cdx0, cdyt_bdx0, cdyt_adx0;
    double adyt_cdx0, adyt_bdx0, bdyt_adx0;
    double bct[8], cat[8], abt[8];
    int bctlen, catlen, abtlen;
     double bdxt_cdyt1, cdxt_bdyt1, cdxt_adyt1;
     double adxt_cdyt1, adxt_bdyt1, bdxt_adyt1;
    double bdxt_cdyt0, cdxt_bdyt0, cdxt_adyt0;
    double adxt_cdyt0, adxt_bdyt0, bdxt_adyt0;
    double u[4], v[12], w[16];
     double u3;
    int vlength, wlength;
    double negate;

     double bvirt;
    double avirt, bround, around;
     double c;
     double abig;
    double ahi, alo, bhi, blo;
    double err1, err2, err3;
     double _i, _j, _k;
    double _0;

    adx      = (double)(pa[0] - pd[0]);
    bdx      = (double)(pb[0] - pd[0]);
    cdx      = (double)(pc[0] - pd[0]);
    ady      = (double)(pa[1] - pd[1]);
    bdy      = (double)(pb[1] - pd[1]);
    cdy      = (double)(pc[1] - pd[1]);
    adheight = (double)(aheight - dheight);
    bdheight = (double)(bheight - dheight);
    cdheight = (double)(cheight - dheight);

    Two_Product(bdx, cdy, bdxcdy1, bdxcdy0);
    Two_Product(cdx, bdy, cdxbdy1, cdxbdy0);
    Two_Two_Diff(bdxcdy1, bdxcdy0, cdxbdy1, cdxbdy0, bc3, bc[2], bc[1], bc[0]);
    bc[3] = bc3;
    alen  = scale_expansion_zeroelimTRI(4, bc, adheight, adet);

    Two_Product(cdx, ady, cdxady1, cdxady0);
    Two_Product(adx, cdy, adxcdy1, adxcdy0);
    Two_Two_Diff(cdxady1, cdxady0, adxcdy1, adxcdy0, ca3, ca[2], ca[1], ca[0]);
    ca[3] = ca3;
    blen  = scale_expansion_zeroelimTRI(4, ca, bdheight, bdet);

    Two_Product(adx, bdy, adxbdy1, adxbdy0);
    Two_Product(bdx, ady, bdxady1, bdxady0);
    Two_Two_Diff(adxbdy1, adxbdy0, bdxady1, bdxady0, ab3, ab[2], ab[1], ab[0]);
    ab[3] = ab3;
    clen  = scale_expansion_zeroelimTRI(4, ab, cdheight, cdet);

    ablen     = fast_expansion_sum_zeroelimTRI(alen, adet, blen, bdet, abdet);
    finlength = fast_expansion_sum_zeroelimTRI(ablen, abdet, clen, cdet, fin1);

    det      = estimateTRI(finlength, fin1);
    errbound = o3derrboundB * permanent;
    if ((det >= errbound) || (-det >= errbound))
    {
        return det;
    }

    Two_Diff_Tail(pa[0], pd[0], adx, adxtail);
    Two_Diff_Tail(pb[0], pd[0], bdx, bdxtail);
    Two_Diff_Tail(pc[0], pd[0], cdx, cdxtail);
    Two_Diff_Tail(pa[1], pd[1], ady, adytail);
    Two_Diff_Tail(pb[1], pd[1], bdy, bdytail);
    Two_Diff_Tail(pc[1], pd[1], cdy, cdytail);
    Two_Diff_Tail(aheight, dheight, adheight, adheighttail);
    Two_Diff_Tail(bheight, dheight, bdheight, bdheighttail);
    Two_Diff_Tail(cheight, dheight, cdheight, cdheighttail);

    if ((adxtail == 0.0) && (bdxtail == 0.0) && (cdxtail == 0.0) &&
        (adytail == 0.0) && (bdytail == 0.0) && (cdytail == 0.0) &&
        (adheighttail == 0.0) && (bdheighttail == 0.0) && (cdheighttail == 0.0))
    {
        return det;
    }

    errbound = o3derrboundC * permanent + resulterrbound * Absolute(det);
    det += (adheight * ((bdx * cdytail + cdy * bdxtail) -
                        (bdy * cdxtail + cdx * bdytail)) +
            adheighttail * (bdx * cdy - bdy * cdx)) +
           (bdheight * ((cdx * adytail + ady * cdxtail) -
                        (cdy * adxtail + adx * cdytail)) +
            bdheighttail * (cdx * ady - cdy * adx)) +
           (cdheight * ((adx * bdytail + bdy * adxtail) -
                        (ady * bdxtail + bdx * adytail)) +
            cdheighttail * (adx * bdy - ady * bdx));
    if ((det >= errbound) || (-det >= errbound))
    {
        return det;
    }

    finnow   = fin1;
    finother = fin2;

    if (adxtail == 0.0)
    {
        if (adytail == 0.0)
        {
            at_b[0] = 0.0;
            at_blen = 1;
            at_c[0] = 0.0;
            at_clen = 1;
        }
        else
        {
            negate = -adytail;
            Two_Product(negate, bdx, at_blarge, at_b[0]);
            at_b[1] = at_blarge;
            at_blen = 2;
            Two_Product(adytail, cdx, at_clarge, at_c[0]);
            at_c[1] = at_clarge;
            at_clen = 2;
        }
    }
    else
    {
        if (adytail == 0.0)
        {
            Two_Product(adxtail, bdy, at_blarge, at_b[0]);
            at_b[1] = at_blarge;
            at_blen = 2;
            negate  = -adxtail;
            Two_Product(negate, cdy, at_clarge, at_c[0]);
            at_c[1] = at_clarge;
            at_clen = 2;
        }
        else
        {
            Two_Product(adxtail, bdy, adxt_bdy1, adxt_bdy0);
            Two_Product(adytail, bdx, adyt_bdx1, adyt_bdx0);
            Two_Two_Diff(adxt_bdy1,
                         adxt_bdy0,
                         adyt_bdx1,
                         adyt_bdx0,
                         at_blarge,
                         at_b[2],
                         at_b[1],
                         at_b[0]);
            at_b[3] = at_blarge;
            at_blen = 4;
            Two_Product(adytail, cdx, adyt_cdx1, adyt_cdx0);
            Two_Product(adxtail, cdy, adxt_cdy1, adxt_cdy0);
            Two_Two_Diff(adyt_cdx1,
                         adyt_cdx0,
                         adxt_cdy1,
                         adxt_cdy0,
                         at_clarge,
                         at_c[2],
                         at_c[1],
                         at_c[0]);
            at_c[3] = at_clarge;
            at_clen = 4;
        }
    }
    if (bdxtail == 0.0)
    {
        if (bdytail == 0.0)
        {
            bt_c[0] = 0.0;
            bt_clen = 1;
            bt_a[0] = 0.0;
            bt_alen = 1;
        }
        else
        {
            negate = -bdytail;
            Two_Product(negate, cdx, bt_clarge, bt_c[0]);
            bt_c[1] = bt_clarge;
            bt_clen = 2;
            Two_Product(bdytail, adx, bt_alarge, bt_a[0]);
            bt_a[1] = bt_alarge;
            bt_alen = 2;
        }
    }
    else
    {
        if (bdytail == 0.0)
        {
            Two_Product(bdxtail, cdy, bt_clarge, bt_c[0]);
            bt_c[1] = bt_clarge;
            bt_clen = 2;
            negate  = -bdxtail;
            Two_Product(negate, ady, bt_alarge, bt_a[0]);
            bt_a[1] = bt_alarge;
            bt_alen = 2;
        }
        else
        {
            Two_Product(bdxtail, cdy, bdxt_cdy1, bdxt_cdy0);
            Two_Product(bdytail, cdx, bdyt_cdx1, bdyt_cdx0);
            Two_Two_Diff(bdxt_cdy1,
                         bdxt_cdy0,
                         bdyt_cdx1,
                         bdyt_cdx0,
                         bt_clarge,
                         bt_c[2],
                         bt_c[1],
                         bt_c[0]);
            bt_c[3] = bt_clarge;
            bt_clen = 4;
            Two_Product(bdytail, adx, bdyt_adx1, bdyt_adx0);
            Two_Product(bdxtail, ady, bdxt_ady1, bdxt_ady0);
            Two_Two_Diff(bdyt_adx1,
                         bdyt_adx0,
                         bdxt_ady1,
                         bdxt_ady0,
                         bt_alarge,
                         bt_a[2],
                         bt_a[1],
                         bt_a[0]);
            bt_a[3] = bt_alarge;
            bt_alen = 4;
        }
    }
    if (cdxtail == 0.0)
    {
        if (cdytail == 0.0)
        {
            ct_a[0] = 0.0;
            ct_alen = 1;
            ct_b[0] = 0.0;
            ct_blen = 1;
        }
        else
        {
            negate = -cdytail;
            Two_Product(negate, adx, ct_alarge, ct_a[0]);
            ct_a[1] = ct_alarge;
            ct_alen = 2;
            Two_Product(cdytail, bdx, ct_blarge, ct_b[0]);
            ct_b[1] = ct_blarge;
            ct_blen = 2;
        }
    }
    else
    {
        if (cdytail == 0.0)
        {
            Two_Product(cdxtail, ady, ct_alarge, ct_a[0]);
            ct_a[1] = ct_alarge;
            ct_alen = 2;
            negate  = -cdxtail;
            Two_Product(negate, bdy, ct_blarge, ct_b[0]);
            ct_b[1] = ct_blarge;
            ct_blen = 2;
        }
        else
        {
            Two_Product(cdxtail, ady, cdxt_ady1, cdxt_ady0);
            Two_Product(cdytail, adx, cdyt_adx1, cdyt_adx0);
            Two_Two_Diff(cdxt_ady1,
                         cdxt_ady0,
                         cdyt_adx1,
                         cdyt_adx0,
                         ct_alarge,
                         ct_a[2],
                         ct_a[1],
                         ct_a[0]);
            ct_a[3] = ct_alarge;
            ct_alen = 4;
            Two_Product(cdytail, bdx, cdyt_bdx1, cdyt_bdx0);
            Two_Product(cdxtail, bdy, cdxt_bdy1, cdxt_bdy0);
            Two_Two_Diff(cdyt_bdx1,
                         cdyt_bdx0,
                         cdxt_bdy1,
                         cdxt_bdy0,
                         ct_blarge,
                         ct_b[2],
                         ct_b[1],
                         ct_b[0]);
            ct_b[3] = ct_blarge;
            ct_blen = 4;
        }
    }

    bctlen  = fast_expansion_sum_zeroelimTRI(bt_clen, bt_c, ct_blen, ct_b, bct);
    wlength = scale_expansion_zeroelimTRI(bctlen, bct, adheight, w);
    finlength =
        fast_expansion_sum_zeroelimTRI(finlength, finnow, wlength, w, finother);
    finswap  = finnow;
    finnow   = finother;
    finother = finswap;

    catlen  = fast_expansion_sum_zeroelimTRI(ct_alen, ct_a, at_clen, at_c, cat);
    wlength = scale_expansion_zeroelimTRI(catlen, cat, bdheight, w);
    finlength =
        fast_expansion_sum_zeroelimTRI(finlength, finnow, wlength, w, finother);
    finswap  = finnow;
    finnow   = finother;
    finother = finswap;

    abtlen  = fast_expansion_sum_zeroelimTRI(at_blen, at_b, bt_alen, bt_a, abt);
    wlength = scale_expansion_zeroelimTRI(abtlen, abt, cdheight, w);
    finlength =
        fast_expansion_sum_zeroelimTRI(finlength, finnow, wlength, w, finother);
    finswap  = finnow;
    finnow   = finother;
    finother = finswap;

    if (adheighttail != 0.0)
    {
        vlength   = scale_expansion_zeroelimTRI(4, bc, adheighttail, v);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, vlength, v, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (bdheighttail != 0.0)
    {
        vlength   = scale_expansion_zeroelimTRI(4, ca, bdheighttail, v);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, vlength, v, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (cdheighttail != 0.0)
    {
        vlength   = scale_expansion_zeroelimTRI(4, ab, cdheighttail, v);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, vlength, v, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }

    if (adxtail != 0.0)
    {
        if (bdytail != 0.0)
        {
            Two_Product(adxtail, bdytail, adxt_bdyt1, adxt_bdyt0);
            Two_One_Product(
                adxt_bdyt1, adxt_bdyt0, cdheight, u3, u[2], u[1], u[0]);
            u[3] = u3;
            finlength =
                fast_expansion_sum_zeroelimTRI(finlength, finnow, 4, u, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (cdheighttail != 0.0)
            {
                Two_One_Product(
                    adxt_bdyt1, adxt_bdyt0, cdheighttail, u3, u[2], u[1], u[0]);
                u[3]      = u3;
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, 4, u, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
        }
        if (cdytail != 0.0)
        {
            negate = -adxtail;
            Two_Product(negate, cdytail, adxt_cdyt1, adxt_cdyt0);
            Two_One_Product(
                adxt_cdyt1, adxt_cdyt0, bdheight, u3, u[2], u[1], u[0]);
            u[3] = u3;
            finlength =
                fast_expansion_sum_zeroelimTRI(finlength, finnow, 4, u, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (bdheighttail != 0.0)
            {
                Two_One_Product(
                    adxt_cdyt1, adxt_cdyt0, bdheighttail, u3, u[2], u[1], u[0]);
                u[3]      = u3;
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, 4, u, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
        }
    }
    if (bdxtail != 0.0)
    {
        if (cdytail != 0.0)
        {
            Two_Product(bdxtail, cdytail, bdxt_cdyt1, bdxt_cdyt0);
            Two_One_Product(
                bdxt_cdyt1, bdxt_cdyt0, adheight, u3, u[2], u[1], u[0]);
            u[3] = u3;
            finlength =
                fast_expansion_sum_zeroelimTRI(finlength, finnow, 4, u, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (adheighttail != 0.0)
            {
                Two_One_Product(
                    bdxt_cdyt1, bdxt_cdyt0, adheighttail, u3, u[2], u[1], u[0]);
                u[3]      = u3;
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, 4, u, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
        }
        if (adytail != 0.0)
        {
            negate = -bdxtail;
            Two_Product(negate, adytail, bdxt_adyt1, bdxt_adyt0);
            Two_One_Product(
                bdxt_adyt1, bdxt_adyt0, cdheight, u3, u[2], u[1], u[0]);
            u[3] = u3;
            finlength =
                fast_expansion_sum_zeroelimTRI(finlength, finnow, 4, u, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (cdheighttail != 0.0)
            {
                Two_One_Product(
                    bdxt_adyt1, bdxt_adyt0, cdheighttail, u3, u[2], u[1], u[0]);
                u[3]      = u3;
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, 4, u, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
        }
    }
    if (cdxtail != 0.0)
    {
        if (adytail != 0.0)
        {
            Two_Product(cdxtail, adytail, cdxt_adyt1, cdxt_adyt0);
            Two_One_Product(
                cdxt_adyt1, cdxt_adyt0, bdheight, u3, u[2], u[1], u[0]);
            u[3] = u3;
            finlength =
                fast_expansion_sum_zeroelimTRI(finlength, finnow, 4, u, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (bdheighttail != 0.0)
            {
                Two_One_Product(
                    cdxt_adyt1, cdxt_adyt0, bdheighttail, u3, u[2], u[1], u[0]);
                u[3]      = u3;
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, 4, u, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
        }
        if (bdytail != 0.0)
        {
            negate = -cdxtail;
            Two_Product(negate, bdytail, cdxt_bdyt1, cdxt_bdyt0);
            Two_One_Product(
                cdxt_bdyt1, cdxt_bdyt0, adheight, u3, u[2], u[1], u[0]);
            u[3] = u3;
            finlength =
                fast_expansion_sum_zeroelimTRI(finlength, finnow, 4, u, finother);
            finswap  = finnow;
            finnow   = finother;
            finother = finswap;
            if (adheighttail != 0.0)
            {
                Two_One_Product(
                    cdxt_bdyt1, cdxt_bdyt0, adheighttail, u3, u[2], u[1], u[0]);
                u[3]      = u3;
                finlength = fast_expansion_sum_zeroelimTRI(
                    finlength, finnow, 4, u, finother);
                finswap  = finnow;
                finnow   = finother;
                finother = finswap;
            }
        }
    }

    if (adheighttail != 0.0)
    {
        wlength   = scale_expansion_zeroelimTRI(bctlen, bct, adheighttail, w);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, wlength, w, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (bdheighttail != 0.0)
    {
        wlength   = scale_expansion_zeroelimTRI(catlen, cat, bdheighttail, w);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, wlength, w, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }
    if (cdheighttail != 0.0)
    {
        wlength   = scale_expansion_zeroelimTRI(abtlen, abt, cdheighttail, w);
        finlength = fast_expansion_sum_zeroelimTRI(
            finlength, finnow, wlength, w, finother);
        finswap  = finnow;
        finnow   = finother;
        finother = finswap;
    }

    return finnow[finlength - 1];
}

3151
double DelaunayTriangle::orient3d(struct mesh *m,
Michael Turner's avatar
files    
Michael Turner committed
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
                struct behavior *b,
                vertex pa,
                vertex pb,
                vertex pc,
                vertex pd,
                double aheight,
                double bheight,
                double cheight,
                double dheight)
{
    double adx, bdx, cdx, ady, bdy, cdy, adheight, bdheight, cdheight;
    double bdxcdy, cdxbdy, cdxady, adxcdy, adxbdy, bdxady;
    double det;
    double permanent, errbound;

    m->orient3dcount++;

    adx      = pa[0] - pd[0];
    bdx      = pb[0] - pd[0];
    cdx      = pc[0] - pd[0];
    ady      = pa[1] - pd[1];
    bdy      = pb[1] - pd[1];
    cdy      = pc[1] - pd[1];
    adheight = aheight - dheight;
    bdheight = bheight - dheight;
    cdheight = cheight - dheight;

    bdxcdy = bdx * cdy;
    cdxbdy = cdx * bdy;

    cdxady = cdx * ady;
    adxcdy = adx * cdy;

    adxbdy = adx * bdy;
    bdxady = bdx * ady;

    det = adheight * (bdxcdy - cdxbdy) + bdheight * (cdxady - adxcdy) +
          cdheight * (adxbdy - bdxady);

    permanent = (Absolute(bdxcdy) + Absolute(cdxbdy)) * Absolute(adheight) +
                (Absolute(cdxady) + Absolute(adxcdy)) * Absolute(bdheight) +
                (Absolute(adxbdy) + Absolute(bdxady)) * Absolute(cdheight);
    errbound = o3derrboundA * permanent;
    if ((det > errbound) || (-det > errbound))
    {
        return det;
    }

    return orient3dadapt(
        pa, pb, pc, pd, aheight, bheight, cheight, dheight, permanent);
}

/*****************************************************************************/
/*                                                                           */
/*  nonregular()   Return a positive value if the point pd is incompatible   */
/*                 with the circle or plane passing through pa, pb, and pc   */
/*                 (meaning that pd is inside the circle or below the        */
/*                 plane); a negative value if it is compatible; and zero if */
/*                 the four points are cocircular/coplanar.  The points pa,  */
/*                 pb, and pc must be in counterclockwise order, or the sign */
/*                 of the result will be reversed.                           */
/*                                                                           */
/*  If the -w switch is used, the points are lifted onto the parabolic       */
/*  lifting map, then they are dropped according to their weights, then the  */
/*  3D orientation test is applied.  If the -W switch is used, the points'   */
/*  heights are already provided, so the 3D orientation test is applied      */
/*  directly.  If neither switch is used, the incircle test is applied.      */
/*                                                                           */
/*****************************************************************************/

3222
double DelaunayTriangle::nonregular(struct mesh *m,
Michael Turner's avatar
files    
Michael Turner committed
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
                  struct behavior *b,
                  vertex pa,
                  vertex pb,
                  vertex pc,
                  vertex pd)
{
    if (b->weighted == 0)
    {
        return incircle(m, b, pa, pb, pc, pd);
    }
    else if (b->weighted == 1)
    {
        return orient3d(m,
                        b,
                        pa,
                        pb,
                        pc,
                        pd,
                        pa[0] * pa[0] + pa[1] * pa[1] - pa[2],
                        pb[0] * pb[0] + pb[1] * pb[1] - pb[2],
                        pc[0] * pc[0] + pc[1] * pc[1] - pc[2],
                        pd[0] * pd[0] + pd[1] * pd[1] - pd[2]);
    }
    else
    {
        return orient3d(m, b, pa, pb, pc, pd, pa[2], pb[2], pc[2], pd[2]);
    }
}

/*****************************************************************************/
/*                                                                           */
/*  findcircumcenter()   Find the circumcenter of a triangle.                */
/*                                                                           */
/*  The result is returned both in terms of x-y coordinates and xi-eta       */
/*  (barycentric) coordinates.  The xi-eta coordinate system is defined in   */
/*  terms of the triangle:  the origin of the triangle is the origin of the  */
/*  coordinate system; the destination of the triangle is one unit along the */
/*  xi axis; and the apex of the triangle is one unit along the eta axis.    */
/*  This procedure also returns the square of the length of the triangle's   */
/*  shortest edge.                                                           */
/*                                                                           */
/*****************************************************************************/

3266
void DelaunayTriangle::findcircumcenter(struct mesh *m,
Michael Turner's avatar
files    
Michael Turner committed
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
                      struct behavior *b,
                      vertex torg,
                      vertex tdest,
                      vertex tapex,
                      vertex circumcenter,
                      double *xi,
                      double *eta,
                      int offcenter)
{
    double xdo, ydo, xao, yao;
    double dodist, aodist, dadist;
    double denominator;
    double dx, dy, dxoff, dyoff;

    m->circumcentercount++;

    /* Compute the circumcenter of the triangle. */
    xdo    = tdest[0] - torg[0];
    ydo    = tdest[1] - torg[1];
    xao    = tapex[0] - torg[0];
    yao    = tapex[1] - torg[1];
    dodist = xdo * xdo + ydo * ydo;
    aodist = xao * xao + yao * yao;
    dadist = (tdest[0] - tapex[0]) * (tdest[0] - tapex[0]) +
             (tdest[1] - tapex[1]) * (tdest[1] - tapex[1]);