SizeManager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "SizeManager.h"
  2. SizeManager::SizeManager()
  3. {
  4. }
  5. double SizeManager::calculateAreaOfNode(VertexDescriptor vVertex, SubGraph &gSubgraph)
  6. {
  7. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  8. // "Calculate area of node",
  9. // "Trying to calculate area of node without asigning graph to method");
  10. // Get properties from vertex
  11. int iHeight = m_boostGraphWrapper.getVertexHeight(vVertex, gSubgraph);
  12. int iWidth = m_boostGraphWrapper.getVertexWidth(vVertex, gSubgraph);
  13. // Calculate Area
  14. double dNodeArea = ((iHeight * iWidth) / 2);
  15. return dNodeArea;
  16. }
  17. double SizeManager::calculateAreaOfCluster(SubGraph &gSubgraph)
  18. {
  19. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  20. // "Calculate area of cluster",
  21. // "Trying to calculate area of cluster without asigning graph to method");
  22. /* To calculate the area of rectangle
  23. area of rectagle = (iHeight * iWidth) / 2.
  24. */
  25. int iHeight = m_boostGraphWrapper.getGraphHeight(gSubgraph);
  26. int iWidth = m_boostGraphWrapper.getGraphWidth(gSubgraph);
  27. double dClusterArea = ((iHeight * iWidth) / 2);
  28. return dClusterArea;
  29. }
  30. int SizeManager::calculateNodeLeftXFromCenterX(VertexDescriptor vVertex, SubGraph &gSubgraph)
  31. {
  32. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  33. // "Calculate left x coordinate from center x",
  34. // "Trying to calculate left x coordinate of node without asigning graph to method");
  35. /* To calculate the node rectangle's left X coordinate
  36. Caculate center X coordinate and
  37. subtract the width/2 factor
  38. */
  39. int iCenterCoordX = m_boostGraphWrapper.getVertexCenterCoordX(vVertex, gSubgraph);
  40. int iWidth = m_boostGraphWrapper.getVertexWidth(vVertex, gSubgraph);
  41. int iWidthShareCoordX = iWidth / 2;
  42. int iLeftCoordX = iCenterCoordX - iWidthShareCoordX;
  43. return iLeftCoordX;
  44. }
  45. int SizeManager::calculateNodeTopYFromCenterY(VertexDescriptor vVertex, SubGraph &gSubgraph)
  46. {
  47. /* To calculate the node rectangle's top Y coordinate
  48. Calculate center Y coordinate and
  49. subtract the height/2 factor
  50. */
  51. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  52. // "Calculate top y coordinate from center y",
  53. // "Trying to calculate top y coordinate of node without asigning graph to method");
  54. int iCenterCoordY = m_boostGraphWrapper.getVertexCenterCoordY(vVertex, gSubgraph);
  55. int iHeight = m_boostGraphWrapper.getVertexHeight(vVertex, gSubgraph);
  56. int iHeightShareCoordY = iHeight / 2;
  57. int iTopCoordY = iCenterCoordY - iHeightShareCoordY;
  58. return iTopCoordY;
  59. }
  60. double SizeManager::calculateDiagonalOfNode(VertexDescriptor& vVertex, SubGraph& gSubgraph)
  61. {
  62. /**
  63. This function first gets the vertex height and width and then
  64. calculates the diagonal as per the pythagoras formula
  65. */
  66. // XXX obselete LAYOUT_ASSERT(&gSubgraph != NULL,
  67. // LayoutMemoryException(__FUNCTION__,
  68. // LayoutExceptionEnum::NULL_POINTER_EXCEPTION,
  69. // GRAPH));
  70. int iHeight;
  71. int iWidth;
  72. try
  73. {
  74. LayoutEnum::NodeType nodeType = m_boostGraphWrapper.getVertexType(vVertex, gSubgraph);
  75. // consider height and width for dummy node as per its contents in terms of number of nodes
  76. if(nodeType == LayoutEnum::DummyNode)
  77. {
  78. // get the subgraph vertices count related with this dummy node
  79. VertexDescriptor vGlobalVertex = (gSubgraph).local_to_global(vVertex);
  80. int iNumVerticesInDummyNode = (gSubgraph.root())[vGlobalVertex].iVertexCount;
  81. iHeight = m_boostGraphWrapper.getVertexHeight(vVertex, gSubgraph);
  82. iHeight = iHeight + ((iNumVerticesInDummyNode * (int)(2 * PI * RADIUS_SHARE))) + (2*RADIUS_SHARE);
  83. iWidth = m_boostGraphWrapper.getVertexWidth(vVertex, gSubgraph);
  84. iWidth = iWidth + ((iNumVerticesInDummyNode * (int)(2 * PI * RADIUS_SHARE))) +( 2*RADIUS_SHARE);
  85. }
  86. else
  87. {
  88. iHeight = m_boostGraphWrapper.getVertexHeight(vVertex, gSubgraph);
  89. iWidth = m_boostGraphWrapper.getVertexWidth(vVertex, gSubgraph);
  90. }
  91. }
  92. catch(boost::exception& eBoostException)
  93. {
  94. throw *boost::get_error_info<errmsg_info>(eBoostException);
  95. }
  96. double dNodeDiagonalSquare = (double)((iHeight * iHeight) + (iWidth * iWidth));
  97. double dNodeDiagonalLength = (double)qSqrt(dNodeDiagonalSquare);
  98. return dNodeDiagonalLength;
  99. }
  100. int SizeManager::calculateGraphLeftXFromCenterX(SubGraph &gSubgraph)
  101. {
  102. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  103. // "Calculate left x coordinate from center x of cluster",
  104. // "Trying to calculate left x coordinate of cluster without asigning graph to method");
  105. // get the center x coordinate for this cluster
  106. int iGraphCentroidCoordX = calculateGraphCenterCoordX(gSubgraph);
  107. // get the width for this cluster
  108. int iWidth = m_boostGraphWrapper.getGraphWidth(gSubgraph);
  109. int iWidthShareCoordX = iWidth / 2;
  110. // deduct half length of width
  111. int iGraphLeftCoordX = iGraphCentroidCoordX - iWidthShareCoordX;
  112. return iGraphLeftCoordX;
  113. }
  114. int SizeManager::calculateGraphTopYFromCenterY(SubGraph &gSubgraph)
  115. {
  116. // XXX obselte Q_ASSERT_X(&gSubgraph != NULL,
  117. // "Calculate top y coordinate from center y of cluster",
  118. // "Trying to calculate top y coordinate of cluster without asigning graph to method");
  119. // get the center y coordinate from this cluster
  120. int iGraphCentroidCoordY = calculateGraphCenterCoordY(gSubgraph);
  121. // get the height for this cluster
  122. int iHeight = m_boostGraphWrapper.getGraphHeight(gSubgraph);
  123. int iHeightShareCoordY = iHeight / 2;
  124. // deduct half length of height
  125. int iGraphTopCoordY = iGraphCentroidCoordY - iHeightShareCoordY;
  126. return iGraphTopCoordY;
  127. }
  128. int SizeManager::calculateGraphWidth(SubGraph &gSubgraph)
  129. {
  130. // XXX obselete LAYOUT_ASSERT(&gSubgraph != NULL,
  131. // LayoutMemoryException(__FUNCTION__,
  132. // LayoutExceptionEnum::NULL_POINTER_EXCEPTION,
  133. // GRAPH));
  134. int iNodeMaximumWidth = 0;
  135. int iVertexCount = num_vertices(gSubgraph);
  136. // calculate the maximum node width amoung all the nodes
  137. BGL_FORALL_VERTICES(vVertex, gSubgraph, SubGraph)
  138. {
  139. LayoutEnum::NodeType nodeType = m_boostGraphWrapper.getVertexType(vVertex,gSubgraph);
  140. if(LayoutEnum::isValidCircularVertexType(nodeType) == true)
  141. {
  142. if(nodeType == LayoutEnum::DummyNode)
  143. {
  144. // Skip Dummy Nodes
  145. }
  146. else
  147. {
  148. int iLocalWidth = m_boostGraphWrapper.getVertexWidth(vVertex, gSubgraph);
  149. if(iLocalWidth > 0)
  150. {
  151. if(iNodeMaximumWidth < iLocalWidth)
  152. {
  153. iNodeMaximumWidth = iLocalWidth;
  154. }
  155. }
  156. else
  157. {
  158. //cout<<"Exception : Invalid Height value to vertex in calculateGraphWidth()"<<endl;
  159. throw LayoutException(__FUNCTION__, LayoutExceptionEnum::INVALID_ATTRIBUTE_VALUE, WIDTH_VALUE, VERTEX);
  160. }
  161. }
  162. }
  163. else
  164. {
  165. //cout<<"Invalid Node Type in circular layout for calculating graph width"<<endl;
  166. throw LayoutException(__FUNCTION__, LayoutExceptionEnum::INVALID_TYPE, INVALID_NODE_TYPE);
  167. }
  168. }
  169. // calculate the width increased due to radius share
  170. int iSpaceError = iVertexCount * RADIUS_SHARE;
  171. // //cout<<"Graph Width : "<<iNodeMaximumWidth<<" ";
  172. // //cout<<"Graph Space : "<<iSpaceError<<" ";
  173. // //cout<<"Total Width : "<<(iNodeMaximumWidth + iSpaceError)<<endl;
  174. int iSpaceWidth = iNodeMaximumWidth + iSpaceError;
  175. // for cluster inside the main graph
  176. double dClusterRadius;
  177. try
  178. {
  179. dClusterRadius = calculateGraphMaxRadiusUsingDFS(gSubgraph);
  180. }
  181. catch(...)
  182. {throw;}
  183. // calculate the total width for the cluster
  184. int iWidth = iSpaceWidth + ((int)dClusterRadius * 2);
  185. // //cout<<"Graph Width : "<<iWidth<<endl;
  186. return iWidth;
  187. }
  188. int SizeManager::calculateGraphHeight(SubGraph &gSubgraph)
  189. {
  190. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  191. // "Calculate height of cluster",
  192. // "Trying to calculate height of cluster without asigning graph to method");
  193. int iGraphHeight = 0;
  194. int iVertexCount = num_vertices(gSubgraph);
  195. // calculate the maximum node height amoung all the nodes
  196. BGL_FORALL_VERTICES(vVertex, gSubgraph, SubGraph)
  197. {
  198. LayoutEnum::NodeType nodeType = m_boostGraphWrapper.getVertexType(vVertex,gSubgraph);
  199. if(LayoutEnum::isValidCircularVertexType(nodeType) == true)
  200. {
  201. if(nodeType == LayoutEnum::DummyNode)
  202. {
  203. // Skip Dummy Nodes
  204. }
  205. else
  206. {
  207. int iHeight;
  208. try
  209. {
  210. iHeight = m_boostGraphWrapper.getVertexHeight(vVertex, gSubgraph);
  211. }
  212. catch(boost::exception& eBoostException)
  213. {
  214. throw *boost::get_error_info<errmsg_info>(eBoostException);
  215. }
  216. if(iHeight > 0)
  217. {
  218. if(iGraphHeight < iHeight)
  219. {
  220. iGraphHeight = iHeight;
  221. }
  222. }
  223. else
  224. {
  225. //cout<<"Exception : Invalid Height value to vertex in calculateGraphHeight()"<<endl;
  226. throw LayoutException(__FUNCTION__, LayoutExceptionEnum::INVALID_ATTRIBUTE_VALUE, HEIGHT_VALUE, VERTEX);
  227. }
  228. }
  229. }
  230. else
  231. {
  232. //cout<<"Invalid node type for the circular layout in calculating the graph height"<<endl;
  233. throw LayoutException(__FUNCTION__, LayoutExceptionEnum::INVALID_TYPE, INVALID_NODE_TYPE);
  234. }
  235. }
  236. // calculate the height increased due to radius share
  237. int iSpaceError = iVertexCount * RADIUS_SHARE;
  238. // //cout<<"Graph Height : "<<iGraphHeight<<" ";
  239. // //cout<<"Graph Space : "<<iSpaceError<<" ";
  240. // //cout<<"Total Height : "<<(iGraphHeight + iSpaceError)<<endl;
  241. int iSpaceHeight = iGraphHeight + iSpaceError;
  242. //for cluster inside the main graph
  243. double dClusterRadius=0.0;
  244. try
  245. {
  246. dClusterRadius = calculateGraphMaxRadiusUsingDFS(gSubgraph);
  247. }
  248. catch(LayoutMemoryException& eException)
  249. {
  250. throw LayoutMemoryException(__FUNCTION__,
  251. LayoutExceptionEnum::NULL_POINTER_EXCEPTION,
  252. eException.getObjectName());
  253. }
  254. catch(LayoutException& eException)
  255. {
  256. throw LayoutException(__FUNCTION__,
  257. LayoutExceptionEnum::INVALID_PARAMETER,
  258. eException.getEntityValue());
  259. }
  260. catch(boost::exception& eBoostException)
  261. {
  262. throw *boost::get_error_info<errmsg_info>(eBoostException);
  263. }
  264. catch(...)
  265. { }
  266. // calculate the total height for the cluster
  267. int iHeight = iSpaceHeight + ((int)dClusterRadius * 2);
  268. // //cout<<"Graph Height : "<<iHeight<<endl;
  269. return iHeight;
  270. }
  271. int SizeManager::calculateGraphCenterCoordX(SubGraph &gSubgraph)
  272. {
  273. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  274. // "Calculate center x of cluster",
  275. // "Trying to calculate center x of cluster without asigning graph to method");
  276. int iVertexCount = 0;
  277. int iTotalCoordX = 0;
  278. // caculate the total value by adding the values of all x-coordinate of this cluster
  279. BGL_FORALL_VERTICES(vVertex, gSubgraph, SubGraph)
  280. {
  281. LayoutEnum::NodeType nodeType = m_boostGraphWrapper.getVertexType(vVertex,gSubgraph);
  282. if(LayoutEnum::isValidCircularVertexType(nodeType) == true)
  283. {
  284. if(nodeType == LayoutEnum::DummyNode)
  285. {
  286. // Skip Dummy Nodes
  287. }
  288. else
  289. {
  290. int iVertexCenterX = m_boostGraphWrapper.getVertexCenterCoordX(vVertex, gSubgraph);
  291. iTotalCoordX += iVertexCenterX;
  292. iVertexCount++;
  293. }
  294. }
  295. else
  296. {
  297. //cout<<"Invalid node type in circular layout for calculating the center coord X of graph"<<endl;
  298. }
  299. }
  300. int iGraphCentroidCoordX = 0;
  301. // calculate the centroid of this cluster
  302. if (iVertexCount) {
  303. iGraphCentroidCoordX = iTotalCoordX / iVertexCount;
  304. } else {
  305. iGraphCentroidCoordX =0;
  306. }
  307. // //cout<<"Graph Center X : "<<iGraphCentroidCoordX<<endl;
  308. /*
  309. *Calculate displacement between centroidX and center X
  310. of the graph to be used by the second pass of the circular layout
  311. */
  312. int iGraphCenterCoordX = m_boostGraphWrapper.getGraphCenterCoordX(gSubgraph);
  313. int iDistanceOfCenterFromCentroidInGraphX = iGraphCentroidCoordX - iGraphCenterCoordX;
  314. // std:://cout<<"Distance X : "<<iDistanceOfCenterFromCentroidInGraphX<<std::endl;
  315. m_boostGraphWrapper.setGraphDistanceBetweenCentoidAndCenterCoordX(iDistanceOfCenterFromCentroidInGraphX, gSubgraph);
  316. return iGraphCentroidCoordX;
  317. }
  318. int SizeManager::calculateGraphCenterCoordY(SubGraph &gSubgraph)
  319. {
  320. // XXX obselete Q_ASSERT_X(&gSubgraph != NULL,
  321. // "Calculate center y of cluster",
  322. // "Trying to calculate center y of cluster without asigning graph to method");
  323. int iVertexCount = 0;
  324. int iTotalCoordY = 0;
  325. // caculate the total value by adding the values of all y-coordinate of this cluster
  326. BGL_FORALL_VERTICES(vVertex, gSubgraph, SubGraph)
  327. {
  328. LayoutEnum::NodeType nodeType = m_boostGraphWrapper.getVertexType(vVertex,gSubgraph);
  329. if(nodeType == LayoutEnum::DummyNode)
  330. {
  331. // Skip Dummy Nodes
  332. }
  333. else
  334. {
  335. int iVertexCenterY = m_boostGraphWrapper.getVertexCenterCoordY(vVertex, gSubgraph);
  336. iTotalCoordY += iVertexCenterY;
  337. iVertexCount++;
  338. }
  339. }
  340. // calculate the centroid of this cluster
  341. int iGraphCentroidCoordY = 0;
  342. if (iVertexCount) {
  343. iGraphCentroidCoordY = iTotalCoordY / iVertexCount;
  344. } else {
  345. iGraphCentroidCoordY = 0;
  346. }
  347. // //cout<<"Graph Center Y : "<<iGraphCentroidCoordY<<endl;
  348. /*
  349. *Calculate displacement between centroidY and center Y
  350. of the graph to be used by the second pass of the circular layout
  351. */
  352. int iGraphCenterCoordY = m_boostGraphWrapper.getGraphCenterCoordY(gSubgraph);
  353. int iDistanceOfCenterFromCentroidInGraphY = iGraphCentroidCoordY - iGraphCenterCoordY;
  354. // std:://cout<<"Distance Y : "<<iDistanceOfCenterFromCentroidInGraphY<<std::endl;
  355. m_boostGraphWrapper.setGraphDistanceBetweenCentoidAndCenterCoordY(iDistanceOfCenterFromCentroidInGraphY, gSubgraph);
  356. return iGraphCentroidCoordY;
  357. }
  358. void SizeManager::processSizeManager(SubGraph &gSubgraph)
  359. {
  360. // Q_ASSERT_X(&gSubgraph != NULL,
  361. // "process size manager in DFS way",
  362. // "Trying to process subgraphs in dfs without asigning graph to method");
  363. // XXX obselete LAYOUT_ASSERT(&gSubgraph != NULL,
  364. // LayoutMemoryException(__FUNCTION__,
  365. // LayoutExceptionEnum::NULL_POINTER_EXCEPTION,
  366. // GRAPH));
  367. // Haldling for negative or invalid height value
  368. int iHeight;
  369. try
  370. {
  371. //calculate height
  372. iHeight = calculateGraphHeight(gSubgraph);
  373. // set the width in the graph property of this cluster
  374. m_boostGraphWrapper.setGraphHeight(iHeight, gSubgraph);
  375. }
  376. catch(...)
  377. {throw;}
  378. try
  379. {
  380. // calculate width
  381. int iWidth = calculateGraphWidth(gSubgraph);
  382. // set the width in the graph property of this cluster
  383. m_boostGraphWrapper.setGraphWidth(iWidth, gSubgraph);
  384. }
  385. catch(...)
  386. {throw;}
  387. // calculate graph's left x and top y coordinates
  388. int iGraphLeftCoordX = calculateGraphLeftXFromCenterX(gSubgraph);
  389. m_boostGraphWrapper.setGraphLeftTopCoordX(iGraphLeftCoordX,gSubgraph);
  390. int iGraphTopCoordY = calculateGraphTopYFromCenterY(gSubgraph);
  391. m_boostGraphWrapper.setGraphLeftTopCoordY(iGraphTopCoordY, gSubgraph);
  392. // iterate child graphs recursively
  393. ChildrenIterator itrGraph, itrGraphEnd;
  394. for(boost::tie(itrGraph, itrGraphEnd) = gSubgraph.children();
  395. itrGraph != itrGraphEnd;
  396. ++itrGraph)
  397. {
  398. processSizeManager(*itrGraph);
  399. }
  400. }
  401. double SizeManager::calculateGraphMaxRadiusUsingDFS(SubGraph &gSubgraph)
  402. {
  403. /*
  404. This function will calculate the radius required for the graph considering its
  405. all deep level subgraphs and passes the maximum required radius to upper graph.
  406. */
  407. // XXX obselete LAYOUT_ASSERT(&gSubgraph != NULL,
  408. // LayoutMemoryException(__FUNCTION__,
  409. // LayoutExceptionEnum::NULL_POINTER_EXCEPTION,
  410. // GRAPH));
  411. double dMaxChildRadius = 0;
  412. // get the own real radius for every subgraph at each recursive call
  413. double dGrphOwnRadius;
  414. try{
  415. dGrphOwnRadius = m_boostGraphWrapper.getGraphRadius(gSubgraph);
  416. }catch(boost::exception& eBoostException)
  417. {
  418. throw *boost::get_error_info<errmsg_info>(eBoostException);
  419. }
  420. ChildrenIterator itrSubgraph, itrSubgraphEnd;
  421. for(boost::tie(itrSubgraph, itrSubgraphEnd) = gSubgraph.children();
  422. itrSubgraph != itrSubgraphEnd;
  423. ++itrSubgraph)
  424. {
  425. // traverse upto deep level in the subgraph tree
  426. double dCurrentGraphRadius;
  427. dCurrentGraphRadius = calculateGraphMaxRadiusUsingDFS(*itrSubgraph);
  428. if(dCurrentGraphRadius > dMaxChildRadius)
  429. {
  430. dMaxChildRadius = dCurrentGraphRadius;
  431. }
  432. }
  433. if(dGrphOwnRadius > 0)
  434. {
  435. // add lower level childs radius to own radius
  436. dGrphOwnRadius += dMaxChildRadius;
  437. // XXX value never read? true. see below
  438. }
  439. else
  440. {
  441. throw LayoutException(__FUNCTION__, LayoutExceptionEnum::INVALID_PARAMETER, INVALID_RADIUS_VALUE);
  442. }
  443. // Update graph's own radius with deep level max radius
  444. double dRadius=0.0;
  445. try
  446. {
  447. dRadius = m_boostGraphWrapper.getGraphRadius(gSubgraph);
  448. }
  449. catch(boost::exception& eBoostException)
  450. {
  451. throw *boost::get_error_info<errmsg_info>(eBoostException);
  452. }
  453. if(dRadius > 0)
  454. {
  455. dGrphOwnRadius = dRadius + dMaxChildRadius;
  456. }
  457. else
  458. {
  459. throw LayoutException(__FUNCTION__, LayoutExceptionEnum::INVALID_PARAMETER, INVALID_RADIUS_VALUE);
  460. }
  461. // //cout<<"Total Radius : "<<dGrphOwnRadius<<" "<<dRadius<<endl;
  462. return dGrphOwnRadius;
  463. }