Curves: Improve IndexMask usage in curves sculpt brushes

Unify the handling of masks for affected curves in a few of the sculpt
brushes. In the grow shrink brush, replace a more custom "influences
per thread" solution. In the puff brush, use a full array of weights,
and build the mask earlier. In the snake hook brush, use the selection
properly (I observed a 2-3x improvement with a small selection).
This commit is contained in:
Hans Goudey
2023-05-24 20:04:06 -04:00
parent 379a8b700b
commit 1b19f62917
3 changed files with 179 additions and 215 deletions

View File

@@ -51,7 +51,7 @@ class CurvesEffect {
public: public:
virtual ~CurvesEffect() = default; virtual ~CurvesEffect() = default;
virtual void execute(CurvesGeometry &curves, virtual void execute(CurvesGeometry &curves,
Span<int> curve_indices, const IndexMask &curve_mask,
Span<float> move_distances_cu, Span<float> move_distances_cu,
MutableSpan<float3> positions_cu) = 0; MutableSpan<float3> positions_cu) = 0;
}; };
@@ -85,16 +85,15 @@ class ShrinkCurvesEffect : public CurvesEffect {
ShrinkCurvesEffect(const Brush &brush) : brush_(brush) {} ShrinkCurvesEffect(const Brush &brush) : brush_(brush) {}
void execute(CurvesGeometry &curves, void execute(CurvesGeometry &curves,
const Span<int> curve_indices, const IndexMask &curve_mask,
const Span<float> move_distances_cu, const Span<float> move_distances_cu,
MutableSpan<float3> positions_cu) override MutableSpan<float3> positions_cu) override
{ {
const OffsetIndices points_by_curve = curves.points_by_curve(); const OffsetIndices points_by_curve = curves.points_by_curve();
threading::parallel_for(curve_indices.index_range(), 256, [&](const IndexRange range) { curve_mask.foreach_segment(GrainSize(256), [&](IndexMaskSegment segment) {
ParameterizationBuffers data; ParameterizationBuffers data;
for (const int influence_i : range) { for (const int curve_i : segment) {
const int curve_i = curve_indices[influence_i]; const float move_distance_cu = move_distances_cu[curve_i];
const float move_distance_cu = move_distances_cu[influence_i];
const IndexRange points = points_by_curve[curve_i]; const IndexRange points = points_by_curve[curve_i];
this->shrink_curve(positions_cu.slice(points), move_distance_cu, data); this->shrink_curve(positions_cu.slice(points), move_distance_cu, data);
} }
@@ -135,16 +134,15 @@ class ShrinkCurvesEffect : public CurvesEffect {
*/ */
class ExtrapolateCurvesEffect : public CurvesEffect { class ExtrapolateCurvesEffect : public CurvesEffect {
void execute(CurvesGeometry &curves, void execute(CurvesGeometry &curves,
const Span<int> curve_indices, const IndexMask &curve_mask,
const Span<float> move_distances_cu, const Span<float> move_distances_cu,
MutableSpan<float3> positions_cu) override MutableSpan<float3> positions_cu) override
{ {
const OffsetIndices points_by_curve = curves.points_by_curve(); const OffsetIndices points_by_curve = curves.points_by_curve();
threading::parallel_for(curve_indices.index_range(), 256, [&](const IndexRange range) { curve_mask.foreach_segment(GrainSize(256), [&](IndexMaskSegment segment) {
MoveAndResampleBuffers resample_buffer; MoveAndResampleBuffers resample_buffer;
for (const int influence_i : range) { for (const int curve_i : segment) {
const int curve_i = curve_indices[influence_i]; const float move_distance_cu = move_distances_cu[curve_i];
const float move_distance_cu = move_distances_cu[influence_i];
const IndexRange points = points_by_curve[curve_i]; const IndexRange points = points_by_curve[curve_i];
if (points.size() <= 1) { if (points.size() <= 1) {
continue; continue;
@@ -175,27 +173,24 @@ class ScaleCurvesEffect : public CurvesEffect {
ScaleCurvesEffect(bool scale_up, const Brush &brush) : scale_up_(scale_up), brush_(brush) {} ScaleCurvesEffect(bool scale_up, const Brush &brush) : scale_up_(scale_up), brush_(brush) {}
void execute(CurvesGeometry &curves, void execute(CurvesGeometry &curves,
const Span<int> curve_indices, const IndexMask &curve_mask,
const Span<float> move_distances_cu, const Span<float> move_distances_cu,
MutableSpan<float3> positions_cu) override MutableSpan<float3> positions_cu) override
{ {
const OffsetIndices points_by_curve = curves.points_by_curve(); const OffsetIndices points_by_curve = curves.points_by_curve();
threading::parallel_for(curve_indices.index_range(), 256, [&](const IndexRange range) { curve_mask.foreach_index(GrainSize(256), [&](const int64_t curve_i) {
for (const int influence_i : range) { const float move_distance_cu = move_distances_cu[curve_i];
const int curve_i = curve_indices[influence_i]; const IndexRange points = points_by_curve[curve_i];
const float move_distance_cu = move_distances_cu[influence_i];
const IndexRange points = points_by_curve[curve_i];
const float old_length = this->compute_poly_curve_length(positions_cu.slice(points)); const float old_length = this->compute_poly_curve_length(positions_cu.slice(points));
const float length_diff = scale_up_ ? move_distance_cu : -move_distance_cu; const float length_diff = scale_up_ ? move_distance_cu : -move_distance_cu;
const float min_length = brush_.curves_sculpt_settings->minimum_length; const float min_length = brush_.curves_sculpt_settings->minimum_length;
const float new_length = std::max(min_length, old_length + length_diff); const float new_length = std::max(min_length, old_length + length_diff);
const float scale_factor = safe_divide(new_length, old_length); const float scale_factor = safe_divide(new_length, old_length);
const float3 &root_pos_cu = positions_cu[points[0]]; const float3 &root_pos_cu = positions_cu[points[0]];
for (float3 &pos_cu : positions_cu.slice(points.drop_front(1))) { for (float3 &pos_cu : positions_cu.slice(points.drop_front(1))) {
pos_cu = (pos_cu - root_pos_cu) * scale_factor + root_pos_cu; pos_cu = (pos_cu - root_pos_cu) * scale_factor + root_pos_cu;
}
} }
}); });
} }
@@ -255,11 +250,6 @@ struct CurvesEffectOperationExecutor {
float2 brush_pos_start_re_; float2 brush_pos_start_re_;
float2 brush_pos_end_re_; float2 brush_pos_end_re_;
struct Influences {
Vector<int> curve_indices;
Vector<float> move_distances_cu;
};
CurvesEffectOperationExecutor(const bContext &C) : ctx_(C) {} CurvesEffectOperationExecutor(const bContext &C) : ctx_(C) {}
void execute(CurvesEffectOperation &self, void execute(CurvesEffectOperation &self,
@@ -314,22 +304,25 @@ struct CurvesEffectOperationExecutor {
return; return;
} }
Array<float> move_distances_cu(curves_->curves_num());
/* Compute influences. */ /* Compute influences. */
threading::EnumerableThreadSpecific<Influences> influences_for_thread;
if (falloff_shape_ == PAINT_FALLOFF_SHAPE_TUBE) { if (falloff_shape_ == PAINT_FALLOFF_SHAPE_TUBE) {
this->gather_influences_projected(influences_for_thread); this->gather_influences_projected(move_distances_cu);
} }
else if (falloff_shape_ == PAINT_FALLOFF_SHAPE_SPHERE) { else if (falloff_shape_ == PAINT_FALLOFF_SHAPE_SPHERE) {
this->gather_influences_spherical(influences_for_thread); this->gather_influences_spherical(move_distances_cu);
} }
IndexMaskMemory memory;
const IndexMask curves_mask = IndexMask::from_predicate(
curve_selection_, GrainSize(4096), memory, [&](const int64_t curve_i) {
return move_distances_cu[curve_i] > 0.0f;
});
/* Execute effect. */ /* Execute effect. */
MutableSpan<float3> positions_cu = curves_->positions_for_write(); MutableSpan<float3> positions_cu = curves_->positions_for_write();
threading::parallel_for_each(influences_for_thread, [&](const Influences &influences) { self_->effect_->execute(*curves_, curves_mask, move_distances_cu, positions_cu);
BLI_assert(influences.curve_indices.size() == influences.move_distances_cu.size());
self_->effect_->execute(
*curves_, influences.curve_indices, influences.move_distances_cu, positions_cu);
});
curves_->tag_positions_changed(); curves_->tag_positions_changed();
DEG_id_tag_update(&curves_id_->id, ID_RECALC_GEOMETRY); DEG_id_tag_update(&curves_id_->id, ID_RECALC_GEOMETRY);
@@ -337,8 +330,7 @@ struct CurvesEffectOperationExecutor {
ED_region_tag_redraw(ctx_.region); ED_region_tag_redraw(ctx_.region);
} }
void gather_influences_projected( void gather_influences_projected(MutableSpan<float> move_distances_cu)
threading::EnumerableThreadSpecific<Influences> &influences_for_thread)
{ {
const bke::crazyspace::GeometryDeformation deformation = const bke::crazyspace::GeometryDeformation deformation =
bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_); bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_);
@@ -357,84 +349,75 @@ struct CurvesEffectOperationExecutor {
const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_; const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
const float brush_radius_sq_re = pow2f(brush_radius_re); const float brush_radius_sq_re = pow2f(brush_radius_re);
threading::parallel_for(curves_->curves_range(), 256, [&](const IndexRange curves_range) { curve_selection_.foreach_index(GrainSize(256), [&](int64_t curve_i) {
Influences &local_influences = influences_for_thread.local(); const IndexRange points = points_by_curve[curve_i];
for (const int curve_i : curves_range) { const float curve_selection_factor = curve_selection_factors_[curve_i];
const IndexRange points = points_by_curve[curve_i];
const float curve_selection_factor = curve_selection_factors_[curve_i]; float max_move_distance_cu = 0.0f;
for (const float4x4 &brush_transform_inv : symmetry_brush_transforms_inv) {
for (const int segment_i : points.drop_back(1)) {
const float3 p1_cu = math::transform_point(brush_transform_inv,
deformation.positions[segment_i]);
const float3 p2_cu = math::transform_point(brush_transform_inv,
deformation.positions[segment_i + 1]);
float max_move_distance_cu = 0.0f; float2 p1_re, p2_re;
for (const float4x4 &brush_transform_inv : symmetry_brush_transforms_inv) { ED_view3d_project_float_v2_m4(ctx_.region, p1_cu, p1_re, projection.ptr());
for (const int segment_i : points.drop_back(1)) { ED_view3d_project_float_v2_m4(ctx_.region, p2_cu, p2_re, projection.ptr());
const float3 p1_cu = math::transform_point(brush_transform_inv,
deformation.positions[segment_i]);
const float3 p2_cu = math::transform_point(brush_transform_inv,
deformation.positions[segment_i + 1]);
float2 p1_re, p2_re; float2 closest_on_brush_re;
ED_view3d_project_float_v2_m4(ctx_.region, p1_cu, p1_re, projection.ptr()); float2 closest_on_segment_re;
ED_view3d_project_float_v2_m4(ctx_.region, p2_cu, p2_re, projection.ptr()); float lambda_on_brush;
float lambda_on_segment;
const float dist_to_brush_sq_re = closest_seg_seg_v2(closest_on_brush_re,
closest_on_segment_re,
&lambda_on_brush,
&lambda_on_segment,
brush_pos_start_re_,
brush_pos_end_re_,
p1_re,
p2_re);
float2 closest_on_brush_re; if (dist_to_brush_sq_re > brush_radius_sq_re) {
float2 closest_on_segment_re; continue;
float lambda_on_brush;
float lambda_on_segment;
const float dist_to_brush_sq_re = closest_seg_seg_v2(closest_on_brush_re,
closest_on_segment_re,
&lambda_on_brush,
&lambda_on_segment,
brush_pos_start_re_,
brush_pos_end_re_,
p1_re,
p2_re);
if (dist_to_brush_sq_re > brush_radius_sq_re) {
continue;
}
const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
const float radius_falloff = BKE_brush_curve_strength(
brush_, dist_to_brush_re, brush_radius_re);
const float weight = brush_strength_ * radius_falloff * curve_selection_factor;
const float3 closest_on_segment_cu = math::interpolate(
p1_cu, p2_cu, lambda_on_segment);
float3 brush_start_pos_wo, brush_end_pos_wo;
ED_view3d_win_to_3d(
ctx_.v3d,
ctx_.region,
math::transform_point(transforms_.curves_to_world, closest_on_segment_cu),
brush_pos_start_re_,
brush_start_pos_wo);
ED_view3d_win_to_3d(
ctx_.v3d,
ctx_.region,
math::transform_point(transforms_.curves_to_world, closest_on_segment_cu),
brush_pos_end_re_,
brush_end_pos_wo);
const float3 brush_start_pos_cu = math::transform_point(transforms_.world_to_curves,
brush_start_pos_wo);
const float3 brush_end_pos_cu = math::transform_point(transforms_.world_to_curves,
brush_end_pos_wo);
const float move_distance_cu = weight *
math::distance(brush_start_pos_cu, brush_end_pos_cu);
max_move_distance_cu = std::max(max_move_distance_cu, move_distance_cu);
} }
}
if (max_move_distance_cu > 0.0f) { const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
local_influences.curve_indices.append(curve_i); const float radius_falloff = BKE_brush_curve_strength(
local_influences.move_distances_cu.append(max_move_distance_cu); brush_, dist_to_brush_re, brush_radius_re);
const float weight = brush_strength_ * radius_falloff * curve_selection_factor;
const float3 closest_on_segment_cu = math::interpolate(p1_cu, p2_cu, lambda_on_segment);
float3 brush_start_pos_wo, brush_end_pos_wo;
ED_view3d_win_to_3d(
ctx_.v3d,
ctx_.region,
math::transform_point(transforms_.curves_to_world, closest_on_segment_cu),
brush_pos_start_re_,
brush_start_pos_wo);
ED_view3d_win_to_3d(
ctx_.v3d,
ctx_.region,
math::transform_point(transforms_.curves_to_world, closest_on_segment_cu),
brush_pos_end_re_,
brush_end_pos_wo);
const float3 brush_start_pos_cu = math::transform_point(transforms_.world_to_curves,
brush_start_pos_wo);
const float3 brush_end_pos_cu = math::transform_point(transforms_.world_to_curves,
brush_end_pos_wo);
const float move_distance_cu = weight *
math::distance(brush_start_pos_cu, brush_end_pos_cu);
max_move_distance_cu = std::max(max_move_distance_cu, move_distance_cu);
} }
} }
move_distances_cu[curve_i] = max_move_distance_cu;
}); });
} }
void gather_influences_spherical( void gather_influences_spherical(MutableSpan<float> move_distances_cu)
threading::EnumerableThreadSpecific<Influences> &influences_for_thread)
{ {
const bke::crazyspace::GeometryDeformation deformation = const bke::crazyspace::GeometryDeformation deformation =
bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_); bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_);
@@ -459,55 +442,47 @@ struct CurvesEffectOperationExecutor {
eCurvesSymmetryType(curves_id_->symmetry)); eCurvesSymmetryType(curves_id_->symmetry));
const OffsetIndices points_by_curve = curves_->points_by_curve(); const OffsetIndices points_by_curve = curves_->points_by_curve();
threading::parallel_for(curves_->curves_range(), 256, [&](const IndexRange curves_range) { curve_selection_.foreach_index(GrainSize(256), [&](int64_t curve_i) {
Influences &local_influences = influences_for_thread.local(); const IndexRange points = points_by_curve[curve_i];
for (const int curve_i : curves_range) { const float curve_selection_factor = curve_selection_factors_[curve_i];
const IndexRange points = points_by_curve[curve_i];
float max_move_distance_cu = 0.0f; float max_move_distance_cu = 0.0f;
for (const float4x4 &brush_transform : symmetry_brush_transforms) {
const float3 brush_pos_start_transformed_cu = math::transform_point(brush_transform,
brush_pos_start_cu);
const float3 brush_pos_end_transformed_cu = math::transform_point(brush_transform,
brush_pos_end_cu);
const float curve_selection_factor = curve_selection_factors_[curve_i]; for (const int segment_i : points.drop_back(1)) {
const float3 &p1_cu = deformation.positions[segment_i];
const float3 &p2_cu = deformation.positions[segment_i + 1];
for (const float4x4 &brush_transform : symmetry_brush_transforms) { float3 closest_on_segment_cu;
const float3 brush_pos_start_transformed_cu = math::transform_point(brush_transform, float3 closest_on_brush_cu;
brush_pos_start_cu); isect_seg_seg_v3(p1_cu,
const float3 brush_pos_end_transformed_cu = math::transform_point(brush_transform, p2_cu,
brush_pos_end_cu); brush_pos_start_transformed_cu,
brush_pos_end_transformed_cu,
closest_on_segment_cu,
closest_on_brush_cu);
for (const int segment_i : points.drop_back(1)) { const float dist_to_brush_sq_cu = math::distance_squared(closest_on_segment_cu,
const float3 &p1_cu = deformation.positions[segment_i]; closest_on_brush_cu);
const float3 &p2_cu = deformation.positions[segment_i + 1]; if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
continue;
float3 closest_on_segment_cu;
float3 closest_on_brush_cu;
isect_seg_seg_v3(p1_cu,
p2_cu,
brush_pos_start_transformed_cu,
brush_pos_end_transformed_cu,
closest_on_segment_cu,
closest_on_brush_cu);
const float dist_to_brush_sq_cu = math::distance_squared(closest_on_segment_cu,
closest_on_brush_cu);
if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
continue;
}
const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
const float radius_falloff = BKE_brush_curve_strength(
brush_, dist_to_brush_cu, brush_radius_cu);
const float weight = brush_strength_ * radius_falloff * curve_selection_factor;
const float move_distance_cu = weight * brush_pos_diff_length_cu;
max_move_distance_cu = std::max(max_move_distance_cu, move_distance_cu);
} }
}
if (max_move_distance_cu > 0.0f) { const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
local_influences.curve_indices.append(curve_i); const float radius_falloff = BKE_brush_curve_strength(
local_influences.move_distances_cu.append(max_move_distance_cu); brush_, dist_to_brush_cu, brush_radius_cu);
const float weight = brush_strength_ * radius_falloff * curve_selection_factor;
const float move_distance_cu = weight * brush_pos_diff_length_cu;
max_move_distance_cu = std::max(max_move_distance_cu, move_distance_cu);
} }
} }
move_distances_cu[curve_i] = max_move_distance_cu;
}); });
} }
}; };

View File

@@ -142,7 +142,7 @@ struct PuffOperationExecutor {
*curves_, curve_selection_, curves_id_->flag & CV_SCULPT_COLLISION_ENABLED); *curves_, curve_selection_, curves_id_->flag & CV_SCULPT_COLLISION_ENABLED);
} }
Array<float> curve_weights(curve_selection_.size(), 0.0f); Array<float> curve_weights(curves_->curves_num());
if (falloff_shape_ == PAINT_FALLOFF_SHAPE_TUBE) { if (falloff_shape_ == PAINT_FALLOFF_SHAPE_TUBE) {
this->find_curve_weights_projected_with_symmetry(curve_weights); this->find_curve_weights_projected_with_symmetry(curve_weights);
@@ -154,20 +154,15 @@ struct PuffOperationExecutor {
BLI_assert_unreachable(); BLI_assert_unreachable();
} }
this->puff(curve_weights);
Vector<int64_t> changed_curves_indices;
changed_curves_indices.reserve(curve_selection_.size());
for (int64_t select_i : curve_selection_.index_range()) {
if (curve_weights[select_i] > 0.0f) {
changed_curves_indices.append(curve_selection_[select_i]);
}
}
IndexMaskMemory memory; IndexMaskMemory memory;
const IndexMask changed_curves_mask = IndexMask::from_indices<int64_t>(changed_curves_indices, const IndexMask curves_mask = IndexMask::from_predicate(
memory); curve_selection_, GrainSize(4096), memory, [&](const int64_t curve_i) {
return curve_weights[curve_i] > 0.0f;
});
self_->constraint_solver_.solve_step(*curves_, changed_curves_mask, surface_, transforms_); this->puff(curves_mask, curve_weights);
self_->constraint_solver_.solve_step(*curves_, curves_mask, surface_, transforms_);
curves_->tag_positions_changed(); curves_->tag_positions_changed();
DEG_id_tag_update(&curves_id_->id, ID_RECALC_GEOMETRY); DEG_id_tag_update(&curves_id_->id, ID_RECALC_GEOMETRY);
@@ -199,34 +194,32 @@ struct PuffOperationExecutor {
bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_); bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_);
const OffsetIndices points_by_curve = curves_->points_by_curve(); const OffsetIndices points_by_curve = curves_->points_by_curve();
threading::parallel_for(curve_selection_.index_range(), 256, [&](const IndexRange range) { curve_selection_.foreach_index(GrainSize(256), [&](const int64_t curve_i) {
for (const int curve_selection_i : range) { const IndexRange points = points_by_curve[curve_i];
const int curve_i = curve_selection_[curve_selection_i]; const float3 first_pos_cu = math::transform_point(brush_transform_inv,
const IndexRange points = points_by_curve[curve_i]; deformation.positions[points[0]]);
const float3 first_pos_cu = math::transform_point(brush_transform_inv, float2 prev_pos_re;
deformation.positions[points[0]]); ED_view3d_project_float_v2_m4(ctx_.region, first_pos_cu, prev_pos_re, projection.ptr());
float2 prev_pos_re; float max_weight = 0.0f;
ED_view3d_project_float_v2_m4(ctx_.region, first_pos_cu, prev_pos_re, projection.ptr()); for (const int point_i : points.drop_front(1)) {
for (const int point_i : points.drop_front(1)) { const float3 pos_cu = math::transform_point(brush_transform_inv,
const float3 pos_cu = math::transform_point(brush_transform_inv, deformation.positions[point_i]);
deformation.positions[point_i]); float2 pos_re;
float2 pos_re; ED_view3d_project_float_v2_m4(ctx_.region, pos_cu, pos_re, projection.ptr());
ED_view3d_project_float_v2_m4(ctx_.region, pos_cu, pos_re, projection.ptr()); BLI_SCOPED_DEFER([&]() { prev_pos_re = pos_re; });
BLI_SCOPED_DEFER([&]() { prev_pos_re = pos_re; });
const float dist_to_brush_sq_re = dist_squared_to_line_segment_v2( const float dist_to_brush_sq_re = dist_squared_to_line_segment_v2(
brush_pos_re_, prev_pos_re, pos_re); brush_pos_re_, prev_pos_re, pos_re);
if (dist_to_brush_sq_re > brush_radius_sq_re) { if (dist_to_brush_sq_re > brush_radius_sq_re) {
continue; continue;
}
const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
const float radius_falloff = BKE_brush_curve_strength(
brush_, dist_to_brush_re, brush_radius_re);
const float weight = radius_falloff;
math::max_inplace(r_curve_weights[curve_selection_i], weight);
} }
const float dist_to_brush_re = std::sqrt(dist_to_brush_sq_re);
const float radius_falloff = BKE_brush_curve_strength(
brush_, dist_to_brush_re, brush_radius_re);
math::max_inplace(max_weight, radius_falloff);
} }
r_curve_weights[curve_i] = max_weight;
}); });
} }
@@ -263,39 +256,35 @@ struct PuffOperationExecutor {
bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_); bke::crazyspace::get_evaluated_curves_deformation(*ctx_.depsgraph, *object_);
const OffsetIndices points_by_curve = curves_->points_by_curve(); const OffsetIndices points_by_curve = curves_->points_by_curve();
threading::parallel_for(curve_selection_.index_range(), 256, [&](const IndexRange range) { curve_selection_.foreach_index(GrainSize(256), [&](const int64_t curve_i) {
for (const int curve_selection_i : range) { const IndexRange points = points_by_curve[curve_i];
const int curve_i = curve_selection_[curve_selection_i]; float max_weight = 0.0f;
const IndexRange points = points_by_curve[curve_i]; for (const int point_i : points.drop_front(1)) {
for (const int point_i : points.drop_front(1)) { const float3 &prev_pos_cu = deformation.positions[point_i - 1];
const float3 &prev_pos_cu = deformation.positions[point_i - 1]; const float3 &pos_cu = deformation.positions[point_i];
const float3 &pos_cu = deformation.positions[point_i]; const float dist_to_brush_sq_cu = dist_squared_to_line_segment_v3(
const float dist_to_brush_sq_cu = dist_squared_to_line_segment_v3( brush_pos_cu, prev_pos_cu, pos_cu);
brush_pos_cu, prev_pos_cu, pos_cu); if (dist_to_brush_sq_cu > brush_radius_sq_cu) {
if (dist_to_brush_sq_cu > brush_radius_sq_cu) { continue;
continue;
}
const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
const float radius_falloff = BKE_brush_curve_strength(
brush_, dist_to_brush_cu, brush_radius_cu);
const float weight = radius_falloff;
math::max_inplace(r_curve_weights[curve_selection_i], weight);
} }
const float dist_to_brush_cu = std::sqrt(dist_to_brush_sq_cu);
const float radius_falloff = BKE_brush_curve_strength(
brush_, dist_to_brush_cu, brush_radius_cu);
math::max_inplace(max_weight, radius_falloff);
} }
r_curve_weights[curve_i] = max_weight;
}); });
} }
void puff(const Span<float> curve_weights) void puff(const IndexMask &selection, const Span<float> curve_weights)
{ {
BLI_assert(curve_weights.size() == curve_selection_.size());
const OffsetIndices points_by_curve = curves_->points_by_curve(); const OffsetIndices points_by_curve = curves_->points_by_curve();
MutableSpan<float3> positions_cu = curves_->positions_for_write(); MutableSpan<float3> positions_cu = curves_->positions_for_write();
threading::parallel_for(curve_selection_.index_range(), 256, [&](const IndexRange range) { selection.foreach_segment(GrainSize(256), [&](IndexMaskSegment segment) {
Vector<float> accumulated_lengths_cu; Vector<float> accumulated_lengths_cu;
for (const int curve_selection_i : range) { for (const int curve_i : segment) {
const int curve_i = curve_selection_[curve_selection_i];
const IndexRange points = points_by_curve[curve_i]; const IndexRange points = points_by_curve[curve_i];
const int first_point_i = points[0]; const int first_point_i = points[0];
const float3 first_pos_cu = positions_cu[first_point_i]; const float3 first_pos_cu = positions_cu[first_point_i];
@@ -339,7 +328,7 @@ struct PuffOperationExecutor {
const float3 goal_pos_cu = first_pos_cu + length_param_cu * normal_cu; const float3 goal_pos_cu = first_pos_cu + length_param_cu * normal_cu;
const float weight = 0.01f * brush_strength_ * point_factors_[point_i] * const float weight = 0.01f * brush_strength_ * point_factors_[point_i] *
curve_weights[curve_selection_i]; curve_weights[curve_i];
float3 new_pos_cu = math::interpolate(old_pos_cu, goal_pos_cu, weight); float3 new_pos_cu = math::interpolate(old_pos_cu, goal_pos_cu, weight);
/* Make sure the point does not move closer to the root point than it was initially. This /* Make sure the point does not move closer to the root point than it was initially. This

View File

@@ -186,9 +186,9 @@ struct SnakeHookOperatorExecutor {
const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_; const float brush_radius_re = brush_radius_base_re_ * brush_radius_factor_;
const float brush_radius_sq_re = pow2f(brush_radius_re); const float brush_radius_sq_re = pow2f(brush_radius_re);
threading::parallel_for(curves_->curves_range(), 256, [&](const IndexRange curves_range) { curve_selection_.foreach_segment(GrainSize(256), [&](const IndexMaskSegment segment) {
MoveAndResampleBuffers resample_buffer; MoveAndResampleBuffers resample_buffer;
for (const int curve_i : curves_range) { for (const int curve_i : segment) {
const IndexRange points = points_by_curve[curve_i]; const IndexRange points = points_by_curve[curve_i];
const int last_point_i = points.last(); const int last_point_i = points.last();
const float3 old_pos_cu = deformation.positions[last_point_i]; const float3 old_pos_cu = deformation.positions[last_point_i];
@@ -272,9 +272,9 @@ struct SnakeHookOperatorExecutor {
const float3 brush_diff_cu = brush_end_cu - brush_start_cu; const float3 brush_diff_cu = brush_end_cu - brush_start_cu;
const float brush_radius_sq_cu = pow2f(brush_radius_cu); const float brush_radius_sq_cu = pow2f(brush_radius_cu);
threading::parallel_for(curves_->curves_range(), 256, [&](const IndexRange curves_range) { curve_selection_.foreach_segment(GrainSize(256), [&](const IndexMaskSegment segment) {
MoveAndResampleBuffers resample_buffer; MoveAndResampleBuffers resample_buffer;
for (const int curve_i : curves_range) { for (const int curve_i : segment) {
const IndexRange points = points_by_curve[curve_i]; const IndexRange points = points_by_curve[curve_i];
const int last_point_i = points.last(); const int last_point_i = points.last();
const float3 old_pos_cu = deformation.positions[last_point_i]; const float3 old_pos_cu = deformation.positions[last_point_i];