voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
accessor.hpp
1 #ifndef PROCEDURAL_VOXEL_TILE_ACCESSOR_HPP
2 #define PROCEDURAL_VOXEL_TILE_ACCESSOR_HPP
3 
4 #include "blub/core/array.hpp"
5 #include "blub/core/globals.hpp"
6 #include "blub/core/scopedPtr.hpp"
7 #include "blub/math/vector2int.hpp"
8 #include "blub/math/vector3int.hpp"
9 #include "blub/serialization/access.hpp"
10 #include "blub/serialization/nameValuePair.hpp"
11 
12 
13 namespace blub
14 {
15 namespace procedural
16 {
17 namespace voxel
18 {
19 namespace tile
20 {
21 
22 
28 template <class configType>
29 class accessor : public base<accessor<configType> >
30 {
31 public:
32  typedef configType t_config;
33  typedef base<accessor<t_config> > t_base;
34  typedef typename t_config::t_data t_voxel;
35 
36 #if defined(BOOST_NO_CXX11_CONSTEXPR)
37  static const int32 voxelLength;
38  static const int32 voxelLengthWithNormalCorrection;
39  static const int32 voxelLengthLod;
40  static const int32 voxelCount;
41  static const int32 voxelCountLod;
42  static const int32 voxelCountLodAll;
43  static const int32 voxelLengthSurface;
44  static const int32 voxelCountSurface;
45 #else
46  static constexpr int32 voxelLength = t_config::voxelsPerTile;
47  static constexpr int32 voxelLengthWithNormalCorrection = voxelLength+3;
48  static constexpr int32 voxelLengthLod = (voxelLength+1)*2;
49  static constexpr int32 voxelCount = voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection;
50  static constexpr int32 voxelCountLod = voxelLengthLod*voxelLengthLod;
51  static constexpr int32 voxelCountLodAll = 6*voxelCountLod;
52  static constexpr int32 voxelLengthSurface = t_config::voxelsPerTile+1;
53  static constexpr int32 voxelCountSurface = voxelLengthSurface*voxelLengthSurface*voxelLengthSurface;
54 #endif
55 
56  typedef vector<t_voxel> t_voxelArray;
57  typedef vector<t_voxel> t_voxelArrayLod;
58 
63  static typename t_base::pointer create()
64  {
65  return typename t_base::pointer(new accessor());
66  }
67 
71  virtual ~accessor()
72  {
73  if (m_calculateLod)
74  {
75  m_voxelsLod.reset();
76  }
77  }
78 
85  bool setVoxel(const vector3int32& pos, const t_voxel& toSet)
86  {
87  BASSERT(pos.x >= -1);
88  BASSERT(pos.y >= -1);
89  BASSERT(pos.z >= -1);
90  BASSERT(pos.x < voxelLengthWithNormalCorrection-1);
91  BASSERT(pos.y < voxelLengthWithNormalCorrection-1);
92  BASSERT(pos.z < voxelLengthWithNormalCorrection-1);
93 
94  if (toSet.getInterpolation() >= 0 && pos >= vector3int32(0) && pos < vector3int32(voxelLengthSurface))
95  {
96  ++m_numVoxelLargerZero;
97  }
98 
99  const int32 index((pos.x+1)*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection + (pos.y+1)*voxelLengthWithNormalCorrection + pos.z+1);
100  const t_voxel oldValue(m_voxels[index]);
101  m_voxels[index] = toSet;
102 
103  return oldValue != toSet;
104  }
105 
114  bool setVoxelLod(const vector3int32& pos, const t_voxel& toSet, const int32& lod)
115  {
116  return setVoxelLod(calculateCoordsLod(pos, lod), toSet, lod);
117  }
118 
124  const t_voxel& getVoxel(const vector3int32& pos) const
125  {
126  BASSERT(pos.x >= -1);
127  BASSERT(pos.y >= -1);
128  BASSERT(pos.z >= -1);
129  BASSERT(pos.x < voxelLengthWithNormalCorrection-1);
130  BASSERT(pos.y < voxelLengthWithNormalCorrection-1);
131  BASSERT(pos.z < voxelLengthWithNormalCorrection-1);
132 
133  const int32 index((pos.x+1)*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection + (pos.y+1)*voxelLengthWithNormalCorrection + pos.z+1);
134  return m_voxels[index];
135  }
143  const t_voxel& getVoxelLod(const vector3int32& pos, const int32& lod) const
144  {
145  return getVoxelLod(calculateCoordsLod(pos, lod), lod);
146  }
147 
153  bool isEmpty() const
154  {
155  return m_numVoxelLargerZero == 0;
156  }
157 
163  bool isFull() const
164  {
165  return m_numVoxelLargerZero == voxelCountSurface;
166  }
167 
172  const bool& getCalculateLod() const
173  {
174  return m_calculateLod;
175  }
176 
182  const int32& getNumVoxelLargerZero() const
183  {
184  return m_numVoxelLargerZero;
185  }
186 
191  const int32& getNumVoxelLargerZeroLod() const
192  {
193  return m_numVoxelLargerZeroLod;
194  }
195 
200  const t_voxelArray& getVoxelArray() const
201  {
202  return m_voxels;
203  }
208  t_voxelArray& getVoxelArray()
209  {
210  return m_voxels;
211  }
217  t_voxelArrayLod* getVoxelArrayLod() const
218  {
219  return m_voxelsLod.get();
220  }
226  t_voxelArrayLod* getVoxelArrayLod()
227  {
228  return m_voxelsLod.get();
229  }
230 
235  void setCalculateLod(const bool& lod)
236  {
237  if (m_calculateLod == lod)
238  {
239  // do nothing
240 // BWARNING("m_calculateLod == lod");
241  return;
242  }
243  m_calculateLod = lod;
244  if (m_calculateLod)
245  {
246  BASSERT(m_voxelsLod == nullptr);
247  m_voxelsLod.reset(new t_voxelArrayLod(6*voxelCountLod)); // [6*voxelCountLod]
248  }
249  else
250  {
251  m_voxelsLod.reset();
252  }
253  }
254 
259  void setNumVoxelLargerZero(const int32& toSet)
260  {
261  m_numVoxelLargerZero = toSet;
262  }
267  void setNumVoxelLargerZeroLod(const int32& toSet)
268  {
269  m_numVoxelLargerZeroLod = toSet;
270  }
271 
272 protected:
277  : m_voxels(voxelCount)
278  , m_voxelsLod(nullptr)
279  , m_calculateLod(false)
280  , m_numVoxelLargerZero(0)
281  , m_numVoxelLargerZeroLod(0)
282  {
283  }
284 
288  bool setVoxelLod(const vector2int32& index, const t_voxel& toSet, const int32& lod)
289  {
290  BASSERT(index >= 0);
291  BASSERT(index < voxelCountLod);
292  BASSERT(m_calculateLod);
293  BASSERT(m_voxelsLod.get() != nullptr);
294 
295  if (toSet.getInterpolation() >= 0)
296  {
297  ++m_numVoxelLargerZeroLod;
298  }
299  const uint32 index_(lod*voxelCountLod + index.x*voxelLengthLod + index.y);
300  const t_voxel oldValue((*m_voxelsLod)[index_]);
301  (*m_voxelsLod)[index_] = toSet;
302 
303  return oldValue != toSet;
304  }
308  const t_voxel& getVoxelLod(const vector2int32& index, const int32& lod) const
309  {
310  BASSERT(index >= 0);
311  BASSERT(index < voxelCountLod);
312  BASSERT(m_calculateLod);
313  return (*m_voxelsLod)[lod*voxelCountLod + index.x*voxelLengthLod + index.y];
314  }
315 
323  static vector2int32 calculateCoordsLod(const vector3int32& pos, const int32& lod)
324  {
325  BASSERT(lod >= 0);
326  BASSERT(lod < 6);
327  BASSERT(pos.x >= 0);
328  BASSERT(pos.y >= 0);
329  BASSERT(pos.z >= 0);
330  BASSERT(pos.x < voxelLengthLod);
331  BASSERT(pos.y < voxelLengthLod);
332  BASSERT(pos.z < voxelLengthLod);
333  BASSERT(pos.x == 0 || pos.y == 0 || pos.z == 0); // 2d!
334 
335  switch (lod)
336  {
337  case 0: // x
338  case 1:
339  BASSERT(pos.x == 0);
340  return vector2int32(pos.y, pos.z);
341  case 2: // y
342  case 3:
343  BASSERT(pos.y == 0);
344  return vector2int32(pos.x, pos.z);
345  case 4: // z
346  case 5:
347  BASSERT(pos.z == 0);
348  return vector2int32(pos.x, pos.y);
349  default:
350  BASSERT(false);
351  }
352  return vector2int32();
353  }
354 
355 private:
356  BLUB_SERIALIZATION_ACCESS
357 
358  template <class formatType>
359  void save(formatType & readWrite, const uint32& version) const
360  {
361  using namespace serialization;
362 
363  (void)version;
364 
365  readWrite << nameValuePair::create("calculateLod", m_calculateLod);
366  }
367  template <class formatType>
368  void load(formatType & readWrite, const uint32& version)
369  {
370  using namespace serialization;
371 
372  (void)version;
373 
374  bool calculateLod;
375  readWrite >> nameValuePair::create("calculateLod", calculateLod);
376  setCalculateLod(calculateLod);
377  }
378 
379  template <class formatType>
380  void serialize(formatType & readWrite, const uint32& version)
381  {
382  using namespace serialization;
383 
384  (void)version;
385 
386  readWrite & nameValuePair::create("numVoxelLargerZero", m_numVoxelLargerZero);
387  readWrite & nameValuePair::create("numVoxelLargerZeroLod", m_numVoxelLargerZeroLod);
388  saveLoad(readWrite, *this, version); // handle m_calculateLod
389  readWrite & nameValuePair::create("voxels", m_voxels); // OPTIMISE gives twice the size in binary format (2 instead of 1)
390 
391  if (m_calculateLod)
392  {
393  BASSERT(!m_voxelsLod.isNull());
394  readWrite & nameValuePair::create("voxelsLod", *m_voxelsLod);
395  }
396  }
397 
398 
399 
400 private:
401  t_voxelArray m_voxels;
402  blub::scopedPointer<t_voxelArrayLod> m_voxelsLod;
403 
404  bool m_calculateLod;
405  int32 m_numVoxelLargerZero;
406  int32 m_numVoxelLargerZeroLod;
407 
408 
409 };
410 
411 
412 #if defined(BOOST_NO_CXX11_CONSTEXPR)
413 template <class voxelType> const int32 accessor<voxelType>::voxelLength = t_config::voxelsPerTile;
414 template <class voxelType> const int32 accessor<voxelType>::voxelLengthWithNormalCorrection = voxelLength+3;
415 template <class voxelType> const int32 accessor<voxelType>::voxelLengthLod = (voxelLength+1)*2;
416 template <class voxelType> const int32 accessor<voxelType>::voxelCount = voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection*voxelLengthWithNormalCorrection;
417 template <class voxelType> const int32 accessor<voxelType>::voxelCountLod = voxelLengthLod*voxelLengthLod;
418 template <class voxelType> const int32 accessor<voxelType>::voxelCountLodAll = 6*voxelCountLod;
419 template <class voxelType> const int32 accessor<voxelType>::voxelLengthSurface = t_config::voxelsPerTile+1;
420 template <class voxelType> const int32 accessor<voxelType>::voxelCountSurface = voxelLengthSurface*voxelLengthSurface*voxelLengthSurface;
421 #else
422 template <class voxelType> constexpr int32 accessor<voxelType>::voxelLength;
423 template <class voxelType> constexpr int32 accessor<voxelType>::voxelLengthWithNormalCorrection;
424 template <class voxelType> constexpr int32 accessor<voxelType>::voxelLengthLod;
425 template <class voxelType> constexpr int32 accessor<voxelType>::voxelCount;
426 template <class voxelType> constexpr int32 accessor<voxelType>::voxelCountLod;
427 template <class voxelType> constexpr int32 accessor<voxelType>::voxelCountLodAll;
428 template <class voxelType> constexpr int32 accessor<voxelType>::voxelLengthSurface;
429 template <class voxelType> constexpr int32 accessor<voxelType>::voxelCountSurface;
430 #endif
431 
432 
433 }
434 }
435 }
436 }
437 
438 
439 
440 
441 #endif // PROCEDURAL_VOXEL_TILE_ACCESSOR_HPP
const t_voxel & getVoxelLod(const vector2int32 &index, const int32 &lod) const
Definition: accessor.hpp:308
const t_voxelArray & getVoxelArray() const
getVoxelArray return voxel-array
Definition: accessor.hpp:200
Definition: customVertexInformation.cpp:193
const int32 & getNumVoxelLargerZeroLod() const
getNumVoxelLargerZeroLod returns number of voxel in lod not minimum.
Definition: accessor.hpp:191
bool setVoxelLod(const vector3int32 &pos, const t_voxel &toSet, const int32 &lod)
setVoxelLod sets a voxel to a lod array.
Definition: accessor.hpp:114
bool isFull() const
isFull returns true if all voxel are maximum.
Definition: accessor.hpp:163
bool isEmpty() const
isEmpty returns true if all voxel are minimum.
Definition: accessor.hpp:153
const int32 & getNumVoxelLargerZero() const
getNumVoxelLargerZero returns number of voxel not minimum.
Definition: accessor.hpp:182
Definition: sharedPointer.hpp:12
static t_base::pointer create()
create creates an instance.
Definition: accessor.hpp:63
virtual ~accessor()
~accessor destructor.
Definition: accessor.hpp:71
const t_voxel & getVoxelLod(const vector3int32 &pos, const int32 &lod) const
getVoxelLod returns ref to lod-voxel
Definition: accessor.hpp:143
bool setVoxelLod(const vector2int32 &index, const t_voxel &toSet, const int32 &lod)
Definition: accessor.hpp:288
const bool & getCalculateLod() const
getCalculateLod returns true if surface later shall calculate level-of-detail
Definition: accessor.hpp:172
void setCalculateLod(const bool &lod)
setCalculateLod enables or disables lod calculation and voxel buffering for it.
Definition: accessor.hpp:235
static vector2int32 calculateCoordsLod(const vector3int32 &pos, const int32 &lod)
calculateCoordsLod converts a 3d lod-coord to a 2d. Transvoxel needs per quader-side (6) only one 2d ...
Definition: accessor.hpp:323
void setNumVoxelLargerZero(const int32 &toSet)
setNumVoxelLargerZero internally used for extern sync. (optimisation)
Definition: accessor.hpp:259
Definition: predecl.hpp:25
accessor()
accessor constructor
Definition: accessor.hpp:276
The data class is the default voxel. Contains an 8-bit interpolation value. Replace/derive it and set...
Definition: data.hpp:27
void setNumVoxelLargerZeroLod(const int32 &toSet)
setNumVoxelLargerZero internally used for extern sync. (optimisation)
Definition: accessor.hpp:267
bool setVoxel(const vector3int32 &pos, const t_voxel &toSet)
setVoxel set ancalculateIndex convertes a 3d voxel-pos to a 1d array-index.
Definition: accessor.hpp:85
t_voxelArrayLod * getVoxelArrayLod() const
getVoxelArrayLod returns nullptr if no lod shall get calculated else 6-lod-arrays of voxels...
Definition: accessor.hpp:217
const t_voxel & getVoxel(const vector3int32 &pos) const
getVoxel return ref to voxel.
Definition: accessor.hpp:124
int8 & getInterpolation()
getInterpolation returns reference to interpolation.
Definition: data.hpp:70
Definition: deadlineTimer.hpp:10
t_voxelArrayLod * getVoxelArrayLod()
getVoxelArrayLod returns nullptr if no lod shall get calculated else 6-lod-arrays of voxels...
Definition: accessor.hpp:226
t_voxelArray & getVoxelArray()
getVoxelArray return voxel-array
Definition: accessor.hpp:208
Definition: customVertexInformation.cpp:177