Changeset 97498a for src/vector.cpp


Ignore:
Timestamp:
Jan 8, 2010, 2:04:22 PM (16 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, Candidate_v1.7.0, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
9fb860
Parents:
c15ca2
Message:

Attempt to fix the tesselation::IsInnerPoint().

We try the IsInnerPoint() as follows:

  1. Find nearest BoundaryPoints - working
  2. Find Closest BoundaryLine's - working
  3. Find closest Triangle that is well aligned (wrt to NormalVector and Distance) - unsure whether correctly working
  4. Check whether alignment is on boundary or inside/outside - working
  5. If on boundary, we check whether it's inside of triangle by intersecting with boundary lines - not working

Hence, we code a wrapper for GSL routines, to - finally - allow for solution of linear system of equations.

Signed-off-by: Frederik Heber <heber@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/vector.cpp

    rc15ca2 r97498a  
    88#include "defs.hpp"
    99#include "helpers.hpp"
    10 #include "memoryallocator.hpp"
     10#include "info.hpp"
    1111#include "leastsquaremin.hpp"
    1212#include "log.hpp"
     13#include "memoryallocator.hpp"
    1314#include "vector.hpp"
    1415#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>
    1521
    1622/************************************ Functions for class vector ************************************/
     
    219225bool Vector::GetIntersectionWithPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset, const Vector * const Origin, const Vector * const LineVector)
    220226{
     227  Info FunctionInfo(__func__);
    221228  double factor;
    222229  Vector Direction, helper;
     
    226233  Direction.SubtractVector(Origin);
    227234  Direction.Normalize();
    228   //Log() << Verbose(4) << "INFO: Direction is " << Direction << "." << endl;
     235  Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl;
    229236  factor = Direction.ScalarProduct(PlaneNormal);
    230237  if (factor < MYEPSILON) { // Uniqueness: line parallel to plane?
     
    236243  factor = helper.ScalarProduct(PlaneNormal)/factor;
    237244  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;
    239246    CopyVector(Origin);
    240247    return true;
     
    243250  Direction.Scale(factor);
    244251  CopyVector(Origin);
    245   //Log() << Verbose(4) << "INFO: Scaled direction is " << Direction << "." << endl;
     252  Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl;
    246253  AddVector(&Direction);
    247254
     
    250257  helper.SubtractVector(PlaneOffset);
    251258  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;
    253260    return true;
    254261  } else {
     
    299306bool Vector::GetIntersectionOfTwoLinesOnPlane(const Vector * const Line1a, const Vector * const Line1b, const Vector * const Line2a, const Vector * const Line2b, const Vector *PlaneNormal)
    300307{
    301   bool result = true;
     308  Info FunctionInfo(__func__);
    302309  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;
    308315
    309316  // construct both direction vectors
    310   Zero();
    311317  Direction.CopyVector(Line1b);
    312318  Direction.SubtractVector(Line1a);
     
    318324    return false;
    319325
    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())
    333367    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;
    377370};
    378371
Note: See TracChangeset for help on using the changeset viewer.