Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Math.h

Go to the documentation of this file.
00001 /* SimData: Data Infrastructure for Simulations
00002  * Copyright (C) 2002 Mark Rose <tm2@stm.lbl.gov>
00003  * 
00004  * This file is part of SimData.
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 /*
00022  * @file Math.h
00023  *
00024  * Math related utility functions and constants.
00025  *
00026  * Numerous utility functions implemented here are based on 
00027  * the Math header included in the OpenSceneGraph library, 
00028  * Copyright 1998-2003 Robert Osfield.  Source code from 
00029  * OpenSceneGraph is used here under the GNU General Public 
00030  * License Version 2 or later, as permitted under the 
00031  * OpenSceneGraph Public License Version 0.0 (exception 3) 
00032  * and the GNU Lesser Public License Version 2 (clause 3).
00033  **/
00034 
00035 #ifndef __SIMDATA_MATH_H__
00036 #define __SIMDATA_MATH_H__
00037 
00038 #include <SimData/Namespace.h>
00039 
00040 #include <cmath>
00041 
00042 #if defined(_MSC_VER) && (_MSC_VER >= 1300)
00043     #include <float.h>
00044 #endif
00045 
00046 
00047 
00048 NAMESPACE_SIMDATA
00049 
00050 class Vector3;
00051 
00052 #ifdef PI
00053 #undef PI
00054 #undef PI_2
00055 #undef PI_4
00056 #endif
00057 const double PI   = 3.14159265358979323846;
00058 const double PI_2 = 1.57079632679489661923;
00059 const double PI_4 = 0.78539816339744830962;
00060 
00063 template<typename T>
00064 inline T absolute(T v) { return v<(T)0?-v:v; }
00065 
00068 template<typename T>
00069 inline T minimum(T lhs,T rhs) { return lhs<rhs?lhs:rhs; }
00070 
00073 template<typename T>
00074 inline T maximum(T lhs,T rhs) { return lhs>rhs?lhs:rhs; }
00075 
00076 template<typename T>
00077 inline T clampTo(T v,T minimum,T maximum) { return v<minimum?minimum:v>maximum?maximum:v; }
00078 
00079 template<typename T>
00080 inline T clampAbove(T v,T minimum) { return v<minimum?minimum:v; }
00081 
00082 template<typename T>
00083 inline T clampBelow(T v,T maximum) { return v>maximum?maximum:v; }
00084 
00085 template<typename T>
00086 inline T sign(T v) { return v<(T)0?(T)-1:(T)1; }
00087 
00088 template<typename T>
00089 inline T square(T v) { return v*v; }
00090 
00091 template<typename T>
00092 inline T signedSquare(T v) { return v<(T)0?-v*v:v*v;; }
00093 
00094 template<typename T>
00095 inline T toRadians(T deg) { return deg * PI / ((T)180); }
00096 
00097 template<typename T>
00098 inline T toDegrees(T rad) { return rad * ((T)180) / PI; }
00099 
00100 template<typename T>
00101 inline T inDegrees(T angle) { return toRadians(angle); }
00102 
00103 template<typename T>
00104 inline T inRadians(T angle) { return angle; }
00105 
00106 template<typename T>
00107 inline void swap(T &a, T &b) { T t = a; a = b; b = t; }
00108 
00109 
00110 #if (defined(WIN32) && !(defined(_MSC_VER) && (_MSC_VER >= 1300)) && !defined(__MINGW32__) ) ||  defined (sun)
00111     #ifndef isnanf
00112     #define isnanf (float)isnan
00113     #endif
00114 #endif
00115 
00116 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__MWERKS__)
00117     inline bool isNaN(float v) { return _isnan(v)!=0; }
00118     inline bool isNaN(double v) { return _isnan(v)!=0; }
00119 #else
00120     # if defined(__DARWIN_OSX__) && !defined (DARWIN_OSX_PRE_10_2)
00121         inline bool isNaN(float v) { return __isnanf(v); }
00122         inline bool isNaN(double v) { return __isnand(v); }
00123     #else
00124         inline bool isNaN(float v) { return isnan(v); }
00125         inline bool isNaN(double v) { return isnan(v); }
00126     #endif
00127 #endif
00128 
00129 
00130 NAMESPACE_SIMDATA_END
00131 
00132 #endif //__SIMDATA_MATH_H__

SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage.

Generated on Tue Oct 14 12:06:39 2003, using Doxygen 1.2.18.

[SF.net]