chiark / gitweb /
dualx11 is broken atm
[moebius.git] / main.cc
1 /*
2  * Moebius main program.
3  */
4
5 #include <math.h>
6 #include <iostream.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 #include "output.hh"
12 #include "x11.hh"
13 #include "dualx11.hh"
14 #include "postscript.hh"
15 #include "library.hh"
16 #include "moebius.hh"
17 #include "transforms.hh"
18 #include "graphics.hh"
19 #include "parameter.hh"
20
21 static Parameter<int>
22 tn("tn", "Number of sections around `strip'", 30, 1, 1, 500);
23
24 static Parameter<int>
25 un("un", "Number of sections across `strip'", 15, 1, 1, 500);
26
27 static Parameter<int>
28 version("version", "Version to use: 0=original strip,"
29         " 1=reformulated, 2=reformulated again", 0, 1, 0, 2);
30
31 static Parameter<double>
32 theta("theta", "Angle of model rotation",
33       /*-PI/2.*/0, M_PI/8., -M_PI*2., M_PI*2.);
34
35 static Parameter<double>
36 eta("eta", "Angle of model elevation",
37     0., M_PI/8., -M_PI*2., M_PI*2.);
38
39 static Parameter<double>
40 scale("scale", "Scale factor for resulting object", 1.0, 0.25, 0.001, 1000.);
41
42 static Parameter<double>
43 planedistance("plane", "Distance from projection plane to origin", 
44               4., .25, 0., 100.);
45
46 static Parameter<double>
47 eyedistance("eye", "Distance from projection eye to origin", 
48             1.5, .25, 0., 100.);
49
50 static Parameter<double>
51 cutoffdistance("cutoff", "Distance from projection cutoff to origin", 
52                10., .25, -10., 100.);
53
54 static Parameter<double>
55 width("width", "Width of the `strip' before transformation", .4, .1, 0., 1.);
56
57 static Parameter<double>
58 thickness("thick", "Thickness of the `pancake'", 1., .1, 0., 1.);
59
60 static Parameter<double>
61 bottomportion("bp", "Proportion in the bottom half", .7, .1, 0., 1.);
62
63 static Parameter<double>
64 tear("tear", "Angle factor in tear-open", 0/*1.1*/, .1, -5., 5.);
65
66 static Parameter<double>
67 teary("teary", "Axis y coord in tear-open", 1.3, .1, -10., 10.);
68
69 static Parameter<double>
70 twist("twist", "Angle per length in twist", 0/*.8*/, .25, -5., 5.);
71
72 static Parameter<double>
73 twisty("twisty", "Axis y coord in twist", .85, .1, -10., 10.);
74
75 static Parameter<double>
76 bulge("bulge", "Amount of bulge", 0/*1.*/, .25, -15., 15.);
77
78 static Parameter<double>
79 bulgesd("bulgesd", "S.D. of bulge", 1., .2, .000001, 20.);
80
81 static Parameter<double>
82 bulgey("bulgey", "Axis y coord in bulge", .85, .1, -10., 10.);
83
84 static Parameter<double>
85 shearx("shearx", "Amount of shear (x)", 0., .1, -15., 15.);
86
87 static Parameter<double>
88 sheary("sheary", "Amount of shear (y)", 0/*1.*/, .1, -15., 15.);
89
90 static Parameter<double>
91 shearsdx("shearsdx", "S.D. of shear (x)", .5, .1, -15., 15.);
92
93 static Parameter<double>
94 shearsdy("shearsdy", "S.D. of shear (y)", .5, .1, -15., 15.);
95
96 static Parameter<double>
97 shearey("shearey", "Centre of shear (y)", 1.3, .1, -10., 10.);
98
99
100 SortedCellList *list=0;
101 X11Output *x11= 0;
102 DualX11Output *dualx11= 0;
103
104 void generate() {
105   if (list) delete list;
106   Moebius *strip;
107   switch (version) {
108   case 0: strip= new MoebiusStrip(width);                           break;
109   case 1: strip= new MoebiusEnfoldment(thickness,bottomportion);    break;
110   case 2: strip= new MoebiusEnfoldmentNew();                        break;
111   default: abort();
112   }
113   list= new SortedCellList;
114   double t0= 0.0;
115   for (int ti=0; ti<tn; ti++) {
116     double t1= (double)(ti+1)/tn;
117     double u0= 0.0;
118     for (int ui=0; ui<un; ui++) {
119       double u1= (double)(ui+1)/un;
120       Cell *cell= new Cell;
121 //u0=u1=tear;
122       cell->p[0]= strip->middlepoint(t0,u0);
123 //fprintf(stderr,"t %7.4f u %7.4f  %7.4f,%7.4f,%7.4f\n",
124 //        t0,u0, cell->p[0][0], cell->p[0][1], cell->p[0][2]);
125       cell->p[1]= strip->middlepoint(t0,u1);
126       cell->p[2]= strip->middlepoint(t1,u1);
127       cell->p[3]= strip->middlepoint(t1,u0);
128       list->insert(cell);
129       u0= u1;
130     }
131     t0= t1;
132   }
133   delete strip;
134 }
135
136 int main(int argc, char **argv) {
137   char buf[100];
138
139   for (;;) {
140     Point::setobserver(theta,eta,planedistance,eyedistance,cutoffdistance);
141     Point::cleartransform();
142     if (tear != 0.0) Point::appendtransform(new TearOpenTransform(tear,teary));
143     if (twist != 0.0) Point::appendtransform(new TwistTransform(twist,twisty));
144     if (bulge != 0.0) Point::appendtransform(new BulgeTransform(bulge,bulgesd,bulgey));
145     if (shearx != 0.0 || sheary != 0.0)
146       Point::appendtransform(new ShearTransform(Point(0.0,shearey,0.0),
147                                                 Point(shearx,sheary,0.0),
148                                                 Point(shearsdx,shearsdy,1e20)));
149     Point::appendtransform(new ScaleTransform(scale));
150     generate();
151     if (x11) list->display(*x11);
152     if (dualx11) list->display(*dualx11);
153     for (;;) {
154       cerr << "> ";
155       cin.getline(buf,sizeof(buf),'\n');
156       if (!cin.good()) {
157         cerr << "error reading command input, giving up\n";
158         exit(1);
159       }
160       char *equals= strchr(buf,'=');
161       if (equals) {
162         *equals++= 0;
163         AnyParameter *param= AnyParameter::find(buf);
164         if (param) {
165           *param= atof(equals);
166           break;
167         } else {
168           cerr << "parameter `" << buf << "' not found\n";
169         }
170       } else if (!strncmp(buf,"postscript ",11)) {
171         const char *filename= buf+11;
172         ofstream file(filename);
173         if (file) {
174           PostScriptOutput psout(file);
175           list->display(psout);
176         } else {
177           cerr << "Failed to open `" << filename << "'\n";
178           continue;
179         }
180         cerr << "PostScript written to `" << filename <<"'\n";
181       } else if (!strcmp(buf,"x11")) {
182         if (x11) {
183           delete x11;
184           x11= 0;
185         } else {
186           x11= new X11Output;
187         }
188         break;
189       } else if (!strcmp(buf,"dualx11")) {
190         if (dualx11) {
191           delete dualx11;
192           dualx11= 0;
193         } else {
194           dualx11= new DualX11Output;
195         }
196         break;
197       } else if (!strcmp(buf,"quit")) {
198         exit(0);
199       } else if (!strcmp(buf,"list")) {
200         AnyParameter::help();
201       } else if (!strcmp(buf,"help")) {
202         AnyParameter::list();
203       } else if (!*buf) {
204         break;
205       } else {
206         cerr << "command not understood\n";
207       }
208     }
209   }
210 }