From 88e9e97ee907b8021e1ff25ac5bd805d4d8138cb Mon Sep 17 00:00:00 2001 From: Johnny Matthews Date: Tue, 30 Nov 2021 15:25:43 -0600 Subject: Geometry Nodes: Add Point Count to Spline Length Node - Integer Field input of the number of control points on each spline in the spline domain. - In the point domain, it is the number of points on the spline that contains the given control point. Differential Revision: https://developer.blender.org/D13279 --- .../geometry/nodes/node_geo_input_spline_length.cc | 79 ++++++++++++++++++++-- 1 file changed, 75 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_spline_length.cc b/source/blender/nodes/geometry/nodes/node_geo_input_spline_length.cc index 998fce5841b..226d50543f8 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_input_spline_length.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_input_spline_length.cc @@ -23,8 +23,13 @@ namespace blender::nodes::node_geo_input_spline_length_cc { static void node_declare(NodeDeclarationBuilder &b) { b.add_output(N_("Length")).field_source(); + b.add_output(N_("Point Count")).field_source(); } +/* -------------------------------------------------------------------- + * Spline Length + */ + static VArray construct_spline_length_gvarray(const CurveComponent &component, const AttributeDomain domain, ResourceScope &UNUSED(scope)) @@ -46,7 +51,7 @@ static VArray construct_spline_length_gvarray(const CurveComponent &compo std::move(length), ATTR_DOMAIN_CURVE, ATTR_DOMAIN_POINT); } - return nullptr; + return {}; } class SplineLengthFieldInput final : public fn::FieldInput { @@ -85,10 +90,77 @@ class SplineLengthFieldInput final : public fn::FieldInput { } }; +/* -------------------------------------------------------------------- + * Spline Count + */ + +static VArray construct_spline_count_gvarray(const CurveComponent &component, + const AttributeDomain domain, + ResourceScope &UNUSED(scope)) +{ + const CurveEval *curve = component.get_for_read(); + if (curve == nullptr) { + return {}; + } + + Span splines = curve->splines(); + auto count_fn = [splines](int i) { return splines[i]->size(); }; + + if (domain == ATTR_DOMAIN_CURVE) { + return VArray::ForFunc(splines.size(), count_fn); + } + if (domain == ATTR_DOMAIN_POINT) { + VArray count = VArray::ForFunc(splines.size(), count_fn); + return component.attribute_try_adapt_domain( + std::move(count), ATTR_DOMAIN_CURVE, ATTR_DOMAIN_POINT); + } + + return {}; +} + +class SplineCountFieldInput final : public fn::FieldInput { + public: + SplineCountFieldInput() : fn::FieldInput(CPPType::get(), "Spline Point Count") + { + category_ = Category::Generated; + } + + GVArray get_varray_for_context(const fn::FieldContext &context, + IndexMask UNUSED(mask), + ResourceScope &scope) const final + { + if (const GeometryComponentFieldContext *geometry_context = + dynamic_cast(&context)) { + + const GeometryComponent &component = geometry_context->geometry_component(); + const AttributeDomain domain = geometry_context->domain(); + if (component.type() == GEO_COMPONENT_TYPE_CURVE) { + const CurveComponent &curve_component = static_cast(component); + return construct_spline_count_gvarray(curve_component, domain, scope); + } + } + return {}; + } + + uint64_t hash() const override + { + /* Some random constant hash. */ + return 456364322625; + } + + bool is_equal_to(const fn::FieldNode &other) const override + { + return dynamic_cast(&other) != nullptr; + } +}; + static void node_geo_exec(GeoNodeExecParams params) { - Field length_field{std::make_shared()}; - params.set_output("Length", std::move(length_field)); + Field spline_length_field{std::make_shared()}; + Field spline_count_field{std::make_shared()}; + + params.set_output("Length", std::move(spline_length_field)); + params.set_output("Point Count", std::move(spline_count_field)); } } // namespace blender::nodes::node_geo_input_spline_length_cc @@ -98,7 +170,6 @@ void register_node_type_geo_input_spline_length() namespace file_ns = blender::nodes::node_geo_input_spline_length_cc; static bNodeType ntype; - geo_node_type_base(&ntype, GEO_NODE_INPUT_SPLINE_LENGTH, "Spline Length", NODE_CLASS_INPUT, 0); ntype.geometry_node_execute = file_ns::node_geo_exec; ntype.declare = file_ns::node_declare; -- cgit v1.2.3