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

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