calibrate.c File Reference

#include <stdio.h>
#include "calibrate.h"
Include dependency graph for calibrate.c:

Go to the source code of this file.

Functions

int perform_calibration (calibration *cal)

Variables

int flag_debug

Function Documentation

int perform_calibration ( calibration cal  ) 

Definition at line 40 of file calibrate.c.

References calibration::a, e, flag_debug, calibration::x, calibration::xfb, calibration::y, and calibration::yfb.

Referenced by ts_handle_event().

00040                                           {
00041    int j;
00042    float n, x, y, x2, y2, xy, z, zx, zy;
00043    float det, a, b, c, e, f, i;
00044    float scaling = 65536.0;
00045 
00046 // Get sums for matrix
00047    n = x = y = x2 = y2 = xy = 0;
00048    for(j=0;j<5;j++) {
00049       n += 1.0;
00050       x += (float)cal->x[j];
00051       y += (float)cal->y[j];
00052       x2 += (float)(cal->x[j]*cal->x[j]);
00053       y2 += (float)(cal->y[j]*cal->y[j]);
00054       xy += (float)(cal->x[j]*cal->y[j]);
00055    }
00056 
00057 // Get determinant of matrix -- check if determinant is too small
00058    det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
00059    if(det < 0.1 && det > -0.1) {
00060       fprintf(stderr, "ts_calibrate: determinant is too small -- %f\n",det);
00061       return 0;
00062    }
00063 
00064 // Get elements of inverse matrix
00065    a = (x2*y2 - xy*xy)/det;
00066    b = (xy*y - x*y2)/det;
00067    c = (x*xy - y*x2)/det;
00068    e = (n*y2 - y*y)/det;
00069    f = (x*y - n*xy)/det;
00070    i = (n*x2 - x*x)/det;
00071 
00072 // Get sums for x calibration
00073    z = zx = zy = 0;
00074    for(j=0;j<5;j++) {
00075       z += (float)cal->xfb[j];
00076       zx += (float)(cal->xfb[j]*cal->x[j]);
00077       zy += (float)(cal->xfb[j]*cal->y[j]);
00078    }
00079 
00080 // Now multiply out to get the calibration for framebuffer x coord
00081    cal->a[0] = (int)((a*z + b*zx + c*zy)*(scaling));
00082    cal->a[1] = (int)((b*z + e*zx + f*zy)*(scaling));
00083    cal->a[2] = (int)((c*z + f*zx + i*zy)*(scaling));
00084 
00085    if (flag_debug)
00086      fprintf(stderr, "%f %f %f\n",(a*z + b*zx + c*zy),
00087         (b*z + e*zx + f*zy),
00088         (c*z + f*zx + i*zy));
00089 
00090 // Get sums for y calibration
00091    z = zx = zy = 0;
00092    for(j=0;j<5;j++) {
00093       z += (float)cal->yfb[j];
00094       zx += (float)(cal->yfb[j]*cal->x[j]);
00095       zy += (float)(cal->yfb[j]*cal->y[j]);
00096    }
00097 
00098 // Now multiply out to get the calibration for framebuffer y coord
00099    cal->a[3] = (int)((a*z + b*zx + c*zy)*(scaling));
00100    cal->a[4] = (int)((b*z + e*zx + f*zy)*(scaling));
00101    cal->a[5] = (int)((c*z + f*zx + i*zy)*(scaling));
00102 
00103    if (flag_debug)
00104      fprintf(stderr, "%f %f %f\n",(a*z + b*zx + c*zy),
00105         (b*z + e*zx + f*zy),
00106         (c*z + f*zx + i*zy));
00107 
00108 // If we got here, we're OK, so assign scaling to a[6] and return
00109    cal->a[6] = (int)scaling;
00110    return 1;
00111 /* 
00112 // This code was here originally to just insert default values
00113    for(j=0;j<7;j++) {
00114       c->a[j]=0;
00115    }
00116    c->a[1] = c->a[5] = c->a[6] = 1;
00117    return 1;
00118 */
00119 
00120 }

Here is the caller graph for this function:


Variable Documentation

Generated by  doxygen 1.6.2-20100208