gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 880dd88 2/5: Library (polygon): minor correcti


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 880dd88 2/5: Library (polygon): minor corrections to new functions
Date: Sat, 21 Mar 2020 17:59:04 -0400 (EDT)

branch: master
commit 880dd88f8c0dce340dbbcc8d2b72146e1ec29890
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Library (polygon): minor corrections to new functions
    
    Some minor corrections were made to the previous commit to make the code
    fit into the overall sytle of Gnuastro. Also two errors in building the
    documentation were fixed: there was no `@end deftypefun' for the two new
    functions.
    
    The stylistic corrections are listed below:
    
     - The generically named `func_ptr' was too confusing (What is this
       function? What does it do?). Also, there was no check to actually select
       the proper convex-only, or general functions. So I renamed it to
       `isinside' (which is the common part of both functions), and also
       defined a check so it can point to the proper function.
    
     - The braces within the two new functions didn't always follow the GNU
       style and in many cases, they were redundant: only having one statement
       within them. When there is only one statement, and there no confusion
       (for example with an `else' afterwards), it is much better to not put
       braces at all. They just confuse the readers of the code.
    
     - The length of the comments were adjusted to be 75 characters (with
       Emacs's M-Q).
---
 bin/crop/onecrop.c | 12 ++++++--
 doc/gnuastro.texi  |  3 +-
 lib/polygon.c      | 80 ++++++++++++++++++++++++------------------------------
 3 files changed, 47 insertions(+), 48 deletions(-)

diff --git a/bin/crop/onecrop.c b/bin/crop/onecrop.c
index 9f5f6a4..54cd9ad 100644
--- a/bin/crop/onecrop.c
+++ b/bin/crop/onecrop.c
@@ -334,7 +334,7 @@ onecrop_ipolygon_fl(double *ipolygon, size_t nvertices, 
long *fpixel,
     for(i=0;i<size;++i)                                                 \
       {                                                                 \
         point[0]=i%s1+1; point[1]=i/s1+1;                               \
-        if((*func_ptr)(ipolygon, point, nvertices)==outpolygon)           \
+        if((*isinside)(ipolygon, point, nvertices)==outpolygon)           \
           ba[i]=*bb;                                                    \
       }                                                                 \
     free(bb);                                                           \
@@ -348,7 +348,7 @@ polygonmask(struct onecropparams *crp, void *array, long 
*fpixel_i,
   int type=crp->p->type;
   double *ipolygon, point[2];
   int outpolygon=crp->p->outpolygon;
-  int (*func_ptr)(double *, double *, size_t);
+  int (*isinside)(double *, double *, size_t);
   size_t i, *ordinds, size=s0*s1, nvertices=crp->p->nvertices;
 
 
@@ -375,7 +375,13 @@ polygonmask(struct onecropparams *crp, void *array, long 
*fpixel_i,
       ipolygon[i*2+1] = crp->ipolygon[ordinds[i]*2+1] - fpixel_i[1];
     }
 
-  func_ptr=&gal_polygon_isinside;
+  /* Set the function for checking if a point is inside the polygon. For
+     concave polygons the process is more complex and thus
+     slower. Therefore when the polygon is convex, its better to use the
+     simpler/faster function. */
+  isinside = ( gal_polygon_isconvex(crp->ipolygon, crp->p->nvertices)
+               ? &gal_polygon_isinside_convex
+               : &gal_polygon_isinside );
 
   /* Go over all the pixels in the image and if they are within the
      polygon keep them if the user has asked for it.*/
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index f98afdc..64844a5 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -23162,7 +23162,6 @@ its self.
 The @code{GAL_POLYGON_MAX_CORNERS} macro is defined so there will be no
 need to allocate these temporary arrays separately. Since we are dealing
 with pixels, the polygon can't really have too many vertices.
-
 @end deftypefun
 
 @deftypefun double gal_polygon_area (double @code{*v}, size_t @code{n})
@@ -23186,10 +23185,12 @@ one of the edges of a polygon, this will return 
@code{0}.
 @deftypefun int gal_polygon_isinside(double @code{*v}, double @code{*p}, 
size_t @code{n})
 Returns @code{0} if point @code{p} in inside a polygon, either convex or 
concave, whose vertices are defined by @code{v} and @code{0} otherwise.
 This function uses the 
@url{https://en.wikipedia.org/wiki/Point_in_polygon#Winding_number_algorithm, 
winding number algorithm}, to check the points.
+@end deftypefun
 
 @deftypefun int gal_polygon_isconvex(double @code{*v}, size_t @code{n})
 Returns @code{1} if the polygon is convex with vertices defined by @code{v} 
and @code{0} if it is a concave polygon.
 Note that the vertices of the polygon should be sorted in an anti-clockwise 
manner.
+@end deftypefun
 
 @deftypefun void gal_polygon_clip (double @code{*s}, size_t @code{n}, double 
@code{*c}, size_t @code{m}, double @code{*o}, size_t @code{*numcrn})
 Clip (find the overlap of) two polygons. This function uses the
diff --git a/lib/polygon.c b/lib/polygon.c
index 33cf85f..2d794ff 100644
--- a/lib/polygon.c
+++ b/lib/polygon.c
@@ -268,18 +268,15 @@ gal_polygon_ppropin(double *v, double *p, size_t n)
 
 
 
-/* This fuction test if a point is inside the polygon using
-  winding number algorithm.
-
-  See its wiki here:
+/* This fuction test if a point is inside the polygon using winding number
+  algorithm. See its wiki here:
   https://en.wikipedia.org/wiki/Point_in_polygon#Winding_number_algorithm
 
-  We have a polygon with `n' vertices whose vertices are in the array
-  `v' (with 2*n elements). Such that v[0], v[1] are the two
-  coordinates of the first vertice. The vertices also have to be
-  sorted in a counter clockwise fashion. We also have a point (with
-  coordinates (x, y) == (p[0], p[1]) and we want to see if it is inside the
-  polygon or not.
+  We have a polygon with `n' vertices whose vertices are in the array `v'
+  (with 2*n elements). Such that v[0], v[1] are the two coordinates of the
+  first vertice. The vertices also have to be sorted in a counter clockwise
+  fashion. We also have a point (with coordinates (x, y) == (p[0], p[1])
+  and we want to see if it is inside the polygon or not.
 
   If point is outside the polygon, return 0.
   If point is inside the polygon, return a non-zero number.
@@ -294,41 +291,40 @@ gal_polygon_isinside(double *v, double *p, size_t n)
   /* Loop through all the edges of the polygon*/
   while(i<n)
     {
-    /* edge from v[i] to v[i+1] in upward direction */
-    if(v[j*2+1] <= p[1]){
-      if(v[i*2+1] > p[1]){
-        /* p left of edge is an upward intersection, increase wn */
-        if( GAL_POLYGON_TRI_CROSS_PRODUCT(&v[j*2], &v[i*2], p) > 0 ){
-          wn++;
-        }
-      }
-    }
-    else{
-      /* edge from v[i] to v[i+1] in downward direction */
-      if(v[i*2+1] <= p[1]){
-        /* p right of edge is a downward intersection, decrease wn */
-        if( GAL_POLYGON_TRI_CROSS_PRODUCT(&v[j*2], &v[i*2], p) < 0 ){
-          wn--;
+      /* Edge from v[i] to v[i+1] in upward direction */
+      if(v[j*2+1] <= p[1])
+        {
+          if(v[i*2+1] > p[1])
+            /* `p' left of edge is an upward intersection, increase wn. */
+            if( GAL_POLYGON_TRI_CROSS_PRODUCT(&v[j*2], &v[i*2], p) > 0 )
+              wn++;
         }
+      else{
+        /* edge from v[i] to v[i+1] in downward direction */
+        if(v[i*2+1] <= p[1])
+          /* p right of edge is a downward intersection, decrease wn */
+          if( GAL_POLYGON_TRI_CROSS_PRODUCT(&v[j*2], &v[i*2], p) < 0 )
+            wn--;
       }
-    }
-    j=i++;
-  /* For a check:
-    printf("winding number: %ld, %.3f\n", wn);
-    */
+
+      /* Increment `j' */
+      j=i++;
+
+      /* For a check:
+         printf("winding number: %ld, %.3f\n", wn);
+      */
     }
   return wn;
-
 }
 
 
 
 
 
-/* This function checks if the polygon is convex or concave by
-   testing all 3 consecutive points of the sorted polygon.
-   If any of the test returns false, the the polygon is concave
-   else it is convex.
+/* This function checks if the polygon is convex or concave by testing all
+   3 consecutive points of the sorted polygon. If any of the test returns
+   false, the the polygon is concave else it is convex.
+
    return 1: convex polygon
    return 0: concave polygon
    */
@@ -338,27 +334,23 @@ gal_polygon_isconvex(double *v, size_t n)
   size_t i;
   int flag=1;
 
-  /* check the first n-1 edges made by n points. */
+  /* Check the first n-1 edges made by n points. */
   for(i=0; i<n-2; i++)
     {
-    if( GAL_POLYGON_LEFT_OF_LINE(&v[i*2], &v[(i+1)*2], &v[(i+2)*2]) )
-      {
+      if( GAL_POLYGON_LEFT_OF_LINE(&v[i*2], &v[(i+1)*2], &v[(i+2)*2]) )
         continue;
-      }
-    else
-      {
+      else
         return 0;
-      }
     }
 
-  /* check the edge between nth and 1st point */
+  /* Check the edge between nth and 1st point */
   if(flag)
     {
       if( GAL_POLYGON_LEFT_OF_LINE(&v[(n-2)*2], &v[(n-1)*2], &v[0]) )
         return 1;
       else
         return 0;
-  }
+    }
 
   return 1;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]