GridBasedLayout.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #ifndef GRIDBASEDLAYOUT_H
  2. #define GRIDBASEDLAYOUT_H
  3. #include <GraphLayoutLibrary_global.h>
  4. #include <iostream>
  5. #include <boost/graph/adjacency_list.hpp>
  6. #include <boost/graph/properties.hpp>
  7. #include <boost/graph/graph_traits.hpp>
  8. #include <boost/graph/lookup_edge.hpp>
  9. #include <boost/property_map/property_map.hpp>
  10. #include <boost/ref.hpp>
  11. #include <vector>
  12. #include <boost/graph/planar_canonical_ordering.hpp>
  13. #include <boost/graph/boyer_myrvold_planar_test.hpp>
  14. #include <Common/BoostGraphWrapper.h>
  15. #include <Common/LayoutUtility.h>
  16. using namespace boost;
  17. #define ONE 1
  18. #define TWO 2
  19. #define SEPARATION 20
  20. /**
  21. * @brief The GridBasedLayout class
  22. *
  23. * A class to apply grid based layout on a non-clustered graph.
  24. */
  25. class GRAPHLAYOUTLIBRARYSHARED_EXPORT GridBasedLayout
  26. {
  27. public:
  28. /**
  29. * @brief The Graph structure
  30. *
  31. * Structure to store local graph which is used in finding canonical order.
  32. */
  33. typedef adjacency_list
  34. < listS,
  35. vecS,
  36. undirectedS,
  37. property<vertex_index_t, int>,
  38. property<edge_index_t, int>
  39. >
  40. graph;
  41. /**
  42. * @brief The Vertices vector
  43. *
  44. * Vector of vertices.
  45. */
  46. typedef std::vector<graph_traits< graph >::vertex_descriptor> VertexOrderType; /*!< Vector of vertices */
  47. /**
  48. * @brief The Position struct
  49. *
  50. * A stucture to store X and Y coordinates of a vertex.
  51. */
  52. struct Position
  53. {
  54. int iCoordX;
  55. int iCoordY;
  56. };
  57. public:
  58. /** @name Creators
  59. *
  60. * The methods under this section are responsible for constructing
  61. * an instance of type GridBasedLayout.
  62. */
  63. //@{
  64. /**
  65. Constructs new object of type GridBasedLayout.
  66. @pre none
  67. @param none
  68. @return none
  69. @throw none
  70. */
  71. GridBasedLayout();
  72. //@}
  73. /** @name Queries
  74. * The methods under this section are responsible for accessing
  75. * an instance of type GridBasedLayout.
  76. */
  77. //@{
  78. /**
  79. this method reads graph, finds the canonical ordering of the vertices
  80. and writes the canonical order to the supplied parameter
  81. * @brief findCanonicalOrderProcess
  82. *
  83. * @pre gInputGraph ! = NULL
  84. *
  85. * @param gInputGraph
  86. * maximal planar graph in which the canonical order of vertices is to be found.
  87. *
  88. * @param oVertexIndexCanonicalOrder
  89. * vector of integers which will store indices of vertices in canonical order.
  90. *
  91. * @return none
  92. *
  93. * @throw none
  94. */
  95. void findCanonicalOrder(SubGraph &gInputGraph, std::vector<int> & oVertexIndexCanonicalOrder);
  96. /**
  97. this method reads graph and the canonical order of vertices
  98. and finds out the grid based layout of the graph
  99. * @brief findGridBasedLayoutProcess
  100. *
  101. * @pre gInputGraph ! = NULL && oCanonicalOrder != NULL
  102. *
  103. * @param gInputGraph
  104. * maximal planar graph which is to be layed out.
  105. *
  106. * @param oCanonicalOrder
  107. * vector of vertex_desciptors that contains canonical order of graph vertices.
  108. *
  109. * @param oVertexPosition
  110. * vector of Position to store x and y coordinates of vertices.
  111. *
  112. * @return none
  113. *
  114. * @throw LayoutException
  115. */
  116. void findGridBasedLayout(SubGraph & gInputGraph, std::vector<int> & oCanonicalOrder,
  117. std::vector< GridBasedLayout::Position > & oVertexPosition);
  118. //@}
  119. private:
  120. /**
  121. this method reads a vertex and finds out vertices in the canonical order
  122. which are neighbours of the vertex in the input graph
  123. * @brief findNeighboursProcess
  124. *
  125. * @pre gInputGraph ! = NULL && oCanonicalOrder != NULL && vNewVertex != NULL
  126. *
  127. * @param gInputGraph
  128. * maximal planar graph which is to be layed out.
  129. *
  130. * @param oContour
  131. * vector of vertex_desciptors that contains contour of graph in previous stage
  132. *
  133. * @param vNewVertex
  134. * vertex whose neighbours are to be found out
  135. *
  136. * @param p
  137. * index in contour of neighbour of vNewVertex which comes first in contour
  138. *
  139. * @param q
  140. * index in contour of neighbour of vNewVertex which comes last in contour
  141. *
  142. * @return none
  143. *
  144. * @throw none
  145. */
  146. void findNeighbours(SubGraph &gInputGraph, std::vector<VertexDescriptor> & oContour,
  147. int & iNewVertexIndex, int& p, int& q);
  148. /**
  149. this method shifts the vertices to their right by one or two positions in current layout
  150. in order to accomodate the new vertex to be plotted
  151. * @brief shiftRightProcess
  152. *
  153. * @pre vecAssociated ! = NULL && vecPositon != NULL && (iShiftBy == 1 || iShiftBy == 2)
  154. *
  155. * @param iShiftBy
  156. * by how much you want to shift the vertices, possible values are 1 or 2
  157. *
  158. * @param gInputGraph
  159. * maximal planar graph which is to be layed out.
  160. *
  161. * @param vecPositon
  162. * vector of Positions of all the vertices in input graph
  163. *
  164. * @param vecAssociated
  165. * vector of (vectors of vertices) which are associated to a vertex
  166. *
  167. * @param oContour
  168. * vector of vertex_desciptors that contains contour of graph
  169. *
  170. * @param p
  171. * index in contour of neighbour of new vertex which comes first in contour
  172. *
  173. * @param q
  174. * index in contour of neighbour of new vertex which comes last in contour
  175. *
  176. * @return none
  177. *
  178. * @throw LayoutException
  179. */
  180. void shiftRight(int iShiftBy, SubGraph &gInputGraph, std::vector< Position > & vecPositon,
  181. std::vector< std::vector<VertexDescriptor> > & vecAssociated,
  182. std::vector<VertexDescriptor> & oContour,
  183. int& p, int& q);
  184. /**
  185. this method shifts the vertices to their right in current layout
  186. in order to remove the overlapping caused by the new vertex
  187. * @brief secondShiftRightProcess
  188. *
  189. * @pre vecAssociated ! = NULL && vecPositon != NULL && (iShiftBy == 1 || iShiftBy == 2)
  190. *
  191. * @param iOverlapLeft
  192. * amount by which new vertex overlaps the vertex at its left
  193. *
  194. * @param iOverlapRight
  195. * amount by which new vertex overlaps the vertex at its right
  196. *
  197. * @param gInputGraph
  198. * maximal planar graph which is to be layed out.
  199. *
  200. * @param vecPositon
  201. * vector of Positions of all the vertices in input graph
  202. *
  203. * @param vecAssociated
  204. * vector of (vectors of vertices) which are associated to a vertex
  205. *
  206. * @param oContour
  207. * vector of vertex_desciptors that contains contour of graph
  208. *
  209. * @param p
  210. * index in contour of left neighbour of new vertex
  211. *
  212. * @return none
  213. *
  214. * @throw LayoutException
  215. */
  216. void secondShiftRight(int & iOverlapLeft, int & iOverlapRight, SubGraph &gInputGraph,
  217. std::vector< Position > & vecPositon,
  218. std::vector< std::vector<VertexDescriptor> > & vecAssociated,
  219. std::vector<VertexDescriptor> & oContour,
  220. int& p);
  221. /**
  222. this method finds out position of the new vertex on grid
  223. which is to be added to current layout
  224. * @brief findNewPositionProcess
  225. *
  226. * @pre vNewVertex ! = NULL && vecPositon != NULL
  227. *
  228. * @param vNewVertex
  229. * vertex whose position is to be found out
  230. *
  231. * @param gInputGraph
  232. * maximal planar graph which is to be layed out.
  233. *
  234. * @param p
  235. * index in contour of neighbour of new vertex which comes first in contour
  236. *
  237. * @param q
  238. * index in contour of neighbour of new vertex which comes last in contour
  239. *
  240. * @param vecPositon
  241. * vector of Positions of vertices
  242. *
  243. * @param oContour
  244. * vector of vertex_desciptors that contains contour of graph in previous stage
  245. *
  246. * @return none
  247. *
  248. * @throw none
  249. */
  250. void findNewPosition(int & vNewVertexIntex, int& p, int& q, SubGraph &gInputGraph,
  251. std::vector< Position > & vecPositon,
  252. std::vector<VertexDescriptor> & oContour);
  253. /**
  254. this method updates the contour of the graph after a new vertex is added to the layout
  255. * @brief updateContourProcess
  256. *
  257. * @pre oContour ! = NULL && vNewVertex != NULL
  258. *
  259. * @param oContour
  260. * vector of vertex_desciptors that contains contour of graph in previous stage
  261. *
  262. * @param gInputGraph
  263. * maximal planar graph which is to be layed out.
  264. *
  265. * @param vNewVertex
  266. * vertex which is added to graph
  267. *
  268. * @param p
  269. * index in contour of neighbour of new vertex which comes first in contour
  270. *
  271. * @param q
  272. * index in contour of neighbour of new vertex which comes last in contour
  273. *
  274. * @return none
  275. *
  276. * @throw none
  277. */
  278. void updateContour(std::vector<VertexDescriptor> & oContour, SubGraph & gInputGraph,
  279. int & iNewVertexIndex,
  280. int& p, int& q);
  281. /**
  282. this method updates list of vertices associated with a vertex just added to the layout
  283. * @brief updateAssociaedVerticesProcess
  284. *
  285. * @pre vecAssociated ! = NULL && oContour != NULL && vNewVertex != NULL
  286. *
  287. * @param vecAssociated
  288. *
  289. * @param gInputGraph
  290. * maximal planar graph which is to be layed out.
  291. *
  292. * @param oContour
  293. * vector of vertex_desciptors that contains contour of graph in previous stage
  294. *
  295. * @param vNewVertex
  296. * vertex which is added to graph
  297. *
  298. * @param p
  299. * index in contour of neighbour of new vertex which comes first in contour
  300. *
  301. * @param q
  302. * index in contour of neighbour of new vertex which comes last in contour
  303. *
  304. * @return none
  305. *
  306. * @throw none
  307. */
  308. void updateAssociaedVertices(std::vector<std::vector<VertexDescriptor> > &vecAssociated, SubGraph &gInputGraph,
  309. std::vector<VertexDescriptor> &oContour,
  310. int &iNewVertexIndex, int& p, int& q);
  311. /**
  312. this method reads the intermediate contour and returns the amount of overlap of
  313. newly added vertex with vertices to its both sides in current countour
  314. * @brief findOverlapsProcess
  315. *
  316. * @param p
  317. * index in contour of neighbour of new vertex which comes first in contour
  318. *
  319. * @param oContour
  320. * vector of vertex_desciptors that contains contour of graph in previous stage
  321. *
  322. * @param gInputGraph
  323. * object of the original graph
  324. *
  325. * @param vecPositon
  326. * vector of Positions of all the vertices in input graph
  327. *
  328. * @param iOverlapLeft
  329. * amount of overlap of new node with the node at left in the contour
  330. *
  331. * @param iOverlapRight
  332. * amount of overlap of new node with the node at right in the contour
  333. *
  334. * @return none
  335. *
  336. * @throw LayoutException
  337. */
  338. void findOverlaps(int & p, std::vector<VertexDescriptor> & oContour,
  339. SubGraph & gInputGraph, std::vector<Position> &vecPositon,
  340. int & iOverlapLeft, int & iOverlapRight);
  341. /**
  342. this method finds the vertex in current contour in the index range (p....q)
  343. which has highest top point and returns its object the the supplied parameter
  344. * @brief findTopmostVertexProcess
  345. *
  346. * @pre oContour ! = NULL
  347. *
  348. * @param p
  349. * index in contour of neighbour of new vertex which comes first in contour
  350. *
  351. * @param q
  352. * index in contour of neighbour of new vertex which comes last in contour
  353. *
  354. * @param oContour
  355. * vector of vertex_desciptors that contains contour of graph in previous stage
  356. *
  357. * @param gInputGraph
  358. * object of the original graph
  359. *
  360. * @param vecPositon
  361. * vector of Positions of all the vertices in input graph
  362. *
  363. * @param iIndexOfTopmostVertex
  364. * index of vertex (in input graph) which has highest top point
  365. *
  366. * @return none
  367. *
  368. * @throw none
  369. */
  370. void findTopmostVertex(int& p, int& q, std::vector<VertexDescriptor> & oContour,
  371. SubGraph & gInputGraph, std::vector<Position> &vecPositon,
  372. int &iIndexOfTopmostVertex);
  373. /**
  374. this method reads the vertex (under newly added vertex) which has highest top point,
  375. finds whether newly added vertex overlaps with it and shifts the new vertex upwards if needed
  376. * @brief shiftVerticallyProcess
  377. *
  378. * @pre oContour ! = NULL
  379. *
  380. * @param iIndexOfTopmostVertex
  381. * index of vertex (in input graph) which has highest top point
  382. *
  383. * @param iNewVertexIndex
  384. * index of vertex (in input graph) which is newly added to graph
  385. *
  386. * @param gInputGraph
  387. * object of the original graph
  388. *
  389. * @param vecPositon
  390. * vector of Positions of all the vertices in input graph
  391. *
  392. * @return none
  393. *
  394. * @throw none
  395. */
  396. void shiftVertically(int &iIndexOfTopmostVertex, int &iNewVertexIndex,
  397. SubGraph & gInputGraph, std::vector<Position> &vecPositon);
  398. };
  399. #endif // GRIDBASEDLAYOUT_H