gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
9.6KB

  1. /******************************************************************************
  2. * $Id: gnmgraph.h 80f31ac0b0753868ab421bf0ff62e6682ad9617e 2017-12-07 18:13:41Z Even Rouault $
  3. *
  4. * Project: GDAL/OGR Geography Network support (Geographic Network Model)
  5. * Purpose: GNM graph implementation.
  6. * Authors: Mikhail Gusev (gusevmihs at gmail dot com)
  7. * Dmitry Baryshnikov, polimax@mail.ru
  8. *
  9. ******************************************************************************
  10. * Copyright (c) 2014, Mikhail Gusev
  11. * Copyright (c) 2014-2015, NextGIS <info@nextgis.com>
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included
  21. * in all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. ****************************************************************************/
  31. #ifndef GNMGRAPH_H
  32. #define GNMGRAPH_H
  33. #include "cpl_port.h"
  34. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  35. #include <map>
  36. #include <queue>
  37. #include <set>
  38. #include <vector>
  39. #endif
  40. // Alias for some big data type to store identificators.
  41. #define GNMGFID GIntBig
  42. // Graph constants
  43. #define GNM_EDGE_DIR_BOTH 0 // bidirectional
  44. #define GNM_EDGE_DIR_SRCTOTGT 1 // from source to target
  45. #define GNM_EDGE_DIR_TGTTOSRC 2 // from target to source
  46. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  47. // Types declarations.
  48. typedef std::vector<GNMGFID> GNMVECTOR, *LPGNMVECTOR;
  49. typedef const std::vector<GNMGFID> GNMCONSTVECTOR;
  50. typedef const std::vector<GNMGFID>* LPGNMCONSTVECTOR;
  51. typedef std::pair<GNMGFID,GNMGFID> EDGEVERTEXPAIR;
  52. typedef std::vector< EDGEVERTEXPAIR > GNMPATH;
  53. /** Edge */
  54. struct GNMStdEdge
  55. {
  56. GNMGFID nSrcVertexFID; /**< Source vertex FID */
  57. GNMGFID nTgtVertexFID; /**< Target vertex FID */
  58. bool bIsBidir; /**< Whether the edge is bidirectonal */
  59. double dfDirCost; /**< Direct cost */
  60. double dfInvCost; /**< Inverse cost */
  61. bool bIsBloked; /**< Whether the edge is blocked */
  62. };
  63. /** Vertex */
  64. struct GNMStdVertex
  65. {
  66. GNMVECTOR anOutEdgeFIDs; /**< TODO */
  67. bool bIsBloked; /**< Whether the vertex is blocked */
  68. };
  69. /**
  70. * The simple graph class, which holds the appropriate for
  71. * calculations graph in memory (based on STL containers) and provides
  72. * several basic algorithms of this graph's analysis. See the methods of
  73. * this class for details. The common thing for all analysis methods is that
  74. * all of them return the results in the array of GFIDs form. Use the
  75. * GNMGraph class to receive the results in OGRLayer form.
  76. * NOTE: GNMGraph holds the whole graph in memory, so it can consume
  77. * a lot of memory if operating huge networks.
  78. *
  79. * @since GDAL 2.1
  80. */
  81. class CPL_DLL GNMGraph
  82. {
  83. public:
  84. GNMGraph();
  85. virtual ~GNMGraph();
  86. // GNMGraph
  87. /**
  88. * @brief Add a vertex to the graph
  89. *
  90. * NOTE: if there are vertices with these ID's already - nothing will be
  91. * added.
  92. *
  93. * @param nFID - vertex identificator
  94. */
  95. virtual void AddVertex(GNMGFID nFID);
  96. /**
  97. * @brief Delete vertex from the graph
  98. * @param nFID Vertex identificator
  99. */
  100. virtual void DeleteVertex(GNMGFID nFID);
  101. /**
  102. * @brief Add an edge to the graph
  103. * @param nConFID Edge identificator
  104. * @param nSrcFID Source vertex identificator
  105. * @param nTgtFID Target vertex identificator
  106. * @param bIsBidir Is bidirectional
  107. * @param dfCost Cost
  108. * @param dfInvCost Inverse cost
  109. */
  110. virtual void AddEdge(GNMGFID nConFID, GNMGFID nSrcFID, GNMGFID nTgtFID,
  111. bool bIsBidir, double dfCost, double dfInvCost);
  112. /**
  113. * @brief Delete edge from graph
  114. * @param nConFID Edge identificator
  115. */
  116. virtual void DeleteEdge(GNMGFID nConFID);
  117. /**
  118. * @brief Change edge properties
  119. * @param nFID Edge identificator
  120. * @param dfCost Cost
  121. * @param dfInvCost Inverse cost
  122. */
  123. virtual void ChangeEdge(GNMGFID nFID, double dfCost, double dfInvCost);
  124. /**
  125. * @brief Change the block state of edge or vertex
  126. * @param nFID Identificator
  127. * @param bBlock Block or unblock
  128. */
  129. virtual void ChangeBlockState (GNMGFID nFID, bool bBlock);
  130. /**
  131. * @brief Check if vertex is blocked
  132. * @param nFID Vertex identificator
  133. * @return true if blocked, false if not blocked.
  134. */
  135. virtual bool CheckVertexBlocked(GNMGFID nFID) const;
  136. /**
  137. * @brief Change all vertices and edges block state.
  138. *
  139. * This is mainly use for unblock all vertices and edges.
  140. *
  141. * @param bBlock Block or unblock
  142. */
  143. virtual void ChangeAllBlockState (bool bBlock = false);
  144. /**
  145. * @brief An implementation of Dijkstra shortest path algorithm.
  146. *
  147. * Returns the best path between nStartFID and nEndFID features. Method
  148. * uses @see DijkstraShortestPathTree and after that searches in
  149. * the resulting tree the path from end to start point.
  150. *
  151. * @param nStartFID Start identificator
  152. * @param nEndFID End identificator
  153. * @return an array of best path included identificator of vertices and
  154. * edges
  155. */
  156. virtual GNMPATH DijkstraShortestPath(GNMGFID nStartFID, GNMGFID nEndFID);
  157. /**
  158. * @brief An implementation of KShortest paths algorithm.
  159. *
  160. * Calculates several best paths between two points. Method takes in account
  161. * the blocking state of features, i.e. the blocked features are the barriers
  162. * during the routing process.
  163. *
  164. * @param nStartFID Vertex identificator from which to start paths calculating.
  165. * @param nEndFID Vertex identificator to which the path will be calculated.
  166. * @param nK How much best paths try to find between start and end points.
  167. * @return an array of best paths. Each path is an array of pairs, where the
  168. * first in a pair is a vertex identificator and the second is an edge
  169. * identificator leading to this vertex. The elements in a path array are
  170. * sorted by the path segments order, i.e. the first is the pair (nStartFID,
  171. * -1) and the last is (nEndFID, &lt;some edge&gt;).
  172. * If there is no any path between start and end vertex the returned array
  173. * of paths will be empty. Also the actual amount of paths in the returned
  174. * array can be less or equal than the nK parameter.
  175. */
  176. virtual std::vector<GNMPATH> KShortestPaths(GNMGFID nStartFID,
  177. GNMGFID nEndFID, size_t nK);
  178. /**
  179. * @brief Search connected components of the network
  180. *
  181. * Returns the resource distribution in the network. Method search starting
  182. * from the features identificators from input array. Uses the recursive
  183. * Breadth-first search algorithm to find the connected to the given vector
  184. * of GFIDs components. Method takes in account the blocking state of
  185. * features, i.e. the blocked features are the barriers during the routing
  186. * process.
  187. *
  188. * @param anEmittersIDs - array of emitters identificators
  189. * @return an array of connected identificators
  190. */
  191. virtual GNMPATH ConnectedComponents(const GNMVECTOR &anEmittersIDs);
  192. /** Clear */
  193. virtual void Clear();
  194. protected:
  195. /**
  196. * @brief Method to create best path tree.
  197. *
  198. * Calculates and builds the best path tree with the Dijkstra
  199. * algorithm starting from the nFID. Method takes in account the blocking
  200. * state of features, i.e. the blocked features are the barriers during the
  201. * routing process.
  202. *
  203. * @param nFID - Vertex identificator from which to start tree building
  204. * @param mstEdges - TODO
  205. * @param mnPathTree - means < vertex id, edge id >.
  206. * std::map where the first is vertex identificator and the second
  207. * is the edge identificator, which is the best way to the current vertex.
  208. * The identificator to the start vertex is -1. If the vertex is isolated
  209. * the returned map will be empty.
  210. */
  211. virtual void DijkstraShortestPathTree(GNMGFID nFID,
  212. const std::map<GNMGFID, GNMStdEdge> &mstEdges,
  213. std::map<GNMGFID, GNMGFID> &mnPathTree);
  214. /** DijkstraShortestPath */
  215. virtual GNMPATH DijkstraShortestPath(GNMGFID nStartFID, GNMGFID nEndFID,
  216. const std::map<GNMGFID, GNMStdEdge> &mstEdges);
  217. //! @cond Doxygen_Suppress
  218. virtual LPGNMCONSTVECTOR GetOutEdges(GNMGFID nFID) const;
  219. virtual GNMGFID GetOppositVertex(GNMGFID nEdgeFID, GNMGFID nVertexFID) const;
  220. virtual void TraceTargets(std::queue<GNMGFID> &vertexQueue,
  221. std::set<GNMGFID> &markedVertIds,
  222. GNMPATH &connectedIds);
  223. protected:
  224. std::map<GNMGFID, GNMStdVertex> m_mstVertices;
  225. std::map<GNMGFID, GNMStdEdge> m_mstEdges;
  226. //! @endcond
  227. };
  228. #endif // __cplusplus
  229. #endif /* GNMGRAPH_H */
上海开阖软件有限公司 沪ICP备12045867号-1