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