| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  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 | * ApproximateShapeVolume.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Jan 30, 2012 | 
|---|
| 27 | *      Author: heber | 
|---|
| 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 "ApproximateShapeVolume.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include <boost/bind.hpp> | 
|---|
| 40 | #include <vector> | 
|---|
| 41 |  | 
|---|
| 42 | #include "Box.hpp" | 
|---|
| 43 | #include "Filling/Mesh/CubeMesh.hpp" | 
|---|
| 44 | #include "Filling/NodeTypes.hpp" | 
|---|
| 45 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| 46 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 47 | #include "World.hpp" | 
|---|
| 48 |  | 
|---|
| 49 | /** Constructor of class ApproximateShapeVolume. | 
|---|
| 50 | * | 
|---|
| 51 | * @param _shape shape to calculate the volume approximately | 
|---|
| 52 | */ | 
|---|
| 53 | ApproximateShapeVolume::ApproximateShapeVolume(const Shape &_shape) : | 
|---|
| 54 | shape(_shape) | 
|---|
| 55 | {} | 
|---|
| 56 |  | 
|---|
| 57 | /** Destructor of class ApproximateShapeVolume. | 
|---|
| 58 | * | 
|---|
| 59 | */ | 
|---|
| 60 | ApproximateShapeVolume::~ApproximateShapeVolume() | 
|---|
| 61 | {} | 
|---|
| 62 |  | 
|---|
| 63 | /** Calculate the approximate volume of the given \a shape. | 
|---|
| 64 | * | 
|---|
| 65 | * @return surface volume approximated | 
|---|
| 66 | */ | 
|---|
| 67 | double ApproximateShapeVolume::operator()() const | 
|---|
| 68 | { | 
|---|
| 69 | // TODO | 
|---|
| 70 | // generate mesh of points to "integrate" | 
|---|
| 71 | const double min_distance = 10.; | 
|---|
| 72 | Vector distance(min_distance,min_distance,min_distance); | 
|---|
| 73 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM(); | 
|---|
| 74 | Mesh *mesh = new CubeMesh(distance, Vector(0.5,0.5,0.5), M ); | 
|---|
| 75 | NodeSet nodes = mesh->getNodes(); | 
|---|
| 76 |  | 
|---|
| 77 | // fill with grid points and count whether isInside | 
|---|
| 78 | const size_t total = nodes.size(); | 
|---|
| 79 | size_t inside = 0; | 
|---|
| 80 | for (NodeSet::const_iterator iter = nodes.begin(); | 
|---|
| 81 | iter != nodes.end(); ++iter) { | 
|---|
| 82 | if (shape.isInside(*iter)) | 
|---|
| 83 | ++inside; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | // calculate volume of box | 
|---|
| 87 | const double volume = M.determinant(); | 
|---|
| 88 |  | 
|---|
| 89 | // return fraction that is inside | 
|---|
| 90 | return volume*((double)inside/(double)total); | 
|---|
| 91 | } | 
|---|