Blender V4.5
node_geo_triangulate.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
5#include "DNA_mesh_types.h"
6
7#include "NOD_rna_define.hh"
8
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
14#include "GEO_randomize.hh"
15
16#include "node_geometry_util.hh"
17
19
21{
22 b.use_custom_socket_order();
23 b.allow_any_socket_order();
24 b.add_default_layout();
25 b.add_input<decl::Geometry>("Mesh").supported_type(GeometryComponent::Type::Mesh);
26 b.add_output<decl::Geometry>("Mesh").propagate_all().align_with_previous();
27 b.add_input<decl::Bool>("Selection").default_value(true).field_on_all().hide_value();
28}
29
30static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
31{
32 layout->prop(ptr, "quad_method", UI_ITEM_NONE, "", ICON_NONE);
33 layout->prop(ptr, "ngon_method", UI_ITEM_NONE, "", ICON_NONE);
34}
35
41
43{
44 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
45 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
46 const AttributeFilter &attribute_filter = params.get_attribute_filter("Mesh");
47
50
51 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
52 const Mesh *src_mesh = geometry_set.get_mesh();
53 if (!src_mesh) {
54 return;
55 }
56 if (src_mesh->corners_num == src_mesh->faces_num * 3) {
57 /* The mesh is already completely triangulated. */
58 return;
59 }
60
61 const bke::MeshFieldContext context(*src_mesh, AttrDomain::Face);
62 FieldEvaluator evaluator{context, src_mesh->faces_num};
63 evaluator.add(selection_field);
64 evaluator.evaluate();
65 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
66 if (selection.is_empty()) {
67 return;
68 }
69
70 std::optional<Mesh *> mesh = geometry::mesh_triangulate(
71 *src_mesh,
72 selection,
75 attribute_filter);
76 if (!mesh) {
77 return;
78 }
79
80 /* Vertex order is not affected. */
83
84 geometry_set.replace_mesh(*mesh);
85 });
86
87 params.set_output("Mesh", std::move(geometry_set));
88}
89
90static void node_rna(StructRNA *srna)
91{
92 static const EnumPropertyItem rna_node_geometry_triangulate_quad_method_items[] = {
94 "BEAUTY",
95 0,
96 "Beauty",
97 "Split the quads in nice triangles, slower method"},
99 "FIXED",
100 0,
101 "Fixed",
102 "Split the quads on the first and third vertices"},
104 "FIXED_ALTERNATE",
105 0,
106 "Fixed Alternate",
107 "Split the quads on the 2nd and 4th vertices"},
109 "SHORTEST_DIAGONAL",
110 0,
111 "Shortest Diagonal",
112 "Split the quads along their shortest diagonal"},
114 "LONGEST_DIAGONAL",
115 0,
116 "Longest Diagonal",
117 "Split the quads along their longest diagonal"},
118 {0, nullptr, 0, nullptr, nullptr},
119 };
120
121 static const EnumPropertyItem rna_node_geometry_triangulate_ngon_method_items[] = {
123 "BEAUTY",
124 0,
125 "Beauty",
126 "Arrange the new triangles evenly (slow)"},
128 "CLIP",
129 0,
130 "Clip",
131 "Split the polygons with an ear clipping algorithm"},
132 {0, nullptr, 0, nullptr, nullptr},
133 };
134
136 "quad_method",
137 "Quad Method",
138 "Method for splitting the quads into triangles",
139 rna_node_geometry_triangulate_quad_method_items,
142 nullptr,
143 true);
144
146 "ngon_method",
147 "N-gon Method",
148 "Method for splitting the n-gons into triangles",
149 rna_node_geometry_triangulate_ngon_method_items,
152 nullptr,
153 true);
154}
155
156static void node_register()
157{
158 static blender::bke::bNodeType ntype;
159
160 geo_node_type_base(&ntype, "GeometryNodeTriangulate", GEO_NODE_TRIANGULATE);
161 ntype.ui_name = "Triangulate";
162 ntype.ui_description = "Convert all faces in a mesh to triangular faces";
163 ntype.enum_name_legacy = "TRIANGULATE";
165 ntype.declare = node_declare;
170
171 node_rna(ntype.rna_ext.srna);
172}
174
175} // namespace blender::nodes::node_geo_triangulate_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_TRIANGULATE
@ GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_AMOUNT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_as_mask(int field_index)
Definition field.cc:804
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void debug_randomize_edge_order(Mesh *mesh)
Definition randomize.cc:105
std::optional< Mesh * > mesh_triangulate(const Mesh &src_mesh, const IndexMask &selection, TriangulateNGonMode ngon_mode, TriangulateQuadMode quad_mode, const bke::AttributeFilter &attribute_filter)
void debug_randomize_face_order(Mesh *mesh)
Definition randomize.cc:155
static void geo_triangulate_init(bNodeTree *, bNode *node)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
StructRNA * srna
Definition RNA_types.hh:909
int corners_num
int faces_num
int16_t custom1
int16_t custom2
const Mesh * get_mesh() const
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
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4226