source: src/Shapes/Shape.cpp@ ef84ca

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
Last change on this file since ef84ca was ef84ca, checked in by Tillmann Crueger <crueger@…>, 15 years ago

FIX: Mixed up "And" and "Or" cases for Shape-Surface calculation

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Shape.cpp
3 *
4 * Created on: Jun 18, 2010
5 * Author: crueger
6 */
7
8#include "Shape.hpp"
9#include "Shape_impl.hpp"
10
11#include "Helpers/Assert.hpp"
12
13Shape::Shape(const Shape& src) :
14 impl(src.getImpl())
15{}
16
17Shape::~Shape(){}
18
19bool Shape::isInside(const Vector &point) const{
20 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);
29}
30
31Shape::Shape(Shape::impl_ptr _impl) :
32 impl(_impl)
33{}
34
35Shape &Shape::operator=(const Shape& rhs){
36 if(&rhs!=this){
37 impl=rhs.getImpl();
38 }
39 return *this;
40}
41
42Shape::impl_ptr Shape::getImpl() const{
43 return impl;
44}
45
46// allows arbitrary friendship, but only if implementation is known
47Shape::impl_ptr getShapeImpl(const Shape &shape){
48 return shape.getImpl();
49}
50
51/***************************** Some simple Shapes ***************************/
52
53Shape Everywhere(){
54 static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
55 return Shape(impl);
56}
57
58Shape Nowhere(){
59 static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
60 return Shape(impl);
61}
62
63/****************************** Operators ***********************************/
64
65// AND
66
67AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
68 lhs(_lhs),rhs(_rhs)
69{}
70
71AndShape_impl::~AndShape_impl(){}
72
73bool AndShape_impl::isInside(const Vector &point){
74 return lhs->isInside(point) && rhs->isInside(point);
75}
76
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
120Shape operator&&(const Shape &lhs,const Shape &rhs){
121 Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
122 return Shape(newImpl);
123}
124
125// OR
126
127OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
128 lhs(_lhs),rhs(_rhs)
129{}
130
131OrShape_impl::~OrShape_impl(){}
132
133bool OrShape_impl::isInside(const Vector &point){
134 return rhs->isInside(point) || lhs->isInside(point);
135}
136
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
180Shape operator||(const Shape &lhs,const Shape &rhs){
181 Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
182 return Shape(newImpl);
183}
184
185// NOT
186
187NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
188 arg(_arg)
189{}
190
191NotShape_impl::~NotShape_impl(){}
192
193bool NotShape_impl::isInside(const Vector &point){
194 return !arg->isInside(point);
195}
196
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
205Shape operator!(const Shape &arg){
206 Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
207 return Shape(newImpl);
208}
Note: See TracBrowser for help on using the repository browser.