[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 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 |
|
---|
[5a8d61] | 22 | #include <algorithm>
|
---|
| 23 | #include <boost/bind.hpp>
|
---|
| 24 |
|
---|
[b94634] | 25 | #include "Shapes/ShapeExceptions.hpp"
|
---|
[e09b70] | 26 | #include "Shapes/ShapeOps.hpp"
|
---|
| 27 | #include "Shapes/ShapeOps_impl.hpp"
|
---|
| 28 |
|
---|
[6c438f] | 29 | #include "LinearAlgebra/Vector.hpp"
|
---|
[ad011c] | 30 | #include "CodePatterns/Assert.hpp"
|
---|
[394529] | 31 |
|
---|
[5de9da] | 32 | /*************** Base case ***********************/
|
---|
| 33 |
|
---|
| 34 | ShapeOpsBase_impl::ShapeOpsBase_impl(const Shape::impl_ptr &_arg) :
|
---|
| 35 | arg(_arg){}
|
---|
| 36 |
|
---|
| 37 | ShapeOpsBase_impl::~ShapeOpsBase_impl(){}
|
---|
| 38 |
|
---|
[735940] | 39 | bool ShapeOpsBase_impl::isInside(const Vector &point) const{
|
---|
[5de9da] | 40 | return arg->isInside(translateIn(point));
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[735940] | 43 | bool ShapeOpsBase_impl::isOnSurface(const Vector &point) const{
|
---|
[5de9da] | 44 | return arg->isOnSurface(translateIn(point));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[735940] | 47 | Vector ShapeOpsBase_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
---|
[5de9da] | 48 | Vector helper = translateIn(point);
|
---|
| 49 | if(!arg->isOnSurface(helper)){
|
---|
[b94634] | 50 | throw NotOnSurfaceException() << ShapeVector(&helper);
|
---|
[5de9da] | 51 | }
|
---|
[f12805] | 52 | Vector res = translateOutNormal(arg->getNormal(helper));
|
---|
| 53 | res.Normalize();
|
---|
| 54 | return res;
|
---|
[5de9da] | 55 | }
|
---|
| 56 |
|
---|
[6acc2f3] | 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 |
|
---|
[735940] | 68 | LineSegmentSet ShapeOpsBase_impl::getLineIntersections(const Line &line) const{
|
---|
[c6f395] | 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 |
|
---|
[b92e4a] | 87 | enum ShapeType ShapeOpsBase_impl::getType() const {
|
---|
| 88 | return getArg()->getType();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[6c438f] | 91 | std::vector<Vector> ShapeOpsBase_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[5a8d61] | 92 | return getArg()->getHomogeneousPointsOnSurface(N);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | std::vector<Vector> ShapeOpsBase_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 96 | return getArg()->getHomogeneousPointsInVolume(N);
|
---|
[6c438f] | 97 | }
|
---|
| 98 |
|
---|
| 99 | Shape::impl_ptr ShapeOpsBase_impl::getArg() const{
|
---|
[cfda65] | 100 | return arg;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[5e588b5] | 103 | /********************* Resize ********************/
|
---|
| 104 |
|
---|
[e09b70] | 105 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
|
---|
[5de9da] | 106 | ShapeOpsBase_impl(_arg), size(_size)
|
---|
[394529] | 107 | {
|
---|
| 108 | ASSERT(size>0,"Cannot resize a Shape to size zero or below");
|
---|
| 109 | }
|
---|
[e09b70] | 110 |
|
---|
| 111 | Resize_impl::~Resize_impl(){}
|
---|
| 112 |
|
---|
[c67c65] | 113 | double Resize_impl::getVolume() const
|
---|
| 114 | {
|
---|
| 115 | return getArg()->getVolume() * size;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | double Resize_impl::getSurfaceArea() const
|
---|
| 119 | {
|
---|
| 120 | return getArg()->getSurfaceArea() * size;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
[735940] | 124 | bool Resize_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 125 | return getArg()->isInside((1/size) * point);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[735940] | 128 | Vector Resize_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 129 | return (1/size) * point;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[735940] | 132 | Vector Resize_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 133 | return size * point;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[735940] | 136 | Vector Resize_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 137 | return point;
|
---|
[e09b70] | 138 | }
|
---|
| 139 |
|
---|
[b92e4a] | 140 | std::string Resize_impl::toString() const{
|
---|
[955b91] | 141 | std::stringstream sstr;
|
---|
[cfda65] | 142 | sstr << "resize(" << getArg()->toString() << "," << size << ")";
|
---|
| 143 | return sstr.str();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[9c1c89] | 146 | std::vector<Vector> Resize_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 147 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 148 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 149 | boost::bind(&Vector::operator*, _1, size) );
|
---|
[c5186e] | 150 | return PointsOnSurface;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[5a8d61] | 153 | std::vector<Vector> Resize_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 154 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 155 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 156 | boost::bind(&Vector::operator*, _1, size) );
|
---|
| 157 | return std::vector<Vector>();
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[c5186e] | 160 |
|
---|
[e09b70] | 161 | Shape resize(const Shape &arg,double size){
|
---|
| 162 | Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
|
---|
| 163 | return Shape(impl);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[5e588b5] | 166 | /*************************** translate *******************/
|
---|
| 167 |
|
---|
[e09b70] | 168 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
---|
[5de9da] | 169 | ShapeOpsBase_impl(_arg),offset(_offset)
|
---|
[e09b70] | 170 | {}
|
---|
| 171 |
|
---|
| 172 | Translate_impl::~Translate_impl(){}
|
---|
| 173 |
|
---|
[735940] | 174 | bool Translate_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 175 | return getArg()->isInside(point-offset);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[6acc2f3] | 178 | Vector Translate_impl::getCenter() const
|
---|
| 179 | {
|
---|
| 180 | return getArg()->getCenter()+offset;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | double Translate_impl::getRadius() const
|
---|
| 184 | {
|
---|
| 185 | return getArg()->getRadius();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[c67c65] | 188 | double Translate_impl::getVolume() const
|
---|
| 189 | {
|
---|
| 190 | return getArg()->getVolume();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | double Translate_impl::getSurfaceArea() const
|
---|
| 194 | {
|
---|
| 195 | return getArg()->getSurfaceArea();
|
---|
| 196 | }
|
---|
[6acc2f3] | 197 |
|
---|
[735940] | 198 | Vector Translate_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 199 | return point-offset;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[735940] | 202 | Vector Translate_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 203 | return point+offset;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[735940] | 206 | Vector Translate_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 207 | return point;
|
---|
[e09b70] | 208 | }
|
---|
| 209 |
|
---|
[b92e4a] | 210 | std::string Translate_impl::toString() const{
|
---|
[955b91] | 211 | std::stringstream sstr;
|
---|
[cfda65] | 212 | sstr << "translate(" << getArg()->toString() << "," << offset << ")";
|
---|
[79dd0e] | 213 | return sstr.str();
|
---|
[cfda65] | 214 | }
|
---|
| 215 |
|
---|
[9c1c89] | 216 | std::vector<Vector> Translate_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 217 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 218 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 219 | boost::bind(&Vector::operator+, _1, offset) );
|
---|
| 220 | return PointsOnSurface;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | std::vector<Vector> Translate_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 224 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 225 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 226 | boost::bind(&Vector::operator+, _1, offset) );
|
---|
[c5186e] | 227 | return PointsOnSurface;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[e09b70] | 230 | Shape translate(const Shape &arg, const Vector &offset){
|
---|
| 231 | Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
|
---|
| 232 | return Shape(impl);
|
---|
| 233 | }
|
---|
[5e588b5] | 234 |
|
---|
| 235 | /*********************** stretch ******************/
|
---|
| 236 |
|
---|
| 237 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
---|
[5de9da] | 238 | ShapeOpsBase_impl(_arg),factors(_factors)
|
---|
[5e588b5] | 239 | {
|
---|
| 240 | for(int i = NDIM;i--;){
|
---|
[84721b] | 241 | ASSERT(factors[i]>0.,"cannot stretch a shape by a negative amount");
|
---|
| 242 | reciFactors[i] = 1./factors[i];
|
---|
[5e588b5] | 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | Stretch_impl::~Stretch_impl(){}
|
---|
| 247 |
|
---|
[c67c65] | 248 | double Stretch_impl::getVolume() const
|
---|
| 249 | {
|
---|
| 250 | // TODO
|
---|
| 251 | return -1.;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | double Stretch_impl::getSurfaceArea() const
|
---|
| 255 | {
|
---|
| 256 | // TODO
|
---|
| 257 | return -1.;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[735940] | 260 | bool Stretch_impl::isInside(const Vector& point) const{
|
---|
[5e588b5] | 261 | Vector helper=point;
|
---|
| 262 | helper.ScaleAll(reciFactors);
|
---|
[6c438f] | 263 | return getArg()->isInside(helper);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[735940] | 266 | Vector Stretch_impl::translateIn(const Vector& point) const{
|
---|
[5e588b5] | 267 | Vector helper=point;
|
---|
| 268 | helper.ScaleAll(reciFactors);
|
---|
[5de9da] | 269 | return helper;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[735940] | 272 | Vector Stretch_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 273 | Vector helper=point;
|
---|
| 274 | helper.ScaleAll(factors);
|
---|
| 275 | return helper;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[735940] | 278 | Vector Stretch_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 279 | Vector helper=point;
|
---|
| 280 | // the normalFactors are derived from appearances of the factors
|
---|
| 281 | // with in the vectorproduct
|
---|
| 282 | Vector normalFactors;
|
---|
| 283 | normalFactors[0]=factors[1]*factors[2];
|
---|
| 284 | normalFactors[1]=factors[0]*factors[2];
|
---|
| 285 | normalFactors[2]=factors[0]*factors[1];
|
---|
| 286 | helper.ScaleAll(normalFactors);
|
---|
| 287 | return helper;
|
---|
[5e588b5] | 288 | }
|
---|
| 289 |
|
---|
[b92e4a] | 290 | std::string Stretch_impl::toString() const{
|
---|
[955b91] | 291 | std::stringstream sstr;
|
---|
[cfda65] | 292 | sstr << "stretch(" << getArg()->toString() << "," << factors << ")";
|
---|
| 293 | return sstr.str();
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[9c1c89] | 296 | std::vector<Vector> Stretch_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 297 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 298 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 299 | boost::bind( static_cast<void (Vector::*)(const Vector&)>(&Vector::ScaleAll), _1, reciFactors) );
|
---|
| 300 | return PointsOnSurface;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | std::vector<Vector> Stretch_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 304 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 305 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 306 | boost::bind( static_cast<void (Vector::*)(const Vector&)>(&Vector::ScaleAll), _1, reciFactors) );
|
---|
[c5186e] | 307 | return PointsOnSurface;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[5e588b5] | 310 | Shape stretch(const Shape &arg, const Vector &factors){
|
---|
| 311 | Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
|
---|
| 312 | return Shape(impl);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /************************* transform *****************/
|
---|
| 316 |
|
---|
[cca9ef] | 317 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const RealSpaceMatrix &_transformation) :
|
---|
[5de9da] | 318 | ShapeOpsBase_impl(_arg),transformation(_transformation)
|
---|
[5e588b5] | 319 | {
|
---|
| 320 | transformationInv = transformation.invert();
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | Transform_impl::~Transform_impl(){}
|
---|
| 324 |
|
---|
[c67c65] | 325 | double Transform_impl::getVolume() const
|
---|
| 326 | {
|
---|
| 327 | return getArg()->getVolume();
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | double Transform_impl::getSurfaceArea() const
|
---|
| 331 | {
|
---|
| 332 | return getArg()->getSurfaceArea();
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[735940] | 335 | bool Transform_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 336 | return getArg()->isInside(transformationInv * point);
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[735940] | 339 | Vector Transform_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 340 | return transformationInv * point;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[735940] | 343 | Vector Transform_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 344 | return transformation * point;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[735940] | 347 | Vector Transform_impl::translateOutNormal(const Vector& point) const
|
---|
| 348 | {
|
---|
[cca9ef] | 349 | RealSpaceMatrix mat = transformation.invert().transpose();
|
---|
[5de9da] | 350 | return mat * point;
|
---|
[5e588b5] | 351 | }
|
---|
| 352 |
|
---|
[b92e4a] | 353 | std::string Transform_impl::toString() const{
|
---|
[955b91] | 354 | std::stringstream sstr;
|
---|
[cfda65] | 355 | sstr << "transform(" << getArg()->toString() << "," << transformation << ")";
|
---|
| 356 | return sstr.str();
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[9c1c89] | 359 | std::vector<Vector> Transform_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 360 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 361 | std::transform( PointsOnSurface.begin(), PointsOnSurface.end(), PointsOnSurface.begin(),
|
---|
| 362 | boost::bind(static_cast<Vector(*)(const RealSpaceMatrix&,const Vector&)>(operator*), transformation, _1));
|
---|
| 363 | return PointsOnSurface;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | std::vector<Vector> Transform_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 367 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 368 | std::transform( PointsOnSurface.begin(), PointsOnSurface.end(), PointsOnSurface.begin(),
|
---|
| 369 | boost::bind(static_cast<Vector(*)(const RealSpaceMatrix&,const Vector&)>(operator*), transformation, _1));
|
---|
[c5186e] | 370 | return PointsOnSurface;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
[cca9ef] | 373 | Shape transform(const Shape &arg, const RealSpaceMatrix &transformation){
|
---|
[5e588b5] | 374 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
---|
| 375 | return Shape(impl);
|
---|
| 376 | }
|
---|