2018-09-04 19:25:54 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace egm
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct tvec3
|
|
|
|
{
|
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
T z;
|
|
|
|
|
|
|
|
tvec3() : x(0), y(0), z(0) {}
|
|
|
|
tvec3(const tvec3<T>& v) : x(v.x), y(v.y), z(v.z) {}
|
|
|
|
template <typename U>
|
|
|
|
tvec3(const tvec3<U>& v) : x((T)v.x), y((T)v.y), z((T)v.z) {}
|
|
|
|
tvec3(const T& scalar) : x(scalar), y(scalar), z(scalar) {}
|
|
|
|
tvec3(const T& scalar1, const T& scalar2, const T& scalar3) : x(scalar1), y(scalar2), z(scalar3) {}
|
|
|
|
|
|
|
|
T& operator[](const unsigned int& index)
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return x;
|
|
|
|
case 1:
|
|
|
|
return y;
|
|
|
|
case 2:
|
|
|
|
return z;
|
|
|
|
default:
|
|
|
|
throw "Index Out Of Bounds";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const tvec3<T>& o)
|
|
|
|
{
|
|
|
|
return (o.x == x && o.y == y && o.z == z);
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T>& operator=(const tvec3<T>& o)
|
|
|
|
{
|
|
|
|
x = o.x;
|
|
|
|
y = o.y;
|
|
|
|
z = o.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:20:56 +00:00
|
|
|
tvec3<T> operator-() const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
-x,
|
|
|
|
-y,
|
|
|
|
-z,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:25:54 +00:00
|
|
|
tvec3<T> operator+(const tvec3<T>& o) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
x + o.x,
|
|
|
|
y + o.y,
|
|
|
|
z + o.z
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T>& operator+=(const tvec3<T>& o)
|
|
|
|
{
|
|
|
|
x += o.x;
|
|
|
|
y += o.y;
|
|
|
|
z += o.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T> operator-(const tvec3<T>& o) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
x - o.x,
|
|
|
|
y - o.y,
|
|
|
|
z - o.z
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T>& operator-=(const tvec3<T>& o)
|
|
|
|
{
|
|
|
|
x -= o.x;
|
|
|
|
y -= o.y;
|
|
|
|
z -= o.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T> operator*(const tvec3<T>& o) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
x * o.x,
|
|
|
|
y * o.y,
|
|
|
|
z * o.z
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T>& operator*=(const tvec3<T>& o)
|
|
|
|
{
|
|
|
|
x *= o.x;
|
|
|
|
y *= o.y;
|
|
|
|
z *= o.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T> operator*(const T& scalar) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
x * scalar,
|
|
|
|
y * scalar,
|
|
|
|
z * scalar
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T>& operator*=(const T& scalar)
|
|
|
|
{
|
|
|
|
x *= scalar;
|
|
|
|
y *= scalar;
|
|
|
|
z *= scalar;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T> operator/(const tvec3<T>& o) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
x / o.x,
|
|
|
|
y / o.y,
|
|
|
|
z / o.z
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T>& operator/=(const tvec3<T>& o)
|
|
|
|
{
|
|
|
|
x /= o.x;
|
|
|
|
y /= o.y;
|
|
|
|
z /= o.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T> operator/(const T& scalar) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
x / scalar,
|
|
|
|
y / scalar,
|
|
|
|
z / scalar
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T>& operator/=(const T& scalar)
|
|
|
|
{
|
|
|
|
x /= scalar;
|
|
|
|
y /= scalar;
|
|
|
|
z /= scalar;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-10 01:20:56 +00:00
|
|
|
T length() const
|
|
|
|
{
|
|
|
|
return sqrt(x * x + y * y + z * z);
|
|
|
|
}
|
|
|
|
|
|
|
|
T length_squared() const
|
|
|
|
{
|
|
|
|
return x * x + y * y + z * z;
|
|
|
|
}
|
|
|
|
|
|
|
|
void normalize()
|
|
|
|
{
|
|
|
|
T l = length();
|
|
|
|
x /= l;
|
|
|
|
y /= l;
|
|
|
|
z /= l;
|
|
|
|
}
|
|
|
|
|
|
|
|
tvec3<T> normalized() const
|
2018-09-04 19:25:54 +00:00
|
|
|
{
|
2018-09-10 01:20:56 +00:00
|
|
|
tvec3<T> ret(*this);
|
|
|
|
ret.normalize();
|
|
|
|
return ret;
|
2018-09-04 19:25:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef tvec3<float> vec3;
|
|
|
|
typedef tvec3<double> dvec3;
|
|
|
|
typedef tvec3<int> ivec3;
|
|
|
|
}
|