Newer
Older
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStringToNumeric.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.
=========================================================================*/
/*-------------------------------------------------------------------------
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.
-------------------------------------------------------------------------*/
#include "vtkStringToNumeric.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkDemandDrivenPipeline.h"
#include "vtkFieldData.h"
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStringArray.h"

Jeff Baumes
committed
#include "vtkTable.h"
Brian Wylie
committed
#include "vtkUnicodeStringArray.h"
#include "vtkVariant.h"
vtkStandardNewMacro(vtkStringToNumeric);
vtkStringToNumeric::vtkStringToNumeric()
{
this->ConvertFieldData = true;
this->ConvertPointData = true;
this->ConvertCellData = true;
this->ForceDouble = false;
this->DefaultIntegerValue = 0;
this->DefaultDoubleValue = 0.0;
this->TrimWhitespacePriorToNumericConversion = false;
}
vtkStringToNumeric::~vtkStringToNumeric()
{
}
Andy Wilson
committed
int vtkStringToNumeric::CountItemsToConvert(vtkFieldData *fieldData)
{
int count = 0;
for (int arr = 0; arr < fieldData->GetNumberOfArrays(); arr++)
Andy Wilson
committed
vtkAbstractArray *array = fieldData->GetAbstractArray(arr);
vtkStringArray* stringArray = vtkArrayDownCast<vtkStringArray>(array);
vtkUnicodeStringArray* unicodeArray =
vtkArrayDownCast<vtkUnicodeStringArray>(array);
Andy Wilson
committed
if (!stringArray && !unicodeArray)
Andy Wilson
committed
continue;
Andy Wilson
committed
else
Andy Wilson
committed
count += array->GetNumberOfTuples() * array->GetNumberOfComponents();
}
Andy Wilson
committed
return count;
}
vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// Get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// Get the input and output objects
vtkDataObject* input = inInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataObject* output = outInfo->Get(vtkDataObject::DATA_OBJECT());
output->ShallowCopy(input);
Andy Wilson
committed
vtkDataSet* outputDataSet = vtkDataSet::SafeDownCast(output);
vtkGraph* outputGraph = vtkGraph::SafeDownCast(output);
vtkTable* outputTable = vtkTable::SafeDownCast(output);
// Figure out how many items we have to process
int itemCount = 0;
if (this->ConvertFieldData)
Andy Wilson
committed
itemCount += this->CountItemsToConvert(output->GetFieldData());
Andy Wilson
committed
if (outputDataSet && this->ConvertPointData)
Andy Wilson
committed
itemCount += this->CountItemsToConvert(outputDataSet->GetPointData());
Andy Wilson
committed
if (outputDataSet && this->ConvertCellData)
Andy Wilson
committed
itemCount += this->CountItemsToConvert(outputDataSet->GetCellData());
Andy Wilson
committed
if (outputGraph && this->ConvertPointData)
Andy Wilson
committed
itemCount += this->CountItemsToConvert(outputGraph->GetVertexData());
Andy Wilson
committed
if (outputGraph && this->ConvertCellData)
Andy Wilson
committed
itemCount += this->CountItemsToConvert(outputGraph->GetEdgeData());
Andy Wilson
committed
if (outputTable && this->ConvertPointData)
Andy Wilson
committed
itemCount += this->CountItemsToConvert(outputTable->GetRowData());
Andy Wilson
committed
this->ItemsToConvert = itemCount;
this->ItemsConverted = 0;
this->ConvertArrays(output->GetFieldData());
if (outputDataSet && this->ConvertPointData)
this->ConvertArrays(outputDataSet->GetPointData());
if (outputDataSet && this->ConvertCellData)
this->ConvertArrays(outputDataSet->GetCellData());
if (outputGraph && this->ConvertPointData)
this->ConvertArrays(outputGraph->GetVertexData());
if (outputGraph && this->ConvertCellData)
this->ConvertArrays(outputGraph->GetEdgeData());

Jeff Baumes
committed
if (outputTable && this->ConvertPointData)

Jeff Baumes
committed
this->ConvertArrays(outputTable->GetRowData());
return 1;
}
void vtkStringToNumeric::ConvertArrays(vtkFieldData* fieldData)
{
for (int arr = 0; arr < fieldData->GetNumberOfArrays(); arr++)
vtkStringArray* stringArray = vtkArrayDownCast<vtkStringArray>(
vtkUnicodeStringArray* unicodeArray = vtkArrayDownCast<vtkUnicodeStringArray>(
Brian Wylie
committed
fieldData->GetAbstractArray(arr));
if (!stringArray && !unicodeArray)
Brian Wylie
committed
vtkIdType numTuples,numComps;
vtkStdString arrayName;
if (stringArray)
Brian Wylie
committed
numTuples = stringArray->GetNumberOfTuples();
numComps = stringArray->GetNumberOfComponents();
arrayName = stringArray->GetName();
Brian Wylie
committed
else
Brian Wylie
committed
numTuples = unicodeArray->GetNumberOfTuples();
numComps = unicodeArray->GetNumberOfComponents();
arrayName = unicodeArray->GetName();
Brian Wylie
committed
// Set up the output array
vtkDoubleArray* doubleArray = vtkDoubleArray::New();
doubleArray->SetNumberOfComponents(numComps);
Kenneth Moreland
committed
doubleArray->SetNumberOfTuples(numTuples);
Brian Wylie
committed
doubleArray->SetName(arrayName);
// Set up the output array
vtkIntArray* intArray = vtkIntArray::New();
intArray->SetNumberOfComponents(numComps);
Kenneth Moreland
committed
intArray->SetNumberOfTuples(numTuples);
Brian Wylie
committed
intArray->SetName(arrayName);
// Convert the strings to time point values
bool allInteger = true;
bool allNumeric = true;
for (vtkIdType i = 0; i < numTuples*numComps; i++)
Andy Wilson
committed
++ this->ItemsConverted;
if (this->ItemsConverted % 100 == 0)
Andy Wilson
committed
this->UpdateProgress(static_cast<double>(this->ItemsConverted) /
static_cast<double>(this->ItemsToConvert));
Andy Wilson
committed
Brian Wylie
committed
vtkStdString str;
if (stringArray)
Brian Wylie
committed
str = stringArray->GetValue(i);
Brian Wylie
committed
else
str = unicodeArray->GetValue(i).utf8_str();
if (this->TrimWhitespacePriorToNumericConversion)
size_t startPos = str.find_first_not_of(" \n\t\r");
if (startPos == vtkStdString::npos)
str = "";
else
size_t endPos = str.find_last_not_of(" \n\t\r");
str = str.substr(startPos, endPos-startPos+1);
}
intArray->SetValue(i, this->DefaultIntegerValue);
doubleArray->SetValue(i, this->DefaultDoubleValue);
int intValue = vtkVariant(str).ToInt(&ok);
if (ok)
double doubleValue = intValue;
intArray->SetValue(i, intValue);
doubleArray->SetValue(i, doubleValue);
doubleArray->SetValue(i, this->DefaultDoubleValue);
double doubleValue = vtkVariant(str).ToDouble(&ok);
if (!ok)
doubleArray->SetValue(i, doubleValue);
}
}
// Calling AddArray will replace the old array since the names match.
// Are they all ints, and did I test anything?
if (!this->ForceDouble && allInteger && numTuples && numComps)
fieldData->AddArray(doubleArray);
}
intArray->Delete();
doubleArray->Delete();
}
//----------------------------------------------------------------------------
int vtkStringToNumeric::ProcessRequest(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// create the output
if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA_OBJECT()))
return this->RequestDataObject(request, inputVector, outputVector);
return this->Superclass::ProcessRequest(request, inputVector, outputVector);
}
//----------------------------------------------------------------------------
int vtkStringToNumeric::RequestDataObject(
vtkInformation*,
vtkInformationVector** inputVector ,
vtkInformationVector* outputVector)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
if (!inInfo)
vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
// for each output
for(int i=0; i < this->GetNumberOfOutputPorts(); ++i)
vtkInformation* info = outputVector->GetInformationObject(i);
vtkDataObject *output = info->Get(vtkDataObject::DATA_OBJECT());
if (!output || !output->IsA(input->GetClassName()))
vtkDataObject* newOutput = input->NewInstance();
info->Set(vtkDataObject::DATA_OBJECT(), newOutput);
return 0;
}
void vtkStringToNumeric::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ConvertFieldData: "
<< (this->ConvertFieldData ? "on" : "off") << endl;
os << indent << "ConvertPointData: "
<< (this->ConvertPointData ? "on" : "off") << endl;
os << indent << "ConvertCellData: "
<< (this->ConvertCellData ? "on" : "off") << endl;
os << indent << "ForceDouble: "
<< (this->ForceDouble ? "on" : "off") << endl;
os << indent << "DefaultIntegerValue: "
<< this->DefaultIntegerValue << endl;
os << indent << "DefaultDoubleValue: "
<< this->DefaultDoubleValue << endl;
os << indent << "TrimWhitespacePriorToNumericConversion: "
<< (this->TrimWhitespacePriorToNumericConversion ? "on" : "off") << endl;