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.68 2001/03/21 21:45:31 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*);
54 * Projections Constructor for projections matrix storage
57 * proj = projections_create (filename, nView, nDet)
58 * Projections& proj Allocated projections structure & matrix
59 * int nView Number of rotated view
60 * int nDet Number of detectors
64 Projections::Projections (const Scanner& scanner)
67 initFromScanner (scanner);
71 Projections::Projections (const int nView, const int nDet)
77 Projections::Projections (void)
83 Projections::~Projections (void)
89 Projections::convertInterpNameToID (const char* const interpName)
91 int interpID = POLAR_INTERP_INVALID;
93 for (int i = 0; i < s_iInterpCount; i++)
94 if (strcasecmp (interpName, s_aszInterpName[i]) == 0) {
103 Projections::convertInterpIDToName (const int interpID)
105 static const char *interpName = "";
107 if (interpID >= 0 && interpID < s_iInterpCount)
108 return (s_aszInterpName[interpID]);
114 Projections::convertInterpIDToTitle (const int interpID)
116 static const char *interpTitle = "";
118 if (interpID >= 0 && interpID < s_iInterpCount)
119 return (s_aszInterpTitle[interpID]);
121 return (interpTitle);
127 Projections::init (const int nView, const int nDet)
129 m_label.setLabelType (Array2dFileLabel::L_HISTORY);
134 time_t t = time (NULL);
135 tm* lt = localtime (&t);
136 m_year = lt->tm_year;
137 m_month = lt->tm_mon;
139 m_hour = lt->tm_hour;
140 m_minute = lt->tm_min;
141 m_second = lt->tm_sec;
145 Projections::initFromScanner (const Scanner& scanner)
147 m_label.setLabelType (Array2dFileLabel::L_HISTORY);
149 init (scanner.nView(), scanner.nDet());
151 m_rotInc = scanner.rotInc();
152 m_detInc = scanner.detInc();
153 m_detStart = scanner.detStart();
154 m_geometry = scanner.geometry();
155 m_dFocalLength = scanner.focalLength();
156 m_dSourceDetectorLength = scanner.sourceDetectorLength();
157 m_dViewDiameter = scanner.viewDiameter();
159 m_dFanBeamAngle = scanner.fanBeamAngle();
163 Projections::setNView (int nView) // used by MPI to reduce # of views
166 init (nView, m_nDet);
173 Projections::newProjData (void)
176 sys_error(ERR_WARNING, "m_projData != NULL [newProjData]");
178 if (m_nView > 0 && m_nDet) {
179 m_projData = new DetectorArray* [m_nView];
181 for (int i = 0; i < m_nView; i++)
182 m_projData[i] = new DetectorArray (m_nDet);
188 * projections_free Free memory allocated to projections
191 * projections_free(proj)
192 * Projections& proj Projectionss to be deallocated
196 Projections::deleteProjData (void)
198 if (m_projData != NULL) {
199 for (int i = 0; i < m_nView; i++)
200 delete m_projData[i];
209 * Projections::headerWwrite Write data header for projections file
214 Projections::headerWrite (fnetorderstream& fs)
216 kuint16 _hsize = m_headerSize;
217 kuint16 _signature = m_signature;
218 kuint32 _nView = m_nView;
219 kuint32 _nDet = m_nDet;
220 kuint32 _geom = m_geometry;
221 kuint16 _remarksize = m_remark.length();
222 kuint16 _year = m_year;
223 kuint16 _month = m_month;
224 kuint16 _day = m_day;
225 kuint16 _hour = m_hour;
226 kuint16 _minute = m_minute;
227 kuint16 _second = m_second;
229 kfloat64 _calcTime = m_calcTime;
230 kfloat64 _rotStart = m_rotStart;
231 kfloat64 _rotInc = m_rotInc;
232 kfloat64 _detStart = m_detStart;
233 kfloat64 _detInc = m_detInc;
234 kfloat64 _viewDiameter = m_dViewDiameter;
235 kfloat64 _focalLength = m_dFocalLength;
236 kfloat64 _sourceDetectorLength = m_dSourceDetectorLength;
237 kfloat64 _fanBeamAngle = m_dFanBeamAngle;
243 fs.writeInt16 (_hsize);
244 fs.writeInt16 (_signature);
245 fs.writeInt32 (_nView);
246 fs.writeInt32 (_nDet);
247 fs.writeInt32 (_geom);
248 fs.writeFloat64 (_calcTime);
249 fs.writeFloat64 (_rotStart);
250 fs.writeFloat64 (_rotInc);
251 fs.writeFloat64 (_detStart);
252 fs.writeFloat64 (_detInc);
253 fs.writeFloat64 (_viewDiameter);
254 fs.writeFloat64 (_focalLength);
255 fs.writeFloat64 (_sourceDetectorLength);
256 fs.writeFloat64 (_fanBeamAngle);
257 fs.writeInt16 (_year);
258 fs.writeInt16 (_month);
259 fs.writeInt16 (_day);
260 fs.writeInt16 (_hour);
261 fs.writeInt16 (_minute);
262 fs.writeInt16 (_second);
263 fs.writeInt16 (_remarksize);
264 fs.write (m_remark.c_str(), _remarksize);
266 m_headerSize = fs.tellp();
267 _hsize = m_headerSize;
269 fs.writeInt16 (_hsize);
277 * projections_read_header Read data header for projections file
281 Projections::headerRead (fnetorderstream& fs)
283 kuint16 _hsize, _signature, _year, _month, _day, _hour, _minute, _second, _remarksize = 0;
284 kuint32 _nView, _nDet, _geom;
285 kfloat64 _calcTime, _rotStart, _rotInc, _detStart, _detInc, _focalLength, _sourceDetectorLength, _viewDiameter, _fanBeamAngle;
291 fs.readInt16 (_hsize);
292 fs.readInt16 (_signature);
293 fs.readInt32 (_nView);
294 fs.readInt32 (_nDet);
295 fs.readInt32 (_geom);
296 fs.readFloat64 (_calcTime);
297 fs.readFloat64 (_rotStart);
298 fs.readFloat64 (_rotInc);
299 fs.readFloat64 (_detStart);
300 fs.readFloat64 (_detInc);
301 fs.readFloat64 (_viewDiameter);
302 fs.readFloat64 (_focalLength);
303 fs.readFloat64 (_sourceDetectorLength);
304 fs.readFloat64 (_fanBeamAngle);
305 fs.readInt16 (_year);
306 fs.readInt16 (_month);
308 fs.readInt16 (_hour);
309 fs.readInt16 (_minute);
310 fs.readInt16 (_second);
311 fs.readInt16 (_remarksize);
314 sys_error (ERR_SEVERE, "Error reading header information , _remarksize=%d [projections_read_header]", _remarksize);
318 if (_signature != m_signature) {
319 sys_error (ERR_SEVERE, "File %s does not have a valid projection file signature", m_filename.c_str());
323 char* pszRemarkStorage = new char [_remarksize+1];
324 fs.read (pszRemarkStorage, _remarksize);
326 sys_error (ERR_SEVERE, "Error reading remark, _remarksize = %d", _remarksize);
329 pszRemarkStorage[_remarksize] = 0;
330 m_remark = pszRemarkStorage;
331 delete pszRemarkStorage;
333 off_t _hsizeread = fs.tellg();
334 if (!fs || _hsizeread != _hsize) {
335 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);
339 m_headerSize = _hsize;
343 m_calcTime = _calcTime;
344 m_rotStart = _rotStart;
346 m_detStart = _detStart;
348 m_dFocalLength = _focalLength;
349 m_dSourceDetectorLength = _sourceDetectorLength;
350 m_dViewDiameter = _viewDiameter;
351 m_dFanBeamAngle = _fanBeamAngle;
359 m_label.setLabelType (Array2dFileLabel::L_HISTORY);
360 m_label.setLabelString (m_remark);
361 m_label.setCalcTime (m_calcTime);
362 m_label.setDateTime (m_year, m_month, m_day, m_hour, m_minute, m_second);
368 Projections::read (const std::string& filename)
370 return read (filename.c_str());
375 Projections::read (const char* filename)
377 m_filename = filename;
379 frnetorderstream fileRead (m_filename.c_str(), std::ios::in | std::ios::binary);
381 frnetorderstream fileRead (m_filename.c_str(), std::ios::in | std::ios::binary | std::ios::nocreate);
387 if (! headerRead (fileRead))
393 for (int i = 0; i < m_nView; i++) {
394 if (! detarrayRead (fileRead, *m_projData[i], i))
404 Projections::copyViewData (const std::string& filename, std::ostream& os, int startView, int endView)
406 return copyViewData (filename.c_str(), os, startView, endView);
410 Projections::copyViewData (const char* const filename, std::ostream& os, int startView, int endView)
412 frnetorderstream is (filename, std::ios::in | std::ios::binary);
413 kuint16 sizeHeader, signature;
414 kuint32 _nView, _nDet;
418 sys_error (ERR_SEVERE, "Unable to read projection file %s", filename);
422 is.readInt16 (sizeHeader);
423 is.readInt16 (signature);
424 is.readInt32 (_nView);
425 is.readInt32 (_nDet);
429 if (signature != m_signature) {
430 sys_error (ERR_SEVERE, "Illegal signature in projection file %s", filename);
436 if (startView > nView - 1)
438 if (endView < 0 || endView > nView - 1)
441 if (startView > endView) { // swap if start > end
442 int tempView = endView;
444 startView = tempView;
447 int sizeView = 8 /* view_angle */ + 4 /* nDet */ + (4 * nDet);
448 unsigned char* pViewData = new unsigned char [sizeView];
450 for (int i = startView; i <= endView; i++) {
451 is.seekg (sizeHeader + i * sizeView);
452 is.read (reinterpret_cast<char*>(pViewData), sizeView);
453 os.write (reinterpret_cast<char*>(pViewData), sizeView);
454 if (is.fail() || os.fail())
460 sys_error (ERR_SEVERE, "Error reading projection file");
462 sys_error (ERR_SEVERE, "Error writing projection file");
464 return (! (is.fail() | os.fail()));
468 Projections::copyHeader (const std::string& filename, std::ostream& os)
470 return copyHeader (filename.c_str(), os);
474 Projections::copyHeader (const char* const filename, std::ostream& os)
476 frnetorderstream is (filename, std::ios::in | std::ios::binary);
477 kuint16 sizeHeader, signature;
478 is.readInt16 (sizeHeader);
479 is.readInt16 (signature);
481 if (signature != m_signature) {
482 sys_error (ERR_SEVERE, "Illegal signature in projection file %s", filename);
486 unsigned char* pHdrData = new unsigned char [sizeHeader];
487 is.read (reinterpret_cast<char*>(pHdrData), sizeHeader);
489 sys_error (ERR_SEVERE, "Error reading header");
493 os.write (reinterpret_cast<char*>(pHdrData), sizeHeader);
495 sys_error (ERR_SEVERE, "Error writing header");
503 Projections::write (const std::string& filename)
505 return write (filename.c_str());
509 Projections::write (const char* filename)
511 frnetorderstream fs (filename, std::ios::out | std::ios::binary | std::ios::trunc | std::ios::ate);
512 m_filename = filename;
514 sys_error (ERR_SEVERE, "Error opening file %s for output [projections_create]", filename);
518 if (! headerWrite (fs))
521 if (m_projData != NULL) {
522 for (int i = 0; i < m_nView; i++) {
523 if (! detarrayWrite (fs, *m_projData[i], i))
536 * detarrayRead Read a Detector Array structure from the disk
539 * detarrayRead (proj, darray, view_num)
540 * DETARRAY *darray Detector array storage location to be filled
541 * int view_num View number to read
545 Projections::detarrayRead (fnetorderstream& fs, DetectorArray& darray, const int iview)
547 const int detval_bytes = darray.nDet() * sizeof(kfloat32);
548 const int detheader_bytes = sizeof(kfloat64) /* view_angle */ + sizeof(kint32) /* nDet */;
549 const int view_bytes = detheader_bytes + detval_bytes;
550 const off_t start_data = m_headerSize + (iview * view_bytes);
551 DetectorValue* detval_ptr = darray.detValues();
555 fs.seekg (start_data);
557 fs.readFloat64 (view_angle);
559 darray.setViewAngle (view_angle);
560 // darray.setNDet ( nDet);
562 for (unsigned int i = 0; i < nDet; i++) {
564 fs.readFloat32 (detval);
565 detval_ptr[i] = detval;
575 * detarrayWrite Write detector array data to the disk
578 * detarrayWrite (darray, view_num)
579 * DETARRAY *darray Detector array data to be written
580 * int view_num View number to write
583 * This routine writes the detarray data from the disk sequentially to
584 * the file that was opened with open_projections(). Data is written in
589 Projections::detarrayWrite (fnetorderstream& fs, const DetectorArray& darray, const int iview)
591 const int detval_bytes = darray.nDet() * sizeof(float);
592 const int detheader_bytes = sizeof(kfloat64) /* view_angle */ + sizeof(kint32) /* nDet */;
593 const int view_bytes = detheader_bytes + detval_bytes;
594 const off_t start_data = m_headerSize + (iview * view_bytes);
595 const DetectorValue* const detval_ptr = darray.detValues();
596 kfloat64 view_angle = darray.viewAngle();
597 kuint32 nDet = darray.nDet();
599 fs.seekp (start_data);
601 sys_error (ERR_SEVERE, "Error seeking detectory array [detarrayWrite]");
605 fs.writeFloat64 (view_angle);
606 fs.writeInt32 (nDet);
608 for (unsigned int i = 0; i < nDet; i++) {
609 kfloat32 detval = detval_ptr[i];
610 fs.writeFloat32 (detval);
620 * printProjectionData Print projections data
623 * printProjectionData ()
627 Projections::printProjectionData ()
629 printProjectionData (0, nView() - 1);
633 Projections::printProjectionData (int startView, int endView)
635 printf("Projections Data\n\n");
636 printf("Description: %s\n", m_remark.c_str());
637 printf("Geometry: %s\n", Scanner::convertGeometryIDToName (m_geometry));
638 printf("nView = %8d nDet = %8d\n", m_nView, m_nDet);
639 printf("focalLength = %8.4f ViewDiameter = %8.4f\n", m_dFocalLength, m_dViewDiameter);
640 printf("fanBeamAngle= %8.4f SourceDetector = %8.4f\n", convertRadiansToDegrees(m_dFanBeamAngle), m_dSourceDetectorLength);
641 printf("rotStart = %8.4f rotInc = %8.4f\n", m_rotStart, m_rotInc);
642 printf("detStart = %8.4f detInc = %8.4f\n", m_detStart, m_detInc);
643 if (m_projData != NULL) {
647 endView = m_nView - 1;
648 if (startView > m_nView - 1)
649 startView = m_nView - 1;
650 if (endView > m_nView - 1)
651 endView = m_nView - 1;
652 for (int ir = startView; ir <= endView - 1; ir++) {
653 printf("View %d: angle %f\n", ir, m_projData[ir]->viewAngle());
654 DetectorValue* detval = m_projData[ir]->detValues();
655 for (int id = 0; id < m_projData[ir]->nDet(); id++)
656 printf("%8.4f ", detval[id]);
663 Projections::printScanInfo (std::ostringstream& os) const
665 os << "Number of detectors: " << m_nDet << "\n";
666 os << "Number of views: " << m_nView<< "\n";
667 os << "Description: " << m_remark.c_str()<< "\n";
668 os << "Geometry: " << Scanner::convertGeometryIDToName (m_geometry)<< "\n";
669 os << "Focal Length: " << m_dFocalLength<< "\n";
670 os << "Source Detector Length: " << m_dSourceDetectorLength << "\n";
671 os << "View Diameter: " << m_dViewDiameter<< "\n";
672 os << "Fan Beam Angle: " << convertRadiansToDegrees(m_dFanBeamAngle) << "\n";
673 os << "detStart: " << m_detStart<< "\n";
674 os << "detInc: " << m_detInc<< "\n";
675 os << "rotStart: " << m_rotStart<< "\n";
676 os << "rotInc: " << m_rotInc<< "\n";
681 Projections::convertPolar (ImageFile& rIF, int iInterpolationID)
683 unsigned int nx = rIF.nx();
684 unsigned int ny = rIF.ny();
685 ImageFileArray v = rIF.getArray();
686 ImageFileArray vImag = rIF.getImaginaryArray();
688 if (! v || nx == 0 || ny == 0)
691 Projections* pProj = this;
692 if (m_geometry == Scanner::GEOMETRY_EQUIANGULAR || m_geometry == Scanner::GEOMETRY_EQUILINEAR)
693 pProj = interpolateToParallel();
695 Array2d<double> adView (nx, ny);
696 Array2d<double> adDet (nx, ny);
697 double** ppdView = adView.getArray();
698 double** ppdDet = adDet.getArray();
700 std::complex<double>** ppcDetValue = new std::complex<double>* [m_nView];
702 for (iView = 0; iView < m_nView; iView++) {
703 ppcDetValue[iView] = new std::complex<double> [m_nDet];
704 DetectorValue* detval = pProj->getDetectorArray (iView).detValues();
705 for (unsigned int iDet = 0; iDet < m_nDet; iDet++)
706 ppcDetValue[iView][iDet] = std::complex<double>(detval[iDet], 0);
709 pProj->calcArrayPolarCoordinates (nx, ny, ppdView, ppdDet, m_nDet, 1., m_detInc);
711 pProj->interpolatePolar (v, vImag, nx, ny, ppcDetValue, ppdView, ppdDet, pProj->m_nView, pProj->m_nDet,
712 pProj->m_nDet, iInterpolationID);
714 for (iView = 0; iView < m_nView; iView++)
715 delete [] ppcDetValue[iView];
716 delete [] ppcDetValue;
718 if (m_geometry == Scanner::GEOMETRY_EQUIANGULAR || m_geometry == Scanner::GEOMETRY_EQUILINEAR)
726 Projections::convertFFTPolar (ImageFile& rIF, int iInterpolationID, int iZeropad)
729 rIF.arrayDataClear();
732 unsigned int nx = rIF.nx();
733 unsigned int ny = rIF.ny();
734 ImageFileArray v = rIF.getArray();
735 if (! rIF.isComplex())
736 rIF.convertRealToComplex();
737 ImageFileArray vImag = rIF.getImaginaryArray();
739 if (! v || nx == 0 || ny == 0)
742 if (m_geometry != Scanner::GEOMETRY_PARALLEL) {
743 sys_error (ERR_WARNING, "convertFFTPolar supports Parallel only");
748 int iNumInterpDetWithZeros = ProcessSignal::addZeropadFactor (iInterpDet, iZeropad);
750 double dZeropadRatio = static_cast<double>(iNumInterpDetWithZeros) / static_cast<double>(iInterpDet);
752 fftw_plan plan = fftw_create_plan (iNumInterpDetWithZeros, FFTW_FORWARD, FFTW_IN_PLACE | FFTW_ESTIMATE | FFTW_USE_WISDOM);
754 fftw_complex* pcIn = new fftw_complex [iNumInterpDetWithZeros];
755 std::complex<double>** ppcDetValue = new std::complex<double>* [m_nView];
756 double dInterpScale = (m_nDet-1) / static_cast<double>(iInterpDet-1) / SQRT2;
758 for (unsigned int iView = 0; iView < m_nView; iView++) {
759 DetectorValue* detval = getDetectorArray(iView).detValues();
760 LinearInterpolator<DetectorValue> projInterp (detval, m_nDet);
761 for (unsigned int iDet = 0; iDet < iInterpDet; iDet++) {
762 // double dInterpPos = iInterpDet * dInterpScale;
763 double dInterpPos = (m_nDet / 2.) + (iDet - iInterpDet/2.) * dInterpScale;
764 pcIn[iDet].re = projInterp.interpolate (dInterpPos);
767 for (unsigned int iDet2 = iInterpDet; iDet2 < iNumInterpDetWithZeros; iDet2++)
768 pcIn[iDet2].re = pcIn[iDet2].im = 0;
770 fftw_one (plan, pcIn, NULL);
772 ppcDetValue[iView] = new std::complex<double> [iNumInterpDetWithZeros];
773 for (unsigned int iD = 0; iD < iNumInterpDetWithZeros; iD++)
774 ppcDetValue[iView][iD] = std::complex<double> (pcIn[iD].re / iInterpDet / (iInterpDet/2), pcIn[iD].im / iInterpDet / (iInterpDet/2));
776 Fourier::shuffleFourierToNaturalOrder (ppcDetValue[iView], iNumInterpDetWithZeros);
780 fftw_destroy_plan (plan);
782 Array2d<double> adView (nx, ny);
783 Array2d<double> adDet (nx, ny);
784 double** ppdView = adView.getArray();
785 double** ppdDet = adDet.getArray();
786 calcArrayPolarCoordinates (nx, ny, ppdView, ppdDet, iNumInterpDetWithZeros, dZeropadRatio,
787 m_detInc * dInterpScale);
789 interpolatePolar (v, vImag, nx, ny, ppcDetValue, ppdView, ppdDet, m_nView, m_nDet, iNumInterpDetWithZeros,
792 for (int i = 0; i < m_nView; i++)
793 delete [] ppcDetValue[i];
794 delete [] ppcDetValue;
802 Projections::calcArrayPolarCoordinates (unsigned int nx, unsigned int ny, double** ppdView, double** ppdDet,
803 int iNumDetWithZeros, double dZeropadRatio, double dDetInc)
805 // double dLength = viewDiameter();
806 double dLength = phmLen();
807 double xMin = -dLength / 2;
808 double xMax = xMin + dLength;
809 double yMin = -dLength / 2;
810 double yMax = yMin + dLength;
811 double xCent = (xMin + xMax) / 2;
812 double yCent = (yMin + yMax) / 2;
814 xMin = (xMin - xCent) * dZeropadRatio + xCent;
815 xMax = (xMax - xCent) * dZeropadRatio + xCent;
816 yMin = (yMin - yCent) * dZeropadRatio + yCent;
817 yMax = (yMax - yCent) * dZeropadRatio + yCent;
819 double xInc = (xMax - xMin) / nx; // size of cells
820 double yInc = (yMax - yMin) / ny;
822 // +1 is correct for frequency data, ndet-1 is correct for projections
823 int iDetCenter = (iNumDetWithZeros - 1) / 2; // index refering to L=0 projection
824 if (isEven (iNumDetWithZeros))
825 iDetCenter = (iNumDetWithZeros + 1) / 2;
827 // Calculates polar coordinates (view#, det#) for each point on phantom grid
828 double x = xMin + xInc / 2; // Rectang coords of center of pixel
829 for (unsigned int ix = 0; ix < nx; x += xInc, ix++) {
830 double y = yMin + yInc / 2;
831 for (unsigned int iy = 0; iy < ny; y += yInc, iy++) {
832 double r = ::sqrt (x * x + y * y);
833 double phi = atan2 (y, x);
843 ppdView[ix][iy] = (phi - m_rotStart) / m_rotInc;
844 ppdDet[ix][iy] = (r / dDetInc) + iDetCenter;
850 Projections::interpolatePolar (ImageFileArray& v, ImageFileArray& vImag,
851 unsigned int nx, unsigned int ny, std::complex<double>** ppcDetValue, double** ppdView,
852 double** ppdDet, unsigned int nView, unsigned int nDet, unsigned int nDetWithZeros, int iInterpolationID)
854 typedef std::complex<double> complexValue;
855 BilinearInterpolator<complexValue> bilinear (ppcDetValue, nView, nDetWithZeros);
857 for (unsigned int ix = 0; ix < ny; ix++) {
858 for (unsigned int iy = 0; iy < ny; iy++) {
860 if (iInterpolationID == POLAR_INTERP_NEAREST) {
861 unsigned int iView = nearest<int> (ppdView[ix][iy]);
862 unsigned int iDet = nearest<int> (ppdDet[ix][iy]);
863 if (iView == nView) {
865 iDet = m_nDet - iDet;
867 if (iDet >= 0 && iDet < nDetWithZeros && iView >= 0 && iView < nView) {
868 v[ix][iy] = ppcDetValue[iView][iDet].real();
870 vImag[ix][iy] = ppcDetValue[iView][iDet].imag();
874 } else if (iInterpolationID == POLAR_INTERP_BILINEAR) {
876 std::complex<double> vInterp = bilinear.interpolate (ppdView[ix][iy], ppdDet[ix][iy]);
877 v[ix][iy] = vInterp.real();
879 vImag[ix][iy] = vInterp.imag();
881 int iFloorView = ::floor (ppdView[ix][iy]);
882 double dFracView = ppdView[ix][iy] - iFloorView;
883 int iFloorDet = ::floor (ppdDet[ix][iy]);
884 double dFracDet = ppdDet[ix][iy] - iFloorDet;
886 if (iFloorDet >= 0 && iFloorView >= 0) {
887 std::complex<double> v1 = ppcDetValue[iFloorView][iFloorDet];
888 std::complex<double> v2, v3, v4;
889 if (iFloorView < nView - 1)
890 v2 = ppcDetValue[iFloorView + 1][iFloorDet];
892 v2 = ppcDetValue[0][iFloorDet];
893 if (iFloorDet < nDetWithZeros - 1)
894 v4 = ppcDetValue[iFloorView][iFloorDet+1];
897 if (iFloorView < nView - 1 && iFloorDet < nDetWithZeros - 1)
898 v3 = ppcDetValue [iFloorView+1][iFloorDet+1];
899 else if (iFloorView < nView - 1)
902 v3 = ppcDetValue[0][iFloorDet+1];
904 std::complex<double> vInterp = (1 - dFracView) * (1 - dFracDet) * v1 +
905 dFracView * (1 - dFracDet) * v2 + dFracView * dFracDet * v3 +
906 dFracDet * (1 - dFracView) * v4;
907 v[ix][iy] = vInterp.real();
909 vImag[ix][iy] = vInterp.imag();
911 // sys_error (ERR_SEVERE, "Can't find projection data for ix=%d,iy=%d with radView=%f and radDet=%f", ix, iy, ppdView[ix][iy], ppdDet[ix][iy]);
917 } else if (iInterpolationID == POLAR_INTERP_BICUBIC) {
927 Projections::initFromSomatomAR_STAR (int iNViews, int iNDets, unsigned char* pData, unsigned long lDataLength)
929 init (iNViews, iNDets);
930 m_geometry = Scanner::GEOMETRY_EQUIANGULAR;
931 m_dFocalLength = 510;
932 m_dSourceDetectorLength = 890;
933 m_detInc = convertDegreesToRadians (3.06976 / 60);
934 m_dFanBeamAngle = iNDets * m_detInc;
935 m_detStart = -(m_dFanBeamAngle / 2);
936 m_rotInc = TWOPI / static_cast<double>(iNViews);
938 m_dViewDiameter = sin (m_dFanBeamAngle / 2) * m_dFocalLength * 2;
940 if (! ((iNViews == 750 && lDataLength == 1560000L) || (iNViews == 950 && lDataLength == 1976000L)
941 || (iNViews == 1500 && lDataLength == 3120000)))
944 double dCenter = (iNDets - 1.) / 2.; // change from (Nm+1)/2 because of 0 vs. 1 indexing
945 double* pdCosScale = new double [iNDets];
946 for (int i = 0; i < iNDets; i++)
947 pdCosScale[i] = 1. / cos ((i - dCenter) * m_detInc);
950 for (int iv = 0; iv < iNViews; iv++) {
951 unsigned char* pArgBase = pData + lDataPos;
952 unsigned char* p = pArgBase+0; SwapBytes4IfLittleEndian (p);
953 long lProjNumber = *reinterpret_cast<long*>(p);
955 p = pArgBase+20; SwapBytes4IfLittleEndian (p);
956 long lEscale = *reinterpret_cast<long*>(p);
958 p = pArgBase+28; SwapBytes4IfLittleEndian (p);
959 long lTime = *reinterpret_cast<long*>(p);
961 p = pArgBase + 4; SwapBytes4IfLittleEndian (p);
962 double dAlpha = *reinterpret_cast<float*>(p) + HALFPI;
964 p = pArgBase+12; SwapBytes4IfLittleEndian (p);
965 double dAlign = *reinterpret_cast<float*>(p);
967 p = pArgBase + 16; SwapBytes4IfLittleEndian (p);
968 double dMaxValue = *reinterpret_cast<float*>(p);
970 DetectorArray& detArray = getDetectorArray (iv);
971 detArray.setViewAngle (dAlpha);
972 DetectorValue* detval = detArray.detValues();
974 double dViewScale = 1. / (2294.4871 * ::pow (2.0, -lEscale));
976 for (int id = 0; id < iNDets; id++) {
977 int iV = pData[lDataPos+1] + (pData[lDataPos] << 8);
978 if (iV > 32767) // two's complement signed conversion
980 detval[id] = iV * dViewScale * pdCosScale[id];
984 for (int k = iNDets - 2; k >= 0; k--)
985 detval[k+1] = detval[k];
995 Projections::interpolateToParallel () const
997 if (m_geometry == Scanner::GEOMETRY_PARALLEL)
998 return const_cast<Projections*>(this);
1001 int nView = m_nView;
1002 Projections* pProjNew = new Projections (nView, nDet);
1003 pProjNew->m_geometry = Scanner::GEOMETRY_PARALLEL;
1004 pProjNew->m_dFocalLength = m_dFocalLength;
1005 pProjNew->m_dSourceDetectorLength = m_dSourceDetectorLength;
1006 pProjNew->m_dViewDiameter = m_dViewDiameter;
1007 pProjNew->m_dFanBeamAngle = m_dFanBeamAngle;
1008 pProjNew->m_calcTime = 0;
1009 pProjNew->m_remark = m_remark;
1010 pProjNew->m_remark += "; Interpolate to Parallel";
1011 pProjNew->m_label.setLabelType (Array2dFileLabel::L_HISTORY);
1012 pProjNew->m_label.setLabelString (pProjNew->m_remark);
1013 pProjNew->m_label.setCalcTime (pProjNew->m_calcTime);
1014 pProjNew->m_label.setDateTime (pProjNew->m_year, pProjNew->m_month, pProjNew->m_day, pProjNew->m_hour, pProjNew->m_minute, pProjNew->m_second);
1016 pProjNew->m_rotStart = 0;
1017 #ifdef CONVERT_PARALLEL_PI
1018 pProjNew->m_rotInc = PI / nView;;
1020 pProjNew->m_rotInc = TWOPI / nView;
1022 pProjNew->m_detStart = -m_dViewDiameter / 2;
1023 pProjNew->m_detInc = m_dViewDiameter / nDet;
1024 if (isEven (nDet)) // even
1025 pProjNew->m_detInc = m_dViewDiameter / (nDet - 1);
1027 ParallelRaysums parallel (this, ParallelRaysums::THETA_RANGE_NORMALIZE_TO_TWOPI);
1029 double* pdThetaValuesForT = new double [pProjNew->nView()];
1030 double* pdRaysumsForT = new double [pProjNew->nView()];
1032 // interpolate to evenly spaced theta (views)
1033 double dDetPos = pProjNew->m_detStart;
1034 for (int iD = 0; iD < pProjNew->nDet(); iD++, dDetPos += pProjNew->m_detInc) {
1035 parallel.getThetaAndRaysumsForT (iD, pdThetaValuesForT, pdRaysumsForT);
1037 double dViewAngle = m_rotStart;
1038 int iLastFloor = -1;
1039 for (int iV = 0; iV < pProjNew->nView(); iV++, dViewAngle += pProjNew->m_rotInc) {
1040 DetectorValue* detValues = pProjNew->getDetectorArray (iV).detValues();
1041 LinearInterpolator<double> interp (pdThetaValuesForT, pdRaysumsForT, pProjNew->nView());
1042 detValues[iD] = interp.interpolate (dViewAngle, &iLastFloor);
1045 delete pdThetaValuesForT;
1046 delete pdRaysumsForT;
1048 // interpolate to evenly space t (detectors)
1049 double* pdOriginalDetPositions = new double [pProjNew->nDet()];
1050 parallel.getDetPositions (pdOriginalDetPositions);
1052 double* pdDetValueCopy = new double [pProjNew->nDet()];
1053 double dViewAngle = m_rotStart;
1054 for (int iV = 0; iV < pProjNew->nView(); iV++, dViewAngle += pProjNew->m_rotInc) {
1055 DetectorArray& detArray = pProjNew->getDetectorArray (iV);
1056 DetectorValue* detValues = detArray.detValues();
1057 detArray.setViewAngle (dViewAngle);
1059 for (int i = 0; i < pProjNew->nDet(); i++)
1060 pdDetValueCopy[i] = detValues[i];
1062 double dDetPos = pProjNew->m_detStart;
1063 int iLastFloor = -1;
1064 LinearInterpolator<double> interp (pdOriginalDetPositions, pdDetValueCopy, pProjNew->nDet());
1065 for (int iD = 0; iD < pProjNew->nDet(); iD++, dDetPos += pProjNew->m_detInc)
1066 detValues[iD] = interp.interpolate (dDetPos, &iLastFloor);
1068 delete pdDetValueCopy;
1069 delete pdOriginalDetPositions;
1075 ///////////////////////////////////////////////////////////////////////////////
1077 // Class ParallelRaysums
1079 // Used for converting divergent beam raysums into Parallel raysums
1081 ///////////////////////////////////////////////////////////////////////////////
1083 ParallelRaysums::ParallelRaysums (const Projections* pProjections, int iThetaRange)
1084 : m_iNumCoordinates(0), m_iNumView(pProjections->nView()), m_iNumDet(pProjections->nDet()),
1085 m_iThetaRange (iThetaRange), m_pCoordinates(NULL)
1087 int iGeometry = pProjections->geometry();
1088 double dDetInc = pProjections->detInc();
1089 double dDetStart = pProjections->detStart();
1090 double dFocalLength = pProjections->focalLength();
1092 m_iNumCoordinates = m_iNumView * m_iNumDet;
1093 m_pCoordinates = new ParallelRaysumCoordinate [m_iNumCoordinates];
1094 m_vecpCoordinates.reserve (m_iNumCoordinates);
1095 for (int i = 0; i < m_iNumCoordinates; i++)
1096 m_vecpCoordinates[i] = m_pCoordinates + i;
1098 int iCoordinate = 0;
1099 for (int iV = 0; iV < m_iNumView; iV++) {
1100 double dViewAngle = pProjections->getDetectorArray(iV).viewAngle();
1101 const DetectorValue* detValues = pProjections->getDetectorArray(iV).detValues();
1103 double dDetPos = dDetStart;
1104 for (int iD = 0; iD < m_iNumDet; iD++) {
1105 ParallelRaysumCoordinate* pC = m_vecpCoordinates[iCoordinate++];
1107 if (iGeometry == Scanner::GEOMETRY_PARALLEL) {
1108 pC->m_dTheta = dViewAngle;
1110 } else if (iGeometry == Scanner::GEOMETRY_EQUILINEAR) {
1111 double dFanAngle = atan (dDetPos / pProjections->sourceDetectorLength());
1112 pC->m_dTheta = dViewAngle + dFanAngle;
1113 pC->m_dT = dFocalLength * sin(dFanAngle);
1115 } else if (iGeometry == Scanner::GEOMETRY_EQUIANGULAR) {
1116 // fan angle is same as dDetPos
1117 pC->m_dTheta = dViewAngle + dDetPos;
1118 pC->m_dT = dFocalLength * sin (dDetPos);
1120 if (m_iThetaRange != THETA_RANGE_UNCONSTRAINED) {
1121 pC->m_dTheta = normalizeAngle (pC->m_dTheta);
1122 if (m_iThetaRange == THETA_RANGE_FOLD_TO_PI && pC->m_dTheta >= PI) {
1124 pC->m_dT = -pC->m_dT;
1127 pC->m_dRaysum = detValues[iD];
1133 ParallelRaysums::~ParallelRaysums()
1135 delete m_pCoordinates;
1138 ParallelRaysums::CoordinateContainer&
1139 ParallelRaysums::getSortedByTheta()
1141 if (m_vecpSortedByTheta.size() == 0) {
1142 m_vecpSortedByTheta.resize (m_iNumCoordinates);
1143 for (int i = 0; i < m_iNumCoordinates; i++)
1144 m_vecpSortedByTheta[i] = m_vecpCoordinates[i];
1145 std::sort (m_vecpSortedByTheta.begin(), m_vecpSortedByTheta.end(), ParallelRaysumCoordinate::compareByTheta);
1148 return m_vecpSortedByTheta;
1151 ParallelRaysums::CoordinateContainer&
1152 ParallelRaysums::getSortedByT()
1154 if (m_vecpSortedByT.size() == 0) {
1155 m_vecpSortedByT.resize (m_iNumCoordinates);
1156 for (int i = 0; i < m_iNumCoordinates; i++)
1157 m_vecpSortedByT[i] = m_vecpCoordinates[i];
1158 std::sort (m_vecpSortedByT.begin(), m_vecpSortedByT.end(), ParallelRaysumCoordinate::compareByT);
1161 return m_vecpSortedByT;
1166 ParallelRaysums::getLimits (double* dMinT, double* dMaxT, double* dMinTheta, double* dMaxTheta) const
1168 if (m_iNumCoordinates <= 0)
1171 *dMinT = *dMaxT = m_vecpCoordinates[0]->m_dT;
1172 *dMinTheta = *dMaxTheta = m_vecpCoordinates[0]->m_dTheta;
1174 for (int i = 0; i < m_iNumCoordinates; i++) {
1175 double dT = m_vecpCoordinates[i]->m_dT;
1176 double dTheta = m_vecpCoordinates[i]->m_dTheta;
1180 else if (dT > *dMaxT)
1183 if (dTheta < *dMinTheta)
1184 *dMinTheta = dTheta;
1185 else if (dTheta > *dMaxTheta)
1186 *dMaxTheta = dTheta;
1191 ParallelRaysums::getThetaAndRaysumsForT (int iTheta, double* pTheta, double* pRaysum)
1193 const CoordinateContainer& coordsT = getSortedByT();
1195 int iBase = iTheta * m_iNumView;
1196 for (int i = 0; i < m_iNumView; i++) {
1197 int iPos = iBase + i;
1198 pTheta[i] = coordsT[iPos]->m_dTheta;
1199 pRaysum[i] = coordsT[iPos]->m_dRaysum;
1204 ParallelRaysums::getDetPositions (double* pdDetPos)
1206 const CoordinateContainer& coordsT = getSortedByT();
1209 for (int i = 0; i < m_iNumDet; i++) {
1210 pdDetPos[i] = coordsT[iPos]->m_dT;