Path: blob/master/thirdparty/rvo2/rvo2_2d/RVOSimulator2d.h
9905 views
/*1* RVOSimulator2d.h2* 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#ifndef RVO2D_RVO_SIMULATOR_H_33#define RVO2D_RVO_SIMULATOR_H_3435/**36* \file RVOSimulator2d.h37* \brief Contains the RVOSimulator2D class.38*/3940#include <cstddef>41#include <limits>42#include <vector>4344#include "Vector2.h"4546namespace RVO2D {47/**48* \brief Error value.49*50* A value equal to the largest unsigned integer that is returned in case51* of an error by functions in RVO2D::RVOSimulator2D.52*/53const size_t RVO2D_ERROR = std::numeric_limits<size_t>::max();5455/**56* \brief Defines a directed line.57*/58class Line {59public:60/**61* \brief A point on the directed line.62*/63Vector2 point;6465/**66* \brief The direction of the directed line.67*/68Vector2 direction;69};7071class Agent2D;72class KdTree2D;73class Obstacle2D;7475/**76* \brief Defines the simulation.77*78* The main class of the library that contains all simulation functionality.79*/80class RVOSimulator2D {81public:82/**83* \brief Constructs a simulator instance.84*/85RVOSimulator2D();8687/**88* \brief Constructs a simulator instance and sets the default89* properties for any new agent that is added.90* \param timeStep The time step of the simulation.91* Must be positive.92* \param neighborDist The default maximum distance (center point93* to center point) to other agents a new agent94* takes into account in the navigation. The95* larger this number, the longer he running96* time of the simulation. If the number is too97* low, the simulation will not be safe. Must be98* non-negative.99* \param maxNeighbors The default maximum number of other agents a100* new agent takes into account in the101* navigation. The larger this number, the102* longer the running time of the simulation.103* If the number is too low, the simulation104* will not be safe.105* \param timeHorizon The default minimal amount of time for which106* a new agent's velocities that are computed107* by the simulation are safe with respect to108* other agents. The larger this number, the109* sooner an agent will respond to the presence110* of other agents, but the less freedom the111* agent has in choosing its velocities.112* Must be positive.113* \param timeHorizonObst The default minimal amount of time for which114* a new agent's velocities that are computed115* by the simulation are safe with respect to116* obstacles. The larger this number, the117* sooner an agent will respond to the presence118* of obstacles, but the less freedom the agent119* has in choosing its velocities.120* Must be positive.121* \param radius The default radius of a new agent.122* Must be non-negative.123* \param maxSpeed The default maximum speed of a new agent.124* Must be non-negative.125* \param velocity The default initial two-dimensional linear126* velocity of a new agent (optional).127*/128RVOSimulator2D(float timeStep, float neighborDist, size_t maxNeighbors,129float timeHorizon, float timeHorizonObst, float radius,130float maxSpeed, const Vector2 &velocity = Vector2());131132/**133* \brief Destroys this simulator instance.134*/135~RVOSimulator2D();136137/**138* \brief Adds a new agent with default properties to the139* simulation.140* \param position The two-dimensional starting position of141* this agent.142* \return The number of the agent, or RVO2D::RVO2D_ERROR when the agent143* defaults have not been set.144*/145size_t addAgent(const Vector2 &position);146147/**148* \brief Adds a new agent to the simulation.149* \param position The two-dimensional starting position of150* this agent.151* \param neighborDist The maximum distance (center point to152* center point) to other agents this agent153* takes into account in the navigation. The154* larger this number, the longer the running155* time of the simulation. If the number is too156* low, the simulation will not be safe.157* Must be non-negative.158* \param maxNeighbors The maximum number of other agents this159* agent takes into account in the navigation.160* The larger this number, the longer the161* running time of the simulation. If the162* number is too low, the simulation will not163* be safe.164* \param timeHorizon The minimal amount of time for which this165* agent's velocities that are computed by the166* simulation are safe with respect to other167* agents. The larger this number, the sooner168* this agent will respond to the presence of169* other agents, but the less freedom this170* agent has in choosing its velocities.171* Must be positive.172* \param timeHorizonObst The minimal amount of time for which this173* agent's velocities that are computed by the174* simulation are safe with respect to175* obstacles. The larger this number, the176* sooner this agent will respond to the177* presence of obstacles, but the less freedom178* this agent has in choosing its velocities.179* Must be positive.180* \param radius The radius of this agent.181* Must be non-negative.182* \param maxSpeed The maximum speed of this agent.183* Must be non-negative.184* \param velocity The initial two-dimensional linear velocity185* of this agent (optional).186* \return The number of the agent.187*/188size_t addAgent(const Vector2 &position, float neighborDist,189size_t maxNeighbors, float timeHorizon,190float timeHorizonObst, float radius, float maxSpeed,191const Vector2 &velocity = Vector2());192193/**194* \brief Adds a new obstacle to the simulation.195* \param vertices List of the vertices of the polygonal196* obstacle in counterclockwise order.197* \return The number of the first vertex of the obstacle,198* or RVO2D::RVO2D_ERROR when the number of vertices is less than two.199* \note To add a "negative" obstacle, e.g. a bounding polygon around200* the environment, the vertices should be listed in clockwise201* order.202*/203size_t addObstacle(const std::vector<Vector2> &vertices);204205/**206* \brief Lets the simulator perform a simulation step and updates the207* two-dimensional position and two-dimensional velocity of208* each agent.209*/210void doStep();211212/**213* \brief Returns the specified agent neighbor of the specified214* agent.215* \param agentNo The number of the agent whose agent216* neighbor is to be retrieved.217* \param neighborNo The number of the agent neighbor to be218* retrieved.219* \return The number of the neighboring agent.220*/221size_t getAgentAgentNeighbor(size_t agentNo, size_t neighborNo) const;222223/**224* \brief Returns the maximum neighbor count of a specified agent.225* \param agentNo The number of the agent whose maximum226* neighbor count is to be retrieved.227* \return The present maximum neighbor count of the agent.228*/229size_t getAgentMaxNeighbors(size_t agentNo) const;230231/**232* \brief Returns the maximum speed of a specified agent.233* \param agentNo The number of the agent whose maximum speed234* is to be retrieved.235* \return The present maximum speed of the agent.236*/237float getAgentMaxSpeed(size_t agentNo) const;238239/**240* \brief Returns the maximum neighbor distance of a specified241* agent.242* \param agentNo The number of the agent whose maximum243* neighbor distance is to be retrieved.244* \return The present maximum neighbor distance of the agent.245*/246float getAgentNeighborDist(size_t agentNo) const;247248/**249* \brief Returns the count of agent neighbors taken into account to250* compute the current velocity for the specified agent.251* \param agentNo The number of the agent whose count of agent252* neighbors is to be retrieved.253* \return The count of agent neighbors taken into account to compute254* the current velocity for the specified agent.255*/256size_t getAgentNumAgentNeighbors(size_t agentNo) const;257258/**259* \brief Returns the count of obstacle neighbors taken into account260* to compute the current velocity for the specified agent.261* \param agentNo The number of the agent whose count of262* obstacle neighbors is to be retrieved.263* \return The count of obstacle neighbors taken into account to264* compute the current velocity for the specified agent.265*/266size_t getAgentNumObstacleNeighbors(size_t agentNo) const;267268269/**270* \brief Returns the count of ORCA constraints used to compute271* the current velocity for the specified agent.272* \param agentNo The number of the agent whose count of ORCA273* constraints is to be retrieved.274* \return The count of ORCA constraints used to compute the current275* velocity for the specified agent.276*/277size_t getAgentNumORCALines(size_t agentNo) const;278279/**280* \brief Returns the specified obstacle neighbor of the specified281* agent.282* \param agentNo The number of the agent whose obstacle283* neighbor is to be retrieved.284* \param neighborNo The number of the obstacle neighbor to be285* retrieved.286* \return The number of the first vertex of the neighboring obstacle287* edge.288*/289size_t getAgentObstacleNeighbor(size_t agentNo, size_t neighborNo) const;290291/**292* \brief Returns the specified ORCA constraint of the specified293* agent.294* \param agentNo The number of the agent whose ORCA295* constraint is to be retrieved.296* \param lineNo The number of the ORCA constraint to be297* retrieved.298* \return A line representing the specified ORCA constraint.299* \note The halfplane to the left of the line is the region of300* permissible velocities with respect to the specified301* ORCA constraint.302*/303const Line &getAgentORCALine(size_t agentNo, size_t lineNo) const;304305/**306* \brief Returns the two-dimensional position of a specified307* agent.308* \param agentNo The number of the agent whose309* two-dimensional position is to be retrieved.310* \return The present two-dimensional position of the (center of the)311* agent.312*/313const Vector2 &getAgentPosition(size_t agentNo) const;314315/**316* \brief Returns the two-dimensional preferred velocity of a317* specified agent.318* \param agentNo The number of the agent whose319* two-dimensional preferred velocity is to be320* retrieved.321* \return The present two-dimensional preferred velocity of the agent.322*/323const Vector2 &getAgentPrefVelocity(size_t agentNo) const;324325/**326* \brief Returns the radius of a specified agent.327* \param agentNo The number of the agent whose radius is to328* be retrieved.329* \return The present radius of the agent.330*/331float getAgentRadius(size_t agentNo) const;332333/**334* \brief Returns the time horizon of a specified agent.335* \param agentNo The number of the agent whose time horizon336* is to be retrieved.337* \return The present time horizon of the agent.338*/339float getAgentTimeHorizon(size_t agentNo) const;340341/**342* \brief Returns the time horizon with respect to obstacles of a343* specified agent.344* \param agentNo The number of the agent whose time horizon345* with respect to obstacles is to be346* retrieved.347* \return The present time horizon with respect to obstacles of the348* agent.349*/350float getAgentTimeHorizonObst(size_t agentNo) const;351352/**353* \brief Returns the two-dimensional linear velocity of a354* specified agent.355* \param agentNo The number of the agent whose356* two-dimensional linear velocity is to be357* retrieved.358* \return The present two-dimensional linear velocity of the agent.359*/360const Vector2 &getAgentVelocity(size_t agentNo) const;361362/**363* \brief Returns the global time of the simulation.364* \return The present global time of the simulation (zero initially).365*/366float getGlobalTime() const;367368/**369* \brief Returns the count of agents in the simulation.370* \return The count of agents in the simulation.371*/372size_t getNumAgents() const;373374/**375* \brief Returns the count of obstacle vertices in the simulation.376* \return The count of obstacle vertices in the simulation.377*/378size_t getNumObstacleVertices() const;379380/**381* \brief Returns the two-dimensional position of a specified obstacle382* vertex.383* \param vertexNo The number of the obstacle vertex to be384* retrieved.385* \return The two-dimensional position of the specified obstacle386* vertex.387*/388const Vector2 &getObstacleVertex(size_t vertexNo) const;389390/**391* \brief Returns the number of the obstacle vertex succeeding the392* specified obstacle vertex in its polygon.393* \param vertexNo The number of the obstacle vertex whose394* successor is to be retrieved.395* \return The number of the obstacle vertex succeeding the specified396* obstacle vertex in its polygon.397*/398size_t getNextObstacleVertexNo(size_t vertexNo) const;399400/**401* \brief Returns the number of the obstacle vertex preceding the402* specified obstacle vertex in its polygon.403* \param vertexNo The number of the obstacle vertex whose404* predecessor is to be retrieved.405* \return The number of the obstacle vertex preceding the specified406* obstacle vertex in its polygon.407*/408size_t getPrevObstacleVertexNo(size_t vertexNo) const;409410/**411* \brief Returns the time step of the simulation.412* \return The present time step of the simulation.413*/414float getTimeStep() const;415416/**417* \brief Processes the obstacles that have been added so that they418* are accounted for in the simulation.419* \note Obstacles added to the simulation after this function has420* been called are not accounted for in the simulation.421*/422void processObstacles();423424/**425* \brief Performs a visibility query between the two specified426* points with respect to the obstacles427* \param point1 The first point of the query.428* \param point2 The second point of the query.429* \param radius The minimal distance between the line430* connecting the two points and the obstacles431* in order for the points to be mutually432* visible (optional). Must be non-negative.433* \return A boolean specifying whether the two points are mutually434* visible. Returns true when the obstacles have not been435* processed.436*/437bool queryVisibility(const Vector2 &point1, const Vector2 &point2,438float radius = 0.0f) const;439440/**441* \brief Sets the default properties for any new agent that is442* added.443* \param neighborDist The default maximum distance (center point444* to center point) to other agents a new agent445* takes into account in the navigation. The446* larger this number, the longer he running447* time of the simulation. If the number is too448* low, the simulation will not be safe.449* Must be non-negative.450* \param maxNeighbors The default maximum number of other agents a451* new agent takes into account in the452* navigation. The larger this number, the453* longer the running time of the simulation.454* If the number is too low, the simulation455* will not be safe.456* \param timeHorizon The default minimal amount of time for which457* a new agent's velocities that are computed458* by the simulation are safe with respect to459* other agents. The larger this number, the460* sooner an agent will respond to the presence461* of other agents, but the less freedom the462* agent has in choosing its velocities.463* Must be positive.464* \param timeHorizonObst The default minimal amount of time for which465* a new agent's velocities that are computed466* by the simulation are safe with respect to467* obstacles. The larger this number, the468* sooner an agent will respond to the presence469* of obstacles, but the less freedom the agent470* has in choosing its velocities.471* Must be positive.472* \param radius The default radius of a new agent.473* Must be non-negative.474* \param maxSpeed The default maximum speed of a new agent.475* Must be non-negative.476* \param velocity The default initial two-dimensional linear477* velocity of a new agent (optional).478*/479void setAgentDefaults(float neighborDist, size_t maxNeighbors,480float timeHorizon, float timeHorizonObst,481float radius, float maxSpeed,482const Vector2 &velocity = Vector2());483484/**485* \brief Sets the maximum neighbor count of a specified agent.486* \param agentNo The number of the agent whose maximum487* neighbor count is to be modified.488* \param maxNeighbors The replacement maximum neighbor count.489*/490void setAgentMaxNeighbors(size_t agentNo, size_t maxNeighbors);491492/**493* \brief Sets the maximum speed of a specified agent.494* \param agentNo The number of the agent whose maximum speed495* is to be modified.496* \param maxSpeed The replacement maximum speed. Must be497* non-negative.498*/499void setAgentMaxSpeed(size_t agentNo, float maxSpeed);500501/**502* \brief Sets the maximum neighbor distance of a specified agent.503* \param agentNo The number of the agent whose maximum504* neighbor distance is to be modified.505* \param neighborDist The replacement maximum neighbor distance.506* Must be non-negative.507*/508void setAgentNeighborDist(size_t agentNo, float neighborDist);509510/**511* \brief Sets the two-dimensional position of a specified agent.512* \param agentNo The number of the agent whose513* two-dimensional position is to be modified.514* \param position The replacement of the two-dimensional515* position.516*/517void setAgentPosition(size_t agentNo, const Vector2 &position);518519/**520* \brief Sets the two-dimensional preferred velocity of a521* specified agent.522* \param agentNo The number of the agent whose523* two-dimensional preferred velocity is to be524* modified.525* \param prefVelocity The replacement of the two-dimensional526* preferred velocity.527*/528void setAgentPrefVelocity(size_t agentNo, const Vector2 &prefVelocity);529530/**531* \brief Sets the radius of a specified agent.532* \param agentNo The number of the agent whose radius is to533* be modified.534* \param radius The replacement radius.535* Must be non-negative.536*/537void setAgentRadius(size_t agentNo, float radius);538539/**540* \brief Sets the time horizon of a specified agent with respect541* to other agents.542* \param agentNo The number of the agent whose time horizon543* is to be modified.544* \param timeHorizon The replacement time horizon with respect545* to other agents. Must be positive.546*/547void setAgentTimeHorizon(size_t agentNo, float timeHorizon);548549/**550* \brief Sets the time horizon of a specified agent with respect551* to obstacles.552* \param agentNo The number of the agent whose time horizon553* with respect to obstacles is to be modified.554* \param timeHorizonObst The replacement time horizon with respect to555* obstacles. Must be positive.556*/557void setAgentTimeHorizonObst(size_t agentNo, float timeHorizonObst);558559/**560* \brief Sets the two-dimensional linear velocity of a specified561* agent.562* \param agentNo The number of the agent whose563* two-dimensional linear velocity is to be564* modified.565* \param velocity The replacement two-dimensional linear566* velocity.567*/568void setAgentVelocity(size_t agentNo, const Vector2 &velocity);569570/**571* \brief Sets the time step of the simulation.572* \param timeStep The time step of the simulation.573* Must be positive.574*/575void setTimeStep(float timeStep);576577public:578std::vector<Agent2D *> agents_;579Agent2D *defaultAgent_;580float globalTime_;581KdTree2D *kdTree_;582std::vector<Obstacle2D *> obstacles_;583float timeStep_;584585friend class Agent2D;586friend class KdTree2D;587friend class Obstacle2D;588};589}590591#endif /* RVO2D_RVO_SIMULATOR_H_ */592593594