TrueReality  v0.1.1912
Math.h
Go to the documentation of this file.
1 /*
2  * True Reality Open Source Game and Simulation Engine
3  * Copyright © 2021 Acid Rain Studios LLC
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License as published by the Free
7  * Software Foundation; either version 3.0 of the License, or (at your option)
8  * any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * @author Maxim Serebrennik
20  */
21 
22 #pragma once
23 
24 #include <trUtil/Export.h>
25 
26 #include <osg/Math>
27 
28 namespace trUtil::Math
29 {
31  static const double PI = osg::PI;
33  static const double PI_OVER_2 = osg::PI_2;
35  static const double PI_OVER_4 = osg::PI_4;
37  static const double TWO_PI = trUtil::Math::PI * 2.0;
39  static const double FOUR_PI = trUtil::Math::PI * 4.0;
41  static const double SQRT_PI = 1.77245385090551602729816748334;
43  static const double E = 2.7182818284590452354;
45  static const double LOG_OF_2 = osg::LN_2;
47  static const double INV_LOG_OF_2 = osg::INVLN_2;
49  static const double ONE_OVER_PI = 1.0 / trUtil::Math::PI;
51  static const double ONE80_OVER_PI = (180.0*ONE_OVER_PI);
52 
62  inline void SinCos(double x, double& sinVal, double& cosVal)
63  {
64  sinVal = sin(x);
65  cosVal = cos(x);
66  }
67 
77  inline void SinCos(float x, float& sinVal, float& cosVal)
78  {
79  sinVal = sin(x);
80  cosVal = cos(x); // Might benefit from trig identities
81  }
82 
92  inline double Deg2Rad(double degree)
93  {
94  return osg::DegreesToRadians(degree);
95  }
96 
106  inline float Deg2Rad(float degree)
107  {
108  return osg::DegreesToRadians(degree);
109  }
110 
120  inline double Rad2Deg(double radian)
121  {
122  return osg::RadiansToDegrees(radian);
123  }
124 
134  inline float Rad2Deg(float radian)
135  {
136  return osg::RadiansToDegrees(radian);
137  }
138 
148  inline double CheckState(double state)
149  {
150  if (state == 0)
151  return 0;
152  else if (state < 0)
153  return -1;
154  else
155  return 1;
156  }
157 
167  inline float CheckState(float state)
168  {
169  if (state == 0)
170  return 0;
171  else if (state < 0)
172  return -1;
173  else
174  return 1;
175  }
176 
190  template <typename T>
191  inline T Lerp(T from, T to, T alpha)
192  {
193  return from + ((to - from) * alpha);
194  }
195 }
static const double FOUR_PI
/ pi*4.
Definition: Math.h:39
static const double TWO_PI
/ pi*2.
Definition: Math.h:37
double Rad2Deg(double radian)
Takes Radians and converts them to Degrees.
Definition: Math.h:120
static const double INV_LOG_OF_2
/ 1/ln(2)
Definition: Math.h:47
static const double PI_OVER_2
/ pi/2.
Definition: Math.h:33
double Deg2Rad(double degree)
Takes Degrees and converts them to Radians.
Definition: Math.h:92
static const double LOG_OF_2
/ ln(2)
Definition: Math.h:45
static const double ONE_OVER_PI
1/PI.
Definition: Math.h:49
static const double PI
/ pi.
Definition: Math.h:31
T Lerp(T from, T to, T alpha)
Linear Interpolation function.
Definition: Math.h:191
static const double E
/ e.
Definition: Math.h:43
static const double ONE80_OVER_PI
PI/180.
Definition: Math.h:51
void SinCos(double x, double &sinVal, double &cosVal)
Takes a Value X and outputs its Sin and Cos by reference.
Definition: Math.h:62
static const double PI_OVER_4
/ pi/4.
Definition: Math.h:35
static const double SQRT_PI
/ pi^(1/2)
Definition: Math.h:41
double CheckState(double state)
Checks if State is = to 0, less then 0 or greater, and outputs -1, 0, 1.
Definition: Math.h:148