OpendTect-6_4  6.4
smoother2d.h
Go to the documentation of this file.
1 #ifndef smoother2d_h
2 #define smoother2d_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: Feb 2008
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "task.h"
16 #include "convolve2d.h"
17 #include "arrayndimpl.h"
18 #include "iopar.h"
19 #include "coord.h"
20 #include "windowfunction.h"
21 
26 template <class T>
27 mClass(Algo) Smoother2D : public Task
28 {
29 public:
30 
31  Smoother2D();
32  ~Smoother2D();
33 
34  void setInput(const Array2D<T>&,bool hasudf);
35  void setOutput(Array2D<T>&);
37  bool setWindow(const char* nm,float param,
38  int sz0,int sz1);
39  int getWindowSize(int dim) const;
40  const char* getWindowName() const;
41  float getWindowParam() const;
42 
43  inline void fillPar(IOPar&) const;
44  inline bool usePar(const IOPar&);
45 
46  inline void setProgressMeter(ProgressMeter* pm);
47  inline bool execute();
48  inline void enableWorkControl(bool);
49  inline void controlWork(Task::Control);
50  inline Task::Control getState() const;
51 
52  static const char* sKeyWinFunc() { return "Window function"; }
53  static const char* sKeyWinParam() { return "Window parameter"; }
54  static const char* sKeyWinSize() { return "Window size"; }
55 
56 protected:
57 
59  bool hasudf_;
60 
62  int windowsz0_, windowsz1_;
64  float windowparam_;
65 };
66 
67 
68 template <class T> inline
70  : windowparam_( mUdf(float) )
71  , window_( 0 )
72 {
73  convolver_.setNormalize( true );
74  convolver_.setCorrelate( false );
75 }
76 
77 
78 template <class T> inline
80 { delete window_; }
81 
82 
83 template <class T> inline
84 int Smoother2D<T>::getWindowSize(int dim) const
85 { return window_->info().getSize( dim ); }
86 
87 
88 template <class T> inline
89 const char* Smoother2D<T>::getWindowName() const
90 { return windowname_.buf(); }
91 
92 
93 template <class T> inline
95 { return windowparam_; }
96 
97 
98 template <class T> inline
99 void Smoother2D<T>::setInput( const Array2D<T>& ni, bool hasudf )
100 {
101  const Array2D<float>* input = convolver_.getX();
102 
103  if ( !input || hasudf_!=hasudf || input->info()!=ni.info() )
104  { delete window_; window_ = 0; }
105 
106  convolver_.setX( ni, hasudf );
107  hasudf_ = hasudf;
108 }
109 
110 
111 template <class T> inline
113 {
114  convolver_.setZ( no );
115 }
116 
117 
118 template <class T> inline
119 bool Smoother2D<T>::setWindow( const char* nm, float param,
120  int sz0, int sz1 )
121 {
122  windowname_ = nm;
123  windowparam_ = param;
124  windowsz0_ = sz0;
125  windowsz1_ = sz1;
126 
127  delete window_; window_ = 0;
128 
129  return true;
130 }
131 
132 
133 template <class T> inline
134 void Smoother2D<T>::fillPar( IOPar& par ) const
135 {
136  par.set( sKeyWinFunc(), windowname_ );
137  if ( !mIsUdf(windowparam_) )
138  par.set( sKeyWinParam(), windowparam_ );
139 
140  par.set(sKeyWinSize(),window_->info().getSize(0),window_->info().getSize(1));
141 }
142 
143 
144 template <class T> inline
145 bool Smoother2D<T>::usePar( const IOPar& par )
146 {
147  int sz0, sz1;
148  if ( !par.get(sKeyWinSize(), sz0, sz1 ) )
149  return false;
150 
151  const char* wn = par.find( sKeyWinFunc() );
152  float var = mUdf(float);
153  par.get( sKeyWinParam(), var );
154 
155  return setWindow( wn, var, sz0, sz1 );
156 }
157 
158 
159 #define mImplSetFunc( func, vartype ) \
160 template <class T> inline void Smoother2D<T>::func( vartype var ) \
161 { convolver_.func( var ); }
162 
166 
167 template <class T> inline
169 {
170  const Array2D<float>* input = convolver_.getX();
171  if ( !input )
172  return false;
173 
174  if ( !window_ )
175  {
176  PtrMan<WindowFunction> wf = WINFUNCS().create( windowname_ );
177  if ( !wf )
178  return false;
179 
180  if ( wf->hasVariable() &&
182  return false;
183 
184  if ( windowsz0_<=0 || windowsz1_<=0 )
185  return false;
186 
187  //Can we setup for fft? If so, all volumes should have same size
188  if ( typeid(T)==typeid(float) && input->getData() && !hasudf_ )
189  window_ = new Array2DImpl<T>( input->info() );
190  else
192 
193  if ( !window_->isOK() )
194  return false;
195 
196  const float hwinsz0 = ((float)windowsz0_)/2;
197  const float hwinsz1 = ((float)windowsz1_)/2;
198  Coord pos;
199 
200  const int sz0 = window_->info().getSize( 0 );
201  const int sz1 = window_->info().getSize( 1 );
202  const int hsz0 = sz0/2;
203  const int hsz1 = sz1/2;
204 
205  double weightsum = 0;
206 
207  for ( int idx0=0; idx0<sz0; idx0++ )
208  {
209  const float pos0 = mCast( float, idx0>hsz0 ? idx0-sz0 : idx0 );
210  pos[0] = pos0/hwinsz0;
211  for ( int idx1=0; idx1<sz1; idx1++ )
212  {
213  const float pos1 = mCast( float, idx1>hsz1 ? idx1-sz1 : idx1 );
214  pos[1] = pos1/hwinsz1;
215  const float weight = wf->getValue( (float) pos.abs() );
216  window_->set( idx0, idx1, weight );
217  weightsum += weight;
218  }
219  }
220 
221  if ( weightsum>1 )
222  mDoArrayPtrOperation( float, window_->getData(), /= (float)weightsum,
223  window_->info().getTotalSz(), ++ );
224 
225  convolver_.setY( *window_, false );
226  }
227 
228  return convolver_.execute();
229 }
230 
231 
232 template <class T> inline
234 { return convolver_.getState(); }
235 
236 #endif
Array2DImpl< T > * window_
Definition: smoother2d.h:61
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
static const char * sKeyWinFunc()
Definition: smoother2d.h:52
Convolver2D< T > convolver_
Definition: smoother2d.h:58
float windowparam_
Definition: smoother2d.h:64
is an interface where processes can report their progress.
Definition: progressmeter.h:21
#define mCast(tp, v)
Definition: commondefs.h:124
const char * getWindowName() const
Definition: smoother2d.h:89
Smoothes a 2d signal with an operator.
Definition: smoother2d.h:27
void usePar(const IOPar &iop, ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:188
Implementation of Array2D.
Definition: arrayndimpl.h:102
BufferString windowname_
Definition: smoother2d.h:63
virtual bool hasVariable() const
Definition: windowfunction.h:31
A cartesian coordinate in 2D space.
Definition: coord.h:25
bool get(const char *, int &) const
Task::Control getState() const
Definition: smoother2d.h:233
#define mImplSetFunc(func, vartype)
Definition: smoother2d.h:159
int getWindowSize(int dim) const
Definition: smoother2d.h:84
void setOutput(Array2D< T > &)
Definition: smoother2d.h:112
void setInput(const Array2D< T > &, bool hasudf)
Definition: smoother2d.h:99
const T * getData() const
Definition: arraynd.h:55
Definition: ptrman.h:79
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:47
Array2D ( Subclass of ArrayND ) is a two dimensional array.
Definition: arraynd.h:131
void setProgressMeter(ProgressMeter *pm)
Must be called before execute()
Definition: smoother2d.h:163
float getWindowParam() const
Definition: smoother2d.h:94
Control
Definition: task.h:55
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
const char * find(const char *) const
returns null if not found
Convolves (or correlates) two 2D signals.
Definition: convolve2d.h:34
double abs() const
Definition: geometry.h:364
virtual bool setVariable(float)
Definition: windowfunction.h:33
void fillPar(IOPar &iop, const ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:175
void fillPar(IOPar &) const
Definition: smoother2d.h:134
static const char * sKeyWinSize()
Definition: smoother2d.h:54
const char * buf() const
Definition: odstring.h:47
bool setWindow(const char *nm, float param, int sz0, int sz1)
Definition: smoother2d.h:119
int windowsz1_
Definition: smoother2d.h:62
#define mDoArrayPtrOperation(type, arr, operation, arrsz, ptrinc)
Definition: commondefs.h:280
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size...
Definition: bufstring.h:40
bool hasudf_
Definition: smoother2d.h:59
bool execute()
Definition: smoother2d.h:168
~Smoother2D()
Definition: smoother2d.h:79
int windowsz0_
Definition: smoother2d.h:62
void controlWork(Task::Control)
Definition: smoother2d.h:165
static const char * sKeyWinParam()
Definition: smoother2d.h:53
Smoother2D()
Definition: smoother2d.h:69
#define mClass(module)
Definition: commondefs.h:164
virtual const Array2DInfo & info() const =0
void set(const char *ky, const char *val)
virtual RT getValue(PT) const =0
void enableWorkControl(bool)
Must be called before execute()
Definition: smoother2d.h:164
Generalization of something (e.g. a computation) that needs to be done in multiple steps...
Definition: task.h:28
bool usePar(const IOPar &)
Definition: smoother2d.h:145

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