From 10d0d84309702e8d2f826b8c58569be5d710ebf2 Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Sat, 28 Nov 2020 20:03:11 +0400 Subject: =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB=20Matrix.h?= =?UTF-8?q?=20=D0=B8=20Transform.h=20=D0=B2=206=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D1=87=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task06/Guschin/Matrix.h | 179 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) (limited to 'task06/Guschin/Matrix.h') diff --git a/task06/Guschin/Matrix.h b/task06/Guschin/Matrix.h index 3aa0bbf..2334edf 100644 --- a/task06/Guschin/Matrix.h +++ b/task06/Guschin/Matrix.h @@ -1,11 +1,31 @@ #pragma once +class vec3; +class vec4; + class vec2 { public: float x, y; vec2() {} vec2(float a, float b) : x(a), y(b) {} + + vec2& operator*=(const vec2& v) + { + x *= v.x; + y *= v.y; + return *this; + } + + const vec2 operator*(const vec2& v) + { + return vec2(*this) *= v; + } + + float& operator[](int i) + { + return ((float*)this)[i]; + } }; class vec3 @@ -35,12 +55,103 @@ public: } }; +class vec4 +{ +public: + float x, y, z, a; + vec4() {} + vec4(float a, float b, float c, float d) : x(a), y(b), z(c), a(d) {} + vec4(vec3 v, float c) : vec4(v.x, v.y, v.z, c) {} + + vec4& operator *= (const vec4& v) + { + x *= v.x; + y *= v.y; + z *= v.z; + a *= v.a; + return *this; + } + + const vec4 operator* (const vec4& v) + { + return vec4(*this) *= v; + } + + float& operator[] (int i) + { + return ((float*)this)[i]; + } +}; + +float dot(vec2 v1, vec2 v2) +{ + vec2 tmp = v1 * v2; + return tmp.x + tmp.y; +} + float dot(vec3 v1, vec3 v2) { vec3 tmp = v1 * v2; return tmp.x + tmp.y + tmp.z; } +float dot(vec4 v1, vec4 v2) +{ + vec4 tmp = v1 * v2; + return tmp.x + tmp.y + tmp.z + tmp.a; +} + +class mat4 +{ +public: + vec4 row1, row2, row3, row4; + mat4() {} + mat4(vec4 r1, vec4 r2, vec4 r3, vec4 r4) : row1(r1), row2(r2), row3(r3), row4(r4) {} + mat4(float a) + { + row1 = vec4(a, 0.f, 0.f, 0.f); + row2 = vec4(0.f, a, 0.f, 0.f); + row3 = vec4(0.f, 0.f, a, 0.f); + row4 = vec4(0.f, 0.f, 0.f, a); + } + + vec4& operator[](int i) + { + return ((vec4*)this)[i]; + } + + mat4 transpose() + { + mat4 tmp(*this); + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + (*this)[i][j] = tmp[j][i]; + return *this; + } + + const vec4 operator*(const vec4& v) + { + vec4* res = new(vec4); + for (int i = 0; i < 4; ++i) + (*res)[i] = dot((*this)[i], v); + return *res; + } + + mat4& operator*=(const mat4& m) + { + mat4 A(*this), B(m); + B.transpose(); + for (int i = 0; i < 4; ++i) + (*this)[i] = A * B[i]; + return (*this).transpose(); + } + + const mat4 operator*(const mat4& m) + { + return mat4(*this) *= m; + } +}; + class mat3 { public: @@ -53,6 +164,11 @@ public: row2 = vec3(0.f, a, 0.f); row3 = vec3(0.f, 0.f, a); } + mat3(mat4 m) { + row1 = vec3(m.row1.x, m.row1.y, m.row1.z); + row2 = vec3(m.row2.x, m.row2.y, m.row2.z); + row3 = vec3(m.row3.x, m.row3.y, m.row3.z); + } vec3& operator[](int i) { @@ -91,8 +207,71 @@ public: } }; +class mat2 +{ +public: + vec2 row1, row2; + + mat2(vec2 r1, vec2 r2) : row1(r1), row2(r2) {} + mat2(float a) + { + row1 = vec2(a, 0.f); + row2 = vec2(0.f, a); + } + mat2() {} + mat2(mat3 m) + { + row1 = vec2(m[0][0], m[0][1]); + row2 = vec2(m[1][0], m[1][1]); + } + + vec2& operator[](int i) + { + return ((vec2*)this)[i]; + } + + mat2 transpose() + { + mat2 tmp(*this); + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + (*this)[i][j] = tmp[j][i]; + return *this; + } + + const vec2 operator* (const vec2 &v) + { + vec2* res = new(vec2); + for (int i = 0; i < 2; i++) + { + (*res)[i] = dot((*this)[i], v); + } + return *res; + } + + mat2& operator*= (const mat2 &m) + { + mat2 A(*this), B(m); + B.transpose(); + for (int i = 0; i < 2; i++) + (*this)[i] = A * B[i]; + + return (*this).transpose(); + } + + const mat2 operator* (const mat2 &m) + { + return mat2(*this) *= m; + } +}; + vec2 normalize(vec3 v) { return vec2(v.x / v.z, v.y / v.z); } +vec3 normalize(vec4 v) +{ + return vec3(v.x / v.a, v.y / v.a, v.z / v.a); +} + -- cgit v1.2.3