KdTree2d.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * KdTree2d.h
  3. * RVO2 Library
  4. *
  5. * Copyright 2008 University of North Carolina at Chapel Hill
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * Please send all bug reports to <geom@cs.unc.edu>.
  20. *
  21. * The authors may be contacted via:
  22. *
  23. * Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha
  24. * Dept. of Computer Science
  25. * 201 S. Columbia St.
  26. * Frederick P. Brooks, Jr. Computer Science Bldg.
  27. * Chapel Hill, N.C. 27599-3175
  28. * United States of America
  29. *
  30. * <http://gamma.cs.unc.edu/RVO2/>
  31. */
  32. #ifndef RVO2D_KD_TREE_H_
  33. #define RVO2D_KD_TREE_H_
  34. /**
  35. * \file KdTree2d.h
  36. * \brief Contains the KdTree class.
  37. */
  38. #include "Definitions.h"
  39. namespace RVO2D {
  40. /**
  41. * \brief Defines <i>k</i>d-trees for agents and static obstacles in the
  42. * simulation.
  43. */
  44. class KdTree2D {
  45. public:
  46. /**
  47. * \brief Defines an agent <i>k</i>d-tree node.
  48. */
  49. class AgentTreeNode {
  50. public:
  51. /**
  52. * \brief The beginning node number.
  53. */
  54. size_t begin;
  55. /**
  56. * \brief The ending node number.
  57. */
  58. size_t end;
  59. /**
  60. * \brief The left node number.
  61. */
  62. size_t left;
  63. /**
  64. * \brief The maximum x-coordinate.
  65. */
  66. float maxX;
  67. /**
  68. * \brief The maximum y-coordinate.
  69. */
  70. float maxY;
  71. /**
  72. * \brief The minimum x-coordinate.
  73. */
  74. float minX;
  75. /**
  76. * \brief The minimum y-coordinate.
  77. */
  78. float minY;
  79. /**
  80. * \brief The right node number.
  81. */
  82. size_t right;
  83. };
  84. /**
  85. * \brief Defines an obstacle <i>k</i>d-tree node.
  86. */
  87. class ObstacleTreeNode {
  88. public:
  89. /**
  90. * \brief The left obstacle tree node.
  91. */
  92. ObstacleTreeNode *left;
  93. /**
  94. * \brief The obstacle number.
  95. */
  96. const Obstacle2D *obstacle;
  97. /**
  98. * \brief The right obstacle tree node.
  99. */
  100. ObstacleTreeNode *right;
  101. };
  102. /**
  103. * \brief Constructs a <i>k</i>d-tree instance.
  104. * \param sim The simulator instance.
  105. */
  106. explicit KdTree2D(RVOSimulator2D *sim);
  107. /**
  108. * \brief Destroys this kd-tree instance.
  109. */
  110. ~KdTree2D();
  111. /**
  112. * \brief Builds an agent <i>k</i>d-tree.
  113. */
  114. void buildAgentTree(std::vector<Agent2D *> agents);
  115. void buildAgentTreeRecursive(size_t begin, size_t end, size_t node);
  116. /**
  117. * \brief Builds an obstacle <i>k</i>d-tree.
  118. */
  119. void buildObstacleTree(std::vector<Obstacle2D *> obstacles);
  120. ObstacleTreeNode *buildObstacleTreeRecursive(const std::vector<Obstacle2D *> &
  121. obstacles);
  122. /**
  123. * \brief Computes the agent neighbors of the specified agent.
  124. * \param agent A pointer to the agent for which agent
  125. * neighbors are to be computed.
  126. * \param rangeSq The squared range around the agent.
  127. */
  128. void computeAgentNeighbors(Agent2D *agent, float &rangeSq) const;
  129. /**
  130. * \brief Computes the obstacle neighbors of the specified agent.
  131. * \param agent A pointer to the agent for which obstacle
  132. * neighbors are to be computed.
  133. * \param rangeSq The squared range around the agent.
  134. */
  135. void computeObstacleNeighbors(Agent2D *agent, float rangeSq) const;
  136. /**
  137. * \brief Deletes the specified obstacle tree node.
  138. * \param node A pointer to the obstacle tree node to be
  139. * deleted.
  140. */
  141. void deleteObstacleTree(ObstacleTreeNode *node);
  142. void queryAgentTreeRecursive(Agent2D *agent, float &rangeSq,
  143. size_t node) const;
  144. void queryObstacleTreeRecursive(Agent2D *agent, float rangeSq,
  145. const ObstacleTreeNode *node) const;
  146. /**
  147. * \brief Queries the visibility between two points within a
  148. * specified radius.
  149. * \param q1 The first point between which visibility is
  150. * to be tested.
  151. * \param q2 The second point between which visibility is
  152. * to be tested.
  153. * \param radius The radius within which visibility is to be
  154. * tested.
  155. * \return True if q1 and q2 are mutually visible within the radius;
  156. * false otherwise.
  157. */
  158. bool queryVisibility(const Vector2 &q1, const Vector2 &q2,
  159. float radius) const;
  160. bool queryVisibilityRecursive(const Vector2 &q1, const Vector2 &q2,
  161. float radius,
  162. const ObstacleTreeNode *node) const;
  163. std::vector<Agent2D *> agents_;
  164. std::vector<AgentTreeNode> agentTree_;
  165. ObstacleTreeNode *obstacleTree_;
  166. RVOSimulator2D *sim_;
  167. static const size_t MAX_LEAF_SIZE = 10;
  168. friend class Agent2D;
  169. friend class RVOSimulator2D;
  170. };
  171. }
  172. #endif /* RVO2D_KD_TREE_H_ */