Initial snark14m import
[snark14.git] / src / snark / pnchfile.cpp
1 /*
2  ***********************************************************
3  $SNARK_Header: S N A R K  1 4 - A PICTURE RECONSTRUCTION PROGRAM $
4  $HeadURL: svn://dig.cs.gc.cuny.edu/snark/trunk/src/snark/pnchfile.cpp $
5  $LastChangedRevision: 80 $
6  $Date: 2014-07-01 21:01:54 -0400 (Tue, 01 Jul 2014) $
7  $Author: agulati $
8  ***********************************************************
9
10  */
11
12 #include <cstdlib>
13 #include <cstdio>
14
15 #include "blkdta.h"
16 #include "creacm.h"
17 #include "geom.h"
18 #include "spctrm.h"
19 #include "noise.h"
20 #include "objects.h"
21 #include "recfile.h"
22 #include "pnchfile.h"
23
24 PunchFile_class PunchFile;
25
26 PunchFile_class::PunchFile_class()
27 {
28         PnchFil = NULL;
29 }
30
31 int PunchFile_class::Open(BOOLEAN append)
32 {
33         const char* mode = (append) ? "a+" : "w";
34         if ((PnchFil = fopen("punch", mode)) == NULL)
35         {
36                 return -1;
37         }
38
39         return 0;
40 }
41
42 void PunchFile_class::Close()
43 {
44         if (PnchFil != NULL)
45         {
46                 fclose(PnchFil);
47                 PnchFil = NULL;
48         }
49 }
50
51 void PunchFile_class::WriteRecHeader(CHAR* recnam, CHAR* algn, INTEGER count,
52                 CHAR* prjnam)
53 {
54         fprintf(PnchFil, "%30s alg %4s iter %4i %30s\n", recnam, algn, count,
55                         prjnam);
56         WriteHeader(false);
57 }
58
59 void PunchFile_class::WritePhantomHeader(CHAR* phnnam)
60 {
61         fprintf(PnchFil, "%s\n", phnnam);
62
63         WriteHeader(true);
64 }
65
66 void PunchFile_class::WriteData(REAL* recon, INTEGER nelem)
67 {
68         REAL* jlo = recon;
69
70         for (int n = 0; n < nelem; n++)
71         {
72                 for (int j = 0; j < nelem; j++)
73                 {
74                         fprintf(PnchFil, " %10.4f", *jlo);
75                         if ((j % 7) == 6)
76                                 fprintf(PnchFil, "\n");
77
78                         jlo++;
79                 }
80                 if (nelem % 7 != 0)
81                         fprintf(PnchFil, "\n");
82         }
83 }
84
85 void PunchFile_class::WriteHeader(BOOLEAN Phantom)
86 {
87
88         static const CHAR keywdStr[40][5] =
89         { "last", "spec", "mono", "poly", "obje", "phan", "aver", "rays", "geom",
90                         "para", "dive", "stri", "line", "tang", "arc ", "unif", "vari",
91                         "angl", "meas", "perf", "nois", "quan", "scat", "mult", "addi",
92                         "seed", "back", "run ", "modi", "save", "pixe", "equa", "user",
93                         "and ", "afte", "step", "spac", "pnch", "    ", "    " };
94
95         static const CHAR kindStr[5][5] =
96         { "elip", "rect", "tria", "segm", "sect" };
97
98         static const INTEGER KWI_last = 0;
99         static const INTEGER KWI_spec = 1;
100         static const INTEGER KWI_mono = 2;
101         static const INTEGER KWI_poly = 3;
102         static const INTEGER KWI_obj = 4;
103         static const INTEGER KWI_pixel = 30;
104
105         // DATA SPACE FOR OBJECTS IN PHANTOM AT TOP OF BLANK COMMON
106
107         BOOLEAN perfct;
108         BOOLEAN noisfl;
109         BOOLEAN quanfl;
110
111         static const REAL one = 1.0;
112
113         int i, j;
114
115         INTEGER itype;
116
117         quanfl = NoisePar.quanin > 0;
118         noisfl = quanfl || NoisePar.ultnfl || NoisePar.addnfl;
119         perfct = !(NoisePar.sctnfl || noisfl);
120
121         // ALSO SET VALUES PROPERLY FOR UNAVAILABLE QUANTITIES
122         Creacm.nobj = 0;
123         Spctrm.nergy = 1;
124         Spctrm.energy[0] = -1;
125
126         // WRITE HEADER INFORMATION ON FILE11 FOR PHANTOM & RAYSUMS
127         // PUNCH OUT THE HEADER INFORMATION FOR THE PHANTOM AND PICTURES
128
129         // SPECTRUM
130         if (Spctrm.nergy == 1)
131         {
132                 fprintf(PnchFil, "%s    %s %4i\n", keywdStr[KWI_spec],
133                                 keywdStr[KWI_mono], Spctrm.energy[0]);
134         }
135
136         if (Spctrm.nergy > 1)
137         {
138
139                 fprintf(PnchFil, "%s    %4s %4i\n", keywdStr[KWI_spec],
140                                 keywdStr[KWI_poly], Spctrm.nergy);
141
142                 for (i = 0; i < Spctrm.nergy; i++)
143                 {
144                         fprintf(PnchFil, "%5i %5i ", Spctrm.energy[i], Creacm.percnt[i]);
145                 }
146                 fprintf(PnchFil, "\n");
147         }
148
149         // OBJECTS
150
151         fprintf(PnchFil, "%s\n", keywdStr[KWI_obj]);
152
153         if (Creacm.nobj != 0)
154         {
155
156                 for (i = 0; i < Creacm.nobj; i++)
157                 {
158                         itype = (int) objects[i].type;
159
160                         fprintf(PnchFil, "%4s  %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n",
161                                         kindStr[itype], objects[i].cx, objects[i].cy, objects[i].u,
162                                         objects[i].v, objects[i].ang, objects[i].den1[0]);
163
164                         if (Spctrm.nergy > 1)
165                         {
166                                 fprintf(PnchFil, "%4i ", i + 1);
167                                 for (j = 1; j < Spctrm.nergy; j++)
168                                 {
169                                         fprintf(PnchFil, " %9.4f", objects[i].den1[j]);
170                                 }
171                                 fprintf(PnchFil, "\n");
172                         }
173                 }
174         }
175
176         fprintf(PnchFil, "%s %9.4f\n", keywdStr[KWI_last], one);
177
178         // SKIP PHANTOM AVERAGING AND OTHER INFO SUCH AS NELEM FOR RDPROJ
179
180         fprintf(PnchFil, "phan aver 1\n"); // bug 121 - swr - 6/30/05
181         int nelem;               // bug 166 - swr - 9/24/05
182         RecFile.GetNelem(&nelem);
183         REAL pixsiz;
184         RecFile.GetPixSize(&pixsiz);
185         fprintf(PnchFil, "%s    %i    size    %10.4f\n", keywdStr[KWI_pixel], nelem,
186                         pixsiz);
187
188         // IF CALLED FROM PUNCH WE ARE DONE
189         return;
190 }
191