BLI: Allow different integer types when filling span indices

This commit is contained in:
Hans Goudey 2023-04-26 23:50:19 -04:00
parent 9fcfba4aae
commit a6baf7beae
13 changed files with 31 additions and 28 deletions

View File

@ -281,7 +281,7 @@ void CurvesGeometry::fill_curve_types(const IndexMask selection, const CurveType
}
}
/* A potential performance optimization is only counting the changed indices. */
this->curve_types_for_write().fill_indices(selection, type);
this->curve_types_for_write().fill_indices(selection.indices(), type);
this->update_curve_types();
this->tag_topology_changed();
}

View File

@ -551,9 +551,10 @@ template<typename T> class MutableSpan {
* Replace a subset of all elements with the given value. This invokes undefined behavior when
* one of the indices is out of bounds.
*/
constexpr void fill_indices(Span<int64_t> indices, const T &value)
template<typename IndexT> constexpr void fill_indices(Span<IndexT> indices, const T &value)
{
for (int64_t i : indices) {
static_assert(std::is_integral_v<IndexT>);
for (IndexT i : indices) {
BLI_assert(i < size_);
data_[i] = value;
}

View File

@ -225,7 +225,7 @@ TEST(span, FillIndices)
{
std::array<int, 5> a = {0, 0, 0, 0, 0};
MutableSpan<int> a_span(a);
a_span.fill_indices({0, 2, 3}, 1);
a_span.fill_indices(Span({0, 2, 3}), 1);
EXPECT_EQ(a[0], 1);
EXPECT_EQ(a[1], 0);
EXPECT_EQ(a[2], 1);

View File

@ -178,7 +178,7 @@ class OptionalOutputsFunction : public MultiFunction {
{
if (params.single_output_is_required(0, "Out 1")) {
MutableSpan<int> values = params.uninitialized_single_output<int>(0, "Out 1");
values.fill_indices(mask, 5);
values.fill_indices(mask.indices(), 5);
}
MutableSpan<std::string> values = params.uninitialized_single_output<std::string>(1, "Out 2");
for (const int i : mask) {

View File

@ -518,8 +518,9 @@ static bke::CurvesGeometry convert_curves_to_nurbs(
};
auto catmull_rom_to_nurbs = [&](IndexMask selection) {
dst_curves.nurbs_orders_for_write().fill_indices(selection, 4);
dst_curves.nurbs_knots_modes_for_write().fill_indices(selection, NURBS_KNOT_MODE_BEZIER);
dst_curves.nurbs_orders_for_write().fill_indices(selection.indices(), 4);
dst_curves.nurbs_knots_modes_for_write().fill_indices(selection.indices(),
NURBS_KNOT_MODE_BEZIER);
fill_weights_if_necessary(selection);
threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {
@ -544,7 +545,7 @@ static bke::CurvesGeometry convert_curves_to_nurbs(
};
auto poly_to_nurbs = [&](IndexMask selection) {
dst_curves.nurbs_orders_for_write().fill_indices(selection, 4);
dst_curves.nurbs_orders_for_write().fill_indices(selection.indices(), 4);
bke::curves::copy_point_data(
src_points_by_curve, dst_points_by_curve, selection, src_positions, dst_positions);
fill_weights_if_necessary(selection);
@ -553,7 +554,7 @@ static bke::CurvesGeometry convert_curves_to_nurbs(
* start/end. */
if (src_cyclic.is_single()) {
dst_curves.nurbs_knots_modes_for_write().fill_indices(
selection,
selection.indices(),
src_cyclic.get_internal_single() ? NURBS_KNOT_MODE_NORMAL : NURBS_KNOT_MODE_ENDPOINT);
}
else {
@ -576,8 +577,9 @@ static bke::CurvesGeometry convert_curves_to_nurbs(
const Span<float3> src_handles_l = src_curves.handle_positions_left();
const Span<float3> src_handles_r = src_curves.handle_positions_right();
dst_curves.nurbs_orders_for_write().fill_indices(selection, 4);
dst_curves.nurbs_knots_modes_for_write().fill_indices(selection, NURBS_KNOT_MODE_BEZIER);
dst_curves.nurbs_orders_for_write().fill_indices(selection.indices(), 4);
dst_curves.nurbs_knots_modes_for_write().fill_indices(selection.indices(),
NURBS_KNOT_MODE_BEZIER);
fill_weights_if_necessary(selection);
threading::parallel_for(selection.index_range(), 512, [&](IndexRange range) {

View File

@ -1065,7 +1065,7 @@ bke::CurvesGeometry trim_curves(const bke::CurvesGeometry &src_curves,
else {
/* Only trimmed curves are no longer cyclic. */
if (bke::SpanAttributeWriter cyclic = dst_attributes.lookup_for_write_span<bool>("cyclic")) {
cyclic.span.fill_indices(selection, false);
cyclic.span.fill_indices(selection.indices(), false);
cyclic.finish();
}

View File

@ -282,13 +282,13 @@ class SampleCurveFunction : public mf::MultiFunction {
auto return_default = [&]() {
if (!sampled_positions.is_empty()) {
sampled_positions.fill_indices(mask, {0, 0, 0});
sampled_positions.fill_indices(mask.indices(), {0, 0, 0});
}
if (!sampled_tangents.is_empty()) {
sampled_tangents.fill_indices(mask, {0, 0, 0});
sampled_tangents.fill_indices(mask.indices(), {0, 0, 0});
}
if (!sampled_normals.is_empty()) {
sampled_normals.fill_indices(mask, {0, 0, 0});
sampled_normals.fill_indices(mask.indices(), {0, 0, 0});
}
};
@ -325,18 +325,18 @@ class SampleCurveFunction : public mf::MultiFunction {
auto fill_invalid = [&](const IndexMask mask) {
if (!sampled_positions.is_empty()) {
sampled_positions.fill_indices(mask, float3(0));
sampled_positions.fill_indices(mask.indices(), float3(0));
}
if (!sampled_tangents.is_empty()) {
sampled_tangents.fill_indices(mask, float3(0));
sampled_tangents.fill_indices(mask.indices(), float3(0));
}
if (!sampled_normals.is_empty()) {
sampled_normals.fill_indices(mask, float3(0));
sampled_normals.fill_indices(mask.indices(), float3(0));
}
if (!sampled_values.is_empty()) {
attribute_math::convert_to_static_type(source_data_->type(), [&](auto dummy) {
using T = decltype(dummy);
sampled_values.typed<T>().fill_indices(mask, {});
sampled_values.typed<T>().fill_indices(mask.indices(), {});
});
}
};

View File

@ -63,10 +63,10 @@ static void set_handle_type(bke::CurvesGeometry &curves,
const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
if (mode & GEO_NODE_CURVE_HANDLE_LEFT) {
curves.handle_types_left_for_write().fill_indices(selection, new_handle_type);
curves.handle_types_left_for_write().fill_indices(selection.indices(), new_handle_type);
}
if (mode & GEO_NODE_CURVE_HANDLE_RIGHT) {
curves.handle_types_right_for_write().fill_indices(selection, new_handle_type);
curves.handle_types_right_for_write().fill_indices(selection.indices(), new_handle_type);
}
/* Eagerly calculate automatically derived handle positions if necessary. */

View File

@ -82,7 +82,7 @@ static void save_selection_as_attribute(Mesh &mesh,
attribute.span.slice(selection.as_range()).fill(true);
}
else {
attribute.span.fill_indices(selection, true);
attribute.span.fill_indices(selection.indices(), true);
}
attribute.finish();

View File

@ -162,7 +162,7 @@ class ProximityFunction : public mf::MultiFunction {
* comparison per vertex, so it's likely not worth it. */
MutableSpan<float> distances = params.uninitialized_single_output<float>(2, "Distance");
distances.fill_indices(mask, FLT_MAX);
distances.fill_indices(mask.indices(), FLT_MAX);
bool success = false;
if (target_.has_mesh()) {
@ -177,10 +177,10 @@ class ProximityFunction : public mf::MultiFunction {
if (!success) {
if (!positions.is_empty()) {
positions.fill_indices(mask, float3(0));
positions.fill_indices(mask.indices(), float3(0));
}
if (!distances.is_empty()) {
distances.fill_indices(mask, 0.0f);
distances.fill_indices(mask.indices(), 0.0f);
}
return;
}

View File

@ -255,7 +255,7 @@ class SampleNearestFunction : public mf::MultiFunction {
const VArray<float3> &positions = params.readonly_single_input<float3>(0, "Position");
MutableSpan<int> indices = params.uninitialized_single_output<int>(1, "Index");
if (!src_component_) {
indices.fill_indices(mask, 0);
indices.fill_indices(mask.indices(), 0);
return;
}

View File

@ -35,7 +35,7 @@ static void set_normal_mode(bke::CurvesGeometry &curves,
evaluator.set_selection(selection_field);
evaluator.evaluate();
const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
curves.normal_mode_for_write().fill_indices(selection, mode);
curves.normal_mode_for_write().fill_indices(selection.indices(), mode);
curves.tag_normals_changed();
}

View File

@ -53,7 +53,7 @@ static void assign_material_to_faces(Mesh &mesh, const IndexMask selection, Mate
MutableAttributeAccessor attributes = mesh.attributes_for_write();
SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_span<int>(
"material_index", ATTR_DOMAIN_FACE);
material_indices.span.fill_indices(selection, new_material_index);
material_indices.span.fill_indices(selection.indices(), new_material_index);
material_indices.finish();
}