VTK-m  2.2
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 #include <vtkm/VecTraits.h>
18 
19 namespace vtkm
20 {
21 
31 struct Range
32 {
37 
42  : Min(vtkm::Infinity64())
43  , Max(vtkm::NegativeInfinity64())
44  {
45  }
46 
47  Range(const Range&) = default;
48  Range(Range&&) = default;
49 
50  template <typename T1, typename T2>
51  VTKM_EXEC_CONT Range(const T1& min, const T2& max)
52  : Min(static_cast<vtkm::Float64>(min))
53  , Max(static_cast<vtkm::Float64>(max))
54  {
55  }
56 
57  vtkm::Range& operator=(const vtkm::Range& src) = default;
58  vtkm::Range& operator=(vtkm::Range&& src) = default;
59 
70  bool IsNonEmpty() const { return (this->Min <= this->Max); }
71 
78  template <typename T>
79  VTKM_EXEC_CONT bool Contains(const T& value) const
80  {
81  return ((this->Min <= static_cast<vtkm::Float64>(value)) &&
82  (this->Max >= static_cast<vtkm::Float64>(value)));
83  }
84 
92  {
93  if (this->IsNonEmpty())
94  {
95  return (this->Max - this->Min);
96  }
97  else
98  {
99  return 0.0;
100  }
101  }
102 
110  {
111  if (this->IsNonEmpty())
112  {
113  return 0.5 * (this->Max + this->Min);
114  }
115  else
116  {
117  return vtkm::Nan64();
118  }
119  }
120 
127  template <typename T>
128  VTKM_EXEC_CONT void Include(const T& value)
129  {
130  this->Min = vtkm::Min(this->Min, static_cast<vtkm::Float64>(value));
131  this->Max = vtkm::Max(this->Max, static_cast<vtkm::Float64>(value));
132  }
133 
140  void Include(const vtkm::Range& range)
141  {
142  if (range.IsNonEmpty())
143  {
144  this->Min = vtkm::Min(this->Min, range.Min);
145  this->Max = vtkm::Max(this->Max, range.Max);
146  }
147  }
148 
154  vtkm::Range Union(const vtkm::Range& otherRange) const
155  {
156  vtkm::Range unionRange(*this);
157  unionRange.Include(otherRange);
158  return unionRange;
159  }
160 
164  vtkm::Range Intersection(const vtkm::Range& otherRange) const
165  {
166  return vtkm::Range(vtkm::Max(this->Min, otherRange.Min), vtkm::Min(this->Max, otherRange.Max));
167  }
168 
172  vtkm::Range operator+(const vtkm::Range& otherRange) const { return this->Union(otherRange); }
173 
175  bool operator==(const vtkm::Range& otherRange) const
176  {
177  return ((this->Min == otherRange.Min) && (this->Max == otherRange.Max));
178  }
179 
181  bool operator!=(const vtkm::Range& otherRange) const
182  {
183  return ((this->Min != otherRange.Min) || (this->Max != otherRange.Max));
184  }
185 };
186 
189 inline VTKM_CONT std::ostream& operator<<(std::ostream& stream, const vtkm::Range& range)
190 {
191  return stream << "[" << range.Min << ".." << range.Max << "]";
192 } // Declared inside of vtkm namespace so that the operator work with ADL lookup
193 
194 template <>
196 {
199 
200  static constexpr vtkm::IdComponent NUM_COMPONENTS = 2;
202  {
203  return NUM_COMPONENTS;
204  }
207 
209  static const ComponentType& GetComponent(const vtkm::Range& range, vtkm::IdComponent component)
210  {
211  VTKM_ASSERT((component == 0) || (component == 1));
212  return (component == 0) ? range.Min : range.Max;
213  }
216  {
217  VTKM_ASSERT((component == 0) || (component == 1));
218  return (component == 0) ? range.Min : range.Max;
219  }
220 
222  static void SetComponent(vtkm::Range& range, vtkm::IdComponent component, ComponentType value)
223  {
224  VTKM_ASSERT((component == 0) || (component == 1));
225  if (component == 0)
226  {
227  range.Min = value;
228  }
229  else
230  {
231  range.Max = value;
232  }
233  }
234 
235  template <typename NewComponentType>
237  template <typename NewComponentType>
239 
240  template <vtkm::IdComponent destSize>
241  VTKM_EXEC_CONT static void CopyInto(const vtkm::Range& src,
243  {
244  const vtkm::IdComponent maxComponent = (destSize < NUM_COMPONENTS) ? destSize : NUM_COMPONENTS;
245  for (vtkm::IdComponent component = 0; component < maxComponent; ++component)
246  {
247  dest[component] = GetComponent(src, component);
248  }
249  }
250 };
251 
252 } // namespace vtkm
253 
254 
255 #endif //vtk_m_Range_h
vtkm::Range::operator!=
bool operator!=(const vtkm::Range &otherRange) const
Definition: Range.h:181
vtkm::Range::operator==
bool operator==(const vtkm::Range &otherRange) const
Definition: Range.h:175
vtkm::VecTraits< vtkm::Range >::GetNumberOfComponents
static constexpr vtkm::IdComponent GetNumberOfComponents(const vtkm::Range &)
Definition: Range.h:201
vtkm::Range::Range
Range()
Construct a range with a given minimum and maximum.
Definition: Range.h:41
vtkm::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:23
vtkm::Range::Contains
bool Contains(const T &value) const
Determines if a value is within the range.
Definition: Range.h:79
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::Range::Length
vtkm::Float64 Length() const
Returns the length of the range.
Definition: Range.h:91
vtkm::operator<<
std::ostream & operator<<(std::ostream &stream, const vtkm::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:248
vtkm::Range::operator+
vtkm::Range operator+(const vtkm::Range &otherRange) const
Operator for union
Definition: Range.h:172
Assert.h
vtkm::Range::Center
vtkm::Float64 Center() const
Returns the center of the range.
Definition: Range.h:109
vtkm::Range::operator=
vtkm::Range & operator=(const vtkm::Range &src)=default
vtkm::VecTraits< vtkm::Range >::BaseComponentType
vtkm::Float64 BaseComponentType
Definition: Range.h:198
vtkm::Range::Range
Range(const T1 &min, const T2 &max)
Definition: Range.h:51
Math.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::VecTraits< vtkm::Range >::ComponentType
vtkm::Float64 ComponentType
Definition: Range.h:197
vtkm::Range::Include
void Include(const T &value)
Expand range to include a value.
Definition: Range.h:128
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::VecTraits< vtkm::Range >::CopyInto
static void CopyInto(const vtkm::Range &src, vtkm::Vec< ComponentType, destSize > &dest)
Definition: Range.h:241
vtkm::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:36
vtkm::Range::Min
vtkm::Float64 Min
The minumum value of the range (inclusive).
Definition: Range.h:34
vtkm::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:161
vtkm::Range::Union
vtkm::Range Union(const vtkm::Range &otherRange) const
Return the union of this and another range.
Definition: Range.h:154
VTKM_NEVER_EXPORT
#define VTKM_NEVER_EXPORT
Definition: ExportMacros.h:90
vtkm::Range::Include
void Include(const vtkm::Range &range)
Expand range to include other range.
Definition: Range.h:140
vtkm::Range::Max
vtkm::Float64 Max
Tha maximum value of the range (inclusive).
Definition: Range.h:36
vtkm::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:61
vtkm::VecTraits< vtkm::Range >::GetComponent
static ComponentType & GetComponent(vtkm::Range &range, vtkm::IdComponent component)
Definition: Range.h:215
vtkm::VecTraits< vtkm::Range >::GetComponent
static const ComponentType & GetComponent(const vtkm::Range &range, vtkm::IdComponent component)
Definition: Range.h:209
VecTraits.h
vtkm::Range::IsNonEmpty
bool IsNonEmpty() const
Determine if the range is valid (i.e.
Definition: Range.h:70
vtkm::VecTraits< vtkm::Range >::SetComponent
static void SetComponent(vtkm::Range &range, vtkm::IdComponent component, ComponentType value)
Definition: Range.h:222
vtkm::Range::Intersection
vtkm::Range Intersection(const vtkm::Range &otherRange) const
Return the intersection of this and another range.
Definition: Range.h:164
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31