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