added function storage

main
Brett 2023-07-18 18:37:36 -04:00
parent d94bea7415
commit b41ab02c9d
4 changed files with 76 additions and 27 deletions

View File

@ -1,3 +1,3 @@
Start testing: Jul 17 21:33 EDT
Start testing: Jul 18 18:10 EDT
----------------------------------------------------------
End testing: Jul 17 21:33 EDT
End testing: Jul 18 18:10 EDT

View File

@ -33,7 +33,11 @@ namespace parks::genetic {
b = b - trunc(b);
}
explicit Color(double v): Color(v, v, v) {}
explicit Color(double v) {
r = v;
g = 0;
b = 0;
}
};
inline Color normalize(Color c) {
@ -99,7 +103,7 @@ namespace parks::genetic {
public:
ParameterSet() = default;
const inline Color& operator[](int index){return parameters[index];}
inline const Color& operator[](int index) const {return parameters[index];}
inline size_t size(){return parameters.size();}
void add(Color c) {parameters.push_back(c);}

View File

@ -54,7 +54,6 @@ namespace parks::genetic {
public:
Function(std::function<Color(OperatorArguments, ParameterSet)> func, unsigned int requiredScalars, unsigned int requiredColors, unsigned int acceptsArgs): func(std::move(func)), requiredScalars(requiredScalars), requiredColors(requiredColors), acceptsArgs(acceptsArgs) {
}
// in the case of single argument, it is provided to the left side!
[[nodiscard]] inline bool singleArgument() const {
return acceptsArgs & ARGS_SINGLE;
@ -103,22 +102,52 @@ namespace parks::genetic {
};
std::unordered_map<FunctionID, Function> functions = {
{FunctionID::ADD, Function{parks::genetic::add, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::SUBTRACT, Function{parks::genetic::subtract, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MULTIPLY, Function{parks::genetic::multiply, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::DIVIDE, Function{parks::genetic::divide, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MOD, Function{parks::genetic::mod, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::ROUND, Function{parks::genetic::round, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MIN, Function{parks::genetic::min, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MAX, Function{parks::genetic::max, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::ABS, Function{parks::genetic::abs, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::LOG, Function{parks::genetic::log, 0, 0, ARGS_SINGLE | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::SIN, Function{parks::genetic::sin, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::COS, Function{parks::genetic::cos, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::ATAN, Function{parks::genetic::atan, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::NOISE, Function{parks::genetic::noise, 3, 0, ARGS_NONE}},
{FunctionID::COLOR_NOISE, Function{parks::genetic::colorNoise, 3, 0, ARGS_NONE}},
class FunctionStorage {
private:
Function** functions;
size_t size = 0;
public:
FunctionStorage(std::initializer_list<std::pair<FunctionID, Function*>>&& init){
size_t max_value = init.size();
for (const auto& v : init){
int enumID = (int)v.first;
max_value = std::max(max_value, (size_t)enumID);
}
functions = new Function*[max_value];
for (auto& v : init){
functions[(int)v.first] = v.second;
}
size = max_value;
}
[[nodiscard]] const Function& operator[](FunctionID id) const {
return *functions[(int)id];
}
~FunctionStorage(){
for (size_t i = 0; i < size; i++)
delete functions[i];
delete[] functions;
}
};
FunctionStorage functions = {
{FunctionID::ADD, new Function{parks::genetic::add, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::SUBTRACT, new Function{parks::genetic::subtract, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MULTIPLY, new Function{parks::genetic::multiply, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::DIVIDE, new Function{parks::genetic::divide, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MOD, new Function{parks::genetic::mod, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::ROUND, new Function{parks::genetic::round, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MIN, new Function{parks::genetic::min, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::MAX, new Function{parks::genetic::max, 0, 0, ARGS_BOTH | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::ABS, new Function{parks::genetic::abs, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::LOG, new Function{parks::genetic::log, 0, 0, ARGS_SINGLE | ARGS_SCALARS | ARGS_COLORS | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::SIN, new Function{parks::genetic::sin, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::COS, new Function{parks::genetic::cos, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::ATAN, new Function{parks::genetic::atan, 0, 0, ARGS_SINGLE | ARGS_VARIABLES | ARGS_FUNCS}},
{FunctionID::NOISE, new Function{parks::genetic::noise, 5, 0, ARGS_BOTH | ARGS_VARIABLES}},
{FunctionID::COLOR_NOISE, new Function{parks::genetic::colorNoise, 5, 0, ARGS_BOTH | ARGS_VARIABLES}},
};
}

View File

@ -2,6 +2,7 @@
// Created by brett on 7/17/23.
//
#include <genetic/v3/v3_functions.h>
#include <stb/stb_perlin.h>
namespace parks::genetic {
@ -47,26 +48,41 @@ namespace parks::genetic {
}
Color log(OperatorArguments args, const ParameterSet& params) {
return Color(0, 0, 0);
return Color(std::log(args.left.r), std::log(args.left.g), std::log(args.left.b));
}
Color sin(OperatorArguments args, const ParameterSet& params) {
return Color(0, 0, 0);
return Color(std::sin(args.left.r), std::sin(args.left.g), std::sin(args.left.b));
}
Color cos(OperatorArguments args, const ParameterSet& params) {
return Color(0, 0, 0);
return Color(std::cos(args.left.r), std::cos(args.left.g), std::cos(args.left.b));
}
Color atan(OperatorArguments args, const ParameterSet& params) {
return Color(0, 0, 0);
return Color(std::atan(args.left.r), std::atan(args.left.g), std::atan(args.left.b));
}
const float lacunarity = 6;
const float octaves = 12;
const float gain = 2;
const float scale = 1024;
Color noise(OperatorArguments args, const ParameterSet& params) {
return Color(0, 0, 0);
float scaleX = (float)args.left.r * (float)params[3].r * scale;
float scaleY = (float)args.right.r * (float)params[3].r * scale;
return Color(stb_perlin_turbulence_noise3(scaleX, scaleY, 0.52342, (float)params[0].r * lacunarity, (float)params[1].r * gain, (int)std::max(2.0, params[2].r * octaves)));
}
Color colorNoise(OperatorArguments args, const ParameterSet& params) {
return Color(0, 0, 0);
float scaleX = (float)args.left.r * (float)params[3].r * scale;
float scaleY = (float)args.right.r * (float)params[3].r * scale;
float r = stb_perlin_turbulence_noise3(scaleX, scaleY, 0.52342, (float)params[0].r * lacunarity, (float)params[1].r * gain, (int)std::max(2.0, params[2].r * octaves));
float g = stb_perlin_turbulence_noise3(scaleX, 0.21045, scaleY, (float)params[0].r * lacunarity, (float)params[1].r * gain, (int)std::max(2.0, params[2].r * octaves));
float b = stb_perlin_turbulence_noise3(0.78423, scaleY, scaleX, (float)params[0].r * lacunarity, (float)params[1].r * gain, (int)std::max(2.0, params[2].r * octaves));
return Color(r, g, b);
}
}