Blender  V2.93
node_math.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2020 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 float safe_divide(float a, float b)
18 {
19  return (b != 0.0) ? a / b : 0.0;
20 }
21 
23 {
24  return vector((b[0] != 0.0) ? a[0] / b[0] : 0.0,
25  (b[1] != 0.0) ? a[1] / b[1] : 0.0,
26  (b[2] != 0.0) ? a[2] / b[2] : 0.0);
27 }
28 
29 float safe_modulo(float a, float b)
30 {
31  return (b != 0.0) ? fmod(a, b) : 0.0;
32 }
33 
34 float fract(float a)
35 {
36  return a - floor(a);
37 }
38 
39 /* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
40 float smoothmin(float a, float b, float c)
41 {
42  if (c != 0.0) {
43  float h = max(c - abs(a - b), 0.0) / c;
44  return min(a, b) - h * h * h * c * (1.0 / 6.0);
45  }
46  else {
47  return min(a, b);
48  }
49 }
50 
51 float pingpong(float a, float b)
52 {
53  return (b != 0.0) ? abs(fract((a - b) / (b * 2.0)) * b * 2.0 - b) : 0.0;
54 }
55 
56 float safe_sqrt(float a)
57 {
58  return (a > 0.0) ? sqrt(a) : 0.0;
59 }
60 
61 float safe_log(float a, float b)
62 {
63  return (a > 0.0 && b > 0.0) ? log(a) / log(b) : 0.0;
64 }
65 
67 {
68  float lenSquared = dot(v_proj, v_proj);
69  return (lenSquared != 0.0) ? (dot(v, v_proj) / lenSquared) * v_proj : vector(0.0);
70 }
71 
73 {
74  return floor(safe_divide(a, b)) * b;
75 }
76 
77 /* Adapted from GODOT-engine math_funcs.h. */
78 float wrap(float value, float max, float min)
79 {
80  float range = max - min;
81  return (range != 0.0) ? value - (range * floor((value - min) / range)) : min;
82 }
83 
84 point wrap(point value, point max, point min)
85 {
86  return point(wrap(value[0], max[0], min[0]),
87  wrap(value[1], max[1], min[1]),
88  wrap(value[2], max[2], min[2]));
89 }
90 
91 /* Built in OSL faceforward is `(dot(I, Nref) > 0) ? -N : N;` which is different to
92  * GLSL `dot(Nref, I) < 0 ? N : -N` for zero values. */
93 point compatible_faceforward(point vec, point incident, point reference)
94 {
95  return dot(reference, incident) < 0.0 ? vec : -vec;
96 }
97 
98 matrix euler_to_mat(point euler)
99 {
100  float cx = cos(euler[0]);
101  float cy = cos(euler[1]);
102  float cz = cos(euler[2]);
103  float sx = sin(euler[0]);
104  float sy = sin(euler[1]);
105  float sz = sin(euler[2]);
106  matrix mat = matrix(1.0);
107  mat[0][0] = cy * cz;
108  mat[0][1] = cy * sz;
109  mat[0][2] = -sy;
110  mat[1][0] = sy * sx * cz - cx * sz;
111  mat[1][1] = sy * sx * sz + cx * cz;
112  mat[1][2] = cy * sx;
113  +mat[2][0] = sy * cx * cz + sx * sz;
114  mat[2][1] = sy * cx * sz - sx * cz;
115  mat[2][2] = cy * cx;
116  return mat;
117 }
sqrt(x)+1/max(0
ATTR_WARN_UNUSED_RESULT const BMVert * v
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
INLINE Rall1d< T, V, S > log(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:303
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
Definition: vector.h:39
vector snap(vector a, vector b)
Definition: node_math.h:72
matrix euler_to_mat(point euler)
Definition: node_math.h:98
float fract(float a)
Definition: node_math.h:34
float safe_divide(float a, float b)
Definition: node_math.h:17
float wrap(float value, float max, float min)
Definition: node_math.h:78
float safe_sqrt(float a)
Definition: node_math.h:56
vector project(vector v, vector v_proj)
Definition: node_math.h:66
float pingpong(float a, float b)
Definition: node_math.h:51
float safe_log(float a, float b)
Definition: node_math.h:61
float smoothmin(float a, float b, float c)
Definition: node_math.h:40
point compatible_faceforward(point vec, point incident, point reference)
Definition: node_math.h:93
float safe_modulo(float a, float b)
Definition: node_math.h:29
#define min(a, b)
Definition: sort.c:51
float max
__forceinline const avxi abs(const avxi &a)
Definition: util_avxi.h:186
ccl_device_inline float dot(const float2 &a, const float2 &b)
ccl_device_inline float2 floor(const float2 &a)