r254: Added binary output of projection file elements
[ctsim.git] / libctsim / projections.cpp
index 0182eec8d4af2ff6bae3ec189f2a3ad942974c6f..49aaa9942b078ea417817adb45789a0de5df26bc 100644 (file)
@@ -8,7 +8,7 @@
 **  This is part of the CTSim program
 **  Copyright (C) 1983-2000 Kevin Rosenberg
 **
-**  $Id: projections.cpp,v 1.11 2000/07/04 22:21:01 kevin Exp $
+**  $Id: projections.cpp,v 1.29 2000/12/16 02:31:00 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -25,7 +25,8 @@
 ******************************************************************************/
 
 #include "ct.h"
-
+\r
+const kuint16 Projections::m_signature = ('P'*256 + 'J');\r
 
 /* NAME
  *    Projections              Constructor for projections matrix storage 
@@ -66,6 +67,7 @@ Projections::~Projections (void)
 void
 Projections::init (const int nView, const int nDet)
 {
+  m_label.setLabelType (Array2dFileLabel::L_HISTORY);
   m_nView = nView;
   m_nDet = nDet;
   newProjData ();
@@ -74,15 +76,24 @@ Projections::init (const int nView, const int nDet)
 void
 Projections::initFromScanner (const Scanner& scanner)
 {
+  m_label.setLabelType (Array2dFileLabel::L_HISTORY);
   deleteProjData();
   init (scanner.nView(), scanner.nDet());
 
   m_phmLen = scanner.phmLen();
   m_rotInc = scanner.rotInc();
   m_detInc = scanner.detInc();
+  m_geometry = scanner.geometry();
+  m_focalLength = scanner.focalLength();
+  m_fieldOfView = scanner.fieldOfView();
   m_rotStart = 0;
-  m_detStart =  -scanner.radius() + (scanner.detInc() / 2);
-  m_phmLen = scanner.phmLen();
+  m_detStart =  -(scanner.detLen() / 2);
+#if 0
+  if (m_geometry == Scanner::GEOMETRY_EQUILINEAR) {
+    m_detInc /= 2;
+    cout << "Kludge: detInc /= 2 in Projections::initFromScanner" << endl;
+  }
+#endif
 }
 
 void
@@ -158,7 +169,9 @@ Projections::headerWrite (fnetorderstream& fs)
   kfloat64 _detStart = m_detStart;
   kfloat64 _detInc = m_detInc;
   kfloat64 _phmLen = m_phmLen;
-  
+  kfloat64 _fieldOfView = m_fieldOfView;
+  kfloat64 _focalLength = m_focalLength;
+
   fs.seekp(0);
   if (! fs)
     return false;
@@ -174,6 +187,8 @@ Projections::headerWrite (fnetorderstream& fs)
   fs.writeFloat64 (_detStart);
   fs.writeFloat64 (_detInc);
   fs.writeFloat64 (_phmLen);
+  fs.writeFloat64 (_focalLength);
+  fs.writeFloat64 (_fieldOfView);
   fs.writeInt16 (_year);
   fs.writeInt16 (_month);
   fs.writeInt16 (_day);
@@ -202,13 +217,12 @@ Projections::headerRead (fnetorderstream& fs)
 {
   kuint16 _hsize, _signature, _year, _month, _day, _hour, _minute, _second, _remarksize = 0;
   kuint32 _nView, _nDet, _geom;
-  kfloat64 _calcTime, _rotStart, _rotInc, _detStart, _detInc, _phmLen;
+  kfloat64 _calcTime, _rotStart, _rotInc, _detStart, _detInc, _phmLen, _focalLength, _fieldOfView;
   
   fs.seekg(0);
   if (! fs)
       return false;
 
-  off_t testPos;
   fs.readInt16 (_hsize);
   fs.readInt16 (_signature);
   fs.readInt32 (_nView);
@@ -220,6 +234,8 @@ Projections::headerRead (fnetorderstream& fs)
   fs.readFloat64 (_detStart);
   fs.readFloat64 (_detInc);
   fs.readFloat64 (_phmLen);
+  fs.readFloat64 (_focalLength);
+  fs.readFloat64 (_fieldOfView);
   fs.readInt16 (_year);
   fs.readInt16 (_month);
   fs.readInt16 (_day);
@@ -238,14 +254,15 @@ Projections::headerRead (fnetorderstream& fs)
     return false;
   }
 
-  char remarkStorage[_remarksize+1];
-  fs.read (remarkStorage, _remarksize);
+  char* pszRemarkStorage = new char [_remarksize+1];
+  fs.read (pszRemarkStorage, _remarksize);
   if (! fs) {
     sys_error (ERR_SEVERE, "Error reading remark, _remarksize = %d", _remarksize);
     return false;
   }
-  remarkStorage[_remarksize] = 0;
-  m_remark = remarkStorage;
+  pszRemarkStorage[_remarksize] = 0;
+  m_remark = pszRemarkStorage;
+  delete pszRemarkStorage;\r
 
   off_t _hsizeread = fs.tellg();
   if (!fs || _hsizeread != _hsize) {
@@ -263,6 +280,8 @@ Projections::headerRead (fnetorderstream& fs)
   m_detStart = _detStart;
   m_detInc = _detInc;
   m_phmLen = _phmLen;
+  m_focalLength = _focalLength;
+  m_fieldOfView = _fieldOfView;
   m_year = _year;
   m_month = _month;
   m_day = _day;
@@ -278,6 +297,13 @@ Projections::headerRead (fnetorderstream& fs)
   return true;
 }
 
+bool
+Projections::read (const string& filename)
+{
+  return read (filename.c_str());
+}
+
+
 bool
 Projections::read (const char* filename)
 {
@@ -303,6 +329,105 @@ Projections::read (const char* filename)
 }
 
 
+bool 
+Projections::copyViewData (const string& filename, ostream& os, int startView, int endView)
+{
+    return copyViewData (filename.c_str(), os, startView, endView);
+}
+
+bool 
+Projections::copyViewData (const char* const filename, ostream& os, int startView, int endView)
+{
+  frnetorderstream is (filename, ios::in | ios::binary);
+  kuint16 sizeHeader, signature;
+  kuint32 _nView, _nDet;
+
+  is.readInt16 (sizeHeader);
+  is.readInt16 (signature);
+  is.readInt32 (_nView);
+  is.readInt32 (_nDet);
+  int nView = _nView;
+  int nDet = _nDet;
+
+  if (signature != m_signature) {
+    sys_error (ERR_FATAL, "Illegal signature in projection file %s", filename);
+    return false;
+  }
+
+  if (startView < 0)
+      startView = 0;
+  if (startView > nView - 1)
+      startView = nView;
+  if (endView < 0 || endView > nView - 1)
+      endView = nView - 1;
+
+  if (startView > endView) { // swap if start > end
+      int tempView = endView;
+      endView = startView;
+      startView = tempView;
+  }
+
+  int sizeView = 8 /* view_angle */ + 4 /* nDet */ + (4 * nDet);
+  unsigned char* pViewData = new unsigned char [sizeView];
+
+  for (int i = startView; i <= endView; i++) {
+      is.seekg (sizeHeader + i * sizeView);
+      is.read (pViewData, sizeView);
+      os.write (pViewData, sizeView);
+      if (is.fail() || os.fail())
+         break;
+  }
+
+  delete pViewData;
+  if (is.fail()) 
+    sys_error (ERR_FATAL, "Error reading projection file");
+  if (os.fail()) 
+    sys_error (ERR_FATAL, "Error writing projection file");
+    
+  return (! (is.fail() | os.fail()));
+}
+
+bool 
+Projections::copyHeader (const string& filename, ostream& os)
+{
+    return copyHeader (filename.c_str(), os);
+}
+
+bool
+Projections::copyHeader (const char* const filename, ostream& os)
+{
+  frnetorderstream is (filename, ios::in | ios::binary);
+  kuint16 sizeHeader, signature;
+  is.readInt16 (sizeHeader);
+  is.readInt16 (signature);
+  is.seekg (0);
+  if (signature != m_signature) {
+    sys_error (ERR_FATAL, "Illegal signature in projection file %s", filename);
+    return false;
+  }
+
+  unsigned char* pHdrData = new unsigned char [sizeHeader];
+  is.read (pHdrData, sizeHeader);
+  if (is.fail()) {
+      sys_error (ERR_FATAL, "Error reading header");
+      return false;
+  }
+
+  os.write (pHdrData, sizeHeader);
+  if (os.fail()) {
+      sys_error (ERR_FATAL, "Error writing header");
+      return false;
+  }
+
+  return true;
+}
+
+bool
+Projections::write (const string& filename)
+{
+  return write (filename.c_str());
+}
+
 bool
 Projections::write (const char* filename)
 {
@@ -313,6 +438,7 @@ Projections::write (const char* filename)
     return false;
   }
 
+#ifdef HAVE_TIME
   time_t t = time(NULL);
   tm* lt = localtime(&t);
   m_year = lt->tm_year;
@@ -321,6 +447,7 @@ Projections::write (const char* filename)
   m_hour = lt->tm_hour;
   m_minute = lt->tm_min;
   m_second = lt->tm_sec;
+#endif
 
   if (! headerWrite (fs))
       return false;
@@ -366,7 +493,7 @@ Projections::detarrayRead (fnetorderstream& fs, DetectorArray& darray, const int
   darray.setViewAngle (view_angle);
   //  darray.setNDet ( nDet);
   
-  for (int i = 0; i < nDet; i++) {
+  for (unsigned int i = 0; i < nDet; i++) {
       kfloat32 detval;
       fs.readFloat32 (detval);
       detval_ptr[i] = detval;
@@ -412,7 +539,7 @@ Projections::detarrayWrite (fnetorderstream& fs, const DetectorArray& darray, co
   fs.writeFloat64 (view_angle);
   fs.writeInt32 (nDet);
 
-  for (int i = 0; i < nDet; i++) {
+  for (unsigned int i = 0; i < nDet; i++) {
     kfloat32 detval = detval_ptr[i];
     fs.writeFloat32 (detval);
   }
@@ -424,188 +551,60 @@ Projections::detarrayWrite (fnetorderstream& fs, const DetectorArray& darray, co
 }
 
 /* NAME
- *   prt_projections                   Print projections data
+ *   printProjectionData                       Print projections data
  *
  * SYNOPSIS
- *   prt_projections (proj)
- *   Projections& proj                 Projection data to be printed
+ *   printProjectionData ()
  */
 
 void
-Projections::printProjectionData (void)
+Projections::printProjectionData ()
 {
-  printf("Projections Print\n\n");
-  printf("Description: %s\n", m_remark.c_str());
-  printf("nView = %d  nDet = %d\n", m_nView, m_nDet);
-  printf("rotStart = %8.4f   rotInc = %8.4f\n", m_rotStart, m_rotInc);
-  printf("detStart = %8.4f   detInc = %8.4f\n", m_detStart, m_detInc);
+  printProjectionData (0, nView() - 1);
+}
 
+void
+Projections::printProjectionData (int startView, int endView)
+{
+  printf("Projections Data\n\n");
+  printf("Description: %s\n", m_remark.c_str());
+  printf("Geometry: %s\n", Scanner::convertGeometryIDToName (m_geometry));
+  printf("nView       = %8d           nDet = %8d\n", m_nView, m_nDet);
+  printf("focalLength = %8.4f  fieldOfView = %8.4f\n", m_focalLength, m_fieldOfView);
+  printf("rotStart    = %8.4f       rotInc = %8.4f\n", m_rotStart, m_rotInc);
+  printf("detStart    = %8.4f       detInc = %8.4f\n", m_detStart, m_detInc);
   if (m_projData != NULL) {
-      for (int ir = 0; ir < m_nView; ir++) {
-         DetectorValue* detval = m_projData[ir]->detValues();
-         for (int id = 0; id < m_projData[ir]->nDet(); id++)
-             printf("%8.4f  ", detval[id]);
-         printf("\n");
-      }
+    if (startView < 0)
+      startView = 0;
+    if (startView > m_nView - 1)
+      startView = m_nView - 1;
+    if (endView > m_nView - 1)
+      endView = m_nView - 1;
+    for (int ir = startView; ir <= endView - 1; ir++) {
+      printf("View %d: angle %f\n", ir, m_projData[ir]->viewAngle());
+      DetectorValue* detval = m_projData[ir]->detValues();
+      for (int id = 0; id < m_projData[ir]->nDet(); id++)
+       printf("%8.4f  ", detval[id]);
+      printf("\n");
+    }
   }
 }
 
 void 
-Projections::printScanInfo (void) const
+Projections::printScanInfo (ostringstream& os) const
 {
-  printf ("Number of detectors: %d\n", m_nDet);
-  printf ("    Number of views: %d\n", m_nView);
-  printf ("             Remark: %s\n", m_remark.c_str());
-  printf ("             phmLen: %f\n", m_phmLen);
-  printf ("           detStart: %f\n", m_detStart);
-  printf ("             detInc: %f\n", m_detInc);
-  printf ("           rotStart: %f\n", m_rotStart);
-  printf ("             rotInc: %f\n", m_rotInc);
+  os << "Number of detectors: " << m_nDet << "\n";
+  os << "    Number of views: " << m_nView<< "\n";
+  os << "             Remark: " << m_remark.c_str()<< "\n";
+  os << "           Geometry: " << Scanner::convertGeometryIDToName (m_geometry)<< "\n";
+  os << "       Focal Length: " << m_focalLength<< "\n";
+  os << "      Field Of View: " << m_fieldOfView<< "\n";
+  os << "             phmLen: " << m_phmLen<< "\n";
+  os << "           detStart: " << m_detStart<< "\n";
+  os << "             detInc: " << m_detInc<< "\n";
+  os << "           rotStart: " << m_rotStart<< "\n";
+  os << "             rotInc: " << m_rotInc<< "\n";
 }
 
 
 
-/* NAME
- *   Projections::reconstruct      Reconstruct Image from Projections
- *
- * SYNOPSIS
- *   im = proj.reconstruct (im, filt_type, filt_param, interp_type)
- *   IMAGE *im                 Output image
- *   int filt_type             Type of convolution filter to use
- *   double filt_param         Filter specific parameter
- *                             Currently, used only with Hamming filters
- *   int interp_type           Type of interpolation method to use
- *
- * ALGORITHM
- *
- *     Calculate one-dimensional filter in spatial domain
- *     Allocate & clear (zero) the 2d output image array
- *      For each projection view
- *         Convolve raysum array with filter
- *         Backproject raysums and add (summate) to image array
- *      end
- */
-
-bool
-Projections::reconstruct (ImageFile& im, const char* const filterName, double filt_param, const char* const filterMethodName, const char* const interpName, int interp_param, const char* const backprojectName, const int trace)
-{
-  int nview = m_nView;
-  double detInc = m_detInc;
-  int n_filteredProj = m_nDet;
-  double filteredProj [n_filteredProj];   // filtered projections
-
-#ifdef HAVE_BSPLINE_INTERP
-  int spline_order = 0, zoom_factor = 0;
-  if (interp_type == I_BSPLINE) {
-    zoom_factor = interp_param;
-    spline_order = 3;
-    zoom_factor = 3;
-    n_filteredProj = (m_nDet - 1) * (zoom_factor + 1) + 1;
-  }
-#endif
-
-  double filterBW = 1. / detInc;
-  SignalFilter filter (filterName, filterMethodName, filterBW, m_detInc, m_nDet, filt_param, "spatial", 0);
-  filter.setTraceLevel(trace);
-
-  if (filter.fail()) {
-      sys_error (ERR_SEVERE, "%s [Projections::reconstruct]", filter.failMessage().c_str());
-      return false;
-  }
-
-  if (trace)
-    cout << "Reconstruct: filter="<<filterName<< ", interp="<<interpName<<", backproject="<<backprojectName<<endl;
-
-#if HAVE_SGP
-  SGP_ID gid;
-  int nVecFilter = filter.getNFilterPoints();
-  double plot_xaxis [nVecFilter];                      // array for plotting 
-
-  if (trace > TRACE_TEXT && nVecFilter > 0)  {
-    int i;
-    double f;
-    double filterInc = filter.getFilterIncrement();
-    for (i = 0, f = filter.getFilterMin(); i < nVecFilter; i++, f += filterInc) 
-      plot_xaxis[i] = f;
-
-    if (filter.getFilter()) {
-      gid = ezplot (plot_xaxis, filter.getFilter(), nVecFilter);
-      cio_put_str ("Press any key to continue");
-      cio_kb_getc ();
-      sgp2_close (gid);
-    }
-  }
-  if (trace >= TRACE_TEXT) {
-    printf ("nview=%d, ndet=%d, det_start=%.4f, detInc=%.4f\n", m_nView, m_nDet, m_detStart, m_detInc);
-  }
-#endif  //HAVE_SGP
-
-  Backprojector bj (*this, im, backprojectName, interpName);
-  if (bj.fail()) {
-    sys_error (ERR_SEVERE, "%s [Projections::reconstruct]", bj.failMessage().c_str());
-    return false;
-  }
-
-  for (int iview = 0; iview < m_nView; iview++)  {
-    if (trace >= TRACE_TEXT) 
-      printf ("Reconstructing view %d (last = %d)\n",  iview, m_nView - 1);
-      
-    DetectorArray& darray = getDetectorArray (iview);
-    DetectorValue* detval = darray.detValues();
-
-    filter.filterSignal (detval, filteredProj);
-
-#ifdef HAVE_SGP
-    if (trace >= TRACE_PLOT)  {
-      ezset  ("clear.");
-      ezset  ("xticks major 5.");
-      ezset  ("xlabel ");
-      ezset  ("ylabel ");
-      ezset  ("xlength .5.");
-      ezset  ("box.");
-      ezset  ("grid.");
-      ezset  ("ufinish yes.");
-      ezplot (detval, plot_xaxis, m_nDet);
-      ezset  ("clear.");
-      ezset  ("xticks major 5.");
-      ezset  ("xlabel ");
-      ezset  ("ylabel ");
-      ezset  ("ustart yes.");
-      ezset  ("xporigin .5.");
-      ezset  ("xlength .5.");
-      ezset ("box");
-      ezset ("grid");
-      gid = ezplot (filteredProj, plot_xaxis, m_nDet);
-    }
-#endif  //HAVE_SGP
-
-#ifdef HAVE_BSPLINE_INTERP
-    if (interp_type == I_BSPLINE) 
-       bspline (m_nDet, zoom_factor, spline_order, filteredProj, filteredProj);
-    
-#ifdef HAVE_SGP
-    if (trace >= TRACE_PLOT && interp_type == I_BSPLINE) {
-       bspline (m_nDet, zoom_factor, spline_order, filteredProj, filteredProj);
-      ezplot_1d (filteredProj, n_filteredProj);
-    }
-#endif
-#endif
-
-    bj.BackprojectView (filteredProj, darray.viewAngle());
-
-#ifdef HAVE_SGP
-    if (trace >= TRACE_PLOT) {
-      char str[256];
-      printf ("Do you want to exit with current pic (y/n) -- ");
-      fgets(str, sizeof(str), stdin);
-      sgp2_close (sgp2_get_active_win());
-      if (tolower(str[0]) == 'y') {
-       break;
-      }
-    } 
-#endif  //HAVE_SGP
-  }
-
-  return true;
-}
-