RVOSimulator2d.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * RVOSimulator2d.cpp
  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. #include "RVOSimulator2d.h"
  33. #include "Agent2d.h"
  34. #include "KdTree2d.h"
  35. #include "Obstacle2d.h"
  36. #ifdef _OPENMP
  37. #include <omp.h>
  38. #endif
  39. namespace RVO2D {
  40. RVOSimulator2D::RVOSimulator2D() : defaultAgent_(NULL), globalTime_(0.0f), kdTree_(NULL), timeStep_(0.0f)
  41. {
  42. kdTree_ = new KdTree2D(this);
  43. }
  44. RVOSimulator2D::RVOSimulator2D(float timeStep, float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity) : defaultAgent_(NULL), globalTime_(0.0f), kdTree_(NULL), timeStep_(timeStep)
  45. {
  46. kdTree_ = new KdTree2D(this);
  47. defaultAgent_ = new Agent2D();
  48. defaultAgent_->maxNeighbors_ = maxNeighbors;
  49. defaultAgent_->maxSpeed_ = maxSpeed;
  50. defaultAgent_->neighborDist_ = neighborDist;
  51. defaultAgent_->radius_ = radius;
  52. defaultAgent_->timeHorizon_ = timeHorizon;
  53. defaultAgent_->timeHorizonObst_ = timeHorizonObst;
  54. defaultAgent_->velocity_ = velocity;
  55. }
  56. RVOSimulator2D::~RVOSimulator2D()
  57. {
  58. if (defaultAgent_ != NULL) {
  59. delete defaultAgent_;
  60. }
  61. for (size_t i = 0; i < agents_.size(); ++i) {
  62. delete agents_[i];
  63. }
  64. for (size_t i = 0; i < obstacles_.size(); ++i) {
  65. delete obstacles_[i];
  66. }
  67. delete kdTree_;
  68. }
  69. size_t RVOSimulator2D::addAgent(const Vector2 &position)
  70. {
  71. if (defaultAgent_ == NULL) {
  72. return RVO2D_ERROR;
  73. }
  74. Agent2D *agent = new Agent2D();
  75. agent->position_ = position;
  76. agent->maxNeighbors_ = defaultAgent_->maxNeighbors_;
  77. agent->maxSpeed_ = defaultAgent_->maxSpeed_;
  78. agent->neighborDist_ = defaultAgent_->neighborDist_;
  79. agent->radius_ = defaultAgent_->radius_;
  80. agent->timeHorizon_ = defaultAgent_->timeHorizon_;
  81. agent->timeHorizonObst_ = defaultAgent_->timeHorizonObst_;
  82. agent->velocity_ = defaultAgent_->velocity_;
  83. agent->id_ = agents_.size();
  84. agents_.push_back(agent);
  85. return agents_.size() - 1;
  86. }
  87. size_t RVOSimulator2D::addAgent(const Vector2 &position, float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity)
  88. {
  89. Agent2D *agent = new Agent2D();
  90. agent->position_ = position;
  91. agent->maxNeighbors_ = maxNeighbors;
  92. agent->maxSpeed_ = maxSpeed;
  93. agent->neighborDist_ = neighborDist;
  94. agent->radius_ = radius;
  95. agent->timeHorizon_ = timeHorizon;
  96. agent->timeHorizonObst_ = timeHorizonObst;
  97. agent->velocity_ = velocity;
  98. agent->id_ = agents_.size();
  99. agents_.push_back(agent);
  100. return agents_.size() - 1;
  101. }
  102. size_t RVOSimulator2D::addObstacle(const std::vector<Vector2> &vertices)
  103. {
  104. if (vertices.size() < 2) {
  105. return RVO2D_ERROR;
  106. }
  107. const size_t obstacleNo = obstacles_.size();
  108. for (size_t i = 0; i < vertices.size(); ++i) {
  109. Obstacle2D *obstacle = new Obstacle2D();
  110. obstacle->point_ = vertices[i];
  111. if (i != 0) {
  112. obstacle->prevObstacle_ = obstacles_.back();
  113. obstacle->prevObstacle_->nextObstacle_ = obstacle;
  114. }
  115. if (i == vertices.size() - 1) {
  116. obstacle->nextObstacle_ = obstacles_[obstacleNo];
  117. obstacle->nextObstacle_->prevObstacle_ = obstacle;
  118. }
  119. obstacle->unitDir_ = normalize(vertices[(i == vertices.size() - 1 ? 0 : i + 1)] - vertices[i]);
  120. if (vertices.size() == 2) {
  121. obstacle->isConvex_ = true;
  122. }
  123. else {
  124. obstacle->isConvex_ = (leftOf(vertices[(i == 0 ? vertices.size() - 1 : i - 1)], vertices[i], vertices[(i == vertices.size() - 1 ? 0 : i + 1)]) >= 0.0f);
  125. }
  126. obstacle->id_ = obstacles_.size();
  127. obstacles_.push_back(obstacle);
  128. }
  129. return obstacleNo;
  130. }
  131. void RVOSimulator2D::doStep()
  132. {
  133. kdTree_->buildAgentTree(agents_);
  134. for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {
  135. agents_[i]->computeNeighbors(this);
  136. agents_[i]->computeNewVelocity(this);
  137. }
  138. for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {
  139. agents_[i]->update(this);
  140. }
  141. globalTime_ += timeStep_;
  142. }
  143. size_t RVOSimulator2D::getAgentAgentNeighbor(size_t agentNo, size_t neighborNo) const
  144. {
  145. return agents_[agentNo]->agentNeighbors_[neighborNo].second->id_;
  146. }
  147. size_t RVOSimulator2D::getAgentMaxNeighbors(size_t agentNo) const
  148. {
  149. return agents_[agentNo]->maxNeighbors_;
  150. }
  151. float RVOSimulator2D::getAgentMaxSpeed(size_t agentNo) const
  152. {
  153. return agents_[agentNo]->maxSpeed_;
  154. }
  155. float RVOSimulator2D::getAgentNeighborDist(size_t agentNo) const
  156. {
  157. return agents_[agentNo]->neighborDist_;
  158. }
  159. size_t RVOSimulator2D::getAgentNumAgentNeighbors(size_t agentNo) const
  160. {
  161. return agents_[agentNo]->agentNeighbors_.size();
  162. }
  163. size_t RVOSimulator2D::getAgentNumObstacleNeighbors(size_t agentNo) const
  164. {
  165. return agents_[agentNo]->obstacleNeighbors_.size();
  166. }
  167. size_t RVOSimulator2D::getAgentNumORCALines(size_t agentNo) const
  168. {
  169. return agents_[agentNo]->orcaLines_.size();
  170. }
  171. size_t RVOSimulator2D::getAgentObstacleNeighbor(size_t agentNo, size_t neighborNo) const
  172. {
  173. return agents_[agentNo]->obstacleNeighbors_[neighborNo].second->id_;
  174. }
  175. const Line &RVOSimulator2D::getAgentORCALine(size_t agentNo, size_t lineNo) const
  176. {
  177. return agents_[agentNo]->orcaLines_[lineNo];
  178. }
  179. const Vector2 &RVOSimulator2D::getAgentPosition(size_t agentNo) const
  180. {
  181. return agents_[agentNo]->position_;
  182. }
  183. const Vector2 &RVOSimulator2D::getAgentPrefVelocity(size_t agentNo) const
  184. {
  185. return agents_[agentNo]->prefVelocity_;
  186. }
  187. float RVOSimulator2D::getAgentRadius(size_t agentNo) const
  188. {
  189. return agents_[agentNo]->radius_;
  190. }
  191. float RVOSimulator2D::getAgentTimeHorizon(size_t agentNo) const
  192. {
  193. return agents_[agentNo]->timeHorizon_;
  194. }
  195. float RVOSimulator2D::getAgentTimeHorizonObst(size_t agentNo) const
  196. {
  197. return agents_[agentNo]->timeHorizonObst_;
  198. }
  199. const Vector2 &RVOSimulator2D::getAgentVelocity(size_t agentNo) const
  200. {
  201. return agents_[agentNo]->velocity_;
  202. }
  203. float RVOSimulator2D::getGlobalTime() const
  204. {
  205. return globalTime_;
  206. }
  207. size_t RVOSimulator2D::getNumAgents() const
  208. {
  209. return agents_.size();
  210. }
  211. size_t RVOSimulator2D::getNumObstacleVertices() const
  212. {
  213. return obstacles_.size();
  214. }
  215. const Vector2 &RVOSimulator2D::getObstacleVertex(size_t vertexNo) const
  216. {
  217. return obstacles_[vertexNo]->point_;
  218. }
  219. size_t RVOSimulator2D::getNextObstacleVertexNo(size_t vertexNo) const
  220. {
  221. return obstacles_[vertexNo]->nextObstacle_->id_;
  222. }
  223. size_t RVOSimulator2D::getPrevObstacleVertexNo(size_t vertexNo) const
  224. {
  225. return obstacles_[vertexNo]->prevObstacle_->id_;
  226. }
  227. float RVOSimulator2D::getTimeStep() const
  228. {
  229. return timeStep_;
  230. }
  231. void RVOSimulator2D::processObstacles()
  232. {
  233. kdTree_->buildObstacleTree(obstacles_);
  234. }
  235. bool RVOSimulator2D::queryVisibility(const Vector2 &point1, const Vector2 &point2, float radius) const
  236. {
  237. return kdTree_->queryVisibility(point1, point2, radius);
  238. }
  239. void RVOSimulator2D::setAgentDefaults(float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity)
  240. {
  241. if (defaultAgent_ == NULL) {
  242. defaultAgent_ = new Agent2D();
  243. }
  244. defaultAgent_->maxNeighbors_ = maxNeighbors;
  245. defaultAgent_->maxSpeed_ = maxSpeed;
  246. defaultAgent_->neighborDist_ = neighborDist;
  247. defaultAgent_->radius_ = radius;
  248. defaultAgent_->timeHorizon_ = timeHorizon;
  249. defaultAgent_->timeHorizonObst_ = timeHorizonObst;
  250. defaultAgent_->velocity_ = velocity;
  251. }
  252. void RVOSimulator2D::setAgentMaxNeighbors(size_t agentNo, size_t maxNeighbors)
  253. {
  254. agents_[agentNo]->maxNeighbors_ = maxNeighbors;
  255. }
  256. void RVOSimulator2D::setAgentMaxSpeed(size_t agentNo, float maxSpeed)
  257. {
  258. agents_[agentNo]->maxSpeed_ = maxSpeed;
  259. }
  260. void RVOSimulator2D::setAgentNeighborDist(size_t agentNo, float neighborDist)
  261. {
  262. agents_[agentNo]->neighborDist_ = neighborDist;
  263. }
  264. void RVOSimulator2D::setAgentPosition(size_t agentNo, const Vector2 &position)
  265. {
  266. agents_[agentNo]->position_ = position;
  267. }
  268. void RVOSimulator2D::setAgentPrefVelocity(size_t agentNo, const Vector2 &prefVelocity)
  269. {
  270. agents_[agentNo]->prefVelocity_ = prefVelocity;
  271. }
  272. void RVOSimulator2D::setAgentRadius(size_t agentNo, float radius)
  273. {
  274. agents_[agentNo]->radius_ = radius;
  275. }
  276. void RVOSimulator2D::setAgentTimeHorizon(size_t agentNo, float timeHorizon)
  277. {
  278. agents_[agentNo]->timeHorizon_ = timeHorizon;
  279. }
  280. void RVOSimulator2D::setAgentTimeHorizonObst(size_t agentNo, float timeHorizonObst)
  281. {
  282. agents_[agentNo]->timeHorizonObst_ = timeHorizonObst;
  283. }
  284. void RVOSimulator2D::setAgentVelocity(size_t agentNo, const Vector2 &velocity)
  285. {
  286. agents_[agentNo]->velocity_ = velocity;
  287. }
  288. void RVOSimulator2D::setTimeStep(float timeStep)
  289. {
  290. timeStep_ = timeStep;
  291. }
  292. }