Blender  V2.93
type_conversions.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #include "NOD_type_conversions.hh"
18 
20 
21 #include "BLI_color.hh"
22 #include "BLI_float2.hh"
23 #include "BLI_float3.hh"
24 
25 namespace blender::nodes {
26 
27 using fn::MFDataType;
28 
29 template<typename From, typename To, To (*ConversionF)(const From &)>
31 {
32  const CPPType &from_type = CPPType::get<From>();
33  const CPPType &to_type = CPPType::get<To>();
34  const std::string conversion_name = from_type.name() + " to " + to_type.name();
35 
36  static fn::CustomMF_SI_SO<From, To> multi_function{conversion_name, ConversionF};
37  static auto convert_single_to_initialized = [](const void *src, void *dst) {
38  *(To *)dst = ConversionF(*(const From *)src);
39  };
40  static auto convert_single_to_uninitialized = [](const void *src, void *dst) {
41  new (dst) To(ConversionF(*(const From *)src));
42  };
43  conversions.add(fn::MFDataType::ForSingle<From>(),
44  fn::MFDataType::ForSingle<To>(),
45  multi_function,
46  convert_single_to_initialized,
47  convert_single_to_uninitialized);
48 }
49 
50 static float2 float_to_float2(const float &a)
51 {
52  return float2(a);
53 }
54 static float3 float_to_float3(const float &a)
55 {
56  return float3(a);
57 }
58 static int32_t float_to_int(const float &a)
59 {
60  return (int32_t)a;
61 }
62 static bool float_to_bool(const float &a)
63 {
64  return a > 0.0f;
65 }
66 static Color4f float_to_color(const float &a)
67 {
68  return Color4f(a, a, a, 1.0f);
69 }
70 
72 {
73  return float3(a.x, a.y, 0.0f);
74 }
75 static float float2_to_float(const float2 &a)
76 {
77  return (a.x + a.y) / 2.0f;
78 }
79 static int float2_to_int(const float2 &a)
80 {
81  return (int32_t)((a.x + a.y) / 2.0f);
82 }
83 static bool float2_to_bool(const float2 &a)
84 {
85  return !is_zero_v2(a);
86 }
88 {
89  return Color4f(a.x, a.y, 0.0f, 1.0f);
90 }
91 
92 static bool float3_to_bool(const float3 &a)
93 {
94  return !is_zero_v3(a);
95 }
96 static float float3_to_float(const float3 &a)
97 {
98  return (a.x + a.y + a.z) / 3.0f;
99 }
100 static int float3_to_int(const float3 &a)
101 {
102  return (int)((a.x + a.y + a.z) / 3.0f);
103 }
105 {
106  return float2(a);
107 }
109 {
110  return Color4f(a.x, a.y, a.z, 1.0f);
111 }
112 
113 static bool int_to_bool(const int32_t &a)
114 {
115  return a > 0;
116 }
117 static float int_to_float(const int32_t &a)
118 {
119  return (float)a;
120 }
122 {
123  return float2((float)a);
124 }
126 {
127  return float3((float)a);
128 }
130 {
131  return Color4f((float)a, (float)a, (float)a, 1.0f);
132 }
133 
134 static float bool_to_float(const bool &a)
135 {
136  return (bool)a;
137 }
138 static int32_t bool_to_int(const bool &a)
139 {
140  return (int32_t)a;
141 }
142 static float2 bool_to_float2(const bool &a)
143 {
144  return (a) ? float2(1.0f) : float2(0.0f);
145 }
146 static float3 bool_to_float3(const bool &a)
147 {
148  return (a) ? float3(1.0f) : float3(0.0f);
149 }
150 static Color4f bool_to_color(const bool &a)
151 {
152  return (a) ? Color4f(1.0f, 1.0f, 1.0f, 1.0f) : Color4f(0.0f, 0.0f, 0.0f, 1.0f);
153 }
154 
155 static bool color_to_bool(const Color4f &a)
156 {
157  return rgb_to_grayscale(a) > 0.0f;
158 }
159 static float color_to_float(const Color4f &a)
160 {
161  return rgb_to_grayscale(a);
162 }
164 {
165  return (int)rgb_to_grayscale(a);
166 }
168 {
169  return float2(a.r, a.g);
170 }
172 {
173  return float3(a.r, a.g, a.b);
174 }
175 
177 {
178  DataTypeConversions conversions;
179 
180  add_implicit_conversion<float, float2, float_to_float2>(conversions);
181  add_implicit_conversion<float, float3, float_to_float3>(conversions);
182  add_implicit_conversion<float, int32_t, float_to_int>(conversions);
183  add_implicit_conversion<float, bool, float_to_bool>(conversions);
184  add_implicit_conversion<float, Color4f, float_to_color>(conversions);
185 
186  add_implicit_conversion<float2, float3, float2_to_float3>(conversions);
187  add_implicit_conversion<float2, float, float2_to_float>(conversions);
188  add_implicit_conversion<float2, int32_t, float2_to_int>(conversions);
189  add_implicit_conversion<float2, bool, float2_to_bool>(conversions);
190  add_implicit_conversion<float2, Color4f, float2_to_color>(conversions);
191 
192  add_implicit_conversion<float3, bool, float3_to_bool>(conversions);
193  add_implicit_conversion<float3, float, float3_to_float>(conversions);
194  add_implicit_conversion<float3, int32_t, float3_to_int>(conversions);
195  add_implicit_conversion<float3, float2, float3_to_float2>(conversions);
196  add_implicit_conversion<float3, Color4f, float3_to_color>(conversions);
197 
198  add_implicit_conversion<int32_t, bool, int_to_bool>(conversions);
199  add_implicit_conversion<int32_t, float, int_to_float>(conversions);
200  add_implicit_conversion<int32_t, float2, int_to_float2>(conversions);
201  add_implicit_conversion<int32_t, float3, int_to_float3>(conversions);
202  add_implicit_conversion<int32_t, Color4f, int_to_color>(conversions);
203 
204  add_implicit_conversion<bool, float, bool_to_float>(conversions);
205  add_implicit_conversion<bool, int32_t, bool_to_int>(conversions);
206  add_implicit_conversion<bool, float2, bool_to_float2>(conversions);
207  add_implicit_conversion<bool, float3, bool_to_float3>(conversions);
208  add_implicit_conversion<bool, Color4f, bool_to_color>(conversions);
209 
210  add_implicit_conversion<Color4f, bool, color_to_bool>(conversions);
211  add_implicit_conversion<Color4f, float, color_to_float>(conversions);
212  add_implicit_conversion<Color4f, int32_t, color_to_int>(conversions);
213  add_implicit_conversion<Color4f, float2, color_to_float2>(conversions);
214  add_implicit_conversion<Color4f, float3, color_to_float3>(conversions);
215 
216  return conversions;
217 }
218 
220 {
221  static const DataTypeConversions conversions = create_implicit_conversions();
222  return conversions;
223 }
224 
226  const CPPType &to_type,
227  const void *from_value,
228  void *to_value) const
229 {
230  const ConversionFunctions *functions = this->get_conversion_functions(
231  MFDataType::ForSingle(from_type), MFDataType::ForSingle(to_type));
232  BLI_assert(functions != nullptr);
233 
234  functions->convert_single_to_uninitialized(from_value, to_value);
235 }
236 
237 } // namespace blender::nodes
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE float rgb_to_grayscale(const float rgb[3])
MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE bool is_zero_v2(const float a[3]) ATTR_WARN_UNUSED_RESULT
StringRefNull name() const
Definition: FN_cpp_type.hh:269
void convert_to_uninitialized(const CPPType &from_type, const CPPType &to_type, const void *from_value, void *to_value) const
const ConversionFunctions * get_conversion_functions(fn::MFDataType from, fn::MFDataType to) const
void add(fn::MFDataType from_type, fn::MFDataType to_type, const fn::MultiFunction &fn, void(*convert_single_to_initialized)(const void *src, void *dst), void(*convert_single_to_uninitialized)(const void *src, void *dst))
static unsigned a[3]
Definition: RandGen.cpp:92
static bool color_to_bool(const Color4f &a)
static float3 int_to_float3(const int32_t &a)
static float2 int_to_float2(const int32_t &a)
static float2 color_to_float2(const Color4f &a)
static float color_to_float(const Color4f &a)
static int32_t float_to_int(const float &a)
static float bool_to_float(const bool &a)
static int32_t bool_to_int(const bool &a)
static void add_implicit_conversion(DataTypeConversions &conversions)
static DataTypeConversions create_implicit_conversions()
static float3 color_to_float3(const Color4f &a)
static float3 float_to_float3(const float &a)
const DataTypeConversions & get_implicit_type_conversions()
static float2 bool_to_float2(const bool &a)
static float3 bool_to_float3(const bool &a)
static Color4f int_to_color(const int32_t &a)
static bool float3_to_bool(const float3 &a)
static Color4f bool_to_color(const bool &a)
static float2 float3_to_float2(const float3 &a)
static int float3_to_int(const float3 &a)
static int float2_to_int(const float2 &a)
static int32_t color_to_int(const Color4f &a)
static bool float2_to_bool(const float2 &a)
static bool int_to_bool(const int32_t &a)
static float2 float_to_float2(const float &a)
static Color4f float2_to_color(const float2 &a)
static Color4f float3_to_color(const float3 &a)
static float float3_to_float(const float3 &a)
static float int_to_float(const int32_t &a)
static float float2_to_float(const float2 &a)
static bool float_to_bool(const float &a)
static float3 float2_to_float3(const float2 &a)
static Color4f float_to_color(const float &a)
signed int int32_t
Definition: stdint.h:80
void(* convert_single_to_uninitialized)(const void *src, void *dst)