chiark / gitweb /
X window size is a parameter; scale is a parameter; whether to draw original strip...
[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> tn("tn", "Number of sections around `strip'", 30, 1, 1, 500);
22 static Parameter<int> un("un", "Number of sections across `strip'", 15, 1, 1, 500);
23 static Parameter<int> orig("orig", "Use original strip (not reformulated)", 1, 0, 0, 1);
24 static Parameter<double> theta("theta", "Angle of model rotation",
25                                /*-PI/2.*/0, M_PI/8., -M_PI*2., M_PI*2.);
26 static Parameter<double> eta("eta", "Angle of model elevation",
27                              0., M_PI/8., -M_PI*2., M_PI*2.);
28 static Parameter<double> scale("scale", "Scale factor for resulting object", 
29                                1.0, 0.25, 0.001, 1000.);
30 static Parameter<double> planedistance("plane",
31                                        "Distance from projection plane to origin", 
32                                        4., .25, 0., 100.);
33 static Parameter<double> eyedistance("eye", "Distance from projection eye to origin", 
34                                      1.5, .25, 0., 100.);
35 static Parameter<double> cutoffdistance("cutoff",
36                                         "Distance from projection cutoff to origin", 
37                                         10., .25, -10., 100.);
38 static Parameter<double> width("width", "Width of the `strip' before transformation", 
39                                .4, .1, 0., 1.);
40 static Parameter<double> thickness("thick", "Thickness of the `pancake'", 1., .1, 0., 1.);
41 static Parameter<double> bottomportion("bp", "Proportion in the bottom half", 
42                                        .7, .1, 0., 1.);
43 static Parameter<double> tear("tear", "Angle factor in tear-open",
44                               0/*1.1*/, .1, -5., 5.);
45 static Parameter<double> teary("teary", "Axis y coord in tear-open", 1.3, .1, -10., 10.);
46 static Parameter<double> twist("twist", "Angle per length in twist",
47                                0/*.8*/, .25, -5., 5.);
48 static Parameter<double> twisty("twisty", "Axis y coord in twist", .85, .1, -10., 10.);
49 static Parameter<double> bulge("bulge", "Amount of bulge", 0/*1.*/, .25, -15., 15.);
50 static Parameter<double> bulgesd("bulgesd", "S.D. of bulge", 1., .2, .000001, 20.);
51 static Parameter<double> bulgey("bulgey", "Axis y coord in bulge", .85, .1, -10., 10.);
52 static Parameter<double> shearx("shearx", "Amount of shear (x)", 0., .1, -15., 15.);
53 static Parameter<double> sheary("sheary", "Amount of shear (y)", 0/*1.*/, .1, -15., 15.);
54 static Parameter<double> shearsdx("shearsdx", "S.D. of shear (x)", .5, .1, -15., 15.);
55 static Parameter<double> shearsdy("shearsdy", "S.D. of shear (y)", .5, .1, -15., 15.);
56 static Parameter<double> shearey("shearey", "Centre of shear (y)", 1.3, .1, -10., 10.);
57
58 SortedCellList *list=0;
59 X11Output *x11= 0;
60 DualX11Output *dualx11= 0;
61
62 void generate() {
63   if (list) delete list;
64   Moebius *strip;
65   if (orig) {
66     strip= new MoebiusStrip(width);
67   } else {
68     strip= new MoebiusEnfoldment(thickness,bottomportion);
69   }
70   list= new SortedCellList;
71   double t0= 0.0;
72   for (int ti=0; ti<tn; ti++) {
73     double t1= (double)(ti+1)/tn;
74     double u0= 0.0;
75     for (int ui=0; ui<un; ui++) {
76       double u1= (double)(ui+1)/un;
77       Cell *cell= new Cell;
78 //u0=u1=tear;
79       cell->p[0]= strip->middlepoint(t0,u0);
80 //fprintf(stderr,"t %7.4f u %7.4f  %7.4f,%7.4f,%7.4f\n",
81 //        t0,u0, cell->p[0][0], cell->p[0][1], cell->p[0][2]);
82       cell->p[1]= strip->middlepoint(t0,u1);
83       cell->p[2]= strip->middlepoint(t1,u1);
84       cell->p[3]= strip->middlepoint(t1,u0);
85       list->insert(cell);
86       u0= u1;
87     }
88     t0= t1;
89   }
90   delete strip;
91 }
92
93 int main(int argc, char **argv) {
94   char buf[100];
95
96   for (;;) {
97     Point::setobserver(theta,eta,planedistance,eyedistance,cutoffdistance);
98     Point::cleartransform();
99     if (tear != 0.0) Point::appendtransform(new TearOpenTransform(tear,teary));
100     if (twist != 0.0) Point::appendtransform(new TwistTransform(twist,twisty));
101     if (bulge != 0.0) Point::appendtransform(new BulgeTransform(bulge,bulgesd,bulgey));
102     if (shearx != 0.0 || sheary != 0.0)
103       Point::appendtransform(new ShearTransform(Point(0.0,shearey,0.0),
104                                                 Point(shearx,sheary,0.0),
105                                                 Point(shearsdx,shearsdy,1e20)));
106     Point::appendtransform(new ScaleTransform(scale));
107     generate();
108     if (x11) list->display(*x11);
109     if (dualx11) list->display(*dualx11);
110     for (;;) {
111       cin.get(buf,100,'\n');
112       char c;
113       if (!cin.get(c) || c!='\n') {
114         cerr << "error reading command input, giving up\n";
115         exit(1);
116       }
117       char *equals= strchr(buf,'=');
118       if (equals) {
119         *equals++= 0;
120         AnyParameter *param= AnyParameter::find(buf);
121         if (param) {
122           *param= atof(equals);
123           break;
124         } else {
125           cerr << "parameter `" << buf << "' not found\n";
126         }
127       } else if (!strncmp(buf,"postscript ",11)) {
128         const char *filename= buf+11;
129         ofstream file(filename);
130         if (file) {
131           PostScriptOutput psout(file);
132           list->display(psout);
133         } else {
134           cerr << "Failed to open `" << filename << "'\n";
135           continue;
136         }
137         cerr << "PostScript written to `" << filename <<"'\n";
138       } else if (!strcmp(buf,"x11")) {
139         if (x11) {
140           delete x11;
141           x11= 0;
142         } else {
143           x11= new X11Output;
144         }
145         break;
146       } else if (!strcmp(buf,"dualx11")) {
147         if (dualx11) {
148           delete dualx11;
149           dualx11= 0;
150         } else {
151           dualx11= new DualX11Output;
152         }
153         break;
154       } else if (!strcmp(buf,"quit")) {
155         exit(0);
156       } else if (!strcmp(buf,"list")) {
157         AnyParameter::list();
158       } else if (!*buf) {
159         break;
160       } else {
161         cerr << "command not understood\n";
162       }
163     }
164   }
165 }