Xmipp  v3.23.11-Nereus
bspline_geo_transformer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
27 #include "data/filters.h"
28 #include "core/transformations.h"
29 
30 template<typename T>
31 void BSplineGeoTransformer<T>::initialize(bool doAllocation) {
32  const auto &s = this->getSettings();
33 
34  for (auto &hw : s.hw) {
35  if ( ! dynamic_cast<CPU*>(hw)) {
36  REPORT_ERROR(ERR_LOGIC_ERROR, "Instance of CPU is expected");
37  }
38  }
39 // m_threadPool.resize(s.hw.size()); // FIXME DS set to requested number of thread
40  m_threadPool.resize(CPU::findCores());
41 
42  if (doAllocation) {
43  release();
44  m_dest = std::unique_ptr<T[]>(new T[s.dims.size()]);
45  }
46 }
47 
48 template<typename T>
50  const auto &s = this->getSettings();
51  if (InterpolationDegree::Linear != s.degree) {
52  REPORT_ERROR(ERR_NOT_IMPLEMENTED, "Only linear interpolation is available");
53  }
54  if (s.doWrap) {
55  REPORT_ERROR(ERR_NOT_IMPLEMENTED, "Wrapping is not yet implemented");
56  }
57  if (InterpolationType::NToN != s.type) {
58  REPORT_ERROR(ERR_NOT_IMPLEMENTED, "Only NToN is currently implemented");
59  }
60 }
61 
62 
63 template<typename T>
65  bool isReady = this->isInitialized()
66  && this->isSrcSet();
67  if ( ! isReady) {
68  REPORT_ERROR(ERR_LOGIC_ERROR, "Instance is either not initialized or the 'src' has not been set.");
69  }
70  memcpy(m_dest.get(),
71  m_src,
72  this->getSettings().dims.size() * sizeof(T));
73 }
74 
75 template<typename T>
77  m_dest.release();
78  setDefault();
79 }
80 
81 template<typename T>
83  m_dest.reset();
84  m_src = nullptr;
85  m_threadPool.resize(1);
86 }
87 
88 template<typename T>
90  bool result = true;
91  if ( ! this->isInitialized()) {
92  return false;
93  }
94  auto &sOrig = this->getSettings();
95  result &= sOrig.dims.size() >= s.dims.size(); // previously, we needed more space
96  result &= !(( ! sOrig.keepSrcCopy) && s.keepSrcCopy); // we have a problem if now we need to make a copy and before not
97 
98  return result;
99 }
100 
101 template<typename T>
102 void BSplineGeoTransformer<T>::sum(T *dest, const std::vector<float> &weights, size_t firstN, T norm) {
103  REPORT_ERROR(ERR_NOT_IMPLEMENTED, "This functionality is not yet available.");
104 }
105 
106 template<typename T>
107 T *BSplineGeoTransformer<T>::interpolate(const std::vector<float> &matrices) {
108  bool isReady = this->isInitialized()
109  && this->isSrcSet();
110  if ( ! isReady) {
111  REPORT_ERROR(ERR_LOGIC_ERROR, "Instance is either not initialized or the 'original' has not been set.");
112  }
113  const Dimensions dims = this->getSettings().dims;
114  const size_t n = dims.n();
115  const size_t z = dims.z();
116  const size_t y = dims.y();
117  const size_t x = dims.x();
118 
119  auto futures = std::vector<std::future<void>>();
120 
121  auto workload = [&](int id, size_t signalId){
122  size_t offset = signalId * dims.sizeSingle();
123  auto in = MultidimArray<T>(1, z, y, x, const_cast<T*>(m_src + offset)); // removing const, but data should not be changed
124  auto out = MultidimArray<T>(1, z, y, x, m_dest.get() + offset);
125  in.setXmippOrigin();
126  out.setXmippOrigin();
127  // compensate the movement
128  Matrix2D<double> m(3,3);
129  const float *f = matrices.data() + (9 * signalId);
130  for (int i = 0; i < 9; ++i) {
131  m.mdata[i] = f[i];
132  }
133  applyGeometry(xmipp_transformation::LINEAR, out, in, m, true, xmipp_transformation::DONT_WRAP);
134  };
135 
136  for (size_t i = 0; i < n; ++i) {
137  futures.emplace_back(m_threadPool.push(workload, i));
138  }
139  for (auto &f : futures) {
140  f.get();
141  }
142  return m_dest.get();
143 }
144 
145 // explicit instantiation
146 template class BSplineGeoTransformer<float>;
147 template class BSplineGeoTransformer<double>;
virtual void sum(T *dest, const std::vector< float > &weights, size_t firstN, T norm)
Case or algorithm not implemented yet.
Definition: xmipp_error.h:177
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
virtual void check() override
CUDA_HD constexpr size_t z() const
Definition: dimensions.h:69
virtual bool canBeReused(const BSplineTransformSettings< T > &s) const override
static double * y
void applyGeometry(int SplineDegree, MultidimArray< std::complex< double > > &V2, const MultidimArray< std::complex< double > > &V1, const Matrix2D< double > &A, bool inv, bool wrap, std::complex< double > outside, MultidimArray< double > *BcoeffsPtr)
T * mdata
Definition: matrix2d.h:395
T norm(const std::vector< T > &v)
Definition: vector_ops.h:399
virtual T * interpolate(const std::vector< float > &matrices)
static unsigned findCores()
Definition: cpu.h:41
doublereal * x
#define i
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
int in
double * f
CUDA_HD constexpr size_t sizeSingle() const
Definition: dimensions.h:100
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
double z
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
int m
constexpr size_t size() const
Definition: dimensions.h:104
virtual void copySrcToDest() override
virtual void initialize(bool doAllocation) override
int * n
Some logical error in the pipeline.
Definition: xmipp_error.h:147