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