Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Shapes/Shape.cpp

    rcfda65 r205d9b  
    88#include "Shape.hpp"
    99#include "Shape_impl.hpp"
    10 
    11 #include "Helpers/Assert.hpp"
    12 
    13 #include <string>
    14 
    15 using namespace std;
    1610
    1711Shape::Shape(const Shape& src) :
     
    2519}
    2620
    27 bool Shape::isOnSurface(const Vector &point) const{
    28   return impl->isOnSurface(point);
    29 }
    30 
    31 Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
    32   return impl->getNormal(point);
    33 }
    34 
    3521Shape::Shape(Shape::impl_ptr _impl) :
    3622    impl(_impl)
     
    4228  }
    4329  return *this;
    44 }
    45 
    46 std::string Shape::toString() const{
    47   return impl->toString();
    4830}
    4931
     
    8365}
    8466
    85 bool AndShape_impl::isOnSurface(const Vector &point){
    86   // check the number of surfaces that this point is on
    87   int surfaces =0;
    88   surfaces += lhs->isOnSurface(point);
    89   surfaces += rhs->isOnSurface(point);
    90 
    91   switch(surfaces){
    92     case 0:
    93       return false;
    94       // no break necessary
    95     case 1:
    96       // if it is inside for the object where it does not lie on
    97       // the surface the whole point lies inside
    98       return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
    99              (rhs->isOnSurface(point) && lhs->isInside(point));
    100       // no break necessary
    101     case 2:
    102       {
    103         // it lies on both Shapes... could be an edge or an inner point
    104         // test the direction of the normals
    105         Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
    106         // if the directions are opposite we lie on the inside
    107         return !direction.IsZero();
    108       }
    109       // no break necessary
    110     default:
    111       // if this happens there is something wrong
    112       ASSERT(0,"Default case should have never been used");
    113   }
    114   return false; // never reached
    115 }
    116 
    117 Vector AndShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
    118   Vector res;
    119   if(!isOnSurface(point)){
    120     throw NotOnSurfaceException(__FILE__,__LINE__);
    121   }
    122   res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
    123   res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
    124   res.Normalize();
    125   return res;
    126 }
    127 
    128 string AndShape_impl::toString(){
    129   return string("(") + lhs->toString() + string("&&") + rhs->toString() + string(")");
    130 }
    131 
    13267Shape operator&&(const Shape &lhs,const Shape &rhs){
    13368  Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
     
    14580bool OrShape_impl::isInside(const Vector &point){
    14681  return rhs->isInside(point) || lhs->isInside(point);
    147 }
    148 
    149 bool OrShape_impl::isOnSurface(const Vector &point){
    150   // check the number of surfaces that this point is on
    151   int surfaces =0;
    152   surfaces += lhs->isOnSurface(point);
    153   surfaces += rhs->isOnSurface(point);
    154 
    155   switch(surfaces){
    156     case 0:
    157       return false;
    158       // no break necessary
    159     case 1:
    160       // if it is inside for the object where it does not lie on
    161       // the surface the whole point lies inside
    162       return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
    163              (rhs->isOnSurface(point) && !lhs->isInside(point));
    164       // no break necessary
    165     case 2:
    166       {
    167         // it lies on both Shapes... could be an edge or an inner point
    168         // test the direction of the normals
    169         Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
    170         // if the directions are opposite we lie on the inside
    171         return !direction.IsZero();
    172       }
    173       // no break necessary
    174     default:
    175       // if this happens there is something wrong
    176       ASSERT(0,"Default case should have never been used");
    177   }
    178   return false; // never reached
    179 }
    180 
    181 Vector OrShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
    182   Vector res;
    183   if(!isOnSurface(point)){
    184     throw NotOnSurfaceException(__FILE__,__LINE__);
    185   }
    186   res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
    187   res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
    188   res.Normalize();
    189   return res;
    190 }
    191 
    192 string OrShape_impl::toString(){
    193   return string("(") + lhs->toString() + string("||") + rhs->toString() + string(")");
    19482}
    19583
     
    21199}
    212100
    213 bool NotShape_impl::isOnSurface(const Vector &point){
    214   return arg->isOnSurface(point);
    215 }
    216 
    217 Vector NotShape_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
    218   return -1*arg->getNormal(point);
    219 }
    220 
    221 string NotShape_impl::toString(){
    222   return string("!") + arg->toString();
    223 }
    224 
    225101Shape operator!(const Shape &arg){
    226102  Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
    227103  return Shape(newImpl);
    228104}
    229 
    230 /**************** global operations *********************************/
    231 ostream &operator<<(ostream &ost,const Shape &shape){
    232   ost << shape.toString();
    233   return ost;
    234 }
Note: See TracChangeset for help on using the changeset viewer.