| [83c09a] | 1 | /*
 | 
|---|
 | 2 |  * Box.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jun 30, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [527de2] | 8 | //#include "Helpers/MemDebug.hpp"
 | 
|---|
| [6e00dd] | 9 | 
 | 
|---|
| [83c09a] | 10 | #include "Box.hpp"
 | 
|---|
 | 11 | 
 | 
|---|
| [f429d7] | 12 | #include <cmath>
 | 
|---|
 | 13 | 
 | 
|---|
| [83c09a] | 14 | #include "Matrix.hpp"
 | 
|---|
| [3dcb1f] | 15 | #include "vector.hpp"
 | 
|---|
| [c538d1] | 16 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
 | 17 | #include "Shapes/ShapeOps.hpp"
 | 
|---|
| [83c09a] | 18 | 
 | 
|---|
| [6e00dd] | 19 | #include "Helpers/Assert.hpp"
 | 
|---|
 | 20 | 
 | 
|---|
| [83c09a] | 21 | Box::Box()
 | 
|---|
 | 22 | {
 | 
|---|
 | 23 |   M= new Matrix();
 | 
|---|
 | 24 |   M->one();
 | 
|---|
 | 25 |   Minv = new Matrix();
 | 
|---|
 | 26 |   Minv->one();
 | 
|---|
 | 27 | }
 | 
|---|
 | 28 | 
 | 
|---|
| [7579a4b] | 29 | Box::Box(const Box& src){
 | 
|---|
 | 30 |   M=new Matrix(*src.M);
 | 
|---|
 | 31 |   Minv = new Matrix(*src.Minv);
 | 
|---|
 | 32 | }
 | 
|---|
 | 33 | 
 | 
|---|
| [83c09a] | 34 | Box::~Box()
 | 
|---|
 | 35 | {
 | 
|---|
 | 36 |   delete M;
 | 
|---|
 | 37 |   delete Minv;
 | 
|---|
 | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
| [7579a4b] | 40 | const Matrix &Box::getM() const{
 | 
|---|
| [83c09a] | 41 |   return *M;
 | 
|---|
 | 42 | }
 | 
|---|
| [7579a4b] | 43 | const Matrix &Box::getMinv() const{
 | 
|---|
| [83c09a] | 44 |   return *Minv;
 | 
|---|
 | 45 | }
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | void Box::setM(Matrix _M){
 | 
|---|
| [3bf9e2] | 48 |   ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
 | 
|---|
| [83c09a] | 49 |   *M    =_M;
 | 
|---|
 | 50 |   *Minv = M->invert();
 | 
|---|
 | 51 | }
 | 
|---|
| [7579a4b] | 52 | 
 | 
|---|
| [014475] | 53 | Vector Box::translateIn(const Vector &point) const{
 | 
|---|
| [3dcb1f] | 54 |   return (*M) * point;
 | 
|---|
 | 55 | }
 | 
|---|
 | 56 | 
 | 
|---|
| [014475] | 57 | Vector Box::translateOut(const Vector &point) const{
 | 
|---|
| [3dcb1f] | 58 |   return (*Minv) * point;
 | 
|---|
 | 59 | }
 | 
|---|
 | 60 | 
 | 
|---|
| [014475] | 61 | Vector Box::WrapPeriodically(const Vector &point) const{
 | 
|---|
| [f429d7] | 62 |   Vector helper = translateOut(point);
 | 
|---|
 | 63 |   for(int i=NDIM;i--;){
 | 
|---|
 | 64 |     double intpart,fracpart;
 | 
|---|
 | 65 |     fracpart = modf(helper.at(i),&intpart);
 | 
|---|
 | 66 |     if(fracpart<0.)
 | 
|---|
 | 67 |       fracpart+=1.;
 | 
|---|
 | 68 |     helper.at(i)=fracpart;
 | 
|---|
 | 69 |   }
 | 
|---|
 | 70 |   return translateIn(helper);
 | 
|---|
 | 71 | }
 | 
|---|
 | 72 | 
 | 
|---|
| [a630fd] | 73 | VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
 | 
|---|
| [12cf773] | 74 |   VECTORSET(std::list) res;
 | 
|---|
| [89e820] | 75 | 
 | 
|---|
 | 76 |   // translate the Vector into each of the 27 neighbourhoods
 | 
|---|
 | 77 | 
 | 
|---|
| [a630fd] | 78 |   // first create all translation Vectors
 | 
|---|
 | 79 |   // there are (n*2+1)^3 such vectors
 | 
|---|
 | 80 |   int max_dim = (n*2+1);
 | 
|---|
 | 81 |   int max_dim2 = max_dim*max_dim;
 | 
|---|
 | 82 |   int max = max_dim2*max_dim;
 | 
|---|
| [7ac4af] | 83 |   // only one loop to avoid unneccessary jumps
 | 
|---|
| [a630fd] | 84 |   for(int i = 0;i<max;++i){
 | 
|---|
 | 85 |     // get all coordinates for this iteration
 | 
|---|
 | 86 |     int n1 = (i%max_dim)-n;
 | 
|---|
 | 87 |     int n2 = ((i/max_dim)%max_dim)-n;
 | 
|---|
 | 88 |     int n3 = ((i/max_dim2))-n;
 | 
|---|
 | 89 |     Vector translation = translateIn(Vector(n1,n2,n3));
 | 
|---|
 | 90 |     res.push_back(translation);
 | 
|---|
| [89e820] | 91 |   }
 | 
|---|
 | 92 |   // translate all the translation vector by the offset defined by point
 | 
|---|
 | 93 |   res.translate(point);
 | 
|---|
 | 94 |   return res;
 | 
|---|
 | 95 | }
 | 
|---|
 | 96 | 
 | 
|---|
| [7ac4af] | 97 | VECTORSET(std::list) Box::explode(const Vector &point) const{
 | 
|---|
 | 98 |   VECTORSET(std::list) res;
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |   // translate the Vector into each of the 27 neighbourhoods
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 |   // first create all 27 translation Vectors
 | 
|---|
 | 103 |   // these loops depend on fixed parameters and can easily be expanded
 | 
|---|
 | 104 |   // by the compiler to allow code without jumps
 | 
|---|
 | 105 |   for(int n1 = -1;n1<=1;++n1){
 | 
|---|
 | 106 |     for(int n2 = -1;n2<=1;++n2){
 | 
|---|
 | 107 |       for(int n3 = -1;n3<=1;++n3){
 | 
|---|
 | 108 |         // get all coordinates for this iteration
 | 
|---|
 | 109 |         Vector translation = translateIn(Vector(n1,n2,n3));
 | 
|---|
 | 110 |         res.push_back(translation);
 | 
|---|
 | 111 |       }
 | 
|---|
 | 112 |     }
 | 
|---|
 | 113 |   }
 | 
|---|
 | 114 |   // translate all the translation vector by the offset defined by point
 | 
|---|
 | 115 |   res.translate(point);
 | 
|---|
 | 116 |   return res;
 | 
|---|
 | 117 | }
 | 
|---|
 | 118 | 
 | 
|---|
| [014475] | 119 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
 | 
|---|
 | 120 |   VECTORSET(std::list) expansion = explode(point1);
 | 
|---|
 | 121 |   double res = expansion.minDistSquared(point2);
 | 
|---|
 | 122 |   return res;
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
 | 
|---|
 | 126 |   double res;
 | 
|---|
 | 127 |   res = sqrt(periodicDistanceSquared(point1,point2));
 | 
|---|
 | 128 |   return res;
 | 
|---|
| [527de2] | 129 | }
 | 
|---|
 | 130 | 
 | 
|---|
| [c538d1] | 131 | Shape Box::getShape() const{
 | 
|---|
 | 132 |   return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 | }
 | 
|---|
 | 135 | 
 | 
|---|
| [7579a4b] | 136 | Box &Box::operator=(const Box &src){
 | 
|---|
 | 137 |   if(&src!=this){
 | 
|---|
 | 138 |     delete M;
 | 
|---|
 | 139 |     delete Minv;
 | 
|---|
 | 140 |     M    = new Matrix(*src.M);
 | 
|---|
 | 141 |     Minv = new Matrix(*src.Minv);
 | 
|---|
 | 142 |   }
 | 
|---|
 | 143 |   return *this;
 | 
|---|
 | 144 | }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | Box &Box::operator=(const Matrix &mat){
 | 
|---|
 | 147 |   setM(mat);
 | 
|---|
 | 148 |   return *this;
 | 
|---|
 | 149 | }
 | 
|---|