Changes in src/Shapes/Shape.cpp [da1e92:955b91]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Shapes/Shape.cpp
rda1e92 r955b91 26 26 #include "Shapes/Shape_impl.hpp" 27 27 #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 35 29 #include <string> 36 30 … … 54 48 } 55 49 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{ 50 LineSegmentSet Shape::getLineIntersections(const Line &line){ 105 51 return impl->getLineIntersections(line); 106 52 } … … 108 54 std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const { 109 55 return impl->getHomogeneousPointsOnSurface(N); 110 }111 112 std::vector<Vector> Shape::getHomogeneousPointsInVolume(const size_t N) const {113 return impl->getHomogeneousPointsInVolume(N);114 56 } 115 57 … … 125 67 } 126 68 127 bool Shape::operator==(const Shape &rhs) const{128 return (this->getType() == rhs.getType());129 }130 131 69 std::string Shape::toString() const{ 132 70 return impl->toString(); 133 }134 135 enum ShapeType Shape::getType() const{136 return impl->getType();137 71 } 138 72 … … 168 102 AndShape_impl::~AndShape_impl(){} 169 103 170 bool AndShape_impl::isInside(const Vector &point) const{104 bool AndShape_impl::isInside(const Vector &point){ 171 105 return lhs->isInside(point) && rhs->isInside(point); 172 106 } 173 107 174 bool AndShape_impl::isOnSurface(const Vector &point) const{108 bool AndShape_impl::isOnSurface(const Vector &point){ 175 109 // check the number of surfaces that this point is on 176 110 int surfaces =0; … … 204 138 } 205 139 206 Vector AndShape_impl::getNormal(const Vector &point) constthrow (NotOnSurfaceException){140 Vector AndShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){ 207 141 Vector res; 208 142 if(!isOnSurface(point)){ … … 215 149 } 216 150 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{ 151 LineSegmentSet AndShape_impl::getLineIntersections(const Line &line){ 250 152 return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line)); 251 153 } 252 154 253 std::string AndShape_impl::toString() const{155 std::string AndShape_impl::toString() { 254 156 return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")"); 255 }256 257 enum ShapeType AndShape_impl::getType() const{258 return CombinedType;259 157 } 260 158 … … 276 174 } 277 175 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 284 176 285 177 Shape operator&&(const Shape &lhs,const Shape &rhs){ … … 296 188 OrShape_impl::~OrShape_impl(){} 297 189 298 bool OrShape_impl::isInside(const Vector &point) const{190 bool OrShape_impl::isInside(const Vector &point){ 299 191 return rhs->isInside(point) || lhs->isInside(point); 300 192 } 301 193 302 bool OrShape_impl::isOnSurface(const Vector &point) const{194 bool OrShape_impl::isOnSurface(const Vector &point){ 303 195 // check the number of surfaces that this point is on 304 196 int surfaces =0; … … 332 224 } 333 225 334 Vector OrShape_impl::getNormal(const Vector &point) constthrow (NotOnSurfaceException){226 Vector OrShape_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){ 335 227 Vector res; 336 228 if(!isOnSurface(point)){ … … 343 235 } 344 236 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{ 237 LineSegmentSet OrShape_impl::getLineIntersections(const Line &line){ 374 238 return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line)); 375 239 } 376 240 377 std::string OrShape_impl::toString() const{241 std::string OrShape_impl::toString() { 378 242 return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")"); 379 }380 381 enum ShapeType OrShape_impl::getType() const{382 return CombinedType;383 243 } 384 244 … … 400 260 } 401 261 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 408 262 Shape operator||(const Shape &lhs,const Shape &rhs){ 409 263 Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs))); … … 419 273 NotShape_impl::~NotShape_impl(){} 420 274 421 bool NotShape_impl::isInside(const Vector &point) const{275 bool NotShape_impl::isInside(const Vector &point){ 422 276 return !arg->isInside(point); 423 277 } 424 278 425 bool NotShape_impl::isOnSurface(const Vector &point) const{279 bool NotShape_impl::isOnSurface(const Vector &point){ 426 280 return arg->isOnSurface(point); 427 281 } 428 282 429 Vector NotShape_impl::getNormal(const Vector &point) constthrow(NotOnSurfaceException){283 Vector NotShape_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){ 430 284 return -1.*arg->getNormal(point); 431 285 } 432 286 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{ 287 LineSegmentSet NotShape_impl::getLineIntersections(const Line &line){ 456 288 return invert(arg->getLineIntersections(line)); 457 289 } 458 290 459 std::string NotShape_impl::toString() const{291 std::string NotShape_impl::toString(){ 460 292 return std::string("!") + arg->toString(); 461 }462 463 enum ShapeType NotShape_impl::getType() const{464 return CombinedType;465 293 } 466 294 … … 468 296 // surfaces are the same, only normal direction is different 469 297 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>();476 298 } 477 299
Note:
See TracChangeset
for help on using the changeset viewer.