Blender V4.3
node_geo_curve_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_curves.hh"
6
7#include "BLI_math_geom.h"
8
9#include "NOD_rna_define.hh"
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
14#include "node_geometry_util.hh"
15
17
19
21{
22 auto endable_points = [](bNode &node) {
23 node_storage(node).mode = GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS;
24 };
25 auto enable_radius = [](bNode &node) {
26 node_storage(node).mode = GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS;
27 };
28
29 b.add_input<decl::Int>("Resolution")
30 .default_value(32)
31 .min(3)
32 .max(512)
33 .description("Number of points on the circle");
34 auto &start = b.add_input<decl::Vector>("Point 1")
35 .default_value({-1.0f, 0.0f, 0.0f})
36 .subtype(PROP_TRANSLATION)
38 "One of the three points on the circle. The point order determines the "
39 "circle's direction")
40 .make_available(endable_points);
41 auto &middle = b.add_input<decl::Vector>("Point 2")
42 .default_value({0.0f, 1.0f, 0.0f})
43 .subtype(PROP_TRANSLATION)
45 "One of the three points on the circle. The point order determines the "
46 "circle's direction")
47 .make_available(endable_points);
48 auto &end = b.add_input<decl::Vector>("Point 3")
49 .default_value({1.0f, 0.0f, 0.0f})
50 .subtype(PROP_TRANSLATION)
52 "One of the three points on the circle. The point order determines the "
53 "circle's direction")
54 .make_available(endable_points);
55 auto &radius = b.add_input<decl::Float>("Radius")
56 .default_value(1.0f)
57 .min(0.0f)
59 .description("Distance of the points from the origin")
60 .make_available(enable_radius);
61 b.add_output<decl::Geometry>("Curve");
62 auto &center = b.add_output<decl::Vector>("Center").make_available(endable_points);
63
64 const bNode *node = b.node_or_null();
65 if (node != nullptr) {
66 const NodeGeometryCurvePrimitiveCircle &storage = node_storage(*node);
68 storage.mode);
69
70 start.available(mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS);
71 middle.available(mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS);
73 center.available(mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS);
74
75 radius.available(mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS);
76 }
77}
78
79static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
80{
81 uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
82}
83
84static void node_init(bNodeTree * /*tree*/, bNode *node)
85{
86 NodeGeometryCurvePrimitiveCircle *data = MEM_cnew<NodeGeometryCurvePrimitiveCircle>(__func__);
87
89 node->storage = data;
90}
91
92static bool colinear_f3_f3_f3(const float3 p1, const float3 p2, const float3 p3)
93{
94 const float3 a = math::normalize(p2 - p1);
95 const float3 b = math::normalize(p3 - p1);
96 return ELEM(a, b, b * -1.0f);
97}
98
100 const float3 p1, const float3 p2, const float3 p3, const int resolution, float3 &r_center)
101{
102 if (colinear_f3_f3_f3(p1, p2, p3)) {
103 r_center = float3(0);
104 return nullptr;
105 }
106
107 float3 center;
108 /* Midpoints of `P1->P2` and `P2->P3`. */
109 const float3 q1 = math::interpolate(p1, p2, 0.5f);
110 const float3 q2 = math::interpolate(p2, p3, 0.5f);
111
112 /* Normal Vectors of `P1->P2` and `P2->P3` */
113 const float3 v1 = math::normalize(p2 - p1);
114 const float3 v2 = math::normalize(p3 - p2);
115
116 /* Normal of plane of main 2 segments P1->P2 and `P2->P3`. */
117 const float3 v3 = math::normalize(math::cross(v1, v2));
118
119 /* Normal of plane of first perpendicular bisector and `P1->P2`. */
120 const float3 v4 = math::normalize(math::cross(v3, v1));
121
122 /* Determine Center-point from the intersection of 3 planes. */
123 float plane_1[4], plane_2[4], plane_3[4];
124 plane_from_point_normal_v3(plane_1, q1, v3);
125 plane_from_point_normal_v3(plane_2, q1, v1);
126 plane_from_point_normal_v3(plane_3, q2, v2);
127
128 /* If the 3 planes do not intersect at one point, just return empty geometry. */
129 if (!isect_plane_plane_plane_v3(plane_1, plane_2, plane_3, center)) {
130 r_center = float3(0);
131 return nullptr;
132 }
133
134 Curves *curves_id = bke::curves_new_nomain_single(resolution, CURVE_TYPE_POLY);
135 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
136 curves.cyclic_for_write().first() = true;
137
138 MutableSpan<float3> positions = curves.positions_for_write();
139
140 /* Get the radius from the center-point to p1. */
141 const float r = math::distance(p1, center);
142 const float theta_step = ((2 * M_PI) / float(resolution));
143 for (const int i : IndexRange(resolution)) {
144
145 /* Formula for a circle around a point and 2 unit vectors perpendicular
146 * to each other and the axis of the circle from:
147 * https://math.stackexchange.com/questions/73237/parametric-equation-of-a-circle-in-3d-space
148 */
149
150 const float theta = theta_step * i;
151 positions[i] = center + r * sin(theta) * v1 + r * cos(theta) * v4;
152 }
153
154 r_center = center;
155 return curves_id;
156}
157
158static Curves *create_radius_circle_curve(const int resolution, const float radius)
159{
160 Curves *curves_id = bke::curves_new_nomain_single(resolution, CURVE_TYPE_POLY);
161 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
162 curves.cyclic_for_write().first() = true;
163
164 MutableSpan<float3> positions = curves.positions_for_write();
165
166 const float theta_step = (2.0f * M_PI) / float(resolution);
167 for (int i : IndexRange(resolution)) {
168 const float theta = theta_step * i;
169 const float x = radius * cos(theta);
170 const float y = radius * sin(theta);
171 positions[i] = float3(x, y, 0.0f);
172 }
173
174 return curves_id;
175}
176
178{
179 const NodeGeometryCurvePrimitiveCircle &storage = node_storage(params.node());
181 storage.mode;
182
183 Curves *curves = nullptr;
185 float3 center_point;
186 curves = create_point_circle_curve(params.extract_input<float3>("Point 1"),
187 params.extract_input<float3>("Point 2"),
188 params.extract_input<float3>("Point 3"),
189 std::max(params.extract_input<int>("Resolution"), 3),
190 center_point);
191 params.set_output("Center", center_point);
192 }
194 curves = create_radius_circle_curve(std::max(params.extract_input<int>("Resolution"), 3),
195 params.extract_input<float>("Radius"));
196 }
197
198 if (curves) {
199 params.set_output("Curve", GeometrySet::from_curves(curves));
200 }
201 else {
202 params.set_default_remaining_outputs();
203 }
204}
205
206static void node_rna(StructRNA *srna)
207{
208 static const EnumPropertyItem mode_items[] = {
210 "POINTS",
211 ICON_NONE,
212 "Points",
213 "Define the radius and location with three points"},
215 "RADIUS",
216 ICON_NONE,
217 "Radius",
218 "Define the radius with a float"},
219 {0, nullptr, 0, nullptr, nullptr},
220 };
221
223 "mode",
224 "Mode",
225 "Method used to determine radius and placement",
226 mode_items,
229}
230
231static void node_register()
232{
233 static blender::bke::bNodeType ntype;
234 geo_node_type_base(&ntype, GEO_NODE_CURVE_PRIMITIVE_CIRCLE, "Curve Circle", NODE_CLASS_GEOMETRY);
235
236 ntype.initfunc = node_init;
238 "NodeGeometryCurvePrimitiveCircle",
241 ntype.declare = node_declare;
245
246 node_rna(ntype.rna_ext.srna);
247}
249
250} // namespace blender::nodes::node_geo_curve_primitive_circle_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1799
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define M_PI
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:215
bool isect_plane_plane_plane_v3(const float plane_a[4], const float plane_b[4], const float plane_c[4], float r_isect_co[3]) ATTR_WARN_UNUSED_RESULT
#define ELEM(...)
@ CURVE_TYPE_POLY
GeometryNodeCurvePrimitiveCircleMode
@ GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS
@ GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
@ PROP_DISTANCE
Definition RNA_types.hh:159
@ PROP_TRANSLATION
Definition RNA_types.hh:164
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_EXPAND
ATTR_WARN_UNUSED_RESULT const BMVert * v2
local_group_size(16, 16) .push_constant(Type b
OperationNode * node
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float3 cos(float3 v)
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
Curves * curves_new_nomain_single(int points_num, CurveType type)
T distance(const T &a, const T &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static Curves * create_radius_circle_curve(const int resolution, const float radius)
static Curves * create_point_circle_curve(const float3 p1, const float3 p2, const float3 p3, const int resolution, float3 &r_center)
static bool colinear_f3_f3_f3(const float3 p1, const float3 p2, const float3 p3)
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, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
CurvesGeometry geometry
StructRNA * srna
Definition RNA_types.hh:780
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126