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 "qraycaster.h"
5#include "qabstractraycaster_p.h"
6#include <Qt3DCore/qentity.h>
7#include <Qt3DCore/private/qcomponent_p.h>
8#include <Qt3DCore/private/qscene_p.h>
9
10QT_BEGIN_NAMESPACE
11
12namespace Qt3DRender {
13
14/*!
15 \class Qt3DRender::QRayCaster
16 \inmodule Qt3DRender
17
18 \brief Qt3DRender::QRayCaster is used to perform ray casting tests in 3d world coordinates.
19 \inmodule Qt3DRender
20 \since 5.11
21 \inherits QAbstractRayCaster
22
23 The 3d ray is defined by its origin, direction and length. It will be affected by the
24 transformations applied to the entity it belongs to.
25
26 Ray casting tests will be performed every frame as long as the component is enabled.
27 The hits property will be updated with the list of intersections.
28
29 \sa QAbstractRayCaster, QScreenRayCaster, QNoPicking
30*/
31/*!
32 \qmltype RayCaster
33 \brief used to perform ray casting tests in 3d world coordinates.
34 \inqmlmodule Qt3D.Render
35 \since 5.11
36 \instantiates Qt3DRender::QRayCaster
37
38 The 3d ray is defined by its origin, direction and length. It will be affected by the
39 transformations applied to the entity it belongs to.
40
41 Ray casting tests will be performed every frame as long as the component is enabled.
42 The hits property will be updated with the list of intersections.
43
44 \sa AbstractRayCaster, ScreenRayCaster, NoPicking
45*/
46
47/*!
48 \property Qt3DRender::QRayCaster::origin
49
50 Holds the origin of the 3D ray in local coordinates.
51*/
52/*!
53 \qmlproperty vector3d Qt3D.Render::RayCaster::origin
54
55 Holds the origin of the 3D ray in local coordinates.
56*/
57
58/*!
59 \property Qt3DRender::QRayCaster::direction
60
61 Holds the direction of the 3D ray. This should be a unit vector.
62*/
63
64/*!
65 \qmlproperty vector3D Qt3D.Render::RayCaster::direction
66
67 Holds the direction of the 3D ray. This should be a unit vector.
68*/
69
70/*!
71 \property Qt3DRender::QRayCaster::length
72
73 Holds the length of the 3D ray.
74*/
75
76/*!
77 \qmlproperty real Qt3D.Render::RayCaster::length
78
79 Holds the length of the 3d ray.
80*/
81
82
83QRayCaster::QRayCaster(Qt3DCore::QNode *parent)
84 : QAbstractRayCaster(parent)
85{
86 QAbstractRayCasterPrivate::get(obj: this)->m_rayCasterType = QAbstractRayCasterPrivate::WorldSpaceRayCaster;
87}
88
89QRayCaster::QRayCaster(QAbstractRayCasterPrivate &dd, Qt3DCore::QNode *parent)
90 : QAbstractRayCaster(dd, parent)
91{
92 QAbstractRayCasterPrivate::get(obj: this)->m_rayCasterType = QAbstractRayCasterPrivate::WorldSpaceRayCaster;
93}
94
95/*! \internal */
96QRayCaster::~QRayCaster()
97{
98}
99
100QVector3D QRayCaster::origin() const
101{
102 auto d = QAbstractRayCasterPrivate::get(obj: this);
103 return d->m_origin;
104}
105
106void QRayCaster::setOrigin(const QVector3D &origin)
107{
108 auto d = QAbstractRayCasterPrivate::get(obj: this);
109 if (d->m_origin != origin) {
110 d->m_origin = origin;
111 emit originChanged(origin: d->m_origin);
112 }
113}
114
115QVector3D QRayCaster::direction() const
116{
117 auto d = QAbstractRayCasterPrivate::get(obj: this);
118 return d->m_direction;
119}
120
121void QRayCaster::setDirection(const QVector3D &direction)
122{
123 auto d = QAbstractRayCasterPrivate::get(obj: this);
124 if (d->m_direction != direction) {
125 d->m_direction = direction;
126 emit directionChanged(direction: d->m_direction);
127 }
128}
129
130float QRayCaster::length() const
131{
132 auto d = QAbstractRayCasterPrivate::get(obj: this);
133 return d->m_length;
134}
135
136/*!
137 * \brief Sets the length of the ray to \a length.
138 *
139 * If the value is less than or equal to zero, the ray is concidered to be infinite.
140 */
141void QRayCaster::setLength(float length)
142{
143 auto d = QAbstractRayCasterPrivate::get(obj: this);
144 if (!qFuzzyCompare(p1: d->m_length, p2: length)) {
145 d->m_length = length;
146 emit lengthChanged(length: d->m_length);
147 }
148}
149
150/*!
151 * Convenience method to enable the component and trigger tests using the current ray.
152 */
153void QRayCaster::trigger()
154{
155 setEnabled(true);
156}
157
158/*!
159 * Convenience method to set the ray details \a origin, \a direction, and \a length,
160 * and enable the component to trigger tests.
161 */
162void QRayCaster::trigger(const QVector3D &origin, const QVector3D &direction, float length)
163{
164 setOrigin(origin);
165 setDirection(direction);
166 setLength(length);
167 setEnabled(true);
168}
169
170QAbstractRayCaster::Hits QRayCaster::pick(const QVector3D &origin, const QVector3D &direction, float length)
171{
172 setOrigin(origin);
173 setDirection(direction);
174 setLength(length);
175
176 auto d = QAbstractRayCasterPrivate::get(obj: this);
177 return d->pick();
178}
179
180} // Qt3DRender
181
182QT_END_NAMESPACE
183
184#include "moc_qraycaster.cpp"
185

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