Blender  V3.3
node_geo_triangulate.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "BKE_customdata.h"
4 #include "BKE_mesh.h"
5 
6 #include "bmesh.h"
7 #include "bmesh_tools.h"
8 
9 #include "DNA_mesh_types.h"
10 
11 #include "UI_interface.h"
12 #include "UI_resources.h"
13 
14 #include "node_geometry_util.hh"
15 
17 
19 {
20  b.add_input<decl::Geometry>(N_("Mesh")).supported_type(GEO_COMPONENT_TYPE_MESH);
21  b.add_input<decl::Bool>(N_("Selection")).default_value(true).supports_field().hide_value();
22  b.add_input<decl::Int>(N_("Minimum Vertices")).default_value(4).min(4).max(10000);
23  b.add_output<decl::Geometry>(N_("Mesh"));
24 }
25 
26 static void node_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
27 {
28  uiItemR(layout, ptr, "quad_method", 0, "", ICON_NONE);
29  uiItemR(layout, ptr, "ngon_method", 0, "", ICON_NONE);
30 }
31 
33 {
36 }
37 
39  const int quad_method,
40  const int ngon_method,
41  const IndexMask selection,
42  const int min_vertices)
43 {
44  CustomData_MeshMasks cd_mask_extra = {
46  BMeshCreateParams create_params{0};
47  BMeshFromMeshParams from_mesh_params{};
48  from_mesh_params.calc_face_normal = true;
49  from_mesh_params.calc_vert_normal = true;
50  from_mesh_params.cd_mask_extra = cd_mask_extra;
51  BMesh *bm = BKE_mesh_to_bmesh_ex(&mesh, &create_params, &from_mesh_params);
52 
53  /* Tag faces to be triangulated from the selection mask. */
55  for (int i_face : selection) {
57  }
58 
59  BM_mesh_triangulate(bm, quad_method, ngon_method, min_vertices, true, nullptr, nullptr, nullptr);
62  return result;
63 }
64 
66 {
67  GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
68  Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
69  const int min_vertices = std::max(params.extract_input<int>("Minimum Vertices"), 4);
70 
72  params.node().custom1);
74  params.node().custom2);
75 
76  geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
77  if (!geometry_set.has_mesh()) {
78  return;
79  }
81  const Mesh &mesh_in = *geometry_set.get_mesh_for_read();
82 
83  const int domain_size = component.attribute_domain_size(ATTR_DOMAIN_FACE);
85  FieldEvaluator evaluator{context, domain_size};
86  evaluator.add(selection_field);
87  evaluator.evaluate();
88  const IndexMask selection = evaluator.get_evaluated_as_mask(0);
89 
90  Mesh *mesh_out = triangulate_mesh_selection(
91  mesh_in, quad_method, ngon_method, selection, min_vertices);
92  geometry_set.replace_mesh(mesh_out);
93  });
94 
95  params.set_output("Mesh", std::move(geometry_set));
96 }
97 } // namespace blender::nodes::node_geo_triangulate_cc
98 
100 {
101  namespace file_ns = blender::nodes::node_geo_triangulate_cc;
102 
103  static bNodeType ntype;
104 
110  nodeRegisterType(&ntype);
111 }
@ ATTR_DOMAIN_FACE
Definition: BKE_attribute.h:29
CustomData interface, see also DNA_customdata_types.h.
@ GEO_COMPONENT_TYPE_MESH
struct BMesh * BKE_mesh_to_bmesh_ex(const struct Mesh *me, const struct BMeshCreateParams *create_params, const struct BMeshFromMeshParams *convert_params)
struct Mesh * BKE_mesh_from_bmesh_for_eval_nomain(struct BMesh *bm, const struct CustomData_MeshMasks *cd_mask_extra, const struct Mesh *me_settings)
#define GEO_NODE_TRIANGULATE
Definition: BKE_node.h:1383
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4390
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
#define UNUSED(x)
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:108
#define CD_MASK_ORIGINDEX
GeometryNodeTriangulateQuads
@ GEO_NODE_TRIANGULATE_QUAD_SHORTEDGE
GeometryNodeTriangulateNGons
@ GEO_NODE_TRIANGULATE_NGON_BEAUTY
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
#define BM_elem_flag_set(ele, hflag, val)
Definition: bmesh_inline.h:16
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
Definition: bmesh_mesh.cc:258
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.cc:558
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
Definition: bmesh_mesh.h:115
void BM_mesh_triangulate(BMesh *bm, const int quad_method, const int ngon_method, const int min_vertices, const bool tag_only, BMOperator *op, BMOpSlot *slot_facemap_out, BMOpSlot *slot_facemap_double_out)
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static Mesh * triangulate_mesh_selection(const Mesh &mesh, const int quad_method, const int ngon_method, const IndexMask selection, const int min_vertices)
static void geo_triangulate_init(bNodeTree *UNUSED(ntree), bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_triangulate()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
const Mesh * get_mesh_for_read() const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
bool has_mesh() const
Defines a node type.
Definition: BKE_node.h:226
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:316
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:244
NodeDeclareFunction declare
Definition: BKE_node.h:324
float max
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480