Path: blob/master/thirdparty/rvo2/rvo2_2d/RVOSimulator2d.cpp
9913 views
/*1* RVOSimulator2d.cpp2* RVO2 Library3*4* Copyright 2008 University of North Carolina at Chapel Hill5*6* Licensed under the Apache License, Version 2.0 (the "License");7* you may not use this file except in compliance with the License.8* You may obtain a copy of the License at9*10* http://www.apache.org/licenses/LICENSE-2.011*12* Unless required by applicable law or agreed to in writing, software13* distributed under the License is distributed on an "AS IS" BASIS,14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15* See the License for the specific language governing permissions and16* limitations under the License.17*18* Please send all bug reports to <[email protected]>.19*20* The authors may be contacted via:21*22* Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha23* Dept. of Computer Science24* 201 S. Columbia St.25* Frederick P. Brooks, Jr. Computer Science Bldg.26* Chapel Hill, N.C. 27599-317527* United States of America28*29* <http://gamma.cs.unc.edu/RVO2/>30*/3132#include "RVOSimulator2d.h"3334#include "Agent2d.h"35#include "KdTree2d.h"36#include "Obstacle2d.h"3738#ifdef _OPENMP39#include <omp.h>40#endif4142namespace RVO2D {43RVOSimulator2D::RVOSimulator2D() : defaultAgent_(NULL), globalTime_(0.0f), kdTree_(NULL), timeStep_(0.0f)44{45kdTree_ = new KdTree2D(this);46}4748RVOSimulator2D::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)49{50kdTree_ = new KdTree2D(this);51defaultAgent_ = new Agent2D();5253defaultAgent_->maxNeighbors_ = maxNeighbors;54defaultAgent_->maxSpeed_ = maxSpeed;55defaultAgent_->neighborDist_ = neighborDist;56defaultAgent_->radius_ = radius;57defaultAgent_->timeHorizon_ = timeHorizon;58defaultAgent_->timeHorizonObst_ = timeHorizonObst;59defaultAgent_->velocity_ = velocity;60}6162RVOSimulator2D::~RVOSimulator2D()63{64if (defaultAgent_ != NULL) {65delete defaultAgent_;66}6768for (size_t i = 0; i < agents_.size(); ++i) {69delete agents_[i];70}7172for (size_t i = 0; i < obstacles_.size(); ++i) {73delete obstacles_[i];74}7576delete kdTree_;77}7879size_t RVOSimulator2D::addAgent(const Vector2 &position)80{81if (defaultAgent_ == NULL) {82return RVO2D_ERROR;83}8485Agent2D *agent = new Agent2D();8687agent->position_ = position;88agent->maxNeighbors_ = defaultAgent_->maxNeighbors_;89agent->maxSpeed_ = defaultAgent_->maxSpeed_;90agent->neighborDist_ = defaultAgent_->neighborDist_;91agent->radius_ = defaultAgent_->radius_;92agent->timeHorizon_ = defaultAgent_->timeHorizon_;93agent->timeHorizonObst_ = defaultAgent_->timeHorizonObst_;94agent->velocity_ = defaultAgent_->velocity_;9596agent->id_ = agents_.size();9798agents_.push_back(agent);99100return agents_.size() - 1;101}102103size_t RVOSimulator2D::addAgent(const Vector2 &position, float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity)104{105Agent2D *agent = new Agent2D();106107agent->position_ = position;108agent->maxNeighbors_ = maxNeighbors;109agent->maxSpeed_ = maxSpeed;110agent->neighborDist_ = neighborDist;111agent->radius_ = radius;112agent->timeHorizon_ = timeHorizon;113agent->timeHorizonObst_ = timeHorizonObst;114agent->velocity_ = velocity;115116agent->id_ = agents_.size();117118agents_.push_back(agent);119120return agents_.size() - 1;121}122123size_t RVOSimulator2D::addObstacle(const std::vector<Vector2> &vertices)124{125if (vertices.size() < 2) {126return RVO2D_ERROR;127}128129const size_t obstacleNo = obstacles_.size();130131for (size_t i = 0; i < vertices.size(); ++i) {132Obstacle2D *obstacle = new Obstacle2D();133obstacle->point_ = vertices[i];134135if (i != 0) {136obstacle->prevObstacle_ = obstacles_.back();137obstacle->prevObstacle_->nextObstacle_ = obstacle;138}139140if (i == vertices.size() - 1) {141obstacle->nextObstacle_ = obstacles_[obstacleNo];142obstacle->nextObstacle_->prevObstacle_ = obstacle;143}144145obstacle->unitDir_ = normalize(vertices[(i == vertices.size() - 1 ? 0 : i + 1)] - vertices[i]);146147if (vertices.size() == 2) {148obstacle->isConvex_ = true;149}150else {151obstacle->isConvex_ = (leftOf(vertices[(i == 0 ? vertices.size() - 1 : i - 1)], vertices[i], vertices[(i == vertices.size() - 1 ? 0 : i + 1)]) >= 0.0f);152}153154obstacle->id_ = obstacles_.size();155156obstacles_.push_back(obstacle);157}158159return obstacleNo;160}161162void RVOSimulator2D::doStep()163{164kdTree_->buildAgentTree(agents_);165166for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {167agents_[i]->computeNeighbors(this);168agents_[i]->computeNewVelocity(this);169}170171for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {172agents_[i]->update(this);173}174175globalTime_ += timeStep_;176}177178size_t RVOSimulator2D::getAgentAgentNeighbor(size_t agentNo, size_t neighborNo) const179{180return agents_[agentNo]->agentNeighbors_[neighborNo].second->id_;181}182183size_t RVOSimulator2D::getAgentMaxNeighbors(size_t agentNo) const184{185return agents_[agentNo]->maxNeighbors_;186}187188float RVOSimulator2D::getAgentMaxSpeed(size_t agentNo) const189{190return agents_[agentNo]->maxSpeed_;191}192193float RVOSimulator2D::getAgentNeighborDist(size_t agentNo) const194{195return agents_[agentNo]->neighborDist_;196}197198size_t RVOSimulator2D::getAgentNumAgentNeighbors(size_t agentNo) const199{200return agents_[agentNo]->agentNeighbors_.size();201}202203size_t RVOSimulator2D::getAgentNumObstacleNeighbors(size_t agentNo) const204{205return agents_[agentNo]->obstacleNeighbors_.size();206}207208size_t RVOSimulator2D::getAgentNumORCALines(size_t agentNo) const209{210return agents_[agentNo]->orcaLines_.size();211}212213size_t RVOSimulator2D::getAgentObstacleNeighbor(size_t agentNo, size_t neighborNo) const214{215return agents_[agentNo]->obstacleNeighbors_[neighborNo].second->id_;216}217218const Line &RVOSimulator2D::getAgentORCALine(size_t agentNo, size_t lineNo) const219{220return agents_[agentNo]->orcaLines_[lineNo];221}222223const Vector2 &RVOSimulator2D::getAgentPosition(size_t agentNo) const224{225return agents_[agentNo]->position_;226}227228const Vector2 &RVOSimulator2D::getAgentPrefVelocity(size_t agentNo) const229{230return agents_[agentNo]->prefVelocity_;231}232233float RVOSimulator2D::getAgentRadius(size_t agentNo) const234{235return agents_[agentNo]->radius_;236}237238float RVOSimulator2D::getAgentTimeHorizon(size_t agentNo) const239{240return agents_[agentNo]->timeHorizon_;241}242243float RVOSimulator2D::getAgentTimeHorizonObst(size_t agentNo) const244{245return agents_[agentNo]->timeHorizonObst_;246}247248const Vector2 &RVOSimulator2D::getAgentVelocity(size_t agentNo) const249{250return agents_[agentNo]->velocity_;251}252253float RVOSimulator2D::getGlobalTime() const254{255return globalTime_;256}257258size_t RVOSimulator2D::getNumAgents() const259{260return agents_.size();261}262263size_t RVOSimulator2D::getNumObstacleVertices() const264{265return obstacles_.size();266}267268const Vector2 &RVOSimulator2D::getObstacleVertex(size_t vertexNo) const269{270return obstacles_[vertexNo]->point_;271}272273size_t RVOSimulator2D::getNextObstacleVertexNo(size_t vertexNo) const274{275return obstacles_[vertexNo]->nextObstacle_->id_;276}277278size_t RVOSimulator2D::getPrevObstacleVertexNo(size_t vertexNo) const279{280return obstacles_[vertexNo]->prevObstacle_->id_;281}282283float RVOSimulator2D::getTimeStep() const284{285return timeStep_;286}287288void RVOSimulator2D::processObstacles()289{290kdTree_->buildObstacleTree(obstacles_);291}292293bool RVOSimulator2D::queryVisibility(const Vector2 &point1, const Vector2 &point2, float radius) const294{295return kdTree_->queryVisibility(point1, point2, radius);296}297298void RVOSimulator2D::setAgentDefaults(float neighborDist, size_t maxNeighbors, float timeHorizon, float timeHorizonObst, float radius, float maxSpeed, const Vector2 &velocity)299{300if (defaultAgent_ == NULL) {301defaultAgent_ = new Agent2D();302}303304defaultAgent_->maxNeighbors_ = maxNeighbors;305defaultAgent_->maxSpeed_ = maxSpeed;306defaultAgent_->neighborDist_ = neighborDist;307defaultAgent_->radius_ = radius;308defaultAgent_->timeHorizon_ = timeHorizon;309defaultAgent_->timeHorizonObst_ = timeHorizonObst;310defaultAgent_->velocity_ = velocity;311}312313void RVOSimulator2D::setAgentMaxNeighbors(size_t agentNo, size_t maxNeighbors)314{315agents_[agentNo]->maxNeighbors_ = maxNeighbors;316}317318void RVOSimulator2D::setAgentMaxSpeed(size_t agentNo, float maxSpeed)319{320agents_[agentNo]->maxSpeed_ = maxSpeed;321}322323void RVOSimulator2D::setAgentNeighborDist(size_t agentNo, float neighborDist)324{325agents_[agentNo]->neighborDist_ = neighborDist;326}327328void RVOSimulator2D::setAgentPosition(size_t agentNo, const Vector2 &position)329{330agents_[agentNo]->position_ = position;331}332333void RVOSimulator2D::setAgentPrefVelocity(size_t agentNo, const Vector2 &prefVelocity)334{335agents_[agentNo]->prefVelocity_ = prefVelocity;336}337338void RVOSimulator2D::setAgentRadius(size_t agentNo, float radius)339{340agents_[agentNo]->radius_ = radius;341}342343void RVOSimulator2D::setAgentTimeHorizon(size_t agentNo, float timeHorizon)344{345agents_[agentNo]->timeHorizon_ = timeHorizon;346}347348void RVOSimulator2D::setAgentTimeHorizonObst(size_t agentNo, float timeHorizonObst)349{350agents_[agentNo]->timeHorizonObst_ = timeHorizonObst;351}352353void RVOSimulator2D::setAgentVelocity(size_t agentNo, const Vector2 &velocity)354{355agents_[agentNo]->velocity_ = velocity;356}357358void RVOSimulator2D::setTimeStep(float timeStep)359{360timeStep_ = timeStep;361}362}363364365