RVOSimulator3d.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * RVOSimulator.cpp
  3. * RVO2-3D 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 "RVOSimulator3d.h"
  33. #ifdef _OPENMP
  34. #include <omp.h>
  35. #endif
  36. #include "Agent3d.h"
  37. #include "KdTree3d.h"
  38. namespace RVO3D {
  39. RVOSimulator3D::RVOSimulator3D() : defaultAgent_(NULL), kdTree_(NULL), globalTime_(0.0f), timeStep_(0.0f)
  40. {
  41. kdTree_ = new KdTree3D(this);
  42. }
  43. RVOSimulator3D::RVOSimulator3D(float timeStep, float neighborDist, size_t maxNeighbors, float timeHorizon, float radius, float maxSpeed, const Vector3 &velocity) : defaultAgent_(NULL), kdTree_(NULL), globalTime_(0.0f), timeStep_(timeStep)
  44. {
  45. kdTree_ = new KdTree3D(this);
  46. defaultAgent_ = new Agent3D();
  47. defaultAgent_->maxNeighbors_ = maxNeighbors;
  48. defaultAgent_->maxSpeed_ = maxSpeed;
  49. defaultAgent_->neighborDist_ = neighborDist;
  50. defaultAgent_->radius_ = radius;
  51. defaultAgent_->timeHorizon_ = timeHorizon;
  52. defaultAgent_->velocity_ = velocity;
  53. }
  54. RVOSimulator3D::~RVOSimulator3D()
  55. {
  56. if (defaultAgent_ != NULL) {
  57. delete defaultAgent_;
  58. }
  59. for (size_t i = 0; i < agents_.size(); ++i) {
  60. delete agents_[i];
  61. }
  62. if (kdTree_ != NULL) {
  63. delete kdTree_;
  64. }
  65. }
  66. size_t RVOSimulator3D::getAgentNumAgentNeighbors(size_t agentNo) const
  67. {
  68. return agents_[agentNo]->agentNeighbors_.size();
  69. }
  70. size_t RVOSimulator3D::getAgentAgentNeighbor(size_t agentNo, size_t neighborNo) const
  71. {
  72. return agents_[agentNo]->agentNeighbors_[neighborNo].second->id_;
  73. }
  74. size_t RVOSimulator3D::getAgentNumORCAPlanes(size_t agentNo) const
  75. {
  76. return agents_[agentNo]->orcaPlanes_.size();
  77. }
  78. const Plane &RVOSimulator3D::getAgentORCAPlane(size_t agentNo, size_t planeNo) const
  79. {
  80. return agents_[agentNo]->orcaPlanes_[planeNo];
  81. }
  82. void RVOSimulator3D::removeAgent(size_t agentNo)
  83. {
  84. delete agents_[agentNo];
  85. agents_[agentNo] = agents_.back();
  86. agents_.pop_back();
  87. }
  88. size_t RVOSimulator3D::addAgent(const Vector3 &position)
  89. {
  90. if (defaultAgent_ == NULL) {
  91. return RVO3D_ERROR;
  92. }
  93. Agent3D *agent = new Agent3D();
  94. agent->position_ = position;
  95. agent->maxNeighbors_ = defaultAgent_->maxNeighbors_;
  96. agent->maxSpeed_ = defaultAgent_->maxSpeed_;
  97. agent->neighborDist_ = defaultAgent_->neighborDist_;
  98. agent->radius_ = defaultAgent_->radius_;
  99. agent->timeHorizon_ = defaultAgent_->timeHorizon_;
  100. agent->velocity_ = defaultAgent_->velocity_;
  101. agent->id_ = agents_.size();
  102. agents_.push_back(agent);
  103. return agents_.size() - 1;
  104. }
  105. size_t RVOSimulator3D::addAgent(const Vector3 &position, float neighborDist, size_t maxNeighbors, float timeHorizon, float radius, float maxSpeed, const Vector3 &velocity)
  106. {
  107. Agent3D *agent = new Agent3D();
  108. agent->position_ = position;
  109. agent->maxNeighbors_ = maxNeighbors;
  110. agent->maxSpeed_ = maxSpeed;
  111. agent->neighborDist_ = neighborDist;
  112. agent->radius_ = radius;
  113. agent->timeHorizon_ = timeHorizon;
  114. agent->velocity_ = velocity;
  115. agent->id_ = agents_.size();
  116. agents_.push_back(agent);
  117. return agents_.size() - 1;
  118. }
  119. void RVOSimulator3D::doStep()
  120. {
  121. kdTree_->buildAgentTree(agents_);
  122. for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {
  123. agents_[i]->computeNeighbors(this);
  124. agents_[i]->computeNewVelocity(this);
  125. }
  126. for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {
  127. agents_[i]->update(this);
  128. }
  129. globalTime_ += timeStep_;
  130. }
  131. size_t RVOSimulator3D::getAgentMaxNeighbors(size_t agentNo) const
  132. {
  133. return agents_[agentNo]->maxNeighbors_;
  134. }
  135. float RVOSimulator3D::getAgentMaxSpeed(size_t agentNo) const
  136. {
  137. return agents_[agentNo]->maxSpeed_;
  138. }
  139. float RVOSimulator3D::getAgentNeighborDist(size_t agentNo) const
  140. {
  141. return agents_[agentNo]->neighborDist_;
  142. }
  143. const Vector3 &RVOSimulator3D::getAgentPosition(size_t agentNo) const
  144. {
  145. return agents_[agentNo]->position_;
  146. }
  147. const Vector3 &RVOSimulator3D::getAgentPrefVelocity(size_t agentNo) const
  148. {
  149. return agents_[agentNo]->prefVelocity_;
  150. }
  151. float RVOSimulator3D::getAgentRadius(size_t agentNo) const
  152. {
  153. return agents_[agentNo]->radius_;
  154. }
  155. float RVOSimulator3D::getAgentTimeHorizon(size_t agentNo) const
  156. {
  157. return agents_[agentNo]->timeHorizon_;
  158. }
  159. const Vector3 &RVOSimulator3D::getAgentVelocity(size_t agentNo) const
  160. {
  161. return agents_[agentNo]->velocity_;
  162. }
  163. float RVOSimulator3D::getGlobalTime() const
  164. {
  165. return globalTime_;
  166. }
  167. size_t RVOSimulator3D::getNumAgents() const
  168. {
  169. return agents_.size();
  170. }
  171. float RVOSimulator3D::getTimeStep() const
  172. {
  173. return timeStep_;
  174. }
  175. void RVOSimulator3D::setAgentDefaults(float neighborDist, size_t maxNeighbors, float timeHorizon, float radius, float maxSpeed, const Vector3 &velocity)
  176. {
  177. if (defaultAgent_ == NULL) {
  178. defaultAgent_ = new Agent3D();
  179. }
  180. defaultAgent_->maxNeighbors_ = maxNeighbors;
  181. defaultAgent_->maxSpeed_ = maxSpeed;
  182. defaultAgent_->neighborDist_ = neighborDist;
  183. defaultAgent_->radius_ = radius;
  184. defaultAgent_->timeHorizon_ = timeHorizon;
  185. defaultAgent_->velocity_ = velocity;
  186. }
  187. void RVOSimulator3D::setAgentMaxNeighbors(size_t agentNo, size_t maxNeighbors)
  188. {
  189. agents_[agentNo]->maxNeighbors_ = maxNeighbors;
  190. }
  191. void RVOSimulator3D::setAgentMaxSpeed(size_t agentNo, float maxSpeed)
  192. {
  193. agents_[agentNo]->maxSpeed_ = maxSpeed;
  194. }
  195. void RVOSimulator3D::setAgentNeighborDist(size_t agentNo, float neighborDist)
  196. {
  197. agents_[agentNo]->neighborDist_ = neighborDist;
  198. }
  199. void RVOSimulator3D::setAgentPosition(size_t agentNo, const Vector3 &position)
  200. {
  201. agents_[agentNo]->position_ = position;
  202. }
  203. void RVOSimulator3D::setAgentPrefVelocity(size_t agentNo, const Vector3 &prefVelocity)
  204. {
  205. agents_[agentNo]->prefVelocity_ = prefVelocity;
  206. }
  207. void RVOSimulator3D::setAgentRadius(size_t agentNo, float radius)
  208. {
  209. agents_[agentNo]->radius_ = radius;
  210. }
  211. void RVOSimulator3D::setAgentTimeHorizon(size_t agentNo, float timeHorizon)
  212. {
  213. agents_[agentNo]->timeHorizon_ = timeHorizon;
  214. }
  215. void RVOSimulator3D::setAgentVelocity(size_t agentNo, const Vector3 &velocity)
  216. {
  217. agents_[agentNo]->velocity_ = velocity;
  218. }
  219. void RVOSimulator3D::setTimeStep(float timeStep)
  220. {
  221. timeStep_ = timeStep;
  222. }
  223. }