CARLA
Loading...
Searching...
No Matches
test_boost_rtree.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7// Regression guard for boost::geometry::index::rtree after the Boost 1.84 ->
8// 1.90 upgrade. CARLA relies on rtree for road/lane spatial queries in
9// carla::road and carla::geom; this suite exercises the public surface
10// (insertion, nearest-neighbor, box queries) that LibCarla depends on.
11
12#include "test.h"
13
14#include <boost/geometry.hpp>
15#include <boost/geometry/geometries/box.hpp>
16#include <boost/geometry/geometries/point.hpp>
17#include <boost/geometry/index/rtree.hpp>
18
19#include <algorithm>
20#include <utility>
21#include <vector>
22
23namespace bg = boost::geometry;
24namespace bgi = boost::geometry::index;
25
26using Point2D = bg::model::point<double, 2, bg::cs::cartesian>;
27using Point3D = bg::model::point<double, 3, bg::cs::cartesian>;
28using Box2D = bg::model::box<Point2D>;
29using Value2D = std::pair<Point2D, std::size_t>;
30using Value3D = std::pair<Point3D, std::size_t>;
31
32TEST(BoostRtree, InsertAndNearestNeighbour2D) {
33 bgi::rtree<Value2D, bgi::quadratic<16>> tree;
34 tree.insert({Point2D{0.0, 0.0}, 0u});
35 tree.insert({Point2D{1.0, 1.0}, 1u});
36 tree.insert({Point2D{10.0, 10.0}, 2u});
37 tree.insert({Point2D{-5.0, -5.0}, 3u});
38
39 std::vector<Value2D> result;
40 tree.query(bgi::nearest(Point2D{0.9, 1.1}, 1), std::back_inserter(result));
41 ASSERT_EQ(result.size(), 1u);
42 EXPECT_EQ(result.front().second, 1u);
43}
44
45TEST(BoostRtree, InsertAndNearestNeighbour3D) {
46 bgi::rtree<Value3D, bgi::quadratic<16>> tree;
47 tree.insert({Point3D{0.0, 0.0, 0.0}, 0u});
48 tree.insert({Point3D{1.0, 1.0, 1.0}, 1u});
49 tree.insert({Point3D{2.0, 2.0, 2.0}, 2u});
50 tree.insert({Point3D{10.0, 10.0, 10.0}, 3u});
51 tree.insert({Point3D{-5.0, -5.0, -5.0}, 4u});
52
53 std::vector<Value3D> result;
54 tree.query(bgi::nearest(Point3D{2.1, 2.2, 1.9}, 1), std::back_inserter(result));
55 ASSERT_EQ(result.size(), 1u);
56 EXPECT_EQ(result.front().second, 2u);
57}
58
59TEST(BoostRtree, WithinBoxQuery2D) {
60 bgi::rtree<Value2D, bgi::quadratic<16>> tree;
61 tree.insert({Point2D{0.5, 0.5}, 0u});
62 tree.insert({Point2D{2.0, 2.0}, 1u});
63 tree.insert({Point2D{5.0, 5.0}, 2u});
64 tree.insert({Point2D{-1.0, -1.0}, 3u});
65
66 const Box2D box{Point2D{0.0, 0.0}, Point2D{3.0, 3.0}};
67 std::vector<Value2D> result;
68 tree.query(bgi::within(box), std::back_inserter(result));
69 ASSERT_EQ(result.size(), 2u);
70
71 std::vector<std::size_t> ids;
72 for (const auto &entry : result) {
73 ids.push_back(entry.second);
74 }
75 std::sort(ids.begin(), ids.end());
76 EXPECT_EQ(ids[0], 0u);
77 EXPECT_EQ(ids[1], 1u);
78}
79
80TEST(BoostRtree, KNearestReturnsExpectedSet) {
81 bgi::rtree<Value2D, bgi::quadratic<16>> tree;
82 tree.insert({Point2D{0.0, 0.0}, 0u});
83 tree.insert({Point2D{1.0, 0.0}, 1u});
84 tree.insert({Point2D{2.0, 0.0}, 2u});
85 tree.insert({Point2D{3.0, 0.0}, 3u});
86
87 std::vector<Value2D> result;
88 tree.query(bgi::nearest(Point2D{0.0, 0.0}, 3), std::back_inserter(result));
89 ASSERT_EQ(result.size(), 3u);
90
91 // The rtree's nearest() query does not guarantee ordering of its k results,
92 // only that they are the k nearest. Verify the expected set membership.
93 std::vector<std::size_t> ids;
94 for (const auto &entry : result) {
95 ids.push_back(entry.second);
96 }
97 std::sort(ids.begin(), ids.end());
98 EXPECT_EQ(ids[0], 0u);
99 EXPECT_EQ(ids[1], 1u);
100 EXPECT_EQ(ids[2], 2u);
101}
bg::model::box< Point2D > Box2D
bg::model::point< double, 2, bg::cs::cartesian > Point2D
TEST(BoostRtree, InsertAndNearestNeighbour2D)
bg::model::point< double, 3, bg::cs::cartesian > Point3D
std::pair< Point2D, std::size_t > Value2D
std::pair< Point3D, std::size_t > Value3D