//calculate the cost of a ray-traced line double CostmapModel::lineCost(int x0, int x1, int y0, int y1){ double line_cost = 0.0; double point_cost = -1.0; for( LineIterator line( x0, y0, x1, y1 ); line.isValid(); line.advance() ) { point_cost = pointCost( line.getX(), line.getY() ); //Score the current point if(point_cost < 0) return -1; if(line_cost < point_cost) line_cost = point_cost; } return line_cost; }
QgsGraph* RgShortestPathWidget::getPath( QgsPoint& p1, QgsPoint& p2 ) { if ( mFrontPointLineEdit->text().isNull() || mBackPointLineEdit->text().isNull() ) { QMessageBox::critical( this, tr( "Point not selected" ), tr( "First, select start and stop points." ) ); return NULL; } QgsGraphBuilder builder( mPlugin->iface()->mapCanvas()->mapRenderer()->destinationCrs(), mPlugin->iface()->mapCanvas()->mapRenderer()->hasCrsTransformEnabled(), mPlugin->topologyToleranceFactor() ); { const QgsGraphDirector *director = mPlugin->director(); if ( director == NULL ) { QMessageBox::critical( this, tr( "Plugin isn't configured" ), tr( "Plugin isn't configured!" ) ); return NULL; } connect( director, SIGNAL( buildProgress( int, int ) ), mPlugin->iface()->mainWindow(), SLOT( showProgress( int, int ) ) ); connect( director, SIGNAL( buildMessage( QString ) ), mPlugin->iface()->mainWindow(), SLOT( showStatusMessage( QString ) ) ); QVector< QgsPoint > points; QVector< QgsPoint > tiedPoint; points.push_back( mFrontPoint ); points.push_back( mBackPoint ); director->makeGraph( &builder, points, tiedPoint ); p1 = tiedPoint[ 0 ]; p2 = tiedPoint[ 1 ]; // not need delete director; } if ( p1 == QgsPoint( 0.0, 0.0 ) ) { QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Start point doesn't tie to the road!" ) ); return NULL; } if ( p2 == QgsPoint( 0.0, 0.0 ) ) { QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Stop point doesn't tie to the road!" ) ); return NULL; } QgsGraph *graph = builder.graph(); QVector< int > pointIdx( 0, 0 ); QVector< double > pointCost( 0, 0.0 ); int startVertexIdx = graph->findVertex( p1 ); int criterionNum = 0; if ( mCriterionName->currentIndex() > 0 ) criterionNum = 1; QgsGraph* shortestpathTree = QgsGraphAnalyzer::shortestTree( graph, startVertexIdx, criterionNum ); delete graph; if ( shortestpathTree->findVertex( p2 ) == -1 ) { QMessageBox::critical( this, tr( "Path not found" ), tr( "Path not found" ) ); return NULL; } return shortestpathTree; }
//calculate the cost of a ray-traced line double CostmapModel::lineCost(int x0, int x1, int y0, int y1){ //Bresenham Ray-Tracing int deltax = abs(x1 - x0); // The difference between the x's int deltay = abs(y1 - y0); // The difference between the y's int x = x0; // Start x off at the first pixel int y = y0; // Start y off at the first pixel int xinc1, xinc2, yinc1, yinc2; int den, num, numadd, numpixels; double line_cost = 0.0; double point_cost = -1.0; if (x1 >= x0) // The x-values are increasing { xinc1 = 1; xinc2 = 1; } else // The x-values are decreasing { xinc1 = -1; xinc2 = -1; } if (y1 >= y0) // The y-values are increasing { yinc1 = 1; yinc2 = 1; } else // The y-values are decreasing { yinc1 = -1; yinc2 = -1; } if (deltax >= deltay) // There is at least one x-value for every y-value { xinc1 = 0; // Don't change the x when numerator >= denominator yinc2 = 0; // Don't change the y for every iteration den = deltax; num = deltax / 2; numadd = deltay; numpixels = deltax; // There are more x-values than y-values } else // There is at least one y-value for every x-value { xinc2 = 0; // Don't change the x for every iteration yinc1 = 0; // Don't change the y when numerator >= denominator den = deltay; num = deltay / 2; numadd = deltax; numpixels = deltay; // There are more y-values than x-values } for (int curpixel = 0; curpixel <= numpixels; curpixel++) { point_cost = pointCost(x, y); //Score the current point if(point_cost < 0) return -1; if(line_cost < point_cost) line_cost = point_cost; num += numadd; // Increase the numerator by the top of the fraction if (num >= den) // Check if numerator >= denominator { num -= den; // Calculate the new numerator value x += xinc1; // Change the x as appropriate y += yinc1; // Change the y as appropriate } x += xinc2; // Change the x as appropriate y += yinc2; // Change the y as appropriate } return line_cost; }