| 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/RealSpaceMatrix.hpp"
|
|---|
| 32 | #include "LinearAlgebra/Line.hpp"
|
|---|
| 33 | #include "LinearAlgebra/Plane.hpp"
|
|---|
| 34 | #include "LinearAlgebra/LineSegment.hpp"
|
|---|
| 35 | #include "LinearAlgebra/LineSegmentSet.hpp"
|
|---|
| 36 |
|
|---|
| 37 | #include <cmath>
|
|---|
| 38 | #include <algorithm>
|
|---|
| 39 |
|
|---|
| 40 | // CYLINDER CODE
|
|---|
| 41 | // ----------------------------------------------------------------------------
|
|---|
| 42 | bool Cylinder_impl::isInside(const Vector &point) const {
|
|---|
| 43 | return (Vector(point[0], point[1], 0.0).NormSquared() < 1.0+MYEPSILON) &&
|
|---|
| 44 | (point[2] > -1.0-MYEPSILON) && (point[2] < 1.0+MYEPSILON);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | bool Cylinder_impl::isOnSurface(const Vector &point) const {
|
|---|
| 48 | return fabs(Vector(point[0], point[1], 0.0).NormSquared()-1.0)<MYEPSILON &&
|
|---|
| 49 | (point[2] > -1.0-MYEPSILON) && (point[2] < 1.0+MYEPSILON);
|
|---|
| 50 |
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | Vector Cylinder_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException) {
|
|---|
| 54 | if(!isOnSurface(point)){
|
|---|
| 55 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if ((fabs(point[2]-1)<MYEPSILON) || (fabs(point[2])<MYEPSILON))
|
|---|
| 59 | return Vector(0.0, 0.0, point[2]);
|
|---|
| 60 | else
|
|---|
| 61 | return Vector(point[0], point[1], 0.0);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | Vector Cylinder_impl::getCenter() const
|
|---|
| 65 | {
|
|---|
| 66 | return Vector(0.0, 0.0, 0.0);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | double Cylinder_impl::getRadius() const
|
|---|
| 70 | {
|
|---|
| 71 | return 1.0;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | double Cylinder_impl::getVolume() const
|
|---|
| 75 | {
|
|---|
| 76 | return M_PI*2.0; // pi r^2 h
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | double Cylinder_impl::getSurfaceArea() const
|
|---|
| 80 | {
|
|---|
| 81 | return 2.0*M_PI*2.0; // 2 pi r h
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | LineSegmentSet Cylinder_impl::getLineIntersections(const Line &line) const {
|
|---|
| 85 | // TODO
|
|---|
| 86 | ASSERT(0, "Cylinder_impl::getLineIntersections - not implemented.");
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | std::string Cylinder_impl::toString() const
|
|---|
| 90 | {
|
|---|
| 91 | return "Cylinder()";
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | enum ShapeType Cylinder_impl::getType() const
|
|---|
| 95 | {
|
|---|
| 96 | return CylinderType;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | std::vector<Vector> Cylinder_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 100 | // TODO
|
|---|
| 101 | ASSERT(0, "Cylinder_impl::getHomogeneousPointsOnSurface - not implemented.");
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | std::vector<Vector> Cylinder_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
|---|
| 105 | // TODO
|
|---|
| 106 | ASSERT(0, "Cylinder_impl::getHomogeneousPointsInVolume - not implemented.");
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | Shape Cylinder() {
|
|---|
| 110 | Shape::impl_ptr impl = Shape::impl_ptr(new Cylinder_impl());
|
|---|
| 111 | return Shape(impl);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | Shape Cylinder(const Vector ¢er, const double xrot, const double yrot,
|
|---|
| 115 | const double height, const double radius)
|
|---|
| 116 | {
|
|---|
| 117 | RealSpaceMatrix rot;
|
|---|
| 118 | rot.setRotation(xrot, yrot, 0.0);
|
|---|
| 119 |
|
|---|
| 120 | return translate(
|
|---|
| 121 | transform(
|
|---|
| 122 | stretch(
|
|---|
| 123 | Cylinder(),
|
|---|
| 124 | Vector(radius, radius, height*0.5)),
|
|---|
| 125 | rot),
|
|---|
| 126 | center);
|
|---|
| 127 | }
|
|---|
| 128 | // ----------------------------------------------------------------------------
|
|---|
| 129 |
|
|---|
| 130 | bool Sphere_impl::isInside(const Vector &point) const{
|
|---|
| 131 | return point.NormSquared()<=1.;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | bool Sphere_impl::isOnSurface(const Vector &point) const{
|
|---|
| 135 | return fabs(point.NormSquared()-1.)<MYEPSILON;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | Vector Sphere_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
|---|
| 139 | if(!isOnSurface(point)){
|
|---|
| 140 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 141 | }
|
|---|
| 142 | return point;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | Vector Sphere_impl::getCenter() const
|
|---|
| 146 | {
|
|---|
| 147 | return Vector(0.,0.,0.);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | double Sphere_impl::getRadius() const
|
|---|
| 151 | {
|
|---|
| 152 | return 1.;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | double Sphere_impl::getVolume() const
|
|---|
| 156 | {
|
|---|
| 157 | return (4./3.)*M_PI; // 4/3 pi r^3
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | double Sphere_impl::getSurfaceArea() const
|
|---|
| 161 | {
|
|---|
| 162 | return 2.*M_PI; // 2 pi r^2
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | LineSegmentSet Sphere_impl::getLineIntersections(const Line &line) const{
|
|---|
| 167 | LineSegmentSet res(line);
|
|---|
| 168 | std::vector<Vector> intersections = line.getSphereIntersections();
|
|---|
| 169 | if(intersections.size()==2){
|
|---|
| 170 | res.insert(LineSegment(intersections[0],intersections[1]));
|
|---|
| 171 | }
|
|---|
| 172 | return res;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | std::string Sphere_impl::toString() const{
|
|---|
| 176 | return "Sphere()";
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | enum ShapeType Sphere_impl::getType() const
|
|---|
| 180 | {
|
|---|
| 181 | return SphereType;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /**
|
|---|
| 185 | * algorithm taken from http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere
|
|---|
| 186 | * \param N number of points on surface
|
|---|
| 187 | */
|
|---|
| 188 | std::vector<Vector> Sphere_impl::getHomogeneousPointsOnSurface(const size_t N) const
|
|---|
| 189 | {
|
|---|
| 190 | std::vector<Vector> PointsOnSurface;
|
|---|
| 191 | if (true) {
|
|---|
| 192 | // Exactly N points but not symmetric.
|
|---|
| 193 |
|
|---|
| 194 | // This formula is derived by finding a curve on the sphere that spirals down from
|
|---|
| 195 | // the north pole to the south pole keeping a constant distance between consecutive turns.
|
|---|
| 196 | // The curve is then parametrized by arch length and evaluated in constant intervals.
|
|---|
| 197 | double a = sqrt(N) * 2;
|
|---|
| 198 | for (int i=0; i<N; i++){
|
|---|
| 199 | double t0 = ((double)i + 0.5) / (double)N;
|
|---|
| 200 | double t = (sqrt(t0) - sqrt(1.0 - t0) + 1.0) / 2.0 * M_PI;
|
|---|
| 201 | Vector point;
|
|---|
| 202 | point.Zero();
|
|---|
| 203 | point[0] = sin(t) * sin(t * a);
|
|---|
| 204 | point[1] = sin(t) * cos(t * a);
|
|---|
| 205 | point[2] = cos(t);
|
|---|
| 206 | PointsOnSurface.push_back(point);
|
|---|
| 207 | }
|
|---|
| 208 | ASSERT(PointsOnSurface.size() == N,
|
|---|
| 209 | "Sphere_impl::getHomogeneousPointsOnSurface() did not create "
|
|---|
| 210 | +::toString(N)+" but "+::toString(PointsOnSurface.size())+" points.");
|
|---|
| 211 | } else {
|
|---|
| 212 | // Symmetric but only approximately N points.
|
|---|
| 213 | double a=4*M_PI/N;
|
|---|
| 214 | double d= sqrt(a);
|
|---|
| 215 | int Mtheta=int(M_PI/d);
|
|---|
| 216 | double dtheta=M_PI/Mtheta;
|
|---|
| 217 | double dphi=a/dtheta;
|
|---|
| 218 | for (int m=0; m<Mtheta; m++)
|
|---|
| 219 | {
|
|---|
| 220 | double theta=M_PI*(m+0.5)/Mtheta;
|
|---|
| 221 | int Mphi=int(2*M_PI*sin(theta)/dphi);
|
|---|
| 222 | for (int n=0; n<Mphi;n++)
|
|---|
| 223 | {
|
|---|
| 224 | double phi= 2*M_PI*n/Mphi;
|
|---|
| 225 | Vector point;
|
|---|
| 226 | point.Zero();
|
|---|
| 227 | point[0]=sin(theta)*cos(phi);
|
|---|
| 228 | point[1]=sin(theta)*sin(phi);
|
|---|
| 229 | point[2]=cos(theta);
|
|---|
| 230 | PointsOnSurface.push_back(point);
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | return PointsOnSurface;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | std::vector<Vector> Sphere_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
|---|
| 238 | ASSERT(0,
|
|---|
| 239 | "Sphere_impl::getHomogeneousPointsInVolume() - not implemented.");
|
|---|
| 240 | return std::vector<Vector>();
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | Shape Sphere(){
|
|---|
| 244 | Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
|
|---|
| 245 | return Shape(impl);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | Shape Sphere(const Vector ¢er,double radius){
|
|---|
| 249 | return translate(resize(Sphere(),radius),center);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | Shape Ellipsoid(const Vector ¢er, const Vector &radius){
|
|---|
| 253 | return translate(stretch(Sphere(),radius),center);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | bool Cuboid_impl::isInside(const Vector &point) const{
|
|---|
| 257 | return (point[0]>=0 && point[0]<=1) && (point[1]>=0 && point[1]<=1) && (point[2]>=0 && point[2]<=1);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | bool Cuboid_impl::isOnSurface(const Vector &point) const{
|
|---|
| 261 | bool retVal = isInside(point);
|
|---|
| 262 | // test all borders of the cuboid
|
|---|
| 263 | // double fabs
|
|---|
| 264 | retVal = retVal &&
|
|---|
| 265 | (((fabs(point[0]-1.) < MYEPSILON) || (fabs(point[0]) < MYEPSILON)) ||
|
|---|
| 266 | ((fabs(point[1]-1.) < MYEPSILON) || (fabs(point[1]) < MYEPSILON)) ||
|
|---|
| 267 | ((fabs(point[2]-1.) < MYEPSILON) || (fabs(point[2]) < MYEPSILON)));
|
|---|
| 268 | return retVal;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | Vector Cuboid_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
|---|
| 272 | if(!isOnSurface(point)){
|
|---|
| 273 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 274 | }
|
|---|
| 275 | Vector res;
|
|---|
| 276 | // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
|
|---|
| 277 | for(int i=NDIM;i--;){
|
|---|
| 278 | if(fabs(fabs(point[i])-1)<MYEPSILON){
|
|---|
| 279 | // add the scaled (-1/+1) Vector to the set of surface vectors
|
|---|
| 280 | res[i] = point[i];
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 | ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
|
|---|
| 284 |
|
|---|
| 285 | res.Normalize();
|
|---|
| 286 | return res;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 | Vector Cuboid_impl::getCenter() const
|
|---|
| 291 | {
|
|---|
| 292 | return Vector(0.5,0.5,0.5);
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | double Cuboid_impl::getRadius() const
|
|---|
| 296 | {
|
|---|
| 297 | return .5;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | double Cuboid_impl::getVolume() const
|
|---|
| 301 | {
|
|---|
| 302 | return 1.; // l^3
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | double Cuboid_impl::getSurfaceArea() const
|
|---|
| 306 | {
|
|---|
| 307 | return 6.; // 6 * l^2
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line) const{
|
|---|
| 311 | LineSegmentSet res(line);
|
|---|
| 312 | // get the intersection on each of the six faces
|
|---|
| 313 | std::vector<Vector> intersections;
|
|---|
| 314 | intersections.resize(2);
|
|---|
| 315 | int c=0;
|
|---|
| 316 | int x[2]={-1,+1};
|
|---|
| 317 | for(int i=NDIM;i--;){
|
|---|
| 318 | for(int p=0;p<2;++p){
|
|---|
| 319 | if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
|
|---|
| 320 | Vector base;
|
|---|
| 321 | base[i]=x[p];
|
|---|
| 322 | // base now points to the surface and is normal to it at the same time
|
|---|
| 323 | Plane p(base,base);
|
|---|
| 324 | Vector intersection = p.GetIntersection(line);
|
|---|
| 325 | if(isInside(intersection)){
|
|---|
| 326 | // if we have a point on the edge it might already be contained
|
|---|
| 327 | if(c==1 && intersections[0]==intersection)
|
|---|
| 328 | continue;
|
|---|
| 329 | intersections[c++]=intersection;
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 | end:
|
|---|
| 334 | if(c==2){
|
|---|
| 335 | res.insert(LineSegment(intersections[0],intersections[1]));
|
|---|
| 336 | }
|
|---|
| 337 | return res;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | std::string Cuboid_impl::toString() const{
|
|---|
| 341 | return "Cuboid()";
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | enum ShapeType Cuboid_impl::getType() const
|
|---|
| 345 | {
|
|---|
| 346 | return CuboidType;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | /**
|
|---|
| 350 | * \param N number of points on surface
|
|---|
| 351 | */
|
|---|
| 352 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 353 | std::vector<Vector> PointsOnSurface;
|
|---|
| 354 | ASSERT(false, "Cuboid_impl::getHomogeneousPointsOnSurface() not implemented yet");
|
|---|
| 355 | return PointsOnSurface;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | std::vector<Vector> Cuboid_impl::getHomogeneousPointsInVolume(const size_t N) const {
|
|---|
| 359 | ASSERT(0,
|
|---|
| 360 | "Cuboid_impl::getHomogeneousPointsInVolume() - not implemented.");
|
|---|
| 361 | return std::vector<Vector>();
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | Shape Cuboid(){
|
|---|
| 365 | Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
|
|---|
| 366 | return Shape(impl);
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | Shape Cuboid(const Vector &corner1, const Vector &corner2){
|
|---|
| 370 | // make sure the two edges are upper left front and lower right back
|
|---|
| 371 | Vector sortedC1;
|
|---|
| 372 | Vector sortedC2;
|
|---|
| 373 | for(int i=NDIM;i--;){
|
|---|
| 374 | sortedC1[i] = std::min(corner1[i],corner2[i]);
|
|---|
| 375 | sortedC2[i] = std::max(corner1[i],corner2[i]);
|
|---|
| 376 | ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
|
|---|
| 377 | }
|
|---|
| 378 | // get the middle point
|
|---|
| 379 | Vector middle = (1./2.)*(sortedC1+sortedC2);
|
|---|
| 380 | Vector factors = sortedC2-middle;
|
|---|
| 381 | return translate(stretch(Cuboid(),factors),middle);
|
|---|
| 382 | }
|
|---|