Add similarity and experimental drawBranch

This commit is contained in:
2026-02-03 15:37:49 +01:00
parent 0dedb2d6b9
commit d7ea0b99ac
9 changed files with 311 additions and 98 deletions

View File

@@ -32,8 +32,9 @@ private:
Vector2 start = {0};
std::list<DrawArgs> drawCalls;
void drawBranch();
void calculateBranch();
void drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness);
inline size_t getNumOfBranches(int dep);
inline Color getStartColor(DrawArgs &arg);
inline Color getEndColor(int dep, Color &start);

View File

@@ -3,14 +3,14 @@
namespace Similarity
{
// float euclidean_distance(Dna *d1, Dna *d2); direct distance betwen vector. wont give 0 and 1
// float dot_product(Dna *d1, Dna *d2); doent return betwen 0 to 1
// float cosine_similarity(Dna *d1, Dna *d2);
// float cosine_similarity_int(Dna *d1, Dna *d2);
float euclidean_distance(Dna *d1, Dna *d2);// direct distance betwen vector. wont give 0 and 1
float dot_minmax(Dna *d1, Dna *d2); // doent return betwen 0 to 1
float cosine_similarity(Dna *d1, Dna *d2);
float cosine_similarity_int(Dna *d1, Dna *d2);
float hamming_distance(Dna *d1, Dna *d2);
float hamming_distance_without_seeds(Dna *d1, Dna *d2);
// float jaccard_index(Dna *d1, Dna *d2); // primerja unio genov naprimer gleda ce je gen za nebo isti z genom za barvo za liste, to nerabimo
// float levenshtein_distance(Dna *d1, Dna *d2); // odstranjen ker mi vrne iste podatke kot hamming distance ki je bolj enostaven za izracun
float levenshtein_distance(Dna *d1, Dna *d2); // odstranjen ker mi vrne iste podatke kot hamming distance ki je bolj enostaven za izracun
// float needleman_wunsch(Dna *d1, Dna *d2); used for bioinformatics and aligment. Dont need its aligned alredy
typedef float(simil_func)(Dna *d1, Dna *d2);

View File

@@ -1,4 +1,5 @@
#include <cmath>
#include <algorithm>
#include "canvas/BackGround.hpp"
#include "canvas/BackGroundColors.hpp"

View File

@@ -4,6 +4,7 @@
#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
#define ITER_PER_FRAME 5000
@@ -40,7 +41,7 @@ void Tree::init(int size)
start.x = size / 2;
start.y = size;
calculateLevels(size);
//texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
// texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
}
void Tree::draw(Dna *dna)
@@ -56,7 +57,7 @@ bool Tree::tick()
size_t i = 0;
while (!drawCalls.empty())
{
drawBranch();
calculateBranch();
drawCalls.pop_front();
i++;
if (i >= ITER_PER_FRAME)
@@ -68,7 +69,7 @@ bool Tree::tick()
// Private
void Tree::drawBranch()
void Tree::calculateBranch()
{
DrawArgs arg = drawCalls.front();
if (arg.dep == MAX_DEPTH)
@@ -86,17 +87,14 @@ void Tree::drawBranch()
Color colorStart = getStartColor(arg);
Color colorEnd = getEndColor(arg.dep, colorStart);
// drawBranch(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorStart, colorEnd, i);
int size = Lerp(sizeStart, sizeEnd, i);
DrawCircleV(point, size, color);
//DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
// use
// DrawRectangleGradientEx
// DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
}
// add more branches to draw
@@ -200,3 +198,51 @@ inline float Tree::getAngleVar(DrawArgs &arg)
return angleVar;
}
void Tree::drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness)
{
// Calculate the direction vector from startPoint to endPoint
Vector2 direction = {endPoint.x - startPoint.x, endPoint.y - startPoint.y};
// Normalize the direction vector
float length = sqrtf(direction.x * direction.x + direction.y * direction.y);
if (length == 0)
length = 1; // Avoid division by zero
Vector2 normalizedDir = {direction.x / length, direction.y / length};
// Calculate the perpendicular vector (rotate 90 degrees)
Vector2 perpendicular = {-normalizedDir.y, normalizedDir.x};
// Calculate the four vertices of the quadrilateral
Vector2 topLeft = {
startPoint.x + perpendicular.x * startThickness,
startPoint.y + perpendicular.y * startThickness};
Vector2 topRight = {
endPoint.x + perpendicular.x * endThickness,
endPoint.y + perpendicular.y * endThickness};
Vector2 bottomLeft = {
startPoint.x - perpendicular.x * startThickness,
startPoint.y - perpendicular.y * startThickness};
Vector2 bottomRight = {
endPoint.x - perpendicular.x * endThickness,
endPoint.y - perpendicular.y * endThickness};
// Draw the two triangles to form the quadrilateral
rlBegin(RL_TRIANGLES);
// First triangle
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(topLeft.x, topLeft.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(topRight.x, topRight.y);
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(bottomLeft.x, bottomLeft.y);
// Second triangle
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(bottomLeft.x, bottomLeft.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(topRight.x, topRight.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(bottomRight.x, bottomRight.y);
rlEnd();
}

View File

@@ -1,56 +1,88 @@
#include "values/Similarity.hpp"
#include <cmath>
#include <algorithm>
#include <numeric>
#include <raylib.h>
#include <chrono>
namespace Similarity
{
float dot_minmax(Dna *d1, Dna *d2)
{
uint64_t max = sizeof(Dna) * 255 * 255;
uint8_t *a = (uint8_t *)d1;
uint8_t *b = (uint8_t *)d2;
uint32_t result = 0;
for (size_t i = 0; i < sizeof(Dna); ++i)
{
result += static_cast<uint32_t>(a[i]) * static_cast<uint32_t>(b[i]);
}
return result / (double)max;
}
float euclidean_distance(Dna *d1, Dna *d2)
{
uint8_t *a = (uint8_t *)d1;
uint8_t *b = (uint8_t *)d2;
float sum = 0.0f;
for (size_t i = 0; i < sizeof(Dna); ++i)
{
float diff = static_cast<float>(a[i]) - static_cast<float>(b[i]);
sum += diff * diff;
}
float distance = std::sqrt(sum);
float max_distance = 255.0f * std::sqrt(static_cast<float>(sizeof(Dna)));
return 1 - (distance / max_distance);
}
// todo: use int8_t insted of uint8_t and map data
// 0 -> -128
// 255 -> 127
// int8_t = uint8_t - 128
// float cosine_similarity(Dna *d1, Dna *d2)
// {
// uint8_t *d1a = (uint8_t *)d1;
// uint8_t *d2a = (uint8_t *)d2;
float cosine_similarity(Dna *d1, Dna *d2)
{
uint8_t *d1a = (uint8_t *)d1;
uint8_t *d2a = (uint8_t *)d2;
// float mag1 = 0.0f;
// float mag2 = 0.0f;
// float dot_prod = 0.0f;
// for (size_t i = 0; i < sizeof(Dna); i++)
// {
// dot_prod += d1a[i] * d2a[i];
// mag1 += d1a[i] * d1a[i];
// mag2 += d2a[i] * d2a[i];
// }
// mag1 = sqrt(mag1);
// mag2 = sqrt(mag2);
float mag1 = 0.0f;
float mag2 = 0.0f;
float dot_prod = 0.0f;
for (size_t i = 0; i < sizeof(Dna); i++)
{
dot_prod += d1a[i] * d2a[i];
mag1 += d1a[i] * d1a[i];
mag2 += d2a[i] * d2a[i];
}
mag1 = sqrt(mag1);
mag2 = sqrt(mag2);
// return dot_prod / (mag1 * mag2);
// }
return dot_prod / (mag1 * mag2);
}
// float cosine_similarity_int(Dna *d1, Dna *d2)
// {
// auto map = [](uint8_t a) -> int8_t
// { return a - 128; };
// uint8_t *d1a = (uint8_t *)d1;
// uint8_t *d2a = (uint8_t *)d2;
// float mag1 = 0.0f;
// float mag2 = 0.0f;
// float dot_prod = 0.0f;
// for (size_t i = 0; i < sizeof(Dna); i++)
// {
// int8_t a = map(d1a[i]);
// int8_t b = map(d2a[i]);
// dot_prod += a * b;
// mag1 += a * a;
// mag2 += b * b;
// }
// mag1 = sqrt(mag1);
// mag2 = sqrt(mag2);
// return dot_prod / (mag1 * mag2);
// }
float cosine_similarity_int(Dna *d1, Dna *d2)
{
auto map = [](uint8_t a) -> int8_t
{ return a - 128; };
uint8_t *d1a = (uint8_t *)d1;
uint8_t *d2a = (uint8_t *)d2;
float mag1 = 0.0f;
float mag2 = 0.0f;
float dot_prod = 0.0f;
for (size_t i = 0; i < sizeof(Dna); i++)
{
int8_t a = map(d1a[i]);
int8_t b = map(d2a[i]);
dot_prod += a * b;
mag1 += a * a;
mag2 += b * b;
}
mag1 = sqrt(mag1);
mag2 = sqrt(mag2);
return dot_prod / (mag1 * mag2);
}
float hamming_distance(Dna *d1, Dna *d2)
{
@@ -84,8 +116,41 @@ namespace Similarity
return 1 - (distance / (end - start));
}
const char *nameofFunc(simil_func f)
{
if (f == &Similarity::euclidean_distance)
{
return "eucl";
}
else if (f == &Similarity::dot_minmax)
{
return "dot";
}
else if (f == &Similarity::cosine_similarity)
{
return "cos";
}
else if (f == &Similarity::cosine_similarity_int)
{
return "cos_i";
}
else if (f == &Similarity::hamming_distance)
{
return "hamming";
}
else if (f == &Similarity::levenshtein_distance)
{
return "leven";
}
else
{
return "unknown";
}
}
float calc_similarity(std::vector<Dna> &vec, simil_func f)
{
auto start = std::chrono::high_resolution_clock::now();
size_t num_pairs = (vec.size() * (vec.size() - 1)) / 2;
float total_similarity = 0.0;
@@ -97,6 +162,48 @@ namespace Similarity
}
}
float average_similarity = total_similarity / num_pairs;
auto stop = std::chrono::high_resolution_clock::now();
const auto int_ms = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
TraceLog(LOG_INFO, "%s, %d", nameofFunc(f), int_ms);
return average_similarity * 100.0f;
}
float levenshtein_distance(Dna *d1, Dna *d2)
{
size_t len = sizeof(Dna);
uint8_t *a = (uint8_t *)d1;
uint8_t *b = (uint8_t *)d2;
// Create a distance matrix
static std::vector<std::vector<uint32_t>> dp(len + 1, std::vector<uint32_t>(len + 1, 0));
// Initialize the first row and column
for (size_t i = 0; i <= len; ++i)
{
dp[i][0] = i;
}
for (size_t j = 0; j <= len; ++j)
{
dp[0][j] = j;
}
// Fill the distance matrix
for (size_t i = 1; i <= len; ++i)
{
for (size_t j = 1; j <= len; ++j)
{
uint32_t cost = (a[i - 1] == b[j - 1]) ? 0 : 1;
dp[i][j] = std::min({
dp[i - 1][j] + 1, // deletion
dp[i][j - 1] + 1, // insertion
dp[i - 1][j - 1] + cost // substitution
});
}
}
return 1 - (dp[len][len] / float(len + len));
}
}