| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /*
|
|---|
| 9 | * Shape.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Jun 18, 2010
|
|---|
| 12 | * Author: crueger
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include "CodePatterns/Assert.hpp"
|
|---|
| 23 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 24 |
|
|---|
| 25 | #include "Shapes/Shape.hpp"
|
|---|
| 26 | #include "Shapes/Shape_impl.hpp"
|
|---|
| 27 | #include "Shapes/ShapeExceptions.hpp"
|
|---|
| 28 | #include "Shapes/ShapeType.hpp"
|
|---|
| 29 |
|
|---|
| 30 | #include <string>
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | Shape::Shape(const Shape& src) :
|
|---|
| 34 | impl(src.getImpl())
|
|---|
| 35 | {}
|
|---|
| 36 |
|
|---|
| 37 | Shape::~Shape(){}
|
|---|
| 38 |
|
|---|
| 39 | bool Shape::isInside(const Vector &point) const{
|
|---|
| 40 | return impl->isInside(point);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | bool Shape::isOnSurface(const Vector &point) const{
|
|---|
| 44 | return impl->isOnSurface(point);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | Vector Shape::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
|---|
| 48 | return impl->getNormal(point);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | LineSegmentSet Shape::getLineIntersections(const Line &line) const{
|
|---|
| 52 | return impl->getLineIntersections(line);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 56 | return impl->getHomogeneousPointsOnSurface(N);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | Shape::Shape(Shape::impl_ptr _impl) :
|
|---|
| 60 | impl(_impl)
|
|---|
| 61 | {}
|
|---|
| 62 |
|
|---|
| 63 | Shape &Shape::operator=(const Shape& rhs){
|
|---|
| 64 | if(&rhs!=this){
|
|---|
| 65 | impl=rhs.getImpl();
|
|---|
| 66 | }
|
|---|
| 67 | return *this;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | bool Shape::operator==(const Shape &rhs) const{
|
|---|
| 71 | return (this->getType() == rhs.getType());
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | std::string Shape::toString() const{
|
|---|
| 75 | return impl->toString();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | enum ShapeType Shape::getType() const{
|
|---|
| 79 | return impl->getType();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | Shape::impl_ptr Shape::getImpl() const{
|
|---|
| 83 | return impl;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | // allows arbitrary friendship, but only if implementation is known
|
|---|
| 87 | Shape::impl_ptr getShapeImpl(const Shape &shape){
|
|---|
| 88 | return shape.getImpl();
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /***************************** Some simple Shapes ***************************/
|
|---|
| 92 |
|
|---|
| 93 | Shape Everywhere(){
|
|---|
| 94 | static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
|
|---|
| 95 | return Shape(impl);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | Shape Nowhere(){
|
|---|
| 99 | static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
|
|---|
| 100 | return Shape(impl);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /****************************** Operators ***********************************/
|
|---|
| 104 |
|
|---|
| 105 | // AND
|
|---|
| 106 |
|
|---|
| 107 | AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
|---|
| 108 | lhs(_lhs),rhs(_rhs)
|
|---|
| 109 | {}
|
|---|
| 110 |
|
|---|
| 111 | AndShape_impl::~AndShape_impl(){}
|
|---|
| 112 |
|
|---|
| 113 | bool AndShape_impl::isInside(const Vector &point) const{
|
|---|
| 114 | return lhs->isInside(point) && rhs->isInside(point);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | bool AndShape_impl::isOnSurface(const Vector &point) const{
|
|---|
| 118 | // check the number of surfaces that this point is on
|
|---|
| 119 | int surfaces =0;
|
|---|
| 120 | surfaces += lhs->isOnSurface(point);
|
|---|
| 121 | surfaces += rhs->isOnSurface(point);
|
|---|
| 122 |
|
|---|
| 123 | switch(surfaces){
|
|---|
| 124 | case 0:
|
|---|
| 125 | return false;
|
|---|
| 126 | // no break necessary
|
|---|
| 127 | case 1:
|
|---|
| 128 | // if it is inside for the object where it does not lie on
|
|---|
| 129 | // the surface the whole point lies inside
|
|---|
| 130 | return (lhs->isOnSurface(point) && rhs->isInside(point)) ||
|
|---|
| 131 | (rhs->isOnSurface(point) && lhs->isInside(point));
|
|---|
| 132 | // no break necessary
|
|---|
| 133 | case 2:
|
|---|
| 134 | {
|
|---|
| 135 | // it lies on both Shapes... could be an edge or an inner point
|
|---|
| 136 | // test the direction of the normals
|
|---|
| 137 | Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
|
|---|
| 138 | // if the directions are opposite we lie on the inside
|
|---|
| 139 | return !direction.IsZero();
|
|---|
| 140 | }
|
|---|
| 141 | // no break necessary
|
|---|
| 142 | default:
|
|---|
| 143 | // if this happens there is something wrong
|
|---|
| 144 | ASSERT(0,"Default case should have never been used");
|
|---|
| 145 | }
|
|---|
| 146 | return false; // never reached
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | Vector AndShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
|---|
| 150 | Vector res;
|
|---|
| 151 | if(!isOnSurface(point)){
|
|---|
| 152 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 153 | }
|
|---|
| 154 | res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
|
|---|
| 155 | res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
|
|---|
| 156 | res.Normalize();
|
|---|
| 157 | return res;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | LineSegmentSet AndShape_impl::getLineIntersections(const Line &line) const{
|
|---|
| 161 | return intersect(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | std::string AndShape_impl::toString() const{
|
|---|
| 165 | return std::string("(") + lhs->toString() + std::string("&&") + rhs->toString() + std::string(")");
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | enum ShapeType AndShape_impl::getType() const{
|
|---|
| 169 | return CombinedType;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 173 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 174 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 175 | std::vector<Vector> PointsOnSurface;
|
|---|
| 176 |
|
|---|
| 177 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
|
|---|
| 178 | if (rhs->isInside(*iter))
|
|---|
| 179 | PointsOnSurface.push_back(*iter);
|
|---|
| 180 | }
|
|---|
| 181 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
|
|---|
| 182 | if (lhs->isInside(*iter))
|
|---|
| 183 | PointsOnSurface.push_back(*iter);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | return PointsOnSurface;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | Shape operator&&(const Shape &lhs,const Shape &rhs){
|
|---|
| 191 | Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
|---|
| 192 | return Shape(newImpl);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | // OR
|
|---|
| 196 |
|
|---|
| 197 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
|---|
| 198 | lhs(_lhs),rhs(_rhs)
|
|---|
| 199 | {}
|
|---|
| 200 |
|
|---|
| 201 | OrShape_impl::~OrShape_impl(){}
|
|---|
| 202 |
|
|---|
| 203 | bool OrShape_impl::isInside(const Vector &point) const{
|
|---|
| 204 | return rhs->isInside(point) || lhs->isInside(point);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | bool OrShape_impl::isOnSurface(const Vector &point) const{
|
|---|
| 208 | // check the number of surfaces that this point is on
|
|---|
| 209 | int surfaces =0;
|
|---|
| 210 | surfaces += lhs->isOnSurface(point);
|
|---|
| 211 | surfaces += rhs->isOnSurface(point);
|
|---|
| 212 |
|
|---|
| 213 | switch(surfaces){
|
|---|
| 214 | case 0:
|
|---|
| 215 | return false;
|
|---|
| 216 | // no break necessary
|
|---|
| 217 | case 1:
|
|---|
| 218 | // if it is inside for the object where it does not lie on
|
|---|
| 219 | // the surface the whole point lies inside
|
|---|
| 220 | return (lhs->isOnSurface(point) && !rhs->isInside(point)) ||
|
|---|
| 221 | (rhs->isOnSurface(point) && !lhs->isInside(point));
|
|---|
| 222 | // no break necessary
|
|---|
| 223 | case 2:
|
|---|
| 224 | {
|
|---|
| 225 | // it lies on both Shapes... could be an edge or an inner point
|
|---|
| 226 | // test the direction of the normals
|
|---|
| 227 | Vector direction=lhs->getNormal(point)+rhs->getNormal(point);
|
|---|
| 228 | // if the directions are opposite we lie on the inside
|
|---|
| 229 | return !direction.IsZero();
|
|---|
| 230 | }
|
|---|
| 231 | // no break necessary
|
|---|
| 232 | default:
|
|---|
| 233 | // if this happens there is something wrong
|
|---|
| 234 | ASSERT(0,"Default case should have never been used");
|
|---|
| 235 | }
|
|---|
| 236 | return false; // never reached
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | Vector OrShape_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
|---|
| 240 | Vector res;
|
|---|
| 241 | if(!isOnSurface(point)){
|
|---|
| 242 | throw NotOnSurfaceException() << ShapeVector(&point);
|
|---|
| 243 | }
|
|---|
| 244 | res += lhs->isOnSurface(point)?lhs->getNormal(point):zeroVec;
|
|---|
| 245 | res += rhs->isOnSurface(point)?rhs->getNormal(point):zeroVec;
|
|---|
| 246 | res.Normalize();
|
|---|
| 247 | return res;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | LineSegmentSet OrShape_impl::getLineIntersections(const Line &line) const{
|
|---|
| 251 | return merge(lhs->getLineIntersections(line),rhs->getLineIntersections(line));
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | std::string OrShape_impl::toString() const{
|
|---|
| 255 | return std::string("(") + lhs->toString() + std::string("||") + rhs->toString() + std::string(")");
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | enum ShapeType OrShape_impl::getType() const{
|
|---|
| 259 | return CombinedType;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 263 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 264 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 265 | std::vector<Vector> PointsOnSurface;
|
|---|
| 266 |
|
|---|
| 267 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
|
|---|
| 268 | if (!rhs->isInside(*iter))
|
|---|
| 269 | PointsOnSurface.push_back(*iter);
|
|---|
| 270 | }
|
|---|
| 271 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
|
|---|
| 272 | if (!lhs->isInside(*iter))
|
|---|
| 273 | PointsOnSurface.push_back(*iter);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | return PointsOnSurface;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | Shape operator||(const Shape &lhs,const Shape &rhs){
|
|---|
| 280 | Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
|---|
| 281 | return Shape(newImpl);
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | // NOT
|
|---|
| 285 |
|
|---|
| 286 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
|
|---|
| 287 | arg(_arg)
|
|---|
| 288 | {}
|
|---|
| 289 |
|
|---|
| 290 | NotShape_impl::~NotShape_impl(){}
|
|---|
| 291 |
|
|---|
| 292 | bool NotShape_impl::isInside(const Vector &point) const{
|
|---|
| 293 | return !arg->isInside(point);
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | bool NotShape_impl::isOnSurface(const Vector &point) const{
|
|---|
| 297 | return arg->isOnSurface(point);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | Vector NotShape_impl::getNormal(const Vector &point) const throw(NotOnSurfaceException){
|
|---|
| 301 | return -1.*arg->getNormal(point);
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | LineSegmentSet NotShape_impl::getLineIntersections(const Line &line) const{
|
|---|
| 305 | return invert(arg->getLineIntersections(line));
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | std::string NotShape_impl::toString() const{
|
|---|
| 309 | return std::string("!") + arg->toString();
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | enum ShapeType NotShape_impl::getType() const{
|
|---|
| 313 | return CombinedType;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 317 | // surfaces are the same, only normal direction is different
|
|---|
| 318 | return arg->getHomogeneousPointsOnSurface(N);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | Shape operator!(const Shape &arg){
|
|---|
| 322 | Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
|
|---|
| 323 | return Shape(newImpl);
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /**************** global operations *********************************/
|
|---|
| 327 | std::ostream &operator<<(std::ostream &ost,const Shape &shape){
|
|---|
| 328 | ost << shape.toString();
|
|---|
| 329 | return ost;
|
|---|
| 330 | }
|
|---|