source: src/Shapes/Shape.cpp@ 595cfd

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

Added preliminary getVolume() and getSurfaceArea() implementation to all Shapes.

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