Mana
Loading...
Searching...
No Matches
mathutils.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24/* A very fast function to calculate the approximate inverse square root of a
25 * floating point value and a helper function that uses it for getting the
26 * normal squareroot. For an explanation of the inverse squareroot function
27 * read:
28 * http://www.math.purdue.edu/~clomont/Math/Papers/2003/InvSqrt.pdf
29 *
30 * Unfortunately the original creator of this function seems to be unknown.
31 */
32
33inline float fastInvSqrt(float x)
34{
35 union { int i; float x; } tmp;
36 float xhalf = 0.5f * x;
37 tmp.x = x;
38 tmp.i = 0x5f375a86 - (tmp.i >> 1);
39 x = tmp.x;
40 x = x * (1.5f - xhalf * x * x);
41 return x;
42}
43
44inline float fastSqrt(float x)
45{
46 return 1.0f / fastInvSqrt(x);
47}
48
49inline float weightedAverage(float n1, float n2, float w)
50{
51 if (w < 0.0f)
52 return n1;
53
54 if (w > 1.0f)
55 return n2;
56
57 return w * n2 + (1.0f - w) * n1;
58}
float weightedAverage(float n1, float n2, float w)
Definition mathutils.h:49
float fastInvSqrt(float x)
Definition mathutils.h:33
float fastSqrt(float x)
Definition mathutils.h:44