Parks-n-Rec/include/genetic/v3/program_v3.h

139 lines
3.6 KiB
C
Raw Normal View History

2023-07-18 21:59:41 -04:00
//
// Created by brett on 7/18/23.
//
#ifndef PARKSNREC_PROGRAM_V3_H
#define PARKSNREC_PROGRAM_V3_H
#include <genetic/v3/functions_v3.h>
2023-07-19 19:38:52 -04:00
#include "ImNodesEz.h"
2023-07-18 21:59:41 -04:00
namespace parks::genetic {
struct GeneticNode {
FunctionID op;
unsigned int pos{};
ParameterSet set;
2023-07-19 19:38:52 -04:00
GeneticNode(FunctionID op, unsigned int pos, ParameterSet set);
2023-07-18 21:59:41 -04:00
};
class GeneticTree {
private:
GeneticNode** nodes;
int size = 1;
void generateRandomTree();
Color execute_internal(double x, double y, int node);
public:
explicit GeneticTree(int max_height){
for (int i = 0; i < max_height; i++)
size *= 2;
nodes = new GeneticNode*[size];
for (int i = 0; i < size; i++)
nodes[i] = nullptr;
generateRandomTree();
}
Color execute(double x, double y);
static inline int left(int pos){
return 2 * pos;
}
2023-07-19 19:38:52 -04:00
2023-07-18 21:59:41 -04:00
static inline int right(int pos){
return 2 * pos + 1;
}
2023-07-19 19:38:52 -04:00
2023-07-18 21:59:41 -04:00
static inline int parent(int pos){
if (pos <= 0)
2023-07-19 19:38:52 -04:00
return -1;
//if (pos % 2 == 0)
return pos / 2;
//return (pos + 1) / 2;
2023-07-18 21:59:41 -04:00
}
2023-07-19 19:38:52 -04:00
inline GeneticNode* node(int pos){
2023-07-18 21:59:41 -04:00
if (pos < 0 || pos >= size)
return nullptr;
2023-07-19 19:38:52 -04:00
return nodes[pos];
2023-07-18 21:59:41 -04:00
}
2023-07-19 19:38:52 -04:00
inline GeneticNode* leftNode(int pos){
return node(left(pos));
}
2023-07-18 21:59:41 -04:00
inline GeneticNode* rightNode(int pos){
if (pos < 0 || pos >= size)
return nullptr;
2023-07-19 19:38:52 -04:00
return node(right(pos));
2023-07-18 21:59:41 -04:00
}
static int height(int node);
void deleteTree(){
for (int i = 0; i < size; i++) {
delete nodes[i];
nodes[i] = nullptr;
}
}
2023-07-19 19:38:52 -04:00
[[nodiscard]] inline int getSize() const {
return size;
}
2023-07-18 21:59:41 -04:00
~GeneticTree(){
deleteTree();
delete[] nodes;
}
};
class Program {
private:
2023-07-19 19:38:52 -04:00
struct ImNode_t
{
int height{};
int index{};
FunctionID id;
ImVec2 pos{};
bool selected{};
ImNodes::Ez::SlotInfo inputs[1]{};
ImNodes::Ez::SlotInfo outputs[2]{};
};
std::vector<ImNode_t> treeNodes;
2023-07-18 21:59:41 -04:00
unsigned char pixels[WIDTH * HEIGHT * CHANNELS];
2023-07-19 19:38:52 -04:00
GeneticTree* tree;
2023-07-18 21:59:41 -04:00
static size_t getPixelPosition(unsigned int x, unsigned int y){
return x * CHANNELS + y * WIDTH * CHANNELS;
}
2023-07-19 19:38:52 -04:00
void regenTreeDisplay();
float renderProgress;
2023-07-18 21:59:41 -04:00
public:
2023-07-19 19:38:52 -04:00
Program() = default;
2023-07-18 21:59:41 -04:00
void run();
2023-07-19 19:38:52 -04:00
void draw();
2023-07-18 21:59:41 -04:00
2023-07-19 19:38:52 -04:00
[[nodiscard]] float getRenderProgress() const{
return renderProgress;
2023-07-18 21:59:41 -04:00
}
inline unsigned char* getPixels(){
return pixels;
}
2023-07-19 19:38:52 -04:00
~Program(){
delete tree;
}
2023-07-18 21:59:41 -04:00
};
}
#endif //PARKSNREC_PROGRAM_V3_H