Skip to content

Commit

Permalink
1. Fixed some issues in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMalashenko committed Aug 26, 2015
1 parent 0dab08e commit eb6d088
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 53 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
This is a small library that implements a octree.

The main features

*N-dimentional octree

*exact object search
*nearest object search
*N nearest objects search
*functor query
*object search using functors

*multithreading
73 changes: 50 additions & 23 deletions examples/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct TETRAHEDRON {

struct WRAPPER_CLASS;
typedef OCTree::OCTree<3, WRAPPER_CLASS, OCTree::mutex_sync_object> OCTREE;
typedef OCTree::_Node <3, WRAPPER_CLASS, OCTree::mutex_sync_object> NODE;

struct WRAPPER_CLASS {
typedef double value_type;
Expand Down Expand Up @@ -54,25 +55,8 @@ bool WRAPPER_CLASS::operator () (box_const_type& box) const {
return flag;
}

void fill (OCTREE* tree) {
const size_t num_objects = 10;
for(size_t i = 0; i < num_objects; ++i) {
for(size_t j = 0; j < num_objects; ++j) {
for(size_t k = 0; k < num_objects; ++k) {
const double x = 2*(i - num_objects/2)/num_objects;
const double y = 2*(j - num_objects/2)/num_objects;
const double z = 2*(k - num_objects/2)/num_objects;

WRAPPER_CLASS temp;
temp.object = new TETRAHEDRON();
temp.object->x0 = x + 0; temp.object->y0 = y + 0; temp.object->z0 = z + 0;
temp.object->x1 = x + 1; temp.object->y1 = y + 0; temp.object->z1 = z + 0;
temp.object->x2 = x + 0; temp.object->y2 = y + 1; temp.object->z2 = z + 0;
temp.object->x3 = x + 0; temp.object->y3 = y + 0; temp.object->z3 = z + 1;
tree->insert(temp);
}
}
}
void fill (OCTREE* tree, const std::vector<WRAPPER_CLASS>& objects, const int& thread_num) {
for ( auto object : objects ) tree->insert(object);
return;
}

Expand All @@ -81,19 +65,62 @@ void optimize(OCTREE* tree) {
return;
}

void check(OCTREE* tree) {
struct functor {
bool operator( )( const NODE& node ) const {
auto box = node._M_box;

bool flag = true;
flag &= ( 0.5 >= box._M_low_bounds[0] ) && ( -0.5 <= box._M_high_bounds[0] );
flag &= ( 0.5 >= box._M_low_bounds[1] ) && ( -0.5 <= box._M_high_bounds[1] );
flag &= ( 0.5 >= box._M_low_bounds[2] ) && ( -0.5 <= box._M_high_bounds[2] );

return flag;
}
};

void check(OCTREE* tree, const std::vector<WRAPPER_CLASS>& objects) {
OCTREE::query_type query_point = {{ 0.0, 0.0, 0.0}};

std::vector<WRAPPER_CLASS> find_exact = tree->find_exact (query_point);
std::vector<WRAPPER_CLASS> find_nearest = tree->find_nearest (query_point);
std::vector<WRAPPER_CLASS> find_nearest_s = tree->find_nearest_s(query_point);
std::vector<WRAPPER_CLASS> find_if = tree->find_if ( functor());

return;
}

int main() {
const size_t num_threads = 4;
const int num_threads = 8;
const int num_objects = 10;
std::vector<std::thread> threads;
std::vector<WRAPPER_CLASS> objects;

for(int i = 0; i < num_objects; ++i) {
for(int j = 0; j < num_objects; ++j) {
for(int k = 0; k < num_objects; ++k) {
const double x_cur = 2.*(i + 0 - num_objects/2.)/num_objects;
const double y_cur = 2.*(j + 0 - num_objects/2.)/num_objects;
const double z_cur = 2.*(k + 0 - num_objects/2.)/num_objects;
const double x_nxt = 2.*(i + 1 - num_objects/2.)/num_objects;
const double y_nxt = 2.*(j + 1 - num_objects/2.)/num_objects;
const double z_nxt = 2.*(k + 1 - num_objects/2.)/num_objects;

WRAPPER_CLASS temp;
temp.object = new TETRAHEDRON();
temp.object->x0 = x_cur; temp.object->y0 = y_cur; temp.object->z0 = z_cur;
temp.object->x1 = x_nxt; temp.object->y1 = y_cur; temp.object->z1 = z_cur;
temp.object->x2 = x_cur; temp.object->y2 = y_nxt; temp.object->z2 = z_cur;
temp.object->x3 = x_cur; temp.object->y3 = y_cur; temp.object->z3 = z_nxt;
objects.push_back(temp);
}
}
}


OCTREE* tree = new OCTREE( OCTREE::box_type( -1, 1, -1, 1, -1, 1) );
//Fill the tree in multithreaded mode
for (size_t i = 0; i < num_threads; ++i)
threads.push_back(std::thread(&fill , tree));
threads.push_back(std::thread(&fill , tree, objects, i));
for (size_t i = 0; i < num_threads; ++i)
threads[i].join();
threads.clear();
Expand All @@ -109,7 +136,7 @@ int main() {
std::cout << *tree << std::endl;
//Check the tree
for (size_t i = 0; i < num_threads; ++i)
threads.push_back(std::thread(&check , tree));
threads.push_back(std::thread(&check , tree, objects));
for (size_t i = 0; i < num_threads; ++i)
threads[i].join();
threads.clear();
Expand Down
52 changes: 35 additions & 17 deletions examples/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ struct POINT {
};

struct WRAPPER_CLASS;
typedef OCTree::OCTree<3, WRAPPER_CLASS> OCTREE;
typedef OCTree::OCTree<3, WRAPPER_CLASS, OCTree::mutex_sync_object> OCTREE;
typedef OCTree::_Node <3, WRAPPER_CLASS, OCTree::mutex_sync_object> NODE;

struct WRAPPER_CLASS {
typedef double value_type;
Expand All @@ -36,8 +37,8 @@ bool WRAPPER_CLASS::operator () (box_const_type& box) const {
return flag;
}

void fill (OCTREE* tree, const std::vector<WRAPPER_CLASS>& objects) {
for ( auto object : objects ) tree->insert(object);
void fill (OCTREE* tree, const std::vector<WRAPPER_CLASS>& objects, const int& thread_num) {
for ( auto object : objects ) tree->insert(object);
return;
}

Expand All @@ -46,25 +47,43 @@ void optimize(OCTREE* tree) {
return;
}

void check(OCTREE* tree, const std::vector<WRAPPER_CLASS>& objects) {
for(auto object : objects) {
std::cout << "Lalala" << std::endl;
struct functor {
bool operator( )( const NODE& node ) const {
auto box = node._M_box;

bool flag = true;
flag &= ( 0.5 >= box._M_low_bounds[0] ) && ( -0.5 <= box._M_high_bounds[0] );
flag &= ( 0.5 >= box._M_low_bounds[1] ) && ( -0.5 <= box._M_high_bounds[1] );
flag &= ( 0.5 >= box._M_low_bounds[2] ) && ( -0.5 <= box._M_high_bounds[2] );

return flag;
}
};

void check(OCTREE* tree, const std::vector<WRAPPER_CLASS>& objects) {

OCTREE::query_type query_point = {{ 0.0, 0.0, 0.0}};

std::vector<WRAPPER_CLASS> find_exact = tree->find_exact (query_point);
std::vector<WRAPPER_CLASS> find_nearest = tree->find_nearest (query_point);
std::vector<WRAPPER_CLASS> find_nearest_s = tree->find_nearest_s(query_point);
std::vector<WRAPPER_CLASS> find_if = tree->find_if ( functor());

return;
}

int main() {
const size_t num_threads = 4;
const size_t num_objects = 20;
const int num_threads = 8;
const int num_objects = 10;
std::vector<std::thread> threads;
std::vector<WRAPPER_CLASS> objects;

for(size_t i = 0; i <= num_objects; ++i) {
for(size_t j = 0; j <= num_objects; ++j) {
for(size_t k = 0; k <= num_objects; ++k) {
const double x = 2*(i - num_objects/2)/num_objects;
const double y = 2*(j - num_objects/2)/num_objects;
const double z = 2*(k - num_objects/2)/num_objects;
for(int i = 0; i <= num_objects; ++i) {
for(int j = 0; j <= num_objects; ++j) {
for(int k = 0; k <= num_objects; ++k) {
const double x = 2.*(i - num_objects/2.)/num_objects;
const double y = 2.*(j - num_objects/2.)/num_objects;
const double z = 2.*(k - num_objects/2.)/num_objects;

WRAPPER_CLASS temp;
temp.object = new POINT();
Expand All @@ -73,11 +92,11 @@ int main() {
}
}
}

//Allocate memory
OCTREE* tree = new OCTREE( OCTREE::box_type( -1, 1, -1, 1, -1, 1) );
//Fill the tree in multithreaded mode
for (size_t i = 0; i < num_threads; ++i)
threads.push_back(std::thread(&fill , tree, objects));
threads.push_back(std::thread(&fill , tree, objects, i));
for (size_t i = 0; i < num_threads; ++i)
threads[i].join();
threads.clear();
Expand All @@ -92,7 +111,6 @@ int main() {
//Output the tree
std::cout << *tree << std::endl;
//Check the tree in multithreaded mode
std::cout << "Hello world" << std::endl;
for (size_t i = 0; i < num_threads; ++i)
threads.push_back(std::thread(&check , tree, objects));
for (size_t i = 0; i < num_threads; ++i)
Expand Down
15 changes: 7 additions & 8 deletions include/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,31 @@ namespace OCTree {

template <size_t __K, typename __Val, class __Sync>
struct _Node {
typedef __Val object_type;
typedef const __Val object_const_type;
typedef __Val& object_reference;
typedef const __Val& object_const_reference;
typedef __Val object_type;
typedef const __Val object_const_type;
typedef __Val& object_reference;
typedef const __Val& object_const_reference;
typedef typename __Val::value_type value_type;
typedef __Sync sync_object_type;
typedef typename std::array<_Node*, power<__K>::result>::iterator node_iterator;
typedef typename std::array<_Node*, power<__K>::result>::const_iterator node_const_iterator;
typedef typename std::vector<object_type>::iterator data_iterator;
typedef typename std::vector<object_type>::const_iterator data_const_iterator;
typedef typename std::array<value_type, __K> query_type;
//enum class STATE { M_DEFAULT, M_NO_ACTION, M_SPLIT_NODE, M_CLEAR_BRANCH, M_EMPTY_NODE };
//std::atomic<STATE> _M_state;

struct STATE {
static const int M_DEFAULT = 0;
static const int M_NO_ACTION = 1;
static const int M_SPLIT_NODE = 2;
static const int M_CLEAR_BRANCH = 3;
static const int M_EMPTY_NODE = 4;
};
std::atomic<int> _M_state;
std::atomic<int> _M_state;

mutable sync_object_type _M_mutex;

_Node* _M_parent;
std::array<_Node*, power<__K>::result> _M_child;
std::array<_Node*, power<__K>::result> _M_child;
std::vector<__Val> _M_data;
_Box<__K, value_type> _M_box;
private:
Expand Down
7 changes: 3 additions & 4 deletions include/octree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,12 @@ template <typename type> struct max {
bool allOutputNodesAreLeafNodes = true;

for( it_input = begin_input; it_input != end_input; it_input++ ) {
const box_type& _Input_box = (*it_input)->_M_box;
bool _Input_isLeafNode = (*it_input)->isLeafNode();
bool _Input_isEmptyNode = _M_empty_branch(*it_input);
bool _Input_isLeafNode = (*it_input)->isLeafNode();
bool _Input_isEmptyNode = _M_empty_branch(*it_input);

if ( !_Input_isEmptyNode ) {
//Check input predicates
bool allFunctorsTrue = functor(_Input_box);
bool allFunctorsTrue = functor( *(*it_input) );
//If input predicates and the box of the current node intersect we will store child nodes
if(allFunctorsTrue) {
if ( _Input_isLeafNode ) {
Expand Down

0 comments on commit eb6d088

Please sign in to comment.