source: src/Shapes/Shape.cpp@ f3526d

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 f3526d was cfda65, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added methods to output shapes on the screen

  • Property mode set to 100644
File size: 6.0 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
13#include <string>
14
15using namespace std;
16
17Shape::Shape(const Shape& src) :
18 impl(src.getImpl())
19{}
20
21Shape::~Shape(){}
22
23bool Shape::isInside(const Vector &point) const{
24 return impl->isInside(point);
25}
26
27bool Shape::isOnSurface(const Vector &point) const{
28 return impl->isOnSurface(point);
29}
30
31Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
32 return impl->getNormal(point);
33}
34
35Shape::Shape(Shape::impl_ptr _impl) :
36 impl(_impl)
37{}
38
39Shape &Shape::operator=(const Shape& rhs){
40 if(&rhs!=this){
41 impl=rhs.getImpl();
42 }
43 return *this;
44}
45
46std::string Shape::toString() const{
47 return impl->toString();
48}
49
50Shape::impl_ptr Shape::getImpl() const{
51 return impl;
52}
53
54// allows arbitrary friendship, but only if implementation is known
55Shape::impl_ptr getShapeImpl(const Shape &shape){
56 return shape.getImpl();
57}
58
59/***************************** Some simple Shapes ***************************/
60
61Shape Everywhere(){
62 static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
63 return Shape(impl);
64}
65
66Shape Nowhere(){
67 static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
68 return Shape(impl);
69}
70
71/****************************** Operators ***********************************/
72
73// AND
74
75AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
76 lhs(_lhs),rhs(_rhs)
77{}
78
79AndShape_impl::~AndShape_impl(){}
80
81bool AndShape_impl::isInside(const Vector &point){
82 return lhs->isInside(point) && rhs->isInside(point);
83}
84
85bool AndShape_impl::isOnSurface(const Vector &point){
86 // check the number of surfaces that this point is on
87 int surfaces =0;
88 surfaces += lhs->isOnSurface(point);
89 surfaces += rhs->isOnSurface(point);
90
91 switch(surfaces){
92 case 0:
93 return false;
94 // no break necessary
95 case 1:
96 // if it is inside for the object where it does not lie on
97 // the surface the whole point lies inside
98 return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
99 (rhs->isOnSurface(point) && lhs->isInside(point));
100 // no break necessary
101 case 2:
102 {
103 // it lies on both Shapes... could be an edge or an inner point
104 // test the direction of the normals
105 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
106 // if the directions are opposite we lie on the inside
107 return !direction.IsZero();
108 }
109 // no break necessary
110 default:
111 // if this happens there is something wrong
112 ASSERT(0,"Default case should have never been used");
113 }
114 return false; // never reached
115}
116
117Vector AndShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
118 Vector res;
119 if(!isOnSurface(point)){
120 throw NotOnSurfaceException(__FILE__,__LINE__);
121 }
122 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
123 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
124 res.Normalize();
125 return res;
126}
127
128string AndShape_impl::toString(){
129 return string("(") + lhs->toString() + string("&&") + rhs->toString() + string(")");
130}
131
132Shape operator&&(const Shape &lhs,const Shape &rhs){
133 Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
134 return Shape(newImpl);
135}
136
137// OR
138
139OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
140 lhs(_lhs),rhs(_rhs)
141{}
142
143OrShape_impl::~OrShape_impl(){}
144
145bool OrShape_impl::isInside(const Vector &point){
146 return rhs->isInside(point) || lhs->isInside(point);
147}
148
149bool OrShape_impl::isOnSurface(const Vector &point){
150 // check the number of surfaces that this point is on
151 int surfaces =0;
152 surfaces += lhs->isOnSurface(point);
153 surfaces += rhs->isOnSurface(point);
154
155 switch(surfaces){
156 case 0:
157 return false;
158 // no break necessary
159 case 1:
160 // if it is inside for the object where it does not lie on
161 // the surface the whole point lies inside
162 return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
163 (rhs->isOnSurface(point) && !lhs->isInside(point));
164 // no break necessary
165 case 2:
166 {
167 // it lies on both Shapes... could be an edge or an inner point
168 // test the direction of the normals
169 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
170 // if the directions are opposite we lie on the inside
171 return !direction.IsZero();
172 }
173 // no break necessary
174 default:
175 // if this happens there is something wrong
176 ASSERT(0,"Default case should have never been used");
177 }
178 return false; // never reached
179}
180
181Vector OrShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
182 Vector res;
183 if(!isOnSurface(point)){
184 throw NotOnSurfaceException(__FILE__,__LINE__);
185 }
186 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
187 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
188 res.Normalize();
189 return res;
190}
191
192string OrShape_impl::toString(){
193 return string("(") + lhs->toString() + string("||") + rhs->toString() + string(")");
194}
195
196Shape operator||(const Shape &lhs,const Shape &rhs){
197 Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
198 return Shape(newImpl);
199}
200
201// NOT
202
203NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
204 arg(_arg)
205{}
206
207NotShape_impl::~NotShape_impl(){}
208
209bool NotShape_impl::isInside(const Vector &point){
210 return !arg->isInside(point);
211}
212
213bool NotShape_impl::isOnSurface(const Vector &point){
214 return arg->isOnSurface(point);
215}
216
217Vector NotShape_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
218 return -1*arg->getNormal(point);
219}
220
221string NotShape_impl::toString(){
222 return string("!") + arg->toString();
223}
224
225Shape operator!(const Shape &arg){
226 Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
227 return Shape(newImpl);
228}
229
230/**************** global operations *********************************/
231ostream &operator<<(ostream &ost,const Shape &shape){
232 ost << shape.toString();
233 return ost;
234}
Note: See TracBrowser for help on using the repository browser.