Ejemplo n.º 1
0
// keys_edit: Keys for the 2d editor
// ------------------------------ >>
void keys_edit()
{
    if (!map.opened)
        return;

    // Scroll up
    if (binds.pressed("view_up"))
    {
        yoff += ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Scroll down
    if (binds.pressed("view_down"))
    {
        yoff -= ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Scroll left
    if (binds.pressed("view_left"))
    {
        xoff += ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Scroll right
    if (binds.pressed("view_right"))
    {
        xoff -= ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Zoom in
    if (binds.pressed("view_zoomin"))
        view_zoom(true);

    // Zoom out
    if (binds.pressed("view_zoomout"))
        view_zoom(false);

    // Center view on mouse
    if (binds.pressed("view_mousecenter"))
    {
        xoff = -m_x(mouse.x) / MAJOR_UNIT;
        yoff = -m_y(mouse.y) / MAJOR_UNIT;
        force_map_redraw(true, true);
    }

    // Set offsets to 0, 0
    if (binds.pressed("view_origin"))
    {
        xoff = yoff = 0;
        force_map_redraw(true, true);
    }

    // Vertices mode
    if (binds.pressed("mode_vertices"))
        change_edit_mode(0);

    // Linedefs mode
    if (binds.pressed("mode_linedefs"))
        change_edit_mode(1);

    // Sectors mode
    if (binds.pressed("mode_sectors"))
        change_edit_mode(2);

    // Things mode
    if (binds.pressed("mode_things"))
        change_edit_mode(3);

    // Change mode
    if (binds.pressed("mode_change"))
        cycle_edit_mode();

    // Increase grid size
    if (binds.pressed("view_increasegrid"))
    {
        increase_grid();
        force_map_redraw(false, true);
    }

    // Decrease grid size
    if (binds.pressed("view_decreasegrid"))
    {
        decrease_grid();
        force_map_redraw(false, true);
    }

    // Clear selection
    if (binds.pressed("edit_clearselection"))
    {
        clear_selection();
        force_map_redraw(true);
    }

    // Delete item
    if (binds.pressed("edit_deleteitem"))
    {
        if (edit_mode == 0)
            delete_vertex();

        if (edit_mode == 1)
            delete_line();

        if (edit_mode == 2)
            delete_sector();

        if (edit_mode == 3)
            delete_thing();

        force_map_redraw(true);
    }

    // Create item
    if (binds.pressed("edit_createitem"))
    {
        if (edit_mode == 0)
        {
            if (!selection())
                create_vertex();
            else
                create_lines(false);

            force_map_redraw(true);
            return;
        }

        if (edit_mode == 1)
        {
            if (selection())
                create_sector();

            force_map_redraw(true);
            return;
        }

        if (edit_mode == 3)
        {
            create_thing();
            force_map_redraw(true);
            return;
        }

        binds.clear("edit_createitem");
    }

    // Sector height quick changes (8 units)
    if (binds.pressed("sector_upfloor8"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, 8);
    }

    if (binds.pressed("sector_downfloor8"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, -8);
    }

    if (binds.pressed("sector_upceil8"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, 8);
    }

    if (binds.pressed("sector_downceil8"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, -8);
    }

    if (binds.pressed("sector_upboth8"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, 8);
            sector_changeheight(false, 8);
        }
    }

    if (binds.pressed("sector_downboth8"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, -8);
            sector_changeheight(false, -8);
        }
    }

    // Sector height quick changes (1 unit)
    if (binds.pressed("sector_upfloor"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, 1);
    }

    if (binds.pressed("sector_downfloor"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, -1);
    }

    if (binds.pressed("sector_upceil"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, 1);
    }

    if (binds.pressed("sector_downceil"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, -1);
    }

    if (binds.pressed("sector_upboth"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, 1);
            sector_changeheight(false, 1);
        }
    }

    if (binds.pressed("sector_downboth"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, -1);
            sector_changeheight(false, -1);
        }
    }

    // Flip line
    if (binds.pressed("line_flip"))
    {
        if (edit_mode == 1)
            line_flip(true, false);

        force_map_redraw(true);
    }

    // Swap line sides
    if (binds.pressed("line_swapsides"))
    {
        if (edit_mode == 1)
            line_flip(false, true);

        force_map_redraw(true);
    }

    // Flip both line direction and sides
    if (binds.pressed("line_flipboth"))
    {
        if (edit_mode == 1)
            line_flip(true, true);

        force_map_redraw(true);
    }

    // Begin line draw
    if (binds.pressed("line_begindraw"))
    {
        if (!line_draw)
            line_draw = true;

        binds.clear("line_begindraw");
    }

    // Begin rectangle draw
    if (binds.pressed("line_begindraw_rect"))
    {
        if (!line_draw)
        {
            line_draw = true;
            sel_box.set(mouse.x, mouse.y, mouse.x, mouse.y);
        }

        binds.clear("line_begindraw_rect");
    }

    // Undo
    if (binds.pressed("edit_undo"))
    {
        undo();
        clear_selection();
        hilight_item = -1;
        force_map_redraw(true, true);
        //map_changelevel(3);
        map.change_level(MC_NODE_REBUILD);
        binds.clear("edit_undo");
    }

    // Edit item
    if (binds.pressed("edit_edititem"))
    {
        edit_item();
        binds.clear("edit_edititem");
    }

    // Merge sectors
    if (binds.pressed("sector_merge"))
    {
        sector_merge(false);
        binds.clear("sector_merge");
    }

    // Join sectors
    if (binds.pressed("sector_join"))
    {
        sector_merge(true);
        binds.clear("sector_join");
    }

    if (binds.pressed("view_3dmode"))
    {
        binds.clear("view_3dmode");
        binds.clear("3d_exit");
        start_3d_mode();
    }

    if (binds.pressed("open_console"))
    {
        binds.clear("open_console");
        popup_console();
    }

    if (binds.pressed("copy"))
    {
        binds.clear("copy");
        clipboard.Copy();
    }

    if (binds.pressed("paste"))
    {
        binds.clear("paste");
        paste_mode = true;
        clear_selection();
    }

    if (binds.pressed("cancel_paste"))
    {
        binds.clear("cancel_paste");
        paste_mode = false;
        force_map_redraw(true, false);
    }
}
// Main program 
int main(){

	// Declaration of variables
	int i, j;
	double l;
	int max_grid_size; // Maximum grid size during the whole simulation 
	int grid_size; // Start grid size (Dynamic variable during the simulation)
	int grid_midpoint;
	double abs_diff; 
	double h_sq;
	int gamma;
	int nbr_computations; // Calculated how many Gauss-Seidel computations that are done

	// Initiation of variables
	abs_diff = 1.0; 
	l = 1.0;

	max_grid_size = 81;
	grid_size = 11; 

	grid_midpoint = (grid_size - 1)/2;
	h_sq = pow(l/(grid_size-1.0),2.0);
	gamma = 1;
	nbr_computations = 0;

	// Declaration of arrays
	double** u; // The potential
	double** rho; // The charge distribution

	u = (double**) malloc(max_grid_size * sizeof(double*));
	rho = (double**) malloc(max_grid_size * sizeof(double*));

	for(i = 0; i < max_grid_size; i++){
		u[i] = (double*) malloc(max_grid_size * sizeof(double));
		rho[i] = (double*) malloc(max_grid_size * sizeof(double));
	}

	// Initiation of arrays
	for(i = 0; i < max_grid_size; i++){
		for(j = 0; j < max_grid_size; j++){
			u[i][j] = 0.0;
			rho[i][j] = 0.0;			
		}
	}

	rho[grid_midpoint*4/5][grid_midpoint] = pow(h_sq,-1); //1/h^2
	rho[grid_midpoint*6/5][grid_midpoint] = pow(-h_sq,-1);

	// File to save data
	FILE *file;
	file = fopen("phi.data","w");

	// Run the full multigrid 
	while (grid_size < max_grid_size){
		while (abs_diff >= pow(10,-5)){		
			abs_diff = multigrid(u, rho, grid_size, gamma, &nbr_computations);
		}
		abs_diff = 1.0; 		
		
		//Increase gird size		
		grid_size = increase_grid(u, grid_size);
		
		// Initiate a new rho for the new gridsize
		grid_midpoint = (grid_size - 1)/2;
		h_sq = pow(l/(grid_size-1.0),2.0);

		for(i = 0; i < grid_size; i++){
			for(j = 0; j < grid_size; j++){
				rho[i][j] = 0.0;			
			}
		}

		rho[grid_midpoint*4/5][grid_midpoint] = pow(h_sq,-1); //1/h^2
		rho[grid_midpoint*6/5][grid_midpoint] = pow(-h_sq,-1);

	}
	//Run multigrid for the largest size
	abs_diff = 1.0; 
	while (abs_diff >= pow(10,-5)){		
		abs_diff = multigrid(u, rho, grid_size, gamma, &nbr_computations);
	}	
	

	printf("Gridsize: %i Computations: %i\n",grid_size, nbr_computations);
	// Print the final solution to a file
	for(i = 0; i < grid_size; i++){
		for(j = 0; j < grid_size; j++){
			fprintf(file, "%f \t", u[i][j]);
		}
		
		fprintf(file, "\n");
		
	}

	// Close file
	fclose(file);

	// Free allocated memory
	for(i = 0; i < grid_size; i++){
			free(u[i]); 
			free(rho[i]); 
	}

	free(u); free(rho); 
	u = NULL; rho = NULL;
}