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