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

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