From a906b12b3ca9c2de23506d7e32aa893f207d30c3 Mon Sep 17 00:00:00 2001 From: Simon Giraudot Date: Tue, 19 Jan 2021 13:31:41 +0100 Subject: [PATCH] Fix traversal concept, model and examples --- Orthtree/doc/Orthtree/Concepts/Traversal.h | 11 ++++-- Orthtree/doc/Orthtree/PackageDescription.txt | 6 ++-- .../Orthtree/Octree_traversal_custom.cpp | 3 +- .../Orthtree/Octree_traversal_preorder.cpp | 3 +- Orthtree/include/CGAL/Orthtree.h | 10 +++--- Orthtree/include/CGAL/Orthtree/Traversal.h | 36 +++++++++++++++---- Orthtree/include/CGAL/Orthtree_traits_2.h | 2 ++ Orthtree/include/CGAL/Orthtree_traits_3.h | 2 ++ Orthtree/include/CGAL/Orthtree_traits_d.h | 2 ++ Orthtree/test/Orthtree/test_octree_grade.cpp | 6 ++-- .../test/Orthtree/test_octree_traverse.cpp | 10 +++--- 11 files changed, 64 insertions(+), 27 deletions(-) diff --git a/Orthtree/doc/Orthtree/Concepts/Traversal.h b/Orthtree/doc/Orthtree/Concepts/Traversal.h index 696e8ba6373..2a1185d49c2 100644 --- a/Orthtree/doc/Orthtree/Concepts/Traversal.h +++ b/Orthtree/doc/Orthtree/Concepts/Traversal.h @@ -3,28 +3,33 @@ \ingroup PkgOrthtreeConcepts \cgalConcept - \brief a Traversal provides the functions needed to traverse the + \brief a traversal provides the functions needed to traverse the nodes of an Orthtree. A traversal is used to iterate on a tree with a user-selected order (e.g. Preorder, Postorder). + Template parameters are the template parameters of the `CGAL::Orthtree`, + please refer to the documentation of this class for more + information. + \cgalHasModel `CGAL::Orthtrees::Traversal::Preorder` \cgalHasModel `CGAL::Orthtrees::Traversal::Postorder` \cgalHasModel `CGAL::Orthtrees::Traversal::Leaves` */ +template class Traversal { public: + using Node = typename CGAL::Orthtree::Node; ///< The node type + /*! \brief returns the first node to iterate to, given the root of the Orthtree. */ - template Node first (Node root) const; /*! \brief returns the next node to iterate to, given the previous node. */ - template Node next(Node n) const; }; diff --git a/Orthtree/doc/Orthtree/PackageDescription.txt b/Orthtree/doc/Orthtree/PackageDescription.txt index c1aa6268a31..c9062520a7e 100644 --- a/Orthtree/doc/Orthtree/PackageDescription.txt +++ b/Orthtree/doc/Orthtree/PackageDescription.txt @@ -57,8 +57,8 @@ - `CGAL::Orthtrees::Split_predicate::Maximum_depth_and_maximum_number_of_inliers` \cgalCRPSection{%Traversal} -- `CGAL::Orthtrees::Traversal::Preorder` -- `CGAL::Orthtrees::Traversal::Postorder` -- `CGAL::Orthtrees::Traversal::Leaves` +- `CGAL::Orthtrees::Traversal::Preorder` +- `CGAL::Orthtrees::Traversal::Postorder` +- `CGAL::Orthtrees::Traversal::Leaves` */ diff --git a/Orthtree/examples/Orthtree/Octree_traversal_custom.cpp b/Orthtree/examples/Orthtree/Octree_traversal_custom.cpp index 6fe10dca350..8c2fa5edf88 100644 --- a/Orthtree/examples/Orthtree/Octree_traversal_custom.cpp +++ b/Orthtree/examples/Orthtree/Octree_traversal_custom.cpp @@ -13,16 +13,15 @@ typedef CGAL::Point_set_3 Point_set; typedef Point_set::Point_map Point_map; typedef CGAL::Octree Octree; +typedef Octree::Node Node; struct First_branch_traversal { - template Node first (Node root) const { return root; } - template Node next (Node n) const { if (n.is_leaf()) diff --git a/Orthtree/examples/Orthtree/Octree_traversal_preorder.cpp b/Orthtree/examples/Orthtree/Octree_traversal_preorder.cpp index fafe9d0bef3..3fbfe280b40 100644 --- a/Orthtree/examples/Orthtree/Octree_traversal_preorder.cpp +++ b/Orthtree/examples/Orthtree/Octree_traversal_preorder.cpp @@ -13,6 +13,7 @@ typedef CGAL::Point_set_3 Point_set; typedef Point_set::Point_map Point_map; typedef CGAL::Octree Octree; +typedef CGAL::Orthtrees::Traversal::Preorder Preorder_traversal; int main(int argc, char **argv) { @@ -36,7 +37,7 @@ int main(int argc, char **argv) { octree.refine(); // Print out the octree using preorder traversal - for (Octree::Node node : octree.traverse()) { + for (Octree::Node node : octree.traverse()) { std::cout << node << std::endl; } diff --git a/Orthtree/include/CGAL/Orthtree.h b/Orthtree/include/CGAL/Orthtree.h index cbd2137e8ff..335bc0f5344 100644 --- a/Orthtree/include/CGAL/Orthtree.h +++ b/Orthtree/include/CGAL/Orthtree.h @@ -139,6 +139,9 @@ public: typedef typename PointRange::iterator Range_iterator; typedef typename std::iterator_traits::value_type Range_type; typedef internal::Cartesian_ranges Cartesian_ranges; + + typedef Orthtrees::Traversal::Leaves Leaves_traversal; + typedef Orthtrees::Traversal::Preorder Preorder_traversal; /// \endcond /// @} @@ -311,7 +314,7 @@ public: // Collect all the leaf nodes std::queue leaf_nodes; - for (Node leaf : traverse(Orthtrees::Traversal::Leaves())) { + for (Node leaf : traverse(Leaves_traversal())) { // TODO: I'd like to find a better (safer) way of doing this leaf_nodes.push(leaf); } @@ -403,8 +406,7 @@ public: Node first = traversal.first(m_root); - Node_traversal_method_const next = std::bind(&Traversal::template next, - traversal, _1); + Node_traversal_method_const next = std::bind(&Traversal::next, traversal, _1); return boost::make_iterator_range(Traversal_iterator(first, next), Traversal_iterator()); @@ -864,7 +866,7 @@ public: friend std::ostream& operator<< (std::ostream& os, const Self& orthtree) { // Create a range of nodes - auto nodes = orthtree.traverse(CGAL::Orthtrees::Traversal::Preorder()); + auto nodes = orthtree.traverse(Preorder_traversal()); // Iterate over the range for (auto &n : nodes) { // Show the depth diff --git a/Orthtree/include/CGAL/Orthtree/Traversal.h b/Orthtree/include/CGAL/Orthtree/Traversal.h index f0f993a9eb4..70099127377 100644 --- a/Orthtree/include/CGAL/Orthtree/Traversal.h +++ b/Orthtree/include/CGAL/Orthtree/Traversal.h @@ -20,6 +20,12 @@ namespace CGAL { +/// \cond SKIP_IN_MANUAL +// Forward declaration +template +class Orthtree; +/// \endcond + namespace Orthtrees { /// \cond SKIP_IN_MANUAL @@ -86,16 +92,22 @@ namespace Traversal { /*! \ingroup PkgOrthtreeTraversal \brief preorder traversal, starting from the root towards the leaves. + + Template parameters are the template parameters of the `Orthtree`, + please refer to the documentation of this class for more + information. + \cgalModels Traversal */ +template struct Preorder { - template + using Node = typename Orthtree::Node; + Node first(Node root) const { return root; } - template Node next(Node n) const { if (n.is_leaf()) { @@ -116,17 +128,23 @@ struct Preorder { /*! \ingroup PkgOrthtreeTraversal \brief preorder traversal, starting from the leaves towards the root. + + Template parameters are the template parameters of the `Orthtree`, + please refer to the documentation of this class for more + information. + \cgalModels Traversal */ +template struct Postorder { - template + using Node = typename Orthtree::Node; + Node first(Node root) const { return deepest_first_child(root); } - template Node next(Node n) const { Node next = deepest_first_child(next_sibling(n)); @@ -141,17 +159,23 @@ struct Postorder { /*! \ingroup PkgOrthtreeTraversal \brief leaves traversal, ignoring all non-leave nodes. + + Template parameters are the template parameters of the `Orthtree`, + please refer to the documentation of this class for more + information. + \cgalModels Traversal */ +template struct Leaves { - template + using Node = typename Orthtree::Node; + Node first(Node root) const { return deepest_first_child(root); } - template Node next(Node n) const { Node next = deepest_first_child(next_sibling(n)); diff --git a/Orthtree/include/CGAL/Orthtree_traits_2.h b/Orthtree/include/CGAL/Orthtree_traits_2.h index d52ec044c1f..c035de1b9fd 100644 --- a/Orthtree/include/CGAL/Orthtree_traits_2.h +++ b/Orthtree/include/CGAL/Orthtree_traits_2.h @@ -12,6 +12,8 @@ #ifndef CGAL_ORTHTREE_TRAITS_3_H #define CGAL_ORTHTREE_TRAITS_3_H +#include + namespace CGAL { diff --git a/Orthtree/include/CGAL/Orthtree_traits_3.h b/Orthtree/include/CGAL/Orthtree_traits_3.h index 7d939247f6e..5f2abc0f807 100644 --- a/Orthtree/include/CGAL/Orthtree_traits_3.h +++ b/Orthtree/include/CGAL/Orthtree_traits_3.h @@ -12,6 +12,8 @@ #ifndef CGAL_ORTHTREE_TRAITS_3_H #define CGAL_ORTHTREE_TRAITS_3_H +#include + namespace CGAL { diff --git a/Orthtree/include/CGAL/Orthtree_traits_d.h b/Orthtree/include/CGAL/Orthtree_traits_d.h index 542f7b92ab7..a862af2dcc4 100644 --- a/Orthtree/include/CGAL/Orthtree_traits_d.h +++ b/Orthtree/include/CGAL/Orthtree_traits_d.h @@ -12,6 +12,8 @@ #ifndef CGAL_ORTHTREE_TRAITS_D_H #define CGAL_ORTHTREE_TRAITS_D_H +#include + namespace CGAL { diff --git a/Orthtree/test/Orthtree/test_octree_grade.cpp b/Orthtree/test/Orthtree/test_octree_grade.cpp index f22d3dd6ee3..e7eb99c53cb 100644 --- a/Orthtree/test/Orthtree/test_octree_grade.cpp +++ b/Orthtree/test/Orthtree/test_octree_grade.cpp @@ -14,14 +14,14 @@ typedef CGAL::Simple_cartesian Kernel; typedef Kernel::Point_3 Point; typedef Kernel::FT FT; typedef CGAL::Point_set_3 Point_set; -typedef CGAL::Octree -Octree; +typedef CGAL::Octree Octree; +typedef CGAL::Orthtrees::Traversal::Leaves Leaves_traversal; std::size_t count_jumps(Octree &octree) { std::size_t jumps = 0; - for (auto &node : octree.traverse(CGAL::Orthtrees::Traversal::Leaves())) { + for (auto &node : octree.traverse(Leaves_traversal())) { for (int direction = 0; direction < 6; ++direction) { diff --git a/Orthtree/test/Orthtree/test_octree_traverse.cpp b/Orthtree/test/Orthtree/test_octree_traverse.cpp index 095f72af4ed..5399fea5bfd 100644 --- a/Orthtree/test/Orthtree/test_octree_traverse.cpp +++ b/Orthtree/test/Orthtree/test_octree_traverse.cpp @@ -11,8 +11,8 @@ typedef CGAL::Simple_cartesian Kernel; typedef Kernel::Point_3 Point; typedef CGAL::Point_set_3 Point_set; -typedef CGAL::Octree -Octree; +typedef CGAL::Octree Octree; +typedef CGAL::Orthtrees::Traversal::Preorder Preorder_traversal; bool test_preorder_1_node() { @@ -25,7 +25,7 @@ bool test_preorder_1_node() { octree.refine(10, 1); // Create the range - auto nodes = octree.traverse(); + auto nodes = octree.traverse(); // Check each item in the range auto iter = nodes.begin(); @@ -46,7 +46,7 @@ bool test_preorder_9_nodes() { octree.refine(10, 1); // Create the range - auto nodes = octree.traverse(); + auto nodes = octree.traverse(); // Check each item in the range auto iter = nodes.begin(); @@ -73,7 +73,7 @@ bool test_preorder_25_nodes() { octree.refine(10, 1); // Create the range - auto nodes = octree.traverse(); + auto nodes = octree.traverse(); // Check each item in the range auto iter = nodes.begin();