opensurgsim
Grid.h
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_GRID_H
17 #define SURGSIM_DATASTRUCTURES_GRID_H
18 
19 #include <array>
20 #include <unordered_map>
21 
22 #include "SurgSim/Math/Vector.h"
23 
24 namespace SurgSim
25 {
26 
27 namespace DataStructures
28 {
29 
31 template <size_t N>
32 struct powerOf3
33 {
34  enum { value = 3 * powerOf3<N-1>::value };
35 };
36 template <>
37 struct powerOf3<0>
38 {
39  enum { value = 1 };
40 };
41 
46 template <typename T, size_t N>
47 class Grid
48 {
49 public:
53  Grid(const Eigen::Matrix<double, N, 1>& cellSize, const Eigen::AlignedBox<double, N>& bounds);
54 
56  virtual ~Grid();
57 
59  void reset();
60 
65  template <class Derived>
66  void addElement(const T element, const Eigen::MatrixBase<Derived>& position);
67 
71  const std::vector<T>& getNeighbors(const T& element);
72 
76  template <class Derived>
77  const std::vector<T>& getNeighbors(const Eigen::MatrixBase<Derived>& position);
78 
79 protected:
81  typedef struct
82  {
83  std::vector<T> elements;
84  std::vector<T> neighbors;
85  } CellContent;
86 
88  typedef Eigen::Matrix<int, N, 1> NDId;
89 
91  class NDIdHash
92  {
93  public:
94  size_t operator()(const NDId& nd) const;
95  };
96 
98  std::unordered_map<NDId, CellContent, NDIdHash> m_activeCells;
99 
101  std::unordered_map<T, NDId> m_cellIds;
102 
104  Eigen::Matrix<double, N, 1> m_size;
105 
107  Eigen::AlignedBox<double, N> m_aabb;
108 
111 
112 private:
114  void update();
115 
119  void getNeighborsCellIds(NDId cellId,
120  std::array<NDId, powerOf3<N>::value>* cellsIds);
121 
125  bool isNdGreaterOrEqual(const NDId& a, const NDId& b);
126 };
127 
128 }; // namespace DataStructures
129 }; // namespace SurgSim
130 
131 #include "SurgSim/DataStructures/Grid-inl.h"
132 
133 #endif // SURGSIM_DATASTRUCTURES_GRID_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Templated function to compute a power of 3 at compile time (useful for template parameter) ...
Definition: Grid.h:32
Eigen::AlignedBox< double, N > m_aabb
Grid min and max.
Definition: Grid.h:107
Eigen::Matrix< double, N, 1 > m_size
Size of each cell (same on all dimension)
Definition: Grid.h:104
bool m_neighborsDirtyFlag
Does the neighbors needs to be recomputed ?
Definition: Grid.h:110
Data structure for a cell&#39;s content (the list of elements and the list of all the neighbors) ...
Definition: Grid.h:81
Enable the NDId to be used as a key in an unordered map.
Definition: Grid.h:91
std::unordered_map< T, NDId > m_cellIds
Mapping from element to cell id containing the element.
Definition: Grid.h:101
Definitions of small fixed-size vector types.
n-dimensional grid structure with uniform non-cubic cells This data structure is useful to search for...
Definition: Grid.h:47
Eigen::Matrix< int, N, 1 > NDId
The type of the n-dimensional cell Id.
Definition: Grid.h:88
std::unordered_map< NDId, CellContent, NDIdHash > m_activeCells
Active cells (referenced by their ids (spatial hashing)) with their content.
Definition: Grid.h:98