source: src/Shapes/Shape.cpp@ b88fe4

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 b88fe4 was ad011c, checked in by Frederik Heber <heber@…>, 14 years ago

CodePatterns places all includes now in subfolder CodePatterns/.

  • change all includes accordingly.
  • this was necessary as Helpers and Patterns are not very distinctive names for include folders. Already now, we had a conflict between Helpers from CodePatterns and Helpers from this project.
  • changed compilation test in ax_codepatterns.m4 when changing CodePatterns includes.
  • Property mode set to 100644
File size: 8.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * Shape.cpp
10 *
11 * Created on: Jun 18, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include "CodePatterns/Assert.hpp"
23#include "LinearAlgebra/Vector.hpp"
24
25#include "Shape.hpp"
26#include "Shape_impl.hpp"
27
28#include <string>
29
30
31Shape::Shape(const Shape& src) :
32 impl(src.getImpl())
33{}
34
35Shape::~Shape(){}
36
37bool Shape::isInside(const Vector &point) const{
38 return impl->isInside(point);
39}
40
41bool Shape::isOnSurface(const Vector &point) const{
42 return impl->isOnSurface(point);
43}
44
45Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
46 return impl->getNormal(point);
47}
48
49LineSegmentSet Shape::getLineIntersections(const Line &line){
50 return impl->getLineIntersections(line);
51}
52
53std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
54 return impl->getHomogeneousPointsOnSurface(N);
55}
56
57Shape::Shape(Shape::impl_ptr _impl) :
58 impl(_impl)
59{}
60
61Shape &Shape::operator=(const Shape& rhs){
62 if(&rhs!=this){
63 impl=rhs.getImpl();
64 }
65 return *this;
66}
67
68std::string Shape::toString() const{
69 return impl->toString();
70}
71
72Shape::impl_ptr Shape::getImpl() const{
73 return impl;
74}
75
76// allows arbitrary friendship, but only if implementation is known
77Shape::impl_ptr getShapeImpl(const Shape &shape){
78 return shape.getImpl();
79}
80
81/***************************** Some simple Shapes ***************************/
82
83Shape Everywhere(){
84 static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
85 return Shape(impl);
86}
87
88Shape Nowhere(){
89 static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
90 return Shape(impl);
91}
92
93/****************************** Operators ***********************************/
94
95// AND
96
97AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
98 lhs(_lhs),rhs(_rhs)
99{}
100
101AndShape_impl::~AndShape_impl(){}
102
103bool AndShape_impl::isInside(const Vector &point){
104 return lhs->isInside(point) && rhs->isInside(point);
105}
106
107bool AndShape_impl::isOnSurface(const Vector &point){
108 // check the number of surfaces that this point is on
109 int surfaces =0;
110 surfaces += lhs->isOnSurface(point);
111 surfaces += rhs->isOnSurface(point);
112
113 switch(surfaces){
114 case 0:
115 return false;
116 // no break necessary
117 case 1:
118 // if it is inside for the object where it does not lie on
119 // the surface the whole point lies inside
120 return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
121 (rhs->isOnSurface(point) && lhs->isInside(point));
122 // no break necessary
123 case 2:
124 {
125 // it lies on both Shapes... could be an edge or an inner point
126 // test the direction of the normals
127 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
128 // if the directions are opposite we lie on the inside
129 return !direction.IsZero();
130 }
131 // no break necessary
132 default:
133 // if this happens there is something wrong
134 ASSERT(0,"Default case should have never been used");
135 }
136 return false; // never reached
137}
138
139Vector AndShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
140 Vector res;
141 if(!isOnSurface(point)){
142 throw NotOnSurfaceException(__FILE__,__LINE__);
143 }
144 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
145 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
146 res.Normalize();
147 return res;
148}
149
150LineSegmentSet AndShape_impl::getLineIntersections(const Line &line){
151 return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
152}
153
154string AndShape_impl::toString(){
155 return string("(") + lhs->toString() + string("&&") + rhs->toString() + string(")");
156}
157
158std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
159 std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
160 std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
161 std::vector<Vector> PointsOnSurface;
162
163 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
164 if (rhs->isInside(*iter))
165 PointsOnSurface.push_back(*iter);
166 }
167 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
168 if (lhs->isInside(*iter))
169 PointsOnSurface.push_back(*iter);
170 }
171
172 return PointsOnSurface;
173}
174
175
176Shape operator&&(const Shape &lhs,const Shape &rhs){
177 Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
178 return Shape(newImpl);
179}
180
181// OR
182
183OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
184 lhs(_lhs),rhs(_rhs)
185{}
186
187OrShape_impl::~OrShape_impl(){}
188
189bool OrShape_impl::isInside(const Vector &point){
190 return rhs->isInside(point) || lhs->isInside(point);
191}
192
193bool OrShape_impl::isOnSurface(const Vector &point){
194 // check the number of surfaces that this point is on
195 int surfaces =0;
196 surfaces += lhs->isOnSurface(point);
197 surfaces += rhs->isOnSurface(point);
198
199 switch(surfaces){
200 case 0:
201 return false;
202 // no break necessary
203 case 1:
204 // if it is inside for the object where it does not lie on
205 // the surface the whole point lies inside
206 return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
207 (rhs->isOnSurface(point) && !lhs->isInside(point));
208 // no break necessary
209 case 2:
210 {
211 // it lies on both Shapes... could be an edge or an inner point
212 // test the direction of the normals
213 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
214 // if the directions are opposite we lie on the inside
215 return !direction.IsZero();
216 }
217 // no break necessary
218 default:
219 // if this happens there is something wrong
220 ASSERT(0,"Default case should have never been used");
221 }
222 return false; // never reached
223}
224
225Vector OrShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
226 Vector res;
227 if(!isOnSurface(point)){
228 throw NotOnSurfaceException(__FILE__,__LINE__);
229 }
230 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
231 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
232 res.Normalize();
233 return res;
234}
235
236LineSegmentSet OrShape_impl::getLineIntersections(const Line &line){
237 return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
238}
239
240string OrShape_impl::toString(){
241 return string("(") + lhs->toString() + string("||") + rhs->toString() + string(")");
242}
243
244std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
245 std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
246 std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
247 std::vector<Vector> PointsOnSurface;
248
249 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
250 if (!rhs->isInside(*iter))
251 PointsOnSurface.push_back(*iter);
252 }
253 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
254 if (!lhs->isInside(*iter))
255 PointsOnSurface.push_back(*iter);
256 }
257
258 return PointsOnSurface;
259}
260
261Shape operator||(const Shape &lhs,const Shape &rhs){
262 Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
263 return Shape(newImpl);
264}
265
266// NOT
267
268NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
269 arg(_arg)
270{}
271
272NotShape_impl::~NotShape_impl(){}
273
274bool NotShape_impl::isInside(const Vector &point){
275 return !arg->isInside(point);
276}
277
278bool NotShape_impl::isOnSurface(const Vector &point){
279 return arg->isOnSurface(point);
280}
281
282Vector NotShape_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
283 return -1*arg->getNormal(point);
284}
285
286LineSegmentSet NotShape_impl::getLineIntersections(const Line &line){
287 return invert(arg->getLineIntersections(line));
288}
289
290string NotShape_impl::toString(){
291 return string("!") + arg->toString();
292}
293
294std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
295 // surfaces are the same, only normal direction is different
296 return arg->getHomogeneousPointsOnSurface(N);
297}
298
299Shape operator!(const Shape &arg){
300 Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
301 return Shape(newImpl);
302}
303
304/**************** global operations *********************************/
305ostream &operator<<(ostream &ost,const Shape &shape){
306 ost << shape.toString();
307 return ost;
308}
Note: See TracBrowser for help on using the repository browser.