VTK-m  2.0
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 {
35 
38  : Min(vtkm::Infinity64())
39  , Max(vtkm::NegativeInfinity64())
40  {
41  }
42 
43  Range(const Range&) = default;
44  Range(Range&&) = default;
45 
46  template <typename T1, typename T2>
47  VTKM_EXEC_CONT Range(const T1& min, const T2& max)
48  : Min(static_cast<vtkm::Float64>(min))
49  , Max(static_cast<vtkm::Float64>(max))
50  {
51  }
52 
53  vtkm::Range& operator=(const vtkm::Range& src) = default;
54  vtkm::Range& operator=(vtkm::Range&& src) = default;
55 
66  bool IsNonEmpty() const { return (this->Min <= this->Max); }
67 
74  template <typename T>
75  VTKM_EXEC_CONT bool Contains(const T& value) const
76  {
77  return ((this->Min <= static_cast<vtkm::Float64>(value)) &&
78  (this->Max >= static_cast<vtkm::Float64>(value)));
79  }
80 
88  {
89  if (this->IsNonEmpty())
90  {
91  return (this->Max - this->Min);
92  }
93  else
94  {
95  return 0.0;
96  }
97  }
98 
106  {
107  if (this->IsNonEmpty())
108  {
109  return 0.5 * (this->Max + this->Min);
110  }
111  else
112  {
113  return vtkm::Nan64();
114  }
115  }
116 
123  template <typename T>
124  VTKM_EXEC_CONT void Include(const T& value)
125  {
126  this->Min = vtkm::Min(this->Min, static_cast<vtkm::Float64>(value));
127  this->Max = vtkm::Max(this->Max, static_cast<vtkm::Float64>(value));
128  }
129 
136  void Include(const vtkm::Range& range)
137  {
138  if (range.IsNonEmpty())
139  {
140  this->Min = vtkm::Min(this->Min, range.Min);
141  this->Max = vtkm::Max(this->Max, range.Max);
142  }
143  }
144 
150  vtkm::Range Union(const vtkm::Range& otherRange) const
151  {
152  vtkm::Range unionRange(*this);
153  unionRange.Include(otherRange);
154  return unionRange;
155  }
156 
160  vtkm::Range Intersection(const vtkm::Range& otherRange) const
161  {
162  return vtkm::Range(vtkm::Max(this->Min, otherRange.Min), vtkm::Min(this->Max, otherRange.Max));
163  }
164 
168  vtkm::Range operator+(const vtkm::Range& otherRange) const { return this->Union(otherRange); }
169 
171  bool operator==(const vtkm::Range& otherRange) const
172  {
173  return ((this->Min == otherRange.Min) && (this->Max == otherRange.Max));
174  }
175 
177  bool operator!=(const vtkm::Range& otherRange) const
178  {
179  return ((this->Min != otherRange.Min) || (this->Max != otherRange.Max));
180  }
181 };
182 
185 inline VTKM_CONT std::ostream& operator<<(std::ostream& stream, const vtkm::Range& range)
186 {
187  return stream << "[" << range.Min << ".." << range.Max << "]";
188 } // Declared inside of vtkm namespace so that the operator work with ADL lookup
189 
190 template <>
192 {
195 
196  static constexpr vtkm::IdComponent NUM_COMPONENTS = 2;
198  {
199  return NUM_COMPONENTS;
200  }
203 
205  static const ComponentType& GetComponent(const vtkm::Range& range, vtkm::IdComponent component)
206  {
207  VTKM_ASSERT((component == 0) || (component == 1));
208  return (component == 0) ? range.Min : range.Max;
209  }
212  {
213  VTKM_ASSERT((component == 0) || (component == 1));
214  return (component == 0) ? range.Min : range.Max;
215  }
216 
218  static void SetComponent(vtkm::Range& range, vtkm::IdComponent component, ComponentType value)
219  {
220  VTKM_ASSERT((component == 0) || (component == 1));
221  if (component == 0)
222  {
223  range.Min = value;
224  }
225  else
226  {
227  range.Max = value;
228  }
229  }
230 
231  template <typename NewComponentType>
233  template <typename NewComponentType>
235 
236  template <vtkm::IdComponent destSize>
237  VTKM_EXEC_CONT static void CopyInto(const vtkm::Range& src,
239  {
240  const vtkm::IdComponent maxComponent = (destSize < NUM_COMPONENTS) ? destSize : NUM_COMPONENTS;
241  for (vtkm::IdComponent component = 0; component < maxComponent; ++component)
242  {
243  dest[component] = GetComponent(src, component);
244  }
245  }
246 };
247 
248 } // namespace vtkm
249 
250 
251 #endif //vtk_m_Range_h
vtkm::VecTraits< vtkm::Range >::GetNumberOfComponents
static constexpr vtkm::IdComponent GetNumberOfComponents(const vtkm::Range &)
Definition: Range.h:197
vtkm::VecTraits< vtkm::Range >::SetComponent
static VTKM_EXEC_CONT void SetComponent(vtkm::Range &range, vtkm::IdComponent component, ComponentType value)
Definition: Range.h:218
vtkm::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:21
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
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::Range::Range
VTKM_EXEC_CONT Range(const T1 &min, const T2 &max)
Definition: Range.h:47
vtkm::Range::Include
VTKM_EXEC_CONT void Include(const T &value)
Expand range to include a value.
Definition: Range.h:124
Assert.h
vtkm::Range::Include
VTKM_EXEC_CONT void Include(const vtkm::Range &range)
Expand range to include other range.
Definition: Range.h:136
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:105
vtkm::Range::Length
VTKM_EXEC_CONT vtkm::Float64 Length() const
Returns the length of the range.
Definition: Range.h:87
vtkm::VecTraits< vtkm::Range >::BaseComponentType
vtkm::Float64 BaseComponentType
Definition: Range.h:194
vtkm::VecTraits< vtkm::Range >::GetComponent
static VTKM_EXEC_CONT ComponentType & GetComponent(vtkm::Range &range, vtkm::IdComponent component)
Definition: Range.h:211
Math.h
vtkm::Range::Intersection
VTKM_EXEC_CONT vtkm::Range Intersection(const vtkm::Range &otherRange) const
Return the intersection of this and another range.
Definition: Range.h:160
vtkm::VecTraits< vtkm::Range >::GetComponent
static const VTKM_EXEC_CONT ComponentType & GetComponent(const vtkm::Range &range, vtkm::IdComponent component)
Definition: Range.h:205
vtkm::operator<<
VTKM_CONT std::ostream & operator<<(std::ostream &stream, const vtkm::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:237
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:66
vtkm::VecTraits< vtkm::Range >::ComponentType
vtkm::Float64 ComponentType
Definition: Range.h:193
vtkm::Range::Range
VTKM_EXEC_CONT Range()
Definition: Range.h:37
vtkm::Range::operator==
VTKM_EXEC_CONT bool operator==(const vtkm::Range &otherRange) const
Definition: Range.h:171
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:150
vtkm::VecTraits< vtkm::Range >::CopyInto
static VTKM_EXEC_CONT void CopyInto(const vtkm::Range &src, vtkm::Vec< ComponentType, destSize > &dest)
Definition: Range.h:237
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:34
vtkm::Range::Contains
VTKM_EXEC_CONT bool Contains(const T &value) const
Determines if a value is within the range.
Definition: Range.h:75
vtkm::Range::Min
vtkm::Float64 Min
Definition: Range.h:33
vtkm::Float64
double Float64
Definition: Types.h:155
VTKM_NEVER_EXPORT
#define VTKM_NEVER_EXPORT
Definition: ExportMacros.h:93
vtkm::Range::Max
vtkm::Float64 Max
Definition: Range.h:34
vtkm::VecTraits
The VecTraits class gives several static members that define how to use a given type as a vector.
Definition: VecTraits.h:66
vtkm::Range::operator!=
VTKM_EXEC_CONT bool operator!=(const vtkm::Range &otherRange) const
Definition: Range.h:177
vtkm::Range::operator+
VTKM_EXEC_CONT vtkm::Range operator+(const vtkm::Range &otherRange) const
Operator for union
Definition: Range.h:168
VecTraits.h
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31