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

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