| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [bcf653] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
| [997784] | 23 | /* | 
|---|
|  | 24 | * Shape.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Jun 18, 2010 | 
|---|
|  | 27 | *      Author: crueger | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [997784] | 36 |  | 
|---|
| [ad011c] | 37 | #include "CodePatterns/Assert.hpp" | 
|---|
| [6c438f] | 38 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 39 |  | 
|---|
| [b94634] | 40 | #include "Shapes/Shape.hpp" | 
|---|
|  | 41 | #include "Shapes/Shape_impl.hpp" | 
|---|
|  | 42 | #include "Shapes/ShapeExceptions.hpp" | 
|---|
| [b92e4a] | 43 | #include "Shapes/ShapeType.hpp" | 
|---|
| [5de9da] | 44 |  | 
|---|
| [da1e92] | 45 | #include "Tesselation/ApproximateShapeArea.hpp" | 
|---|
|  | 46 | #include "Tesselation/ApproximateShapeVolume.hpp" | 
|---|
|  | 47 |  | 
|---|
| [6acc2f3] | 48 | #include <algorithm> | 
|---|
|  | 49 | #include <limits> | 
|---|
| [cfda65] | 50 | #include <string> | 
|---|
|  | 51 |  | 
|---|
|  | 52 |  | 
|---|
| [997784] | 53 | Shape::Shape(const Shape& src) : | 
|---|
| [d0cd6d] | 54 | impl(src.getImpl()), name(src.getName()) | 
|---|
| [997784] | 55 | {} | 
|---|
|  | 56 |  | 
|---|
|  | 57 | Shape::~Shape(){} | 
|---|
|  | 58 |  | 
|---|
| [205d9b] | 59 | bool Shape::isInside(const Vector &point) const{ | 
|---|
| [997784] | 60 | return impl->isInside(point); | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
| [5de9da] | 63 | bool Shape::isOnSurface(const Vector &point) const{ | 
|---|
|  | 64 | return impl->isOnSurface(point); | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 | Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){ | 
|---|
|  | 68 | return impl->getNormal(point); | 
|---|
|  | 69 | } | 
|---|
|  | 70 |  | 
|---|
| [6acc2f3] | 71 | Vector Shape::getCenter() const{ | 
|---|
|  | 72 | return impl->getCenter(); | 
|---|
|  | 73 | } | 
|---|
|  | 74 |  | 
|---|
|  | 75 | double Shape::getRadius() const{ | 
|---|
|  | 76 | return impl->getRadius(); | 
|---|
|  | 77 | } | 
|---|
|  | 78 |  | 
|---|
| [d0cd6d] | 79 | void Shape::setName(const std::string &_name){ | 
|---|
|  | 80 | name = _name; | 
|---|
|  | 81 | } | 
|---|
|  | 82 |  | 
|---|
|  | 83 | std::string Shape::getName() const{ | 
|---|
|  | 84 | return name; | 
|---|
|  | 85 | } | 
|---|
|  | 86 |  | 
|---|
| [c67c65] | 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 | */ | 
|---|
|  | 96 | double Shape::getVolume() const | 
|---|
|  | 97 | { | 
|---|
|  | 98 | const double volume = impl->getVolume(); | 
|---|
|  | 99 | if (volume  != -1.) { | 
|---|
|  | 100 | return volume; | 
|---|
|  | 101 | } else { | 
|---|
| [da1e92] | 102 | ApproximateShapeVolume Approximator(*this); | 
|---|
|  | 103 | return Approximator(); | 
|---|
| [c67c65] | 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 | */ | 
|---|
|  | 116 | double Shape::getSurfaceArea() const | 
|---|
|  | 117 | { | 
|---|
|  | 118 | const double surfacearea = impl->getSurfaceArea(); | 
|---|
|  | 119 | if (surfacearea != -1.) { | 
|---|
|  | 120 | return surfacearea; | 
|---|
|  | 121 | } else { | 
|---|
| [da1e92] | 122 | ApproximateShapeArea Approximator(*this); | 
|---|
|  | 123 | return Approximator(); | 
|---|
| [c67c65] | 124 | } | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
| [735940] | 127 | LineSegmentSet Shape::getLineIntersections(const Line &line) const{ | 
|---|
| [c6f395] | 128 | return impl->getLineIntersections(line); | 
|---|
|  | 129 | } | 
|---|
|  | 130 |  | 
|---|
| [9c1c89] | 131 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const { | 
|---|
| [c5186e] | 132 | return impl->getHomogeneousPointsOnSurface(N); | 
|---|
|  | 133 | } | 
|---|
|  | 134 |  | 
|---|
| [5a8d61] | 135 | std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const { | 
|---|
|  | 136 | return impl->getHomogeneousPointsInVolume(N); | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
| [997784] | 139 | Shape::Shape(Shape::impl_ptr _impl) : | 
|---|
|  | 140 | impl(_impl) | 
|---|
|  | 141 | {} | 
|---|
|  | 142 |  | 
|---|
|  | 143 | Shape &Shape::operator=(const Shape& rhs){ | 
|---|
|  | 144 | if(&rhs!=this){ | 
|---|
|  | 145 | impl=rhs.getImpl(); | 
|---|
|  | 146 | } | 
|---|
|  | 147 | return *this; | 
|---|
|  | 148 | } | 
|---|
|  | 149 |  | 
|---|
| [b92e4a] | 150 | bool Shape::operator==(const Shape &rhs) const{ | 
|---|
|  | 151 | return (this->getType() == rhs.getType()); | 
|---|
|  | 152 | } | 
|---|
|  | 153 |  | 
|---|
| [cfda65] | 154 | std::string Shape::toString() const{ | 
|---|
|  | 155 | return impl->toString(); | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
| [b92e4a] | 158 | enum ShapeType Shape::getType() const{ | 
|---|
|  | 159 | return impl->getType(); | 
|---|
|  | 160 | } | 
|---|
|  | 161 |  | 
|---|
| [997784] | 162 | Shape::impl_ptr Shape::getImpl() const{ | 
|---|
|  | 163 | return impl; | 
|---|
|  | 164 | } | 
|---|
|  | 165 |  | 
|---|
| [e09b70] | 166 | // allows arbitrary friendship, but only if implementation is known | 
|---|
|  | 167 | Shape::impl_ptr getShapeImpl(const Shape &shape){ | 
|---|
|  | 168 | return shape.getImpl(); | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
| [997784] | 171 | /***************************** Some simple Shapes ***************************/ | 
|---|
|  | 172 |  | 
|---|
|  | 173 | Shape Everywhere(){ | 
|---|
|  | 174 | static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl()); | 
|---|
|  | 175 | return Shape(impl); | 
|---|
|  | 176 | } | 
|---|
|  | 177 |  | 
|---|
|  | 178 | Shape 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 |  | 
|---|
|  | 187 | AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) : | 
|---|
|  | 188 | lhs(_lhs),rhs(_rhs) | 
|---|
|  | 189 | {} | 
|---|
|  | 190 |  | 
|---|
|  | 191 | AndShape_impl::~AndShape_impl(){} | 
|---|
|  | 192 |  | 
|---|
| [735940] | 193 | bool AndShape_impl::isInside(const Vector &point) const{ | 
|---|
| [997784] | 194 | return lhs->isInside(point) && rhs->isInside(point); | 
|---|
|  | 195 | } | 
|---|
|  | 196 |  | 
|---|
| [735940] | 197 | bool AndShape_impl::isOnSurface(const Vector &point) const{ | 
|---|
| [5de9da] | 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 | 
|---|
| [ef84ca] | 210 | return (lhs->isOnSurface(point) && rhs->isInside(point)) || | 
|---|
|  | 211 | (rhs->isOnSurface(point) && lhs->isInside(point)); | 
|---|
| [5de9da] | 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 |  | 
|---|
| [735940] | 229 | Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){ | 
|---|
| [5de9da] | 230 | Vector res; | 
|---|
|  | 231 | if(!isOnSurface(point)){ | 
|---|
| [b94634] | 232 | throw NotOnSurfaceException() << ShapeVector(&point); | 
|---|
| [5de9da] | 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 |  | 
|---|
| [6acc2f3] | 240 | Vector 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 |  | 
|---|
|  | 249 | double 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 |  | 
|---|
| [c67c65] | 260 | double AndShape_impl::getVolume() const | 
|---|
|  | 261 | { | 
|---|
|  | 262 | // TODO | 
|---|
|  | 263 | return -1.; | 
|---|
|  | 264 | } | 
|---|
|  | 265 |  | 
|---|
|  | 266 | double AndShape_impl::getSurfaceArea() const | 
|---|
|  | 267 | { | 
|---|
|  | 268 | // TODO | 
|---|
|  | 269 | return -1.; | 
|---|
|  | 270 | } | 
|---|
|  | 271 |  | 
|---|
| [735940] | 272 | LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{ | 
|---|
| [c6f395] | 273 | return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line)); | 
|---|
|  | 274 | } | 
|---|
|  | 275 |  | 
|---|
| [b92e4a] | 276 | std::string AndShape_impl::toString() const{ | 
|---|
| [955b91] | 277 | return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")"); | 
|---|
| [cfda65] | 278 | } | 
|---|
|  | 279 |  | 
|---|
| [b92e4a] | 280 | enum ShapeType AndShape_impl::getType() const{ | 
|---|
|  | 281 | return CombinedType; | 
|---|
|  | 282 | } | 
|---|
|  | 283 |  | 
|---|
| [9c1c89] | 284 | std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const { | 
|---|
| [23bade] | 285 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N); | 
|---|
|  | 286 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N); | 
|---|
| [c5186e] | 287 | std::vector<Vector> PointsOnSurface; | 
|---|
| [23bade] | 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 |  | 
|---|
| [c5186e] | 298 | return PointsOnSurface; | 
|---|
|  | 299 | } | 
|---|
|  | 300 |  | 
|---|
| [5a8d61] | 301 | std::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 |  | 
|---|
| [c5186e] | 307 |  | 
|---|
| [997784] | 308 | Shape operator&&(const Shape &lhs,const Shape &rhs){ | 
|---|
| [e09b70] | 309 | Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs))); | 
|---|
| [997784] | 310 | return Shape(newImpl); | 
|---|
|  | 311 | } | 
|---|
|  | 312 |  | 
|---|
|  | 313 | // OR | 
|---|
|  | 314 |  | 
|---|
|  | 315 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) : | 
|---|
|  | 316 | lhs(_lhs),rhs(_rhs) | 
|---|
|  | 317 | {} | 
|---|
|  | 318 |  | 
|---|
|  | 319 | OrShape_impl::~OrShape_impl(){} | 
|---|
|  | 320 |  | 
|---|
| [735940] | 321 | bool OrShape_impl::isInside(const Vector &point) const{ | 
|---|
| [997784] | 322 | return rhs->isInside(point) || lhs->isInside(point); | 
|---|
|  | 323 | } | 
|---|
|  | 324 |  | 
|---|
| [735940] | 325 | bool OrShape_impl::isOnSurface(const Vector &point) const{ | 
|---|
| [5de9da] | 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 | 
|---|
| [ef84ca] | 338 | return (lhs->isOnSurface(point) && !rhs->isInside(point)) || | 
|---|
|  | 339 | (rhs->isOnSurface(point) && !lhs->isInside(point)); | 
|---|
| [5de9da] | 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 |  | 
|---|
| [735940] | 357 | Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){ | 
|---|
| [5de9da] | 358 | Vector res; | 
|---|
|  | 359 | if(!isOnSurface(point)){ | 
|---|
| [b94634] | 360 | throw NotOnSurfaceException() << ShapeVector(&point); | 
|---|
| [5de9da] | 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 |  | 
|---|
| [6acc2f3] | 368 | Vector 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 |  | 
|---|
|  | 377 | double 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 |  | 
|---|
| [c67c65] | 384 | double OrShape_impl::getVolume() const | 
|---|
|  | 385 | { | 
|---|
|  | 386 | // TODO | 
|---|
|  | 387 | return -1.; | 
|---|
|  | 388 | } | 
|---|
|  | 389 |  | 
|---|
|  | 390 | double OrShape_impl::getSurfaceArea() const | 
|---|
|  | 391 | { | 
|---|
|  | 392 | // TODO | 
|---|
|  | 393 | return -1.; | 
|---|
|  | 394 | } | 
|---|
|  | 395 |  | 
|---|
| [735940] | 396 | LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{ | 
|---|
| [c6f395] | 397 | return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line)); | 
|---|
|  | 398 | } | 
|---|
|  | 399 |  | 
|---|
| [b92e4a] | 400 | std::string OrShape_impl::toString() const{ | 
|---|
| [955b91] | 401 | return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")"); | 
|---|
| [cfda65] | 402 | } | 
|---|
|  | 403 |  | 
|---|
| [b92e4a] | 404 | enum ShapeType OrShape_impl::getType() const{ | 
|---|
|  | 405 | return CombinedType; | 
|---|
|  | 406 | } | 
|---|
|  | 407 |  | 
|---|
| [9c1c89] | 408 | std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const { | 
|---|
| [c5186e] | 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 |  | 
|---|
| [5a8d61] | 425 | std::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 |  | 
|---|
| [997784] | 431 | Shape operator||(const Shape &lhs,const Shape &rhs){ | 
|---|
| [e09b70] | 432 | Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs))); | 
|---|
| [997784] | 433 | return Shape(newImpl); | 
|---|
|  | 434 | } | 
|---|
|  | 435 |  | 
|---|
|  | 436 | // NOT | 
|---|
|  | 437 |  | 
|---|
|  | 438 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) : | 
|---|
|  | 439 | arg(_arg) | 
|---|
|  | 440 | {} | 
|---|
|  | 441 |  | 
|---|
|  | 442 | NotShape_impl::~NotShape_impl(){} | 
|---|
|  | 443 |  | 
|---|
| [735940] | 444 | bool NotShape_impl::isInside(const Vector &point) const{ | 
|---|
| [997784] | 445 | return !arg->isInside(point); | 
|---|
|  | 446 | } | 
|---|
|  | 447 |  | 
|---|
| [735940] | 448 | bool NotShape_impl::isOnSurface(const Vector &point) const{ | 
|---|
| [5de9da] | 449 | return arg->isOnSurface(point); | 
|---|
|  | 450 | } | 
|---|
|  | 451 |  | 
|---|
| [735940] | 452 | Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){ | 
|---|
| [b94634] | 453 | return -1.*arg->getNormal(point); | 
|---|
| [5de9da] | 454 | } | 
|---|
|  | 455 |  | 
|---|
| [6acc2f3] | 456 | Vector NotShape_impl::getCenter() const | 
|---|
|  | 457 | { | 
|---|
|  | 458 | return arg->getCenter(); | 
|---|
|  | 459 | } | 
|---|
|  | 460 |  | 
|---|
|  | 461 | double NotShape_impl::getRadius() const | 
|---|
|  | 462 | { | 
|---|
|  | 463 | return std::numeric_limits<double>::infinity(); | 
|---|
|  | 464 | } | 
|---|
|  | 465 |  | 
|---|
| [c67c65] | 466 | double NotShape_impl::getVolume() const | 
|---|
|  | 467 | { | 
|---|
|  | 468 | // TODO | 
|---|
|  | 469 | return -1.; //-arg->getVolume(); | 
|---|
|  | 470 | } | 
|---|
|  | 471 |  | 
|---|
|  | 472 | double NotShape_impl::getSurfaceArea() const | 
|---|
|  | 473 | { | 
|---|
|  | 474 | // TODO | 
|---|
|  | 475 | return -1.; // -arg->getSurfaceArea(); | 
|---|
|  | 476 | } | 
|---|
|  | 477 |  | 
|---|
| [735940] | 478 | LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{ | 
|---|
| [c6f395] | 479 | return invert(arg->getLineIntersections(line)); | 
|---|
|  | 480 | } | 
|---|
|  | 481 |  | 
|---|
| [b92e4a] | 482 | std::string NotShape_impl::toString() const{ | 
|---|
| [955b91] | 483 | return std::string("!") + arg->toString(); | 
|---|
| [cfda65] | 484 | } | 
|---|
|  | 485 |  | 
|---|
| [b92e4a] | 486 | enum ShapeType NotShape_impl::getType() const{ | 
|---|
|  | 487 | return CombinedType; | 
|---|
|  | 488 | } | 
|---|
|  | 489 |  | 
|---|
| [9c1c89] | 490 | std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const { | 
|---|
| [c5186e] | 491 | // surfaces are the same, only normal direction is different | 
|---|
|  | 492 | return arg->getHomogeneousPointsOnSurface(N); | 
|---|
|  | 493 | } | 
|---|
|  | 494 |  | 
|---|
| [5a8d61] | 495 | std::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 |  | 
|---|
| [997784] | 501 | Shape operator!(const Shape &arg){ | 
|---|
| [e09b70] | 502 | Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg))); | 
|---|
| [997784] | 503 | return Shape(newImpl); | 
|---|
|  | 504 | } | 
|---|
| [cfda65] | 505 |  | 
|---|
|  | 506 | /**************** global operations *********************************/ | 
|---|
| [955b91] | 507 | std::ostream &operator<<(std::ostream &ost,const Shape &shape){ | 
|---|
| [cfda65] | 508 | ost << shape.toString(); | 
|---|
|  | 509 | return ost; | 
|---|
|  | 510 | } | 
|---|