| 1 | /*
 | 
|---|
| 2 |  * Shape.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jun 18, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | // include config.h
 | 
|---|
| 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 10 | #include <config.h>
 | 
|---|
| 11 | #endif
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include "Shape.hpp"
 | 
|---|
| 16 | #include "Shape_impl.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "Helpers/Assert.hpp"
 | 
|---|
| 20 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | Shape::Shape(const Shape& src) :
 | 
|---|
| 23 |   impl(src.getImpl())
 | 
|---|
| 24 | {}
 | 
|---|
| 25 | 
 | 
|---|
| 26 | Shape::~Shape(){}
 | 
|---|
| 27 | 
 | 
|---|
| 28 | bool Shape::isInside(const Vector &point) const{
 | 
|---|
| 29 |   return impl->isInside(point);
 | 
|---|
| 30 | }
 | 
|---|
| 31 | 
 | 
|---|
| 32 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const int N) const {
 | 
|---|
| 33 |   return impl->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | Shape::Shape(Shape::impl_ptr _impl) :
 | 
|---|
| 37 |     impl(_impl)
 | 
|---|
| 38 | {}
 | 
|---|
| 39 | 
 | 
|---|
| 40 | Shape &Shape::operator=(const Shape& rhs){
 | 
|---|
| 41 |   if(&rhs!=this){
 | 
|---|
| 42 |     impl=rhs.getImpl();
 | 
|---|
| 43 |   }
 | 
|---|
| 44 |   return *this;
 | 
|---|
| 45 | }
 | 
|---|
| 46 | 
 | 
|---|
| 47 | Shape::impl_ptr Shape::getImpl() const{
 | 
|---|
| 48 |   return impl;
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | // allows arbitrary friendship, but only if implementation is known
 | 
|---|
| 52 | Shape::impl_ptr getShapeImpl(const Shape &shape){
 | 
|---|
| 53 |   return shape.getImpl();
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /***************************** Some simple Shapes ***************************/
 | 
|---|
| 57 | 
 | 
|---|
| 58 | Shape Everywhere(){
 | 
|---|
| 59 |   static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
 | 
|---|
| 60 |   return Shape(impl);
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | Shape Nowhere(){
 | 
|---|
| 64 |   static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
 | 
|---|
| 65 |   return Shape(impl);
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | /****************************** Operators ***********************************/
 | 
|---|
| 69 | 
 | 
|---|
| 70 | // AND
 | 
|---|
| 71 | 
 | 
|---|
| 72 | AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
 | 
|---|
| 73 |   lhs(_lhs),rhs(_rhs)
 | 
|---|
| 74 | {}
 | 
|---|
| 75 | 
 | 
|---|
| 76 | AndShape_impl::~AndShape_impl(){}
 | 
|---|
| 77 | 
 | 
|---|
| 78 | bool AndShape_impl::isInside(const Vector &point){
 | 
|---|
| 79 |   return lhs->isInside(point) && rhs->isInside(point);
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const int N) const {
 | 
|---|
| 83 |   std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 84 |   std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 85 |   std::vector<Vector> PointsOnSurface;
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
 | 
|---|
| 88 |     if (rhs->isInside(*iter))
 | 
|---|
| 89 |       PointsOnSurface.push_back(*iter);
 | 
|---|
| 90 |   }
 | 
|---|
| 91 |   for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
 | 
|---|
| 92 |     if (lhs->isInside(*iter))
 | 
|---|
| 93 |       PointsOnSurface.push_back(*iter);
 | 
|---|
| 94 |   }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   return PointsOnSurface;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | 
 | 
|---|
| 100 | Shape operator&&(const Shape &lhs,const Shape &rhs){
 | 
|---|
| 101 |   Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
 | 
|---|
| 102 |   return Shape(newImpl);
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | // OR
 | 
|---|
| 106 | 
 | 
|---|
| 107 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
 | 
|---|
| 108 |   lhs(_lhs),rhs(_rhs)
 | 
|---|
| 109 | {}
 | 
|---|
| 110 | 
 | 
|---|
| 111 | OrShape_impl::~OrShape_impl(){}
 | 
|---|
| 112 | 
 | 
|---|
| 113 | bool OrShape_impl::isInside(const Vector &point){
 | 
|---|
| 114 |   return rhs->isInside(point) || lhs->isInside(point);
 | 
|---|
| 115 | }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const int N) const {
 | 
|---|
| 118 |   std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 119 |   std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 120 |   std::vector<Vector> PointsOnSurface;
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
 | 
|---|
| 123 |     if (!rhs->isInside(*iter))
 | 
|---|
| 124 |       PointsOnSurface.push_back(*iter);
 | 
|---|
| 125 |   }
 | 
|---|
| 126 |   for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
 | 
|---|
| 127 |     if (!lhs->isInside(*iter))
 | 
|---|
| 128 |       PointsOnSurface.push_back(*iter);
 | 
|---|
| 129 |   }
 | 
|---|
| 130 | 
 | 
|---|
| 131 |   return PointsOnSurface;
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | Shape operator||(const Shape &lhs,const Shape &rhs){
 | 
|---|
| 135 |   Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
 | 
|---|
| 136 |   return Shape(newImpl);
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | // NOT
 | 
|---|
| 140 | 
 | 
|---|
| 141 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
 | 
|---|
| 142 |   arg(_arg)
 | 
|---|
| 143 | {}
 | 
|---|
| 144 | 
 | 
|---|
| 145 | NotShape_impl::~NotShape_impl(){}
 | 
|---|
| 146 | 
 | 
|---|
| 147 | bool NotShape_impl::isInside(const Vector &point){
 | 
|---|
| 148 |   return !arg->isInside(point);
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const int N) const {
 | 
|---|
| 152 |   // surfaces are the same, only normal direction is different
 | 
|---|
| 153 |   return arg->getHomogeneousPointsOnSurface(N);
 | 
|---|
| 154 | }
 | 
|---|
| 155 | 
 | 
|---|
| 156 | Shape operator!(const Shape &arg){
 | 
|---|
| 157 |   Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
 | 
|---|
| 158 |   return Shape(newImpl);
 | 
|---|
| 159 | }
 | 
|---|