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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
|
/**
@file g3dmath.h
Math util class.
@maintainer Morgan McGuire, http://graphics.cs.williams.edu
@cite highestBit by Jukka Liimatta
@created 2001-06-02
@edited 2009-04-07
Copyright 2000-2006, Morgan McGuire.
All rights reserved.
*/
#ifndef G3D_g3dmath_h
#define G3D_g3dmath_h
#ifdef _MSC_VER
// Disable conditional expression is constant, which occurs incorrectly on inlined functions
# pragma warning (push)
# pragma warning (disable : 4127)
// disable: "C++ exception handler used"
# pragma warning (disable : 4530)
#endif
#include "G3D/platform.h"
#include <ctype.h>
#include <float.h>
#include <limits>
#include <stdlib.h>
#if defined(_MSC_VER) && (_MSC_VER < 1000)
// Visual Studio is missing inttypes.h
# ifndef PRId64
# define PRId64 "I64d"
# endif
#else
#include <inttypes.h>
#endif
/*These defines enable functionality introduced with the 1999 ISO C
**standard. They must be defined before the inclusion of math.h to
**engage them. If optimisation is enabled, these functions will be
**inlined. With optimisation switched off, you have to link in the
**maths library using -lm.
*/
#define _ISOC9X_SOURCE1
#define _ISOC99_SOURCE1
#define __USE_ISOC9X1
#define __USE_ISOC991
#include <math.h>
#include "G3D/debug.h"
#undef min
#undef max
namespace G3D {
#ifdef _MSC_VER
inline double __fastcall drand48() {
return ::rand() / double(RAND_MAX);
}
#if !defined(_WIN64)
/**
Win32 implementation of the C99 fast rounding routines.
@cite routines are
Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
Permission to use, copy, modify, distribute, and sell this file for any
purpose is hereby granted without fee, provided that the above copyright
and this permission notice appear in all copies. No representations are
made about the suitability of this software for any purpose. It is
provided "as is" without express or implied warranty.
*/
__inline long int lrint (double flt) {
int intgr;
_asm {
fld flt
fistp intgr
};
return intgr;
}
__inline long int lrintf(float flt) {
int intgr;
_asm {
fld flt
fistp intgr
};
return intgr;
}
#else
__inline long int lrint (double flt) {
return (long int)floor(flt+0.5f);
}
__inline long int lrintf(float flt) {
return (long int)floorf(flt+0.5f);
}
#endif
#endif
#define fuzzyEpsilon (0.00001f)
/**
This value should not be tested against directly, instead
G3D::isNan() and G3D::isFinite() will return reliable results. */
double inf();
/** This value should not be tested against directly, instead
G3D::isNan() and G3D::isFinite() will return reliable results. */
double nan();
float finf();
float fnan();
inline double pi() {
return 3.1415926535898;
}
inline float pif() {
return 3.1415926535898f;
}
inline double halfPi() {
return 1.57079633;
}
inline double twoPi() {
return 6.28318531;
}
typedef signed char int8;
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
#ifdef _MSC_EXTENSIONS
typedef __int64 int64;
typedef unsigned __int64 uint64;
#elif ! defined(_MSC_VER)
typedef int64_t int64;
typedef uint64_t uint64;
#else
typedef long long int64;
typedef unsigned long long uint64;
#endif
typedef float float32;
typedef double float64;
int iAbs(int iValue);
int iCeil(double fValue);
/**
Clamps the value to the range [low, hi] (inclusive)
*/
int iClamp(int val, int low, int hi);
int16 iClamp(int16 val, int16 low, int16 hi);
double clamp(double val, double low, double hi);
float clamp(float val, float low, float hi);
/**
Returns a + (b - a) * f;
*/
inline double lerp(double a, double b, double f) {
return a + (b - a) * f;
}
inline float lerp(float a, float b, float f) {
return a + (b - a) * f;
}
/**
Wraps the value to the range [0, hi) (exclusive
on the high end). This is like the clock arithmetic
produced by % (modulo) except the result is guaranteed
to be positive.
*/
int iWrap(int val, int hi);
int iFloor(double fValue);
int iSign(int iValue);
int iSign(double fValue);
inline int iSign(float f) {
return iSign((double)f);
}
/**
Fast round to integer using the lrint routine.
Typically 6x faster than casting to integer.
*/
inline int iRound(double fValue) {
return lrint(fValue);
}
/**
Fast round to integer using the lrint routine.
Typically 6x faster than casting to integer.
*/
inline int iRound(float f) {
return lrintf(f);
}
/**
Returns a random number uniformly at random between low and hi
(inclusive).
@deprecated Use Random::integer
*/
int iRandom(int low, int hi);
double abs (double fValue);
double aCos (double fValue);
double aSin (double fValue);
double aTan (double fValue);
double aTan2 (double fY, double fX);
double sign (double fValue);
double square (double fValue);
/**
Returns true if the argument is a finite real number.
*/
bool isFinite(double x);
/**
Returns true if the argument is NaN (not a number).
You can't use x == nan to test this because all
comparisons against nan return false.
*/
bool isNaN(double x);
bool isNaN(float x);
inline bool isNaN(int x) {
(void)x;
return false;
}
/**
Computes x % 3.
*/
int iMod3(int x);
/**
Uniform random number between low and hi, inclusive. [low, hi]
@deprecated
@sa Random::uniform
*/
float uniformRandom(float low = 0.0f, float hi = 1.0f);
/**
Normally distributed random number.
@deprecated
@sa Random::gaussian
*/
float gaussRandom(float mean = 0.0f, float stdev = 1.0f);
/** Returns x<sup>5</sup> */
template <class T>
inline T pow5(T x) {
const T y = x * x;
return y * y * x;
}
template <class T>
inline T min(const T& x, const T& y) {
return std::min<T>(x, y);
}
template <class T>
inline T min(const T& x, const T& y, const T& z) {
return std::min<T>(std::min<T>(x, y), z);
}
template <class T>
inline T min(const T& x, const T& y, const T& z, const T& w) {
return std::min<T>(std::min<T>(x, y), std::min<T>(z, w));
}
template <class T>
inline T max(const T& x, const T& y) {
return std::max<T>(x, y);
}
template <class T>
inline T max(const T& x, const T& y, const T& z) {
return std::max<T>(std::max<T>(x, y), z);
}
template <class T>
inline T max(const T& x, const T& y, const T& z, const T& w) {
return std::max<T>(std::max<T>(x, y), std::max<T>(z, w));
}
int iMin(int x, int y);
int iMax(int x, int y);
double square(double x);
double sumSquares(double x, double y);
double sumSquares(double x, double y, double z);
double distance(double x, double y);
double distance(double x, double y, double z);
/**
Returnes the 0-based index of the highest 1 bit from
the left. -1 means the number was 0.
@cite Based on code by jukka@liimatta.org
*/
int highestBit(uint32 x);
/**
Note that fuzzyEq(a, b) && fuzzyEq(b, c) does not imply
fuzzyEq(a, c), although that will be the case on some
occasions.
*/
bool fuzzyEq(double a, double b);
/** True if a is definitely not equal to b.
Guaranteed false if a == b.
Possibly false when a != b.*/
bool fuzzyNe(double a, double b);
/** Is a strictly greater than b? (Guaranteed false if a <= b).
(Possibly false if a > b) */
bool fuzzyGt(double a, double b);
/** Is a near or greater than b? */
bool fuzzyGe(double a, double b);
/** Is a strictly less than b? (Guaranteed false if a >= b)*/
bool fuzzyLt(double a, double b);
/** Is a near or less than b? */
bool fuzzyLe(double a, double b);
/**
Computes 1 / sqrt(x).
*/
inline float rsq(float x) {
return 1.0f / sqrtf(x);
}
/**
Return the next power of 2 higher than the input
If the input is already a power of 2, the output will be the same
as the input.
*/
int ceilPow2(unsigned int in);
/** Returns 2^x */
inline int pow2(unsigned int x) {
return 1 << x;
}
inline double log2(double x) {
return ::log(x) * 1.442695;
}
inline float log2(float x) {
return ::logf(x) * 1.442695f;
}
inline double log2(int x) {
return log2((double)x);
}
/**
* True if num is a power of two.
*/
bool isPow2(int num);
bool isOdd(int num);
bool isEven(int num);
double toRadians(double deg);
double toDegrees(double rad);
/**
Returns true if x is not exactly equal to 0.0f.
*/
inline bool any(float x) {
return x != 0;
}
/**
Returns true if x is not exactly equal to 0.0f.
*/
inline bool all(float x) {
return x != 0;
}
/**
v / v (for DirectX/Cg support)
*/
inline float normalize(float v) {
return v / v;
}
/**
a * b (for DirectX/Cg support)
*/
inline float dot(float a, float b) {
return a * b;
}
/**
a * b (for DirectX/Cg support)
*/
inline float mul(float a, float b) {
return a * b;
}
/**
2^x
*/
inline double exp2(double x) {
return pow(2.0, x);
}
inline float exp2(float x) {
return powf(2.0f, x);
}
/** @deprecated Use rsq */
inline double rsqrt(double x) {
return 1.0 / sqrt(x);
}
/** @deprecated Use rsq */
inline float rsqrt(float x) {
// TODO: default this to using the SSE2 instruction
return 1.0 / sqrtf(x);
}
/**
sin(x)/x
*/
inline double sinc(double x) {
double r = sin(x) / x;
if (isNaN(r)) {
return 1.0;
} else {
return r;
}
}
/**
Computes a floating point modulo; the result is t wrapped to the range [lo, hi).
*/
inline float wrap(float t, float lo, float hi) {
if ((t >= lo) && (t < hi)) {
return t;
}
debugAssert(hi > lo);
float interval = hi - lo;
return t - interval * iFloor((t - lo) / interval);
}
inline double wrap(double t, double lo, double hi) {
if ((t >= lo) && (t < hi)) {
return t;
}
debugAssert(hi > lo);
double interval = hi - lo;
return t - interval * iFloor((t - lo) / interval);
}
inline double wrap(double t, double hi) {
return wrap(t, 0.0, hi);
}
inline bool isFinite(double x) {
return ! isNaN(x) && (x < G3D::inf()) && (x > -G3D::inf());
}
inline bool isFinite(float x) {
return ! isNaN(x) && (x < G3D::finf()) && (x > -G3D::finf());
}
//----------------------------------------------------------------------------
inline int iAbs (int iValue) {
return ( iValue >= 0 ? iValue : -iValue );
}
//----------------------------------------------------------------------------
inline int iCeil (double fValue) {
return int(::ceil(fValue));
}
//----------------------------------------------------------------------------
inline int iClamp(int val, int low, int hi) {
debugAssert(low <= hi);
if (val <= low) {
return low;
} else if (val >= hi) {
return hi;
} else {
return val;
}
}
//----------------------------------------------------------------------------
inline int16 iClamp(int16 val, int16 low, int16 hi) {
debugAssert(low <= hi);
if (val <= low) {
return low;
} else if (val >= hi) {
return hi;
} else {
return val;
}
}
//----------------------------------------------------------------------------
inline double clamp(double val, double low, double hi) {
debugAssert(low <= hi);
if (val <= low) {
return low;
} else if (val >= hi) {
return hi;
} else {
return val;
}
}
inline float clamp(float val, float low, float hi) {
debugAssert(low <= hi);
if (val <= low) {
return low;
} else if (val >= hi) {
return hi;
} else {
return val;
}
}
//----------------------------------------------------------------------------
inline int iWrap(int val, int hi) {
if (val < 0) {
return ((val % hi) + hi) % hi;
} else {
return val % hi;
}
}
//----------------------------------------------------------------------------
inline int iFloor (double fValue) {
return int(::floor(fValue));
}
//----------------------------------------------------------------------------
inline int iSign (int iValue) {
return ( iValue > 0 ? + 1 : ( iValue < 0 ? -1 : 0 ) );
}
inline int iSign (double fValue) {
return ( fValue > 0.0 ? + 1 : ( fValue < 0.0 ? -1 : 0 ) );
}
//----------------------------------------------------------------------------
inline double abs (double fValue) {
return double(::fabs(fValue));
}
//----------------------------------------------------------------------------
inline double aCos (double fValue) {
if ( -1.0 < fValue ) {
if ( fValue < 1.0 )
return double(::acos(fValue));
else
return 0.0;
} else {
return pi();
}
}
inline float acos (float fValue) {
if ( -1.0f < fValue ) {
if ( fValue < 1.0f ) {
return ::acos(fValue);
} else {
return 0.0f;
}
} else {
return pif();
}
}
//----------------------------------------------------------------------------
inline double aSin (double fValue) {
if ( -1.0 < fValue ) {
if ( fValue < 1.0 ) {
return double(::asin(fValue));
} else {
return -halfPi();
}
} else {
return halfPi();
}
}
//----------------------------------------------------------------------------
inline double aTan (double fValue) {
return double(::atan(fValue));
}
//----------------------------------------------------------------------------
inline double aTan2 (double fY, double fX) {
return double(::atan2(fY, fX));
}
//----------------------------------------------------------------------------
inline double sign (double fValue) {
if (fValue > 0.0) {
return 1.0;
}
if (fValue < 0.0) {
return -1.0;
}
return 0.0;
}
inline float sign (float fValue) {
if (fValue > 0.0f) {
return 1.0f;
}
if (fValue < 0.0f) {
return -1.0f;
}
return 0.0f;
}
inline float uniformRandom(float low, float hi) {
return (hi - low) * float(::rand()) / float(RAND_MAX) + low;
}
inline double square(double x) {
return x * x;
}
inline float square(float x) {
return x * x;
}
inline int square(int x) {
return x * x;
}
//----------------------------------------------------------------------------
inline double sumSquares(double x, double y) {
return x*x + y*y;
}
//----------------------------------------------------------------------------
inline float sumSquares(float x, float y) {
return x*x + y*y;
}
//----------------------------------------------------------------------------
inline double sumSquares(double x, double y, double z) {
return x*x + y*y + z*z;
}
//----------------------------------------------------------------------------
inline float sumSquares(float x, float y, float z) {
return x*x + y*y + z*z;
}
//----------------------------------------------------------------------------
inline double distance(double x, double y) {
return sqrt(sumSquares(x, y));
}
//----------------------------------------------------------------------------
inline float distance(float x, float y) {
return sqrt(sumSquares(x, y));
}
//----------------------------------------------------------------------------
inline double distance(double x, double y, double z) {
return sqrt(sumSquares(x, y, z));
}
//----------------------------------------------------------------------------
inline float distance(float x, float y, float z) {
return sqrt(sumSquares(x, y, z));
}
//----------------------------------------------------------------------------
/** @deprecated use G3D::min */
inline int iMin(int x, int y) {
return (x >= y) ? y : x;
}
//----------------------------------------------------------------------------
/** @deprecated use G3D::min */
inline int iMax(int x, int y) {
return (x >= y) ? x : y;
}
//----------------------------------------------------------------------------
inline int ceilPow2(unsigned int in) {
in -= 1;
in |= in >> 16;
in |= in >> 8;
in |= in >> 4;
in |= in >> 2;
in |= in >> 1;
return in + 1;
}
inline bool isPow2(int num) {
return ((num & -num) == num);
}
inline bool isOdd(int num) {
return (num & 1) == 1;
}
inline bool isEven(int num) {
return (num & 1) == 0;
}
inline double toRadians(double deg) {
return deg * pi() / 180.0;
}
inline double toDegrees(double rad) {
return rad * 180.0 / pi();
}
inline float toRadians(float deg) {
return deg * (float)pi() / 180.0f;
}
inline float toDegrees(float rad) {
return rad * 180.0f / (float)pi();
}
inline float toRadians(int deg) {
return deg * (float)pi() / 180.0f;
}
inline float toDegrees(int rad) {
return rad * 180.0f / (float)pi();
}
/**
Computes an appropriate epsilon for comparing a and b.
*/
inline double eps(double a, double b) {
// For a and b to be nearly equal, they must have nearly
// the same magnitude. This means that we can ignore b
// since it either has the same magnitude or the comparison
// will fail anyway.
(void)b;
const double aa = abs(a) + 1.0;
if (aa == inf()) {
return fuzzyEpsilon;
} else {
return fuzzyEpsilon * aa;
}
}
inline bool fuzzyEq(double a, double b) {
return (a == b) || (abs(a - b) <= eps(a, b));
}
inline bool fuzzyNe(double a, double b) {
return ! fuzzyEq(a, b);
}
inline bool fuzzyGt(double a, double b) {
return a > b + eps(a, b);
}
inline bool fuzzyGe(double a, double b) {
return a > b - eps(a, b);
}
inline bool fuzzyLt(double a, double b) {
return a < b - eps(a, b);
}
inline bool fuzzyLe(double a, double b) {
return a < b + eps(a, b);
}
inline int iMod3(int x) {
return x % 3;
}
/**
Given a 32-bit integer, returns the integer with the bytes in the opposite order.
*/
inline uint32 flipEndian32(const uint32 x) {
return (x << 24) | ((x & 0xFF00) << 8) |
((x & 0xFF0000) >> 8) | ((x & 0xFF000000) >> 24);
}
/**
Given a 16-bit integer, returns the integer with the bytes in the opposite order.
*/
inline uint16 flipEndian16(const uint16 x) {
return (x << 8) | ((x & 0xFF00) >> 8);
}
} // namespace
#ifdef _MSC_VER
# pragma warning (pop)
#endif
#endif
|