voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
inMemory.hpp
1 #ifndef VOXEL_SIMPLE_CONTAINER_INMEMORY_HPP
2 #define VOXEL_SIMPLE_CONTAINER_INMEMORY_HPP
3 
4 
5 #include "blub/math/vector3int32map.hpp"
6 #include "blub/procedural/voxel/simple/container/base.hpp"
7 #include "blub/serialization/access.hpp"
8 #include "blub/serialization/nameValuePair.hpp"
9 #include "blub/serialization/saveLoad.hpp"
10 
11 
12 namespace blub
13 {
14 namespace procedural
15 {
16 namespace voxel
17 {
18 namespace simple
19 {
20 namespace container
21 {
22 
23 
31 template <class configType>
32 class inMemory : public base<configType>
33 {
34 public:
35  typedef base<configType> t_base;
36 
37  typedef vector3int32map<typename t_base::t_utilsTile> t_tilesMap;
38 
44  : t_base(worker)
45  {
46 #ifdef BLUB_LOG_VOXEL
47  blub::BOUT("inMemory::inMemory()");
48 #endif
49  }
50 
55  {
56  #ifdef BLUB_LOG_VOXEL
57  blub::BOUT("inMemory::~inMemory()");
58  #endif
59  }
60 
67  typename t_base::t_utilsTile getTileHolder(const blub::vector3int32& id) const override
68  {
69  const axisAlignedBoxInt32 bounds(m_tiles.getBounds().getMinimum(), m_tiles.getBounds().getMaximum() - vector3int32(1));
70  if (bounds.isInside(id))
71  {
72  return m_tiles.getValue(id);
73  }
74  typename t_base::t_utilsTile result;
75  return result;
76  }
77 
82  const t_tilesMap& getTilesMap() const
83  {
84  return m_tiles;
85  }
86 
91  void setTileBounds(const axisAlignedBoxInt32& bounds)
92  {
93  m_tiles.resize(bounds);
94  }
95 
101  {
102  return m_tiles.getBounds();
103  }
104 
105 protected:
112  virtual void setTileToContainerMaster(const typename t_base::t_tileId id, const typename t_base::t_utilsTile &oldOne, const typename t_base::t_utilsTile &toSet)
113  {
114  (void)oldOne;
115 
116  m_tiles.extend(axisAlignedBoxInt32(id, id+vector3int32(1)));
117  m_tiles.setValue(id, toSet);
118  }
119 
125  void setTileMaster(const blub::vector3int32& id, const typename t_base::t_utilsTile& toSet) override
126  {
127 #ifdef BLUB_LOG_VOXEL
128  blub::BOUT("inMemory::setTileMaster id:" + blub::string::number(id));
129 #endif
130 
131  typename t_base::t_utilsTile holder(getTileHolder(id));
132  bool alreadyEmpty(holder.state == utils::tileState::empty);
133 
134  if (toSet.state == utils::tileState::empty && alreadyEmpty)
135  {
136  return;
137  }
138  if (!alreadyEmpty)
139  {
140  if (toSet.state == utils::tileState::full &&
141  holder.state == utils::tileState::full) // nothing will change
142  {
143  return;
144  }
145  }
146  setTileToContainerMaster(id, holder, toSet);
147 
148  if (toSet.state == utils::tileState::partitial)
149  {
150  BASSERT(!toSet.data->isEmpty());
151  BASSERT(!toSet.data->isFull());
152 
153  if (!toSet.data->getEditedVoxelBoundingBox().isValid()) // no voxel changed
154  {
155  toSet.data->endEdit();
156  return;
157  }
158  }
159  t_base::addToChangeList(id, toSet);
160  }
161 
162  void setTileToFullMaster(const vector3int32& id) override
163  {
164 #ifdef BLUB_LOG_VOXEL
165  blub::BOUT("inMemory::setTileToFullMaster id:" + blub::string::number(id));
166 #endif
167  const typename t_base::t_utilsTile holder(utils::tileState::full);
168  setTileMaster(id, holder);
169  }
170  void setTileToEmtpyMaster(const vector3int32& id) override
171  {
172 #ifdef BLUB_LOG_VOXEL
173  blub::BOUT("inMemory::setTileToEmtpyMaster id:" + blub::string::number(id));
174 #endif
175  const typename t_base::t_utilsTile holder(utils::tileState::empty);
176  setTileMaster(id, holder);
177  }
178 
179 protected:
180  BLUB_SERIALIZATION_ACCESS
181 
182  template <class formatType>
183  void save(formatType & readWrite, const uint32& version) const
184  {
185  (void)version;
186 
187  const axisAlignedBoxInt32& bounds(getTileBounds());
188  readWrite & BLUB_SERIALIZATION_NAMEVALUEPAIR(bounds);
189 
190  for (int32 indX = bounds.getMinimum().x; indX < bounds.getMaximum().x; ++indX)
191  {
192  for (int32 indY = bounds.getMinimum().y; indY < bounds.getMaximum().y; ++indY)
193  {
194  for (int32 indZ = bounds.getMinimum().z; indZ < bounds.getMaximum().z; ++indZ)
195  {
196  const vector3int32 id(indX, indY, indZ);
197  const typename t_base::t_utilsTile holder(getTileHolder(id));
198 
199  readWrite & BLUB_SERIALIZATION_NAMEVALUEPAIR(id);
200  readWrite & serialization::nameValuePair::create("state", holder.state);
201 
202  if (holder.state != utils::tileState::partitial)
203  {
204  continue;
205  }
206 
207  readWrite & serialization::nameValuePair::create("tile", *holder.data.data());
208  }
209  }
210  }
211  }
212  template <class formatType>
213  void load(formatType & readWrite, const uint32& version)
214  {
215  (void)version;
216 
218 
219  axisAlignedBoxInt32 bounds;
220  readWrite & BLUB_SERIALIZATION_NAMEVALUEPAIR(bounds);
221 
222  setTileBounds(bounds);
223 
224  for (int32 indX = bounds.getMinimum().x; indX < bounds.getMaximum().x; ++indX)
225  {
226  for (int32 indY = bounds.getMinimum().y; indY < bounds.getMaximum().y; ++indY)
227  {
228  for (int32 indZ = bounds.getMinimum().z; indZ < bounds.getMaximum().z; ++indZ)
229  {
230  vector3int32 id;
231  readWrite & BLUB_SERIALIZATION_NAMEVALUEPAIR(id);
232  BASSERT(id == vector3int32(indX, indY, indZ));
233 
234  typename t_base::t_utilsTile holder;
235  readWrite & serialization::nameValuePair::create("state", holder.state);
236 
237  if (holder.state == utils::tileState::partitial)
238  {
239  holder.data = t_base::createTile();
240  readWrite & serialization::nameValuePair::create("tile", *holder.data.data());
241  }
242 
243  setTile(id, holder);
244  }
245  }
246  }
247 
249  }
250  template <class formatType>
251  void serialize(formatType & readWrite, const uint32& version)
252  {
253  using namespace serialization;
254 
255  saveLoad(readWrite, *this, version);
256  }
257 
258 private:
259  t_tilesMap m_tiles;
260 
261 };
262 
263 
264 }
265 }
266 }
267 }
268 }
269 
270 
271 #endif // VOXEL_SIMPLE_CONTAINER_INMEMORY_HPP
void setTileBounds(const axisAlignedBoxInt32 &bounds)
setTileBounds sets the size for the map. Call for optimization, if you know the tile-dimensions befor...
Definition: inMemory.hpp:91
const axisAlignedBoxInt32 & getTileBounds() const
getTileBounds returns the tile bounds.
Definition: inMemory.hpp:100
virtual t_tilePtr createTile() const
createTile creates a new Tile. Uses callback set by setCreateTileCallback()
void setTile(const t_tileId &id, const t_utilsTile &toSet)
editVoxel edits the container. Its guranteed that the edits are getting in order of calling this meth...
Definition: base.hpp:87
void setTileToEmtpyMaster(const vector3int32 &id) override
setTileToFullMaster sets a tiles voxel to minimum - for memory optimizations. Method gets called by a...
Definition: inMemory.hpp:170
Definition: axisAlignedBoxInt32.hpp:12
Definition: dispatcher.hpp:29
~inMemory()
~inMemory descructor
Definition: inMemory.hpp:54
const t_tilesMap & getTilesMap() const
getTilesMap returns all tiles.
Definition: inMemory.hpp:82
void lockForEdit()
lockForEdit locks the class for editing/writing it. Call unlockForEdit() after work done...
t_base::t_utilsTile getTileHolder(const blub::vector3int32 &id) const override
getTileHolder returns a utils::tileHolder setted by setTile() or by editVoxel(). Read-lock class befo...
Definition: inMemory.hpp:67
void setTileToFullMaster(const vector3int32 &id) override
setTileToFullMaster sets a tiles voxel to maximum - for memory optimizations. Method gets called by a...
Definition: inMemory.hpp:162
void unlockForEdit()
unlockForEdit unlocks edit/write-lock and calls signalEditDone() . Method executes unlock by the disp...
void setTileMaster(const blub::vector3int32 &id, const typename t_base::t_utilsTile &toSet) override
setTileMaster sets a tile to an id. Call by one thread at a time.
Definition: inMemory.hpp:125
Definition: deadlineTimer.hpp:10
inMemory(blub::async::dispatcher &worker)
inMemory constructor
Definition: inMemory.hpp:43
virtual void setTileToContainerMaster(const typename t_base::t_tileId id, const typename t_base::t_utilsTile &oldOne, const typename t_base::t_utilsTile &toSet)
setTileToContainerMaster replaces a tile. Call method by one thread at a time. Write-lock class befor...
Definition: inMemory.hpp:112