Blender V5.3
node_geo_curve_primitive_quadrilateral.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#include "UI_resources.hh"
9
10#include "NOD_rna_define.hh"
12
13#include "node_geometry_util.hh"
14
16
18
20{
21 auto &width = b.add_input<decl::Float>("Width"_ustr)
22 .default_value(2.0f)
23 .min(0.0f)
24 .subtype(PROP_DISTANCE)
25 .description("The X axis size of the shape")
26 .available(false);
27 auto &height = b.add_input<decl::Float>("Height"_ustr)
28 .default_value(2.0f)
29 .min(0.0f)
30 .subtype(PROP_DISTANCE)
31 .description("The Y axis size of the shape")
32 .available(false);
33 auto &bottom = b.add_input<decl::Float>("Bottom Width"_ustr)
34 .default_value(4.0f)
35 .min(0.0f)
36 .subtype(PROP_DISTANCE)
37 .description("The X axis size of the shape")
38 .available(false);
39 auto &top = b.add_input<decl::Float>("Top Width"_ustr)
40 .default_value(2.0f)
41 .min(0.0f)
42 .subtype(PROP_DISTANCE)
43 .description("The X axis size of the shape")
44 .available(false);
45 auto &offset =
46 b.add_input<decl::Float>("Offset"_ustr)
47 .default_value(1.0f)
48 .subtype(PROP_DISTANCE)
49 .description(
50 "For Parallelogram, the relative X difference between the top and bottom edges. For "
51 "Trapezoid, the amount to move the top edge in the positive X axis")
52 .available(false);
53 auto &bottom_height = b.add_input<decl::Float>("Bottom Height"_ustr)
54 .default_value(3.0f)
55 .min(0.0f)
56 .subtype(PROP_DISTANCE)
57 .description("The distance between the bottom point and the X axis")
58 .available(false);
59 auto &top_height = b.add_input<decl::Float>("Top Height"_ustr)
60 .default_value(1.0f)
61 .subtype(PROP_DISTANCE)
62 .description("The distance between the top point and the X axis")
63 .available(false);
64 auto &p1 = b.add_input<decl::Vector>("Point 1"_ustr)
65 .default_value({-1.0f, -1.0f, 0.0f})
66 .subtype(PROP_TRANSLATION)
67 .description("The exact location of the point to use")
68 .available(false);
69 auto &p2 = b.add_input<decl::Vector>("Point 2"_ustr)
70 .default_value({1.0f, -1.0f, 0.0f})
71 .subtype(PROP_TRANSLATION)
72 .description("The exact location of the point to use")
73 .available(false);
74 auto &p3 = b.add_input<decl::Vector>("Point 3"_ustr)
75 .default_value({1.0f, 1.0f, 0.0f})
76 .subtype(PROP_TRANSLATION)
77 .description("The exact location of the point to use")
78 .available(false);
79 auto &p4 = b.add_input<decl::Vector>("Point 4"_ustr)
80 .default_value({-1.0f, 1.0f, 0.0f})
81 .subtype(PROP_TRANSLATION)
82 .description("The exact location of the point to use")
83 .available(false);
84 b.add_output<decl::Geometry>("Curve"_ustr);
85
86 const bNode *node = b.node_or_null();
87 if (node != nullptr) {
88 const NodeGeometryCurvePrimitiveQuad &storage = node_storage(*node);
91 width.available(true);
92 height.available(true);
93 break;
95 width.available(true);
96 height.available(true);
97 offset.available(true);
98 break;
100 bottom.available(true);
101 top.available(true);
102 offset.available(true);
103 height.available(true);
104 break;
106 width.available(true);
107 bottom_height.available(true);
108 top_height.available(true);
109 break;
111 p1.available(true);
112 p2.available(true);
113 p3.available(true);
114 p4.available(true);
115 break;
116 }
117 }
118}
119
120static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
121{
122 layout.prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
123}
124
125static void node_init(bNodeTree * /*tree*/, bNode *node)
126{
127 NodeGeometryCurvePrimitiveQuad *data = MEM_new<NodeGeometryCurvePrimitiveQuad>(__func__);
129 node->storage = data;
130}
131
133 public:
137 {
138 bNode &node = params.add_node("GeometryNodeCurvePrimitiveQuadrilateral"_ustr);
139 node_storage(node).mode = quad_mode;
141 }
142};
143
145{
146 const NodeDeclaration &declaration = *params.node_type().static_declaration;
147 if (params.in_out() == SOCK_OUT) {
148 search_link_ops_for_declarations(params, declaration.outputs);
149 }
150 else if (params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_FLOAT)) {
151 params.add_item(IFACE_("Width"),
153 params.add_item(IFACE_("Height"),
155 params.add_item(
156 IFACE_("Bottom Width"),
158 params.add_item(
159 IFACE_("Top Width"),
161 params.add_item(
162 IFACE_("Offset"),
164 params.add_item(IFACE_("Point 1"),
166 }
167}
168
170 const float height,
171 const float width)
172{
173 positions[0] = float3(width / 2.0f, height / 2.0f, 0.0f);
174 positions[1] = float3(-width / 2.0f, height / 2.0f, 0.0f);
175 positions[2] = float3(-width / 2.0f, -height / 2.0f, 0.0f);
176 positions[3] = float3(width / 2.0f, -height / 2.0f, 0.0f);
177}
178
180 const float3 &p1,
181 const float3 &p2,
182 const float3 &p3,
183 const float3 &p4)
184{
185 positions[0] = p1;
186 positions[1] = p2;
187 positions[2] = p3;
188 positions[3] = p4;
189}
190
192 const float height,
193 const float width,
194 const float offset)
195{
196 positions[0] = float3(width / 2.0f + offset / 2.0f, height / 2.0f, 0.0f);
197 positions[1] = float3(-width / 2.0f + offset / 2.0f, height / 2.0f, 0.0f);
198 positions[2] = float3(-width / 2.0f - offset / 2.0f, -height / 2.0f, 0.0f);
199 positions[3] = float3(width / 2.0f - offset / 2.0f, -height / 2.0f, 0.0f);
200}
202 const float bottom,
203 const float top,
204 const float offset,
205 const float height)
206{
207 positions[0] = float3(top / 2.0f + offset, height / 2.0f, 0.0f);
208 positions[1] = float3(-top / 2.0f + offset, height / 2.0f, 0.0f);
209 positions[2] = float3(-bottom / 2.0f, -height / 2.0f, 0.0f);
210 positions[3] = float3(bottom / 2.0f, -height / 2.0f, 0.0f);
211}
212
214 const float width,
215 const float bottom_height,
216 const float top_height)
217{
218 positions[0] = float3(0, -bottom_height, 0);
219 positions[1] = float3(width / 2, 0, 0);
220 positions[2] = float3(0, top_height, 0);
221 positions[3] = float3(-width / 2.0f, 0, 0);
222}
223
225{
226 const NodeGeometryCurvePrimitiveQuad &storage = node_storage(params.node());
228
230 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
231 curves.cyclic_for_write().first() = true;
232
233 MutableSpan<float3> positions = curves.positions_for_write();
234
235 switch (mode) {
237 create_rectangle_curve(positions,
238 std::max(params.extract_input<float>("Height"_ustr), 0.0f),
239 std::max(params.extract_input<float>("Width"_ustr), 0.0f));
240 break;
241
244 std::max(params.extract_input<float>("Height"_ustr), 0.0f),
245 std::max(params.extract_input<float>("Width"_ustr), 0.0f),
246 params.extract_input<float>("Offset"_ustr));
247 break;
249 create_trapezoid_curve(positions,
250 std::max(params.extract_input<float>("Bottom Width"_ustr), 0.0f),
251 std::max(params.extract_input<float>("Top Width"_ustr), 0.0f),
252 params.extract_input<float>("Offset"_ustr),
253 std::max(params.extract_input<float>("Height"_ustr), 0.0f));
254 break;
256 create_kite_curve(positions,
257 std::max(params.extract_input<float>("Width"_ustr), 0.0f),
258 std::max(params.extract_input<float>("Bottom Height"_ustr), 0.0f),
259 params.extract_input<float>("Top Height"_ustr));
260 break;
262 create_points_curve(positions,
263 params.extract_input<float3>("Point 1"_ustr),
264 params.extract_input<float3>("Point 2"_ustr),
265 params.extract_input<float3>("Point 3"_ustr),
266 params.extract_input<float3>("Point 4"_ustr));
267 break;
268 default:
270 return;
271 }
272
273 params.set_output("Curve"_ustr, GeometrySet::from_curves(curves_id));
274}
275
276static void node_rna(StructRNA *srna)
277{
278 static EnumPropertyItem mode_items[] = {
280 "RECTANGLE",
281 0,
282 "Rectangle",
283 "Create a rectangle"},
285 "PARALLELOGRAM",
286 0,
287 "Parallelogram",
288 "Create a parallelogram"},
290 "TRAPEZOID",
291 0,
292 "Trapezoid",
293 "Create a trapezoid"},
294 {GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE, "KITE", 0, "Kite", "Create a Kite / Dart"},
296 "POINTS",
297 0,
298 "Points",
299 "Create a quadrilateral from four points"},
300 {0, nullptr, 0, nullptr, nullptr},
301 };
302
304 "mode",
305 "Mode",
306 "",
307 mode_items,
310}
311
312static void node_register()
313{
314 static bke::bNodeType ntype;
315 geo_node_type_base(&ntype,
316 "GeometryNodeCurvePrimitiveQuadrilateral"_ustr,
318 ntype.ui_name = "Quadrilateral";
319 ntype.ui_description = "Generate a polygon with four points";
320 ntype.enum_name_legacy = "CURVE_PRIMITIVE_QUADRILATERAL";
322 ntype.declare = node_declare;
325 ntype.initfunc = node_init;
327 "NodeGeometryCurvePrimitiveQuad",
332
333 node_rna(ntype.rna_ext.srna);
334}
336
337} // namespace blender::nodes::node_geo_curve_primitive_quadrilateral_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_CURVE_PRIMITIVE_QUADRILATERAL
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
Vector< SocketDeclaration * > outputs
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
Curves * curves_new_nomain_single(int points_num, CurveType type)
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
static void create_kite_curve(MutableSpan< float3 > positions, const float width, const float bottom_height, const float top_height)
static void create_rectangle_curve(MutableSpan< float3 > positions, const float height, const float width)
static void create_parallelogram_curve(MutableSpan< float3 > positions, const float height, const float width, const float offset)
static void create_points_curve(MutableSpan< float3 > positions, const float3 &p1, const float3 &p2, const float3 &p3, const float3 &p4)
static void create_trapezoid_curve(MutableSpan< float3 > positions, const float bottom, const float top, const float offset, const float height)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
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)
GeometryNodeCurvePrimitiveQuadMode
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_PARALLELOGRAM
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_TRAPEZOID
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_POINTS
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE
BMesh const char void * data
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_TRANSLATION
Definition RNA_types.hh:318
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
nodes::NodeDeclaration * static_declaration
Definition BKE_node.hh:402
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
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)