[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[bcf653] | 21 | */
|
---|
| 22 |
|
---|
[e09b70] | 23 | /*
|
---|
| 24 | * ShapeOps.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Jun 18, 2010
|
---|
| 27 | * Author: crueger
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[bf3817] | 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
|
---|
[bbbad5] | 36 |
|
---|
[5a8d61] | 37 | #include <algorithm>
|
---|
| 38 | #include <boost/bind.hpp>
|
---|
[08a09ed] | 39 | #include <cmath>
|
---|
[5a8d61] | 40 |
|
---|
[b94634] | 41 | #include "Shapes/ShapeExceptions.hpp"
|
---|
[e09b70] | 42 | #include "Shapes/ShapeOps.hpp"
|
---|
| 43 | #include "Shapes/ShapeOps_impl.hpp"
|
---|
| 44 |
|
---|
[6c438f] | 45 | #include "LinearAlgebra/Vector.hpp"
|
---|
[ad011c] | 46 | #include "CodePatterns/Assert.hpp"
|
---|
[394529] | 47 |
|
---|
[5de9da] | 48 | /*************** Base case ***********************/
|
---|
| 49 |
|
---|
| 50 | ShapeOpsBase_impl::ShapeOpsBase_impl(const Shape::impl_ptr &_arg) :
|
---|
| 51 | arg(_arg){}
|
---|
| 52 |
|
---|
| 53 | ShapeOpsBase_impl::~ShapeOpsBase_impl(){}
|
---|
| 54 |
|
---|
[735940] | 55 | bool ShapeOpsBase_impl::isInside(const Vector &point) const{
|
---|
[5de9da] | 56 | return arg->isInside(translateIn(point));
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[735940] | 59 | bool ShapeOpsBase_impl::isOnSurface(const Vector &point) const{
|
---|
[5de9da] | 60 | return arg->isOnSurface(translateIn(point));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[735940] | 63 | Vector ShapeOpsBase_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
---|
[5de9da] | 64 | Vector helper = translateIn(point);
|
---|
| 65 | if(!arg->isOnSurface(helper)){
|
---|
[b94634] | 66 | throw NotOnSurfaceException() << ShapeVector(&helper);
|
---|
[5de9da] | 67 | }
|
---|
[f12805] | 68 | Vector res = translateOutNormal(arg->getNormal(helper));
|
---|
| 69 | res.Normalize();
|
---|
| 70 | return res;
|
---|
[5de9da] | 71 | }
|
---|
| 72 |
|
---|
[6acc2f3] | 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 |
|
---|
[735940] | 84 | LineSegmentSet ShapeOpsBase_impl::getLineIntersections(const Line &line) const{
|
---|
[c6f395] | 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 |
|
---|
[b92e4a] | 103 | enum ShapeType ShapeOpsBase_impl::getType() const {
|
---|
| 104 | return getArg()->getType();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[6c438f] | 107 | std::vector<Vector> ShapeOpsBase_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
[c48641] | 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;
|
---|
[5a8d61] | 112 | }
|
---|
| 113 |
|
---|
| 114 | std::vector<Vector> ShapeOpsBase_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
---|
[c48641] | 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;
|
---|
[6c438f] | 119 | }
|
---|
| 120 |
|
---|
| 121 | Shape::impl_ptr ShapeOpsBase_impl::getArg() const{
|
---|
[cfda65] | 122 | return arg;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[5e588b5] | 125 | /********************* Resize ********************/
|
---|
| 126 |
|
---|
[e09b70] | 127 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
|
---|
[5de9da] | 128 | ShapeOpsBase_impl(_arg), size(_size)
|
---|
[394529] | 129 | {
|
---|
| 130 | ASSERT(size>0,"Cannot resize a Shape to size zero or below");
|
---|
| 131 | }
|
---|
[e09b70] | 132 |
|
---|
| 133 | Resize_impl::~Resize_impl(){}
|
---|
| 134 |
|
---|
[c67c65] | 135 | double Resize_impl::getVolume() const
|
---|
| 136 | {
|
---|
[b98a32] | 137 | return getArg()->getVolume() * size * size * size;
|
---|
[c67c65] | 138 | }
|
---|
| 139 |
|
---|
| 140 | double Resize_impl::getSurfaceArea() const
|
---|
| 141 | {
|
---|
[b98a32] | 142 | return getArg()->getSurfaceArea() * size * size;
|
---|
[c67c65] | 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
[735940] | 146 | bool Resize_impl::isInside(const Vector& point) const{
|
---|
[b98a32] | 147 | return getArg()->isInside((1./size) * point);
|
---|
[6c438f] | 148 | }
|
---|
| 149 |
|
---|
[735940] | 150 | Vector Resize_impl::translateIn(const Vector& point) const{
|
---|
[b98a32] | 151 | return (1./size) * point;
|
---|
[5de9da] | 152 | }
|
---|
| 153 |
|
---|
[735940] | 154 | Vector Resize_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 155 | return size * point;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[735940] | 158 | Vector Resize_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 159 | return point;
|
---|
[e09b70] | 160 | }
|
---|
| 161 |
|
---|
[b92e4a] | 162 | std::string Resize_impl::toString() const{
|
---|
[955b91] | 163 | std::stringstream sstr;
|
---|
[cfda65] | 164 | sstr << "resize(" << getArg()->toString() << "," << size << ")";
|
---|
| 165 | return sstr.str();
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[e09b70] | 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 |
|
---|
[5e588b5] | 173 | /*************************** translate *******************/
|
---|
| 174 |
|
---|
[e09b70] | 175 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
---|
[5de9da] | 176 | ShapeOpsBase_impl(_arg),offset(_offset)
|
---|
[e09b70] | 177 | {}
|
---|
| 178 |
|
---|
| 179 | Translate_impl::~Translate_impl(){}
|
---|
| 180 |
|
---|
[735940] | 181 | bool Translate_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 182 | return getArg()->isInside(point-offset);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[6acc2f3] | 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 |
|
---|
[c67c65] | 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 | }
|
---|
[6acc2f3] | 204 |
|
---|
[735940] | 205 | Vector Translate_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 206 | return point-offset;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[735940] | 209 | Vector Translate_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 210 | return point+offset;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[735940] | 213 | Vector Translate_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 214 | return point;
|
---|
[e09b70] | 215 | }
|
---|
| 216 |
|
---|
[b92e4a] | 217 | std::string Translate_impl::toString() const{
|
---|
[955b91] | 218 | std::stringstream sstr;
|
---|
[cfda65] | 219 | sstr << "translate(" << getArg()->toString() << "," << offset << ")";
|
---|
[79dd0e] | 220 | return sstr.str();
|
---|
[cfda65] | 221 | }
|
---|
| 222 |
|
---|
[e09b70] | 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 | }
|
---|
[5e588b5] | 227 |
|
---|
| 228 | /*********************** stretch ******************/
|
---|
| 229 |
|
---|
| 230 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
---|
[5de9da] | 231 | ShapeOpsBase_impl(_arg),factors(_factors)
|
---|
[5e588b5] | 232 | {
|
---|
| 233 | for(int i = NDIM;i--;){
|
---|
[84721b] | 234 | ASSERT(factors[i]>0.,"cannot stretch a shape by a negative amount");
|
---|
| 235 | reciFactors[i] = 1./factors[i];
|
---|
[5e588b5] | 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | Stretch_impl::~Stretch_impl(){}
|
---|
| 240 |
|
---|
[c67c65] | 241 | double Stretch_impl::getVolume() const
|
---|
| 242 | {
|
---|
| 243 | // TODO
|
---|
[08a09ed] | 244 | return getArg()->getVolume() * factors[0] * factors[1] * factors[2];
|
---|
[c67c65] | 245 | }
|
---|
| 246 |
|
---|
| 247 | double Stretch_impl::getSurfaceArea() const
|
---|
| 248 | {
|
---|
| 249 | // TODO
|
---|
[08a09ed] | 250 | return getArg()->getSurfaceArea() * pow(factors[0] * factors[1] * factors[2], 2./3.);
|
---|
[c67c65] | 251 | }
|
---|
| 252 |
|
---|
[735940] | 253 | bool Stretch_impl::isInside(const Vector& point) const{
|
---|
[5e588b5] | 254 | Vector helper=point;
|
---|
| 255 | helper.ScaleAll(reciFactors);
|
---|
[6c438f] | 256 | return getArg()->isInside(helper);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[735940] | 259 | Vector Stretch_impl::translateIn(const Vector& point) const{
|
---|
[5e588b5] | 260 | Vector helper=point;
|
---|
| 261 | helper.ScaleAll(reciFactors);
|
---|
[5de9da] | 262 | return helper;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[735940] | 265 | Vector Stretch_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 266 | Vector helper=point;
|
---|
| 267 | helper.ScaleAll(factors);
|
---|
| 268 | return helper;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[735940] | 271 | Vector Stretch_impl::translateOutNormal(const Vector& point) const{
|
---|
[5de9da] | 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;
|
---|
[5e588b5] | 281 | }
|
---|
| 282 |
|
---|
[b92e4a] | 283 | std::string Stretch_impl::toString() const{
|
---|
[955b91] | 284 | std::stringstream sstr;
|
---|
[cfda65] | 285 | sstr << "stretch(" << getArg()->toString() << "," << factors << ")";
|
---|
| 286 | return sstr.str();
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[5e588b5] | 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 |
|
---|
[cca9ef] | 296 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const RealSpaceMatrix &_transformation) :
|
---|
[5de9da] | 297 | ShapeOpsBase_impl(_arg),transformation(_transformation)
|
---|
[5e588b5] | 298 | {
|
---|
| 299 | transformationInv = transformation.invert();
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | Transform_impl::~Transform_impl(){}
|
---|
| 303 |
|
---|
[c67c65] | 304 | double Transform_impl::getVolume() const
|
---|
| 305 | {
|
---|
[08a09ed] | 306 | return getArg()->getVolume() * transformation.determinant();
|
---|
[c67c65] | 307 | }
|
---|
| 308 |
|
---|
| 309 | double Transform_impl::getSurfaceArea() const
|
---|
| 310 | {
|
---|
[08a09ed] | 311 | return getArg()->getSurfaceArea() * pow(transformation.determinant(), 2./3.);
|
---|
[c67c65] | 312 | }
|
---|
| 313 |
|
---|
[735940] | 314 | bool Transform_impl::isInside(const Vector& point) const{
|
---|
[6c438f] | 315 | return getArg()->isInside(transformationInv * point);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[735940] | 318 | Vector Transform_impl::translateIn(const Vector& point) const{
|
---|
[5de9da] | 319 | return transformationInv * point;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[735940] | 322 | Vector Transform_impl::translateOutPos(const Vector& point) const{
|
---|
[5de9da] | 323 | return transformation * point;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[735940] | 326 | Vector Transform_impl::translateOutNormal(const Vector& point) const
|
---|
| 327 | {
|
---|
[cca9ef] | 328 | RealSpaceMatrix mat = transformation.invert().transpose();
|
---|
[5de9da] | 329 | return mat * point;
|
---|
[5e588b5] | 330 | }
|
---|
| 331 |
|
---|
[b92e4a] | 332 | std::string Transform_impl::toString() const{
|
---|
[955b91] | 333 | std::stringstream sstr;
|
---|
[cfda65] | 334 | sstr << "transform(" << getArg()->toString() << "," << transformation << ")";
|
---|
| 335 | return sstr.str();
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[cca9ef] | 338 | Shape transform(const Shape &arg, const RealSpaceMatrix &transformation){
|
---|
[5e588b5] | 339 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
---|
| 340 | return Shape(impl);
|
---|
| 341 | }
|
---|