| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * NodeGenerator.cpp | 
|---|
| 10 | * | 
|---|
| 11 | * | 
|---|
| 12 | *  Created on: Jan 16, 2012 | 
|---|
| 13 | *      Author: heber | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | // include config.h | 
|---|
| 18 | #ifdef HAVE_CONFIG_H | 
|---|
| 19 | #include <config.h> | 
|---|
| 20 | #endif | 
|---|
| 21 |  | 
|---|
| 22 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 23 |  | 
|---|
| 24 | #include "NodeGenerator.hpp" | 
|---|
| 25 |  | 
|---|
| 26 | #include <cmath> | 
|---|
| 27 | #include <vector> | 
|---|
| 28 |  | 
|---|
| 29 | #include "Shapes/Shape.hpp" | 
|---|
| 30 | #include "Shapes/ShapeOps.hpp" | 
|---|
| 31 |  | 
|---|
| 32 | /** Constructor for NodeGenerator. | 
|---|
| 33 | * | 
|---|
| 34 | * @param _shape Shape which is to be filled with nodes | 
|---|
| 35 | */ | 
|---|
| 36 | NodeGenerator::NodeGenerator(const Shape &_shape) : | 
|---|
| 37 | shape(_shape) | 
|---|
| 38 | {} | 
|---|
| 39 |  | 
|---|
| 40 | /** Destructor for NodeGenerator. | 
|---|
| 41 | * | 
|---|
| 42 | */ | 
|---|
| 43 | NodeGenerator::~NodeGenerator() | 
|---|
| 44 | {} | 
|---|
| 45 |  | 
|---|
| 46 | /** Returns a set of points contained in the \a NodeGenerator::_shape | 
|---|
| 47 | * with a homogeneous density. | 
|---|
| 48 | * | 
|---|
| 49 | * The central idea of the algorithm is to make use of the present function | 
|---|
| 50 | * for obtaining homogeneously distributed points on all surfaces of | 
|---|
| 51 | * presently implemented Shape's. | 
|---|
| 52 | * | 
|---|
| 53 | * The given \a _shape is shrinked such that eventually the volume is filled | 
|---|
| 54 | * with points. Points filled in that actually reside outside the given | 
|---|
| 55 | * Shape are eventually extracted as all are checked for inclusion. | 
|---|
| 56 | * | 
|---|
| 57 | * \note Although a certain density of points given as it has to be converted | 
|---|
| 58 | * to an discrete regime this density can in general be matched only | 
|---|
| 59 | * approximately. | 
|---|
| 60 | * | 
|---|
| 61 | * @param density desired density of points, the unit is the inverse of the | 
|---|
| 62 | * required volume per point | 
|---|
| 63 | * @return set of nodes | 
|---|
| 64 | */ | 
|---|
| 65 | NodeSet  NodeGenerator::operator()(const double density) | 
|---|
| 66 | { | 
|---|
| 67 | // calculate volume and surface of the given Shape | 
|---|
| 68 | const double volume = shape.getVolume(); | 
|---|
| 69 | const double surface = shape.getSurfaceArea(); | 
|---|
| 70 |  | 
|---|
| 71 | // calculate the number of shrinking operations | 
|---|
| 72 | const double radius = shape.getRadius(); | 
|---|
| 73 | const double fraction = surface/volume; | 
|---|
| 74 | const int factor = floor(radius * fraction); | 
|---|
| 75 |  | 
|---|
| 76 | // calculate correction for surface density due to discrete number of layers | 
|---|
| 77 | const double surfaceDensity = volume/(double)factor; | 
|---|
| 78 |  | 
|---|
| 79 | // fill the shrinking vector | 
|---|
| 80 | std::vector<double> shrinking_factors; | 
|---|
| 81 | for(int f=0; f < factor; ++f) | 
|---|
| 82 | shrinking_factors.push_back(radius*((double)f/(double)factor)); | 
|---|
| 83 |  | 
|---|
| 84 | // go through the shrinking operations | 
|---|
| 85 | NodeSet nodes; | 
|---|
| 86 | for (std::vector<double>::const_iterator iter = shrinking_factors.begin(); | 
|---|
| 87 | iter != shrinking_factors.end(); ++iter) { | 
|---|
| 88 | const Shape currentShape = resize(shape, *iter); | 
|---|
| 89 | std::vector<Vector> pointsOnSurface = | 
|---|
| 90 | currentShape.getHomogeneousPointsOnSurface(surfaceDensity); | 
|---|
| 91 | nodes.insert(nodes.end(), pointsOnSurface.begin(), pointsOnSurface.end()); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | // check each point whether its inside the surface | 
|---|
| 95 | return filterOutsidePoints(nodes); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | /** Filters out all points that are not contained inside NodeGenerator::shape. | 
|---|
| 99 | * | 
|---|
| 100 | * @param nodes nodes to check | 
|---|
| 101 | * @return subset of nodes that fulfill Shape::isInisde(). | 
|---|
| 102 | */ | 
|---|
| 103 | NodeSet NodeGenerator::filterOutsidePoints(const NodeSet &nodes) const | 
|---|
| 104 | { | 
|---|
| 105 | NodeSet return_nodes; | 
|---|
| 106 | return_nodes.reserve(nodes.size()); | 
|---|
| 107 | for (NodeSet::const_iterator iter = nodes.begin(); iter != nodes.end(); ++iter) { | 
|---|
| 108 | if (shape.isInside(*iter)) | 
|---|
| 109 | return_nodes.push_back(*iter); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | return return_nodes; | 
|---|
| 113 | } | 
|---|