voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
sender.hpp
1 #ifndef NETWORK_SYNC_VOXEL_ACCESSOR_TERRAIN_SENDER_HPP
2 #define NETWORK_SYNC_VOXEL_ACCESSOR_TERRAIN_SENDER_HPP
3 
4 #include "blub/async/dispatcher.hpp"
5 #include "blub/async/predecl.hpp"
6 #include "blub/core/byteArray.hpp"
7 #include "blub/core/globals.hpp"
8 #include "blub/core/signal.hpp"
9 #include "blub/core/vector.hpp"
10 #include "blub/log/global.hpp"
11 #include "blub/math/axisAlignedBox.hpp"
12 #include "blub/procedural/predecl.hpp"
13 #include "blub/procedural/voxel/terrain/accessor.hpp"
14 #include "blub/procedural/voxel/tile/container.hpp"
15 #include "blub/sync/voxel/accessor/multipleTiles/base.hpp"
16 #include "blub/sync/voxel/accessor/multipleTiles/sender.hpp"
17 
18 
19 namespace blub
20 {
21 namespace sync
22 {
23 namespace voxel
24 {
25 namespace accessor
26 {
27 namespace terrain
28 {
29 
30 
31 template <class voxelType, class identifierType>
32 class sender
33 {
34 public:
36  typedef identifierType t_identifier;
38  typedef t_multipleTiles* t_multipleTilesPtr;
42 
45 
47 
48 
49  sender(async::dispatcher &worker,
50  const t_syncRadiusList& syncRadien,
51  t_terrainAccessor* tiles)
52  : m_syncRadien(syncRadien)
53  {
54  BASSERT(tiles != nullptr);
55  BASSERT(tiles->getNumLod() <= (int32)syncRadien.size());
56 
57  real voxelSize(1.);
58 
59  for (int32 indLod = 0; indLod < tiles->getNumLod(); ++indLod)
60  {
61  const auto callback(boost::bind(&sender::isInRange, this, _1, _2, indLod));
62 
63  t_simpleAccessor* toWork(tiles->getLod(indLod));
64  t_multipleTilesPtr lod(new t_multipleTiles(worker, voxelSize, callback, toWork));
65  voxelSize*=2.;
66  m_multipleTiles.push_back(lod);
67 
68  lod->signalSendTileData()->connect(boost::bind(&sender::lodWantsToSendTileData, this, _1, _2, indLod));
69  }
70  }
71  virtual ~sender()
72  {
73  for (t_multipleTilesPtr toDelete : m_multipleTiles)
74  {
75  delete toDelete;
76  }
77  m_multipleTiles.clear();
78  }
79 
80  t_multipleTilesPtr getLodSync(const uint16& lod) const
81  {
82  return m_multipleTiles.at(lod);
83  }
84 
85  // add/update/remove sync-reveiver
86  void addSyncReceiver(t_receiverIdentifierPtr receiver, const vector3& pos)
87  {
88  for (t_multipleTilesPtr work : m_multipleTiles)
89  {
90  work->addSyncReceiver(receiver, pos);
91  }
92  }
93  void updateSyncReceiver(t_receiverIdentifierPtr receiver, const vector3& pos)
94  {
95  for (t_multipleTilesPtr work : m_multipleTiles)
96  {
97  work->updateSyncReceiver(receiver, pos);
98  }
99  }
100  void removeSyncReceiver(t_receiverIdentifierPtr receiver)
101  {
102  for (t_multipleTilesPtr work : m_multipleTiles)
103  {
104  work->removeSyncReceiver(receiver);
105  }
106  }
107 
108  // to "send sync" signals
110  t_sigSendTileData* signalSendTileData()
111  {
112  return &m_sigSendTileData;
113  }
114 
115 protected:
116  void lodWantsToSendTileData(t_receiverIdentifierPtr rec, t_tileDataPtr data, const uint32 &lodInd)
117  {
118  std::ostringstream result;
119  {
121 
122  format << lodInd;
123  format << *data.get();
124  } // flush happens here
125  t_tileDataPtr dataWithLodInformation(new byteArray(result.str().c_str(), result.str().size()));
126 
127  m_sigSendTileData(rec, dataWithLodInformation);
128  }
129  bool isInRange(const vector3& posLeafCenter, const axisAlignedBox& octreeNode, const uint32& lod)
130  {
131  const vector3 sizeLeaf(t_tileContainer::voxelLength);
132  const vector3 sizeLeafDoubled(sizeLeaf*2);
133  const vector3 octreeNodeTwiceMinimum((octreeNode.getMinimum()/sizeLeafDoubled).getFloor()*sizeLeafDoubled);
134  const axisAlignedBox octreeNodeTwice(octreeNodeTwiceMinimum, octreeNodeTwiceMinimum + octreeNode.getSize()*2.);
135 
136  const vector3 posLeafCenterScaled((posLeafCenter / sizeLeafDoubled).getFloor());
137 
138  if (lod != 0 && false)
139  {
140  if (octreeNode.getSize() == sizeLeaf)
141  {
142  const real radius(m_syncRadien[lod-1] / 2.);
143  blub::sphere coll2(posLeafCenter, radius);
144  if (coll2.intersects(octreeNode))
145  {
146  return false;
147  }
148  }
149  }
150 
151  const real radius(m_syncRadien[lod]);
152  blub::sphere coll(posLeafCenterScaled * sizeLeafDoubled + sizeLeaf, radius);
153  const bool result(coll.intersects(octreeNodeTwice));
154 
155  return result;
156  }
157 
158 protected:
159  t_syncRadiusList m_syncRadien;
160  t_multipleTilesList m_multipleTiles;
161 
162  t_sigSendTileData m_sigSendTileData;
163 
164 };
165 
166 
167 }
168 }
169 }
170 }
171 }
172 
173 
174 #endif // NETWORK_SYNC_VOXEL_ACCESSOR_TERRAIN_SENDER_HPP
The base class gets derived by every class in the namespace simple::*. It represends one level of det...
Definition: predecl.hpp:30
t_lod getLod(const uint16 &lod) const
getLod returns a level of detail.
Definition: base.hpp:94
vector3 getSize(void) const
Gets the size of the box.
Definition: axisAlignedBox.cpp:57
bool intersects(const sphere &s) const
Definition: sphere.cpp:9
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
Definition: portable_binary_oarchive.hpp:63
Definition: sharedPointer.hpp:12
Definition: dispatcher.hpp:29
Definition: vector3.hpp:26
Definition: axisAlignedBox.hpp:20
const vector3 & getMinimum(void) const
Definition: axisAlignedBox.hpp:126
Definition: byteArray.hpp:17
int32 getNumLod() const
getNumLod returns number of level of details.
Definition: base.hpp:107
Definition: deadlineTimer.hpp:10
The accessor class contains a custom amount of level of details of type simple::accessor.
Definition: predecl.hpp:60
Definition: customVertexInformation.cpp:177
Definition: sphere.hpp:10