Skip to content

Instantly share code, notes, and snippets.

@gylns
Created July 13, 2016 14:43
Show Gist options
  • Select an option

  • Save gylns/e3f4ce708d79c468b2569cfaed8f268c to your computer and use it in GitHub Desktop.

Select an option

Save gylns/e3f4ce708d79c468b2569cfaed8f268c to your computer and use it in GitHub Desktop.

Revisions

  1. gylns created this gist Jul 13, 2016.
    30 changes: 30 additions & 0 deletions fastAtan2.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    // from mathfuncs_core.cpp of opencv
    void fastAtan2(const float *Y, const float *X, float *angle, int len, bool angleInDegrees )
    {
    int i = 0;
    float scale = angleInDegrees ? 1 : (float)(CV_PI/180);

    for( ; i < len; i++ )
    {
    float x = X[i], y = Y[i];
    float ax = std::abs(x), ay = std::abs(y);
    float a, c, c2;
    if( ax >= ay )
    {
    c = ay/(ax + (float)DBL_EPSILON);
    c2 = c*c;
    a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    }
    else
    {
    c = ax/(ay + (float)DBL_EPSILON);
    c2 = c*c;
    a = 90.f - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
    }
    if( x < 0 )
    a = 180.f - a;
    if( y < 0 )
    a = 360.f - a;
    angle[i] = (float)(a*scale);
    }
    }