ECG/exercise3/include/matrix.h

126 lines
2.7 KiB
C
Raw Normal View History

2019-06-01 10:25:25 +00:00
#pragma once
#include <cmath>
#include "tiny_vec.h"
template <class T>
class Matrix4 {
public:
Matrix4(
T c0r0, T c0r1, T c0r2, T c0r3,
T c1r0, T c1r1, T c1r2, T c1r3,
T c2r0, T c2r1, T c2r2, T c2r3,
2019-06-01 11:02:40 +00:00
T c3r0, T c3r1, T c3r2, T c3r3
2019-06-01 10:25:25 +00:00
) {
2019-06-01 11:02:40 +00:00
x = tiny_vec<T, 4>(c0r0, c0r1, c0r2, c0r3);
y = tiny_vec<T, 4>(c1r0, c1r1, c1r2, c1r3);
z = tiny_vec<T, 4>(c2r0, c2r1, c2r2, c2r3);
w = tiny_vec<T, 4>(c3r0, c3r1, c3r2, c3r3);
2019-06-01 10:25:25 +00:00
}
~Matrix4() { }
static Matrix4 identity()
{
T zero = T::value_type(0);
T one = T::value_type(1);
2019-06-01 11:02:40 +00:00
return Matrix4(
2019-06-01 10:25:25 +00:00
one, zero, zero, zero,
zero, one, zero, zero,
zero, zero, one, zero,
zero, zero, zero, one
2019-06-01 11:02:40 +00:00
);
2019-06-01 10:25:25 +00:00
}
static Matrix4 from_translation(tiny_vec<T, 3> v)
{
T zero = T::value_type(0);
T one = T::value_type(1);
2019-06-01 11:02:40 +00:00
return Matrix4(
2019-06-01 10:25:25 +00:00
one, zero, zero, zero,
zero, one, zero, zero,
zero, zero, one, zero,
2019-06-01 11:02:40 +00:00
v.x, v.y, v.z, one
);
2019-06-01 10:25:25 +00:00
}
static Matrix4 from_scale(T s)
{
2019-06-01 11:02:40 +00:00
return from_non_uniform_scale(s, s, s);
2019-06-01 10:25:25 +00:00
}
static Matrix4 from_non_uniform_scale(T x, T y, T z)
{
T zero = T::value_type(0);
T one = T::value_type(1);
2019-06-01 11:02:40 +00:00
return Matrix4(
2019-06-01 10:25:25 +00:00
x, zero, zero, zero,
zero, y, zero, zero,
zero, zero, z, zero,
2019-06-01 11:02:40 +00:00
zero, zero, zero, one
);
2019-06-01 10:25:25 +00:00
}
static Matrix4 from_angle_x(T a)
{
T s = std::sin(a);
T c = std::cos(a);
T zero = T::value_type(0);
T one = T::value_type(1);
2019-06-01 11:02:40 +00:00
return Matrix4(
2019-06-01 10:25:25 +00:00
one, zero, zero, zero,
zero, c, s, zero,
zero, -s, c, zero,
2019-06-01 11:02:40 +00:00
zero, zero, zero, one
);
2019-06-01 10:25:25 +00:00
}
static Matrix4 from_angle_y(T a)
{
T s = std::sin(a);
T c = std::cos(a);
T zero = T::value_type(0);
T one = T::value_type(1);
2019-06-01 11:02:40 +00:00
return Matrix4(
2019-06-01 10:25:25 +00:00
c, zero, -s, zero,
zero, one, zero, zero,
s, zero, c, zero,
zero, zero, zero, one
2019-06-01 11:02:40 +00:00
);
2019-06-01 10:25:25 +00:00
}
static Matrix4 from_angle_z(T a)
{
T s = std::sin(a);
T c = std::cos(a);
T zero = T::value_type(0);
T one = T::value_type(1);
2019-06-01 11:02:40 +00:00
return Matrix4(
2019-06-01 10:25:25 +00:00
c, s, zero, zero,
-s, c, zero, zero,
zero, zero, one, zero,
zero, zero, zero, one
2019-06-01 11:02:40 +00:00
);
2019-06-01 10:25:25 +00:00
}
private:
// first column
tiny_vec<T, 4> x;
// second column
tiny_vec<T, 4> y;
// third column
tiny_vec<T, 4> z;
// fourth column
tiny_vec<T, 4> w;
2019-06-01 11:02:40 +00:00
};