From b41ab02c9d909e5832d6394383f07940492d6d2d Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 18 Jul 2023 18:37:36 -0400 Subject: [PATCH] added function storage --- .../Testing/Temporary/LastTest.log | 4 +- include/genetic/v3/arguments.h | 8 ++- include/genetic/v3/v3_functions.h | 63 ++++++++++++++----- src/genetic/v3/v3_functions.cpp | 28 +++++++-- 4 files changed, 76 insertions(+), 27 deletions(-) diff --git a/cmake-build-relwithdebinfo/Testing/Temporary/LastTest.log b/cmake-build-relwithdebinfo/Testing/Temporary/LastTest.log index 5bf1a9a..1b3141d 100644 --- a/cmake-build-relwithdebinfo/Testing/Temporary/LastTest.log +++ b/cmake-build-relwithdebinfo/Testing/Temporary/LastTest.log @@ -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 diff --git a/include/genetic/v3/arguments.h b/include/genetic/v3/arguments.h index a466260..3768eba 100644 --- a/include/genetic/v3/arguments.h +++ b/include/genetic/v3/arguments.h @@ -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);} diff --git a/include/genetic/v3/v3_functions.h b/include/genetic/v3/v3_functions.h index 7fda6e0..d3a5aa4 100644 --- a/include/genetic/v3/v3_functions.h +++ b/include/genetic/v3/v3_functions.h @@ -54,7 +54,6 @@ namespace parks::genetic { public: Function(std::function 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 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>&& 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}}, }; } diff --git a/src/genetic/v3/v3_functions.cpp b/src/genetic/v3/v3_functions.cpp index 07d36a9..af20154 100644 --- a/src/genetic/v3/v3_functions.cpp +++ b/src/genetic/v3/v3_functions.cpp @@ -2,6 +2,7 @@ // Created by brett on 7/17/23. // #include +#include 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); } } \ No newline at end of file