| 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 |
|
|---|
| 14 | #include "Matrix.hpp"
|
|---|
| 15 | #include "vector.hpp"
|
|---|
| 16 | #include "Plane.hpp"
|
|---|
| 17 |
|
|---|
| 18 | #include "Helpers/Assert.hpp"
|
|---|
| 19 |
|
|---|
| 20 | Box::Box()
|
|---|
| 21 | {
|
|---|
| 22 | M= new Matrix();
|
|---|
| 23 | M->one();
|
|---|
| 24 | Minv = new Matrix();
|
|---|
| 25 | Minv->one();
|
|---|
| 26 | conditions.resize(3);
|
|---|
| 27 | conditions[0] = conditions[1] = conditions[2] = Wrap;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | Box::Box(const Box& src){
|
|---|
| 31 | M=new Matrix(*src.M);
|
|---|
| 32 | Minv = new Matrix(*src.Minv);
|
|---|
| 33 | conditions = src.conditions;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | Box::~Box()
|
|---|
| 37 | {
|
|---|
| 38 | delete M;
|
|---|
| 39 | delete Minv;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | const Matrix &Box::getM() const{
|
|---|
| 43 | return *M;
|
|---|
| 44 | }
|
|---|
| 45 | const Matrix &Box::getMinv() const{
|
|---|
| 46 | return *Minv;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | void Box::setM(Matrix _M){
|
|---|
| 50 | ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
|
|---|
| 51 | *M =_M;
|
|---|
| 52 | *Minv = M->invert();
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | Vector Box::translateIn(const Vector &point) const{
|
|---|
| 56 | return (*M) * point;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | Vector Box::translateOut(const Vector &point) const{
|
|---|
| 60 | return (*Minv) * point;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | Vector Box::WrapPeriodically(const Vector &point) const{
|
|---|
| 64 | Vector helper = translateOut(point);
|
|---|
| 65 | for(int i=NDIM;i--;){
|
|---|
| 66 |
|
|---|
| 67 | switch(conditions[i]){
|
|---|
| 68 | case Wrap:
|
|---|
| 69 | helper.at(i)=fmod(helper.at(i),1);
|
|---|
| 70 | helper.at(i)+=(helper.at(i)>0)?0:1;
|
|---|
| 71 | break;
|
|---|
| 72 | case Bounce:
|
|---|
| 73 | {
|
|---|
| 74 | // there probably is a better way to handle this...
|
|---|
| 75 | // all the fabs and fmod modf probably makes it very slow
|
|---|
| 76 | double intpart,fracpart;
|
|---|
| 77 | fracpart = modf(fabs(helper.at(i)),&intpart);
|
|---|
| 78 | helper.at(i) = fabs(fracpart-fmod(intpart,2));
|
|---|
| 79 | }
|
|---|
| 80 | break;
|
|---|
| 81 | case Ignore:
|
|---|
| 82 | break;
|
|---|
| 83 | default:
|
|---|
| 84 | ASSERT(0,"No default case for this");
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | }
|
|---|
| 88 | return translateIn(helper);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | bool Box::isInside(const Vector &point) const
|
|---|
| 92 | {
|
|---|
| 93 | bool result = true;
|
|---|
| 94 | Vector tester = translateOut(point);
|
|---|
| 95 |
|
|---|
| 96 | for(int i=0;i<NDIM;i++)
|
|---|
| 97 | result = result && ((tester[i] >= -MYEPSILON) && ((tester[i] - 1.) < MYEPSILON));
|
|---|
| 98 |
|
|---|
| 99 | return result;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
|
|---|
| 104 | VECTORSET(std::list) res;
|
|---|
| 105 |
|
|---|
| 106 | // translate the Vector into each of the 27 neighbourhoods
|
|---|
| 107 |
|
|---|
| 108 | // first create all translation Vectors
|
|---|
| 109 | // there are (n*2+1)^3 such vectors
|
|---|
| 110 | int max_dim = (n*2+1);
|
|---|
| 111 | int max_dim2 = max_dim*max_dim;
|
|---|
| 112 | int max = max_dim2*max_dim;
|
|---|
| 113 | // only one loop to avoid unneccessary jumps
|
|---|
| 114 | for(int i = 0;i<max;++i){
|
|---|
| 115 | // get all coordinates for this iteration
|
|---|
| 116 | int n1 = (i%max_dim)-n;
|
|---|
| 117 | int n2 = ((i/max_dim)%max_dim)-n;
|
|---|
| 118 | int n3 = ((i/max_dim2))-n;
|
|---|
| 119 | Vector translation = translateIn(Vector(n1,n2,n3));
|
|---|
| 120 | res.push_back(translation);
|
|---|
| 121 | }
|
|---|
| 122 | // translate all the translation vector by the offset defined by point
|
|---|
| 123 | res.translate(point);
|
|---|
| 124 | return res;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | VECTORSET(std::list) Box::explode(const Vector &point) const{
|
|---|
| 128 | VECTORSET(std::list) res;
|
|---|
| 129 |
|
|---|
| 130 | // translate the Vector into each of the 27 neighbourhoods
|
|---|
| 131 |
|
|---|
| 132 | // first create all 27 translation Vectors
|
|---|
| 133 | // these loops depend on fixed parameters and can easily be expanded
|
|---|
| 134 | // by the compiler to allow code without jumps
|
|---|
| 135 | for(int n1 = -1;n1<=1;++n1){
|
|---|
| 136 | for(int n2 = -1;n2<=1;++n2){
|
|---|
| 137 | for(int n3 = -1;n3<=1;++n3){
|
|---|
| 138 | // get all coordinates for this iteration
|
|---|
| 139 | Vector translation = translateIn(Vector(n1,n2,n3));
|
|---|
| 140 | res.push_back(translation);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | // translate all the translation vector by the offset defined by point
|
|---|
| 145 | res.translate(point);
|
|---|
| 146 | return res;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
|
|---|
| 150 | VECTORSET(std::list) expansion = explode(point1);
|
|---|
| 151 | double res = expansion.minDistSquared(point2);
|
|---|
| 152 | return res;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
|
|---|
| 156 | double res;
|
|---|
| 157 | res = sqrt(periodicDistanceSquared(point1,point2));
|
|---|
| 158 | return res;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | const Box::Conditions_t Box::getConditions(){
|
|---|
| 162 | return conditions;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | void Box::setCondition(int i,Box::BoundaryCondition_t condition){
|
|---|
| 166 | conditions[i]=condition;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | const vector<pair<Plane,Plane> > Box::getBoundingPlanes(){
|
|---|
| 170 | vector<pair<Plane,Plane> > res;
|
|---|
| 171 | for(int i=0;i<NDIM;++i){
|
|---|
| 172 | Vector base1,base2,base3;
|
|---|
| 173 | base2[(i+1)%NDIM] = 1.;
|
|---|
| 174 | base3[(i+2)%NDIM] = 1.;
|
|---|
| 175 | Plane p1(translateIn(base1),
|
|---|
| 176 | translateIn(base2),
|
|---|
| 177 | translateIn(base3));
|
|---|
| 178 | Vector offset;
|
|---|
| 179 | offset[i]=1;
|
|---|
| 180 | Plane p2(translateIn(base1+offset),
|
|---|
| 181 | translateIn(base2+offset),
|
|---|
| 182 | translateIn(base3+offset));
|
|---|
| 183 | res.push_back(make_pair(p1,p2));
|
|---|
| 184 | }
|
|---|
| 185 | return res;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | Box &Box::operator=(const Box &src){
|
|---|
| 189 | if(&src!=this){
|
|---|
| 190 | delete M;
|
|---|
| 191 | delete Minv;
|
|---|
| 192 | M = new Matrix(*src.M);
|
|---|
| 193 | Minv = new Matrix(*src.Minv);
|
|---|
| 194 | conditions = src.conditions;
|
|---|
| 195 | }
|
|---|
| 196 | return *this;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | Box &Box::operator=(const Matrix &mat){
|
|---|
| 200 | setM(mat);
|
|---|
| 201 | return *this;
|
|---|
| 202 | }
|
|---|