r156: *** empty log message ***
[ctsim.git] / libctsim / scanner.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          scanner.cpp
5 **   Purpose:       Classes for CT scanner
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: scanner.cpp,v 1.4 2000/07/20 11:17:31 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include "ct.h"
29
30 const char Scanner::GEOMETRY_PARALLEL_STR[] = "parallel";
31 const char Scanner::GEOMETRY_EQUILINEAR_STR[] = "equilinear";
32 const char Scanner::GEOMETRY_EQUIANGULAR_STR[] = "equiangular";
33
34 const char Scanner::GEOMETRY_PARALLEL_TITLE_STR[] = "Parallel";
35 const char Scanner::GEOMETRY_EQUILINEAR_TITLE_STR[] = "Equilinear";
36 const char Scanner::GEOMETRY_EQUIANGULAR_TITLE_STR[] = "Equiangular";
37
38 // NAME
39 //   DetectorArray       Construct a DetectorArray
40
41 DetectorArray::DetectorArray (const int nDet)
42 {
43   m_nDet = nDet;
44   m_detValues = new DetectorValue [m_nDet];
45 }
46
47
48 // NAME
49 //   ~DetectorArray             Free memory allocated to a detector array
50
51 DetectorArray::~DetectorArray (void)
52 {
53   delete [] m_detValues;
54 }
55
56
57
58 /* NAME
59  *   Scanner::Scanner           Construct a user specified detector structure
60  *
61  * SYNOPSIS
62  *   Scanner (phm, nDet, nView, nSample)
63  *   Phantom& phm               PHANTOM that we are making detector for
64  *   int geomety                Geometry of detector
65  *   int nDet                   Number of detector along detector array
66  *   int nView                  Number of rotated views
67  *   int nSample                Number of rays per detector
68  */
69
70 Scanner::Scanner (const Phantom& phm, const char* const geometryName, int nDet, int nView, int nSample, const double rot_anglen)
71 {
72   m_phmLen = phm.maxAxisLength();      // maximal length along an axis
73
74   m_fail = false;
75   m_idGeometry = convertGeometryNameToID (geometryName);
76   if (m_idGeometry == GEOMETRY_INVALID) {
77     m_fail = true;
78     m_failMessage = "Invalid geometry name ";
79     m_failMessage += geometryName;
80     return;
81   }
82
83   if (nView < 1)
84     nView = 1;
85   if (nSample < 1)
86     m_nSample = 1;
87   if (nDet < 1)
88     nDet = 1;
89   if ((nDet % 2) == 0)
90     ++nDet;             // ensure odd number of detectors
91
92   m_nDet     = nDet;
93   m_nView    = nView;
94   m_nSample  = nSample;
95   m_detLen   = SQRT2 * m_phmLen * ((m_nDet + N_EXTRA_DETECTORS) / static_cast<double>(m_nDet));
96
97   m_rotLen   =  rot_anglen;
98
99   m_radius  = m_detLen / 2;
100   m_detInc  = m_detLen / m_nDet;
101   m_rotInc  = m_rotLen / m_nView;
102
103   m_initPos.xd1 = m_detLen / 2;
104   m_initPos.yd1 = -m_detLen / 2;
105   m_initPos.xd2 = m_detLen / 2;
106   m_initPos.yd2 = m_detLen / 2;
107   m_initPos.xs1 = -m_detLen / 2;
108   m_initPos.ys1 = -m_detLen / 2;
109   m_initPos.xs2 = -m_detLen / 2;
110   m_initPos.ys2 = m_detLen / 2;
111   m_initPos.angle = 0.0;
112 }
113
114 Scanner::~Scanner (void)
115 {
116 }
117
118
119 Scanner::GeometryID 
120 Scanner::convertGeometryNameToID (const char* const geometryName)
121 {
122   GeometryID geometryID = GEOMETRY_INVALID;
123
124   if (strcasecmp (geometryName, GEOMETRY_PARALLEL_STR) == 0)
125     geometryID = GEOMETRY_PARALLEL;
126   else if (strcasecmp (geometryName, GEOMETRY_EQUILINEAR_STR) == 0)
127     geometryID = GEOMETRY_EQUILINEAR;
128   else if (strcasecmp (geometryName, GEOMETRY_EQUIANGULAR_STR) == 0)
129     geometryID = GEOMETRY_EQUIANGULAR;
130
131   return (geometryID);
132 }
133
134
135
136 /* NAME
137  *   raysum_collect                             Calculate ray sums for a Phantom
138  *
139  * SYNOPSIS
140  *   rs = raysum_collect (det, phm, start_view, trace, unit_pulse)
141  *   Scanner& det                       Scanner specifications**
142  *   RAYSUM *rs                         Calculated ray sum matrix
143  *   Phantom& phm                       Phantom we are taking ray sums of
144  *   int trace                          Boolean flag to signal ray sum tracing
145 */
146
147 void
148 Scanner::collectProjections (Projections& proj, const Phantom& phm, const int start_view, const int trace)
149 {
150   GRFMTX_2D rotmtx_initial, temp;
151   GRFMTX_2D rotmtx_incr;
152
153   double start_angle = start_view * proj.rotInc();
154   double xcent = phm.xmin() + (phm.xmax() - phm.xmin()) / 2;
155   double ycent = phm.ymin() + (phm.ymax() - phm.ymin()) / 2;
156
157   double xd1 = xcent + m_initPos.xd1;
158   double yd1 = ycent + m_initPos.yd1;
159   double xd2 = xcent + m_initPos.xd2;
160   double yd2 = ycent + m_initPos.yd2;
161   double xs1 = xcent + m_initPos.xs1;
162   double ys1 = ycent + m_initPos.ys1;
163   double xs2 = xcent + m_initPos.xs2;
164   double ys2 = ycent + m_initPos.ys2;
165  
166   m_trace = trace;
167
168 #ifdef HAVE_SGP 
169   SGP_ID gid;
170   if (m_trace >= TRACE_PHM) {
171     double wsize = 1.42 * m_phmLen / 2; /* sqrt(2) * radius */
172       
173     gid = sgp2_init (512, 512, "RayCollect");
174     sgp2_color (C_LTBLUE);
175     sgp2_window (xcent - wsize, ycent - wsize, xcent + wsize, ycent + wsize);
176     sgp2_color (C_BROWN);
177 #if RADIUS
178     sgp2_draw_circle (m_phmLen / 2);
179 #else
180     sgp2_draw_rect (xcent - m_phmLen / 2, ycent - m_phmLen / 2,
181                     xcent + m_phmLen / 2, ycent + m_phmLen / 2);
182 #endif
183     sgp2_color (C_BROWN);
184     sgp2_move_abs (0., 0.);
185     sgp2_draw_circle (wsize);
186     //      raysum_trace_menu_column = (crt->xsize * crt->asp) / 8 + 3;
187     traceShowParam ("X-Ray Simulator", "%s", RAYSUM_TRACE_ROW_TITLE, 8+C_LTWHITE, " ");
188     traceShowParam ("---------------", "%s", RAYSUM_TRACE_ROW_TITLE2, 8+C_LTWHITE, " ");
189     traceShowParam ("Phantom:",       "%s", RAYSUM_TRACE_ROW_PHANT_ID, C_YELLOW, " Herman");
190     traceShowParam ("Chomaticity  :", "%s", RAYSUM_TRACE_ROW_CHROMATIC, C_LTGREEN, "Mono");
191     traceShowParam ("Scatter      :", "%5.1f", RAYSUM_TRACE_ROW_SCATTER, C_LTGREEN, 0.);
192     traceShowParam ("Photon Uncert:", "%5.1f", RAYSUM_TRACE_ROW_PHOT_STAT, C_LTGREEN, 0.);
193     traceShowParam ("Num Scanners:", "%5d", RAYSUM_TRACE_ROW_NDET, C_LTRED, proj.nDet());
194     traceShowParam ("Num Views    :", "%5d", RAYSUM_TRACE_ROW_NVIEW, C_LTRED, proj.nView());
195     traceShowParam ("Samples / Ray:", "%5d", RAYSUM_TRACE_ROW_SAMPLES, C_LTRED, m_nSample);
196     
197     sgp2_color (C_LTGREEN);
198     phm.draw();
199     
200     initmarker (BDIAMOND, 129);
201   }
202 #endif
203
204 /* Calculate initial rotation matrix */
205   xlat_mtx2 (rotmtx_initial, -xcent, -ycent);
206   rot_mtx2 (temp, start_angle);
207   mult_mtx2 (rotmtx_initial, temp, rotmtx_initial);
208   xlat_mtx2 (temp, xcent, ycent);
209   mult_mtx2 (rotmtx_initial, temp, rotmtx_initial);
210
211   xform_mtx2 (rotmtx_initial, xd1, yd1);        /* rotate detector endpoints */
212   xform_mtx2 (rotmtx_initial, xd2, yd2);      /* to initial view_angle */
213   xform_mtx2 (rotmtx_initial, xs1, ys1);
214   xform_mtx2 (rotmtx_initial, xs2, ys2);
215
216 /* Calculate incrementatal rotation matrix */
217   xlat_mtx2 (rotmtx_incr, -xcent, -ycent);
218   rot_mtx2 (temp, proj.rotInc());
219   mult_mtx2 (rotmtx_incr, temp, rotmtx_incr);
220   xlat_mtx2 (temp, xcent, ycent);
221   mult_mtx2 (rotmtx_incr, temp, rotmtx_incr);
222   
223   int iview;
224   double viewAngle;
225   for (iview = 0, viewAngle = start_angle;  iview < proj.nView(); iview++, viewAngle += proj.rotInc()) {
226       DetectorArray& detArray = proj.getDetectorArray( iview );
227
228 #ifdef HAVE_SGP
229     if (m_trace >= TRACE_PHM) {
230       sgp2_move_abs (xd1, yd1);
231       sgp2_line_abs (xd2, yd2);
232       sgp2_move_abs (xs1, ys1);
233       sgp2_line_abs (xs2, ys2);
234     }
235 #endif
236     if (m_trace)
237       traceShowParam ("Current View :", "%5d", RAYSUM_TRACE_ROW_CURR_VIEW, C_LTMAGENTA, iview);
238             
239     projectSingleView (phm, detArray, xd1, yd1, xd2, yd2, xs1, ys1, xs2, ys2);
240     detArray.setViewAngle (viewAngle);
241       
242 #ifdef HAVE_SGP
243     if (m_trace >= TRACE_PHM) {
244       //        rs_plot (detArray, xd1, yd1, xcent, ycent, theta);
245       sgp2_move_abs (xd1, yd1);
246       sgp2_line_abs (xd2, yd2);
247       sgp2_move_abs (xs1, ys1);
248       sgp2_line_abs (xs2, ys2);
249     }
250 #endif
251     xform_mtx2 (rotmtx_incr, xd1, yd1);  // rotate detector endpoints 
252     xform_mtx2 (rotmtx_incr, xd2, yd2);
253     xform_mtx2 (rotmtx_incr, xs1, ys1);
254     xform_mtx2 (rotmtx_incr, xs2, ys2);
255   } /* for each iview */
256 }
257
258
259 /* NAME
260  *    rayview                   Calculate raysums for a view at any angle
261  *
262  * SYNOPSIS
263  *    rayview (phm, detArray, xd1, nSample, yd1, xd2, yd2, xs1, ys1, xs2, ys2)
264  *    Phantom& phm              Phantom to scan
265  *    DETARRAY *detArray                Storage of values for detector array
266  *    Scanner& det              Scanner parameters
267  *    double xd1, yd1, xd2, yd2 Beginning & ending detector positions
268  *    double xs1, ys1, xs2, ys2 Beginning & ending source positions
269  *
270  * RAY POSITIONING
271  *         For each detector, have there are a variable number of rays traced.
272  *     The source of each ray is the center of the source x-ray cell. The
273  *     detector positions are equally spaced within the cell
274  *
275  *         The increments between rays are calculated so that the cells start
276  *     at the beginning of a detector cell and they end on the endpoint
277  *     of the cell.  Thus, the last cell starts at (xd2-ddx),(yd2-ddy).
278  *         The exception to this is if there is only one ray per detector.
279  *     In that case, the detector position is the center of the detector cell.
280  */
281
282 void 
283 Scanner::projectSingleView (const Phantom& phm, DetectorArray& detArray, const double xd1, const double yd1, const double xd2, const double yd2, const double xs1, const double ys1, const double xs2, const double ys2)
284 {
285   double ddx = (xd2 - xd1) / detArray.nDet();  // change in coords between detectors
286   double ddy = (yd2 - yd1) / detArray.nDet();
287   double sdx = (xs2 - xs1) / detArray.nDet();  // change in coords between source
288   double sdy = (ys2 - ys1) / detArray.nDet();
289
290   double ddx2 = ddx / m_nSample;        // Incr. between rays with detector cell
291   double ddy2 = ddy / m_nSample;        // Doesn't include detector endpoints 
292   double ddx2_ofs = ddx2 / 2;           // offset of 1st ray from start of detector cell
293   double ddy2_ofs = ddy2 / 2;
294   
295   double xd_maj = xd1 + ddx2_ofs;       // Incr. between detector cells
296   double yd_maj = yd1 + ddy2_ofs;
297   double xs_maj = xs1 + (sdx / 2);      // put ray source in center of cell 
298   double ys_maj = ys1 + (sdy / 2);
299
300   DetectorValue* detval = detArray.detValues();
301
302   if (phm.getComposition() == P_UNIT_PULSE) {  // put unit pulse in center of view
303     for (int d = 0; d < detArray.nDet(); d++)
304       if (detArray.nDet() / 2 == d && (d % 2) == 1)
305         detval[d] = 1;
306       else
307         detval[d] = 0;
308   } else {
309     for (int d = 0; d < detArray.nDet(); d++) {
310       double xd = xd_maj;
311       double yd = yd_maj;
312       double xs = xs_maj;
313       double ys = ys_maj;
314       double sum = 0.0;
315       for (unsigned int i = 0; i < m_nSample; i++) {
316 #ifdef HAVE_SGP
317         if (m_trace >= TRACE_RAYS) {
318           sgp2_move_abs (xs, ys);
319           sgp2_line_abs (xd, yd);
320         }
321 #endif
322         sum += projectSingleLine (phm, xd, yd, xs, ys);
323               
324         if (m_trace >= TRACE_RAYS)
325           traceShowParam ("Attenuation  :", "%5.2f", RAYSUM_TRACE_ROW_ATTEN, C_LTMAGENTA, "sum");
326
327 #ifdef HAVE_SGP
328         if (m_trace >= TRACE_RAYS) {
329           sgp2_move_abs (xs, ys);
330           sgp2_line_abs (xd, yd);
331         }
332 #endif
333         xd += ddx2;
334         yd += ddy2;
335       }
336
337       detval[d] = sum / m_nSample;
338       xd_maj += ddx;
339       yd_maj += ddy;
340       xs_maj += sdx;
341       ys_maj += sdy;
342     } /* for each detector */
343   } /* if not unit pulse */
344 }
345
346
347 void 
348 Scanner::traceShowParam (const char *label, const char *fmt, int row, int color, ...)
349 {  
350   char s[80];
351   va_list arg;
352
353   va_start(arg, color);
354   //  cio_set_cpos (raysum_trace_menu_column, row);
355   snprintf (s, sizeof(s), label, "%s");
356   //  cio_set_text_clr (color - 8, 0);
357   cio_put_str (s);
358   vsnprintf (s, sizeof(s), fmt, arg);
359   //  cio_set_text_clr (color, 0);
360   cio_put_str (s);
361
362   va_end(arg);
363 }
364
365
366 /* NAME
367  *    projectSingleLine                 INTERNAL: Calculates raysum along a line for a Phantom
368  *
369  * SYNOPSIS
370  *    rsum = phm_ray_attenuation (phm, x1, y1, x2, y2)
371  *    double rsum               Ray sum of Phantom along given line
372  *    Phantom& phm;             Phantom from which to calculate raysum
373  *    double *x1, *y1, *x2, y2  Endpoints of ray path (in Phantom coords)
374  */
375
376 double 
377 Scanner::projectSingleLine (const Phantom& phm, const double x1, const double y1, const double x2, const double y2)
378 {
379   // check ray against each pelem in Phantom 
380   double rsum = 0.0;
381   for (PElemConstIterator i = phm.listPElem().begin(); i != phm.listPElem().end(); i++)
382     rsum += projectLineAgainstPElem (**i, x1, y1, x2, y2);
383
384   return (rsum);
385 }
386
387
388 /* NAME
389  *   pelem_ray_attenuation              Calculate raysum of an pelem along one line
390  *
391  * SYNOPSIS
392  *   rsum = pelem_ray_attenuation (pelem, x1, y1, x2, y2)
393  *   double rsum                Computed raysum
394  *   PhantomElement& pelem              Pelem to scan
395  *   double x1, y1, x2, y2      Endpoints of raysum line
396  */
397
398 double 
399 Scanner::projectLineAgainstPElem (const PhantomElement& pelem, double x1, double y1, double x2, double y2)
400 {
401   if (! pelem.clipLineWorldCoords (x1, y1, x2, y2)) {
402     if (m_trace == TRACE_CLIPPING)
403       cio_tone (1000., 0.05);
404     return (0.0);
405   }
406
407 #ifdef HAVE_SGP
408   if (m_trace == TRACE_CLIPPING) {
409     sgp2_move_abs (x1, y1);
410     sgp2_line_abs (x2, y2);
411     cio_tone (8000., 0.05);
412     sgp2_move_abs (x1, y1);
413     sgp2_line_abs (x2, y2);
414   }
415 #endif
416
417   double len = lineLength (x1, y1, x2, y2);
418   return (len * pelem.atten());
419 }
420