Ejemplo n.º 1
0
SegImage* MeanFiller::fillAlt(int startX, int startY, int startZ, int startRadius)
{
	timer.start("Fill Alt");
	int cols, rows, slices;
	cube sample;
	image->getSize(cols, rows, slices);
	cube result = zeros(rows, cols, slices);
	cube sphere = Utils::sphere(startRadius);
	result(startY, startX, startZ, arma::size(sphere)) = sphere;
	timer.stage("Init");
	res_image = Utils::convert(result);
	timer.stage("Convert");
	Utils::bounds(result, minx, miny, minz, maxx, maxy, maxz);
	timer.stage("Bounds");
	result.reset();
	history.clear();

	if (minx < 3) minx = 3;
	if (maxx > cols - 2) maxx = cols - 2;
	if (miny < 3) miny = 3;
	if (maxy > rows - 2) maxy = rows - 2;
	if (minz < 3) minz = 3;
	if (maxz > slices - 2) maxz = slices - 2;
	for (int i = minx; i <= maxx; i++)
	{
		for (int j = miny; j <= maxy; j++)
		{
			for (int k = minz; k <= maxz; k++)
			{
				addNeighbors(i, j, k);
			}
		}
	}
	timer.stage("First candidates");
	while (!candidates.empty()) {
		set<triple>::iterator last = --candidates.end();
		triple c = *last;
		candidates.erase(last);
		sample = wBright * Utils::convert_d(image->getRegion(c.x - 2, c.y - 2, c.x + 2, c.y + 2, c.z - 2, c.z + 2)) +
			wCurv * Utils::convert_d(res_image->getRegion(c.x - 2, c.y - 2, c.x + 2, c.y + 2, c.z - 2, c.z + 2));
		double der = derivate(sample);
		if (der > threshold)
		{
			res_image->setVoxel(c.x, c.y, c.z, 255);
			addNeighbors(c.x, c.y, c.z);
		}
	}
	timer.stop();
	return res_image;
}
Ejemplo n.º 2
0
std::vector<Cell> Pathfinding::getPath(Cell start, Cell end) {
    mOpenedNodes.clear();
    mClosedNodes.clear();

    mStart = start;
    mEnd = end;

    Node startNode{0, 0, 0, start};
    Cell current = start;

    mOpenedNodes[current] = startNode;
    addToClosedMap(current);
    addNeighbors(current);

    while((current != mEnd) && !mOpenedNodes.empty()) {
        /* on cherche le meilleur noeud de la liste ouverte, on sait qu'elle n'est pas vide donc il existe */
        current = getBestNode();

        /* on le passe dans la liste fermee, il ne peut pas déjà y être */
        addToClosedMap(current);

        /* on recommence la recherche des noeuds adjacents */
        addNeighbors(current);
    }

    std::vector<Cell> path;
    /* si la destination est atteinte, on remonte le chemin */
    if (current == end) {
        path = getPathFromClosedMap();
    } else{
        throw std::runtime_error("Impossible de trouver un chemin dans la grille");
    }


    return path;
}
Ejemplo n.º 3
0
bool OccupancyMap::nextNode(double max_occ_dist, Node *curr_node, bool allow_unknown) {
  if (!Q_->empty()) {
    // Copy node and then erase it
    *curr_node = *Q_->begin();
    int ci = curr_node->coord.first, cj = curr_node->coord.second;
    // fprintf(stderr, "At %i %i (cost = %6.2f)  % 7.2f % 7.2f \n",
    //     ci, cj, curr_node.true_dist, MAP_WXGX(map_, ci), MAP_WYGY(map_, cj));
    costs_[MAP_INDEX(map_, ci, cj)] = curr_node->true_cost;
    Q_->erase(Q_->begin());
    addNeighbors(*curr_node, max_occ_dist, allow_unknown);
    return true;
  } else {
    return false;
  }
}
Ejemplo n.º 4
0
    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
		//Set up
        unordered_set<string> foundWordsF, foundWordsB;
		unordered_set<string>* workingSet = &foundWordsF;
		unordered_set<string>* idleSet = &foundWordsB;
		queue<string> unexploredF;
		queue<string> unexploredB;
		queue<string> *unexplored = &unexploredF;
		queue<string> *unexploredIdle = &unexploredB;

		foundWordsF.insert(beginWord);
        foundWordsB.insert(endWord);
        unexplored->push(beginWord);
		unexploredIdle->push(endWord);


		int distance = 0;
		//Process

		//Removed elements from queue, add neighbors to queue, add element to foundWords
		while(!unexplored->empty())
		{
		    int endNum = unexplored->size();
			for(int i = 0; i < endNum; i++)
			{
				string newElement = unexplored->front();
				unexplored->pop();
				if(workingSet->find(newElement) == workingSet->end() || distance < 2)
                    addNeighbors(unexplored, newElement, &wordDict, workingSet);//could save time by checking to make sure neighbours
																//aren't in workingSet
				//If the current word is in the idle set, then we have found the path
				if(idleSet->find(newElement) != idleSet->end())
					return distance;
				else if(workingSet->find(newElement) == workingSet->end())
                {
					workingSet->insert(newElement);
                }
			}
			distance++;
			if(unexplored->size() >= unexploredIdle->size())
            {
                swap(workingSet, idleSet);
                swap(unexplored, unexploredIdle);
            }
		}
		return 0;

    }
Ejemplo n.º 5
0
void OrthographicGridPathfinder::buildPath(	list<PathNode> *pathToFill,
        float startX, float startY,
        float endX, float endY)
{
    // SO BUILD THE PATH
    pathToFill->clear();
    vector<bool> *pathGridPointer = &pathGrid;
    GameStateManager *gsm = game->getGSM();
    World *world = gsm->getWorld();
    int gridWidth = getGridWidth();
    int gridHeight = getGridHeight();

    // DETERMINE THE START COLUMN AND START ROW
    int startColumn = (int)(startX/gridWidth);
    int startRow = (int)(startY/gridHeight);

    // DETERMINE THE END COLUMN AND END ROW
    int endColumn = (int)(endX/gridWidth);
    int endRow = (int)(endY/gridHeight);

    // IF THE DESTINATION IS A COLLIDABLE TILE LOCATION
    // THEN EXIT
    int endIndex = getGridIndex(endColumn, endRow);
    bool endIndexIsWalkable = pathGrid[getGridIndex(endColumn, endRow)];
    if (!endIndexIsWalkable)
        return;

    map<int, PathNode> openNodes;
    map<int, PathNode> closedNodes;
    //	list<PathNode> openList;
    //	list<PathNode> closedList;
    bool pathFound = false;
    bool nodesToAdd[9];

    PathNode startNode;
    startNode.column = startColumn;
    startNode.row = startRow;
    startNode.parentNode = NULL;
    startNode.G = 0;

    PathNode endNode;
    endNode.column = endColumn;
    endNode.row = endRow;
    endNode.parentNode = NULL;
    startNode.H = calculateH(startNode, &endNode);
    //	openList.push_back(startNode);
    int nodeIndex = getGridIndex(startColumn, startRow);
    openNodes[nodeIndex] = startNode;

    while (!pathFound)
    {
        // IF THERE ARE NO MORE NODES TO SEARCH THROUGH TO FIND
        // OUR DESTINATION THEN WE'RE DONE
        //		if (openList.size() == 0)
        if (openNodes.size() == 0)
        {
            pathToFill->clear();
            return;
        }

        // FIRST GET THE CLOSEST NODE FROM THE OPEN LIST
        //		PathNode *foundNode = findCheapestNode(&openList);
        PathNode *foundNode = findCheapestNode(&openNodes);
        PathNode cheapestNode;
        cheapestNode.column = foundNode->column;
        cheapestNode.row = foundNode->row;
        cheapestNode.G = foundNode->G;
        cheapestNode.H = foundNode->H;
        cheapestNode.parentNode = foundNode->parentNode;
        //		removeNodeFromList(&cheapestNode, &openList);
        openNodes.erase(getGridIndex(cheapestNode.column, cheapestNode.row));

        // IS THE CHEAPEST NODE THE DESTINATION?
        if ((cheapestNode.column == endNode.column) && (cheapestNode.row == endNode.row))
        {
            // WE'VE REACHED THE DESTINATION
            pathFound = true;
            PathNode *traveller = &cheapestNode;
            while (traveller != NULL)
            {
                PathNode nodeToAdd;
                nodeToAdd.column = traveller->column;
                nodeToAdd.row = traveller->row;
                pathToFill->push_front(nodeToAdd);
                traveller = traveller->parentNode;
            }
        }
        else
        {
            // WE'LL NEED TO LOOK AT THE CHEAPEST NODE'S NEIGHBORS
            // FIRST LET'S PUT IT INTO THE CLOSED LIST SO WE DON'T
            // END UP IN AN INFINITELY CIRCULAR LOOP
            //			closedList.push_back(cheapestNode);
            closedNodes[getGridIndex(cheapestNode.column, cheapestNode.row)] = cheapestNode;
            //			PathNode *nodeJustAdded = &closedList.back();
            PathNode *nodeJustAdded = &closedNodes[getGridIndex(cheapestNode.column, cheapestNode.row)];

            // NOW FIGURE OUT WHICH NEIGHBORS MIGHT BE USABLE
            //			findNeighborsToCheck(world, nodesToAdd, nodeJustAdded, &closedList);
            findNeighborsToCheck(nodesToAdd, nodeJustAdded, &closedNodes);

            // NOW ADD THE NEIGHBORS TO OUR OPEN LIST
            //			addNeighbors(nodesToAdd, nodeJustAdded, &endNode, &openList, &closedList);
            addNeighbors(nodesToAdd, nodeJustAdded, &endNode, &openNodes, &closedNodes);
        }
    }
    PathNode lastNode = pathToFill->back();
    destinationPathfindingCell.setCenterX(getColumnCenterX(lastNode.column));
    destinationPathfindingCell.setCenterY(getRowCenterY(lastNode.row));
    destinationPathfindingCell.setWidth(this->getGridWidth());
    destinationPathfindingCell.setHeight(this->getGridHeight());
}
Ejemplo n.º 6
0
void MetalineEnemy::render()
{	
	glColor4f(0.5, 0.5, 0.5, 1.0);
	
	glMaterialfv(GL_FRONT, GL_AMBIENT, centerMaterialAmbient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, centerMaterialDiffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, centerMaterialSpecular);
    glMaterialfv(GL_FRONT, GL_SHININESS, &centerShininess);
	
	glPushMatrix();
	glTranslatef(center.x, center.y, center.z);
	glutSolidSphere(CENTER_SPHERE_RADIUS * this->radius, CENTER_SPHERE_SLICES, CENTER_SPHERE_STACKS);
	glPopMatrix();
	
	glColor4f(0.5, 0.5, 0.5, 1.0);
	
	glMaterialfv(GL_FRONT, GL_AMBIENT, blobMaterialAmbient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, blobMaterialDiffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, blobMaterialSpecular);
    glMaterialfv(GL_FRONT, GL_SHININESS, &blobShininess);
	
	glBegin(GL_TRIANGLES);
	
	memset(added, 0, addedSize3D * sizeof(added[0]));
	
	int curNeighborIndex = 0;
	int endNeighborIndex = 0;
	
	memset(fieldStrengths, 0, strengthSize3D * sizeof(fieldStrengths[0]));
	
	for (int i = 0; i < numBlobs; i++) {
		LineBlob curBlob = blobs[i];
		
		Index blobCubePosition = getCubePosition(curBlob.center);
		
		bool isComputed = false;
		bool found = false;
		
		for(;blobCubePosition.x < DEFAULT_NUM_CUBES_PER_DIMENSION; blobCubePosition.x++)
		{
			if (wasAdded(blobCubePosition))
			{
				isComputed = true;
				found = true;
				break;
			}
			else
			{
				if (renderCube(blobCubePosition))
				{
					found = true;
					break;
				}
			}
		}
		
		if (!isComputed)
		{
			
			addNeighbors(blobCubePosition, endNeighborIndex);
			
			while (curNeighborIndex < endNeighborIndex)
			{
				if (renderCube(neighbors[curNeighborIndex]))
				{
					addNeighbors(neighbors[curNeighborIndex], endNeighborIndex);
				}
				
				curNeighborIndex++;
			}
		}
	}
	
	glEnd();
}