| [357fba] | 1 | /*
 | 
|---|
 | 2 |  * TesselationHelpers.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Aug 3, 2009
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #include "tesselationhelpers.hpp"
 | 
|---|
 | 9 | 
 | 
|---|
| [f1cccd] | 10 | double DetGet(gsl_matrix *A, int inPlace) {
 | 
|---|
| [357fba] | 11 |   /*
 | 
|---|
 | 12 |   inPlace = 1 => A is replaced with the LU decomposed copy.
 | 
|---|
 | 13 |   inPlace = 0 => A is retained, and a copy is used for LU.
 | 
|---|
 | 14 |   */
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 |   double det;
 | 
|---|
 | 17 |   int signum;
 | 
|---|
 | 18 |   gsl_permutation *p = gsl_permutation_alloc(A->size1);
 | 
|---|
 | 19 |   gsl_matrix *tmpA;
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 |   if (inPlace)
 | 
|---|
 | 22 |   tmpA = A;
 | 
|---|
 | 23 |   else {
 | 
|---|
 | 24 |   gsl_matrix *tmpA = gsl_matrix_alloc(A->size1, A->size2);
 | 
|---|
 | 25 |   gsl_matrix_memcpy(tmpA , A);
 | 
|---|
 | 26 |   }
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 |   gsl_linalg_LU_decomp(tmpA , p , &signum);
 | 
|---|
 | 30 |   det = gsl_linalg_LU_det(tmpA , signum);
 | 
|---|
 | 31 |   gsl_permutation_free(p);
 | 
|---|
 | 32 |   if (! inPlace)
 | 
|---|
 | 33 |   gsl_matrix_free(tmpA);
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |   return det;
 | 
|---|
 | 36 | };
 | 
|---|
 | 37 | 
 | 
|---|
| [f1cccd] | 38 | void GetSphere(Vector *center, Vector &a, Vector &b, Vector &c, double RADIUS)
 | 
|---|
| [357fba] | 39 | {
 | 
|---|
 | 40 |   gsl_matrix *A = gsl_matrix_calloc(3,3);
 | 
|---|
 | 41 |   double m11, m12, m13, m14;
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 |   for(int i=0;i<3;i++) {
 | 
|---|
 | 44 |     gsl_matrix_set(A, i, 0, a.x[i]);
 | 
|---|
 | 45 |     gsl_matrix_set(A, i, 1, b.x[i]);
 | 
|---|
 | 46 |     gsl_matrix_set(A, i, 2, c.x[i]);
 | 
|---|
 | 47 |   }
 | 
|---|
| [f1cccd] | 48 |   m11 = DetGet(A, 1);
 | 
|---|
| [357fba] | 49 | 
 | 
|---|
 | 50 |   for(int i=0;i<3;i++) {
 | 
|---|
 | 51 |     gsl_matrix_set(A, i, 0, a.x[i]*a.x[i] + b.x[i]*b.x[i] + c.x[i]*c.x[i]);
 | 
|---|
 | 52 |     gsl_matrix_set(A, i, 1, b.x[i]);
 | 
|---|
 | 53 |     gsl_matrix_set(A, i, 2, c.x[i]);
 | 
|---|
 | 54 |   }
 | 
|---|
| [f1cccd] | 55 |   m12 = DetGet(A, 1);
 | 
|---|
| [357fba] | 56 | 
 | 
|---|
 | 57 |   for(int i=0;i<3;i++) {
 | 
|---|
 | 58 |     gsl_matrix_set(A, i, 0, a.x[i]*a.x[i] + b.x[i]*b.x[i] + c.x[i]*c.x[i]);
 | 
|---|
 | 59 |     gsl_matrix_set(A, i, 1, a.x[i]);
 | 
|---|
 | 60 |     gsl_matrix_set(A, i, 2, c.x[i]);
 | 
|---|
 | 61 |   }
 | 
|---|
| [f1cccd] | 62 |   m13 = DetGet(A, 1);
 | 
|---|
| [357fba] | 63 | 
 | 
|---|
 | 64 |   for(int i=0;i<3;i++) {
 | 
|---|
 | 65 |     gsl_matrix_set(A, i, 0, a.x[i]*a.x[i] + b.x[i]*b.x[i] + c.x[i]*c.x[i]);
 | 
|---|
 | 66 |     gsl_matrix_set(A, i, 1, a.x[i]);
 | 
|---|
 | 67 |     gsl_matrix_set(A, i, 2, b.x[i]);
 | 
|---|
 | 68 |   }
 | 
|---|
| [f1cccd] | 69 |   m14 = DetGet(A, 1);
 | 
|---|
| [357fba] | 70 | 
 | 
|---|
 | 71 |   if (fabs(m11) < MYEPSILON)
 | 
|---|
 | 72 |     cerr << "ERROR: three points are colinear." << endl;
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   center->x[0] =  0.5 * m12/ m11;
 | 
|---|
 | 75 |   center->x[1] = -0.5 * m13/ m11;
 | 
|---|
 | 76 |   center->x[2] =  0.5 * m14/ m11;
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 |   if (fabs(a.Distance(center) - RADIUS) > MYEPSILON)
 | 
|---|
 | 79 |     cerr << "ERROR: The given center is further way by " << fabs(a.Distance(center) - RADIUS) << " from a than RADIUS." << endl;
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |   gsl_matrix_free(A);
 | 
|---|
 | 82 | };
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | /**
 | 
|---|
 | 87 |  * Function returns center of sphere with RADIUS, which rests on points a, b, c
 | 
|---|
 | 88 |  * @param Center this vector will be used for return
 | 
|---|
 | 89 |  * @param a vector first point of triangle
 | 
|---|
 | 90 |  * @param b vector second point of triangle
 | 
|---|
 | 91 |  * @param c vector third point of triangle
 | 
|---|
 | 92 |  * @param *Umkreismittelpunkt new cneter point of circumference
 | 
|---|
 | 93 |  * @param Direction vector indicates up/down
 | 
|---|
 | 94 |  * @param AlternativeDirection vecotr, needed in case the triangles have 90 deg angle
 | 
|---|
 | 95 |  * @param Halfplaneindicator double indicates whether Direction is up or down
 | 
|---|
 | 96 |  * @param AlternativeIndicator doube indicates in case of orthogonal triangles which direction of AlternativeDirection is suitable
 | 
|---|
 | 97 |  * @param alpha double angle at a
 | 
|---|
 | 98 |  * @param beta double, angle at b
 | 
|---|
 | 99 |  * @param gamma, double, angle at c
 | 
|---|
 | 100 |  * @param Radius, double
 | 
|---|
 | 101 |  * @param Umkreisradius double radius of circumscribing circle
 | 
|---|
 | 102 |  */
 | 
|---|
| [f1cccd] | 103 | void GetCenterOfSphere(Vector* Center, Vector a, Vector b, Vector c, Vector *NewUmkreismittelpunkt, Vector* Direction, Vector* AlternativeDirection,
 | 
|---|
| [357fba] | 104 |     double HalfplaneIndicator, double AlternativeIndicator, double alpha, double beta, double gamma, double RADIUS, double Umkreisradius)
 | 
|---|
 | 105 | {
 | 
|---|
 | 106 |   Vector TempNormal, helper;
 | 
|---|
 | 107 |   double Restradius;
 | 
|---|
 | 108 |   Vector OtherCenter;
 | 
|---|
| [f1cccd] | 109 |   cout << Verbose(3) << "Begin of GetCenterOfSphere.\n";
 | 
|---|
| [357fba] | 110 |   Center->Zero();
 | 
|---|
 | 111 |   helper.CopyVector(&a);
 | 
|---|
 | 112 |   helper.Scale(sin(2.*alpha));
 | 
|---|
 | 113 |   Center->AddVector(&helper);
 | 
|---|
 | 114 |   helper.CopyVector(&b);
 | 
|---|
 | 115 |   helper.Scale(sin(2.*beta));
 | 
|---|
 | 116 |   Center->AddVector(&helper);
 | 
|---|
 | 117 |   helper.CopyVector(&c);
 | 
|---|
 | 118 |   helper.Scale(sin(2.*gamma));
 | 
|---|
 | 119 |   Center->AddVector(&helper);
 | 
|---|
 | 120 |   //*Center = a * sin(2.*alpha) + b * sin(2.*beta) + c * sin(2.*gamma) ;
 | 
|---|
 | 121 |   Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
 | 
|---|
 | 122 |   NewUmkreismittelpunkt->CopyVector(Center);
 | 
|---|
 | 123 |   cout << Verbose(4) << "Center of new circumference is " << *NewUmkreismittelpunkt << ".\n";
 | 
|---|
 | 124 |   // Here we calculated center of circumscribing circle, using barycentric coordinates
 | 
|---|
 | 125 |   cout << Verbose(4) << "Center of circumference is " << *Center << " in direction " << *Direction << ".\n";
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |   TempNormal.CopyVector(&a);
 | 
|---|
 | 128 |   TempNormal.SubtractVector(&b);
 | 
|---|
 | 129 |   helper.CopyVector(&a);
 | 
|---|
 | 130 |   helper.SubtractVector(&c);
 | 
|---|
 | 131 |   TempNormal.VectorProduct(&helper);
 | 
|---|
 | 132 |   if (fabs(HalfplaneIndicator) < MYEPSILON)
 | 
|---|
 | 133 |     {
 | 
|---|
 | 134 |       if ((TempNormal.ScalarProduct(AlternativeDirection) <0 and AlternativeIndicator >0) or (TempNormal.ScalarProduct(AlternativeDirection) >0 and AlternativeIndicator <0))
 | 
|---|
 | 135 |         {
 | 
|---|
 | 136 |           TempNormal.Scale(-1);
 | 
|---|
 | 137 |         }
 | 
|---|
 | 138 |     }
 | 
|---|
 | 139 |   else
 | 
|---|
 | 140 |     {
 | 
|---|
 | 141 |       if (TempNormal.ScalarProduct(Direction)<0 && HalfplaneIndicator >0 || TempNormal.ScalarProduct(Direction)>0 && HalfplaneIndicator<0)
 | 
|---|
 | 142 |         {
 | 
|---|
 | 143 |           TempNormal.Scale(-1);
 | 
|---|
 | 144 |         }
 | 
|---|
 | 145 |     }
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 |   TempNormal.Normalize();
 | 
|---|
 | 148 |   Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius);
 | 
|---|
 | 149 |   cout << Verbose(4) << "Height of center of circumference to center of sphere is " << Restradius << ".\n";
 | 
|---|
 | 150 |   TempNormal.Scale(Restradius);
 | 
|---|
 | 151 |   cout << Verbose(4) << "Shift vector to sphere of circumference is " << TempNormal << ".\n";
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 |   Center->AddVector(&TempNormal);
 | 
|---|
 | 154 |   cout << Verbose(0) << "Center of sphere of circumference is " << *Center << ".\n";
 | 
|---|
| [f1cccd] | 155 |   GetSphere(&OtherCenter, a, b, c, RADIUS);
 | 
|---|
| [357fba] | 156 |   cout << Verbose(0) << "OtherCenter of sphere of circumference is " << OtherCenter << ".\n";
 | 
|---|
| [f1cccd] | 157 |   cout << Verbose(3) << "End of GetCenterOfSphere.\n";
 | 
|---|
| [357fba] | 158 | };
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 | /** Constructs the center of the circumcircle defined by three points \a *a, \a *b and \a *c.
 | 
|---|
 | 162 |  * \param *Center new center on return
 | 
|---|
 | 163 |  * \param *a first point
 | 
|---|
 | 164 |  * \param *b second point
 | 
|---|
 | 165 |  * \param *c third point
 | 
|---|
 | 166 |  */
 | 
|---|
 | 167 | void GetCenterofCircumcircle(Vector *Center, Vector *a, Vector *b, Vector *c)
 | 
|---|
 | 168 | {
 | 
|---|
 | 169 |   Vector helper;
 | 
|---|
 | 170 |   double alpha, beta, gamma;
 | 
|---|
 | 171 |   Vector SideA, SideB, SideC;
 | 
|---|
 | 172 |   SideA.CopyVector(b);
 | 
|---|
 | 173 |   SideA.SubtractVector(c);
 | 
|---|
 | 174 |   SideB.CopyVector(c);
 | 
|---|
 | 175 |   SideB.SubtractVector(a);
 | 
|---|
 | 176 |   SideC.CopyVector(a);
 | 
|---|
 | 177 |   SideC.SubtractVector(b);
 | 
|---|
 | 178 |   alpha = M_PI - SideB.Angle(&SideC);
 | 
|---|
 | 179 |   beta = M_PI - SideC.Angle(&SideA);
 | 
|---|
 | 180 |   gamma = M_PI - SideA.Angle(&SideB);
 | 
|---|
 | 181 |   //cout << Verbose(3) << "INFO: alpha = " << alpha/M_PI*180. << ", beta = " << beta/M_PI*180. << ", gamma = " << gamma/M_PI*180. << "." << endl;
 | 
|---|
 | 182 |   if (fabs(M_PI - alpha - beta - gamma) > HULLEPSILON)
 | 
|---|
 | 183 |     cerr << "GetCenterofCircumcircle: Sum of angles " << (alpha+beta+gamma)/M_PI*180. << " > 180 degrees by " << fabs(M_PI - alpha - beta - gamma)/M_PI*180. << "!" << endl;
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |   Center->Zero();
 | 
|---|
 | 186 |   helper.CopyVector(a);
 | 
|---|
 | 187 |   helper.Scale(sin(2.*alpha));
 | 
|---|
 | 188 |   Center->AddVector(&helper);
 | 
|---|
 | 189 |   helper.CopyVector(b);
 | 
|---|
 | 190 |   helper.Scale(sin(2.*beta));
 | 
|---|
 | 191 |   Center->AddVector(&helper);
 | 
|---|
 | 192 |   helper.CopyVector(c);
 | 
|---|
 | 193 |   helper.Scale(sin(2.*gamma));
 | 
|---|
 | 194 |   Center->AddVector(&helper);
 | 
|---|
 | 195 |   Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
 | 
|---|
 | 196 | };
 | 
|---|
 | 197 | 
 | 
|---|
 | 198 | /** 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.
 | 
|---|
 | 199 |  * Test whether the \a NewSphereCenter is really on the given plane and in distance \a CircleRadius from \a CircleCenter.
 | 
|---|
 | 200 |  * It calculates the angle, making it unique on [0,2.*M_PI) by comparing to SearchDirection.
 | 
|---|
 | 201 |  * 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).
 | 
|---|
 | 202 |  * \param CircleCenter Center of the parameter circle
 | 
|---|
 | 203 |  * \param CirclePlaneNormal normal vector to plane of the parameter circle
 | 
|---|
 | 204 |  * \param CircleRadius radius of the parameter circle
 | 
|---|
 | 205 |  * \param NewSphereCenter new center of a circumcircle
 | 
|---|
 | 206 |  * \param OldSphereCenter old center of a circumcircle, defining the zero "path length" on the parameter circle
 | 
|---|
 | 207 |  * \param NormalVector normal vector
 | 
|---|
 | 208 |  * \param SearchDirection search direction to make angle unique on return.
 | 
|---|
 | 209 |  * \return Angle between \a NewSphereCenter and \a OldSphereCenter relative to \a CircleCenter, 2.*M_PI if one test fails
 | 
|---|
 | 210 |  */
 | 
|---|
 | 211 | double GetPathLengthonCircumCircle(Vector &CircleCenter, Vector &CirclePlaneNormal, double CircleRadius, Vector &NewSphereCenter, Vector &OldSphereCenter, Vector &NormalVector, Vector &SearchDirection)
 | 
|---|
 | 212 | {
 | 
|---|
 | 213 |   Vector helper;
 | 
|---|
 | 214 |   double radius, alpha;
 | 
|---|
 | 215 | 
 | 
|---|
 | 216 |   helper.CopyVector(&NewSphereCenter);
 | 
|---|
 | 217 |   // test whether new center is on the parameter circle's plane
 | 
|---|
 | 218 |   if (fabs(helper.ScalarProduct(&CirclePlaneNormal)) > HULLEPSILON) {
 | 
|---|
 | 219 |     cerr << "ERROR: Something's very wrong here: NewSphereCenter is not on the band's plane as desired by " <<fabs(helper.ScalarProduct(&CirclePlaneNormal))  << "!" << endl;
 | 
|---|
 | 220 |     helper.ProjectOntoPlane(&CirclePlaneNormal);
 | 
|---|
 | 221 |   }
 | 
|---|
 | 222 |   radius = helper.ScalarProduct(&helper);
 | 
|---|
 | 223 |   // test whether the new center vector has length of CircleRadius
 | 
|---|
 | 224 |   if (fabs(radius - CircleRadius) > HULLEPSILON)
 | 
|---|
 | 225 |     cerr << Verbose(1) << "ERROR: The projected center of the new sphere has radius " << radius << " instead of " << CircleRadius << "." << endl;
 | 
|---|
 | 226 |   alpha = helper.Angle(&OldSphereCenter);
 | 
|---|
 | 227 |   // make the angle unique by checking the halfplanes/search direction
 | 
|---|
 | 228 |   if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)  // acos is not unique on [0, 2.*M_PI), hence extra check to decide between two half intervals
 | 
|---|
 | 229 |     alpha = 2.*M_PI - alpha;
 | 
|---|
 | 230 |   //cout << Verbose(2) << "INFO: RelativeNewSphereCenter is " << helper << ", RelativeOldSphereCenter is " << OldSphereCenter << " and resulting angle is " << alpha << "." << endl;
 | 
|---|
 | 231 |   radius = helper.Distance(&OldSphereCenter);
 | 
|---|
 | 232 |   helper.ProjectOntoPlane(&NormalVector);
 | 
|---|
 | 233 |   // check whether new center is somewhat away or at least right over the current baseline to prevent intersecting triangles
 | 
|---|
 | 234 |   if ((radius > HULLEPSILON) || (helper.Norm() < HULLEPSILON)) {
 | 
|---|
 | 235 |     //cout << Verbose(2) << "INFO: Distance between old and new center is " << radius << " and between new center and baseline center is " << helper.Norm() << "." << endl;
 | 
|---|
 | 236 |     return alpha;
 | 
|---|
 | 237 |   } else {
 | 
|---|
 | 238 |     //cout << Verbose(1) << "INFO: NewSphereCenter " << helper << " is too close to OldSphereCenter" << OldSphereCenter << "." << endl;
 | 
|---|
 | 239 |     return 2.*M_PI;
 | 
|---|
 | 240 |   }
 | 
|---|
 | 241 | };
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 | struct Intersection {
 | 
|---|
 | 244 |   Vector x1;
 | 
|---|
 | 245 |   Vector x2;
 | 
|---|
 | 246 |   Vector x3;
 | 
|---|
 | 247 |   Vector x4;
 | 
|---|
 | 248 | };
 | 
|---|
 | 249 | 
 | 
|---|
 | 250 | /**
 | 
|---|
 | 251 |  * Intersection calculation function.
 | 
|---|
 | 252 |  *
 | 
|---|
 | 253 |  * @param x to find the result for
 | 
|---|
 | 254 |  * @param function parameter
 | 
|---|
 | 255 |  */
 | 
|---|
 | 256 | double MinIntersectDistance(const gsl_vector * x, void *params)
 | 
|---|
 | 257 | {
 | 
|---|
 | 258 |   double retval = 0;
 | 
|---|
 | 259 |   struct Intersection *I = (struct Intersection *)params;
 | 
|---|
 | 260 |   Vector intersection;
 | 
|---|
 | 261 |   Vector SideA,SideB,HeightA, HeightB;
 | 
|---|
 | 262 |   for (int i=0;i<NDIM;i++)
 | 
|---|
 | 263 |     intersection.x[i] = gsl_vector_get(x, i);
 | 
|---|
 | 264 | 
 | 
|---|
 | 265 |   SideA.CopyVector(&(I->x1));
 | 
|---|
 | 266 |   SideA.SubtractVector(&I->x2);
 | 
|---|
 | 267 |   HeightA.CopyVector(&intersection);
 | 
|---|
 | 268 |   HeightA.SubtractVector(&I->x1);
 | 
|---|
 | 269 |   HeightA.ProjectOntoPlane(&SideA);
 | 
|---|
 | 270 | 
 | 
|---|
 | 271 |   SideB.CopyVector(&I->x3);
 | 
|---|
 | 272 |   SideB.SubtractVector(&I->x4);
 | 
|---|
 | 273 |   HeightB.CopyVector(&intersection);
 | 
|---|
 | 274 |   HeightB.SubtractVector(&I->x3);
 | 
|---|
 | 275 |   HeightB.ProjectOntoPlane(&SideB);
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 |   retval = HeightA.ScalarProduct(&HeightA) + HeightB.ScalarProduct(&HeightB);
 | 
|---|
 | 278 |   //cout << Verbose(2) << "MinIntersectDistance called, result: " << retval << endl;
 | 
|---|
 | 279 | 
 | 
|---|
 | 280 |   return retval;
 | 
|---|
 | 281 | };
 | 
|---|
 | 282 | 
 | 
|---|
 | 283 | 
 | 
|---|
 | 284 | /**
 | 
|---|
 | 285 |  * Calculates whether there is an intersection between two lines. The first line
 | 
|---|
 | 286 |  * always goes through point 1 and point 2 and the second line is given by the
 | 
|---|
 | 287 |  * connection between point 4 and point 5.
 | 
|---|
 | 288 |  *
 | 
|---|
 | 289 |  * @param point 1 of line 1
 | 
|---|
 | 290 |  * @param point 2 of line 1
 | 
|---|
 | 291 |  * @param point 1 of line 2
 | 
|---|
 | 292 |  * @param point 2 of line 2
 | 
|---|
 | 293 |  *
 | 
|---|
 | 294 |  * @return true if there is an intersection between the given lines, false otherwise
 | 
|---|
 | 295 |  */
 | 
|---|
 | 296 | bool existsIntersection(Vector point1, Vector point2, Vector point3, Vector point4)
 | 
|---|
 | 297 | {
 | 
|---|
 | 298 |   bool result;
 | 
|---|
 | 299 | 
 | 
|---|
 | 300 |   struct Intersection par;
 | 
|---|
 | 301 |     par.x1.CopyVector(&point1);
 | 
|---|
 | 302 |     par.x2.CopyVector(&point2);
 | 
|---|
 | 303 |     par.x3.CopyVector(&point3);
 | 
|---|
 | 304 |     par.x4.CopyVector(&point4);
 | 
|---|
 | 305 | 
 | 
|---|
 | 306 |     const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
 | 
|---|
 | 307 |     gsl_multimin_fminimizer *s = NULL;
 | 
|---|
 | 308 |     gsl_vector *ss, *x;
 | 
|---|
| [f1cccd] | 309 |     gsl_multimin_function minexFunction;
 | 
|---|
| [357fba] | 310 | 
 | 
|---|
 | 311 |     size_t iter = 0;
 | 
|---|
 | 312 |     int status;
 | 
|---|
 | 313 |     double size;
 | 
|---|
 | 314 | 
 | 
|---|
 | 315 |     /* Starting point */
 | 
|---|
 | 316 |     x = gsl_vector_alloc(NDIM);
 | 
|---|
 | 317 |     gsl_vector_set(x, 0, point1.x[0]);
 | 
|---|
 | 318 |     gsl_vector_set(x, 1, point1.x[1]);
 | 
|---|
 | 319 |     gsl_vector_set(x, 2, point1.x[2]);
 | 
|---|
 | 320 | 
 | 
|---|
 | 321 |     /* Set initial step sizes to 1 */
 | 
|---|
 | 322 |     ss = gsl_vector_alloc(NDIM);
 | 
|---|
 | 323 |     gsl_vector_set_all(ss, 1.0);
 | 
|---|
 | 324 | 
 | 
|---|
 | 325 |     /* Initialize method and iterate */
 | 
|---|
| [f1cccd] | 326 |     minexFunction.n = NDIM;
 | 
|---|
 | 327 |     minexFunction.f = &MinIntersectDistance;
 | 
|---|
 | 328 |     minexFunction.params = (void *)∥
 | 
|---|
| [357fba] | 329 | 
 | 
|---|
 | 330 |     s = gsl_multimin_fminimizer_alloc(T, NDIM);
 | 
|---|
| [f1cccd] | 331 |     gsl_multimin_fminimizer_set(s, &minexFunction, x, ss);
 | 
|---|
| [357fba] | 332 | 
 | 
|---|
 | 333 |     do {
 | 
|---|
 | 334 |         iter++;
 | 
|---|
 | 335 |         status = gsl_multimin_fminimizer_iterate(s);
 | 
|---|
 | 336 | 
 | 
|---|
 | 337 |         if (status) {
 | 
|---|
 | 338 |           break;
 | 
|---|
 | 339 |         }
 | 
|---|
 | 340 | 
 | 
|---|
 | 341 |         size = gsl_multimin_fminimizer_size(s);
 | 
|---|
 | 342 |         status = gsl_multimin_test_size(size, 1e-2);
 | 
|---|
 | 343 | 
 | 
|---|
 | 344 |         if (status == GSL_SUCCESS) {
 | 
|---|
 | 345 |           cout << Verbose(2) << "converged to minimum" <<  endl;
 | 
|---|
 | 346 |         }
 | 
|---|
 | 347 |     } while (status == GSL_CONTINUE && iter < 100);
 | 
|---|
 | 348 | 
 | 
|---|
 | 349 |     // check whether intersection is in between or not
 | 
|---|
 | 350 |   Vector intersection, SideA, SideB, HeightA, HeightB;
 | 
|---|
 | 351 |   double t1, t2;
 | 
|---|
 | 352 |   for (int i = 0; i < NDIM; i++) {
 | 
|---|
 | 353 |     intersection.x[i] = gsl_vector_get(s->x, i);
 | 
|---|
 | 354 |   }
 | 
|---|
 | 355 | 
 | 
|---|
 | 356 |   SideA.CopyVector(&par.x2);
 | 
|---|
 | 357 |   SideA.SubtractVector(&par.x1);
 | 
|---|
 | 358 |   HeightA.CopyVector(&intersection);
 | 
|---|
 | 359 |   HeightA.SubtractVector(&par.x1);
 | 
|---|
 | 360 | 
 | 
|---|
| [658efb] | 361 |   t1 = HeightA.ScalarProduct(&SideA)/SideA.ScalarProduct(&SideA);
 | 
|---|
| [357fba] | 362 | 
 | 
|---|
 | 363 |   SideB.CopyVector(&par.x4);
 | 
|---|
 | 364 |   SideB.SubtractVector(&par.x3);
 | 
|---|
 | 365 |   HeightB.CopyVector(&intersection);
 | 
|---|
 | 366 |   HeightB.SubtractVector(&par.x3);
 | 
|---|
 | 367 | 
 | 
|---|
| [658efb] | 368 |   t2 = HeightB.ScalarProduct(&SideB)/SideB.ScalarProduct(&SideB);
 | 
|---|
| [357fba] | 369 | 
 | 
|---|
 | 370 |   cout << Verbose(2) << "Intersection " << intersection << " is at "
 | 
|---|
 | 371 |     << t1 << " for (" << point1 << "," << point2 << ") and at "
 | 
|---|
 | 372 |     << t2 << " for (" << point3 << "," << point4 << "): ";
 | 
|---|
 | 373 | 
 | 
|---|
 | 374 |   if (((t1 >= 0) && (t1 <= 1)) && ((t2 >= 0) && (t2 <= 1))) {
 | 
|---|
 | 375 |     cout << "true intersection." << endl;
 | 
|---|
 | 376 |     result = true;
 | 
|---|
 | 377 |   } else {
 | 
|---|
 | 378 |     cout << "intersection out of region of interest." << endl;
 | 
|---|
 | 379 |     result = false;
 | 
|---|
 | 380 |   }
 | 
|---|
 | 381 | 
 | 
|---|
 | 382 |   // free minimizer stuff
 | 
|---|
 | 383 |     gsl_vector_free(x);
 | 
|---|
 | 384 |     gsl_vector_free(ss);
 | 
|---|
 | 385 |     gsl_multimin_fminimizer_free(s);
 | 
|---|
 | 386 | 
 | 
|---|
 | 387 |   return result;
 | 
|---|
 | 388 | }
 | 
|---|
 | 389 | 
 | 
|---|