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