Fraxinus  18.10
An IGT application
intersection.hpp
Go to the documentation of this file.
1 #ifndef INTERSECTION_HPP
2 #define INTERSECTION_HPP
3 
4 #include <algorithm>
5 #include <functional>
6 #include <numeric>
7 #include "spline3d.hpp"
8 #include "metaimage.hpp"
9 
10 template<typename T>
11 class Spline3D;
12 
16 template<typename T>
18 {
19 public:
20 
24  Intersection():m_points()
25  {
26  m_spline = NULL;
27  m_intersection_pos = 0.0;
28  m_cosTheta = 0.0;
29  m_origAvgValue = 0.0;
30  m_valid = false;
31  m_img = NULL;
32  m_avg_computed = 0;
33  m_avgValue = 0;
34  }
39  inline const Spline3D<T>
40  *getSpline() const
41  {
42  return m_spline;
43  }
44 
49  inline void
50  setSpline(const Spline3D<T> *spline)
51  {
52  m_spline = spline;
53  }
54 
59  inline T
61  {
62  return m_intersection_pos;
63  }
64 
69  inline void
71  {
72  m_intersection_pos = t;
73  }
74 
80  inline T
81  getCosTheta() const
82  {
83  return m_cosTheta;
84  }
85 
90  inline void
92  {
93  m_cosTheta = t;
94  }
95 
100  inline void
101  setPoints(const vector<T>& points)
102  {
103  m_points = points;
104  m_avg_computed = false;
105  }
106 
111  inline void
112  setPoints(vector<T>&& points)
113  {
114  m_points = std::move(points);
115  m_avg_computed = false;
116  }
117 
122  inline vector<T>&
124  {
125  return m_points;
126  }
127 
131  inline T
133  {
134  if(!m_avg_computed)
135  __computeAverage();
136  return m_avgValue;
137  }
138 
143  inline void
144  setValid(bool valid)
145  {
146  m_valid = valid;
147  }
148 
153  inline bool
154  isValid() const
155  {
156  return m_valid;
157  }
158 
163  inline const MetaImage<inData_t>*
164  getMetaImage() const
165  {
166  return m_img;
167  }
168 
173  inline void
175  {
176  m_img = img;
177  m_avg_computed = false;
178  }
179 
184  inline void
185  evaluate(T p[3]) const
186  {
187  if(isValid())
188  {
189  m_spline->evaluateSingle(m_intersection_pos,p);
190  }
191  }
192 
197  inline T
199  {
200  return m_origAvgValue;
201  }
202 
208  inline void
209  correctAliasing(T direction,T Vnyq)
210  {
211 
212  m_avgValue = 0.0;
213  for(auto it = m_points.begin(); it != m_points.end(); it++)
214  {
215  bool sign = sgn(direction) == sgn(m_cosTheta);
216  if(*it < 0 && sign)
217  {
218  *it += 2*Vnyq;
219  }
220  else if(*it > 0 && !sign)
221  {
222  *it -= 2*Vnyq;
223  }
224  m_avgValue += *it;
225  }
226  if (m_points.size()==0){
227  m_avgValue =0.0;
228  m_valid = false;
229  }else{
230  m_avgValue = m_avgValue/m_points.size();
231  }
232  }
233 
241  inline T
242  sampleWeight(const T A, const T a, const T b) const
243  {
244  int positive;
245  int negative;
246  positive = std::accumulate(m_points.begin(), m_points.end(), 0,
247  [](int k, T l){ if(l > 0) return k + 1; return k; }
248  );
249 
250  negative = m_points.size()-positive;
251 
252  T pos_weight = (T)(positive - negative)/(double)(positive + negative);
253  pos_weight = pos_weight * pos_weight;
254  T ret = A*pos_weight;
255  if(abs(m_cosTheta) > a && abs(m_cosTheta) < b)
256  {
257  ret += 1;
258  }
259  return ret;
260  }
261 
266  inline T
268  {
269  if(isValid())
270  {
271  return getAverage()/m_cosTheta;
272  }
273  else
274  return 0.0;
275  }
276 
280  inline void
282  {
283  if(!isValid()) return;
284  T p[3];
285  evaluate(p);
286  T img_x, img_y;
287  m_img->toImgCoords(img_x, img_y, p);
288  if(m_img->inImage(img_x, img_y))
289  {
290  m_img->regionGrow(m_points,(int)img_x, (int)img_y);
291  }
292  }
293 
294 
295 private:
296  void __computeAverage()
297  {
298  m_avgValue = std::accumulate(m_points.begin(), m_points.end(), 0.0, plus<T>());
299  //m_avgValue = m_avgValue/(T)m_points.size();
300  m_origAvgValue = m_avgValue;
301  m_avg_computed = true;
302 
303  if (m_points.size()==0){
304  m_avgValue =0.0;
305  m_valid = false;
306  }else{
307  m_avgValue = m_avgValue/(T)m_points.size();
308  }
309  }
310 
311  const Spline3D<T> *m_spline;
312  T m_intersection_pos;
313  T m_cosTheta;
314  T m_avgValue;
315  const MetaImage<inData_t> *m_img;
316  vector<T> m_points;
317  bool m_valid;
318  bool m_avg_computed;
319  T m_origAvgValue;
320 };
321 
322 
323 #endif //INTERSECTION_HPP
void correctAliasing(T direction, T Vnyq)
T estimateVelocitySimple()
T getParameterPosition() const
T getCosTheta() const
void setPoints(const vector< T > &points)
void setSpline(const Spline3D< T > *spline)
const Spline3D< T > * getSpline() const
int sgn(T val)
Definition: helpers.hpp:9
void regionGrow(vector< Dt > &ret, int imgx, int imgy) const
Definition: metaimage.hpp:171
void evaluate(T p[3]) const
T sampleWeight(const T A, const T a, const T b) const
void setMetaImage(const MetaImage< inData_t > *img)
bool isValid() const
void toImgCoords(double &x, double &y, const double p[3]) const
Definition: metaimage.hpp:105
void setValid(bool valid)
int sign(double x)
void setPoints(vector< T > &&points)
const MetaImage< inData_t > * getMetaImage() const
bool inImage(double img_x, double img_y) const
Definition: metaimage.hpp:363
vector< T > & getPoints()
void setParameterPosition(T t)
void setCosTheta(T t)