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

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