1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * ShapeOps.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jun 18, 2010
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "Shapes/ShapeExceptions.hpp"
|
---|
23 | #include "Shapes/ShapeOps.hpp"
|
---|
24 | #include "Shapes/ShapeOps_impl.hpp"
|
---|
25 |
|
---|
26 | #include "LinearAlgebra/Vector.hpp"
|
---|
27 | #include "CodePatterns/Assert.hpp"
|
---|
28 |
|
---|
29 | /*************** Base case ***********************/
|
---|
30 |
|
---|
31 | ShapeOpsBase_impl::ShapeOpsBase_impl(const Shape::impl_ptr &_arg) :
|
---|
32 | arg(_arg){}
|
---|
33 |
|
---|
34 | ShapeOpsBase_impl::~ShapeOpsBase_impl(){}
|
---|
35 |
|
---|
36 | bool ShapeOpsBase_impl::isInside(const Vector &point) const{
|
---|
37 | return arg->isInside(translateIn(point));
|
---|
38 | }
|
---|
39 |
|
---|
40 | bool ShapeOpsBase_impl::isOnSurface(const Vector &point) const{
|
---|
41 | return arg->isOnSurface(translateIn(point));
|
---|
42 | }
|
---|
43 |
|
---|
44 | Vector ShapeOpsBase_impl::getNormal(const Vector &point) const throw (NotOnSurfaceException){
|
---|
45 | Vector helper = translateIn(point);
|
---|
46 | if(!arg->isOnSurface(helper)){
|
---|
47 | throw NotOnSurfaceException() << ShapeVector(&helper);
|
---|
48 | }
|
---|
49 | Vector res = translateOutNormal(arg->getNormal(helper));
|
---|
50 | res.Normalize();
|
---|
51 | return res;
|
---|
52 | }
|
---|
53 |
|
---|
54 | LineSegmentSet ShapeOpsBase_impl::getLineIntersections(const Line &line) const{
|
---|
55 | Line newLine(translateIn(line.getOrigin()),translateIn(line.getDirection()));
|
---|
56 | LineSegmentSet res(line);
|
---|
57 | LineSegmentSet helper = getArg()->getLineIntersections(newLine);
|
---|
58 | for(LineSegmentSet::iterator iter = helper.begin();iter!=helper.end();++iter){
|
---|
59 | LinePoint lpBegin = iter->getBegin();
|
---|
60 | LinePoint lpEnd = iter->getBegin();
|
---|
61 | // translate both linepoints
|
---|
62 | lpBegin = lpBegin.isNegInfinity()?
|
---|
63 | line.negEndpoint():
|
---|
64 | line.getLinePoint(translateOutPos(lpBegin.getPoint()));
|
---|
65 | lpEnd = lpEnd.isPosInfinity()?
|
---|
66 | line.posEndpoint():
|
---|
67 | line.getLinePoint(translateOutPos(lpEnd.getPoint()));
|
---|
68 | res.insert(LineSegment(lpBegin,lpEnd));
|
---|
69 | }
|
---|
70 | return res;
|
---|
71 | }
|
---|
72 |
|
---|
73 | enum ShapeType ShapeOpsBase_impl::getType() const {
|
---|
74 | return getArg()->getType();
|
---|
75 | }
|
---|
76 |
|
---|
77 | std::vector<Vector> ShapeOpsBase_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
78 | return getArg()->getHomogeneousPointsOnSurface(N);;
|
---|
79 | }
|
---|
80 |
|
---|
81 | Shape::impl_ptr ShapeOpsBase_impl::getArg() const{
|
---|
82 | return arg;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /********************* Resize ********************/
|
---|
86 |
|
---|
87 | Resize_impl::Resize_impl(const Shape::impl_ptr &_arg,double _size) :
|
---|
88 | ShapeOpsBase_impl(_arg), size(_size)
|
---|
89 | {
|
---|
90 | ASSERT(size>0,"Cannot resize a Shape to size zero or below");
|
---|
91 | }
|
---|
92 |
|
---|
93 | Resize_impl::~Resize_impl(){}
|
---|
94 |
|
---|
95 | bool Resize_impl::isInside(const Vector& point) const{
|
---|
96 | return getArg()->isInside((1/size) * point);
|
---|
97 | }
|
---|
98 |
|
---|
99 | Vector Resize_impl::translateIn(const Vector& point) const{
|
---|
100 | return (1/size) * point;
|
---|
101 | }
|
---|
102 |
|
---|
103 | Vector Resize_impl::translateOutPos(const Vector& point) const{
|
---|
104 | return size * point;
|
---|
105 | }
|
---|
106 |
|
---|
107 | Vector Resize_impl::translateOutNormal(const Vector& point) const{
|
---|
108 | return point;
|
---|
109 | }
|
---|
110 |
|
---|
111 | std::string Resize_impl::toString() const{
|
---|
112 | std::stringstream sstr;
|
---|
113 | sstr << "resize(" << getArg()->toString() << "," << size << ")";
|
---|
114 | return sstr.str();
|
---|
115 | }
|
---|
116 |
|
---|
117 | std::vector<Vector> Resize_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
118 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
119 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
120 | *iter *= size;
|
---|
121 | }
|
---|
122 | return PointsOnSurface;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | Shape resize(const Shape &arg,double size){
|
---|
127 | Shape::impl_ptr impl = Shape::impl_ptr(new Resize_impl(getShapeImpl(arg),size));
|
---|
128 | return Shape(impl);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /*************************** translate *******************/
|
---|
132 |
|
---|
133 | Translate_impl::Translate_impl(const Shape::impl_ptr &_arg, const Vector &_offset) :
|
---|
134 | ShapeOpsBase_impl(_arg),offset(_offset)
|
---|
135 | {}
|
---|
136 |
|
---|
137 | Translate_impl::~Translate_impl(){}
|
---|
138 |
|
---|
139 | bool Translate_impl::isInside(const Vector& point) const{
|
---|
140 | return getArg()->isInside(point-offset);
|
---|
141 | }
|
---|
142 |
|
---|
143 | Vector Translate_impl::translateIn(const Vector& point) const{
|
---|
144 | return point-offset;
|
---|
145 | }
|
---|
146 |
|
---|
147 | Vector Translate_impl::translateOutPos(const Vector& point) const{
|
---|
148 | return point+offset;
|
---|
149 | }
|
---|
150 |
|
---|
151 | Vector Translate_impl::translateOutNormal(const Vector& point) const{
|
---|
152 | return point;
|
---|
153 | }
|
---|
154 |
|
---|
155 | std::string Translate_impl::toString() const{
|
---|
156 | std::stringstream sstr;
|
---|
157 | sstr << "translate(" << getArg()->toString() << "," << offset << ")";
|
---|
158 | return sstr.str();
|
---|
159 | }
|
---|
160 |
|
---|
161 | std::vector<Vector> Translate_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
162 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
163 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
164 | *iter += offset;
|
---|
165 | }
|
---|
166 | return PointsOnSurface;
|
---|
167 | }
|
---|
168 |
|
---|
169 | Shape translate(const Shape &arg, const Vector &offset){
|
---|
170 | Shape::impl_ptr impl = Shape::impl_ptr(new Translate_impl(getShapeImpl(arg),offset));
|
---|
171 | return Shape(impl);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /*********************** stretch ******************/
|
---|
175 |
|
---|
176 | Stretch_impl::Stretch_impl(const Shape::impl_ptr &_arg, const Vector &_factors) :
|
---|
177 | ShapeOpsBase_impl(_arg),factors(_factors)
|
---|
178 | {
|
---|
179 | ASSERT(factors[0]>0,"cannot stretch a shape by a negative amount");
|
---|
180 | ASSERT(factors[1]>0,"cannot stretch a shape by a negative amount");
|
---|
181 | ASSERT(factors[2]>0,"cannot stretch a shape by a negative amount");
|
---|
182 | for(int i = NDIM;i--;){
|
---|
183 | reciFactors[i] = 1/factors[i];
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | Stretch_impl::~Stretch_impl(){}
|
---|
188 |
|
---|
189 | bool Stretch_impl::isInside(const Vector& point) const{
|
---|
190 | Vector helper=point;
|
---|
191 | helper.ScaleAll(reciFactors);
|
---|
192 | return getArg()->isInside(helper);
|
---|
193 | }
|
---|
194 |
|
---|
195 | Vector Stretch_impl::translateIn(const Vector& point) const{
|
---|
196 | Vector helper=point;
|
---|
197 | helper.ScaleAll(reciFactors);
|
---|
198 | return helper;
|
---|
199 | }
|
---|
200 |
|
---|
201 | Vector Stretch_impl::translateOutPos(const Vector& point) const{
|
---|
202 | Vector helper=point;
|
---|
203 | helper.ScaleAll(factors);
|
---|
204 | return helper;
|
---|
205 | }
|
---|
206 |
|
---|
207 | Vector Stretch_impl::translateOutNormal(const Vector& point) const{
|
---|
208 | Vector helper=point;
|
---|
209 | // the normalFactors are derived from appearances of the factors
|
---|
210 | // with in the vectorproduct
|
---|
211 | Vector normalFactors;
|
---|
212 | normalFactors[0]=factors[1]*factors[2];
|
---|
213 | normalFactors[1]=factors[0]*factors[2];
|
---|
214 | normalFactors[2]=factors[0]*factors[1];
|
---|
215 | helper.ScaleAll(normalFactors);
|
---|
216 | return helper;
|
---|
217 | }
|
---|
218 |
|
---|
219 | std::string Stretch_impl::toString() const{
|
---|
220 | std::stringstream sstr;
|
---|
221 | sstr << "stretch(" << getArg()->toString() << "," << factors << ")";
|
---|
222 | return sstr.str();
|
---|
223 | }
|
---|
224 |
|
---|
225 | std::vector<Vector> Stretch_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
226 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
227 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
228 | (*iter).ScaleAll(reciFactors);
|
---|
229 | }
|
---|
230 | return PointsOnSurface;
|
---|
231 | }
|
---|
232 |
|
---|
233 | Shape stretch(const Shape &arg, const Vector &factors){
|
---|
234 | Shape::impl_ptr impl = Shape::impl_ptr(new Stretch_impl(getShapeImpl(arg),factors));
|
---|
235 | return Shape(impl);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /************************* transform *****************/
|
---|
239 |
|
---|
240 | Transform_impl::Transform_impl(const Shape::impl_ptr &_arg, const RealSpaceMatrix &_transformation) :
|
---|
241 | ShapeOpsBase_impl(_arg),transformation(_transformation)
|
---|
242 | {
|
---|
243 | transformationInv = transformation.invert();
|
---|
244 | }
|
---|
245 |
|
---|
246 | Transform_impl::~Transform_impl(){}
|
---|
247 |
|
---|
248 | bool Transform_impl::isInside(const Vector& point) const{
|
---|
249 | return getArg()->isInside(transformationInv * point);
|
---|
250 | }
|
---|
251 |
|
---|
252 | Vector Transform_impl::translateIn(const Vector& point) const{
|
---|
253 | return transformationInv * point;
|
---|
254 | }
|
---|
255 |
|
---|
256 | Vector Transform_impl::translateOutPos(const Vector& point) const{
|
---|
257 | return transformation * point;
|
---|
258 | }
|
---|
259 |
|
---|
260 | Vector Transform_impl::translateOutNormal(const Vector& point) const
|
---|
261 | {
|
---|
262 | RealSpaceMatrix mat = transformation.invert().transpose();
|
---|
263 | return mat * point;
|
---|
264 | }
|
---|
265 |
|
---|
266 | std::string Transform_impl::toString() const{
|
---|
267 | std::stringstream sstr;
|
---|
268 | sstr << "transform(" << getArg()->toString() << "," << transformation << ")";
|
---|
269 | return sstr.str();
|
---|
270 | }
|
---|
271 |
|
---|
272 | std::vector<Vector> Transform_impl::getHomogeneousPointsOnSurface(const size_t N) const {
|
---|
273 | std::vector<Vector> PointsOnSurface = getArg()->getHomogeneousPointsOnSurface(N);
|
---|
274 | for(std::vector<Vector>::iterator iter = PointsOnSurface.begin(); iter != PointsOnSurface.end(); ++iter) {
|
---|
275 | *iter = transformation * (*iter);
|
---|
276 | }
|
---|
277 | return PointsOnSurface;
|
---|
278 | }
|
---|
279 |
|
---|
280 | Shape transform(const Shape &arg, const RealSpaceMatrix &transformation){
|
---|
281 | Shape::impl_ptr impl = Shape::impl_ptr(new Transform_impl(getShapeImpl(arg),transformation));
|
---|
282 | return Shape(impl);
|
---|
283 | }
|
---|