Skip to content

Commit

Permalink
Add VoxelArea::intersect()
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jun 23, 2023
1 parent 659828b commit 84fb663
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/unittest/test_voxelarea.cpp
Expand Up @@ -38,6 +38,7 @@ class TestVoxelArea : public TestBase
void test_equal();
void test_plus();
void test_minor();
void test_intersect();
void test_index_xyz_all_pos();
void test_index_xyz_x_neg();
void test_index_xyz_y_neg();
Expand Down Expand Up @@ -74,6 +75,7 @@ void TestVoxelArea::runTests(IGameDef *gamedef)
TEST(test_equal);
TEST(test_plus);
TEST(test_minor);
TEST(test_intersect);
TEST(test_index_xyz_all_pos);
TEST(test_index_xyz_x_neg);
TEST(test_index_xyz_y_neg);
Expand Down Expand Up @@ -210,6 +212,20 @@ void TestVoxelArea::test_minor()
VoxelArea(v3s16(-10, -10, -45), v3s16(100, 100, 65)));
}

void TestVoxelArea::test_intersect()
{
VoxelArea v1({-10, -10, -10}, {10, 10, 10});
VoxelArea v2({1, 2, 3}, {4, 5, 6});
VoxelArea v3({11, 11, 11}, {11, 11, 11});
VoxelArea v4({-11, -2, -10}, {10, 2, 11});
UASSERT(v2.intersect(v1) == v2);
UASSERT(v1.intersect(v2) == v2.intersect(v1));
UASSERT(v1.intersect(v3).hasEmptyExtent());
UASSERT(v3.intersect(v1) == v1.intersect(v3));
UASSERT(v1.intersect(v4) ==
VoxelArea({-10, -2, -10}, {10, 2, 10}));
}

void TestVoxelArea::test_index_xyz_all_pos()
{
VoxelArea v1;
Expand Down
25 changes: 25 additions & 0 deletions src/voxel.h
Expand Up @@ -183,6 +183,31 @@ class VoxelArea
return {MinEdge-off, MaxEdge-off};
}

/*
Returns the intersection of this area and `a`.
*/
VoxelArea intersect(const VoxelArea &a) const
{
// This is an example of an operation that would be simpler with
// non-inclusive edges, but oh well.
VoxelArea ret;

if (a.MaxEdge.X < MinEdge.X || a.MinEdge.X > MaxEdge.X)
return VoxelArea();
if (a.MaxEdge.Y < MinEdge.Y || a.MinEdge.Y > MaxEdge.Y)
return VoxelArea();
if (a.MaxEdge.Z < MinEdge.Z || a.MinEdge.Z > MaxEdge.Z)
return VoxelArea();
ret.MinEdge.X = std::max(a.MinEdge.X, MinEdge.X);
ret.MaxEdge.X = std::min(a.MaxEdge.X, MaxEdge.X);
ret.MinEdge.Y = std::max(a.MinEdge.Y, MinEdge.Y);
ret.MaxEdge.Y = std::min(a.MaxEdge.Y, MaxEdge.Y);
ret.MinEdge.Z = std::max(a.MinEdge.Z, MinEdge.Z);
ret.MaxEdge.Z = std::min(a.MaxEdge.Z, MaxEdge.Z);

return ret;
}

/*
Returns 0-6 non-overlapping areas that can be added to
a to make up this area.
Expand Down

0 comments on commit 84fb663

Please sign in to comment.