TrinityCore
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 _BIH_WRAP
19#define _BIH_WRAP
20
22#include <G3D/Table.h>
23#include <G3D/Array.h>
24#include <G3D/Set.h>
25
26template<class T, class BoundsFunc = BoundsTrait<T> >
28{
29 template<class RayCallback>
31 {
32 const T* const* objects;
33 RayCallback& _callback;
35
36 MDLCallback(RayCallback& callback, const T* const* objects_array, uint32 objects_size ) : objects(objects_array), _callback(callback), objects_size(objects_size) { }
37
39 bool operator() (const G3D::Ray& ray, uint32 idx, float& maxDist, bool /*stopAtFirst*/)
40 {
41 if (idx >= objects_size)
42 return false;
43 if (const T* obj = objects[idx])
44 return _callback(ray, *obj, maxDist/*, stopAtFirst*/);
45 return false;
46 }
47
49 void operator() (const G3D::Vector3& p, uint32 idx)
50 {
51 if (idx >= objects_size)
52 return;
53 if (const T* obj = objects[idx])
54 _callback(p, *obj);
55 }
56 };
57
58 typedef G3D::Array<const T*> ObjArray;
59
62 G3D::Table<const T*, uint32> m_obj2Idx;
63 G3D::Set<const T*> m_objects_to_push;
65
66public:
68
69 void insert(const T& obj)
70 {
72 m_objects_to_push.insert(&obj);
73 }
74
75 void remove(const T& obj)
76 {
78 uint32 Idx = 0;
79 const T * temp;
80 if (m_obj2Idx.getRemove(&obj, temp, Idx))
81 m_objects[Idx] = nullptr;
82 else
83 m_objects_to_push.remove(&obj);
84 }
85
86 void balance()
87 {
88 if (unbalanced_times == 0)
89 return;
90
92 m_objects.fastClear();
93 m_obj2Idx.getKeys(m_objects);
94 m_objects_to_push.getMembers(m_objects);
95 //assert that m_obj2Idx has all the keys
96
97 m_tree.build(m_objects, BoundsFunc::getBounds2);
98 }
99
100 template<typename RayCallback>
101 void intersectRay(const G3D::Ray& ray, RayCallback& intersectCallback, float& maxDist)
102 {
103 balance();
104 MDLCallback<RayCallback> temp_cb(intersectCallback, m_objects.getCArray(), m_objects.size());
105 m_tree.intersectRay(ray, temp_cb, maxDist, true);
106 }
107
108 template<typename IsectCallback>
109 void intersectPoint(const G3D::Vector3& point, IsectCallback& intersectCallback)
110 {
111 balance();
112 MDLCallback<IsectCallback> callback(intersectCallback, m_objects.getCArray(), m_objects.size());
113 m_tree.intersectPoint(point, callback);
114 }
115};
116
117#endif // _BIH_WRAP
uint32_t uint32
Definition: Define.h:142
G3D::Table< const T *, uint32 > m_obj2Idx
G3D::Array< const T * > ObjArray
void insert(const T &obj)
G3D::Set< const T * > m_objects_to_push
void remove(const T &obj)
void intersectRay(const G3D::Ray &ray, RayCallback &intersectCallback, float &maxDist)
void intersectPoint(const G3D::Vector3 &point, IsectCallback &intersectCallback)
void build(PrimArray const &primitives, BoundsFunc &getBounds, uint32 leafSize=3, bool printStats=false)
void intersectPoint(const G3D::Vector3 &p, IsectCallback &intersectCallback) const
void intersectRay(const G3D::Ray &r, RayCallback &intersectCallback, float &maxDist, bool stopAtFirst=false) const
MDLCallback(RayCallback &callback, const T *const *objects_array, uint32 objects_size)
bool operator()(const G3D::Ray &ray, uint32 idx, float &maxDist, bool)
Intersect ray.