OpendTect-6_4  6.4
sampledextremefindernd.h
Go to the documentation of this file.
1 #ifndef sampledextremefindernd_h
2 #define sampledextremefindernd_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: K. Tingdahl
9  Date: July 2008
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "dataclipper.h"
16 #include "thread.h"
17 #include "paralleltask.h"
18 
23 template <class T>
25 {
26 public:
27  SampledExtremeFinderND(const ArrayND<T>& arr, bool minima)
28  : array_( arr )
29  , minima_( minima )
30  , relcube_( arr.info().getNDim() )
31  {
32  const int ndim = array_.info().getNDim();
33  for ( int idx=0; idx<ndim; idx++ )
34  relcube_.setSize( idx, 3 );
35  }
36 
37  inline int nrExtremes() const;
38 
39  const int* getExtremes() const { return extremes_.arr(); }
42 
43 protected:
44  inline od_int64 nrIterations() const;
45  inline bool doWork(od_int64,od_int64,int);
46  inline bool findExtreme(int*) const;
47  inline int indexOf(const int*) const;
48 
51  bool minima_;
54 };
55 
56 
57 template <class T> inline
59 { return array_.info().getTotalSz(); }
60 
61 
62 template <class T> inline
64 {
65  const int ndim = array_.info().getNDim();
66  return extremes_.size()/ndim;
67 }
68 
69 
70 template <class T> inline
72 {
73  const int ndim = array_.info().getNDim();
74  mAllocVarLenArr( int, pos, ndim );
75  if ( !array_.info().getArrayPos( start, pos ) )
76  return false;
77 
78  ArrayNDIter iter( array_.info() );
79  iter.setPos<int*>( pos );
80 
81  mAllocVarLenArr( int, currentextreme, ndim );
82  for ( int idx=mCast(int,start); idx<=stop && shouldContinue();
83  idx++, addToNrDone(1), iter.next() )
84  {
85  OD::sysMemCopy( currentextreme, iter.getPos(), ndim*sizeof(int) );
86  if ( !findExtreme( currentextreme ) )
87  continue;
88 
89  lock_.readLock();
90 
91  int extremeidx = indexOf( currentextreme );
92  if ( extremeidx!=-1 )
93  {
94  lock_.readUnLock();
95  continue;
96  }
97 
98  if ( !lock_.convReadToWriteLock() )
99  {
100  extremeidx = indexOf( currentextreme );
101  if ( extremeidx!=-1 )
102  {
103  lock_.writeUnLock();
104  continue;
105  }
106  }
107 
108  for ( int idy=0; idy<ndim; idy++ )
109  extremes_ += currentextreme[idy];
110 
111  lock_.writeUnLock();
112  }
113 
114  return true;
115 }
116 
117 
118 template <class T> inline
119 int SampledExtremeFinderND<T>::indexOf( const int* pos ) const
120 {
121  const int nrextremes = nrExtremes();
122  const int ndim = array_.info().getNDim();
123 
124  for ( int idx=0; idx<nrextremes; idx++ )
125  {
126  const int* curpos = extremes_.arr() + idx*ndim;
127  bool found = true;
128  for ( int idy=0; idy<ndim; idy++ )
129  {
130  if ( curpos[idy]!=pos[idy] )
131  {
132  found = false;
133  break;
134  }
135  }
136 
137  if ( found )
138  return idx;
139  }
140 
141  return -1;
142 }
143 
144 
145 template <class T> inline
146 bool SampledExtremeFinderND<T>::findExtreme( int* extremepos ) const
147 {
148  const int ndim = array_.info().getNDim();
149 
150  T extremeval = array_.getND( extremepos );
151 
152  mAllocVarLenArr( int, curpos, ndim );
153  mAllocVarLenArr( int, bestpos, ndim );
154  OD::sysMemCopy( bestpos, extremepos, ndim*sizeof(int) );
155 
156  bool change = true;
157  bool anychange = false;
158 
159  while ( change )
160  {
161  ArrayNDIter iter( relcube_ );
162  change = false;
163  do
164  {
165  bool invalid = false;
166  bool isnull = true;
167  for ( int idx=0; idx<ndim; idx++ )
168  {
169  if ( iter[idx] ) isnull = false;
170  const int newpos = extremepos[idx]+iter[idx]-1;
171  if ( newpos<0 || newpos>=array_.info().getSize(idx) )
172  {
173  invalid = true;
174  break;
175  }
176 
177  curpos[idx] = newpos;
178  }
179 
180  if ( invalid || isnull )
181  continue;
182 
183  const T val = array_.getND( curpos );
184  if ( (minima_ && val<extremeval) || (!minima_ && val>extremeval) )
185  {
186  OD::sysMemCopy( bestpos, curpos, ndim*sizeof(int) );
187  extremeval = val;
188  change = true;
189  }
190  } while ( iter.next() );
191 
192  if ( change )
193  {
194  OD::sysMemCopy( extremepos, bestpos, ndim*sizeof(int) );
195  anychange = true;
196  }
197  }
198 
199  return anychange;
200 }
201 
202 
203 #endif
Lock that permits multiple readers to lock the object at the same time, but it will not allow any rea...
Definition: thread.h:141
Finds all local maximas/minimas in an ArrayND.
Definition: sampledextremefindernd.h:24
int nrExtremes() const
Definition: sampledextremefindernd.h:63
Threads::ReadWriteLock lock_
Definition: sampledextremefindernd.h:53
ArrayNDInfoImpl relcube_
Definition: sampledextremefindernd.h:50
#define mCast(tp, v)
Definition: commondefs.h:124
#define od_int64
Definition: plftypes.h:36
Implementation of ArrayNDInfo.
Definition: arrayndinfo.h:219
TypeSet< int > extremes_
Definition: sampledextremefindernd.h:52
bool findExtreme(int *) const
Definition: sampledextremefindernd.h:146
ObjectSet< T >::size_type indexOf(const ObjectSet< T > &os, const S &val)
Locate object in set.
Definition: objectset.h:169
bool doWork(od_int64, od_int64, int)
Definition: sampledextremefindernd.h:71
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
float findExtreme(const FloatMathFunction &, bool minima, float x1, float x2, float tol=1e-5)
const ArrayND< T > & array_
Definition: sampledextremefindernd.h:49
od_int64 nrIterations() const
Definition: sampledextremefindernd.h:58
void setPos(const T &idxabl)
Definition: arraynd.h:256
int indexOf(const int *) const
Definition: sampledextremefindernd.h:119
#define mAllocVarLenArr(type, varnm, __size)
Definition: varlenarray.h:54
const int * getExtremes() const
Definition: sampledextremefindernd.h:39
bool minima_
Definition: sampledextremefindernd.h:51
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 sysMemCopy(void *, const void *, int64_t)
SampledExtremeFinderND(const ArrayND< T > &arr, bool minima)
Definition: sampledextremefindernd.h:27
#define mClass(module)
Definition: commondefs.h:164

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