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