| 1 | /*
 | 
|---|
| 2 |  * TesselationHelpers.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Aug 3, 2009
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <fstream>
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include "info.hpp"
 | 
|---|
| 13 | #include "linkedcell.hpp"
 | 
|---|
| 14 | #include "linearsystemofequations.hpp"
 | 
|---|
| 15 | #include "log.hpp"
 | 
|---|
| 16 | #include "tesselation.hpp"
 | 
|---|
| 17 | #include "tesselationhelpers.hpp"
 | 
|---|
| 18 | #include "vector.hpp"
 | 
|---|
| 19 | #include "Line.hpp"
 | 
|---|
| 20 | #include "vector_ops.hpp"
 | 
|---|
| 21 | #include "verbose.hpp"
 | 
|---|
| 22 | #include "Plane.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | double 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 | };
 | 
|---|
| 53 | 
 | 
|---|
| 54 | void GetSphere(Vector * const center, const Vector &a, const Vector &b, const Vector &c, const double RADIUS)
 | 
|---|
| 55 | {
 | 
|---|
| 56 |         Info FunctionInfo(__func__);
 | 
|---|
| 57 |   gsl_matrix *A = gsl_matrix_calloc(3,3);
 | 
|---|
| 58 |   double m11, m12, m13, m14;
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   for(int i=0;i<3;i++) {
 | 
|---|
| 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);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   for(int i=0;i<3;i++) {
 | 
|---|
| 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);
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   for(int i=0;i<3;i++) {
 | 
|---|
| 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);
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   for(int i=0;i<3;i++) {
 | 
|---|
| 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);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   if (fabs(m11) < MYEPSILON)
 | 
|---|
| 89 |     DoeLog(1) && (eLog()<< Verbose(1) << "three points are colinear." << endl);
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   center->at(0) =  0.5 * m12/ m11;
 | 
|---|
| 92 |   center->at(1) = -0.5 * m13/ m11;
 | 
|---|
| 93 |   center->at(2) =  0.5 * m14/ m11;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   if (fabs(a.distance(*center) - RADIUS) > MYEPSILON)
 | 
|---|
| 96 |     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);
 | 
|---|
| 99 | };
 | 
|---|
| 100 | 
 | 
|---|
| 101 | 
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /**
 | 
|---|
| 104 |  * Function returns center of sphere with RADIUS, which rests on points a, b, c
 | 
|---|
| 105 |  * @param Center this vector will be used for return
 | 
|---|
| 106 |  * @param a vector first point of triangle
 | 
|---|
| 107 |  * @param b vector second point of triangle
 | 
|---|
| 108 |  * @param c vector third point of triangle
 | 
|---|
| 109 |  * @param *Umkreismittelpunkt new center point of circumference
 | 
|---|
| 110 |  * @param Direction vector indicates up/down
 | 
|---|
| 111 |  * @param AlternativeDirection Vector, needed in case the triangles have 90 deg angle
 | 
|---|
| 112 |  * @param Halfplaneindicator double indicates whether Direction is up or down
 | 
|---|
| 113 |  * @param AlternativeIndicator double indicates in case of orthogonal triangles which direction of AlternativeDirection is suitable
 | 
|---|
| 114 |  * @param alpha double angle at a
 | 
|---|
| 115 |  * @param beta double, angle at b
 | 
|---|
| 116 |  * @param gamma, double, angle at c
 | 
|---|
| 117 |  * @param Radius, double
 | 
|---|
| 118 |  * @param Umkreisradius double radius of circumscribing circle
 | 
|---|
| 119 |  */
 | 
|---|
| 120 | void GetCenterOfSphere(Vector* const & Center, const Vector &a, const Vector &b, const Vector &c, Vector * const NewUmkreismittelpunkt, const Vector* const Direction, const Vector* const AlternativeDirection,
 | 
|---|
| 121 |     const double HalfplaneIndicator, const double AlternativeIndicator, const double alpha, const double beta, const double gamma, const double RADIUS, const double Umkreisradius)
 | 
|---|
| 122 | {
 | 
|---|
| 123 |         Info FunctionInfo(__func__);
 | 
|---|
| 124 |   Vector TempNormal, helper;
 | 
|---|
| 125 |   double Restradius;
 | 
|---|
| 126 |   Vector OtherCenter;
 | 
|---|
| 127 |   Center->Zero();
 | 
|---|
| 128 |   helper = sin(2.*alpha) * a;
 | 
|---|
| 129 |   (*Center) += helper;
 | 
|---|
| 130 |   helper = sin(2.*beta) * b;
 | 
|---|
| 131 |   (*Center) += helper;
 | 
|---|
| 132 |   helper = sin(2.*gamma) * c;
 | 
|---|
| 133 |   (*Center) += helper;
 | 
|---|
| 134 |   //*Center = a * sin(2.*alpha) + b * sin(2.*beta) + c * sin(2.*gamma) ;
 | 
|---|
| 135 |   Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
 | 
|---|
| 136 |   (*NewUmkreismittelpunkt) = (*Center);
 | 
|---|
| 137 |   DoLog(1) && (Log() << Verbose(1) << "Center of new circumference is " << *NewUmkreismittelpunkt << ".\n");
 | 
|---|
| 138 |   // Here we calculated center of circumscribing circle, using barycentric coordinates
 | 
|---|
| 139 |   DoLog(1) && (Log() << Verbose(1) << "Center of circumference is " << *Center << " in direction " << *Direction << ".\n");
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   TempNormal = a - b;
 | 
|---|
| 142 |   helper = a - c;
 | 
|---|
| 143 |   TempNormal.VectorProduct(helper);
 | 
|---|
| 144 |   if (fabs(HalfplaneIndicator) < MYEPSILON)
 | 
|---|
| 145 |     {
 | 
|---|
| 146 |       if ((TempNormal.ScalarProduct(*AlternativeDirection) <0 && AlternativeIndicator >0) || (TempNormal.ScalarProduct(*AlternativeDirection) >0 && AlternativeIndicator <0))
 | 
|---|
| 147 |         {
 | 
|---|
| 148 |           TempNormal *= -1;
 | 
|---|
| 149 |         }
 | 
|---|
| 150 |     }
 | 
|---|
| 151 |   else
 | 
|---|
| 152 |     {
 | 
|---|
| 153 |       if (((TempNormal.ScalarProduct(*Direction)<0) && (HalfplaneIndicator >0)) || ((TempNormal.ScalarProduct(*Direction)>0) && (HalfplaneIndicator<0)))
 | 
|---|
| 154 |         {
 | 
|---|
| 155 |           TempNormal *= -1;
 | 
|---|
| 156 |         }
 | 
|---|
| 157 |     }
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   TempNormal.Normalize();
 | 
|---|
| 160 |   Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius);
 | 
|---|
| 161 |   DoLog(1) && (Log() << Verbose(1) << "Height of center of circumference to center of sphere is " << Restradius << ".\n");
 | 
|---|
| 162 |   TempNormal.Scale(Restradius);
 | 
|---|
| 163 |   DoLog(1) && (Log() << Verbose(1) << "Shift vector to sphere of circumference is " << TempNormal << ".\n");
 | 
|---|
| 164 |   (*Center) += TempNormal;
 | 
|---|
| 165 |   DoLog(1) && (Log() << Verbose(1) << "Center of sphere of circumference is " << *Center << ".\n");
 | 
|---|
| 166 |   GetSphere(&OtherCenter, a, b, c, RADIUS);
 | 
|---|
| 167 |   DoLog(1) && (Log() << Verbose(1) << "OtherCenter of sphere of circumference is " << OtherCenter << ".\n");
 | 
|---|
| 168 | };
 | 
|---|
| 169 | 
 | 
|---|
| 170 | 
 | 
|---|
| 171 | /** Constructs the center of the circumcircle defined by three points \a *a, \a *b and \a *c.
 | 
|---|
| 172 |  * \param *Center new center on return
 | 
|---|
| 173 |  * \param *a first point
 | 
|---|
| 174 |  * \param *b second point
 | 
|---|
| 175 |  * \param *c third point
 | 
|---|
| 176 |  */
 | 
|---|
| 177 | void GetCenterofCircumcircle(Vector * const Center, const Vector &a, const Vector &b, const Vector &c)
 | 
|---|
| 178 | {
 | 
|---|
| 179 |         Info FunctionInfo(__func__);
 | 
|---|
| 180 |   Vector helper;
 | 
|---|
| 181 |   Vector SideA = b - c;
 | 
|---|
| 182 |   Vector SideB = c - a;
 | 
|---|
| 183 |   Vector SideC = a - b;
 | 
|---|
| 184 | 
 | 
|---|
| 185 |   helper[0] = SideA.NormSquared()*(SideB.NormSquared()+SideC.NormSquared() - SideA.NormSquared());
 | 
|---|
| 186 |   helper[1] = SideB.NormSquared()*(SideC.NormSquared()+SideA.NormSquared() - SideB.NormSquared());
 | 
|---|
| 187 |   helper[2] = SideC.NormSquared()*(SideA.NormSquared()+SideB.NormSquared() - SideC.NormSquared());
 | 
|---|
| 188 | 
 | 
|---|
| 189 |   Center->Zero();
 | 
|---|
| 190 |   *Center += helper[0] * a;
 | 
|---|
| 191 |   *Center += helper[1] * b;
 | 
|---|
| 192 |   *Center += helper[2] * c;
 | 
|---|
| 193 |   Center->Scale(1./(helper[0]+helper[1]+helper[2]));
 | 
|---|
| 194 |   Log() << Verbose(1) << "INFO: Center (2nd algo) is at " << *Center << "." << endl;
 | 
|---|
| 195 | };
 | 
|---|
| 196 | 
 | 
|---|
| 197 | /** Returns the parameter "path length" for a given \a NewSphereCenter relative to \a OldSphereCenter on a circle on the plane \a CirclePlaneNormal with center \a CircleCenter and radius \a CircleRadius.
 | 
|---|
| 198 |  * Test whether the \a NewSphereCenter is really on the given plane and in distance \a CircleRadius from \a CircleCenter.
 | 
|---|
| 199 |  * It calculates the angle, making it unique on [0,2.*M_PI) by comparing to SearchDirection.
 | 
|---|
| 200 |  * Also the new center is invalid if it the same as the old one and does not lie right above (\a NormalVector) the base line (\a CircleCenter).
 | 
|---|
| 201 |  * \param CircleCenter Center of the parameter circle
 | 
|---|
| 202 |  * \param CirclePlaneNormal normal vector to plane of the parameter circle
 | 
|---|
| 203 |  * \param CircleRadius radius of the parameter circle
 | 
|---|
| 204 |  * \param NewSphereCenter new center of a circumcircle
 | 
|---|
| 205 |  * \param OldSphereCenter old center of a circumcircle, defining the zero "path length" on the parameter circle
 | 
|---|
| 206 |  * \param NormalVector normal vector
 | 
|---|
| 207 |  * \param SearchDirection search direction to make angle unique on return.
 | 
|---|
| 208 |  * \return Angle between \a NewSphereCenter and \a OldSphereCenter relative to \a CircleCenter, 2.*M_PI if one test fails
 | 
|---|
| 209 |  */
 | 
|---|
| 210 | double GetPathLengthonCircumCircle(const Vector &CircleCenter, const Vector &CirclePlaneNormal, const double CircleRadius, const Vector &NewSphereCenter, const Vector &OldSphereCenter, const Vector &NormalVector, const Vector &SearchDirection)
 | 
|---|
| 211 | {
 | 
|---|
| 212 |         Info FunctionInfo(__func__);
 | 
|---|
| 213 |   Vector helper;
 | 
|---|
| 214 |   double radius, alpha;
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   Vector RelativeOldSphereCenter = OldSphereCenter - CircleCenter;
 | 
|---|
| 217 |   Vector RelativeNewSphereCenter = NewSphereCenter - CircleCenter;
 | 
|---|
| 218 |   helper = RelativeNewSphereCenter;
 | 
|---|
| 219 |   // test whether new center is on the parameter circle's plane
 | 
|---|
| 220 |   if (fabs(helper.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) {
 | 
|---|
| 221 |     DoeLog(1) && (eLog()<< Verbose(1) << "Something's very wrong here: NewSphereCenter is not on the band's plane as desired by " <<fabs(helper.ScalarProduct(CirclePlaneNormal))  << "!" << endl);
 | 
|---|
| 222 |     helper.ProjectOntoPlane(CirclePlaneNormal);
 | 
|---|
| 223 |   }
 | 
|---|
| 224 |   radius = helper.NormSquared();
 | 
|---|
| 225 |   // test whether the new center vector has length of CircleRadius
 | 
|---|
| 226 |   if (fabs(radius - CircleRadius) > HULLEPSILON)
 | 
|---|
| 227 |     DoeLog(1) && (eLog()<< Verbose(1) << "The projected center of the new sphere has radius " << radius << " instead of " << CircleRadius << "." << endl);
 | 
|---|
| 228 |   alpha = helper.Angle(RelativeOldSphereCenter);
 | 
|---|
| 229 |   // make the angle unique by checking the halfplanes/search direction
 | 
|---|
| 230 |   if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON)  // acos is not unique on [0, 2.*M_PI), hence extra check to decide between two half intervals
 | 
|---|
| 231 |     alpha = 2.*M_PI - alpha;
 | 
|---|
| 232 |   DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeNewSphereCenter is " << helper << ", RelativeOldSphereCenter is " << RelativeOldSphereCenter << " and resulting angle is " << alpha << "." << endl);
 | 
|---|
| 233 |   radius = helper.distance(RelativeOldSphereCenter);
 | 
|---|
| 234 |   helper.ProjectOntoPlane(NormalVector);
 | 
|---|
| 235 |   // check whether new center is somewhat away or at least right over the current baseline to prevent intersecting triangles
 | 
|---|
| 236 |   if ((radius > HULLEPSILON) || (helper.Norm() < HULLEPSILON)) {
 | 
|---|
| 237 |     DoLog(1) && (Log() << Verbose(1) << "INFO: Distance between old and new center is " << radius << " and between new center and baseline center is " << helper.Norm() << "." << endl);
 | 
|---|
| 238 |     return alpha;
 | 
|---|
| 239 |   } else {
 | 
|---|
| 240 |     DoLog(1) && (Log() << Verbose(1) << "INFO: NewSphereCenter " << RelativeNewSphereCenter << " is too close to RelativeOldSphereCenter" << RelativeOldSphereCenter << "." << endl);
 | 
|---|
| 241 |     return 2.*M_PI;
 | 
|---|
| 242 |   }
 | 
|---|
| 243 | };
 | 
|---|
| 244 | 
 | 
|---|
| 245 | struct Intersection {
 | 
|---|
| 246 |   Vector x1;
 | 
|---|
| 247 |   Vector x2;
 | 
|---|
| 248 |   Vector x3;
 | 
|---|
| 249 |   Vector x4;
 | 
|---|
| 250 | };
 | 
|---|
| 251 | 
 | 
|---|
| 252 | /**
 | 
|---|
| 253 |  * Intersection calculation function.
 | 
|---|
| 254 |  *
 | 
|---|
| 255 |  * @param x to find the result for
 | 
|---|
| 256 |  * @param function parameter
 | 
|---|
| 257 |  */
 | 
|---|
| 258 | double 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 |  */
 | 
|---|
| 294 | bool 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 *)∥
 | 
|---|
| 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;
 | 
|---|
| 383 | };
 | 
|---|
| 384 | 
 | 
|---|
| 385 | /** Gets the angle between a point and a reference relative to the provided center.
 | 
|---|
| 386 |  * We have two shanks point and reference between which the angle is calculated
 | 
|---|
| 387 |  * and by scalar product with OrthogonalVector we decide the interval.
 | 
|---|
| 388 |  * @param point to calculate the angle for
 | 
|---|
| 389 |  * @param reference to which to calculate the angle
 | 
|---|
| 390 |  * @param OrthogonalVector points in direction of [pi,2pi] interval
 | 
|---|
| 391 |  *
 | 
|---|
| 392 |  * @return angle between point and reference
 | 
|---|
| 393 |  */
 | 
|---|
| 394 | double GetAngle(const Vector &point, const Vector &reference, const Vector &OrthogonalVector)
 | 
|---|
| 395 | {
 | 
|---|
| 396 |         Info FunctionInfo(__func__);
 | 
|---|
| 397 |   if (reference.IsZero())
 | 
|---|
| 398 |     return M_PI;
 | 
|---|
| 399 | 
 | 
|---|
| 400 |   // calculate both angles and correct with in-plane vector
 | 
|---|
| 401 |   if (point.IsZero())
 | 
|---|
| 402 |     return M_PI;
 | 
|---|
| 403 |   double phi = point.Angle(reference);
 | 
|---|
| 404 |   if (OrthogonalVector.ScalarProduct(point) > 0) {
 | 
|---|
| 405 |     phi = 2.*M_PI - phi;
 | 
|---|
| 406 |   }
 | 
|---|
| 407 | 
 | 
|---|
| 408 |   DoLog(1) && (Log() << Verbose(1) << "INFO: " << point << " has angle " << phi << " with respect to reference " << reference << "." << endl);
 | 
|---|
| 409 | 
 | 
|---|
| 410 |   return phi;
 | 
|---|
| 411 | }
 | 
|---|
| 412 | 
 | 
|---|
| 413 | 
 | 
|---|
| 414 | /** Calculates the volume of a general tetraeder.
 | 
|---|
| 415 |  * \param *a first vector
 | 
|---|
| 416 |  * \param *b second vector
 | 
|---|
| 417 |  * \param *c third vector
 | 
|---|
| 418 |  * \param *d fourth vector
 | 
|---|
| 419 |  * \return \f$ \frac{1}{6} \cdot ((a-d) \times (a-c) \cdot  (a-b)) \f$
 | 
|---|
| 420 |  */
 | 
|---|
| 421 | double CalculateVolumeofGeneralTetraeder(const Vector &a, const Vector &b, const Vector &c, const Vector &d)
 | 
|---|
| 422 | {
 | 
|---|
| 423 |         Info FunctionInfo(__func__);
 | 
|---|
| 424 |   Vector Point, TetraederVector[3];
 | 
|---|
| 425 |   double volume;
 | 
|---|
| 426 | 
 | 
|---|
| 427 |   TetraederVector[0] = a;
 | 
|---|
| 428 |   TetraederVector[1] = b;
 | 
|---|
| 429 |   TetraederVector[2] = c;
 | 
|---|
| 430 |   for (int j=0;j<3;j++)
 | 
|---|
| 431 |     TetraederVector[j].SubtractVector(d);
 | 
|---|
| 432 |   Point = TetraederVector[0];
 | 
|---|
| 433 |   Point.VectorProduct(TetraederVector[1]);
 | 
|---|
| 434 |   volume = 1./6. * fabs(Point.ScalarProduct(TetraederVector[2]));
 | 
|---|
| 435 |   return volume;
 | 
|---|
| 436 | };
 | 
|---|
| 437 | 
 | 
|---|
| 438 | /** Calculates the area of a general triangle.
 | 
|---|
| 439 |  * We use the Heron's formula of area, [Bronstein, S. 138]
 | 
|---|
| 440 |  * \param &A first vector
 | 
|---|
| 441 |  * \param &B second vector
 | 
|---|
| 442 |  * \param &C third vector
 | 
|---|
| 443 |  * \return \f$ \frac{1}{6} \cdot ((a-d) \times (a-c) \cdot  (a-b)) \f$
 | 
|---|
| 444 |  */
 | 
|---|
| 445 | double CalculateAreaofGeneralTriangle(const Vector &A, const Vector &B, const Vector &C)
 | 
|---|
| 446 | {
 | 
|---|
| 447 |   Info FunctionInfo(__func__);
 | 
|---|
| 448 | 
 | 
|---|
| 449 |   const double sidea = B.distance(C);
 | 
|---|
| 450 |   const double sideb = A.distance(C);
 | 
|---|
| 451 |   const double sidec = A.distance(B);
 | 
|---|
| 452 |   const double s = (sidea+sideb+sidec)/2.;
 | 
|---|
| 453 | 
 | 
|---|
| 454 |   const double area = sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
 | 
|---|
| 455 |   return area;
 | 
|---|
| 456 | };
 | 
|---|
| 457 | 
 | 
|---|
| 458 | 
 | 
|---|
| 459 | /** Checks for a new special triangle whether one of its edges is already present with one one triangle connected.
 | 
|---|
| 460 |  * This enforces that special triangles (i.e. degenerated ones) should at last close the open-edge frontier and not
 | 
|---|
| 461 |  * make it bigger (i.e. closing one (the baseline) and opening two new ones).
 | 
|---|
| 462 |  * \param TPS[3] nodes of the triangle
 | 
|---|
| 463 |  * \return true - there is such a line (i.e. creation of degenerated triangle is valid), false - no such line (don't create)
 | 
|---|
| 464 |  */
 | 
|---|
| 465 | bool CheckLineCriteriaForDegeneratedTriangle(const BoundaryPointSet * const nodes[3])
 | 
|---|
| 466 | {
 | 
|---|
| 467 |         Info FunctionInfo(__func__);
 | 
|---|
| 468 |   bool result = false;
 | 
|---|
| 469 |   int counter = 0;
 | 
|---|
| 470 | 
 | 
|---|
| 471 |   // check all three points
 | 
|---|
| 472 |   for (int i=0;i<3;i++)
 | 
|---|
| 473 |     for (int j=i+1; j<3; j++) {
 | 
|---|
| 474 |       if (nodes[i] == NULL) {
 | 
|---|
| 475 |         DoLog(1) && (Log() << Verbose(1) << "Node nr. " << i << " is not yet present." << endl);
 | 
|---|
| 476 |         result = true;
 | 
|---|
| 477 |       } else if (nodes[i]->lines.find(nodes[j]->node->nr) != nodes[i]->lines.end()) {  // there already is a line
 | 
|---|
| 478 |         LineMap::const_iterator FindLine;
 | 
|---|
| 479 |         pair<LineMap::const_iterator,LineMap::const_iterator> FindPair;
 | 
|---|
| 480 |         FindPair = nodes[i]->lines.equal_range(nodes[j]->node->nr);
 | 
|---|
| 481 |         for (FindLine = FindPair.first; FindLine != FindPair.second; ++FindLine) {
 | 
|---|
| 482 |           // If there is a line with less than two attached triangles, we don't need a new line.
 | 
|---|
| 483 |           if (FindLine->second->triangles.size() < 2) {
 | 
|---|
| 484 |             counter++;
 | 
|---|
| 485 |             break;  // increase counter only once per edge
 | 
|---|
| 486 |           }
 | 
|---|
| 487 |         }
 | 
|---|
| 488 |       } else { // no line
 | 
|---|
| 489 |         DoLog(1) && (Log() << Verbose(1) << "The line between " << *nodes[i] << " and " << *nodes[j] << " is not yet present, hence no need for a degenerate triangle." << endl);
 | 
|---|
| 490 |         result = true;
 | 
|---|
| 491 |       }
 | 
|---|
| 492 |     }
 | 
|---|
| 493 |   if ((!result) && (counter > 1)) {
 | 
|---|
| 494 |     DoLog(1) && (Log() << Verbose(1) << "INFO: Degenerate triangle is ok, at least two, here " << counter << ", existing lines are used." << endl);
 | 
|---|
| 495 |     result = true;
 | 
|---|
| 496 |   }
 | 
|---|
| 497 |   return result;
 | 
|---|
| 498 | };
 | 
|---|
| 499 | 
 | 
|---|
| 500 | 
 | 
|---|
| 501 | ///** Sort function for the candidate list.
 | 
|---|
| 502 | // */
 | 
|---|
| 503 | //bool SortCandidates(const CandidateForTesselation* candidate1, const CandidateForTesselation* candidate2)
 | 
|---|
| 504 | //{
 | 
|---|
| 505 | //      Info FunctionInfo(__func__);
 | 
|---|
| 506 | //  Vector BaseLineVector, OrthogonalVector, helper;
 | 
|---|
| 507 | //  if (candidate1->BaseLine != candidate2->BaseLine) {  // sanity check
 | 
|---|
| 508 | //    DoeLog(1) && (eLog()<< Verbose(1) << "sortCandidates was called for two different baselines: " << candidate1->BaseLine << " and " << candidate2->BaseLine << "." << endl);
 | 
|---|
| 509 | //    //return false;
 | 
|---|
| 510 | //    exit(1);
 | 
|---|
| 511 | //  }
 | 
|---|
| 512 | //  // create baseline vector
 | 
|---|
| 513 | //  BaseLineVector.CopyVector(candidate1->BaseLine->endpoints[1]->node->node);
 | 
|---|
| 514 | //  BaseLineVector.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
| 515 | //  BaseLineVector.Normalize();
 | 
|---|
| 516 | //
 | 
|---|
| 517 | //  // create normal in-plane vector to cope with acos() non-uniqueness on [0,2pi] (note that is pointing in the "right" direction already, hence ">0" test!)
 | 
|---|
| 518 | //  helper.CopyVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
| 519 | //  helper.SubtractVector(candidate1->point->node);
 | 
|---|
| 520 | //  OrthogonalVector.CopyVector(&helper);
 | 
|---|
| 521 | //  helper.VectorProduct(&BaseLineVector);
 | 
|---|
| 522 | //  OrthogonalVector.SubtractVector(&helper);
 | 
|---|
| 523 | //  OrthogonalVector.Normalize();
 | 
|---|
| 524 | //
 | 
|---|
| 525 | //  // calculate both angles and correct with in-plane vector
 | 
|---|
| 526 | //  helper.CopyVector(candidate1->point->node);
 | 
|---|
| 527 | //  helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
| 528 | //  double phi = BaseLineVector.Angle(&helper);
 | 
|---|
| 529 | //  if (OrthogonalVector.ScalarProduct(&helper) > 0) {
 | 
|---|
| 530 | //    phi = 2.*M_PI - phi;
 | 
|---|
| 531 | //  }
 | 
|---|
| 532 | //  helper.CopyVector(candidate2->point->node);
 | 
|---|
| 533 | //  helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
| 534 | //  double psi = BaseLineVector.Angle(&helper);
 | 
|---|
| 535 | //  if (OrthogonalVector.ScalarProduct(&helper) > 0) {
 | 
|---|
| 536 | //    psi = 2.*M_PI - psi;
 | 
|---|
| 537 | //  }
 | 
|---|
| 538 | //
 | 
|---|
| 539 | //  Log() << Verbose(1) << *candidate1->point << " has angle " << phi << endl;
 | 
|---|
| 540 | //  Log() << Verbose(1) << *candidate2->point << " has angle " << psi << endl;
 | 
|---|
| 541 | //
 | 
|---|
| 542 | //  // return comparison
 | 
|---|
| 543 | //  return phi < psi;
 | 
|---|
| 544 | //};
 | 
|---|
| 545 | 
 | 
|---|
| 546 | /**
 | 
|---|
| 547 |  * Finds the point which is second closest to the provided one.
 | 
|---|
| 548 |  *
 | 
|---|
| 549 |  * @param Point to which to find the second closest other point
 | 
|---|
| 550 |  * @param linked cell structure
 | 
|---|
| 551 |  *
 | 
|---|
| 552 |  * @return point which is second closest to the provided one
 | 
|---|
| 553 |  */
 | 
|---|
| 554 | TesselPoint* FindSecondClosestTesselPoint(const Vector* Point, const LinkedCell* const LC)
 | 
|---|
| 555 | {
 | 
|---|
| 556 |         Info FunctionInfo(__func__);
 | 
|---|
| 557 |   TesselPoint* closestPoint = NULL;
 | 
|---|
| 558 |   TesselPoint* secondClosestPoint = NULL;
 | 
|---|
| 559 |   double distance = 1e16;
 | 
|---|
| 560 |   double secondDistance = 1e16;
 | 
|---|
| 561 |   Vector helper;
 | 
|---|
| 562 |   int N[NDIM], Nlower[NDIM], Nupper[NDIM];
 | 
|---|
| 563 | 
 | 
|---|
| 564 |   LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
 | 
|---|
| 565 |   for(int i=0;i<NDIM;i++) // store indices of this cell
 | 
|---|
| 566 |     N[i] = LC->n[i];
 | 
|---|
| 567 |   DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
 | 
|---|
| 568 | 
 | 
|---|
| 569 |   LC->GetNeighbourBounds(Nlower, Nupper);
 | 
|---|
| 570 |   //Log() << Verbose(1) << endl;
 | 
|---|
| 571 |   for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
| 572 |     for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
| 573 |       for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| 574 |         const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| 575 |         //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
 | 
|---|
| 576 |         if (List != NULL) {
 | 
|---|
| 577 |           for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| 578 |             helper = (*Point) - (*(*Runner)->node);
 | 
|---|
| 579 |             double currentNorm = helper. Norm();
 | 
|---|
| 580 |             if (currentNorm < distance) {
 | 
|---|
| 581 |               // remember second point
 | 
|---|
| 582 |               secondDistance = distance;
 | 
|---|
| 583 |               secondClosestPoint = closestPoint;
 | 
|---|
| 584 |               // mark down new closest point
 | 
|---|
| 585 |               distance = currentNorm;
 | 
|---|
| 586 |               closestPoint = (*Runner);
 | 
|---|
| 587 |               //Log() << Verbose(2) << "INFO: New Second Nearest Neighbour is " << *secondClosestPoint << "." << endl;
 | 
|---|
| 588 |             }
 | 
|---|
| 589 |           }
 | 
|---|
| 590 |         } else {
 | 
|---|
| 591 |           DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
 | 
|---|
| 592 |         }
 | 
|---|
| 593 |       }
 | 
|---|
| 594 | 
 | 
|---|
| 595 |   return secondClosestPoint;
 | 
|---|
| 596 | };
 | 
|---|
| 597 | 
 | 
|---|
| 598 | /**
 | 
|---|
| 599 |  * Finds the point which is closest to the provided one.
 | 
|---|
| 600 |  *
 | 
|---|
| 601 |  * @param Point to which to find the closest other point
 | 
|---|
| 602 |  * @param SecondPoint the second closest other point on return, NULL if none found
 | 
|---|
| 603 |  * @param linked cell structure
 | 
|---|
| 604 |  *
 | 
|---|
| 605 |  * @return point which is closest to the provided one, NULL if none found
 | 
|---|
| 606 |  */
 | 
|---|
| 607 | TesselPoint* FindClosestTesselPoint(const Vector* Point, TesselPoint *&SecondPoint, const LinkedCell* const LC)
 | 
|---|
| 608 | {
 | 
|---|
| 609 |         Info FunctionInfo(__func__);
 | 
|---|
| 610 |   TesselPoint* closestPoint = NULL;
 | 
|---|
| 611 |   SecondPoint = NULL;
 | 
|---|
| 612 |   double distance = 1e16;
 | 
|---|
| 613 |   double secondDistance = 1e16;
 | 
|---|
| 614 |   Vector helper;
 | 
|---|
| 615 |   int N[NDIM], Nlower[NDIM], Nupper[NDIM];
 | 
|---|
| 616 | 
 | 
|---|
| 617 |   LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
 | 
|---|
| 618 |   for(int i=0;i<NDIM;i++) // store indices of this cell
 | 
|---|
| 619 |     N[i] = LC->n[i];
 | 
|---|
| 620 |   DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
 | 
|---|
| 621 | 
 | 
|---|
| 622 |   LC->GetNeighbourBounds(Nlower, Nupper);
 | 
|---|
| 623 |   //Log() << Verbose(1) << endl;
 | 
|---|
| 624 |   for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
| 625 |     for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
| 626 |       for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| 627 |         const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| 628 |         //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
 | 
|---|
| 629 |         if (List != NULL) {
 | 
|---|
| 630 |           for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| 631 |             helper = (*Point) - (*(*Runner)->node);
 | 
|---|
| 632 |             double currentNorm = helper.NormSquared();
 | 
|---|
| 633 |             if (currentNorm < distance) {
 | 
|---|
| 634 |               secondDistance = distance;
 | 
|---|
| 635 |               SecondPoint = closestPoint;
 | 
|---|
| 636 |               distance = currentNorm;
 | 
|---|
| 637 |               closestPoint = (*Runner);
 | 
|---|
| 638 |               //Log() << Verbose(1) << "INFO: New Nearest Neighbour is " << *closestPoint << "." << endl;
 | 
|---|
| 639 |             } else if (currentNorm < secondDistance) {
 | 
|---|
| 640 |               secondDistance = currentNorm;
 | 
|---|
| 641 |               SecondPoint = (*Runner);
 | 
|---|
| 642 |               //Log() << Verbose(1) << "INFO: New Second Nearest Neighbour is " << *SecondPoint << "." << endl;
 | 
|---|
| 643 |             }
 | 
|---|
| 644 |           }
 | 
|---|
| 645 |         } else {
 | 
|---|
| 646 |           DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl);
 | 
|---|
| 647 |         }
 | 
|---|
| 648 |       }
 | 
|---|
| 649 |   // output
 | 
|---|
| 650 |   if (closestPoint != NULL) {
 | 
|---|
| 651 |     DoLog(1) && (Log() << Verbose(1) << "Closest point is " << *closestPoint);
 | 
|---|
| 652 |     if (SecondPoint != NULL)
 | 
|---|
| 653 |       DoLog(0) && (Log() << Verbose(0) << " and second closest is " << *SecondPoint);
 | 
|---|
| 654 |     DoLog(0) && (Log() << Verbose(0) << "." << endl);
 | 
|---|
| 655 |   }
 | 
|---|
| 656 |   return closestPoint;
 | 
|---|
| 657 | };
 | 
|---|
| 658 | 
 | 
|---|
| 659 | /** Returns the closest point on \a *Base with respect to \a *OtherBase.
 | 
|---|
| 660 |  * \param *out output stream for debugging
 | 
|---|
| 661 |  * \param *Base reference line
 | 
|---|
| 662 |  * \param *OtherBase other base line
 | 
|---|
| 663 |  * \return Vector on reference line that has closest distance
 | 
|---|
| 664 |  */
 | 
|---|
| 665 | Vector * GetClosestPointBetweenLine(const BoundaryLineSet * const Base, const BoundaryLineSet * const OtherBase)
 | 
|---|
| 666 | {
 | 
|---|
| 667 |         Info FunctionInfo(__func__);
 | 
|---|
| 668 |   // construct the plane of the two baselines (i.e. take both their directional vectors)
 | 
|---|
| 669 |   Vector Baseline = (*Base->endpoints[1]->node->node) - (*Base->endpoints[0]->node->node);
 | 
|---|
| 670 |   Vector OtherBaseline = (*OtherBase->endpoints[1]->node->node) - (*OtherBase->endpoints[0]->node->node);
 | 
|---|
| 671 |   Vector Normal = Baseline;
 | 
|---|
| 672 |   Normal.VectorProduct(OtherBaseline);
 | 
|---|
| 673 |   Normal.Normalize();
 | 
|---|
| 674 |   DoLog(1) && (Log() << Verbose(1) << "First direction is " << Baseline << ", second direction is " << OtherBaseline << ", normal of intersection plane is " << Normal << "." << endl);
 | 
|---|
| 675 | 
 | 
|---|
| 676 |   // project one offset point of OtherBase onto this plane (and add plane offset vector)
 | 
|---|
| 677 |   Vector NewOffset = (*OtherBase->endpoints[0]->node->node) - (*Base->endpoints[0]->node->node);
 | 
|---|
| 678 |   NewOffset.ProjectOntoPlane(Normal);
 | 
|---|
| 679 |   NewOffset += (*Base->endpoints[0]->node->node);
 | 
|---|
| 680 |   Vector NewDirection = NewOffset + OtherBaseline;
 | 
|---|
| 681 | 
 | 
|---|
| 682 |   // calculate the intersection between this projected baseline and Base
 | 
|---|
| 683 |   Vector *Intersection = new Vector;
 | 
|---|
| 684 |   Line line1 = makeLineThrough(*(Base->endpoints[0]->node->node),*(Base->endpoints[1]->node->node));
 | 
|---|
| 685 |   Line line2 = makeLineThrough(NewOffset, NewDirection);
 | 
|---|
| 686 |   *Intersection = line1.getIntersection(line2);
 | 
|---|
| 687 |   Normal = (*Intersection) - (*Base->endpoints[0]->node->node);
 | 
|---|
| 688 |   DoLog(1) && (Log() << Verbose(1) << "Found closest point on " << *Base << " at " << *Intersection << ", factor in line is " << fabs(Normal.ScalarProduct(Baseline)/Baseline.NormSquared()) << "." << endl);
 | 
|---|
| 689 | 
 | 
|---|
| 690 |   return Intersection;
 | 
|---|
| 691 | };
 | 
|---|
| 692 | 
 | 
|---|
| 693 | /** Returns the distance to the plane defined by \a *triangle
 | 
|---|
| 694 |  * \param *out output stream for debugging
 | 
|---|
| 695 |  * \param *x Vector to calculate distance to
 | 
|---|
| 696 |  * \param *triangle triangle defining plane
 | 
|---|
| 697 |  * \return distance between \a *x and plane defined by \a *triangle, -1 - if something went wrong
 | 
|---|
| 698 |  */
 | 
|---|
| 699 | double DistanceToTrianglePlane(const Vector *x, const BoundaryTriangleSet * const triangle)
 | 
|---|
| 700 | {
 | 
|---|
| 701 |         Info FunctionInfo(__func__);
 | 
|---|
| 702 |   double distance = 0.;
 | 
|---|
| 703 |   if (x == NULL) {
 | 
|---|
| 704 |     return -1;
 | 
|---|
| 705 |   }
 | 
|---|
| 706 |   distance = x->DistanceToSpace(triangle->getPlane());
 | 
|---|
| 707 |   return distance;
 | 
|---|
| 708 | };
 | 
|---|
| 709 | 
 | 
|---|
| 710 | /** Creates the objects in a VRML file.
 | 
|---|
| 711 |  * \param *out output stream for debugging
 | 
|---|
| 712 |  * \param *vrmlfile output stream for tecplot data
 | 
|---|
| 713 |  * \param *Tess Tesselation structure with constructed triangles
 | 
|---|
| 714 |  * \param *mol molecule structure with atom positions
 | 
|---|
| 715 |  */
 | 
|---|
| 716 | void WriteVrmlFile(ofstream * const vrmlfile, const Tesselation * const Tess, const PointCloud * const cloud)
 | 
|---|
| 717 | {
 | 
|---|
| 718 |         Info FunctionInfo(__func__);
 | 
|---|
| 719 |   TesselPoint *Walker = NULL;
 | 
|---|
| 720 |   int i;
 | 
|---|
| 721 |   Vector *center = cloud->GetCenter();
 | 
|---|
| 722 |   if (vrmlfile != NULL) {
 | 
|---|
| 723 |     //Log() << Verbose(1) << "Writing Raster3D file ... ";
 | 
|---|
| 724 |     *vrmlfile << "#VRML V2.0 utf8" << endl;
 | 
|---|
| 725 |     *vrmlfile << "#Created by molecuilder" << endl;
 | 
|---|
| 726 |     *vrmlfile << "#All atoms as spheres" << endl;
 | 
|---|
| 727 |     cloud->GoToFirst();
 | 
|---|
| 728 |     while (!cloud->IsEnd()) {
 | 
|---|
| 729 |       Walker = cloud->GetPoint();
 | 
|---|
| 730 |       *vrmlfile << "Sphere {" << endl << "  "; // 2 is sphere type
 | 
|---|
| 731 |       for (i=0;i<NDIM;i++)
 | 
|---|
| 732 |         *vrmlfile << Walker->node->at(i)-center->at(i) << " ";
 | 
|---|
| 733 |       *vrmlfile << "\t0.1\t1. 1. 1." << endl; // radius 0.05 and white as colour
 | 
|---|
| 734 |       cloud->GoToNext();
 | 
|---|
| 735 |     }
 | 
|---|
| 736 | 
 | 
|---|
| 737 |     *vrmlfile << "# All tesselation triangles" << endl;
 | 
|---|
| 738 |     for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
 | 
|---|
| 739 |       *vrmlfile << "1" << endl << "  "; // 1 is triangle type
 | 
|---|
| 740 |       for (i=0;i<3;i++) { // print each node
 | 
|---|
| 741 |         for (int j=0;j<NDIM;j++)  // and for each node all NDIM coordinates
 | 
|---|
| 742 |           *vrmlfile << TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j) << " ";
 | 
|---|
| 743 |         *vrmlfile << "\t";
 | 
|---|
| 744 |       }
 | 
|---|
| 745 |       *vrmlfile << "1. 0. 0." << endl;  // red as colour
 | 
|---|
| 746 |       *vrmlfile << "18" << endl << "  0.5 0.5 0.5" << endl; // 18 is transparency type for previous object
 | 
|---|
| 747 |     }
 | 
|---|
| 748 |   } else {
 | 
|---|
| 749 |     DoeLog(1) && (eLog()<< Verbose(1) << "Given vrmlfile is " << vrmlfile << "." << endl);
 | 
|---|
| 750 |   }
 | 
|---|
| 751 |   delete(center);
 | 
|---|
| 752 | };
 | 
|---|
| 753 | 
 | 
|---|
| 754 | /** Writes additionally the current sphere (i.e. the last triangle to file).
 | 
|---|
| 755 |  * \param *out output stream for debugging
 | 
|---|
| 756 |  * \param *rasterfile output stream for tecplot data
 | 
|---|
| 757 |  * \param *Tess Tesselation structure with constructed triangles
 | 
|---|
| 758 |  * \param *mol molecule structure with atom positions
 | 
|---|
| 759 |  */
 | 
|---|
| 760 | void IncludeSphereinRaster3D(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
 | 
|---|
| 761 | {
 | 
|---|
| 762 |         Info FunctionInfo(__func__);
 | 
|---|
| 763 |   Vector helper;
 | 
|---|
| 764 | 
 | 
|---|
| 765 |   if (Tess->LastTriangle != NULL) {
 | 
|---|
| 766 |     // include the current position of the virtual sphere in the temporary raster3d file
 | 
|---|
| 767 |     Vector *center = cloud->GetCenter();
 | 
|---|
| 768 |     // make the circumsphere's center absolute again
 | 
|---|
| 769 |     Vector helper = (1./3.) * ((*Tess->LastTriangle->endpoints[0]->node->node) +
 | 
|---|
| 770 |                                (*Tess->LastTriangle->endpoints[1]->node->node) +
 | 
|---|
| 771 |                                (*Tess->LastTriangle->endpoints[2]->node->node));
 | 
|---|
| 772 |     helper -= (*center);
 | 
|---|
| 773 |     // and add to file plus translucency object
 | 
|---|
| 774 |     *rasterfile << "# current virtual sphere\n";
 | 
|---|
| 775 |     *rasterfile << "8\n  25.0    0.6     -1.0 -1.0 -1.0     0.2        0 0 0 0\n";
 | 
|---|
| 776 |     *rasterfile << "2\n  " << helper[0] << " " << helper[1] << " " << helper[2] << "\t" << 5. << "\t1 0 0\n";
 | 
|---|
| 777 |     *rasterfile << "9\n  terminating special property\n";
 | 
|---|
| 778 |     delete(center);
 | 
|---|
| 779 |   }
 | 
|---|
| 780 | };
 | 
|---|
| 781 | 
 | 
|---|
| 782 | /** Creates the objects in a raster3d file (renderable with a header.r3d).
 | 
|---|
| 783 |  * \param *out output stream for debugging
 | 
|---|
| 784 |  * \param *rasterfile output stream for tecplot data
 | 
|---|
| 785 |  * \param *Tess Tesselation structure with constructed triangles
 | 
|---|
| 786 |  * \param *mol molecule structure with atom positions
 | 
|---|
| 787 |  */
 | 
|---|
| 788 | void WriteRaster3dFile(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
 | 
|---|
| 789 | {
 | 
|---|
| 790 |         Info FunctionInfo(__func__);
 | 
|---|
| 791 |   TesselPoint *Walker = NULL;
 | 
|---|
| 792 |   int i;
 | 
|---|
| 793 |   Vector *center = cloud->GetCenter();
 | 
|---|
| 794 |   if (rasterfile != NULL) {
 | 
|---|
| 795 |     //Log() << Verbose(1) << "Writing Raster3D file ... ";
 | 
|---|
| 796 |     *rasterfile << "# Raster3D object description, created by MoleCuilder" << endl;
 | 
|---|
| 797 |     *rasterfile << "@header.r3d" << endl;
 | 
|---|
| 798 |     *rasterfile << "# All atoms as spheres" << endl;
 | 
|---|
| 799 |     cloud->GoToFirst();
 | 
|---|
| 800 |     while (!cloud->IsEnd()) {
 | 
|---|
| 801 |       Walker = cloud->GetPoint();
 | 
|---|
| 802 |       *rasterfile << "2" << endl << "  ";  // 2 is sphere type
 | 
|---|
| 803 |       for (int j=0;j<NDIM;j++) { // and for each node all NDIM coordinates
 | 
|---|
| 804 |         const double tmp = Walker->node->at(j)-center->at(j);
 | 
|---|
| 805 |         *rasterfile << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
 | 
|---|
| 806 |       }
 | 
|---|
| 807 |       *rasterfile << "\t0.1\t1. 1. 1." << endl; // radius 0.05 and white as colour
 | 
|---|
| 808 |       cloud->GoToNext();
 | 
|---|
| 809 |     }
 | 
|---|
| 810 | 
 | 
|---|
| 811 |     *rasterfile << "# All tesselation triangles" << endl;
 | 
|---|
| 812 |     *rasterfile << "8\n  25. -1.   1. 1. 1.   0.0    0 0 0 2\n  SOLID     1.0 0.0 0.0\n  BACKFACE  0.3 0.3 1.0   0 0\n";
 | 
|---|
| 813 |     for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
 | 
|---|
| 814 |       *rasterfile << "1" << endl << "  ";  // 1 is triangle type
 | 
|---|
| 815 |       for (i=0;i<3;i++) {  // print each node
 | 
|---|
| 816 |         for (int j=0;j<NDIM;j++) { // and for each node all NDIM coordinates
 | 
|---|
| 817 |           const double tmp = TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j);
 | 
|---|
| 818 |           *rasterfile << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
 | 
|---|
| 819 |         }
 | 
|---|
| 820 |         *rasterfile << "\t";
 | 
|---|
| 821 |       }
 | 
|---|
| 822 |       *rasterfile << "1. 0. 0." << endl;  // red as colour
 | 
|---|
| 823 |       //*rasterfile << "18" << endl << "  0.5 0.5 0.5" << endl;  // 18 is transparency type for previous object
 | 
|---|
| 824 |     }
 | 
|---|
| 825 |     *rasterfile << "9\n#  terminating special property\n";
 | 
|---|
| 826 |   } else {
 | 
|---|
| 827 |     DoeLog(1) && (eLog()<< Verbose(1) << "Given rasterfile is " << rasterfile << "." << endl);
 | 
|---|
| 828 |   }
 | 
|---|
| 829 |   IncludeSphereinRaster3D(rasterfile, Tess, cloud);
 | 
|---|
| 830 |   delete(center);
 | 
|---|
| 831 | };
 | 
|---|
| 832 | 
 | 
|---|
| 833 | /** This function creates the tecplot file, displaying the tesselation of the hull.
 | 
|---|
| 834 |  * \param *out output stream for debugging
 | 
|---|
| 835 |  * \param *tecplot output stream for tecplot data
 | 
|---|
| 836 |  * \param N arbitrary number to differentiate various zones in the tecplot format
 | 
|---|
| 837 |  */
 | 
|---|
| 838 | void WriteTecplotFile(ofstream * const tecplot, const Tesselation * const TesselStruct, const PointCloud * const cloud, const int N)
 | 
|---|
| 839 | {
 | 
|---|
| 840 |         Info FunctionInfo(__func__);
 | 
|---|
| 841 |   if ((tecplot != NULL) && (TesselStruct != NULL)) {
 | 
|---|
| 842 |     // write header
 | 
|---|
| 843 |     *tecplot << "TITLE = \"3D CONVEX SHELL\"" << endl;
 | 
|---|
| 844 |     *tecplot << "VARIABLES = \"X\" \"Y\" \"Z\" \"U\"" << endl;
 | 
|---|
| 845 |     *tecplot << "ZONE T=\"";
 | 
|---|
| 846 |     if (N < 0) {
 | 
|---|
| 847 |       *tecplot << cloud->GetName();
 | 
|---|
| 848 |     } else {
 | 
|---|
| 849 |       *tecplot << N << "-";
 | 
|---|
| 850 |       if (TesselStruct->LastTriangle != NULL) {
 | 
|---|
| 851 |         for (int i=0;i<3;i++)
 | 
|---|
| 852 |           *tecplot << (i==0 ? "" : "_") << TesselStruct->LastTriangle->endpoints[i]->node->getName();
 | 
|---|
| 853 |       } else {
 | 
|---|
| 854 |         *tecplot << "none";
 | 
|---|
| 855 |       }
 | 
|---|
| 856 |     }
 | 
|---|
| 857 |     *tecplot << "\", N=" << TesselStruct->PointsOnBoundary.size() << ", E=" << TesselStruct->TrianglesOnBoundary.size() << ", DATAPACKING=POINT, ZONETYPE=FETRIANGLE" << endl;
 | 
|---|
| 858 |     const int MaxId=cloud->GetMaxId();
 | 
|---|
| 859 |     int *LookupList = new int[MaxId];
 | 
|---|
| 860 |     for (int i=0; i< MaxId ; i++){
 | 
|---|
| 861 |       LookupList[i] = -1;
 | 
|---|
| 862 |     }
 | 
|---|
| 863 | 
 | 
|---|
| 864 |     // print atom coordinates
 | 
|---|
| 865 |     int Counter = 1;
 | 
|---|
| 866 |     TesselPoint *Walker = NULL;
 | 
|---|
| 867 |     for (PointMap::const_iterator target = TesselStruct->PointsOnBoundary.begin(); target != TesselStruct->PointsOnBoundary.end(); ++target) {
 | 
|---|
| 868 |       Walker = target->second->node;
 | 
|---|
| 869 |       LookupList[Walker->nr] = Counter++;
 | 
|---|
| 870 |       for (int i=0;i<NDIM;i++) {
 | 
|---|
| 871 |         const double tmp = Walker->node->at(i);
 | 
|---|
| 872 |         *tecplot << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
 | 
|---|
| 873 |       }
 | 
|---|
| 874 |       *tecplot << target->second->value << endl;
 | 
|---|
| 875 |     }
 | 
|---|
| 876 |     *tecplot << endl;
 | 
|---|
| 877 |     // print connectivity
 | 
|---|
| 878 |     DoLog(1) && (Log() << Verbose(1) << "The following triangles were created:" << endl);
 | 
|---|
| 879 |     for (TriangleMap::const_iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner != TesselStruct->TrianglesOnBoundary.end(); runner++) {
 | 
|---|
| 880 |       DoLog(1) && (Log() << Verbose(1) << " " << runner->second->endpoints[0]->node->getName() << "<->" << runner->second->endpoints[1]->node->getName() << "<->" << runner->second->endpoints[2]->node->getName() << endl);
 | 
|---|
| 881 |       *tecplot << LookupList[runner->second->endpoints[0]->node->nr] << " " << LookupList[runner->second->endpoints[1]->node->nr] << " " << LookupList[runner->second->endpoints[2]->node->nr] << endl;
 | 
|---|
| 882 |     }
 | 
|---|
| 883 |     delete[] (LookupList);
 | 
|---|
| 884 |   }
 | 
|---|
| 885 | };
 | 
|---|
| 886 | 
 | 
|---|
| 887 | /** Calculates the concavity for each of the BoundaryPointSet's in a Tesselation.
 | 
|---|
| 888 |  * Sets BoundaryPointSet::value equal to the number of connected lines that are not convex.
 | 
|---|
| 889 |  * \param *out output stream for debugging
 | 
|---|
| 890 |  * \param *TesselStruct pointer to Tesselation structure
 | 
|---|
| 891 |  */
 | 
|---|
| 892 | void CalculateConcavityPerBoundaryPoint(const Tesselation * const TesselStruct)
 | 
|---|
| 893 | {
 | 
|---|
| 894 |         Info FunctionInfo(__func__);
 | 
|---|
| 895 |   class BoundaryPointSet *point = NULL;
 | 
|---|
| 896 |   class BoundaryLineSet *line = NULL;
 | 
|---|
| 897 |   class BoundaryTriangleSet *triangle = NULL;
 | 
|---|
| 898 |   double ConcavityPerLine = 0.;
 | 
|---|
| 899 |   double ConcavityPerTriangle = 0.;
 | 
|---|
| 900 |   double area = 0.;
 | 
|---|
| 901 |   double totalarea = 0.;
 | 
|---|
| 902 | 
 | 
|---|
| 903 |   for (PointMap::const_iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) {
 | 
|---|
| 904 |     point = PointRunner->second;
 | 
|---|
| 905 |     DoLog(1) && (Log() << Verbose(1) << "INFO: Current point is " << *point << "." << endl);
 | 
|---|
| 906 | 
 | 
|---|
| 907 |     // calculate mean concavity over all connected line
 | 
|---|
| 908 |     ConcavityPerLine = 0.;
 | 
|---|
| 909 |     for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
 | 
|---|
| 910 |       line = LineRunner->second;
 | 
|---|
| 911 |       //Log() << Verbose(1) << "INFO: Current line of point " << *point << " is " << *line << "." << endl;
 | 
|---|
| 912 |       ConcavityPerLine -= line->CalculateConvexity();
 | 
|---|
| 913 |     }
 | 
|---|
| 914 |     ConcavityPerLine /= point->lines.size();
 | 
|---|
| 915 | 
 | 
|---|
| 916 |     // weigh with total area of the surrounding triangles
 | 
|---|
| 917 |     totalarea  = 0.;
 | 
|---|
| 918 |     TriangleSet *triangles = TesselStruct->GetAllTriangles(PointRunner->second);
 | 
|---|
| 919 |     for (TriangleSet::iterator TriangleRunner = triangles->begin(); TriangleRunner != triangles->end(); ++TriangleRunner) {
 | 
|---|
| 920 |       totalarea += CalculateAreaofGeneralTriangle(*(*TriangleRunner)->endpoints[0]->node->node, *(*TriangleRunner)->endpoints[1]->node->node, *(*TriangleRunner)->endpoints[2]->node->node);
 | 
|---|
| 921 |     }
 | 
|---|
| 922 |     ConcavityPerLine *= totalarea;
 | 
|---|
| 923 | 
 | 
|---|
| 924 |     // calculate mean concavity over all attached triangles
 | 
|---|
| 925 |     ConcavityPerTriangle = 0.;
 | 
|---|
| 926 |     for (TriangleSet::const_iterator TriangleRunner = triangles->begin(); TriangleRunner != triangles->end(); ++TriangleRunner) {
 | 
|---|
| 927 |       line = (*TriangleRunner)->GetThirdLine(PointRunner->second);
 | 
|---|
| 928 |       triangle = line->GetOtherTriangle(*TriangleRunner);
 | 
|---|
| 929 |       area = CalculateAreaofGeneralTriangle(*triangle->endpoints[0]->node->node, *triangle->endpoints[1]->node->node, *triangle->endpoints[2]->node->node);
 | 
|---|
| 930 |       area += CalculateAreaofGeneralTriangle(*(*TriangleRunner)->endpoints[0]->node->node, *(*TriangleRunner)->endpoints[1]->node->node, *(*TriangleRunner)->endpoints[2]->node->node);
 | 
|---|
| 931 |       area *= -line->CalculateConvexity();
 | 
|---|
| 932 |       if (area > 0)
 | 
|---|
| 933 |         ConcavityPerTriangle += area;
 | 
|---|
| 934 | //      else
 | 
|---|
| 935 | //        ConcavityPerTriangle -= area;
 | 
|---|
| 936 |     }
 | 
|---|
| 937 |     ConcavityPerTriangle /= triangles->size()/totalarea;
 | 
|---|
| 938 |     delete(triangles);
 | 
|---|
| 939 | 
 | 
|---|
| 940 |     // add up
 | 
|---|
| 941 |     point->value = ConcavityPerLine + ConcavityPerTriangle;
 | 
|---|
| 942 |   }
 | 
|---|
| 943 | };
 | 
|---|
| 944 | 
 | 
|---|
| 945 | 
 | 
|---|
| 946 | 
 | 
|---|
| 947 | /** Calculates the concavity for each of the BoundaryPointSet's in a Tesselation.
 | 
|---|
| 948 |  * Sets BoundaryPointSet::value equal to the nearest distance to convex envelope.
 | 
|---|
| 949 |  * \param *out output stream for debugging
 | 
|---|
| 950 |  * \param *TesselStruct pointer to Tesselation structure
 | 
|---|
| 951 |  * \param *Convex pointer to convex Tesselation structure as reference
 | 
|---|
| 952 |  */
 | 
|---|
| 953 | void CalculateConstrictionPerBoundaryPoint(const Tesselation * const TesselStruct, const Tesselation * const Convex)
 | 
|---|
| 954 | {
 | 
|---|
| 955 |   Info FunctionInfo(__func__);
 | 
|---|
| 956 |   double distance = 0.;
 | 
|---|
| 957 | 
 | 
|---|
| 958 |   for (PointMap::const_iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) {
 | 
|---|
| 959 |     DoeLog(1) && (eLog() << Verbose(1) << "INFO: Current point is " << * PointRunner->second << "." << endl);
 | 
|---|
| 960 | 
 | 
|---|
| 961 |     distance = 0.;
 | 
|---|
| 962 |     for (TriangleMap::const_iterator TriangleRunner = Convex->TrianglesOnBoundary.begin(); TriangleRunner != Convex->TrianglesOnBoundary.end(); TriangleRunner++) {
 | 
|---|
| 963 |       const double CurrentDistance = Convex->GetDistanceSquaredToTriangle(*PointRunner->second->node->node, TriangleRunner->second);
 | 
|---|
| 964 |       if (CurrentDistance < distance)
 | 
|---|
| 965 |         distance = CurrentDistance;
 | 
|---|
| 966 |     }
 | 
|---|
| 967 | 
 | 
|---|
| 968 |     PointRunner->second->value = distance;
 | 
|---|
| 969 |   }
 | 
|---|
| 970 | };
 | 
|---|
| 971 | 
 | 
|---|
| 972 | /** Checks whether each BoundaryLineSet in the Tesselation has two triangles.
 | 
|---|
| 973 |  * \param *out output stream for debugging
 | 
|---|
| 974 |  * \param *TesselStruct
 | 
|---|
| 975 |  * \return true - all have exactly two triangles, false - some not, list is printed to screen
 | 
|---|
| 976 |  */
 | 
|---|
| 977 | bool CheckListOfBaselines(const Tesselation * const TesselStruct)
 | 
|---|
| 978 | {
 | 
|---|
| 979 |         Info FunctionInfo(__func__);
 | 
|---|
| 980 |   LineMap::const_iterator testline;
 | 
|---|
| 981 |   bool result = false;
 | 
|---|
| 982 |   int counter = 0;
 | 
|---|
| 983 | 
 | 
|---|
| 984 |   DoLog(1) && (Log() << Verbose(1) << "Check: List of Baselines with not two connected triangles:" << endl);
 | 
|---|
| 985 |   for (testline = TesselStruct->LinesOnBoundary.begin(); testline != TesselStruct->LinesOnBoundary.end(); testline++) {
 | 
|---|
| 986 |     if (testline->second->triangles.size() != 2) {
 | 
|---|
| 987 |       DoLog(2) && (Log() << Verbose(2) << *testline->second << "\t" << testline->second->triangles.size() << endl);
 | 
|---|
| 988 |       counter++;
 | 
|---|
| 989 |     }
 | 
|---|
| 990 |   }
 | 
|---|
| 991 |   if (counter == 0) {
 | 
|---|
| 992 |     DoLog(1) && (Log() << Verbose(1) << "None." << endl);
 | 
|---|
| 993 |     result = true;
 | 
|---|
| 994 |   }
 | 
|---|
| 995 |   return result;
 | 
|---|
| 996 | }
 | 
|---|
| 997 | 
 | 
|---|
| 998 | /** Counts the number of triangle pairs that contain the given polygon.
 | 
|---|
| 999 |  * \param *P polygon with endpoints to look for
 | 
|---|
| 1000 |  * \param *T set of triangles to create pairs from containing \a *P
 | 
|---|
| 1001 |  */
 | 
|---|
| 1002 | int CountTrianglePairContainingPolygon(const BoundaryPolygonSet * const P, const TriangleSet * const T)
 | 
|---|
| 1003 | {
 | 
|---|
| 1004 |   Info FunctionInfo(__func__);
 | 
|---|
| 1005 |   // check number of endpoints in *P
 | 
|---|
| 1006 |   if (P->endpoints.size() != 4) {
 | 
|---|
| 1007 |     DoeLog(1) && (eLog()<< Verbose(1) << "CountTrianglePairContainingPolygon works only on polygons with 4 nodes!" << endl);
 | 
|---|
| 1008 |     return 0;
 | 
|---|
| 1009 |   }
 | 
|---|
| 1010 | 
 | 
|---|
| 1011 |   // check number of triangles in *T
 | 
|---|
| 1012 |   if (T->size() < 2) {
 | 
|---|
| 1013 |     DoeLog(1) && (eLog()<< Verbose(1) << "Not enough triangles to have pairs!" << endl);
 | 
|---|
| 1014 |     return 0;
 | 
|---|
| 1015 |   }
 | 
|---|
| 1016 | 
 | 
|---|
| 1017 |   DoLog(0) && (Log() << Verbose(0) << "Polygon is " << *P << endl);
 | 
|---|
| 1018 |   // create each pair, get the endpoints and check whether *P is contained.
 | 
|---|
| 1019 |   int counter = 0;
 | 
|---|
| 1020 |   PointSet Trianglenodes;
 | 
|---|
| 1021 |   class BoundaryPolygonSet PairTrianglenodes;
 | 
|---|
| 1022 |   for(TriangleSet::iterator Walker = T->begin(); Walker != T->end(); Walker++) {
 | 
|---|
| 1023 |     for (int i=0;i<3;i++)
 | 
|---|
| 1024 |       Trianglenodes.insert((*Walker)->endpoints[i]);
 | 
|---|
| 1025 | 
 | 
|---|
| 1026 |     for(TriangleSet::iterator PairWalker = Walker; PairWalker != T->end(); PairWalker++) {
 | 
|---|
| 1027 |       if (Walker != PairWalker) { // skip first
 | 
|---|
| 1028 |         PairTrianglenodes.endpoints = Trianglenodes;
 | 
|---|
| 1029 |         for (int i=0;i<3;i++)
 | 
|---|
| 1030 |           PairTrianglenodes.endpoints.insert((*PairWalker)->endpoints[i]);
 | 
|---|
| 1031 |         const int size = PairTrianglenodes.endpoints.size();
 | 
|---|
| 1032 |         if (size == 4) {
 | 
|---|
| 1033 |           DoLog(0) && (Log() << Verbose(0) << " Current pair of triangles: " << **Walker << "," << **PairWalker << " with " << size << " distinct endpoints:" << PairTrianglenodes << endl);
 | 
|---|
| 1034 |           // now check
 | 
|---|
| 1035 |           if (PairTrianglenodes.ContainsPresentTupel(P)) {
 | 
|---|
| 1036 |             counter++;
 | 
|---|
| 1037 |             DoLog(0) && (Log() << Verbose(0) << "  ACCEPT: Matches with " << *P << endl);
 | 
|---|
| 1038 |           } else {
 | 
|---|
| 1039 |             DoLog(0) && (Log() << Verbose(0) << "  REJECT: No match with " << *P << endl);
 | 
|---|
| 1040 |           }
 | 
|---|
| 1041 |         } else {
 | 
|---|
| 1042 |           DoLog(0) && (Log() << Verbose(0) << "  REJECT: Less than four endpoints." << endl);
 | 
|---|
| 1043 |         }
 | 
|---|
| 1044 |       }
 | 
|---|
| 1045 |     }
 | 
|---|
| 1046 |     Trianglenodes.clear();
 | 
|---|
| 1047 |   }
 | 
|---|
| 1048 |   return counter;
 | 
|---|
| 1049 | };
 | 
|---|
| 1050 | 
 | 
|---|
| 1051 | /** Checks whether two give polygons have two or more points in common.
 | 
|---|
| 1052 |  * \param *P1 first polygon
 | 
|---|
| 1053 |  * \param *P2 second polygon
 | 
|---|
| 1054 |  * \return true - are connected, false = are note
 | 
|---|
| 1055 |  */
 | 
|---|
| 1056 | bool ArePolygonsEdgeConnected(const BoundaryPolygonSet * const P1, const BoundaryPolygonSet * const P2)
 | 
|---|
| 1057 | {
 | 
|---|
| 1058 |   Info FunctionInfo(__func__);
 | 
|---|
| 1059 |   int counter = 0;
 | 
|---|
| 1060 |   for(PointSet::const_iterator Runner = P1->endpoints.begin(); Runner != P1->endpoints.end(); Runner++) {
 | 
|---|
| 1061 |     if (P2->ContainsBoundaryPoint((*Runner))) {
 | 
|---|
| 1062 |       counter++;
 | 
|---|
| 1063 |       DoLog(1) && (Log() << Verbose(1) << *(*Runner) << " of second polygon is found in the first one." << endl);
 | 
|---|
| 1064 |       return true;
 | 
|---|
| 1065 |     }
 | 
|---|
| 1066 |   }
 | 
|---|
| 1067 |   return false;
 | 
|---|
| 1068 | };
 | 
|---|
| 1069 | 
 | 
|---|
| 1070 | /** Combines second into the first and deletes the second.
 | 
|---|
| 1071 |  * \param *P1 first polygon, contains all nodes on return
 | 
|---|
| 1072 |  * \param *&P2 second polygon, is deleted.
 | 
|---|
| 1073 |  */
 | 
|---|
| 1074 | void CombinePolygons(BoundaryPolygonSet * const P1, BoundaryPolygonSet * &P2)
 | 
|---|
| 1075 | {
 | 
|---|
| 1076 |   Info FunctionInfo(__func__);
 | 
|---|
| 1077 |   pair <PointSet::iterator, bool> Tester;
 | 
|---|
| 1078 |   for(PointSet::iterator Runner = P2->endpoints.begin(); Runner != P2->endpoints.end(); Runner++) {
 | 
|---|
| 1079 |     Tester = P1->endpoints.insert((*Runner));
 | 
|---|
| 1080 |     if (Tester.second)
 | 
|---|
| 1081 |       DoLog(0) && (Log() << Verbose(0) << "Inserting endpoint " << *(*Runner) << " into first polygon." << endl);
 | 
|---|
| 1082 |   }
 | 
|---|
| 1083 |   P2->endpoints.clear();
 | 
|---|
| 1084 |   delete(P2);
 | 
|---|
| 1085 | };
 | 
|---|
| 1086 | 
 | 
|---|