vtkmAverageToPoints.cxx 3.71 KB
Newer Older
Berk Geveci's avatar
Berk Geveci 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
//=============================================================================
//
//  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 2012 Sandia Corporation.
//  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
//  the U.S. Government retains certain rights in this software.
//
//=============================================================================
#include "vtkmAverageToPoints.h"

#include "vtkDataSet.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"

#include "vtkmlib/Storage.h"
#include "vtkmlib/DataSetConverters.h"
#include "vtkmlib/ArrayConverters.h"

#include "vtkmCellSetExplicit.h"
#include "vtkmCellSetSingleType.h"
#include "vtkmFilterPolicy.h"

#include <vtkm/filter/PointAverage.h>

vtkStandardNewMacro(vtkmAverageToPoints)

//------------------------------------------------------------------------------
vtkmAverageToPoints::vtkmAverageToPoints()
{
}

//------------------------------------------------------------------------------
vtkmAverageToPoints::~vtkmAverageToPoints()
{
}

//------------------------------------------------------------------------------
48 49 50
int vtkmAverageToPoints::RequestData(vtkInformation *vtkNotUsed(request),
                                     vtkInformationVector **inputVector,
                                     vtkInformationVector *outputVector)
Berk Geveci's avatar
Berk Geveci committed
51 52 53 54 55 56 57 58 59 60 61 62 63
{
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));

  vtkDataSet* output = vtkDataSet::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  output->ShallowCopy(input);

  //grab the input array to process to determine the field we want to average
  int association = this->GetInputArrayAssociation(0, inputVector);
Sujin Philip's avatar
Sujin Philip committed
64 65 66 67 68 69
  auto fieldArray = this->GetInputArrayToProcess(0, inputVector);
  if (association != vtkDataObject::FIELD_ASSOCIATION_CELLS ||
      fieldArray == nullptr ||
      fieldArray->GetName() == nullptr || fieldArray->GetName()[0] == '\0')
  {
    vtkErrorMacro(<< "Invalid field: Requires a cell field with a valid name.");
Berk Geveci's avatar
Berk Geveci committed
70
    return 0;
Sujin Philip's avatar
Sujin Philip committed
71 72 73
  }

  const char* fieldName = fieldArray->GetName();
Berk Geveci's avatar
Berk Geveci committed
74

Sujin Philip's avatar
Sujin Philip committed
75 76 77 78 79 80
  try
  {
    //convert the input dataset to a vtkm::cont::DataSet
    vtkm::cont::DataSet in = tovtkm::Convert(input);
    auto field = tovtkm::Convert(fieldArray, association);
    in.AddField(field);
Berk Geveci's avatar
Berk Geveci committed
81

Sujin Philip's avatar
Sujin Philip committed
82 83
    vtkmInputFilterPolicy policy;
    vtkm::filter::PointAverage filter;
84
    filter.SetActiveField(fieldName, vtkm::cont::Field::Association::CELL_SET);
Sujin Philip's avatar
Sujin Philip committed
85 86 87 88 89 90 91 92
    filter.SetOutputFieldName(fieldName); //should we expose this control?

    auto result = filter.Execute(in, policy);

    // convert back the dataset to VTK, and add the field as a point field
    vtkDataArray* resultingArray =
      fromvtkm::Convert(result.GetPointField(fieldName));
    if (resultingArray == nullptr)
Berk Geveci's avatar
Berk Geveci committed
93
    {
Sujin Philip's avatar
Sujin Philip committed
94 95 96 97
      vtkErrorMacro(<< "Unable to convert result array from VTK-m to VTK");
      return 0;
    }

Berk Geveci's avatar
Berk Geveci committed
98
    output->GetPointData()->AddArray(resultingArray);
99
    resultingArray->FastDelete();
Sujin Philip's avatar
Sujin Philip committed
100 101 102 103 104 105
  }
  catch (const vtkm::cont::Error& e)
  {
    vtkErrorMacro(<< "VTK-m error: " << e.GetMessage());
    return 0;
  }
Berk Geveci's avatar
Berk Geveci committed
106

Sujin Philip's avatar
Sujin Philip committed
107
  return 1;
Berk Geveci's avatar
Berk Geveci committed
108 109 110 111 112 113 114
}

//------------------------------------------------------------------------------
void vtkmAverageToPoints::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}