source: src/Shapes/Shape.cpp@ 145498

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

Shape: added name property for use in ShapeRegistry

  • Property mode set to 100644
File size: 14.3 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]21 */
22
[997784]23/*
24 * Shape.cpp
25 *
26 * Created on: Jun 18, 2010
27 * Author: crueger
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
[ad011c]35#include "CodePatterns/MemDebug.hpp"
[997784]36
[ad011c]37#include "CodePatterns/Assert.hpp"
[6c438f]38#include "LinearAlgebra/Vector.hpp"
39
[b94634]40#include "Shapes/Shape.hpp"
41#include "Shapes/Shape_impl.hpp"
42#include "Shapes/ShapeExceptions.hpp"
[b92e4a]43#include "Shapes/ShapeType.hpp"
[5de9da]44
[da1e92]45#include "Tesselation/ApproximateShapeArea.hpp"
46#include "Tesselation/ApproximateShapeVolume.hpp"
47
[6acc2f3]48#include <algorithm>
49#include <limits>
[cfda65]50#include <string>
51
52
[997784]53Shape::Shape(const Shape& src) :
[d0cd6d]54 impl(src.getImpl()), name(src.getName())
[997784]55{}
56
57Shape::~Shape(){}
58
[205d9b]59bool Shape::isInside(const Vector &point) const{
[997784]60 return impl->isInside(point);
61}
62
[5de9da]63bool Shape::isOnSurface(const Vector &point) const{
64 return impl->isOnSurface(point);
65}
66
67Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
68 return impl->getNormal(point);
69}
70
[6acc2f3]71Vector Shape::getCenter() const{
72 return impl->getCenter();
73}
74
75double Shape::getRadius() const{
76 return impl->getRadius();
77}
78
[d0cd6d]79void Shape::setName(const std::string &_name){
80 name = _name;
81}
82
83std::string Shape::getName() const{
84 return name;
85}
86
[c67c65]87/** Returns the volume of the Shape.
88 *
89 * If the underlying implementation does not have a working implementation,
90 * i.e. returns -1., then we use an approximate method to calculate the
91 * volume via a mesh of grid points and checking for isInside (basically
92 * a Monte-Carlo integration of the volume).
93 *
94 * \return volume of the shape
95 */
96double Shape::getVolume() const
97{
98 const double volume = impl->getVolume();
99 if (volume != -1.) {
100 return volume;
101 } else {
[da1e92]102 ApproximateShapeVolume Approximator(*this);
103 return Approximator();
[c67c65]104 }
105}
106
107/** Returns the surface area of the Shape.
108 *
109 * If the underlying implementation does not have a working implementation,
110 * i.e. returns -1., then we use the working filling of the shapes surface
111 * with points and subsequent tesselation and obtaining the approximate
112 * surface area therefrom.
113 *
114 * @return surface area of the Shape
115 */
116double Shape::getSurfaceArea() const
117{
118 const double surfacearea = impl->getSurfaceArea();
119 if (surfacearea != -1.) {
120 return surfacearea;
121 } else {
[da1e92]122 ApproximateShapeArea Approximator(*this);
123 return Approximator();
[c67c65]124 }
125}
126
[735940]127LineSegmentSet Shape::getLineIntersections(const Line &line) const{
[c6f395]128 return impl->getLineIntersections(line);
129}
130
[9c1c89]131std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
[c5186e]132 return impl->getHomogeneousPointsOnSurface(N);
133}
134
[5a8d61]135std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {
136 return impl->getHomogeneousPointsInVolume(N);
137}
138
[997784]139Shape::Shape(Shape::impl_ptr _impl) :
140 impl(_impl)
141{}
142
143Shape &Shape::operator=(const Shape& rhs){
144 if(&rhs!=this){
145 impl=rhs.getImpl();
146 }
147 return *this;
148}
149
[b92e4a]150bool Shape::operator==(const Shape &rhs) const{
151 return (this->getType() == rhs.getType());
152}
153
[cfda65]154std::string Shape::toString() const{
155 return impl->toString();
156}
157
[b92e4a]158enum ShapeType Shape::getType() const{
159 return impl->getType();
160}
161
[997784]162Shape::impl_ptr Shape::getImpl() const{
163 return impl;
164}
165
[e09b70]166// allows arbitrary friendship, but only if implementation is known
167Shape::impl_ptr getShapeImpl(const Shape &shape){
168 return shape.getImpl();
169}
170
[997784]171/***************************** Some simple Shapes ***************************/
172
173Shape Everywhere(){
174 static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
175 return Shape(impl);
176}
177
178Shape Nowhere(){
179 static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
180 return Shape(impl);
181}
182
183/****************************** Operators ***********************************/
184
185// AND
186
187AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
188 lhs(_lhs),rhs(_rhs)
189{}
190
191AndShape_impl::~AndShape_impl(){}
192
[735940]193bool AndShape_impl::isInside(const Vector &point) const{
[997784]194 return lhs->isInside(point) && rhs->isInside(point);
195}
196
[735940]197bool AndShape_impl::isOnSurface(const Vector &point) const{
[5de9da]198 // check the number of surfaces that this point is on
199 int surfaces =0;
200 surfaces += lhs->isOnSurface(point);
201 surfaces += rhs->isOnSurface(point);
202
203 switch(surfaces){
204 case 0:
205 return false;
206 // no break necessary
207 case 1:
208 // if it is inside for the object where it does not lie on
209 // the surface the whole point lies inside
[ef84ca]210 return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
211 (rhs->isOnSurface(point) && lhs->isInside(point));
[5de9da]212 // no break necessary
213 case 2:
214 {
215 // it lies on both Shapes... could be an edge or an inner point
216 // test the direction of the normals
217 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
218 // if the directions are opposite we lie on the inside
219 return !direction.IsZero();
220 }
221 // no break necessary
222 default:
223 // if this happens there is something wrong
224 ASSERT(0,"Default case should have never been used");
225 }
226 return false; // never reached
227}
228
[735940]229Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
[5de9da]230 Vector res;
231 if(!isOnSurface(point)){
[b94634]232 throw NotOnSurfaceException() << ShapeVector(&point);
[5de9da]233 }
234 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
235 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
236 res.Normalize();
237 return res;
238}
239
[6acc2f3]240Vector AndShape_impl::getCenter() const
241{
242 // calculate closest position on sphere surface to other center ..
243 const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
244 const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
245 // .. and then it's right in between those two
246 return 0.5*(rhsDistance + lhsDistance);
247}
248
249double AndShape_impl::getRadius() const
250{
251 const double distance = (lhs->getCenter() - rhs->getCenter()).Norm();
252 const double minradii = std::min(lhs->getRadius(), rhs->getRadius());
253 // if no intersection
254 if (distance > (lhs->getRadius() + rhs->getRadius()))
255 return 0.;
256 else // if intersection it can only be the smaller one
257 return minradii;
258}
259
[c67c65]260double AndShape_impl::getVolume() const
261{
262 // TODO
263 return -1.;
264}
265
266double AndShape_impl::getSurfaceArea() const
267{
268 // TODO
269 return -1.;
270}
271
[735940]272LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
[c6f395]273 return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
274}
275
[b92e4a]276std::string AndShape_impl::toString() const{
[955b91]277 return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
[cfda65]278}
279
[b92e4a]280enum ShapeType AndShape_impl::getType() const{
281 return CombinedType;
282}
283
[9c1c89]284std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
[23bade]285 std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
286 std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
[c5186e]287 std::vector<Vector> PointsOnSurface;
[23bade]288
289 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
290 if (rhs->isInside(*iter))
291 PointsOnSurface.push_back(*iter);
292 }
293 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
294 if (lhs->isInside(*iter))
295 PointsOnSurface.push_back(*iter);
296 }
297
[c5186e]298 return PointsOnSurface;
299}
300
[5a8d61]301std::vector<Vector> AndShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
302 ASSERT(0,
303 "AndShape_impl::getHomogeneousPointsInVolume() - not implemented.");
304 return std::vector<Vector>();
305}
306
[c5186e]307
[997784]308Shape operator&&(const Shape &lhs,const Shape &rhs){
[e09b70]309 Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
[997784]310 return Shape(newImpl);
311}
312
313// OR
314
315OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
316 lhs(_lhs),rhs(_rhs)
317{}
318
319OrShape_impl::~OrShape_impl(){}
320
[735940]321bool OrShape_impl::isInside(const Vector &point) const{
[997784]322 return rhs->isInside(point) || lhs->isInside(point);
323}
324
[735940]325bool OrShape_impl::isOnSurface(const Vector &point) const{
[5de9da]326 // check the number of surfaces that this point is on
327 int surfaces =0;
328 surfaces += lhs->isOnSurface(point);
329 surfaces += rhs->isOnSurface(point);
330
331 switch(surfaces){
332 case 0:
333 return false;
334 // no break necessary
335 case 1:
336 // if it is inside for the object where it does not lie on
337 // the surface the whole point lies inside
[ef84ca]338 return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
339 (rhs->isOnSurface(point) && !lhs->isInside(point));
[5de9da]340 // no break necessary
341 case 2:
342 {
343 // it lies on both Shapes... could be an edge or an inner point
344 // test the direction of the normals
345 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
346 // if the directions are opposite we lie on the inside
347 return !direction.IsZero();
348 }
349 // no break necessary
350 default:
351 // if this happens there is something wrong
352 ASSERT(0,"Default case should have never been used");
353 }
354 return false; // never reached
355}
356
[735940]357Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
[5de9da]358 Vector res;
359 if(!isOnSurface(point)){
[b94634]360 throw NotOnSurfaceException() << ShapeVector(&point);
[5de9da]361 }
362 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
363 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
364 res.Normalize();
365 return res;
366}
367
[6acc2f3]368Vector OrShape_impl::getCenter() const
369{
370 // calculate furthest position on sphere surface to other center ..
371 const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
372 const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
373 // .. and then it's right in between those two
374 return .5*(rhsDistance + lhsDistance);
375}
376
377double OrShape_impl::getRadius() const
378{
379 const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
380 const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
381 return .5*(rhsDistance - lhsDistance).Norm();
382}
383
[c67c65]384double OrShape_impl::getVolume() const
385{
386 // TODO
387 return -1.;
388}
389
390double OrShape_impl::getSurfaceArea() const
391{
392 // TODO
393 return -1.;
394}
395
[735940]396LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
[c6f395]397 return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
398}
399
[b92e4a]400std::string OrShape_impl::toString() const{
[955b91]401 return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
[cfda65]402}
403
[b92e4a]404enum ShapeType OrShape_impl::getType() const{
405 return CombinedType;
406}
407
[9c1c89]408std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
[c5186e]409 std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
410 std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
411 std::vector<Vector> PointsOnSurface;
412
413 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
414 if (!rhs->isInside(*iter))
415 PointsOnSurface.push_back(*iter);
416 }
417 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
418 if (!lhs->isInside(*iter))
419 PointsOnSurface.push_back(*iter);
420 }
421
422 return PointsOnSurface;
423}
424
[5a8d61]425std::vector<Vector> OrShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
426 ASSERT(0,
427 "OrShape_impl::getHomogeneousPointsInVolume() - not implemented.");
428 return std::vector<Vector>();
429}
430
[997784]431Shape operator||(const Shape &lhs,const Shape &rhs){
[e09b70]432 Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
[997784]433 return Shape(newImpl);
434}
435
436// NOT
437
438NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
439 arg(_arg)
440{}
441
442NotShape_impl::~NotShape_impl(){}
443
[735940]444bool NotShape_impl::isInside(const Vector &point) const{
[997784]445 return !arg->isInside(point);
446}
447
[735940]448bool NotShape_impl::isOnSurface(const Vector &point) const{
[5de9da]449 return arg->isOnSurface(point);
450}
451
[735940]452Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
[b94634]453 return -1.*arg->getNormal(point);
[5de9da]454}
455
[6acc2f3]456Vector NotShape_impl::getCenter() const
457{
458 return arg->getCenter();
459}
460
461double NotShape_impl::getRadius() const
462{
463 return std::numeric_limits<double>::infinity();
464}
465
[c67c65]466double NotShape_impl::getVolume() const
467{
468 // TODO
469 return -1.; //-arg->getVolume();
470}
471
472double NotShape_impl::getSurfaceArea() const
473{
474 // TODO
475 return -1.; // -arg->getSurfaceArea();
476}
477
[735940]478LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
[c6f395]479 return invert(arg->getLineIntersections(line));
480}
481
[b92e4a]482std::string NotShape_impl::toString() const{
[955b91]483 return std::string("!") + arg->toString();
[cfda65]484}
485
[b92e4a]486enum ShapeType NotShape_impl::getType() const{
487 return CombinedType;
488}
489
[9c1c89]490std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
[c5186e]491 // surfaces are the same, only normal direction is different
492 return arg->getHomogeneousPointsOnSurface(N);
493}
494
[5a8d61]495std::vector<Vector> NotShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
496 ASSERT(0,
497 "NotShape_impl::getHomogeneousPointsInVolume() - not implemented.");
498 return std::vector<Vector>();
499}
500
[997784]501Shape operator!(const Shape &arg){
[e09b70]502 Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
[997784]503 return Shape(newImpl);
504}
[cfda65]505
506/**************** global operations *********************************/
[955b91]507std::ostream &operator<<(std::ostream &ost,const Shape &shape){
[cfda65]508 ost << shape.toString();
509 return ost;
510}
Note: See TracBrowser for help on using the repository browser.