From 7fd9814e33a6f322571f8bc369baeec1138da57c Mon Sep 17 00:00:00 2001 From: Neirokan Date: Thu, 4 Nov 2021 03:35:34 +0300 Subject: [PATCH] Math classes optimization --- engine/Matrix4x4.cpp | 64 +++++++++++++++++++++++--------------------- engine/Vec2D.cpp | 8 +++--- engine/Vec3D.cpp | 8 +++--- engine/Vec4D.cpp | 8 +++--- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/engine/Matrix4x4.cpp b/engine/Matrix4x4.cpp index 2f02ed9..541a668 100644 --- a/engine/Matrix4x4.cpp +++ b/engine/Matrix4x4.cpp @@ -37,15 +37,10 @@ Vec3D Matrix4x4::operator*(const Vec3D &vec) const { Matrix4x4 Matrix4x4::Identity() { Matrix4x4 result; - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - if (i == j) { - result._arr[j][i] = 1.0; - } else { - result._arr[j][i] = 0.0; - } - } - } + result._arr[0][0] = 1.0; + result._arr[1][1] = 1.0; + result._arr[2][2] = 1.0; + result._arr[3][3] = 1.0; return result; } @@ -93,12 +88,15 @@ Matrix4x4 Matrix4x4::Translation(const Vec3D &v) { Matrix4x4 Matrix4x4::RotationX(double rx) { Matrix4x4 Rx{}; + + double c = cos(rx), s = sin(rx); + Rx._arr[0][0] = 1.0; - Rx._arr[1][1] = cos(rx); - Rx._arr[1][2] = -sin(rx); - Rx._arr[2][1] = sin(rx); - Rx._arr[2][2] = cos(rx); + Rx._arr[1][1] = c; + Rx._arr[1][2] = -s; + Rx._arr[2][1] = s; + Rx._arr[2][2] = c; Rx._arr[3][3] = 1.0; @@ -108,12 +106,14 @@ Matrix4x4 Matrix4x4::RotationX(double rx) { Matrix4x4 Matrix4x4::RotationY(double ry) { Matrix4x4 Ry{}; + double c = cos(ry), s = sin(ry); + Ry._arr[1][1] = 1.0; - Ry._arr[0][0] = cos(ry); - Ry._arr[0][2] = sin(ry); - Ry._arr[2][0] = -sin(ry); - Ry._arr[2][2] = cos(ry); + Ry._arr[0][0] = c; + Ry._arr[0][2] = s; + Ry._arr[2][0] = -s; + Ry._arr[2][2] = c; Ry._arr[3][3] = 1.0; @@ -123,12 +123,14 @@ Matrix4x4 Matrix4x4::RotationY(double ry) { Matrix4x4 Matrix4x4::RotationZ(double rz) { Matrix4x4 Rz{}; + double c = cos(rz), s = sin(rz); + Rz._arr[2][2] = 1.0; - Rz._arr[0][0] = cos(rz); - Rz._arr[0][1] = -sin(rz); - Rz._arr[1][0] = sin(rz); - Rz._arr[1][1] = cos(rz); + Rz._arr[0][0] = c; + Rz._arr[0][1] = -s; + Rz._arr[1][0] = s; + Rz._arr[1][1] = c; Rz._arr[3][3] = 1.0; @@ -143,17 +145,19 @@ Matrix4x4 Matrix4x4::Rotation(const Vec3D &v, double rv) { Matrix4x4 Rv{}; Vec3D nv(v.normalized()); - Rv._arr[0][0] = cos(rv) + (1.0 - cos(rv)) * nv.x() * nv.x(); - Rv._arr[0][1] = (1.0 - cos(rv)) * nv.x() * nv.y() - sin(rv) * nv.z(); - Rv._arr[0][2] = (1.0 - cos(rv)) * nv.x() * nv.z() + sin(rv) * nv.y(); + double c = cos(rv), s = sin(rv); - Rv._arr[1][0] = (1.0 - cos(rv)) * nv.x() * nv.y() + sin(rv) * nv.z(); - Rv._arr[1][1] = cos(rv) + (1.0 - cos(rv)) * nv.y() * nv.y(); - Rv._arr[1][2] = (1.0 - cos(rv)) * nv.y() * nv.z() - sin(rv) * nv.x(); + Rv._arr[0][0] = c + (1.0 - c) * nv.x() * nv.x(); + Rv._arr[0][1] = (1.0 - c) * nv.x() * nv.y() - s * nv.z(); + Rv._arr[0][2] = (1.0 - c) * nv.x() * nv.z() + s * nv.y(); - Rv._arr[2][0] = (1.0 - cos(rv)) * nv.z() * nv.x() - sin(rv) * nv.y(); - Rv._arr[2][1] = (1.0 - cos(rv)) * nv.z() * nv.y() + sin(rv) * nv.x(); - Rv._arr[2][2] = cos(rv) + (1.0 - cos(rv)) * nv.z() * nv.z(); + Rv._arr[1][0] = (1.0 - c) * nv.x() * nv.y() + s * nv.z(); + Rv._arr[1][1] = c + (1.0 - c) * nv.y() * nv.y(); + Rv._arr[1][2] = (1.0 - c) * nv.y() * nv.z() - s * nv.x(); + + Rv._arr[2][0] = (1.0 - c) * nv.z() * nv.x() - s * nv.y(); + Rv._arr[2][1] = (1.0 - c) * nv.z() * nv.y() + s * nv.x(); + Rv._arr[2][2] = c + (1.0 - c) * nv.z() * nv.z(); Rv._arr[3][3] = 1.0; diff --git a/engine/Vec2D.cpp b/engine/Vec2D.cpp index ef0fd37..4305a8d 100644 --- a/engine/Vec2D.cpp +++ b/engine/Vec2D.cpp @@ -39,7 +39,7 @@ Vec2D Vec2D::operator+(const Vec2D &vec) const { } Vec2D Vec2D::operator-(const Vec2D &vec) const { - return Vec2D(*this) + -vec; + return *this + -vec; } Vec2D Vec2D::operator*(double number) const { @@ -48,7 +48,7 @@ Vec2D Vec2D::operator*(double number) const { Vec2D Vec2D::operator/(double number) const { if (std::abs(number) > Consts::EPS) { - return Vec2D(*this) * (1.0 / number); + return *this * (1.0 / number); } else { throw std::domain_error{"Vec2D::operator/(double number): division by zero"}; } @@ -64,9 +64,9 @@ double Vec2D::abs() const { } Vec2D Vec2D::normalized() const { - double vecAbs = abs(); + double vecAbs = sqrAbs(); if (vecAbs > Consts::EPS) { - return Vec2D(*this) / vecAbs; + return *this / sqrt(vecAbs); } else { return Vec2D(0); } diff --git a/engine/Vec3D.cpp b/engine/Vec3D.cpp index 3a41ca4..349c9a8 100644 --- a/engine/Vec3D.cpp +++ b/engine/Vec3D.cpp @@ -44,7 +44,7 @@ Vec3D Vec3D::operator+(const Vec3D &vec) const { } Vec3D Vec3D::operator-(const Vec3D &vec) const { - return Vec3D(*this) + -vec; + return *this + -vec; } Vec3D Vec3D::operator*(double number) const { @@ -53,7 +53,7 @@ Vec3D Vec3D::operator*(double number) const { Vec3D Vec3D::operator/(double number) const { if (std::abs(number) > Consts::EPS) { - return Vec3D(*this) * (1.0 / number); + return *this * (1.0 / number); } else { throw std::domain_error{"Vec3D::operator/(double number): division by zero"}; } @@ -69,9 +69,9 @@ double Vec3D::abs() const { } Vec3D Vec3D::normalized() const { - double vecAbs = abs(); + double vecAbs = sqrAbs(); if (vecAbs > Consts::EPS) { - return Vec3D(*this) / vecAbs; + return *this / sqrt(vecAbs); } else { return Vec3D(1); } diff --git a/engine/Vec4D.cpp b/engine/Vec4D.cpp index f9a92a6..b11321a 100644 --- a/engine/Vec4D.cpp +++ b/engine/Vec4D.cpp @@ -40,7 +40,7 @@ Vec4D Vec4D::operator+(const Vec4D &point4D) const { } Vec4D Vec4D::operator-(const Vec4D &point4D) const { - return Vec4D(*this) + -point4D; + return *this + -point4D; } Vec4D Vec4D::operator*(double number) const { @@ -49,7 +49,7 @@ Vec4D Vec4D::operator*(double number) const { Vec4D Vec4D::operator/(double number) const { if (std::abs(number) > Consts::EPS) { - return Vec4D(*this) * (1.0 / number); + return *this * (1.0 / number); } else { throw std::domain_error{"Vec4D::operator/(double number): division by zero"}; } @@ -65,9 +65,9 @@ double Vec4D::abs() const { } Vec4D Vec4D::normalized() const { - double vecAbs = abs(); + double vecAbs = sqrAbs(); if (vecAbs > Consts::EPS) { - return Vec4D(*this) / vecAbs; + return *this / sqrt(vecAbs); } else { return Vec4D(1); }