| [e09b70] | 1 | /*
|
|---|
| 2 | * ShapeOps.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Jun 18, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include "Shapes/ShapeOps.hpp"
|
|---|
| 9 | #include "Shapes/ShapeOps_impl.hpp"
|
|---|
| 10 |
|
|---|
| [394529] | 11 | #include "Helpers/Assert.hpp"
|
|---|
| 12 |
|
|---|
| [5de9da] | 13 | /*************** Base case ***********************/
|
|---|
| 14 |
|
|---|
| 15 | ShapeOpsBase_impl::ShapeOpsBase_impl(const Shape::impl_ptr &_arg) :
|
|---|
| 16 | arg(_arg){}
|
|---|
| 17 |
|
|---|
| 18 | ShapeOpsBase_impl::~ShapeOpsBase_impl(){}
|
|---|
| 19 |
|
|---|
| 20 | bool ShapeOpsBase_impl::isInside(const Vector &point){
|
|---|
| 21 | return arg->isInside(translateIn(point));
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | bool ShapeOpsBase_impl::isOnSurface(const Vector &point){
|
|---|
| 25 | return arg->isOnSurface(translateIn(point));
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | Vector ShapeOpsBase_impl::getNormal(const Vector &point) throw (NotOnSurfaceException){
|
|---|
| 29 | Vector helper = translateIn(point);
|
|---|
| 30 | if(!arg->isOnSurface(helper)){
|
|---|
| 31 | throw NotOnSurfaceException(__FILE__,__LINE__);
|
|---|
| 32 | }
|
|---|
| 33 | return translateOutNormal(arg->getNormal(helper));
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| [cfda65] | 36 | Shape::impl_ptr ShapeOpsBase_impl::getArg(){
|
|---|
| 37 | return arg;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| [5e588b5] | 40 | /********************* Resize ********************/
|
|---|
| 41 |
|
|---|
| [e09b70] | 42 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
|
|---|
| [5de9da] | 43 | ShapeOpsBase_impl(_arg), size(_size)
|
|---|
| [394529] | 44 | {
|
|---|
| 45 | ASSERT(size>0,"Cannot resize a Shape to size zero or below");
|
|---|
| 46 | }
|
|---|
| [e09b70] | 47 |
|
|---|
| 48 | Resize_impl::~Resize_impl(){}
|
|---|
| 49 |
|
|---|
| [5de9da] | 50 | Vector Resize_impl::translateIn(const Vector& point){
|
|---|
| 51 | return (1/size) * point;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | Vector Resize_impl::translateOutPos(const Vector& point){
|
|---|
| 55 | return size * point;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | Vector Resize_impl::translateOutNormal(const Vector& point){
|
|---|
| 59 | return point;
|
|---|
| [e09b70] | 60 | }
|
|---|
| 61 |
|
|---|
| [cfda65] | 62 | string Resize_impl::toString(){
|
|---|
| 63 | stringstream sstr;
|
|---|
| 64 | sstr << "resize(" << getArg()->toString() << "," << size << ")";
|
|---|
| 65 | return sstr.str();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| [e09b70] | 68 | Shape resize(const Shape &arg,double size){
|
|---|
| 69 | Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
|
|---|
| 70 | return Shape(impl);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| [5e588b5] | 73 | /*************************** translate *******************/
|
|---|
| 74 |
|
|---|
| [e09b70] | 75 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
|---|
| [5de9da] | 76 | ShapeOpsBase_impl(_arg),offset(_offset)
|
|---|
| [e09b70] | 77 | {}
|
|---|
| 78 |
|
|---|
| 79 | Translate_impl::~Translate_impl(){}
|
|---|
| 80 |
|
|---|
| [5de9da] | 81 | Vector Translate_impl::translateIn(const Vector& point){
|
|---|
| 82 | return point-offset;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | Vector Translate_impl::translateOutPos(const Vector& point){
|
|---|
| 86 | return point+offset;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | Vector Translate_impl::translateOutNormal(const Vector& point){
|
|---|
| 90 | return point;
|
|---|
| [e09b70] | 91 | }
|
|---|
| 92 |
|
|---|
| [cfda65] | 93 | string Translate_impl::toString(){
|
|---|
| 94 | stringstream sstr;
|
|---|
| 95 | sstr << "translate(" << getArg()->toString() << "," << offset << ")";
|
|---|
| [79dd0e] | 96 | return sstr.str();
|
|---|
| [cfda65] | 97 | }
|
|---|
| 98 |
|
|---|
| [e09b70] | 99 | Shape translate(const Shape &arg, const Vector &offset){
|
|---|
| 100 | Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
|
|---|
| 101 | return Shape(impl);
|
|---|
| 102 | }
|
|---|
| [5e588b5] | 103 |
|
|---|
| 104 | /*********************** stretch ******************/
|
|---|
| 105 |
|
|---|
| 106 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
|---|
| [5de9da] | 107 | ShapeOpsBase_impl(_arg),factors(_factors)
|
|---|
| [5e588b5] | 108 | {
|
|---|
| 109 | ASSERT(factors[0]>0,"cannot stretch a shape by a negative amount");
|
|---|
| 110 | ASSERT(factors[1]>0,"cannot stretch a shape by a negative amount");
|
|---|
| 111 | ASSERT(factors[2]>0,"cannot stretch a shape by a negative amount");
|
|---|
| 112 | for(int i = NDIM;i--;){
|
|---|
| 113 | reciFactors[i] = 1/factors[i];
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | Stretch_impl::~Stretch_impl(){}
|
|---|
| 118 |
|
|---|
| [5de9da] | 119 | Vector Stretch_impl::translateIn(const Vector& point){
|
|---|
| [5e588b5] | 120 | Vector helper=point;
|
|---|
| 121 | helper.ScaleAll(reciFactors);
|
|---|
| [5de9da] | 122 | return helper;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | Vector Stretch_impl::translateOutPos(const Vector& point){
|
|---|
| 126 | Vector helper=point;
|
|---|
| 127 | helper.ScaleAll(factors);
|
|---|
| 128 | return helper;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | Vector Stretch_impl::translateOutNormal(const Vector& point){
|
|---|
| 132 | Vector helper=point;
|
|---|
| 133 | // the normalFactors are derived from appearances of the factors
|
|---|
| 134 | // with in the vectorproduct
|
|---|
| 135 | Vector normalFactors;
|
|---|
| 136 | normalFactors[0]=factors[1]*factors[2];
|
|---|
| 137 | normalFactors[1]=factors[0]*factors[2];
|
|---|
| 138 | normalFactors[2]=factors[0]*factors[1];
|
|---|
| 139 | helper.ScaleAll(normalFactors);
|
|---|
| 140 | return helper;
|
|---|
| [5e588b5] | 141 | }
|
|---|
| 142 |
|
|---|
| [cfda65] | 143 | string Stretch_impl::toString(){
|
|---|
| 144 | stringstream sstr;
|
|---|
| 145 | sstr << "stretch(" << getArg()->toString() << "," << factors << ")";
|
|---|
| 146 | return sstr.str();
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| [5e588b5] | 149 | Shape stretch(const Shape &arg, const Vector &factors){
|
|---|
| 150 | Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
|
|---|
| 151 | return Shape(impl);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /************************* transform *****************/
|
|---|
| 155 |
|
|---|
| 156 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const Matrix &_transformation) :
|
|---|
| [5de9da] | 157 | ShapeOpsBase_impl(_arg),transformation(_transformation)
|
|---|
| [5e588b5] | 158 | {
|
|---|
| 159 | transformationInv = transformation.invert();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | Transform_impl::~Transform_impl(){}
|
|---|
| 163 |
|
|---|
| [5de9da] | 164 | Vector Transform_impl::translateIn(const Vector& point){
|
|---|
| 165 | return transformationInv * point;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | Vector Transform_impl::translateOutPos(const Vector& point){
|
|---|
| 169 | return transformation * point;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | Vector Transform_impl::translateOutNormal(const Vector& point){
|
|---|
| 173 | Matrix mat = transformation.determinant() * transformation.invert().transpose();
|
|---|
| 174 | return mat * point;
|
|---|
| [5e588b5] | 175 | }
|
|---|
| 176 |
|
|---|
| [cfda65] | 177 | string Transform_impl::toString(){
|
|---|
| 178 | stringstream sstr;
|
|---|
| 179 | sstr << "transform(" << getArg()->toString() << "," << transformation << ")";
|
|---|
| 180 | return sstr.str();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| [5e588b5] | 183 | Shape transform(const Shape &arg, const Matrix &transformation){
|
|---|
| 184 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
|---|
| 185 | return Shape(impl);
|
|---|
| 186 | }
|
|---|