Blender V5.0
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")
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");
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(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
121{
122 layout->prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
123}
124
131
133 public:
134 std::string socket_name;
137 {
138 bNode &node = params.add_node("GeometryNodeCurvePrimitiveQuadrilateral");
139 node_storage(node).mode = quad_mode;
140 params.update_and_connect_available_socket(node, socket_name);
141 }
142};
143
145{
146 const NodeDeclaration &declaration = *params.node_type().static_declaration;
147 if (params.in_out() == SOCK_OUT) {
149 }
150 else if (params.node_tree().typeinfo->validate_link(
151 eNodeSocketDatatype(params.other_socket().type), SOCK_FLOAT))
152 {
153 params.add_item(IFACE_("Width"),
155 params.add_item(IFACE_("Height"),
157 params.add_item(IFACE_("Bottom Width"),
159 params.add_item(IFACE_("Top Width"),
161 params.add_item(IFACE_("Offset"),
163 params.add_item(IFACE_("Point 1"),
165 }
166}
167
169 const float height,
170 const float width)
171{
172 positions[0] = float3(width / 2.0f, height / 2.0f, 0.0f);
173 positions[1] = float3(-width / 2.0f, height / 2.0f, 0.0f);
174 positions[2] = float3(-width / 2.0f, -height / 2.0f, 0.0f);
175 positions[3] = float3(width / 2.0f, -height / 2.0f, 0.0f);
176}
177
179 const float3 &p1,
180 const float3 &p2,
181 const float3 &p3,
182 const float3 &p4)
183{
184 positions[0] = p1;
185 positions[1] = p2;
186 positions[2] = p3;
187 positions[3] = p4;
188}
189
191 const float height,
192 const float width,
193 const float offset)
194{
195 positions[0] = float3(width / 2.0f + offset / 2.0f, height / 2.0f, 0.0f);
196 positions[1] = float3(-width / 2.0f + offset / 2.0f, height / 2.0f, 0.0f);
197 positions[2] = float3(-width / 2.0f - offset / 2.0f, -height / 2.0f, 0.0f);
198 positions[3] = float3(width / 2.0f - offset / 2.0f, -height / 2.0f, 0.0f);
199}
201 const float bottom,
202 const float top,
203 const float offset,
204 const float height)
205{
206 positions[0] = float3(top / 2.0f + offset, height / 2.0f, 0.0f);
207 positions[1] = float3(-top / 2.0f + offset, height / 2.0f, 0.0f);
208 positions[2] = float3(-bottom / 2.0f, -height / 2.0f, 0.0f);
209 positions[3] = float3(bottom / 2.0f, -height / 2.0f, 0.0f);
210}
211
213 const float width,
214 const float bottom_height,
215 const float top_height)
216{
217 positions[0] = float3(0, -bottom_height, 0);
218 positions[1] = float3(width / 2, 0, 0);
219 positions[2] = float3(0, top_height, 0);
220 positions[3] = float3(-width / 2.0f, 0, 0);
221}
222
224{
225 const NodeGeometryCurvePrimitiveQuad &storage = node_storage(params.node());
227
229 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
230 curves.cyclic_for_write().first() = true;
231
232 MutableSpan<float3> positions = curves.positions_for_write();
233
234 switch (mode) {
236 create_rectangle_curve(positions,
237 std::max(params.extract_input<float>("Height"), 0.0f),
238 std::max(params.extract_input<float>("Width"), 0.0f));
239 break;
240
243 std::max(params.extract_input<float>("Height"), 0.0f),
244 std::max(params.extract_input<float>("Width"), 0.0f),
245 params.extract_input<float>("Offset"));
246 break;
248 create_trapezoid_curve(positions,
249 std::max(params.extract_input<float>("Bottom Width"), 0.0f),
250 std::max(params.extract_input<float>("Top Width"), 0.0f),
251 params.extract_input<float>("Offset"),
252 std::max(params.extract_input<float>("Height"), 0.0f));
253 break;
255 create_kite_curve(positions,
256 std::max(params.extract_input<float>("Width"), 0.0f),
257 std::max(params.extract_input<float>("Bottom Height"), 0.0f),
258 params.extract_input<float>("Top Height"));
259 break;
261 create_points_curve(positions,
262 params.extract_input<float3>("Point 1"),
263 params.extract_input<float3>("Point 2"),
264 params.extract_input<float3>("Point 3"),
265 params.extract_input<float3>("Point 4"));
266 break;
267 default:
268 params.set_default_remaining_outputs();
269 return;
270 }
271
272 params.set_output("Curve", GeometrySet::from_curves(curves_id));
273}
274
275static void node_rna(StructRNA *srna)
276{
277 static EnumPropertyItem mode_items[] = {
279 "RECTANGLE",
280 0,
281 "Rectangle",
282 "Create a rectangle"},
284 "PARALLELOGRAM",
285 0,
286 "Parallelogram",
287 "Create a parallelogram"},
289 "TRAPEZOID",
290 0,
291 "Trapezoid",
292 "Create a trapezoid"},
293 {GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE, "KITE", 0, "Kite", "Create a Kite / Dart"},
295 "POINTS",
296 0,
297 "Points",
298 "Create a quadrilateral from four points"},
299 {0, nullptr, 0, nullptr, nullptr},
300 };
301
303 "mode",
304 "Mode",
305 "",
306 mode_items,
309}
310
311static void node_register()
312{
313 static blender::bke::bNodeType ntype;
315 &ntype, "GeometryNodeCurvePrimitiveQuadrilateral", GEO_NODE_CURVE_PRIMITIVE_QUADRILATERAL);
316 ntype.ui_name = "Quadrilateral";
317 ntype.ui_description = "Generate a polygon with four points";
318 ntype.enum_name_legacy = "CURVE_PRIMITIVE_QUADRILATERAL";
320 ntype.declare = node_declare;
323 ntype.initfunc = node_init;
325 "NodeGeometryCurvePrimitiveQuad",
330
331 node_rna(ntype.rna_ext.srna);
332}
334
335} // namespace blender::nodes::node_geo_curve_primitive_quadrilateral_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_CURVE_PRIMITIVE_QUADRILATERAL
#define IFACE_(msgid)
@ SOCK_OUT
eNodeSocketDatatype
@ SOCK_FLOAT
GeometryNodeCurvePrimitiveQuadMode
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_TRAPEZOID
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_POINTS
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE
@ GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_PARALLELOGRAM
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
@ PROP_DISTANCE
Definition RNA_types.hh:256
@ PROP_TRANSLATION
Definition RNA_types.hh:261
#define UI_ITEM_NONE
BMesh const char void * data
constexpr T & first() const
Definition BLI_span.hh:679
MutableSpan< float3 > positions_for_write()
MutableSpan< bool > cyclic_for_write()
Vector< SocketDeclaration * > outputs
uint top
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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:5414
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
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)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
#define min(a, b)
Definition sort.cc:36
CurvesGeometry geometry
StructRNA * srna
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
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< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238