| 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 | if (true) {
|
|---|
| 101 | // Exactly N points but not symmetric.
|
|---|
| 102 |
|
|---|
| 103 | // This formula is derived by finding a curve on the sphere that spirals down from
|
|---|
| 104 | // the north pole to the south pole keeping a constant distance between consecutive turns.
|
|---|
| 105 | // The curve is then parametrized by arch length and evaluated in constant intervals.
|
|---|
| 106 | double a = sqrt(N) * 2;
|
|---|
| 107 | for (int i=0; i<N; i++){
|
|---|
| 108 | double t0 = ((double)i + 0.5) / (double)N;
|
|---|
| 109 | double t = (sqrt(t0) - sqrt(1.0 - t0) + 1.0) / 2.0 * M_PI;
|
|---|
| 110 | Vector point;
|
|---|
| 111 | point.Zero();
|
|---|
| 112 | point[0] = sin(t) * sin(t * a);
|
|---|
| 113 | point[1] = sin(t) * cos(t * a);
|
|---|
| 114 | point[2] = cos(t);
|
|---|
| 115 | PointsOnSurface.push_back(point);
|
|---|
| 116 | }
|
|---|
| 117 | ASSERT(PointsOnSurface.size() == N,
|
|---|
| 118 | "Sphere_impl::getHomogeneousPointsOnSurface() did not create "
|
|---|
| 119 | +::toString(N)+" but "+::toString(PointsOnSurface.size())+" points.");
|
|---|
| 120 | } else {
|
|---|
| 121 | // Symmetric but only approximately N points.
|
|---|
| 122 | double a=4*M_PI/N;
|
|---|
| 123 | double d= sqrt(a);
|
|---|
| 124 | int Mtheta=int(M_PI/d);
|
|---|
| 125 | double dtheta=M_PI/Mtheta;
|
|---|
| 126 | double dphi=a/dtheta;
|
|---|
| 127 | for (int m=0; m<Mtheta; m++)
|
|---|
| 128 | {
|
|---|
| 129 | double theta=M_PI*(m+0.5)/Mtheta;
|
|---|
| 130 | int Mphi=int(2*M_PI*sin(theta)/dphi);
|
|---|
| 131 | for (int n=0; n<Mphi;n++)
|
|---|
| 132 | {
|
|---|
| 133 | double phi= 2*M_PI*n/Mphi;
|
|---|
| 134 | Vector point;
|
|---|
| 135 | point.Zero();
|
|---|
| 136 | point[0]=sin(theta)*cos(phi);
|
|---|
| 137 | point[1]=sin(theta)*sin(phi);
|
|---|
| 138 | point[2]=cos(theta);
|
|---|
| 139 | PointsOnSurface.push_back(point);
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | return PointsOnSurface;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | std::vector<Vector> Sphere_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
|---|
| 147 | ASSERT(0,
|
|---|
| 148 | "Sphere_impl::getHomogeneousPointsInVolume() - not implemented.");
|
|---|
| 149 | return std::vector<Vector>();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | Shape Sphere(){
|
|---|
| 153 | Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
|
|---|
| 154 | return Shape(impl);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | Shape Sphere(const Vector ¢er,double radius){
|
|---|
| 158 | return translate(resize(Sphere(),radius),center);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | Shape Ellipsoid(const Vector ¢er, const Vector &radius){
|
|---|
| 162 | return translate(stretch(Sphere(),radius),center);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | bool Cuboid_impl::isInside(const Vector &point) const{
|
|---|
| 166 | return (point[0]>=0 && point[0]<=1) && (point[1]>=0 && point[1]<=1) && (point[2]>=0 && point[2]<=1);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | bool Cuboid_impl::isOnSurface(const Vector &point) const{
|
|---|
| 170 | bool retVal = isInside(point);
|
|---|
| 171 | // test all borders of the cuboid
|
|---|
| 172 | // double fabs
|
|---|
| 173 | retVal = retVal &&
|
|---|
| 174 | (((fabs(point[0]-1.) < MYEPSILON) || (fabs(point[0]) < MYEPSILON)) ||
|
|---|
| 175 | ((fabs(point[1]-1.) < MYEPSILON) || (fabs(point[1]) < MYEPSILON)) ||
|
|---|
| 176 | ((fabs(point[2]-1.) < MYEPSILON) || (fabs(point[2]) < MYEPSILON)));
|
|---|
| 177 | return retVal;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | Vector Cuboid_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
|---|
| 181 | if(!isOnSurface(point)){
|
|---|
| 182 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 183 | }
|
|---|
| 184 | Vector res;
|
|---|
| 185 | // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
|
|---|
| 186 | for(int i=NDIM;i--;){
|
|---|
| 187 | if(fabs(fabs(point[i])-1)<MYEPSILON){
|
|---|
| 188 | // add the scaled (-1/+1) Vector to the set of surface vectors
|
|---|
| 189 | res[i] = point[i];
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
|
|---|
| 193 |
|
|---|
| 194 | res.Normalize();
|
|---|
| 195 | return res;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | Vector Cuboid_impl::getCenter() const
|
|---|
| 200 | {
|
|---|
| 201 | return Vector(0.5,0.5,0.5);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | double Cuboid_impl::getRadius() const
|
|---|
| 205 | {
|
|---|
| 206 | return .5;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | double Cuboid_impl::getVolume() const
|
|---|
| 210 | {
|
|---|
| 211 | return 1.; // l^3
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | double Cuboid_impl::getSurfaceArea() const
|
|---|
| 215 | {
|
|---|
| 216 | return 6.; // 6 * l^2
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line) const{
|
|---|
| 220 | LineSegmentSet res(line);
|
|---|
| 221 | // get the intersection on each of the six faces
|
|---|
| 222 | std::vector<Vector> intersections;
|
|---|
| 223 | intersections.resize(2);
|
|---|
| 224 | int c=0;
|
|---|
| 225 | int x[2]={-1,+1};
|
|---|
| 226 | for(int i=NDIM;i--;){
|
|---|
| 227 | for(int p=0;p<2;++p){
|
|---|
| 228 | if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
|
|---|
| 229 | Vector base;
|
|---|
| 230 | base[i]=x[p];
|
|---|
| 231 | // base now points to the surface and is normal to it at the same time
|
|---|
| 232 | Plane p(base,base);
|
|---|
| 233 | Vector intersection = p.GetIntersection(line);
|
|---|
| 234 | if(isInside(intersection)){
|
|---|
| 235 | // if we have a point on the edge it might already be contained
|
|---|
| 236 | if(c==1 && intersections[0]==intersection)
|
|---|
| 237 | continue;
|
|---|
| 238 | intersections[c++]=intersection;
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 | end:
|
|---|
| 243 | if(c==2){
|
|---|
| 244 | res.insert(LineSegment(intersections[0],intersections[1]));
|
|---|
| 245 | }
|
|---|
| 246 | return res;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | std::string Cuboid_impl::toString() const{
|
|---|
| 250 | return "Cuboid()";
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | enum ShapeType Cuboid_impl::getType() const
|
|---|
| 254 | {
|
|---|
| 255 | return CuboidType;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /**
|
|---|
| 259 | * \param N number of points on surface
|
|---|
| 260 | */
|
|---|
| 261 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 262 | std::vector<Vector> PointsOnSurface;
|
|---|
| 263 | ASSERT(false, "Cuboid_impl::getHomogeneousPointsOnSurface() not implemented yet");
|
|---|
| 264 | return PointsOnSurface;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
|---|
| 268 | ASSERT(0,
|
|---|
| 269 | "Cuboid_impl::getHomogeneousPointsInVolume() - not implemented.");
|
|---|
| 270 | return std::vector<Vector>();
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | Shape Cuboid(){
|
|---|
| 274 | Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
|
|---|
| 275 | return Shape(impl);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | Shape Cuboid(const Vector &corner1, const Vector &corner2){
|
|---|
| 279 | // make sure the two edges are upper left front and lower right back
|
|---|
| 280 | Vector sortedC1;
|
|---|
| 281 | Vector sortedC2;
|
|---|
| 282 | for(int i=NDIM;i--;){
|
|---|
| 283 | sortedC1[i] = std::min(corner1[i],corner2[i]);
|
|---|
| 284 | sortedC2[i] = std::max(corner1[i],corner2[i]);
|
|---|
| 285 | ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
|
|---|
| 286 | }
|
|---|
| 287 | // get the middle point
|
|---|
| 288 | Vector middle = (1./2.)*(sortedC1+sortedC2);
|
|---|
| 289 | Vector factors = sortedC2-middle;
|
|---|
| 290 | return translate(stretch(Cuboid(),factors),middle);
|
|---|
| 291 | }
|
|---|