ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/xyz2pov/src/pov_writer.c
Revision: 864
Committed: Tue Nov 18 17:04:25 2003 UTC (21 years, 5 months ago) by gezelter
Content type: text/plain
File size: 11435 byte(s)
Log Message:
Added Vectors

File Contents

# Content
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5
6 #include "pov_writer.h"
7 #include "atom_parser.h"
8
9 struct bond{
10 int i;
11 int j;
12 };
13
14 struct linked_bond_list{
15 struct bond the_bond;
16 struct linked_bond_list *next;
17 };
18
19 void make_bonds(struct coords *, int);
20
21 struct linked_bond_list *bl_head;
22
23 void clean_bonds(void);
24
25 void initBondList(void){
26 bl_head = NULL;
27 }
28
29 void pov_write(FILE *out_file, struct coords *the_coords, int n_atoms,
30 int d_hydrogens, int d_bonds, int d_atoms, int d_vectors){
31
32 int i,j; /*loop counters */
33 int skip_atom, skip_bond, test1, test2; /*booleans */
34 double dx, dy, dz; /* used in making the bonds */
35
36 struct linked_bond_list *current_bond; /*keeps track of the linked list*/
37
38 for(i = 0; i < n_atoms; i++){
39 update_types(the_coords[i].name);
40 }
41
42 if(d_atoms){
43
44 fprintf(out_file,
45 "//************************************************************\n"
46 "// The list of atoms\n"
47 "//************************************************************\n"
48 "\n"
49 "\n");
50
51 for(i = 0; i < n_atoms; i++){
52
53 skip_atom = 0;
54
55 if(!d_hydrogens){
56 skip_atom = !strcmp("H", the_coords[i].name);
57 }
58
59 if(!skip_atom){
60
61 fprintf(out_file,
62 "make_%s_atom( %lf, %lf, %lf )\n",
63 the_coords[i].name,
64 the_coords[i].x,
65 the_coords[i].z,
66 the_coords[i].y);
67 }
68 }
69
70 fprintf(out_file,
71 "\n"
72 "\n");
73 }
74
75
76 if (d_vectors) {
77
78 fprintf(out_file,
79 "//************************************************************\n"
80 "// The list of vectors\n"
81 "//************************************************************\n"
82 "\n"
83 "\n");
84
85 for(i = 0; i < n_atoms; i++){
86
87 if (the_coords[i].hasVector) {
88 fprintf(out_file,
89 "make_%s_vector(%lf, %lf, %lf, %lf, %lf, %lf)\n",
90 the_coords[i].name,
91 the_coords[i].x,
92 the_coords[i].z,
93 the_coords[i].y,
94 the_coords[i].ux,
95 the_coords[i].uz,
96 the_coords[i].uy);
97 }
98 }
99
100 fprintf(out_file,
101 "\n"
102 "\n");
103 }
104
105 if(d_bonds){
106
107 fprintf(out_file,
108 "//************************************************************\n"
109 "// The list of bonds\n"
110 "//************************************************************\n"
111 "\n"
112 "\n");
113
114 if( bl_head == NULL ) make_bonds(the_coords, n_atoms);
115
116 current_bond = bl_head->next;
117
118 while(current_bond != NULL){
119
120 skip_bond = 0;
121
122 i = current_bond->the_bond.i;
123 j = current_bond->the_bond.j;
124
125 if(!d_hydrogens){
126
127 test1 = !strcmp("H", the_coords[i].name);
128 test2 = !strcmp("H", the_coords[j].name);
129
130 skip_bond = (test1 || test2);
131 }
132
133 if(!skip_bond){
134
135 dx = (the_coords[j].x - the_coords[i].x) / 2.0;
136 dy = (the_coords[j].y - the_coords[i].y) / 2.0;
137 dz = (the_coords[j].z - the_coords[i].z) / 2.0;
138
139 fprintf(out_file,
140 "make_%s_bond( %lf, %lf, %lf, %lf, %lf, %lf )\n",
141 the_coords[i].name,
142 the_coords[i].x,
143 the_coords[i].z,
144 the_coords[i].y,
145 (the_coords[i].x + dx),
146 (the_coords[i].z + dz),
147 (the_coords[i].y + dy));
148
149 fprintf(out_file,
150 "make_%s_bond( %lf, %lf, %lf, %lf, %lf, %lf )\n",
151 the_coords[j].name,
152 the_coords[j].x,
153 the_coords[j].z,
154 the_coords[j].y,
155 (the_coords[j].x - dx),
156 (the_coords[j].z - dz),
157 (the_coords[j].y - dy));
158
159 fprintf(out_file, "\n");
160 }
161
162 current_bond = current_bond->next;
163 }
164
165 if( regenerateBonds )clean_bonds();
166 }
167 }
168
169
170 void make_bonds(struct coords *the_coords, int n_atoms){
171
172 int i, j; /*counters */
173 struct linked_bond_list *temp_bond; /*bond place holder */
174
175 const double bond_fudge = 1.12; // a fudge factor
176 struct atom type_i, type_j; /* holds the atom types */
177
178 int test; /* booleans */
179 double dx, dy, dz, dr2, dcv, dcv2; // used to determine bond existence
180
181
182 bl_head = (struct linked_bond_list *)malloc(sizeof(struct linked_bond_list));
183 bl_head->next = NULL;
184
185 for(i = 0; i < (n_atoms - 1); i++){
186
187 for(j = (i+1); j < n_atoms; j++){
188
189 dx = the_coords[j].x - the_coords[i].x;
190 dy = the_coords[j].y - the_coords[i].y;
191 dz = the_coords[j].z - the_coords[i].z;
192
193 dr2 = dx * dx + dy * dy + dz * dz;
194
195 test = !findAtomType(the_coords[i].name, &type_i);
196 if(test){
197 fprintf(stderr, "Atom Type %s, not found!\n",
198 the_coords[i].name);
199 exit(8);
200 }
201
202 test = !findAtomType(the_coords[j].name, &type_j);
203 if(test){
204 fprintf(stderr, "Atom Type %s, not found!\n",
205 the_coords[j].name);
206 exit(8);
207 }
208
209
210 dcv = bond_fudge * (type_i.covalentRadii + type_j.covalentRadii);
211 dcv2 = dcv * dcv;
212
213 if(dr2 <= dcv2){
214
215 temp_bond =
216 (struct linked_bond_list *)malloc(sizeof(struct linked_bond_list));
217
218 bl_head->the_bond.i = i;
219 bl_head->the_bond.j = j;
220
221 temp_bond->next = bl_head;
222 bl_head = temp_bond;
223 }
224 }
225 }
226 }
227
228
229 void clean_bonds(){
230 struct linked_bond_list *current_bond;
231 struct linked_bond_list *next_bond; /* place holders */
232
233
234 current_bond = bl_head->next;
235
236 while(current_bond != NULL){
237
238 next_bond = current_bond->next;
239 free(current_bond);
240 current_bond = next_bond;
241 }
242
243 bl_head->next = NULL;
244 free( bl_head );
245 bl_head = NULL;
246 }
247
248
249 void make_header_macros(FILE *out_file){
250
251 struct linked_atom *type_list; // list of all atom types
252 struct linked_atom *current_type; // current atom type
253
254 char *name;
255 double red, green, blue;
256 double radius;
257
258 type_list = get_type_list();
259 current_type = type_list->next;
260
261 while(current_type != NULL){
262
263 name = current_type->myAtom.name;
264 radius = current_type->myAtom.vanDerWallRadii;
265 red = ((double)current_type->myAtom.red) / 255.0;
266 green = ((double)current_type->myAtom.green) / 255.0;
267 blue = ((double)current_type->myAtom.blue) / 255.0;
268
269
270
271 fprintf(out_file,
272 "//****************************************************\n"
273 "// DEFINE %s MACROS\n"
274 "//****************************************************\n"
275 "\n"
276 "#macro make_%s_bond "
277 "(end_1x, end_1y, end_1z, end_2x, end_2y, end_2z)\n"
278 "\n"
279 " #local x1 = end_1x;\n"
280 " #local y1 = end_1y;\n"
281 " #local z1 = end_1z;\n"
282 " #local x2 = end_2x;\n"
283 " #local y2 = end_2y;\n"
284 " #local z2 = end_2z;\n"
285 "\n"
286 " #if(ROTATE)\n"
287 " #local x1_new = A11 * x1 + A12 * y1 + A13 * z1;\n"
288 " #local y1_new = A21 * x1 + A22 * y1 + A23 * z1;\n"
289 " #local z1_new = A31 * x1 + A32 * y1 + A33 * z1;\n"
290 "\n"
291 " #local x2_new = A11 * x2 + A12 * y2 + A13 * z2;\n"
292 " #local y2_new = A21 * x2 + A22 * y2 + A23 * z2;\n"
293 " #local z2_new = A31 * x2 + A32 * y2 + A33 * z2;\n"
294 "\n"
295 " #else\n"
296 " #local x1_new = x1;"
297 " #local y1_new = y1;"
298 " #local z1_new = z1;"
299 "\n"
300 " #local x2_new = x2;"
301 " #local y2_new = y2;"
302 " #local z2_new = z2;"
303 "\n"
304 " #end\n"
305 "\n"
306 " cylinder{\n"
307 " < x1_new, y1_new, z1_new >,\n"
308 " < x2_new, y2_new, z2_new >,\n"
309 " BOND_RADIUS\n"
310 " texture{\n"
311 " pigment{ rgb < %lf, %lf, %lf > }\n"
312 " finish{\n"
313 " ambient .2\n"
314 " diffuse .6\n"
315 " specular 1\n"
316 " roughness .001\n"
317 " metallic\n"
318 " }\n"
319 " }\n"
320 " }\n"
321 "#end\n"
322 "#macro make_%s_atom "
323 "(center_x, center_y, center_z)\n"
324 "\n"
325 " #local x1 = center_x;\n"
326 " #local y1 = center_y;\n"
327 " #local z1 = center_z;\n"
328 "\n"
329 " #if(ROTATE)\n"
330 "\n"
331 " #local x1_new = A11 * x1 + A12 * y1 + A13 * z1;\n"
332 " #local y1_new = A21 * x1 + A22 * y1 + A23 * z1;\n"
333 " #local z1_new = A31 * x1 + A32 * y1 + A33 * z1;\n"
334 "\n"
335 " #else\n"
336 "\n"
337 " #local x1_new = x1;"
338 " #local y1_new = y1;"
339 " #local z1_new = z1;"
340 "\n"
341 " #end\n"
342 "\n"
343 " sphere{\n"
344 " < x1_new, y1_new, z1_new >,\n"
345 " ATOM_SPHERE_FACTOR * %lf\n"
346 " texture{\n"
347 " pigment{ rgb < %lf, %lf, %lf > }\n"
348 " finish{\n"
349 " ambient .2\n"
350 " diffuse .6\n"
351 " specular 1\n"
352 " roughness .001\n"
353 " metallic\n"
354 " }\n"
355 " }\n"
356 " }\n"
357 "#end\n"
358 "#macro make_%s_vector "
359 "(center_x, center_y, center_z, ux, uy, uz)\n"
360 "\n"
361 " #local vx = VECTOR_SCALE * ux;\n"
362 " #local vy = VECTOR_SCALE * uy;\n"
363 " #local vz = VECTOR_SCALE * uz;\n"
364 " #local x1 = center_x - 0.5 * vx;\n"
365 " #local y1 = center_y - 0.5 * vy;\n"
366 " #local z1 = center_z - 0.5 * vz;\n"
367 " #local x2 = center_x + 0.5 * vx;\n"
368 " #local y2 = center_y + 0.5 * vy;\n"
369 " #local z2 = center_z + 0.5 * vz;\n"
370 " #local v2 = vx*vx + vy*vy + vz*vz;\n"
371 " #local vl = sqrt(v2);\n"
372 " #local x3 = x1 + vx * (1.0 - CONE_FRACTION);\n"
373 " #local y3 = y1 + vy * (1.0 - CONE_FRACTION);\n"
374 " #local z3 = z1 + vz * (1.0 - CONE_FRACTION);\n"
375 "\n"
376 " #if(ROTATE)\n"
377 " #local x1_new = A11 * x1 + A12 * y1 + A13 * z1;\n"
378 " #local y1_new = A21 * x1 + A22 * y1 + A23 * z1;\n"
379 " #local z1_new = A31 * x1 + A32 * y1 + A33 * z1;\n"
380 "\n"
381 " #local x2_new = A11 * x2 + A12 * y2 + A13 * z2;\n"
382 " #local y2_new = A21 * x2 + A22 * y2 + A23 * z2;\n"
383 " #local z2_new = A31 * x2 + A32 * y2 + A33 * z2;\n"
384 "\n"
385 " #local x3_new = A11 * x3 + A12 * y3 + A13 * z3;\n"
386 " #local y3_new = A21 * x3 + A22 * y3 + A23 * z3;\n"
387 " #local z3_new = A31 * x3 + A32 * y3 + A33 * z3;\n"
388 "\n"
389 " #else\n"
390 " #local x1_new = x1;"
391 " #local y1_new = y1;"
392 " #local z1_new = z1;"
393 "\n"
394 " #local x2_new = x2;"
395 " #local y2_new = y2;"
396 " #local z2_new = z2;"
397 "\n"
398 " #local x3_new = x3;"
399 " #local y3_new = y3;"
400 " #local z3_new = z3;"
401 "\n"
402 " #end\n"
403 "\n"
404 " cylinder{\n"
405 " < x1_new, y1_new, z1_new >,\n"
406 " < x3_new, y3_new, z3_new >,\n"
407 " STICK_RADIUS\n"
408 " texture{\n"
409 " pigment{ rgb < %lf, %lf, %lf > }\n"
410 " finish{\n"
411 " ambient .2\n"
412 " diffuse .6\n"
413 " specular 1\n"
414 " roughness .001\n"
415 " metallic\n"
416 " }\n"
417 " }\n"
418 " }\n"
419 " cone{\n"
420 " < x2_new, y2_new, z2_new >, 0.0\n"
421 " < x3_new, y3_new, z3_new >, CONE_RADIUS\n"
422 " texture{\n"
423 " pigment{ rgb < %lf, %lf, %lf > }\n"
424 " finish{\n"
425 " ambient .2\n"
426 " diffuse .6\n"
427 " specular 1\n"
428 " roughness .001\n"
429 " metallic\n"
430 " }\n"
431 " }\n"
432 " }\n"
433 "#end\n"
434 "\n"
435 "\n",
436 name,
437 name,
438 red, green, blue,
439 name,
440 radius,
441 red, green, blue,
442 name,
443 red, green, blue,
444 red, green, blue);
445
446 current_type = current_type->next;
447 }
448 }