[e09b70] | 1 | /*
|
---|
| 2 | * ShapeOps.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jun 18, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[bbbad5] | 13 | #include "Helpers/MemDebug.hpp"
|
---|
| 14 |
|
---|
[e09b70] | 15 | #include "Shapes/ShapeOps.hpp"
|
---|
| 16 | #include "Shapes/ShapeOps_impl.hpp"
|
---|
| 17 |
|
---|
[394529] | 18 | #include "Helpers/Assert.hpp"
|
---|
| 19 |
|
---|
[5e588b5] | 20 | /********************* Resize ********************/
|
---|
| 21 |
|
---|
[e09b70] | 22 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
|
---|
| 23 | arg(_arg), size(_size)
|
---|
[394529] | 24 | {
|
---|
| 25 | ASSERT(size>0,"Cannot resize a Shape to size zero or below");
|
---|
| 26 | }
|
---|
[e09b70] | 27 |
|
---|
| 28 | Resize_impl::~Resize_impl(){}
|
---|
| 29 |
|
---|
| 30 | bool Resize_impl::isInside(const Vector& point){
|
---|
| 31 | return arg->isInside((1/size) * point);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[c5186e] | 34 | std::vector<Vector> Resize_impl::getHomogeneousPointsOnSurface(const int N) const {
|
---|
| 35 | std::vector<Vector> PointsOnSurface = arg->getHomogeneousPointsOnSurface(N);
|
---|
| 36 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
| 37 | *iter *= size;
|
---|
| 38 | }
|
---|
| 39 | return PointsOnSurface;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 |
|
---|
[e09b70] | 43 | Shape resize(const Shape &arg,double size){
|
---|
| 44 | Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
|
---|
| 45 | return Shape(impl);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[5e588b5] | 48 | /*************************** translate *******************/
|
---|
| 49 |
|
---|
[e09b70] | 50 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
---|
| 51 | arg(_arg),offset(_offset)
|
---|
| 52 | {}
|
---|
| 53 |
|
---|
| 54 | Translate_impl::~Translate_impl(){}
|
---|
| 55 |
|
---|
| 56 | bool Translate_impl::isInside(const Vector& point){
|
---|
| 57 | return arg->isInside(point-offset);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[c5186e] | 60 | std::vector<Vector> Translate_impl::getHomogeneousPointsOnSurface(const int N) const {
|
---|
| 61 | std::vector<Vector> PointsOnSurface = arg->getHomogeneousPointsOnSurface(N);
|
---|
| 62 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
| 63 | *iter += offset;
|
---|
| 64 | }
|
---|
| 65 | return PointsOnSurface;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[e09b70] | 68 | Shape translate(const Shape &arg, const Vector &offset){
|
---|
| 69 | Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
|
---|
| 70 | return Shape(impl);
|
---|
| 71 | }
|
---|
[5e588b5] | 72 |
|
---|
| 73 | /*********************** stretch ******************/
|
---|
| 74 |
|
---|
| 75 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
---|
| 76 | arg(_arg),factors(_factors)
|
---|
| 77 | {
|
---|
| 78 | ASSERT(factors[0]>0,"cannot stretch a shape by a negative amount");
|
---|
| 79 | ASSERT(factors[1]>0,"cannot stretch a shape by a negative amount");
|
---|
| 80 | ASSERT(factors[2]>0,"cannot stretch a shape by a negative amount");
|
---|
| 81 | for(int i = NDIM;i--;){
|
---|
| 82 | reciFactors[i] = 1/factors[i];
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | Stretch_impl::~Stretch_impl(){}
|
---|
| 87 |
|
---|
| 88 | bool Stretch_impl::isInside(const Vector& point){
|
---|
| 89 | Vector helper=point;
|
---|
| 90 | helper.ScaleAll(reciFactors);
|
---|
| 91 | return arg->isInside(helper);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[c5186e] | 94 | std::vector<Vector> Stretch_impl::getHomogeneousPointsOnSurface(const int N) const {
|
---|
| 95 | std::vector<Vector> PointsOnSurface = arg->getHomogeneousPointsOnSurface(N);
|
---|
| 96 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
| 97 | (*iter).ScaleAll(reciFactors);
|
---|
| 98 | }
|
---|
| 99 | return PointsOnSurface;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[5e588b5] | 102 | Shape stretch(const Shape &arg, const Vector &factors){
|
---|
| 103 | Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
|
---|
| 104 | return Shape(impl);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /************************* transform *****************/
|
---|
| 108 |
|
---|
| 109 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const Matrix &_transformation) :
|
---|
| 110 | arg(_arg),transformation(_transformation)
|
---|
| 111 | {
|
---|
| 112 | transformationInv = transformation.invert();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | Transform_impl::~Transform_impl(){}
|
---|
| 116 |
|
---|
| 117 | bool Transform_impl::isInside(const Vector& point){
|
---|
| 118 | return arg->isInside(transformationInv * point);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[c5186e] | 121 | std::vector<Vector> Transform_impl::getHomogeneousPointsOnSurface(const int N) const {
|
---|
| 122 | std::vector<Vector> PointsOnSurface = arg->getHomogeneousPointsOnSurface(N);
|
---|
| 123 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
| 124 | *iter = transformation * (*iter);
|
---|
| 125 | }
|
---|
| 126 | return PointsOnSurface;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[5e588b5] | 129 | Shape transform(const Shape &arg, const Matrix &transformation){
|
---|
| 130 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
---|
| 131 | return Shape(impl);
|
---|
| 132 | }
|
---|