"Fossies" - the Fresh Open Source Software Archive

Member "saga-9.0.2/saga-gis/src/tools/contrib/contrib_perego/A2WiTh.cpp" (25 May 2023, 4437 Bytes) of package /linux/misc/saga-9.0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 /**********************************************************
    2  * Version $Id: A2WiTh.cpp 911 2011-02-14 16:38:15Z reklov_w $
    3  *********************************************************/
    4 
    5 ///////////////////////////////////////////////////////////
    6 //                                                       //
    7 //                        Tool:                        //
    8 //                        A2WiTh                         //
    9 //                                                       //
   10 //                       for SAGA                        //
   11 //      System for Automated Geoscientific Analyses      //
   12 //                                                       //
   13 //-------------------------------------------------------//
   14 //                                                       //
   15 //                      A2WiTh.cpp                       //
   16 //                                                       //
   17 //                                                       //
   18 //-------------------------------------------------------//
   19 //                                                       //
   20 //                                                       //
   21 //    by Alessandro Perego (Italy)                       //
   22 //                                                       //
   23 //    e-mail:     alper78@alice.it                       //
   24 //                                                       //
   25 //                                                       //
   26 ///////////////////////////////////////////////////////////
   27 
   28 //---------------------------------------------------------
   29 #include "A2WiTh.h"
   30 
   31 //---------------------------------------------------------
   32 CA2WiTh::CA2WiTh(void)
   33 {
   34     // 1. Info...
   35     Set_Name(_TL("Average With Thereshold 2"));
   36     Set_Author(_TL("Alessandro Perego"));
   37     Set_Description(_TL("Average 2 With Thereshold for Grids calculates average in X and Y distances unsing only the values that differ form central pixel less than a specified threshold. Each value has a weight which is inversely proportional to the distance (method 1)."));
   38 
   39     // 2. Parameters...
   40     Parameters.Add_Grid(NULL, "INPUT", _TL("Input"), _TL("This must be your input data of type grid."), PARAMETER_INPUT);
   41     Parameters.Add_Grid(NULL, "RESULT", _TL("AWT Grid"), _TL("New grid filtered with the A2WiTh tool"), PARAMETER_OUTPUT);
   42     Parameters.Add_Value(NULL, "RX", _TL("Radius X"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true);
   43     Parameters.Add_Value(NULL, "RY", _TL("Radius Y"), _TL(""), PARAMETER_TYPE_Int, 1, 1, true);
   44     Parameters.Add_Value(NULL, "THRESH", _TL("Threshold"), _TL("The values in the specified radius is used in the average calculation only if its difference with the central value is lesser or equal to this threshold."), PARAMETER_TYPE_Double, 2.0);
   45 }
   46 
   47 //---------------------------------------------------------
   48 CA2WiTh::~CA2WiTh(void)
   49 {}
   50 
   51 //---------------------------------------------------------
   52 bool CA2WiTh::On_Execute(void)
   53 {
   54     int  x, y, Rx, Ry, ax, ay, bx, by, ix, iy, iv, iw, irv, irw, n;
   55     double  Thresh, Diff, Sum, c, d, i;
   56     CSG_Grid  *pInput, *pResult;
   57 
   58     pInput = Parameters("INPUT")->asGrid();
   59     pResult = Parameters("RESULT")->asGrid();
   60     Rx = Parameters("RX")->asInt();
   61     Ry = Parameters("RY")->asInt();
   62     Thresh = Parameters("THRESH")->asDouble();
   63 
   64     //-----------------------------------------------------
   65     for(y=0; y<Get_NY() && Set_Progress_Rows(y); y++)
   66     {
   67         for(x=0; x<Get_NX(); x++)
   68         {
   69             Sum = 0.0;
   70             n = 0;
   71             //----------------------------------------------------
   72             
   73             if( (ax = x - Rx) <  0 )        {   ax  = 0;            }   
   74             if( (bx = x + Rx) >= Get_NX() ) {   bx  = Get_NX() - 1; }
   75             if( (ay = y - Ry) <  0 )        {   ay  = 0;            }
   76             if( (by = y + Ry) >= Get_NY() ) {   by  = Get_NY() - 1; }
   77 
   78             //-----------------------------------------------------
   79             for(iy=ay; iy<=by; iy++)
   80             {
   81                 for(ix=ax; ix<=bx; ix++)
   82                 {
   83                     iv = x -ix;
   84                     if( iv >= 0) { irv = Rx - iv + 1; }
   85                     else { irv = Rx + iv + 1; }
   86 
   87                     iw = y -iy;
   88                     if( iw >= 0) { irw = Ry - iw + 1; }
   89                     else { irw = Ry + iw + 1; }
   90 
   91                     c = pInput->asDouble(ix, iy);
   92                     d = pInput->asDouble(x, y);
   93                     Diff = c - d; 
   94                     if( Diff < 0)
   95                     {
   96                         Diff = 0 - Diff;
   97                     }
   98                     if( Diff <= Thresh )
   99                     {
  100                         i = pInput->asDouble(ix, iy);
  101                         Sum += ( i *( irv + irw ));
  102                         n += ( irv + irw );
  103                     }
  104                 }
  105             }
  106 
  107             //------------------------------------------------------
  108             if( n > 0 )
  109             {
  110                 pResult->Set_Value(x, y, Sum / n);
  111             }
  112 
  113         }
  114     }
  115 
  116     return( true );
  117 }
  118 
  119