/** * yangruiguo 2011-12-27 * num : the number of low images' cluster for restore * lcluster: the low images' clusters * l_mrf : the low images' MRF * return : 0 success */ int replaceLMRF_Store(LL num, L_cluster* lcluster, IMAGE_L_MRF *l_mrf) { LL i = 0; int r = 0; int t = 0; int j = 0; int m = 0; int n = 0; int min = 0; for(m = 0; m < l_mrf->xnum; m++) { for(n = 0; n < l_mrf->ynum; n++) { r = lcluster[0].l; min = countDistance(l_mrf->label[m * l_mrf->ynum + n], lcluster[0].l); for(i = 1; i < num; i++) { t = countDistance(l_mrf->label[m * l_mrf->ynum + n], lcluster[j].l); if(t < min) { r = lcluster[j].l; min = t; } if(min == 0) { break; } } } } return 0; }
std::list<nodePosition> AStar::find() { std::list<Node *> neighbourOfCurrent; std::list<Node *>::iterator niterator, findCloseIterator, findOpernIterator; Node *tempNode; int newG; do { current = findLowestFinOpenList(); if(current == NULL) break; openList.remove(current); closeList.push_back(current); neighbourOfCurrent.clear(); neighbourOfCurrent = findNeighboursOfCurrent(); for(niterator = neighbourOfCurrent.begin(); niterator != neighbourOfCurrent.end(); ++niterator) { tempNode = *niterator; // find if neighbur is in closeList findCloseIterator = std::find(closeList.begin(), closeList.end(), tempNode); if(findCloseIterator != closeList.end()) { // yep, there is neighbour in close list continue; } else { // nope there is no neighbour in closeList findOpernIterator = std::find(openList.begin(), openList.end(), tempNode); newG = current->getG() + countDistance(current->getPosition(), tempNode->getPosition()); // new G for this neighbour if(findOpernIterator != openList.end()) { // neighbour is in open list, so we nieed to update distance from start node (if needed) if(tempNode->getStatusG() == false || newG < tempNode->getG()) { tempNode->setG(newG); tempNode->countF(); tempNode->setParent(current); } } else { // ther is no neighbour in open list, so we nieed to add it tempNode->setG(newG); tempNode->countF(); tempNode->setParent(current); openList.push_back(tempNode); } } } } while(current != end); return route(end); }
void AStar::setStartEnd(int startR, int startC, int endR, int endC) { // set values to start and end nodes. init two necessary points start = &allNodes[startR][startC]; end = &allNodes[endR][endC]; start->setPosition(startR, startC); end->setPosition(endR, endC); start->setG(0); start->setH(countDistance(start->getPosition(), end->getPosition())); start->countF(); start->setParent(NULL); current = start; end->setG(countDistance(start->getPosition(), end->getPosition())); end->setH(0); end->countF(); end->setParent(NULL); openList.push_back(current); // initializa openList with start node }
// selectin all neighbours of current node std::list<Node *> AStar::findNeighboursOfCurrent() { std::list<Node *> neighbours; Node *n; nodePosition currentPos = current->getPosition(); int maxCol = currentPos.col+1, minCol = currentPos.col - 1, maxRow = currentPos.row+1, minRow = currentPos.row -1; // check if is in the edge if(currentPos.col == 0) { // first column minCol = currentPos.col; } else if(currentPos.col == (cols-1)) { // last column maxCol = currentPos.col; } if(currentPos.row == 0) { // first row minRow = currentPos.row; } else if(currentPos.row == (rows-1)) { // last row maxRow = currentPos.row; } for(int r = minRow; r <= maxRow; r++) { for(int c = minCol; c <= maxCol; c++) { if(currentPos.row == r && currentPos.col == c) { continue; } // create new neigbour n = &allNodes[r][c]; if(n->getStatusH() == false) { // First use of that element n->setPosition(r, c); n->setH(countDistance(n->getPosition(), end->getPosition())); } neighbours.push_back(n); //qDebug() << n->getG() << allNodes[r][c].getG(); } } return neighbours; }
// return minimum distance from start to end int AStar::countDistance(nodePosition startNode, nodePosition endNode) { // integer distance between each node. In diagonal is 14 (sqrt 2 * 10) in normal it is 1 * 10; // multiply by 10 because we want to have an integer value int diagonalVal = 14, normalVal = 10; if(startNode.col == endNode.col && startNode.row == endNode.row) { // start = end return 0; // distance is 0 } else if(startNode.col == endNode.col) { // same column, calculate only distance in a row return abs(startNode.row - endNode.row) * normalVal; } else if(startNode.row == endNode.row) { // same row, calculate distance in col return abs(startNode.col - endNode.col) * normalVal; } else { // we have some diagonal movement neccessary int diaginalIteration = 0, rowStep = 1, colStep = 1; // determine of the sing of step (go up or down or left or right) if(startNode.row > endNode.row) rowStep *= -1; if(startNode.col > endNode.col) colStep *= -1; do { // count how many diagonal movement we do ++diaginalIteration; // calculate "new" start position startNode.col += colStep; startNode.row += rowStep; } while(startNode.col != endNode.col && startNode.row != endNode.row); // do it as lon as we will be in the sam row or column // recurency, use this function to calculate te distans in the same row or column // plus distance in diagonal steps return countDistance(startNode, endNode) + (diaginalIteration * diagonalVal); } }