OpendTect  6.3
convolve3d.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: Feb 2008
9 ________________________________________________________________________
10 
11 
12 */
13 
14 #include "arraynd.h"
15 #include "paralleltask.h"
16 #include "math2.h"
17 
22 template <class T>
25 public:
26  inline Convolver3D();
27 
28  inline void setX(const Array3D<T>&,
29  int first0=0,int first1=0, int first2=0);
30  inline void setY(const Array3D<T>&,
31  int first0=0,int first1=0, int first2=0);
32  inline void setZ(Array3D<T>& z ) { z_ = &z; }
33  void setNormalize( bool n ) { normalize_ = n; }
36  void setCorrelate( bool yn ) { correlate_ = yn; }
39  void setHasUdfs(bool yn) { hasudfs_ = yn; }
41 
42  virtual bool executeParallel(bool);
43 
44  uiString message() const { return tr("Convolving 3D volume"); }
45  uiString nrDoneText() const { return tr("Positions done"); }
46 
47 protected:
48  inline bool shouldFFT() const;
49 
50  bool doFFT();
51  inline bool doWork( od_int64, od_int64, int );
52  od_int64 nrIterations() const { return z_->info().getTotalSz(); }
53  const Array3D<T>* x_;
54  int xshift0_;
55  int xshift1_;
56  int xshift2_;
57  const Array3D<T>* y_;
58  int yshift0_;
59  int yshift1_;
60  int yshift2_;
61 
63  bool normalize_;
64  bool correlate_;
65 
66  bool hasudfs_;
67 };
68 
69 
70 template <class T> inline
72  : x_( 0 )
73  , xshift0_( 0 )
74  , xshift1_( 0 )
75  , xshift2_( 0 )
76  , y_( 0 )
77  , yshift0_( 0 )
78  , yshift1_( 0 )
79  , yshift2_( 0 )
80  , z_( 0 )
81  , normalize_( false )
82  , correlate_( false )
83  , hasudfs_( false )
84 {}
85 
86 
87 template <class T> inline
89  int first0, int first1, int first2 )
90 {
91  x_ = &x;
92  xshift0_ = first0;
93  xshift1_ = first1;
94  xshift2_ = first2;
95 }
96 
97 
98 template <class T> inline
99 void Convolver3D<T>::setY( const Array3D<T>& y, int first0, int first1,
100  int first2 )
101 {
102  y_ = &y;
103  yshift0_ = first0;
104  yshift1_ = first1;
105  yshift2_ = first2;
106 }
107 
108 
109 #define mConvolver3DSetY( dim ) \
110 const int firsty##dim = correlate_ \
111  ? -xshift##dim##_-zvar[dim]+yshift##dim##_ \
112  : zvar[dim]+xshift##dim##_+yshift##dim##_; \
113 \
114  const char y##dim##inc = correlate_ ? 1 : -1
115 
116 
117 #define mConvolver3DSetIndex( dim ) \
118 const int idy##dim = firsty##dim+idx##dim*y##dim##inc; \
119 if ( idy##dim<0 ) \
120 { \
121  if ( correlate_ ) \
122  { \
123  idx##dim += (-idy##dim)-1; \
124  continue; \
125  } \
126  \
127  break; \
128 } \
129  \
130 if ( idy##dim>=ysz##dim ) \
131 { \
132  if ( correlate_ ) \
133  break; \
134  \
135  const int diff = idy##dim-(ysz##dim-1); \
136  idx##dim += diff-1; \
137  continue; \
138 }
139 
140 
141 template <class T> inline
142 bool Convolver3D<T>::doWork( od_int64 start, od_int64 stop, int )
143 {
144  const int xsz0 = x_->info().getSize( 0 );
145  const int xsz1 = x_->info().getSize( 1 );
146  const int xsz2 = x_->info().getSize( 2 );
147  const int ysz0 = y_->info().getSize( 0 );
148  const int ysz1 = y_->info().getSize( 1 );
149  const int ysz2 = y_->info().getSize( 2 );
150 
151  int startpos[3];
152 
153  if ( !z_->info().getArrayPos( start, startpos ) )
154  return false;
155 
156  ArrayNDIter iterator( z_->info() );
157  iterator.setPos( startpos );
158 
159  const ValueSeries<T>* xstor_ = x_->getStorage();
160  const T* xptr_ = x_->getData();
161 
162  const ValueSeries<T>* ystor_ = y_->getStorage();
163  const T* yptr_ = y_->getData();
164 
165  for ( int idx=mCast(int,start); idx<=stop; idx++ )
166  {
167  const int* zvar = iterator.getPos();
168  T sum = 0;
169  T ysum = 0;
170  int nrsamples = 0;
171 
172  mConvolver3DSetY( 0 );
173  mConvolver3DSetY( 1 );
174  mConvolver3DSetY( 2 );
175 
176  for ( int idx0=0; idx0<xsz0 && shouldContinue(); idx0++ )
177  {
179 
180  for ( int idx1=0; idx1<xsz1; idx1++ )
181  {
183 
184  const od_int64 yoffset = ystor_ || yptr_ ?
185  y_->info().getOffset( idy0, idy1, 0 ) : 0;
186 
187  const od_int64 xoffset = xstor_ || xptr_ ?
188  x_->info().getOffset( idx0, idx1, 0 ) : 0;
189 
190  for ( int idx2=0; idx2<xsz2; idx2++ )
191  {
193 
194  const T yval = yptr_
195  ? yptr_[yoffset+idy2]
196  : ystor_
197  ? ystor_->value( yoffset+idy2 )
198  : y_->get( idy0, idy1, idy2 );
199 
200  if ( mIsUdf(yval) )
201  continue;
202 
203  const T xval = xptr_
204  ? xptr_[xoffset+idx2]
205  : xstor_
206  ? xstor_->value( xoffset+idx2 )
207  : x_->get( idx0, idx1, idx2 );
208 
209  if ( mIsUdf(xval) )
210  continue;
211 
212  sum += xval * yval;
213  ysum += yval;
214  nrsamples++;
215  }
216  }
217  }
218 
219  if ( !nrsamples ) z_->setND( zvar, 0 );
220  else if ( normalize_ && !mIsZero(ysum,1e-8) )
221  z_->setND( zvar, sum/ysum );
222  else z_->setND( zvar, sum );
223 
224  addToNrDone( 1 );
225 
226  if ( !iterator.next() && idx!=stop )
227  return false;
228  }
229 
230  return true;
231 }
232 
233 
234 template <class T> inline
236 {
237  if ( shouldFFT() )
238  return doFFT();
239 
240  return ParallelTask::executeParallel( yn );
241 }
242 
243 
244 template <class T> inline
246 {
247  //TODO
248  return false;
249 }
250 
251 
252 template <class T> inline
254 {
255  return false;
256 }
257 
258 template <> inline
260 {
261  return false; //Remove when doFFT is implemented
262 /*
263  if ( correlate_ || normalize_ )
264  return false;
265 
266  if ( !x_ || !y_ || !z_ )
267  return false;
268 
269  const int xsz = x_->info().getTotalSz();
270  const int ysz = y_->info().getTotalSz();
271  const int zsz = z_->info().getTotalSz();
272 
273  int maxsz = x_->info().getTotalSz();
274  maxsz = mMAX( maxsz, y_->info().getTotalSz() );
275  maxsz = mMAX( maxsz, z_->info().getTotalSz() );
276 
277  const int tradsz = zsz * mMIN(ysz,xsz);
278  const float fftszf = maxsz * Math::Log( (float) maxsz );
279  const int fftsz = mNINT32( fftszf );
280 
281 
282  return fftsz<tradsz;
283  */
284 }
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:285
virtual T value(int64_t) const =0
bool correlate_
Definition: convolve3d.h:64
uiString message() const
Definition: convolve3d.h:44
Convolves (or correlates) two 3D signals.
Definition: convolve3d.h:23
const Array3D< T > * y_
Definition: convolve3d.h:57
int yshift0_
Definition: convolve3d.h:58
#define mODTextTranslationClass(clss)
Definition: uistring.h:37
void setY(const Array3D< T > &, int first0=0, int first1=0, int first2=0)
Definition: convolve3d.h:99
od_int64 nrIterations() const
Definition: convolve3d.h:52
#define mIsZero(x, eps)
Definition: commondefs.h:55
#define mCast(tp, v)
Definition: commondefs.h:120
#define od_int64
Definition: plftypes.h:34
int yshift1_
Definition: convolve3d.h:59
virtual bool shouldContinue()
Definition: uistring.h:88
int xshift1_
Definition: convolve3d.h:55
bool shouldFFT() const
Definition: convolve3d.h:253
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:64
int xshift0_
Definition: convolve3d.h:54
Array3D< T > * z_
Definition: convolve3d.h:62
bool normalize_
Definition: convolve3d.h:63
Convolver3D()
Definition: convolve3d.h:71
#define mConvolver3DSetIndex(dim)
Definition: convolve3d.h:117
void setPos(const T &idxabl)
Definition: arraynd.h:252
void setNormalize(bool n)
Definition: convolve3d.h:33
Interface to a series of values.
Definition: odmemory.h:15
int yshift2_
Definition: convolve3d.h:60
int xshift2_
Definition: convolve3d.h:56
void setCorrelate(bool yn)
Definition: convolve3d.h:36
uiString nrDoneText() const
Definition: convolve3d.h:45
virtual bool executeParallel(bool parallel)
#define mConvolver3DSetY(dim)
Definition: convolve3d.h:109
bool doFFT()
Definition: convolve3d.h:245
Iterates through all samples in an ArrayND.
Definition: arraynd.h:175
virtual bool executeParallel(bool)
Definition: convolve3d.h:235
void setX(const Array3D< T > &, int first0=0, int first1=0, int first2=0)
Definition: convolve3d.h:88
void setZ(Array3D< T > &z)
Definition: convolve3d.h:32
const Array3D< T > * x_
Definition: convolve3d.h:53
#define mClass(module)
Definition: commondefs.h:161
void setHasUdfs(bool yn)
Default is false.
Definition: convolve3d.h:39
Array3D ( Subclass of ArrayND ) is a three dimensional array.
Definition: arraynd.h:149
void addToNrDone(int64_t increment)
bool doWork(od_int64, od_int64, int)
Definition: convolve3d.h:142
bool hasudfs_
Definition: convolve3d.h:66

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