OpendTect-6_4  6.4
periodicvalue.h
Go to the documentation of this file.
1 #ifndef periodicvalue_h
2 #define periodicvalue_h
3 
4 /*
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kris Tingdahl
9  Date: 12-4-1999
10  Contents: Periodic value interpolation and so forth
11  RCS: $Id$
12 ________________________________________________________________________
13 
14 */
15 
16 #include "simpnumer.h"
17 #include "idxable.h"
18 
19 
25 template <class T>
26 inline T dePeriodize( T val, T period )
27 {
28  int n = (int) (val / period);
29  if ( val < 0 ) n--;
30 
31  return n ? val - n * period : val;
32 }
33 
34 
39 template <class T,int P>
41 {
42 public:
43  T val(bool positive=true) const
44  {
45  T res = dePeriodize(val_,(T)P);
46  if ( !positive && res > ((T)P)/2 )
47  return res-P;
48  return res;
49  }
56  { return PeriodicValue<T,P>(val_+nv); }
58  { return PeriodicValue<T,P>(val_-nv); }
60  { return PeriodicValue<T,P>(val_*nv); }
62  { return PeriodicValue<T,P>(val_/nv); }
64  { return PeriodicValue<T,P>(val_+nv.val()); }
66  { return PeriodicValue<T,P>(val_-nv.val()); }
68  { return PeriodicValue<T,P>(val_*nv.val()); }
70  { return PeriodicValue<T,P>(val_/nv.val()); }
71 
72  const PeriodicValue<T,P>& operator=(T nv) const
73  { val_ = nv; return this; }
75  { val_ += nv; return this; }
77  { val_ -= nv; return this; }
79  { val_ *= nv; return this; }
81  { val_ /= nv; return this; }
83  { val_ = nv.val(); return this; }
85  { val_ += nv.val(); return this; }
87  { val_ -= nv.val(); return this; }
89  { val_ *= nv.val(); return this; }
91  { val_ /= nv.val(); return this; }
92 
93  bool operator<(const PeriodicValue<T,P>& b) const
94  {
95  PeriodicValue<T,P> tmp = *this-b;
96  if ( tmp.val(true)>P/2 ) return true;
97  return false;
98  }
99  bool operator>(const PeriodicValue<T,P>& b) const
100  {
101  PeriodicValue<T,P> tmp = *this-b;
102  if ( tmp.val(true)<=P/2 ) return true;
103  return false;
104  }
105  bool operator<(T b) const
106  { return *this < PeriodicValue<T,P>(b); }
107  bool operator>(T b) const
108  { return *this > PeriodicValue<T,P>(b); }
109 
110 
111  PeriodicValue(T nv) : val_( nv ) {}
112 
113 protected:
114  T val_;
115 };
116 
117 
118 namespace IdxAble
119 {
120 
126 template <class T, class RT>
127 inline void interpolateYPeriodicReg( const T& idxabl, int sz, float pos,
128  RT& ret, RT period, bool extrapolate=false )
129 {
130  const float halfperiod = period / 2;
131  int intpos = mNINT32( pos );
132  float dist = pos - intpos;
133  if( mIsZero(dist,1e-10) && intpos >= 0 && intpos < sz )
134  { ret = idxabl[intpos]; return; }
135 
136  int prevpos = dist > 0 ? intpos : intpos - 1;
137  if ( !extrapolate && (prevpos > sz-2 || prevpos < 0) )
138  ret = mUdf(RT);
139  else if ( prevpos < 1 )
140  {
141  const float val0 = idxabl[0];
142  RT val1 = idxabl[1];
143  while ( val1 - val0 > halfperiod ) val1 -= period;
144  while ( val1 - val0 < -halfperiod ) val1 += period;
145 
146  ret = dePeriodize( Interpolate::linearReg1D( val0, val1, pos ),
147  period );
148  }
149  else if ( prevpos > sz-3 )
150  {
151  const RT val0 = idxabl[sz-2];
152  RT val1 = idxabl[sz-1];
153  while ( val1 - val0 > halfperiod ) val1 -= period;
154  while ( val1 - val0 < -halfperiod ) val1 += period;
155  ret = dePeriodize( Interpolate::linearReg1D( val0, val1, pos-(sz-2) ),
156  period );
157  }
158  else
159  {
160  const RT val0 = idxabl[prevpos-1];
161 
162  RT val1 = idxabl[prevpos];
163  while ( val1 - val0 > halfperiod ) val1 -= period;
164  while ( val1 - val0 < -halfperiod ) val1 += period;
165 
166  RT val2 = idxabl[prevpos+1];
167  while ( val2 - val1 > halfperiod ) val2 -= period;
168  while ( val2 - val1 < -halfperiod ) val2 += period;
169 
170  RT val3 = idxabl[prevpos+2];
171  while ( val3 - val2 > halfperiod ) val3 -= period;
172  while ( val3 - val2 < -halfperiod ) val3 += period;
173 
174  ret = dePeriodize(Interpolate::polyReg1D( val0, val1, val2, val3,
175  pos - prevpos ), period );
176  }
177 }
178 
179 
180 template <class T>
181 inline float interpolateYPeriodicReg( const T& idxabl, int sz, float pos,
182  float period, bool extrapolate=false )
183 {
184  float ret = mUdf(float);
185  interpolateYPeriodicReg( idxabl, sz, pos, ret, period, extrapolate );
186  return ret;
187 }
188 
189 
196 template <class T, class RT>
197 inline void interpolateXPeriodicReg( const T& idxabl, int sz, float pos,
198  RT& ret)
199 {
200  int intpos = mNINT32( pos );
201  float dist = pos - intpos;
202  if( mIsZero(dist,1e-10) && intpos >= 0 && intpos < sz )
203  { ret = idxabl[intpos]; return; }
204 
205  int prevpos = dist > 0 ? intpos : intpos - 1;
206  const float relpos = pos - prevpos;
207  prevpos = dePeriodize( prevpos, sz );
208 
209  int prevpos2 = prevpos - 1;
210  prevpos2 = dePeriodize( prevpos2, sz );
211 
212  int nextpos = prevpos + 1;
213  nextpos = dePeriodize( nextpos, sz );
214 
215  int nextpos2 = prevpos + 2;
216  nextpos2 = dePeriodize( nextpos2, sz );
217 
218  const RT prevval2 = idxabl[prevpos2];
219  const RT prevval = idxabl[prevpos];
220  const RT nextval = idxabl[nextpos];
221  const RT nextval2 = idxabl[nextpos2];
222 
223  ret = Interpolate::polyReg1D( prevval2, prevval, nextval, nextval2, relpos);
224 }
225 
226 } // namespace IdxAble
227 
228 #endif
PeriodicValue(T nv)
Definition: periodicvalue.h:111
#define mIsZero(x, eps)
Definition: commondefs.h:53
const PeriodicValue< T, P > & operator-=(const PeriodicValue< T, P > &nv)
Definition: periodicvalue.h:86
void interpolateXPeriodicReg(const T &idxabl, int sz, float pos, RT &ret)
Definition: periodicvalue.h:197
const PeriodicValue< T, P > & operator/=(T nv)
Definition: periodicvalue.h:80
T polyReg1D(T vm1, T v0, T v1, T v2, float x)
Definition: interpol1d.h:130
const PeriodicValue< T, P > & operator-=(T nv)
Definition: periodicvalue.h:76
PeriodicValue handles periodic data through mathematical operations.
Definition: periodicvalue.h:40
PeriodicValue< T, P > operator+(const PeriodicValue< T, P > &nv) const
Definition: periodicvalue.h:63
const PeriodicValue< T, P > & operator=(const PeriodicValue< T, P > &nv) const
Definition: periodicvalue.h:82
PeriodicValue< T, P > operator*(const PeriodicValue< T, P > &nv) const
Definition: periodicvalue.h:67
PeriodicValue< T, P > operator-(const PeriodicValue< T, P > &nv) const
Definition: periodicvalue.h:65
#define mNINT32(x)
Definition: commondefs.h:45
const T val2
Definition: arrayndalgo.h:1699
T val_
Definition: periodicvalue.h:114
PeriodicValue< T, P > operator/(const PeriodicValue< T, P > &nv) const
Definition: periodicvalue.h:69
PeriodicValue< T, P > operator+(T nv) const
Definition: periodicvalue.h:55
PeriodicValue< T, P > operator/(T nv) const
Definition: periodicvalue.h:61
Definition: seistype.h:61
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
T val(bool positive=true) const
Definition: periodicvalue.h:43
bool operator<(T b) const
Definition: periodicvalue.h:105
void interpolateYPeriodicReg(const T &idxabl, int sz, float pos, RT &ret, RT period, bool extrapolate=false)
Definition: periodicvalue.h:127
T linearReg1D(T v0, T v1, float x)
Definition: interpol1d.h:46
const PeriodicValue< T, P > & operator=(T nv) const
Definition: periodicvalue.h:72
const PeriodicValue< T, P > & operator/=(const PeriodicValue< T, P > &nv)
Definition: periodicvalue.h:90
bool operator>(T b) const
Definition: periodicvalue.h:107
Position-sorted indexable objects.
Definition: idxable.h:30
PeriodicValue< T, P > operator*(T nv) const
Definition: periodicvalue.h:59
T dePeriodize(T val, T period)
Definition: periodicvalue.h:26
const RefTree & RT()
const PeriodicValue< T, P > & operator*=(T nv)
Definition: periodicvalue.h:78
const PeriodicValue< T, P > & operator+=(T nv)
Definition: periodicvalue.h:74
bool operator>(const PeriodicValue< T, P > &b) const
Definition: periodicvalue.h:99
PeriodicValue< T, P > operator-(T nv) const
Definition: periodicvalue.h:57
const PeriodicValue< T, P > & operator*=(const PeriodicValue< T, P > &nv)
Definition: periodicvalue.h:88
#define mClass(module)
Definition: commondefs.h:164
const T val1
Definition: arrayndalgo.h:1699
const PeriodicValue< T, P > & operator+=(const PeriodicValue< T, P > &nv)
Definition: periodicvalue.h:84

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