Blender  V3.3
node_geo_bounding_box.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
4 
5 #include "node_geometry_util.hh"
6 
8 
10 {
11  b.add_input<decl::Geometry>(N_("Geometry"));
12  b.add_output<decl::Geometry>(N_("Bounding Box"));
13  b.add_output<decl::Vector>(N_("Min"));
14  b.add_output<decl::Vector>(N_("Max"));
15 }
16 
18 {
19  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
20 
21  /* Compute the min and max of all realized geometry for the two
22  * vector outputs, which are only meant to consider real geometry. */
23  float3 min = float3(FLT_MAX);
24  float3 max = float3(-FLT_MAX);
26  if (min == float3(FLT_MAX)) {
27  params.set_output("Min", float3(0));
28  params.set_output("Max", float3(0));
29  }
30  else {
31  params.set_output("Min", min);
32  params.set_output("Max", max);
33  }
34 
35  /* Generate the bounding box meshes inside each unique geometry set (including individually for
36  * every instance). Because geometry components are reference counted anyway, we can just
37  * repurpose the original geometry sets for the output. */
38  if (params.output_is_required("Bounding Box")) {
39  geometry_set.modify_geometry_sets([&](GeometrySet &sub_geometry) {
40  float3 sub_min = float3(FLT_MAX);
41  float3 sub_max = float3(-FLT_MAX);
42 
43  /* Reuse the min and max calculation if this is the main "real" geometry set. */
44  if (&sub_geometry == &geometry_set) {
45  sub_min = min;
46  sub_max = max;
47  }
48  else {
49  sub_geometry.compute_boundbox_without_instances(&sub_min, &sub_max);
50  }
51 
52  if (sub_min == float3(FLT_MAX)) {
53  sub_geometry.remove_geometry_during_modify();
54  }
55  else {
56  const float3 scale = sub_max - sub_min;
57  const float3 center = sub_min + scale / 2.0f;
58  Mesh *mesh = geometry::create_cuboid_mesh(scale, 2, 2, 2, "uv_map");
60  sub_geometry.replace_mesh(mesh);
62  }
63  });
64 
65  params.set_output("Bounding Box", std::move(geometry_set));
66  }
67 }
68 
69 } // namespace blender::nodes::node_geo_bounding_box_cc
70 
72 {
73  namespace file_ns = blender::nodes::node_geo_bounding_box_cc;
74 
75  static bNodeType ntype;
76 
80  nodeRegisterType(&ntype);
81 }
@ GEO_COMPONENT_TYPE_MESH
#define GEO_NODE_BOUNDING_BOX
Definition: BKE_node.h:1399
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
NSNotificationCenter * center
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
Mesh * create_cuboid_mesh(const float3 &size, int verts_x, int verts_y, int verts_z, const bke::AttributeIDRef &uv_id)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
void transform_mesh(Mesh &mesh, const float3 translation, const float3 rotation, const float3 scale)
vec_base< float, 3 > float3
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_bounding_box()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
#define min(a, b)
Definition: sort.c:35
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void keep_only_during_modify(const blender::Span< GeometryComponentType > component_types)
void modify_geometry_sets(ForeachSubGeometryCallback callback)
bool compute_boundbox_without_instances(blender::float3 *r_min, blender::float3 *r_max) const
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
float max
#define N_(msgid)