1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
|
// Copyright (c) 2007-2020 INRIA (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Orthtree/include/CGAL/Orthtree.h $
// $Id: include/CGAL/Orthtree.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Jackson Campolattaro, Simon Giraudot, Cédric Portaneri, Tong Zhao
#ifndef CGAL_ORTHTREE_H
#define CGAL_ORTHTREE_H
#include <CGAL/license/Orthtree.h>
#include <CGAL/Orthtree/Cartesian_ranges.h>
#include <CGAL/Orthtree/Split_predicates.h>
#include <CGAL/Orthtree/Traversals.h>
#include <CGAL/Orthtree/Traversal_iterator.h>
#include <CGAL/Orthtree/IO.h>
#include <CGAL/NT_converter.h>
#include <CGAL/Cartesian_converter.h>
#include <CGAL/Property_container.h>
#include <CGAL/property_map.h>
#include <CGAL/intersections.h>
#include <CGAL/squared_distance_3.h>
#include <boost/function.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
#include <fstream>
#include <ostream>
#include <functional>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <math.h>
#include <utility>
#include <boost/mpl/has_xxx.hpp>
namespace CGAL {
namespace Orthtree_impl {
BOOST_MPL_HAS_XXX_TRAIT_DEF(Node_data)
BOOST_MPL_HAS_XXX_TRAIT_DEF(Squared_distance_of_element)
template <class GT, bool has_data>
struct Node_data_wrapper;
template <class GT>
struct Node_data_wrapper<GT, true>
{
using Node_index = typename GT::Node_index;
using Node_data = typename GT::Node_data;
typename CGAL::Properties::Experimental::Property_container<Node_index>::template Array<Node_data>& m_node_contents;
template <class Property_container>
Node_data_wrapper(Property_container& node_properties)
: m_node_contents(node_properties.template get_or_add_property<Node_data>("contents").first)
{}
const Node_data& operator[](Node_index n) const
{
return m_node_contents[n];
}
Node_data& operator[](Node_index n)
{
return m_node_contents[n];
}
};
template <class GT>
struct Node_data_wrapper<GT, false>
{
using Node_index = typename GT::Node_index;
using Node_data = void*;
template <class Property_container>
Node_data_wrapper(Property_container&) {}
void* operator[](Node_index) const
{
return nullptr;
}
};
} // end of Orthtree_impl namespace
/*!
\ingroup PkgOrthtreeRef
\brief A data structure using an axis-aligned hyperrectangle
decomposition of dD space for efficient access and
computation.
\details It builds a hierarchy of nodes which subdivides the space.
Each node represents an axis-aligned hyperrectangle region of space.
The contents of nodes depend on the traits class, non-leaf nodes also
contain \f$2^{dim}\f$ other nodes which further subdivide the
region.
\sa `CGAL::Quadtree`
\sa `CGAL::Octree`
\tparam GeomTraits must be a model of `OrthtreeTraits` or `OrthtreeTraitsWithData`.
*/
template <typename GeomTraits>
class Orthtree {
public:
/// \name Template Types
/// @{
using Traits = GeomTraits; ///< Geometry traits
/// @}
/// \name Traits Types
/// @{
#ifndef DOXYGEN_RUNNING
static inline constexpr bool has_data = Orthtree_impl::has_Node_data<GeomTraits>::value;
static inline constexpr bool supports_neighbor_search = Orthtree_impl::has_Squared_distance_of_element<GeomTraits>::value;
#else
static inline constexpr bool has_data = bool_value; ///< `true` if `GeomTraits` is a model of `OrthtreeTraitsWithData` and `false` otherwise.
static inline constexpr bool supports_neighbor_search = bool_value; ///< `true` if `GeomTraits` is a model of `CollectionPartitioningOrthtreeTraits` and `false` otherwise.
#endif
static constexpr int dimension = Traits::dimension; ///< Dimension of the tree
using Kernel = typename Traits::Kernel; ///< Kernel type.
using Geom_traits = Kernel;
using FT = typename Traits::FT; ///< Number type.
using Point = typename Traits::Point_d; ///< Point type.
using Bbox = typename Traits::Bbox_d; ///< Bounding box type.
using Sphere = typename Traits::Sphere_d; ///< Sphere type.
using Adjacency = typename Traits::Adjacency; ///< Adjacency type.
using Node_index = typename Traits::Node_index; ///< Index of a given node in the tree; the root always has index 0.
#ifndef DOXYGEN_RUNNING
using Node_data = typename Orthtree_impl::Node_data_wrapper<Traits, has_data>::Node_data;
#else
using Node_data = std::conditional_t<has_data,typename GeomTraits::Node_data,void*>;
#endif
/// @}
/// \name Public Types
/// @{
/*!
* \brief Self alias for convenience.
*/
using Self = Orthtree<Traits>;
/*!
* \brief Degree of the tree (number of children of non-leaf nodes).
*/
static constexpr int degree = (2 << (dimension - 1));
/*!
\brief Set of bits representing this node's relationship to its parent.
Equivalent to an array of Booleans, where index[0] is whether `x`
is greater, index[1] is whether `y` is greater, index[2] is whether
`z` is greater, and so on for higher dimensions if needed.
Used to represent a node's relationship to the center of its parent.
*/
using Local_coordinates = std::bitset<dimension>;
/*!
\brief Coordinates representing this node's relationship
with the rest of the tree.
Each value `(x, y, z, ...)` of global coordinates is calculated by doubling
the parent's global coordinates and adding the local coordinates.
*/
using Global_coordinates = std::array<std::uint32_t, dimension>;
/*!
* \brief A predicate that determines whether a node must be split when refining a tree.
*/
using Split_predicate = std::function<bool(Node_index, const Self&)>;
/*!
* \brief A model of `ForwardRange` whose value type is `Node_index`.
*/
#ifdef DOXYGEN_RUNNING
using Node_index_range = unspecified_type;
#else
using Node_index_range = boost::iterator_range<Index_traversal_iterator<Self>>;
#endif
/*!
* \brief A model of `LvaluePropertyMap` with `Node_index` as key type and `T` as value type.
*/
#ifdef DOXYGEN_RUNNING
template <class T>
using Property_map = unspecified_type;
#else
template <class T>
using Property_map = Properties::Experimental::Property_array_handle<Node_index, T>;
#endif
/// @}
private: // data members :
using Cartesian_ranges = Orthtrees::internal::Cartesian_ranges<Traits>;
using Node_property_container = Properties::Experimental::Property_container<Node_index>;
template <typename T>
using Property_array = typename Properties::Experimental::Property_container<Node_index>::template Array<T>;
Traits m_traits; /* the tree traits */
Kernel m_kernel;
Node_property_container m_node_properties;
Orthtree_impl::Node_data_wrapper<Traits, has_data> m_node_contents;
Property_array<std::uint8_t>& m_node_depths;
Property_array<Global_coordinates>& m_node_coordinates;
Property_array<std::optional<Node_index>>& m_node_parents;
Property_array<std::optional<Node_index>>& m_node_children;
using Bbox_dimensions = std::array<FT, dimension>;
Bbox m_bbox;
std::vector<Bbox_dimensions> m_side_per_depth; /* precomputed (potentially approximated) side length per node's depth */
Cartesian_ranges cartesian_range; /* a helper to easily iterate over coordinates of points */
public:
/// \name Constructor
/// @{
/*!
\brief constructs an orthtree for a traits instance.
The constructed orthtree has a root node with no children,
containing the contents determined by `Construct_root_node_contents` from the traits class.
That root node has a bounding box determined by `Construct_root_node_bbox` from the traits class,
which typically encloses its contents.
This single-node orthtree is valid and compatible
with all orthtree functionality, but any performance benefits are
unlikely to be realized until `refine()` is called.
\param traits the traits object.
*/
explicit Orthtree(Traits traits) :
m_traits(traits),
m_node_contents(m_node_properties),
m_node_depths(m_node_properties.template get_or_add_property<std::uint8_t>("depths", 0).first),
m_node_coordinates(m_node_properties.template get_or_add_property<Global_coordinates>("coordinates").first),
m_node_parents(m_node_properties.template get_or_add_property<std::optional<Node_index>>("parents").first),
m_node_children(m_node_properties.template get_or_add_property<std::optional<Node_index>>("children").first) {
m_node_properties.emplace();
// init bbox with first values found
m_bbox = m_traits.construct_root_node_bbox_object()();
// Determine dimensions of the root bbox
Bbox_dimensions size;
for (int i = 0; i < dimension; ++i)
{
size[i] = (m_bbox.max)()[i] - (m_bbox.min)()[i];
}
// save orthtree attributes
m_side_per_depth.push_back(size);
if constexpr (has_data)
data(root()) = m_traits.construct_root_node_contents_object()();
}
/*!
constructs an orthtree from a set of arguments provided to the traits constructor
*/
template <class ... Args, class = std::enable_if_t<sizeof...(Args)>= 2>>
explicit Orthtree(Args&& ... args)
: Orthtree(Traits(std::forward<Args>(args)...))
{}
/// copy constructor
explicit Orthtree(const Orthtree& other) :
m_traits(other.m_traits),
m_node_properties(other.m_node_properties),
m_node_contents(m_node_properties),
m_node_depths(m_node_properties.template get_property<std::uint8_t>("depths")),
m_node_coordinates(m_node_properties.template get_property<Global_coordinates>("coordinates")),
m_node_parents(m_node_properties.template get_property<std::optional<Node_index>>("parents")),
m_node_children(m_node_properties.template get_property<std::optional<Node_index>>("children")),
m_bbox(other.m_bbox), m_side_per_depth(other.m_side_per_depth) {}
/// move constructor
explicit Orthtree(Orthtree&& other) :
m_traits(other.m_traits),
m_node_properties(std::move(other.m_node_properties)),
m_node_contents(m_node_properties),
m_node_depths(m_node_properties.template get_property<std::uint8_t>("depths")),
m_node_coordinates(m_node_properties.template get_property<Global_coordinates>("coordinates")),
m_node_parents(m_node_properties.template get_property<std::optional<Node_index>>("parents")),
m_node_children(m_node_properties.template get_property<std::optional<Node_index>>("children")),
m_bbox(other.m_bbox), m_side_per_depth(other.m_side_per_depth)
{
other.m_node_properties.emplace();
}
/// @}
// Non-necessary but just to be clear on the rule of 5:
// assignment operators deleted
Orthtree& operator=(const Orthtree& other) = delete;
Orthtree& operator=(Orthtree&& other) = delete;
/// \name Tree Building
/// @{
/*!
\brief recursively subdivides the orthtree until it meets the given criteria.
The split predicate should return `true` if a leaf node should be split and `false` otherwise.
This function may be called several times with different
predicates: in that case, nodes already split are left unaltered,
while nodes that were not split and for which `split_predicate`
returns `true` are split.
\param split_predicate determines whether or not a leaf node needs to be subdivided.
*/
void refine(const Split_predicate& split_predicate) {
// Initialize a queue of nodes that need to be refined
std::queue<Node_index> todo;
todo.push(0);
// Process items in the queue until it's consumed fully
while (!todo.empty()) {
// Get the next element
auto current = todo.front();
todo.pop();
// Check if this node needs to be processed
if (split_predicate(current, *this)) {
// Split the node, redistributing its contents to its children
split(current);
}
// Check if the node has children which need to be processed
if (!is_leaf(current)) {
// Process each of its children
for (int i = 0; i < degree; ++i)
todo.push(child(current, i));
}
}
}
/*!
\brief convenience overload that refines an orthtree using a
maximum depth and maximum number of contained elements in a node as split
predicate.
This is equivalent to calling
`refine(Orthtrees::Maximum_depth_and_maximum_contained_elements(max_depth,
bucket_size))`.
The refinement is stopped as soon as one of the conditions is
violated: if a node contains more elements than `bucket_size` but is
already at `max_depth`, it is not split. Similarly, a node that is
at a depth smaller than `max_depth` but already contains fewer elements
than `bucket_size`, it is not split.
\warning This convenience method is only appropriate for trees with traits classes where
`Node_data` is a model of `Range`. `RandomAccessRange` is suggested for performance.
\param max_depth deepest a tree is allowed to be (nodes at this depth will not be split).
\param bucket_size maximum number of items a node is allowed to contain.
*/
template<typename Orthtree = Self>
auto refine(size_t max_depth = 10, size_t bucket_size = 20) -> std::enable_if_t<Orthtree::has_data, void> {
refine(Orthtrees::Maximum_depth_and_maximum_contained_elements(max_depth, bucket_size));
}
/*!
\brief refines the orthtree such that the difference of depth
between two immediate neighbor leaves is never more than 1.
This is done only by adding nodes, nodes are never removed.
*/
void grade() {
// Collect all the leaf nodes
std::queue<Node_index> leaf_nodes;
for (Node_index leaf: traverse(Orthtrees::Leaves_traversal<Self>(*this))) {
leaf_nodes.push(leaf);
}
// Iterate over the nodes
while (!leaf_nodes.empty()) {
// Get the next node
Node_index node = leaf_nodes.front();
leaf_nodes.pop();
// Skip this node if it isn't a leaf anymore
if (!is_leaf(node))
continue;
// Iterate over each of the neighbors
for (int direction = 0; direction < 6; ++direction) {
// Get the neighbor
auto neighbor = adjacent_node(node, direction);
// If it doesn't exist, skip it
if (!neighbor)
continue;
// Skip if this neighbor is a direct sibling (it's guaranteed to be the same depth)
// TODO: This check might be redundant, if it doesn't affect performance maybe I could remove it
if (parent(*neighbor) == parent(node))
continue;
// If it's already been split, skip it
if (!is_leaf(*neighbor))
continue;
// Check if the neighbor breaks our grading rule
// TODO: could the rule be parametrized?
if ((depth(node) - depth(*neighbor)) > 1) {
// Split the neighbor
split(*neighbor);
// Add newly created children to the queue
for (int i = 0; i < degree; ++i) {
leaf_nodes.push(child(*neighbor, i));
}
}
}
}
}
/// @}
/// \name Accessors
/// @{
/*!
* \brief provides direct read-only access to the tree traits.
*/
const Traits& traits() const { return m_traits; }
const Kernel& geom_traits() const
{
return m_kernel;
}
/*!
\brief provides access to the root node, and by
extension the rest of the tree.
*/
Node_index root() const { return 0; }
/*!
\brief returns the deepest level reached by a leaf node in this tree (root being level 0).
*/
std::size_t depth() const { return m_side_per_depth.size() - 1; }
/*!
\brief constructs a node index range using a tree-traversal function.
This method allows iteration over the nodes of the tree with a
user-selected order (preorder, postorder, leaves-only, etc.).
\tparam Traversal a model of `OrthtreeTraversal`
\param traversal class defining the traversal strategy
\return a `ForwardRange` over the node indices of the tree
*/
template <typename Traversal>
Node_index_range traverse(Traversal traversal) const {
Node_index first = traversal.first_index();
auto next = [=](const Self&, Node_index index) -> std::optional<Node_index> {
return traversal.next_index(index);
};
return boost::make_iterator_range(Index_traversal_iterator<Self>(*this, first, next),
Index_traversal_iterator<Self>());
}
/*!
\brief convenience method for using a traversal without constructing it yourself
\tparam Traversal a model of `OrthtreeTraversal`
\param args Arguments to to pass to the traversal's constructor, excluding the first (always an orthtree reference)
\return a `ForwardRange` over the node indices of the tree
*/
template <typename Traversal, typename ...Args>
Node_index_range traverse(Args&& ...args) const {
return traverse(Traversal{*this, std::forward<Args>(args)...});
}
// TODO shall we document it?
FT
compute_cartesian_coordinate(std::uint32_t gc, std::size_t depth, int ci) const
{
CGAL_assertion(depth <= m_side_per_depth.size());
// an odd coordinate will be first computed at the current depth,
// while an even coordinate has already been computed at a previous depth.
// So while the coordinate is even, we decrease the depth to end up of the first
// non-even coordinate to compute it (with particular case for bbox limits).
// Note that if the depth becomes too large, we might end up with incorrect coordinates
// due to rounding errors.
if (gc == (1u << depth)) return (m_bbox.max)()[ci]; // gc == 2^node_depth
if (gc == 0) return (m_bbox.min)()[ci];
if (gc % 2 !=0)
{
FT size = depth < m_side_per_depth.size()
? m_side_per_depth[depth][ci]
: m_side_per_depth[depth-1][ci]/FT(2);
return (m_bbox.min)()[ci] + int(gc) * size;
}
std::size_t nd = depth;
do{
--nd;
gc = gc >> 1;
}
while((gc&1)==0); // while even, shift
return (m_bbox.min)()[ci] + int(gc) * m_side_per_depth[nd][ci];
}
/*!
\brief constructs the bounding box of a node.
\note The object constructed is not the bounding box of the node's contents,
but the bounding box of the node itself.
\param n node to generate a bounding box for
\return the bounding box of the node n
*/
Bbox bbox(Node_index n) const {
using Cartesian_coordinate = std::array<FT, dimension>;
Cartesian_coordinate min_corner, max_corner;
std::size_t node_depth = depth(n);
for (int i = 0; i < dimension; i++)
{
min_corner[i]=compute_cartesian_coordinate(global_coordinates(n)[i], node_depth, i);
max_corner[i]=compute_cartesian_coordinate(global_coordinates(n)[i]+1, node_depth, i);
}
return {std::apply(m_traits.construct_point_d_object(), min_corner),
std::apply(m_traits.construct_point_d_object(), max_corner)};
}
/// @}
/// \name Custom Properties
/// @{
/*!
\brief gets a property for nodes, adding it if it does not already exist.
\tparam T the type of the property to add
\param name the name of the new property
\param default_value the default value assigned to nodes for this property
\return pair of the property map and a Boolean which is `true` if the property needed to be created
*/
template <typename T>
std::pair<Property_map<T>, bool> add_property(const std::string& name, const T default_value = T()) {
auto p = m_node_properties.get_or_add_property(name, default_value);
return std::pair<Property_map<T>, bool>(Property_map<T>(p.first), p.second);
}
/*!
\brief gets a property of the nodes if it exists.
\tparam T the type of the property to retrieve
\param name the name of the property
\return an optional containing the property map if it exists
*/
template <typename T>
std::optional<Property_map<T>> property(const std::string& name) const {
auto p = m_node_properties.template get_property_if_exists<T>(name);
if (p)
return std::optional<Property_map<T> >(Property_map<T>(*p));
else
return std::nullopt;
}
/*!
\brief returns a vector of all property names.
*/
std::vector<std::string> properties() const {
return m_node_properties.properties();
}
/*!
\brief removes the node property from the tree.
\tparam T the type of the property to remove
\param property the property to be removed from the tree.
\return true if property was a valid property of the tree.
*/
template <typename T>
bool remove_property(Property_map<T> property) {
return m_node_properties.remove_property(property.array());
}
/// @}
/// \name Queries
/// @{
/*!
\brief finds the leaf node which contains a particular point in space.
Traverses the orthtree and finds the leaf cell that has a
domain enclosing the point passed. The point passed must be within
the region enclosed by the orthtree (bbox of the root node). The point is contained in the
lower cell of each direction if its coordinate is lower than the center.
\param point query point.
\return the index of the node which contains the point.
*/
Node_index locate(const Point& point) const {
// Make sure the point is enclosed by the orthtree
CGAL_precondition (CGAL::do_intersect(point, bbox(root())));
// Start at the root node
Node_index node_for_point = root();
// Descend the tree until reaching a leaf node
while (!is_leaf(node_for_point)) {
// Find the point to split around
Point center = barycenter(node_for_point);
// Find the index of the correct sub-node
Local_coordinates local_coords;
std::size_t dim = 0;
for (const auto& r : cartesian_range(center, point))
local_coords[dim++] = (get<0>(r) <= get<1>(r));
// Find the correct sub-node of the current node
node_for_point = child(node_for_point, local_coords.to_ulong());
}
// Return the result
return node_for_point;
}
/*!
\brief finds the `k` nearest neighbors of the point `query`.
Nearest neighbors are outputted in order of increasing distance to `query`.
\tparam OutputIterator a model of `OutputIterator` that accepts `GeomTraits::Node_data_element` objects.
\param query query point
\param k number of neighbors to find
\param output output iterator
\warning Nearest neighbor searches requires `GeomTraits` to be a model of `CollectionPartitioningOrthtreeTraits`.
*/
template<typename OutputIterator, typename Orthtree = Self>
auto nearest_k_neighbors(const Point& query,
std::size_t k,
OutputIterator output) const -> std::enable_if_t<Orthtree::supports_neighbor_search, OutputIterator> {
Sphere query_sphere(query, (std::numeric_limits<FT>::max)());
CGAL_precondition(k > 0);
return nearest_k_neighbors_within_radius(query_sphere, k, output);
}
/*!
\brief finds the elements in the sphere `query`.
Elements are outputted in order of increasing distance to
the center of the sphere.
\tparam OutputIterator a model of `OutputIterator` that accepts `GeomTraits::Node_data_element` objects.
\param query query sphere
\param output output iterator
\warning Nearest neighbor searches requires `GeomTraits` to be a model of `CollectionPartitioningOrthtreeTraits`.
*/
template<typename OutputIterator, typename Orthtree = Self>
auto neighbors_within_radius(const Sphere& query, OutputIterator output) const -> std::enable_if_t<Orthtree::supports_neighbor_search, OutputIterator> {
return nearest_k_neighbors_within_radius(query, (std::numeric_limits<std::size_t>::max)(), output);
}
/*!
\brief finds at most `k` elements within a specific radius that are
nearest to the center of the sphere `query`: if `query` does not contain
at least `k` elements, only contained elements will be returned.
This function is useful when the user already knows how sparse the elements are,
or if they do not care about elements that are too far away.
Setting a small radius may have performance benefits.
\tparam OutputIterator must be a model of `OutputIterator` that accepts `GeomTraits::Node_data_element`
\param query the region to search within
\param k the number of elements to find
\param output the output iterator to add the found elements to (in order of increasing distance)
\warning Nearest neighbor searches requires `GeomTraits` to be a model of `CollectionPartitioningOrthtreeTraits`.
*/
template <typename OutputIterator, typename Orthtree = Self>
auto nearest_k_neighbors_within_radius(
const Sphere& query,
std::size_t k,
OutputIterator output
) const -> std::enable_if_t<Orthtree::supports_neighbor_search, OutputIterator> {
CGAL_precondition(k > 0);
Sphere query_sphere = query;
// todo: this type is over-constrained, this must be made more generic
struct Node_element_with_distance {
typename Traits::Node_data_element element;
FT distance;
};
// Create an empty list of elements
std::vector<Node_element_with_distance> element_list;
if (k != (std::numeric_limits<std::size_t>::max)())
element_list.reserve(k);
// Invoking the recursive function adds those elements to the vector (passed by reference)
nearest_k_neighbors_recursive(query_sphere, root(), element_list, k);
// Add all the points found to the output
for (auto& item : element_list)
*output++ = item.element;
return output;
}
/*!
\brief finds the leaf nodes that intersect with any primitive.
\note this function requires the function
`bool CGAL::do_intersect(QueryType, Traits::Bbox_d)` to be defined.
This function finds all the intersecting leaf nodes and writes their indices to the output iterator.
\tparam Query the primitive class (e.g., sphere, ray)
\tparam OutputIterator a model of `OutputIterator` that accepts `Node_index` types
\param query the intersecting primitive.
\param output output iterator.
\return the output iterator after writing
*/
template <typename Query, typename OutputIterator>
OutputIterator intersected_nodes(const Query& query, OutputIterator output) const {
return intersected_nodes_recursive(query, root(), output);
}
/// @}
/// \name Operators
/// @{
/*!
\brief compares the topology of the orthtree with that of `rhs`.
Trees may be considered equivalent even if they have different contents.
Equivalent trees must have the same root bounding box and the same node structure.
\param rhs the other orthtree
\return `true` if the trees have the same topology, and `false` otherwise
*/
bool operator==(const Self& rhs) const {
// Identical trees should have the same bounding box
if (rhs.m_bbox != m_bbox || rhs.m_side_per_depth[0] != m_side_per_depth[0])
return false;
// Identical trees should have the same depth
if (rhs.depth() != depth())
return false;
// If all else is equal, recursively compare the trees themselves
return is_topology_equal(*this, rhs);
}
/*!
\brief compares the topology of the orthtree with that of `rhs`.
\param rhs the other orthtree
\return `false` if the trees have the same topology, and `true` otherwise
*/
bool operator!=(const Self& rhs) const {
return !operator==(rhs);
}
/// @}
/// \name Node Access
/// @{
/*!
\brief determines whether the node specified by index `n` is a leaf node.
*/
bool is_leaf(Node_index n) const {
return !m_node_children[n].has_value();
}
/*!
\brief determines whether the node specified by index `n` is the root node.
*/
bool is_root(Node_index n) const {
return n == 0;
}
/*!
\brief determines the depth of the node specified.
The root node has depth 0, its children have depth 1, and so on.
\param n index of the node to check.
\return the depth of the node n within its tree.
*/
std::size_t depth(Node_index n) const {
return m_node_depths[n];
}
/*!
\brief retrieves a reference to the `Node_data` associated with the node specified by `n` if
`GeomTraits` is a model of `OrthtreeTraitswithData`, and `nullptr` otherwise.
*/
std::conditional_t<has_data,Node_data&,void*>& data(Node_index n){
return m_node_contents[n];
}
/*!
\brief retrieves a const reference to the `Node_data` associated with the node specified by `n` if
`GeomTraits` is a model of `OrthtreeTraitswithData`, and `nullptr` otherwise.
*/
std::conditional_t<has_data,const Node_data&,void*> data(Node_index n) const{
return m_node_contents[n];
}
/*!
\brief retrieves the global coordinates of the node.
*/
Global_coordinates global_coordinates(Node_index n) const {
return m_node_coordinates[n];
}
/*!
\brief retrieves the local coordinates of the node.
*/
Local_coordinates local_coordinates(Node_index n) const {
Local_coordinates result;
for (std::size_t i = 0; i < dimension; ++i)
result[i] = global_coordinates(n)[i] & 1;
return result;
}
/*!
\brief returns this n's parent.
\pre `!is_root()`
\param n index of the node to retrieve the parent of
\return the index of the parent of node n
*/
Node_index parent(Node_index n) const {
CGAL_precondition (!is_root(n));
return *m_node_parents[n];
}
/*!
\brief returns a node's `i`th child.
\pre `!is_leaf()`
\param n index of the node to retrieve the child of
\param i in [0, 2^D) specifying the child to retrieve
\return the index of the `i`th child of node n
*/
Node_index child(Node_index n, std::size_t i) const {
CGAL_precondition (!is_leaf(n));
return *m_node_children[n] + i;
}
/*!
\brief retrieves an arbitrary descendant of the node specified by `node`.
Convenience function to avoid the need to call `orthtree.child(orthtree.child(node, 0), 1)`.
Each index in `indices` specifies which child to enter as descending the tree from `node` down.
Indices are evaluated in the order they appear as parameters, so
`descendant(root, 0, 1)` returns the second child of the first child of the root.
\param node the node to descend
\param indices the integer indices specifying the descent to perform
\return the index of the specified descendant node
*/
template <typename... Indices>
Node_index descendant(Node_index node, Indices... indices) const {
return recursive_descendant(node, indices...);
}
/*!
\brief convenience function for retrieving arbitrary nodes.
Equivalent to `tree.descendant(tree.root(), indices...)`.
\param indices the integer indices specifying the descent to perform, starting from the root
\return the index of the specified node
*/
template <typename... Indices>
Node_index node(Indices... indices) const {
return descendant(root(), indices...);
}
/*!
\brief finds the next sibling in the parent of the node specified by the index `n`.
Traverses the tree in increasing order of local index (e.g., 000, 001, 010, etc.)
\param n the index of the node to find the sibling of
\return the index of the next sibling of n
if n is not the last node in its parent, otherwise `std::nullopt`.
*/
const std::optional<Node_index> next_sibling(Node_index n) const {
// Root node has no siblings
if (is_root(n)) return {};
// Find out which child this is
std::size_t local_coords = local_coordinates(n).to_ulong();
// The last child has no more siblings
if (int(local_coords) == degree - 1)
return {};
// The next sibling is the child of the parent with the following local coordinates
return child(parent(n), local_coords + 1);
}
/*!
\brief finds the next sibling of the parent of the node specified by `n` if it exists.
\param n the index node to find the sibling up of.
\return The index of the next sibling of the parent of n
if n is not the root and its parent has a sibling, otherwise nothing.
*/
const std::optional<Node_index> next_sibling_up(Node_index n) const {
// the root node has no next sibling up
if (n == 0) return {};
auto up = std::optional<Node_index>{parent(n)};
while (up) {
if (next_sibling(*up)) return {next_sibling(*up)};
up = is_root(*up) ? std::optional<Node_index>{} : std::optional<Node_index>{parent(*up)};
}
return {};
}
/*!
\brief finds the leaf node reached when descending the tree and always choosing child 0.
This is the starting point of a depth-first traversal.
\param n the index of the node to find the deepest first child of.
\return the index of the deepest first child of node n.
*/
Node_index deepest_first_child(Node_index n) const {
auto first = n;
while (!is_leaf(first))
first = child(first, 0);
return first;
}
/*!
\brief finds node reached when descending the tree to a depth `d` and always choosing child 0.
Similar to `deepest_first_child()`, but does go to a fixed depth.
\param n the index of the node to find the `d`th first child of.
\param d the depth to descend to.
\return the index of the `d`th first child, nothing if the tree is not deep enough.
*/
std::optional<Node_index> first_child_at_depth(Node_index n, std::size_t d) const {
std::queue<Node_index> todo;
todo.push(n);
while (!todo.empty()) {
Node_index node = todo.front();
todo.pop();
if (depth(node) == d)
return node;
if (!is_leaf(node))
for (int i = 0; i < degree; ++i)
todo.push(child(node, i));
}
return {};
}
/*!
\brief splits a node into subnodes.
Only leaf nodes should be split.
When a node is split it is no longer a leaf node.
The full set of `degree` children are constructed automatically, and their values are set.
Contents of this node are _not_ propagated automatically, this is responsibility of the
`distribute_node_contents_object` in the traits class.
\param n index of the node to split
*/
void split(Node_index n) {
// Make sure the node hasn't already been split
CGAL_precondition (is_leaf(n));
// Split the node to create children
using Local_coordinates = Local_coordinates;
m_node_children[n] = m_node_properties.emplace_group(degree);
for (std::size_t i = 0; i < degree; i++) {
Node_index c = *m_node_children[n] + i;
// Make sure the node isn't one of its own children
CGAL_assertion(n != *m_node_children[n] + i);
Local_coordinates local_coordinates{i};
for (int i = 0; i < dimension; i++)
m_node_coordinates[c][i] = (2 * m_node_coordinates[n][i]) + local_coordinates[i];
m_node_depths[c] = m_node_depths[n] + 1;
m_node_parents[c] = n;
}
// Check if we've reached a new max depth
if (depth(n) + 1 == m_side_per_depth.size()) {
// Update the side length map with the dimensions of the children
Bbox_dimensions size = m_side_per_depth.back();
Bbox_dimensions child_size;
for (int i = 0; i < dimension; ++i)
child_size[i] = size[i] / FT(2);
m_side_per_depth.push_back(child_size);
}
// Find the point around which the node is split
Point center = barycenter(n);
// Add the node's contents to its children
if constexpr (has_data)
m_traits.distribute_node_contents_object()(n, *this, center);
}
/*!
* \brief returns the center point of a node.
*
* @param n index of the node to find the center point for
*
* @return the center point of node n
*/
Point barycenter(Node_index n) const {
std::size_t node_depth = depth(n);
// the barycenter is computed as the lower corner of the lexicographically top child node
std::array<FT, dimension> bary;
for (int i = 0; i < dimension; i++)
bary[i] = compute_cartesian_coordinate(2 * global_coordinates(n)[i]+1, node_depth+1, i);
return std::apply(m_traits.construct_point_d_object(), bary);
}
/*!
\brief determines whether a pair of subtrees have the same topology.
\param lhsNode index of a node in lhsTree
\param lhsTree an Orthtree
\param rhsNode index of a node in rhsTree
\param rhsTree another Orthtree
@return true if lhsNode and rhsNode have the same topology, false otherwise
*/
static bool is_topology_equal(Node_index lhsNode, const Self& lhsTree, Node_index rhsNode, const Self& rhsTree) {
// If one node is a leaf, and the other isn't, they're not the same
if (lhsTree.is_leaf(lhsNode) != rhsTree.is_leaf(rhsNode))
return false;
// If both nodes are non-leaf
if (!lhsTree.is_leaf(lhsNode)) {
// Check all the children
for (int i = 0; i < degree; ++i) {
// If any child cell is different, they're not the same
if (!is_topology_equal(lhsTree.child(lhsNode, i), lhsTree,
rhsTree.child(rhsNode, i), rhsTree))
return false;
}
}
return (lhsTree.global_coordinates(lhsNode) == rhsTree.global_coordinates(rhsNode));
}
/*!
\brief helper function for calling `is_topology_equal()` on the root nodes of two trees.
\param lhs an Orthtree
\param rhs another Orthtree
\return `true` if `lhs` and `rhs` have the same topology, and `false` otherwise
*/
static bool is_topology_equal(const Self& lhs, const Self& rhs) {
return is_topology_equal(lhs.root(), lhs, rhs.root(), rhs);
}
/*!
\brief finds the directly adjacent node in a specific direction
\pre `direction.to_ulong < 2 * dimension`
Adjacent nodes are found according to several properties:
- adjacent nodes may be larger than the seek node, but never smaller
- a node has at most `2 * dimension` different adjacent nodes (in 3D: left, right, up, down, front, back)
- adjacent nodes are not required to be leaf nodes
Here's a diagram demonstrating the concept for a quadtree:
```
+---------------+---------------+
| | |
| | |
| | |
| A | |
| | |
| | |
| | |
+-------+-------+---+---+-------+
| | | | | |
| A | (S) +---A---+ |
| | | | | |
+---+---+-------+---+---+-------+
| | | | | |
+---+---+ A | | |
| | | | | |
+---+---+-------+-------+-------+
```
- (S) : Seek node
- A : Adjacent node
Note how the top adjacent node is larger than the seek node. The
right adjacent node is the same size, even though it contains
further subdivisions.
This implementation returns the adjacent node if it's found. If
there is no adjacent node in that direction, it returns a null
node.
\param n index of the node to find a neighbor of
\param direction which way to find the adjacent node relative to
this one. Each successive bit selects the direction for the
corresponding dimension: for an octree in 3D, 010 means: negative
direction in X, position direction in Y, negative direction in Z.
\return the index of the adjacent node if it exists, nothing otherwise.
*/
std::optional<Node_index> adjacent_node(Node_index n, const Local_coordinates& direction) const {
// Direction: LEFT RIGHT DOWN UP BACK FRONT
// direction: 000 001 010 011 100 101
// Nodes only have up to 2*dim different adjacent nodes (since boxes have 6 sides)
CGAL_precondition(direction.to_ulong() < dimension * 2);
// The root node has no adjacent nodes!
if (is_root(n)) return {};
// The least significant bit indicates the sign (which side of the node)
bool sign = direction[0];
// The first two bits indicate the dimension/axis (x, y, z)
uint8_t dim = uint8_t((direction >> 1).to_ulong());
// Create an offset so that the bit-significance lines up with the dimension (e.g., 1, 2, 4 --> 001, 010, 100)
int8_t offset = (uint8_t) 1 << dim;
// Finally, apply the sign to the offset
offset = (sign ? offset : -offset);
// Check if this child has the opposite sign along the direction's axis
if (local_coordinates(n)[dim] != sign) {
// This means the adjacent node is a direct sibling, the offset can be applied easily!
return {child(parent(n), local_coordinates(n).to_ulong() + offset)};
}
// Find the parent's neighbor in that direction, if it exists
auto adjacent_node_of_parent = adjacent_node(parent(n), direction);
// If the parent has no neighbor, then this node doesn't have one
if (!adjacent_node_of_parent) return {};
// If the parent's adjacent node has no children, then it's this node's adjacent node
if (is_leaf(*adjacent_node_of_parent))
return adjacent_node_of_parent;
// Return the nearest node of the parent by subtracting the offset instead of adding
return {child(*adjacent_node_of_parent, local_coordinates(n).to_ulong() - offset)};
}
/*!
\brief equivalent to `adjacent_node()`, with an adjacency direction rather than a bitset.
\param n index of the node to find a neighbor of
\param adjacency which way to find the adjacent node relative to this one
*/
std::optional<Node_index> adjacent_node(Node_index n, Adjacency adjacency) const {
return adjacent_node(n, std::bitset<dimension>(static_cast<int>(adjacency)));
}
/// @}
private: // functions :
Node_index recursive_descendant(Node_index node, std::size_t i) const { return child(node, i); }
template <typename... Indices>
Node_index recursive_descendant(Node_index node, std::size_t i, Indices... remaining_indices) const {
return recursive_descendant(child(node, i), remaining_indices...);
}
bool do_intersect(Node_index n, const Sphere& sphere) const {
// Create a bounding box from the node
Bbox node_box = bbox(n);
// Check for intersection between the node and the sphere
return CGAL::do_intersect(node_box, sphere);
}
template <typename Query, typename Node_output_iterator>
Node_output_iterator intersected_nodes_recursive(const Query& query, Node_index node,
Node_output_iterator output) const {
// Check if the current node intersects with the query
if (CGAL::do_intersect(query, bbox(node))) {
// if this node is a leaf, then it's considered an intersecting node
if (is_leaf(node)) {
*output++ = node;
return output;
}
// Otherwise, each of the children need to be checked
for (int i = 0; i < degree; ++i) {
intersected_nodes_recursive(query, child(node, i), output);
}
}
return output;
}
template <typename Result, typename Orthtree = Self>
auto nearest_k_neighbors_recursive(
Sphere& search_bounds,
Node_index node,
std::vector<Result>& results,
std::size_t k,
FT epsilon = 0) const -> std::enable_if_t<Orthtree::supports_neighbor_search> {
// Check whether the node has children
if (is_leaf(node)) {
// Base case: the node has no children
// Loop through each of the elements contained by the node
// Note: there might be none, and that should be fine!
for (auto& e : data(node)) {
// Pair that element with its distance from the search point
Result current_element_with_distance =
{ e, m_traits.squared_distance_of_element_object()(e, m_traits.construct_center_d_object()(search_bounds)) };
// Check if the new element is within the bounds
if (current_element_with_distance.distance < m_traits.compute_squared_radius_d_object()(search_bounds)) {
// Check if the results list is full
if (results.size() == k) {
// Delete a element if we need to make room
results.pop_back();
}
// Add the new element
results.push_back(current_element_with_distance);
// Sort the list
std::sort(results.begin(), results.end(), [=](auto& left, auto& right) {
return left.distance < right.distance;
});
// Check if the results list is full
if (results.size() == k) {
// Set the search radius
search_bounds = m_traits.construct_sphere_d_object()(m_traits.construct_center_d_object()(search_bounds), results.back().distance + epsilon);
}
}
}
}
else {
struct Node_index_with_distance {
Node_index index;
FT distance;
Node_index_with_distance(const Node_index& index, FT distance) :
index(index), distance(distance) {}
};
// Recursive case: the node has children
// Create a list to map children to their distances
std::vector<Node_index_with_distance> children_with_distances;
children_with_distances.reserve(Self::degree);
// Fill the list with child nodes
for (int i = 0; i < Self::degree; ++i) {
auto child_node = child(node, i);
FT squared_distance = 0;
Point c = m_traits.construct_center_d_object()(search_bounds);
Point b = barycenter(child_node);
for (const auto r : cartesian_range(c, b)) {
FT d = (get<0>(r) - get<1>(r));
squared_distance += d * d;
}
// Add a child to the list, with its distance
children_with_distances.emplace_back(
child_node,
squared_distance
);
}
// Sort the children by their distance from the search point
std::sort(children_with_distances.begin(), children_with_distances.end(), [=](auto& left, auto& right) {
return left.distance < right.distance;
});
// Loop over the children
for (auto child_with_distance : children_with_distances) {
// Check whether the bounding box of the child intersects with the search bounds
if (CGAL::do_intersect(bbox(child_with_distance.index), search_bounds)) {
// Recursively invoke this function
nearest_k_neighbors_recursive(search_bounds, child_with_distance.index, results, k);
}
}
}
}
public:
/// \cond SKIP_IN_MANUAL
template <class K>
void dump_box_to_polylines(const Iso_rectangle_2<K>& box, std::ostream& os) const {
// dump in 3D for visualization
os << "5 "
<< box.xmin() << " " << box.ymin() << " 0 "
<< box.xmin() << " " << box.ymax() << " 0 "
<< box.xmax() << " " << box.ymax() << " 0 "
<< box.xmax() << " " << box.ymin() << " 0 "
<< box.xmin() << " " << box.ymin() << " 0" << std::endl;
}
template <class K>
void dump_box_to_polylines(const Iso_cuboid_3<K>& box, std::ostream& os) const {
// Back face
os << "5 "
<< box.xmin() << " " << box.ymin() << " " << box.zmin() << " "
<< box.xmin() << " " << box.ymax() << " " << box.zmin() << " "
<< box.xmax() << " " << box.ymax() << " " << box.zmin() << " "
<< box.xmax() << " " << box.ymin() << " " << box.zmin() << " "
<< box.xmin() << " " << box.ymin() << " " << box.zmin() << std::endl;
// Front face
os << "5 "
<< box.xmin() << " " << box.ymin() << " " << box.zmax() << " "
<< box.xmin() << " " << box.ymax() << " " << box.zmax() << " "
<< box.xmax() << " " << box.ymax() << " " << box.zmax() << " "
<< box.xmax() << " " << box.ymin() << " " << box.zmax() << " "
<< box.xmin() << " " << box.ymin() << " " << box.zmax() << std::endl;
// Traversal edges
os << "2 "
<< box.xmin() << " " << box.ymin() << " " << box.zmin() << " "
<< box.xmin() << " " << box.ymin() << " " << box.zmax() << std::endl;
os << "2 "
<< box.xmin() << " " << box.ymax() << " " << box.zmin() << " "
<< box.xmin() << " " << box.ymax() << " " << box.zmax() << std::endl;
os << "2 "
<< box.xmax() << " " << box.ymin() << " " << box.zmin() << " "
<< box.xmax() << " " << box.ymin() << " " << box.zmax() << std::endl;
os << "2 "
<< box.xmax() << " " << box.ymax() << " " << box.zmin() << " "
<< box.xmax() << " " << box.ymax() << " " << box.zmax() << std::endl;
}
void dump_to_polylines(std::ostream& os) const {
for (Node_index n: traverse(Orthtrees::Preorder_traversal<Self>(*this)))
if (is_leaf(n)) {
Bbox box = bbox(n);
dump_box_to_polylines(box, os);
}
}
std::string to_string(Node_index node) {
std::stringstream stream;
internal::print_orthtree_node(stream, node, *this);
return stream.str();
}
friend std::ostream& operator<<(std::ostream& os, const Self& orthtree) {
// Iterate over all nodes
for (auto n: orthtree.traverse(Orthtrees::Preorder_traversal<Self>(orthtree))) {
// Show the depth
for (std::size_t i = 0; i < orthtree.depth(n); ++i)
os << ". ";
// Print the node
internal::print_orthtree_node(os, n, orthtree);
os << std::endl;
}
return os;
}
/// \endcond
}; // end class Orthtree
} // namespace CGAL
#endif // CGAL_ORTHTREE_H
|