/*1* KdTree2d.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_KD_TREE_H_33#define RVO2D_KD_TREE_H_3435/**36* \file KdTree2d.h37* \brief Contains the KdTree class.38*/3940#include "Definitions.h"4142namespace RVO2D {43/**44* \brief Defines <i>k</i>d-trees for agents and static obstacles in the45* simulation.46*/47class KdTree2D {48public:49/**50* \brief Defines an agent <i>k</i>d-tree node.51*/52class AgentTreeNode {53public:54/**55* \brief The beginning node number.56*/57size_t begin;5859/**60* \brief The ending node number.61*/62size_t end;6364/**65* \brief The left node number.66*/67size_t left;6869/**70* \brief The maximum x-coordinate.71*/72float maxX;7374/**75* \brief The maximum y-coordinate.76*/77float maxY;7879/**80* \brief The minimum x-coordinate.81*/82float minX;8384/**85* \brief The minimum y-coordinate.86*/87float minY;8889/**90* \brief The right node number.91*/92size_t right;93};9495/**96* \brief Defines an obstacle <i>k</i>d-tree node.97*/98class ObstacleTreeNode {99public:100/**101* \brief The left obstacle tree node.102*/103ObstacleTreeNode *left;104105/**106* \brief The obstacle number.107*/108const Obstacle2D *obstacle;109110/**111* \brief The right obstacle tree node.112*/113ObstacleTreeNode *right;114};115116/**117* \brief Constructs a <i>k</i>d-tree instance.118* \param sim The simulator instance.119*/120explicit KdTree2D(RVOSimulator2D *sim);121122/**123* \brief Destroys this kd-tree instance.124*/125~KdTree2D();126127/**128* \brief Builds an agent <i>k</i>d-tree.129*/130void buildAgentTree(std::vector<Agent2D *> agents);131132void buildAgentTreeRecursive(size_t begin, size_t end, size_t node);133134/**135* \brief Builds an obstacle <i>k</i>d-tree.136*/137void buildObstacleTree(std::vector<Obstacle2D *> obstacles);138139ObstacleTreeNode *buildObstacleTreeRecursive(const std::vector<Obstacle2D *> &140obstacles);141142/**143* \brief Computes the agent neighbors of the specified agent.144* \param agent A pointer to the agent for which agent145* neighbors are to be computed.146* \param rangeSq The squared range around the agent.147*/148void computeAgentNeighbors(Agent2D *agent, float &rangeSq) const;149150/**151* \brief Computes the obstacle neighbors of the specified agent.152* \param agent A pointer to the agent for which obstacle153* neighbors are to be computed.154* \param rangeSq The squared range around the agent.155*/156void computeObstacleNeighbors(Agent2D *agent, float rangeSq) const;157158/**159* \brief Deletes the specified obstacle tree node.160* \param node A pointer to the obstacle tree node to be161* deleted.162*/163void deleteObstacleTree(ObstacleTreeNode *node);164165void queryAgentTreeRecursive(Agent2D *agent, float &rangeSq,166size_t node) const;167168void queryObstacleTreeRecursive(Agent2D *agent, float rangeSq,169const ObstacleTreeNode *node) const;170171/**172* \brief Queries the visibility between two points within a173* specified radius.174* \param q1 The first point between which visibility is175* to be tested.176* \param q2 The second point between which visibility is177* to be tested.178* \param radius The radius within which visibility is to be179* tested.180* \return True if q1 and q2 are mutually visible within the radius;181* false otherwise.182*/183bool queryVisibility(const Vector2 &q1, const Vector2 &q2,184float radius) const;185186bool queryVisibilityRecursive(const Vector2 &q1, const Vector2 &q2,187float radius,188const ObstacleTreeNode *node) const;189190std::vector<Agent2D *> agents_;191std::vector<AgentTreeNode> agentTree_;192ObstacleTreeNode *obstacleTree_;193RVOSimulator2D *sim_;194195static const size_t MAX_LEAF_SIZE = 10;196197friend class Agent2D;198friend class RVOSimulator2D;199};200}201202#endif /* RVO2D_KD_TREE_H_ */203204205