OctreeNode.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_OCTREENODE_H
17 #define SURGSIM_DATASTRUCTURES_OCTREENODE_H
18 
19 #include <array>
20 #include <memory>
21 #include <functional>
22 
25 #include "SurgSim/Math/Vector.h"
26 #include "SurgSim/Math/Aabb.h"
27 
28 
29 namespace SurgSim
30 {
31 
32 namespace Math
33 {
34 class OctreeShape;
35 }
36 
37 namespace DataStructures
38 {
39 
43 typedef std::vector<size_t> OctreePath;
44 
49 {
50 public:
51  size_t operator()(const OctreePath& path) const
52  {
53  size_t result = 0;
54  for (auto i : path)
55  {
56  result = (result << 3) | i;
57  }
58 
59  return m_hasher(result);
60  }
61 private:
62  std::hash<size_t> m_hasher;
63 };
64 
67 {
72  NEIGHBORHOOD_ALL = 0x1 | 0x2 | 0x4
73 };
74 
76 enum Symbol
77 {
80  SYMBOL_UP = 1,
85 };
86 
103 OctreePath getNeighbor(const OctreePath& origin, const std::array<Symbol, 3>& direction);
104 
110 std::vector<OctreePath> getNeighbors(const OctreePath& origin, int type);
111 
127 template<class Data>
129  public std::enable_shared_from_this<OctreeNode<Data>>
130 {
132 
133 public:
134 
136  typedef Eigen::AlignedBox<double, 3> AxisAlignedBoundingBox;
137 
139  OctreeNode();
140 
143  OctreeNode(const OctreeNode& other);
144 
149  template <class T>
150  OctreeNode(const OctreeNode<T>& other);
151 
154  explicit OctreeNode(const SurgSim::Math::Aabbd& boundingBox);
155 
157  virtual ~OctreeNode();
158 
161  const SurgSim::Math::Aabbd& getBoundingBox() const;
162 
165  bool isActive() const;
166 
169  void setIsActive(bool isActive);
170 
173  bool hasChildren() const;
174 
178  void subdivide();
179 
187  bool addData(const SurgSim::Math::Vector3d& position, const Data& nodeData, const int level);
188 
191  std::array<std::shared_ptr<OctreeNode<Data> >, 8>& getChildren();
192 
195  const std::array<std::shared_ptr<OctreeNode<Data> >, 8>& getChildren() const;
196 
201  std::shared_ptr<OctreeNode<Data> > getChild(size_t index);
202 
207  const std::shared_ptr<OctreeNode<Data> > getChild(size_t index) const;
208 
213  // the last node on a given path, otherwise it will throw.
215  virtual std::shared_ptr<OctreeNode<Data> > getNode(const OctreePath& path, bool returnLastValid = false);
216 
218  Data data;
219 
220 protected:
227  bool doAddData(const SurgSim::Math::Vector3d& position, const Data& nodeData, const int level,
228  const int currentLevel);
229 
230  virtual bool doLoad(const std::string& filePath) override;
231 
234 
237 
240 
242  std::array<std::shared_ptr<OctreeNode<Data> >, 8> m_children;
243 };
244 
245 
249 std::shared_ptr<OctreeNode<SurgSim::DataStructures::EmptyData>> loadOctree(const std::string& fileName);
250 
251 }; // namespace DataStructures
252 }; // namespace SurgSim
253 
255 
256 #endif // SURGSIM_DATASTRUCTURES_OCTREENODE_H
Definition: OctreeNode.h:81
Definition: DriveElementFromInputBehavior.cpp:27
SurgSim::Math::Aabbd m_boundingBox
The bounding box of the current OctreeNode.
Definition: OctreeNode.h:233
This class is used to facilitate file loading.
Definition: Asset.h:33
virtual bool doLoad(const std::string &filePath) override
Derived classes will overwrite this method to do actual loading.
Definition: OctreeShape.cpp:41
Enable the OctreePath to be used as a key in an unordered map, if the int range is exceeded this will...
Definition: OctreeNode.h:48
Definition: OctreeNode.h:80
SurgSim::DataStructures::OctreePath getNeighbor(const OctreePath &origin, const std::array< Symbol, 3 > &direction)
Calculate the neighbor of an node in the octree by traversing a state machine, see http://ww1...
Definition: OctreeNode.cpp:179
std::vector< size_t > OctreePath
Typedef of octree path The path is a vector of children indexes (each within 0 to 7) that lead to the...
Definition: OctreeNode.h:43
size_t operator()(const OctreePath &path) const
Definition: OctreeNode.h:51
string(TOUPPER ${DEVICE}DEVICE_UPPER_CASE) option(BUILD_DEVICE_ $
Definition: CMakeLists.txt:35
std::hash< size_t > m_hasher
Definition: OctreeNode.h:62
Definition: OctreeNode.h:83
Eigen::AlignedBox< double, 3 > Aabbd
Wrapper around the Eigen type.
Definition: Aabb.h:30
Definition: OctreeNode.h:79
Definition: OctreeNode.h:84
std::shared_ptr< OctreeNode< EmptyData > > loadOctree(const std::string &fileName)
A free function to load an octree from file.
Definition: OctreeNode.cpp:146
Definitions of small fixed-size vector types.
bool m_hasChildren
True if the node has children.
Definition: OctreeNode.h:239
bool m_isActive
True if there is any data inside this node, including data held by children, children&#39;s children...
Definition: OctreeNode.h:236
Eigen::AlignedBox< double, 3 > AxisAlignedBoundingBox
Bounding box type for convenience.
Definition: OctreeNode.h:136
Definition: OctreeNode.h:78
Definition: OctreeNode.h:82
Neighborhood
Indicates what neighbors to grab.
Definition: OctreeNode.h:66
std::vector< OctreePath > getNeighbors(const OctreePath &origin, int type)
Fetch a list of neighbors, indicated by the type, Face, Edge and Vertex are possible types and can be...
Definition: OctreeNode.cpp:240
std::array< std::shared_ptr< OctreeNode< Data > >, 8 > m_children
The children of this node.
Definition: OctreeNode.h:242
Data data
Extra node data.
Definition: OctreeNode.h:218
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:56
Octree data structure.
Definition: OctreeNode.h:128
Octree Shape A defined by an octree data structure.
Definition: OctreeShape.h:34
Symbol
Direction code for the neighborhood search.
Definition: OctreeNode.h:76