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