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