OpendTect  6.3
genericnumer.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: Kristofer Tingdahl
8  Date: 07-10-1999
9 ________________________________________________________________________
10 
11 
12 */
13 
14 #include "algomod.h"
15 #include "mathfunc.h"
16 #include <math.h>
17 
26 template <class A, class B, class C>
27 inline void GenericConvolve( int lx, int ifx, const A& x,
28  int ly, int ify, const B& y,
29  int lz, int ifz, C& z)
30 {
31  int ilx=ifx+lx-1,ily=ify+ly-1,ilz=ifz+lz-1,i,j,jlow,jhigh;
32  float sum;
33 
34  for ( i=ifz; i<=ilz; ++i )
35  {
36  jlow = i-ily;
37  if ( jlow < ifx ) jlow = ifx;
38 
39  jhigh = i-ify;
40  if ( jhigh > ilx ) jhigh = ilx;
41 
42  for ( j=jlow,sum=0.0; j<=jhigh; ++j )
43  {
44  if( !mIsUdf(x[j-ifx]) && !mIsUdf(y[i-j-ify]) )
45  sum += x[j-ifx]*y[i-j-ify];
46  }
47 
48  z[i-ifz] = sum;
49  }
50 }
51 
52 
53 template <class A, class B, class C>
54 inline void GenericConvolveNoUdf( int lx, int ifx, const A& x,
55  int ly, int ify, const B& y,
56  int lz, int ifz, C& z)
57 {
58  int ilx=ifx+lx-1,ily=ify+ly-1,ilz=ifz+lz-1,i,j,jlow,jhigh;
59  float sum;
60 
61  for ( i=ifz; i<=ilz; ++i )
62  {
63  jlow = i-ily;
64  if ( jlow < ifx ) jlow = ifx;
65 
66  jhigh = i-ify;
67  if ( jhigh > ilx ) jhigh = ilx;
68 
69  for ( j=jlow,sum=0.0; j<=jhigh; ++j )
70  sum += x[j-ifx]*y[i-j-ify];
71 
72  z[i-ifz] = sum;
73  }
74 }
75 
76 
77 
81 template <class A, class B>
82 inline float similarity( const A& a, const B& b, int sz, bool normalize=false,
83  int firstposa=0, int firstposb=0 )
84 {
85  float val1, val2;
86  double sqdist = 0, sq1 = 0, sq2 = 0;
87 
88  double meana = mUdf(double), stddeva = mUdf(double);
89  double meanb = mUdf(double), stddevb = mUdf(double);
90 
91  if ( normalize )
92  {
93  if ( sz==1 ) normalize = false;
94  else
95  {
96  double asum=0,bsum=0;
97  for ( int idx=0; idx<sz; idx++ )
98  {
99  asum += a[firstposa+idx];
100  bsum += b[firstposb+idx];
101  }
102 
103  meana = asum/sz;
104  meanb = bsum/sz;
105 
106  asum = 0;
107  bsum = 0;
108  for ( int idx=0; idx<sz; idx++ )
109  {
110  const double adiff = a[firstposa+idx]-meana;
111  const double bdiff = b[firstposb+idx]-meanb;
112  asum += adiff*adiff;
113  bsum += bdiff*bdiff;
114  }
115 
116  stddeva = Math::Sqrt(asum/(sz-1));
117  stddevb = Math::Sqrt(bsum/(sz-1));
118 
119  if ( mIsZero(stddeva,mDefEps) || mIsZero(stddevb,mDefEps) )
120  normalize=false;
121  }
122  }
123 
124  int curposa = firstposa;
125  int curposb = firstposb;
126 
127  for ( int idx=0; idx<sz; idx++ )
128  {
129  val1 = normalize ? (float) ( (a[curposa]-meana)/stddeva ) : a[curposa];
130  val2 = normalize ? (float) ( (b[curposb]-meanb)/stddevb ) : b[curposb];
131  if ( mIsUdf(val1) || mIsUdf(val2) )
132  return mUdf(float);
133 
134  sq1 += val1 * val1;
135  sq2 += val2 * val2;
136  sqdist += (val1-val2) * (val1-val2);
137 
138  curposa ++;
139  curposb ++;
140  }
141 
142  if ( mIsZero(sq1,mDefEps) && mIsZero(sq2,mDefEps) )
143  return 1;
144 
145  if ( mIsZero(sq1,mDefEps) || mIsZero(sq2,mDefEps) )
146  return 0;
147 
148  const float rt =
149  (float) ( Math::Sqrt(sqdist) / (Math::Sqrt(sq1) + Math::Sqrt(sq2)) );
150  return 1 - rt;
151 }
152 
153 
154 mGlobal(Algo) float similarity(const FloatMathFunction&,
155  const FloatMathFunction&,
156  float x1, float x2, float dist, int sz, bool normalize );
157 
158 
159 mGlobal(Algo) float semblance( const ObjectSet<float>& trcs,
160  const Interval<int>& );
161 
162 mGlobal(Algo) float semblance( const ObjectSet<float>& trcs,int signalsize,
163  const TypeSet<float>& signalstarts,
164  const Interval<int>& gate );
165 
166 mGlobal(Algo) double LanczosKernel( int size, double x );
167 
174 mGlobal(Algo) bool findValue(const FloatMathFunction&,float x1,float x2,
175  float& res,float targetval = 0,float tol=1e-5);
176 
177 
185 mGlobal(Algo) float findValueInAperture(const FloatMathFunction&,float startx,
186  const Interval<float>& aperture,float dx,float target=0,
187  float tol=1e-5);
188 
196 mGlobal(Algo) float findExtreme(const FloatMathFunction&,bool minima,float x1,
197  float x2,float tol = 1e-5);
198 
199 
200 template <class A, class B> inline
201 void reSample( const FloatMathFunction& input, const A& samplevals,
202  B& output, int nrsamples )
203 {
204  for ( int idx=0; idx<nrsamples; idx++ )
205  {
206  const float sampleval = samplevals[idx];
207  output[idx] = Values::isUdf(sampleval) ? mUdf(float) :
208  input.getValue(sampleval);
209  }
210 }
211 
212 
227 template <class A, class B, class C>
228 inline void genericCrossCorrelation( int lx, int ifx, const A& x,
229  int ly, int ify, const B& y,
230  int lz, int ifz, C& z)
231 {
232  mAllocLargeVarLenArr( float, xreversed, lx );
233 
234  for ( int i=0,j=lx-1; i<lx; ++i,--j)
235  xreversed[i] = x[j];
236  GenericConvolve( lx, 1-ifx-lx, xreversed, ly, ify, y, lz, ifz, z );
237 }
238 
239 
240 template <class A>
241 inline void reverseArray( A* in, int sz, A* out=0 )
242 {
243  if ( out )
244  for ( int idx=0; idx<sz; idx++ )
245  out[idx] = in[sz-1-idx];
246  else
247  {
248  mAllocVarLenArr( A, tmparr, sz/2 );
249  for ( int idx=0; idx<sz/2; idx++ )
250  {
251  tmparr[idx] = in[idx];
252  int opsamp = sz-1-idx;
253  in[idx] = in[opsamp];
254  in[opsamp] = tmparr[idx];
255  }
256  }
257 }
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:285
double LanczosKernel(int size, double x)
#define mGlobal(module)
Definition: commondefs.h:160
#define mIsZero(x, eps)
Definition: commondefs.h:55
void GenericConvolve(int lx, int ifx, const A &x, int ly, int ify, const B &y, int lz, int ifz, C &z)
Definition: genericnumer.h:27
Mathematical function.
Definition: mathfunc.h:59
void genericCrossCorrelation(int lx, int ifx, const A &x, int ly, int ify, const B &y, int lz, int ifz, C &z)
Definition: genericnumer.h:228
float semblance(const ObjectSet< float > &trcs, const Interval< int > &)
float findExtreme(const FloatMathFunction &, bool minima, float x1, float x2, float tol=1e-5)
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:270
float similarity(const A &a, const B &b, int sz, bool normalize=false, int firstposa=0, int firstposb=0)
Definition: genericnumer.h:82
float findValueInAperture(const FloatMathFunction &, float startx, const Interval< float > &aperture, float dx, float target=0, float tol=1e-5)
#define mAllocVarLenArr(type, varnm, __size)
Definition: varlenarray.h:52
#define mDefEps
Definition: commondefs.h:60
void reverseArray(A *in, int sz, A *out=0)
Definition: genericnumer.h:241
bool findValue(const FloatMathFunction &, float x1, float x2, float &res, float targetval=0, float tol=1e-5)
void GenericConvolveNoUdf(int lx, int ifx, const A &x, int ly, int ify, const B &y, int lz, int ifz, C &z)
Definition: genericnumer.h:54
#define mAllocLargeVarLenArr(type, varnm, __size)
Definition: varlenarray.h:29
bool isUdf(const T &t)
Definition: undefval.h:241
virtual RT getValue(PT) const =0
float Sqrt(float)
void reSample(const FloatMathFunction &input, const A &samplevals, B &output, int nrsamples)
Definition: genericnumer.h:201

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