region.hh
Go to the documentation of this file.
1 #pragma once
2 /*
3  region.hh
4  data structures supporting multi-resolution ray tracing in world class.
5  Copyright Richard Vaughan 2008
6 */
7 
8 #include "stage.hh"
9 
10 namespace Stg {
11 
12 // a bit of experimenting suggests that these values are fast. YMMV.
13 const uint32_t RBITS(5); // regions contain (2^RBITS)^2 pixels
14 const uint32_t SBITS(5); // superregions contain (2^SBITS)^2 regions
15 const uint32_t SRBITS(RBITS + SBITS);
16 
17 const int32_t REGIONWIDTH(1 << RBITS);
18 const int32_t REGIONSIZE(REGIONWIDTH *REGIONWIDTH);
19 
20 const int32_t SUPERREGIONWIDTH(1 << SBITS);
22 
23 const int32_t CELLMASK(~((~0x00u) << RBITS));
24 const int32_t REGIONMASK(~((~0x00u) << SRBITS));
25 
26 inline int32_t GETCELL(const int32_t x)
27 {
28  return (x & CELLMASK);
29 }
30 inline int32_t GETREG(const int32_t x)
31 {
32  return ((x & REGIONMASK) >> RBITS);
33 }
34 inline int32_t GETSREG(const int32_t x)
35 {
36  return (x >> SRBITS);
37 }
38 
39 // this is slightly faster than the inline method above, but not as safe
40 //#define GETREG(X) (( (static_cast<int32_t>(X)) & REGIONMASK ) >> RBITS)
41 
42 class Cell {
43  friend class SuperRegion;
44  friend class World;
45 
46 private:
47  std::vector<Block *> blocks[2];
48 
49 public:
50  Cell() : blocks(), region(NULL)
51  {
52  // prevent frequent memory allocations
53  blocks[0].reserve(8);
54  blocks[1].reserve(8);
55  /* nothing to do */
56  }
57 
58  void RemoveBlock(Block *b, unsigned int index);
59  void AddBlock(Block *b, unsigned int index);
60 
61  inline const std::vector<Block *> &GetBlocks(unsigned int index) { return blocks[index]; }
63 }; // class Cell
64 
65 class Region {
66  friend class SuperRegion;
67  friend class World; // for raytracing
68 
69 private:
70  std::vector<Cell> cells;
71  unsigned long count; // number of blocks rendered into this region
72 
73 public:
74  Region();
75  ~Region();
76 
77  inline Cell *GetCell(int32_t x, int32_t y)
78  {
79  if (cells.size() == 0) {
80  assert(count == 0);
81 
82  cells.resize(REGIONSIZE);
83 
84  for (int32_t c = 0; c < REGIONSIZE; ++c)
85  cells[c].region = this;
86  }
87 
88  return (&cells[x + y * REGIONWIDTH]);
89  }
90 
91  inline void AddBlock();
92  inline void RemoveBlock();
93 
95 
96 }; // class Region
97 
98 class SuperRegion {
99 private:
100  unsigned long count; // number of blocks rendered into this superregion
104 
105 public:
106  SuperRegion(World *world, point_int_t origin);
107  ~SuperRegion();
108 
109  inline Region *GetRegion(int32_t x, int32_t y) { return (&regions[x + y * SUPERREGIONWIDTH]); }
110  void DrawOccupancy(void) const;
111  void DrawVoxels(unsigned int layer) const;
112 
113  inline void AddBlock();
114  inline void RemoveBlock();
115 
116  const point_int_t &GetOrigin() const { return origin; }
117 }; // class SuperRegion;
118 
119 } // namespace Stg
int32_t GETSREG(const int32_t x)
Definition: region.hh:34
std::vector< Block * > blocks[2]
Definition: region.hh:47
Region * region
Definition: region.hh:62
World class
Definition: stage.hh:764
The Stage library uses its own namespace.
Definition: canvas.hh:8
const uint32_t SRBITS(RBITS+SBITS)
int32_t GETREG(const int32_t x)
Definition: region.hh:30
Cell()
Definition: region.hh:50
const int32_t REGIONMASK(~((~0x00u)<< SRBITS))
std::vector< Cell > cells
Definition: region.hh:70
void AddBlock(Block *b, unsigned int index)
Definition: region.cc:274
SuperRegion * superregion
Definition: region.hh:94
const int32_t REGIONSIZE(REGIONWIDTH *REGIONWIDTH)
const uint32_t RBITS(5)
unsigned long count
Definition: region.hh:100
void RemoveBlock(Block *b, unsigned int index)
Definition: region.cc:313
Region * GetRegion(int32_t x, int32_t y)
Definition: region.hh:109
const int32_t CELLMASK(~((~0x00u)<< RBITS))
const int32_t REGIONWIDTH(1<< RBITS)
Cell * GetCell(int32_t x, int32_t y)
Definition: region.hh:77
const int32_t SUPERREGIONSIZE(SUPERREGIONWIDTH *SUPERREGIONWIDTH)
const uint32_t SBITS(5)
friend class SuperRegion
Definition: region.hh:43
World * world
Definition: region.hh:103
point_int_t origin
Definition: region.hh:101
const std::vector< Block * > & GetBlocks(unsigned int index)
Definition: region.hh:61
int32_t GETCELL(const int32_t x)
Definition: region.hh:26
const point_int_t & GetOrigin() const
Definition: region.hh:116
unsigned long count
Definition: region.hh:71
const int32_t SUPERREGIONWIDTH(1<< SBITS)


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Feb 28 2022 23:48:56