TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BoundingIntervalHierarchyWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _BIH_WRAP
20 #define _BIH_WRAP
21 
22 #include "G3D/Table.h"
23 #include "G3D/Array.h"
24 #include "G3D/Set.h"
26 
27 
28 template<class T, class BoundsFunc = BoundsTrait<T> >
29 class BIHWrap
30 {
31  template<class RayCallback>
32  struct MDLCallback
33  {
34  const T* const* objects;
35  RayCallback& _callback;
37 
38  MDLCallback(RayCallback& callback, const T* const* objects_array, uint32 objects_size ) : objects(objects_array), _callback(callback), objects_size(objects_size) { }
39 
41  bool operator() (const G3D::Ray& ray, uint32 idx, float& maxDist, bool /*stopAtFirst*/)
42  {
43  if (idx >= objects_size)
44  return false;
45  if (const T* obj = objects[idx])
46  return _callback(ray, *obj, maxDist/*, stopAtFirst*/);
47  return false;
48  }
49 
51  void operator() (const G3D::Vector3& p, uint32 idx)
52  {
53  if (idx >= objects_size)
54  return;
55  if (const T* obj = objects[idx])
56  _callback(p, *obj);
57  }
58  };
59 
61 
63  ObjArray m_objects;
67 
68 public:
69  BIHWrap() : unbalanced_times(0) { }
70 
71  void insert(const T& obj)
72  {
74  m_objects_to_push.insert(&obj);
75  }
76 
77  void remove(const T& obj)
78  {
80  uint32 Idx = 0;
81  const T * temp;
82  if (m_obj2Idx.getRemove(&obj, temp, Idx))
83  m_objects[Idx] = NULL;
84  else
85  m_objects_to_push.remove(&obj);
86  }
87 
88  void balance()
89  {
90  if (unbalanced_times == 0)
91  return;
92 
93  unbalanced_times = 0;
94  m_objects.fastClear();
95  m_obj2Idx.getKeys(m_objects);
96  m_objects_to_push.getMembers(m_objects);
97  //assert that m_obj2Idx has all the keys
98 
99  m_tree.build(m_objects, BoundsFunc::getBounds2);
100  }
101 
102  template<typename RayCallback>
103  void intersectRay(const G3D::Ray& ray, RayCallback& intersectCallback, float& maxDist)
104  {
105  balance();
106  MDLCallback<RayCallback> temp_cb(intersectCallback, m_objects.getCArray(), m_objects.size());
107  m_tree.intersectRay(ray, temp_cb, maxDist, true);
108  }
109 
110  template<typename IsectCallback>
111  void intersectPoint(const G3D::Vector3& point, IsectCallback& intersectCallback)
112  {
113  balance();
114  MDLCallback<IsectCallback> callback(intersectCallback, m_objects.getCArray(), m_objects.size());
115  m_tree.intersectPoint(point, callback);
116  }
117 };
118 
119 #endif // _BIH_WRAP
Definition: BoundingIntervalHierarchyWrapper.h:32
ObjArray m_objects
Definition: BoundingIntervalHierarchyWrapper.h:63
bool remove(const T &member)
Definition: Set.h:72
void intersectPoint(const G3D::Vector3 &point, IsectCallback &intersectCallback)
Definition: BoundingIntervalHierarchyWrapper.h:111
void fastClear()
Definition: Array.h:419
bool getRemove(const Key &key, Key &removedKey, Value &removedValue)
Definition: Table.h:660
void intersectRay(const G3D::Ray &ray, RayCallback &intersectCallback, float &maxDist)
Definition: BoundingIntervalHierarchyWrapper.h:103
int unbalanced_times
Definition: BoundingIntervalHierarchyWrapper.h:66
T * getCArray()
Definition: Array.h:256
const T *const * objects
Definition: BoundingIntervalHierarchyWrapper.h:34
arena_t NULL
Definition: jemalloc_internal.h:624
void insert(const T &obj)
Definition: BoundingIntervalHierarchyWrapper.h:71
G3D::Set< const T * > m_objects_to_push
Definition: BoundingIntervalHierarchyWrapper.h:65
Definition: BoundingIntervalHierarchyWrapper.h:29
Definition: Vector3.h:58
BIH m_tree
Definition: BoundingIntervalHierarchyWrapper.h:62
RayCallback & _callback
Definition: BoundingIntervalHierarchyWrapper.h:35
bool insert(const T &member)
Definition: Set.h:62
uint32_t uint32
Definition: Define.h:150
bool operator()(const G3D::Ray &ray, uint32 idx, float &maxDist, bool)
Intersect ray.
Definition: BoundingIntervalHierarchyWrapper.h:41
uint32 objects_size
Definition: BoundingIntervalHierarchyWrapper.h:36
int size() const
Definition: Array.h:430
Definition: Ray.h:24
G3D::Table< const T *, uint32 > m_obj2Idx
Definition: BoundingIntervalHierarchyWrapper.h:64
Array< Key > getKeys() const
Definition: Table.h:907
Array< T > getMembers() const
Definition: Set.h:93
void intersectPoint(const G3D::Vector3 &p, IsectCallback &intersectCallback) const
Definition: BoundingIntervalHierarchy.h:260
BIHWrap()
Definition: BoundingIntervalHierarchyWrapper.h:69
void build(const PrimArray &primitives, BoundsFunc &getBounds, uint32 leafSize=3, bool printStats=false)
Definition: BoundingIntervalHierarchy.h:84
MDLCallback(RayCallback &callback, const T *const *objects_array, uint32 objects_size)
Definition: BoundingIntervalHierarchyWrapper.h:38
Definition: BoundingIntervalHierarchy.h:70
void balance()
Definition: BoundingIntervalHierarchyWrapper.h:88
void intersectRay(const G3D::Ray &r, RayCallback &intersectCallback, float &maxDist, bool stopAtFirst=false) const
Definition: BoundingIntervalHierarchy.h:121
G3D::Array< const T * > ObjArray
Definition: BoundingIntervalHierarchyWrapper.h:60