Refactor: use IndexRange for loops in graph_draw.cc #120786

Merged
Christoph Lendenfeld merged 2 commits from ChrisLend/blender:refactor_graph_draw_use_index_range into main 2024-04-19 10:10:35 +02:00
1 changed files with 19 additions and 16 deletions

View File

@ -53,7 +53,9 @@ static float fcurve_display_alpha(FCurve *fcu)
}
/** Get the first and last index to the bezt array that are just outside min and max. */
static blender::int2 get_bounding_bezt_indices(FCurve *fcu, const float min, const float max)
static blender::IndexRange get_bounding_bezt_index_range(FCurve *fcu,
const float min,
const float max)
{
bool replace;
int first, last;
@ -63,7 +65,8 @@ static blender::int2 get_bounding_bezt_indices(FCurve *fcu, const float min, con
last = BKE_fcurve_bezt_binarysearch_index(fcu->bezt, max, fcu->totvert, &replace);
last = replace ? last + 1 : last;
last = clamp_i(last, 0, fcu->totvert - 1);
return {first, last};
/* Iterating over index range is exlusive of the last index. But we need `last` to be visited. */
return blender::IndexRange(first, (last - first) + 1);
}
/** \} */
@ -206,13 +209,13 @@ static void draw_cross(float position[2], const float scale[2], uint attr_id)
static void draw_fcurve_selected_keyframe_vertices(FCurve *fcu,
bool sel,
uint pos,
const blender::int2 &bounding_indices)
const blender::IndexRange index_range)

IndexRange is a small struct like Span that should be passed by value

IndexRange is a small struct like Span that should be passed by value
{
set_fcurve_vertex_color(fcu, sel);
immBeginAtMost(GPU_PRIM_POINTS, fcu->totvert);
for (int i = bounding_indices[0]; i <= bounding_indices[1]; i++) {
for (const int i : index_range) {
BezTriple *bezt = &fcu->bezt[i];
/* 'Keyframe' vertex only, as handle lines and handles have already been drawn
* - only draw those with correct selection state for the current drawing color
@ -265,10 +268,10 @@ static void draw_fcurve_keyframe_vertices(FCurve *fcu, View2D *v2d, const uint p
immUniform1f("size", (UI_GetThemeValuef(TH_VERTEX_SIZE) * UI_SCALE_FAC) * 0.8f);
}
const blender::int2 bounding_indices = get_bounding_bezt_indices(
const blender::IndexRange index_range = get_bounding_bezt_index_range(
fcu, v2d->cur.xmin, v2d->cur.xmax);
draw_fcurve_selected_keyframe_vertices(fcu, false, pos, bounding_indices);
draw_fcurve_selected_keyframe_vertices(fcu, true, pos, bounding_indices);
draw_fcurve_selected_keyframe_vertices(fcu, false, pos, index_range);
draw_fcurve_selected_keyframe_vertices(fcu, true, pos, index_range);
draw_fcurve_active_vertex(fcu, v2d, pos);
immUnbindProgram();
@ -278,7 +281,7 @@ static void draw_fcurve_keyframe_vertices(FCurve *fcu, View2D *v2d, const uint p
static void draw_fcurve_selected_handle_vertices(
FCurve *fcu, View2D *v2d, bool sel, bool sel_handle_only, uint pos)
{
const blender::int2 bounding_indices = get_bounding_bezt_indices(
const blender::IndexRange index_range = get_bounding_bezt_index_range(
fcu, v2d->cur.xmin, v2d->cur.xmax);
/* set handle color */
@ -290,7 +293,7 @@ static void draw_fcurve_selected_handle_vertices(
immBeginAtMost(GPU_PRIM_POINTS, fcu->totvert * 2);
BezTriple *prevbezt = nullptr;
for (int i = bounding_indices[0]; i <= bounding_indices[1]; i++) {
for (const int i : index_range) {
BezTriple *bezt = &fcu->bezt[i];
/* Draw the editmode handles for a bezier curve (others don't have handles)
* if their selection status matches the selection status we're drawing for
@ -449,7 +452,7 @@ static void draw_fcurve_handles(SpaceGraph *sipo, ARegion *region, FCurve *fcu)
immBeginAtMost(GPU_PRIM_LINES, 4 * 2 * fcu->totvert);
const int2 bounding_indices = get_bounding_bezt_indices(
const IndexRange index_range = get_bounding_bezt_index_range(
fcu, region->v2d.cur.xmin, region->v2d.cur.xmax);
/* slightly hacky, but we want to draw unselected points before selected ones
@ -460,7 +463,7 @@ static void draw_fcurve_handles(SpaceGraph *sipo, ARegion *region, FCurve *fcu)
uchar col[4];
BezTriple *prevbezt = nullptr;
for (int i = bounding_indices[0]; i <= bounding_indices[1]; i++) {
for (const int i : index_range) {
BezTriple *bezt = &fcu->bezt[i];
/* if only selected keyframes can get their handles shown,
* check that keyframe is selected
@ -1030,11 +1033,11 @@ static void draw_fcurve_curve_keys(
add_extrapolation_point_left(fcu, v2d->cur.xmin, curve_vertices);
}
const int2 bounding_indices = get_bounding_bezt_indices(fcu, v2d->cur.xmin, v2d->cur.xmax);
const IndexRange index_range = get_bounding_bezt_index_range(fcu, v2d->cur.xmin, v2d->cur.xmax);
/* Always add the first point so the extrapolation line doesn't jump. */
curve_vertices.append(
{fcu->bezt[bounding_indices[0]].vec[1][0], fcu->bezt[bounding_indices[0]].vec[1][1]});
{fcu->bezt[index_range.first()].vec[1][0], fcu->bezt[index_range.first()].vec[1][1]});
const blender::float2 pixels_per_unit = calculate_pixels_per_unit(v2d);
const int window_width = BLI_rcti_size_x(&v2d->mask);
@ -1043,7 +1046,7 @@ static void draw_fcurve_curve_keys(
const float samples_per_pixel = 0.66f;
const float evaluation_step = pixel_width / samples_per_pixel;
BezTriple *first_key = &fcu->bezt[bounding_indices[0]];
BezTriple *first_key = &fcu->bezt[index_range.first()];
rctf key_bounds = {
first_key->vec[1][0], first_key->vec[1][1], first_key->vec[1][0], first_key->vec[1][1]};
/* Used when skipping keys. */
@ -1051,7 +1054,7 @@ static void draw_fcurve_curve_keys(
const float min_pixel_distance = 3.0f;
/* Draw curve between first and last keyframe (if there are enough to do so). */
for (int i = bounding_indices[0] + 1; i <= bounding_indices[1]; i++) {
for (const int i : index_range.drop_front(1)) {
BezTriple *prevbezt = &fcu->bezt[i - 1];
BezTriple *bezt = &fcu->bezt[i];
expand_key_bounds(prevbezt, bezt, key_bounds);
@ -1114,7 +1117,7 @@ static void draw_fcurve_curve_keys(
/* Always add the last point so the extrapolation line doesn't jump. */
curve_vertices.append(
{fcu->bezt[bounding_indices[1]].vec[1][0], fcu->bezt[bounding_indices[1]].vec[1][1]});
{fcu->bezt[index_range.last()].vec[1][0], fcu->bezt[index_range.last()].vec[1][1]});
/* Extrapolate to the right? (see code for left-extrapolation above too) */
if (draw_extrapolation && fcu->bezt[fcu->totvert - 1].vec[1][0] < v2d->cur.xmax) {