| 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()), name(src.getName())
 | 
|---|
| 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 | void Shape::setName(const std::string &_name){
 | 
|---|
| 80 |   name = _name;
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | std::string Shape::getName() const{
 | 
|---|
| 84 |   return name;
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 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 {
 | 
|---|
| 102 |                 ApproximateShapeVolume Approximator(*this);
 | 
|---|
| 103 |                 return Approximator();
 | 
|---|
| 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 {
 | 
|---|
| 122 |                 ApproximateShapeArea Approximator(*this);
 | 
|---|
| 123 |                 return Approximator();
 | 
|---|
| 124 |         }
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | LineSegmentSet Shape::getLineIntersections(const Line &line) const{
 | 
|---|
| 128 |   return impl->getLineIntersections(line);
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| 132 |   return impl->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {
 | 
|---|
| 136 |         return impl->getHomogeneousPointsInVolume(N);
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 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 | 
 | 
|---|
| 150 | bool Shape::operator==(const Shape &rhs) const{
 | 
|---|
| 151 |         return (this->getType() == rhs.getType());
 | 
|---|
| 152 | }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | std::string Shape::toString() const{
 | 
|---|
| 155 |   return impl->toString();
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | enum ShapeType Shape::getType() const{
 | 
|---|
| 159 |         return impl->getType();
 | 
|---|
| 160 | }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | Shape::impl_ptr Shape::getImpl() const{
 | 
|---|
| 163 |   return impl;
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | // allows arbitrary friendship, but only if implementation is known
 | 
|---|
| 167 | Shape::impl_ptr getShapeImpl(const Shape &shape){
 | 
|---|
| 168 |   return shape.getImpl();
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 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 | 
 | 
|---|
| 193 | bool AndShape_impl::isInside(const Vector &point) const{
 | 
|---|
| 194 |   return lhs->isInside(point) && rhs->isInside(point);
 | 
|---|
| 195 | }
 | 
|---|
| 196 | 
 | 
|---|
| 197 | bool AndShape_impl::isOnSurface(const Vector &point) const{
 | 
|---|
| 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
 | 
|---|
| 210 |       return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
 | 
|---|
| 211 |              (rhs->isOnSurface(point) && lhs->isInside(point));
 | 
|---|
| 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 | 
 | 
|---|
| 229 | Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
 | 
|---|
| 230 |   Vector res;
 | 
|---|
| 231 |   if(!isOnSurface(point)){
 | 
|---|
| 232 |     throw NotOnSurfaceException() << ShapeVector(&point);
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 272 | LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
 | 
|---|
| 273 |   return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
 | 
|---|
| 274 | }
 | 
|---|
| 275 | 
 | 
|---|
| 276 | std::string AndShape_impl::toString() const{
 | 
|---|
| 277 |   return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
 | 
|---|
| 278 | }
 | 
|---|
| 279 | 
 | 
|---|
| 280 | enum ShapeType AndShape_impl::getType() const{
 | 
|---|
| 281 |         return CombinedType;
 | 
|---|
| 282 | }
 | 
|---|
| 283 | 
 | 
|---|
| 284 | std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| 285 |   std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 286 |   std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 287 |   std::vector<Vector> PointsOnSurface;
 | 
|---|
| 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 | 
 | 
|---|
| 298 |   return PointsOnSurface;
 | 
|---|
| 299 | }
 | 
|---|
| 300 | 
 | 
|---|
| 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 | 
 | 
|---|
| 307 | 
 | 
|---|
| 308 | Shape operator&&(const Shape &lhs,const Shape &rhs){
 | 
|---|
| 309 |   Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
 | 
|---|
| 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 | 
 | 
|---|
| 321 | bool OrShape_impl::isInside(const Vector &point) const{
 | 
|---|
| 322 |   return rhs->isInside(point) || lhs->isInside(point);
 | 
|---|
| 323 | }
 | 
|---|
| 324 | 
 | 
|---|
| 325 | bool OrShape_impl::isOnSurface(const Vector &point) const{
 | 
|---|
| 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
 | 
|---|
| 338 |       return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
 | 
|---|
| 339 |              (rhs->isOnSurface(point) && !lhs->isInside(point));
 | 
|---|
| 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 | 
 | 
|---|
| 357 | Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
 | 
|---|
| 358 |   Vector res;
 | 
|---|
| 359 |   if(!isOnSurface(point)){
 | 
|---|
| 360 |     throw NotOnSurfaceException() << ShapeVector(&point);
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 396 | LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
 | 
|---|
| 397 |   return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
 | 
|---|
| 398 | }
 | 
|---|
| 399 | 
 | 
|---|
| 400 | std::string OrShape_impl::toString() const{
 | 
|---|
| 401 |   return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
 | 
|---|
| 402 | }
 | 
|---|
| 403 | 
 | 
|---|
| 404 | enum ShapeType OrShape_impl::getType() const{
 | 
|---|
| 405 |         return CombinedType;
 | 
|---|
| 406 | }
 | 
|---|
| 407 | 
 | 
|---|
| 408 | std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 431 | Shape operator||(const Shape &lhs,const Shape &rhs){
 | 
|---|
| 432 |   Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
 | 
|---|
| 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 | 
 | 
|---|
| 444 | bool NotShape_impl::isInside(const Vector &point) const{
 | 
|---|
| 445 |   return !arg->isInside(point);
 | 
|---|
| 446 | }
 | 
|---|
| 447 | 
 | 
|---|
| 448 | bool NotShape_impl::isOnSurface(const Vector &point) const{
 | 
|---|
| 449 |   return arg->isOnSurface(point);
 | 
|---|
| 450 | }
 | 
|---|
| 451 | 
 | 
|---|
| 452 | Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
 | 
|---|
| 453 |   return -1.*arg->getNormal(point);
 | 
|---|
| 454 | }
 | 
|---|
| 455 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 478 | LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
 | 
|---|
| 479 |   return invert(arg->getLineIntersections(line));
 | 
|---|
| 480 | }
 | 
|---|
| 481 | 
 | 
|---|
| 482 | std::string NotShape_impl::toString() const{
 | 
|---|
| 483 |   return std::string("!") + arg->toString();
 | 
|---|
| 484 | }
 | 
|---|
| 485 | 
 | 
|---|
| 486 | enum ShapeType NotShape_impl::getType() const{
 | 
|---|
| 487 |         return CombinedType;
 | 
|---|
| 488 | }
 | 
|---|
| 489 | 
 | 
|---|
| 490 | std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| 491 |   // surfaces are the same, only normal direction is different
 | 
|---|
| 492 |   return arg->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 493 | }
 | 
|---|
| 494 | 
 | 
|---|
| 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 | 
 | 
|---|
| 501 | Shape operator!(const Shape &arg){
 | 
|---|
| 502 |   Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
 | 
|---|
| 503 |   return Shape(newImpl);
 | 
|---|
| 504 | }
 | 
|---|
| 505 | 
 | 
|---|
| 506 | /**************** global operations *********************************/
 | 
|---|
| 507 | std::ostream &operator<<(std::ostream &ost,const Shape &shape){
 | 
|---|
| 508 |   ost << shape.toString();
 | 
|---|
| 509 |   return ost;
 | 
|---|
| 510 | }
 | 
|---|