Xmipp  v3.23.11-Nereus
Public Member Functions | Static Public Member Functions | List of all members
Alignment::ShiftCorrEstimator< T > Class Template Reference

#include <shift_corr_estimator.h>

Inheritance diagram for Alignment::ShiftCorrEstimator< T >:
Inheritance graph
[legend]
Collaboration diagram for Alignment::ShiftCorrEstimator< T >:
Collaboration graph
[legend]

Public Member Functions

 ShiftCorrEstimator ()
 
virtual ~ShiftCorrEstimator ()
 
void release () override
 
void init2D (const std::vector< HW *> &hw, AlignType type, const FFTSettings< T > &dims, size_t maxShift, bool includingBatchFT, bool includingSingleFT, bool allowDataOverwrite) override
 
void load2DReferenceOneToN (const std::complex< T > *ref) override
 
void load2DReferenceOneToN (const T *ref) override
 
void computeShift2DOneToN (T *others) override
 
void computeCorrelations2DOneToN (std::complex< T > *inOut, bool center) override
 
void computeCorrelations2DOneToN (const HW &hw, std::complex< T > *inOut, const std::complex< T > *ref, const Dimensions &dims, bool center) override
 
HWgetHW () const override
 
template<bool CENTER>
void sComputeCorrelations2DOneToN (const HW &hw, std::complex< T > *__restrict inOut, const std::complex< T > *__restrict ref, const Dimensions &__restrict dims)
 
- Public Member Functions inherited from Alignment::AShiftCorrEstimator< T >
 AShiftCorrEstimator ()
 
 AShiftCorrEstimator (const AShiftCorrEstimator &)=delete
 
 AShiftCorrEstimator (const AShiftCorrEstimator &&)=delete
 
virtual ~AShiftCorrEstimator ()
 
AShiftCorrEstimatoroperator= (const AShiftCorrEstimator &)=delete
 
AShiftCorrEstimatoroperator= (const AShiftCorrEstimator &&)=delete
 
- Public Member Functions inherited from Alignment::AShiftEstimator< T >
 AShiftEstimator ()
 
 AShiftEstimator (const AShiftEstimator &)=delete
 
 AShiftEstimator (const AShiftEstimator &&)=delete
 
virtual ~AShiftEstimator ()
 
AShiftEstimatoroperator= (const AShiftEstimator &)=delete
 
AShiftEstimatoroperator= (const AShiftEstimator &&)=delete
 
std::vector< Point2D< float > > getShifts2D ()
 
constexpr bool isInitialized () const
 
constexpr Dimensions getDimensions () const
 
constexpr AlignType getAlignType () const
 

Static Public Member Functions

static std::vector< Point2D< float > > computeShifts2DOneToN (const CPU &cpu, std::complex< T > *othersF, T *othersS, std::complex< T > *ref, const FFTSettings< T > &settings, void *plan, size_t maxShift)
 
static void sComputeCorrelations2DOneToN (const HW &hw, std::complex< T > *inOut, const std::complex< T > *ref, const Dimensions &dims, bool center)
 
template<bool CENTER>
static void sComputeCorrelations2DOneToN (const HW &hw, std::complex< T > *inOut, const std::complex< T > *ref, const Dimensions &dims)
 

Additional Inherited Members

- Protected Member Functions inherited from Alignment::AShiftCorrEstimator< T >
virtual void init2D (AlignType type, const FFTSettings< T > &dims, size_t maxShift, bool includingBatchFT, bool includingSingleFT, bool allowDataOverwrite)
 
void init2D (const std::vector< HW *> &hw, AlignType type, const Dimensions &dims, size_t batch, size_t maxShift)
 
- Protected Member Functions inherited from Alignment::AShiftEstimator< T >
virtual void init2D (AlignType type, const Dimensions &dims, size_t batch, size_t maxShift)
 
- Protected Attributes inherited from Alignment::AShiftCorrEstimator< T >
FFTSettings< T > * m_settingsInv
 
size_t m_centerSize
 
bool m_includingBatchFT
 
bool m_includingSingleFT
 
bool m_is_ref_FD_loaded
 
bool m_allowDataOverwrite
 
- Protected Attributes inherited from Alignment::AShiftEstimator< T >
AlignType m_type
 
const Dimensionsm_dims
 
size_t m_batch
 
size_t m_maxShift
 
std::vector< Point2D< float > > m_shifts2D
 
bool m_is_ref_loaded
 
bool m_is_shift_computed
 
bool m_isInit
 

Detailed Description

template<typename T>
class Alignment::ShiftCorrEstimator< T >

Definition at line 42 of file shift_corr_estimator.h.

Constructor & Destructor Documentation

◆ ShiftCorrEstimator()

template<typename T>
Alignment::ShiftCorrEstimator< T >::ShiftCorrEstimator ( )
inline

Definition at line 44 of file shift_corr_estimator.h.

44  {
45  setDefault();
46  }

◆ ~ShiftCorrEstimator()

template<typename T>
virtual Alignment::ShiftCorrEstimator< T >::~ShiftCorrEstimator ( )
inlinevirtual

Definition at line 48 of file shift_corr_estimator.h.

48  {
49  release();
50  }

Member Function Documentation

◆ computeCorrelations2DOneToN() [1/2]

template<typename T >
void Alignment::ShiftCorrEstimator< T >::computeCorrelations2DOneToN ( std::complex< T > *  inOut,
bool  center 
)
overridevirtual

Implements Alignment::AShiftCorrEstimator< T >.

Definition at line 141 of file shift_corr_estimator.cpp.

142  {
143  bool isReady = (this->m_isInit && (AlignType::OneToN == this->m_type) && this->m_is_ref_FD_loaded);
144 
145  if ( ! isReady) {
146  REPORT_ERROR(ERR_LOGIC_ERROR, "Not ready to execute. Call init() before");
147  }
148 
150  *m_cpu,
151  inOut, m_single_FD,
152  this->m_settingsInv->fDim(),
153  center);
154 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
static void sComputeCorrelations2DOneToN(const HW &hw, std::complex< T > *inOut, const std::complex< T > *ref, const Dimensions &dims, bool center)
Some logical error in the pipeline.
Definition: xmipp_error.h:147

◆ computeCorrelations2DOneToN() [2/2]

template<typename T >
void Alignment::ShiftCorrEstimator< T >::computeCorrelations2DOneToN ( const HW hw,
std::complex< T > *  inOut,
const std::complex< T > *  ref,
const Dimensions dims,
bool  center 
)
overridevirtual

Implements Alignment::AShiftCorrEstimator< T >.

Definition at line 157 of file shift_corr_estimator.cpp.

162  {
163  const CPU *cpu;
164  try {
165  cpu = &dynamic_cast<const CPU&>(hw);
166  } catch (std::bad_cast&) {
167  REPORT_ERROR(ERR_ARG_INCORRECT, "Instance of CPU expected");
168  }
169  return sComputeCorrelations2DOneToN(*cpu, inOut, ref, dims, center);
170 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Incorrect argument received.
Definition: xmipp_error.h:113
Definition: cpu.h:37
static void sComputeCorrelations2DOneToN(const HW &hw, std::complex< T > *inOut, const std::complex< T > *ref, const Dimensions &dims, bool center)

◆ computeShift2DOneToN()

template<typename T >
void Alignment::ShiftCorrEstimator< T >::computeShift2DOneToN ( T *  others)
overridevirtual

Implements Alignment::AShiftEstimator< T >.

Definition at line 212 of file shift_corr_estimator.cpp.

213  {
214  bool isReady = (this->m_isInit && (AlignType::OneToN == this->m_type)
215  && this->m_is_ref_FD_loaded && this->m_includingBatchFT);
216 
217  if ( ! isReady) {
218  REPORT_ERROR(ERR_LOGIC_ERROR, "Not ready to execute. Call init() before");
219  }
220 
221  // reserve enough space for shifts
222  this->m_shifts2D.reserve(this->m_settingsInv->fDim().n());
223  // process signals
224  for (size_t offset = 0; offset < this->m_settingsInv->fDim().n(); offset += this->m_settingsInv->batch()) {
225  // how many signals to process
226  size_t toProcess = std::min(this->m_settingsInv->batch(), this->m_settingsInv->fDim().n() - offset);
227 
228  T *batchStart;
229  if (toProcess == this->m_settingsInv->batch()) {
230  // we process whole batch, so we don't need to copy data
231  batchStart = others + offset * this->m_settingsInv->sDim().xyPadded();
232  } else {
233  assert(this->m_settingsInv->batch() <= this->m_settingsInv->sDim().n());
234  // less than 'batch' signals are left, so we need to process last 'batch'
235  // signals to avoid invalid memory access
236  batchStart = others
237  + (this->m_settingsInv->sDim().n() - this->m_settingsInv->batch())
238  * this->m_settingsInv->sDim().xy();
239  }
240 
241  // perform FT
242  FFTwT<T>::fft(m_batchToFD, batchStart, m_batch_FD);
243 
244  // compute shifts
245  auto shifts = computeShifts2DOneToN(
246  *m_cpu,
247  m_batch_FD,
248  m_batch_SD,
249  m_single_FD,
250  this->m_settingsInv->createBatch(), // always process whole batch, as we do it to avoid copying memory
251  m_batchToSD,
252  this->m_maxShift);
253 
254  // append shifts to existing results
255  this->m_shifts2D.insert(this->m_shifts2D.end(),
256  // in case of the last iteration, take only the shifts we actually need
257  shifts.begin() + this->m_settingsInv->batch() - toProcess,
258  shifts.end());
259  }
260 
261  // update state
262  this->m_is_shift_computed = true;
263 }
void min(Image< double > &op1, const Image< double > &op2)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
std::vector< Point2D< float > > m_shifts2D
std::complex< T > * fft(T *inOut)
Definition: fftwT.cpp:204
static std::vector< Point2D< float > > computeShifts2DOneToN(const CPU &cpu, std::complex< T > *othersF, T *othersS, std::complex< T > *ref, const FFTSettings< T > &settings, void *plan, size_t maxShift)
Some logical error in the pipeline.
Definition: xmipp_error.h:147

◆ computeShifts2DOneToN()

template<typename T >
std::vector< Point2D< float > > Alignment::ShiftCorrEstimator< T >::computeShifts2DOneToN ( const CPU cpu,
std::complex< T > *  othersF,
T *  othersS,
std::complex< T > *  ref,
const FFTSettings< T > &  settings,
void *  plan,
size_t  maxShift 
)
static

Definition at line 266 of file shift_corr_estimator.cpp.

273  {
274  // we need even input in order to perform the shift (in FD, while correlating) properly
275  assert(0 == (settings.sDim().x() % 2));
276  assert(0 == (settings.sDim().y() % 2));
277  assert(1 == settings.sDim().zPadded());
278 
279  // correlate signals and shift FT so that it will be centered after IFT
281  othersF, ref,
282  settings.fDim(), true);
283 
284  // perform IFT
285  FFTwT<T>::ifft(plan, othersF, othersS);
286 
287  // compute shifts
288  auto maxIndices = std::vector<float>(settings.sDim().n(), -1.f);
290  othersS, maxIndices.data(), nullptr, maxShift);
291  // convert absolute indices to 2D position
292  // FIXME DS extract this to some indexing utils or sth
293  int cX = settings.sDim().x() / 2;
294  int cY = settings.sDim().y() / 2;
295  auto result = std::vector<Point2D<float>>();
296  const int x = settings.sDim().x();
297  for (auto i : maxIndices) {
298  result.emplace_back(((int)i % x) - cX, ((int)i / x) - cY);
299  }
300  return result;
301 }
constexpr Dimensions fDim() const
Definition: fft_settings.h:82
doublereal * x
#define i
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
constexpr size_t zPadded() const
Definition: dimensions.h:73
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
constexpr Dimensions sDim() const
Definition: fft_settings.h:78
T * ifft(std::complex< T > *inOut)
Definition: fftwT.cpp:252
static void sFindMax2DAroundCenter(const CPU &cpu, const Dimensions &dims, const T *data, float *positions, T *values, size_t maxDist)
static void sComputeCorrelations2DOneToN(const HW &hw, std::complex< T > *inOut, const std::complex< T > *ref, const Dimensions &dims, bool center)

◆ getHW()

template<typename T>
HW& Alignment::ShiftCorrEstimator< T >::getHW ( ) const
inlineoverridevirtual

Implements Alignment::AShiftEstimator< T >.

Definition at line 102 of file shift_corr_estimator.h.

102  {
103  return *m_cpu;
104  }

◆ init2D()

template<typename T >
void Alignment::ShiftCorrEstimator< T >::init2D ( const std::vector< HW *> &  hw,
AlignType  type,
const FFTSettings< T > &  dims,
size_t  maxShift,
bool  includingBatchFT,
bool  includingSingleFT,
bool  allowDataOverwrite 
)
overridevirtual

Implements Alignment::AShiftCorrEstimator< T >.

Definition at line 33 of file shift_corr_estimator.cpp.

36  {
37  if (1 != hw.size()) {
38  REPORT_ERROR(ERR_ARG_INCORRECT, "A single CPU thread expected");
39  }
40  release();
41  try {
42  m_cpu = dynamic_cast<CPU*>(hw.at(0));
43  } catch (std::bad_cast&) {
44  REPORT_ERROR(ERR_ARG_INCORRECT, "Instance of CPU expected");
45  }
46 
47  AShiftCorrEstimator<T>::init2D(type, settings, maxShift,
48  includingBatchFT, includingSingleFT, allowDataOverwrite);
49 
50  this->m_isInit = true;
51 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual void init2D(const std::vector< HW *> &hw, AlignType type, const FFTSettings< T > &dims, size_t maxShift, bool includingBatchFT, bool includingSingleFT, bool allowDataOverwrite)=0
viol type
Incorrect argument received.
Definition: xmipp_error.h:113
Definition: cpu.h:37

◆ load2DReferenceOneToN() [1/2]

template<typename T >
void Alignment::ShiftCorrEstimator< T >::load2DReferenceOneToN ( const std::complex< T > *  ref)
overridevirtual

Implements Alignment::AShiftCorrEstimator< T >.

Definition at line 125 of file shift_corr_estimator.cpp.

125  {
126  auto isReady = (this->m_isInit && (AlignType::OneToN == this->m_type));
127  if ( ! isReady) {
128  REPORT_ERROR(ERR_LOGIC_ERROR, "Not ready to load a reference signal");
129  }
130 
131  // simply remember the pointer. Expect that nobody will change it meanwhile
132  // We won't change it, but since generally speaking, we do change this pointer
133  // remove the const
134  m_single_FD = const_cast<std::complex<T>*>(ref);
135 
136  // update state
137  this->m_is_ref_FD_loaded = true;
138 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Some logical error in the pipeline.
Definition: xmipp_error.h:147

◆ load2DReferenceOneToN() [2/2]

template<typename T >
void Alignment::ShiftCorrEstimator< T >::load2DReferenceOneToN ( const T *  ref)
overridevirtual

Implements Alignment::AShiftEstimator< T >.

Definition at line 54 of file shift_corr_estimator.cpp.

54  {
55  auto isReady = (this->m_isInit && (AlignType::OneToN == this->m_type) && this->m_includingSingleFT);
56  if ( ! isReady) {
57  REPORT_ERROR(ERR_LOGIC_ERROR, "Not ready to load a reference signal");
58  }
59 
60  // perform FT
61  FFTwT<T>::fft(m_singleToFD, ref, m_single_FD);
62 
63  // update state
64  this->m_is_ref_FD_loaded = true;
65 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
std::complex< T > * fft(T *inOut)
Definition: fftwT.cpp:204
Some logical error in the pipeline.
Definition: xmipp_error.h:147

◆ release()

template<typename T >
void Alignment::ShiftCorrEstimator< T >::release ( )
overridevirtual

Reimplemented from Alignment::AShiftCorrEstimator< T >.

Definition at line 86 of file shift_corr_estimator.cpp.

86  {
87  // host memory
88  if (this->m_includingSingleFT) {
89  delete[] m_single_FD;
90  }
91  if (this->m_includingBatchFT) {
92  delete[] m_batch_FD;
93  delete[] m_batch_SD;
94  }
95 
96  // FT plans
97  FFTwT<T>::release(m_singleToFD);
98  FFTwT<T>::release(m_batchToFD);
99  FFTwT<T>::release(m_batchToSD);
100 
102 
103  ShiftCorrEstimator<T>::setDefault();
104 }
void release()
Definition: fftwT.cpp:180

◆ sComputeCorrelations2DOneToN() [1/3]

template<typename T>
static void Alignment::ShiftCorrEstimator< T >::sComputeCorrelations2DOneToN ( const HW hw,
std::complex< T > *  inOut,
const std::complex< T > *  ref,
const Dimensions dims,
bool  center 
)
inlinestatic

Definition at line 82 of file shift_corr_estimator.h.

87  {
88  if (center) {
89  sComputeCorrelations2DOneToN<true>(hw, inOut, ref, dims);
90  } else {
91  sComputeCorrelations2DOneToN<false>(hw, inOut, ref, dims);
92  }
93  }

◆ sComputeCorrelations2DOneToN() [2/3]

template<typename T>
template<bool CENTER>
static void Alignment::ShiftCorrEstimator< T >::sComputeCorrelations2DOneToN ( const HW hw,
std::complex< T > *  inOut,
const std::complex< T > *  ref,
const Dimensions dims 
)
static

◆ sComputeCorrelations2DOneToN() [3/3]

template<typename T>
template<bool CENTER>
void Alignment::ShiftCorrEstimator< T >::sComputeCorrelations2DOneToN ( const HW hw,
std::complex< T > *__restrict  inOut,
const std::complex< T > *__restrict  ref,
const Dimensions &__restrict  dims 
)

Definition at line 174 of file shift_corr_estimator.cpp.

178  {
179  if (CENTER) {
180  // we cannot assert xDim, as we don't know if the spatial size was even
181  assert(0 == (dims.y() % 2));
182  }
183  assert(0 < dims.x());
184  assert(0 < dims.y());
185  assert(1 == dims.z());
186  assert(0 < dims.n());
187 
188  const size_t maxN = dims.n();
189  const size_t maxY = dims.y();
190  const size_t maxX = dims.x();
191 
192  for (size_t n = 0; n < maxN; ++n) {
193  size_t offsetN = n * dims.xyzPadded();
194  for (size_t y = 0; y < maxY; ++y) {
195  int centerCoeff = (0 == y % 2) ? 1 : -1;
196  size_t offsetY = y * dims.xPadded();
197  for (size_t x = 0; x < maxX; ++x) {
198  size_t destIndex = offsetN + offsetY + x;
199  auto r = ref[offsetY + x];
200  auto o = r * std::conj(inOut[destIndex]);
201  inOut[destIndex] = o;
202  if (CENTER) {
203  inOut[destIndex] *= centerCoeff;
204  centerCoeff *= -1;
205  }
206  }
207  }
208  }
209 }
constexpr size_t xyzPadded() const
Definition: dimensions.h:95
alglib::complex conj(const alglib::complex &z)
Definition: ap.cpp:4988
CUDA_HD constexpr size_t z() const
Definition: dimensions.h:69
static double * y
doublereal * x
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
constexpr size_t xPadded() const
Definition: dimensions.h:55
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
int * n

The documentation for this class was generated from the following files: