/* * 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 "BKE_attribute_math.hh" #include "BKE_mesh.h" #include "node_geometry_util.hh" namespace blender::nodes::node_geo_points_to_vertices_cc { using blender::Array; static void node_declare(NodeDeclarationBuilder &b) { b.add_input(N_("Points")).supported_type(GEO_COMPONENT_TYPE_POINT_CLOUD); b.add_input(N_("Selection")).default_value(true).supports_field().hide_value(); b.add_output(N_("Mesh")); } template static void copy_attribute_to_vertices(const Span src, const IndexMask mask, MutableSpan dst) { for (const int i : mask.index_range()) { dst[i] = src[mask[i]]; } } /* One improvement would be to move the attribute arrays directly to the mesh when possible. */ static void geometry_set_points_to_vertices(GeometrySet &geometry_set, Field &selection_field) { const PointCloudComponent *point_component = geometry_set.get_component_for_read(); if (point_component == nullptr) { geometry_set.keep_only({GEO_COMPONENT_TYPE_INSTANCES}); return; } GeometryComponentFieldContext field_context{*point_component, ATTR_DOMAIN_POINT}; const int domain_size = point_component->attribute_domain_size(ATTR_DOMAIN_POINT); if (domain_size == 0) { geometry_set.keep_only({GEO_COMPONENT_TYPE_INSTANCES}); return; } fn::FieldEvaluator selection_evaluator{field_context, domain_size}; selection_evaluator.add(selection_field); selection_evaluator.evaluate(); const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0); Map attributes; geometry_set.gather_attributes_for_propagation( {GEO_COMPONENT_TYPE_POINT_CLOUD}, GEO_COMPONENT_TYPE_MESH, false, attributes); Mesh *mesh = BKE_mesh_new_nomain(selection.size(), 0, 0, 0, 0); geometry_set.replace_mesh(mesh); MeshComponent &mesh_component = geometry_set.get_component_for_write(); for (Map::Item entry : attributes.items()) { const AttributeIDRef attribute_id = entry.key; const CustomDataType data_type = entry.value.data_type; GVArray src = point_component->attribute_get_for_read( attribute_id, ATTR_DOMAIN_POINT, data_type); OutputAttribute dst = mesh_component.attribute_try_get_for_output_only( attribute_id, ATTR_DOMAIN_POINT, data_type); if (dst && src) { attribute_math::convert_to_static_type(data_type, [&](auto dummy) { using T = decltype(dummy); VArray src_typed = src.typed(); VArray_Span src_typed_span{src_typed}; copy_attribute_to_vertices(src_typed_span, selection, dst.as_span().typed()); }); dst.save(); } } geometry_set.keep_only({GEO_COMPONENT_TYPE_MESH, GEO_COMPONENT_TYPE_INSTANCES}); } static void node_geo_exec(GeoNodeExecParams params) { GeometrySet geometry_set = params.extract_input("Points"); Field selection_field = params.extract_input>("Selection"); geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) { geometry_set_points_to_vertices(geometry_set, selection_field); }); params.set_output("Mesh", std::move(geometry_set)); } } // namespace blender::nodes::node_geo_points_to_vertices_cc void register_node_type_geo_points_to_vertices() { namespace file_ns = blender::nodes::node_geo_points_to_vertices_cc; static bNodeType ntype; geo_node_type_base( &ntype, GEO_NODE_POINTS_TO_VERTICES, "Points to Vertices", NODE_CLASS_GEOMETRY, 0); ntype.declare = file_ns::node_declare; ntype.geometry_node_execute = file_ns::node_geo_exec; nodeRegisterType(&ntype); }