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