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
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
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/>.
21 */
22
23/*
24 * Shape.cpp
25 *
26 * Created on: Jun 18, 2010
27 * Author: crueger
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include "CodePatterns/Assert.hpp"
38#include "LinearAlgebra/Vector.hpp"
39
40#include "Shapes/Shape.hpp"
41#include "Shapes/Shape_impl.hpp"
42#include "Shapes/ShapeExceptions.hpp"
43#include "Shapes/ShapeType.hpp"
44
45#include "Tesselation/ApproximateShapeArea.hpp"
46#include "Tesselation/ApproximateShapeVolume.hpp"
47
48#include <algorithm>
49#include <limits>
50#include <string>
51
52
53Shape::Shape(const Shape& src) :
54 impl(src.getImpl()), name(src.getName())
55{}
56
57Shape::~Shape(){}
58
59bool Shape::isInside(const Vector &point) const{
60 return impl->isInside(point);
61}
62
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
71Vector Shape::getCenter() const{
72 return impl->getCenter();
73}
74
75double Shape::getRadius() const{
76 return impl->getRadius();
77}
78
79void Shape::setName(const std::string &_name){
80 name = _name;
81}
82
83std::string Shape::getName() const{
84 return name;
85}
86
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 {
102 ApproximateShapeVolume Approximator(*this);
103 return Approximator();
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 {
122 ApproximateShapeArea Approximator(*this);
123 return Approximator();
124 }
125}
126
127LineSegmentSet Shape::getLineIntersections(const Line &line) const{
128 return impl->getLineIntersections(line);
129}
130
131std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
132 return impl->getHomogeneousPointsOnSurface(N);
133}
134
135std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {
136 return impl->getHomogeneousPointsInVolume(N);
137}
138
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
150bool Shape::operator==(const Shape &rhs) const{
151 return (this->getType() == rhs.getType());
152}
153
154std::string Shape::toString() const{
155 return impl->toString();
156}
157
158enum ShapeType Shape::getType() const{
159 return impl->getType();
160}
161
162Shape::impl_ptr Shape::getImpl() const{
163 return impl;
164}
165
166// allows arbitrary friendship, but only if implementation is known
167Shape::impl_ptr getShapeImpl(const Shape &shape){
168 return shape.getImpl();
169}
170
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
193bool AndShape_impl::isInside(const Vector &point) const{
194 return lhs->isInside(point) && rhs->isInside(point);
195}
196
197bool AndShape_impl::isOnSurface(const Vector &point) const{
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
210 return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
211 (rhs->isOnSurface(point) && lhs->isInside(point));
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
229Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
230 Vector res;
231 if(!isOnSurface(point)){
232 throw NotOnSurfaceException() << ShapeVector(&point);
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
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
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
272LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
273 return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
274}
275
276std::string AndShape_impl::toString() const{
277 return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
278}
279
280enum ShapeType AndShape_impl::getType() const{
281 return CombinedType;
282}
283
284std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
285 std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
286 std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
287 std::vector<Vector> PointsOnSurface;
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
298 return PointsOnSurface;
299}
300
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
307
308Shape operator&&(const Shape &lhs,const Shape &rhs){
309 Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
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
321bool OrShape_impl::isInside(const Vector &point) const{
322 return rhs->isInside(point) || lhs->isInside(point);
323}
324
325bool OrShape_impl::isOnSurface(const Vector &point) const{
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
338 return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
339 (rhs->isOnSurface(point) && !lhs->isInside(point));
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
357Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
358 Vector res;
359 if(!isOnSurface(point)){
360 throw NotOnSurfaceException() << ShapeVector(&point);
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
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
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
396LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
397 return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
398}
399
400std::string OrShape_impl::toString() const{
401 return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
402}
403
404enum ShapeType OrShape_impl::getType() const{
405 return CombinedType;
406}
407
408std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
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
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
431Shape operator||(const Shape &lhs,const Shape &rhs){
432 Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
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
444bool NotShape_impl::isInside(const Vector &point) const{
445 return !arg->isInside(point);
446}
447
448bool NotShape_impl::isOnSurface(const Vector &point) const{
449 return arg->isOnSurface(point);
450}
451
452Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
453 return -1.*arg->getNormal(point);
454}
455
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
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
478LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
479 return invert(arg->getLineIntersections(line));
480}
481
482std::string NotShape_impl::toString() const{
483 return std::string("!") + arg->toString();
484}
485
486enum ShapeType NotShape_impl::getType() const{
487 return CombinedType;
488}
489
490std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
491 // surfaces are the same, only normal direction is different
492 return arg->getHomogeneousPointsOnSurface(N);
493}
494
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
501Shape operator!(const Shape &arg){
502 Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
503 return Shape(newImpl);
504}
505
506/**************** global operations *********************************/
507std::ostream &operator<<(std::ostream &ost,const Shape &shape){
508 ost << shape.toString();
509 return ost;
510}
Note: See TracBrowser for help on using the repository browser.