aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/qssgbounds3_p.h
blob: 75773ec0aa02153a87466c100204b215cfeaaae0 (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
// Copyright (C) 2008-2012 NVIDIA Corporation.
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QSSGBOUNDS3_H
#define QSSGBOUNDS3_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qssgutils_p.h"
#include <QtQuick3DUtils/private/qtquick3dutilsglobal_p.h>

#include <QVector3D>
#include <QMatrix3x3>
#include <QMatrix4x4>

#include <limits>
#include <qnumeric.h>

QT_BEGIN_NAMESPACE

using QSSGBoxPoints = std::array<QVector3D, 8>;

/**
\brief Class representing 3D range or axis aligned bounding box.

Stored as minimum and maximum extent corners. Alternate representation
would be center and dimensions.
May be empty or nonempty. If not empty, minimum <= maximum has to hold.
*/
class Q_QUICK3DUTILS_EXPORT QSSGBounds3
{
public:
    /**
    \brief Default constructor, using empty bounds.
    */
    Q_DECL_CONSTEXPR Q_ALWAYS_INLINE QSSGBounds3();

    /**
    \brief Construct uninitialized.
    */
    Q_ALWAYS_INLINE QSSGBounds3(Qt::Initialization);

    /**
    \brief Construct from two bounding points
    */
    Q_DECL_CONSTEXPR Q_ALWAYS_INLINE QSSGBounds3(const QVector3D &minimum, const QVector3D &maximum);

    /**
    \brief returns the AABB containing v0 and v1.
    \param v0 first point included in the AABB.
    \param v1 second point included in the AABB.
    */
    static Q_ALWAYS_INLINE QSSGBounds3 boundsOfPoints(const QVector3D &v0, const QVector3D &v1);

    /**
    \brief returns the AABB from center and extents vectors.
    \param center Center vector
    \param extent Extents vector
    */
    static Q_ALWAYS_INLINE QSSGBounds3 centerExtents(const QVector3D &center, const QVector3D &extent);

    /**
    \brief Construct from center, extent, and (not necessarily orthogonal) basis
    */
    static Q_ALWAYS_INLINE QSSGBounds3 basisExtent(const QVector3D &center, const QMatrix3x3 &basis, const QVector3D &extent);

    /**
    \brief gets the transformed bounds of the passed AABB (resulting in a bigger AABB).
    \param[in] matrix Transform to apply, can contain scaling as well
    \param[in] bounds The bounds to transform.
    */
    static QSSGBounds3 transform(const QMatrix3x3 &matrix, const QSSGBounds3 &bounds);

    /**
    \brief Sets empty to true
    */
    Q_ALWAYS_INLINE void setEmpty();

    /**
    \brief Sets infinite bounds
    */
    Q_ALWAYS_INLINE void setInfinite();

    /**
    \brief expands the volume to include v
    \param v Point to expand to.
    */
    void include(const QVector3D &v);

    /**
    \brief expands the volume to include b.
    \param b Bounds to perform union with.
    */
    void include(const QSSGBounds3 &b);

    Q_ALWAYS_INLINE bool isEmpty() const;

    /**
    \brief indicates whether the intersection of this and b is empty or not.
    \param b Bounds to test for intersection.
    */
    Q_ALWAYS_INLINE bool intersects(const QSSGBounds3 &b) const;

    /**
     \brief computes the 1D-intersection between two AABBs, on a given axis.
     \param	a		the other AABB
     \param	axis	the axis (0, 1, 2)
     */
    Q_ALWAYS_INLINE bool intersects1D(const QSSGBounds3 &a, quint32 axis) const;

    /**
    \brief indicates if these bounds contain v.
    \param v Point to test against bounds.
    */
    Q_ALWAYS_INLINE bool contains(const QVector3D &v) const;

    /**
     \brief	checks a box is inside another box.
     \param	box		the other AABB
     */
    Q_ALWAYS_INLINE bool isInside(const QSSGBounds3 &box) const;

    /**
    \brief returns the center of this axis aligned box.
    */
    Q_ALWAYS_INLINE QVector3D center() const;

    /**
    \brief get component of the box's center along a given axis
    */
    Q_ALWAYS_INLINE float center(quint32 axis) const;

    /**
    \brief get component of the box's extents along a given axis
    */
    Q_ALWAYS_INLINE float extents(quint32 axis) const;

    /**
    \brief returns the dimensions (width/height/depth) of this axis aligned box.
    */
    Q_ALWAYS_INLINE QVector3D dimensions() const;

    /**
    \brief returns the extents, which are half of the width/height/depth.
    */
    Q_ALWAYS_INLINE QVector3D extents() const;

    /**
    \brief scales the AABB.
    \param scale Factor to scale AABB by.
    */
    Q_ALWAYS_INLINE void scale(float scale);

    /**
    fattens the AABB in all 3 dimensions by the given distance.
    */
    Q_ALWAYS_INLINE void fatten(double distance);

    /**
    checks that the AABB values are not NaN
    */

    bool isFinite() const;

    /**
    Use when the bounds is already verified to be non-empty!!!
    */
    Q_ALWAYS_INLINE QSSGBoxPoints toQSSGBoxPointsNoEmptyCheck() const;
    /**
    Verifies that the bounds is non-empty.
    */
    Q_ALWAYS_INLINE QSSGBoxPoints toQSSGBoxPoints() const;

    void transform(const QMatrix4x4 &inMatrix);

    /**
    Returns the support point in a given direction
    */
    QVector3D getSupport(const QVector3D &direction) const;

    QVector3D minimum;
    QVector3D maximum;
};

Q_DECL_CONSTEXPR Q_ALWAYS_INLINE QSSGBounds3::QSSGBounds3()
    : minimum(QVector3D(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max()))
    , maximum(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(), -std::numeric_limits<float>::max())
{
}

Q_ALWAYS_INLINE QSSGBounds3::QSSGBounds3(Qt::Initialization)
    : minimum(Qt::Uninitialized), maximum(Qt::Uninitialized) { }

Q_DECL_CONSTEXPR Q_ALWAYS_INLINE QSSGBounds3::QSSGBounds3(const QVector3D &_minimum, const QVector3D &_maximum)
    : minimum(_minimum), maximum(_maximum)
{
}

Q_ALWAYS_INLINE QSSGBounds3 QSSGBounds3::centerExtents(const QVector3D &center, const QVector3D &extent)
{
    return QSSGBounds3(center - extent, center + extent);
}

Q_ALWAYS_INLINE void QSSGBounds3::setEmpty()
{
    constexpr float maxFloat = std::numeric_limits<float>::max();
    minimum = QVector3D(maxFloat, maxFloat, maxFloat);
    maximum = QVector3D(-maxFloat, -maxFloat, -maxFloat);
}

Q_ALWAYS_INLINE void QSSGBounds3::setInfinite()
{
    constexpr float maxFloat = std::numeric_limits<float>::max();
    minimum = QVector3D(-maxFloat, -maxFloat, -maxFloat);
    maximum = QVector3D(maxFloat, maxFloat, maxFloat);
}

Q_ALWAYS_INLINE bool QSSGBounds3::isEmpty() const
{
    Q_ASSERT(isFinite());
    // Consistency condition for (Min, Max) boxes: minimum < maximum
    return minimum.x() > maximum.x() || minimum.y() > maximum.y() || minimum.z() > maximum.z();
}

Q_ALWAYS_INLINE bool QSSGBounds3::intersects(const QSSGBounds3 &b) const
{
    Q_ASSERT(isFinite() && b.isFinite());
    return !(b.minimum.x() > maximum.x() || minimum.x() > b.maximum.x() || b.minimum.y() > maximum.y()
             || minimum.y() > b.maximum.y() || b.minimum.z() > maximum.z() || minimum.z() > b.maximum.z());
}

Q_ALWAYS_INLINE bool QSSGBounds3::intersects1D(const QSSGBounds3 &a, quint32 axis) const
{
    Q_ASSERT(isFinite() && a.isFinite());
    return maximum[int(axis)] >= a.minimum[axis] && a.maximum[axis] >= minimum[axis];
}

Q_ALWAYS_INLINE bool QSSGBounds3::contains(const QVector3D &v) const
{
    Q_ASSERT(isFinite());

    return !(v.x() < minimum.x() || v.x() > maximum.x() || v.y() < minimum.y() || v.y() > maximum.y()
             || v.z() < minimum.z() || v.z() > maximum.z());
}

Q_ALWAYS_INLINE bool QSSGBounds3::isInside(const QSSGBounds3 &box) const
{
    Q_ASSERT(isFinite() && box.isFinite());
    if (box.minimum.x() > minimum.x())
        return false;
    if (box.minimum.y() > minimum.y())
        return false;
    if (box.minimum.z() > minimum.z())
        return false;
    if (box.maximum.x() < maximum.x())
        return false;
    if (box.maximum.y() < maximum.y())
        return false;
    if (box.maximum.z() < maximum.z())
        return false;
    return true;
}

Q_ALWAYS_INLINE QVector3D QSSGBounds3::center() const
{
    Q_ASSERT(isFinite());
    return (minimum + maximum) * double(0.5);
}

Q_ALWAYS_INLINE float QSSGBounds3::center(quint32 axis) const
{
    Q_ASSERT(isFinite());
    return (minimum[int(axis)] + maximum[int(axis)]) * 0.5f;
}

Q_ALWAYS_INLINE float QSSGBounds3::extents(quint32 axis) const
{
    Q_ASSERT(isFinite());
    return (maximum[int(axis)] - minimum[int(axis)]) * 0.5f;
}

Q_ALWAYS_INLINE QVector3D QSSGBounds3::dimensions() const
{
    Q_ASSERT(isFinite());
    return maximum - minimum;
}

Q_ALWAYS_INLINE QVector3D QSSGBounds3::extents() const
{
    Q_ASSERT(isFinite());
    return dimensions() * double(0.5);
}

Q_ALWAYS_INLINE void QSSGBounds3::scale(float scale)
{
    Q_ASSERT(isFinite());
    *this = centerExtents(center(), extents() * scale);
}

Q_ALWAYS_INLINE void QSSGBounds3::fatten(double distance)
{
    Q_ASSERT(isFinite());
    minimum -= QVector3D(float(distance), float(distance), float(distance));
    maximum += QVector3D(float(distance), float(distance), float(distance));
}

Q_ALWAYS_INLINE QSSGBoxPoints QSSGBounds3::toQSSGBoxPointsNoEmptyCheck() const
{
    return { // Min corner of box
             QVector3D(minimum[0], minimum[1], minimum[2]),
             QVector3D(maximum[0], minimum[1], minimum[2]),
             QVector3D(maximum[0], maximum[1], minimum[2]),
             QVector3D(minimum[0], maximum[1], minimum[2]),
             // Max corner of box
             QVector3D(minimum[0], minimum[1], maximum[2]),
             QVector3D(maximum[0], minimum[1], maximum[2]),
             QVector3D(maximum[0], maximum[1], maximum[2]),
             QVector3D(minimum[0], maximum[1], maximum[2])
    };
}

Q_ALWAYS_INLINE QSSGBoxPoints QSSGBounds3::toQSSGBoxPoints() const
{
    if (isEmpty())
        return { QVector3D(0, 0, 0), QVector3D(0, 0, 0), QVector3D(0, 0, 0), QVector3D(0, 0, 0),
                 QVector3D(0, 0, 0), QVector3D(0, 0, 0), QVector3D(0, 0, 0), QVector3D(0, 0, 0) };
    return toQSSGBoxPointsNoEmptyCheck();
}

QT_END_NAMESPACE

#endif // QSSGBOUNDS3_H