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