forked from bschauerte/opencv_matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_common.hpp
584 lines (510 loc) · 14.6 KB
/
math_common.hpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/**
* Copyright 2011 B. Schauerte. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY B. SCHAUERTE ''AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL B. SCHAUERTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as representing
* official policies, either expressed or implied, of B. Schauerte.
*/
/** math_commmon
* Provide basic math functions and constants
*
* \author B. Schauerte
* \email <schauerte@kit.edu>
* \date 2008-2011
*/
#pragma once
#include <math.h>
#include <cmath>
#define ID(x) (x)
#define SQRT(x) ( sqrt(x) )
#define SQR(x) ( (x)*(x) )
#define DIAG(a,b) ( SQRT(SQR(a) + SQR(b)) )
#define PYTH(a,b) DIAG(a,b)
#ifndef MIN
#define MIN(a,b) ((a) > (b) ? (b) : (a))
#endif
#ifndef MAX
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#endif
#define BETWEEN(X,Y,Z) ((((X) >= (Y)) && ((X) <= (Z))) ? true : false)
/** Return the signum */
#define SGN(x) ((x) > 0 ? 1 : ((x) < 0 ? -1 : 0))
#define _INTERVAL_NORMALIZE(__X,__XMIN,__XMAX) ((__X - __XMIN) / (__XMAX - __XMIN))
#define _INTERVAL_DENORMALIZE(__X,__XMIN,__XMAX) ((__X * (__XMAX - __XMIN)) + __XMIN)
/** ld(x)=log2(x) */
template <typename T>
inline
T log2(const T x)
{
static const T _inv_clog2 = (T)(1.0 / log(2.0));
return (log(x) * _inv_clog2);
}
//////////////////////////////////////////////////////////////////////////////
// Basic Array-Array Operations
//////////////////////////////////////////////////////////////////////////////
/** Calculate the min/max and argmin/argmax of the array. */
template <typename T, typename S>
inline bool
MinMaxArray(const T* elements, const S numElements, T& min, T& max, S& argMin, S& argMax)
{
if (elements != 0 && numElements > 0)
{
argMin = 0; argMax = 0;
min = elements[argMin]; max = elements[argMax];
for (S i = 1; i < numElements; i++)
{
if (elements[i] < min)
{
min = elements[i];
argMin = i;
}
else if (elements[i] > max)
{
max = elements[i];
argMax = i;
}
}
return true;
}
else
return false;
}
/** Calculate the min/max and argmin/argmax of the array. */
template <typename T, typename S>
inline bool
MinMaxArray(const T* elements, const S numElements, T& min, T& max)
{
if (elements != 0 && numElements > 0)
{
S argMin = 0, argMax = 0;
min = elements[argMin]; max = elements[argMax];
for (S i = 1; i < numElements; i++)
{
if (elements[i] < min)
{
min = elements[i];
argMin = i;
}
else if (elements[i] > max)
{
max = elements[i];
argMax = i;
}
}
return true;
}
else
return false;
}
/** Add, elementwise (dst[x] = dst[x] + src[x]). */
template <typename T, typename S>
void
AddArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] += src[i];
}
/** Add, elementwise (dst[x] = src1[x] + src2[x]). */
template <typename T, typename S>
void
AddArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] + src2[i];
}
/** Subtract, elementwise (dst[x] = dst[x] - src[x]). */
template <typename T, typename S>
void
SubArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] -= src[i];
}
/** Subtract, elementwise (dst[x] = src1[x] - src2[x]). */
template <typename T, typename S>
void
SubArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] - src2[i];
}
/** Multiply, elementwise (dst[x] = dst[x] * src[x]). */
template <typename T, typename S>
void
MulArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] *= src[i];
}
/** Multiply, elementwise (dst[x] = src1[x] * src2[x]). */
template <typename T, typename S>
void
MulArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] * src2[i];
}
/** Divide, elementwise (dst[x] = dst[x] / src[x]). */
template <typename T, typename S>
void
DivArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] /= src[i];
}
/** Divide, elementwise (dst[x] = src1[x] / src2[x]). */
template <typename T, typename S>
void
DivArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] / src2[i];
}
/** (Bitwise) OR, elementwise (dst[x] = dst[x] | src[x]). */
template <typename T, typename S>
void
OrArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] |= src[i];
}
/** (Bitwise) OR, elementwise (dst[x] = src1[x] | src2[x]). */
template <typename T, typename S>
void
OrArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] | src2[i];
}
/** (Bitwise) AND, elementwise (dst[x] = dst[x] & src[x]). */
template <typename T, typename S>
void
AndArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] &= src[i];
}
/** (Bitwise) AND, elementwise (dst[x] = src1[x] & src2[x]). */
template <typename T, typename S>
void
AndArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] & src2[i];
}
/** (Bitwise) XOR, elementwise (dst[x] = dst[x] ^ src[x]). */
template <typename T, typename S>
void
XorArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] ^= src[i];
}
/** (Bitwise) XOR, elementwise (dst[x] = src1[x] ^ src2[x]). */
template <typename T, typename S>
void
XorArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] ^ src2[i];
}
/** Minimum, elementwise (dst[x] = min(dst[x], src[x])). */
template <typename T, typename S>
void
MinArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
if (src[i] < dst[i])
dst[i] = src[i];
}
/** Minimum, elementwise (dst[x] = min(src1[x], src2[x])). */
template <typename T, typename S>
void
MinArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = MIN(src1[i], src2[i]);
}
/** Maximum, elementwise (dst[x] = max(dst[x], src[x])). */
template <typename T, typename S>
void
MaxArray(const T* src, T* dst, const S n)
{
for (S i = 0; i < n; i++)
if (src[i] > dst[i])
dst[i] = src[i];
}
/** Maximum, elementwise (dst[x] = max(src1[x], src2[x])). */
template <typename T, typename S>
void
MaxArrays(const T* src1, const T* src2, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = MAX(src1[i], src2[i]);
}
/** Copy elements. Wrapper for memcpy(..); (dst[x] = src[x]). */
template <typename T, typename S>
void
CopyArray(const T* src, T* dst, const S n)
{
memcpy((void*)dst, (void*)src, sizeof(T) * n);
}
/** Set elements to a constant value (dst[x] = value). */
template <typename T, typename S>
void
SetArray(T* dst, const T value, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = value;
}
/** Set elements to zero (dst[x] = 0). */
template <typename T, typename S>
void
ZeroArray(T* dst, const S n)
{
memset((void*)dst, 0, sizeof(T) * n);
// @note: memset can be unsafe (i.e. produce results !=0) on some machines/systems, but "normally" works
// this depends on the used FP-standard (by Compiler and Platform)
}
//////////////////////////////////////////////////////////////////////////////
// Basic Array-Scalar Operations
//////////////////////////////////////////////////////////////////////////////
/** Add, elementwise (dst[x] = src1[x] + value). */
template <typename T, typename S>
void
AddArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] + value;
}
/** Subtract, elementwise (dst[x] = src1[x] - value). */
template <typename T, typename S>
void
SubArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] - value;
}
/** Subtract, elementwise (dst[x] = value - src1[x]). */
template <typename T, typename S>
void
SubArrayScalar(const T value, const T* src1, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = value - src1[i];
}
/** Multiply, elementwise (dst[x] = src1[x] * value). */
template <typename T, typename S>
void
MulArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] * value;
}
/** Divide, elementwise (dst[x] = src1[x] / value). */
template <typename T, typename S>
void
DivArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] / value;
}
/** Divide, elementwise (dst[x] = value / src1[x]). */
template <typename T, typename S>
void
DivArrayScalar(const T value, const T* src1, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = value / src1[i];
}
/** (Bitwise) OR, elementwise (dst[x] = src1[x] | value). */
template <typename T, typename S>
void
OrArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] | value;
}
/** (Bitwise) AND, elementwise (dst[x] = src1[x] & value). */
template <typename T, typename S>
void
AndArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] & value;
}
/** (Bitwise) XOR, elementwise (dst[x] = src1[x] ^ value). */
template <typename T, typename S>
void
XorArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = src1[i] ^ value;
}
/** Minimum, elementwise (dst[x] = min(src1[x], value)). */
template <typename T, typename S>
void
MinArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = MIN(src1[i], value);
}
/** Maximum, elementwise (dst[x] = max(src1[x], value)). */
template <typename T, typename S>
void
MaxArrayScalar(const T* src1, const T value, T* dst, const S n)
{
for (S i = 0; i < n; i++)
dst[i] = MAX(src1[i], value);
}
//////////////////////////////////////////////////////////////////////////////
// Basic Operations for Angles
//////////////////////////////////////////////////////////////////////////////
//
// Definitions to work with angles
//
//#define PI 3.1415926535897932384626433
#ifndef M_PI
#define M_PI 3.1415926535897932384626433
#endif
#define PI (M_PI)
#define RAD2DEG(rad) (360.0 * (rad) / (2.0 * PI))
#define DEG2RAD(deg) ((2.0 * PI) * (deg) / 360.0)
/** Truncate the angle (in rad) into the interval [-PI;PI] */
// also possible to use a combination of acos/cos, ...
template <typename T>
inline T TruncateAngle(T angle)
{
// 1. transform into [-2PI;2PI]
T tmp = angle / ((T)2.0 * (T)PI);
if (tmp >= (T)0.0)
tmp = floor(tmp);
else
tmp = ceil(tmp);
angle -= tmp * (T)2.0 * (T)PI;
// 2. bring into [-PI;PI]
if (angle > (T)PI)
{
angle = angle - (T)2.0 * (T)PI;
}
else
{
if (angle < (T)(-1.0 * PI))
angle = (T)2.0 * (T)PI + angle;
}
return angle;
}
/** fmod(...) extension with correct negative behaviour (remainder is always positive), e.g. -1.1 mod 3 = 1.9 */
// not designed for negative denominator -> assumes denominator > 0!
inline double
fmodulus(double numerator, double denominator)
{
double c = fmod(numerator,denominator);
return (c > 0 ? c : denominator+c);
}
/** fmod(...) extension with correct negative behaviour (remainder is always positive), e.g. -1.1 mod 3 = 1.9 */
// not designed for negative denominator -> assumes denominator > 0!
inline float
fmodulus(float numerator, float denominator)
{
float c = fmod(numerator,denominator);
return (c > 0 ? c : denominator+c);
}
/** Truncate the angle (in rad) into the interval [-PI;PI] */
// faster than "TruncateAngle(...)" on SOME systems/compilers - check if this version is better for your system/implementation!
template <typename T>
inline T TruncateAngle2(T angle)
{
const T d = fmodulus(angle, (T)(2.0 * PI));
return (d > PI ? d - 2.0*PI : d);
}
/** Truncate the angle (in rad) into the interval [0;2*PI] */
template <typename T>
inline T TruncateAnglePos(T angle)
{
return fmodulus(angle, (T)(2.0 * PI));
}
//////////////////////////////////////////////////////////////////////////////
// Binomial Coefficient
//////////////////////////////////////////////////////////////////////////////
/** Calculate n choose k. */
template <typename T>
inline T BinomialCoefficient(T n, T k)
{
if (k > n)
return 0;
if (k > n/2)
k = n-k; // faster
double accum = 1;
for (T i = 0; i++ < k;)
accum *= (double)(n - k + i) / (double)i;
return (T)(accum + 0.5); // avoid rounding error
}
/** Calculate n choose k. */
template <typename T>
inline T Choose(T n, T k)
{
return BinomialCoefficient<T>(n,k);
}
//////////////////////////////////////////////////////////////////////////////
// Basic/Primitive Functions
//////////////////////////////////////////////////////////////////////////////
//
// Definitions for basic/common functions
//
#define RAMP(x) ((x) > 0 ? (x) : 0)
#define HEAVISIDE(x) ((x) > 0 ? 1 : 0)
#define DELTA_KRONECKER(x,y) ((x) == (y) ? 1 : 0)
#define KRONECKER_DELTA(x,y) DELTA_KRONECKER(x,y)
/** The Kronecker-Delta-Function, i.e. ((x) == (y) ? 1 : 0) */
template <typename T>
inline T
KroneckerDelta(T x, T y)
{
return KRONECKER_DELTA(x,y);
}
/** The Kronecker-Delta-Function, i.e. ((x) == (y) ? 1 : 0) */
template <typename T>
inline T
DeltaKronecker(T x, T y)
{
return KRONECKER_DELTA(x,y);
}
/** The Ramp-Function, i.e. ((x) > 0 ? (x) : 0) */
template <typename T>
inline T
Ramp(T x)
{
return RAMP(x);
}
/** The Heaviside-Function, i.e. ((x) > 0 ? 1 : 0) */
template <typename T>
inline T
Heaviside(T x)
{
return HEAVISIDE(x);
}