From 9d7aac6d66c9e5c1d418f9fbd4b82415ec171e38 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 3 Aug 2022 15:37:57 -0500 Subject: Cleanup: Use generic utility to retrieve attributes to transfer --- source/blender/geometry/intern/set_curve_type.cc | 124 +++++++++-------------- 1 file changed, 49 insertions(+), 75 deletions(-) (limited to 'source/blender/geometry') diff --git a/source/blender/geometry/intern/set_curve_type.cc b/source/blender/geometry/intern/set_curve_type.cc index 40ee2488a4b..92609a45bdc 100644 --- a/source/blender/geometry/intern/set_curve_type.cc +++ b/source/blender/geometry/intern/set_curve_type.cc @@ -286,42 +286,6 @@ static void retrieve_curve_sizes(const bke::CurvesGeometry &curves, MutableSpan< }); } -struct GenericAttributes : NonCopyable, NonMovable { - Vector src; - Vector dst; - - Vector attributes; -}; - -static void retrieve_generic_point_attributes(const bke::AttributeAccessor &src_attributes, - bke::MutableAttributeAccessor &dst_attributes, - GenericAttributes &attributes) -{ - src_attributes.for_all( - [&](const bke::AttributeIDRef &id, const bke::AttributeMetaData meta_data) { - if (meta_data.domain != ATTR_DOMAIN_POINT) { - /* Curve domain attributes are all copied directly to the result in one step. */ - return true; - } - if (src_attributes.is_builtin(id)) { - if (!(id.is_named() && ELEM(id, "tilt", "radius"))) { - return true; - } - } - - GVArray src_attribute = src_attributes.lookup(id, ATTR_DOMAIN_POINT); - BLI_assert(src_attribute); - attributes.src.append(src_attribute.get_internal_span()); - - bke::GSpanAttributeWriter dst_attribute = dst_attributes.lookup_or_add_for_write_span( - id, ATTR_DOMAIN_POINT, meta_data.data_type); - attributes.dst.append(dst_attribute.span); - attributes.attributes.append(std::move(dst_attribute)); - - return true; - }); -} - static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &src_curves, const IndexMask selection) { @@ -347,8 +311,16 @@ static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &s const bke::AttributeAccessor src_attributes = src_curves.attributes(); bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write(); - GenericAttributes attributes; - retrieve_generic_point_attributes(src_attributes, dst_attributes, attributes); + Vector generic_attributes = bke::retrieve_attributes_for_transfer( + src_attributes, + dst_attributes, + ATTR_DOMAIN_MASK_POINT, + {"position", + "handle_type_left", + "handle_type_right", + "handle_right", + "handle_left", + "nurbs_weight"}); MutableSpan dst_positions = dst_curves.positions_for_write(); MutableSpan dst_handles_l = dst_curves.handle_positions_left_for_write(); @@ -373,9 +345,9 @@ static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &s } }); - for (const int i : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { bke::curves::copy_point_data( - src_curves, dst_curves, selection, attributes.src[i], attributes.dst[i]); + src_curves, dst_curves, selection, attribute.src, attribute.dst.span); } }; @@ -384,9 +356,9 @@ static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &s bke::curves::fill_points(dst_curves, selection, BEZIER_HANDLE_VECTOR, dst_types_l); bke::curves::fill_points(dst_curves, selection, BEZIER_HANDLE_VECTOR, dst_types_r); dst_curves.calculate_bezier_auto_handles(); - for (const int i : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { bke::curves::copy_point_data( - src_curves, dst_curves, selection, attributes.src[i], attributes.dst[i]); + src_curves, dst_curves, selection, attribute.src, attribute.dst.span); } }; @@ -404,9 +376,9 @@ static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &s dst_curves.calculate_bezier_auto_handles(); - for (const int i : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { bke::curves::copy_point_data( - src_curves, dst_curves, selection, attributes.src[i], attributes.dst[i]); + src_curves, dst_curves, selection, attribute.src, attribute.dst.span); } }; @@ -445,14 +417,14 @@ static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &s } }); - for (const int i_attribute : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) { for (const int i : selection.slice(range)) { const IndexRange src_points = src_curves.points_for_curve(i); const IndexRange dst_points = dst_curves.points_for_curve(i); - nurbs_to_bezier_assign(attributes.src[i_attribute].slice(src_points), + nurbs_to_bezier_assign(attribute.src.slice(src_points), KnotsMode(src_knot_modes[i]), - attributes.dst[i_attribute].slice(dst_points)); + attribute.dst.span.slice(dst_points)); } }); } @@ -469,13 +441,13 @@ static bke::CurvesGeometry convert_curves_to_bezier(const bke::CurvesGeometry &s const Vector unselected_ranges = selection.extract_ranges_invert( src_curves.curves_range()); - for (const int i : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { bke::curves::copy_point_data( - src_curves, dst_curves, unselected_ranges, attributes.src[i], attributes.dst[i]); + src_curves, dst_curves, unselected_ranges, attribute.src, attribute.dst.span); } - for (bke::GSpanAttributeWriter &attribute : attributes.attributes) { - attribute.finish(); + for (bke::AttributeTransferData &attribute : generic_attributes) { + attribute.dst.finish(); } return dst_curves; @@ -504,8 +476,16 @@ static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &sr const bke::AttributeAccessor src_attributes = src_curves.attributes(); bke::MutableAttributeAccessor dst_attributes = dst_curves.attributes_for_write(); - GenericAttributes attributes; - retrieve_generic_point_attributes(src_attributes, dst_attributes, attributes); + Vector generic_attributes = bke::retrieve_attributes_for_transfer( + src_attributes, + dst_attributes, + ATTR_DOMAIN_MASK_POINT, + {"position", + "handle_type_left", + "handle_type_right", + "handle_right", + "handle_left", + "nurbs_weight"}); MutableSpan dst_positions = dst_curves.positions_for_write(); @@ -529,13 +509,13 @@ static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &sr } }); - for (const int i_attribute : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) { for (const int i : selection.slice(range)) { const IndexRange src_points = src_curves.points_for_curve(i); const IndexRange dst_points = dst_curves.points_for_curve(i); - bezier_generic_to_nurbs(attributes.src[i_attribute].slice(src_points), - attributes.dst[i_attribute].slice(dst_points)); + bezier_generic_to_nurbs(attribute.src.slice(src_points), + attribute.dst.span.slice(dst_points)); } }); } @@ -563,12 +543,9 @@ static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &sr }); } - for (const int i_attribute : attributes.src.index_range()) { - bke::curves::copy_point_data(src_curves, - dst_curves, - selection, - attributes.src[i_attribute], - attributes.dst[i_attribute]); + for (bke::AttributeTransferData &attribute : generic_attributes) { + bke::curves::copy_point_data( + src_curves, dst_curves, selection, attribute.src, attribute.dst.span); } }; @@ -591,13 +568,13 @@ static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &sr } }); - for (const int i_attribute : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) { for (const int i : selection.slice(range)) { const IndexRange src_points = src_curves.points_for_curve(i); const IndexRange dst_points = dst_curves.points_for_curve(i); - bezier_generic_to_nurbs(attributes.src[i_attribute].slice(src_points), - attributes.dst[i_attribute].slice(dst_points)); + bezier_generic_to_nurbs(attribute.src.slice(src_points), + attribute.dst.span.slice(dst_points)); } }); } @@ -614,12 +591,9 @@ static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &sr dst_curves.nurbs_weights_for_write()); } - for (const int i_attribute : attributes.src.index_range()) { - bke::curves::copy_point_data(src_curves, - dst_curves, - selection, - attributes.src[i_attribute], - attributes.dst[i_attribute]); + for (bke::AttributeTransferData &attribute : generic_attributes) { + bke::curves::copy_point_data( + src_curves, dst_curves, selection, attribute.src, attribute.dst.span); } }; @@ -634,13 +608,13 @@ static bke::CurvesGeometry convert_curves_to_nurbs(const bke::CurvesGeometry &sr const Vector unselected_ranges = selection.extract_ranges_invert( src_curves.curves_range()); - for (const int i : attributes.src.index_range()) { + for (bke::AttributeTransferData &attribute : generic_attributes) { bke::curves::copy_point_data( - src_curves, dst_curves, unselected_ranges, attributes.src[i], attributes.dst[i]); + src_curves, dst_curves, unselected_ranges, attribute.src, attribute.dst.span); } - for (bke::GSpanAttributeWriter &attribute : attributes.attributes) { - attribute.finish(); + for (bke::AttributeTransferData &attribute : generic_attributes) { + attribute.dst.finish(); } return dst_curves; -- cgit v1.2.3