WFMath  1.0.1
const.h
00001 // const.h (Defined constants for the WFMath library)
00002 //
00003 //  The WorldForge Project
00004 //  Copyright (C) 2001, 2002  The WorldForge Project
00005 //
00006 //  This program is free software; you can redistribute it and/or modify
00007 //  it under the terms of the GNU General Public License as published by
00008 //  the Free Software Foundation; either version 2 of the License, or
00009 //  (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., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 //
00020 //  For information about WorldForge and its authors, please contact
00021 //  the Worldforge Web Site at http://www.worldforge.org.
00022 
00023 // Author: Ron Steinke
00024 // Created: 2001-12-7
00025 
00026 #ifndef WFMATH_CONST_H
00027 #define WFMATH_CONST_H
00028 
00029 #include <limits>
00030 
00031 #ifdef _MSC_VER
00032   #if _MSC_VER < 1500
00033     #error "You are using an older version of MSVC++ with extremely poor"
00034     #error "template support. Please use at least version 2008,"
00035     #error "or try a different compiler."
00036   #endif
00037 #endif
00038 
00040 namespace WFMath {
00041 
00042 // WFMath::Foo::toAtlas() has to return a definite type,
00043 // we deal with supporting both 0.4 and 0.6 by forward declaring
00044 // types which we define in the AtlasConv header
00045 class AtlasInType;
00046 class AtlasOutType;
00047 
00048 template<int dim> class AxisBox;
00049 template<int dim> class Ball;
00050 template<int dim> class Point;
00051 template<int dim> class Polygon;
00052 template<int dim> class RotBox;
00053 template<int dim> class RotMatrix;
00054 template<int dim> class Segment;
00055 template<int dim> class Vector;
00056 class Quaternion;
00057 
00058 // Constants
00059 
00061 #define WFMATH_PRECISION_FUDGE_FACTOR 30
00062 
00063 template<typename FloatType>
00064 struct numeric_constants
00065 {
00067   static FloatType pi();
00069   static FloatType sqrt_pi();
00071   static FloatType log_pi();
00073   static FloatType sqrt2();
00075   static FloatType sqrt3();
00077   static FloatType log2();
00079   static FloatType epsilon();
00080 };
00081 
00082 template<>
00083 struct numeric_constants<float>
00084 {
00085   static float pi() {
00086     return 3.14159265358979323846264338327950288419716939937508F;
00087   }
00088   static float sqrt_pi() {
00089     return 1.77245385090551602729816748334114518279754945612237F;
00090   }
00091   static float log_pi() {
00092     return 1.14472988584940017414342735135305871164729481291530F;
00093   }
00094   static float sqrt2() {
00095     return 1.41421356237309504880168872420969807856967187537693F;
00096   }
00097   static float sqrt3() {
00098     return 1.73205080756887729352744634150587236694280525381037F;
00099   }
00100   static float log2() {
00101     return 0.69314718055994530941723212145817656807550013436025F;
00102   }
00103   static float epsilon() {
00104     return (WFMATH_PRECISION_FUDGE_FACTOR *
00105             std::numeric_limits<float>::epsilon());
00106   }
00107 };
00108 
00109 template<>
00110 struct numeric_constants<double>
00111 {
00112   static double pi() {
00113     return 3.14159265358979323846264338327950288419716939937508;
00114   }
00115   static double sqrt_pi() {
00116     return 1.77245385090551602729816748334114518279754945612237;
00117   }
00118   static double log_pi() {
00119     return 1.14472988584940017414342735135305871164729481291530;
00120   }
00121   static double sqrt2() {
00122     return 1.41421356237309504880168872420969807856967187537693;
00123   }
00124   static double sqrt3() {
00125     return 1.73205080756887729352744634150587236694280525381037;
00126   }
00127   static double log2() {
00128     return 0.69314718055994530941723212145817656807550013436025;
00129   }
00130   static double epsilon() {
00131     return (WFMATH_PRECISION_FUDGE_FACTOR *
00132             std::numeric_limits<double>::epsilon());
00133   }
00134 };
00135 
00137 #define WFMATH_MAX_NORM_AGE ((WFMATH_PRECISION_FUDGE_FACTOR * 2) / 3)
00138 
00140 typedef float CoordType;
00141 
00142 // Basic comparisons
00143 
00144 double _ScaleEpsilon(double x1, double x2, double epsilon);
00145 float _ScaleEpsilon(float x1, float x2, float epsilon);
00146 CoordType _ScaleEpsilon(const CoordType* x1, const CoordType* x2,
00147                         int length, CoordType epsilon = numeric_constants<CoordType>::epsilon());
00148 
00150 
00157 template<class C>
00158 inline bool Equal(const C& c1, const C& c2, CoordType epsilon = numeric_constants<CoordType>::epsilon())
00159         {return c1.isEqualTo(c2, epsilon);}
00160 
00161 bool Equal(double x1, double x2, double epsilon = numeric_constants<double>::epsilon());
00162 // Avoid template and expensive casts from float to doubles.
00163 bool Equal(float x1, float x2, float epsilon = numeric_constants<float>::epsilon());
00164 
00165 // These let us avoid including <algorithm> for the sake of
00166 // std::max() and std::min().
00167 
00168 inline CoordType FloatMax(CoordType a, CoordType b)
00169         {return (a > b) ? a : b;}
00170 inline CoordType FloatMin(CoordType a, CoordType b)
00171         {return (a < b) ? a : b;}
00172 inline CoordType FloatClamp(CoordType val, CoordType min, CoordType max)
00173         {return (min >= val) ? min : (max <= val ? max : val);}
00174 
00175 inline double DoubleMax(double a, double b)
00176         {return (a > b) ? a : b;}
00177 inline double DoubleMin(double a, double b)
00178         {return (a < b) ? a : b;}
00179 inline double DoubleClamp(double val, double min, double max)
00180         {return (min >= val) ? min : (max <= val ? max : val);}
00181 
00182 } // namespace WFMath
00183 
00184 #endif // WFMATH_CONST_H