Cleanup: improvements ED_markers_find_nearest_marker & it's doc-string

- Remove reference to variable in the function from the doc-string,
- Note that a non-empty marker list always returns a marker.
  Some callers assumed but wasn't the case until recently, see #136059.
- Replace null check in the look with an initial assignment to ensure
  a marker is always returned.
- Rename argument `x` to `frame`.
This commit is contained in:
Campbell Barton
2025-03-19 01:16:20 +00:00
parent c37455acac
commit 38ca9b0b87
2 changed files with 19 additions and 15 deletions

View File

@@ -142,20 +142,22 @@ int ED_markers_post_apply_transform(
/* --------------------------------- */
TimeMarker *ED_markers_find_nearest_marker(ListBase *markers, float x)
TimeMarker *ED_markers_find_nearest_marker(ListBase *markers, const float frame)
{
TimeMarker *nearest = nullptr;
if (markers == nullptr || BLI_listbase_is_empty(markers)) {
return nullptr;
}
if (markers) {
/* Don't initialize with a large/infinite value since a non-finite `x` could
* fail to return a marker which can crash in some cases, see: #136059. */
float min_dist;
LISTBASE_FOREACH (TimeMarker *, marker, markers) {
const float dist = fabsf(float(marker->frame) - x);
if (!nearest || (dist < min_dist)) {
min_dist = dist;
nearest = marker;
}
/* Always initialize the first so it's guaranteed to return a marker
* even if `frame` is NAN or the deltas are not finite. see: #136059. */
TimeMarker *marker = static_cast<TimeMarker *>(markers->first);
TimeMarker *nearest = marker;
float min_dist = fabsf(float(marker->frame) - frame);
for (marker = marker->next; marker; marker = marker->next) {
const float dist = fabsf(float(marker->frame) - frame);
if (dist < min_dist) {
min_dist = dist;
nearest = marker;
}
}

View File

@@ -71,10 +71,12 @@ int ED_markers_post_apply_transform(
ListBase *markers, Scene *scene, int mode, float value, char side);
/**
* Get the marker that is closest to this point.
* XXX: for select, the min_dist should be small.
* \return the marker that is closest to `frame`.
* Non-empty `markers` is guaranteed to return a marker.
*
* \note For selecting, the caller is expected to exclude markers beyond a small threshold.
*/
TimeMarker *ED_markers_find_nearest_marker(ListBase *markers, float x);
TimeMarker *ED_markers_find_nearest_marker(ListBase *markers, float frame);
/**
* Return the time of the marker that occurs on a frame closest to the given time.
*/