// Hyperbolic Rogue // Copyright (C) 2011-2016 Zeno Rogue, see 'hyper.cpp' for details // Hyperbolic geometry is a good tool to visualize data, // especially trees and scale-free networks. This file // uses HyperRogue to browse such vizualizations. // Since it is not of use for general HyperRogue players, // it is disabled by default -- compile with the ROGUEVIZ flag to enable this. // How to use: // hyper -embed -- visualize a social network // embedded into hyperbolic plane, see: // https://bitbucket.org/HaiZhung/hyperbolic-embedder/overview // (it uses the same format) // hyper -tol -- visualize the tree of life, // based on a XML dump from https://tree.opentreeoflife.org/ // hyper -tess -- visualize a horocyclic tesselation, #ifndef CAP_ROGUEVIZ #define CU 1 #include "init.cpp" #endif #include "rogueviz.h" namespace rogueviz { const transmatrix centralsym = {{{-1,0,0}, {0,-1,0}, {0,0,-1}}}; using namespace hr; edgetype default_edgetype = { .1, DEFAULT_COLOR, "default" }; void init(); bool showlabels = false; bool specialmark = false; bool edge_legend = false; bool rog3 = false; int vertex_shape = 1; string edgename; string fname; // const char *fname; // const char *cfname; enum eVizkind { kNONE, kAnyGraph, kTree, kSpiral, kSAG, kCollatz, kFullNet, kKohonen }; eVizkind kind; bool on; vector> edgetypes; edgetype *add_edgetype(const string& name) { auto e = make_shared (default_edgetype); e->name = name; edgetypes.push_back(e); return &*e; } colorpair parse(const string& s) { colorpair cp; cp.shade = 0; cp.color2 = 0; sscanf(s.c_str(), "%x:%c%x", &cp.color1, &cp.shade, &cp.color2); return cp; } int nh = 0; int hues[256*6]; void buildhue() { unsigned mh = 193; for(unsigned y=0; y<=mh; y++) hues[nh++] = (int) (0xFF + 0x1000000*mh + (unsigned) 0x10000 * y); for(unsigned y=0; y<=mh; y++) hues[nh++] = (int) (0xFF + 0x1010000*mh - 0x1000000 * y); for(unsigned y=0; y<=mh; y++) hues[nh++] = (int) (0xFF + 0x0010000*mh + 0x100 * y); for(unsigned y=0; y<=mh; y++) hues[nh++] = (int) (0xFF + 0x0010100*mh - 0x10000 * y); for(unsigned y=0; y<=mh; y++) hues[nh++] = (int) (0xFF + 0x0000100*mh + 0x1000000 * y); for(unsigned y=0; y<=mh; y++) hues[nh++] = (int) (0xFF + 0x1000100*mh - 0x100 * y); } int perturb(int c) { if(nh == 0) buildhue(); int hueid = 0; for(int t=0; t> (8*u)) & 0xFF; } int if(part[1] == 255 && part[2] == 0) int k = k += rand() % 16; k -= rand() % 16; if(k<0) k=-k; if(k>255) k = 255-(k-255); c &=~ (0xFF << (8*u)); c |= k << (8*u); } */ return c; } colorpair perturb(colorpair cp) { cp.color1 = perturb(cp.color1); cp.color2 = perturb(cp.color2); return cp; } vector vdata; transmatrix cspin(int i, int ch) { return spin(M_PI + (2 * M_PI * (i+1)) / (ch+1)); } map labeler; int getid(const string& s) { if(labeler.count(s)) return labeler[s]; else { int id = isize(vdata); vdata.resize(isize(vdata) + 1); vdata[id].name = s; return labeler[s] = id; } } int getnewid(string s) { while(labeler.count(s)) s += "'"; return getid(s); } void addedge0(int i, int j, edgeinfo *ei) { vdata[i].edges.push_back(make_pair(j, ei)); vdata[j].edges.push_back(make_pair(i, ei)); } void createViz(int id, cell *c, transmatrix at) { vertexdata& vd(vdata[id]); vd.m = new shmup::monster; vd.m->pid = id; vd.m->type = moRogueviz; vd.m->base = c; vd.m->at = at; } void notimpl() { printf("Not implemented\n"); exit(1); } hyperpoint where(int i, cell *base) { auto m = vdata[i].m; if(m->base == base) return tC0(m->at); else if(quotient || elliptic || torus) { return calc_relative_matrix(m->base, base, C0) * tC0(m->at); } else { // notimpl(); // actually probably that's a buug return inverse(ggmatrix(currentmap->gamestart())) * (ggmatrix(m->base) * tC0(m->at)); } } void addedge(int i, int j, edgeinfo *ei) { cell *base = (quotient || elliptic || torus) ? vdata[i].m->base : currentmap->gamestart(); hyperpoint hi = where(i, base); hyperpoint hj = where(j, base); double d = hdist(hi, hj); if(d >= 4) { // printf("splitting %lf\n", d); hyperpoint h = mid(hi, hj); int id = isize(vdata); vdata.resize(id+1); vertexdata& vd(vdata[id]); vd.cp = colorpair(0x400000FF); vd.virt = ei; createViz(id, base, rgpushxto0(h)); vd.m->no_targetting = true; addedge(i, id, ei); addedge(id, j, ei); virtualRebase(vdata[i].m, true); } else addedge0(i, j, ei); } vector edgeinfos; void addedge(int i, int j, double wei, bool subdiv, edgetype *t) { edgeinfo *ei = new edgeinfo(t); edgeinfos.push_back(ei); ei->i = i; ei->j = j; ei->weight = wei; if(subdiv) addedge(i, j, ei); else addedge0(i, j, ei); } void storeall(int from) { for(int i=from; istore(); } int dftcolor = 0x282828FF; namespace spiral { ld mul; transmatrix at(double d) { return spin(log(d) * 2 * M_PI / log(mul)) * xpush(log(d)); } void place(int N, ld _mul) { mul = _mul; init(); kind = kSpiral; vdata.resize(N); for(int i=0; ivisible_from = 1. / (N+.5); for(int i=0; i= 0 && j0 < N) addedge(i, j0, 1/(i+1), false, t); } } void color(ld start, ld period, colorpair c) { int N = isize(vdata); int maxw = N; while(start >= 0 && start < N) { int i = int(start); vdata[i].cp = c; start += period; maxw--; if(maxw <= 0) break; } } } namespace collatz { double s2, s3, p2, p3; double cshift = -1; edgetype *collatz1, *collatz2; void start() { init(); kind = kCollatz; collatz1 = add_edgetype("1"); collatz2 = add_edgetype("2"); vdata.resize(1); vertexdata& vd = vdata[0]; createViz(0, cwt.at, xpush(cshift)); virtualRebase(vd.m, true); vd.cp = perturb(dftcolor); vd.data = 0; addedge(0, 0, 1, false, collatz::collatz1); vd.name = "1"; storeall(); } } string readLabel_s(FILE *f) { char xlabel[10000]; if(fscanf(f, "%9500s", xlabel) <= 0) return ""; return xlabel; } int readLabel(FILE *f) { string s = readLabel_s(f); if(s == "") return -1; return getid(s); } namespace anygraph { double R, alpha, T; vector > coords; edgetype *any; int N; void fixedges() { for(int i=N; idead = true; for(int i=0; iorig = NULL; addedge(e->i, e->j, e); } } void read(string fn, bool subdiv, bool doRebase, bool doStore) { init(); kind = kAnyGraph; any = add_edgetype("embedded edges"); fname = fn; FILE *f = fopen((fn + "-coordinates.txt").c_str(), "rt"); if(!f) { printf("Missing file: %s-coordinates.txt\n", fname.c_str()); exit(1); } printf("Reading coordinates...\n"); char buf[100]; int err; err = fscanf(f, "%s%s%s%s%d%lf%lf%lf", buf, buf, buf, buf, &N, &anygraph::R, &anygraph::alpha, &anygraph::T); if(err < 8) { printf("Error: incorrect format of the first line\n"); exit(1); } vdata.reserve(N); while(true) { string s = readLabel_s(f); if(s == "" || s == "#ROGUEVIZ_ENDOFDATA") break; int id = getid(s); vertexdata& vd(vdata[id]); vd.name = s; vd.cp = colorpair(dftcolor); double r, alpha; int err = fscanf(f, "%lf%lf", &r, &alpha); coords.push_back(make_pair(r, alpha)); if(err < 2) { printf("Error: incorrect format of r/alpha\n"); exit(1); } transmatrix h = spin(alpha * 2 * M_PI / 360) * xpush(r); createViz(id, currentmap->gamestart(), h); } fclose(f); f = fopen((fn + "-links.txt").c_str(), "rt"); if(!f) { printf("Missing file: %s-links.txt\n", fname.c_str()); exit(1); } printf("Reading links...\n"); int qlink = 0; while(true) { int i = readLabel(f), j = readLabel(f); if(i == -1 || j == -1) break; addedge(i, j, 0, subdiv, any); qlink++; } fclose(f); if(doRebase) { printf("Rebasing...\n"); for(int i=0; i children; }; vector tol; void child(int pid, int id) { if(isize(tol) <= id) tol.resize(id+1); treevertex& v = tol[id]; v.parent = pid; tol.push_back(v); if(pid >= 0) tol[pid].children.push_back(id); } void readnode(FILE *f, int pid) { string lab = ""; while(true) { int c = fgetc(f); if(c == EOF) { fprintf(stderr, "Ended prematurely\n"); exit(1); } if(c == ',') break; if(c == ')') { int id = getnewid(lab); child(pid, id); return; } lab += c; } int id = getnewid(lab); child(pid, id); while(true) { int c = fgetc(f); // printf("c=%c at %d/%d\n", c, pid, id); if(c == EOF) { fprintf(stderr, "Ended prematurely\n"); exit(1); } if(c == ' ' || c == 10 || c == 13 || c == 9 || c == ',') continue; else if(c == '(') readnode(f, id); else if(c == ')') break; } } int xpos; void spos(int at, int d) { tol[at].spos = xpos++; tol[at].depth = d; for(int i=0; ipid = i; vd.data = lv.parent; createViz(i, cwt.at, h); vd.cp = dftcolor; if(tol[i].parent >= 0) addedge(i, tol[i].parent, 1, true, tree_edge); } for(int i=0; i snakecells; vector snakefirst, snakelast; vector snakenode; vector snakeid; vector lpbak; vector wpbak; bool snake_enabled; void setsnake(cellwalker& cw, int i) { lpbak[i] = cw.at->landparam; wpbak[i] = cw.at->wparam; cw.at->landparam = i; cw.at->wparam = INSNAKE; // cw.at->monst = moWormtail; cw.at->mondir = cw.spin; snakecells[i] = cw.at; } void snakeswitch() { for(int i=0; ilandparam; c->landparam = x; x = wpbak[i]; wpbak[i] = c->wparam; c->wparam = x; } snake_enabled = !snake_enabled; } void enable_snake() { if(!snake_enabled) snakeswitch(); } void disable_snake() { if(snake_enabled) snakeswitch(); } int snakedist(int i, int j) { if(i < insnaketab && j < insnaketab) return sdist[i][j]; if(bounded) return celldistance(snakecells[i], snakecells[j]); int i0 = i, i1 = i, j0 = j, j1 = j; int cost = 0; // intersect while(true) { if(j0 > i1+1) { j0 = snakefirst[j0], j1 = snakelast[j1]; cost++; } else if(i0 > j1+1) { i0 = snakefirst[i0], i1 = snakelast[i1]; cost++; } else if(j1+1 == i0) return cost+1; else if(i1+1 == j0) return cost+1; else return cost; } } void initSnake(int n) { if(bounded) n = isize(currentmap->allcells()); numsnake = n; snakecells.resize(numsnake); snakefirst.resize(numsnake); snakelast.resize(numsnake); snakenode.resize(numsnake); lpbak.resize(numsnake); wpbak.resize(numsnake); if(bounded) { for(int i=0; iallcells()[i], 0); setsnake(cw, i); } } else { cellwalker cw = cwt; setsnake(cw, 0); cw += wstep; setsnake(cw, 1); for(int i=2; i<=numsnake; i++) { if(i == numsnake && sphere) break; cw += wstep; snakefirst[i-1] = cw.at->landparam; while(cw.at->wparam == INSNAKE) { snakelast[i-1] = cw.at->landparam; cw = cw + wstep + 1 + wstep; } if(i == numsnake) break; setsnake(cw, i); cw += 1; } } int stab = min(numsnake, MAXSNAKETAB); for(int i=0; iweight2; } /* cell *c = snakecells[id]; for(int i=0; itype; i++) { cell *c2 = c->move(i); if(c2 && c2->wparam == INSNAKE && snakenode[c2->landparam] >= 0) cost += 100; } */ return cost; } // std::mt19937 los; bool infullsa; double cost; int N; vector chgs; edgetype *sag_edge; void forgetedges(int id) { for(int i=0; iorig = NULL; } bool chance(double p) { p *= double(hrngen.max()) + 1; auto l = hrngen(); auto pv = (decltype(l)) p; if(l < pv) return true; if(l == pv) return chance(p-pv); return false; } void saiter() { aiter: int t1 = hrand(N); int sid1 = snakeid[t1]; int sid2; int s = hrand(6); if(s == 3) s = 2; if(s == 4) s = 5; if((sagpar&1) && (s == 2 || s == 3 || s == 4)) return; if(s == 5) sid2 = hrand(numsnake); else { cell *c; if(s>=2 && isize(vdata[t1].edges)) c = snakecells[snakeid[hrand(isize(vdata[t1].edges))]]; else c = snakecells[sid1]; int it = s<2 ? (s+1) : s-2; for(int ii=0; iitype); c = c->move(d); if(!c) goto aiter; if(c->wparam != INSNAKE) goto aiter; } sid2 = c->landparam; } int t2 = snakenode[sid2]; snakenode[sid1] = -1; snakeid[t1] = -1; snakenode[sid2] = -1; if(t2 >= 0) snakeid[t2] = -1; double change = costat(t1,sid2) + costat(t2,sid1) - costat(t1,sid1) - costat(t2,sid2); snakenode[sid1] = t1; snakeid[t1] = sid1; snakenode[sid2] = t2; if(t2 >= 0) snakeid[t2] = sid2; if(change < 0) chgs.push_back(-change); if(change > 0 && (sagmode == sagHC || !chance(exp(-change * exp(-temperature))))) return; snakenode[sid1] = t2; snakenode[sid2] = t1; snakeid[t1] = sid2; if(t2 >= 0) snakeid[t2] = sid1; if(vdata[t1].m) vdata[t1].m->base = snakecells[sid2]; if(t2 >= 0 && vdata[t2].m) vdata[t2].m->base = snakecells[sid1]; cost += 2*change; if(t1 >= 0) forgetedges(t1); if(t2 >= 0) forgetedges(t2); } void organize() { for(int i=0; i freenodes; for(int i=0; i= numsnake || err < 1) sid = -1; if(!labeler.count(lab)) { printf("unknown vertex: %s\n", lab.c_str()); } else { int id = getid(lab); snakeid[id] = sid; } } afterload: if(sf) fclose(sf); organize(); for(int i=0; ibase = snakecells[sag::snakeid[i]]; forgetedges(i); } shmup::fixStorage(); } vector sagedges; /* bool totcmp(int i, int j) { return totwei[i] > totwei[j]; } */ int ipturn = 100; int numiter = 0; int hightemp = 10; int lowtemp = -15; void dofullsa(int satime) { sagmode = sagSA; enable_snake(); int t1 = SDL_GetTicks(); while(true) { int t2 = SDL_GetTicks(); double d = (t2-t1) / (1000. * satime); if(d > 1) break; temperature = hightemp - (d*(hightemp-lowtemp)); chgs.clear(); for(int i=0; i<50000; i++) { numiter++; sag::saiter(); } printf("it %8d temp %6.4" PLDF" [1/e at %13.6" PLDF"] cost = %lf ", numiter, sag::temperature, (ld) exp(sag::temperature), sag::cost); sort(chgs.begin(), chgs.end()); int cc = chgs.size() - 1; printf("%9.4lf .. %9.4lf .. %9.4lf .. %9.4lf .. %9.4lf\n", chgs[0], chgs[cc/4], chgs[cc/2], chgs[cc*3/4], chgs[cc]); fflush(stdout); } temperature = -5; disable_snake(); sagmode = sagOff; } void iterate() { if(!sagmode) return; int t1 = SDL_GetTicks(); enable_snake(); for(int i=0; i 200) ipturn /= 2; else ipturn = ipturn * 100 / t; printf("it %8d temp %6.4" PLDF" [2:%8.6" PLDF",10:%8.6" PLDF",50:%8.6" PLDF"] cost = %lf\n", numiter, sag::temperature, exp(-2 * exp(-sag::temperature)), exp(-10 * exp(-sag::temperature)), exp(-50 * exp(-sag::temperature)), sag::cost); } void savesnake(const string& fname) { FILE *f = fopen(fname.c_str(), "wt"); for(int i=0; i= sag_edge->visible_from) pedge[snakedist(snakeid[ei.i], snakeid[ei.j])]++; } for(int d=0; d<30; d++) if(indist[d]) printf("%2d: %7d/%7d %7.3lf\n", d, pedge[d], indist[d], double(pedge[d] * 100. / indist[d])); ld loglik = 0; for(int d=0; d<30; d++) { int p = pedge[d], pq = indist[d]; int q = pq - p; if(p && q) loglik += p * log(p) + q * log(q) - pq * log(pq); } printf("loglikelihood = %lf\n", (double) loglik); } void readsag(const char *fname) { maxweight = 0; sag_edge = add_edgetype("SAG edge"); FILE *f = fopen(fname, "rt"); if(!f) { printf("Failed to open SAG file: %s\n", fname); exit(1); } // while(fgetc(f) != 10 && fgetc(f) != 13 && !feof(f)) ; while(!feof(f)) { string l1, l2; while(true) { int c = fgetc(f); if(c == EOF) { fclose(f); return; } else if(c == ';') break; else if(c == 10 || c == 13 || c == 32 || c == 9) ; else l1 += c; } while(true) { int c = fgetc(f); if(c == EOF) { fclose(f); return; } else if(c == ';') break; else if(c == 10 || c == 13 || c == 32 || c == 9) ; else l2 += c; } double wei; int err = fscanf(f, "%lf", &wei); if(err < 1) continue; edgeinfo ei(sag_edge); ei.i = getid(l1); ei.j = getid(l2); ei.weight = wei; sagedges.push_back(ei); } } ld edgepower=1, edgemul=1; void read(string fn) { fname = fn; init(); kind = kSAG; temperature = 0; sagmode = sagOff; readsag(fname.c_str()); N = isize(vdata); // totwei.resize(N); // for(int i=0; i= maxwei[ei.i] / 5 || ei.weight >= maxwei[ei.j] / 5); ei.weight2 = pow((double) ei.weight, (double) edgepower) * edgemul; // LANG:: pow(ei.weight, .4) / 50; // ei.weight2 = 0; int w = ei.weight; while(w) { w >>= 1; ei.weight2++; } /* if(totwei[ei.i] <= 0 || totwei[ei.j] <= 0) { printf("BAD TOTWEI\n"); exit(1); } ei.weight2 = 3 * ( sqrt(ei.weight * 1. / totwei[ei.i]) * log(totwei[ei.i]) * log(totwei[ei.i]) + sqrt(ei.weight * 1. / totwei[ei.j]) * log(totwei[ei.j]) * log(totwei[ei.j])); */ // printf("%f\n", ei.weight2); addedge0(ei.i, ei.j, &ei); } initSnake(N*2); printf("numsnake = %d\n", numsnake); if(numsnake < N) { printf("Error: snake does not fit\n"); exit(1); } snakeid.resize(N); for(int i=0; iweight > e2->weight; } bool which_weight = false; void rogueviz_help(int id, int pagenumber) { vertexdata& vd = vdata[id]; int noedges = isize(vd.edges); help = helptitle(vd.name, vd.cp.color1 >> 8); if(vd.info) { #if CAP_URL help_extension hex; hex.key = 'L'; hex.text = "open link"; hex.subtext = *vd.info; hex.action = [&vd] () { open_url(*vd.info); }; help_extensions.push_back(hex); #else help += "\n\nlink: " + *vd.info; #endif } vector alledges; for(int j=0; jweight < ei->type->visible_from) continue; int k = ei->i ^ ei->j ^ id; hex.text = vdata[k].name; hex.color = vdata[k].cp.color1 >> 8; if(kind == kSAG) { if(which_weight) hex.subtext = fts(ei->weight2); else hex.subtext = fts(ei->weight); } hex.action = [k] () { help_extensions.clear(); rogueviz_help(k, 0); }; help_extensions.push_back(hex); } if(noedges > pagenumber + 10) { help_extension hex; hex.key = 'z'; hex.text = "next page"; hex.subtext = its(pagenumber+10) + "/" + its(noedges) + " edges"; hex.action = [id, pagenumber] () { help_extensions.clear(); rogueviz_help(id, pagenumber + 10); }; help_extensions.push_back(hex); } if(kind == kSAG && noedges) { help_extension hex; hex.key = 'w'; hex.text = "displayed weight"; hex.subtext = which_weight ? "attraction force" : "weight from the data"; hex.action = [id, pagenumber] () { which_weight = !which_weight; help_extensions.clear(); rogueviz_help(id, pagenumber); }; help_extensions.push_back(hex); } } bool describe_monster(shmup::monster *m, string& out) { if(m->type != moRogueviz) return false; int i = m->pid; vertexdata& vd = vdata[i]; string o = vd.name + ", "+its(isize(vd.edges))+" edges"; /* if(isize(vd.edges) < 10) { for(int i=0; isnakeid)); } */ help = bygen([i] () { rogueviz_help(i, 0); }); if(out == XLATN("Canvas")) out = o; else out = out + ", " + o; return true; } bool activate(shmup::monster *m) { if(m->type != moRogueviz) return false; int i = m->pid; vertexdata& vd = vdata[i]; vd.cp = colorpair(rand() & 0xFFFFFFFF); for(int i=0; iorig = NULL; return true; /* if(ealpha == 1) ealpha = 8; else if(ealpha == 8) ealpha = 32; else if(ealpha == 32) ealpha = 255; else ealpha = 1; */ } void storevertex(vector& tab, const hyperpoint& h) { tab.push_back(glhr::pointtogl(h)); } double linequality = .1; void storelineto(vector& tab, const hyperpoint& h1, const hyperpoint& h2) { if(intval(h1, h2) < linequality) storevertex(tab, h2); else { hyperpoint h3 = mid(h1, h2); storelineto(tab, h1, h3); storelineto(tab, h3, h2); } } void storeline(vector& tab, const hyperpoint& h1, const hyperpoint& h2) { storevertex(tab, h1); storelineto(tab, h1, h2); } hpcshape *vshapes[4] = { &shDisk, &shDisk, &shHeptaMarker, &shSnowball }; color_t darken_a(color_t c) { for(int p=0; p<3; p++) for(int i=0; i> 1; return c; } void queuedisk(const transmatrix& V, const colorpair& cp, bool legend, const string* info) { if(legend && (int) cp.color1 == (int) 0x000000FF && backcolor == 0) poly_outline = 0x606060FF; else poly_outline = (bordcolor << 8) | 0xFF; transmatrix V1; auto& sh = *(vshapes[vertex_shape]); if(vertex_shape == 0) ; else if(rog3) { int p = poly_outline; poly_outline = OUTLINE_TRANS; queuepolyat(V, sh, 0x80, PPR::MONSTER_SHADOW); poly_outline = p; if(info) queueaction(PPR::MONSTER_HEAD, [info] () { svg::link = *info; }); queuepolyat(V1 = mscale(V, geom3::BODY), sh, darken_a(cp.color1), PPR::MONSTER_HEAD); if(info) queueaction(PPR::MONSTER_HEAD, [] () { svg::link = ""; }); } else { if(info) queueaction(PPR::MONSTER_HEAD, [info] () { svg::link = *info; }); queuepoly(V1 = V, sh, darken_a(cp.color1)); if(info) queueaction(PPR::MONSTER_HEAD, [] () { svg::link = ""; }); } if(cp.shade == 't') queuepoly(V1, shDiskT, darken_a(cp.color2)); if(cp.shade == 's') queuepoly(V1, shDiskS, darken_a(cp.color2)); if(cp.shade == 'q') queuepoly(V1, shDiskSq, darken_a(cp.color2)); if(cp.shade == 'm') queuepoly(V1, shDiskM, darken_a(cp.color2)); } unordered_map, int> drawn_edges; map, transmatrix> relmatrices; transmatrix& memo_relative_matrix(cell *c1, cell *c2) { auto& p = relmatrices[make_pair(c1, c2)]; if(p[2][2] == 0) p = calc_relative_matrix(c1, c2, C0); return p; } bool drawVertex(const transmatrix &V, cell *c, shmup::monster *m) { if(m->dead) return true; int i = m->pid; vertexdata& vd = vdata[i]; bool ghilite = false; if(vd.special && specialmark) ghilite = true; if(!gmatrix.count(m->base)) printf("base not in gmatrix\n"); int lid = shmup::lmousetarget ? shmup::lmousetarget->pid : -2; if(!leftclick) for(int j=0; jweight < ei->type->visible_from) continue; vertexdata& vd1 = vdata[ei->i]; vertexdata& vd2 = vdata[ei->j]; int oi = ei->i, oj = ei->j; bool hilite = false; if(vdata[oi].special && vdata[oj].special && specialmark) hilite = true; else if(svg::in || inHighQual) hilite = false; else if(vd1.m == shmup::lmousetarget) hilite = true; else if(vd2.m == shmup::lmousetarget) hilite = true; else if(oi == lid || oj == lid) hilite = true; if(hilite) ghilite = true; bool multidraw = quotient || torus; if(ei->lastdraw < frameid || multidraw) { ei->lastdraw = frameid; color_t col = ei->type->color; auto& alpha = part(col, 0); if(kind == kSAG) { if(ei->weight2 > maxweight) maxweight = ei->weight2; ld gamma = log(alpha / 255.) / log(.5); alpha = int(pow(ei->weight2 / maxweight, gamma) * 255); } if(hilite || hiliteclick) alpha = (alpha + 256) / 2; if(svg::in && alpha < 16) continue; if(ISWEB) { if(alpha >= 128) alpha |= 15; else if(alpha >= 64) alpha |= 7; else if(alpha >= 32) alpha |= 3; else if(alpha >= 16) alpha |= 1; } alpha >>= darken; transmatrix gm1 = (multidraw || elliptic) ? V * memo_relative_matrix(vd1.m->base, c) : ggmatrix(vd1.m->base); transmatrix gm2 = (multidraw || elliptic) ? V * memo_relative_matrix(vd2.m->base, c) : ggmatrix(vd2.m->base); hyperpoint h1 = gm1 * vd1.m->at * C0; hyperpoint h2 = gm2 * vd2.m->at * C0; if(elliptic && intval(h1, h2) > intval(h1, centralsym * h2)) h2 = centralsym * h2; if(multidraw) { int code = int(h1[0]) + int(h1[1]) * 12789117 + int(h2[0]) * 126081253 + int(h2[1]) * 126891531; int& lastdraw = drawn_edges[make_pair(ei, code)]; if(lastdraw == frameid) continue; lastdraw = frameid; } /* if(hdist0(h1) < .001 || hdist0(h2) < .001) { printf("h1 = %s\n", display(h1)); printf("h2 = %s\n", display(h2)); display(m->at); display(vd2.m->at); display(V); display(gmatrix[vd2.m->base]); display(shmup::calc_gmatrix(vd2.m->base)); } */ if(hilite) { col &= 0xFF; col |= 0xFF000000; } else if((col >> 8) == (DEFAULT_COLOR >> 8)) { col &= 0xFF; col |= (forecolor << 8); } bool onspiral = kind == kSpiral && abs(ei->i - ei->j) == 1; if(pmodel || onspiral) { if(onspiral) { const int prec = 20; transmatrix T = ggmatrix(currentmap->gamestart()); hyperpoint l1 = T*tC0(spiral::at(1+ei->i)); for(int z=1; z<=prec; z++) { hyperpoint l2 = T*tC0(spiral::at(1+ei->i+(ei->j-ei->i) * z / (prec+.0))); queueline(l1, l2, col, vid.linequality).prio = PPR::STRUCT0; l1 = l2; } } else { queueline(h1, h2, col, 2 + vid.linequality).prio = PPR::STRUCT0; } } else { cell *center = multidraw ? c : euclid ? cwt.at : viewctr.at->c7; if(!multidraw && ei->orig && ei->orig != center && celldistance(ei->orig, center) > 3) ei->orig = NULL; if(!ei->orig) { ei->orig = center; // cwt.at; ei->prec.clear(); transmatrix T = inverse(ggmatrix(ei->orig)); if(kind == kSpiral && abs(ei->i - ei->j) == 1) { ei->orig = currentmap->gamestart(); hyperpoint l1 = tC0(spiral::at(1+ei->i)); storevertex(ei->prec, l1); const int prec = 20; for(int z=1; z<=prec; z++) { hyperpoint l2 = tC0(spiral::at(1+ei->i+(ei->j-ei->i) * z / (prec+.0))); storeline(ei->prec, l1, l2); l1 = l2; } } else storeline(ei->prec, T*h1, T*h2); } queuetable(multidraw ? V : ggmatrix(ei->orig), ei->prec, isize(ei->prec), col, 0, PPR::STRUCT0); if(elliptic) queuetable(centralsym * ggmatrix(ei->orig), ei->prec, isize(ei->prec), col, 0, PPR::STRUCT0); } } /* */ } if(!vd.virt) { queuedisk(V * m->at, ghilite ? colorpair(0xFF0000FF) : vd.cp, false, vd.info); } if(showlabels) { bool doshow = true; if(kind == kTree && i > 0 && !vd.virt) { vertexdata& vdp = vdata[vd.data]; hyperpoint h2 = ggmatrix(vdp.m->base) * vdp.m->at * C0; if(hdist(h2, V * m->at * C0) < 0.1) doshow = false; } hyperpoint h = tC0(V * m->at); transmatrix V2 = rgpushxto0(h) * ypush(PURE ? .3 : .2); // todo-variation if(doshow && !behindsphere(V2)) { auto info = vd.info; if(info) queueaction(PPR::MONSTER_HEAD, [info] () { svg::link = *info; }); queuestr(V2, (svg::in ? .28 : .2) * crossf / hcrossf, vd.name, backcolor ? 0x000000 : 0xFFFF00, (svg::in || ISWEB) ? 0 : 1); if(info) queueaction(PPR::MONSTER_HEAD, [] () { svg::link = ""; }); } } if(kind == kCollatz) { if(c->cpdist > 7 && euclid) ; else if(vd.data == 2) { // doubler vertex string s = vd.name; colorpair cp = vd.cp; vd.data = 20; int i0 = isize(vdata); vdata.resize(i0+1); vertexdata& vdn = vdata[i0]; createViz(i0, m->base, m->at * spin(collatz::s2) * xpush(collatz::p2)); virtualRebase(vdn.m, true); vdn.cp = perturb(cp); vdn.data = 0; addedge(i, i0, 1, false, collatz::collatz1); vdn.m->store(); int carry = 0; string s2 = s; for(int i=isize(s2)-1; i>=0; i--) { int x = 2*(s2[i] - '0') + carry; carry = x>=10; if(carry) x-=10; s2[i] = '0'+x; } if(carry) s2 = "1" + s2; vdn.name = s2; int m3 = 0; for(int i=0; ibase, m->at * spin(collatz::s3) * xpush(collatz::p3)); virtualRebase(vdn.m, true); vdn.cp = perturb(cp); vdn.data = 0; addedge(i, i0+1, 1, false, collatz::collatz2); vdn.m->store(); int carry = -1; string s2 = s; for(int i=isize(s2)-1; i>=0; i--) { carry += 2 * (s2[i] - '0'); int ncarry = 0; while(carry % 3) carry += 10, ncarry--; if(carry >= 30) carry -= 30, ncarry += 3; s2[i] = '0'+carry/3; carry = ncarry; } if(s2[0] == '0') s2 = s2.substr(1); vdn.name = s2; vdn.cp = perturb(vdn.cp); } } else if(vd.data < 2) { vd.data++; fixmatrix(vd.m->at); } } return true; } vector legend; vector named; bool rogueviz_hud() { if(!rogueviz::on) return false; if(cmode & sm::DRAW) return false; int qet = isize(edgetypes); if(qet == 1 || !edge_legend) qet = 0; int legit = qet + isize(legend); if(legit == 0) return true; initquickqueue(); int rad = vid.radius/10; ld x = vid.xres - rad; for(int i=0; icolor | 0xFF; queuepolyat(V, shTriangle, 0, PPR::MONSTER_HEAD); poly_outline = OUTLINE_DEFAULT; queuestr(int(x-rad), int(y), 0, rad*(svg::in?5:3)/4, t->name, forecolor, 0, 16); } quickqueue(); return true; } void drawExtra() { if(kind == kFullNet) { for(map::iterator it = gmatrix.begin(); it != gmatrix.end(); it++) { cell *c = it->first; c->wall = waChasm; } for(map::iterator it = gmatrix.begin(); it != gmatrix.end(); it++) { cell *c = it->first; bool draw = true; for(int i=0; isecond, shDisk, dftcolor, PPR::LINE); } for(int i=0; i 0) vdata[getid(lab)].info = new string(buf); // replace with std::shared_ptr in C++111 continue; } else { ungetc(c2, f); char buf[60]; int err = fscanf(f, "%30s", buf); if(err > 0) x = parse(buf); } if(isize(lab) && lab[0] == '*') { lab = lab.substr(1); for(int i=0; i= 0) goto again; } } } } void init() { if(on) return; autocheat = true; showstartmenu = false; #if !ISWEB mapeditor::drawplayer = false; stop_game(); firstland = specialland = laCanvas; restart_game(shmup::on ? rg::shmup : rg::nothing); #else stop_game(); firstland = specialland = laCanvas; restart_game(rg::nothing); #endif on = true; autocheat = true; items[itOrbLife] = 0; timerghost = false; gmatrix.clear(); calcparam(); drawthemap(); gmatrix0 = gmatrix; addMessage("RogueViz enabled"); } int search_for = -1; void close() { search_for = -1; for(int i=0; idead = true; vdata.clear(); labeler.clear(); legend.clear(); for(int i=0; irebase(); } void fixparam() { if(!legend.empty() && !nohud) { if((svg::in || inHighQual) && pngformat == 0) vid.xres = vid.xres * 22/16; vid.xcenter = vid.ycenter; } } #ifndef CAP_RVSLIDES #define CAP_RVSLIDES (CAP_TOUR && !ISWEB) #endif #if CAP_COMMANDLINE int readArgs() { using namespace arg; // options before reading if(0) ; else if(argis("-dftcolor")) { shift(); dftcolor = arghex(); } // tree visualizer (e.g. Tree of Life) //------------------------------------- else if(argis("-tree")) { PHASE(3); shift(); tree::read(args()); } // SAG visualizer (e.g. Reddit roguelikes, GitHub languages) //----------------------------------------------------------- // (1) configure edge weights else if(argis("-edgepower")) { shift(); sag::edgepower = argf(); shift(); sag::edgemul = argf(); } // (1) configure temperature (high, low) else if(argis("-sagtemp")) { shift(); sag::hightemp = argf(); shift(); sag::lowtemp = argf(); } else if(argis("-sagmin")) { shift(); default_edgetype.visible_from = argf(); } // (2) read the edge data else if(argis("-sagpar")) { PHASE(3); shift(); sag::sagpar = argi(); } else if(argis("-sag")) { PHASE(3); shift(); sag::read(args()); } // (3) load the initial positioning else if(argis("-gload")) { PHASE(3); shift(); sag::loadsnake(args()); } // (4) perform simulated annealing: -fullsa