Geometry Nodes: Curve Primitive Circle

This node has two modes: the first mode computes a circle from three
locations and a resolution. The second takes radius and resolution.
The first mode also outputs the center of the computed circle as
a vector.

Differential Revision: https://developer.blender.org/D11650
This commit is contained in:
Johnny Matthews 2021-06-30 19:17:28 -05:00 committed by Hans Goudey
parent 5a64c687dd
commit c1fc180861
Notes: blender-bot 2023-02-14 11:35:46 +01:00
Referenced by issue #89220, Curve Primitive Nodes
9 changed files with 269 additions and 0 deletions

View File

@ -510,6 +510,7 @@ geometry_node_categories = [
NodeItem("GeometryNodeCurveReverse"),
]),
GeometryNodeCategory("GEO_PRIMITIVES_CURVE", "Curve Primitives", items=[
NodeItem("GeometryNodeCurvePrimitiveCircle"),
NodeItem("GeometryNodeCurveStar"),
NodeItem("GeometryNodeCurveSpiral"),
NodeItem("GeometryNodeCurveQuadraticBezier"),

View File

@ -1443,6 +1443,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
#define GEO_NODE_CURVE_PRIMITIVE_SPIRAL 1063
#define GEO_NODE_CURVE_PRIMITIVE_QUADRATIC_BEZIER 1064
#define GEO_NODE_CURVE_PRIMITIVE_BEZIER_SEGMENT 1065
#define GEO_NODE_CURVE_PRIMITIVE_CIRCLE 1066
/** \} */

View File

@ -5055,6 +5055,7 @@ static void registerGeometryNodes()
register_node_type_geo_convex_hull();
register_node_type_geo_curve_length();
register_node_type_geo_curve_primitive_bezier_segment();
register_node_type_geo_curve_primitive_circle();
register_node_type_geo_curve_primitive_quadratic_bezier();
register_node_type_geo_curve_primitive_spiral();
register_node_type_geo_curve_primitive_star();

View File

@ -1362,6 +1362,11 @@ typedef struct NodeGeometryCurvePrimitiveBezierSegment {
uint8_t mode;
} NodeGeometryCurvePrimitiveBezierSegment;
typedef struct NodeGeometryCurvePrimitiveCircle {
/* GeometryNodeCurvePrimitiveMode. */
uint8_t mode;
} NodeGeometryCurvePrimitiveCircle;
typedef struct NodeGeometryCurveResample {
/* GeometryNodeCurveSampleMode. */
uint8_t mode;
@ -1795,6 +1800,11 @@ typedef enum GeometryNodeBooleanOperation {
GEO_NODE_BOOLEAN_DIFFERENCE = 2,
} GeometryNodeBooleanOperation;
typedef enum GeometryNodeCurvePrimitiveCircleMode {
GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS = 0,
GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS = 1
} GeometryNodeCurvePrimitiveCircleMode;
typedef enum GeometryNodeTriangulateNGons {
GEO_NODE_TRIANGULATE_NGON_BEAUTY = 0,
GEO_NODE_TRIANGULATE_NGON_EARCLIP = 1,

View File

@ -9425,6 +9425,32 @@ static void def_geo_attribute_vector_rotate(StructRNA *srna)
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
static void def_geo_curve_primitive_circle(StructRNA *srna)
{
static const EnumPropertyItem mode_items[] = {
{GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS,
"POINTS",
ICON_NONE,
"Points",
"Define the radius and location with three points"},
{GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS,
"RADIUS",
ICON_NONE,
"Radius",
"Define the radius with a float"},
{0, NULL, 0, NULL, NULL},
};
PropertyRNA *prop;
RNA_def_struct_sdna_from(srna, "NodeGeometryCurvePrimitiveCircle", "storage");
prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, mode_items);
RNA_def_property_ui_text(prop, "Mode", "Method used to determine radius and placement");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
static void def_geo_point_rotate(StructRNA *srna)
{
static const EnumPropertyItem type_items[] = {

View File

@ -165,6 +165,7 @@ set(SRC
geometry/nodes/node_geo_convex_hull.cc
geometry/nodes/node_geo_curve_length.cc
geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
geometry/nodes/node_geo_curve_primitive_circle.cc
geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc
geometry/nodes/node_geo_curve_primitive_spiral.cc
geometry/nodes/node_geo_curve_primitive_star.cc

View File

@ -53,6 +53,7 @@ void register_node_type_geo_collection_info(void);
void register_node_type_geo_convex_hull(void);
void register_node_type_geo_curve_length(void);
void register_node_type_geo_curve_primitive_bezier_segment(void);
void register_node_type_geo_curve_primitive_circle(void);
void register_node_type_geo_curve_primitive_quadratic_bezier(void);
void register_node_type_geo_curve_primitive_spiral(void);
void register_node_type_geo_curve_primitive_star(void);

View File

@ -292,6 +292,7 @@ DefNode(GeometryNode, GEO_NODE_COLLECTION_INFO, def_geo_collection_info, "COLLEC
DefNode(GeometryNode, GEO_NODE_CONVEX_HULL, 0, "CONVEX_HULL", ConvexHull, "Convex Hull", "")
DefNode(GeometryNode, GEO_NODE_CURVE_LENGTH, 0, "CURVE_LENGTH", CurveLength, "Curve Length", "")
DefNode(GeometryNode, GEO_NODE_CURVE_PRIMITIVE_BEZIER_SEGMENT, def_geo_curve_primitive_bezier_segment, "CURVE_PRIMITIVE_BEZIER_SEGMENT", CurvePrimitiveBezierSegment, "Bezier Segment", "")
DefNode(GeometryNode, GEO_NODE_CURVE_PRIMITIVE_CIRCLE, def_geo_curve_primitive_circle, "CURVE_PRIMITIVE_CIRCLE", CurvePrimitiveCircle, "Circle", "")
DefNode(GeometryNode, GEO_NODE_CURVE_PRIMITIVE_QUADRATIC_BEZIER, 0, "CURVE_PRIMITIVE_QUADRATIC_BEZIER", CurveQuadraticBezier, "Quadratic Bezier", "")
DefNode(GeometryNode, GEO_NODE_CURVE_PRIMITIVE_STAR, 0, "CURVE_PRIMITIVE_STAR", CurveStar, "Star", "")
DefNode(GeometryNode, GEO_NODE_CURVE_PRIMITIVE_SPIRAL, 0, "CURVE_PRIMITIVE_SPIRAL", CurveSpiral, "Curve Spiral", "")

View File

@ -0,0 +1,227 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "BKE_spline.hh"
#include "UI_interface.h"
#include "UI_resources.h"
#include "node_geometry_util.hh"
static bNodeSocketTemplate geo_node_curve_primitive_circle_in[] = {
{SOCK_INT, N_("Resolution"), 32.0f, 0.0f, 0.0f, 0.0f, 3.0f, 512.0f},
{SOCK_VECTOR, N_("Point 1"), -1.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_TRANSLATION},
{SOCK_VECTOR, N_("Point 2"), 0.0f, 1.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_TRANSLATION},
{SOCK_VECTOR, N_("Point 3"), 1.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_TRANSLATION},
{SOCK_FLOAT, N_("Radius"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE},
{-1, ""},
};
static bNodeSocketTemplate geo_node_curve_primitive_circle_out[] = {
{SOCK_GEOMETRY, N_("Curve")},
{SOCK_VECTOR, N_("Center")},
{-1, ""},
};
static void geo_node_curve_primitive_circle_layout(uiLayout *layout,
bContext *UNUSED(C),
PointerRNA *ptr)
{
uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
}
namespace blender::nodes {
static void geo_node_curve_primitive_circle_init(bNodeTree *UNUSED(tree), bNode *node)
{
NodeGeometryCurvePrimitiveCircle *data = (NodeGeometryCurvePrimitiveCircle *)MEM_callocN(
sizeof(NodeGeometryCurvePrimitiveCircle), __func__);
data->mode = GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS;
node->storage = data;
}
static void geo_node_curve_primitive_circle_update(bNodeTree *UNUSED(ntree), bNode *node)
{
const NodeGeometryCurvePrimitiveCircle *node_storage = (NodeGeometryCurvePrimitiveCircle *)
node->storage;
const GeometryNodeCurvePrimitiveCircleMode mode = (const GeometryNodeCurvePrimitiveCircleMode)
node_storage->mode;
bNodeSocket *start_socket = ((bNodeSocket *)node->inputs.first)->next;
bNodeSocket *middle_socket = start_socket->next;
bNodeSocket *end_socket = middle_socket->next;
bNodeSocket *radius_socket = end_socket->next;
bNodeSocket *center_socket = ((bNodeSocket *)node->outputs.first)->next;
nodeSetSocketAvailability(start_socket, mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS);
nodeSetSocketAvailability(middle_socket, mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS);
nodeSetSocketAvailability(end_socket, mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS);
nodeSetSocketAvailability(center_socket, mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS);
nodeSetSocketAvailability(radius_socket, mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS);
}
static bool colinear_f3_f3_f3(const float3 p1, const float3 p2, const float3 p3)
{
const float3 a = (p2 - p1).normalized();
const float3 b = (p3 - p1).normalized();
return (a == b || a == b * -1.0f);
}
static std::unique_ptr<CurveEval> create_point_circle_curve(
const float3 p1, const float3 p2, const float3 p3, const int resolution, float center_out[3])
{
if (colinear_f3_f3_f3(p1, p2, p3)) {
return nullptr;
}
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
spline->resize(resolution);
MutableSpan<float3> positions = spline->positions();
float3 center;
/* Midpoints of P1->P2 and P2->P3. */
const float3 q1 = float3::interpolate(p1, p2, 0.5f);
const float3 q2 = float3::interpolate(p2, p3, 0.5f);
/* Normal Vectors of P1->P2 and P2->P3*/
const float3 v1 = (p2 - p1).normalized();
const float3 v2 = (p3 - p2).normalized();
/*Normal of plane of main 2 segments P1->P2 and P2->P3. */
const float3 v3 = float3::cross(v1, v2).normalized();
/*Normal of plane of first perpendicular bisector and P1->P2. */
const float3 v4 = float3::cross(v3, v1).normalized();
/* Determine Centerpoint from the intersection of 3 planes. */
float plane_1[4], plane_2[4], plane_3[4];
plane_from_point_normal_v3(plane_1, q1, v3);
plane_from_point_normal_v3(plane_2, q1, v1);
plane_from_point_normal_v3(plane_3, q2, v2);
/* If the 3 planes do not intersect at one point, just return empty geometry. */
if (!isect_plane_plane_plane_v3(plane_1, plane_2, plane_3, center)) {
return nullptr;
}
/* Get the radius from the centerpoint to p1. */
const float r = float3::distance(p1, center);
const float theta_step = ((2 * M_PI) / (float)resolution);
for (const int i : IndexRange(resolution)) {
/* Formula for a circle around a point and 2 unit vectors perp. to each other and the axis of
* the cirlce from
* https://math.stackexchange.com/questions/73237/parametric-equation-of-a-circle-in-3d-space
*/
const float theta = theta_step * i;
positions[i] = center + r * cos(theta) * v1 + r * sin(theta) * v4;
}
spline->radii().fill(1.0f);
spline->tilts().fill(0.0f);
spline->set_cyclic(true);
curve->add_spline(std::move(spline));
curve->attributes.reallocate(curve->splines().size());
copy_v3_v3(center_out, center);
return curve;
}
static std::unique_ptr<CurveEval> create_radius_circle_curve(const int resolution,
const float radius)
{
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
spline->resize(resolution);
MutableSpan<float3> positions = spline->positions();
const float theta_step = (2.0f * M_PI) / float(resolution);
for (int i : IndexRange(resolution)) {
const float theta = theta_step * i;
const float x = radius * cos(theta);
const float y = radius * sin(theta);
positions[i] = float3(x, y, 0.0f);
}
spline->radii().fill(1.0f);
spline->tilts().fill(0.0f);
spline->set_cyclic(true);
curve->add_spline(std::move(spline));
curve->attributes.reallocate(curve->splines().size());
return curve;
}
static void geo_node_curve_primitive_circle_exec(GeoNodeExecParams params)
{
const NodeGeometryCurvePrimitiveCircle *node_storage =
(NodeGeometryCurvePrimitiveCircle *)params.node().storage;
const GeometryNodeCurvePrimitiveCircleMode mode = (GeometryNodeCurvePrimitiveCircleMode)
node_storage->mode;
std::unique_ptr<CurveEval> curve;
if (mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_POINTS) {
float center_point[3];
curve = create_point_circle_curve(params.extract_input<float3>("Point 1"),
params.extract_input<float3>("Point 2"),
params.extract_input<float3>("Point 3"),
std::max(params.extract_input<int>("Resolution"), 3),
center_point);
if (curve) {
params.set_output("Center", float3(center_point));
}
else {
params.set_output("Center", float3(0, 0, 0));
}
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_CIRCLE_TYPE_RADIUS) {
curve = create_radius_circle_curve(std::max(params.extract_input<int>("Resolution"), 3),
params.extract_input<float>("Radius"));
}
if (curve) {
params.set_output("Curve", GeometrySet::create_with_curve(curve.release()));
}
else {
params.set_output("Curve", GeometrySet());
}
}
} // namespace blender::nodes
void register_node_type_geo_curve_primitive_circle()
{
static bNodeType ntype;
geo_node_type_base(&ntype, GEO_NODE_CURVE_PRIMITIVE_CIRCLE, "Circle", NODE_CLASS_GEOMETRY, 0);
node_type_socket_templates(
&ntype, geo_node_curve_primitive_circle_in, geo_node_curve_primitive_circle_out);
node_type_init(&ntype, blender::nodes::geo_node_curve_primitive_circle_init);
node_type_update(&ntype, blender::nodes::geo_node_curve_primitive_circle_update);
node_type_storage(&ntype,
"NodeGeometryCurvePrimitiveCircle",
node_free_standard_storage,
node_copy_standard_storage);
ntype.geometry_node_execute = blender::nodes::geo_node_curve_primitive_circle_exec;
ntype.draw_buttons = geo_node_curve_primitive_circle_layout;
nodeRegisterType(&ntype);
}