Geometry Nodes: Curve Primitive Quadrilateral

This commit adds a curve primitive node for creating squares,
rectangles, trapezoids, kites, and parallelograms. It also includes
a mode where the four points are just vector inputs.

Differential Revision: https://developer.blender.org/D11665
This commit is contained in:
Johnny Matthews 2021-07-12 13:10:56 -04:00 committed by Hans Goudey
parent a072e87e04
commit 2a41ab5e6c
Notes: blender-bot 2023-02-14 06:25:25 +01:00
Referenced by issue #89220, Curve Primitive Nodes
9 changed files with 300 additions and 0 deletions

View File

@ -516,6 +516,7 @@ geometry_node_categories = [
NodeItem("GeometryNodeCurveStar"),
NodeItem("GeometryNodeCurveSpiral"),
NodeItem("GeometryNodeCurveQuadraticBezier"),
NodeItem("GeometryNodeCurvePrimitiveQuadrilateral"),
NodeItem("GeometryNodeCurvePrimitiveBezierSegment"),
]),
GeometryNodeCategory("GEO_GEOMETRY", "Geometry", items=[
@ -559,6 +560,7 @@ geometry_node_categories = [
NodeItem("GeometryNodeMeshLine"),
NodeItem("GeometryNodeMeshUVSphere"),
]),
GeometryNodeCategory("GEO_POINT", "Point", items=[
NodeItem("GeometryNodePointDistribute"),
NodeItem("GeometryNodePointInstance"),

View File

@ -1458,6 +1458,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
#define GEO_NODE_VIEWER 1067
#define GEO_NODE_CURVE_PRIMITIVE_LINE 1068
#define GEO_NODE_CURVE_ENDPOINTS 1069
#define GEO_NODE_CURVE_PRIMITIVE_QUADRILATERAL 1070
/** \} */

View File

@ -5112,6 +5112,7 @@ static void registerGeometryNodes()
register_node_type_geo_curve_primitive_circle();
register_node_type_geo_curve_primitive_line();
register_node_type_geo_curve_primitive_quadratic_bezier();
register_node_type_geo_curve_primitive_quadrilateral();
register_node_type_geo_curve_primitive_spiral();
register_node_type_geo_curve_primitive_star();
register_node_type_geo_curve_resample();

View File

@ -1370,6 +1370,11 @@ typedef struct NodeGeometryCurvePrimitiveCircle {
uint8_t mode;
} NodeGeometryCurvePrimitiveCircle;
typedef struct NodeGeometryCurvePrimitiveQuad {
/* GeometryNodeCurvePrimitiveQuadMode. */
uint8_t mode;
} NodeGeometryCurvePrimitiveQuad;
typedef struct NodeGeometryCurveResample {
/* GeometryNodeCurveSampleMode. */
uint8_t mode;
@ -1920,6 +1925,14 @@ typedef enum GeometryNodeCurvePrimitiveLineMode {
GEO_NODE_CURVE_PRIMITIVE_LINE_MODE_DIRECTION = 1
} GeometryNodeCurvePrimitiveLineMode;
typedef enum GeometryNodeCurvePrimitiveQuadMode {
GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE = 0,
GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_PARALLELOGRAM = 1,
GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_TRAPEZOID = 2,
GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE = 3,
GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_POINTS = 4,
} GeometryNodeCurvePrimitiveQuadMode;
typedef enum GeometryNodeCurvePrimitiveBezierSegmentMode {
GEO_NODE_CURVE_PRIMITIVE_BEZIER_SEGMENT_POSITION = 0,
GEO_NODE_CURVE_PRIMITIVE_BEZIER_SEGMENT_OFFSET = 1,

View File

@ -9947,6 +9947,45 @@ static void def_geo_switch(StructRNA *srna)
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
static void def_geo_curve_primitive_quadrilateral(StructRNA *srna)
{
PropertyRNA *prop;
static EnumPropertyItem mode_items[] = {
{GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE,
"RECTANGLE",
0,
"Rectangle",
"Create a rectangle"},
{GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_PARALLELOGRAM,
"PARALLELOGRAM",
0,
"Parallelogram",
"Create a parallelogram"},
{GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_TRAPEZOID,
"TRAPEZOID",
0,
"Trapezoid",
"Create a trapezoid"},
{GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE, "KITE", 0, "Kite", "Create a Kite / Dart"},
{GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_POINTS,
"POINTS",
0,
"Points",
"Create a quadrilateral from four points"},
{0, NULL, 0, NULL, NULL},
};
RNA_def_struct_sdna_from(srna, "NodeGeometryCurvePrimitiveQuad", "storage");
prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "mode");
RNA_def_property_enum_items(prop, mode_items);
RNA_def_property_enum_default(prop, GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE);
RNA_def_property_ui_text(prop, "Mode", "");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
static void def_geo_curve_resample(StructRNA *srna)
{
PropertyRNA *prop;

View File

@ -170,6 +170,7 @@ set(SRC
geometry/nodes/node_geo_curve_primitive_circle.cc
geometry/nodes/node_geo_curve_primitive_line.cc
geometry/nodes/node_geo_curve_primitive_quadratic_bezier.cc
geometry/nodes/node_geo_curve_primitive_quadrilateral.cc
geometry/nodes/node_geo_curve_primitive_spiral.cc
geometry/nodes/node_geo_curve_primitive_star.cc
geometry/nodes/node_geo_curve_resample.cc

View File

@ -57,6 +57,7 @@ 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_line(void);
void register_node_type_geo_curve_primitive_quadratic_bezier(void);
void register_node_type_geo_curve_primitive_quadrilateral(void);
void register_node_type_geo_curve_primitive_spiral(void);
void register_node_type_geo_curve_primitive_star(void);
void register_node_type_geo_curve_resample(void);

View File

@ -295,6 +295,7 @@ DefNode(GeometryNode, GEO_NODE_CURVE_LENGTH, 0, "CURVE_LENGTH", CurveLength, "Cu
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, "Curve Circle", "")
DefNode(GeometryNode, GEO_NODE_CURVE_PRIMITIVE_LINE, def_geo_curve_primitive_line, "CURVE_PRIMITIVE_LINE", CurvePrimitiveLine, "Curve Line", "")
DefNode(GeometryNode, GEO_NODE_CURVE_PRIMITIVE_QUADRILATERAL, def_geo_curve_primitive_quadrilateral, "CURVE_PRIMITIVE_QUADRILATERAL", CurvePrimitiveQuadrilateral, "Quadrilateral", "")
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,241 @@
/*
* 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_quadrilateral_in[] = {
{SOCK_FLOAT, N_("Width"), 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE},
{SOCK_FLOAT, N_("Height"), 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE},
{SOCK_FLOAT, N_("Bottom Width"), 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE},
{SOCK_FLOAT, N_("Top Width"), 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE},
{SOCK_FLOAT, N_("Offset"), 1.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_DISTANCE},
{SOCK_FLOAT, N_("Bottom Height"), 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE},
{SOCK_FLOAT, N_("Top Height"), 1.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_DISTANCE},
{SOCK_VECTOR, N_("Point 1"), -1.0f, 1.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_DISTANCE},
{SOCK_VECTOR, N_("Point 2"), 1.0f, 1.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_DISTANCE},
{SOCK_VECTOR, N_("Point 3"), 1.0f, -1.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_DISTANCE},
{SOCK_VECTOR, N_("Point 4"), -1.0f, -1.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_DISTANCE},
{-1, ""},
};
static bNodeSocketTemplate geo_node_curve_primitive_quadrilateral_out[] = {
{SOCK_GEOMETRY, N_("Curve")},
{-1, ""},
};
static void geo_node_curve_primitive_quadrilateral_layout(uiLayout *layout,
bContext *UNUSED(C),
PointerRNA *ptr)
{
uiItemR(layout, ptr, "mode", 0, "", ICON_NONE);
}
static void geo_node_curve_primitive_quadrilateral_init(bNodeTree *UNUSED(tree), bNode *node)
{
NodeGeometryCurvePrimitiveQuad *data = (NodeGeometryCurvePrimitiveQuad *)MEM_callocN(
sizeof(NodeGeometryCurvePrimitiveQuad), __func__);
data->mode = GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE;
node->storage = data;
}
namespace blender::nodes {
static void geo_node_curve_primitive_quadrilateral_update(bNodeTree *UNUSED(ntree), bNode *node)
{
NodeGeometryCurvePrimitiveQuad &node_storage = *(NodeGeometryCurvePrimitiveQuad *)node->storage;
GeometryNodeCurvePrimitiveQuadMode mode = static_cast<GeometryNodeCurvePrimitiveQuadMode>(
node_storage.mode);
bNodeSocket *width = ((bNodeSocket *)node->inputs.first);
bNodeSocket *height = width->next;
bNodeSocket *bottom = height->next;
bNodeSocket *top = bottom->next;
bNodeSocket *offset = top->next;
bNodeSocket *bottom_height = offset->next;
bNodeSocket *top_height = bottom_height->next;
bNodeSocket *p1 = top_height->next;
bNodeSocket *p2 = p1->next;
bNodeSocket *p3 = p2->next;
bNodeSocket *p4 = p3->next;
LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) {
nodeSetSocketAvailability(sock, false);
}
if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE) {
nodeSetSocketAvailability(width, true);
nodeSetSocketAvailability(height, true);
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_PARALLELOGRAM) {
nodeSetSocketAvailability(width, true);
nodeSetSocketAvailability(height, true);
nodeSetSocketAvailability(offset, true);
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_TRAPEZOID) {
nodeSetSocketAvailability(bottom, true);
nodeSetSocketAvailability(top, true);
nodeSetSocketAvailability(offset, true);
nodeSetSocketAvailability(height, true);
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE) {
nodeSetSocketAvailability(width, true);
nodeSetSocketAvailability(bottom_height, true);
nodeSetSocketAvailability(top_height, true);
}
else if (mode == GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_POINTS) {
nodeSetSocketAvailability(p1, true);
nodeSetSocketAvailability(p2, true);
nodeSetSocketAvailability(p3, true);
nodeSetSocketAvailability(p4, true);
}
}
static void create_rectangle_curve(MutableSpan<float3> positions,
const float height,
const float width)
{
positions[0] = float3(width / 2.0f, -height / 2.0f, 0.0f);
positions[1] = float3(-width / 2.0f, -height / 2.0f, 0.0f);
positions[2] = float3(-width / 2.0f, height / 2.0f, 0.0f);
positions[3] = float3(width / 2.0f, height / 2.0f, 0.0f);
}
static void create_points_curve(MutableSpan<float3> positions,
const float3 &p1,
const float3 &p2,
const float3 &p3,
const float3 &p4)
{
positions[0] = p1;
positions[1] = p2;
positions[2] = p3;
positions[3] = p4;
}
static void create_parallelogram_curve(MutableSpan<float3> positions,
const float height,
const float width,
const float offset)
{
positions[0] = float3(width / 2.0f - offset / 2.0f, -height / 2.0f, 0.0f);
positions[1] = float3(-width / 2.0f - offset / 2.0f, -height / 2.0f, 0.0f);
positions[2] = float3(-width / 2.0f + offset / 2.0f, height / 2.0f, 0.0f);
positions[3] = float3(width / 2.0f + offset / 2.0f, height / 2.0f, 0.0f);
}
static void create_trapezoid_curve(MutableSpan<float3> positions,
const float bottom,
const float top,
const float offset,
const float height)
{
positions[0] = float3(bottom / 2.0f, -height / 2.0f, 0.0f);
positions[1] = float3(-bottom / 2.0f, -height / 2.0f, 0.0f);
positions[2] = float3(-top / 2.0f + offset, height / 2.0f, 0.0f);
positions[3] = float3(top / 2.0f + offset, height / 2.0f, 0.0f);
}
static void create_kite_curve(MutableSpan<float3> positions,
const float width,
const float bottom_height,
const float top_height)
{
positions[0] = float3(-width / 2.0f, 0, 0);
positions[1] = float3(0, top_height, 0);
positions[2] = float3(width / 2, 0, 0);
positions[3] = float3(0, -bottom_height, 0);
}
static void geo_node_curve_primitive_quadrilateral_exec(GeoNodeExecParams params)
{
const NodeGeometryCurvePrimitiveQuad &node_storage =
*(NodeGeometryCurvePrimitiveQuad *)(params.node()).storage;
const GeometryNodeCurvePrimitiveQuadMode mode = static_cast<GeometryNodeCurvePrimitiveQuadMode>(
node_storage.mode);
std::unique_ptr<CurveEval> curve = std::make_unique<CurveEval>();
std::unique_ptr<PolySpline> spline = std::make_unique<PolySpline>();
spline->resize(4);
spline->radii().fill(1.0f);
spline->tilts().fill(0.0f);
spline->set_cyclic(true);
MutableSpan<float3> positions = spline->positions();
switch (mode) {
case GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_RECTANGLE:
create_rectangle_curve(positions,
std::max(params.extract_input<float>("Height"), 0.0f),
std::max(params.extract_input<float>("Width"), 0.0f));
break;
case GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_PARALLELOGRAM:
create_parallelogram_curve(positions,
std::max(params.extract_input<float>("Height"), 0.0f),
std::max(params.extract_input<float>("Width"), 0.0f),
params.extract_input<float>("Offset"));
break;
case GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_TRAPEZOID:
create_trapezoid_curve(positions,
std::max(params.extract_input<float>("Bottom Width"), 0.0f),
std::max(params.extract_input<float>("Top Width"), 0.0f),
params.extract_input<float>("Offset"),
std::max(params.extract_input<float>("Height"), 0.0f));
break;
case GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_KITE:
create_kite_curve(positions,
std::max(params.extract_input<float>("Width"), 0.0f),
std::max(params.extract_input<float>("Bottom Height"), 0.0f),
params.extract_input<float>("Top Height"));
break;
case GEO_NODE_CURVE_PRIMITIVE_QUAD_MODE_POINTS:
create_points_curve(positions,
params.extract_input<float3>("Point 1"),
params.extract_input<float3>("Point 2"),
params.extract_input<float3>("Point 3"),
params.extract_input<float3>("Point 4"));
break;
default:
params.set_output("Curve", GeometrySet());
return;
}
curve->add_spline(std::move(spline));
curve->attributes.reallocate(curve->splines().size());
params.set_output("Curve", GeometrySet::create_with_curve(curve.release()));
}
} // namespace blender::nodes
void register_node_type_geo_curve_primitive_quadrilateral()
{
static bNodeType ntype;
geo_node_type_base(
&ntype, GEO_NODE_CURVE_PRIMITIVE_QUADRILATERAL, "Quadrilateral", NODE_CLASS_GEOMETRY, 0);
node_type_socket_templates(&ntype,
geo_node_curve_primitive_quadrilateral_in,
geo_node_curve_primitive_quadrilateral_out);
ntype.geometry_node_execute = blender::nodes::geo_node_curve_primitive_quadrilateral_exec;
ntype.draw_buttons = geo_node_curve_primitive_quadrilateral_layout;
node_type_update(&ntype, blender::nodes::geo_node_curve_primitive_quadrilateral_update);
node_type_init(&ntype, geo_node_curve_primitive_quadrilateral_init);
node_type_storage(&ntype,
"NodeGeometryCurvePrimitiveQuad",
node_free_standard_storage,
node_copy_standard_storage);
nodeRegisterType(&ntype);
}