source: NonGTP/FCollada/FMath/FMath.h @ 964

Revision 964, 3.2 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*
2        Copyright (C) 2005-2006 Feeling Software Inc.
3        MIT License: http://www.opensource.org/licenses/mit-license.php
4*/
5
6/**
7        @file FMath.h
8        The file containing functions and constants for math.
9
10        @defgroup FMath Mathematics Classes.
11*/
12
13#ifndef _F_MATH_H_
14#define _F_MATH_H_
15
16#include <math.h>
17#include <algorithm>
18
19// Includes the improved vector class.
20#include "FMath/FMArray.h"
21
22// Includes the common integer and floating-point definitions and functions.
23#include "FMath/FMFloat.h"
24#include "FMath/FMInteger.h"
25
26/**
27        A namespace for common math functions.
28
29        @ingroup FMath
30*/
31namespace FMath
32{
33        const double Pi = 3.14159;      /**< Mathematical value of pi to 5 decimals. */
34
35        /**
36         * Convert radians to degrees.
37         *
38         * @param val The value in radians.
39         * @return The value in degrees.
40         */
41        inline double RadToDeg(double val) { return (val * 180.0/Pi); }
42
43        /**
44         * Convert radians to degrees.
45         *
46         * @param val The value in radians.
47         * @return The value in degrees.
48         */
49        inline float RadToDeg(float val) { return (val * 180.0f/(float)Pi); }
50
51        /**
52         * Convert degrees to radians.
53         *
54         * @param val The value in degrees.
55         * @return The value in radians.
56         */
57        inline double DegToRad(double val) { return (val * Pi/180.0); }
58
59        /**
60         * Convert degrees to radians.
61         *
62         * @param val The value in degrees.
63         * @return The value in radians.
64         */
65        inline float DegToRad(float val) { return (val * (float)Pi/180.0f); }
66
67/**
68 * Determines if given float is encoding for not a number (NAN).
69 *
70 * @param f The float to check.
71 * @return 0 if it is a number, something else if is NAN.
72 */
73#ifdef WIN32
74        inline int IsNotANumber(float f) { return _isnan(f); }
75#elif __PPU__
76        inline int IsNotANumber(float f) { return !isfinite(f); }
77#else // Linux and Mac
78        inline int IsNotANumber(float f) { return !finite(f); }
79#endif
80
81        /**
82         * Determine the sign of a number.
83         *
84         * @param val The number to check.
85         * @return 1.0 if positive, -1.0 if negative.
86         */
87        inline double Sign(double val) { return (val >= 0.0) ? 1.0 : -1.0; }
88
89        /**
90         * Determine the sign of a number.
91         *
92         * @param val The number to check.
93         * @return 1.0f if positive, -1.0f if negative.
94         */
95        inline float Sign(float val) { return (val >= 0.0f) ? 1.0f : -1.0f; }
96
97        /**
98         * Determine the sign of a number.
99         *
100         * @param val The number to check.
101         * @return 1 if positive, -1 if negative.
102         */
103        inline int32 Sign(int32 val) { return (val >= 0) ? 1 : -1; }
104
105        /**
106         * Clamp the specified object within a range specified by two other objects
107         * of the same class.
108         *
109         * Clamp refers to setting a value within a given range. If the value is
110         * lower than the minimum of the range, it is set to the minimum; same for
111         * the maximum.
112         *
113         * @param val The object to clamp.
114         * @param mx The highest object of the range.
115         * @param mn The lowest object of the range.
116         * @return The clamped value.
117         */
118        template <class T>
119        inline T Clamp(T val, T mn, T mx) { return std::max(std::min(val, mx), mn); }
120};
121
122// Include commonly used mathematical classes
123#include "FMath/FMVector2.h"
124#include "FMath/FMVector3.h"
125#include "FMath/FMVector4.h"
126#include "FMath/FMColor.h"
127#include "FMath/FMMatrix33.h"
128#include "FMath/FMMatrix44.h"
129
130#endif // _F_MATH_H_
Note: See TracBrowser for help on using the repository browser.