| [357fba] | 1 | /* | 
|---|
|  | 2 | * TesselationHelpers.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Aug 3, 2009 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 9 |  | 
|---|
| [f66195] | 10 | #include <fstream> | 
|---|
|  | 11 |  | 
|---|
| [f67b6e] | 12 | #include "info.hpp" | 
|---|
| [f66195] | 13 | #include "linkedcell.hpp" | 
|---|
| [b32dbb] | 14 | #include "linearsystemofequations.hpp" | 
|---|
| [e138de] | 15 | #include "log.hpp" | 
|---|
| [f66195] | 16 | #include "tesselation.hpp" | 
|---|
| [357fba] | 17 | #include "tesselationhelpers.hpp" | 
|---|
| [f66195] | 18 | #include "vector.hpp" | 
|---|
| [643e76] | 19 | #include "Line.hpp" | 
|---|
| [0a4f7f] | 20 | #include "vector_ops.hpp" | 
|---|
| [f66195] | 21 | #include "verbose.hpp" | 
|---|
| [d4c9ae] | 22 | #include "Plane.hpp" | 
|---|
| [357fba] | 23 |  | 
|---|
| [f67b6e] | 24 | double DetGet(gsl_matrix * const A, const int inPlace) | 
|---|
|  | 25 | { | 
|---|
|  | 26 | Info FunctionInfo(__func__); | 
|---|
| [357fba] | 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); | 
|---|
| [24a5e0] | 35 | gsl_matrix *tmpA=0; | 
|---|
| [357fba] | 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 |  | 
|---|
| [c0f6c6] | 54 | void GetSphere(Vector * const center, const Vector &a, const Vector &b, const Vector &c, const double RADIUS) | 
|---|
| [357fba] | 55 | { | 
|---|
| [f67b6e] | 56 | Info FunctionInfo(__func__); | 
|---|
| [357fba] | 57 | gsl_matrix *A = gsl_matrix_calloc(3,3); | 
|---|
|  | 58 | double m11, m12, m13, m14; | 
|---|
|  | 59 |  | 
|---|
|  | 60 | for(int i=0;i<3;i++) { | 
|---|
| [0a4f7f] | 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]); | 
|---|
| [357fba] | 64 | } | 
|---|
| [f1cccd] | 65 | m11 = DetGet(A, 1); | 
|---|
| [357fba] | 66 |  | 
|---|
|  | 67 | for(int i=0;i<3;i++) { | 
|---|
| [0a4f7f] | 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]); | 
|---|
| [357fba] | 71 | } | 
|---|
| [f1cccd] | 72 | m12 = DetGet(A, 1); | 
|---|
| [357fba] | 73 |  | 
|---|
|  | 74 | for(int i=0;i<3;i++) { | 
|---|
| [0a4f7f] | 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]); | 
|---|
| [357fba] | 78 | } | 
|---|
| [f1cccd] | 79 | m13 = DetGet(A, 1); | 
|---|
| [357fba] | 80 |  | 
|---|
|  | 81 | for(int i=0;i<3;i++) { | 
|---|
| [0a4f7f] | 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]); | 
|---|
| [357fba] | 85 | } | 
|---|
| [f1cccd] | 86 | m14 = DetGet(A, 1); | 
|---|
| [357fba] | 87 |  | 
|---|
|  | 88 | if (fabs(m11) < MYEPSILON) | 
|---|
| [58ed4a] | 89 | DoeLog(1) && (eLog()<< Verbose(1) << "three points are colinear." << endl); | 
|---|
| [357fba] | 90 |  | 
|---|
| [0a4f7f] | 91 | center->at(0) =  0.5 * m12/ m11; | 
|---|
|  | 92 | center->at(1) = -0.5 * m13/ m11; | 
|---|
|  | 93 | center->at(2) =  0.5 * m14/ m11; | 
|---|
| [357fba] | 94 |  | 
|---|
| [1513a74] | 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); | 
|---|
| [357fba] | 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 | 
|---|
| [c0f6c6] | 109 | * @param *Umkreismittelpunkt new center point of circumference | 
|---|
| [357fba] | 110 | * @param Direction vector indicates up/down | 
|---|
| [c0f6c6] | 111 | * @param AlternativeDirection Vector, needed in case the triangles have 90 deg angle | 
|---|
| [357fba] | 112 | * @param Halfplaneindicator double indicates whether Direction is up or down | 
|---|
| [c0f6c6] | 113 | * @param AlternativeIndicator double indicates in case of orthogonal triangles which direction of AlternativeDirection is suitable | 
|---|
| [357fba] | 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 | */ | 
|---|
| [c0f6c6] | 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) | 
|---|
| [357fba] | 122 | { | 
|---|
| [f67b6e] | 123 | Info FunctionInfo(__func__); | 
|---|
| [357fba] | 124 | Vector TempNormal, helper; | 
|---|
|  | 125 | double Restradius; | 
|---|
|  | 126 | Vector OtherCenter; | 
|---|
|  | 127 | Center->Zero(); | 
|---|
| [273382] | 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; | 
|---|
| [357fba] | 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))); | 
|---|
| [273382] | 136 | (*NewUmkreismittelpunkt) = (*Center); | 
|---|
| [a67d19] | 137 | DoLog(1) && (Log() << Verbose(1) << "Center of new circumference is " << *NewUmkreismittelpunkt << ".\n"); | 
|---|
| [357fba] | 138 | // Here we calculated center of circumscribing circle, using barycentric coordinates | 
|---|
| [a67d19] | 139 | DoLog(1) && (Log() << Verbose(1) << "Center of circumference is " << *Center << " in direction " << *Direction << ".\n"); | 
|---|
| [357fba] | 140 |  | 
|---|
| [273382] | 141 | TempNormal = a - b; | 
|---|
|  | 142 | helper = a - c; | 
|---|
|  | 143 | TempNormal.VectorProduct(helper); | 
|---|
| [357fba] | 144 | if (fabs(HalfplaneIndicator) < MYEPSILON) | 
|---|
|  | 145 | { | 
|---|
| [273382] | 146 | if ((TempNormal.ScalarProduct(*AlternativeDirection) <0 && AlternativeIndicator >0) || (TempNormal.ScalarProduct(*AlternativeDirection) >0 && AlternativeIndicator <0)) | 
|---|
| [357fba] | 147 | { | 
|---|
| [273382] | 148 | TempNormal *= -1; | 
|---|
| [357fba] | 149 | } | 
|---|
|  | 150 | } | 
|---|
|  | 151 | else | 
|---|
|  | 152 | { | 
|---|
| [273382] | 153 | if (((TempNormal.ScalarProduct(*Direction)<0) && (HalfplaneIndicator >0)) || ((TempNormal.ScalarProduct(*Direction)>0) && (HalfplaneIndicator<0))) | 
|---|
| [357fba] | 154 | { | 
|---|
| [273382] | 155 | TempNormal *= -1; | 
|---|
| [357fba] | 156 | } | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
|  | 159 | TempNormal.Normalize(); | 
|---|
|  | 160 | Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius); | 
|---|
| [a67d19] | 161 | DoLog(1) && (Log() << Verbose(1) << "Height of center of circumference to center of sphere is " << Restradius << ".\n"); | 
|---|
| [357fba] | 162 | TempNormal.Scale(Restradius); | 
|---|
| [a67d19] | 163 | DoLog(1) && (Log() << Verbose(1) << "Shift vector to sphere of circumference is " << TempNormal << ".\n"); | 
|---|
| [273382] | 164 | (*Center) += TempNormal; | 
|---|
| [a67d19] | 165 | DoLog(1) && (Log() << Verbose(1) << "Center of sphere of circumference is " << *Center << ".\n"); | 
|---|
| [f1cccd] | 166 | GetSphere(&OtherCenter, a, b, c, RADIUS); | 
|---|
| [a67d19] | 167 | DoLog(1) && (Log() << Verbose(1) << "OtherCenter of sphere of circumference is " << OtherCenter << ".\n"); | 
|---|
| [357fba] | 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 | */ | 
|---|
| [c0f6c6] | 177 | void GetCenterofCircumcircle(Vector * const Center, const Vector &a, const Vector &b, const Vector &c) | 
|---|
| [357fba] | 178 | { | 
|---|
| [f67b6e] | 179 | Info FunctionInfo(__func__); | 
|---|
| [357fba] | 180 | Vector helper; | 
|---|
| [273382] | 181 | Vector SideA = b - c; | 
|---|
|  | 182 | Vector SideB = c - a; | 
|---|
|  | 183 | Vector SideC = a - b; | 
|---|
| [357fba] | 184 |  | 
|---|
| [b32dbb] | 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; | 
|---|
| [357fba] | 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 | */ | 
|---|
| [c0f6c6] | 210 | double GetPathLengthonCircumCircle(const Vector &CircleCenter, const Vector &CirclePlaneNormal, const double CircleRadius, const Vector &NewSphereCenter, const Vector &OldSphereCenter, const Vector &NormalVector, const Vector &SearchDirection) | 
|---|
| [357fba] | 211 | { | 
|---|
| [f67b6e] | 212 | Info FunctionInfo(__func__); | 
|---|
| [357fba] | 213 | Vector helper; | 
|---|
|  | 214 | double radius, alpha; | 
|---|
| [273382] | 215 |  | 
|---|
|  | 216 | Vector RelativeOldSphereCenter = OldSphereCenter - CircleCenter; | 
|---|
|  | 217 | Vector RelativeNewSphereCenter = NewSphereCenter - CircleCenter; | 
|---|
|  | 218 | helper = RelativeNewSphereCenter; | 
|---|
| [357fba] | 219 | // test whether new center is on the parameter circle's plane | 
|---|
| [273382] | 220 | if (fabs(helper.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) { | 
|---|
| [8cbb97] | 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); | 
|---|
| [273382] | 222 | helper.ProjectOntoPlane(CirclePlaneNormal); | 
|---|
| [357fba] | 223 | } | 
|---|
| [b998c3] | 224 | radius = helper.NormSquared(); | 
|---|
| [357fba] | 225 | // test whether the new center vector has length of CircleRadius | 
|---|
|  | 226 | if (fabs(radius - CircleRadius) > HULLEPSILON) | 
|---|
| [58ed4a] | 227 | DoeLog(1) && (eLog()<< Verbose(1) << "The projected center of the new sphere has radius " << radius << " instead of " << CircleRadius << "." << endl); | 
|---|
| [273382] | 228 | alpha = helper.Angle(RelativeOldSphereCenter); | 
|---|
| [357fba] | 229 | // make the angle unique by checking the halfplanes/search direction | 
|---|
| [273382] | 230 | if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON)  // acos is not unique on [0, 2.*M_PI), hence extra check to decide between two half intervals | 
|---|
| [357fba] | 231 | alpha = 2.*M_PI - alpha; | 
|---|
| [a67d19] | 232 | DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeNewSphereCenter is " << helper << ", RelativeOldSphereCenter is " << RelativeOldSphereCenter << " and resulting angle is " << alpha << "." << endl); | 
|---|
| [1513a74] | 233 | radius = helper.distance(RelativeOldSphereCenter); | 
|---|
| [273382] | 234 | helper.ProjectOntoPlane(NormalVector); | 
|---|
| [357fba] | 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)) { | 
|---|
| [a67d19] | 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); | 
|---|
| [357fba] | 238 | return alpha; | 
|---|
|  | 239 | } else { | 
|---|
| [a67d19] | 240 | DoLog(1) && (Log() << Verbose(1) << "INFO: NewSphereCenter " << RelativeNewSphereCenter << " is too close to RelativeOldSphereCenter" << RelativeOldSphereCenter << "." << endl); | 
|---|
| [357fba] | 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 | { | 
|---|
| [f67b6e] | 260 | Info FunctionInfo(__func__); | 
|---|
| [357fba] | 261 | double retval = 0; | 
|---|
|  | 262 | struct Intersection *I = (struct Intersection *)params; | 
|---|
|  | 263 | Vector intersection; | 
|---|
|  | 264 | for (int i=0;i<NDIM;i++) | 
|---|
| [0a4f7f] | 265 | intersection[i] = gsl_vector_get(x, i); | 
|---|
| [357fba] | 266 |  | 
|---|
| [273382] | 267 | Vector SideA = I->x1 -I->x2 ; | 
|---|
|  | 268 | Vector HeightA = intersection - I->x1; | 
|---|
|  | 269 | HeightA.ProjectOntoPlane(SideA); | 
|---|
| [357fba] | 270 |  | 
|---|
| [273382] | 271 | Vector SideB = I->x3 - I->x4; | 
|---|
|  | 272 | Vector HeightB = intersection - I->x3; | 
|---|
|  | 273 | HeightB.ProjectOntoPlane(SideB); | 
|---|
| [357fba] | 274 |  | 
|---|
| [273382] | 275 | retval = HeightA.ScalarProduct(HeightA) + HeightB.ScalarProduct(HeightB); | 
|---|
| [f67b6e] | 276 | //Log() << Verbose(1) << "MinIntersectDistance called, result: " << retval << endl; | 
|---|
| [357fba] | 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 | */ | 
|---|
| [c0f6c6] | 294 | bool existsIntersection(const Vector &point1, const Vector &point2, const Vector &point3, const Vector &point4) | 
|---|
| [357fba] | 295 | { | 
|---|
| [f67b6e] | 296 | Info FunctionInfo(__func__); | 
|---|
| [357fba] | 297 | bool result; | 
|---|
|  | 298 |  | 
|---|
|  | 299 | struct Intersection par; | 
|---|
| [273382] | 300 | par.x1 = point1; | 
|---|
|  | 301 | par.x2 = point2; | 
|---|
|  | 302 | par.x3 = point3; | 
|---|
|  | 303 | par.x4 = point4; | 
|---|
| [357fba] | 304 |  | 
|---|
|  | 305 | const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex; | 
|---|
|  | 306 | gsl_multimin_fminimizer *s = NULL; | 
|---|
|  | 307 | gsl_vector *ss, *x; | 
|---|
| [f1cccd] | 308 | gsl_multimin_function minexFunction; | 
|---|
| [357fba] | 309 |  | 
|---|
|  | 310 | size_t iter = 0; | 
|---|
|  | 311 | int status; | 
|---|
|  | 312 | double size; | 
|---|
|  | 313 |  | 
|---|
|  | 314 | /* Starting point */ | 
|---|
|  | 315 | x = gsl_vector_alloc(NDIM); | 
|---|
| [0a4f7f] | 316 | gsl_vector_set(x, 0, point1[0]); | 
|---|
|  | 317 | gsl_vector_set(x, 1, point1[1]); | 
|---|
|  | 318 | gsl_vector_set(x, 2, point1[2]); | 
|---|
| [357fba] | 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 */ | 
|---|
| [f1cccd] | 325 | minexFunction.n = NDIM; | 
|---|
|  | 326 | minexFunction.f = &MinIntersectDistance; | 
|---|
|  | 327 | minexFunction.params = (void *)∥ | 
|---|
| [357fba] | 328 |  | 
|---|
|  | 329 | s = gsl_multimin_fminimizer_alloc(T, NDIM); | 
|---|
| [f1cccd] | 330 | gsl_multimin_fminimizer_set(s, &minexFunction, x, ss); | 
|---|
| [357fba] | 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) { | 
|---|
| [a67d19] | 344 | DoLog(1) && (Log() << Verbose(1) << "converged to minimum" <<  endl); | 
|---|
| [357fba] | 345 | } | 
|---|
|  | 346 | } while (status == GSL_CONTINUE && iter < 100); | 
|---|
|  | 347 |  | 
|---|
|  | 348 | // check whether intersection is in between or not | 
|---|
| [273382] | 349 | Vector intersection; | 
|---|
| [357fba] | 350 | double t1, t2; | 
|---|
|  | 351 | for (int i = 0; i < NDIM; i++) { | 
|---|
| [0a4f7f] | 352 | intersection[i] = gsl_vector_get(s->x, i); | 
|---|
| [357fba] | 353 | } | 
|---|
|  | 354 |  | 
|---|
| [273382] | 355 | Vector SideA = par.x2 - par.x1; | 
|---|
|  | 356 | Vector HeightA = intersection - par.x1; | 
|---|
| [357fba] | 357 |  | 
|---|
| [273382] | 358 | t1 = HeightA.ScalarProduct(SideA)/SideA.ScalarProduct(SideA); | 
|---|
| [357fba] | 359 |  | 
|---|
| [273382] | 360 | Vector SideB = par.x4 - par.x3; | 
|---|
|  | 361 | Vector HeightB = intersection - par.x3; | 
|---|
| [357fba] | 362 |  | 
|---|
| [273382] | 363 | t2 = HeightB.ScalarProduct(SideB)/SideB.ScalarProduct(SideB); | 
|---|
| [357fba] | 364 |  | 
|---|
| [f67b6e] | 365 | Log() << Verbose(1) << "Intersection " << intersection << " is at " | 
|---|
| [357fba] | 366 | << t1 << " for (" << point1 << "," << point2 << ") and at " | 
|---|
|  | 367 | << t2 << " for (" << point3 << "," << point4 << "): "; | 
|---|
|  | 368 |  | 
|---|
|  | 369 | if (((t1 >= 0) && (t1 <= 1)) && ((t2 >= 0) && (t2 <= 1))) { | 
|---|
| [a67d19] | 370 | DoLog(1) && (Log() << Verbose(1) << "true intersection." << endl); | 
|---|
| [357fba] | 371 | result = true; | 
|---|
|  | 372 | } else { | 
|---|
| [a67d19] | 373 | DoLog(1) && (Log() << Verbose(1) << "intersection out of region of interest." << endl); | 
|---|
| [357fba] | 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; | 
|---|
| [91e7e4a] | 383 | }; | 
|---|
|  | 384 |  | 
|---|
| [57066a] | 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 | */ | 
|---|
| [c0f6c6] | 394 | double GetAngle(const Vector &point, const Vector &reference, const Vector &OrthogonalVector) | 
|---|
| [57066a] | 395 | { | 
|---|
| [f67b6e] | 396 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 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; | 
|---|
| [273382] | 403 | double phi = point.Angle(reference); | 
|---|
|  | 404 | if (OrthogonalVector.ScalarProduct(point) > 0) { | 
|---|
| [57066a] | 405 | phi = 2.*M_PI - phi; | 
|---|
|  | 406 | } | 
|---|
|  | 407 |  | 
|---|
| [a67d19] | 408 | DoLog(1) && (Log() << Verbose(1) << "INFO: " << point << " has angle " << phi << " with respect to reference " << reference << "." << endl); | 
|---|
| [57066a] | 409 |  | 
|---|
|  | 410 | return phi; | 
|---|
|  | 411 | } | 
|---|
|  | 412 |  | 
|---|
| [91e7e4a] | 413 |  | 
|---|
|  | 414 | /** Calculates the volume of a general tetraeder. | 
|---|
|  | 415 | * \param *a first vector | 
|---|
| [b32dbb] | 416 | * \param *b second vector | 
|---|
|  | 417 | * \param *c third vector | 
|---|
|  | 418 | * \param *d fourth vector | 
|---|
| [91e7e4a] | 419 | * \return \f$ \frac{1}{6} \cdot ((a-d) \times (a-c) \cdot  (a-b)) \f$ | 
|---|
|  | 420 | */ | 
|---|
| [c0f6c6] | 421 | double CalculateVolumeofGeneralTetraeder(const Vector &a, const Vector &b, const Vector &c, const Vector &d) | 
|---|
| [91e7e4a] | 422 | { | 
|---|
| [f67b6e] | 423 | Info FunctionInfo(__func__); | 
|---|
| [91e7e4a] | 424 | Vector Point, TetraederVector[3]; | 
|---|
|  | 425 | double volume; | 
|---|
|  | 426 |  | 
|---|
| [1bd79e] | 427 | TetraederVector[0] = a; | 
|---|
|  | 428 | TetraederVector[1] = b; | 
|---|
|  | 429 | TetraederVector[2] = c; | 
|---|
| [91e7e4a] | 430 | for (int j=0;j<3;j++) | 
|---|
| [273382] | 431 | TetraederVector[j].SubtractVector(d); | 
|---|
| [1bd79e] | 432 | Point = TetraederVector[0]; | 
|---|
| [273382] | 433 | Point.VectorProduct(TetraederVector[1]); | 
|---|
|  | 434 | volume = 1./6. * fabs(Point.ScalarProduct(TetraederVector[2])); | 
|---|
| [91e7e4a] | 435 | return volume; | 
|---|
|  | 436 | }; | 
|---|
| [357fba] | 437 |  | 
|---|
| [b32dbb] | 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 |  | 
|---|
| [57066a] | 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 | */ | 
|---|
| [c0f6c6] | 465 | bool CheckLineCriteriaForDegeneratedTriangle(const BoundaryPointSet * const nodes[3]) | 
|---|
| [57066a] | 466 | { | 
|---|
| [f67b6e] | 467 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 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++) { | 
|---|
| [f1ef60a] | 474 | if (nodes[i] == NULL) { | 
|---|
| [a67d19] | 475 | DoLog(1) && (Log() << Verbose(1) << "Node nr. " << i << " is not yet present." << endl); | 
|---|
| [f1ef60a] | 476 | result = true; | 
|---|
|  | 477 | } else if (nodes[i]->lines.find(nodes[j]->node->nr) != nodes[i]->lines.end()) {  // there already is a line | 
|---|
| [776b64] | 478 | LineMap::const_iterator FindLine; | 
|---|
|  | 479 | pair<LineMap::const_iterator,LineMap::const_iterator> FindPair; | 
|---|
| [57066a] | 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 | 
|---|
| [a67d19] | 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); | 
|---|
| [57066a] | 490 | result = true; | 
|---|
|  | 491 | } | 
|---|
|  | 492 | } | 
|---|
|  | 493 | if ((!result) && (counter > 1)) { | 
|---|
| [a67d19] | 494 | DoLog(1) && (Log() << Verbose(1) << "INFO: Degenerate triangle is ok, at least two, here " << counter << ", existing lines are used." << endl); | 
|---|
| [57066a] | 495 | result = true; | 
|---|
|  | 496 | } | 
|---|
|  | 497 | return result; | 
|---|
|  | 498 | }; | 
|---|
|  | 499 |  | 
|---|
|  | 500 |  | 
|---|
| [f67b6e] | 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 | 
|---|
| [58ed4a] | 508 | //    DoeLog(1) && (eLog()<< Verbose(1) << "sortCandidates was called for two different baselines: " << candidate1->BaseLine << " and " << candidate2->BaseLine << "." << endl); | 
|---|
| [f67b6e] | 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 | //}; | 
|---|
| [57066a] | 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 | */ | 
|---|
| [71b20e] | 554 | TesselPoint* FindSecondClosestTesselPoint(const Vector* Point, const LinkedCell* const LC) | 
|---|
| [57066a] | 555 | { | 
|---|
| [f67b6e] | 556 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 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]; | 
|---|
| [a67d19] | 567 | DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl); | 
|---|
| [57066a] | 568 |  | 
|---|
|  | 569 | LC->GetNeighbourBounds(Nlower, Nupper); | 
|---|
| [f67b6e] | 570 | //Log() << Verbose(1) << endl; | 
|---|
| [57066a] | 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]++) { | 
|---|
| [734816] | 574 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| [f67b6e] | 575 | //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl; | 
|---|
| [57066a] | 576 | if (List != NULL) { | 
|---|
| [734816] | 577 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| [273382] | 578 | helper = (*Point) - (*(*Runner)->node); | 
|---|
| [57066a] | 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); | 
|---|
| [e138de] | 587 | //Log() << Verbose(2) << "INFO: New Second Nearest Neighbour is " << *secondClosestPoint << "." << endl; | 
|---|
| [57066a] | 588 | } | 
|---|
|  | 589 | } | 
|---|
|  | 590 | } else { | 
|---|
| [bdc91e] | 591 | DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl); | 
|---|
| [57066a] | 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 | */ | 
|---|
| [71b20e] | 607 | TesselPoint* FindClosestTesselPoint(const Vector* Point, TesselPoint *&SecondPoint, const LinkedCell* const LC) | 
|---|
| [57066a] | 608 | { | 
|---|
| [f67b6e] | 609 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 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]; | 
|---|
| [a67d19] | 620 | DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl); | 
|---|
| [57066a] | 621 |  | 
|---|
|  | 622 | LC->GetNeighbourBounds(Nlower, Nupper); | 
|---|
| [f67b6e] | 623 | //Log() << Verbose(1) << endl; | 
|---|
| [57066a] | 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]++) { | 
|---|
| [734816] | 627 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| [f67b6e] | 628 | //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl; | 
|---|
| [57066a] | 629 | if (List != NULL) { | 
|---|
| [734816] | 630 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| [273382] | 631 | helper = (*Point) - (*(*Runner)->node); | 
|---|
| [71b20e] | 632 | double currentNorm = helper.NormSquared(); | 
|---|
| [57066a] | 633 | if (currentNorm < distance) { | 
|---|
|  | 634 | secondDistance = distance; | 
|---|
|  | 635 | SecondPoint = closestPoint; | 
|---|
|  | 636 | distance = currentNorm; | 
|---|
|  | 637 | closestPoint = (*Runner); | 
|---|
| [f67b6e] | 638 | //Log() << Verbose(1) << "INFO: New Nearest Neighbour is " << *closestPoint << "." << endl; | 
|---|
| [57066a] | 639 | } else if (currentNorm < secondDistance) { | 
|---|
|  | 640 | secondDistance = currentNorm; | 
|---|
|  | 641 | SecondPoint = (*Runner); | 
|---|
| [f67b6e] | 642 | //Log() << Verbose(1) << "INFO: New Second Nearest Neighbour is " << *SecondPoint << "." << endl; | 
|---|
| [57066a] | 643 | } | 
|---|
|  | 644 | } | 
|---|
|  | 645 | } else { | 
|---|
| [bdc91e] | 646 | DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl); | 
|---|
| [57066a] | 647 | } | 
|---|
|  | 648 | } | 
|---|
| [a2028e] | 649 | // output | 
|---|
|  | 650 | if (closestPoint != NULL) { | 
|---|
| [a67d19] | 651 | DoLog(1) && (Log() << Verbose(1) << "Closest point is " << *closestPoint); | 
|---|
| [a2028e] | 652 | if (SecondPoint != NULL) | 
|---|
| [a67d19] | 653 | DoLog(0) && (Log() << Verbose(0) << " and second closest is " << *SecondPoint); | 
|---|
|  | 654 | DoLog(0) && (Log() << Verbose(0) << "." << endl); | 
|---|
| [a2028e] | 655 | } | 
|---|
| [57066a] | 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 | */ | 
|---|
| [e138de] | 665 | Vector * GetClosestPointBetweenLine(const BoundaryLineSet * const Base, const BoundaryLineSet * const OtherBase) | 
|---|
| [57066a] | 666 | { | 
|---|
| [f67b6e] | 667 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 668 | // construct the plane of the two baselines (i.e. take both their directional vectors) | 
|---|
| [273382] | 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); | 
|---|
| [57066a] | 673 | Normal.Normalize(); | 
|---|
| [a67d19] | 674 | DoLog(1) && (Log() << Verbose(1) << "First direction is " << Baseline << ", second direction is " << OtherBaseline << ", normal of intersection plane is " << Normal << "." << endl); | 
|---|
| [57066a] | 675 |  | 
|---|
|  | 676 | // project one offset point of OtherBase onto this plane (and add plane offset vector) | 
|---|
| [273382] | 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; | 
|---|
| [57066a] | 681 |  | 
|---|
|  | 682 | // calculate the intersection between this projected baseline and Base | 
|---|
|  | 683 | Vector *Intersection = new Vector; | 
|---|
| [643e76] | 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); | 
|---|
| [273382] | 687 | Normal = (*Intersection) - (*Base->endpoints[0]->node->node); | 
|---|
| [8cbb97] | 688 | DoLog(1) && (Log() << Verbose(1) << "Found closest point on " << *Base << " at " << *Intersection << ", factor in line is " << fabs(Normal.ScalarProduct(Baseline)/Baseline.NormSquared()) << "." << endl); | 
|---|
| [57066a] | 689 |  | 
|---|
|  | 690 | return Intersection; | 
|---|
|  | 691 | }; | 
|---|
|  | 692 |  | 
|---|
| [c4d4df] | 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 | */ | 
|---|
| [e138de] | 699 | double DistanceToTrianglePlane(const Vector *x, const BoundaryTriangleSet * const triangle) | 
|---|
| [c4d4df] | 700 | { | 
|---|
| [f67b6e] | 701 | Info FunctionInfo(__func__); | 
|---|
| [c4d4df] | 702 | double distance = 0.; | 
|---|
|  | 703 | if (x == NULL) { | 
|---|
|  | 704 | return -1; | 
|---|
|  | 705 | } | 
|---|
| [d4c9ae] | 706 | distance = x->DistanceToSpace(triangle->getPlane()); | 
|---|
| [c4d4df] | 707 | return distance; | 
|---|
|  | 708 | }; | 
|---|
| [57066a] | 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 | */ | 
|---|
| [e138de] | 716 | void WriteVrmlFile(ofstream * const vrmlfile, const Tesselation * const Tess, const PointCloud * const cloud) | 
|---|
| [57066a] | 717 | { | 
|---|
| [f67b6e] | 718 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 719 | TesselPoint *Walker = NULL; | 
|---|
|  | 720 | int i; | 
|---|
| [e138de] | 721 | Vector *center = cloud->GetCenter(); | 
|---|
| [57066a] | 722 | if (vrmlfile != NULL) { | 
|---|
| [e138de] | 723 | //Log() << Verbose(1) << "Writing Raster3D file ... "; | 
|---|
| [57066a] | 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++) | 
|---|
| [0a4f7f] | 732 | *vrmlfile << Walker->node->at(i)-center->at(i) << " "; | 
|---|
| [57066a] | 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; | 
|---|
| [776b64] | 738 | for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) { | 
|---|
| [57066a] | 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 | 
|---|
| [0a4f7f] | 742 | *vrmlfile << TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j) << " "; | 
|---|
| [57066a] | 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 { | 
|---|
| [58ed4a] | 749 | DoeLog(1) && (eLog()<< Verbose(1) << "Given vrmlfile is " << vrmlfile << "." << endl); | 
|---|
| [57066a] | 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 | */ | 
|---|
| [e138de] | 760 | void IncludeSphereinRaster3D(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud) | 
|---|
| [57066a] | 761 | { | 
|---|
| [f67b6e] | 762 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 763 | Vector helper; | 
|---|
| [6a7f78c] | 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 | 
|---|
| [273382] | 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); | 
|---|
| [6a7f78c] | 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"; | 
|---|
| [0a4f7f] | 776 | *rasterfile << "2\n  " << helper[0] << " " << helper[1] << " " << helper[2] << "\t" << 5. << "\t1 0 0\n"; | 
|---|
| [6a7f78c] | 777 | *rasterfile << "9\n  terminating special property\n"; | 
|---|
|  | 778 | delete(center); | 
|---|
|  | 779 | } | 
|---|
| [57066a] | 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 | */ | 
|---|
| [e138de] | 788 | void WriteRaster3dFile(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud) | 
|---|
| [57066a] | 789 | { | 
|---|
| [f67b6e] | 790 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 791 | TesselPoint *Walker = NULL; | 
|---|
|  | 792 | int i; | 
|---|
| [fc9992] | 793 | Vector *center = cloud->GetCenter(); | 
|---|
| [57066a] | 794 | if (rasterfile != NULL) { | 
|---|
| [e138de] | 795 | //Log() << Verbose(1) << "Writing Raster3D file ... "; | 
|---|
| [57066a] | 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 | 
|---|
| [15b670] | 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 | } | 
|---|
| [57066a] | 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"; | 
|---|
| [776b64] | 813 | for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) { | 
|---|
| [57066a] | 814 | *rasterfile << "1" << endl << "  ";  // 1 is triangle type | 
|---|
|  | 815 | for (i=0;i<3;i++) {  // print each node | 
|---|
| [15b670] | 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 | } | 
|---|
| [57066a] | 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 { | 
|---|
| [58ed4a] | 827 | DoeLog(1) && (eLog()<< Verbose(1) << "Given rasterfile is " << rasterfile << "." << endl); | 
|---|
| [57066a] | 828 | } | 
|---|
| [e138de] | 829 | IncludeSphereinRaster3D(rasterfile, Tess, cloud); | 
|---|
| [57066a] | 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 | */ | 
|---|
| [e138de] | 838 | void WriteTecplotFile(ofstream * const tecplot, const Tesselation * const TesselStruct, const PointCloud * const cloud, const int N) | 
|---|
| [57066a] | 839 | { | 
|---|
| [f67b6e] | 840 | Info FunctionInfo(__func__); | 
|---|
| [57066a] | 841 | if ((tecplot != NULL) && (TesselStruct != NULL)) { | 
|---|
|  | 842 | // write header | 
|---|
|  | 843 | *tecplot << "TITLE = \"3D CONVEX SHELL\"" << endl; | 
|---|
|  | 844 | *tecplot << "VARIABLES = \"X\" \"Y\" \"Z\" \"U\"" << endl; | 
|---|
| [6a7f78c] | 845 | *tecplot << "ZONE T=\""; | 
|---|
|  | 846 | if (N < 0) { | 
|---|
|  | 847 | *tecplot << cloud->GetName(); | 
|---|
|  | 848 | } else { | 
|---|
|  | 849 | *tecplot << N << "-"; | 
|---|
| [b60a29] | 850 | if (TesselStruct->LastTriangle != NULL) { | 
|---|
|  | 851 | for (int i=0;i<3;i++) | 
|---|
| [68f03d] | 852 | *tecplot << (i==0 ? "" : "_") << TesselStruct->LastTriangle->endpoints[i]->node->getName(); | 
|---|
| [b60a29] | 853 | } else { | 
|---|
|  | 854 | *tecplot << "none"; | 
|---|
|  | 855 | } | 
|---|
| [6a7f78c] | 856 | } | 
|---|
| [57066a] | 857 | *tecplot << "\", N=" << TesselStruct->PointsOnBoundary.size() << ", E=" << TesselStruct->TrianglesOnBoundary.size() << ", DATAPACKING=POINT, ZONETYPE=FETRIANGLE" << endl; | 
|---|
| [15b670] | 858 | const int MaxId=cloud->GetMaxId(); | 
|---|
|  | 859 | int *LookupList = new int[MaxId]; | 
|---|
|  | 860 | for (int i=0; i< MaxId ; i++){ | 
|---|
| [57066a] | 861 | LookupList[i] = -1; | 
|---|
| [c72112] | 862 | } | 
|---|
| [57066a] | 863 |  | 
|---|
|  | 864 | // print atom coordinates | 
|---|
|  | 865 | int Counter = 1; | 
|---|
|  | 866 | TesselPoint *Walker = NULL; | 
|---|
| [c72112] | 867 | for (PointMap::const_iterator target = TesselStruct->PointsOnBoundary.begin(); target != TesselStruct->PointsOnBoundary.end(); ++target) { | 
|---|
| [57066a] | 868 | Walker = target->second->node; | 
|---|
|  | 869 | LookupList[Walker->nr] = Counter++; | 
|---|
| [15b670] | 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; | 
|---|
| [57066a] | 875 | } | 
|---|
|  | 876 | *tecplot << endl; | 
|---|
|  | 877 | // print connectivity | 
|---|
| [a67d19] | 878 | DoLog(1) && (Log() << Verbose(1) << "The following triangles were created:" << endl); | 
|---|
| [776b64] | 879 | for (TriangleMap::const_iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner != TesselStruct->TrianglesOnBoundary.end(); runner++) { | 
|---|
| [68f03d] | 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); | 
|---|
| [57066a] | 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 | }; | 
|---|
| [7dea7c] | 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 | */ | 
|---|
| [e138de] | 892 | void CalculateConcavityPerBoundaryPoint(const Tesselation * const TesselStruct) | 
|---|
| [7dea7c] | 893 | { | 
|---|
| [f67b6e] | 894 | Info FunctionInfo(__func__); | 
|---|
| [7dea7c] | 895 | class BoundaryPointSet *point = NULL; | 
|---|
|  | 896 | class BoundaryLineSet *line = NULL; | 
|---|
| [b32dbb] | 897 | class BoundaryTriangleSet *triangle = NULL; | 
|---|
|  | 898 | double ConcavityPerLine = 0.; | 
|---|
|  | 899 | double ConcavityPerTriangle = 0.; | 
|---|
|  | 900 | double area = 0.; | 
|---|
|  | 901 | double totalarea = 0.; | 
|---|
| [7dea7c] | 902 |  | 
|---|
| [776b64] | 903 | for (PointMap::const_iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) { | 
|---|
| [7dea7c] | 904 | point = PointRunner->second; | 
|---|
| [a67d19] | 905 | DoLog(1) && (Log() << Verbose(1) << "INFO: Current point is " << *point << "." << endl); | 
|---|
| [b32dbb] | 906 |  | 
|---|
|  | 907 | // calculate mean concavity over all connected line | 
|---|
|  | 908 | ConcavityPerLine = 0.; | 
|---|
| [7dea7c] | 909 | for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) { | 
|---|
|  | 910 | line = LineRunner->second; | 
|---|
| [f67b6e] | 911 | //Log() << Verbose(1) << "INFO: Current line of point " << *point << " is " << *line << "." << endl; | 
|---|
| [b32dbb] | 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; | 
|---|
| [7dea7c] | 936 | } | 
|---|
| [b32dbb] | 937 | ConcavityPerTriangle /= triangles->size()/totalarea; | 
|---|
|  | 938 | delete(triangles); | 
|---|
|  | 939 |  | 
|---|
|  | 940 | // add up | 
|---|
|  | 941 | point->value = ConcavityPerLine + ConcavityPerTriangle; | 
|---|
| [7dea7c] | 942 | } | 
|---|
|  | 943 | }; | 
|---|
|  | 944 |  | 
|---|
|  | 945 |  | 
|---|
| [b32dbb] | 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 |  | 
|---|
| [7dea7c] | 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 | */ | 
|---|
| [e138de] | 977 | bool CheckListOfBaselines(const Tesselation * const TesselStruct) | 
|---|
| [7dea7c] | 978 | { | 
|---|
| [f67b6e] | 979 | Info FunctionInfo(__func__); | 
|---|
| [776b64] | 980 | LineMap::const_iterator testline; | 
|---|
| [7dea7c] | 981 | bool result = false; | 
|---|
|  | 982 | int counter = 0; | 
|---|
|  | 983 |  | 
|---|
| [a67d19] | 984 | DoLog(1) && (Log() << Verbose(1) << "Check: List of Baselines with not two connected triangles:" << endl); | 
|---|
| [7dea7c] | 985 | for (testline = TesselStruct->LinesOnBoundary.begin(); testline != TesselStruct->LinesOnBoundary.end(); testline++) { | 
|---|
|  | 986 | if (testline->second->triangles.size() != 2) { | 
|---|
| [a67d19] | 987 | DoLog(2) && (Log() << Verbose(2) << *testline->second << "\t" << testline->second->triangles.size() << endl); | 
|---|
| [7dea7c] | 988 | counter++; | 
|---|
|  | 989 | } | 
|---|
|  | 990 | } | 
|---|
|  | 991 | if (counter == 0) { | 
|---|
| [a67d19] | 992 | DoLog(1) && (Log() << Verbose(1) << "None." << endl); | 
|---|
| [7dea7c] | 993 | result = true; | 
|---|
|  | 994 | } | 
|---|
|  | 995 | return result; | 
|---|
|  | 996 | } | 
|---|
|  | 997 |  | 
|---|
| [262bae] | 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) { | 
|---|
| [58ed4a] | 1007 | DoeLog(1) && (eLog()<< Verbose(1) << "CountTrianglePairContainingPolygon works only on polygons with 4 nodes!" << endl); | 
|---|
| [262bae] | 1008 | return 0; | 
|---|
|  | 1009 | } | 
|---|
|  | 1010 |  | 
|---|
|  | 1011 | // check number of triangles in *T | 
|---|
|  | 1012 | if (T->size() < 2) { | 
|---|
| [58ed4a] | 1013 | DoeLog(1) && (eLog()<< Verbose(1) << "Not enough triangles to have pairs!" << endl); | 
|---|
| [262bae] | 1014 | return 0; | 
|---|
|  | 1015 | } | 
|---|
|  | 1016 |  | 
|---|
| [a67d19] | 1017 | DoLog(0) && (Log() << Verbose(0) << "Polygon is " << *P << endl); | 
|---|
| [262bae] | 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]); | 
|---|
| [856098] | 1031 | const int size = PairTrianglenodes.endpoints.size(); | 
|---|
|  | 1032 | if (size == 4) { | 
|---|
| [a67d19] | 1033 | DoLog(0) && (Log() << Verbose(0) << " Current pair of triangles: " << **Walker << "," << **PairWalker << " with " << size << " distinct endpoints:" << PairTrianglenodes << endl); | 
|---|
| [856098] | 1034 | // now check | 
|---|
|  | 1035 | if (PairTrianglenodes.ContainsPresentTupel(P)) { | 
|---|
|  | 1036 | counter++; | 
|---|
| [a67d19] | 1037 | DoLog(0) && (Log() << Verbose(0) << "  ACCEPT: Matches with " << *P << endl); | 
|---|
| [856098] | 1038 | } else { | 
|---|
| [a67d19] | 1039 | DoLog(0) && (Log() << Verbose(0) << "  REJECT: No match with " << *P << endl); | 
|---|
| [856098] | 1040 | } | 
|---|
| [262bae] | 1041 | } else { | 
|---|
| [a67d19] | 1042 | DoLog(0) && (Log() << Verbose(0) << "  REJECT: Less than four endpoints." << endl); | 
|---|
| [262bae] | 1043 | } | 
|---|
|  | 1044 | } | 
|---|
|  | 1045 | } | 
|---|
| [856098] | 1046 | Trianglenodes.clear(); | 
|---|
| [262bae] | 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++; | 
|---|
| [a67d19] | 1063 | DoLog(1) && (Log() << Verbose(1) << *(*Runner) << " of second polygon is found in the first one." << endl); | 
|---|
| [262bae] | 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__); | 
|---|
| [856098] | 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) | 
|---|
| [a67d19] | 1081 | DoLog(0) && (Log() << Verbose(0) << "Inserting endpoint " << *(*Runner) << " into first polygon." << endl); | 
|---|
| [262bae] | 1082 | } | 
|---|
|  | 1083 | P2->endpoints.clear(); | 
|---|
|  | 1084 | delete(P2); | 
|---|
|  | 1085 | }; | 
|---|
|  | 1086 |  | 
|---|