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