Skip to content
Snippets Groups Projects
Commit e9139a46 authored by Philip Trettner's avatar Philip Trettner
Browse files

more matrix ops

parent 33b0d748
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,22 @@
#include "../../types/pos.hh"
#include "../special_values.hh"
/*
* Supported operations:
* mat * vec (of same dimension)
* mat * vec (with mat dimension + 1, e.g. treats vec3 as vec4(..., 0))
* mat * pos (with mat dimension + 1, e.g. treats pos3 as vec4(..., 1))
* mat * mat
* mat + mat
* mat - mat
* +mat
* -mat
* mat + scalar
* mat - scalar
* mat / scalar
* mat * scalar
*/
namespace tg
{
// mat * vec (of same dimension)
......@@ -57,7 +73,7 @@ constexpr pos<D - 1, ScalarT> operator*(mat<D, 2, ScalarT> const& m, pos<D - 1,
{
auto v = m * vec<D, ScalarT>(p - zero<pos<D - 1, ScalarT>>(), ScalarT(1));
auto r = pos<D - 1, ScalarT>(v + zero<pos<D - 1, ScalarT>>());
return r / v[D];
return v[D] == ScalarT(1) ? r : r / v[D];
}
// mat * mat
......@@ -96,4 +112,292 @@ constexpr mat<4, A, ScalarT> operator*(mat<B, A, ScalarT> const& a, mat<4, B, Sc
return m;
}
// mat + mat
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator+(mat<1, R, ScalarT> const& a, mat<1, R, ScalarT> const& b)
{
mat<1, R, ScalarT> m;
m[0] = a[0] + b[0];
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator+(mat<2, R, ScalarT> const& a, mat<2, R, ScalarT> const& b)
{
mat<2, R, ScalarT> m;
m[0] = a[0] + b[0];
m[1] = a[1] + b[1];
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator+(mat<3, R, ScalarT> const& a, mat<3, R, ScalarT> const& b)
{
mat<3, R, ScalarT> m;
m[0] = a[0] + b[0];
m[1] = a[1] + b[1];
m[2] = a[2] + b[2];
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator+(mat<4, R, ScalarT> const& a, mat<4, R, ScalarT> const& b)
{
mat<4, R, ScalarT> m;
m[0] = a[0] + b[0];
m[1] = a[1] + b[1];
m[2] = a[2] + b[2];
m[3] = a[3] + b[3];
return m;
}
// mat - mat
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator-(mat<1, R, ScalarT> const& a, mat<1, R, ScalarT> const& b)
{
mat<1, R, ScalarT> m;
m[0] = a[0] - b[0];
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator-(mat<2, R, ScalarT> const& a, mat<2, R, ScalarT> const& b)
{
mat<2, R, ScalarT> m;
m[0] = a[0] - b[0];
m[1] = a[1] - b[1];
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator-(mat<3, R, ScalarT> const& a, mat<3, R, ScalarT> const& b)
{
mat<3, R, ScalarT> m;
m[0] = a[0] - b[0];
m[1] = a[1] - b[1];
m[2] = a[2] - b[2];
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator-(mat<4, R, ScalarT> const& a, mat<4, R, ScalarT> const& b)
{
mat<4, R, ScalarT> m;
m[0] = a[0] - b[0];
m[1] = a[1] - b[1];
m[2] = a[2] - b[2];
m[3] = a[3] - b[3];
return m;
}
// +mat
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator+(mat<1, R, ScalarT> const& a)
{
mat<1, R, ScalarT> m;
m[0] = +a[0];
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator+(mat<2, R, ScalarT> const& a)
{
mat<2, R, ScalarT> m;
m[0] = +a[0];
m[1] = +a[1];
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator+(mat<3, R, ScalarT> const& a)
{
mat<3, R, ScalarT> m;
m[0] = +a[0];
m[1] = +a[1];
m[2] = +a[2];
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator+(mat<4, R, ScalarT> const& a)
{
mat<4, R, ScalarT> m;
m[0] = +a[0];
m[1] = +a[1];
m[2] = +a[2];
m[3] = +a[3];
return m;
}
// -mat
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator-(mat<1, R, ScalarT> const& a)
{
mat<1, R, ScalarT> m;
m[0] = -a[0];
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator-(mat<2, R, ScalarT> const& a)
{
mat<2, R, ScalarT> m;
m[0] = -a[0];
m[1] = -a[1];
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator-(mat<3, R, ScalarT> const& a)
{
mat<3, R, ScalarT> m;
m[0] = -a[0];
m[1] = -a[1];
m[2] = -a[2];
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator-(mat<4, R, ScalarT> const& a)
{
mat<4, R, ScalarT> m;
m[0] = -a[0];
m[1] = -a[1];
m[2] = -a[2];
m[3] = -a[3];
return m;
}
// mat + scalar
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator+(mat<1, R, ScalarT> const& a, ScalarT b)
{
mat<1, R, ScalarT> m;
m[0] = a[0] + b;
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator+(mat<2, R, ScalarT> const& a, ScalarT b)
{
mat<2, R, ScalarT> m;
m[0] = a[0] + b;
m[1] = a[1] + b;
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator+(mat<3, R, ScalarT> const& a, ScalarT b)
{
mat<3, R, ScalarT> m;
m[0] = a[0] + b;
m[1] = a[1] + b;
m[2] = a[2] + b;
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator+(mat<4, R, ScalarT> const& a, ScalarT b)
{
mat<4, R, ScalarT> m;
m[0] = a[0] + b;
m[1] = a[1] + b;
m[2] = a[2] + b;
m[3] = a[3] + b;
return m;
}
// mat - scalar
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator-(mat<1, R, ScalarT> const& a, ScalarT b)
{
mat<1, R, ScalarT> m;
m[0] = a[0] - b;
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator-(mat<2, R, ScalarT> const& a, ScalarT b)
{
mat<2, R, ScalarT> m;
m[0] = a[0] - b;
m[1] = a[1] - b;
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator-(mat<3, R, ScalarT> const& a, ScalarT b)
{
mat<3, R, ScalarT> m;
m[0] = a[0] - b;
m[1] = a[1] - b;
m[2] = a[2] - b;
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator-(mat<4, R, ScalarT> const& a, ScalarT b)
{
mat<4, R, ScalarT> m;
m[0] = a[0] - b;
m[1] = a[1] - b;
m[2] = a[2] - b;
m[3] = a[3] - b;
return m;
}
// mat * scalar
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator*(mat<1, R, ScalarT> const& a, ScalarT b)
{
mat<1, R, ScalarT> m;
m[0] = a[0] * b;
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator*(mat<2, R, ScalarT> const& a, ScalarT b)
{
mat<2, R, ScalarT> m;
m[0] = a[0] * b;
m[1] = a[1] * b;
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator*(mat<3, R, ScalarT> const& a, ScalarT b)
{
mat<3, R, ScalarT> m;
m[0] = a[0] * b;
m[1] = a[1] * b;
m[2] = a[2] * b;
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator*(mat<4, R, ScalarT> const& a, ScalarT b)
{
mat<4, R, ScalarT> m;
m[0] = a[0] * b;
m[1] = a[1] * b;
m[2] = a[2] * b;
m[3] = a[3] * b;
return m;
}
// mat / scalar
template <int R, class ScalarT>
constexpr mat<1, R, ScalarT> operator/(mat<1, R, ScalarT> const& a, ScalarT b)
{
mat<1, R, ScalarT> m;
m[0] = a[0] / b;
return m;
}
template <int R, class ScalarT>
constexpr mat<2, R, ScalarT> operator/(mat<2, R, ScalarT> const& a, ScalarT b)
{
mat<2, R, ScalarT> m;
m[0] = a[0] / b;
m[1] = a[1] / b;
return m;
}
template <int R, class ScalarT>
constexpr mat<3, R, ScalarT> operator/(mat<3, R, ScalarT> const& a, ScalarT b)
{
mat<3, R, ScalarT> m;
m[0] = a[0] / b;
m[1] = a[1] / b;
m[2] = a[2] / b;
return m;
}
template <int R, class ScalarT>
constexpr mat<4, R, ScalarT> operator/(mat<4, R, ScalarT> const& a, ScalarT b)
{
mat<4, R, ScalarT> m;
m[0] = a[0] / b;
m[1] = a[1] / b;
m[2] = a[2] / b;
m[3] = a[3] / b;
return m;
}
} // namespace tg
......@@ -284,6 +284,8 @@ template <int C, int R, class ScalarT>
constexpr mat<C, R, ScalarT> mat<C, R, ScalarT>::zero = {};
template <int C, int R, class ScalarT>
constexpr mat<C, R, ScalarT> mat<C, R, ScalarT>::identity = tg::identity<mat<C, R, ScalarT>>();
template <int C, int R, class ScalarT>
constexpr mat<C, R, ScalarT> mat<C, R, ScalarT>::ones = tg::ones<mat<C, R, ScalarT>>();
template <int C, int R, class ScalarT>
constexpr mat<C, R, ScalarT> mat<C, R, ScalarT>::diag(ScalarT v)
......
......@@ -154,4 +154,30 @@ struct mat
static constexpr mat<C, R, ScalarT> diag(vec<detail::min(C, R), ScalarT> const& v);
};
template <int R, class ScalarT>
constexpr bool operator==(mat<1, R, ScalarT> const& a, mat<1, R, ScalarT> const& b)
{
return a[0] == b[0];
}
template <int R, class ScalarT>
constexpr bool operator==(mat<2, R, ScalarT> const& a, mat<2, R, ScalarT> const& b)
{
return a[0] == b[0] && a[1] == b[1];
}
template <int R, class ScalarT>
constexpr bool operator==(mat<3, R, ScalarT> const& a, mat<3, R, ScalarT> const& b)
{
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
}
template <int R, class ScalarT>
constexpr bool operator==(mat<4, R, ScalarT> const& a, mat<4, R, ScalarT> const& b)
{
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
}
template <int C, int R, class ScalarT>
constexpr bool operator!=(mat<C, R, ScalarT> const& a, mat<C, R, ScalarT> const& b)
{
return !(a == b);
}
} // namespace tg
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment