visualizations

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

main.cpp (2739B)


      1 #include "../headers/graph.hpp"
      2 #include "../headers/prim.hpp"
      3 #include "../headers/background.hpp"
      4 #include "../headers/constants.hpp"
      5 #include "../vendor/argparse.hpp"
      6 #include <cstddef>
      7 #include <cstdint>
      8 #include <cstdlib>
      9 #include <iostream>
     10 #include <unistd.h>
     11 #include <raylib.h>
     12 #include <ctime>
     13 #include <queue>
     14 #include <unordered_set>
     15 
     16 int main(int argc, char** argv) {
     17 
     18     srand(clock());
     19     // reverse semver
     20     argparse::ArgumentParser program("abg", "10.0.0");
     21 
     22     program.add_argument("--vertices")
     23         .help("number of vertices in the graph")
     24         .default_value(DEFAULT_VERTEX_COUNT)
     25         .scan<'i', std::size_t>();
     26 
     27     program.add_argument("--sleep", "-s")
     28         .help("amount of time to sleep between traversals")
     29         .default_value(DEFAULT_SLEEP_TIME)
     30         .scan<'g', float>();
     31 
     32 
     33     // edges != total number of unique edges where uniqueness is defined by vertices
     34     // this is because we allow multiple edges between two vertices (though they aren't rendered differently)
     35     // we don't allow self-edges though. 
     36 
     37     program.add_argument("--edges", "-e")
     38         .help("number of edges in the graph")
     39         .default_value(DEFAULT_EDGE_COUNT)
     40         .scan<'i', std::size_t>();
     41 
     42     try {
     43       program.parse_args(argc, argv);
     44     }
     45     catch (const std::exception& err) {
     46       std::cerr << err.what() << std::endl;
     47       std::cerr << program;
     48       std::exit(1);
     49     }
     50 
     51     std::size_t vertexCount = program.get<std::size_t>("--vertices");
     52     std::size_t edgeCount = program.get<std::size_t>("--edges");
     53     float sleepTime = program.get<float>("--sleep");
     54 
     55     SetTraceLogLevel(LOG_ERROR);
     56 
     57     auto ss = getScreenSize();
     58 
     59     uint32_t xMax = ss[0];
     60     uint32_t yMax = ss[1];
     61 
     62     // would be nice to do this all in background.cpp, but raylib and x11 can't both be imported 
     63     // by the same file because of some dependency chain thing with Font.
     64 
     65     InitWindow(xMax, yMax, "abg");
     66     sendToBg("abg");
     67 
     68     int count = 0;
     69     while (!WindowShouldClose()) {
     70         count += 1;
     71 
     72         Graph g = Graph(edgeCount, vertexCount, xMax,yMax);
     73         std::unordered_set<std::size_t> visitedIndices {};
     74         std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>> toVisit {};
     75         std::vector<Edge> edges = g.getEdgesOfVertexIdx(0);
     76         for(auto edge: edges) {
     77             toVisit.push(edge);
     78         }
     79         g.traverseVertexIdx(0);
     80         visitedIndices.insert(0);
     81 
     82         while (!WindowShouldClose() && toVisit.size() != 0) {
     83             BeginDrawing();
     84             ClearBackground(BLACK);
     85             g.render();
     86             EndDrawing(); 
     87             usleep((int)(sleepTime * 1000000));
     88             oneStepPrim(toVisit, visitedIndices, g);
     89         }
     90     }
     91 }