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

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