mantaflow  0.10
A framework for fluid simulation
grid4d.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * MantaFlow fluid solver framework
5  * Copyright 2011 Tobias Pfaff, Nils Thuerey
6  *
7  * This program is free software, distributed under the terms of the
8  * GNU General Public License (GPL)
9  * http://www.gnu.org/licenses
10  *
11  * Grid representation
12  *
13  ******************************************************************************/
14 
15 #ifndef _GRID4D_H
16 #define _GRID4D_H
17 
18 #include "manta.h"
19 #include "vectorbase.h"
20 #include "vector4d.h"
21 #include "kernel.h"
22 
23 
24 namespace Manta {
25 
26 
29 PYTHON class Grid4dBase : public PbClass {
30 public:
31  enum Grid4dType { TypeNone = 0, TypeReal = 1, TypeInt = 2, TypeVec3 = 4, TypeVec4 = 8 };
32 
33  PYTHON() Grid4dBase(FluidSolver* parent);
34 
36  inline int getSizeX() const { return mSize.x; }
38  inline int getSizeY() const { return mSize.y; }
40  inline int getSizeZ() const { return mSize.z; }
42  inline int getSizeT() const { return mSize.t; }
44  inline Vec4i getSize() const { return mSize; }
45 
47  inline IndexInt getStrideX() const { return 1; }
49  inline IndexInt getStrideY() const { return mSize.x; }
51  inline IndexInt getStrideZ() const { return mStrideZ; }
53  inline IndexInt getStrideT() const { return mStrideT; }
54 
55  inline Real getDx() { return mDx; }
56 
58  inline void checkIndex(int i, int j, int k, int t) const;
60  inline void checkIndex(IndexInt idx) const;
62  inline bool isInBounds(const Vec4i& p, int bnd) const;
64  inline bool isInBounds(const Vec4i& p) const;
66  inline bool isInBounds(const Vec4& p, int bnd = 0) const { return isInBounds(toVec4i(p), bnd); }
68  inline bool isInBounds(IndexInt idx) const;
69 
71  inline Grid4dType getType() const { return mType; }
73  inline bool is3D() const { return true; }
74  inline bool is4D() const { return true; }
75 
77  inline bool isInBounds(int i,int j, int k, int t, int bnd) const { return isInBounds( Vec4i(i,j,k,t), bnd ); }
78 
80  inline IndexInt index(int i, int j, int k, int t) const { DEBUG_ONLY(checkIndex(i,j,k,t)); return (IndexInt)i + (IndexInt)mSize.x * j + (IndexInt)mStrideZ * k + (IndexInt)mStrideT * t; }
82  inline IndexInt index(const Vec4i& pos) const { DEBUG_ONLY(checkIndex(pos.x,pos.y,pos.z,pos.t)); return (IndexInt)pos.x + (IndexInt)mSize.x * pos.y + (IndexInt)mStrideZ * pos.z + (IndexInt)mStrideT * pos.t; }
83 protected:
84 
85  Grid4dType mType;
86  Vec4i mSize;
87  Real mDx;
88  // precomputed Z,T shift: to ensure 2D compatibility, always use this instead of sx*sy !
89  IndexInt mStrideZ;
90  IndexInt mStrideT;
91 };
92 
95 PYTHON template<class T> class Grid4d : public Grid4dBase {
96 public:
98  PYTHON() Grid4d(FluidSolver* parent, bool show = true);
100  Grid4d(const Grid4d<T>& a);
102  virtual ~Grid4d();
103 
104  typedef T BASETYPE;
105  typedef Grid4dBase BASETYPE_GRID;
106 
107  PYTHON() void save(std::string name);
108  PYTHON() void load(std::string name);
109 
111  PYTHON() void clear();
112 
115  inline T get(int i,int j, int k, int t) const { return mData[index(i,j,k,t)]; }
117  inline T& get(int i,int j, int k, int t) { return mData[index(i,j,k,t)]; }
119  inline T get(IndexInt idx) const { DEBUG_ONLY(checkIndex(idx)); return mData[idx]; }
121  inline T get(const Vec4i& pos) const { return mData[index(pos)]; }
123  inline T& operator()(int i, int j, int k, int t) { return mData[index(i, j, k,t)]; }
125  inline T operator()(int i, int j, int k, int t) const { return mData[index(i, j, k,t)]; }
127  inline T& operator()(IndexInt idx) { DEBUG_ONLY(checkIndex(idx)); return mData[idx]; }
129  inline T operator()(IndexInt idx) const { DEBUG_ONLY(checkIndex(idx)); return mData[idx]; }
131  inline T& operator()(const Vec4i& pos) { return mData[index(pos)]; }
133  inline T operator()(const Vec4i& pos) const { return mData[index(pos)]; }
135  inline T& operator[](IndexInt idx) { DEBUG_ONLY(checkIndex(idx)); return mData[idx]; }
137  inline const T operator[](IndexInt idx) const { DEBUG_ONLY(checkIndex(idx)); return mData[idx]; }
138 
139  // interpolated access
140  inline T getInterpolated(const Vec4& pos) const { return interpol4d<T>(mData, mSize, mStrideZ, mStrideT, pos); }
141 
142  // assignment / copy
143 
145  //Grid4d<T>& operator=(const Grid4d<T>& a);
147  PYTHON() Grid4d<T>& copyFrom(const Grid4d<T>& a, bool copyType=true ); // old: { *this = a; }
148 
149  // helper functions to work with grids in scene files
150 
152  PYTHON() void add(const Grid4d<T>& a);
153  PYTHON() void sub(const Grid4d<T>& a);
155  PYTHON() void setConst(T s);
157  PYTHON() void addConst(T s);
159  PYTHON() void addScaled(const Grid4d<T>& a, const T& factor);
161  PYTHON() void mult( const Grid4d<T>& a);
163  PYTHON() void multConst(T s);
165  PYTHON() void clamp(Real min, Real max);
166 
167  // common compound operators
169  PYTHON() Real getMaxAbs();
171  PYTHON() Real getMax();
173  PYTHON() Real getMin();
175  PYTHON() void setBound(T value, int boundaryWidth=1);
177  PYTHON() void setBoundNeumann(int boundaryWidth=1);
178 
180  PYTHON() void printGrid(int zSlice=-1, int tSlice=-1, bool printIndex=false, int bnd=0);
181 
182  // c++ only operators
183  template<class S> Grid4d<T>& operator+=(const Grid4d<S>& a);
184  template<class S> Grid4d<T>& operator+=(const S& a);
185  template<class S> Grid4d<T>& operator-=(const Grid4d<S>& a);
186  template<class S> Grid4d<T>& operator-=(const S& a);
187  template<class S> Grid4d<T>& operator*=(const Grid4d<S>& a);
188  template<class S> Grid4d<T>& operator*=(const S& a);
189  template<class S> Grid4d<T>& operator/=(const Grid4d<S>& a);
190  template<class S> Grid4d<T>& operator/=(const S& a);
191  Grid4d<T>& safeDivide(const Grid4d<T>& a);
192 
194  void swap(Grid4d<T>& other);
195 
196 protected:
197  T* mData;
198 };
199 
200 // Python doesn't know about templates: explicit aliases needed
201 
202 
203 
204 
205 
206 
209  return Vec4( Real(s1[0])/s2[0], Real(s1[1])/s2[1], Real(s1[2])/s2[2] , Real(s1[3])/s2[3] );
210 }
211 inline Vec4 calcGridSizeFactor4d(Vec4 s1, Vec4 s2) {
212  return Vec4( s1[0]/s2[0], s1[1]/s2[1], s1[2]/s2[2] , s1[3]/s2[3] );
213 }
214 
215 // prototypes for grid plugins
216 void getComponent4d(const Grid4d<Vec4>& src, Grid4d<Real>& dst, int c);
217 void setComponent4d(const Grid4d<Real>& src, Grid4d<Vec4>& dst, int c);
218 
219 
220 //******************************************************************************
221 // Implementation of inline functions
222 
223 inline void Grid4dBase::checkIndex(int i, int j, int k, int t) const {
224  if ( i<0 || j<0 || i>=mSize.x || j>=mSize.y || k<0|| k>= mSize.z ||
225  t<0|| t>= mSize.t ) {
226  std::ostringstream s;
227  s << "Grid4d " << mName << " dim " << mSize << " : index " << i << "," << j << "," << k << ","<<t<<" out of bound ";
228  errMsg(s.str());
229  }
230 }
231 
232 inline void Grid4dBase::checkIndex(IndexInt idx) const {
233  if (idx<0 || idx >= mSize.x * mSize.y * mSize.z * mSize.t) {
234  std::ostringstream s;
235  s << "Grid4d " << mName << " dim " << mSize << " : index " << idx << " out of bound ";
236  errMsg(s.str());
237  }
238 }
239 
240 bool Grid4dBase::isInBounds(const Vec4i& p) const {
241  return (p.x >= 0 && p.y >= 0 && p.z >= 0 && p.t >= 0 &&
242  p.x < mSize.x && p.y < mSize.y && p.z < mSize.z && p.t < mSize.t);
243 }
244 
245 bool Grid4dBase::isInBounds(const Vec4i& p, int bnd) const {
246  bool ret = (p.x >= bnd && p.y >= bnd && p.x < mSize.x-bnd && p.y < mSize.y-bnd);
247  ret &= (p.z >= bnd && p.z < mSize.z-bnd);
248  ret &= (p.t >= bnd && p.t < mSize.t-bnd);
249  return ret;
250 }
252 bool Grid4dBase::isInBounds(IndexInt idx) const {
253  if (idx<0 || idx >= mSize.x * mSize.y * mSize.z * mSize.t) {
254  return false;
255  }
256  return true;
257 }
258 
259 // note - ugly, mostly copied from normal GRID!
260 
262 void Grid4dAdd (Grid4d<T>& me, const Grid4d<S>& other) {}
263 
265 void Grid4dSub (Grid4d<T>& me, const Grid4d<S>& other) {}
266 
268 void Grid4dMult (Grid4d<T>& me, const Grid4d<S>& other) {}
269 
271 void Grid4dDiv (Grid4d<T>& me, const Grid4d<S>& other) {}
272 
274 void Grid4dAddScalar (Grid4d<T>& me, const S& other) {}
275 
277 void Grid4dMultScalar(Grid4d<T>& me, const S& other) {}
278 
280 void Grid4dScaledAdd (Grid4d<T>& me, const Grid4d<T>& other, const S& factor) {}
281 
282 
284 void Grid4dSafeDiv (Grid4d<T>& me, const Grid4d<T>& other) {}
285 
287 void Grid4dSetConst(Grid4d<T>& me, T value) {}
288 
289 
290 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator+= (const Grid4d<S>& a) {
291  Grid4dAdd<T,S> (*this, a);
292  return *this;
293 }
294 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator+= (const S& a) {
295  Grid4dAddScalar<T,S> (*this, a);
296  return *this;
297 }
298 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator-= (const Grid4d<S>& a) {
299  Grid4dSub<T,S> (*this, a);
300  return *this;
301 }
302 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator-= (const S& a) {
303  Grid4dAddScalar<T,S> (*this, -a);
304  return *this;
305 }
306 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator*= (const Grid4d<S>& a) {
307  Grid4dMult<T,S> (*this, a);
308  return *this;
309 }
310 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator*= (const S& a) {
311  Grid4dMultScalar<T,S> (*this, a);
312  return *this;
313 }
314 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator/= (const Grid4d<S>& a) {
315  Grid4dDiv<T,S> (*this, a);
316  return *this;
317 }
318 template<class T> template<class S> Grid4d<T>& Grid4d<T>::operator/= (const S& a) {
319  S rez((S)1.0 / a);
320  Grid4dMultScalar<T,S> (*this, rez);
321  return *this;
322 }
323 
324 
325 //******************************************************************************
326 // Other helper functions
327 
328 inline Vec4 getGradient4d(const Grid4d<Real>& data, int i, int j, int k, int t) {
329  Vec4 v;
330  if (i > data.getSizeX()-2) i= data.getSizeX()-2;
331  if (j > data.getSizeY()-2) j= data.getSizeY()-2;
332  if (k > data.getSizeZ()-2) k= data.getSizeZ()-2;
333  if (t > data.getSizeT()-2) t= data.getSizeT()-2;
334  if (i < 1) i = 1;
335  if (j < 1) j = 1;
336  if (k < 1) k = 1;
337  if (t < 1) t = 1;
338  v = Vec4( data(i+1,j ,k ,t ) - data(i-1,j ,k ,t ) ,
339  data(i ,j+1,k ,t ) - data(i ,j-1,k ,t ) ,
340  data(i ,j ,k+1,t ) - data(i ,j ,k-1,t ) ,
341  data(i ,j ,k ,t+1) - data(i ,j ,k ,t-1) );
342  return v;
343 }
344 
345 
347 void KnInterpolateGrid4dTempl(Grid4d<S>& target, Grid4d<S>& source, const Vec4& sourceFactor , Vec4 offset) {}
348 
349 
350 } //namespace
351 #endif
352 
353 
Definition: commonkernels.h:22
IndexInt getStrideZ() const
Get Stride in Z dimension.
Definition: grid4d.h:51
Definition: fileio.h:26
bool isInBounds(const Vec4 &p, int bnd=0) const
Check if index is within given boundaries.
Definition: grid4d.h:66
const T operator[](IndexInt idx) const
access data
Definition: grid4d.h:137
bool isInBounds(const Vec4i &p, int bnd) const
Check if index is within given boundaries.
Definition: grid4d.h:245
IndexInt getStrideX() const
Get Stride in X dimension.
Definition: grid4d.h:47
STL namespace.
int getSizeZ() const
Get the grids Z dimension.
Definition: grid4d.h:40
T operator()(IndexInt idx) const
access data
Definition: grid4d.h:129
Grid4dType getType() const
Get the type of grid.
Definition: grid4d.h:71
T & operator()(const Vec4i &pos)
access data
Definition: grid4d.h:131
Vec4 calcGridSizeFactor4d(Vec4i s1, Vec4i s2)
helper to compute grid conversion factor between local coordinates of two grids
Definition: grid4d.h:208
IndexInt index(int i, int j, int k, int t) const
Get index into the data.
Definition: grid4d.h:80
T & operator[](IndexInt idx)
access data
Definition: grid4d.h:135
Definition: grid4d.h:29
Vec4i getSize() const
Get the grids dimensions.
Definition: grid4d.h:44
int getSizeX() const
Get the grids X dimension.
Definition: grid4d.h:36
int getSizeT() const
Get the grids T dimension.
Definition: grid4d.h:42
T operator()(const Vec4i &pos) const
access data
Definition: grid4d.h:133
void checkIndex(int i, int j, int k, int t) const
Check if indices are within bounds, otherwise error (should only be called when debugging) ...
Definition: grid4d.h:223
T operator()(int i, int j, int k, int t) const
access data
Definition: grid4d.h:125
IndexInt index(const Vec4i &pos) const
Get index into the data.
Definition: grid4d.h:82
IndexInt getStrideT() const
Get Stride in T dimension.
Definition: grid4d.h:53
bool is3D() const
Check dimensionality.
Definition: grid4d.h:73
T & operator()(IndexInt idx)
access data
Definition: grid4d.h:127
IndexInt getStrideY() const
Get Stride in Y dimension.
Definition: grid4d.h:49
T & operator()(int i, int j, int k, int t)
access data
Definition: grid4d.h:123
int getSizeY() const
Get the grids Y dimension.
Definition: grid4d.h:38
bool isInBounds(int i, int j, int k, int t, int bnd) const
3d compatibility
Definition: grid4d.h:77
Definition: fluidsolver.h:28