Number5
Visualisierung 2 Project - Florian Schober (0828151, f.schober@live.com), Andreas Walch (0926780, walch.andreas89@gmail.com)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
QuadTree.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "HUD.hpp"
4 
9 class QuadTree
10 {
14  template<typename T>
15  class Level
16  {
17  uvec2 m_size;
18  vector<T> m_data;
21  public:
28  Level(uvec2 const & size = uvec2(0,0), T const & borderValue = (T)0)
29  : m_data(size.x*size.y, 0)
30  , m_size(size)
31  , m_borderValue(borderValue)
32  { }
33 
39  T& at(uvec2 const & pos)
40  {
41  if (pos.x < 0 || pos.x >= m_size.x ||
42  pos.y < 0 || pos.y >= m_size.y)
43  {
44  return m_borderValue;
45  }
46 
47  return m_data[m_size.x * pos.y + pos.x];
48  }
49 
56  T& at(uint const x, uint const y)
57  {
58  return at(vec2(x,y));
59  }
64  T* data()
65  {
66  return m_data.data();
67  }
71  void clear()
72  {
73  m_data.assign(m_size.x*m_size.y, 0);
74  }
79  void resize(uvec2 const & size)
80  {
81  if (m_size == size) return;
82 
83  m_size = size;
84 
85  auto realSize = size.x*size.y;
86 
87  if (realSize == 0)
88  m_data.clear();
89  else
90  {
91  m_data.resize(realSize);
92  m_data.assign(realSize, 0);
93  }
94  m_data.shrink_to_fit();
95  }
100  uvec2 const & size() const
101  {
102  return m_size;
103  }
104 
105  };
106 
109 
110 
113  vector<MidLevel> m_levels;
115  array<auto_reset_event, 4> m_updateLayersParallelMutex;
118  public:
125  QuadTree(Application* app, uvec2 const & size, uint const maxDistance);
126 
131  void resize(uvec2 const & size);
132 
137  template<typename tFnc>
138  void fill(tFnc readIds)
139  {
140  if (m_data.size() == uvec2(0,0))
141  return;
142 
143  // copy data
144  readIds(m_data.data());
145 
146  if (m_levels.empty())
147  return;
148 
149  if (m_app->getConfig().pickerMultiThreaded() && (m_levels.size() > 5))
151  else
153  }
154 
163  bool findNear(uvec2 const & pos, OUT id_t & foundId, OUT uvec2 & foundPos, OUT float & foundDistance);
164 
169  void maxDistance(uint const distance);
174  uint const maxDistance() const;
175 
176 
177  private:
181  void updateLayersParallel();
185  void updateLayersSerial();
186 
193  void updateLayersBaseCell(uint const layer, uint const x, uint const y);
194 
203  template<typename TBaseLayer, typename TCurrentLayer>
204  void updateLayerArea(TBaseLayer base, TCurrentLayer current, uint const offset_x, uint const offset_y, uint size)
205  {
206  for (uint y = 0; y < min(offset_y+size,current->size().y); y++)
207  for (uint x = 0; x < min(offset_x+size,current->size().x); x++)
208  updateLayerCell(base, current, x, y);
209  }
210 
218  template<typename TBaseLayer, typename TCurrentLayer>
219  void updateLayerCell(TBaseLayer base, TCurrentLayer current, uint const x, uint const y)
220  {
221  current->at(x,y) = base->at(x*2+0, y*2+0)
222  || base->at(x*2+1, y*2+0)
223  || base->at(x*2+0, y*2+1)
224  || base->at(x*2+1, y*2+1);
225  }
226 };
uint16 id_t
The data-type used for the id-buffer.
Definition: Util.hpp:6
Configuration & getConfig()
Definition: Application.hpp:36
void updateLayerArea(TBaseLayer base, TCurrentLayer current, uint const offset_x, uint const offset_y, uint size)
Definition: QuadTree.hpp:204
vector< MidLevel > m_levels
Definition: QuadTree.hpp:113
uint const maxDistance() const
Definition: QuadTree.cpp:163
void resize(uvec2 const &size)
Definition: QuadTree.cpp:14
void updateLayersBaseCell(uint const layer, uint const x, uint const y)
Definition: QuadTree.cpp:169
array< auto_reset_event, 4 > m_updateLayersParallelMutex
Definition: QuadTree.hpp:115
uint m_maxDistance
Definition: QuadTree.hpp:114
Application * m_app
Definition: QuadTree.hpp:111
Level< BOOLEAN > MidLevel
Definition: QuadTree.hpp:108
void updateLayersParallel()
Definition: QuadTree.cpp:193
vector< T > m_data
Definition: QuadTree.hpp:18
bool pickerMultiThreaded() const
Level< id_t > DataLevel
Definition: QuadTree.hpp:107
Level(uvec2 const &size=uvec2(0, 0), T const &borderValue=(T) 0)
Definition: QuadTree.hpp:28
void updateLayersSerial()
Definition: QuadTree.cpp:207
DataLevel m_data
Definition: QuadTree.hpp:112
T & at(uvec2 const &pos)
Definition: QuadTree.hpp:39
void fill(tFnc readIds)
Definition: QuadTree.hpp:138
void updateLayerCell(TBaseLayer base, TCurrentLayer current, uint const x, uint const y)
Definition: QuadTree.hpp:219
uvec2 const & size() const
Definition: QuadTree.hpp:100
T & at(uint const x, uint const y)
Definition: QuadTree.hpp:56
void resize(uvec2 const &size)
Definition: QuadTree.hpp:79
bool findNear(uvec2 const &pos, OUT id_t &foundId, OUT uvec2 &foundPos, OUT float &foundDistance)
Definition: QuadTree.cpp:35
QuadTree(Application *app, uvec2 const &size, uint const maxDistance)
Definition: QuadTree.cpp:3