| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010 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 "Helpers/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include "Shape.hpp"
|
|---|
| 23 | #include "Shape_impl.hpp"
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | #include "Helpers/Assert.hpp"
|
|---|
| 27 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 28 |
|
|---|
| 29 | Shape::Shape(const Shape& src) :
|
|---|
| 30 | impl(src.getImpl())
|
|---|
| 31 | {}
|
|---|
| 32 |
|
|---|
| 33 | Shape::~Shape(){}
|
|---|
| 34 |
|
|---|
| 35 | bool Shape::isInside(const Vector &point) const{
|
|---|
| 36 | return impl->isInside(point);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | std::vector<Vector> Shape::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 40 | return impl->getHomogeneousPointsOnSurface(N);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | Shape::Shape(Shape::impl_ptr _impl) :
|
|---|
| 44 | impl(_impl)
|
|---|
| 45 | {}
|
|---|
| 46 |
|
|---|
| 47 | Shape &Shape::operator=(const Shape& rhs){
|
|---|
| 48 | if(&rhs!=this){
|
|---|
| 49 | impl=rhs.getImpl();
|
|---|
| 50 | }
|
|---|
| 51 | return *this;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | Shape::impl_ptr Shape::getImpl() const{
|
|---|
| 55 | return impl;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | // allows arbitrary friendship, but only if implementation is known
|
|---|
| 59 | Shape::impl_ptr getShapeImpl(const Shape &shape){
|
|---|
| 60 | return shape.getImpl();
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /***************************** Some simple Shapes ***************************/
|
|---|
| 64 |
|
|---|
| 65 | Shape Everywhere(){
|
|---|
| 66 | static Shape::impl_ptr impl = Shape::impl_ptr(new Everywhere_impl());
|
|---|
| 67 | return Shape(impl);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | Shape Nowhere(){
|
|---|
| 71 | static Shape::impl_ptr impl = Shape::impl_ptr(new Nowhere_impl());
|
|---|
| 72 | return Shape(impl);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /****************************** Operators ***********************************/
|
|---|
| 76 |
|
|---|
| 77 | // AND
|
|---|
| 78 |
|
|---|
| 79 | AndShape_impl::AndShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
|---|
| 80 | lhs(_lhs),rhs(_rhs)
|
|---|
| 81 | {}
|
|---|
| 82 |
|
|---|
| 83 | AndShape_impl::~AndShape_impl(){}
|
|---|
| 84 |
|
|---|
| 85 | bool AndShape_impl::isInside(const Vector &point){
|
|---|
| 86 | return lhs->isInside(point) && rhs->isInside(point);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | std::vector<Vector> AndShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 90 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 91 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 92 | std::vector<Vector> PointsOnSurface;
|
|---|
| 93 |
|
|---|
| 94 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
|
|---|
| 95 | if (rhs->isInside(*iter))
|
|---|
| 96 | PointsOnSurface.push_back(*iter);
|
|---|
| 97 | }
|
|---|
| 98 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
|
|---|
| 99 | if (lhs->isInside(*iter))
|
|---|
| 100 | PointsOnSurface.push_back(*iter);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return PointsOnSurface;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 | Shape operator&&(const Shape &lhs,const Shape &rhs){
|
|---|
| 108 | Shape::impl_ptr newImpl = Shape::impl_ptr(new AndShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
|---|
| 109 | return Shape(newImpl);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | // OR
|
|---|
| 113 |
|
|---|
| 114 | OrShape_impl::OrShape_impl(const Shape::impl_ptr &_lhs, const Shape::impl_ptr &_rhs) :
|
|---|
| 115 | lhs(_lhs),rhs(_rhs)
|
|---|
| 116 | {}
|
|---|
| 117 |
|
|---|
| 118 | OrShape_impl::~OrShape_impl(){}
|
|---|
| 119 |
|
|---|
| 120 | bool OrShape_impl::isInside(const Vector &point){
|
|---|
| 121 | return rhs->isInside(point) || lhs->isInside(point);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | std::vector<Vector> OrShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 125 | std::vector<Vector> PointsOnSurface_lhs = lhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 126 | std::vector<Vector> PointsOnSurface_rhs = rhs->getHomogeneousPointsOnSurface(N);
|
|---|
| 127 | std::vector<Vector> PointsOnSurface;
|
|---|
| 128 |
|
|---|
| 129 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_lhs.begin(); iter != PointsOnSurface_lhs.end(); ++iter) {
|
|---|
| 130 | if (!rhs->isInside(*iter))
|
|---|
| 131 | PointsOnSurface.push_back(*iter);
|
|---|
| 132 | }
|
|---|
| 133 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface_rhs.begin(); iter != PointsOnSurface_rhs.end(); ++iter) {
|
|---|
| 134 | if (!lhs->isInside(*iter))
|
|---|
| 135 | PointsOnSurface.push_back(*iter);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | return PointsOnSurface;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | Shape operator||(const Shape &lhs,const Shape &rhs){
|
|---|
| 142 | Shape::impl_ptr newImpl = Shape::impl_ptr(new OrShape_impl(getShapeImpl(lhs),getShapeImpl(rhs)));
|
|---|
| 143 | return Shape(newImpl);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | // NOT
|
|---|
| 147 |
|
|---|
| 148 | NotShape_impl::NotShape_impl(const Shape::impl_ptr &_arg) :
|
|---|
| 149 | arg(_arg)
|
|---|
| 150 | {}
|
|---|
| 151 |
|
|---|
| 152 | NotShape_impl::~NotShape_impl(){}
|
|---|
| 153 |
|
|---|
| 154 | bool NotShape_impl::isInside(const Vector &point){
|
|---|
| 155 | return !arg->isInside(point);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | std::vector<Vector> NotShape_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
|---|
| 159 | // surfaces are the same, only normal direction is different
|
|---|
| 160 | return arg->getHomogeneousPointsOnSurface(N);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | Shape operator!(const Shape &arg){
|
|---|
| 164 | Shape::impl_ptr newImpl = Shape::impl_ptr(new NotShape_impl(getShapeImpl(arg)));
|
|---|
| 165 | return Shape(newImpl);
|
|---|
| 166 | }
|
|---|