charcoal/archive/math/vec3.h
elipzer 75357b330c Added Dependency: GLM
Now using GLM instead of the custom math libraries.
Sadly, this did not 100% fix the problem at hand but it does give
some closure that the math is not the problem.

Also it will be nice to have a general math library to not have to
deal with creating every math function every time.
2018-09-09 21:20:56 -04:00

189 lines
2.6 KiB
C++

#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;
}
tvec3<T> operator-() const
{
return {
-x,
-y,
-z,
};
}
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;
}
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
{
tvec3<T> ret(*this);
ret.normalize();
return ret;
}
};
typedef tvec3<float> vec3;
typedef tvec3<double> dvec3;
typedef tvec3<int> ivec3;
}