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