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/Dna.cpp
shared/src/values/DnaManager.cpp shared/src/values/DnaManager.cpp
shared/src/values/mrand.cpp shared/src/values/mrand.cpp
shared/src/values/Similarity.cpp
shared/src/sql.cpp shared/src/sql.cpp
) )
# Add include directories # Add include directories

View File

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

View File

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

View File

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