Пример #1
0
void LiveWireDP(int seedX, int seedY, Node* nodes, int width, int height, const unsigned char* selection, int numExpanded)
{
  CTypedPtrHeap<Node> pq;
  for (int i=0;i<width*height;i++){ nodes[i].state = INITIAL;  }
  NODE(nodes, seedX, seedY, width).totalCost = 0;
  NODE(nodes, seedX, seedY, width).prevNode = NULL;
  pq.Insert(&NODE(nodes,seedX,seedY,width));
  
  Node* cur;
  int dx, dy;
  while (!pq.IsEmpty() && numExpanded > 0){
    cur = pq.ExtractMin();
    cur->state = EXPANDED;
    for(int i=0;i<8;i++){
      cur->nbrNodeOffset( dx, dy, i);
      int x2 = cur->column+dx;
      int y2 = cur->row + dy;
      if (x2 > -1 && x2 < width && y2 > -1 && y2 < height && (selection == NULL || selection[y2*width+x2])){
        Node* n = &NODE(nodes, cur->column + dx, cur->row + dy, width);
        int v = cur->totalCost + cur->linkCost[i];
        switch(n->state){
        case INITIAL:
          n->totalCost = v;
          n->state = ACTIVE;
          n->prevNode = cur;
          pq.Insert(n);
          break;
        case ACTIVE:
          if (n->totalCost > v) {
            n->totalCost = v;
            n->prevNode = cur;
            pq.Update(n);
          }
          break;
        }
      }
    }
  }
}
void LiveWireDP(int seedX, int seedY, Node *nodes, int width, int height, const unsigned char *selection, int numExpanded)
{
	CTypedPtrHeap<Node> priorityQueue;
	Node *curNode = nodes + seedY*width + seedX;
	int expandedCount = 0;

	//Initialize all nodes
	for (int i = 0; i < width * height; i++)
	{
		nodes[i].state = INITIAL;
	}

	curNode->totalCost = 0.;
	curNode->prevNode = NULL;

	priorityQueue.Insert(curNode);
	while (!priorityQueue.IsEmpty())
	{
		// remove selected node from the running
		// and expand it
		curNode = priorityQueue.ExtractMin();
		curNode->state = EXPANDED;

		if (++expandedCount == numExpanded)
		{
			return;
		}

		//iterate across neighbors
		for (int linkIndex = 0; linkIndex < 8; linkIndex++)
		{
			int offsetX, offsetY;
			curNode->nbrNodeOffset(offsetX, offsetY, linkIndex);
			int xPosition = curNode->column + offsetX;
			int yPosition = curNode->row + offsetY;

			if (xPosition < 0 || xPosition >= width || yPosition < 0 || yPosition >= height)
			{
				continue;
			}
			if (selection == NULL || selection[yPosition*width+xPosition] == 1)
			{
				Node *tempNode = &nodes[yPosition*width+xPosition];
				if (tempNode->state == INITIAL)
				{
					tempNode->totalCost = curNode->totalCost + curNode->linkCost[linkIndex];
					tempNode->state = ACTIVE;
					tempNode->prevNode = curNode;
					priorityQueue.Insert(tempNode);
				}
				else if(tempNode->state == ACTIVE)
				{
					if (curNode->totalCost + curNode->linkCost[linkIndex] < tempNode->totalCost)
					{
						tempNode->prevNode = curNode;
						priorityQueue.Update(tempNode);
					}
				}
				else
				{
					continue;
				}
			}
		}
	}
}