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
10namespace Stg {
11
12// a bit of experimenting suggests that these values are fast. YMMV.
13const uint32_t RBITS(5); // regions contain (2^RBITS)^2 pixels
14const uint32_t SBITS(5); // superregions contain (2^SBITS)^2 regions
15const uint32_t SRBITS(RBITS + SBITS);
16
17const int32_t REGIONWIDTH(1 << RBITS);
19
20const int32_t SUPERREGIONWIDTH(1 << SBITS);
22
23const int32_t CELLMASK(~((~0x00u) << RBITS));
24const int32_t REGIONMASK(~((~0x00u) << SRBITS));
25
26inline int32_t GETCELL(const int32_t x)
27{
28 return (x & CELLMASK);
29}
30inline int32_t GETREG(const int32_t x)
31{
32 return ((x & REGIONMASK) >> RBITS);
33}
34inline 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
42class Cell {
43 friend class SuperRegion;
44 friend class World;
45
46private:
47 std::vector<Block *> blocks[2];
48
49public:
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
65class Region {
66 friend class SuperRegion;
67 friend class World; // for raytracing
68
69private:
70 std::vector<Cell> cells;
71 unsigned long count; // number of blocks rendered into this region
72
73public:
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
99private:
100 unsigned long count; // number of blocks rendered into this superregion
101 point_int_t origin;
102 Region regions[SUPERREGIONSIZE];
103 World *world;
104
105public:
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
Definition stage.hh:1128
Definition region.hh:42
Region * region
Definition region.hh:62
void AddBlock(Block *b, unsigned int index)
Definition region.cc:274
const std::vector< Block * > & GetBlocks(unsigned int index)
Definition region.hh:61
Cell()
Definition region.hh:50
friend class World
Definition region.hh:44
void RemoveBlock(Block *b, unsigned int index)
Definition region.cc:313
friend class SuperRegion
Definition region.hh:43
Definition region.hh:65
~Region()
Definition region.cc:15
void AddBlock()
Definition region.cc:19
Cell * GetCell(int32_t x, int32_t y)
Definition region.hh:77
SuperRegion * superregion
Definition region.hh:94
friend class World
Definition region.hh:67
void RemoveBlock()
Definition region.cc:25
Region()
Definition region.cc:11
friend class SuperRegion
Definition region.hh:66
SuperRegion(World *world, point_int_t origin)
Definition region.cc:36
void DrawOccupancy(void) const
Definition region.cc:57
const point_int_t & GetOrigin() const
Definition region.hh:116
~SuperRegion()
Definition region.cc:43
void RemoveBlock()
Definition region.cc:52
void DrawVoxels(unsigned int layer) const
Definition region.cc:212
Region * GetRegion(int32_t x, int32_t y)
Definition region.hh:109
void AddBlock()
Definition region.cc:47
World class
Definition stage.hh:764
Definition stage.hh:472
The Stage library uses its own namespace.
Definition canvas.hh:8
const uint32_t RBITS(5)
const int32_t SUPERREGIONWIDTH(1<< SBITS)
int32_t GETCELL(const int32_t x)
Definition region.hh:26
int32_t GETREG(const int32_t x)
Definition region.hh:30
const int32_t REGIONWIDTH(1<< RBITS)
const int32_t CELLMASK(~((~0x00u)<< RBITS))
int32_t GETSREG(const int32_t x)
Definition region.hh:34
const uint32_t SRBITS(RBITS+SBITS)
const int32_t REGIONMASK(~((~0x00u)<< SRBITS))
const uint32_t SBITS(5)
const int32_t REGIONSIZE(REGIONWIDTH *REGIONWIDTH)
const int32_t SUPERREGIONSIZE(SUPERREGIONWIDTH *SUPERREGIONWIDTH)