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

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