Geometry Nodes: Curve Endpoints Node

This node is quite similar to the curve to points node, but creates
points for only the start and end of each spline. This is a separate
node because the sampling from the curve to points node don't apply,
and just for ease of use.

All attributes from the curves are copied, including the data for
instancing: tangents, normals, and the derived rotations. One simple
use case is to make round caps on curves by instancinghalves of a
sphere on each end of the splines.

Differential Revision: https://developer.blender.org/D11719
This commit is contained in:
Angus Stanton 2021-07-06 22:24:04 -05:00 committed by Hans Goudey
parent 31e6f0dc7a
commit 63a8b3b972
9 changed files with 264 additions and 32 deletions

@ -506,6 +506,7 @@ geometry_node_categories = [
NodeItem("GeometryNodeCurveResample"),
NodeItem("GeometryNodeMeshToCurve"),
NodeItem("GeometryNodeCurveToPoints"),
NodeItem("GeometryNodeCurveEndpoints"),
NodeItem("GeometryNodeCurveLength"),
NodeItem("GeometryNodeCurveReverse"),
]),

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

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

@ -164,6 +164,7 @@ set(SRC
geometry/nodes/node_geo_collection_info.cc
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_length.cc
geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
geometry/nodes/node_geo_curve_primitive_circle.cc

@ -51,6 +51,7 @@ void register_node_type_geo_boolean(void);
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_length(void);
void register_node_type_geo_curve_primitive_bezier_segment(void);
void register_node_type_geo_curve_primitive_circle(void);

@ -303,6 +303,7 @@ DefNode(GeometryNode, GEO_NODE_CURVE_SUBDIVIDE, def_geo_curve_subdivide, "CURVE_
DefNode(GeometryNode, GEO_NODE_CURVE_TO_MESH, 0, "CURVE_TO_MESH", CurveToMesh, "Curve to Mesh", "")
DefNode(GeometryNode, GEO_NODE_CURVE_REVERSE, 0, "CURVE_REVERSE", CurveReverse, "Curve Reverse", "")
DefNode(GeometryNode, GEO_NODE_CURVE_TO_POINTS, def_geo_curve_to_points, "CURVE_TO_POINTS", CurveToPoints, "Curve to Points", "")
DefNode(GeometryNode, GEO_NODE_CURVE_ENDPOINTS, 0, "CURVE_ENDPOINTS", CurveEndpoints, "Curve Endpoints", "")
DefNode(GeometryNode, GEO_NODE_DELETE_GEOMETRY, 0, "DELETE_GEOMETRY", DeleteGeometry, "Delete Geometry", "")
DefNode(GeometryNode, GEO_NODE_EDGE_SPLIT, 0, "EDGE_SPLIT", EdgeSplit, "Edge Split", "")
DefNode(GeometryNode, GEO_NODE_INPUT_MATERIAL, def_geo_input_material, "INPUT_MATERIAL", InputMaterial, "Material", "")

@ -70,4 +70,26 @@ void copy_point_attributes_based_on_mask(const GeometryComponent &in_component,
Span<bool> masks,
const bool invert);
struct CurveToPointsResults {
int result_size;
MutableSpan<float3> positions;
MutableSpan<float> radii;
MutableSpan<float> tilts;
Map<std::string, GMutableSpan> point_attributes;
MutableSpan<float3> tangents;
MutableSpan<float3> normals;
MutableSpan<float3> rotations;
};
/**
* Create references for all result point cloud attributes to simplify accessing them later on.
*/
CurveToPointsResults curve_to_points_create_result_attributes(PointCloudComponent &points,
const CurveEval &curve);
void curve_create_default_rotation_attribute(Span<float3> tangents,
Span<float3> normals,
MutableSpan<float3> rotations);
} // namespace blender::nodes

@ -0,0 +1,221 @@
/*
* 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_task.hh"
#include "BLI_timeit.hh"
#include "BKE_pointcloud.h"
#include "BKE_spline.hh"
#include "UI_interface.h"
#include "UI_resources.h"
#include "node_geometry_util.hh"
static bNodeSocketTemplate geo_node_curve_endpoints_in[] = {
{SOCK_GEOMETRY, N_("Geometry")},
{-1, ""},
};
static bNodeSocketTemplate geo_node_curve_endpoints_out[] = {
{SOCK_GEOMETRY, N_("Start Points")},
{SOCK_GEOMETRY, N_("End Points")},
{-1, ""},
};
namespace blender::nodes {
/**
* Evaluate splines in parallel to speed up the rest of the node's execution.
*/
static void evaluate_splines(Span<SplinePtr> splines)
{
threading::parallel_for_each(splines, [](const SplinePtr &spline) {
/* These functions fill the corresponding caches on each spline. */
spline->evaluated_positions();
spline->evaluated_tangents();
spline->evaluated_normals();
spline->evaluated_lengths();
});
}
/**
* \note Use attributes from the curve component rather than the attribute data directly on the
* attribute storage to allow reading the virtual spline attributes like "cyclic" and "resolution".
*/
static void copy_spline_domain_attributes(const CurveComponent &curve_component,
Span<int> offsets,
PointCloudComponent &points)
{
curve_component.attribute_foreach([&](StringRefNull name, const AttributeMetaData &meta_data) {
if (meta_data.domain != ATTR_DOMAIN_CURVE) {
return true;
}
GVArrayPtr spline_attribute = curve_component.attribute_get_for_read(
name, ATTR_DOMAIN_CURVE, meta_data.data_type);
OutputAttribute result_attribute = points.attribute_try_get_for_output_only(
name, ATTR_DOMAIN_POINT, meta_data.data_type);
GMutableSpan result = result_attribute.as_span();
/* Only copy the attributes of splines in the offsets. */
for (const int i : offsets.index_range()) {
spline_attribute->get(offsets[i], result[i]);
}
result_attribute.save();
return true;
});
}
/* Get the offsets for the splines whose endpoints we want to output. Filter those which are cylic,
* or that evaluate to empty. Could be easily adapted to include a selection argument to support
* attribute selection. */
static blender::Vector<int> get_endpoint_spline_offsets(Span<SplinePtr> splines)
{
blender::Vector<int> spline_offsets;
spline_offsets.reserve(splines.size());
for (const int i : splines.index_range()) {
if (!(splines[i]->is_cyclic() || splines[i]->evaluated_points_size() == 0)) {
spline_offsets.append(i);
}
}
return spline_offsets;
}
/**
* Copy the endpoint attributes from the correct positions at the splines at the offsets to
* the start and end attributes.
*/
static void copy_endpoint_attributes(Span<SplinePtr> splines,
Span<int> offsets,
CurveToPointsResults &start_data,
CurveToPointsResults &end_data)
{
threading::parallel_for(offsets.index_range(), 64, [&](IndexRange range) {
for (const int i : range) {
const Spline &spline = *splines[offsets[i]];
/* Copy the start and end point data over. */
start_data.positions[i] = spline.evaluated_positions().first();
start_data.tangents[i] = spline.evaluated_tangents().first();
start_data.normals[i] = spline.evaluated_normals().first();
start_data.radii[i] = spline.radii().first();
start_data.tilts[i] = spline.tilts().first();
end_data.positions[i] = spline.evaluated_positions().last();
end_data.tangents[i] = spline.evaluated_tangents().last();
end_data.normals[i] = spline.evaluated_normals().last();
end_data.radii[i] = spline.radii().last();
end_data.tilts[i] = spline.tilts().last();
/* Copy the point attribute data over. */
for (const auto &item : start_data.point_attributes.items()) {
const StringRef name = item.key;
GMutableSpan point_span = item.value;
BLI_assert(spline.attributes.get_for_read(name));
GSpan spline_span = *spline.attributes.get_for_read(name);
blender::fn::GVArray_For_GSpan(spline_span).get(0, point_span[i]);
}
for (const auto &item : end_data.point_attributes.items()) {
const StringRef name = item.key;
GMutableSpan point_span = item.value;
BLI_assert(spline.attributes.get_for_read(name));
GSpan spline_span = *spline.attributes.get_for_read(name);
blender::fn::GVArray_For_GSpan(spline_span).get(spline.size() - 1, point_span[i]);
}
}
});
}
static void geo_node_curve_endpoints_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
geometry_set = bke::geometry_set_realize_instances(geometry_set);
if (!geometry_set.has_curve()) {
params.set_output("Start Points", GeometrySet());
params.set_output("End Points", GeometrySet());
return;
}
const CurveComponent &curve_component = *geometry_set.get_component_for_read<CurveComponent>();
const CurveEval &curve = *curve_component.get_for_read();
const Span<SplinePtr> splines = curve.splines();
curve.assert_valid_point_attributes();
evaluate_splines(splines);
const Vector<int> offsets = get_endpoint_spline_offsets(splines);
const int total_size = offsets.size();
if (total_size == 0) {
params.set_output("Start Points", GeometrySet());
params.set_output("End Points", GeometrySet());
return;
}
GeometrySet start_result = GeometrySet::create_with_pointcloud(
BKE_pointcloud_new_nomain(total_size));
GeometrySet end_result = GeometrySet::create_with_pointcloud(
BKE_pointcloud_new_nomain(total_size));
PointCloudComponent &start_point_component =
start_result.get_component_for_write<PointCloudComponent>();
PointCloudComponent &end_point_component =
end_result.get_component_for_write<PointCloudComponent>();
CurveToPointsResults start_attributes = curve_to_points_create_result_attributes(
start_point_component, curve);
CurveToPointsResults end_attributes = curve_to_points_create_result_attributes(
end_point_component, curve);
copy_endpoint_attributes(splines, offsets.as_span(), start_attributes, end_attributes);
copy_spline_domain_attributes(curve_component, offsets.as_span(), start_point_component);
curve_create_default_rotation_attribute(
start_attributes.tangents, start_attributes.normals, start_attributes.rotations);
curve_create_default_rotation_attribute(
end_attributes.tangents, end_attributes.normals, end_attributes.rotations);
/* The default radius is way too large for points, divide by 10. */
for (float &radius : start_attributes.radii) {
radius *= 0.1f;
}
for (float &radius : end_attributes.radii) {
radius *= 0.1f;
}
params.set_output("Start Points", std::move(start_result));
params.set_output("End Points", std::move(end_result));
}
} // namespace blender::nodes
void register_node_type_geo_curve_endpoints()
{
static bNodeType ntype;
geo_node_type_base(&ntype, GEO_NODE_CURVE_ENDPOINTS, "Curve Endpoints", NODE_CLASS_GEOMETRY, 0);
node_type_socket_templates(&ntype, geo_node_curve_endpoints_in, geo_node_curve_endpoints_out);
ntype.geometry_node_execute = blender::nodes::geo_node_curve_endpoints_exec;
nodeRegisterType(&ntype);
}

@ -118,22 +118,6 @@ static Array<int> calculate_spline_point_offsets(GeoNodeExecParams &params,
return {0};
}
/**
* \note This doesn't store a map for spline domain attributes.
*/
struct ResultAttributes {
int result_size;
MutableSpan<float3> positions;
MutableSpan<float> radii;
MutableSpan<float> tilts;
Map<std::string, GMutableSpan> point_attributes;
MutableSpan<float3> tangents;
MutableSpan<float3> normals;
MutableSpan<float3> rotations;
};
static GMutableSpan create_attribute_and_retrieve_span(PointCloudComponent &points,
const StringRef name,
const CustomDataType data_type)
@ -153,13 +137,10 @@ static MutableSpan<T> create_attribute_and_retrieve_span(PointCloudComponent &po
return attribute.typed<T>();
}
/**
* Create references for all result point cloud attributes to simplify accessing them later on.
*/
static ResultAttributes create_point_attributes(PointCloudComponent &points,
const CurveEval &curve)
CurveToPointsResults curve_to_points_create_result_attributes(PointCloudComponent &points,
const CurveEval &curve)
{
ResultAttributes attributes;
CurveToPointsResults attributes;
attributes.result_size = points.attribute_domain_size(ATTR_DOMAIN_POINT);
@ -190,7 +171,7 @@ static ResultAttributes create_point_attributes(PointCloudComponent &points,
*/
static void copy_evaluated_point_attributes(Span<SplinePtr> splines,
Span<int> offsets,
ResultAttributes &data)
CurveToPointsResults &data)
{
threading::parallel_for(splines.index_range(), 64, [&](IndexRange range) {
for (const int i : range) {
@ -221,7 +202,7 @@ static void copy_evaluated_point_attributes(Span<SplinePtr> splines,
static void copy_uniform_sample_point_attributes(Span<SplinePtr> splines,
Span<int> offsets,
ResultAttributes &data)
CurveToPointsResults &data)
{
threading::parallel_for(splines.index_range(), 64, [&](IndexRange range) {
for (const int i : range) {
@ -307,13 +288,14 @@ static void copy_spline_domain_attributes(const CurveComponent &curve_component,
});
}
static void create_default_rotation_attribute(ResultAttributes &data)
void curve_create_default_rotation_attribute(Span<float3> tangents,
Span<float3> normals,
MutableSpan<float3> rotations)
{
threading::parallel_for(IndexRange(data.result_size), 512, [&](IndexRange range) {
threading::parallel_for(IndexRange(rotations.size()), 512, [&](IndexRange range) {
for (const int i : range) {
data.rotations[i] = float4x4::from_normalized_axis_data(
{0, 0, 0}, data.normals[i], data.tangents[i])
.to_euler();
rotations[i] =
float4x4::from_normalized_axis_data({0, 0, 0}, normals[i], tangents[i]).to_euler();
}
});
}
@ -348,8 +330,8 @@ static void geo_node_curve_to_points_exec(GeoNodeExecParams params)
GeometrySet result = GeometrySet::create_with_pointcloud(BKE_pointcloud_new_nomain(total_size));
PointCloudComponent &point_component = result.get_component_for_write<PointCloudComponent>();
ResultAttributes new_attributes = create_point_attributes(point_component, curve);
CurveToPointsResults new_attributes = curve_to_points_create_result_attributes(point_component,
curve);
switch (mode) {
case GEO_NODE_CURVE_SAMPLE_COUNT:
case GEO_NODE_CURVE_SAMPLE_LENGTH:
@ -361,7 +343,8 @@ static void geo_node_curve_to_points_exec(GeoNodeExecParams params)
}
copy_spline_domain_attributes(curve_component, offsets, point_component);
create_default_rotation_attribute(new_attributes);
curve_create_default_rotation_attribute(
new_attributes.tangents, new_attributes.normals, new_attributes.rotations);
/* The default radius is way too large for points, divide by 10. */
for (float &radius : new_attributes.radii) {