voronoi_2d.c 55.9 KB
Newer Older
Rainer Weinberger's avatar
Rainer Weinberger 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
/*!
 * \copyright   This file is part of the public version of the AREPO code.
 * \copyright   Copyright (C) 2009-2019, Max-Planck Institute for Astrophysics
 * \copyright   Developed by Volker Springel (vspringel@MPA-Garching.MPG.DE) and
 *              contributing authors.
 * \copyright   Arepo is free software: you can redistribute it and/or modify
 *              it under the terms of the GNU General Public License as published by
 *              the Free Software Foundation, either version 3 of the License, or
 *              (at your option) any later version.
 *
 *              Arepo is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU General Public License for more details.
 *
 *              A copy of the GNU General Public License is available under
 *              LICENSE as part of this program.  See also
 *              <https://www.gnu.org/licenses/>.
 *
 * \file        src/mesh/voronoi/voronoi_2d.c
 * \date        05/2018
 * \brief       Routines to build a 2d Voronoi mesh.
 * \details     Note that some of these routines have the same name as the ones
 *              in voronoi_1d.c and voronoi_3d.c and just replace them in case
 *              the Config-option TWODIMS is active. This is also the reason
 *              why some of these functions are empty but nonetheless have to
 *              exist in this file.
 *              contains functions:
 *                void initialize_and_create_first_tetra(tessellation * T)
 *                int insert_point(tessellation * T, int pp, int ttstart)
 *                void make_a_2_to_4_flip(tessellation * T, int pp, int tt0,
 *                  int tt1, int tt2, int tt3, int i0, int j0)
 *                void make_a_1_to_3_flip(tessellation * T, int pp, int tt0,
 *                  int tt1, int tt2)
 *                void check_edge_and_flip_if_needed(tessellation * T, int ip,
 *                  int it)
 *                int get_triangle(tessellation * T, int pp, int *moves, int
 *                  *degenerate_flag, int ttstart)
 *                static inline void add_row_2d(double *m, int r1, int r2,
 *                  double fac)
 *                int solve_linear_equations_2d(double *m, double *res)
 *                int FindTriangle(tessellation * T, int tt, int pp,
 *                  int *degnerate_flag, int *nexttetra)
 *                int InCircle_Quick(tessellation * T, int pp0, int pp1,
 *                  int pp2, int pp)
 *                int InCircle_Errorbound(tessellation * T, int pp0, int pp1,
 *                  int pp2, int pp)
 *                int InCircle_Exact(tessellation * T, int pp0, int pp1,
 *                  int pp2, int pp)
 *                double test_triangle_orientation(tessellation * T, int pp0,
 *                  int pp1, int pp2)
 *                int Orient2d_Quick(tessellation * T, int pp0, int pp1,
 *                  int pp2)
 *                int Orient2d_Exact(tessellation * T, int pp0, int pp1,
 *                  int pp2)
 *                void process_edge_faces_and_volumes(tessellation * T, int tt,
 *                  int nr)
 *                int derefine_refine_get_triangles(tessellation * T, int tt,
 *                  int nr, point * dtip, triangle * trilist, int ntri,
 *                  int max_n_tri)
 *                int derefine_add_point_and_split_tri(int q, triangle
 *                  * trilist, int ntri, int max_ntri, double vol)
 *                double get_tri_volume(int i, triangle * trilist)
 *                void derefine_refine_process_edge(tessellation * T, double
 *                  *vol, int tt, int nr)
 *                void compute_circumcircles(tessellation * T)
 *                void update_circumcircle(tessellation * T, int tt)
 *                void set_integers_for_pointer(point * p)
 *                void write_voronoi_mesh(tessellation * T, char *fname, int
 *                  writeTask, int lastTask)
71
 *
Rainer Weinberger's avatar
Rainer Weinberger committed
72
 * \par Major modifications and contributions:
73
 *
Rainer Weinberger's avatar
Rainer Weinberger committed
74 75 76 77
 * - DD.MM.YYYY Description
 * - 21.05.2018 Prepared file for public release -- Rainer Weinberger
 */

78 79
#include <gmp.h>
#include <math.h>
Rainer Weinberger's avatar
Rainer Weinberger committed
80 81 82 83 84 85
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../../main/allvars.h"
#include "../../main/proto.h"
86

Rainer Weinberger's avatar
Rainer Weinberger committed
87 88
#include "voronoi.h"

89
#if defined(TWODIMS) && !defined(ONEDIMS) /* will only be compiled in 2D case */
Rainer Weinberger's avatar
Rainer Weinberger committed
90 91 92 93 94 95 96 97 98 99 100

#define INSIDE_EPS 1.0e-8
#define GAUSS_EPS 1.0e-8

/*! \brief Initializes 2d tessellation and create all-enclosing triangle.
 *
 *  \param[out] T Pointer to tessellation structure which is set and its arrays
 *              are allocated in this routine.
 *
 *  \return void
 */
101
void initialize_and_create_first_tetra(tessellation *T)
Rainer Weinberger's avatar
Rainer Weinberger committed
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
{
  point *p;
  int i, n;

  T->MaxNdp = T->Indi.AllocFacNdp;
  T->MaxNdt = T->Indi.AllocFacNdt;
  T->MaxNvf = T->Indi.AllocFacNvf;

  T->Ndp = 0;
  T->Nvf = 0;
  T->Ndt = 0;

  T->VF = mymalloc_movable(&T->VF, "VF", T->MaxNvf * sizeof(face));

  T->DP = mymalloc_movable(&T->DP, "DP", (T->MaxNdp + 5) * sizeof(point));
  T->DP += 5;

  T->DT = mymalloc_movable(&T->DT, "DT", T->MaxNdt * sizeof(tetra));

  /* construct all encompassing huge triangle */
  double box, tetra_incircle, tetra_sidelength, tetra_height;

  box = boxSize_X;
  if(box < boxSize_Y)
    box = boxSize_Y;

  box *= 1.05;

130 131 132
  tetra_incircle = 2.001 * (1 + sqrt(3)) / 3.0 * box; /* to give room for ghost particles needed for periodic/reflective
                                                         boundary conditions, the incircle is twice as large, i.e.
                                                         [-0.5*box, 1.5*box,-0.5*box, 1.5*box] should be inside triangle */
Rainer Weinberger's avatar
Rainer Weinberger committed
133
  tetra_sidelength = tetra_incircle * sqrt(12);
134
  tetra_height     = sqrt(3.0) / 2 * tetra_sidelength;
Rainer Weinberger's avatar
Rainer Weinberger committed
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

  if(ThisTask == 0)
    printf("side-length of enclosing triangle=%g tetra_height=%g box=%g\n", tetra_sidelength, tetra_height, box);

  point *DP = T->DP;
  tetra *DT = T->DT;

  /* first, let's make the points */
  DP[-3].x = 0.5 * tetra_sidelength;
  DP[-3].y = -1.0 / 3 * tetra_height;
  DP[-3].z = 0;

  DP[-2].x = 0;
  DP[-2].y = 2.0 / 3 * tetra_height;
  DP[-2].z = 0;

  DP[-1].x = -0.5 * tetra_sidelength;
  DP[-1].y = -1.0 / 3 * tetra_height;
  DP[-1].z = 0;

  for(i = -3; i <= -1; i++)
    {
      DP[i].x += 0.5 * box;
      DP[i].y += 1.0 / 3 * tetra_height - 0.5 * box;
    }

  for(i = -3, p = &DP[-3]; i < 0; i++, p++)
    {
163 164
      p->index   = -1;
      p->task    = ThisTask;
Rainer Weinberger's avatar
Rainer Weinberger committed
165 166 167 168 169 170
      p->timebin = 0;
    }

  /* we also define a neutral element at infinity */
  DPinfinity = -4;

171 172 173 174 175
  DP[DPinfinity].x       = MAX_DOUBLE_NUMBER;
  DP[DPinfinity].y       = MAX_DOUBLE_NUMBER;
  DP[DPinfinity].z       = MAX_DOUBLE_NUMBER;
  DP[DPinfinity].index   = -1;
  DP[DPinfinity].task    = ThisTask;
Rainer Weinberger's avatar
Rainer Weinberger committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189
  DP[DPinfinity].timebin = 0;

  /* now let's make the big triangle */
  DT[0].p[0] = -3;
  DT[0].p[1] = -2;
  DT[0].p[2] = -1;

  /* On the outer faces, we attach tetrahedra with the neutral element as tip.
   * This way we will be able to navigate nicely within the tesselation,
   * and all tetrahedra have defined neighbouring tetrahedra.
   */

  for(i = 0; i < 3; i++)
    {
190
      n = i + 1; /* tetra index */
Rainer Weinberger's avatar
Rainer Weinberger committed
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

      DT[0].t[i] = n;
      DT[0].s[i] = 2;

      DT[n].t[2] = 0;
      DT[n].s[2] = i;
      DT[n].p[2] = DPinfinity;
    }

  DT[1].p[0] = DT[0].p[2];
  DT[1].p[1] = DT[0].p[1];

  DT[2].p[0] = DT[0].p[0];
  DT[2].p[1] = DT[0].p[2];

  DT[3].p[0] = DT[0].p[1];
  DT[3].p[1] = DT[0].p[0];

  DT[1].t[0] = 3;
  DT[3].t[1] = 1;
  DT[1].s[0] = 1;
  DT[3].s[1] = 0;

  DT[1].t[1] = 2;
  DT[2].t[0] = 1;
  DT[1].s[1] = 0;
  DT[2].s[0] = 1;

  DT[2].t[1] = 3;
  DT[3].t[0] = 2;
  DT[2].s[1] = 0;
  DT[3].s[0] = 1;

224
  T->Ndt = 4; /* we'll start out with 4 triangles */
Rainer Weinberger's avatar
Rainer Weinberger committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

  CentralOffsetX = 0.5 * box - 0.5000001 * tetra_sidelength;
  CentralOffsetY = -0.5000001 * box;

  ConversionFac = 1.0 / (1.001 * tetra_sidelength);

  for(i = -3; i < 0; i++)
    set_integers_for_point(T, i);
}

/*! \brief Insert a point into mesh.
 *
 *  Finds the triangle that contains this point, splits the triangle (usually
 *  into three). After this, flip the edges if needed restore
 *  Delaunayhood (which is applied recursively) until a valid Delaunay mesh
 *  is restored.
 *
 *  \param[in, out] T Pointer to tessellation.
 *  \param[in] pp Index of Delaunay point in DP array.
 *  \param[in] ttstart Initial guess in which triangle it might be,
 *             index in DT array.
 *
 * \return Index of triangle containing point pp.
 */
249
int insert_point(tessellation *T, int pp, int ttstart)
Rainer Weinberger's avatar
Rainer Weinberger committed
250 251 252 253 254 255 256 257 258
{
  int tt0, tt1, tt2, tt3, ttetra_with_p;
  int moves, degenerate_flag;

  /* first, need to do a point location */
  tt0 = get_triangle(T, pp, &moves, &degenerate_flag, ttstart);

  ttetra_with_p = tt0;

259
  if(degenerate_flag == 1) /* that's the normal split of a triangle into 3 */
Rainer Weinberger's avatar
Rainer Weinberger committed
260 261 262 263 264 265 266 267 268 269 270 271
    {
      /* we now need to split this triangle into three  */
      tt1 = T->Ndt++;
      tt2 = T->Ndt++;

      if(T->Ndt > T->MaxNdt)
        {
          T->Indi.AllocFacNdt *= ALLOC_INCREASE_FACTOR;
          T->MaxNdt = T->Indi.AllocFacNdt;
#ifdef VERBOSE
          printf("Task=%d: increase memory allocation, MaxNdt=%d Indi.AllocFacNdt=%g\n", ThisTask, T->MaxNdt, T->Indi.AllocFacNdt);
#endif /* #ifdef VERBOSE */
272
          T->DT  = myrealloc_movable(T->DT, T->MaxNdt * sizeof(tetra));
Rainer Weinberger's avatar
Rainer Weinberger committed
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
          T->DTC = myrealloc_movable(T->DTC, T->MaxNdt * sizeof(tetra_center));
          T->DTF = myrealloc_movable(T->DTF, T->MaxNdt * sizeof(char));

          if(T->Ndt > T->MaxNdt)
            terminate("Ndt > MaxNdt");
        }

      T->DT[tt1] = T->DT[tt0];
      T->DT[tt2] = T->DT[tt0];

      make_a_1_to_3_flip(T, pp, tt0, tt1, tt2);

      T->DTF[tt0] = 0;
      T->DTF[tt1] = 0;
      T->DTF[tt2] = 0;

      check_edge_and_flip_if_needed(T, pp, tt0);
      check_edge_and_flip_if_needed(T, pp, tt1);
      check_edge_and_flip_if_needed(T, pp, tt2);
    }
  else
    {
      degenerate_flag -= 10;

      tt1 = T->DT[tt0].t[degenerate_flag];

      /* we now need to split this into two triangles */
      tt2 = T->Ndt++;
      tt3 = T->Ndt++;

      if(T->Ndt > T->MaxNdt)
        {
          T->Indi.AllocFacNdt *= ALLOC_INCREASE_FACTOR;
          T->MaxNdt = T->Indi.AllocFacNdt;
#ifdef VERBOSE
          printf("Task=%d: increase memory allocation, MaxNdt=%d Indi.AllocFacNdt=%g\n", ThisTask, T->MaxNdt, T->Indi.AllocFacNdt);
#endif /* #ifdef VERBOSE */
310
          T->DT  = myrealloc_movable(T->DT, T->MaxNdt * sizeof(tetra));
Rainer Weinberger's avatar
Rainer Weinberger committed
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
          T->DTC = myrealloc_movable(T->DTC, T->MaxNdt * sizeof(tetra_center));
          T->DTF = myrealloc_movable(T->DTF, T->MaxNdt * sizeof(char));

          if(T->Ndt > T->MaxNdt)
            terminate("Ndt > MaxNdt");
        }

      T->DT[tt2] = T->DT[tt0];
      T->DT[tt3] = T->DT[tt1];

      make_a_2_to_4_flip(T, pp, tt0, tt1, tt2, tt3, degenerate_flag, T->DT[tt0].s[degenerate_flag]);

      T->DTF[tt0] = 0;
      T->DTF[tt1] = 0;
      T->DTF[tt2] = 0;
      T->DTF[tt3] = 0;

      check_edge_and_flip_if_needed(T, pp, tt0);
      check_edge_and_flip_if_needed(T, pp, tt1);
      check_edge_and_flip_if_needed(T, pp, tt2);
      check_edge_and_flip_if_needed(T, pp, tt3);
    }

  return ttetra_with_p;
}

/*! \brief Make a 2 to 4 flip needed if point is on edge of a Delaunay
 *         triangle.
 *
 *  If a new point is at the edge of a Delaunay triangle, both adjacent
 *  triangles need to be split into two. See Springel (2010) for a
 *  detailed discussion.
 *
 *  \param[in, out] T Pointer to tessellation.
 *  \param[in] pp Index of Delaunay point in DP array.
 *  \param[in] tt0 Index of point 0 in DT array.
 *  \param[in] tt1 Index of point 1 in DT array.
 *  \param[in] tt2 Index of point 2 in DT array.
 *  \param[in] tt3 Index of point 3 in DT array.
 *  \param[in] i0 Index (in DT->s) of point opposite to common face that needs
 *             to be involved in flip.
 *  \param[in] j0 Second Index (in DT->s) of point opposite to common face that
 *             needs to be involved in flip.
 *
 *  \return void
 */
357
void make_a_2_to_4_flip(tessellation *T, int pp, int tt0, int tt1, int tt2, int tt3, int i0, int j0)
Rainer Weinberger's avatar
Rainer Weinberger committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 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 420 421 422 423 424 425 426 427 428
{
  tetra *DT = T->DT;
  tetra *t0 = &DT[tt0];
  tetra *t1 = &DT[tt1];
  tetra *t2 = &DT[tt2];
  tetra *t3 = &DT[tt3];

  int i1, i2, j1, j2;

  CountFlips++;
  Count_2_to_4_Flips2d++;

  i1 = i0 + 1;
  i2 = i0 + 2;
  j1 = j0 + 1;
  j2 = j0 + 2;

  if(i1 > 2)
    i1 -= 3;
  if(i2 > 2)
    i2 -= 3;

  if(j1 > 2)
    j1 -= 3;
  if(j2 > 2)
    j2 -= 3;

  t0->p[i1] = pp;
  t1->p[j2] = pp;
  t2->p[i2] = pp;
  t3->p[j1] = pp;

  t0->t[i0] = tt1;
  t1->t[j0] = tt0;
  t0->s[i0] = j0;
  t1->s[j0] = i0;

  t1->t[j1] = tt3;
  t3->t[j2] = tt1;
  t1->s[j1] = j2;
  t3->s[j2] = j1;

  t2->t[i1] = tt0;
  t0->t[i2] = tt2;
  t2->s[i1] = i2;
  t0->s[i2] = i1;

  t2->t[i0] = tt3;
  t3->t[j0] = tt2;
  t2->s[i0] = j0;
  t3->s[j0] = i0;

  DT[t0->t[i1]].t[t0->s[i1]] = tt0;
  DT[t1->t[j2]].t[t1->s[j2]] = tt1;
  DT[t2->t[i2]].t[t2->s[i2]] = tt2;
  DT[t3->t[j1]].t[t3->s[j1]] = tt3;
}

/*! \brief Makes a 1 to 3 flip needed if point is in a Delaunay triangle.
 *
 *  If a new point is in a Delaunay triangle, this
 *  triangles need to be split into three.
 *
 *  \param[in, out] T Pointer to tessellation.
 *  \param[in] pp Index of Delaunay point in DP array.
 *  \param[in] tt0 Index of point 0 in DT array.
 *  \param[in] tt1 Index of point 1 in DT array.
 *  \param[in] tt2 Index of point 2 in DT array.
 *
 *  \return void
 */
429
void make_a_1_to_3_flip(tessellation *T, int pp, int tt0, int tt1, int tt2)
Rainer Weinberger's avatar
Rainer Weinberger committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
{
  tetra *DT = T->DT;
  tetra *t0 = &DT[tt0];
  tetra *t1 = &DT[tt1];
  tetra *t2 = &DT[tt2];

  CountFlips++;
  Count_1_to_3_Flips2d++;

  t0->p[0] = pp;
  t1->p[1] = pp;
  t2->p[2] = pp;

  t0->t[1] = tt1;
  t1->t[0] = tt0;
  t0->s[1] = 0;
  t1->s[0] = 1;

  t1->t[2] = tt2;
  t2->t[1] = tt1;
  t1->s[2] = 1;
  t2->s[1] = 2;

  t2->t[0] = tt0;
  t0->t[2] = tt2;
  t2->s[0] = 2;
  t0->s[2] = 0;

  DT[t0->t[0]].t[t0->s[0]] = tt0;
  DT[t1->t[1]].t[t1->s[1]] = tt1;
  DT[t2->t[2]].t[t2->s[2]] = tt2;
}

/*! \brief Flips trangle if needed.
 *
 *  See Springel (2010) for detailed discussion how mesh is constructed.
 *
 *  \param[in, out] T Pointer to tessellation.
 *  \param[in] ip Index to Delaunay point, DP array.
 *  \param[in] it Index to corner of triangle, DT array.
 *
 *  \return void
 */
473
void check_edge_and_flip_if_needed(tessellation *T, int ip, int it)
Rainer Weinberger's avatar
Rainer Weinberger committed
474 475 476 477 478 479 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
{
  tetra *DT = T->DT;
  char *DTF = T->DTF;

  tetra *t = &DT[it];

  int tt, pp, t0, t2;
  int pi, pi1, pi2;
  int ni, ni1, ni2;
  int st2, st0;

  if(t->p[0] == ip)
    pi = 0;
  else if(t->p[1] == ip)
    pi = 1;
  else
    pi = 2;

  /* get the point that lies accross the edge to obtain the quadriliteral */

  tt = t->t[pi];
  ni = t->s[pi];
  pp = DT[tt].p[ni];

  int ret, ret_exact;

  ret = InCircle_Errorbound(T, t->p[0], t->p[1], t->p[2], pp);
  CountInSphereTests++;

  if(ret != 0)
    ret_exact = ret;
  else
    {
      ret_exact = InCircle_Exact(T, t->p[0], t->p[1], t->p[2], pp);
      CountInSphereTestsExact++;
    }

  if(ret_exact > 0)
    {
      /* pp lies in the triangle, the edge is not Delaunay. Need to do a flip */

      CountFlips++;

      ni1 = ni + 1;
      if(ni1 > 2)
        ni1 -= 3;
      ni2 = ni + 2;
      if(ni2 > 2)
        ni2 -= 3;

      pi1 = pi + 1;
      if(pi1 > 2)
        pi1 -= 3;
      pi2 = pi + 2;
      if(pi2 > 2)
        pi2 -= 3;

      t0 = DT[tt].t[ni1];
      t2 = t->t[pi1];

      st0 = DT[tt].s[ni1];
      st2 = t->s[pi1];

      /* change the points of the triangles */
538
      t->p[pi2]     = pp;
Rainer Weinberger's avatar
Rainer Weinberger committed
539 540 541 542 543
      DT[tt].p[ni2] = ip;

      /* change the pointers to the neighbouring triangles, and fix
         the adjency relations */

544
      t->t[pi1]     = tt;
Rainer Weinberger's avatar
Rainer Weinberger committed
545
      DT[tt].t[ni1] = it;
546
      t->s[pi1]     = ni1;
Rainer Weinberger's avatar
Rainer Weinberger committed
547 548
      DT[tt].s[ni1] = pi1;

549
      t->t[pi]      = t0;
Rainer Weinberger's avatar
Rainer Weinberger committed
550
      DT[t0].t[st0] = it;
551
      t->s[pi]      = st0;
Rainer Weinberger's avatar
Rainer Weinberger committed
552 553
      DT[t0].s[st0] = pi;

554
      DT[tt].t[ni]  = t2;
Rainer Weinberger's avatar
Rainer Weinberger committed
555
      DT[t2].t[st2] = tt;
556
      DT[tt].s[ni]  = st2;
Rainer Weinberger's avatar
Rainer Weinberger committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
      DT[t2].s[st2] = ni;

      DTF[tt] = 0;
      DTF[it] = 0;

      /* now we need to test also the two sides opposite of p */
      check_edge_and_flip_if_needed(T, ip, it);
      check_edge_and_flip_if_needed(T, ip, tt);
    }
}

/*! \brief Finds triangle in which new Delaunay point is located.
 *
 *  Starts with a suggested triangle ttstart and checks if the point is
 *  contained in this triangle. If not, the procedure is repeated for the
 *  neighboring triangle.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] pp Index of Delaunay point in DP array.
 *  \param[out] moves Number of iterations to find the correct triangle.
 *  \param[out] degenerate_flag Flag if point lies on edge of a triangle.
 *  \param[in] ttstart Starting index for the search for the correct triangle.
 *
 *  \return Index of triangle in DT array.
 */
582
int get_triangle(tessellation *T, int pp, int *moves, int *degenerate_flag, int ttstart)
Rainer Weinberger's avatar
Rainer Weinberger committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
{
  int count_moves = 0;
  int ret;
  int tt, next_tetra;

  tt = ttstart;

#define MAX_COUNT_MOVES 1000000

  while((ret = FindTriangle(T, tt, pp, degenerate_flag, &next_tetra)) == 0)
    {
      /* we need to see in which of the three possible neighbouring triangles
         we should walk. We'll choose the one which lies along the face that
         is traversed by a line from the cm of the triangle to the point in
         question.
       */
      count_moves++;

      if(count_moves > MAX_COUNT_MOVES)
        {
603
          printf("ta=%d triangle=%d  xy=(%g|%g) ID=%d\n", ThisTask, (int)(tt), T->DP[pp].x, T->DP[pp].y, T->DP[pp].ID);
Rainer Weinberger's avatar
Rainer Weinberger committed
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 639 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
          if(count_moves > MAX_COUNT_MOVES + 10)
            terminate("too many moves, problem to find triangle");
        }

      tt = next_tetra;
    }

  *moves = count_moves;

  return tt;
}

/*! \brief Add row in matrix equation.
 *
 *  Auxiliary function for solve_linear_equations_2d.
 *
 *  \param[in, out] m Matrix.
 *  \param[in] r1 Index of row to be modified.
 *  \param[in] r2 Index of row which is added to r1.
 *  \param[in] fac Factor by which row r2 is multiplied before adding to r1.
 *
 *  \return void
 */
static inline void add_row_2d(double *m, int r1, int r2, double fac)
{
  int i;

  for(i = 0; i < 3; i++)
    m[r1 * 3 + i] += fac * m[r2 * 3 + i];
}

/*! \brief Solve system of linear equations for 2d Voronoi construction.
 *
 *  This is needed in get_triangle routine.
 *
 *  \param[in, out] m Matrix.
 *  \param[in, out] res Array for result.
 *
 *  \return 0 if success, -1 else.
 */
int solve_linear_equations_2d(double *m, double *res)
{
  int ix, iy;

  if(fabs(m[0]) > fabs(m[3]))
    {
      ix = 0;
      iy = 1;
    }
  else
    {
      ix = 1;
      iy = 0;
    }

  add_row_2d(m, iy, ix, -m[iy * 3] / m[ix * 3]);

  res[1] = m[iy * 3 + 2] / m[iy * 3 + 1];
  res[0] = (m[ix * 3 + 2] - res[1] * m[ix * 3 + 1]) / m[ix * 3];

  if(fabs(m[ix * 3]) < 1.0e-12)
    return -1;

  return 0;
}

/*! \brief Does point lie in triangle?
 *
 *  Tests whether point pp lies in the triangle, on an edge, or outside. In the
 *  latter case, a neighboring triangle is returned. First, a fast search is
 *  performed and if this yields that point might be on an edge, a (more
 *  expensive) exact determination is performed.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] tt Index of triangle in DT array.
 *  \param[in] pp Index of Delaunay point in DP array.
 *  \param[out] degenerate_flag Flag if point lies on edge of a triangle.
 *  \param[out] nexttetra Index of neighboring triangle in direction of point.
 *
 *  \return 1: point inside triangle; 0 outside; 10,11,12: on edge.
 */
685
int FindTriangle(tessellation *T, int tt, int pp, int *degnerate_flag, int *nexttetra)
Rainer Weinberger's avatar
Rainer Weinberger committed
686 687 688
{
  tetra *DT = T->DT;
  point *DP = T->DP;
689 690
  tetra *t  = &DT[tt];
  point *p  = &DP[pp];
Rainer Weinberger's avatar
Rainer Weinberger committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705

  int pp0, pp1, pp2;
  point *p0, *p1, *p2;

  pp0 = t->p[0];
  pp1 = t->p[1];
  pp2 = t->p[2];

  p0 = &DP[pp0];
  p1 = &DP[pp1];
  p2 = &DP[pp2];

  if(pp0 == DPinfinity || pp1 == DPinfinity || pp2 == DPinfinity)
    {
      char buf[1000];
706
      sprintf(buf, "we are in a triangle with an infinity point. tetra=%d  p=(%g|%g)\n", (int)(tt), p->x, p->y);
Rainer Weinberger's avatar
Rainer Weinberger committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720
      terminate(buf);
    }

  Count_InTetra++;

  double ax = p1->xx - p0->xx;
  double ay = p1->yy - p0->yy;

  double bx = p2->xx - p0->xx;
  double by = p2->yy - p0->yy;

  double qx = p->xx - p0->xx;
  double qy = p->yy - p0->yy;

721
  double mv_data[] = {ax, bx, qx, ay, by, qy};
Rainer Weinberger's avatar
Rainer Weinberger committed
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 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 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 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
  double x[2];

  int ivol, flag2, flag1, flag0;
  int count_zeros = 0;

  int status;

  status = solve_linear_equations_2d(mv_data, x);

  if(status < 0)
    {
      ivol = Orient2d_Exact(T, t->p[0], t->p[1], t->p[2]);
      if(ivol <= 0)
        {
          char buf[1000];
          sprintf(buf, "flat or negatively triangle found (ivol=%d)\n", ivol);
          terminate(buf);
        }
    }

  if(status >= 0)
    {
      if(x[0] > INSIDE_EPS && x[1] > INSIDE_EPS && (1 - (x[0] + x[1])) > INSIDE_EPS)
        {
          /* looks like we are safely inside the triangle */

          *degnerate_flag = 1;
          return 1;
        }

      if(x[0] < -INSIDE_EPS || x[1] < -INSIDE_EPS || (1 - (x[0] + x[1])) < -INSIDE_EPS)
        {
          /* looks like we are clearly outside the triangle.
             Let's look for a good neighbouring triangle to continue the search */

          /* note: in the (a,b) basis, the center-of-mass has coordinates (1/3, 1/3) */

          double w, u;

          if(fabs(x[1] - (1.0 / 3)) > INSIDE_EPS)
            {
              w = (1.0 / 3) / ((1.0 / 3) - x[1]);
              if(w > 0)
                {
                  u = (1.0 / 3) + w * (x[0] - (1.0 / 3));
                  if(u > -INSIDE_EPS && (1 - u) > -INSIDE_EPS)
                    {
                      *nexttetra = t->t[2];
                      return 0;
                    }
                }
            }

          if(fabs(x[0] - (1.0 / 3)) > INSIDE_EPS)
            {
              w = (1.0 / 3) / ((1.0 / 3) - x[0]);
              if(w > 0)
                {
                  u = (1.0 / 3) + w * (x[1] - (1.0 / 3));
                  if(u > -INSIDE_EPS && (1 - u) > -INSIDE_EPS)
                    {
                      *nexttetra = t->t[1];
                      return 0;
                    }
                }
            }

          *nexttetra = t->t[0];
          return 0;
        }
    }

  /* here we need to decide whether we have a degenerate case, i.e.
     whether we think the point lies on an edge of the triangle */

  Count_InTetraExact++;

  ivol = Orient2d_Exact(T, t->p[0], t->p[1], t->p[2]);

  if(ivol <= 0)
    {
      char buf[1000];
      sprintf(buf, "flat or negatively oriented triangle found (ivol=%d)\n", ivol);
      terminate(buf);
    }

  flag0 = Orient2d_Exact(T, pp1, pp2, pp);
  flag1 = Orient2d_Exact(T, pp2, pp0, pp);
  flag2 = Orient2d_Exact(T, pp0, pp1, pp);

  if(flag0 == 0)
    count_zeros++;

  if(flag1 == 0)
    count_zeros++;

  if(flag2 == 0)
    count_zeros++;

  if(count_zeros >= 2)
    {
      printf("flags=%d %d %d\n", flag0, flag1, flag2);

825
      printf("points: %d %d %d %d\n", (int)(pp0), (int)(pp1), (int)(pp2), (int)(pp));
Rainer Weinberger's avatar
Rainer Weinberger committed
826
      printf("Ngas=%d\n", NumGas);
827 828 829 830 831 832 833
      printf("xyz, p=%d: (%g|%g)  index=%d task=%d ID=%d  flags\n", (int)(pp0), p0->x, p0->y, p0->index, p0->task,
             P[p0->index % NumGas].ID);
      printf("xyz, p=%d: (%g|%g)  index=%d task=%d ID=%d  flags\n", (int)(pp1), p1->x, p1->y, p1->index, p1->task,
             P[p1->index % NumGas].ID);
      printf("xyz, p=%d: (%g|%g)  index=%d task=%d ID=%d  flags\n", (int)(pp2), p2->x, p2->y, p2->index, p2->task,
             P[p2->index % NumGas].ID);
      printf("xyz, p=%d: (%g|%g)  index=%d task=%d ID=%d  flags\n", (int)(pp), p->x, p->y, p->index, p->task, P[p->index % NumGas].ID);
Rainer Weinberger's avatar
Rainer Weinberger committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847
      terminate("too many zeros - (perhaps identical points inserted?)");
    }

  if(flag0 >= 0 && flag1 >= 0 && flag2 >= 0)
    {
      /* we have a point inside the triangle, but it may still be on one of the edges */

      if(count_zeros == 0)
        {
          /* ok, we are inside */
          *degnerate_flag = 1;
          return 1;
        }

848
      if(count_zeros == 1) /* we lie on a face */
Rainer Weinberger's avatar
Rainer Weinberger committed
849 850 851 852
        {
          if(flag2 == 0)
            {
              *degnerate_flag = 12;
853
              return 12; /* point lies on side A */
Rainer Weinberger's avatar
Rainer Weinberger committed
854 855 856 857
            }
          if(flag1 == 0)
            {
              *degnerate_flag = 11;
858
              return 11; /* point lies on side C */
Rainer Weinberger's avatar
Rainer Weinberger committed
859 860 861 862 863
            }

          if(flag0 == 0)
            {
              *degnerate_flag = 10;
864
              return 10; /* point lies on side B */
Rainer Weinberger's avatar
Rainer Weinberger committed
865 866 867 868 869 870 871 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
            }
        }
    }

  /* we are clearly outside, let's select the suitable neighbour */

  if(flag0 < 0 && flag1 >= 0 && flag2 >= 0)
    {
      *nexttetra = t->t[0];
      return 0;
    }

  if(flag0 >= 0 && flag1 < 0 && flag2 >= 0)
    {
      *nexttetra = t->t[1];
      return 0;
    }

  if(flag0 >= 0 && flag1 >= 0 && flag2 < 0)
    {
      *nexttetra = t->t[2];
      return 0;
    }

  /* there are apparently two negative values. Let's pick a random one */

  int ind = -1;

  if(flag0 < 0)
    {
      if(ind < 0)
        ind = 0;
      else
        {
          if(get_random_number() < 0.5)
            ind = 0;
        }
    }

  if(flag1 < 0)
    {
      if(ind < 0)
        ind = 0;
      else
        {
          if(get_random_number() < 0.5)
            ind = 0;
        }
    }

  if(flag2 < 0)
    {
      if(ind < 0)
        ind = 0;
      else
        {
          if(get_random_number() < 0.5)
            ind = 0;
        }
    }

  *nexttetra = t->t[ind];
  return 0;
}

/*! \brief Tests whether point pp lies in the circumcircle around triangle
 *        p0,p1,p2.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] pp0 Index in DP of first point in triangle.
 *  \param[in] pp1 Index in DP of second point in triangle.
 *  \param[in] pp2 Index in DP of third point in triangle.
 *  \param[in] pp Index in DP of point to be checked.
 *
 *  \return (-1,0,1); -1: in circle; 0 on circle, 1: outside circle.
 */
941
int InCircle_Quick(tessellation *T, int pp0, int pp1, int pp2, int pp)
Rainer Weinberger's avatar
Rainer Weinberger committed
942 943 944 945 946
{
  point *DP = T->DP;
  point *p0 = &DP[pp0];
  point *p1 = &DP[pp1];
  point *p2 = &DP[pp2];
947
  point *p  = &DP[pp];
Rainer Weinberger's avatar
Rainer Weinberger committed
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 986 987 988 989 990 991 992 993 994

  double ax, ay, bx, by, cx, cy;
  double ab, bc, ca, a2, b2, c2, x;

  if(pp0 == DPinfinity || pp1 == DPinfinity || pp2 == DPinfinity || pp == DPinfinity)
    return -1;

  ax = p0->xx - p->xx;
  ay = p0->yy - p->yy;
  bx = p1->xx - p->xx;
  by = p1->yy - p->yy;
  cx = p2->xx - p->xx;
  cy = p2->yy - p->yy;

  ab = ax * by - bx * ay;
  bc = bx * cy - cx * by;
  ca = cx * ay - ax * cy;

  a2 = ax * ax + ay * ay;
  b2 = bx * bx + by * by;
  c2 = cx * cx + cy * cy;

  x = a2 * bc + b2 * ca + c2 * ab;

  if(x < 0)
    return -1;
  if(x > 0)
    return +1;

  return 0;
}

/*! \brief Tests whether point pp lies in the circumcircle around triangle
 *        p0,p1,p2 with some error margin.
 *
 *  This error margin should be large enough to exclude that close cases are
 *  misclssified due to numerical round-off errors.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] pp0 Index in DP of first point in triangle.
 *  \param[in] pp1 Index in DP of second point in triangle.
 *  \param[in] pp2 Index in DP of third point in triangle.
 *  \param[in] pp Index in DP of point to be checked.
 *
 *  \return (-1,0,1); -1: in circle; 0 on circle (within tolerance),
 *          1: outside circle.
 */
995
int InCircle_Errorbound(tessellation *T, int pp0, int pp1, int pp2, int pp)
Rainer Weinberger's avatar
Rainer Weinberger committed
996 997 998 999 1000
{
  point *DP = T->DP;
  point *p0 = &DP[pp0];
  point *p1 = &DP[pp1];
  point *p2 = &DP[pp2];
1001
  point *p  = &DP[pp];
Rainer Weinberger's avatar
Rainer Weinberger committed
1002 1003 1004 1005 1006 1007 1008 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 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

  if(pp0 == DPinfinity || pp1 == DPinfinity || pp2 == DPinfinity || pp == DPinfinity)
    return -1;

  double ax, ay, bx, by, cx, cy;
  double ab, bc, ca, a2, b2, c2, x;
  double axby, bxay, bxcy, cxby, cxay, axcy;

  ax = p0->xx - p->xx;
  ay = p0->yy - p->yy;
  bx = p1->xx - p->xx;
  by = p1->yy - p->yy;
  cx = p2->xx - p->xx;
  cy = p2->yy - p->yy;

  axby = ax * by;
  bxay = bx * ay;
  bxcy = bx * cy;
  cxby = cx * by;
  cxay = cx * ay;
  axcy = ax * cy;

  ca = cxay - axcy;
  ab = axby - bxay;
  bc = bxcy - cxby;

  a2 = ax * ax + ay * ay;
  b2 = bx * bx + by * by;
  c2 = cx * cx + cy * cy;

  x = a2 * bc + b2 * ca + c2 * ab;

  /* calculate absolute maximum size */

  double sizelimit = a2 * (fabs(bxcy) + fabs(cxby)) + b2 * (fabs(cxay) + fabs(axcy)) + c2 * (fabs(axby) + fabs(bxay));

  double errbound = 1.0e-14 * sizelimit;

  if(x < -errbound)
    return -1;
  else if(x > errbound)
    return +1;

  return 0;
}

/*! \brief Tests whether point pp lies in the circumcircle around triangle
 *  p0,p1,p2 using arbitrary precision operations.
 *
 *  This is the exact solution, but computationally very expensive, thus only
 *  called for the unclear cases.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] pp0 Index in DP of first point in triangle.
 *  \param[in] pp1 Index in DP of second point in triangle.
 *  \param[in] pp2 Index in DP of third point in triangle.
 *  \param[in] pp Index in DP of point to be checked.
 *
 *  \return (-1,0,1); -1: in circle; 0 on circle,
 *          1: outside circle.
 */
1063
int InCircle_Exact(tessellation *T, int pp0, int pp1, int pp2, int pp)
Rainer Weinberger's avatar
Rainer Weinberger committed
1064 1065 1066 1067 1068
{
  point *DP = T->DP;
  point *p0 = &DP[pp0];
  point *p1 = &DP[pp1];
  point *p2 = &DP[pp2];
1069
  point *p  = &DP[pp];
Rainer Weinberger's avatar
Rainer Weinberger committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

  if(pp0 == DPinfinity || pp1 == DPinfinity || pp2 == DPinfinity || pp == DPinfinity)
    return -1;

  IntegerMapType ax, ay, bx, by, cx, cy;

  ax = p0->ix - p->ix;
  ay = p0->iy - p->iy;
  bx = p1->ix - p->ix;
  by = p1->iy - p->iy;
  cx = p2->ix - p->ix;
  cy = p2->iy - p->iy;

  mpz_t axby, bxay, bxcy, cxby, cxay, axcy, tmp;

  mpz_init(tmp);

  mpz_init(axby);
  MY_mpz_set_si(tmp, ax);
  MY_mpz_mul_si(axby, tmp, by);
  mpz_init(bxay);
  MY_mpz_set_si(tmp, bx);
  MY_mpz_mul_si(bxay, tmp, ay);
  mpz_init(bxcy);
  MY_mpz_set_si(tmp, bx);
  MY_mpz_mul_si(bxcy, tmp, cy);
  mpz_init(cxby);
  MY_mpz_set_si(tmp, cx);
  MY_mpz_mul_si(cxby, tmp, by);
  mpz_init(cxay);
  MY_mpz_set_si(tmp, cx);
  MY_mpz_mul_si(cxay, tmp, ay);
  mpz_init(axcy);
  MY_mpz_set_si(tmp, ax);
  MY_mpz_mul_si(axcy, tmp, cy);

  mpz_t ca, ab, bc;

  mpz_init(ca);
  mpz_init(ab);
  mpz_init(bc);

  mpz_sub(ca, cxay, axcy);
  mpz_sub(ab, axby, bxay);
  mpz_sub(bc, bxcy, cxby);

  mpz_t AA, BB, a2, b2, c2;

  mpz_init(AA);
  mpz_init(BB);
  mpz_init(a2);
  mpz_init(b2);
  mpz_init(c2);

  MY_mpz_set_si(tmp, ax);
  MY_mpz_mul_si(AA, tmp, ax);
  MY_mpz_set_si(tmp, ay);
  MY_mpz_mul_si(BB, tmp, ay);
  mpz_add(a2, AA, BB);

  MY_mpz_set_si(tmp, bx);
  MY_mpz_mul_si(AA, tmp, bx);
  MY_mpz_set_si(tmp, by);
  MY_mpz_mul_si(BB, tmp, by);
  mpz_add(b2, AA, BB);

  MY_mpz_set_si(tmp, cx);
  MY_mpz_mul_si(AA, tmp, cx);
  MY_mpz_set_si(tmp, cy);
  MY_mpz_mul_si(BB, tmp, cy);
  mpz_add(c2, AA, BB);

  /* now calculate the final result */

  mpz_mul(AA, a2, bc);
  mpz_mul(BB, b2, ca);
  mpz_add(tmp, AA, BB);
  mpz_mul(BB, c2, ab);
  mpz_add(AA, BB, tmp);

  int sign = mpz_sgn(AA);

  mpz_clear(c2);
  mpz_clear(b2);
  mpz_clear(a2);
  mpz_clear(BB);
  mpz_clear(AA);
  mpz_clear(bc);
  mpz_clear(ab);
  mpz_clear(ca);
  mpz_clear(axcy);
  mpz_clear(cxay);
  mpz_clear(cxby);
  mpz_clear(bxcy);
  mpz_clear(bxay);
  mpz_clear(axby);
  mpz_clear(tmp);

  return sign;
}

/*! \brief Returns the orientation of the triangle.
 *
 *  Defined as the determinant of the matrix of the position of the three edge
 *  points a, b and c:
 *  | ax, ay, 1 |
 *  | bx, by, 1 |
 *  | cx, cy, 1 |
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] pp0 Index in DP of first point in triangle.
 *  \param[in] pp1 Index in DP of second point in triangle.
 *  \param[in] pp2 Index in DP of third point in triangle.
 *
 *  \return Determinant of orientation matrix.
 */
1186
double test_triangle_orientation(tessellation *T, int pp0, int pp1, int pp2)
Rainer Weinberger's avatar
Rainer Weinberger committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
{
  point *DP = T->DP;
  point *p0 = &DP[pp0];
  point *p1 = &DP[pp1];
  point *p2 = &DP[pp2];

  return (p1->x - p0->x) * (p2->y - p0->y) - (p1->y - p0->y) * (p2->x - p0->x);
}

/*! \brief Check if triangle is positively or negatively oriented.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] pp0 Index in DP of first point in triangle.
 *  \param[in] pp1 Index in DP of second point in triangle.
 *  \param[in] pp2 Index in DP of third point in triangle.
 *
 *  \return -1 if negatively, 0 if degenerate (in a line) and 1 if positively
 *          oriented.
 */
1206
int Orient2d_Quick(tessellation *T, int pp0, int pp1, int pp2)
Rainer Weinberger's avatar
Rainer Weinberger committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
{
  point *DP = T->DP;
  point *p0 = &DP[pp0];
  point *p1 = &DP[pp1];
  point *p2 = &DP[pp2];

  double x;

  x = (p1->xx - p0->xx) * (p2->yy - p0->yy) - (p1->yy - p0->yy) * (p2->xx - p0->xx);

  if(x < 0)
    return -1;
  if(x > 0)
    return +1;
  return 0;
}

/*! \brief Check if triangle is positively or negatively oriented.
 *
 *  Uses arbitrary precision operations, which is computationally expensive but
 *  garantees the correct result.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] pp0 Index in DP of first point in triangle.
 *  \param[in] pp1 Index in DP of second point in triangle.
 *  \param[in] pp2 Index in DP of third point in triangle.
 *
 *  \return -1 if negatively, 0 if degenerate (in a line) and 1 if positively
 *          oriented.
 */
1237
int Orient2d_Exact(tessellation *T, int pp0, int pp1, int pp2)
Rainer Weinberger's avatar
Rainer Weinberger committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
{
  point *DP = T->DP;
  point *p0 = &DP[pp0];
  point *p1 = &DP[pp1];
  point *p2 = &DP[pp2];

#if USEDBITS > 31
  IntegerMapType dx1, dy1, dx2, dy2;

  dx1 = (p1->ix - p0->ix);
  dy1 = (p1->iy - p0->iy);
  dx2 = (p2->ix - p0->ix);
  dy2 = (p2->iy - p0->iy);

  mpz_t dx1dy2, dx2dy1, tmp;

  mpz_init(tmp);
  mpz_init(dx1dy2);
  mpz_init(dx2dy1);

  MY_mpz_set_si(tmp, dx1);
  MY_mpz_mul_si(dx1dy2, tmp, dy2);

  MY_mpz_set_si(tmp, dx2);
  MY_mpz_mul_si(dx2dy1, tmp, dy1);

  mpz_sub(tmp, dx1dy2, dx2dy1);

  int sign = mpz_sgn(tmp);

  mpz_clear(dx2dy1);
  mpz_clear(dx1dy2);
  mpz_clear(tmp);

  return (sign);

1274
#else  /* #if USEDBITS > 31 */
Rainer Weinberger's avatar
Rainer Weinberger committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
  signed long long dx1, dy1, dx2, dy2, x;

  dx1 = (p1->ix - p0->ix);
  dy1 = (p1->iy - p0->iy);
  dx2 = (p2->ix - p0->ix);
  dy2 = (p2->iy - p0->iy);

  x = dx1 * dy2 - dy1 * dx2;

  if(x < 0)
    return -1;
  if(x > 0)
    return +1;
  return 0;
#endif /* #if USEDBITS > 31 #else */
}

1292 1293
const int edge_start[3] = {1, 2, 0};
const int edge_end[3]   = {2, 0, 1};
Rainer Weinberger's avatar
Rainer Weinberger committed
1294 1295 1296 1297 1298 1299 1300 1301 1302

/*! \brief Calculate cell volumes and face areas of mesh.
 *
 *  \param[in, out] T Pointer to tessellation.
 *  \param[in] tt Index in DT array.
 *  \param[in] nr Index in edges.
 *
 *  \return void
 */
1303
void process_edge_faces_and_volumes(tessellation *T, int tt, int nr)
Rainer Weinberger's avatar
Rainer Weinberger committed
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
{
  int i, j, qq, p1, p2, k;
  face *f;
  double nx, ny;
  double sx, sy;
  double hx, hy;
  double dvol, h;

  if(T->Nvf + 1 >= T->MaxNvf)
    {
      T->Indi.AllocFacNvf *= ALLOC_INCREASE_FACTOR;
      T->MaxNvf = T->Indi.AllocFacNvf;
#ifdef VERBOSE
      printf("Task=%d: increase memory allocation, MaxNvf=%d Indi.AllocFacNvf=%g\n", ThisTask, T->MaxNvf, T->Indi.AllocFacNvf);
#endif /* #ifdef VERBOSE */
      T->VF = myrealloc_movable(T->VF, T->MaxNvf * sizeof(face));

      if(T->Nvf + 1 >= T->MaxNvf)
        terminate("Nvf larger than MaxNvf");
    }

1325 1326 1327
  tetra *DT         = T->DT;
  point *DP         = T->DP;
  face *VF          = T->VF;
Rainer Weinberger's avatar
Rainer Weinberger committed
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
  tetra_center *DTC = T->DTC;

  tetra *t = &DT[tt];

  i = edge_start[nr];
  j = edge_end[nr];

  point *dpi = &DP[t->p[i]];
  point *dpj = &DP[t->p[j]];

  qq = t->t[nr];

  Edge_visited[tt] |= (1 << nr);
  Edge_visited[qq] |= (1 << (t->s[nr]));

  p1 = t->p[i];
  p2 = t->p[j];

  f = &VF[T->Nvf++];

  f->p1 = p1;
  f->p2 = p2;

  f->cx = 0.5 * (DTC[tt].cx + DTC[qq].cx);
  f->cy = 0.5 * (DTC[tt].cy + DTC[qq].cy);
  f->cz = 0;

#ifdef TETRA_INDEX_IN_FACE
  f->dt_index = tt;
#endif /* #ifdef TETRA_INDEX_IN_FACE */

#ifdef REFINEMENT_MERGE_CELLS
1360 1361 1362
  f->t  = tt;
  f->nr = nr; /* delaunay tetra and edge number that generated this face */
#endif        /* #ifdef REFINEMENT_MERGE_CELLS */
Rainer Weinberger's avatar
Rainer Weinberger committed
1363 1364 1365 1366 1367 1368 1369 1370 1371

  nx = DTC[tt].cx - DTC[qq].cx;
  ny = DTC[tt].cy - DTC[qq].cy;

  f->area = sqrt(nx * nx + ny * ny);

  hx = 0.5 * (dpi->x - dpj->x);
  hy = 0.5 * (dpi->y - dpj->y);

1372
  h    = sqrt(hx * hx + hy * hy);
Rainer Weinberger's avatar
Rainer Weinberger committed
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
  dvol = 0.5 * f->area * h;

#if defined(REGULARIZE_MESH_FACE_ANGLE) || defined(OUTPUT_MESH_FACE_ANGLE)
  double angle = 0.5 * f->area / h;
#endif /* #if defined(REGULARIZE_MESH_FACE_ANGLE) || defined(OUTPUT_MESH_FACE_ANGLE) */

  if(dpi->task == ThisTask && dpi->index >= 0 && dpi->index < NumGas)
    {
      if(TimeBinSynchronized[P[dpi->index].TimeBinHydro])
        {
          SphP[dpi->index].Volume += dvol;
          SphP[dpi->index].SurfaceArea += f->area;

#if defined(REGULARIZE_MESH_FACE_ANGLE) || defined(OUTPUT_MESH_FACE_ANGLE)
          if(SphP[dpi->index].MaxFaceAngle < angle)
            SphP[dpi->index].MaxFaceAngle = angle;
#endif /* #if defined(REGULARIZE_MESH_FACE_ANGLE) || defined(OUTPUT_MESH_FACE_ANGLE) */

#ifdef OUTPUT_SURFACE_AREA
          if(f->area)
            SphP[dpi->index].CountFaces++;
#endif /* #ifdef OUTPUT_SURFACE_AREA */

#if defined(REFINEMENT_SPLIT_CELLS)
          if(SphP[dpi->index].MinimumEdgeDistance > h)
            SphP[dpi->index].MinimumEdgeDistance = h;
#endif /* #if defined(REFINEMENT_SPLIT_CELLS) */
          /* let's now compute the center-of-mass of the pyramid at the bottom top */
          sx = (2.0 / 3) * f->cx + (1.0 / 3) * dpi->x;
          sy = (2.0 / 3) * f->cy + (1.0 / 3) * dpi->y;

          SphP[dpi->index].Center[0] += dvol * sx;
          SphP[dpi->index].Center[1] += dvol * sy;
        }
    }

  if(dpj->task == ThisTask && dpj->index >= 0 && dpj->index < NumGas)
    {
      if(TimeBinSynchronized[P[dpj->index].TimeBinHydro])
        {
          SphP[dpj->index].Volume += dvol;
          SphP[dpj->index].SurfaceArea += f->area;

#if defined(REGULARIZE_MESH_FACE_ANGLE) || defined(OUTPUT_MESH_FACE_ANGLE)
          if(SphP[dpj->index].MaxFaceAngle < angle)
            SphP[dpj->index].MaxFaceAngle = angle;
#endif /* #if defined(REGULARIZE_MESH_FACE_ANGLE) || defined(OUTPUT_MESH_FACE_ANGLE) */

#ifdef OUTPUT_SURFACE_AREA
          if(f->area)
            SphP[dpj->index].CountFaces++;
#endif /* #ifdef OUTPUT_SURFACE_AREA */

#if defined(REFINEMENT_SPLIT_CELLS)
          if(SphP[dpj->index].MinimumEdgeDistance > h)
            SphP[dpj->index].MinimumEdgeDistance = h;
#endif /* #if defined(REFINEMENT_SPLIT_CELLS) */

          /* let's now compute the center-of-mass of the pyramid on top */
          sx = (2.0 / 3) * f->cx + (1.0 / 3) * dpj->x;
          sy = (2.0 / 3) * f->cy + (1.0 / 3) * dpj->y;

          SphP[dpj->index].Center[0] += dvol * sx;
          SphP[dpj->index].Center[1] += dvol * sy;
        }
    }
  int low_p, high_p;

  if(DP[p1].ID < DP[p2].ID)
    {
1443
      low_p  = p1;
Rainer Weinberger's avatar
Rainer Weinberger committed
1444 1445 1446 1447
      high_p = p2;
    }
  else
    {
1448
      low_p  = p2;
Rainer Weinberger's avatar
Rainer Weinberger committed
1449 1450 1451 1452 1453
      high_p = p1;
    }

  int this_task_responsible_flag = 0;

1454
  if(TimeBinSynchronized[DP[low_p].timebin]) /* the one with the lower ID is active */
Rainer Weinberger's avatar
Rainer Weinberger committed
1455 1456 1457 1458 1459
    {
      /* we need to check whether the one with the lower ID is a local particle */
      if(DP[low_p].task == ThisTask && DP[low_p].index >= 0 && DP[low_p].index < NumGas)
        this_task_responsible_flag = 1;
    }
1460
  else if(TimeBinSynchronized[DP[high_p].timebin]) /* only the side with the higher ID is active */
Rainer Weinberger's avatar
Rainer Weinberger committed
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 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
    {
      /* we need to check whether we hold the one with the higher ID, if yes, we'll do it */
      if(DP[high_p].task == ThisTask && DP[high_p].index >= 0 && DP[high_p].index < NumGas)
        this_task_responsible_flag = 1;
    }

  if(this_task_responsible_flag)
    {
      for(k = 0; k < 2; k++)
        {
          int p, q;

          if(k == 0)
            {
              q = p1;
              p = DP[q].index;
            }
          else
            {
              q = p2;
              p = DP[q].index;
            }

          if(DP[q].task == ThisTask)
            {
              if(DP[q].index >= NumGas) /* this is a local ghost point */
                p -= NumGas;

              SphP[p].ActiveArea += f->area;
            }
          else
            {
              /* here we have a foreign ghost point */
              if(DP[q].originalindex < 0)
                terminate("should not happen");

              if(Narea >= MaxNarea)
                {
                  T->Indi.AllocFacNflux *= ALLOC_INCREASE_FACTOR;
                  MaxNarea = T->Indi.AllocFacNflux;
                  AreaList = myrealloc_movable(AreaList, MaxNarea * sizeof(struct area_list_data));

                  if(Narea >= MaxNarea)
                    terminate("Narea >= MaxNarea");
                }

1507
              AreaList[Narea].task  = DP[q].task;
Rainer Weinberger's avatar
Rainer Weinberger committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
              AreaList[Narea].index = DP[q].originalindex;
              AreaList[Narea].darea = f->area;
              Narea++;
            }
        }
    }
}

/*! \brief Copies triangle information from DTC array to trilist.
 *
 *  Performs an orientation check and swaps orientation if needed.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in] tt Index of triangle in DT array.
 *  \param[in] nr Index in DT[tt].t array (adjacent tetrahedrons).
 *  \param[in] dtip Pointer to point to be inserted.
 *  \param[out] trilist Array of triangles.
 *  \param[in] ntri Index in trilist array.
 *  \param[in] max_n_tri Maximum index in trilist array.
 *
 *  \return Next index in trilist array.
 */
1530
int derefine_refine_get_triangles(tessellation *T, int tt, int nr, point *dtip, triangle *trilist, int ntri, int max_n_tri)
Rainer Weinberger's avatar
Rainer Weinberger committed
1531
{
1532
  tetra *DT         = T->DT;
Rainer Weinberger's avatar
Rainer Weinberger committed
1533
  tetra_center *DTC = T->DTC;
1534 1535
  tetra *t          = &DT[tt];
  int qq            = t->t[nr];
Rainer Weinberger's avatar
Rainer Weinberger committed
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

  if(ntri >= max_n_tri)
    terminate("ntri >= max_n_tri");

  trilist[ntri].p[0][0] = DTC[tt].cx;
  trilist[ntri].p[0][1] = DTC[tt].cy;

  trilist[ntri].p[1][0] = DTC[qq].cx;
  trilist[ntri].p[1][1] = DTC[qq].cy;

  trilist[ntri].p[2][0] = dtip->x;
  trilist[ntri].p[2][1] = dtip->y;

  if(get_tri_volume(ntri, trilist) < 0)
    {
      /* swap two points to get proper orientation */
      trilist[ntri].p[1][0] = DTC[tt].cx;
      trilist[ntri].p[1][1] = DTC[tt].cy;

      trilist[ntri].p[0][0] = DTC[qq].cx;
      trilist[ntri].p[0][1] = DTC[qq].cy;
    }

  ntri++;

  return ntri;
}

/*! \brief Add point and adjust triangles accordingly.
 *
 *  \param[in] q Index of point in DP array.
 *  \param[in, out] trilist Array of triangles.
 *  \param[in] ntri Number of elements in trilist before splitting.
 *  \param[in] max_ntri Maximum number of triangles allowed.
 *  \param[in] vol (Unused)
 *
 *  \return Updated number of triangles.
 */
1574
int derefine_add_point_and_split_tri(int q, triangle *trilist, int ntri, int max_ntri, double vol)
Rainer Weinberger's avatar
Rainer Weinberger committed
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
{
  double m[2], n[2], sc[3], *a;
  double cut[2][2], ed[2];
  int i, j, k, kk, l, nnew, flag[3], count, oldq;

  for(i = 0, nnew = ntri; i < ntri; i++)
    {
      if(trilist[i].owner < 0 || trilist[i].owner >= Mesh.Ndp)
        {
          char buf[1000];
          sprintf(buf, "i=%d trilist[i].owner=%d\n", i, trilist[i].owner);
          terminate(buf);
        }

      if(q < 0 || q >= Mesh.Ndp)
        {
          char buf[1000];
          sprintf(buf, "i=%d q=%d\n", i, q);
          terminate(buf);
        }

      /* midpoint */
      m[0] = 0.5 * (Mesh.DP[q].x + Mesh.DP[trilist[i].owner].x);
      m[1] = 0.5 * (Mesh.DP[q].y + Mesh.DP[trilist[i].owner].y);

      n[0] = (Mesh.DP[q].x - Mesh.DP[trilist[i].owner].x);
      n[1] = (Mesh.DP[q].y - Mesh.DP[trilist[i].owner].y);

      if(q == trilist[i].owner)
        terminate("q == trilist[i].owner");

      for(k = 0, count = 0; k < 3; k++) /* determine the side of each point */
        {
          a = &trilist[i].p[k][0];

          sc[k] = (a[0] - m[0]) * n[0] + (a[1] - m[1]) * n[1];

          if(sc[k] > 0)
            {
              flag[k] = 1;
              count++;
            }
          else
            flag[k] = 0;
        }

1621
      switch(count)
Rainer Weinberger's avatar
Rainer Weinberger committed
1622
        {
1623 1624 1625 1626 1627 1628 1629 1630 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 1682 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 1715 1716
          case 0: /* the whole tetra is on the side of current owner - nothing to be done */
            break;

          case 3:                 /* the whole tetra is on the side of new point */
            trilist[i].owner = q; /* change owner */
            break;

          case 1:
          case 2:

            if(nnew + 2 > max_ntri)
              terminate("nnew + 2 > max_ntri");

            trilist[nnew]     = trilist[i];
            trilist[nnew + 1] = trilist[i];

            /* find the point index that is on the other side */
            for(k = 0; k < 3; k++)
              {
                if(flag[k] == 1 && count == 1)
                  break;
                if(flag[k] == 0 && count == 2)
                  break;
              }

            for(j = 0; j < 2; j++)
              {
                kk = k + j + 1;
                if(kk > 2)
                  kk -= 3;

                double *b = trilist[i].p[k];
                double *a = trilist[i].p[kk];

                for(l = 0; l < 2; l++)
                  ed[l] = a[l] - b[l];

                double prod = (ed[0] * n[0] + ed[1] * n[1]);
                double t;
                if(prod)
                  t = -sc[k] / prod;
                else
                  t = 0.5;

                if(t < 0)
                  t = 0;
                if(t > 1)
                  t = 1;

                for(l = 0; l < 2; l++)
                  cut[j][l] = b[l] + t * ed[l];
              }

            /* modify the tetra that's assigned to the new point */
            for(j = 0; j < 2; j++)
              {
                kk = k + j + 1;
                if(kk > 2)
                  kk -= 3;

                for(l = 0; l < 2; l++)
                  trilist[i].p[kk][l] = cut[j][l];
              }

            oldq = trilist[i].owner;

            if(count == 1)
              trilist[i].owner = q;

            /* modify the two new tetras */
            kk = k + 1;
            if(kk > 2)
              kk -= 3;

            for(l = 0; l < 2; l++)
              {
                trilist[nnew].p[k][l] = cut[0][l];

                trilist[nnew + 1].p[k][l]  = cut[1][l];
                trilist[nnew + 1].p[kk][l] = cut[0][l];
              }

            if(count == 1)
              {
                trilist[nnew].owner     = oldq;
                trilist[nnew + 1].owner = oldq;
              }
            else
              {
                trilist[nnew].owner     = q;
                trilist[nnew + 1].owner = q;
              }
            nnew += 2;
            break;
Rainer Weinberger's avatar
Rainer Weinberger committed
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
        }
    }

  return nnew;
}

/*! \brief Determines area of triangle (i.e. 2d Volume).
 *
 *  \param i Index in trilist array.
 *  \param trilist Array with triangles.
 *
 *  \return Area of triangle.
 */
1730
double get_tri_volume(int i, triangle *trilist)
Rainer Weinberger's avatar
Rainer Weinberger committed
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
{
  double *p0 = &trilist[i].p[0][0];
  double *p1 = &trilist[i].p[1][0];
  double *p2 = &trilist[i].p[2][0];

  double nz = (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p1[1] - p0[1]) * (p2[0] - p0[0]);

  return 0.5 * nz;
}

/*! \brief Process edge for volume calculation.
 *
 *  Calculates the contribution of edge to volumes of neighboring
 *  Voronoi cells in vol array.
 *
 *  \param[in] T Pointer to tessellation.
 *  \param[in, out] vol Volume of tetrahedra.
 *  \param[in] tt Index of triangle in DT array.
 *  \param[in] nr Index in edge array.
 *
 *  \return void
 */
1753
void derefine_refine_process_edge(tessellation *T, double *vol, int tt, int nr)
Rainer Weinberger's avatar
Rainer Weinberger committed
1754
{
1755 1756
  tetra *DT         = T->DT;
  point *DP         = T->DP;
Rainer Weinberger's avatar
Rainer Weinberger committed
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
  tetra_center *DTC = T->DTC;

  int i, j, qq, p1, p2;
  double nx, ny;
  double hx, hy;
  double dvol, h;

  tetra *t = &DT[tt];

  i = edge_start[nr];
  j = edge_end[nr];

  point *dpi = &DP[t->p[i]];
  point *dpj = &DP[t->p[j]];

  qq = t->t[nr];

  Edge_visited[tt] |= (1 << nr);
  Edge_visited[qq] |= (1 << (t->s[nr]));

  p1 = t->p[i];
  p2 = t->p[j];

  nx = DTC[tt].cx - DTC[qq].cx;
  ny = DTC[tt].cy - DTC[qq].cy;

  double area = sqrt(nx * nx + ny * ny);

  hx = 0.5 * (dpi->x - dpj->x);
  hy = 0.5 * (dpi->y - dpj->y);

1788
  h    = sqrt(hx * hx + hy * hy);
Rainer Weinberger's avatar
Rainer Weinberger committed
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
  dvol = 0.5 * area * h;

  if(p1 >= 0 && p1 < DeRefMesh.Ndp)
    vol[p1] += dvol;

  if(p2 >= 0 && p2 < DeRefMesh.Ndp)
    vol[p2] += dvol;
}

/*! \brief Computes the circum-circle of all triangles in mesh.
 *
 *  \param[in, out] T Pointer to tessellation.
 *
 *  \return void
 */
1804
void compute_circumcircles(tessellation *T)
Rainer Weinberger's avatar
Rainer Weinberger committed
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
{
  tetra *DT = T->DT;
  char *DTF = T->DTF;

  int i;

  for(i = 0; i < T->Ndt; i++)
    {
      if(DTF[i] & 1)
        continue;
      DTF[i] |= 1;

      if(DT[i].p[0] == DPinfinity)
        continue;
      if(DT[i].p[1] == DPinfinity)
        continue;
      if(DT[i].p[2] == DPinfinity)
        continue;

      update_circumcircle(T, i);
    }
}

/*! \brief Computes the circum-circle of triangle tt.
 *
 *  \param[in, out] T Pointer to tessellation.
 *  \param[in] tt Index of triangle in DT array.
 *
 *  \return void
 */
1835
void update_circumcircle(tessellation *T, int tt)
Rainer Weinberger's avatar
Rainer Weinberger committed
1836
{
1837
  tetra *DT         = T->DT;
Rainer Weinberger's avatar
Rainer Weinberger committed
1838
  tetra_center *DTC = T->DTC;
1839
  point *DP         = T->DP;
Rainer Weinberger's avatar
Rainer Weinberger committed
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857