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>
This commit is contained in:
parent
e0f2603f49
commit
a70c0ad1d1
@ -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.
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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.
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user