OctreeNode.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_OCTREENODE_H
#define SURGSIM_DATASTRUCTURES_OCTREENODE_H
#include <array>
#include <memory>
#include <functional>
#include "SurgSim/DataStructures/EmptyData.h"
#include "SurgSim/Framework/Asset.h"
#include "SurgSim/Math/Vector.h"
#include "SurgSim/Math/Aabb.h"
namespace SurgSim
{
namespace Math
{
class OctreeShape;
}
namespace DataStructures
{
typedef std::vector<size_t> OctreePath;
class OctreePathHash
{
public:
size_t operator()(const OctreePath& path) const
{
size_t result = 0;
for (auto i : path)
{
result = (result << 3) | i;
}
return m_hasher(result);
}
private:
std::hash<size_t> m_hasher;
};
enum Neighborhood
{
NEIGHBORHOOD_NONE = 0x0,
NEIGHBORHOOD_FACE = 0x1,
NEIGHBORHOOD_EDGE = 0x2,
NEIGHBORHOOD_VERTEX = 0x4,
NEIGHBORHOOD_ALL = 0x1 | 0x2 | 0x4
};
enum Symbol
{
SYMBOL_HALT = -1,
SYMBOL_DOWN = 0,
SYMBOL_UP = 1,
SYMBOL_RIGHT = 2,
SYMBOL_LEFT = 3,
SYMBOL_BACK = 4,
SYMBOL_FRONT = 5
};
OctreePath getNeighbor(const OctreePath& origin, const std::array<Symbol, 3>& direction);
std::vector<OctreePath> getNeighbors(const OctreePath& origin, int type);
template <typename Data>
class OctreeNodePlyReaderDelegate;
template<class Data>
class OctreeNode : public SurgSim::Framework::Asset,
public std::enable_shared_from_this<OctreeNode<Data>>
{
friend class SurgSim::Math::OctreeShape;
friend class SurgSim::DataStructures::OctreeNodePlyReaderDelegate<Data>;
public:
typedef Eigen::AlignedBox<double, 3> AxisAlignedBoundingBox;
OctreeNode();
OctreeNode(const OctreeNode& other);
template <class T>
OctreeNode(const OctreeNode<T>& other);
std::string getClassName() const override;
explicit OctreeNode(const SurgSim::Math::Aabbd& boundingBox);
virtual ~OctreeNode();
const SurgSim::Math::Aabbd& getBoundingBox() const;
bool isActive() const;
void setIsActive(bool isActive);
bool hasChildren() const;
void subdivide();
bool addData(const SurgSim::Math::Vector3d& position, const int level, const Data& nodeData = Data());
std::array<std::shared_ptr<OctreeNode<Data>>, 8>& getChildren();
const std::array<std::shared_ptr<OctreeNode<Data>>, 8>& getChildren() const;
std::shared_ptr<OctreeNode<Data>> getChild(size_t index);
const std::shared_ptr<OctreeNode<Data>> getChild(size_t index) const;
// the last node on a given path, otherwise it will throw.
virtual std::shared_ptr<OctreeNode<Data>> getNode(const OctreePath& path, bool returnLastValid = false);
Data data;
protected:
bool doAddData(const SurgSim::Math::Vector3d& position, const Data& nodeData, const int level,
const int currentLevel);
bool doLoad(const std::string& filePath) override;
SurgSim::Math::Aabbd m_boundingBox;
bool m_isActive;
bool m_hasChildren;
std::array<std::shared_ptr<OctreeNode<Data>>, 8> m_children;
private:
static std::string m_className;
};
}; // namespace DataStructures
}; // namespace SurgSim
#include "SurgSim/DataStructures/OctreeNode-inl.h"
#endif // SURGSIM_DATASTRUCTURES_OCTREENODE_H
SurgSim::DataStructures::SYMBOL_RIGHT
Definition: OctreeNode.h:81
SurgSim::DataStructures::NEIGHBORHOOD_FACE
Definition: OctreeNode.h:69
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::OctreeNode::m_boundingBox
SurgSim::Math::Aabbd m_boundingBox
The bounding box of the current OctreeNode.
Definition: OctreeNode.h:239
SurgSim::Framework::Asset
This class is used to facilitate file loading.
Definition: Asset.h:39
OctreeNode-inl.h
SurgSim::DataStructures::OctreePathHash::operator()
size_t operator()(const OctreePath &path) const
Definition: OctreeNode.h:51
SurgSim::DataStructures::OctreePathHash
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
Aabb.h
SurgSim::DataStructures::NEIGHBORHOOD_VERTEX
Definition: OctreeNode.h:71
SurgSim::DataStructures::SYMBOL_UP
Definition: OctreeNode.h:80
SurgSim::DataStructures::getNeighbor
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:155
SurgSim::DataStructures::OctreePath
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
SurgSim::DataStructures::NEIGHBORHOOD_EDGE
Definition: OctreeNode.h:70
SurgSim::DataStructures::NEIGHBORHOOD_ALL
Definition: OctreeNode.h:72
Asset.h
SurgSim::DataStructures::OctreePathHash::m_hasher
std::hash< size_t > m_hasher
Definition: OctreeNode.h:62
SurgSim::DataStructures::SYMBOL_BACK
Definition: OctreeNode.h:83
SurgSim::Math::Aabbd
Eigen::AlignedBox< double, 3 > Aabbd
Wrapper around the Eigen type.
Definition: Aabb.h:30
SurgSim::DataStructures::OctreeNodePlyReaderDelegate
Subclass the OctreeNodePLyReaderDelegateBase class to enable processing of the templated data...
Definition: OctreeNode.h:113
SurgSim::DataStructures::NEIGHBORHOOD_NONE
Definition: OctreeNode.h:68
SurgSim::DataStructures::SYMBOL_DOWN
Definition: OctreeNode.h:79
SurgSim::DataStructures::SYMBOL_FRONT
Definition: OctreeNode.h:84
Vector.h
Definitions of small fixed-size vector types.
SurgSim::DataStructures::OctreeNode::m_hasChildren
bool m_hasChildren
True if the node has children.
Definition: OctreeNode.h:245
SurgSim::DataStructures::OctreeNode::m_isActive
bool m_isActive
True if there is any data inside this node, including data held by children, children&#39;s children...
Definition: OctreeNode.h:242
SurgSim::DataStructures::OctreeNode::AxisAlignedBoundingBox
Eigen::AlignedBox< double, 3 > AxisAlignedBoundingBox
Bounding box type for convenience.
Definition: OctreeNode.h:140
SurgSim::DataStructures::SYMBOL_HALT
Definition: OctreeNode.h:78
SurgSim::DataStructures::SYMBOL_LEFT
Definition: OctreeNode.h:82
SurgSim::DataStructures::Neighborhood
Neighborhood
Indicates what neighbors to grab.
Definition: OctreeNode.h:66
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::OctreeNode::m_children
std::array< std::shared_ptr< OctreeNode< Data > >, 8 > m_children
The children of this node.
Definition: OctreeNode.h:248
SurgSim::DataStructures::OctreeNode::data
Data data
Extra node data.
Definition: OctreeNode.h:224
SurgSim::DataStructures::OctreeNode::m_className
static std::string m_className
Definition: OctreeNode.h:251
SurgSim::Math::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
SurgSim::DataStructures::OctreeNode
Octree data structure.
Definition: OctreeNode.h:131
SurgSim::Math::OctreeShape
Octree Shape A defined by an octree data structure.
Definition: OctreeShape.h:34
SurgSim::DataStructures::Symbol
Symbol
Direction code for the neighborhood search.
Definition: OctreeNode.h:76
EmptyData.h