Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClment Foucault <fclem>2022-01-12 14:46:52 +0300
committerClément Foucault <foucault.clem@gmail.com>2022-01-12 14:47:43 +0300
commit46e049d0ce2bce2f53ddc41a0dbbea2969d00a5d (patch)
tree2b814f1b737ebe17364de1a5938deb9a7066d2a9 /source/blender/blenlib/intern/noise.cc
parente5766752d04794c2693dedad75baeb8c7d68f4cf (diff)
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
Diffstat (limited to 'source/blender/blenlib/intern/noise.cc')
-rw-r--r--source/blender/blenlib/intern/noise.cc79
1 files changed, 38 insertions, 41 deletions
diff --git a/source/blender/blenlib/intern/noise.cc b/source/blender/blenlib/intern/noise.cc
index a6ad18801fd..3460c1284fc 100644
--- a/source/blender/blenlib/intern/noise.cc
+++ b/source/blender/blenlib/intern/noise.cc
@@ -50,9 +50,7 @@
#include <cmath>
#include <cstdint>
-#include "BLI_float2.hh"
-#include "BLI_float3.hh"
-#include "BLI_float4.hh"
+#include "BLI_math_vec_types.hh"
#include "BLI_math_base_safe.h"
#include "BLI_noise.hh"
#include "BLI_utildefines.h"
@@ -1469,7 +1467,7 @@ void voronoi_smooth_f1(const float w,
correctionFactor /= 1.0f + 3.0f * smoothness;
if (r_color != nullptr) {
const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset);
- smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor;
+ smoothColor = math::interpolate(smoothColor, cellColor, h) - correctionFactor;
}
if (r_w != nullptr) {
smoothPosition = mix(smoothPosition, pointPosition, h) - correctionFactor;
@@ -1592,7 +1590,7 @@ static float voronoi_distance(const float2 a,
{
switch (metric) {
case NOISE_SHD_VORONOI_EUCLIDEAN:
- return float2::distance(a, b);
+ return math::distance(a, b);
case NOISE_SHD_VORONOI_MANHATTAN:
return fabsf(a.x - b.x) + fabsf(a.y - b.y);
case NOISE_SHD_VORONOI_CHEBYCHEV:
@@ -1615,7 +1613,7 @@ void voronoi_f1(const float2 coord,
float3 *r_color,
float2 *r_position)
{
- const float2 cellPosition = float2::floor(coord);
+ const float2 cellPosition = math::floor(coord);
const float2 localPosition = coord - cellPosition;
float minDistance = 8.0f;
@@ -1654,7 +1652,7 @@ void voronoi_smooth_f1(const float2 coord,
float3 *r_color,
float2 *r_position)
{
- const float2 cellPosition = float2::floor(coord);
+ const float2 cellPosition = math::floor(coord);
const float2 localPosition = coord - cellPosition;
const float smoothness_clamped = max_ff(smoothness, FLT_MIN);
@@ -1676,11 +1674,10 @@ void voronoi_smooth_f1(const float2 coord,
correctionFactor /= 1.0f + 3.0f * smoothness;
if (r_color != nullptr) {
const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset);
- smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor;
+ smoothColor = math::interpolate(smoothColor, cellColor, h) - correctionFactor;
}
if (r_position != nullptr) {
- smoothPosition = float2::interpolate(smoothPosition, pointPosition, h) -
- correctionFactor;
+ smoothPosition = math::interpolate(smoothPosition, pointPosition, h) - correctionFactor;
}
}
}
@@ -1704,7 +1701,7 @@ void voronoi_f2(const float2 coord,
float3 *r_color,
float2 *r_position)
{
- const float2 cellPosition = float2::floor(coord);
+ const float2 cellPosition = math::floor(coord);
const float2 localPosition = coord - cellPosition;
float distanceF1 = 8.0f;
@@ -1748,7 +1745,7 @@ void voronoi_f2(const float2 coord,
void voronoi_distance_to_edge(const float2 coord, const float randomness, float *r_distance)
{
- const float2 cellPosition = float2::floor(coord);
+ const float2 cellPosition = math::floor(coord);
const float2 localPosition = coord - cellPosition;
float2 vectorToClosest = float2(0.0f, 0.0f);
@@ -1777,7 +1774,7 @@ void voronoi_distance_to_edge(const float2 coord, const float randomness, float
const float2 perpendicularToEdge = vectorToPoint - vectorToClosest;
if (dot_v2v2(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
const float distanceToEdge = dot_v2v2((vectorToClosest + vectorToPoint) / 2.0f,
- perpendicularToEdge.normalized());
+ math::normalize(perpendicularToEdge));
minDistance = std::min(minDistance, distanceToEdge);
}
}
@@ -1787,7 +1784,7 @@ void voronoi_distance_to_edge(const float2 coord, const float randomness, float
void voronoi_n_sphere_radius(const float2 coord, const float randomness, float *r_radius)
{
- const float2 cellPosition = float2::floor(coord);
+ const float2 cellPosition = math::floor(coord);
const float2 localPosition = coord - cellPosition;
float2 closestPoint = float2(0.0f, 0.0f);
@@ -1798,7 +1795,7 @@ void voronoi_n_sphere_radius(const float2 coord, const float randomness, float *
const float2 cellOffset = float2(i, j);
const float2 pointPosition = cellOffset +
hash_float_to_float2(cellPosition + cellOffset) * randomness;
- const float distanceToPoint = float2::distance(pointPosition, localPosition);
+ const float distanceToPoint = math::distance(pointPosition, localPosition);
if (distanceToPoint < minDistance) {
minDistance = distanceToPoint;
closestPoint = pointPosition;
@@ -1817,14 +1814,14 @@ void voronoi_n_sphere_radius(const float2 coord, const float randomness, float *
const float2 cellOffset = float2(i, j) + closestPointOffset;
const float2 pointPosition = cellOffset +
hash_float_to_float2(cellPosition + cellOffset) * randomness;
- const float distanceToPoint = float2::distance(closestPoint, pointPosition);
+ const float distanceToPoint = math::distance(closestPoint, pointPosition);
if (distanceToPoint < minDistance) {
minDistance = distanceToPoint;
closestPointToClosestPoint = pointPosition;
}
}
}
- *r_radius = float2::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
+ *r_radius = math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
}
/* **** 3D Voronoi **** */
@@ -1836,7 +1833,7 @@ static float voronoi_distance(const float3 a,
{
switch (metric) {
case NOISE_SHD_VORONOI_EUCLIDEAN:
- return float3::distance(a, b);
+ return math::distance(a, b);
case NOISE_SHD_VORONOI_MANHATTAN:
return fabsf(a.x - b.x) + fabsf(a.y - b.y) + fabsf(a.z - b.z);
case NOISE_SHD_VORONOI_CHEBYCHEV:
@@ -1860,7 +1857,7 @@ void voronoi_f1(const float3 coord,
float3 *r_color,
float3 *r_position)
{
- const float3 cellPosition = float3::floor(coord);
+ const float3 cellPosition = math::floor(coord);
const float3 localPosition = coord - cellPosition;
float minDistance = 8.0f;
@@ -1902,7 +1899,7 @@ void voronoi_smooth_f1(const float3 coord,
float3 *r_color,
float3 *r_position)
{
- const float3 cellPosition = float3::floor(coord);
+ const float3 cellPosition = math::floor(coord);
const float3 localPosition = coord - cellPosition;
const float smoothness_clamped = max_ff(smoothness, FLT_MIN);
@@ -1925,10 +1922,10 @@ void voronoi_smooth_f1(const float3 coord,
correctionFactor /= 1.0f + 3.0f * smoothness;
if (r_color != nullptr) {
const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset);
- smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor;
+ smoothColor = math::interpolate(smoothColor, cellColor, h) - correctionFactor;
}
if (r_position != nullptr) {
- smoothPosition = float3::interpolate(smoothPosition, pointPosition, h) -
+ smoothPosition = math::interpolate(smoothPosition, pointPosition, h) -
correctionFactor;
}
}
@@ -1954,7 +1951,7 @@ void voronoi_f2(const float3 coord,
float3 *r_color,
float3 *r_position)
{
- const float3 cellPosition = float3::floor(coord);
+ const float3 cellPosition = math::floor(coord);
const float3 localPosition = coord - cellPosition;
float distanceF1 = 8.0f;
@@ -2000,7 +1997,7 @@ void voronoi_f2(const float3 coord,
void voronoi_distance_to_edge(const float3 coord, const float randomness, float *r_distance)
{
- const float3 cellPosition = float3::floor(coord);
+ const float3 cellPosition = math::floor(coord);
const float3 localPosition = coord - cellPosition;
float3 vectorToClosest = float3(0.0f, 0.0f, 0.0f);
@@ -2032,7 +2029,7 @@ void voronoi_distance_to_edge(const float3 coord, const float randomness, float
const float3 perpendicularToEdge = vectorToPoint - vectorToClosest;
if (dot_v3v3(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
const float distanceToEdge = dot_v3v3((vectorToClosest + vectorToPoint) / 2.0f,
- perpendicularToEdge.normalized());
+ math::normalize(perpendicularToEdge));
minDistance = std::min(minDistance, distanceToEdge);
}
}
@@ -2043,7 +2040,7 @@ void voronoi_distance_to_edge(const float3 coord, const float randomness, float
void voronoi_n_sphere_radius(const float3 coord, const float randomness, float *r_radius)
{
- const float3 cellPosition = float3::floor(coord);
+ const float3 cellPosition = math::floor(coord);
const float3 localPosition = coord - cellPosition;
float3 closestPoint = float3(0.0f, 0.0f, 0.0f);
@@ -2055,7 +2052,7 @@ void voronoi_n_sphere_radius(const float3 coord, const float randomness, float *
const float3 cellOffset = float3(i, j, k);
const float3 pointPosition = cellOffset +
hash_float_to_float3(cellPosition + cellOffset) * randomness;
- const float distanceToPoint = float3::distance(pointPosition, localPosition);
+ const float distanceToPoint = math::distance(pointPosition, localPosition);
if (distanceToPoint < minDistance) {
minDistance = distanceToPoint;
closestPoint = pointPosition;
@@ -2076,7 +2073,7 @@ void voronoi_n_sphere_radius(const float3 coord, const float randomness, float *
const float3 cellOffset = float3(i, j, k) + closestPointOffset;
const float3 pointPosition = cellOffset +
hash_float_to_float3(cellPosition + cellOffset) * randomness;
- const float distanceToPoint = float3::distance(closestPoint, pointPosition);
+ const float distanceToPoint = math::distance(closestPoint, pointPosition);
if (distanceToPoint < minDistance) {
minDistance = distanceToPoint;
closestPointToClosestPoint = pointPosition;
@@ -2084,7 +2081,7 @@ void voronoi_n_sphere_radius(const float3 coord, const float randomness, float *
}
}
}
- *r_radius = float3::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
+ *r_radius = math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
}
/* **** 4D Voronoi **** */
@@ -2096,7 +2093,7 @@ static float voronoi_distance(const float4 a,
{
switch (metric) {
case NOISE_SHD_VORONOI_EUCLIDEAN:
- return float4::distance(a, b);
+ return math::distance(a, b);
case NOISE_SHD_VORONOI_MANHATTAN:
return fabsf(a.x - b.x) + fabsf(a.y - b.y) + fabsf(a.z - b.z) + fabsf(a.w - b.w);
case NOISE_SHD_VORONOI_CHEBYCHEV:
@@ -2121,7 +2118,7 @@ void voronoi_f1(const float4 coord,
float3 *r_color,
float4 *r_position)
{
- const float4 cellPosition = float4::floor(coord);
+ const float4 cellPosition = math::floor(coord);
const float4 localPosition = coord - cellPosition;
float minDistance = 8.0f;
@@ -2166,7 +2163,7 @@ void voronoi_smooth_f1(const float4 coord,
float3 *r_color,
float4 *r_position)
{
- const float4 cellPosition = float4::floor(coord);
+ const float4 cellPosition = math::floor(coord);
const float4 localPosition = coord - cellPosition;
const float smoothness_clamped = max_ff(smoothness, FLT_MIN);
@@ -2191,10 +2188,10 @@ void voronoi_smooth_f1(const float4 coord,
correctionFactor /= 1.0f + 3.0f * smoothness;
if (r_color != nullptr) {
const float3 cellColor = hash_float_to_float3(cellPosition + cellOffset);
- smoothColor = float3::interpolate(smoothColor, cellColor, h) - correctionFactor;
+ smoothColor = math::interpolate(smoothColor, cellColor, h) - correctionFactor;
}
if (r_position != nullptr) {
- smoothPosition = float4::interpolate(smoothPosition, pointPosition, h) -
+ smoothPosition = math::interpolate(smoothPosition, pointPosition, h) -
correctionFactor;
}
}
@@ -2221,7 +2218,7 @@ void voronoi_f2(const float4 coord,
float3 *r_color,
float4 *r_position)
{
- const float4 cellPosition = float4::floor(coord);
+ const float4 cellPosition = math::floor(coord);
const float4 localPosition = coord - cellPosition;
float distanceF1 = 8.0f;
@@ -2270,7 +2267,7 @@ void voronoi_f2(const float4 coord,
void voronoi_distance_to_edge(const float4 coord, const float randomness, float *r_distance)
{
- const float4 cellPosition = float4::floor(coord);
+ const float4 cellPosition = math::floor(coord);
const float4 localPosition = coord - cellPosition;
float4 vectorToClosest = float4(0.0f, 0.0f, 0.0f, 0.0f);
@@ -2307,7 +2304,7 @@ void voronoi_distance_to_edge(const float4 coord, const float randomness, float
const float4 perpendicularToEdge = vectorToPoint - vectorToClosest;
if (dot_v4v4(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
const float distanceToEdge = dot_v4v4((vectorToClosest + vectorToPoint) / 2.0f,
- float4::normalize(perpendicularToEdge));
+ math::normalize(perpendicularToEdge));
minDistance = std::min(minDistance, distanceToEdge);
}
}
@@ -2319,7 +2316,7 @@ void voronoi_distance_to_edge(const float4 coord, const float randomness, float
void voronoi_n_sphere_radius(const float4 coord, const float randomness, float *r_radius)
{
- const float4 cellPosition = float4::floor(coord);
+ const float4 cellPosition = math::floor(coord);
const float4 localPosition = coord - cellPosition;
float4 closestPoint = float4(0.0f, 0.0f, 0.0f, 0.0f);
@@ -2333,7 +2330,7 @@ void voronoi_n_sphere_radius(const float4 coord, const float randomness, float *
const float4 pointPosition = cellOffset +
hash_float_to_float4(cellPosition + cellOffset) *
randomness;
- const float distanceToPoint = float4::distance(pointPosition, localPosition);
+ const float distanceToPoint = math::distance(pointPosition, localPosition);
if (distanceToPoint < minDistance) {
minDistance = distanceToPoint;
closestPoint = pointPosition;
@@ -2357,7 +2354,7 @@ void voronoi_n_sphere_radius(const float4 coord, const float randomness, float *
const float4 pointPosition = cellOffset +
hash_float_to_float4(cellPosition + cellOffset) *
randomness;
- const float distanceToPoint = float4::distance(closestPoint, pointPosition);
+ const float distanceToPoint = math::distance(closestPoint, pointPosition);
if (distanceToPoint < minDistance) {
minDistance = distanceToPoint;
closestPointToClosestPoint = pointPosition;
@@ -2366,7 +2363,7 @@ void voronoi_n_sphere_radius(const float4 coord, const float randomness, float *
}
}
}
- *r_radius = float4::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
+ *r_radius = math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
}
/** \} */