1 | // Copyright (C) 2008-2012 NVIDIA Corporation. |
2 | // Copyright (C) 2019 The Qt Company Ltd. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
4 | |
5 | #ifndef QSSGPLANE_H |
6 | #define QSSGPLANE_H |
7 | |
8 | // |
9 | // W A R N I N G |
10 | // ------------- |
11 | // |
12 | // This file is not part of the Qt API. It exists purely as an |
13 | // implementation detail. This header file may change from version to |
14 | // version without notice, or even be removed. |
15 | // |
16 | // We mean it. |
17 | // |
18 | |
19 | #include <QtQuick3DUtils/private/qtquick3dutilsglobal_p.h> |
20 | #include <QtGui/QVector3D> |
21 | #include <QtCore/qmath.h> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | /** |
26 | \brief Representation of a plane. |
27 | |
28 | Plane equation used: n.dot(v) + d = 0 |
29 | */ |
30 | class Q_QUICK3DUTILS_EXPORT QSSGPlane |
31 | { |
32 | public: |
33 | QSSGPlane() = default; |
34 | |
35 | /** |
36 | \brief Constructor from a normal and a distance |
37 | */ |
38 | Q_ALWAYS_INLINE QSSGPlane(float nx, float ny, float nz, float distance) : n(nx, ny, nz), d(distance) {} |
39 | |
40 | /** |
41 | \brief Constructor from a normal and a distance |
42 | */ |
43 | Q_ALWAYS_INLINE QSSGPlane(const QVector3D &normal, float distance) : n(normal), d(distance) {} |
44 | |
45 | /** |
46 | \brief Constructor from a point on the plane and a normal |
47 | */ |
48 | Q_ALWAYS_INLINE QSSGPlane(const QVector3D &point, const QVector3D &normal) |
49 | : n(normal), d(-QVector3D::dotProduct(v1: point, v2: n)) // p satisfies normal.dot(p) + d = 0 |
50 | { |
51 | } |
52 | |
53 | /** |
54 | \brief Constructor from three points |
55 | */ |
56 | Q_ALWAYS_INLINE QSSGPlane(const QVector3D &p0, const QVector3D &p1, const QVector3D &p2) |
57 | { |
58 | n = QVector3D::crossProduct(v1: p1 - p0, v2: p2 - p0).normalized(); |
59 | d = QVector3D::dotProduct(v1: -p0, v2: n); |
60 | } |
61 | |
62 | Q_ALWAYS_INLINE float distance(const QVector3D &p) const { return QVector3D::dotProduct(v1: p, v2: n) + d; } |
63 | |
64 | Q_ALWAYS_INLINE bool contains(const QVector3D &p) const { return qAbs(t: distance(p)) < (1.0e-7f); } |
65 | |
66 | /** |
67 | \brief projects p into the plane |
68 | */ |
69 | Q_ALWAYS_INLINE QVector3D project(const QVector3D &p) const { return p - n * distance(p); } |
70 | |
71 | /** |
72 | \brief find an arbitrary point in the plane |
73 | */ |
74 | Q_ALWAYS_INLINE QVector3D pointInPlane() const { return -n * d; } |
75 | |
76 | /** |
77 | \brief equivalent plane with unit normal |
78 | */ |
79 | |
80 | void normalize(); |
81 | |
82 | QVector3D n; //!< The normal to the plane |
83 | float d = 0.0f; //!< The distance from the origin |
84 | }; |
85 | |
86 | QT_END_NAMESPACE |
87 | |
88 | #endif // QSSGPLANE_H |
89 | |