VecAxisAlignedPointCoordinates.h 5.87 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  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.
//
9
//  Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 11 12
//  Copyright 2015 UT-Battelle, LLC.
//  Copyright 2015 Los Alamos National Security.
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19
//  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.
//============================================================================
20 21 22

#ifndef vtk_m_VecAxisAlignedPointCoordinates_h
#define vtk_m_VecAxisAlignedPointCoordinates_h
23 24 25

#include <vtkm/Math.h>
#include <vtkm/TypeTraits.h>
26
#include <vtkm/Types.h>
27
#include <vtkm/VecTraits.h>
28
#include <vtkm/internal/ExportMacros.h>
29

30 31
namespace vtkm
{
32

33 34
namespace detail
{
35

36
/// Specifies the size of VecAxisAlignedPointCoordinates for the given
37 38
/// dimension.
///
39
template <vtkm::IdComponent NumDimensions>
40
struct VecAxisAlignedPointCoordinatesNumComponents;
41

42
template <>
43
struct VecAxisAlignedPointCoordinatesNumComponents<1>
44
{
45
  static constexpr vtkm::IdComponent NUM_COMPONENTS = 2;
46 47
};

48
template <>
49
struct VecAxisAlignedPointCoordinatesNumComponents<2>
50
{
51
  static constexpr vtkm::IdComponent NUM_COMPONENTS = 4;
52 53
};

54
template <>
55
struct VecAxisAlignedPointCoordinatesNumComponents<3>
56
{
57
  static constexpr vtkm::IdComponent NUM_COMPONENTS = 8;
58 59
};

60 61 62 63 64 65 66 67 68 69 70 71
struct VecAxisAlignedPointCoordinatesOffsetTable
{
  VTKM_EXEC_CONT vtkm::FloatDefault Get(vtkm::Int32 i, vtkm::Int32 j) const
  {
    VTKM_STATIC_CONSTEXPR_ARRAY vtkm::FloatDefault offsetTable[8][3] = {
      { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f },
      { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f }
    };
    return offsetTable[i][j];
  }
};

72 73
} // namespace detail

74 75 76
/// \brief An implicit vector for point coordinates in axis aligned cells. For
/// internal use only.
///
77
/// The \c VecAxisAlignedPointCoordinates class is a Vec-like class that holds
78 79
/// the point coordinates for a axis aligned cell. The class is templated on the
/// dimensions of the cell, which can be 1 (for a line).
80
///
81 82 83 84 85
/// This is an internal class used to represent coordinates for uniform datasets
/// in an execution environment when executing a WorkletMapPointToCell. Users
/// should not directly construct this class under any circumstances. Use the
/// related ArrayPortalUniformPointCoordinates and
/// ArrayHandleUniformPointCoordinates classes instead.
86
///
87
template <vtkm::IdComponent NumDimensions>
88
class VecAxisAlignedPointCoordinates
89 90
{
public:
91
  using ComponentType = vtkm::Vec<vtkm::FloatDefault, 3>;
92

93
  static constexpr vtkm::IdComponent NUM_COMPONENTS =
94
    detail::VecAxisAlignedPointCoordinatesNumComponents<NumDimensions>::NUM_COMPONENTS;
95

96
  VTKM_EXEC_CONT
97
  VecAxisAlignedPointCoordinates(ComponentType origin = ComponentType(0, 0, 0),
98 99 100 101 102
                                 ComponentType spacing = ComponentType(1, 1, 1))
    : Origin(origin)
    , Spacing(spacing)
  {
  }
103

104
  VTKM_EXEC_CONT
105 106
  vtkm::IdComponent GetNumberOfComponents() const { return NUM_COMPONENTS; }

107 108
  template <vtkm::IdComponent DestSize>
  VTKM_EXEC_CONT void CopyInto(vtkm::Vec<ComponentType, DestSize>& dest) const
109
  {
110
    vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->GetNumberOfComponents());
111 112 113 114 115 116
    for (vtkm::IdComponent index = 0; index < numComponents; index++)
    {
      dest[index] = (*this)[index];
    }
  }

117
  VTKM_EXEC_CONT
118 119
  ComponentType operator[](vtkm::IdComponent index) const
  {
120 121 122 123
    detail::VecAxisAlignedPointCoordinatesOffsetTable table;
    return ComponentType(this->Origin[0] + table.Get(index, 0) * this->Spacing[0],
                         this->Origin[1] + table.Get(index, 1) * this->Spacing[1],
                         this->Origin[2] + table.Get(index, 2) * this->Spacing[2]);
124 125
  }

126
  VTKM_EXEC_CONT
127
  const ComponentType& GetOrigin() const { return this->Origin; }
128

129
  VTKM_EXEC_CONT
130
  const ComponentType& GetSpacing() const { return this->Spacing; }
131 132 133 134 135 136 137 138 139

private:
  // Position of lower left point.
  ComponentType Origin;

  // Spacing in the x, y, and z directions.
  ComponentType Spacing;
};

140
template <vtkm::IdComponent NumDimensions>
141
struct TypeTraits<vtkm::VecAxisAlignedPointCoordinates<NumDimensions>>
142
{
143 144
  using NumericTag = vtkm::TypeTraitsRealTag;
  using DimensionalityTag = TypeTraitsVectorTag;
145

146
  VTKM_EXEC_CONT
147
  static vtkm::VecAxisAlignedPointCoordinates<NumDimensions> ZeroInitialization()
148
  {
149
    return vtkm::VecAxisAlignedPointCoordinates<NumDimensions>(
150
      vtkm::Vec<vtkm::FloatDefault, 3>(0, 0, 0), vtkm::Vec<vtkm::FloatDefault, 3>(0, 0, 0));
151 152 153
  }
};

154
template <vtkm::IdComponent NumDimensions>
155
struct VecTraits<vtkm::VecAxisAlignedPointCoordinates<NumDimensions>>
156
{
157
  using VecType = vtkm::VecAxisAlignedPointCoordinates<NumDimensions>;
158

159 160 161
  using ComponentType = vtkm::Vec<vtkm::FloatDefault, 3>;
  using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;
  using IsSizeStatic = vtkm::VecTraitsTagSizeStatic;
162

163
  static constexpr vtkm::IdComponent NUM_COMPONENTS = VecType::NUM_COMPONENTS;
164

165
  VTKM_EXEC_CONT
166
  static vtkm::IdComponent GetNumberOfComponents(const VecType&) { return NUM_COMPONENTS; }
167

168
  VTKM_EXEC_CONT
169
  static ComponentType GetComponent(const VecType& vector, vtkm::IdComponent componentIndex)
170 171 172 173
  {
    return vector[componentIndex];
  }

174 175
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
176 177 178 179 180 181 182
  {
    src.CopyInto(dest);
  }
};

} // namespace vtkm

183
#endif //vtk_m_VecAxisAlignedPointCoordinates_h