Compare commits

..

10 Commits

Author SHA1 Message Date
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
26 changed files with 326 additions and 376 deletions

View File

@@ -26,7 +26,6 @@ add_executable(app
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
@@ -56,7 +55,6 @@ add_executable(view
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

View File

@@ -1,10 +1,3 @@
# how to build
```
g++ build.cpp -o build
./build
or
./build run
```
# Code style
@@ -31,10 +24,3 @@ or
- 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
```

View File

@@ -42,4 +42,6 @@ private:
Rectangle genTextBox;
Rectangle simTextBox;
float simil = 100.0f;
Color appColor = BLUE;
};

View File

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

View File

@@ -74,7 +74,7 @@ void App::init(int screenWidth, int screenHeight)
}
DnaStore::load(&manager);
simil = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
simil = Similarity::calc_similarity(manager.vector);
upTex(Liked::tbd);
while (!canvas.tick(canvasTexure[TOP]))
{
@@ -114,6 +114,10 @@ void App::init(int screenWidth, int screenHeight)
float recPosX = screenWidth * 0.3f;
disLikeBox = {0, posY, (float)recPosX, (float)screenWidth};
likeBox = {screenWidth - recPosX, posY, (float)recPosX, (float)screenWidth};
if(!DnaStore::ping()){
appColor = RED;
}
}
void App::upTex(Liked liked)
@@ -128,7 +132,7 @@ void App::upTex(Liked liked)
DnaStore::saveGen(&manager);
DnaManager::newGen(&manager);
DnaStore::saveVec(&manager);
simil = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
simil = Similarity::calc_similarity(manager.vector);
}
DnaStore::saveData(&manager);
}
@@ -141,7 +145,7 @@ void App::upTex(Liked liked)
BeginTextureMode(canvasTexure[TOP]);
ClearBackground(BLACK);
DrawText("NEXT GEN", 10, 10, 20, WHITE);
DrawText("NEXT GEN", 10, 10, screenWidth/10, WHITE);
EndTextureMode();
}
@@ -202,7 +206,7 @@ void App::update()
void App::draw()
{
ClearBackground(BLUE);
ClearBackground(appColor);
Rectangle source = {0, 0, (float)screenWidth, (float)-screenWidth};
Vector2 origin = {0.0f, 0.0f};

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 "petrovv.com"
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

@@ -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;
}

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

@@ -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

@@ -27,6 +27,7 @@ public:
private:
Dna *m_dna;
uint128 branchSeed;
//Texture2D texBunny;
Vector2 start = {0};
std::list<DrawArgs> drawCalls;

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
{

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

@@ -5,13 +5,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 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 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

@@ -2,7 +2,6 @@
#include "canvas/BackGround.hpp"
#include "canvas/BackGroundColors.hpp"
#include "canvas/Circle.hpp"
#include "canvas/stb_perlin.h"
#include <raylib.h>
@@ -44,6 +43,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 +54,6 @@ void BackGround::deinit()
void BackGround::draw(Dna *dna)
{
Circle::setSoftEdge(true);
m_dna = dna;
mountenSeed = dna->mountenSeed;
starSeed = dna->starSeed;
@@ -118,26 +119,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,11 +1,9 @@
#include "canvas/Canvas.hpp"
#include "canvas/Circle.hpp"
void Canvas::init(int size)
{
backGround.init(size);
tree.init(size);
Circle::init();
}
void Canvas::newGen(RenderTexture2D &target, Dna *dna)
@@ -30,5 +28,4 @@ bool Canvas::tick(RenderTexture2D &target)
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,7 +1,6 @@
#include <cmath>
#include "canvas/Tree.hpp"
#include "canvas/Circle.hpp"
#include <raylib.h>
#include <raymath.h>
@@ -41,12 +40,11 @@ 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);
m_dna = dna;
branchSeed = dna->branchSeed;
drawCalls.push_back({start, 180.0f, 0});
@@ -94,9 +92,8 @@ void Tree::drawBranch()
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
DrawCircleV(point, size, color);
//DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
// use
// DrawRectangleGradientEx

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);
}

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

@@ -7,50 +7,50 @@ namespace Similarity
// 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; };
// 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;
// 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);
// 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);
}
// return dot_prod / (mag1 * mag2);
// }
float hamming_distance(Dna *d1, Dna *d2)
{
@@ -67,6 +67,23 @@ 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));
}
float calc_similarity(std::vector<Dna> &vec, simil_func f)
{
size_t num_pairs = (vec.size() * (vec.size() - 1)) / 2;

View File

@@ -14,10 +14,12 @@ enum DrawingStage
done,
};
constexpr int numberOfFunc = 2;
class Vapp
{
public:
void init();
void init(char* filename);
void update();
void draw();
void deinit();
@@ -46,8 +48,8 @@ private:
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();
};

View File

@@ -11,7 +11,7 @@ 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 +20,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,9 +100,8 @@ 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[2] = Similarity::calc_similarity(manager.vector, Similarity::cosine_similarity_int);
simil[0] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance);
simil[1] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance_without_seeds);
stageOfDrawing = DrawingStage::done;
break;
@@ -174,12 +173,11 @@ 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("##sim3", "cosine_similarity_int: %f", simil[2]);
ImGui::LabelText("##sim1", "hamming_distance: %f", simil[0]);
ImGui::LabelText("##sim2", "hamming_distance_without_seeds: %f", simil[1]);
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))
{
for (int row = 0; row < similTable.size(); row++)
@@ -292,9 +290,8 @@ 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::hamming_distance);
similTable[s][1] = Similarity::calc_similarity(manager.vector, Similarity::hamming_distance_without_seeds);
DnaManager::newGen(&manager);
}

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())