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

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