Skip to content

Removing "break" statements from While loop - calib3d, USAC source code #25651

Open
@Bruce387

Description

@Bruce387

System Information

There is a code smell "break" in loop in the calib3d, USAC source code in the following source code module:
sampler.cpp

See attached screenshots for examples:
Screenshot from 2024-05-17 22-40-18

Detailed description

The whle loops below should be rewritten to not rely on "break" statements to terminate. They should instead have a condition written into the while loops to terminate when a value is reached, instead of just "breaking".
Screenshot from 2024-05-17 22-40-18

Steps to reproduce

One can have embedded while loops to terminate where a break is reached. Here is the link to the file as is:

https://github.com/opencv/opencv/blob/4.x/modules/calib3d/src/usac/sampler.cpp

This can be written similar to as shown in the After chunk of code, compared to the Before chunk:

Before

void setSampleNumber (int k) {
    kth_sample_number = k;

    // If the method should act exactly like RANSAC
    if (kth_sample_number > growth_max_samples)
        return;
    else { // Increment the size of the sampling pool while required
        subset_size = sample_size; // reset subset size as from the beginning
        while (kth_sample_number > growth_function[subset_size-1]) {
            subset_size++;
            if (subset_size >= points_size){
                subset_size = points_size;
                break;
            }
        }
        if (termination_length < subset_size)
            termination_length = subset_size;
    }
}

Proposed

void setSampleNumber (int k) {
    kth_sample_number = k;
    **int stop = 0;**

    // If the method should act exactly like RANSAC
    if (kth_sample_number > growth_max_samples)
        return;
    else { // Increment the size of the sampling pool while required
        subset_size = sample_size; // reset subset size as from the beginning
        while (kth_sample_number > growth_function[subset_size-1] ) {
            subset_size++;
            if (subset_size >= points_size **and stop_int == 0**){
                subset_size = points_size;
                **stop = 1;**
            }
        }
        if (termination_length < subset_size)
            termination_length = subset_size;
    }
}

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions