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