File: scanner.cpp

package info (click to toggle)
ctsim 6.0.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,868 kB
  • sloc: cpp: 26,967; sh: 7,782; ansic: 1,256; perl: 296; makefile: 148
file content (653 lines) | stat: -rw-r--r-- 22,267 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*****************************************************************************
** FILE IDENTIFICATION
**
**   Name:          scanner.cpp
**   Purpose:       Classes for CT scanner
**   Programmer:    Kevin Rosenberg
**   Date Started:  1984
**
**  This is part of the CTSim program
**  Copyright (c) 1983-2009 Kevin Rosenberg
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License (version 2) as
**  published by the Free Software Foundation.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

#include "ct.h"


const int Scanner::GEOMETRY_INVALID = -1;
const int Scanner::GEOMETRY_PARALLEL = 0;
const int Scanner::GEOMETRY_EQUIANGULAR = 1;
const int Scanner::GEOMETRY_EQUILINEAR = 2;
const int Scanner::GEOMETRY_LINOGRAM = 3;

const char* const Scanner::s_aszGeometryName[] =
{
  "parallel",
  "equiangular",
  "equilinear",
  "linogram",
};

const char* const Scanner::s_aszGeometryTitle[] =
{
  "Parallel",
  "Equiangular",
  "Equilinear",
  "Linogram",
};

const int Scanner::s_iGeometryCount = sizeof(s_aszGeometryName) / sizeof(const char*);


// NAME
//   DetectorArray       Construct a DetectorArray

DetectorArray::DetectorArray (const int nDet)
{
  m_nDet = nDet;
  m_detValues = new DetectorValue [m_nDet];
}


// NAME
//   ~DetectorArray             Free memory allocated to a detector array

DetectorArray::~DetectorArray (void)
{
  delete [] m_detValues;
}



/* NAME
*   Scanner::Scanner            Construct a user specified detector structure
*
* SYNOPSIS
*   Scanner (phm, nDet, nView, nSample)
*   Phantom& phm                PHANTOM that we are making detector for
*   int geomety                Geometry of detector
*   int nDet                    Number of detector along detector array
*   int nView                   Number of rotated views
*   int nSample         Number of rays per detector
*/

Scanner::Scanner (const Phantom& phm, const char* const geometryName,
                  int nDet, int nView, int offsetView,
                                  int nSample, const double rot_anglen,
                  const double dFocalLengthRatio,
                                  const double dCenterDetectorRatio,
                  const double dViewRatio, const double dScanRatio)
{
  m_fail = false;
  m_idGeometry = convertGeometryNameToID (geometryName);
  if (m_idGeometry == GEOMETRY_INVALID) {
    m_fail = true;
    m_failMessage = "Invalid geometry name ";
    m_failMessage += geometryName;
    return;
  }

  if (nView < 1 || nDet < 1) {
    m_fail = true;
    m_failMessage = "nView & nDet must be greater than 0";
    return;
  }
  if (nSample < 1)
    m_nSample = 1;

  m_nDet     = nDet;
  m_nView    = nView;
  m_iOffsetView = offsetView;
  m_nSample  = nSample;
  m_dFocalLengthRatio = dFocalLengthRatio;
  m_dCenterDetectorRatio = dCenterDetectorRatio;
  m_dViewRatio = dViewRatio;
  m_dScanRatio = dScanRatio;

  m_dViewDiameter = phm.getDiameterBoundaryCircle() * m_dViewRatio;
  m_dFocalLength = (m_dViewDiameter / 2) * m_dFocalLengthRatio;
  m_dCenterDetectorLength = (m_dViewDiameter / 2) * m_dCenterDetectorRatio;
  m_dSourceDetectorLength = m_dFocalLength + m_dCenterDetectorLength;
  m_dScanDiameter = m_dViewDiameter * m_dScanRatio;

  m_dXCenter = phm.xmin() + (phm.xmax() - phm.xmin()) / 2;
  m_dYCenter = phm.ymin() + (phm.ymax() - phm.ymin()) / 2;
  m_rotLen  = rot_anglen;
  m_rotInc  = m_rotLen / m_nView;
  if (m_idGeometry == GEOMETRY_PARALLEL) {
    m_dFanBeamAngle = 0;
    m_detLen   = m_dScanDiameter;
    m_detStart = -m_detLen / 2;
    m_detInc  = m_detLen / m_nDet;
    double dDetectorArrayEndOffset = 0;
    // For even number of detectors, make detInc slightly larger so that center lies
    // at nDet/2. Also, extend detector array by one detInc so that all of the phantom is scanned
    if (isEven (m_nDet)) { // Adjust for Even number of detectors
      m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
      dDetectorArrayEndOffset = m_detInc;
    }

    double dHalfDetLen = m_detLen / 2;
    m_initPos.xs1 = m_dXCenter - dHalfDetLen;
    m_initPos.ys1 = m_dYCenter + m_dFocalLength;
    m_initPos.xs2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
    m_initPos.ys2 = m_dYCenter + m_dFocalLength;
    m_initPos.xd1 = m_dXCenter - dHalfDetLen;
    m_initPos.yd1 = m_dYCenter - m_dCenterDetectorLength;
    m_initPos.xd2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
    m_initPos.yd2 = m_dYCenter - m_dCenterDetectorLength;
    m_initPos.angle = m_iOffsetView * m_rotInc;
    m_detLen += dDetectorArrayEndOffset;
  } else if (m_idGeometry == GEOMETRY_EQUILINEAR) {
  if (m_dScanDiameter / 2 >= m_dFocalLength) {
      m_fail = true;
      m_failMessage = "Invalid geometry: Focal length must be larger than scan length";
      return;
    }

    const double dAngle = asin ((m_dScanDiameter / 2) / m_dFocalLength);
    const double dHalfDetLen = m_dSourceDetectorLength * tan (dAngle);

    m_detLen = dHalfDetLen * 2;
    m_detStart = -dHalfDetLen;
    m_detInc  = m_detLen / m_nDet;
    double dDetectorArrayEndOffset = 0;
    if (isEven (m_nDet)) { // Adjust for Even number of detectors
      m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
      dDetectorArrayEndOffset = m_detInc;
      m_detLen += dDetectorArrayEndOffset;
    }

    m_dFanBeamAngle = dAngle * 2;
    m_initPos.xs1 = m_dXCenter;
    m_initPos.ys1 = m_dYCenter + m_dFocalLength;
    m_initPos.xs2 = m_dXCenter;
    m_initPos.ys2 = m_dYCenter + m_dFocalLength;
    m_initPos.xd1 = m_dXCenter - dHalfDetLen;
    m_initPos.yd1 = m_dYCenter - m_dCenterDetectorLength;
    m_initPos.xd2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
    m_initPos.yd2 = m_dYCenter - m_dCenterDetectorLength;
    m_initPos.angle = m_iOffsetView * m_rotInc;
  } else if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
    if (m_dScanDiameter / 2 > m_dFocalLength) {
      m_fail = true;
      m_failMessage = "Invalid geometry: Focal length must be larger than scan length";
      return;
    }
    const double dAngle = asin ((m_dScanDiameter / 2) / m_dFocalLength);

    m_detLen = 2 * dAngle;
    m_detStart = -dAngle;
    m_detInc = m_detLen / m_nDet;
    double dDetectorArrayEndOffset = 0;
    if (isEven (m_nDet)) { // Adjust for Even number of detectors
      m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
      dDetectorArrayEndOffset = m_detInc;
    }
    // adjust for center-detector length
    double dA1 = acos ((m_dScanDiameter / 2) / m_dCenterDetectorLength);
    double dAngularScale = 2 * (HALFPI + dAngle - dA1) / m_detLen;

    m_dAngularDetLen = dAngularScale * (m_detLen + dDetectorArrayEndOffset);
    m_dAngularDetIncrement = dAngularScale * m_detInc;
    m_initPos.dAngularDet = -m_dAngularDetLen / 2;

    m_dFanBeamAngle = dAngle * 2;
    m_initPos.angle = m_iOffsetView * m_rotInc;
    m_initPos.xs1 = m_dXCenter;
    m_initPos.ys1 = m_dYCenter + m_dFocalLength;;
    m_initPos.xs2 = m_dXCenter;
    m_initPos.ys2 = m_dYCenter + m_dFocalLength;
    m_detLen += dDetectorArrayEndOffset;
  }

  // Calculate incrementatal rotation matrix
  GRFMTX_2D temp;
  xlat_mtx2 (m_rotmtxIncrement, -m_dXCenter, -m_dYCenter);
  rot_mtx2 (temp, m_rotInc);
  mult_mtx2 (m_rotmtxIncrement, temp, m_rotmtxIncrement);
  xlat_mtx2 (temp, m_dXCenter, m_dYCenter);
  mult_mtx2 (m_rotmtxIncrement, temp, m_rotmtxIncrement);

}

Scanner::~Scanner (void)
{
}


const char*
Scanner::convertGeometryIDToName (const int geomID)
{
  const char *name = "";

  if (geomID >= 0 && geomID < s_iGeometryCount)
    return (s_aszGeometryName[geomID]);

  return (name);
}

const char*
Scanner::convertGeometryIDToTitle (const int geomID)
{
  const char *title = "";

  if (geomID >= 0 && geomID < s_iGeometryCount)
    return (s_aszGeometryName[geomID]);

  return (title);
}

int
Scanner::convertGeometryNameToID (const char* const geomName)
{
  int id = GEOMETRY_INVALID;

  for (int i = 0; i < s_iGeometryCount; i++) {
    if (strcasecmp (geomName, s_aszGeometryName[i]) == 0) {
      id = i;
      break;
    }
  }
  return (id);
}


/* NAME
*   collectProjections          Calculate projections for a Phantom
*
* SYNOPSIS
*   collectProjections (proj, phm, start_view, nView, bStoreViewPos, trace)
*   Projectrions& proj      Projection storage
*   Phantom& phm             Phantom for which we collect projections
*   bool bStoreViewPos      TRUE then storage proj at normal view position
*   int trace                Trace level
*/


void
Scanner::collectProjections (Projections& proj, const Phantom& phm, const int trace, SGP* pSGP)
{
  collectProjections (proj, phm, m_startView, proj.nView(), m_iOffsetView, true, trace, pSGP);
}

void
Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iStartView,
                             const int iNumViews, const int iOffsetView,  bool bStoreAtViewPosition,
                             const int trace, SGP* pSGP)
{
  int iStorageOffset = (bStoreAtViewPosition ? iStartView : 0);
  collectProjections (proj, phm, iStartView, iNumViews, iOffsetView, iStorageOffset, trace, pSGP);
}

static void mtx2_offset_rot (GRFMTX_2D m, double angle, double x, double y) {
  GRFMTX_2D temp;
  xlat_mtx2 (m, -x, -y);
  rot_mtx2 (temp, angle);
  mult_mtx2 (m, temp, m);
  xlat_mtx2 (temp, x, y);
  mult_mtx2 (m, temp, m);
}

void
Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iStartView,
                             const int iNumViews, const int iOffsetView, int iStorageOffset,
                             const int trace, SGP* pSGP)
{
  m_trace = trace;
  double start_angle = (iStartView + iOffsetView) * proj.rotInc();

#ifdef HAVE_OPENMP
  #pragma omp parallel for
#endif
  
  for (int iView = 0;  iView < iNumViews; iView++) {
    double viewAngle = start_angle + (iView * proj.rotInc());
    
    // With OpenMP, need to calculate source and detector positions at each view
    GRFMTX_2D rotmtx;
    mtx2_offset_rot (rotmtx, viewAngle, m_dXCenter, m_dYCenter);
    double xd1=0, yd1=0, xd2=0, yd2=0;
    if (m_idGeometry != GEOMETRY_EQUIANGULAR) {
      xd1 = m_initPos.xd1; yd1 = m_initPos.yd1;
      xd2 = m_initPos.xd2; yd2 = m_initPos.yd2;
      xform_mtx2 (rotmtx, xd1, yd1);      // rotate detector endpoints
      xform_mtx2 (rotmtx, xd2, yd2);      // to initial view_angle
    }
    
    double xs1 = m_initPos.xs1, ys1 = m_initPos.ys1;
    double xs2 = m_initPos.xs2, ys2 = m_initPos.ys2;
    xform_mtx2 (rotmtx, xs1, ys1);      // rotate source endpoints to
    xform_mtx2 (rotmtx, xs2, ys2);      // initial view angle
    
    int iStoragePosition = iView + iStorageOffset;
    DetectorArray& detArray = proj.getDetectorArray( iStoragePosition );

#ifdef HAVE_SGP
    if (pSGP && m_trace >= Trace::TRACE_PHANTOM) {
      m_pSGP = pSGP;
      double dWindowSize = dmax (m_detLen, m_dSourceDetectorLength) * 2;
      double dHalfWindowSize = dWindowSize / 2;
      m_dXMinWin = m_dXCenter - dHalfWindowSize;
      m_dXMaxWin = m_dXCenter + dHalfWindowSize;
      m_dYMinWin = m_dYCenter - dHalfWindowSize;
      m_dYMaxWin = m_dYCenter + dHalfWindowSize;

      m_pSGP->setWindow (m_dXMinWin, m_dYMinWin, m_dXMaxWin, m_dYMaxWin);
      m_pSGP->setRasterOp (RO_COPY);

      m_pSGP->setColor (C_RED);
      m_pSGP->moveAbs (0., 0.);
      m_pSGP->drawCircle (m_dViewDiameter / 2);

      m_pSGP->moveAbs (0., 0.);
      m_pSGP->setColor (C_GREEN);
      m_pSGP->drawCircle (m_dFocalLength);
      m_pSGP->setColor (C_BLUE);
      m_pSGP->setTextPointSize (9);
      phm.draw (*m_pSGP);
      m_dTextHeight = m_pSGP->getCharHeight ();

      traceShowParam ("Phantom:",       "%s", PROJECTION_TRACE_ROW_PHANT_ID, C_BLACK, phm.name().c_str());
      traceShowParam ("Geometry:", "%s", PROJECTION_TRACE_ROW_GEOMETRY, C_BLUE, convertGeometryIDToName(m_idGeometry));
      traceShowParam ("Focal Length Ratio:", "%.2f", PROJECTION_TRACE_ROW_FOCAL_LENGTH, C_BLUE, m_dFocalLengthRatio);
      //      traceShowParam ("Field Of View Ratio:", "%.2f", PROJECTION_TRACE_ROW_FIELD_OF_VIEW, C_BLUE, m_dFieldOfViewRatio);
      traceShowParam ("Num Detectors:", "%d", PROJECTION_TRACE_ROW_NDET, C_BLUE, proj.nDet());
      traceShowParam ("Num Views:", "%d", PROJECTION_TRACE_ROW_NVIEW, C_BLUE, proj.nView());
      traceShowParam ("Samples / Ray:", "%d", PROJECTION_TRACE_ROW_SAMPLES, C_BLUE, m_nSample);

      m_pSGP->setMarker (SGP::MARKER_BDIAMOND);

      
      m_pSGP->setColor (C_BLACK);
      m_pSGP->setPenWidth (2);
      if (m_idGeometry == GEOMETRY_PARALLEL) {
        m_pSGP->moveAbs (xs1, ys1);
        m_pSGP->lineAbs (xs2, ys2);
        m_pSGP->moveAbs (xd1, yd1);
        m_pSGP->lineAbs (xd2, yd2);
      } else if (m_idGeometry == GEOMETRY_EQUILINEAR) {
        m_pSGP->setPenWidth (4);
        m_pSGP->moveAbs (xs1, ys1);
        m_pSGP->lineAbs (xs2, ys2);
        m_pSGP->setPenWidth (2);
        m_pSGP->moveAbs (xd1, yd1);
        m_pSGP->lineAbs (xd2, yd2);
      } else if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
        m_pSGP->setPenWidth (4);
        m_pSGP->moveAbs (xs1, ys1);
        m_pSGP->lineAbs (xs2, ys2);
        m_pSGP->setPenWidth (2);
        m_pSGP->moveAbs (0., 0.);
        m_pSGP->drawArc (m_dCenterDetectorLength, viewAngle + 3 * HALFPI - (m_dAngularDetLen/2), viewAngle + 3 * HALFPI + (m_dAngularDetLen/2));
      }
      m_pSGP->setPenWidth (1);
    }
    if (m_trace > Trace::TRACE_CONSOLE)
      traceShowParam ("Current View:", "%d (%.0f%%)", PROJECTION_TRACE_ROW_CURR_VIEW, C_RED, iView + iStartView, (iView + iStartView) / static_cast<double>(m_nView) * 100.);
#endif

    if (m_trace == Trace::TRACE_CONSOLE)
      std::cout << "Current View: " << iView+iStartView << std::endl;

    projectSingleView (phm, detArray, xd1, yd1, xd2, yd2, xs1, ys1, xs2, ys2, viewAngle + 3 * HALFPI);
    detArray.setViewAngle (viewAngle);

#ifdef HAVE_SGP
    if (m_pSGP && m_trace >= Trace::TRACE_PHANTOM) {
      //        rs_plot (detArray, xd1, yd1, dXCenter, dYCenter, theta);
    }
#endif

  } /* for each iView */
}


/* NAME
*    rayview                    Calculate raysums for a view at any angle
*
* SYNOPSIS
*    rayview (phm, detArray, xd1, nSample, yd1, xd2, yd2, xs1, ys1, xs2, ys2)
*    Phantom& phm               Phantom to scan
*    DETARRAY *detArray         Storage of values for detector array
*    Scanner& det               Scanner parameters
*    double xd1, yd1, xd2, yd2  Beginning & ending detector positions
*    double xs1, ys1, xs2, ys2  Beginning & ending source positions
*
* RAY POSITIONING
*         For each detector, have there are a variable number of rays traced.
*     The source of each ray is the center of the source x-ray cell. The
*     detector positions are equally spaced within the cell
*
*         The increments between rays are calculated so that the cells start
*     at the beginning of a detector cell and they end on the endpoint
*     of the cell.  Thus, the last cell starts at (xd2-ddx),(yd2-ddy).
*         The exception to this is if there is only one ray per detector.
*     In that case, the detector position is the center of the detector cell.
*/

void
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)
{

  double sdx = (xs2 - xs1) / detArray.nDet();  // change in coords
  double sdy = (ys2 - ys1) / detArray.nDet();  // between source
  double xs_maj = xs1 + (sdx / 2);      // put ray source in center of cell
  double ys_maj = ys1 + (sdy / 2);

  double ddx=0, ddy=0, ddx2=0, ddy2=0, ddx2_ofs=0, ddy2_ofs=0, xd_maj=0, yd_maj=0;
  double dAngleInc=0, dAngleSampleInc=0, dAngleSampleOffset=0, dAngleMajor=0;
  if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
    dAngleInc = m_dAngularDetIncrement;
    dAngleSampleInc = dAngleInc / m_nSample;
    dAngleSampleOffset = dAngleSampleInc / 2;
    dAngleMajor = dDetAngle - (m_dAngularDetLen/2) + dAngleSampleOffset;
  } else {
    ddx = (xd2 - xd1) / detArray.nDet();  // change in coords
    ddy = (yd2 - yd1) / detArray.nDet();  // between detectors
    ddx2 = ddx / m_nSample;     // Incr. between rays with detector cell
    ddy2 = ddy / m_nSample;  // Doesn't include detector endpoints
    ddx2_ofs = ddx2 / 2;    // offset of 1st ray from start of detector cell
    ddy2_ofs = ddy2 / 2;

    xd_maj = xd1 + ddx2_ofs;       // Incr. between detector cells
    yd_maj = yd1 + ddy2_ofs;
  }

  DetectorValue* detval = detArray.detValues();

  if (phm.getComposition() == P_UNIT_PULSE) {  // put unit pulse in center of view
    for (int d = 0; d < detArray.nDet(); d++)
        detval[d] = 0;
    detval[ detArray.nDet() / 2 ] = 1;
  } else {
    for (int d = 0; d < detArray.nDet(); d++) {
      double xs = xs_maj;
      double ys = ys_maj;
      double xd=0, yd=0, dAngle=0;
      if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
        dAngle = dAngleMajor;
      } else {
        xd = xd_maj;
        yd = yd_maj;
      }
      double sum = 0.0;
      for (unsigned int i = 0; i < m_nSample; i++) {
        if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
          xd = m_dCenterDetectorLength * cos (dAngle);
          yd = m_dCenterDetectorLength * sin (dAngle);
        }

#ifdef HAVE_SGP
        if (m_pSGP && m_trace >= Trace::TRACE_PROJECTIONS) {
          m_pSGP->setColor (C_YELLOW);
          m_pSGP->setRasterOp (RO_AND);
          m_pSGP->moveAbs (xs, ys);
          m_pSGP->lineAbs (xd, yd);
        }
#endif

        sum += projectSingleLine (phm, xd, yd, xs, ys);

#ifdef HAVE_SGP
        //      if (m_trace >= Trace::TRACE_CLIPPING) {
        //        traceShowParam ("Attenuation:", "%s", PROJECTION_TRACE_ROW_ATTEN, C_LTMAGENTA, "        ");
        //        traceShowParam ("Attenuation:", "%.3f", PROJECTION_TRACE_ROW_ATTEN, C_LTMAGENTA, sum);
        //      }
#endif
        if (m_idGeometry == GEOMETRY_EQUIANGULAR)
          dAngle += dAngleSampleInc;
        else {
          xd += ddx2;
          yd += ddy2;
        }
      } // for each sample in detector

      detval[d] = sum / m_nSample;
      xs_maj += sdx;
      ys_maj += sdy;
      if (m_idGeometry == GEOMETRY_EQUIANGULAR)
        dAngleMajor += dAngleInc;
      else {
        xd_maj += ddx;
        yd_maj += ddy;
      }
    } /* for each detector */
  } /* if not unit pulse */
}


void
Scanner::traceShowParam (const char *szLabel, const char *fmt, int row, int color, ...)
{
  va_list arg;
  va_start(arg, color);
#ifdef HAVE_SGP
  traceShowParamRasterOp (RO_COPY, szLabel, fmt, row, color, arg);
#else
  traceShowParamRasterOp (0, szLabel, fmt, row, color, arg);
#endif
  va_end(arg);
}

void
Scanner::traceShowParamXOR (const char *szLabel, const char *fmt, int row, int color, ...)
{
  va_list arg;
  va_start(arg, color);
#ifdef HAVE_SGP
  traceShowParamRasterOp (RO_XOR, szLabel, fmt, row, color, arg);
#else
  traceShowParamRasterOp (0, szLabel, fmt, row, color, arg);
#endif
  va_end(arg);
}

void
Scanner::traceShowParamRasterOp (int iRasterOp, const char *szLabel, const char *fmt, int row, int color, va_list args)
{
  char szValue[256];

  vsnprintf (szValue, sizeof(szValue), fmt, args);

#ifdef HAVE_SGP
  if (m_pSGP) {
    m_pSGP->setRasterOp (iRasterOp);
    m_pSGP->setTextColor (color, -1);
    double dValueOffset = (m_dXMaxWin - m_dXMinWin) / 4;
    if (row < 4) {
      double dYPos = m_dYMaxWin - (row * m_dTextHeight);
      double dXPos = m_dXMinWin;
      m_pSGP->moveAbs (dXPos, dYPos);
      m_pSGP->drawText (szLabel);
      m_pSGP->moveAbs (dXPos + dValueOffset, dYPos);
      m_pSGP->drawText (szValue);
    } else {
      row -= 4;
      double dYPos = m_dYMaxWin - (row * m_dTextHeight);
      double dXPos = m_dXMinWin + (m_dXMaxWin - m_dXMinWin) * 0.5;
      m_pSGP->moveAbs (dXPos, dYPos);
      m_pSGP->drawText (szLabel);
      m_pSGP->moveAbs (dXPos + dValueOffset, dYPos);
      m_pSGP->drawText (szValue);
    }
  } else
#endif
  {
    cio_put_str (szLabel);
    cio_put_str (szValue);
    cio_put_str ("\n");
  }
}



/* NAME
*    projectSingleLine                  INTERNAL: Calculates raysum along a line for a Phantom
*
* SYNOPSIS
*    rsum = phm_ray_attenuation (phm, x1, y1, x2, y2)
*    double rsum                Ray sum of Phantom along given line
*    Phantom& phm;              Phantom from which to calculate raysum
*    double *x1, *y1, *x2, y2   Endpoints of ray path (in Phantom coords)
*/

double
Scanner::projectSingleLine (const Phantom& phm, const double x1, const double y1, const double x2, const double y2)
{
  // check ray against each pelem in Phantom
  double rsum = 0.0;
  for (PElemConstIterator i = phm.listPElem().begin(); i != phm.listPElem().end(); i++)
    rsum += projectLineAgainstPElem (**i, x1, y1, x2, y2);

  return (rsum);
}


/* NAME
*   pelem_ray_attenuation               Calculate raysum of an pelem along one line
*
* SYNOPSIS
*   rsum = pelem_ray_attenuation (pelem, x1, y1, x2, y2)
*   double rsum         Computed raysum
*   PhantomElement& pelem               Pelem to scan
*   double x1, y1, x2, y2       Endpoints of raysum line
*/

double
Scanner::projectLineAgainstPElem (const PhantomElement& pelem, double x1, double y1, double x2, double y2)
{
  if (! pelem.clipLineWorldCoords (x1, y1, x2, y2)) {
    if (m_trace == Trace::TRACE_CLIPPING)
      cio_tone (1000., 0.05);
    return (0.0);
  }

#ifdef HAVE_SGP
  if (m_pSGP && m_trace == Trace::TRACE_CLIPPING) {
    m_pSGP->setRasterOp (RO_XOR);
    m_pSGP->moveAbs (x1, y1);
    m_pSGP->lineAbs (x2, y2);
    cio_tone (8000., 0.05);
    m_pSGP->moveAbs (x1, y1);
    m_pSGP->lineAbs (x2, y2);
    m_pSGP->setRasterOp (RO_SET);
  }
#endif

  double len = lineLength (x1, y1, x2, y2);
  return (len * pelem.atten());
}