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