OpendTect  6.6
indexinfo.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: A.H. Bril
8  Date: Jan 2006
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "basicmod.h"
15 #include "samplingdata.h"
16 
17 
23 {
24 public:
25  IndexInfo( int i, bool r=true, bool u=false )
26  : nearest_(i)
27  , roundedtolow_(r)
28  , inundef_(u) {}
29  template <class X,class Y>
30  IndexInfo( const StepInterval<X>& s, Y y )
31  { set( s, y ); }
32  template <class X,class Y>
33  IndexInfo( const SamplingData<X>& s, Y y, int len )
34  { set( s, y, len ); }
35  template <class T>
36  IndexInfo(const T*,int sz,T val);
37 
38  template <class X,class Y>
39  void set(const StepInterval<X>&,Y);
40  template <class X,class Y>
41  void set(const SamplingData<X>&,Y,int length);
42 
43 
44  int nearest_;
46  bool inundef_;
47 };
48 
49 
50 template <class T> inline
51 IndexInfo::IndexInfo( const T* arr, int sz, T val )
52  : nearest_(0)
53  , roundedtolow_(true)
54  , inundef_(true)
55 {
56  if ( sz < 1 || !arr )
57  return;
58  if ( sz == 1 )
59  { inundef_ = val != arr[0]; return; }
60  const bool isrev = arr[0] > arr[sz-1];
61  if ( (isrev && val >= arr[0]) || (!isrev && val<=arr[0]) )
62  { inundef_ = val != arr[0]; roundedtolow_ = isrev; return; }
63  if ( (!isrev && val >= arr[sz-1]) || (isrev && val<=arr[sz-1]) )
64  { nearest_ = sz-1; inundef_ = val != arr[sz-1]; roundedtolow_ = !isrev;
65  return; }
66 
67  inundef_ = false;
68  for ( nearest_=1; nearest_<sz; nearest_++ )
69  {
70  if ( arr[nearest_] == val )
71  return;
72  if ( (!isrev && val < arr[nearest_]) || (isrev && val > arr[nearest_]) )
73  {
74  T halfway = (arr[nearest_] + arr[nearest_-1]) * .5f;
75  roundedtolow_ = isrev ? val > halfway : val < halfway;
76  if ( (!isrev && roundedtolow_) || (isrev && !roundedtolow_) )
77  nearest_ -= 1;
78  return;
79  }
80  }
81  // Can we get here? Better safe than sorry.
82  nearest_ = sz - 1; inundef_ = true; roundedtolow_ = !isrev;
83 }
84 
85 
86 template <class X,class Y> inline
87 void IndexInfo::set( const StepInterval<X>& intv, Y y )
88 {
89  const bool isrev = intv.step < 0;
90  const Y hstep = intv.step * 0.5;
91 
92  if ( (isrev && y>intv.start+hstep) || (!isrev && y<intv.start-hstep) )
93  { inundef_ = true; roundedtolow_ = false; nearest_ = 0; }
94  else if ( (isrev && y< intv.stop-hstep) || (!isrev && y>intv.stop+hstep) )
95  { inundef_ = true; roundedtolow_ = true; nearest_ = intv.nrSteps(); }
96  else
97  {
98  inundef_ = false;
99  nearest_ = intv.getIndex( y );
100  const Y pred = intv.atIndex( nearest_ );
101  roundedtolow_ = isrev ? pred > y : pred < y;
102  }
103 }
104 
105 template <class X,class Y> inline
106 void IndexInfo::set( const SamplingData<X>& sd, Y y, int nr )
107 {
108  return set( StepInterval<X>( sd.start, sd.atIndex(nr-1), sd.step ), y );
109 }
110 
111 
IndexInfo::IndexInfo
IndexInfo(const SamplingData< X > &s, Y y, int len)
Definition: indexinfo.h:33
StepInterval::nrSteps
int nrSteps() const
Definition: ranges.h:791
IndexInfo::IndexInfo
IndexInfo(const StepInterval< X > &s, Y y)
Definition: indexinfo.h:30
mExpClass
#define mExpClass(module)
Definition: commondefs.h:177
StepInterval::atIndex
T atIndex(int) const
Definition: ranges.h:695
samplingdata.h
IndexInfo
Info on (floating-point) position in an array or StepInterval.
Definition: indexinfo.h:23
StepInterval::getIndex
int getIndex(const X &) const
Definition: ranges.h:700
IndexInfo::set
void set(const StepInterval< X > &, Y)
Definition: indexinfo.h:87
StepInterval
Interval with step.
Definition: commontypes.h:32
SamplingData::atIndex
T atIndex(IT) const
Definition: samplingdata.h:164
SamplingData
Holds the fundamental sampling info: start and interval.
Definition: samplingdata.h:22
IndexInfo::inundef_
bool inundef_
Definition: indexinfo.h:46
SamplingData::start
T start
Definition: samplingdata.h:49
Conv::set
void set(T &_to, const F &fr)
template based type conversion
Definition: convert.h:27
IndexInfo::IndexInfo
IndexInfo(int i, bool r=true, bool u=false)
Definition: indexinfo.h:25
IndexInfo::nearest_
int nearest_
Definition: indexinfo.h:44
IndexInfo::roundedtolow_
bool roundedtolow_
Definition: indexinfo.h:45
sKey::Y
FixedString Y()
Definition: keystrs.h:193
SamplingData::step
T step
Definition: samplingdata.h:50
StepInterval::step
T step
Definition: ranges.h:200

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