Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Shapes/BaseShapes.cpp

    re38447 rd76a7c  
    88#include "Shapes/BaseShapes.hpp"
    99#include "Shapes/BaseShapes_impl.hpp"
     10#include "Shapes/ShapeOps.hpp"
    1011
    1112#include "vector.hpp"
     13#include "Helpers/Assert.hpp"
     14
     15#include <cmath>
     16#include <algorithm>
    1217
    1318bool Sphere_impl::isInside(const Vector &point){
    1419  return point.NormSquared()<=1;
     20}
     21
     22bool Sphere_impl::isOnSurface(const Vector &point){
     23  return fabs(point.NormSquared()-1)<MYEPSILON;
     24}
     25
     26Vector Sphere_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
     27  if(!isOnSurface(point)){
     28    throw NotOnSurfaceException(__FILE__,__LINE__);
     29  }
     30  return point;
     31}
     32
     33string Sphere_impl::toString(){
     34  return "Sphere()";
    1535}
    1636
     
    2040}
    2141
     42Shape Sphere(const Vector &center,double radius){
     43  return translate(resize(Sphere(),radius),center);
     44}
     45
     46Shape Ellipsoid(const Vector &center, const Vector &radius){
     47  return translate(stretch(Sphere(),radius),center);
     48}
     49
    2250bool Cuboid_impl::isInside(const Vector &point){
    23   return point[0]<=1 && point[1]<=1 && point[2]<=1;
     51  return fabs(point[0])<=1 && fabs(point[1])<=1 && fabs(point[2])<=1;
     52}
     53
     54bool Cuboid_impl::isOnSurface(const Vector &point){
     55  bool retVal = isInside(point);
     56  // test all borders of the cuboid
     57  // double fabs
     58  retVal = retVal &&
     59           ((fabs(fabs(point[0])-1)  < MYEPSILON) ||
     60            (fabs(fabs(point[1])-1)  < MYEPSILON) ||
     61            (fabs(fabs(point[2])-1)  < MYEPSILON));
     62  return retVal;
     63}
     64
     65Vector Cuboid_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
     66  if(!isOnSurface(point)){
     67    throw NotOnSurfaceException(__FILE__,__LINE__);
     68  }
     69  Vector res;
     70  // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
     71  for(int i=NDIM;i--;){
     72    if(fabs(fabs(point[i])-1)<MYEPSILON){
     73      // add the scaled (-1/+1) Vector to the set of surface vectors
     74      res[i] = point[i];
     75    }
     76  }
     77  ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
     78
     79  res.Normalize();
     80  return res;
     81}
     82
     83string Cuboid_impl::toString(){
     84  return "Cuboid()";
    2485}
    2586
    2687Shape Cuboid(){
    27   Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
     88  Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
    2889  return Shape(impl);
    2990}
     91
     92Shape Cuboid(const Vector &corner1, const Vector &corner2){
     93  // make sure the two edges are upper left front and lower right back
     94  Vector sortedC1;
     95  Vector sortedC2;
     96  for(int i=NDIM;i--;){
     97    sortedC1[i] = min(corner1[i],corner2[i]);
     98    sortedC2[i] = max(corner1[i],corner2[i]);
     99    ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
     100  }
     101  // get the middle point
     102  Vector middle = (1./2.)*(sortedC1+sortedC2);
     103  Vector factors = sortedC2-middle;
     104  return translate(stretch(Cuboid(),factors),middle);
     105}
Note: See TracChangeset for help on using the changeset viewer.