MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mathtool.h
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _mathtool_h
41 #define _mathtool_h
42 
43 #include "vector3.h"
44 
45 namespace Mezzanine
46 {
47  class AxisAlignedBox;
48  class Plane;
49  class Ray;
50  class Sphere;
51  /// @namespace Mezzanine::MathTools
52  /// @brief This namespace is the home of a number of utility variables and methods to facilitate various math related tasks.
53  namespace MathTools
54  {
55  /// @brief This is a type used for the ray intersection tests performed on Planes.
56  /// @details This type provides more verbose return data that can be used for further tests.
57  typedef std::pair<Boolean,Vector3> PlaneRayTestResult;
58  /// @brief This is a type used for the return of a ray intersection test.
59  /// @details This type provides more verbose return data that can be used for further tests.
60  typedef std::pair<Boolean,Ray> GeometryRayTestResult;
61 
62  ///////////////////////////////////////////////////////////////////////////////
63  // Predefined Constants
64 
65  /// @brief Gets Pi.
67  /// @brief Gets Pi times two.
69  /// @brief Gets half of Pi.
71  /// @brief Gets Quarter Pi.
73 
74  /// @brief Gets a multiplier used in converting Radian angular units to degrees.
76  /// @brief Gets a multiplier used in converting Degree angular units to Radians.
78 
79  ///////////////////////////////////////////////////////////////////////////////
80  // Real Math & Check Functions
81 
82  /// @brief Rounds a Real up.
83  /// @param Val The value to be rounded.
84  /// @return Returns the result of the rounding.
85  Real MEZZ_LIB Ceil(const Real& Val);
86  /// @brief Rounds a Real down.
87  /// @param Val The value to be rounded.
88  /// @return Returns the result of the rounding.
89  Real MEZZ_LIB Floor(const Real& Val);
90  /// @brief Raises a Real to a power.
91  /// @param Val The base number to be raised.
92  /// @param Power The power (or exponent) to raise the Real by.
93  /// @return Returns the Raised Eeal.
94  Real MEZZ_LIB Pow(const Real& Val, const Real& Power);
95  /// @brief Gets the square root of a Real.
96  /// @param Val The number to be calculated.
97  /// @return Returns the calculated square root.
98  Real MEZZ_LIB Sqrt(const Real& Val);
99  /// @brief Gets the absolute value of a Real.
100  /// @param Val The value to get the absolute value of.
101  /// @return Returns the absolute value of the Real.
102  Real MEZZ_LIB Fabs(const Real& Val);
103  /// @brief Modulo, fo' Reals. Returns the remainder of the division between two Reals.
104  /// @param Numerator The number on top.
105  /// @param Denominator The number on bottom.
106  /// @return Returns the remainder of division between the two Reals passed in.
107  Real MEZZ_LIB Fmod(const Real& Numerator, const Real& Denominator);
108  /// @brief Checks to see if two Reals are within a certain range of each other.
109  /// @param First The first of two Reals to compare.
110  /// @param Second The second of two Reals to compare.
111  /// @param Tolerance The leeway allowed in how far the two can be apart.
112  /// @return Returns true if the two numbers are within the amount specified by Tolerance of each other, false otherwise.
113  bool MEZZ_LIB WithinTolerance(const Real& First, const Real& Second, const Real& Tolerance);
114 
115  ///////////////////////////////////////////////////////////////////////////////
116  // Angle Calculation Functions and Values
117 
118  /// @brief Calculates the Cosine of an angle.
119  /// @param Radians The angle to be computed in Radians.
120  /// @return Returns a Real representing the Cosine of the angle provided.
121  Real MEZZ_LIB Cos(const Real& Radians);
122  /// @brief Calculates the Sine of an angle.
123  /// @param Radians The angle to be computed in Radians.
124  /// @return Returns a Real representing the Sine of the angle provided.
125  Real MEZZ_LIB Sin(const Real& Radians);
126  /// @brief Calculates the Tangent of an angle.
127  /// @param Radians The angle to be computed in Radians.
128  /// @return Returns a Real representing the Tangent of the angle provided.
129  Real MEZZ_LIB Tan(const Real& Radians);
130  /// @brief Calculates the principal value of the arc Cosine of an angle.
131  /// @param Interval The Interval to calculate the arc Cosine. Range is -1.0 to 1.0.
132  /// @return Returns a Real representing the principal value of the arc Cosine of the angle provided in Radians.
133  Real MEZZ_LIB ACos(const Real& Interval);
134  /// @brief Calculates the principal value of the arc Sine of an angle.
135  /// @param Interval The Interval to calculate the arc Cosine. Range is -1.0 to 1.0.
136  /// @return Returns a Real representing the principal value of the arc Sine of the angle provided in Radians.
137  Real MEZZ_LIB ASin(const Real& Interval);
138  /// @brief Calculates the principal value of the arc Tangent of an angle.
139  /// @param Interval The Interval to calculate the arc Cosine. Range is -1.0 to 1.0.
140  /// @return Returns a Real representing the principal value of the arc Tangent of the angle provided in Radians.
141  Real MEZZ_LIB ATan(const Real& Interval);
142 
143  ///////////////////////////////////////////////////////////////////////////////
144  // Angle Conversion Functions and Values
145 
146  /// @brief Converts an angle in Degree's to the same angle Radians.
147  /// @param Degrees The number of Degree's to convert.
148  /// @return Returns the converted number in Radians.
149  Real MEZZ_LIB DegreesToRadians(const Real& Degrees);
150  /// @brief Converts an angle in Radians's to the same angle Degrees.
151  /// @param Radians The number of Radians's to convert.
152  /// @return Returns the converted number in Degrees.
153  Real MEZZ_LIB RadiansToDegrees(const Real& Radians);
154 
155  ///////////////////////////////////////////////////////////////////////////////
156  // Geometry Math
157 
158  ///////////////////////////////////////////////////////////////////////////////
159  // Geometry Intersection Query
160 
161  /// @brief Checks to see if a point in 3D space is within an AABB.
162  /// @param Box The AABB to check if the point is inside.
163  /// @param Point The point in 3D space to check if it is within the AABB.
164  /// @return Returns true if the provided point is within the AABB, false otherwise.
165  Boolean MEZZ_LIB IsInside(const AxisAlignedBox& Box, const Vector3& Point);
166  /// @brief Checks to see if a point in 3D space is within a Sphere.
167  /// @param Ball The Sphere to check if the point is inside.
168  /// @param Point The point in 3D space to check if it is within the Sphere.
169  /// @return Returns true if the provided point is within the sphere, false otherwise.
170  Boolean MEZZ_LIB IsInside(const Sphere& Ball, const Vector3& Point);
171 
172  /// @brief Checks to see if an AABB and Sphere are overlapping.
173  /// @param Box The AABB to check for overlap.
174  /// @param Ball The Sphere to check for overlap.
175  /// @return Returns true if the AABB and Sphere overlap, false otherwise.
176  Boolean MEZZ_LIB Overlap(const AxisAlignedBox& Box, const Sphere& Ball);
177  /// @brief Checks to see if an AANN and Plane are overlapping.
178  /// @param Box The AABB to check for overlap.
179  /// @param Surface The Plane to check for overlap.
180  /// @return Returns true if the AABB and Pland overlap, false otherwise.
181  Boolean MEZZ_LIB Overlap(const AxisAlignedBox& Box, const Plane& Surface);
182  /// @brief Checks to see if a Plane and Sphere are overlapping.
183  /// @param Surface The Plane to check for overlap.
184  /// @param Ball The Sphere to check for overlap.
185  /// @return Returns true if the Plane and Sphere overlap, false otherwise.
186  Boolean MEZZ_LIB Overlap(const Plane& Surface, const Sphere& Ball);
187 
188  /// @brief Checks to see if two AABBs are overlapping.
189  /// @param Box1 The first AABB to check for overlap.
190  /// @param Box2 The second AABB to check for overlap.
191  /// @return Returns true if the two AABBs are overlapping, false otherwise.
192  Boolean MEZZ_LIB Overlap(const AxisAlignedBox& Box1, const AxisAlignedBox& Box2);
193  /// @brief Checks to see if two Spheres are overlapping.
194  /// @param Ball1 The first Sphere to check for overlap.
195  /// @param Ball2 The second Sphere to check for overlap.
196  /// @return Returns true if the two Spheres are overlapping, false otherwise.
197  Boolean MEZZ_LIB Overlap(const Sphere& Ball1, const Sphere& Ball2);
198  /// @brief Checks to see if two Planes are overlapping.
199  /// @param Surface1 The first Plane to check for overlap.
200  /// @param Surface2 The second Plane to check for overlap.
201  /// @return Returns true if the two Planes are overlapping, false otherwise.
202  Boolean MEZZ_LIB Overlap(const Plane& Surface1, const Plane& Surface2);
203 
204  /// @brief Checks to see if a ray intersects with a Plane.
205  /// @param Surface The Plane to check for intersection.
206  /// @param Cast The casted ray to check for intersection.
207  /// @return Returns true if the ray intersects with the provided Plane.
208  PlaneRayTestResult MEZZ_LIB Intersects(const Plane& Surface, const Ray& Cast);
209  /// @brief Checks to see if a ray intersects with an AABB.
210  /// @param Box The AABB to check for intersection.
211  /// @param Cast The casted ray to check for intersection.
212  /// @return Returns a std::pair containing whether or not the ray hit, and if it did also a ray that is the subsection of the casted ray that went through the AABB.
214  /// @brief Checks to see if a ray intersects with a Sphere.
215  /// @param Ball The Sphere to check for intersection.
216  /// @param Cast The casted ray to check for intersection.
217  /// @return Returns a std::pair containing whether or not the ray hit, and if it did also a ray that is the subsection of the casted ray that went through the Sphere.
218  GeometryRayTestResult MEZZ_LIB Intersects(const Sphere& Ball, const Ray& Cast);
219  }//MathTools
220 }//Mezzanine
221 
222 #endif