ddnet/src/base/vmath.h

406 lines
9.3 KiB
C
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#ifndef BASE_VMATH_H
#define BASE_VMATH_H
#include <cmath>
#include <cstdint>
2020-09-18 16:45:42 +00:00
#include "math.h"
// ------------------------------------
template<typename T>
class vector2_base
{
public:
union
{
T x, u;
};
union
{
T y, v;
};
constexpr vector2_base() :
x(T()), y(T())
{
}
constexpr vector2_base(T nx, T ny) :
x(nx), y(ny)
{
}
vector2_base operator-() const { return vector2_base(-x, -y); }
vector2_base operator-(const vector2_base &vec) const { return vector2_base(x - vec.x, y - vec.y); }
vector2_base operator+(const vector2_base &vec) const { return vector2_base(x + vec.x, y + vec.y); }
vector2_base operator*(const T rhs) const { return vector2_base(x * rhs, y * rhs); }
vector2_base operator*(const vector2_base &vec) const { return vector2_base(x * vec.x, y * vec.y); }
vector2_base operator/(const T rhs) const { return vector2_base(x / rhs, y / rhs); }
vector2_base operator/(const vector2_base &vec) const { return vector2_base(x / vec.x, y / vec.y); }
const vector2_base &operator+=(const vector2_base &vec)
{
x += vec.x;
y += vec.y;
return *this;
}
const vector2_base &operator-=(const vector2_base &vec)
{
x -= vec.x;
y -= vec.y;
return *this;
}
const vector2_base &operator*=(const T rhs)
{
x *= rhs;
y *= rhs;
return *this;
}
const vector2_base &operator*=(const vector2_base &vec)
{
x *= vec.x;
y *= vec.y;
return *this;
}
const vector2_base &operator/=(const T rhs)
{
x /= rhs;
y /= rhs;
return *this;
}
const vector2_base &operator/=(const vector2_base &vec)
{
x /= vec.x;
y /= vec.y;
return *this;
}
bool operator==(const vector2_base &vec) const { return x == vec.x && y == vec.y; } //TODO: do this with an eps instead
bool operator!=(const vector2_base &vec) const { return x != vec.x || y != vec.y; }
T &operator[](const int index) { return index ? y : x; }
};
template<typename T>
constexpr inline vector2_base<T> rotate(const vector2_base<T> &a, float angle)
{
angle = angle * pi / 180.0f;
float s = std::sin(angle);
float c = std::cos(angle);
return vector2_base<T>((T)(c * a.x - s * a.y), (T)(s * a.x + c * a.y));
}
template<typename T>
inline T distance(const vector2_base<T> a, const vector2_base<T> &b)
{
return length(a - b);
}
template<typename T>
constexpr inline T dot(const vector2_base<T> a, const vector2_base<T> &b)
{
return a.x * b.x + a.y * b.y;
}
inline float length(const vector2_base<float> &a)
{
return std::sqrt(dot(a, a));
}
inline float length(const vector2_base<int> &a)
{
return std::sqrt(dot(a, a));
}
2023-01-30 23:46:39 +00:00
inline float length_squared(const vector2_base<float> &a)
{
return dot(a, a);
}
constexpr inline float angle(const vector2_base<float> &a)
{
if(a.x == 0 && a.y == 0)
return 0.0f;
else if(a.x == 0)
return a.y < 0 ? -pi / 2 : pi / 2;
float result = std::atan(a.y / a.x);
if(a.x < 0)
result = result + pi;
return result;
}
template<typename T>
constexpr inline vector2_base<T> normalize_pre_length(const vector2_base<T> &v, T len)
{
2022-05-25 23:22:14 +00:00
if(len == 0)
return vector2_base<T>();
return vector2_base<T>(v.x / len, v.y / len);
}
inline vector2_base<float> normalize(const vector2_base<float> &v)
{
float divisor = length(v);
2022-05-25 23:22:14 +00:00
if(divisor == 0.0f)
return vector2_base<float>(0.0f, 0.0f);
float l = (float)(1.0f / divisor);
return vector2_base<float>(v.x * l, v.y * l);
}
Fix build with clang on debian 11 $ clang --version Debian clang version 11.0.1-2 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Fixes this error ``` In file included from /home/chiller/Desktop/git/ddnet/src/base/color.h:6: /home/chiller/Desktop/git/ddnet/src/base/vmath.h:141:38: error: constexpr function never produces a constant expression [-Winvalid-constexpr] constexpr inline vector2_base<float> direction(float angle) ^ /home/chiller/Desktop/git/ddnet/src/base/vmath.h:143:29: note: non-constexpr function 'cosf' cannot be used in a constant expression return vector2_base<float>(cosf(angle), sinf(angle)); ^ /usr/include/x86_64-linux-gnu/bits/mathcalls.h:62:1: note: declared here __MATHCALL_VEC (cos,, (_Mdouble_ __x)); ^ /usr/include/math.h:266:3: note: expanded from macro '__MATHCALL_VEC' __MATHCALL (function, suffix, args) ^ /usr/include/math.h:273:3: note: expanded from macro '__MATHCALL' __MATHDECL (_Mdouble_,function,suffix, args) ^ /usr/include/math.h:275:3: note: expanded from macro '__MATHDECL' __MATHDECL_1(type, function,suffix, args); \ ^ /usr/include/math.h:283:15: note: expanded from macro '__MATHDECL_1' extern type __MATH_PRECNAME(function,suffix) args __THROW ^ /usr/include/math.h:303:34: note: expanded from macro '__MATH_PRECNAME' ^ <scratch space>:34:1: note: expanded from here cosf ^ 1 error generated. make[2]: *** [CMakeFiles/engine-shared.dir/build.make:173: CMakeFiles/engine-shared.dir/src/engine/shared/console.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:604: CMakeFiles/engine-shared.dir/all] Error 2 make: *** [Makefile:171: all] Error 2 ```
2023-01-16 14:08:37 +00:00
inline vector2_base<float> direction(float angle)
{
return vector2_base<float>(std::cos(angle), std::sin(angle));
}
inline vector2_base<float> random_direction()
{
return direction(random_angle());
}
typedef vector2_base<float> vec2;
typedef vector2_base<bool> bvec2;
typedef vector2_base<int> ivec2;
template<typename T>
constexpr inline bool closest_point_on_line(vector2_base<T> line_pointA, vector2_base<T> line_pointB, vector2_base<T> target_point, vector2_base<T> &out_pos)
{
vector2_base<T> AB = line_pointB - line_pointA;
T SquaredMagnitudeAB = dot(AB, AB);
if(SquaredMagnitudeAB > 0)
{
vector2_base<T> AP = target_point - line_pointA;
T APdotAB = dot(AP, AB);
T t = APdotAB / SquaredMagnitudeAB;
out_pos = line_pointA + AB * clamp(t, (T)0, (T)1);
return true;
}
else
return false;
}
// ------------------------------------
template<typename T>
class vector3_base
{
public:
union
{
T x, r, h, u;
};
union
{
T y, g, s, v;
};
union
{
T z, b, l, w;
};
constexpr vector3_base() :
x(T()), y(T()), z(T())
{
}
constexpr vector3_base(T nx, T ny, T nz) :
x(nx), y(ny), z(nz)
{
}
vector3_base operator-(const vector3_base &vec) const { return vector3_base(x - vec.x, y - vec.y, z - vec.z); }
vector3_base operator-() const { return vector3_base(-x, -y, -z); }
vector3_base operator+(const vector3_base &vec) const { return vector3_base(x + vec.x, y + vec.y, z + vec.z); }
vector3_base operator*(const T rhs) const { return vector3_base(x * rhs, y * rhs, z * rhs); }
vector3_base operator*(const vector3_base &vec) const { return vector3_base(x * vec.x, y * vec.y, z * vec.z); }
vector3_base operator/(const T rhs) const { return vector3_base(x / rhs, y / rhs, z / rhs); }
vector3_base operator/(const vector3_base &vec) const { return vector3_base(x / vec.x, y / vec.y, z / vec.z); }
const vector3_base &operator+=(const vector3_base &vec)
{
x += vec.x;
y += vec.y;
z += vec.z;
return *this;
}
const vector3_base &operator-=(const vector3_base &vec)
{
x -= vec.x;
y -= vec.y;
z -= vec.z;
return *this;
}
const vector3_base &operator*=(const T rhs)
{
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
const vector3_base &operator*=(const vector3_base &vec)
{
x *= vec.x;
y *= vec.y;
z *= vec.z;
return *this;
}
const vector3_base &operator/=(const T rhs)
{
x /= rhs;
y /= rhs;
z /= rhs;
return *this;
}
const vector3_base &operator/=(const vector3_base &vec)
{
x /= vec.x;
y /= vec.y;
z /= vec.z;
return *this;
}
bool operator==(const vector3_base &vec) const { return x == vec.x && y == vec.y && z == vec.z; } //TODO: do this with an eps instead
bool operator!=(const vector3_base &vec) const { return x != vec.x || y != vec.y || z != vec.z; }
};
template<typename T>
inline T distance(const vector3_base<T> &a, const vector3_base<T> &b)
{
return length(a - b);
}
template<typename T>
constexpr inline T dot(const vector3_base<T> &a, const vector3_base<T> &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
template<typename T>
constexpr inline vector3_base<T> cross(const vector3_base<T> &a, const vector3_base<T> &b)
{
return vector3_base<T>(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
//
inline float length(const vector3_base<float> &a)
{
return std::sqrt(dot(a, a));
}
inline vector3_base<float> normalize(const vector3_base<float> &v)
{
float divisor = length(v);
2022-05-25 23:22:14 +00:00
if(divisor == 0.0f)
return vector3_base<float>(0.0f, 0.0f, 0.0f);
float l = (float)(1.0f / divisor);
return vector3_base<float>(v.x * l, v.y * l, v.z * l);
}
typedef vector3_base<float> vec3;
typedef vector3_base<bool> bvec3;
typedef vector3_base<int> ivec3;
// ------------------------------------
template<typename T>
class vector4_base
{
public:
union
{
T x, r, h;
};
union
{
T y, g, s;
};
union
{
T z, b, l;
};
union
{
T w, a;
};
constexpr vector4_base() :
x(T()), y(T()), z(T()), w(T())
{
}
constexpr vector4_base(T nx, T ny, T nz, T nw) :
x(nx), y(ny), z(nz), w(nw)
{
}
vector4_base operator+(const vector4_base &vec) const { return vector4_base(x + vec.x, y + vec.y, z + vec.z, w + vec.w); }
vector4_base operator-(const vector4_base &vec) const { return vector4_base(x - vec.x, y - vec.y, z - vec.z, w - vec.w); }
vector4_base operator-() const { return vector4_base(-x, -y, -z, -w); }
vector4_base operator*(const vector4_base &vec) const { return vector4_base(x * vec.x, y * vec.y, z * vec.z, w * vec.w); }
vector4_base operator*(const T rhs) const { return vector4_base(x * rhs, y * rhs, z * rhs, w * rhs); }
vector4_base operator/(const vector4_base &vec) const { return vector4_base(x / vec.x, y / vec.y, z / vec.z, w / vec.w); }
vector4_base operator/(const T vec) const { return vector4_base(x / vec, y / vec, z / vec, w / vec); }
const vector4_base &operator+=(const vector4_base &vec)
{
x += vec.x;
y += vec.y;
z += vec.z;
w += vec.w;
return *this;
}
const vector4_base &operator-=(const vector4_base &vec)
{
x -= vec.x;
y -= vec.y;
z -= vec.z;
w -= vec.w;
return *this;
}
const vector4_base &operator*=(const T rhs)
{
x *= rhs;
y *= rhs;
z *= rhs;
w *= rhs;
return *this;
}
const vector4_base &operator*=(const vector4_base &vec)
{
x *= vec.x;
y *= vec.y;
z *= vec.z;
w *= vec.w;
return *this;
}
const vector4_base &operator/=(const T rhs)
{
x /= rhs;
y /= rhs;
z /= rhs;
w /= rhs;
return *this;
}
const vector4_base &operator/=(const vector4_base &vec)
{
x /= vec.x;
y /= vec.y;
z /= vec.z;
w /= vec.w;
return *this;
}
bool operator==(const vector4_base &vec) const { return x == vec.x && y == vec.y && z == vec.z && w == vec.w; } //TODO: do this with an eps instead
bool operator!=(const vector4_base &vec) const { return x != vec.x || y != vec.y || z != vec.z || w != vec.w; }
};
typedef vector4_base<float> vec4;
typedef vector4_base<bool> bvec4;
typedef vector4_base<int> ivec4;
typedef vector4_base<uint8_t> ubvec4;
#endif