Reingold.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. #include "Reingold.h"
  2. Reingold::Reingold()
  3. {
  4. }
  5. void Reingold :: ApplyLocalFR(SubGraph &l_graph)
  6. {
  7. // setCompartMentProps(l_graph);
  8. int clustercounter =0;
  9. int iTotalClusters =0;
  10. QQueue<SubGraph*> qSubgraphs; // a queue of subgraphs
  11. qSubgraphs.enqueue(&l_graph);
  12. SubGraph* gSubgraph1 = NULL;
  13. while(qSubgraphs.isEmpty() == false) // Iterating subgraphs till they exist
  14. {
  15. gSubgraph1 = qSubgraphs.dequeue();
  16. iTotalClusters++;
  17. ChildrenIterator iterChild , iterChildEnd;
  18. for(boost::tie(iterChild , iterChildEnd) = gSubgraph1->children();
  19. iterChild != iterChildEnd;
  20. iterChild++)
  21. {
  22. clustercounter++;
  23. SubGraph* gChildGraph = &(*iterChild);
  24. qSubgraphs.enqueue(gChildGraph);
  25. SpringEmbedder *spe = new SpringEmbedder();
  26. spe->SpringEmbedderStep(*gChildGraph);
  27. }
  28. }
  29. if(clustercounter ==0)
  30. {
  31. // ApplyReingold(l_graph);
  32. }
  33. // uncomment it later
  34. }
  35. void Reingold :: ApplyReingold(SubGraph &l_graph)
  36. {
  37. // Actual working FR algo ....
  38. cout<<"Before Wrapper";
  39. BoostGraphWrapper b_gwrapper;
  40. int m_igHeight = b_gwrapper.getGraphHeight(l_graph);
  41. int m_igWidth = b_gwrapper.getGraphWidth(l_graph);
  42. cout<<"\nHeight "<<m_igHeight;
  43. cout<<"\nWidth "<<m_igWidth;
  44. int m_igArea = m_igHeight*m_igWidth;
  45. int numVert = b_gwrapper.numVertices(l_graph);
  46. int inter = m_igArea/numVert;
  47. double temperature = m_igWidth/10;
  48. iK = sqrt(inter)*0.75 ; // constant default is 0.75
  49. int iterations = 200; // iterations = 50
  50. int counter = 0;
  51. double m_iXcordaffected=0.0 ;
  52. double m_iYcordaffected=0.0 ;
  53. double m_iXcordvisit=0.0 ;
  54. double m_iYcordvisit=0.0 ;
  55. double m_iDist=0.0;
  56. double iXdisp =0.0;
  57. double iYdisp =0.0;
  58. double iTotalXDisp=0.0 ;
  59. double iTotalYDisp=0.0 ;
  60. double iMinX = 0.0;
  61. double iMinY = 0.0;
  62. double iMaxX =0.0;
  63. double iMaxY =0.0;
  64. iMinX = b_gwrapper.getGraphLeftTopCoordX(l_graph);
  65. iMinY = b_gwrapper.getGraphLeftTopCoordY(l_graph);
  66. iMaxX = iMinX + m_igWidth;
  67. iMaxY = iMinY + m_igHeight;
  68. int maxDimension = std::max(m_igWidth,m_igHeight); // set max later on
  69. VertexIterPair verIter,verIterEnd;
  70. VertexDescriptor verAffected,verVisitor;
  71. EdgeBoolPair edgebool;
  72. while( temperature != 1/maxDimension || iterations != 0 )
  73. {
  74. for(tie(verIter.first,verIter.second)=vertices(l_graph);verIter.first!=verIter.second;++verIter.first)
  75. {
  76. verAffected = *verIter.first;
  77. b_gwrapper.setVertXDisp(0,verAffected,l_graph);
  78. b_gwrapper.setVertYDisp(0,verAffected,l_graph);
  79. for(tie(verIterEnd.first,verIterEnd.second)=vertices(l_graph);
  80. verIterEnd.first != verIterEnd.second;++verIterEnd.first)
  81. {
  82. // to check loop running right or not
  83. verVisitor = *verIterEnd.first;
  84. counter++;
  85. if(verAffected==verVisitor)
  86. {
  87. // Do nothing
  88. }
  89. else
  90. {
  91. cout<<"\n*******************New Iteration *******************";
  92. cout<<"\nInside Loop";
  93. m_iXcordaffected = l_graph[verAffected].iCoord_X;
  94. m_iYcordaffected = l_graph[verAffected].iCoord_Y;
  95. cout<<"\nX Cordinate : "<<m_iXcordaffected;
  96. cout<<"\nY Cordinate : "<<m_iYcordaffected;
  97. m_iXcordvisit = l_graph[verVisitor].iCoord_X;
  98. m_iYcordvisit = l_graph[verVisitor].iCoord_Y;
  99. iXdisp = m_iXcordaffected - m_iXcordvisit;
  100. iYdisp = m_iYcordaffected - m_iYcordvisit;
  101. m_iDist = fabs(sqrt((iXdisp*iXdisp)
  102. +(iYdisp*iYdisp)));
  103. cout<<"\nDistance : "<<m_iDist;
  104. iTotalXDisp = b_gwrapper.getVertXDisp(verAffected,l_graph);
  105. iTotalYDisp = b_gwrapper.getVertYDisp(verAffected,l_graph);
  106. cout<<"\nTotal Disp x : "<<iTotalXDisp;
  107. cout<<"\nTotal Disp y : "<<iTotalYDisp;
  108. if(!b_gwrapper.getIsVertexLocked(verAffected,l_graph))
  109. {
  110. iTotalXDisp = iTotalXDisp + (iXdisp/fabs(iXdisp))*getRepulsiveForce(m_iDist);
  111. iTotalYDisp = iTotalYDisp + (iYdisp/fabs(iYdisp))*getRepulsiveForce(m_iDist);
  112. }
  113. edgebool= boost::edge(verAffected,verVisitor,l_graph);
  114. if(edgebool.second==1)
  115. {
  116. if(!b_gwrapper.getIsVertexLocked(verAffected,l_graph))
  117. {
  118. iTotalXDisp = iTotalXDisp - (iXdisp/fabs(iXdisp))*getAttractiveForce(m_iDist);
  119. iTotalYDisp = iTotalYDisp - (iYdisp/fabs(iYdisp))*getAttractiveForce(m_iDist);
  120. }
  121. }
  122. if(!b_gwrapper.getIsVertexLocked(verAffected,l_graph))
  123. {
  124. m_iXcordaffected = m_iXcordaffected + (iTotalXDisp/fabs(iTotalXDisp))*std::min(fabs((iTotalXDisp)),temperature);
  125. m_iYcordaffected = m_iYcordaffected + (iTotalYDisp/fabs(iTotalYDisp))*std::min(fabs((iTotalYDisp)),temperature);
  126. }
  127. b_gwrapper.setVertXDisp(iTotalXDisp,verAffected,l_graph);
  128. b_gwrapper.setVertYDisp(iTotalYDisp,verAffected,l_graph);
  129. cout<<"\nIteration No : "<<counter;
  130. cout<<"\nAttractive Force : "<<getAttractiveForce(m_iDist);
  131. cout<<"\nRepulsive Force : "<<getRepulsiveForce(m_iDist);
  132. }
  133. }
  134. if(m_iXcordaffected > iMaxX || m_iXcordaffected < iMinX ||
  135. m_iYcordaffected >iMaxY || m_iYcordaffected < iMinY)
  136. {
  137. // set vertex lock here
  138. // bVertexLock = true;
  139. b_gwrapper.setVertexLock(verAffected,l_graph);
  140. if(m_iXcordaffected > iMaxX || m_iXcordaffected < iMinX )
  141. {
  142. if(m_iXcordaffected < iMinX)
  143. {
  144. while(m_iXcordaffected < iMinX)
  145. {
  146. m_iXcordaffected = m_iXcordaffected +10 ; // use std rand instead 75
  147. }
  148. }
  149. if(m_iXcordaffected > iMaxX)
  150. {
  151. while(m_iXcordaffected > iMaxX)
  152. {
  153. m_iXcordaffected = (iMaxX - 15); // use std rand instead 75
  154. }
  155. }
  156. }
  157. if(m_iYcordaffected > iMaxY || m_iYcordaffected < iMinY)
  158. {
  159. /// XXX may be unitialized
  160. if(m_iYcordaffected > iMaxY)
  161. {
  162. while(m_iYcordaffected > iMaxY )
  163. {
  164. m_iYcordaffected = (m_iYcordaffected - 15);
  165. }
  166. }
  167. if(m_iYcordaffected<iMinY)
  168. {
  169. while(m_iYcordaffected < iMinY )
  170. {
  171. m_iYcordaffected = (m_iYcordaffected + 10);
  172. }
  173. }
  174. }
  175. }
  176. b_gwrapper.setVertexCenterCoordX(verAffected,l_graph,(int)m_iXcordaffected);
  177. b_gwrapper.setVertexCenterCoordY(verAffected,l_graph,(int)m_iYcordaffected);
  178. temperature = temperature*(1-counter/iterations);
  179. }
  180. iterations = iterations - 1;
  181. }
  182. cout<<"Loop Running : "<<counter;
  183. }
  184. float Reingold:: getAttractiveForce(double idist)
  185. {
  186. float attra = (idist*idist)/iK;
  187. return attra;
  188. }
  189. float Reingold :: getRepulsiveForce(double idist)
  190. {
  191. float repul = (iK*iK)/idist;
  192. return repul;
  193. }
  194. int Reingold::setCompartMentProps(SubGraph &l_graph, int iMargine)
  195. {
  196. LAYOUT_ASSERT(iMargine > 0,
  197. LayoutException("setCompartMentProps",
  198. LayoutExceptionEnum::INVALID_PARAMETER,
  199. "iMargine",
  200. " "));
  201. BoostGraphWrapper bGraphWrap;
  202. int iMaxMargine = 10; //default margine; used by innermost cluster
  203. ChildrenIterator itrChild, itrEndChild;
  204. //set children's props and find max margine among the children---------------------------------------------
  205. //for each child of current graph
  206. for(boost::tie(itrChild, itrEndChild) = l_graph.children(); itrChild != itrEndChild; ++itrChild)
  207. {
  208. //recursive call to set child's properties
  209. int iCurrentMargine = this->setCompartMentProps(*itrChild, iMargine);
  210. //update maximum margine
  211. if(iCurrentMargine > iMaxMargine)
  212. {
  213. iMaxMargine = iCurrentMargine;
  214. }
  215. }
  216. //---------------------------------------------------------------------------------------------------------
  217. //set current graph's props--------------------------------------------------------------------------------
  218. int iXMin = 2147483647; //x-coordinate of leftmost node
  219. int iYMin = 2147483647; //y-coordinate of topmost node
  220. int iXMax = -2147483648;//x-coordinate of rightmost node
  221. int iYMax = -2147483648;//y-coordinate of bottommost node
  222. int iHeightOfTopmost = 0;
  223. int iHeightOfLowermost = 0;
  224. int iWidthOfLeftmost = 0;
  225. int iWidthOfRightmost = 0;
  226. //for each vertex, find if it is leftmost/rightmost/topmost/lowermost--------------------------------------
  227. VertexIterPair verIter;
  228. for(tie(verIter.first,verIter.second) = vertices(l_graph); verIter.first!=verIter.second; ++verIter.first)
  229. {
  230. VertexDescriptor verDis = *verIter.first;
  231. int iXval = bGraphWrap.getVertexCenterCoordX(verDis, l_graph);
  232. int iYval = bGraphWrap.getVertexCenterCoordY(verDis, l_graph);
  233. int iVertexHeight = bGraphWrap.getVertexHeight(verDis, l_graph) ;
  234. int iVertexWidth = bGraphWrap.getVertexWidth(verDis, l_graph);
  235. if(iXval <= iXMin) //if it is leftmost node
  236. {
  237. iXMin = iXval;
  238. if(iVertexWidth > iWidthOfLeftmost)
  239. iWidthOfLeftmost = iVertexWidth;
  240. }
  241. if(iXval >= iXMax) //if it is rightmost node
  242. {
  243. iXMax = iXval;
  244. if(iVertexWidth > iWidthOfRightmost)
  245. iWidthOfRightmost = iVertexWidth;
  246. }
  247. if(iYval <= iYMin) //if it is lowermost node
  248. {
  249. iYMin = iYval;
  250. if(iVertexHeight > iHeightOfLowermost)
  251. iHeightOfLowermost = iVertexHeight;
  252. }
  253. if(iYval >= iYMax) //if it is topmost node
  254. {
  255. iYMax = iYval;
  256. if(iVertexHeight > iHeightOfTopmost)
  257. iHeightOfTopmost = iVertexHeight;
  258. }
  259. }
  260. LAYOUT_ASSERT((iXMax>=iXMin)&(iYMax>=iYMin),
  261. LayoutException("setCompartMentProps",
  262. LayoutExceptionEnum::INVALID_ATTRIBUTE_VALUE,
  263. "min/max x-y coordinates",
  264. "vertices"));
  265. //---------------------------------------------------------------------------------------------------------
  266. //set properties-------------------------------------------------------------------------------------------
  267. int iGraphWidth = std::abs(iXMin - iXMax);
  268. int iGraphHeight = std::abs(iYMin - iYMax);
  269. int iLeftMargin = (iWidthOfLeftmost/2) + iMaxMargine;
  270. int iRightMargin = (iWidthOfRightmost/2) + iMaxMargine;
  271. int iTopMargin = (iHeightOfTopmost/2) + iMaxMargine;
  272. int iLowerMargin = (iHeightOfLowermost/2) + iMaxMargine;
  273. iGraphWidth = iGraphWidth + iLeftMargin + iRightMargin;
  274. iGraphHeight = iGraphHeight + iTopMargin + iLowerMargin;
  275. LAYOUT_ASSERT((iGraphWidth>0)&(iGraphHeight>0),
  276. LayoutException("setCompartMentProps",
  277. LayoutExceptionEnum::INVALID_ATTRIBUTE_VALUE,
  278. "width/height",
  279. "vertices"));
  280. bGraphWrap.setGraphLeftTopCoordX(iXMin - iLeftMargin, l_graph);
  281. bGraphWrap.setGraphLeftTopCoordY(iYMin - iLowerMargin, l_graph);
  282. bGraphWrap.setGraphHeight(iGraphHeight, l_graph);
  283. bGraphWrap.setGraphWidth(iGraphWidth, l_graph);
  284. //---------------------------------------------------------------------------------------------------------
  285. // cout<<"iXMin: "<<iXMin<< "\n";
  286. // cout<<"iYMin: "<<iYMin<< "\n";
  287. // cout<<"iXMax: "<<iXMax<< "\n";
  288. // cout<<"iYMax: "<<iYMax<< "\n";
  289. // cout<<"iHeightOfTopmost: "<<iHeightOfTopmost<< "\n";
  290. // cout<<"iHeightOfLowermost: "<<iHeightOfLowermost<< "\n";
  291. // cout<<"iWidthOfLeftmost: "<<iWidthOfLeftmost<< "\n";
  292. // cout<<"iWidthOfRightmost: "<<iWidthOfRightmost<< "\n";
  293. // cout<<"iLeftMargin: "<<iLeftMargin<< "\n";
  294. // cout<<"iRightMargin: "<<iRightMargin<< "\n";
  295. // cout<<"iTopMargin: "<<iTopMargin<< "\n";
  296. // cout<<"iLowerMargin: "<<iLowerMargin<< "\n";
  297. // cout<<"Height:" << iGraphHeight << ", Width:" << iGraphWidth << "\n";
  298. // cout<<"TOPLEFT:(" << iXMin - iLeftMargin << "," << iYMin - iLowerMargin << ")\n";
  299. //----------------------------------------------------------------------------------------------------------
  300. return iMaxMargine+10;
  301. }
  302. void Reingold :: SetClusterCompartmentProps(SubGraph &l_graph)
  303. {
  304. //This function is used to set sub graph height and width
  305. VertexDescriptor verDisEnd;
  306. VertexIterPair verIterEnd;
  307. BoostGraphWrapper bGraphWrap;
  308. int iXMin =INT_MAX;
  309. int iYMin =INT_MAX;
  310. int iXMax = INT_MIN;
  311. int iYMax =INT_MIN;
  312. int iXval = 0;
  313. int iYval = 0;
  314. int iHeight =0;
  315. int iWidth=0;
  316. bool bIsVirtual = false;
  317. // setCompartMentProps(l_graph);
  318. int clustercounter =0;
  319. SubGraph* gChildGraph = NULL;
  320. int iTotalClusters =0;
  321. QQueue<SubGraph*> qSubgraphs; // a queue of subgraphs
  322. qSubgraphs.enqueue(&l_graph);
  323. SubGraph* gSubgraph1 = NULL;
  324. while(qSubgraphs.isEmpty() == false) // Iterating subgraphs till they exist
  325. {
  326. gSubgraph1 = qSubgraphs.dequeue();
  327. iTotalClusters++;
  328. ChildrenIterator iterChild , iterChildEnd;
  329. for(boost::tie(iterChild , iterChildEnd) = gSubgraph1->children();
  330. iterChild != iterChildEnd;
  331. iterChild++)
  332. {
  333. clustercounter++;
  334. gChildGraph = &(*iterChild);
  335. qSubgraphs.enqueue(gChildGraph);
  336. }
  337. for(tie(verIterEnd.first,verIterEnd.second)=vertices((*gSubgraph1));
  338. verIterEnd.first!=verIterEnd.second;++verIterEnd.first)
  339. {
  340. verDisEnd = *verIterEnd.first;
  341. iXval = bGraphWrap.getVertexCenterCoordX(verDisEnd,(*gSubgraph1));
  342. iYval = bGraphWrap.getVertexCenterCoordY(verDisEnd,(*gSubgraph1));
  343. bIsVirtual = bGraphWrap.getIsVirtualNode(verDisEnd,(*gSubgraph1));
  344. if(!bIsVirtual) // skip virtual nodes
  345. {
  346. if(iXval < iXMin)
  347. {
  348. iXMin = iXval;
  349. }
  350. if(iXval > iXMax)
  351. {
  352. iXMax = iXval;
  353. }
  354. if(iYval < iYMin)
  355. {
  356. iYMin = iYval;
  357. }
  358. if(iYval > iYMax)
  359. {
  360. iYMax = iYval;
  361. }
  362. }
  363. bIsVirtual = false;
  364. }
  365. iWidth = std::abs(iXMin - iXMax);
  366. iHeight= std::abs(iYMin - iYMax);
  367. bGraphWrap.setGraphLeftTopCoordX(iXMin-10,(*gSubgraph1)); // +10
  368. bGraphWrap.setGraphLeftTopCoordY(iYMin-10,(*gSubgraph1)); // +10
  369. bGraphWrap.setGraphHeight(iHeight+20,(*gSubgraph1)); // +10
  370. bGraphWrap.setGraphWidth(iWidth+20,(*gSubgraph1)); // +10
  371. bGraphWrap.setGraphCenterCoordX((iXMin+iXMax)/2);
  372. bGraphWrap.setGraphCenterCoordY((iYMax+iYMin)/2);
  373. cout<<"\n ***** Cluster Id : "<<bGraphWrap.getGraphClusterID(*gSubgraph1);
  374. cout<<" Height : "<<iHeight;
  375. cout<<" Width : "<<iWidth;
  376. cout<<" Top Left X : "<<iXMin - 30 ;
  377. cout<<" Top Left Y : "<<iYMin - 30 ;
  378. cout<<"***********\n";
  379. iXMin =INT_MAX;
  380. iYMin =INT_MAX;
  381. iXMax = INT_MIN;
  382. iYMax =INT_MIN;
  383. iXval = 0;
  384. iYval = 0;
  385. iHeight =0;
  386. iWidth=0;
  387. }
  388. }
  389. void Reingold :: SetClusterCompartmentPropsNew(SubGraph &l_graph)
  390. {
  391. ChildrenIterator itrChild, itrEndChild;
  392. for(boost::tie(itrChild, itrEndChild) = l_graph.children(); itrChild != itrEndChild; ++itrChild)
  393. {
  394. this->SetClusterCompartmentPropsNew(*itrChild);
  395. }
  396. VertexDescriptor verDis;
  397. VertexIterPair verIter;
  398. BoostGraphWrapper bGraphWrap;
  399. int iXMin = 2147483647;
  400. int iYMin = 2147483647;
  401. int iXMax = -2147483648;
  402. int iYMax = -2147483648;
  403. int iMaxHeight = 0;
  404. int iMaxWidth = 0;
  405. int iHeightOfTopmost = 0;
  406. int iHeightOfLowermost = 0;
  407. int iWidthOfLeftmost = 0;
  408. int iWidthOfRightmost = 0;
  409. for(tie(verIter.first,verIter.second) = vertices(l_graph); verIter.first!=verIter.second; ++verIter.first)
  410. {
  411. verDis = *verIter.first;
  412. int iXval = bGraphWrap.getVertexCenterCoordX(verDis, l_graph);
  413. int iYval = bGraphWrap.getVertexCenterCoordY(verDis, l_graph);
  414. int iVertexHeight = bGraphWrap.getVertexHeight(verDis, l_graph) ;
  415. int iVertexWidth = bGraphWrap.getVertexWidth(verDis, l_graph);
  416. if(iXval < iXMin)
  417. {
  418. iXMin = iXval;
  419. if(iVertexWidth > iWidthOfLeftmost)
  420. iWidthOfLeftmost = iVertexWidth;
  421. }
  422. if(iXval > iXMax)
  423. {
  424. iXMax = iXval;
  425. if(iVertexWidth > iWidthOfRightmost)
  426. iWidthOfRightmost = iVertexWidth;
  427. }
  428. if(iYval < iYMin)
  429. {
  430. iYMin = iYval;
  431. if(iVertexHeight > iHeightOfLowermost)
  432. iHeightOfLowermost = iVertexHeight;
  433. }
  434. if(iYval > iYMax)
  435. {
  436. iYMax = iYval;
  437. if(iVertexHeight > iHeightOfTopmost)
  438. iHeightOfTopmost = iVertexHeight;
  439. }
  440. if(iVertexHeight > iMaxHeight)
  441. iMaxHeight = iVertexHeight;
  442. if(iVertexWidth > iMaxWidth)
  443. iMaxWidth = iVertexWidth;
  444. }
  445. int iGraphWidth = std::abs(iXMin - iXMax);
  446. int iGraphHeight = std::abs(iYMin - iYMax);
  447. int iLeftMargin = (iWidthOfLeftmost/2) + 20;
  448. int iRightMargin = (iWidthOfRightmost/2) + 20;
  449. int iTopMargin = (iHeightOfTopmost/2) + 20;
  450. int iLowerMargin = (iHeightOfLowermost/2) + 20;
  451. iGraphWidth = iGraphWidth + iLeftMargin + iRightMargin;
  452. iGraphHeight = iGraphHeight + iTopMargin + iLowerMargin;
  453. bGraphWrap.setGraphLeftTopCoordX(iXMin - iLeftMargin, l_graph);
  454. bGraphWrap.setGraphLeftTopCoordY(iYMin - iLowerMargin, l_graph);
  455. bGraphWrap.setGraphHeight(iGraphHeight, l_graph);
  456. bGraphWrap.setGraphWidth(iGraphWidth, l_graph);
  457. }