Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Shapes/BaseShapes.cpp

    re38447 rc6f395  
    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 "Line.hpp"
     16#include "Plane.hpp"
     17#include "LineSegment.hpp"
     18#include "LineSegmentSet.hpp"
     19
     20#include <cmath>
     21#include <algorithm>
    1222
    1323bool Sphere_impl::isInside(const Vector &point){
    1424  return point.NormSquared()<=1;
     25}
     26
     27bool Sphere_impl::isOnSurface(const Vector &point){
     28  return fabs(point.NormSquared()-1)<MYEPSILON;
     29}
     30
     31Vector Sphere_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
     32  if(!isOnSurface(point)){
     33    throw NotOnSurfaceException(__FILE__,__LINE__);
     34  }
     35  return point;
     36}
     37
     38LineSegmentSet Sphere_impl::getLineIntersections(const Line &line){
     39  LineSegmentSet res(line);
     40  std::vector<Vector> intersections = line.getSphereIntersections();
     41  if(intersections.size()==2){
     42    res.insert(LineSegment(intersections[0],intersections[1]));
     43  }
     44  return res;
     45}
     46
     47string Sphere_impl::toString(){
     48  return "Sphere()";
    1549}
    1650
     
    2054}
    2155
     56Shape Sphere(const Vector &center,double radius){
     57  return translate(resize(Sphere(),radius),center);
     58}
     59
     60Shape Ellipsoid(const Vector &center, const Vector &radius){
     61  return translate(stretch(Sphere(),radius),center);
     62}
     63
    2264bool Cuboid_impl::isInside(const Vector &point){
    23   return point[0]<=1 && point[1]<=1 && point[2]<=1;
     65  return fabs(point[0])<=1 && fabs(point[1])<=1 && fabs(point[2])<=1;
     66}
     67
     68bool Cuboid_impl::isOnSurface(const Vector &point){
     69  bool retVal = isInside(point);
     70  // test all borders of the cuboid
     71  // double fabs
     72  retVal = retVal &&
     73           ((fabs(fabs(point[0])-1)  < MYEPSILON) ||
     74            (fabs(fabs(point[1])-1)  < MYEPSILON) ||
     75            (fabs(fabs(point[2])-1)  < MYEPSILON));
     76  return retVal;
     77}
     78
     79Vector Cuboid_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
     80  if(!isOnSurface(point)){
     81    throw NotOnSurfaceException(__FILE__,__LINE__);
     82  }
     83  Vector res;
     84  // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
     85  for(int i=NDIM;i--;){
     86    if(fabs(fabs(point[i])-1)<MYEPSILON){
     87      // add the scaled (-1/+1) Vector to the set of surface vectors
     88      res[i] = point[i];
     89    }
     90  }
     91  ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
     92
     93  res.Normalize();
     94  return res;
     95}
     96
     97LineSegmentSet Cuboid_impl::getLineIntersections(const Line &line){
     98  LineSegmentSet res(line);
     99  // get the intersection on each of the six faces
     100  vector<Vector> intersections;
     101  intersections.resize(2);
     102  int c=0;
     103  int x[2]={-1,+1};
     104  for(int i=NDIM;i--;){
     105    for(int p=0;p<2;++p){
     106      if(c==2) goto end; // I know this sucks, but breaking two loops is stupid
     107      Vector base;
     108      base[i]=x[p];
     109      // base now points to the surface and is normal to it at the same time
     110      Plane p(base,base);
     111      Vector intersection = p.GetIntersection(line);
     112      if(isInside(intersection)){
     113        // if we have a point on the edge it might already be contained
     114        if(c==1 && intersections[0]==intersection)
     115          continue;
     116        intersections[c++]=intersection;
     117      }
     118    }
     119  }
     120  end:
     121  if(c==2){
     122    res.insert(LineSegment(intersections[0],intersections[1]));
     123  }
     124  return res;
     125}
     126
     127string Cuboid_impl::toString(){
     128  return "Cuboid()";
    24129}
    25130
    26131Shape Cuboid(){
    27   Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
     132  Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
    28133  return Shape(impl);
    29134}
     135
     136Shape Cuboid(const Vector &corner1, const Vector &corner2){
     137  // make sure the two edges are upper left front and lower right back
     138  Vector sortedC1;
     139  Vector sortedC2;
     140  for(int i=NDIM;i--;){
     141    sortedC1[i] = min(corner1[i],corner2[i]);
     142    sortedC2[i] = max(corner1[i],corner2[i]);
     143    ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
     144  }
     145  // get the middle point
     146  Vector middle = (1./2.)*(sortedC1+sortedC2);
     147  Vector factors = sortedC2-middle;
     148  return translate(stretch(Cuboid(),factors),middle);
     149}
Note: See TracChangeset for help on using the changeset viewer.