Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 of 4 tasks
Bruce387 opened this issue May 27, 2024 · 0 comments
Open
2 of 4 tasks
Labels

Comments

@Bruce387
Copy link

Bruce387 commented May 27, 2024

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)
@Bruce387 Bruce387 added the bug label May 27, 2024
@Bruce387 Bruce387 changed the title Removing "break" statements from loops - calib3d Removing "break" statements from While loop - calib3d, USAC source code May 27, 2024
@opencv-alalek opencv-alalek added invalid and removed bug labels May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants