Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fastmarch.h « preprocessed « mantaflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8d0511cc06d131270d4bef0284a93d2739dc5743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241


// DO NOT EDIT !
// This file is generated using the MantaFlow preprocessor (prep generate).

/******************************************************************************
 *
 * MantaFlow fluid solver framework
 * Copyright 2011 Tobias Pfaff, Nils Thuerey
 *
 * This program is free software, distributed under the terms of the
 * Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Fast marching
 *
 ******************************************************************************/

#ifndef _FASTMARCH_H
#define _FASTMARCH_H

#include <queue>
#include "levelset.h"

namespace Manta {

//! Fast marching. Transport certain values
//  This class exists in two versions: for scalar, and for vector values - the only difference are
//  flag checks i transpTouch (for simplicity in separate classes)

template<class GRID, class T>
inline T fmInterpolateNeighbors(GRID *mpVal, int x, int y, int z, Real *weights)
{
  T val(0.);
  if (weights[0] > 0.0)
    val += mpVal->get(x + 1, y + 0, z + 0) * weights[0];
  if (weights[1] > 0.0)
    val += mpVal->get(x - 1, y + 0, z + 0) * weights[1];
  if (weights[2] > 0.0)
    val += mpVal->get(x + 0, y + 1, z + 0) * weights[2];
  if (weights[3] > 0.0)
    val += mpVal->get(x + 0, y - 1, z + 0) * weights[3];
  if (mpVal->is3D()) {
    if (weights[4] > 0.0)
      val += mpVal->get(x + 0, y + 0, z + 1) * weights[4];
    if (weights[5] > 0.0)
      val += mpVal->get(x + 0, y + 0, z - 1) * weights[5];
  }
  return val;
}

template<class GRID, class T> class FmValueTransportScalar {
 public:
  FmValueTransportScalar() : mpVal(0), mpFlags(0){};
  ~FmValueTransportScalar(){};
  void initMarching(GRID *val, FlagGrid *flags)
  {
    mpVal = val;
    mpFlags = flags;
  }
  inline bool isInitialized()
  {
    return mpVal != 0;
  }

  //! cell is touched by marching from source cell
  inline void transpTouch(int x, int y, int z, Real *weights, Real time)
  {
    if (!mpVal || !mpFlags->isEmpty(x, y, z))
      return;
    T val = fmInterpolateNeighbors<GRID, T>(mpVal, x, y, z, weights);
    (*mpVal)(x, y, z) = val;
  };

 protected:
  GRID *mpVal;
  FlagGrid *mpFlags;
};

template<class GRID, class T> class FmValueTransportVec3 {
 public:
  FmValueTransportVec3() : mpVal(0), mpFlags(0){};
  ~FmValueTransportVec3(){};
  inline bool isInitialized()
  {
    return mpVal != 0;
  }
  void initMarching(GRID *val, const FlagGrid *flags)
  {
    mpVal = val;
    mpFlags = flags;
  }

  //! cell is touched by marching from source cell
  inline void transpTouch(int x, int y, int z, Real *weights, Real time)
  {
    if (!mpVal || !mpFlags->isEmpty(x, y, z))
      return;

    T val = fmInterpolateNeighbors<GRID, T>(mpVal, x, y, z, weights);

    // set velocity components if adjacent is empty
    if (mpFlags->isEmpty(x - 1, y, z))
      (*mpVal)(x, y, z).x = val.x;
    if (mpFlags->isEmpty(x, y - 1, z))
      (*mpVal)(x, y, z).y = val.y;
    if (mpVal->is3D()) {
      if (mpFlags->isEmpty(x, y, z - 1))
        (*mpVal)(x, y, z).z = val.z;
    }
  };

 protected:
  GRID *mpVal;
  const FlagGrid *mpFlags;
};

class FmHeapEntryOut {
 public:
  Vec3i p;
  // quick time access for sorting
  Real time;
  static inline bool compare(const Real x, const Real y)
  {
    return x > y;
  }

  inline bool operator<(const FmHeapEntryOut &o) const
  {
    const Real d = fabs((time) - ((o.time)));
    if (d > 0.)
      return (time) > ((o.time));
    if (p.z != o.p.z)
      return p.z > o.p.z;
    if (p.y != o.p.y)
      return p.y > o.p.y;
    return p.x > o.p.x;
  };
};

class FmHeapEntryIn {
 public:
  Vec3i p;
  // quick time access for sorting
  Real time;
  static inline bool compare(const Real x, const Real y)
  {
    return x < y;
  }

  inline bool operator<(const FmHeapEntryIn &o) const
  {
    const Real d = fabs((time) - ((o.time)));
    if (d > 0.)
      return (time) < ((o.time));
    if (p.z != o.p.z)
      return p.z < o.p.z;
    if (p.y != o.p.y)
      return p.y < o.p.y;
    return p.x < o.p.x;
  };
};

//! fast marching algorithm wrapper class
template<class T, int TDIR> class FastMarch {

 public:
  // MSVC doesn't allow static const variables in template classes
  static inline Real InvalidTime()
  {
    return -1000;
  }
  static inline Real InvtOffset()
  {
    return 500;
  }

  enum SpecialValues { FlagInited = 1, FlagIsOnHeap = 2 };

  FastMarch(const FlagGrid &flags,
            Grid<int> &fmFlags,
            Grid<Real> &levelset,
            Real maxTime,
            MACGrid *velTransport = NULL);
  ~FastMarch()
  {
  }

  //! advect level set function with given velocity */
  void performMarching();

  //! test value for invalidity
  inline bool isInvalid(Real v) const
  {
    return (v <= InvalidTime());
  }

  void addToList(const Vec3i &p, const Vec3i &src);

  //! convert phi to time value
  inline Real phi2time(Real phival)
  {
    return (phival - InvalidTime() + InvtOffset()) * -1.0;
  }

  //! ... and back
  inline Real time2phi(Real tval)
  {
    return (InvalidTime() - InvtOffset() - tval);
  }

  inline Real _phi(int i, int j, int k)
  {
    return mLevelset(i, j, k);
  }

 protected:
  Grid<Real> &mLevelset;
  const FlagGrid &mFlags;
  Grid<int> &mFmFlags;

  //! velocity extrpolation
  FmValueTransportVec3<MACGrid, Vec3> mVelTransport;

  //! maximal time to march for
  Real mMaxTime;

  //! fast marching list
  std::priority_queue<T, std::vector<T>, std::less<T>> mHeap;
  Real mReheapVal;

  //! weights for touching points
  Real mWeights[6];

  template<int C> inline Real calcWeights(int &okCnt, int &invcnt, Real *v, const Vec3i &idx);

  inline Real calculateDistance(const Vec3i &pos);
};

}  // namespace Manta
#endif