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

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