OpendTect  6.6
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  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "task.h"
15 #include "convolve2d.h"
16 #include "arrayndimpl.h"
17 #include "iopar.h"
18 #include "coord.h"
19 #include "windowfunction.h"
20 
25 template <class T>
26 mClass(Algo) Smoother2D : public Task
27 {
28 public:
29 
30  Smoother2D();
31  ~Smoother2D();
32 
33  void setInput(const Array2D<T>&,bool hasudf);
34  void setOutput(Array2D<T>&);
36  bool setWindow(const char* nm,float param,
37  int sz0,int sz1);
38  int getWindowSize(int dim) const;
39  const char* getWindowName() const;
40  float getWindowParam() const;
41 
42  inline void fillPar(IOPar&) const;
43  inline bool usePar(const IOPar&);
44 
45  inline void setProgressMeter(ProgressMeter* pm);
46  inline bool execute();
47  inline void enableWorkControl(bool);
48  inline void controlWork(Task::Control);
49  inline Task::Control getState() const;
50 
51  static const char* sKeyWinFunc() { return "Window function"; }
52  static const char* sKeyWinParam() { return "Window parameter"; }
53  static const char* sKeyWinSize() { return "Window size"; }
54 
55 protected:
56 
58  bool hasudf_;
59 
61  int windowsz0_, windowsz1_;
63  float windowparam_;
64 };
65 
66 
67 template <class T> inline
69  : windowparam_( mUdf(float) )
70  , window_( 0 )
71 {
72  convolver_.setNormalize( true );
73  convolver_.setCorrelate( false );
74 }
75 
76 
77 template <class T> inline
79 { delete window_; }
80 
81 
82 template <class T> inline
83 int Smoother2D<T>::getWindowSize(int dim) const
84 { return window_->info().getSize( dim ); }
85 
86 
87 template <class T> inline
88 const char* Smoother2D<T>::getWindowName() const
89 { return windowname_.buf(); }
90 
91 
92 template <class T> inline
94 { return windowparam_; }
95 
96 
97 template <class T> inline
98 void Smoother2D<T>::setInput( const Array2D<T>& ni, bool hasudf )
99 {
100  const Array2D<float>* input = convolver_.getX();
101 
102  if ( !input || hasudf_!=hasudf || input->info()!=ni.info() )
103  { delete window_; window_ = 0; }
104 
105  convolver_.setX( ni, hasudf );
106  hasudf_ = hasudf;
107 }
108 
109 
110 template <class T> inline
112 {
113  convolver_.setZ( no );
114 }
115 
116 
117 template <class T> inline
118 bool Smoother2D<T>::setWindow( const char* nm, float param,
119  int sz0, int sz1 )
120 {
121  windowname_ = nm;
122  windowparam_ = param;
123  windowsz0_ = sz0;
124  windowsz1_ = sz1;
125 
126  delete window_; window_ = 0;
127 
128  return true;
129 }
130 
131 
132 template <class T> inline
133 void Smoother2D<T>::fillPar( IOPar& par ) const
134 {
135  par.set( sKeyWinFunc(), windowname_ );
136  if ( !mIsUdf(windowparam_) )
137  par.set( sKeyWinParam(), windowparam_ );
138 
139  par.set(sKeyWinSize(),window_->info().getSize(0),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 
162 mImplSetFunc( setProgressMeter, ProgressMeter* );
163 mImplSetFunc( enableWorkControl, bool);
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() &&
180  (mIsUdf(windowparam_) || !wf->setVariable( windowparam_ ) ) )
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
190  window_ = new Array2DImpl<T>( windowsz0_, windowsz1_ );
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( (float) pos.abs() );
215  window_->set( idx0, idx1, weight );
216  weightsum += weight;
217  }
218  }
219 
220  if ( weightsum>1 )
221  mDoArrayPtrOperation( float, window_->getData(), /= (float)weightsum,
222  window_->info().getTotalSz(), ++ );
223 
224  convolver_.setY( *window_, false );
225  }
226 
227  return convolver_.execute();
228 }
229 
230 
231 template <class T> inline
233 { return convolver_.getState(); }
234 
Smoother2D::~Smoother2D
~Smoother2D()
Definition: smoother2d.h:78
IOPar::set
void set(const char *ky, const char *val)
Smoother2D::setOutput
void setOutput(Array2D< T > &)
Definition: smoother2d.h:111
task.h
Smoother2D::sKeyWinParam
static const char * sKeyWinParam()
Definition: smoother2d.h:52
IOPar::get
bool get(const char *, short &) const
Smoother2D
Smoothes a 2d signal with an operator.
Definition: smoother2d.h:27
Smoother2D::windowname_
BufferString windowname_
Definition: smoother2d.h:62
Smoother2D::window_
Array2DImpl< T > * window_
Definition: smoother2d.h:60
convolve2d.h
mIsUdf
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:289
ProgressMeter
is an interface where processes can report their progress.
Definition: progressmeter.h:21
Smoother2D::getWindowParam
float getWindowParam() const
Definition: smoother2d.h:93
WindowFunction::hasVariable
virtual bool hasVariable() const
Definition: windowfunction.h:30
usePar
void usePar(const IOPar &iop, ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:187
Task::Control
Control
Definition: task.h:55
Geom::Point2D::abs
double abs() const
Definition: geometry.h:376
arrayndimpl.h
windowfunction.h
Smoother2D::getWindowName
const char * getWindowName() const
Definition: smoother2d.h:88
Coord
A cartesian coordinate in 2D space.
Definition: coord.h:25
Convolver2D
Convolves (or correlates) two 2D signals.
Definition: convolve2d.h:34
Smoother2D::getWindowSize
int getWindowSize(int dim) const
Definition: smoother2d.h:83
Task
Generalization of something (e.g. a computation) that needs to be done in multiple steps.
Definition: task.h:28
mClass
#define mClass(module)
Definition: commondefs.h:181
Smoother2D::setInput
void setInput(const Array2D< T > &, bool hasudf)
Definition: smoother2d.h:98
PtrMan
Definition: ptrman.h:121
Smoother2D::windowsz1_
int windowsz1_
Definition: smoother2d.h:61
fillPar
void fillPar(IOPar &iop, const ODPolygon< T > &poly, const char *inpkey)
Definition: polygon.h:174
Array2D::info
virtual const Array2DInfo & info() const =0
Smoother2D::usePar
bool usePar(const IOPar &)
Definition: smoother2d.h:144
Smoother2D::sKeyWinSize
static const char * sKeyWinSize()
Definition: smoother2d.h:53
WindowFunction::setVariable
virtual bool setVariable(float)
Definition: windowfunction.h:32
mCast
#define mCast(tp, v)
Definition: commondefs.h:137
BufferString
OD::String with its own variable length buffer. The buffer has a guaranteed minimum size.
Definition: bufstring.h:40
mImplSetFunc
#define mImplSetFunc(func, vartype)
Definition: smoother2d.h:158
Smoother2D::sKeyWinFunc
static const char * sKeyWinFunc()
Definition: smoother2d.h:51
mDoArrayPtrOperation
#define mDoArrayPtrOperation(type, arr, operation, arrsz, ptrinc)
Definition: commondefs.h:285
Smoother2D::getState
Task::Control getState() const
Definition: smoother2d.h:232
Array2DImpl
Implementation of Array2D.
Definition: arrayndimpl.h:102
Smoother2D::convolver_
Convolver2D< T > convolver_
Definition: smoother2d.h:57
IOPar::find
const char * find(const char *) const
returns null if not found
Smoother2D::fillPar
void fillPar(IOPar &) const
Definition: smoother2d.h:133
MathFunction::getValue
virtual RT getValue(PT) const =0
Smoother2D::execute
bool execute()
Definition: smoother2d.h:167
Smoother2D::setWindow
bool setWindow(const char *nm, float param, int sz0, int sz1)
Definition: smoother2d.h:118
mUdf
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:274
Smoother2D::Smoother2D
Smoother2D()
Definition: smoother2d.h:68
iopar.h
IOPar
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:55
Array2D
Array2D ( Subclass of ArrayND ) is a two dimensional array.
Definition: arraynd.h:140
ArrayND::getData
const T * getData() const
Definition: arraynd.h:54
Smoother2D::windowparam_
float windowparam_
Definition: smoother2d.h:63
Smoother2D::hasudf_
bool hasudf_
Definition: smoother2d.h:58
coord.h

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