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