Skip to content

Commit

Permalink
Split tree walker testing into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonCampolattaro committed Jun 25, 2020
1 parent beac2ee commit 43a9819
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 35 deletions.
6 changes: 3 additions & 3 deletions Octree/include/CGAL/Octree.h
Expand Up @@ -176,9 +176,9 @@ namespace CGAL {

const Node &root() const { return m_root; }

void print(std::ostream &os, std::function<Node *(Node *, Node *)> tree_walker) {
void print(std::ostream &os, Node *first, std::function<Node *(Node *)> tree_walker) {

Node *node = &root();
Node *node = first;

while (nullptr != node) {

Expand All @@ -188,7 +188,7 @@ namespace CGAL {
os << "(" << node->location()[0] << "," << node->location()[1] << "," << node->location()[2] << ") ";
os << std::endl;

node = tree_walker(&root(), node);
node = tree_walker(node);
}
}

Expand Down
54 changes: 24 additions & 30 deletions Octree/include/CGAL/Octree/Tree_walker_criterion.h
Expand Up @@ -3,62 +3,56 @@

namespace CGAL {

struct Siblings {
template<class Node>
Node *next_sibling(Node *n) {

template<class Node>
Node *operator()(Node *root, Node *n) {

// Passing null returns the first node
if (nullptr == n)
return root;
// Passing null returns the first node
if (nullptr == n)
return n;

// If this node has no parent, it has no siblings
if (nullptr == n->parent())
return nullptr;
// If this node has no parent, it has no siblings
if (nullptr == n->parent())
return nullptr;

// Find out which child this is
std::size_t index = n->index().to_ulong();
// Find out which child this is
std::size_t index = n->index().to_ulong();

// Return null if this is the last child
if (7 == index)
return nullptr;
// Return null if this is the last child
if (7 == index)
return nullptr;

// Otherwise, return the next child
return &((*n->parent())[index + 1]);
}
};
// Otherwise, return the next child
return &((*n->parent())[index + 1]);
}

struct Depth_first {

template<class Node>
Node *operator()(Node *root, Node *n) {
Node *first(Node *root) {
return root;
}

template<class Node>
Node *operator()(Node *n) {

// Passing null returns the first node
if (nullptr == n) {

return root;
// Find the deepest child on the left
// Node *first = root;
// while (!first->is_leaf())
// first = &(*first)[0];
// return first;
}

if (n->is_leaf()) {

Siblings siblings;

// Check if this node is the last sibling
if (7 != n->index().to_ulong()) {

// Return the next sibling
return (siblings(root, n));
return (next_sibling(n));

} else {

// Return the next sibling of the parent
// FIXME: this should be able to search upwards at the last sibling
return (siblings(root, n->parent()));
return (next_sibling(n->parent()));
}


Expand Down
1 change: 1 addition & 0 deletions Octree/test/Octree/CMakeLists.txt
Expand Up @@ -9,4 +9,5 @@ find_package(CGAL REQUIRED QUIET OPTIONAL_COMPONENTS Core )
create_single_source_cgal_program( "octree_test.cpp" )
create_single_source_cgal_program( "test_octree_equality.cpp" )
create_single_source_cgal_program( "test_node_index.cpp" )
create_single_source_cgal_program( "test_tree_walk.cpp" )

8 changes: 6 additions & 2 deletions Octree/test/Octree/test_node_index.cpp
Expand Up @@ -41,8 +41,12 @@ int main(void) {
Octree octree(points, point_map);
octree.refine(10, 1);

auto treeWalker = CGAL::Depth_first();
octree.print(std::cout, treeWalker);
std::cout << "root: " << octree.root().index() << std::endl;
std::cout << "first child: " << octree.root()[0].index() << std::endl;
std::cout << "fifth child: " << octree.root()[4].index() << std::endl;
std::cout << "fifth child of first child: " << octree.root()[0][4].index() << std::endl;

// TODO

return 0;
}
48 changes: 48 additions & 0 deletions Octree/test/Octree/test_tree_walk.cpp
@@ -0,0 +1,48 @@
#define CGAL_TRACE_STREAM std::cerr

#include <CGAL/Octree.h>
#include <CGAL/Octree/IO.h>
#include <CGAL/Octree/Tree_walker_criterion.h>

#include <CGAL/Simple_cartesian.h>
#include <CGAL/Point_set_3.h>

typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_3 Point;
typedef CGAL::Point_set_3<Point> Point_set;
typedef CGAL::Octree
<Point_set, typename Point_set::Point_map>
Octree;

int main(void) {

Point_set points;
points.insert({-1, -1, -1});
points.insert({1, -1, -1});
points.insert({-1, 1, -1});
points.insert({1, 1, -1});
points.insert({-1, -1, 1});
points.insert({1, -1, 1});
points.insert({-1, 1, 1});
points.insert({1, 1, 1});

points.insert({-1, -1, -1.1});
points.insert({-1, -1, -1.2});
points.insert({-1, -1, -1.3});
points.insert({-1, -1, -1.4});
points.insert({-1, -1, -1.5});
points.insert({-1, -1, -1.6});
points.insert({-1, -1, -1.7});
points.insert({-1, -1, -1.8});
points.insert({-1, -1, -1.9});

auto point_map = points.point_map();

Octree octree(points, point_map);
octree.refine(10, 1);

auto treeWalker = CGAL::Depth_first();
octree.print(std::cout, treeWalker.first(&octree.root()), treeWalker);

return 0;
}

0 comments on commit 43a9819

Please sign in to comment.