Blender V5.3
node_math.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "vector2.h"
6#include "vector4.h"
7
8#define vector3 point
9
10float safe_divide(float a, float b)
11{
12 return (b != 0.0) ? a / b : 0.0;
13}
14
16{
17 return vector((b[0] != 0.0) ? a[0] / b[0] : 0.0,
18 (b[1] != 0.0) ? a[1] / b[1] : 0.0,
19 (b[2] != 0.0) ? a[2] / b[2] : 0.0);
20}
21
22float safe_modulo(float a, float b)
23{
24 return (b != 0.0) ? fmod(a, b) : 0.0;
25}
26
27float safe_floored_modulo(float a, float b)
28{
29 return (b != 0.0) ? a - floor(a / b) * b : 0.0;
30}
31
32float sqr(float a)
33{
34 return a * a;
35}
36
37/* There are two common ways of implementing a linear interpolation: result = a + t * (b - a) and
38 * result = (1 - t) * a + t * b. The former variant is called "mix" in our code and it ensures that
39 * result always changes monotonically when t increases monotonically. This comes at the cost of
40 * the fact that generally result != b when t == 1, which becomes particularly noticeable when the
41 * magnitudes of a and b are vastly different. The latter variant is called
42 * "endvalue_preserving_mix" in our code ensures that result == b when t == 1. This comes at the
43 * cost of an additional multiplication step compared to the former version and the fact that
44 * result may not change monotonically when a and b have different signs and t increases
45 * monotonically, which however isn't noticeable in most cases as long as monotony isn't explicitly
46 * required. In general, "endvalue_preserving_mix" should be preferred over "mix" when it is
47 * important that result == b when t == 1 or when a and b may have vastly different magnitudes.
48 *
49 * The mix formula needs to be (1 - t) * a + t * b for consistency with SVM and EEVEE. However
50 * the OSL optimizer specifically optimizes mix() to skip evaluating one side when the factor is
51 * 0 or 1. Luckily it implements the same formula, so we can just use it. */
52float endvalue_preserving_mix(float a, float b, float t)
53{
54 return mix(a, b, t);
55}
56
57vector2 endvalue_preserving_mix(vector2 a, vector2 b, float t)
58{
59 return (1.0 - t) * a + t * b;
60}
61
62vector2 endvalue_preserving_mix(vector2 a, vector2 b, vector2 t)
63{
64 return (vector2(1.0, 1.0) - t) * a + t * b;
65}
66
68{
69 return mix(a, b, t);
70}
71
73{
74 return mix(a, b, t);
75}
76
77vector4 endvalue_preserving_mix(vector4 a, vector4 b, float t)
78{
79 return (1.0 - t) * a + t * b;
80}
81
82vector4 endvalue_preserving_mix(vector4 a, vector4 b, vector4 t)
83{
84 return (vector4(1.0, 1.0, 1.0, 1.0) - t) * a + t * b;
85}
86
87/* The float and vector3 overloads are already defined in stdosl.h.
88 *
89 * float round(float a)
90 * {
91 * return floor(a + 0.5);
92 * }
93 *
94 * vector3 round(vector3 a)
95 * {
96 * return vector3(floor(a.x + 0.5), floor(a.y + 0.5), floor(a.z + 0.5));
97 * } */
98
99vector2 round(vector2 a)
100{
101 return vector2(floor(a.x + 0.5), floor(a.y + 0.5));
102}
103
104vector4 round(vector4 a)
105{
106 return vector4(floor(a.x + 0.5), floor(a.y + 0.5), floor(a.z + 0.5), floor(a.w + 0.5));
107}
108
109float fract(float a)
110{
111 return a - floor(a);
112}
113
114/* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
115float smoothmin(float a, float b, float c)
116{
117 if (c != 0.0) {
118 float h = max(c - abs(a - b), 0.0) / c;
119 return min(a, b) - h * h * h * c * (1.0 / 6.0);
120 }
121 else {
122 return min(a, b);
123 }
124}
125
126float pingpong(float a, float b)
127{
128 return (b != 0.0) ? abs(fract((a - b) / (b * 2.0)) * b * 2.0 - b) : 0.0;
129}
130
131float safe_sqrt(float a)
132{
133 return (a > 0.0) ? sqrt(a) : 0.0;
134}
135
136float safe_log(float a, float b)
137{
138 return (a > 0.0 && b > 0.0) ? log(a) / log(b) : 0.0;
139}
140
142{
143 float lenSquared = dot(v_proj, v_proj);
144 return (lenSquared != 0.0) ? (dot(v, v_proj) / lenSquared) * v_proj : vector(0.0);
145}
146
148{
149 return floor(safe_divide(a, b)) * b;
150}
151
152/* Adapted from GODOT-engine math_funcs.h. */
153float wrap(float value, float max, float min)
154{
155 float range = max - min;
156 return (range != 0.0) ? value - (range * floor((value - min) / range)) : min;
157}
158
159point wrap(point value, point max, point min)
160{
161 return point(wrap(value[0], max[0], min[0]),
162 wrap(value[1], max[1], min[1]),
163 wrap(value[2], max[2], min[2]));
164}
165
166/* Built in OSL faceforward is `(dot(I, Nref) > 0) ? -N : N;` which is different to
167 * GLSL `dot(Nref, I) < 0 ? N : -N` for zero values. */
168point compatible_faceforward(point vec, point incident, point reference)
169{
170 return dot(reference, incident) < 0.0 ? vec : -vec;
171}
172
173matrix euler_to_mat(point euler)
174{
175 float cx = cos(euler[0]);
176 float cy = cos(euler[1]);
177 float cz = cos(euler[2]);
178 float sx = sin(euler[0]);
179 float sy = sin(euler[1]);
180 float sz = sin(euler[2]);
181 matrix mat = matrix(1.0);
182 mat[0][0] = cy * cz;
183 mat[0][1] = cy * sz;
184 mat[0][2] = -sy;
185 mat[1][0] = sy * sx * cz - cx * sz;
186 mat[1][1] = sy * sx * sz + cx * cz;
187 mat[1][2] = cy * sx;
188 +mat[2][0] = sy * cx * cz + sx * sz;
189 mat[2][1] = sy * cx * sz - sx * cz;
190 mat[2][2] = cy * cx;
191 return mat;
192}
193
194float average(point a)
195{
196 return (a[0] + a[1] + a[2]) * (1.0 / 3.0);
197}
btScalar max
#define log
#define sin
#define round
T fract(T) RET
#define cos
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define floor
#define sqrt
float endvalue_preserving_mix(float a, float b, float t)
float wrap(float a, float b, float c)
float safe_divide(float a, float b)
float safe_sqrt(float a)
float average(float2 a)
ccl_device_inline float2 fmod(const float2 a, const float b)
#define vector3
vector snap(vector a, vector b)
Definition node_math.h:147
matrix euler_to_mat(point euler)
Definition node_math.h:173
float safe_floored_modulo(float a, float b)
Definition node_math.h:27
vector project(vector v, vector v_proj)
Definition node_math.h:141
float pingpong(float a, float b)
Definition node_math.h:126
float safe_log(float a, float b)
Definition node_math.h:136
float smoothmin(float a, float b, float c)
Definition node_math.h:115
point compatible_faceforward(point vec, point incident, point reference)
Definition node_math.h:168
float safe_modulo(float a, float b)
Definition node_math.h:22
#define mix
#define sqr
#define min(a, b)
Definition sort.cc:38