OpendTect-6_4  6.4
indexinfo.h
Go to the documentation of this file.
1 #ifndef indexinfo_h
2 #define indexinfo_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H. Bril
9  Date: Jan 2006
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "basicmod.h"
16 #include "samplingdata.h"
17 
18 
24 {
25 public:
26  IndexInfo( int i, bool r=true, bool u=false )
27  : nearest_(i)
28  , roundedtolow_(r)
29  , inundef_(u) {}
30  template <class X,class Y>
31  IndexInfo( const StepInterval<X>& s, Y y )
32  { set( s, y ); }
33  template <class X,class Y>
34  IndexInfo( const SamplingData<X>& s, Y y, int len )
35  { set( s, y, len ); }
36  template <class T>
37  IndexInfo(const T*,int sz,T val);
38 
39  template <class X,class Y>
40  void set(const StepInterval<X>&,Y);
41  template <class X,class Y>
42  void set(const SamplingData<X>&,Y,int length);
43 
44 
45  int nearest_;
47  bool inundef_;
48 };
49 
50 
51 template <class T> inline
52 IndexInfo::IndexInfo( const T* arr, int sz, T val )
53  : nearest_(0)
54  , roundedtolow_(true)
55  , inundef_(true)
56 {
57  if ( sz < 1 || !arr )
58  return;
59  if ( sz == 1 )
60  { inundef_ = val != arr[0]; return; }
61  const bool isrev = arr[0] > arr[sz-1];
62  if ( (isrev && val >= arr[0]) || (!isrev && val<=arr[0]) )
63  { inundef_ = val != arr[0]; roundedtolow_ = isrev; return; }
64  if ( (!isrev && val >= arr[sz-1]) || (isrev && val<=arr[sz-1]) )
65  { nearest_ = sz-1; inundef_ = val != arr[sz-1]; roundedtolow_ = !isrev;
66  return; }
67 
68  inundef_ = false;
69  for ( nearest_=1; nearest_<sz; nearest_++ )
70  {
71  if ( arr[nearest_] == val )
72  return;
73  if ( (!isrev && val < arr[nearest_]) || (isrev && val > arr[nearest_]) )
74  {
75  T halfway = (arr[nearest_] + arr[nearest_-1]) * .5f;
76  roundedtolow_ = isrev ? val > halfway : val < halfway;
77  if ( (!isrev && roundedtolow_) || (isrev && !roundedtolow_) )
78  nearest_ -= 1;
79  return;
80  }
81  }
82  // Can we get here? Better safe than sorry.
83  nearest_ = sz - 1; inundef_ = true; roundedtolow_ = !isrev;
84 }
85 
86 
87 template <class X,class Y> inline
88 void IndexInfo::set( const StepInterval<X>& intv, Y y )
89 {
90  const bool isrev = intv.step < 0;
91  const Y hstep = intv.step * 0.5;
92 
93  if ( (isrev && y>intv.start+hstep) || (!isrev && y<intv.start-hstep) )
94  { inundef_ = true; roundedtolow_ = false; nearest_ = 0; }
95  else if ( (isrev && y< intv.stop-hstep) || (!isrev && y>intv.stop+hstep) )
96  { inundef_ = true; roundedtolow_ = true; nearest_ = intv.nrSteps(); }
97  else
98  {
99  inundef_ = false;
100  nearest_ = intv.getIndex( y );
101  const Y pred = intv.atIndex( nearest_ );
102  roundedtolow_ = isrev ? pred > y : pred < y;
103  }
104 }
105 
106 template <class X,class Y> inline
107 void IndexInfo::set( const SamplingData<X>& sd, Y y, int nr )
108 {
109  return set( StepInterval<X>( sd.start, sd.atIndex(nr-1), sd.step ), y );
110 }
111 
112 
113 #endif
#define mExpClass(module)
Definition: commondefs.h:160
IndexInfo(const SamplingData< X > &s, Y y, int len)
Definition: indexinfo.h:34
T step
Definition: samplingdata.h:50
Info on (floating-point) position in an array or StepInterval.
Definition: indexinfo.h:23
int nrSteps() const
Definition: ranges.h:771
T atIndex(int) const
Definition: ranges.h:687
IndexInfo(const StepInterval< X > &s, Y y)
Definition: indexinfo.h:31
int getIndex(const X &) const
Definition: ranges.h:692
T atIndex(IT) const
Definition: samplingdata.h:158
T step
Definition: ranges.h:199
int nearest_
Definition: indexinfo.h:45
T start
Definition: samplingdata.h:49
Interval with step.
Definition: commontypes.h:33
T stop
Definition: ranges.h:93
void set(const StepInterval< X > &, Y)
Definition: indexinfo.h:88
T start
Definition: ranges.h:92
Holds the fundamental sampling info: start and interval.
Definition: samplingdata.h:22
bool inundef_
Definition: indexinfo.h:47
bool roundedtolow_
Definition: indexinfo.h:46
IndexInfo(int i, bool r=true, bool u=false)
Definition: indexinfo.h:26

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