Viskores  1.0
Range.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 
19 #ifndef viskores_Range_h
20 #define viskores_Range_h
21 
22 #include <viskores/Assert.h>
23 #include <viskores/Math.h>
24 #include <viskores/Types.h>
25 #include <viskores/VecTraits.h>
26 
27 namespace viskores
28 {
29 
39 struct Range
40 {
45 
50  : Min(viskores::Infinity64())
51  , Max(viskores::NegativeInfinity64())
52  {
53  }
54 
55  Range(const Range&) = default;
56  Range(Range&&) = default;
57 
58  template <typename T1, typename T2>
59  VISKORES_EXEC_CONT Range(const T1& min, const T2& max)
60  : Min(static_cast<viskores::Float64>(min))
61  , Max(static_cast<viskores::Float64>(max))
62  {
63  }
64 
65  viskores::Range& operator=(const viskores::Range& src) = default;
66  viskores::Range& operator=(viskores::Range&& src) = default;
67 
78  bool IsNonEmpty() const { return (this->Min <= this->Max); }
79 
86  template <typename T>
87  VISKORES_EXEC_CONT bool Contains(const T& value) const
88  {
89  return ((this->Min <= static_cast<viskores::Float64>(value)) &&
90  (this->Max >= static_cast<viskores::Float64>(value)));
91  }
92 
100  {
101  if (this->IsNonEmpty())
102  {
103  return (this->Max - this->Min);
104  }
105  else
106  {
107  return 0.0;
108  }
109  }
110 
118  {
119  if (this->IsNonEmpty())
120  {
121  return 0.5 * (this->Max + this->Min);
122  }
123  else
124  {
125  return viskores::Nan64();
126  }
127  }
128 
135  template <typename T>
136  VISKORES_EXEC_CONT void Include(const T& value)
137  {
138  this->Min = viskores::Min(this->Min, static_cast<viskores::Float64>(value));
139  this->Max = viskores::Max(this->Max, static_cast<viskores::Float64>(value));
140  }
141 
148  void Include(const viskores::Range& range)
149  {
150  if (range.IsNonEmpty())
151  {
152  this->Min = viskores::Min(this->Min, range.Min);
153  this->Max = viskores::Max(this->Max, range.Max);
154  }
155  }
156 
162  viskores::Range Union(const viskores::Range& otherRange) const
163  {
164  viskores::Range unionRange(*this);
165  unionRange.Include(otherRange);
166  return unionRange;
167  }
168 
173  {
174  return viskores::Range(viskores::Max(this->Min, otherRange.Min),
175  viskores::Min(this->Max, otherRange.Max));
176  }
177 
181  viskores::Range operator+(const viskores::Range& otherRange) const
182  {
183  return this->Union(otherRange);
184  }
185 
187  bool operator==(const viskores::Range& otherRange) const
188  {
189  return ((this->Min == otherRange.Min) && (this->Max == otherRange.Max));
190  }
191 
193  bool operator!=(const viskores::Range& otherRange) const
194  {
195  return ((this->Min != otherRange.Min) || (this->Max != otherRange.Max));
196  }
197 };
198 
201 inline VISKORES_CONT std::ostream& operator<<(std::ostream& stream, const viskores::Range& range)
202 {
203  return stream << "[" << range.Min << ".." << range.Max << "]";
204 } // Declared inside of viskores namespace so that the operator work with ADL lookup
205 
206 template <>
207 struct VISKORES_NEVER_EXPORT VecTraits<viskores::Range>
208 {
211 
212  static constexpr viskores::IdComponent NUM_COMPONENTS = 2;
214  {
215  return NUM_COMPONENTS;
216  }
219 
221  static const ComponentType& GetComponent(const viskores::Range& range,
222  viskores::IdComponent component)
223  {
224  VISKORES_ASSERT((component == 0) || (component == 1));
225  return (component == 0) ? range.Min : range.Max;
226  }
229  {
230  VISKORES_ASSERT((component == 0) || (component == 1));
231  return (component == 0) ? range.Min : range.Max;
232  }
233 
235  static void SetComponent(viskores::Range& range,
236  viskores::IdComponent component,
237  ComponentType value)
238  {
239  VISKORES_ASSERT((component == 0) || (component == 1));
240  if (component == 0)
241  {
242  range.Min = value;
243  }
244  else
245  {
246  range.Max = value;
247  }
248  }
249 
250  template <typename NewComponentType>
252  template <typename NewComponentType>
254 
255  template <viskores::IdComponent destSize>
258  {
259  const viskores::IdComponent maxComponent =
260  (destSize < NUM_COMPONENTS) ? destSize : NUM_COMPONENTS;
261  for (viskores::IdComponent component = 0; component < maxComponent; ++component)
262  {
263  dest[component] = GetComponent(src, component);
264  }
265  }
266 };
267 
268 } // namespace viskores
269 
270 
271 #endif //viskores_Range_h
viskores::Range::Intersection
viskores::Range Intersection(const viskores::Range &otherRange) const
Return the intersection of this and another range.
Definition: Range.h:172
viskores::Range::Length
viskores::Float64 Length() const
Returns the length of the range.
Definition: Range.h:99
viskores::Range::Center
viskores::Float64 Center() const
Returns the center of the range.
Definition: Range.h:117
Types.h
viskores::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:44
viskores::Range::Min
viskores::Float64 Min
The minumum value of the range (inclusive).
Definition: Range.h:42
viskores::Range::Union
viskores::Range Union(const viskores::Range &otherRange) const
Return the union of this and another range.
Definition: Range.h:162
viskores::VecTraits< viskores::Range >::CopyInto
static void CopyInto(const viskores::Range &src, viskores::Vec< ComponentType, destSize > &dest)
Definition: Range.h:256
viskores::Range::Range
Range()
Construct a range with a given minimum and maximum.
Definition: Range.h:49
viskores::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:31
Assert.h
viskores::Range::IsNonEmpty
bool IsNonEmpty() const
Determine if the range is valid (i.e.
Definition: Range.h:78
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores::Range::Range
Range(const T1 &min, const T2 &max)
Definition: Range.h:59
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Range::Max
viskores::Float64 Max
Tha maximum value of the range (inclusive).
Definition: Range.h:44
viskores::Range::operator!=
bool operator!=(const viskores::Range &otherRange) const
Definition: Range.h:193
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores::operator<<
std::ostream & operator<<(std::ostream &stream, const viskores::Bounds &bounds)
Helper function for printing bounds during testing.
Definition: Bounds.h:268
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
Math.h
viskores::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
viskores::VecTraits< viskores::Range >::SetComponent
static void SetComponent(viskores::Range &range, viskores::IdComponent component, ComponentType value)
Definition: Range.h:235
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
viskores::Range::Include
void Include(const viskores::Range &range)
Expand range to include other range.
Definition: Range.h:148
viskores::Range::operator=
viskores::Range & operator=(const viskores::Range &src)=default
viskores::Range
Represent a continuous scalar range of values.
Definition: Range.h:39
viskores::VecTraits< viskores::Range >::BaseComponentType
viskores::Float64 BaseComponentType
Definition: Range.h:210
viskores::Range::operator+
viskores::Range operator+(const viskores::Range &otherRange) const
Operator for union
Definition: Range.h:181
viskores::VecTraits< viskores::Range >::ComponentType
viskores::Float64 ComponentType
Definition: Range.h:209
viskores::Range::Include
void Include(const T &value)
Expand range to include a value.
Definition: Range.h:136
viskores::VecTraits< viskores::Range >::GetNumberOfComponents
static constexpr viskores::IdComponent GetNumberOfComponents(const viskores::Range &)
Definition: Range.h:213
viskores::Range::Contains
bool Contains(const T &value) const
Determines if a value is within the range.
Definition: Range.h:87
viskores::VecTraits< viskores::Range >::GetComponent
static const ComponentType & GetComponent(const viskores::Range &range, viskores::IdComponent component)
Definition: Range.h:221
viskores::Float64
double Float64
Base type to use for 64-bit floating-point numbers.
Definition: Types.h:169
viskores::Vec
A short fixed-length array.
Definition: Types.h:365
viskores::VecTraits< viskores::Range >::GetComponent
static ComponentType & GetComponent(viskores::Range &range, viskores::IdComponent component)
Definition: Range.h:228
VecTraits.h
viskores::Range::operator==
bool operator==(const viskores::Range &otherRange) const
Definition: Range.h:187