summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/math3d
diff options
context:
space:
mode:
authorPaul Lemire <paul.lemire@kdab.com>2015-01-19 12:50:05 +0100
committerSean Harmer <sean.harmer@kdab.com>2015-01-22 12:39:20 +0100
commit595ed595eabe01a1cf11c8b846fd777de8233721 (patch)
treec23d42c57c16fe0df5ec5450f237512d8202e85a /tests/auto/gui/math3d
parentfdbf3bec3009343362448141faa42734ec31b1c3 (diff)
Add project/unproject methods in QVector3D
Equivalent of gluProject and gluUnproject. [ChangeLog][QtCore][QVector3D] add convenience project and unproject methods to use like gluProject and gluUnproject Change-Id: I6e4e3e79ea6e34d1fb0c375e15185c950b699ef0 Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'tests/auto/gui/math3d')
-rw-r--r--tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp158
1 files changed, 158 insertions, 0 deletions
diff --git a/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp b/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp
index 2d4b6d16b0..fa5d1b576f 100644
--- a/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp
+++ b/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp
@@ -36,6 +36,7 @@
#include <QtGui/qvector2d.h>
#include <QtGui/qvector3d.h>
#include <QtGui/qvector4d.h>
+#include <QtGui/qmatrix4x4.h>
class tst_QVectorND : public QObject
{
@@ -142,6 +143,11 @@ private slots:
void dotProduct4_data();
void dotProduct4();
+ void project_data();
+ void project();
+ void unproject_data();
+ void unproject();
+
void properties();
void metaTypes();
};
@@ -2291,6 +2297,158 @@ void tst_QVectorND::dotProduct4()
QCOMPARE(QVector4D::dotProduct(v1, v2), d);
}
+void tst_QVectorND::project_data()
+{
+ QTest::addColumn<QVector3D>("point");
+ QTest::addColumn<QRect>("viewport");
+ QTest::addColumn<QMatrix4x4>("projection");
+ QTest::addColumn<QMatrix4x4>("view");
+ QTest::addColumn<QVector2D>("result");
+
+ QMatrix4x4 projection;
+ projection.ortho(-1.0f, 1.0f, -1.0f, 1.0f, 0.1f, 1000.0f);
+
+ QMatrix4x4 view;
+ // Located at (0, 0, 10), looking at origin, y is up
+ view.lookAt(QVector3D(0.0f, 0.0f, 10.0f), QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0f, 1.0f, 0.0f));
+
+ QMatrix4x4 nullMatrix(0.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f);
+
+ QTest::newRow("center")
+ << QVector3D(0.0f, 0.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector2D(400.0f, 300.0f);
+
+ QTest::newRow("topLeft")
+ << QVector3D(-1.0f, 1.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector2D(0.0f, 600.0f);
+
+ QTest::newRow("topRight")
+ << QVector3D(1.0f, 1.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector2D(800.0f, 600.0f);
+
+ QTest::newRow("bottomLeft")
+ << QVector3D(-1.0f, -1.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector2D(0.0f, 0.0f);
+
+ QTest::newRow("bottomRight")
+ << QVector3D(1.0f, -1.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector2D(800.0f, 0.0f);
+
+ QTest::newRow("nullMatrix")
+ << QVector3D(0.0f, 0.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << nullMatrix
+ << nullMatrix
+ << QVector2D(400.0f, 300.0f);
+}
+
+void tst_QVectorND::project()
+{
+ QFETCH(QVector3D, point);
+ QFETCH(QRect, viewport);
+ QFETCH(QMatrix4x4, projection);
+ QFETCH(QMatrix4x4, view);
+ QFETCH(QVector2D, result);
+
+ QVector3D project = point.project(view, projection, viewport);
+
+ QCOMPARE(project.toVector2D(), result);
+}
+
+void tst_QVectorND::unproject_data()
+{
+ QTest::addColumn<QVector3D>("point");
+ QTest::addColumn<QRect>("viewport");
+ QTest::addColumn<QMatrix4x4>("projection");
+ QTest::addColumn<QMatrix4x4>("view");
+ QTest::addColumn<QVector3D>("result");
+
+ QMatrix4x4 projection;
+ projection.ortho(-1.0f, 1.0f, -1.0f, 1.0f, 0.1f, 1000.0f);
+
+ QMatrix4x4 view;
+ // Located at (0, 0, 10), looking at origin, y is up
+ view.lookAt(QVector3D(0.0f, 0.0f, 10.0f), QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0f, 1.0f, 0.0f));
+
+ QMatrix4x4 nullMatrix(0.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f);
+
+ QTest::newRow("center")
+ << QVector3D(400.0f, 300.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector3D(0.0f, 0.0f, 9.9f);
+
+ QTest::newRow("topLeft")
+ << QVector3D(0.0f, 600.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector3D(-1.0f, 1.0f, 9.9f);
+
+ QTest::newRow("topRight")
+ << QVector3D(800.0f, 600.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector3D(1.0f, 1.0f, 9.9f);
+
+ QTest::newRow("bottomLeft")
+ << QVector3D(0.0f, 0.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector3D(-1.0, -1.0f, 9.9f);
+
+ QTest::newRow("bottomRight")
+ << QVector3D(800.0f, 0.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << projection
+ << view
+ << QVector3D(1.0f, -1.0f, 9.9f);
+
+ QTest::newRow("nullMatrix")
+ << QVector3D(400.0f, 300.0f, 0.0f)
+ << QRect(0.0f, 0.0f, 800.0f, 600.0f)
+ << nullMatrix
+ << nullMatrix
+ << QVector3D(0.0f, 0.0f, -1.0f);
+
+}
+
+void tst_QVectorND::unproject()
+{
+ QFETCH(QVector3D, point);
+ QFETCH(QRect, viewport);
+ QFETCH(QMatrix4x4, projection);
+ QFETCH(QMatrix4x4, view);
+ QFETCH(QVector3D, result);
+
+ QVector3D unproject = point.unproject(view, projection, viewport);
+ QVERIFY(qFuzzyCompare(unproject, result));
+}
+
class tst_QVectorNDProperties : public QObject
{
Q_OBJECT