Blender V5.3
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
9#include "GEO_randomize.hh"
10
11#include "node_geometry_util.hh"
12
14
17 "BEAUTY",
18 0,
19 N_("Beauty"),
20 N_("Split the quads in nice triangles, slower method")},
22 "FIXED",
23 0,
24 N_("Fixed"),
25 N_("Split the quads on the first and third vertices")},
27 "FIXED_ALTERNATE",
28 0,
29 N_("Fixed Alternate"),
30 N_("Split the quads on the 2nd and 4th vertices")},
32 "SHORTEST_DIAGONAL",
33 0,
34 N_("Shortest Diagonal"),
35 N_("Split the quads along their shortest diagonal")},
37 "LONGEST_DIAGONAL",
38 0,
39 N_("Longest Diagonal"),
40 N_("Split the quads along their longest diagonal")},
41 {0, nullptr, 0, nullptr, nullptr},
42};
43
46 "BEAUTY",
47 0,
48 N_("Beauty"),
49 N_("Arrange the new triangles evenly (slow)")},
51 "CLIP",
52 0,
53 N_("Clip"),
54 N_("Split the polygons with an ear clipping algorithm")},
55 {0, nullptr, 0, nullptr, nullptr},
56};
57
59{
60 b.use_custom_socket_order();
61 b.allow_any_socket_order();
62
63 b.add_input<decl::Geometry>("Mesh"_ustr)
64 .supported_type(GeometryComponent::Type::Mesh)
66 .description("Mesh to triangulate");
67 b.add_output<decl::Geometry>("Mesh"_ustr).propagate_all_geometry().align_with_previous();
68 b.add_input<decl::Bool>("Selection"_ustr)
69 .default_value(true)
70 .evaluated_geometry_field()
71 .hide_value();
72 b.add_input<decl::Menu>("Quad Method"_ustr)
75 .optional_label()
76 .description("Method for splitting the quads into triangles");
77 b.add_input<decl::Menu>("N-gon Method"_ustr)
81 .description("Method for splitting the n-gons into triangles");
82}
83
85{
86 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh"_ustr);
87 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
88 const AttributeFilter &attribute_filter = params.get_attribute_filter("Mesh"_ustr);
89
90 const auto ngon_method = params.extract_input<geometry::TriangulateNGonMode>(
91 "N-gon Method"_ustr);
92 const auto quad_method = params.extract_input<geometry::TriangulateQuadMode>("Quad Method"_ustr);
93
94 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
95 const Mesh *src_mesh = geometry_set.get_mesh();
96 if (!src_mesh) {
97 return;
98 }
99 if (src_mesh->corners_num == src_mesh->faces_num * 3) {
100 /* The mesh is already completely triangulated. */
101 return;
102 }
103
104 const bke::MeshFieldContext context(*src_mesh, AttrDomain::Face);
105 FieldEvaluator evaluator{context, src_mesh->faces_num};
106 evaluator.add(selection_field);
107 evaluator.evaluate();
108 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
109 if (selection.is_empty()) {
110 return;
111 }
112
113 std::optional<Mesh *> mesh = geometry::mesh_triangulate(
114 *src_mesh,
115 selection,
118 attribute_filter);
119 if (!mesh) {
120 return;
121 }
122
123 /* Vertex order is not affected. */
126
127 geometry_set.replace_mesh(*mesh);
128 });
129
130 params.set_output("Mesh"_ustr, std::move(geometry_set));
131}
132
133static void node_register()
134{
135 static bke::bNodeType ntype;
136
137 geo_node_type_base(&ntype, "GeometryNodeTriangulate"_ustr, GEO_NODE_TRIANGULATE);
138 ntype.ui_name = "Triangulate";
139 ntype.ui_description = "Convert all faces in a mesh to triangular faces";
140 ntype.enum_name_legacy = "TRIANGULATE";
142 ntype.declare = node_declare;
145}
147
148} // namespace blender::nodes::node_geo_triangulate_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_TRIANGULATE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void debug_randomize_edge_order(Mesh *mesh)
Definition randomize.cc:138
std::optional< Mesh * > mesh_triangulate(const Mesh &src_mesh, const IndexMask &selection, TriangulateNGonMode ngon_mode, TriangulateQuadMode quad_mode, const bke::AttributeFilter &attribute_filter)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void debug_randomize_face_order(Mesh *mesh)
Definition randomize.cc:229
static const EnumPropertyItem rna_node_geometry_triangulate_quad_method_items[]
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static const EnumPropertyItem rna_node_geometry_triangulate_ngon_method_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
const Mesh * get_mesh() const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)