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

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