TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RegularGrid.h
Go to the documentation of this file.
1 #ifndef _REGULAR_GRID_H
2 #define _REGULAR_GRID_H
3 
4 
5 #include <G3D/Ray.h>
6 #include <G3D/Table.h>
7 #include <G3D/BoundsTrait.h>
8 #include <G3D/PositionTrait.h>
9 
10 #include "Errors.h"
11 
12 template<class Node>
13 struct NodeCreator{
14  static Node * makeNode(int /*x*/, int /*y*/) { return new Node();}
15 };
16 
17 template<class T,
18 class Node,
19 class NodeCreatorFunc = NodeCreator<Node>,
20  /*class BoundsFunc = BoundsTrait<T>,*/
21 class PositionFunc = PositionTrait<T>
22 >
24 {
25 public:
26 
27  enum{
28  CELL_NUMBER = 64,
29  };
30 
31  #define HGRID_MAP_SIZE (533.33333f * 64.f) // shouldn't be changed
32  #define CELL_SIZE float(HGRID_MAP_SIZE/(float)CELL_NUMBER)
33 
35 
36  MemberTable memberTable;
37  Node* nodes[CELL_NUMBER][CELL_NUMBER];
38 
40  memset(nodes, 0, sizeof(nodes));
41  }
42 
44  for (int x = 0; x < CELL_NUMBER; ++x)
45  for (int y = 0; y < CELL_NUMBER; ++y)
46  delete nodes[x][y];
47  }
48 
49  void insert(const T& value)
50  {
51  G3D::Vector3 pos;
52  PositionFunc::getPosition(value, pos);
53  Node& node = getGridFor(pos.x, pos.y);
54  node.insert(value);
55  memberTable.set(&value, &node);
56  }
57 
58  void remove(const T& value)
59  {
60  memberTable[&value]->remove(value);
61  // Remove the member
62  memberTable.remove(&value);
63  }
64 
65  void balance()
66  {
67  for (int x = 0; x < CELL_NUMBER; ++x)
68  for (int y = 0; y < CELL_NUMBER; ++y)
69  if (Node* n = nodes[x][y])
70  n->balance();
71  }
72 
73  bool contains(const T& value) const { return memberTable.containsKey(&value); }
74  int size() const { return uint32(memberTable.size()); }
75 
76  struct Cell
77  {
78  int x, y;
79  bool operator == (const Cell& c2) const { return x == c2.x && y == c2.y;}
80 
81  static Cell ComputeCell(float fx, float fy)
82  {
83  Cell c = { int(fx * (1.f/CELL_SIZE) + (CELL_NUMBER/2)), int(fy * (1.f/CELL_SIZE) + (CELL_NUMBER/2)) };
84  return c;
85  }
86 
87  bool isValid() const { return x >= 0 && x < CELL_NUMBER && y >= 0 && y < CELL_NUMBER;}
88  };
89 
90 
91  Node& getGridFor(float fx, float fy)
92  {
93  Cell c = Cell::ComputeCell(fx, fy);
94  return getGrid(c.x, c.y);
95  }
96 
97  Node& getGrid(int x, int y)
98  {
99  ASSERT(x < CELL_NUMBER && y < CELL_NUMBER);
100  if (!nodes[x][y])
101  nodes[x][y] = NodeCreatorFunc::makeNode(x, y);
102  return *nodes[x][y];
103  }
104 
105  template<typename RayCallback>
106  void intersectRay(const G3D::Ray& ray, RayCallback& intersectCallback, float max_dist)
107  {
108  intersectRay(ray, intersectCallback, max_dist, ray.origin() + ray.direction() * max_dist);
109  }
110 
111  template<typename RayCallback>
112  void intersectRay(const G3D::Ray& ray, RayCallback& intersectCallback, float& max_dist, const G3D::Vector3& end)
113  {
114  Cell cell = Cell::ComputeCell(ray.origin().x, ray.origin().y);
115  if (!cell.isValid())
116  return;
117 
118  Cell last_cell = Cell::ComputeCell(end.x, end.y);
119 
120  if (cell == last_cell)
121  {
122  if (Node* node = nodes[cell.x][cell.y])
123  node->intersectRay(ray, intersectCallback, max_dist);
124  return;
125  }
126 
127  float voxel = (float)CELL_SIZE;
128  float kx_inv = ray.invDirection().x, bx = ray.origin().x;
129  float ky_inv = ray.invDirection().y, by = ray.origin().y;
130 
131  int stepX, stepY;
132  float tMaxX, tMaxY;
133  if (kx_inv >= 0)
134  {
135  stepX = 1;
136  float x_border = (cell.x+1) * voxel;
137  tMaxX = (x_border - bx) * kx_inv;
138  }
139  else
140  {
141  stepX = -1;
142  float x_border = (cell.x-1) * voxel;
143  tMaxX = (x_border - bx) * kx_inv;
144  }
145 
146  if (ky_inv >= 0)
147  {
148  stepY = 1;
149  float y_border = (cell.y+1) * voxel;
150  tMaxY = (y_border - by) * ky_inv;
151  }
152  else
153  {
154  stepY = -1;
155  float y_border = (cell.y-1) * voxel;
156  tMaxY = (y_border - by) * ky_inv;
157  }
158 
159  //int Cycles = std::max((int)ceilf(max_dist/tMaxX),(int)ceilf(max_dist/tMaxY));
160  //int i = 0;
161 
162  float tDeltaX = voxel * std::fabs(kx_inv);
163  float tDeltaY = voxel * std::fabs(ky_inv);
164  do
165  {
166  if (Node* node = nodes[cell.x][cell.y])
167  {
168  //float enterdist = max_dist;
169  node->intersectRay(ray, intersectCallback, max_dist);
170  }
171  if (cell == last_cell)
172  break;
173  if (tMaxX < tMaxY)
174  {
175  tMaxX += tDeltaX;
176  cell.x += stepX;
177  }
178  else
179  {
180  tMaxY += tDeltaY;
181  cell.y += stepY;
182  }
183  //++i;
184  } while (cell.isValid());
185  }
186 
187  template<typename IsectCallback>
188  void intersectPoint(const G3D::Vector3& point, IsectCallback& intersectCallback)
189  {
190  Cell cell = Cell::ComputeCell(point.x, point.y);
191  if (!cell.isValid())
192  return;
193  if (Node* node = nodes[cell.x][cell.y])
194  node->intersectPoint(point, intersectCallback);
195  }
196 
197  // Optimized verson of intersectRay function for rays with vertical directions
198  template<typename RayCallback>
199  void intersectZAllignedRay(const G3D::Ray& ray, RayCallback& intersectCallback, float& max_dist)
200  {
201  Cell cell = Cell::ComputeCell(ray.origin().x, ray.origin().y);
202  if (!cell.isValid())
203  return;
204  if (Node* node = nodes[cell.x][cell.y])
205  node->intersectRay(ray, intersectCallback, max_dist);
206  }
207 };
208 
209 #undef CELL_SIZE
210 #undef HGRID_MAP_SIZE
211 
212 #endif
int y
Definition: RegularGrid.h:78
static Cell ComputeCell(float fx, float fy)
Definition: RegularGrid.h:81
int x
Definition: RegularGrid.h:78
#define CELL_SIZE
Definition: RegularGrid.h:32
Node & getGrid(int x, int y)
Definition: RegularGrid.h:97
float x
Definition: Vector3.h:62
size_t size() const
Definition: Table.h:589
void intersectRay(const G3D::Ray &ray, RayCallback &intersectCallback, float max_dist)
Definition: RegularGrid.h:106
Definition: PositionTrait.h:5
static Node * makeNode(int, int)
Definition: RegularGrid.h:14
void intersectPoint(const G3D::Vector3 &point, IsectCallback &intersectCallback)
Definition: RegularGrid.h:188
void set(const Key &key, const Value &value)
Definition: Table.h:599
G3D::Table< const T *, Node * > MemberTable
Definition: RegularGrid.h:34
float y
Definition: Vector3.h:62
Definition: Vector3.h:58
void intersectRay(const G3D::Ray &ray, RayCallback &intersectCallback, float &max_dist, const G3D::Vector3 &end)
Definition: RegularGrid.h:112
bool isValid() const
Definition: RegularGrid.h:87
~RegularGrid2D()
Definition: RegularGrid.h:43
void balance()
Definition: RegularGrid.h:65
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
bool operator==(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:160
G3D::int16 y
Definition: Vector2int16.h:38
const Vector3 & invDirection() const
Definition: Ray.h:66
Definition: Ray.h:24
#define TC_COMMON_API
Definition: Define.h:116
Node & getGridFor(float fx, float fy)
Definition: RegularGrid.h:91
Definition: RegularGrid.h:13
void intersectZAllignedRay(const G3D::Ray &ray, RayCallback &intersectCallback, float &max_dist)
Definition: RegularGrid.h:199
MemberTable memberTable
Definition: RegularGrid.h:36
const Point3 & origin() const
Definition: Ray.h:56
Definition: RegularGrid.h:76
void insert(const T &value)
Definition: RegularGrid.h:49
bool contains(const T &value) const
Definition: RegularGrid.h:73
int size() const
Definition: RegularGrid.h:74
RegularGrid2D()
Definition: RegularGrid.h:39
#define ASSERT
Definition: Errors.h:55
const FieldDescriptor value
Definition: descriptor.h:1522
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
Definition: RegularGrid.h:23
const Vector3 & direction() const
Definition: Ray.h:61
bool containsKey(const Key &key) const
Definition: Table.h:874