1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
Revision 1.21 2007/01/08 09:23:49 pietroni
added explicit cast in function inline float Sqrt(const int v) in order to avoid warnings
Revision 1.20 2006/10/13 13:14:50 cignoni
Added two sqrt templates for resolving ambiguity of sqrt(int)
Revision 1.19 2005/12/01 01:03:37 cignoni
Removed excess ';' from end of template functions, for gcc compiling
Revision 1.18 2004/08/31 15:42:59 fasano
Aggiunte macro sin/cos/atan per C++ Builder
Revision 1.17 2004/05/10 13:00:14 ganovelli
limits function cancelled
Revision 1.16 2004/05/03 08:38:08 ganovelli
correction on templates
Revision 1.15 2004/04/15 09:36:59 ganovelli
Min and Max changed from const members to static class function
Use: Value<float>::Min()
Revision 1.14 2004/03/31 12:41:55 tarini
debugged Max and Min const values (to make them linkable)
Revision 1.13 2004/03/31 10:09:19 cignoni
int64 -> long long for GCC compatibility
Revision 1.12 2004/03/16 00:23:50 tarini
- added VoidType - added "static_assert"
Revision 1.11 2004/03/10 17:37:54 tarini
Added Atan2.
Added common utilities: Max, Min, Swap, Sort(a,b), Sort(a,b,c).
Changed Max values syntax. example: Value<float>::Max
Revision 1.10 2004/03/10 16:54:57 tarini
Added Atan2.
Added common utilities: Max, Min, Swap, Sort(a,b), Sort(a,b,c).
Changed Max values syntax. example: Value<float>::Max
Revision 1.7 2004/03/08 19:38:29 tarini
Added Min e Max. usage: Min<float>::Value (tarini)
Revision 1.6 2004/03/08 14:49:37 ponchio
Aggiunti un po di inline davanti alle funzioni
Revision 1.5 2004/03/04 00:21:00 ponchio
added Acos e Asin
Revision 1.4 2004/03/03 22:51:49 cignoni
changed math from class to template
Revision 1.2 2004/02/13 02:18:57 cignoni
Edited Comments and GPL license
****************************************************************************/
#ifndef __VCGLIB_MATH_BASE
#define __VCGLIB_MATH_BASE
#include <math.h>
#include <assert.h>
#include <limits>
#include <math.h>
/// static_assert: implemented as a macro for "assert", but it is separated for clarity.
/// Should be used for checking integrity constraints that can be tested at complile time,
/// as the ones involving templated constants in templated classes.
#define static_assert assert
#ifdef __BORLANDC__
float sqrtf (float v) {return sqrt(v);}
float fabsf (float v) {return fabs(v);}
float cosf (float v) {return cos(v);}
float sinf (float v) {return sin(v);}
float acosf (float v) {return acos(v);}
float asinf (float v) {return asin(v);}
float atan2f (float v0, float v1) {return atan2(v0,v1);}
#endif
namespace vcg {
namespace math {
template <class SCALAR>
class MagnitudoComparer
{
public:
inline bool operator() ( const SCALAR a, const SCALAR b ) { return fabs(a)>fabs(b); }
};
inline float Sqrt(const short v) { return sqrtf(v); }
inline float Sqrt(const int v) { return sqrtf((float)v); }
inline float Sqrt(const float v) { return sqrtf(v); }
inline float Abs(const float v) { return fabsf(v); }
inline float Cos(const float v) { return cosf(v); }
inline float Sin(const float v) { return sinf(v); }
inline float Acos(const float v) { return acosf(v); }
inline float Asin(const float v) { return asinf(v); }
inline float Atan2(const float v0,const float v1) { return atan2f(v0,v1); }
inline double Sqrt(const double v) { return sqrt(v); }
inline double Abs(const double v) { return fabs(v); }
inline double Cos(const double v) { return cos(v); }
inline double Sin(const double v) { return sin(v); }
inline double Acos(const double v) { return acos(v); }
inline double Asin(const double v) { return asin(v); }
inline double Atan2(const double v0,const double v1) { return atan2(v0,v1); }
template <typename T> inline static T Sqr(T a) { return a*a; }
template<class T> inline const T & Min(const T &a, const T &b,const T &c){
if (a<b) {
if(a<c) return a;
else return c;
} else {
if(b<c) return b;
else return c;
}
}
template<class T> inline const T & Max(const T &a, const T &b, const T &c){
if (a>b) {
if(a>c) return a;
else return c; // if c<a then c is smaller than b...
} else {
if(b>c) return b;
else return c;
}
}
template<class T> inline void Swap(T &a, T &b){
T tmp=a; a=b; b=tmp;
}
template<class T> inline void Sort(T &a, T &b){
if (a>b) Swap(a,b);
}
template<class T> inline void Sort(T &a, T &b, T &c){
if (a>b) Swap(a,b);
if (b>c) {Swap(b,c); if (a>b) Swap(a,b);}
}
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef SQRT_TWO
#define SQRT_TWO 1.4142135623730950488
#endif
template <class SCALAR>
inline SCALAR Clamp( const SCALAR & val, const SCALAR& minval, const SCALAR& maxval)
{
if(val < minval) return minval;
if(val > maxval) return maxval;
return val;
}
inline float ToDeg(const float &a){return a*180.0f/float(M_PI);}
inline float ToRad(const float &a){return float(M_PI)*a/180.0f;}
inline double ToDeg(const double &a){return a*180.0/M_PI;}
inline double ToRad(const double &a){return M_PI*a/180.0;}
#if defined(_MSC_VER) // Microsoft Visual C++
template<class T> int IsNAN(T t) { return _isnan(t); }
#elif defined(__GNUC__) // GCC
template<class T> int IsNAN(T t) { return isnan(t); }
#else // generic
template<class T> int IsNAN(T t)
{
if(std::numeric_limits<T>::has_infinity)
return !(t <= std::numeric_limits<T>::infinity());
else
return t != t;
}
#endif
} // End math namespace
/// a type that stands for "void". Useful for Parameter type of a point.
class VoidType{ public:
VoidType(){};
};
} // End vcg namespace
#endif
|