Skip to content

Commit

Permalink
Refactor spatial join tests (#1019)
Browse files Browse the repository at this point in the history
Closes #1006
Closes #1007

I realized that the `join_quadtree_and_bounding_boxes()` tests are redundant to the `quadtree_point_in_polygon` tests and `quadtree_point_to_nearest_linestring` tests since the "small" test is duplicated in those. So I eliminated that test and added expectations for `join_quadtree_and_bounding_boxes` in those tests.

Authors:
  - Mark Harris (https://github.com/harrism)

Approvers:
  - Robert Maynard (https://github.com/robertmaynard)
  - Paul Taylor (https://github.com/trxcllnt)

URL: #1019
  • Loading branch information
harrism authored Apr 5, 2023
1 parent c5d8240 commit ccf526a
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 649 deletions.
9 changes: 9 additions & 0 deletions cpp/include/cuspatial/experimental/geometry/box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class alignas(sizeof(Vertex)) box {
using value_type = T;
Vertex v1;
Vertex v2;

private:
/**
* @brief Output stream operator for `vec_2d<T>` for human-readable formatting
*/
friend std::ostream& operator<<(std::ostream& os, cuspatial::box<T> const& b)
{
return os << "{" << b.v1 << ", " << b.v2 << "}";
}
};

// deduction guide, enables CTAD
Expand Down
2 changes: 0 additions & 2 deletions cpp/include/cuspatial/experimental/point_quadtree.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ namespace cuspatial {
* @{
*/

struct point_quadtree_ref;

struct point_quadtree {
// uint32_t vector of quad node keys
rmm::device_uvector<uint32_t> key;
Expand Down
14 changes: 7 additions & 7 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ConfigureTest(PAIRWISE_POINT_IN_POLYGON_TEST
spatial/pairwise_point_in_polygon_test.cpp)

ConfigureTest(POINT_QUADTREE_TEST
indexing/point_quadtree_test.cu)
indexing/point_quadtree_test.cpp)

ConfigureTest(LINESTRING_BOUNDING_BOXES_TEST
spatial/linestring_bounding_boxes_test.cpp)
Expand All @@ -96,11 +96,8 @@ ConfigureTest(LINESTRING_INTERSECTION_TEST
ConfigureTest(POINT_LINESTRING_NEAREST_POINT_TEST
spatial/point_linestring_nearest_points_test.cpp)

ConfigureTest(QUADTREE_POLYGON_FILTERING_TEST
join/quadtree_polygon_filtering_test.cu)

ConfigureTest(QUADTREE_LINESTRING_FILTERING_TEST
join/quadtree_linestring_filtering_test.cu)
ConfigureTest(JOIN_QUADTREE_AND_BOUNDING_BOXES_TEST
join/join_quadtree_and_bounding_boxes_test.cpp)

ConfigureTest(TRAJECTORY_DISTANCES_AND_SPEEDS_TEST
trajectory/test_trajectory_distances_and_speeds.cu)
Expand All @@ -120,7 +117,7 @@ ConfigureTest(UTILITY_TEST
utility_test/test_geometry_generators.cu
)

# Experimental API
# Header-only API
ConfigureTest(HAVERSINE_TEST_EXP
experimental/spatial/haversine_test.cu)

Expand Down Expand Up @@ -187,6 +184,9 @@ ConfigureTest(FIND_TEST_EXP
experimental/find/find_points_on_segments_test.cu
experimental/find/find_duplicate_points_test.cu)

ConfigureTest(JOIN_QUADTREE_AND_BOUNDING_BOXES_TEST_EXP
experimental/join/join_quadtree_and_bounding_boxes_test.cu)

ConfigureTest(JOIN_POINT_IN_POLYGON_SMALL_TEST_EXP
experimental/join/quadtree_point_in_polygon_test_small.cu)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <cuspatial_test/base_fixture.hpp>
#include <cuspatial_test/vector_equality.hpp>
#include <cuspatial_test/vector_factories.cuh>

#include <cuspatial/error.hpp>
#include <cuspatial/experimental/point_quadtree.cuh>
#include <cuspatial/experimental/spatial_join.cuh>

// Note: the detailed correctness test of the join_quadtree_and_bounding_boxes() function is covered
// by the quadtree_point_in_polygon_test_small.cu test file.

template <typename T>
struct JoinQuadtreeAndBoundingBoxesErrorTest : public cuspatial::test::BaseFixture {
};

using TestTypes = ::testing::Types<float, double>;

TYPED_TEST_CASE(JoinQuadtreeAndBoundingBoxesErrorTest, TestTypes);

TYPED_TEST(JoinQuadtreeAndBoundingBoxesErrorTest, test_empty)
{
using T = TypeParam;

using namespace cuspatial;
using cuspatial::vec_2d;
using cuspatial::test::make_device_vector;

vec_2d<T> v_min{0.0, 0.0};
T const scale{1.0};
uint8_t const max_depth{1};

auto empty_quadtree = point_quadtree_ref{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};

auto empty_bboxes = rmm::device_uvector<box<T>>{0, this->stream()};

auto [polygon_idx, quad_idx] = cuspatial::join_quadtree_and_bounding_boxes(empty_quadtree,
empty_bboxes.begin(),
empty_bboxes.end(),
v_min,
scale,
max_depth,
this->stream());

auto expected_polygon_idx = rmm::device_uvector<std::uint32_t>{0, this->stream()};
auto expected_quad_idx = rmm::device_uvector<std::uint32_t>{0, this->stream()};

CUSPATIAL_EXPECT_VECTORS_EQUIVALENT(expected_polygon_idx, polygon_idx);
CUSPATIAL_EXPECT_VECTORS_EQUIVALENT(expected_quad_idx, quad_idx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ TYPED_TEST(PIPRefineTestSmall, TestSmall)
auto [poly_indices, quad_indices] = cuspatial::join_quadtree_and_bounding_boxes(
quadtree, bboxes.begin(), bboxes.end(), v_min, scale, max_depth, this->stream(), this->mr());

{
auto expected_poly_indices = make_device_vector<uint32_t>({3, 3, 1, 2, 1, 1, 0, 3});
auto expected_quad_indices = make_device_vector<uint32_t>({10, 11, 6, 6, 12, 13, 2, 2});

CUSPATIAL_EXPECT_VECTORS_EQUIVALENT(poly_indices, expected_poly_indices);
CUSPATIAL_EXPECT_VECTORS_EQUIVALENT(quad_indices, expected_quad_indices);
}

auto [poly_offset, point_offset] = cuspatial::quadtree_point_in_polygon(poly_indices.begin(),
poly_indices.end(),
quad_indices.begin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ TYPED_TEST(QuadtreePointToLinestringTestSmall, TestSmall)
auto [linestring_indices, quad_indices] = cuspatial::join_quadtree_and_bounding_boxes(
quadtree, bboxes.begin(), bboxes.end(), v_min, scale, max_depth, this->stream(), this->mr());

// This tests the output of `join_quadtree_and_bounding_boxes` for correctness, rather than
// repeating this test standalone.
{
auto expected_linestring_indices =
make_device_vector<uint32_t>({3, 1, 2, 3, 3, 0, 1, 2, 3, 0, 3, 1, 2, 3, 1, 2, 1, 2, 0, 1, 3});
auto expected_quad_indices = make_device_vector<uint32_t>(
{3, 8, 8, 8, 9, 10, 10, 10, 10, 11, 11, 6, 6, 6, 12, 12, 13, 13, 2, 2, 2});

CUSPATIAL_EXPECT_VECTORS_EQUIVALENT(linestring_indices, expected_linestring_indices);
CUSPATIAL_EXPECT_VECTORS_EQUIVALENT(quad_indices, expected_quad_indices);
}

auto [point_idx, linestring_idx, distance] =
cuspatial::quadtree_point_to_nearest_linestring(linestring_indices.begin(),
linestring_indices.end(),
Expand Down
74 changes: 74 additions & 0 deletions cpp/tests/indexing/point_quadtree_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <cuspatial/error.hpp>
#include <cuspatial/point_quadtree.hpp>

#include <cudf/column/column_view.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>

#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>

struct QuadtreeOnPointErrorTest : public cudf::test::BaseFixture {
};

TYPED_TEST_CASE(QuadtreeOnPointIndexingTest, cudf::test::FloatingPointTypes);

TEST_F(QuadtreeOnPointErrorTest, test_empty)
{
using T = float;
using namespace cudf::test;
const int8_t max_depth = 1;
uint32_t min_size = 1;
double scale = 1.0;
double x_min = 0, x_max = 1, y_min = 0, y_max = 1;

fixed_width_column_wrapper<T> x({});
fixed_width_column_wrapper<T> y({});

auto quadtree_pair =
cuspatial::quadtree_on_points(x, y, x_min, x_max, y_min, y_max, scale, max_depth, min_size);
auto& quadtree = std::get<1>(quadtree_pair);

CUSPATIAL_EXPECTS(
quadtree->num_columns() == 5,
"a quadtree table must have 5 columns (keys, levels, is_node, lengths, offsets)");

CUSPATIAL_EXPECTS(quadtree->num_rows() == 0,
"the resulting quadtree must have a single quadrant");
}

TEST_F(QuadtreeOnPointErrorTest, test_x_y_size_mismatch)
{
using T = float;
using namespace cudf::test;
const int8_t max_depth = 1;
uint32_t min_size = 1;
double scale = 1.0;
double x_min = 0, x_max = 1, y_min = 0, y_max = 1;

fixed_width_column_wrapper<T> x({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
fixed_width_column_wrapper<T> y({0, 1, 2, 3, 4, 5, 6, 7, 8});

EXPECT_THROW(
cuspatial::quadtree_on_points(x, y, x_min, x_max, y_min, y_max, scale, max_depth, min_size),
cuspatial::logic_error);
}
Loading

0 comments on commit ccf526a

Please sign in to comment.