Field.h 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
//============================================================================
//  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 2014 Sandia Corporation.
//  Copyright 2014 UT-Battelle, LLC.
11
//  Copyright 2014 Los Alamos National Security.
12 13 14 15 16 17 18 19
//
//  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.
//============================================================================
20 21
#ifndef vtk_m_cont_Field_h
#define vtk_m_cont_Field_h
22

Sujin Philip's avatar
Sujin Philip committed
23
#include <vtkm/Math.h>
24 25
#include <vtkm/Range.h>
#include <vtkm/Types.h>
Sujin Philip's avatar
Sujin Philip committed
26 27
#include <vtkm/VecTraits.h>

28
#include <vtkm/cont/ArrayHandle.h>
Sujin Philip's avatar
Sujin Philip committed
29
#include <vtkm/cont/ArrayHandleTransform.h>
30
#include <vtkm/cont/ArrayHandleUniformPointCoordinates.h>
Sujin Philip's avatar
Sujin Philip committed
31
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
32
#include <vtkm/cont/DynamicArrayHandle.h>
33 34

#include <vtkm/cont/internal/ArrayPortalFromIterators.h>
35 36 37 38

namespace vtkm {
namespace cont {

Sujin Philip's avatar
Sujin Philip committed
39 40
namespace internal {

41
struct RangeMin
Sujin Philip's avatar
Sujin Philip committed
42
{
43 44
  template<typename T>
  T operator()(const T& a, const T& b)const { return vtkm::Min(a,b); }
Sujin Philip's avatar
Sujin Philip committed
45 46
};

47
struct RangeMax
Sujin Philip's avatar
Sujin Philip committed
48
{
49 50
  template<typename T>
  T operator()(const T& a, const T& b)const { return vtkm::Max(a,b); }
Sujin Philip's avatar
Sujin Philip committed
51 52 53
};


54
template<typename DeviceAdapterTag>
55
class ComputeRange
Sujin Philip's avatar
Sujin Philip committed
56
{
57
public:
58
  ComputeRange(ArrayHandle<vtkm::Range>& range) : Range(&range) {}
Sujin Philip's avatar
Sujin Philip committed
59

60 61 62 63 64 65
  template<typename ArrayHandleType>
  void operator()(const ArrayHandleType &input) const
  {
    typedef typename ArrayHandleType::ValueType ValueType;
    typedef vtkm::VecTraits<ValueType> VecType;
    const vtkm::IdComponent NumberOfComponents = VecType::NUM_COMPONENTS;
Sujin Philip's avatar
Sujin Philip committed
66

67
    typedef vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapterTag> Algorithm;
Sujin Philip's avatar
Sujin Philip committed
68

69 70 71 72
    //not the greatest way of doing this for performance reasons. But
    //this implementation should generate the smallest amount of code
    ValueType initialMin = input.GetPortalConstControl().Get(0);
    ValueType initialMax = initialMin;
Sujin Philip's avatar
Sujin Philip committed
73

74 75
    ValueType minResult = Algorithm::Reduce(input, initialMin, RangeMin());
    ValueType maxResult = Algorithm::Reduce(input, initialMax, RangeMax());
Sujin Philip's avatar
Sujin Philip committed
76

77
    this->Range->Allocate(NumberOfComponents);
78
    for (vtkm::IdComponent i = 0; i < NumberOfComponents; ++i)
79
    {
80 81 82
      this->Range->GetPortalControl().Set(
            i, vtkm::Range(VecType::GetComponent(minResult, i),
                           VecType::GetComponent(maxResult, i)));
83
    }
Sujin Philip's avatar
Sujin Philip committed
84 85
  }

86 87 88 89 90 91
  // Special implementation for regular point coordinates, which are easy
  // to determine.
  void operator()(const vtkm::cont::ArrayHandle<
                      vtkm::Vec<vtkm::FloatDefault,3>,
                      vtkm::cont::ArrayHandleUniformPointCoordinates::StorageTag>
                    &array)
Sujin Philip's avatar
Sujin Philip committed
92
  {
93 94 95 96 97 98 99 100 101
    vtkm::internal::ArrayPortalUniformPointCoordinates portal =
        array.GetPortalConstControl();

    // In this portal we know that the min value is the first entry and the
    // max value is the last entry.
    vtkm::Vec<vtkm::FloatDefault,3> minimum = portal.Get(0);
    vtkm::Vec<vtkm::FloatDefault,3> maximum =
        portal.Get(portal.GetNumberOfValues()-1);

102 103 104 105 106 107
    this->Range->Allocate(3);
    vtkm::cont::ArrayHandle<vtkm::Range>::PortalControl outPortal =
        this->Range->GetPortalControl();
    outPortal.Set(0, vtkm::Range(minimum[0], maximum[0]));
    outPortal.Set(1, vtkm::Range(minimum[1], maximum[1]));
    outPortal.Set(2, vtkm::Range(minimum[2], maximum[2]));
Sujin Philip's avatar
Sujin Philip committed
108
  }
109 110

private:
111
    vtkm::cont::ArrayHandle<vtkm::Range> *Range;
Sujin Philip's avatar
Sujin Philip committed
112 113 114 115 116
};

} // namespace internal


117
/// A \c Field encapsulates an array on some piece of the mesh, such as
Kenneth Moreland's avatar
Kenneth Moreland committed
118
/// the points, a cell set, a point logical dimension, or the whole mesh.
119 120 121 122
///
class Field
{
public:
Jeremy Meredith's avatar
Jeremy Meredith committed
123

124
  enum AssociationEnum
125
  {
126
    ASSOC_ANY,
127 128 129 130 131
    ASSOC_WHOLE_MESH,
    ASSOC_POINTS,
    ASSOC_CELL_SET,
    ASSOC_LOGICAL_DIM
  };
132

133
  /// constructors for points / whole mesh
Sujin Philip's avatar
Sujin Philip committed
134 135 136
  VTKM_CONT_EXPORT
  Field(std::string name,
        AssociationEnum association,
137
        const vtkm::cont::DynamicArrayHandle &data)
Sujin Philip's avatar
Sujin Philip committed
138 139 140 141 142
    : Name(name),
      Association(association),
      AssocCellSetName(),
      AssocLogicalDim(-1),
      Data(data),
143
      Range(),
Sujin Philip's avatar
Sujin Philip committed
144 145
      ModifiedFlag(true)
  {
146 147
    VTKM_ASSERT(this->Association == ASSOC_WHOLE_MESH ||
                this->Association == ASSOC_POINTS);
Sujin Philip's avatar
Sujin Philip committed
148 149
  }

150
  template<typename T, typename Storage>
151
  VTKM_CONT_EXPORT
152 153
  Field(std::string name,
        AssociationEnum association,
154
        const ArrayHandle<T, Storage> &data)
155 156 157 158 159
    : Name(name),
      Association(association),
      AssocCellSetName(),
      AssocLogicalDim(-1),
      Data(data),
160
      Range(),
161
      ModifiedFlag(true)
Jeremy Meredith's avatar
Jeremy Meredith committed
162
  {
163 164
    VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) ||
                (this->Association == ASSOC_POINTS));
Jeremy Meredith's avatar
Jeremy Meredith committed
165
  }
166

167
  template <typename T>
168
  VTKM_CONT_EXPORT
169 170 171 172 173 174 175
  Field(std::string name,
        AssociationEnum association,
        const std::vector<T> &data)
    : Name(name),
      Association(association),
      AssocCellSetName(),
      AssocLogicalDim(-1),
176
      Range(),
177
      ModifiedFlag(true)
178
  {
179 180
    VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) ||
                (this->Association == ASSOC_POINTS));
181
    this->CopyData(&data[0], static_cast<vtkm::Id>(data.size()));
182
  }
183

184
  template <typename T>
185
  VTKM_CONT_EXPORT
186 187 188 189 190 191 192 193
  Field(std::string name,
        AssociationEnum association,
        const T *data,
        vtkm::Id nvals)
    : Name(name),
      Association(association),
      AssocCellSetName(),
      AssocLogicalDim(-1),
194
      Range(),
195
      ModifiedFlag(true)
196
  {
197 198
    VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) ||
                (this->Association == ASSOC_POINTS));
199
    this->CopyData(data, nvals);
200
  }
201

202
  /// constructors for cell set associations
203
  VTKM_CONT_EXPORT
204 205 206
  Field(std::string name,
        AssociationEnum association,
        const std::string& cellSetName,
207
        const vtkm::cont::DynamicArrayHandle &data)
208 209 210 211 212
    : Name(name),
      Association(association),
      AssocCellSetName(cellSetName),
      AssocLogicalDim(-1),
      Data(data),
213
      Range(),
214
      ModifiedFlag(true)
215
  {
216
    VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
217 218
  }

Sujin Philip's avatar
Sujin Philip committed
219
  template <typename T, typename Storage>
220
  VTKM_CONT_EXPORT
221 222 223
  Field(std::string name,
        AssociationEnum association,
        const std::string& cellSetName,
224
        const vtkm::cont::ArrayHandle<T, Storage> &data)
225 226 227 228 229
    : Name(name),
      Association(association),
      AssocCellSetName(cellSetName),
      AssocLogicalDim(-1),
      Data(data),
230
      Range(),
231
      ModifiedFlag(true)
Jeremy Meredith's avatar
Jeremy Meredith committed
232
  {
233
    VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
Jeremy Meredith's avatar
Jeremy Meredith committed
234
  }
235

236
  template <typename T>
237
  VTKM_CONT_EXPORT
238 239 240 241 242 243 244 245
  Field(std::string name,
        AssociationEnum association,
        const std::string& cellSetName,
        const std::vector<T> &data)
    : Name(name),
      Association(association),
      AssocCellSetName(cellSetName),
      AssocLogicalDim(-1),
246
      Range(),
247
      ModifiedFlag(true)
248
  {
249
    VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
250
    this->CopyData(&data[0], static_cast<vtkm::Id>(data.size()));
251
  }
252

253
  template <typename T>
254
  VTKM_CONT_EXPORT
255 256 257 258 259 260 261 262 263
  Field(std::string name,
        AssociationEnum association,
        const std::string& cellSetName,
        const T *data,
        vtkm::Id nvals)
    : Name(name),
      Association(association),
      AssocCellSetName(cellSetName),
      AssocLogicalDim(-1),
264
      Range(),
265
      ModifiedFlag(true)
266
  {
267
    VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
268
    this->CopyData(data, nvals);
269
  }
270

271
  /// constructors for logical dimension associations
272
  VTKM_CONT_EXPORT
273 274 275
  Field(std::string name,
        AssociationEnum association,
        vtkm::IdComponent logicalDim,
276
        const vtkm::cont::DynamicArrayHandle &data)
277 278 279 280 281
    : Name(name),
      Association(association),
      AssocCellSetName(),
      AssocLogicalDim(logicalDim),
      Data(data),
282
      Range(),
283
      ModifiedFlag(true)
284
  {
285
    VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
286 287
  }

Sujin Philip's avatar
Sujin Philip committed
288
  template <typename T, typename Storage>
289
  VTKM_CONT_EXPORT
290 291 292
  Field(std::string name,
        AssociationEnum association,
        vtkm::IdComponent logicalDim,
293
        const vtkm::cont::ArrayHandle<T, Storage> &data)
294 295 296 297
    : Name(name),
      Association(association),
      AssocLogicalDim(logicalDim),
      Data(data),
298
      Range(),
299
      ModifiedFlag(true)
Jeremy Meredith's avatar
Jeremy Meredith committed
300
  {
301
    VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
Jeremy Meredith's avatar
Jeremy Meredith committed
302
  }
303

304
  template <typename T>
305
  VTKM_CONT_EXPORT
306 307 308 309 310 311 312
  Field(std::string name,
        AssociationEnum association,
        vtkm::IdComponent logicalDim,
        const std::vector<T> &data)
    : Name(name),
      Association(association),
      AssocLogicalDim(logicalDim),
313
      Range(),
314
      ModifiedFlag(true)
315
  {
316
    VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
317
    this->CopyData(&data[0], static_cast<vtkm::Id>(data.size()));
318
  }
319

320
  template <typename T>
321
  VTKM_CONT_EXPORT
322 323 324 325 326 327 328
  Field(std::string name,
        AssociationEnum association,
        vtkm::IdComponent logicalDim,
        const T *data, vtkm::Id nvals)
    : Name(name),
      Association(association),
      AssocLogicalDim(logicalDim),
329
      Range(),
330
      ModifiedFlag(true)
331
  {
332
    VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
333
    CopyData(data, nvals);
334
  }
335

336 337 338
  VTKM_CONT_EXPORT
  Field()
    : Name(),
339
      Association(ASSOC_ANY),
340 341 342
      AssocCellSetName(),
      AssocLogicalDim(),
      Data(),
343
      Range(),
344 345 346 347 348
      ModifiedFlag(true)
  {
    //Generate an empty field
  }

349
  VTKM_CONT_EXPORT
350
  const std::string &GetName() const
351
  {
352
    return this->Name;
353 354
  }

355
  VTKM_CONT_EXPORT
356
  AssociationEnum GetAssociation() const
Jeremy Meredith's avatar
Jeremy Meredith committed
357
  {
358
    return this->Association;
Jeremy Meredith's avatar
Jeremy Meredith committed
359 360
  }

361
  VTKM_CONT_EXPORT
362
  std::string GetAssocCellSet() const
Jeremy Meredith's avatar
Jeremy Meredith committed
363
  {
364
    return this->AssocCellSetName;
Jeremy Meredith's avatar
Jeremy Meredith committed
365 366
  }

367
  VTKM_CONT_EXPORT
368
  vtkm::IdComponent GetAssocLogicalDim() const
Jeremy Meredith's avatar
Jeremy Meredith committed
369
  {
370
    return this->AssocLogicalDim;
Jeremy Meredith's avatar
Jeremy Meredith committed
371 372
  }

Sujin Philip's avatar
Sujin Philip committed
373 374
  template<typename DeviceAdapterTag, typename TypeList, typename StorageList>
  VTKM_CONT_EXPORT
375 376 377
  const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(DeviceAdapterTag,
                                                       TypeList,
                                                       StorageList) const
Sujin Philip's avatar
Sujin Philip committed
378 379 380
  {
    if (this->ModifiedFlag)
    {
381 382
      internal::ComputeRange<DeviceAdapterTag> computeRange(this->Range);
      this->Data.ResetTypeAndStorageLists(TypeList(),StorageList()).CastAndCall(computeRange);
Sujin Philip's avatar
Sujin Philip committed
383 384 385
      this->ModifiedFlag = false;
    }

386
    return this->Range;
Sujin Philip's avatar
Sujin Philip committed
387 388 389 390
  }

  template<typename DeviceAdapterTag, typename TypeList, typename StorageList>
  VTKM_CONT_EXPORT
391 392 393 394
  void GetRange(vtkm::Range *range,
                DeviceAdapterTag,
                TypeList,
                StorageList) const
Sujin Philip's avatar
Sujin Philip committed
395
  {
396
    this->GetRange(DeviceAdapterTag(), TypeList(), StorageList());
Sujin Philip's avatar
Sujin Philip committed
397

398
    vtkm::Id length = this->Range.GetNumberOfValues();
Sujin Philip's avatar
Sujin Philip committed
399 400
    for (vtkm::Id i = 0; i < length; ++i)
    {
401
      range[i] = this->Range.GetPortalConstControl().Get(i);
Sujin Philip's avatar
Sujin Philip committed
402 403 404 405 406
    }
  }

  template<typename DeviceAdapterTag, typename TypeList>
  VTKM_CONT_EXPORT
407 408
  const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(DeviceAdapterTag,
                                                       TypeList) const
Sujin Philip's avatar
Sujin Philip committed
409
  {
410 411 412
    return this->GetRange(DeviceAdapterTag(),
                          TypeList(),
                          VTKM_DEFAULT_STORAGE_LIST_TAG());
Sujin Philip's avatar
Sujin Philip committed
413 414 415 416
  }

  template<typename DeviceAdapterTag, typename TypeList>
  VTKM_CONT_EXPORT
417
  void GetRange(vtkm::Range *range, DeviceAdapterTag, TypeList) const
Sujin Philip's avatar
Sujin Philip committed
418
  {
419 420 421 422
    this->GetRange(range,
                   DeviceAdapterTag(),
                   TypeList(),
                   VTKM_DEFAULT_STORAGE_LIST_TAG());
Sujin Philip's avatar
Sujin Philip committed
423 424 425 426
  }

  template<typename DeviceAdapterTag>
  VTKM_CONT_EXPORT
427
  const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(DeviceAdapterTag) const
Sujin Philip's avatar
Sujin Philip committed
428
  {
429 430 431
    return this->GetRange(DeviceAdapterTag(),
                          VTKM_DEFAULT_TYPE_LIST_TAG(),
                          VTKM_DEFAULT_STORAGE_LIST_TAG());
Sujin Philip's avatar
Sujin Philip committed
432 433 434 435
  }

  template<typename DeviceAdapterTag>
  VTKM_CONT_EXPORT
436
  void GetRange(vtkm::Range *range, DeviceAdapterTag) const
Sujin Philip's avatar
Sujin Philip committed
437
  {
438 439 440 441
    this->GetRange(range,
                   DeviceAdapterTag(),
                   VTKM_DEFAULT_TYPE_LIST_TAG(),
                   VTKM_DEFAULT_STORAGE_LIST_TAG());
Sujin Philip's avatar
Sujin Philip committed
442 443
  }

444
  VTKM_CONT_EXPORT
445
  const vtkm::cont::DynamicArrayHandle &GetData() const
Sujin Philip's avatar
Sujin Philip committed
446 447 448 449 450 451
  {
    return this->Data;
  }

  VTKM_CONT_EXPORT
  vtkm::cont::DynamicArrayHandle &GetData()
452
  {
Sujin Philip's avatar
Sujin Philip committed
453
    this->ModifiedFlag = true;
454
    return this->Data;
455 456
  }

Jeremy Meredith's avatar
Jeremy Meredith committed
457
  template <typename T>
458
  VTKM_CONT_EXPORT
459 460 461 462 463 464 465 466
  void SetData(const vtkm::cont::ArrayHandle<T> &newdata)
  {
    this->Data = newdata;
    this->ModifiedFlag = true;
  }

  VTKM_CONT_EXPORT
  void SetData(const vtkm::cont::DynamicArrayHandle &newdata)
467
  {
468
    this->Data = newdata;
Sujin Philip's avatar
Sujin Philip committed
469
    this->ModifiedFlag = true;
470 471
  }

472
  template <typename T>
473
  VTKM_CONT_EXPORT
474
  void CopyData(const T *ptr, vtkm::Id nvals)
475
  {
476 477 478 479 480 481 482 483 484 485
    //allocate main memory using an array handle
    vtkm::cont::ArrayHandle<T> tmp;
    tmp.Allocate(nvals);

    //copy into the memory owned by the array handle
    std::copy(ptr,
              ptr + static_cast<std::size_t>(nvals),
              vtkm::cont::ArrayPortalToIteratorBegin(tmp.GetPortalControl()));

    //assign to the dynamic array handle
486
    this->Data = tmp;
Sujin Philip's avatar
Sujin Philip committed
487
    this->ModifiedFlag = true;
488
  }
Dave Pugmire's avatar
Dave Pugmire committed
489

490
  VTKM_CONT_EXPORT
491
  virtual void PrintSummary(std::ostream &out) const
Dave Pugmire's avatar
Dave Pugmire committed
492
  {
493
      out<<"   "<<this->Name;
Dave Pugmire's avatar
Dave Pugmire committed
494
      out<<" assoc= ";
495
      switch (this->GetAssociation())
Dave Pugmire's avatar
Dave Pugmire committed
496
      {
497
      case ASSOC_ANY: out<<"Any "; break;
Dave Pugmire's avatar
Dave Pugmire committed
498 499 500 501 502
      case ASSOC_WHOLE_MESH: out<<"Mesh "; break;
      case ASSOC_POINTS: out<<"Points "; break;
      case ASSOC_CELL_SET: out<<"Cells "; break;
      case ASSOC_LOGICAL_DIM: out<<"LogicalDim "; break;
      }
503
      this->Data.PrintSummary(out);
Dave Pugmire's avatar
Dave Pugmire committed
504 505
      out<<"\n";
  }
506

507
private:
508
  std::string       Name;  ///< name of field
509

510 511 512
  AssociationEnum   Association;
  std::string       AssocCellSetName;  ///< only populate if assoc is cells
  vtkm::IdComponent AssocLogicalDim; ///< only populate if assoc is logical dim
Jeremy Meredith's avatar
Jeremy Meredith committed
513

514
  vtkm::cont::DynamicArrayHandle Data;
515
  mutable vtkm::cont::ArrayHandle<vtkm::Range> Range;
Sujin Philip's avatar
Sujin Philip committed
516
  mutable bool ModifiedFlag;
517 518 519 520 521 522
};


} // namespace cont
} // namespace vtkm

523
#endif //vtk_m_cont_Field_h