"Fossies" - the Fresh Open Source Software Archive

Member "g3data-1.5.4/g3data/points.c" (8 Jan 2011, 7968 Bytes) of package /linux/privat/old/g3data-1.5.4.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. For more information about "points.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2                                                                                                                                                                      
    3 g3data : A program for grabbing data from scanned graphs
    4 Copyright (C) 2000 Jonas Frantz
    5                                                                                                                                                                      
    6     This file is part of g3data.
    7                                                                                                                                                                      
    8     g3data is free software; you can redistribute it and/or modify
    9     it under the terms of the GNU General Public License as published by
   10     the Free Software Foundation; either version 2 of the License, or
   11     (at your option) any later version.
   12                                                                                                                                                                      
   13     g3data is distributed in the hope that it will be useful,
   14     but WITHOUT ANY WARRANTY; without even the implied warranty of
   15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16     GNU General Public License for more details.
   17                                                                                                                                                                      
   18     You should have received a copy of the GNU General Public License
   19     along with this program; if not, write to the Free Software
   20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21                                                                                                                                                                      
   22                                                                                                                                                                      
   23 Authors email : jonas.frantz@welho.com
   24                                                                                                                                                                      
   25 */
   26 
   27 #include <stdio.h>
   28 #include <stdlib.h>
   29 #include <gtk/gtk.h>
   30 #include <math.h>
   31 #include "main.h"
   32 #include "sort.h"
   33 #include "points.h"
   34 
   35 /* Extern variables */
   36 
   37 extern  gint        axiscoords[MAXNUMTABS][4][2];
   38 extern  gint        **points[MAXNUMTABS];
   39 extern  gint        numpoints[MAXNUMTABS];
   40 extern  gint        ordering[MAXNUMTABS];
   41 extern  gdouble     realcoords[MAXNUMTABS][4];
   42 extern  gboolean    UseErrors[MAXNUMTABS];
   43 extern  gboolean    logxy[MAXNUMTABS][2];
   44 extern  gint        print2file[MAXNUMTABS];
   45 extern  gchar       *file_name[MAXNUMTABS];
   46 extern  gint        ViewedTabNum;
   47 
   48 /****************************************************************/
   49 /* This function sets the numpoints entry to numpoints variable */
   50 /* value.                           */
   51 /****************************************************************/
   52 void SetNumPointsEntry(GtkWidget *np_entry, gint np)
   53 {
   54   char buf[128];
   55 
   56     sprintf(buf,"%d",np);
   57     gtk_entry_set_text(GTK_ENTRY(np_entry),buf);
   58 }
   59 
   60 
   61 /****************************************************************/
   62 /* This function calculates the true value of the point based   */
   63 /* on the coordinates of the point on the bitmap.       */
   64 /****************************************************************/
   65 struct PointValue CalcPointValue(gint Xpos, gint Ypos, gint TabNum)
   66 {
   67   double alpha,beta,x21,x43,y21,y43,rlc[4];             /* Declare help variables */
   68   struct PointValue PV;
   69 
   70     x21=(double) axiscoords[TabNum][1][0]-axiscoords[TabNum][0][0];         /* Calculate deltax of x axis points */
   71     y21=(double) axiscoords[TabNum][1][1]-axiscoords[TabNum][0][1];         /* Calculate deltay of x axis points */
   72     x43=(double) axiscoords[TabNum][3][0]-axiscoords[TabNum][2][0];         /* Calculate deltax of y axis points */
   73     y43=(double) axiscoords[TabNum][3][1]-axiscoords[TabNum][2][1];         /* Calculate deltay of y axis points */
   74 
   75     if (logxy[TabNum][0]) {                         /* If x axis is logarithmic, store  */
   76     rlc[0]=log(realcoords[TabNum][0]);                  /* recalculated values in rlc.      */ 
   77     rlc[1]=log(realcoords[TabNum][1]);
   78     }
   79     else {
   80     rlc[0]=realcoords[TabNum][0];                       /* Else store old values in rlc.    */
   81     rlc[1]=realcoords[TabNum][1];
   82     }
   83 
   84     if (logxy[TabNum][1]) {
   85     rlc[2]=log(realcoords[TabNum][2]);                  /* If y axis is logarithmic, store      */
   86     rlc[3]=log(realcoords[TabNum][3]);                  /* recalculated values in rlc.          */
   87     }
   88     else {
   89     rlc[2]=realcoords[TabNum][2];                       /* Else store old values in rlc.        */
   90     rlc[3]=realcoords[TabNum][3];
   91     }
   92 
   93     alpha=((axiscoords[TabNum][0][0]-(double) Xpos)
   94               -(axiscoords[TabNum][0][1]-(double)  Ypos)*(x43/y43))/(x21-((y21*x43)/y43));
   95         beta=((axiscoords[TabNum][2][1]-(double) Ypos)
   96               -(axiscoords[TabNum][2][0]-(double) Xpos)*(y21/x21))/(y43-((x43*y21)/x21));
   97 
   98     if (logxy[TabNum][0]) PV.Xv = exp(-alpha*(rlc[1]-rlc[0])+rlc[0]);
   99     else PV.Xv = -alpha*(rlc[1]-rlc[0])+rlc[0];
  100 
  101     if (logxy[TabNum][1]) PV.Yv = exp(-beta*(rlc[3]-rlc[2])+rlc[2]);
  102     else PV.Yv = -beta*(rlc[3]-rlc[2])+rlc[2];
  103 
  104     alpha=((axiscoords[TabNum][0][0]-(double) (Xpos+1))
  105               -(axiscoords[TabNum][0][1]-(double) (Ypos+1))*(x43/y43))/(x21-((y21*x43)/y43));
  106         beta=((axiscoords[TabNum][2][1]-(double) (Ypos+1))
  107               -(axiscoords[TabNum][2][0]-(double) (Xpos+1))*(y21/x21))/(y43-((x43*y21)/x21));
  108 
  109     if (logxy[TabNum][0]) PV.Xerr = exp(-alpha*(rlc[1]-rlc[0])+rlc[0]);
  110     else PV.Xerr = -alpha*(rlc[1]-rlc[0])+rlc[0];
  111 
  112     if (logxy[TabNum][1]) PV.Yerr = exp(-beta*(rlc[3]-rlc[2])+rlc[2]);
  113     else PV.Yerr = -beta*(rlc[3]-rlc[2])+rlc[2];
  114 
  115     alpha=((axiscoords[TabNum][0][0]-(double) (Xpos-1))
  116               -(axiscoords[TabNum][0][1]-(double) (Ypos-1))*(x43/y43))/(x21-((y21*x43)/y43));
  117         beta=((axiscoords[TabNum][2][1]-(double) (Ypos-1))
  118               -(axiscoords[TabNum][2][0]-(double) (Xpos-1))*(y21/x21))/(y43-((x43*y21)/x21));
  119 
  120     if (logxy[TabNum][0]) PV.Xerr -= exp(-alpha*(rlc[1]-rlc[0])+rlc[0]);
  121     else PV.Xerr -= -alpha*(rlc[1]-rlc[0])+rlc[0];
  122 
  123     if (logxy[TabNum][1]) PV.Yerr -= exp(-beta*(rlc[3]-rlc[2])+rlc[2]);
  124     else PV.Yerr -= -beta*(rlc[3]-rlc[2])+rlc[2];
  125 
  126     PV.Xerr = fabs(PV.Xerr/4.0);
  127     PV.Yerr = fabs(PV.Yerr/4.0);
  128 
  129     return PV;
  130 }
  131 
  132 
  133 /****************************************************************/
  134 /* This function is called when the "Print results" button is   */
  135 /* pressed, it calculate the values of the datapoints and   */
  136 /* prints them through stdout.                  */
  137 /****************************************************************/
  138 void print_results(GtkWidget *widget, gpointer func_data) 
  139 {
  140   gint i;                               /* Declare index variable */
  141   FILE *FP;
  142 
  143   struct PointValue *RealPos, CalcVal;
  144 
  145     if (print2file[ViewedTabNum] == TRUE) {
  146     FP = fopen(file_name[ViewedTabNum],"w");                    /* Open file for writing */
  147     if (FP==NULL) {
  148         printf("Could not open %s for writing\n",file_name[ViewedTabNum]);  /* If unable to open print error */
  149         return;
  150     }
  151     } else {
  152         FP = stdout;
  153     }
  154 
  155     RealPos = (struct PointValue *) malloc(sizeof(struct PointValue) * numpoints[ViewedTabNum]);
  156 
  157 /* Next up is recalculating the positions of the points by solving a 2*2 matrix */
  158 
  159     for (i=0;i<numpoints[ViewedTabNum];i++) {
  160         CalcVal = CalcPointValue(points[ViewedTabNum][i][0], points[ViewedTabNum][i][1], ViewedTabNum);
  161     RealPos[i].Xv = CalcVal.Xv;
  162     RealPos[i].Yv = CalcVal.Yv;
  163     RealPos[i].Xerr = CalcVal.Xerr;
  164     RealPos[i].Yerr = CalcVal.Yerr;
  165     }
  166 
  167     if (ordering[ViewedTabNum] != 0) {
  168     Order(RealPos, numpoints[ViewedTabNum], ordering[ViewedTabNum]);
  169     }
  170 
  171 /* Print results to stdout or file */
  172 
  173     for (i=0;i<numpoints[ViewedTabNum];i++) {
  174         fprintf(FP,"%.12g  %.12g", RealPos[i].Xv, RealPos[i].Yv);
  175         if (UseErrors[ViewedTabNum]) {
  176         fprintf(FP,"\t%.12g  %.12g\n", RealPos[i].Xerr, RealPos[i].Yerr);
  177         } else fprintf(FP,"\n");
  178     }
  179     free(RealPos);
  180 
  181     if (print2file[ViewedTabNum] == TRUE) fclose(FP);
  182 }