Debian Codegraph @

tests/auto/render/raycasting/tst_raycasting.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/qraycastingservice_p.h>
7 #include <Qt3DRender/private/sphere_p.h>
8 #include <Qt3DRender/private/entity_p.h>
9 #include <Qt3DRender/private/abstractpickingjob_p.h>
10 #include <Qt3DRender/private/qboundingvolumeprovider_p.h>
11 #include <Qt3DRender/private/qray3d_p.h>
12 #include <Qt3DRender/qcamera.h>
13
14 using namespace Qt3DCore;
15 using namespace Qt3DRender;
16 using namespace Qt3DRender::Render;
17 using namespace Qt3DRender::RayCasting;
18
19 class tst_RayCasting : public QObject
20 {
21 Q_OBJECT
22 public:
23 tst_RayCasting() {}
24 ~tst_RayCasting() {}
25
26 private Q_SLOTS:
27 void initTestCase();
28 void shouldReturnValidHandle();
29 void shouldReturnResultForEachHandle();
30 void shouldReturnAllResults();
31 void shouldReturnHits();
32 void shouldReturnHits_data();
33 void shouldIntersect_data();
34 void shouldIntersect();
35 void shouldUseProvidedBoudingVolumes();
36 void mousePicking();
37
38 void cleanupTestCase();
39
40 private:
41 Sphere *volumeAt(int index);
42 QList<Sphere> boundingVolumes;
43 };
44
45 void tst_RayCasting::initTestCase()
46 {
47 #if defined Q_OS_QNX
48 QSKIP("This test times out on QNX (QTBUG-107694)");
49 #endif
50 }
51
52 void tst_RayCasting::shouldIntersect_data()
53 {
54 QTest::addColumn<QRay3D>("ray");
55 QTest::addColumn<Sphere>("sphere");
56 QTest::addColumn<bool>("shouldIntersect");
57
58 QRay3D ray(Vector3D(1, 1, 1), Vector3D(0, 0, 1));
59
60 Sphere sphere1(Vector3D(1, 1, 1), 2);
61 Sphere sphere2(Vector3D(0, 0, 0), 3);
62 Sphere sphere3(Vector3D(0, 1, 3), 1);
63 Sphere sphere4(Vector3D(4, 4, 5), 1);
64 Sphere sphere5(Vector3D(2, 2, 11), 5);
65 Sphere sphere6(Vector3D(2, 2, 13), 1);
66 Sphere sphere7(Vector3D(2, 2, 15), 5);
67
68 QTest::newRow("Ray starts inside sphere") << ray << sphere1 << true;
69 QTest::newRow("Ray starts inside sphere") << ray << sphere2 << true;
70 QTest::newRow("Ray intersects sphere tangentially") << ray << sphere3 << true;
71 QTest::newRow("No intersection") << ray << sphere4 << false;
72 QTest::newRow("Ray intersect sphere") << ray << sphere5 << true;
73 QTest::newRow("No intersection") << ray << sphere6 << false;
74 QTest::newRow("Ray intersect sphere") << ray << sphere7 << true;
75 }
76
77 void tst_RayCasting::shouldIntersect()
78 {
79 QFETCH(QRay3D, ray);
80 QFETCH(Sphere, sphere);
81 QFETCH(bool, shouldIntersect);
82
83 Vector3D intersectionPoint;
84
85 QCOMPARE(sphere.intersects(ray, &intersectionPoint), shouldIntersect);
86 }
87
88 class MyBoudingVolumesProvider : public QBoundingVolumeProvider
89 {
90 public:
91 MyBoudingVolumesProvider(QList<QBoundingVolume *> volumes)
92 : m_volumes(volumes)
93 {}
94
95 QList<QBoundingVolume *> boundingVolumes() const override
96 {
97 return m_volumes;
98 }
99
100 private:
101 QList<QBoundingVolume *> m_volumes;
102 };
103
104 void tst_RayCasting::shouldReturnValidHandle()
105 {
106 // GIVEN
107 QRay3D ray;
108 Sphere v1;
109 MyBoudingVolumesProvider provider = QList<QBoundingVolume *> { &v1 };
110
111 QRayCastingService service;
112
113 // WHEN
114 QQueryHandle handle = service.query(ray,
115 QAbstractCollisionQueryService::AllHits,
116 &provider);
117
118 // THEN
119 QVERIFY(handle >= 0);
120
121 // Wait the query to finish
122 service.fetchResult(handle);
123 }
124
125 void tst_RayCasting::shouldReturnResultForEachHandle()
126 {
127 // GIVEN
128 QRay3D ray;
129 QList<QBoundingVolume *> volumes;
130 MyBoudingVolumesProvider provider(volumes);
131
132 QRayCastingService service;
133
134 QQueryHandle handle1 = service.query(ray,
135 QAbstractCollisionQueryService::AllHits,
136 &provider);
137 QQueryHandle handle2 = service.query(ray,
138 QAbstractCollisionQueryService::FirstHit,
139 &provider);
140
141 // WHEN
142 QCollisionQueryResult result2 = service.fetchResult(handle2);
143 QCollisionQueryResult result1 = service.fetchResult(handle1);
144
145 // THEN
146 QCOMPARE(result1.handle(), handle1);
147 QCOMPARE(result2.handle(), handle2);
148 }
149
150 void tst_RayCasting::shouldReturnAllResults()
151 {
152 // GIVEN
153 QRay3D ray;
154 QList<QBoundingVolume *> volumes;
155 MyBoudingVolumesProvider provider(volumes);
156
157 QRayCastingService service;
158
159 QList<QQueryHandle> handles;
160 handles.append(service.query(ray,
161 QAbstractCollisionQueryService::AllHits,
162 &provider));
163 handles.append(service.query(ray,
164 QAbstractCollisionQueryService::FirstHit,
165 &provider));
166
167 // WHEN
168 const QList<QCollisionQueryResult> results = service.fetchAllResults();
169
170 // THEN
171 bool expectedHandlesFound = true;
172 for (QQueryHandle expected : std::as_const(handles)) {
173 bool found = false;
174 for (QCollisionQueryResult result : results) {
175 if (result.handle() == expected)
176 found = true;
177 }
178
179 expectedHandlesFound &= found;
180 }
181
182 QVERIFY(expectedHandlesFound);
183 }
184
185 void tst_RayCasting::shouldReturnHits_data()
186 {
187 QTest::addColumn<QRay3D>("ray");
188 QTest::addColumn<QList<QBoundingVolume *>>("volumes");
189 QTest::addColumn<std::vector<QNodeId> >("hits");
190 QTest::addColumn<QAbstractCollisionQueryService::QueryMode>("queryMode");
191
192 QRay3D ray(Vector3D(1, 1, 1), Vector3D(0, 0, 1));
193
194 this->boundingVolumes.clear();
195 this->boundingVolumes.append(QList<Sphere>
196 { Sphere(Vector3D(1, 1, 1), 3, QNodeId::createId()),
197 Sphere(Vector3D(0, 0, 0), 3, QNodeId::createId()),
198 Sphere(Vector3D(0, 1, 3), 1, QNodeId::createId()),
199 Sphere(Vector3D(4, 4, 5), 1, QNodeId::createId()),
200 Sphere(Vector3D(2, 2, 11), 5, QNodeId::createId()),
201 Sphere(Vector3D(2, 2, 13), 1, QNodeId::createId()),
202 Sphere(Vector3D(2, 2, 15), 5, QNodeId::createId()) });
203
204 QTest::newRow("All hits, One sphere intersect") << ray
205 << (QList<QBoundingVolume *> { volumeAt(0), volumeAt(3) })
206 << (std::vector<QNodeId>{ volumeAt(0)->id() })
207 << QAbstractCollisionQueryService::AllHits;
208
209 QTest::newRow("All hits, Three sphere intersect") << ray
210 << (QList<QBoundingVolume *> { volumeAt(0), volumeAt(3), volumeAt(6), volumeAt(2) })
211 << (std::vector<QNodeId>{ volumeAt(0)->id(), volumeAt(2)->id(), volumeAt(6)->id() })
212 << QAbstractCollisionQueryService::AllHits;
213
214 QTest::newRow("All hits, No sphere intersect") << ray
215 << (QList<QBoundingVolume *> { volumeAt(3), volumeAt(5) })
216 << (std::vector<QNodeId>{})
217 << QAbstractCollisionQueryService::AllHits;
218
219 QTest::newRow("Sphere 1 intersect, returns First Hit") << ray
220 << (QList<QBoundingVolume *> { volumeAt(0), volumeAt(3), volumeAt(6) })
221 << (std::vector<QNodeId>{ volumeAt(0)->id() })
222 << QAbstractCollisionQueryService::FirstHit;
223
224 QTest::newRow("Sphere 3 and 5 intersects, returns First Hit") << ray
225 << (QList<QBoundingVolume *> { volumeAt(3), volumeAt(6), volumeAt(4) })
226 << (std::vector<QNodeId>{ volumeAt(4)->id() })
227 << QAbstractCollisionQueryService::FirstHit;
228
229 QTest::newRow("Sphere 5 and 3 intersects, unordered list, returns First Hit") << ray
230 << (QList<QBoundingVolume *> { volumeAt(4), volumeAt(3), volumeAt(6) })
231 << (std::vector<QNodeId>{volumeAt(4)->id() })
232 << QAbstractCollisionQueryService::FirstHit;
233
234 QTest::newRow("No sphere intersect, returns First Hit") << ray
235 << (QList<QBoundingVolume *> { volumeAt(3), volumeAt(5) })
236 << (std::vector<QNodeId>{})
237 << QAbstractCollisionQueryService::FirstHit;
238 }
239
240 void tst_RayCasting::shouldReturnHits()
241 {
242 // GIVEN
243 QFETCH(QRay3D, ray);
244 QFETCH(QList<QBoundingVolume *>, volumes);
245 QFETCH(std::vector<QNodeId>, hits);
246 QFETCH(QAbstractCollisionQueryService::QueryMode, queryMode);
247
248 MyBoudingVolumesProvider provider(volumes);
249 QRayCastingService service;
250
251 // WHEN
252 QQueryHandle handle = service.query(ray, queryMode, &provider);
253 QCollisionQueryResult result = service.fetchResult(handle);
254
255 // THEN
256 QCOMPARE(result.entitiesHit().size(), hits.size());
257 QCOMPARE(result.entitiesHit(), hits);
258 }
259
260 void tst_RayCasting::shouldUseProvidedBoudingVolumes()
261 {
262 // GIVEN
263 QRay3D ray(Vector3D(1, 1, 1), Vector3D(0, 0, 1));
264
265 Sphere sphere1(Vector3D(1, 1, 1), 3);
266 Sphere sphere3(Vector3D(0, 1, 3), 1);
267 Sphere sphere4(Vector3D(4, 4, 5), 1);
268
269 MyBoudingVolumesProvider provider(QList<QBoundingVolume *> { &sphere1, &sphere4, &sphere3 });
270 std::vector<QNodeId> hits{ sphere1.id(), sphere3.id()};
271
272 QRayCastingService service;
273
274 // WHEN
275 QQueryHandle handle = service.query(ray,
276 QAbstractCollisionQueryService::AllHits,
277 &provider);
278 QCollisionQueryResult result = service.fetchResult(handle);
279
280 // THEN
281 QCOMPARE(result.entitiesHit().size(), hits.size());
282 QCOMPARE(result.entitiesHit(), hits);
283 }
284
285 void tst_RayCasting::cleanupTestCase()
286 {
287 this->boundingVolumes.clear();
288 }
289
290 Sphere *tst_RayCasting::volumeAt(int index)
291 {
292 return &*(boundingVolumes.begin() + index);
293 }
294
295 void tst_RayCasting::mousePicking()
296 {
297 // GIVEN
298 Qt3DRender::QCamera camera;
299 camera.setProjectionType(QCameraLens::PerspectiveProjection);
300 camera.setFieldOfView(45.0f);
301 camera.setAspectRatio(800.0f/600.0f);
302 camera.setNearPlane(0.1f);
303 camera.setFarPlane(1000.0f);
304 camera.setPosition(QVector3D(0.0f, 0.0f, -40.0f));
305 camera.setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
306 camera.setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
307
308 const QRectF viewport(0., 0., 800., 600.);
309
310 // Window center on near plane
311 QRay3D ray = Qt3DRender::Render::AbstractPickingJob::intersectionRay(viewport.center().toPoint(),
312 Matrix4x4(camera.viewMatrix()),
313 Matrix4x4(camera.projectionMatrix()),
314 viewport.toRect());
315 Qt3DRender::Render::Sphere s(Vector3D(0.0f, 0.5f, 0.0f), 1.0f);
316
317 // WHEN
318 bool intersects = s.intersects(ray, nullptr);
319
320 // THEN
321 QVERIFY(intersects);
322
323 // WHEN
324 ray = Qt3DRender::Render::AbstractPickingJob::intersectionRay(viewport.topLeft().toPoint(),
325 Matrix4x4(camera.viewMatrix()),
326 Matrix4x4(camera.projectionMatrix()),
327 viewport.toRect());
328 intersects = s.intersects(ray, nullptr);
329
330 // THEN
331 QVERIFY(!intersects);
332
333 // WHEN
334 ray = Qt3DRender::Render::AbstractPickingJob::intersectionRay(viewport.topRight().toPoint(),
335 Matrix4x4(camera.viewMatrix()),
336 Matrix4x4(camera.projectionMatrix()),
337 viewport.toRect());
338 intersects = s.intersects(ray, nullptr);
339
340 // THEN
341 QVERIFY(!intersects);
342
343 // WHEN
344 ray = Qt3DRender::Render::AbstractPickingJob::intersectionRay(viewport.bottomLeft().toPoint(),
345 Matrix4x4(camera.viewMatrix()),
346 Matrix4x4(camera.projectionMatrix()),
347 viewport.toRect());
348 intersects = s.intersects(ray, nullptr);
349
350 // THEN
351 QVERIFY(!intersects);
352
353 // WHEN
354 ray = Qt3DRender::Render::AbstractPickingJob::intersectionRay(viewport.bottomRight().toPoint(),
355 Matrix4x4(camera.viewMatrix()),
356 Matrix4x4(camera.projectionMatrix()),
357 viewport.toRect());
358 intersects = s.intersects(ray, nullptr);
359
360 // THEN
361 QVERIFY(!intersects);
362 }
363
364 QTEST_APPLESS_MAIN(tst_RayCasting)
365
366 #include "tst_raycasting.moc"