123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- #include "VertexOverlapRemoval.h"
- VertexOverlapRemoval::VertexOverlapRemoval()
- {
- }
- void VertexOverlapRemoval::repelOverlappingVerticesWithOther(SubGraph& gMaingraph)
- {
- cout<<"\n \n *********In Overlap Removal ******** \n \n";
- do
- {
- QVector<VertexIterator> vOverLappingVertices = getOverlappingVertices(gMaingraph);
- m_iVertexOverlapConter++;
- cout<<"\n Counter "<<m_iVertexOverlapConter;
- cout<<"\n Overlapping Vertices are : "<< vOverLappingVertices.size();
- cout<<"\n \n \n";
- BoostGraphWrapper bGraphWrapper;
- if(!vOverLappingVertices.size() == 0)
- {
- for(int index =0 ; index < vOverLappingVertices.size();index++)
- {
- for(int innerIndex = 0 ; innerIndex < vOverLappingVertices.size(); innerIndex++)
- {
- if(innerIndex == index)
- {
- continue;
- }
- VertexIterator vVertexOuterIterator = vOverLappingVertices.at(index);
- VertexIterator vVertexInnerIterator = vOverLappingVertices.at(innerIndex);
- VertexDescriptor vVertexOuter = *vVertexOuterIterator;
- VertexDescriptor vVertexInner = *vVertexInnerIterator;
- if(vVertexInner == vVertexOuter)
- {
- continue;
- }
- if(bIsOverlappingVertices(vVertexOuterIterator,vVertexInnerIterator,gMaingraph))
- {
- double dXCoordV = bGraphWrapper.getVertexCenterCoordX(vVertexOuter,gMaingraph);
- double dYCoordV = bGraphWrapper.getVertexCenterCoordY(vVertexOuter,gMaingraph);
- double dXCoordU = bGraphWrapper.getVertexCenterCoordX(vVertexInner,gMaingraph);
- double dYCoordU = bGraphWrapper.getVertexCenterCoordY(vVertexInner,gMaingraph);
- if(dXCoordV < dXCoordU)
- {
- if(dYCoordU < dYCoordV) // WORKING LOGIC
- {
- dXCoordU = dXCoordU + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
- dYCoordU = dYCoordU + std::rand()%20- CORRECTED_DISTANCE/4 ;
- bGraphWrapper.setVertexCenterCoordX(vVertexInner,gMaingraph,dXCoordU);
- bGraphWrapper.setVertexCenterCoordY(vVertexInner,gMaingraph,dYCoordU);
- }
- else
- {
- dXCoordU = dXCoordU + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
- dYCoordU = dYCoordU + std::rand()%20+ CORRECTED_DISTANCE/4;
- bGraphWrapper.setVertexCenterCoordX(vVertexInner,gMaingraph,dXCoordU);
- bGraphWrapper.setVertexCenterCoordY(vVertexInner,gMaingraph,dYCoordU);
- }
- }
- else
- {
- if(dYCoordU < dYCoordV) // WORKING LOGIC
- {
- dXCoordV = dXCoordV + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
- dYCoordV = dYCoordV + std::rand()%20- CORRECTED_DISTANCE/4 ;
- bGraphWrapper.setVertexCenterCoordX(vVertexOuter,gMaingraph,dXCoordU);
- bGraphWrapper.setVertexCenterCoordY(vVertexOuter,gMaingraph,dYCoordU);
- }
- else
- {
- dXCoordV = dXCoordV + std::rand()%20+ CORRECTED_DISTANCE/2 +10 ; //+100;
- dYCoordV = dYCoordV + std::rand()%20+ CORRECTED_DISTANCE/4;
- bGraphWrapper.setVertexCenterCoordX(vVertexOuter,gMaingraph,dXCoordU);
- bGraphWrapper.setVertexCenterCoordY(vVertexOuter,gMaingraph,dYCoordU);
- }
- }
- // bGraphWrapper.setVertexCenterCoordX(vVertexOuter,gMaingraph,(int)dXCoordV);
- // bGraphWrapper.setVertexCenterCoordY(vVertexOuter,gMaingraph,(int)dYCoordV);
- }
- }
- }
- }
- }
- while(bOverlapExist(gMaingraph)&& m_iVertexOverlapConter < 100); // while overlap
- }
- QVector<VertexIterator> VertexOverlapRemoval::getOverlappingVertices(SubGraph& gMaingraph)
- {
- QVector<VertexIterator> vOverLappingVertices;
- VertexDescriptor verAffected,verVisitor;
- VertexIterPair verIter,verIterEnd;
- BoostGraphWrapper bGraphWrapper;
- for(tie(verIter.first,verIter.second)=vertices(gMaingraph)
- ;verIter.first!=verIter.second;++verIter.first)
- {
- verAffected = *verIter.first;
- double dXCoordV = bGraphWrapper.getVertexCenterCoordX(verAffected,gMaingraph);
- double dYCoordV = bGraphWrapper.getVertexCenterCoordY(verAffected,gMaingraph);
- for(tie(verIterEnd.first,verIterEnd.second)=vertices(gMaingraph);
- verIterEnd.first != verIterEnd.second;++verIterEnd.first)
- {
- verVisitor = *verIterEnd.first;
- double dXCoordU = bGraphWrapper.getVertexCenterCoordX(verVisitor,gMaingraph);
- double dYCoordU = bGraphWrapper.getVertexCenterCoordY(verVisitor,gMaingraph);
- if(verAffected == verVisitor)
- {
- continue;
- }
- // Logic for vertex size here
- double dWidthU = bGraphWrapper.getVertexWidth(verVisitor,gMaingraph);
- double dHeightU = bGraphWrapper.getVertexHeight(verVisitor,gMaingraph);
- double dWidthV = bGraphWrapper.getVertexWidth(verAffected,gMaingraph);
- double dHeightV = bGraphWrapper.getVertexHeight(verAffected,gMaingraph );
- double dXLeftTopU = dXCoordU - dWidthU/2 -5;
- double dXLeftTopV = dXCoordV - dWidthV/2 -5;
- double dYLeftTopU = dYCoordU - dHeightU/2 -5;
- double dYLeftTopV = dYCoordV - dHeightV/2 -5;
- if( (dXLeftTopV >= dXLeftTopU && dXLeftTopV <= dXLeftTopU+ dWidthU)||
- (dXLeftTopV <= dXLeftTopU && dXLeftTopV+ dWidthV >= dXLeftTopU))
- {
- // x intercept
- if((dYLeftTopV >= dYLeftTopU && dYLeftTopU + dHeightU >= dYLeftTopV)
- || (dYLeftTopU >= dYLeftTopV && dYLeftTopV + dHeightV >= dYLeftTopU) )
- {
- // check if already added
- if(!vOverLappingVertices.contains(verIterEnd.first))
- {
- vOverLappingVertices.push_back(verIterEnd.first);
- }
- if(!vOverLappingVertices.contains(verIter.first))
- {
- vOverLappingVertices.push_back(verIter.first);
- }
- }
- }
- }
- }
- return vOverLappingVertices;
- }
- GraphLayoutErrorCodes::LayoutErrorCode VertexOverlapRemoval::removeOverlaps(QString sInputGraphMLFilePath,
- QString sOutputGraphMLFilePath) //SubGraph& gMaingraph)
- {
- GraphLayoutErrorCodes::LayoutErrorCode enVertexOverlapErrorCode =
- GraphLayoutErrorCodes::LAYOUT_SUCCESS;
- if( (sInputGraphMLFilePath.isEmpty() == true))
- {
- enVertexOverlapErrorCode = GraphLayoutErrorCodes::INVALID_FILE_NAME;
- }
- if((sInputGraphMLFilePath.trimmed().endsWith(GRAPHML, Qt::CaseInsensitive) == false)
- && (enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
- {
- enVertexOverlapErrorCode = GraphLayoutErrorCodes::UNSUPPORTED_FILE_FORMAT;
- }
- if((QFile::exists(sInputGraphMLFilePath) == false) && (enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
- {
- enVertexOverlapErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
- }
- // create global graph pointer.
- SubGraph *gMaingraph = NULL ;
- if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
- {
- // read input GraphML file
- QFile inFile(sInputGraphMLFilePath);
- GraphMLReader reader;
- try
- {
- SubGraph &gInputGraph = reader.readGraphML(&inFile);
- gMaingraph = &(gInputGraph);
- inFile.close();
- }
- catch(LayoutFileIOException& eException)
- {
- inFile.close();
- //We return the file exception enums
- enVertexOverlapErrorCode = (GraphLayoutErrorCodes::LayoutErrorCode)eException.getErrorCode();
- }
- }
- cout<<"\n \n *********In Overlap Removal ******** \n \n";
- if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
- {
- do
- {
- QVector<VertexIterator> vOverLappingVertices = getOverlappingVertices(*gMaingraph);
- m_iVertexOverlapConter++;
- cout<<"\n Counter "<<m_iVertexOverlapConter;
- cout<<"\n Overlapping Vertices are : "<< vOverLappingVertices.size();
- cout<<"\n \n \n";
- VertexDescriptor verOther;
- VertexIterPair verIterInner;
- BoostGraphWrapper bGraphWrapper;
- if(!vOverLappingVertices.size() == 0)
- {
- for(int index =0 ; index < vOverLappingVertices.size();index++)
- {
- for(int innerIndex = 0 ; innerIndex < vOverLappingVertices.size(); innerIndex++)
- {
- if(innerIndex == index)
- {
- continue;
- }
- VertexIterator vVertexOuterIterator = vOverLappingVertices.at(index);
- VertexIterator vVertexInnerIterator = vOverLappingVertices.at(innerIndex);
- VertexDescriptor vVertexOuter = *vVertexOuterIterator;
- VertexDescriptor vVertexInner = *vVertexInnerIterator;
- if(vVertexInner == vVertexOuter)
- {
- continue;
- }
- if(bIsOverlappingVertices(vVertexOuterIterator,vVertexInnerIterator,*gMaingraph))
- {
- // define a moement vector
- int iMovementVectorX = 0;
- int iMovementVectorY = 0;
- double dXCoordV = bGraphWrapper.getVertexCenterCoordX(vVertexOuter,*gMaingraph);
- double dYCoordV = bGraphWrapper.getVertexCenterCoordY(vVertexOuter,*gMaingraph);
- double dXCoordU = bGraphWrapper.getVertexCenterCoordX(vVertexInner,*gMaingraph);
- double dYCoordU = bGraphWrapper.getVertexCenterCoordY(vVertexInner,*gMaingraph);
- iMovementVectorX = dXCoordV - dXCoordU;
- iMovementVectorY = dYCoordV - dYCoordU;
- double dDistance = iMovementVectorX*iMovementVectorX +
- iMovementVectorY*iMovementVectorY;
- dDistance = sqrt(dDistance);
- if(dDistance == 0)
- {
- dDistance = 0.001; //avoid nan
- }
- iMovementVectorX = 200*iMovementVectorX/dDistance;
- iMovementVectorY = 200*iMovementVectorY/dDistance;
- //
- for(tie(verIterInner.first,verIterInner.second)=vertices(*gMaingraph);
- verIterInner.first != verIterInner.second;++verIterInner.first)
- {
- // repel with all other vertices
- verOther = *verIterInner.first;
- double dXCoordOther = bGraphWrapper.getVertexCenterCoordX(verOther,*gMaingraph);
- double dYCoordOther = bGraphWrapper.getVertexCenterCoordY(verOther,*gMaingraph);
- double dDeltaX = dXCoordV - dXCoordOther;
- double dDeltaY = dYCoordV - dYCoordOther;
- double dDistanceOther = dDeltaX*dDeltaX +
- dDeltaY*dDeltaY;
- if(dDistanceOther == 0)
- {
- dDistanceOther = 0.001;
- }
- iMovementVectorX += 20*dDeltaX/dDistanceOther;
- iMovementVectorY += 20*dDeltaY/dDistanceOther;
- }
- //
- dXCoordV += 1*iMovementVectorX;
- dYCoordV += 1*iMovementVectorY;
- // move
- bGraphWrapper.setVertexCenterCoordX(vVertexOuter,*gMaingraph,(int)dXCoordV);
- bGraphWrapper.setVertexCenterCoordY(vVertexOuter,*gMaingraph,(int)dYCoordV);
- }
- }
- }
- }
- }
- while(bOverlapExist(*gMaingraph)&& m_iVertexOverlapConter < 5000);
- // while overlap
- Reingold resetClusterProps;
- resetClusterProps.setCompartMentProps(*gMaingraph,10);
- }
- if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
- {
- // write file to output path
- QFile outFile(sOutputGraphMLFilePath);
- if ((!outFile.open(QFile::WriteOnly | QFile::Truncate)))
- {
- enVertexOverlapErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
- }
- if(enVertexOverlapErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
- {
- GraphMLWriter writer;
- try
- {
- writer.writeGraphml(*gMaingraph , &outFile);
- outFile.flush();
- outFile.close();
- }
- catch(...)
- {
- outFile.close();
- DELETE_AND_SET_NULL(gMaingraph);
- enVertexOverlapErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
- outFile.close();
- }
- }
- }
- Postprocessing postProcess; //Remove clusters if there are
- postProcess.checkClusterOverlap(*gMaingraph);
- return enVertexOverlapErrorCode;
- }
- bool VertexOverlapRemoval::bIsOverlappingVertices(VertexIterator vVertexFirstIterator
- ,VertexIterator vVertexSecondIterator
- ,SubGraph& gMaingraph)
- {
- // return true if vertices overlap
- bool bOverlap = false;
- BoostGraphWrapper bGraphWrapper;
- VertexDescriptor vVertexFirst = *vVertexFirstIterator;
- VertexDescriptor vVertexSecond = *vVertexSecondIterator;
- double dXCoordV = bGraphWrapper.getVertexCenterCoordX(vVertexFirst,gMaingraph);
- double dYCoordV = bGraphWrapper.getVertexCenterCoordY(vVertexFirst,gMaingraph);
- double dXCoordU = bGraphWrapper.getVertexCenterCoordX(vVertexSecond,gMaingraph);
- double dYCoordU = bGraphWrapper.getVertexCenterCoordY(vVertexSecond,gMaingraph);
- double dWidthU = bGraphWrapper.getVertexWidth(vVertexSecond,gMaingraph);
- double dHeightU = bGraphWrapper.getVertexHeight(vVertexSecond,gMaingraph);
- double dWidthV = bGraphWrapper.getVertexWidth(vVertexFirst,gMaingraph);
- double dHeightV = bGraphWrapper.getVertexHeight(vVertexFirst,gMaingraph );
- double dXLeftTopU = dXCoordU - dWidthU/2 - 5;
- double dXLeftTopV = dXCoordV - dWidthV/2 - 5;
- double dYLeftTopU = dYCoordU - dHeightU/2 -5;
- double dYLeftTopV = dYCoordV - dHeightV/2 - 5;
- if( (dXLeftTopV >= dXLeftTopU && dXLeftTopV <= dXLeftTopU+ dWidthU)||
- (dXLeftTopV <= dXLeftTopU && dXLeftTopV+ dWidthV >= dXLeftTopU))
- {
- // x intercept
- if((dYLeftTopV >= dYLeftTopU && dYLeftTopU + dHeightU >= dYLeftTopV)
- || (dYLeftTopU >= dYLeftTopV && dYLeftTopV + dHeightV >= dYLeftTopU))
- {
- bOverlap = true;
- }
- }
- return bOverlap;
- }
- bool VertexOverlapRemoval::bOverlapExist(SubGraph& gMaingraph)
- {
- // returns true if graph has overlappng vertices.
- bool bOverlap = false;
- VertexDescriptor verAffected,verVisitor;
- VertexIterPair verIter,verIterEnd;
- BoostGraphWrapper bGraphWrapper;
- for(tie(verIter.first,verIter.second)=vertices(gMaingraph)
- ;verIter.first!=verIter.second;++verIter.first)
- {
- verAffected = *verIter.first;
- double dXCoordV = bGraphWrapper.getVertexCenterCoordX(verAffected,gMaingraph);
- double dYCoordV = bGraphWrapper.getVertexCenterCoordY(verAffected,gMaingraph);
- for(tie(verIterEnd.first,verIterEnd.second)=vertices(gMaingraph);
- verIterEnd.first != verIterEnd.second;++verIterEnd.first)
- {
- verVisitor = *verIterEnd.first;
- double dXCoordU = bGraphWrapper.getVertexCenterCoordX(verVisitor,gMaingraph);
- double dYCoordU = bGraphWrapper.getVertexCenterCoordY(verVisitor,gMaingraph);
- if(verAffected == verVisitor)
- {
- continue;
- }
- // Logic for vertex size here
- double dWidthU = bGraphWrapper.getVertexWidth(verVisitor,gMaingraph);
- double dHeightU = bGraphWrapper.getVertexHeight(verVisitor,gMaingraph);
- double dWidthV = bGraphWrapper.getVertexWidth(verAffected,gMaingraph);
- double dHeightV = bGraphWrapper.getVertexHeight(verAffected,gMaingraph );
- double dXLeftTopU = dXCoordU - dWidthU/2 - 5;
- double dXLeftTopV = dXCoordV - dWidthV/2 - 5;
- double dYLeftTopU = dYCoordU - dHeightU/2 - 5;
- double dYLeftTopV = dYCoordV - dHeightV/2 -5;
- if( (dXLeftTopV >= dXLeftTopU && dXLeftTopV <= dXLeftTopU+ dWidthU)||
- (dXLeftTopV <= dXLeftTopU && dXLeftTopV+ dWidthV >= dXLeftTopU))
- {
- // x intercept
- if((dYLeftTopV >= dYLeftTopU && dYLeftTopU + dHeightU >= dYLeftTopV)
- || (dYLeftTopU >= dYLeftTopV && dYLeftTopV + dHeightV >= dYLeftTopU) )
- {
- return true;
- }
- }
- }
- }
- return bOverlap;
- }
|