| [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 |
|
|---|
| [c6f395] | 36 | LineSegmentSet ShapeOpsBase_impl::getLineIntersections(const Line &line){
|
|---|
| 37 | Line newLine(translateIn(line.getOrigin()),translateIn(line.getDirection()));
|
|---|
| 38 | LineSegmentSet res(line);
|
|---|
| 39 | LineSegmentSet helper = getArg()->getLineIntersections(newLine);
|
|---|
| 40 | for(LineSegmentSet::iterator iter = helper.begin();iter!=helper.end();++iter){
|
|---|
| 41 | LinePoint lpBegin = iter->getBegin();
|
|---|
| 42 | LinePoint lpEnd = iter->getBegin();
|
|---|
| 43 | // translate both linepoints
|
|---|
| 44 | lpBegin = lpBegin.isNegInfinity()?
|
|---|
| 45 | line.negEndpoint():
|
|---|
| 46 | line.getLinePoint(translateOutPos(lpBegin.getPoint()));
|
|---|
| 47 | lpEnd = lpEnd.isPosInfinity()?
|
|---|
| 48 | line.posEndpoint():
|
|---|
| 49 | line.getLinePoint(translateOutPos(lpEnd.getPoint()));
|
|---|
| 50 | res.insert(LineSegment(lpBegin,lpEnd));
|
|---|
| 51 | }
|
|---|
| 52 | return res;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| [cfda65] | 55 | Shape::impl_ptr ShapeOpsBase_impl::getArg(){
|
|---|
| 56 | return arg;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| [5e588b5] | 59 | /********************* Resize ********************/
|
|---|
| 60 |
|
|---|
| [e09b70] | 61 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
|
|---|
| [5de9da] | 62 | ShapeOpsBase_impl(_arg), size(_size)
|
|---|
| [394529] | 63 | {
|
|---|
| 64 | ASSERT(size>0,"Cannot resize a Shape to size zero or below");
|
|---|
| 65 | }
|
|---|
| [e09b70] | 66 |
|
|---|
| 67 | Resize_impl::~Resize_impl(){}
|
|---|
| 68 |
|
|---|
| [5de9da] | 69 | Vector Resize_impl::translateIn(const Vector& point){
|
|---|
| 70 | return (1/size) * point;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | Vector Resize_impl::translateOutPos(const Vector& point){
|
|---|
| 74 | return size * point;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | Vector Resize_impl::translateOutNormal(const Vector& point){
|
|---|
| 78 | return point;
|
|---|
| [e09b70] | 79 | }
|
|---|
| 80 |
|
|---|
| [cfda65] | 81 | string Resize_impl::toString(){
|
|---|
| 82 | stringstream sstr;
|
|---|
| 83 | sstr << "resize(" << getArg()->toString() << "," << size << ")";
|
|---|
| 84 | return sstr.str();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| [e09b70] | 87 | Shape resize(const Shape &arg,double size){
|
|---|
| 88 | Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
|
|---|
| 89 | return Shape(impl);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| [5e588b5] | 92 | /*************************** translate *******************/
|
|---|
| 93 |
|
|---|
| [e09b70] | 94 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
|---|
| [5de9da] | 95 | ShapeOpsBase_impl(_arg),offset(_offset)
|
|---|
| [e09b70] | 96 | {}
|
|---|
| 97 |
|
|---|
| 98 | Translate_impl::~Translate_impl(){}
|
|---|
| 99 |
|
|---|
| [5de9da] | 100 | Vector Translate_impl::translateIn(const Vector& point){
|
|---|
| 101 | return point-offset;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | Vector Translate_impl::translateOutPos(const Vector& point){
|
|---|
| 105 | return point+offset;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | Vector Translate_impl::translateOutNormal(const Vector& point){
|
|---|
| 109 | return point;
|
|---|
| [e09b70] | 110 | }
|
|---|
| 111 |
|
|---|
| [cfda65] | 112 | string Translate_impl::toString(){
|
|---|
| 113 | stringstream sstr;
|
|---|
| 114 | sstr << "translate(" << getArg()->toString() << "," << offset << ")";
|
|---|
| [79dd0e] | 115 | return sstr.str();
|
|---|
| [cfda65] | 116 | }
|
|---|
| 117 |
|
|---|
| [e09b70] | 118 | Shape translate(const Shape &arg, const Vector &offset){
|
|---|
| 119 | Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
|
|---|
| 120 | return Shape(impl);
|
|---|
| 121 | }
|
|---|
| [5e588b5] | 122 |
|
|---|
| 123 | /*********************** stretch ******************/
|
|---|
| 124 |
|
|---|
| 125 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
|---|
| [5de9da] | 126 | ShapeOpsBase_impl(_arg),factors(_factors)
|
|---|
| [5e588b5] | 127 | {
|
|---|
| 128 | ASSERT(factors[0]>0,"cannot stretch a shape by a negative amount");
|
|---|
| 129 | ASSERT(factors[1]>0,"cannot stretch a shape by a negative amount");
|
|---|
| 130 | ASSERT(factors[2]>0,"cannot stretch a shape by a negative amount");
|
|---|
| 131 | for(int i = NDIM;i--;){
|
|---|
| 132 | reciFactors[i] = 1/factors[i];
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | Stretch_impl::~Stretch_impl(){}
|
|---|
| 137 |
|
|---|
| [5de9da] | 138 | Vector Stretch_impl::translateIn(const Vector& point){
|
|---|
| [5e588b5] | 139 | Vector helper=point;
|
|---|
| 140 | helper.ScaleAll(reciFactors);
|
|---|
| [5de9da] | 141 | return helper;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | Vector Stretch_impl::translateOutPos(const Vector& point){
|
|---|
| 145 | Vector helper=point;
|
|---|
| 146 | helper.ScaleAll(factors);
|
|---|
| 147 | return helper;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | Vector Stretch_impl::translateOutNormal(const Vector& point){
|
|---|
| 151 | Vector helper=point;
|
|---|
| 152 | // the normalFactors are derived from appearances of the factors
|
|---|
| 153 | // with in the vectorproduct
|
|---|
| 154 | Vector normalFactors;
|
|---|
| 155 | normalFactors[0]=factors[1]*factors[2];
|
|---|
| 156 | normalFactors[1]=factors[0]*factors[2];
|
|---|
| 157 | normalFactors[2]=factors[0]*factors[1];
|
|---|
| 158 | helper.ScaleAll(normalFactors);
|
|---|
| 159 | return helper;
|
|---|
| [5e588b5] | 160 | }
|
|---|
| 161 |
|
|---|
| [cfda65] | 162 | string Stretch_impl::toString(){
|
|---|
| 163 | stringstream sstr;
|
|---|
| 164 | sstr << "stretch(" << getArg()->toString() << "," << factors << ")";
|
|---|
| 165 | return sstr.str();
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| [5e588b5] | 168 | Shape stretch(const Shape &arg, const Vector &factors){
|
|---|
| 169 | Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
|
|---|
| 170 | return Shape(impl);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /************************* transform *****************/
|
|---|
| 174 |
|
|---|
| 175 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const Matrix &_transformation) :
|
|---|
| [5de9da] | 176 | ShapeOpsBase_impl(_arg),transformation(_transformation)
|
|---|
| [5e588b5] | 177 | {
|
|---|
| 178 | transformationInv = transformation.invert();
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | Transform_impl::~Transform_impl(){}
|
|---|
| 182 |
|
|---|
| [5de9da] | 183 | Vector Transform_impl::translateIn(const Vector& point){
|
|---|
| 184 | return transformationInv * point;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | Vector Transform_impl::translateOutPos(const Vector& point){
|
|---|
| 188 | return transformation * point;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | Vector Transform_impl::translateOutNormal(const Vector& point){
|
|---|
| 192 | Matrix mat = transformation.determinant() * transformation.invert().transpose();
|
|---|
| 193 | return mat * point;
|
|---|
| [5e588b5] | 194 | }
|
|---|
| 195 |
|
|---|
| [cfda65] | 196 | string Transform_impl::toString(){
|
|---|
| 197 | stringstream sstr;
|
|---|
| 198 | sstr << "transform(" << getArg()->toString() << "," << transformation << ")";
|
|---|
| 199 | return sstr.str();
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| [5e588b5] | 202 | Shape transform(const Shape &arg, const Matrix &transformation){
|
|---|
| 203 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
|---|
| 204 | return Shape(impl);
|
|---|
| 205 | }
|
|---|