3D City v-1.0
Computer Graphics Project
maths.h
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cmath>
5#include <iostream>
6#include <cstring>
7#include <array>
8#include "glm/glm.hpp"
9
10constexpr float radian = 3.14159265f / 180.f;
11constexpr float epsilon = 1e-4f;
12constexpr float pi = 3.14159265f;
13
14template <class T>
15struct vec4_T;
16template <class T>
17struct vec3_T;
18
24template <class T>
25struct vec2_T {
26 T x, y;
27
28 vec2_T() { x = y = 0; }
29 explicit vec2_T(T a) : x(a), y(a) {}
30 vec2_T(T a, T b) : x(a), y(b) {}
31 vec2_T(const glm::vec2 &in) : x(in.x), y(in.y) {}
32 vec2_T(glm::vec2 &in) : x(in.x), y(in.y) {}
33 vec2_T(const vec3_T<T> &in) : x(in.x), y(in.y) {}
34 vec2_T(const vec4_T<T> &in) : x(in.x), y(in.y) {}
35
36 template <class U>
37 vec2_T(const vec2_T<U> &in) : x(in.x), y(in.y) {}
38
39 float getmag() const {
40 return sqrtf(pow(x, 2) + pow(y, 2));
41 }
42
43 operator glm::vec4() {
44 return glm::vec4(x, y, 1, 1.0);
45 }
46
47 operator glm::vec2() {
48 return glm::vec2(x, y, 1.0);
49 }
50
51 vec2_T<T> operator+(const vec2_T<T> &b) const {
52 return vec2_T<T>(x + b.x, y + b.y);
53 }
54
55 vec2_T<T> operator+(T a) const {
56 return vec2_T<T>(x + a, y + a);
57 }
58
59 vec2_T<T> operator-(const vec2_T<T> &b) const {
60 return vec2_T<T>(x - b.x, y - b.y);
61 }
62
63 vec2_T<T> operator-(T a) const {
64 return vec2_T<T>(x - a, y - a);
65 }
66
67 vec2_T<T> operator*(const vec2_T<T> &b) const {
68 return vec2_T<T>(x * b.x, y * b.y);
69 }
70
71 vec2_T<float> operator*(float a) const {
72 return vec2_T<float>(x * a, y * a);
73 }
74
75 vec2_T<T> operator/(const vec2_T<T> &b) const {
76 return vec2_T<T>(x / b.x, y / b.y);
77 }
78
79 vec2_T<T> operator/(T a) const {
80 return vec2_T<T>(x / a, y / a);
81 }
82
84 return vec2_T<T>(-x, -y);
85 }
86
88 x += in.x;
89 y += in.y;
90 return *this;
91 }
92
94 x -= in.x;
95 y -= in.y;
96 return *this;
97 }
98
99 // void operator*=(const vec2_T<T> &in) {
100 // x *= in.x;
101 // y *= in.y;
102 // }
103
104 template <class U>
105 void operator*=(const U &in) {
106 x *= in;
107 y *= in;
108 }
109
110 // void operator/=(const vec2_T<T> &in) {
111 // x /= in.x;
112 // y /= in.y;
113 // }
114
115 template <class U>
116 void operator/=(const U &in) {
117 x /= in;
118 y /= in;
119 }
120
121 inline bool operator==(const vec2_T<T> &in) const {
122 return x == in.x && y == in.y;
123 }
124
125 inline bool operator!=(const vec2_T<T> &in) const {
126 return !operator==(in);
127 }
128
129 friend std::ostream &operator<<(std::ostream &out, const vec2_T<T> &vec) {
130 out << "( " << vec.x << ", " << vec.y << " )";
131 return out;
132 }
133
135 T temp = sqrt(x * x + y * y);
136 *this = *this / temp;
137 return *this;
138 }
139
140 static vec2_T<T> normalize(const vec2_T<T> &in) {
141 T temp = sqrt(in.x * in.x + in.y * in.y);
142 return vec2_T<T>(in / temp);
143 }
144
145 static double dist(const vec2_T<T> &a, const vec2_T<T> &b) {
146 return sqrtf(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
147 }
148
149 static T dot(const vec2_T<T> &a, const vec2_T<T> &b) {
150 return a.x * b.x + a.y * b.y;
151 }
152};
154
160template <class T>
161struct vec3_T {
162 T x, y, z;
163
164 vec3_T() { x = y = z = T(0); }
165 explicit vec3_T(T a) : x(a), y(a), z(a) {}
166 vec3_T(T a, T b, T c) : x(a), y(b), z(c) {}
167 vec3_T(const glm::vec3 &in) : x(in.x), y(in.y), z(in.z) {}
168 vec3_T(const vec2_T<T> &in) : x(in.x), y(in.y), z(1) {}
169 vec3_T(const vec4_T<T> &in) : x(in.x), y(in.y), z(in.z) {}
170
171 template <class U>
172 vec3_T(vec3_T<U> &in) : x(in.x), y(in.y), z(in.z) {}
173
174 float getmag() const {
175 return sqrtf(pow(x, 2) + pow(y, 2) + pow(z, 2));
176 }
177
178 operator glm::vec4() {
179 return glm::vec4(x, y, z, 1.0);
180 }
181
182 operator glm::vec3() {
183 return glm::vec3(x, y, z);
184 }
185
186 vec3_T<T> operator+(const vec3_T<T> &b) const {
187 return vec3_T<T>(x + b.x, y + b.y, z + b.z);
188 }
189
190 vec3_T<T> operator+(T a) const {
191 return vec3_T<T>(x + a, y + a, z + a);
192 }
193
194 vec3_T<T> operator-(const vec3_T<T> &b) const {
195 return vec3_T<T>(x - b.x, y - b.y, z - b.z);
196 }
197
198 vec3_T<T> operator-(T a) const {
199 return vec3_T<T>(x - a, y - a, z - a);
200 }
201
202 vec3_T<T> operator*(const vec3_T<T> &b) const {
203 return vec3_T<T>(x * b.x, y * b.y, z * b.z);
204 }
205
206 template <class U>
207 vec3_T<T> operator*(const U &a) const {
208 return vec3_T<T>(x * a, y * a, z * a);
209 }
210
211 vec3_T<T> operator/(const vec3_T<T> &b) const {
212 return vec3_T<T>(x / b.x, y / b.y, z / b.z);
213 }
214
215 template <class U>
216 vec3_T<T> operator/(const U &a) const {
217 return vec3_T<T>(x / a, y / a, z / a);
218 }
219
221 return vec3_T<T>(-x, -y, -z);
222 }
223
225 x += in.x;
226 y += in.y;
227 z += in.z;
228 return *this;
229 }
230
232 x -= in.x;
233 y -= in.y;
234 z -= in.z;
235 return *this;
236 }
237
238 // vec3_T<T> operator*=(const vec3_T<T> &in) {
239 // x *= in.x;
240 // y *= in.y;
241 // z *= in.z;
242 // return *this;
243 // }
244
245 template <class U>
246 void operator*=(const U &a) {
247 x *= a;
248 y *= a;
249 z *= a;
250 }
251
252 // void operator/=(const vec3_T<T> &in) {
253 // x /= in.x;
254 // y /= in.y;
255 // z /= in.z;
256 // }
257
258 template <class U>
259 void operator/=(const U &in) {
260 x /= in;
261 y /= in;
262 z /= in;
263 }
264
265 inline bool operator==(const vec3_T<T> &in) const {
266 return x == in.x && y == in.y && z == in.z;
267 }
268
269 inline bool operator!=(const vec3_T<T> &in) const {
270 return !operator==(in);
271 }
272
273 friend std::ostream &operator<<(std::ostream &out, const vec3_T<T> &vec) {
274 out << "( " << vec.x << ", " << vec.y << ", " << vec.z << " )";
275 return out;
276 }
277
279 T temp = sqrt(x * x + y * y + z * z);
280 *this = *this / temp;
281 return *this;
282 }
283
284 static vec3_T<T> normalize(const vec3_T<T> &in) {
285 T temp = sqrt(in.x * in.x + in.y * in.y + in.z * in.z);
286 return vec3_T<T>(in / temp);
287 }
288
289 static double dist(const vec3_T<T> &a, const vec3_T<T> &b) {
290 return sqrtf(pow(b.x - a.x, 2) + pow(b.y - a.y, 2) + pow(b.z - a.z, 2));
291 }
292
293 static vec3_T<T> cross(const vec3_T<T> &a, const vec3_T<T> &b) {
294 vec3_T<T> ret;
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;
298 return ret.normalize();
299 }
300
301 static T dot(const vec3_T<T> &a, const vec3_T<T> &b) {
302 return a.x * b.x + a.y * b.y + a.z * b.z;
303 }
304};
306
312template <class T>
313struct vec4_T {
314 T x, y, z, w;
315
316 vec4_T() { x = y = z = w = 0; }
317 explicit vec4_T(T a) : x(a), y(a), z(a), w(a) {}
318 vec4_T(T a, T b, T c, T d) : x(a), y(b), z(c), w(d) {}
319 vec4_T(const glm::vec4 &in) : x(in.x), y(in.y), z(in.z), w(in.w) {}
320 vec4_T(glm::vec4 &in) : x(in.x), y(in.y), z(in.z), w(in.w) {}
321 vec4_T(const vec3_T<T> &in, const T &iw = 1) : x(in.x), y(in.y), z(in.z), w(iw) {}
322
323 float getmag() const {
324 return sqrtf(pow(x, 2) + pow(y, 2) + pow(z, 2) + pow(w, 2));
325 }
326
327 operator glm::vec4() {
328 return glm::vec4(x, y, z, w);
329 }
330 vec4_T<T> operator+(const vec4_T<T> &b) const {
331 return vec4_T<T>(x + b.x, y + b.y, z + b.z, w + b.w);
332 }
333
334 vec4_T<T> operator+(const T &a) const {
335 return vec4_T<T>(x + a, y + a, z + a, w + a);
336 }
337
338 vec4_T<T> operator-(const vec4_T<T> &b) const {
339 return vec4_T<T>(x - b.x, y - b.y, z - b.z, w - b.w);
340 }
341
342 vec4_T<T> operator-(T a) const {
343 return vec4_T<T>(x - a, y - a, z - a, w - a);
344 }
345
346 template <class U>
347 vec4_T<T> operator*(const U &a) const {
348 return vec4_T<T>(x * a, y * a, z * a, w * a);
349 }
350 vec4_T<T> operator/(const vec4_T<T> &b) const {
351 return vec4_T<T>(x / b.x, y / b.y, z / b.z, w / b.w);
352 }
353
354 template <class U>
355 vec4_T<T> operator/(const U &a) const {
356 return vec4_T<T>(x / a, y / a, z / a, w / a);
357 }
358
360 return vec4_T<T>(-x, -y, -z, -w);
361 }
362
363 void operator+=(const vec4_T<T> &in) {
364 x += in.x;
365 y += in.y;
366 z += in.z;
367 w += in.w;
368 }
369
370 void operator-=(const vec4_T<T> &in) {
371 x -= in.x;
372 y -= in.y;
373 z -= in.z;
374 w -= in.w;
375 }
376
377 // void operator*=(const vec4_T<T> &in) {
378 // x *= in.x;
379 // y *= in.y;
380 // z *= in.z;
381 // w *= in.w;
382 // }
383
384 template <class U>
385 void operator*=(const U &in) {
386 x *= in;
387 y *= in;
388 z *= in;
389 w *= in;
390 }
391
392 template <class U>
393 void operator/=(const U &in) {
394 x /= in;
395 y /= in;
396 z /= in;
397 w /= in;
398 }
399
400 // void operator/=(const vec4_T<T> &in) {
401 // x /= in.x;
402 // y /= in.y;
403 // z /= in.z;
404 // w /= in.w;
405 // }
406
407 inline bool operator==(const vec4_T<T> &in) const {
408 return x == in.x && y == in.y && z == in.z && w == in.w;
409 }
410
411 inline bool operator!=(const vec4_T<T> &in) const {
412 return !operator==(in);
413 }
414
415 friend std::ostream &operator<<(std::ostream &out, const vec4_T<T> &vec) {
416 out << "( " << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << " )";
417 return out;
418 }
419
421 T temp = sqrt(x * x + y * y + z * z + w * w);
422 *this = *this / temp;
423 return *this;
424 }
425
426 static vec4_T<T> normalize(const vec4_T<T> &in) {
427 T temp = sqrt(in.x * in.x + in.y * in.y + in.z * in.z + in.w * in.w);
428 return vec4_T<T>(in / temp);
429 }
430
431 static double dist(const vec4_T<T> &a, const vec4_T<T> &b) {
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));
433 }
434
435 static T dot(const vec4_T<T> &a, const vec4_T<T> &b) {
436 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
437 }
438};
440
446template <class Type, size_t x_dim, size_t y_dim>
447class mat {
448 protected:
449 std::array<Type, x_dim * y_dim> m_data;
450
451 public:
452 mat() {
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;
456 }
457
458 mat(const Type &in) {
459 for (int i = 0; i < y_dim; i++)
460 for (size_t j = 0; j < x_dim; j++)
461 at(i, j) = in;
462 }
463
464 mat(const std::array<Type, x_dim * y_dim> &in) {
465 assert(in.size() == x_dim * y_dim);
466 m_data = in;
467 }
468
469 template <class U>
471 for (int i = 0; i < y_dim; i++)
472 for (size_t j = 0; j < x_dim; j++)
473 at(i, j) = in(i, j);
474 }
475
476 inline Type &operator()(const size_t &r, const size_t &c) {
477 assert(r >= 0 && r < y_dim && c >= 0 && c < x_dim);
478 return m_data[r * x_dim + c];
479 }
480
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];
484 }
485
486 inline const Type &at(const size_t &r, const size_t &c) const {
487 return m_data[r * x_dim + c];
488 }
489
490 inline Type &at(const size_t &r, const size_t &c) {
491 return m_data[r * x_dim + c];
492 }
493
494 void print() const {
495 for (size_t i = 0; i < y_dim; i++) {
496 printf("(");
497 for (size_t j = 0; j < x_dim; j++)
498 printf("%8.3lf", at(i, j));
499 printf(")\n");
500 }
501 // printf("\n");
502 }
503
505 mat<Type, x_dim, y_dim> temp = *this;
506 size_t i, j, k;
507 Type constant;
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);
514 }
515 }
516 return temp;
517 }
518
520 assert(x_dim == y_dim);
521 mat<Type, x_dim, y_dim> temp = *this;
523
524 auto determinant = this->determinant();
525 if (determinant < 1e-7 && determinant > -1e-7) {
526 printf("determinant is zero \n");
527 throw std::runtime_error("inverse of the doesnot exist");
528 }
529
530 size_t i, j, k;
531
532 Type constant;
533
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);
540
541 auto *swaptemp = new Type[x_dim];
542
543 memcpy(swaptemp, dest, x_dim * sizeof(Type));
544 memcpy(dest, src, x_dim * sizeof(Type));
545 memcpy(src, swaptemp, x_dim * sizeof(Type));
546
547 dest = &inv(j, 0);
548 src = &inv(i, 0);
549
550 memcpy(swaptemp, dest, x_dim * sizeof(Type));
551 memcpy(dest, src, x_dim * sizeof(Type));
552 memcpy(src, swaptemp, x_dim * sizeof(Type));
553
554 delete[] swaptemp;
555 }
556 }
557 }
558
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;
564
565 for (j = 0; j < y_dim; j++) {
566 if (j == i)
567 continue;
568 constant = temp(j, i);
569 for (k = i; k < x_dim; k++) {
570 temp(j, k) = temp(j, k) - temp(i, k) * constant;
571 }
572
573 for (k = 0; k < x_dim; k++) {
574 inv(j, k) = inv(j, k) - inv(i, k) * constant;
575 }
576 }
577 }
578 return inv;
579 }
580
582 mat<Type, x_dim, y_dim> ret = *this;
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);
586 return ret;
587 }
588
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);
594 return ret;
595 }
596
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);
602 return ret;
603 }
604
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);
609 }
610
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);
615 }
616
618 assert(x_dim == y_dim);
619 std::array<Type, x_dim * y_dim> new_buffer;
620 int i, j, k;
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);
626 }
627 }
628 m_data = std::move(new_buffer);
629 }
630
631 void operator*=(const Type &a) {
632 for (size_t i = 0; i < y_dim; i++)
633 for (size_t j = 0; j < x_dim; j++)
634 at(i, j) *= a;
635 }
636
637 void operator/=(const Type &a) {
638 for (size_t i = 0; i < y_dim; i++)
639 for (size_t j = 0; j < x_dim; j++)
640 at(i, j) /= a;
641 }
642
644 assert(x_dim == 2 && y_dim == 2);
645 vec2_T<Type> ret;
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;
648 return ret;
649 }
650
651 mat<Type, x_dim, y_dim> operator*(const Type &a) const {
652 mat<Type, x_dim, y_dim> ret = *this;
653 for (auto &i : ret.m_data)
654 i *= a;
655 return ret;
656 }
657
658 mat<Type, x_dim, y_dim> operator/(const Type &a) const {
659 mat<Type, x_dim, y_dim> ret = *this;
660 for (auto &i : ret.m_data)
661 i /= a;
662 return ret;
663 }
664
665 Type determinant() {
666 assert(x_dim == y_dim);
668 Type ret = 1;
669 for (size_t i = 0; i < y_dim; i++)
670 ret *= temp(i, i);
671
672 return ret;
673 }
674};
675
679template <class Type, size_t x1, size_t y1, size_t x2>
682 int i, j, k;
683 for (i = 0; i < y1; i++) {
684 for (j = 0; j < x2; j++) {
685 ret.at(i, j) = 0;
686 for (k = 0; k < x1; k++)
687 ret.at(i, j) += first(i, k) * second(k, j);
688 }
689 }
690 return ret;
691}
692
696template <class Type>
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)});
714}
715
719template <class Type>
721 return vec3_T<Type>(
722 mat.at(0, 0) * in.x + mat.at(0, 1) * in.y + mat.at(0, 2) * in.z,
723 mat.at(1, 0) * in.x + mat.at(1, 1) * in.y + mat.at(1, 2) * in.z,
724 mat.at(2, 0) * in.x + mat.at(2, 1) * in.y + mat.at(2, 2) * in.z);
725}
726
730template <class Type>
732 return vec3_T<Type>(
733 mat.at(0, 0) * in.x + mat.at(0, 1) * in.y + mat.at(0, 2) * in.z,
734 mat.at(1, 0) * in.x + mat.at(1, 1) * in.y + mat.at(1, 2) * in.z,
735 mat.at(2, 0) * in.x + mat.at(2, 1) * in.y + mat.at(2, 2) * in.z);
736}
737
741template <class Type>
743 return vec4_T<Type>(
744 mat.at(0, 0) * in.x + mat.at(0, 1) * in.y + mat.at(0, 2) * in.z + mat.at(0, 3) * in.w,
745 mat.at(1, 0) * in.x + mat.at(1, 1) * in.y + mat.at(1, 2) * in.z + mat.at(1, 3) * in.w,
746 mat.at(2, 0) * in.x + mat.at(2, 1) * in.y + mat.at(2, 2) * in.z + mat.at(2, 3) * in.w,
747 mat.at(3, 0) * in.x + mat.at(3, 1) * in.y + mat.at(3, 2) * in.z + mat.at(3, 3) * in.w);
748}
749
758
759static void printVec(const vec4 &in) {
760 printf("(%f, %f, %f, %f)\n", in.x, in.y, in.z, in.w);
761}
762static void printVec(const vec3 &in) {
763 printf("(%f, %f, %f)\n", in.x, in.y, in.z);
764}
765
766static void printVec(const vec2 &in) {
767 printf("(%f, %f)\n", in.x, in.y);
768}
A general matrix class for any size of matrix.
Definition: maths.h:447
const Type & operator()(const size_t &r, const size_t &c) const
Definition: maths.h:481
mat(const Type &in)
Definition: maths.h:458
Type & operator()(const size_t &r, const size_t &c)
Definition: maths.h:476
void operator-=(const mat< Type, x_dim, y_dim > &in)
Definition: maths.h:611
mat< Type, x_dim, y_dim > operator*(const Type &a) const
Definition: maths.h:651
mat< Type, x_dim, y_dim > operator-()
Definition: maths.h:581
vec2_T< Type > operator*(const vec2_T< Type > &in) const
Definition: maths.h:643
mat< Type, x_dim, y_dim > operator+(const mat< Type, x_dim, y_dim > &in)
Definition: maths.h:589
void operator/=(const Type &a)
Definition: maths.h:637
void print() const
Definition: maths.h:494
Type determinant()
Definition: maths.h:665
mat< Type, x_dim, y_dim > toUpper()
Definition: maths.h:504
mat< Type, x_dim, y_dim > operator/(const Type &a) const
Definition: maths.h:658
mat(const std::array< Type, x_dim *y_dim > &in)
Definition: maths.h:464
mat()
Definition: maths.h:452
void operator*=(const Type &a)
Definition: maths.h:631
mat< Type, x_dim, y_dim > inverse() const
Definition: maths.h:519
mat(const mat< U, x_dim, y_dim > &in)
Definition: maths.h:470
Type & at(const size_t &r, const size_t &c)
Definition: maths.h:490
const Type & at(const size_t &r, const size_t &c) const
Definition: maths.h:486
std::array< Type, x_dim *y_dim > m_data
Definition: maths.h:449
void operator+=(const mat< Type, x_dim, y_dim > &in)
Definition: maths.h:605
mat< Type, x_dim, y_dim > operator-(const mat< Type, x_dim, y_dim > &in)
Definition: maths.h:597
void operator*=(const mat< Type, x_dim, y_dim > &in)
Definition: maths.h:617
mat< float, 3, 3 > mat3f
Definition: maths.h:751
mat< double, 3, 3 > mat3d
Definition: maths.h:753
constexpr float pi
Definition: maths.h:12
mat< int, 4, 4 > mat4i
Definition: maths.h:754
mat< float, 4, 4 > mat4f
Definition: maths.h:750
vec4_T< float > vec4
Definition: maths.h:439
vec2_T< float > vec2
Definition: maths.h:153
constexpr float epsilon
Definition: maths.h:11
mat< unsigned int, 3, 3 > mat3u
Definition: maths.h:757
constexpr float radian
Definition: maths.h:10
vec3_T< float > vec3
Definition: maths.h:305
mat< unsigned int, 4, 4 > mat4u
Definition: maths.h:756
mat< double, 4, 4 > mat4d
Definition: maths.h:752
mat< Type, x2, y1 > operator*(const mat< Type, x1, y1 > &first, const mat< Type, x2, x1 > &second)
operator overload for matrix multiplication.
Definition: maths.h:680
mat< int, 3, 3 > mat3i
Definition: maths.h:755
2d Vector class for storing x and y.
Definition: maths.h:25
vec2_T< T > operator/(T a) const
Definition: maths.h:79
vec2_T(const vec3_T< T > &in)
Definition: maths.h:33
vec2_T< T > operator-() const
Definition: maths.h:83
vec2_T(glm::vec2 &in)
Definition: maths.h:32
bool operator!=(const vec2_T< T > &in) const
Definition: maths.h:125
vec2_T< float > operator*(float a) const
Definition: maths.h:71
T y
Definition: maths.h:26
void operator*=(const U &in)
Definition: maths.h:105
vec2_T(const glm::vec2 &in)
Definition: maths.h:31
vec2_T< T > operator-=(const vec2_T< T > &in)
Definition: maths.h:93
vec2_T< T > operator-(const vec2_T< T > &b) const
Definition: maths.h:59
float getmag() const
Definition: maths.h:39
static double dist(const vec2_T< T > &a, const vec2_T< T > &b)
Definition: maths.h:145
vec2_T(T a, T b)
Definition: maths.h:30
vec2_T< T > operator+(const vec2_T< T > &b) const
Definition: maths.h:51
friend std::ostream & operator<<(std::ostream &out, const vec2_T< T > &vec)
Definition: maths.h:129
bool operator==(const vec2_T< T > &in) const
Definition: maths.h:121
vec2_T< T > operator/(const vec2_T< T > &b) const
Definition: maths.h:75
void operator/=(const U &in)
Definition: maths.h:116
static T dot(const vec2_T< T > &a, const vec2_T< T > &b)
Definition: maths.h:149
vec2_T(const vec4_T< T > &in)
Definition: maths.h:34
vec2_T< T > operator*(const vec2_T< T > &b) const
Definition: maths.h:67
vec2_T< T > normalize()
Definition: maths.h:134
static vec2_T< T > normalize(const vec2_T< T > &in)
Definition: maths.h:140
vec2_T(const vec2_T< U > &in)
Definition: maths.h:37
vec2_T(T a)
Definition: maths.h:29
vec2_T()
Definition: maths.h:28
vec2_T< T > operator-(T a) const
Definition: maths.h:63
vec2_T< T > operator+(T a) const
Definition: maths.h:55
vec2_T< T > operator+=(const vec2_T< T > &in)
Definition: maths.h:87
T x
Definition: maths.h:26
3 element Vector class for storing x, y and z.
Definition: maths.h:161
vec3_T()
Definition: maths.h:164
vec3_T(const vec4_T< T > &in)
Definition: maths.h:169
vec3_T(T a)
Definition: maths.h:165
vec3_T< T > operator/(const vec3_T< T > &b) const
Definition: maths.h:211
vec3_T< T > operator/(const U &a) const
Definition: maths.h:216
static T dot(const vec3_T< T > &a, const vec3_T< T > &b)
Definition: maths.h:301
vec3_T< T > operator-(T a) const
Definition: maths.h:198
vec3_T(vec3_T< U > &in)
Definition: maths.h:172
vec3_T< T > operator+=(const vec3_T< T > &in)
Definition: maths.h:224
vec3_T< T > operator*(const vec3_T< T > &b) const
Definition: maths.h:202
vec3_T(T a, T b, T c)
Definition: maths.h:166
vec3_T< T > operator+(T a) const
Definition: maths.h:190
void operator/=(const U &in)
Definition: maths.h:259
bool operator!=(const vec3_T< T > &in) const
Definition: maths.h:269
static vec3_T< T > cross(const vec3_T< T > &a, const vec3_T< T > &b)
Definition: maths.h:293
vec3_T< T > operator+(const vec3_T< T > &b) const
Definition: maths.h:186
static double dist(const vec3_T< T > &a, const vec3_T< T > &b)
Definition: maths.h:289
vec3_T(const vec2_T< T > &in)
Definition: maths.h:168
vec3_T< T > operator-() const
Definition: maths.h:220
float getmag() const
Definition: maths.h:174
vec3_T< T > operator-=(const vec3_T< T > &in)
Definition: maths.h:231
bool operator==(const vec3_T< T > &in) const
Definition: maths.h:265
T z
Definition: maths.h:162
void operator*=(const U &a)
Definition: maths.h:246
vec3_T< T > operator*(const U &a) const
Definition: maths.h:207
vec3_T(const glm::vec3 &in)
Definition: maths.h:167
static vec3_T< T > normalize(const vec3_T< T > &in)
Definition: maths.h:284
T x
Definition: maths.h:162
vec3_T< T > operator-(const vec3_T< T > &b) const
Definition: maths.h:194
vec3_T< T > normalize()
Definition: maths.h:278
T y
Definition: maths.h:162
friend std::ostream & operator<<(std::ostream &out, const vec3_T< T > &vec)
Definition: maths.h:273
4 element Vector class for storing x, y, z and w.
Definition: maths.h:313
vec4_T< T > operator/(const U &a) const
Definition: maths.h:355
static T dot(const vec4_T< T > &a, const vec4_T< T > &b)
Definition: maths.h:435
vec4_T< T > operator+(const T &a) const
Definition: maths.h:334
vec4_T(glm::vec4 &in)
Definition: maths.h:320
T z
Definition: maths.h:314
void operator*=(const U &in)
Definition: maths.h:385
vec4_T< T > operator*(const U &a) const
Definition: maths.h:347
void operator+=(const vec4_T< T > &in)
Definition: maths.h:363
T w
Definition: maths.h:314
vec4_T(const vec3_T< T > &in, const T &iw=1)
Definition: maths.h:321
static double dist(const vec4_T< T > &a, const vec4_T< T > &b)
Definition: maths.h:431
void operator-=(const vec4_T< T > &in)
Definition: maths.h:370
vec4_T< T > operator/(const vec4_T< T > &b) const
Definition: maths.h:350
vec4_T(T a, T b, T c, T d)
Definition: maths.h:318
vec4_T< T > operator-(T a) const
Definition: maths.h:342
bool operator!=(const vec4_T< T > &in) const
Definition: maths.h:411
vec4_T< T > operator-(const vec4_T< T > &b) const
Definition: maths.h:338
T y
Definition: maths.h:314
vec4_T< T > operator+(const vec4_T< T > &b) const
Definition: maths.h:330
static vec4_T< T > normalize(const vec4_T< T > &in)
Definition: maths.h:426
void operator/=(const U &in)
Definition: maths.h:393
vec4_T()
Definition: maths.h:316
vec4_T< T > normalize()
Definition: maths.h:420
float getmag() const
Definition: maths.h:323
friend std::ostream & operator<<(std::ostream &out, const vec4_T< T > &vec)
Definition: maths.h:415
vec4_T< T > operator-() const
Definition: maths.h:359
vec4_T(const glm::vec4 &in)
Definition: maths.h:319
bool operator==(const vec4_T< T > &in) const
Definition: maths.h:407
vec4_T(T a)
Definition: maths.h:317
T x
Definition: maths.h:314