mapindex.h
1 /************************************************************
2 * (C) Voxel Farm Inc. 2015
3 */
4 
5 #pragma once
6 
7 #include "VoxelFarmConfig.h"
8 
9 #include <set>
10 #include <map>
11 #include "ExternalMutex.h"
12 
13 namespace VoxelFarm
14 {
16  const int CELL_BITS_X = 24;
18  const int CELL_BITS_Y = 12;
20  const int CELL_BITS_Z = 24;
21 
23  typedef unsigned __int64 CellId;
25  typedef TSet<CellId> Cells;
26 
28  inline CellId packCellId(int level, int x, int y, int z)
29  {
30  CellId id = 0;
31  id |= (CellId)level << (CELL_BITS_X + CELL_BITS_Y + CELL_BITS_Z);
32  id |= (CellId)x << (CELL_BITS_Y + CELL_BITS_Z);
33  id |= (CellId)y << (CELL_BITS_Z);
34  id |= z;
35  return id;
36  }
37 
39  inline void unpackCellId(CellId id, int &level, int &x, int &y, int &z)
40  {
41  level = (int)(id >> (CELL_BITS_X + CELL_BITS_Y + CELL_BITS_Z));
42  id &= ~((CellId)level << (CELL_BITS_X + CELL_BITS_Y + CELL_BITS_Z));
43 
44  x = (int)(id >> (CELL_BITS_Y + CELL_BITS_Z));
45  id &= ~((CellId)x << (CELL_BITS_Y + CELL_BITS_Z));
46 
47  y = (int)(id >> CELL_BITS_Z);
48  id &= ~((CellId)y << CELL_BITS_Z);
49 
50  z = (int)id;
51  }
52 
54  class MapIndex
55  {
56  public:
57  MapIndex(int aKeyLevel, char* aIndexPath);
58  ~MapIndex();
60  bool cellIsEmpty(CellId cell, int level, int x, int y, int z);
61  private:
62  TMap<CellId, Cells*> cache;
63  int keyLevel;
64  char* indexPath;
65  Cells* topCell;
66  protected:
67  Cells* loadSection(CellId key);
69  };
70 
72  bool CellSort(const CellId& a, const CellId& b);
73 
75  inline void getChildCells(CellId cell, CellId childCells[2][2][2])
76  {
77  int level, xc, yc, zc;
78  unpackCellId(cell, level, xc, yc, zc);
79 
80  // Load children data into array
81  for (int qz = 0; qz < 2; qz++)
82  for (int qx = 0; qx < 2; qx++)
83  for (int qy = 0; qy < 2; qy++)
84  {
85  // Compute Id of child cell
86  CellId childId = packCellId(
87  level - 1,
88  2*xc + qx,
89  2*yc + qy,
90  2*zc + qz);
91 
92  childCells[qx][qy][qz] = childId;
93  }
94  }
95 
96 
97 
98 }
CellId packCellId(int level, int x, int y, int z)
Compose a CellId from the location of a Cell in the octree.
Definition: mapindex.h:28
const int CELL_BITS_Y
Bits to encode octree Y coordinate in Cell Id.
Definition: mapindex.h:18
Contains all classes and functions for the VoxelFarm engine.
An index of which Cells are empty and which ones contain information. For offline mode only...
Definition: mapindex.h:54
TSet< CellId > Cells
A set of cells.
Definition: mapindex.h:25
const int CELL_BITS_X
Bits to encode octree X coordinate in Cell Id.
Definition: mapindex.h:16
const int CELL_BITS_Z
Bits to encode octree Z coordinate in Cell Id.
Definition: mapindex.h:20
unsigned __int64 CellId
A 64bit integer that identifies a single world octree Cell.
Definition: mapindex.h:23
bool CellSort(const CellId &a, const CellId &b)
A sorting criteria for cells in a scene so closer cells appear first.
void unpackCellId(CellId id, int &level, int &x, int &y, int &z)
Obtain location of a Cell in the octree from its CellId identifier.
Definition: mapindex.h:39
bool cellIsEmpty(CellId cell, int level, int x, int y, int z)
Returns whether a cell has data.
void getChildCells(CellId cell, CellId childCells[2][2][2])
Given a cell's ID computes its eight children.
Definition: mapindex.h:75