Grid.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_DATASTRUCTURES_GRID_H
#define SURGSIM_DATASTRUCTURES_GRID_H
#include <array>
#include <unordered_map>
#include "SurgSim/Math/Vector.h"
namespace SurgSim
{
namespace DataStructures
{
template <size_t N>
struct powerOf3
{
enum { value = 3 * powerOf3<N-1>::value };
};
template <>
struct powerOf3<0>
{
enum { value = 1 };
};
template <typename T, size_t N>
class Grid
{
public:
Grid(const Eigen::Matrix<double, N, 1>& cellSize, const Eigen::AlignedBox<double, N>& bounds);
virtual ~Grid();
void reset();
template <class Derived>
void addElement(const T element, const Eigen::MatrixBase<Derived>& position);
const std::vector<T>& getNeighbors(const T& element);
template <class Derived>
const std::vector<T>& getNeighbors(const Eigen::MatrixBase<Derived>& position);
protected:
typedef struct
{
std::vector<T> elements;
std::vector<T> neighbors;
} CellContent;
typedef Eigen::Matrix<int, N, 1> NDId;
class NDIdHash
{
public:
size_t operator()(const NDId& nd) const;
};
std::unordered_map<NDId, CellContent, NDIdHash> m_activeCells;
std::unordered_map<T, NDId> m_cellIds;
Eigen::Matrix<double, N, 1> m_size;
Eigen::AlignedBox<double, N> m_aabb;
bool m_neighborsDirtyFlag;
private:
void update();
void getNeighborsCellIds(NDId cellId,
std::array<NDId, powerOf3<N>::value>* cellsIds);
bool isNdGreaterOrEqual(const NDId& a, const NDId& b);
};
}; // namespace DataStructures
}; // namespace SurgSim
#include "SurgSim/DataStructures/Grid-inl.h"
#endif // SURGSIM_DATASTRUCTURES_GRID_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::powerOf3
Templated function to compute a power of 3 at compile time (useful for template parameter) ...
Definition: Grid.h:32
SurgSim::DataStructures::Grid::m_aabb
Eigen::AlignedBox< double, N > m_aabb
Grid min and max.
Definition: Grid.h:107
SurgSim::DataStructures::Grid::m_size
Eigen::Matrix< double, N, 1 > m_size
Size of each cell (same on all dimension)
Definition: Grid.h:104
SurgSim::DataStructures::Grid::CellContent::neighbors
std::vector< T > neighbors
Definition: Grid.h:84
Grid-inl.h
SurgSim::DataStructures::Grid::m_neighborsDirtyFlag
bool m_neighborsDirtyFlag
Does the neighbors needs to be recomputed ?
Definition: Grid.h:110
SurgSim::DataStructures::Grid::CellContent
Data structure for a cell&#39;s content (the list of elements and the list of all the neighbors) ...
Definition: Grid.h:81
SurgSim::DataStructures::powerOf3::value
Definition: Grid.h:34
SurgSim::DataStructures::Grid::NDIdHash
Enable the NDId to be used as a key in an unordered map.
Definition: Grid.h:91
SurgSim::DataStructures::Grid::m_cellIds
std::unordered_map< T, NDId > m_cellIds
Mapping from element to cell id containing the element.
Definition: Grid.h:101
Vector.h
Definitions of small fixed-size vector types.
SurgSim::DataStructures::Grid
n-dimensional grid structure with uniform non-cubic cells This data structure is useful to search for...
Definition: Grid.h:47
SurgSim::DataStructures::Grid::CellContent::elements
std::vector< T > elements
Definition: Grid.h:83
SurgSim::DataStructures::Grid::NDId
Eigen::Matrix< int, N, 1 > NDId
The type of the n-dimensional cell Id.
Definition: Grid.h:88
SurgSim::DataStructures::getNeighbors
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:216
SurgSim::DataStructures::Grid::m_activeCells
std::unordered_map< NDId, CellContent, NDIdHash > m_activeCells
Active cells (referenced by their ids (spatial hashing)) with their content.
Definition: Grid.h:98