Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_geo_attribute_math.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efa09215b45aac76b23d2a555eddd309703bf1f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "BLI_task.hh"

#include "RNA_enum_types.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "NOD_math_functions.hh"

#include "node_geometry_util.hh"

namespace blender::nodes {

static void geo_node_attribute_math_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>("Geometry");
  b.add_input<decl::String>("A");
  b.add_input<decl::Float>("A", "A_001");
  b.add_input<decl::String>("B");
  b.add_input<decl::Float>("B", "B_001");
  b.add_input<decl::String>("C");
  b.add_input<decl::Float>("C", "C_001");
  b.add_input<decl::String>("Result");
  b.add_output<decl::Geometry>("Geometry");
}

static bool operation_use_input_c(const NodeMathOperation operation)
{
  return ELEM(operation,
              NODE_MATH_MULTIPLY_ADD,
              NODE_MATH_SMOOTH_MIN,
              NODE_MATH_SMOOTH_MAX,
              NODE_MATH_WRAP,
              NODE_MATH_COMPARE);
}

static bool operation_use_input_b(const NodeMathOperation operation)
{
  switch (operation) {
    case NODE_MATH_ADD:
    case NODE_MATH_SUBTRACT:
    case NODE_MATH_MULTIPLY:
    case NODE_MATH_DIVIDE:
    case NODE_MATH_POWER:
    case NODE_MATH_LOGARITHM:
    case NODE_MATH_MINIMUM:
    case NODE_MATH_MAXIMUM:
    case NODE_MATH_LESS_THAN:
    case NODE_MATH_GREATER_THAN:
    case NODE_MATH_MODULO:
    case NODE_MATH_ARCTAN2:
    case NODE_MATH_SNAP:
    case NODE_MATH_WRAP:
    case NODE_MATH_COMPARE:
    case NODE_MATH_MULTIPLY_ADD:
    case NODE_MATH_PINGPONG:
    case NODE_MATH_SMOOTH_MIN:
    case NODE_MATH_SMOOTH_MAX:
      return true;
    case NODE_MATH_SINE:
    case NODE_MATH_COSINE:
    case NODE_MATH_TANGENT:
    case NODE_MATH_ARCSINE:
    case NODE_MATH_ARCCOSINE:
    case NODE_MATH_ARCTANGENT:
    case NODE_MATH_ROUND:
    case NODE_MATH_ABSOLUTE:
    case NODE_MATH_FLOOR:
    case NODE_MATH_CEIL:
    case NODE_MATH_FRACTION:
    case NODE_MATH_SQRT:
    case NODE_MATH_INV_SQRT:
    case NODE_MATH_SIGN:
    case NODE_MATH_EXPONENT:
    case NODE_MATH_RADIANS:
    case NODE_MATH_DEGREES:
    case NODE_MATH_SINH:
    case NODE_MATH_COSH:
    case NODE_MATH_TANH:
    case NODE_MATH_TRUNC:
      return false;
  }
  BLI_assert(false);
  return false;
}

static void geo_node_attribute_math_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
  bNode *node = (bNode *)ptr->data;
  NodeAttributeMath *node_storage = (NodeAttributeMath *)node->storage;
  NodeMathOperation operation = (NodeMathOperation)node_storage->operation;

  uiItemR(layout, ptr, "operation", 0, "", ICON_NONE);

  uiLayoutSetPropSep(layout, true);
  uiLayoutSetPropDecorate(layout, false);
  uiItemR(layout, ptr, "input_type_a", 0, IFACE_("A"), ICON_NONE);
  if (operation_use_input_b(operation)) {
    uiItemR(layout, ptr, "input_type_b", 0, IFACE_("B"), ICON_NONE);
  }
  if (operation_use_input_c(operation)) {
    uiItemR(layout, ptr, "input_type_c", 0, IFACE_("C"), ICON_NONE);
  }
}

static void geo_node_attribute_math_init(bNodeTree *UNUSED(tree), bNode *node)
{
  NodeAttributeMath *data = (NodeAttributeMath *)MEM_callocN(sizeof(NodeAttributeMath), __func__);

  data->operation = NODE_MATH_ADD;
  data->input_type_a = GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE;
  data->input_type_b = GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE;
  data->input_type_c = GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE;
  node->storage = data;
}

static void geo_node_math_label(bNodeTree *UNUSED(ntree), bNode *node, char *label, int maxlen)
{
  NodeAttributeMath &node_storage = *(NodeAttributeMath *)node->storage;
  const char *name;
  bool enum_label = RNA_enum_name(rna_enum_node_math_items, node_storage.operation, &name);
  if (!enum_label) {
    name = "Unknown";
  }
  BLI_strncpy(label, IFACE_(name), maxlen);
}

static void geo_node_attribute_math_update(bNodeTree *UNUSED(ntree), bNode *node)
{
  NodeAttributeMath &node_storage = *(NodeAttributeMath *)node->storage;
  NodeMathOperation operation = static_cast<NodeMathOperation>(node_storage.operation);

  update_attribute_input_socket_availabilities(
      *node, "A", (GeometryNodeAttributeInputMode)node_storage.input_type_a);
  update_attribute_input_socket_availabilities(
      *node,
      "B",
      (GeometryNodeAttributeInputMode)node_storage.input_type_b,
      operation_use_input_b(operation));
  update_attribute_input_socket_availabilities(
      *node,
      "C",
      (GeometryNodeAttributeInputMode)node_storage.input_type_c,
      operation_use_input_c(operation));
}

static void do_math_operation(const VArray<float> &span_a,
                              const VArray<float> &span_b,
                              const VArray<float> &span_c,
                              MutableSpan<float> span_result,
                              const NodeMathOperation operation)
{
  bool success = try_dispatch_float_math_fl_fl_fl_to_fl(
      operation, [&](auto math_function, const FloatMathOperationInfo &UNUSED(info)) {
        threading::parallel_for(IndexRange(span_result.size()), 512, [&](IndexRange range) {
          for (const int i : range) {
            span_result[i] = math_function(span_a[i], span_b[i], span_c[i]);
          }
        });
      });
  BLI_assert(success);
  UNUSED_VARS_NDEBUG(success);
}

static void do_math_operation(const VArray<float> &span_a,
                              const VArray<float> &span_b,
                              MutableSpan<float> span_result,
                              const NodeMathOperation operation)
{
  bool success = try_dispatch_float_math_fl_fl_to_fl(
      operation, [&](auto math_function, const FloatMathOperationInfo &UNUSED(info)) {
        threading::parallel_for(IndexRange(span_result.size()), 1024, [&](IndexRange range) {
          for (const int i : range) {
            span_result[i] = math_function(span_a[i], span_b[i]);
          }
        });
      });
  BLI_assert(success);
  UNUSED_VARS_NDEBUG(success);
}

static void do_math_operation(const VArray<float> &span_input,
                              MutableSpan<float> span_result,
                              const NodeMathOperation operation)
{
  bool success = try_dispatch_float_math_fl_to_fl(
      operation, [&](auto math_function, const FloatMathOperationInfo &UNUSED(info)) {
        threading::parallel_for(IndexRange(span_result.size()), 1024, [&](IndexRange range) {
          for (const int i : range) {
            span_result[i] = math_function(span_input[i]);
          }
        });
      });
  BLI_assert(success);
  UNUSED_VARS_NDEBUG(success);
}

static AttributeDomain get_result_domain(const GeometryComponent &component,
                                         const GeoNodeExecParams &params,
                                         const NodeMathOperation operation,
                                         StringRef result_name)
{
  /* Use the domain of the result attribute if it already exists. */
  std::optional<AttributeMetaData> result_info = component.attribute_get_meta_data(result_name);
  if (result_info) {
    return result_info->domain;
  }

  /* Otherwise use the highest priority domain from existing input attributes, or the default. */
  const AttributeDomain default_domain = ATTR_DOMAIN_POINT;
  if (operation_use_input_b(operation)) {
    if (operation_use_input_c(operation)) {
      return params.get_highest_priority_input_domain({"A", "B", "C"}, component, default_domain);
    }
    return params.get_highest_priority_input_domain({"A", "B"}, component, default_domain);
  }
  return params.get_highest_priority_input_domain({"A"}, component, default_domain);
}

static void attribute_math_calc(GeometryComponent &component, const GeoNodeExecParams &params)
{
  const bNode &node = params.node();
  const NodeAttributeMath *node_storage = (const NodeAttributeMath *)node.storage;
  const NodeMathOperation operation = static_cast<NodeMathOperation>(node_storage->operation);
  const std::string result_name = params.get_input<std::string>("Result");

  /* The result type of this node is always float. */
  const AttributeDomain result_domain = get_result_domain(
      component, params, operation, result_name);

  OutputAttribute_Typed<float> attribute_result =
      component.attribute_try_get_for_output_only<float>(result_name, result_domain);
  if (!attribute_result) {
    return;
  }

  GVArray_Typed<float> attribute_a = params.get_input_attribute<float>(
      "A", component, result_domain, 0.0f);

  MutableSpan<float> result_span = attribute_result.as_span();

  /* Note that passing the data with `get_internal_span<float>()` works
   * because the attributes were accessed with #CD_PROP_FLOAT. */
  if (operation_use_input_b(operation)) {
    GVArray_Typed<float> attribute_b = params.get_input_attribute<float>(
        "B", component, result_domain, 0.0f);
    if (operation_use_input_c(operation)) {
      GVArray_Typed<float> attribute_c = params.get_input_attribute<float>(
          "C", component, result_domain, 0.0f);
      do_math_operation(attribute_a, attribute_b, attribute_c, result_span, operation);
    }
    else {
      do_math_operation(attribute_a, attribute_b, result_span, operation);
    }
  }
  else {
    do_math_operation(attribute_a, result_span, operation);
  }

  attribute_result.save();
}

static void geo_node_attribute_math_exec(GeoNodeExecParams params)
{
  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");

  geometry_set = geometry_set_realize_instances(geometry_set);

  if (geometry_set.has<MeshComponent>()) {
    attribute_math_calc(geometry_set.get_component_for_write<MeshComponent>(), params);
  }
  if (geometry_set.has<PointCloudComponent>()) {
    attribute_math_calc(geometry_set.get_component_for_write<PointCloudComponent>(), params);
  }
  if (geometry_set.has<CurveComponent>()) {
    attribute_math_calc(geometry_set.get_component_for_write<CurveComponent>(), params);
  }

  params.set_output("Geometry", geometry_set);
}

}  // namespace blender::nodes

void register_node_type_geo_attribute_math()
{
  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_LEGACY_ATTRIBUTE_MATH, "Attribute Math", NODE_CLASS_ATTRIBUTE, 0);
  ntype.declare = blender::nodes::geo_node_attribute_math_declare;
  ntype.geometry_node_execute = blender::nodes::geo_node_attribute_math_exec;
  ntype.draw_buttons = blender::nodes::geo_node_attribute_math_layout;
  node_type_label(&ntype, blender::nodes::geo_node_math_label);
  node_type_update(&ntype, blender::nodes::geo_node_attribute_math_update);
  node_type_init(&ntype, blender::nodes::geo_node_attribute_math_init);
  node_type_storage(
      &ntype, "NodeAttributeMath", node_free_standard_storage, node_copy_standard_storage);
  nodeRegisterType(&ntype);
}