Skip to content

Commit

Permalink
Remove reference to simulator in agent class
Browse files Browse the repository at this point in the history
  • Loading branch information
snape committed Sep 25, 2022
1 parent 6bd842f commit 1701730
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
20 changes: 9 additions & 11 deletions src/Agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

#include "KdTree.h"
#include "Obstacle.h"
#include "RVOSimulator.h"

namespace RVO {
namespace {
Expand Down Expand Up @@ -243,9 +242,8 @@ void linearProgram3(const std::vector<Line> &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),
Expand All @@ -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_;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
26 changes: 14 additions & 12 deletions src/Agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,36 @@
#include "Vector2.h"

namespace RVO {
class KdTree;
class Obstacle;
class RVOSimulator;

/**
* @brief Defines an agent in the simulation.
*/
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.
*/
~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
Expand All @@ -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);
Expand All @@ -112,7 +115,6 @@ class Agent {
Vector2 position_;
Vector2 prefVelocity_;
Vector2 velocity_;
RVOSimulator *simulator_;
std::size_t id_;
std::size_t maxNeighbors_;
float maxSpeed_;
Expand Down
16 changes: 8 additions & 8 deletions src/RVOSimulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -200,15 +200,15 @@ void RVOSimulator::doStep() {
#pragma omp parallel for
#endif /* _OPENMP */
for (int i = 0; i < static_cast<int>(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<int>(agents_.size()); ++i) {
agents_[i]->update();
agents_[i]->update(timeStep_);
}

globalTime_ += timeStep_;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/RVOSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,7 @@ class RVO_EXPORT RVOSimulator {
float globalTime_;
float timeStep_;

friend class Agent;
friend class KdTree;
friend class Obstacle;
};
} /* namespace RVO */

Expand Down

0 comments on commit 1701730

Please sign in to comment.