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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageDataToUniformGrid.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 "vtkImageDataToUniformGrid.h"
#include "vtkCellData.h"
#include "vtkDataObjectTree.h"
#include "vtkDataObjectTreeIterator.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkUniformGrid.h"
#include "vtkUnsignedCharArray.h"
vtkStandardNewMacro(vtkImageDataToUniformGrid);
//----------------------------------------------------------------------------
vtkImageDataToUniformGrid::vtkImageDataToUniformGrid()
{
this->Reverse = 0;
}
//----------------------------------------------------------------------------
vtkImageDataToUniformGrid::~vtkImageDataToUniformGrid()
{
}
//----------------------------------------------------------------------------
void vtkImageDataToUniformGrid::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Reverse: " << this->Reverse << "\n";
}
//----------------------------------------------------------------------------
int vtkImageDataToUniformGrid::RequestDataObject(vtkInformation *,
vtkInformationVector **inV,
vtkInformationVector *outV)
{
vtkInformation* inInfo = inV[0]->GetInformationObject(0);
if (!inInfo)
{
return VTK_ERROR;
}
vtkInformation* outInfo = outV->GetInformationObject(0);
if(vtkDataObjectTree* input = vtkDataObjectTree::GetData(inInfo) )
{ // multiblock data sets
vtkDataObjectTree* output = vtkDataObjectTree::GetData(outInfo);
if (!output)
{
output = input->NewInstance();
outInfo->Set(vtkDataObject::DATA_OBJECT(), output);
this->GetOutputPortInformation(0)->Set(
vtkDataObject::DATA_EXTENT_TYPE(), output->GetExtentType());
output->Delete();
}
return VTK_OK;
}
if(vtkImageData::GetData(inInfo) != NULL)
{
vtkUniformGrid* output = vtkUniformGrid::New();
outInfo->Set(vtkDataObject::DATA_OBJECT(), output);
this->GetOutputPortInformation(0)->Set(
vtkDataObject::DATA_EXTENT_TYPE(), output->GetExtentType());
output->Delete();
return VTK_OK;
}
vtkErrorMacro("Don't know how to handle input of type " <<
vtkDataObject::GetData(inInfo)->GetClassName());
return VTK_ERROR;
}
//----------------------------------------------------------------------------
int vtkImageDataToUniformGrid::RequestData(vtkInformation *,
vtkInformationVector **,
vtkInformationVector *outV)
{
vtkDataObject* input = this->GetInput();
vtkInformation *outInfo = outV->GetInformationObject(0);
vtkDataObject *output = outInfo->Get(vtkDataObject::DATA_OBJECT());
vtkInformation *inArrayInfo = this->GetInputArrayInformation(0);
if (!inArrayInfo)
{
vtkErrorMacro("Problem getting array to process.");
return 0;
}
int association = -1;
if (!inArrayInfo->Has(vtkDataObject::FIELD_ASSOCIATION()))
{
vtkErrorMacro("Unable to query field association for the scalar.");
return 0;
}
association = inArrayInfo->Get(vtkDataObject::FIELD_ASSOCIATION());
const char* arrayName = inArrayInfo->Get(vtkDataObject::FIELD_NAME());
if (!arrayName)
{
vtkErrorMacro("Problem getting array name to process.");
return 0;
}
if(vtkImageData* inImageData = vtkImageData::SafeDownCast(input))
{
return this->Process(inImageData, association, arrayName,
vtkUniformGrid::SafeDownCast(output));
}
vtkDataObjectTree* inMB = vtkDataObjectTree::SafeDownCast(input);
vtkDataObjectTree* outMB = vtkDataObjectTree::SafeDownCast(output);
outMB->CopyStructure(inMB);
vtkDataObjectTreeIterator* iter = inMB->NewTreeIterator();
iter->VisitOnlyLeavesOn();
iter->TraverseSubTreeOn();
for(iter->GoToFirstItem();!iter->IsDoneWithTraversal();iter->GoToNextItem())
{
if(vtkImageData* inImageData =
vtkImageData::SafeDownCast(iter->GetCurrentDataObject()))
{
vtkNew<vtkUniformGrid> outUniformGrid;
if(this->Process(inImageData, association, arrayName, outUniformGrid.GetPointer()) != VTK_OK)
{
iter->Delete();
return VTK_ERROR;
}
outMB->SetDataSetFrom(iter, outUniformGrid.GetPointer());
}
else
{ // not a uniform grid so we just shallow copy from input to output
outMB->SetDataSetFrom(iter, iter->GetCurrentDataObject());
}
}
iter->Delete();
return VTK_OK;
}
//-----------------------------------------------------------------------------
int vtkImageDataToUniformGrid::FillInputPortInformation(int port,
vtkInformation* info)
{
this->Superclass::FillInputPortInformation(port, info);
// According to the documentation this is the way to append additional
// input data set type since VTK 5.2.
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(),
"vtkDataObjectTree");
return VTK_OK;
}
//----------------------------------------------------------------------------
int vtkImageDataToUniformGrid::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
// now add our info
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkDataObject");
return VTK_OK;
}
//----------------------------------------------------------------------------
int vtkImageDataToUniformGrid::Process(
vtkImageData* input, int association,
const char* arrayName, vtkUniformGrid* output)
{
if(vtkUniformGrid* uniformGrid = vtkUniformGrid::SafeDownCast(input))
{
output->ShallowCopy(uniformGrid);
}
else
{
output->ShallowCopy(input);
}
vtkDataArray* inScalars = NULL;
if(association == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
inScalars = input->GetPointData()->GetArray(arrayName);
}
else if(association == vtkDataObject::FIELD_ASSOCIATION_CELLS)
{
inScalars = input->GetCellData()->GetArray(arrayName);
}
else
{
vtkErrorMacro("Wrong assocation type: " << association);
return VTK_ERROR;
}
if (!inScalars)
{
vtkErrorMacro("No scalar data to use for blanking.");
return VTK_ERROR;
}
else if(inScalars->GetNumberOfComponents() != 1)
{
vtkErrorMacro("Scalar data must be a single component array.");
return VTK_ERROR;
}
vtkNew<vtkUnsignedCharArray> blankingArray;
blankingArray->DeepCopy(inScalars);
blankingArray->SetName(vtkDataSetAttributes::GhostArrayName());
unsigned char value1;
unsigned char value2;
if (association == vtkDataObject::FIELD_ASSOCIATION_CELLS)
{
if (this->Reverse)
{
value1 = 0;
value2 = vtkDataSetAttributes::HIDDENCELL;
}
else
{
value1 = vtkDataSetAttributes::HIDDENCELL;
value2 = 0;
}
}
else
{
if (this->Reverse)
{
value1 = 0;
value2 = vtkDataSetAttributes::HIDDENPOINT;
}
else
{
value1 = vtkDataSetAttributes::HIDDENPOINT;
value2 = 0;
}
}
for(vtkIdType i=0;i<blankingArray->GetNumberOfTuples();i++)
{
char value = blankingArray->GetValue(i) == 0 ? value1 : value2;
blankingArray->SetValue(i, value);
}
if(association == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
output->GetPointData()->AddArray(blankingArray.GetPointer());
}
else
{
output->GetCellData()->AddArray(blankingArray.GetPointer());
}
return VTK_OK;
}
|