RVOSimulator2d.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * RVOSimulator2d.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_RVO_SIMULATOR_H_
  33. #define RVO2D_RVO_SIMULATOR_H_
  34. /**
  35. * \file RVOSimulator2d.h
  36. * \brief Contains the RVOSimulator2D class.
  37. */
  38. #include <cstddef>
  39. #include <limits>
  40. #include <vector>
  41. #include "Vector2.h"
  42. namespace RVO2D {
  43. /**
  44. * \brief Error value.
  45. *
  46. * A value equal to the largest unsigned integer that is returned in case
  47. * of an error by functions in RVO2D::RVOSimulator2D.
  48. */
  49. const size_t RVO2D_ERROR = std::numeric_limits<size_t>::max();
  50. /**
  51. * \brief Defines a directed line.
  52. */
  53. class Line {
  54. public:
  55. /**
  56. * \brief A point on the directed line.
  57. */
  58. Vector2 point;
  59. /**
  60. * \brief The direction of the directed line.
  61. */
  62. Vector2 direction;
  63. };
  64. class Agent2D;
  65. class KdTree2D;
  66. class Obstacle2D;
  67. /**
  68. * \brief Defines the simulation.
  69. *
  70. * The main class of the library that contains all simulation functionality.
  71. */
  72. class RVOSimulator2D {
  73. public:
  74. /**
  75. * \brief Constructs a simulator instance.
  76. */
  77. RVOSimulator2D();
  78. /**
  79. * \brief Constructs a simulator instance and sets the default
  80. * properties for any new agent that is added.
  81. * \param timeStep The time step of the simulation.
  82. * Must be positive.
  83. * \param neighborDist The default maximum distance (center point
  84. * to center point) to other agents a new agent
  85. * takes into account in the navigation. The
  86. * larger this number, the longer he running
  87. * time of the simulation. If the number is too
  88. * low, the simulation will not be safe. Must be
  89. * non-negative.
  90. * \param maxNeighbors The default maximum number of other agents a
  91. * new agent takes into account in the
  92. * navigation. The larger this number, the
  93. * longer the running time of the simulation.
  94. * If the number is too low, the simulation
  95. * will not be safe.
  96. * \param timeHorizon The default minimal amount of time for which
  97. * a new agent's velocities that are computed
  98. * by the simulation are safe with respect to
  99. * other agents. The larger this number, the
  100. * sooner an agent will respond to the presence
  101. * of other agents, but the less freedom the
  102. * agent has in choosing its velocities.
  103. * Must be positive.
  104. * \param timeHorizonObst The default minimal amount of time for which
  105. * a new agent's velocities that are computed
  106. * by the simulation are safe with respect to
  107. * obstacles. The larger this number, the
  108. * sooner an agent will respond to the presence
  109. * of obstacles, but the less freedom the agent
  110. * has in choosing its velocities.
  111. * Must be positive.
  112. * \param radius The default radius of a new agent.
  113. * Must be non-negative.
  114. * \param maxSpeed The default maximum speed of a new agent.
  115. * Must be non-negative.
  116. * \param velocity The default initial two-dimensional linear
  117. * velocity of a new agent (optional).
  118. */
  119. RVOSimulator2D(float timeStep, float neighborDist, size_t maxNeighbors,
  120. float timeHorizon, float timeHorizonObst, float radius,
  121. float maxSpeed, const Vector2 &velocity = Vector2());
  122. /**
  123. * \brief Destroys this simulator instance.
  124. */
  125. ~RVOSimulator2D();
  126. /**
  127. * \brief Adds a new agent with default properties to the
  128. * simulation.
  129. * \param position The two-dimensional starting position of
  130. * this agent.
  131. * \return The number of the agent, or RVO2D::RVO2D_ERROR when the agent
  132. * defaults have not been set.
  133. */
  134. size_t addAgent(const Vector2 &position);
  135. /**
  136. * \brief Adds a new agent to the simulation.
  137. * \param position The two-dimensional starting position of
  138. * this agent.
  139. * \param neighborDist The maximum distance (center point to
  140. * center point) to other agents this agent
  141. * takes into account in the navigation. The
  142. * larger this number, the longer the running
  143. * time of the simulation. If the number is too
  144. * low, the simulation will not be safe.
  145. * Must be non-negative.
  146. * \param maxNeighbors The maximum number of other agents this
  147. * agent takes into account in the navigation.
  148. * The larger this number, the longer the
  149. * running time of the simulation. If the
  150. * number is too low, the simulation will not
  151. * be safe.
  152. * \param timeHorizon The minimal amount of time for which this
  153. * agent's velocities that are computed by the
  154. * simulation are safe with respect to other
  155. * agents. The larger this number, the sooner
  156. * this agent will respond to the presence of
  157. * other agents, but the less freedom this
  158. * agent has in choosing its velocities.
  159. * Must be positive.
  160. * \param timeHorizonObst The minimal amount of time for which this
  161. * agent's velocities that are computed by the
  162. * simulation are safe with respect to
  163. * obstacles. The larger this number, the
  164. * sooner this agent will respond to the
  165. * presence of obstacles, but the less freedom
  166. * this agent has in choosing its velocities.
  167. * Must be positive.
  168. * \param radius The radius of this agent.
  169. * Must be non-negative.
  170. * \param maxSpeed The maximum speed of this agent.
  171. * Must be non-negative.
  172. * \param velocity The initial two-dimensional linear velocity
  173. * of this agent (optional).
  174. * \return The number of the agent.
  175. */
  176. size_t addAgent(const Vector2 &position, float neighborDist,
  177. size_t maxNeighbors, float timeHorizon,
  178. float timeHorizonObst, float radius, float maxSpeed,
  179. const Vector2 &velocity = Vector2());
  180. /**
  181. * \brief Adds a new obstacle to the simulation.
  182. * \param vertices List of the vertices of the polygonal
  183. * obstacle in counterclockwise order.
  184. * \return The number of the first vertex of the obstacle,
  185. * or RVO2D::RVO2D_ERROR when the number of vertices is less than two.
  186. * \note To add a "negative" obstacle, e.g. a bounding polygon around
  187. * the environment, the vertices should be listed in clockwise
  188. * order.
  189. */
  190. size_t addObstacle(const std::vector<Vector2> &vertices);
  191. /**
  192. * \brief Lets the simulator perform a simulation step and updates the
  193. * two-dimensional position and two-dimensional velocity of
  194. * each agent.
  195. */
  196. void doStep();
  197. /**
  198. * \brief Returns the specified agent neighbor of the specified
  199. * agent.
  200. * \param agentNo The number of the agent whose agent
  201. * neighbor is to be retrieved.
  202. * \param neighborNo The number of the agent neighbor to be
  203. * retrieved.
  204. * \return The number of the neighboring agent.
  205. */
  206. size_t getAgentAgentNeighbor(size_t agentNo, size_t neighborNo) const;
  207. /**
  208. * \brief Returns the maximum neighbor count of a specified agent.
  209. * \param agentNo The number of the agent whose maximum
  210. * neighbor count is to be retrieved.
  211. * \return The present maximum neighbor count of the agent.
  212. */
  213. size_t getAgentMaxNeighbors(size_t agentNo) const;
  214. /**
  215. * \brief Returns the maximum speed of a specified agent.
  216. * \param agentNo The number of the agent whose maximum speed
  217. * is to be retrieved.
  218. * \return The present maximum speed of the agent.
  219. */
  220. float getAgentMaxSpeed(size_t agentNo) const;
  221. /**
  222. * \brief Returns the maximum neighbor distance of a specified
  223. * agent.
  224. * \param agentNo The number of the agent whose maximum
  225. * neighbor distance is to be retrieved.
  226. * \return The present maximum neighbor distance of the agent.
  227. */
  228. float getAgentNeighborDist(size_t agentNo) const;
  229. /**
  230. * \brief Returns the count of agent neighbors taken into account to
  231. * compute the current velocity for the specified agent.
  232. * \param agentNo The number of the agent whose count of agent
  233. * neighbors is to be retrieved.
  234. * \return The count of agent neighbors taken into account to compute
  235. * the current velocity for the specified agent.
  236. */
  237. size_t getAgentNumAgentNeighbors(size_t agentNo) const;
  238. /**
  239. * \brief Returns the count of obstacle neighbors taken into account
  240. * to compute the current velocity for the specified agent.
  241. * \param agentNo The number of the agent whose count of
  242. * obstacle neighbors is to be retrieved.
  243. * \return The count of obstacle neighbors taken into account to
  244. * compute the current velocity for the specified agent.
  245. */
  246. size_t getAgentNumObstacleNeighbors(size_t agentNo) const;
  247. /**
  248. * \brief Returns the count of ORCA constraints used to compute
  249. * the current velocity for the specified agent.
  250. * \param agentNo The number of the agent whose count of ORCA
  251. * constraints is to be retrieved.
  252. * \return The count of ORCA constraints used to compute the current
  253. * velocity for the specified agent.
  254. */
  255. size_t getAgentNumORCALines(size_t agentNo) const;
  256. /**
  257. * \brief Returns the specified obstacle neighbor of the specified
  258. * agent.
  259. * \param agentNo The number of the agent whose obstacle
  260. * neighbor is to be retrieved.
  261. * \param neighborNo The number of the obstacle neighbor to be
  262. * retrieved.
  263. * \return The number of the first vertex of the neighboring obstacle
  264. * edge.
  265. */
  266. size_t getAgentObstacleNeighbor(size_t agentNo, size_t neighborNo) const;
  267. /**
  268. * \brief Returns the specified ORCA constraint of the specified
  269. * agent.
  270. * \param agentNo The number of the agent whose ORCA
  271. * constraint is to be retrieved.
  272. * \param lineNo The number of the ORCA constraint to be
  273. * retrieved.
  274. * \return A line representing the specified ORCA constraint.
  275. * \note The halfplane to the left of the line is the region of
  276. * permissible velocities with respect to the specified
  277. * ORCA constraint.
  278. */
  279. const Line &getAgentORCALine(size_t agentNo, size_t lineNo) const;
  280. /**
  281. * \brief Returns the two-dimensional position of a specified
  282. * agent.
  283. * \param agentNo The number of the agent whose
  284. * two-dimensional position is to be retrieved.
  285. * \return The present two-dimensional position of the (center of the)
  286. * agent.
  287. */
  288. const Vector2 &getAgentPosition(size_t agentNo) const;
  289. /**
  290. * \brief Returns the two-dimensional preferred velocity of a
  291. * specified agent.
  292. * \param agentNo The number of the agent whose
  293. * two-dimensional preferred velocity is to be
  294. * retrieved.
  295. * \return The present two-dimensional preferred velocity of the agent.
  296. */
  297. const Vector2 &getAgentPrefVelocity(size_t agentNo) const;
  298. /**
  299. * \brief Returns the radius of a specified agent.
  300. * \param agentNo The number of the agent whose radius is to
  301. * be retrieved.
  302. * \return The present radius of the agent.
  303. */
  304. float getAgentRadius(size_t agentNo) const;
  305. /**
  306. * \brief Returns the time horizon of a specified agent.
  307. * \param agentNo The number of the agent whose time horizon
  308. * is to be retrieved.
  309. * \return The present time horizon of the agent.
  310. */
  311. float getAgentTimeHorizon(size_t agentNo) const;
  312. /**
  313. * \brief Returns the time horizon with respect to obstacles of a
  314. * specified agent.
  315. * \param agentNo The number of the agent whose time horizon
  316. * with respect to obstacles is to be
  317. * retrieved.
  318. * \return The present time horizon with respect to obstacles of the
  319. * agent.
  320. */
  321. float getAgentTimeHorizonObst(size_t agentNo) const;
  322. /**
  323. * \brief Returns the two-dimensional linear velocity of a
  324. * specified agent.
  325. * \param agentNo The number of the agent whose
  326. * two-dimensional linear velocity is to be
  327. * retrieved.
  328. * \return The present two-dimensional linear velocity of the agent.
  329. */
  330. const Vector2 &getAgentVelocity(size_t agentNo) const;
  331. /**
  332. * \brief Returns the global time of the simulation.
  333. * \return The present global time of the simulation (zero initially).
  334. */
  335. float getGlobalTime() const;
  336. /**
  337. * \brief Returns the count of agents in the simulation.
  338. * \return The count of agents in the simulation.
  339. */
  340. size_t getNumAgents() const;
  341. /**
  342. * \brief Returns the count of obstacle vertices in the simulation.
  343. * \return The count of obstacle vertices in the simulation.
  344. */
  345. size_t getNumObstacleVertices() const;
  346. /**
  347. * \brief Returns the two-dimensional position of a specified obstacle
  348. * vertex.
  349. * \param vertexNo The number of the obstacle vertex to be
  350. * retrieved.
  351. * \return The two-dimensional position of the specified obstacle
  352. * vertex.
  353. */
  354. const Vector2 &getObstacleVertex(size_t vertexNo) const;
  355. /**
  356. * \brief Returns the number of the obstacle vertex succeeding the
  357. * specified obstacle vertex in its polygon.
  358. * \param vertexNo The number of the obstacle vertex whose
  359. * successor is to be retrieved.
  360. * \return The number of the obstacle vertex succeeding the specified
  361. * obstacle vertex in its polygon.
  362. */
  363. size_t getNextObstacleVertexNo(size_t vertexNo) const;
  364. /**
  365. * \brief Returns the number of the obstacle vertex preceding the
  366. * specified obstacle vertex in its polygon.
  367. * \param vertexNo The number of the obstacle vertex whose
  368. * predecessor is to be retrieved.
  369. * \return The number of the obstacle vertex preceding the specified
  370. * obstacle vertex in its polygon.
  371. */
  372. size_t getPrevObstacleVertexNo(size_t vertexNo) const;
  373. /**
  374. * \brief Returns the time step of the simulation.
  375. * \return The present time step of the simulation.
  376. */
  377. float getTimeStep() const;
  378. /**
  379. * \brief Processes the obstacles that have been added so that they
  380. * are accounted for in the simulation.
  381. * \note Obstacles added to the simulation after this function has
  382. * been called are not accounted for in the simulation.
  383. */
  384. void processObstacles();
  385. /**
  386. * \brief Performs a visibility query between the two specified
  387. * points with respect to the obstacles
  388. * \param point1 The first point of the query.
  389. * \param point2 The second point of the query.
  390. * \param radius The minimal distance between the line
  391. * connecting the two points and the obstacles
  392. * in order for the points to be mutually
  393. * visible (optional). Must be non-negative.
  394. * \return A boolean specifying whether the two points are mutually
  395. * visible. Returns true when the obstacles have not been
  396. * processed.
  397. */
  398. bool queryVisibility(const Vector2 &point1, const Vector2 &point2,
  399. float radius = 0.0f) const;
  400. /**
  401. * \brief Sets the default properties for any new agent that is
  402. * added.
  403. * \param neighborDist The default maximum distance (center point
  404. * to center point) to other agents a new agent
  405. * takes into account in the navigation. The
  406. * larger this number, the longer he running
  407. * time of the simulation. If the number is too
  408. * low, the simulation will not be safe.
  409. * Must be non-negative.
  410. * \param maxNeighbors The default maximum number of other agents a
  411. * new agent takes into account in the
  412. * navigation. The larger this number, the
  413. * longer the running time of the simulation.
  414. * If the number is too low, the simulation
  415. * will not be safe.
  416. * \param timeHorizon The default minimal amount of time for which
  417. * a new agent's velocities that are computed
  418. * by the simulation are safe with respect to
  419. * other agents. The larger this number, the
  420. * sooner an agent will respond to the presence
  421. * of other agents, but the less freedom the
  422. * agent has in choosing its velocities.
  423. * Must be positive.
  424. * \param timeHorizonObst The default minimal amount of time for which
  425. * a new agent's velocities that are computed
  426. * by the simulation are safe with respect to
  427. * obstacles. The larger this number, the
  428. * sooner an agent will respond to the presence
  429. * of obstacles, but the less freedom the agent
  430. * has in choosing its velocities.
  431. * Must be positive.
  432. * \param radius The default radius of a new agent.
  433. * Must be non-negative.
  434. * \param maxSpeed The default maximum speed of a new agent.
  435. * Must be non-negative.
  436. * \param velocity The default initial two-dimensional linear
  437. * velocity of a new agent (optional).
  438. */
  439. void setAgentDefaults(float neighborDist, size_t maxNeighbors,
  440. float timeHorizon, float timeHorizonObst,
  441. float radius, float maxSpeed,
  442. const Vector2 &velocity = Vector2());
  443. /**
  444. * \brief Sets the maximum neighbor count of a specified agent.
  445. * \param agentNo The number of the agent whose maximum
  446. * neighbor count is to be modified.
  447. * \param maxNeighbors The replacement maximum neighbor count.
  448. */
  449. void setAgentMaxNeighbors(size_t agentNo, size_t maxNeighbors);
  450. /**
  451. * \brief Sets the maximum speed of a specified agent.
  452. * \param agentNo The number of the agent whose maximum speed
  453. * is to be modified.
  454. * \param maxSpeed The replacement maximum speed. Must be
  455. * non-negative.
  456. */
  457. void setAgentMaxSpeed(size_t agentNo, float maxSpeed);
  458. /**
  459. * \brief Sets the maximum neighbor distance of a specified agent.
  460. * \param agentNo The number of the agent whose maximum
  461. * neighbor distance is to be modified.
  462. * \param neighborDist The replacement maximum neighbor distance.
  463. * Must be non-negative.
  464. */
  465. void setAgentNeighborDist(size_t agentNo, float neighborDist);
  466. /**
  467. * \brief Sets the two-dimensional position of a specified agent.
  468. * \param agentNo The number of the agent whose
  469. * two-dimensional position is to be modified.
  470. * \param position The replacement of the two-dimensional
  471. * position.
  472. */
  473. void setAgentPosition(size_t agentNo, const Vector2 &position);
  474. /**
  475. * \brief Sets the two-dimensional preferred velocity of a
  476. * specified agent.
  477. * \param agentNo The number of the agent whose
  478. * two-dimensional preferred velocity is to be
  479. * modified.
  480. * \param prefVelocity The replacement of the two-dimensional
  481. * preferred velocity.
  482. */
  483. void setAgentPrefVelocity(size_t agentNo, const Vector2 &prefVelocity);
  484. /**
  485. * \brief Sets the radius of a specified agent.
  486. * \param agentNo The number of the agent whose radius is to
  487. * be modified.
  488. * \param radius The replacement radius.
  489. * Must be non-negative.
  490. */
  491. void setAgentRadius(size_t agentNo, float radius);
  492. /**
  493. * \brief Sets the time horizon of a specified agent with respect
  494. * to other agents.
  495. * \param agentNo The number of the agent whose time horizon
  496. * is to be modified.
  497. * \param timeHorizon The replacement time horizon with respect
  498. * to other agents. Must be positive.
  499. */
  500. void setAgentTimeHorizon(size_t agentNo, float timeHorizon);
  501. /**
  502. * \brief Sets the time horizon of a specified agent with respect
  503. * to obstacles.
  504. * \param agentNo The number of the agent whose time horizon
  505. * with respect to obstacles is to be modified.
  506. * \param timeHorizonObst The replacement time horizon with respect to
  507. * obstacles. Must be positive.
  508. */
  509. void setAgentTimeHorizonObst(size_t agentNo, float timeHorizonObst);
  510. /**
  511. * \brief Sets the two-dimensional linear velocity of a specified
  512. * agent.
  513. * \param agentNo The number of the agent whose
  514. * two-dimensional linear velocity is to be
  515. * modified.
  516. * \param velocity The replacement two-dimensional linear
  517. * velocity.
  518. */
  519. void setAgentVelocity(size_t agentNo, const Vector2 &velocity);
  520. /**
  521. * \brief Sets the time step of the simulation.
  522. * \param timeStep The time step of the simulation.
  523. * Must be positive.
  524. */
  525. void setTimeStep(float timeStep);
  526. public:
  527. std::vector<Agent2D *> agents_;
  528. Agent2D *defaultAgent_;
  529. float globalTime_;
  530. KdTree2D *kdTree_;
  531. std::vector<Obstacle2D *> obstacles_;
  532. float timeStep_;
  533. friend class Agent2D;
  534. friend class KdTree2D;
  535. friend class Obstacle2D;
  536. };
  537. }
  538. #endif /* RVO2D_RVO_SIMULATOR_H_ */