summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2013-08-16 17:57:38 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-08-27 17:50:40 +0200
commita70c0ad1d15e1346f7784f4e720bbe460af85c17 (patch)
treea5c2e1c59a2d2c1977d1cd922f15ccdfcb46696a
parente0f2603f49b17eb4e562ec81e92796e3b2435490 (diff)
Add operator[] to QVectorND classes
Change-Id: Ia786d4fab64da974bb60f24c05325925d42a1e70 Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
-rw-r--r--src/gui/math3d/qvector2d.cpp19
-rw-r--r--src/gui/math3d/qvector2d.h15
-rw-r--r--src/gui/math3d/qvector3d.cpp19
-rw-r--r--src/gui/math3d/qvector3d.h15
-rw-r--r--src/gui/math3d/qvector4d.cpp19
-rw-r--r--src/gui/math3d/qvector4d.h15
-rw-r--r--tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp80
7 files changed, 182 insertions, 0 deletions
diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp
index 784297ca70..7446d27dab 100644
--- a/src/gui/math3d/qvector2d.cpp
+++ b/src/gui/math3d/qvector2d.cpp
@@ -160,6 +160,25 @@ QVector2D::QVector2D(const QVector4D& vector)
\sa y(), setX()
*/
+/*! \fn float &QVector2D::operator[](int i)
+ \since 5.2
+
+ Returns the component of the vector at index position \a i
+ as a modifiable reference.
+
+ \a i must be a valid index position in the vector (i.e., 0 <= \a i
+ < 2).
+*/
+
+/*! \fn float QVector2D::operator[](int i) const
+ \since 5.2
+
+ Returns the component of the vector at index position \a i.
+
+ \a i must be a valid index position in the vector (i.e., 0 <= \a i
+ < 2).
+*/
+
/*!
Returns the length of the vector from the origin.
diff --git a/src/gui/math3d/qvector2d.h b/src/gui/math3d/qvector2d.h
index b3c2272287..55e606ec35 100644
--- a/src/gui/math3d/qvector2d.h
+++ b/src/gui/math3d/qvector2d.h
@@ -76,6 +76,9 @@ public:
void setX(float x);
void setY(float y);
+ float &operator[](int i);
+ float operator[](int i) const;
+
float length() const;
float lengthSquared() const;
@@ -145,6 +148,18 @@ inline float QVector2D::y() const { return yp; }
inline void QVector2D::setX(float aX) { xp = aX; }
inline void QVector2D::setY(float aY) { yp = aY; }
+inline float &QVector2D::operator[](int i)
+{
+ Q_ASSERT(uint(i) < 2u);
+ return *(&xp + i);
+}
+
+inline float QVector2D::operator[](int i) const
+{
+ Q_ASSERT(uint(i) < 2u);
+ return *(&xp + i);
+}
+
inline QVector2D &QVector2D::operator+=(const QVector2D &vector)
{
xp += vector.xp;
diff --git a/src/gui/math3d/qvector3d.cpp b/src/gui/math3d/qvector3d.cpp
index 5537562b61..226483c227 100644
--- a/src/gui/math3d/qvector3d.cpp
+++ b/src/gui/math3d/qvector3d.cpp
@@ -196,6 +196,25 @@ QVector3D::QVector3D(const QVector4D& vector)
\sa z(), setX(), setY()
*/
+/*! \fn float &QVector3D::operator[](int i)
+ \since 5.2
+
+ Returns the component of the vector at index position \a i
+ as a modifiable reference.
+
+ \a i must be a valid index position in the vector (i.e., 0 <= \a i
+ < 3).
+*/
+
+/*! \fn float QVector3D::operator[](int i) const
+ \since 5.2
+
+ Returns the component of the vector at index position \a i.
+
+ \a i must be a valid index position in the vector (i.e., 0 <= \a i
+ < 3).
+*/
+
/*!
Returns the normalized unit vector form of this vector.
diff --git a/src/gui/math3d/qvector3d.h b/src/gui/math3d/qvector3d.h
index 49d9ca6bf8..c880930935 100644
--- a/src/gui/math3d/qvector3d.h
+++ b/src/gui/math3d/qvector3d.h
@@ -79,6 +79,9 @@ public:
void setY(float y);
void setZ(float z);
+ float &operator[](int i);
+ float operator[](int i) const;
+
float length() const;
float lengthSquared() const;
@@ -160,6 +163,18 @@ inline void QVector3D::setX(float aX) { xp = aX; }
inline void QVector3D::setY(float aY) { yp = aY; }
inline void QVector3D::setZ(float aZ) { zp = aZ; }
+inline float &QVector3D::operator[](int i)
+{
+ Q_ASSERT(uint(i) < 3u);
+ return *(&xp + i);
+}
+
+inline float QVector3D::operator[](int i) const
+{
+ Q_ASSERT(uint(i) < 3u);
+ return *(&xp + i);
+}
+
inline QVector3D &QVector3D::operator+=(const QVector3D &vector)
{
xp += vector.xp;
diff --git a/src/gui/math3d/qvector4d.cpp b/src/gui/math3d/qvector4d.cpp
index 7ced99dfeb..2247e95d5e 100644
--- a/src/gui/math3d/qvector4d.cpp
+++ b/src/gui/math3d/qvector4d.cpp
@@ -225,6 +225,25 @@ QVector4D::QVector4D(const QVector3D& vector, float wpos)
\sa w(), setX(), setY(), setZ()
*/
+/*! \fn float &QVector4D::operator[](int i)
+ \since 5.2
+
+ Returns the component of the vector at index position \a i
+ as a modifiable reference.
+
+ \a i must be a valid index position in the vector (i.e., 0 <= \a i
+ < 4).
+*/
+
+/*! \fn float QVector4D::operator[](int i) const
+ \since 5.2
+
+ Returns the component of the vector at index position \a i.
+
+ \a i must be a valid index position in the vector (i.e., 0 <= \a i
+ < 4).
+*/
+
/*!
Returns the length of the vector from the origin.
diff --git a/src/gui/math3d/qvector4d.h b/src/gui/math3d/qvector4d.h
index 4bd3822133..810380b805 100644
--- a/src/gui/math3d/qvector4d.h
+++ b/src/gui/math3d/qvector4d.h
@@ -82,6 +82,9 @@ public:
void setZ(float z);
void setW(float w);
+ float &operator[](int i);
+ float operator[](int i) const;
+
float length() const;
float lengthSquared() const;
@@ -158,6 +161,18 @@ inline void QVector4D::setY(float aY) { yp = aY; }
inline void QVector4D::setZ(float aZ) { zp = aZ; }
inline void QVector4D::setW(float aW) { wp = aW; }
+inline float &QVector4D::operator[](int i)
+{
+ Q_ASSERT(uint(i) < 4u);
+ return *(&xp + i);
+}
+
+inline float QVector4D::operator[](int i) const
+{
+ Q_ASSERT(uint(i) < 4u);
+ return *(&xp + i);
+}
+
inline QVector4D &QVector4D::operator+=(const QVector4D &vector)
{
xp += vector.xp;
diff --git a/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp b/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp
index 0041ad0015..57c4c094a2 100644
--- a/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp
+++ b/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp
@@ -57,6 +57,10 @@ private slots:
void create3();
void create4();
+ void modify2();
+ void modify3();
+ void modify4();
+
void length2_data();
void length2();
void length3_data();
@@ -570,6 +574,82 @@ void tst_QVectorND::create4()
QVERIFY(v14.isNull());
}
+// Test modifying vectors in various ways
+void tst_QVectorND::modify2()
+{
+ const float e = 2.7182818f;
+ const float pi = 3.14159f;
+ const QVector2D p(e, pi);
+
+ QVector2D p1;
+ p1.setX(e);
+ p1.setY(pi);
+ QVERIFY(qFuzzyCompare(p, p1));
+
+ QVector2D p2;
+ p2[0] = e;
+ p2[1] = pi;
+ QVERIFY(qFuzzyCompare(p, p2));
+
+ QVector2D p3;
+ for (int i = 0; i < 2; ++i)
+ p3[i] = p[i];
+ QVERIFY(qFuzzyCompare(p, p3));
+}
+
+void tst_QVectorND::modify3()
+{
+ const float one = 1.0f;
+ const float e = 2.7182818f;
+ const float pi = 3.14159f;
+ const QVector3D p(one, e, pi);
+
+ QVector3D p1;
+ p1.setX(one);
+ p1.setY(e);
+ p1.setZ(pi);
+ QVERIFY(qFuzzyCompare(p, p1));
+
+ QVector3D p2;
+ p2[0] = one;
+ p2[1] = e;
+ p2[2] = pi;
+ QVERIFY(qFuzzyCompare(p, p2));
+
+ QVector3D p3;
+ for (int i = 0; i < 3; ++i)
+ p3[i] = p[i];
+ QVERIFY(qFuzzyCompare(p, p3));
+}
+
+void tst_QVectorND::modify4()
+{
+ const float one = 1.0f;
+ const float e = 2.7182818f;
+ const float pi = 3.14159f;
+ const float big = 1.0e6f;
+ const QVector4D p(one, e, pi, big);
+
+ QVector4D p1;
+ p1.setX(one);
+ p1.setY(e);
+ p1.setZ(pi);
+ p1.setW(big);
+ QVERIFY(qFuzzyCompare(p, p1));
+
+ QVector4D p2;
+ p2[0] = one;
+ p2[1] = e;
+ p2[2] = pi;
+ p2[3] = big;
+ QVERIFY(qFuzzyCompare(p, p2));
+
+ QVector4D p3;
+ for (int i = 0; i < 4; ++i)
+ p3[i] = p[i];
+ QVERIFY(qFuzzyCompare(p, p3));
+}
+
// Test vector length computation for 2D vectors.
void tst_QVectorND::length2_data()
{