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 | return translateOutNormal(arg->getNormal(helper));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /********************* Resize ********************/
|
---|
37 |
|
---|
38 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
|
---|
39 | ShapeOpsBase_impl(_arg), size(_size)
|
---|
40 | {
|
---|
41 | ASSERT(size>0,"Cannot resize a Shape to size zero or below");
|
---|
42 | }
|
---|
43 |
|
---|
44 | Resize_impl::~Resize_impl(){}
|
---|
45 |
|
---|
46 | Vector Resize_impl::translateIn(const Vector& point){
|
---|
47 | return (1/size) * point;
|
---|
48 | }
|
---|
49 |
|
---|
50 | Vector Resize_impl::translateOutPos(const Vector& point){
|
---|
51 | return size * point;
|
---|
52 | }
|
---|
53 |
|
---|
54 | Vector Resize_impl::translateOutNormal(const Vector& point){
|
---|
55 | return point;
|
---|
56 | }
|
---|
57 |
|
---|
58 | Shape resize(const Shape &arg,double size){
|
---|
59 | Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
|
---|
60 | return Shape(impl);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*************************** translate *******************/
|
---|
64 |
|
---|
65 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
---|
66 | ShapeOpsBase_impl(_arg),offset(_offset)
|
---|
67 | {}
|
---|
68 |
|
---|
69 | Translate_impl::~Translate_impl(){}
|
---|
70 |
|
---|
71 | Vector Translate_impl::translateIn(const Vector& point){
|
---|
72 | return point-offset;
|
---|
73 | }
|
---|
74 |
|
---|
75 | Vector Translate_impl::translateOutPos(const Vector& point){
|
---|
76 | return point+offset;
|
---|
77 | }
|
---|
78 |
|
---|
79 | Vector Translate_impl::translateOutNormal(const Vector& point){
|
---|
80 | return point;
|
---|
81 | }
|
---|
82 |
|
---|
83 | Shape translate(const Shape &arg, const Vector &offset){
|
---|
84 | Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
|
---|
85 | return Shape(impl);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /*********************** stretch ******************/
|
---|
89 |
|
---|
90 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
---|
91 | ShapeOpsBase_impl(_arg),factors(_factors)
|
---|
92 | {
|
---|
93 | ASSERT(factors[0]>0,"cannot stretch a shape by a negative amount");
|
---|
94 | ASSERT(factors[1]>0,"cannot stretch a shape by a negative amount");
|
---|
95 | ASSERT(factors[2]>0,"cannot stretch a shape by a negative amount");
|
---|
96 | for(int i = NDIM;i--;){
|
---|
97 | reciFactors[i] = 1/factors[i];
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | Stretch_impl::~Stretch_impl(){}
|
---|
102 |
|
---|
103 | Vector Stretch_impl::translateIn(const Vector& point){
|
---|
104 | Vector helper=point;
|
---|
105 | helper.ScaleAll(reciFactors);
|
---|
106 | return helper;
|
---|
107 | }
|
---|
108 |
|
---|
109 | Vector Stretch_impl::translateOutPos(const Vector& point){
|
---|
110 | Vector helper=point;
|
---|
111 | helper.ScaleAll(factors);
|
---|
112 | return helper;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Vector Stretch_impl::translateOutNormal(const Vector& point){
|
---|
116 | Vector helper=point;
|
---|
117 | // the normalFactors are derived from appearances of the factors
|
---|
118 | // with in the vectorproduct
|
---|
119 | Vector normalFactors;
|
---|
120 | normalFactors[0]=factors[1]*factors[2];
|
---|
121 | normalFactors[1]=factors[0]*factors[2];
|
---|
122 | normalFactors[2]=factors[0]*factors[1];
|
---|
123 | helper.ScaleAll(normalFactors);
|
---|
124 | return helper;
|
---|
125 | }
|
---|
126 |
|
---|
127 | Shape stretch(const Shape &arg, const Vector &factors){
|
---|
128 | Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
|
---|
129 | return Shape(impl);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /************************* transform *****************/
|
---|
133 |
|
---|
134 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const Matrix &_transformation) :
|
---|
135 | ShapeOpsBase_impl(_arg),transformation(_transformation)
|
---|
136 | {
|
---|
137 | transformationInv = transformation.invert();
|
---|
138 | }
|
---|
139 |
|
---|
140 | Transform_impl::~Transform_impl(){}
|
---|
141 |
|
---|
142 | Vector Transform_impl::translateIn(const Vector& point){
|
---|
143 | return transformationInv * point;
|
---|
144 | }
|
---|
145 |
|
---|
146 | Vector Transform_impl::translateOutPos(const Vector& point){
|
---|
147 | return transformation * point;
|
---|
148 | }
|
---|
149 |
|
---|
150 | Vector Transform_impl::translateOutNormal(const Vector& point){
|
---|
151 | Matrix mat = transformation.determinant() * transformation.invert().transpose();
|
---|
152 | return mat * point;
|
---|
153 | }
|
---|
154 |
|
---|
155 | Shape transform(const Shape &arg, const Matrix &transformation){
|
---|
156 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
---|
157 | return Shape(impl);
|
---|
158 | }
|
---|