Changeset 5de9da


Ignore:
Timestamp:
Jul 14, 2010, 2:52:07 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
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, 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:
ef84ca
Parents:
06300d
Message:

Added some important Methods to Shape class hierachy

  • Shape::isOnSurface()
  • Shape::getNormal()
Location:
src/Shapes
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/Shapes/BaseShapes.cpp

    r06300d r5de9da  
    1010
    1111#include "vector.hpp"
     12#include "Helpers/Assert.hpp"
     13
     14#include <cmath>
    1215
    1316bool Sphere_impl::isInside(const Vector &point){
    1417  return point.NormSquared()<=1;
     18}
     19
     20bool Sphere_impl::isOnSurface(const Vector &point){
     21  return fabs(point.NormSquared()-1)<MYEPSILON;
     22}
     23
     24Vector Sphere_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
     25  if(!isOnSurface(point)){
     26    throw NotOnSurfaceException(__FILE__,__LINE__);
     27  }
     28  return point;
    1529}
    1630
     
    2135
    2236bool Cuboid_impl::isInside(const Vector &point){
    23   return point[0]<=1 && point[1]<=1 && point[2]<=1;
     37  return fabs(point[0])<=1 && fabs(point[1])<=1 && fabs(point[2])<=1;
     38}
     39
     40bool Cuboid_impl::isOnSurface(const Vector &point){
     41  bool retVal = isInside(point);
     42  // test all borders of the cuboid
     43  // double fabs
     44  retVal = retVal &&
     45           ((fabs(fabs(point[0])-1)  < MYEPSILON) ||
     46            (fabs(fabs(point[1])-1)  < MYEPSILON) ||
     47            (fabs(fabs(point[2])-1)  < MYEPSILON));
     48  return retVal;
     49}
     50
     51Vector Cuboid_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
     52  if(!isOnSurface(point)){
     53    throw NotOnSurfaceException(__FILE__,__LINE__);
     54  }
     55  Vector res;
     56  // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
     57  for(int i=NDIM;i--;){
     58    if(fabs(fabs(point[i])-1)<MYEPSILON){
     59      // add the scaled (-1/+1) Vector to the set of surface vectors
     60      res[i] = point[i];
     61    }
     62  }
     63  ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
     64
     65  res.Normalize();
     66  return res;
    2467}
    2568
    2669Shape Cuboid(){
    27   Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
     70  Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
    2871  return Shape(impl);
    2972}
  • src/Shapes/BaseShapes_impl.hpp

    r06300d r5de9da  
    1313class Sphere_impl : public Shape_impl {
    1414  virtual bool isInside(const Vector &point);
     15  virtual bool isOnSurface(const Vector &point);
     16  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException);
    1517};
    1618
    1719class Cuboid_impl : public Shape_impl {
    1820  virtual bool isInside(const Vector &point);
     21  virtual bool isOnSurface(const Vector &point);
     22  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException);
    1923};
    2024
  • src/Shapes/Shape.cpp

    r06300d r5de9da  
    99#include "Shape_impl.hpp"
    1010
     11#include "Helpers/Assert.hpp"
     12
    1113Shape::Shape(const Shape& src) :
    1214  impl(src.getImpl())
     
    1719bool Shape::isInside(const Vector &point) const{
    1820  return impl->isInside(point);
     21}
     22
     23bool Shape::isOnSurface(const Vector &point) const{
     24  return impl->isOnSurface(point);
     25}
     26
     27Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
     28  return impl->getNormal(point);
    1929}
    2030
     
    6575}
    6676
     77bool AndShape_impl::isOnSurface(const Vector &point){
     78  // check the number of surfaces that this point is on
     79  int surfaces =0;
     80  surfaces += lhs->isOnSurface(point);
     81  surfaces += rhs->isOnSurface(point);
     82
     83  switch(surfaces){
     84    case 0:
     85      return false;
     86      // no break necessary
     87    case 1:
     88      // if it is inside for the object where it does not lie on
     89      // the surface the whole point lies inside
     90      return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
     91             (rhs->isOnSurface(point) && !lhs->isInside(point));
     92      // no break necessary
     93    case 2:
     94      {
     95        // it lies on both Shapes... could be an edge or an inner point
     96        // test the direction of the normals
     97        Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
     98        // if the directions are opposite we lie on the inside
     99        return !direction.IsZero();
     100      }
     101      // no break necessary
     102    default:
     103      // if this happens there is something wrong
     104      ASSERT(0,"Default case should have never been used");
     105  }
     106  return false; // never reached
     107}
     108
     109Vector AndShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
     110  Vector res;
     111  if(!isOnSurface(point)){
     112    throw NotOnSurfaceException(__FILE__,__LINE__);
     113  }
     114  res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
     115  res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
     116  res.Normalize();
     117  return res;
     118}
     119
    67120Shape operator&&(const Shape &lhs,const Shape &rhs){
    68121  Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
     
    82135}
    83136
     137bool OrShape_impl::isOnSurface(const Vector &point){
     138  // check the number of surfaces that this point is on
     139  int surfaces =0;
     140  surfaces += lhs->isOnSurface(point);
     141  surfaces += rhs->isOnSurface(point);
     142
     143  switch(surfaces){
     144    case 0:
     145      return false;
     146      // no break necessary
     147    case 1:
     148      // if it is inside for the object where it does not lie on
     149      // the surface the whole point lies inside
     150      return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
     151             (rhs->isOnSurface(point) && lhs->isInside(point));
     152      // no break necessary
     153    case 2:
     154      {
     155        // it lies on both Shapes... could be an edge or an inner point
     156        // test the direction of the normals
     157        Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
     158        // if the directions are opposite we lie on the inside
     159        return !direction.IsZero();
     160      }
     161      // no break necessary
     162    default:
     163      // if this happens there is something wrong
     164      ASSERT(0,"Default case should have never been used");
     165  }
     166  return false; // never reached
     167}
     168
     169Vector OrShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
     170  Vector res;
     171  if(!isOnSurface(point)){
     172    throw NotOnSurfaceException(__FILE__,__LINE__);
     173  }
     174  res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
     175  res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
     176  res.Normalize();
     177  return res;
     178}
     179
    84180Shape operator||(const Shape &lhs,const Shape &rhs){
    85181  Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
     
    99195}
    100196
     197bool NotShape_impl::isOnSurface(const Vector &point){
     198  return arg->isOnSurface(point);
     199}
     200
     201Vector NotShape_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
     202  return -1*arg->getNormal(point);
     203}
     204
    101205Shape operator!(const Shape &arg){
    102206  Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
  • src/Shapes/Shape.hpp

    r06300d r5de9da  
    1010
    1111#include <boost/shared_ptr.hpp>
     12
     13#include "Exceptions/NotOnSurfaceException.hpp"
    1214
    1315class Vector;
     
    2527
    2628  bool isInside(const Vector &point) const;
     29  bool isOnSurface(const Vector &point) const;
     30  Vector getNormal(const Vector &point) const throw(NotOnSurfaceException);
    2731
    2832  Shape &operator=(const Shape& rhs);
  • src/Shapes/ShapeOps.cpp

    r06300d r5de9da  
    1111#include "Helpers/Assert.hpp"
    1212
     13/*************** Base case ***********************/
     14
     15ShapeOpsBase_impl::ShapeOpsBase_impl(const Shape::impl_ptr &_arg) :
     16  arg(_arg){}
     17
     18ShapeOpsBase_impl::~ShapeOpsBase_impl(){}
     19
     20bool ShapeOpsBase_impl::isInside(const Vector &point){
     21  return arg->isInside(translateIn(point));
     22}
     23
     24bool ShapeOpsBase_impl::isOnSurface(const Vector &point){
     25  return arg->isOnSurface(translateIn(point));
     26}
     27
     28Vector ShapeOpsBase_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
     29  Vector helper = translateIn(point);
     30  if(!arg->isOnSurface(helper)){
     31    throw NotOnSurfaceException(__FILE__,__LINE__);
     32  }
     33  return translateOutNormal(arg->getNormal(helper));
     34}
     35
    1336/********************* Resize ********************/
    1437
    1538Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
    16   arg(_arg), size(_size)
     39  ShapeOpsBase_impl(_arg), size(_size)
    1740{
    1841  ASSERT(size>0,"Cannot resize a Shape to size zero or below");
     
    2144Resize_impl::~Resize_impl(){}
    2245
    23 bool Resize_impl::isInside(const Vector& point){
    24   return arg->isInside((1/size) * point);
     46Vector Resize_impl::translateIn(const Vector& point){
     47  return (1/size) * point;
     48}
     49
     50Vector Resize_impl::translateOutPos(const Vector& point){
     51  return size * point;
     52}
     53
     54Vector Resize_impl::translateOutNormal(const Vector& point){
     55  return point;
    2556}
    2657
     
    3364
    3465Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
    35   arg(_arg),offset(_offset)
     66  ShapeOpsBase_impl(_arg),offset(_offset)
    3667{}
    3768
    3869Translate_impl::~Translate_impl(){}
    3970
    40 bool Translate_impl::isInside(const Vector& point){
    41   return arg->isInside(point-offset);
     71Vector Translate_impl::translateIn(const Vector& point){
     72  return point-offset;
     73}
     74
     75Vector Translate_impl::translateOutPos(const Vector& point){
     76  return point+offset;
     77}
     78
     79Vector Translate_impl::translateOutNormal(const Vector& point){
     80  return point;
    4281}
    4382
     
    5089
    5190Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
    52   arg(_arg),factors(_factors)
     91  ShapeOpsBase_impl(_arg),factors(_factors)
    5392{
    5493  ASSERT(factors[0]>0,"cannot stretch a shape by a negative amount");
     
    62101Stretch_impl::~Stretch_impl(){}
    63102
    64 bool Stretch_impl::isInside(const Vector& point){
     103Vector Stretch_impl::translateIn(const Vector& point){
    65104  Vector helper=point;
    66105  helper.ScaleAll(reciFactors);
    67   return arg->isInside(helper);
     106  return helper;
     107}
     108
     109Vector Stretch_impl::translateOutPos(const Vector& point){
     110  Vector helper=point;
     111  helper.ScaleAll(factors);
     112  return helper;
     113}
     114
     115Vector Stretch_impl::translateOutNormal(const Vector& point){
     116  Vector helper=point;
     117  // the normalFactors are derived from appearances of the factors
     118  // with in the vectorproduct
     119  Vector normalFactors;
     120  normalFactors[0]=factors[1]*factors[2];
     121  normalFactors[1]=factors[0]*factors[2];
     122  normalFactors[2]=factors[0]*factors[1];
     123  helper.ScaleAll(normalFactors);
     124  return helper;
    68125}
    69126
     
    76133
    77134Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const Matrix &_transformation) :
    78   arg(_arg),transformation(_transformation)
     135  ShapeOpsBase_impl(_arg),transformation(_transformation)
    79136{
    80137  transformationInv = transformation.invert();
     
    83140Transform_impl::~Transform_impl(){}
    84141
    85 bool Transform_impl::isInside(const Vector& point){
    86   return arg->isInside(transformationInv * point);
     142Vector Transform_impl::translateIn(const Vector& point){
     143  return transformationInv * point;
     144}
     145
     146Vector Transform_impl::translateOutPos(const Vector& point){
     147  return transformation * point;
     148}
     149
     150Vector Transform_impl::translateOutNormal(const Vector& point){
     151  Matrix mat = transformation.determinant() * transformation.invert().transpose();
     152  return mat * point;
    87153}
    88154
  • src/Shapes/ShapeOps_impl.hpp

    r06300d r5de9da  
    1313#include "Matrix.hpp"
    1414
    15 class Resize_impl :  public Shape_impl
     15class ShapeOpsBase_impl : public Shape_impl{
     16public:
     17  ShapeOpsBase_impl(const Shape::impl_ptr&);
     18  virtual ~ShapeOpsBase_impl();
     19  virtual bool isInside(const Vector &point);
     20  virtual bool isOnSurface(const Vector &point);
     21  virtual Vector getNormal(const Vector &point) throw (NotOnSurfaceException);
     22protected:
     23  virtual Vector translateIn(const Vector &point)=0;
     24  virtual Vector translateOutPos(const Vector &point)=0;
     25  virtual Vector translateOutNormal(const Vector &point)=0;
     26private:
     27  Shape::impl_ptr arg;
     28};
     29
     30class Resize_impl :  public ShapeOpsBase_impl
    1631{
    1732public:
    1833  Resize_impl(const Shape::impl_ptr&,double);
    1934  virtual ~Resize_impl();
    20   virtual bool isInside(const Vector& point);
     35protected:
     36  virtual Vector translateIn(const Vector &point);
     37  virtual Vector translateOutPos(const Vector &point);
     38  virtual Vector translateOutNormal(const Vector &point);
    2139private:
    22   Shape::impl_ptr arg;
    2340  double size;
    2441};
    2542
    26 class Translate_impl :  public Shape_impl
     43class Translate_impl :  public ShapeOpsBase_impl
    2744{
    2845public:
    2946  Translate_impl(const Shape::impl_ptr&, const Vector&);
    3047  virtual ~Translate_impl();
    31   virtual bool isInside(const Vector& point);
     48protected:
     49  virtual Vector translateIn(const Vector &point);
     50  virtual Vector translateOutPos(const Vector &point);
     51  virtual Vector translateOutNormal(const Vector &point);
    3252private:
    33   Shape::impl_ptr arg;
    3453  Vector offset;
    3554};
    3655
    37 class Stretch_impl : public Shape_impl
     56class Stretch_impl : public ShapeOpsBase_impl
    3857{
    3958public:
    4059  Stretch_impl(const Shape::impl_ptr&, const Vector&);
    4160  virtual ~Stretch_impl();
    42   virtual bool isInside(const Vector& point);
     61protected:
     62  virtual Vector translateIn(const Vector &point);
     63  virtual Vector translateOutPos(const Vector &point);
     64  virtual Vector translateOutNormal(const Vector &point);
    4365private:
    44   Shape::impl_ptr arg;
    4566  Vector factors;
    4667  Vector reciFactors;
    4768};
    4869
    49 class Transform_impl : public Shape_impl
     70class Transform_impl : public ShapeOpsBase_impl
    5071{
    5172public:
    5273  Transform_impl(const Shape::impl_ptr&, const Matrix&);
    5374  virtual ~Transform_impl();
    54   virtual bool isInside(const Vector& point);
     75protected:
     76  virtual Vector translateIn(const Vector &point);
     77  virtual Vector translateOutPos(const Vector &point);
     78  virtual Vector translateOutNormal(const Vector &point);
    5579private:
    56   Shape::impl_ptr arg;
    5780  Matrix transformation;
    5881  Matrix transformationInv;
  • src/Shapes/Shape_impl.hpp

    r06300d r5de9da  
    1010
    1111#include "Shapes/Shape.hpp"
     12#include "vector.hpp"
    1213
    1314class Shape_impl {
     
    1617  virtual ~Shape_impl(){};
    1718  virtual bool isInside(const Vector &point)=0;
     19  virtual bool isOnSurface(const Vector &point)=0;
     20  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException)=0;
    1821};
    1922
     
    2326    return true;
    2427  }
     28  virtual bool isOnSurface(const Vector &point){
     29    return false;
     30  }
     31  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException){
     32    throw NotOnSurfaceException(__FILE__,__LINE__);
     33  }
    2534};
    2635
     
    2837  virtual bool isInside(const Vector &point){
    2938    return false;
     39  }
     40  virtual bool isOnSurface(const Vector &point){
     41    return false;
     42  }
     43  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException){
     44    throw NotOnSurfaceException(__FILE__,__LINE__);
    3045  }
    3146};
     
    3651  virtual ~AndShape_impl();
    3752  virtual bool isInside(const Vector &point);
     53  virtual bool isOnSurface(const Vector &point);
     54  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException);
    3855private:
    3956  Shape::impl_ptr lhs;
     
    4663  virtual ~OrShape_impl();
    4764  virtual bool isInside(const Vector &point);
     65  virtual bool isOnSurface(const Vector &point);
     66  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException);
    4867private:
    4968  Shape::impl_ptr lhs;
     
    5675  virtual ~NotShape_impl();
    5776  virtual bool isInside(const Vector &point);
     77  virtual bool isOnSurface(const Vector &point);
     78  virtual Vector getNormal(const Vector &point) throw(NotOnSurfaceException);
    5879private:
    5980  Shape::impl_ptr arg;
Note: See TracChangeset for help on using the changeset viewer.