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