Namespaced range_utility

This commit is contained in:
Marc Gilleron 2022-01-03 03:31:24 +00:00
parent 24e9e36434
commit 5bb4efa204
4 changed files with 54 additions and 36 deletions

View File

@ -42,7 +42,7 @@ void ImageRangeGrid::generate(Image &im) {
const int max_x = min(min_x + chunk_size, im.get_width());
const int max_y = min(min_y + chunk_size, im.get_height());
const Interval r = get_heightmap_range(im, Rect2i(min_x, min_y, max_x - min_x, max_y - min_y));
const Interval r = zylann::get_heightmap_range(im, Rect2i(min_x, min_y, max_x - min_x, max_y - min_y));
lod.data[cx + cy * lod.size_x] = r;
}

View File

@ -6,6 +6,8 @@
#include <modules/opensimplex/open_simplex_noise.h>
#include <scene/resources/curve.h>
namespace zylann {
// TODO We could skew max derivative estimation if the anchor is on a bump or a dip
// because in these cases, it becomes impossible for noise to go further up or further down
@ -259,6 +261,8 @@ Interval get_heightmap_range(Image &im, Rect2i rect) {
return Interval();
}
namespace math {
SdfAffectingArguments sdf_subtract_side(Interval a, Interval b) {
if (b.min > -a.min) {
return SDF_ONLY_A;
@ -376,6 +380,8 @@ Interval sdf_smooth_subtract(Interval p_b, Interval p_a, float p_s) {
p_b, p_a, p_s, [](float b, float a, float s) { return zylann::math::sdf_smooth_subtract(b, a, s); });
}
} // namespace math
static Interval get_fnl_cellular_value_range_2d(const FastNoiseLite *noise, Interval x, Interval y) {
const float c0 = noise->get_noise_2d(x.min, y.min);
const float c1 = noise->get_noise_2d(x.max, y.min);
@ -685,15 +691,17 @@ Interval get_fnl_range_3d(const FastNoiseLite *noise, Interval x, Interval y, In
}
}
Interval2 get_fnl_gradient_range_2d(const FastNoiseLiteGradient *noise, Interval x, Interval y) {
math::Interval2 get_fnl_gradient_range_2d(const FastNoiseLiteGradient *noise, Interval x, Interval y) {
// TODO More precise analysis
const float amp = Math::abs(noise->get_amplitude());
return Interval2{ Interval{ x.min - amp, x.max + amp }, Interval{ y.min - amp, y.max + amp } };
return math::Interval2{ Interval{ x.min - amp, x.max + amp }, Interval{ y.min - amp, y.max + amp } };
}
Interval3 get_fnl_gradient_range_3d(const FastNoiseLiteGradient *noise, Interval x, Interval y, Interval z) {
math::Interval3 get_fnl_gradient_range_3d(const FastNoiseLiteGradient *noise, Interval x, Interval y, Interval z) {
// TODO More precise analysis
const float amp = Math::abs(noise->get_amplitude());
return Interval3{ Interval{ x.min - amp, x.max + amp }, Interval{ y.min - amp, y.max + amp },
return math::Interval3{ Interval{ x.min - amp, x.max + amp }, Interval{ y.min - amp, y.max + amp },
Interval{ z.min - amp, z.max + amp } };
}
} // namespace zylann

View File

@ -11,6 +11,8 @@ class Image;
class FastNoiseLite;
class FastNoiseLiteGradient;
namespace zylann {
Interval get_osn_range_2d(OpenSimplexNoise *noise, Interval x, Interval y);
Interval get_osn_range_3d(OpenSimplexNoise *noise, Interval x, Interval y, Interval z);
@ -45,6 +47,8 @@ Interval get_curve_range(Curve &curve, bool &is_monotonic_increasing);
Interval get_heightmap_range(Image &im);
Interval get_heightmap_range(Image &im, Rect2i rect);
namespace math {
inline Interval sdf_union(Interval a, Interval b) {
return min_interval(a, b);
}
@ -59,7 +63,7 @@ Interval sdf_smooth_union(Interval p_b, Interval p_a, float p_s);
// Does b - a
Interval sdf_smooth_subtract(Interval p_b, Interval p_a, float p_s);
enum SdfAffectingArguments {
enum SdfAffectingArguments { //
SDF_ONLY_A,
SDF_ONLY_B,
SDF_BOTH
@ -85,9 +89,13 @@ struct Interval3 {
Interval z;
};
} // namespace math
Interval get_fnl_range_2d(const FastNoiseLite *noise, Interval x, Interval y);
Interval get_fnl_range_3d(const FastNoiseLite *noise, Interval x, Interval y, Interval z);
Interval2 get_fnl_gradient_range_2d(const FastNoiseLiteGradient *noise, Interval x, Interval y);
Interval3 get_fnl_gradient_range_3d(const FastNoiseLiteGradient *noise, Interval x, Interval y, Interval z);
math::Interval2 get_fnl_gradient_range_2d(const FastNoiseLiteGradient *noise, Interval x, Interval y);
math::Interval3 get_fnl_gradient_range_3d(const FastNoiseLiteGradient *noise, Interval x, Interval y, Interval z);
} // namespace zylann
#endif // RANGE_UTILITY_H

View File

@ -782,7 +782,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
struct Params {
// TODO Should be `const` but isn't because it auto-bakes, and it's a concern for multithreading
Curve *curve;
CurveRangeData *curve_range_data;
zylann::CurveRangeData *curve_range_data;
};
NodeType &t = types[VoxelGeneratorGraph::NODE_CURVE];
t.name = "Curve";
@ -799,7 +799,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
// Make sure it is baked. We don't want multithreading to bail out because of a write operation
// happening in `interpolate_baked`...
curve->bake();
CurveRangeData *curve_range_data = memnew(CurveRangeData);
zylann::CurveRangeData *curve_range_data = memnew(zylann::CurveRangeData);
get_curve_monotonic_sections(**curve, curve_range_data->sections);
Params p;
p.curve_range_data = curve_range_data;
@ -868,7 +868,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Interval x = ctx.get_input(0);
const Interval y = ctx.get_input(1);
const Params p = ctx.get_params<Params>();
ctx.set_output(0, get_osn_range_2d(p.noise, x, y));
ctx.set_output(0, zylann::get_osn_range_2d(p.noise, x, y));
};
}
{
@ -914,7 +914,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Interval y = ctx.get_input(1);
const Interval z = ctx.get_input(2);
const Params p = ctx.get_params<Params>();
ctx.set_output(0, get_osn_range_3d(p.noise, x, y, z));
ctx.set_output(0, zylann::get_osn_range_3d(p.noise, x, y, z));
};
}
{
@ -1118,38 +1118,39 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Params params = ctx.get_params<Params>();
if (params.smoothness > 0.0001f) {
const SdfAffectingArguments args = sdf_polynomial_smooth_union_side(a, b, params.smoothness);
const zylann::math::SdfAffectingArguments args =
zylann::math::sdf_polynomial_smooth_union_side(a, b, params.smoothness);
switch (args) {
case SDF_ONLY_A:
case zylann::math::SDF_ONLY_A:
ctx.ignore_input(0);
break;
case SDF_ONLY_B:
case zylann::math::SDF_ONLY_B:
ctx.ignore_input(1);
break;
case SDF_BOTH:
case zylann::math::SDF_BOTH:
break;
default:
CRASH_NOW();
break;
}
ctx.set_output(0, sdf_smooth_union(a, b, params.smoothness));
ctx.set_output(0, zylann::math::sdf_smooth_union(a, b, params.smoothness));
} else {
const SdfAffectingArguments args = sdf_union_side(a, b);
const zylann::math::SdfAffectingArguments args = zylann::math::sdf_union_side(a, b);
switch (args) {
case SDF_ONLY_A:
case zylann::math::SDF_ONLY_A:
ctx.ignore_input(0);
break;
case SDF_ONLY_B:
case zylann::math::SDF_ONLY_B:
ctx.ignore_input(1);
break;
case SDF_BOTH:
case zylann::math::SDF_BOTH:
break;
default:
CRASH_NOW();
break;
}
ctx.set_output(0, sdf_union(a, b));
ctx.set_output(0, zylann::math::sdf_union(a, b));
}
};
}
@ -1202,38 +1203,39 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Params params = ctx.get_params<Params>();
if (params.smoothness > 0.0001f) {
const SdfAffectingArguments args = sdf_polynomial_smooth_subtract_side(a, b, params.smoothness);
const zylann::math::SdfAffectingArguments args =
zylann::math::sdf_polynomial_smooth_subtract_side(a, b, params.smoothness);
switch (args) {
case SDF_ONLY_A:
case zylann::math::SDF_ONLY_A:
ctx.ignore_input(0);
break;
case SDF_ONLY_B:
case zylann::math::SDF_ONLY_B:
ctx.ignore_input(1);
break;
case SDF_BOTH:
case zylann::math::SDF_BOTH:
break;
default:
CRASH_NOW();
break;
}
ctx.set_output(0, sdf_smooth_subtract(a, b, params.smoothness));
ctx.set_output(0, zylann::math::sdf_smooth_subtract(a, b, params.smoothness));
} else {
const SdfAffectingArguments args = sdf_subtract_side(a, b);
const zylann::math::SdfAffectingArguments args = zylann::math::sdf_subtract_side(a, b);
switch (args) {
case SDF_ONLY_A:
case zylann::math::SDF_ONLY_A:
ctx.ignore_input(0);
break;
case SDF_ONLY_B:
case zylann::math::SDF_ONLY_B:
ctx.ignore_input(1);
break;
case SDF_BOTH:
case zylann::math::SDF_BOTH:
break;
default:
CRASH_NOW();
break;
}
ctx.set_output(0, sdf_subtract(a, b));
ctx.set_output(0, zylann::math::sdf_subtract(a, b));
}
};
}
@ -1443,7 +1445,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Interval x = ctx.get_input(0);
const Interval y = ctx.get_input(1);
const Params p = ctx.get_params<Params>();
ctx.set_output(0, get_fnl_range_2d(p.noise, x, y));
ctx.set_output(0, zylann::get_fnl_range_2d(p.noise, x, y));
};
}
{
@ -1488,7 +1490,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Interval y = ctx.get_input(1);
const Interval z = ctx.get_input(2);
const Params p = ctx.get_params<Params>();
ctx.set_output(0, get_fnl_range_3d(p.noise, x, y, z));
ctx.set_output(0, zylann::get_fnl_range_3d(p.noise, x, y, z));
};
}
{
@ -1536,7 +1538,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Interval x = ctx.get_input(0);
const Interval y = ctx.get_input(1);
const Params p = ctx.get_params<Params>();
const Interval2 r = get_fnl_gradient_range_2d(p.noise, x, y);
const zylann::math::Interval2 r = zylann::get_fnl_gradient_range_2d(p.noise, x, y);
ctx.set_output(0, r.x);
ctx.set_output(1, r.y);
};
@ -1593,7 +1595,7 @@ VoxelGraphNodeDB::VoxelGraphNodeDB() {
const Interval y = ctx.get_input(1);
const Interval z = ctx.get_input(2);
const Params p = ctx.get_params<Params>();
const Interval3 r = get_fnl_gradient_range_3d(p.noise, x, y, z);
const zylann::math::Interval3 r = zylann::get_fnl_gradient_range_3d(p.noise, x, y, z);
ctx.set_output(0, r.x);
ctx.set_output(1, r.y);
ctx.set_output(2, r.z);