Range.h 5.36 KB
Newer Older
Kenneth Moreland's avatar
Kenneth Moreland committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//
//  Copyright 2016 Sandia Corporation.
//  Copyright 2016 UT-Battelle, LLC.
//  Copyright 2016 Los Alamos National Security.
//
//  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================

#ifndef vtk_m_Range_h
#define vtk_m_Range_h

#include <vtkm/Assert.h>
#include <vtkm/Math.h>
#include <vtkm/Types.h>

namespace vtkm {

/// \brief Represent a continuous scalar range of values.
///
Kenneth Moreland's avatar
Kenneth Moreland committed
32 33 34
/// \c vtkm::Range is a helper class for representing a range of floating point
/// values from a minimum value to a maximum value. This is specified simply
/// enough with a \c Min and \c Max value.
Kenneth Moreland's avatar
Kenneth Moreland committed
35
///
Kenneth Moreland's avatar
Kenneth Moreland committed
36 37
/// \c Range also contains several helper functions for computing and
/// maintaining the range.
Kenneth Moreland's avatar
Kenneth Moreland committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
///
struct Range
{
  vtkm::Float64 Min;
  vtkm::Float64 Max;

  VTKM_EXEC_CONT_EXPORT
  Range() : Min(vtkm::Infinity64()), Max(vtkm::NegativeInfinity64()) {  }

  template<typename T1, typename T2>
  VTKM_EXEC_CONT_EXPORT
  Range(const T1 &min, const T2 &max)
    : Min(static_cast<vtkm::Float64>(min)), Max(static_cast<vtkm::Float64>(max))
  {  }

  VTKM_EXEC_CONT_EXPORT
  const vtkm::Range &operator=(const vtkm::Range &src)
  {
    this->Min = src.Min;
    this->Max = src.Max;
    return *this;
  }

  /// \b Determine if the range is valid (i.e. has at least one valid point).
  ///
  /// \c IsNonEmpty return true if the range contains some valid values between
  /// \c Min and \c Max. If \c Max is less than \c Min, then no values satisfy
  /// the range and \c IsNonEmpty returns false. Otherwise, return true.
  ///
  /// \c IsNonEmpty assumes \c Min and \c Max are inclusive. That is, if they
  /// are equal then true is returned.
  ///
  VTKM_EXEC_CONT_EXPORT
  bool IsNonEmpty() const
  {
    return (this->Min <= this->Max);
  }

  /// \b Determines if a value is within the range.
  ///
  /// \c Contains returns true if the give value is within the range, false
  /// otherwise. \c Contains treats the min and max as inclusive. That is, if
  /// the value is exactly the min or max, true is returned.
  ///
  template<typename T>
  VTKM_EXEC_CONT_EXPORT
  bool Contains(const T &value) const
  {
    return ((this->Min <= static_cast<vtkm::Float64>(value)) &&
            (this->Max >= static_cast<vtkm::Float64>(value)));
  }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  /// \b Returns the length of the range.
  ///
  /// \c Length computes the distance between the min and max. If the range
  /// is empty, 0 is returned.
  ///
  VTKM_EXEC_CONT_EXPORT
  vtkm::Float64 Length() const
  {
    if (this->IsNonEmpty())
    {
      return (this->Max - this->Min);
    }
    else
    {
      return 0.0;
    }
  }

  /// \b Returns the center of the range.
  ///
  /// \c Center computes the middle value of the range. If the range is empty,
  /// NaN is returned.
  ///
  VTKM_EXEC_CONT_EXPORT
  vtkm::Float64 Center() const
  {
    if (this->IsNonEmpty())
    {
      return 0.5*(this->Max + this->Min);
    }
    else
    {
      return vtkm::Nan64();
    }
  }

Kenneth Moreland's avatar
Kenneth Moreland committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  /// \b Expand range to include a value.
  ///
  /// This version of \c Include expands the range just enough to include the
  /// given value. If the range already includes this value, then nothing is
  /// done.
  ///
  template<typename T>
  VTKM_EXEC_CONT_EXPORT
  void Include(const T &value)
  {
    this->Min = vtkm::Min(this->Min, static_cast<vtkm::Float64>(value));
    this->Max = vtkm::Max(this->Max, static_cast<vtkm::Float64>(value));
  }

  /// \b Expand range to include other range.
  ///
  /// This version of \c Include expands this range just enough to include that
  /// of another range. Esentially it is the union of the two ranges.
  ///
  VTKM_EXEC_CONT_EXPORT
  void Include(const vtkm::Range &range)
  {
    this->Include(range.Min);
    this->Include(range.Max);
  }

  /// \b Return the union of this and another range.
  ///
  /// This is a nondestructive form of \c Include.
  ///
  VTKM_EXEC_CONT_EXPORT
  vtkm::Range Union(const vtkm::Range &otherRange) const
  {
    vtkm::Range unionRange(*this);
    unionRange.Include(otherRange);
    return unionRange;
  }

  /// \b Operator for union
  ///
  VTKM_EXEC_CONT_EXPORT
  vtkm::Range operator+(const vtkm::Range &otherRange) const
  {
    return this->Union(otherRange);
  }

  VTKM_EXEC_CONT_EXPORT
  bool operator==(const vtkm::Range &otherRange) const
  {
    return ((this->Min == otherRange.Min) && (this->Max == otherRange.Max));
  }

  VTKM_EXEC_CONT_EXPORT
  bool operator!=(const vtkm::Range &otherRange) const
  {
Kenneth Moreland's avatar
Kenneth Moreland committed
181
    return ((this->Min != otherRange.Min) || (this->Max != otherRange.Max));
Kenneth Moreland's avatar
Kenneth Moreland committed
182 183 184 185 186
  }
};

} // namespace vtkm

187 188 189 190 191 192 193 194
/// Helper function for printing ranges during testing
///
VTKM_CONT_EXPORT
std::ostream &operator<<(std::ostream &stream, const vtkm::Range &range)
{
  return stream << "[" << range.Min << ".." << range.Max << "]";
}

Kenneth Moreland's avatar
Kenneth Moreland committed
195
#endif //vtk_m_Range_h