adjacent_node() now returns an optional index

This commit is contained in:
JacksonCampolattaro 2023-04-25 10:57:56 +02:00
parent 825abd1727
commit d761f6ebc0
2 changed files with 34 additions and 30 deletions

View File

@ -392,7 +392,7 @@ public:
for (int direction = 0; direction < 6; ++direction) { for (int direction = 0; direction < 6; ++direction) {
// Get the neighbor // Get the neighbor
auto neighbor = index(adjacent_node(node, direction)); auto neighbor = adjacent_node(node, direction);
// If it doesn't exist, skip it // If it doesn't exist, skip it
if (!neighbor) if (!neighbor)
@ -977,8 +977,8 @@ public:
} }
const Node* adjacent_node(Node_index n, typename Node::Local_coordinates direction) const { boost::optional<Node_index> adjacent_node(Node_index n, typename Node::Local_coordinates direction) const {
return adjacent_node(m_nodes[n], direction); return {index(adjacent_node(m_nodes[n], direction))};
} }
/*! /*!
@ -988,10 +988,6 @@ public:
return adjacent_node(node, std::bitset<Dimension::value>(static_cast<int>(adjacency))); return adjacent_node(node, std::bitset<Dimension::value>(static_cast<int>(adjacency)));
} }
const Node* adjacent_node(Node_index n, typename Node::Adjacency adjacency) const {
return adjacent_node(m_nodes[n], adjacency);
}
/*! /*!
* \brief equivalent to adjacent_node, except non-const * \brief equivalent to adjacent_node, except non-const
*/ */

View File

@ -42,39 +42,47 @@ int main(void) {
std::cout << octree << std::endl; std::cout << octree << std::endl;
// Root node should have no siblings // Root node should have no siblings
assert(octree.adjacent_node(octree.root(), 0) == nullptr); assert(!octree.adjacent_node(octree.index(octree.root()), 0));
assert(octree.adjacent_node(octree.root(), 1) == nullptr); assert(!octree.adjacent_node(octree.index(octree.root()), 1));
assert(octree.adjacent_node(octree.root(), 2) == nullptr); assert(!octree.adjacent_node(octree.index(octree.root()), 2));
assert(octree.adjacent_node(octree.root(), 3) == nullptr); assert(!octree.adjacent_node(octree.index(octree.root()), 3));
assert(octree.adjacent_node(octree.root(), 4) == nullptr); assert(!octree.adjacent_node(octree.index(octree.root()), 4));
assert(octree.adjacent_node(octree.root(), 5) == nullptr); assert(!octree.adjacent_node(octree.index(octree.root()), 5));
// Left Top Front node should have siblings to the Right, Down, and Back // Left Top Front node should have siblings to the Right, Down, and Back
auto left_top_back = octree.children(octree.root())[Traits::LEFT_TOP_BACK]; auto left_top_back = octree.index(octree.children(octree.root())[Traits::LEFT_TOP_BACK]);
assert(&octree.children(octree.root())[Traits::RIGHT_TOP_BACK] == octree.adjacent_node(left_top_back, Traits::RIGHT)); assert(octree.index(&octree.children(octree.root())[Traits::RIGHT_TOP_BACK]) ==
octree.adjacent_node(left_top_back, Traits::RIGHT));
assert( assert(
&octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK] == octree.adjacent_node(left_top_back, Traits::DOWN)); octree.index(&octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK]) ==
assert(&octree.children(octree.root())[Traits::LEFT_TOP_FRONT] == octree.adjacent_node(left_top_back, Traits::FRONT)); octree.adjacent_node(left_top_back, Traits::DOWN));
assert(octree.adjacent_node(left_top_back, Traits::LEFT) == nullptr); assert(octree.index(&octree.children(octree.root())[Traits::LEFT_TOP_FRONT]) ==
assert(octree.adjacent_node(left_top_back, Traits::UP) == nullptr); octree.adjacent_node(left_top_back, Traits::FRONT));
assert(octree.adjacent_node(left_top_back, Traits::BACK) == nullptr); assert(!octree.adjacent_node(left_top_back, Traits::LEFT));
assert(!octree.adjacent_node(left_top_back, Traits::UP));
assert(!octree.adjacent_node(left_top_back, Traits::BACK));
std::cout << octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK] << std::endl; std::cout << octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK] << std::endl;
auto right_top_back_of_left_bottom_back = auto right_top_back_of_left_bottom_back =
octree.children(octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK])[Traits::RIGHT_TOP_BACK]; octree.index(octree.children(octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK])[Traits::RIGHT_TOP_BACK]);
assert(&octree.children(octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK])[Traits::LEFT_TOP_BACK] ==
octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::LEFT)); assert(
assert(&octree.children(octree.root())[Traits::RIGHT_BOTTOM_BACK] == octree.index(&octree.children(octree.children(octree.root())[Traits::LEFT_BOTTOM_BACK])[Traits::LEFT_TOP_BACK]) ==
octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::RIGHT)); octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::LEFT)
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::RIGHT) != nullptr); );
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::UP) != nullptr); assert(
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::DOWN) != nullptr); octree.index(&octree.children(octree.root())[Traits::RIGHT_BOTTOM_BACK]) ==
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::FRONT) != nullptr); octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::RIGHT)
);
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::RIGHT).has_value());
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::UP).has_value());
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::DOWN).has_value());
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::FRONT).has_value());
// A node at the back of the tree should have no neighbor to its back // A node at the back of the tree should have no neighbor to its back
assert(octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::BACK) == nullptr); assert(!octree.adjacent_node(right_top_back_of_left_bottom_back, Traits::BACK));
return 0; return 0;
} }