GraphPreProcessor.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #ifndef GRAPHPREPROCESSOR_H
  2. #define GRAPHPREPROCESSOR_H
  3. #include <GraphLayoutLibrary_global.h>
  4. #include <iostream>
  5. #include <boost/tuple/tuple.hpp>
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/graph/properties.hpp>
  8. #include <boost/graph/graph_traits.hpp>
  9. #include <boost/graph/lookup_edge.hpp>
  10. #include <boost/property_map/property_map.hpp>
  11. #include <boost/ref.hpp>
  12. #include <vector>
  13. #include <boost/graph/make_connected.hpp>
  14. #include <boost/graph/make_biconnected_planar.hpp>
  15. #include <boost/graph/make_maximal_planar.hpp>
  16. #include <boost/graph/planar_canonical_ordering.hpp>
  17. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  18. #include <boost/graph/dijkstra_shortest_paths.hpp>
  19. #include <Common/BoostGraphWrapper.h>
  20. #include <Common/LayoutUtility.h>
  21. #include <GridBasedLayout/CreateDualGraph.hpp>
  22. using namespace boost;
  23. /**
  24. * @brief The GraphPreProcessor class
  25. *
  26. * A class to handle all preprocessing and postprocessing operations on the graph for grid based layouting.
  27. */
  28. class GRAPHLAYOUTLIBRARYSHARED_EXPORT GraphPreProcessor
  29. {
  30. public:
  31. /**
  32. * @brief graph
  33. *
  34. * adjecency_list graph type used by the layout algorithm for maintaining local copy of input graph.
  35. */
  36. typedef adjacency_list
  37. < listS,
  38. vecS,
  39. undirectedS,
  40. property<vertex_index_t, int>,
  41. property<edge_index_t, int>
  42. >
  43. graph;
  44. /**
  45. * @brief UndirEdgeOrderType
  46. *
  47. * vector of edges in graph.
  48. */
  49. typedef std::vector<graph_traits< graph >::edge_descriptor > UndirEdgeOrderType;
  50. /**
  51. * @brief UndirVertexOrderType
  52. *
  53. * vector of vertices in graph.
  54. */
  55. typedef std::vector<graph_traits< graph >::vertex_descriptor > UndirVertexOrderType;
  56. /**
  57. * @brief dijkstra_graph
  58. *
  59. * adjecency_list undirected and weighted graph type used by the Dijkstra's algorithm for converting non-planar graph to planar.
  60. */
  61. typedef adjacency_list
  62. < listS,
  63. vecS,
  64. undirectedS,//derectedS
  65. property<vertex_index_t, int>,//no_property,
  66. property < edge_weight_t, int, property<edge_index_t, int> >
  67. >
  68. dijkstra_graph;
  69. /**
  70. * @brief dijkstra_vertex_descriptor
  71. *
  72. * adjecency_list graph type used by the layout algorithm for maintaining local copy of input graph.
  73. */
  74. typedef graph_traits < dijkstra_graph >::vertex_descriptor dijkstra_vertex_descriptor;
  75. /**
  76. * @brief EdgeOrderType
  77. *
  78. * vector of edges in dijkstra_graph.
  79. */
  80. typedef std::vector< EdgeDescriptor > EdgeOrderType;
  81. /**
  82. * @brief VertexOrderType
  83. *
  84. * vector of vertices in dijkstra_graph.
  85. */
  86. typedef std::vector< VertexDescriptor > VertexOrderType;
  87. /**
  88. * @brief EdgePairType
  89. *
  90. * vector of VertexOrderType.
  91. */
  92. typedef std::vector<VertexOrderType> EdgePairType;
  93. /**
  94. * @brief CorrespondingEdgesType
  95. *
  96. * vector of EdgeOrderType.
  97. */
  98. typedef std::vector<EdgeOrderType> CorrespondingEdgesType;
  99. public:
  100. /** @name Creators
  101. * The methods under this section are responsible for constructing
  102. * an instance of type GraphPreProcessor.
  103. */
  104. //@{
  105. /**
  106. Constructs object of type GraphPreProcessor.
  107. @pre
  108. none
  109. @throw
  110. none
  111. */
  112. GraphPreProcessor();
  113. //@}
  114. /** @name Modifiers
  115. * The methods under this section are responsible for modifying
  116. * an instance of GraphPreProcessor.
  117. */
  118. //@{
  119. /**
  120. This method reads a planar graph, converts it to maximal planar graph by adding some extra edges, if needed,
  121. and writes the edges added in the supplied parameter.
  122. * @brief toMaximalPlanarProcess
  123. * converts planar graph to maximal planar.
  124. *
  125. * @pre gInputGraph is planar
  126. *
  127. * @param gInputGraph
  128. * graph which is to be made maximal planar.
  129. *
  130. * @param oDummyEdges
  131. * vector of edges that will store dummy edges that will be added in gInputGraph.
  132. *
  133. * @return none
  134. *
  135. * @throw LayoutException
  136. */
  137. void toMaximalPlanar(SubGraph &gInputGraph, EdgeOrderType &oDummyEdges);
  138. /**
  139. This method reads a maximal planar graph (by toMaximalPlanar()),
  140. and converts it to planar graph by removing the extra edges. The extra dummy edges
  141. need to be supplied in the parameter.
  142. * @brief removeDummyEdgesProcess
  143. * converts maximal planar graph obtained from toMaximalPlanar() to its original graph.
  144. *
  145. * @pre edges in oDummyEdges must be present in gInputGraph
  146. *
  147. * @param gInputGraph
  148. * graph from which dummy vertices need to be removed.
  149. *
  150. * @param oDummyEdges
  151. * vector of edges that stores dummy edges that have to be removed from gInputGraph
  152. *
  153. * @return none
  154. *
  155. * @throw LayoutException
  156. */
  157. void removeDummyEdges(SubGraph &gInputGraph, EdgeOrderType &oDummyEdges);
  158. /**
  159. This method reads a non-planar graph, converts it to planar graph by adding some extra vertices
  160. and writes the vertices added in the supplied parameter. Dummy vertex replaces each edge crossing.
  161. The process replaces crossing pair of edges by a dummy vertex and four edges connecting that dummy vertex
  162. to endpoints of crossing edges.
  163. * @brief toPlanarProcess
  164. * converts gInputGraph to a planar graph.
  165. *
  166. * @param gInputGraph
  167. * graph from which dummy vertices need to be removed.
  168. *
  169. * @param oDummyVertices
  170. * vector of vertices that will store dummy vertices that will be added in gInputGraph
  171. *
  172. * @param correspondingCrossingEdgeList
  173. * vector of vector of vector (3D vector) of vertices to store crossing edges
  174. * that a dummy vertex will replace
  175. *
  176. * @return none
  177. *
  178. * @throw LayoutException
  179. */
  180. void toPlanar(SubGraph & gInputGraph, VertexOrderType &oDummyVertexList,
  181. std::vector<GraphPreProcessor::EdgePairType> & correspondingCrossingEdgeList);
  182. /**
  183. this method reads a planar graph (which is output of layout algorithm),
  184. and converts it to non-planar graph by removing the extra vertices and edges
  185. in the supplied parameter
  186. * @brief removeDummyVerticesProcess
  187. * replaces dummy vertices with corresponding crossing pair of edges.
  188. *
  189. * @param gInputGraph
  190. *
  191. * @param oDummyVertices
  192. * vector of vertices that stores dummy vertices that will be removed from gInputGraph
  193. *
  194. * @param correspondingCrossingEdge
  195. * vector of vector of vector (3D vector) of vertices that stores crossing edges
  196. * that will replace a dummy vertex
  197. *
  198. * @return none
  199. *
  200. * @throw LayoutException
  201. */
  202. void removeDummyVertices(SubGraph & gInputGraph, VertexOrderType &oDummyVertices,
  203. std::vector<GraphPreProcessor::EdgePairType> & correspondingCrossingEdge);
  204. /**
  205. this method reads a non-planar graph, removes the parallel edges from input graph
  206. and stores them in the supplied parameter
  207. * @brief removeParallelEdgesProcess
  208. *
  209. * @pre gInputGraph ! = NULL
  210. *
  211. * @param gInputGraph
  212. *
  213. * @param oParallelEdgeList
  214. * vector of edges that will store parallel edges that are removed from gInputGraph
  215. *
  216. * @return none
  217. *
  218. * @throw LayoutException
  219. */
  220. void removeParallelEdges(SubGraph & gInputGraph, EdgeOrderType & oParallelEdgeList, std::vector<QString> & oParrallelEdgeIDs);
  221. /**
  222. this method reads a non-planar graph, adds the parallel edges stored in the supplied parameter
  223. to input graph
  224. * @brief addParallelEdgesProcess
  225. *
  226. * @pre gInputGraph ! = NULL
  227. *
  228. * @param gInputGraph
  229. *
  230. * @param oParallelEdgeList
  231. * vector of edges that stores parallel edges that are added to gInputGraph
  232. *
  233. * @return none
  234. *
  235. * @throw LayoutException
  236. */
  237. void addParallelEdges(SubGraph & gInputGraph, EdgeOrderType & oParallelEdgeList, std::vector<QString> & oParrallelEdgeIDs);
  238. /**
  239. this method reads a non-planar graph, converts it to planar graph by removing some crossing edges
  240. and writes the edges removed in the supplied parameter
  241. * @brief toPlanarAlternateProcess
  242. *
  243. * @pre gInputGraph ! = NULL
  244. *
  245. * @param gInputGraph
  246. *
  247. * @param oNonPlanarEdges
  248. * vector of edges that will store edges that caused crossings and will be removed from gInputGraph
  249. *
  250. * @return none
  251. *
  252. * @throw none
  253. */
  254. void toPlanarAlternate(SubGraph & gInputGraph, EdgeOrderType &oNonPlanarEdges);
  255. /**
  256. this method reads planar output graph and adds the edges that were making it non-planar.
  257. These edges are supplied in the parameter
  258. * @brief addNonPlanarEdgesProcess
  259. *
  260. * @pre gInputGraph ! = NULL
  261. *
  262. * @param gInputGraph
  263. *
  264. * @param oNonPlanarEdges
  265. * vector of edges that will store edges that caused crossings and will be removed from gInputGraph
  266. *
  267. * @return none
  268. *
  269. * @throw none
  270. */
  271. void addNonPlanarEdges(SubGraph & gInputGraph, EdgeOrderType &oNonPlanarEdges);
  272. //@}
  273. // void searchAndRemoveDummyVertices(SubGraph & gInputGraph);
  274. private:
  275. /**
  276. This method finds path between source and target given list of parents of each node
  277. in Dijkstra's minimal spanning tree. The path is in reverse order.
  278. * @brief findPathProcess
  279. * calculates path from list of parents of vertices.
  280. *
  281. * @pre vTarget exists in parentVertex
  282. *
  283. * @param vTarget
  284. * target vertex to which the path is to be found out (from source provided to Dijkstra's algorithm).
  285. *
  286. * @param myPath
  287. * vector of vertices that stores path in reverse (target to source).
  288. *
  289. * @param parentVertex
  290. * vector of vertices that contains parent of each vertex in Dijkstra's paths.
  291. *
  292. * @return none
  293. *
  294. * @throw LayoutException
  295. */
  296. void findPath(GraphPreProcessor::dijkstra_vertex_descriptor &vTarget,
  297. std::vector<GraphPreProcessor::dijkstra_vertex_descriptor> &myPath,
  298. std::vector<GraphPreProcessor::dijkstra_vertex_descriptor> &parentVertex);
  299. /**
  300. This method finds the edges which cross with other edges when present in the InputGraph.
  301. It generates a copy of graph with such edges removed and a list of these edges.
  302. * @brief findCrossingEdgesProcess
  303. * separates out edges that introduce crossings in the graph.
  304. *
  305. * @param gInputGraph
  306. * graph from which crossing edges need to be found out.
  307. *
  308. * @param gLocalGraph
  309. * copy of graph which will be generated after removing crossing edges.
  310. *
  311. * @param localCrossingEdgeList
  312. * vector of crossing edges.
  313. *
  314. * @return none
  315. *
  316. * @throw LayoutException
  317. */
  318. void findCrossingEdges(SubGraph & gInputGraph, GraphPreProcessor::graph & gLocalGraph,
  319. std::vector< graph_traits< graph >::edge_descriptor > & localCrossingEdgeList);
  320. /**
  321. This method replaces each pair of crossing edge by a dummy vertex and four connecting edges.
  322. It stores the pair of replaced edges corresponding to each dummy vertex in the supplie parameter.
  323. * @brief replaceCrossingsByDummyVerticesProcess
  324. * replaces each crossing by a dummy vertex.
  325. *
  326. * @param gInputGraph
  327. * graph from which crossing edges need to be removed.
  328. *
  329. * @param gLocalGraph
  330. * copy of graph which was generated after removing crossing edges.
  331. *
  332. * @param localCrossingEdgeList
  333. * vector of crossing edges.
  334. *
  335. * @param oDummyVertexList
  336. * vector of dummy vertices which will be added to the graph.
  337. *
  338. * @param correspondingCrossingEdgeList
  339. * vector of vector of vector (3D vector) of vertices to store crossing edges
  340. * that a dummy vertex will replace
  341. *
  342. * @return none
  343. *
  344. * @throw LayoutException
  345. */
  346. void replaceCrossingsByDummyVertices(SubGraph & gInputGraph, GraphPreProcessor::graph & gLocalGraph,
  347. std::vector< graph_traits< graph >::edge_descriptor > & localCrossingEdgeList,
  348. VertexOrderType &oDummyVertexList,
  349. std::vector<GraphPreProcessor::EdgePairType> & correspondingCrossingEdgeList);
  350. /**
  351. This method replaces each pair of crossing edge by a dummy vertex and four connecting edges.
  352. It stores the pair of replaced edges corresponding to each dummy vertex in the supplie parameter.
  353. * @brief createAugmentedDualGraphProcess
  354. * constructs dual graph of a given graph augmented with source and terget vertices of a crossing edge.
  355. *
  356. * @param gLocalGraph
  357. * copy of graph whose augmented dual graph need to be generated.
  358. *
  359. * @param gDualGraph
  360. * augmented dual graph which will be generated.
  361. *
  362. * @param dualEdge
  363. * vector of edges from LocalGraph which are dual of edges in DualGraph.
  364. *
  365. * @param oIndicesOfEndsOfCrossingEdge
  366. * vector of vertex indices which need to be augmented to the DualGraph.
  367. *
  368. * @param oAugmentedVertices
  369. * vector of vertices which will get augmented to the DualGraph.
  370. *
  371. * @return none
  372. *
  373. * @throw LayoutException
  374. */
  375. void createAugmentedDualGraph(GraphPreProcessor::graph & gLocalGraph,
  376. GraphPreProcessor::graph & gDualGraph,
  377. UndirEdgeOrderType & dualEdge,
  378. std::vector< int > & oIndicesOfEndsOfCrossingEdge,
  379. std::vector< graph_traits<GraphPreProcessor::graph>::vertex_descriptor > & oAugmentedVertices);
  380. /**
  381. This method replaces each pair of crossing edge by a dummy vertex and four connecting edges.
  382. It stores the pair of replaced edges corresponding to each dummy vertex in the supplie parameter.
  383. * @brief createAugmentedDualGraphProcess
  384. * constructs dual graph of a given graph augmented with source and terget vertices of a crossing edge.
  385. *
  386. * @param gLocalGraph
  387. * copy of graph whose augmented dual graph need to be generated.
  388. *
  389. * @param gDualGraph
  390. * augmented dual graph which will be generated.
  391. *
  392. * @param dualEdge
  393. * vector of edges from LocalGraph which are dual of edges in DualGraph.
  394. *
  395. * @param oIndicesOfEndsOfCrossingEdge
  396. * vector of vertex indices which need to be augmented to the DualGraph.
  397. *
  398. * @param oAugmentedVertices
  399. * vector of vertices which will get augmented to the DualGraph.
  400. *
  401. * @return none
  402. *
  403. * @throw LayoutException
  404. */
  405. void findShortestPath(GraphPreProcessor::graph & gDualGraph,
  406. GraphPreProcessor::dijkstra_graph & gDijkstraGraph,
  407. graph_traits<GraphPreProcessor::graph>::vertex_descriptor & vAugmentedSource,
  408. graph_traits<GraphPreProcessor::graph>::vertex_descriptor & vAugmentedTarget,
  409. std::vector<GraphPreProcessor::dijkstra_vertex_descriptor> & path);
  410. };
  411. #endif // GRAPHPREPROCESSOR_H