VecTraits.h 13 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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10
//  Copyright 2014 UT-Battelle, LLC.
11
//  Copyright 2014 Los Alamos National Security.
12
//
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
#ifndef vtk_m_VecTraits_h
#define vtk_m_VecTraits_h
22 23 24

#include <vtkm/Types.h>

25 26
namespace vtkm
{
27 28 29 30

/// A tag for vectors that are "true" vectors (i.e. have more than one
/// component).
///
31 32 33
struct VecTraitsTagMultipleComponents
{
};
34

35 36
/// A tag for vectors that are really just scalars (i.e. have only one
/// component)
37
///
38 39 40
struct VecTraitsTagSingleComponent
{
};
41

42 43
/// A tag for vectors where the number of components are known at compile time.
///
44 45 46
struct VecTraitsTagSizeStatic
{
};
47 48 49 50

/// A tag for vectors where the number of components are not determined until
/// run time.
///
51 52 53
struct VecTraitsTagSizeVariable
{
};
54

55 56
namespace internal
{
57

58
template <vtkm::IdComponent numComponents>
59
struct VecTraitsMultipleComponentChooser
60
{
61
  using Type = vtkm::VecTraitsTagMultipleComponents;
62 63
};

64
template <>
65
struct VecTraitsMultipleComponentChooser<1>
66
{
67
  using Type = vtkm::VecTraitsTagSingleComponent;
68 69 70 71
};

} // namespace detail

72
/// The VecTraits class gives several static members that define how
73 74
/// to use a given type as a vector.
///
75
template <class VecType>
76
struct VecTraits
77 78 79 80
#ifdef VTKM_DOXYGEN_ONLY
{
  /// Type of the components in the vector.
  ///
81
  using ComponentType = typename VecType::ComponentType;
82

83 84 85
  /// \brief Number of components in the vector.
  ///
  /// This is only defined for vectors of a static size.
86
  ///
87
  static constexpr vtkm::IdComponent NUM_COMPONENTS = VecType::NUM_COMPONENTS;
88

89 90
  /// Number of components in the given vector.
  ///
91
  static vtkm::IdComponent GetNumberOfComponents(const VecType& vec);
92

93 94 95 96
  /// \brief A tag specifying whether this vector has multiple components (i.e. is a "real" vector).
  ///
  /// This tag can be useful for creating specialized functions when a vector
  /// is really just a scalar.
97
  ///
98 99
  using HasMultipleComponents =
    typename internal::VecTraitsMultipleComponentChooser<NUM_COMPONENTS>::Type;
100

101 102 103 104 105
  /// \brief A tag specifying whether the size of this vector is known at compile time.
  ///
  /// If set to \c VecTraitsTagSizeStatic, then \c NUM_COMPONENTS is set. If
  /// set to \c VecTraitsTagSizeVariable, then the number of components is not
  /// known at compile time and must be queried with \c GetNumberOfComponents.
106
  ///
107
  using IsSizeStatic = vtkm::VecTraitsTagSizeStatic;
108

109 110
  /// Returns the value in a given component of the vector.
  ///
111
  VTKM_EXEC_CONT static const ComponentType& GetComponent(
112 113
    const typename std::remove_const<VecType>::type& vector,
    vtkm::IdComponent component);
114
  VTKM_EXEC_CONT static ComponentType& GetComponent(
115 116
    typename std::remove_const<VecType>::type& vector,
    vtkm::IdComponent component);
117 118 119

  /// Changes the value in a given component of the vector.
  ///
120 121
  VTKM_EXEC_CONT static void SetComponent(VecType& vector,
                                          vtkm::IdComponent component,
122
                                          ComponentType value);
123

124
  /// Copies the components in the given vector into a given Vec object.
125
  ///
126 127
  template <vktm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest);
128
};
129 130
#else  // VTKM_DOXYGEN_ONLY
  ;
131 132 133
#endif // VTKM_DOXYGEN_ONLY

// This partial specialization allows you to define a non-const version of
134
// VecTraits and have it still work for const version.
135
//
136
template <typename T>
137
struct VecTraits<const T> : VecTraits<T>
138 139
{
};
140

141 142
template <typename T, vtkm::IdComponent Size>
struct VecTraits<vtkm::Vec<T, Size>>
143
{
144
  using VecType = vtkm::Vec<T, Size>;
145 146 147

  /// Type of the components in the vector.
  ///
148
  using ComponentType = typename VecType::ComponentType;
149 150 151

  /// Number of components in the vector.
  ///
152
  static constexpr vtkm::IdComponent NUM_COMPONENTS = VecType::NUM_COMPONENTS;
153

154 155
  /// Number of components in the given vector.
  ///
156
  VTKM_EXEC_CONT
157
  static vtkm::IdComponent GetNumberOfComponents(const VecType&) { return NUM_COMPONENTS; }
158

159 160 161 162
  /// A tag specifying whether this vector has multiple components (i.e. is a
  /// "real" vector). This tag can be useful for creating specialized functions
  /// when a vector is really just a scalar.
  ///
163 164
  using HasMultipleComponents =
    typename internal::VecTraitsMultipleComponentChooser<NUM_COMPONENTS>::Type;
165

166 167 168 169 170 171
  /// A tag specifying whether the size of this vector is known at compile
  /// time. If set to \c VecTraitsTagSizeStatic, then \c NUM_COMPONENTS is set.
  /// If set to \c VecTraitsTagSizeVariable, then the number of components is
  /// not known at compile time and must be queried with \c
  /// GetNumberOfComponents.
  ///
172
  using IsSizeStatic = vtkm::VecTraitsTagSizeStatic;
173

174 175
  /// Returns the value in a given component of the vector.
  ///
176
  VTKM_EXEC_CONT
177
  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
178 179 180
  {
    return vector[component];
  }
181
  VTKM_EXEC_CONT
182 183
  static ComponentType& GetComponent(VecType& vector, vtkm::IdComponent component)
  {
184 185 186 187 188
    return vector[component];
  }

  /// Changes the value in a given component of the vector.
  ///
189 190
  VTKM_EXEC_CONT static void SetComponent(VecType& vector,
                                          vtkm::IdComponent component,
191 192
                                          ComponentType value)
  {
193 194 195 196 197
    vector[component] = value;
  }

  /// Converts whatever type this vector is into the standard VTKm Tuple.
  ///
198 199
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
200
  {
201
    src.CopyInto(dest);
202 203 204
  }
};

205 206
template <typename T>
struct VecTraits<vtkm::VecC<T>>
207 208 209 210 211 212 213 214 215 216
{
  using VecType = vtkm::VecC<T>;

  /// Type of the components in the vector.
  ///
  using ComponentType = typename VecType::ComponentType;

  /// Number of components in the given vector.
  ///
  VTKM_EXEC_CONT
217
  static vtkm::IdComponent GetNumberOfComponents(const VecType& vector)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  {
    return vector.GetNumberOfComponents();
  }

  /// A tag specifying whether this vector has multiple components (i.e. is a
  /// "real" vector). This tag can be useful for creating specialized functions
  /// when a vector is really just a scalar.
  ///
  /// The size of a \c VecC is not known until runtime and can always
  /// potentially have multiple components, this is always set to \c
  /// HasMultipleComponents.
  ///
  using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;

  /// A tag specifying whether the size of this vector is known at compile
  /// time. If set to \c VecTraitsTagSizeStatic, then \c NUM_COMPONENTS is set.
  /// If set to \c VecTraitsTagSizeVariable, then the number of components is
  /// not known at compile time and must be queried with \c
  /// GetNumberOfComponents.
  ///
  using IsSizeStatic = vtkm::VecTraitsTagSizeVariable;

  /// Returns the value in a given component of the vector.
  ///
  VTKM_EXEC_CONT
243
  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
244 245 246 247
  {
    return vector[component];
  }
  VTKM_EXEC_CONT
248 249
  static ComponentType& GetComponent(VecType& vector, vtkm::IdComponent component)
  {
250 251 252 253 254 255
    return vector[component];
  }

  /// Changes the value in a given component of the vector.
  ///
  VTKM_EXEC_CONT
256 257
  static void SetComponent(VecType& vector, vtkm::IdComponent component, ComponentType value)
  {
258 259 260 261 262
    vector[component] = value;
  }

  /// Converts whatever type this vector is into the standard VTKm Tuple.
  ///
263 264
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
265 266 267 268 269
  {
    src.CopyInto(dest);
  }
};

270 271
template <typename T>
struct VecTraits<vtkm::VecCConst<T>>
272 273 274 275 276 277 278 279 280 281
{
  using VecType = vtkm::VecCConst<T>;

  /// Type of the components in the vector.
  ///
  using ComponentType = typename VecType::ComponentType;

  /// Number of components in the given vector.
  ///
  VTKM_EXEC_CONT
282
  static vtkm::IdComponent GetNumberOfComponents(const VecType& vector)
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  {
    return vector.GetNumberOfComponents();
  }

  /// A tag specifying whether this vector has multiple components (i.e. is a
  /// "real" vector). This tag can be useful for creating specialized functions
  /// when a vector is really just a scalar.
  ///
  /// The size of a \c VecCConst is not known until runtime and can always
  /// potentially have multiple components, this is always set to \c
  /// HasMultipleComponents.
  ///
  using HasMultipleComponents = vtkm::VecTraitsTagMultipleComponents;

  /// A tag specifying whether the size of this vector is known at compile
  /// time. If set to \c VecTraitsTagSizeStatic, then \c NUM_COMPONENTS is set.
  /// If set to \c VecTraitsTagSizeVariable, then the number of components is
  /// not known at compile time and must be queried with \c
  /// GetNumberOfComponents.
  ///
  using IsSizeStatic = vtkm::VecTraitsTagSizeVariable;

  /// Returns the value in a given component of the vector.
  ///
  VTKM_EXEC_CONT
308
  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
309 310 311 312 313 314 315
  {
    return vector[component];
  }

  /// Changes the value in a given component of the vector.
  ///
  VTKM_EXEC_CONT
316 317
  static void SetComponent(VecType& vector, vtkm::IdComponent component, ComponentType value)
  {
318 319 320 321 322
    vector[component] = value;
  }

  /// Converts whatever type this vector is into the standard VTKm Tuple.
  ///
323 324
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest)
325 326 327 328 329
  {
    src.CopyInto(dest);
  }
};

330 331
namespace internal
{
332
/// Used for overriding VecTraits for basic scalar types.
333
///
334 335 336
template <typename ScalarType>
struct VecTraitsBasic
{
337
  using ComponentType = ScalarType;
338
  static constexpr vtkm::IdComponent NUM_COMPONENTS = 1;
339 340
  using HasMultipleComponents = vtkm::VecTraitsTagSingleComponent;
  using IsSizeStatic = vtkm::VecTraitsTagSizeStatic;
341

342
  VTKM_EXEC_CONT
343
  static vtkm::IdComponent GetNumberOfComponents(const ScalarType&) { return 1; }
344

345
  VTKM_EXEC_CONT
346 347
  static const ComponentType& GetComponent(const ScalarType& vector, vtkm::IdComponent)
  {
348 349
    return vector;
  }
350
  VTKM_EXEC_CONT
351
  static ComponentType& GetComponent(ScalarType& vector, vtkm::IdComponent) { return vector; }
352

353 354
  VTKM_EXEC_CONT static void SetComponent(ScalarType& vector,
                                          vtkm::IdComponent,
355 356
                                          ComponentType value)
  {
357 358 359
    vector = value;
  }

360 361
  template <vtkm::IdComponent destSize>
  VTKM_EXEC_CONT static void CopyInto(const ScalarType& src, vtkm::Vec<ScalarType, destSize>& dest)
362
  {
363
    dest[0] = src;
364 365
  }
};
366 367
} // namespace internal

368 369
/// \brief VecTraits for Pair types
///
luz.paz's avatar
luz.paz committed
370
/// Although a pair would seem better as a size-2 vector, we treat it as a
371 372 373 374
/// scalar. This is because a \c Vec is assumed to have the same type for
/// every component, and a pair in general has a different type for each
/// component. Thus we treat a pair as a "scalar" unit.
///
375 376
template <typename T, typename U>
struct VecTraits<vtkm::Pair<T, U>> : public vtkm::internal::VecTraitsBasic<vtkm::Pair<T, U>>
377 378 379
{
};

380
} // anonymous namespace
381

382 383 384 385 386 387 388
#define VTKM_BASIC_TYPE_VECTOR(type)                                                               \
  namespace vtkm                                                                                   \
  {                                                                                                \
  template <>                                                                                      \
  struct VecTraits<type> : public vtkm::internal::VecTraitsBasic<type>                             \
  {                                                                                                \
  };                                                                                               \
389
  }
390 391 392

/// Allows you to treat basic types as if they were vectors.

393 394 395
VTKM_BASIC_TYPE_VECTOR(float)
VTKM_BASIC_TYPE_VECTOR(double)

396
VTKM_BASIC_TYPE_VECTOR(bool)
397 398 399 400 401 402 403 404 405 406 407 408
VTKM_BASIC_TYPE_VECTOR(char)
VTKM_BASIC_TYPE_VECTOR(signed char)
VTKM_BASIC_TYPE_VECTOR(unsigned char)
VTKM_BASIC_TYPE_VECTOR(short)
VTKM_BASIC_TYPE_VECTOR(unsigned short)
VTKM_BASIC_TYPE_VECTOR(int)
VTKM_BASIC_TYPE_VECTOR(unsigned int)
VTKM_BASIC_TYPE_VECTOR(long)
VTKM_BASIC_TYPE_VECTOR(unsigned long)
VTKM_BASIC_TYPE_VECTOR(long long)
VTKM_BASIC_TYPE_VECTOR(unsigned long long)

409
//#undef VTKM_BASIC_TYPE_VECTOR
410

411
#endif //vtk_m_VecTraits_h