diff --git a/src/lib/utility/math/Vector2.h b/src/lib/utility/math/Vector2.h index f04b3582..852dbcb6 100644 --- a/src/lib/utility/math/Vector2.h +++ b/src/lib/utility/math/Vector2.h @@ -4,7 +4,7 @@ #include "utility/logging/logging.h" #include "utility/Property.h" -#include "VectorBase.h" +#include "utility/math/VectorBase.h" template class Vector2 : public VectorBase @@ -37,18 +37,18 @@ protected: template Vector2::Vector2() : VectorBase() - , x(&m_values[m_xIndex]) - , y(&m_values[m_yIndex]) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) { - setValue(m_xIndex, 0); - setValue(m_yIndex, 0); + setValue(m_xIndex, 0); + setValue(m_yIndex, 0); } template Vector2::Vector2(const T& x, const T& y) : VectorBase() - , x(&m_values[m_xIndex]) - , y(&m_values[m_yIndex]) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) { setValue(m_xIndex, x); setValue(m_yIndex, y); @@ -57,8 +57,8 @@ Vector2::Vector2(const T& x, const T& y) template Vector2::Vector2(const VectorBase& vector) : VectorBase(vector) - , x(&m_values[m_xIndex]) - , y(&m_values[m_yIndex]) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) { } @@ -74,7 +74,7 @@ T Vector2::getValue(const unsigned int index) const { return VectorBase::getValue(index); } - catch(std::exception& e) + catch (std::exception& e) { LOG_ERROR(e.what()); return 0; @@ -88,7 +88,7 @@ void Vector2::setValue(const unsigned int index, const T& value) { VectorBase::setValue(index, value); } - catch(std::exception& e) + catch (std::exception& e) { LOG_ERROR(e.what()); } @@ -99,9 +99,9 @@ T& Vector2::operator[](const unsigned int index) { try { - return VectorBase::[index]; + return VectorBase::getValue(index); } - catch(std::exception& e) + catch (std::exception& e) { LOG_ERROR(e.what()); return 0; diff --git a/src/lib/utility/math/Vector4.h b/src/lib/utility/math/Vector4.h index b81e0dc1..eb86e154 100644 --- a/src/lib/utility/math/Vector4.h +++ b/src/lib/utility/math/Vector4.h @@ -4,7 +4,7 @@ #include "utility/logging/logging.h" #include "utility/Property.h" -#include "VectorBase.h" +#include "utility/math/VectorBase.h" template class Vector4 : public VectorBase @@ -12,7 +12,7 @@ class Vector4 : public VectorBase public: Vector4(); Vector4(const T& x, const T& y, const T& z, const T& w); - Vector4(const VectorBase& vector); + Vector4(const VectorBase& vector); virtual ~Vector4(); T getValue(const unsigned int index) const; @@ -41,10 +41,10 @@ protected: template Vector4::Vector4() : VectorBase() - , x(&m_values[m_xIndex]) - , y(&m_values[m_yIndex]) - , z(&m_values[m_zIndex]) - , w(&m_values[m_wIndex]) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) + , z(&VectorBase::m_values[m_zIndex]) + , w(&VectorBase::m_values[m_wIndex]) { setValue(m_xIndex, 0); setValue(m_yIndex, 0); @@ -55,10 +55,10 @@ Vector4::Vector4() template Vector4::Vector4(const T& x, const T& y, const T& z, const T& w) : VectorBase() - , x(&m_values[m_xIndex]) - , y(&m_values[m_yIndex]) - , z(&m_values[m_zIndex]) - , w(&m_values[m_wIndex]) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) + , z(&VectorBase::m_values[m_zIndex]) + , w(&VectorBase::m_values[m_wIndex]) { setValue(m_xIndex, x); setValue(m_yIndex, y); @@ -69,10 +69,10 @@ Vector4::Vector4(const T& x, const T& y, const T& z, const T& w) template Vector4::Vector4(const VectorBase& vector) : VectorBase(vector) - , x(&m_values[m_xIndex]) - , y(&m_values[m_yIndex]) - , z(&m_values[m_zIndex]) - , w(&m_values[m_wIndex]) + , x(&VectorBase::m_values[m_xIndex]) + , y(&VectorBase::m_values[m_yIndex]) + , z(&VectorBase::m_values[m_zIndex]) + , w(&VectorBase::m_values[m_wIndex]) { } @@ -88,7 +88,7 @@ T Vector4::getValue(const unsigned int index) const { return VectorBase::getValue(index); } - catch(std::exception& e) + catch (std::exception& e) { LOG_ERROR(e.what()); return 0; @@ -102,7 +102,7 @@ void Vector4::setValue(const unsigned int index, const T& value) { VectorBase::setValue(index, value); } - catch(std::exception& e) + catch (std::exception& e) { LOG_ERROR(e.what()); } @@ -113,9 +113,9 @@ T& Vector4::operator[](const unsigned int index) { try { - return VectorBase::[index]; + return VectorBase::getValue(index); } - catch(std::exception& e) + catch (std::exception& e) { LOG_ERROR(e.what()); return 0; diff --git a/src/lib/utility/math/VectorBase.h b/src/lib/utility/math/VectorBase.h index ad568640..ac693460 100644 --- a/src/lib/utility/math/VectorBase.h +++ b/src/lib/utility/math/VectorBase.h @@ -2,8 +2,8 @@ #define VECTOR_BASE_H #include -#include #include +#include #include #define VECTOR_CHECK_INDEX(idx) \ @@ -12,7 +12,7 @@ unsigned int i((idx)); \ checkIndexInRange(i, __FUNCTION__); \ } \ - while(0) \ + while (0) \ template @@ -47,10 +47,10 @@ public: template T dotProduct(const VectorBase& other); - // checks whether all values are the same + // Checks whether all values are the same. template bool isEqual(const VectorBase& other) const; - // checks whether it really is the same object (at one and the same memory address) + // Checks whether it really is the same object (at one and the same memory address). template bool isSame(const VectorBase& other) const; @@ -79,10 +79,10 @@ public: template VectorBase operator/=(const U& scalar); - // checks whether all values are the same + // Checks whether all values are the same. template bool operator==(const VectorBase& other) const; - // checks whether at least one value is different + // Checks whether at least one value is different. template bool operator!=(const VectorBase& other) const; @@ -94,16 +94,17 @@ protected: private: inline void checkIndexInRange(unsigned int index, const std::string& function) const { - if(index >= m_dimensions) + if (index >= m_dimensions) { std::stringstream message; - message << function << ": index " << index << " is out of range, maximum is " << m_dimensions-1; - throw(std::exception(message.str().c_str())); + message << function << ": index " << index << " is out of range, maximum is " << m_dimensions - 1; + throw std::range_error(message.str()); } } + inline void setValues(const T values[N]) { - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { m_values[i] = values[i]; } @@ -114,20 +115,20 @@ private: template -VectorBase::VectorBase(): - m_dimensions(N) +VectorBase::VectorBase() + : m_dimensions(N) {} template -VectorBase::VectorBase(const T values[N]): - m_dimensions(N) +VectorBase::VectorBase(const T values[N]) + : m_dimensions(N) { setValues(values); } template -VectorBase::VectorBase(const VectorBase& vector): - m_dimensions(vector.m_dimensions) +VectorBase::VectorBase(const VectorBase& vector) + : m_dimensions(vector.m_dimensions) { setValues(vector.m_values); } @@ -161,17 +162,17 @@ unsigned int VectorBase::getDimensions() const template float VectorBase::getLengthSquared() const +{ + float result = 0.0f; + + for (unsigned int i = 0; i < m_dimensions; i++) { - float result = 0.0f; - - for(unsigned int i = 0; i < m_dimensions; i++) - { - result += float(m_values[i] * m_values[i]); - } - - return result; + result += float(m_values[i] * m_values[i]); } + return result; +} + template float VectorBase::getLength() const { @@ -186,12 +187,12 @@ VectorBase VectorBase::normalize() if (length > 0.0f) { T tmpValues[N]; - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { tmpValues[i] = m_values[i] / length; } - //the values of *this won't be changed until all are in a valid state + // The values of *this won't be changed until all are in a valid state. setValues(tmpValues); } @@ -207,14 +208,14 @@ VectorBase VectorBase::normalized() const if (length > 0.0f) { - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { tmpValues[i] = m_values[i] / length; } } else { - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { tmpValues[i] = 0; } @@ -227,12 +228,12 @@ template template void VectorBase::assign(const VectorBase& other) { - if(isSame(other)) + if (isSame(other)) { return; } - if(isEqual(other)) + if (isEqual(other)) { return; } @@ -245,12 +246,12 @@ template VectorBase VectorBase::add(const VectorBase& other) { T tmpValues[N]; - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { tmpValues[i] = m_values[i] + other.m_values[i]; } - //the values of *this won't be changed until all are in a valid state + // The values of *this won't be changed until all are in a valid state. setValues(tmpValues); return *this; } @@ -260,12 +261,12 @@ template VectorBase VectorBase::subtract(const VectorBase& other) { T tmpValues[N]; - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { tmpValues[i] = m_values[i] - other.m_values[i]; } - //the values of *this won't be changed until all are in a valid state + // The values of *this won't be changed until all are in a valid state. setValues(tmpValues); return *this; } @@ -275,12 +276,12 @@ template VectorBase VectorBase::scalarMultiplication(const U& scalar) { T tmpValues[N]; - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { tmpValues[i] = m_values[i] * scalar; } - //the values of *this won't be changed until all are in a valid state + // The values of *this won't be changed until all are in a valid state. setValues(tmpValues); return *this; } @@ -290,9 +291,9 @@ template T VectorBase::dotProduct(const VectorBase& other) { T result = 0.0f; - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { - result += (m_values[i]*other.m_values[i]); + result += (m_values[i] * other.m_values[i]); } return result; } @@ -301,14 +302,14 @@ template template bool VectorBase::isEqual(const VectorBase& other) const { - if(m_dimensions != other.m_dimensions) + if (m_dimensions != other.m_dimensions) { return false; } - for(unsigned int i = 0; i < m_dimensions; i++) + for (unsigned int i = 0; i < m_dimensions; i++) { - if(m_values[i] != other.m_values[i]) + if (m_values[i] != other.m_values[i]) { return false; } @@ -375,7 +376,7 @@ template VectorBase VectorBase::operator/(const U& scalar) const { VectorBase result(*this); - return result.scalarMultiplication(1.0f/scalar); + return result.scalarMultiplication(1.0f / scalar); } template @@ -403,7 +404,7 @@ template template VectorBase VectorBase::operator/=(const U& scalar) { - return scalarMultiplication(1.0f/scalar); + return scalarMultiplication(1.0f / scalar); } template @@ -426,11 +427,11 @@ std::string VectorBase::toString() const std::stringstream result; result << "["; - for(unsigned int i = 0; i < m_dimensions-1; i++) + for (unsigned int i = 0; i < m_dimensions - 1; i++) { result << m_values[i] << ", "; } - result << m_values[m_dimensions-1] << "]"; + result << m_values[m_dimensions - 1] << "]"; return result.str(); } @@ -438,12 +439,6 @@ std::string VectorBase::toString() const template std::ostream& operator<<(std::ostream& ostream, const VectorBase& vector) { - /*for(unsigned int i = 0; i < vector.getDimensions()-1; i++) - { - ostream << vector.getValue(i) << ", "; - } - ostream << vector.getValue(vector.getDimensions()-1);*/ - ostream << vector.toString(); return ostream; diff --git a/src/test/Vector2TestSuite.h b/src/test/Vector2TestSuite.h index c16ed121..4420c0a8 100644 --- a/src/test/Vector2TestSuite.h +++ b/src/test/Vector2TestSuite.h @@ -1,8 +1,7 @@ #include "cxxtest/TestSuite.h" -#include "utility/math/Vector2.h" - #include "utility/logging/logging.h" +#include "utility/math/Vector2.h" class Vector2TestSuite : public CxxTest::TestSuite {