// Voronoi used to measure the quality of the embedding (Villman's measure) // Copyright (C) 2011-2022 Tehora and Zeno Rogue, see 'hyper.cpp' for details #include "kohonen.h" namespace rogueviz { namespace voronoi { void manifold::generate_data() { T = isize(triangles); triangles_of_tile.resize(N); for(int i=0; i& cells) { map neuron_id; int N = isize(cells); for(int i=0; i > faces_seen; for(auto c: cells) { for(int i=0; itype; i++) { cellwalker cw1(c, i); cellwalker cw2 = cw1; vector tlist; do { cw2 += wstep; cw2++; auto p = at_or_null(neuron_id, cw2.at); if(!p) goto next; tlist.push_back(*p); } while(cw2 != cw1); if(1) { int minv = 0; for(int i=0; i tlist_sorted; for(int i=minv; i tlist_sorted.back()) reverse(tlist_sorted.begin()+1, tlist_sorted.end()); faces_seen.insert(tlist_sorted); } next: ; } } manifold m; m.N = N; for(const auto& v: faces_seen) for(int i=2; i > compute_voronoi_edges(manifold& m) { using kohonen::net; using kohonen::vnorm; using kohonen::vshift; using kohonen::data; using kohonen::kohvec; using kohonen::samples; vector best_tile; /* for every neuron, compute its best tile */ int N = isize(net); for(int i=0; i; set< neuron_triangle_pair > visited; queue q; vector projected(N); auto visit = [&] (neuron_triangle_pair p) { if(visited.count(p)) return; visited.insert(p); q.push(p); }; kohvec at; kohonen::alloc(at); auto project = [&] (kohvec& at, const array& tri, int i, int j) { int k = SUBD-i-j; for(auto& x: at) x = 0; vshift(at, data[tri[0]].val, i * 1. / SUBD); vshift(at, data[tri[1]].val, j * 1. / SUBD); vshift(at, data[tri[2]].val, k * 1. / SUBD); }; set already_picked; map which_best; /* project all the net[ni].net on the manifold */ for(int ni=0; ni triangles_to_visit; queue triangles_queue; auto visit1 = [&] (int tri) { if(triangles_to_visit.count(tri)) return; triangles_to_visit.insert(tri); triangles_queue.push(tri); }; for(int tr: m.triangles_of_tile[best_tile[ni]]) visit1(tr); auto& bes = which_best[ni]; while(!triangles_queue.empty()) { int tri = triangles_queue.front(); triangles_queue.pop(); for(int i=0; i<=SUBD; i++) for(int j=0; j<=SUBD-i; j++) { project(at, m.triangles[tri], i, j); ld dist = vnorm(at, net[ni].net); if(dist < best_dist && !already_picked.count(at)) { best_dist = dist, best = at, best_tri = tri; bes = lalign(0, tie(tri, i, j)); better = [&tri, i, j, &m, &visit1] () { auto flip_edge = [&] (int t1, int t2) { if(t2 < t1) swap(t1, t2); for(auto tri1: m.triangles_of_edge[{t1, t2}]) visit1(tri1); }; auto& tria = m.triangles[tri]; if(i == 0) flip_edge(tria[1], tria[2]); if(j == 0) flip_edge(tria[0], tria[2]); if(i+j == SUBD) flip_edge(tria[0], tria[1]); }; } } better(); } projected[ni] = best; already_picked.insert(best); visit({ni, best_tri}); } struct triangle_data { double dist[SUBD+1][SUBD+1]; int which[SUBD+1][SUBD+1]; triangle_data() { for(int i=0; i<=SUBD; i++) for(int j=0; j<=SUBD; j++) dist[i][j] = HUGE_VAL, which[i][j] = -1; } }; vector tdatas(m.T); while(!q.empty()) { auto ntp = q.front(); q.pop(); auto ni = ntp.first; auto& tri = m.triangles[ntp.second]; auto& td = tdatas[ntp.second]; for(int i=0; i<=SUBD; i++) for(int j=0; j<=SUBD-i; j++) { project(at, tri, i, j); ld dist = vnorm(at, projected[ni]); auto& odist = td.dist[i][j]; bool tie = abs(dist - odist) < 1e-6; if(tie ? ni < td.which[i][j] : dist < odist) { td.dist[i][j] = dist, td.which[i][j] = ni; auto flip_edge = [&] (int t1, int t2) { if(t2 < t1) swap(t1, t2); for(auto tr: m.triangles_of_edge[{t1, t2}]) { visit({ni, tr}); } }; if(i == 0) flip_edge(tri[1], tri[2]); if(j == 0) flip_edge(tri[0], tri[2]); if(i+j == SUBD) flip_edge(tri[0], tri[1]); } } } set > voronoi_edges; auto add_edge = [&] (int i, int j) { if(i>j) swap(i, j); if(i==j) return; voronoi_edges.insert({i, j}); }; for(auto& td: tdatas) { for(int i=0; i<=SUBD; i++) for(int j=0; j<=SUBD-i; j++) { if(i>0) add_edge(td.which[i][j], td.which[i-1][j]); if(j>0) add_edge(td.which[i][j], td.which[i][j-1]); if(j>0) add_edge(td.which[i][j], td.which[i+1][j-1]); } } if(1) { vector degs(N, 0); for(auto e: voronoi_edges) degs[e.first]++, degs[e.second]++; for(int v=0; v > result; for(auto ve: voronoi_edges) result.push_back(ve); return result; } } }