vtkMatricizeArray.cxx 4.43 KB
Newer Older
1 2 3 4
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkMatricizeArray.cxx
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
-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm 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.

=========================================================================*/

#include "vtkCommand.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMatricizeArray.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkSparseArray.h"

30
#include <numeric>
31 32 33 34 35 36 37 38 39 40 41

///////////////////////////////////////////////////////////////////////////////
// vtkMatricizeArray

vtkStandardNewMacro(vtkMatricizeArray);

vtkMatricizeArray::vtkMatricizeArray() :
  SliceDimension(0)
{
}

42
vtkMatricizeArray::~vtkMatricizeArray() = default;
43 44 45 46 47 48 49 50

void vtkMatricizeArray::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "SliceDimension: " << this->SliceDimension << endl;
}

int vtkMatricizeArray::RequestData(
51 52
  vtkInformation*,
  vtkInformationVector** inputVector,
53 54 55
  vtkInformationVector* outputVector)
{
  vtkArrayData* const input = vtkArrayData::GetData(inputVector[0]);
56
  if(input->GetNumberOfArrays() != 1)
57
  {
58 59
    vtkErrorMacro(<< "vtkMatricizeArray requires vtkArrayData containing exactly one array as input.");
    return 0;
60
  }
61

62 63
  vtkSparseArray<double>* const input_array = vtkSparseArray<double>::SafeDownCast(
    input->GetArray(static_cast<vtkIdType>(0)));
64
  if(!input_array)
65
  {
66 67
    vtkErrorMacro(<< "vtkMatricizeArray requires a vtkSparseArray<double> as input.");
    return 0;
68
  }
69 70

  if(this->SliceDimension < 0 || this->SliceDimension >= input_array->GetDimensions())
71
  {
72 73
    vtkErrorMacro(<< "Slice dimension " << this->SliceDimension << " out-of-range for array with " << input_array->GetDimensions() << " dimensions.");
    return 0;
74
  }
75 76 77 78 79 80 81

  vtkSparseArray<double>* const output_array = vtkSparseArray<double>::New();

  // Compute the extents of the output array ...
  const vtkArrayExtents input_extents = input_array->GetExtents();
  vtkArrayExtents output_extents(0, 0);
  output_extents[0] = input_extents[this->SliceDimension];
82
  output_extents[1] = vtkArrayRange(0, input_extents.GetSize() / input_extents[this->SliceDimension].GetSize());
83 84 85 86 87 88 89 90 91
  output_array->Resize(output_extents);

  // "Map" every non-null element in the input array to its position in the output array.
  // Indices in the slice dimension map directly to the row index in the output.
  // The remaining coordinates are multiplied by a "stride" value for each dimension and
  // the results are summed to compute the output column index.
  //
  // Setting the slice-dimension stride to zero simplifies computation of column coordinates
  // later-on and eliminate an inner-loop comparison.
92
  std::vector<vtkIdType> strides(input_array->GetDimensions());
93
  for(vtkIdType i = input_array->GetDimensions() - 1, stride = 1; i >= 0; --i)
94
  {
95
    if(i == this->SliceDimension)
96
    {
97
      strides[i] = 0;
98
    }
99
    else
100
    {
101
      strides[i] = stride;
102
      stride *= input_extents[i].GetSize();
103
    }
104
  }
105

106
  std::vector<vtkIdType> temp(input_array->GetDimensions());
107 108 109 110 111

  vtkArrayCoordinates coordinates;
  vtkArrayCoordinates new_coordinates(0, 0);
  const vtkIdType element_count = input_array->GetNonNullSize();
  for(vtkIdType n = 0; n != element_count; ++n)
112
  {
113 114 115 116 117
    input_array->GetCoordinatesN(n, coordinates);

    new_coordinates[0] = coordinates[this->SliceDimension];

    for(vtkIdType i = 0; i != coordinates.GetDimensions(); ++i)
118
      temp[i] = (coordinates[i] - input_extents[i].GetBegin()) * strides[i];
119
    new_coordinates[1] = std::accumulate(temp.begin(), temp.end(), 0);
120 121

    output_array->AddValue(new_coordinates, input_array->GetValueN(n));
122
  }
123 124

  vtkArrayData* const output = vtkArrayData::GetData(outputVector);
125 126
  output->ClearArrays();
  output->AddArray(output_array);
127 128 129 130 131
  output_array->Delete();

  return 1;
}