Logo Search packages:      
Sourcecode: ktechlab version File versions  Download package

void ConRouter::mapRoute ( int  sx,
int  sy,
int  ex,
int  ey 
)

What this class is all about - finding a route, from (sx,sy) to (ex,ey).

Definition at line 351 of file conrouter.cpp.

References ItemDocument::canvas(), ICNDocument::cells(), checkLineRoute(), ICNDocument::isValidCellReference(), removeDuplicatePoints(), and Cells::reset().

Referenced by ICNDocument::createConnector(), Connector::rerouteConnector(), and NodeGroup::updateRoutes().

{
      const int scx = fromCanvas(sx);
      const int scy = fromCanvas(sy);
      const int ecx = fromCanvas(ex);
      const int ecy = fromCanvas(ey);
      
      if ( !p_icnDocument->isValidCellReference( scx, scy ) ||
             !p_icnDocument->isValidCellReference( ecx, ecy ) )
      {
            return;
      }
      
      m_cellPointList.clear();
      m_lcx = ecx;
      m_lcy = ecy;
      
      
      // First, lets try some common connector routes (which will not necesssarily
      // be shortest, but they will be neat, and cut down on overall CPU usage)
      // If that fails, we will resort to a shortest-route algorithm to find an
      // appropriate route.
      
      // Connector configuration: Line
      {
            bool ok = checkLineRoute( scx, scy, ecx, ecy, 4*ICNDocument::hs_connector, 0 );
            if (ok) {
                  return;
            } else {
                  m_cellPointList.clear();
            }
      }
      
      // Corner 1
      {
            bool ok = checkLineRoute( scx, scy, ecx, ecy, 2*ICNDocument::hs_connector, 0 );
            if (!ok) {
                  m_cellPointList.clear();
            } else {
                  ok = checkLineRoute( scx, scy, ecx, ecy, ICNDocument::hs_connector-1, 0 );
                  if (ok) {
                        return;
                  } else {
                        m_cellPointList.clear();
                  }
            }
      }
      
      // Corner 2
      {
            bool ok = checkLineRoute( scx, scy, ecx, ecy, 2*ICNDocument::hs_connector, 0 );
            if (!ok) {
                  m_cellPointList.clear();
            } else {
                  ok = checkLineRoute( scx, scy, ecx, ecy, ICNDocument::hs_connector-1, 0 );
                  if (ok) {
                        return;
                  } else {
                        m_cellPointList.clear();
                  }
            }
      }
      
      // It seems we must resort to brute-force route-checking
      {     
            cellsPtr = p_icnDocument->cells();
            cellsPtr->reset();
      
            xcells = p_icnDocument->canvas()->width()/8;
            ycells = p_icnDocument->canvas()->height()/8;
      
            // Now to map out the shortest routes to the cells
            Cell * const startCell = &(*cellsPtr)[ecx][ecy];
            startCell->permanent = true;
            startCell->bestScore = 0;
            startCell->prevX = -1;
            startCell->prevY = -1;
            
            tempLabels.clear();
            checkCell( ecx, ecy );
            
            // Daniel: I changed it from a do while to a while otherwise
            // in rare cases the iterator can end up as end().
            while ( tempLabels.size() > 0 && !(*cellsPtr)[scx][scy].permanent )
            {
                  TempLabelMap::iterator it = tempLabels.begin();
                  checkCell( it->second.x, it->second.y );
                  tempLabels.erase(it);
            }
            
            // Now, retrace the shortest route from the endcell to get out points :)
            int x = scx, y = scy;
            bool ok = true;
            do
            {
                  m_cellPointList.append( QPoint( x, y ) );
                  int newx = (*cellsPtr)[x][y].prevX;
                  int newy = (*cellsPtr)[x][y].prevY;
                  if ( newx == x && newy == y ) {
                        ok = false;
                  }
                  x = newx;
                  y = newy;
            }
            while ( p_icnDocument->isValidCellReference(x,y) && x != -1 && y != -1 && ok );
            
            // And append the last point...
            m_cellPointList.append( QPoint( ecx, ecy ) );
      }
      
      removeDuplicatePoints();
}


Generated by  Doxygen 1.6.0   Back to index