[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 |
|
---|
[735940] | 113 | bool Resize_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 114 | return getArg()->isInside((1/size) * point);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[735940] | 117 | Vector Resize_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 118 | return (1/size) * point;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[735940] | 121 | Vector Resize_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 122 | return size * point;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[735940] | 125 | Vector Resize_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 126 | return point;
|
---|
[e09b70] | 127 | }
|
---|
| 128 |
|
---|
[b92e4a] | 129 | std::string Resize_impl::toString() const{
|
---|
[955b91] | 130 | std::stringstream sstr;
|
---|
[cfda65] | 131 | sstr << "resize(" << getArg()->toString() << "," << size << ")";
|
---|
| 132 | return sstr.str();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[9c1c89] | 135 | std::vector<Vector> Resize_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 136 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 137 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 138 | boost::bind(&Vector::operator*, _1, size) );
|
---|
[c5186e] | 139 | return PointsOnSurface;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[5a8d61] | 142 | std::vector<Vector> Resize_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 143 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 144 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 145 | boost::bind(&Vector::operator*, _1, size) );
|
---|
| 146 | return std::vector<Vector>();
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[c5186e] | 149 |
|
---|
[e09b70] | 150 | Shape resize(const Shape &arg,double size){
|
---|
| 151 | Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
|
---|
| 152 | return Shape(impl);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[5e588b5] | 155 | /*************************** translate *******************/
|
---|
| 156 |
|
---|
[e09b70] | 157 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
---|
[5de9da] | 158 | ShapeOpsBase_impl(_arg),offset(_offset)
|
---|
[e09b70] | 159 | {}
|
---|
| 160 |
|
---|
| 161 | Translate_impl::~Translate_impl(){}
|
---|
| 162 |
|
---|
[735940] | 163 | bool Translate_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 164 | return getArg()->isInside(point-offset);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[6acc2f3] | 167 | Vector Translate_impl::getCenter() const
|
---|
| 168 | {
|
---|
| 169 | return getArg()->getCenter()+offset;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | double Translate_impl::getRadius() const
|
---|
| 173 | {
|
---|
| 174 | return getArg()->getRadius();
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
[735940] | 178 | Vector Translate_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 179 | return point-offset;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[735940] | 182 | Vector Translate_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 183 | return point+offset;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[735940] | 186 | Vector Translate_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 187 | return point;
|
---|
[e09b70] | 188 | }
|
---|
| 189 |
|
---|
[b92e4a] | 190 | std::string Translate_impl::toString() const{
|
---|
[955b91] | 191 | std::stringstream sstr;
|
---|
[cfda65] | 192 | sstr << "translate(" << getArg()->toString() << "," << offset << ")";
|
---|
[79dd0e] | 193 | return sstr.str();
|
---|
[cfda65] | 194 | }
|
---|
| 195 |
|
---|
[9c1c89] | 196 | std::vector<Vector> Translate_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 197 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 198 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 199 | boost::bind(&Vector::operator+, _1, offset) );
|
---|
| 200 | return PointsOnSurface;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | std::vector<Vector> Translate_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 204 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 205 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 206 | boost::bind(&Vector::operator+, _1, offset) );
|
---|
[c5186e] | 207 | return PointsOnSurface;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[e09b70] | 210 | Shape translate(const Shape &arg, const Vector &offset){
|
---|
| 211 | Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
|
---|
| 212 | return Shape(impl);
|
---|
| 213 | }
|
---|
[5e588b5] | 214 |
|
---|
| 215 | /*********************** stretch ******************/
|
---|
| 216 |
|
---|
| 217 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
---|
[5de9da] | 218 | ShapeOpsBase_impl(_arg),factors(_factors)
|
---|
[5e588b5] | 219 | {
|
---|
| 220 | for(int i = NDIM;i--;){
|
---|
[84721b] | 221 | ASSERT(factors[i]>0.,"cannot stretch a shape by a negative amount");
|
---|
| 222 | reciFactors[i] = 1./factors[i];
|
---|
[5e588b5] | 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | Stretch_impl::~Stretch_impl(){}
|
---|
| 227 |
|
---|
[735940] | 228 | bool Stretch_impl::isInside(const Vector& point) const{
|
---|
[5e588b5] | 229 | Vector helper=point;
|
---|
| 230 | helper.ScaleAll(reciFactors);
|
---|
[6c438f] | 231 | return getArg()->isInside(helper);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[735940] | 234 | Vector Stretch_impl::translateIn(const Vector& point) const{
|
---|
[5e588b5] | 235 | Vector helper=point;
|
---|
| 236 | helper.ScaleAll(reciFactors);
|
---|
[5de9da] | 237 | return helper;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[735940] | 240 | Vector Stretch_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 241 | Vector helper=point;
|
---|
| 242 | helper.ScaleAll(factors);
|
---|
| 243 | return helper;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[735940] | 246 | Vector Stretch_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 247 | Vector helper=point;
|
---|
| 248 | // the normalFactors are derived from appearances of the factors
|
---|
| 249 | // with in the vectorproduct
|
---|
| 250 | Vector normalFactors;
|
---|
| 251 | normalFactors[0]=factors[1]*factors[2];
|
---|
| 252 | normalFactors[1]=factors[0]*factors[2];
|
---|
| 253 | normalFactors[2]=factors[0]*factors[1];
|
---|
| 254 | helper.ScaleAll(normalFactors);
|
---|
| 255 | return helper;
|
---|
[5e588b5] | 256 | }
|
---|
| 257 |
|
---|
[b92e4a] | 258 | std::string Stretch_impl::toString() const{
|
---|
[955b91] | 259 | std::stringstream sstr;
|
---|
[cfda65] | 260 | sstr << "stretch(" << getArg()->toString() << "," << factors << ")";
|
---|
| 261 | return sstr.str();
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[9c1c89] | 264 | std::vector<Vector> Stretch_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 265 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 266 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 267 | boost::bind( static_cast<void (Vector::*)(const Vector&)>(&Vector::ScaleAll), _1, reciFactors) );
|
---|
| 268 | return PointsOnSurface;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | std::vector<Vector> Stretch_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 272 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 273 | std::for_each(PointsOnSurface.begin(), PointsOnSurface.end(),
|
---|
| 274 | boost::bind( static_cast<void (Vector::*)(const Vector&)>(&Vector::ScaleAll), _1, reciFactors) );
|
---|
[c5186e] | 275 | return PointsOnSurface;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[5e588b5] | 278 | Shape stretch(const Shape &arg, const Vector &factors){
|
---|
| 279 | Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
|
---|
| 280 | return Shape(impl);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /************************* transform *****************/
|
---|
| 284 |
|
---|
[cca9ef] | 285 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const RealSpaceMatrix &_transformation) :
|
---|
[5de9da] | 286 | ShapeOpsBase_impl(_arg),transformation(_transformation)
|
---|
[5e588b5] | 287 | {
|
---|
| 288 | transformationInv = transformation.invert();
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | Transform_impl::~Transform_impl(){}
|
---|
| 292 |
|
---|
[735940] | 293 | bool Transform_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 294 | return getArg()->isInside(transformationInv * point);
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[735940] | 297 | Vector Transform_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 298 | return transformationInv * point;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[735940] | 301 | Vector Transform_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 302 | return transformation * point;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[735940] | 305 | Vector Transform_impl::translateOutNormal(const Vector& point) const
|
---|
| 306 | {
|
---|
[cca9ef] | 307 | RealSpaceMatrix mat = transformation.invert().transpose();
|
---|
[5de9da] | 308 | return mat * point;
|
---|
[5e588b5] | 309 | }
|
---|
| 310 |
|
---|
[b92e4a] | 311 | std::string Transform_impl::toString() const{
|
---|
[955b91] | 312 | std::stringstream sstr;
|
---|
[cfda65] | 313 | sstr << "transform(" << getArg()->toString() << "," << transformation << ")";
|
---|
| 314 | return sstr.str();
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[9c1c89] | 317 | std::vector<Vector> Transform_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[6c438f] | 318 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
[5a8d61] | 319 | std::transform( PointsOnSurface.begin(), PointsOnSurface.end(), PointsOnSurface.begin(),
|
---|
| 320 | boost::bind(static_cast<Vector(*)(const RealSpaceMatrix&,const Vector&)>(operator*), transformation, _1));
|
---|
| 321 | return PointsOnSurface;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | std::vector<Vector> Transform_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
| 325 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsInVolume(N);
|
---|
| 326 | std::transform( PointsOnSurface.begin(), PointsOnSurface.end(), PointsOnSurface.begin(),
|
---|
| 327 | boost::bind(static_cast<Vector(*)(const RealSpaceMatrix&,const Vector&)>(operator*), transformation, _1));
|
---|
[c5186e] | 328 | return PointsOnSurface;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[cca9ef] | 331 | Shape transform(const Shape &arg, const RealSpaceMatrix &transformation){
|
---|
[5e588b5] | 332 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
---|
| 333 | return Shape(impl);
|
---|
| 334 | }
|
---|