visualizations

Programmatic visualizations
git clone git://git.laack.co/visualizations.git
Log | Files | Refs | README

main.cpp (2864B)


      1 #include "graph.hpp"
      2 #include <cstdlib>
      3 #include <unistd.h>
      4 #include <raylib.h>
      5 #include <ctime>
      6 #include <queue>
      7 #include <unordered_set>
      8 #include <filesystem>
      9 
     10 
     11 void CustomTakeScreenshot(char* filePath){
     12     const char *customParam;
     13     Image screenshot = LoadImageFromScreen(); 
     14     ExportImage(screenshot, TextFormat(filePath, customParam));
     15     UnloadImage(screenshot); 
     16 }
     17 void explore(
     18         std::size_t cIdx, 
     19         std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>>& toVisit,
     20         Edge& current,
     21         Graph& g
     22     ) {
     23     g.traverseVertexIdx(cIdx);
     24     std::vector<Edge> edges = g.getEdgesOfVertexIdx(cIdx);
     25     for(auto edge: edges) {
     26         toVisit.push(edge);
     27     }
     28     g.setEdgeTraversed(current);
     29 }
     30 
     31 int main() {
     32 
     33     std::filesystem::create_directory("/dev/shm/bg");
     34     srand(clock());
     35 
     36     std::size_t edgeCount = 30;
     37     std::size_t vertCount = 10;
     38 
     39     float xMax = 5120;
     40     float yMax = 1440;
     41 
     42     SetConfigFlags(FLAG_WINDOW_HIDDEN);
     43     InitWindow(xMax, yMax, "Raylib animation window");
     44 
     45     while (!WindowShouldClose()) {
     46 
     47         Graph g = Graph(edgeCount, vertCount, xMax,yMax);
     48         std::unordered_set<std::size_t> visitedIndices {};
     49         std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> toVisit {};
     50         std::vector<Edge> edges = g.getEdgesOfVertexIdx(0);
     51 
     52         for(auto edge: edges) {
     53             toVisit.push(edge);
     54         }
     55 
     56         g.traverseVertexIdx(0);
     57         visitedIndices.insert(0);
     58 
     59         while (!WindowShouldClose()) {
     60             sleep(5);
     61 
     62             BeginDrawing();
     63             ClearBackground(BLACK);
     64             g.render();
     65             EndDrawing(); 
     66             
     67             char path[] = "/dev/shm/bg/out.png";
     68             CustomTakeScreenshot(path);
     69             // TODO: Can this be done away with? It's not *that* slow...
     70             system("/usr/bin/feh --no-fehbg --bg-tile /dev/shm/bg/out.png");
     71 
     72 
     73             bool found = false;
     74             if(toVisit.size() == 0) {
     75                 break;
     76             }
     77             while(found == false) {
     78                 if(toVisit.size() == 0) {
     79                     break;
     80                 }
     81 
     82                 found = true;
     83                 auto current = toVisit.top();
     84                 toVisit.pop();
     85 
     86                 if(visitedIndices.find(current.v2Index) == visitedIndices.end()) {
     87                     auto cIdx = current.v2Index;
     88                     visitedIndices.insert(current.v2Index);
     89                     explore(cIdx, toVisit, current, g);
     90 
     91                 } else if(visitedIndices.find(current.v1Index) == visitedIndices.end()) {
     92                     auto cIdx = current.v1Index;
     93                     visitedIndices.insert(current.v1Index);
     94                     explore(cIdx, toVisit, current, g);
     95                 } else {
     96                     found = false;
     97                 }
     98 
     99             }
    100 
    101         }
    102 
    103     }
    104 
    105 }