Add a minor convenience for calculating the distance to a point

Change-Id: I312727bf6858ead6c30fe20bf3abc5afc73a3a14
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
bb10
Laszlo Papp 2013-03-15 04:54:43 +00:00 committed by The Qt Project
parent 379886d24b
commit d8bef6bb2e
3 changed files with 69 additions and 0 deletions

View File

@ -347,6 +347,19 @@ QVector3D QVector3D::normal
return crossProduct((v2 - v1), (v3 - v1)).normalized();
}
/*!
\since 5.1
Returns the distance from this vertex to a point defined by
the vertex \a point.
\sa distanceToPlane(), distanceToLine()
*/
float QVector3D::distanceToPoint(const QVector3D& point) const
{
return (*this - point).length();
}
/*!
Returns the distance from this vertex to a plane defined by
the vertex \a plane and a \a normal unit vector. The \a normal

View File

@ -97,6 +97,7 @@ public:
static QVector3D normal
(const QVector3D& v1, const QVector3D& v2, const QVector3D& v3);
float distanceToPoint(const QVector3D& point) const;
float distanceToPlane(const QVector3D& plane, const QVector3D& normal) const;
float distanceToPlane(const QVector3D& plane1, const QVector3D& plane2, const QVector3D& plane3) const;
float distanceToLine(const QVector3D& point, const QVector3D& direction) const;

View File

@ -128,6 +128,8 @@ private slots:
void crossProduct();
void normal_data();
void normal();
void distanceToPoint_data();
void distanceToPoint();
void distanceToPlane_data();
void distanceToPlane();
void distanceToLine_data();
@ -1788,6 +1790,59 @@ void tst_QVectorND::normal()
QVERIFY(QVector3D::normal(point, v1 + point, v2 + point) == v3.normalized());
}
// Test distance to point calculations.
void tst_QVectorND::distanceToPoint_data()
{
QTest::addColumn<float>("x1"); // Point to test for distance
QTest::addColumn<float>("y1");
QTest::addColumn<float>("z1");
QTest::addColumn<float>("x2"); // Point to test against
QTest::addColumn<float>("y2");
QTest::addColumn<float>("z2");
QTest::addColumn<float>("distance");
QTest::newRow("null")
<< 0.0f << 0.0f << 0.0f
<< 0.0f << 0.0f << 1.0f
<< 1.0f;
QTest::newRow("on point")
<< 0.0f << 0.0f << 0.0f
<< 0.0f << 0.0f << 0.0f
<< 0.0f;
QTest::newRow("off point")
<< 0.0f << 0.0f << 0.0f
<< 0.0f << 0.0f << 1.0f
<< 1.0f;
QTest::newRow("off point 2")
<< 0.0f << 0.0f << 0.0f
<< 0.0f << 2.0f << 0.0f
<< 2.0f;
QTest::newRow("minus point")
<< 0.0f << 0.0f << 0.0f
<< 0.0f << -2.0f << 0.0f
<< 2.0f;
}
void tst_QVectorND::distanceToPoint()
{
QFETCH(float, x1);
QFETCH(float, y1);
QFETCH(float, z1);
QFETCH(float, x2);
QFETCH(float, y2);
QFETCH(float, z2);
QFETCH(float, distance);
QVector3D v1(x1, y1, z1);
QVector3D v2(x2, y2, z2);
QCOMPARE(v1.distanceToPoint(v2), distance);
}
// Test distance to plane calculations.
void tst_QVectorND::distanceToPlane_data()
{