~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/tools/smartROI/sroi_io.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * sroi_io.c
  3  *
  4  * Functions for reading and writing model data
  5  *
  6  */
  7 
  8 #include <stdio.h>
  9 #include <values.h>
 10 #include <math.h>
 11 #include <tina/sys.h>
 12 #include <tina/sysfuncs.h>
 13 #include <tina/math.h>
 14 #include <tina/mathfuncs.h>
 15 #include <tina/vision.h>
 16 #include <tina/visionfuncs.h>
 17 #include <tina/tv.h>
 18 #include <tina/tvfuncs.h>
 19 #include <tina/draw.h>
 20 #include <tina/drawfuncs.h>
 21 #include <tina/infrtoolfuncs.h>
 22 #include <tina/toolsfuncs.h>
 23 #include <tina/vector.h>
 24 #include <tina/sroi_tool.h>
 25 
 26 /*
 27  * Local variables
 28  */
 29 
 30 static FILE *blfp = NULL;
 31 static int   blcount = 0;
 32 static int   bltotal = 0;
 33 
 34 
 35 int get_int(FILE * f)
 36 {
 37   int n = 0;
 38 
 39   fscanf(f, "%d", &n);
 40   return n;
 41 }
 42 
 43 float get_float(FILE * f)
 44 {
 45   float x = 0.0;
 46 
 47   fscanf(f, "%f", &x);
 48   return x;
 49 }
 50 
 51 double get_ledouble(FILE * f)
 52 {
 53   double x = 0.0;
 54 
 55   fscanf(f, "%le", &x);
 56   return x;
 57 }
 58 
 59 double get_lfdouble(FILE * f)
 60 {
 61   double x = 0.0;
 62 
 63   fscanf(f, "%lf", &x);
 64   return x;
 65 }
 66 
 67 void dvector_printf(FILE * f, Vector * vec)
 68 {
 69   double *el = (double *) vec->data;
 70   int i, n = vec->n;
 71 
 72   fprintf(f, "double ");
 73   fprintf(f, "%d ", n);
 74   for (i = 0; i < n; i++)
 75     fprintf(f, "%f ", el[i]);
 76 }
 77 
 78 void dvector_scanf(FILE * f, Vector * vec)
 79 {
 80 
 81   double *el = (double *) vec->data;
 82   int i, n;
 83   char type[64];
 84 
 85   fscanf(f, "%s", type);
 86   if (strcmp(type, "double") != 0)
 87   {
 88     error("Wrong data type in file", non_fatal);
 89     return;
 90   }
 91   vec->n = get_int(f);
 92   for (i = 0; i < vec->n; i++)
 93     el[i] = get_lfdouble(f);
 94 }
 95 
 96 
 97 void dmatrix_scanf(FILE * f, Matrix * mat)
 98 {
 99   double **el = mat->el.double_v;
100   int i, j, m, n;
101   char dtype[64], mtype[64];
102 
103   fscanf(f, "%s", dtype);
104   if (strcmp(dtype, "double") != 0)
105   {
106     error("Wrong data type in file", non_fatal);
107     return;
108   }
109   switch (mat->shape)
110   {
111   case matrix_full:
112     fscanf(f, "%s", mtype);
113     if (strcmp(mtype, "full") != 0)
114     {
115       error("Wrong matrix shape in file", non_fatal);
116       return;
117     }
118     mat->m = get_int(f);
119     mat->n = get_int(f);
120     for (i = 0; i < mat->m; ++i)
121       for (j = 0; j < mat->n; ++j)
122         el[i][j] = get_lfdouble(f);
123     break;
124   case matrix_lower:
125     fscanf(f, "%s", mtype);
126     if (strcmp(mtype, "lower") != 0)
127     {
128       error("Wrong matrix shape in file", non_fatal);
129       return;
130     }
131     mat->m = get_int(f);
132     mat->n = get_int(f);
133     for (i = 0; i < mat->m; ++i)
134       for (j = 0; j < i; ++j)
135         el[i][j] = get_lfdouble(f);
136     break;
137   case matrix_upper:
138     fscanf(f, "%s", mtype);
139     if (strcmp(mtype, "upper") != 0)
140     {
141       error("Wrong matrix shape in file", non_fatal);
142       return;
143     }
144     mat->m = get_int(f);
145     mat->n = get_int(f);
146     for (i = 0; i < mat->m; ++i)
147       for (j = i; j < mat->m; ++j)
148         el[i][j] = get_lfdouble(f);
149     break;
150   case matrix_symmetric:
151     fscanf(f, "%s", mtype);
152     if (strcmp(mtype, "symmetric") != 0)
153     {
154       error("Wrong matrix shape in file", non_fatal);
155       return;
156     }
157     mat->m = get_int(f);
158     mat->n = get_int(f);
159     for (i = 0; i < mat->m; ++i)
160       for (j = 0; j <= i; ++j)
161         el[i][j] = get_lfdouble(f);
162     break;
163   default:
164     error("Wrong matrix shape in file", non_fatal);
165     return;
166   }
167 }
168 
169 
170 void sroi_sample_profile(Imrect *im)
171 {
172   Model *model = NULL;
173   Tv *tv = NULL;
174   double *a, **el, val, x0, y0, x, y;
175   float **p;
176   int n, m, i, j, *v, max, *ma, middle, length;
177 
178   if ((model = get_model()) == NULL)
179     return;
180 
181   el = model->m->el.double_v;
182   a = (double *) model->alpha->data;
183   p = model->profile->el.float_v;
184   middle = model->vsize;
185   length = 2 * model->vsize - 1;
186 
187   for (i = 0; i < model->c; i++)
188   {
189     x0 = cos(a[i]) * (double) (middle - 1);
190     y0 = sin(a[i]) * (double) (middle - 1);
191     for (j = 0; j < length; j++)
192     {
193       x = cos(a[i]) * (double) j - x0;
194       y = sin(a[i]) * (double) j - y0;
195       p[i][j] = im_sub_pixf(im, (el[1][i] + y), (el[0][i] + x));
196     }
197   }
198 }
199 
200 
201 void position()
202 {
203   Model *model = NULL;
204   Mat2 m;
205   Vec2 v;
206   double **el, **ev, *w, x, y;
207   int i, j;
208 
209   if ((model = get_model()) == NULL)
210     return;
211 
212   el = model->m->el.double_v;
213   ev = model->M_Evec->el.double_v;
214   m = rot2(-model->theta);
215   for (i = 0; i < model->c; i++)
216   {
217     x = el[2][i];
218     y = el[3][i];
219     for (j = 0; j < model->m_modes; j++)
220     {
221       x += ev[model->c + i][j] * ((double *) (model->m_weight->data))[j];
222       y += ev[i][j] * ((double *) (model->m_weight->data))[j];
223     }
224     v = mat2_vprod(m, vec2(x, y));
225     el[0][i] = (v.el[0] * model->s) + model->tx;
226     el[1][i] = (v.el[1] * model->s) + model->ty;
227   }
228 }
229 
230 
231 void i_position()
232 {
233   Model *model = NULL;
234   Mat2 m;
235   Vec2 v;
236   double **el;
237   int i;
238 
239   if ((model = get_model()) == NULL)
240     return;
241 
242   el = model->m->el.double_v;
243   m = rot2(model->theta);
244   for (i = 0; i < model->c; i++)
245   {
246     v = mat2_vprod(m, vec2(el[0][i] - model->tx,
247                            el[1][i] - model->ty));
248     el[2][i] = v.el[0] / model->s;
249     el[3][i] = v.el[1] / model->s;
250   }
251 }
252 
253 
254 void angle_points()
255 {
256   Model *model = NULL;
257   double dx, dy;
258   double *a, **el;
259   int nm1, i;
260 
261   if ((model = get_model()) == NULL)
262     return;
263 
264   el = model->m->el.double_v;
265   a = (double *) model->alpha->data;
266   nm1 = model->o - 1;
267   i = 0;
268   dx = el[0][nm1] - el[0][i + 1];
269   dy = el[1][i + 1] - el[1][nm1];
270   a[i] = atan2(dx, dy);
271   for (i = 1; i < nm1; i++)
272   {
273     dx = el[0][i - 1] - el[0][i + 1];
274     dy = el[1][i + 1] - el[1][i - 1];
275     a[i] = atan2(dx, dy);
276   }
277   dx = el[0][i - 1] - el[0][0];
278   dy = el[1][0] - el[1][i - 1];
279   a[i] = atan2(dx, dy);
280   if (model->c > model->o)
281   {
282     nm1 = model->c - 1;
283     i = model->o;
284     dx = el[0][nm1] - el[0][i + 1];
285     dy = el[1][i + 1] - el[1][nm1];
286     a[i] = atan2(dx, dy);
287     for (i = model->o + 1; i < nm1; i++)
288     {
289       dx = el[0][i - 1] - el[0][i + 1];
290       dy = el[1][i + 1] - el[1][i - 1];
291       a[i] = atan2(dx, dy);
292     }
293     dx = el[0][i - 1] - el[0][model->o];
294     dy = el[1][model->o] - el[1][i - 1];
295     a[i] = atan2(dx, dy);
296   }
297 }
298 
299 
300 void output_model(char *fname)
301 {
302   char temps[256];
303   FILE *fp;
304   int i, j;
305   float **p;
306   double **el;
307   Model *model = NULL;
308 
309   
310   if ((model = get_model()) == NULL)
311     return;
312 
313   if ((fp = fopen(fname, "wt")) == NULL)
314   {
315     string_append(temps, "can't open file", fname, 0);
316     error(temps, non_fatal);
317     return;
318   }
319   el = model->m->el.double_v;
320   p = model->profile->el.float_v;
321 
322   fprintf(fp, "%d ", model->r);
323   fprintf(fp, "%d ", model->c);
324   fprintf(fp, "%d ", model->o);
325   fprintf(fp, "%le ", model->tx);
326   fprintf(fp, "%le ", model->ty);
327   fprintf(fp, "%le ", model->s);
328   fprintf(fp, "%le ", model->theta);
329   fprintf(fp, "%d ", model->vsize);
330   fprintf(fp, "\n");
331 
332   for (i = 0; i < model->r; i++)
333   {
334     for (j = 0; j < model->c; j++)
335       fprintf(fp, "%le ", el[i][j]);
336     fprintf(fp, "\n");
337   }
338   for (i = 0; i < model->c; i++)
339   {
340     for (j = 0; j < 2 * model->vsize - 1; j++)
341       fprintf(fp, "%f ", p[i][j]);
342     fprintf(fp, "\n");
343   }
344   fflush(fp);
345   fclose(fp);
346 }
347 
348 
349 
350 void input_model(char *fname)
351 {
352   Model *model = get_model();
353   FILE *fp;
354   int i, j, r, c, o, vsize;
355   float **p;
356   double **el, tx, ty, s, theta;
357   char temps[256];
358 
359   if ((fp = fopen(fname, "r")) == NULL)
360   {
361     string_append(temps, "can't open file", fname, 0);
362     error(temps, non_fatal);
363     return;
364   }
365   r = get_int(fp);
366   c = get_int(fp);
367   o = get_int(fp);
368   tx = get_ledouble(fp);
369   ty = get_ledouble(fp);
370   s = get_ledouble(fp);
371   theta = get_ledouble(fp);
372   vsize = get_int(fp);
373 
374   if (model == NULL || model->c != c || model->o != o || 
375        model-> r != r || model->vsize != vsize)
376   {
377     if ((model = model_alloc(r, c, o, vsize)) == NULL)
378       return;
379     set_model(model);
380   }
381 
382   el = model->m->el.double_v;
383   p = model->profile->el.float_v;
384 
385   model->r = r;
386   model->c = c;
387   model->o = o;
388   model->tx = tx;
389   model->ty = ty;
390   model->s = s;
391   model->theta = theta;
392   model->vsize = vsize;
393 
394   for (i = 0; i < model->r; i++)
395     for (j = 0; j < model->c; j++)
396       el[i][j] = get_ledouble(fp);
397   for (i = 0; i < model->c; i++)
398     for (j = 0; j < 2 * model->vsize - 1; j++)
399       p[i][j] = get_float(fp);
400 
401   fclose(fp);
402 }
403 
404 
405 int   sroi_open_buildlist(char *fname)
406 {
407   char  temp[512];
408 
409   if ((fname == NULL) || (blfp = fopen(fname, "r")) == NULL)
410   {
411     sprintf(temp, "sroi_open_buildlist: build list %s not found", fname);
412     error(temp, warning);
413     return(0);
414   }
415 
416   bltotal = blcount = 0;
417   while (fscanf(blfp, "%s", temp) != EOF)
418     bltotal++;
419   rewind(blfp);
420   
421   return(bltotal);    
422 }
423 
424 
425 int  sroi_getnext_build(char *fname)
426 {
427   int  status;
428 
429   if (fname == NULL || blfp == NULL)
430     return(0);
431 
432   status = fscanf(blfp, "%s", fname);
433   if (status == 0 || status == EOF)
434   {
435     if (blcount != bltotal)
436       return(0);
437     else
438       return(-1);
439   }
440 
441   return(1); 
442 }
443 
444 
445 void  sroi_close_buildlist(void)
446 {
447   if (blfp == NULL)
448     return;
449  
450   fclose(blfp);
451   blfp = NULL;
452 
453   return;
454 }
455 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.