ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/lsys/main.cpp
Revision: 1.2
Committed: Wed Nov 7 12:50:17 2018 UTC (5 years, 5 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * simple main program to illustrate the facilities
3 */
4
5 #include "config.h"
6
7 #ifdef HAVE_GETOPT_H
8 #include <getopt.h>
9 #else
10 #include "getopt.h"
11 #endif
12
13 #include <iostream>
14 #include <fstream>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18
19 #include "lsys.h"
20 #include "interp.h"
21 #include "interface.h"
22
23 #include "tokens.h" // antlr has problems without!
24 #include "Parser.h"
25
26 #include "ps_int.h"
27 #include "gl_int.h"
28 #include "rad_int.h"
29 #include "ray_int.h"
30
31 using namespace std;
32
33 static struct option options[] = {
34 { "interface" , required_argument , 0, 'f' },
35 { "interpreter" , required_argument , 0, 'i' },
36 { "output" , required_argument , 0, 'o' },
37 { "show-depth" , required_argument , 0, 'd' },
38 { "module" , required_argument , 0, 'm' },
39 { "help" , no_argument , 0, 'h' },
40 { 0 , 0 , 0, 0 },
41 };
42
43 void usage(int ret)
44 {
45 cerr << "\n"
46 PACKAGE " version " VERSION ", a l-system exploration system\n"
47 "written by Marc Lehmann <pcg@goof.com> You can find more info at\n"
48 "http://www.plan9.de/ or http://www.goof.com/pcg/marc/lsys.html\n"
49 "\n"
50 "USAGE: lsys [options] sourcefile [min [max [step]]]\n"
51 " where options are:\n"
52 " [ -h | --help ] this help\n"
53 " [ -m | --module string ] use a specific module as seed\n"
54 " [ -i | --interpreter int ] select an interpreter [turtle2d, turtle3d]\n"
55 " [ -f | --interface iface ] select an interface [ps, gl, rad, ray]\n"
56 " [ -d | --show-depth depth ] show evaluated system\n"
57 " [ -o | --output dest ] select an output destination\n"
58 " [ -I | --include path ] search includes also in path\n"
59 "\n";
60
61 exit (ret);
62 }
63
64 int verbose = 0;
65
66 int main(int argc, char *argv[])
67 try
68 {
69 char c;
70 int option_index = 0;
71
72 gfx_int *iface = 0;
73 interpreter *inter = 0;
74 ostream *ofile = 0;
75
76 char *outfile_str = 0;
77 const char *interpreter_str = "turtle";
78 const char *interface_str = "gl";
79 char *module_str = 0;
80 double min, max, step;
81 int show_depth = 0;
82
83 lsys root_lsys;
84
85 while ((c = getopt_long (argc, argv, "hm:o:I:f:i:d:v", options, &option_index)) != -1)
86 switch (c)
87 {
88 case 'm':
89 module_str = optarg;
90 break;
91
92 case 'i':
93 interpreter_str = optarg;
94 break;
95
96 case 'f':
97 interface_str = optarg;
98 break;
99
100 case 'o':
101 outfile_str = optarg;
102 break;
103
104 case 'I':
105 include_dirs.push_back (string (optarg));
106 break;
107
108 case 'd':
109 show_depth = atoi (optarg);
110 break;
111
112 case 'v':
113 verbose++;
114 break;
115
116 case ':':
117 fprintf (stderr, "required argument missing, try -h for help.\n");
118 exit (1);
119
120 case '?':
121 fprintf (stderr, "unknown option, try -h for help.\n");
122 exit (1);
123
124 case 'h':
125 default:
126 usage (1);
127 }
128
129 if (outfile_str)
130 ofile = new ofstream (outfile_str);
131
132 if (!interface_str)
133 throw error ("you _must_ specify an interface");
134 else if (strcmp (interface_str, "ps") == 0
135 || strcmp (interface_str, "postscript") == 0)
136 ofile
137 ? (void) (iface = new ps_int (*ofile))
138 : throw error ("output file required for postscript interface");
139 else if (strcmp (interface_str, "rad") == 0
140 || strcmp (interface_str, "radiance") == 0)
141 ofile
142 ? (void) (iface = new rad_int (*ofile))
143 : throw error ("output file required for radiance interface");
144 else if (strcmp (interface_str, "ray") == 0
145 || strcmp (interface_str, "rayshade") == 0)
146 ofile
147 ? (void) (iface = new ray_int (*ofile))
148 : throw error ("output file required for rayshade interface");
149 else if (strcmp (interface_str, "gl") == 0
150 || strcmp (interface_str, "opengl") == 0)
151 #if HAVE_OPENGL
152 iface = new gl_int ();
153 #else
154 throw error ("this binary was compiled without opengl support");
155 #endif
156 else
157 throw error ("unknown interface");
158
159 if (!interpreter_str)
160 throw error ("you _must_ specify an interpreter");
161 else if (strcmp (interpreter_str, "turtle2d") == 0)
162 inter = new turtle_interpreter (*iface, true);
163 else if (strcmp (interpreter_str, "turtle3d") == 0
164 || strcmp (interpreter_str, "turtle") == 0)
165 inter = new turtle_interpreter (*iface, false);
166 else
167 throw error ("unknown interface\n");
168
169 if (optind >= argc)
170 throw error ("you have to specify at least a source file");
171
172 if (optind < argc)
173 parse_file (argv [optind++], root_lsys);
174
175 if (optind < argc)
176 min = atof (argv [optind++]);
177 else
178 min = 1;
179
180 if (optind < argc)
181 max = atof (argv [optind++]);
182 else
183 max = 1000;
184
185 if (optind < argc)
186 step = atof (argv [optind++]);
187 else
188 step = 1;
189
190 if (verbose)
191 cout << endl
192 << root_lsys << endl
193 << "evaluating from " << min << " to " << max << " stepsize " << step << endl;
194
195 simple_interpreter s_int(show_depth);
196
197 while (min <= max)
198 {
199 module seed(module_str ? module_str : root_lsys.str, min);
200 module_vec v(root_lsys (seed.wrap ()));
201
202 if (verbose)
203 cout << "STEP " << min << ": " << v << endl;
204
205 s_int (v, root_lsys);
206 (*inter) (v, root_lsys);
207
208 min += step;
209 }
210
211 if (inter) delete inter;
212 if (iface) delete iface;
213 if (ofile) delete ofile;
214
215 return 0;
216 } catch (const error &e) {
217 cerr << e;
218 return 1;
219 }
220