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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*=========================================================================
Program: ParaView
Module: vtkTableToPolyData.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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 "vtkTableToPolyData.h"
#include "vtkDoubleArray.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkTable.h"
#include "vtkCellArray.h"
#include "vtkInformation.h"
vtkStandardNewMacro(vtkTableToPolyData);
//----------------------------------------------------------------------------
vtkTableToPolyData::vtkTableToPolyData()
{
this->XColumn = 0;
this->YColumn = 0;
this->ZColumn = 0;
this->XColumnIndex = -1;
this->YColumnIndex = -1;
this->ZColumnIndex = -1;
this->XComponent = 0;
this->YComponent = 0;
this->ZComponent = 0;
this->Create2DPoints = 0;
this->PreserveCoordinateColumnsAsDataArrays = false;
}
//----------------------------------------------------------------------------
vtkTableToPolyData::~vtkTableToPolyData()
{
this->SetXColumn(0);
this->SetYColumn(0);
this->SetZColumn(0);
}
//----------------------------------------------------------------------------
int vtkTableToPolyData::FillInputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
return 1;
}
//----------------------------------------------------------------------------
int vtkTableToPolyData::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
vtkTable* input = vtkTable::GetData(inputVector[0], 0);
vtkPolyData* output = vtkPolyData::GetData(outputVector, 0);
if (input->GetNumberOfRows() == 0)
{
// empty input.
return 1;
}
vtkDataArray* xarray = NULL;
vtkDataArray* yarray = NULL;
vtkDataArray* zarray = NULL;
if(this->XColumn && this->YColumn)
{
xarray = vtkArrayDownCast<vtkDataArray>(
input->GetColumnByName(this->XColumn));
yarray = vtkArrayDownCast<vtkDataArray>(
input->GetColumnByName(this->YColumn));
zarray = vtkArrayDownCast<vtkDataArray>(
input->GetColumnByName(this->ZColumn));
}
else if(this->XColumnIndex >= 0)
{
xarray = vtkArrayDownCast<vtkDataArray>(
input->GetColumn(this->XColumnIndex));
yarray = vtkArrayDownCast<vtkDataArray>(
input->GetColumn(this->YColumnIndex));
zarray = vtkArrayDownCast<vtkDataArray>(
input->GetColumn(this->ZColumnIndex));
}
// zarray is optional
if(this->Create2DPoints)
{
if (!xarray || !yarray)
{
vtkErrorMacro("Failed to locate the columns to use for the point"
" coordinates");
return 0;
}
}
else
{
if (!xarray || !yarray || !zarray)
{
vtkErrorMacro("Failed to locate the columns to use for the point"
" coordinates");
return 0;
}
}
vtkPoints* newPoints = vtkPoints::New();
if (xarray == yarray && yarray == zarray &&
this->XComponent == 0 &&
this->YComponent == 1 &&
this->ZComponent == 2 &&
xarray->GetNumberOfComponents() == 3)
{
newPoints->SetData(xarray);
}
else
{
// 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();
if(this->Create2DPoints)
{
for (vtkIdType cc=0; cc < numtuples; cc++)
{
newData->SetComponent(cc, 0, xarray->GetComponent(cc, this->XComponent));
newData->SetComponent(cc, 1, yarray->GetComponent(cc, this->YComponent));
newData->SetComponent(cc, 2, 0.0);
}
}
else
{
for (vtkIdType cc=0; cc < numtuples; cc++)
{
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));
}
}
newPoints->SetData(newData);
newData->Delete();
}
output->SetPoints(newPoints);
newPoints->Delete();
// Now create a poly-vertex cell will all the points.
vtkIdType numPts = newPoints->GetNumberOfPoints();
vtkIdType *ptIds = new vtkIdType[numPts];
for (vtkIdType cc=0; cc < numPts; cc++)
{
ptIds[cc] = cc;
}
output->Allocate(1);
output->InsertNextCell(VTK_POLY_VERTEX, numPts, ptIds);
delete [] ptIds;
// Add all other columns as point data.
for (int cc=0; cc < input->GetNumberOfColumns(); cc++)
{
vtkAbstractArray* arr = input->GetColumn(cc);
if(this->PreserveCoordinateColumnsAsDataArrays)
{
output->GetPointData()->AddArray(arr);
}
else if (arr != xarray && arr != yarray && arr != zarray)
{
output->GetPointData()->AddArray(arr);
}
}
return 1;
}
//----------------------------------------------------------------------------
void vtkTableToPolyData::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "XColumn: "
<< (this->XColumn? this->XColumn : "(none)") << endl;
os << indent << "XComponent: " << this->XComponent << endl;
os << indent << "XColumnIndex: " << this->XColumnIndex << endl;
os << indent << "YColumn: "
<< (this->YColumn? this->YColumn : "(none)") << endl;
os << indent << "YComponent: " << this->YComponent << endl;
os << indent << "YColumnIndex: " << this->YColumnIndex << endl;
os << indent << "ZColumn: "
<< (this->ZColumn? this->ZColumn : "(none)") << endl;
os << indent << "ZComponent: " << this->ZComponent << endl;
os << indent << "ZColumnIndex: " << this->ZColumnIndex << endl;
os << indent << "Create2DPoints: " << (this->Create2DPoints ? "true" : "false") << endl;
os << indent << "PreserveCoordinateColumnsAsDataArrays: "
<< (this->PreserveCoordinateColumnsAsDataArrays ? "true" : "false") << endl;
}
|