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

general.h « preprocessed « mantaflow « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50eac71e87eaa85e7e0bf12a91e24a88d1dc7a82 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280


// 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
 *
 * Globally used macros and functions
 *
 ******************************************************************************/

#ifndef _GENERAL_H
#define _GENERAL_H

#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>

namespace Manta {

// ui data exchange
#ifdef GUI
// defined in qtmain.cpp
extern void updateQtGui(bool full, int frame, float time, const std::string &curPlugin);
#else
// dummy function if GUI is not enabled
inline void updateQtGui(bool full, int frame, float time, const std::string &curPlugin)
{
}
#endif

// activate debug mode if _DEBUG is defined (eg for windows)
#ifndef DEBUG
#  ifdef _DEBUG
#    define DEBUG 1
#  endif  // _DEBUG
#endif  // DEBUG

// Standard exception
class Error : public std::exception {
 public:
  Error(const std::string &s) : mS(s)
  {
#ifdef DEBUG
    // print error
    std::cerr << "Aborting: " << s << " \n";
    // then force immedieate crash in debug mode
    *(volatile int *)(0) = 1;
#endif
  }
  virtual ~Error() throw()
  {
  }
  virtual const char *what() const throw()
  {
    return mS.c_str();
  }

 private:
  std::string mS;
};

// mark unused parameter variables
#define unusedParameter(x) ((void)x)

// Debug output functions and macros
extern int gDebugLevel;

#define MSGSTREAM \
  std::ostringstream msg; \
  msg.precision(7); \
  msg.width(9);
#define debMsg(mStr, level) \
  if (_chklevel(level)) { \
    MSGSTREAM; \
    msg << mStr; \
    std::cout << msg.str() << std::endl; \
  }
inline bool _chklevel(int level = 0)
{
  return gDebugLevel >= level;
}

// error and assertation macros
#ifdef DEBUG
#  define DEBUG_ONLY(a) a
#else
#  define DEBUG_ONLY(a)
#endif
#define throwError(msg) \
  { \
    std::ostringstream __s; \
    __s << msg << std::endl << "Error raised in " << __FILE__ << ":" << __LINE__; \
    throw Manta::Error(__s.str()); \
  }
#define errMsg(msg) throwError(msg);
#define assertMsg(cond, msg) \
  if (!(cond)) \
  throwError(msg)
#define assertDeb(cond, msg) DEBUG_ONLY(assertMsg(cond, msg))

// for compatibility with blender, blender only defines WITH_FLUID, make sure we have "BLENDER"
#ifndef BLENDER
#  ifdef WITH_FLUID
#    define BLENDER 1
#  endif
#endif

// common type for indexing large grids
typedef long long IndexInt;

// template tricks
template<typename T> struct remove_pointers {
  typedef T type;
};

template<typename T> struct remove_pointers<T *> {
  typedef T type;
};

template<typename T> struct remove_pointers<T &> {
  typedef T type;
};

// Commonly used enums and types
//! Timing class for preformance measuring
struct MuTime {
  MuTime()
  {
    get();
  }
  MuTime operator-(const MuTime &a)
  {
    MuTime b;
    b.time = time - a.time;
    return b;
  };
  MuTime operator+(const MuTime &a)
  {
    MuTime b;
    b.time = time + a.time;
    return b;
  };
  MuTime operator/(unsigned long a)
  {
    MuTime b;
    b.time = time / a;
    return b;
  };
  MuTime &operator+=(const MuTime &a)
  {
    time += a.time;
    return *this;
  }
  MuTime &operator-=(const MuTime &a)
  {
    time -= a.time;
    return *this;
  }
  MuTime &operator/=(unsigned long a)
  {
    time /= a;
    return *this;
  }
  std::string toString();

  void clear()
  {
    time = 0;
  }
  void get();
  MuTime update();

  unsigned long time;
};
std::ostream &operator<<(std::ostream &os, const MuTime &t);

//! generate a string with infos about the current mantaflow build
std::string buildInfoString();

// Some commonly used math helpers
template<class T> inline T square(T a)
{
  return a * a;
}
template<class T> inline T cubed(T a)
{
  return a * a * a;
}

template<class T> inline T clamp(const T &val, const T &vmin, const T &vmax)
{
  if (val < vmin)
    return vmin;
  if (val > vmax)
    return vmax;
  return val;
}

template<class T> inline T nmod(const T &a, const T &b);
template<> inline int nmod(const int &a, const int &b)
{
  int c = a % b;
  return (c < 0) ? (c + b) : c;
}
template<> inline float nmod(const float &a, const float &b)
{
  float c = std::fmod(a, b);
  return (c < 0) ? (c + b) : c;
}
template<> inline double nmod(const double &a, const double &b)
{
  double c = std::fmod(a, b);
  return (c < 0) ? (c + b) : c;
}

template<class T> inline T safeDivide(const T &a, const T &b);
template<> inline int safeDivide<int>(const int &a, const int &b)
{
  return (b) ? (a / b) : a;
}
template<> inline float safeDivide<float>(const float &a, const float &b)
{
  return (b) ? (a / b) : a;
}
template<> inline double safeDivide<double>(const double &a, const double &b)
{
  return (b) ? (a / b) : a;
}

inline bool c_isnan(float c)
{
  volatile float d = c;
  return d != d;
}

//! Swap so that a<b
template<class T> inline void sort(T &a, T &b)
{
  if (a > b)
    std::swap(a, b);
}

//! Swap so that a<b<c
template<class T> inline void sort(T &a, T &b, T &c)
{
  if (a > b)
    std::swap(a, b);
  if (a > c)
    std::swap(a, c);
  if (b > c)
    std::swap(b, c);
}

//! Swap so that a<b<c<d
template<class T> inline void sort(T &a, T &b, T &c, T &d)
{
  if (a > b)
    std::swap(a, b);
  if (c > d)
    std::swap(c, d);
  if (a > c)
    std::swap(a, c);
  if (b > d)
    std::swap(b, d);
  if (b > c)
    std::swap(b, c);
}

}  // namespace Manta

#endif