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

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