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