SpringEmbedder.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include "SpringEmbedder.h"
  2. #include "MembershipInitializer.h"
  3. #include <HierarchicalLayoutGenerator/HierarchicalLayouter.h>
  4. #include <Common/LayoutEnum.h>
  5. #include<Common/GraphType.h>
  6. SpringEmbedder::SpringEmbedder()
  7. {
  8. }
  9. void SpringEmbedder :: getRestrictedBounds(SubGraph& gMainGraph, QVector<int>&
  10. vRestBound, QVector<SubGraph *> vClusters )
  11. {
  12. // returns a vector of cluster dimensions and rstricted bounds
  13. BoostGraphWrapper bGraphWrapper;
  14. QQueue<SubGraph*> qSubgraphs; // a queue of subgraphs
  15. qSubgraphs.enqueue(&gMainGraph);
  16. SubGraph* gSubgraph1 = NULL;
  17. while(qSubgraphs.isEmpty() == false) // Iterating subgraphs till they exist
  18. {
  19. gSubgraph1 = qSubgraphs.dequeue();
  20. ChildrenIterator iterChild , iterChildEnd;
  21. for(boost::tie(iterChild , iterChildEnd) = gSubgraph1->children();
  22. iterChild != iterChildEnd;
  23. iterChild++)
  24. {
  25. SubGraph* gChildGraph = &(*iterChild);
  26. if(boost::num_vertices(*gChildGraph) !=0)
  27. {
  28. vRestBound.append(bGraphWrapper.getGraphCenterCoordX(*gChildGraph));
  29. vRestBound.append(bGraphWrapper.getGraphCenterCoordY(*gChildGraph));
  30. vRestBound.append( bGraphWrapper.getGraphHeight(*gChildGraph));
  31. vRestBound.append(bGraphWrapper.getGraphWidth(*gChildGraph));
  32. vRestBound.append(bGraphWrapper.getGraphLeftTopCoordX(*gChildGraph));
  33. vRestBound.append(bGraphWrapper.getGraphLeftTopCoordY(*gChildGraph));
  34. vClusters.append(gChildGraph);
  35. }
  36. }
  37. }
  38. }
  39. void SpringEmbedder :: SpringEmbedderStep(SubGraph& gMainGraph)
  40. {
  41. BoostGraphWrapper bgraphWrapper;
  42. int iNumnodes = bgraphWrapper.numVertices(gMainGraph);
  43. if(iNumnodes > 49 )
  44. {
  45. m_dIterations = 100;
  46. }
  47. else
  48. {
  49. m_dIterations = 50;
  50. }
  51. while(m_dIterations >0)
  52. {
  53. // relax edges
  54. SpringRelaxEdges(gMainGraph);
  55. // repel vertices
  56. SpringRepelVertices(gMainGraph);
  57. // move nodes
  58. SpringMoveNodes(gMainGraph);
  59. m_dIterations--;
  60. }
  61. }
  62. void SpringEmbedder :: setDesiredEdgeLength(double dEdgeLength)
  63. {
  64. m_dDesiredEdgeLength = dEdgeLength;
  65. }
  66. void SpringEmbedder :: setNoOfIterations(double dNumIterations)
  67. {
  68. m_dIterations = dNumIterations;
  69. }
  70. void SpringEmbedder :: SpringRelaxEdges(SubGraph& gMainGraph)
  71. {
  72. BoostGraphWrapper bGraphWrapper;
  73. EdgeIterator eStart, eEnd;
  74. for (boost::tie(eStart, eEnd) = edges(gMainGraph); eStart != eEnd; ++eStart)
  75. {
  76. VertexDescriptor verV = source(*eStart, gMainGraph);
  77. VertexDescriptor verU = target(*eStart, gMainGraph);
  78. double dXCoordV = bGraphWrapper.getVertexCenterCoordX(verV,gMainGraph);
  79. double dYCoordV = bGraphWrapper.getVertexCenterCoordY(verV,gMainGraph);
  80. double dXCoordU = bGraphWrapper.getVertexCenterCoordX(verU,gMainGraph);
  81. double dYCoordU = bGraphWrapper.getVertexCenterCoordY(verU,gMainGraph);
  82. double dXDisp = bGraphWrapper.getVertXDisp(verV,gMainGraph);
  83. double dYDisp = bGraphWrapper.getVertYDisp(verV,gMainGraph);
  84. double dXDisp_U = 0;
  85. double dYDisp_U = 0;
  86. dXDisp_U = bGraphWrapper.getVertXDisp(verU,gMainGraph);
  87. dYDisp_U = bGraphWrapper.getVertYDisp(verU,gMainGraph);
  88. // Logic for vertex size here
  89. double dXsizeU = bGraphWrapper.getVertexWidth(verU,gMainGraph);
  90. double dXsizeV = bGraphWrapper.getVertexWidth(verV,gMainGraph);
  91. if(dXCoordU > dXCoordV)
  92. {
  93. dXCoordV = dXCoordV + dXsizeV/2;
  94. dXCoordU = dXCoordU - dXsizeU/2;
  95. }
  96. else
  97. {
  98. dXCoordV = dXCoordV - dXsizeV/2;
  99. dXCoordU = dXCoordU + dXsizeU/2;
  100. }
  101. double d_Vx = dXCoordV - dXCoordU;
  102. double d_Vy = dYCoordV - dYCoordU;
  103. cout<<"\n Delta X and Y at attractive force : "<<d_Vy<<" "
  104. << d_Vx;
  105. double dDist = fabs(sqrt((d_Vx*d_Vx+d_Vy* d_Vy)));
  106. cout<<"Distance at attractive force : "<<dDist;
  107. if(dDist == 0)
  108. {
  109. dDist = 0.0001;
  110. }
  111. // desired length = 50
  112. double dForce = 0.3 *(m_dDesiredEdgeLength - dDist)/dDist ; //0.3 force multiplier
  113. dForce = dForce * std::pow(0.7,in_degree(verV,gMainGraph)+in_degree(verU,gMainGraph)
  114. + out_degree(verU,gMainGraph)+out_degree(verV,gMainGraph) -2); // not known 0.3
  115. double dx = dForce * d_Vx;
  116. double dy = dForce * d_Vy;
  117. dXDisp += dx;
  118. dYDisp += dy;
  119. dXDisp_U -= dx;
  120. dYDisp_U -= dy;
  121. cout<<"\n Attractive Forces Displacement " ;
  122. cout<<"\n at I X Disp : "<<dXDisp;
  123. cout<<" Y Disp : "<<dYDisp;
  124. cout<<"\n at J X Disp : "<<dXDisp_U;
  125. cout<<" Y Disp : "<<dYDisp_U;
  126. bGraphWrapper.setVertXDisp(dXDisp,verV,gMainGraph);
  127. bGraphWrapper.setVertYDisp(dYDisp,verV,gMainGraph);
  128. bGraphWrapper.setVertXDisp(dXDisp_U,verU,gMainGraph);
  129. bGraphWrapper.setVertYDisp(dYDisp_U,verU,gMainGraph);
  130. }
  131. }
  132. void SpringEmbedder :: SpringRepelVertices(SubGraph& gMainGraph)
  133. {
  134. VertexDescriptor verAffected,verVisitor;
  135. VertexIterPair verIter,verIterEnd;
  136. BoostGraphWrapper bGraphWrapper;
  137. for(tie(verIter.first,verIter.second)=vertices(gMainGraph)
  138. ;verIter.first!=verIter.second;++verIter.first)
  139. {
  140. double dx = 0;
  141. double dy = 0;
  142. double XDisp =0;
  143. double YDisp = 0;
  144. double dXCoordV = bGraphWrapper.getVertexCenterCoordX(verAffected,gMainGraph);
  145. double dYCoordV = bGraphWrapper.getVertexCenterCoordY(verAffected,gMainGraph);
  146. verAffected = *verIter.first;
  147. for(tie(verIterEnd.first,verIterEnd.second)=vertices(gMainGraph);
  148. verIterEnd.first != verIterEnd.second;++verIterEnd.first)
  149. {
  150. verVisitor = *verIterEnd.first;
  151. double dXCoordU = bGraphWrapper.getVertexCenterCoordX(verVisitor,gMainGraph);
  152. double dYCoordU = bGraphWrapper.getVertexCenterCoordY(verVisitor,gMainGraph);
  153. if(verAffected == verVisitor)
  154. {
  155. continue;
  156. }
  157. // Logic for vertex size here
  158. double dXsizeU = bGraphWrapper.getVertexWidth(verVisitor,gMainGraph);
  159. double dXsizeV = bGraphWrapper.getVertexWidth(verAffected,gMainGraph);
  160. if(dXCoordU > dXCoordV)
  161. {
  162. dXCoordV = dXCoordV + dXsizeV/2;
  163. dXCoordU = dXCoordU - dXsizeU/2;
  164. }
  165. else
  166. {
  167. dXCoordV = dXCoordV - dXsizeV/2;
  168. dXCoordU = dXCoordU + dXsizeU/2;
  169. }
  170. double dvx = dXCoordV - dXCoordU;
  171. double dvy = dYCoordV - dYCoordU;
  172. double dDist = fabs((dvx*dvx + dvy* dvy));
  173. cout<<"\nDistance at Repulsive force : "<<dDist;
  174. if(dDist == 0 || dDist < 500 )
  175. {
  176. dDist = std::rand()%300+100;
  177. }
  178. if(dDist < 100*100*100 )
  179. {
  180. dx += 1*dvx/dDist; // factor =1 def works well for 1000
  181. dy += 1*dvy/dDist;
  182. }
  183. else{
  184. cout<<"\n Skipping due to distance";
  185. }
  186. cout<<"\ndx and dy at Repulsive force : "<<dx<<" "<<dy;
  187. }
  188. double dLen = dx*dx+ dy*dy;
  189. dLen = sqrt(fabs(dLen))/2;
  190. if(dLen > 0)
  191. {
  192. XDisp = bGraphWrapper.getVertXDisp(verAffected,gMainGraph);
  193. YDisp = bGraphWrapper.getVertYDisp(verAffected,gMainGraph);
  194. XDisp += (dx/dLen);
  195. YDisp += (dy/dLen);
  196. bGraphWrapper.setVertXDisp(XDisp,verAffected,gMainGraph);
  197. bGraphWrapper.setVertYDisp(YDisp,verAffected,gMainGraph);
  198. cout<<"\nDisplacement at Repulsive force : "<<YDisp<<" "
  199. << XDisp;
  200. }
  201. }
  202. }
  203. void SpringEmbedder :: SpringMoveNodes(SubGraph& gMainGraph)
  204. {
  205. BoostGraphWrapper bGraphWrapper;
  206. VertexDescriptor verAffected;
  207. VertexIterPair verIter;
  208. for(tie(verIter.first,verIter.second)=vertices(gMainGraph)
  209. ;verIter.first!=verIter.second;++verIter.first)
  210. {
  211. verAffected = *verIter.first;
  212. double dXCoordV = bGraphWrapper.getVertexCenterCoordX
  213. (verAffected,gMainGraph);
  214. double dYCoordV = bGraphWrapper.getVertexCenterCoordY
  215. (verAffected,gMainGraph);
  216. double dXDisp = bGraphWrapper.getVertXDisp(verAffected,gMainGraph);
  217. double dYDisp = bGraphWrapper.getVertYDisp(verAffected,gMainGraph);
  218. bGraphWrapper.setVertXDisp(dXDisp/4,verAffected,gMainGraph);
  219. bGraphWrapper.setVertYDisp(dYDisp/4,verAffected,gMainGraph);
  220. dXDisp = std::min(dXDisp,10.0);
  221. dYDisp = std::min(dYDisp,10.0);
  222. bGraphWrapper.setVertexCenterCoordX(verAffected,gMainGraph,
  223. dXCoordV+dXDisp);
  224. bGraphWrapper.setVertexCenterCoordY(verAffected,gMainGraph,
  225. dYCoordV+dYDisp);
  226. }
  227. }