| [357fba] | 1 | /*
 | 
|---|
 | 2 |  * TesselationHelpers.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Aug 3, 2009
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [f66195] | 8 | #include <fstream>
 | 
|---|
 | 9 | 
 | 
|---|
| [f67b6e] | 10 | #include "info.hpp"
 | 
|---|
| [f66195] | 11 | #include "linkedcell.hpp"
 | 
|---|
| [e138de] | 12 | #include "log.hpp"
 | 
|---|
| [f66195] | 13 | #include "tesselation.hpp"
 | 
|---|
| [357fba] | 14 | #include "tesselationhelpers.hpp"
 | 
|---|
| [f66195] | 15 | #include "vector.hpp"
 | 
|---|
| [0a4f7f] | 16 | #include "vector_ops.hpp"
 | 
|---|
| [f66195] | 17 | #include "verbose.hpp"
 | 
|---|
| [d4c9ae] | 18 | #include "Plane.hpp"
 | 
|---|
| [357fba] | 19 | 
 | 
|---|
| [f67b6e] | 20 | double DetGet(gsl_matrix * const A, const int inPlace)
 | 
|---|
 | 21 | {
 | 
|---|
 | 22 |         Info FunctionInfo(__func__);
 | 
|---|
| [357fba] | 23 |   /*
 | 
|---|
 | 24 |   inPlace = 1 => A is replaced with the LU decomposed copy.
 | 
|---|
 | 25 |   inPlace = 0 => A is retained, and a copy is used for LU.
 | 
|---|
 | 26 |   */
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 |   double det;
 | 
|---|
 | 29 |   int signum;
 | 
|---|
 | 30 |   gsl_permutation *p = gsl_permutation_alloc(A->size1);
 | 
|---|
| [24a5e0] | 31 |   gsl_matrix *tmpA=0;
 | 
|---|
| [357fba] | 32 | 
 | 
|---|
 | 33 |   if (inPlace)
 | 
|---|
 | 34 |   tmpA = A;
 | 
|---|
 | 35 |   else {
 | 
|---|
 | 36 |   gsl_matrix *tmpA = gsl_matrix_alloc(A->size1, A->size2);
 | 
|---|
 | 37 |   gsl_matrix_memcpy(tmpA , A);
 | 
|---|
 | 38 |   }
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 |   gsl_linalg_LU_decomp(tmpA , p , &signum);
 | 
|---|
 | 42 |   det = gsl_linalg_LU_det(tmpA , signum);
 | 
|---|
 | 43 |   gsl_permutation_free(p);
 | 
|---|
 | 44 |   if (! inPlace)
 | 
|---|
 | 45 |   gsl_matrix_free(tmpA);
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |   return det;
 | 
|---|
 | 48 | };
 | 
|---|
 | 49 | 
 | 
|---|
| [c0f6c6] | 50 | void GetSphere(Vector * const center, const Vector &a, const Vector &b, const Vector &c, const double RADIUS)
 | 
|---|
| [357fba] | 51 | {
 | 
|---|
| [f67b6e] | 52 |         Info FunctionInfo(__func__);
 | 
|---|
| [357fba] | 53 |   gsl_matrix *A = gsl_matrix_calloc(3,3);
 | 
|---|
 | 54 |   double m11, m12, m13, m14;
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |   for(int i=0;i<3;i++) {
 | 
|---|
| [0a4f7f] | 57 |     gsl_matrix_set(A, i, 0, a[i]);
 | 
|---|
 | 58 |     gsl_matrix_set(A, i, 1, b[i]);
 | 
|---|
 | 59 |     gsl_matrix_set(A, i, 2, c[i]);
 | 
|---|
| [357fba] | 60 |   }
 | 
|---|
| [f1cccd] | 61 |   m11 = DetGet(A, 1);
 | 
|---|
| [357fba] | 62 | 
 | 
|---|
 | 63 |   for(int i=0;i<3;i++) {
 | 
|---|
| [0a4f7f] | 64 |     gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
 | 
|---|
 | 65 |     gsl_matrix_set(A, i, 1, b[i]);
 | 
|---|
 | 66 |     gsl_matrix_set(A, i, 2, c[i]);
 | 
|---|
| [357fba] | 67 |   }
 | 
|---|
| [f1cccd] | 68 |   m12 = DetGet(A, 1);
 | 
|---|
| [357fba] | 69 | 
 | 
|---|
 | 70 |   for(int i=0;i<3;i++) {
 | 
|---|
| [0a4f7f] | 71 |     gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
 | 
|---|
 | 72 |     gsl_matrix_set(A, i, 1, a[i]);
 | 
|---|
 | 73 |     gsl_matrix_set(A, i, 2, c[i]);
 | 
|---|
| [357fba] | 74 |   }
 | 
|---|
| [f1cccd] | 75 |   m13 = DetGet(A, 1);
 | 
|---|
| [357fba] | 76 | 
 | 
|---|
 | 77 |   for(int i=0;i<3;i++) {
 | 
|---|
| [0a4f7f] | 78 |     gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
 | 
|---|
 | 79 |     gsl_matrix_set(A, i, 1, a[i]);
 | 
|---|
 | 80 |     gsl_matrix_set(A, i, 2, b[i]);
 | 
|---|
| [357fba] | 81 |   }
 | 
|---|
| [f1cccd] | 82 |   m14 = DetGet(A, 1);
 | 
|---|
| [357fba] | 83 | 
 | 
|---|
 | 84 |   if (fabs(m11) < MYEPSILON)
 | 
|---|
| [58ed4a] | 85 |     DoeLog(1) && (eLog()<< Verbose(1) << "three points are colinear." << endl);
 | 
|---|
| [357fba] | 86 | 
 | 
|---|
| [0a4f7f] | 87 |   center->at(0) =  0.5 * m12/ m11;
 | 
|---|
 | 88 |   center->at(1) = -0.5 * m13/ m11;
 | 
|---|
 | 89 |   center->at(2) =  0.5 * m14/ m11;
 | 
|---|
| [357fba] | 90 | 
 | 
|---|
| [1513a74] | 91 |   if (fabs(a.distance(*center) - RADIUS) > MYEPSILON)
 | 
|---|
 | 92 |     DoeLog(1) && (eLog()<< Verbose(1) << "The given center is further way by " << fabs(a.distance(*center) - RADIUS) << " from a than RADIUS." << endl);
 | 
|---|
| [357fba] | 93 | 
 | 
|---|
 | 94 |   gsl_matrix_free(A);
 | 
|---|
 | 95 | };
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | /**
 | 
|---|
 | 100 |  * Function returns center of sphere with RADIUS, which rests on points a, b, c
 | 
|---|
 | 101 |  * @param Center this vector will be used for return
 | 
|---|
 | 102 |  * @param a vector first point of triangle
 | 
|---|
 | 103 |  * @param b vector second point of triangle
 | 
|---|
 | 104 |  * @param c vector third point of triangle
 | 
|---|
| [c0f6c6] | 105 |  * @param *Umkreismittelpunkt new center point of circumference
 | 
|---|
| [357fba] | 106 |  * @param Direction vector indicates up/down
 | 
|---|
| [c0f6c6] | 107 |  * @param AlternativeDirection Vector, needed in case the triangles have 90 deg angle
 | 
|---|
| [357fba] | 108 |  * @param Halfplaneindicator double indicates whether Direction is up or down
 | 
|---|
| [c0f6c6] | 109 |  * @param AlternativeIndicator double indicates in case of orthogonal triangles which direction of AlternativeDirection is suitable
 | 
|---|
| [357fba] | 110 |  * @param alpha double angle at a
 | 
|---|
 | 111 |  * @param beta double, angle at b
 | 
|---|
 | 112 |  * @param gamma, double, angle at c
 | 
|---|
 | 113 |  * @param Radius, double
 | 
|---|
 | 114 |  * @param Umkreisradius double radius of circumscribing circle
 | 
|---|
 | 115 |  */
 | 
|---|
| [c0f6c6] | 116 | 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,
 | 
|---|
 | 117 |     const double HalfplaneIndicator, const double AlternativeIndicator, const double alpha, const double beta, const double gamma, const double RADIUS, const double Umkreisradius)
 | 
|---|
| [357fba] | 118 | {
 | 
|---|
| [f67b6e] | 119 |         Info FunctionInfo(__func__);
 | 
|---|
| [357fba] | 120 |   Vector TempNormal, helper;
 | 
|---|
 | 121 |   double Restradius;
 | 
|---|
 | 122 |   Vector OtherCenter;
 | 
|---|
 | 123 |   Center->Zero();
 | 
|---|
| [273382] | 124 |   helper = sin(2.*alpha) * a;
 | 
|---|
 | 125 |   (*Center) += helper;
 | 
|---|
 | 126 |   helper = sin(2.*beta) * b;
 | 
|---|
 | 127 |   (*Center) += helper;
 | 
|---|
 | 128 |   helper = sin(2.*gamma) * c;
 | 
|---|
 | 129 |   (*Center) += helper;
 | 
|---|
| [357fba] | 130 |   //*Center = a * sin(2.*alpha) + b * sin(2.*beta) + c * sin(2.*gamma) ;
 | 
|---|
 | 131 |   Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
 | 
|---|
| [273382] | 132 |   (*NewUmkreismittelpunkt) = (*Center);
 | 
|---|
| [a67d19] | 133 |   DoLog(1) && (Log() << Verbose(1) << "Center of new circumference is " << *NewUmkreismittelpunkt << ".\n");
 | 
|---|
| [357fba] | 134 |   // Here we calculated center of circumscribing circle, using barycentric coordinates
 | 
|---|
| [a67d19] | 135 |   DoLog(1) && (Log() << Verbose(1) << "Center of circumference is " << *Center << " in direction " << *Direction << ".\n");
 | 
|---|
| [357fba] | 136 | 
 | 
|---|
| [273382] | 137 |   TempNormal = a - b;
 | 
|---|
 | 138 |   helper = a - c;
 | 
|---|
 | 139 |   TempNormal.VectorProduct(helper);
 | 
|---|
| [357fba] | 140 |   if (fabs(HalfplaneIndicator) < MYEPSILON)
 | 
|---|
 | 141 |     {
 | 
|---|
| [273382] | 142 |       if ((TempNormal.ScalarProduct(*AlternativeDirection) <0 && AlternativeIndicator >0) || (TempNormal.ScalarProduct(*AlternativeDirection) >0 && AlternativeIndicator <0))
 | 
|---|
| [357fba] | 143 |         {
 | 
|---|
| [273382] | 144 |           TempNormal *= -1;
 | 
|---|
| [357fba] | 145 |         }
 | 
|---|
 | 146 |     }
 | 
|---|
 | 147 |   else
 | 
|---|
 | 148 |     {
 | 
|---|
| [273382] | 149 |       if (((TempNormal.ScalarProduct(*Direction)<0) && (HalfplaneIndicator >0)) || ((TempNormal.ScalarProduct(*Direction)>0) && (HalfplaneIndicator<0)))
 | 
|---|
| [357fba] | 150 |         {
 | 
|---|
| [273382] | 151 |           TempNormal *= -1;
 | 
|---|
| [357fba] | 152 |         }
 | 
|---|
 | 153 |     }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |   TempNormal.Normalize();
 | 
|---|
 | 156 |   Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius);
 | 
|---|
| [a67d19] | 157 |   DoLog(1) && (Log() << Verbose(1) << "Height of center of circumference to center of sphere is " << Restradius << ".\n");
 | 
|---|
| [357fba] | 158 |   TempNormal.Scale(Restradius);
 | 
|---|
| [a67d19] | 159 |   DoLog(1) && (Log() << Verbose(1) << "Shift vector to sphere of circumference is " << TempNormal << ".\n");
 | 
|---|
| [273382] | 160 |   (*Center) += TempNormal;
 | 
|---|
| [a67d19] | 161 |   DoLog(1) && (Log() << Verbose(1) << "Center of sphere of circumference is " << *Center << ".\n");
 | 
|---|
| [f1cccd] | 162 |   GetSphere(&OtherCenter, a, b, c, RADIUS);
 | 
|---|
| [a67d19] | 163 |   DoLog(1) && (Log() << Verbose(1) << "OtherCenter of sphere of circumference is " << OtherCenter << ".\n");
 | 
|---|
| [357fba] | 164 | };
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 | /** Constructs the center of the circumcircle defined by three points \a *a, \a *b and \a *c.
 | 
|---|
 | 168 |  * \param *Center new center on return
 | 
|---|
 | 169 |  * \param *a first point
 | 
|---|
 | 170 |  * \param *b second point
 | 
|---|
 | 171 |  * \param *c third point
 | 
|---|
 | 172 |  */
 | 
|---|
| [c0f6c6] | 173 | void GetCenterofCircumcircle(Vector * const Center, const Vector &a, const Vector &b, const Vector &c)
 | 
|---|
| [357fba] | 174 | {
 | 
|---|
| [f67b6e] | 175 |         Info FunctionInfo(__func__);
 | 
|---|
| [357fba] | 176 |   Vector helper;
 | 
|---|
 | 177 |   double alpha, beta, gamma;
 | 
|---|
| [273382] | 178 |   Vector SideA = b - c;
 | 
|---|
 | 179 |   Vector SideB = c - a;
 | 
|---|
 | 180 |   Vector SideC = a - b;
 | 
|---|
 | 181 |   alpha = M_PI - SideB.Angle(SideC);
 | 
|---|
 | 182 |   beta = M_PI - SideC.Angle(SideA);
 | 
|---|
 | 183 |   gamma = M_PI - SideA.Angle(SideB);
 | 
|---|
| [f67b6e] | 184 |   //Log() << Verbose(1) << "INFO: alpha = " << alpha/M_PI*180. << ", beta = " << beta/M_PI*180. << ", gamma = " << gamma/M_PI*180. << "." << endl;
 | 
|---|
| [e359a8] | 185 |   if (fabs(M_PI - alpha - beta - gamma) > HULLEPSILON) {
 | 
|---|
| [299554] | 186 |     DoeLog(2) && (eLog()<< Verbose(2) << "GetCenterofCircumcircle: Sum of angles " << (alpha+beta+gamma)/M_PI*180. << " > 180 degrees by " << fabs(M_PI - alpha - beta - gamma)/M_PI*180. << "!" << endl);
 | 
|---|
| [e359a8] | 187 |   }
 | 
|---|
| [357fba] | 188 | 
 | 
|---|
 | 189 |   Center->Zero();
 | 
|---|
| [273382] | 190 |   helper = sin(2.*alpha) * a;
 | 
|---|
 | 191 |   (*Center) += helper;
 | 
|---|
 | 192 |   helper = sin(2.*beta) * b;
 | 
|---|
 | 193 |   (*Center) += helper;
 | 
|---|
 | 194 |   helper = sin(2.*gamma) * c;
 | 
|---|
 | 195 |   (*Center) += helper;
 | 
|---|
| [357fba] | 196 |   Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
 | 
|---|
 | 197 | };
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 | /** 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.
 | 
|---|
 | 200 |  * Test whether the \a NewSphereCenter is really on the given plane and in distance \a CircleRadius from \a CircleCenter.
 | 
|---|
 | 201 |  * It calculates the angle, making it unique on [0,2.*M_PI) by comparing to SearchDirection.
 | 
|---|
 | 202 |  * 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).
 | 
|---|
 | 203 |  * \param CircleCenter Center of the parameter circle
 | 
|---|
 | 204 |  * \param CirclePlaneNormal normal vector to plane of the parameter circle
 | 
|---|
 | 205 |  * \param CircleRadius radius of the parameter circle
 | 
|---|
 | 206 |  * \param NewSphereCenter new center of a circumcircle
 | 
|---|
 | 207 |  * \param OldSphereCenter old center of a circumcircle, defining the zero "path length" on the parameter circle
 | 
|---|
 | 208 |  * \param NormalVector normal vector
 | 
|---|
 | 209 |  * \param SearchDirection search direction to make angle unique on return.
 | 
|---|
 | 210 |  * \return Angle between \a NewSphereCenter and \a OldSphereCenter relative to \a CircleCenter, 2.*M_PI if one test fails
 | 
|---|
 | 211 |  */
 | 
|---|
| [c0f6c6] | 212 | double GetPathLengthonCircumCircle(const Vector &CircleCenter, const Vector &CirclePlaneNormal, const double CircleRadius, const Vector &NewSphereCenter, const Vector &OldSphereCenter, const Vector &NormalVector, const Vector &SearchDirection)
 | 
|---|
| [357fba] | 213 | {
 | 
|---|
| [f67b6e] | 214 |         Info FunctionInfo(__func__);
 | 
|---|
| [357fba] | 215 |   Vector helper;
 | 
|---|
 | 216 |   double radius, alpha;
 | 
|---|
| [273382] | 217 | 
 | 
|---|
 | 218 |   Vector RelativeOldSphereCenter = OldSphereCenter - CircleCenter;
 | 
|---|
 | 219 |   Vector RelativeNewSphereCenter = NewSphereCenter - CircleCenter;
 | 
|---|
 | 220 |   helper = RelativeNewSphereCenter;
 | 
|---|
| [357fba] | 221 |   // test whether new center is on the parameter circle's plane
 | 
|---|
| [273382] | 222 |   if (fabs(helper.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) {
 | 
|---|
| [8cbb97] | 223 |     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);
 | 
|---|
| [273382] | 224 |     helper.ProjectOntoPlane(CirclePlaneNormal);
 | 
|---|
| [357fba] | 225 |   }
 | 
|---|
| [b998c3] | 226 |   radius = helper.NormSquared();
 | 
|---|
| [357fba] | 227 |   // test whether the new center vector has length of CircleRadius
 | 
|---|
 | 228 |   if (fabs(radius - CircleRadius) > HULLEPSILON)
 | 
|---|
| [58ed4a] | 229 |     DoeLog(1) && (eLog()<< Verbose(1) << "The projected center of the new sphere has radius " << radius << " instead of " << CircleRadius << "." << endl);
 | 
|---|
| [273382] | 230 |   alpha = helper.Angle(RelativeOldSphereCenter);
 | 
|---|
| [357fba] | 231 |   // make the angle unique by checking the halfplanes/search direction
 | 
|---|
| [273382] | 232 |   if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON)  // acos is not unique on [0, 2.*M_PI), hence extra check to decide between two half intervals
 | 
|---|
| [357fba] | 233 |     alpha = 2.*M_PI - alpha;
 | 
|---|
| [a67d19] | 234 |   DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeNewSphereCenter is " << helper << ", RelativeOldSphereCenter is " << RelativeOldSphereCenter << " and resulting angle is " << alpha << "." << endl);
 | 
|---|
| [1513a74] | 235 |   radius = helper.distance(RelativeOldSphereCenter);
 | 
|---|
| [273382] | 236 |   helper.ProjectOntoPlane(NormalVector);
 | 
|---|
| [357fba] | 237 |   // check whether new center is somewhat away or at least right over the current baseline to prevent intersecting triangles
 | 
|---|
 | 238 |   if ((radius > HULLEPSILON) || (helper.Norm() < HULLEPSILON)) {
 | 
|---|
| [a67d19] | 239 |     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);
 | 
|---|
| [357fba] | 240 |     return alpha;
 | 
|---|
 | 241 |   } else {
 | 
|---|
| [a67d19] | 242 |     DoLog(1) && (Log() << Verbose(1) << "INFO: NewSphereCenter " << RelativeNewSphereCenter << " is too close to RelativeOldSphereCenter" << RelativeOldSphereCenter << "." << endl);
 | 
|---|
| [357fba] | 243 |     return 2.*M_PI;
 | 
|---|
 | 244 |   }
 | 
|---|
 | 245 | };
 | 
|---|
 | 246 | 
 | 
|---|
 | 247 | struct Intersection {
 | 
|---|
 | 248 |   Vector x1;
 | 
|---|
 | 249 |   Vector x2;
 | 
|---|
 | 250 |   Vector x3;
 | 
|---|
 | 251 |   Vector x4;
 | 
|---|
 | 252 | };
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 | /**
 | 
|---|
 | 255 |  * Intersection calculation function.
 | 
|---|
 | 256 |  *
 | 
|---|
 | 257 |  * @param x to find the result for
 | 
|---|
 | 258 |  * @param function parameter
 | 
|---|
 | 259 |  */
 | 
|---|
 | 260 | double MinIntersectDistance(const gsl_vector * x, void *params)
 | 
|---|
 | 261 | {
 | 
|---|
| [f67b6e] | 262 |         Info FunctionInfo(__func__);
 | 
|---|
| [357fba] | 263 |   double retval = 0;
 | 
|---|
 | 264 |   struct Intersection *I = (struct Intersection *)params;
 | 
|---|
 | 265 |   Vector intersection;
 | 
|---|
 | 266 |   for (int i=0;i<NDIM;i++)
 | 
|---|
| [0a4f7f] | 267 |     intersection[i] = gsl_vector_get(x, i);
 | 
|---|
| [357fba] | 268 | 
 | 
|---|
| [273382] | 269 |   Vector SideA = I->x1 -I->x2 ;
 | 
|---|
 | 270 |   Vector HeightA = intersection - I->x1;
 | 
|---|
 | 271 |   HeightA.ProjectOntoPlane(SideA);
 | 
|---|
| [357fba] | 272 | 
 | 
|---|
| [273382] | 273 |   Vector SideB = I->x3 - I->x4;
 | 
|---|
 | 274 |   Vector HeightB = intersection - I->x3;
 | 
|---|
 | 275 |   HeightB.ProjectOntoPlane(SideB);
 | 
|---|
| [357fba] | 276 | 
 | 
|---|
| [273382] | 277 |   retval = HeightA.ScalarProduct(HeightA) + HeightB.ScalarProduct(HeightB);
 | 
|---|
| [f67b6e] | 278 |   //Log() << Verbose(1) << "MinIntersectDistance called, result: " << retval << endl;
 | 
|---|
| [357fba] | 279 | 
 | 
|---|
 | 280 |   return retval;
 | 
|---|
 | 281 | };
 | 
|---|
 | 282 | 
 | 
|---|
 | 283 | 
 | 
|---|
 | 284 | /**
 | 
|---|
 | 285 |  * Calculates whether there is an intersection between two lines. The first line
 | 
|---|
 | 286 |  * always goes through point 1 and point 2 and the second line is given by the
 | 
|---|
 | 287 |  * connection between point 4 and point 5.
 | 
|---|
 | 288 |  *
 | 
|---|
 | 289 |  * @param point 1 of line 1
 | 
|---|
 | 290 |  * @param point 2 of line 1
 | 
|---|
 | 291 |  * @param point 1 of line 2
 | 
|---|
 | 292 |  * @param point 2 of line 2
 | 
|---|
 | 293 |  *
 | 
|---|
 | 294 |  * @return true if there is an intersection between the given lines, false otherwise
 | 
|---|
 | 295 |  */
 | 
|---|
| [c0f6c6] | 296 | bool existsIntersection(const Vector &point1, const Vector &point2, const Vector &point3, const Vector &point4)
 | 
|---|
| [357fba] | 297 | {
 | 
|---|
| [f67b6e] | 298 |         Info FunctionInfo(__func__);
 | 
|---|
| [357fba] | 299 |   bool result;
 | 
|---|
 | 300 | 
 | 
|---|
 | 301 |   struct Intersection par;
 | 
|---|
| [273382] | 302 |     par.x1 = point1;
 | 
|---|
 | 303 |     par.x2 = point2;
 | 
|---|
 | 304 |     par.x3 = point3;
 | 
|---|
 | 305 |     par.x4 = point4;
 | 
|---|
| [357fba] | 306 | 
 | 
|---|
 | 307 |     const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
 | 
|---|
 | 308 |     gsl_multimin_fminimizer *s = NULL;
 | 
|---|
 | 309 |     gsl_vector *ss, *x;
 | 
|---|
| [f1cccd] | 310 |     gsl_multimin_function minexFunction;
 | 
|---|
| [357fba] | 311 | 
 | 
|---|
 | 312 |     size_t iter = 0;
 | 
|---|
 | 313 |     int status;
 | 
|---|
 | 314 |     double size;
 | 
|---|
 | 315 | 
 | 
|---|
 | 316 |     /* Starting point */
 | 
|---|
 | 317 |     x = gsl_vector_alloc(NDIM);
 | 
|---|
| [0a4f7f] | 318 |     gsl_vector_set(x, 0, point1[0]);
 | 
|---|
 | 319 |     gsl_vector_set(x, 1, point1[1]);
 | 
|---|
 | 320 |     gsl_vector_set(x, 2, point1[2]);
 | 
|---|
| [357fba] | 321 | 
 | 
|---|
 | 322 |     /* Set initial step sizes to 1 */
 | 
|---|
 | 323 |     ss = gsl_vector_alloc(NDIM);
 | 
|---|
 | 324 |     gsl_vector_set_all(ss, 1.0);
 | 
|---|
 | 325 | 
 | 
|---|
 | 326 |     /* Initialize method and iterate */
 | 
|---|
| [f1cccd] | 327 |     minexFunction.n = NDIM;
 | 
|---|
 | 328 |     minexFunction.f = &MinIntersectDistance;
 | 
|---|
 | 329 |     minexFunction.params = (void *)∥
 | 
|---|
| [357fba] | 330 | 
 | 
|---|
 | 331 |     s = gsl_multimin_fminimizer_alloc(T, NDIM);
 | 
|---|
| [f1cccd] | 332 |     gsl_multimin_fminimizer_set(s, &minexFunction, x, ss);
 | 
|---|
| [357fba] | 333 | 
 | 
|---|
 | 334 |     do {
 | 
|---|
 | 335 |         iter++;
 | 
|---|
 | 336 |         status = gsl_multimin_fminimizer_iterate(s);
 | 
|---|
 | 337 | 
 | 
|---|
 | 338 |         if (status) {
 | 
|---|
 | 339 |           break;
 | 
|---|
 | 340 |         }
 | 
|---|
 | 341 | 
 | 
|---|
 | 342 |         size = gsl_multimin_fminimizer_size(s);
 | 
|---|
 | 343 |         status = gsl_multimin_test_size(size, 1e-2);
 | 
|---|
 | 344 | 
 | 
|---|
 | 345 |         if (status == GSL_SUCCESS) {
 | 
|---|
| [a67d19] | 346 |           DoLog(1) && (Log() << Verbose(1) << "converged to minimum" <<  endl);
 | 
|---|
| [357fba] | 347 |         }
 | 
|---|
 | 348 |     } while (status == GSL_CONTINUE && iter < 100);
 | 
|---|
 | 349 | 
 | 
|---|
 | 350 |     // check whether intersection is in between or not
 | 
|---|
| [273382] | 351 |   Vector intersection;
 | 
|---|
| [357fba] | 352 |   double t1, t2;
 | 
|---|
 | 353 |   for (int i = 0; i < NDIM; i++) {
 | 
|---|
| [0a4f7f] | 354 |     intersection[i] = gsl_vector_get(s->x, i);
 | 
|---|
| [357fba] | 355 |   }
 | 
|---|
 | 356 | 
 | 
|---|
| [273382] | 357 |   Vector SideA = par.x2 - par.x1;
 | 
|---|
 | 358 |   Vector HeightA = intersection - par.x1;
 | 
|---|
| [357fba] | 359 | 
 | 
|---|
| [273382] | 360 |   t1 = HeightA.ScalarProduct(SideA)/SideA.ScalarProduct(SideA);
 | 
|---|
| [357fba] | 361 | 
 | 
|---|
| [273382] | 362 |   Vector SideB = par.x4 - par.x3;
 | 
|---|
 | 363 |   Vector HeightB = intersection - par.x3;
 | 
|---|
| [357fba] | 364 | 
 | 
|---|
| [273382] | 365 |   t2 = HeightB.ScalarProduct(SideB)/SideB.ScalarProduct(SideB);
 | 
|---|
| [357fba] | 366 | 
 | 
|---|
| [f67b6e] | 367 |   Log() << Verbose(1) << "Intersection " << intersection << " is at "
 | 
|---|
| [357fba] | 368 |     << t1 << " for (" << point1 << "," << point2 << ") and at "
 | 
|---|
 | 369 |     << t2 << " for (" << point3 << "," << point4 << "): ";
 | 
|---|
 | 370 | 
 | 
|---|
 | 371 |   if (((t1 >= 0) && (t1 <= 1)) && ((t2 >= 0) && (t2 <= 1))) {
 | 
|---|
| [a67d19] | 372 |     DoLog(1) && (Log() << Verbose(1) << "true intersection." << endl);
 | 
|---|
| [357fba] | 373 |     result = true;
 | 
|---|
 | 374 |   } else {
 | 
|---|
| [a67d19] | 375 |     DoLog(1) && (Log() << Verbose(1) << "intersection out of region of interest." << endl);
 | 
|---|
| [357fba] | 376 |     result = false;
 | 
|---|
 | 377 |   }
 | 
|---|
 | 378 | 
 | 
|---|
 | 379 |   // free minimizer stuff
 | 
|---|
 | 380 |     gsl_vector_free(x);
 | 
|---|
 | 381 |     gsl_vector_free(ss);
 | 
|---|
 | 382 |     gsl_multimin_fminimizer_free(s);
 | 
|---|
 | 383 | 
 | 
|---|
 | 384 |   return result;
 | 
|---|
| [91e7e4a] | 385 | };
 | 
|---|
 | 386 | 
 | 
|---|
| [57066a] | 387 | /** Gets the angle between a point and a reference relative to the provided center.
 | 
|---|
 | 388 |  * We have two shanks point and reference between which the angle is calculated
 | 
|---|
 | 389 |  * and by scalar product with OrthogonalVector we decide the interval.
 | 
|---|
 | 390 |  * @param point to calculate the angle for
 | 
|---|
 | 391 |  * @param reference to which to calculate the angle
 | 
|---|
 | 392 |  * @param OrthogonalVector points in direction of [pi,2pi] interval
 | 
|---|
 | 393 |  *
 | 
|---|
 | 394 |  * @return angle between point and reference
 | 
|---|
 | 395 |  */
 | 
|---|
| [c0f6c6] | 396 | double GetAngle(const Vector &point, const Vector &reference, const Vector &OrthogonalVector)
 | 
|---|
| [57066a] | 397 | {
 | 
|---|
| [f67b6e] | 398 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 399 |   if (reference.IsZero())
 | 
|---|
 | 400 |     return M_PI;
 | 
|---|
 | 401 | 
 | 
|---|
 | 402 |   // calculate both angles and correct with in-plane vector
 | 
|---|
 | 403 |   if (point.IsZero())
 | 
|---|
 | 404 |     return M_PI;
 | 
|---|
| [273382] | 405 |   double phi = point.Angle(reference);
 | 
|---|
 | 406 |   if (OrthogonalVector.ScalarProduct(point) > 0) {
 | 
|---|
| [57066a] | 407 |     phi = 2.*M_PI - phi;
 | 
|---|
 | 408 |   }
 | 
|---|
 | 409 | 
 | 
|---|
| [a67d19] | 410 |   DoLog(1) && (Log() << Verbose(1) << "INFO: " << point << " has angle " << phi << " with respect to reference " << reference << "." << endl);
 | 
|---|
| [57066a] | 411 | 
 | 
|---|
 | 412 |   return phi;
 | 
|---|
 | 413 | }
 | 
|---|
 | 414 | 
 | 
|---|
| [91e7e4a] | 415 | 
 | 
|---|
 | 416 | /** Calculates the volume of a general tetraeder.
 | 
|---|
 | 417 |  * \param *a first vector
 | 
|---|
 | 418 |  * \param *a first vector
 | 
|---|
 | 419 |  * \param *a first vector
 | 
|---|
 | 420 |  * \param *a first vector
 | 
|---|
 | 421 |  * \return \f$ \frac{1}{6} \cdot ((a-d) \times (a-c) \cdot  (a-b)) \f$
 | 
|---|
 | 422 |  */
 | 
|---|
| [c0f6c6] | 423 | double CalculateVolumeofGeneralTetraeder(const Vector &a, const Vector &b, const Vector &c, const Vector &d)
 | 
|---|
| [91e7e4a] | 424 | {
 | 
|---|
| [f67b6e] | 425 |         Info FunctionInfo(__func__);
 | 
|---|
| [91e7e4a] | 426 |   Vector Point, TetraederVector[3];
 | 
|---|
 | 427 |   double volume;
 | 
|---|
 | 428 | 
 | 
|---|
| [1bd79e] | 429 |   TetraederVector[0] = a;
 | 
|---|
 | 430 |   TetraederVector[1] = b;
 | 
|---|
 | 431 |   TetraederVector[2] = c;
 | 
|---|
| [91e7e4a] | 432 |   for (int j=0;j<3;j++)
 | 
|---|
| [273382] | 433 |     TetraederVector[j].SubtractVector(d);
 | 
|---|
| [1bd79e] | 434 |   Point = TetraederVector[0];
 | 
|---|
| [273382] | 435 |   Point.VectorProduct(TetraederVector[1]);
 | 
|---|
 | 436 |   volume = 1./6. * fabs(Point.ScalarProduct(TetraederVector[2]));
 | 
|---|
| [91e7e4a] | 437 |   return volume;
 | 
|---|
 | 438 | };
 | 
|---|
| [357fba] | 439 | 
 | 
|---|
| [57066a] | 440 | 
 | 
|---|
 | 441 | /** Checks for a new special triangle whether one of its edges is already present with one one triangle connected.
 | 
|---|
 | 442 |  * This enforces that special triangles (i.e. degenerated ones) should at last close the open-edge frontier and not
 | 
|---|
 | 443 |  * make it bigger (i.e. closing one (the baseline) and opening two new ones).
 | 
|---|
 | 444 |  * \param TPS[3] nodes of the triangle
 | 
|---|
 | 445 |  * \return true - there is such a line (i.e. creation of degenerated triangle is valid), false - no such line (don't create)
 | 
|---|
 | 446 |  */
 | 
|---|
| [c0f6c6] | 447 | bool CheckLineCriteriaForDegeneratedTriangle(const BoundaryPointSet * const nodes[3])
 | 
|---|
| [57066a] | 448 | {
 | 
|---|
| [f67b6e] | 449 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 450 |   bool result = false;
 | 
|---|
 | 451 |   int counter = 0;
 | 
|---|
 | 452 | 
 | 
|---|
 | 453 |   // check all three points
 | 
|---|
 | 454 |   for (int i=0;i<3;i++)
 | 
|---|
 | 455 |     for (int j=i+1; j<3; j++) {
 | 
|---|
| [f1ef60a] | 456 |       if (nodes[i] == NULL) {
 | 
|---|
| [a67d19] | 457 |         DoLog(1) && (Log() << Verbose(1) << "Node nr. " << i << " is not yet present." << endl);
 | 
|---|
| [f1ef60a] | 458 |         result = true;
 | 
|---|
 | 459 |       } else if (nodes[i]->lines.find(nodes[j]->node->nr) != nodes[i]->lines.end()) {  // there already is a line
 | 
|---|
| [776b64] | 460 |         LineMap::const_iterator FindLine;
 | 
|---|
 | 461 |         pair<LineMap::const_iterator,LineMap::const_iterator> FindPair;
 | 
|---|
| [57066a] | 462 |         FindPair = nodes[i]->lines.equal_range(nodes[j]->node->nr);
 | 
|---|
 | 463 |         for (FindLine = FindPair.first; FindLine != FindPair.second; ++FindLine) {
 | 
|---|
 | 464 |           // If there is a line with less than two attached triangles, we don't need a new line.
 | 
|---|
 | 465 |           if (FindLine->second->triangles.size() < 2) {
 | 
|---|
 | 466 |             counter++;
 | 
|---|
 | 467 |             break;  // increase counter only once per edge
 | 
|---|
 | 468 |           }
 | 
|---|
 | 469 |         }
 | 
|---|
 | 470 |       } else { // no line
 | 
|---|
| [a67d19] | 471 |         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);
 | 
|---|
| [57066a] | 472 |         result = true;
 | 
|---|
 | 473 |       }
 | 
|---|
 | 474 |     }
 | 
|---|
 | 475 |   if ((!result) && (counter > 1)) {
 | 
|---|
| [a67d19] | 476 |     DoLog(1) && (Log() << Verbose(1) << "INFO: Degenerate triangle is ok, at least two, here " << counter << ", existing lines are used." << endl);
 | 
|---|
| [57066a] | 477 |     result = true;
 | 
|---|
 | 478 |   }
 | 
|---|
 | 479 |   return result;
 | 
|---|
 | 480 | };
 | 
|---|
 | 481 | 
 | 
|---|
 | 482 | 
 | 
|---|
| [f67b6e] | 483 | ///** Sort function for the candidate list.
 | 
|---|
 | 484 | // */
 | 
|---|
 | 485 | //bool SortCandidates(const CandidateForTesselation* candidate1, const CandidateForTesselation* candidate2)
 | 
|---|
 | 486 | //{
 | 
|---|
 | 487 | //      Info FunctionInfo(__func__);
 | 
|---|
 | 488 | //  Vector BaseLineVector, OrthogonalVector, helper;
 | 
|---|
 | 489 | //  if (candidate1->BaseLine != candidate2->BaseLine) {  // sanity check
 | 
|---|
| [58ed4a] | 490 | //    DoeLog(1) && (eLog()<< Verbose(1) << "sortCandidates was called for two different baselines: " << candidate1->BaseLine << " and " << candidate2->BaseLine << "." << endl);
 | 
|---|
| [f67b6e] | 491 | //    //return false;
 | 
|---|
 | 492 | //    exit(1);
 | 
|---|
 | 493 | //  }
 | 
|---|
 | 494 | //  // create baseline vector
 | 
|---|
 | 495 | //  BaseLineVector.CopyVector(candidate1->BaseLine->endpoints[1]->node->node);
 | 
|---|
 | 496 | //  BaseLineVector.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
 | 497 | //  BaseLineVector.Normalize();
 | 
|---|
 | 498 | //
 | 
|---|
 | 499 | //  // 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!)
 | 
|---|
 | 500 | //  helper.CopyVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
 | 501 | //  helper.SubtractVector(candidate1->point->node);
 | 
|---|
 | 502 | //  OrthogonalVector.CopyVector(&helper);
 | 
|---|
 | 503 | //  helper.VectorProduct(&BaseLineVector);
 | 
|---|
 | 504 | //  OrthogonalVector.SubtractVector(&helper);
 | 
|---|
 | 505 | //  OrthogonalVector.Normalize();
 | 
|---|
 | 506 | //
 | 
|---|
 | 507 | //  // calculate both angles and correct with in-plane vector
 | 
|---|
 | 508 | //  helper.CopyVector(candidate1->point->node);
 | 
|---|
 | 509 | //  helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
 | 510 | //  double phi = BaseLineVector.Angle(&helper);
 | 
|---|
 | 511 | //  if (OrthogonalVector.ScalarProduct(&helper) > 0) {
 | 
|---|
 | 512 | //    phi = 2.*M_PI - phi;
 | 
|---|
 | 513 | //  }
 | 
|---|
 | 514 | //  helper.CopyVector(candidate2->point->node);
 | 
|---|
 | 515 | //  helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
 | 
|---|
 | 516 | //  double psi = BaseLineVector.Angle(&helper);
 | 
|---|
 | 517 | //  if (OrthogonalVector.ScalarProduct(&helper) > 0) {
 | 
|---|
 | 518 | //    psi = 2.*M_PI - psi;
 | 
|---|
 | 519 | //  }
 | 
|---|
 | 520 | //
 | 
|---|
 | 521 | //  Log() << Verbose(1) << *candidate1->point << " has angle " << phi << endl;
 | 
|---|
 | 522 | //  Log() << Verbose(1) << *candidate2->point << " has angle " << psi << endl;
 | 
|---|
 | 523 | //
 | 
|---|
 | 524 | //  // return comparison
 | 
|---|
 | 525 | //  return phi < psi;
 | 
|---|
 | 526 | //};
 | 
|---|
| [57066a] | 527 | 
 | 
|---|
 | 528 | /**
 | 
|---|
 | 529 |  * Finds the point which is second closest to the provided one.
 | 
|---|
 | 530 |  *
 | 
|---|
 | 531 |  * @param Point to which to find the second closest other point
 | 
|---|
 | 532 |  * @param linked cell structure
 | 
|---|
 | 533 |  *
 | 
|---|
 | 534 |  * @return point which is second closest to the provided one
 | 
|---|
 | 535 |  */
 | 
|---|
| [71b20e] | 536 | TesselPoint* FindSecondClosestTesselPoint(const Vector* Point, const LinkedCell* const LC)
 | 
|---|
| [57066a] | 537 | {
 | 
|---|
| [f67b6e] | 538 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 539 |   TesselPoint* closestPoint = NULL;
 | 
|---|
 | 540 |   TesselPoint* secondClosestPoint = NULL;
 | 
|---|
 | 541 |   double distance = 1e16;
 | 
|---|
 | 542 |   double secondDistance = 1e16;
 | 
|---|
 | 543 |   Vector helper;
 | 
|---|
 | 544 |   int N[NDIM], Nlower[NDIM], Nupper[NDIM];
 | 
|---|
 | 545 | 
 | 
|---|
 | 546 |   LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
 | 
|---|
 | 547 |   for(int i=0;i<NDIM;i++) // store indices of this cell
 | 
|---|
 | 548 |     N[i] = LC->n[i];
 | 
|---|
| [a67d19] | 549 |   DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
 | 
|---|
| [57066a] | 550 | 
 | 
|---|
 | 551 |   LC->GetNeighbourBounds(Nlower, Nupper);
 | 
|---|
| [f67b6e] | 552 |   //Log() << Verbose(1) << endl;
 | 
|---|
| [57066a] | 553 |   for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
 | 554 |     for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
 | 555 |       for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| [734816] | 556 |         const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| [f67b6e] | 557 |         //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
 | 
|---|
| [57066a] | 558 |         if (List != NULL) {
 | 
|---|
| [734816] | 559 |           for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| [273382] | 560 |             helper = (*Point) - (*(*Runner)->node);
 | 
|---|
| [57066a] | 561 |             double currentNorm = helper. Norm();
 | 
|---|
 | 562 |             if (currentNorm < distance) {
 | 
|---|
 | 563 |               // remember second point
 | 
|---|
 | 564 |               secondDistance = distance;
 | 
|---|
 | 565 |               secondClosestPoint = closestPoint;
 | 
|---|
 | 566 |               // mark down new closest point
 | 
|---|
 | 567 |               distance = currentNorm;
 | 
|---|
 | 568 |               closestPoint = (*Runner);
 | 
|---|
| [e138de] | 569 |               //Log() << Verbose(2) << "INFO: New Second Nearest Neighbour is " << *secondClosestPoint << "." << endl;
 | 
|---|
| [57066a] | 570 |             }
 | 
|---|
 | 571 |           }
 | 
|---|
 | 572 |         } else {
 | 
|---|
| [717e0c] | 573 |           eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
 | 
|---|
| [57066a] | 574 |             << LC->n[2] << " is invalid!" << endl;
 | 
|---|
 | 575 |         }
 | 
|---|
 | 576 |       }
 | 
|---|
 | 577 | 
 | 
|---|
 | 578 |   return secondClosestPoint;
 | 
|---|
 | 579 | };
 | 
|---|
 | 580 | 
 | 
|---|
 | 581 | /**
 | 
|---|
 | 582 |  * Finds the point which is closest to the provided one.
 | 
|---|
 | 583 |  *
 | 
|---|
 | 584 |  * @param Point to which to find the closest other point
 | 
|---|
 | 585 |  * @param SecondPoint the second closest other point on return, NULL if none found
 | 
|---|
 | 586 |  * @param linked cell structure
 | 
|---|
 | 587 |  *
 | 
|---|
 | 588 |  * @return point which is closest to the provided one, NULL if none found
 | 
|---|
 | 589 |  */
 | 
|---|
| [71b20e] | 590 | TesselPoint* FindClosestTesselPoint(const Vector* Point, TesselPoint *&SecondPoint, const LinkedCell* const LC)
 | 
|---|
| [57066a] | 591 | {
 | 
|---|
| [f67b6e] | 592 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 593 |   TesselPoint* closestPoint = NULL;
 | 
|---|
 | 594 |   SecondPoint = NULL;
 | 
|---|
 | 595 |   double distance = 1e16;
 | 
|---|
 | 596 |   double secondDistance = 1e16;
 | 
|---|
 | 597 |   Vector helper;
 | 
|---|
 | 598 |   int N[NDIM], Nlower[NDIM], Nupper[NDIM];
 | 
|---|
 | 599 | 
 | 
|---|
 | 600 |   LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
 | 
|---|
 | 601 |   for(int i=0;i<NDIM;i++) // store indices of this cell
 | 
|---|
 | 602 |     N[i] = LC->n[i];
 | 
|---|
| [a67d19] | 603 |   DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
 | 
|---|
| [57066a] | 604 | 
 | 
|---|
 | 605 |   LC->GetNeighbourBounds(Nlower, Nupper);
 | 
|---|
| [f67b6e] | 606 |   //Log() << Verbose(1) << endl;
 | 
|---|
| [57066a] | 607 |   for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
 | 608 |     for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
 | 609 |       for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| [734816] | 610 |         const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| [f67b6e] | 611 |         //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
 | 
|---|
| [57066a] | 612 |         if (List != NULL) {
 | 
|---|
| [734816] | 613 |           for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| [273382] | 614 |             helper = (*Point) - (*(*Runner)->node);
 | 
|---|
| [71b20e] | 615 |             double currentNorm = helper.NormSquared();
 | 
|---|
| [57066a] | 616 |             if (currentNorm < distance) {
 | 
|---|
 | 617 |               secondDistance = distance;
 | 
|---|
 | 618 |               SecondPoint = closestPoint;
 | 
|---|
 | 619 |               distance = currentNorm;
 | 
|---|
 | 620 |               closestPoint = (*Runner);
 | 
|---|
| [f67b6e] | 621 |               //Log() << Verbose(1) << "INFO: New Nearest Neighbour is " << *closestPoint << "." << endl;
 | 
|---|
| [57066a] | 622 |             } else if (currentNorm < secondDistance) {
 | 
|---|
 | 623 |               secondDistance = currentNorm;
 | 
|---|
 | 624 |               SecondPoint = (*Runner);
 | 
|---|
| [f67b6e] | 625 |               //Log() << Verbose(1) << "INFO: New Second Nearest Neighbour is " << *SecondPoint << "." << endl;
 | 
|---|
| [57066a] | 626 |             }
 | 
|---|
 | 627 |           }
 | 
|---|
 | 628 |         } else {
 | 
|---|
| [717e0c] | 629 |           eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
 | 
|---|
| [57066a] | 630 |             << LC->n[2] << " is invalid!" << endl;
 | 
|---|
 | 631 |         }
 | 
|---|
 | 632 |       }
 | 
|---|
| [a2028e] | 633 |   // output
 | 
|---|
 | 634 |   if (closestPoint != NULL) {
 | 
|---|
| [a67d19] | 635 |     DoLog(1) && (Log() << Verbose(1) << "Closest point is " << *closestPoint);
 | 
|---|
| [a2028e] | 636 |     if (SecondPoint != NULL)
 | 
|---|
| [a67d19] | 637 |       DoLog(0) && (Log() << Verbose(0) << " and second closest is " << *SecondPoint);
 | 
|---|
 | 638 |     DoLog(0) && (Log() << Verbose(0) << "." << endl);
 | 
|---|
| [a2028e] | 639 |   }
 | 
|---|
| [57066a] | 640 |   return closestPoint;
 | 
|---|
 | 641 | };
 | 
|---|
 | 642 | 
 | 
|---|
 | 643 | /** Returns the closest point on \a *Base with respect to \a *OtherBase.
 | 
|---|
 | 644 |  * \param *out output stream for debugging
 | 
|---|
 | 645 |  * \param *Base reference line
 | 
|---|
 | 646 |  * \param *OtherBase other base line
 | 
|---|
 | 647 |  * \return Vector on reference line that has closest distance
 | 
|---|
 | 648 |  */
 | 
|---|
| [e138de] | 649 | Vector * GetClosestPointBetweenLine(const BoundaryLineSet * const Base, const BoundaryLineSet * const OtherBase)
 | 
|---|
| [57066a] | 650 | {
 | 
|---|
| [f67b6e] | 651 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 652 |   // construct the plane of the two baselines (i.e. take both their directional vectors)
 | 
|---|
| [273382] | 653 |   Vector Baseline = (*Base->endpoints[1]->node->node) - (*Base->endpoints[0]->node->node);
 | 
|---|
 | 654 |   Vector OtherBaseline = (*OtherBase->endpoints[1]->node->node) - (*OtherBase->endpoints[0]->node->node);
 | 
|---|
 | 655 |   Vector Normal = Baseline;
 | 
|---|
 | 656 |   Normal.VectorProduct(OtherBaseline);
 | 
|---|
| [57066a] | 657 |   Normal.Normalize();
 | 
|---|
| [a67d19] | 658 |   DoLog(1) && (Log() << Verbose(1) << "First direction is " << Baseline << ", second direction is " << OtherBaseline << ", normal of intersection plane is " << Normal << "." << endl);
 | 
|---|
| [57066a] | 659 | 
 | 
|---|
 | 660 |   // project one offset point of OtherBase onto this plane (and add plane offset vector)
 | 
|---|
| [273382] | 661 |   Vector NewOffset = (*OtherBase->endpoints[0]->node->node) - (*Base->endpoints[0]->node->node);
 | 
|---|
 | 662 |   NewOffset.ProjectOntoPlane(Normal);
 | 
|---|
 | 663 |   NewOffset += (*Base->endpoints[0]->node->node);
 | 
|---|
 | 664 |   Vector NewDirection = NewOffset + OtherBaseline;
 | 
|---|
| [57066a] | 665 | 
 | 
|---|
 | 666 |   // calculate the intersection between this projected baseline and Base
 | 
|---|
 | 667 |   Vector *Intersection = new Vector;
 | 
|---|
| [0a4f7f] | 668 |   *Intersection = GetIntersectionOfTwoLinesOnPlane(*(Base->endpoints[0]->node->node),
 | 
|---|
 | 669 |                                                    *(Base->endpoints[1]->node->node),
 | 
|---|
 | 670 |                                                      NewOffset, NewDirection);
 | 
|---|
| [273382] | 671 |   Normal = (*Intersection) - (*Base->endpoints[0]->node->node);
 | 
|---|
| [8cbb97] | 672 |   DoLog(1) && (Log() << Verbose(1) << "Found closest point on " << *Base << " at " << *Intersection << ", factor in line is " << fabs(Normal.ScalarProduct(Baseline)/Baseline.NormSquared()) << "." << endl);
 | 
|---|
| [57066a] | 673 | 
 | 
|---|
 | 674 |   return Intersection;
 | 
|---|
 | 675 | };
 | 
|---|
 | 676 | 
 | 
|---|
| [c4d4df] | 677 | /** Returns the distance to the plane defined by \a *triangle
 | 
|---|
 | 678 |  * \param *out output stream for debugging
 | 
|---|
 | 679 |  * \param *x Vector to calculate distance to
 | 
|---|
 | 680 |  * \param *triangle triangle defining plane
 | 
|---|
 | 681 |  * \return distance between \a *x and plane defined by \a *triangle, -1 - if something went wrong
 | 
|---|
 | 682 |  */
 | 
|---|
| [e138de] | 683 | double DistanceToTrianglePlane(const Vector *x, const BoundaryTriangleSet * const triangle)
 | 
|---|
| [c4d4df] | 684 | {
 | 
|---|
| [f67b6e] | 685 |         Info FunctionInfo(__func__);
 | 
|---|
| [c4d4df] | 686 |   double distance = 0.;
 | 
|---|
 | 687 |   if (x == NULL) {
 | 
|---|
 | 688 |     return -1;
 | 
|---|
 | 689 |   }
 | 
|---|
| [d4c9ae] | 690 |   distance = x->DistanceToSpace(triangle->getPlane());
 | 
|---|
| [c4d4df] | 691 |   return distance;
 | 
|---|
 | 692 | };
 | 
|---|
| [57066a] | 693 | 
 | 
|---|
 | 694 | /** Creates the objects in a VRML file.
 | 
|---|
 | 695 |  * \param *out output stream for debugging
 | 
|---|
 | 696 |  * \param *vrmlfile output stream for tecplot data
 | 
|---|
 | 697 |  * \param *Tess Tesselation structure with constructed triangles
 | 
|---|
 | 698 |  * \param *mol molecule structure with atom positions
 | 
|---|
 | 699 |  */
 | 
|---|
| [e138de] | 700 | void WriteVrmlFile(ofstream * const vrmlfile, const Tesselation * const Tess, const PointCloud * const cloud)
 | 
|---|
| [57066a] | 701 | {
 | 
|---|
| [f67b6e] | 702 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 703 |   TesselPoint *Walker = NULL;
 | 
|---|
 | 704 |   int i;
 | 
|---|
| [e138de] | 705 |   Vector *center = cloud->GetCenter();
 | 
|---|
| [57066a] | 706 |   if (vrmlfile != NULL) {
 | 
|---|
| [e138de] | 707 |     //Log() << Verbose(1) << "Writing Raster3D file ... ";
 | 
|---|
| [57066a] | 708 |     *vrmlfile << "#VRML V2.0 utf8" << endl;
 | 
|---|
 | 709 |     *vrmlfile << "#Created by molecuilder" << endl;
 | 
|---|
 | 710 |     *vrmlfile << "#All atoms as spheres" << endl;
 | 
|---|
 | 711 |     cloud->GoToFirst();
 | 
|---|
 | 712 |     while (!cloud->IsEnd()) {
 | 
|---|
 | 713 |       Walker = cloud->GetPoint();
 | 
|---|
 | 714 |       *vrmlfile << "Sphere {" << endl << "  "; // 2 is sphere type
 | 
|---|
 | 715 |       for (i=0;i<NDIM;i++)
 | 
|---|
| [0a4f7f] | 716 |         *vrmlfile << Walker->node->at(i)-center->at(i) << " ";
 | 
|---|
| [57066a] | 717 |       *vrmlfile << "\t0.1\t1. 1. 1." << endl; // radius 0.05 and white as colour
 | 
|---|
 | 718 |       cloud->GoToNext();
 | 
|---|
 | 719 |     }
 | 
|---|
 | 720 | 
 | 
|---|
 | 721 |     *vrmlfile << "# All tesselation triangles" << endl;
 | 
|---|
| [776b64] | 722 |     for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
 | 
|---|
| [57066a] | 723 |       *vrmlfile << "1" << endl << "  "; // 1 is triangle type
 | 
|---|
 | 724 |       for (i=0;i<3;i++) { // print each node
 | 
|---|
 | 725 |         for (int j=0;j<NDIM;j++)  // and for each node all NDIM coordinates
 | 
|---|
| [0a4f7f] | 726 |           *vrmlfile << TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j) << " ";
 | 
|---|
| [57066a] | 727 |         *vrmlfile << "\t";
 | 
|---|
 | 728 |       }
 | 
|---|
 | 729 |       *vrmlfile << "1. 0. 0." << endl;  // red as colour
 | 
|---|
 | 730 |       *vrmlfile << "18" << endl << "  0.5 0.5 0.5" << endl; // 18 is transparency type for previous object
 | 
|---|
 | 731 |     }
 | 
|---|
 | 732 |   } else {
 | 
|---|
| [58ed4a] | 733 |     DoeLog(1) && (eLog()<< Verbose(1) << "Given vrmlfile is " << vrmlfile << "." << endl);
 | 
|---|
| [57066a] | 734 |   }
 | 
|---|
 | 735 |   delete(center);
 | 
|---|
 | 736 | };
 | 
|---|
 | 737 | 
 | 
|---|
 | 738 | /** Writes additionally the current sphere (i.e. the last triangle to file).
 | 
|---|
 | 739 |  * \param *out output stream for debugging
 | 
|---|
 | 740 |  * \param *rasterfile output stream for tecplot data
 | 
|---|
 | 741 |  * \param *Tess Tesselation structure with constructed triangles
 | 
|---|
 | 742 |  * \param *mol molecule structure with atom positions
 | 
|---|
 | 743 |  */
 | 
|---|
| [e138de] | 744 | void IncludeSphereinRaster3D(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
 | 
|---|
| [57066a] | 745 | {
 | 
|---|
| [f67b6e] | 746 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 747 |   Vector helper;
 | 
|---|
| [6a7f78c] | 748 | 
 | 
|---|
 | 749 |   if (Tess->LastTriangle != NULL) {
 | 
|---|
 | 750 |     // include the current position of the virtual sphere in the temporary raster3d file
 | 
|---|
 | 751 |     Vector *center = cloud->GetCenter();
 | 
|---|
 | 752 |     // make the circumsphere's center absolute again
 | 
|---|
| [273382] | 753 |     Vector helper = (1./3.) * ((*Tess->LastTriangle->endpoints[0]->node->node) +
 | 
|---|
 | 754 |                                (*Tess->LastTriangle->endpoints[1]->node->node) +
 | 
|---|
 | 755 |                                (*Tess->LastTriangle->endpoints[2]->node->node));
 | 
|---|
 | 756 |     helper -= (*center);
 | 
|---|
| [6a7f78c] | 757 |     // and add to file plus translucency object
 | 
|---|
 | 758 |     *rasterfile << "# current virtual sphere\n";
 | 
|---|
 | 759 |     *rasterfile << "8\n  25.0    0.6     -1.0 -1.0 -1.0     0.2        0 0 0 0\n";
 | 
|---|
| [0a4f7f] | 760 |     *rasterfile << "2\n  " << helper[0] << " " << helper[1] << " " << helper[2] << "\t" << 5. << "\t1 0 0\n";
 | 
|---|
| [6a7f78c] | 761 |     *rasterfile << "9\n  terminating special property\n";
 | 
|---|
 | 762 |     delete(center);
 | 
|---|
 | 763 |   }
 | 
|---|
| [57066a] | 764 | };
 | 
|---|
 | 765 | 
 | 
|---|
 | 766 | /** Creates the objects in a raster3d file (renderable with a header.r3d).
 | 
|---|
 | 767 |  * \param *out output stream for debugging
 | 
|---|
 | 768 |  * \param *rasterfile output stream for tecplot data
 | 
|---|
 | 769 |  * \param *Tess Tesselation structure with constructed triangles
 | 
|---|
 | 770 |  * \param *mol molecule structure with atom positions
 | 
|---|
 | 771 |  */
 | 
|---|
| [e138de] | 772 | void WriteRaster3dFile(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
 | 
|---|
| [57066a] | 773 | {
 | 
|---|
| [f67b6e] | 774 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 775 |   TesselPoint *Walker = NULL;
 | 
|---|
 | 776 |   int i;
 | 
|---|
| [fc9992] | 777 |   Vector *center = cloud->GetCenter();
 | 
|---|
| [57066a] | 778 |   if (rasterfile != NULL) {
 | 
|---|
| [e138de] | 779 |     //Log() << Verbose(1) << "Writing Raster3D file ... ";
 | 
|---|
| [57066a] | 780 |     *rasterfile << "# Raster3D object description, created by MoleCuilder" << endl;
 | 
|---|
 | 781 |     *rasterfile << "@header.r3d" << endl;
 | 
|---|
 | 782 |     *rasterfile << "# All atoms as spheres" << endl;
 | 
|---|
 | 783 |     cloud->GoToFirst();
 | 
|---|
 | 784 |     while (!cloud->IsEnd()) {
 | 
|---|
 | 785 |       Walker = cloud->GetPoint();
 | 
|---|
 | 786 |       *rasterfile << "2" << endl << "  ";  // 2 is sphere type
 | 
|---|
| [15b670] | 787 |       for (int j=0;j<NDIM;j++) { // and for each node all NDIM coordinates
 | 
|---|
 | 788 |         const double tmp = Walker->node->at(j)-center->at(j);
 | 
|---|
 | 789 |         *rasterfile << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
 | 
|---|
 | 790 |       }
 | 
|---|
| [57066a] | 791 |       *rasterfile << "\t0.1\t1. 1. 1." << endl; // radius 0.05 and white as colour
 | 
|---|
 | 792 |       cloud->GoToNext();
 | 
|---|
 | 793 |     }
 | 
|---|
 | 794 | 
 | 
|---|
 | 795 |     *rasterfile << "# All tesselation triangles" << endl;
 | 
|---|
 | 796 |     *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";
 | 
|---|
| [776b64] | 797 |     for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
 | 
|---|
| [57066a] | 798 |       *rasterfile << "1" << endl << "  ";  // 1 is triangle type
 | 
|---|
 | 799 |       for (i=0;i<3;i++) {  // print each node
 | 
|---|
| [15b670] | 800 |         for (int j=0;j<NDIM;j++) { // and for each node all NDIM coordinates
 | 
|---|
 | 801 |           const double tmp = TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j);
 | 
|---|
 | 802 |           *rasterfile << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
 | 
|---|
 | 803 |         }
 | 
|---|
| [57066a] | 804 |         *rasterfile << "\t";
 | 
|---|
 | 805 |       }
 | 
|---|
 | 806 |       *rasterfile << "1. 0. 0." << endl;  // red as colour
 | 
|---|
 | 807 |       //*rasterfile << "18" << endl << "  0.5 0.5 0.5" << endl;  // 18 is transparency type for previous object
 | 
|---|
 | 808 |     }
 | 
|---|
 | 809 |     *rasterfile << "9\n#  terminating special property\n";
 | 
|---|
 | 810 |   } else {
 | 
|---|
| [58ed4a] | 811 |     DoeLog(1) && (eLog()<< Verbose(1) << "Given rasterfile is " << rasterfile << "." << endl);
 | 
|---|
| [57066a] | 812 |   }
 | 
|---|
| [e138de] | 813 |   IncludeSphereinRaster3D(rasterfile, Tess, cloud);
 | 
|---|
| [57066a] | 814 |   delete(center);
 | 
|---|
 | 815 | };
 | 
|---|
 | 816 | 
 | 
|---|
 | 817 | /** This function creates the tecplot file, displaying the tesselation of the hull.
 | 
|---|
 | 818 |  * \param *out output stream for debugging
 | 
|---|
 | 819 |  * \param *tecplot output stream for tecplot data
 | 
|---|
 | 820 |  * \param N arbitrary number to differentiate various zones in the tecplot format
 | 
|---|
 | 821 |  */
 | 
|---|
| [e138de] | 822 | void WriteTecplotFile(ofstream * const tecplot, const Tesselation * const TesselStruct, const PointCloud * const cloud, const int N)
 | 
|---|
| [57066a] | 823 | {
 | 
|---|
| [f67b6e] | 824 |         Info FunctionInfo(__func__);
 | 
|---|
| [57066a] | 825 |   if ((tecplot != NULL) && (TesselStruct != NULL)) {
 | 
|---|
 | 826 |     // write header
 | 
|---|
 | 827 |     *tecplot << "TITLE = \"3D CONVEX SHELL\"" << endl;
 | 
|---|
 | 828 |     *tecplot << "VARIABLES = \"X\" \"Y\" \"Z\" \"U\"" << endl;
 | 
|---|
| [6a7f78c] | 829 |     *tecplot << "ZONE T=\"";
 | 
|---|
 | 830 |     if (N < 0) {
 | 
|---|
 | 831 |       *tecplot << cloud->GetName();
 | 
|---|
 | 832 |     } else {
 | 
|---|
 | 833 |       *tecplot << N << "-";
 | 
|---|
| [b60a29] | 834 |       if (TesselStruct->LastTriangle != NULL) {
 | 
|---|
 | 835 |         for (int i=0;i<3;i++)
 | 
|---|
| [68f03d] | 836 |           *tecplot << (i==0 ? "" : "_") << TesselStruct->LastTriangle->endpoints[i]->node->getName();
 | 
|---|
| [b60a29] | 837 |       } else {
 | 
|---|
 | 838 |         *tecplot << "none";
 | 
|---|
 | 839 |       }
 | 
|---|
| [6a7f78c] | 840 |     }
 | 
|---|
| [57066a] | 841 |     *tecplot << "\", N=" << TesselStruct->PointsOnBoundary.size() << ", E=" << TesselStruct->TrianglesOnBoundary.size() << ", DATAPACKING=POINT, ZONETYPE=FETRIANGLE" << endl;
 | 
|---|
| [15b670] | 842 |     const int MaxId=cloud->GetMaxId();
 | 
|---|
 | 843 |     int *LookupList = new int[MaxId];
 | 
|---|
 | 844 |     for (int i=0; i< MaxId ; i++){
 | 
|---|
| [57066a] | 845 |       LookupList[i] = -1;
 | 
|---|
| [c72112] | 846 |     }
 | 
|---|
| [57066a] | 847 | 
 | 
|---|
 | 848 |     // print atom coordinates
 | 
|---|
 | 849 |     int Counter = 1;
 | 
|---|
 | 850 |     TesselPoint *Walker = NULL;
 | 
|---|
| [c72112] | 851 |     for (PointMap::const_iterator target = TesselStruct->PointsOnBoundary.begin(); target != TesselStruct->PointsOnBoundary.end(); ++target) {
 | 
|---|
| [57066a] | 852 |       Walker = target->second->node;
 | 
|---|
 | 853 |       LookupList[Walker->nr] = Counter++;
 | 
|---|
| [15b670] | 854 |       for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 855 |         const double tmp = Walker->node->at(i);
 | 
|---|
 | 856 |         *tecplot << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
 | 
|---|
 | 857 |       }
 | 
|---|
 | 858 |       *tecplot << target->second->value << endl;
 | 
|---|
| [57066a] | 859 |     }
 | 
|---|
 | 860 |     *tecplot << endl;
 | 
|---|
 | 861 |     // print connectivity
 | 
|---|
| [a67d19] | 862 |     DoLog(1) && (Log() << Verbose(1) << "The following triangles were created:" << endl);
 | 
|---|
| [776b64] | 863 |     for (TriangleMap::const_iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner != TesselStruct->TrianglesOnBoundary.end(); runner++) {
 | 
|---|
| [68f03d] | 864 |       DoLog(1) && (Log() << Verbose(1) << " " << runner->second->endpoints[0]->node->getName() << "<->" << runner->second->endpoints[1]->node->getName() << "<->" << runner->second->endpoints[2]->node->getName() << endl);
 | 
|---|
| [57066a] | 865 |       *tecplot << LookupList[runner->second->endpoints[0]->node->nr] << " " << LookupList[runner->second->endpoints[1]->node->nr] << " " << LookupList[runner->second->endpoints[2]->node->nr] << endl;
 | 
|---|
 | 866 |     }
 | 
|---|
 | 867 |     delete[] (LookupList);
 | 
|---|
 | 868 |   }
 | 
|---|
 | 869 | };
 | 
|---|
| [7dea7c] | 870 | 
 | 
|---|
 | 871 | /** Calculates the concavity for each of the BoundaryPointSet's in a Tesselation.
 | 
|---|
 | 872 |  * Sets BoundaryPointSet::value equal to the number of connected lines that are not convex.
 | 
|---|
 | 873 |  * \param *out output stream for debugging
 | 
|---|
 | 874 |  * \param *TesselStruct pointer to Tesselation structure
 | 
|---|
 | 875 |  */
 | 
|---|
| [e138de] | 876 | void CalculateConcavityPerBoundaryPoint(const Tesselation * const TesselStruct)
 | 
|---|
| [7dea7c] | 877 | {
 | 
|---|
| [f67b6e] | 878 |         Info FunctionInfo(__func__);
 | 
|---|
| [7dea7c] | 879 |   class BoundaryPointSet *point = NULL;
 | 
|---|
 | 880 |   class BoundaryLineSet *line = NULL;
 | 
|---|
 | 881 | 
 | 
|---|
 | 882 |   // calculate remaining concavity
 | 
|---|
| [776b64] | 883 |   for (PointMap::const_iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) {
 | 
|---|
| [7dea7c] | 884 |     point = PointRunner->second;
 | 
|---|
| [a67d19] | 885 |     DoLog(1) && (Log() << Verbose(1) << "INFO: Current point is " << *point << "." << endl);
 | 
|---|
| [7dea7c] | 886 |     point->value = 0;
 | 
|---|
 | 887 |     for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
 | 
|---|
 | 888 |       line = LineRunner->second;
 | 
|---|
| [f67b6e] | 889 |       //Log() << Verbose(1) << "INFO: Current line of point " << *point << " is " << *line << "." << endl;
 | 
|---|
| [e138de] | 890 |       if (!line->CheckConvexityCriterion())
 | 
|---|
| [7dea7c] | 891 |         point->value += 1;
 | 
|---|
 | 892 |     }
 | 
|---|
 | 893 |   }
 | 
|---|
 | 894 | };
 | 
|---|
 | 895 | 
 | 
|---|
 | 896 | 
 | 
|---|
 | 897 | /** Checks whether each BoundaryLineSet in the Tesselation has two triangles.
 | 
|---|
 | 898 |  * \param *out output stream for debugging
 | 
|---|
 | 899 |  * \param *TesselStruct
 | 
|---|
 | 900 |  * \return true - all have exactly two triangles, false - some not, list is printed to screen
 | 
|---|
 | 901 |  */
 | 
|---|
| [e138de] | 902 | bool CheckListOfBaselines(const Tesselation * const TesselStruct)
 | 
|---|
| [7dea7c] | 903 | {
 | 
|---|
| [f67b6e] | 904 |         Info FunctionInfo(__func__);
 | 
|---|
| [776b64] | 905 |   LineMap::const_iterator testline;
 | 
|---|
| [7dea7c] | 906 |   bool result = false;
 | 
|---|
 | 907 |   int counter = 0;
 | 
|---|
 | 908 | 
 | 
|---|
| [a67d19] | 909 |   DoLog(1) && (Log() << Verbose(1) << "Check: List of Baselines with not two connected triangles:" << endl);
 | 
|---|
| [7dea7c] | 910 |   for (testline = TesselStruct->LinesOnBoundary.begin(); testline != TesselStruct->LinesOnBoundary.end(); testline++) {
 | 
|---|
 | 911 |     if (testline->second->triangles.size() != 2) {
 | 
|---|
| [a67d19] | 912 |       DoLog(2) && (Log() << Verbose(2) << *testline->second << "\t" << testline->second->triangles.size() << endl);
 | 
|---|
| [7dea7c] | 913 |       counter++;
 | 
|---|
 | 914 |     }
 | 
|---|
 | 915 |   }
 | 
|---|
 | 916 |   if (counter == 0) {
 | 
|---|
| [a67d19] | 917 |     DoLog(1) && (Log() << Verbose(1) << "None." << endl);
 | 
|---|
| [7dea7c] | 918 |     result = true;
 | 
|---|
 | 919 |   }
 | 
|---|
 | 920 |   return result;
 | 
|---|
 | 921 | }
 | 
|---|
 | 922 | 
 | 
|---|
| [262bae] | 923 | /** Counts the number of triangle pairs that contain the given polygon.
 | 
|---|
 | 924 |  * \param *P polygon with endpoints to look for
 | 
|---|
 | 925 |  * \param *T set of triangles to create pairs from containing \a *P
 | 
|---|
 | 926 |  */
 | 
|---|
 | 927 | int CountTrianglePairContainingPolygon(const BoundaryPolygonSet * const P, const TriangleSet * const T)
 | 
|---|
 | 928 | {
 | 
|---|
 | 929 |   Info FunctionInfo(__func__);
 | 
|---|
 | 930 |   // check number of endpoints in *P
 | 
|---|
 | 931 |   if (P->endpoints.size() != 4) {
 | 
|---|
| [58ed4a] | 932 |     DoeLog(1) && (eLog()<< Verbose(1) << "CountTrianglePairContainingPolygon works only on polygons with 4 nodes!" << endl);
 | 
|---|
| [262bae] | 933 |     return 0;
 | 
|---|
 | 934 |   }
 | 
|---|
 | 935 | 
 | 
|---|
 | 936 |   // check number of triangles in *T
 | 
|---|
 | 937 |   if (T->size() < 2) {
 | 
|---|
| [58ed4a] | 938 |     DoeLog(1) && (eLog()<< Verbose(1) << "Not enough triangles to have pairs!" << endl);
 | 
|---|
| [262bae] | 939 |     return 0;
 | 
|---|
 | 940 |   }
 | 
|---|
 | 941 | 
 | 
|---|
| [a67d19] | 942 |   DoLog(0) && (Log() << Verbose(0) << "Polygon is " << *P << endl);
 | 
|---|
| [262bae] | 943 |   // create each pair, get the endpoints and check whether *P is contained.
 | 
|---|
 | 944 |   int counter = 0;
 | 
|---|
 | 945 |   PointSet Trianglenodes;
 | 
|---|
 | 946 |   class BoundaryPolygonSet PairTrianglenodes;
 | 
|---|
 | 947 |   for(TriangleSet::iterator Walker = T->begin(); Walker != T->end(); Walker++) {
 | 
|---|
 | 948 |     for (int i=0;i<3;i++)
 | 
|---|
 | 949 |       Trianglenodes.insert((*Walker)->endpoints[i]);
 | 
|---|
 | 950 | 
 | 
|---|
 | 951 |     for(TriangleSet::iterator PairWalker = Walker; PairWalker != T->end(); PairWalker++) {
 | 
|---|
 | 952 |       if (Walker != PairWalker) { // skip first
 | 
|---|
 | 953 |         PairTrianglenodes.endpoints = Trianglenodes;
 | 
|---|
 | 954 |         for (int i=0;i<3;i++)
 | 
|---|
 | 955 |           PairTrianglenodes.endpoints.insert((*PairWalker)->endpoints[i]);
 | 
|---|
| [856098] | 956 |         const int size = PairTrianglenodes.endpoints.size();
 | 
|---|
 | 957 |         if (size == 4) {
 | 
|---|
| [a67d19] | 958 |           DoLog(0) && (Log() << Verbose(0) << " Current pair of triangles: " << **Walker << "," << **PairWalker << " with " << size << " distinct endpoints:" << PairTrianglenodes << endl);
 | 
|---|
| [856098] | 959 |           // now check
 | 
|---|
 | 960 |           if (PairTrianglenodes.ContainsPresentTupel(P)) {
 | 
|---|
 | 961 |             counter++;
 | 
|---|
| [a67d19] | 962 |             DoLog(0) && (Log() << Verbose(0) << "  ACCEPT: Matches with " << *P << endl);
 | 
|---|
| [856098] | 963 |           } else {
 | 
|---|
| [a67d19] | 964 |             DoLog(0) && (Log() << Verbose(0) << "  REJECT: No match with " << *P << endl);
 | 
|---|
| [856098] | 965 |           }
 | 
|---|
| [262bae] | 966 |         } else {
 | 
|---|
| [a67d19] | 967 |           DoLog(0) && (Log() << Verbose(0) << "  REJECT: Less than four endpoints." << endl);
 | 
|---|
| [262bae] | 968 |         }
 | 
|---|
 | 969 |       }
 | 
|---|
 | 970 |     }
 | 
|---|
| [856098] | 971 |     Trianglenodes.clear();
 | 
|---|
| [262bae] | 972 |   }
 | 
|---|
 | 973 |   return counter;
 | 
|---|
 | 974 | };
 | 
|---|
 | 975 | 
 | 
|---|
 | 976 | /** Checks whether two give polygons have two or more points in common.
 | 
|---|
 | 977 |  * \param *P1 first polygon
 | 
|---|
 | 978 |  * \param *P2 second polygon
 | 
|---|
 | 979 |  * \return true - are connected, false = are note
 | 
|---|
 | 980 |  */
 | 
|---|
 | 981 | bool ArePolygonsEdgeConnected(const BoundaryPolygonSet * const P1, const BoundaryPolygonSet * const P2)
 | 
|---|
 | 982 | {
 | 
|---|
 | 983 |   Info FunctionInfo(__func__);
 | 
|---|
 | 984 |   int counter = 0;
 | 
|---|
 | 985 |   for(PointSet::const_iterator Runner = P1->endpoints.begin(); Runner != P1->endpoints.end(); Runner++) {
 | 
|---|
 | 986 |     if (P2->ContainsBoundaryPoint((*Runner))) {
 | 
|---|
 | 987 |       counter++;
 | 
|---|
| [a67d19] | 988 |       DoLog(1) && (Log() << Verbose(1) << *(*Runner) << " of second polygon is found in the first one." << endl);
 | 
|---|
| [262bae] | 989 |       return true;
 | 
|---|
 | 990 |     }
 | 
|---|
 | 991 |   }
 | 
|---|
 | 992 |   return false;
 | 
|---|
 | 993 | };
 | 
|---|
 | 994 | 
 | 
|---|
 | 995 | /** Combines second into the first and deletes the second.
 | 
|---|
 | 996 |  * \param *P1 first polygon, contains all nodes on return
 | 
|---|
 | 997 |  * \param *&P2 second polygon, is deleted.
 | 
|---|
 | 998 |  */
 | 
|---|
 | 999 | void CombinePolygons(BoundaryPolygonSet * const P1, BoundaryPolygonSet * &P2)
 | 
|---|
 | 1000 | {
 | 
|---|
 | 1001 |   Info FunctionInfo(__func__);
 | 
|---|
| [856098] | 1002 |   pair <PointSet::iterator, bool> Tester;
 | 
|---|
 | 1003 |   for(PointSet::iterator Runner = P2->endpoints.begin(); Runner != P2->endpoints.end(); Runner++) {
 | 
|---|
 | 1004 |     Tester = P1->endpoints.insert((*Runner));
 | 
|---|
 | 1005 |     if (Tester.second)
 | 
|---|
| [a67d19] | 1006 |       DoLog(0) && (Log() << Verbose(0) << "Inserting endpoint " << *(*Runner) << " into first polygon." << endl);
 | 
|---|
| [262bae] | 1007 |   }
 | 
|---|
 | 1008 |   P2->endpoints.clear();
 | 
|---|
 | 1009 |   delete(P2);
 | 
|---|
 | 1010 | };
 | 
|---|
 | 1011 | 
 | 
|---|