summaryrefslogtreecommitdiffstats
path: root/src/render/picking/qabstractraycaster.cpp
blob: 42c7bc1e5ff88119de98c87f8c3beb4da76448b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/****************************************************************************
**
** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qabstractraycaster.h"
#include "qabstractraycaster_p.h"
#include <Qt3DCore/qentity.h>
#include <Qt3DCore/qpropertyupdatedchange.h>
#include <Qt3DCore/qpropertynodeaddedchange.h>
#include <Qt3DCore/qpropertynoderemovedchange.h>
#include <Qt3DCore/private/qcomponent_p.h>
#include <Qt3DCore/private/qscene_p.h>
#include <Qt3DRender/qlayer.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {

QAbstractRayCasterPrivate::QAbstractRayCasterPrivate()
    : QComponentPrivate()
{
    m_enabled = false;
    m_shareable = false;
}

QAbstractRayCasterPrivate *QAbstractRayCasterPrivate::get(QAbstractRayCaster *obj)
{
    return obj->d_func();
}

const QAbstractRayCasterPrivate *QAbstractRayCasterPrivate::get(const QAbstractRayCaster *obj)
{
    return obj->d_func();
}

void QAbstractRayCasterPrivate::updateHitEntites(QAbstractRayCaster::Hits &hits, Qt3DCore::QScene *scene)
{
    for (int i = 0; i < hits.size(); i++)
        hits[i].setEntity(qobject_cast<Qt3DCore::QEntity *>(scene->lookupNode(hits[i].entityId())));
}

void QAbstractRayCasterPrivate::dispatchHits(const QAbstractRayCaster::Hits &hits)
{
    Q_Q(QAbstractRayCaster);
    m_hits = hits;
    updateHitEntites(m_hits, m_scene);
    bool v = q->blockNotifications(true);
    emit q->hitsChanged(m_hits);
    q->blockNotifications(v);
}

/*!
    \class Qt3DRender::QAbstractRayCaster
    \brief An abstract base class for ray casting in 3d scenes.
    \inmodule Qt3DRender
    \since 5.11
    \inherits QComponent

    Qt3DRender::QAbstractRayCaster is an abstract base class for casting rays into a 3d scene.
    Qt3DRender::QAbstractRayCaster can not be directly instantiated, but rather
    through its subclasses. QAbstractRayCaster specifies common properties
    for all ray casters, such as run mode and layer handling, while leaving the actual
    ray casting details to the subclasses.

    Ray castings differs from picking (using Qt3DRender::QObjectPicker) in that it does not
    require mouse events to trigger.

    By default, the instances of Qt3DRender::QAbstractRayCaster are disabled. When enabled,
    the specified ray will be tested for intersecting objects at every frame. The
    QAbstractRayCaster::hits property will be updated with the results of the ray casting,
    even if no objects are found.

    The Qt3DRender::QPickingSettings can be used to control the ray casting, such as which
    primitives are tested and how the results are returned.

    Furthermore, Qt3DRender::QLayer components can be used to control how entities, or entity
    sub-graphs, react to ray casting.

    \note Components derived from QAbstractRayCaster should not be shared amount multiple entities.

    \sa Qt3DRender::QRayCaster, Qt3DRender::QScreenRayCaster, Qt3DRender::QObjectPicker,
    Qt3DRender::QPickingSettings, Qt3DRender::QNoPicking
*/
/*!
    \qmltype AbstractRayCaster
    \brief An abstract base class for ray casting in 3d scenes.
    \inqmlmodule Qt3D.Render
    \since 5.11
    \instantiates Qt3DRender::QAbstractRayCaster

    AbstractRayCaster is an abstract base class for casting rays into a 3d scene.
    AbstractRayCaster can not be directly instantiated, but rather
    through its subclasses. QAbstractRayCaster specifies common properties
    for all ray casters, such as run mode and layer handling, while leaving the actual
    ray casting details to the subclasses.

    Ray castings differs from picking (using ObjectPicker) in that it does not
    require mouse events to trigger.

    By default, the instances of AbstractRayCaster are disabled. When enabled,
    the specified ray will be tested for intersecting objects at every frame. The
    AbstractRayCaster.hits property will be updated with the results of the ray casting,
    even if no objects are found.

    The Qt3D.Render::PickingSettings can be used to control the ray casting, such as which
    primitives are tested and how the results are returned.

    Furthermore, Qt3D.Render::Layer components can be used to control how entities, or entity
    sub-graphs, react to ray casting.

    Note: components derived from AbstractRayCaster should not be shared amount multiple entities.

    \sa Qt3D.Render::RayCaster, Qt3D.Render::ScreenRayCaster, Qt3D.Render::ObjectPicker,
    Qt3D.Render::PickingSettings, Qt3D.Render::NoPicking
*/

/*!
    \enum QAbstractRayCaster::RunMode

    This enumeration specifies how often ray casting is performed
    \value Continuous Ray casting is performed at every frame as long as the component is enabled.
    \value SingleShot Ray casting is done once then the component disables itself. This is the default
*/
/*!
    \enum QAbstractRayCaster::FilterMode

    Specifies the rules for selecting entities to test for raycasting.

    \value AcceptAnyMatchingLayers
    Accept entities that reference one or more \l QLayer objects added to this
    QAbstractRayCaster. This is the default

    \value AcceptAllMatchingLayers
    Accept entities that reference all the \l QLayer objects added to this QAbstractRayCaster

    \value DiscardAnyMatchingLayers
    Discard entities that reference one or more \l QLayer objects added to this QAbstractRayCaster

    \value DiscardAllMatchingLayers
    Discard entities that reference all \l QLayer objects added to this QAbstractRayCaster
*/

/*!
    \property Qt3DRender::QAbstractRayCaster::filterMode

    Holds the filter mode specifying the entities to select for ray casting tests.

    The default value is AcceptMatchingLayers.
*/
/*!
    \qmlproperty enumeration AbstractRayCaster::filterMode

    Holds the filter mode specifying the entities to select for ray casting tests.

    The default value is \c {AbstractRayCaster.AcceptMatchingLayers}.

    \value AcceptAnyMatchingLayers
    Accept entities that reference one or more \l Layer objects added to this
    AbstractRayCaster. This is the default

    \value AcceptAllMatchingLayers
    Accept entities that reference all the \l Layer objects added to this AbstractRayCaster

    \value DiscardAnyMatchingLayers
    Discard entities that reference one or more \l Layer objects added to this AbstractRayCaster

    \value DiscardAllMatchingLayers
    Discard entities that reference all \l Layer objects added to this AbstractRayCaster
*/

/*!
    \property Qt3DRender::QAbstractRayCaster::runMode

    Holds the run mode controlling how often ray casting tests are performed.

    If set to SingleShot (the default), when the component is enabled, a single ray casting
    test will be performed and the component will automatically disable itself.

    If set to Continuous, ray casting tests will be performed at every frame as long as
    the component is enabled.
*/
/*!
    \qmlproperty enumeration AbstractRayCaster::runMode
    Holds the run mode controlling how often ray casting tests are performed.

    \value Continuous Ray casting is performed at every frame as long as the component is enabled.

    \value SingleShot Ray casting is done once then the component disables itself. This is the default
*/

/*!
    \property Qt3DRender::QAbstractRayCaster::hits

    Holds the results of last ray casting test as a vector of Qt3DRender::QRayCasterHit instances.

    Note that even if successive tests return the exact same results (or empty results), a
    change notification will be emitted at every test.
*/

/*!
    \qmlproperty array AbstractRayCaster::hits

    Holds the results of last ray casting test as an array of javascript objects. The fields
    defined on the objects are defined below.

    \code
    {
        type // enum value of RayCasterHit.HitType
        entity // entity that was intersected
        distance // distance from ray origin to intersection
        localIntersection.x: // coordinate of intersection in the entity's coordinate system
        localIntersection.y
        localIntersection.z
        worldIntersection.x // coordinate of intersection in the model's coordinate system
        worldIntersection.y
        worldIntersection.z
        primitiveIndex // index of the primitive (triangle, line, point) that was intersected;
                       // (undefined if the picking mode is set to bounding volume)
        vertex1Index // index of the first point of the triangle or line that was intersected
                     // (undefined if the picking mode is set to bounding volume or points)
        vertex2Index // index of the second point of the triangle or line that was intersected
                     // (undefined if the picking mode is set to bounding volume or points)
        vertex3Index // index of the second point of the triangle that was intersected
                     // (undefined if the picking mode is set to bounding volume, points or lines)
    }
    \endcode

    Note that even if successive tests return the exact same results (or empty results), a
    change notification will be emitted at every test.
*/


QAbstractRayCaster::QAbstractRayCaster(Qt3DCore::QNode *parent)
    : Qt3DCore::QComponent(*new QAbstractRayCasterPrivate(), parent)
{
}

/*! \internal */
QAbstractRayCaster::QAbstractRayCaster(QAbstractRayCasterPrivate &dd, Qt3DCore::QNode *parent)
    : Qt3DCore::QComponent(dd, parent)
{
}

/*! \internal */
QAbstractRayCaster::~QAbstractRayCaster()
{
}

QAbstractRayCaster::RunMode QAbstractRayCaster::runMode() const
{
    Q_D(const QAbstractRayCaster);
    return d->m_runMode;
}

void QAbstractRayCaster::setRunMode(QAbstractRayCaster::RunMode runMode)
{
    Q_D(QAbstractRayCaster);
    if (d->m_runMode != runMode) {
        d->m_runMode = runMode;
        emit runModeChanged(d->m_runMode);
    }
}

QAbstractRayCaster::FilterMode QAbstractRayCaster::filterMode() const
{
    Q_D(const QAbstractRayCaster);
    return d->m_filterMode;
}

void QAbstractRayCaster::setFilterMode(QAbstractRayCaster::FilterMode filterMode)
{
    Q_D(QAbstractRayCaster);
    if (d->m_filterMode != filterMode) {
        d->m_filterMode = filterMode;
        emit filterModeChanged(d->m_filterMode);
    }
}

QAbstractRayCaster::Hits QAbstractRayCaster::hits() const
{
    Q_D(const QAbstractRayCaster);
    return d->m_hits;
}

/*!
    Add \a layer to the current list of layers
 */
void QAbstractRayCaster::addLayer(QLayer *layer)
{
    Q_ASSERT(layer);
    Q_D(QAbstractRayCaster);
    if (!d->m_layers.contains(layer)) {
        d->m_layers.append(layer);

        // Ensures proper bookkeeping
        d->registerDestructionHelper(layer, &QAbstractRayCaster::removeLayer, d->m_layers);

        // We need to add it as a child of the current node if it has been declared inline
        // Or not previously added as a child of the current node so that
        // 1) The backend gets notified about it's creation
        // 2) When the current node is destroyed, it gets destroyed as well
        if (!layer->parent())
            layer->setParent(this);

        d->updateNode(layer, "layer", Qt3DCore::PropertyValueAdded);
    }
}

/*!
    Remove \a layer from the current list of layers
 */
void QAbstractRayCaster::removeLayer(QLayer *layer)
{
    Q_ASSERT(layer);
    Q_D(QAbstractRayCaster);
    if (!d->m_layers.removeOne(layer))
        return;
    d->updateNode(layer, "layer", Qt3DCore::PropertyValueRemoved);
    // Remove bookkeeping connection
    d->unregisterDestructionHelper(layer);
}

/*!
    \return the current list of layers
 */
QVector<QLayer *> QAbstractRayCaster::layers() const
{
    Q_D(const QAbstractRayCaster);
    return d->m_layers;
}

/*! \internal */
void QAbstractRayCaster::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change)
{
    Q_D(QAbstractRayCaster);
    Qt3DCore::QPropertyUpdatedChangePtr e = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(change);
    if (e->type() == Qt3DCore::PropertyUpdated) {
        const QByteArray propertyName = e->propertyName();
        if (propertyName == QByteArrayLiteral("hits")) {
            Hits hits = e->value().value<Hits>();
            d->dispatchHits(hits);
        }
    }

    QComponent::sceneChangeEvent(change);
}

/*! \internal */
Qt3DCore::QNodeCreatedChangeBasePtr QAbstractRayCaster::createNodeCreationChange() const
{
    auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QAbstractRayCasterData>::create(this);
    auto &data = creationChange->data;
    Q_D(const QAbstractRayCaster);
    data.casterType = d->m_rayCasterType;
    data.runMode = d->m_runMode;
    data.origin = d->m_origin;
    data.direction = d->m_direction;
    data.length = d->m_length;
    data.position = d->m_position;
    data.filterMode = d->m_filterMode;
    data.layerIds = qIdsForNodes(d->m_layers);
    return creationChange;
}

} // Qt3DRender

QT_END_NAMESPACE