OpendTect-6_4  6.4
convolve2d.h
Go to the documentation of this file.
1 #ifndef convolve2d_h
2 #define convolve2d_h
3 
4 /*@+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kristofer Tingdahl
9  Date: 07-10-1999
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 
14 */
15 
16 #include "odcomplex.h"
17 #include "arraynd.h"
18 #include "paralleltask.h"
19 #include "rowcol.h"
20 
21 #if defined(__msvc__) && (defined(ALGO_EXPORTS) || defined(Algo_EXPORTS))
22 # define mAlgoClass class dll_export
23 #else
24 # define mAlgoClass class
25 #endif
26 
27 namespace Fourier { class CC; }
28 
33 template <class T>
35 {
36 public:
37  Convolver2D();
38  ~Convolver2D();
39 
40  inline void setX(const Array2D<T>&,bool hasudfs);
41  inline const Array2D<T>* getX() const { return x_; }
42  inline void setY(const Array2D<T>&,bool hasudfs);
43  inline const Array2D<T>* getY() const { return x_; }
44  inline void setZ(Array2D<T>& z ) { z_ = &z; }
45  void setNormalize( bool n ) { normalize_ = n; }
48  void setCorrelate( bool yn ) { correlate_ = yn; }
51  od_int64 nrIterations() const;
52 
53 protected:
54  bool doWork(od_int64,od_int64,int);
55  bool doNonFFTWork(od_int64,od_int64,int);
56  bool doPrepare(int);
57  bool shouldFFT() const;
58 
59  const Array2D<T>* x_;
60  const Array2D<T>* y_;
62  bool normalize_;
63  bool correlate_;
64  bool xhasudfs_;
65  bool yhasudfs_;
66 
70 
71  bool updatexf_;
72  bool updateyf_;
73 
75 };
76 
77 
78 
79 
80 template <> inline
82 {
83  if ( xhasudfs_ || yhasudfs_ || x_->info()!=y_->info() ||
84  x_->info()!=z_->info() )
85  return false;
86 
87  return true;
88 }
89 
90 
91 template <> inline
93 {
94  return shouldFFT()
95  ? 1
96  : z_->info().getTotalSz();
97 }
98 
99 
100 template <class T> inline
102 { return false; }
103 
104 
105 template <class T> inline
107  : x_( 0 )
108  , y_( 0 )
109  , z_( 0 )
110  , normalize_( false )
111  , correlate_( false )
112  , xhasudfs_( false )
113  , yhasudfs_( false )
114  , updatexf_( true )
115  , updateyf_( true )
116  , xf_( 0 )
117  , yf_( 0 )
118  , zf_( 0 )
119  , fft_( 0 )
120 {}
121 
122 
123 template <>
125 
126 
127 template <class T>
129 {}
130 
131 template <class T> inline
132 void Convolver2D<T>::setX( const Array2D<T>& x, bool hasudfs )
133 {
134  x_ = &x;
135  updatexf_ = true;
136  xhasudfs_ = hasudfs;
137 }
138 
139 
140 template <class T> inline
141 void Convolver2D<T>::setY( const Array2D<T>& y, bool hasudfs )
142 {
143  y_ = &y;
144  updateyf_ = true;
145  yhasudfs_ = hasudfs;
146 }
147 
148 
149 
150 
151 #define mConvolver2DSetIndex( dim ) \
152 const char y##dim##inc = correlate_ ? 1 : -1; \
153 const int idy##dim = firsty##dim+idx##dim*y##dim##inc; \
154 if ( idy##dim<0 ) \
155 { \
156  if ( correlate_ ) \
157  { \
158  idx##dim += (-idy##dim)-1; \
159  continue; \
160  } \
161  \
162  break; \
163 } \
164  \
165 if ( idy##dim>=ysz##dim ) \
166 { \
167  if ( correlate_ ) \
168  break; \
169  \
170  const int diff = idy##dim-(ysz##dim-1); \
171  idx##dim += diff-1; \
172  continue; \
173 }
174 
175 
176 template <class T> inline
178 {
179  return shouldFFT()
180  ? 1
181  : z_->info().getTotalSz();
182 }
183 
184 
185 template <>
186 bool Convolver2D<float>::doWork( od_int64 start, od_int64 stop, int );
187 
188 
189 template <class T> inline
190 bool Convolver2D<T>::doWork( od_int64 start, od_int64 stop, int thread )
191 {
192  return doNonFFTWork( start, stop, thread );
193 }
194 
195 
196 template <class T> inline
198 {
199  const int xsz0 = x_->info().getSize( 0 );
200  const int xsz1 = x_->info().getSize( 1 );
201  const int ysz0 = y_->info().getSize( 0 );
202  const int ysz1 = y_->info().getSize( 1 );
203 
204  int startpos[2];
205 
206  if ( !z_->info().getArrayPos( start, startpos ) )
207  return false;
208 
209  ArrayNDIter iterator( z_->info() );
210  iterator.setPos( startpos );
211 
212  const ValueSeries<T>* xstor_ = x_->getStorage();
213  const T* xptr_ = x_->getData();
214 
215  const ValueSeries<T>* ystor_ = y_->getStorage();
216  const T* yptr_ = y_->getData();
217 
218  for ( od_int64 idx=start; idx<=stop; idx++ )
219  {
220  const int* zvar = iterator.getPos();
221  const int firsty0 = correlate_ ? -zvar[0] : zvar[0];
222  const int firsty1 = correlate_ ? -zvar[1] : zvar[1];
223  T sum = 0;
224  T ysum = 0;
225  int nrsamples = 0;
226 
227  for ( int idx0=0; idx0<xsz0 && shouldContinue(); idx0++ )
228  {
230 
231  const od_int64 yoffset = ystor_ || yptr_ ?
232  y_->info().getOffset( idy0, 0 ) : 0;
233 
234  const od_int64 xoffset = xstor_ || xptr_ ?
235  x_->info().getOffset( idx0, 0 ) : 0;
236 
237  for ( int idx1=0; idx1<xsz1; idx1++ )
238  {
240 
241  const T yval = yptr_
242  ? yptr_[yoffset+idy1]
243  : ystor_
244  ? ystor_->value( yoffset+idy1 )
245  : y_->get( idy0, idy1 );
246 
247  if ( mIsUdf(yval) )
248  continue;
249 
250  const T xval = xptr_
251  ? xptr_[xoffset+idx1]
252  : xstor_
253  ? xstor_->value( xoffset+idx1 )
254  : x_->get( idx0, idx1 );
255 
256  if ( mIsUdf(xval) )
257  continue;
258 
259  sum += xval * yval;
260  ysum += yval;
261  nrsamples++;
262  }
263  }
264 
265  if ( !nrsamples ) z_->setND( zvar, 0 );
266  else if ( normalize_ && !mIsZero(ysum,1e-8) )
267  z_->setND( zvar, sum/ysum );
268  else z_->setND( zvar, sum );
269 
270  addToNrDone( 1 );
271 
272  if ( !iterator.next() && idx!=stop )
273  return false;
274  }
275 
276  return true;
277 }
278 
279 
280 #endif
Does Fourier Transforms of any size.
Definition: fourier.h:30
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
virtual T value(int64_t) const =0
bool xhasudfs_
Definition: convolve2d.h:64
#define mIsZero(x, eps)
Definition: commondefs.h:53
#define od_int64
Definition: plftypes.h:36
~Convolver2D()
Definition: convolve2d.h:128
virtual bool shouldContinue()
bool correlate_
Definition: convolve2d.h:63
float_complex * xf_
Definition: convolve2d.h:67
const Array2D< T > * getX() const
Definition: convolve2d.h:41
float_complex * zf_
Definition: convolve2d.h:69
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
void setZ(Array2D< T > &z)
Definition: convolve2d.h:44
bool normalize_
Definition: convolve2d.h:62
Array2D< T > * z_
Definition: convolve2d.h:61
bool updatexf_
Definition: convolve2d.h:71
Fourier::CC * fft_
Definition: convolve2d.h:74
Array2D ( Subclass of ArrayND ) is a two dimensional array.
Definition: arraynd.h:131
void setPos(const T &idxabl)
Definition: arraynd.h:256
Interface to a series of values.
Definition: odmemory.h:17
bool doWork(od_int64, od_int64, int)
Definition: convolve2d.h:190
void setNormalize(bool n)
Definition: convolve2d.h:45
Convolves (or correlates) two 2D signals.
Definition: convolve2d.h:34
void setCorrelate(bool yn)
Definition: convolve2d.h:48
bool yhasudfs_
Definition: convolve2d.h:65
bool doNonFFTWork(od_int64, od_int64, int)
Definition: convolve2d.h:197
const Array2D< T > * y_
Definition: convolve2d.h:60
std::complex< float > float_complex
Definition: odcomplex.h:18
#define mConvolver2DSetIndex(dim)
Definition: convolve2d.h:151
#define mAlgoClass
Definition: convolve2d.h:24
bool shouldFFT() const
Definition: convolve2d.h:101
Definition: convolve2d.h:27
Iterates through all samples in an ArrayND.
Definition: arraynd.h:179
const Array2D< T > * x_
Definition: convolve2d.h:59
od_int64 nrIterations() const
Definition: convolve2d.h:177
bool updateyf_
Definition: convolve2d.h:72
void setX(const Array2D< T > &, bool hasudfs)
Definition: convolve2d.h:132
void setY(const Array2D< T > &, bool hasudfs)
Definition: convolve2d.h:141
const Array2D< T > * getY() const
Definition: convolve2d.h:43
float_complex * yf_
Definition: convolve2d.h:68
void addToNrDone(int64_t increment)
Convolver2D()
Definition: convolve2d.h:106

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019