// Hyperbolic Rogue -- regular honeycombs // works with spherical and hyperbolic ones -- Euclidean cubic tiling implemented in euclid.cpp // hyperbolic honeycombs rely on binary:: to deal with floating point errors (just like archimedean) // Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details namespace hr { #if MAXMDIM >= 4 transmatrix cpush(int cid, ld alpha); transmatrix cspin(int a, int b, ld alpha); extern vector shWall3D, shMiniWall3D; namespace binary { void build_tmatrix(); void virtualRebaseSimple(heptagon*& base, transmatrix& at); int celldistance3(heptagon *c1, heptagon *c2); hyperpoint deparabolic3(hyperpoint h); } namespace reg3 { int loop, face; vector cellshape; transmatrix spins[12], adjmoves[12]; template ld binsearch(ld dmin, ld dmax, const T& f) { for(int i=0; i<200; i++) { ld d = (dmin + dmax) / 2; if(f(d)) dmax = d; else dmin = d; } return dmin; } void generate() { using namespace hyperpoint_vec; if(S7 == 4) face = 3; if(S7 == 6) face = 4; if(S7 == 12) face = 5; if(S7 == 8) face = 3; /* icosahedron not implemented */ loop = ginf[geometry].tiling_name[5] - '0'; println(hlog, "face = ", face, " loop = ", loop, " S7 = ", S7); ld dual_angle = binsearch(0, M_PI, [&] (ld d) { hyperpoint h0 = cpush(0, 1) * C0; hyperpoint h1 = cspin(0, 1, d) * h0; hyperpoint h2 = cspin(1, 2, 2*M_PI/loop) * h1; return hdist(h0, h1) > hdist(h1, h2); }); ld dodecahedron_angle = binsearch(0, M_PI, [&] (ld d) { hyperpoint h0 = cpush(0, 1) * C0; hyperpoint h1 = cspin(0, 1, d) * h0; hyperpoint h2 = cspin(1, 2, 2*M_PI/face) * h1; return hdist(h0, h1) > hdist(h1, h2); }); if(S7 == 8) { /* 24-cell is a special case because it is the only one with '4' in the middle of the Schlaefli symbol. */ /* The computations above assume 3 */ hyperpoint h1 = hpxy3(.5,.5,.5); hyperpoint h2 = hpxy3(.5,.5,-.5); dual_angle = hdist(h1, h2); } println(hlog, "dodecahedron angle = ", dodecahedron_angle); println(hlog, "dual angle = ", dual_angle); ld inp_length = binsearch(0, 1.55, [&] (ld d) { hyperpoint h = xpush(-d) * spin(2*M_PI/face) * xpush0(d); ld alpha = M_PI - atan2(-h[1], h[0]); return (alpha < dual_angle / 2) ? hyperbolic : sphere; }); println(hlog, "inp length = ", inp_length); ld edge_length = hdist(xpush0(inp_length), spin(2*M_PI/face) * xpush0(inp_length)); if(S7 == 8) edge_length = hdist(normalize(hpxyz3(1,1,0,0)), normalize(hpxyz3(1,0,1,0))); println(hlog, "edge length = ", edge_length); hyperpoint h0 = cpush(0, 1) * C0; hyperpoint h1 = cspin(0, 1, dodecahedron_angle) * h0; hyperpoint h2 = cspin(1, 2, 2*M_PI/face) * h1; hyperpoint h3 = cspin(1, 2, -2*M_PI/face) * h1; hyperpoint a2 = S7 == 8 ? normalize(h1 + h2) : normalize(h0 + h1 + h2); hyperpoint a3 = S7 == 8 ? normalize(h1 + h3) : normalize(h0 + h1 + h3); println(hlog, "S7 = ", S7); ld whereonline = binsearch(0, 5, [&] (ld d) { // sometimes breaks in elliptic dynamicval g(geometry, elliptic ? gCell120 : geometry); hyperpoint z2 = a2 * d + C0 * (1-d); if(hyperbolic && intval(z2, Hypc) >= 0) return true; hyperpoint b2 = normalize(z2); hyperpoint z3 = a3 * d + C0 * (1-d); hyperpoint b3 = normalize(z3); return hdist(b2, b3) >= edge_length; }); println(hlog, "whereonline = ", whereonline); a2 = normalize(a2 * whereonline + C0 * (1-whereonline)); a3 = normalize(a3 * whereonline + C0 * (1-whereonline)); hyperpoint mid = Hypc; for(int i=0; i g(geometry, gBinary3); binary::virtualRebaseSimple(alt, T); } fixmatrix(T); auto hT = tC0(T); if(DEB) println(hlog, "searching at ", alt, ":", hT); if(DEB) for(auto& p2: altmap[alt]) println(hlog, "for ", tC0(p2.second), " intval is ", intval(tC0(p2.second), hT)); ld err; for(auto& p2: altmap[alt]) if((err = intval(tC0(p2.second), hT)) < 1e-3) { if(err > worst_error1) println(hlog, format("worst_error1 = %lg", double(worst_error1 = err))); // println(hlog, "YES found in ", isize(altmap[alt])); if(DEB) println(hlog, "-> found ", p2.first); int fb = 0; hyperpoint old = T * (inverse(T1) * tC0(p1.second)); for(int d2=0; d2 worst_error2) println(hlog, format("worst_error2 = %lg", double(worst_error2 = err))); if(p2.first->move(d2)) println(hlog, "error: repeated edge"); p2.first->c.connect(d2, parent, d, false); fb++; } } if(fb != 1) { println(hlog, "found fb = ", fb); println(hlog, old); for(int d2=0; d2c.connect(d, parent, d, false); return parent; } return p2.first; } if(DEB) println(hlog, "-> not found"); heptagon *created = tailored_alloc (S7); created->c7 = newCell(S7, created); created->alt = NULL; created->zebraval = hrand(10); fixmatrix(T); reg_gmatrix[created] = make_pair(alt, T); altmap[alt].emplace_back(created, T); created->c.connect(0, parent, d, false); return created; } }; hrmap* new_map() { return new hrmap_reg3; } hrmap_reg3* regmap() { return ((hrmap_reg3*) currentmap); } heptagon *createStep(heptagon *parent, int d) { return regmap()->createStep(parent, d); } transmatrix relative_matrix(heptagon *h2, heptagon *h1) { auto m = regmap(); auto p1 = m->reg_gmatrix[h1]; auto p2 = m->reg_gmatrix[h2]; transmatrix T = Id; if(hyperbolic) { dynamicval g(geometry, gBinary3); T = binary::relative_matrix(p2.first, p1.first); } return inverse(p1.second) * T * p2.second; } void draw() { sphereflip = Id; // for(int i=0; i(p); transmatrix V = get<1>(p); dynamicval b(band_shift, get<2>(p)); bandfixer bf(V); dq::drawqueue.pop(); cell *c = h->c7; if(!do_draw(c, V)) continue; drawcell(c, V, 0, false); for(int i=0; imove(i), V * relative_matrix(h->move(i), h)); } } int celldistance(cell *c1, cell *c2) { if(c1 == c2) return 0; auto r = regmap(); dynamicval g(geometry, gBinary3); return 1 + binary::celldistance3(r->reg_gmatrix[c1->master].first, r->reg_gmatrix[c2->master].first); } bool pseudohept(cell *c) { if(sphere) { hyperpoint h = tC0(relative_matrix(c->master, regmap()->origin)); if(S7 == 12) { hyperpoint h1 = cspin(0, 1, atan2(16, 69) + M_PI/4) * h; for(int i=0; i<4; i++) if(abs(abs(h1[i]) - .5) > .01) return false; return true; } if(S7 == 8) return h[3] >= .99 || h[3] <= -.99 || abs(h[3]) < .01; if(loop == 3 && face == 3 && S7 == 4) return c == currentmap->gamestart(); if(loop == 4 && face == 3) return abs(h[3]) > .9; if(loop == 3 && face == 4) return abs(h[3]) > .9; if(loop == 5 && face == 3) return abs(h[3]) > .99 || abs(h[0]) > .99 || abs(h[1]) > .99 || abs(h[2]) > .99; } if(hyperbolic) { heptagon *h = regmap()->reg_gmatrix[c->master].first; return (h->zebraval == 1) && (h->distance & 1); } return false; } int dist_alt(cell *c) { return regmap()->reg_gmatrix[c->master].first->distance; } #endif #if 0 /* More precise, but very slow distance. Not used/optimized for now */ ld adistance(cell *c) { hyperpoint h = tC0(regmap()->reg_gmatrix[c->master].second); h = binary::deparabolic3(h); return regmap()->reg_gmatrix[c->master].first->distance * log(2) - h[0]; } int bucketer(ld x) { return int(x * 10 + 100000.5) - 100000; } int bucketer(hyperpoint h) { return bucketer(h[0]) + 1000 * bucketer(h[1]) + 1000000 * bucketer(h[2]); } map close_distances; unordered_map, int> memo; bool cdd; int celldistance(cell *c1, cell *c2) { if(memo.count(make_pair(c1, c2))) return memo[make_pair(c1, c2)]; if(c1 == c2) return 0; vector v[2]; v[0].push_back(c1); v[1].push_back(c2); int steps = 0; map visited; visited[c1] = 1; visited[c2] = 2; while(true) { if(cdd) { println(hlog, "state ", steps, "/",isize(v[0]), "/", isize(v[1])); println(hlog, " A: ", v[0]); println(hlog, " B: ", v[1]); } for(int i: {0,1}) { vector new_v; for(cell *c: v[i]) forCellCM(cn, c) if(adistance(cn) < adistance(c)) { auto &vi = visited[cn]; if((vi&3) == 0) { vi = 4 * (steps+1); vi |= (1<> ca1, ca2; int b1 = 4*steps-4; int b2 = ((vi>>2)<<2) - 4; for(auto p: visited) { if(cdd) println(hlog, p); int ps = p.second & 3; if(ps == 1+i && p.second >= b1) ca1.emplace_back(p.first, p.second/4); if(ps == 2-i && p.second >= b2 && p.second <= b2+8) ca2.emplace_back(p.first, p.second/4); } int bound = 1<<16; for(auto p1: ca1) for(auto p2: ca2) { hyperpoint h = tC0(relative_matrix(p1.first->master, p2.first->master)); int b = bucketer(h); if(close_distances.count(b)) { int d = close_distances[b] + p1.second + p2.second; if(cdd) println(hlog, "candidate: close=", close_distances[b], p1, p2, "; h = ", h); if(d < bound) bound = d; } else if(cdd) println(hlog, "bucket missing"); } return memo[make_pair(c1, c2)] = bound; return bound; } } v[i] = std::move(new_v); } steps++; } } cellwalker target; int tsteps; int dist_alt(cell *c) { if(!target.at) { target = cellwalker(currentmap->gamestart(), 0); tsteps = 0; for(int i=0; i<30; i++) target += wstep, target += rev, tsteps++; } if(specialland == laCamelot) return reg3::celldistance(c, target.at); else { int d = reg3::celldistance(c, target.at) - tsteps; if(d < 10) target += wstep, target += rev, tsteps++; return d; } } #endif } }