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

Last change on this file since 5074 was 2046, checked in by moralejo, 22 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 =
58 (fabs(ct_data[i].sx) > ct_max_radius? fabs(ct_data[i].sx) :
59 ct_max_radius);
60 ct_max_radius =
61 (fabs(ct_data[i].sy) > ct_max_radius? fabs(ct_data[i].sy) :
62 ct_max_radius);
63 }
64
65 if (i < ct_NMirrors)
66 FatalError(MIRR_FEW___FTL, i);
67
68} /* end of ReadMirrorTable */
69
70static void ReadReflectivity(char *datname)
71{
72 FILE *datfile = fopen(datname, "r");
73 int current = 0;
74
75 if (datfile == NULL)
76 FatalError(RFLF_ERROR_FTL, datname);
77 else
78 printf("Reading file %s\n", datname);
79
80 while (fgets(line, LINE_MAX_LENGTH, datfile))
81 {
82 if (line[0] == '#') continue;
83
84 if (nReflectivity == 0)
85 {
86 nReflectivity = atoi(line);
87 if (nReflectivity)
88 {
89 if ((Reflectivity[0] =
90 (float *) malloc(sizeof(float) * nReflectivity)) == NULL ||
91 (Reflectivity[1] =
92 (float *) malloc(sizeof(float) * nReflectivity)) == NULL)
93 FatalError(REFL_ALLOC_FTL, nReflectivity);
94 }
95 }
96 else if (2 == sscanf(line, "%f %f", &Reflectivity[0][current],
97 &Reflectivity[1][current]))
98 {
99 current++;
100 if (current >= nReflectivity) break;
101 }
102 }
103 fclose(datfile);
104
105 nReflectivity = current;
106
107} /* end of ReadReflectivity */
108
109static void ReadAxisDev(char *datname)
110{ FILE *datfile = fopen(datname, "r");
111 int current = 0;
112
113 if (datfile == NULL)
114 FatalError(AXIS_ERROR_FTL, datname);
115 else
116 printf("Reading file %s\n", axisdev_filename);
117
118 if ((AxisDeviation[0]=
119 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL
120 || (AxisDeviation[1]=
121 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
122 FatalError(AXIS_ALLOC_FTL, ct_NMirrors);
123 while (fgets(line, LINE_MAX_LENGTH, datfile))
124 { if (line[0] == '#') continue;
125 if (2==sscanf(line, "%f %f",
126 &AxisDeviation[0][current], &AxisDeviation[1][current]));
127 { current++;
128 if (current >= ct_NMirrors) break; }}
129 fclose(datfile);
130
131 if (current != ct_NMirrors)
132 FatalError(AXIS_FEW___FTL, current, ct_NMirrors);
133} /* end of ReadAxisDev */
134
135void GeometrySwitch(FILE *geofile)
136{ char *value_ptr = NULL; /* ptr at parm value */
137 int switch_end = FALSE; /* bool to exit loop */
138 extern char whites[]; /* white chars (init) */
139 extern int ParseLine(FILE *parfile, /* FILE with parms */
140 const char *token_list[], /* array w/tokens */
141 int tokens, /* nr of tokens */
142 char **value_ptr); /* ptr->parm val. */
143
144 /* Initialise arrays */
145 Reflectivity[0] = AxisDeviation[0] = NULL;
146
147 do
148 { switch(ParseLine(geofile, ctparms, ARRAY_SZ(ctparms), &value_ptr))
149 { case type:
150 if (1 != atoi(value_ptr))
151 FatalError(TYPE_ERROR_FTL);
152 break;
153 case focal_distance:
154 Log(LOG__FLOAT_LOG, "focal distance (average, cm)",
155 ct_Focal_mean = (float) atof(value_ptr));
156 break;
157 case focal_std: /* not implemented. */
158 break;
159 case point_spread:
160 Log(LOG__FLOAT_LOG, "point spread fn. sigma (average, cm)",
161 ct_PSpread_mean = (float) atof(value_ptr));
162 break;
163 case point_std: /* not implemented */
164 break;
165 case adjustment_dev: /* not implemented */
166 break;
167 case black_spot:
168 Log(LOG__FLOAT_LOG, "radius of black spot (cm)",
169 ct_BlackSpot_rad = (float) atof(value_ptr));
170 break;
171 case n_mirrors:
172 Log(LOG__INT___LOG, "number of mirrors",
173 ct_NMirrors = atoi(value_ptr));
174 break;
175 case r_mirror:
176 Log(LOG__FLOAT_LOG, "single mirror radius (cm)",
177 ct_RMirror = (float) atof(value_ptr));
178 break;
179 case camera_width:
180 Log(LOG__FLOAT_LOG, "camera radius (cm)",
181 ct_CameraWidth = (float) atof(value_ptr));
182 break;
183 case n_pixels:
184 Log(LOG__INT___LOG, "number of pixels",
185 ct_NPixels = atoi(value_ptr));
186 break;
187 case pixel_width:
188 Log(LOG__FLOAT_LOG, "pixel width (cm)",
189 ct_PixelWidth = (float) atof(value_ptr));
190 break;
191 case n_centralpixels:
192 /* this parameter is for camera, not for reflector */
193 break;
194 case n_gappixels:
195 /* this parameter is for camera, not for reflector */
196 break;
197 case refl_file:
198 ReadReflectivity(value_ptr);
199 break;
200 case axisdev_file:
201 ReadAxisDev(value_ptr);
202 break;
203 case define_mirrors:
204 if (ct_NMirrors) ReadMirrorTable(geofile);
205 else FatalError(MIRR_NSPEC_FTL);
206 switch_end = TRUE;
207 break;
208 default: switch_end = TRUE;
209 break; }}
210 while (!switch_end);
211 fclose(geofile);
212
213 ct_max_radius += ct_RMirror;
214
215 if (strlen(reflectivity_filename) == 0)
216 strcpy(reflectivity_filename, REFLECTIVITY_FILE);
217 if (Reflectivity[0] == NULL) ReadReflectivity(reflectivity_filename);
218
219 if (strlen(axisdev_filename) == 0)
220 strcpy(axisdev_filename, AXISDEVIATION_FILE);
221 if (AxisDeviation[0]== NULL) ReadAxisDev(axisdev_filename);
222
223
224} /* end of GeometrySwitch */
225
Note: See TracBrowser for help on using the repository browser.