aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3d/qquick3dpickresult.cpp
blob: 99c2147fe896010c7d2cae3db4c8a12515c2597c (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qquick3dpickresult_p.h"
#include "qquick3dmodel_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmlvaluetype pickResult
    \inqmlmodule QtQuick3D
    \brief Contains the results of a pick.

    Created as a return object to View3D::pick.
*/

QQuick3DPickResult::QQuick3DPickResult()
    : m_objectHit(nullptr)
    , m_distance(0.0f)
    , m_instanceIndex(-1)
    , m_itemHit(nullptr)
    , m_hitType(QQuick3DPickResultEnums::HitType::Null)
{

}

QQuick3DPickResult::QQuick3DPickResult(QQuick3DModel *hitObject,
                                       float distanceFromCamera,
                                       const QVector2D &uvPosition,
                                       const QVector3D &scenePosition,
                                       const QVector3D &position,
                                       const QVector3D &normal,
                                       int instanceIndex)
    : m_objectHit(hitObject)
    , m_distance(distanceFromCamera)
    , m_uvPosition(uvPosition)
    , m_scenePosition(scenePosition)
    , m_position(position)
    , m_normal(normal)
    , m_instanceIndex(instanceIndex)
    , m_itemHit(nullptr)
    , m_hitType(QQuick3DPickResultEnums::HitType::Model)
{
}

// NB: we are intentionally storing the sceneNormal in the "m_normal" member variable
// as 2D Items always have the same face normal, but we can't calculate the scene normal
// on demand either. This logic should be handled in the respective getters.
QQuick3DPickResult::QQuick3DPickResult(QQuickItem *itemHit,
                                       float distanceFromCamera,
                                       const QVector2D &uvPosition,
                                       const QVector3D &scenePosition,
                                       const QVector3D &position,
                                       const QVector3D &sceneNormal)
    : m_objectHit(nullptr)
    , m_distance(distanceFromCamera)
    , m_uvPosition(uvPosition)
    , m_scenePosition(scenePosition)
    , m_position(position)
    , m_normal(sceneNormal)
    , m_instanceIndex(-1)
    , m_itemHit(itemHit)
    , m_hitType(QQuick3DPickResultEnums::HitType::Item)
{

}

/*!
    \qmlproperty Model pickResult::objectHit
    \readonly

    This property holds the model object hit by the pick. This value will be null if
    \l{pickResult::hitType} {hitType} is not \c pickResult.Model.

    \sa itemHit
*/
QQuick3DModel *QQuick3DPickResult::objectHit() const
{
    return m_objectHit;
}

/*!
    \qmlproperty real pickResult::distance
    \readonly

    This property holds the distance between the pick origin and the hit position
    i.e. the length of the ray. In the case of using viewport coordinates for
    picking the pick origin will be the active camera's position.
*/
float QQuick3DPickResult::distance() const
{
    return m_distance;
}

/*!
    \qmlproperty vector2d pickResult::uvPosition
    \readonly

    This property holds the UV position of the hit. The UV position is calculated as
    the normalized local x and y coordinates of the hit point relative to the bounding volume.
    Useful for further picking against an offscreen-rendered object.

    When \l{pickResult::}{hitType} is \c pickResult.Item this value will represent the position
    of the hit in the coordinate space of \l{pickResult::}{itemHit}.
*/
QVector2D QQuick3DPickResult::uvPosition() const
{
    return m_uvPosition;
}

/*!
    \qmlproperty vector3d pickResult::scenePosition
    \readonly

    This property holds the scene position of the hit.
*/
QVector3D QQuick3DPickResult::scenePosition() const
{
    return m_scenePosition;
}

/*!
    \qmlproperty vector3d pickResult::position
    \readonly

    This property holds the scene position of the hit in local coordinate
    space.
*/
QVector3D QQuick3DPickResult::position() const
{
    return m_position;
}

/*!
    \qmlproperty vector3d pickResult::normal
    \readonly

    This property holds the normal of the face that was hit in local coordinate
    space.

    \note for 2D Items this will always be (0, 0, 1).
*/
QVector3D QQuick3DPickResult::normal() const
{
    if (m_itemHit)
        return QVector3D(0, 0, 1);

    return m_normal;
}


/*!
    \qmlproperty vector3d pickResult::sceneNormal
    \readonly

    This property holds the normal of the face that was hit in scene coordinate
    space.
*/
QVector3D QQuick3DPickResult::sceneNormal() const
{
    if (m_objectHit)
        return m_objectHit->mapDirectionToScene(m_normal);

    return m_normal;
}


/*!
    \qmlproperty int pickResult::instanceIndex
    \readonly
    \since 6.5

    This property holds the index in the instance table for the case
    where the pick hit an instance of an instanced model.
*/
int QQuick3DPickResult::instanceIndex() const
{
    return m_instanceIndex;
}

/*!
    \qmlproperty Item pickResult::itemHit
    \readonly
    \since 6.8

    This property holds the Qt Quick Item hit by the pick. This value will be null if
    \l{pickResult::}{hitType} is not \c pickResult.Item.

    \sa objectHit
*/

QQuickItem *QQuick3DPickResult::itemHit() const
{
    return m_itemHit;
}

/*!
    \qmlproperty enumeration pickResult::hitType
    \readonly
    \since 6.8

    This property holds the hit type of the pick result.

    \value PickResult.Null The pick did not hit anything.
    \value PickResult.Model The pick hit a Model.
    \value PickResult.Item The pick hit a QQuickItem.
*/

QQuick3DPickResultEnums::HitType QQuick3DPickResult::hitType() const
{
    return m_hitType;
}

QT_END_NAMESPACE