1/****************************************************************************
2**
3** Copyright (C) 2014 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#ifndef QT3DRENDER_QAXISALIGNEDBOUNDINGBOX_P_H
41#define QT3DRENDER_QAXISALIGNEDBOUNDINGBOX_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of other Qt classes. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QMatrix4x4>
55#include <QVector>
56#include <QVector3D>
57
58#include <Qt3DRender/private/qt3drender_global_p.h>
59
60QT_BEGIN_NAMESPACE
61
62class QDebug;
63
64namespace Qt3DRender {
65
66class QAxisAlignedBoundingBox
67{
68public:
69 inline QAxisAlignedBoundingBox()
70 : m_center(),
71 m_radii()
72 {}
73
74 inline explicit QAxisAlignedBoundingBox(const QVector<QVector3D> &points)
75 : m_center(),
76 m_radii()
77 {
78 update(points);
79 }
80
81 void clear()
82 {
83 m_center = QVector3D();
84 m_radii = QVector3D();
85 }
86
87 bool isNull() const { return m_center.isNull() && m_radii.isNull(); }
88
89 void Q_3DRENDERSHARED_PRIVATE_EXPORT update(const QVector<QVector3D> &points);
90
91 inline QVector3D center() const { return m_center; }
92 inline QVector3D radii() const { return m_radii; }
93
94 inline QVector3D minPoint() const { return m_center - m_radii; }
95 inline QVector3D maxPoint() const { return m_center + m_radii; }
96
97 inline float xExtent() const { return 2.0f * m_radii.x(); }
98 inline float yExtent() const { return 2.0f * m_radii.y(); }
99 inline float zExtent() const { return 2.0f * m_radii.z(); }
100
101 inline float maxExtent() const { return qMax( a: xExtent(), b: qMax( a: yExtent(), b: zExtent() ) ); }
102 inline float minExtent() const { return qMin( a: xExtent(), b: qMin( a: yExtent(), b: zExtent() ) ); }
103
104 inline bool contains( const QVector3D& pt ) const
105 {
106 QVector3D minP(minPoint()), maxP(maxPoint());
107 if ((pt.x() < minP.x()) || (pt.x() > maxP.x()) ||
108 (pt.y() < minP.y()) || (pt.y() > maxP.y()) ||
109 (pt.z() < minP.z()) || (pt.z() > maxP.z()) )
110 {
111 return false;
112 }
113 return true;
114 }
115
116 inline void expandToContain(const QVector3D &pt)
117 {
118 QVector<QVector3D> pts = QVector<QVector3D>() << pt;
119 update(points: pts);
120 }
121
122 inline void expandToContain(const QAxisAlignedBoundingBox &other)
123 {
124 QVector<QVector3D> pts = QVector<QVector3D>() << other.minPoint() << other.maxPoint();
125 update(points: pts);
126 }
127
128 inline QAxisAlignedBoundingBox transformBy(const QMatrix4x4 &mat) const
129 {
130 QAxisAlignedBoundingBox r;
131 r.m_center = mat.map(point: m_center);
132 r.m_radii = mat.map(point: m_radii);
133 return r;
134 }
135
136 inline QAxisAlignedBoundingBox &transform(const QMatrix4x4 &mat)
137 {
138 m_center = mat.map(point: m_center);
139 m_radii = mat.map(point: m_radii);
140 return *this;
141 }
142
143private:
144 QVector3D m_center;
145 QVector3D m_radii;
146
147 friend bool intersects(const QAxisAlignedBoundingBox &a,
148 const QAxisAlignedBoundingBox &b);
149};
150
151QDebug operator<<(QDebug dbg, const QAxisAlignedBoundingBox &c);
152
153inline bool intersects(const QAxisAlignedBoundingBox &a,
154 const QAxisAlignedBoundingBox &b)
155{
156 // Test y axis last as this is often the least discriminatory in OpenGL applications
157 // where worlds tend to be mostly in the xz-plane
158 if (qAbs(t: a.m_center[0] - b.m_center[0]) > a.m_radii[0] + b.m_radii[0])
159 return false;
160 if (qAbs(t: a.m_center[2] - b.m_center[2]) > a.m_radii[2] + b.m_radii[2])
161 return false;
162 if (qAbs(t: a.m_center[1] - b.m_center[1]) > a.m_radii[1] + b.m_radii[1])
163 return false;
164 return true;
165}
166
167} // namespace Qt3DRender
168
169QT_END_NAMESPACE
170
171#endif // QT3DRENDER_QAXISALIGNEDBOUNDINGBOX_P_H
172

source code of qt3d/src/render/io/qaxisalignedboundingbox_p.h