Example #1
0
void AbstractCell::rotateClockwise()
{
    Directions newdirs = None;
    if (m_cables & Up) newdirs = Directions(newdirs | Right);
    if (m_cables & Right) newdirs = Directions(newdirs | Down);
    if (m_cables & Down) newdirs = Directions(newdirs | Left);
    if (m_cables & Left) newdirs = Directions(newdirs | Up);
    m_cables = newdirs;
    m_hasBeenMoved = true;
}
Example #2
0
std::list<MatrixCoord> dumbPath(Matrix<Cell> &matrix, const MatrixCoord &start, const MatrixCoord &end)
{
	std::list<MatrixCoord> path;
	MatrixCoord matrixSize = matrix.size();
	Matrix<Directions> directionsMatrix(matrixSize);
	Directions alldir(1,1,1,1);

	MatrixCoord currentDir = alldir.dir[rand()%4];
	for (int i=0; i<matrixSize.x; ++i) {
		for (int j=0; j<matrixSize.y; ++j) {
			MatrixCoord ij(i, j);
			directionsMatrix[ij] = Directions(matrix[ij].up, matrix[ij].down, matrix[ij].right, matrix[ij].left);
		}
	} // directionsMatrix has, in each coord, the possible directionsMatrix from that coord
	path.clear();

	MatrixCoord currentCoord = start;
	while (!(currentCoord == end)) { // at each arrived coord
		path.push_back(currentCoord); // add it as arrived(stuff here only gets added(never removed), it makes the full back-and-forth trace)
		std::random_shuffle(directionsMatrix[currentCoord].dir.begin(), directionsMatrix[currentCoord].dir.end()); // get a coord from this one
		currentCoord += directionsMatrix[currentCoord].dir.back();
	}
	path.push_back(currentCoord);

	return path;
}
Example #3
0
	std::vector<vgl_point_2d<double> > ScannerImage(const vector<vgl_point_3d<double> > &Points, const vgl_point_3d<double> &ScannerLocation)
	{
		std::vector<vgl_point_3d<double> > Intersections(Points.size());
		
		//get all directions
		std::vector<vgl_vector_3d<double> > Directions(Points.size());
		for(unsigned int i = 0; i < Points.size(); i++)
		{
			Directions[i] = Points[i] - ScannerLocation;
		}
		
		vgl_vector_3d<double> ProjectionPlaneNormal = VXLHelpers::normalize(AverageDirection(Directions));
			
		vgl_point_3d<double> ProjectionPlanePoint = ScannerLocation + ProjectionPlaneNormal;
		
		//find the 3d coords of the points projected on the plane
		for(unsigned int i = 0; i < Points.size(); i++)
		{
			vgl_line_3d_2_points<double> Line(ScannerLocation, Points[i]);
	
			//vgl_plane_3d<double> Plane(ScannerForward, ScannerLocation + ScannerForward);
			vgl_plane_3d<double> Plane(ProjectionPlaneNormal, ProjectionPlanePoint);
	
			vgl_point_3d<double> Intersection = vgl_intersection(Line, Plane);
	
			Intersections[i] = Intersection;
		}
		
		vgl_vector_3d<double> b = VXLHelpers::normalize(Intersections[0] - ProjectionPlanePoint);
		
		//WriteAxisFile(vgl_point_3d<double> (0,0,0), cross_product(ProjectionPlaneNormal, b), b, ProjectionPlaneNormal, "BeforeAxis.vtp");
		
		vnl_double_3x3 R = FindAligningRotation(cross_product(ProjectionPlaneNormal, b), b, ProjectionPlaneNormal);
		
		/*
		//write out axes after transformation
		vnl_double_3 a1 = vnl_inverse(R) * vgl_vector_to_vnl_vector(cross_product(ProjectionPlaneNormal, b));
		vnl_double_3 a2 = vnl_inverse(R) * vgl_vector_to_vnl_vector(b);
		vnl_double_3 a3 = vnl_inverse(R) * vgl_vector_to_vnl_vector(ProjectionPlaneNormal);
		WriteAxisFile(vgl_point_3d<double> (0,0,0), vnl_vector_to_vgl_vector(a1), vnl_vector_to_vgl_vector(a2), vnl_vector_to_vgl_vector(a3), "AfterAxis.vtp");
		*/
		
		std::vector<vgl_point_2d<double> > Points2d(Points.size());
		
		for(unsigned int i = 0; i < Points.size(); i++)
		{
			vnl_double_3 temp = vnl_inverse(R) * VXLHelpers::vgl_point_to_vnl_vector(Intersections[i]);
			Points2d[i] = vgl_point_2d<double> (temp(0), temp(1));
		}
		
		return Points2d;
	}
Example #4
0
std::list<MatrixCoord> randomDFSPath(Matrix<Cell> &matrix, const MatrixCoord &start, const MatrixCoord &end)
{
	std::list<MatrixCoord> path;
	MatrixCoord matrixSize = matrix.size();
	Matrix<Directions> directionsMatrix(matrixSize);
	for (int i=0; i<matrixSize.x; ++i) {
		for (int j=0; j<matrixSize.y; ++j) {
			MatrixCoord ij(i, j);
			directionsMatrix[ij] = Directions(matrix[ij].up, matrix[ij].down, matrix[ij].right, matrix[ij].left);
		}
	} // directionsMatrix has, in each coord, the possible directionsMatrix from that coord
	path.clear();
	std::list<MatrixCoord> coordStack;

	MatrixCoord currentCoord = start;
	while (!(currentCoord == end)) { // at each arrived coord
		path.push_back(currentCoord); // add it as arrived(stuff here only gets added(never removed), it makes the full back-and-forth trace)
		// every coord I arrive must be added here
		if (directionsMatrix[currentCoord].dir.size() > 0) {
			std::random_shuffle(directionsMatrix[currentCoord].dir.begin(), directionsMatrix[currentCoord].dir.end()); // get a coord from this one
			MatrixCoord modifier = directionsMatrix[currentCoord].dir.back();
			directionsMatrix[currentCoord].dir.pop_back(); // erase the pointer from currentCoord to currentCoord+modifier

			const MatrixCoord modified = currentCoord + modifier; // just an alias to the next coordinate I will visit
			directionsMatrix[modified].dir.erase(std::find(directionsMatrix[modified].dir.begin(),
						directionsMatrix[modified].dir.end(),
						-modifier)); // erase the pointer to the current currentCoord from(currentCoord+modifier)

			coordStack.push_back(currentCoord); // add it to the stack, so that if I get to a dead end, I can get back
			currentCoord = modified;

		}
		else {
			currentCoord = coordStack.back(); // dead end, just get back to last one
			coordStack.pop_back();
		}
		//		std::cout << path << std::endl;
	}
	path.push_back(currentCoord);

	return path;
}
Example #5
0
// adds a random direction and moves on (if possible)
void AbstractGrid::addRandomCable(QList<uint>& list)
{
    int cell = list.first();
    // find all the cells surrounding list.first()
    // (0 when cells don't exist)
    int ucell = uCell(cell); // up
    int rcell = rCell(cell); // right
    int dcell = dCell(cell); // down
    int lcell = lCell(cell); // left

    QMap<Directions, int> freeCells;

    // of those cells map the ones that are free
    if (ucell != NO_CELL && m_cells[ucell]->cables() == None) {
        freeCells[Up] = ucell;
    }
    if (rcell != NO_CELL && m_cells[rcell]->cables() == None) {
        freeCells[Right] = rcell;
    }
    if (dcell != NO_CELL && m_cells[dcell]->cables() == None) {
        freeCells[Down] = dcell;
    }
    if (lcell != NO_CELL && m_cells[lcell]->cables() == None) {
        freeCells[Left] = lcell;
    }

    if (freeCells.isEmpty()) return; // no free cells left

    QMap<Directions, int>::ConstIterator it = freeCells.constBegin();
    // move the iterator to a random direction connecting to a free cell
    for (int i = qrand() % freeCells.count(); i > 0; --i) ++it;

    // add the cable in the direction of cell
    Directions newCables = Directions(m_cells[cell]->cables() | it.key());
    m_cells[cell]->setCables(newCables);
    // add a cable in the opposite direction, on the free cell
    m_cells[it.value()]->setCables(invertDirection(it.key()));
    // append the cell that was free to the list
    list.append(it.value());
}
Example #6
0
void Explosion::create(int _id) {
	if(id[_id] == -1) return;

	Explosion *newExplosion = nullptr;

	checkTile(ptr, id[_id], LayerType::LAYER_EXPLOSIONS, &Explosion::explosion);

	if(checkTile(ptr, id[_id], LayerType::LAYER_STONES, &Explosion::stone) || checkTile(ptr, id[_id], LayerType::LAYER_BOMBS, &Explosion::bomb))
		newExplosion = new Explosion(ptr, id[_id], delay + Constants::Explosion::DELAY, position[_id], explosionLength - 1, Directions::none);

	else if(!checkTile(ptr, id[_id], LayerType::LAYER_BLOCKS, &Explosion::block))
		newExplosion = new Explosion(ptr, id[_id], delay + Constants::Explosion::DELAY, position[_id], explosionLength - 1, Directions(_id));

	if(newExplosion != nullptr)
		ptr->world[ id[_id] ].insert( std::make_pair(LayerType::LAYER_EXPLOSIONS, newExplosion) );
}