| 1 | // Copyright (C) 2016 Paul Lemire |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DCore/qentity.h> |
| 6 | #include <Qt3DCore/qtransform.h> |
| 7 | #include <Qt3DCore/qgeometry.h> |
| 8 | #include <Qt3DRender/qgeometryrenderer.h> |
| 9 | #include <Qt3DCore/qattribute.h> |
| 10 | #include <Qt3DCore/qbuffer.h> |
| 11 | #include <Qt3DRender/private/nodemanagers_p.h> |
| 12 | #include <Qt3DRender/private/managers_p.h> |
| 13 | #include <Qt3DRender/private/entity_p.h> |
| 14 | #include <Qt3DRender/private/filterproximitydistancejob_p.h> |
| 15 | #include <Qt3DRender/private/updatetreeenabledjob_p.h> |
| 16 | #include <Qt3DRender/private/updateworldtransformjob_p.h> |
| 17 | #include <Qt3DRender/private/updateworldboundingvolumejob_p.h> |
| 18 | #include <Qt3DRender/private/calcboundingvolumejob_p.h> |
| 19 | #include <Qt3DRender/private/expandboundingvolumejob_p.h> |
| 20 | #include <Qt3DRender/qproximityfilter.h> |
| 21 | |
| 22 | #include "testaspect.h" |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | Qt3DCore::QEntity *buildEntityAtDistance(float distance, Qt3DCore::QEntity *parent) |
| 27 | { |
| 28 | Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(parent); |
| 29 | |
| 30 | // create geometry with a valid bounding volume - a single point is sufficient |
| 31 | auto geometry = new Qt3DCore::QGeometry; |
| 32 | auto vertexBuffer = new Qt3DCore::QBuffer(geometry); |
| 33 | |
| 34 | auto positionAttribute = new Qt3DCore::QAttribute; |
| 35 | positionAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); |
| 36 | positionAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 37 | positionAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 38 | positionAttribute->setVertexSize(3); |
| 39 | positionAttribute->setByteStride(3 * sizeof(float)); |
| 40 | positionAttribute->setBuffer(vertexBuffer); |
| 41 | |
| 42 | QByteArray vertexBufferData; |
| 43 | vertexBufferData.resize(static_cast<int>(3 * sizeof(float))); |
| 44 | |
| 45 | auto vertexArray = reinterpret_cast<float*>(vertexBufferData.data()); |
| 46 | |
| 47 | int i = 0; |
| 48 | vertexArray[i++] = 0.0f; |
| 49 | vertexArray[i++] = 0.0f; |
| 50 | vertexArray[i++] = 0.0f; |
| 51 | |
| 52 | vertexBuffer->setData(vertexBufferData); |
| 53 | positionAttribute->setCount(1); |
| 54 | |
| 55 | geometry->addAttribute(positionAttribute); |
| 56 | |
| 57 | auto geometryRenderer = new Qt3DRender::QGeometryRenderer; |
| 58 | geometryRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Points); |
| 59 | geometryRenderer->setGeometry(geometry); |
| 60 | |
| 61 | entity->addComponent(geometryRenderer); |
| 62 | |
| 63 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform(parent); |
| 64 | const QVector3D t = QVector3D(1.0f, 0.0f, 0.0f) * distance; |
| 65 | |
| 66 | transform->setTranslation(t); |
| 67 | entity->addComponent(transform); |
| 68 | |
| 69 | return entity; |
| 70 | } |
| 71 | |
| 72 | } // anonymous |
| 73 | |
| 74 | class tst_ProximityFiltering : public QObject |
| 75 | { |
| 76 | Q_OBJECT |
| 77 | private Q_SLOTS: |
| 78 | |
| 79 | void checkInitialState() |
| 80 | { |
| 81 | // GIVEN |
| 82 | Qt3DRender::Render::FilterProximityDistanceJob filterJob; |
| 83 | |
| 84 | // THEN |
| 85 | QCOMPARE(filterJob.hasProximityFilter(), false); |
| 86 | QCOMPARE(filterJob.filteredEntities().size(), 0U); |
| 87 | QCOMPARE(filterJob.proximityFilterIds().size(), 0U); |
| 88 | QVERIFY(filterJob.manager() == nullptr); |
| 89 | } |
| 90 | |
| 91 | void filterEntities_data() |
| 92 | { |
| 93 | QTest::addColumn<Qt3DCore::QEntity *>("entitySubtree"); |
| 94 | QTest::addColumn<Qt3DCore::QNodeIdVector>("proximityFilterIds"); |
| 95 | QTest::addColumn<Qt3DCore::QNodeIdVector>("expectedSelectedEntities"); |
| 96 | |
| 97 | { |
| 98 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 99 | Qt3DCore::QEntity *targetEntity = new Qt3DCore::QEntity(rootEntity); |
| 100 | Qt3DCore::QEntity *childEntity1 = buildEntityAtDistance(50.0f, rootEntity); |
| 101 | Qt3DCore::QEntity *childEntity2 = buildEntityAtDistance(25.0f, rootEntity); |
| 102 | Qt3DCore::QEntity *childEntity3 = buildEntityAtDistance(75.0f, rootEntity); |
| 103 | |
| 104 | Qt3DRender::QProximityFilter *proximityFilter = new Qt3DRender::QProximityFilter(rootEntity); |
| 105 | proximityFilter->setDistanceThreshold(200.0f); |
| 106 | proximityFilter->setEntity(targetEntity); |
| 107 | |
| 108 | QTest::newRow("ShouldSelectAll") << rootEntity |
| 109 | << (Qt3DCore::QNodeIdVector() << proximityFilter->id()) |
| 110 | << (Qt3DCore::QNodeIdVector() |
| 111 | << rootEntity->id() |
| 112 | << targetEntity->id() |
| 113 | << childEntity1->id() |
| 114 | << childEntity2->id() |
| 115 | << childEntity3->id() |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | { |
| 120 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 121 | Qt3DCore::QEntity *childEntity1 = new Qt3DCore::QEntity(rootEntity); |
| 122 | Qt3DCore::QEntity *childEntity2 = new Qt3DCore::QEntity(rootEntity); |
| 123 | Qt3DCore::QEntity *childEntity3 = new Qt3DCore::QEntity(rootEntity); |
| 124 | Q_UNUSED(childEntity1); |
| 125 | Q_UNUSED(childEntity2); |
| 126 | Q_UNUSED(childEntity3); |
| 127 | |
| 128 | Qt3DRender::QProximityFilter *proximityFilter = new Qt3DRender::QProximityFilter(rootEntity); |
| 129 | |
| 130 | QTest::newRow("ShouldSelectNone") << rootEntity |
| 131 | << (Qt3DCore::QNodeIdVector() << proximityFilter->id()) |
| 132 | << (Qt3DCore::QNodeIdVector()); |
| 133 | } |
| 134 | |
| 135 | { |
| 136 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 137 | Qt3DCore::QEntity *targetEntity = new Qt3DCore::QEntity(rootEntity); |
| 138 | Qt3DCore::QEntity *childEntity1 = buildEntityAtDistance(50.0f, rootEntity); |
| 139 | Qt3DCore::QEntity *childEntity2 = buildEntityAtDistance(25.0f, rootEntity); |
| 140 | Qt3DCore::QEntity *childEntity3 = buildEntityAtDistance(75.0f, rootEntity); |
| 141 | Qt3DRender::QProximityFilter *proximityFilter = new Qt3DRender::QProximityFilter(rootEntity); |
| 142 | proximityFilter->setDistanceThreshold(30.0f); |
| 143 | |
| 144 | // Note: rootEntity BoundingSphere will be centered in vec3(75.0f / 2.0, 0.0f 0.0f); |
| 145 | |
| 146 | // Note: we cannot set rootEntity here as that would mean |
| 147 | // that the parent of the root would then be the proximity filter |
| 148 | // (since rootEntity has no parent) but this isn't valid in the way |
| 149 | // we have build the test |
| 150 | // Also rootEntity is centered based on the size of the child it contains |
| 151 | proximityFilter->setEntity(targetEntity); |
| 152 | |
| 153 | Q_UNUSED(childEntity1); |
| 154 | Q_UNUSED(childEntity3); |
| 155 | |
| 156 | QTest::newRow("ShouldSelectChild2") << rootEntity |
| 157 | << (Qt3DCore::QNodeIdVector() << proximityFilter->id()) |
| 158 | << (Qt3DCore::QNodeIdVector() |
| 159 | << targetEntity->id() |
| 160 | << childEntity2->id() |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | { |
| 165 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 166 | Qt3DCore::QEntity *targetEntity = new Qt3DCore::QEntity(rootEntity); |
| 167 | Qt3DCore::QEntity *childEntity1 = buildEntityAtDistance(50.0f, rootEntity); |
| 168 | Qt3DCore::QEntity *childEntity2 = buildEntityAtDistance(25.0f, rootEntity); |
| 169 | Qt3DCore::QEntity *childEntity3 = buildEntityAtDistance(49.9f, rootEntity); |
| 170 | Qt3DRender::QProximityFilter *proximityFilter = new Qt3DRender::QProximityFilter(rootEntity); |
| 171 | proximityFilter->setDistanceThreshold(50.0f); |
| 172 | |
| 173 | // Note: rootEntity BoundingSphere will be centered in vec3(50.0f / 2.0, 0.0f 0.0f); |
| 174 | |
| 175 | // Note: we cannot set rootEntity here as that would mean |
| 176 | // that the parent of the root would then be the proximity filter |
| 177 | // (since rootEntity has no parent) but this isn't valid in the way |
| 178 | // we have build the test |
| 179 | // Also rootEntity is centered based on the size of the child it contains |
| 180 | proximityFilter->setEntity(targetEntity); |
| 181 | |
| 182 | QTest::newRow("ShouldSelectRootChild123") << rootEntity |
| 183 | << (Qt3DCore::QNodeIdVector() << proximityFilter->id()) |
| 184 | << (Qt3DCore::QNodeIdVector() |
| 185 | << rootEntity->id() |
| 186 | << targetEntity->id() |
| 187 | << childEntity1->id() |
| 188 | << childEntity2->id() |
| 189 | << childEntity3->id() |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | { |
| 194 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 195 | Qt3DCore::QEntity *targetEntity = new Qt3DCore::QEntity(rootEntity); |
| 196 | Qt3DCore::QEntity *childEntity1 = buildEntityAtDistance(51.0f, rootEntity); |
| 197 | Qt3DCore::QEntity *childEntity2 = buildEntityAtDistance(75.0f, rootEntity); |
| 198 | Qt3DCore::QEntity *childEntity3 = buildEntityAtDistance(883.0f, rootEntity); |
| 199 | Qt3DRender::QProximityFilter *proximityFilter = new Qt3DRender::QProximityFilter(rootEntity); |
| 200 | proximityFilter->setDistanceThreshold(50.0f); |
| 201 | |
| 202 | // Note: rootEntity BoundingSphere will be centered in vec3(883.0f / 2.0, 0.0f 0.0f); |
| 203 | |
| 204 | // Note: we cannot set rootEntity here as that would mean |
| 205 | // that the parent of the root would then be the proximity filter |
| 206 | // (since rootEntity has no parent) but this isn't valid in the way |
| 207 | // we have build the test |
| 208 | // Also rootEntity is centered based on the size of the child it contains |
| 209 | proximityFilter->setEntity(targetEntity); |
| 210 | |
| 211 | Q_UNUSED(childEntity1); |
| 212 | Q_UNUSED(childEntity2); |
| 213 | Q_UNUSED(childEntity3); |
| 214 | |
| 215 | QTest::newRow("ShouldSelectNoneButTarget") << rootEntity |
| 216 | << (Qt3DCore::QNodeIdVector() << proximityFilter->id()) |
| 217 | << (Qt3DCore::QNodeIdVector() << targetEntity->id()); |
| 218 | } |
| 219 | |
| 220 | { |
| 221 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
| 222 | Qt3DCore::QEntity *targetEntity = new Qt3DCore::QEntity(rootEntity); |
| 223 | Qt3DCore::QEntity *childEntity1 = buildEntityAtDistance(50.0f, rootEntity); |
| 224 | Qt3DCore::QEntity *childEntity2 = buildEntityAtDistance(150.0f, rootEntity); |
| 225 | Qt3DCore::QEntity *childEntity3 = buildEntityAtDistance(25.0f, rootEntity); |
| 226 | |
| 227 | Qt3DRender::QProximityFilter *proximityFilter = new Qt3DRender::QProximityFilter(rootEntity); |
| 228 | proximityFilter->setDistanceThreshold(50.0f); |
| 229 | |
| 230 | Qt3DRender::QProximityFilter *proximityFilter2 = new Qt3DRender::QProximityFilter(rootEntity); |
| 231 | proximityFilter2->setDistanceThreshold(30.0f); |
| 232 | |
| 233 | // Note: rootEntity BoundingSphere will be centered in vec3(150.0f / 2.0, 0.0f 0.0f); |
| 234 | |
| 235 | // Note: we cannot set rootEntity here as that would mean |
| 236 | // that the parent of the root would then be the proximity filter |
| 237 | // (since rootEntity has no parent) but this isn't valid in the way |
| 238 | // we have build the test |
| 239 | // Also rootEntity is centered based on the size of the child it contains |
| 240 | proximityFilter->setEntity(targetEntity); |
| 241 | proximityFilter2->setEntity(targetEntity); |
| 242 | |
| 243 | Q_UNUSED(childEntity2); |
| 244 | |
| 245 | QTest::newRow("Nested-Step1") << rootEntity |
| 246 | << (Qt3DCore::QNodeIdVector() << proximityFilter->id()) |
| 247 | << (Qt3DCore::QNodeIdVector() |
| 248 | << targetEntity->id() |
| 249 | << childEntity1->id() |
| 250 | << childEntity3->id() |
| 251 | ); |
| 252 | QTest::newRow("Nested-Step2") << rootEntity |
| 253 | << (Qt3DCore::QNodeIdVector() << proximityFilter->id() << proximityFilter2->id()) |
| 254 | << (Qt3DCore::QNodeIdVector() |
| 255 | << targetEntity->id() |
| 256 | << childEntity3->id() |
| 257 | ); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void filterEntities() |
| 262 | { |
| 263 | QFETCH(Qt3DCore::QEntity *, entitySubtree); |
| 264 | QFETCH(Qt3DCore::QNodeIdVector, proximityFilterIds); |
| 265 | QFETCH(Qt3DCore::QNodeIdVector, expectedSelectedEntities); |
| 266 | |
| 267 | // GIVEN |
| 268 | QScopedPointer<Qt3DRender::TestAspect> aspect(new Qt3DRender::TestAspect(entitySubtree)); |
| 269 | aspect->registerTree(entitySubtree); |
| 270 | |
| 271 | // WHEN |
| 272 | Qt3DRender::Render::Entity *backendRoot = aspect->nodeManagers()->renderNodesManager()->getOrCreateResource(entitySubtree->id()); |
| 273 | |
| 274 | Qt3DRender::Render::UpdateTreeEnabledJob updateTreeEnabledJob; |
| 275 | updateTreeEnabledJob.setRoot(backendRoot); |
| 276 | updateTreeEnabledJob.setManagers(aspect->nodeManagers()); |
| 277 | updateTreeEnabledJob.run(); |
| 278 | |
| 279 | Qt3DRender::Render::UpdateWorldTransformJob updateWorldTransform; |
| 280 | updateWorldTransform.setRoot(backendRoot); |
| 281 | updateWorldTransform.setManagers(aspect->nodeManagers()); |
| 282 | updateWorldTransform.run(); |
| 283 | |
| 284 | Qt3DRender::Render::CalculateBoundingVolumeJob calcBVolume; |
| 285 | calcBVolume.setManagers(aspect->nodeManagers()); |
| 286 | calcBVolume.setRoot(backendRoot); |
| 287 | calcBVolume.setFrontEndNodeManager(aspect.data()); |
| 288 | calcBVolume.run(); |
| 289 | |
| 290 | Qt3DRender::Render::UpdateWorldBoundingVolumeJob updateWorldBVolume; |
| 291 | updateWorldBVolume.setManager(aspect->nodeManagers()->renderNodesManager()); |
| 292 | updateWorldBVolume.run(); |
| 293 | |
| 294 | Qt3DRender::Render::ExpandBoundingVolumeJob expandBVolume; |
| 295 | expandBVolume.setRoot(backendRoot); |
| 296 | expandBVolume.setManagers(aspect->nodeManagers()); |
| 297 | expandBVolume.run(); |
| 298 | |
| 299 | // WHEN |
| 300 | Qt3DRender::Render::FilterProximityDistanceJob filterJob; |
| 301 | filterJob.setProximityFilterIds(proximityFilterIds); |
| 302 | filterJob.setManager(aspect->nodeManagers()); |
| 303 | filterJob.run(); |
| 304 | |
| 305 | // THEN |
| 306 | const std::vector<Qt3DRender::Render::Entity *> &filterEntities = filterJob.filteredEntities(); |
| 307 | QCOMPARE(filterEntities.size(), size_t(expectedSelectedEntities.size())); |
| 308 | |
| 309 | for (int i = 0, m = expectedSelectedEntities.size(); i < m; ++i) |
| 310 | QCOMPARE(filterEntities.at(i)->peerId(), expectedSelectedEntities.at(i)); |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | QTEST_MAIN(tst_ProximityFiltering) |
| 315 | |
| 316 | #include "tst_proximityfiltering.moc" |