OpendTect  6.6
smoother1d.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: May 2007
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "paralleltask.h"
15 #include "valseries.h"
16 #include "genericnumer.h"
17 #include "typeset.h"
18 #include "iopar.h"
19 #include "bufstring.h"
20 #include "windowfunction.h"
21 
26 template <class T>
28 {
29 public:
30 
33 
34  bool operator==(const Smoother1D<T>&) const;
35 
36  void setInput(const T*,int sz);
37  void setOutput(T*);
39  bool setWindow(const char* nm,float param,
40  int lenght );
41  int getWindowLength() const {return window_.size();}
42  const char* getWindowName() const{return windowname_.buf();}
43  float getWindowParam() const{return windowparam_;}
44 
45  inline void fillPar(IOPar&) const;
46  inline bool usePar(const IOPar&);
47 
48 protected:
49 
50  static const char* sKeyWinFunc() { return "Window function"; }
51  static const char* sKeyWinParam() { return "Window parameter"; }
52  static const char* sKeyWinLen() { return "Window length"; }
53 
54  inline od_int64 nrIterations() const { return size_; }
55  inline bool doPrepare(int);
56  inline bool doWork(od_int64 start,od_int64 stop,int);
57 
60  float windowparam_;
61 
62  const T* input_;
63  T* output_;
65 
68 };
69 
70 
71 template <class T> inline
73  : input_( 0 )
74  , output_( 0 )
75  , windowparam_( mUdf(float) )
76  , firstdefined_( -1 )
77  , lastdefined_( -1 )
78  , size_( -1 )
79 {}
80 
81 
82 template <class T> inline
84  : input_( 0 )
85  , output_( 0 )
86  , windowparam_( b.windowparam_ )
87  , window_( b.window_ )
88  , windowname_( b.windowname_ )
89  , firstdefined_( -1 )
90  , lastdefined_( -1 )
91  , size_( -1 )
92 { }
93 
94 
95 template <class T> inline
96 void Smoother1D<T>::setInput( const T* ni , int sz )
97 {
98  input_ = ni;
99  size_ = sz;
100 }
101 
102 
103 template <class T> inline
105 {
106  output_ = ni;
107 }
108 
109 
110 template <class T> inline
112 {
113  return window_.size()==b.window_.size() &&
114  windowname_==b.windowname_ &&
115  mIsEqual(windowparam_, b.windowparam_, 1e-3 );
116 }
117 
118 
119 template <class T> inline
120 bool Smoother1D<T>::setWindow( const char* nm, float param, int length )
121 {
122  PtrMan<WindowFunction> wf = WINFUNCS().create( nm );
123  if ( !wf )
124  return false;
125 
126  if ( wf->hasVariable() && !wf->setVariable( param ) )
127  return false;
128 
129  if ( length<0 )
130  return false;
131 
132  window_.setSize( length );
133  const double step = 2.0/(length-1);
134  for ( int idx=0; idx<length; idx++ )
135  window_[idx] = wf->getValue( (float) (step*idx-1) );
136 
137  windowname_ = nm;
138  windowparam_ = (float) ( wf->hasVariable() ? param : 1e30 );
139 
140  return true;
141 }
142 
143 
144 template <class T> inline
145 void Smoother1D<T>::fillPar( IOPar& par ) const
146 {
147  par.set( sKeyWinFunc(), windowname_ );
148  if ( !mIsUdf(windowparam_) )
149  par.set( sKeyWinParam(), windowparam_ );
150  par.set( sKeyWinLen(), window_.size() );
151 }
152 
153 
154 template <class T> inline
155 bool Smoother1D<T>::usePar( const IOPar& par )
156 {
157  int sz;
158  if ( !par.get(sKeyWinLen(), sz ) )
159  return false;
160 
161  const char* wn = par.find( sKeyWinFunc() );
162  float var = mUdf(float);
163  par.get( sKeyWinParam(), var );
164 
165  return setWindow( wn, var, sz );
166 }
167 
168 
169 template <class T> inline
171 {
172  if ( !input_ || !output_ || !window_.size() )
173  return false;
174 
175  firstdefined_ = -1;
176  lastdefined_ = -1;
177  for ( int idx=0; idx<size_; idx++ )
178  {
179  if ( !mIsUdf(input_[idx]) )
180  {
181  firstdefined_=idx;
182  break;
183  }
184  }
185 
186  if ( firstdefined_!=-1 )
187  {
188  for ( int idx=mCast(int,size_-1); idx>=0; idx-- )
189  {
190  if ( !mIsUdf(input_[idx]) )
191  {
192  lastdefined_=idx;
193  break;
194  }
195  }
196  }
197 
198  return true;
199 }
200 
201 
202 template <class T> inline
204 {
205  const float* window = window_.arr();
206  const int windowsize = window_.size();
207  const int hwinsize = windowsize/2;
208 
209  for ( int outidx=mCast(int,start); outidx<=stop; outidx++, addToNrDone(1) )
210  {
211  if ( firstdefined_==-1 || outidx<firstdefined_ || outidx>lastdefined_ )
212  {
213  output_[outidx] = mUdf(T);
214  continue;
215  }
216 
217  int sumstart = outidx-hwinsize;
218  int sumstop = outidx+windowsize-hwinsize-1;
219  int winstart = 0;
220  if ( sumstart<0 )
221  {
222  winstart = -sumstart;
223  sumstart = 0;
224  }
225 
226  if ( sumstop>=size_ )
227  sumstop = mCast(int,size_-1);
228 
229  double sum = 0;
230  double weightsum = 0;
231  for ( int sumidx=sumstart, winidx=winstart;
232  sumidx<=sumstop; sumidx++, winidx++ )
233  {
234  double val = input_[sumidx];
235  if ( mIsUdf(val) )
236  continue;
237 
238  sum += val * window[winidx];
239  weightsum += window[winidx];
240  }
241 
242  output_[outidx] = (T) (weightsum ? sum/weightsum : mUdf(float));
243  }
244 
245  return true;
246 }
247 
Smoother1D::getWindowParam
float getWindowParam() const
Definition: smoother1d.h:43
IOPar::set
void set(const char *ky, const char *val)
IOPar::get
bool get(const char *, short &) const
Smoother1D::setOutput
void setOutput(T *)
Definition: smoother1d.h:104
mIsEqual
#define mIsEqual(x, y, eps)
Definition: commondefs.h:67
Smoother1D::setInput
void setInput(const T *, int sz)
Definition: smoother1d.h:96
valseries.h
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
Smoother1D::firstdefined_
int firstdefined_
Definition: smoother1d.h:66
Smoother1D::Smoother1D
Smoother1D()
Definition: smoother1d.h:72
WindowFunction::hasVariable
virtual bool hasVariable() const
Definition: windowfunction.h:30
Smoother1D::size_
od_int64 size_
Definition: smoother1d.h:64
Smoother1D::getWindowName
const char * getWindowName() const
Definition: smoother1d.h:42
Smoother1D::lastdefined_
int lastdefined_
Definition: smoother1d.h:67
typeset.h
Smoother1D::sKeyWinLen
static const char * sKeyWinLen()
Definition: smoother1d.h:52
windowfunction.h
bufstring.h
Smoother1D::windowparam_
float windowparam_
Definition: smoother1d.h:60
Smoother1D::window_
TypeSet< T > window_
Definition: smoother1d.h:58
Smoother1D::setWindow
bool setWindow(const char *nm, float param, int lenght)
Definition: smoother1d.h:120
mClass
#define mClass(module)
Definition: commondefs.h:181
Smoother1D::windowname_
BufferString windowname_
Definition: smoother1d.h:59
Smoother1D::getWindowLength
int getWindowLength() const
Definition: smoother1d.h:41
PtrMan
Definition: ptrman.h:121
Smoother1D
Smoothes a 1d signal with an operator.
Definition: smoother1d.h:28
Smoother1D::input_
const T * input_
Definition: smoother1d.h:62
Smoother1D::fillPar
void fillPar(IOPar &) const
Definition: smoother1d.h:145
WindowFunction::setVariable
virtual bool setVariable(float)
Definition: windowfunction.h:32
ParallelTask
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
Smoother1D::operator==
bool operator==(const Smoother1D< T > &) const
Definition: smoother1d.h:111
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
Smoother1D::usePar
bool usePar(const IOPar &)
Definition: smoother1d.h:155
Smoother1D::sKeyWinParam
static const char * sKeyWinParam()
Definition: smoother1d.h:51
IOPar::find
const char * find(const char *) const
returns null if not found
MathFunction::getValue
virtual RT getValue(PT) const =0
mUdf
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:274
iopar.h
Smoother1D::doWork
bool doWork(od_int64 start, od_int64 stop, int)
Definition: smoother1d.h:203
Smoother1D::doPrepare
bool doPrepare(int)
Definition: smoother1d.h:170
genericnumer.h
Smoother1D::Smoother1D
Smoother1D(const Smoother1D &)
Definition: smoother1d.h:83
paralleltask.h
Smoother1D::nrIterations
od_int64 nrIterations() const
Definition: smoother1d.h:54
Smoother1D::output_
T * output_
Definition: smoother1d.h:63
IOPar
Generalized set of parameters of the keyword-value type.
Definition: iopar.h:55
Smoother1D::sKeyWinFunc
static const char * sKeyWinFunc()
Definition: smoother1d.h:50
TypeSet
Sets of (small) copyable elements.
Definition: commontypes.h:29

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