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

node_geo_legacy_attribute_map_range.cc « legacy « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 398af08749944354a24f41121653413d9947034f (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
 * 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_math_base_safe.h"
#include "BLI_task.hh"

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

#include "node_geometry_util.hh"

namespace blender::nodes::node_geo_legacy_attribute_map_range_cc {

static void node_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Geometry>(N_("Geometry"));
  b.add_input<decl::String>(N_("Attribute"));
  b.add_input<decl::String>(N_("Result"));
  b.add_input<decl::Float>(N_("From Min"));
  b.add_input<decl::Float>(N_("From Max")).default_value(1.0f);
  b.add_input<decl::Float>(N_("To Min"));
  b.add_input<decl::Float>(N_("To Max")).default_value(1.0f);
  b.add_input<decl::Float>(N_("Steps")).default_value(4.0f);
  b.add_input<decl::Vector>(N_("From Min"), "From Min_001");
  b.add_input<decl::Vector>(N_("From Max"), "From Max_001").default_value({1.0f, 1.0f, 1.0f});
  b.add_input<decl::Vector>(N_("To Min"), "To Min_001");
  b.add_input<decl::Vector>(N_("To Max"), "To Max_001").default_value({1.0f, 1.0f, 1.0f});
  b.add_input<decl::Vector>(N_("Steps"), "Steps_001").default_value({4.0f, 4.0f, 4.0f});
  b.add_input<decl::Bool>(N_("Clamp"));
  b.add_output<decl::Geometry>(N_("Geometry"));
}

static void fn_attribute_map_range_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
  uiItemR(layout, ptr, "data_type", 0, "", ICON_NONE);
  uiItemR(layout, ptr, "interpolation_type", 0, "", ICON_NONE);
}

static void node_init(bNodeTree *UNUSED(ntree), bNode *node)
{
  NodeAttributeMapRange *data = MEM_cnew<NodeAttributeMapRange>(__func__);
  data->data_type = CD_PROP_FLOAT;
  data->interpolation_type = NODE_MAP_RANGE_LINEAR;

  node->storage = data;
}
static void node_update(bNodeTree *ntree, bNode *node)
{
  NodeAttributeMapRange &node_storage = *(NodeAttributeMapRange *)node->storage;

  bNodeSocket *sock_from_min_float = (bNodeSocket *)BLI_findlink(&node->inputs, 3);
  bNodeSocket *sock_from_max_float = sock_from_min_float->next;
  bNodeSocket *sock_to_min_float = sock_from_max_float->next;
  bNodeSocket *sock_to_max_float = sock_to_min_float->next;
  bNodeSocket *sock_steps_float = sock_to_max_float->next;

  bNodeSocket *sock_from_min_vector = sock_steps_float->next;
  bNodeSocket *sock_from_max_vector = sock_from_min_vector->next;
  bNodeSocket *sock_to_min_vector = sock_from_max_vector->next;
  bNodeSocket *sock_to_max_vector = sock_to_min_vector->next;
  bNodeSocket *sock_steps_vector = sock_to_max_vector->next;

  bNodeSocket *sock_clamp = sock_steps_vector->next;

  const CustomDataType data_type = static_cast<CustomDataType>(node_storage.data_type);

  nodeSetSocketAvailability(ntree,
                            sock_clamp,
                            node_storage.interpolation_type == NODE_MAP_RANGE_LINEAR ||
                                node_storage.interpolation_type == NODE_MAP_RANGE_STEPPED);

  nodeSetSocketAvailability(ntree, sock_from_min_float, data_type == CD_PROP_FLOAT);
  nodeSetSocketAvailability(ntree, sock_from_max_float, data_type == CD_PROP_FLOAT);
  nodeSetSocketAvailability(ntree, sock_to_min_float, data_type == CD_PROP_FLOAT);
  nodeSetSocketAvailability(ntree, sock_to_max_float, data_type == CD_PROP_FLOAT);
  nodeSetSocketAvailability(ntree,
                            sock_steps_float,
                            data_type == CD_PROP_FLOAT &&
                                node_storage.interpolation_type == NODE_MAP_RANGE_STEPPED);

  nodeSetSocketAvailability(ntree, sock_from_min_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(ntree, sock_from_max_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(ntree, sock_to_min_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(ntree, sock_to_max_vector, data_type == CD_PROP_FLOAT3);
  nodeSetSocketAvailability(ntree,
                            sock_steps_vector,
                            data_type == CD_PROP_FLOAT3 &&
                                node_storage.interpolation_type == NODE_MAP_RANGE_STEPPED);
}

static float map_linear(const float value,
                        const float min_from,
                        const float max_from,
                        const float min_to,
                        const float max_to)
{
  /* First we calculate a fraction that measures how far along
   * the [min_from, max_from] interval the value lies.
   *
   *                value
   * min_from [------>|------------------------] max_from
   *               factor (e.g. 0.25)
   *
   * Then to find where the value is mapped, we add the same fraction
   * of the [min_to, max_to] interval to min_to.
   *
   * min_to [--->|-----------] max_to
   *             v
   *      min_to + (max_to - min_to) * factor
   */
  const float factor = safe_divide(value - min_from, max_from - min_from);
  return min_to + factor * (max_to - min_to);
}

static float map_stepped(const float value,
                         const float min_from,
                         const float max_from,
                         const float min_to,
                         const float max_to,
                         const float steps)
{
  /* First the factor is calculated here in the same way as for the linear mapping.
   *
   * Then the factor is mapped to multiples of 1.0 / steps.
   * This is best understood with a few examples. Assume steps == 3.
   * ____________________________________
   * | factor | * 4.0 | floor() | / 3.0 |
   * |--------|-------|---------|-------|
   * | 0.0    | 0.0   | 0.0     | 0.0   |
   * | 0.1    | 0.4   | 0.0     | 0.0   |
   * | 0.25   | 1.0   | 1.0     | 0.333 |
   * | 0.45   | 1.8   | 1.0     | 0.333 |
   * | 0.5    | 2.0   | 2.0     | 0.666 |
   * | 0.55   | 2.2   | 2.0     | 0.666 |
   * | 0.999  | 3.999 | 3.0     | 1.0   |
   * | 1.0    | 4.0   | 4.0     | 1.333 |
   * ------------------------------------
   * Note that the factor is not always mapped the closest multiple of 1.0 /steps.
   */
  const float factor = safe_divide(value - min_from, max_from - min_from);
  const float factor_mapped = safe_divide(floorf(factor * (steps + 1.0f)), steps);
  return min_to + factor_mapped * (max_to - min_to);
}

static float smoothstep_polynomial(float x)
{
  /* This polynomial is only meant to be used for the [0, 1] range. */
  return (3.0f - 2.0f * x) * (x * x);
}

static float map_smoothstep(const float value,
                            const float min_from,
                            const float max_from,
                            const float min_to,
                            const float max_to)
{
  const float factor = safe_divide(value - min_from, max_from - min_from);
  const float factor_clamped = std::clamp(factor, 0.0f, 1.0f);
  const float factor_mapped = smoothstep_polynomial(factor_clamped);
  return min_to + factor_mapped * (max_to - min_to);
}

static float smootherstep_polynomial(float x)
{
  /* This polynomial is only meant to be used for the [0, 1] range. */
  return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f);
}

static float map_smootherstep(const float value,
                              const float min_from,
                              const float max_from,
                              const float min_to,
                              const float max_to)
{
  const float factor = safe_divide(value - min_from, max_from - min_from);
  const float factor_clamped = std::clamp(factor, 0.0f, 1.0f);
  const float factor_mapped = smootherstep_polynomial(factor_clamped);
  return min_to + factor_mapped * (max_to - min_to);
}

static void map_range_float(const VArray<float> &attribute_input,
                            MutableSpan<float> results,
                            const GeoNodeExecParams &params)
{
  const bNode &node = params.node();
  NodeAttributeMapRange &node_storage = *(NodeAttributeMapRange *)node.storage;
  const int interpolation_type = node_storage.interpolation_type;
  const float min_from = params.get_input<float>("From Min");
  const float max_from = params.get_input<float>("From Max");
  const float min_to = params.get_input<float>("To Min");
  const float max_to = params.get_input<float>("To Max");

  VArray_Span<float> span{attribute_input};

  switch (interpolation_type) {
    case NODE_MAP_RANGE_LINEAR: {
      threading::parallel_for(span.index_range(), 2048, [&](IndexRange range) {
        for (const int i : range) {
          results[i] = map_linear(span[i], min_from, max_from, min_to, max_to);
        }
      });
      break;
    }
    case NODE_MAP_RANGE_STEPPED: {
      const float steps = params.get_input<float>("Steps");
      threading::parallel_for(span.index_range(), 1024, [&](IndexRange range) {
        for (const int i : range) {
          results[i] = map_stepped(span[i], min_from, max_from, min_to, max_to, steps);
        }
      });
      break;
    }
    case NODE_MAP_RANGE_SMOOTHSTEP: {
      threading::parallel_for(span.index_range(), 1024, [&](IndexRange range) {
        for (const int i : range) {
          results[i] = map_smoothstep(span[i], min_from, max_from, min_to, max_to);
        }
      });
      break;
    }
    case NODE_MAP_RANGE_SMOOTHERSTEP: {
      threading::parallel_for(span.index_range(), 1024, [&](IndexRange range) {
        for (const int i : range) {
          results[i] = map_smootherstep(span[i], min_from, max_from, min_to, max_to);
        }
      });
      break;
    }
  }

  if (ELEM(interpolation_type, NODE_MAP_RANGE_LINEAR, NODE_MAP_RANGE_STEPPED) &&
      params.get_input<bool>("Clamp")) {
    /* Users can specify min_to > max_to, but clamping expects min < max. */
    const float clamp_min = min_to < max_to ? min_to : max_to;
    const float clamp_max = min_to < max_to ? max_to : min_to;

    threading::parallel_for(results.index_range(), 2048, [&](IndexRange range) {
      for (const int i : range) {
        results[i] = std::clamp(results[i], clamp_min, clamp_max);
      }
    });
  }
}

static void map_range_float3(const VArray<float3> &attribute_input,
                             const MutableSpan<float3> results,
                             const GeoNodeExecParams &params)
{
  const bNode &node = params.node();
  NodeAttributeMapRange &node_storage = *(NodeAttributeMapRange *)node.storage;
  const int interpolation_type = node_storage.interpolation_type;
  const float3 min_from = params.get_input<float3>("From Min_001");
  const float3 max_from = params.get_input<float3>("From Max_001");
  const float3 min_to = params.get_input<float3>("To Min_001");
  const float3 max_to = params.get_input<float3>("To Max_001");

  VArray_Span<float3> span{attribute_input};

  switch (interpolation_type) {
    case NODE_MAP_RANGE_LINEAR: {
      threading::parallel_for(span.index_range(), 1024, [&](IndexRange range) {
        for (const int i : range) {
          results[i].x = map_linear(span[i].x, min_from.x, max_from.x, min_to.x, max_to.x);
          results[i].y = map_linear(span[i].y, min_from.y, max_from.y, min_to.y, max_to.y);
          results[i].z = map_linear(span[i].z, min_from.z, max_from.z, min_to.z, max_to.z);
        }
      });
      break;
    }
    case NODE_MAP_RANGE_STEPPED: {
      const float3 steps = params.get_input<float3>("Steps_001");
      threading::parallel_for(span.index_range(), 1024, [&](IndexRange range) {
        for (const int i : range) {
          results[i].x = map_stepped(
              span[i].x, min_from.x, max_from.x, min_to.x, max_to.x, steps.x);
          results[i].y = map_stepped(
              span[i].y, min_from.y, max_from.y, min_to.y, max_to.y, steps.y);
          results[i].z = map_stepped(
              span[i].z, min_from.z, max_from.z, min_to.z, max_to.z, steps.z);
        }
      });
      break;
    }
    case NODE_MAP_RANGE_SMOOTHSTEP: {
      threading::parallel_for(span.index_range(), 1024, [&](IndexRange range) {
        for (const int i : range) {
          results[i].x = map_smoothstep(span[i].x, min_from.x, max_from.x, min_to.x, max_to.x);
          results[i].y = map_smoothstep(span[i].y, min_from.y, max_from.y, min_to.y, max_to.y);
          results[i].z = map_smoothstep(span[i].z, min_from.z, max_from.z, min_to.z, max_to.z);
        }
      });
      break;
    }
    case NODE_MAP_RANGE_SMOOTHERSTEP: {
      threading::parallel_for(span.index_range(), 1024, [&](IndexRange range) {
        for (const int i : range) {
          results[i].x = map_smootherstep(span[i].x, min_from.x, max_from.x, min_to.x, max_to.x);
          results[i].y = map_smootherstep(span[i].y, min_from.y, max_from.y, min_to.y, max_to.y);
          results[i].z = map_smootherstep(span[i].z, min_from.z, max_from.z, min_to.z, max_to.z);
        }
      });
      break;
    }
  }

  if (ELEM(interpolation_type, NODE_MAP_RANGE_LINEAR, NODE_MAP_RANGE_STEPPED) &&
      params.get_input<bool>("Clamp")) {
    /* Users can specify min_to > max_to, but clamping expects min < max. */
    float3 clamp_min;
    float3 clamp_max;
    clamp_min.x = min_to.x < max_to.x ? min_to.x : max_to.x;
    clamp_max.x = min_to.x < max_to.x ? max_to.x : min_to.x;
    clamp_min.y = min_to.y < max_to.y ? min_to.y : max_to.y;
    clamp_max.y = min_to.y < max_to.y ? max_to.y : min_to.y;
    clamp_min.z = min_to.z < max_to.z ? min_to.z : max_to.z;
    clamp_max.z = min_to.z < max_to.z ? max_to.z : min_to.z;

    for (int i : results.index_range()) {
      clamp_v3_v3v3(results[i], clamp_min, clamp_max);
    }
  }
}

static AttributeDomain get_result_domain(const GeometryComponent &component,
                                         StringRef source_name,
                                         StringRef result_name)
{
  std::optional<AttributeMetaData> result_info = component.attribute_get_meta_data(result_name);
  if (result_info) {
    return result_info->domain;
  }
  std::optional<AttributeMetaData> source_info = component.attribute_get_meta_data(source_name);
  if (source_info) {
    return source_info->domain;
  }
  return ATTR_DOMAIN_POINT;
}

static void map_range_attribute(GeometryComponent &component, const GeoNodeExecParams &params)
{
  const std::string input_name = params.get_input<std::string>("Attribute");
  const std::string result_name = params.get_input<std::string>("Result");

  if (input_name.empty() || result_name.empty()) {
    return;
  }

  const bNode &node = params.node();
  NodeAttributeMapRange &node_storage = *(NodeAttributeMapRange *)node.storage;
  const CustomDataType data_type = static_cast<CustomDataType>(node_storage.data_type);

  const AttributeDomain domain = get_result_domain(component, input_name, result_name);

  GVArray attribute_input = component.attribute_try_get_for_read(input_name, domain, data_type);

  if (!attribute_input) {
    params.error_message_add(NodeWarningType::Error,
                             TIP_("No attribute with name \"") + input_name + "\"");
    return;
  }

  OutputAttribute attribute_result = component.attribute_try_get_for_output_only(
      result_name, domain, data_type);
  if (!attribute_result) {
    params.error_message_add(NodeWarningType::Error,
                             TIP_("Could not find or create attribute with name \"") +
                                 result_name + "\"");
    return;
  }

  switch (data_type) {
    case CD_PROP_FLOAT: {
      map_range_float(attribute_input.typed<float>(), attribute_result.as_span<float>(), params);
      break;
    }
    case CD_PROP_FLOAT3: {
      map_range_float3(
          attribute_input.typed<float3>(), attribute_result.as_span<float3>(), params);
      break;
    }
    default:
      BLI_assert_unreachable();
  }

  attribute_result.save();
}

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

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

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

}  // namespace blender::nodes::node_geo_legacy_attribute_map_range_cc

void register_node_type_geo_attribute_map_range()
{
  namespace file_ns = blender::nodes::node_geo_legacy_attribute_map_range_cc;

  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_LEGACY_ATTRIBUTE_MAP_RANGE, "Attribute Map Range", NODE_CLASS_ATTRIBUTE);
  ntype.geometry_node_execute = file_ns::node_geo_exec;
  node_type_init(&ntype, file_ns::node_init);
  node_type_update(&ntype, file_ns::node_update);
  node_type_storage(
      &ntype, "NodeAttributeMapRange", node_free_standard_storage, node_copy_standard_storage);
  ntype.declare = file_ns::node_declare;
  ntype.draw_buttons = file_ns::fn_attribute_map_range_layout;
  nodeRegisterType(&ntype);
}