source: src/Shapes/Shape.cpp@ 311137

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

Shape::getVolume() and ::getSurfaceArea() use Approximators if implementation returns -1.

  • We use the approximators via tesselation for surface area and via a stupid Monte Carlo integration for the volume.
  • implemented stubs to use with Shape unit tests.
  • Property mode set to 100644
File size: 13.5 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 "Tesselation/ApproximateShapeArea.hpp"
31#include "Tesselation/ApproximateShapeVolume.hpp"
32
33#include <algorithm>
34#include <limits>
35#include <string>
36
37
38Shape::Shape(const Shape& src) :
39 impl(src.getImpl())
40{}
41
42Shape::~Shape(){}
43
44bool Shape::isInside(const Vector &point) const{
45 return impl->isInside(point);
46}
47
48bool Shape::isOnSurface(const Vector &point) const{
49 return impl->isOnSurface(point);
50}
51
52Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
53 return impl->getNormal(point);
54}
55
56Vector Shape::getCenter() const{
57 return impl->getCenter();
58}
59
60double Shape::getRadius() const{
61 return impl->getRadius();
62}
63
64/** Returns the volume of the Shape.
65 *
66 * If the underlying implementation does not have a working implementation,
67 * i.e. returns -1., then we use an approximate method to calculate the
68 * volume via a mesh of grid points and checking for isInside (basically
69 * a Monte-Carlo integration of the volume).
70 *
71 * \return volume of the shape
72 */
73double Shape::getVolume() const
74{
75 const double volume = impl->getVolume();
76 if (volume != -1.) {
77 return volume;
78 } else {
79 ApproximateShapeVolume Approximator(*this);
80 return Approximator();
81 }
82}
83
84/** Returns the surface area of the Shape.
85 *
86 * If the underlying implementation does not have a working implementation,
87 * i.e. returns -1., then we use the working filling of the shapes surface
88 * with points and subsequent tesselation and obtaining the approximate
89 * surface area therefrom.
90 *
91 * @return surface area of the Shape
92 */
93double Shape::getSurfaceArea() const
94{
95 const double surfacearea = impl->getSurfaceArea();
96 if (surfacearea != -1.) {
97 return surfacearea;
98 } else {
99 ApproximateShapeArea Approximator(*this);
100 return Approximator();
101 }
102}
103
104LineSegmentSet Shape::getLineIntersections(const Line &line) const{
105 return impl->getLineIntersections(line);
106}
107
108std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
109 return impl->getHomogeneousPointsOnSurface(N);
110}
111
112std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {
113 return impl->getHomogeneousPointsInVolume(N);
114}
115
116Shape::Shape(Shape::impl_ptr _impl) :
117 impl(_impl)
118{}
119
120Shape &Shape::operator=(const Shape& rhs){
121 if(&rhs!=this){
122 impl=rhs.getImpl();
123 }
124 return *this;
125}
126
127bool Shape::operator==(const Shape &rhs) const{
128 return (this->getType() == rhs.getType());
129}
130
131std::string Shape::toString() const{
132 return impl->toString();
133}
134
135enum ShapeType Shape::getType() const{
136 return impl->getType();
137}
138
139Shape::impl_ptr Shape::getImpl() const{
140 return impl;
141}
142
143// allows arbitrary friendship, but only if implementation is known
144Shape::impl_ptr getShapeImpl(const Shape &shape){
145 return shape.getImpl();
146}
147
148/***************************** Some simple Shapes ***************************/
149
150Shape Everywhere(){
151 static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
152 return Shape(impl);
153}
154
155Shape Nowhere(){
156 static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
157 return Shape(impl);
158}
159
160/****************************** Operators ***********************************/
161
162// AND
163
164AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
165 lhs(_lhs),rhs(_rhs)
166{}
167
168AndShape_impl::~AndShape_impl(){}
169
170bool AndShape_impl::isInside(const Vector &point) const{
171 return lhs->isInside(point) && rhs->isInside(point);
172}
173
174bool AndShape_impl::isOnSurface(const Vector &point) const{
175 // check the number of surfaces that this point is on
176 int surfaces =0;
177 surfaces += lhs->isOnSurface(point);
178 surfaces += rhs->isOnSurface(point);
179
180 switch(surfaces){
181 case 0:
182 return false;
183 // no break necessary
184 case 1:
185 // if it is inside for the object where it does not lie on
186 // the surface the whole point lies inside
187 return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
188 (rhs->isOnSurface(point) && lhs->isInside(point));
189 // no break necessary
190 case 2:
191 {
192 // it lies on both Shapes... could be an edge or an inner point
193 // test the direction of the normals
194 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
195 // if the directions are opposite we lie on the inside
196 return !direction.IsZero();
197 }
198 // no break necessary
199 default:
200 // if this happens there is something wrong
201 ASSERT(0,"Default case should have never been used");
202 }
203 return false; // never reached
204}
205
206Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
207 Vector res;
208 if(!isOnSurface(point)){
209 throw NotOnSurfaceException() << ShapeVector(&point);
210 }
211 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
212 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
213 res.Normalize();
214 return res;
215}
216
217Vector AndShape_impl::getCenter() const
218{
219 // calculate closest position on sphere surface to other center ..
220 const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
221 const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
222 // .. and then it's right in between those two
223 return 0.5*(rhsDistance + lhsDistance);
224}
225
226double AndShape_impl::getRadius() const
227{
228 const double distance = (lhs->getCenter() - rhs->getCenter()).Norm();
229 const double minradii = std::min(lhs->getRadius(), rhs->getRadius());
230 // if no intersection
231 if (distance > (lhs->getRadius() + rhs->getRadius()))
232 return 0.;
233 else // if intersection it can only be the smaller one
234 return minradii;
235}
236
237double AndShape_impl::getVolume() const
238{
239 // TODO
240 return -1.;
241}
242
243double AndShape_impl::getSurfaceArea() const
244{
245 // TODO
246 return -1.;
247}
248
249LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
250 return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
251}
252
253std::string AndShape_impl::toString() const{
254 return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
255}
256
257enum ShapeType AndShape_impl::getType() const{
258 return CombinedType;
259}
260
261std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
262 std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
263 std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
264 std::vector<Vector> PointsOnSurface;
265
266 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
267 if (rhs->isInside(*iter))
268 PointsOnSurface.push_back(*iter);
269 }
270 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
271 if (lhs->isInside(*iter))
272 PointsOnSurface.push_back(*iter);
273 }
274
275 return PointsOnSurface;
276}
277
278std::vector<Vector> AndShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
279 ASSERT(0,
280 "AndShape_impl::getHomogeneousPointsInVolume() - not implemented.");
281 return std::vector<Vector>();
282}
283
284
285Shape operator&&(const Shape &lhs,const Shape &rhs){
286 Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
287 return Shape(newImpl);
288}
289
290// OR
291
292OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
293 lhs(_lhs),rhs(_rhs)
294{}
295
296OrShape_impl::~OrShape_impl(){}
297
298bool OrShape_impl::isInside(const Vector &point) const{
299 return rhs->isInside(point) || lhs->isInside(point);
300}
301
302bool OrShape_impl::isOnSurface(const Vector &point) const{
303 // check the number of surfaces that this point is on
304 int surfaces =0;
305 surfaces += lhs->isOnSurface(point);
306 surfaces += rhs->isOnSurface(point);
307
308 switch(surfaces){
309 case 0:
310 return false;
311 // no break necessary
312 case 1:
313 // if it is inside for the object where it does not lie on
314 // the surface the whole point lies inside
315 return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
316 (rhs->isOnSurface(point) && !lhs->isInside(point));
317 // no break necessary
318 case 2:
319 {
320 // it lies on both Shapes... could be an edge or an inner point
321 // test the direction of the normals
322 Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
323 // if the directions are opposite we lie on the inside
324 return !direction.IsZero();
325 }
326 // no break necessary
327 default:
328 // if this happens there is something wrong
329 ASSERT(0,"Default case should have never been used");
330 }
331 return false; // never reached
332}
333
334Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
335 Vector res;
336 if(!isOnSurface(point)){
337 throw NotOnSurfaceException() << ShapeVector(&point);
338 }
339 res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
340 res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
341 res.Normalize();
342 return res;
343}
344
345Vector OrShape_impl::getCenter() const
346{
347 // calculate furthest position on sphere surface to other center ..
348 const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
349 const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
350 // .. and then it's right in between those two
351 return .5*(rhsDistance + lhsDistance);
352}
353
354double OrShape_impl::getRadius() const
355{
356 const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
357 const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
358 return .5*(rhsDistance - lhsDistance).Norm();
359}
360
361double OrShape_impl::getVolume() const
362{
363 // TODO
364 return -1.;
365}
366
367double OrShape_impl::getSurfaceArea() const
368{
369 // TODO
370 return -1.;
371}
372
373LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
374 return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
375}
376
377std::string OrShape_impl::toString() const{
378 return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
379}
380
381enum ShapeType OrShape_impl::getType() const{
382 return CombinedType;
383}
384
385std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
386 std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
387 std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
388 std::vector<Vector> PointsOnSurface;
389
390 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
391 if (!rhs->isInside(*iter))
392 PointsOnSurface.push_back(*iter);
393 }
394 for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
395 if (!lhs->isInside(*iter))
396 PointsOnSurface.push_back(*iter);
397 }
398
399 return PointsOnSurface;
400}
401
402std::vector<Vector> OrShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
403 ASSERT(0,
404 "OrShape_impl::getHomogeneousPointsInVolume() - not implemented.");
405 return std::vector<Vector>();
406}
407
408Shape operator||(const Shape &lhs,const Shape &rhs){
409 Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
410 return Shape(newImpl);
411}
412
413// NOT
414
415NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
416 arg(_arg)
417{}
418
419NotShape_impl::~NotShape_impl(){}
420
421bool NotShape_impl::isInside(const Vector &point) const{
422 return !arg->isInside(point);
423}
424
425bool NotShape_impl::isOnSurface(const Vector &point) const{
426 return arg->isOnSurface(point);
427}
428
429Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
430 return -1.*arg->getNormal(point);
431}
432
433Vector NotShape_impl::getCenter() const
434{
435 return arg->getCenter();
436}
437
438double NotShape_impl::getRadius() const
439{
440 return std::numeric_limits<double>::infinity();
441}
442
443double NotShape_impl::getVolume() const
444{
445 // TODO
446 return -1.; //-arg->getVolume();
447}
448
449double NotShape_impl::getSurfaceArea() const
450{
451 // TODO
452 return -1.; // -arg->getSurfaceArea();
453}
454
455LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
456 return invert(arg->getLineIntersections(line));
457}
458
459std::string NotShape_impl::toString() const{
460 return std::string("!") + arg->toString();
461}
462
463enum ShapeType NotShape_impl::getType() const{
464 return CombinedType;
465}
466
467std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
468 // surfaces are the same, only normal direction is different
469 return arg->getHomogeneousPointsOnSurface(N);
470}
471
472std::vector<Vector> NotShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
473 ASSERT(0,
474 "NotShape_impl::getHomogeneousPointsInVolume() - not implemented.");
475 return std::vector<Vector>();
476}
477
478Shape operator!(const Shape &arg){
479 Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
480 return Shape(newImpl);
481}
482
483/**************** global operations *********************************/
484std::ostream &operator<<(std::ostream &ost,const Shape &shape){
485 ost << shape.toString();
486 return ost;
487}
Note: See TracBrowser for help on using the repository browser.