Blender V5.0
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")
64 .supported_type(GeometryComponent::Type::Mesh)
66 .description("Mesh to triangulate");
67 b.add_output<decl::Geometry>("Mesh").propagate_all().align_with_previous();
68 b.add_input<decl::Bool>("Selection").default_value(true).field_on_all().hide_value();
69 b.add_input<decl::Menu>("Quad Method")
72 .optional_label()
73 .description("Method for splitting the quads into triangles");
74 b.add_input<decl::Menu>("N-gon Method")
78 .description("Method for splitting the n-gons into triangles");
79}
80
82{
83 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
84 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
85 const AttributeFilter &attribute_filter = params.get_attribute_filter("Mesh");
86
87 const auto ngon_method = params.extract_input<geometry::TriangulateNGonMode>("N-gon Method");
88 const auto quad_method = params.extract_input<geometry::TriangulateQuadMode>("Quad Method");
89
90 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
91 const Mesh *src_mesh = geometry_set.get_mesh();
92 if (!src_mesh) {
93 return;
94 }
95 if (src_mesh->corners_num == src_mesh->faces_num * 3) {
96 /* The mesh is already completely triangulated. */
97 return;
98 }
99
100 const bke::MeshFieldContext context(*src_mesh, AttrDomain::Face);
101 FieldEvaluator evaluator{context, src_mesh->faces_num};
102 evaluator.add(selection_field);
103 evaluator.evaluate();
104 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
105 if (selection.is_empty()) {
106 return;
107 }
108
109 std::optional<Mesh *> mesh = geometry::mesh_triangulate(
110 *src_mesh,
111 selection,
114 attribute_filter);
115 if (!mesh) {
116 return;
117 }
118
119 /* Vertex order is not affected. */
122
123 geometry_set.replace_mesh(*mesh);
124 });
125
126 params.set_output("Mesh", std::move(geometry_set));
127}
128
129static void node_register()
130{
131 static blender::bke::bNodeType ntype;
132
133 geo_node_type_base(&ntype, "GeometryNodeTriangulate", GEO_NODE_TRIANGULATE);
134 ntype.ui_name = "Triangulate";
135 ntype.ui_description = "Convert all faces in a mesh to triangular faces";
136 ntype.enum_name_legacy = "TRIANGULATE";
138 ntype.declare = node_declare;
141}
143
144} // namespace blender::nodes::node_geo_triangulate_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_TRIANGULATE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
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:2416
void debug_randomize_edge_order(Mesh *mesh)
Definition randomize.cc:131
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:219
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(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
int corners_num
int faces_num
const Mesh * get_mesh() const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
#define N_(msgid)