r626: no 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-2001 Kevin Rosenberg
11 **
12 **  $Id: scanner.cpp,v 1.34 2001/03/10 23:14:16 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 const int Scanner::GEOMETRY_INVALID = -1;
32 const int Scanner::GEOMETRY_PARALLEL = 0;
33 const int Scanner::GEOMETRY_EQUIANGULAR = 1;
34 const int Scanner::GEOMETRY_EQUILINEAR = 2;
35
36 const char* Scanner::s_aszGeometryName[] = 
37 {
38   {"parallel"},
39   {"equiangular"},
40   {"equilinear"},
41 };
42
43 const char* Scanner::s_aszGeometryTitle[] = 
44 {
45   {"Parallel"},
46   {"Equiangular"},
47   {"Equilinear"},
48 };
49
50 const int Scanner::s_iGeometryCount = sizeof(s_aszGeometryName) / sizeof(const char*);
51
52
53 // NAME
54 //   DetectorArray       Construct a DetectorArray
55
56 DetectorArray::DetectorArray (const int nDet)
57 {
58   m_nDet = nDet;
59   m_detValues = new DetectorValue [m_nDet];
60 }
61
62
63 // NAME
64 //   ~DetectorArray             Free memory allocated to a detector array
65
66 DetectorArray::~DetectorArray (void)
67 {
68   delete [] m_detValues;
69 }
70
71
72
73 /* NAME
74 *   Scanner::Scanner            Construct a user specified detector structure
75 *
76 * SYNOPSIS
77 *   Scanner (phm, nDet, nView, nSample)
78 *   Phantom& phm                PHANTOM that we are making detector for
79 *   int geomety                Geometry of detector
80 *   int nDet                    Number of detector along detector array
81 *   int nView                   Number of rotated views
82 *   int nSample         Number of rays per detector
83 */
84
85 Scanner::Scanner (const Phantom& phm, const char* const geometryName, 
86                   int nDet, int nView, int nSample, const double rot_anglen, 
87                   const double dFocalLengthRatio, const double dCenterDetectorRatio,
88                   const double dViewRatio, const double dScanRatio)
89 {
90   m_fail = false;
91   m_idGeometry = convertGeometryNameToID (geometryName);
92   if (m_idGeometry == GEOMETRY_INVALID) {
93     m_fail = true;
94     m_failMessage = "Invalid geometry name ";
95     m_failMessage += geometryName;
96     return;
97   }
98   
99   if (nView < 1 || nDet < 1) {
100     m_fail = true;
101     m_failMessage = "nView & nDet must be greater than 0";
102     return;
103   }
104   if (nSample < 1)
105     m_nSample = 1;
106   
107   m_nDet     = nDet;
108   m_nView    = nView;
109   m_nSample  = nSample;
110   m_dFocalLengthRatio = dFocalLengthRatio;
111   m_dCenterDetectorRatio = dCenterDetectorRatio;
112   m_dViewRatio = dViewRatio;
113   m_dScanRatio = dScanRatio;
114
115   m_dViewDiameter = phm.getDiameterBoundaryCircle() * m_dViewRatio;
116   m_dFocalLength = (m_dViewDiameter / 2) * m_dFocalLengthRatio;
117   m_dCenterDetectorLength = (m_dViewDiameter / 2) * m_dCenterDetectorRatio;
118   m_dSourceDetectorLength = m_dFocalLength + m_dCenterDetectorLength;
119   m_dScanDiameter = m_dViewDiameter * m_dScanRatio;
120   
121   m_dXCenter = phm.xmin() + (phm.xmax() - phm.xmin()) / 2;
122   m_dYCenter = phm.ymin() + (phm.ymax() - phm.ymin()) / 2;
123   m_rotLen  = rot_anglen;
124   m_rotInc  = m_rotLen / m_nView;
125   if (m_idGeometry == GEOMETRY_PARALLEL) {
126     m_dFanBeamAngle = 0;
127     m_detLen   = m_dScanDiameter;
128     m_detInc  = m_detLen / m_nDet;
129     double dDetectorArrayEndOffset = 0;
130     // For even number of detectors, make detInc slightly larger so that center lies
131     // at nDet/2. Also, extend detector array by one detInc so that all of the phantom is scanned
132     if (m_nDet % 2 == 0) { // Adjust for Even number of detectors
133       m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
134       dDetectorArrayEndOffset = m_detInc;
135     }
136
137     double dHalfDetLen = m_detLen / 2;
138     m_initPos.xs1 = m_dXCenter - dHalfDetLen;
139     m_initPos.ys1 = m_dYCenter + m_dFocalLength;
140     m_initPos.xs2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
141     m_initPos.ys2 = m_dYCenter + m_dFocalLength;
142     m_initPos.xd1 = m_dXCenter - dHalfDetLen;
143     m_initPos.yd1 = m_dYCenter - m_dCenterDetectorLength;
144     m_initPos.xd2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
145     m_initPos.yd2 = m_dYCenter - m_dCenterDetectorLength;
146     m_initPos.angle = 0.0;
147   } else if (m_idGeometry == GEOMETRY_EQUILINEAR) {
148   if (m_dScanDiameter / 2 >= m_dFocalLength) {
149       m_fail = true;
150       m_failMessage = "Invalid geometry: Focal length must be larger than scan length";
151       return;
152     }
153
154     const double dAngle = asin ((m_dScanDiameter / 2) / m_dFocalLength);
155     const double dHalfDetLen = m_dSourceDetectorLength * tan (dAngle);
156     
157     m_detLen = dHalfDetLen * 2;
158     m_detInc  = m_detLen / m_nDet;
159     double dDetectorArrayEndOffset = 0;
160     if (m_nDet % 2 == 0) { // Adjust for Even number of detectors
161       m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
162       dDetectorArrayEndOffset = m_detInc;
163     }
164   
165     m_dFanBeamAngle = dAngle * 2;
166     m_initPos.angle = 0.0;
167     m_initPos.xs1 = m_dXCenter;
168     m_initPos.ys1 = m_dYCenter + m_dFocalLength;
169     m_initPos.xs2 = m_dXCenter;
170     m_initPos.ys2 = m_dYCenter + m_dFocalLength;
171     m_initPos.xd1 = m_dXCenter - dHalfDetLen;
172     m_initPos.yd1 = m_dYCenter - m_dCenterDetectorLength;
173     m_initPos.xd2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
174     m_initPos.yd2 = m_dYCenter - m_dCenterDetectorLength;
175     m_initPos.angle = 0.0;
176   } else if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
177     if (m_dScanDiameter / 2 > m_dFocalLength) {
178       m_fail = true;
179       m_failMessage = "Invalid geometry: Focal length must be larger than scan length";
180       return;
181     }
182     const double dAngle = asin ((m_dScanDiameter / 2) / m_dFocalLength);
183
184     m_detLen = 2 * dAngle;
185     m_detInc = m_detLen / m_nDet;
186     double dDetectorArrayEndOffset = 0;
187     if (m_nDet % 2 == 0) { // Adjust for Even number of detectors
188       m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
189       dDetectorArrayEndOffset = m_detInc;
190     }
191     // adjust for center-detector length
192     double dA1 = acos ((m_dScanDiameter / 2) / m_dCenterDetectorLength);
193     double dAngularScale = 2 * (HALFPI + dAngle - dA1) / m_detLen;
194
195     m_dAngularDetLen = dAngularScale * (m_detLen + dDetectorArrayEndOffset);
196     m_dAngularDetIncrement = dAngularScale * m_detInc;
197     m_initPos.dAngularDet = -m_dAngularDetLen / 2;
198     
199     m_dFanBeamAngle = dAngle * 2;
200     m_initPos.angle = 0;
201     m_initPos.xs1 = m_dXCenter;
202     m_initPos.ys1 = m_dYCenter + m_dFocalLength;;
203     m_initPos.xs2 = m_dXCenter;
204     m_initPos.ys2 = m_dYCenter + m_dFocalLength;
205   }
206   
207   // Calculate incrementatal rotation matrix 
208   GRFMTX_2D temp;
209   xlat_mtx2 (m_rotmtxIncrement, -m_dXCenter, -m_dYCenter);
210   rot_mtx2 (temp, m_rotInc);
211   mult_mtx2 (m_rotmtxIncrement, temp, m_rotmtxIncrement);
212   xlat_mtx2 (temp, m_dXCenter, m_dYCenter);
213   mult_mtx2 (m_rotmtxIncrement, temp, m_rotmtxIncrement);
214   
215 }
216
217 Scanner::~Scanner (void)
218 {
219 }
220
221
222 const char*
223 Scanner::convertGeometryIDToName (const int geomID)
224 {
225   const char *name = "";
226   
227   if (geomID >= 0 && geomID < s_iGeometryCount)
228     return (s_aszGeometryName[geomID]);
229   
230   return (name);
231 }
232
233 const char*
234 Scanner::convertGeometryIDToTitle (const int geomID)
235 {
236   const char *title = "";
237   
238   if (geomID >= 0 && geomID < s_iGeometryCount)
239     return (s_aszGeometryName[geomID]);
240   
241   return (title);
242 }
243
244 int
245 Scanner::convertGeometryNameToID (const char* const geomName) 
246 {
247   int id = GEOMETRY_INVALID;
248   
249   for (int i = 0; i < s_iGeometryCount; i++)
250     if (strcasecmp (geomName, s_aszGeometryName[i]) == 0) {
251       id = i;
252       break;
253     }
254     
255     return (id);
256 }
257
258
259 /* NAME
260 *   collectProjections          Calculate projections for a Phantom
261 *
262 * SYNOPSIS
263 *   collectProjections (proj, phm, start_view, nView, bStoreViewPos, trace)
264 *   Projectrions& proj      Projection storage
265 *   Phantom& phm             Phantom for which we collect projections
266 *   bool bStoreViewPos      TRUE then storage proj at normal view position
267 *   int trace                Trace level
268 */
269
270
271 void
272 Scanner::collectProjections (Projections& proj, const Phantom& phm, const int trace, SGP* pSGP)
273 {
274   collectProjections (proj, phm, 0, proj.nView(), true, trace, pSGP);
275 }
276
277 void
278 Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iStartView, const int iNumViews, 
279                              bool bStoreAtViewPosition, const int trace, SGP* pSGP)
280 {
281   int iStorageOffset = (bStoreAtViewPosition ? iStartView : 0);
282   collectProjections (proj, phm, iStartView, iNumViews, iStorageOffset, trace, pSGP);
283 }
284
285 void
286 Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iStartView, const int iNumViews, 
287                              int iStorageOffset, const int trace, SGP* pSGP)
288 {
289   m_trace = trace;
290   double start_angle = iStartView * proj.rotInc();
291   
292   // Calculate initial rotation matrix 
293   GRFMTX_2D rotmtx_initial, temp;
294   xlat_mtx2 (rotmtx_initial, -m_dXCenter, -m_dYCenter);
295   rot_mtx2 (temp, start_angle);
296   mult_mtx2 (rotmtx_initial, temp, rotmtx_initial);
297   xlat_mtx2 (temp, m_dXCenter, m_dYCenter);
298   mult_mtx2 (rotmtx_initial, temp, rotmtx_initial);
299   
300   double xd1=0, yd1=0, xd2=0, yd2=0;
301   if (m_idGeometry != GEOMETRY_EQUIANGULAR) {
302     xd1 = m_initPos.xd1;
303     yd1 = m_initPos.yd1;
304     xd2 = m_initPos.xd2;
305     yd2 = m_initPos.yd2;
306     xform_mtx2 (rotmtx_initial, xd1, yd1);      // rotate detector endpoints 
307     xform_mtx2 (rotmtx_initial, xd2, yd2);      // to initial view_angle 
308   }
309   
310   double xs1 = m_initPos.xs1;
311   double ys1 = m_initPos.ys1;
312   double xs2 = m_initPos.xs2;
313   double ys2 = m_initPos.ys2;
314   xform_mtx2 (rotmtx_initial, xs1, ys1);      // rotate source endpoints to
315   xform_mtx2 (rotmtx_initial, xs2, ys2);      // initial view angle
316   
317   int iView;
318   double viewAngle;
319   for (iView = 0, viewAngle = start_angle;  iView < iNumViews; iView++, viewAngle += proj.rotInc()) {
320     int iStoragePosition = iView + iStorageOffset;
321
322     DetectorArray& detArray = proj.getDetectorArray( iStoragePosition );
323     
324 #ifdef HAVE_SGP 
325     if (pSGP && m_trace >= Trace::TRACE_PHANTOM) {
326       m_pSGP = pSGP;
327       double dWindowSize = dmax (m_detLen, m_dSourceDetectorLength) * 2;
328       double dHalfWindowSize = dWindowSize / 2;
329       m_dXMinWin = m_dXCenter - dHalfWindowSize;
330       m_dXMaxWin = m_dXCenter + dHalfWindowSize;
331       m_dYMinWin = m_dYCenter - dHalfWindowSize;
332       m_dYMaxWin = m_dYCenter + dHalfWindowSize;
333       
334       m_pSGP->setWindow (m_dXMinWin, m_dYMinWin, m_dXMaxWin, m_dYMaxWin);
335       m_pSGP->setRasterOp (RO_COPY);
336
337       m_pSGP->setColor (C_RED);
338       m_pSGP->moveAbs (0., 0.);
339       m_pSGP->drawCircle (m_dViewDiameter / 2);
340
341       m_pSGP->moveAbs (0., 0.);
342       m_pSGP->setColor (C_GREEN);
343       m_pSGP->drawCircle (m_dFocalLength);
344       m_pSGP->setColor (C_BLUE);
345       m_pSGP->setTextPointSize (9);
346       phm.draw (*m_pSGP);
347       m_dTextHeight = m_pSGP->getCharHeight ();
348       
349       traceShowParam ("Phantom:",       "%s", PROJECTION_TRACE_ROW_PHANT_ID, C_BLACK, phm.name().c_str());
350       traceShowParam ("Geometry:", "%s", PROJECTION_TRACE_ROW_GEOMETRY, C_BLUE, convertGeometryIDToName(m_idGeometry));
351       traceShowParam ("Focal Length Ratio:", "%.2f", PROJECTION_TRACE_ROW_FOCAL_LENGTH, C_BLUE, m_dFocalLengthRatio);
352 //      traceShowParam ("Field Of View Ratio:", "%.2f", PROJECTION_TRACE_ROW_FIELD_OF_VIEW, C_BLUE, m_dFieldOfViewRatio);
353       traceShowParam ("Num Detectors:", "%d", PROJECTION_TRACE_ROW_NDET, C_BLUE, proj.nDet());
354       traceShowParam ("Num Views:", "%d", PROJECTION_TRACE_ROW_NVIEW, C_BLUE, proj.nView());
355       traceShowParam ("Samples / Ray:", "%d", PROJECTION_TRACE_ROW_SAMPLES, C_BLUE, m_nSample);
356       
357       m_pSGP->setMarker (SGP::MARK_BDIAMOND, C_LTGREEN);
358     }
359 #endif
360     
361 #ifdef HAVE_SGP
362     if (m_pSGP && m_trace >= Trace::TRACE_PHANTOM) {
363       m_pSGP->setColor (C_BLACK);
364       m_pSGP->setPenWidth (2);
365       if (m_idGeometry == GEOMETRY_PARALLEL) {
366         m_pSGP->moveAbs (xs1, ys1);
367         m_pSGP->lineAbs (xs2, ys2);
368         m_pSGP->moveAbs (xd1, yd1);
369         m_pSGP->lineAbs (xd2, yd2);
370       } else if (m_idGeometry == GEOMETRY_EQUILINEAR) { 
371         m_pSGP->setPenWidth (4);
372         m_pSGP->moveAbs (xs1, ys1);
373         m_pSGP->lineAbs (xs2, ys2);
374         m_pSGP->setPenWidth (2);
375         m_pSGP->moveAbs (xd1, yd1);
376         m_pSGP->lineAbs (xd2, yd2);
377       } else if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
378         m_pSGP->setPenWidth (4);
379         m_pSGP->moveAbs (xs1, ys1);
380         m_pSGP->lineAbs (xs2, ys2);
381         m_pSGP->setPenWidth (2);
382         m_pSGP->moveAbs (0., 0.);
383         m_pSGP->drawArc (m_dCenterDetectorLength, viewAngle + 3 * HALFPI - (m_dAngularDetLen/2), viewAngle + 3 * HALFPI + (m_dAngularDetLen/2));
384       }
385       m_pSGP->setPenWidth (1);
386     }
387     if (m_trace > Trace::TRACE_CONSOLE)
388       traceShowParam ("Current View:", "%d (%.0f%%)", PROJECTION_TRACE_ROW_CURR_VIEW, C_RED, iView + iStartView, (iView + iStartView) / static_cast<double>(m_nView) * 100.);
389 #endif
390     if (m_trace == Trace::TRACE_CONSOLE)
391       std::cout << "Current View: " << iView+iStartView << std::endl;
392     
393     projectSingleView (phm, detArray, xd1, yd1, xd2, yd2, xs1, ys1, xs2, ys2, viewAngle + 3 * HALFPI);
394     detArray.setViewAngle (viewAngle);
395     
396 #ifdef HAVE_SGP
397     if (m_pSGP && m_trace >= Trace::TRACE_PHANTOM) {
398       //        rs_plot (detArray, xd1, yd1, dXCenter, dYCenter, theta);
399     }
400 #endif
401     xform_mtx2 (m_rotmtxIncrement, xs1, ys1);
402     xform_mtx2 (m_rotmtxIncrement, xs2, ys2);
403     if (m_idGeometry != GEOMETRY_EQUIANGULAR) {
404       xform_mtx2 (m_rotmtxIncrement, xd1, yd1);  // rotate detector endpoints 
405       xform_mtx2 (m_rotmtxIncrement, xd2, yd2);
406     }
407   } /* for each iView */
408 }
409
410
411 /* NAME
412 *    rayview                    Calculate raysums for a view at any angle
413 *
414 * SYNOPSIS
415 *    rayview (phm, detArray, xd1, nSample, yd1, xd2, yd2, xs1, ys1, xs2, ys2)
416 *    Phantom& phm               Phantom to scan
417 *    DETARRAY *detArray         Storage of values for detector array
418 *    Scanner& det               Scanner parameters
419 *    double xd1, yd1, xd2, yd2  Beginning & ending detector positions
420 *    double xs1, ys1, xs2, ys2  Beginning & ending source positions
421 *
422 * RAY POSITIONING
423 *         For each detector, have there are a variable number of rays traced.
424 *     The source of each ray is the center of the source x-ray cell. The
425 *     detector positions are equally spaced within the cell
426 *
427 *         The increments between rays are calculated so that the cells start
428 *     at the beginning of a detector cell and they end on the endpoint
429 *     of the cell.  Thus, the last cell starts at (xd2-ddx),(yd2-ddy).
430 *         The exception to this is if there is only one ray per detector.
431 *     In that case, the detector position is the center of the detector cell.
432 */
433
434 void 
435 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, const double dDetAngle)
436 {
437   
438   double sdx = (xs2 - xs1) / detArray.nDet();  // change in coords 
439   double sdy = (ys2 - ys1) / detArray.nDet();  // between source
440   double xs_maj = xs1 + (sdx / 2);      // put ray source in center of cell 
441   double ys_maj = ys1 + (sdy / 2);
442   
443   double ddx=0, ddy=0, ddx2=0, ddy2=0, ddx2_ofs=0, ddy2_ofs=0, xd_maj=0, yd_maj=0;
444   double dAngleInc=0, dAngleSampleInc=0, dAngleSampleOffset=0, dAngleMajor=0;
445   if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
446     dAngleInc = m_dAngularDetIncrement;
447     dAngleSampleInc = dAngleInc / m_nSample;
448     dAngleSampleOffset = dAngleSampleInc / 2;
449     dAngleMajor = dDetAngle - (m_dAngularDetLen/2) + dAngleSampleOffset;
450   } else {
451     ddx = (xd2 - xd1) / detArray.nDet();  // change in coords 
452     ddy = (yd2 - yd1) / detArray.nDet();  // between detectors
453     ddx2 = ddx / m_nSample;     // Incr. between rays with detector cell
454     ddy2 = ddy / m_nSample;  // Doesn't include detector endpoints 
455     ddx2_ofs = ddx2 / 2;    // offset of 1st ray from start of detector cell
456     ddy2_ofs = ddy2 / 2;
457     
458     xd_maj = xd1 + ddx2_ofs;       // Incr. between detector cells
459     yd_maj = yd1 + ddy2_ofs;
460   }
461   
462   DetectorValue* detval = detArray.detValues();
463   
464   if (phm.getComposition() == P_UNIT_PULSE) {  // put unit pulse in center of view
465     for (int d = 0; d < detArray.nDet(); d++)
466       if (detArray.nDet() / 2 == d && (d % 2) == 1)
467         detval[d] = 1;
468       else
469         detval[d] = 0;
470   } else {
471     for (int d = 0; d < detArray.nDet(); d++) {
472       double xs = xs_maj;
473       double ys = ys_maj;
474       double xd=0, yd=0, dAngle=0;
475       if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
476         dAngle = dAngleMajor;
477       } else {
478         xd = xd_maj;
479         yd = yd_maj;
480       }
481       double sum = 0.0;
482       for (unsigned int i = 0; i < m_nSample; i++) {
483         if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
484           xd = m_dCenterDetectorLength * cos (dAngle);
485           yd = m_dCenterDetectorLength * sin (dAngle);
486         }
487         
488 #ifdef HAVE_SGP
489         if (m_pSGP && m_trace >= Trace::TRACE_PROJECTIONS) {
490           m_pSGP->setColor (C_YELLOW);
491           m_pSGP->setRasterOp (RO_AND);
492           m_pSGP->moveAbs (xs, ys);
493           m_pSGP->lineAbs (xd, yd);
494         }
495 #endif
496         
497         sum += projectSingleLine (phm, xd, yd, xs, ys);
498         
499 #ifdef HAVE_SGP
500         //      if (m_trace >= Trace::TRACE_CLIPPING) {
501         //        traceShowParam ("Attenuation:", "%s", PROJECTION_TRACE_ROW_ATTEN, C_LTMAGENTA, "        ");
502         //        traceShowParam ("Attenuation:", "%.3f", PROJECTION_TRACE_ROW_ATTEN, C_LTMAGENTA, sum);
503         //      }
504 #endif
505         if (m_idGeometry == GEOMETRY_EQUIANGULAR)
506           dAngle += dAngleSampleInc;
507         else {
508           xd += ddx2;
509           yd += ddy2;
510         }
511       } // for each sample in detector
512       
513       detval[d] = sum / m_nSample;
514       xs_maj += sdx;
515       ys_maj += sdy;
516       if (m_idGeometry == GEOMETRY_EQUIANGULAR)
517         dAngleMajor += dAngleInc;
518       else {
519         xd_maj += ddx;
520         yd_maj += ddy;
521       }
522     } /* for each detector */
523   } /* if not unit pulse */
524 }
525
526
527 void 
528 Scanner::traceShowParam (const char *szLabel, const char *fmt, int row, int color, ...)
529 {  
530   va_list arg;
531   va_start(arg, color);
532 #ifdef HAVE_SGP
533   traceShowParamRasterOp (RO_COPY, szLabel, fmt, row, color, arg);
534 #else
535   traceShowParamRasterOp (0, szLabel, fmt, row, color, arg);
536 #endif  
537   va_end(arg);
538 }
539
540 void 
541 Scanner::traceShowParamXOR (const char *szLabel, const char *fmt, int row, int color, ...)
542 {  
543   va_list arg;
544   va_start(arg, color);
545 #ifdef HAVE_SGP
546   traceShowParamRasterOp (RO_XOR, szLabel, fmt, row, color, arg);
547 #else
548   traceShowParamRasterOp (0, szLabel, fmt, row, color, arg);
549 #endif
550   va_end(arg);
551 }
552
553 void 
554 Scanner::traceShowParamRasterOp (int iRasterOp, const char *szLabel, const char *fmt, int row, int color, va_list args)
555 {  
556   char szValue[256];
557   
558   vsnprintf (szValue, sizeof(szValue), fmt, args);
559   
560 #ifdef HAVE_SGP
561   if (m_pSGP) {
562     m_pSGP->setRasterOp (iRasterOp);
563     m_pSGP->setTextColor (color, -1);
564     double dValueOffset = (m_dXMaxWin - m_dXMinWin) / 4;
565     if (row < 4) {
566       double dYPos = m_dYMaxWin - (row * m_dTextHeight);
567       double dXPos = m_dXMinWin;
568       m_pSGP->moveAbs (dXPos, dYPos);
569       m_pSGP->drawText (szLabel);
570       m_pSGP->moveAbs (dXPos + dValueOffset, dYPos);
571       m_pSGP->drawText (szValue);
572     } else {
573       row -= 4;
574       double dYPos = m_dYMaxWin - (row * m_dTextHeight);
575       double dXPos = m_dXMinWin + (m_dXMaxWin - m_dXMinWin) * 0.5;
576       m_pSGP->moveAbs (dXPos, dYPos);
577       m_pSGP->drawText (szLabel);
578       m_pSGP->moveAbs (dXPos + dValueOffset, dYPos);
579       m_pSGP->drawText (szValue);
580     }
581   } else 
582 #endif
583   {
584     cio_put_str (szLabel);
585     cio_put_str (szValue);
586     cio_put_str ("\n");
587   }
588 }
589
590
591
592 /* NAME
593 *    projectSingleLine                  INTERNAL: Calculates raysum along a line for a Phantom
594 *
595 * SYNOPSIS
596 *    rsum = phm_ray_attenuation (phm, x1, y1, x2, y2)
597 *    double rsum                Ray sum of Phantom along given line
598 *    Phantom& phm;              Phantom from which to calculate raysum
599 *    double *x1, *y1, *x2, y2   Endpoints of ray path (in Phantom coords)
600 */
601
602 double 
603 Scanner::projectSingleLine (const Phantom& phm, const double x1, const double y1, const double x2, const double y2)
604 {
605   // check ray against each pelem in Phantom 
606   double rsum = 0.0;
607   for (PElemConstIterator i = phm.listPElem().begin(); i != phm.listPElem().end(); i++)
608     rsum += projectLineAgainstPElem (**i, x1, y1, x2, y2);
609   
610   return (rsum);
611 }
612
613
614 /* NAME
615 *   pelem_ray_attenuation               Calculate raysum of an pelem along one line
616 *
617 * SYNOPSIS
618 *   rsum = pelem_ray_attenuation (pelem, x1, y1, x2, y2)
619 *   double rsum         Computed raysum
620 *   PhantomElement& pelem               Pelem to scan
621 *   double x1, y1, x2, y2       Endpoints of raysum line
622 */
623
624 double 
625 Scanner::projectLineAgainstPElem (const PhantomElement& pelem, double x1, double y1, double x2, double y2)
626 {
627   if (! pelem.clipLineWorldCoords (x1, y1, x2, y2)) {
628     if (m_trace == Trace::TRACE_CLIPPING)
629       cio_tone (1000., 0.05);
630     return (0.0);
631   }
632   
633 #ifdef HAVE_SGP
634   if (m_pSGP && m_trace == Trace::TRACE_CLIPPING) {
635     m_pSGP->setRasterOp (RO_XOR);
636     m_pSGP->moveAbs (x1, y1);
637     m_pSGP->lineAbs (x2, y2);
638     cio_tone (8000., 0.05);
639     m_pSGP->moveAbs (x1, y1);
640     m_pSGP->lineAbs (x2, y2);
641     m_pSGP->setRasterOp (RO_SET);
642   }
643 #endif
644   
645   double len = lineLength (x1, y1, x2, y2);
646   return (len * pelem.atten());
647 }
648