| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <math.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <sys/types.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
|
| 9 |
#include "atom_parser.h" |
| 10 |
#include "pov_writer.h" |
| 11 |
#include "file_stuff.h" |
| 12 |
#define POV_DIR "./pov" |
| 13 |
|
| 14 |
struct linked_xyz{ |
| 15 |
struct coords *r; |
| 16 |
struct linked_xyz *next; |
| 17 |
}; |
| 18 |
|
| 19 |
char *program_name; /*the name of the program */ |
| 20 |
int draw_bonds = 0; /* boolean to draw bonds or not */ |
| 21 |
int draw_hydrogens = 1; /*boolean to draw hydrogens */ |
| 22 |
int draw_atoms = 1; /*boolean to draw atoms */ |
| 23 |
|
| 24 |
void usage(void); |
| 25 |
|
| 26 |
int main(argc, argv) |
| 27 |
int argc; |
| 28 |
char *argv[]; |
| 29 |
{ |
| 30 |
|
| 31 |
|
| 32 |
struct coords *out_coords; |
| 33 |
|
| 34 |
int i,j; /* loop counters */ |
| 35 |
mode_t dir_mode = S_IRWXU; |
| 36 |
|
| 37 |
int generate_header = 0; /* boolean for generating the pov ray header */ |
| 38 |
double big_x = 0; |
| 39 |
double big_y = 0; /* lets me know the biggest x y and z */ |
| 40 |
double big_z = 0; |
| 41 |
double small_x = 0; |
| 42 |
double small_y = 0; /* lets me know the smallest x, y, z */ |
| 43 |
double small_z = 0; |
| 44 |
double rsqr; /* the square of the diagonal */ |
| 45 |
double diagonal; /* the diagonal length of the sim box */ |
| 46 |
|
| 47 |
unsigned int n_atoms; /*the number of atoms in each time step */ |
| 48 |
char read_buffer[120]; /*the line buffer for reading */ |
| 49 |
char *eof_test; /*ptr to see when we reach the end of the file */ |
| 50 |
char *foo; /*the pointer to the current string token */ |
| 51 |
FILE *in_file; /* the input file */ |
| 52 |
FILE *out_file; /*the output file */ |
| 53 |
char *out_prefix = NULL; /*the prefix of the output file */ |
| 54 |
char out_name[500]; /*the output name */ |
| 55 |
char temp_name[500]; |
| 56 |
char *in_name = NULL; /*the name of the input file */ |
| 57 |
unsigned int n_out = 0; /*keeps track of which output file is being written*/ |
| 58 |
|
| 59 |
struct linked_xyz *current_frame; |
| 60 |
struct linked_xyz *temp_frame; |
| 61 |
|
| 62 |
unsigned int n_interpolate = 0; /* number of frames to interpolate */ |
| 63 |
double dx, dy, dz; /* temp variables for interpolating distances */ |
| 64 |
|
| 65 |
char pov_dir[500]; /* the pov_dir */ |
| 66 |
|
| 67 |
program_name = argv[0]; /*save the program name in case we need it*/ |
| 68 |
|
| 69 |
for( i = 0; i < argc; i++){ |
| 70 |
|
| 71 |
if(argv[i][0] =='-'){ |
| 72 |
|
| 73 |
/* argv[i][1] is the actual option character */ |
| 74 |
|
| 75 |
switch(argv[i][1]){ |
| 76 |
|
| 77 |
/* -H => generate header */ |
| 78 |
|
| 79 |
case 'H': |
| 80 |
generate_header = 1; |
| 81 |
break; |
| 82 |
|
| 83 |
/* -i # => the number of frames to interpolate. |
| 84 |
* [i+1] actually starts the name |
| 85 |
*/ |
| 86 |
|
| 87 |
case 'i': |
| 88 |
(void)sscanf(argv[i+1],"%d", &n_interpolate); |
| 89 |
break; |
| 90 |
|
| 91 |
/* -f <name> => the xyz input file |
| 92 |
* [i+1] actually starts the name |
| 93 |
*/ |
| 94 |
|
| 95 |
case 'f': |
| 96 |
in_name = argv[i+1]; |
| 97 |
break; |
| 98 |
|
| 99 |
/* -o <name> => the orientation file prefix |
| 100 |
* [i+1] actually starts the name |
| 101 |
*/ |
| 102 |
|
| 103 |
case 'o': |
| 104 |
out_prefix = argv[i+1]; |
| 105 |
break; |
| 106 |
|
| 107 |
default: |
| 108 |
(void)fprintf(stderr, "Bad option %s\n", argv[i]); |
| 109 |
usage(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if(in_name == NULL){ |
| 115 |
usage(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
in_file = fopen(in_name, "r"); |
| 121 |
if(in_file == NULL){ |
| 122 |
printf("Cannot open file: %s\n", in_name); |
| 123 |
exit(8); |
| 124 |
} |
| 125 |
|
| 126 |
if(out_prefix == NULL){ |
| 127 |
out_prefix = strtok(in_name, "."); |
| 128 |
} |
| 129 |
|
| 130 |
if(access(POV_DIR, F_OK)){ |
| 131 |
/*create the pov directory*/ |
| 132 |
mkdir(POV_DIR, dir_mode); |
| 133 |
} |
| 134 |
strcpy(pov_dir, POV_DIR); strcat(pov_dir, "/"); |
| 135 |
|
| 136 |
|
| 137 |
// initialize atom type parser |
| 138 |
|
| 139 |
initializeParser(); |
| 140 |
|
| 141 |
// start reading the first frame |
| 142 |
|
| 143 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 144 |
|
| 145 |
current_frame = (struct linked_xyz *)malloc(sizeof(struct linked_xyz)); |
| 146 |
current_frame->next = NULL; |
| 147 |
|
| 148 |
|
| 149 |
while(eof_test != NULL){ |
| 150 |
|
| 151 |
(void)sscanf(read_buffer, "%d", &n_atoms); |
| 152 |
current_frame->r = |
| 153 |
(struct coords *)calloc(n_atoms, sizeof(struct coords)); |
| 154 |
|
| 155 |
/*read and toss the comment line */ |
| 156 |
|
| 157 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 158 |
if(eof_test == NULL){ |
| 159 |
printf("error in reading file\n"); |
| 160 |
exit(8); |
| 161 |
} |
| 162 |
|
| 163 |
for( i=0; i < n_atoms; i++){ |
| 164 |
|
| 165 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 166 |
if(eof_test == NULL){ |
| 167 |
printf("error in reading file\n"); |
| 168 |
exit(8); |
| 169 |
} |
| 170 |
|
| 171 |
foo = strtok(read_buffer, " ,;\t"); |
| 172 |
(void)strcpy(current_frame->r[i].name, foo); /*copy the atom name */ |
| 173 |
|
| 174 |
/* next we grab the positions */ |
| 175 |
|
| 176 |
foo = strtok(NULL, " ,;\t"); |
| 177 |
if(foo == NULL){ |
| 178 |
printf("error in reading file\n"); |
| 179 |
exit(8); |
| 180 |
} |
| 181 |
(void)sscanf(foo, "%lf",¤t_frame->r[i].x); |
| 182 |
if(current_frame->r[i].x > big_x) big_x = current_frame->r[i].x; |
| 183 |
if(current_frame->r[i].x < small_x) small_x = current_frame->r[i].x; |
| 184 |
|
| 185 |
|
| 186 |
foo = strtok(NULL, " ,;\t"); |
| 187 |
if(foo == NULL){ |
| 188 |
printf("error in reading file\n"); |
| 189 |
exit(8); |
| 190 |
} |
| 191 |
(void)sscanf(foo, "%lf", ¤t_frame->r[i].y); |
| 192 |
if(current_frame->r[i].y > big_y) big_y = current_frame->r[i].y; |
| 193 |
if(current_frame->r[i].y < small_y) small_y = current_frame->r[i].y; |
| 194 |
|
| 195 |
|
| 196 |
foo = strtok(NULL, " ,;\t"); |
| 197 |
if(foo == NULL){ |
| 198 |
printf("error in reading file\n"); |
| 199 |
exit(8); |
| 200 |
} |
| 201 |
(void)sscanf(foo, "%lf", ¤t_frame->r[i].z); |
| 202 |
if(current_frame->r[i].z > big_z) big_z = current_frame->r[i].z; |
| 203 |
if(current_frame->r[i].z < small_z) small_z = current_frame->r[i].z; |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
if(n_interpolate && current_frame->next != NULL){ |
| 208 |
|
| 209 |
temp_frame = current_frame->next; |
| 210 |
|
| 211 |
for(i = 0; i < n_interpolate; i++){ |
| 212 |
|
| 213 |
/* open the new output file */ |
| 214 |
|
| 215 |
strcpy(out_name, pov_dir); |
| 216 |
make_filename(temp_name, out_prefix, n_out); |
| 217 |
strcat(out_name, temp_name); strcat(out_name, ".pov"); |
| 218 |
out_file = fopen(out_name, "w"); |
| 219 |
n_out++; |
| 220 |
if(out_file == NULL){ |
| 221 |
printf("error opening output file: %s\n", out_name); |
| 222 |
exit(8); |
| 223 |
} |
| 224 |
(void)fprintf(out_file, |
| 225 |
"// The following script was automatically generated by:\n" |
| 226 |
"// xyz2pov Copyright 2001 by MATTHEW A. MEINEKE\n" |
| 227 |
"\n" |
| 228 |
"#include \"pov_header.pov\"\n" |
| 229 |
"\n"); |
| 230 |
|
| 231 |
out_coords = |
| 232 |
(struct coords *)calloc(n_atoms, sizeof(struct coords)); |
| 233 |
|
| 234 |
for(j=0; j < n_atoms; j++){ |
| 235 |
dx = current_frame->r[j].x - temp_frame->r[j].x; |
| 236 |
dy = current_frame->r[j].y - temp_frame->r[j].y; |
| 237 |
dz = current_frame->r[j].z - temp_frame->r[j].z; |
| 238 |
|
| 239 |
dx /= (double)(n_interpolate + 1); |
| 240 |
dy /= (double)(n_interpolate + 1); |
| 241 |
dz /= (double)(n_interpolate + 1); |
| 242 |
|
| 243 |
strcpy(out_coords[j].name, temp_frame->r[j].name); |
| 244 |
out_coords[j].x = temp_frame->r[j].x + dx * (i+1); |
| 245 |
out_coords[j].y = temp_frame->r[j].y + dy * (i+1); |
| 246 |
out_coords[j].z = temp_frame->r[j].z + dz * (i+1); |
| 247 |
} |
| 248 |
|
| 249 |
pov_write(out_file, out_coords, n_atoms, draw_hydrogens, draw_bonds, |
| 250 |
draw_atoms); |
| 251 |
free(out_coords); |
| 252 |
(void)fclose(out_file); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/* open the new output file */ |
| 257 |
|
| 258 |
strcpy(out_name, pov_dir); |
| 259 |
make_filename(temp_name, out_prefix, n_out); |
| 260 |
strcat(out_name, temp_name); strcat(out_name, ".pov"); |
| 261 |
out_file = fopen(out_name, "w"); |
| 262 |
n_out++; |
| 263 |
if(out_file == NULL){ |
| 264 |
printf("error opening output file: %s\n", out_name); |
| 265 |
exit(8); |
| 266 |
} |
| 267 |
(void)fprintf(out_file, |
| 268 |
"// The following script was automatically generated by:\n" |
| 269 |
"// xyz2pov Copyright 2001 by MATTHEW A. MEINEKE\n" |
| 270 |
"\n" |
| 271 |
"#include \"pov_header.pov\"\n" |
| 272 |
"\n"); |
| 273 |
|
| 274 |
out_coords = |
| 275 |
(struct coords *)calloc(n_atoms, sizeof(struct coords)); |
| 276 |
|
| 277 |
for(i = 0; i < n_atoms; i++){ |
| 278 |
strcpy(out_coords[i].name, current_frame->r[i].name); |
| 279 |
out_coords[i].x = current_frame->r[i].x; |
| 280 |
out_coords[i].y = current_frame->r[i].y; |
| 281 |
out_coords[i].z = current_frame->r[i].z; |
| 282 |
} |
| 283 |
pov_write(out_file, out_coords, n_atoms, draw_hydrogens, draw_bonds, |
| 284 |
draw_atoms); |
| 285 |
free(out_coords); |
| 286 |
|
| 287 |
(void)fclose(out_file); |
| 288 |
|
| 289 |
/*free up memory */ |
| 290 |
|
| 291 |
temp_frame = current_frame->next; |
| 292 |
current_frame->next = NULL; |
| 293 |
|
| 294 |
if(temp_frame != NULL){ |
| 295 |
|
| 296 |
free(temp_frame->r); |
| 297 |
free(temp_frame); |
| 298 |
} |
| 299 |
|
| 300 |
/* make a new frame */ |
| 301 |
|
| 302 |
temp_frame = (struct linked_xyz *)malloc(sizeof(struct linked_xyz)); |
| 303 |
temp_frame->next = current_frame; |
| 304 |
current_frame = temp_frame; |
| 305 |
|
| 306 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
(void)fclose(in_file); |
| 311 |
|
| 312 |
|
| 313 |
if(generate_header){ |
| 314 |
|
| 315 |
dx = big_x - small_x; |
| 316 |
dy = big_y - small_y; |
| 317 |
dz = big_z - small_z; |
| 318 |
|
| 319 |
rsqr = dx * dx + dy * dy + dz * dz; |
| 320 |
diagonal = sqrt(rsqr); |
| 321 |
diagonal *= 0.5; |
| 322 |
|
| 323 |
dx /= 2.0; |
| 324 |
dy /= 2.0; |
| 325 |
dz /= 2.0; |
| 326 |
|
| 327 |
dx += small_x; |
| 328 |
dy += small_y; |
| 329 |
dz += small_z; |
| 330 |
|
| 331 |
|
| 332 |
/*note the y and z axis is exchanged for the different coordinate |
| 333 |
system in pov-ray*/ |
| 334 |
|
| 335 |
|
| 336 |
out_file = fopen("pov_header.pov", "w"); |
| 337 |
|
| 338 |
fprintf(out_file, |
| 339 |
"// The following script was automatically generated by:\n" |
| 340 |
"// xyz2pov Copyright 2001 by MATTHEW A. MEINEKE\n" |
| 341 |
"\n" |
| 342 |
"\n" |
| 343 |
"background { rgb <1.0, 1.0, 1.0> }\n" |
| 344 |
"\n" |
| 345 |
"\n" |
| 346 |
); |
| 347 |
|
| 348 |
fprintf(out_file, |
| 349 |
"//******************************************************\n" |
| 350 |
"// Declare the resolution, camera, and light sources.\n" |
| 351 |
"//******************************************************\n" |
| 352 |
"\n" |
| 353 |
"// NOTE: if you plan to render at a different resoltion,\n" |
| 354 |
"// be sure to update the following two lines to maintain\n" |
| 355 |
"// the correct aspect ratio.\n" |
| 356 |
"\n" |
| 357 |
"#declare Width = 640.0;\n" |
| 358 |
"#declare Height = 480.0;\n" |
| 359 |
"#declare Ratio = Width / Height;\n" |
| 360 |
"\n" |
| 361 |
"#declare zoom = %lf;\n" |
| 362 |
"\n" |
| 363 |
"#declare ATOM_SPHERE_FACTOR = 0.2;\n" |
| 364 |
"#declare BOND_RADIUS = 0.1;\n" |
| 365 |
"\n" |
| 366 |
"camera{\n" |
| 367 |
" location < %lf, %lf, %lf - zoom>\n" |
| 368 |
" right < Ratio , 0, 0>\n" |
| 369 |
" look_at < %lf, %lf, %lf >\n" |
| 370 |
"}\n" |
| 371 |
"\n", |
| 372 |
diagonal, |
| 373 |
dx, dz, dy, |
| 374 |
dx, dz, dy); |
| 375 |
|
| 376 |
fprintf(out_file, |
| 377 |
"light_source{\n" |
| 378 |
" < %lf, %lf, %lf - zoom >\n" |
| 379 |
" rgb < 1.0, 1.0, 1.0 > }\n", |
| 380 |
dx, dz, dy); |
| 381 |
|
| 382 |
fprintf(out_file, |
| 383 |
"light_source{\n" |
| 384 |
" < %lf - zoom , %lf + zoom, %lf - zoom >\n" |
| 385 |
" rgb < 1.0, 1.0, 1.0 > }\n" |
| 386 |
"\n" |
| 387 |
"\n", |
| 388 |
dx, dz, dy); |
| 389 |
|
| 390 |
fprintf(out_file, |
| 391 |
"//************************************************************\n" |
| 392 |
"// Set whether or not to rotate the system.\n" |
| 393 |
"//\n" |
| 394 |
"// To Rotate, set ROTATE to 1.0 (true),\n" |
| 395 |
"// Then set the Euler Angles PHI, THETA, and PSI in degrees.\n" |
| 396 |
"//************************************************************\n" |
| 397 |
"\n" |
| 398 |
"#declare ROTATE = 0.0;\n" |
| 399 |
"#declare PHI = 0.0;\n" |
| 400 |
"#declare THETA = 0.0;\n" |
| 401 |
"#declare PSI = 0.0;\n" |
| 402 |
"\n" |
| 403 |
"#if(ROTATE)\n" |
| 404 |
" #declare phi_r = radians(PHI);\n" |
| 405 |
" #declare theta_r = radians(THETA);\n" |
| 406 |
" #declare psi_r = radians(PSI);\n" |
| 407 |
"\n" |
| 408 |
" #declare A11 = cos(phi_r) * cos(psi_r) - sin(phi_r) * cos(theta_r) * sin(psi_r);\n" |
| 409 |
" #declare A12 = sin(phi_r) * cos(psi_r) + cos(phi_r) * cos(theta_r) * sin(psi_r);\n" |
| 410 |
" #declare A13 = sin(theta_r) * sin(psi_r);\n" |
| 411 |
"\n" |
| 412 |
" #declare A21 = -cos(phi_r) * sin(psi_r) - sin(phi_r) * cos(theta_r) * cos(psi_r);\n" |
| 413 |
" #declare A22 = -sin(phi_r) * sin(psi_r) + cos(phi_r) * cos(theta_r) * cos(psi_r);\n" |
| 414 |
" #declare A23 = sin(theta_r) * cos(psi_r);\n" |
| 415 |
"\n" |
| 416 |
" #declare A31 = sin(phi_r) * sin(theta_r);\n" |
| 417 |
" #declare A32 = -cos(phi_r) * sin(theta_r);\n" |
| 418 |
" #declare A33 = cos(theta_r);\n" |
| 419 |
"\n" |
| 420 |
"#end\n" |
| 421 |
"\n"); |
| 422 |
|
| 423 |
|
| 424 |
make_header_macros(out_file); |
| 425 |
|
| 426 |
fclose(out_file); |
| 427 |
} |
| 428 |
|
| 429 |
return 0; |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
/*************************************************************************** |
| 436 |
* prints out the usage for the command line arguments, then exits. |
| 437 |
***************************************************************************/ |
| 438 |
|
| 439 |
void usage(){ |
| 440 |
(void)fprintf(stderr, |
| 441 |
"The proper usage is: %s [options] -f <xyz_file>\n\n" |
| 442 |
"Options:\n" |
| 443 |
" -o <name> the output file prefix\n" |
| 444 |
" -H generate a pov-ray header file\n" |
| 445 |
" -i # number of frames to interpolate\n", |
| 446 |
program_name); |
| 447 |
exit(8); |
| 448 |
} |