source: src/Shapes/Shape.cpp@ 735940

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

FIX: Made practically all Shape... member functions const.

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