chiark / gitweb /
645d93a568db6c4df3907f622413631f9e62ca1b
[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 static Moebius *strip= 0;
105
106 void generate() {
107   if (list) delete list;
108   if (strip) { delete strip; strip= 0; }
109   switch (version) {
110   case 0: strip= new MoebiusStrip(width);                           break;
111   case 1: strip= new MoebiusEnfoldment(thickness,bottomportion);    break;
112   case 2: strip= new MoebiusEnfoldmentNew();                        break;
113   default: abort();
114   }
115   list= new SortedCellList;
116   double t0= 0.0;
117   for (int ti=0; ti<tn; ti++) {
118     double t1= (double)(ti+1)/tn;
119     double u0= 0.0;
120     for (int ui=0; ui<un; ui++) {
121       double u1= (double)(ui+1)/un;
122       Cell *cell= new Cell;
123 //u0=u1=tear;
124       cell->p[0]= strip->middlepoint(t0,u0);
125 //fprintf(stderr,"t %7.4f u %7.4f  %7.4f,%7.4f,%7.4f\n",
126 //        t0,u0, cell->p[0][0], cell->p[0][1], cell->p[0][2]);
127       cell->p[1]= strip->middlepoint(t0,u1);
128       cell->p[2]= strip->middlepoint(t1,u1);
129       cell->p[3]= strip->middlepoint(t1,u0);
130       list->insert(cell);
131       u0= u1;
132     }
133     t0= t1;
134   }
135 }
136
137 int main(int argc, char **argv) {
138   static const char pointmap_cmd[]= "pointmap ";
139   char buf[100];
140   double pointmap_t, pointmap_u;
141
142   for (;;) {
143     Point::setobserver(theta,eta,planedistance,eyedistance,cutoffdistance);
144     Point::cleartransform();
145     if (tear != 0.0) Point::appendtransform(new TearOpenTransform(tear,teary));
146     if (twist != 0.0) Point::appendtransform(new TwistTransform(twist,twisty));
147     if (bulge != 0.0) Point::appendtransform(new BulgeTransform(bulge,bulgesd,bulgey));
148     if (shearx != 0.0 || sheary != 0.0)
149       Point::appendtransform(new ShearTransform(Point(0.0,shearey,0.0),
150                                                 Point(shearx,sheary,0.0),
151                                                 Point(shearsdx,shearsdy,1e20)));
152     Point::appendtransform(new ScaleTransform(scale));
153     generate();
154     if (x11) list->display(*x11);
155     if (dualx11) list->display(*dualx11);
156     for (;;) {
157       cerr << "> ";
158       cin.getline(buf,sizeof(buf),'\n');
159       if (!cin.good()) {
160         cerr << "error reading command input, giving up\n";
161         exit(1);
162       }
163       char *equals= strchr(buf,'=');
164       if (equals) {
165         *equals++= 0;
166         AnyParameter *param= AnyParameter::find(buf);
167         if (param) {
168           *param= atof(equals);
169           break;
170         } else {
171           cerr << "parameter `" << buf << "' not found\n";
172         }
173       } else if (!strncmp(buf,"postscript ",11)) {
174         const char *filename= buf+11;
175         ofstream file(filename);
176         if (file) {
177           PostScriptOutput psout(file);
178           list->display(psout);
179         } else {
180           cerr << "Failed to open `" << filename << "'\n";
181           continue;
182         }
183         cerr << "PostScript written to `" << filename <<"'\n";
184       } else if (!strcmp(buf,"x11")) {
185         if (x11) {
186           delete x11;
187           x11= 0;
188         } else {
189           x11= new X11Output;
190         }
191         break;
192       } else if (!strcmp(buf,"dualx11")) {
193         if (dualx11) {
194           delete dualx11;
195           dualx11= 0;
196         } else {
197           dualx11= new DualX11Output;
198         }
199         break;
200       } else if (!strcmp(buf,"quit")) {
201         exit(0);
202       } else if (!strcmp(buf,"list")) {
203         AnyParameter::help();
204       } else if (!strcmp(buf,"help")) {
205         AnyParameter::list();
206       } else if (!strncmp(buf,pointmap_cmd,sizeof(pointmap_cmd)-1)) {
207         if (sscanf(buf+sizeof(pointmap_cmd)-1, "%lf %lf",
208                    &pointmap_t, &pointmap_u) != 2) {
209           cerr << "failed to parse pointmap args\n";
210           continue;
211         }
212         Point p= strip->middlepoint(pointmap_t,pointmap_u);
213         cout << "\n";
214         for (int i=0; i<3; i++) cout << p.halftransformed()[i] << "\n";
215       } else if (!*buf) {
216         break;
217       } else {
218         cerr << "command not understood\n";
219       }
220     }
221   }
222 }