vtkTableToStructuredGrid.cxx 6.06 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTableToStructuredGrid.cxx

  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 "vtkTableToStructuredGrid.h"

#include "vtkDoubleArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkTable.h"

vtkStandardNewMacro(vtkTableToStructuredGrid);
//----------------------------------------------------------------------------
vtkTableToStructuredGrid::vtkTableToStructuredGrid()
{
30 31 32
  this->XColumn = nullptr;
  this->YColumn = nullptr;
  this->ZColumn = nullptr;
33 34 35 36 37 38 39 40 41 42
  this->XComponent = 0;
  this->YComponent = 0;
  this->ZComponent = 0;
  this->WholeExtent[0] = this->WholeExtent[1] = this->WholeExtent[2] =
    this->WholeExtent[3] = this->WholeExtent[4] = this->WholeExtent[5] = 0;
}

//----------------------------------------------------------------------------
vtkTableToStructuredGrid::~vtkTableToStructuredGrid()
{
43 44 45
  this->SetXColumn(nullptr);
  this->SetYColumn(nullptr);
  this->SetZColumn(nullptr);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

//----------------------------------------------------------------------------
int vtkTableToStructuredGrid::FillInputPortInformation(
  int vtkNotUsed(port), vtkInformation* info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
  return 1;
}

// ----------------------------------------------------------------------------
int vtkTableToStructuredGrid::RequestInformation(
   vtkInformation *vtkNotUsed(request),
   vtkInformationVector **vtkNotUsed(inputVector),
   vtkInformationVector *outputVector)
{
  // get the info objects
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
               this->WholeExtent, 6);
66

67 68 69 70 71 72
  return 1;
}


//----------------------------------------------------------------------------
int vtkTableToStructuredGrid::RequestData(
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
73
  vtkInformation* vtkNotUsed(request), vtkInformationVector** inputVector,
74 75 76 77 78
  vtkInformationVector* outputVector)
{
  vtkStructuredGrid* output = vtkStructuredGrid::GetData(outputVector, 0);
  vtkTable* input = vtkTable::GetData(inputVector[0], 0);

79
  vtkStreamingDemandDrivenPipeline *sddp =
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    vtkStreamingDemandDrivenPipeline::SafeDownCast(this->GetExecutive());
  int extent[6];
  sddp->GetOutputInformation(0)->Get(
    vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(), extent);
  return this->Convert(input, output, extent);
}

//----------------------------------------------------------------------------
int vtkTableToStructuredGrid::Convert(vtkTable* input,
  vtkStructuredGrid* output, int extent[6])
{
  int num_values = (extent[1] - extent[0] + 1) *
    (extent[3] - extent[2] + 1) *
    (extent[5] - extent[4] + 1);

  if (input->GetNumberOfRows() != num_values)
96
  {
97 98 99
    vtkErrorMacro("The input table must have exactly " << num_values
      << " rows. Currently it has " << input->GetNumberOfRows() << " rows.");
    return 0;
100
  }
101

102
  vtkDataArray* xarray = vtkArrayDownCast<vtkDataArray>(
103
    input->GetColumnByName(this->XColumn));
104
  vtkDataArray* yarray = vtkArrayDownCast<vtkDataArray>(
105
    input->GetColumnByName(this->YColumn));
106
  vtkDataArray* zarray = vtkArrayDownCast<vtkDataArray>(
107 108
    input->GetColumnByName(this->ZColumn));
  if (!xarray || !yarray || !zarray)
109
  {
110
    vtkErrorMacro("Failed to locate the columns to use for the point"
111 112
      " coordinates");
    return 0;
113
  }
114 115

  vtkPoints* newPoints = vtkPoints::New();
116
  if (xarray == yarray && yarray == zarray &&
117 118 119 120
    this->XComponent == 0 &&
    this->YComponent == 1 &&
    this->ZComponent == 2 &&
    xarray->GetNumberOfComponents() == 3)
121
  {
122
    newPoints->SetData(xarray);
123
  }
124
  else
125
  {
126 127 128 129 130 131 132
    // Ideally we determine the smallest data type that can contain the values
    // in all the 3 arrays. For now I am just going with doubles.
    vtkDoubleArray* newData =  vtkDoubleArray::New();
    newData->SetNumberOfComponents(3);
    newData->SetNumberOfTuples(input->GetNumberOfRows());
    vtkIdType numtuples = newData->GetNumberOfTuples();
    for (vtkIdType cc=0; cc < numtuples; cc++)
133
    {
134 135 136
      newData->SetComponent(cc, 0, xarray->GetComponent(cc, this->XComponent));
      newData->SetComponent(cc, 1, yarray->GetComponent(cc, this->YComponent));
      newData->SetComponent(cc, 2, zarray->GetComponent(cc, this->ZComponent));
137
    }
138 139
    newPoints->SetData(newData);
    newData->Delete();
140
  }
141 142 143 144 145 146 147

  output->SetExtent(extent);
  output->SetPoints(newPoints);
  newPoints->Delete();

  // Add all other columns as point data.
  for (int cc=0; cc < input->GetNumberOfColumns(); cc++)
148
  {
149 150
    vtkAbstractArray* arr = input->GetColumn(cc);
    if (arr != xarray && arr != yarray && arr != zarray)
151
    {
152 153
      output->GetPointData()->AddArray(arr);
    }
154
  }
155 156 157 158 159 160 161
  return 1;
}

//----------------------------------------------------------------------------
void vtkTableToStructuredGrid::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
162
  os << indent << "WholeExtent: "
163 164 165 166 167 168
    << this->WholeExtent[0] << ", "
    << this->WholeExtent[1] << ", "
    << this->WholeExtent[2] << ", "
    << this->WholeExtent[3] << ", "
    << this->WholeExtent[4] << ", "
    << this->WholeExtent[5] << endl;
169
  os << indent << "XColumn: "
170 171
    << (this->XColumn? this->XColumn : "(none)") << endl;
  os << indent << "XComponent: " << this->XComponent << endl;
172
  os << indent << "YColumn: "
173 174
    << (this->YColumn? this->YColumn : "(none)") << endl;
  os << indent << "YComponent: " << this->YComponent << endl;
175
  os << indent << "ZColumn: "
176 177 178 179
    << (this->ZColumn? this->ZColumn : "(none)") << endl;
  os << indent << "ZComponent: " << this->ZComponent << endl;
}