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