Parks-n-Rec/include/genetic/old/operators.h

682 lines
31 KiB
C
Raw Normal View History

2023-07-12 21:38:34 -04:00
//
// Created by brett on 7/11/23.
//
#ifndef PARKSNREC_OPERATORS_H
#define PARKSNREC_OPERATORS_H
#include <blt/std/random.h>
#include <blt/std/time.h>
#include <cmath>
#include <stb/stb_perlin.h>
#include <random>
#include <utility>
namespace parks::genetic {
constexpr long seed = 213434;
double randomDouble(double min, double max) {
std::mt19937 rng(blt::system::getCurrentTimeNanoseconds());
static std::uniform_real_distribution<double> gen(0, 1);
return gen(rng) * (max - min) + min;
}
int randomInt(int min, int max) {
return (int)randomDouble(min, max);
}
2023-07-16 22:08:48 -04:00
bool chance(int bound = 2){
return randomInt(0, 100) <= bound;
}
2023-07-12 21:38:34 -04:00
struct Color {
Color(double r, double g, double b): r(r), g(g), b(b) {}
explicit Color(double v): Color(v,v,v){}
double r, g, b;
[[nodiscard]] double v() const {
return std::sqrt(r * r + g * g + b * b);
}
2023-07-16 22:08:48 -04:00
Color& normalize(){
return *this;
}
2023-07-12 21:38:34 -04:00
};
struct OperatorArguments {
double x, y;
long long time;
int arguments;
Color left, right;
};
2023-07-12 21:38:34 -04:00
class Operator {
public:
[[nodiscard]] inline virtual Color apply(const OperatorArguments& args) const = 0;
2023-07-16 22:08:48 -04:00
// returns modified
inline virtual Operator* mutate() = 0;
// returns new
[[nodiscard]] inline virtual Operator* breed(Operator* other) const = 0;
2023-07-12 21:38:34 -04:00
virtual ~Operator() = default;
};
2023-07-16 22:08:48 -04:00
template<typename T>
class OperatorBase : public Operator {
protected:
inline static double lerp(double x, double y) {
return (x + y) / 2;
}
public:
[[nodiscard]] inline T* create() const {
return new T;
}
[[nodiscard]] inline Operator* mutate() override {
return this;
}
[[nodiscard]] inline Operator* breed(Operator* other) const override {
return create();
}
[[nodiscard]] inline T* cast(Operator* op) const {
return dynamic_cast<T*>(op);
}
};
class RandomScalarOperator : public OperatorBase<RandomScalarOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
2023-07-16 22:08:48 -04:00
return Color(randomDouble(0, 1));
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class RandomColorOperator : public OperatorBase<RandomColorOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
2023-07-16 22:08:48 -04:00
return Color{randomDouble(0, 1),randomDouble(0, 1),randomDouble(0, 1)}.normalize();
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class XOperator : public OperatorBase<XOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
return Color(args.x);
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class YOperator : public OperatorBase<YOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
return Color(args.y);
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class MultiplicationOperator : public OperatorBase<MultiplicationOperator>{
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
return Color{0};
case 1:
2023-07-16 22:08:48 -04:00
return right.normalize();
case 2:
2023-07-16 22:08:48 -04:00
return left.normalize();
case 3:
2023-07-16 22:08:48 -04:00
return Color{left.r * right.r, left.g * right.g, left.b * right.b}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class AdditionOperator : public OperatorBase<AdditionOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
return Color{0};
case 1:
return right;
case 2:
return left;
case 3:
2023-07-16 22:08:48 -04:00
return Color{left.r + right.r, left.g + right.g, left.b + right.b}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class SubtractionOperator : public OperatorBase<SubtractionOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
return Color{0};
case 1:
2023-07-16 22:08:48 -04:00
return Color{-right.r, -right.g, -right.b}.normalize();
case 2:
2023-07-16 22:08:48 -04:00
return Color{-left.r, -left.g, -left.b}.normalize();
case 3:
2023-07-16 22:08:48 -04:00
return Color{left.r - right.r, left.g - right.g, left.b - right.b}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class ModOperator : public OperatorBase<ModOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
return Color{0};
case 1:
return right;
case 2:
return left;
case 3:
2023-07-16 22:08:48 -04:00
return Color{(double)((long)left.r % (long)std::max(1.0, right.r)),
(double)((long)left.g % (long)std::max(1.0, right.g)),
2023-07-16 22:08:48 -04:00
(double)((long)left.b % (long)std::max(1.0, right.b))}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class MinOperator : public OperatorBase<MinOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
2023-07-16 22:08:48 -04:00
return Color{std::min(left.r, right.r), std::min(left.g, right.g), std::min(left.b, right.b)}.normalize();
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class MaxOperator : public OperatorBase<MaxOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
2023-07-16 22:08:48 -04:00
return Color{std::max(left.r, right.r), std::max(left.g, right.g), std::max(left.b, right.b)}.normalize();
2023-07-12 21:38:34 -04:00
}
};
2023-07-16 22:08:48 -04:00
class LogOperator : public OperatorBase<LogOperator> {
2023-07-12 21:38:34 -04:00
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
return Color{0};
case 1:
2023-07-16 22:08:48 -04:00
return Color{std::log(right.r), std::log(right.g), std::log(right.b)}.normalize();
case 2:
2023-07-16 22:08:48 -04:00
return Color{std::log(left.r), std::log(left.g), std::log(left.b)}.normalize();
case 3:
2023-07-16 22:08:48 -04:00
return Color{std::log(left.r) + std::log(right.r), std::log(left.g) + std::log(right.g), std::log(left.b) + std::log(right.b)}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
};
constexpr double MIN = 0;
2023-07-16 22:08:48 -04:00
constexpr double SCALE_MIN = 1;
constexpr double MAX = 1;
constexpr double SCALE_MAX = 2048;
2023-07-16 22:08:48 -04:00
#define SCALE_OP /
2023-07-16 22:08:48 -04:00
class PerlinBWOperator : public OperatorBase<PerlinBWOperator> {
2023-07-12 21:38:34 -04:00
private:
double _y, _z;
float scale = 256.523;
public:
PerlinBWOperator() {
2023-07-12 21:38:34 -04:00
do {
_y = randomDouble(MIN, MAX);
} while (trunc(_y) == _y);
do {
_z = randomDouble(MIN, MAX);
} while (trunc(_z) == _z);
do {
2023-07-16 22:08:48 -04:00
scale = (float)randomDouble(SCALE_MIN, SCALE_MAX);
2023-07-12 21:38:34 -04:00
} while (trunc((double)scale) == (double)scale);
}
public:
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
2023-07-16 22:08:48 -04:00
return Color{stb_perlin_noise3((float)args.time SCALE_OP scale, (float)_y, (float)_z, 0, 0, 0)};
case 1:
return Color{
2023-07-16 22:08:48 -04:00
stb_perlin_noise3((float)right.v() SCALE_OP scale, (float)_y, (float)_z, 0, 0, 0)
}.normalize();
case 2:
return Color{
2023-07-16 22:08:48 -04:00
stb_perlin_noise3((float)left.v() SCALE_OP scale, (float)_y, (float)_z, 0, 0, 0)
}.normalize();
case 3:
return Color{
2023-07-16 22:08:48 -04:00
stb_perlin_noise3((float)left.v() SCALE_OP scale, (float)_y, (float)_z, 0, 0, 0)
+ stb_perlin_noise3((float)right.v() SCALE_OP scale, (float)_y, (float)_z, 0, 0, 0)
}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
2023-07-16 22:08:48 -04:00
[[nodiscard]] inline Operator* mutate() final {
if (chance())
_y += randomDouble(-MAX, MAX) / 2;
if (chance())
_z += randomDouble(-MAX, MAX) / 2;
if (chance())
scale += (float)randomDouble(-SCALE_MAX, SCALE_MAX) / 2;
return this;
}
[[nodiscard]] inline Operator* breed(Operator* other) const final {
auto parent = cast(other);
auto perlin = create();
perlin->_y = lerp(parent->_y, _y);
perlin->_z = lerp(parent->_z, _z);
perlin->scale = (float)lerp(parent->scale, scale);
return perlin;
}
2023-07-12 21:38:34 -04:00
};
2023-07-16 22:08:48 -04:00
class PerlinColorOperator : public OperatorBase<PerlinColorOperator> {
2023-07-12 21:38:34 -04:00
private:
double uniques[12]{};
float scale = 1;
2023-07-12 21:38:34 -04:00
public:
PerlinColorOperator() {
for (double& unique : uniques) {
do {
unique = randomDouble(MIN, MAX);
} while (trunc(unique) == unique);
}
2023-07-16 22:08:48 -04:00
scale = (float)randomDouble(SCALE_MIN, SCALE_MAX);
BLT_TRACE("Scale: %f", scale);
2023-07-12 21:38:34 -04:00
}
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
2023-07-16 22:08:48 -04:00
return Color{stb_perlin_noise3((float)args.time SCALE_OP scale, (float)uniques[0], (float)uniques[1], 0, 0, 0),
stb_perlin_noise3((float)uniques[2], (float)(float)args.time SCALE_OP scale, (float)uniques[3], 0, 0, 0),
stb_perlin_noise3((float)uniques[4], (float)uniques[5], (float)(float)args.time SCALE_OP scale, 0, 0, 0)}.normalize();
case 1:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_noise3((float)right.r SCALE_OP scale, (float)uniques[0], (float)uniques[1], 0, 0, 0),
stb_perlin_noise3((float)uniques[2], (float)right.g SCALE_OP scale, (float)uniques[3], 0, 0, 0),
stb_perlin_noise3((float)uniques[4], (float)uniques[5], (float)right.b SCALE_OP scale, 0, 0, 0)
}.normalize();
case 2:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_noise3((float)left.r SCALE_OP scale, (float)uniques[0], (float)uniques[1], 0, 0, 0),
stb_perlin_noise3((float)uniques[2], (float)left.g SCALE_OP scale, (float)uniques[3], 0, 0, 0),
stb_perlin_noise3((float)uniques[4], (float)uniques[5], (float)left.b SCALE_OP scale, 0, 0, 0)
}.normalize();
case 3:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_noise3((float)left.r SCALE_OP scale, (float)uniques[0], (float)uniques[1], 0, 0, 0)
+ stb_perlin_noise3((float)right.r SCALE_OP scale, (float)uniques[2], (float)uniques[3], 0, 0, 0),
stb_perlin_noise3((float)uniques[4], (float)left.g SCALE_OP scale, (float)uniques[5], 0, 0, 0)
+ stb_perlin_noise3((float)uniques[6], (float)right.g SCALE_OP scale, (float)uniques[7], 0, 0, 0),
stb_perlin_noise3((float)uniques[8], (float)uniques[9], (float)left.b SCALE_OP scale, 0, 0, 0)
+ stb_perlin_noise3((float)uniques[10], (float)uniques[11], (float)right.b SCALE_OP scale, 0, 0, 0)
}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
2023-07-16 22:08:48 -04:00
[[nodiscard]] inline Operator* breed(Operator* other) const final {
auto parent = cast(other);
auto perlin = create();
for (int i = 0; i < 12; i++)
perlin->uniques[i] = lerp(parent->uniques[i], uniques[i]);
perlin->scale = (float)lerp(parent->scale, scale);
return perlin;
}
[[nodiscard]] inline Operator* mutate() final {
for (auto& u : uniques)
if (chance())
u += randomDouble(-MAX, MAX) / 2;
if (chance())
scale += (float)randomDouble(-SCALE_MAX, SCALE_MAX) / 2;
return this;
}
2023-07-12 21:38:34 -04:00
};
2023-07-16 22:08:48 -04:00
class PerlinRidgeOperator : public OperatorBase<PerlinRidgeOperator> {
2023-07-12 21:38:34 -04:00
private:
double _y, _z;
int octaves;
float lacunarity = 2.0;
float gain = 0.5;
float offset = 1.0;
float scale = 256.523;
public:
PerlinRidgeOperator() {
2023-07-12 21:38:34 -04:00
do {
_y = randomDouble(MIN, MAX);
} while (trunc(_y) == _y);
do {
_z = randomDouble(MIN, MAX);
} while (trunc(_z) == _z);
do {
2023-07-16 22:08:48 -04:00
scale = (float)randomDouble(SCALE_MIN, SCALE_MAX);
2023-07-12 21:38:34 -04:00
} while (trunc((double)scale) == (double)scale);
octaves = randomInt(2, 12);
gain = (float)randomDouble(0.1, 1);
}
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_ridge_noise3((float)args.time SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)_y, (float)(float)args.time SCALE_OP scale, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)_z, (float)_y, (float)(float)args.time SCALE_OP scale, lacunarity, gain, offset, octaves)
}.normalize();
case 1:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_ridge_noise3((float)right.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)right.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)right.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves)
}.normalize();
case 2:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_ridge_noise3((float)left.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)left.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)left.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves)
}.normalize();
case 3:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_ridge_noise3((float)left.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves)
+ stb_perlin_ridge_noise3((float)right.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)left.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves)
+ stb_perlin_ridge_noise3((float)right.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves),
stb_perlin_ridge_noise3((float)left.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves)
+ stb_perlin_ridge_noise3((float)right.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, offset, octaves)
}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
2023-07-16 22:08:48 -04:00
[[nodiscard]] inline Operator* breed(Operator* other) const final {
auto parent = cast(other);
auto perlin = create();
perlin->_y = lerp(parent->_y, _y);
perlin->_z = lerp(parent->_z, _z);
perlin->octaves = (int)lerp(parent->octaves, octaves);
perlin->gain = (float)lerp(parent->gain, gain);
perlin->lacunarity = (float)lerp(parent->lacunarity, lacunarity);
perlin->offset = (float)lerp(parent->offset, offset);
perlin->scale = (float)lerp(parent->scale, scale);
return perlin;
}
[[nodiscard]] inline Operator* mutate() final {
if (chance())
_y += randomDouble(-MAX, MAX) / 2;
if (chance())
_z += randomDouble(-MAX, MAX) / 2;
if (chance())
scale += (float)randomDouble(-SCALE_MAX, SCALE_MAX) / 2;
if (chance())
octaves += randomInt(-2, 2);
octaves = std::max(2, octaves);
if (chance())
gain += (float)randomDouble(-1, 1)/2;
if (chance())
lacunarity += (float)randomDouble(-1, 1)/2;
if (chance())
offset += (float)randomDouble(-1, 1)/2;
return this;
}
2023-07-12 21:38:34 -04:00
};
2023-07-16 22:08:48 -04:00
class PerlinFBMOperator : public OperatorBase<PerlinFBMOperator> {
2023-07-12 21:38:34 -04:00
private:
double _y, _z;
int octaves;
float lacunarity = 2.0;
float gain = 0.5;
float scale = 256.523;
public:
PerlinFBMOperator() {
2023-07-12 21:38:34 -04:00
do {
_y = randomDouble(MIN, MAX);
} while (trunc(_y) == _y);
do {
_z = randomDouble(MIN, MAX);
} while (trunc(_z) == _z);
do {
2023-07-16 22:08:48 -04:00
scale = (float)randomDouble(SCALE_MIN, SCALE_MAX);
2023-07-12 21:38:34 -04:00
} while (trunc((double)scale) == (double)scale);
octaves = randomInt(2, 12);
gain = (float)randomDouble(0.1, 1);
}
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_fbm_noise3((float)args.time SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)_y, (float)(float)args.time SCALE_OP scale, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)_z, (float)_y, (float)(float)args.time SCALE_OP scale, lacunarity, gain, octaves)
}.normalize();
case 1:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_fbm_noise3((float)right.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)right.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)right.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
}.normalize();
case 2:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_fbm_noise3((float)left.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)left.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)left.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
}.normalize();
case 3:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_fbm_noise3((float)left.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
+ stb_perlin_fbm_noise3((float)right.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)left.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
+ stb_perlin_fbm_noise3((float)right.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_fbm_noise3((float)left.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
+ stb_perlin_fbm_noise3((float)right.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
2023-07-16 22:08:48 -04:00
[[nodiscard]] inline Operator* breed(Operator* other) const final {
auto parent = cast(other);
auto perlin = create();
perlin->_y = lerp(parent->_y, _y);
perlin->_z = lerp(parent->_z, _z);
perlin->octaves = (int)lerp(parent->octaves, octaves);
perlin->gain = (float)lerp(parent->gain, gain);
perlin->lacunarity = (float)lerp(parent->lacunarity, lacunarity);
perlin->scale = (float)lerp(parent->scale, scale);
return perlin;
}
[[nodiscard]] inline Operator* mutate() final {
if (chance())
_y += randomDouble(-MAX, MAX) / 2;
if (chance())
_z += randomDouble(-MAX, MAX) / 2;
if (chance())
scale += (float)randomDouble(-SCALE_MAX, SCALE_MAX) / 2;
if (chance())
octaves += randomInt(-2, 2);
octaves = std::max(2, octaves);
if (chance())
gain += (float)randomDouble(-1, 1)/2;
if (chance())
lacunarity += (float)randomDouble(-1, 1)/2;
return this;
}
2023-07-12 21:38:34 -04:00
};
2023-07-16 22:08:48 -04:00
class PerlinTurbulenceOperator : public OperatorBase<PerlinTurbulenceOperator> {
2023-07-12 21:38:34 -04:00
private:
double _y, _z;
int octaves;
float lacunarity = 2.0;
float gain = 0.5;
float scale = 256.523;
public:
PerlinTurbulenceOperator() {
2023-07-12 21:38:34 -04:00
do {
_y = randomDouble(MIN, MAX);
} while (trunc(_y) == _y);
do {
_z = randomDouble(MIN, MAX);
} while (trunc(_z) == _z);
do {
2023-07-16 22:08:48 -04:00
scale = (float)randomDouble(SCALE_MIN, SCALE_MAX);
2023-07-12 21:38:34 -04:00
} while (trunc((double)scale) == (double)scale);
octaves = randomInt(2, 12);
gain = (float)randomDouble(0.1, 1);
}
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
auto left = args.left;
auto right = args.right;
switch (args.arguments){
case 0:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_turbulence_noise3((float)args.time SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)_y, (float)(float)args.time SCALE_OP scale, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)_z, (float)_y, (float)(float)args.time SCALE_OP scale, lacunarity, gain, octaves)
}.normalize();
case 1:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_turbulence_noise3((float)right.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)right.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)right.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
}.normalize();
case 2:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_turbulence_noise3((float)left.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)left.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)left.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
}.normalize();
case 3:
2023-07-16 22:08:48 -04:00
return Color{
stb_perlin_turbulence_noise3((float)left.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
+ stb_perlin_turbulence_noise3((float)right.r SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)left.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
+ stb_perlin_turbulence_noise3((float)right.g SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves),
stb_perlin_turbulence_noise3((float)left.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
+ stb_perlin_turbulence_noise3((float)right.b SCALE_OP scale, (float)_y, (float)_z, lacunarity, gain, octaves)
}.normalize();
}
throw std::runtime_error("You shouldn't be able to reach here!");
2023-07-12 21:38:34 -04:00
}
2023-07-16 22:08:48 -04:00
[[nodiscard]] inline Operator* breed(Operator* other) const final {
auto parent = cast(other);
auto perlin = create();
perlin->_y = lerp(parent->_y, _y);
perlin->_z = lerp(parent->_z, _z);
perlin->octaves = (int)lerp(parent->octaves, octaves);
perlin->gain = (float)lerp(parent->gain, gain);
perlin->lacunarity = (float)lerp(parent->lacunarity, lacunarity);
perlin->scale = (float)lerp(parent->scale, scale);
return perlin;
}
[[nodiscard]] inline Operator* mutate() final {
if (chance())
_y += randomDouble(-MAX, MAX) / 2;
if (chance())
_z += randomDouble(-MAX, MAX) / 2;
if (chance())
scale += (float)randomDouble(-SCALE_MAX, SCALE_MAX) / 2;
if (chance())
octaves += randomInt(-2, 2);
octaves = std::max(2, octaves);
if (chance())
gain += (float)randomDouble(-1, 1)/2;
if (chance())
lacunarity += (float)randomDouble(-1, 1)/2;
return this;
}
2023-07-12 21:38:34 -04:00
};
2023-07-16 22:08:48 -04:00
class ColorNoiseOperator : public OperatorBase<ColorNoiseOperator> {
2023-07-12 21:38:34 -04:00
private:
double scale;
public:
ColorNoiseOperator() {
2023-07-12 21:38:34 -04:00
do {
scale = (float)randomDouble(1, 255);
} while (trunc((double)scale) == (double)scale);
}
[[nodiscard]] inline Color apply(const OperatorArguments& args) const final {
2023-07-16 22:08:48 -04:00
return Color{randomDouble(0, scale), randomDouble(0, scale), randomDouble(0, scale)}.normalize();
2023-07-12 21:38:34 -04:00
}
};
enum class Operators {
2023-07-16 22:08:48 -04:00
RandScalar,
RandColor,
X,
Y,
Multiplication,
Addition,
Subtraction,
Modulo,
Min,
Max,
Log,
PerlinBW,
PerlinColor,
PerlinRidge,
PerlinFBM,
PerlinTurbulence,
ColorNoise
};
struct OperatorProperties {
Operators index;
std::string opCode;
int acceptsInput; // 0000 00lr (bit mask) accepts l -> left subtree; r -> right subtree
};
const inline OperatorProperties operatorInfo[] = {
2023-07-16 22:08:48 -04:00
{Operators::RandScalar, "S", 0},
{Operators::RandColor, "C", 0},
{Operators::X, "X", 0},
{Operators::Y, "Y", 0},
{Operators::Multiplication, "*", 3},
{Operators::Addition, "+", 3},
{Operators::Subtraction, "-", 3},
{Operators::Modulo, "%", 3},
{Operators::Min, "Min", 3},
{Operators::Max, "Max", 3},
{Operators::Log, "Log", 3},
{Operators::PerlinBW, "PerlinBW", 3},
{Operators::PerlinColor, "PerlinColor", 3},
{Operators::PerlinRidge, "PerlinRidge", 3},
{Operators::PerlinFBM, "PerlinFBM", 3},
{Operators::PerlinTurbulence, "PerlinTurbulence", 3},
{Operators::ColorNoise, "ColorNoise", 0},
};
2023-07-12 21:38:34 -04:00
}
#endif //PARKSNREC_OPERATORS_H