voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
vector3int32map.hpp
1 #ifndef VECTOR3INT32MAP_HPP
2 #define VECTOR3INT32MAP_HPP
3 
4 #include "blub/math/axisAlignedBoxInt32.hpp"
5 #include "blub/core/noncopyable.hpp"
6 
7 
8 namespace blub
9 {
10 
11 template <typename dataType>
13 {
14 public:
16  : m_data(nullptr)
17  {
18  ;
19  }
20 
22  {
23  if (m_data != nullptr)
24  {
25  delete [] m_data;
26  }
27  }
28 
29  void setValue(const vector3int32& pos, const dataType& toSet)
30  {
31  const int32 index(convertToIndex(pos));
32  BASSERT(index < m_bounds.getVolume());
33  m_data[index] = toSet;
34  }
35  const dataType& getValue(const vector3int32& pos) const
36  {
37  const int32 index(convertToIndex(pos));
38  return m_data[index];
39  }
40 
41 
42  void resize(const axisAlignedBoxInt32& aabb)
43  {
44  axisAlignedBoxInt32 resizedAabb(m_bounds);
45  resizedAabb.extend(aabb);
46  if (resizedAabb == m_bounds)
47  {
48  return;
49  }
50  const axisAlignedBoxInt32 oldBounds(m_bounds);
51  const vector3int32 oldStart(oldBounds.getMinimum());
52  const vector3int32 oldEnd(oldBounds.getMaximum());
53  const dataType* oldData(m_data);
54 
55  m_bounds = resizedAabb;
56  BASSERT(m_bounds.getVolume() > 0);
57  m_data = new dataType[m_bounds.getVolume()];
58 
59  if (oldData != nullptr)
60  {
61  for (int32 indX = oldStart.x; indX < oldEnd.x; ++indX)
62  {
63  for (int32 indY = oldStart.y; indY < oldEnd.y; ++indY)
64  {
65  for (int32 indZ = oldStart.z; indZ < oldEnd.z; ++indZ)
66  {
67  m_data[convertToIndex(vector3int32(indX, indY, indZ))] = oldData[convertToIndex(vector3int32(indX, indY, indZ), oldBounds)];
68  }
69  }
70  }
71  delete [] oldData;
72  }
73  }
74 
75  void extend(const vector3int32& toExtend)
76  {
77  axisAlignedBoxInt32 resizedAabb(m_bounds);
78  resizedAabb.extend(toExtend);
79  resize(resizedAabb);
80  }
81  void extend(const axisAlignedBoxInt32 &toExtend)
82  {
83  axisAlignedBoxInt32 resizedAabb(m_bounds);
84  resizedAabb.extend(toExtend);
85  resize(resizedAabb);
86  }
87 
88  const axisAlignedBoxInt32& getBounds(void) const
89  {
90  return m_bounds;
91  }
92 
93 protected:
94 
95  int32 convertToIndex(const vector3int32& toConvert) const
96  {
97  return convertToIndex(toConvert, m_bounds);
98  }
99  static int32 convertToIndex(const vector3int32& toConvert, const axisAlignedBoxInt32& bounds)
100  {
101  BASSERT(toConvert >= bounds.getMinimum());
102  BASSERT(toConvert < bounds.getMaximum());
103 
104  const vector3int32 size(bounds.getSize());
105  const vector3int32 min(bounds.getMinimum());
106  return (toConvert.x-min.x)*size.y*size.z + (toConvert.y-min.y)*size.z + (toConvert.z-min.z);
107  }
108 
109  dataType *m_data;
110  axisAlignedBoxInt32 m_bounds;
111 
112 
113 };
114 
115 }
116 
117 
118 #endif // VECTOR3INT32MAP_HPP
Definition: vector3int32map.hpp:12
Definition: axisAlignedBoxInt32.hpp:12
Definition: noncopyable.hpp:10
Definition: deadlineTimer.hpp:10