| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QT3DRENDER_RENDER_PICKBOUNDINGVOLUMEUTILS_H |
| 5 | #define QT3DRENDER_RENDER_PICKBOUNDINGVOLUMEUTILS_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <Qt3DCore/QNodeId> |
| 19 | #include <Qt3DRender/QAbstractRayCaster> |
| 20 | #include <Qt3DRender/private/qray3d_p.h> |
| 21 | #include <Qt3DRender/private/qraycastingservice_p.h> |
| 22 | #include <Qt3DRender/qpickingsettings.h> |
| 23 | |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QSurface; |
| 28 | |
| 29 | namespace Qt3DRender { |
| 30 | namespace RayCasting { |
| 31 | class QAbstractCollisionQueryService; |
| 32 | } |
| 33 | |
| 34 | namespace Render { |
| 35 | |
| 36 | class Entity; |
| 37 | class Renderer; |
| 38 | class FrameGraphNode; |
| 39 | class RenderSettings; |
| 40 | class NodeManagers; |
| 41 | |
| 42 | namespace PickingUtils { |
| 43 | |
| 44 | struct Q_AUTOTEST_EXPORT ViewportCameraAreaDetails |
| 45 | { |
| 46 | Qt3DCore::QNodeId cameraId; |
| 47 | Qt3DCore::QNodeId viewportNodeId; |
| 48 | QRectF viewport; |
| 49 | QSize area; |
| 50 | QSurface *surface = nullptr; |
| 51 | Qt3DCore::QNodeIdVector layersFilters; |
| 52 | }; |
| 53 | QT3D_DECLARE_TYPEINFO_3(Qt3DRender, Render, PickingUtils, ViewportCameraAreaDetails, Q_COMPLEX_TYPE) |
| 54 | |
| 55 | struct PickConfiguration { |
| 56 | std::vector<ViewportCameraAreaDetails> vcaDetails; |
| 57 | bool trianglePickingRequested = false; |
| 58 | bool edgePickingRequested = false; |
| 59 | bool pointPickingRequested = false; |
| 60 | bool primitivePickingRequested = false; |
| 61 | bool frontFaceRequested = false; |
| 62 | bool backFaceRequested = false; |
| 63 | float pickWorldSpaceTolerance = -1.f; |
| 64 | |
| 65 | PickConfiguration(FrameGraphNode *frameGraphRoot, RenderSettings *renderSettings); |
| 66 | }; |
| 67 | |
| 68 | class Q_AUTOTEST_EXPORT ViewportCameraAreaGatherer |
| 69 | { |
| 70 | public: |
| 71 | ViewportCameraAreaGatherer(const Qt3DCore::QNodeId &nodeId = Qt3DCore::QNodeId()) : m_targetCamera(nodeId) { } |
| 72 | std::vector<ViewportCameraAreaDetails> gather(FrameGraphNode *root); |
| 73 | |
| 74 | private: |
| 75 | Qt3DCore::QNodeId m_targetCamera; |
| 76 | std::vector<FrameGraphNode *> m_leaves; |
| 77 | |
| 78 | void visit(FrameGraphNode *node); |
| 79 | ViewportCameraAreaDetails gatherUpViewportCameraAreas(Render::FrameGraphNode *node) const; |
| 80 | bool isUnique(const std::vector<ViewportCameraAreaDetails> &vcaList, const ViewportCameraAreaDetails &vca) const; |
| 81 | }; |
| 82 | |
| 83 | typedef std::vector<RayCasting::QCollisionQueryResult::Hit> HitList; |
| 84 | |
| 85 | class Q_AUTOTEST_EXPORT HierarchicalEntityPicker |
| 86 | { |
| 87 | public: |
| 88 | explicit HierarchicalEntityPicker(const RayCasting::QRay3D &ray, bool requireObjectPicker = true); |
| 89 | |
| 90 | void setLayerFilterIds(const Qt3DCore::QNodeIdVector &layerFilterIds); |
| 91 | void setLayerIds(const Qt3DCore::QNodeIdVector &layerIds, QAbstractRayCaster::FilterMode mode); |
| 92 | |
| 93 | bool collectHits(NodeManagers *manager, Entity *root); |
| 94 | inline HitList hits() const { return m_hits; } |
| 95 | inline const std::vector<Entity *> &entities() const { return m_entities; } |
| 96 | inline QHash<Qt3DCore::QNodeId, int> entityToPriorityTable() const { return m_entityToPriorityTable; } |
| 97 | |
| 98 | private: |
| 99 | RayCasting::QRay3D m_ray; |
| 100 | HitList m_hits; |
| 101 | std::vector<Entity *> m_entities; |
| 102 | bool m_objectPickersRequired; |
| 103 | Qt3DCore::QNodeIdVector m_layerFilterIds; |
| 104 | Qt3DCore::QNodeIdVector m_layerIds; |
| 105 | QAbstractRayCaster::FilterMode m_layerFilterMode = QAbstractRayCaster::AcceptAnyMatchingLayers; |
| 106 | QHash<Qt3DCore::QNodeId, int> m_entityToPriorityTable; |
| 107 | }; |
| 108 | |
| 109 | struct Q_AUTOTEST_EXPORT AbstractCollisionGathererFunctor |
| 110 | { |
| 111 | AbstractCollisionGathererFunctor(); |
| 112 | virtual ~AbstractCollisionGathererFunctor(); |
| 113 | |
| 114 | bool m_objectPickersRequired = true; |
| 115 | NodeManagers *m_manager = nullptr; |
| 116 | RayCasting::QRay3D m_ray; |
| 117 | QHash<Qt3DCore::QNodeId, int> m_entityToPriorityTable; |
| 118 | |
| 119 | virtual HitList computeHits(const std::vector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) = 0; |
| 120 | |
| 121 | // This define is required to work with QtConcurrent |
| 122 | typedef HitList result_type; |
| 123 | HitList operator ()(const Entity *entity) const; |
| 124 | virtual HitList pick(const Entity *entity) const = 0; |
| 125 | |
| 126 | bool rayHitsEntity(const Entity *entity) const; |
| 127 | static void sortHits(HitList &results); |
| 128 | }; |
| 129 | |
| 130 | struct Q_AUTOTEST_EXPORT EntityCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
| 131 | { |
| 132 | HitList computeHits(const std::vector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
| 133 | HitList pick(const Entity *entity) const override; |
| 134 | }; |
| 135 | |
| 136 | struct Q_AUTOTEST_EXPORT TriangleCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
| 137 | { |
| 138 | bool m_frontFaceRequested; |
| 139 | bool m_backFaceRequested; |
| 140 | |
| 141 | HitList computeHits(const std::vector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
| 142 | HitList pick(const Entity *entity) const override; |
| 143 | }; |
| 144 | |
| 145 | struct Q_AUTOTEST_EXPORT LineCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
| 146 | { |
| 147 | float m_pickWorldSpaceTolerance; |
| 148 | |
| 149 | HitList computeHits(const std::vector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
| 150 | HitList pick(const Entity *entity) const override; |
| 151 | }; |
| 152 | |
| 153 | struct Q_AUTOTEST_EXPORT PointCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
| 154 | { |
| 155 | float m_pickWorldSpaceTolerance; |
| 156 | |
| 157 | HitList computeHits(const std::vector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
| 158 | HitList pick(const Entity *entity) const override; |
| 159 | }; |
| 160 | |
| 161 | } // PickingUtils |
| 162 | |
| 163 | } // Render |
| 164 | |
| 165 | } // Qt3DRender |
| 166 | |
| 167 | QT_END_NAMESPACE |
| 168 | |
| 169 | #endif // QT3DRENDER_RENDER_PICKBOUNDINGVOLUMEUTILS_H |
| 170 | |