1 /*****************************************************************************
4 ** Name: projections.cpp Projection data classes
5 ** Programmer: Kevin Rosenberg
6 ** Date Started: Aug 84
8 ** This is part of the CTSim program
9 ** Copyright (c) 1983-2001 Kevin Rosenberg
11 ** $Id: projections.cpp,v 1.72 2001/03/29 21:25:49 kevin Exp $
13 ** This program is free software; you can redistribute it and/or modify
14 ** it under the terms of the GNU General Public License (version 2) as
15 ** published by the Free Software Foundation.
17 ** This program is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ** GNU General Public License for more details.
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 ******************************************************************************/
29 const kuint16 Projections::m_signature = ('P'*256 + 'J');
31 const int Projections::POLAR_INTERP_INVALID = -1;
32 const int Projections::POLAR_INTERP_NEAREST = 0;
33 const int Projections::POLAR_INTERP_BILINEAR = 1;
34 const int Projections::POLAR_INTERP_BICUBIC = 2;
36 const char* const Projections::s_aszInterpName[] =
43 const char* const Projections::s_aszInterpTitle[] =
50 const int Projections::s_iInterpCount = sizeof(s_aszInterpName) / sizeof(char*);
55 * Projections Constructor for projections matrix storage
58 * proj = projections_create (filename, nView, nDet)
59 * Projections& proj Allocated projections structure & matrix
60 * int nView Number of rotated view
61 * int nDet Number of detectors
65 Projections::Projections (const Scanner& scanner)
68 initFromScanner (scanner);
72 Projections::Projections (const int nView, const int nDet)
78 Projections::Projections (void)
84 Projections::~Projections (void)
90 Projections::convertInterpNameToID (const char* const interpName)
92 int interpID = POLAR_INTERP_INVALID;
94 for (int i = 0; i < s_iInterpCount; i++)
95 if (strcasecmp (interpName, s_aszInterpName[i]) == 0) {
104 Projections::convertInterpIDToName (const int interpID)
106 static const char *interpName = "";
108 if (interpID >= 0 && interpID < s_iInterpCount)
109 return (s_aszInterpName[interpID]);
115 Projections::convertInterpIDToTitle (const int interpID)
117 static const char *interpTitle = "";
119 if (interpID >= 0 && interpID < s_iInterpCount)
120 return (s_aszInterpTitle[interpID]);
122 return (interpTitle);
128 Projections::init (const int nView, const int nDet)
130 m_label.setLabelType (Array2dFileLabel::L_HISTORY);
135 time_t t = time (NULL);
136 tm* lt = localtime (&t);
137 m_year = lt->tm_year;
138 m_month = lt->tm_mon;
140 m_hour = lt->tm_hour;
141 m_minute = lt->tm_min;
142 m_second = lt->tm_sec;
146 Projections::initFromScanner (const Scanner& scanner)
148 m_label.setLabelType (Array2dFileLabel::L_HISTORY);
150 init (scanner.nView(), scanner.nDet());
152 m_rotInc = scanner.rotInc();
153 m_detInc = scanner.detInc();
154 m_detStart = scanner.detStart();
155 m_geometry = scanner.geometry();
156 m_dFocalLength = scanner.focalLength();
157 m_dSourceDetectorLength = scanner.sourceDetectorLength();
158 m_dViewDiameter = scanner.viewDiameter();
160 m_dFanBeamAngle = scanner.fanBeamAngle();
164 Projections::setNView (int nView) // used by MPI to reduce # of views
167 init (nView, m_nDet);
174 Projections::newProjData (void)
177 sys_error(ERR_WARNING, "m_projData != NULL [newProjData]");
179 if (m_nView > 0 && m_nDet) {
180 m_projData = new DetectorArray* [m_nView];
182 for (int i = 0; i < m_nView; i++)
183 m_projData[i] = new DetectorArray (m_nDet);
189 * projections_free Free memory allocated to projections
192 * projections_free(proj)
193 * Projections& proj Projectionss to be deallocated
197 Projections::deleteProjData (void)
199 if (m_projData != NULL) {
200 for (int i = 0; i < m_nView; i++)
201 delete m_projData[i];
210 * Projections::headerWwrite Write data header for projections file
215 Projections::headerWrite (fnetorderstream& fs)
217 kuint16 _hsize = m_headerSize;
218 kuint16 _signature = m_signature;
219 kuint32 _nView = m_nView;
220 kuint32 _nDet = m_nDet;
221 kuint32 _geom = m_geometry;
222 kuint16 _remarksize = m_remark.length();
223 kuint16 _year = m_year;
224 kuint16 _month = m_month;
225 kuint16 _day = m_day;
226 kuint16 _hour = m_hour;
227 kuint16 _minute = m_minute;
228 kuint16 _second = m_second;
230 kfloat64 _calcTime = m_calcTime;
231 kfloat64 _rotStart = m_rotStart;
232 kfloat64 _rotInc = m_rotInc;
233 kfloat64 _detStart = m_detStart;
234 kfloat64 _detInc = m_detInc;
235 kfloat64 _viewDiameter = m_dViewDiameter;
236 kfloat64 _focalLength = m_dFocalLength;
237 kfloat64 _sourceDetectorLength = m_dSourceDetectorLength;
238 kfloat64 _fanBeamAngle = m_dFanBeamAngle;
244 fs.writeInt16 (_hsize);
245 fs.writeInt16 (_signature);
246 fs.writeInt32 (_nView);
247 fs.writeInt32 (_nDet);
248 fs.writeInt32 (_geom);
249 fs.writeFloat64 (_calcTime);
250 fs.writeFloat64 (_rotStart);
251 fs.writeFloat64 (_rotInc);
252 fs.writeFloat64 (_detStart);
253 fs.writeFloat64 (_detInc);
254 fs.writeFloat64 (_viewDiameter);
255 fs.writeFloat64 (_focalLength);
256 fs.writeFloat64 (_sourceDetectorLength);
257 fs.writeFloat64 (_fanBeamAngle);
258 fs.writeInt16 (_year);
259 fs.writeInt16 (_month);
260 fs.writeInt16 (_day);
261 fs.writeInt16 (_hour);
262 fs.writeInt16 (_minute);
263 fs.writeInt16 (_second);
264 fs.writeInt16 (_remarksize);
265 fs.write (m_remark.c_str(), _remarksize);
267 m_headerSize = fs.tellp();
268 _hsize = m_headerSize;
270 fs.writeInt16 (_hsize);
278 * projections_read_header Read data header for projections file
282 Projections::headerRead (fnetorderstream& fs)
284 kuint16 _hsize, _signature, _year, _month, _day, _hour, _minute, _second, _remarksize = 0;
285 kuint32 _nView, _nDet, _geom;
286 kfloat64 _calcTime, _rotStart, _rotInc, _detStart, _detInc, _focalLength, _sourceDetectorLength, _viewDiameter, _fanBeamAngle;
292 fs.readInt16 (_hsize);
293 fs.readInt16 (_signature);
294 fs.readInt32 (_nView);
295 fs.readInt32 (_nDet);
296 fs.readInt32 (_geom);
297 fs.readFloat64 (_calcTime);
298 fs.readFloat64 (_rotStart);
299 fs.readFloat64 (_rotInc);
300 fs.readFloat64 (_detStart);
301 fs.readFloat64 (_detInc);
302 fs.readFloat64 (_viewDiameter);
303 fs.readFloat64 (_focalLength);
304 fs.readFloat64 (_sourceDetectorLength);
305 fs.readFloat64 (_fanBeamAngle);
306 fs.readInt16 (_year);
307 fs.readInt16 (_month);
309 fs.readInt16 (_hour);
310 fs.readInt16 (_minute);
311 fs.readInt16 (_second);
312 fs.readInt16 (_remarksize);
315 sys_error (ERR_SEVERE, "Error reading header information , _remarksize=%d [projections_read_header]", _remarksize);
319 if (_signature != m_signature) {
320 sys_error (ERR_SEVERE, "File %s does not have a valid projection file signature", m_filename.c_str());
324 char* pszRemarkStorage = new char [_remarksize+1];
325 fs.read (pszRemarkStorage, _remarksize);
327 sys_error (ERR_SEVERE, "Error reading remark, _remarksize = %d", _remarksize);
330 pszRemarkStorage[_remarksize] = 0;
331 m_remark = pszRemarkStorage;
332 delete pszRemarkStorage;
334 off_t _hsizeread = fs.tellg();
335 if (!fs || _hsizeread != _hsize) {
336 sys_error (ERR_WARNING, "File header size read %ld != file header size stored %ld [read_projections_header]\n_remarksize=%ld", (long int) _hsizeread, _hsize, _remarksize);
340 m_headerSize = _hsize;
344 m_calcTime = _calcTime;
345 m_rotStart = _rotStart;
347 m_detStart = _detStart;
349 m_dFocalLength = _focalLength;
350 m_dSourceDetectorLength = _sourceDetectorLength;
351 m_dViewDiameter = _viewDiameter;
352 m_dFanBeamAngle = _fanBeamAngle;
360 m_label.setLabelType (Array2dFileLabel::L_HISTORY);
361 m_label.setLabelString (m_remark);
362 m_label.setCalcTime (m_calcTime);
363 m_label.setDateTime (m_year, m_month, m_day, m_hour, m_minute, m_second);
369 Projections::read (const std::string& filename)
371 return read (filename.c_str());
376 Projections::read (const char* filename)
378 m_filename = filename;
380 frnetorderstream fileRead (m_filename.c_str(), std::ios::in | std::ios::binary);
382 frnetorderstream fileRead (m_filename.c_str(), std::ios::in | std::ios::binary | std::ios::nocreate);
388 if (! headerRead (fileRead))
394 for (int i = 0; i < m_nView; i++) {
395 if (! detarrayRead (fileRead, *m_projData[i], i))
405 Projections::copyViewData (const std::string& filename, std::ostream& os, int startView, int endView)
407 return copyViewData (filename.c_str(), os, startView, endView);
411 Projections::copyViewData (const char* const filename, std::ostream& os, int startView, int endView)
413 frnetorderstream is (filename, std::ios::in | std::ios::binary);
414 kuint16 sizeHeader, signature;
415 kuint32 _nView, _nDet;
419 sys_error (ERR_SEVERE, "Unable to read projection file %s", filename);
423 is.readInt16 (sizeHeader);
424 is.readInt16 (signature);
425 is.readInt32 (_nView);
426 is.readInt32 (_nDet);
430 if (signature != m_signature) {
431 sys_error (ERR_SEVERE, "Illegal signature in projection file %s", filename);
437 if (startView > nView - 1)
439 if (endView < 0 || endView > nView - 1)
442 if (startView > endView) { // swap if start > end
443 int tempView = endView;
445 startView = tempView;
448 int sizeView = 8 /* view_angle */ + 4 /* nDet */ + (4 * nDet);
449 unsigned char* pViewData = new unsigned char [sizeView];
451 for (int i = startView; i <= endView; i++) {
452 is.seekg (sizeHeader + i * sizeView);
453 is.read (reinterpret_cast<char*>(pViewData), sizeView);
454 os.write (reinterpret_cast<char*>(pViewData), sizeView);
455 if (is.fail() || os.fail())
461 sys_error (ERR_SEVERE, "Error reading projection file");
463 sys_error (ERR_SEVERE, "Error writing projection file");
465 return (! (is.fail() | os.fail()));
469 Projections::copyHeader (const std::string& filename, std::ostream& os)
471 return copyHeader (filename.c_str(), os);
475 Projections::copyHeader (const char* const filename, std::ostream& os)
477 frnetorderstream is (filename, std::ios::in | std::ios::binary);
478 kuint16 sizeHeader, signature;
479 is.readInt16 (sizeHeader);
480 is.readInt16 (signature);
482 if (signature != m_signature) {
483 sys_error (ERR_SEVERE, "Illegal signature in projection file %s", filename);
487 unsigned char* pHdrData = new unsigned char [sizeHeader];
488 is.read (reinterpret_cast<char*>(pHdrData), sizeHeader);
490 sys_error (ERR_SEVERE, "Error reading header");
494 os.write (reinterpret_cast<char*>(pHdrData), sizeHeader);
496 sys_error (ERR_SEVERE, "Error writing header");
504 Projections::write (const std::string& filename)
506 return write (filename.c_str());
510 Projections::write (const char* filename)
512 frnetorderstream fs (filename, std::ios::out | std::ios::binary | std::ios::trunc | std::ios::ate);
513 m_filename = filename;
515 sys_error (ERR_SEVERE, "Error opening file %s for output [projections_create]", filename);
519 if (! headerWrite (fs))
522 if (m_projData != NULL) {
523 for (int i = 0; i < m_nView; i++) {
524 if (! detarrayWrite (fs, *m_projData[i], i))
537 * detarrayRead Read a Detector Array structure from the disk
540 * detarrayRead (proj, darray, view_num)
541 * DETARRAY *darray Detector array storage location to be filled
542 * int view_num View number to read
546 Projections::detarrayRead (fnetorderstream& fs, DetectorArray& darray, const int iview)
548 const int detval_bytes = darray.nDet() * sizeof(kfloat32);
549 const int detheader_bytes = sizeof(kfloat64) /* view_angle */ + sizeof(kint32) /* nDet */;
550 const int view_bytes = detheader_bytes + detval_bytes;
551 const off_t start_data = m_headerSize + (iview * view_bytes);
552 DetectorValue* detval_ptr = darray.detValues();
556 fs.seekg (start_data);
558 fs.readFloat64 (view_angle);
560 darray.setViewAngle (view_angle);
561 // darray.setNDet ( nDet);
563 for (unsigned int i = 0; i < nDet; i++) {
565 fs.readFloat32 (detval);
566 detval_ptr[i] = detval;
576 * detarrayWrite Write detector array data to the disk
579 * detarrayWrite (darray, view_num)
580 * DETARRAY *darray Detector array data to be written
581 * int view_num View number to write
584 * This routine writes the detarray data from the disk sequentially to
585 * the file that was opened with open_projections(). Data is written in
590 Projections::detarrayWrite (fnetorderstream& fs, const DetectorArray& darray, const int iview)
592 const int detval_bytes = darray.nDet() * sizeof(float);
593 const int detheader_bytes = sizeof(kfloat64) /* view_angle */ + sizeof(kint32) /* nDet */;
594 const int view_bytes = detheader_bytes + detval_bytes;
595 const off_t start_data = m_headerSize + (iview * view_bytes);
596 const DetectorValue* const detval_ptr = darray.detValues();
597 kfloat64 view_angle = darray.viewAngle();
598 kuint32 nDet = darray.nDet();
600 fs.seekp (start_data);
602 sys_error (ERR_SEVERE, "Error seeking detectory array [detarrayWrite]");
606 fs.writeFloat64 (view_angle);
607 fs.writeInt32 (nDet);
609 for (unsigned int i = 0; i < nDet; i++) {
610 kfloat32 detval = detval_ptr[i];
611 fs.writeFloat32 (detval);
621 * printProjectionData Print projections data
624 * printProjectionData ()
628 Projections::printProjectionData ()
630 printProjectionData (0, nView() - 1);
634 Projections::printProjectionData (int startView, int endView)
636 printf("Projections Data\n\n");
637 printf("Description: %s\n", m_remark.c_str());
638 printf("Geometry: %s\n", Scanner::convertGeometryIDToName (m_geometry));
639 printf("nView = %8d nDet = %8d\n", m_nView, m_nDet);
640 printf("focalLength = %8.4f ViewDiameter = %8.4f\n", m_dFocalLength, m_dViewDiameter);
641 printf("fanBeamAngle= %8.4f SourceDetector = %8.4f\n", convertRadiansToDegrees(m_dFanBeamAngle), m_dSourceDetectorLength);
642 printf("rotStart = %8.4f rotInc = %8.4f\n", m_rotStart, m_rotInc);
643 printf("detStart = %8.4f detInc = %8.4f\n", m_detStart, m_detInc);
644 if (m_projData != NULL) {
648 endView = m_nView - 1;
649 if (startView > m_nView - 1)
650 startView = m_nView - 1;
651 if (endView > m_nView - 1)
652 endView = m_nView - 1;
653 for (int ir = startView; ir <= endView - 1; ir++) {
654 printf("View %d: angle %f\n", ir, m_projData[ir]->viewAngle());
655 DetectorValue* detval = m_projData[ir]->detValues();
656 for (int id = 0; id < m_projData[ir]->nDet(); id++)
657 printf("%8.4f ", detval[id]);
664 Projections::printScanInfo (std::ostringstream& os) const
666 os << "Number of detectors: " << m_nDet << "\n";
667 os << "Number of views: " << m_nView<< "\n";
668 os << "Description: " << m_remark.c_str()<< "\n";
669 os << "Geometry: " << Scanner::convertGeometryIDToName (m_geometry)<< "\n";
670 os << "Focal Length: " << m_dFocalLength<< "\n";
671 os << "Source Detector Length: " << m_dSourceDetectorLength << "\n";
672 os << "View Diameter: " << m_dViewDiameter<< "\n";
673 os << "Fan Beam Angle: " << convertRadiansToDegrees(m_dFanBeamAngle) << "\n";
674 os << "detStart: " << m_detStart<< "\n";
675 os << "detInc: " << m_detInc<< "\n";
676 os << "rotStart: " << m_rotStart<< "\n";
677 os << "rotInc: " << m_rotInc<< "\n";
682 Projections::convertPolar (ImageFile& rIF, int iInterpolationID)
684 unsigned int nx = rIF.nx();
685 unsigned int ny = rIF.ny();
686 ImageFileArray v = rIF.getArray();
687 ImageFileArray vImag = rIF.getImaginaryArray();
689 if (! v || nx == 0 || ny == 0)
692 Projections* pProj = this;
693 if (m_geometry == Scanner::GEOMETRY_EQUIANGULAR || m_geometry == Scanner::GEOMETRY_EQUILINEAR)
694 pProj = interpolateToParallel();
696 Array2d<double> adView (nx, ny);
697 Array2d<double> adDet (nx, ny);
698 double** ppdView = adView.getArray();
699 double** ppdDet = adDet.getArray();
701 std::complex<double>** ppcDetValue = new std::complex<double>* [pProj->m_nView];
703 for (iView = 0; iView < pProj->m_nView; iView++) {
704 ppcDetValue[iView] = new std::complex<double> [pProj->m_nDet];
705 DetectorValue* detval = pProj->getDetectorArray (iView).detValues();
706 for (unsigned int iDet = 0; iDet < pProj->m_nDet; iDet++)
707 ppcDetValue[iView][iDet] = std::complex<double>(detval[iDet], 0);
710 pProj->calcArrayPolarCoordinates (nx, ny, ppdView, ppdDet, pProj->m_nDet, 1., pProj->m_detInc);
712 pProj->interpolatePolar (v, vImag, nx, ny, ppcDetValue, ppdView, ppdDet, pProj->m_nView, pProj->m_nDet,
713 pProj->m_nDet, iInterpolationID);
715 for (iView = 0; iView < pProj->m_nView; iView++)
716 delete [] ppcDetValue[iView];
717 delete [] ppcDetValue;
719 if (m_geometry == Scanner::GEOMETRY_EQUIANGULAR || m_geometry == Scanner::GEOMETRY_EQUILINEAR)
727 Projections::convertFFTPolar (ImageFile& rIF, int iInterpolationID, int iZeropad)
730 rIF.arrayDataClear();
733 unsigned int nx = rIF.nx();
734 unsigned int ny = rIF.ny();
735 ImageFileArray v = rIF.getArray();
736 if (! rIF.isComplex())
737 rIF.convertRealToComplex();
738 ImageFileArray vImag = rIF.getImaginaryArray();
740 if (! v || nx == 0 || ny == 0)
743 if (m_geometry != Scanner::GEOMETRY_PARALLEL) {
744 sys_error (ERR_WARNING, "convertFFTPolar supports Parallel only");
749 int iNumInterpDetWithZeros = ProcessSignal::addZeropadFactor (iInterpDet, iZeropad);
751 double dZeropadRatio = static_cast<double>(iNumInterpDetWithZeros) / static_cast<double>(iInterpDet);
753 fftw_plan plan = fftw_create_plan (iNumInterpDetWithZeros, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
755 fftw_complex* pcIn = new fftw_complex [iNumInterpDetWithZeros];
756 std::complex<double>** ppcDetValue = new std::complex<double>* [m_nView];
757 double dInterpScale = (m_nDet-1) / static_cast<double>(iInterpDet-1) / SQRT2;
759 double dFFTScale = 1. / static_cast<double>(iInterpDet * iInterpDet);
760 int iMidPoint = iInterpDet / 2;
761 double dMidPoint = static_cast<double>(iInterpDet) / 2.;
762 int iZerosAdded = iNumInterpDetWithZeros - iInterpDet;
763 for (unsigned int iView = 0; iView < m_nView; iView++) {
764 DetectorValue* detval = getDetectorArray(iView).detValues();
765 LinearInterpolator<DetectorValue> projInterp (detval, m_nDet);
766 for (unsigned int iDet = 0; iDet < iInterpDet; iDet++) {
767 double dInterpPos = (m_nDet / 2.) + (iDet - dMidPoint) * dInterpScale;
768 pcIn[iDet].re = projInterp.interpolate (dInterpPos) * dInterpScale;
771 Fourier::shuffleFourierToNaturalOrder (pcIn, iInterpDet);
772 if (iZerosAdded > 0) {
773 for (unsigned int iDet1 = iMidPoint; iDet1 < iInterpDet; iDet1++)
774 pcIn[iDet1+iZerosAdded] = pcIn[iDet1];
775 for (unsigned int iDet2 = iMidPoint; iDet2 < iMidPoint + iZerosAdded; iDet2++)
776 pcIn[iDet2].re = pcIn[iDet2].im = 0;
779 fftw_one (plan, pcIn, NULL);
781 ppcDetValue[iView] = new std::complex<double> [iNumInterpDetWithZeros];
782 for (unsigned int iD = 0; iD < iNumInterpDetWithZeros; iD++) {
783 ppcDetValue[iView][iD] = std::complex<double> (pcIn[iD].re * dFFTScale, pcIn[iD].im * dFFTScale);
786 Fourier::shuffleFourierToNaturalOrder (ppcDetValue[iView], iNumInterpDetWithZeros);
790 fftw_destroy_plan (plan);
792 Array2d<double> adView (nx, ny);
793 Array2d<double> adDet (nx, ny);
794 double** ppdView = adView.getArray();
795 double** ppdDet = adDet.getArray();
796 calcArrayPolarCoordinates (nx, ny, ppdView, ppdDet, iNumInterpDetWithZeros, dZeropadRatio,
797 m_detInc * dInterpScale);
799 interpolatePolar (v, vImag, nx, ny, ppcDetValue, ppdView, ppdDet, m_nView, m_nDet, iNumInterpDetWithZeros,
802 for (int i = 0; i < m_nView; i++)
803 delete [] ppcDetValue[i];
804 delete [] ppcDetValue;
812 Projections::calcArrayPolarCoordinates (unsigned int nx, unsigned int ny, double** ppdView, double** ppdDet,
813 int iNumDetWithZeros, double dZeropadRatio, double dDetInc)
815 // double dLength = viewDiameter();
816 double dLength = phmLen();
817 double xMin = -dLength / 2;
818 double xMax = xMin + dLength;
819 double yMin = -dLength / 2;
820 double yMax = yMin + dLength;
821 double xCent = (xMin + xMax) / 2;
822 double yCent = (yMin + yMax) / 2;
824 xMin = (xMin - xCent) * dZeropadRatio + xCent;
825 xMax = (xMax - xCent) * dZeropadRatio + xCent;
826 yMin = (yMin - yCent) * dZeropadRatio + yCent;
827 yMax = (yMax - yCent) * dZeropadRatio + yCent;
829 double xInc = (xMax - xMin) / nx; // size of cells
830 double yInc = (yMax - yMin) / ny;
832 // +1 is correct for frequency data, ndet-1 is correct for projections
833 int iDetCenter = (iNumDetWithZeros - 1) / 2; // index refering to L=0 projection
834 if (isEven (iNumDetWithZeros))
835 iDetCenter = (iNumDetWithZeros + 1) / 2;
837 // Calculates polar coordinates (view#, det#) for each point on phantom grid
838 double x = xMin + xInc / 2; // Rectang coords of center of pixel
839 for (unsigned int ix = 0; ix < nx; x += xInc, ix++) {
840 double y = yMin + yInc / 2;
841 for (unsigned int iy = 0; iy < ny; y += yInc, iy++) {
842 double r = ::sqrt (x * x + y * y);
843 double phi = atan2 (y, x);
852 ppdView[ix][iy] = (phi - m_rotStart) / m_rotInc;
853 ppdDet[ix][iy] = (r / dDetInc) + iDetCenter;
859 Projections::interpolatePolar (ImageFileArray& v, ImageFileArray& vImag,
860 unsigned int nx, unsigned int ny, std::complex<double>** ppcDetValue, double** ppdView,
861 double** ppdDet, unsigned int nView, unsigned int nDet, unsigned int nDetWithZeros, int iInterpolationID)
863 typedef std::complex<double> complexValue;
865 BilinearInterpolator<complexValue>* pBilinear;
866 if (iInterpolationID == POLAR_INTERP_BILINEAR)
867 pBilinear = new BilinearInterpolator<complexValue> (ppcDetValue, nView, nDetWithZeros);
869 BicubicPolyInterpolator<complexValue>* pBicubic;
870 if (iInterpolationID == POLAR_INTERP_BICUBIC)
871 pBicubic = new BicubicPolyInterpolator<complexValue> (ppcDetValue, nView, nDetWithZeros);
873 for (unsigned int ix = 0; ix < ny; ix++) {
874 for (unsigned int iy = 0; iy < ny; iy++) {
876 if (iInterpolationID == POLAR_INTERP_NEAREST) {
877 unsigned int iView = nearest<int> (ppdView[ix][iy]);
878 unsigned int iDet = nearest<int> (ppdDet[ix][iy]);
879 if (iView == nView) {
881 iDet = m_nDet - iDet;
883 if (iDet >= 0 && iDet < nDetWithZeros && iView >= 0 && iView < nView) {
884 v[ix][iy] = ppcDetValue[iView][iDet].real();
886 vImag[ix][iy] = ppcDetValue[iView][iDet].imag();
890 } else if (iInterpolationID == POLAR_INTERP_BILINEAR) {
891 std::complex<double> vInterp = pBilinear->interpolate (ppdView[ix][iy], ppdDet[ix][iy]);
892 v[ix][iy] = vInterp.real();
894 vImag[ix][iy] = vInterp.imag();
895 } else if (iInterpolationID == POLAR_INTERP_BICUBIC) {
896 std::complex<double> vInterp = pBicubic->interpolate (ppdView[ix][iy], ppdDet[ix][iy]);
897 v[ix][iy] = vInterp.real();
899 vImag[ix][iy] = vInterp.imag();
906 Projections::initFromSomatomAR_STAR (int iNViews, int iNDets, unsigned char* pData, unsigned long lDataLength)
908 init (iNViews, iNDets);
909 m_geometry = Scanner::GEOMETRY_EQUIANGULAR;
910 m_dFocalLength = 510;
911 m_dSourceDetectorLength = 890;
912 m_detInc = convertDegreesToRadians (3.06976 / 60);
913 m_dFanBeamAngle = iNDets * m_detInc;
914 m_detStart = -(m_dFanBeamAngle / 2);
915 m_rotInc = TWOPI / static_cast<double>(iNViews);
917 m_dViewDiameter = sin (m_dFanBeamAngle / 2) * m_dFocalLength * 2;
919 if (! ((iNViews == 750 && lDataLength == 1560000L) || (iNViews == 950 && lDataLength == 1976000L)
920 || (iNViews == 1500 && lDataLength == 3120000)))
923 double dCenter = (iNDets - 1.) / 2.; // change from (Nm+1)/2 because of 0 vs. 1 indexing
924 double* pdCosScale = new double [iNDets];
925 for (int i = 0; i < iNDets; i++)
926 pdCosScale[i] = 1. / cos ((i - dCenter) * m_detInc);
929 for (int iv = 0; iv < iNViews; iv++) {
930 unsigned char* pArgBase = pData + lDataPos;
931 unsigned char* p = pArgBase+0; SwapBytes4IfLittleEndian (p);
932 long lProjNumber = *reinterpret_cast<long*>(p);
934 p = pArgBase+20; SwapBytes4IfLittleEndian (p);
935 long lEscale = *reinterpret_cast<long*>(p);
937 p = pArgBase+28; SwapBytes4IfLittleEndian (p);
938 long lTime = *reinterpret_cast<long*>(p);
940 p = pArgBase + 4; SwapBytes4IfLittleEndian (p);
941 double dAlpha = *reinterpret_cast<float*>(p) + HALFPI;
943 p = pArgBase+12; SwapBytes4IfLittleEndian (p);
944 double dAlign = *reinterpret_cast<float*>(p);
946 p = pArgBase + 16; SwapBytes4IfLittleEndian (p);
947 double dMaxValue = *reinterpret_cast<float*>(p);
949 DetectorArray& detArray = getDetectorArray (iv);
950 detArray.setViewAngle (dAlpha);
951 DetectorValue* detval = detArray.detValues();
953 double dViewScale = 1. / (2294.4871 * ::pow (2.0, -lEscale));
955 for (int id = 0; id < iNDets; id++) {
956 int iV = pData[lDataPos+1] + (pData[lDataPos] << 8);
957 if (iV > 32767) // two's complement signed conversion
959 detval[id] = iV * dViewScale * pdCosScale[id];
963 for (int k = iNDets - 2; k >= 0; k--)
964 detval[k+1] = detval[k];
974 Projections::interpolateToParallel () const
976 if (m_geometry == Scanner::GEOMETRY_PARALLEL)
977 return const_cast<Projections*>(this);
981 Projections* pProjNew = new Projections (nView, nDet);
982 pProjNew->m_geometry = Scanner::GEOMETRY_PARALLEL;
983 pProjNew->m_dFocalLength = m_dFocalLength;
984 pProjNew->m_dSourceDetectorLength = m_dSourceDetectorLength;
985 pProjNew->m_dViewDiameter = m_dViewDiameter;
986 pProjNew->m_dFanBeamAngle = m_dFanBeamAngle;
987 pProjNew->m_calcTime = 0;
988 pProjNew->m_remark = m_remark;
989 pProjNew->m_remark += "; Interpolate to Parallel";
990 pProjNew->m_label.setLabelType (Array2dFileLabel::L_HISTORY);
991 pProjNew->m_label.setLabelString (pProjNew->m_remark);
992 pProjNew->m_label.setCalcTime (pProjNew->m_calcTime);
993 pProjNew->m_label.setDateTime (pProjNew->m_year, pProjNew->m_month, pProjNew->m_day, pProjNew->m_hour, pProjNew->m_minute, pProjNew->m_second);
995 pProjNew->m_rotStart = 0;
996 #ifdef CONVERT_PARALLEL_PI
997 pProjNew->m_rotInc = PI / nView;;
999 pProjNew->m_rotInc = TWOPI / nView;
1001 pProjNew->m_detStart = -m_dViewDiameter / 2;
1002 pProjNew->m_detInc = m_dViewDiameter / nDet;
1003 if (isEven (nDet)) // even
1004 pProjNew->m_detInc = m_dViewDiameter / (nDet - 1);
1006 ParallelRaysums parallel (this, ParallelRaysums::THETA_RANGE_NORMALIZE_TO_TWOPI);
1008 double* pdThetaValuesForT = new double [pProjNew->nView()];
1009 double* pdRaysumsForT = new double [pProjNew->nView()];
1011 // interpolate to evenly spaced theta (views)
1012 double dDetPos = pProjNew->m_detStart;
1013 for (int iD = 0; iD < pProjNew->nDet(); iD++, dDetPos += pProjNew->m_detInc) {
1014 parallel.getThetaAndRaysumsForT (iD, pdThetaValuesForT, pdRaysumsForT);
1016 double dViewAngle = m_rotStart;
1017 int iLastFloor = -1;
1018 for (int iV = 0; iV < pProjNew->nView(); iV++, dViewAngle += pProjNew->m_rotInc) {
1019 DetectorValue* detValues = pProjNew->getDetectorArray (iV).detValues();
1020 LinearInterpolator<double> interp (pdThetaValuesForT, pdRaysumsForT, pProjNew->nView(), false);
1021 detValues[iD] = interp.interpolate (dViewAngle, &iLastFloor);
1024 delete pdThetaValuesForT;
1025 delete pdRaysumsForT;
1027 // interpolate to evenly space t (detectors)
1028 double* pdOriginalDetPositions = new double [pProjNew->nDet()];
1029 parallel.getDetPositions (pdOriginalDetPositions);
1031 double* pdDetValueCopy = new double [pProjNew->nDet()];
1032 double dViewAngle = m_rotStart;
1033 for (int iV = 0; iV < pProjNew->nView(); iV++, dViewAngle += pProjNew->m_rotInc) {
1034 DetectorArray& detArray = pProjNew->getDetectorArray (iV);
1035 DetectorValue* detValues = detArray.detValues();
1036 detArray.setViewAngle (dViewAngle);
1038 for (int i = 0; i < pProjNew->nDet(); i++)
1039 pdDetValueCopy[i] = detValues[i];
1041 double dDetPos = pProjNew->m_detStart;
1042 int iLastFloor = -1;
1043 LinearInterpolator<double> interp (pdOriginalDetPositions, pdDetValueCopy, pProjNew->nDet(), false);
1044 for (int iD = 0; iD < pProjNew->nDet(); iD++, dDetPos += pProjNew->m_detInc)
1045 detValues[iD] = interp.interpolate (dDetPos, &iLastFloor);
1047 delete pdDetValueCopy;
1048 delete pdOriginalDetPositions;
1054 ///////////////////////////////////////////////////////////////////////////////
1056 // Class ParallelRaysums
1058 // Used for converting divergent beam raysums into Parallel raysums
1060 ///////////////////////////////////////////////////////////////////////////////
1062 ParallelRaysums::ParallelRaysums (const Projections* pProjections, int iThetaRange)
1063 : m_iNumCoordinates(0), m_iNumView(pProjections->nView()), m_iNumDet(pProjections->nDet()),
1064 m_iThetaRange (iThetaRange), m_pCoordinates(NULL)
1066 int iGeometry = pProjections->geometry();
1067 double dDetInc = pProjections->detInc();
1068 double dDetStart = pProjections->detStart();
1069 double dFocalLength = pProjections->focalLength();
1071 m_iNumCoordinates = m_iNumView * m_iNumDet;
1072 m_pCoordinates = new ParallelRaysumCoordinate [m_iNumCoordinates];
1073 m_vecpCoordinates.reserve (m_iNumCoordinates);
1074 for (int i = 0; i < m_iNumCoordinates; i++)
1075 m_vecpCoordinates[i] = m_pCoordinates + i;
1077 int iCoordinate = 0;
1078 for (int iV = 0; iV < m_iNumView; iV++) {
1079 double dViewAngle = pProjections->getDetectorArray(iV).viewAngle();
1080 const DetectorValue* detValues = pProjections->getDetectorArray(iV).detValues();
1082 double dDetPos = dDetStart;
1083 for (int iD = 0; iD < m_iNumDet; iD++) {
1084 ParallelRaysumCoordinate* pC = m_vecpCoordinates[iCoordinate++];
1086 if (iGeometry == Scanner::GEOMETRY_PARALLEL) {
1087 pC->m_dTheta = dViewAngle;
1089 } else if (iGeometry == Scanner::GEOMETRY_EQUILINEAR) {
1090 double dFanAngle = atan (dDetPos / pProjections->sourceDetectorLength());
1091 pC->m_dTheta = dViewAngle + dFanAngle;
1092 pC->m_dT = dFocalLength * sin(dFanAngle);
1094 } else if (iGeometry == Scanner::GEOMETRY_EQUIANGULAR) {
1095 // fan angle is same as dDetPos
1096 pC->m_dTheta = dViewAngle + dDetPos;
1097 pC->m_dT = dFocalLength * sin (dDetPos);
1099 if (m_iThetaRange != THETA_RANGE_UNCONSTRAINED) {
1100 pC->m_dTheta = normalizeAngle (pC->m_dTheta);
1101 if (m_iThetaRange == THETA_RANGE_FOLD_TO_PI && pC->m_dTheta >= PI) {
1103 pC->m_dT = -pC->m_dT;
1106 pC->m_dRaysum = detValues[iD];
1112 ParallelRaysums::~ParallelRaysums()
1114 delete m_pCoordinates;
1117 ParallelRaysums::CoordinateContainer&
1118 ParallelRaysums::getSortedByTheta()
1120 if (m_vecpSortedByTheta.size() == 0) {
1121 m_vecpSortedByTheta.resize (m_iNumCoordinates);
1122 for (int i = 0; i < m_iNumCoordinates; i++)
1123 m_vecpSortedByTheta[i] = m_vecpCoordinates[i];
1124 std::sort (m_vecpSortedByTheta.begin(), m_vecpSortedByTheta.end(), ParallelRaysumCoordinate::compareByTheta);
1127 return m_vecpSortedByTheta;
1130 ParallelRaysums::CoordinateContainer&
1131 ParallelRaysums::getSortedByT()
1133 if (m_vecpSortedByT.size() == 0) {
1134 m_vecpSortedByT.resize (m_iNumCoordinates);
1135 for (int i = 0; i < m_iNumCoordinates; i++)
1136 m_vecpSortedByT[i] = m_vecpCoordinates[i];
1137 std::sort (m_vecpSortedByT.begin(), m_vecpSortedByT.end(), ParallelRaysumCoordinate::compareByT);
1140 return m_vecpSortedByT;
1145 ParallelRaysums::getLimits (double* dMinT, double* dMaxT, double* dMinTheta, double* dMaxTheta) const
1147 if (m_iNumCoordinates <= 0)
1150 *dMinT = *dMaxT = m_vecpCoordinates[0]->m_dT;
1151 *dMinTheta = *dMaxTheta = m_vecpCoordinates[0]->m_dTheta;
1153 for (int i = 0; i < m_iNumCoordinates; i++) {
1154 double dT = m_vecpCoordinates[i]->m_dT;
1155 double dTheta = m_vecpCoordinates[i]->m_dTheta;
1159 else if (dT > *dMaxT)
1162 if (dTheta < *dMinTheta)
1163 *dMinTheta = dTheta;
1164 else if (dTheta > *dMaxTheta)
1165 *dMaxTheta = dTheta;
1170 ParallelRaysums::getThetaAndRaysumsForT (int iTheta, double* pTheta, double* pRaysum)
1172 const CoordinateContainer& coordsT = getSortedByT();
1174 int iBase = iTheta * m_iNumView;
1175 for (int i = 0; i < m_iNumView; i++) {
1176 int iPos = iBase + i;
1177 pTheta[i] = coordsT[iPos]->m_dTheta;
1178 pRaysum[i] = coordsT[iPos]->m_dRaysum;
1183 ParallelRaysums::getDetPositions (double* pdDetPos)
1185 const CoordinateContainer& coordsT = getSortedByT();
1188 for (int i = 0; i < m_iNumDet; i++) {
1189 pdDetPos[i] = coordsT[iPos]->m_dT;