Blender V5.3
node_geo_input_instance_bounds.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "BKE_instances.hh"
7
9
11
13{
14 b.add_input<decl::Bool>("Use Radius"_ustr)
15 .default_value(true)
17 "For curves, point clouds, and Grease Pencil, take the radius attribute into account "
18 "when computing the bounds.");
19 b.add_output<decl::Vector>("Min"_ustr).structure_type(StructureType::Field);
20 b.add_output<decl::Vector>("Max"_ustr).structure_type(StructureType::Field);
21}
22
24 private:
25 bool use_radius_;
26 bool return_max_;
27
28 public:
29 InstanceBoundsField(bool use_radius, bool return_max)
30 : bke::InstancesFieldInput(CPPType::get<float3>(), return_max ? "Max" : "Min"),
31 use_radius_(use_radius),
32 return_max_(return_max)
33 {
34 }
35
37 const IndexMask &mask) const final
38 {
39 const Span<int> handles = instances.reference_handles();
40 const Span<bke::InstanceReference> references = instances.references();
41
42 IndexMaskMemory memory;
43 IndexMask reference_mask(references.size());
44 Array<bool> reference_in_mask(references.size(), false);
45
46 mask.foreach_index(
47 [&](const int i) {
48 const int handle = handles[i];
49 if (handle < reference_in_mask.size()) {
50 reference_in_mask[handle] = true;
51 }
52 },
54
55 reference_mask = IndexMask::from_bools(reference_in_mask.as_span(), memory);
56 Array<float3> reference_bounds(references.size());
57
58 reference_mask.foreach_index(
59 [&](const int reference_index) {
60 const bke::InstanceReference &reference = references[reference_index];
61
62 GeometrySet instance_geometry;
63 switch (reference.type()) {
65 instance_geometry = reference.geometry_set();
66 break;
68 instance_geometry = bke::object_get_evaluated_geometry_set(reference.object());
69 break;
71 break;
73 break;
74 }
75
76 std::optional<Bounds<float3>> sub_bounds =
77 instance_geometry.compute_boundbox_without_instances(use_radius_);
78
79 if (sub_bounds) {
80 reference_bounds[reference_index] = return_max_ ? sub_bounds->max : sub_bounds->min;
81 }
82 else {
83 reference_bounds[reference_index] = float3(0.0f);
84 }
85 },
87
88 Array<float3> output_bounds(mask.min_array_size());
89 mask.foreach_index(
90 [&](const int instance_index) {
91 output_bounds[instance_index] = reference_bounds[handles[instance_index]];
92 },
94
95 return VArray<float3>::from_container(std::move(output_bounds));
96 }
97
98 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
99 {
100 static constexpr int8_t id = 0;
101 hash.add(&id);
102 hash.add(use_radius_);
103 hash.add(return_max_);
104 }
105};
106
108{
109 const bool use_radius = params.extract_input<bool>("Use Radius"_ustr);
110 params.set_output("Min"_ustr, Field<float3>::from_input<InstanceBoundsField>(use_radius, false));
111 params.set_output("Max"_ustr, Field<float3>::from_input<InstanceBoundsField>(use_radius, true));
112}
113
114static void node_register()
115{
116 static bke::bNodeType ntype;
117
118 geo_node_type_base(&ntype, "GeometryNodeInputInstanceBounds"_ustr);
119 ntype.ui_name = "Instance Bounds";
120 ntype.ui_description = "Calculate position bounds of each instance's geometry set";
121 ntype.nclass = NODE_CLASS_INPUT;
123 ntype.declare = node_declare;
125}
127
128} // namespace blender::nodes::node_geo_input_instance_bounds_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
int64_t size() const
Definition BLI_array.hh:267
Span< T > as_span() const
Definition BLI_array.hh:254
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr int64_t size() const
Definition BLI_span.hh:256
static VArray from_container(ContainerT container)
uint64_t hash() const
Definition field.cc:185
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const bke::Instances &instances, const IndexMask &mask) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
#define instance_index
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
GeometrySet object_get_evaluated_geometry_set(const Object &object, bool apply_subdiv=true)
constexpr ParallelGrainSize grain_size(int grain_size)
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
std::optional< Bounds< float3 > > compute_boundbox_without_instances(bool use_radius=true, bool use_subdiv=false) const
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
NodeDeclareFunction declare
Definition BKE_node.hh:393