[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[997784] | 8 | /*
|
---|
| 9 | * Shape.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Jun 18, 2010
|
---|
| 12 | * Author: crueger
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[997784] | 21 |
|
---|
[ad011c] | 22 | #include "CodePatterns/Assert.hpp"
|
---|
[6c438f] | 23 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 24 |
|
---|
[b94634] | 25 | #include "Shapes/Shape.hpp"
|
---|
| 26 | #include "Shapes/Shape_impl.hpp"
|
---|
| 27 | #include "Shapes/ShapeExceptions.hpp"
|
---|
[b92e4a] | 28 | #include "Shapes/ShapeType.hpp"
|
---|
[5de9da] | 29 |
|
---|
[6acc2f3] | 30 | #include <algorithm>
|
---|
| 31 | #include <limits>
|
---|
[cfda65] | 32 | #include <string>
|
---|
| 33 |
|
---|
| 34 |
|
---|
[997784] | 35 | Shape::Shape(const Shape& src) :
|
---|
| 36 | impl(src.getImpl())
|
---|
| 37 | {}
|
---|
| 38 |
|
---|
| 39 | Shape::~Shape(){}
|
---|
| 40 |
|
---|
[205d9b] | 41 | bool Shape::isInside(const Vector &point) const{
|
---|
[997784] | 42 | return impl->isInside(point);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[5de9da] | 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 |
|
---|
[6acc2f3] | 53 | Vector Shape::getCenter() const{
|
---|
| 54 | return impl->getCenter();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | double Shape::getRadius() const{
|
---|
| 58 | return impl->getRadius();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[735940] | 61 | LineSegmentSet Shape::getLineIntersections(const Line &line) const{
|
---|
[c6f395] | 62 | return impl->getLineIntersections(line);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[9c1c89] | 65 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[c5186e] | 66 | return impl->getHomogeneousPointsOnSurface(N);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[5a8d61] | 69 | std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 70 | return impl->getHomogeneousPointsInVolume(N);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[997784] | 73 | Shape::Shape(Shape::impl_ptr _impl) :
|
---|
| 74 | impl(_impl)
|
---|
| 75 | {}
|
---|
| 76 |
|
---|
| 77 | Shape &Shape::operator=(const Shape& rhs){
|
---|
| 78 | if(&rhs!=this){
|
---|
| 79 | impl=rhs.getImpl();
|
---|
| 80 | }
|
---|
| 81 | return *this;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[b92e4a] | 84 | bool Shape::operator==(const Shape &rhs) const{
|
---|
| 85 | return (this->getType() == rhs.getType());
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[cfda65] | 88 | std::string Shape::toString() const{
|
---|
| 89 | return impl->toString();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[b92e4a] | 92 | enum ShapeType Shape::getType() const{
|
---|
| 93 | return impl->getType();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[997784] | 96 | Shape::impl_ptr Shape::getImpl() const{
|
---|
| 97 | return impl;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[e09b70] | 100 | // allows arbitrary friendship, but only if implementation is known
|
---|
| 101 | Shape::impl_ptr getShapeImpl(const Shape &shape){
|
---|
| 102 | return shape.getImpl();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[997784] | 105 | /***************************** Some simple Shapes ***************************/
|
---|
| 106 |
|
---|
| 107 | Shape Everywhere(){
|
---|
| 108 | static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
|
---|
| 109 | return Shape(impl);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | Shape Nowhere(){
|
---|
| 113 | static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
|
---|
| 114 | return Shape(impl);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /****************************** Operators ***********************************/
|
---|
| 118 |
|
---|
| 119 | // AND
|
---|
| 120 |
|
---|
| 121 | AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
---|
| 122 | lhs(_lhs),rhs(_rhs)
|
---|
| 123 | {}
|
---|
| 124 |
|
---|
| 125 | AndShape_impl::~AndShape_impl(){}
|
---|
| 126 |
|
---|
[735940] | 127 | bool AndShape_impl::isInside(const Vector &point) const{
|
---|
[997784] | 128 | return lhs->isInside(point) && rhs->isInside(point);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[735940] | 131 | bool AndShape_impl::isOnSurface(const Vector &point) const{
|
---|
[5de9da] | 132 | // check the number of surfaces that this point is on
|
---|
| 133 | int surfaces =0;
|
---|
| 134 | surfaces += lhs->isOnSurface(point);
|
---|
| 135 | surfaces += rhs->isOnSurface(point);
|
---|
| 136 |
|
---|
| 137 | switch(surfaces){
|
---|
| 138 | case 0:
|
---|
| 139 | return false;
|
---|
| 140 | // no break necessary
|
---|
| 141 | case 1:
|
---|
| 142 | // if it is inside for the object where it does not lie on
|
---|
| 143 | // the surface the whole point lies inside
|
---|
[ef84ca] | 144 | return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
|
---|
| 145 | (rhs->isOnSurface(point) && lhs->isInside(point));
|
---|
[5de9da] | 146 | // no break necessary
|
---|
| 147 | case 2:
|
---|
| 148 | {
|
---|
| 149 | // it lies on both Shapes... could be an edge or an inner point
|
---|
| 150 | // test the direction of the normals
|
---|
| 151 | Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
|
---|
| 152 | // if the directions are opposite we lie on the inside
|
---|
| 153 | return !direction.IsZero();
|
---|
| 154 | }
|
---|
| 155 | // no break necessary
|
---|
| 156 | default:
|
---|
| 157 | // if this happens there is something wrong
|
---|
| 158 | ASSERT(0,"Default case should have never been used");
|
---|
| 159 | }
|
---|
| 160 | return false; // never reached
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[735940] | 163 | Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
---|
[5de9da] | 164 | Vector res;
|
---|
| 165 | if(!isOnSurface(point)){
|
---|
[b94634] | 166 | throw NotOnSurfaceException() << ShapeVector(&point);
|
---|
[5de9da] | 167 | }
|
---|
| 168 | res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
|
---|
| 169 | res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
|
---|
| 170 | res.Normalize();
|
---|
| 171 | return res;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[6acc2f3] | 174 | Vector AndShape_impl::getCenter() const
|
---|
| 175 | {
|
---|
| 176 | // calculate closest position on sphere surface to other center ..
|
---|
| 177 | const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
|
---|
| 178 | const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
|
---|
| 179 | // .. and then it's right in between those two
|
---|
| 180 | return 0.5*(rhsDistance + lhsDistance);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | double AndShape_impl::getRadius() const
|
---|
| 184 | {
|
---|
| 185 | const double distance = (lhs->getCenter() - rhs->getCenter()).Norm();
|
---|
| 186 | const double minradii = std::min(lhs->getRadius(), rhs->getRadius());
|
---|
| 187 | // if no intersection
|
---|
| 188 | if (distance > (lhs->getRadius() + rhs->getRadius()))
|
---|
| 189 | return 0.;
|
---|
| 190 | else // if intersection it can only be the smaller one
|
---|
| 191 | return minradii;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[735940] | 194 | LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
|
---|
[c6f395] | 195 | return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[b92e4a] | 198 | std::string AndShape_impl::toString() const{
|
---|
[955b91] | 199 | return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
|
---|
[cfda65] | 200 | }
|
---|
| 201 |
|
---|
[b92e4a] | 202 | enum ShapeType AndShape_impl::getType() const{
|
---|
| 203 | return CombinedType;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[9c1c89] | 206 | std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[23bade] | 207 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
|
---|
| 208 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
|
---|
[c5186e] | 209 | std::vector<Vector> PointsOnSurface;
|
---|
[23bade] | 210 |
|
---|
| 211 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
|
---|
| 212 | if (rhs->isInside(*iter))
|
---|
| 213 | PointsOnSurface.push_back(*iter);
|
---|
| 214 | }
|
---|
| 215 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
|
---|
| 216 | if (lhs->isInside(*iter))
|
---|
| 217 | PointsOnSurface.push_back(*iter);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[c5186e] | 220 | return PointsOnSurface;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[5a8d61] | 223 | std::vector<Vector> AndShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 224 | ASSERT(0,
|
---|
| 225 | "AndShape_impl::getHomogeneousPointsInVolume() - not implemented.");
|
---|
| 226 | return std::vector<Vector>();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[c5186e] | 229 |
|
---|
[997784] | 230 | Shape operator&&(const Shape &lhs,const Shape &rhs){
|
---|
[e09b70] | 231 | Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
---|
[997784] | 232 | return Shape(newImpl);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | // OR
|
---|
| 236 |
|
---|
| 237 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
---|
| 238 | lhs(_lhs),rhs(_rhs)
|
---|
| 239 | {}
|
---|
| 240 |
|
---|
| 241 | OrShape_impl::~OrShape_impl(){}
|
---|
| 242 |
|
---|
[735940] | 243 | bool OrShape_impl::isInside(const Vector &point) const{
|
---|
[997784] | 244 | return rhs->isInside(point) || lhs->isInside(point);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[735940] | 247 | bool OrShape_impl::isOnSurface(const Vector &point) const{
|
---|
[5de9da] | 248 | // check the number of surfaces that this point is on
|
---|
| 249 | int surfaces =0;
|
---|
| 250 | surfaces += lhs->isOnSurface(point);
|
---|
| 251 | surfaces += rhs->isOnSurface(point);
|
---|
| 252 |
|
---|
| 253 | switch(surfaces){
|
---|
| 254 | case 0:
|
---|
| 255 | return false;
|
---|
| 256 | // no break necessary
|
---|
| 257 | case 1:
|
---|
| 258 | // if it is inside for the object where it does not lie on
|
---|
| 259 | // the surface the whole point lies inside
|
---|
[ef84ca] | 260 | return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
|
---|
| 261 | (rhs->isOnSurface(point) && !lhs->isInside(point));
|
---|
[5de9da] | 262 | // no break necessary
|
---|
| 263 | case 2:
|
---|
| 264 | {
|
---|
| 265 | // it lies on both Shapes... could be an edge or an inner point
|
---|
| 266 | // test the direction of the normals
|
---|
| 267 | Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
|
---|
| 268 | // if the directions are opposite we lie on the inside
|
---|
| 269 | return !direction.IsZero();
|
---|
| 270 | }
|
---|
| 271 | // no break necessary
|
---|
| 272 | default:
|
---|
| 273 | // if this happens there is something wrong
|
---|
| 274 | ASSERT(0,"Default case should have never been used");
|
---|
| 275 | }
|
---|
| 276 | return false; // never reached
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[735940] | 279 | Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
---|
[5de9da] | 280 | Vector res;
|
---|
| 281 | if(!isOnSurface(point)){
|
---|
[b94634] | 282 | throw NotOnSurfaceException() << ShapeVector(&point);
|
---|
[5de9da] | 283 | }
|
---|
| 284 | res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
|
---|
| 285 | res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
|
---|
| 286 | res.Normalize();
|
---|
| 287 | return res;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[6acc2f3] | 290 | Vector OrShape_impl::getCenter() const
|
---|
| 291 | {
|
---|
| 292 | // calculate furthest position on sphere surface to other center ..
|
---|
| 293 | const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
|
---|
| 294 | const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
|
---|
| 295 | // .. and then it's right in between those two
|
---|
| 296 | return .5*(rhsDistance + lhsDistance);
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | double OrShape_impl::getRadius() const
|
---|
| 300 | {
|
---|
| 301 | const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
|
---|
| 302 | const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
|
---|
| 303 | return .5*(rhsDistance - lhsDistance).Norm();
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[735940] | 306 | LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
|
---|
[c6f395] | 307 | return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[b92e4a] | 310 | std::string OrShape_impl::toString() const{
|
---|
[955b91] | 311 | return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
|
---|
[cfda65] | 312 | }
|
---|
| 313 |
|
---|
[b92e4a] | 314 | enum ShapeType OrShape_impl::getType() const{
|
---|
| 315 | return CombinedType;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[9c1c89] | 318 | std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[c5186e] | 319 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
|
---|
| 320 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
|
---|
| 321 | std::vector<Vector> PointsOnSurface;
|
---|
| 322 |
|
---|
| 323 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
|
---|
| 324 | if (!rhs->isInside(*iter))
|
---|
| 325 | PointsOnSurface.push_back(*iter);
|
---|
| 326 | }
|
---|
| 327 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
|
---|
| 328 | if (!lhs->isInside(*iter))
|
---|
| 329 | PointsOnSurface.push_back(*iter);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | return PointsOnSurface;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[5a8d61] | 335 | std::vector<Vector> OrShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 336 | ASSERT(0,
|
---|
| 337 | "OrShape_impl::getHomogeneousPointsInVolume() - not implemented.");
|
---|
| 338 | return std::vector<Vector>();
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[997784] | 341 | Shape operator||(const Shape &lhs,const Shape &rhs){
|
---|
[e09b70] | 342 | Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
---|
[997784] | 343 | return Shape(newImpl);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | // NOT
|
---|
| 347 |
|
---|
| 348 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
|
---|
| 349 | arg(_arg)
|
---|
| 350 | {}
|
---|
| 351 |
|
---|
| 352 | NotShape_impl::~NotShape_impl(){}
|
---|
| 353 |
|
---|
[735940] | 354 | bool NotShape_impl::isInside(const Vector &point) const{
|
---|
[997784] | 355 | return !arg->isInside(point);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[735940] | 358 | bool NotShape_impl::isOnSurface(const Vector &point) const{
|
---|
[5de9da] | 359 | return arg->isOnSurface(point);
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[735940] | 362 | Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
---|
[b94634] | 363 | return -1.*arg->getNormal(point);
|
---|
[5de9da] | 364 | }
|
---|
| 365 |
|
---|
[6acc2f3] | 366 | Vector NotShape_impl::getCenter() const
|
---|
| 367 | {
|
---|
| 368 | return arg->getCenter();
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | double NotShape_impl::getRadius() const
|
---|
| 372 | {
|
---|
| 373 | return std::numeric_limits<double>::infinity();
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[735940] | 376 | LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
|
---|
[c6f395] | 377 | return invert(arg->getLineIntersections(line));
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[b92e4a] | 380 | std::string NotShape_impl::toString() const{
|
---|
[955b91] | 381 | return std::string("!") + arg->toString();
|
---|
[cfda65] | 382 | }
|
---|
| 383 |
|
---|
[b92e4a] | 384 | enum ShapeType NotShape_impl::getType() const{
|
---|
| 385 | return CombinedType;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[9c1c89] | 388 | std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[c5186e] | 389 | // surfaces are the same, only normal direction is different
|
---|
| 390 | return arg->getHomogeneousPointsOnSurface(N);
|
---|
| 391 | }
|
---|
| 392 |
|
---|
[5a8d61] | 393 | std::vector<Vector> NotShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 394 | ASSERT(0,
|
---|
| 395 | "NotShape_impl::getHomogeneousPointsInVolume() - not implemented.");
|
---|
| 396 | return std::vector<Vector>();
|
---|
| 397 | }
|
---|
| 398 |
|
---|
[997784] | 399 | Shape operator!(const Shape &arg){
|
---|
[e09b70] | 400 | Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
|
---|
[997784] | 401 | return Shape(newImpl);
|
---|
| 402 | }
|
---|
[cfda65] | 403 |
|
---|
| 404 | /**************** global operations *********************************/
|
---|
[955b91] | 405 | std::ostream &operator<<(std::ostream &ost,const Shape &shape){
|
---|
[cfda65] | 406 | ost << shape.toString();
|
---|
| 407 | return ost;
|
---|
| 408 | }
|
---|