int main(void) { float positions[3] = {0, 0, 0}, colors[3] = {0.5, 0.5, 0.5}, radii[1] = {2}; gr3_clear(); gr_setviewport(0.1, 0.95, 0.1, 0.95); gr3_setbackgroundcolor(1, 1, 1, 1); gr3_drawspheremesh(1, positions, colors, radii); gr3_drawimage(0, 1, 0, 1, 500, 500, GR3_DRAWABLE_GKS); gr_axes(0.04, 0.04, 0, 0, 5, 5, -0.01); gr_settextalign(2, 4); gr_text(0.525, 0.95, "GR3 Demo"); gr3_export("gr3demo.png", 500, 500); gr_updatews(); }
int main(void) { int mesh = 0; int nx = 93; int ny = 64; int nz = 64; unsigned int stride_x = 1; unsigned int stride_y = nx; unsigned int stride_z = nx*ny; double step_x = 2.0/(nx-1); double step_y = 2.0*ny/nx/(ny-1)*1.5; double step_z = 2.0*nz/nx/(nz-1)*1.5; double offset_x = -step_x * (nx-1) / 2; double offset_y = -step_y * (ny-1) / 2; double offset_z = -step_z * (nz-1) / 2; float position[3] = {0, 0, 0}; float direction[3] = {0, 0, 1}; float up[3] = {0, 1, 0}; float color[3] = {1, 1, 1}; float scales[3] = {1, 1, 1}; GR3_MC_DTYPE *data = malloc(nx * ny * nz * sizeof(GR3_MC_DTYPE)); FILE *fp = fopen("mri.raw", "rb"); assert(fp); assert(data); fread(data, 2, nx*ny*nz, fp); fclose(fp); { int ix, iy, iz; float input_max = 2000; GR3_MC_DTYPE dtype_max = (1UL << (8*sizeof(GR3_MC_DTYPE)))-1; for (iz = 0; iz < nz; iz++) { for (iy = 0; iy < ny; iy++) { for (ix = 0; ix < nx; ix++) { if (data[nx*nz*iz+nx*iy+ix] > input_max) { data[nx*nz*iz+nx*iy+ix] = input_max; } data[nx*ny*iz+nx*iy+ix] = (GR3_MC_DTYPE)(data[nx*nz*iz+nx*iy+ix]/input_max*dtype_max); } } } } gr_setviewport(0, 1, 0, 1); gr_setcolormap(1); gr3_init(NULL); gr3_createisosurfacemesh(&mesh, data, 40000, nx, ny, nz, stride_x, stride_y, stride_z, step_x, step_y, step_z, offset_x, offset_y, offset_z); gr3_cameralookat(-3, 3, -4, 0, 0, 0, -1, 0, 0); { int i; for (i = 0; i < 200; i++) { gr3_clear(); gr3_drawmesh(mesh, 1, position, direction, up, color, scales); gr3_drawxslicemesh(data, i*nx/199, nx, ny, nz, stride_x, stride_y, stride_z, step_x, step_y, step_z, offset_x, offset_y, offset_z); gr3_drawyslicemesh(data, i*ny/199, nx, ny, nz, stride_x, stride_y, stride_z, step_x, step_y, step_z, offset_x, offset_y, offset_z); gr3_drawzslicemesh(data, i*nz/199, nx, ny, nz, stride_x, stride_y, stride_z, step_x, step_y, step_z, offset_x, offset_y, offset_z); gr_clearws(); gr3_drawimage(0, 1, 0, 1, 500, 500, GR3_DRAWABLE_GKS); gr_updatews(); } } gr_setcolormap(19); { int i; for (i = 299; i >= 0; i--) { gr3_clear(); gr3_drawmesh(mesh, 1, position, direction, up, color, scales); gr3_drawxslicemesh(data, i*nx/299, nx, ny, nz, stride_x, stride_y, stride_z, step_x, step_y, step_z, offset_x, offset_y, offset_z); gr_clearws(); gr3_drawimage(0, 1, 0, 1, 500, 500, GR3_DRAWABLE_GKS); gr_updatews(); } for (i = 0; i < 300; i++) { gr3_clear(); gr3_drawmesh(mesh, 1, position, direction, up, color, scales); gr3_drawyslicemesh(data, i*ny/299, nx, ny, nz, stride_x, stride_y, stride_z, step_x, step_y, step_z, offset_x, offset_y, offset_z); gr_clearws(); gr3_drawimage(0, 1, 0, 1, 500, 500, GR3_DRAWABLE_GKS); gr_updatews(); } for (i = 299; i >= 0; i--) { gr3_clear(); gr3_drawmesh(mesh, 1, position, direction, up, color, scales); gr3_drawzslicemesh(data, i*nz/299, nx, ny, nz, stride_x, stride_y, stride_z, step_x, step_y, step_z, offset_x, offset_y, offset_z); gr_clearws(); gr3_drawimage(0, 1, 0, 1, 500, 500, GR3_DRAWABLE_GKS); gr_updatews(); } } gr3_deletemesh(mesh); free(data); gr3_terminate(); return 0; }
void moldyn_update_graphics(void) { double x, y, z, fx, fy, fz, ux, uy, uz; double c1 = cos(torad(-rotation)); double s1 = sin(torad(-rotation)); double c2 = cos(torad(-tilt)); double s2 = sin(torad(-tilt)); /* Transform the view */ x = -c1 * xeye + s1 * zeye; y = s1 * s2 * xeye - c2 * yeye + s2 * c1 * zeye; z = -c2 * s1 * xeye - s2 * yeye - c2 * c1 * zeye; fx = x + s1; fy = y + s2 * c1; fz = z - c2 * c1; ux = 0; uy = c2; uz = s2; gr3_setcameraprojectionparameters(45, 0.5, 7.0 * range); gr3_cameralookat(x, y, z, fx, fy, fz, ux, uy, uz); if (file_done) { /* TODO: disable NEXT in a better way */ last_init_done = True; } gr3_clear(); if (num_atoms > 0) { if (colors) { gr3_drawspheremesh(num_atoms, atom_positions, atom_colors, atom_radii); } else { float *atom_color_replacement = (float *)malloc(num_atoms*sizeof(float)*3); if (!atom_color_replacement) { moldyn_error("Failed to allocate memory for atom_color_replacement."); } else { int i; for (i = 0; i < num_atoms*3; i++) { atom_color_replacement[i] = 1.0f; } gr3_drawspheremesh(num_atoms, atom_positions, atom_color_replacement, atom_radii); free(atom_color_replacement); } } } if (format == xyz) { int i; int num_spins = 0; float spin_len = 1; float spintop_len = 0.4; float *spin_positions; float *spin_directions; float *spin_colors; float *spin_radii; float *spin_lengths; for (i = 0; i < num_atoms; i++) { if (atom_spins[3*i+0] != 0 || atom_spins[3*i+1] != 0 || atom_spins[3*i+2] != 0) { num_spins++; } } spin_positions = (float *) malloc(sizeof(float) * 3 * num_spins); spin_directions = (float *) malloc(sizeof(float) * 3 * num_spins); spin_colors = (float *) malloc(sizeof(float) * 3 * num_spins); spin_radii = (float *) malloc(sizeof(float) * num_spins); spin_lengths = (float *) malloc(sizeof(float) * num_spins); for (i = 0; i < num_atoms; i++) { if (atom_spins[3*i+0] != 0 || atom_spins[3*i+1] != 0 || atom_spins[3*i+2] != 0) { spin_positions[3*i+0] = atom_positions[3*i+0] - atom_spins[3*i+0]/2*spin_len; spin_positions[3*i+1] = atom_positions[3*i+1] - atom_spins[3*i+1]/2*spin_len; spin_positions[3*i+2] = atom_positions[3*i+2] - atom_spins[3*i+2]/2*spin_len; spin_directions[3*i+0] = atom_spins[3*i+0]; spin_directions[3*i+1] = atom_spins[3*i+1]; spin_directions[3*i+2] = atom_spins[3*i+2]; spin_colors[3*i+0] = 1; spin_colors[3*i+1] = 1; spin_colors[3*i+2] = 1; spin_lengths[i] = spin_len; spin_radii[i] = cyl_rad; } } gr3_drawcylindermesh(num_spins, spin_positions, spin_directions, spin_colors, spin_radii, spin_lengths); for (i = 0; i < num_atoms; i++) { if (atom_spins[3*i+0] != 0 || atom_spins[3*i+1] != 0 || atom_spins[3*i+2] != 0) { spin_positions[3*i+0] = atom_positions[3*i+0] + atom_spins[3*i+0]/2*spin_len; spin_positions[3*i+1] = atom_positions[3*i+1] + atom_spins[3*i+1]/2*spin_len; spin_positions[3*i+2] = atom_positions[3*i+2] + atom_spins[3*i+2]/2*spin_len; spin_directions[3*i+0] = atom_spins[3*i+0]; spin_directions[3*i+1] = atom_spins[3*i+1]; spin_directions[3*i+2] = atom_spins[3*i+2]; spin_colors[3*i+0] = 1; spin_colors[3*i+1] = 1; spin_colors[3*i+2] = 1; spin_lengths[i] = spintop_len; spin_radii[i] = 2*cyl_rad; } } gr3_drawconemesh(num_spins, spin_positions, spin_directions, spin_colors, spin_radii, spin_lengths); free(spin_positions); free(spin_directions); free(spin_colors); free(spin_radii); free(spin_lengths); } if (bonds) { int i, j, k, l; double vx, vy, vz, cyl_len; int num_bonds = 0; float *bond_positions; float *bond_directions; float *bond_colors; float *bond_radii; float *bond_lengths; for (i = 0; i < num_atoms; i++) { if (atom_numbers[i] == 0) continue; for (j = i + 1; j < num_atoms; j++) { if (atom_numbers[j] && atom_adjacency_matrix[i * num_atoms + j]) { num_bonds++; } } } bond_positions = (float *) malloc(sizeof(float) * 3 * num_bonds); bond_directions = (float *) malloc(sizeof(float) * 3 * num_bonds); bond_colors = (float *) malloc(sizeof(float) * 3 * num_bonds); bond_radii = (float *) malloc(sizeof(float) * num_bonds); bond_lengths = (float *) malloc(sizeof(float) * num_bonds); for (i = 0, j = 0, l = 0; i < num_atoms; i++) { if (atom_numbers[i] == 0) continue; for (j = i + 1; j < num_atoms; j++) { if (atom_numbers[j] && atom_adjacency_matrix[i * num_atoms + j]) { if (atom_positions[2+3*j] > atom_positions[2+3*i]) { vx = atom_positions[0+3*j] - atom_positions[0+3*i]; vy = atom_positions[1+3*j] - atom_positions[1+3*i]; vz = atom_positions[2+3*j] - atom_positions[2+3*i]; k = i; } else { vx = atom_positions[0+3*i] - atom_positions[0+3*j]; vy = atom_positions[1+3*i] - atom_positions[1+3*j]; vz = atom_positions[2+3*i] - atom_positions[2+3*j]; k = j; } cyl_len = sqrt(vx * vx + vy * vy + vz * vz); bond_positions[3 * l + 0] = atom_positions[0+3*k]; bond_positions[3 * l + 1] = atom_positions[1+3*k]; bond_positions[3 * l + 2] = atom_positions[2+3*k]; bond_directions[3 * l + 0] = vx; bond_directions[3 * l + 1] = vy; bond_directions[3 * l + 2] = vz; bond_lengths[l] = cyl_len; bond_radii[l] = cyl_rad; bond_colors[3 * l + 0] = 1; bond_colors[3 * l + 1] = 1; bond_colors[3 * l + 2] = 1; l++; } } } gr3_drawcylindermesh(num_bonds, bond_positions, bond_directions, bond_colors, bond_radii, bond_lengths); free(bond_positions); free(bond_directions); free(bond_colors); free(bond_radii); free(bond_lengths); } }