Changeset a1acc5 for molecuilder/src/vector.cpp
- Timestamp:
- Jan 8, 2010, 2:04:22 PM (16 years ago)
- Children:
- 22b47e
- Parents:
- eda56a
- File:
-
- 1 edited
-
molecuilder/src/vector.cpp (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/vector.cpp
reda56a ra1acc5 8 8 #include "defs.hpp" 9 9 #include "helpers.hpp" 10 #include " memoryallocator.hpp"10 #include "info.hpp" 11 11 #include "leastsquaremin.hpp" 12 12 #include "log.hpp" 13 #include "memoryallocator.hpp" 13 14 #include "vector.hpp" 14 15 #include "verbose.hpp" 16 17 #include <gsl/gsl_linalg.h> 18 #include <gsl/gsl_matrix.h> 19 #include <gsl/gsl_permutation.h> 20 #include <gsl/gsl_vector.h> 15 21 16 22 /************************************ Functions for class vector ************************************/ … … 219 225 bool Vector::GetIntersectionWithPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset, const Vector * const Origin, const Vector * const LineVector) 220 226 { 227 Info FunctionInfo(__func__); 221 228 double factor; 222 229 Vector Direction, helper; … … 226 233 Direction.SubtractVector(Origin); 227 234 Direction.Normalize(); 228 //Log() << Verbose(4) << "INFO: Direction is " << Direction << "." << endl;235 Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl; 229 236 factor = Direction.ScalarProduct(PlaneNormal); 230 237 if (factor < MYEPSILON) { // Uniqueness: line parallel to plane? … … 236 243 factor = helper.ScalarProduct(PlaneNormal)/factor; 237 244 if (factor < MYEPSILON) { // Origin is in-plane 238 //Log() << Verbose(2) << "Origin of line is in-plane, simple." << endl;245 Log() << Verbose(1) << "Origin of line is in-plane, simple." << endl; 239 246 CopyVector(Origin); 240 247 return true; … … 243 250 Direction.Scale(factor); 244 251 CopyVector(Origin); 245 //Log() << Verbose(4) << "INFO: Scaled direction is " << Direction << "." << endl;252 Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl; 246 253 AddVector(&Direction); 247 254 … … 250 257 helper.SubtractVector(PlaneOffset); 251 258 if (helper.ScalarProduct(PlaneNormal) < MYEPSILON) { 252 //Log() << Verbose(2) << "INFO: Intersection at " << *this << " is good." << endl;259 Log() << Verbose(1) << "INFO: Intersection at " << *this << " is good." << endl; 253 260 return true; 254 261 } else { … … 299 306 bool Vector::GetIntersectionOfTwoLinesOnPlane(const Vector * const Line1a, const Vector * const Line1b, const Vector * const Line2a, const Vector * const Line2b, const Vector *PlaneNormal) 300 307 { 301 bool result = true;308 Info FunctionInfo(__func__); 302 309 Vector Direction, OtherDirection; 303 Vector AuxiliaryNormal;304 Vector Distance;305 const Vector *Normal = NULL;306 Vector *ConstructedNormal= NULL;307 bool FreeNormal = false;310 gsl_matrix *A = gsl_matrix_alloc(NDIM,NDIM); 311 gsl_vector *b = gsl_vector_alloc(NDIM); 312 gsl_vector *x = gsl_vector_alloc(NDIM); 313 gsl_permutation *perm = NULL; 314 int signum; 308 315 309 316 // construct both direction vectors 310 Zero();311 317 Direction.CopyVector(Line1b); 312 318 Direction.SubtractVector(Line1a); … … 318 324 return false; 319 325 320 Direction.Normalize(); 321 OtherDirection.Normalize(); 322 323 //Log() << Verbose(4) << "INFO: Normalized Direction " << Direction << " and OtherDirection " << OtherDirection << "." << endl; 324 325 if (fabs(OtherDirection.ScalarProduct(&Direction) - 1.) < MYEPSILON) { // lines are parallel 326 if ((Line1a == Line2a) || (Line1a == Line2b)) 327 CopyVector(Line1a); 328 else if ((Line1b == Line2b) || (Line1b == Line2b)) 329 CopyVector(Line1b); 330 else 331 return false; 332 Log() << Verbose(4) << "INFO: Intersection is " << *this << "." << endl; 326 // set vector 327 for (int i=0;i<NDIM;i++) 328 gsl_vector_set(b, i, Line1a->x[i]-Line2a->x[i]); 329 Log() << Verbose(1) << "b = " << endl; 330 gsl_vector_fprintf(stdout, b, "%g"); 331 332 // set matrix 333 for (int i=0;i<NDIM;i++) 334 gsl_matrix_set(A, 0, i, -Direction.x[i]); 335 for (int i=0;i<NDIM;i++) 336 gsl_matrix_set(A, 1, i, OtherDirection.x[i]); 337 for (int i=0;i<NDIM;i++) 338 gsl_matrix_set(A, 2, i, 1.); 339 Log() << Verbose(1) << "A = " << endl; 340 gsl_matrix_fprintf(stdout, A, "%g"); 341 342 // Solve A x=b for x 343 perm = gsl_permutation_alloc(NDIM); 344 gsl_linalg_LU_decomp(A, perm, &signum); 345 gsl_linalg_LU_solve(A, perm, b, x); 346 gsl_permutation_free(perm); 347 gsl_vector_free(b); 348 gsl_matrix_free(A); 349 350 Log() << Verbose(1) << "Solution is " << gsl_vector_get(x,0) << ", " << gsl_vector_get(x,1) << "." << endl; 351 352 CopyVector(&Direction); 353 Scale(gsl_vector_get(x,0)); 354 AddVector(Line1a); 355 Log() << Verbose(1) << "INFO: First intersection is " << *this << "." << endl; 356 357 Vector test; 358 test.CopyVector(&OtherDirection); 359 test.Scale(gsl_vector_get(x,1)); 360 test.AddVector(Line2a); 361 Log() << Verbose(1) << "INFO: Second intersection is " << test << "." << endl; 362 test.SubtractVector(this); 363 364 gsl_vector_free(x); 365 366 if (test.IsZero()) 333 367 return true; 334 } else { 335 // check whether we have a plane normal vector 336 if (PlaneNormal == NULL) { 337 ConstructedNormal = new Vector; 338 ConstructedNormal->MakeNormalVector(&Direction, &OtherDirection); 339 Normal = ConstructedNormal; 340 FreeNormal = true; 341 } else 342 Normal = PlaneNormal; 343 344 AuxiliaryNormal.MakeNormalVector(&OtherDirection, Normal); 345 //Log() << Verbose(4) << "INFO: PlaneNormal is " << *Normal << " and AuxiliaryNormal " << AuxiliaryNormal << "." << endl; 346 347 Distance.CopyVector(Line2a); 348 Distance.SubtractVector(Line1a); 349 //Log() << Verbose(4) << "INFO: Distance is " << Distance << "." << endl; 350 if (Distance.IsZero()) { 351 // offsets are equal, match found 352 CopyVector(Line1a); 353 result = true; 354 } else { 355 CopyVector(Distance.Projection(&AuxiliaryNormal)); 356 //Log() << Verbose(4) << "INFO: Projected Distance is " << *this << "." << endl; 357 double factor = Direction.ScalarProduct(&AuxiliaryNormal); 358 //Log() << Verbose(4) << "INFO: Scaling factor is " << factor << "." << endl; 359 Scale(1./(factor*factor)); 360 //Log() << Verbose(4) << "INFO: Scaled Distance is " << *this << "." << endl; 361 CopyVector(Projection(&Direction)); 362 //Log() << Verbose(4) << "INFO: Distance, projected into Direction, is " << *this << "." << endl; 363 if (this->IsZero()) 364 result = false; 365 else 366 result = true; 367 AddVector(Line1a); 368 } 369 370 if (FreeNormal) 371 delete(ConstructedNormal); 372 } 373 if (result) 374 Log() << Verbose(4) << "INFO: Intersection is " << *this << "." << endl; 375 376 return result; 368 else 369 return false; 377 370 }; 378 371
Note:
See TracChangeset
for help on using the changeset viewer.
