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