| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 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){
 | 
|---|
| 40 |   return point.NormSquared()<=1;
 | 
|---|
| 41 | }
 | 
|---|
| 42 | 
 | 
|---|
| 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)){
 | 
|---|
| 49 |     throw NotOnSurfaceException() << ShapeVector(&point);
 | 
|---|
| 50 |   }
 | 
|---|
| 51 |   return point;
 | 
|---|
| 52 | }
 | 
|---|
| 53 | 
 | 
|---|
| 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 | 
 | 
|---|
| 63 | string Sphere_impl::toString(){
 | 
|---|
| 64 |   return "Sphere()";
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /**
 | 
|---|
| 68 |  * algorithm taken from http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere
 | 
|---|
| 69 |  * \param N number of points on surface
 | 
|---|
| 70 |  */
 | 
|---|
| 71 | std::vector<Vector> Sphere_impl::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| 72 |   std::vector<Vector> PointsOnSurface;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   const double dlength = M_PI*(3.-sqrt(5.));
 | 
|---|
| 75 |   double length = 0;
 | 
|---|
| 76 |   const double dz = 2.0/N;
 | 
|---|
| 77 |   double z = 1. - dz/2.;
 | 
|---|
| 78 |   Vector point;
 | 
|---|
| 79 |   for (size_t ka = 0; ka<N; ka++){
 | 
|---|
| 80 |     const double r = sqrt(1.-z*z);
 | 
|---|
| 81 |     point.Zero();
 | 
|---|
| 82 |     point[0] = cos(length)*r;
 | 
|---|
| 83 |     point[1] = sin(length)*r;
 | 
|---|
| 84 |     point[2] = z;
 | 
|---|
| 85 |     PointsOnSurface.push_back(point);
 | 
|---|
| 86 |     z = z - dz;
 | 
|---|
| 87 |     length = length + dlength;
 | 
|---|
| 88 |   }
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   ASSERT(PointsOnSurface.size() == N, "Sphere_impl::getHomogeneousPointsOnSurface() did not create enough points.");
 | 
|---|
| 91 |   return PointsOnSurface;
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | 
 | 
|---|
| 95 | Shape Sphere(){
 | 
|---|
| 96 |   Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
 | 
|---|
| 97 |   return Shape(impl);
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | Shape Sphere(const Vector ¢er,double radius){
 | 
|---|
| 101 |   return translate(resize(Sphere(),radius),center);
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | Shape Ellipsoid(const Vector ¢er, const Vector &radius){
 | 
|---|
| 105 |   return translate(stretch(Sphere(),radius),center);
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | bool Cuboid_impl::isInside(const Vector &point){
 | 
|---|
| 109 |   return (point[0]>=0 && point[0]<=1) && (point[1]>=0 && point[1]<=1) && (point[2]>=0 && point[2]<=1);
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | bool Cuboid_impl::isOnSurface(const Vector &point){
 | 
|---|
| 113 |   bool retVal = isInside(point);
 | 
|---|
| 114 |   // test all borders of the cuboid
 | 
|---|
| 115 |   // double fabs
 | 
|---|
| 116 |   retVal = retVal &&
 | 
|---|
| 117 |            (((fabs(point[0]-1.)  < MYEPSILON) || (fabs(point[0])  < MYEPSILON)) ||
 | 
|---|
| 118 |             ((fabs(point[1]-1.)  < MYEPSILON) || (fabs(point[1])  < MYEPSILON)) ||
 | 
|---|
| 119 |             ((fabs(point[2]-1.)  < MYEPSILON) || (fabs(point[2])  < MYEPSILON)));
 | 
|---|
| 120 |   return retVal;
 | 
|---|
| 121 | }
 | 
|---|
| 122 | 
 | 
|---|
| 123 | Vector Cuboid_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
 | 
|---|
| 124 |   if(!isOnSurface(point)){
 | 
|---|
| 125 |     throw NotOnSurfaceException() << ShapeVector(&point);
 | 
|---|
| 126 |   }
 | 
|---|
| 127 |   Vector res;
 | 
|---|
| 128 |   // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
 | 
|---|
| 129 |   for(int i=NDIM;i--;){
 | 
|---|
| 130 |     if(fabs(fabs(point[i])-1)<MYEPSILON){
 | 
|---|
| 131 |       // add the scaled (-1/+1) Vector to the set of surface vectors
 | 
|---|
| 132 |       res[i] = point[i];
 | 
|---|
| 133 |     }
 | 
|---|
| 134 |   }
 | 
|---|
| 135 |   ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   res.Normalize();
 | 
|---|
| 138 |   return res;
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line){
 | 
|---|
| 142 |   LineSegmentSet res(line);
 | 
|---|
| 143 |   // get the intersection on each of the six faces
 | 
|---|
| 144 |   vector<Vector> intersections;
 | 
|---|
| 145 |   intersections.resize(2);
 | 
|---|
| 146 |   int c=0;
 | 
|---|
| 147 |   int x[2]={-1,+1};
 | 
|---|
| 148 |   for(int i=NDIM;i--;){
 | 
|---|
| 149 |     for(int p=0;p<2;++p){
 | 
|---|
| 150 |       if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
 | 
|---|
| 151 |       Vector base;
 | 
|---|
| 152 |       base[i]=x[p];
 | 
|---|
| 153 |       // base now points to the surface and is normal to it at the same time
 | 
|---|
| 154 |       Plane p(base,base);
 | 
|---|
| 155 |       Vector intersection = p.GetIntersection(line);
 | 
|---|
| 156 |       if(isInside(intersection)){
 | 
|---|
| 157 |         // if we have a point on the edge it might already be contained
 | 
|---|
| 158 |         if(c==1 && intersections[0]==intersection)
 | 
|---|
| 159 |           continue;
 | 
|---|
| 160 |         intersections[c++]=intersection;
 | 
|---|
| 161 |       }
 | 
|---|
| 162 |     }
 | 
|---|
| 163 |   }
 | 
|---|
| 164 |   end:
 | 
|---|
| 165 |   if(c==2){
 | 
|---|
| 166 |     res.insert(LineSegment(intersections[0],intersections[1]));
 | 
|---|
| 167 |   }
 | 
|---|
| 168 |   return res;
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | string Cuboid_impl::toString(){
 | 
|---|
| 172 |   return "Cuboid()";
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | /**
 | 
|---|
| 176 |  * \param N number of points on surface
 | 
|---|
| 177 |  */
 | 
|---|
| 178 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsOnSurface(const size_t N) const {
 | 
|---|
| 179 |   std::vector<Vector> PointsOnSurface;
 | 
|---|
| 180 |   ASSERT(false, "Cuboid_impl::getHomogeneousPointsOnSurface() not implemented yet");
 | 
|---|
| 181 |   return PointsOnSurface;
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | Shape Cuboid(){
 | 
|---|
| 185 |   Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
 | 
|---|
| 186 |   return Shape(impl);
 | 
|---|
| 187 | }
 | 
|---|
| 188 | 
 | 
|---|
| 189 | Shape Cuboid(const Vector &corner1, const Vector &corner2){
 | 
|---|
| 190 |   // make sure the two edges are upper left front and lower right back
 | 
|---|
| 191 |   Vector sortedC1;
 | 
|---|
| 192 |   Vector sortedC2;
 | 
|---|
| 193 |   for(int i=NDIM;i--;){
 | 
|---|
| 194 |     sortedC1[i] = min(corner1[i],corner2[i]);
 | 
|---|
| 195 |     sortedC2[i] = max(corner1[i],corner2[i]);
 | 
|---|
| 196 |     ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
 | 
|---|
| 197 |   }
 | 
|---|
| 198 |   // get the middle point
 | 
|---|
| 199 |   Vector middle = (1./2.)*(sortedC1+sortedC2);
 | 
|---|
| 200 |   Vector factors = sortedC2-middle;
 | 
|---|
| 201 |   return translate(stretch(Cuboid(),factors),middle);
 | 
|---|
| 202 | }
 | 
|---|