aaboxkdtree2d.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #pragma once
  2. #include "math/aabox2d.h"
  3. #include "math/math_utils.h"
  4. #include <algorithm>
  5. #include <limits>
  6. #include <memory>
  7. #include <vector>
  8. namespace decision::math {
  9. struct AABoxKDTreeParams {
  10. // The maximum depth of the kdtree.
  11. int max_depth = -1;
  12. // The maximum number of items in one leaf node.
  13. int max_leaf_size = -1;
  14. // The maximum dimension size of leaf node.
  15. double max_leaf_dimension = -1.0;
  16. };
  17. template <class ObjectType>
  18. class AABoxKDTree2dNode {
  19. public:
  20. using ObjectPtr = const ObjectType*;
  21. AABoxKDTree2dNode(const std::vector<ObjectPtr>& objects,
  22. const AABoxKDTreeParams& params, int depth)
  23. : _depth(depth)
  24. {
  25. CHECK(!objects.empty());
  26. compute_boundary(objects);
  27. compute_partition();
  28. if (split_to_subnodes(objects, params)) {
  29. std::vector<ObjectPtr> left_subnode_objects;
  30. std::vector<ObjectPtr> right_subnode_objects;
  31. partition_objects(objects, &left_subnode_objects, &right_subnode_objects);
  32. // Split to sub-nodes.
  33. if (!left_subnode_objects.empty()) {
  34. _left_subnode.reset(new AABoxKDTree2dNode<ObjectType>(
  35. left_subnode_objects, params, depth + 1));
  36. }
  37. if (!right_subnode_objects.empty()) {
  38. _right_subnode.reset(new AABoxKDTree2dNode<ObjectType>(
  39. right_subnode_objects, params, depth + 1));
  40. }
  41. } else {
  42. init_objects(objects);
  43. }
  44. }
  45. ObjectPtr get_nearest_object(const Vec2d& point) const
  46. {
  47. ObjectPtr nearest_object = nullptr;
  48. double min_distance_sqr = std::numeric_limits<double>::infinity();
  49. get_nearest_object_internal(point, &min_distance_sqr, &nearest_object);
  50. return nearest_object;
  51. }
  52. std::vector<ObjectPtr> get_objects(const Vec2d& point, const double distance) const
  53. {
  54. std::vector<ObjectPtr> result_objects;
  55. get_objects_internal(point, distance, sqr(distance), &result_objects);
  56. return result_objects;
  57. }
  58. AABox2d get_bounding_box() const
  59. {
  60. return AABox2d({ _min_x, _min_y }, { _max_x, _max_y });
  61. }
  62. private:
  63. void init_objects(const std::vector<ObjectPtr>& objects)
  64. {
  65. _num_objects = objects.size();
  66. _objects_sorted_by_min = objects;
  67. _objects_sorted_by_max = objects;
  68. std::sort(_objects_sorted_by_min.begin(), _objects_sorted_by_min.end(),
  69. [&](ObjectPtr obj1, ObjectPtr obj2) {
  70. return _partition == PARTITION_X ? obj1->aabox().min_x() < obj2->aabox().min_x()
  71. : obj1->aabox().min_y() < obj2->aabox().min_y();
  72. });
  73. std::sort(_objects_sorted_by_max.begin(), _objects_sorted_by_max.end(),
  74. [&](ObjectPtr obj1, ObjectPtr obj2) {
  75. return _partition == PARTITION_X ? obj1->aabox().max_x() > obj2->aabox().max_x()
  76. : obj1->aabox().max_y() > obj2->aabox().max_y();
  77. });
  78. _objects_sorted_by_min_bound.reserve(_num_objects);
  79. for (ObjectPtr object : _objects_sorted_by_min) {
  80. _objects_sorted_by_min_bound.push_back(
  81. _partition == PARTITION_X ? object->aabox().min_x() : object->aabox().min_y());
  82. }
  83. _objects_sorted_by_max_bound.reserve(_num_objects);
  84. for (ObjectPtr object : _objects_sorted_by_max) {
  85. _objects_sorted_by_max_bound.push_back(
  86. _partition == PARTITION_X ? object->aabox().max_x() : object->aabox().max_y());
  87. }
  88. }
  89. bool split_to_subnodes(const std::vector<ObjectPtr>& objects, const AABoxKDTreeParams& params)
  90. {
  91. if (params.max_depth >= 0 && _depth >= params.max_depth) {
  92. return false;
  93. }
  94. if (static_cast<int>(objects.size()) <= std::max(1, params.max_leaf_size)) {
  95. return false;
  96. }
  97. if (params.max_leaf_dimension >= 0.0 && std::max(_max_x - _min_x, _max_y - _min_y) <= params.max_leaf_dimension) {
  98. return false;
  99. }
  100. return true;
  101. }
  102. double lowerbound_distance_sqr_to_point(const Vec2d& point) const
  103. {
  104. double dx = 0.0;
  105. if (point.x() < _min_x) {
  106. dx = _min_x - point.x();
  107. } else if (point.x() > _max_x) {
  108. dx = point.x() - _max_x;
  109. }
  110. double dy = 0.0;
  111. if (point.y() < _min_y) {
  112. dy = _min_y - point.y();
  113. } else if (point.y() > _max_y) {
  114. dy = point.y() - _max_y;
  115. }
  116. return dx * dx + dy * dy;
  117. }
  118. double upperbound_distance_sqr_to_point(const Vec2d& point) const
  119. {
  120. const double dx = (point.x() > _mid_x ? (point.x() - _min_x) : (point.x() - _max_x));
  121. const double dy = (point.y() > _mid_y ? (point.y() - _min_y) : (point.y() - _max_y));
  122. return dx * dx + dy * dy;
  123. }
  124. void get_all_objects(std::vector<ObjectPtr>* const result_objects) const
  125. {
  126. result_objects->insert(result_objects->end(),
  127. _objects_sorted_by_min.begin(), _objects_sorted_by_min.end());
  128. if (_left_subnode != nullptr) {
  129. _left_subnode->get_all_objects(result_objects);
  130. }
  131. if (_right_subnode != nullptr) {
  132. _right_subnode->get_all_objects(result_objects);
  133. }
  134. }
  135. void get_objects_internal(const Vec2d& point,
  136. const double distance,
  137. const double distance_sqr,
  138. std::vector<ObjectPtr>* const result_objects) const
  139. {
  140. if (lowerbound_distance_sqr_to_point(point) > distance_sqr) {
  141. return;
  142. }
  143. if (upperbound_distance_sqr_to_point(point) <= distance_sqr) {
  144. get_all_objects(result_objects);
  145. return;
  146. }
  147. const double pvalue = (_partition == PARTITION_X ? point.x() : point.y());
  148. if (pvalue < _partition_position) {
  149. const double limit = pvalue + distance;
  150. for (int i = 0; i < _num_objects; ++i) {
  151. if (_objects_sorted_by_min_bound[i] > limit) {
  152. break;
  153. }
  154. ObjectPtr object = _objects_sorted_by_min[i];
  155. if (object->distance_sqr_to(point) <= distance_sqr) {
  156. result_objects->push_back(object);
  157. }
  158. }
  159. } else {
  160. const double limit = pvalue - distance;
  161. for (int i = 0; i < _num_objects; ++i) {
  162. if (_objects_sorted_by_max_bound[i] < limit) {
  163. break;
  164. }
  165. ObjectPtr object = _objects_sorted_by_max[i];
  166. if (object->distance_sqr_to(point) <= distance_sqr) {
  167. result_objects->push_back(object);
  168. }
  169. }
  170. }
  171. if (_left_subnode != nullptr) {
  172. _left_subnode->get_objects_internal(point, distance, distance_sqr, result_objects);
  173. }
  174. if (_right_subnode != nullptr) {
  175. _right_subnode->get_objects_internal(point, distance, distance_sqr, result_objects);
  176. }
  177. }
  178. void get_nearest_object_internal(const Vec2d& point,
  179. double* const min_distance_sqr,
  180. ObjectPtr* const nearest_object) const
  181. {
  182. if (lowerbound_distance_sqr_to_point(point) >= *min_distance_sqr - kMathEpsilon) {
  183. return;
  184. }
  185. const double pvalue = (_partition == PARTITION_X ? point.x() : point.y());
  186. const bool search_left_first = (pvalue < _partition_position);
  187. if (search_left_first) {
  188. if (_left_subnode != nullptr) {
  189. _left_subnode->get_nearest_object_internal(
  190. point, min_distance_sqr, nearest_object);
  191. }
  192. } else {
  193. if (_right_subnode != nullptr) {
  194. _right_subnode->get_nearest_object_internal(
  195. point, min_distance_sqr, nearest_object);
  196. }
  197. }
  198. if (*min_distance_sqr <= kMathEpsilon) {
  199. return;
  200. }
  201. if (search_left_first) {
  202. for (int i = 0; i < _num_objects; ++i) {
  203. const double bound = _objects_sorted_by_min_bound[i];
  204. if (bound > pvalue && sqr(bound - pvalue) > *min_distance_sqr) {
  205. break;
  206. }
  207. ObjectPtr object = _objects_sorted_by_min[i];
  208. const double distance_sqr = object->distance_sqr_to(point);
  209. if (distance_sqr < *min_distance_sqr) {
  210. *min_distance_sqr = distance_sqr;
  211. *nearest_object = object;
  212. }
  213. }
  214. } else {
  215. for (int i = 0; i < _num_objects; ++i) {
  216. const double bound = _objects_sorted_by_max_bound[i];
  217. if (bound < pvalue && sqr(bound - pvalue) > *min_distance_sqr) {
  218. break;
  219. }
  220. ObjectPtr object = _objects_sorted_by_max[i];
  221. const double distance_sqr = object->distance_sqr_to(point);
  222. if (distance_sqr < *min_distance_sqr) {
  223. *min_distance_sqr = distance_sqr;
  224. *nearest_object = object;
  225. }
  226. }
  227. }
  228. if (*min_distance_sqr <= kMathEpsilon) {
  229. return;
  230. }
  231. if (search_left_first) {
  232. if (_right_subnode != nullptr) {
  233. _right_subnode->get_nearest_object_internal(
  234. point, min_distance_sqr, nearest_object);
  235. }
  236. } else {
  237. if (_left_subnode != nullptr) {
  238. _left_subnode->get_nearest_object_internal(
  239. point, min_distance_sqr, nearest_object);
  240. }
  241. }
  242. }
  243. void compute_boundary(const std::vector<ObjectPtr>& objects)
  244. {
  245. _min_x = std::numeric_limits<double>::infinity();
  246. _min_y = std::numeric_limits<double>::infinity();
  247. _max_x = -std::numeric_limits<double>::infinity();
  248. _max_y = -std::numeric_limits<double>::infinity();
  249. for (ObjectPtr object : objects) {
  250. _min_x = std::min(_min_x, object->aabox().min_x());
  251. _max_x = std::max(_max_x, object->aabox().max_x());
  252. _min_y = std::min(_min_y, object->aabox().min_y());
  253. _max_y = std::max(_max_y, object->aabox().max_y());
  254. }
  255. _mid_x = (_min_x + _max_x) / 2.0;
  256. _mid_y = (_min_y + _max_y) / 2.0;
  257. }
  258. void compute_partition()
  259. {
  260. if (_max_x - _min_x >= _max_y - _min_y) {
  261. _partition = PARTITION_X;
  262. _partition_position = (_min_x + _max_x) / 2.0;
  263. } else {
  264. _partition = PARTITION_Y;
  265. _partition_position = (_min_y + _max_y) / 2.0;
  266. }
  267. }
  268. void partition_objects(const std::vector<ObjectPtr>& objects,
  269. std::vector<ObjectPtr>* const left_subnode_objects,
  270. std::vector<ObjectPtr>* const right_subnode_objects)
  271. {
  272. left_subnode_objects->clear();
  273. right_subnode_objects->clear();
  274. std::vector<ObjectPtr> other_objects;
  275. if (_partition == PARTITION_X) {
  276. for (ObjectPtr object : objects) {
  277. if (object->aabox().max_x() <= _partition_position) {
  278. left_subnode_objects->push_back(object);
  279. } else if (object->aabox().min_x() >= _partition_position) {
  280. right_subnode_objects->push_back(object);
  281. } else {
  282. other_objects.push_back(object);
  283. }
  284. }
  285. } else {
  286. for (ObjectPtr object : objects) {
  287. if (object->aabox().max_y() <= _partition_position) {
  288. left_subnode_objects->push_back(object);
  289. } else if (object->aabox().min_y() >= _partition_position) {
  290. right_subnode_objects->push_back(object);
  291. } else {
  292. other_objects.push_back(object);
  293. }
  294. }
  295. }
  296. init_objects(other_objects);
  297. }
  298. private:
  299. int _num_objects = 0;
  300. std::vector<ObjectPtr> _objects_sorted_by_min;
  301. std::vector<ObjectPtr> _objects_sorted_by_max;
  302. std::vector<double> _objects_sorted_by_min_bound;
  303. std::vector<double> _objects_sorted_by_max_bound;
  304. int _depth = 0;
  305. // Boundary
  306. double _min_x = 0.0;
  307. double _max_x = 0.0;
  308. double _min_y = 0.0;
  309. double _max_y = 0.0;
  310. double _mid_x = 0.0;
  311. double _mid_y = 0.0;
  312. enum Partition {
  313. PARTITION_X = 1,
  314. PARTITION_Y = 2,
  315. };
  316. Partition _partition = PARTITION_X;
  317. double _partition_position = 0.0;
  318. std::unique_ptr<AABoxKDTree2dNode<ObjectType>> _left_subnode = nullptr;
  319. std::unique_ptr<AABoxKDTree2dNode<ObjectType>> _right_subnode = nullptr;
  320. };
  321. template <class ObjectType>
  322. class AABoxKDTree2d {
  323. public:
  324. using ObjectPtr = const ObjectType*;
  325. AABoxKDTree2d(const std::vector<ObjectType>& objects, const AABoxKDTreeParams& params)
  326. {
  327. if (!objects.empty()) {
  328. std::vector<ObjectPtr> object_ptrs;
  329. for (const auto& object : objects) {
  330. object_ptrs.push_back(&object);
  331. }
  332. _root.reset(new AABoxKDTree2dNode<ObjectType>(object_ptrs, params, 0));
  333. }
  334. }
  335. ObjectPtr get_nearest_object(const Vec2d& point) const
  336. {
  337. return _root == nullptr ? nullptr : _root->get_nearest_object(point);
  338. }
  339. std::vector<ObjectPtr> get_objects(const Vec2d& point, const double distance) const
  340. {
  341. if (_root == nullptr) {
  342. return {};
  343. }
  344. return _root->get_objects(point, distance);
  345. }
  346. AABox2d get_bounding_box() const
  347. {
  348. return _root == nullptr ? AABox2d() : _root->get_bounding_box();
  349. }
  350. private:
  351. std::unique_ptr<AABoxKDTree2dNode<ObjectType>> _root = nullptr;
  352. };
  353. }