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 |
|
---|
11 |
|
---|
12 | #include "Helpers/Assert.hpp"
|
---|
13 | #include "LinearAlgebra/Vector.hpp"
|
---|
14 |
|
---|
15 | Shape::Shape(const Shape& src) :
|
---|
16 | impl(src.getImpl())
|
---|
17 | {}
|
---|
18 |
|
---|
19 | Shape::~Shape(){}
|
---|
20 |
|
---|
21 | bool Shape::isInside(const Vector &point) const{
|
---|
22 | return impl->isInside(point);
|
---|
23 | }
|
---|
24 |
|
---|
25 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const int N) const {
|
---|
26 | return impl->getHomogeneousPointsOnSurface(N);
|
---|
27 | }
|
---|
28 |
|
---|
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 |
|
---|
44 | // allows arbitrary friendship, but only if implementation is known
|
---|
45 | Shape::impl_ptr getShapeImpl(const Shape &shape){
|
---|
46 | return shape.getImpl();
|
---|
47 | }
|
---|
48 |
|
---|
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 |
|
---|
75 | std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const int N) const {
|
---|
76 | std::vector<Vector> PointsOnSurface;
|
---|
77 | ASSERT(false, "AndShape_impl::getHomogeneousPointsOnSurface() not implemented yet");
|
---|
78 | return PointsOnSurface;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | Shape operator&&(const Shape &lhs,const Shape &rhs){
|
---|
83 | Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
---|
84 | return Shape(newImpl);
|
---|
85 | }
|
---|
86 |
|
---|
87 | // OR
|
---|
88 |
|
---|
89 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
---|
90 | lhs(_lhs),rhs(_rhs)
|
---|
91 | {}
|
---|
92 |
|
---|
93 | OrShape_impl::~OrShape_impl(){}
|
---|
94 |
|
---|
95 | bool OrShape_impl::isInside(const Vector &point){
|
---|
96 | return rhs->isInside(point) || lhs->isInside(point);
|
---|
97 | }
|
---|
98 |
|
---|
99 | std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const int N) const {
|
---|
100 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
|
---|
101 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
|
---|
102 | std::vector<Vector> PointsOnSurface;
|
---|
103 |
|
---|
104 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
|
---|
105 | if (!rhs->isInside(*iter))
|
---|
106 | PointsOnSurface.push_back(*iter);
|
---|
107 | }
|
---|
108 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
|
---|
109 | if (!lhs->isInside(*iter))
|
---|
110 | PointsOnSurface.push_back(*iter);
|
---|
111 | }
|
---|
112 |
|
---|
113 | return PointsOnSurface;
|
---|
114 | }
|
---|
115 |
|
---|
116 | Shape operator||(const Shape &lhs,const Shape &rhs){
|
---|
117 | Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
---|
118 | return Shape(newImpl);
|
---|
119 | }
|
---|
120 |
|
---|
121 | // NOT
|
---|
122 |
|
---|
123 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
|
---|
124 | arg(_arg)
|
---|
125 | {}
|
---|
126 |
|
---|
127 | NotShape_impl::~NotShape_impl(){}
|
---|
128 |
|
---|
129 | bool NotShape_impl::isInside(const Vector &point){
|
---|
130 | return !arg->isInside(point);
|
---|
131 | }
|
---|
132 |
|
---|
133 | std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const int N) const {
|
---|
134 | // surfaces are the same, only normal direction is different
|
---|
135 | return arg->getHomogeneousPointsOnSurface(N);
|
---|
136 | }
|
---|
137 |
|
---|
138 | Shape operator!(const Shape &arg){
|
---|
139 | Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
|
---|
140 | return Shape(newImpl);
|
---|
141 | }
|
---|