mantaflow  0.10
A framework for fluid simulation
fastmarch.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  * Fast marching
12  *
13  ******************************************************************************/
14 
15 #ifndef _FASTMARCH_H
16 #define _FASTMARCH_H
17 
18 #include <queue>
19 #include "levelset.h"
20 
21 namespace Manta {
22 
24 // This class exists in two versions: for scalar, and for vector values - the only difference are
25 // flag checks i transpTouch (for simplicity in separate classes)
26 
27 template<class GRID, class T>
28 inline T fmInterpolateNeighbors(GRID* mpVal, int x,int y,int z, Real *weights) {
29  T val(0.);
30  if(weights[0]>0.0) val += mpVal->get(x+1, y+0, z+0) * weights[0];
31  if(weights[1]>0.0) val += mpVal->get(x-1, y+0, z+0) * weights[1];
32  if(weights[2]>0.0) val += mpVal->get(x+0, y+1, z+0) * weights[2];
33  if(weights[3]>0.0) val += mpVal->get(x+0, y-1, z+0) * weights[3];
34  if(mpVal->is3D()) {
35  if(weights[4]>0.0) val += mpVal->get(x+0, y+0, z+1) * weights[4];
36  if(weights[5]>0.0) val += mpVal->get(x+0, y+0, z-1) * weights[5];
37  }
38  return val;
39 }
40 
41 template<class GRID, class T>
43 public:
44  FmValueTransportScalar() : mpVal(0),mpFlags(0) { };
45  ~FmValueTransportScalar() { };
46  void initMarching(GRID* val, FlagGrid* flags) {
47  mpVal = val;
48  mpFlags = flags;
49  }
50  inline bool isInitialized() { return mpVal != 0; }
51 
53  inline void transpTouch(int x,int y,int z, Real *weights, Real time) {
54  if(!mpVal || !mpFlags->isEmpty(x,y,z)) return;
55  T val = fmInterpolateNeighbors<GRID,T>(mpVal,x,y,z,weights);
56  (*mpVal)(x,y,z) = val;
57  };
58 protected:
59  GRID* mpVal;
60  FlagGrid* mpFlags;
61 };
62 
63 template<class GRID, class T>
65 public:
66  FmValueTransportVec3() : mpVal(0), mpFlags(0) { };
67  ~FmValueTransportVec3() { };
68  inline bool isInitialized() { return mpVal != 0; }
69  void initMarching(GRID* val, const FlagGrid* flags) {
70  mpVal = val;
71  mpFlags = flags;
72  }
73 
75  inline void transpTouch(int x,int y,int z, Real *weights, Real time) {
76  if(!mpVal || !mpFlags->isEmpty(x,y,z)) return;
77 
78  T val = fmInterpolateNeighbors<GRID,T>(mpVal,x,y,z,weights);
79 
80  // set velocity components if adjacent is empty
81  if (mpFlags->isEmpty(x-1,y,z)) (*mpVal)(x,y,z).x = val.x;
82  if (mpFlags->isEmpty(x,y-1,z)) (*mpVal)(x,y,z).y = val.y;
83  if(mpVal->is3D()) { if (mpFlags->isEmpty(x,y,z-1)) (*mpVal)(x,y,z).z = val.z; }
84  };
85 
86 protected:
87  GRID* mpVal;
88  const FlagGrid* mpFlags;
89 };
90 
92 public:
93  Vec3i p;
94  // quick time access for sorting
95  Real time;
96  static inline bool compare(const Real x, const Real y) {
97  return x > y;
98  }
99 
100  inline bool operator< (const FmHeapEntryOut& o) const {
101  const Real d = fabs((time) - ((o.time)));
102  if (d > 0.) return (time) > ((o.time));
103  if (p.z != o.p.z) return p.z > o.p.z;
104  if (p.y != o.p.y) return p.y > o.p.y;
105  return p.x > o.p.x;
106  };
107 
108 };
109 
111 public:
112  Vec3i p;
113  // quick time access for sorting
114  Real time;
115  static inline bool compare(const Real x, const Real y) {
116  return x < y;
117  }
118 
119  inline bool operator< (const FmHeapEntryIn& o) const {
120  const Real d = fabs((time) - ((o.time)));
121  if (d > 0.) return (time) < ((o.time));
122  if (p.z != o.p.z) return p.z < o.p.z;
123  if (p.y != o.p.y) return p.y < o.p.y;
124  return p.x < o.p.x;
125  };
126 };
127 
128 
130 template<class T, int TDIR>
131 class FastMarch {
132 
133 public:
134  // MSVC doesn't allow static const variables in template classes
135  static inline Real InvalidTime() { return -1000; }
136  static inline Real InvtOffset() { return 500; }
137 
138  enum SpecialValues { FlagInited = 1, FlagIsOnHeap = 2};
139 
140  FastMarch(const FlagGrid& flags, Grid<int>& fmFlags, Grid<Real>& levelset, Real maxTime, MACGrid* velTransport = NULL);
141  ~FastMarch() {}
142 
144  void performMarching();
145 
147  inline bool isInvalid(Real v) const { return (v <= InvalidTime()); }
148 
149  void addToList(const Vec3i& p, const Vec3i& src);
150 
152  inline Real phi2time(Real phival) { return (phival-InvalidTime()+ InvtOffset()) * -1.0; }
153 
155  inline Real time2phi(Real tval) { return (InvalidTime() - InvtOffset() - tval); }
156 
157  inline Real _phi(int i, int j, int k) { return mLevelset(i,j,k); }
158 protected:
159  Grid<Real>& mLevelset;
160  const FlagGrid& mFlags;
161  Grid<int>& mFmFlags;
162 
165 
167  Real mMaxTime;
168 
170  std::priority_queue<T, std::vector<T>, std::less<T> > mHeap;
171  Real mReheapVal;
172 
174  Real mWeights[6];
175 
176  template<int C> inline Real calcWeights(int& okCnt, int& invcnt, Real* v, const Vec3i& idx);
177 
178  inline Real calculateDistance(const Vec3i& pos);
179 };
180 
181 } // namespace
182 #endif
183 
184 
185 
Definition: commonkernels.h:22
Real mMaxTime
maximal time to march for
Definition: fastmarch.h:167
Definition: grid.h:267
FmValueTransportVec3< MACGrid, Vec3 > mVelTransport
velocity extrpolation
Definition: fastmarch.h:164
Real time2phi(Real tval)
... and back
Definition: fastmarch.h:155
Definition: grid.h:229
T fmInterpolateNeighbors(GRID *mpVal, int x, int y, int z, Real *weights)
Fast marching. Transport certain values.
Definition: fastmarch.h:28
Basic inlined vector class.
Definition: vectorbase.h:71
std::priority_queue< T, std::vector< T >, std::less< T > > mHeap
fast marching list
Definition: fastmarch.h:170
void transpTouch(int x, int y, int z, Real *weights, Real time)
cell is touched by marching from source cell
Definition: fastmarch.h:53
Real phi2time(Real phival)
convert phi to time value
Definition: fastmarch.h:152
Definition: fastmarch.h:42
void transpTouch(int x, int y, int z, Real *weights, Real time)
cell is touched by marching from source cell
Definition: fastmarch.h:75
Definition: fastmarch.h:110
Definition: fastmarch.h:91
fast marching algorithm wrapper class
Definition: fastmarch.h:131
bool isInvalid(Real v) const
test value for invalidity
Definition: fastmarch.h:147
Definition: fastmarch.h:64