-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.h
463 lines (408 loc) · 10.1 KB
/
util.h
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#ifndef UTIL_H
#define UTIL_H
#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
#ifndef M_PI
const double M_PI = 3.1415926535897932384626433832795;
#endif
#ifdef WIN32
#undef min
#undef max
#endif
using std::min;
using std::max;
using std::swap;
template<class T>
inline T sqr(const T& x)
{ return x*x; }
template<class T>
inline T cube(const T& x)
{ return x*x*x; }
template<class T>
inline T min(T a1, T a2, T a3)
{ return min(a1, min(a2, a3)); }
template<class T>
inline T min(T a1, T a2, T a3, T a4)
{ return min(min(a1, a2), min(a3, a4)); }
template<class T>
inline T min(T a1, T a2, T a3, T a4, T a5)
{ return min(min(a1, a2), min(a3, a4), a5); }
template<class T>
inline T min(T a1, T a2, T a3, T a4, T a5, T a6)
{ return min(min(a1, a2), min(a3, a4), min(a5, a6)); }
template<class T>
inline T max(T a1, T a2, T a3)
{ return max(a1, max(a2, a3)); }
template<class T>
inline T max(T a1, T a2, T a3, T a4)
{ return max(max(a1, a2), max(a3, a4)); }
template<class T>
inline T max(T a1, T a2, T a3, T a4, T a5)
{ return max(max(a1, a2), max(a3, a4), a5); }
template<class T>
inline T max(T a1, T a2, T a3, T a4, T a5, T a6)
{ return max(max(a1, a2), max(a3, a4), max(a5, a6)); }
template<class T>
inline void minmax(T a1, T a2, T& amin, T& amax)
{
if(a1<a2){
amin=a1;
amax=a2;
}else{
amin=a2;
amax=a1;
}
}
template<class T>
inline void minmax(T a1, T a2, T a3, T& amin, T& amax)
{
if(a1<a2){
if(a1<a3){
amin=a1;
if(a2<a3) amax=a3;
else amax=a2;
}else{
amin=a3;
if(a1<a2) amax=a2;
else amax=a1;
}
}else{
if(a2<a3){
amin=a2;
if(a1<a3) amax=a3;
else amax=a1;
}else{
amin=a3;
amax=a1;
}
}
}
template<class T>
inline void minmax(T a1, T a2, T a3, T a4, T& amin, T& amax)
{
if(a1<a2){
if(a3<a4){
amin=min(a1,a3);
amax=max(a2,a4);
}else{
amin=min(a1,a4);
amax=max(a2,a3);
}
}else{
if(a3<a4){
amin=min(a2,a3);
amax=max(a1,a4);
}else{
amin=min(a2,a4);
amax=max(a1,a3);
}
}
}
template<class T>
inline void minmax(T a1, T a2, T a3, T a4, T a5, T& amin, T& amax)
{
//@@@ the logic could be shortcircuited a lot!
amin=min(a1,a2,a3,a4,a5);
amax=max(a1,a2,a3,a4,a5);
}
template<class T>
inline void minmax(T a1, T a2, T a3, T a4, T a5, T a6, T& amin, T& amax)
{
//@@@ the logic could be shortcircuited a lot!
amin=min(a1,a2,a3,a4,a5,a6);
amax=max(a1,a2,a3,a4,a5,a6);
}
template<class T>
inline void update_minmax(T a1, T& amin, T& amax)
{
if(a1<amin) amin=a1;
else if(a1>amax) amax=a1;
}
template<class T>
inline void sort(T &a, T &b, T &c)
{
T temp;
if(a<b){
if(a<c){
if(c<b){ // a<c<b
temp=c;c=b;b=temp;
} // else: a<b<c
}else{ // c<a<b
temp=c;c=b;b=a;a=temp;
}
}else{
if(b<c){
if(a<c){ //b<a<c
temp=b;b=a;a=temp;
}else{ // b<c<a
temp=b;b=c;c=a;a=temp;
}
}else{ // c<b<a
temp=c;c=a;a=temp;
}
}
}
template<class T>
inline T clamp(T a, T lower, T upper)
{
if(a<lower) return lower;
else if(a>upper) return upper;
else return a;
}
// only makes sense with T=float or double
template<class T>
inline T smooth_step(T r)
{
if(r<0) return 0;
else if(r>1) return 1;
return r*r*r*(10+r*(-15+r*6));
}
// only makes sense with T=float or double
template<class T>
inline T smooth_step(T r, T r_lower, T r_upper, T value_lower, T value_upper)
{ return value_lower + smooth_step((r-r_lower)/(r_upper-r_lower)) * (value_upper-value_lower); }
// only makes sense with T=float or double
template<class T>
inline T ramp(T r)
{ return smooth_step((r+1)/2)*2-1; }
#ifdef _MSC_VER
inline double remainder(double x, double y)
{
return x-std::floor(x/y+0.5)*y;
}
#endif
inline unsigned int round_up_to_power_of_two(unsigned int n)
{
int exponent=0;
--n;
while(n){
++exponent;
n>>=1;
}
return 1<<exponent;
}
inline unsigned int round_down_to_power_of_two(unsigned int n)
{
int exponent=0;
while(n>1){
++exponent;
n>>=1;
}
return 1<<exponent;
}
// Transforms even the sequence 0,1,2,3,... into reasonably good random numbers
// Challenge: improve on this in speed and "randomness"!
// This seems to pass several statistical tests, and is a bijective map (of 32-bit unsigned ints)
inline unsigned int randhash(unsigned int seed)
{
unsigned int i=(seed^0xA3C59AC3u)*2654435769u;
i^=(i>>16);
i*=2654435769u;
i^=(i>>16);
i*=2654435769u;
return i;
}
// the inverse of randhash
inline unsigned int unhash(unsigned int h)
{
h*=340573321u;
h^=(h>>16);
h*=340573321u;
h^=(h>>16);
h*=340573321u;
h^=0xA3C59AC3u;
return h;
}
// returns repeatable stateless pseudo-random number in [0,1]
inline double randhashd(unsigned int seed)
{ return randhash(seed)/(double)UINT_MAX; }
inline float randhashf(unsigned int seed)
{ return randhash(seed)/(float)UINT_MAX; }
// returns repeatable stateless pseudo-random number in [a,b]
inline double randhashd(unsigned int seed, double a, double b)
{ return (b-a)*randhash(seed)/(double)UINT_MAX + a; }
inline float randhashf(unsigned int seed, float a, float b)
{ return ( (b-a)*randhash(seed)/(float)UINT_MAX + a); }
inline int intlog2(int x)
{
int exp=-1;
while(x){
x>>=1;
++exp;
}
return exp;
}
template<class T>
inline void get_barycentric(T x, int& i, T& f, int i_low, int i_high)
{
T s=std::floor(x);
i=(int)s;
if(i<i_low){
i=i_low;
f=0;
}else if(i>i_high-2){
i=i_high-2;
f=1;
}else
f=(T)(x-s);
}
template<class S, class T>
inline S lerp(const S& value0, const S& value1, T f)
{ return (1-f)*value0 + f*value1; }
template<class S, class T>
inline S bilerp(const S& v00, const S& v10,
const S& v01, const S& v11,
T fx, T fy)
{
return lerp(lerp(v00, v10, fx),
lerp(v01, v11, fx),
fy);
}
template<class S, class T>
inline S trilerp(const S& v000, const S& v100,
const S& v010, const S& v110,
const S& v001, const S& v101,
const S& v011, const S& v111,
T fx, T fy, T fz)
{
return lerp(bilerp(v000, v100, v010, v110, fx, fy),
bilerp(v001, v101, v011, v111, fx, fy),
fz);
}
template<class S, class T>
inline S quadlerp(const S& v0000, const S& v1000,
const S& v0100, const S& v1100,
const S& v0010, const S& v1010,
const S& v0110, const S& v1110,
const S& v0001, const S& v1001,
const S& v0101, const S& v1101,
const S& v0011, const S& v1011,
const S& v0111, const S& v1111,
T fx, T fy, T fz, T ft)
{
return lerp(trilerp(v0000, v1000, v0100, v1100, v0010, v1010, v0110, v1110, fx, fy, fz),
trilerp(v0001, v1001, v0101, v1101, v0011, v1011, v0111, v1111, fx, fy, fz),
ft);
}
// f should be between 0 and 1, with f=0.5 corresponding to balanced weighting between w0 and w2
template<class T>
inline void quadratic_bspline_weights(T f, T& w0, T& w1, T& w2)
{
w0=T(0.5)*sqr(f-1);
w1=T(0.75)-sqr(f-T(0.5));;
w2=T(0.5)*sqr(f);
}
// f should be between 0 and 1
template<class T>
inline void cubic_interp_weights(T f, T& wneg1, T& w0, T& w1, T& w2)
{
T f2(f*f), f3(f2*f);
wneg1=-T(1./3)*f+T(1./2)*f2-T(1./6)*f3;
w0=1-f2+T(1./2)*(f3-f);
w1=f+T(1./2)*(f2-f3);
w2=T(1./6)*(f3-f);
}
template<class S, class T>
inline S cubic_interp(const S& value_neg1, const S& value0, const S& value1, const S& value2, T f)
{
T wneg1, w0, w1, w2;
cubic_interp_weights(f, wneg1, w0, w1, w2);
return wneg1*value_neg1 + w0*value0 + w1*value1 + w2*value2;
}
template<class T>
void zero(std::vector<T>& v)
{ for(int i=(int)v.size()-1; i>=0; --i) v[i]=0; }
template<class T>
T abs_max(const std::vector<T>& v)
{
T m=0;
for(int i=(int)v.size()-1; i>=0; --i){
if(std::fabs(v[i])>m)
m=std::fabs(v[i]);
}
return m;
}
template<class T>
bool contains(const std::vector<T>& a, T e)
{
for(unsigned int i=0; i<a.size(); ++i)
if(a[i]==e) return true;
return false;
}
template<class T>
void add_unique(std::vector<T>& a, T e)
{
for(unsigned int i=0; i<a.size(); ++i)
if(a[i]==e) return;
a.push_back(e);
}
template<class T>
void insert(std::vector<T>& a, unsigned int index, T e)
{
a.push_back(a.back());
for(unsigned int i=(unsigned int)a.size()-1; i>index; --i)
a[i]=a[i-1];
a[index]=e;
}
template<class T>
void erase(std::vector<T>& a, unsigned int index)
{
for(unsigned int i=index; i<a.size()-1; ++i)
a[i]=a[i+1];
a.pop_back();
}
template<class T>
void erase_swap(std::vector<T>& a, unsigned int index)
{
for(unsigned int i=index; i<a.size()-1; ++i)
swap(a[i], a[i+1]);
a.pop_back();
}
template<class T>
void erase_unordered(std::vector<T>& a, unsigned int index)
{
a[index]=a.back();
a.pop_back();
}
template<class T>
void erase_unordered_swap(std::vector<T>& a, unsigned int index)
{
swap(a[index], a.back());
a.pop_back();
}
template<class T>
void find_and_erase_unordered(std::vector<T>& a, const T& doomed_element)
{
for(unsigned int i=0; i<a.size(); ++i)
if(a[i]==doomed_element){
erase_unordered(a, i);
return;
}
}
template<class T>
void replace_once(std::vector<T>& a, const T& old_element, const T& new_element)
{
for(unsigned int i=0; i<a.size(); ++i)
if(a[i]==old_element){
a[i]=new_element;
return;
}
}
template<class T>
void write_matlab(std::ostream& output, const std::vector<T>& a, const char *variable_name, bool column_vector=true, int significant_digits=18)
{
output<<variable_name<<"=[";
std::streamsize old_precision=output.precision();
output.precision(significant_digits);
for(unsigned int i=0; i<a.size(); ++i){
output<<a[i]<<" ";
}
output<<"]";
if(column_vector)
output<<"'";
output<<";"<<std::endl;
output.precision(old_precision);
}
#endif