Blender V5.0
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/* The float and vector3 overloads are already defined in stdosl.h.
38 *
39 * float round(float a)
40 * {
41 * return floor(a + 0.5);
42 * }
43 *
44 * vector3 round(vector3 a)
45 * {
46 * return vector3(floor(a.x + 0.5), floor(a.y + 0.5), floor(a.z + 0.5));
47 * } */
48
49vector2 round(vector2 a)
50{
51 return vector2(floor(a.x + 0.5), floor(a.y + 0.5));
52}
53
54vector4 round(vector4 a)
55{
56 return vector4(floor(a.x + 0.5), floor(a.y + 0.5), floor(a.z + 0.5), floor(a.w + 0.5));
57}
58
59float fract(float a)
60{
61 return a - floor(a);
62}
63
64/* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
65float smoothmin(float a, float b, float c)
66{
67 if (c != 0.0) {
68 float h = max(c - abs(a - b), 0.0) / c;
69 return min(a, b) - h * h * h * c * (1.0 / 6.0);
70 }
71 else {
72 return min(a, b);
73 }
74}
75
76float pingpong(float a, float b)
77{
78 return (b != 0.0) ? abs(fract((a - b) / (b * 2.0)) * b * 2.0 - b) : 0.0;
79}
80
81float safe_sqrt(float a)
82{
83 return (a > 0.0) ? sqrt(a) : 0.0;
84}
85
86float safe_log(float a, float b)
87{
88 return (a > 0.0 && b > 0.0) ? log(a) / log(b) : 0.0;
89}
90
92{
93 float lenSquared = dot(v_proj, v_proj);
94 return (lenSquared != 0.0) ? (dot(v, v_proj) / lenSquared) * v_proj : vector(0.0);
95}
96
98{
99 return floor(safe_divide(a, b)) * b;
100}
101
102/* Adapted from GODOT-engine math_funcs.h. */
103float wrap(float value, float max, float min)
104{
105 float range = max - min;
106 return (range != 0.0) ? value - (range * floor((value - min) / range)) : min;
107}
108
109point wrap(point value, point max, point min)
110{
111 return point(wrap(value[0], max[0], min[0]),
112 wrap(value[1], max[1], min[1]),
113 wrap(value[2], max[2], min[2]));
114}
115
116/* Built in OSL faceforward is `(dot(I, Nref) > 0) ? -N : N;` which is different to
117 * GLSL `dot(Nref, I) < 0 ? N : -N` for zero values. */
118point compatible_faceforward(point vec, point incident, point reference)
119{
120 return dot(reference, incident) < 0.0 ? vec : -vec;
121}
122
123matrix euler_to_mat(point euler)
124{
125 float cx = cos(euler[0]);
126 float cy = cos(euler[1]);
127 float cz = cos(euler[2]);
128 float sx = sin(euler[0]);
129 float sy = sin(euler[1]);
130 float sz = sin(euler[2]);
131 matrix mat = matrix(1.0);
132 mat[0][0] = cy * cz;
133 mat[0][1] = cy * sz;
134 mat[0][2] = -sy;
135 mat[1][0] = sy * sx * cz - cx * sz;
136 mat[1][1] = sy * sx * sz + cx * cz;
137 mat[1][2] = cy * sx;
138 +mat[2][0] = sy * cx * cz + sx * sz;
139 mat[2][1] = sy * cx * sz - sx * cz;
140 mat[2][2] = cy * cx;
141 return mat;
142}
143
144float average(point a)
145{
146 return (a[0] + a[1] + a[2]) * (1.0 / 3.0);
147}
ATTR_WARN_UNUSED_RESULT const BMVert * v
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define log
#define sin
#define round
#define cos
#define abs
#define floor
#define sqrt
ccl_device_inline float2 fmod(const float2 a, const float b)
vector snap(vector a, vector b)
Definition node_math.h:97
matrix euler_to_mat(point euler)
Definition node_math.h:123
float safe_floored_modulo(float a, float b)
Definition node_math.h:27
float fract(float a)
Definition node_math.h:59
float safe_divide(float a, float b)
Definition node_math.h:10
float wrap(float value, float max, float min)
Definition node_math.h:103
float average(point a)
Definition node_math.h:144
float safe_sqrt(float a)
Definition node_math.h:81
vector project(vector v, vector v_proj)
Definition node_math.h:91
float pingpong(float a, float b)
Definition node_math.h:76
float safe_log(float a, float b)
Definition node_math.h:86
float smoothmin(float a, float b, float c)
Definition node_math.h:65
point compatible_faceforward(point vec, point incident, point reference)
Definition node_math.h:118
float safe_modulo(float a, float b)
Definition node_math.h:22
#define sqr
#define min(a, b)
Definition sort.cc:36
max
Definition text_draw.cc:251