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