gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 4d853b4 112/125: Old neighbors.h header remove


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 4d853b4 112/125: Old neighbors.h header removed, used new in MakeProfiles
Date: Sun, 23 Apr 2017 22:36:50 -0400 (EDT)

branch: master
commit 4d853b42958a26177b01a6eeabbfa103e8aecb32
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Old neighbors.h header removed, used new in MakeProfiles
    
    In the previous commit, a new macro was defined to easily parse over the
    neighbors of an index in an array. But the old `lib/neighbors.h' header
    still existed and was used in MakeProfiles to build the profile from the
    center outwards. So as a good test for that macro and also a cleaner
    implementation of MakeProfiles, the new macro is now used and the old
    `lib/neighbors.h' header has been completely removed.
---
 bin/mkprof/oneprofile.c  |  81 ++++++++++---------------
 lib/Makefile.am          |   2 +-
 lib/gnuastro/dimension.h |   6 +-
 lib/interpolate.c        |   2 +-
 lib/neighbors.h          | 152 -----------------------------------------------
 5 files changed, 39 insertions(+), 204 deletions(-)

diff --git a/bin/mkprof/oneprofile.c b/bin/mkprof/oneprofile.c
index c0409ed..e0e3fdd 100644
--- a/bin/mkprof/oneprofile.c
+++ b/bin/mkprof/oneprofile.c
@@ -35,11 +35,11 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 #include <gnuastro/fits.h>
 #include <gnuastro/array.h>
+#include <gnuastro/dimension.h>
 #include <gnuastro/statistics.h>
 #include <gnuastro/linkedlist.h>
 
 #include <timing.h>
-#include <neighbors.h>
 
 #include "main.h"
 
@@ -225,17 +225,18 @@ integ2d(struct mkonthread *mkp)
 static void
 makepixbypix(struct mkonthread *mkp)
 {
-  float circ_r;
-  struct gal_linkedlist_sll *Q=NULL;
-  unsigned char *byt;
-  float *img=mkp->ibq->img;
+  size_t ndim=2, dsize[2]={mkp->width[1], mkp->width[0]};
+
+  uint8_t *byt;
   int use_rand_points=1, ispeak=1;
   struct builtqueue *ibq=mkp->ibq;
-  size_t is1=mkp->width[0], is0=mkp->width[1];
-  size_t p, ngb[4], *ind=&p, numngb, *n, *nf, x, y;
+  float circ_r, *img=mkp->ibq->img;
+  struct gal_linkedlist_sll *Q=NULL;
+  size_t *dinc=gal_dimension_increment(ndim, dsize);
   double tolerance=mkp->p->tolerance, pixfrac, junk;
   double (*profile)(struct mkonthread *)=mkp->profile;
   double xc=mkp->xc, yc=mkp->yc, os=mkp->p->oversample;
+  size_t p, x, y, is1=mkp->width[0], is0=mkp->width[1];
   double truncr=mkp->truncr, approx, hp=0.5f/mkp->p->oversample;
 
   /* lQ: Largest. sQ: Smallest in queue */
@@ -254,12 +255,7 @@ makepixbypix(struct mkonthread *mkp)
     { img[p]=1; return; }
 
   /* Allocate the byt array to not repeat completed pixels. */
-  errno=0;
-  byt=calloc(is0*is1, sizeof *byt);
-  if(byt==NULL)
-    error(EXIT_FAILURE, 0, "%zu bytes for map of object in row %zu of "
-          "data in %s", is0*is1*sizeof *byt, ibq->id,
-          mkp->p->catname);
+  byt = gal_data_malloc_array(GAL_DATA_TYPE_UINT8, is0*is1);
 
   /* Start the queue: */
   byt[p]=1;
@@ -281,8 +277,7 @@ makepixbypix(struct mkonthread *mkp)
              over sampled image. But all the profile parameters are in the
              non-oversampled image. So we divide the distance by os
              (p->oversample in double type) */
-          gal_linkedlist_pop_from_tosll_start(&lQ, &sQ,
-                                              ind, &circ_r); /* ind=&p */
+          gal_linkedlist_pop_from_tosll_start(&lQ, &sQ, &p, &circ_r);
           mkp->x=(p/is1-xc)/os;
           mkp->y=(p%is1-yc)/os;
           r_el(mkp);
@@ -311,25 +306,17 @@ makepixbypix(struct mkonthread *mkp)
           ++ibq->numaccu;
           ibq->accufrac+=img[p];
 
-          /*
-            printf("\tac: %f, ap: %f, frac: %f\n", img[p], approx,
-            fabs(img[p]-approx)/img[p]);
-            gal_fits_array_to_file("tmp.fits", "", FLOAT_IMG, img, is0,
-                                   is1, NULL, SPACK_STRING);
-          */
-
-          /* Go over the neighbours and add them to queue of elements
-             to check. */
-          GAL_NEIGHBORS_FILL_4_ALLIMG;
-          nf=(n=ngb)+numngb;
-          do
-            if(byt[*n]==0)
-              {
-                byt[*n]=1;
-                gal_linkedlist_add_to_tosll_end( &lQ, &sQ, *n,
-                                                 r_circle(*n, mkp) );
-              }
-          while(++n<nf);
+          /* Go over the neighbors and add them to queue of elements to
+             check if they haven't been done already. */
+          GAL_DIMENSION_NEIGHBOR_OP(p, ndim, dsize, 1, dinc,
+            {
+              if(byt[nind]==0)
+                {
+                  byt[nind]=1;
+                  gal_linkedlist_add_to_tosll_end( &lQ, &sQ, nind,
+                                                   r_circle(nind, mkp) );
+                }
+            } );
 
           if(use_rand_points==0) break;
         }
@@ -344,7 +331,7 @@ makepixbypix(struct mkonthread *mkp)
   /* Order doesn't matter any more, add all the pixels you find. */
   while(Q)
     {
-      gal_linkedlist_pop_from_sll(&Q, ind);        /* ind=&p */
+      gal_linkedlist_pop_from_sll(&Q, &p);
       mkp->x=(p/is1-xc)/os;
       mkp->y=(p%is1-yc)/os;
       r_el(mkp);
@@ -363,23 +350,21 @@ makepixbypix(struct mkonthread *mkp)
       /* Save the peak flux if this is the first pixel: */
       if(ispeak) { mkp->peakflux=img[p]; ispeak=0; }
 
-      /*
-      gal_fits_array_to_file("tmp.fits", "", FLOAT_IMG, img, is0, is1,
-                              NULL, SPACK_STRING);
-      */
       /* Go over the neighbours and add them to queue of elements
          to check. */
-      GAL_NEIGHBORS_FILL_4_ALLIMG;
-      nf=(n=ngb)+numngb;
-      do
-        if(byt[*n]==0)
-          {
-            byt[*n]=1;
-            gal_linkedlist_add_to_sll(&Q, *n);
-          }
-      while(++n<nf);
+      GAL_DIMENSION_NEIGHBOR_OP(p, ndim, dsize, 1, dinc,
+        {
+          if(byt[nind]==0)
+            {
+              byt[nind]=1;
+              gal_linkedlist_add_to_sll(&Q, nind);
+            }
+        } );
     }
+
+  /* Clean up. */
   free(byt);
+  free(dinc);
 }
 
 
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 180f879..ffa5cb4 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -83,7 +83,7 @@ pkginclude_HEADERS = gnuastro/config.h 
$(headersdir)/arithmetic.h          \
 # distribute them here.
 EXTRA_DIST = $(headersdir)/README gnuastro.pc.in arithmetic-binary.h    \
   arithmetic-onlyint.h arithmetic-other.h config.h.in checkset.h        \
-  fixedstringmacros.h neighbors.h options.h timing.h
+  fixedstringmacros.h options.h timing.h
 
 
 
diff --git a/lib/gnuastro/dimension.h b/lib/gnuastro/dimension.h
index b27ff70..60a0c8b 100644
--- a/lib/gnuastro/dimension.h
+++ b/lib/gnuastro/dimension.h
@@ -142,7 +142,9 @@ gal_dimension_index_to_coord(size_t ind, size_t ndim, 
size_t *dsize,
                 each dimension. You can make this array with the following
                 function:
 
-                  dinc=gal_dimension_increment(ndim, dsize);
+                  size_t *dinc=gal_dimension_increment(ndim, dsize);
+
+                Don't forget to free it afterwards.
 
         `operation': Any C operation. `nind' is a `size_t' type variable
                 that is defined by this macro and will have the index of
@@ -167,7 +169,7 @@ gal_dimension_index_to_coord(size_t ind, size_t ndim, 
size_t *dsize,
    The bit information is in two two-byte spaces, so in theory, this works
    for 16 dimensions.
 */
-#define gal_dimension_neighbor_op(index, ndim, dsize, connectivity,     \
+#define GAL_DIMENSION_NEIGHBOR_OP(index, ndim, dsize, connectivity,     \
                                   dinc, operation) {                    \
     uint32_t bitstr=0;                                                  \
     size_t nind, ind=index;                                             \
diff --git a/lib/interpolate.c b/lib/interpolate.c
index a61a0f2..0723d46 100644
--- a/lib/interpolate.c
+++ b/lib/interpolate.c
@@ -52,7 +52,7 @@ gal_interpolate(gal_data_t *input)
     if(flagarr[F])
       {
         printf("to be filled: %zu\n", F);
-        gal_dimension_neighbor_op(F, input->ndim, input->dsize, 2,
+        GAL_DIMENSION_NEIGHBOR_OP(F, input->ndim, input->dsize, 2,
                                   dinc, {printf("\tneighbor: %zu\n", nind);});
       }
 
diff --git a/lib/neighbors.h b/lib/neighbors.h
deleted file mode 100644
index 6104ac1..0000000
--- a/lib/neighbors.h
+++ /dev/null
@@ -1,152 +0,0 @@
-/*********************************************************************
-neighbors.h -- Find the neighbours around a pixel.
-This is part of GNU Astronomy Utilities (Gnuastro) package.
-
-Original author:
-     Mohammad Akhlaghi <address@hidden>
-Contributing author(s):
-Copyright (C) 2015, Free Software Foundation, Inc.
-
-Gnuastro is free software: you can redistribute it and/or modify it
-under the terms of the GNU General Public License as published by the
-Free Software Foundation, either version 3 of the License, or (at your
-option) any later version.
-
-Gnuastro is distributed in the hope that it will be useful, but
-WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
-**********************************************************************/
-#ifndef __GAL_NEIGHBORS_H__
-#define __GAL_NEIGHBORS_H__
-
-/* Include other headers if necessary here. Note that other header files
-   must be included before the C++ preparations below */
-
-
-
-/* C++ Preparations */
-#undef __BEGIN_C_DECLS
-#undef __END_C_DECLS
-#ifdef __cplusplus
-# define __BEGIN_C_DECLS extern "C" {
-# define __END_C_DECLS }
-#else
-# define __BEGIN_C_DECLS                /* empty */
-# define __END_C_DECLS                  /* empty */
-#endif
-/* End of C++ preparations */
-
-
-
-/* Actual header contants (the above were for the Pre-processor). */
-__BEGIN_C_DECLS  /* From C++ preparations */
-
-
-/* These macros can be put any where you want to know the neighbors of
-   an index, the important values that have to be declared and defined
-   before this macro are:
-
-   Inputs:
-   ind        : Pointer to the index being considered.
-   (is0, is1) : Input image number of rows and columns.
-   (x0,y0)    : The bottom left corner of image or mesh box (included).
-   (x1,y1)    : The Top right corner of image or mesh box (not included).
-
-   Outputs:
-   numngb     : Number of filled neighbors.
-   *ngb       : Array keeping the neighbours indexs (allocated outside).
-*/
-
-#define GAL_NEIGHBORS_FILL_4_REGION {                                   \
-    numngb=0;                                                           \
-    if (*ind/is1!=x0  ) ngb[numngb++]=*ind-is1;                         \
-    if (*ind/is1!=x1-1) ngb[numngb++]=*ind+is1;                         \
-    if (*ind%is1!=y0  ) ngb[numngb++]=*ind-1;                           \
-    if (*ind%is1!=y1-1) ngb[numngb++]=*ind+1;                           \
-  }
-
-
-#define GAL_NEIGHBORS_FILL_4_ALLIMG {                                   \
-    numngb=0;                                                           \
-    if (*ind/is1!=0    ) ngb[numngb++]=*ind-is1;                        \
-    if (*ind/is1!=is0-1) ngb[numngb++]=*ind+is1;                        \
-    if (*ind%is1!=0    ) ngb[numngb++]=*ind-1;                          \
-    if (*ind%is1!=is1-1) ngb[numngb++]=*ind+1;                          \
-  }
-
-
-#define GAL_NEIGHBORS_FILL_8_REGION {                                   \
-    unsigned char bl=0, br=0, tl=0, tr=0;                               \
-    numngb=0;                                                           \
-    if (*ind/is1!=x0  ) {ngb[numngb++]=*ind-is1; ++bl; ++br;}           \
-    if (*ind/is1!=x1-1) {ngb[numngb++]=*ind+is1; ++tl; ++tr;}           \
-    if (*ind%is1!=y0  ) {ngb[numngb++]=*ind-1;   ++bl; ++tl;}           \
-    if (*ind%is1!=y1-1) {ngb[numngb++]=*ind+1;   ++tr; ++br;}           \
-    if (numngb==4)                                                      \
-      {                                                                 \
-        numngb=8;                                                       \
-        ngb[4]=*ind-is1-1; ngb[5]=*ind-is1+1;                           \
-        ngb[6]=*ind+is1-1; ngb[7]=*ind+is1+1;                           \
-      }                                                                 \
-    else                                                                \
-      {                                                                 \
-        if(bl==2) ngb[numngb++]=*ind-is1-1;                             \
-        if(br==2) ngb[numngb++]=*ind-is1+1;                             \
-        if(tl==2) ngb[numngb++]=*ind+is1-1;                             \
-        if(tr==2) ngb[numngb++]=*ind+is1+1;                             \
-      }                                                                 \
-  }
-
-
-#define GAL_NEIGHBORS_FILL_8_ALLIMG {                                   \
-    unsigned char bl=0, br=0, tl=0, tr=0;                               \
-    numngb=0;                                                           \
-    if (*ind/is1!=0     ) {ngb[numngb++]=*ind-is1; ++bl; ++br;}         \
-    if (*ind/is1!=is0-1 ) {ngb[numngb++]=*ind+is1; ++tl; ++tr;}         \
-    if (*ind%is1!=0     ) {ngb[numngb++]=*ind-1;   ++bl; ++tl;}         \
-    if (*ind%is1!=is1-1 ) {ngb[numngb++]=*ind+1;   ++tr; ++br;}         \
-    if (numngb==4)                                                      \
-      {                                                                 \
-        numngb=8;                                                       \
-        ngb[4]=*ind-is1-1; ngb[5]=*ind-is1+1;                           \
-        ngb[6]=*ind+is1-1; ngb[7]=*ind+is1+1;                           \
-      }                                                                 \
-    else                                                                \
-      {                                                                 \
-        if(bl==2) ngb[numngb++]=*ind-is1-1;                             \
-        if(br==2) ngb[numngb++]=*ind-is1+1;                             \
-        if(tl==2) ngb[numngb++]=*ind+is1-1;                             \
-        if(tr==2) ngb[numngb++]=*ind+is1+1;                             \
-      }                                                                 \
-  }
-
-
-#define GAL_NEIGHBORS_FILL_8_ALLIMG_IJ {                                \
-    unsigned char bl=0, br=0, tl=0, tr=0;                               \
-    numngb=0;                                                           \
-    if (i!=0     ) {ngb[numngb++]=(i-1)*is1+j; ++bl; ++br;}             \
-    if (i!=is0-1 ) {ngb[numngb++]=(i+1)*is1+j; ++tl; ++tr;}             \
-    if (j!=0     ) {ngb[numngb++]=i*is1+j-1;   ++bl; ++tl;}             \
-    if (j!=is1-1 ) {ngb[numngb++]=i*is1+j+1;   ++tr; ++br;}             \
-    if (numngb==4)                                                      \
-      {                                                                 \
-        numngb=8;                                                       \
-        ngb[4]=(i-1)*is1+j-1; ngb[5]=(i-1)*is1+j+1;                     \
-        ngb[6]=(i+1)*is1+j-1; ngb[7]=(i+1)*is1+j+1;                     \
-      }                                                                 \
-    else                                                                \
-      {                                                                 \
-        if(bl==2) ngb[numngb++]=(i-1)*is1+j-1;                          \
-        if(br==2) ngb[numngb++]=(i-1)*is1+j+1;                          \
-        if(tl==2) ngb[numngb++]=(i+1)*is1+j-1;                          \
-        if(tr==2) ngb[numngb++]=(i+1)*is1+j+1;                          \
-      }                                                                 \
-  }
-
-__END_C_DECLS    /* From C++ preparations */
-
-#endif           /* __GAL_NEIGHBORS_H__ */



reply via email to

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