1// Copyright (C) 2018 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#include "qraycasterhit.h"
5#include <Qt3DCore/qentity.h>
6
7QT_BEGIN_NAMESPACE
8
9namespace Qt3DRender {
10
11class QRayCasterHitData : public QSharedData
12{
13public:
14 QRayCasterHitData() { }
15 QRayCasterHitData(QRayCasterHit::HitType type, Qt3DCore::QNodeId id, float distance,
16 const QVector3D &localIntersect, const QVector3D &worldIntersect,
17 uint primitiveIndex, uint v1 = 0, uint v2 = 0, uint v3 = 0);
18 QRayCasterHitData(const QRayCasterHitData& other) : QSharedData(), m_type(other.m_type), m_entityId(other.m_entityId), m_entity(other.m_entity),
19 m_distance(other.m_distance), m_localIntersection(other.m_localIntersection),
20 m_worldIntersection(other.m_worldIntersection), m_primitiveIndex(other.m_primitiveIndex),
21 m_vertex1Index(other.m_vertex1Index), m_vertex2Index(other.m_vertex2Index), m_vertex3Index(other.m_vertex3Index) { }
22
23 QRayCasterHit::HitType m_type = QRayCasterHit::EntityHit;
24 Qt3DCore::QNodeId m_entityId;
25 Qt3DCore::QEntity *m_entity = nullptr;
26 float m_distance = 0.f;
27 QVector3D m_localIntersection;
28 QVector3D m_worldIntersection;
29 uint m_primitiveIndex = 0;
30 uint m_vertex1Index = 0;
31 uint m_vertex2Index = 0;
32 uint m_vertex3Index = 0;
33};
34
35QRayCasterHitData::QRayCasterHitData(QRayCasterHit::HitType type, Qt3DCore::QNodeId id, float distance,
36 const QVector3D &localIntersect, const QVector3D &worldIntersect,
37 uint primitiveIndex, uint v1, uint v2, uint v3)
38 : m_type(type)
39 , m_entityId(id)
40 , m_entity(nullptr)
41 , m_distance(distance)
42 , m_localIntersection(localIntersect)
43 , m_worldIntersection(worldIntersect)
44 , m_primitiveIndex(primitiveIndex)
45 , m_vertex1Index(v1)
46 , m_vertex2Index(v2)
47 , m_vertex3Index(v3)
48{
49
50}
51
52/*!
53 \class Qt3DRender::QRayCasterHit
54 \brief Details of a hit when casting a ray through a model.
55 \inmodule Qt3DRender
56 \since 5.11
57
58 Qt3DRender::QRayCasterHit contains the details of a successful hit when casting a ray through
59 a model using a Qt3DRender::QRayCaster or Qt3DRender::QScreenRayCaster component.
60
61 \sa Qt3DRender::QRayCaster, Qt3DRender::QScreenRayCaster, Qt3DRender::QPickingSettings
62*/
63
64/*!
65 \enum QRayCasterHit::HitType
66
67 Specifies type of hit that was returned. This is controlled using QPickingSettings.
68
69 \value TriangleHit The picked primitive was a triangle and the vertex indices refer to the
70 three points making up the triangle
71
72 \value LineHit The picked primitive was a line segment, and the first two vertices refer to
73 the two points making up the line
74
75 \value PointHit The picked primitive was a single point; all 3 vertex indices
76 will be undefined
77
78 \value EntityHit Only the bounding volume was considered; the primitive and vertex indices
79 will be undefined
80*/
81
82QRayCasterHit::QRayCasterHit()
83 : d(new QRayCasterHitData)
84{
85
86}
87
88QRayCasterHit::QRayCasterHit(QRayCasterHit::HitType type, Qt3DCore::QNodeId id, float distance,
89 const QVector3D &localIntersect, const QVector3D &worldIntersect,
90 uint primitiveIndex, uint v1, uint v2, uint v3)
91 : d(new QRayCasterHitData(type, id, distance, localIntersect, worldIntersect, primitiveIndex, v1, v2, v3))
92{
93}
94
95QRayCasterHit::QRayCasterHit(const QRayCasterHit &other)
96 : d(other.d)
97{
98
99}
100
101QRayCasterHit::~QRayCasterHit()
102{
103
104}
105
106QRayCasterHit &QRayCasterHit::operator =(const QRayCasterHit &other)
107{
108 d = other.d;
109 return *this;
110}
111
112/*!
113 * \brief Returns the type of the hit
114 */
115QRayCasterHit::HitType Qt3DRender::QRayCasterHit::type() const
116{
117 return d->m_type;
118}
119
120/*!
121 * \brief Returns the id of the entity that was hit
122 */
123Qt3DCore::QNodeId QRayCasterHit::entityId() const
124{
125 return d->m_entityId;
126}
127
128/*!
129 * \brief Returns a pointer to the entity that was hit
130 */
131Qt3DCore::QEntity *QRayCasterHit::entity() const
132{
133 return d->m_entity;
134}
135
136/*!
137 * \brief Returns the distance between the origin of the ray and the intersection point
138 */
139float QRayCasterHit::distance() const
140{
141 return d->m_distance;
142}
143
144/*!
145 * \brief Returns the coordinates of the intersection point in the entity's coordinate system
146 */
147QVector3D QRayCasterHit::localIntersection() const
148{
149 return d->m_localIntersection;
150}
151
152/*!
153 * \brief Returns the coordinates of the intersection point in the model's coordinate system
154 */
155QVector3D QRayCasterHit::worldIntersection() const
156{
157 return d->m_worldIntersection;
158}
159
160/*!
161 * \brief Returns the index of the picked primitive
162 */
163uint QRayCasterHit::primitiveIndex() const
164{
165 return d->m_primitiveIndex;
166}
167
168/*!
169 * \brief Returns the index of the first vertex of the picked primitive
170 */
171uint QRayCasterHit::vertex1Index() const
172{
173 return d->m_vertex1Index;
174}
175
176/*!
177 * \brief Returns the index of the second vertex of the picked primitive
178 */
179uint QRayCasterHit::vertex2Index() const
180{
181 return d->m_vertex2Index;
182}
183
184/*!
185 * \brief Returns the index of the third vertex of the picked primitive
186 */
187uint QRayCasterHit::vertex3Index() const
188{
189 return d->m_vertex3Index;
190}
191
192QString QRayCasterHit::toString()
193{
194 QString res;
195 if (!d->m_entity)
196 return QLatin1String("{}");
197 if (d->m_entity->objectName().size())
198 res = d->m_entity->objectName();
199 else
200 res = QLatin1String("Entity");
201
202 res += QString(QLatin1String(" (%1) Distance: %2 Local: (%3, %4, %5) World: (%6, %7, %8)"))
203 .arg(a: d->m_entity->id().id()).arg(a: double(d->m_distance))
204 .arg(a: double(d->m_localIntersection.x())).arg(a: double(d->m_localIntersection.y())).arg(a: double(d->m_localIntersection.z()))
205 .arg(a: double(d->m_worldIntersection.x())).arg(a: double(d->m_worldIntersection.y())).arg(a: double(d->m_worldIntersection.z()));
206
207 switch (d->m_type) {
208 case TriangleHit:
209 res += QString(QLatin1String(" Type: Triangle Index: %1 Vertices: %2 / %3 / %4")).arg(a: d->m_primitiveIndex).arg(a: d->m_vertex1Index).arg(a: d->m_vertex2Index).arg(a: d->m_vertex3Index);
210 break;
211 case LineHit:
212 res += QString(QLatin1String(" Type: Line Index: %1 Vertices: %2 / %3")).arg(a: d->m_primitiveIndex).arg(a: d->m_vertex1Index).arg(a: d->m_vertex2Index);
213 break;
214 case PointHit:
215 res += QString(QLatin1String(" Type: Point Index: %1")).arg(a: d->m_primitiveIndex);
216 break;
217 case EntityHit:
218 res += QLatin1String(" Type: Entity");
219 break;
220 }
221 return res;
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
235#include "moc_qraycasterhit.cpp"
236

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