source: trunk/MagicSoft/Simulation/Detector/ReflectorII/geometry.c@ 1908

Last change on this file since 1908 was 1908, checked in by moralejo, 21 years ago
*** empty log message ***
File size: 6.5 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <math.h>
5#include "diag.h"
6#include "geometry.h"
7#include "init.h"
8
9extern char line[]; /* parsing buf. (init) */
10extern char axisdev_filename[256], reflectivity_filename[256];
11
12float ct_Focal_mean; /* focal dist. (mean) (cm) */
13float ct_PSpread_mean; /* pt. spread fn. (mean) (cm) */
14float ct_BlackSpot_rad; /* black spot radius (cm) */
15float ct_RMirror; /* rad. of single mirror (cm) */
16int ct_NMirrors=0; /* number of mirrors */
17float ct_CameraWidth; /* camera width (cm) */
18int ct_NPixels; /* number of pixels */
19float ct_PixelWidth; /* pixel width (cm) */
20float ct_max_radius; /* Maximum value of curvilinear
21 * coordinates of the center of
22 * the mirrors.
23 */
24
25mirror *ct_data=NULL; /* ptr to mirror data */
26
27int nReflectivity=0; /* elements in refl. table */
28float *Reflectivity[2]; /* reflectivity table */
29float *AxisDeviation[2]; /* axis deviation table */
30
31/* Prototypes */
32static void ReadMirrorTable(FILE *geofile);
33static void ReadReflectivity(char *datname);
34static void ReadAxisDev(char *datname);
35
36static void ReadMirrorTable(FILE *geofile)
37{ int i; /* Mirror index */
38
39 ct_max_radius = 0.;
40
41 if ((ct_data=(mirror *)malloc(sizeof(mirror)*ct_NMirrors)) == NULL)
42 FatalError(MIRR_ALLOC_FTL, ct_NMirrors);
43 Log(MIRR_ALLOC_LOG, ct_NMirrors);
44 Log(MIRR_TABLE_LOG);
45
46 Log(READ_ASCII_LOG);
47 for (i=0; i<ct_NMirrors; i++)
48 {
49 if (12 != fscanf(geofile, "%d %f %f %f %f %f %f %f %f %f %f %f",
50 &ct_data[i].i, &ct_data[i].f,
51 &ct_data[i].sx, &ct_data[i].sy,
52 &ct_data[i].x, &ct_data[i].y, &ct_data[i].z,
53 &ct_data[i].theta, &ct_data[i].phi,
54 &ct_data[i].xn, &ct_data[i].yn, &ct_data[i].zn))
55 break;
56
57 ct_max_radius = (ct_data[i].sx > ct_max_radius? ct_data[i].sx :
58 ct_max_radius);
59 ct_max_radius = (ct_data[i].sy > ct_max_radius? ct_data[i].sy :
60 ct_max_radius);
61 }
62
63 if (i < ct_NMirrors)
64 FatalError(MIRR_FEW___FTL, i);
65
66} /* end of ReadMirrorTable */
67
68static void ReadReflectivity(char *datname)
69{
70 FILE *datfile = fopen(datname, "r");
71 int current = 0;
72
73 if (datfile == NULL)
74 FatalError(RFLF_ERROR_FTL, datname);
75 else
76 printf("Reading file %s\n", datname);
77
78 while (fgets(line, LINE_MAX_LENGTH, datfile))
79 {
80 if (line[0] == '#') continue;
81
82 if (nReflectivity == 0)
83 {
84 nReflectivity = atoi(line);
85 if (nReflectivity)
86 {
87 if ((Reflectivity[0] =
88 (float *) malloc(sizeof(float) * nReflectivity)) == NULL ||
89 (Reflectivity[1] =
90 (float *) malloc(sizeof(float) * nReflectivity)) == NULL)
91 FatalError(REFL_ALLOC_FTL, nReflectivity);
92 }
93 }
94 else if (2 == sscanf(line, "%f %f", &Reflectivity[0][current],
95 &Reflectivity[1][current]))
96 {
97 current++;
98 if (current >= nReflectivity) break;
99 }
100 }
101 fclose(datfile);
102
103 nReflectivity = current;
104
105} /* end of ReadReflectivity */
106
107static void ReadAxisDev(char *datname)
108{ FILE *datfile = fopen(datname, "r");
109 int current = 0;
110
111 if (datfile == NULL)
112 FatalError(AXIS_ERROR_FTL, datname);
113 else
114 printf("Reading file %s\n", axisdev_filename);
115
116 if ((AxisDeviation[0]=
117 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL
118 || (AxisDeviation[1]=
119 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
120 FatalError(AXIS_ALLOC_FTL, ct_NMirrors);
121 while (fgets(line, LINE_MAX_LENGTH, datfile))
122 { if (line[0] == '#') continue;
123 if (2==sscanf(line, "%f %f",
124 &AxisDeviation[0][current], &AxisDeviation[1][current]));
125 { current++;
126 if (current >= ct_NMirrors) break; }}
127 fclose(datfile);
128
129 if (current != ct_NMirrors)
130 FatalError(AXIS_FEW___FTL, current, ct_NMirrors);
131} /* end of ReadAxisDev */
132
133void GeometrySwitch(FILE *geofile)
134{ char *value_ptr = NULL; /* ptr at parm value */
135 int switch_end = FALSE; /* bool to exit loop */
136 extern char whites[]; /* white chars (init) */
137 extern int ParseLine(FILE *parfile, /* FILE with parms */
138 const char *token_list[], /* array w/tokens */
139 int tokens, /* nr of tokens */
140 char **value_ptr); /* ptr->parm val. */
141
142 /* Initialise arrays */
143 Reflectivity[0] = AxisDeviation[0] = NULL;
144
145 do
146 { switch(ParseLine(geofile, ctparms, ARRAY_SZ(ctparms), &value_ptr))
147 { case type:
148 if (1 != atoi(value_ptr))
149 FatalError(TYPE_ERROR_FTL);
150 break;
151 case focal_distance:
152 Log(LOG__FLOAT_LOG, "focal distance (average, cm)",
153 ct_Focal_mean = (float) atof(value_ptr));
154 break;
155 case focal_std: /* not implemented. */
156 break;
157 case point_spread:
158 Log(LOG__FLOAT_LOG, "point spread fn. sigma (average, cm)",
159 ct_PSpread_mean = (float) atof(value_ptr));
160 break;
161 case point_std: /* not implemented */
162 break;
163 case adjustment_dev: /* not implemented */
164 break;
165 case black_spot:
166 Log(LOG__FLOAT_LOG, "radius of black spot (cm)",
167 ct_BlackSpot_rad = (float) atof(value_ptr));
168 break;
169 case n_mirrors:
170 Log(LOG__INT___LOG, "number of mirrors",
171 ct_NMirrors = atoi(value_ptr));
172 break;
173 case r_mirror:
174 Log(LOG__FLOAT_LOG, "single mirror radius (cm)",
175 ct_RMirror = (float) atof(value_ptr));
176 break;
177 case camera_width:
178 Log(LOG__FLOAT_LOG, "camera radius (cm)",
179 ct_CameraWidth = (float) atof(value_ptr));
180 break;
181 case n_pixels:
182 Log(LOG__INT___LOG, "number of pixels",
183 ct_NPixels = atoi(value_ptr));
184 break;
185 case pixel_width:
186 Log(LOG__FLOAT_LOG, "pixel width (cm)",
187 ct_PixelWidth = (float) atof(value_ptr));
188 break;
189 case n_centralpixels:
190 /* this parameter is for camera, not for reflector */
191 break;
192 case n_gappixels:
193 /* this parameter is for camera, not for reflector */
194 break;
195 case refl_file:
196 ReadReflectivity(value_ptr);
197 break;
198 case axisdev_file:
199 ReadAxisDev(value_ptr);
200 break;
201 case define_mirrors:
202 if (ct_NMirrors) ReadMirrorTable(geofile);
203 else FatalError(MIRR_NSPEC_FTL);
204 switch_end = TRUE;
205 break;
206 default: switch_end = TRUE;
207 break; }}
208 while (!switch_end);
209 fclose(geofile);
210
211 ct_max_radius += ct_RMirror;
212
213 if (strlen(reflectivity_filename) == 0)
214 strcpy(reflectivity_filename, REFLECTIVITY_FILE);
215 if (Reflectivity[0] == NULL) ReadReflectivity(reflectivity_filename);
216
217 if (strlen(axisdev_filename) == 0)
218 strcpy(axisdev_filename, AXISDEVIATION_FILE);
219 if (AxisDeviation[0]== NULL) ReadAxisDev(axisdev_filename);
220
221
222} /* end of GeometrySwitch */
223
Note: See TracBrowser for help on using the repository browser.