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