From d8bef6bb2e7523112781ad81e0268fa67b610c3b Mon Sep 17 00:00:00 2001 From: Laszlo Papp Date: Fri, 15 Mar 2013 04:54:43 +0000 Subject: Add a minor convenience for calculating the distance to a point Change-Id: I312727bf6858ead6c30fe20bf3abc5afc73a3a14 Reviewed-by: Sean Harmer --- src/gui/math3d/qvector3d.cpp | 13 ++++++ src/gui/math3d/qvector3d.h | 1 + tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp | 55 +++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/gui/math3d/qvector3d.cpp b/src/gui/math3d/qvector3d.cpp index b3bf292f9b..5537562b61 100644 --- a/src/gui/math3d/qvector3d.cpp +++ b/src/gui/math3d/qvector3d.cpp @@ -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 diff --git a/src/gui/math3d/qvector3d.h b/src/gui/math3d/qvector3d.h index 879b2125ea..49d9ca6bf8 100644 --- a/src/gui/math3d/qvector3d.h +++ b/src/gui/math3d/qvector3d.h @@ -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; diff --git a/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp b/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp index 5cd597aeb8..56f9ad5cb7 100644 --- a/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp +++ b/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp @@ -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("x1"); // Point to test for distance + QTest::addColumn("y1"); + QTest::addColumn("z1"); + QTest::addColumn("x2"); // Point to test against + QTest::addColumn("y2"); + QTest::addColumn("z2"); + + QTest::addColumn("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() { -- cgit v1.2.3