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