Math classes optimization

master
Neirokan 2021-11-04 03:35:34 +03:00
parent 1ad591ed04
commit 7fd9814e33
4 changed files with 46 additions and 42 deletions

View File

@ -37,15 +37,10 @@ Vec3D Matrix4x4::operator*(const Vec3D &vec) const {
Matrix4x4 Matrix4x4::Identity() { Matrix4x4 Matrix4x4::Identity() {
Matrix4x4 result; Matrix4x4 result;
for (int i = 0; i < 4; i++) { result._arr[0][0] = 1.0;
for (int j = 0; j < 4; j++) { result._arr[1][1] = 1.0;
if (i == j) { result._arr[2][2] = 1.0;
result._arr[j][i] = 1.0; result._arr[3][3] = 1.0;
} else {
result._arr[j][i] = 0.0;
}
}
}
return result; return result;
} }
@ -93,12 +88,15 @@ Matrix4x4 Matrix4x4::Translation(const Vec3D &v) {
Matrix4x4 Matrix4x4::RotationX(double rx) { Matrix4x4 Matrix4x4::RotationX(double rx) {
Matrix4x4 Rx{}; Matrix4x4 Rx{};
double c = cos(rx), s = sin(rx);
Rx._arr[0][0] = 1.0; Rx._arr[0][0] = 1.0;
Rx._arr[1][1] = cos(rx); Rx._arr[1][1] = c;
Rx._arr[1][2] = -sin(rx); Rx._arr[1][2] = -s;
Rx._arr[2][1] = sin(rx); Rx._arr[2][1] = s;
Rx._arr[2][2] = cos(rx); Rx._arr[2][2] = c;
Rx._arr[3][3] = 1.0; Rx._arr[3][3] = 1.0;
@ -108,12 +106,14 @@ Matrix4x4 Matrix4x4::RotationX(double rx) {
Matrix4x4 Matrix4x4::RotationY(double ry) { Matrix4x4 Matrix4x4::RotationY(double ry) {
Matrix4x4 Ry{}; Matrix4x4 Ry{};
double c = cos(ry), s = sin(ry);
Ry._arr[1][1] = 1.0; Ry._arr[1][1] = 1.0;
Ry._arr[0][0] = cos(ry); Ry._arr[0][0] = c;
Ry._arr[0][2] = sin(ry); Ry._arr[0][2] = s;
Ry._arr[2][0] = -sin(ry); Ry._arr[2][0] = -s;
Ry._arr[2][2] = cos(ry); Ry._arr[2][2] = c;
Ry._arr[3][3] = 1.0; Ry._arr[3][3] = 1.0;
@ -123,12 +123,14 @@ Matrix4x4 Matrix4x4::RotationY(double ry) {
Matrix4x4 Matrix4x4::RotationZ(double rz) { Matrix4x4 Matrix4x4::RotationZ(double rz) {
Matrix4x4 Rz{}; Matrix4x4 Rz{};
double c = cos(rz), s = sin(rz);
Rz._arr[2][2] = 1.0; Rz._arr[2][2] = 1.0;
Rz._arr[0][0] = cos(rz); Rz._arr[0][0] = c;
Rz._arr[0][1] = -sin(rz); Rz._arr[0][1] = -s;
Rz._arr[1][0] = sin(rz); Rz._arr[1][0] = s;
Rz._arr[1][1] = cos(rz); Rz._arr[1][1] = c;
Rz._arr[3][3] = 1.0; Rz._arr[3][3] = 1.0;
@ -143,17 +145,19 @@ Matrix4x4 Matrix4x4::Rotation(const Vec3D &v, double rv) {
Matrix4x4 Rv{}; Matrix4x4 Rv{};
Vec3D nv(v.normalized()); Vec3D nv(v.normalized());
Rv._arr[0][0] = cos(rv) + (1.0 - cos(rv)) * nv.x() * nv.x(); double c = cos(rv), s = sin(rv);
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();
Rv._arr[1][0] = (1.0 - cos(rv)) * nv.x() * nv.y() + sin(rv) * nv.z(); Rv._arr[0][0] = c + (1.0 - c) * nv.x() * nv.x();
Rv._arr[1][1] = cos(rv) + (1.0 - cos(rv)) * nv.y() * nv.y(); Rv._arr[0][1] = (1.0 - c) * nv.x() * nv.y() - s * nv.z();
Rv._arr[1][2] = (1.0 - cos(rv)) * nv.y() * nv.z() - sin(rv) * nv.x(); 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[1][0] = (1.0 - c) * nv.x() * nv.y() + s * nv.z();
Rv._arr[2][1] = (1.0 - cos(rv)) * nv.z() * nv.y() + sin(rv) * nv.x(); Rv._arr[1][1] = c + (1.0 - c) * nv.y() * nv.y();
Rv._arr[2][2] = cos(rv) + (1.0 - cos(rv)) * nv.z() * nv.z(); 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; Rv._arr[3][3] = 1.0;

View File

@ -39,7 +39,7 @@ Vec2D Vec2D::operator+(const Vec2D &vec) const {
} }
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 { Vec2D Vec2D::operator*(double number) const {
@ -48,7 +48,7 @@ Vec2D Vec2D::operator*(double number) const {
Vec2D Vec2D::operator/(double number) const { Vec2D Vec2D::operator/(double number) const {
if (std::abs(number) > Consts::EPS) { if (std::abs(number) > Consts::EPS) {
return Vec2D(*this) * (1.0 / number); return *this * (1.0 / number);
} else { } else {
throw std::domain_error{"Vec2D::operator/(double number): division by zero"}; throw std::domain_error{"Vec2D::operator/(double number): division by zero"};
} }
@ -64,9 +64,9 @@ double Vec2D::abs() const {
} }
Vec2D Vec2D::normalized() const { Vec2D Vec2D::normalized() const {
double vecAbs = abs(); double vecAbs = sqrAbs();
if (vecAbs > Consts::EPS) { if (vecAbs > Consts::EPS) {
return Vec2D(*this) / vecAbs; return *this / sqrt(vecAbs);
} else { } else {
return Vec2D(0); return Vec2D(0);
} }

View File

@ -44,7 +44,7 @@ Vec3D Vec3D::operator+(const Vec3D &vec) const {
} }
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 { Vec3D Vec3D::operator*(double number) const {
@ -53,7 +53,7 @@ Vec3D Vec3D::operator*(double number) const {
Vec3D Vec3D::operator/(double number) const { Vec3D Vec3D::operator/(double number) const {
if (std::abs(number) > Consts::EPS) { if (std::abs(number) > Consts::EPS) {
return Vec3D(*this) * (1.0 / number); return *this * (1.0 / number);
} else { } else {
throw std::domain_error{"Vec3D::operator/(double number): division by zero"}; throw std::domain_error{"Vec3D::operator/(double number): division by zero"};
} }
@ -69,9 +69,9 @@ double Vec3D::abs() const {
} }
Vec3D Vec3D::normalized() const { Vec3D Vec3D::normalized() const {
double vecAbs = abs(); double vecAbs = sqrAbs();
if (vecAbs > Consts::EPS) { if (vecAbs > Consts::EPS) {
return Vec3D(*this) / vecAbs; return *this / sqrt(vecAbs);
} else { } else {
return Vec3D(1); return Vec3D(1);
} }

View File

@ -40,7 +40,7 @@ Vec4D Vec4D::operator+(const Vec4D &point4D) const {
} }
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 { Vec4D Vec4D::operator*(double number) const {
@ -49,7 +49,7 @@ Vec4D Vec4D::operator*(double number) const {
Vec4D Vec4D::operator/(double number) const { Vec4D Vec4D::operator/(double number) const {
if (std::abs(number) > Consts::EPS) { if (std::abs(number) > Consts::EPS) {
return Vec4D(*this) * (1.0 / number); return *this * (1.0 / number);
} else { } else {
throw std::domain_error{"Vec4D::operator/(double number): division by zero"}; throw std::domain_error{"Vec4D::operator/(double number): division by zero"};
} }
@ -65,9 +65,9 @@ double Vec4D::abs() const {
} }
Vec4D Vec4D::normalized() const { Vec4D Vec4D::normalized() const {
double vecAbs = abs(); double vecAbs = sqrAbs();
if (vecAbs > Consts::EPS) { if (vecAbs > Consts::EPS) {
return Vec4D(*this) / vecAbs; return *this / sqrt(vecAbs);
} else { } else {
return Vec4D(1); return Vec4D(1);
} }