Geometry Nodes: Curve Fill Node

This node takes a curve geometry input and creates a filled mesh at Z=0
using a constrained Delaunay triangulation algorithm. Because of the
choice of algorithm, the results should be higher quality than the
filling for 2D curve objects.

This commit adds an initial fairly simple version of the node, but more
features may be added in the future, like transferring attributes when
necessary, or an index attribute input to break up the calculations
into smaller chunks to improve performance.

Differential Revision: https://developer.blender.org/D11846
This commit is contained in:
Erik Abrahamsson 2021-08-29 23:27:35 -05:00 committed by Hans Goudey
parent 41eb33794c
commit a71d2b2601
9 changed files with 211 additions and 0 deletions

View File

@ -508,6 +508,7 @@ geometry_node_categories = [
NodeItem("GeometryNodeMeshToCurve"),
NodeItem("GeometryNodeCurveToPoints"),
NodeItem("GeometryNodeCurveEndpoints"),
NodeItem("GeometryNodeCurveFill"),
NodeItem("GeometryNodeCurveTrim"),
NodeItem("GeometryNodeCurveLength"),
NodeItem("GeometryNodeCurveReverse"),

View File

@ -1472,6 +1472,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
#define GEO_NODE_CURVE_SET_HANDLES 1072
#define GEO_NODE_CURVE_SPLINE_TYPE 1073
#define GEO_NODE_CURVE_SELECT_HANDLES 1074
#define GEO_NODE_CURVE_FILL 1075
/** \} */

View File

@ -5138,6 +5138,7 @@ static void registerGeometryNodes()
register_node_type_geo_collection_info();
register_node_type_geo_convex_hull();
register_node_type_geo_curve_endpoints();
register_node_type_geo_curve_fill();
register_node_type_geo_curve_length();
register_node_type_geo_curve_primitive_bezier_segment();
register_node_type_geo_curve_primitive_circle();

View File

@ -1437,6 +1437,10 @@ typedef struct NodeGeometryRaycast {
char _pad[1];
} NodeGeometryRaycast;
typedef struct NodeGeometryCurveFill {
uint8_t mode;
} NodeGeometryCurveFill;
/* script node mode */
#define NODE_SCRIPT_INTERNAL 0
#define NODE_SCRIPT_EXTERNAL 1
@ -2008,6 +2012,11 @@ typedef enum GeometryNodeRaycastMapMode {
GEO_NODE_RAYCAST_NEAREST = 1,
} GeometryNodeRaycastMapMode;
typedef enum GeometryNodeCurveFillMode {
GEO_NODE_CURVE_FILL_MODE_TRIANGULATED = 0,
GEO_NODE_CURVE_FILL_MODE_NGONS = 1,
} GeometryNodeCurveFillMode;
#ifdef __cplusplus
}
#endif

View File

@ -10261,6 +10261,24 @@ static void def_geo_raycast(StructRNA *srna)
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
static void def_geo_curve_fill(StructRNA *srna)
{
static const EnumPropertyItem mode_items[] = {
{GEO_NODE_CURVE_FILL_MODE_TRIANGULATED, "TRIANGLES", 0, "Triangles", ""},
{GEO_NODE_CURVE_FILL_MODE_NGONS, "NGONS", 0, "N-gons", ""},
{0, NULL, 0, NULL, NULL},
};
PropertyRNA *prop;
RNA_def_struct_sdna_from(srna, "NodeGeometryCurveFill", "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_ui_text(prop, "Mode", "");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
}
/* -------------------------------------------------------------------------- */
static void rna_def_shader_node(BlenderRNA *brna)

View File

@ -165,6 +165,7 @@ set(SRC
geometry/nodes/node_geo_common.cc
geometry/nodes/node_geo_convex_hull.cc
geometry/nodes/node_geo_curve_endpoints.cc
geometry/nodes/node_geo_curve_fill.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

View File

@ -52,6 +52,7 @@ void register_node_type_geo_bounding_box(void);
void register_node_type_geo_collection_info(void);
void register_node_type_geo_convex_hull(void);
void register_node_type_geo_curve_endpoints(void);
void register_node_type_geo_curve_fill(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);

View File

@ -292,6 +292,7 @@ DefNode(GeometryNode, GEO_NODE_BOUNDING_BOX, 0, "BOUNDING_BOX", BoundBox, "Bound
DefNode(GeometryNode, GEO_NODE_COLLECTION_INFO, def_geo_collection_info, "COLLECTION_INFO", CollectionInfo, "Collection Info", "")
DefNode(GeometryNode, GEO_NODE_CONVEX_HULL, 0, "CONVEX_HULL", ConvexHull, "Convex Hull", "")
DefNode(GeometryNode, GEO_NODE_CURVE_ENDPOINTS, 0, "CURVE_ENDPOINTS", CurveEndpoints, "Curve Endpoints", "")
DefNode(GeometryNode, GEO_NODE_CURVE_FILL, def_geo_curve_fill, "CURVE_FILL", CurveFill, "Curve Fill", "")
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, "Curve Circle", "")

View File

@ -0,0 +1,178 @@
/*
* 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 "BLI_array.hh"
#include "BLI_delaunay_2d.h"
#include "BLI_double2.hh"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "BKE_mesh.h"
#include "BKE_spline.hh"
#include "BLI_task.hh"
#include "UI_interface.h"
#include "UI_resources.h"
#include "node_geometry_util.hh"
static bNodeSocketTemplate geo_node_curve_fill_in[] = {
{SOCK_GEOMETRY, N_("Curve")},
{-1, ""},
};
static bNodeSocketTemplate geo_node_curve_fill_out[] = {
{SOCK_GEOMETRY, N_("Mesh")},
{-1, ""},
};
static void geo_node_curve_fill_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
{
uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
}
namespace blender::nodes {
static void geo_node_curve_fill_init(bNodeTree *, bNode *node)
{
NodeGeometryCurveFill *data = (NodeGeometryCurveFill *)MEM_callocN(sizeof(NodeGeometryCurveFill),
__func__);
data->mode = GEO_NODE_CURVE_FILL_MODE_TRIANGULATED;
node->storage = data;
}
static blender::meshintersect::CDT_result<double> do_cdt(const CurveEval &curve,
const CDT_output_type output_type)
{
Span<SplinePtr> splines = curve.splines();
blender::meshintersect::CDT_input<double> input;
input.need_ids = false;
Array<int> offsets = curve.evaluated_point_offsets();
input.vert.reinitialize(offsets.last());
input.face.reinitialize(splines.size());
for (const int i_spline : splines.index_range()) {
const SplinePtr &spline = splines[i_spline];
const int vert_offset = offsets[i_spline];
Span<float3> positions = spline->evaluated_positions();
for (const int i : positions.index_range()) {
input.vert[vert_offset + i] = double2(positions[i].x, positions[i].y);
}
input.face[i_spline].resize(spline->evaluated_edges_size());
MutableSpan<int> face_verts = input.face[i_spline];
for (const int i : IndexRange(spline->evaluated_edges_size())) {
face_verts[i] = vert_offset + i;
}
}
blender::meshintersect::CDT_result<double> result = delaunay_2d_calc(input, output_type);
return result;
}
/* Converts the CDT result into a Mesh. */
static Mesh *cdt_to_mesh(const blender::meshintersect::CDT_result<double> &result)
{
int vert_len = result.vert.size();
int edge_len = result.edge.size();
int poly_len = result.face.size();
int loop_len = 0;
for (const Vector<int> &face : result.face) {
loop_len += face.size();
}
Mesh *mesh = BKE_mesh_new_nomain(vert_len, edge_len, 0, loop_len, poly_len);
MutableSpan<MVert> verts{mesh->mvert, mesh->totvert};
MutableSpan<MEdge> edges{mesh->medge, mesh->totedge};
MutableSpan<MLoop> loops{mesh->mloop, mesh->totloop};
MutableSpan<MPoly> polys{mesh->mpoly, mesh->totpoly};
for (const int i : IndexRange(result.vert.size())) {
copy_v3_v3(verts[i].co, float3((float)result.vert[i].x, (float)result.vert[i].y, 0.0f));
}
for (const int i : IndexRange(result.edge.size())) {
edges[i].v1 = result.edge[i].first;
edges[i].v2 = result.edge[i].second;
edges[i].flag = ME_EDGEDRAW | ME_EDGERENDER;
}
int i_loop = 0;
for (const int i : IndexRange(result.face.size())) {
polys[i].loopstart = i_loop;
polys[i].totloop = result.face[i].size();
for (const int j : result.face[i].index_range()) {
loops[i_loop].v = result.face[i][j];
i_loop++;
}
}
/* The delaunay triangulation doesn't seem to return all of the necessary edges, even in
* triangulation mode. */
BKE_mesh_calc_edges(mesh, true, false);
return mesh;
}
static Mesh *curve_fill_calculate(GeoNodeExecParams &params, const CurveComponent &component)
{
const CurveEval &curve = *component.get_for_read();
if (curve.splines().size() == 0) {
return nullptr;
}
const NodeGeometryCurveFill &storage = *(const NodeGeometryCurveFill *)params.node().storage;
const GeometryNodeCurveFillMode mode = (GeometryNodeCurveFillMode)storage.mode;
const CDT_output_type output_type = (mode == GEO_NODE_CURVE_FILL_MODE_NGONS) ?
CDT_CONSTRAINTS_VALID_BMESH_WITH_HOLES :
CDT_INSIDE_WITH_HOLES;
const blender::meshintersect::CDT_result<double> results = do_cdt(curve, output_type);
return cdt_to_mesh(results);
}
static void geo_node_curve_fill_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
geometry_set = bke::geometry_set_realize_instances(geometry_set);
if (!geometry_set.has_curve()) {
params.set_output("Mesh", GeometrySet());
return;
}
Mesh *mesh = curve_fill_calculate(params,
*geometry_set.get_component_for_read<CurveComponent>());
params.set_output("Mesh", GeometrySet::create_with_mesh(mesh));
}
} // namespace blender::nodes
void register_node_type_geo_curve_fill()
{
static bNodeType ntype;
geo_node_type_base(&ntype, GEO_NODE_CURVE_FILL, "Curve Fill", NODE_CLASS_GEOMETRY, 0);
node_type_socket_templates(&ntype, geo_node_curve_fill_in, geo_node_curve_fill_out);
node_type_init(&ntype, blender::nodes::geo_node_curve_fill_init);
node_type_storage(
&ntype, "NodeGeometryCurveFill", node_free_standard_storage, node_copy_standard_storage);
ntype.geometry_node_execute = blender::nodes::geo_node_curve_fill_exec;
ntype.draw_buttons = geo_node_curve_fill_layout;
nodeRegisterType(&ntype);
}