ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/xyz2pov/src/pov_writer.c
Revision: 508
Committed: Tue Apr 29 15:21:34 2003 UTC (21 years, 2 months ago) by mmeineke
Content type: text/plain
File size: 8049 byte(s)
Log Message:
added some stuff, and forgot to update. who knows whats different now.

File Contents

# User Rev Content
1 mmeineke 60 #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 mmeineke 508 void initBondList(void){
26     bl_head = NULL;
27     }
28 mmeineke 60
29     void pov_write(FILE *out_file, struct coords *the_coords, int n_atoms,
30     int d_hydrogens, int d_bonds, int d_atoms){
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_bonds){
77    
78     fprintf(out_file,
79     "//************************************************************\n"
80     "// The list of bonds\n"
81     "//************************************************************\n"
82     "\n"
83     "\n");
84    
85 mmeineke 508 if( bl_head == NULL ) make_bonds(the_coords, n_atoms);
86 mmeineke 60
87     current_bond = bl_head->next;
88    
89     while(current_bond != NULL){
90    
91     skip_bond = 0;
92    
93     i = current_bond->the_bond.i;
94     j = current_bond->the_bond.j;
95    
96     if(!d_hydrogens){
97    
98     test1 = !strcmp("H", the_coords[i].name);
99     test2 = !strcmp("H", the_coords[j].name);
100    
101     skip_bond = (test1 || test2);
102     }
103    
104     if(!skip_bond){
105    
106     dx = (the_coords[j].x - the_coords[i].x) / 2.0;
107     dy = (the_coords[j].y - the_coords[i].y) / 2.0;
108     dz = (the_coords[j].z - the_coords[i].z) / 2.0;
109    
110     fprintf(out_file,
111     "make_%s_bond( %lf, %lf, %lf, %lf, %lf, %lf )\n",
112     the_coords[i].name,
113     the_coords[i].x,
114     the_coords[i].z,
115     the_coords[i].y,
116     (the_coords[i].x + dx),
117     (the_coords[i].z + dz),
118     (the_coords[i].y + dy));
119    
120     fprintf(out_file,
121     "make_%s_bond( %lf, %lf, %lf, %lf, %lf, %lf )\n",
122     the_coords[j].name,
123     the_coords[j].x,
124     the_coords[j].z,
125     the_coords[j].y,
126     (the_coords[j].x - dx),
127     (the_coords[j].z - dz),
128     (the_coords[j].y - dy));
129    
130     fprintf(out_file, "\n");
131     }
132    
133     current_bond = current_bond->next;
134     }
135    
136 mmeineke 508 if( regenerateBonds )clean_bonds();
137 mmeineke 60 }
138     }
139    
140    
141     void make_bonds(struct coords *the_coords, int n_atoms){
142    
143     int i, j; /*counters */
144     struct linked_bond_list *temp_bond; /*bond place holder */
145    
146     const double bond_fudge = 1.12; // a fudge factor
147     struct atom type_i, type_j; /* holds the atom types */
148    
149     int test; /* booleans */
150     double dx, dy, dz, dr2, dcv, dcv2; // used to determine bond existence
151    
152    
153     bl_head = (struct linked_bond_list *)malloc(sizeof(struct linked_bond_list));
154     bl_head->next = NULL;
155    
156     for(i = 0; i < (n_atoms - 1); i++){
157    
158     for(j = (i+1); j < n_atoms; j++){
159    
160     dx = the_coords[j].x - the_coords[i].x;
161     dy = the_coords[j].y - the_coords[i].y;
162     dz = the_coords[j].z - the_coords[i].z;
163    
164     dr2 = dx * dx + dy * dy + dz * dz;
165    
166     test = !findAtomType(the_coords[i].name, &type_i);
167     if(test){
168     fprintf(stderr, "Atom Type %s, not found!\n",
169     the_coords[i].name);
170     exit(8);
171     }
172    
173     test = !findAtomType(the_coords[j].name, &type_j);
174     if(test){
175     fprintf(stderr, "Atom Type %s, not found!\n",
176     the_coords[j].name);
177     exit(8);
178     }
179    
180    
181     dcv = bond_fudge * (type_i.covalentRadii + type_j.covalentRadii);
182     dcv2 = dcv * dcv;
183    
184     if(dr2 <= dcv2){
185    
186     temp_bond =
187     (struct linked_bond_list *)malloc(sizeof(struct linked_bond_list));
188    
189     bl_head->the_bond.i = i;
190     bl_head->the_bond.j = j;
191    
192     temp_bond->next = bl_head;
193     bl_head = temp_bond;
194     }
195     }
196     }
197     }
198    
199    
200     void clean_bonds(){
201     struct linked_bond_list *current_bond;
202     struct linked_bond_list *next_bond; /* place holders */
203    
204    
205     current_bond = bl_head->next;
206    
207     while(current_bond != NULL){
208    
209     next_bond = current_bond->next;
210     free(current_bond);
211     current_bond = next_bond;
212     }
213    
214     bl_head->next = NULL;
215 mmeineke 508 free( bl_head );
216     bl_head = NULL;
217 mmeineke 60 }
218    
219    
220     void make_header_macros(FILE *out_file){
221    
222     struct linked_atom *type_list; // list of all atom types
223     struct linked_atom *current_type; // current atom type
224    
225     char *name;
226     double red, green, blue;
227     double radius;
228    
229     type_list = get_type_list();
230     current_type = type_list->next;
231    
232     while(current_type != NULL){
233    
234     name = current_type->myAtom.name;
235     radius = current_type->myAtom.vanDerWallRadii;
236     red = ((double)current_type->myAtom.red) / 255.0;
237     green = ((double)current_type->myAtom.green) / 255.0;
238     blue = ((double)current_type->myAtom.blue) / 255.0;
239    
240    
241    
242     fprintf(out_file,
243     "//****************************************************\n"
244     "// DEFINE %s MACROS\n"
245     "//****************************************************\n"
246     "\n"
247     "#macro make_%s_bond "
248     "(end_1x, end_1y, end_1z, end_2x, end_2y, end_2z)\n"
249     "\n"
250     " #local x1 = end_1x;\n"
251     " #local y1 = end_1y;\n"
252     " #local z1 = end_1z;\n"
253     " #local x2 = end_2x;\n"
254     " #local y2 = end_2y;\n"
255     " #local z2 = end_2z;\n"
256     "\n"
257     " #if(ROTATE)\n"
258     " #local x1_new = A11 * x1 + A12 * y1 + A13 * z1;\n"
259     " #local y1_new = A21 * x1 + A22 * y1 + A23 * z1;\n"
260     " #local z1_new = A31 * x1 + A32 * y1 + A33 * z1;\n"
261     "\n"
262     " #local x2_new = A11 * x2 + A12 * y2 + A13 * z2;\n"
263     " #local y2_new = A21 * x2 + A22 * y2 + A23 * z2;\n"
264     " #local z2_new = A31 * x2 + A32 * y2 + A33 * z2;\n"
265     "\n"
266     " #else\n"
267     " #local x1_new = x1;"
268     " #local y1_new = y1;"
269     " #local z1_new = z1;"
270     "\n"
271     " #local x2_new = x2;"
272     " #local y2_new = y2;"
273     " #local z2_new = z2;"
274     "\n"
275     " #end\n"
276     "\n"
277     " cylinder{\n"
278     " < x1_new, y1_new, z1_new >,\n"
279     " < x2_new, y2_new, z2_new >,\n"
280     " BOND_RADIUS\n"
281     " texture{\n"
282     " pigment{ rgb < %lf, %lf, %lf > }\n"
283     " finish{\n"
284     " ambient .2\n"
285     " diffuse .6\n"
286     " specular 1\n"
287     " roughness .001\n"
288     " metallic\n"
289     " }\n"
290     " }\n"
291     " }\n"
292     "#end\n"
293     "#macro make_%s_atom "
294     "(center_x, center_y, center_z)\n"
295     "\n"
296     " #local x1 = center_x;\n"
297     " #local y1 = center_y;\n"
298     " #local z1 = center_z;\n"
299     "\n"
300     " #if(ROTATE)\n"
301     "\n"
302     " #local x1_new = A11 * x1 + A12 * y1 + A13 * z1;\n"
303     " #local y1_new = A21 * x1 + A22 * y1 + A23 * z1;\n"
304     " #local z1_new = A31 * x1 + A32 * y1 + A33 * z1;\n"
305     "\n"
306     " #else\n"
307     "\n"
308     " #local x1_new = x1;"
309     " #local y1_new = y1;"
310     " #local z1_new = z1;"
311     "\n"
312     " #end\n"
313     "\n"
314     " sphere{\n"
315     " < x1_new, y1_new, z1_new >,\n"
316     " ATOM_SPHERE_FACTOR * %lf\n"
317     " texture{\n"
318     " pigment{ rgb < %lf, %lf, %lf > }\n"
319     " finish{\n"
320     " ambient .2\n"
321     " diffuse .6\n"
322     " specular 1\n"
323     " roughness .001\n"
324     " metallic\n"
325     " }\n"
326     " }\n"
327     " }\n"
328     "#end\n"
329     "\n"
330     "\n",
331     name,
332     name,
333     red, green, blue,
334     name,
335     radius,
336     red, green, blue);
337    
338     current_type = current_type->next;
339     }
340     }