VertexOverlapRemoval.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. #include "VertexOverlapRemoval.h"
  2. VertexOverlapRemoval::VertexOverlapRemoval()
  3. {
  4. }
  5. void VertexOverlapRemoval::repelOverlappingVerticesWithOther(SubGraph& gMaingraph)
  6. {
  7. cout<<"\n \n *********In Overlap Removal ******** \n \n";
  8. do
  9. {
  10. QVector<VertexIterator> vOverLappingVertices = getOverlappingVertices(gMaingraph);
  11. m_iVertexOverlapConter++;
  12. cout<<"\n Counter "<<m_iVertexOverlapConter;
  13. cout<<"\n Overlapping Vertices are : "<< vOverLappingVertices.size();
  14. cout<<"\n \n \n";
  15. BoostGraphWrapper bGraphWrapper;
  16. if(!vOverLappingVertices.size() == 0)
  17. {
  18. for(int index =0 ; index < vOverLappingVertices.size();index++)
  19. {
  20. for(int innerIndex = 0 ; innerIndex < vOverLappingVertices.size(); innerIndex++)
  21. {
  22. if(innerIndex == index)
  23. {
  24. continue;
  25. }
  26. VertexIterator vVertexOuterIterator = vOverLappingVertices.at(index);
  27. VertexIterator vVertexInnerIterator = vOverLappingVertices.at(innerIndex);
  28. VertexDescriptor vVertexOuter = *vVertexOuterIterator;
  29. VertexDescriptor vVertexInner = *vVertexInnerIterator;
  30. if(vVertexInner == vVertexOuter)
  31. {
  32. continue;
  33. }
  34. if(bIsOverlappingVertices(vVertexOuterIterator,vVertexInnerIterator,gMaingraph))
  35. {
  36. double dXCoordV = bGraphWrapper.getVertexCenterCoordX(vVertexOuter,gMaingraph);
  37. double dYCoordV = bGraphWrapper.getVertexCenterCoordY(vVertexOuter,gMaingraph);
  38. double dXCoordU = bGraphWrapper.getVertexCenterCoordX(vVertexInner,gMaingraph);
  39. double dYCoordU = bGraphWrapper.getVertexCenterCoordY(vVertexInner,gMaingraph);
  40. if(dXCoordV < dXCoordU)
  41. {
  42. if(dYCoordU < dYCoordV) // WORKING LOGIC
  43. {
  44. dXCoordU = dXCoordU + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
  45. dYCoordU = dYCoordU + std::rand()%20- CORRECTED_DISTANCE/4 ;
  46. bGraphWrapper.setVertexCenterCoordX(vVertexInner,gMaingraph,dXCoordU);
  47. bGraphWrapper.setVertexCenterCoordY(vVertexInner,gMaingraph,dYCoordU);
  48. }
  49. else
  50. {
  51. dXCoordU = dXCoordU + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
  52. dYCoordU = dYCoordU + std::rand()%20+ CORRECTED_DISTANCE/4;
  53. bGraphWrapper.setVertexCenterCoordX(vVertexInner,gMaingraph,dXCoordU);
  54. bGraphWrapper.setVertexCenterCoordY(vVertexInner,gMaingraph,dYCoordU);
  55. }
  56. }
  57. else
  58. {
  59. if(dYCoordU < dYCoordV) // WORKING LOGIC
  60. {
  61. dXCoordV = dXCoordV + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
  62. dYCoordV = dYCoordV + std::rand()%20- CORRECTED_DISTANCE/4 ;
  63. bGraphWrapper.setVertexCenterCoordX(vVertexOuter,gMaingraph,dXCoordU);
  64. bGraphWrapper.setVertexCenterCoordY(vVertexOuter,gMaingraph,dYCoordU);
  65. }
  66. else
  67. {
  68. dXCoordV = dXCoordV + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
  69. dYCoordV = dYCoordV + std::rand()%20+ CORRECTED_DISTANCE/4;
  70. bGraphWrapper.setVertexCenterCoordX(vVertexOuter,gMaingraph,dXCoordU);
  71. bGraphWrapper.setVertexCenterCoordY(vVertexOuter,gMaingraph,dYCoordU);
  72. }
  73. }
  74. // bGraphWrapper.setVertexCenterCoordX(vVertexOuter,gMaingraph,(int)dXCoordV);
  75. // bGraphWrapper.setVertexCenterCoordY(vVertexOuter,gMaingraph,(int)dYCoordV);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. while(bOverlapExist(gMaingraph)&& m_iVertexOverlapConter < 100); // while overlap
  82. }
  83. QVector<VertexIterator> VertexOverlapRemoval::getOverlappingVertices(SubGraph& gMaingraph)
  84. {
  85. QVector<VertexIterator> vOverLappingVertices;
  86. VertexDescriptor verAffected,verVisitor;
  87. VertexIterPair verIter,verIterEnd;
  88. BoostGraphWrapper bGraphWrapper;
  89. for(tie(verIter.first,verIter.second)=vertices(gMaingraph)
  90. ;verIter.first!=verIter.second;++verIter.first)
  91. {
  92. verAffected = *verIter.first;
  93. double dXCoordV = bGraphWrapper.getVertexCenterCoordX(verAffected,gMaingraph);
  94. double dYCoordV = bGraphWrapper.getVertexCenterCoordY(verAffected,gMaingraph);
  95. for(tie(verIterEnd.first,verIterEnd.second)=vertices(gMaingraph);
  96. verIterEnd.first != verIterEnd.second;++verIterEnd.first)
  97. {
  98. verVisitor = *verIterEnd.first;
  99. double dXCoordU = bGraphWrapper.getVertexCenterCoordX(verVisitor,gMaingraph);
  100. double dYCoordU = bGraphWrapper.getVertexCenterCoordY(verVisitor,gMaingraph);
  101. if(verAffected == verVisitor)
  102. {
  103. continue;
  104. }
  105. // Logic for vertex size here
  106. double dWidthU = bGraphWrapper.getVertexWidth(verVisitor,gMaingraph);
  107. double dHeightU = bGraphWrapper.getVertexHeight(verVisitor,gMaingraph);
  108. double dWidthV = bGraphWrapper.getVertexWidth(verAffected,gMaingraph);
  109. double dHeightV = bGraphWrapper.getVertexHeight(verAffected,gMaingraph );
  110. double dXLeftTopU = dXCoordU - dWidthU/2 -5;
  111. double dXLeftTopV = dXCoordV - dWidthV/2 -5;
  112. double dYLeftTopU = dYCoordU - dHeightU/2 -5;
  113. double dYLeftTopV = dYCoordV - dHeightV/2 -5;
  114. if( (dXLeftTopV >= dXLeftTopU && dXLeftTopV <= dXLeftTopU+ dWidthU)||
  115. (dXLeftTopV <= dXLeftTopU && dXLeftTopV+ dWidthV >= dXLeftTopU))
  116. {
  117. // x intercept
  118. if((dYLeftTopV >= dYLeftTopU && dYLeftTopU + dHeightU >= dYLeftTopV)
  119. || (dYLeftTopU >= dYLeftTopV && dYLeftTopV + dHeightV >= dYLeftTopU) )
  120. {
  121. // check if already added
  122. if(!vOverLappingVertices.contains(verIterEnd.first))
  123. {
  124. vOverLappingVertices.push_back(verIterEnd.first);
  125. }
  126. if(!vOverLappingVertices.contains(verIter.first))
  127. {
  128. vOverLappingVertices.push_back(verIter.first);
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return vOverLappingVertices;
  135. }
  136. GraphLayoutErrorCodes::LayoutErrorCode VertexOverlapRemoval::removeOverlaps(QString sInputGraphMLFilePath,
  137. QString sOutputGraphMLFilePath) //SubGraph& gMaingraph)
  138. {
  139. GraphLayoutErrorCodes::LayoutErrorCode enVertexOverlapErrorCode =
  140. GraphLayoutErrorCodes::LAYOUT_SUCCESS;
  141. if( (sInputGraphMLFilePath.isEmpty() == true))
  142. {
  143. enVertexOverlapErrorCode = GraphLayoutErrorCodes::INVALID_FILE_NAME;
  144. }
  145. if((sInputGraphMLFilePath.trimmed().endsWith(GRAPHML, Qt::CaseInsensitive) == false)
  146. && (enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  147. {
  148. enVertexOverlapErrorCode = GraphLayoutErrorCodes::UNSUPPORTED_FILE_FORMAT;
  149. }
  150. if((QFile::exists(sInputGraphMLFilePath) == false) && (enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  151. {
  152. enVertexOverlapErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  153. }
  154. // create global graph pointer.
  155. SubGraph *gMaingraph = NULL ;
  156. if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  157. {
  158. // read input GraphML file
  159. QFile inFile(sInputGraphMLFilePath);
  160. GraphMLReader reader;
  161. try
  162. {
  163. SubGraph &gInputGraph = reader.readGraphML(&inFile);
  164. gMaingraph = &(gInputGraph);
  165. inFile.close();
  166. }
  167. catch(LayoutFileIOException& eException)
  168. {
  169. inFile.close();
  170. //We return the file exception enums
  171. enVertexOverlapErrorCode = (GraphLayoutErrorCodes::LayoutErrorCode)eException.getErrorCode();
  172. }
  173. }
  174. cout<<"\n \n *********In Overlap Removal ******** \n \n";
  175. if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  176. {
  177. do
  178. {
  179. QVector<VertexIterator> vOverLappingVertices = getOverlappingVertices(*gMaingraph);
  180. m_iVertexOverlapConter++;
  181. cout<<"\n Counter "<<m_iVertexOverlapConter;
  182. cout<<"\n Overlapping Vertices are : "<< vOverLappingVertices.size();
  183. cout<<"\n \n \n";
  184. VertexDescriptor verOther;
  185. VertexIterPair verIterInner;
  186. BoostGraphWrapper bGraphWrapper;
  187. if(!vOverLappingVertices.size() == 0)
  188. {
  189. for(int index =0 ; index < vOverLappingVertices.size();index++)
  190. {
  191. for(int innerIndex = 0 ; innerIndex < vOverLappingVertices.size(); innerIndex++)
  192. {
  193. if(innerIndex == index)
  194. {
  195. continue;
  196. }
  197. VertexIterator vVertexOuterIterator = vOverLappingVertices.at(index);
  198. VertexIterator vVertexInnerIterator = vOverLappingVertices.at(innerIndex);
  199. VertexDescriptor vVertexOuter = *vVertexOuterIterator;
  200. VertexDescriptor vVertexInner = *vVertexInnerIterator;
  201. if(vVertexInner == vVertexOuter)
  202. {
  203. continue;
  204. }
  205. if(bIsOverlappingVertices(vVertexOuterIterator,vVertexInnerIterator,*gMaingraph))
  206. {
  207. // define a moement vector
  208. int iMovementVectorX = 0;
  209. int iMovementVectorY = 0;
  210. double dXCoordV = bGraphWrapper.getVertexCenterCoordX(vVertexOuter,*gMaingraph);
  211. double dYCoordV = bGraphWrapper.getVertexCenterCoordY(vVertexOuter,*gMaingraph);
  212. double dXCoordU = bGraphWrapper.getVertexCenterCoordX(vVertexInner,*gMaingraph);
  213. double dYCoordU = bGraphWrapper.getVertexCenterCoordY(vVertexInner,*gMaingraph);
  214. iMovementVectorX = dXCoordV - dXCoordU;
  215. iMovementVectorY = dYCoordV - dYCoordU;
  216. double dDistance = iMovementVectorX*iMovementVectorX +
  217. iMovementVectorY*iMovementVectorY;
  218. dDistance = sqrt(dDistance);
  219. if(dDistance == 0)
  220. {
  221. dDistance = 0.001; //avoid nan
  222. }
  223. iMovementVectorX = 200*iMovementVectorX/dDistance;
  224. iMovementVectorY = 200*iMovementVectorY/dDistance;
  225. //
  226. for(tie(verIterInner.first,verIterInner.second)=vertices(*gMaingraph);
  227. verIterInner.first != verIterInner.second;++verIterInner.first)
  228. {
  229. // repel with all other vertices
  230. verOther = *verIterInner.first;
  231. double dXCoordOther = bGraphWrapper.getVertexCenterCoordX(verOther,*gMaingraph);
  232. double dYCoordOther = bGraphWrapper.getVertexCenterCoordY(verOther,*gMaingraph);
  233. double dDeltaX = dXCoordV - dXCoordOther;
  234. double dDeltaY = dYCoordV - dYCoordOther;
  235. double dDistanceOther = dDeltaX*dDeltaX +
  236. dDeltaY*dDeltaY;
  237. if(dDistanceOther == 0)
  238. {
  239. dDistanceOther = 0.001;
  240. }
  241. iMovementVectorX += 20*dDeltaX/dDistanceOther;
  242. iMovementVectorY += 20*dDeltaY/dDistanceOther;
  243. }
  244. //
  245. dXCoordV += 1*iMovementVectorX;
  246. dYCoordV += 1*iMovementVectorY;
  247. // move
  248. bGraphWrapper.setVertexCenterCoordX(vVertexOuter,*gMaingraph,(int)dXCoordV);
  249. bGraphWrapper.setVertexCenterCoordY(vVertexOuter,*gMaingraph,(int)dYCoordV);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. while(bOverlapExist(*gMaingraph)&& m_iVertexOverlapConter < 5000);
  256. // while overlap
  257. Reingold resetClusterProps;
  258. resetClusterProps.setCompartMentProps(*gMaingraph,10);
  259. }
  260. if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  261. {
  262. // write file to output path
  263. QFile outFile(sOutputGraphMLFilePath);
  264. if ((!outFile.open(QFile::WriteOnly | QFile::Truncate)))
  265. {
  266. enVertexOverlapErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  267. }
  268. if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  269. {
  270. GraphMLWriter writer;
  271. try
  272. {
  273. writer.writeGraphml(*gMaingraph , &outFile);
  274. outFile.flush();
  275. outFile.close();
  276. }
  277. catch(...)
  278. {
  279. outFile.close();
  280. DELETE_AND_SET_NULL(gMaingraph);
  281. enVertexOverlapErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  282. outFile.close();
  283. }
  284. }
  285. }
  286. Postprocessing postProcess; //Remove clusters if there are
  287. postProcess.checkClusterOverlap(*gMaingraph);
  288. return enVertexOverlapErrorCode;
  289. }
  290. bool VertexOverlapRemoval::bIsOverlappingVertices(VertexIterator vVertexFirstIterator
  291. ,VertexIterator vVertexSecondIterator
  292. ,SubGraph& gMaingraph)
  293. {
  294. // return true if vertices overlap
  295. bool bOverlap = false;
  296. BoostGraphWrapper bGraphWrapper;
  297. VertexDescriptor vVertexFirst = *vVertexFirstIterator;
  298. VertexDescriptor vVertexSecond = *vVertexSecondIterator;
  299. double dXCoordV = bGraphWrapper.getVertexCenterCoordX(vVertexFirst,gMaingraph);
  300. double dYCoordV = bGraphWrapper.getVertexCenterCoordY(vVertexFirst,gMaingraph);
  301. double dXCoordU = bGraphWrapper.getVertexCenterCoordX(vVertexSecond,gMaingraph);
  302. double dYCoordU = bGraphWrapper.getVertexCenterCoordY(vVertexSecond,gMaingraph);
  303. double dWidthU = bGraphWrapper.getVertexWidth(vVertexSecond,gMaingraph);
  304. double dHeightU = bGraphWrapper.getVertexHeight(vVertexSecond,gMaingraph);
  305. double dWidthV = bGraphWrapper.getVertexWidth(vVertexFirst,gMaingraph);
  306. double dHeightV = bGraphWrapper.getVertexHeight(vVertexFirst,gMaingraph );
  307. double dXLeftTopU = dXCoordU - dWidthU/2 - 5;
  308. double dXLeftTopV = dXCoordV - dWidthV/2 - 5;
  309. double dYLeftTopU = dYCoordU - dHeightU/2 -5;
  310. double dYLeftTopV = dYCoordV - dHeightV/2 - 5;
  311. if( (dXLeftTopV >= dXLeftTopU && dXLeftTopV <= dXLeftTopU+ dWidthU)||
  312. (dXLeftTopV <= dXLeftTopU && dXLeftTopV+ dWidthV >= dXLeftTopU))
  313. {
  314. // x intercept
  315. if((dYLeftTopV >= dYLeftTopU && dYLeftTopU + dHeightU >= dYLeftTopV)
  316. || (dYLeftTopU >= dYLeftTopV && dYLeftTopV + dHeightV >= dYLeftTopU))
  317. {
  318. bOverlap = true;
  319. }
  320. }
  321. return bOverlap;
  322. }
  323. bool VertexOverlapRemoval::bOverlapExist(SubGraph& gMaingraph)
  324. {
  325. // returns true if graph has overlappng vertices.
  326. bool bOverlap = false;
  327. VertexDescriptor verAffected,verVisitor;
  328. VertexIterPair verIter,verIterEnd;
  329. BoostGraphWrapper bGraphWrapper;
  330. for(tie(verIter.first,verIter.second)=vertices(gMaingraph)
  331. ;verIter.first!=verIter.second;++verIter.first)
  332. {
  333. verAffected = *verIter.first;
  334. double dXCoordV = bGraphWrapper.getVertexCenterCoordX(verAffected,gMaingraph);
  335. double dYCoordV = bGraphWrapper.getVertexCenterCoordY(verAffected,gMaingraph);
  336. for(tie(verIterEnd.first,verIterEnd.second)=vertices(gMaingraph);
  337. verIterEnd.first != verIterEnd.second;++verIterEnd.first)
  338. {
  339. verVisitor = *verIterEnd.first;
  340. double dXCoordU = bGraphWrapper.getVertexCenterCoordX(verVisitor,gMaingraph);
  341. double dYCoordU = bGraphWrapper.getVertexCenterCoordY(verVisitor,gMaingraph);
  342. if(verAffected == verVisitor)
  343. {
  344. continue;
  345. }
  346. // Logic for vertex size here
  347. double dWidthU = bGraphWrapper.getVertexWidth(verVisitor,gMaingraph);
  348. double dHeightU = bGraphWrapper.getVertexHeight(verVisitor,gMaingraph);
  349. double dWidthV = bGraphWrapper.getVertexWidth(verAffected,gMaingraph);
  350. double dHeightV = bGraphWrapper.getVertexHeight(verAffected,gMaingraph );
  351. double dXLeftTopU = dXCoordU - dWidthU/2 - 5;
  352. double dXLeftTopV = dXCoordV - dWidthV/2 - 5;
  353. double dYLeftTopU = dYCoordU - dHeightU/2 - 5;
  354. double dYLeftTopV = dYCoordV - dHeightV/2 -5;
  355. if( (dXLeftTopV >= dXLeftTopU && dXLeftTopV <= dXLeftTopU+ dWidthU)||
  356. (dXLeftTopV <= dXLeftTopU && dXLeftTopV+ dWidthV >= dXLeftTopU))
  357. {
  358. // x intercept
  359. if((dYLeftTopV >= dYLeftTopU && dYLeftTopU + dHeightU >= dYLeftTopV)
  360. || (dYLeftTopU >= dYLeftTopV && dYLeftTopV + dHeightV >= dYLeftTopU) )
  361. {
  362. return true;
  363. }
  364. }
  365. }
  366. }
  367. return bOverlap;
  368. }