Debian Codegraph @

tests/auto/render/triangleboundingvolume/tst_triangleboundingvolume.cpp

raw symbols SCIP
1 // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/QTest>
5 #include <Qt3DRender/private/entity_p.h>
6 #include <Qt3DRender/private/triangleboundingvolume_p.h>
7 #include <Qt3DRender/private/qraycastingservice_p.h>
8 #include <Qt3DRender/private/qray3d_p.h>
9 #include <Qt3DRender/qcameralens.h>
10 #include <Qt3DRender/qcamera.h>
11 #include <Qt3DRender/private/qboundingvolume_p.h>
12
13 class tst_TriangleBoundingVolume : public QObject
14 {
15 Q_OBJECT
16 public:
17 tst_TriangleBoundingVolume() {}
18 ~tst_TriangleBoundingVolume() {}
19
20 private Q_SLOTS:
21 void checkInitialState()
22 {
23 // GIVEN
24 Qt3DRender::Render::TriangleBoundingVolume volume = Qt3DRender::Render::TriangleBoundingVolume(Qt3DCore::QNodeId(),
25 Vector3D(),
26 Vector3D(),
27 Vector3D());
28
29 // THEN
30 QCOMPARE(volume.id(), Qt3DCore::QNodeId());
31 QCOMPARE(volume.a(), Vector3D());
32 QCOMPARE(volume.b(), Vector3D());
33 QCOMPARE(volume.c(), Vector3D());
34 QCOMPARE(volume.type(), Qt3DRender::RayCasting::QBoundingVolume::Triangle);
35 }
36
37 void transformed_data()
38 {
39 QTest::addColumn<Vector3D>("a");
40 QTest::addColumn<Vector3D>("b");
41 QTest::addColumn<Vector3D>("c");
42
43 QTest::addColumn<Vector3D>("transformedA");
44 QTest::addColumn<Vector3D>("transformedB");
45 QTest::addColumn<Vector3D>("transformedC");
46
47 QTest::newRow("onFarPlane")
48 << Vector3D(-1.0, 1.0, 0.0)
49 << Vector3D(0.0, -1.0, 0.0)
50 << Vector3D(1.0, 1.0, 0.0)
51 << Vector3D(-1.0, 1.0, -40.0)
52 << Vector3D(0.0, -1.0, -40.0)
53 << Vector3D(1.0, 1.0, -40.0);
54
55 QTest::newRow("onNearPlane")
56 << Vector3D(-1.0, 1.0, 40.0)
57 << Vector3D(0.0, -1.0, 40.0)
58 << Vector3D(1.0, 1.0, 40.0)
59 << Vector3D(-1.0, 1.0, 0.0)
60 << Vector3D(0.0, -1.0, 0.0)
61 << Vector3D(1.0, 1.0, 0.0);
62
63
64 }
65
66 void transformed()
67 {
68 // GIVEN
69 QFETCH(Vector3D, a);
70 QFETCH(Vector3D, b);
71 QFETCH(Vector3D, c);
72 QFETCH(Vector3D, transformedA);
73 QFETCH(Vector3D, transformedB);
74 QFETCH(Vector3D, transformedC);
75 Qt3DRender::Render::TriangleBoundingVolume volume(Qt3DCore::QNodeId(),
76 a,
77 b,
78 c);
79 Qt3DRender::QCamera camera;
80 camera.setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection);
81 camera.setFieldOfView(45.0f);
82 camera.setAspectRatio(800.0f/600.0f);
83 camera.setNearPlane(0.1f);
84 camera.setFarPlane(1000.0f);
85 camera.setPosition(QVector3D(0.0f, 0.0f, 40.0f));
86 camera.setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
87 camera.setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
88
89 const Matrix4x4 viewMatrix(camera.viewMatrix());
90
91 // WHEN
92 volume.transform(viewMatrix);
93
94 // THEN
95 QCOMPARE(transformedA, volume.a());
96 QCOMPARE(transformedB, volume.b());
97 QCOMPARE(transformedC, volume.c());
98 }
99
100 void intersects_data()
101 {
102 QTest::addColumn<Qt3DRender::RayCasting::QRay3D>("ray");
103 QTest::addColumn<Vector3D>("a");
104 QTest::addColumn<Vector3D>("b");
105 QTest::addColumn<Vector3D>("c");
106 QTest::addColumn<Vector3D>("uvw");
107 QTest::addColumn<float>("t");
108 QTest::addColumn<bool>("isIntersecting");
109
110 const float farPlaneDistance = 40.0;
111
112 QTest::newRow("halfway_center")
113 << Qt3DRender::RayCasting::QRay3D(Vector3D(), Vector3D(0.0, 0.0, 1.0), farPlaneDistance)
114 << Vector3D(3.0, 1.5, 20.0)
115 << Vector3D(0.0, -1.5, 20.0)
116 << Vector3D(-3, 1.5, 20.0)
117 << Vector3D(0.25, 0.5, 0.25)
118 << 0.5f
119 << true;
120 QTest::newRow("miss_halfway_center_too_short")
121 << Qt3DRender::RayCasting::QRay3D(Vector3D(), Vector3D(0.0, 0.0, 1.0), farPlaneDistance * 0.25f)
122 << Vector3D(3.0, 1.5, 20.0)
123 << Vector3D(0.0, -1.5, 20.0)
124 << Vector3D(-3, 1.5, 20.0)
125 << Vector3D()
126 << 0.0f
127 << false;
128 QTest::newRow("far_center")
129 << Qt3DRender::RayCasting::QRay3D(Vector3D(), Vector3D(0.0, 0.0, 1.0), farPlaneDistance)
130 << Vector3D(3.0, 1.5, 40.0)
131 << Vector3D(0.0, -1.5, 40.0)
132 << Vector3D(-3, 1.5, 40.0)
133 << Vector3D(0.25, 0.5, 0.25)
134 << 1.0f
135 << true;
136 QTest::newRow("near_center")
137 << Qt3DRender::RayCasting::QRay3D(Vector3D(), Vector3D(0.0, 0.0, 1.0), 1.0f)
138 << Vector3D(3.0, 1.5, 0.0)
139 << Vector3D(0.0, -1.5, 0.0)
140 << Vector3D(-3, 1.5, 0.0)
141 << Vector3D(0.25, 0.5, 0.25)
142 << 0.0f
143 << true;
144 QTest::newRow("above_miss_center")
145 << Qt3DRender::RayCasting::QRay3D(Vector3D(0.0, 2.0, 0.0), Vector3D(0.0, 2.0, 1.0), 1.0f)
146 << Vector3D(3.0, 1.5, 0.0)
147 << Vector3D(0.0, -1.5, 0.0)
148 << Vector3D(-3, 1.5, 0.0)
149 << Vector3D()
150 << 0.0f
151 << false;
152 QTest::newRow("below_miss_center")
153 << Qt3DRender::RayCasting::QRay3D(Vector3D(0.0, -2.0, 0.0), Vector3D(0.0, -2.0, 1.0), 1.0f)
154 << Vector3D(3.0, 1.5, 0.0)
155 << Vector3D(0.0, -1.5, 0.0)
156 << Vector3D(-3, 1.5, 0.0)
157 << Vector3D()
158 << 0.0f
159 << false;
160 }
161
162 void intersects()
163 {
164 // GIVEN
165 QFETCH(Qt3DRender::RayCasting::QRay3D, ray);
166 QFETCH(Vector3D, a);
167 QFETCH(Vector3D, b);
168 QFETCH(Vector3D, c);
169 QFETCH(Vector3D, uvw);
170 QFETCH(float, t);
171 QFETCH(bool, isIntersecting);
172
173 // WHEN
174 Vector3D tmp_uvw;
175 float tmp_t;
176 const bool shouldBeIntersecting = Qt3DRender::Render::intersectsSegmentTriangle(ray,
177 a, b, c,
178 tmp_uvw,
179 tmp_t);
180
181 // THEN
182 QCOMPARE(shouldBeIntersecting, isIntersecting);
183 if (isIntersecting) {
184 QVERIFY(qFuzzyCompare(uvw, tmp_uvw));
185 QVERIFY(qFuzzyCompare(t, tmp_t));
186 }
187 }
188 };
189
190 QTEST_APPLESS_MAIN(tst_TriangleBoundingVolume)
191
192 #include "tst_triangleboundingvolume.moc"