Compare commits

..

18 Commits

Author SHA1 Message Date
29230f4d07 Timing 2026-02-16 19:35:41 +01:00
6ff4a66551 Fix small mistake 2026-02-04 14:23:27 +01:00
45f73c721e Add savaToFile option 2026-02-04 13:18:54 +01:00
e99bd33985 update slike.cpp 2026-02-04 12:38:14 +01:00
40b2f963a6 save old data 2026-02-03 15:39:19 +01:00
cc00586bc9 add random/slike 2026-02-03 15:39:01 +01:00
d7ea0b99ac Add similarity and experimental drawBranch 2026-02-03 15:37:49 +01:00
0dedb2d6b9 add old build script 2025-09-02 16:33:46 +02:00
d41f11a333 comments and new random 2025-08-27 17:56:25 +02:00
53c333d46d Formationg and comments 2025-08-08 17:38:26 +02:00
30980423eb add acept db file name 2025-06-18 18:32:15 +02:00
31e8f302ac Add check if connected and remove block list and removing of old data. 2025-06-18 17:47:40 +02:00
1516ad63cb android fix 2025-04-21 11:44:13 +02:00
4fa74d0cfb add block id 2025-04-05 23:20:35 +02:00
64364bfefd Add host name 2025-03-27 14:32:56 +01:00
2cd8ef5558 Remove shaders for drawin sun/moon 2025-03-10 00:52:34 +01:00
266d250881 Update similarity calc 2025-03-09 15:14:21 +01:00
1994e7ccb1 Update shader for android 2025-02-15 22:34:24 +01:00
36 changed files with 1367 additions and 594 deletions

View File

@@ -8,7 +8,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
# set(CMAKE_VERBOSE_MAKEFILE ON)
add_compile_options(-ggdb)
include_directories(external/include/)
include_directories(
external/include
shared/inc
)
link_libraries(
${CMAKE_SOURCE_DIR}/external/libimgui.a
${CMAKE_SOURCE_DIR}/external/libraylib.a
@@ -18,52 +22,46 @@ link_libraries(
m
)
add_library(shared STATIC
shared/src/canvas/BackGround.cpp
shared/src/canvas/BackGroundColors.cpp
shared/src/canvas/Canvas.cpp
shared/src/canvas/Tree.cpp
shared/src/values/Dna.cpp
shared/src/values/DnaManager.cpp
shared/src/values/mrand.cpp
shared/src/values/Similarity.cpp
shared/src/TcpSocket.cpp
shared/src/sql.cpp
shared/src/timing.cpp
)
add_executable(app
app/src/main.cpp
app/src/App.cpp
app/src/DnaStore.cpp
app/src/sys.cpp
shared/src/canvas/BackGround.cpp
shared/src/canvas/BackGroundColors.cpp
shared/src/canvas/Canvas.cpp
shared/src/canvas/Circle.cpp
shared/src/canvas/Tree.cpp
shared/src/values/Dna.cpp
shared/src/values/DnaManager.cpp
shared/src/values/mrand.cpp
shared/src/values/Similarity.cpp
shared/src/TcpSocket.cpp
)
# Add include directories
target_include_directories(app PRIVATE app/inc shared/inc )
target_include_directories(app PRIVATE app/inc)
target_link_libraries(app PRIVATE shared)
add_executable(server
server/src/server.cpp
server/src/checker.cpp
# add_executable(server
# server/src/server.cpp
# server/src/checker.cpp
# )
# target_include_directories(server PRIVATE server/inc)
# target_link_libraries(server PRIVATE shared)
shared/src/sql.cpp
shared/src/TcpSocket.cpp
shared/src/values/Dna.cpp
shared/src/values/DnaManager.cpp
shared/src/values/mrand.cpp
)
# Add include directories
target_include_directories(server PRIVATE server/inc shared/inc)
# add_executable(view
# view/src/main.cpp
# view/src/Vapp.cpp
# )
# target_include_directories(view PRIVATE view/inc)
# target_link_libraries(view PRIVATE shared)
add_executable(view
view/src/main.cpp
view/src/Vapp.cpp
shared/src/canvas/BackGround.cpp
shared/src/canvas/BackGroundColors.cpp
shared/src/canvas/Canvas.cpp
shared/src/canvas/Circle.cpp
shared/src/canvas/Tree.cpp
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
target_include_directories(view PRIVATE view/inc shared/inc)
# add_executable(slike
# random/slike.cpp
# )
# target_link_libraries(slike PRIVATE shared)
file(COPY CascadiaCode.ttf DESTINATION "res")

BIN
CascadiaCode.ttf Normal file

Binary file not shown.

View File

@@ -1,10 +1,3 @@
# how to build
```
g++ build.cpp -o build
./build
or
./build run
```
# Code style
@@ -30,11 +23,4 @@ or
## Include order
- Std
- local ( form inc dir )
- external raylib
## Generate shader .h files
```
xxd -i shaders/sun_100.fs > inc/sunShader.hpp
xxd -i shaders/sun_330.fs >> inc/sunShader.hpp
```
- external raylib

View File

@@ -18,28 +18,14 @@ private:
int screenWidth, screenHeight;
Canvas canvas;
int pos = 0;
std::array<RenderTexture2D, 2> canvasTexure = {0};
RenderTexture2D canvasTexure = {0};
float rotation = 0.0f;
Vector2 mouseStart;
bool validHit = false;
float len;
float ofset;
Liked topLiked = Liked::tbd;
Rectangle destA;
Rectangle destB;
Rectangle likeBox;
Rectangle disLikeBox;
std::array<UiUnit, 2> unit = {0};
UiUnit unit = {0};
DnaManagerData manager;
Rectangle likedTextBox;
Rectangle genTextBox;
Rectangle simTextBox;
float simil = 100.0f;
};

View File

@@ -7,4 +7,5 @@ namespace DnaStore
void saveVec(DnaManagerData *data);
void saveGen(DnaManagerData *data);
void sync();
bool ping();
} // namespace DnaStore

View File

@@ -2,237 +2,118 @@
#include <cmath>
#include "App.hpp"
#include "timing.hpp"
#include <raylib.h>
#include <raymath.h>
#define TOP (1 - pos)
#define BOTTOM pos
Font fontTtf;
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom, bool leftOrRigth)
void DrawTextB(const char *text, float posX, float posY, int fontSize, Color color)
{
float br = box.width / box.height;
float tr = textW / textH;
Rectangle ret = {0, 0, 0, 0};
float hm = box.height * margin;
float wm = box.width * margin;
ret.height = box.height - hm;
ret.width = box.width - wm;
if (br < tr)
{
// bolj kvadrat izracunaj visino iz sirine
ret.height = ret.width / tr;
}
else
{
// bolj podolgovat izracunaj sirino iz visine
ret.width = ret.height * tr;
}
if (topOrBottom)
{
ret.y = box.y + hm;
}
else
{
ret.y = box.y + box.height - hm - ret.height;
}
if (leftOrRigth)
{
ret.x = box.x + wm;
}
else
{
ret.x = box.x + box.width - wm - ret.width;
}
return ret;
DrawTextEx(fontTtf, text, (Vector2){ posX, posY }, fontSize, 1, color);
}
// Dimentions for font size 20
// DISLIKE 83
// LIKE 46
// GEN 9999: 999/999 -> 200
constexpr float textMargin = 0.02f;
void App::init(int screenWidth, int screenHeight)
{
fontTtf = LoadFontEx("res/CascadiaCode.ttf", 32, 0, 0);
SetTextLineSpacing(16);
this->screenWidth = screenWidth;
this->screenHeight = screenHeight;
this->canvas.init(screenWidth);
// int s = MeasureText("GEN 9999: 999/999", 20);
// TraceLog(LOG_INFO, "%d", s);
for (size_t i = 0; i < canvasTexure.size(); i++)
{
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
}
canvasTexure = LoadRenderTexture(screenWidth, screenWidth);
DnaStore::load(&manager);
simil = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
upTex(Liked::tbd);
while (!canvas.tick(canvasTexure[TOP]))
while (!canvas.tick(canvasTexure))
{
// wait to finish drawing
}
pos = 1 - pos;
upTex(Liked::tbd);
while (!canvas.tick(canvasTexure[TOP]))
{
// wait to finish drawing
}
pos = 1 - pos;
float posY = (screenHeight - screenWidth) / 2.0f;
likedTextBox = TextInSpace({0,
0,
(float)screenWidth / 2.0f,
posY},
20.0f, 83.0f, textMargin, false, true);
genTextBox = TextInSpace({0,
posY + screenWidth,
(float)screenWidth,
posY},
20.0f, 200.0f, textMargin, true, true);
simTextBox = TextInSpace({(float)screenWidth / 2.0f,
likedTextBox.y,
(float)screenWidth / 2.0f,
likedTextBox.height},
20.0f, 60.0f, textMargin, false, false);
destB = {0, posY, (float)screenWidth, (float)screenWidth};
destA = destB;
float recPosX = screenWidth * 0.3f;
disLikeBox = {0, posY, (float)recPosX, (float)screenWidth};
likeBox = {screenWidth - recPosX, posY, (float)recPosX, (float)screenWidth};
destA = {0, posY, (float)screenWidth, (float)screenWidth};
}
void App::upTex(Liked liked)
{
if (liked != Liked::tbd)
{
unit[TOP].liked = liked;
bool ng = DnaManager::like(unit[TOP], &manager);
unit.liked = liked;
bool ng = DnaManager::like(unit, &manager);
if (ng)
{
DnaStore::saveGen(&manager);
DnaManager::newGen(&manager);
DnaStore::saveVec(&manager);
simil = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
}
DnaStore::saveData(&manager);
}
unit[TOP] = DnaManager::next(&manager);
if (unit[TOP].dna != nullptr)
unit = DnaManager::next(&manager);
if (unit.dna != nullptr)
{
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
canvas.newGen(canvasTexure, unit.dna);
return;
}
BeginTextureMode(canvasTexure[TOP]);
BeginTextureMode(canvasTexure);
ClearBackground(BLACK);
DrawText("NEXT GEN", 10, 10, 20, WHITE);
DrawText("NEXT GEN", 10, 10, screenWidth/10, WHITE);
EndTextureMode();
}
void App::update()
{
bool isDone = canvas.tick(canvasTexure[BOTTOM]);
Vector2 mousePosition = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
mouseStart = mousePosition;
validHit = CheckCollisionPointRec(mouseStart, destA);
len = Vector2Distance(mouseStart, {destB.x, destB.y});
ofset = std::atan2(destB.x - mouseStart.x, destB.y - mouseStart.y);
}
void App::update(){}
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && validHit)
{
float dist = mousePosition.x - mouseStart.x;
float l = dist / screenWidth;
rotation = Lerp(45.0f, -45.0f, (l + 1) / 2);
enum class Rend_type{
every_frame,
in_one_frame,
over_frames,
};
float angle = ((rotation)*PI) / 180.0f;
angle += ofset;
Vector2 newCenter = {.x = len * std::sin(angle), .y = len * std::cos(angle)};
destA.x = newCenter.x + mousePosition.x;
destA.y = newCenter.y + mousePosition.y;
if (CheckCollisionPointRec(mousePosition, disLikeBox))
{
topLiked = Liked::no;
}
else if (CheckCollisionPointRec(mousePosition, likeBox))
{
topLiked = Liked::yes;
}
else
{
topLiked = Liked::tbd;
}
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && validHit)
{
if (isDone)
{
if (topLiked != Liked::tbd)
{
upTex(topLiked);
pos = 1 - pos; // switch bottom and top
}
}
rotation = 0.0f;
destA = destB;
topLiked = Liked::tbd;
}
}
constexpr Rend_type rend_type = Rend_type::over_frames;
void App::draw()
{
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
TraceLog(LOG_INFO, "CLICK");
upTex(Liked::yes);
if constexpr (rend_type == Rend_type::in_one_frame){
while (!canvas.tick(canvasTexure))
{
// wait to finish drawing
}
}
}
if constexpr (rend_type == Rend_type::over_frames){
canvas.tick(canvasTexure);
}
ClearBackground(BLUE);
int fps = GetFPS();
const char* fpstext = TextFormat("%04d FPS", fps);
DrawTextB(fpstext, 10, 10, 36, BLACK);
const char* ctext = TextFormat("%04ld Circles", canvas.tree.circles);
DrawTextB(ctext, 10, 50, 36, BLACK);
Rectangle source = {0, 0, (float)screenWidth, (float)-screenWidth};
Vector2 origin = {0.0f, 0.0f};
DrawTexturePro(canvasTexure[BOTTOM].texture, source, destB, origin, 0.0f, WHITE);
DrawTexturePro(canvasTexure[TOP].texture, source, destA, origin, 360 - rotation, WHITE);
const char *textGen = TextFormat("GEN %d: %d / %d", manager.generation, unit[TOP].index + 1, NUM_PER_GEN);
DrawText(textGen, genTextBox.x, genTextBox.y, genTextBox.height, BLACK);
const char *textProc = TextFormat("%.1f%%", simil);
DrawText(textProc, simTextBox.x, simTextBox.y, simTextBox.height, BLACK);
switch (topLiked)
{
case Liked::yes:
DrawText("LIKED", likedTextBox.x, likedTextBox.y, likedTextBox.height, BLACK);
break;
case Liked::no:
DrawText("DISLIKE", likedTextBox.x, likedTextBox.y, likedTextBox.height, BLACK);
break;
default:
break;
if constexpr (rend_type == Rend_type::every_frame){
canvas.newGen(canvasTexure, unit.dna);
while (!canvas.tick(canvasTexure))
{
// wait to finish drawing
}
}
DrawTexturePro(canvasTexure.texture, source, destA, origin, 360, WHITE);
}
void App::deinit()
{
for (size_t i = 0; i < canvasTexure.size(); i++)
{
UnloadRenderTexture(canvasTexure[i]);
}
UnloadRenderTexture(canvasTexure);
UnloadFont(fontTtf);
canvas.deinit();
}

View File

@@ -16,6 +16,7 @@
#define DATA_FILE_NAME "DATA.bin"
#define VECTOR_FILE_NAME "VECTOR.bin"
#define GEN_FILE_PATTRN "gen/%04d.bin"
#define HOST_NAME "localhost"
void DnaStore::load(DnaManagerData *data)
{
@@ -149,24 +150,38 @@ void DnaStore::saveGen(DnaManagerData *data)
sync();
}
void client(std::string prefix);
void client(std::string_view prefix);
void DnaStore::sync()
{
const char *prefix = sys::transformFilePath("");
std::string prefixs = prefix;
std::string_view prefixs = prefix;
std::thread t(client, prefixs);
t.detach();
}
void client(std::string prefix)
bool DnaStore::ping(){
int sock = TcpSocket::connectt(HOST_NAME, keyPort);
if (sock < 0)
{
return false;
}
int64_t header = 0;
TcpSocket::recvt(sock, &header, sizeof(header));
TcpSocket::closet(sock);
return header == StartHeader;
}
void client(std::string_view prefix)
{
constexpr int extra_buff = 22; // len of 2**31 -> 2147483648 plus the pattern size
std::string buffer;
buffer.resize(prefix.size() + extra_buff);
int sock = TcpSocket::connectt("petrovv.com", keyPort);
int sock = TcpSocket::connectt(HOST_NAME, keyPort);
if (sock < 0)
{
return;
@@ -177,7 +192,7 @@ void client(std::string prefix)
TcpSocket::closet(sock);
sock = TcpSocket::connectt("petrovv.com", serverPort);
sock = TcpSocket::connectt(HOST_NAME, serverPort);
if (sock < 0)
{
@@ -185,7 +200,7 @@ void client(std::string prefix)
}
#if defined(PLATFORM_ANDROID)
int ret = sprintf(buffer.data(), "%s/" ID_FILE_NAME, prefix.c_str());
int ret = sprintf(buffer.data(), "%s/" ID_FILE_NAME, prefix.data());
#else
int ret = sprintf(buffer.data(), ID_FILE_NAME);
#endif
@@ -220,7 +235,7 @@ void client(std::string prefix)
needed_gen = message.data;
#if defined(PLATFORM_ANDROID)
int ret = sprintf(buffer.data(), "%s/" GEN_FILE_PATTRN, prefix.c_str(), needed_gen);
int ret = sprintf(buffer.data(), "%s/" GEN_FILE_PATTRN, prefix.data(), needed_gen);
#else
int ret = sprintf(buffer.data(), GEN_FILE_PATTRN, needed_gen);
#endif

View File

@@ -1,5 +1,6 @@
#include <raylib.h>
#include "App.hpp"
#include "timing.hpp"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
@@ -7,12 +8,14 @@
static App app;
Timer t;
void UpdateDrawFrame()
{
app.update();
t.reset_and_start();
BeginDrawing();
app.draw();
EndDrawing();
t.stop_and_print("to draw frame");
}
int main(void)
@@ -34,7 +37,7 @@ int main(void)
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(60);
// SetTargetFPS(60);
while (!WindowShouldClose())
{
UpdateDrawFrame();

BIN
data.db Normal file

Binary file not shown.

View File

@@ -0,0 +1,77 @@
#define EXT_LINK
#define EXT_INC
#include "build.hpp"
#include <unordered_set>
std::vector<std::string> LINK = {"raylib/lib/libraylib.a"};
std::vector<std::string> RAYINCLUDE = {"-Iraylib/include"};
std::filesystem::path RAYLIB_DIR = "raylib";
bool build = false;
bool clear = false;
bool run = false;
int main(int argc, char const *argv[])
{
if (rebuild_my_self(__FILE__, argc, argv))
return 0;
for (int i = 1; i < argc; ++i)
{
if (!std::strcmp(argv[i], "opt"))
opt_flags = "-O3";
if (!std::strcmp(argv[i], "build"))
build = true;
if (!std::strcmp(argv[i], "run"))
run = true;
if (!std::strcmp(argv[i], "clear"))
{
clear_all_build();
clear = true;
}
}
if (clear && !build)
{
return 0;
}
if (!std::filesystem::is_directory(RAYLIB_DIR))
{
command = {"wget", "https://github.com/raysan5/raylib/releases/download/5.5/raylib-5.5_linux_amd64.tar.gz"};
run_command(command);
command = {"tar", "-xzvf", "raylib-5.5_linux_amd64.tar.gz"};
run_command(command);
std::filesystem::rename("raylib-5.5_linux_amd64", "raylib");
std::filesystem::remove_all("raylib-5.5_linux_amd64.tar.gz");
}
if (!std::filesystem::is_directory(RAYLIB_DIR))
return 0;
std::filesystem::create_directory(OBJ_DIR);
int res = compile_src_dir(RAYINCLUDE);
switch (res)
{
case 1:
compile_obj_dir(LINK);
break;
case -1:
return 0;
}
if (!std::filesystem::exists(BUILD_FILE))
compile_obj_dir(LINK);
if (run)
{
run_main();
}
return 0;
}

View File

@@ -0,0 +1,408 @@
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <list>
#include <filesystem>
#include <fstream>
#include <unordered_set>
// #define EXT_ARGS
// #define EXT_LINK
// #define EXT_INC
// #define EXT_OBJ
// #define Werror
// #define Wextra
std::filesystem::path OBJ_DIR = "obj";
std::filesystem::path INC_DIR = "inc";
std::filesystem::path SRC_DIR = "src";
#ifdef _WIN32
std::filesystem::path BUILD_FILE = "main.exe";
std::string cpp_compiler = "zig c++";
std::string c_compiler = "zig cc";
#else
std::filesystem::path BUILD_FILE = "main";
std::string cpp_compiler = "g++";
std::string c_compiler = "gcc";
#endif
std::string opt_flags = "-ggdb";
std::vector<std::string> command;
enum class Color
{
black,
red,
green,
yellow,
blue,
purple,
cyan,
white,
reset,
};
void pick_color(Color color)
{
switch (color)
{
case Color::black:
printf("\033[0;30m");
break;
case Color::red:
printf("\033[0;31m");
break;
case Color::green:
printf("\033[0;32m");
break;
case Color::yellow:
printf("\033[0;33m");
break;
case Color::blue:
printf("\033[0;34m");
break;
case Color::purple:
printf("\033[0;35m");
break;
case Color::cyan:
printf("\033[0;36m");
break;
case Color::white:
printf("\033[0;37m");
break;
case Color::reset:
printf("\033[0m");
break;
default:
break;
}
}
void print_command(std::vector<std::string> &arguments)
{
pick_color(Color::cyan);
for (auto &&i : arguments)
{
printf("%s ", i.c_str());
}
pick_color(Color::white);
printf("\n");
}
bool run_command(std::vector<std::string> &arguments)
{
print_command(arguments);
std::string command = "";
for (auto &&i : arguments)
{
command += " " + i;
}
return std::system(command.c_str()) == 0;
}
bool check_if_rebuild(const std::filesystem::path &org_path, const std::filesystem::path &new_path)
{
if (!std::filesystem::exists(org_path))
return false;
if (!std::filesystem::exists(new_path))
return true;
auto file_time_one = std::filesystem::last_write_time(org_path);
auto file_time_two = std::filesystem::last_write_time(new_path);
return file_time_one > file_time_two;
}
bool try_remove(std::filesystem::path path)
{
try
{
std::filesystem::remove(path);
return true;
}
catch (const std::exception &e)
{
}
return false;
}
bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec_path)
{
std::filesystem::path exec = exec_path[0];
try_remove("old_build");
if (!check_if_rebuild(src_path, exec) && !check_if_rebuild(__FILE__, exec))
return false;
std::filesystem::rename(exec, "old_build");
std::vector<std::string> input;
for (int i = 0; i < argc; ++i)
input.push_back(exec_path[i]);
std::vector<std::string> comand = {cpp_compiler, src_path.string(), "-o", exec_path[0], "-ggdb"};
if (!run_command(comand))
return false;
printf("rebuild\n");
if (!run_command(input))
return false;
return true;
}
struct all_in_directory
{
std::vector<std::filesystem::path> files;
std::vector<std::filesystem::path> dirs;
};
all_in_directory get_all_files_in_dir(std::filesystem::path directory_path)
{
namespace fs = std::filesystem;
all_in_directory dir;
if (!fs::is_directory(directory_path))
return dir;
std::list<fs::path> dirs;
dirs.push_back(directory_path);
dir.dirs.push_back(directory_path);
while (!dirs.empty())
{
for (const auto &entry : fs::directory_iterator(dirs.front()))
{
// Check if the entry is a regular file
if (fs::is_regular_file(entry))
{
dir.files.push_back(entry.path());
}
// Check if the entry is a directory
else if (fs::is_directory(entry))
{
dirs.push_back(entry.path());
dir.dirs.push_back(entry.path());
}
}
dirs.pop_front();
}
return dir;
}
std::vector<std::string> get_includes(const std::filesystem::path &path)
{
std::vector<std::string> ret;
std::ifstream ifs(path);
std::string line;
while (std::getline(ifs, line))
{
if (line.empty())
continue;
if (line[0] != '#')
break;
// if include ends with " its my .h
if (line[line.size() - 1] == '\"')
{
// magic num 10 is lenOf(#include ") so start at 10 until end of
// line witch is len of line - 10 - 1 of cahar "
ret.push_back(line.substr(10, line.length() - 10 - 1));
}
}
return ret;
}
void clear_all_build()
{
std::filesystem::remove_all(OBJ_DIR);
std::filesystem::remove_all(BUILD_FILE);
}
#ifdef EXT_INC
int compile_src_dir(std::vector<std::string> ext_inc)
#else
int compile_src_dir()
#endif
{
all_in_directory src_dir = get_all_files_in_dir(SRC_DIR);
all_in_directory obj_dir;
for (auto &&i : src_dir.dirs)
{
std::filesystem::create_directory(OBJ_DIR / i);
}
std::unordered_set<std::string> modified_heders;
all_in_directory inc_dir = get_all_files_in_dir(INC_DIR);
for (size_t i = 0; i < inc_dir.files.size(); i++)
{
if (check_if_rebuild(inc_dir.files[i], BUILD_FILE))
{
std::string heder = inc_dir.files[i].string().substr(4);
modified_heders.insert(heder);
}
}
command.clear();
command.push_back(cpp_compiler);
command.push_back("-c");
int src_loc = command.size();
command.push_back("");
command.push_back("-o");
int obj_loc = command.size();
command.push_back("");
command.push_back(opt_flags);
#ifdef EXT_INC
command.insert(command.end(), ext_inc.begin(), ext_inc.end());
#endif
command.push_back("-I" + INC_DIR.string());
command.push_back("-std=c++20");
command.push_back("-Wall");
#ifdef Wextra
command.push_back("-Wextra");
#endif
#ifdef Werror
command.push_back("-Werror");
#endif
int build = 0;
for (auto &&i : src_dir.files)
{
std::filesystem::path tmp = i;
std::filesystem::path out = OBJ_DIR / tmp.replace_extension(".o");
bool reb = false;
// check if .cpp changed
if (check_if_rebuild(i, out))
reb = true;
// if .cpp didnt change check include files changed
if (!reb)
{
std::vector<std::string> incudes = get_includes(i);
for (auto &&j : incudes)
{
if (modified_heders.find(j) != modified_heders.end())
{
reb = true;
break;
}
}
}
if (reb)
{
command[src_loc] = i.string();
command[obj_loc] = out.string();
if (!run_command(command))
return -1;
build = 1;
}
}
return build;
}
#if defined(EXT_LINK)
bool compile_obj_dir(std::vector<std::string> ext_link)
#else
bool compile_obj_dir()
#endif
{
all_in_directory obj_dir = get_all_files_in_dir(OBJ_DIR);
command.clear();
command.push_back(cpp_compiler);
for (auto &&i : obj_dir.files)
{
command.push_back(i.string());
}
command.push_back(opt_flags);
command.push_back("-o");
command.push_back(BUILD_FILE.string());
#if defined(EXT_LINK)
command.insert(command.end(), ext_link.begin(), ext_link.end());
#endif
return run_command(command);
}
#if defined(EXT_OBJ) && defined(EXT_INC)
void build_as_one(std::vector<std::string> ext_obj, std::vector<std::string> ext_inc)
#elif defined(EXT_OBJ)
void build_as_one(std::vector<std::string> ext_obj)
#elif defined(EXT_INC)
void build_as_one(std::vector<std::string> ext_inc)
#else
void build_as_one()
#endif
{
all_in_directory src_dir = get_all_files_in_dir(SRC_DIR);
command.clear();
command.push_back(cpp_compiler);
command.push_back("-o");
command.push_back(BUILD_FILE.string());
command.push_back(opt_flags);
#ifdef EXT_INC
command.insert(command.end(), ext_inc.begin(), ext_inc.end());
#endif
command.push_back("-I" + INC_DIR.string());
command.push_back("-std=c++20");
command.push_back("-Wall");
#ifdef Wextra
command.push_back("-Wextra");
#endif
#ifdef Werror
command.push_back("-Werror");
#endif
for (auto &&i : src_dir.files)
{
command.push_back(i.string());
}
#ifdef EXT_OBJ
command.insert(command.end(), ext_obj.begin(), ext_obj.end());
#endif
run_command(command);
}
#ifdef EXT_ARGS
bool run_main(std::vector<std::string> ext_args)
#else
bool run_main()
#endif
{
command.clear();
#ifndef _WIN32
command.push_back("./" + BUILD_FILE.string());
#else
command.push_back(BUILD_FILE.string());
#endif
#ifdef EXT_ARGS
command.insert(command.end(), ext_args.begin(), ext_args.end());
#endif
return run_command(command);
}

View File

@@ -0,0 +1,68 @@
#include <raymath.h>
#include <cstdio>
int main(int argc, char const *argv[])
{
std::printf("\n\n\n");
Vector2 mousePosition = {.x = 15, .y = 10};
Vector2 mouseStart;
Vector2 orgPosition = {.x = 0, .y = 10};
float screenWidth = 30;
mouseStart = mousePosition;
std::printf("MouseStart x:%f, y:%f\n", mouseStart.x, mouseStart.y);
std::printf("orgPosition x:%f, y:%f\n", orgPosition.x, orgPosition.y);
// distance betwen corner of image and mouse startPoint
float len = Vector2Distance(mouseStart, orgPosition);
std::printf("len %f\n", len);
// angle in rad betwen mouse and image corner
float atan2x = orgPosition.x - mouseStart.x;
float atan2y = orgPosition.y - mouseStart.y;
std::printf("atan2 x:%f, y:%f\n", atan2x, atan2y);
float angleOfset = std::atan2( orgPosition.y - mouseStart.y, orgPosition.x - mouseStart.x);
std::printf("angleOffset %f\n", angleOfset);
mousePosition = {.x = 20, .y = 10};
// get distance that mouse has moved from original press location
// can be positive or negative if mouse start on right side and
// end on left it will be 0 - 100 so -100 dist
float dist = mousePosition.x - mouseStart.x;
printf("dist: %f\n", dist);
// this value can get from -1 to 1 based on if we swipe left or right
float l = dist / screenWidth;
printf("l: %f\n", l);
// first convert range of -1 to 1 to 0 to 1
// then change to degres from 45 to -45
float rotation = Lerp(45.0f, -45.0f, (l + 1) / 2);
printf("rotation: %f\n", rotation);
// convert to radians
float angle = ((rotation)*PI) / 180.0f;
printf("angle: %f\n", angle);
angle += angleOfset;
printf("angle + angleOfser: %f\n", angle);
// vector betwen image corrner and mouse
Vector2 newCornerVec = {.x = len * std::cos(angle), .y = len * std::sin(angle)};
printf("newCornerVec x: %f, y:%f \n", newCornerVec.x, newCornerVec.y);
// get global vector where to draw image
Vector2 newPosition;
newPosition.x = newCornerVec.x + mousePosition.x;
newPosition.y = newCornerVec.y + mousePosition.y;
printf("New position x: %f, y:%f \n", newPosition.x, newPosition.y);
printf("Draw image at this angle %f\n", rotation + 90);
return 0;
}

147
random/slike.cpp Normal file
View File

@@ -0,0 +1,147 @@
#include <raylib.h>
#include <raymath.h>
#include <cmath>
#include <vector>
Vector2 newPoint(Vector2 start, float len, float angleD){
float angleR = (angleD * PI) / 180; // radian
return {start.x + len * cos(angleR), start.y - len * sin(angleR)};
}
void drawBranch(Vector2 startV, Vector2 endV, Color startC, Color endC, int startR, int endR){
float fstep = 0.05;
for (float i = 0; i < 1.05; i += fstep)
{
Vector2 point = Vector2Lerp(startV, endV, i);
Color color = ColorLerp(startC, endC, i);
int size = Lerp(startR, endR, i);
DrawCircleV(point, size, color);
}
}
constexpr float screenWidth = 800;
constexpr float screenHeight = 800;
constexpr float len = 300;
constexpr float angleD = 100;
constexpr Vector2 start = {screenWidth / 2, screenHeight - 100};
constexpr float radius = 20;
constexpr float radiusS = 80;
constexpr float radiusE = 60;
constexpr Color colorS = RED;
constexpr Color colorE = GREEN;
typedef void (*slika)();
int idx = 0;
std::vector<slika> v_slik = {
[]()
{
DrawCircleV(start, radius, BLACK);
},
[]()
{
Vector2 end = {start.x, start.y - len};
DrawCircleV(start, radius, BLACK);
DrawLineEx(start, end, 10, BLACK);
},
[]()
{
Vector2 end = newPoint(start, len, angleD);
DrawCircleV(start, radius, BLACK);
DrawLineEx(start, end, 10, BLACK);
DrawCircleV(end, radius, BLACK);
DrawCircleSectorLines(start, len / 3, 360 - angleD, 360, 30, BLACK);
},
[]()
{
Vector2 end = newPoint(start, len, angleD);
DrawCircleV(start, radius, BLACK);
DrawLineEx(start, end, 10, BLACK);
DrawCircleV(end, radius, BLACK);
DrawCircleLinesV(start, radiusS, BLACK);
DrawCircleLinesV(end, radiusE, BLACK);
},
[]()
{
Vector2 end = newPoint(start, len, angleD);
DrawLineEx(start, end, 10, BLACK);
DrawCircleV(start, radiusS, colorS);
DrawCircleV(end, radiusE, colorE);
},
[]()
{
Vector2 end = newPoint(start, len, angleD);
drawBranch(start, end, colorS, colorE, radiusS, radiusE);
},
[]()
{
Vector2 p = newPoint(start, len, angleD);
drawBranch(start, p, colorS, colorE, radiusS, radiusE);
Vector2 p1 = newPoint(p, len, 135);
Vector2 p2 = newPoint(p, len, 90);
Vector2 p3 = newPoint(p, len, 45);
DrawLineEx(p, p1, 10, BLACK);
DrawLineEx(p, p2, 10, BLACK);
DrawLineEx(p, p3, 10, BLACK);
DrawCircleV(p, radius, BLACK);
DrawCircleV(p1, radius, BLACK);
DrawCircleV(p2, radius, BLACK);
DrawCircleV(p3, radius, BLACK);
},
[]()
{
Vector2 p = newPoint(start, len, angleD);
drawBranch(start, p, colorS, colorE, radiusS, radiusE);
Vector2 p1 = newPoint(p, len, 135);
Vector2 p2 = newPoint(p, len, 90);
Vector2 p3 = newPoint(p, len, 45);
drawBranch(p, p1, colorE, BLUE, radiusE, 50);
drawBranch(p, p2, colorE, ORANGE, radiusE, 50);
drawBranch(p, p3, colorE, PURPLE, radiusE, 50);
},
};
int main(int argc, char *argv[])
{
InitWindow(screenWidth, screenHeight, "Slike");
SetTargetFPS(60);
while (!WindowShouldClose())
{
BeginDrawing();
if (IsKeyPressed(KEY_S))
{
TakeScreenshot(TextFormat("slika%.2d.png", idx));
}
if (IsKeyPressed(KEY_N))
{
idx++;
if (idx >= v_slik.size())
idx = v_slik.size() - 1;
}
if (IsKeyPressed(KEY_P))
{
idx--;
if (idx < 0)
idx = 0;
}
ClearBackground(WHITE);
v_slik[idx]();
EndDrawing();
}
CloseWindow();
return 0;
}

View File

@@ -6,14 +6,91 @@
#include <iostream>
#include <map>
#include <thread>
#include <unordered_map>
#include <mutex>
#include <thread>
// use pthread rw_lock to lock db so you can safy clone .db file
class BlockedList
{
private:
std::mutex m;
sqlite3 *db;
sqlite3_stmt *sel_stmt;
sqlite3_stmt *ins_stmt;
sqlite3_stmt *upd_stmt;
public:
void init()
{
int rc = sql::open(DB_NAME, &db);
if (rc)
{
fprintf(stderr, "Can't open database: %s\n", sql::errmsg(db));
return;
}
sql::prepare_v2(db, get_warnings, -1, &sel_stmt, NULL);
sql::prepare_v2(db, insert_warnings, -1, &ins_stmt, NULL);
sql::prepare_v2(db, set_warnings, -1, &upd_stmt, NULL);
}
bool isBlocked(uint32_t id)
{
std::scoped_lock lock(m);
sql::bind_int64(sel_stmt, 1, id);
int64_t found = 0;
if (sql::step(sel_stmt) == SQLITE_ROW)
{
int type = sql::column_type(sel_stmt, 0);
if (type != SQL_NULL)
found = sql::column_int64(sel_stmt, 0);
}
sql::reset(sel_stmt);
return found >= 3;
}
void addWarning(uint32_t id)
{
std::scoped_lock lock(m);
sql::bind_int64(sel_stmt, 1, id);
int64_t found = 0;
int res = sql::step(sel_stmt);
if (res == SQLITE_ROW)
{
int type = sql::column_type(sel_stmt, 0);
if (type != SQL_NULL)
{
found = sql::column_int64(sel_stmt, 0);
}
sql::reset(sel_stmt);
}
else if (res == SQL_DONE)
{
sql::bind_int64(ins_stmt, 1, id);
sql::step(ins_stmt);
sql::reset(ins_stmt);
sql::reset(sel_stmt);
return;
}
sql::bind_int64(upd_stmt, 1, found + 1);
sql::bind_int64(upd_stmt, 2, id);
sql::step(upd_stmt);
sql::reset(upd_stmt);
}
};
BlockedList blockedList;
// When a new client connected:
void call(int sock, sockaddr_in newSocketInfo)
{
std::string add = TcpSocket::remoteAddress(newSocketInfo);
printf("new: %s\n", add.c_str());
//printf("new: %s\n", add.c_str());
int64_t conf, id;
Message message;
@@ -22,15 +99,17 @@ void call(int sock, sockaddr_in newSocketInfo)
if (conf != StartHeader)
{
printf("StartHeader ERROR\n");
//printf("StartHeader ERROR\n");
TcpSocket::closet(sock);
//blockedList.addWarning(newSocketInfo.sin_addr.s_addr);
return;
}
if (id == 0)
{
printf("ID ERROR\n");
//printf("ID ERROR\n");
TcpSocket::closet(sock);
//blockedList.addWarning(newSocketInfo.sin_addr.s_addr);
return;
}
@@ -51,8 +130,6 @@ void call(int sock, sockaddr_in newSocketInfo)
int64_t gen = 0;
while (sql::step(max_gen_stmt) != SQL_DONE)
{
int num_cols = sql::column_count(max_gen_stmt);
int type = sql::column_type(max_gen_stmt, 0);
if (type == SQL_NULL)
@@ -67,12 +144,12 @@ void call(int sock, sockaddr_in newSocketInfo)
sqlite3_stmt *insert_user_stmt;
sql::prepare_v2(db, insert_user_data, -1, &insert_user_stmt, NULL);
// limit how many gen they can send so some cant just come to here and start sendding random data.
while (ok)
{
message.mess = Mess::REQ_SEND_GEN;
message.data = gen;
printf("requesting gen %ld\n", gen);
//printf("requesting gen %ld\n", gen);
TcpSocket::sendt(sock, &message, sizeof(Message));
TcpSocket::recvt(sock, &message, sizeof(Message));
@@ -110,7 +187,7 @@ void call(int sock, sockaddr_in newSocketInfo)
sql::finalize(insert_liked_stmt);
sql::close(db);
printf("del\n");
//printf("del\n");
TcpSocket::closet(sock);
}
@@ -133,7 +210,8 @@ int main()
{
sql::init();
sql::create_tables();
std::thread t(checker);
blockedList.init();
//std::thread t(checker);
std::thread l(listen_key);
// Bind the server to a port.
int err = TcpSocket::listent("0.0.0.0", serverPort, call);

View File

@@ -1,36 +0,0 @@
#version 100
precision mediump float;
varying vec2 fragTexCoord;
varying vec4 fragColor;
uniform vec3 color;
uniform float sun_radius;
uniform float start_transperency;
vec2 offset = vec2(1.0, 1.0);
float sun_end = 1.0;
void main()
{
offset.x -= fragTexCoord.x * 2.0;
offset.y -= fragTexCoord.y * 2.0;
float radius = length(offset);
if(radius < sun_radius){
gl_FragColor = vec4(color, 1.0);
}else if(radius < sun_end){
float gradient = radius;
gradient -= sun_radius;
gradient = gradient / (sun_end - sun_radius) * start_transperency;
gl_FragColor = vec4(color, start_transperency - gradient);
}else{
gl_FragColor = vec4(color, 0.0);
}
}

View File

@@ -1,28 +0,0 @@
#version 330
in vec2 fragTexCoord;
in vec4 fragColor;
out vec4 finalColor;
uniform vec3 color;
uniform float sun_radius;
uniform float start_transperency;
vec2 offset = vec2(1.0f, 1.0f);
float sun_end = 1.0f;
void main()
{
offset.x -= fragTexCoord.x * 2;
offset.y -= fragTexCoord.y * 2;
float radius = length(offset);
if(radius < sun_radius){
finalColor = vec4(color, 1.0f);
}else if(radius < sun_end){
float gradient = radius;
gradient -= sun_radius;
gradient = gradient / (sun_end - sun_radius) * start_transperency;
finalColor = vec4(color, start_transperency - gradient);
}else{
finalColor = vec4(color, 0.0f);
}
}

View File

@@ -13,7 +13,6 @@ public:
void newGen(RenderTexture2D &target, Dna *dna);
bool tick(RenderTexture2D &target);
private:
BackGround backGround;
Tree tree;
};

View File

@@ -1,26 +0,0 @@
#include <raylib.h>
class Circle
{
public:
static void init();
static void deinit();
static void setColor(Color color);
static void setSoftEdge(bool soft);
static void draw(float x, float y, float size);
Circle() = delete;
private:
static const int sizeTexute = 250;
static RenderTexture2D target;
static Shader shader;
static float radius;
static float start_transperency;
static float c[3];
static int radius_loc;
static int start_transperency_loc;
static int colorLoc;
};

View File

@@ -23,16 +23,20 @@ public:
void init(int size);
void draw(Dna *dna);
bool tick();
uint64_t circles;
private:
Dna *m_dna;
uint128 branchSeed;
//Texture2D texBunny;
Vector2 start = {0};
std::list<DrawArgs> drawCalls;
void drawBranch();
void calculateBranch();
void drawBranch_rlTriangle(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness, float length);
void drawBranch_circles(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness, float length);
inline size_t getNumOfBranches(int dep);
inline Color getStartColor(DrawArgs &arg);
inline Color getEndColor(int dep, Color &start);

View File

@@ -1,88 +0,0 @@
const char *sun_shader_100 = "#version 100\n\
precision mediump float;\
varying vec2 fragTexCoord;\
varying vec4 fragColor;\
uniform vec3 color;\
uniform float sun_radius;\
uniform float start_transperency;\
vec2 offset = vec2(1.0, 1.0);\
float sun_end = 1.0;\
void main()\
{\
offset.x -= fragTexCoord.x * 2.0;\
offset.y -= fragTexCoord.y * 2.0;\
float radius = length(offset);\
if (radius < sun_radius) {\
gl_FragColor = vec4(color, 1.0);\
}\
else if (radius < sun_end) {\
float gradient = radius;\
gradient -= sun_radius;\
gradient = gradient / (sun_end - sun_radius) * start_transperency;\
gl_FragColor = vec4(color, start_transperency - gradient);\
}\
else {\
gl_FragColor = vec4(color, 0.0);\
}\
}";
unsigned char shaders_sun_330_fs[] = {
0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x33, 0x33, 0x30,
0x0a, 0x0a, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x66, 0x72,
0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a,
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x66, 0x72, 0x61, 0x67,
0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x76,
0x65, 0x63, 0x34, 0x20, 0x66, 0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c,
0x6f, 0x72, 0x3b, 0x0a, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d,
0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b,
0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x66, 0x6c, 0x6f,
0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75,
0x73, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x66,
0x6c, 0x6f, 0x61, 0x74, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74,
0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x3b,
0x0a, 0x76, 0x65, 0x63, 0x32, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74,
0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, 0x31, 0x2e, 0x30, 0x66,
0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x66, 0x6c, 0x6f,
0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x20, 0x3d,
0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64,
0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x78, 0x20, 0x2d,
0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f,
0x72, 0x64, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x3b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x79, 0x20, 0x2d,
0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f,
0x72, 0x64, 0x2e, 0x79, 0x20, 0x2a, 0x20, 0x32, 0x3b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x72, 0x61, 0x64, 0x69,
0x75, 0x73, 0x20, 0x3d, 0x20, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x28,
0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x69, 0x66, 0x28, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x20, 0x3c,
0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29,
0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69,
0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76,
0x65, 0x63, 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x31,
0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65,
0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x28, 0x72, 0x61, 0x64, 0x69, 0x75,
0x73, 0x20, 0x3c, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x29,
0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6c,
0x6f, 0x61, 0x74, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74,
0x20, 0x3d, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69,
0x65, 0x6e, 0x74, 0x20, 0x2d, 0x3d, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72,
0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20,
0x3d, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x2f,
0x20, 0x28, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x20, 0x2d, 0x20,
0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29, 0x20,
0x2a, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, 0x72, 0x61, 0x6e,
0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x3b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69, 0x6e, 0x61, 0x6c, 0x43,
0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28,
0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74,
0x5f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63,
0x79, 0x20, 0x2d, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74,
0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65, 0x6c, 0x73, 0x65,
0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69,
0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76,
0x65, 0x63, 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x30,
0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a,
0x7d, 0x0a};

View File

@@ -4,6 +4,12 @@ const char create_like_table[] = "CREATE TABLE IF NOT EXISTS like_table ( ID INT
const char create_user_table[] = "CREATE TABLE IF NOT EXISTS user_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, CHECKED INTEGER);";
const char create_blocked_ip_table[] = "CREATE TABLE IF NOT EXISTS blocked_ip_table ( ID INTEGER PRIMARY KEY, WARNINGS INTEGER);";
const char get_warnings[] = "SELECT WARNINGS FROM blocked_ip_table WHERE ID = ?;";
const char insert_warnings[] = "INSERT INTO blocked_ip_table (ID, WARNINGS) VALUES(?, 1);";
const char set_warnings[] = "UPDATE blocked_ip_table SET WARNINGS = ? WHERE ID = ?;";
const char max_gen[] = "SELECT MAX(GEN) FROM user_table WHERE USER_ID = ?;";
const char insert_user_data[] = "INSERT INTO user_table (USER_ID, GEN, CHECKED) VALUES(?, ?, 0);";
@@ -27,8 +33,10 @@ constexpr char DB_NAME[] = "data.db";
struct sqlite3;
struct sqlite3_stmt;
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
#define SQL_DONE 101
#define SQL_NULL 5
#define SQLITE_OK 0
namespace sql
{

18
shared/inc/timing.hpp Normal file
View File

@@ -0,0 +1,18 @@
#include <chrono>
struct Timer
{
std::chrono::steady_clock::time_point start_t;
std::chrono::steady_clock::time_point end_t;
std::chrono::nanoseconds dur;
void start();
void stop();
void reset();
void print(const char * str);
void stop_and_print(const char * str);
void reset_and_start();
};

View File

@@ -45,6 +45,7 @@ struct Dna
uint8_t colorSet;
Branch branches[MAX_DEPTH];
};
namespace DNA
{
void newDna(Dna *dna, uint128 *state);

View File

@@ -3,15 +3,16 @@
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 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);
float calc_similarity(std::vector<Dna> &vec, simil_func f);
float calc_similarity(std::vector<Dna> &vec, simil_func f = hamming_distance_without_seeds);
}

View File

@@ -1,8 +1,8 @@
#include <cmath>
#include <algorithm>
#include "canvas/BackGround.hpp"
#include "canvas/BackGroundColors.hpp"
#include "canvas/Circle.hpp"
#include "canvas/stb_perlin.h"
#include <raylib.h>
@@ -44,6 +44,9 @@ Color ColorAdd(Color c1, Color c2)
void BackGround::init(int canvasSize)
{
this->canvasSize = canvasSize;
// resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika
// https://github.com/raysan5/raylib/issues/3820
rlSetBlendFactorsSeparate(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA, RL_ONE, RL_ONE, RL_FUNC_ADD, RL_MAX);
}
void BackGround::deinit()
@@ -52,7 +55,6 @@ void BackGround::deinit()
void BackGround::draw(Dna *dna)
{
Circle::setSoftEdge(true);
m_dna = dna;
mountenSeed = dna->mountenSeed;
starSeed = dna->starSeed;
@@ -118,26 +120,45 @@ void BackGround::drawStars()
void BackGround::drawSun()
{
int r = ((m_dna->moonY / 255.0f * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
int radius = ((m_dna->moonY / 255.0f * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
int xpos = Lerp(canvasSize * moonXOffset, canvasSize - canvasSize * moonXOffset, m_dna->moonX / 255.0f);
int ypos = Lerp(canvasSize * moonXOffset, maxYPosOfMoon * canvasSize, m_dna->moonY / 255.0f);
Color color = {0};
if (getColorSet() == 3)
{
Circle::setColor(BackGroundColors::moonColor);
r = (((m_dna->moonSize / 255.0f) * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
color = BackGroundColors::moonColor;
radius = (((m_dna->moonSize / 255.0f) * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
}
else
{
Color color = {0};
color.r = 255;
color.g = std::lerp(200, 50, m_dna->moonY / 255.0f);
color.b = std::lerp(50, 0, m_dna->moonY / 255.0f);
color.a = 255;
Circle::setColor(color);
}
Circle::draw(xpos, ypos, r);
BeginBlendMode(RL_BLEND_CUSTOM_SEPARATE);
constexpr float numOfCircles = 15.0f;
constexpr float sunMin = 1.7f;
float amount = 0.0f;
color.a = 15;
for (size_t i = 0; i < numOfCircles; i++)
{
float r = Lerp(radius, radius / sunMin, amount);
DrawCircle(xpos, ypos, r, color);
amount += 1.0f / numOfCircles;
}
EndBlendMode();
color.a = 255;
DrawCircle(xpos, ypos, radius / sunMin, color);
}
void BackGround::drawMounten(size_t mountenSegments, int min, int max, Color color, float scale)

View File

@@ -1,15 +1,19 @@
#include "canvas/Canvas.hpp"
#include "canvas/Circle.hpp"
#include "timing.hpp"
void Canvas::init(int size)
{
backGround.init(size);
tree.init(size);
Circle::init();
}
bool finished = false;
Timer timer;
void Canvas::newGen(RenderTexture2D &target, Dna *dna)
{
finished = false;
timer.reset_and_start();
BeginTextureMode(target);
ClearBackground(WHITE);
@@ -17,18 +21,24 @@ void Canvas::newGen(RenderTexture2D &target, Dna *dna)
tree.draw(dna);
EndTextureMode();
timer.stop();
}
bool Canvas::tick(RenderTexture2D &target)
{
timer.start();
BeginTextureMode(target);
bool ret = tree.tick();
EndTextureMode();
timer.stop();
if(ret && !finished){
finished = true;
timer.print("to draw canvas");
}
return ret;
}
void Canvas::deinit()
{
backGround.deinit();
Circle::deinit();
}

View File

@@ -1,83 +0,0 @@
#include "canvas/Circle.hpp"
#include "canvas/sunShader.hpp"
#include <raylib.h>
#include <rlgl.h>
RenderTexture2D Circle::target;
Shader Circle::shader;
float Circle::radius;
float Circle::start_transperency;
float Circle::c[3];
int Circle::radius_loc;
int Circle::start_transperency_loc;
int Circle::colorLoc;
void Circle::init()
{
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
shader = LoadShaderFromMemory(nullptr, sun_shader_100);
#else
shader = LoadShaderFromMemory(nullptr, (const char *)shaders_sun_330_fs);
#endif
target = LoadRenderTexture(sizeTexute, sizeTexute);
radius = 0.6f;
radius_loc = GetShaderLocation(shader, "sun_radius");
SetShaderValue(shader, radius_loc, &radius, SHADER_UNIFORM_FLOAT);
start_transperency = 0.40f;
start_transperency_loc = GetShaderLocation(shader, "start_transperency");
SetShaderValue(shader, start_transperency_loc, &start_transperency, SHADER_UNIFORM_FLOAT);
c[0] = 1.0f;
c[1] = 1.0f;
c[2] = 1.0f;
colorLoc = GetShaderLocation(shader, "color");
SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3);
// resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika
// https://github.com/raysan5/raylib/issues/3820
rlSetBlendFactorsSeparate(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA, RL_ONE, RL_ONE, RL_FUNC_ADD, RL_MAX);
}
void Circle::deinit()
{
UnloadShader(shader);
UnloadRenderTexture(target);
}
void Circle::setColor(Color color)
{
c[0] = color.r / 255.0f;
c[1] = color.g / 255.0f;
c[2] = color.b / 255.0f;
SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3);
}
void Circle::setSoftEdge(bool soft)
{
if (soft)
{
radius = 0.6f;
}
else
{
radius = 1.0f;
}
SetShaderValue(shader, radius_loc, &radius, SHADER_UNIFORM_FLOAT);
}
void Circle::draw(float x, float y, float size)
{
Rectangle dest = {x - size, y - size, size * 2, size * 2};
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
Vector2 origin = {0.0f, 0.0f};
// zgorni komentar da se mesanje barv oklopi pravilno
BeginBlendMode(RL_BLEND_CUSTOM_SEPARATE);
BeginShaderMode(shader);
DrawTexturePro(target.texture, source, dest, origin, 0.0f, WHITE);
EndShaderMode();
EndBlendMode();
}

View File

@@ -1,12 +1,13 @@
#include <cmath>
#include "canvas/Tree.hpp"
#include "canvas/Circle.hpp"
#include "timing.hpp"
#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
#define ITER_PER_FRAME 5000
#define ITER_PER_FRAME 10000
constexpr int maxColorChange = 15;
constexpr int minColorChange = -15;
@@ -41,28 +42,28 @@ void Tree::init(int size)
start.x = size / 2;
start.y = size;
calculateLevels(size);
// texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
}
void Tree::draw(Dna *dna)
{
Circle::setSoftEdge(false);
circles = 0;
m_dna = dna;
branchSeed = dna->branchSeed;
drawCalls.push_back({start, 180.0f, 0});
tick();
}
bool Tree::tick()
{
size_t i = 0;
while (!drawCalls.empty())
using namespace std::chrono;
auto start = steady_clock::now();
nanoseconds dur = 0us;
while (!drawCalls.empty() && dur < 14ms)
{
drawBranch();
calculateBranch();
drawCalls.pop_front();
i++;
if (i >= ITER_PER_FRAME)
break;
auto end = steady_clock::now();
dur = end - start;
}
return drawCalls.empty();
@@ -70,7 +71,7 @@ bool Tree::tick()
// Private
void Tree::drawBranch()
void Tree::calculateBranch()
{
DrawArgs arg = drawCalls.front();
if (arg.dep == MAX_DEPTH)
@@ -84,24 +85,13 @@ void Tree::drawBranch()
int sizeStart = getStartSize(arg);
int sizeEnd = getEndSize(arg, sizeStart);
float fstep = 1.0 / ((length / sizeStart) * 2.0f);
Color colorStart = getStartColor(arg);
Color colorEnd = getEndColor(arg.dep, colorStart);
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); // Fester on the phone to call DrawCircle insted of the Circle shader
// Circle::setColor(color);
// Circle::draw(point.x, point.y, thick); // TODO Change to BeginShaderMode and EndShaderMode only onece
// use
// DrawRectangleGradientEx
}
// drawBranch_rlTriangle(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd, length);
drawBranch_circles(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd, length);
// DrawLineEx(arg.start, end, sizeStart, colorStart);
// add more branches to draw
if (arg.dep + 1 >= MAX_DEPTH)
@@ -119,6 +109,7 @@ void Tree::drawBranch()
inline size_t Tree::getNumOfBranches(int dep)
{
return 3;
if (m_dna->branches[dep].branchCount < 128)
return 2;
else
@@ -168,7 +159,7 @@ inline int Tree::getStartSize(DrawArgs &arg)
float mixLevel = m_dna->branches[arg.dep].sizeLevel / 255.0f;
size = std::lerp(size, sizes[MAX_DEPTH - arg.dep - 1], mixLevel);
if (size < 1)
//if (size < 1)
size = 1;
return size;
}
@@ -178,7 +169,7 @@ inline int Tree::getEndSize(DrawArgs &arg, int start)
int size = Remap(m_dna->branches[arg.dep].sizeChange, 0, 255, MinSizeChange, maxSizeChange);
size += start;
if (size < 1)
//if (size < 1)
size = 1;
return size;
}
@@ -186,9 +177,9 @@ inline int Tree::getEndSize(DrawArgs &arg, int start)
inline float Tree::getLength(DrawArgs &arg)
{
float lenght = lengths[arg.dep];
float lenghtRatio = Remap(m_dna->branches[arg.dep].length, 0, 255, 0.5f, 1.3f);
float lenghtRatio = Remap(255, 0, 255, 0.5f, 1.3f);
lenght *= lenghtRatio;
float lenghtVar = Remap(m_dna->branches[arg.dep].lengthVar, 0, 255, -0.15f, 0.15f);
float lenghtVar = Remap(255, 0, 255, -0.15f, 0.15f);
lenght += lenght * lenghtVar * mrand::getFloat(&branchSeed);
if (lenght < 1)
lenght = 1;
@@ -203,3 +194,64 @@ inline float Tree::getAngleVar(DrawArgs &arg)
return angleVar;
}
void Tree::drawBranch_circles(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness, float length)
{
float fstep = 1.0 / ((length / startThickness) * 2.0f);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(startPoint, endPoint, i);
Color color = ColorLerp(startColor, endColor, i);
int size = Lerp(startThickness, endThickness, i);
DrawCircleV(point, size, color);
circles++;
}
}
void Tree::drawBranch_rlTriangle(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness, float length)
{
DrawCircleV(startPoint, startThickness, startColor);
DrawCircleV(endPoint, endThickness, endColor);
// Calculate the direction vector from startPoint to endPoint
Vector2 direction = {endPoint.x - startPoint.x, endPoint.y - startPoint.y};
// Normalize the direction vector
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

@@ -50,6 +50,17 @@ namespace sql
fprintf(stdout, "user_table created successfully\n");
}
rc = sqlite3_exec(db, create_blocked_ip_table, nullptr, 0, &zErrMsg);
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "blocked_ip_table created successfully\n");
}
sqlite3_close(db);
}

42
shared/src/timing.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include <raylib.h>
#include <chrono>
#include "timing.hpp"
using namespace std::chrono;
void Timer::start()
{
start_t = std::chrono::steady_clock::now();
}
void Timer::stop()
{
end_t = std::chrono::steady_clock::now();
dur += end_t - start_t;
}
void Timer::reset()
{
dur = 0ns;
}
void Timer::print(const char * str)
{
auto s = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
if (s > 1000us)
{
TraceLog(LOG_INFO, "%ld ms %s", s.count(), str);
}
}
void Timer::stop_and_print(const char *str)
{
stop();
print(str);
}
void Timer::reset_and_start()
{
reset();
start();
}

View File

@@ -52,6 +52,7 @@ namespace DNA
}
}
}
void mutate(Dna *dna, uint32_t num, uint128 *state)
{
uint8_t *array = (uint8_t *)dna;

View File

@@ -1,8 +1,43 @@
#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
@@ -31,10 +66,8 @@ namespace Similarity
{
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;
@@ -48,7 +81,6 @@ namespace Similarity
}
mag1 = sqrt(mag1);
mag2 = sqrt(mag2);
return dot_prod / (mag1 * mag2);
}
@@ -67,8 +99,62 @@ namespace Similarity
return 1 - (distance / sizeof(Dna));
}
float hamming_distance_without_seeds(Dna *d1, Dna *d2)
{
constexpr size_t start = sizeof(uint128) * 3;
constexpr size_t end = sizeof(Dna);
uint8_t *d1a = (uint8_t *)d1;
uint8_t *d2a = (uint8_t *)d2;
float distance = 0;
for (size_t i = start; i < end; i++)
{
if (d1a[i] != d2a[i])
{
distance++;
}
}
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_distance";
}
else if (f == &Similarity::hamming_distance_without_seeds)
{
return "hamming_distance_without_seeds";
}
else if (f == &Similarity::levenshtein_distance)
{
return "leven";
}
else
{
return "unknown nameofFunc";
}
}
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;
@@ -80,6 +166,47 @@ 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);
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));
}
}

14
tmp.txt Normal file
View File

@@ -0,0 +1,14 @@
eucl cos cos_i hamming dot leven
91 117 181 87 41 105799
60 78 305 250 40 100331
61 78 121 105 40 97438
66 81 124 106 40 97529
60 78 127 108 40 96296
62 85 131 104 39 96456
61 81 125 106 40 96510
61 81 125 103 40 97253
61 81 125 78 40 97409
60 82 125 103 40 99816
62 81 128 81 40 98978
68 81 126 58 40 98289
61 88 130 60 39 99663

View File

@@ -11,13 +11,16 @@ enum DrawingStage
drawTree,
drawBig,
calSim,
save,
done,
};
constexpr int numberOfFunc = 6;
class Vapp
{
public:
void init();
void init(char* filename);
void update();
void draw();
void deinit();
@@ -25,6 +28,7 @@ public:
private:
bool showSelection;
bool showStats;
bool saveToFile;
sqlite3 *db;
sqlite3_stmt *get_gen_num;
std::vector<int64_t> ids;
@@ -45,9 +49,10 @@ private:
int drawX = 0;
int drawY = 0;
void setUpManager();
std::array<float, 3> simil;
std::vector<std::array<float, 3>> similTable;
std::array<float, numberOfFunc> simil;
std::vector<std::array<float, numberOfFunc>> similTable;
void setUpTable();
void drawToFile();
};

View File

@@ -6,12 +6,13 @@
#include <rlImGui.h>
#include <imgui.h>
#include <raylib.h>
#include <fstream>
const char select_user_id[] = "SELECT USER_ID FROM user_table GROUP BY USER_ID;";
constexpr int sizeOfCanvas = 1000;
void Vapp::init()
void Vapp::init(char *filename)
{
bigTexture = LoadRenderTexture(sizeOfCanvas * 4, sizeOfCanvas * 4);
treeTexture = LoadRenderTexture(sizeOfCanvas, sizeOfCanvas);
@@ -20,7 +21,7 @@ void Vapp::init()
DnaManager::setUp(&manager, 0);
sql::init();
sql::open(DB_NAME, &db);
sql::open(filename, &db);
sqlite3_stmt *stmt;
sql::prepare_v2(db, select_user_id, -1, &stmt, NULL);
@@ -100,12 +101,18 @@ void Vapp::update()
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[0] = Similarity::calc_similarity(manager.vector, Similarity::euclidean_distance);
simil[1] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
simil[2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
simil[3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
simil[4] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
simil[5] = Similarity::calc_similarity(manager.vector, Similarity::dot_minmax);
stageOfDrawing = DrawingStage::save;
break;
case DrawingStage::save:
if(saveToFile) drawToFile();
stageOfDrawing = DrawingStage::done;
break;
case DrawingStage::done:
enableAll = true;
break;
@@ -133,6 +140,10 @@ void Vapp::draw()
{
setUpTable();
}
if(ImGui::MenuItem("Save to File", nullptr, saveToFile, true))
{
saveToFile = !saveToFile;
}
ImGui::EndMainMenuBar();
}
@@ -174,14 +185,35 @@ void Vapp::draw()
if (showStats)
{
ImGui::Begin("Status", &showStats);
ImGui::LabelText("##sim1", "cosine_similarity: %f", simil[0]);
ImGui::LabelText("##sim2", "hamming_distance: %f", simil[1]);
ImGui::LabelText("##sim1", "euclidean_distance: %f", simil[0]);
ImGui::LabelText("##sim2", "cosine_similarity: %f", simil[1]);
ImGui::LabelText("##sim3", "cosine_similarity_int: %f", simil[2]);
ImGui::LabelText("##sim4", "hamming_distance: %f", simil[3]);
ImGui::LabelText("##sim5", "levenshtein_distance: %f", simil[4]);
ImGui::LabelText("##sim6", "dot_minmax: %f", simil[5]);
const ImGuiTableFlags flags = ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
const int columns = 4;
const int columns = numberOfFunc + 1;
if (ImGui::BeginTable("table1", columns, flags))
{
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::Text("index");
ImGui::TableSetColumnIndex(1);
ImGui::Text("euclidean_distance");
ImGui::TableSetColumnIndex(2);
ImGui::Text("cosine_similarity");
ImGui::TableSetColumnIndex(3);
ImGui::Text("cosine_similarity_int");
ImGui::TableSetColumnIndex(4);
ImGui::Text("hamming_distance");
ImGui::TableSetColumnIndex(5);
ImGui::Text("levenshtein_distance");
ImGui::TableSetColumnIndex(6);
ImGui::Text("dot_minmax");
for (int row = 0; row < similTable.size(); row++)
{
ImGui::TableNextRow();
@@ -279,7 +311,7 @@ void Vapp::setUpTable()
UiUnit unit = DnaManager::next(&manager);
if ((unit.index != pos))
{
// DOTO: SET ERROR
// TODO: SET ERROR
TraceLog(LOG_ERROR, "LOADING DNA");
sql::finalize(get_gen_stmt);
return;
@@ -292,10 +324,13 @@ void Vapp::setUpTable()
{
similTable.emplace_back();
int s = similTable.size() - 1;
similTable[s][0] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
similTable[s][1] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
similTable[s][2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
similTable[s][0] = Similarity::calc_similarity(manager.vector, Similarity::euclidean_distance);
similTable[s][1] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity);
similTable[s][2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
similTable[s][3] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
similTable[s][4] = Similarity::calc_similarity(manager.vector, Similarity::levenshtein_distance);
similTable[s][5] = Similarity::calc_similarity(manager.vector, Similarity::dot_minmax);
DnaManager::newGen(&manager);
}
else
@@ -304,7 +339,38 @@ void Vapp::setUpTable()
}
sql::reset(get_gen_stmt);
}
if(saveToFile)
{
int64_t id = ids[selected_id_index];
const char* buff = TextFormat("%ld.txt", id);
std::ofstream file(buff);
file << "| index | euclidean_distance | cosine_similarity | cosine_similarity_int | hamming_distance | levenshtein_distance | dot_minmax |\n";
file << "| --- | --- | --- | --- | --- | --- | --- |\n";
for (size_t i = 0; i < similTable.size(); i++)
{
file << "|" << i << "|";
for (size_t j = 0; j < similTable[i].size(); j++)
{
file << similTable[i][j] << "|";
}
file << "\n";
}
}
}
sql::finalize(get_gen_stmt);
}
void Vapp::drawToFile()
{
int64_t id = ids[selected_id_index];
char buff[50];
sprintf(buff, "%ld_%d.png", id, selected_gen);
Image image = LoadImageFromTexture(bigTexture.texture);
ExportImage(image, buff);
UnloadImage(image);
}

View File

@@ -2,18 +2,24 @@
#include <imgui.h>
#include <rlImGui.h>
#include "Vapp.hpp"
#include <cstdio>
int main(int argc, char *argv[])
{
int screenWidth = 1000;
int screenHeight = 1000;
if(argc != 2){
printf("MISING DB FILE\n");
return 0;
}
Vapp app;
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(screenWidth, screenHeight, "VIEW");
SetTargetFPS(60);
rlImGuiSetup(true);
app.init();
app.init(argv[1]);
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
while (!WindowShouldClose())