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

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