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