1 #ifndef VOXEL_SIMPLE_CONTAINER_BASE_HPP
2 #define VOXEL_SIMPLE_CONTAINER_BASE_HPP
4 #include "blub/core/sharedPointer.hpp"
5 #include "blub/math/axisAlignedBox.hpp"
6 #include "blub/math/axisAlignedBoxInt32.hpp"
7 #include "blub/math/transform.hpp"
8 #include "blub/procedural/voxel/simple/base.hpp"
9 #include "blub/procedural/voxel/simple/container/utils/tile.hpp"
29 template <
class configType>
30 class base :
public simple::base<typename configType::t_container::t_tile>
35 typedef typename configType::t_container::t_tile t_tile;
36 typedef simple::base<t_tile> t_base;
37 typedef sharedPointer<t_tile> t_tilePtr;
38 typedef utils::tile<t_tile> t_utilsTile;
40 typedef sharedPointer<edit::base<t_config>
const> t_editConstPtr;
41 typedef sharedPointer<edit::base<t_config> > t_editPtr;
44 typedef hashMap<t_tileId, t_utilsTile> t_tilesGotChangedMap;
52 , m_numInTilesInTask(0)
87 void setTile(
const t_tileId&
id,
const t_utilsTile& toSet)
98 virtual t_utilsTile
getTileHolder(
const t_tileId&
id)
const = 0;
119 if (workTile.state == utils::tileState::full)
124 if (workTile.state == utils::tileState::empty)
149 vector3int32 result(voxelPos % t_config::voxelsPerTile);
150 if (voxelPos.x < 0 && voxelPos.x%t_config::voxelsPerTile != 0)
152 result.x = t_config::voxelsPerTile + result.x;
154 if (voxelPos.y < 0 && voxelPos.y%t_config::voxelsPerTile != 0)
156 result.y = t_config::voxelsPerTile + result.y;
158 if (voxelPos.z < 0 && voxelPos.z%t_config::voxelsPerTile != 0)
160 result.z = t_config::voxelsPerTile + result.z;
165 const t_tilesGotChangedMap &getTilesThatGotEdited()
const
167 return m_tilesThatGotEdited;
171 void addToChangeList(
const t_tileId &
id, t_utilsTile toAdd)
173 BASSERT(!t_base::m_classLocker.tryLockForWrite());
174 m_tilesThatGotEdited.insert(
id, toAdd);
179 if (!m_tilesThatGotEdited.empty())
181 t_base::m_sigEditDone();
189 m_tilesThatGotEdited.clear();
196 m_tilesThatGotEdited.clear();
224 t_editConstPtr edit_;
236 #ifdef BLUB_LOG_VOXEL
237 blub::BOUT(
"base::editVoxel");
240 editTodo edit(change, trans);
242 m_editsTodo.push_back(edit);
253 #ifdef BLUB_LOG_VOXEL
254 blub::BOUT(
"base::doNextEditMaster");
256 BASSERT(m_numInTilesInTask >= 0);
258 if (m_editsTodo.empty())
262 if (m_numInTilesInTask > 0)
272 BASSERT(m_numInTilesInTask == 0);
274 editTodo edit(*m_editsTodo.begin());
275 m_editsTodo.erase(m_editsTodo.begin());
276 t_editConstPtr change(edit.edit_);
286 for (blub::int32 indX = startEdit.x; indX < endEdit.x; ++indX)
288 for (blub::int32 indY = startEdit.y; indY < endEdit.y; ++indY)
290 for (blub::int32 indZ = startEdit.z; indZ < endEdit.z; ++indZ)
295 ++m_numInTilesInTask;
312 #ifdef BLUB_LOG_VOXEL
313 blub::BOUT(
"base::editVoxelTS id:" + blub::string::number(
id));
316 if (holder.state == utils::tileState::partitial)
318 workTile = holder.data;
322 workTile =
createTileFull(holder.state == utils::tileState::full);
325 if (!workTile->getEditing())
327 workTile->startEdit();
329 change->calculateVoxel(workTile.data(), id, trans);
332 if (workTile->isEmpty())
334 result.state = utils::tileState::empty;
338 if (workTile->isFull())
340 result.state = utils::tileState::full;
344 result.state = utils::tileState::partitial;
345 result.data = workTile;
360 --m_numInTilesInTask;
361 if (m_numInTilesInTask == 0)
363 if (m_editsTodo.isEmpty())
366 for (
const typename t_tilesGotChangedMap::value_type& work : getTilesThatGotEdited())
368 if (!work.second.data.isNull())
370 work.second.data->endEdit();
386 virtual void setTileMaster(
const t_tileId&
id,
const t_utilsTile& toSet) = 0;
395 #ifdef BLUB_LOG_VOXEL
396 blub::BOUT(
"base::getOrCreateTile id:" + blub::string::number(
id));
400 if (foundTile.state == utils::tileState::empty)
404 if (foundTile.state == utils::tileState::full)
408 return foundTile.data;
418 #ifdef BLUB_LOG_VOXEL
419 blub::BOUT(
"base::createTile id: full:" + blub::string::number(full));
421 t_tilePtr newOne(t_tile::create());
457 int32 m_numInTilesInTask;
460 t_editTodoList m_editsTodo;
463 t_tilesGotChangedMap m_tilesThatGotEdited;
474 #endif // VOXEL_SIMPLE_CONTAINER_BASE_HPP
virtual void lockForEditMaster()
lockForEditMaster locks for write, or waits until possible. Call by master dispatcher.
Definition: base.hpp:228
virtual void setTileToEmtpyMaster(const t_tileId &id)=0
setTileToFullMaster sets a tiles voxel to minimum - for memory optimizations. Method gets called by a...
Definition: customVertexInformation.cpp:193
static void calculateAffectetedTilesByAabb(const axisAlignedBoxInt32 &voxelAabb, t_tileId &startResult, t_tileId &endResult)
calculateAffectetedTilesByAabb caluclates a list of affected tiles by an axisAlignedBox. Used to determine which tiles to recalculate for an edit.
Definition: base.hpp:450
static t_tilePtr createTileFull(const bool &full)
createTileFull creates a tile in which all voxel are maximum or minimum.
Definition: base.hpp:416
void setMax()
setMax sets all values to maximum. See class description.
Definition: data.hpp:113
void setMin()
setMin sets all values to minimum. See class description.
Definition: data.hpp:105
void doNextEditMaster(const bool &alreadyLocked=false)
doNextEditMaster finds out which tiles the edit affects and dispaches the change to the worker-thread...
Definition: base.hpp:251
void lockForEditMaster() override
lockForEditMaster locks for write, or waits until possible. Call by master dispatcher.
Definition: base.hpp:193
vector3int32 t_tileId
Definition: base.hpp:44
static vector3int32 calculateVoxelPosInTile(const vector3int32 &voxelPos)
calculateVoxelPosInTile converts an absolute voxel-position to an relative container-confirm position...
Definition: base.hpp:147
The editTodo struct holds the edit information, for the dispatcher. A primitive buffer for the paramt...
Definition: base.hpp:202
void unlockForEditMaster() override
unlockForEditMaster unlocks write. Call by master dispatcher.
Definition: base.hpp:176
void editMaster(t_editConstPtr change, const blub::transform &trans)
editMaster saves the edit in a qeue and starts to handle it.
Definition: base.hpp:234
virtual bool tryLockForEditMaster()
tryLockForEditMaster tries to lock for write. Call by master dispatcher.
Definition: base.hpp:217
virtual void unlockForEditMaster()
unlockForEditMaster unlocks write. Call by master dispatcher.
Definition: base.hpp:236
void editVoxel(t_editConstPtr change, const transform &trans=blub::transform())
editVoxel edits the container. Its guranteed that the edits are getting in order of calling this meth...
Definition: base.hpp:64
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
Definition: axisAlignedBoxInt32.hpp:12
void editVoxelWorker(t_editConstPtr change, const t_utilsTile &holder, const blub::vector3int32 &id, const blub::transform &trans)
editVoxelWorker affects with change holder. Method gets called paralell by various threads...
Definition: base.hpp:310
void editVoxelDoneMaster(t_utilsTile tileHolder_, const blub::vector3int32 &id)
editVoxelDoneMaster gets called after edit got applied on the tile. Call method only by one thread at...
Definition: base.hpp:356
static t_tileId calculateVoxelPosToTileId(const vector3int32 &voxelPos)
calculateVoxelPosToTileId converts an absolute voxel-position to an relative container-id position...
Definition: base.hpp:137
virtual t_utilsTile getTileHolder(const t_tileId &id) const =0
getTileHolder returns a utils::tileHolder setted by setTile() or by editVoxel(). Read-lock class befo...
Definition: dispatcher.hpp:29
virtual void setTileToFullMaster(const t_tileId &id)=0
setTileToFullMaster sets a tiles voxel to maximum - for memory optimizations. Method gets called by a...
t_tilePtr getOrCreateTile(const vector3int32 &id)
getOrCreateTile looks up if tile already exists. If not creates one.
Definition: base.hpp:393
Definition: vector3.hpp:26
Definition: axisAlignedBox.hpp:20
The data class is the default voxel. Contains an 8-bit interpolation value. Replace/derive it and set...
Definition: data.hpp:27
bool tryLockForEditMaster() override
tryLockForEditMaster tries to lock for write. Call by master dispatcher.
Definition: base.hpp:184
blub::async::dispatcher & m_worker
m_worker use it to dispatch heavy work. Don't write to class member with it. Do not use any locks...
Definition: base.hpp:148
t_utilsTile getTileHolderByVoxelPosition(const blub::vector3int32 &pos) const
getTileHolderByVoxelPosition returns a utils::tileHolder setted by setTile() or by editVoxel()...
Definition: base.hpp:104
t_voxel getVoxel(const vector3int32 &voxelPos) const
getVoxel returns an voxel. Read-lock the class before call.
Definition: base.hpp:114
Definition: deadlineTimer.hpp:10
blub::async::strand m_master
m_master The master synchronises jobs for the worker-thread and writes to class member. The master calls all methods which end with *Master.
Definition: base.hpp:143
Definition: customVertexInformation.cpp:177
virtual void setTileMaster(const t_tileId &id, const t_utilsTile &toSet)=0
gets called by setTile. Call only by one thread at a time. Write-lock class before.
base(blub::async::dispatcher &worker)
base constructor.
Definition: base.hpp:50