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

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