Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Shapes/Shape.cpp

    rda1e92 r955b91  
    2626#include "Shapes/Shape_impl.hpp"
    2727#include "Shapes/ShapeExceptions.hpp"
    28 #include "Shapes/ShapeType.hpp"
    29 
    30 #include "Tesselation/ApproximateShapeArea.hpp"
    31 #include "Tesselation/ApproximateShapeVolume.hpp"
    32 
    33 #include <algorithm>
    34 #include <limits>
     28
    3529#include <string>
    3630
     
    5448}
    5549
    56 Vector Shape::getCenter() const{
    57   return impl->getCenter();
    58 }
    59 
    60 double Shape::getRadius() const{
    61   return impl->getRadius();
    62 }
    63 
    64 /** Returns the volume of the Shape.
    65  *
    66  * If the underlying implementation does not have a working implementation,
    67  * i.e. returns -1., then we use an approximate method to calculate the
    68  * volume via a mesh of grid points and checking for isInside (basically
    69  * a Monte-Carlo integration of the volume).
    70  *
    71  * \return volume of the shape
    72  */
    73 double Shape::getVolume() const
    74 {
    75         const double volume = impl->getVolume();
    76         if (volume  != -1.) {
    77                 return volume;
    78         } else {
    79                 ApproximateShapeVolume Approximator(*this);
    80                 return Approximator();
    81         }
    82 }
    83 
    84 /** Returns the surface area of the Shape.
    85  *
    86  * If the underlying implementation does not have a working implementation,
    87  * i.e. returns -1., then we use the working filling of the shapes surface
    88  * with points and subsequent tesselation and obtaining the approximate
    89  * surface area therefrom.
    90  *
    91  * @return surface area of the Shape
    92  */
    93 double Shape::getSurfaceArea() const
    94 {
    95         const double surfacearea = impl->getSurfaceArea();
    96         if (surfacearea != -1.) {
    97                 return surfacearea;
    98         } else {
    99                 ApproximateShapeArea Approximator(*this);
    100                 return Approximator();
    101         }
    102 }
    103 
    104 LineSegmentSet Shape::getLineIntersections(const Line &line) const{
     50LineSegmentSet Shape::getLineIntersections(const Line &line){
    10551  return impl->getLineIntersections(line);
    10652}
     
    10854std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
    10955  return impl->getHomogeneousPointsOnSurface(N);
    110 }
    111 
    112 std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {
    113         return impl->getHomogeneousPointsInVolume(N);
    11456}
    11557
     
    12567}
    12668
    127 bool Shape::operator==(const Shape &rhs) const{
    128         return (this->getType() == rhs.getType());
    129 }
    130 
    13169std::string Shape::toString() const{
    13270  return impl->toString();
    133 }
    134 
    135 enum ShapeType Shape::getType() const{
    136         return impl->getType();
    13771}
    13872
     
    168102AndShape_impl::~AndShape_impl(){}
    169103
    170 bool AndShape_impl::isInside(const Vector &point) const{
     104bool AndShape_impl::isInside(const Vector &point){
    171105  return lhs->isInside(point) && rhs->isInside(point);
    172106}
    173107
    174 bool AndShape_impl::isOnSurface(const Vector &point) const{
     108bool AndShape_impl::isOnSurface(const Vector &point){
    175109  // check the number of surfaces that this point is on
    176110  int surfaces =0;
     
    204138}
    205139
    206 Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
     140Vector AndShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
    207141  Vector res;
    208142  if(!isOnSurface(point)){
     
    215149}
    216150
    217 Vector AndShape_impl::getCenter() const
    218 {
    219   // calculate closest position on sphere surface to other center ..
    220   const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
    221   const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
    222   // .. and then it's right in between those two
    223   return 0.5*(rhsDistance + lhsDistance);
    224 }
    225 
    226 double AndShape_impl::getRadius() const
    227 {
    228   const double distance = (lhs->getCenter() - rhs->getCenter()).Norm();
    229   const double minradii = std::min(lhs->getRadius(), rhs->getRadius());
    230   // if no intersection
    231   if (distance > (lhs->getRadius() + rhs->getRadius()))
    232     return 0.;
    233   else // if intersection it can only be the smaller one
    234     return minradii;
    235 }
    236 
    237 double AndShape_impl::getVolume() const
    238 {
    239         // TODO
    240         return -1.;
    241 }
    242 
    243 double AndShape_impl::getSurfaceArea() const
    244 {
    245         // TODO
    246         return -1.;
    247 }
    248 
    249 LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
     151LineSegmentSet AndShape_impl::getLineIntersections(const Line &line){
    250152  return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
    251153}
    252154
    253 std::string AndShape_impl::toString() const{
     155std::string AndShape_impl::toString() {
    254156  return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
    255 }
    256 
    257 enum ShapeType AndShape_impl::getType() const{
    258         return CombinedType;
    259157}
    260158
     
    276174}
    277175
    278 std::vector<Vector> AndShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
    279         ASSERT(0,
    280                         "AndShape_impl::getHomogeneousPointsInVolume() - not implemented.");
    281         return std::vector<Vector>();
    282 }
    283 
    284176
    285177Shape operator&&(const Shape &lhs,const Shape &rhs){
     
    296188OrShape_impl::~OrShape_impl(){}
    297189
    298 bool OrShape_impl::isInside(const Vector &point) const{
     190bool OrShape_impl::isInside(const Vector &point){
    299191  return rhs->isInside(point) || lhs->isInside(point);
    300192}
    301193
    302 bool OrShape_impl::isOnSurface(const Vector &point) const{
     194bool OrShape_impl::isOnSurface(const Vector &point){
    303195  // check the number of surfaces that this point is on
    304196  int surfaces =0;
     
    332224}
    333225
    334 Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
     226Vector OrShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
    335227  Vector res;
    336228  if(!isOnSurface(point)){
     
    343235}
    344236
    345 Vector OrShape_impl::getCenter() const
    346 {
    347   // calculate furthest position on sphere surface to other center ..
    348   const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
    349   const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
    350   // .. and then it's right in between those two
    351   return .5*(rhsDistance + lhsDistance);
    352 }
    353 
    354 double OrShape_impl::getRadius() const
    355 {
    356   const Vector rhsDistance = rhs->getCenter() + rhs->getRadius()*((rhs->getCenter() - lhs->getCenter()).getNormalized());
    357   const Vector lhsDistance = lhs->getCenter() + lhs->getRadius()*((lhs->getCenter() - rhs->getCenter()).getNormalized());
    358   return .5*(rhsDistance - lhsDistance).Norm();
    359 }
    360 
    361 double OrShape_impl::getVolume() const
    362 {
    363         // TODO
    364         return -1.;
    365 }
    366 
    367 double OrShape_impl::getSurfaceArea() const
    368 {
    369         // TODO
    370         return -1.;
    371 }
    372 
    373 LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
     237LineSegmentSet OrShape_impl::getLineIntersections(const Line &line){
    374238  return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
    375239}
    376240
    377 std::string OrShape_impl::toString() const{
     241std::string OrShape_impl::toString() {
    378242  return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
    379 }
    380 
    381 enum ShapeType OrShape_impl::getType() const{
    382         return CombinedType;
    383243}
    384244
     
    400260}
    401261
    402 std::vector<Vector> OrShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
    403         ASSERT(0,
    404                         "OrShape_impl::getHomogeneousPointsInVolume() - not implemented.");
    405         return std::vector<Vector>();
    406 }
    407 
    408262Shape operator||(const Shape &lhs,const Shape &rhs){
    409263  Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
     
    419273NotShape_impl::~NotShape_impl(){}
    420274
    421 bool NotShape_impl::isInside(const Vector &point) const{
     275bool NotShape_impl::isInside(const Vector &point){
    422276  return !arg->isInside(point);
    423277}
    424278
    425 bool NotShape_impl::isOnSurface(const Vector &point) const{
     279bool NotShape_impl::isOnSurface(const Vector &point){
    426280  return arg->isOnSurface(point);
    427281}
    428282
    429 Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
     283Vector NotShape_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
    430284  return -1.*arg->getNormal(point);
    431285}
    432286
    433 Vector NotShape_impl::getCenter() const
    434 {
    435   return arg->getCenter();
    436 }
    437 
    438 double NotShape_impl::getRadius() const
    439 {
    440   return std::numeric_limits<double>::infinity();
    441 }
    442 
    443 double NotShape_impl::getVolume() const
    444 {
    445         // TODO
    446         return -1.; //-arg->getVolume();
    447 }
    448 
    449 double NotShape_impl::getSurfaceArea() const
    450 {
    451         // TODO
    452         return -1.; // -arg->getSurfaceArea();
    453 }
    454 
    455 LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
     287LineSegmentSet NotShape_impl::getLineIntersections(const Line &line){
    456288  return invert(arg->getLineIntersections(line));
    457289}
    458290
    459 std::string NotShape_impl::toString() const{
     291std::string NotShape_impl::toString(){
    460292  return std::string("!") + arg->toString();
    461 }
    462 
    463 enum ShapeType NotShape_impl::getType() const{
    464         return CombinedType;
    465293}
    466294
     
    468296  // surfaces are the same, only normal direction is different
    469297  return arg->getHomogeneousPointsOnSurface(N);
    470 }
    471 
    472 std::vector<Vector> NotShape_impl::getHomogeneousPointsInVolume(const size_t N) const {
    473         ASSERT(0,
    474                         "NotShape_impl::getHomogeneousPointsInVolume() - not implemented.");
    475         return std::vector<Vector>();
    476298}
    477299
Note: See TracChangeset for help on using the changeset viewer.