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 |
|
---|
35 | Shape::Shape(const Shape& src) :
|
---|
36 | impl(src.getImpl())
|
---|
37 | {}
|
---|
38 |
|
---|
39 | Shape::~Shape(){}
|
---|
40 |
|
---|
41 | bool Shape::isInside(const Vector &point) const{
|
---|
42 | return impl->isInside(point);
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool Shape::isOnSurface(const Vector &point) const{
|
---|
46 | return impl->isOnSurface(point);
|
---|
47 | }
|
---|
48 |
|
---|
49 | Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
---|
50 | return impl->getNormal(point);
|
---|
51 | }
|
---|
52 |
|
---|
53 | Vector Shape::getCenter() const{
|
---|
54 | return impl->getCenter();
|
---|
55 | }
|
---|
56 |
|
---|
57 | double 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 | */
|
---|
70 | double 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 | */
|
---|
89 | double 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 |
|
---|
99 | LineSegmentSet Shape::getLineIntersections(const Line &line) const{
|
---|
100 | return impl->getLineIntersections(line);
|
---|
101 | }
|
---|
102 |
|
---|
103 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
104 | return impl->getHomogeneousPointsOnSurface(N);
|
---|
105 | }
|
---|
106 |
|
---|
107 | std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
108 | return impl->getHomogeneousPointsInVolume(N);
|
---|
109 | }
|
---|
110 |
|
---|
111 | Shape::Shape(Shape::impl_ptr _impl) :
|
---|
112 | impl(_impl)
|
---|
113 | {}
|
---|
114 |
|
---|
115 | Shape &Shape::operator=(const Shape& rhs){
|
---|
116 | if(&rhs!=this){
|
---|
117 | impl=rhs.getImpl();
|
---|
118 | }
|
---|
119 | return *this;
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool Shape::operator==(const Shape &rhs) const{
|
---|
123 | return (this->getType() == rhs.getType());
|
---|
124 | }
|
---|
125 |
|
---|
126 | std::string Shape::toString() const{
|
---|
127 | return impl->toString();
|
---|
128 | }
|
---|
129 |
|
---|
130 | enum ShapeType Shape::getType() const{
|
---|
131 | return impl->getType();
|
---|
132 | }
|
---|
133 |
|
---|
134 | Shape::impl_ptr Shape::getImpl() const{
|
---|
135 | return impl;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // allows arbitrary friendship, but only if implementation is known
|
---|
139 | Shape::impl_ptr getShapeImpl(const Shape &shape){
|
---|
140 | return shape.getImpl();
|
---|
141 | }
|
---|
142 |
|
---|
143 | /***************************** Some simple Shapes ***************************/
|
---|
144 |
|
---|
145 | Shape Everywhere(){
|
---|
146 | static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
|
---|
147 | return Shape(impl);
|
---|
148 | }
|
---|
149 |
|
---|
150 | Shape 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 |
|
---|
159 | AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
---|
160 | lhs(_lhs),rhs(_rhs)
|
---|
161 | {}
|
---|
162 |
|
---|
163 | AndShape_impl::~AndShape_impl(){}
|
---|
164 |
|
---|
165 | bool AndShape_impl::isInside(const Vector &point) const{
|
---|
166 | return lhs->isInside(point) && rhs->isInside(point);
|
---|
167 | }
|
---|
168 |
|
---|
169 | bool 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 |
|
---|
201 | Vector 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 |
|
---|
212 | Vector 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 |
|
---|
221 | double 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 |
|
---|
232 | double AndShape_impl::getVolume() const
|
---|
233 | {
|
---|
234 | // TODO
|
---|
235 | return -1.;
|
---|
236 | }
|
---|
237 |
|
---|
238 | double AndShape_impl::getSurfaceArea() const
|
---|
239 | {
|
---|
240 | // TODO
|
---|
241 | return -1.;
|
---|
242 | }
|
---|
243 |
|
---|
244 | LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
|
---|
245 | return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
|
---|
246 | }
|
---|
247 |
|
---|
248 | std::string AndShape_impl::toString() const{
|
---|
249 | return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
|
---|
250 | }
|
---|
251 |
|
---|
252 | enum ShapeType AndShape_impl::getType() const{
|
---|
253 | return CombinedType;
|
---|
254 | }
|
---|
255 |
|
---|
256 | std::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 |
|
---|
273 | std::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 |
|
---|
280 | Shape 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 |
|
---|
287 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
---|
288 | lhs(_lhs),rhs(_rhs)
|
---|
289 | {}
|
---|
290 |
|
---|
291 | OrShape_impl::~OrShape_impl(){}
|
---|
292 |
|
---|
293 | bool OrShape_impl::isInside(const Vector &point) const{
|
---|
294 | return rhs->isInside(point) || lhs->isInside(point);
|
---|
295 | }
|
---|
296 |
|
---|
297 | bool 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 |
|
---|
329 | Vector 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 |
|
---|
340 | Vector 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 |
|
---|
349 | double 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 |
|
---|
356 | double OrShape_impl::getVolume() const
|
---|
357 | {
|
---|
358 | // TODO
|
---|
359 | return -1.;
|
---|
360 | }
|
---|
361 |
|
---|
362 | double OrShape_impl::getSurfaceArea() const
|
---|
363 | {
|
---|
364 | // TODO
|
---|
365 | return -1.;
|
---|
366 | }
|
---|
367 |
|
---|
368 | LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
|
---|
369 | return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
|
---|
370 | }
|
---|
371 |
|
---|
372 | std::string OrShape_impl::toString() const{
|
---|
373 | return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
|
---|
374 | }
|
---|
375 |
|
---|
376 | enum ShapeType OrShape_impl::getType() const{
|
---|
377 | return CombinedType;
|
---|
378 | }
|
---|
379 |
|
---|
380 | std::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 |
|
---|
397 | std::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 |
|
---|
403 | Shape 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 |
|
---|
410 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
|
---|
411 | arg(_arg)
|
---|
412 | {}
|
---|
413 |
|
---|
414 | NotShape_impl::~NotShape_impl(){}
|
---|
415 |
|
---|
416 | bool NotShape_impl::isInside(const Vector &point) const{
|
---|
417 | return !arg->isInside(point);
|
---|
418 | }
|
---|
419 |
|
---|
420 | bool NotShape_impl::isOnSurface(const Vector &point) const{
|
---|
421 | return arg->isOnSurface(point);
|
---|
422 | }
|
---|
423 |
|
---|
424 | Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
---|
425 | return -1.*arg->getNormal(point);
|
---|
426 | }
|
---|
427 |
|
---|
428 | Vector NotShape_impl::getCenter() const
|
---|
429 | {
|
---|
430 | return arg->getCenter();
|
---|
431 | }
|
---|
432 |
|
---|
433 | double NotShape_impl::getRadius() const
|
---|
434 | {
|
---|
435 | return std::numeric_limits<double>::infinity();
|
---|
436 | }
|
---|
437 |
|
---|
438 | double NotShape_impl::getVolume() const
|
---|
439 | {
|
---|
440 | // TODO
|
---|
441 | return -1.; //-arg->getVolume();
|
---|
442 | }
|
---|
443 |
|
---|
444 | double NotShape_impl::getSurfaceArea() const
|
---|
445 | {
|
---|
446 | // TODO
|
---|
447 | return -1.; // -arg->getSurfaceArea();
|
---|
448 | }
|
---|
449 |
|
---|
450 | LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
|
---|
451 | return invert(arg->getLineIntersections(line));
|
---|
452 | }
|
---|
453 |
|
---|
454 | std::string NotShape_impl::toString() const{
|
---|
455 | return std::string("!") + arg->toString();
|
---|
456 | }
|
---|
457 |
|
---|
458 | enum ShapeType NotShape_impl::getType() const{
|
---|
459 | return CombinedType;
|
---|
460 | }
|
---|
461 |
|
---|
462 | std::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 |
|
---|
467 | std::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 |
|
---|
473 | Shape 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 *********************************/
|
---|
479 | std::ostream &operator<<(std::ostream &ost,const Shape &shape){
|
---|
480 | ost << shape.toString();
|
---|
481 | return ost;
|
---|
482 | }
|
---|