From 5b6b627a437a5cd299dcfde548928da96acbff79 Mon Sep 17 00:00:00 2001 From: Godzil Date: Fri, 13 Mar 2020 01:04:56 +0000 Subject: [PATCH] Small but effective optimisations. Allow inlining the function helps a bit! --- source/include/matrix.h | 9 ++++++++- source/include/tuple.h | 14 ++++++++++++-- source/matrix.cpp | 5 +++-- source/tuple.cpp | 4 ++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/source/include/matrix.h b/source/include/matrix.h index 24bc5a0..55d8b67 100644 --- a/source/include/matrix.h +++ b/source/include/matrix.h @@ -19,6 +19,8 @@ #undef minor #endif +#define FastGet4(_x, _y) (this->data[4 * (_x) + (_y)]) + class Matrix { protected: @@ -46,7 +48,12 @@ public: bool isInvertible() { return this->determinant() != 0; } Matrix operator*(const Matrix &b) const; - Tuple operator*(const Tuple &b) const; + Tuple operator*(const Tuple &b) const { + return Tuple(b.x * FastGet4(0, 0) + b.y * FastGet4(0, 1) + b.z * FastGet4(0, 2) + b.w * FastGet4(0, 3), + b.x * FastGet4(1, 0) + b.y * FastGet4(1, 1) + b.z * FastGet4(1, 2) + b.w * FastGet4(1, 3), + b.x * FastGet4(2, 0) + b.y * FastGet4(2, 1) + b.z * FastGet4(2, 2) + b.w * FastGet4(2, 3), + b.x * FastGet4(3, 0) + b.y * FastGet4(3, 1) + b.z * FastGet4(3, 2) + b.w * FastGet4(3, 3)); + } }; class Matrix4: public Matrix diff --git a/source/include/tuple.h b/source/include/tuple.h index 0b5dbeb..2e7b479 100644 --- a/source/include/tuple.h +++ b/source/include/tuple.h @@ -44,8 +44,18 @@ public: void set(double nX, double nY, double nZ) { this->x = nX; this->y = nY; this->z = nZ; }; double magnitude(); Tuple normalise(); - double dot(const Tuple &b); - Tuple cross(const Tuple &b) const; + + double dot(const Tuple &b) { + return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w; + } + + Tuple cross(const Tuple &b) const { + return Tuple(this->y * b.z - this->z * b.y, + this->z * b.x - this->x * b.z, + this->x * b.y - this->y * b.x, + 0); + } + Tuple reflect(const Tuple &normal); }; diff --git a/source/matrix.cpp b/source/matrix.cpp index ee25549..0a179bf 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -101,9 +101,10 @@ Matrix Matrix::operator*(const Matrix &b) const return ret; } -#define FastGet4(_x, _y) (this->data[4 * (_x) + (_y)]) +//#define FastGet4(_x, _y) (this->data[4 * (_x) + (_y)]) /* TODO: Check if we can optimise this function. It is called a lot */ +/* Tuple Matrix::operator*(const Tuple &b) const { return Tuple(b.x * FastGet4(0, 0) + b.y * FastGet4(0, 1) + b.z * FastGet4(0, 2) + b.w * FastGet4(0, 3), @@ -111,7 +112,7 @@ Tuple Matrix::operator*(const Tuple &b) const b.x * FastGet4(2, 0) + b.y * FastGet4(2, 1) + b.z * FastGet4(2, 2) + b.w * FastGet4(2, 3), b.x * FastGet4(3, 0) + b.y * FastGet4(3, 1) + b.z * FastGet4(3, 2) + b.w * FastGet4(3, 3)); } - +*/ Matrix Matrix::identity() { int i; diff --git a/source/tuple.cpp b/source/tuple.cpp index c9e24b1..a3d1b01 100644 --- a/source/tuple.cpp +++ b/source/tuple.cpp @@ -26,7 +26,7 @@ Tuple Tuple::normalise() return Tuple(this->x / mag, this->y / mag, this->z / mag, this->w / mag); } - +/* double Tuple::dot(const Tuple &b) { return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w; @@ -39,7 +39,7 @@ Tuple Tuple::cross(const Tuple &b) const this->x * b.y - this->y * b.x, 0); } - +*/ Tuple Tuple::reflect(const Tuple &normal) { return *this - normal * 2 * this->dot(normal);