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