Blender V4.3
node_geo_curve_primitive_star.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_curves.hh"
6
8
10
12{
13 b.add_input<decl::Int>("Points")
14 .default_value(8)
15 .min(3)
16 .max(256)
18 .description("Number of points on each of the circles");
19 b.add_input<decl::Float>("Inner Radius")
20 .default_value(1.0f)
21 .min(0.0f)
23 .description("Radius of the inner circle; can be larger than outer radius");
24 b.add_input<decl::Float>("Outer Radius")
25 .default_value(2.0f)
26 .min(0.0f)
28 .description("Radius of the outer circle; can be smaller than inner radius");
29 b.add_input<decl::Float>("Twist")
30 .subtype(PROP_ANGLE)
31 .description("The counterclockwise rotation of the inner set of points");
32 b.add_output<decl::Geometry>("Curve");
33 b.add_output<decl::Bool>("Outer Points")
34 .field_on_all()
35 .description("An attribute field with a selection of the outer points");
36}
37
38static Curves *create_star_curve(const float inner_radius,
39 const float outer_radius,
40 const float twist,
41 const int points)
42{
44 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
45 curves.cyclic_for_write().first() = true;
46
47 MutableSpan<float3> positions = curves.positions_for_write();
48
49 const float theta_step = (2.0f * M_PI) / float(points);
50 for (const int i : IndexRange(points)) {
51 const float x = outer_radius * cos(theta_step * i);
52 const float y = outer_radius * sin(theta_step * i);
53 positions[i * 2] = {x, y, 0.0f};
54
55 const float inner_x = inner_radius * cos(theta_step * i + theta_step * 0.5f + twist);
56 const float inner_y = inner_radius * sin(theta_step * i + theta_step * 0.5f + twist);
57 positions[i * 2 + 1] = {inner_x, inner_y, 0.0f};
58 }
59
60 return curves_id;
61}
62
63static void create_selection_output(CurveComponent &component, const StringRef &r_attribute)
64{
66 component.attributes_for_write()->lookup_or_add_for_write_only_span<bool>(r_attribute,
67 AttrDomain::Point);
68 for (int i : selection.span.index_range()) {
69 selection.span[i] = i % 2 == 0;
70 }
71 selection.finish();
72}
73
75{
76 Curves *curves = create_star_curve(std::max(params.extract_input<float>("Inner Radius"), 0.0f),
77 std::max(params.extract_input<float>("Outer Radius"), 0.0f),
78 params.extract_input<float>("Twist"),
79 std::max(params.extract_input<int>("Points"), 3));
81
82 if (std::optional<std::string> outer_points_id =
83 params.get_output_anonymous_attribute_id_if_needed("Outer Points"))
84 {
85 create_selection_output(output.get_component_for_write<CurveComponent>(), *outer_points_id);
86 }
87 params.set_output("Curve", std::move(output));
88}
89
90static void node_register()
91{
92 static blender::bke::bNodeType ntype;
93 geo_node_type_base(&ntype, GEO_NODE_CURVE_PRIMITIVE_STAR, "Star", NODE_CLASS_GEOMETRY);
94 ntype.declare = node_declare;
97}
99
100} // namespace blender::nodes::node_geo_curve_primitive_star_cc
Low-level operations for curves.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define M_PI
@ CURVE_TYPE_POLY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_DISTANCE
Definition RNA_types.hh:159
@ PROP_ANGLE
Definition RNA_types.hh:155
@ PROP_UNSIGNED
Definition RNA_types.hh:152
std::optional< MutableAttributeAccessor > attributes_for_write() final
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float3 cos(float3 v)
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
Curves * curves_new_nomain_single(int points_num, CurveType type)
static void create_selection_output(CurveComponent &component, const StringRef &r_attribute)
static Curves * create_star_curve(const float inner_radius, const float outer_radius, const float twist, const int points)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
CurvesGeometry geometry
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeDeclareFunction declare
Definition BKE_node.hh:347