mathutils.cpp

Go to the documentation of this file.
00001 /*
00002  *  The Mana World Server
00003  *  Copyright 2004 The Mana World Development Team
00004  *
00005  *  This file is part of The Mana World.
00006  *
00007  *  The Mana World  is free software; you can redistribute  it and/or modify it
00008  *  under the terms of the GNU General  Public License as published by the Free
00009  *  Software Foundation; either version 2 of the License, or any later version.
00010  *
00011  *  The Mana  World is  distributed in  the hope  that it  will be  useful, but
00012  *  WITHOUT ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY
00013  *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00014  *  more details.
00015  *
00016  *  You should  have received a  copy of the  GNU General Public  License along
00017  *  with The Mana  World; if not, write to the  Free Software Foundation, Inc.,
00018  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019  *
00020  *  $Id: mathutils.cpp 3165 2007-03-03 12:02:41Z b_lindeijer $
00021  */
00022 
00023 #include "mathutils.h"
00024 
00025 #include <cmath>
00026 #include <float.h>
00027 
00028 #define MATH_UTILS_MAX_ANGLE 360
00029 
00030 float sinList[MATH_UTILS_MAX_ANGLE];
00031 float cosList[MATH_UTILS_MAX_ANGLE];
00032 float tanList[MATH_UTILS_MAX_ANGLE];
00033 
00034 /*
00035  * A very fast function to calculate the approximate inverse square root of a
00036  * floating point value. For an explanation of the inverse squareroot function
00037  * read:
00038  * http://www.math.purdue.edu/~clomont/Math/Papers/2003/InvSqrt.pdf
00039  *
00040  * Unfortunately the original creator of this function seems to be unknown.
00041  *
00042  * TODO: - Make this function run on 64-bit architectures
00043  *       - Find out and fix why this function fails when compiled with the -O2
00044  *         optimization flag on
00045  *         gcc version 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9).
00046  */
00047 float utils::math::fastInvSqrt(float x)
00048 {
00049     float xhalf = 0.5f * x;
00050     int i = *(int*) &x;
00051     i = 0x5f375a86 - (i >> 1);
00052     x = *(float*) &i;
00053     x = x * (1.5f-xhalf * x * x);
00054     return x;
00055 }
00056 
00057 float utils::math::fastSqrt(float x)
00058 {
00059     return 1.0f / utils::math::fastInvSqrt(x);
00060 }
00061 
00062 void utils::math::init()
00063 {
00064     // Constant for calculating an angle in radians out of an angle in degrees
00065     const float radianAngleRatio = M_PI_2 / 90.0f; // pi/2 / 90[deg]
00066 
00067     for (int i = 0; i < MATH_UTILS_MAX_ANGLE; i++)
00068     {
00069         sinList[i] = sin(radianAngleRatio * (float) i);
00070         cosList[i] = cos(radianAngleRatio * (float) i);
00071 
00072         if (i == 90)
00073         {
00074             tanList[i] = FLT_MAX; // approximately infinity
00075             continue;
00076         }
00077         if (i == 270)
00078         {
00079             tanList[i] = -FLT_MAX; // approximately infinity
00080             continue;
00081         }
00082         tanList[i] = tan(radianAngleRatio * (float) i);
00083     }
00084 }
00085 
00086 float utils::math::cachedSin(int angle)
00087 {
00088     return sinList[angle];
00089 }
00090 
00091 float utils::math::cachedCos(int angle)
00092 {
00093     return cosList[angle];
00094 }
00095 
00096 float utils::math::cachedTan(int angle)
00097 {
00098     return tanList[angle];
00099 }

Generated on Fri Mar 30 15:39:16 2007 for TMW Server by  doxygen 1.3.9.1