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-2000 Kevin Rosenberg
11 ** $Id: projections.cpp,v 1.45 2001/01/16 20:55:29 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* Projections::s_aszInterpName[] =
43 const char* 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_phmLen = scanner.phmLen();
152 m_rotInc = scanner.rotInc();
153 m_detInc = scanner.detInc();
154 m_geometry = scanner.geometry();
155 m_focalLength = scanner.focalLength();
156 m_fieldOfView = scanner.fieldOfView();
158 m_detStart = -(scanner.detLen() / 2);
162 Projections::setNView (int nView) // used by MPI to reduce # of views
165 init (nView, m_nDet);
172 Projections::newProjData (void)
175 sys_error(ERR_WARNING, "m_projData != NULL [newProjData]");
177 if (m_nView > 0 && m_nDet) {
178 m_projData = new DetectorArray* [m_nView];
180 for (int i = 0; i < m_nView; i++)
181 m_projData[i] = new DetectorArray (m_nDet);
187 * projections_free Free memory allocated to projections
190 * projections_free(proj)
191 * Projections& proj Projectionss to be deallocated
195 Projections::deleteProjData (void)
197 if (m_projData != NULL) {
198 for (int i = 0; i < m_nView; i++)
199 delete m_projData[i];
208 * Projections::headerWwrite Write data header for projections file
213 Projections::headerWrite (fnetorderstream& fs)
215 kuint16 _hsize = m_headerSize;
216 kuint16 _signature = m_signature;
217 kuint32 _nView = m_nView;
218 kuint32 _nDet = m_nDet;
219 kuint32 _geom = m_geometry;
220 kuint16 _remarksize = m_remark.length();
221 kuint16 _year = m_year;
222 kuint16 _month = m_month;
223 kuint16 _day = m_day;
224 kuint16 _hour = m_hour;
225 kuint16 _minute = m_minute;
226 kuint16 _second = m_second;
228 kfloat64 _calcTime = m_calcTime;
229 kfloat64 _rotStart = m_rotStart;
230 kfloat64 _rotInc = m_rotInc;
231 kfloat64 _detStart = m_detStart;
232 kfloat64 _detInc = m_detInc;
233 kfloat64 _phmLen = m_phmLen;
234 kfloat64 _fieldOfView = m_fieldOfView;
235 kfloat64 _focalLength = m_focalLength;
241 fs.writeInt16 (_hsize);
242 fs.writeInt16 (_signature);
243 fs.writeInt32 (_nView);
244 fs.writeInt32 (_nDet);
245 fs.writeInt32 (_geom);
246 fs.writeFloat64 (_calcTime);
247 fs.writeFloat64 (_rotStart);
248 fs.writeFloat64 (_rotInc);
249 fs.writeFloat64 (_detStart);
250 fs.writeFloat64 (_detInc);
251 fs.writeFloat64 (_phmLen);
252 fs.writeFloat64 (_focalLength);
253 fs.writeFloat64 (_fieldOfView);
254 fs.writeInt16 (_year);
255 fs.writeInt16 (_month);
256 fs.writeInt16 (_day);
257 fs.writeInt16 (_hour);
258 fs.writeInt16 (_minute);
259 fs.writeInt16 (_second);
260 fs.writeInt16 (_remarksize);
261 fs.write (m_remark.c_str(), _remarksize);
263 m_headerSize = fs.tellp();
264 _hsize = m_headerSize;
266 fs.writeInt16 (_hsize);
274 * projections_read_header Read data header for projections file
278 Projections::headerRead (fnetorderstream& fs)
280 kuint16 _hsize, _signature, _year, _month, _day, _hour, _minute, _second, _remarksize = 0;
281 kuint32 _nView, _nDet, _geom;
282 kfloat64 _calcTime, _rotStart, _rotInc, _detStart, _detInc, _phmLen, _focalLength, _fieldOfView;
288 fs.readInt16 (_hsize);
289 fs.readInt16 (_signature);
290 fs.readInt32 (_nView);
291 fs.readInt32 (_nDet);
292 fs.readInt32 (_geom);
293 fs.readFloat64 (_calcTime);
294 fs.readFloat64 (_rotStart);
295 fs.readFloat64 (_rotInc);
296 fs.readFloat64 (_detStart);
297 fs.readFloat64 (_detInc);
298 fs.readFloat64 (_phmLen);
299 fs.readFloat64 (_focalLength);
300 fs.readFloat64 (_fieldOfView);
301 fs.readInt16 (_year);
302 fs.readInt16 (_month);
304 fs.readInt16 (_hour);
305 fs.readInt16 (_minute);
306 fs.readInt16 (_second);
307 fs.readInt16 (_remarksize);
310 sys_error (ERR_SEVERE, "Error reading header information , _remarksize=%d [projections_read_header]", _remarksize);
314 if (_signature != m_signature) {
315 sys_error (ERR_SEVERE, "File %s does not have a valid projection file signature", m_filename.c_str());
319 char* pszRemarkStorage = new char [_remarksize+1];
320 fs.read (pszRemarkStorage, _remarksize);
322 sys_error (ERR_SEVERE, "Error reading remark, _remarksize = %d", _remarksize);
325 pszRemarkStorage[_remarksize] = 0;
326 m_remark = pszRemarkStorage;
327 delete pszRemarkStorage;
329 off_t _hsizeread = fs.tellg();
330 if (!fs || _hsizeread != _hsize) {
331 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);
335 m_headerSize = _hsize;
339 m_calcTime = _calcTime;
340 m_rotStart = _rotStart;
342 m_detStart = _detStart;
345 m_focalLength = _focalLength;
346 m_fieldOfView = _fieldOfView;
354 m_label.setLabelType (Array2dFileLabel::L_HISTORY);
355 m_label.setLabelString (m_remark);
356 m_label.setCalcTime (m_calcTime);
357 m_label.setDateTime (m_year, m_month, m_day, m_hour, m_minute, m_second);
363 Projections::read (const std::string& filename)
365 return read (filename.c_str());
370 Projections::read (const char* filename)
372 m_filename = filename;
374 frnetorderstream fileRead (m_filename.c_str(), std::ios::in | std::ios::binary);
376 frnetorderstream fileRead (m_filename.c_str(), std::ios::in | std::ios::binary | std::ios::nocreate);
382 if (! headerRead (fileRead))
388 for (int i = 0; i < m_nView; i++) {
389 if (! detarrayRead (fileRead, *m_projData[i], i))
399 Projections::copyViewData (const std::string& filename, std::ostream& os, int startView, int endView)
401 return copyViewData (filename.c_str(), os, startView, endView);
405 Projections::copyViewData (const char* const filename, std::ostream& os, int startView, int endView)
407 frnetorderstream is (filename, std::ios::in | std::ios::binary);
408 kuint16 sizeHeader, signature;
409 kuint32 _nView, _nDet;
413 sys_error (ERR_SEVERE, "Unable to read projection file %s", filename);
417 is.readInt16 (sizeHeader);
418 is.readInt16 (signature);
419 is.readInt32 (_nView);
420 is.readInt32 (_nDet);
424 if (signature != m_signature) {
425 sys_error (ERR_SEVERE, "Illegal signature in projection file %s", filename);
431 if (startView > nView - 1)
433 if (endView < 0 || endView > nView - 1)
436 if (startView > endView) { // swap if start > end
437 int tempView = endView;
439 startView = tempView;
442 int sizeView = 8 /* view_angle */ + 4 /* nDet */ + (4 * nDet);
443 unsigned char* pViewData = new unsigned char [sizeView];
445 for (int i = startView; i <= endView; i++) {
446 is.seekg (sizeHeader + i * sizeView);
447 is.read (reinterpret_cast<char*>(pViewData), sizeView);
448 os.write (reinterpret_cast<char*>(pViewData), sizeView);
449 if (is.fail() || os.fail())
455 sys_error (ERR_SEVERE, "Error reading projection file");
457 sys_error (ERR_SEVERE, "Error writing projection file");
459 return (! (is.fail() | os.fail()));
463 Projections::copyHeader (const std::string& filename, std::ostream& os)
465 return copyHeader (filename.c_str(), os);
469 Projections::copyHeader (const char* const filename, std::ostream& os)
471 frnetorderstream is (filename, std::ios::in | std::ios::binary);
472 kuint16 sizeHeader, signature;
473 is.readInt16 (sizeHeader);
474 is.readInt16 (signature);
476 if (signature != m_signature) {
477 sys_error (ERR_SEVERE, "Illegal signature in projection file %s", filename);
481 unsigned char* pHdrData = new unsigned char [sizeHeader];
482 is.read (reinterpret_cast<char*>(pHdrData), sizeHeader);
484 sys_error (ERR_SEVERE, "Error reading header");
488 os.write (reinterpret_cast<char*>(pHdrData), sizeHeader);
490 sys_error (ERR_SEVERE, "Error writing header");
498 Projections::write (const std::string& filename)
500 return write (filename.c_str());
504 Projections::write (const char* filename)
506 frnetorderstream fs (filename, std::ios::out | std::ios::binary | std::ios::trunc | std::ios::ate);
507 m_filename = filename;
509 sys_error (ERR_SEVERE, "Error opening file %s for output [projections_create]", filename);
513 if (! headerWrite (fs))
516 if (m_projData != NULL) {
517 for (int i = 0; i < m_nView; i++) {
518 if (! detarrayWrite (fs, *m_projData[i], i))
531 * detarrayRead Read a Detector Array structure from the disk
534 * detarrayRead (proj, darray, view_num)
535 * DETARRAY *darray Detector array storage location to be filled
536 * int view_num View number to read
540 Projections::detarrayRead (fnetorderstream& fs, DetectorArray& darray, const int iview)
542 const int detval_bytes = darray.nDet() * sizeof(kfloat32);
543 const int detheader_bytes = sizeof(kfloat64) /* view_angle */ + sizeof(kint32) /* nDet */;
544 const int view_bytes = detheader_bytes + detval_bytes;
545 const off_t start_data = m_headerSize + (iview * view_bytes);
546 DetectorValue* detval_ptr = darray.detValues();
550 fs.seekg (start_data);
552 fs.readFloat64 (view_angle);
554 darray.setViewAngle (view_angle);
555 // darray.setNDet ( nDet);
557 for (unsigned int i = 0; i < nDet; i++) {
559 fs.readFloat32 (detval);
560 detval_ptr[i] = detval;
570 * detarrayWrite Write detector array data to the disk
573 * detarrayWrite (darray, view_num)
574 * DETARRAY *darray Detector array data to be written
575 * int view_num View number to write
578 * This routine writes the detarray data from the disk sequentially to
579 * the file that was opened with open_projections(). Data is written in
584 Projections::detarrayWrite (fnetorderstream& fs, const DetectorArray& darray, const int iview)
586 const int detval_bytes = darray.nDet() * sizeof(float);
587 const int detheader_bytes = sizeof(kfloat64) /* view_angle */ + sizeof(kint32) /* nDet */;
588 const int view_bytes = detheader_bytes + detval_bytes;
589 const off_t start_data = m_headerSize + (iview * view_bytes);
590 const DetectorValue* const detval_ptr = darray.detValues();
591 kfloat64 view_angle = darray.viewAngle();
592 kuint32 nDet = darray.nDet();
594 fs.seekp (start_data);
596 sys_error (ERR_SEVERE, "Error seeking detectory array [detarrayWrite]");
600 fs.writeFloat64 (view_angle);
601 fs.writeInt32 (nDet);
603 for (unsigned int i = 0; i < nDet; i++) {
604 kfloat32 detval = detval_ptr[i];
605 fs.writeFloat32 (detval);
615 * printProjectionData Print projections data
618 * printProjectionData ()
622 Projections::printProjectionData ()
624 printProjectionData (0, nView() - 1);
628 Projections::printProjectionData (int startView, int endView)
630 printf("Projections Data\n\n");
631 printf("Description: %s\n", m_remark.c_str());
632 printf("Geometry: %s\n", Scanner::convertGeometryIDToName (m_geometry));
633 printf("nView = %8d nDet = %8d\n", m_nView, m_nDet);
634 printf("focalLength = %8.4f fieldOfView = %8.4f\n", m_focalLength, m_fieldOfView);
635 printf("rotStart = %8.4f rotInc = %8.4f\n", m_rotStart, m_rotInc);
636 printf("detStart = %8.4f detInc = %8.4f\n", m_detStart, m_detInc);
637 if (m_projData != NULL) {
641 endView = m_nView - 1;
642 if (startView > m_nView - 1)
643 startView = m_nView - 1;
644 if (endView > m_nView - 1)
645 endView = m_nView - 1;
646 for (int ir = startView; ir <= endView - 1; ir++) {
647 printf("View %d: angle %f\n", ir, m_projData[ir]->viewAngle());
648 DetectorValue* detval = m_projData[ir]->detValues();
649 for (int id = 0; id < m_projData[ir]->nDet(); id++)
650 printf("%8.4f ", detval[id]);
657 Projections::printScanInfo (std::ostringstream& os) const
659 os << "Number of detectors: " << m_nDet << "\n";
660 os << " Number of views: " << m_nView<< "\n";
661 os << " Remark: " << m_remark.c_str()<< "\n";
662 os << " Geometry: " << Scanner::convertGeometryIDToName (m_geometry)<< "\n";
663 os << " Focal Length: " << m_focalLength<< "\n";
664 os << " Field Of View: " << m_fieldOfView<< "\n";
665 os << " phmLen: " << m_phmLen<< "\n";
666 os << " detStart: " << m_detStart<< "\n";
667 os << " detInc: " << m_detInc<< "\n";
668 os << " rotStart: " << m_rotStart<< "\n";
669 os << " rotInc: " << m_rotInc<< "\n";
674 Projections::convertPolar (ImageFile& rIF, int iInterpolationID)
676 unsigned int nx = rIF.nx();
677 unsigned int ny = rIF.ny();
678 ImageFileArray v = rIF.getArray();
679 ImageFileArray vImag = rIF.getImaginaryArray();
681 if (! v || nx == 0 || ny == 0)
684 Array2d<double> adView (nx, ny);
685 Array2d<double> adDet (nx, ny);
686 double** ppdView = adView.getArray();
687 double** ppdDet = adDet.getArray();
689 calcArrayPolarCoordinates (nx, ny, ppdView, ppdDet);
691 std::complex<double>** ppcDetValue = new std::complex<double>* [m_nView];
693 for (iView = 0; iView < m_nView; iView++) {
694 ppcDetValue[iView] = new std::complex<double> [m_nDet];
695 for (unsigned int iDet = 0; iDet < m_nDet; iDet++)
696 ppcDetValue[iView][iDet] = std::complex<double>(getDetectorArray (iView).detValues()[iDet], 0);
699 interpolatePolar (v, vImag, nx, ny, ppcDetValue, ppdView, ppdDet, m_nView, m_nDet, iInterpolationID);
701 for (iView = 0; iView < m_nView; iView++)
702 delete [] ppcDetValue[iView];
703 delete [] ppcDetValue;
710 Projections::convertFFTPolar (ImageFile& rIF, int iInterpolationID, int iZeropad)
712 unsigned int nx = rIF.nx();
713 unsigned int ny = rIF.ny();
714 ImageFileArray v = rIF.getArray();
715 if (! rIF.isComplex())
716 rIF.convertRealToComplex();
717 ImageFileArray vImag = rIF.getImaginaryArray();
719 if (! v || nx == 0 || ny == 0)
725 Array2d<double> adView (nx, ny);
726 Array2d<double> adDet (nx, ny);
727 double** ppdView = adView.getArray();
728 double** ppdDet = adDet.getArray();
730 std::complex<double>** ppcDetValue = new std::complex<double>* [m_nView];
732 double* pdDet = new double [m_nDet];
733 fftw_complex* pcIn = new fftw_complex [m_nDet];
734 fftw_plan plan = fftw_create_plan (m_nDet, FFTW_FORWARD, FFTW_IN_PLACE);
736 for (iView = 0; iView < m_nView; iView++) {
738 for (iDet = 0; iDet < m_nDet; iDet++) {
739 pcIn[iDet].re = getDetectorArray(iView).detValues()[iDet];
742 fftw_one (plan, pcIn, NULL);
743 ppcDetValue[iView] = new std::complex<double> [m_nDet];
744 for (iDet = 0; iDet < m_nDet; iDet++)
745 ppcDetValue[iView][iDet] = std::complex<double> (pcIn[iDet].re, pcIn[iDet].im);
746 Fourier::shuffleFourierToNaturalOrder (ppcDetValue[iView], m_nDet);
749 fftw_destroy_plan (plan);
752 calcArrayPolarCoordinates (nx, ny, ppdView, ppdDet);
754 interpolatePolar (v, vImag, nx, ny, ppcDetValue, ppdView, ppdDet, m_nView, m_nDet, iInterpolationID);
756 for (iView = 0; iView < m_nView; iView++)
757 delete [] ppcDetValue[iView];
758 delete [] ppcDetValue;
766 Projections::calcArrayPolarCoordinates (unsigned int nx, unsigned int ny, double** ppdView, double** ppdDet)
768 double xMin = -m_phmLen / 2;
769 double xMax = xMin + m_phmLen;
770 double yMin = -m_phmLen / 2;
771 double yMax = yMin + m_phmLen;
773 double xInc = (xMax - xMin) / nx; // size of cells
774 double yInc = (yMax - yMin) / ny;
776 int iDetCenter = (m_nDet - 1) / 2; // index refering to L=0 projection
778 if (m_geometry != Scanner::GEOMETRY_PARALLEL) {
779 sys_error (ERR_WARNING, "convertPolar supports Parallel only");
783 // Calculates polar coordinates (view#, det#) for each point on phantom grid
784 double x = xMin + xInc / 2; // Rectang coords of center of pixel
785 for (unsigned int ix = 0; ix < nx; x += xInc, ix++) {
786 double y = yMin + yInc / 2;
787 for (unsigned int iy = 0; iy < ny; y += yInc, iy++) {
788 double r = ::sqrt (x * x + y * y);
789 double phi = atan2 (y, x);
793 } else if (phi < 0) {
798 ppdView[ix][iy] = (phi - m_rotStart) / m_rotInc;
799 ppdDet[ix][iy] = (r / m_detInc) + iDetCenter;
805 Projections::interpolatePolar (ImageFileArray& v, ImageFileArray& vImag,
806 unsigned int nx, unsigned int ny, std::complex<double>** ppcDetValue,
807 double** ppdView, double** ppdDet, unsigned int nView, unsigned int nDet, int iInterpolationID)
809 for (unsigned int ix = 0; ix < ny; ix++) {
810 for (unsigned int iy = 0; iy < ny; iy++) {
811 if (iInterpolationID == POLAR_INTERP_NEAREST) {
812 int iView = nearest<int> (ppdView[ix][iy]);
813 int iDet = nearest<int> (ppdDet[ix][iy]);
814 if (iView == nView) {
816 // iDet = m_nDet - iDet;
818 if (iDet >= 0 && iDet < nDet && iView >= 0 && iView < nView) {
819 v[ix][iy] = ppcDetValue[iView][iDet].real();
821 vImag[ix][iy] = ppcDetValue[iView][iDet].imag();
823 sys_error (ERR_SEVERE, "Can't find projection data for ix=%d,iy=%d with radView=%f and radDet=%f",
824 ix, iy, ppdView[ix][iy], ppdDet[ix][iy]);
827 } else if (iInterpolationID == POLAR_INTERP_BILINEAR) {
828 int iFloorView = static_cast<int>(ppdView[ix][iy]);
829 double dFracView = ppdView[ix][iy] - iFloorView;
830 int iFloorDet = static_cast<int>(ppdDet[ix][iy]);
831 double dFracDet = ppdDet[ix][iy] - iFloorDet;
833 if (iFloorDet >= 0 && iFloorView >= 0) {
834 std::complex<double> v1 = ppcDetValue[iFloorView][iFloorDet];
835 std::complex<double> v2, v3, v4;
836 if (iFloorView < nView - 1)
837 v2 = ppcDetValue[iFloorView + 1][iFloorDet];
839 v2 = ppcDetValue[0][iFloorDet];
840 if (iFloorDet < nDet - 1)
841 v4 = ppcDetValue[iFloorView][iFloorDet+1];
844 if (iFloorView < nView - 1 && iFloorDet < nDet - 1)
845 v3 = ppcDetValue [iFloorView+1][iFloorDet+1];
846 else if (iFloorView < nView - 1)
849 v3 = ppcDetValue[0][iFloorDet+1];
850 std::complex<double> vInterp = (1 - dFracView) * (1 - dFracDet) * v1 +
851 dFracView * (1 - dFracDet) * v2 + dFracView * dFracDet * v3 +
852 dFracDet * (1 - dFracView) * v4;
853 v[ix][iy] = vInterp.real();
855 vImag[ix][iy] = vInterp.imag();
857 sys_error (ERR_SEVERE, "Can't find projection data for ix=%d,iy=%d with radView=%f and radDet=%f",
858 ix, iy, ppdView[ix][iy], ppdDet[ix][iy]);
863 } else if (iInterpolationID == POLAR_INTERP_BICUBIC) {