| [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 | 
 | 
|---|
| [e09b70] | 23 | /*
 | 
|---|
 | 24 |  * ShapeOps.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Jun 18, 2010
 | 
|---|
 | 27 |  *      Author: crueger
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [bbbad5] | 36 | 
 | 
|---|
| [5a8d61] | 37 | #include <algorithm>
 | 
|---|
 | 38 | #include <boost/bind.hpp>
 | 
|---|
 | 39 | 
 | 
|---|
| [b94634] | 40 | #include "Shapes/ShapeExceptions.hpp"
 | 
|---|
| [e09b70] | 41 | #include "Shapes/ShapeOps.hpp"
 | 
|---|
 | 42 | #include "Shapes/ShapeOps_impl.hpp"
 | 
|---|
 | 43 | 
 | 
|---|
| [6c438f] | 44 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [ad011c] | 45 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [394529] | 46 | 
 | 
|---|
| [5de9da] | 47 | /*************** Base case ***********************/
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | ShapeOpsBase_impl::ShapeOpsBase_impl(const Shape::impl_ptr &_arg) :
 | 
|---|
 | 50 |   arg(_arg){}
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | ShapeOpsBase_impl::~ShapeOpsBase_impl(){}
 | 
|---|
 | 53 | 
 | 
|---|
| [735940] | 54 | bool ShapeOpsBase_impl::isInside(const Vector &point) const{
 | 
|---|
| [5de9da] | 55 |   return arg->isInside(translateIn(point));
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
| [735940] | 58 | bool ShapeOpsBase_impl::isOnSurface(const Vector &point) const{
 | 
|---|
| [5de9da] | 59 |   return arg->isOnSurface(translateIn(point));
 | 
|---|
 | 60 | }
 | 
|---|
 | 61 | 
 | 
|---|
| [735940] | 62 | Vector ShapeOpsBase_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
 | 
|---|
| [5de9da] | 63 |   Vector helper = translateIn(point);
 | 
|---|
 | 64 |   if(!arg->isOnSurface(helper)){
 | 
|---|
| [b94634] | 65 |     throw NotOnSurfaceException() << ShapeVector(&helper);
 | 
|---|
| [5de9da] | 66 |   }
 | 
|---|
| [f12805] | 67 |   Vector res = translateOutNormal(arg->getNormal(helper));
 | 
|---|
 | 68 |   res.Normalize();
 | 
|---|
 | 69 |   return res;
 | 
|---|
| [5de9da] | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
| [6acc2f3] | 72 | Vector ShapeOpsBase_impl::getCenter() const
 | 
|---|
 | 73 | {
 | 
|---|
 | 74 |   return arg->getCenter();
 | 
|---|
 | 75 | }
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | double ShapeOpsBase_impl::getRadius() const
 | 
|---|
 | 78 | {
 | 
|---|
 | 79 |   return translateOutPos(Vector(arg->getRadius(), 0., 0.)).Norm();
 | 
|---|
 | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | 
 | 
|---|
| [735940] | 83 | LineSegmentSet ShapeOpsBase_impl::getLineIntersections(const Line &line) const{
 | 
|---|
| [c6f395] | 84 |   Line newLine(translateIn(line.getOrigin()),translateIn(line.getDirection()));
 | 
|---|
 | 85 |   LineSegmentSet res(line);
 | 
|---|
 | 86 |   LineSegmentSet helper = getArg()->getLineIntersections(newLine);
 | 
|---|
 | 87 |   for(LineSegmentSet::iterator iter = helper.begin();iter!=helper.end();++iter){
 | 
|---|
 | 88 |     LinePoint lpBegin = iter->getBegin();
 | 
|---|
 | 89 |     LinePoint lpEnd = iter->getBegin();
 | 
|---|
 | 90 |     // translate both linepoints
 | 
|---|
 | 91 |     lpBegin = lpBegin.isNegInfinity()?
 | 
|---|
 | 92 |                 line.negEndpoint():
 | 
|---|
 | 93 |                 line.getLinePoint(translateOutPos(lpBegin.getPoint()));
 | 
|---|
 | 94 |     lpEnd = lpEnd.isPosInfinity()?
 | 
|---|
 | 95 |               line.posEndpoint():
 | 
|---|
 | 96 |               line.getLinePoint(translateOutPos(lpEnd.getPoint()));
 | 
|---|
 | 97 |     res.insert(LineSegment(lpBegin,lpEnd));
 | 
|---|
 | 98 |   }
 | 
|---|
 | 99 |   return res;
 | 
|---|
 | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
| [b92e4a] | 102 | enum ShapeType ShapeOpsBase_impl::getType() const {
 | 
|---|
 | 103 |         return getArg()->getType();
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
| [6c438f] | 106 | std::vector<Vector> ShapeOpsBase_impl::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| [c48641] | 107 |   std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
 | 
|---|
 | 108 |   std::transform(PointsOnSurface.begin(), PointsOnSurface.end(), PointsOnSurface.begin(),
 | 
|---|
 | 109 |       boost::bind(&ShapeOpsBase_impl::translateOutPos, this, _1) );
 | 
|---|
 | 110 |   return PointsOnSurface;
 | 
|---|
| [5a8d61] | 111 | }
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 | std::vector<Vector> ShapeOpsBase_impl::getHomogeneousPointsInVolume(const size_t N) const {
 | 
|---|
| [c48641] | 114 |   std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
 | 
|---|
 | 115 |   std::transform(PointsOnSurface.begin(), PointsOnSurface.end(), PointsOnSurface.begin(),
 | 
|---|
 | 116 |       boost::bind(&ShapeOpsBase_impl::translateOutPos, this, _1) );
 | 
|---|
 | 117 |   return PointsOnSurface;
 | 
|---|
| [6c438f] | 118 | }
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | Shape::impl_ptr ShapeOpsBase_impl::getArg() const{
 | 
|---|
| [cfda65] | 121 |   return arg;
 | 
|---|
 | 122 | }
 | 
|---|
 | 123 | 
 | 
|---|
| [5e588b5] | 124 | /********************* Resize ********************/
 | 
|---|
 | 125 | 
 | 
|---|
| [e09b70] | 126 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
 | 
|---|
| [5de9da] | 127 |   ShapeOpsBase_impl(_arg), size(_size)
 | 
|---|
| [394529] | 128 | {
 | 
|---|
 | 129 |   ASSERT(size>0,"Cannot resize a Shape to size zero or below");
 | 
|---|
 | 130 | }
 | 
|---|
| [e09b70] | 131 | 
 | 
|---|
 | 132 | Resize_impl::~Resize_impl(){}
 | 
|---|
 | 133 | 
 | 
|---|
| [c67c65] | 134 | double Resize_impl::getVolume() const
 | 
|---|
 | 135 | {
 | 
|---|
| [b98a32] | 136 |         return getArg()->getVolume() * size * size * size;
 | 
|---|
| [c67c65] | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 | double Resize_impl::getSurfaceArea() const
 | 
|---|
 | 140 | {
 | 
|---|
| [b98a32] | 141 |         return getArg()->getSurfaceArea() * size * size;
 | 
|---|
| [c67c65] | 142 | }
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | 
 | 
|---|
| [735940] | 145 | bool Resize_impl::isInside(const Vector& point) const{
 | 
|---|
| [b98a32] | 146 |   return getArg()->isInside((1./size) * point);
 | 
|---|
| [6c438f] | 147 | }
 | 
|---|
 | 148 | 
 | 
|---|
| [735940] | 149 | Vector Resize_impl::translateIn(const Vector& point) const{
 | 
|---|
| [b98a32] | 150 |   return (1./size) * point;
 | 
|---|
| [5de9da] | 151 | }
 | 
|---|
 | 152 | 
 | 
|---|
| [735940] | 153 | Vector Resize_impl::translateOutPos(const Vector& point) const{
 | 
|---|
| [5de9da] | 154 |   return size * point;
 | 
|---|
 | 155 | }
 | 
|---|
 | 156 | 
 | 
|---|
| [735940] | 157 | Vector Resize_impl::translateOutNormal(const Vector& point) const{
 | 
|---|
| [5de9da] | 158 |   return point;
 | 
|---|
| [e09b70] | 159 | }
 | 
|---|
 | 160 | 
 | 
|---|
| [b92e4a] | 161 | std::string Resize_impl::toString() const{
 | 
|---|
| [955b91] | 162 |   std::stringstream sstr;
 | 
|---|
| [cfda65] | 163 |   sstr << "resize(" << getArg()->toString() << "," << size << ")";
 | 
|---|
 | 164 |   return sstr.str();
 | 
|---|
 | 165 | }
 | 
|---|
 | 166 | 
 | 
|---|
| [e09b70] | 167 | Shape resize(const Shape &arg,double size){
 | 
|---|
 | 168 |   Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
 | 
|---|
 | 169 |   return Shape(impl);
 | 
|---|
 | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
| [5e588b5] | 172 | /*************************** translate *******************/
 | 
|---|
 | 173 | 
 | 
|---|
| [e09b70] | 174 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
 | 
|---|
| [5de9da] | 175 |   ShapeOpsBase_impl(_arg),offset(_offset)
 | 
|---|
| [e09b70] | 176 | {}
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 | Translate_impl::~Translate_impl(){}
 | 
|---|
 | 179 | 
 | 
|---|
| [735940] | 180 | bool Translate_impl::isInside(const Vector& point) const{
 | 
|---|
| [6c438f] | 181 |   return getArg()->isInside(point-offset);
 | 
|---|
 | 182 | }
 | 
|---|
 | 183 | 
 | 
|---|
| [6acc2f3] | 184 | Vector Translate_impl::getCenter() const
 | 
|---|
 | 185 | {
 | 
|---|
 | 186 |   return getArg()->getCenter()+offset;
 | 
|---|
 | 187 | }
 | 
|---|
 | 188 | 
 | 
|---|
 | 189 | double Translate_impl::getRadius() const
 | 
|---|
 | 190 | {
 | 
|---|
 | 191 |   return getArg()->getRadius();
 | 
|---|
 | 192 | }
 | 
|---|
 | 193 | 
 | 
|---|
| [c67c65] | 194 | double Translate_impl::getVolume() const
 | 
|---|
 | 195 | {
 | 
|---|
 | 196 |         return getArg()->getVolume();
 | 
|---|
 | 197 | }
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 | double Translate_impl::getSurfaceArea() const
 | 
|---|
 | 200 | {
 | 
|---|
 | 201 |         return getArg()->getSurfaceArea();
 | 
|---|
 | 202 | }
 | 
|---|
| [6acc2f3] | 203 | 
 | 
|---|
| [735940] | 204 | Vector Translate_impl::translateIn(const Vector& point) const{
 | 
|---|
| [5de9da] | 205 |   return point-offset;
 | 
|---|
 | 206 | }
 | 
|---|
 | 207 | 
 | 
|---|
| [735940] | 208 | Vector Translate_impl::translateOutPos(const Vector& point) const{
 | 
|---|
| [5de9da] | 209 |   return point+offset;
 | 
|---|
 | 210 | }
 | 
|---|
 | 211 | 
 | 
|---|
| [735940] | 212 | Vector Translate_impl::translateOutNormal(const Vector& point) const{
 | 
|---|
| [5de9da] | 213 |   return point;
 | 
|---|
| [e09b70] | 214 | }
 | 
|---|
 | 215 | 
 | 
|---|
| [b92e4a] | 216 | std::string Translate_impl::toString() const{
 | 
|---|
| [955b91] | 217 |   std::stringstream sstr;
 | 
|---|
| [cfda65] | 218 |   sstr << "translate(" << getArg()->toString() << "," << offset << ")";
 | 
|---|
| [79dd0e] | 219 |   return sstr.str();
 | 
|---|
| [cfda65] | 220 | }
 | 
|---|
 | 221 | 
 | 
|---|
| [e09b70] | 222 | Shape translate(const Shape &arg, const Vector &offset){
 | 
|---|
 | 223 |   Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
 | 
|---|
 | 224 |   return Shape(impl);
 | 
|---|
 | 225 | }
 | 
|---|
| [5e588b5] | 226 | 
 | 
|---|
 | 227 | /*********************** stretch ******************/
 | 
|---|
 | 228 | 
 | 
|---|
 | 229 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
 | 
|---|
| [5de9da] | 230 |   ShapeOpsBase_impl(_arg),factors(_factors)
 | 
|---|
| [5e588b5] | 231 | {
 | 
|---|
 | 232 |   for(int i = NDIM;i--;){
 | 
|---|
| [84721b] | 233 |     ASSERT(factors[i]>0.,"cannot stretch a shape by a negative amount");
 | 
|---|
 | 234 |     reciFactors[i] = 1./factors[i];
 | 
|---|
| [5e588b5] | 235 |   }
 | 
|---|
 | 236 | }
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 | Stretch_impl::~Stretch_impl(){}
 | 
|---|
 | 239 | 
 | 
|---|
| [c67c65] | 240 | double Stretch_impl::getVolume() const
 | 
|---|
 | 241 | {
 | 
|---|
 | 242 |         // TODO
 | 
|---|
 | 243 |         return -1.;
 | 
|---|
 | 244 | }
 | 
|---|
 | 245 | 
 | 
|---|
 | 246 | double Stretch_impl::getSurfaceArea() const
 | 
|---|
 | 247 | {
 | 
|---|
 | 248 |         // TODO
 | 
|---|
 | 249 |         return -1.;
 | 
|---|
 | 250 | }
 | 
|---|
 | 251 | 
 | 
|---|
| [735940] | 252 | bool Stretch_impl::isInside(const Vector& point) const{
 | 
|---|
| [5e588b5] | 253 |   Vector helper=point;
 | 
|---|
 | 254 |   helper.ScaleAll(reciFactors);
 | 
|---|
| [6c438f] | 255 |   return getArg()->isInside(helper);
 | 
|---|
 | 256 | }
 | 
|---|
 | 257 | 
 | 
|---|
| [735940] | 258 | Vector Stretch_impl::translateIn(const Vector& point) const{
 | 
|---|
| [5e588b5] | 259 |   Vector helper=point;
 | 
|---|
 | 260 |   helper.ScaleAll(reciFactors);
 | 
|---|
| [5de9da] | 261 |   return helper;
 | 
|---|
 | 262 | }
 | 
|---|
 | 263 | 
 | 
|---|
| [735940] | 264 | Vector Stretch_impl::translateOutPos(const Vector& point) const{
 | 
|---|
| [5de9da] | 265 |   Vector helper=point;
 | 
|---|
 | 266 |   helper.ScaleAll(factors);
 | 
|---|
 | 267 |   return helper;
 | 
|---|
 | 268 | }
 | 
|---|
 | 269 | 
 | 
|---|
| [735940] | 270 | Vector Stretch_impl::translateOutNormal(const Vector& point) const{
 | 
|---|
| [5de9da] | 271 |   Vector helper=point;
 | 
|---|
 | 272 |   // the normalFactors are derived from appearances of the factors
 | 
|---|
 | 273 |   // with in the vectorproduct
 | 
|---|
 | 274 |   Vector normalFactors;
 | 
|---|
 | 275 |   normalFactors[0]=factors[1]*factors[2];
 | 
|---|
 | 276 |   normalFactors[1]=factors[0]*factors[2];
 | 
|---|
 | 277 |   normalFactors[2]=factors[0]*factors[1];
 | 
|---|
 | 278 |   helper.ScaleAll(normalFactors);
 | 
|---|
 | 279 |   return helper;
 | 
|---|
| [5e588b5] | 280 | }
 | 
|---|
 | 281 | 
 | 
|---|
| [b92e4a] | 282 | std::string Stretch_impl::toString() const{
 | 
|---|
| [955b91] | 283 |   std::stringstream sstr;
 | 
|---|
| [cfda65] | 284 |   sstr << "stretch(" << getArg()->toString() << "," << factors << ")";
 | 
|---|
 | 285 |   return sstr.str();
 | 
|---|
 | 286 | }
 | 
|---|
 | 287 | 
 | 
|---|
| [5e588b5] | 288 | Shape stretch(const Shape &arg, const Vector &factors){
 | 
|---|
 | 289 |   Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
 | 
|---|
 | 290 |   return Shape(impl);
 | 
|---|
 | 291 | }
 | 
|---|
 | 292 | 
 | 
|---|
 | 293 | /************************* transform *****************/
 | 
|---|
 | 294 | 
 | 
|---|
| [cca9ef] | 295 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const RealSpaceMatrix &_transformation) :
 | 
|---|
| [5de9da] | 296 |   ShapeOpsBase_impl(_arg),transformation(_transformation)
 | 
|---|
| [5e588b5] | 297 | {
 | 
|---|
 | 298 |   transformationInv = transformation.invert();
 | 
|---|
 | 299 | }
 | 
|---|
 | 300 | 
 | 
|---|
 | 301 | Transform_impl::~Transform_impl(){}
 | 
|---|
 | 302 | 
 | 
|---|
| [c67c65] | 303 | double Transform_impl::getVolume() const
 | 
|---|
 | 304 | {
 | 
|---|
 | 305 |         return getArg()->getVolume();
 | 
|---|
 | 306 | }
 | 
|---|
 | 307 | 
 | 
|---|
 | 308 | double Transform_impl::getSurfaceArea() const
 | 
|---|
 | 309 | {
 | 
|---|
 | 310 |         return getArg()->getSurfaceArea();
 | 
|---|
 | 311 | }
 | 
|---|
 | 312 | 
 | 
|---|
| [735940] | 313 | bool Transform_impl::isInside(const Vector& point) const{
 | 
|---|
| [6c438f] | 314 |   return getArg()->isInside(transformationInv * point);
 | 
|---|
 | 315 | }
 | 
|---|
 | 316 | 
 | 
|---|
| [735940] | 317 | Vector Transform_impl::translateIn(const Vector& point) const{
 | 
|---|
| [5de9da] | 318 |   return transformationInv * point;
 | 
|---|
 | 319 | }
 | 
|---|
 | 320 | 
 | 
|---|
| [735940] | 321 | Vector Transform_impl::translateOutPos(const Vector& point) const{
 | 
|---|
| [5de9da] | 322 |   return transformation * point;
 | 
|---|
 | 323 | }
 | 
|---|
 | 324 | 
 | 
|---|
| [735940] | 325 | Vector Transform_impl::translateOutNormal(const Vector& point) const
 | 
|---|
 | 326 | {
 | 
|---|
| [cca9ef] | 327 |   RealSpaceMatrix mat = transformation.invert().transpose();
 | 
|---|
| [5de9da] | 328 |   return mat * point;
 | 
|---|
| [5e588b5] | 329 | }
 | 
|---|
 | 330 | 
 | 
|---|
| [b92e4a] | 331 | std::string Transform_impl::toString() const{
 | 
|---|
| [955b91] | 332 |   std::stringstream sstr;
 | 
|---|
| [cfda65] | 333 |   sstr << "transform(" << getArg()->toString() << "," << transformation << ")";
 | 
|---|
 | 334 |   return sstr.str();
 | 
|---|
 | 335 | }
 | 
|---|
 | 336 | 
 | 
|---|
| [cca9ef] | 337 | Shape transform(const Shape &arg, const RealSpaceMatrix &transformation){
 | 
|---|
| [5e588b5] | 338 |   Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
 | 
|---|
 | 339 |   return Shape(impl);
 | 
|---|
 | 340 | }
 | 
|---|