# HG changeset patch # User John Tsiombikas # Date 1435433607 -10800 # Node ID f3c5134b491485185d1caf4e37b374e8afc94c8c # Parent ae1c60726c413c14fc7d50cd4d81e5ba40fda98b generalized the surfaces of revolution evaluator a bit diff -r ae1c60726c41 -r f3c5134b4914 src/board.cc --- a/src/board.cc Sat Jun 27 13:53:43 2015 +0300 +++ b/src/board.cc Sat Jun 27 22:33:27 2015 +0300 @@ -60,44 +60,62 @@ #define HINGE_HEIGHT (VSIZE * 0.075) #define PIECE_RAD (0.45 * HSIZE / 5.0) -static const float piece_cp[][3][2] = { - {{0, 0.25}, {1, 0.25}, {2, 0.5}}, - {{2, 0.5}, {2.5, 0.5}, {3, 0.5}}, - {{3, 0.5}, {4, 0.5}, {4, 0}}, - {{4, 0}, {4, -0.5}, {3, -0.5}}, - {{3, -0.5}, {2.5, -0.5}, {0, -0.5}}, - //{{2, -0.5}, {1, -0.25}, {0, -0.25}} +struct BezCurve { + int numcp; + vec2_t *cp; + float scale; }; -static const int piece_ncurves = sizeof piece_cp / sizeof *piece_cp; + +static const vec2_t piece_cp[] = { + {0, 0.25}, + {1, 0.25}, // mid0 + {2, 0.5}, + {2.5, 0.5}, // mid1 + {3, 0.5}, + {4, 0.5}, // mid2 + {4, 0}, + {4, -0.5}, // mid3 + {3, -0.5}, + {2.5, -0.5}, // mid4 + {0, -0.5} +}; +static const BezCurve piece_curve = {sizeof piece_cp / sizeof *piece_cp, (vec2_t*)piece_cp, 0.25 * PIECE_RAD}; + static Vector2 piece_revol(float u, float v, void *cls) { + BezCurve *curve = (BezCurve*)cls; + int nseg = (curve->numcp - 1) / 2; + if(v >= 1.0) v = 1.0 - 1e-6; - int idx = std::min((int)(v * piece_ncurves), piece_ncurves - 1); - float t = fmod(v * (float)piece_ncurves, 1.0); + int cidx = std::min((int)(v * nseg), nseg - 1); + float t = fmod(v * (float)nseg, 1.0); - Vector2 res; - for(int i=0; i<2; i++) { - float mid = piece_cp[idx][1][i]; - res[i] = bezier(piece_cp[idx][0][i], mid, mid, piece_cp[idx][2][i], t); - } - return res * 0.25 * PIECE_RAD; + const vec2_t *cp = curve->cp + cidx * 2; + + float resx = bezier(cp[0].x, cp[1].x, cp[1].x, cp[2].x, t); + float resy = bezier(cp[0].y, cp[1].y, cp[1].y, cp[2].y, t); + return Vector2(resx * curve->scale, resy * curve->scale); } static Vector2 piece_revol_normal(float u, float v, void *cls) { + BezCurve *curve = (BezCurve*)cls; + int nseg = (curve->numcp - 1) / 2; + if(v >= 1.0) v = 1.0 - 1e-6; - int idx = std::min((int)(v * piece_ncurves), piece_ncurves - 1); - float t = fmod(v * (float)piece_ncurves, 1.0); + int cidx = std::min((int)(v * nseg), nseg - 1); + float t = fmod(v * (float)nseg, 1.0); + + const vec2_t *cp = curve->cp + cidx * 2; + Vector2 cp0 = cp[0]; + Vector2 cp1 = cp[1]; + Vector2 cp2 = cp[2]; Vector2 pprev, pnext; for(int i=0; i<2; i++) { - float start = piece_cp[idx][0][i]; - float mid = piece_cp[idx][1][i]; - float end = piece_cp[idx][2][i]; - - pprev[i] = bezier(start, mid, mid, end, t - 0.05); - pnext[i] = bezier(start, mid, mid, end, t + 0.05); + pprev[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t - 0.05); + pnext[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t + 0.05); } float tx = pnext.x - pprev.x; @@ -232,7 +250,7 @@ */ Mesh *piece = new Mesh; - gen_revol(piece, 18, 17, piece_revol, piece_revol_normal, 0); + gen_revol(piece, 18, 17, piece_revol, piece_revol_normal, (void*)&piece_curve); Object *opiece = new Object; opiece->set_mesh(piece);