Blender V5.3
node_geo_mesh_primitive_circle.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 "BKE_material.hh"
6#include "BKE_mesh.hh"
7
8#include "NOD_rna_define.hh"
9
11#include "UI_resources.hh"
12
13#include "BLI_array_utils.hh"
14
16
17#include "node_geometry_util.hh"
18
19#include "RNA_enum_types.hh"
20
22
24
26{
27 b.add_input<decl::Int>("Vertices"_ustr)
28 .default_value(32)
29 .min(3)
30 .description("Number of vertices on the circle");
31 b.add_input<decl::Float>("Radius"_ustr)
32 .default_value(1.0f)
33 .min(0.0f)
34 .subtype(PROP_DISTANCE)
35 .description("Distance of the vertices from the origin");
36 b.add_output<decl::Geometry>("Mesh"_ustr);
37}
38
39static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
40{
41 layout.use_property_split_set(true);
42 layout.use_property_decorate_set(false);
43 layout.prop(ptr, "fill_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
44}
45
46static void node_init(bNodeTree * /*tree*/, bNode *node)
47{
48 NodeGeometryMeshCircle *node_storage = MEM_new<NodeGeometryMeshCircle>(__func__);
49
51
52 node->storage = node_storage;
53}
54
55static int circle_vert_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
56{
57 switch (fill_type) {
60 return verts_num;
62 return verts_num + 1;
63 }
65 return 0;
66}
67
68static int circle_edge_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
69{
70 switch (fill_type) {
73 return verts_num;
75 return verts_num * 2;
76 }
78 return 0;
79}
80
81static int circle_corner_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
82{
83 switch (fill_type) {
85 return 0;
87 return verts_num;
89 return verts_num * 3;
90 }
92 return 0;
93}
94
95static int circle_face_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
96{
97 switch (fill_type) {
99 return 0;
101 return 1;
103 return verts_num;
104 }
106 return 0;
107}
108
109static Bounds<float3> calculate_bounds_circle(const float radius, const int verts_num)
110{
111 return geometry::calculate_bounds_radial_primitive(0.0f, radius, verts_num, 0.0f);
112}
113
114static Mesh *create_circle_mesh(const float radius,
115 const int verts_num,
116 const GeometryNodeMeshCircleFillType fill_type)
117{
118 Mesh *mesh = BKE_mesh_new_nomain(circle_vert_total(fill_type, verts_num),
119 circle_edge_total(fill_type, verts_num),
120 circle_face_total(fill_type, verts_num),
121 circle_corner_total(fill_type, verts_num));
123 MutableSpan<float3> positions = mesh->vert_positions_for_write();
124 MutableSpan<int2> edges = mesh->edges_for_write();
125 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
126 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
127 MutableSpan<int> corner_edges = mesh->corner_edges_for_write();
128 if (fill_type != GEO_NODE_MESH_CIRCLE_FILL_NONE) {
129 bke::mesh_smooth_set(*mesh, false);
130 }
131
132 /* Assign vertex coordinates. */
133 const float angle_delta = 2.0f * (M_PI / float(verts_num));
134 for (const int i : IndexRange(verts_num)) {
135 const float angle = i * angle_delta;
136 positions[i] = float3(std::cos(angle) * radius, std::sin(angle) * radius, 0.0f);
137 }
139 positions.last() = float3(0);
140 }
141
142 /* Create outer edges. */
143 for (const int i : IndexRange(verts_num)) {
144 int2 &edge = edges[i];
145 edge[0] = i;
146 edge[1] = (i + 1) % verts_num;
147 }
148
149 /* Create triangle fan edges. */
151 for (const int i : IndexRange(verts_num)) {
152 int2 &edge = edges[verts_num + i];
153 edge[0] = verts_num;
154 edge[1] = i;
155 }
156 }
157
158 /* Create corners and faces. */
159 if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_NGON) {
160 face_offsets.first() = 0;
161 face_offsets.last() = corner_verts.size();
162
165
166 mesh->tag_loose_edges_none();
167 }
168 else if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) {
169 for (const int i : face_offsets.index_range()) {
170 face_offsets[i] = 3 * i;
171 }
172 for (const int i : IndexRange(verts_num)) {
173 corner_verts[3 * i] = i;
174 corner_edges[3 * i] = i;
175
176 corner_verts[3 * i + 1] = (i + 1) % verts_num;
177 corner_edges[3 * i + 1] = verts_num + ((i + 1) % verts_num);
178
179 corner_verts[3 * i + 2] = verts_num;
180 corner_edges[3 * i + 2] = verts_num + i;
181 }
182 }
183
184 mesh->tag_loose_verts_none();
185 mesh->tag_overlapping_none();
186 mesh->bounds_set_eager(calculate_bounds_circle(radius, verts_num));
187
188 return mesh;
189}
190
192{
193 const NodeGeometryMeshCircle &storage = node_storage(params.node());
195
196 const float radius = params.extract_input<float>("Radius"_ustr);
197 const int verts_num = params.extract_input<int>("Vertices"_ustr);
198 if (verts_num < 3) {
199 params.error_message_add(NodeWarningType::Info, TIP_("Vertices must be at least 3"));
201 return;
202 }
203
204 Mesh *mesh = create_circle_mesh(radius, verts_num, fill);
205
206 params.set_output("Mesh"_ustr, GeometrySet::from_mesh(mesh));
207}
208
209static void node_rna(StructRNA *srna)
210{
212 "fill_type",
213 "Fill Type",
214 "",
218 nullptr,
219 true);
220}
221
222static void node_register()
223{
224 static bke::bNodeType ntype;
225
226 geo_node_type_base(&ntype, "GeometryNodeMeshCircle"_ustr, GEO_NODE_MESH_PRIMITIVE_CIRCLE);
227 ntype.ui_name = "Mesh Circle";
228 ntype.ui_description = "Generate a circular ring of edges";
229 ntype.enum_name_legacy = "MESH_PRIMITIVE_CIRCLE";
231 ntype.initfunc = node_init;
233 ntype, "NodeGeometryMeshCircle", node_free_standard_storage, node_copy_standard_storage);
236 ntype.declare = node_declare;
238
239 node_rna(ntype.rna_ext.srna);
240}
242
243} // namespace blender::nodes::node_geo_mesh_primitive_circle_cc
General operations, lookup, etc. for materials.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_MESH_PRIMITIVE_CIRCLE
#define M_PI
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T & first() const
Definition BLI_span.hh:685
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
#define storage(slot, qualifiers)
void fill_index_range(MutableSpan< T > span, const T start=0)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
Bounds< float3 > calculate_bounds_radial_primitive(float radius_top, float radius_bottom, int segments, float height)
static int circle_edge_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static int circle_corner_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static int circle_vert_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static Mesh * create_circle_mesh(const float radius, const int verts_num, const GeometryNodeMeshCircleFillType fill_type)
static int circle_face_total(const GeometryNodeMeshCircleFillType fill_type, const int verts_num)
static Bounds< float3 > calculate_bounds_circle(const float radius, const int verts_num)
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)
VecBase< int32_t, 2 > int2
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_id_material_eval_ensure_default_slot(ID *id)
const EnumPropertyItem rna_enum_node_geometry_mesh_circle_fill_type_items[]
GeometryNodeMeshCircleFillType
@ GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN
@ GEO_NODE_MESH_CIRCLE_FILL_NONE
@ GEO_NODE_MESH_CIRCLE_FILL_NGON
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
GeometryNodeMeshCircleFillType fill_type
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)