1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-04 02:41:19 +00:00
OpenMW/components/misc/mathutil.hpp
2023-02-05 12:30:38 -08:00

45 lines
1009 B
C++

#ifndef MISC_MATHUTIL_H
#define MISC_MATHUTIL_H
#include <osg/Math>
#include <osg/Vec2f>
namespace Misc
{
/// Normalizes given angle to the range [-PI, PI]. E.g. PI*3/2 -> -PI/2.
inline double normalizeAngle(double angle)
{
double fullTurns = angle / (2 * osg::PI) + 0.5;
return (fullTurns - floor(fullTurns) - 0.5) * (2 * osg::PI);
}
/// Rotates given 2d vector counterclockwise. Angle is in radians.
inline osg::Vec2f rotateVec2f(osg::Vec2f vec, float angle)
{
float s = std::sin(angle);
float c = std::cos(angle);
return osg::Vec2f(vec.x() * c + vec.y() * -s, vec.x() * s + vec.y() * c);
}
inline bool isPowerOfTwo(int x)
{
return ((x > 0) && ((x & (x - 1)) == 0));
}
inline int nextPowerOfTwo(int v)
{
if (isPowerOfTwo(v))
return v;
int depth = 0;
while (v)
{
v >>= 1;
depth++;
}
return 1 << depth;
}
}
#endif