Grid.h 5.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /******************************************************************************
  2. Use 'Grid' to handle space partitioning (like a QuadTree).
  3. /******************************************************************************/
  4. T1(TYPE) struct Cell // Cell (template)
  5. {
  6. TYPE * data () {return _data ;} // get cell data
  7. TYPE & operator()() {return *_data ;} // get cell data
  8. T1(TYPE2) TYPE2* cast () {return CAST(TYPE2, _data);} // get cell data casted to custom class
  9. Int x()C {return _x[0] ;} // get x coordinate of the cell
  10. Int y()C {return _y[0] ;} // get y coordinate of the cell
  11. VecI2 xy()C {return VecI2(_x[0], _y[0]);} // get xy coordinates of the cell
  12. private:
  13. TYPE *_data;
  14. Int _x[4],
  15. _y[4];
  16. Cell *_parent;
  17. union
  18. {
  19. struct{Cell *_g[9];};
  20. struct{Cell *_lb, *_b, *_rb, *_l, *_c, *_r, *_lf, *_f, *_rf;};
  21. };
  22. Cell() {Zero(T);}
  23. NO_COPY_CONSTRUCTOR(Cell);
  24. };
  25. /******************************************************************************/
  26. T1(TYPE) struct Grid : private _Grid // Grid
  27. {
  28. // manage
  29. void del( ); // delete all cells
  30. void del(Cell<TYPE> *cell); // delete selected cell
  31. // get / set
  32. Cell<TYPE>& get(C VecI2 &xy ) ; // get 'xy' cell, create it if not found
  33. Cell<TYPE>* find(C VecI2 &xy )C; // get 'xy' cell, return null if not found
  34. Bool size( RectI &rect)C; // get rectangle covering all created grid cells, false on fail (if no grid cells are present)
  35. Ptr user( )C {return _Grid::user;} void user(Ptr user) {_Grid::user=user;} // get/set custom user data
  36. Grid& fastAccess (C RectI *rect); // optimize accessing cells (via 'find/get' methods) within 'rect' rectangle, normally cells are accessed recursively, however after calling this method all cells within the rectangle will be available instantly, if null is provided then the optimization is disabled
  37. T1(EXTENDED) Grid& replaceClass( ); // replace the type of class stored in the grid, all grid cells are automatically removed before changing the type of the class, the new type must be extended from the base 'TYPE' (if you're receiving a compilation error pointing to this method this means that the new class isn't extended from the base class)
  38. // call custom function on grid cells
  39. void func ( void func(Cell<TYPE> &cell, Ptr user) ); // call 'func' on all existing grid cells
  40. T1(USER_DATA) void func ( void func(Cell<TYPE> &cell, USER_DATA *user), USER_DATA *user=null); // call 'func' on all existing grid cells
  41. T1(USER_DATA) void func ( void func(Cell<TYPE> &cell, USER_DATA &user), USER_DATA &user ); // call 'func' on all existing grid cells
  42. void func (C RectI &rect, void func(Cell<TYPE> &cell, Ptr user) ); // call 'func' on all existing grid cells in specified rectangle
  43. T1(USER_DATA) void func (C RectI &rect, void func(Cell<TYPE> &cell, USER_DATA *user), USER_DATA *user=null); // call 'func' on all existing grid cells in specified rectangle
  44. T1(USER_DATA) void func (C RectI &rect, void func(Cell<TYPE> &cell, USER_DATA &user), USER_DATA &user ); // call 'func' on all existing grid cells in specified rectangle
  45. void funcCreate(C RectI &rect, void func(Cell<TYPE> &cell, Ptr user) ); // call 'func' on all grid cells in specified rectangle (this method creates grid cells if they don't exist yet)
  46. T1(USER_DATA) void funcCreate(C RectI &rect, void func(Cell<TYPE> &cell, USER_DATA *user), USER_DATA *user=null); // call 'func' on all grid cells in specified rectangle (this method creates grid cells if they don't exist yet)
  47. T1(USER_DATA) void funcCreate(C RectI &rect, void func(Cell<TYPE> &cell, USER_DATA &user), USER_DATA &user ); // call 'func' on all grid cells in specified rectangle (this method creates grid cells if they don't exist yet)
  48. // call custom function on grid cells (multi-threaded version)
  49. void mtFunc(Threads &threads, void func(Cell<TYPE> &cell, Ptr user, Int thread_index) ); // call 'func' on all existing grid cells
  50. T1(USER_DATA) void mtFunc(Threads &threads, void func(Cell<TYPE> &cell, USER_DATA *user, Int thread_index), USER_DATA *user=null); // call 'func' on all existing grid cells
  51. T1(USER_DATA) void mtFunc(Threads &threads, void func(Cell<TYPE> &cell, USER_DATA &user, Int thread_index), USER_DATA &user ); // call 'func' on all existing grid cells
  52. void mtFunc(Threads &threads, C RectI &rect, void func(Cell<TYPE> &cell, Ptr user, Int thread_index) ); // call 'func' on all existing grid cells in specified rectangle
  53. T1(USER_DATA) void mtFunc(Threads &threads, C RectI &rect, void func(Cell<TYPE> &cell, USER_DATA *user, Int thread_index), USER_DATA *user=null); // call 'func' on all existing grid cells in specified rectangle
  54. T1(USER_DATA) void mtFunc(Threads &threads, C RectI &rect, void func(Cell<TYPE> &cell, USER_DATA &user, Int thread_index), USER_DATA &user ); // call 'func' on all existing grid cells in specified rectangle
  55. Grid() {replaceClass<TYPE>();}
  56. };
  57. /******************************************************************************/