00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include "nvsgcommon.h"
00016
00017 #include <algorithm>
00018 #include <float.h>
00019
00020
00021 #include "__WIN64__workarounds.h"
00022
00024 namespace nvmath
00025 {
00026
00027 #ifdef PI
00028 # undef PI
00029 # undef PI_HALF
00030 # undef PI_QUARTER
00031 #endif
00032
00033 const float PI = 3.14159265358979323846f;
00035 const float PI_HALF = 1.57079632679489661923f;
00037 const float PI_QUARTER = 0.78539816339744830962f;
00039 const float SQRT_TWO = 1.41421356237309504880f;
00041 const float SQRT_TWO_HALF = 0.70710678118654752440f;
00042
00044
00045 template<class T> T clamp( T v
00046 , T l
00047 , T u
00048 )
00049 {
00050 return __WIN64__wa__std_min(u, __WIN64__wa__std_max(l,v));
00051 }
00052
00054
00055 inline float degToRad( float angle
00056 )
00057 {
00058 return( angle*PI/180.0f );
00059 }
00060
00062
00063 inline bool isPowerOfTwo( int n
00064 )
00065 {
00066 return( n == ( n & -n ) );
00067 }
00068
00070
00071 template<typename T> T lerp( float alpha
00072 , const T &v0
00073 , const T &v1
00074 )
00075 {
00076 return( v0 + alpha * ( v1 - v0 ) );
00077 }
00078
00080
00081 inline int powerOfTwoBelow( int n
00082 )
00083 {
00084 int h2 = 1;
00085 for ( size_t h = n ; h ; h>>=1, h2<<=1 )
00086 ;
00087 return( h2 >> 1 );
00088 }
00089
00091
00092 inline float radToDeg( float angle
00093 )
00094 {
00095 return( angle*180.0f/PI );
00096 }
00097
00099
00100 template<typename T> int sign( const T &t
00101 )
00102 {
00103 return( ( t < 0 ) ? -1 : ( ( t > 0 ) ? 1 : 0 ) );
00104 }
00105
00107
00108 template<typename T> float square( const T& t
00109 )
00110 {
00111 return( t * t );
00112 }
00113
00114 }