gwnavruntime/database/databasegeometrybuildingmanager.h Source File

databasegeometrybuildingmanager.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #pragma once
9 
13 
14 namespace Kaim
15 {
16 
17 class DatabaseGeometryBuildingManager
18 {
19  class DatabaseVisualTile
20  {
21  public:
22  DatabaseVisualTile() : m_needToBeRebuild(false) {}
23  ~DatabaseVisualTile() {}
24 
25  Ptr<IVisualGeometry> m_visualGeometry;
26  CellBox m_cellBox;
27  bool m_needToBeRebuild;
28  };
29 
30 public:
31  DatabaseGeometryBuildingManager();
32  ~DatabaseGeometryBuildingManager();
33 
34  void Clear();
35 
36  void BuildGeometry(bool forceRebuildAll = false);
37 
38  void SetDetailLevel(DatabaseVisualDetails::Enum detailLevel) { m_visualDetails = detailLevel; }
39  DatabaseVisualDetails::Enum GetDetailLevel() const { return m_visualDetails; }
40 
41  // no tiling
42  void SetVisualGeometry(Ptr<IVisualGeometry> geometry);
43  IVisualGeometry* GetVisualGeometry() const { return m_visualGeometry; }
44 
45  // tilling is used
46  void SetVisualGeometryFactory(Ptr<IVisualGeometryFactory> factory);
47  IVisualGeometryFactory* GetVisualGeometryFactory() const { return m_visualGeometryFactory; }
48 
49  // m_tileSize can be changed through this accessor only when the database is clear
50  // once at least one navdata has been added, it cannot be changed anymore until the Database is cleared again.
51  void SetTileSize(KyInt32 tileSize);
52 
53 public: // internal
54  void OnEnlarge(const CellBox& cellBox);
55  void OnChangeAtPos(const CellPos& cellPos);
56  void OnChangeInGraph();
57 
58  KyInt32 ComputeTileCoordFromCellCoord(KyInt32 coord) const;
59  Vec2i ComputeTilePosFromCellPos(const CellPos& cellPos) const;
60  Box2i ComputeTileBoxFromCellBox(const CellBox& cellBox) const;
61  CellBox ComputeCellBoxForTilePos(const Vec2i& tilePos) const;
62 
63 private:
64  friend class Database;
65  void OnEnlarge_FactoryProvided(const CellBox& newCellBox);
66  void OnChangeAtPos_FactoryProvided(const CellPos& cellPos);
67  void OnChangeInGraph_FactoryProvided();
68 
69  void AskNewGeometryForTileIfNeeded(DatabaseVisualTile& tile);
70 private:
71  Database* m_database;
72  KyUInt32 m_databaseIdx;
73 
74  DatabaseVisualDetails::Enum m_visualDetails;
75 
76  KyInt32 m_tileSize; // number of cell on x/y axis per tile
77 
78  // not a CellBox, a tileBox !
79  // tile at pos (0, 0) has cellBox [(0 ; 0) (m_tileSize-1 ;m_tileSize-1)]
80  // tile at pos (1, 0) has cellBox [(m_tileSize; 0) (2*m_tileSize-1;m_tileSize-1)]
81  // tile at pos (0,-1) has cellBox [(0 ;m_tileSize) (m_tileSize-1 ; -1)]
82  Box2i m_tileBox;
83 
84  // array of visRep associated to m_boxOfTile. Access it through RawMajorIdx
85  DatabaseVisualTile* m_bufferOfTile;
86  KyUInt32 m_bufferSize;
87 
88  DatabaseVisualTile m_tileForGraphs;
89 
90  Ptr<IVisualGeometryFactory> m_visualGeometryFactory;
91 
92  // Used if provided AND if no geometryFactory provided (when no tile mode)
93  Ptr<IVisualGeometry> m_visualGeometry;
94 };
95 
96 KY_INLINE KyInt32 DatabaseGeometryBuildingManager::ComputeTileCoordFromCellCoord(KyInt32 coord) const
97 {
98  return coord >= 0 ? coord / m_tileSize : ((coord + 1) / m_tileSize) - 1;
99 }
100 
101 KY_INLINE Vec2i DatabaseGeometryBuildingManager::ComputeTilePosFromCellPos(const CellPos& cellPos) const
102 {
103  return Vec2i(ComputeTileCoordFromCellCoord(cellPos.x), ComputeTileCoordFromCellCoord(cellPos.y));
104 }
105 
106 KY_INLINE Box2i DatabaseGeometryBuildingManager::ComputeTileBoxFromCellBox(const CellBox& cellBox) const
107 {
108  return Box2i(ComputeTilePosFromCellPos(cellBox.Min()), ComputeTilePosFromCellPos(cellBox.Max()));
109 }
110 
111 KY_INLINE CellBox DatabaseGeometryBuildingManager::ComputeCellBoxForTilePos(const Vec2i& tilePos) const
112 {
113  const CellPos minCellPos(tilePos.x * m_tileSize, tilePos.y * m_tileSize);
114  return CellBox(minCellPos, minCellPos + CellPos(m_tileSize - 1, m_tileSize - 1));
115 }
116 
117 KY_INLINE void DatabaseGeometryBuildingManager::OnEnlarge(const CellBox& cellBox)
118 {
119  if (m_visualGeometryFactory != nullptr)
120  OnEnlarge_FactoryProvided(cellBox);
121 }
122 KY_INLINE void DatabaseGeometryBuildingManager::OnChangeAtPos(const CellPos& cellPos)
123 {
124  if (m_visualGeometryFactory != nullptr)
125  OnChangeAtPos_FactoryProvided(cellPos);
126 }
127 KY_INLINE void DatabaseGeometryBuildingManager::OnChangeInGraph()
128 {
129  if (m_visualGeometryFactory != nullptr)
130  AskNewGeometryForTileIfNeeded(m_tileForGraphs);
131 }
132 
133 KY_INLINE void DatabaseGeometryBuildingManager::AskNewGeometryForTileIfNeeded(DatabaseVisualTile& tile)
134 {
135  tile.m_needToBeRebuild = true;
136  if (tile.m_visualGeometry == nullptr)
137  {
138  tile.m_visualGeometry = m_visualGeometryFactory->CreateIVisualGeometryForDatabase(m_databaseIdx);
139  KY_LOG_ERROR_IF(tile.m_visualGeometry == nullptr,
140  ("Failed to create an IVisualGeometry from the IVisualGeometryFactory. A portion of the Database won't be displayed."));
141  }
142 }
143 
144 } // namespace Kaim
145 
Box2i CellBox
A type that represents a bounding box around cells in a 2D grid.
Definition: navmeshtypes.h:31
Vec2i CellPos
A type that represents the position of a cell within a 2D grid.
Definition: navmeshtypes.h:30
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::int32_t KyInt32
int32_t
Definition: types.h:24