| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [d103d3] | 4 |  * Copyright (C)  2010-2011 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [e38447] | 8 | /*
 | 
|---|
 | 9 |  * BaseShapes_impl.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 | 
 | 
|---|
| [e38447] | 22 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
 | 23 | #include "Shapes/BaseShapes_impl.hpp"
 | 
|---|
| [b94634] | 24 | #include "Shapes/ShapeExceptions.hpp"
 | 
|---|
| [f3526d] | 25 | #include "Shapes/ShapeOps.hpp"
 | 
|---|
| [e38447] | 26 | 
 | 
|---|
| [e4fe8d] | 27 | #include "Helpers/defs.hpp"
 | 
|---|
| [5de9da] | 28 | 
 | 
|---|
| [ad011c] | 29 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [57f243] | 30 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [6c438f] | 31 | #include "LinearAlgebra/Line.hpp"
 | 
|---|
 | 32 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
 | 33 | #include "LinearAlgebra/LineSegment.hpp"
 | 
|---|
 | 34 | #include "LinearAlgebra/LineSegmentSet.hpp"
 | 
|---|
| [c6f395] | 35 | 
 | 
|---|
| [5de9da] | 36 | #include <cmath>
 | 
|---|
| [d76a7c] | 37 | #include <algorithm>
 | 
|---|
| [e38447] | 38 | 
 | 
|---|
 | 39 | bool Sphere_impl::isInside(const Vector &point){
 | 
|---|
 | 40 |   return point.NormSquared()<=1;
 | 
|---|
 | 41 | }
 | 
|---|
 | 42 | 
 | 
|---|
| [5de9da] | 43 | bool Sphere_impl::isOnSurface(const Vector &point){
 | 
|---|
 | 44 |   return fabs(point.NormSquared()-1)<MYEPSILON;
 | 
|---|
 | 45 | }
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | Vector Sphere_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
 | 
|---|
 | 48 |   if(!isOnSurface(point)){
 | 
|---|
| [b94634] | 49 |     throw NotOnSurfaceException() << ShapeVector(&point);
 | 
|---|
| [5de9da] | 50 |   }
 | 
|---|
 | 51 |   return point;
 | 
|---|
 | 52 | }
 | 
|---|
 | 53 | 
 | 
|---|
| [c6f395] | 54 | LineSegmentSet Sphere_impl::getLineIntersections(const Line &line){
 | 
|---|
 | 55 |   LineSegmentSet res(line);
 | 
|---|
 | 56 |   std::vector<Vector> intersections = line.getSphereIntersections();
 | 
|---|
 | 57 |   if(intersections.size()==2){
 | 
|---|
 | 58 |     res.insert(LineSegment(intersections[0],intersections[1]));
 | 
|---|
 | 59 |   }
 | 
|---|
 | 60 |   return res;
 | 
|---|
 | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
| [cfda65] | 63 | string Sphere_impl::toString(){
 | 
|---|
 | 64 |   return "Sphere()";
 | 
|---|
 | 65 | }
 | 
|---|
 | 66 | 
 | 
|---|
| [c5186e] | 67 | /**
 | 
|---|
 | 68 |  * algorithm taken from http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere
 | 
|---|
 | 69 |  * \param N number of points on surface
 | 
|---|
 | 70 |  */
 | 
|---|
| [f6ba43] | 71 | std::vector<Vector> Sphere_impl::getHomogeneousPointsOnSurface(const size_t N) const
 | 
|---|
 | 72 | {
 | 
|---|
| [c5186e] | 73 |   std::vector<Vector> PointsOnSurface;
 | 
|---|
 | 74 | 
 | 
|---|
| [faca99] | 75 |     double PI=3.14159265358979323846;
 | 
|---|
 | 76 |     double a=4*PI/N;
 | 
|---|
 | 77 |     double d= sqrt(a);
 | 
|---|
 | 78 |     int Mtheta=int(PI/d);
 | 
|---|
 | 79 |     double dtheta=PI/Mtheta;
 | 
|---|
 | 80 |     double dphi=a/dtheta;
 | 
|---|
 | 81 |     for (int m=0; m<Mtheta; m++)
 | 
|---|
 | 82 |     {
 | 
|---|
 | 83 |           double theta=PI*(m+0.5)/Mtheta;
 | 
|---|
 | 84 |           int Mphi=int(2*PI*sin(theta)/dphi);
 | 
|---|
 | 85 |           for (int n=0; n<Mphi;n++)
 | 
|---|
 | 86 |           {
 | 
|---|
 | 87 |                   double phi= 2*PI*n/Mphi;
 | 
|---|
 | 88 |                   Vector point;
 | 
|---|
 | 89 |                   point.Zero();
 | 
|---|
 | 90 |                   point[0]=sin(theta)*cos(phi);
 | 
|---|
 | 91 |                   point[1]=sin(theta)*sin(phi);
 | 
|---|
 | 92 |                   point[2]=cos(theta);
 | 
|---|
 | 93 |                   PointsOnSurface.push_back(point);
 | 
|---|
 | 94 |           }
 | 
|---|
 | 95 |     }
 | 
|---|
 | 96 |   /*const double dlength = M_PI*(3.-sqrt(5.));
 | 
|---|
| [c5186e] | 97 |   double length = 0;
 | 
|---|
 | 98 |   const double dz = 2.0/N;
 | 
|---|
 | 99 |   double z = 1. - dz/2.;
 | 
|---|
 | 100 |   Vector point;
 | 
|---|
| [9c1c89] | 101 |   for (size_t ka = 0; ka<N; ka++){
 | 
|---|
| [c5186e] | 102 |     const double r = sqrt(1.-z*z);
 | 
|---|
 | 103 |     point.Zero();
 | 
|---|
 | 104 |     point[0] = cos(length)*r;
 | 
|---|
 | 105 |     point[1] = sin(length)*r;
 | 
|---|
 | 106 |     point[2] = z;
 | 
|---|
 | 107 |     PointsOnSurface.push_back(point);
 | 
|---|
 | 108 |     z = z - dz;
 | 
|---|
| [faca99] | 109 |     length = length + dlength;*/
 | 
|---|
| [c5186e] | 110 | 
 | 
|---|
| [f6ba43] | 111 | //  ASSERT(PointsOnSurface.size() == N,
 | 
|---|
 | 112 | //      "Sphere_impl::getHomogeneousPointsOnSurface() did not create "
 | 
|---|
 | 113 | //      +::toString(N)+" but "+::toString(PointsOnSurface.size())+" points.");
 | 
|---|
| [c5186e] | 114 |   return PointsOnSurface;
 | 
|---|
 | 115 | }
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | 
 | 
|---|
| [e38447] | 118 | Shape Sphere(){
 | 
|---|
 | 119 |   Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
 | 
|---|
 | 120 |   return Shape(impl);
 | 
|---|
 | 121 | }
 | 
|---|
 | 122 | 
 | 
|---|
| [f3526d] | 123 | Shape Sphere(const Vector ¢er,double radius){
 | 
|---|
 | 124 |   return translate(resize(Sphere(),radius),center);
 | 
|---|
 | 125 | }
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | Shape Ellipsoid(const Vector ¢er, const Vector &radius){
 | 
|---|
 | 128 |   return translate(stretch(Sphere(),radius),center);
 | 
|---|
 | 129 | }
 | 
|---|
 | 130 | 
 | 
|---|
| [e38447] | 131 | bool Cuboid_impl::isInside(const Vector &point){
 | 
|---|
| [0d02fb] | 132 |   return (point[0]>=0 && point[0]<=1) && (point[1]>=0 && point[1]<=1) && (point[2]>=0 && point[2]<=1);
 | 
|---|
| [5de9da] | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | bool Cuboid_impl::isOnSurface(const Vector &point){
 | 
|---|
 | 136 |   bool retVal = isInside(point);
 | 
|---|
 | 137 |   // test all borders of the cuboid
 | 
|---|
 | 138 |   // double fabs
 | 
|---|
 | 139 |   retVal = retVal &&
 | 
|---|
| [6c438f] | 140 |            (((fabs(point[0]-1.)  < MYEPSILON) || (fabs(point[0])  < MYEPSILON)) ||
 | 
|---|
 | 141 |             ((fabs(point[1]-1.)  < MYEPSILON) || (fabs(point[1])  < MYEPSILON)) ||
 | 
|---|
 | 142 |             ((fabs(point[2]-1.)  < MYEPSILON) || (fabs(point[2])  < MYEPSILON)));
 | 
|---|
| [5de9da] | 143 |   return retVal;
 | 
|---|
 | 144 | }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | Vector Cuboid_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
 | 
|---|
 | 147 |   if(!isOnSurface(point)){
 | 
|---|
| [b94634] | 148 |     throw NotOnSurfaceException() << ShapeVector(&point);
 | 
|---|
| [5de9da] | 149 |   }
 | 
|---|
 | 150 |   Vector res;
 | 
|---|
 | 151 |   // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
 | 
|---|
 | 152 |   for(int i=NDIM;i--;){
 | 
|---|
 | 153 |     if(fabs(fabs(point[i])-1)<MYEPSILON){
 | 
|---|
 | 154 |       // add the scaled (-1/+1) Vector to the set of surface vectors
 | 
|---|
 | 155 |       res[i] = point[i];
 | 
|---|
 | 156 |     }
 | 
|---|
 | 157 |   }
 | 
|---|
 | 158 |   ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 |   res.Normalize();
 | 
|---|
 | 161 |   return res;
 | 
|---|
| [e38447] | 162 | }
 | 
|---|
 | 163 | 
 | 
|---|
| [c6f395] | 164 | LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line){
 | 
|---|
 | 165 |   LineSegmentSet res(line);
 | 
|---|
 | 166 |   // get the intersection on each of the six faces
 | 
|---|
 | 167 |   vector<Vector> intersections;
 | 
|---|
 | 168 |   intersections.resize(2);
 | 
|---|
 | 169 |   int c=0;
 | 
|---|
 | 170 |   int x[2]={-1,+1};
 | 
|---|
 | 171 |   for(int i=NDIM;i--;){
 | 
|---|
 | 172 |     for(int p=0;p<2;++p){
 | 
|---|
 | 173 |       if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
 | 
|---|
 | 174 |       Vector base;
 | 
|---|
 | 175 |       base[i]=x[p];
 | 
|---|
 | 176 |       // base now points to the surface and is normal to it at the same time
 | 
|---|
 | 177 |       Plane p(base,base);
 | 
|---|
 | 178 |       Vector intersection = p.GetIntersection(line);
 | 
|---|
 | 179 |       if(isInside(intersection)){
 | 
|---|
 | 180 |         // if we have a point on the edge it might already be contained
 | 
|---|
 | 181 |         if(c==1 && intersections[0]==intersection)
 | 
|---|
 | 182 |           continue;
 | 
|---|
 | 183 |         intersections[c++]=intersection;
 | 
|---|
 | 184 |       }
 | 
|---|
 | 185 |     }
 | 
|---|
 | 186 |   }
 | 
|---|
 | 187 |   end:
 | 
|---|
 | 188 |   if(c==2){
 | 
|---|
 | 189 |     res.insert(LineSegment(intersections[0],intersections[1]));
 | 
|---|
 | 190 |   }
 | 
|---|
 | 191 |   return res;
 | 
|---|
 | 192 | }
 | 
|---|
 | 193 | 
 | 
|---|
| [cfda65] | 194 | string Cuboid_impl::toString(){
 | 
|---|
 | 195 |   return "Cuboid()";
 | 
|---|
 | 196 | }
 | 
|---|
 | 197 | 
 | 
|---|
| [c5186e] | 198 | /**
 | 
|---|
 | 199 |  * \param N number of points on surface
 | 
|---|
 | 200 |  */
 | 
|---|
| [9c1c89] | 201 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| [c5186e] | 202 |   std::vector<Vector> PointsOnSurface;
 | 
|---|
 | 203 |   ASSERT(false, "Cuboid_impl::getHomogeneousPointsOnSurface() not implemented yet");
 | 
|---|
 | 204 |   return PointsOnSurface;
 | 
|---|
 | 205 | }
 | 
|---|
 | 206 | 
 | 
|---|
| [e38447] | 207 | Shape Cuboid(){
 | 
|---|
| [5de9da] | 208 |   Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
 | 
|---|
| [e38447] | 209 |   return Shape(impl);
 | 
|---|
 | 210 | }
 | 
|---|
| [d76a7c] | 211 | 
 | 
|---|
 | 212 | Shape Cuboid(const Vector &corner1, const Vector &corner2){
 | 
|---|
 | 213 |   // make sure the two edges are upper left front and lower right back
 | 
|---|
 | 214 |   Vector sortedC1;
 | 
|---|
 | 215 |   Vector sortedC2;
 | 
|---|
 | 216 |   for(int i=NDIM;i--;){
 | 
|---|
 | 217 |     sortedC1[i] = min(corner1[i],corner2[i]);
 | 
|---|
 | 218 |     sortedC2[i] = max(corner1[i],corner2[i]);
 | 
|---|
 | 219 |     ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
 | 
|---|
 | 220 |   }
 | 
|---|
 | 221 |   // get the middle point
 | 
|---|
 | 222 |   Vector middle = (1./2.)*(sortedC1+sortedC2);
 | 
|---|
 | 223 |   Vector factors = sortedC2-middle;
 | 
|---|
 | 224 |   return translate(stretch(Cuboid(),factors),middle);
 | 
|---|
 | 225 | }
 | 
|---|