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

Signed distance field optimizations #275

Closed
duvisit opened this issue Dec 10, 2023 · 4 comments
Closed

Signed distance field optimizations #275

duvisit opened this issue Dec 10, 2023 · 4 comments

Comments

@duvisit
Copy link
Contributor

duvisit commented Dec 10, 2023

After trying demos and reading through source code of edt3aafunc.c I think there is some room for small optimizations.

  • Use float instead of double in distance field computations.

  • Remove lines from computegradient() function


#if 0
glength = gx[k]*gx[k] + gy[k]*gy[k];
if(glength > 0.0) { // Avoid division by zero
    glength = sqrt(glength);
    gx[k]=gx[k]/glength;
    gy[k]=gy[k]/glength;
}
#endif

and lines from edgedf() function


#if 0
    if(glength>0) {
        gx = gx/glength;
        gy = gy/glength;
    }
    /* Everything is symmetric wrt sign and transposition,
     * so move to first octant (gx>=0, gy>=0, gx>=gy) to
     * avoid handling all possible edge directions.
     */
    gx = fabs(gx);
    gy = fabs(gy);
#else
    gx = fabs(gx/glength);
    gy = fabs(gy/glength);
#endif

Division by zero is handled by the if (gx==0 || gy==0) few lines before.

  • In edtaa3() function use:

  #if 0
    if(a > 1.0) a = 1.0;
    if(a < 0.0) a = 0.0; // Clip grayscale values outside the range [0,1]
    if(a == 0.0) return 1000000.0; // Not an object pixel, return "very far" ("don't know yet")
  #else
    if(a <= 0.0f) return 1000000.0f;
    if(a > 1.0f) a = 1.0f;
  #endif

Hope this doesn't break anything.

Great library, bye.

@rougier
Copy link
Owner

rougier commented Dec 13, 2023

Thanks. the edt3aafunc.c comes from another library and mostly used for illustration. Not sure it's worth optimizing it and it may break things. Did you notice huge speedup with your patch?

@duvisit
Copy link
Contributor Author

duvisit commented Dec 13, 2023

I get around 20% improvements. Probably mostly from double to float change.

@rougier
Copy link
Owner

rougier commented Jan 9, 2024

Maybe not worth the change.

@duvisit
Copy link
Contributor Author

duvisit commented Jan 10, 2024

I agree. PR #278 is better.

@rougier rougier closed this as completed Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants