imgproc/drawing: minor refactoring in FillConvexPoly
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Wed, 7 Dec 2016 00:43:02 +0000 (03:43 +0300)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Thu, 8 Dec 2016 19:14:32 +0000 (22:14 +0300)
modules/imgproc/src/drawing.cpp

index bd838c6..113f499 100644 (file)
@@ -1099,7 +1099,7 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin
     edge[2];
 
     int delta = 1 << shift >> 1;
-    int i, y, imin = 0, left = 0, right = 1;
+    int i, y, imin = 0;
     int edges = npts;
     int64 xmin, xmax, ymin, ymax;
     uchar* ptr = img.ptr();
@@ -1181,44 +1181,46 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin
             {
                 if( y >= edge[i].ye )
                 {
-                    int idx = edge[i].idx, di = edge[i].di;
-                    int64 xs = 0, xe;
+                    int idx0 = edge[i].idx, di = edge[i].di;
+                    int idx = idx0 + di;
+                    if (idx >= npts) idx -= npts;
                     int ty = 0;
 
-                    for(;;)
+                    for (; edges-- > 0; )
                     {
                         ty = (int)((v[idx].y + delta) >> shift);
-                        if( ty > y || edges == 0 )
+                        if (ty > y)
+                        {
+                            int64 xs = v[idx0].x;
+                            int64 xe = v[idx].x;
+                            if (shift != XY_SHIFT)
+                            {
+                                xs <<= XY_SHIFT - shift;
+                                xe <<= XY_SHIFT - shift;
+                            }
+
+                            edge[i].ye = ty;
+                            edge[i].dx = ((xe - xs)*2 + (ty - y)) / (2 * (ty - y));
+                            edge[i].x = xs;
+                            edge[i].idx = idx;
                             break;
-                        xs = v[idx].x;
+                        }
+                        idx0 = idx;
                         idx += di;
-                        idx -= ((idx < npts) - 1) & npts;   /* idx -= idx >= npts ? npts : 0 */
-                        edges--;
+                        if (idx >= npts) idx -= npts;
                     }
-
-                    xs <<= XY_SHIFT - shift;
-                    xe = v[idx].x << (XY_SHIFT - shift);
-
-                    /* no more edges */
-                    if( y >= ty)
-                        return;
-
-                    edge[i].ye = ty;
-                    edge[i].dx = ((xe - xs)*2 + (ty - y)) / (2 * (ty - y));
-                    edge[i].x = xs;
-                    edge[i].idx = idx;
                 }
             }
         }
 
-        if( edge[left].x > edge[right].x )
+        if (y >= 0)
         {
-            left ^= 1;
-            right ^= 1;
-        }
+            int left = 0, right = 1;
+            if (edge[0].x > edge[1].x)
+            {
+                left = 1, right = 0;
+            }
 
-        if( y >= 0 )
-        {
             int xx1 = (int)((edge[left].x + delta1) >> XY_SHIFT);
             int xx2 = (int)((edge[right].x + delta2) >> XY_SHIFT);
 
@@ -1231,9 +1233,13 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin
                 ICV_HLINE( ptr, xx1, xx2, color, pix_size );
             }
         }
+        else
+        {
+            // TODO optimize scan for negative y
+        }
 
-        edge[left].x += edge[left].dx;
-        edge[right].x += edge[right].dx;
+        edge[0].x += edge[0].dx;
+        edge[1].x += edge[1].dx;
         ptr += img.step;
     }
     while( ++y <= (int)ymax );