1 | /*
|
---|
2 | * Box.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jun 30, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | //#include "Helpers/MemDebug.hpp"
|
---|
9 |
|
---|
10 | #include "Box.hpp"
|
---|
11 |
|
---|
12 | #include <cmath>
|
---|
13 | #include <iostream>
|
---|
14 | #include <cstdlib>
|
---|
15 |
|
---|
16 | #include "Matrix.hpp"
|
---|
17 | #include "vector.hpp"
|
---|
18 | #include "Plane.hpp"
|
---|
19 |
|
---|
20 | #include "Helpers/Assert.hpp"
|
---|
21 |
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 | Box::Box()
|
---|
25 | {
|
---|
26 | M= new Matrix();
|
---|
27 | M->one();
|
---|
28 | Minv = new Matrix();
|
---|
29 | Minv->one();
|
---|
30 | conditions.resize(3);
|
---|
31 | conditions[0] = conditions[1] = conditions[2] = Wrap;
|
---|
32 | }
|
---|
33 |
|
---|
34 | Box::Box(const Box& src){
|
---|
35 | M=new Matrix(*src.M);
|
---|
36 | Minv = new Matrix(*src.Minv);
|
---|
37 | conditions = src.conditions;
|
---|
38 | }
|
---|
39 |
|
---|
40 | Box::~Box()
|
---|
41 | {
|
---|
42 | delete M;
|
---|
43 | delete Minv;
|
---|
44 | }
|
---|
45 |
|
---|
46 | const Matrix &Box::getM() const{
|
---|
47 | return *M;
|
---|
48 | }
|
---|
49 | const Matrix &Box::getMinv() const{
|
---|
50 | return *Minv;
|
---|
51 | }
|
---|
52 |
|
---|
53 | void Box::setM(Matrix _M){
|
---|
54 | ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
|
---|
55 | *M =_M;
|
---|
56 | *Minv = M->invert();
|
---|
57 | }
|
---|
58 |
|
---|
59 | Vector Box::translateIn(const Vector &point) const{
|
---|
60 | return (*M) * point;
|
---|
61 | }
|
---|
62 |
|
---|
63 | Vector Box::translateOut(const Vector &point) const{
|
---|
64 | return (*Minv) * point;
|
---|
65 | }
|
---|
66 |
|
---|
67 | Vector Box::WrapPeriodically(const Vector &point) const{
|
---|
68 | Vector helper = translateOut(point);
|
---|
69 | for(int i=NDIM;i--;){
|
---|
70 |
|
---|
71 | switch(conditions[i]){
|
---|
72 | case Wrap:
|
---|
73 | helper.at(i)=fmod(helper.at(i),1);
|
---|
74 | helper.at(i)+=(helper.at(i)>=0)?0:1;
|
---|
75 | break;
|
---|
76 | case Bounce:
|
---|
77 | {
|
---|
78 | // there probably is a better way to handle this...
|
---|
79 | // all the fabs and fmod modf probably makes it very slow
|
---|
80 | double intpart,fracpart;
|
---|
81 | fracpart = modf(fabs(helper.at(i)),&intpart);
|
---|
82 | helper.at(i) = fabs(fracpart-fmod(intpart,2));
|
---|
83 | }
|
---|
84 | break;
|
---|
85 | case Ignore:
|
---|
86 | break;
|
---|
87 | default:
|
---|
88 | ASSERT(0,"No default case for this");
|
---|
89 | }
|
---|
90 |
|
---|
91 | }
|
---|
92 | return translateIn(helper);
|
---|
93 | }
|
---|
94 |
|
---|
95 | bool Box::isInside(const Vector &point) const
|
---|
96 | {
|
---|
97 | bool result = true;
|
---|
98 | Vector tester = translateOut(point);
|
---|
99 |
|
---|
100 | for(int i=0;i<NDIM;i++)
|
---|
101 | result = result &&
|
---|
102 | ((conditions[i] == Ignore) ||
|
---|
103 | ((tester[i] >= -MYEPSILON) &&
|
---|
104 | ((tester[i] - 1.) < MYEPSILON)));
|
---|
105 |
|
---|
106 | return result;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
|
---|
111 | ASSERT(isInside(point),"Exploded point not inside Box");
|
---|
112 | VECTORSET(std::list) res;
|
---|
113 |
|
---|
114 | Vector translater = translateOut(point);
|
---|
115 | Vector mask; // contains the ignored coordinates
|
---|
116 |
|
---|
117 | // count the number of coordinates we need to do
|
---|
118 | int dims = 0; // number of dimensions that are not ignored
|
---|
119 | vector<int> coords;
|
---|
120 | vector<int> index;
|
---|
121 | for(int i=0;i<NDIM;++i){
|
---|
122 | if(conditions[i]==Ignore){
|
---|
123 | mask[i]=translater[i];
|
---|
124 | continue;
|
---|
125 | }
|
---|
126 | coords.push_back(i);
|
---|
127 | index.push_back(-n);
|
---|
128 | dims++;
|
---|
129 | } // there are max vectors in total we need to create
|
---|
130 |
|
---|
131 | if(!dims){
|
---|
132 | // all boundaries are ignored
|
---|
133 | res.push_back(point);
|
---|
134 | return res;
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool done = false;
|
---|
138 | while(!done){
|
---|
139 | // create this vector
|
---|
140 | Vector helper;
|
---|
141 | for(int i=0;i<dims;++i){
|
---|
142 | switch(conditions[coords[i]]){
|
---|
143 | case Wrap:
|
---|
144 | helper[coords[i]] = index[i]+translater[coords[i]];
|
---|
145 | break;
|
---|
146 | case Bounce:
|
---|
147 | {
|
---|
148 | // Bouncing the coordinate x produces the series:
|
---|
149 | // 0 -> x
|
---|
150 | // 1 -> 2-x
|
---|
151 | // 2 -> 2+x
|
---|
152 | // 3 -> 4-x
|
---|
153 | // 4 -> 4+x
|
---|
154 | // the first number is the next bigger even number (n+n%2)
|
---|
155 | // the next number is the value with alternating sign (x-2*(n%2)*x)
|
---|
156 | // the negative numbers produce the same sequence reversed and shifted
|
---|
157 | int n = abs(index[i]) + ((index[i]<0)?-1:0);
|
---|
158 | int sign = (index[i]<0)?-1:+1;
|
---|
159 | int even = n%2;
|
---|
160 | helper[coords[i]]=n+even+translater[coords[i]]-2*even*translater[coords[i]];
|
---|
161 | helper[coords[i]]*=sign;
|
---|
162 | }
|
---|
163 | break;
|
---|
164 | case Ignore:
|
---|
165 | ASSERT(0,"Ignored coordinate handled in generation loop");
|
---|
166 | default:
|
---|
167 | ASSERT(0,"No default case for this switch-case");
|
---|
168 | }
|
---|
169 |
|
---|
170 | }
|
---|
171 | // add back all ignored coordinates (not handled in above loop)
|
---|
172 | helper+=mask;
|
---|
173 | res.push_back(translateIn(helper));
|
---|
174 | // set the new indexes
|
---|
175 | int pos=0;
|
---|
176 | ++index[pos];
|
---|
177 | while(index[pos]>n){
|
---|
178 | index[pos++]=-n;
|
---|
179 | if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
|
---|
180 | done = true;
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | ++index[pos];
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return res;
|
---|
187 | }
|
---|
188 |
|
---|
189 | VECTORSET(std::list) Box::explode(const Vector &point) const{
|
---|
190 | ASSERT(isInside(point),"Exploded point not inside Box");
|
---|
191 | return explode(point,1);
|
---|
192 | }
|
---|
193 |
|
---|
194 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
|
---|
195 | Vector helper1 = WrapPeriodically(point1);
|
---|
196 | Vector helper2 = WrapPeriodically(point2);
|
---|
197 | VECTORSET(std::list) expansion = explode(helper1);
|
---|
198 | double res = expansion.minDistSquared(helper2);
|
---|
199 | return res;
|
---|
200 | }
|
---|
201 |
|
---|
202 | double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
|
---|
203 | double res;
|
---|
204 | res = sqrt(periodicDistanceSquared(point1,point2));
|
---|
205 | return res;
|
---|
206 | }
|
---|
207 |
|
---|
208 | const Box::Conditions_t Box::getConditions(){
|
---|
209 | return conditions;
|
---|
210 | }
|
---|
211 |
|
---|
212 | void Box::setCondition(int i,Box::BoundaryCondition_t condition){
|
---|
213 | conditions[i]=condition;
|
---|
214 | }
|
---|
215 |
|
---|
216 | const vector<pair<Plane,Plane> > Box::getBoundingPlanes(){
|
---|
217 | vector<pair<Plane,Plane> > res;
|
---|
218 | for(int i=0;i<NDIM;++i){
|
---|
219 | Vector base1,base2,base3;
|
---|
220 | base2[(i+1)%NDIM] = 1.;
|
---|
221 | base3[(i+2)%NDIM] = 1.;
|
---|
222 | Plane p1(translateIn(base1),
|
---|
223 | translateIn(base2),
|
---|
224 | translateIn(base3));
|
---|
225 | Vector offset;
|
---|
226 | offset[i]=1;
|
---|
227 | Plane p2(translateIn(base1+offset),
|
---|
228 | translateIn(base2+offset),
|
---|
229 | translateIn(base3+offset));
|
---|
230 | res.push_back(make_pair(p1,p2));
|
---|
231 | }
|
---|
232 | return res;
|
---|
233 | }
|
---|
234 |
|
---|
235 | void Box::setCuboid(const Vector &endpoint){
|
---|
236 | ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
|
---|
237 | M->one();
|
---|
238 | M->diagonal()=endpoint;
|
---|
239 | Vector &dinv = Minv->diagonal();
|
---|
240 | for(int i=NDIM;i--;)
|
---|
241 | dinv[i]=1/endpoint[i];
|
---|
242 | }
|
---|
243 |
|
---|
244 | Box &Box::operator=(const Box &src){
|
---|
245 | if(&src!=this){
|
---|
246 | delete M;
|
---|
247 | delete Minv;
|
---|
248 | M = new Matrix(*src.M);
|
---|
249 | Minv = new Matrix(*src.Minv);
|
---|
250 | conditions = src.conditions;
|
---|
251 | }
|
---|
252 | return *this;
|
---|
253 | }
|
---|
254 |
|
---|
255 | Box &Box::operator=(const Matrix &mat){
|
---|
256 | setM(mat);
|
---|
257 | return *this;
|
---|
258 | }
|
---|