summaryrefslogtreecommitdiff
path: root/task06/Guschin/Matrix.h
diff options
context:
space:
mode:
Diffstat (limited to 'task06/Guschin/Matrix.h')
-rw-r--r--task06/Guschin/Matrix.h98
1 files changed, 98 insertions, 0 deletions
diff --git a/task06/Guschin/Matrix.h b/task06/Guschin/Matrix.h
new file mode 100644
index 0000000..3aa0bbf
--- /dev/null
+++ b/task06/Guschin/Matrix.h
@@ -0,0 +1,98 @@
+#pragma once
+
+class vec2
+{
+public:
+ float x, y;
+ vec2() {}
+ vec2(float a, float b) : x(a), y(b) {}
+};
+
+class vec3
+{
+public:
+ float x, y, z;
+ vec3() {}
+ vec3(float a, float b, float c) : x(a), y(b), z(c) {}
+ vec3(vec2 v, float c) : vec3(v.x, v.y, c) {}
+
+ vec3& operator*=(const vec3& v)
+ {
+ x *= v.x;
+ y *= v.y;
+ z *= v.z;
+ return *this;
+ }
+
+ const vec3 operator*(const vec3& v)
+ {
+ return vec3(*this) *= v;
+ }
+
+ float& operator[](int i)
+ {
+ return ((float*)this)[i];
+ }
+};
+
+float dot(vec3 v1, vec3 v2)
+{
+ vec3 tmp = v1 * v2;
+ return tmp.x + tmp.y + tmp.z;
+}
+
+class mat3
+{
+public:
+ vec3 row1, row2, row3;
+ mat3() {}
+ mat3(vec3 r1, vec3 r2, vec3 r3) : row1(r1), row2(r2), row3(r3) {}
+ mat3(float a)
+ {
+ row1 = vec3(a, 0.f, 0.f);
+ row2 = vec3(0.f, a, 0.f);
+ row3 = vec3(0.f, 0.f, a);
+ }
+
+ vec3& operator[](int i)
+ {
+ return ((vec3*)this)[i];
+ }
+
+ mat3 transpose()
+ {
+ mat3 tmp(*this);
+ for (int i = 0; i < 3; ++i)
+ for (int j = 0; j < 3; ++j)
+ (*this)[i][j] = tmp[j][i];
+ return *this;
+ }
+
+ const vec3 operator*(const vec3& v)
+ {
+ vec3* res = new(vec3);
+ for (int i = 0; i < 3; ++i)
+ (*res)[i] = dot((*this)[i], v);
+ return *res;
+ }
+
+ mat3& operator*=(const mat3& m)
+ {
+ mat3 A(*this), B(m);
+ B.transpose();
+ for (int i = 0; i < 3; ++i)
+ (*this)[i] = A * B[i];
+ return (*this).transpose();
+ }
+
+ const mat3 operator*(const mat3& m)
+ {
+ return mat3(*this) *= m;
+ }
+};
+
+vec2 normalize(vec3 v)
+{
+ return vec2(v.x / v.z, v.y / v.z);
+}
+