Voxomap
A C++11 voxel container.
VoxelNode.hpp
Go to the documentation of this file.
1 #ifndef _VOXOMAP_VOXELNODE_HPP_
2 #define _VOXOMAP_VOXELNODE_HPP_
3 
4 #include <cstdint>
5 #include <map>
6 #include <memory>
7 #include <functional>
8 #include "../octree/Node.hpp"
9 #include "../utils/BoundingBox.hpp"
10 
11 namespace voxomap
12 {
13 
14 template <class T_Container> class VoxelOctree;
15 
20 template <class T_Container>
21 class VoxelNode : public Node<VoxelNode<T_Container>>
22 {
23 public:
25  using VoxelData = typename T_Container::VoxelData;
26  using iterator = typename T_Container::iterator;
28 
33  {
38 
39  std::map<Vector3I, VoxelNode<T_Container>*> nodes;
40  std::pair<VoxelNode<T_Container>*, bool> neighbor_nodes[3][3][3];
41  };
42 
50  VoxelNode(int x, int y, int z, int size);
54  VoxelNode(VoxelNode const& other);
58  virtual ~VoxelNode() = default;
59 
68 
72  unsigned int getNbVoxel() const;
76  bool hasVoxel() const;
88  std::shared_ptr<T_Container> getSharedVoxelContainer();
92  void setVoxelContainer(std::shared_ptr<T_Container> container);
100  iterator findVoxel(int x, int y, int z);
106  bool findVoxel(iterator& it);
114  iterator findRelativeVoxel(int x, int y, int z) const;
124  iterator findRelativeVoxel(NeighborAreaCache& neighbor_cache, int x, int y, int z) const;
131  template <typename... Args>
132  bool addVoxel(iterator& it, Args&&... args);
139  template <typename... Args>
140  bool updateVoxel(iterator& it, Args&&... args);
146  template <typename... Args>
147  void putVoxel(iterator& it, Args&&... args);
154  template <typename... Args>
155  bool removeVoxel(iterator const& it, Args&&... args);
156 
161  void exploreVoxel(std::function<void(iterator const&)> const& predicate) const;
162 
167  void exploreVoxelContainer(std::function<void(typename T_Container::VoxelContainer const&)> const& predicate) const;
168 
173  void exploreVoxelNode(std::function<void(VoxelNode const&)> const& predicate) const;
178  void exploreVoxelNode(std::function<void(VoxelNode&)> const& predicate);
185  void exploreBoundingBox(BoundingBox<int> const& bounding_box,
186  std::function<void(VoxelNode&)> const& in_predicate,
187  std::function<void(VoxelNode&)> const& out_predicate);
188 
193  void copyOnWrite();
198  void merge(VoxelNode& node);
202  bool empty() const;
207  void serialize(std::string& str) const;
214  size_t unserialize(char const* str, size_t strsize);
215 
216 private:
220  uint32_t serializeNode(std::string& str) const;
221 
222  std::shared_ptr<T_Container> _container;
223  friend T_Container;
224 
225 public:
232  inline static int findContainerPosition(int src, int container_id);
238  inline static int findVoxelPosition(int src);
239 };
240 
241 }
242 
243 #include "VoxelNode.ipp"
244 
245 #endif // _VOXOMAP_VOXELNODE_HPP_
voxomap::VoxelNode::VoxelNode
VoxelNode(int x, int y, int z, int size)
Constructs VoxelNode with its properties.
voxomap::VoxelNode::findRelativeVoxel
iterator findRelativeVoxel(int x, int y, int z) const
Search voxel with position relative to the node position.
voxomap::VoxelNode::NeighborAreaCache::neighbor_nodes
std::pair< VoxelNode< T_Container > *, bool > neighbor_nodes[3][3][3]
Cache of neighbor nodes.
Definition: VoxelNode.hpp:40
voxomap
Definition: Node.hpp:8
voxomap::VoxelNode< T >::Container
T Container
Definition: VoxelNode.hpp:24
voxomap::VoxelNode::NeighborAreaCache::nodes
std::map< Vector3I, VoxelNode< T_Container > * > nodes
Cache of nodes.
Definition: VoxelNode.hpp:39
voxomap::VoxelNode::setVoxelContainer
void setVoxelContainer(std::shared_ptr< T_Container > container)
Sets the voxel container.
voxomap::VoxelNode::T_Container
friend T_Container
Definition: VoxelNode.hpp:223
voxomap::VoxelNode::exploreVoxelNode
void exploreVoxelNode(std::function< void(VoxelNode const &)> const &predicate) const
Browse all voxel nodes and call predicate on each.
voxomap::VoxelNode::~VoxelNode
virtual ~VoxelNode()=default
Default destructor.
voxomap::VoxelNode::unserialize
size_t unserialize(char const *str, size_t strsize)
Unserialize str.
voxomap::VoxelNode::findVoxel
bool findVoxel(iterator &it)
Search voxel.
voxomap::VoxelNode::hasVoxel
bool hasVoxel() const
Returns true if node has voxel.
voxomap::VoxelNode::getVoxelContainer
T_Container const * getVoxelContainer() const
Returns the voxel container.
voxomap::VoxelNode::updateVoxel
bool updateVoxel(iterator &it, Args &&... args)
Update the voxel if already exist.
voxomap::VoxelNode::serialize
void serialize(std::string &str) const
Serialize the structure.
voxomap::VoxelNode::exploreVoxelContainer
void exploreVoxelContainer(std::function< void(typename T_Container::VoxelContainer const &)> const &predicate) const
Browse all voxel containers and call predicate on each.
voxomap::VoxelNode< T >::iterator
typename T_Container::iterator iterator
Definition: VoxelNode.hpp:26
voxomap::VoxelNode::serializeNode
uint32_t serializeNode(std::string &str) const
Serialize node in str.
voxomap::VoxelNode::findVoxelPosition
static int findVoxelPosition(int src)
Method to find voxel index inside its container.
voxomap::VoxelNode
Node optimized for voxel.
Definition: VoxelNode.hpp:14
voxomap::VoxelNode::begin
iterator begin()
Returns an iterator to the first voxel of the node.
voxomap::VoxelNode::getVoxelContainer
T_Container * getVoxelContainer()
Returns the voxel container.
voxomap::VoxelNode::NeighborAreaCache
Use by the method findRelativeVoxel to improve performance.
Definition: VoxelNode.hpp:33
voxomap::VoxelNode::merge
void merge(VoxelNode &node)
Merge this with node Add existing voxel in node inside this but don't update voxel that already exist...
voxomap::VoxelNode::exploreVoxel
void exploreVoxel(std::function< void(iterator const &)> const &predicate) const
Browse all voxels and call predicate on each.
voxomap::VoxelNode::exploreBoundingBox
void exploreBoundingBox(BoundingBox< int > const &bounding_box, std::function< void(VoxelNode &)> const &in_predicate, std::function< void(VoxelNode &)> const &out_predicate)
Browse all voxel containers.
voxomap::VoxelNode::getSharedVoxelContainer
std::shared_ptr< T_Container > getSharedVoxelContainer()
Returns a shared pointer to the voxel container.
voxomap::VoxelNode::copyOnWrite
void copyOnWrite()
Copy the voxel container if a modification occured (add/remove/update voxel) and the container is sha...
voxomap::VoxelNode::_container
std::shared_ptr< T_Container > _container
Voxel container.
Definition: VoxelNode.hpp:222
voxomap::VoxelNode::NeighborAreaCache::NeighborAreaCache
NeighborAreaCache()
Default constructor.
voxomap::VoxelNode::exploreVoxelNode
void exploreVoxelNode(std::function< void(VoxelNode &)> const &predicate)
Browse all voxel containers and call predicate on each.
voxomap::Node
Basic node of Octree.
Definition: Node.hpp:18
voxomap::VoxelNode::getNbVoxel
unsigned int getNbVoxel() const
Returns the number of voxels.
voxomap::VoxelNode::empty
bool empty() const
Returns true if there is no voxel.
voxomap::VoxelNode::findRelativeVoxel
iterator findRelativeVoxel(NeighborAreaCache &neighbor_cache, int x, int y, int z) const
Search voxel with position relative to the node position. Use cache structure for improve performance...
voxomap::VoxelNode::putVoxel
void putVoxel(iterator &it, Args &&... args)
Add or update the voxel.
voxomap::VoxelNode::removeVoxel
bool removeVoxel(iterator const &it, Args &&... args)
Remove an existing voxel.
voxomap::VoxelNode::VoxelNode
VoxelNode(VoxelNode const &other)
Copy constructor.
voxomap::VoxelNode::addVoxel
bool addVoxel(iterator &it, Args &&... args)
Add the voxel if not exist.
voxomap::VoxelNode::findVoxel
iterator findVoxel(int x, int y, int z)
Search voxel.
voxomap::VoxelNode< T >::VoxelData
typename T_Container::VoxelData VoxelData
Definition: VoxelNode.hpp:25
voxomap::VoxelNode::end
iterator end()
Returns an iterator to the voxel folowing the last voxel of the node.
voxomap::VoxelNode::findContainerPosition
static int findContainerPosition(int src, int container_id)
Method to find voxel container index inside its container.