1 /*****************************************************************************
5 ** Purpose: Classes for CT scanner
6 ** Programmer: Kevin Rosenberg
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: scanner.cpp,v 1.32 2001/02/25 16:21:36 kevin Exp $
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.
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.
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 ******************************************************************************/
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;
36 const char* Scanner::s_aszGeometryName[] =
43 const char* Scanner::s_aszGeometryTitle[] =
50 const int Scanner::s_iGeometryCount = sizeof(s_aszGeometryName) / sizeof(const char*);
54 // DetectorArray Construct a DetectorArray
56 DetectorArray::DetectorArray (const int nDet)
59 m_detValues = new DetectorValue [m_nDet];
64 // ~DetectorArray Free memory allocated to a detector array
66 DetectorArray::~DetectorArray (void)
68 delete [] m_detValues;
74 * Scanner::Scanner Construct a user specified detector structure
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
85 Scanner::Scanner (const Phantom& phm, const char* const geometryName,
86 int nDet, int nView, int nSample, const double rot_anglen,
87 const double dFocalLengthRatio, const double dViewRatio,
91 m_idGeometry = convertGeometryNameToID (geometryName);
92 if (m_idGeometry == GEOMETRY_INVALID) {
94 m_failMessage = "Invalid geometry name ";
95 m_failMessage += geometryName;
99 if (nView < 1 || nDet < 1) {
101 m_failMessage = "nView & nDet must be greater than 0";
110 m_dFocalLengthRatio = dFocalLengthRatio;
111 m_dViewRatio = dViewRatio;
112 m_dScanRatio = dScanRatio;
113 m_dViewDiameter = phm.getDiameterBoundaryCircle() * m_dViewRatio;
114 m_dFocalLength = (m_dViewDiameter / 2) * dFocalLengthRatio;
115 m_dScanDiameter = m_dViewDiameter * m_dScanRatio;
117 m_dXCenter = phm.xmin() + (phm.xmax() - phm.xmin()) / 2;
118 m_dYCenter = phm.ymin() + (phm.ymax() - phm.ymin()) / 2;
119 m_rotLen = rot_anglen;
120 m_rotInc = m_rotLen / m_nView;
121 if (m_idGeometry == GEOMETRY_PARALLEL) {
123 m_detLen = m_dScanDiameter;
124 m_detInc = m_detLen / m_nDet;
125 double dDetectorArrayEndOffset = 0;
126 // For even number of detectors, make detInc slightly larger so that center lies
127 // at nDet/2. Also, extend detector array by one detInc so that all of the phantom is scanned
128 if (m_nDet % 2 == 0) { // Adjust for Even number of detectors
129 m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
130 dDetectorArrayEndOffset = m_detInc;
133 double dHalfDetLen = m_detLen / 2;
134 m_initPos.xs1 = m_dXCenter - dHalfDetLen;
135 m_initPos.ys1 = m_dYCenter + m_dFocalLength;
136 m_initPos.xs2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
137 m_initPos.ys2 = m_dYCenter + m_dFocalLength;
138 m_initPos.xd1 = m_dXCenter - dHalfDetLen;
139 m_initPos.yd1 = m_dYCenter - m_dFocalLength;
140 m_initPos.xd2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
141 m_initPos.yd2 = m_dYCenter - m_dFocalLength;
142 m_initPos.angle = 0.0;
143 } else if (m_idGeometry == GEOMETRY_EQUILINEAR) {
144 if (m_dScanDiameter / 2 >= m_dFocalLength) {
146 m_failMessage = "Invalid geometry: Focal length must be larger than scan length";
149 const double dAngle = asin ((m_dScanDiameter / 2) / m_dFocalLength);
150 const double dHalfDetLen = 2 * m_dFocalLength * tan (dAngle);
152 m_detLen = dHalfDetLen * 2;
153 m_detInc = m_detLen / m_nDet;
154 double dDetectorArrayEndOffset = 0;
155 if (m_nDet % 2 == 0) { // Adjust for Even number of detectors
156 m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
157 dDetectorArrayEndOffset = m_detInc;
160 m_dFanBeamAngle = dAngle * 2;
161 m_initPos.angle = 0.0;
162 m_initPos.xs1 = m_dXCenter;
163 m_initPos.ys1 = m_dYCenter + m_dFocalLength;
164 m_initPos.xs2 = m_dXCenter;
165 m_initPos.ys2 = m_dYCenter + m_dFocalLength;
166 m_initPos.xd1 = m_dXCenter - dHalfDetLen;
167 m_initPos.yd1 = m_dYCenter - m_dFocalLength;
168 m_initPos.xd2 = m_dXCenter + dHalfDetLen + dDetectorArrayEndOffset;
169 m_initPos.yd2 = m_dYCenter - m_dFocalLength;
170 m_initPos.angle = 0.0;
171 } else if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
172 if (m_dScanDiameter / 2 > m_dFocalLength) {
174 m_failMessage = "Invalid geometry: Focal length must be larger than scan length";
177 const double dAngle = asin ((m_dScanDiameter / 2) / m_dFocalLength);
179 m_detLen = 2 * dAngle;
180 m_detInc = m_detLen / m_nDet;
181 double dDetectorArrayEndOffset = 0;
182 if (m_nDet % 2 == 0) { // Adjust for Even number of detectors
183 m_detInc = m_detLen / (m_nDet - 1); // center detector = (nDet/2)
184 dDetectorArrayEndOffset = m_detInc;
186 m_dAngularDetIncrement = 2 * m_detInc; // Angular Position 2x gamma angle
187 m_dAngularDetLen = 2 * m_detLen + 2 * dDetectorArrayEndOffset;
188 m_initPos.dAngularDet = -m_dAngularDetLen / 2;
190 m_dFanBeamAngle = dAngle * 2;
192 m_initPos.xs1 = m_dXCenter;
193 m_initPos.ys1 = m_dYCenter + m_dFocalLength;;
194 m_initPos.xs2 = m_dXCenter;
195 m_initPos.ys2 = m_dYCenter + m_dFocalLength;
198 // Calculate incrementatal rotation matrix
200 xlat_mtx2 (m_rotmtxIncrement, -m_dXCenter, -m_dYCenter);
201 rot_mtx2 (temp, m_rotInc);
202 mult_mtx2 (m_rotmtxIncrement, temp, m_rotmtxIncrement);
203 xlat_mtx2 (temp, m_dXCenter, m_dYCenter);
204 mult_mtx2 (m_rotmtxIncrement, temp, m_rotmtxIncrement);
208 Scanner::~Scanner (void)
214 Scanner::convertGeometryIDToName (const int geomID)
216 const char *name = "";
218 if (geomID >= 0 && geomID < s_iGeometryCount)
219 return (s_aszGeometryName[geomID]);
225 Scanner::convertGeometryIDToTitle (const int geomID)
227 const char *title = "";
229 if (geomID >= 0 && geomID < s_iGeometryCount)
230 return (s_aszGeometryName[geomID]);
236 Scanner::convertGeometryNameToID (const char* const geomName)
238 int id = GEOMETRY_INVALID;
240 for (int i = 0; i < s_iGeometryCount; i++)
241 if (strcasecmp (geomName, s_aszGeometryName[i]) == 0) {
251 * collectProjections Calculate projections for a Phantom
254 * collectProjections (proj, phm, start_view, nView, bStoreViewPos, trace)
255 * Projectrions& proj Projection storage
256 * Phantom& phm Phantom for which we collect projections
257 * bool bStoreViewPos TRUE then storage proj at normal view position
258 * int trace Trace level
263 Scanner::collectProjections (Projections& proj, const Phantom& phm, const int trace, SGP* pSGP)
265 collectProjections (proj, phm, 0, proj.nView(), true, trace, pSGP);
269 Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iStartView, const int iNumViews,
270 bool bStoreAtViewPosition, const int trace, SGP* pSGP)
272 int iStorageOffset = (bStoreAtViewPosition ? iStartView : 0);
273 collectProjections (proj, phm, iStartView, iNumViews, iStorageOffset, trace, pSGP);
277 Scanner::collectProjections (Projections& proj, const Phantom& phm, const int iStartView, const int iNumViews,
278 int iStorageOffset, const int trace, SGP* pSGP)
281 double start_angle = iStartView * proj.rotInc();
283 // Calculate initial rotation matrix
284 GRFMTX_2D rotmtx_initial, temp;
285 xlat_mtx2 (rotmtx_initial, -m_dXCenter, -m_dYCenter);
286 rot_mtx2 (temp, start_angle);
287 mult_mtx2 (rotmtx_initial, temp, rotmtx_initial);
288 xlat_mtx2 (temp, m_dXCenter, m_dYCenter);
289 mult_mtx2 (rotmtx_initial, temp, rotmtx_initial);
291 double xd1=0, yd1=0, xd2=0, yd2=0;
292 if (m_idGeometry != GEOMETRY_EQUIANGULAR) {
297 xform_mtx2 (rotmtx_initial, xd1, yd1); // rotate detector endpoints
298 xform_mtx2 (rotmtx_initial, xd2, yd2); // to initial view_angle
301 double xs1 = m_initPos.xs1;
302 double ys1 = m_initPos.ys1;
303 double xs2 = m_initPos.xs2;
304 double ys2 = m_initPos.ys2;
305 xform_mtx2 (rotmtx_initial, xs1, ys1); // rotate source endpoints to
306 xform_mtx2 (rotmtx_initial, xs2, ys2); // initial view angle
310 for (iView = 0, viewAngle = start_angle; iView < iNumViews; iView++, viewAngle += proj.rotInc()) {
311 int iStoragePosition = iView + iStorageOffset;
313 DetectorArray& detArray = proj.getDetectorArray( iStoragePosition );
316 if (pSGP && m_trace >= Trace::TRACE_PHANTOM) {
318 double dWindowSize = dmax (m_detLen, m_dFocalLength * 2) * SQRT2;
319 double dHalfWindowSize = dWindowSize / 2;
320 m_dXMinWin = m_dXCenter - dHalfWindowSize;
321 m_dXMaxWin = m_dXCenter + dHalfWindowSize;
322 m_dYMinWin = m_dYCenter - dHalfWindowSize;
323 m_dYMaxWin = m_dYCenter + dHalfWindowSize;
325 m_pSGP->setWindow (m_dXMinWin, m_dYMinWin, m_dXMaxWin, m_dYMaxWin);
326 m_pSGP->setRasterOp (RO_COPY);
328 m_pSGP->setColor (C_RED);
329 m_pSGP->moveAbs (0., 0.);
330 m_pSGP->drawCircle (m_dViewDiameter / 2);
332 m_pSGP->moveAbs (0., 0.);
333 m_pSGP->setColor (C_GREEN);
334 m_pSGP->drawCircle (m_dFocalLength);
335 m_pSGP->setColor (C_BLUE);
336 m_pSGP->setTextPointSize (9);
338 m_dTextHeight = m_pSGP->getCharHeight ();
340 traceShowParam ("Phantom:", "%s", PROJECTION_TRACE_ROW_PHANT_ID, C_BLACK, phm.name().c_str());
341 traceShowParam ("Geometry:", "%s", PROJECTION_TRACE_ROW_GEOMETRY, C_BLUE, convertGeometryIDToName(m_idGeometry));
342 traceShowParam ("Focal Length Ratio:", "%.2f", PROJECTION_TRACE_ROW_FOCAL_LENGTH, C_BLUE, m_dFocalLengthRatio);
343 // traceShowParam ("Field Of View Ratio:", "%.2f", PROJECTION_TRACE_ROW_FIELD_OF_VIEW, C_BLUE, m_dFieldOfViewRatio);
344 traceShowParam ("Num Detectors:", "%d", PROJECTION_TRACE_ROW_NDET, C_BLUE, proj.nDet());
345 traceShowParam ("Num Views:", "%d", PROJECTION_TRACE_ROW_NVIEW, C_BLUE, proj.nView());
346 traceShowParam ("Samples / Ray:", "%d", PROJECTION_TRACE_ROW_SAMPLES, C_BLUE, m_nSample);
348 m_pSGP->setMarker (SGP::MARK_BDIAMOND, C_LTGREEN);
353 if (m_pSGP && m_trace >= Trace::TRACE_PHANTOM) {
354 m_pSGP->setColor (C_BLACK);
355 m_pSGP->setPenWidth (2);
356 if (m_idGeometry == GEOMETRY_PARALLEL) {
357 m_pSGP->moveAbs (xs1, ys1);
358 m_pSGP->lineAbs (xs2, ys2);
359 m_pSGP->moveAbs (xd1, yd1);
360 m_pSGP->lineAbs (xd2, yd2);
361 } else if (m_idGeometry == GEOMETRY_EQUILINEAR) {
362 m_pSGP->setPenWidth (4);
363 m_pSGP->moveAbs (xs1, ys1);
364 m_pSGP->lineAbs (xs2, ys2);
365 m_pSGP->setPenWidth (2);
366 m_pSGP->moveAbs (xd1, yd1);
367 m_pSGP->lineAbs (xd2, yd2);
368 } else if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
369 m_pSGP->setPenWidth (4);
370 m_pSGP->moveAbs (xs1, ys1);
371 m_pSGP->lineAbs (xs2, ys2);
372 m_pSGP->setPenWidth (2);
373 m_pSGP->moveAbs (0., 0.);
374 m_pSGP->drawArc (m_dFocalLength, viewAngle + 3 * HALFPI - (m_dAngularDetLen/2), viewAngle + 3 * HALFPI + (m_dAngularDetLen/2));
376 m_pSGP->setPenWidth (1);
378 if (m_trace > Trace::TRACE_CONSOLE)
379 traceShowParam ("Current View:", "%d (%.0f%%)", PROJECTION_TRACE_ROW_CURR_VIEW, C_RED, iView + iStartView, (iView + iStartView) / static_cast<double>(m_nView) * 100.);
381 if (m_trace == Trace::TRACE_CONSOLE)
382 std::cout << "Current View: " << iView+iStartView << std::endl;
384 projectSingleView (phm, detArray, xd1, yd1, xd2, yd2, xs1, ys1, xs2, ys2, viewAngle + 3 * HALFPI);
385 detArray.setViewAngle (viewAngle);
388 if (m_pSGP && m_trace >= Trace::TRACE_PHANTOM) {
389 // rs_plot (detArray, xd1, yd1, dXCenter, dYCenter, theta);
392 xform_mtx2 (m_rotmtxIncrement, xs1, ys1);
393 xform_mtx2 (m_rotmtxIncrement, xs2, ys2);
394 if (m_idGeometry != GEOMETRY_EQUIANGULAR) {
395 xform_mtx2 (m_rotmtxIncrement, xd1, yd1); // rotate detector endpoints
396 xform_mtx2 (m_rotmtxIncrement, xd2, yd2);
398 } /* for each iView */
403 * rayview Calculate raysums for a view at any angle
406 * rayview (phm, detArray, xd1, nSample, yd1, xd2, yd2, xs1, ys1, xs2, ys2)
407 * Phantom& phm Phantom to scan
408 * DETARRAY *detArray Storage of values for detector array
409 * Scanner& det Scanner parameters
410 * double xd1, yd1, xd2, yd2 Beginning & ending detector positions
411 * double xs1, ys1, xs2, ys2 Beginning & ending source positions
414 * For each detector, have there are a variable number of rays traced.
415 * The source of each ray is the center of the source x-ray cell. The
416 * detector positions are equally spaced within the cell
418 * The increments between rays are calculated so that the cells start
419 * at the beginning of a detector cell and they end on the endpoint
420 * of the cell. Thus, the last cell starts at (xd2-ddx),(yd2-ddy).
421 * The exception to this is if there is only one ray per detector.
422 * In that case, the detector position is the center of the detector cell.
426 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)
429 double sdx = (xs2 - xs1) / detArray.nDet(); // change in coords
430 double sdy = (ys2 - ys1) / detArray.nDet(); // between source
431 double xs_maj = xs1 + (sdx / 2); // put ray source in center of cell
432 double ys_maj = ys1 + (sdy / 2);
434 double ddx=0, ddy=0, ddx2=0, ddy2=0, ddx2_ofs=0, ddy2_ofs=0, xd_maj=0, yd_maj=0;
435 double dAngleInc=0, dAngleSampleInc=0, dAngleSampleOffset=0, dAngleMajor=0;
436 if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
437 dAngleInc = m_dAngularDetIncrement;
438 dAngleSampleInc = dAngleInc / m_nSample;
439 dAngleSampleOffset = dAngleSampleInc / 2;
440 dAngleMajor = dDetAngle - (m_dAngularDetLen/2) + dAngleSampleOffset;
442 ddx = (xd2 - xd1) / detArray.nDet(); // change in coords
443 ddy = (yd2 - yd1) / detArray.nDet(); // between detectors
444 ddx2 = ddx / m_nSample; // Incr. between rays with detector cell
445 ddy2 = ddy / m_nSample; // Doesn't include detector endpoints
446 ddx2_ofs = ddx2 / 2; // offset of 1st ray from start of detector cell
449 xd_maj = xd1 + ddx2_ofs; // Incr. between detector cells
450 yd_maj = yd1 + ddy2_ofs;
453 DetectorValue* detval = detArray.detValues();
455 if (phm.getComposition() == P_UNIT_PULSE) { // put unit pulse in center of view
456 for (int d = 0; d < detArray.nDet(); d++)
457 if (detArray.nDet() / 2 == d && (d % 2) == 1)
462 for (int d = 0; d < detArray.nDet(); d++) {
465 double xd=0, yd=0, dAngle=0;
466 if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
467 dAngle = dAngleMajor;
473 for (unsigned int i = 0; i < m_nSample; i++) {
474 if (m_idGeometry == GEOMETRY_EQUIANGULAR) {
475 xd = m_dFocalLength * cos (dAngle);
476 yd = m_dFocalLength * sin (dAngle);
480 if (m_pSGP && m_trace >= Trace::TRACE_PROJECTIONS) {
481 m_pSGP->setColor (C_YELLOW);
482 m_pSGP->setRasterOp (RO_AND);
483 m_pSGP->moveAbs (xs, ys);
484 m_pSGP->lineAbs (xd, yd);
488 sum += projectSingleLine (phm, xd, yd, xs, ys);
491 // if (m_trace >= Trace::TRACE_CLIPPING) {
492 // traceShowParam ("Attenuation:", "%s", PROJECTION_TRACE_ROW_ATTEN, C_LTMAGENTA, " ");
493 // traceShowParam ("Attenuation:", "%.3f", PROJECTION_TRACE_ROW_ATTEN, C_LTMAGENTA, sum);
496 if (m_idGeometry == GEOMETRY_EQUIANGULAR)
497 dAngle += dAngleSampleInc;
502 } // for each sample in detector
504 detval[d] = sum / m_nSample;
507 if (m_idGeometry == GEOMETRY_EQUIANGULAR)
508 dAngleMajor += dAngleInc;
513 } /* for each detector */
514 } /* if not unit pulse */
519 Scanner::traceShowParam (const char *szLabel, const char *fmt, int row, int color, ...)
522 va_start(arg, color);
524 traceShowParamRasterOp (RO_COPY, szLabel, fmt, row, color, arg);
526 traceShowParamRasterOp (0, szLabel, fmt, row, color, arg);
532 Scanner::traceShowParamXOR (const char *szLabel, const char *fmt, int row, int color, ...)
535 va_start(arg, color);
537 traceShowParamRasterOp (RO_XOR, szLabel, fmt, row, color, arg);
539 traceShowParamRasterOp (0, szLabel, fmt, row, color, arg);
545 Scanner::traceShowParamRasterOp (int iRasterOp, const char *szLabel, const char *fmt, int row, int color, va_list args)
549 vsnprintf (szValue, sizeof(szValue), fmt, args);
553 m_pSGP->setRasterOp (iRasterOp);
554 m_pSGP->setTextColor (color, -1);
555 double dValueOffset = (m_dXMaxWin - m_dXMinWin) / 4;
557 double dYPos = m_dYMaxWin - (row * m_dTextHeight);
558 double dXPos = m_dXMinWin;
559 m_pSGP->moveAbs (dXPos, dYPos);
560 m_pSGP->drawText (szLabel);
561 m_pSGP->moveAbs (dXPos + dValueOffset, dYPos);
562 m_pSGP->drawText (szValue);
565 double dYPos = m_dYMaxWin - (row * m_dTextHeight);
566 double dXPos = m_dXMinWin + (m_dXMaxWin - m_dXMinWin) * 0.5;
567 m_pSGP->moveAbs (dXPos, dYPos);
568 m_pSGP->drawText (szLabel);
569 m_pSGP->moveAbs (dXPos + dValueOffset, dYPos);
570 m_pSGP->drawText (szValue);
575 cio_put_str (szLabel);
576 cio_put_str (szValue);
584 * projectSingleLine INTERNAL: Calculates raysum along a line for a Phantom
587 * rsum = phm_ray_attenuation (phm, x1, y1, x2, y2)
588 * double rsum Ray sum of Phantom along given line
589 * Phantom& phm; Phantom from which to calculate raysum
590 * double *x1, *y1, *x2, y2 Endpoints of ray path (in Phantom coords)
594 Scanner::projectSingleLine (const Phantom& phm, const double x1, const double y1, const double x2, const double y2)
596 // check ray against each pelem in Phantom
598 for (PElemConstIterator i = phm.listPElem().begin(); i != phm.listPElem().end(); i++)
599 rsum += projectLineAgainstPElem (**i, x1, y1, x2, y2);
606 * pelem_ray_attenuation Calculate raysum of an pelem along one line
609 * rsum = pelem_ray_attenuation (pelem, x1, y1, x2, y2)
610 * double rsum Computed raysum
611 * PhantomElement& pelem Pelem to scan
612 * double x1, y1, x2, y2 Endpoints of raysum line
616 Scanner::projectLineAgainstPElem (const PhantomElement& pelem, double x1, double y1, double x2, double y2)
618 if (! pelem.clipLineWorldCoords (x1, y1, x2, y2)) {
619 if (m_trace == Trace::TRACE_CLIPPING)
620 cio_tone (1000., 0.05);
625 if (m_pSGP && m_trace == Trace::TRACE_CLIPPING) {
626 m_pSGP->setRasterOp (RO_XOR);
627 m_pSGP->moveAbs (x1, y1);
628 m_pSGP->lineAbs (x2, y2);
629 cio_tone (8000., 0.05);
630 m_pSGP->moveAbs (x1, y1);
631 m_pSGP->lineAbs (x2, y2);
632 m_pSGP->setRasterOp (RO_SET);
636 double len = lineLength (x1, y1, x2, y2);
637 return (len * pelem.atten());