1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QT3DRENDER_RENDER_PICKBOUNDINGVOLUMEUTILS_H |
41 | #define QT3DRENDER_RENDER_PICKBOUNDINGVOLUMEUTILS_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of other Qt classes. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <Qt3DCore/QNodeId> |
55 | #include <Qt3DRender/QAbstractRayCaster> |
56 | #include <Qt3DRender/private/qray3d_p.h> |
57 | #include <Qt3DRender/private/qraycastingservice_p.h> |
58 | #include <Qt3DRender/qpickingsettings.h> |
59 | |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | |
63 | class QSurface; |
64 | |
65 | namespace Qt3DRender { |
66 | namespace RayCasting { |
67 | class QAbstractCollisionQueryService; |
68 | } |
69 | |
70 | namespace Render { |
71 | |
72 | class Entity; |
73 | class Renderer; |
74 | class FrameGraphNode; |
75 | class NodeManagers; |
76 | |
77 | namespace PickingUtils { |
78 | |
79 | struct Q_AUTOTEST_EXPORT ViewportCameraAreaDetails |
80 | { |
81 | Qt3DCore::QNodeId cameraId; |
82 | Qt3DCore::QNodeId viewportNodeId; |
83 | QRectF viewport; |
84 | QSize area; |
85 | QSurface *surface = nullptr; |
86 | Qt3DCore::QNodeIdVector layersFilters; |
87 | }; |
88 | QT3D_DECLARE_TYPEINFO_3(Qt3DRender, Render, PickingUtils, ViewportCameraAreaDetails, Q_COMPLEX_TYPE) |
89 | |
90 | class Q_AUTOTEST_EXPORT ViewportCameraAreaGatherer |
91 | { |
92 | public: |
93 | ViewportCameraAreaGatherer(const Qt3DCore::QNodeId &nodeId = Qt3DCore::QNodeId()) : m_targetCamera(nodeId) { } |
94 | QVector<ViewportCameraAreaDetails> gather(FrameGraphNode *root); |
95 | |
96 | private: |
97 | Qt3DCore::QNodeId m_targetCamera; |
98 | QVector<FrameGraphNode *> m_leaves; |
99 | |
100 | void visit(FrameGraphNode *node); |
101 | ViewportCameraAreaDetails gatherUpViewportCameraAreas(Render::FrameGraphNode *node) const; |
102 | bool isUnique(const QVector<ViewportCameraAreaDetails> &vcaList, const ViewportCameraAreaDetails &vca) const; |
103 | }; |
104 | |
105 | typedef QVector<RayCasting::QCollisionQueryResult::Hit> HitList; |
106 | |
107 | class Q_AUTOTEST_EXPORT HierarchicalEntityPicker |
108 | { |
109 | public: |
110 | explicit HierarchicalEntityPicker(const RayCasting::QRay3D &ray, bool requireObjectPicker = true); |
111 | |
112 | void setLayerFilterIds(const Qt3DCore::QNodeIdVector &layerFilterIds); |
113 | void setLayerIds(const Qt3DCore::QNodeIdVector &layerIds, QAbstractRayCaster::FilterMode mode); |
114 | |
115 | bool collectHits(NodeManagers *manager, Entity *root); |
116 | inline HitList hits() const { return m_hits; } |
117 | inline QVector<Entity *> entities() const { return m_entities; } |
118 | inline QHash<Qt3DCore::QNodeId, int> entityToPriorityTable() const { return m_entityToPriorityTable; } |
119 | |
120 | private: |
121 | RayCasting::QRay3D m_ray; |
122 | HitList m_hits; |
123 | QVector<Entity *> m_entities; |
124 | bool m_objectPickersRequired; |
125 | Qt3DCore::QNodeIdVector m_layerFilterIds; |
126 | Qt3DCore::QNodeIdVector m_layerIds; |
127 | QAbstractRayCaster::FilterMode m_layerFilterMode = QAbstractRayCaster::AcceptAnyMatchingLayers; |
128 | QHash<Qt3DCore::QNodeId, int> m_entityToPriorityTable; |
129 | }; |
130 | |
131 | struct Q_AUTOTEST_EXPORT AbstractCollisionGathererFunctor |
132 | { |
133 | AbstractCollisionGathererFunctor(); |
134 | virtual ~AbstractCollisionGathererFunctor(); |
135 | |
136 | bool m_objectPickersRequired = true; |
137 | NodeManagers *m_manager = nullptr; |
138 | RayCasting::QRay3D m_ray; |
139 | QHash<Qt3DCore::QNodeId, int> m_entityToPriorityTable; |
140 | |
141 | virtual HitList computeHits(const QVector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) = 0; |
142 | |
143 | // This define is required to work with QtConcurrent |
144 | typedef HitList result_type; |
145 | HitList operator ()(const Entity *entity) const; |
146 | virtual HitList pick(const Entity *entity) const = 0; |
147 | |
148 | bool rayHitsEntity(const Entity *entity) const; |
149 | static void sortHits(HitList &results); |
150 | }; |
151 | |
152 | struct Q_AUTOTEST_EXPORT EntityCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
153 | { |
154 | HitList computeHits(const QVector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
155 | HitList pick(const Entity *entity) const override; |
156 | }; |
157 | |
158 | struct Q_AUTOTEST_EXPORT TriangleCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
159 | { |
160 | bool m_frontFaceRequested; |
161 | bool m_backFaceRequested; |
162 | |
163 | HitList computeHits(const QVector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
164 | HitList pick(const Entity *entity) const override; |
165 | }; |
166 | |
167 | struct Q_AUTOTEST_EXPORT LineCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
168 | { |
169 | float m_pickWorldSpaceTolerance; |
170 | |
171 | HitList computeHits(const QVector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
172 | HitList pick(const Entity *entity) const override; |
173 | }; |
174 | |
175 | struct Q_AUTOTEST_EXPORT PointCollisionGathererFunctor : public AbstractCollisionGathererFunctor |
176 | { |
177 | float m_pickWorldSpaceTolerance; |
178 | |
179 | HitList computeHits(const QVector<Entity *> &entities, Qt3DRender::QPickingSettings::PickResultMode mode) override; |
180 | HitList pick(const Entity *entity) const override; |
181 | }; |
182 | |
183 | } // PickingUtils |
184 | |
185 | } // Render |
186 | |
187 | } // Qt3DRender |
188 | |
189 | QT_END_NAMESPACE |
190 | |
191 | #endif // QT3DRENDER_RENDER_PICKBOUNDINGVOLUMEUTILS_H |
192 | |