From 1701730410392a69cba30995f9017d7b6be5a495 Mon Sep 17 00:00:00 2001 From: Jamie Snape Date: Sat, 24 Sep 2022 20:53:37 -0400 Subject: [PATCH] Remove reference to simulator in agent class --- src/Agent.cc | 20 +++++++++----------- src/Agent.h | 26 ++++++++++++++------------ src/RVOSimulator.cc | 16 ++++++++-------- src/RVOSimulator.h | 2 -- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/Agent.cc b/src/Agent.cc index 612c260..92c41b4 100644 --- a/src/Agent.cc +++ b/src/Agent.cc @@ -44,7 +44,6 @@ #include "KdTree.h" #include "Obstacle.h" -#include "RVOSimulator.h" namespace RVO { namespace { @@ -243,9 +242,8 @@ void linearProgram3(const std::vector &lines, std::size_t numObstLines, } } /* namespace */ -Agent::Agent(RVOSimulator *simulator) - : simulator_(simulator), - id_(0U), +Agent::Agent() + : id_(0U), maxNeighbors_(0U), maxSpeed_(0.0F), neighborDist_(0.0F), @@ -255,21 +253,21 @@ Agent::Agent(RVOSimulator *simulator) Agent::~Agent() {} -void Agent::computeNeighbors() { +void Agent::computeNeighbors(const KdTree *kdTree) { obstacleNeighbors_.clear(); const float range = timeHorizonObst_ * maxSpeed_ + radius_; - simulator_->kdTree_->computeObstacleNeighbors(this, range * range); + kdTree->computeObstacleNeighbors(this, range * range); agentNeighbors_.clear(); if (maxNeighbors_ > 0U) { float rangeSq = neighborDist_ * neighborDist_; - simulator_->kdTree_->computeAgentNeighbors(this, rangeSq); + kdTree->computeAgentNeighbors(this, rangeSq); } } /* Search for the best new velocity. */ -void Agent::computeNewVelocity() { +void Agent::computeNewVelocity(float timeStep) { orcaLines_.clear(); const float invTimeHorizonObst = 1.0F / timeHorizonObst_; @@ -594,7 +592,7 @@ void Agent::computeNewVelocity() { } } else { /* Collision. Project on cut-off circle of time timeStep. */ - const float invTimeStep = 1.0F / simulator_->timeStep_; + const float invTimeStep = 1.0F / timeStep; /* Vector from cutoff center to relative velocity. */ const Vector2 w = relativeVelocity - invTimeStep * relativePosition; @@ -674,8 +672,8 @@ void Agent::insertObstacleNeighbor(const Obstacle *obstacle, float rangeSq) { } } -void Agent::update() { +void Agent::update(float timeStep) { velocity_ = newVelocity_; - position_ += velocity_ * simulator_->timeStep_; + position_ += velocity_ * timeStep; } } /* namespace RVO */ diff --git a/src/Agent.h b/src/Agent.h index b6c88dc..987afdf 100644 --- a/src/Agent.h +++ b/src/Agent.h @@ -47,8 +47,8 @@ #include "Vector2.h" namespace RVO { +class KdTree; class Obstacle; -class RVOSimulator; /** * @brief Defines an agent in the simulation. @@ -56,10 +56,9 @@ class RVOSimulator; class Agent { private: /** - * @brief Constructs an agent instance. - * @param[in] simulator The simulator instance. + * @brief Constructs an agent instance. */ - explicit Agent(RVOSimulator *simulator); + Agent(); /** * @brief Destroys this agent instance. @@ -67,14 +66,17 @@ class Agent { ~Agent(); /** - * @brief Computes the neighbors of this agent. + * @brief Computes the neighbors of this agent. + * @param[in] kdTree A pointer to the k-D trees for agents and static + * obstacles in the simulation. */ - void computeNeighbors(); + void computeNeighbors(const KdTree *kdTree); /** - * @brief Computes the new velocity of this agent. + * @brief Computes the new velocity of this agent. + * @param[in] timeStep The time step of the simulation. */ - void computeNewVelocity(); + void computeNewVelocity(float timeStep); /** * @brief Inserts an agent neighbor into the set of neighbors of this @@ -94,10 +96,11 @@ class Agent { void insertObstacleNeighbor(const Obstacle *obstacle, float rangeSq); /** - * @brief Updates the two-dimensional position and two-dimensional velocity of - * this agent. + * @brief Updates the two-dimensional position and two-dimensional + * velocity of this agent. + * @param[in] timeStep The time step of the simulation. */ - void update(); + void update(float timeStep); /* Not implemented. */ Agent(const Agent &other); @@ -112,7 +115,6 @@ class Agent { Vector2 position_; Vector2 prefVelocity_; Vector2 velocity_; - RVOSimulator *simulator_; std::size_t id_; std::size_t maxNeighbors_; float maxSpeed_; diff --git a/src/RVOSimulator.cc b/src/RVOSimulator.cc index 703ace3..1ecf171 100644 --- a/src/RVOSimulator.cc +++ b/src/RVOSimulator.cc @@ -63,7 +63,7 @@ RVOSimulator::RVOSimulator() RVOSimulator::RVOSimulator(float timeStep, float neighborDist, std::size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed) - : defaultAgent_(new Agent(this)), + : defaultAgent_(new Agent()), kdTree_(new KdTree(this)), globalTime_(0.0F), timeStep_(timeStep) { @@ -79,7 +79,7 @@ RVOSimulator::RVOSimulator(float timeStep, float neighborDist, std::size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity) - : defaultAgent_(new Agent(this)), + : defaultAgent_(new Agent()), kdTree_(new KdTree(this)), globalTime_(0.0F), timeStep_(timeStep) { @@ -107,7 +107,7 @@ RVOSimulator::~RVOSimulator() { std::size_t RVOSimulator::addAgent(const Vector2 &position) { if (defaultAgent_ != NULL) { - Agent *const agent = new Agent(this); + Agent *const agent = new Agent(); agent->position_ = position; agent->velocity_ = defaultAgent_->velocity_; agent->id_ = agents_.size(); @@ -137,7 +137,7 @@ std::size_t RVOSimulator::addAgent(const Vector2 &position, float neighborDist, std::size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity) { - Agent *const agent = new Agent(this); + Agent *const agent = new Agent(); agent->position_ = position; agent->velocity_ = velocity; agent->id_ = agents_.size(); @@ -200,15 +200,15 @@ void RVOSimulator::doStep() { #pragma omp parallel for #endif /* _OPENMP */ for (int i = 0; i < static_cast(agents_.size()); ++i) { - agents_[i]->computeNeighbors(); - agents_[i]->computeNewVelocity(); + agents_[i]->computeNeighbors(kdTree_); + agents_[i]->computeNewVelocity(timeStep_); } #ifdef _OPENMP #pragma omp parallel for #endif /* _OPENMP */ for (int i = 0; i < static_cast(agents_.size()); ++i) { - agents_[i]->update(); + agents_[i]->update(timeStep_); } globalTime_ += timeStep_; @@ -315,7 +315,7 @@ void RVOSimulator::setAgentDefaults(float neighborDist, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity) { if (defaultAgent_ == NULL) { - defaultAgent_ = new Agent(this); + defaultAgent_ = new Agent(); } defaultAgent_->maxNeighbors_ = maxNeighbors; diff --git a/src/RVOSimulator.h b/src/RVOSimulator.h index 06bba14..46e3414 100644 --- a/src/RVOSimulator.h +++ b/src/RVOSimulator.h @@ -659,9 +659,7 @@ class RVO_EXPORT RVOSimulator { float globalTime_; float timeStep_; - friend class Agent; friend class KdTree; - friend class Obstacle; }; } /* namespace RVO */