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