| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-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 | * BaseShapes_impl.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Jun 18, 2010
|
|---|
| 12 | * Author: crueger
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include "Shapes/BaseShapes.hpp"
|
|---|
| 23 | #include "Shapes/BaseShapes_impl.hpp"
|
|---|
| 24 | #include "Shapes/ShapeExceptions.hpp"
|
|---|
| 25 | #include "Shapes/ShapeOps.hpp"
|
|---|
| 26 |
|
|---|
| 27 | #include "Helpers/defs.hpp"
|
|---|
| 28 |
|
|---|
| 29 | #include "CodePatterns/Assert.hpp"
|
|---|
| 30 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 31 | #include "LinearAlgebra/Line.hpp"
|
|---|
| 32 | #include "LinearAlgebra/Plane.hpp"
|
|---|
| 33 | #include "LinearAlgebra/LineSegment.hpp"
|
|---|
| 34 | #include "LinearAlgebra/LineSegmentSet.hpp"
|
|---|
| 35 |
|
|---|
| 36 | #include <cmath>
|
|---|
| 37 | #include <algorithm>
|
|---|
| 38 |
|
|---|
| 39 | bool Sphere_impl::isInside(const Vector &point) const{
|
|---|
| 40 | return point.NormSquared()<=1.;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | bool Sphere_impl::isOnSurface(const Vector &point) const{
|
|---|
| 44 | return fabs(point.NormSquared()-1.)<MYEPSILON;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | Vector Sphere_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
|---|
| 48 | if(!isOnSurface(point)){
|
|---|
| 49 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 50 | }
|
|---|
| 51 | return point;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | Vector Sphere_impl::getCenter() const
|
|---|
| 55 | {
|
|---|
| 56 | return Vector(0.,0.,0.);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | double Sphere_impl::getRadius() const
|
|---|
| 60 | {
|
|---|
| 61 | return 1.;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | double Sphere_impl::getVolume() const
|
|---|
| 65 | {
|
|---|
| 66 | return (4./3.)*M_PI; // 4/3 pi r^3
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | double Sphere_impl::getSurfaceArea() const
|
|---|
| 70 | {
|
|---|
| 71 | return 2.*M_PI; // 2 pi r^2
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | LineSegmentSet Sphere_impl::getLineIntersections(const Line &line) const{
|
|---|
| 76 | LineSegmentSet res(line);
|
|---|
| 77 | std::vector<Vector> intersections = line.getSphereIntersections();
|
|---|
| 78 | if(intersections.size()==2){
|
|---|
| 79 | res.insert(LineSegment(intersections[0],intersections[1]));
|
|---|
| 80 | }
|
|---|
| 81 | return res;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | std::string Sphere_impl::toString() const{
|
|---|
| 85 | return "Sphere()";
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | enum ShapeType Sphere_impl::getType() const
|
|---|
| 89 | {
|
|---|
| 90 | return SphereType;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * algorithm taken from http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere
|
|---|
| 95 | * \param N number of points on surface
|
|---|
| 96 | */
|
|---|
| 97 | std::vector<Vector> Sphere_impl::getHomogeneousPointsOnSurface(const size_t N) const
|
|---|
| 98 | {
|
|---|
| 99 | std::vector<Vector> PointsOnSurface;
|
|---|
| 100 |
|
|---|
| 101 | double PI=3.14159265358979323846;
|
|---|
| 102 | double a=4*PI/N;
|
|---|
| 103 | double d= sqrt(a);
|
|---|
| 104 | int Mtheta=int(PI/d);
|
|---|
| 105 | double dtheta=PI/Mtheta;
|
|---|
| 106 | double dphi=a/dtheta;
|
|---|
| 107 | for (int m=0; m<Mtheta; m++)
|
|---|
| 108 | {
|
|---|
| 109 | double theta=PI*(m+0.5)/Mtheta;
|
|---|
| 110 | int Mphi=int(2*PI*sin(theta)/dphi);
|
|---|
| 111 | for (int n=0; n<Mphi;n++)
|
|---|
| 112 | {
|
|---|
| 113 | double phi= 2*PI*n/Mphi;
|
|---|
| 114 | Vector point;
|
|---|
| 115 | point.Zero();
|
|---|
| 116 | point[0]=sin(theta)*cos(phi);
|
|---|
| 117 | point[1]=sin(theta)*sin(phi);
|
|---|
| 118 | point[2]=cos(theta);
|
|---|
| 119 | PointsOnSurface.push_back(point);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | /*const double dlength = M_PI*(3.-sqrt(5.));
|
|---|
| 123 | double length = 0;
|
|---|
| 124 | const double dz = 2.0/N;
|
|---|
| 125 | double z = 1. - dz/2.;
|
|---|
| 126 | Vector point;
|
|---|
| 127 | for (size_t ka = 0; ka<N; ka++){
|
|---|
| 128 | const double r = sqrt(1.-z*z);
|
|---|
| 129 | point.Zero();
|
|---|
| 130 | point[0] = cos(length)*r;
|
|---|
| 131 | point[1] = sin(length)*r;
|
|---|
| 132 | point[2] = z;
|
|---|
| 133 | PointsOnSurface.push_back(point);
|
|---|
| 134 | z = z - dz;
|
|---|
| 135 | length = length + dlength;*/
|
|---|
| 136 |
|
|---|
| 137 | // ASSERT(PointsOnSurface.size() == N,
|
|---|
| 138 | // "Sphere_impl::getHomogeneousPointsOnSurface() did not create "
|
|---|
| 139 | // +::toString(N)+" but "+::toString(PointsOnSurface.size())+" points.");
|
|---|
| 140 | return PointsOnSurface;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | std::vector<Vector> Sphere_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
|---|
| 144 | ASSERT(0,
|
|---|
| 145 | "Sphere_impl::getHomogeneousPointsInVolume() - not implemented.");
|
|---|
| 146 | return std::vector<Vector>();
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | Shape Sphere(){
|
|---|
| 150 | Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
|
|---|
| 151 | return Shape(impl);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | Shape Sphere(const Vector ¢er,double radius){
|
|---|
| 155 | return translate(resize(Sphere(),radius),center);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | Shape Ellipsoid(const Vector ¢er, const Vector &radius){
|
|---|
| 159 | return translate(stretch(Sphere(),radius),center);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | bool Cuboid_impl::isInside(const Vector &point) const{
|
|---|
| 163 | return (point[0]>=0 && point[0]<=1) && (point[1]>=0 && point[1]<=1) && (point[2]>=0 && point[2]<=1);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | bool Cuboid_impl::isOnSurface(const Vector &point) const{
|
|---|
| 167 | bool retVal = isInside(point);
|
|---|
| 168 | // test all borders of the cuboid
|
|---|
| 169 | // double fabs
|
|---|
| 170 | retVal = retVal &&
|
|---|
| 171 | (((fabs(point[0]-1.) < MYEPSILON) || (fabs(point[0]) < MYEPSILON)) ||
|
|---|
| 172 | ((fabs(point[1]-1.) < MYEPSILON) || (fabs(point[1]) < MYEPSILON)) ||
|
|---|
| 173 | ((fabs(point[2]-1.) < MYEPSILON) || (fabs(point[2]) < MYEPSILON)));
|
|---|
| 174 | return retVal;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | Vector Cuboid_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
|---|
| 178 | if(!isOnSurface(point)){
|
|---|
| 179 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 180 | }
|
|---|
| 181 | Vector res;
|
|---|
| 182 | // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
|
|---|
| 183 | for(int i=NDIM;i--;){
|
|---|
| 184 | if(fabs(fabs(point[i])-1)<MYEPSILON){
|
|---|
| 185 | // add the scaled (-1/+1) Vector to the set of surface vectors
|
|---|
| 186 | res[i] = point[i];
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
|
|---|
| 190 |
|
|---|
| 191 | res.Normalize();
|
|---|
| 192 | return res;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 | Vector Cuboid_impl::getCenter() const
|
|---|
| 197 | {
|
|---|
| 198 | return Vector(0.5,0.5,0.5);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | double Cuboid_impl::getRadius() const
|
|---|
| 202 | {
|
|---|
| 203 | return .5;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | double Cuboid_impl::getVolume() const
|
|---|
| 207 | {
|
|---|
| 208 | return 1.; // l^3
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | double Cuboid_impl::getSurfaceArea() const
|
|---|
| 212 | {
|
|---|
| 213 | return 6.; // 6 * l^2
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line) const{
|
|---|
| 217 | LineSegmentSet res(line);
|
|---|
| 218 | // get the intersection on each of the six faces
|
|---|
| 219 | std::vector<Vector> intersections;
|
|---|
| 220 | intersections.resize(2);
|
|---|
| 221 | int c=0;
|
|---|
| 222 | int x[2]={-1,+1};
|
|---|
| 223 | for(int i=NDIM;i--;){
|
|---|
| 224 | for(int p=0;p<2;++p){
|
|---|
| 225 | if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
|
|---|
| 226 | Vector base;
|
|---|
| 227 | base[i]=x[p];
|
|---|
| 228 | // base now points to the surface and is normal to it at the same time
|
|---|
| 229 | Plane p(base,base);
|
|---|
| 230 | Vector intersection = p.GetIntersection(line);
|
|---|
| 231 | if(isInside(intersection)){
|
|---|
| 232 | // if we have a point on the edge it might already be contained
|
|---|
| 233 | if(c==1 && intersections[0]==intersection)
|
|---|
| 234 | continue;
|
|---|
| 235 | intersections[c++]=intersection;
|
|---|
| 236 | }
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 | end:
|
|---|
| 240 | if(c==2){
|
|---|
| 241 | res.insert(LineSegment(intersections[0],intersections[1]));
|
|---|
| 242 | }
|
|---|
| 243 | return res;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | std::string Cuboid_impl::toString() const{
|
|---|
| 247 | return "Cuboid()";
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | enum ShapeType Cuboid_impl::getType() const
|
|---|
| 251 | {
|
|---|
| 252 | return CuboidType;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /**
|
|---|
| 256 | * \param N number of points on surface
|
|---|
| 257 | */
|
|---|
| 258 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 259 | std::vector<Vector> PointsOnSurface;
|
|---|
| 260 | ASSERT(false, "Cuboid_impl::getHomogeneousPointsOnSurface() not implemented yet");
|
|---|
| 261 | return PointsOnSurface;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
|---|
| 265 | ASSERT(0,
|
|---|
| 266 | "Cuboid_impl::getHomogeneousPointsInVolume() - not implemented.");
|
|---|
| 267 | return std::vector<Vector>();
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | Shape Cuboid(){
|
|---|
| 271 | Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
|
|---|
| 272 | return Shape(impl);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | Shape Cuboid(const Vector &corner1, const Vector &corner2){
|
|---|
| 276 | // make sure the two edges are upper left front and lower right back
|
|---|
| 277 | Vector sortedC1;
|
|---|
| 278 | Vector sortedC2;
|
|---|
| 279 | for(int i=NDIM;i--;){
|
|---|
| 280 | sortedC1[i] = std::min(corner1[i],corner2[i]);
|
|---|
| 281 | sortedC2[i] = std::max(corner1[i],corner2[i]);
|
|---|
| 282 | ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
|
|---|
| 283 | }
|
|---|
| 284 | // get the middle point
|
|---|
| 285 | Vector middle = (1./2.)*(sortedC1+sortedC2);
|
|---|
| 286 | Vector factors = sortedC2-middle;
|
|---|
| 287 | return translate(stretch(Cuboid(),factors),middle);
|
|---|
| 288 | }
|
|---|