| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QSSGMESHBVH_H |
| 5 | #define QSSGMESHBVH_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 purely as an |
| 12 | // implementation detail. 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 <QtQuick3DUtils/private/qtquick3dutilsglobal_p.h> |
| 19 | #include <QtQuick3DUtils/private/qssgbounds3_p.h> |
| 20 | #include <QtQuick3DUtils/private/qssgassert_p.h> |
| 21 | |
| 22 | #include <QtGui/QVector2D> |
| 23 | #include <QtCore/QVector> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QSSGMeshBVH; |
| 28 | |
| 29 | class Q_QUICK3DUTILS_EXPORT QSSGMeshBVHNode |
| 30 | { |
| 31 | public: |
| 32 | class Handle |
| 33 | { |
| 34 | public: |
| 35 | Handle() = default; |
| 36 | bool isNull() const { return !m_owner || m_idx < size_t(FallbackIndex::Count); } |
| 37 | |
| 38 | inline explicit operator const QSSGMeshBVHNode *() const; |
| 39 | inline QSSGMeshBVHNode *operator->() const; |
| 40 | private: |
| 41 | friend class QSSGMeshBVH; |
| 42 | Handle(QSSGMeshBVH *owner, size_t idx) |
| 43 | : m_owner(owner) |
| 44 | , m_idx(idx) |
| 45 | {} |
| 46 | QSSGMeshBVH *m_owner = nullptr; |
| 47 | size_t m_idx = 0; |
| 48 | }; |
| 49 | |
| 50 | // Internal |
| 51 | Handle left; |
| 52 | Handle right; |
| 53 | QSSGBounds3 boundingData; |
| 54 | //splitAxis |
| 55 | |
| 56 | // Leaf |
| 57 | int offset = 0; |
| 58 | int count = 0; |
| 59 | |
| 60 | private: |
| 61 | friend class QSSGMeshBVH; |
| 62 | friend class QSSGMeshBVHBuilder; |
| 63 | |
| 64 | enum class FallbackIndex : quint8 |
| 65 | { |
| 66 | InvalidRead = 0, |
| 67 | InvalidWrite = 1, |
| 68 | Count |
| 69 | }; |
| 70 | }; |
| 71 | |
| 72 | struct Q_QUICK3DUTILS_EXPORT QSSGMeshBVHTriangle |
| 73 | { |
| 74 | QSSGBounds3 bounds; |
| 75 | QVector3D vertex1; |
| 76 | QVector3D vertex2; |
| 77 | QVector3D vertex3; |
| 78 | QVector2D uvCoord1; |
| 79 | QVector2D uvCoord2; |
| 80 | QVector2D uvCoord3; |
| 81 | }; |
| 82 | |
| 83 | using QSSGMeshBVHTriangles = std::vector<QSSGMeshBVHTriangle>; |
| 84 | using QSSGMeshBVHRoots = std::vector<QSSGMeshBVHNode::Handle>; |
| 85 | using QSSGMeshBVHNodes = std::vector<QSSGMeshBVHNode>; |
| 86 | |
| 87 | class Q_QUICK3DUTILS_EXPORT QSSGMeshBVH |
| 88 | { |
| 89 | public: |
| 90 | QSSGMeshBVH() = default; |
| 91 | ~QSSGMeshBVH(); |
| 92 | |
| 93 | [[nodiscard]] QSSGMeshBVHNode::Handle newHandle() |
| 94 | { |
| 95 | m_nodes.emplace_back(); |
| 96 | return { this, m_nodes.size() - 1 }; |
| 97 | } |
| 98 | |
| 99 | [[nodiscard]] const QSSGMeshBVHTriangles &triangles() const { return m_triangles; } |
| 100 | [[nodiscard]] const QSSGMeshBVHRoots &roots() const { return m_roots; } |
| 101 | [[nodiscard]] const QSSGMeshBVHNodes &nodes() const { return m_nodes; } |
| 102 | |
| 103 | private: |
| 104 | friend class QSSGMeshBVHNode::Handle; |
| 105 | friend class QSSGMeshBVHBuilder; |
| 106 | using FallbackIndex = QSSGMeshBVHNode::FallbackIndex; |
| 107 | size_t getNodeIndex(size_t idx, FallbackIndex op) const |
| 108 | { |
| 109 | const bool valid = (idx >= size_t(FallbackIndex::Count) && idx < m_nodes.size()); |
| 110 | return (valid * idx) + (!valid * size_t(op)); |
| 111 | } |
| 112 | |
| 113 | QSSGMeshBVHNode &mutableValue(qsizetype idx) |
| 114 | { |
| 115 | return m_nodes[getNodeIndex(idx, op: FallbackIndex::InvalidWrite)]; |
| 116 | } |
| 117 | |
| 118 | const QSSGMeshBVHNode &value(qsizetype idx) const |
| 119 | { |
| 120 | return m_nodes[getNodeIndex(idx, op: FallbackIndex::InvalidRead)]; |
| 121 | } |
| 122 | |
| 123 | QSSGMeshBVHRoots m_roots; |
| 124 | QSSGMeshBVHNodes m_nodes { { /* 0 - reserved for invalid reads */ }, { /* 1 - reserved for invalid writes */ } }; |
| 125 | QSSGMeshBVHTriangles m_triangles; |
| 126 | }; |
| 127 | |
| 128 | QSSGMeshBVHNode::Handle::operator const QSSGMeshBVHNode *() const |
| 129 | { |
| 130 | return &m_owner->value(idx: m_idx); |
| 131 | } |
| 132 | |
| 133 | QSSGMeshBVHNode *QSSGMeshBVHNode::Handle::operator->() const |
| 134 | { |
| 135 | return &m_owner->mutableValue(idx: m_idx); |
| 136 | } |
| 137 | |
| 138 | QT_END_NAMESPACE |
| 139 | |
| 140 | #endif // QSSGMESHBVH_H |
| 141 | |