slopeabs.C

Go to the documentation of this file.
00001 /* Gnome gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  *
00005  * Quick nine points Gxsm Plugin GUIDE by PZ:
00006  * ------------------------------------------------------------
00007  * 1.) Make a copy of this "SlopeAbs.C" to "your_plugins_name.C"!
00008  * 2.) Replace all "SlopeAbs" by "your_plugins_name" 
00009  *     --> please do a search and replace starting here NOW!! (Emacs doese preserve caps!)
00010  * 3.) Decide: One or Two Source Math: see line 54
00011  * 4.) Fill in GxsmPlugin Structure, see below
00012  * 5.) Replace the "about_text" below a desired
00013  * 6.) * Optional: Start your Code/Vars definition below (if needed more than the run-fkt itself!), 
00014  *       Goto Line 155 ff. please, and see comment there!!
00015  * 7.) Fill in math code in SlopeAbs_run(), 
00016  *     have a look at the Data-Access methods infos at end
00017  * 8.) Add SlopeAbs.C to the Makefile.am in analogy to others
00018  * 9.) Make a "make; make install"
00019  * A.) Call Gxsm->Tools->reload Plugins, be happy!
00020  * ... That's it!
00021  * 
00022  * Gxsm Plugin Name: SlopeAbs.C
00023  * ========================================
00024  * 
00025  * Copyright (C) 1999 The Free Software Foundation
00026  *
00027  * Authors: Percy Zahl <zahl@fkp.uni-hannover.de>
00028  * additional features: Andreas Klust <klust@fkp.uni-hannover.de>
00029  *
00030  * This program is free software; you can redistribute it and/or modify
00031  * it under the terms of the GNU General Public License as published by
00032  * the Free Software Foundation; either version 2 of the License, or
00033  * (at your option) any later version.
00034  *
00035  * This program is distributed in the hope that it will be useful,
00036  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00037  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00038  * GNU General Public License for more details.
00039  *
00040  * You should have received a copy of the GNU General Public License
00041  * along with this program; if not, write to the Free Software
00042  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00043  */
00044 
00045 /* Please do not change the Begin/End lines of this comment section!
00046  * this is a LaTeX style section used for auto generation of the PlugIn Manual 
00047  * Chapter. Add a complete PlugIn documentation inbetween the Begin/End marks!
00048  * --------------------------------------------------------------------------------
00049 % BeginPlugInDocuSection
00050 % PlugInDocuCaption: Calculate gradient (slope)
00051 
00052 % PlugInName: SlopeAbs
00053 
00054 % PlugInAuthor: Percy Zahl
00055 
00056 % PlugInAuthorEmail: zahl@users.sf.net
00057 
00058 % PlugInMenuPath: Math/Statistics/Slope Abs
00059 
00060 % PlugInDescription
00061 Calculation of the absolute local slope (gradient) using a user
00062 defined facet size at each pixel as reference area.  A plane
00063 regression is performed at each pixel to find the best matching local
00064 facet of the given size. Its normal is used to find the gradient.
00065 
00066 % PlugInUsage
00067 Activate chanel to use and call it from Menu \emph{Math/Statistics/Slope Abs}.
00068 
00069 % OptPlugInSources
00070 The active channel is used.
00071 
00072 % OptPlugInDest
00073 Existing Math channel, else newly created Math channel.
00074 
00075 % OptPlugInConfig
00076 Can set a default facet size, if set to zero it will ask at each call.
00077 
00078 % OptPlugInKnownBugs
00079 No bugs known.
00080 
00081 % EndPlugInDocuSection
00082  * -------------------------------------------------------------------------------- 
00083  */
00084 
00085 #include <gtk/gtk.h>
00086 #include "config.h"
00087 #include "gxsm/plugin.h"
00088 
00089 // Plugin Prototypes
00090 static void SlopeAbs_init( void );
00091 static void SlopeAbs_about( void );
00092 static void SlopeAbs_configure( void );
00093 static void SlopeAbs_cleanup( void );
00094 
00095 // Define Type of math plugin here, only one line should be commented in!!
00096 #define GXSM_ONE_SRC_PLUGIN__DEF
00097 // #define GXSM_TWO_SRC_PLUGIN__DEF
00098 
00099 // Math-Run-Function, use only one of (automatically done :=)
00100 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00101 // "OneSrc" Prototype
00102  static gboolean SlopeAbs_run( Scan *Src, Scan *Dest );
00103 #else
00104 // "TwoSrc" Prototype
00105  static gboolean SlopeAbs_run( Scan *Src1, Scan *Src2, Scan *Dest );
00106 #endif
00107 
00108 // Fill in the GxsmPlugin Description here
00109 GxsmPlugin SlopeAbs_pi = {
00110   NULL,                   // filled in and used by Gxsm, don't touch !
00111   NULL,                   // filled in and used by Gxsm, don't touch !
00112   0,                      // filled in and used by Gxsm, don't touch !
00113   NULL,                   // The Gxsm-App Class Ref.pointer (called "gapp" in Gxsm) is 
00114                           // filled in here by Gxsm on Plugin load, 
00115                           // just after init() is called !!!
00116   // ----------------------------------------------------------------------
00117   // Plugins Name, CodeStly is like: Name-M1S|M2S-BG|F1D|F2D|ST|TR|Misc
00118   "SlopeAbs-"
00119 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00120   "M1S"
00121 #else
00122   "M2S"
00123 #endif
00124   "-ST",
00125   // Plugin's Category - used to autodecide on Pluginloading or ignoring
00126   // NULL: load, else
00127   // example: "+noHARD +STM +AFM"
00128   // load only, if "+noHARD: no hardware" and Instrument is STM or AFM
00129   // +/-xxxHARD und (+/-INST or ...)
00130   NULL,
00131   // Description, is shown by PluginViewer (Plugin: listplugin, Tools->Plugin Details)
00132   " Calculates absolute local slope!",                   
00133   // Author(s)
00134   "Percy Zahl",
00135   // Menupath to position where it is appendet to
00136   N_("_Math/_Statistics/"),
00137   // Menuentry
00138   N_("Slope Abs"),
00139   // help text shown on menu
00140   N_("Sorry, no help for SlopeAbs filter!"),
00141   // more info...
00142   "no more info",
00143   NULL,          // error msg, plugin may put error status msg here later
00144   NULL,          // Plugin Status, managed by Gxsm, plugin may manipulate it too
00145   // init-function pointer, can be "NULL", 
00146   // called if present at plugin load
00147   SlopeAbs_init,  
00148   // query-function pointer, can be "NULL", 
00149   // called if present after plugin init to let plugin manage it install itself
00150   NULL, // query should be "NULL" for Gxsm-Math-Plugin !!!
00151   // about-function, can be "NULL"
00152   // can be called by "Plugin Details"
00153   SlopeAbs_about,
00154   // configure-function, can be "NULL"
00155   // can be called by "Plugin Details"
00156   SlopeAbs_configure,
00157   // run-function, can be "NULL", if non-Zero and no query defined, 
00158   // it is called on menupath->"plugin"
00159   NULL, // run should be "NULL" for Gxsm-Math-Plugin !!!
00160   // cleanup-function, can be "NULL"
00161   // called if present at plugin removeal
00162   SlopeAbs_cleanup
00163 };
00164 
00165 // special math Plugin-Strucure, use
00166 // GxsmMathOneSrcPlugin SlopeAbs_m1s_pi -> "OneSrcMath"
00167 // GxsmMathTwoSrcPlugin SlopeAbs_m2s_pi -> "TwoSrcMath"
00168 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00169  GxsmMathOneSrcPlugin SlopeAbs_m1s_pi
00170 #else
00171  GxsmMathTwoSrcPlugin SlopeAbs_m2s_pi
00172 #endif
00173  = {
00174    // math-function to run, see prototype(s) above!!
00175    SlopeAbs_run
00176  };
00177 
00178 // Text used in Aboutbox, please update!!
00179 static const char *about_text = N_("Gxsm SlopeAbs Plugin\n\n"
00180                                    "SlopeAbs calculates the\n"
00181                                    "absolute local Slope.");
00182 
00183 int FacetRadius=2;
00184 int FacetRadiusDefault=0;
00185 
00186 // Symbol "get_gxsm_plugin_info" is resolved by dlsym from Gxsm, used to get Plugin's info!! 
00187 // Essential Plugin Function!!
00188 GxsmPlugin *get_gxsm_plugin_info ( void ){ 
00189   SlopeAbs_pi.description = g_strdup_printf(N_("Gxsm MathOneArg SlopeAbs plugin %s"), VERSION);
00190   return &SlopeAbs_pi; 
00191 }
00192 
00193 // Symbol "get_gxsm_math_one|two_src_plugin_info" is resolved by dlsym from Gxsm, 
00194 // used to find out which Math Type the Plugin is!! 
00195 // Essential Plugin Function!!
00196 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00197 GxsmMathOneSrcPlugin *get_gxsm_math_one_src_plugin_info( void ) {
00198   return &SlopeAbs_m1s_pi; 
00199 }
00200 #else
00201 GxsmMathTwoSrcPlugin *get_gxsm_math_two_src_plugin_info( void ) { 
00202   return &SlopeAbs_m2s_pi; 
00203 }
00204 #endif
00205 
00206 // 5.) Start here with the plugins code, vars def., etc.... here.
00207 // ----------------------------------------------------------------------
00208 //
00209 
00210 
00211 // init-Function
00212 static void SlopeAbs_init(void)
00213 {
00214   PI_DEBUG (DBG_L2, "SlopeAbs Plugin Init");
00215 }
00216 
00217 // about-Function
00218 static void SlopeAbs_about(void)
00219 {
00220   const gchar *authors[] = { SlopeAbs_pi.authors, NULL};
00221   gtk_widget_show(gnome_about_new ( SlopeAbs_pi.name,
00222                                     VERSION,
00223                                     N_("(C) 2000 the Free Software Foundation"),
00224                                     about_text,
00225                                     authors,
00226                                     NULL, NULL, NULL
00227                                     ));
00228 }
00229 
00230 // configure-Function
00231 static void SlopeAbs_configure(void)
00232 {
00233   double x = (double)FacetRadiusDefault;
00234   SlopeAbs_pi.app->ValueRequest("Enter Value", "FacetRadius", 
00235                                 "Default FacetRadius (Pixel), if set to Zero I will ask later!",
00236                                 SlopeAbs_pi.app->xsm->Unity, 
00237                                 1., 100., ".0f", &x);
00238   FacetRadiusDefault = (int)x;
00239 }
00240 
00241 // cleanup-Function
00242 static void SlopeAbs_cleanup(void)
00243 {
00244   PI_DEBUG (DBG_L2, "SlopeAbs Plugin Cleanup");
00245 }
00246 
00247 typedef struct {
00248   double b,ax,ay;
00249 }APlane;
00250 
00251 typedef struct {
00252   int Cline, Crow, Crx, Cry;
00253 }Facet;
00254 
00255 // Do E Regress
00256 void FacetERegress(Scan *Src, Facet *fac, APlane *ap){
00257   double sumi, sumiy, sumyq, sumiq, sumy, val;
00258   double ysumi, ysumiy, ysumyq, ysumiq, ysumy;
00259   double a, b, nx, ny, imean, ymean,ax,bx,ay,by;
00260   int line, i;
00261 
00262   sumi = sumiq = sumy = sumyq = sumiy = ax = ay = bx = by = 0.0;
00263   ysumi = ysumiq = ysumy = ysumyq = ysumiy = 0.0;
00264   nx = ny = 0.;
00265   
00266   for (i = -fac->Crx; i <= fac->Crx; ++i){
00267     sumiq += (double)(i)*(double)(i);
00268     sumi  += (double)i;
00269     nx += 1.;
00270   }
00271   imean = sumi / nx;
00272   for (line = -fac->Cry; line <= fac->Cry; line++) {
00273     sumy = sumyq = sumiy = 0.0;
00274     for (i = -fac->Crx; i <= fac->Crx; i++) {
00275       val = Src->mem2d->GetDataPkt(i+fac->Crow, line+fac->Cline);
00276       sumy   += val;
00277       sumyq  += val*val;
00278       sumiy  += val*i;
00279     }
00280     ymean = sumy / nx;
00281     a = (sumiy - nx * imean * ymean ) / (sumiq - nx * imean * imean);
00282     b =  ymean -  a * imean;
00283     ax += a;
00284     bx += b;
00285     /* Werte fuer y-linregress */
00286     ysumy  += b;
00287     ysumyq += b*b;
00288     ysumiy += b * line;
00289     ysumiq += (double)(line)*(double)(line);
00290     ysumi  += (double)line;
00291     ny     += 1.;
00292   }
00293   ax = ax/ny;
00294   bx = bx/ny;
00295   imean = ysumi / ny;
00296   ymean = ysumy / ny;
00297   ay = (ysumiy - ny * imean * ymean ) / (ysumiq - ny * imean * imean);
00298   by =  ymean - ay * imean;
00299 
00300   // E-Facet: Y = by + xi*ax + yi*ay
00301 
00302   ap->b  = by;
00303   ap->ax = ax;
00304   ap->ay = ay;
00305 }
00306 
00307 
00308 // run-Function
00309 #ifdef GXSM_ONE_SRC_PLUGIN__DEF
00310  static gboolean SlopeAbs_run(Scan *Src, Scan *Dest)
00311 #else
00312  static gboolean SlopeAbs_run(Scan *Src1, Scan *Src2, Scan *Dest)
00313 #endif
00314 {
00315   // put plugins math code here...
00316 
00317   if(FacetRadiusDefault)
00318     FacetRadius=FacetRadiusDefault;
00319   else{
00320       double x=(double)FacetRadius;
00321       SlopeAbs_pi.app->ValueRequest("Enter Value", "FacetRadius", 
00322                                     "need FacetRadius (in Pixel)!",
00323                                     SlopeAbs_pi.app->xsm->Unity, 
00324                                     1., 100., ".0f", &x);
00325       FacetRadius = (int)x;
00326   }
00327 
00328   if(   Src->mem2d->GetNx() <= 2*FacetRadius 
00329      || Src->mem2d->GetNy() <= 2*FacetRadius)
00330     return MATH_SIZEERR;
00331 
00332   Dest->mem2d->Resize(Dest->data.s.nx, Dest->data.s.ny, ZD_FLOAT);
00333   Dest->mem2d->data->MkXLookup(-Src->data.s.rx/2.,
00334                                +Src->data.s.rx/2.);
00335   Dest->mem2d->data->MkYLookup(0., Src->data.s.ry);
00336   UnitObj *u;
00337   Dest->data.SetZUnit(u=new UnitObj("°","°")); delete u;
00338   Dest->data.s.dz  = 1.;
00339   Dest->data.s.rz  = 90.;
00340  
00341   APlane ap;
00342   Facet  facet;
00343   facet.Crx   = FacetRadius;
00344   facet.Cry   = FacetRadius;
00345   double fac = Src->data.s.dz/((Src->data.s.dx+Src->data.s.dy)/2.);
00346   for(int line=FacetRadius; line<Src->mem2d->GetNy()-FacetRadius; ++line){
00347     gchar *mld = g_strdup_printf("Calculating SlopeAbs: %d/%d", 
00348                                  line, Dest->data.s.ny);
00349     gapp->SetStatus(mld); 
00350     g_free(mld);
00351     SET_PROGRESS((double)line/(double)Dest->data.s.ny);
00352     for(int row=FacetRadius; row<Src->mem2d->GetNx()-FacetRadius; ++row){
00353       facet.Crow  = row;
00354       facet.Cline = line;
00355       FacetERegress(Src, &facet, &ap);
00356       //      Dest->mem2d->PutDataPkt(sqrt(ap.ax*ap.ax + ap.ay*ap.ay)*fac, row, line);
00357       Dest->mem2d->PutDataPkt(180.*acos(1./sqrt(1. + (ap.ax*ap.ax + ap.ay*ap.ay)*fac*fac))/M_PI, row, line);
00358       //      Dest->mem2d->PutDataPkt(180.*atan(sqrt(ap.ax*ap.ax + ap.ay*ap.ay)*fac)/M_PI, row, line);
00359     }
00360   }
00361   SET_PROGRESS(0.);
00362 
00363   return MATH_OK;
00364 }

Generated on Sat Apr 1 09:04:23 2006 for GXSM by  doxygen 1.4.6