File: reconstruct.cpp

package info (click to toggle)
ctsim 6.0.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,868 kB
  • sloc: cpp: 26,967; sh: 7,782; ansic: 1,256; perl: 296; makefile: 148
file content (240 lines) | stat: -rw-r--r-- 8,153 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*****************************************************************************
** FILE IDENTIFICATION
**
**   Name:         reconstruct.cpp         Reconstruction class
**   Programmer:   Kevin Rosenberg
**   Date Started: Aug 84
**
**  This is part of the CTSim program
**  Copyright (c) 1983-2009 Kevin Rosenberg
**
**  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
**  published by the Free Software Foundation.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

#include "ct.h"


/* NAME
 *   Reconstructor::Reconstructor      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
 */


Reconstructor::Reconstructor (const Projections& rProj, ImageFile& rIF, const char* const filterName,
                              double filt_param, const char* const filterMethodName, const int zeropad,
                              const char* filterGenerationName, const char* const interpName,
                              int interpFactor, const char* const backprojectName, const int iTrace,
                              ReconstructionROI* pROI, bool bRebinToParallel, SGP* pSGP)
  : m_rOriginalProj(rProj),
    m_pProj(bRebinToParallel ? m_rOriginalProj.interpolateToParallel() : &m_rOriginalProj),
    m_rImagefile(rIF), m_pProcessSignal(0), m_pBackprojector(0),
    m_iTrace(iTrace), m_bRebinToParallel(bRebinToParallel), m_bFail(false), m_adPlotXAxis(0)
{
  m_nFilteredProjections = m_pProj->nDet() * interpFactor;

#ifdef HAVE_BSPLINE_INTERP
  int spline_order = 0, zoom_factor = 0;
  if (interp_type == I_BSPLINE) {
    zoom_factor = interpFactor;
    spline_order = 3;
    zoom_factor = 3;
    m_nFilteredProjections = (m_nDet - 1) * (zoom_factor + 1) + 1;
  }
#endif

  double filterBW = 1. / m_pProj->detInc();
  m_pProcessSignal = new ProcessSignal (filterName, filterMethodName, filterBW, m_pProj->detInc(),
    m_pProj->nDet(), filt_param, "spatial", filterGenerationName, zeropad, interpFactor, iTrace,
    m_pProj->geometry(), m_pProj->focalLength(), m_pProj->sourceDetectorLength(), pSGP);

  if (m_pProcessSignal->fail()) {
    m_bFail = true;
    m_strFailMessage = "Error creating ProcessSignal: ";
    m_strFailMessage += m_pProcessSignal->failMessage();
    delete m_pProcessSignal; m_pProcessSignal = NULL;
    return;
  }

  m_pBackprojector = new Backprojector (*m_pProj, m_rImagefile, backprojectName, interpName, interpFactor, pROI);
  if (m_pBackprojector->fail()) {
    m_bFail = true;
    m_strFailMessage = "Error creating backprojector: ";
    m_strFailMessage += m_pBackprojector->failMessage();
    delete m_pBackprojector; m_pBackprojector = NULL;
    delete m_pProcessSignal; m_pProcessSignal = NULL;
    return;
  }

#ifdef HAVE_SGP
  m_adPlotXAxis = new double [m_pProj->nDet()];
  double x = - ((m_pProj->nDet() - 1) / 2) * m_pProj->detInc();
  double xInc = m_pProj->detInc();

  for (int i = 0; i < m_pProj->nDet(); i++, x += xInc)
    m_adPlotXAxis[i] = x;
#endif
}

Reconstructor::~Reconstructor ()
{
  if (m_bRebinToParallel)
    delete m_pProj;

  delete m_pBackprojector;
  delete m_pProcessSignal;
  delete m_adPlotXAxis;
}


void
Reconstructor::plotFilter (SGP* pSGP)
{
#ifdef HAVE_SGP
  int nVecFilter = m_pProcessSignal->getNFilterPoints();
  double* adPlotXAxis = new double [nVecFilter];

  if (nVecFilter > 0 && pSGP)  {
    double f = m_pProcessSignal->getFilterMin();
    double filterInc = m_pProcessSignal->getFilterIncrement();
    for (int i = 0; i < nVecFilter; i++, f += filterInc)
      adPlotXAxis[i] = f;

    if (m_pProcessSignal->getFilter()) {
      EZPlot ezplot;

      ezplot.ezset ("title Filter Response");
      ezplot.addCurve (adPlotXAxis, m_pProcessSignal->getFilter(), nVecFilter);
      ezplot.plot (pSGP);
    }
  }
  delete adPlotXAxis;
#endif
}


void
Reconstructor::reconstructAllViews ()
{
  reconstructView (0, m_pProj->nView());
  postProcessing();
}

void
Reconstructor::postProcessing()
{
  m_pBackprojector->PostProcessing();
}


void
Reconstructor::reconstructView (int iStartView, int iViewCount, SGP* pSGP, bool bBackprojectView, double dGraphWidth)
{
  double* adFilteredProj = new double [m_nFilteredProjections];   // filtered projections

  if (iViewCount <= 0)
    iViewCount = m_pProj->nView() - iStartView;

  for (int iView = iStartView; iView < (iStartView + iViewCount); iView++)  {
    if (m_iTrace == Trace::TRACE_CONSOLE)
                std::cout <<"Reconstructing view " << iView << " (last = " << m_pProj->nView() - 1 << ")\n";

    const DetectorArray& rDetArray = m_pProj->getDetectorArray (iView);
    const DetectorValue* detval = rDetArray.detValues();

    m_pProcessSignal->filterSignal (detval, adFilteredProj);

#ifdef HAVE_BSPLINE_INTERP
    if (interp_type == I_BSPLINE)
        bspline (m_pProj->nDet(), zoom_factor, spline_order, adFilteredProj, adFilteredProj);

#ifdef HAVE_SGP
    if (trace >= Trace::TRACE_PLOT && interp_type == I_BSPLINE && pSGP) {
        bspline (m_pProj->nDet(), zoom_factor, spline_order, adFilteredProj, adFilteredProj);
      ezplot_1d (adFilteredProj, m_nFilteredProjections);
    }
#endif
#endif

        if (bBackprojectView)
      m_pBackprojector->BackprojectView (adFilteredProj, rDetArray.viewAngle());

#ifdef HAVE_SGP
    if (m_iTrace >= Trace::TRACE_PLOT && pSGP) {
      EZPlot ezplotProj;

      std::ostringstream osXLength;
      osXLength << "xlength " << dGraphWidth;

      ezplotProj.ezset ("clear");
      ezplotProj.ezset ("title Raw Projection");
      ezplotProj.ezset ("xticks major 5");
      ezplotProj.ezset ("yticks major 5");
      ezplotProj.ezset ("xlabel ");
      ezplotProj.ezset ("ylabel ");
      ezplotProj.ezset ("yporigin 0.55");
      ezplotProj.ezset ("ylength 0.45");
      ezplotProj.ezset (osXLength.str().c_str());
      ezplotProj.ezset ("box.");
      ezplotProj.ezset ("grid.");
#if 0  // workaround c++ optimizer bug, now disabled by using /O1 in code
      double* pdDetval = new double [m_pProj->nDet()];
      for (unsigned int id = 0; id < m_pProj->nDet(); id++) {
        pdDetval[id] = detval[id];
      }
      ezplotProj.addCurve (m_adPlotXAxis, pdDetval, m_pProj->nDet());
      delete pdDetval;
#else
      ezplotProj.addCurve (m_adPlotXAxis, detval, m_pProj->nDet());
#endif
      pSGP->setTextPointSize (9);
      ezplotProj.plot (pSGP);

      ezplotProj.ezset ("clear");
      ezplotProj.ezset ("title Filtered Projection");
      ezplotProj.ezset ("xticks major 5");
      ezplotProj.ezset ("xlabel ");
      ezplotProj.ezset ("ylabel ");
      ezplotProj.ezset ("yticks major 5");
      ezplotProj.ezset ("yporigin 0.10");
      ezplotProj.ezset ("ylength 0.45");
      ezplotProj.ezset (osXLength.str().c_str());
      ezplotProj.ezset ("box");
      ezplotProj.ezset ("grid");
      ezplotProj.addCurve (m_adPlotXAxis, adFilteredProj,  m_nFilteredProjections);
      pSGP->setTextPointSize (9);
      ezplotProj.plot (pSGP);

}
#endif  //HAVE_SGP
  }

  delete adFilteredProj;
}