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