From fd155bead0e24cc5018b9f4cee2d73c85b18badc Mon Sep 17 00:00:00 2001 From: cutealien Date: Tue, 23 Jul 2019 15:30:50 +0000 Subject: [PATCH] Add operator[] to vector2d and vector3d git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@5841 dfc29bdd-3216-0410-991c-e03cc46cb475 --- changes.txt | 1 + include/vector2d.h | 14 ++++++++++++++ include/vector3d.h | 14 ++++++++++++++ tests/testVector2d.cpp | 9 +++++++++ tests/testVector3d.cpp | 12 ++++++++++++ 5 files changed, 50 insertions(+) diff --git a/changes.txt b/changes.txt index 65e2afbf..29bfc679 100644 --- a/changes.txt +++ b/changes.txt @@ -1,5 +1,6 @@ -------------------------- Changes in 1.9 (not yet released) +- Add operator[] to vector2d and vector3d - Bugfix: IrrlichtDevice::isWindowMinimized no longer returns true when it's maximized on Windows. - Ignore degenerated faces in obj file loader when they would generate triangles where 2 vertices use identical indices. - Add CMatrix4::transformVec4 to transform vectors with 4 elements (thx @ devsh) diff --git a/include/vector2d.h b/include/vector2d.h index b158a033..98075a8a 100644 --- a/include/vector2d.h +++ b/include/vector2d.h @@ -64,6 +64,20 @@ public: vector2d operator/(const T v) const { return vector2d(X / v, Y / v); } vector2d& operator/=(const T v) { X/=v; Y/=v; return *this; } + T& operator [](u32 index) + { + _IRR_DEBUG_BREAK_IF(index>1) // access violation + + return *(&X+index); + } + + const T& operator [](u32 index) const + { + _IRR_DEBUG_BREAK_IF(index>1) // access violation + + return *(&X+index); + } + //! sort in order X, Y. Equality with rounding tolerance. bool operator<=(const vector2d&other) const { diff --git a/include/vector3d.h b/include/vector3d.h index 31d118bd..5f4f6133 100644 --- a/include/vector3d.h +++ b/include/vector3d.h @@ -57,6 +57,20 @@ namespace core vector3d operator/(const T v) const { T i=(T)1.0/v; return vector3d(X * i, Y * i, Z * i); } vector3d& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; } + T& operator [](u32 index) + { + _IRR_DEBUG_BREAK_IF(index>2) // access violation + + return *(&X+index); + } + + const T& operator [](u32 index) const + { + _IRR_DEBUG_BREAK_IF(index>2) // access violation + + return *(&X+index); + } + //! sort in order X, Y, Z. Equality with rounding tolerance. bool operator<=(const vector3d&other) const { diff --git a/tests/testVector2d.cpp b/tests/testVector2d.cpp index 97e7aef0..b06edc76 100644 --- a/tests/testVector2d.cpp +++ b/tests/testVector2d.cpp @@ -36,6 +36,15 @@ static bool doTests() return false; } + otherVec = vector2d(1,2); + otherVec[0] = vec[0]; + otherVec[1] = vec[1]; + if(!vec.equals(otherVec)) + { + logTestString("vector2d::operator[] failed\n"); + assert_log(0); + return false; + } vec.rotateBy(45); // Test implicit (0, 0) center COMPARE_VECTORS(vec, vector2d(0, (T)7.0710678118654755)); diff --git a/tests/testVector3d.cpp b/tests/testVector3d.cpp index fa22b9c2..a94d0303 100644 --- a/tests/testVector3d.cpp +++ b/tests/testVector3d.cpp @@ -208,6 +208,7 @@ static bool doTests() { vector3d vec(-5, 5, 0); vector3d otherVec((T)-5.1, 5, 0); + if(!vec.equals(otherVec, (T)0.1)) { logTestString("vector3d::equals failed\n"); @@ -215,6 +216,17 @@ static bool doTests() return false; } + otherVec = vector3d(1,2,3); + otherVec[0] = vec[0]; + otherVec[1] = vec[1]; + otherVec[2] = vec[2]; + if(!vec.equals(otherVec)) + { + logTestString("vector3d::operator[] failed\n"); + assert_log(0); + return false; + } + vec.set(5, 5, 0); otherVec.set(10, 20, 0); if(!equals(vec.getDistanceFrom(otherVec), (T)15.8113883))