Blender  V3.3
node_geo_points_to_vertices.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "BLI_task.hh"
4 
5 #include "BKE_attribute_math.hh"
6 #include "BKE_mesh.h"
7 
8 #include "node_geometry_util.hh"
9 
11 
12 using blender::Array;
13 
15 {
16  b.add_input<decl::Geometry>(N_("Points")).supported_type(GEO_COMPONENT_TYPE_POINT_CLOUD);
17  b.add_input<decl::Bool>(N_("Selection")).default_value(true).supports_field().hide_value();
18  b.add_output<decl::Geometry>(N_("Mesh"));
19 }
20 
21 /* One improvement would be to move the attribute arrays directly to the mesh when possible. */
22 static void geometry_set_points_to_vertices(GeometrySet &geometry_set,
23  Field<bool> &selection_field)
24 {
25  const PointCloudComponent *point_component =
27  if (point_component == nullptr) {
28  geometry_set.remove_geometry_during_modify();
29  return;
30  }
31 
32  GeometryComponentFieldContext field_context{*point_component, ATTR_DOMAIN_POINT};
33  const int domain_num = point_component->attribute_domain_size(ATTR_DOMAIN_POINT);
34  if (domain_num == 0) {
35  geometry_set.remove_geometry_during_modify();
36  return;
37  }
38 
39  fn::FieldEvaluator selection_evaluator{field_context, domain_num};
40  selection_evaluator.add(selection_field);
41  selection_evaluator.evaluate();
42  const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0);
43 
47 
48  Mesh *mesh = BKE_mesh_new_nomain(selection.size(), 0, 0, 0, 0);
49  geometry_set.replace_mesh(mesh);
50  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
51 
52  for (Map<AttributeIDRef, AttributeKind>::Item entry : attributes.items()) {
53  const AttributeIDRef attribute_id = entry.key;
54  const eCustomDataType data_type = entry.value.data_type;
55  GVArray src = point_component->attributes()->lookup_or_default(
56  attribute_id, ATTR_DOMAIN_POINT, data_type);
58  mesh_component.attributes_for_write()->lookup_or_add_for_write_only_span(
59  attribute_id, ATTR_DOMAIN_POINT, data_type);
60  if (dst && src) {
61  src.materialize_compressed_to_uninitialized(selection, dst.span.data());
62  dst.finish();
63  }
64  }
65 
67 }
68 
70 {
71  GeometrySet geometry_set = params.extract_input<GeometrySet>("Points");
72  Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
73 
74  geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
75  geometry_set_points_to_vertices(geometry_set, selection_field);
76  });
77 
78  params.set_output("Mesh", std::move(geometry_set));
79 }
80 
81 } // namespace blender::nodes::node_geo_points_to_vertices_cc
82 
84 {
86 
87  static bNodeType ntype;
88 
90  &ntype, GEO_NODE_POINTS_TO_VERTICES, "Points to Vertices", NODE_CLASS_GEOMETRY);
93  nodeRegisterType(&ntype);
94 }
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:27
@ GEO_COMPONENT_TYPE_MESH
@ GEO_COMPONENT_TYPE_POINT_CLOUD
struct Mesh * BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
Definition: mesh.cc:991
#define GEO_NODE_POINTS_TO_VERTICES
Definition: BKE_node.h:1436
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
eCustomDataType
int attribute_domain_size(eAttrDomain domain) const
Definition: geometry_set.cc:63
std::optional< blender::bke::MutableAttributeAccessor > attributes_for_write() final
std::optional< blender::bke::AttributeAccessor > attributes() const final
int64_t size() const
ItemIterator items() const
Definition: BLI_map.hh:859
SyclQueue void void * src
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static int domain_num(const CurvesGeometry &curves, const eAttrDomain domain)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void geometry_set_points_to_vertices(GeometrySet &geometry_set, Field< bool > &selection_field)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_points_to_vertices()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
void keep_only_during_modify(const blender::Span< GeometryComponentType > component_types)
const GeometryComponent * get_component_for_read(GeometryComponentType component_type) const
void gather_attributes_for_propagation(blender::Span< GeometryComponentType > component_types, GeometryComponentType dst_component_type, bool include_instances, blender::Map< blender::bke::AttributeIDRef, blender::bke::AttributeKind > &r_attributes) const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
void remove_geometry_during_modify()
Defines a node type.
Definition: BKE_node.h:226
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:316
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)