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