Aether3D
Game Engine
MathUtil.hpp
1 #ifndef MATH_UTIL_H
2 #define MATH_UTIL_H
3 
4 #ifndef INCLUDE_VECTOR_H
5 # include <vector>
6 # define INCLUDE_VECTOR_H
7 #endif
8 
9 #ifndef VEC3_H
10 # include "Vec3.hpp"
11 #endif
12 
13 struct Face
14 {
15  Face() : a( 0 ), b( 0 ), c( 0 ) {}
16 
17  Face( unsigned short fa, unsigned short fb, unsigned short fc )
18  : a( fa )
19  , b( fb )
20  , c( fc )
21  {}
22 
23  unsigned short a, b, c;
24 };
25 
26 namespace MathUtil
27 {
28  const float deg2rad = 3.1415926535f / 180.0f;
29  const float rad2deg = 180.0f / 3.1415926535f;
30  const float tolerance = 0.00001f;
31  const float pi = 3.1415926535f;
32 
38  bool AlmostEquals( float f1, float f2 );
39 
45  unsigned short ConvertF32ToF16( float valueToConvert );
46 
54  void GetMinMax( const std::vector< Vec3 >& points, Vec3& outMin, Vec3& outMax );
55 
63  void GetCorners( const Vec3& min, const Vec3& max, std::vector< Vec3 >& outCorners );
64 
66  float Floor( float f );
67 
69  float Ceil( float f );
70 
72  float Round( float f );
73 
82  float Lerp( float start, float end, float amount );
83 
91  float Random( float aMin, float aMax );
92 
103  float Remap( float value, float oldMin, float oldMax, float newMin, float newMax );
104 
112  bool IsNaN( float f );
113 
120  bool IsPowerOfTwo( unsigned i );
121 
122  template< typename T >
123  T Min2( T t1, T t2 )
124  {
125  return t1 < t2 ? t1 : t2;
126  }
127 
128  template< typename T >
129  T Max2( T t1, T t2 )
130  {
131  return t1 > t2 ? t1 : t2;
132  }
133 
143  float IntersectRayAABB( const Vec3& origin, const Vec3& target, const Vec3& min, const Vec3& max );
144 
154  bool IntersectSphereAndAABB( const Vec3& sphereOrigin, float sphereRadius, const Vec3& aabbMin, const Vec3& aabbMax );
155 
165  bool IntersectAABBandAABB( const Vec3& aabb1Min, const Vec3& aabb1Max, const Vec3& aabb2Min, const Vec3& aabb2Max );
166 
175  float IntersectRayTriangles( const Vec3& origin, const Vec3& target, const std::vector< Vec3 >& vertices, const std::vector< Face >& faces );
176 }
177 #endif
Face
Definition: MathUtil.hpp:13
Vec3
3-component vector.
Definition: Vec3.hpp:22