voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
container.hpp
1 #ifndef PROCEDURAL_VOXEL_TILE_CONTAINER_HPP
2 #define PROCEDURAL_VOXEL_TILE_CONTAINER_HPP
3 
4 #include "blub/core/array.hpp"
5 #include "blub/core/classVersion.hpp"
6 #include "blub/core/vector.hpp"
7 #include "blub/math/axisAlignedBoxInt32.hpp"
8 #include "blub/math/vector3int.hpp"
9 #include "blub/procedural/voxel/tile/base.hpp"
10 #include "blub/serialization/access.hpp"
11 #include "blub/serialization/nameValuePair.hpp"
12 
13 
14 namespace blub
15 {
16 namespace procedural
17 {
18 namespace voxel
19 {
20 namespace tile
21 {
22 
23 
29 template <class configType>
30 class container : public base<container<configType> >
31 {
32 public:
33  typedef configType t_config;
34  typedef base<container<t_config> > t_base;
35  typedef typename t_config::t_data t_data;
36 
37 #if defined(BOOST_NO_CXX11_CONSTEXPR)
38  static const int32 voxelLength;
39  static const int32 voxelCount;
40 #else
41  static constexpr int32 voxelLength = t_config::voxelsPerTile;
42  static constexpr int32 voxelCount = voxelLength*voxelLength*voxelLength;
43 #endif
44 
45  typedef vector<t_data> t_voxelArray;
46 
51  static typename t_base::pointer create()
52  {
53  return new container();
54  }
58  virtual ~container()
59  {
60  }
61 
65  void startEdit()
66  {
67  BASSERT(!m_editing);
68  m_changedVoxelBoundingBox.setInvalid();
69  m_editing = true;
70  }
74  void endEdit()
75  {
76  BASSERT(m_editing);
77  m_editing = false;
78  }
83  const bool& getEditing() const
84  {
85  return m_editing;
86  }
87 
93  {
94  return m_changedVoxelBoundingBox;
95  }
96 
104  void setVoxel(const vector3int32& pos, const t_data& toSet)
105  {
106  if(setVoxel(calculateIndex(pos), toSet))
107  {
108  m_changedVoxelBoundingBox.extend(pos);
109  }
110  }
111  // TODO remove me - do lambda / boost/std::function
112  void setVoxelIfInterpolationHigher(const vector3int32& pos, const t_data& toSet)
113  {
114  if(setVoxelIfInterpolationHigher(calculateIndex(pos), toSet))
115  {
116  m_changedVoxelBoundingBox.extend(pos);
117  }
118  }
119  // TODO remove me - do lambda / boost/std::function
120  void setVoxelIfInterpolationHigherEqualZero(const vector3int32& pos, const t_data& toSet)
121  {
122  if(setVoxelIfInterpolationHigherEqualZero(calculateIndex(pos), toSet))
123  {
124  m_changedVoxelBoundingBox.extend(pos);
125  }
126  }
127  // TODO remove me - do lambda / boost/std::function
128  void setVoxelIfInterpolationLower(const vector3int32& pos, const t_data& toSet)
129  {
130  if(setVoxelIfInterpolationLower(calculateIndex(pos), toSet))
131  {
132  m_changedVoxelBoundingBox.extend(pos);
133  }
134  }
135 
140  void setFull()
141  {
142  BASSERT(m_editing);
143 
144  for (uint32 indVoxel = 0; indVoxel < m_voxels.size(); ++indVoxel)
145  {
146  m_voxels[indVoxel].setMax();
147  }
148  m_countVoxelInterpolationLargerZero = voxelCount;
149  m_countVoxelMinimum = 0;
150  m_countVoxelMaximum = voxelCount;
151  }
156  void setEmpty()
157  {
158  BASSERT(m_editing);
159 
160  for (uint32 indVoxel = 0; indVoxel < m_voxels.size(); ++indVoxel)
161  {
162  m_voxels[indVoxel].setMin();
163  }
164  m_countVoxelInterpolationLargerZero = 0;
165  m_countVoxelMinimum = voxelCount;
166  m_countVoxelMaximum = 0;
167  }
168 
174  t_data getVoxel(const vector3int32& pos) const
175  {
176  return getVoxel(calculateIndex(pos));
177  }
184  const t_data& getVoxel(const int32& index) const
185  {
186  BASSERT(index >= 0);
187  BASSERT(index < voxelCount);
188  return m_voxels[index];
189  }
194  const t_voxelArray& getVoxelArray(void) const
195  {
196  return m_voxels;
197  }
198 
204  static int32 calculateIndex(const vector3int32& pos)
205  {
206  BASSERT(pos.x >= 0);
207  BASSERT(pos.y >= 0);
208  BASSERT(pos.z >= 0);
209  BASSERT(pos.x < voxelLength);
210  BASSERT(pos.y < voxelLength);
211  BASSERT(pos.z < voxelLength);
212  return (pos.x)*(voxelLength*voxelLength) + (pos.y)*(voxelLength) + (pos.z);
213  }
214 
220  const int32& getCountVoxelLargerZero() const
221  {
222  BASSERT(m_countVoxelInterpolationLargerZero >= 0);
223  BASSERT(m_countVoxelInterpolationLargerZero <= voxelCount);
224  return m_countVoxelInterpolationLargerZero;
225  }
231  const int32& getCountVoxelMaximum() const
232  {
233  BASSERT(m_countVoxelMaximum >= 0);
234  BASSERT(m_countVoxelMaximum <= voxelCount);
235  return m_countVoxelMaximum;
236  }
242  const int32& getCountVoxelMinimum() const
243  {
244  BASSERT(m_countVoxelMinimum >= 0);
245  BASSERT(m_countVoxelMinimum <= voxelCount);
246  return m_countVoxelMinimum;
247  }
248 
254  bool isEmpty() const
255  {
256  return m_countVoxelMinimum == container::voxelCount;
257  }
258 
264  bool isFull() const
265  {
266  return m_countVoxelMaximum == container::voxelCount;
267  }
268 
273  void operator = (const container& other)
274  {
275  m_countVoxelInterpolationLargerZero = other.getCountVoxelLargerZero();
276  m_countVoxelMinimum = other.getCountVoxelMinimum();
277  m_countVoxelMaximum = other.getCountVoxelMaximum();
278  m_voxels = other.getVoxelArray();
279  }
280 
281 protected:
286  : m_voxels(voxelCount)
287  , m_countVoxelInterpolationLargerZero(0)
288  , m_countVoxelMinimum(voxelCount)
289  , m_countVoxelMaximum(0)
290  , m_editing(false)
291  {
292  ;
293  }
294 
295  // TODO remove me - do lambda / boost/std::function
296  bool setVoxelIfInterpolationHigher(const int32& index, const t_data& toSet)
297  {
298  BASSERT(m_editing);
299  BASSERT(index >= 0);
300  BASSERT(index < voxelCount);
301 
302  const int8& currentInterpolation(getVoxel(index).getInterpolation());
303  if (currentInterpolation >= toSet.getInterpolation())
304  {
305  return false;
306  }
307  return setVoxel(index, toSet);
308  }
309 
310  // TODO remove me - do lambda / boost/std::function
311  bool setVoxelIfInterpolationHigherEqualZero(const int32& index, const t_data& toSet)
312  {
313  BASSERT(m_editing);
314  BASSERT(index >= 0);
315  BASSERT(index < voxelCount);
316 
317  const int8& currentInterpolation(getVoxel(index).getInterpolation());
318  if (currentInterpolation >= 0)
319  {
320  return false;
321  }
322  return setVoxel(index, toSet);
323  }
324 
325  // TODO remove me - do lambda / boost/std::function
326  bool setVoxelIfInterpolationLower(const int32& index, const t_data& toSet)
327  {
328  BASSERT(m_editing);
329  BASSERT(index >= 0);
330  BASSERT(index < voxelCount);
331 
332  const int8& currentInterpolation(getVoxel(index).getInterpolation());
333  if (currentInterpolation <= toSet.getInterpolation())
334  {
335  return false;
336  }
337  return setVoxel(index, toSet);
338  }
339 
348  bool setVoxel(const int32& index, const t_data& toSet)
349  {
350  BASSERT(m_editing);
351  BASSERT(index >= 0);
352  BASSERT(index < voxelCount);
353 
354  const t_data& currentVoxel(getVoxel(index));
355 
356  if (currentVoxel == toSet)
357  {
358  return false;
359  }
360 
361  // count for full/empty voxel --> memory optimisation
362  if (currentVoxel.getInterpolation() < 0 && toSet.getInterpolation() >= 0)
363  {
364  ++m_countVoxelInterpolationLargerZero;
365  }
366  else
367  {
368  if (currentVoxel.getInterpolation() >= 0 && toSet.getInterpolation() < 0)
369  {
370  --m_countVoxelInterpolationLargerZero;
371  }
372  }
373  if (!currentVoxel.isMin() && toSet.isMin())
374  {
375  ++m_countVoxelMinimum;
376  }
377  else
378  {
379  if (currentVoxel.isMin() && !toSet.isMin())
380  {
381  --m_countVoxelMinimum;
382  }
383  }
384  if (!currentVoxel.isMax() && toSet.isMax())
385  {
386  ++m_countVoxelMaximum;
387  }
388  else
389  {
390  if (currentVoxel.isMax() && !toSet.isMax())
391  {
392  --m_countVoxelMaximum;
393  }
394  }
395 
396  // actually set the voxel
397  m_voxels[index] = toSet;
398 
399  return true;
400  }
401 
402 private:
403  BLUB_SERIALIZATION_ACCESS
404 
405  template <class formatType>
406  void serialize(formatType & readWrite, const uint32& version)
407  {
408  using namespace serialization;
409 
410  (void)version;
411 
412  readWrite & nameValuePair::create("countVoxelMinimum", m_countVoxelMinimum);
413  readWrite & nameValuePair::create("countVoxelMaximum", m_countVoxelMaximum);
414  readWrite & nameValuePair::create("countVoxelLargerZero", m_countVoxelInterpolationLargerZero);
415  readWrite & nameValuePair::create("editing", m_editing);
416  readWrite & nameValuePair::create("changedVoxelBoundingBox", m_changedVoxelBoundingBox);
417  readWrite & nameValuePair::create("voxels", m_voxels);
418  }
419 
420 private:
421  t_voxelArray m_voxels;
422 
423  int32 m_countVoxelInterpolationLargerZero;
424  int32 m_countVoxelMinimum;
425  int32 m_countVoxelMaximum;
426 
427  bool m_editing;
428  axisAlignedBoxInt32 m_changedVoxelBoundingBox;
429 
430 };
431 
432 
433 #if defined(BOOST_NO_CXX11_CONSTEXPR)
434 template <class voxelType>
435 const int32 container<voxelType>::voxelLength = t_config::voxelsPerTile;
436 template <class voxelType>
437 const int32 container<voxelType>::voxelCount = voxelLength*voxelLength*voxelLength;
438 #else
439 template <class voxelType>
440 constexpr int32 container<voxelType>::voxelLength;
441 template <class voxelType>
442 constexpr int32 container<voxelType>::voxelCount;
443 #endif
444 
445 
446 }
447 }
448 }
449 }
450 
451 
452 
453 
454 #endif // PROCEDURAL_VOXEL_TILE_CONTAINER_HPP
bool setVoxel(const int32 &index, const t_data &toSet)
setVoxel sets voxel to array and counts if voxel is maximum or minimum
Definition: container.hpp:348
const t_data & getVoxel(const int32 &index) const
getVoxel returns a copy of a local voxel.
Definition: container.hpp:184
Definition: customVertexInformation.cpp:193
const bool & getEditing() const
getEditing returns if editing is active.
Definition: container.hpp:83
const axisAlignedBoxInt32 & getEditedVoxelBoundingBox() const
getEditedVoxelBoundingBox returns an axisAlignedBox which describes the bounds of the voxel that chan...
Definition: container.hpp:92
void endEdit()
endEdit end edit
Definition: container.hpp:74
virtual ~container()
~container destructor.
Definition: container.hpp:58
const t_voxelArray & getVoxelArray(void) const
getVoxelArray returns reference voxel-array
Definition: container.hpp:194
const int32 & getCountVoxelMaximum() const
getCountVoxelMaximum returns number of voxel that are maximum.
Definition: container.hpp:231
void setFull()
setFull sets all voxel to max.
Definition: container.hpp:140
static int32 calculateIndex(const vector3int32 &pos)
calculateIndex convertes a 3d voxel-pos to a 1d array-index. 0 <= pos.xyz < voxelLength ...
Definition: container.hpp:204
The container class contains an array of voxel. The amount of voxel per tile is voxelLength^3. The class counts how many voxel are max and how many are min. if all voxel are min or max the class simple::container::base doesnt save them. Additionally it saves an axisAlignedBox which describes the bounds of the voxel that changed.
Definition: predecl.hpp:19
const int32 & getCountVoxelLargerZero() const
getCountVoxelLargerZero returns number of voxel not minimum.
Definition: container.hpp:220
Definition: sharedPointer.hpp:12
Definition: axisAlignedBoxInt32.hpp:12
static t_base::pointer create()
create creates an instance.
Definition: container.hpp:51
void setVoxel(const vector3int32 &pos, const t_data &toSet)
setVoxel sets an voxel to a local position. Extends changed axisAlignedBox-bounds and counts if voxel...
Definition: container.hpp:104
void operator=(const container &other)
operator = copy operator
Definition: container.hpp:273
void setEmpty()
setFull sets all voxel to max.
Definition: container.hpp:156
bool isMax() const
isMax checks if all values are maximum. See class description.
Definition: data.hpp:88
bool isFull() const
isFull returns true if all voxel are maximum.
Definition: container.hpp:264
container()
container constructor
Definition: container.hpp:285
bool isMin() const
isMin checks if all values are minimum. See class description.
Definition: data.hpp:97
bool isEmpty() const
isEmpty returns true if all voxel are minimum.
Definition: container.hpp:254
void startEdit()
startEdit resets the changed-voxel-bound-aab
Definition: container.hpp:65
t_data getVoxel(const vector3int32 &pos) const
getVoxel returns a copy of a local voxel.
Definition: container.hpp:174
int8 & getInterpolation()
getInterpolation returns reference to interpolation.
Definition: data.hpp:70
Definition: deadlineTimer.hpp:10
const int32 & getCountVoxelMinimum() const
getCountVoxelMinimum returns number of voxel that are minimum.
Definition: container.hpp:242
Definition: customVertexInformation.cpp:177