parser sortof-works
[antikythera] / src / machine.cc
index d6bb338..441826e 100644 (file)
@@ -3,6 +3,7 @@
 #include <math.h>
 #include <float.h>
 #include <assert.h>
+#include "opengl.h"
 #include "machine.h"
 
 static float delta_angle(float a, float b);
@@ -30,6 +31,10 @@ Machine::~Machine()
 
 void Machine::add_gear(Gear *g)
 {
+       if(gearidx.find(g) != gearidx.end()) {
+               return; // already have this gear
+       }
+       gearidx[g] = gears.size();
        gears.push_back(g);
        meshing_valid = false;
 }
@@ -42,6 +47,15 @@ void Machine::add_motor(int gearidx, float speed_hz)
        motors.push_back(m);
 }
 
+int Machine::get_gear_index(Gear *g) const
+{
+       std::map<Gear*, int>::const_iterator it = gearidx.find(g);
+       if(it == gearidx.end()) {
+               return -1;
+       }
+       return it->second;
+}
+
 void Machine::invalidate_meshing()
 {
        meshing_valid = false;
@@ -67,14 +81,17 @@ void Machine::calc_meshing()
        // we're going to need the planar position of each gear on its plane, so let's cache it
        Vec3 *ppos = (Vec3*)alloca(ngears * sizeof *ppos);
        for(int i=0; i<ngears; i++) {
-               ppos[i] = gears[i]->get_planar_position();
+               ppos[i] = gears[i]->get_position();
        }
 
        for(int i=0; i<ngears; i++) {
                for(int j=i; j<ngears; j++) {
                        meshing[i][j] = meshing[j][i] = false;
 
-                       if(i == j) continue;
+                       if(i == j || gears[i]->get_super() == gears[j] || gears[j]->get_super() == gears[i]) {
+                               // don't attempt meshing if it's the same gear, or they are attached to each other
+                               continue;
+                       }
 
                        if(1.0 - fabs(dot(gears[i]->axis, gears[j]->axis)) < 1e-5) {
                                // co-planar, just check Z range after inverse-transforming to the XY plane
@@ -105,9 +122,9 @@ void Machine::calc_meshing()
        // fix the initial angles so that teeth mesh as best as possible
        // should work in one pass as long as the gear train is not impossible
        for(int i=0; i<ngears; i++) {
-               float rnd = gears[i]->angle + gears[i]->get_angular_pitch() / 2.0;
+               /*float rnd = gears[i]->angle + gears[i]->get_angular_pitch() / 2.0;
                float snap = rnd - fmod(rnd, gears[i]->get_angular_pitch());
-               gears[i]->set_angle(snap);
+               gears[i]->set_angle(snap);*/
                gears[i]->set_angular_offset(0);
        }
 
@@ -130,30 +147,56 @@ void Machine::calc_meshing()
                        }
                }
        }
+
+       /*
+       printf("meshing graph\n");
+       for(int i=0; i<ngears; i++) {
+               putchar(' ');
+               for(int j=0; j<ngears; j++) {
+                       printf("| %d ", meshing[i][j] ? 1 : 0);
+               }
+               printf("|\n");
+       }
+       */
 }
 
 void Machine::update_gear(int idx, float angle)
 {
+       Gear *gear = gears[idx];
+
        if(visited[idx]) {
-               if(delta_angle(angle, gears[idx]->angle) > 0.25 / gears[idx]->nteeth) {
+               if(delta_angle(angle, gear->angle) > 0.25 / gear->nteeth) {
                        fprintf(stderr, "warning: trying to transmit different values to gear %s (%d)\n",
-                                       gears[idx]->name.c_str(), idx);
-                       gears[idx]->angle = 0;
+                                       gear->name.c_str(), idx);
+                       gear->angle = 0;
                }
                return;
        }
 
-       gears[idx]->set_angle(angle);
+       gear->set_angle(angle);
        visited[idx] = true;
 
+       // propagate to meshing gears (depth-first)
        int ngears = (int)gears.size();
        for(int i=0; i<ngears; i++) {
                if(!meshing[idx][i]) continue;
                assert(idx != i);
 
-               float ratio = -(float)gears[idx]->nteeth / (float)gears[i]->nteeth;
+               float ratio = -(float)gear->nteeth / (float)gears[i]->nteeth;
                update_gear(i, angle * ratio);
        }
+
+       // propagate to rigidly attached gears
+       if(gear->supergear) {
+               int supidx = gearidx[gear->supergear];
+               update_gear(supidx, angle);
+       }
+
+       int nsub = (int)gear->subgears.size();
+       for(int i=0; i<nsub; i++) {
+               int subidx = gearidx[gear->subgears[i]];
+               update_gear(subidx, angle);
+       }
 }
 
 void Machine::update(float dt)
@@ -179,6 +222,19 @@ void Machine::draw() const
        for(size_t i=0; i<gears.size(); i++) {
                gears[i]->draw();
        }
+
+       float dcol[] = {0.4, 0.4, 0.4, 1.0};
+       float scol[] = {0, 0, 0, 0};
+       glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
+       glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
+
+       glBegin(GL_QUADS);
+       glNormal3f(0, 1, 0);
+       glVertex3f(-300, -100, 300);
+       glVertex3f(300, -100, 300);
+       glVertex3f(300, -100, -300);
+       glVertex3f(-300, -100, -300);
+       glEnd();
 }
 
 Gear *Machine::intersect_gear(const Ray &ray, HitPoint *hitp) const