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