wfmath 1.0.3
A math library for the Worldforge system.
const.h
1// const.h (Defined constants for the WFMath library)
2//
3// The WorldForge Project
4// Copyright (C) 2001, 2002 The WorldForge Project
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19//
20// For information about WorldForge and its authors, please contact
21// the Worldforge Web Site at http://www.worldforge.org.
22
23// Author: Ron Steinke
24// Created: 2001-12-7
25
26#ifndef WFMATH_CONST_H
27#define WFMATH_CONST_H
28
29#include <limits>
30
31#ifdef _MSC_VER
32 #if _MSC_VER < 1500
33 #error "You are using an older version of MSVC++ with extremely poor"
34 #error "template support. Please use at least version 2008,"
35 #error "or try a different compiler."
36 #endif
37#endif
38
40namespace WFMath {
41
42// WFMath::Foo::toAtlas() has to return a definite type,
43// we deal with supporting both 0.4 and 0.6 by forward declaring
44// types which we define in the AtlasConv header
45class AtlasInType;
46class AtlasOutType;
47
48template<int dim> class AxisBox;
49template<int dim> class Ball;
50template<int dim> class Point;
51template<int dim> class Polygon;
52template<int dim> class RotBox;
53template<int dim> class RotMatrix;
54template<int dim> class Segment;
55template<int dim> class Vector;
56class Quaternion;
57
58// Constants
59
61#define WFMATH_PRECISION_FUDGE_FACTOR 30
62
63template<typename FloatType>
65{
67 static FloatType pi();
69 static FloatType sqrt_pi();
71 static FloatType log_pi();
73 static FloatType sqrt2();
75 static FloatType sqrt3();
77 static FloatType log2();
79 static FloatType epsilon();
80};
81
82template<>
83struct numeric_constants<float>
84{
85 static float pi() {
86 return 3.14159265358979323846264338327950288419716939937508F;
87 }
88 static float sqrt_pi() {
89 return 1.77245385090551602729816748334114518279754945612237F;
90 }
91 static float log_pi() {
92 return 1.14472988584940017414342735135305871164729481291530F;
93 }
94 static float sqrt2() {
95 return 1.41421356237309504880168872420969807856967187537693F;
96 }
97 static float sqrt3() {
98 return 1.73205080756887729352744634150587236694280525381037F;
99 }
100 static float log2() {
101 return 0.69314718055994530941723212145817656807550013436025F;
102 }
103 static float epsilon() {
104 return (WFMATH_PRECISION_FUDGE_FACTOR *
105 std::numeric_limits<float>::epsilon());
106 }
107};
108
109template<>
110struct numeric_constants<double>
111{
112 static double pi() {
113 return 3.14159265358979323846264338327950288419716939937508;
114 }
115 static double sqrt_pi() {
116 return 1.77245385090551602729816748334114518279754945612237;
117 }
118 static double log_pi() {
119 return 1.14472988584940017414342735135305871164729481291530;
120 }
121 static double sqrt2() {
122 return 1.41421356237309504880168872420969807856967187537693;
123 }
124 static double sqrt3() {
125 return 1.73205080756887729352744634150587236694280525381037;
126 }
127 static double log2() {
128 return 0.69314718055994530941723212145817656807550013436025;
129 }
130 static double epsilon() {
131 return (WFMATH_PRECISION_FUDGE_FACTOR *
132 std::numeric_limits<double>::epsilon());
133 }
134};
135
137#define WFMATH_MAX_NORM_AGE ((WFMATH_PRECISION_FUDGE_FACTOR * 2) / 3)
138
140typedef double CoordType;
141
142// Basic comparisons
143
144double _ScaleEpsilon(double x1, double x2, double epsilon);
145float _ScaleEpsilon(float x1, float x2, float epsilon);
146CoordType _ScaleEpsilon(const CoordType* x1, const CoordType* x2,
147 int length, CoordType epsilon = numeric_constants<CoordType>::epsilon());
148
150
157template<class C>
158inline bool Equal(const C& c1, const C& c2, CoordType epsilon = numeric_constants<CoordType>::epsilon())
159 {return c1.isEqualTo(c2, epsilon);}
160
161bool Equal(double x1, double x2, double epsilon = numeric_constants<double>::epsilon());
162// Avoid template and expensive casts from float to doubles.
163bool Equal(float x1, float x2, float epsilon = numeric_constants<float>::epsilon());
164
165// These let us avoid including <algorithm> for the sake of
166// std::max() and std::min().
167
168inline CoordType FloatMax(CoordType a, CoordType b)
169 {return (a > b) ? a : b;}
170inline CoordType FloatMin(CoordType a, CoordType b)
171 {return (a < b) ? a : b;}
172inline CoordType FloatClamp(CoordType val, CoordType min, CoordType max)
173 {return (min >= val) ? min : (max <= val ? max : val);}
174
175inline double DoubleMax(double a, double b)
176 {return (a > b) ? a : b;}
177inline double DoubleMin(double a, double b)
178 {return (a < b) ? a : b;}
179inline double DoubleClamp(double val, double min, double max)
180 {return (min >= val) ? min : (max <= val ? max : val);}
181
182} // namespace WFMath
183
184#endif // WFMATH_CONST_H
Generic library namespace.
Definition: shape.h:41
double CoordType
Basic floating point type.
Definition: const.h:140
static FloatType pi()
The constant pi.
static FloatType log2()
The natural logarithm of 2.
static FloatType log_pi()
The natural logarithm of pi.
static FloatType sqrt2()
The square root of 2.
static FloatType sqrt_pi()
The square root of pi.
static FloatType sqrt3()
The square root of 3.
static FloatType epsilon()
This is the attempted precision of the library.