1/****************************************************************************
2**
3** Copyright (C) 2018 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#include "qraycasterhit.h"
41
42QT_BEGIN_NAMESPACE
43
44namespace Qt3DRender {
45
46class QRayCasterHitData : public QSharedData
47{
48public:
49 QRayCasterHitData() { }
50 QRayCasterHitData(QRayCasterHit::HitType type, Qt3DCore::QNodeId id, float distance,
51 const QVector3D &localIntersect, const QVector3D &worldIntersect,
52 uint primitiveIndex, uint v1 = 0, uint v2 = 0, uint v3 = 0);
53
54 QRayCasterHit::HitType m_type = QRayCasterHit::EntityHit;
55 Qt3DCore::QNodeId m_entityId;
56 Qt3DCore::QEntity *m_entity = nullptr;
57 float m_distance = 0.f;
58 QVector3D m_localIntersection;
59 QVector3D m_worldIntersection;
60 uint m_primitiveIndex = 0;
61 uint m_vertex1Index = 0;
62 uint m_vertex2Index = 0;
63 uint m_vertex3Index = 0;
64};
65
66QRayCasterHitData::QRayCasterHitData(QRayCasterHit::HitType type, Qt3DCore::QNodeId id, float distance,
67 const QVector3D &localIntersect, const QVector3D &worldIntersect,
68 uint primitiveIndex, uint v1, uint v2, uint v3)
69 : m_type(type)
70 , m_entityId(id)
71 , m_entity(nullptr)
72 , m_distance(distance)
73 , m_localIntersection(localIntersect)
74 , m_worldIntersection(worldIntersect)
75 , m_primitiveIndex(primitiveIndex)
76 , m_vertex1Index(v1)
77 , m_vertex2Index(v2)
78 , m_vertex3Index(v3)
79{
80
81}
82
83/*!
84 \class Qt3DRender::QRayCasterHit
85 \brief Details of a hit when casting a ray through a model.
86 \inmodule Qt3DRender
87 \since 5.11
88
89 Qt3DRender::QRayCasterHit contains the details of a successful hit when casting a ray through
90 a model using a Qt3DRender::QRayCaster or Qt3DRender::QScreenRayCaster component.
91
92 \sa Qt3DRender::QRayCaster, Qt3DRender::QScreenRayCaster, Qt3DRender::QPickingSettings
93*/
94
95/*!
96 \enum QRayCasterHit::HitType
97
98 Specifies type of hit that was returned. This is controlled using QPickingSettings.
99
100 \value TriangleHit The picked primitive was a triangle and the vertex indices refer to the
101 three points making up the triangle
102
103 \value LineHit The picked primitive was a line segment, and the first two vertices refer to
104 the two points making up the line
105
106 \value PointHit The picked primitive was a single point; all 3 vertex indices
107 will be undefined
108
109 \value EntityHit Only the bounding volume was considered; the primitive and vertex indices
110 will be undefined
111*/
112
113QRayCasterHit::QRayCasterHit()
114 : d(new QRayCasterHitData)
115{
116
117}
118
119QRayCasterHit::QRayCasterHit(QRayCasterHit::HitType type, Qt3DCore::QNodeId id, float distance,
120 const QVector3D &localIntersect, const QVector3D &worldIntersect,
121 uint primitiveIndex, uint v1, uint v2, uint v3)
122 : d(new QRayCasterHitData(type, id, distance, localIntersect, worldIntersect, primitiveIndex, v1, v2, v3))
123{
124
125}
126
127QRayCasterHit::QRayCasterHit(const QRayCasterHit &other)
128 : d(other.d)
129{
130
131}
132
133QRayCasterHit::~QRayCasterHit()
134{
135
136}
137
138QRayCasterHit &QRayCasterHit::operator =(const QRayCasterHit &other)
139{
140 d = other.d;
141 return *this;
142}
143
144/*!
145 * \brief Returns the type of the hit
146 */
147QRayCasterHit::HitType Qt3DRender::QRayCasterHit::type() const
148{
149 return d->m_type;
150}
151
152/*!
153 * \brief Returns the id of the entity that was hit
154 */
155Qt3DCore::QNodeId QRayCasterHit::entityId() const
156{
157 return d->m_entityId;
158}
159
160/*!
161 * \brief Returns a pointer to the entity that was hit
162 */
163Qt3DCore::QEntity *QRayCasterHit::entity() const
164{
165 return d->m_entity;
166}
167
168/*!
169 * \brief Returns the distance between the origin of the ray and the intersection point
170 */
171float QRayCasterHit::distance() const
172{
173 return d->m_distance;
174}
175
176/*!
177 * \brief Returns the coordinates of the intersection point in the entity's coordinate system
178 */
179QVector3D QRayCasterHit::localIntersection() const
180{
181 return d->m_localIntersection;
182}
183
184/*!
185 * \brief Returns the coordinates of the intersection point in the model's coordinate system
186 */
187QVector3D QRayCasterHit::worldIntersection() const
188{
189 return d->m_worldIntersection;
190}
191
192/*!
193 * \brief Returns the index of the picked primitive
194 */
195uint QRayCasterHit::primitiveIndex() const
196{
197 return d->m_primitiveIndex;
198}
199
200/*!
201 * \brief Returns the index of the first vertex of the picked primitive
202 */
203uint QRayCasterHit::vertex1Index() const
204{
205 return d->m_vertex1Index;
206}
207
208/*!
209 * \brief Returns the index of the second vertex of the picked primitive
210 */
211uint QRayCasterHit::vertex2Index() const
212{
213 return d->m_vertex2Index;
214}
215
216/*!
217 * \brief Returns the index of the third vertex of the picked primitive
218 */
219uint QRayCasterHit::vertex3Index() const
220{
221 return d->m_vertex3Index;
222}
223
224/*! \internal */
225void QRayCasterHit::setEntity(Qt3DCore::QEntity *entity) const
226{
227 // don't detach
228 const_cast<QRayCasterHitData *>(d.constData())->m_entity = entity;
229}
230
231} // Qt3DRender
232
233QT_END_NAMESPACE
234

source code of qt3d/src/render/picking/qraycasterhit.cpp