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