AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc > Class Template Reference

#include "RegularGrid.h"

Inheritance diagram for RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >:
DynTreeImpl

Classes

struct  Cell
 

Public Types

enum  { CELL_NUMBER = 64 }
 
typedef G3D::Table< const T *, NodeArray< Node > > MemberTable
 

Public Member Functions

 RegularGrid2D ()
 
 ~RegularGrid2D ()
 
void insert (const T &value)
 
void remove (const T &value)
 
void balance ()
 
bool contains (const T &value) const
 
int size () const
 
Node & getGridFor (float fx, float fy)
 
Node & getGrid (int x, int y)
 
template<typename RayCallback >
void intersectRay (const G3D::Ray &ray, RayCallback &intersectCallback, float max_dist, bool stopAtFirstHit)
 
template<typename RayCallback >
void intersectRay (const G3D::Ray &ray, RayCallback &intersectCallback, float &max_dist, const G3D::Vector3 &end, bool stopAtFirstHit)
 
template<typename IsectCallback >
void intersectPoint (const G3D::Vector3 &point, IsectCallback &intersectCallback)
 
template<typename RayCallback >
void intersectZAllignedRay (const G3D::Ray &ray, RayCallback &intersectCallback, float &max_dist)
 

Public Attributes

MemberTable memberTable
 
Node * nodes [CELL_NUMBER][CELL_NUMBER]
 

Detailed Description

template<class T, class Node, class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
class RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >

Member Typedef Documentation

◆ MemberTable

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
typedef G3D::Table<const T*, NodeArray<Node> > RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::MemberTable

Member Enumeration Documentation

◆ anonymous enum

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
anonymous enum
Enumerator
CELL_NUMBER 
47 {
48 CELL_NUMBER = 64,
49 };
@ CELL_NUMBER
Definition: RegularGrid.h:48

Constructor & Destructor Documentation

◆ RegularGrid2D()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::RegularGrid2D ( )
inline
60 {
61 memset(nodes, 0, sizeof(nodes));
62 }
Node * nodes[CELL_NUMBER][CELL_NUMBER]
Definition: RegularGrid.h:57

References RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::nodes.

◆ ~RegularGrid2D()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::~RegularGrid2D ( )
inline
65 {
66 for (int x = 0; x < CELL_NUMBER; ++x)
67 for (int y = 0; y < CELL_NUMBER; ++y)
68 {
69 delete nodes[x][y];
70 }
71 }

References RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::CELL_NUMBER, and RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::nodes.

Member Function Documentation

◆ balance()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
void RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::balance ( )
inline
133 {
134 for (int x = 0; x < CELL_NUMBER; ++x)
135 for (int y = 0; y < CELL_NUMBER; ++y)
136 if (Node* n = nodes[x][y])
137 {
138 n->balance();
139 }
140 }

References RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::CELL_NUMBER, and RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::nodes.

Referenced by DynTreeImpl::balance().

◆ contains()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
bool RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::contains ( const T &  value) const
inline
142{ return memberTable.containsKey(&value); }
MemberTable memberTable
Definition: RegularGrid.h:56

References RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::memberTable.

Referenced by DynamicMapTree::contains().

◆ getGrid()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
Node & RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::getGrid ( int  x,
int  y 
)
inline
166 {
167 ASSERT(x < CELL_NUMBER && y < CELL_NUMBER);
168 if (!nodes[x][y])
169 {
170 nodes[x][y] = NodeCreatorFunc::makeNode(x, y);
171 }
172 return *nodes[x][y];
173 }
#define ASSERT
Definition: Errors.h:68

References ASSERT, RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::CELL_NUMBER, and RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::nodes.

Referenced by RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::getGridFor().

◆ getGridFor()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
Node & RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::getGridFor ( float  fx,
float  fy 
)
inline

◆ insert()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
void RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::insert ( const T &  value)
inline
74 {
75 G3D::Vector3 pos[9];
76 pos[0] = value.GetBounds().corner(0);
77 pos[1] = value.GetBounds().corner(1);
78 pos[2] = value.GetBounds().corner(2);
79 pos[3] = value.GetBounds().corner(3);
80 pos[4] = (pos[0] + pos[1]) / 2.0f;
81 pos[5] = (pos[1] + pos[2]) / 2.0f;
82 pos[6] = (pos[2] + pos[3]) / 2.0f;
83 pos[7] = (pos[3] + pos[0]) / 2.0f;
84 pos[8] = (pos[0] + pos[2]) / 2.0f;
85
87 for (uint8 i = 0; i < 9; ++i)
88 {
89 Cell c = Cell::ComputeCell(pos[i].x, pos[i].y);
90 if (!c.isValid())
91 {
92 continue;
93 }
94 Node& node = getGridFor(pos[i].x, pos[i].y);
95 na.AddNode(&node);
96 }
97
98 for (uint8 i = 0; i < 9; ++i)
99 {
100 if (na._nodes[i])
101 {
102 na._nodes[i]->insert(value);
103 }
104 else
105 {
106 break;
107 }
108 }
109
110 memberTable.set(&value, na);
111 }
std::uint8_t uint8
Definition: Define.h:109
Definition: RegularGrid.h:12
void AddNode(Node *n)
Definition: RegularGrid.h:15
Node * _nodes[9]
Definition: RegularGrid.h:28
Node & getGridFor(float fx, float fy)
Definition: RegularGrid.h:159

References NodeArray< Node >::_nodes, NodeArray< Node >::AddNode(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::ComputeCell(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::getGridFor(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::isValid(), and RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::memberTable.

Referenced by DynTreeImpl::insert().

◆ intersectPoint()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
template<typename IsectCallback >
void RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::intersectPoint ( const G3D::Vector3 &  point,
IsectCallback &  intersectCallback 
)
inline

◆ intersectRay() [1/2]

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
template<typename RayCallback >
void RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::intersectRay ( const G3D::Ray &  ray,
RayCallback &  intersectCallback,
float &  max_dist,
const G3D::Vector3 &  end,
bool  stopAtFirstHit 
)
inline
183 {
184 Cell cell = Cell::ComputeCell(ray.origin().x, ray.origin().y);
185 if (!cell.isValid())
186 {
187 return;
188 }
189
190 Cell last_cell = Cell::ComputeCell(end.x, end.y);
191
192 if (cell == last_cell)
193 {
194 if (Node* node = nodes[cell.x][cell.y])
195 {
196 node->intersectRay(ray, intersectCallback, max_dist, stopAtFirstHit);
197 }
198 return;
199 }
200
201 float voxel = (float)CELL_SIZE;
202 float kx_inv = ray.invDirection().x, bx = ray.origin().x;
203 float ky_inv = ray.invDirection().y, by = ray.origin().y;
204
205 int stepX, stepY;
206 float tMaxX, tMaxY;
207 if (kx_inv >= 0)
208 {
209 stepX = 1;
210 float x_border = (cell.x + 1) * voxel;
211 tMaxX = (x_border - bx) * kx_inv;
212 }
213 else
214 {
215 stepX = -1;
216 float x_border = (cell.x - 1) * voxel;
217 tMaxX = (x_border - bx) * kx_inv;
218 }
219
220 if (ky_inv >= 0)
221 {
222 stepY = 1;
223 float y_border = (cell.y + 1) * voxel;
224 tMaxY = (y_border - by) * ky_inv;
225 }
226 else
227 {
228 stepY = -1;
229 float y_border = (cell.y - 1) * voxel;
230 tMaxY = (y_border - by) * ky_inv;
231 }
232
233 //int Cycles = std::max((int)ceilf(max_dist/tMaxX),(int)ceilf(max_dist/tMaxY));
234 //int i = 0;
235
236 float tDeltaX = voxel * std::fabs(kx_inv);
237 float tDeltaY = voxel * std::fabs(ky_inv);
238 do
239 {
240 if (Node* node = nodes[cell.x][cell.y])
241 {
242 //float enterdist = max_dist;
243 node->intersectRay(ray, intersectCallback, max_dist, stopAtFirstHit);
244 }
245 if (cell == last_cell)
246 {
247 break;
248 }
249 if (tMaxX < tMaxY)
250 {
251 tMaxX += tDeltaX;
252 cell.x += stepX;
253 }
254 else
255 {
256 tMaxY += tDeltaY;
257 cell.y += stepY;
258 }
259 //++i;
260 } while (cell.isValid());
261 }
#define CELL_SIZE
Definition: RegularGrid.h:52

References CELL_SIZE, RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::ComputeCell(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::isValid(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::nodes, RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::x, and RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::y.

◆ intersectRay() [2/2]

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
template<typename RayCallback >
void RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::intersectRay ( const G3D::Ray &  ray,
RayCallback &  intersectCallback,
float  max_dist,
bool  stopAtFirstHit 
)
inline
177 {
178 intersectRay(ray, intersectCallback, max_dist, ray.origin() + ray.direction() * max_dist, stopAtFirstHit);
179 }
void intersectRay(const G3D::Ray &ray, RayCallback &intersectCallback, float max_dist, bool stopAtFirstHit)
Definition: RegularGrid.h:176

References RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::intersectRay().

Referenced by DynamicMapTree::GetIntersectionTime(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::intersectRay(), and DynamicMapTree::isInLineOfSight().

◆ intersectZAllignedRay()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
template<typename RayCallback >
void RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::intersectZAllignedRay ( const G3D::Ray &  ray,
RayCallback &  intersectCallback,
float &  max_dist 
)
inline
280 {
281 Cell cell = Cell::ComputeCell(ray.origin().x, ray.origin().y);
282 if (!cell.isValid())
283 {
284 return;
285 }
286 if (Node* node = nodes[cell.x][cell.y])
287 {
288 node->intersectRay(ray, intersectCallback, max_dist, false);
289 }
290 }

References RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::ComputeCell(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::isValid(), RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::nodes, RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::x, and RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::Cell::y.

Referenced by DynamicMapTree::getHeight().

◆ remove()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
void RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::remove ( const T &  value)
inline
114 {
115 NodeArray<Node>& na = memberTable[&value];
116 for (uint8 i = 0; i < 9; ++i)
117 {
118 if (na._nodes[i])
119 {
120 na._nodes[i]->remove(value);
121 }
122 else
123 {
124 break;
125 }
126 }
127
128 // Remove the member
129 memberTable.remove(&value);
130 }

References NodeArray< Node >::_nodes, and RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::memberTable.

Referenced by DynTreeImpl::remove().

◆ size()

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
int RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::size ( ) const
inline

Member Data Documentation

◆ memberTable

template<class T , class Node , class NodeCreatorFunc = NodeCreator<Node>, class PositionFunc = PositionTrait<T>>
MemberTable RegularGrid2D< T, Node, NodeCreatorFunc, PositionFunc >::memberTable

◆ nodes