Blender V4.3
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_output<decl::Geometry>("Bounding Box");
16 b.add_output<decl::Vector>("Min");
17 b.add_output<decl::Vector>("Max");
18}
19
21{
22 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
23
24 /* Compute the min and max of all realized geometry for the two
25 * vector outputs, which are only meant to consider real geometry. */
26 const std::optional<Bounds<float3>> bounds = geometry_set.compute_boundbox_without_instances();
27 if (!bounds) {
28 params.set_output("Min", float3(0));
29 params.set_output("Max", float3(0));
30 }
31 else {
32 params.set_output("Min", bounds->min);
33 params.set_output("Max", bounds->max);
34 }
35
36 /* Generate the bounding box meshes inside each unique geometry set (including individually for
37 * every instance). Because geometry components are reference counted anyway, we can just
38 * repurpose the original geometry sets for the output. */
39 if (params.output_is_required("Bounding Box")) {
40 geometry_set.modify_geometry_sets([&](GeometrySet &sub_geometry) {
41 std::optional<Bounds<float3>> sub_bounds;
42
43 /* Reuse the min and max calculation if this is the main "real" geometry set. */
44 if (&sub_geometry == &geometry_set) {
45 sub_bounds = bounds;
46 }
47 else {
48 sub_bounds = sub_geometry.compute_boundbox_without_instances();
49 }
50
51 if (!sub_bounds) {
52 sub_geometry.remove_geometry_during_modify();
53 }
54 else {
55 const float3 scale = sub_bounds->max - sub_bounds->min;
56 const float3 center = sub_bounds->min + scale / 2.0f;
57 Mesh *mesh = geometry::create_cuboid_mesh(scale, 2, 2, 2, "uv_map");
59 sub_geometry.replace_mesh(mesh);
61 }
62 });
63
64 params.set_output("Bounding Box", std::move(geometry_set));
65 }
66}
67
68static void node_register()
69{
70 static blender::bke::bNodeType ntype;
71
72 geo_node_type_base(&ntype, GEO_NODE_BOUNDING_BOX, "Bounding Box", NODE_CLASS_GEOMETRY);
73 ntype.declare = node_declare;
76}
78
79} // namespace blender::nodes::node_geo_bounding_box_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define NOD_REGISTER_NODE(REGISTER_FUNC)
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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, int type, const char *name, short nclass)
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)
std::optional< Bounds< float3 > > compute_boundbox_without_instances() const
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeDeclareFunction declare
Definition BKE_node.hh:347