Neo  0.5.0
Developer Documentation
Maths.h
Go to the documentation of this file.
1 //========================================================================
2 // Copyright (c) 2003-2011 Anael Seghezzi <www.maratis3d.com>
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 //
8 // Permission is granted to anyone to use this software for any purpose,
9 // including commercial applications, and to alter it and redistribute it
10 // freely, subject to the following restrictions:
11 //
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would
15 // be appreciated but is not required.
16 //
17 // 2. Altered source versions must be plainly marked as such, and must not
18 // be misrepresented as being the original software.
19 //
20 // 3. This notice may not be removed or altered from any source
21 // distribution.
22 //
23 //========================================================================
24 
25 
26 #ifndef __MATHS_H
27 #define __MATHS_H
28 
29 #include <math.h>
30 
31 
32 // PI
33 #ifndef M_PI
34 #define M_PI 3.14159265358979323846
35 #endif
36 
37 // Degree/Radian conversions
38 #define DEG_TO_RAD ((M_PI * 2) / 360.0)
39 #define RAD_TO_DEG (1.0 / DEG_TO_RAD)
40 
41 // MIN
42 #ifndef MIN
43 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
44 #endif
45 
46 // MAX
47 #ifndef MAX
48 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
49 #endif
50 
51 // CLAMP
52 #ifndef CLAMP
53 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
54 #endif
55 
56 // ABS
57 #ifndef ABS
58 #define ABS(a) (((a) < 0) ? -(a) : (a))
59 #endif
60 
61 // Power of two
62 #define isPowerOfTwo(x) (((x&(x - 1)) == 0) && (x != 0))
63 
64 namespace Neo
65 {
66 
67 NEO_CORE_EXPORT unsigned int getNextPowerOfTwo(unsigned int x);
68 
69 // loop
70 NEO_CORE_EXPORT float loopFloat(float val, float min, float max);
71 
72 
76 struct NEO_CORE_EXPORT Range
77 {
78  int start;
79  int end;
80 };
81 
82 // classes
83 class Vector2;
84 class Vector3;
85 class Vector4;
86 class Color;
87 class Matrix4x4;
88 class Quaternion;
89 
90 
91 // functions
92 NEO_CORE_EXPORT void sortFloatList(int indexList[], float floatList[], int start, int end);
93 NEO_CORE_EXPORT void sortFloatList(float floatList[], int start, int end);
94 NEO_CORE_EXPORT bool isBoxToBoxCollision(const Vector3 & minA, const Vector3 & maxA, const Vector3 & minB, const Vector3 & maxB);
95 NEO_CORE_EXPORT bool isBoxToBox2dCollision(const Vector2 & minA, const Vector2 & maxA, const Vector2 & minB, const Vector2 & maxB);
96 NEO_CORE_EXPORT bool isPointInBox(const Vector3 & point, const Vector3 & min, const Vector3 & max);
97 NEO_CORE_EXPORT bool isPointInBox2d(const Vector2 & point, const Vector2 & min, const Vector2 & max);
98 NEO_CORE_EXPORT bool isEdgeToBoxCollision(const Vector3 & origin, const Vector3 & dest, const Vector3 & min, const Vector3 & max);
99 NEO_CORE_EXPORT bool isEdgeToEdge2dIntersection(const Vector2 & A, const Vector2 & B, const Vector2 & C, const Vector2 & D, Vector2 * I);
100 NEO_CORE_EXPORT bool isPointInTriangle(const Vector3 & point, const Vector3 & a, const Vector3 & b, const Vector3 & c, const Vector3 & normal);
101 NEO_CORE_EXPORT bool isLineCircleIntersection(const Vector2 & origin, const Vector2 & dest, const Vector2 & circleCenter, float circleRadius);
102 NEO_CORE_EXPORT bool isRaySphereIntersection(const Vector3 & origin, const Vector3 & direction, const Vector3 & sphereCenter, float sphereRadius, Vector3 * point);
103 NEO_CORE_EXPORT bool isRayPlaneIntersection(const Vector3 & origin, const Vector3 & direction, const Vector3 & planePoint, const Vector3 & planeNormal, Vector3 * point);
104 NEO_CORE_EXPORT bool isEdgePlaneIntersection(const Vector3 & origin, const Vector3 & dest, const Vector3 & planePoint, const Vector3 & normal, Vector3 * point);
105 NEO_CORE_EXPORT bool isEdgeTriangleIntersection(const Vector3 & origin, const Vector3 & dest, const Vector3 & a, const Vector3 & b, const Vector3 & c, const Vector3 & normal, Vector3 * point);
106 
107 NEO_CORE_EXPORT Vector3 getTriangleNormal(const Vector3 & a, const Vector3 & b, const Vector3 & c);
108 
109 NEO_CORE_EXPORT void simplifyDP(float tol, Vector3 * v, int j, int k, int * mk);
110 
111 NEO_CORE_EXPORT float linearInterpolation(float y1, float y2, float mu);
112 NEO_CORE_EXPORT float cubicInterpolation(float y0, float y1, float y2, float y3, float mu);
113 NEO_CORE_EXPORT float CatmullRomInterpolation(float y0, float y1, float y2, float y3, float mu);
114 NEO_CORE_EXPORT float HermiteInterpolation(float y0, float y1, float y2, float y3, float mu, float tension, float bias, bool begin = false, bool end = false);
115 
116 // color convertions
117 NEO_CORE_EXPORT float HueToRGB(float v1, float v2, float vH);
118 NEO_CORE_EXPORT Vector3 RGBToHSV(Vector3 rgbColor);
119 NEO_CORE_EXPORT Vector3 HSVToRGB(Vector3 HSVColor);
120 NEO_CORE_EXPORT Vector3 RGBToHSL(Vector3 rgbColor);
121 NEO_CORE_EXPORT Vector3 HSLToRGB(Vector3 hslColor);
122 }
123 
124 // include classes
125 #include <Vector2.h>
126 #include <Vector3.h>
127 #include <Vector4.h>
128 #include <Color.h>
129 #include <Matrix4x4.h>
130 #include <Quaternion.h>
131 
132 #endif
Definition: Vector4.h:31
NEO_CORE_EXPORT bool isRaySphereIntersection(const Vector3 &origin, const Vector3 &direction, const Vector3 &sphereCenter, float sphereRadius, Vector3 *point)
NEO_CORE_EXPORT float HermiteInterpolation(float y0, float y1, float y2, float y3, float mu, float tension, float bias, bool begin=false, bool end=false)
NEO_CORE_EXPORT Vector3 RGBToHSL(Vector3 rgbColor)
NEO_CORE_EXPORT bool isPointInBox(const Vector3 &point, const Vector3 &min, const Vector3 &max)
NEO_CORE_EXPORT float HueToRGB(float v1, float v2, float vH)
NEO_CORE_EXPORT bool isEdgeToEdge2dIntersection(const Vector2 &A, const Vector2 &B, const Vector2 &C, const Vector2 &D, Vector2 *I)
NEO_CORE_EXPORT void simplifyDP(float tol, Vector3 *v, int j, int k, int *mk)
NEO_CORE_EXPORT Vector3 HSLToRGB(Vector3 hslColor)
NEO_CORE_EXPORT float CatmullRomInterpolation(float y0, float y1, float y2, float y3, float mu)
NEO_CORE_EXPORT float linearInterpolation(float y1, float y2, float mu)
Definition: Matrix4x4.h:31
Definition: Vector3.h:31
NEO_CORE_EXPORT unsigned int getNextPowerOfTwo(unsigned int x)
NEO_CORE_EXPORT Vector3 getTriangleNormal(const Vector3 &a, const Vector3 &b, const Vector3 &c)
NEO_CORE_EXPORT bool isRayPlaneIntersection(const Vector3 &origin, const Vector3 &direction, const Vector3 &planePoint, const Vector3 &planeNormal, Vector3 *point)
NEO_CORE_EXPORT Vector3 RGBToHSV(Vector3 rgbColor)
int end
Definition: Maths.h:79
int start
Definition: Maths.h:78
NEO_CORE_EXPORT Vector3 HSVToRGB(Vector3 HSVColor)
Implements a range data structure.
Definition: Maths.h:76
NEO_CORE_EXPORT float loopFloat(float val, float min, float max)
NEO_CORE_EXPORT bool isEdgeTriangleIntersection(const Vector3 &origin, const Vector3 &dest, const Vector3 &a, const Vector3 &b, const Vector3 &c, const Vector3 &normal, Vector3 *point)
NEO_CORE_EXPORT bool isEdgeToBoxCollision(const Vector3 &origin, const Vector3 &dest, const Vector3 &min, const Vector3 &max)
NEO_CORE_EXPORT bool isLineCircleIntersection(const Vector2 &origin, const Vector2 &dest, const Vector2 &circleCenter, float circleRadius)
NEO_CORE_EXPORT bool isEdgePlaneIntersection(const Vector3 &origin, const Vector3 &dest, const Vector3 &planePoint, const Vector3 &normal, Vector3 *point)
NEO_CORE_EXPORT bool isBoxToBoxCollision(const Vector3 &minA, const Vector3 &maxA, const Vector3 &minB, const Vector3 &maxB)
Definition: Vector2.h:31
Definition: Color.h:31
NEO_CORE_EXPORT void sortFloatList(int indexList[], float floatList[], int start, int end)
NEO_CORE_EXPORT float cubicInterpolation(float y0, float y1, float y2, float y3, float mu)
NEO_CORE_EXPORT bool isBoxToBox2dCollision(const Vector2 &minA, const Vector2 &maxA, const Vector2 &minB, const Vector2 &maxB)
NEO_CORE_EXPORT bool isPointInBox2d(const Vector2 &point, const Vector2 &min, const Vector2 &max)
Definition: Color.h:29
Definition: Quaternion.h:31
NEO_CORE_EXPORT bool isPointInTriangle(const Vector3 &point, const Vector3 &a, const Vector3 &b, const Vector3 &c, const Vector3 &normal)