CARLA
Rtree.h
Go to the documentation of this file.
1 // Copyright (c) 2020 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 #pragma once
8 
9 #include <vector>
10 
11 #if defined(__clang__)
12 # pragma clang diagnostic push
13 # pragma clang diagnostic ignored "-Wshadow"
14 #endif
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/geometries/point.hpp>
17 #include <boost/geometry/index/rtree.hpp>
18 #if defined(__clang__)
19 # pragma clang diagnostic pop
20 #endif
21 
22 
23 namespace carla {
24 namespace geom {
25 
26  /// Rtree class working with 3D point clouds.
27  /// Asociates a T element with a 3D point
28  /// Useful to perform fast k-NN searches
29  template <typename T, size_t Dimension = 3>
31  public:
32 
33  typedef boost::geometry::model::point<float, Dimension, boost::geometry::cs::cartesian> BPoint;
34  typedef std::pair<BPoint, T> TreeElement;
35 
36  void InsertElement(const BPoint &point, const T &element) {
37  _rtree.insert(std::make_pair(point, element));
38  }
39 
40  void InsertElement(const TreeElement &element) {
41  _rtree.insert(element);
42  }
43 
44  void InsertElements(const std::vector<TreeElement> &elements) {
45  _rtree.insert(elements.begin(), elements.end());
46  }
47 
48  /// Return nearest neighbors with a user defined filter.
49  /// The filter reveices as an argument a TreeElement value and needs to
50  /// return a bool to accept or reject the value
51  /// [&](Rtree::TreeElement const &element){if (IsOk(element)) return true;
52  /// else return false;}
53  template <typename Filter>
54  std::vector<TreeElement> GetNearestNeighboursWithFilter(const BPoint &point, Filter filter,
55  size_t number_neighbours = 1) const {
56  std::vector<TreeElement> query_result;
57  auto nearest = boost::geometry::index::nearest(point, static_cast<unsigned int>(number_neighbours));
58  auto satisfies = boost::geometry::index::satisfies(filter);
59  // Using explicit operator&& to workaround Bullseye coverage issue
60  // https://www.bullseye.com/help/trouble-logicalOverload.html.
61  _rtree.query(operator&&(nearest, satisfies), std::back_inserter(query_result));
62  return query_result;
63  }
64 
65  std::vector<TreeElement> GetNearestNeighbours(const BPoint &point, size_t number_neighbours = 1) const {
66  std::vector<TreeElement> query_result;
67  _rtree.query(boost::geometry::index::nearest(point, static_cast<unsigned int>(number_neighbours)),
68  std::back_inserter(query_result));
69  return query_result;
70  }
71 
72  size_t GetTreeSize() const {
73  return _rtree.size();
74  }
75 
76  private:
77 
78  boost::geometry::index::rtree<TreeElement, boost::geometry::index::linear<16>> _rtree;
79 
80  };
81 
82  /// Rtree class working with 3D segment clouds.
83  /// Stores a pair of T elements (one for each end of the segment)
84  /// Useful to perform fast k-NN searches.
85  template <typename T, size_t Dimension = 3>
87  public:
88 
89  typedef boost::geometry::model::point<float, Dimension, boost::geometry::cs::cartesian> BPoint;
90  typedef boost::geometry::model::segment<BPoint> BSegment;
91  typedef std::pair<BSegment, std::pair<T, T>> TreeElement;
92 
93  void InsertElement(const BSegment &segment, const T &element_start, const T &element_end) {
94  _rtree.insert(std::make_pair(segment, std::make_pair(element_start, element_end)));
95  }
96 
97  void InsertElement(const TreeElement &element) {
98  _rtree.insert(element);
99  }
100 
101  void InsertElements(const std::vector<TreeElement> &elements) {
102  _rtree.insert(elements.begin(), elements.end());
103  }
104 
105  /// Return nearest neighbors with a user defined filter.
106  /// The filter reveices as an argument a TreeElement value and needs to
107  /// return a bool to accept or reject the value
108  /// [&](Rtree::TreeElement const &element){if (IsOk(element)) return true;
109  /// else return false;}
110  template <typename Geometry, typename Filter>
111  std::vector<TreeElement> GetNearestNeighboursWithFilter(
112  const Geometry &geometry,
113  Filter filter,
114  size_t number_neighbours = 1) const {
115  std::vector<TreeElement> query_result;
116  _rtree.query(
117  boost::geometry::index::nearest(geometry, static_cast<unsigned int>(number_neighbours)) &&
118  boost::geometry::index::satisfies(filter),
119  std::back_inserter(query_result));
120  return query_result;
121  }
122 
123  template<typename Geometry>
124  std::vector<TreeElement> GetNearestNeighbours(const Geometry &geometry, size_t number_neighbours = 1) const {
125  std::vector<TreeElement> query_result;
126  _rtree.query(
127  boost::geometry::index::nearest(geometry, static_cast<unsigned int>(number_neighbours)),
128  std::back_inserter(query_result));
129  return query_result;
130  }
131 
132  /// Returns segments that intersec the specified geometry
133  /// Warning: intersection between 3D segments is not implemented by boost
134  template<typename Geometry>
135  std::vector<TreeElement> GetIntersections(const Geometry &geometry) const {
136  std::vector<TreeElement> query_result;
137  _rtree.query(
138  boost::geometry::index::intersects(geometry),
139  std::back_inserter(query_result));
140  return query_result;
141  }
142 
143  size_t GetTreeSize() const {
144  return _rtree.size();
145  }
146 
147  private:
148 
149  boost::geometry::index::rtree<TreeElement, boost::geometry::index::linear<16>> _rtree;
150 
151  };
152 
153 } // namespace geom
154 } // namespace carla
std::vector< TreeElement > GetNearestNeighbours(const BPoint &point, size_t number_neighbours=1) const
Definition: Rtree.h:65
boost::geometry::index::rtree< TreeElement, boost::geometry::index::linear< 16 > > _rtree
Definition: Rtree.h:78
std::vector< TreeElement > GetNearestNeighbours(const Geometry &geometry, size_t number_neighbours=1) const
Definition: Rtree.h:124
std::pair< BPoint, T > TreeElement
Definition: Rtree.h:34
size_t GetTreeSize() const
Definition: Rtree.h:72
void InsertElements(const std::vector< TreeElement > &elements)
Definition: Rtree.h:101
std::vector< TreeElement > GetIntersections(const Geometry &geometry) const
Returns segments that intersec the specified geometry Warning: intersection between 3D segments is no...
Definition: Rtree.h:135
boost::geometry::model::point< float, Dimension, boost::geometry::cs::cartesian > BPoint
Definition: Rtree.h:33
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
void InsertElement(const BPoint &point, const T &element)
Definition: Rtree.h:36
void InsertElement(const TreeElement &element)
Definition: Rtree.h:40
std::vector< TreeElement > GetNearestNeighboursWithFilter(const BPoint &point, Filter filter, size_t number_neighbours=1) const
Return nearest neighbors with a user defined filter.
Definition: Rtree.h:54
Rtree class working with 3D segment clouds.
Definition: Rtree.h:86
boost::geometry::model::point< float, Dimension, boost::geometry::cs::cartesian > BPoint
Definition: Rtree.h:89
void InsertElements(const std::vector< TreeElement > &elements)
Definition: Rtree.h:44
boost::geometry::index::rtree< TreeElement, boost::geometry::index::linear< 16 > > _rtree
Definition: Rtree.h:149
void InsertElement(const TreeElement &element)
Definition: Rtree.h:97
void InsertElement(const BSegment &segment, const T &element_start, const T &element_end)
Definition: Rtree.h:93
boost::geometry::model::segment< BPoint > BSegment
Definition: Rtree.h:90
Rtree class working with 3D point clouds.
Definition: Rtree.h:30
size_t GetTreeSize() const
Definition: Rtree.h:143
std::vector< TreeElement > GetNearestNeighboursWithFilter(const Geometry &geometry, Filter filter, size_t number_neighbours=1) const
Return nearest neighbors with a user defined filter.
Definition: Rtree.h:111
std::pair< BSegment, std::pair< T, T > > TreeElement
Definition: Rtree.h:91