Path: blob/master/thirdparty/rvo2/rvo2_3d/RVOSimulator3d.cpp
9902 views
/*1* RVOSimulator.cpp2* RVO2-3D 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 "RVOSimulator3d.h"3334#ifdef _OPENMP35#include <omp.h>36#endif3738#include "Agent3d.h"39#include "KdTree3d.h"4041namespace RVO3D {42RVOSimulator3D::RVOSimulator3D() : defaultAgent_(NULL), kdTree_(NULL), globalTime_(0.0f), timeStep_(0.0f)43{44kdTree_ = new KdTree3D(this);45}4647RVOSimulator3D::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)48{49kdTree_ = new KdTree3D(this);50defaultAgent_ = new Agent3D();5152defaultAgent_->maxNeighbors_ = maxNeighbors;53defaultAgent_->maxSpeed_ = maxSpeed;54defaultAgent_->neighborDist_ = neighborDist;55defaultAgent_->radius_ = radius;56defaultAgent_->timeHorizon_ = timeHorizon;57defaultAgent_->velocity_ = velocity;58}5960RVOSimulator3D::~RVOSimulator3D()61{62if (defaultAgent_ != NULL) {63delete defaultAgent_;64}6566for (size_t i = 0; i < agents_.size(); ++i) {67delete agents_[i];68}6970if (kdTree_ != NULL) {71delete kdTree_;72}73}7475size_t RVOSimulator3D::getAgentNumAgentNeighbors(size_t agentNo) const76{77return agents_[agentNo]->agentNeighbors_.size();78}7980size_t RVOSimulator3D::getAgentAgentNeighbor(size_t agentNo, size_t neighborNo) const81{82return agents_[agentNo]->agentNeighbors_[neighborNo].second->id_;83}8485size_t RVOSimulator3D::getAgentNumORCAPlanes(size_t agentNo) const86{87return agents_[agentNo]->orcaPlanes_.size();88}8990const Plane &RVOSimulator3D::getAgentORCAPlane(size_t agentNo, size_t planeNo) const91{92return agents_[agentNo]->orcaPlanes_[planeNo];93}9495void RVOSimulator3D::removeAgent(size_t agentNo)96{97delete agents_[agentNo];98agents_[agentNo] = agents_.back();99agents_.pop_back();100}101102size_t RVOSimulator3D::addAgent(const Vector3 &position)103{104if (defaultAgent_ == NULL) {105return RVO3D_ERROR;106}107108Agent3D *agent = new Agent3D();109110agent->position_ = position;111agent->maxNeighbors_ = defaultAgent_->maxNeighbors_;112agent->maxSpeed_ = defaultAgent_->maxSpeed_;113agent->neighborDist_ = defaultAgent_->neighborDist_;114agent->radius_ = defaultAgent_->radius_;115agent->timeHorizon_ = defaultAgent_->timeHorizon_;116agent->velocity_ = defaultAgent_->velocity_;117118agent->id_ = agents_.size();119120agents_.push_back(agent);121122return agents_.size() - 1;123}124125size_t RVOSimulator3D::addAgent(const Vector3 &position, float neighborDist, size_t maxNeighbors, float timeHorizon, float radius, float maxSpeed, const Vector3 &velocity)126{127Agent3D *agent = new Agent3D();128129agent->position_ = position;130agent->maxNeighbors_ = maxNeighbors;131agent->maxSpeed_ = maxSpeed;132agent->neighborDist_ = neighborDist;133agent->radius_ = radius;134agent->timeHorizon_ = timeHorizon;135agent->velocity_ = velocity;136137agent->id_ = agents_.size();138139agents_.push_back(agent);140141return agents_.size() - 1;142}143144void RVOSimulator3D::doStep()145{146kdTree_->buildAgentTree(agents_);147148for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {149agents_[i]->computeNeighbors(this);150agents_[i]->computeNewVelocity(this);151}152153for (int i = 0; i < static_cast<int>(agents_.size()); ++i) {154agents_[i]->update(this);155}156157globalTime_ += timeStep_;158}159160size_t RVOSimulator3D::getAgentMaxNeighbors(size_t agentNo) const161{162return agents_[agentNo]->maxNeighbors_;163}164165float RVOSimulator3D::getAgentMaxSpeed(size_t agentNo) const166{167return agents_[agentNo]->maxSpeed_;168}169170float RVOSimulator3D::getAgentNeighborDist(size_t agentNo) const171{172return agents_[agentNo]->neighborDist_;173}174175const Vector3 &RVOSimulator3D::getAgentPosition(size_t agentNo) const176{177return agents_[agentNo]->position_;178}179180const Vector3 &RVOSimulator3D::getAgentPrefVelocity(size_t agentNo) const181{182return agents_[agentNo]->prefVelocity_;183}184185float RVOSimulator3D::getAgentRadius(size_t agentNo) const186{187return agents_[agentNo]->radius_;188}189190float RVOSimulator3D::getAgentTimeHorizon(size_t agentNo) const191{192return agents_[agentNo]->timeHorizon_;193}194195const Vector3 &RVOSimulator3D::getAgentVelocity(size_t agentNo) const196{197return agents_[agentNo]->velocity_;198}199200float RVOSimulator3D::getGlobalTime() const201{202return globalTime_;203}204205size_t RVOSimulator3D::getNumAgents() const206{207return agents_.size();208}209210float RVOSimulator3D::getTimeStep() const211{212return timeStep_;213}214215void RVOSimulator3D::setAgentDefaults(float neighborDist, size_t maxNeighbors, float timeHorizon, float radius, float maxSpeed, const Vector3 &velocity)216{217if (defaultAgent_ == NULL) {218defaultAgent_ = new Agent3D();219}220221defaultAgent_->maxNeighbors_ = maxNeighbors;222defaultAgent_->maxSpeed_ = maxSpeed;223defaultAgent_->neighborDist_ = neighborDist;224defaultAgent_->radius_ = radius;225defaultAgent_->timeHorizon_ = timeHorizon;226defaultAgent_->velocity_ = velocity;227}228229void RVOSimulator3D::setAgentMaxNeighbors(size_t agentNo, size_t maxNeighbors)230{231agents_[agentNo]->maxNeighbors_ = maxNeighbors;232}233234void RVOSimulator3D::setAgentMaxSpeed(size_t agentNo, float maxSpeed)235{236agents_[agentNo]->maxSpeed_ = maxSpeed;237}238239void RVOSimulator3D::setAgentNeighborDist(size_t agentNo, float neighborDist)240{241agents_[agentNo]->neighborDist_ = neighborDist;242}243244void RVOSimulator3D::setAgentPosition(size_t agentNo, const Vector3 &position)245{246agents_[agentNo]->position_ = position;247}248249void RVOSimulator3D::setAgentPrefVelocity(size_t agentNo, const Vector3 &prefVelocity)250{251agents_[agentNo]->prefVelocity_ = prefVelocity;252}253254void RVOSimulator3D::setAgentRadius(size_t agentNo, float radius)255{256agents_[agentNo]->radius_ = radius;257}258259void RVOSimulator3D::setAgentTimeHorizon(size_t agentNo, float timeHorizon)260{261agents_[agentNo]->timeHorizon_ = timeHorizon;262}263264void RVOSimulator3D::setAgentVelocity(size_t agentNo, const Vector3 &velocity)265{266agents_[agentNo]->velocity_ = velocity;267}268269void RVOSimulator3D::setTimeStep(float timeStep)270{271timeStep_ = timeStep;272}273}274275276