Show similarity

This commit is contained in:
Nikola Petrov 2025-02-06 12:11:36 +01:00
parent 3c0211b3e5
commit 4c0aefc3b1
4 changed files with 49 additions and 17 deletions

View File

@ -60,7 +60,8 @@ add_executable(view
shared/src/values/Dna.cpp
shared/src/values/DnaManager.cpp
shared/src/values/mrand.cpp
shared/src/values/Similarity.cpp
shared/src/sql.cpp
)
# Add include directories

View File

@ -1,4 +1,4 @@
#include "Dna.hpp"
#include "DnaManager.hpp"
#include <vector>
namespace Similarity

View File

@ -1,12 +1,15 @@
#include <vector>
#include <array>
#include "sql.hpp"
#include "values/DnaManager.hpp"
#include "values/Similarity.hpp"
#include "canvas/Canvas.hpp"
enum DrawingStage{
setup,
drawTree,
drawBig,
calSim,
done,
};
@ -19,7 +22,8 @@ public:
void deinit();
private:
bool showSelection = false;
bool showSelection;
bool showStats;
sqlite3 *db;
sqlite3_stmt *get_gen_num;
std::vector<int64_t> ids;
@ -27,17 +31,19 @@ private:
bool update_gen = false;
bool enableAll = true;
int64_t selected_id = -1;
int selected_gen = -1;
int32_t selected_id_index = -1;
int32_t selected_gen = -1;
DnaManagerData manager;
DrawingStage stageOfDrawing = DrawingStage::done;
Canvas canvas;
RenderTexture2D treeTexture;
int drawTreeIndex = -1;
int32_t drawTreeIndex = -1;
RenderTexture2D bigTexture;
int drawX = 0;
int drawY = 0;
void setUpManager();
std::array<float, 4> simil;
};

View File

@ -2,7 +2,6 @@
#include <cmath>
#include "Vapp.hpp"
#include "values/Dna.hpp"
#include <rlImGui.h>
#include <imgui.h>
@ -40,9 +39,9 @@ void Vapp::init()
void Vapp::update()
{
if (update_gen && selected_id >= 0)
if (update_gen && selected_id_index >= 0)
{
sql::bind_int64(get_gen_num, 1, ids[selected_id]);
sql::bind_int64(get_gen_num, 1, ids[selected_id_index]);
while (sql::step(get_gen_num) != SQL_DONE)
{
int type = sql::column_type(get_gen_num, 0);
@ -85,7 +84,7 @@ void Vapp::update()
}
else
{
stageOfDrawing = DrawingStage::done;
stageOfDrawing = DrawingStage::calSim;
}
drawX++;
@ -99,6 +98,16 @@ void Vapp::update()
drawY = 0;
}
break;
case DrawingStage::calSim:
simil[0] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
simil[1] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
simil[2] = Similarity::calc_similarity(manager.vector, Similarity::jaccard_index);
simil[3] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
stageOfDrawing = DrawingStage::done;
break;
case DrawingStage::done:
enableAll = true;
break;
@ -111,17 +120,15 @@ void Vapp::draw()
{
ClearBackground(RAYWHITE);
DrawTextureEx(bigTexture.texture, Vector2(0, 0), 0.0f, 0.25f, WHITE);
ImGui::DockSpaceOverViewport(0, NULL, ImGuiDockNodeFlags_PassthruCentralNode);
if (ImGui::BeginMainMenuBar())
{
ImGui::MenuItem("Selection", nullptr, &showSelection, enableAll);
ImGui::MenuItem("Stats", nullptr, &showStats, enableAll);
if (ImGui::MenuItem("Draw", nullptr, false, enableAll))
{
enableAll = false;
showSelection = false;
stageOfDrawing = DrawingStage::setup;
}
ImGui::EndMainMenuBar();
@ -130,14 +137,16 @@ void Vapp::draw()
if (showSelection)
{
ImGui::Begin("Selection", &showSelection);
if (!enableAll)
ImGui::BeginDisabled();
if (ImGui::BeginListBox("##list_id", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, -FLT_MIN)))
{
for (int n = 0; n < ids.size(); n++)
{
const bool is_selected = (selected_id == n);
const bool is_selected = (selected_id_index == n);
if (ImGui::Selectable(TextFormat("%d", ids[n]), is_selected))
{
selected_id = n;
selected_id_index = n;
gens = -1;
update_gen = true;
}
@ -155,8 +164,24 @@ void Vapp::draw()
}
ImGui::EndListBox();
}
if (!enableAll)
ImGui::EndDisabled();
ImGui::End();
}
if (showStats)
{
ImGui::Begin("Status", &showStats);
ImGui::LabelText("##sim1", "cosine_similarity: %f", simil[0]);
ImGui::LabelText("##sim2", "hamming_distance: %f", simil[1]);
ImGui::LabelText("##sim3", "jaccard_index: %f", simil[2]);
ImGui::LabelText("##sim4", "levenshtein_distance: %f", simil[3]);
ImGui::End();
}
ImGui::Begin("Trees", nullptr);
rlImGuiImageRenderTextureFit(&bigTexture, true);
ImGui::End();
}
void Vapp::deinit()
@ -174,7 +199,7 @@ void Vapp::setUpManager()
sqlite3_stmt *get_gen_stmt;
sql::prepare_v2(db, get_gen, -1, &get_gen_stmt, NULL);
DnaManager::cleanUp(&manager, ids[selected_id]);
DnaManager::cleanUp(&manager, ids[selected_id_index]);
int lodedGen = -1;
while (lodedGen < selected_gen)