Blender V5.3
node_geo_points.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_pointcloud.hh"
6
8
10
12
14{
15 b.add_input<decl::Int>("Count"_ustr)
16 .default_value(1)
17 .min(0)
18 .description("The number of points to create");
19 b.add_input<decl::Vector>("Position"_ustr)
20 .subtype(PROP_TRANSLATION)
21 .default_value(float3(0.0f))
22 .structure_type(StructureType::Field)
23 .description("The positions of the new points");
24 b.add_input<decl::Float>("Radius"_ustr)
25 .min(0.0f)
26 .default_value(0.1f)
27 .subtype(PROP_DISTANCE)
28 .structure_type(StructureType::Field)
29 .description("The radii of the new points");
30 b.add_output<decl::Geometry>("Points"_ustr, "Geometry"_ustr);
31}
32
34 private:
35 int points_num_;
36
37 public:
38 PointsFieldContext(const int points_num) : points_num_(points_num) {}
39
41 {
42 return points_num_;
43 }
44
46 const IndexMask &mask,
47 ResourceScope & /*scope*/) const override
48 {
49 const bke::IDAttributeFieldInput *id_field_input =
50 dynamic_cast<const bke::IDAttributeFieldInput *>(&field_input);
51
52 const fn::IndexFieldInput *index_field_input = dynamic_cast<const fn::IndexFieldInput *>(
53 &field_input);
54
55 if (id_field_input == nullptr && index_field_input == nullptr) {
56 return {};
57 }
58
60 }
61};
62
64{
65 const int count = params.extract_input<int>("Count"_ustr);
66 if (count <= 0) {
68 return;
69 }
70
71 Field<float3> position_field = params.extract_input<Field<float3>>("Position"_ustr);
72 Field<float> radius_field = params.extract_input<Field<float>>("Radius"_ustr);
73
75 MutableAttributeAccessor attributes = points->attributes_for_write();
76
77 PointsFieldContext context{count};
78 fn::FieldEvaluator evaluator{context, count};
79 evaluator.add_with_destination(position_field, points->positions_for_write());
80 if (radius_field.depends_on_input()) {
81 AttributeWriter<float> output_radii = attributes.lookup_or_add_for_write<float>(
82 "radius", AttrDomain::Point);
83 evaluator.add_with_destination(radius_field, output_radii.varray);
84 evaluator.evaluate();
85 output_radii.finish();
86 }
87 else {
88 evaluator.evaluate();
89 const float radius = fn::evaluate_constant_field(radius_field);
90 attributes.add<float>("radius", AttrDomain::Point, bke::AttributeInitValue(radius));
91 }
92
93 params.set_output("Geometry"_ustr, GeometrySet::from_pointcloud(points));
94}
95
96static void node_register()
97{
98 static bke::bNodeType ntype;
99 geo_node_type_base(&ntype, "GeometryNodePoints"_ustr, GEO_NODE_POINTS);
100 ntype.ui_name = "Points";
101 ntype.ui_description = "Generate a point cloud with positions and radii defined by fields";
102 ntype.enum_name_legacy = "POINTS";
105 ntype.declare = node_declare;
107}
109
110} // namespace blender::nodes::node_geo_points_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_POINTS
General operations for point clouds.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GAttributeWriter lookup_or_add_for_write(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
int add_with_destination(GField field, GVMutableArray dst)
bool depends_on_input() const
Definition FN_field.hh:513
static GVArray get_index_varray(const IndexMask &mask)
Definition field.cc:536
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_input(const FieldInput &field_input, const IndexMask &mask, ResourceScope &) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void evaluate_constant_field(const GField &field, void *r_value)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
__int64 int64_t
Definition BKE_text.h:90
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
VecBase< float, 3 > float3
@ PROP_TRANSLATION
Definition RNA_types.hh:318
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)