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 | #include "Helpers/Assert.hpp"
|
---|
12 |
|
---|
13 | Shape::Shape(const Shape& src) :
|
---|
14 | impl(src.getImpl())
|
---|
15 | {}
|
---|
16 |
|
---|
17 | Shape::~Shape(){}
|
---|
18 |
|
---|
19 | bool Shape::isInside(const Vector &point) const{
|
---|
20 | return impl->isInside(point);
|
---|
21 | }
|
---|
22 |
|
---|
23 | bool Shape::isOnSurface(const Vector &point) const{
|
---|
24 | return impl->isOnSurface(point);
|
---|
25 | }
|
---|
26 |
|
---|
27 | Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
---|
28 | return impl->getNormal(point);
|
---|
29 | }
|
---|
30 |
|
---|
31 | Shape::Shape(Shape::impl_ptr _impl) :
|
---|
32 | impl(_impl)
|
---|
33 | {}
|
---|
34 |
|
---|
35 | Shape &Shape::operator=(const Shape& rhs){
|
---|
36 | if(&rhs!=this){
|
---|
37 | impl=rhs.getImpl();
|
---|
38 | }
|
---|
39 | return *this;
|
---|
40 | }
|
---|
41 |
|
---|
42 | Shape::impl_ptr Shape::getImpl() const{
|
---|
43 | return impl;
|
---|
44 | }
|
---|
45 |
|
---|
46 | // allows arbitrary friendship, but only if implementation is known
|
---|
47 | Shape::impl_ptr getShapeImpl(const Shape &shape){
|
---|
48 | return shape.getImpl();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /***************************** Some simple Shapes ***************************/
|
---|
52 |
|
---|
53 | Shape Everywhere(){
|
---|
54 | static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
|
---|
55 | return Shape(impl);
|
---|
56 | }
|
---|
57 |
|
---|
58 | Shape Nowhere(){
|
---|
59 | static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
|
---|
60 | return Shape(impl);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /****************************** Operators ***********************************/
|
---|
64 |
|
---|
65 | // AND
|
---|
66 |
|
---|
67 | AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
---|
68 | lhs(_lhs),rhs(_rhs)
|
---|
69 | {}
|
---|
70 |
|
---|
71 | AndShape_impl::~AndShape_impl(){}
|
---|
72 |
|
---|
73 | bool AndShape_impl::isInside(const Vector &point){
|
---|
74 | return lhs->isInside(point) && rhs->isInside(point);
|
---|
75 | }
|
---|
76 |
|
---|
77 | bool AndShape_impl::isOnSurface(const Vector &point){
|
---|
78 | // check the number of surfaces that this point is on
|
---|
79 | int surfaces =0;
|
---|
80 | surfaces += lhs->isOnSurface(point);
|
---|
81 | surfaces += rhs->isOnSurface(point);
|
---|
82 |
|
---|
83 | switch(surfaces){
|
---|
84 | case 0:
|
---|
85 | return false;
|
---|
86 | // no break necessary
|
---|
87 | case 1:
|
---|
88 | // if it is inside for the object where it does not lie on
|
---|
89 | // the surface the whole point lies inside
|
---|
90 | return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
|
---|
91 | (rhs->isOnSurface(point) && lhs->isInside(point));
|
---|
92 | // no break necessary
|
---|
93 | case 2:
|
---|
94 | {
|
---|
95 | // it lies on both Shapes... could be an edge or an inner point
|
---|
96 | // test the direction of the normals
|
---|
97 | Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
|
---|
98 | // if the directions are opposite we lie on the inside
|
---|
99 | return !direction.IsZero();
|
---|
100 | }
|
---|
101 | // no break necessary
|
---|
102 | default:
|
---|
103 | // if this happens there is something wrong
|
---|
104 | ASSERT(0,"Default case should have never been used");
|
---|
105 | }
|
---|
106 | return false; // never reached
|
---|
107 | }
|
---|
108 |
|
---|
109 | Vector AndShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
|
---|
110 | Vector res;
|
---|
111 | if(!isOnSurface(point)){
|
---|
112 | throw NotOnSurfaceException(__FILE__,__LINE__);
|
---|
113 | }
|
---|
114 | res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
|
---|
115 | res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
|
---|
116 | res.Normalize();
|
---|
117 | return res;
|
---|
118 | }
|
---|
119 |
|
---|
120 | Shape operator&&(const Shape &lhs,const Shape &rhs){
|
---|
121 | Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
---|
122 | return Shape(newImpl);
|
---|
123 | }
|
---|
124 |
|
---|
125 | // OR
|
---|
126 |
|
---|
127 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
---|
128 | lhs(_lhs),rhs(_rhs)
|
---|
129 | {}
|
---|
130 |
|
---|
131 | OrShape_impl::~OrShape_impl(){}
|
---|
132 |
|
---|
133 | bool OrShape_impl::isInside(const Vector &point){
|
---|
134 | return rhs->isInside(point) || lhs->isInside(point);
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool OrShape_impl::isOnSurface(const Vector &point){
|
---|
138 | // check the number of surfaces that this point is on
|
---|
139 | int surfaces =0;
|
---|
140 | surfaces += lhs->isOnSurface(point);
|
---|
141 | surfaces += rhs->isOnSurface(point);
|
---|
142 |
|
---|
143 | switch(surfaces){
|
---|
144 | case 0:
|
---|
145 | return false;
|
---|
146 | // no break necessary
|
---|
147 | case 1:
|
---|
148 | // if it is inside for the object where it does not lie on
|
---|
149 | // the surface the whole point lies inside
|
---|
150 | return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
|
---|
151 | (rhs->isOnSurface(point) && !lhs->isInside(point));
|
---|
152 | // no break necessary
|
---|
153 | case 2:
|
---|
154 | {
|
---|
155 | // it lies on both Shapes... could be an edge or an inner point
|
---|
156 | // test the direction of the normals
|
---|
157 | Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
|
---|
158 | // if the directions are opposite we lie on the inside
|
---|
159 | return !direction.IsZero();
|
---|
160 | }
|
---|
161 | // no break necessary
|
---|
162 | default:
|
---|
163 | // if this happens there is something wrong
|
---|
164 | ASSERT(0,"Default case should have never been used");
|
---|
165 | }
|
---|
166 | return false; // never reached
|
---|
167 | }
|
---|
168 |
|
---|
169 | Vector OrShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
|
---|
170 | Vector res;
|
---|
171 | if(!isOnSurface(point)){
|
---|
172 | throw NotOnSurfaceException(__FILE__,__LINE__);
|
---|
173 | }
|
---|
174 | res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
|
---|
175 | res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
|
---|
176 | res.Normalize();
|
---|
177 | return res;
|
---|
178 | }
|
---|
179 |
|
---|
180 | Shape operator||(const Shape &lhs,const Shape &rhs){
|
---|
181 | Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
---|
182 | return Shape(newImpl);
|
---|
183 | }
|
---|
184 |
|
---|
185 | // NOT
|
---|
186 |
|
---|
187 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
|
---|
188 | arg(_arg)
|
---|
189 | {}
|
---|
190 |
|
---|
191 | NotShape_impl::~NotShape_impl(){}
|
---|
192 |
|
---|
193 | bool NotShape_impl::isInside(const Vector &point){
|
---|
194 | return !arg->isInside(point);
|
---|
195 | }
|
---|
196 |
|
---|
197 | bool NotShape_impl::isOnSurface(const Vector &point){
|
---|
198 | return arg->isOnSurface(point);
|
---|
199 | }
|
---|
200 |
|
---|
201 | Vector NotShape_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
|
---|
202 | return -1*arg->getNormal(point);
|
---|
203 | }
|
---|
204 |
|
---|
205 | Shape operator!(const Shape &arg){
|
---|
206 | Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
|
---|
207 | return Shape(newImpl);
|
---|
208 | }
|
---|