From 4ddc5a936e07129aaf94ed7d188b8f5f5ea14085 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 3 Oct 2022 16:48:01 -0500 Subject: Geometry Nodes: Add index input to Edge Vertices node Previously the edge index was always determined by the field context, and the node didn't work when the context was in any other domain. Adding an index input makes it work much more nicely with the other topology nodes. It's now in the topology submenu too. I also reimplemented the edge positions input to use the field at index node internally. That will probably make it slower for now, but we need to optimize that to do nothing in some special cases anyway. Differential Revision: https://developer.blender.org/D16105 --- .../nodes/node_geo_input_mesh_edge_vertices.cc | 130 ++++++++------------- 1 file changed, 46 insertions(+), 84 deletions(-) (limited to 'source/blender/nodes/geometry') diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_mesh_edge_vertices.cc b/source/blender/nodes/geometry/nodes/node_geo_input_mesh_edge_vertices.cc index b345ea68cd1..31c82133e2f 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_input_mesh_edge_vertices.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_input_mesh_edge_vertices.cc @@ -11,35 +11,33 @@ namespace blender::nodes::node_geo_input_mesh_edge_vertices_cc { static void node_declare(NodeDeclarationBuilder &b) { + b.add_input(N_("Edge Index")) + .implicit_field(implicit_field_inputs::index) + .description(N_("The edge to retrieve data from. Defaults to the edge from the context")); b.add_output(N_("Vertex Index 1")) - .field_source() + .dependent_field() .description(N_("The index of the first vertex in the edge")); b.add_output(N_("Vertex Index 2")) - .field_source() + .dependent_field() .description(N_("The index of the second vertex in the edge")); b.add_output(N_("Position 1")) - .field_source() + .dependent_field() .description(N_("The position of the first vertex in the edge")); b.add_output(N_("Position 2")) - .field_source() + .dependent_field() .description(N_("The position of the second vertex in the edge")); } enum class VertNumber { V1, V2 }; -static VArray construct_edge_verts_gvarray(const Mesh &mesh, - const VertNumber vertex, - const eAttrDomain domain) +static int edge_get_v1(const MEdge &edge) { - const Span edges = mesh.edges(); - if (domain == ATTR_DOMAIN_EDGE) { - if (vertex == VertNumber::V1) { - return VArray::ForFunc(edges.size(), - [edges](const int i) -> int { return edges[i].v1; }); - } - return VArray::ForFunc(edges.size(), [edges](const int i) -> int { return edges[i].v2; }); - } - return {}; + return edge.v1; +} + +static int edge_get_v2(const MEdge &edge) +{ + return edge.v2; } class EdgeVertsInput final : public bke::MeshFieldInput { @@ -57,7 +55,17 @@ class EdgeVertsInput final : public bke::MeshFieldInput { const eAttrDomain domain, IndexMask UNUSED(mask)) const final { - return construct_edge_verts_gvarray(mesh, vertex_, domain); + if (domain != ATTR_DOMAIN_EDGE) { + return {}; + } + switch (vertex_) { + case VertNumber::V1: + return VArray::ForDerivedSpan(mesh.edges()); + case VertNumber::V2: + return VArray::ForDerivedSpan(mesh.edges()); + } + BLI_assert_unreachable(); + return {}; } uint64_t hash() const override @@ -79,76 +87,30 @@ class EdgeVertsInput final : public bke::MeshFieldInput { } }; -static VArray construct_edge_positions_gvarray(const Mesh &mesh, - const VertNumber vertex, - const eAttrDomain domain) +static void node_geo_exec(GeoNodeExecParams params) { - const Span verts = mesh.verts(); - const Span edges = mesh.edges(); - - if (vertex == VertNumber::V1) { - return mesh.attributes().adapt_domain( - VArray::ForFunc(edges.size(), - [verts, edges](const int i) { return verts[edges[i].v1].co; }), - ATTR_DOMAIN_EDGE, - domain); - } - return mesh.attributes().adapt_domain( - VArray::ForFunc(edges.size(), - [verts, edges](const int i) { return verts[edges[i].v2].co; }), - ATTR_DOMAIN_EDGE, - domain); -} - -class EdgePositionFieldInput final : public bke::MeshFieldInput { - private: - VertNumber vertex_; - - public: - EdgePositionFieldInput(VertNumber vertex) - : bke::MeshFieldInput(CPPType::get(), "Edge Position Field"), vertex_(vertex) - { - category_ = Category::Generated; - } - - GVArray get_varray_for_context(const Mesh &mesh, - const eAttrDomain domain, - IndexMask UNUSED(mask)) const final - { - return construct_edge_positions_gvarray(mesh, vertex_, domain); + const Field edge_index = params.extract_input>("Edge Index"); + Field vertex_field_1(std::make_shared( + edge_index, Field(std::make_shared(VertNumber::V1)), ATTR_DOMAIN_EDGE)); + Field vertex_field_2(std::make_shared( + edge_index, Field(std::make_shared(VertNumber::V2)), ATTR_DOMAIN_EDGE)); + + params.set_output("Vertex Index 1", vertex_field_1); + params.set_output("Vertex Index 2", vertex_field_2); + if (params.output_is_required("Position 1")) { + params.set_output("Position 1", + Field(std::make_shared( + vertex_field_1, + Field(AttributeFieldInput::Create("position")), + ATTR_DOMAIN_POINT))); } - - uint64_t hash() const override - { - return vertex_ == VertNumber::V1 ? 987456978362 : 374587679866; + if (params.output_is_required("Position 2")) { + params.set_output("Position 2", + Field(std::make_shared( + vertex_field_2, + Field(AttributeFieldInput::Create("position")), + ATTR_DOMAIN_POINT))); } - - bool is_equal_to(const fn::FieldNode &other) const override - { - if (const EdgePositionFieldInput *other_field = dynamic_cast( - &other)) { - return vertex_ == other_field->vertex_; - } - return false; - } - - std::optional preferred_domain(const Mesh & /*mesh*/) const override - { - return ATTR_DOMAIN_EDGE; - } -}; - -static void node_geo_exec(GeoNodeExecParams params) -{ - Field vertex_field_1{std::make_shared(VertNumber::V1)}; - Field vertex_field_2{std::make_shared(VertNumber::V2)}; - Field position_field_1{std::make_shared(VertNumber::V1)}; - Field position_field_2{std::make_shared(VertNumber::V2)}; - - params.set_output("Vertex Index 1", std::move(vertex_field_1)); - params.set_output("Vertex Index 2", std::move(vertex_field_2)); - params.set_output("Position 1", std::move(position_field_1)); - params.set_output("Position 2", std::move(position_field_2)); } } // namespace blender::nodes::node_geo_input_mesh_edge_vertices_cc -- cgit v1.2.3