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