OpendTect  6.6
axislayout.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 2005 / Dec 2009
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "algomod.h"
15 #include "ranges.h"
16 #include "samplingdata.h"
17 
18 #include <math.h>
19 
20 
25 template <class T>
27 {
28 public:
29  // Have layout calculated
30  AxisLayout( const Interval<T>& dr, bool asint=false,
31  bool inside=false )
32  : annotinint_(asint)
33  , annotinsiderg_(inside)
34  { setDataRange( dr ); }
35 
36  void setDataRange(const Interval<T>&);
37  StepInterval<T> getSampling() const;
38 
40  T stop_;
43 
44  // Init with explicit layout
45  AxisLayout( T start=0, T st_op=1,
46  T step=1 )
47  : sd_(start,step), stop_(st_op)
48  , annotinsiderg_(false)
49  , annotinint_(false) {}
50 
52  : sd_(rg.start,rg.step)
53  , stop_(rg.stop)
54  , annotinsiderg_(false)
55  , annotinint_(false) {}
56 
57  // Returns 'modulo' end with this sd_ and stop_
58  T findEnd(T datastop) const;
59 };
60 
61 
62 template <class T> inline
64 { return StepInterval<T>( sd_.start, stop_, sd_.step ); }
65 
66 
67 template <class T> inline
69 {
70  Interval<T> intv = dr;
71  sd_.start = intv.start;
72  const bool rev = intv.start > intv.stop;
73  if ( rev ) Swap( intv.start, intv.stop );
74  T wdth = intv.width();
75 
76 
77  // guard against zero interval
78  T indic = intv.start + wdth;
79  T indic_start = intv.start;
80  if ( mIsZero(indic,mDefEps) ) { indic_start += 1; indic += 1; }
81  indic = 1 - indic_start / indic;
82  if ( mIsZero(indic,mDefEps) )
83  {
84  sd_.start = intv.start - 1;
85  sd_.step = 1;
86  stop_ = intv.start + 1;
87  return;
88  }
89 
90  double scwdth = wdth < 1e-30 ? -30 : log10( (double)wdth );
91  int tenpow = 1 - (int)scwdth; if ( scwdth < 0 ) tenpow++;
92  double stepfac = Math::IntPowerOf( ((double)10), tenpow );
93  scwdth = wdth * stepfac;
94 
95  double scstep;
96  if ( scwdth < 15 ) scstep = annotinint_ ? 2. : 2.5;
97  else if ( scwdth < 30 ) scstep = 5;
98  else if ( scwdth < 50 ) scstep = 10;
99  else scstep = 20;
100 
101  sd_.step = (T) ( scstep / stepfac );
102  if ( annotinint_ )
103  {
104  int istep = mNINT32( sd_.step );
105  if ( istep == 0 )
106  istep = 1;
107  sd_.step = (T)istep;
108  }
109 
110  if ( mIsZero(sd_.step,mDefEps) )
111  sd_.step = 1;
112 
113  if ( wdth > 1e-30 )
114  {
115  double idx0 = 0;
116  if ( annotinsiderg_ )
117  {
118  idx0 = rev ? Math::Floor(intv.stop / sd_.step + 1e-6)
119  : Math::Ceil(intv.start / sd_.step + 1e-6);
120  }
121  else
122  {
123  idx0 = rev ? Math::Ceil(intv.stop / sd_.step + 1e-6)
124  : Math::Floor(intv.start / sd_.step + 1e-6);
125  }
126 
127  sd_.start = mNINT32( idx0 ) * sd_.step;
128  }
129  if ( rev ) sd_.step = -sd_.step;
130 
131  stop_ = findEnd( rev ? intv.start : intv.stop );
132 }
133 
134 
135 template <class T> inline
136 T AxisLayout<T>::findEnd( T datastop ) const
137 {
138  SamplingData<T> worksd = sd_;
139  const bool rev = worksd.step < 0;
140  if ( rev )
141  {
142  worksd.start = -worksd.start;
143  worksd.step = -worksd.step;
144  datastop = -datastop;
145  }
146 
147  if ( worksd.start + 10000 * worksd.step < datastop )
148  return datastop;
149 
150  const double dnrsteps = double(datastop-worksd.start)/worksd.step - 1e-6;
151  int nrsteps =
152  mNINT32( (annotinsiderg_ ? Math::Floor(dnrsteps)
153  : Math::Ceil(dnrsteps)) );
154  if ( nrsteps < 1 ) nrsteps = 1;
155  T wdth = nrsteps * worksd.step;
156  return sd_.start + (rev ? -wdth : wdth);
157 }
158 
AxisLayout::setDataRange
void setDataRange(const Interval< T > &)
Definition: axislayout.h:68
Swap
void Swap(T &a, T &b)
Definition: commondefs.h:46
AxisLayout::annotinsiderg_
bool annotinsiderg_
Definition: axislayout.h:42
AxisLayout::AxisLayout
AxisLayout(const StepInterval< T > &rg)
Definition: axislayout.h:51
Math::Floor
float Floor(float)
samplingdata.h
mDefEps
#define mDefEps
Definition: commondefs.h:71
AxisLayout::stop_
T stop_
Definition: axislayout.h:40
ArrayMath::if
if(!noudf_ &&(mIsUdf(inpval)))
Definition: arrayndalgo.h:1682
AxisLayout::findEnd
T findEnd(T datastop) const
Definition: axislayout.h:136
Math::IntPowerOf
iT IntPowerOf(iT i, iPOW p)
Definition: math2.h:121
AxisLayout::getSampling
StepInterval< T > getSampling() const
Definition: axislayout.h:63
AxisLayout::sd_
SamplingData< T > sd_
Definition: axislayout.h:39
AxisLayout::AxisLayout
AxisLayout(const Interval< T > &dr, bool asint=false, bool inside=false)
Definition: axislayout.h:30
StepInterval
Interval with step.
Definition: commontypes.h:32
AxisLayout::AxisLayout
AxisLayout(T start=0, T st_op=1, T step=1)
Definition: axislayout.h:45
mIsZero
#define mIsZero(x, eps)
Definition: commondefs.h:66
mClass
#define mClass(module)
Definition: commondefs.h:181
SamplingData
Holds the fundamental sampling info: start and interval.
Definition: samplingdata.h:22
SamplingData::start
T start
Definition: samplingdata.h:49
AxisLayout::annotinint_
bool annotinint_
Definition: axislayout.h:41
Math::Ceil
float Ceil(float)
ranges.h
Interval
Interval of values.
Definition: commontypes.h:30
SamplingData::step
T step
Definition: samplingdata.h:50
mNINT32
#define mNINT32(x)
Definition: commondefs.h:58
AxisLayout
Helps making nice axes for graphs.
Definition: axislayout.h:27

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