VTK-m  1.6
Range.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 
11 #ifndef vtk_m_Range_h
12 #define vtk_m_Range_h
13 
14 #include <vtkm/Assert.h>
15 #include <vtkm/Math.h>
16 #include <vtkm/Types.h>
17 
18 namespace vtkm
19 {
20 
30 struct Range
31 {
34 
37  : Min(vtkm::Infinity64())
38  , Max(vtkm::NegativeInfinity64())
39  {
40  }
41 
42  Range(const Range&) = default;
43  Range(Range&&) = default;
44 
45  template <typename T1, typename T2>
46  VTKM_EXEC_CONT Range(const T1& min, const T2& max)
47  : Min(static_cast<vtkm::Float64>(min))
48  , Max(static_cast<vtkm::Float64>(max))
49  {
50  }
51 
52  vtkm::Range& operator=(const vtkm::Range& src) = default;
53  vtkm::Range& operator=(vtkm::Range&& src) = default;
54 
65  bool IsNonEmpty() const { return (this->Min <= this->Max); }
66 
73  template <typename T>
74  VTKM_EXEC_CONT bool Contains(const T& value) const
75  {
76  return ((this->Min <= static_cast<vtkm::Float64>(value)) &&
77  (this->Max >= static_cast<vtkm::Float64>(value)));
78  }
79 
87  {
88  if (this->IsNonEmpty())
89  {
90  return (this->Max - this->Min);
91  }
92  else
93  {
94  return 0.0;
95  }
96  }
97 
105  {
106  if (this->IsNonEmpty())
107  {
108  return 0.5 * (this->Max + this->Min);
109  }
110  else
111  {
112  return vtkm::Nan64();
113  }
114  }
115 
122  template <typename T>
123  VTKM_EXEC_CONT void Include(const T& value)
124  {
125  this->Min = vtkm::Min(this->Min, static_cast<vtkm::Float64>(value));
126  this->Max = vtkm::Max(this->Max, static_cast<vtkm::Float64>(value));
127  }
128 
135  void Include(const vtkm::Range& range)
136  {
137  if (range.IsNonEmpty())
138  {
139  this->Min = vtkm::Min(this->Min, range.Min);
140  this->Max = vtkm::Max(this->Max, range.Max);
141  }
142  }
143 
149  vtkm::Range Union(const vtkm::Range& otherRange) const
150  {
151  vtkm::Range unionRange(*this);
152  unionRange.Include(otherRange);
153  return unionRange;
154  }
155 
159  vtkm::Range operator+(const vtkm::Range& otherRange) const { return this->Union(otherRange); }
160 
162  bool operator==(const vtkm::Range& otherRange) const
163  {
164  return ((this->Min == otherRange.Min) && (this->Max == otherRange.Max));
165  }
166 
168  bool operator!=(const vtkm::Range& otherRange) const
169  {
170  return ((this->Min != otherRange.Min) || (this->Max != otherRange.Max));
171  }
172 };
173 
176 inline VTKM_CONT std::ostream& operator<<(std::ostream& stream, const vtkm::Range& range)
177 {
178  return stream << "[" << range.Min << ".." << range.Max << "]";
179 } // Declared inside of vtkm namespace so that the operator work with ADL lookup
180 } // namespace vtkm
181 
182 
183 #endif //vtk_m_Range_h
vtkm
VTKM_NO_DEPRECATED_VIRTUAL.
Definition: Algorithms.h:23
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Range::Range
VTKM_EXEC_CONT Range(const T1 &min, const T2 &max)
Definition: Range.h:46
vtkm::Range::Include
VTKM_EXEC_CONT void Include(const T &value)
Expand range to include a value.
Definition: Range.h:123
Assert.h
vtkm::Range::Include
VTKM_EXEC_CONT void Include(const vtkm::Range &range)
Expand range to include other range.
Definition: Range.h:135
vtkm::Range::operator=
vtkm::Range & operator=(const vtkm::Range &src)=default
vtkm::Range::Center
VTKM_EXEC_CONT vtkm::Float64 Center() const
Returns the center of the range.
Definition: Range.h:104
vtkm::Range::Length
VTKM_EXEC_CONT vtkm::Float64 Length() const
Returns the length of the range.
Definition: Range.h:86
Math.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Range::IsNonEmpty
VTKM_EXEC_CONT bool IsNonEmpty() const
Determine if the range is valid (i.e.
Definition: Range.h:65
vtkm::Range::Range
VTKM_EXEC_CONT Range()
Definition: Range.h:36
vtkm::Range::operator==
VTKM_EXEC_CONT bool operator==(const vtkm::Range &otherRange) const
Definition: Range.h:162
vtkm::Range::Union
VTKM_EXEC_CONT vtkm::Range Union(const vtkm::Range &otherRange) const
Return the union of this and another range.
Definition: Range.h:149
vtkm::Range::Contains
VTKM_EXEC_CONT bool Contains(const T &value) const
Determines if a value is within the range.
Definition: Range.h:74
vtkm::Range::Min
vtkm::Float64 Min
Definition: Range.h:32
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::Range::Max
vtkm::Float64 Max
Definition: Range.h:33
vtkm::Range::operator!=
VTKM_EXEC_CONT bool operator!=(const vtkm::Range &otherRange) const
Definition: Range.h:168
vtkm::Range::operator+
VTKM_EXEC_CONT vtkm::Range operator+(const vtkm::Range &otherRange) const
Operator for union
Definition: Range.h:159
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:30