10constexpr float radian = 3.14159265f / 180.f;
12constexpr float pi = 3.14159265f;
40 return sqrtf(pow(
x, 2) + pow(
y, 2));
122 return x == in.
x &&
y == in.
y;
130 out <<
"( " << vec.
x <<
", " << vec.
y <<
" )";
135 T temp = sqrt(
x *
x +
y *
y);
136 *
this = *
this / temp;
141 T temp = sqrt(in.
x * in.
x + in.
y * in.
y);
146 return sqrtf(pow(b.
x - a.
x, 2) + pow(b.
y - a.
y, 2));
150 return a.
x * b.
x + a.
y * b.
y;
175 return sqrtf(pow(
x, 2) + pow(
y, 2) + pow(
z, 2));
266 return x == in.
x &&
y == in.
y &&
z == in.
z;
274 out <<
"( " << vec.
x <<
", " << vec.
y <<
", " << vec.
z <<
" )";
279 T temp = sqrt(
x *
x +
y *
y +
z *
z);
280 *
this = *
this / temp;
285 T temp = sqrt(in.
x * in.
x + in.
y * in.
y + in.
z * in.
z);
290 return sqrtf(pow(b.
x - a.
x, 2) + pow(b.
y - a.
y, 2) + pow(b.
z - a.
z, 2));
295 ret.
x = a.
y * b.
z - b.
y * a.
z;
296 ret.
y = b.
x * a.
z - a.
x * b.
z;
297 ret.
z = a.
x * b.
y - b.
x * a.
y;
302 return a.
x * b.
x + a.
y * b.
y + a.
z * b.
z;
324 return sqrtf(pow(
x, 2) + pow(
y, 2) + pow(
z, 2) + pow(
w, 2));
408 return x == in.
x &&
y == in.
y &&
z == in.
z &&
w == in.
w;
416 out <<
"( " << vec.
x <<
", " << vec.
y <<
", " << vec.
z <<
", " << vec.
w <<
" )";
421 T temp = sqrt(
x *
x +
y *
y +
z *
z +
w *
w);
422 *
this = *
this / temp;
427 T temp = sqrt(in.
x * in.
x + in.
y * in.
y + in.
z * in.
z + in.
w * in.
w);
432 return sqrt(pow(b.
x - a.
x, 2) + pow(b.
y - a.
y, 2) + pow(b.
z - a.
z, 2) + pow(b.
w - a.
w, 2));
436 return a.
x * b.
x + a.
y * b.
y + a.
z * b.
z + a.
w * b.
w;
446template <
class Type,
size_t x_dim,
size_t y_dim>
453 for (
int i = 0; i < y_dim; i++)
454 for (
size_t j = 0; j < x_dim; j++)
455 at(i, j) = i == j ? 1 : 0;
459 for (
int i = 0; i < y_dim; i++)
460 for (
size_t j = 0; j < x_dim; j++)
464 mat(
const std::array<Type, x_dim * y_dim> &in) {
465 assert(in.size() == x_dim * y_dim);
471 for (
int i = 0; i < y_dim; i++)
472 for (
size_t j = 0; j < x_dim; j++)
477 assert(r >= 0 && r < y_dim && c >= 0 && c < x_dim);
478 return m_data[r * x_dim + c];
481 inline const Type &
operator()(
const size_t &r,
const size_t &c)
const {
482 assert(r >= 0 && r < y_dim && c >= 0 && c < x_dim);
483 return m_data[r * x_dim + c];
486 inline const Type &
at(
const size_t &r,
const size_t &c)
const {
487 return m_data[r * x_dim + c];
490 inline Type &
at(
const size_t &r,
const size_t &c) {
491 return m_data[r * x_dim + c];
495 for (
size_t i = 0; i < y_dim; i++) {
497 for (
size_t j = 0; j < x_dim; j++)
498 printf(
"%8.3lf",
at(i, j));
508 for (i = 0; i < y_dim; i++) {
509 constant = temp(i, i);
510 for (j = i + 1; j < y_dim; j++) {
511 constant = temp(j, i) / temp(i, i);
512 for (k = i; k < x_dim; k++)
513 temp(j, k) = temp(j, k) - constant * temp(i, k);
520 assert(x_dim == y_dim);
525 if (determinant < 1e-7 && determinant > -1e-7) {
526 printf(
"determinant is zero \n");
527 throw std::runtime_error(
"inverse of the doesnot exist");
534 for (i = 0; i < y_dim; ++i) {
535 if (temp(i, i) == 0) {
536 for (j = i; j < y_dim; j++) {
537 if (temp.
at(j, j) != 0) {
538 auto dest = &temp.
at(j, 0);
539 auto src = &temp.
at(i, 0);
541 auto *swaptemp =
new Type[x_dim];
543 memcpy(swaptemp, dest, x_dim *
sizeof(Type));
544 memcpy(dest, src, x_dim *
sizeof(Type));
545 memcpy(src, swaptemp, x_dim *
sizeof(Type));
550 memcpy(swaptemp, dest, x_dim *
sizeof(Type));
551 memcpy(dest, src, x_dim *
sizeof(Type));
552 memcpy(src, swaptemp, x_dim *
sizeof(Type));
559 constant = temp(i, i);
560 for (j = i; j < x_dim; j++)
561 temp(i, j) /= constant;
562 for (j = 0; j < x_dim; j++)
563 inv(i, j) /= constant;
565 for (j = 0; j < y_dim; j++) {
568 constant = temp(j, i);
569 for (k = i; k < x_dim; k++) {
570 temp(j, k) = temp(j, k) - temp(i, k) * constant;
573 for (k = 0; k < x_dim; k++) {
574 inv(j, k) = inv(j, k) - inv(i, k) * constant;
583 for (
size_t i = 0; i < y_dim; i++)
584 for (
size_t j = 0; j < x_dim; j++)
585 ret(i, j) = -ret(i, j);
591 for (
size_t i = 0; i < y_dim; i++)
592 for (
size_t j = 0; j < x_dim; j++)
593 ret(i, j) = in(i, j) +
at(i, j);
599 for (
size_t i = 0; i < y_dim; i++)
600 for (
size_t j = 0; j < x_dim; j++)
601 ret(i, j) = in(i, j) -
at(i, j);
606 for (
size_t i = 0; i < y_dim; i++)
607 for (
size_t j = 0; j < x_dim; j++)
608 at(i, j) += in(i, j);
612 for (
size_t i = 0; i < y_dim; i++)
613 for (
size_t j = 0; j < x_dim; j++)
614 at(i, j) -= in(i, j);
618 assert(x_dim == y_dim);
619 std::array<Type, x_dim * y_dim> new_buffer;
621 for (i = 0; i < y_dim; i++) {
622 for (j = 0; j < y_dim; j++) {
623 new_buffer[i * y_dim + j] = 0;
624 for (k = 0; k < y_dim; k++)
625 new_buffer[i * y_dim + j] +=
at(i, k) * in(k, j);
628 m_data = std::move(new_buffer);
632 for (
size_t i = 0; i < y_dim; i++)
633 for (
size_t j = 0; j < x_dim; j++)
638 for (
size_t i = 0; i < y_dim; i++)
639 for (
size_t j = 0; j < x_dim; j++)
644 assert(x_dim == 2 && y_dim == 2);
646 ret.
x =
at(0, 0) * in.
x +
at(0, 1) * in.
y;
647 ret.
y =
at(1, 0) * in.
x +
at(1, 1) * in.
y;
653 for (
auto &i : ret.
m_data)
660 for (
auto &i : ret.
m_data)
666 assert(x_dim == y_dim);
669 for (
size_t i = 0; i < y_dim; i++)
679template <
class Type,
size_t x1,
size_t y1,
size_t x2>
683 for (i = 0; i < y1; i++) {
684 for (j = 0; j < x2; j++) {
686 for (k = 0; k < x1; k++)
687 ret.
at(i, j) += first(i, k) * second(k, j);
698 return mat<Type, 4, 4>({first.
at(0, 0) * second.
at(0, 0) + first.
at(0, 1) * second.
at(1, 0) + first.
at(0, 2) * second.
at(2, 0) + first.
at(0, 3) * second.
at(3, 0),
699 first.
at(0, 0) * second.
at(0, 1) + first.
at(0, 1) * second.
at(1, 1) + first.
at(0, 2) * second.
at(2, 1) + first.
at(0, 3) * second.
at(3, 1),
700 first.
at(0, 0) * second.
at(0, 2) + first.
at(0, 1) * second.
at(1, 2) + first.
at(0, 2) * second.
at(2, 2) + first.
at(0, 3) * second.
at(3, 2),
701 first.
at(0, 0) * second.
at(0, 3) + first.
at(0, 1) * second.
at(1, 3) + first.
at(0, 2) * second.
at(2, 3) + first.
at(0, 3) * second.
at(3, 3),
702 first.
at(1, 0) * second.
at(0, 0) + first.
at(1, 1) * second.
at(1, 0) + first.
at(1, 2) * second.
at(2, 0) + first.
at(1, 3) * second.
at(3, 0),
703 first.
at(1, 0) * second.
at(0, 1) + first.
at(1, 1) * second.
at(1, 1) + first.
at(1, 2) * second.
at(2, 1) + first.
at(1, 3) * second.
at(3, 1),
704 first.
at(1, 0) * second.
at(0, 2) + first.
at(1, 1) * second.
at(1, 2) + first.
at(1, 2) * second.
at(2, 2) + first.
at(1, 3) * second.
at(3, 2),
705 first.
at(1, 0) * second.
at(0, 3) + first.
at(1, 1) * second.
at(1, 3) + first.
at(1, 2) * second.
at(2, 3) + first.
at(1, 3) * second.
at(3, 3),
706 first.
at(2, 0) * second.
at(0, 0) + first.
at(2, 1) * second.
at(1, 0) + first.
at(2, 2) * second.
at(2, 0) + first.
at(2, 3) * second.
at(3, 0),
707 first.
at(2, 0) * second.
at(0, 1) + first.
at(2, 1) * second.
at(1, 1) + first.
at(2, 2) * second.
at(2, 1) + first.
at(2, 3) * second.
at(3, 1),
708 first.
at(2, 0) * second.
at(0, 2) + first.
at(2, 1) * second.
at(1, 2) + first.
at(2, 2) * second.
at(2, 2) + first.
at(2, 3) * second.
at(3, 2),
709 first.
at(2, 0) * second.
at(0, 3) + first.
at(2, 1) * second.
at(1, 3) + first.
at(2, 2) * second.
at(2, 3) + first.
at(2, 3) * second.
at(3, 3),
710 first.
at(3, 0) * second.
at(0, 0) + first.
at(3, 1) * second.
at(1, 0) + first.
at(3, 2) * second.
at(2, 0) + first.
at(3, 3) * second.
at(3, 0),
711 first.
at(3, 0) * second.
at(0, 1) + first.
at(3, 1) * second.
at(1, 1) + first.
at(3, 2) * second.
at(2, 1) + first.
at(3, 3) * second.
at(3, 1),
712 first.
at(3, 0) * second.
at(0, 2) + first.
at(3, 1) * second.
at(1, 2) + first.
at(3, 2) * second.
at(2, 2) + first.
at(3, 3) * second.
at(3, 2),
713 first.
at(3, 0) * second.
at(0, 3) + first.
at(3, 1) * second.
at(1, 3) + first.
at(3, 2) * second.
at(2, 3) + first.
at(3, 3) * second.
at(3, 3)});
759static void printVec(
const vec4 &in) {
760 printf(
"(%f, %f, %f, %f)\n", in.
x, in.
y, in.
z, in.
w);
762static void printVec(
const vec3 &in) {
763 printf(
"(%f, %f, %f)\n", in.
x, in.
y, in.
z);
766static void printVec(
const vec2 &in) {
767 printf(
"(%f, %f)\n", in.
x, in.
y);
A general matrix class for any size of matrix.
const Type & operator()(const size_t &r, const size_t &c) const
Type & operator()(const size_t &r, const size_t &c)
void operator-=(const mat< Type, x_dim, y_dim > &in)
mat< Type, x_dim, y_dim > operator*(const Type &a) const
mat< Type, x_dim, y_dim > operator-()
vec2_T< Type > operator*(const vec2_T< Type > &in) const
mat< Type, x_dim, y_dim > operator+(const mat< Type, x_dim, y_dim > &in)
void operator/=(const Type &a)
mat< Type, x_dim, y_dim > toUpper()
mat< Type, x_dim, y_dim > operator/(const Type &a) const
mat(const std::array< Type, x_dim *y_dim > &in)
void operator*=(const Type &a)
mat< Type, x_dim, y_dim > inverse() const
mat(const mat< U, x_dim, y_dim > &in)
Type & at(const size_t &r, const size_t &c)
const Type & at(const size_t &r, const size_t &c) const
std::array< Type, x_dim *y_dim > m_data
void operator+=(const mat< Type, x_dim, y_dim > &in)
mat< Type, x_dim, y_dim > operator-(const mat< Type, x_dim, y_dim > &in)
void operator*=(const mat< Type, x_dim, y_dim > &in)
mat< double, 3, 3 > mat3d
mat< unsigned int, 3, 3 > mat3u
mat< unsigned int, 4, 4 > mat4u
mat< double, 4, 4 > mat4d
mat< Type, x2, y1 > operator*(const mat< Type, x1, y1 > &first, const mat< Type, x2, x1 > &second)
operator overload for matrix multiplication.
2d Vector class for storing x and y.
vec2_T< T > operator/(T a) const
vec2_T(const vec3_T< T > &in)
vec2_T< T > operator-() const
bool operator!=(const vec2_T< T > &in) const
vec2_T< float > operator*(float a) const
void operator*=(const U &in)
vec2_T(const glm::vec2 &in)
vec2_T< T > operator-=(const vec2_T< T > &in)
vec2_T< T > operator-(const vec2_T< T > &b) const
static double dist(const vec2_T< T > &a, const vec2_T< T > &b)
vec2_T< T > operator+(const vec2_T< T > &b) const
friend std::ostream & operator<<(std::ostream &out, const vec2_T< T > &vec)
bool operator==(const vec2_T< T > &in) const
vec2_T< T > operator/(const vec2_T< T > &b) const
void operator/=(const U &in)
static T dot(const vec2_T< T > &a, const vec2_T< T > &b)
vec2_T(const vec4_T< T > &in)
vec2_T< T > operator*(const vec2_T< T > &b) const
static vec2_T< T > normalize(const vec2_T< T > &in)
vec2_T(const vec2_T< U > &in)
vec2_T< T > operator-(T a) const
vec2_T< T > operator+(T a) const
vec2_T< T > operator+=(const vec2_T< T > &in)
3 element Vector class for storing x, y and z.
vec3_T(const vec4_T< T > &in)
vec3_T< T > operator/(const vec3_T< T > &b) const
vec3_T< T > operator/(const U &a) const
static T dot(const vec3_T< T > &a, const vec3_T< T > &b)
vec3_T< T > operator-(T a) const
vec3_T< T > operator+=(const vec3_T< T > &in)
vec3_T< T > operator*(const vec3_T< T > &b) const
vec3_T< T > operator+(T a) const
void operator/=(const U &in)
bool operator!=(const vec3_T< T > &in) const
static vec3_T< T > cross(const vec3_T< T > &a, const vec3_T< T > &b)
vec3_T< T > operator+(const vec3_T< T > &b) const
static double dist(const vec3_T< T > &a, const vec3_T< T > &b)
vec3_T(const vec2_T< T > &in)
vec3_T< T > operator-() const
vec3_T< T > operator-=(const vec3_T< T > &in)
bool operator==(const vec3_T< T > &in) const
void operator*=(const U &a)
vec3_T< T > operator*(const U &a) const
vec3_T(const glm::vec3 &in)
static vec3_T< T > normalize(const vec3_T< T > &in)
vec3_T< T > operator-(const vec3_T< T > &b) const
friend std::ostream & operator<<(std::ostream &out, const vec3_T< T > &vec)
4 element Vector class for storing x, y, z and w.
vec4_T< T > operator/(const U &a) const
static T dot(const vec4_T< T > &a, const vec4_T< T > &b)
vec4_T< T > operator+(const T &a) const
void operator*=(const U &in)
vec4_T< T > operator*(const U &a) const
void operator+=(const vec4_T< T > &in)
vec4_T(const vec3_T< T > &in, const T &iw=1)
static double dist(const vec4_T< T > &a, const vec4_T< T > &b)
void operator-=(const vec4_T< T > &in)
vec4_T< T > operator/(const vec4_T< T > &b) const
vec4_T(T a, T b, T c, T d)
vec4_T< T > operator-(T a) const
bool operator!=(const vec4_T< T > &in) const
vec4_T< T > operator-(const vec4_T< T > &b) const
vec4_T< T > operator+(const vec4_T< T > &b) const
static vec4_T< T > normalize(const vec4_T< T > &in)
void operator/=(const U &in)
friend std::ostream & operator<<(std::ostream &out, const vec4_T< T > &vec)
vec4_T< T > operator-() const
vec4_T(const glm::vec4 &in)
bool operator==(const vec4_T< T > &in) const