std::vector<float> Accessibility::findNearestPOIs(int srcnode, float maxradius, unsigned number, unsigned cat, int gno) { assert(cat >= 0 && cat < POI_MAXVAL); DistanceMap distances = ga[gno]->NearestPOI(cat,srcnode,maxradius, number, omp_get_thread_num()); std::vector<float> ret; accessibility_vars_t &vars = accessibilityVarsForPOIs[cat]; /* need to account for the possibility of having multiple locations at single node */ for (DistanceMap::const_iterator itDist = distances.begin(); itDist != distances.end(); ++itDist) { int nodeid = itDist->first; double distance = itDist->second; for(int i = 0 ; i < vars[nodeid].size() ; i++) { if(vars[nodeid][i] == 0) continue; ret.push_back((float)distance); } } std::sort(ret.begin(),ret.end()); return ret; }
/** Print a distance map. */ static void printDistanceMap(ostream& out, const Graph& g, const ContigNode& u, const ContigPath& path) { typedef map<ContigNode, int> DistanceMap; DistanceMap distanceMap = makeDistanceMap(g, u, path); for (DistanceMap::const_iterator it = distanceMap.begin(); it != distanceMap.end(); ++it) out << get(edge_name, g, make_pair(u, it->first)) << " [d=" << it->second << "]\n"; }
// does the dynamic programming search void MapTools::search(DistanceMap & dmap,const int sR,const int sC) { // reset the internal variables resetFringe(); // set the starting position for this search dmap.setStartPosition(sR,sC); // set the distance of the start cell to zero dmap[getIndex(sR,sC)] = 0; // set the fringe variables accordingly int fringeSize(1); int fringeIndex(0); _fringe[0] = getIndex(sR,sC); dmap.addSorted(getTilePosition(_fringe[0])); // temporary variables used in search loop int currentIndex,nextIndex; int newDist; // the size of the map int size = _rows*_cols; // while we still have things left to expand while (fringeIndex < fringeSize) { // grab the current index to expand from the fringe currentIndex = _fringe[fringeIndex++]; newDist = dmap[currentIndex] + 1; // search up nextIndex = (currentIndex > _cols) ? (currentIndex - _cols) : -1; if (unexplored(dmap,nextIndex)) { // set the distance based on distance to current cell dmap.setDistance(nextIndex,newDist); dmap.setMoveTo(nextIndex,'D'); dmap.addSorted(getTilePosition(nextIndex)); // put it in the fringe _fringe[fringeSize++] = nextIndex; } // search down nextIndex = (currentIndex + _cols < size) ? (currentIndex + _cols) : -1; if (unexplored(dmap,nextIndex)) { // set the distance based on distance to current cell dmap.setDistance(nextIndex,newDist); dmap.setMoveTo(nextIndex,'U'); dmap.addSorted(getTilePosition(nextIndex)); // put it in the fringe _fringe[fringeSize++] = nextIndex; } // search left nextIndex = (currentIndex % _cols > 0) ? (currentIndex - 1) : -1; if (unexplored(dmap,nextIndex)) { // set the distance based on distance to current cell dmap.setDistance(nextIndex,newDist); dmap.setMoveTo(nextIndex,'R'); dmap.addSorted(getTilePosition(nextIndex)); // put it in the fringe _fringe[fringeSize++] = nextIndex; } // search right nextIndex = (currentIndex % _cols < _cols - 1) ? (currentIndex + 1) : -1; if (unexplored(dmap,nextIndex)) { // set the distance based on distance to current cell dmap.setDistance(nextIndex,newDist); dmap.setMoveTo(nextIndex,'L'); dmap.addSorted(getTilePosition(nextIndex)); // put it in the fringe _fringe[fringeSize++] = nextIndex; } } }
void TileGrid::recursivePathfind(TilePos point, int maxCost, PathMap& pmap, DistanceMap& dmap, bool diagonal) { // Get a list of the nearest neighbours and cache the current fastest // path to this point TilePosList nneighbor = nearestNeighbors(point); auto partialPath = pmap[point]; auto partialCost = dmap[point]; // Repeat the process for each of the nearest-neighbours of this point for (auto const &p : nneighbor) { // Is the candidate point diagonal? (diagonal paths take longer to walk) bool diagCopy = diagonal; int pathCost = 1; if (!point.isInLine(p)) { if (diagCopy) { pathCost++; } diagCopy = !diagCopy; } int adjustedCost = partialCost + pathCost; if (adjustedCost > maxCost) { // We can't get here taking this route - stop. continue; } // Do we already have this point in our map? auto it = dmap.find(p); if (it == dmap.end()) { // This point is not in our map - this is the fastest route here automatically pmap[p] = partialPath; pmap[p].push_back(p); dmap[p] = adjustedCost; } else { // This point is already in our map - is it faster? if (adjustedCost > it->second) { continue; } else { // Replace pmap[p] = partialPath; pmap[p].push_back(p); dmap[p] = adjustedCost; } } // RECURSE recursivePathfind(p, maxCost, pmap, dmap, diagCopy); } }