Range.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 // Include max() and min() missing from MicroSoft Visual C++.
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "Range.h"
18 
19 #include <algorithm>
20 
21 #include <ostream>
22 
23 #include <cassert>
24 #include <cmath>
25 
26 #ifndef _MSC_VER
27 using std::floor;
28 #endif
29 
30 using std::ostream;
31 using std::max;
32 using std::min;
33 using std::vector;
34 
35 using namespace hippodraw;
36 
38  : m_min( 0.0 ),
39  m_max( 0.0 ),
40  m_pos( DBL_MAX ),
41  m_empty( true )
42 {
43 }
44 
45 Range::Range ( double x, double y, double p )
46 {
47 
48  // x and y are being passed as 'nan' in some cases, leading to failure
49  // of assertion below. I dont know why they are being passed as nan and
50  // from where.
51 
52  if ( x > y ) {
53  m_min = 0;
54  m_max = 0;
55  m_pos = DBL_MAX;
56  m_empty = true;
57  }
58  else{
59  m_min = x;
60  m_max = y;
61  m_pos = p;
62  m_empty = false;
63  }
64 
65  // This assertion is failing.
66  //assert ( m_min <= m_max );
67 
68  //This is a dirty fix:
69  if ( ! ( m_min <= m_max ) ){
70  m_min = 0;
71  m_max = 0.1;
72  m_pos = DBL_MAX;
73  m_empty = true;
74  }
75 
76  assert ( m_min <= m_max );
77 
78 }
79 
80 Range::
81 Range ( const std::vector < double > & array )
82 {
83  setRange ( array.begin(), array.end () );
84 }
85 
86 double
87 Range::low() const
88 {
89  return m_min;
90 }
91 
92 void
93 Range::setLow ( double x )
94 {
95  m_min = x;
96  assert ( m_min <= m_max );
97 }
98 
99 double
100 Range::high() const
101 {
102  return m_max;
103 }
104 
105 void
106 Range::setHigh ( double x )
107 {
108  m_max = x;
109  assert ( m_min <= m_max );
110 }
111 
112 double
113 Range::pos() const
114 {
115  return m_pos;
116 }
117 
118 void
119 Range::setPos (double x)
120 {
121  m_pos = x;
122  assert ( m_min <= m_max );
123 }
124 
125 void
126 Range::setRange ( double low, double high, double pos )
127 {
128  m_min = low;
129  m_max = high;
130  m_pos = pos;
131  assert ( m_min <= m_max );
132 }
133 
134 void
135 Range::setLength ( double val, bool high_hold )
136 {
137  if( high_hold ){
138  m_min = m_max - val;
139  } else {
140  m_max = m_min + val;
141  }
142  assert ( m_min <= m_max );
143 }
144 
145 bool
146 Range::includes ( double val ) const
147 {
148  return val >= m_min && val <= m_max;
149 }
150 
151 bool
152 Range::
153 excludes ( double value ) const
154 {
155  return value < m_min || value > m_max;
156 }
157 
158 double
159 Range::
160 fraction ( double value ) const
161 {
162  return ( value - m_min ) / ( m_max - m_min );
163 }
164 
165 void Range::setEmpty ( bool yes )
166 {
167  m_empty = yes;
168 }
169 
170 void Range::setUnion ( const Range & range )
171 {
172  if ( m_empty ) {
173  m_min = range.m_min;
174  m_max = range.m_max;
175  m_pos = range.m_pos;
176  m_empty = false;
177  }
178  else {
179  m_min = min ( m_min, range.m_min );
180  m_max = max ( m_max, range.m_max );
181  m_pos = min ( m_pos, range.m_pos );
182  }
183 
184  assert ( m_min <= m_max );
185 
186 }
187 
188 void Range::setIntersect ( const Range & range )
189 {
190  if ( m_min > range.m_max || m_max < range.m_min ) return;
191  m_min = max ( m_min, range.m_min );
192  m_max = min ( m_max, range.m_max );
193  m_pos = max ( m_pos, range.m_min );
194 
195  assert ( m_min <= m_max );
196 }
197 
198 int
199 Range::numberOfBins ( double width ) const
200 {
201  assert ( m_max > m_min );
202  double number = (m_max - m_min) / width;
203 
204 #ifdef _MSC_VER
205  return static_cast < int > ( number+0.5 );
206 #else
207  return static_cast < int > ( rint( number ) );
208 #endif
209 }
bool m_empty
A flag indicating that the range is empty.
Definition: Range.h:52
double high() const
Returns the maximum of the range object.
Definition: Range.cxx:100
Range()
The following constructors sets the range to {0.0, 0.0} but sets m_empty to true. ...
Definition: Range.cxx:37
double m_max
The maximum in the range.
Definition: Range.h:43
double fraction(double value) const
Returns the fraction of the range that value represents.
Definition: Range.cxx:160
double m_pos
The minimum positive value in the range, for log scale.
Definition: Range.h:47
void setLow(double x)
Sets the minimum of the range object.
Definition: Range.cxx:93
bool includes(double value) const
Returns true if the argument value is inside the range.
Definition: Range.cxx:146
return yes
Definition: CanvasView.cxx:883
void setHigh(double x)
Sets the maximum of the range object.
Definition: Range.cxx:106
void setLength(double val, bool high_hold=false)
Sets the length of the range.
Definition: Range.cxx:135
void setRange(double low, double high, double pos)
Changes the current Range.
Definition: Range.cxx:126
bool excludes(double value) const
Returns true if the argument value is outside the range.
Definition: Range.cxx:153
void setPos(double x)
Sets the first positive element in range.
Definition: Range.cxx:119
void setIntersect(const Range &range)
Forms the intersect with the range in the argument.
Definition: Range.cxx:188
void setEmpty(bool yes=true)
Sets the range to empty.
Definition: Range.cxx:165
hippodraw::Range class interface
double low() const
Returns the minimum of the range object.
Definition: Range.cxx:87
void setUnion(const Range &range)
Forms the union with the range range.
Definition: Range.cxx:170
Expresses a range of values.
Definition: Range.h:33
int numberOfBins(double width) const
Returns the number of bins of width width required to fill the range.
Definition: Range.cxx:199
double m_min
The minimum in the range.
Definition: Range.h:39
double pos() const
Returns the first positive element in range.
Definition: Range.cxx:113

Generated for HippoDraw Class Library by doxygen