OpendTect-6_4  6.4
polynd.h
Go to the documentation of this file.
1 #ifndef polynd_h
2 #define polynd_h
3 
4 /*@+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kristofer Tingdahl
9  Date: 10-12-1999
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 @$*/
14 
15 #include "linsolv.h"
16 #include "arrayndimpl.h"
17 #include "arrayndalgo.h"
18 
25 template <class T>
27 {
28 public:
29  PolynomialND( const ArrayNDInfo& );
30  ~PolynomialND( );
31 
32  bool fit( const ArrayND<T>& );
33 
34  void setCoeff( const int* pos, T val )
35  { coeffs(pos) = val; }
36 
37  T getCoeff( const int* pos ) const
38  { return coeffs.get( pos ); }
39 
40  T getValue( const TypeSet<float>& ) const;
41  T getValue3D( float x0, float x1, float x2 ) const;
42 
43 protected:
46 };
47 
48 
49 template <class T>
51  : coeffs( size_ )
52  , solver( 0 )
53 {}
54 
55 
56 template <class T>
58 { delete solver; }
59 
60 
61 template <class T> inline
63 {
64  ArrayNDIter coeffiter( coeffs.info() );
65 
66  const int ndim = coeffs.info().getNDim();
67 
68  T res = 0;
69 
70  do
71  {
72  float posproduct = 1;
73  for ( int idx=0; idx<ndim; idx++ )
74  posproduct *= Math::IntPowerOf( pos[idx], coeffiter[idx] );
75 
76  res += posproduct * coeffs.getND( coeffiter.getPos() );
77  } while ( coeffiter.next() );
78 
79  return res;
80 }
81 
82 template <class T> inline
83 T PolynomialND<T>::getValue3D( float p0, float p1, float p2 ) const
84 {
85  const ArrayNDInfo& size = coeffs.info();
86  if ( size.getNDim() != 3 || size.getTotalSz() != 64 )
87  {
88  TypeSet<float> pos( 3,0 );
89  pos[0] = p0; pos[1] = p1; pos[2] = p2;
90 
91  return getValue( pos );
92  }
93 
94  float p0_2 = p0 * p0; float p0_3 = p0_2 * p0;
95  float p1_2 = p1 * p1; float p1_3 = p1_2 * p1;
96  float p2_2 = p2 * p2; float p2_3 = p2_2 * p2;
97 
98  const float p0p1 = p0 * p1;
99  const float p0p1_2 = p0p1 * p1;
100  const float p0p1_3 = p0p1_2 * p1;
101  const float p0_2p1 = p0_2 * p1;
102  const float p0_2p1_2 = p0_2p1 * p1;
103  const float p0_2p1_3 = p0_2p1_2 * p1;
104  const float p0_3p1 = p0_3 * p1;
105  const float p0_3p1_2 = p0_3p1 * p1;
106  const float p0_3p1_3 = p0_3p1_2 * p1;
107 
108  const T* ptr = coeffs.getData();
109 
110  T res = ptr[0] +
111  ptr[1] * p2 +
112  ptr[2] * p2_2 +
113  ptr[3] * p2_3 +
114 
115  ptr[4] * p1 +
116  ptr[5] * p1 * p2 +
117  ptr[6] * p1 * p2_2 +
118  ptr[7] * p1 * p2_3 +
119 
120  ptr[8] * p1_2 +
121  ptr[9] * p1_2 * p2 +
122  ptr[10] * p1_2 * p2_2 +
123  ptr[11] * p1_2 * p2_3 +
124 
125  ptr[12] * p1_3 +
126  ptr[13] * p1_3 * p2 +
127  ptr[14] * p1_3 * p2_2 +
128  ptr[15] * p1_3 * p2_3 +
129 
130 
131  ptr[16] * p0 +
132  ptr[17] * p0 * p2 +
133  ptr[18] * p0 * p2_2 +
134  ptr[19] * p0 * p2_3 +
135 
136  ptr[20] * p0p1 +
137  ptr[21] * p0p1 * p2 +
138  ptr[22] * p0p1 * p2_2 +
139  ptr[23] * p0p1 * p2_3 +
140 
141  ptr[24] * p0p1_2 +
142  ptr[25] * p0p1_2 * p2 +
143  ptr[26] * p0p1_2 * p2_2 +
144  ptr[27] * p0p1_2 * p2_3 +
145 
146  ptr[28] * p0p1_3 +
147  ptr[29] * p0p1_3 * p2 +
148  ptr[30] * p0p1_3 * p2_2 +
149  ptr[31] * p0p1_3 * p2_3 +
150 
151 
152  ptr[32] * p0_2 +
153  ptr[33] * p0_2 * p2 +
154  ptr[34] * p0_2 * p2_2 +
155  ptr[35] * p0_2 * p2_3 +
156 
157  ptr[36] * p0_2p1 +
158  ptr[37] * p0_2p1 * p2 +
159  ptr[38] * p0_2p1 * p2_2 +
160  ptr[39] * p0_2p1 * p2_3 +
161 
162  ptr[40] * p0_2p1_2 +
163  ptr[41] * p0_2p1_2 * p2 +
164  ptr[42] * p0_2p1_2 * p2_2 +
165  ptr[43] * p0_2p1_2 * p2_3 +
166 
167  ptr[44] * p0_2p1_3 +
168  ptr[45] * p0_2p1_3 * p2 +
169  ptr[46] * p0_2p1_3 * p2_2 +
170  ptr[47] * p0_2p1_3 * p2_3 +
171 
172 
173 
174  ptr[48] * p0_3 +
175  ptr[49] * p0_3 * p2 +
176  ptr[50] * p0_3 * p2_2 +
177  ptr[51] * p0_3 * p2_3 +
178 
179  ptr[52] * p0_3p1 +
180  ptr[53] * p0_3p1 * p2 +
181  ptr[54] * p0_3p1 * p2_2 +
182  ptr[55] * p0_3p1 * p2_3 +
183 
184  ptr[56] * p0_3p1_2 +
185  ptr[57] * p0_3p1_2 * p2 +
186  ptr[58] * p0_3p1_2 * p2_2 +
187  ptr[59] * p0_3p1_2 * p2_3 +
188 
189  ptr[60] * p0_3p1_3 +
190  ptr[61] * p0_3p1_3 * p2 +
191  ptr[62] * p0_3p1_3 * p2_2 +
192  ptr[63] * p0_3p1_3 * p2_3;
193 
194  return res;
195 }
196 
197 
198 template<class T>
199 bool PolynomialND<T>::fit( const ArrayND<T>& input )
200 {
201  const int totalsz = mCast( int, input.info().getTotalSz() );
202 
203  if ( !solver || solver->size() != totalsz )
204  {
205  if ( solver ) delete solver;
206 
207  Array2DImpl<T> poscoeffs(totalsz,totalsz);
208 
209  ArrayNDIter positer( input.info() );
210  const int ndim = input.info().getNDim();
211  int row = 0;
212  do
213  {
214  int col = 0;
215  ArrayNDIter powiter( input.info() );
216 
217  do
218  {
219  int coeff = 1;
220  for ( int idx=0; idx<ndim; idx++ )
221  coeff *= Math::IntPowerOf( positer[idx], powiter[idx] );
222 
223  poscoeffs.set( row, col, (T)coeff );
224  col++;
225  } while ( powiter.next() );
226 
227  row++;
228  } while ( positer.next() );
229 
230  solver = new LinSolver<T>( poscoeffs );
231  if ( !solver->init() )
232  return false;
233  }
234 
235  solver->apply( input.getData(), coeffs.getData() );
236 
237  return true;
238 }
239 
240 #endif
virtual uint64_t getTotalSz() const
virtual int getNDim() const =0
LinSolver< T > * solver
Definition: polynd.h:45
T getValue3D(float x0, float x1, float x2) const
Definition: polynd.h:83
#define mCast(tp, v)
Definition: commondefs.h:124
Implementation of Array2D.
Definition: arrayndimpl.h:102
Contains the information about the size of ArrayND, and in what order the data is stored (if accessab...
Definition: arrayndinfo.h:23
ArrayNDImpl< T > coeffs
Definition: polynd.h:44
const T * getData() const
Definition: arraynd.h:55
LinSolver - Solves linear systems of equations on the form A*x=B. A is a matrix of the size N*N...
Definition: gridder2d.h:24
PolynomialND(const ArrayNDInfo &)
Definition: polynd.h:50
Implementation of ArrayND.
Definition: arrayndimpl.h:200
T getValue(const TypeSet< float > &) const
Definition: polynd.h:62
An ArrayND is an array with a given number of dimensions and a size.
Definition: arraynd.h:33
Iterates through all samples in an ArrayND.
Definition: arraynd.h:179
void setCoeff(const int *pos, T val)
Definition: polynd.h:34
void set(int, int, T)
Definition: arrayndimpl.h:448
bool fit(const ArrayND< T > &)
Definition: polynd.h:199
T getCoeff(const int *pos) const
Definition: polynd.h:37
~PolynomialND()
Definition: polynd.h:57
virtual const ArrayNDInfo & info() const =0
#define mClass(module)
Definition: commondefs.h:164
iT IntPowerOf(iT i, iPOW p)
Definition: math2.h:122
PolynomialND is a N-dimensional polynomial with arbitary orders in each dimension. It can be fitted any ArrayND. To access the polynomial&#39;s data use getValue. getValue3D is optimized for third order, tree-dimensional cases.
Definition: polynd.h:26

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