1 /*
2 Copyright (C) 2005-2007 Feeling Software Inc.
3 Portions of the code are:
4 Copyright (C) 2005-2007 Sony Computer Entertainment America
5
6 MIT License: http://www.opensource.org/licenses/mit-license.php
7 */
8
9 /**
10 @file FMath.h
11 The file containing functions and constants for math.
12
13 @defgroup FMath Mathematics Classes.
14 */
15
16 #ifndef _F_MATH_H_
17 #define _F_MATH_H_
18
19 #ifndef _INC_MATH
20 #include <math.h>
21 #endif // _INC_MATH
22
23 #ifndef _FM_FLOAT_H_
24 #include "FMath/FMFloat.h"
25 #endif // _FM_FLOAT_H_
26
27
28 /** Retrieves whether two values are equivalent.
29 This template simply calls the operator== on the two values.
30 @param v1 A first value.
31 @param v2 A second value.
32 @return Whether the two values are equivalent. */
33 template <class T>
IsEquivalent(const T & v1,const T & v2)34 bool IsEquivalent(const T& v1, const T& v2) { return v1 == v2; }
35
36 // Includes the improved vector class.
37 #ifndef _FM_ARRAY_H_
38 #include "FMath/FMArray.h"
39 #endif // _FM_ARRAY_H_
40 #ifndef _FM_ARRAY_POINTER_H_
41 #include "FMath/FMArrayPointer.h"
42 #endif // _FM_ARRAY_POINTER_H_
43 #ifndef _FM_TREE_H_
44 #include "FMath/FMTree.h"
45 #endif // _FM_TREE_H_
46
47 /** A dynamically-sized array of double-sized floating-point values. */
48 typedef fm::vector<double, true> DoubleList;
49
50 /** A dynamically-sized array of floating-point values. */
51 typedef fm::vector<float, true> FloatList;
52
53 /** A dynamically-sized array of integer values. */
54 typedef fm::vector<int, true> IntList;
55
56
57 #ifndef _FM_INTEGER_H_
58 #include "FMath/FMInteger.h"
59 #endif // _FM_INTEGER_H_
60
61 /**
62 A namespace for common math functions.
63 @ingroup FMath
64 */
65 namespace FMath
66 {
67 /** Mathematical value of pi to 50 decimals. */
68 const double Pi = 3.14159265358979323846264338327950288419716939937510;
69 const float Pif = 3.14159265358979323846264338327950288419716939937510f; /**< See above. */
70
71 /** Numerical values for the different axis of the standard axis system.
72 You can use these enumerated types with the FMatrix44::GetAxis function. */
73 enum AXIS
74 {
75 X = 0, /**< The X-axis. */
76 Y, /**< The Y-axis. */
77 Z, /**< The Z-axis. */
78 W, /**< The W-axis. */
79
80 TRANS = W/**< The 3D translation value. */
81 };
82
83 /** Convert radians to degrees.
84 @param val The value in radians.
85 @return The value in degrees. */
RadToDeg(double val)86 inline double RadToDeg(double val) { return (val * 180.0/Pi); }
87
88 /** Convert radians to degrees.
89 @param val The value in radians.
90 @return The value in degrees. */
RadToDeg(float val)91 inline float RadToDeg(float val) { return (val * 180.0f/Pif); }
92
93 /** Convert degrees to radians.
94 @param val The value in degrees.
95 @return The value in radians. */
DegToRad(double val)96 inline double DegToRad(double val) { return (val * Pi/180.0); }
97
98 /** Convert degrees to radians.
99 @param val The value in degrees.
100 @return The value in radians. */
DegToRad(float val)101 inline float DegToRad(float val) { return (val * (float)Pi/180.0f); }
102
103 /** Determines if given float is encoding for not a number (NAN).
104 @param f The float to check.
105 @return 0 if it is a number, something else if is NAN. */
106 #ifdef WIN32
IsNotANumber(float f)107 inline int IsNotANumber(float f) { return _isnan(f); }
108 #elif defined(__PPU__) || defined(__APPLE__)
IsNotANumber(float f)109 inline int IsNotANumber(float f) { return !isfinite(f); }
110 #else // Linux
IsNotANumber(float f)111 inline int IsNotANumber(float f) { return !finite(f); }
112 #endif
113
114 /** Retrieves the sign of a number.
115 @param val The number to check.
116 @return 1 if positive, -1 if negative. */
117 template <class T>
Sign(const T & val)118 inline T Sign(const T& val) { return (val >= T(0)) ? T(1) : T(-1); }
119
120 /** Clamps the specified object within a range specified by two other objects
121 of the same class.
122 Clamping refers to setting a value within a given range. If the value is
123 lower than the minimum of the range, it is set to the minimum; same for
124 the maximum.
125 @param val The object to clamp.
126 @param mx The highest object of the range.
127 @param mn The lowest object of the range.
128 @return The clamped value. */
129 template <class T, class T2, class T3>
Clamp(T val,T2 mn,T3 mx)130 inline T Clamp(T val, T2 mn, T3 mx) { return (T) ((val > (T) mx) ? (T) mx : (val < (T) mn) ? (T) mn : val); }
131
132 /** Wraps the specified object within a range specified by two other objects
133 of the same class.
134 Wrapping refers to looping around maximum and minimum values until we're
135 within that range. e.g. Provided that mn = 0.0f, mx = 1.0f and val = 1.01f,
136 then the Wrap result will be 0.01f.
137 @param val The value to wrap.
138 @param mn The minimum value.
139 @param mx The maximum value (no sanity checks are made).
140 @return The wrapped value.*/
141 template <class T, class T2, class T3>
Wrap(T val,T2 mn,T3 mx)142 inline T Wrap(T val, T2 mn, T3 mx) { T d = (T)(mx - mn); while (val > (T)mx) val -= d; while (val < (T)mn) val += d; return val; }
143
144 /** Template specializations of the Wrap method. Optimizing for known primitives.
145 Feel free to add more supported primitives when needed.*/
146 template <>
Wrap(float val,float mn,float mx)147 inline float Wrap(float val, float mn, float mx) {
148 if (val > mx) return (mn + fmodf(val - mx, mx - mn));
149 else if (val < mn) return (mx - fmodf(mn - val, mx - mn));
150 return val; }
151 /** See above.*/
152 template <>
Wrap(double val,double mn,double mx)153 inline double Wrap(double val, double mn, double mx) {
154 if (val > mx) return (mn + fmod(val - mx, mx - mn));
155 else if (val < mn) return (mx - fmod(mn - val, mx - mn));
156 return val; }
157 /** See above.*/
158 template <>
Wrap(int val,int mn,int mx)159 inline int Wrap(int val, int mn, int mx) {
160 if (val > mx) return mn + ((val - mx) % (mx - mn));
161 else if (val < mn) return mx - ((mn - val) % (mx - mn));
162 return val; }
163 /** See above.*/
164 template <>
Wrap(uint32 val,uint32 mn,uint32 mx)165 inline uint32 Wrap(uint32 val, uint32 mn, uint32 mx) {
166 if (val > mx) return mn + ((val - mx) % (mx - mn));
167 else if (val < mn) return mx - ((mn - val) % (mx - mn));
168 return val; }
169 };
170
171 // Include commonly used mathematical classes
172 #include "FMath/FMVector2.h"
173 #include "FMath/FMVector3.h"
174 #include "FMath/FMVector4.h"
175 #include "FMath/FMColor.h"
176 #include "FMath/FMMatrix33.h"
177 #include "FMath/FMMatrix44.h"
178
179 #endif // _F_MATH_H_
180