[Bf-blender-cvs] [88e9e97ee90] master: Geometry Nodes: Add Point Count to Spline Length Node

Johnny Matthews noreply at git.blender.org
Tue Nov 30 22:26:46 CET 2021


Commit: 88e9e97ee907b8021e1ff25ac5bd805d4d8138cb
Author: Johnny Matthews
Date:   Tue Nov 30 15:25:43 2021 -0600
Branches: master
https://developer.blender.org/rB88e9e97ee907b8021e1ff25ac5bd805d4d8138cb

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

===================================================================

M	source/blender/nodes/geometry/nodes/node_geo_input_spline_length.cc

===================================================================

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<decl::Float>(N_("Length")).field_source();
+  b.add_output<decl::Int>(N_("Point Count")).field_source();
 }
 
+/* --------------------------------------------------------------------
+ * Spline Length
+ */
+
 static VArray<float> construct_spline_length_gvarray(const CurveComponent &component,
                                                      const AttributeDomain domain,
                                                      ResourceScope &UNUSED(scope))
@@ -46,7 +51,7 @@ static VArray<float> 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<int> 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<SplinePtr> splines = curve->splines();
+  auto count_fn = [splines](int i) { return splines[i]->size(); };
+
+  if (domain == ATTR_DOMAIN_CURVE) {
+    return VArray<int>::ForFunc(splines.size(), count_fn);
+  }
+  if (domain == ATTR_DOMAIN_POINT) {
+    VArray<int> count = VArray<int>::ForFunc(splines.size(), count_fn);
+    return component.attribute_try_adapt_domain<int>(
+        std::move(count), ATTR_DOMAIN_CURVE, ATTR_DOMAIN_POINT);
+  }
+
+  return {};
+}
+
+class SplineCountFieldInput final : public fn::FieldInput {
+ public:
+  SplineCountFieldInput() : fn::FieldInput(CPPType::get<int>(), "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<const GeometryComponentFieldContext *>(&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<const CurveComponent &>(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<const SplineCountFieldInput *>(&other) != nullptr;
+  }
+};
+
 static void node_geo_exec(GeoNodeExecParams params)
 {
-  Field<float> length_field{std::make_shared<SplineLengthFieldInput>()};
-  params.set_output("Length", std::move(length_field));
+  Field<float> spline_length_field{std::make_shared<SplineLengthFieldInput>()};
+  Field<int> spline_count_field{std::make_shared<SplineCountFieldInput>()};
+
+  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;



More information about the Bf-blender-cvs mailing list