Commit 1100ba20 authored by Robert Maynard's avatar Robert Maynard
Browse files

BUG: Fixed problems with the component names implementation in the XML reading and writing

BUG: Fixed warnings that the component names implementation was causing in vtkDataSetAttributes, vtkAbstractArray, and vtkPVArrayInformation
ENH: Added support for component names to vtkAppendData and vtkTemporalStatistics
ENH: Add CopyComponentNames method to vtkAbstractArray so that we have a nicer way to copy component names
ENH: Added test for CopyComponentNames method to TestDataArrayComponentNames
parent 5777d999
......@@ -143,5 +143,69 @@ int TestDataArrayComponentNames(int,char *[])
deepCopy->Delete();
farray->Delete();
//test the copy components command
vtkIntArray *source = vtkIntArray::New();
source->SetComponentName(0,"x");
source->SetComponentName(1,"y");
source->SetComponentName(2,"z");
vtkIntArray *dest = vtkIntArray::New();
dest->SetComponentName(0,"a");
dest->SetComponentName(1,"b");
dest->SetComponentName(2,"c");
dest->CopyComponentNames( source );
if ( strcmp(dest->GetComponentName(0),"x") != 0||
strcmp(dest->GetComponentName(1),"y") != 0||
strcmp(dest->GetComponentName(2),"z") != 0)
{
cerr
<< "Copy Component Names failed to work correctly";
dest->Delete();
source->Delete();
return 1;
}
dest->Delete();
//test copy when dest has a null pointer
dest = vtkIntArray::New();
dest->CopyComponentNames( source );
if ( strcmp(dest->GetComponentName(0),"x") != 0||
strcmp(dest->GetComponentName(1),"y") != 0||
strcmp(dest->GetComponentName(2),"z") != 0)
{
cerr
<< "Copy Component Names failed to work correctly";
dest->Delete();
source->Delete();
return 1;
}
dest->Delete();
//test copy when dest had more components
dest = vtkIntArray::New();
dest->SetComponentName(10,"10");
dest->CopyComponentNames( source );
if ( strcmp(dest->GetComponentName(0),"x") != 0||
strcmp(dest->GetComponentName(1),"y") != 0||
strcmp(dest->GetComponentName(2),"z") != 0)
{
cerr
<< "Copy Component Names failed to work correctly";
dest->Delete();
source->Delete();
return 1;
}
dest->Delete();
source->Delete();
return 0;
}
......@@ -45,10 +45,14 @@
# endif
#endif
typedef vtkstd::vector< vtkStdString* > vtkComponentNameBase;
class vtkComponentNames : public vtkComponentNameBase {};
namespace
{
typedef vtkstd::vector< vtkStdString* > vtkInternalComponentNameBase;
}
class vtkAbstractArray::vtkInternalComponentNames : public vtkInternalComponentNameBase {};
vtkCxxRevisionMacro(vtkAbstractArray, "1.18");
vtkCxxRevisionMacro(vtkAbstractArray, "1.19");
//----------------------------------------------------------------------------
// Construct object with sane defaults.
......@@ -78,36 +82,36 @@ vtkAbstractArray::~vtkAbstractArray()
}
//----------------------------------------------------------------------------
void vtkAbstractArray::SetComponentName( int component, const char *name )
{
void vtkAbstractArray::SetComponentName( vtkIdType component, const char *name )
{
if ( component < 0 || name == NULL )
{
return;
}
unsigned int index = static_cast<unsigned int>( component );
if ( this->ComponentNames == NULL )
{
//delayed allocate
this->ComponentNames = new vtkComponentNames();
this->ComponentNames = new vtkAbstractArray::vtkInternalComponentNames();
}
if ( component == this->ComponentNames->size() )
if ( index == this->ComponentNames->size() )
{
//the array isn't large enough, so we will resize
this->ComponentNames->push_back( new vtkStdString(name) );
return;
}
else if ( component > this->ComponentNames->size() )
else if ( index > this->ComponentNames->size() )
{
this->ComponentNames->resize( component+1, NULL );
this->ComponentNames->resize( index+1, NULL );
}
//replace an exisiting element
vtkStdString *compName = this->ComponentNames->at(component);
vtkStdString *compName = this->ComponentNames->at(index);
if ( !compName )
{
compName = new vtkStdString(name);
this->ComponentNames->at(component) = compName;
this->ComponentNames->at(index) = compName;
}
else
{
......@@ -116,22 +120,54 @@ void vtkAbstractArray::SetComponentName( int component, const char *name )
}
//----------------------------------------------------------------------------
const char* vtkAbstractArray::GetComponentName( int component )
{
const char* vtkAbstractArray::GetComponentName( vtkIdType component )
{
unsigned int index = static_cast<unsigned int>( component );
if ( !this->ComponentNames || component < 0 ||
component >= this->ComponentNames->size() )
index >= this->ComponentNames->size() )
{
//make sure we have valid vector
return NULL;
}
vtkStdString *compName = this->ComponentNames->at( component );
vtkStdString *compName = this->ComponentNames->at( index );
return ( compName ) ? compName->c_str() : NULL;
}
//----------------------------------------------------------------------------
bool vtkAbstractArray::HasAComponentName()
{
{
return (this->ComponentNames) ? ( this->ComponentNames->size() > 0 ) : 0;
}
}
//----------------------------------------------------------------------------
int vtkAbstractArray::CopyComponentNames( vtkAbstractArray *da )
{
if ( da && da != this && da->ComponentNames )
{
//clear the vector of the all data
if ( !this->ComponentNames )
{
this->ComponentNames = new vtkAbstractArray::vtkInternalComponentNames();
}
//copy the passed in components
this->ComponentNames->clear();
this->ComponentNames->reserve( da->ComponentNames->size() );
const char *name;
for ( unsigned int i = 0; i < da->ComponentNames->size(); ++i )
{
name = da->GetComponentName(i);
if ( name )
{
this->SetComponentName(i, name);
}
}
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
void vtkAbstractArray::SetInformation(vtkInformation *args)
......@@ -195,28 +231,7 @@ void vtkAbstractArray::DeepCopy( vtkAbstractArray* da )
{
this->CopyInformation(da->GetInformation(),/*deep=*/1);
}
if ( da && da != this && da->ComponentNames )
{
//clear the vector of the all data
if ( this->ComponentNames )
{
this->ComponentNames->clear();
delete this->ComponentNames;
}
//copy the passed in components
this->ComponentNames = new vtkComponentNames();
this->ComponentNames->reserve( da->ComponentNames->size() );
const char *name;
for ( vtkIdType i = 0; i < da->ComponentNames->size(); ++i )
{
name = da->GetComponentName(i);
if ( name )
{
this->SetComponentName(i, name);
}
}
}
this->CopyComponentNames( da );
}
//----------------------------------------------------------------------------
......@@ -401,7 +416,7 @@ void vtkAbstractArray::PrintSelf(ostream& os, vtkIndent indent)
{
os << indent << "ComponentNames: " << endl;
vtkIndent nextIndent = indent.GetNextIndent();
for ( int i=0; i < this->ComponentNames->size(); ++i )
for ( unsigned int i=0; i < this->ComponentNames->size(); ++i )
{
os << nextIndent << i << " : " << this->ComponentNames->at(i) << endl;
}
......
......@@ -49,7 +49,6 @@ class vtkDataArray;
class vtkIdList;
class vtkIdTypeArray;
class vtkInformation;
class vtkComponentNames;
class VTK_COMMON_EXPORT vtkAbstractArray : public vtkObject
{
......@@ -94,16 +93,21 @@ public:
// Description:
// Set the name for a component. Must be >= 1.
void SetComponentName( int component, const char *name );
void SetComponentName( vtkIdType component, const char *name );
//Description:
// Get the component name for a given component.
// Note: will return the actual string that is stored
const char* GetComponentName( int component );
const char* GetComponentName( vtkIdType component );
// Description:
// Returns if any component has had a name assigned
bool HasAComponentName();
// Description:
// Copies the component names from the inputed array to the current array
// make sure that the current array has the same number of components as the input array
int CopyComponentNames( vtkAbstractArray *da );
// Description:
// Set the number of tuples (a component group) in the array. Note that
......@@ -353,7 +357,10 @@ protected:
vtkInformation* Information;
vtkComponentNames* ComponentNames; //names for each component
//BTX
class vtkInternalComponentNames;
vtkInternalComponentNames* ComponentNames; //names for each component
//ETX
private:
vtkAbstractArray(const vtkAbstractArray&); // Not implemented.
......
......@@ -33,10 +33,13 @@
#include <vtkstd/vector>
typedef vtkstd::vector< vtkStdString* > vtkComponentNameBase;
class vtkComponentNames : public vtkComponentNameBase {};
namespace
{
typedef vtkstd::vector< vtkStdString* > vtkInternalComponentNameBase;
}
class vtkDataSetAttributes::vtkInternalComponentNames : public vtkInternalComponentNameBase {};
vtkCxxRevisionMacro(vtkDataSetAttributes, "1.35");
vtkCxxRevisionMacro(vtkDataSetAttributes, "1.36");
vtkStandardNewMacro(vtkDataSetAttributes);
//--------------------------------------------------------------------------
......@@ -622,19 +625,7 @@ void vtkDataSetAttributes::InternalCopyAllocate(vtkDataSetAttributes* pd,
{
newAA = aa->NewInstance();
newAA->SetNumberOfComponents(aa->GetNumberOfComponents());
//set the component names
if ( aa->HasAComponentName() )
{
const char *name;
for ( vtkIdType i = 0; i < newAA->GetNumberOfComponents(); ++i)
{
name = aa->GetComponentName(i);
if ( name )
{
newAA->SetComponentName(i, name );
}
}
}
newAA->CopyComponentNames( aa );
newAA->SetName(aa->GetName());
if (aa->HasInformation())
{
......@@ -1686,7 +1677,8 @@ void vtkDataSetAttributes::FieldList::InitializeFieldList(
this->Fields = new char*[this->NumberOfFields];
this->FieldTypes = new int [this->NumberOfFields];
this->FieldComponents = new int [this->NumberOfFields];
this->FieldComponentsNames = new vtkComponentNames*[ this->NumberOfFields ];
this->FieldComponentsNames =
new vtkDataSetAttributes::vtkInternalComponentNames*[this->NumberOfFields];
this->FieldIndices = new int [this->NumberOfFields];
this->LUT = new vtkLookupTable* [this->NumberOfFields];
this->FieldInformation = new vtkInformation* [this->NumberOfFields];
......@@ -1866,7 +1858,9 @@ void vtkDataSetAttributes::FieldList::GrowBy(unsigned int delta)
char** newFields = new char*[new_size];
int* newFieldTypes = new int[new_size];
int* newFieldComponents = new int [new_size];
vtkComponentNames** newFieldComponentsNames = new vtkComponentNames* [ new_size ];
vtkDataSetAttributes::vtkInternalComponentNames** newFieldComponentsNames
= new vtkDataSetAttributes::vtkInternalComponentNames* [ new_size ];
int* newFieldIndices = new int [new_size];
vtkLookupTable** newLUT = new vtkLookupTable* [new_size];
vtkInformation** newFieldInformation = new vtkInformation* [new_size];
......@@ -2101,7 +2095,8 @@ void vtkDataSetAttributes::FieldList::SetField(
//so we unallocate correctly
if ( !this->FieldComponentsNames[index] )
{
this->FieldComponentsNames[index] = new vtkComponentNames();
this->FieldComponentsNames[index] =
new vtkDataSetAttributes::vtkInternalComponentNames();
}
//delete the array of component names
......
......@@ -598,6 +598,7 @@ public:
// This public class is used to perform set operations, other misc.
// operations on fields. For example, vtkAppendFilter uses it to
// determine which attributes the input datasets share in common.
class vtkInternalComponentNames;
class VTK_FILTERING_EXPORT FieldList
{
public:
......@@ -646,10 +647,11 @@ public:
char** Fields; // the names of the fields
int *FieldTypes; // the types of the fields
int *FieldComponents; // the number of components in field
int *FieldIndices; // output data array index
vtkComponentNames **FieldComponentsNames; // the name for each component in the field
int *FieldIndices; // output data array index
vtkLookupTable **LUT; // luts associated with each array
vtkInformation **FieldInformation; // Information map associated with each array
vtkInternalComponentNames **FieldComponentsNames; // the name for each component in the field
vtkIdType NumberOfTuples; // a running total of values
......@@ -662,8 +664,7 @@ public:
int NumberOfDSAIndices;
int CurrentInput;
};
};
//ETX
};
......
......@@ -25,7 +25,7 @@
#include "vtkPolyData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
vtkCxxRevisionMacro(vtkAppendPolyData, "1.108");
vtkCxxRevisionMacro(vtkAppendPolyData, "1.109");
vtkStandardNewMacro(vtkAppendPolyData);
//----------------------------------------------------------------------------
......@@ -287,6 +287,7 @@ int vtkAppendPolyData::ExecuteAppend(vtkPolyData* output,
outputPD->CopyScalarsOff();
newPtScalars = inDA->NewInstance();
newPtScalars->SetNumberOfComponents(inDA->GetNumberOfComponents());
newPtScalars->CopyComponentNames( inDA );
newPtScalars->SetName(inDA->GetName());
newPtScalars->SetNumberOfTuples(numPts);
if (inDA->HasInformation())
......@@ -300,6 +301,7 @@ int vtkAppendPolyData::ExecuteAppend(vtkPolyData* output,
outputPD->CopyVectorsOff();
newPtVectors = inDA->NewInstance();
newPtVectors->SetNumberOfComponents(inDA->GetNumberOfComponents());
newPtVectors->CopyComponentNames( inDA );
newPtVectors->SetName(inDA->GetName());
newPtVectors->SetNumberOfTuples(numPts);
if (inDA->HasInformation())
......@@ -313,6 +315,7 @@ int vtkAppendPolyData::ExecuteAppend(vtkPolyData* output,
outputPD->CopyTensorsOff();
newPtTensors = inDA->NewInstance();
newPtTensors->SetNumberOfComponents(inDA->GetNumberOfComponents());
newPtTensors->CopyComponentNames( inDA );
newPtTensors->SetName(inDA->GetName());
newPtTensors->SetNumberOfTuples(numPts);
if (inDA->HasInformation())
......@@ -326,6 +329,7 @@ int vtkAppendPolyData::ExecuteAppend(vtkPolyData* output,
outputPD->CopyNormalsOff();
newPtNormals = inDA->NewInstance();
newPtNormals->SetNumberOfComponents(inDA->GetNumberOfComponents());
newPtNormals->CopyComponentNames( inDA );
newPtNormals->SetName(inDA->GetName());
newPtNormals->SetNumberOfTuples(numPts);
if (inDA->HasInformation())
......@@ -339,6 +343,7 @@ int vtkAppendPolyData::ExecuteAppend(vtkPolyData* output,
outputPD->CopyTCoordsOff();
newPtTCoords = inDA->NewInstance();
newPtTCoords->SetNumberOfComponents(inDA->GetNumberOfComponents());
newPtTCoords->CopyComponentNames( inDA );
newPtTCoords->SetName(inDA->GetName());
newPtTCoords->SetNumberOfTuples(numPts);
if (inDA->HasInformation())
......
......@@ -44,7 +44,7 @@
#include <vtkstd/algorithm>
//=============================================================================
vtkCxxRevisionMacro(vtkTemporalStatistics, "1.7");
vtkCxxRevisionMacro(vtkTemporalStatistics, "1.8");
vtkStandardNewMacro(vtkTemporalStatistics);
//=============================================================================
......@@ -460,12 +460,8 @@ void vtkTemporalStatistics::InitializeArray(vtkDataArray *array,
newArray->SetName(vtkTemporalStatisticsMangleName(array->GetName(),
STANDARD_DEVIATION_SUFFIX));
newArray->SetNumberOfComponents(array->GetNumberOfComponents());
//add support for component names
for ( int i=0; i < array->GetNumberOfComponents(); ++i )
{
newArray->SetComponentName( i, array->GetComponentName(i) );
}
newArray->SetNumberOfComponents(array->GetNumberOfComponents());
newArray->CopyComponentNames( array );
newArray->SetNumberOfTuples(array->GetNumberOfTuples());
switch (array->GetDataType())
......
......@@ -36,7 +36,7 @@
#include <assert.h>
#include <locale> // C++ locale
vtkCxxRevisionMacro(vtkXMLReader, "1.58");
vtkCxxRevisionMacro(vtkXMLReader, "1.59");
//-----------------------------------------------------------------------------
static void ReadStringVersion(const char* version, int& major, int& minor)
{
......@@ -691,33 +691,31 @@ vtkAbstractArray* vtkXMLReader::CreateArray(vtkXMLDataElement* da)
vtkAbstractArray* array = vtkAbstractArray::CreateArray(dataType);
array->SetName(da->GetAttribute("Name"));
//this needs to be zero since GetScalarAttribute will not fill it
//with a value if da does not have a NumberOfComponents
int components = 0;
if(da->GetScalarAttribute("NumberOfComponents", components))
{
array->SetNumberOfComponents(components);
}
//determine what component names have been saved in the file.
const char* compName = NULL;
vtksys_ios::ostringstream buff;
for ( int i=0; i < components && i < 10; ++i )
{
//get the component names
buff << "ComponentName" << i;
compName = da->GetAttribute( buff.str().c_str() );
if ( compName )
{
//detected a component name, add it
array->SetComponentName( i ,compName );
compName=NULL;
}
buff.str("");
buff.clear();
}
//determine what component names have been saved in the file.
const char* compName = NULL;
vtksys_ios::ostringstream buff;
for ( int i=0; i < components && i < 10; ++i )
{
//get the component names
buff << "ComponentName" << i;
compName = da->GetAttribute( buff.str().c_str() );
if ( compName )
{
//detected a component name, add it
array->SetComponentName( i ,compName );
compName=NULL;
}
buff.str("");
buff.clear();
}
}
// Scan/load for vtkInformationKey data.
int nElements=da->GetNumberOfNestedElements();
for (int i=0; i<nElements; ++i)
......
......@@ -214,7 +214,7 @@ int vtkXMLWriterWriteBinaryDataBlocks(vtkXMLWriter* writer,
}
//*****************************************************************************
vtkCxxRevisionMacro(vtkXMLWriter, "1.79");
vtkCxxRevisionMacro(vtkXMLWriter, "1.80");
vtkCxxSetObjectMacro(vtkXMLWriter, Compressor, vtkDataCompressor);
//----------------------------------------------------------------------------
vtkXMLWriter::vtkXMLWriter()
......@@ -1804,18 +1804,18 @@ void vtkXMLWriter::WriteArrayHeader(vtkAbstractArray* a, vtkIndent indent,
{
this->WriteScalarAttribute("NumberOfComponents",
a->GetNumberOfComponents());
vtkstd::ostringstream buff;
vtkstd::string compNameBase = "ComponentName";
const char* compName;
vtksys_ios::ostringstream buff;
const char* compName = NULL;
for ( int i=0; i < a->GetNumberOfComponents(); ++i )
{
//get the component names
buff << compNameBase << i;
buff << "ComponentName" << i;
compName = a->GetComponentName( i );
if ( compName )
{
this->WriteStringAttribute( buff.str().c_str(), compName );
compName = NULL;
}
buff.str("");
buff.clear();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment