Blender V4.5
node_geo_bounding_box.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
6#include "GEO_transform.hh"
7
9
11
13{
14 b.add_input<decl::Geometry>("Geometry");
15 b.add_input<decl::Bool>("Use Radius")
16 .default_value(true)
18 "For curves, point clouds, and Grease Pencil, take the radius attribute into account "
19 "when computing the bounds.");
20 b.add_output<decl::Geometry>("Bounding Box").propagate_all_instance_attributes();
21 b.add_output<decl::Vector>("Min");
22 b.add_output<decl::Vector>("Max");
23}
24
26{
27 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
28 const bool use_radius = params.extract_input<bool>("Use Radius");
29
30 /* Compute the min and max of all realized geometry for the two
31 * vector outputs, which are only meant to consider real geometry. */
32 const std::optional<Bounds<float3>> bounds = geometry_set.compute_boundbox_without_instances(
33 use_radius);
34 if (!bounds) {
35 params.set_output("Min", float3(0));
36 params.set_output("Max", float3(0));
37 }
38 else {
39 params.set_output("Min", bounds->min);
40 params.set_output("Max", bounds->max);
41 }
42
43 /* Generate the bounding box meshes inside each unique geometry set (including individually for
44 * every instance). Because geometry components are reference counted anyway, we can just
45 * repurpose the original geometry sets for the output. */
46 if (params.output_is_required("Bounding Box")) {
47 geometry_set.modify_geometry_sets([&](GeometrySet &sub_geometry) {
48 std::optional<Bounds<float3>> sub_bounds;
49
50 /* Reuse the min and max calculation if this is the main "real" geometry set. */
51 if (&sub_geometry == &geometry_set) {
52 sub_bounds = bounds;
53 }
54 else {
55 sub_bounds = sub_geometry.compute_boundbox_without_instances(use_radius);
56 }
57
58 if (!sub_bounds) {
59 sub_geometry.remove_geometry_during_modify();
60 }
61 else {
62 const float3 scale = sub_bounds->max - sub_bounds->min;
63 const float3 center = sub_bounds->min + scale / 2.0f;
64 Mesh *mesh = geometry::create_cuboid_mesh(scale, 2, 2, 2, "uv_map");
66 sub_geometry.replace_mesh(mesh);
67 sub_geometry.keep_only_during_modify({GeometryComponent::Type::Mesh});
68 }
69 });
70
71 params.set_output("Bounding Box", std::move(geometry_set));
72 }
73}
74
75static void node_register()
76{
77 static blender::bke::bNodeType ntype;
78 geo_node_type_base(&ntype, "GeometryNodeBoundBox", GEO_NODE_BOUNDING_BOX);
79 ntype.ui_name = "Bounding Box";
80 ntype.ui_description =
81 "Calculate the limits of a geometry's positions and generate a box mesh with those "
82 "dimensions";
83 ntype.enum_name_legacy = "BOUNDING_BOX";
85 ntype.declare = node_declare;
88}
90
91} // namespace blender::nodes::node_geo_bounding_box_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_BOUNDING_BOX
#define NOD_REGISTER_NODE(REGISTER_FUNC)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void transform_mesh(Mesh &mesh, float3 translation, math::Quaternion rotation, float3 scale)
Mesh * create_cuboid_mesh(const float3 &size, int verts_x, int verts_y, int verts_z, const std::optional< StringRef > &uv_id)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
std::optional< Bounds< float3 > > compute_boundbox_without_instances(bool use_radius=true, bool use_subdiv=false) const
void keep_only_during_modify(Span< GeometryComponent::Type > component_types)
void modify_geometry_sets(ForeachSubGeometryCallback callback)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355