Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
opmath.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 by Marten Svanfeldt
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public
15  License along with this library; if not, write to the Free
16  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 #ifndef __CS_MATH_H__
20 #define __CS_MATH_H__
21 
22 
23 #define HAVE_ISNORMAL 1
24 
25 
26 #include "config.h"
27 #include <math.h>
28 #include <float.h>
30 /*
31 inline bool isfinited (float x) {return((x) >= -DBL_MAX && (x) <= DBL_MAX);}
32 inline bool isfinitef (float x) {return((x) >= -FLT_MAX && (x) <= FLT_MAX);}
33 
34 inline float isfinite (float f) { return (sizeof(f) == sizeof(float) ? isfinitef(f) : isfinited(f));}
35 */
46 template<class T>
47 const T& csMax (const T& a, const T& b)
48 {
49  if (b < a) return a;
50  return b;
51 }
52 
56 template<class T>
57 const T& csMin (const T& a, const T& b)
58 {
59  if (a < b) return a;
60  return b;
61 }
62 
66 template<class T>
67 void csSort (T& a, T& b)
68 {
69  if (b < a)
70  CS::Swap (a, b);
71 }
72 
77 template<class T, class U>
78 void csSort (T& a, T& b, U& x, U& y)
79 {
80  if (b < a)
81  {
82  CS::Swap (a, b);
83  CS::Swap (x, y);
84  }
85 }
86 
87 
91 template<class T>
92 T csClamp (const T& a, T max, T min)
93 {
94  return csMin (csMax (a, min), max);
95 }
96 
102 template<class T>
103 T csSmoothStep (const T& a, T max, T min)
104 {
105  T tmp, tmp2;
106  if (a <= min)
107  tmp = 0.0f;
108  else if (a >= max)
109  tmp = 1.0f;
110  else
111  {
112  tmp2 = (a - min) / (max-min);
113  tmp = tmp2*tmp2 * (3.0 - 2.0*tmp2);
114  }
115  return tmp;
116 }
117 
122 template<class T, class Tfactor>
123 T csLerp (const T& a, const T& b, const Tfactor& f)
124 {
125  return (a + (b - a) * f);
126 }
127 
131 template<class T>
132 T csSquare (const T& x)
133 {
134  return x * x;
135 }
136 
138 CS_FORCEINLINE bool csFinite (float f)
140 {
141 #if defined (HAVE_FINITEF)
142  return finitef (f);
143 #elif defined (HAVE_STD__ISFINITE)
144  return std::isfinite (f);
145 #elif defined(HAVE_ISFINITE)
146  return (isfinite (f));
147 #elif defined (HAVE_FINITE)
148  return finite (f);
149 #elif defined (HAVE__FINITE)
150  return _finite (f) != 0;
151 #else
152 #error Your platform has no isfinite()-alike function!
153 #endif
154 }
156 CS_FORCEINLINE bool csFinite (double d)
157 {
158 #if defined (HAVE_STD__ISFINITE)
159  return std::isfinite (d);
160 #elif defined(HAVE_ISFINITE)
161  return isfinite (d);
162 #elif defined (HAVE_FINITE)
163  return finite (d);
164 #elif defined (HAVE__FINITE)
165  return _finite (d) != 0;
166 #else
167 #error Your platform has no isfinite()-alike function!
168 #endif
169 }
170 
172 CS_FORCEINLINE bool csNaN (float f)
173 {
174 #if defined (HAVE_NANF)
175  return isnanf (f);
176 #elif defined (HAVE_STD__ISNAN)
177  return std::isnan (f);
178 #elif defined(HAVE_ISNAN)
179  return isnan (f);
180 #elif defined (HAVE__ISNAN)
181  return _isnan (f) != 0;
182 #else
183 #error Your platform has no isnan()-alike function!
184 #endif
185 }
187 CS_FORCEINLINE bool csNaN (double d)
188 {
189 #if defined (HAVE_STD__ISNAN)
190  return std::isnan (d);
191 #elif defined(HAVE_ISNAN)
192  return isnan (d);
193 #elif defined (HAVE__ISNAN)
194  return _isnan (d) != 0;
195 #else
196 #error Your platform has no isnan()-alike function!
197 #endif
198 }
199 
201 CS_FORCEINLINE bool csNormal (float f)
202 {
203 #if defined (HAVE_NORMALF)
204  return normalf (f);
205 #elif defined (HAVE_STD__ISNORMAL)
206  return std::isnormal (f);
207 #elif defined(HAVE_ISNORMAL)
208  return isnormal (f);
209 #else
210  return csFinite(f) && !csNaN(f);
211 #endif
212 }
214 CS_FORCEINLINE bool csNormal (double d)
215 {
216 #if defined (HAVE_STD__ISNORMAL)
217  return std::isnormal (d);
218 #elif defined(HAVE_ISNORMAL)
219  return isnormal (d);
220 #else
221  return csFinite(d) && !csNaN(d);
222 #endif
223 }
225 
228 #endif //__CS_MATH_H__