Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tesselationhelpers.cpp

    r797126 rbdc91e  
    2121#include "verbose.hpp"
    2222#include "Plane.hpp"
    23 #include "Matrix.hpp"
     23
     24double DetGet(gsl_matrix * const A, const int inPlace)
     25{
     26        Info FunctionInfo(__func__);
     27  /*
     28  inPlace = 1 => A is replaced with the LU decomposed copy.
     29  inPlace = 0 => A is retained, and a copy is used for LU.
     30  */
     31
     32  double det;
     33  int signum;
     34  gsl_permutation *p = gsl_permutation_alloc(A->size1);
     35  gsl_matrix *tmpA=0;
     36
     37  if (inPlace)
     38  tmpA = A;
     39  else {
     40  gsl_matrix *tmpA = gsl_matrix_alloc(A->size1, A->size2);
     41  gsl_matrix_memcpy(tmpA , A);
     42  }
     43
     44
     45  gsl_linalg_LU_decomp(tmpA , p , &signum);
     46  det = gsl_linalg_LU_det(tmpA , signum);
     47  gsl_permutation_free(p);
     48  if (! inPlace)
     49  gsl_matrix_free(tmpA);
     50
     51  return det;
     52};
    2453
    2554void GetSphere(Vector * const center, const Vector &a, const Vector &b, const Vector &c, const double RADIUS)
    2655{
    2756        Info FunctionInfo(__func__);
    28   Matrix mat;
     57  gsl_matrix *A = gsl_matrix_calloc(3,3);
    2958  double m11, m12, m13, m14;
    3059
    3160  for(int i=0;i<3;i++) {
    32     mat.set(i, 0, a[i]);
    33     mat.set(i, 1, b[i]);
    34     mat.set(i, 2, c[i]);
    35   }
    36   m11 = mat.determinant();
     61    gsl_matrix_set(A, i, 0, a[i]);
     62    gsl_matrix_set(A, i, 1, b[i]);
     63    gsl_matrix_set(A, i, 2, c[i]);
     64  }
     65  m11 = DetGet(A, 1);
    3766
    3867  for(int i=0;i<3;i++) {
    39     mat.set(i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
    40     mat.set(i, 1, b[i]);
    41     mat.set(i, 2, c[i]);
    42   }
    43   m12 = mat.determinant();
     68    gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
     69    gsl_matrix_set(A, i, 1, b[i]);
     70    gsl_matrix_set(A, i, 2, c[i]);
     71  }
     72  m12 = DetGet(A, 1);
    4473
    4574  for(int i=0;i<3;i++) {
    46     mat.set(i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
    47     mat.set(i, 1, a[i]);
    48     mat.set(i, 2, c[i]);
    49   }
    50   m13 = mat.determinant();
     75    gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
     76    gsl_matrix_set(A, i, 1, a[i]);
     77    gsl_matrix_set(A, i, 2, c[i]);
     78  }
     79  m13 = DetGet(A, 1);
    5180
    5281  for(int i=0;i<3;i++) {
    53     mat.set(i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
    54     mat.set(i, 1, a[i]);
    55     mat.set(i, 2, b[i]);
    56   }
    57   m14 = mat.determinant();
     82    gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
     83    gsl_matrix_set(A, i, 1, a[i]);
     84    gsl_matrix_set(A, i, 2, b[i]);
     85  }
     86  m14 = DetGet(A, 1);
    5887
    5988  if (fabs(m11) < MYEPSILON)
     
    6695  if (fabs(a.distance(*center) - RADIUS) > MYEPSILON)
    6796    DoeLog(1) && (eLog()<< Verbose(1) << "The given center is further way by " << fabs(a.distance(*center) - RADIUS) << " from a than RADIUS." << endl);
     97
     98  gsl_matrix_free(A);
    6899};
    69100
     
    217248  Vector x3;
    218249  Vector x4;
     250};
     251
     252/**
     253 * Intersection calculation function.
     254 *
     255 * @param x to find the result for
     256 * @param function parameter
     257 */
     258double MinIntersectDistance(const gsl_vector * x, void *params)
     259{
     260        Info FunctionInfo(__func__);
     261  double retval = 0;
     262  struct Intersection *I = (struct Intersection *)params;
     263  Vector intersection;
     264  for (int i=0;i<NDIM;i++)
     265    intersection[i] = gsl_vector_get(x, i);
     266
     267  Vector SideA = I->x1 -I->x2 ;
     268  Vector HeightA = intersection - I->x1;
     269  HeightA.ProjectOntoPlane(SideA);
     270
     271  Vector SideB = I->x3 - I->x4;
     272  Vector HeightB = intersection - I->x3;
     273  HeightB.ProjectOntoPlane(SideB);
     274
     275  retval = HeightA.ScalarProduct(HeightA) + HeightB.ScalarProduct(HeightB);
     276  //Log() << Verbose(1) << "MinIntersectDistance called, result: " << retval << endl;
     277
     278  return retval;
     279};
     280
     281
     282/**
     283 * Calculates whether there is an intersection between two lines. The first line
     284 * always goes through point 1 and point 2 and the second line is given by the
     285 * connection between point 4 and point 5.
     286 *
     287 * @param point 1 of line 1
     288 * @param point 2 of line 1
     289 * @param point 1 of line 2
     290 * @param point 2 of line 2
     291 *
     292 * @return true if there is an intersection between the given lines, false otherwise
     293 */
     294bool existsIntersection(const Vector &point1, const Vector &point2, const Vector &point3, const Vector &point4)
     295{
     296        Info FunctionInfo(__func__);
     297  bool result;
     298
     299  struct Intersection par;
     300    par.x1 = point1;
     301    par.x2 = point2;
     302    par.x3 = point3;
     303    par.x4 = point4;
     304
     305    const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
     306    gsl_multimin_fminimizer *s = NULL;
     307    gsl_vector *ss, *x;
     308    gsl_multimin_function minexFunction;
     309
     310    size_t iter = 0;
     311    int status;
     312    double size;
     313
     314    /* Starting point */
     315    x = gsl_vector_alloc(NDIM);
     316    gsl_vector_set(x, 0, point1[0]);
     317    gsl_vector_set(x, 1, point1[1]);
     318    gsl_vector_set(x, 2, point1[2]);
     319
     320    /* Set initial step sizes to 1 */
     321    ss = gsl_vector_alloc(NDIM);
     322    gsl_vector_set_all(ss, 1.0);
     323
     324    /* Initialize method and iterate */
     325    minexFunction.n = NDIM;
     326    minexFunction.f = &MinIntersectDistance;
     327    minexFunction.params = (void *)&par;
     328
     329    s = gsl_multimin_fminimizer_alloc(T, NDIM);
     330    gsl_multimin_fminimizer_set(s, &minexFunction, x, ss);
     331
     332    do {
     333        iter++;
     334        status = gsl_multimin_fminimizer_iterate(s);
     335
     336        if (status) {
     337          break;
     338        }
     339
     340        size = gsl_multimin_fminimizer_size(s);
     341        status = gsl_multimin_test_size(size, 1e-2);
     342
     343        if (status == GSL_SUCCESS) {
     344          DoLog(1) && (Log() << Verbose(1) << "converged to minimum" <<  endl);
     345        }
     346    } while (status == GSL_CONTINUE && iter < 100);
     347
     348    // check whether intersection is in between or not
     349  Vector intersection;
     350  double t1, t2;
     351  for (int i = 0; i < NDIM; i++) {
     352    intersection[i] = gsl_vector_get(s->x, i);
     353  }
     354
     355  Vector SideA = par.x2 - par.x1;
     356  Vector HeightA = intersection - par.x1;
     357
     358  t1 = HeightA.ScalarProduct(SideA)/SideA.ScalarProduct(SideA);
     359
     360  Vector SideB = par.x4 - par.x3;
     361  Vector HeightB = intersection - par.x3;
     362
     363  t2 = HeightB.ScalarProduct(SideB)/SideB.ScalarProduct(SideB);
     364
     365  Log() << Verbose(1) << "Intersection " << intersection << " is at "
     366    << t1 << " for (" << point1 << "," << point2 << ") and at "
     367    << t2 << " for (" << point3 << "," << point4 << "): ";
     368
     369  if (((t1 >= 0) && (t1 <= 1)) && ((t2 >= 0) && (t2 <= 1))) {
     370    DoLog(1) && (Log() << Verbose(1) << "true intersection." << endl);
     371    result = true;
     372  } else {
     373    DoLog(1) && (Log() << Verbose(1) << "intersection out of region of interest." << endl);
     374    result = false;
     375  }
     376
     377  // free minimizer stuff
     378    gsl_vector_free(x);
     379    gsl_vector_free(ss);
     380    gsl_multimin_fminimizer_free(s);
     381
     382  return result;
    219383};
    220384
     
    425589          }
    426590        } else {
    427           eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
    428             << LC->n[2] << " is invalid!" << endl;
     591          DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
    429592        }
    430593      }
     
    481644          }
    482645        } else {
    483           eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
    484             << LC->n[2] << " is invalid!" << endl;
     646          DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
    485647        }
    486648      }
Note: See TracChangeset for help on using the changeset viewer.