ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/xyz2pov/src/pov2anim.c
Revision: 60
Committed: Thu Aug 1 21:12:33 2002 UTC (21 years, 11 months ago) by mmeineke
Content type: text/plain
Original Path: branches/mmeineke/xyz2pov/src/pov2anim.c
File size: 7208 byte(s)
Log Message:
A pair of utilities to turn an xyz file into a sequence of povray rendered images.

File Contents

# User Rev Content
1 mmeineke 60 #include <stdio.h>
2     #include <stdlib.h>
3     #include <string.h>
4     #include <unistd.h>
5     #include <sys/types.h>
6     #include <sys/stat.h>
7     #include <dirent.h>
8    
9     #define ANIM_DIR "./anim"
10     #define POV_DIR "./pov"
11    
12    
13     char *program_name;
14     void usage(void);
15     int isNumber( char c );
16    
17     int main(argc, argv)
18     int argc;
19     char *argv[];
20     {
21    
22     int i, j, k; /*loop counters */
23     mode_t dir_mode = S_IRWXU;
24    
25     char pov_flags[120] = ""; /*flags to be placed on the povray command line */
26     char *prefix = NULL; /*the prefix for the input and output files */
27     int start_frame = 0; /*the start frame */
28     int in_frame; /*the current in frame we're working with. */
29     int out_frame; /*the current out frame were working with */
30     int end_frame; /*the end frame */
31     short is_end_frame = 0; /*tells us to stop before we run out of pov files */
32     int in_file_exists = 0; /*lets us know if the in_file exists */
33     int compression = 0; /* boolean for toggling the compression flag */
34     int duplicate = 0; /* boolean to turn on duplicate frames */
35     int n_duplicates = 0; /* the number of duplicate frames */
36     int done;
37    
38     char current_flag;
39    
40     char command[1000];
41     char pov_dir[500];
42     char anim_dir[500];
43     char out_format[500];
44     char in_format[500];
45     char in_name[5000];
46     char out_name[500];
47     char out_name2[5000];
48    
49     DIR *theDir;
50     struct dirent *dirEntry;
51     int nFrames;
52     size_t prefixLength;
53     double count;
54     int out_count;
55     int in_count;
56     char test_c;
57    
58    
59     program_name = argv[0]; /*save the program name in case we need it*/
60    
61    
62    
63     for( i = 1; i < argc; i++){
64    
65     if(argv[i][0] =='-'){
66    
67     // parse the option
68    
69     if(argv[i][1] == '-' ){
70    
71     // parse long word options
72    
73     fprintf( stderr,
74     "Invalid option \"%s\"\n", argv[i] );
75     usage();
76    
77     }
78    
79     else{
80    
81     // parse single character options
82    
83     done = 0;
84     j = 1;
85     current_flag = argv[i][j];
86     while( (current_flag != '\0') && (!done) ){
87    
88     switch(current_flag){
89    
90     case 'c':
91     // -c => turn on the compression flag for x-povray
92    
93     compression = 1;
94     strcat(pov_flags, " +FP ");
95     break;
96    
97     case 'd':
98     // -d <#> => duplicate the frames
99    
100     duplicate = 1;
101     i++;
102     n_duplicates = atoi(argv[i]);
103     done =1;
104     break;
105    
106    
107    
108     case 'f':
109     // -f <#> => tells us the final frame
110    
111     is_end_frame = 1;
112     i++;
113     end_frame = atoi(argv[i]);
114     done=1;
115     break;
116    
117     case 's':
118     // -s <#> => tells the start frame
119    
120     i++;
121     start_frame = atoi(argv[i]);
122     done=1;
123     break;
124    
125     case 'F':
126     // -F <flags> => The flag arguments to be passed to x-povray
127    
128     i++;
129     strcat(pov_flags, argv[i]);
130     done=1;
131     break;
132    
133     default:
134    
135     (void)fprintf(stderr, "Bad option \"-%c\"\n", current_flag);
136     usage();
137     }
138     j++;
139     current_flag = argv[i][j];
140     }
141     }
142     }
143    
144     else{
145    
146     if( prefix != NULL ){
147     fprintf( stderr,
148     "Error at \"%s\", program does not currently support\n"
149     "more than one prefix.\n"
150     "\n",
151     argv[i]);
152     usage();
153     }
154    
155     prefix = argv[i];
156     }
157     }
158    
159    
160     if(prefix == NULL){
161     usage();
162     }
163    
164    
165     if(access(ANIM_DIR, F_OK)){
166     /*create the anim directory*/
167     mkdir(ANIM_DIR, dir_mode);
168     }
169    
170     strcpy(anim_dir, ANIM_DIR); strcat(anim_dir, "/");
171     strcpy(pov_dir, POV_DIR); strcat(pov_dir, "/");
172    
173     // find the number of digits to output
174    
175     nFrames = 0;
176     if( is_end_frame ){
177     nFrames = end_frame - start_frame;
178     }
179     else{
180    
181     theDir = opendir( POV_DIR );
182     dirEntry = readdir( theDir );
183     prefixLength = strlen( prefix );
184     while( dirEntry != NULL ){
185    
186     if( !strncmp( dirEntry->d_name, prefix, prefixLength ) ) nFrames++;
187    
188     dirEntry = readdir( theDir );
189     }
190     closedir( theDir );
191     }
192    
193     out_count = 1;
194     count = (double)( nFrames * (n_duplicates+1) );
195     while( count >= 10.0 ){
196     count /= 10.0;
197     out_count++;
198     }
199    
200     // how many padded digits are there for the input
201    
202     theDir = opendir( POV_DIR );
203     dirEntry = readdir( theDir );
204     prefixLength = strlen( prefix );
205     while( dirEntry != NULL ){
206    
207     in_count = 0;
208     if( !strncmp( dirEntry->d_name, prefix, prefixLength ) ){
209    
210     i = prefixLength;
211     test_c = dirEntry->d_name[i];
212     while( isNumber( test_c ) ){
213    
214     in_count++;
215     i++;
216     test_c = dirEntry->d_name[i];
217     }
218     break;
219     }
220    
221     dirEntry = readdir( theDir );
222     }
223     closedir( theDir );
224    
225    
226     strcpy(in_name, pov_dir);
227     strcpy(out_name, anim_dir);
228    
229     sprintf( in_format, "%s%s%%0%dd.pov", pov_dir, prefix, in_count );
230    
231     sprintf( out_format, "%s%s%%0%dd", anim_dir, prefix, out_count );
232     if(compression) strcat(out_format, ".ppm");
233     else strcat(out_format, ".tga");
234    
235     in_frame = start_frame;
236     out_frame = 0;
237    
238     if(access(ANIM_DIR, F_OK)){
239     /*create the anim directory*/
240     mkdir(ANIM_DIR, dir_mode);
241     }
242    
243    
244     sprintf( in_name, in_format, in_frame );
245     sprintf( out_name, out_format, out_frame );
246    
247     in_file_exists = !access(in_name, F_OK);
248    
249     if( is_end_frame ){
250    
251     while( (in_frame <= end_frame) && in_file_exists ){
252    
253    
254     sprintf(command, "x-povray +I %s +O %s %s",
255     in_name, out_name, pov_flags);
256     system(command);
257    
258     if(duplicate){
259     for(k = 0; k < n_duplicates; k++){
260     out_frame++;
261    
262     sprintf( out_name2, out_format, out_frame );
263     sprintf(command, "cp %s %s", out_name, out_name2);
264     system(command);
265     }
266     }
267    
268     in_frame++;
269     out_frame++;
270    
271     sprintf( in_name, in_format, in_frame );
272     sprintf( out_name, out_format, out_frame );
273    
274     in_file_exists = !access(in_name, F_OK);
275     }
276     }
277     else{
278    
279     while( in_file_exists ){
280    
281    
282     sprintf(command, "x-povray +I %s +O %s %s",
283     in_name, out_name, pov_flags);
284     system(command);
285    
286     if(duplicate){
287     for(k = 0; k < n_duplicates; k++){
288     out_frame++;
289    
290     sprintf( out_name2, out_format, out_frame );
291     sprintf(command, "cp %s %s", out_name, out_name2);
292     system(command);
293     }
294     }
295    
296     in_frame++;
297     out_frame++;
298    
299     sprintf( in_name, in_format, in_frame );
300     sprintf( out_name, out_format, out_frame );
301    
302     in_file_exists = !access(in_name, F_OK);
303     }
304     }
305    
306     return 0;
307     }
308    
309    
310    
311    
312    
313     /***************************************************************************
314     * prints out the usage for the command line arguments, then exits.
315     ***************************************************************************/
316    
317     void usage(){
318     (void)fprintf(stderr,
319     "The proper usage is: %s [options] <pov_prefix>\n\n"
320     "Options:\n"
321     " -c turns on compression for x-povray\n"
322     " -d <#> turns on and duplicates the number of frames\n"
323     " -s <#> the starting frame number\n"
324     " -f <#> the final frame number\n"
325     " -F \"<povray flags>\"\n"
326     " flags to pass to x-povray\n"
327    
328     "\n",
329     program_name);
330     exit(8);
331     }
332    
333    
334     int isNumber( char c ){
335    
336     if( c == '0') return 1;
337     else if( c == '1') return 1;
338     else if( c == '2') return 1;
339     else if( c == '3') return 1;
340     else if( c == '4') return 1;
341     else if( c == '5') return 1;
342     else if( c == '6') return 1;
343     else if( c == '7') return 1;
344     else if( c == '8') return 1;
345     else if( c == '9') return 1;
346    
347     return 0;
348     }