CTL  0.6.1
Computed Tomography Library
backprojectorbase.h
Go to the documentation of this file.
1 #ifndef CTL_BACKPROJECTORBASE_H
2 #define CTL_BACKPROJECTORBASE_H
3 
5 #include "acquisition/acquisitionsetup.h"
6 #include "mat/matrix_types.h"
7 #include "ocl/pinnedmem.h"
8 
9 namespace CTL {
10 
11 class SingleViewGeometry;
12 class FullGeometry;
13 
14 namespace OCL {
15 
87 {
89  Q_GADGET
90 
91  // abstract interface
92  public: void configure(const AcquisitionSetup& setup) override;
93 
94 public:
95  using ProjectionWeights = std::vector<std::vector<float>>;
96  enum class WeightingType { DistanceWeightsOnly, GeometryFactors };
97  Q_ENUM(WeightingType)
98 
99  // AbstractReconstructor interface
100  bool reconstructToPlain(const ProjectionDataView& projections,
101  VoxelVolume<float>& targetVolume) override;
102 
103  bool isConfigConsistentWith(const ProjectionDataView& projections) const;
104  bool customWeightsValid(const ProjectionDataView& projections) const;
105 
106  const AcquisitionSetup& setup() const;
107  WeightingType weightingType() const;
108  void setWeightingType(WeightingType weigthType);
109  void setCustomWeights(ProjectionWeights weights);
110 
111  // SerializationInterface interface
112  QVariant parameter() const override;
113  void setParameter(const QVariant& parameter) override;
114 
115 protected:
116  BackprojectorBase(cl::Kernel* backsmearKernel, WeightingType weightingType);
117 
118  virtual void transferAdditionalData(uint startView, uint nbViews);
119  virtual float weighting(uint view, uint module) const;
120 
121  // reset kernel
122  void setBacksmearKernel(cl::Kernel* backsmearKernel);
123 
124  cl::Kernel* backsmearKernel() const;
125  const cl::CommandQueue& queue() const;
126 
127  float precomputedGeometryFactor(uint view, uint module) const;
128 
129  static uint viewsPerBlock(const ProjectionData::Dimensions& projDim);
130  static QMetaEnum metaEnum();
131 
132 private:
133  // the setup (copied via `configure`)
134  AcquisitionSetup _setup;
135 
136  // weighting type
137  WeightingType _weightingType;
138 
139  // precomputed geometry factors
140  ProjectionWeights _precompGeomFactors;
141 
142  // custom weights
143  ProjectionWeights _customWeights;
144 
145  // OCL related
146  cl::CommandQueue _queue;
147 
148  cl::Kernel* _kernelBacksmear;
149  cl::Kernel* _kernelNullifier;
150 
151  // regular buffers
152  cl::Buffer _volCornerBuffer;
153  cl::Buffer _voxSizeBuffer;
154  cl::Buffer _pMatBuffer;
155  cl::Buffer _sliceBuffer;
156 
157  // pinned memory
158  std::unique_ptr<PinnedImg3DHostWrite> _projBuf;
159 
160  // dimension info
161  std::array<uint, 3> _nbVoxels{ 0, 0, 0 };
162  Vector3x1 _voxelSize{ 0.0 };
163  Vector3x1 _corner{ 0.0 };
164  cl::size_t<3> _zeroVec, _projBufferDim;
165  size_t _bytesLocPMatMem;
166  uint _nbModules, _viewsPerBlock;
167 
168  // helper functions
169  void createBuffers();
170  void createCommandQueue();
171  void createPinnedMemory();
172  void freeGPUMemory();
173  void initNullifierKernel();
174  void readoutResult(VoxelVolume<float>& targetVolume, uint z);
175  void setDimensions(const ProjectionData::Dimensions& projDim,
176  const VoxelVolume<float>& targetVolume);
177  void setFixedKernelArgs();
178  void startBacksmearKernel(uint slice, uint view);
179  void startNullifierKernel();
180  void transferProjectionData(const ProjectionDataView& projections,
181  uint startView, uint nbViews);
182  void transferProjectionMatrices(const float* src, uint startView, uint nbViews);
183  void transferSlice(const VoxelVolume<float>& targetVolume, uint z);
184  void writeFixedBuffers();
185 
186  void precomputeGeometryFactors(float volumeOfSingleVoxel, const FullGeometry& pMats);
187  static std::vector<float> computeGeometryFactors(float volumeOfSingleVoxel,
188  const SingleViewGeometry& modPMats);
189 };
190 
191 } // namespace OCL
192 
198 using BackprojectorWeighting = OCL::BackprojectorBase::WeightingType;
199 
200 namespace assist {
201 bool isConsistentPair(const AcquisitionSetup& setup, const ProjectionDataView& projections);
202 
206 template<typename T>
207 QVariantList vectorToVariantList(const std::vector<T>& vector)
208 {
209  QVariantList ret;
210  ret.reserve(static_cast<int>(vector.size()));
211 
212  for(const auto& val : vector)
213  ret.append(val);
214 
215  return ret;
216 }
217 
223 template<typename T>
224 std::vector<T> variantListToVector(const QVariantList& list)
225 {
226  std::vector<T> ret;
227  ret.reserve(list.size());
228 
229  for(const auto& val : list)
230  ret.push_back(qvariant_cast<T>(val));
231 
232  return ret;
233 }
234 }
235 
236 } // namespace CTL
237 
240 #endif // CTL_BACKPROJECTORBASE_H
WeightingType weightingType() const
Returns the weighting type of this instance.
Definition: backprojectorbase.cpp:109
void transferProjectionData(const ProjectionDataView &projections, uint startView, uint nbViews)
Writes (non-blocking) projection data into the corresponding OpenCL buffer.
Definition: backprojectorbase.cpp:604
void readoutResult(VoxelVolume< float > &targetVolume, uint z)
Reads out the data current present in the slice buffer.
Definition: backprojectorbase.cpp:454
void setBacksmearKernel(cl::Kernel *backsmearKernel)
Sets the backprojection kernel to be used to backsmearKernel.
Definition: backprojectorbase.cpp:529
void createBuffers()
Creates OpenCL buffers for volume corner, voxel size, projection matrices and the slice data.
Definition: backprojectorbase.cpp:378
static std::vector< float > computeGeometryFactors(float volumeOfSingleVoxel, const SingleViewGeometry &modPMats)
Computes (and returns) geometry factors for all modules in modPMats.
Definition: backprojectorbase.cpp:694
void precomputeGeometryFactors(float volumeOfSingleVoxel, const FullGeometry &pMats)
Pre-computes geometry factors for all projection matrices in pMats.
Definition: backprojectorbase.cpp:713
bool isConfigConsistentWith(const ProjectionDataView &projections) const
Returns true if the currently configured setup is consistent with projections.
Definition: backprojectorbase.cpp:305
void createPinnedMemory()
Creates OpenCL 3D image (using pinned memory approach) for projection data.
Definition: backprojectorbase.cpp:394
void configure(const AcquisitionSetup &setup) override
Passes the AcquisitionSetup that describes the setting in which projections that shall be backproject...
Definition: backprojectorbase.cpp:59
void createCommandQueue()
Creates the OpenCL command queue.
Definition: backprojectorbase.cpp:363
Holds a CTSystem together with the information about the system settings for all views from which pro...
Definition: acquisitionsetup.h:175
The ProjectionDataView class is a read-only wrapper for a reference to ProjectionData.
Definition: projectiondataview.h:51
virtual void transferAdditionalData(uint startView, uint nbViews)
Placeholder method to be used to transfer data for processing of nbViews views beginning with startVi...
Definition: backprojectorbase.cpp:43
void transferSlice(const VoxelVolume< float > &targetVolume, uint z)
Writes (non-blocking) data of slice z from targetVolume into the corresponding OpenCL buffer.
Definition: backprojectorbase.cpp:590
void startBacksmearKernel(uint slice, uint view)
Sets the slice index and view id argument of the backprojection kernel and starts it.
Definition: backprojectorbase.cpp:537
static QMetaEnum metaEnum()
QMetaEnum object used to decode/encode subset order enum values.
Definition: backprojectorbase.cpp:682
The AbstractReconstructor class defined the interface for reconstruction types.
Definition: abstractreconstructor.h:163
virtual float weighting(uint view, uint module) const
Returns the weighting factor for data from detector module module in view view.
Definition: backprojectorbase.cpp:87
void setDimensions(const ProjectionData::Dimensions &projDim, const VoxelVolume< float > &targetVolume)
Sets all relevant dimension spec for this instance based on the projection dimensions projDim and the...
Definition: backprojectorbase.cpp:477
void setFixedKernelArgs()
Sets all fixed (i.e. contant for the entire backprojection operation) kernel arguments.
Definition: backprojectorbase.cpp:501
void transferProjectionMatrices(const float *src, uint startView, uint nbViews)
Writes (non-blocking) projection matrices into the corresponding OpenCL buffer.
Definition: backprojectorbase.cpp:579
void setCustomWeights(ProjectionWeights weights)
Sets custom weights to weights.
Definition: backprojectorbase.cpp:122
QVariantList vectorToVariantList(const std::vector< T > &vector)
Converts data from vector into a QVariantList.
Definition: backprojectorbase.h:207
void initNullifierKernel()
Initializes the OpenCL kernel that nullifies slice data.
Definition: backprojectorbase.cpp:418
void writeFixedBuffers()
Writes (blocking) all data into the fixed (i.e. contant for the entire backprojection operation) Open...
Definition: backprojectorbase.cpp:559
Struct that holds the dimensions of a ProjectionData object.
Definition: projectiondata.h:118
const AcquisitionSetup & setup() const
Returns a reference-to-const to the current AcquisitionSetup set via configure().
Definition: backprojectorbase.cpp:96
void startNullifierKernel()
Starts the nullifier kernel.
Definition: backprojectorbase.cpp:549
bool reconstructToPlain(const ProjectionDataView &projections, VoxelVolume< float > &targetVolume) override
Backprojects data from projections into targetVolume and returns true on success.
Definition: backprojectorbase.cpp:204
#define CTL_TYPE_ID(newIndex)
Definition: serializationinterface.h:189
void setParameter(const QVariant &parameter) override
Sets the parameters of this instance based on the passed QVariant parameter.
Definition: backprojectorbase.cpp:164
unsigned int uint
Qt style alias for unsigned int.
Definition: modulelayout.h:6
void freeGPUMemory()
Cleans up the GPU memory allocated by createBuffers() and createPinnedMemory().
Definition: backprojectorbase.cpp:402
float precomputedGeometryFactor(uint view, uint module) const
Returns a (pre-computed) geometry factor for detector module module in view view.
Definition: backprojectorbase.cpp:727
cl::Kernel * backsmearKernel() const
Returns a pointer to the cl::Kernel object of this instances OpenCL backprojection kernel.
Definition: backprojectorbase.cpp:355
bool isConsistentPair(const AcquisitionSetup &setup, const ProjectionDataView &projections)
Returns true if the specifications in setup are consistent with projections.
Definition: backprojectorbase.cpp:749
static uint viewsPerBlock(const ProjectionData::Dimensions &projDim)
Computes how many views of data with dimensions given by projDim can be processed within one block (i...
Definition: backprojectorbase.cpp:639
The BackprojectorBase class is a helper class from which OpenCL implementations of cone-beam backproj...
Definition: backprojectorbase.h:86
bool customWeightsValid(const ProjectionDataView &projections) const
Returns true if the custom weights set to this instance are consistent with projections.
Definition: backprojectorbase.cpp:326
const cl::CommandQueue & queue() const
Returns a reference-to-const to the OpenCL command queue used by this instance.
Definition: backprojectorbase.cpp:104
QVariant parameter() const override
Returns the parameters of this instance as a QVariant.
Definition: backprojectorbase.cpp:135
void setWeightingType(WeightingType weigthType)
Sets the weighting type of this instance to weightType.
Definition: backprojectorbase.cpp:114
BackprojectorBase(cl::Kernel *backsmearKernel, WeightingType weightingType)
Constructs BackprojectorBase with the OpenCL kernel backsmearKernel using weighting defined by weight...
Definition: backprojectorbase.cpp:20
std::vector< T > variantListToVector(const QVariantList &list)
Converts data from list vector into an std::vector<T>.
Definition: backprojectorbase.h:224