Skip to content

Commit

Permalink
Octree: Cleanup and more
Browse files Browse the repository at this point in the history
Signed-off-by: Squareys <[email protected]>
  • Loading branch information
Squareys committed Mar 30, 2018
1 parent 4809db5 commit a107dd0
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 351 deletions.
21 changes: 13 additions & 8 deletions src/Magnum/Octree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,31 @@
# DEALINGS IN THE SOFTWARE.
#

cmake_minimum_required(VERSION 2.8.9)
cmake_minimum_required(VERSION 2.8.12)

find_package(Magnum REQUIRED Shapes)
find_package(Magnum REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CORRADE_CXX_FLAGS}")

include_directories(${MAGNUM_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
set(MagnumOctree_SOURCES
instantiation.cpp)

set(MagnumOctree_HEADERS
Octree.h
Octree.hpp

visibility.h)

add_custom_target(MagnumOctree SOURCES ${MagnumOctree_HEADERS})
target_link_libraries(MagnumOctree ${MAGNUM_LIBRARIES} ${MAGNUM_SHAPES_LIBRARIES})
add_library(MagnumOctree STATIC ${MagnumOctree_SOURCES} ${MagnumOctree_HEADERS})
target_include_directories(MagnumOctree PUBLIC
${PROJECT_SOURCE_DIR}/src
${PROJECT_BINARY_DIR}/src)
target_link_libraries(MagnumOctree Magnum::Magnum)

install(TARGETS MagnumOctree
RUNTIME DESTINATION ${MAGNUM_BINARY_INSTALL_DIR}
LIBRARY DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR}
ARCHIVE DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR})
install(FILES ${MagnumOctree_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Octree)

if(BUILD_TESTS)
add_subdirectory(Test)
endif()

140 changes: 67 additions & 73 deletions src/Magnum/Octree/Octree.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,103 +29,101 @@

/** @file
* @brief Class @ref Magnum::Octree::Octree
*
* @author Andrea Capobianco
*/

#include <Magnum/Magnum.h>
#include <Magnum/Math/Frustum.h>
#include <Magnum/Math/Range.h>
#include <Magnum/Math/Vector3.h>
#include <Magnum/Math/Vector4.h>
#include <Magnum/Shapes/AxisAlignedBox.h>

#include <vector>
#include "visibility.h"
#include "Magnum/Octree/visibility.h"

namespace Magnum { namespace Octree {


enum class OctantStatus: Byte {
NoCorners = 0, /**< No corners of the octant are inside the testing volume */
SomeCorners = 1, /**< Neither all nor no corners of the octant are inside the testing volume */
AllCorners = 2, /**< All corners of the octant are inside the testing volume */
};

/**
* @brief Octree @see https://en.wikipedia.org/wiki/Octree
* An octree is a cubic structure, which is subdivided in up to 8 smaller cubes, called octants.
* Each octant can be seen as an octree itself. Data is stored in the leaf octants (Octants without children).
*/
@brief Octree
An octree is a cubic structure, which is subdivided in up to 8 smaller cubes,
called octants. Each octant can be seen as an octree itself. Data is stored
in the leaf octants (octants without children).
*/
template<typename T>
class /* MAGNUM_OCTREE_EXPORT */Octree {
class Octree {
public:

/** @brief Default constructor */
Octree() :
_center { },
_radius(),
_depth(),
_isLeafNode()
{
for(int i = 0; i < 8; ++i) {
_children[i] = nullptr;
}
}
_children{}
{}

/** @brief Copying is not allowed */
Octree(const Octree&)=delete;

/** @brief Default destructor */
~Octree() {}
~Octree()=default;

/** @brief Get the center of the octree */
Vector3 center() const {
return _center;
}
const Vector3& center() const { return _center; }

/** @brief Get the radius of the octree. */
Float radius() const {
return _radius;
}
Float radius() const { return _radius; }

/** @brief Get the depth of the octree */
Int depth() const {
return _depth;
}
Int depth() const { return _depth; }

/** @brief Get the bounding boxes in this octree node */
const std::vector<Shapes::AxisAlignedBox3D>& entries() const {
return _entries;
}
const std::vector<Range3D>& entries() const { return _entries; }

/** @brief Get the data contained in this octree node */
const std::vector<T>& data() const {
return _data;
}
const std::vector<T>& data() const { return _data; }

/** @brief Get the i-th child of this octree */
Octree<T>* child(int i) const {
return _children[i];
}
Octree<T>* child(int i) const { return _children[i]; }

/** @brief Check if the octree is a leaf node. An octree is a leaf node if it has no children. */
bool isLeafNode() const {
return _isLeafNode;
}
/**
* @brief Check if the octree is a leaf node.
*
* An octree is a leaf node if it has no children.
*/
bool isLeafNode() const { return _isLeafNode; }

/** @brief Set if octree is a leaf node. An octree is a leaf node if it has no children. */
/**
* @brief Set if octree is a leaf node.
*
* An octree is a leaf node if it has no children.
*/
void setLeafNode(bool isLeafNode) {
_isLeafNode = isLeafNode;
}

/** @brief Whether this node is empty */
bool empty() const { return _data.empty(); }

/**
* @brief Build the octree for the passed points with a maximal depth of maxDepth
* @brief Build the octree for points and a maximal depth
* @param points The points that should get stored in the octree
* @param maxDepth The maximal depth of the octree
* @return Reference to self (for method chaining)
*/
Octree<T>& build(const std::vector<Shapes::AxisAlignedBox3D>& entries, const std::vector<T>& data, const Int maxDepth) {
Octree<T>& build(const std::vector<Range3D>& entries, const std::vector<T>& data, const Int maxDepth) {
CORRADE_ASSERT(data.size() == entries.size(), "Entries and data need to be same size.", *this);

auto entryItor = entries.begin();
auto entryEnd = entries.end();
auto dataItor = data.begin();

createInitialBounds(entries, maxDepth);

while(entryItor != entryEnd) {
insert(*entryItor, *dataItor);
entryItor++;
dataItor++;
auto dataItor = data.begin();
for(const auto& entry : entries) {
insert(entry, *dataItor);
++dataItor;
}

return *this;
Expand All @@ -136,17 +134,18 @@ class /* MAGNUM_OCTREE_EXPORT */Octree {
* @param point The point that should be found
* @return The index of the child potentially containing the point
*/
Int getOctantContainingBox(const Shapes::AxisAlignedBox3D& point) const;
Int octantContainingBox(const Range3D& point) const;

/**
* @brief Get all points contained in the part of the octree which is inside the bounds spanned by min and max
* @brief Get all points contained of the octree within given bounds
* @param found All the points found in the specified bounds
* @param min The minimal corner of the search bounds
* @param max The maximal corner of the search bounds
* @return Reference to self (for method chaining)
*/
Octree<T>& points(std::vector<T>& resultData, const Vector3& min, const Vector3& max);
Octree<T>& points(std::vector<T>& resultData, const Vector4 (&frustum)[6]);
const Octree<T>& points(std::vector<T>& resultData, const Range3D& range) const;
const Octree<T>& points(std::vector<T>& resultData, const Math::Frustum<Float>& frustum) const;
const Octree<T>& points(std::vector<T>& resultData) const;

private:
/**
Expand All @@ -155,33 +154,28 @@ class /* MAGNUM_OCTREE_EXPORT */Octree {
* @param radius The radius of the octree
* @param depth The depth of the octree
*/
Octree(Vector3 center, Float radius, Int depth, bool isLeafNode) :
_center { center },
_radius(radius),
_depth(depth),
_isLeafNode(isLeafNode)
{
for(int i = 0; i < 8; ++i) {
_children[i] = nullptr;
}
}

Octree<T>& createInitialBounds(const std::vector<Shapes::AxisAlignedBox3D>& points, Int maxDepth);
Octree<T>& insert(const Shapes::AxisAlignedBox3D& point, T data);
Int cubeInFrustum(const Vector4 (&frustum)[6]);
bool boxInFrustum(const Shapes::AxisAlignedBox3D& point, const Vector4 (&frustum)[6]);
Octree(const Vector3& center, Float radius, Int depth, bool isLeafNode) :
_center{center},
_radius(radius),
_depth(depth),
_isLeafNode(isLeafNode),
_children{}
{}

void createInitialBounds(const std::vector<Range3D>& points, Int maxDepth);
void insert(const Range3D& point, T data);
OctantStatus cubeInFrustum(const Math::Frustum<Float>& frustum) const;

Vector3 _center;
Float _radius;
Int _depth;
bool _isLeafNode;

Octree<T>* _children[8];
std::vector<Shapes::AxisAlignedBox3D> _entries;
std::vector<Range3D> _entries;
std::vector<T> _data;

};

}}

#endif /* OCTREE_H_ */
#endif
Loading

0 comments on commit a107dd0

Please sign in to comment.