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