Advertisement
Guest User

SAPDivision.h

a guest
Sep 24th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. /* Copyright (C) 2013 Wildfire Games.
  2.  * This file is part of 0 A.D.
  3.  *
  4.  * 0 A.D. is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * 0 A.D. is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #ifndef INCLUDED_SAPDIVISION
  19. #define INCLUDED_SAPDIVISION
  20.  
  21. #include <vector>
  22. #include "Vector234.h"
  23. #include "GuiObject.h"
  24.  
  25.  
  26. /**
  27.  * A simulated Entity object for this demo alone. The pyrogenesis Entity is completely different,
  28.  * but its semantics remain the same. Entities have position, size and range.
  29.  */
  30. struct CEntityHandle
  31. {
  32.     Vector2 pos;
  33.     Vector2 size;
  34.     float range;
  35.     GuiText text;
  36.     inline CEntityHandle(const Vector2& pos) : pos(pos), range(10.0f)
  37.     {
  38.         size.x = float((rand() % 20) + 20);
  39.         size.y = float((rand() % 20) + 20);
  40.     }
  41.     inline CEntityHandle(const Vector2& pos, const Vector2& size) : pos(pos), size(size), range(10.0f)
  42.     {
  43.     }
  44. };
  45.  
  46.  
  47. /**
  48.  * SAPDivision entity handle
  49.  * Contains X axis index for fast random access
  50.  */
  51. struct SAPHandle
  52. {
  53.     int index;              // index in X axis
  54.     CEntityHandle* entity;  // pointer to the entity object with some data
  55. };
  56.  
  57.  
  58. /**
  59.  * SAPDivision entity node
  60.  * Contains entity BOUNDS, POSITION and pointer to SAPHandle that owns the index
  61.  */
  62. struct SAPNode
  63. {
  64.     float x_min, x_max; // ------| object |------> axis : the object BOUNDS
  65.     float z_min, z_max;
  66.     float x_pos, z_pos; // object position X & Z
  67.     union {
  68.     SAPHandle* handle;  // pointer to handle that owns the index
  69.     struct {int a,b;};  // force 32-byte alignment
  70.     };
  71. };
  72.  
  73.  
  74.  
  75.  
  76. /**
  77.  * A range query structure for fast queries
  78.  */
  79. struct SAPRangeQuery
  80. {
  81.     enum { MAX_COUNT = 2048 };
  82.     struct Result
  83.     {
  84.         float sqrange;      // squared range
  85.         SAPHandle* handle;  // entity handle
  86.     };
  87.     int count;
  88.     Result items[MAX_COUNT];
  89.  
  90.     inline SAPRangeQuery() : count(0) {}
  91.     inline const Result& operator[](int index) const { return items[index]; } // read-only
  92.     inline int size() const { return count; }
  93.     inline void clear() { count = 0; }
  94. };
  95.  
  96.  
  97. /**
  98.  * Sweep and Prune spatial subdivision - Great for rangefinding.
  99.  */
  100. class SAPDivision
  101. {
  102.     std::vector<SAPNode> m_XAxis;       // we only need X axis for sweeping
  103.     std::vector<SAPHandle*> m_LiveHandles;  // currently used handles
  104.     std::vector<SAPHandle*> m_FreeHandles;  // reuse handles if possible
  105. public:
  106.     SAPDivision();
  107.     ~SAPDivision();
  108.  
  109.     SAPDivision(const SAPDivision& rhs);
  110.     SAPDivision& operator=(const SAPDivision& rhs);
  111.  
  112. private:
  113.     SAPHandle* AllocHandle();
  114.  
  115.  
  116. public:
  117.  
  118.     /**
  119.      * Clears the current state, but doesn't free any SAPHandle's
  120.      */
  121.     void Clear();
  122.  
  123.     /**
  124.      * Destroys the current state, deleting all handles
  125.      */
  126.     void Destroy();
  127.  
  128.     /**
  129.      * @brief Inserts a new entity into the subdivision map as a bounding box.
  130.      * @param entity Entity to insert
  131.      * @param toMin Entity world BOUNDING BOX min
  132.      * @param toMax Entity world BOUNDING BOX max
  133.      */
  134.     SAPHandle* Insert(CEntityHandle* entity, const Vector2& toMin, const Vector2& toMax);
  135.  
  136.     /**
  137.      * @brief Inserts a new entity into the subdivision map as a point.
  138.      * @param entity Entity to insert
  139.      * @param to Point in world.
  140.      */
  141.     inline SAPHandle* Insert(CEntityHandle* entity, const Vector2& to)
  142.     {
  143.         return Insert(entity, to, to);
  144.     }
  145.  
  146.     /**
  147.      * @brief Removes an entity from the subdivision map.
  148.      * @param handle SAPEntity handle to erase.
  149.      */
  150.     void Remove(SAPHandle* handle);
  151.  
  152.     /**
  153.      * @brief Moves an entity by the given amount. Very cheap - simple shift in the Axis.
  154.      * @note For small moves the entity is shifted in the SAP axises. Large moves shift the axis array.
  155.      * @param handle SAPEntity handle to move.
  156.      * @param pos The new position where to move the entity.
  157.      */
  158.     void Move(SAPHandle* handle, const Vector2& pos);
  159.  
  160.     /**
  161.      * @brief Get entities in range of a BOUNDING BOX.
  162.      * @note This is used by OBSTRUCTION MANAGER.
  163.      * @param inrange [out] The resulting array of entities in range. Sorted from nearest to furthest.
  164.      * @param min Bounding box minimum.
  165.      * @param max Bounding box maximum.
  166.      */
  167.     void GetInBoundingBox(SAPRangeQuery& inrange, const Vector2& min, const Vector2& max);
  168.  
  169.     /**
  170.      * @brief Get entities in range of a RADII.
  171.      * @note This works differently from GetInBoundingBox and is MUCH FASTER!! Used by RANGE MANAGER.
  172.      * @param inrange [out] The resulting array of entities in range. Sorted from nearest to furthest.
  173.      * @param pos Center of the range query.
  174.      * @param range RADII of the range query.
  175.      */
  176.     int GetInEntityRange(SAPRangeQuery& inrange, SAPHandle* handle, float range);
  177. };
  178.  
  179.  
  180. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement