BlockData.h
1 /************************************************************
2 * (C) Voxel Farm Inc. 2015
3 */
4 
5 #pragma once
6 
7 #include "mapindex.h"
8 #include "Vector.h"
9 
10 #include <map>
11 #include <list>
12 #include <set>
13 #include <vector>
14 #include "VoxelLayer.h"
15 
16 namespace VoxelFarm
17 {
18 
21  {
23  float x;
25  float y;
27  float z;
29  bool mat;
31  bool vector;
32  };
33 
35 
36  class IBlockData
37  {
38  public:
40  virtual void setBlock(
42  CellId& cell,
44  int& x,
46  int& y,
48  int& z,
50  int material,
52  double dx,
54  double dy,
56  double dz,
57  TSet<CellId>* changedCells) = 0;
59  virtual BlockVoxelData* fetchData(
61  CellId cell,
63  bool create) = 0;
65  virtual BlockVoxelData* fetchCacheData(
67  CellId cell,
69  bool create) = 0;
71  virtual void beginChanges() = 0;
73  virtual void trackCellChanges(CellId cell, const BlockVoxelData* blockData) = 0;
75  virtual void endChanges() = 0;
76  };
77 
78 
80  void updateBlockLOD(IBlockData* blocks, CellId cell);
81 
84  IBlockData* blockData,
86  CellId cell,
88  ContourVoxelData* data,
90  bool& empty,
92  BlockVoxelData* blockKernel[3][3][3]);
93 
94  int getCuboidIntersections(VoxelFarm::Algebra::Vector* result, int sizeX, int sizeY, int sizeZ, int x, int y, int z);
95  int getVoxelListIntersections(VoxelFarm::Algebra::Vector* result, unsigned int* data, int xsize, int ysize, int zsize, int x, int y, int z);
96  void convertOldData(BlockVoxelData* data, double oldMinValue = 0.0, double oldMaxValue = 1.0);
97  int getContourIntersections(VoxelFarm::Algebra::Vector* result, ContourVoxelData* data, int x, int y, int z);
98 
99  int getVoxelIntersections(
101  VoxelFarm::IBlockData* blocks,
102  BlockVoxelData* data,
103  CellId cell,
104  int x, int y, int z,
105  bool air = true
106  );
107 
108  int getMaskVoxelIntersections(VoxelFarm::Algebra::Vector* result, VoxelMaskData* data, int xsize, int ysize, int zsize, int x, int y, int z);
109 
112  {
113  public:
114  CBufferBlockData(IBlockData* blockData);
115  virtual ~CBufferBlockData(void);
116 
118  void merge(TSet<CellId>* changedCells);
119  private:
121  TMap<CellId, BlockVoxelData*> blockCache;
122  IBlockData* blockData;
123  public:
125  virtual void setBlock(
127  CellId& cell,
129  int& x,
131  int& y,
133  int& z,
135  int material,
137  double dx,
139  double dy,
141  double dz,
142  TSet<CellId>* changedCells);
144  virtual void getBlock(
146  CellId cell,
148  int x,
150  int y,
152  int z,
154  int& material,
156  double& dx,
158  double& dy,
160  double& dz);
162  virtual void beginChanges();
164  virtual void trackCellChanges(CellId cell, const BlockVoxelData* blockData);
166  virtual void endChanges();
167  virtual BlockVoxelData* fetchData(
169  CellId cell,
171  bool create);
173  virtual BlockVoxelData* fetchCacheData(
175  CellId cell,
177  bool create);
178  };
179 
180 
182  class CBlockData : public IVoxelLayer, public IBlockData
183  {
184  public:
185  const static int IMMEDIATE_UPDATE_LOD = LOD_0 + 1;
186  public:
187  CBlockData(void);
188  virtual ~CBlockData(void);
189  public:
191  virtual void setBlock(
193  CellId& cell,
195  int& x,
197  int& y,
199  int& z,
201  int material,
203  double dx,
205  double dy,
207  double dz,
208  TSet<CellId>* changedCells);
210  virtual void getBlock(
212  CellId cell,
214  int x,
216  int y,
218  int z,
220  int& material,
222  double& dx,
224  double& dy,
226  double& dz);
228  virtual void getContourData(
230  CellId cell,
232  ContourVoxelData* data,
234  bool& empty,
236  void* threadContext) override;
238  virtual void* createThreadContext() override;
240  virtual void disposeThreadContext(void* threadContext) override;
241  virtual int getStatsContourId() override
242  {
243  return STAT_SANDBOX;
244  }
245  virtual bool isCacheable() override
246  {
247  return false;
248  }
250  virtual void updateLOD(const TSet<CellId>& changedCells);
252  virtual void beginChanges();
254  virtual void trackCellChanges(CellId cell, const BlockVoxelData* blockData);
256  virtual void endChanges();
258  virtual void undo(TSet<CellId>& changedCells);
259  public:
261  class IBlockIO
262  {
263  public:
265  virtual BlockVoxelData* loadCell(CellId cell)
266  {
267  return NULL;
268  }
270  virtual void saveCell(CellId cell, BlockVoxelData* data) {}
271  };
273  void setBlockIOHandler(IBlockIO* blockIOHandler);
275  BlockVoxelData* loadCell(CellId cell);
277  void saveCell(CellId cell, BlockVoxelData* data);
278  protected:
279  IBlockIO* blockIO;
280  public:
284  {
285  public:
287  virtual BlockVoxelData* getCell(const CellId& cell, bool create) = 0;
289  virtual void commitCell(const CellId& cell, BlockVoxelData* cellBocks) = 0;
291  virtual void clearCellCache(const CellId& cell) = 0;
292  };
294  void setBlockCacheHandler(IBlockCache* blockCacheHandler);
295  protected:
296  IBlockCache* blockCacheHandler;
297  public:
299  TMap<CellId, BlockVoxelData*> blockCache;
301  virtual BlockVoxelData* fetchData(
303  CellId cell,
305  bool create);
307  virtual BlockVoxelData* fetchCacheData(
309  CellId cell,
311  bool create);
313  void removeFromCache(CellId cell);
314 
317  public:
319  //TSet<CellId> invalidatedCells;
321  TMap<CellId, BlockVoxelData*> undoData;
322  private:
323  bool trackChanges;
324 
325  public:
327  {
328  BlockVoxelData* kernel[3][3][3];
329  };
330  private:
331  TSet<CellId> pendingLODUpdateSet;
332  TSet<CellId> inprocessLODUpdateSet;
333  public:
334  bool pendingForUpdate();
335  void cellProcessed(CellId cell);
336  bool cellChildIsInProcessForUpdate(CellId cell);
337  CellId getNextPendingForUpdate(CellId cell = 0);
338  bool cellIsPendingForUpdate(CellId cell);
339 
345  BlockVoxelData* data,
347  CellId cell, int x, int y, int z
348  );
349  private:
350  void blockContourData(CellId cell, ContourVoxelData* data, bool& empty, BlockVoxelData* blockKernel[3][3][3]);
351  };
352 
353 }
virtual void getBlock(CellId cell, int x, int y, int z, int &material, double &dx, double &dy, double &dz)
Gets the value of a voxel.
virtual void endChanges()
Closes the marker for a change set that can be undone.
virtual void endChanges()
Closes the marker for a change set that can be undone.
virtual void beginChanges()
Sets a marker for a change set that can be undone.
void getBlockContourData(IBlockData *blockData, CellId cell, ContourVoxelData *data, bool &empty, BlockVoxelData *blockKernel[3][3][3])
Copies voxel data from a block and its neighbors.
virtual int getStatsContourId() override
Returns the stats ID to be used for measuring performance of the getContourData() method...
Definition: BlockData.h:241
virtual BlockVoxelData * fetchData(CellId cell, bool create)
Returns the voxel data buffer for the specified cell. This is a synchronous call. ...
virtual bool isCacheable() override
Returns true if the layer's output can be cached. Return true if the output for a cell will always be...
Definition: BlockData.h:245
Contains all classes and functions for the VoxelFarm engine.
virtual void getBlock(CellId cell, int x, int y, int z, int &material, double &dx, double &dy, double &dz)
Gets the value of a voxel.
void saveCell(CellId cell, BlockVoxelData *data)
Saves voxel data for the specified cell using the assigned IBlockIO handler.
virtual BlockVoxelData * fetchCacheData(CellId cell, bool create)
Returns a cache voxel data buffer for the specified cell.
TMap< CellId, BlockVoxelData * > blockCache
An index where cell IDs are matched to their corresponding voxel data buffer.
Definition: BlockData.h:299
virtual void * createThreadContext() override
The voxel layer can use this method to return a structure that will be unique for each calling thread...
virtual void clearCellCache(const CellId &cell)=0
Remove a voxel data from the cache.
A 3D Vector.
Definition: Vector.h:34
virtual void beginChanges()=0
Sets a marker for a change set that can be undone.
void setBlockIOHandler(IBlockIO *blockIOHandler)
Sets the IBlockIO interface.
A default implementation of IBlockData that also acts as a VoxelLayer.
Definition: BlockData.h:182
void setBlockCacheHandler(IBlockCache *blockCacheHandler)
Sets the IBlockCache interface.
virtual void commitCell(const CellId &cell, BlockVoxelData *cellBocks)=0
Commit modified voxel data to the cache.
virtual void saveCell(CellId cell, BlockVoxelData *data)
Saves voxel data for the specified cell.
Definition: BlockData.h:270
BlockVoxelData * loadCell(CellId cell)
Loads voxel data for the specified cell using the assigned IBlockIO handler.
virtual void setBlock(CellId &cell, int &x, int &y, int &z, int material, double dx, double dy, double dz, TSet< CellId > *changedCells)=0
Sets a value for a voxel.
virtual void setBlock(CellId &cell, int &x, int &y, int &z, int material, double dx, double dy, double dz, TSet< CellId > *changedCells)
Sets a value for a voxel.
unsigned __int64 CellId
A 64bit integer that identifies a single world octree Cell.
Definition: mapindex.h:23
void merge(TSet< CellId > *changedCells)
Merge the data in blockCache with the data in blockData and returns the affected cells in changedCell...
An interface for a voxel layer. By implementing this interface, very different modules can contribute...
Definition: VoxelLayer.h:535
virtual BlockVoxelData * fetchData(CellId cell, bool create)=0
Returns the voxel data buffer for the specified cell. This is a synchronous call. ...
virtual BlockVoxelData * fetchData(CellId cell, bool create)
Returns the voxel data buffer for the specified cell.
TMap< CellId, BlockVoxelData * > undoData
Contains the set of Cells that changed in the past modification.
Definition: BlockData.h:321
void removeFromCache(CellId cell)
Removes a cell from the memory cache.
ExternalMutex::Mutex lock
A critical section used to protect the block cache.
Definition: BlockData.h:316
virtual void getContourData(CellId cell, ContourVoxelData *data, bool &empty, void *threadContext) override
Reads the entire Cell contents into a buffer.
virtual BlockVoxelData * fetchCacheData(CellId cell, bool create)=0
Returns a cache voxel data buffer for the specified cell.
virtual void undo(TSet< CellId > &changedCells)
Undoes the last changes.
float y
Vector y coordinate.
Definition: BlockData.h:25
virtual void beginChanges()
Sets a marker for a change set that can be undone.
int getVoxelIntersections(VoxelFarm::Algebra::Vector *result, BlockVoxelData *data, CellId cell, int x, int y, int z)
Returns all triangles that intercept a voxel.
virtual BlockVoxelData * getCell(const CellId &cell, bool create)=0
Get read only voxel data. Returns NULL if data doesn't exist.
virtual void updateLOD(const TSet< CellId > &changedCells)
Recomputes higher LOD data.
virtual void endChanges()=0
Closes the marker for a change set that can be undone.
This interface allows the CBlockData object to persist blocks in an external storage.
Definition: BlockData.h:261
void updateBlockLOD(IBlockData *blocks, CellId cell)
Computes voxel data for a block using its eight octree children.
virtual void disposeThreadContext(void *threadContext) override
Destroys the specified thread context.
virtual void trackCellChanges(CellId cell, const BlockVoxelData *blockData)
Requests changes made to a cell to be remembered so they can be undone.
A structure for storing voxel information when we generate a mask from a mesh.
Definition: BlockData.h:20
float x
Vector x coordinate.
Definition: BlockData.h:23
bool vector
An indication if the voxel has a vector.
Definition: BlockData.h:31
virtual void trackCellChanges(CellId cell, const BlockVoxelData *blockData)=0
Requests changes made to a cell to be remembered so they can be undone.
bool mat
An indication if the voxel has material.
Definition: BlockData.h:29
virtual BlockVoxelData * loadCell(CellId cell)
Loads voxel data for the specified cell.
Definition: BlockData.h:265
float z
Vector z coordinate.
Definition: BlockData.h:27
virtual void trackCellChanges(CellId cell, const BlockVoxelData *blockData)
Requests changes made to a cell to be remembered so they can be undone.
virtual void setBlock(CellId &cell, int &x, int &y, int &z, int material, double dx, double dy, double dz, TSet< CellId > *changedCells)
Sets a value for a voxel.
virtual BlockVoxelData * fetchCacheData(CellId cell, bool create)
Returns a cache voxel data buffer for the specified cell.
An implementation of IBlockData for merging a buffer into another IBlockData.
Definition: BlockData.h:111