TrinityCore
Loading...
Searching...
No Matches
BoundingIntervalHierarchyWrapper.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_BOUNDING_INTERVAL_HIERARCHY_WRAPPER_H
19#define TRINITYCORE_BOUNDING_INTERVAL_HIERARCHY_WRAPPER_H
20
22#include <span>
23#include <unordered_map>
24
25template<class T, class BoundsFunc = BoundsTrait<T> >
27{
28 template<class RayCallback>
30 {
31 std::size_t objects_size;
32 T const* const* objects;
33 RayCallback& _callback;
34
35 MDLCallback(RayCallback& callback, T const* const* objects_array, std::size_t objects_size ) : objects_size(objects_size), objects(objects_array), _callback(callback) { }
36
38 bool operator() (G3D::Ray const& ray, std::size_t idx, float& maxDist, bool /*stopAtFirst*/)
39 {
40 if (idx >= objects_size)
41 return false;
42 if (T const* obj = objects[idx])
43 return _callback(ray, *obj, maxDist/*, stopAtFirst*/);
44 return false;
45 }
46
48 void operator() (G3D::Vector3 const& p, std::size_t idx)
49 {
50 if (idx >= objects_size)
51 return;
52 if (T const* obj = objects[idx])
53 _callback(p, *obj);
54 }
55 };
56
58 std::vector<T const*> m_objects;
59 std::unordered_map<T const*, std::size_t> m_obj2Idx;
61
62public:
64
65 void insert(T const& obj)
66 {
67 auto [itr, isNew] = m_obj2Idx.try_emplace(&obj, m_objects.size());
68 if (!isNew)
69 return;
70
71 m_objects.push_back(itr->first);
73 }
74
75 void remove(T const& obj)
76 {
77 auto node = m_obj2Idx.extract(&obj);
78 if (!node)
79 return;
80
81 if (node.key() != m_objects.back())
82 {
83 // update index of last element (will be swapped with removed one)
84 m_obj2Idx.find(m_objects.back())->second = node.mapped();
85
86 // move last into removed element slot
87 m_objects[node.mapped()] = m_objects.back();
88 }
89
90 m_objects.pop_back();
91
93 }
94
95 void balance()
96 {
97 if (unbalanced_times == 0)
98 return;
99
100 m_tree.build(m_objects, BoundsFunc());
102 }
103
104 template<typename RayCallback>
105 void intersectRay(G3D::Ray const& ray, RayCallback& intersectCallback, float& maxDist)
106 {
107 balance();
108 MDLCallback<RayCallback> temp_cb(intersectCallback, m_objects.data(), m_objects.size());
109 m_tree.intersectRay(ray, temp_cb, maxDist, true);
110 }
111
112 template<typename IsectCallback>
113 void intersectPoint(G3D::Vector3 const& point, IsectCallback& intersectCallback)
114 {
115 balance();
116 MDLCallback<IsectCallback> callback(intersectCallback, m_objects.data(), m_objects.size());
117 m_tree.intersectPoint(point, callback);
118 }
119
120 std::span<T const* const> getObjects() const { return m_objects; }
121};
122
123#endif // TRINITYCORE_BOUNDING_INTERVAL_HIERARCHY_WRAPPER_H
void intersectRay(G3D::Ray const &ray, RayCallback &intersectCallback, float &maxDist)
std::span< T const *const > getObjects() const
void intersectPoint(G3D::Vector3 const &point, IsectCallback &intersectCallback)
std::vector< T const * > m_objects
void remove(T const &obj)
std::unordered_map< T const *, std::size_t > m_obj2Idx
void insert(T const &obj)
void build(PrimArray const &primitives, BoundsFunc const &getBounds, uint32 leafSize=3, bool printStats=false)
void intersectPoint(G3D::Vector3 const &p, IsectCallback &intersectCallback) const
void intersectRay(G3D::Ray const &r, RayCallback &intersectCallback, float &maxDist, bool stopAtFirst=false) const
MDLCallback(RayCallback &callback, T const *const *objects_array, std::size_t objects_size)
bool operator()(G3D::Ray const &ray, std::size_t idx, float &maxDist, bool)
Intersect ray.