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