This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenlib/BLI_noise.hh

392 lines
14 KiB
C++
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
BLI: Refactor vector types & functions to use templates This patch implements the vector types (i.e:`float2`) by making heavy usage of templating. All vector functions are now outside of the vector classes (inside the `blender::math` namespace) and are not vector size dependent for the most part. In the ongoing effort to make shaders less GL centric, we are aiming to share more code between GLSL and C++ to avoid code duplication. ####Motivations: - We are aiming to share UBO and SSBO structures between GLSL and C++. This means we will use many of the existing vector types and others we currently don't have (uintX, intX). All these variations were asking for many more code duplication. - Deduplicate existing code which is duplicated for each vector size. - We also want to share small functions. Which means that vector functions should be static and not in the class namespace. - Reduce friction to use these types in new projects due to their incompleteness. - The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a bit of a let down. Most clases are incomplete, out of sync with each others with different codestyles, and some functions that should be static are not (i.e: `float3::reflect()`). ####Upsides: - Still support `.x, .y, .z, .w` for readability. - Compact, readable and easilly extendable. - All of the vector functions are available for all the vectors types and can be restricted to certain types. Also template specialization let us define exception for special class (like mpq). - With optimization ON, the compiler unroll the loops and performance is the same. ####Downsides: - Might impact debugability. Though I would arge that the bugs are rarelly caused by the vector class itself (since the operations are quite trivial) but by the type conversions. - Might impact compile time. I did not saw a significant impact since the usage is not really widespread. - Functions needs to be rewritten to support arbitrary vector length. For instance, one can't call `len_squared_v3v3` in `math::length_squared()` and call it a day. - Type cast does not work with the template version of the `math::` vector functions. Meaning you need to manually cast `float *` and `(float *)[3]` to `float3` for the function calls. i.e: `math::distance_squared(float3(nearest.co), positions[i]);` - Some parts might loose in readability: `float3::dot(v1.normalized(), v2.normalized())` becoming `math::dot(math::normalize(v1), math::normalize(v2))` But I propose, when appropriate, to use `using namespace blender::math;` on function local or file scope to increase readability. `dot(normalize(v1), normalize(v2))` ####Consideration: - Include back `.length()` method. It is quite handy and is more C++ oriented. - I considered the GLM library as a candidate for replacement. It felt like too much for what we need and would be difficult to extend / modify to our needs. - I used Macros to reduce code in operators declaration and potential copy paste bugs. This could reduce debugability and could be reverted. - This touches `delaunay_2d.cc` and the intersection code. I would like to know @howardt opinion on the matter. - The `noexcept` on the copy constructor of `mpq(2|3)` is being removed. But according to @JacquesLucke it is not a real problem for now. I would like to give a huge thanks to @JacquesLucke who helped during this and pushed me to reduce the duplication further. Reviewed By: brecht, sergey, JacquesLucke Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
#include "BLI_math_vec_types.hh"
namespace blender::noise {
2021-10-05 11:10:25 +11:00
/* -------------------------------------------------------------------- */
/** \name Hash Functions
*
* Create a randomized hash from the given inputs. Contrary to hash functions in `BLI_hash.hh`
* these functions produce better randomness but are more expensive to compute.
2021-10-05 11:10:25 +11:00
* \{ */
/* Hash integers to `uint32_t`. */
uint32_t hash(uint32_t kx);
uint32_t hash(uint32_t kx, uint32_t ky);
uint32_t hash(uint32_t kx, uint32_t ky, uint32_t kz);
uint32_t hash(uint32_t kx, uint32_t ky, uint32_t kz, uint32_t kw);
/* Hash floats to `uint32_t`. */
uint32_t hash_float(float kx);
uint32_t hash_float(float2 k);
uint32_t hash_float(float3 k);
uint32_t hash_float(float4 k);
/* Hash integers to `float` between 0 and 1. */
float hash_to_float(uint32_t kx);
float hash_to_float(uint32_t kx, uint32_t ky);
float hash_to_float(uint32_t kx, uint32_t ky, uint32_t kz);
float hash_to_float(uint32_t kx, uint32_t ky, uint32_t kz, uint32_t kw);
/* Hash floats to `float` between 0 and 1. */
float hash_float_to_float(float k);
float hash_float_to_float(float2 k);
float hash_float_to_float(float3 k);
float hash_float_to_float(float4 k);
float2 hash_float_to_float2(float2 k);
float3 hash_float_to_float3(float k);
float3 hash_float_to_float3(float2 k);
float3 hash_float_to_float3(float3 k);
float3 hash_float_to_float3(float4 k);
float4 hash_float_to_float4(float4 k);
2021-10-05 11:10:25 +11:00
/** \} */
/* -------------------------------------------------------------------- */
/** \name Perlin Noise
* \{ */
/* Perlin noise in the range [-1, 1]. */
float perlin_signed(float position);
float perlin_signed(float2 position);
float perlin_signed(float3 position);
float perlin_signed(float4 position);
/* Perlin noise in the range [0, 1]. */
float perlin(float position);
float perlin(float2 position);
float perlin(float3 position);
float perlin(float4 position);
/* Fractal perlin noise in the range [0, 1]. */
float perlin_fractal(float position, float octaves, float roughness);
float perlin_fractal(float2 position, float octaves, float roughness);
float perlin_fractal(float3 position, float octaves, float roughness);
float perlin_fractal(float4 position, float octaves, float roughness);
/* Positive distorted fractal perlin noise. */
float perlin_fractal_distorted(float position, float octaves, float roughness, float distortion);
float perlin_fractal_distorted(float2 position, float octaves, float roughness, float distortion);
float perlin_fractal_distorted(float3 position, float octaves, float roughness, float distortion);
float perlin_fractal_distorted(float4 position, float octaves, float roughness, float distortion);
/* Positive distorted fractal perlin noise that outputs a float3. */
float3 perlin_float3_fractal_distorted(float position,
float octaves,
float roughness,
float distortion);
float3 perlin_float3_fractal_distorted(float2 position,
float octaves,
float roughness,
float distortion);
float3 perlin_float3_fractal_distorted(float3 position,
float octaves,
float roughness,
float distortion);
float3 perlin_float3_fractal_distorted(float4 position,
float octaves,
float roughness,
float distortion);
2021-10-05 11:10:25 +11:00
/** \} */
/* -------------------------------------------------------------------- */
/** \name Musgrave Multi Fractal
* \{ */
/**
* 1D Ridged Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_ridged_multi_fractal(
float co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 2D Ridged Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_ridged_multi_fractal(
const float2 co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 3D Ridged Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_ridged_multi_fractal(
const float3 co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 4D Ridged Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_ridged_multi_fractal(
const float4 co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 1D Hybrid Additive/Multiplicative Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hybrid_multi_fractal(
float co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 2D Hybrid Additive/Multiplicative Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hybrid_multi_fractal(
const float2 co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 3D Hybrid Additive/Multiplicative Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hybrid_multi_fractal(
const float3 co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 4D Hybrid Additive/Multiplicative Multi-fractal Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hybrid_multi_fractal(
const float4 co, float H, float lacunarity, float octaves, float offset, float gain);
/**
* 1D Musgrave fBm
*
* \param H: fractal increment parameter.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_fBm(float co, float H, float lacunarity, float octaves);
/**
* 2D Musgrave fBm
*
* \param H: fractal increment parameter.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_fBm(const float2 co, float H, float lacunarity, float octaves);
/**
* 3D Musgrave fBm
*
* \param H: fractal increment parameter.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_fBm(const float3 co, float H, float lacunarity, float octaves);
/**
* 4D Musgrave fBm
*
* \param H: fractal increment parameter.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_fBm(const float4 co, float H, float lacunarity, float octaves);
/**
* 1D Musgrave Multi-fractal
*
* \param H: highest fractal dimension.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_multi_fractal(float co, float H, float lacunarity, float octaves);
/**
* 2D Musgrave Multi-fractal
*
* \param H: highest fractal dimension.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_multi_fractal(const float2 co, float H, float lacunarity, float octaves);
/**
* 3D Musgrave Multi-fractal
*
* \param H: highest fractal dimension.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_multi_fractal(const float3 co, float H, float lacunarity, float octaves);
/**
* 4D Musgrave Multi-fractal
*
* \param H: highest fractal dimension.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
*/
float musgrave_multi_fractal(const float4 co, float H, float lacunarity, float octaves);
/**
* 1D Musgrave Heterogeneous Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hetero_terrain(float co, float H, float lacunarity, float octaves, float offset);
/**
* 2D Musgrave Heterogeneous Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hetero_terrain(
const float2 co, float H, float lacunarity, float octaves, float offset);
/**
* 3D Musgrave Heterogeneous Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hetero_terrain(
const float3 co, float H, float lacunarity, float octaves, float offset);
/**
* 4D Musgrave Heterogeneous Terrain
*
* \param H: fractal dimension of the roughest area.
* \param lacunarity: gap between successive frequencies.
* \param octaves: number of frequencies in the fBm.
* \param offset: raises the terrain from `sea level'.
*/
float musgrave_hetero_terrain(
const float4 co, float H, float lacunarity, float octaves, float offset);
/** \} */
/* -------------------------------------------------------------------- */
/** \name Voronoi Noise
* \{ */
void voronoi_f1(float w, float randomness, float *r_distance, float3 *r_color, float *r_w);
void voronoi_smooth_f1(
float w, float smoothness, float randomness, float *r_distance, float3 *r_color, float *r_w);
void voronoi_f2(float w, float randomness, float *r_distance, float3 *r_color, float *r_w);
void voronoi_distance_to_edge(float w, float randomness, float *r_distance);
void voronoi_n_sphere_radius(float w, float randomness, float *r_radius);
void voronoi_f1(const float2 coord,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float2 *r_position);
void voronoi_smooth_f1(const float2 coord,
float smoothness,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float2 *r_position);
void voronoi_f2(const float2 coord,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float2 *r_position);
void voronoi_distance_to_edge(const float2 coord, float randomness, float *r_distance);
void voronoi_n_sphere_radius(const float2 coord, float randomness, float *r_radius);
void voronoi_f1(const float3 coord,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float3 *r_position);
void voronoi_smooth_f1(const float3 coord,
float smoothness,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float3 *r_position);
void voronoi_f2(const float3 coord,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float3 *r_position);
void voronoi_distance_to_edge(const float3 coord, float randomness, float *r_distance);
void voronoi_n_sphere_radius(const float3 coord, float randomness, float *r_radius);
void voronoi_f1(const float4 coord,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float4 *r_position);
void voronoi_smooth_f1(const float4 coord,
float smoothness,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float4 *r_position);
void voronoi_f2(const float4 coord,
float exponent,
float randomness,
int metric,
float *r_distance,
float3 *r_color,
float4 *r_position);
void voronoi_distance_to_edge(const float4 coord, float randomness, float *r_distance);
void voronoi_n_sphere_radius(const float4 coord, float randomness, float *r_radius);
/** \} */
} // namespace blender::noise