Blender V5.3
gpu_shader_math_vector.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
9
10/* There are two common ways of implementing a linear interpolation: result = a + t * (b - a) and
11 * result = (1 - t) * a + t * b. The former variant is called "mix" in our code and it ensures that
12 * result always changes monotonically when t increases monotonically. This comes at the cost of
13 * the fact that generally result != b when t == 1, which becomes particularly noticeable when the
14 * magnitudes of a and b are vastly different. The latter variant is called
15 * "endvalue_preserving_mix" in our code ensures that result == b when t == 1. This comes at the
16 * cost of an additional multiplication step compared to the former version and the fact that
17 * result may not change monotonically when a and b have different signs and t increases
18 * monotonically, which however isn't noticeable in most cases as long as monotony isn't explicitly
19 * required. In general, "endvalue_preserving_mix" should be preferred over "mix" when it is
20 * important that result == b when t == 1 or when a and b may have vastly different magnitudes.*/
21template<typename VecT, typename FacT> VecT endvalue_preserving_mix(VecT a, VecT b, FacT t)
22{
23 return (FacT(1.0f) - t) * a + t * b;
24}
31
37template<typename VecT> VecT ceil_to_multiple(VecT a, VecT b)
38{
39 return ((a + b - VecT(1)) / b) * b;
40}
47
52template<typename VecT> VecT divide_ceil(VecT a, VecT b)
53{
54 return (a + b - VecT(1)) / b;
55}
62
66template<typename VecT> void min_max(VecT vector, VecT &min_v, VecT &max_v)
67{
68 min_v = min(vector, min_v);
69 max_v = max(vector, max_v);
70}
71template void min_max<float2>(float2, float2 &, float2 &);
72template void min_max<float3>(float3, float3 &, float3 &);
73template void min_max<float4>(float4, float4 &, float4 &);
74
79template<typename VecT> float length_manhattan(VecT a)
80{
81 return dot(abs(a), VecT(1));
82}
86
90template<typename VecT> float length_squared(VecT a)
91{
92 return dot(a, a);
93}
97
101template<typename VecT> float distance_manhattan(VecT a, VecT b)
102{
103 return length_manhattan(a - b);
104}
108
112template<typename VecT> float distance_squared(VecT a, VecT b)
113{
114 return length_squared(a - b);
115}
119
123template<typename VecT> float angle_normalized(VecT v1, VecT v2)
124{
125 /* Ensure inputs are normalized. */
126 v1 = normalize(v1);
127 v2 = normalize(v2);
128
129 /* This is the same as acos(dot_v3v3(v1, v2)), but more accurate. */
130 if (dot(v1, v2) >= 0.0f) {
131 return 2.0f * asin(clamp(length(v2 - v1) / 2.0f, -1.0f, 1.0f));
132 }
133
134 const VecT v2_n = -v2;
135 return M_PI - 2.0f * asin(clamp(length(v2_n - v1) / 2.0f, -1.0f, 1.0f));
136}
140
144template<typename VecT> VecT normalize_and_get_length(VecT vector, float &out_length)
145{
146 out_length = length_squared(vector);
147 constexpr float threshold = 1e-35f;
148 if (out_length > threshold) {
149 out_length = sqrt(out_length);
150 return vector / out_length;
151 }
152 /* Either the vector is small or one of its values contained `nan`. */
153 out_length = 0.0f;
154 return VecT(0.0f);
155}
159
163template<typename VecT> VecT interpolate(VecT a, VecT b, float t)
164{
165 return mix(a, b, t);
166}
170
174template<typename VecT> VecT midpoint(VecT a, VecT b)
175{
176 return (a + b) * 0.5f;
177}
181
186{
187 float3 b = abs(a);
188 return ((b.x > b.y) ? ((b.x > b.z) ? 0 : 2) : ((b.y > b.z) ? 1 : 2));
189}
190
195template<typename VecT> VecT orthogonal(VecT v)
196{
197 return VecT(-v.y, v.x);
198}
201
208{
209 switch (dominant_axis(v)) {
210 default:
211 case 0:
212 return float3(-v.y - v.z, v.x, v.x);
213 case 1:
214 return float3(v.y, -v.x - v.z, v.y);
215 case 2:
216 return float3(v.z, v.z, -v.x - v.y);
217 }
218}
219
223// float2 faceforward(float2 vector, float2 incident, float2 reference); /* Built-in GLSL. */
#define M_PI
btScalar max
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define sqrt
#define asin
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
void min_max(float value, float &min_v, float &max_v)
float endvalue_preserving_mix(float a, float b, float t)
int divide_ceil(int a, int b)
int ceil_to_multiple(int a, int b)
int midpoint(int a, int b)
Quaternion interpolate(Quaternion a, Quaternion b, float t)
float distance_squared(VecT a, VecT b)
float length_manhattan(VecT a)
VecT normalize_and_get_length(VecT vector, float &out_length)
float length_squared(VecT a)
VecT orthogonal(VecT v)
float angle_normalized(VecT v1, VecT v2)
int dominant_axis(float3 a)
float distance_manhattan(VecT a, VecT b)
#define mix
#define min(a, b)
Definition sort.cc:38