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

node_geo_attribute_transfer.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c5d7891387d046c0434275323f60b3e95ecef6e (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
/*
 * 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 "UI_interface.h"
#include "UI_resources.h"

#include "BLI_kdopbvh.h"

#include "BKE_bvhutils.h"

#include "DNA_mesh_types.h"
#include "DNA_pointcloud_types.h"

#include "node_geometry_util.hh"

static bNodeSocketTemplate geo_node_attribute_transfer_in[] = {
    {SOCK_GEOMETRY, N_("Geometry")},
    {SOCK_GEOMETRY, N_("Target")},
    {SOCK_STRING, N_("Source")},
    {SOCK_STRING, N_("Destination")},
    {-1, ""},
};

static bNodeSocketTemplate geo_node_attribute_transfer_out[] = {
    {SOCK_GEOMETRY, N_("Geometry")},
    {-1, ""},
};

static void geo_node_attribute_transfer_layout(uiLayout *layout,
                                               bContext *UNUSED(C),
                                               PointerRNA *ptr)
{
  uiLayoutSetPropSep(layout, true);
  uiLayoutSetPropDecorate(layout, false);
  uiItemR(layout, ptr, "domain", 0, IFACE_("Domain"), ICON_NONE);
  uiItemR(layout, ptr, "mapping", 0, IFACE_("Mapping"), ICON_NONE);
}

namespace blender::nodes {

static void geo_node_attribute_transfer_init(bNodeTree *UNUSED(tree), bNode *node)
{
  NodeGeometryAttributeTransfer *data = (NodeGeometryAttributeTransfer *)MEM_callocN(
      sizeof(NodeGeometryAttributeTransfer), __func__);
  node->storage = data;
}

static CustomDataType get_result_data_type(const GeometrySet &geometry,
                                           const StringRef attribute_name)
{
  Vector<CustomDataType> data_types;

  const PointCloudComponent *pointcloud_component =
      geometry.get_component_for_read<PointCloudComponent>();
  if (pointcloud_component != nullptr) {
    ReadAttributeLookup attribute = pointcloud_component->attribute_try_get_for_read(
        attribute_name);
    if (attribute) {
      data_types.append(bke::cpp_type_to_custom_data_type(attribute.varray->type()));
    }
  }

  const MeshComponent *mesh_component = geometry.get_component_for_read<MeshComponent>();
  if (mesh_component != nullptr) {
    ReadAttributeLookup attribute = mesh_component->attribute_try_get_for_read(attribute_name);
    if (attribute) {
      data_types.append(bke::cpp_type_to_custom_data_type(attribute.varray->type()));
    }
  }
  return bke::attribute_data_type_highest_complexity(data_types);
}

static void get_closest_pointcloud_point_indices(const PointCloud &pointcloud,
                                                 const VArray<float3> &positions,
                                                 const MutableSpan<int> r_indices,
                                                 const MutableSpan<float> r_distances_sq)
{
  BLI_assert(positions.size() == r_indices.size());
  BLI_assert(pointcloud.totpoint > 0);

  BVHTreeFromPointCloud tree_data;
  BKE_bvhtree_from_pointcloud_get(&tree_data, &pointcloud, 2);

  for (const int i : positions.index_range()) {
    BVHTreeNearest nearest;
    nearest.dist_sq = FLT_MAX;
    const float3 position = positions[i];
    BLI_bvhtree_find_nearest(
        tree_data.tree, position, &nearest, tree_data.nearest_callback, &tree_data);
    r_indices[i] = nearest.index;
    r_distances_sq[i] = nearest.dist_sq;
  }

  free_bvhtree_from_pointcloud(&tree_data);
}

static void get_closest_mesh_point_indices(const Mesh &mesh,
                                           const VArray<float3> &positions,
                                           const MutableSpan<int> r_indices,
                                           const MutableSpan<float> r_distances_sq)
{
  BLI_assert(positions.size() == r_indices.size());
  BLI_assert(mesh.totvert > 0);

  BVHTreeFromMesh tree_data;
  BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_VERTS, 2);

  for (const int i : positions.index_range()) {
    BVHTreeNearest nearest;
    nearest.dist_sq = FLT_MAX;
    const float3 position = positions[i];
    BLI_bvhtree_find_nearest(
        tree_data.tree, position, &nearest, tree_data.nearest_callback, &tree_data);
    r_indices[i] = nearest.index;
    r_distances_sq[i] = nearest.dist_sq;
  }

  free_bvhtree_from_mesh(&tree_data);
}

static void get_closest_mesh_surface_samples(const Mesh &mesh,
                                             const VArray<float3> &positions,
                                             const MutableSpan<int> r_looptri_indices,
                                             const MutableSpan<float3> r_positions,
                                             const MutableSpan<float> r_distances_sq)
{
  BLI_assert(positions.size() == r_looptri_indices.size());
  BLI_assert(positions.size() == r_positions.size());

  BVHTreeFromMesh tree_data;
  BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_LOOPTRI, 2);

  for (const int i : positions.index_range()) {
    BVHTreeNearest nearest;
    nearest.dist_sq = FLT_MAX;
    const float3 position = positions[i];
    BLI_bvhtree_find_nearest(
        tree_data.tree, position, &nearest, tree_data.nearest_callback, &tree_data);
    r_looptri_indices[i] = nearest.index;
    r_positions[i] = nearest.co;
    r_distances_sq[i] = nearest.dist_sq;
  }

  free_bvhtree_from_mesh(&tree_data);
}

static void transfer_attribute(const GeometrySet &src_geometry,
                               GeometryComponent &dst_component,
                               const AttributeDomain result_domain,
                               const CustomDataType data_type,
                               const StringRef src_name,
                               const StringRef dst_name)
{
  const CPPType &type = *bke::custom_data_type_to_cpp_type(data_type);

  GVArray_Typed<float3> dst_positions = dst_component.attribute_get_for_read<float3>(
      "position", result_domain, {0, 0, 0});
  const int64_t tot_dst_positions = dst_positions.size();

  bool use_pointcloud = false;
  Array<int> pointcloud_point_indices;
  Array<float> pointcloud_point_distances_sq;

  bool use_mesh = false;
  Array<int> mesh_looptri_indices;
  Array<float3> mesh_point_positions;
  Array<float> mesh_point_distances_sq;

  if (src_geometry.has<PointCloudComponent>()) {
    const PointCloudComponent &component =
        *src_geometry.get_component_for_read<PointCloudComponent>();
    const PointCloud *pointcloud = component.get_for_read();
    if (pointcloud != nullptr && pointcloud->totpoint > 0) {
      pointcloud_point_indices.reinitialize(tot_dst_positions);
      pointcloud_point_distances_sq.reinitialize(tot_dst_positions);
      get_closest_pointcloud_point_indices(
          *pointcloud, dst_positions, pointcloud_point_indices, pointcloud_point_distances_sq);
      use_pointcloud = true;
    }
  }
  if (src_geometry.has<MeshComponent>()) {
    const MeshComponent &component = *src_geometry.get_component_for_read<MeshComponent>();
    const Mesh *mesh = component.get_for_read();
    if (mesh != nullptr && mesh->totpoly > 0) {
      mesh_looptri_indices.reinitialize(tot_dst_positions);
      mesh_point_positions.reinitialize(tot_dst_positions);
      mesh_point_distances_sq.reinitialize(tot_dst_positions);
      get_closest_mesh_surface_samples(*mesh,
                                       dst_positions,
                                       mesh_looptri_indices,
                                       mesh_point_positions,
                                       mesh_point_distances_sq);
      use_mesh = true;
    }
  }

  Vector<int> pointcloud_sample_indices;
  Vector<int> mesh_sample_indices;

  if (use_mesh && use_pointcloud) {
    for (const int i : IndexRange(tot_dst_positions)) {
      if (pointcloud_point_distances_sq[i] < mesh_point_distances_sq[i]) {
        pointcloud_sample_indices.append(i);
      }
      else {
        mesh_sample_indices.append(i);
      }
    }
  }
  else if (use_mesh) {
    /* TODO: Optimize. */
    mesh_sample_indices = IndexRange(tot_dst_positions).as_span();
  }
  else if (use_pointcloud) {
    pointcloud_sample_indices = IndexRange(tot_dst_positions).as_span();
  }
  else {
    return;
  }

  OutputAttribute dst_attribute = dst_component.attribute_try_get_for_output_only(
      dst_name, result_domain, data_type);
  if (!dst_attribute) {
    return;
  }

  BUFFER_FOR_CPP_TYPE_VALUE(type, buffer);

  if (!pointcloud_sample_indices.is_empty()) {
    const PointCloudComponent &component =
        *src_geometry.get_component_for_read<PointCloudComponent>();
    ReadAttributeLookup src_attribute = component.attribute_try_get_for_read(src_name, data_type);
    if (src_attribute) {
      BLI_assert(src_attribute.domain == ATTR_DOMAIN_POINT);
      for (const int i : pointcloud_sample_indices) {
        const int point_index = pointcloud_point_indices[i];
        src_attribute.varray->get(point_index, buffer);
        dst_attribute->set_by_relocate(i, buffer);
      }
    }
    else {
      const void *default_value = type.default_value();
      for (const int i : pointcloud_point_indices) {
        dst_attribute->set_by_copy(i, default_value);
      }
    }
  }

  if (!mesh_sample_indices.is_empty()) {
    const MeshComponent &component = *src_geometry.get_component_for_read<MeshComponent>();
    ReadAttributeLookup src_attribute = component.attribute_try_get_for_read(src_name, data_type);
    if (src_attribute) {
      GVArray_GSpan src_span{*src_attribute.varray};
      switch (src_attribute.domain) {
        case ATTR_DOMAIN_POINT: {
          break;
        }
        case ATTR_DOMAIN_FACE: {
          break;
        }
        case ATTR_DOMAIN_CORNER: {
          break;
        }
        case ATTR_DOMAIN_EDGE: {
          break;
        }
      }
    }
    else {
      const void *default_value = type.default_value();
      for (const int i : mesh_sample_indices) {
        dst_attribute->set_by_copy(i, default_value);
      }
    }
  }

  dst_attribute.save();
}

static void geo_node_attribute_transfer_exec(GeoNodeExecParams params)
{
  GeometrySet dst_geometry_set = params.extract_input<GeometrySet>("Geometry");
  GeometrySet src_geometry_set = params.extract_input<GeometrySet>("Target");
  const std::string src_attribute_name = params.extract_input<std::string>("Source");
  const std::string dst_attribute_name = params.extract_input<std::string>("Destination");

  if (src_attribute_name.empty() || dst_attribute_name.empty()) {
    params.set_output("Geometry", dst_geometry_set);
    return;
  }

  const NodeGeometryAttributeTransfer &storage =
      *(const NodeGeometryAttributeTransfer *)params.node().storage;
  const AttributeDomain dst_domain = (AttributeDomain)storage.domain;
  const GeometryNodeAttributeTransferMappingMode mapping =
      (GeometryNodeAttributeTransferMappingMode)storage.mapping;

  dst_geometry_set = bke::geometry_set_realize_instances(dst_geometry_set);
  src_geometry_set = bke::geometry_set_realize_instances(src_geometry_set);

  const CustomDataType result_data_type = get_result_data_type(src_geometry_set,
                                                               src_attribute_name);

  if (dst_geometry_set.has<MeshComponent>()) {
    transfer_attribute(src_geometry_set,
                       dst_geometry_set.get_component_for_write<MeshComponent>(),
                       dst_domain,
                       result_data_type,
                       src_attribute_name,
                       dst_attribute_name);
  }
  if (dst_geometry_set.has<PointCloudComponent>()) {
    transfer_attribute(src_geometry_set,
                       dst_geometry_set.get_component_for_write<PointCloudComponent>(),
                       dst_domain,
                       result_data_type,
                       src_attribute_name,
                       dst_attribute_name);
  }

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

}  // namespace blender::nodes

void register_node_type_geo_attribute_transfer()
{
  static bNodeType ntype;

  geo_node_type_base(
      &ntype, GEO_NODE_ATTRIBUTE_TRANSFER, "Attribute Transfer", NODE_CLASS_ATTRIBUTE, 0);
  node_type_socket_templates(
      &ntype, geo_node_attribute_transfer_in, geo_node_attribute_transfer_out);
  node_type_init(&ntype, blender::nodes::geo_node_attribute_transfer_init);
  node_type_storage(&ntype,
                    "NodeGeometryAttributeTransfer",
                    node_free_standard_storage,
                    node_copy_standard_storage);
  ntype.geometry_node_execute = blender::nodes::geo_node_attribute_transfer_exec;
  ntype.draw_buttons = geo_node_attribute_transfer_layout;
  nodeRegisterType(&ntype);
}