1// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QT3DRENDER_RENDER_SPHERE_H
5#define QT3DRENDER_RENDER_SPHERE_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <Qt3DRender/private/qt3drender_global_p.h>
19#include <Qt3DCore/qnodeid.h>
20#include <Qt3DCore/private/matrix4x4_p.h>
21#include <Qt3DRender/private/boundingsphere_p.h>
22
23QT_BEGIN_NAMESPACE
24
25namespace Qt3DRender {
26
27namespace Render {
28
29class Q_3DRENDERSHARED_PRIVATE_EXPORT Sphere : public RayCasting::BoundingSphere
30{
31public:
32 inline Sphere(Qt3DCore::QNodeId i = Qt3DCore::QNodeId())
33 : m_center()
34 , m_radius(-1.0f)
35 , m_id(i)
36 {}
37
38 inline Sphere(const Vector3D &c, float r, Qt3DCore::QNodeId i = Qt3DCore::QNodeId())
39 : m_center(c)
40 , m_radius(r)
41 , m_id(i)
42 {}
43
44 void setCenter(const Vector3D &c);
45 Vector3D center() const override;
46
47 bool isNull() const { return m_center == Vector3D() && m_radius == -1.0f; }
48
49 void setRadius(float r);
50 float radius() const override;
51
52 void clear();
53 void initializeFromPoints(const QList<Vector3D> &points);
54 void expandToContain(const Vector3D &point);
55 inline void expandToContain(const QList<Vector3D> &points)
56 {
57 for (const Vector3D &p : points)
58 expandToContain(point: p);
59 }
60
61 void expandToContain(const Sphere &sphere);
62
63 Sphere transformed(const Matrix4x4 &mat) const;
64 inline Sphere &transform(const Matrix4x4 &mat)
65 {
66 *this = transformed(mat);
67 return *this;
68 }
69
70 Qt3DCore::QNodeId id() const final;
71 bool intersects(const RayCasting::QRay3D &ray, Vector3D *q, Vector3D *uvw = nullptr) const final;
72 Type type() const final;
73
74 static Sphere fromPoints(const QList<Vector3D> &points);
75
76private:
77 Vector3D m_center;
78 float m_radius;
79 Qt3DCore::QNodeId m_id;
80
81 static const float ms_epsilon;
82};
83
84inline void Sphere::setCenter(const Vector3D &c)
85{
86 m_center = c;
87}
88
89inline Vector3D Sphere::center() const
90{
91 return m_center;
92}
93
94inline void Sphere::setRadius(float r)
95{
96 Q_ASSERT(r >= 0.0f);
97 if (r >= 0.0f)
98 m_radius = r;
99}
100
101inline float Sphere::radius() const
102{
103 return m_radius;
104}
105
106inline void Sphere::clear()
107{
108 m_center = Vector3D();
109 m_radius = -1.0f;
110}
111
112inline bool intersects(const Sphere &a, const Sphere &b)
113{
114 if (a.isNull() || b.isNull())
115 return false;
116
117 // Calculate squared distance between sphere centers
118 const Vector3D d = a.center() - b.center();
119 const float distSq = Vector3D::dotProduct(a: d, b: d);
120
121 // Spheres intersect if squared distance is less than squared
122 // sum of radii
123 const float sumRadii = a.radius() + b.radius();
124 return distSq <= sumRadii * sumRadii;
125}
126
127#ifndef QT_NO_DEBUG_STREAM
128Q_3DRENDERSHARED_EXPORT QDebug operator<<(QDebug dbg, const Sphere &sphere);
129#endif
130
131} // Render
132
133} // Qt3DRender
134
135QT_END_NAMESPACE
136
137Q_DECLARE_METATYPE(Qt3DRender::Render::Sphere) // LCOV_EXCL_LINE
138
139#endif // QT3DRENDER_RENDER_SPHERE_H
140

source code of qt3d/src/render/frontend/sphere_p.h