Xmipp  v3.23.11-Nereus
aextrema_finder.h
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 
26 #ifndef LIBRARIES_RECONSTRUCTION_AEXTREMA_FINDER_H_
27 #define LIBRARIES_RECONSTRUCTION_AEXTREMA_FINDER_H_
28 
29 #include "data/dimensions.h"
30 #include "data/hw.h"
31 #include "data/point3D.h"
32 #include "core/xmipp_error.h"
33 #include <vector>
34 #include <cassert>
35 #include <limits>
36 
40 
41 namespace ExtremaFinder {
42 // FIXME DS we should have search type Min, Max, MaxAbs, Lowest, Custom
43 // FIXME DS we should have search location Entire, NearCenter, AroundCenter, Window, AroundWindow
44 enum class SearchType {
45  Lowest, // in the whole signal (for each signal)
46  Max, // in the whole signal (for each signal)
47  MaxAroundCenter, // for each signal, search a circular area around center
48  MaxNearCenter, // for each signal, search a square area around center
49  LowestAroundCenter // for each signal, search a circular area around center
50 };
51 
52 enum class ResultType {
53  Value,
54  Position,
55  Both
56 };
57 
59 public:
60  std::vector<HW*> hw;
64  size_t batch = 0;
65  float maxDistFromCenter = 0.f;
66 
68  return Point3D<size_t>(dims.x() / 2, dims.y() / 2, dims.z() / 2);
69  }
70 
71  void check() const {
72  if (0 == hw.size()) {
73  REPORT_ERROR(ERR_VALUE_INCORRECT, "HW contains zero (0) devices");
74  }
75  if ( ! dims.isValid()) {
76  REPORT_ERROR(ERR_LOGIC_ERROR, "Dimensions are invalid (contain 0)");
77  }
78  if (0 == batch) {
79  REPORT_ERROR(ERR_LOGIC_ERROR, "Batch is zero (0)");
80  }
81  if ((SearchType::MaxAroundCenter == searchType)
82  || (SearchType::LowestAroundCenter == searchType)) {
83  const auto center = getCenter();
84  if (0 == maxDistFromCenter) {
85  REPORT_ERROR(ERR_LOGIC_ERROR, "'maxDistFromCenter' is set to zero (0)");
86  }
87  if (dims.is1D()) {
88  if (maxDistFromCenter >= center.x) {
89  REPORT_ERROR(ERR_LOGIC_ERROR, "'maxDistFromCenter' is bigger than half of the signal's X dimension");
90  }
91  } else if (dims.is2D()) {
92  if ((maxDistFromCenter >= center.x)
93  || (maxDistFromCenter >= center.y)) {
94  REPORT_ERROR(ERR_LOGIC_ERROR, "'maxDistFromCenter' is bigger than half of the signal's X or Y dimensions");
95  }
96  } else {
97  if ((maxDistFromCenter >= center.x)
98  || (maxDistFromCenter >= center.y)
99  || (maxDistFromCenter >= center.z)) {
100  REPORT_ERROR(ERR_LOGIC_ERROR, "'maxDistFromCenter' is bigger than half of the signal's X, Y or Z dimensions");
101  }
102  }
103  }
104  }
105 };
106 
107 template<typename T>
109 public:
111  m_isInit(false) {};
112 
113  virtual ~AExtremaFinder() {};
114 
115  void init(const ExtremaFinderSettings &settings, bool reuse);
116 
117  void find(const T *data);
118 
119  HW& getHW() const { // FIXME DS remove once we use the new data-centric approach
120  assert(m_isInit);
121  return *m_settings.hw.at(0);
122  }
123 
124  inline const ExtremaFinderSettings &getSettings() const {
125  return m_settings;
126  }
127 
128  inline const std::vector<T> &getValues() const {
129  return m_values;
130  }
131 
132  inline const std::vector<float> &getPositions() const {
133  return m_positions;
134  }
135 
136 protected:
137  virtual void check() const = 0;
138 
139  virtual void initMax() = 0;
140  virtual void findMax(const T *data) = 0;
141  virtual bool canBeReusedMax(const ExtremaFinderSettings &s) const = 0;
142 
143  virtual void initLowest() = 0;
144  virtual void findLowest(const T *data) = 0;
145  virtual bool canBeReusedLowest(const ExtremaFinderSettings &s) const = 0;
146 
147  virtual void initMaxAroundCenter() = 0;
148  virtual void findMaxAroundCenter(const T *data) = 0;
149  virtual bool canBeReusedMaxAroundCenter(const ExtremaFinderSettings &s) const = 0;
150 
151  virtual void initLowestAroundCenter() = 0;
152  virtual void findLowestAroundCenter(const T *data) = 0;
153  virtual bool canBeReusedLowestAroundCenter(const ExtremaFinderSettings &s) const = 0;
154 
155  inline std::vector<T> &getValues() {
156  return m_values;
157  }
158 
159  inline std::vector<float> &getPositions() {
160  return m_positions;
161  }
162 
163  inline constexpr bool isInitialized() const {
164  return m_isInit;
165  }
166 
167 private:
168  ExtremaFinderSettings m_settings;
169 
170  // results
171  std::vector<T> m_values;
172  std::vector<float> m_positions; // absolute, 0 based indices
173 
174  // flags
175  bool m_isInit;
176 
177  bool canBeReused(const ExtremaFinderSettings &settings) const;
178 };
179 
180 } /* namespace ExtremaFinder */
182 #endif /* LIBRARIES_RECONSTRUCTION_AEXTREMA_FINDER_H_ */
Point3D< size_t > getCenter() const
const std::vector< float > & getPositions() const
CUDA_HD constexpr bool is2D() const
Definition: dimensions.h:162
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
constexpr bool isInitialized() const
CUDA_HD constexpr size_t z() const
Definition: dimensions.h:69
std::vector< SelLine >::iterator find(std::vector< SelLine > &text, const std::string &img_name)
Definition: selfile.cpp:553
Definition: hw.h:35
std::vector< float > & getPositions()
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
constexpr bool is1D() const
Definition: dimensions.h:157
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
const std::vector< T > & getValues() const
const ExtremaFinderSettings & getSettings() const
constexpr bool isValid() const
Definition: dimensions.h:138
std::vector< T > & getValues()
Incorrect value received.
Definition: xmipp_error.h:195
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)
Some logical error in the pipeline.
Definition: xmipp_error.h:147